1: %%
    2: %% %CopyrightBegin%
    3: %%
    4: %% Copyright Ericsson AB 1999-2013. All Rights Reserved.
    5: %%
    6: %% The contents of this file are subject to the Erlang Public License,
    7: %% Version 1.1, (the "License"); you may not use this file except in
    8: %% compliance with the License. You should have received a copy of the
    9: %% Erlang Public License along with this software. If not, it can be
   10: %% retrieved online at http://www.erlang.org/.
   11: %%
   12: %% Software distributed under the License is distributed on an "AS IS"
   13: %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
   14: %% the License for the specific language governing rights and limitations
   15: %% under the License.
   16: %%
   17: %% %CopyrightEnd%
   18: %%
   19: -module(old_crypto_SUITE).
   20: 
   21: -include_lib("test_server/include/test_server.hrl").
   22: 
   23: -export([all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1, init_per_group/2,end_per_group/2, 
   24: 	 init_per_testcase/2,
   25: 	 end_per_testcase/2,
   26: 	 info/1,
   27: 	 link_test/1,
   28: 	 md5/1,
   29: 	 md5_update/1,
   30: 	 md4/1,
   31: 	 md4_update/1,
   32: 	 sha/1,
   33: 	 sha_update/1,
   34:          hmac_update_sha/1,
   35:          hmac_update_sha_n/1,
   36:          hmac_update_sha256/1,
   37:          hmac_update_sha512/1,
   38:          hmac_update_md5/1,
   39:          hmac_update_md5_io/1,
   40:          hmac_update_md5_n/1,
   41:          hmac_rfc2202/1,
   42: 	 hmac_rfc4231_sha224/1,
   43: 	 hmac_rfc4231_sha256/1,
   44: 	 hmac_rfc4231_sha384/1,
   45: 	 hmac_rfc4231_sha512/1,
   46:    ripemd160/1,
   47:    ripemd160_update/1,
   48: 	 sha256/1,
   49: 	 sha256_update/1,
   50: 	 sha512/1,
   51: 	 sha512_update/1,
   52: 	 md5_mac/1,
   53: 	 md5_mac_io/1,
   54: 	 des_cbc/1,
   55: 	 des_cbc_iter/1,
   56: 	 des_cfb/1,
   57: 	 des_cfb_iter/1,
   58: 	 des_ecb/1,
   59: 	 des3_cbc/1,
   60: 	 des3_cfb/1,
   61: 	 rc2_cbc/1,
   62: 	 aes_cfb/1,
   63: 	 aes_cbc/1,
   64: 	 aes_cbc_iter/1,
   65: 	 aes_ctr/1,
   66: 	 aes_ctr_stream/1,
   67: 	 mod_exp_test/1,
   68: 	 rand_uniform_test/1,
   69: 	 strong_rand_test/1,
   70: 	 rsa_verify_test/1,
   71: 	 dsa_verify_test/1,
   72: 	 rsa_sign_test/1,
   73: 	 rsa_sign_hash_test/1,
   74: 	 dsa_sign_test/1,	 
   75: 	 dsa_sign_hash_test/1,
   76: 	 rsa_encrypt_decrypt/1,
   77: 	 dh/1,
   78: 	 srp3/1, srp6/1, srp6a/1,
   79: 	 ec/1,
   80: 	 exor_test/1,
   81: 	 rc4_test/1,
   82: 	 rc4_stream_test/1,
   83: 	 blowfish_cfb64/1,
   84: 	 smp/1]).
   85: 
   86: -export([hexstr2bin/1]).
   87: 
   88: suite() -> [{ct_hooks,[ts_install_cth]}].
   89: 
   90: all() -> 
   91:     [link_test, {group, info}].
   92: 
   93: groups() ->
   94:     [{info, [sequence],[info, {group, rest}]},
   95:      {rest, [],
   96:       [md5, md5_update, md4, md4_update, md5_mac,
   97:        md5_mac_io, ripemd160, ripemd160_update, sha, sha_update,
   98:        sha256, sha256_update, sha512, sha512_update,
   99:        hmac_update_sha, hmac_update_sha_n, hmac_update_sha256, hmac_update_sha512,
  100:        hmac_update_md5_n, hmac_update_md5_io, hmac_update_md5,
  101:        hmac_rfc2202, hmac_rfc4231_sha224, hmac_rfc4231_sha256,
  102:        hmac_rfc4231_sha384, hmac_rfc4231_sha512,
  103:        des_cbc, aes_cfb, aes_cbc,
  104:        des_cfb, des_cfb_iter, des3_cbc, des3_cfb, rc2_cbc,
  105:        aes_cbc_iter, aes_ctr, aes_ctr_stream, des_cbc_iter, des_ecb,
  106:        rand_uniform_test, strong_rand_test,
  107:        rsa_verify_test, dsa_verify_test, rsa_sign_test,
  108:        rsa_sign_hash_test, dsa_sign_test, dsa_sign_hash_test,
  109:        rsa_encrypt_decrypt, dh, srp3, srp6, srp6a, ec, exor_test,
  110:        rc4_test, rc4_stream_test, mod_exp_test, blowfish_cfb64,
  111:        smp]}].
  112: 
  113: init_per_suite(Config) ->
  114:     Config.
  115: 
  116: end_per_suite(_Config) ->
  117:     ok.
  118: 
  119: init_per_group(_GroupName, Config) ->
  120:     Config.
  121: 
  122: end_per_group(_GroupName, Config) ->
  123:     Config.
  124: 
  125: init_per_testcase(info, Config) ->
  126:     Config;
  127: init_per_testcase(_Name,Config) ->
  128:     io:format("init_per_testcase\n"),
  129:     ?line crypto:start(),
  130:     Config.
  131: 
  132: end_per_testcase(info, Config) ->
  133:     Config;
  134: end_per_testcase(_Name,Config) ->
  135:     io:format("end_per_testcase\n"),
  136:     ?line crypto:stop(),
  137:     Config.
  138: 
  139: %%
  140: %%
  141: link_test(doc) ->
  142:     ["Test that the library is statically linked to libcrypto.a."];
  143: link_test(suite) ->
  144:     [];
  145: link_test(Config) when is_list(Config) ->
  146:     ?line case os:type() of
  147: 	      {unix,darwin} -> {skipped,"Darwin cannot link statically"};
  148: 	      {unix,_} -> link_test_1();
  149: 	      _ -> {skip,"Only runs on Unix"}
  150: 	  end.
  151: 
  152: link_test_1() ->    
  153:     ?line CryptoPriv = code:priv_dir(crypto),
  154:     ?line Wc = filename:join([CryptoPriv,"lib","crypto.*"]),
  155:     ?line case filelib:wildcard(Wc) of
  156: 	      [] -> {skip,"Didn't find the crypto driver"};
  157: 	      [Drv] -> link_test_2(Drv)
  158: 	  end.
  159: 
  160: link_test_2(Drv) ->
  161:     case ldd_program() of
  162: 	none ->
  163: 	    {skip,"No ldd-like program found"};
  164: 	Ldd ->
  165: 	    Cmd = Ldd ++ " " ++ Drv,
  166: 	    Libs = os:cmd(Cmd),
  167: 	    io:format("~p\n", [Libs]),
  168: 	    case string:str(Libs, "libcrypto") of
  169: 		0 -> 
  170: 		    case ?t:is_commercial() of
  171: 			true ->
  172: 			    ?t:fail({libcrypto,statically_linked});
  173: 			false ->
  174: 			    {comment,"Statically linked (OK for open-source platform)"}
  175: 		    end;
  176: 		_ ->
  177: 		    ok
  178: 	    end
  179:     end.
  180: 
  181: ldd_program() ->
  182:     case os:find_executable("ldd") of
  183: 	false ->
  184: 	    case os:type() of
  185: 		{unix,darwin} ->
  186: 		    case os:find_executable("otool") of
  187: 			false -> none;
  188: 			Otool -> Otool ++ " -L"
  189: 		    end
  190: 	    end;
  191:  	Ldd when is_list(Ldd) -> Ldd
  192:     end.
  193: 
  194: 
  195: 
  196: info(doc) ->
  197:     ["Call the info function."];
  198: info(suite) ->
  199:     [];
  200: info(Config) when is_list(Config) ->
  201:     case {code:lib_dir(crypto),?t:is_commercial()} of
  202: 	{{error,bad_name},false} ->
  203: 	    {skip,"Missing crypto application"};
  204: 	{_,_} ->
  205: 	    ?line crypto:start(),
  206: 	    ?line Info = crypto:info(),
  207: 	    ?line Exports = lists:usort([F || {F,_} <- crypto:module_info(exports)]),
  208: 	    ?line [] = Info -- Exports,
  209: 	    ?line NotInInfo = Exports -- Info,
  210: 	    io:format("NotInInfo = ~p\n", [NotInInfo]),
  211: 	    %% BlackList = lists:sort([des_ede3_cbc_decrypt, des_ede3_cbc_encrypt,
  212: 	    %% 			    dh_check, dh_generate_parameters,
  213: 	    %% 			    module_info, start, stop, version]),
  214: 	    %% ?line BlackList = NotInInfo,
  215: 
  216: 	    ?line InfoLib = crypto:info_lib(),
  217: 	    ?line [_|_] = InfoLib,
  218: 	    F = fun([{Name,VerN,VerS}|T],Me) ->
  219: 			?line true = is_binary(Name),
  220: 			?line true = is_integer(VerN),
  221: 			?line true = is_binary(VerS),
  222: 			Me(T,Me);
  223: 		   ([],_) ->
  224: 			ok
  225: 		end,
  226: 	    ?line F(InfoLib,F),
  227: 	    ?line crypto:stop()
  228:      end.
  229: 
  230: %%
  231: %%
  232: md5(doc) ->
  233:     ["Generate MD5 message digests and check the result. Examples are "
  234:      "from RFC-1321."];
  235: md5(suite) ->
  236:     [];
  237: md5(Config) when is_list(Config) ->
  238:     ?line m(crypto:md5(""), 
  239: 		hexstr2bin("d41d8cd98f00b204e9800998ecf8427e")),
  240:     ?line m(crypto:md5("a"), 
  241: 		hexstr2bin("0cc175b9c0f1b6a831c399e269772661")),
  242:     ?line m(crypto:md5("abc"), 
  243: 		hexstr2bin("900150983cd24fb0d6963f7d28e17f72")),
  244:     ?line m(crypto:md5("message digest"),
  245: 		hexstr2bin("f96b697d7cb7938d525a2f31aaf161d0")),
  246:     ?line m(crypto:md5("abcdefghijklmnopqrstuvwxyz"),
  247: 	    hexstr2bin("c3fcd3d76192e4007dfb496cca67e13b")),
  248:     ?line m(crypto:md5("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  249: 		     "0123456789"),  
  250: 	    hexstr2bin("d174ab98d277d9f5a5611c2c9f419d9f")),
  251:     ?line m(crypto:md5("12345678901234567890123456789012345678901234567890"
  252: 		     "123456789012345678901234567890"),
  253: 	    hexstr2bin("57edf4a22be3c955ac49da2e2107b67a")).
  254: 
  255: %%
  256: %%
  257: md5_update(doc) ->
  258:     ["Generate MD5 message using md5_init, md5_update, and md5_final, and"
  259:      "check the result. Examples are from RFC-1321."];
  260: md5_update(suite) ->
  261:     [];
  262: md5_update(Config) when is_list(Config) ->
  263:     ?line Ctx = crypto:md5_init(),
  264:     ?line Ctx1 = crypto:md5_update(Ctx, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"),
  265:     ?line Ctx2 = crypto:md5_update(Ctx1, "abcdefghijklmnopqrstuvwxyz"
  266: 				   "0123456789"),
  267:     ?line m(crypto:md5_final(Ctx2),  
  268: 	    hexstr2bin("d174ab98d277d9f5a5611c2c9f419d9f")).
  269: 
  270: %%
  271: %%
  272: md4(doc) ->
  273:     ["Generate MD4 message digests and check the result. Examples are "
  274:      "from RFC-1321."];
  275: md4(suite) ->
  276:     [];
  277: md4(Config) when is_list(Config) ->
  278:     ?line m(crypto:md4(""), 
  279: 	    hexstr2bin("31d6cfe0d16ae931b73c59d7e0c089c0")),
  280:     ?line m(crypto:md4("a"), 
  281: 	    hexstr2bin("bde52cb31de33e46245e05fbdbd6fb24")),
  282:     ?line m(crypto:md4("abc"), 
  283: 	    hexstr2bin("a448017aaf21d8525fc10ae87aa6729d")),
  284:     ?line m(crypto:md4("message digest"),
  285: 	    hexstr2bin("d9130a8164549fe818874806e1c7014b")),
  286:     ?line m(crypto:md4("abcdefghijklmnopqrstuvwxyz"),
  287: 	    hexstr2bin("d79e1c308aa5bbcdeea8ed63df412da9")),
  288:     ?line m(crypto:md4("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  289: 		       "0123456789"),  
  290: 	    hexstr2bin("043f8582f241db351ce627e153e7f0e4")),
  291:     ?line m(crypto:md4("12345678901234567890123456789012345678901234567890"
  292: 		       "123456789012345678901234567890"),
  293: 	    hexstr2bin("e33b4ddc9c38f2199c3e7b164fcc0536")).
  294: 
  295: %%
  296: %%
  297: md4_update(doc) ->
  298:     ["Generate MD5 message using md5_init, md5_update, and md5_final, and"
  299:      "check the result. Examples are from RFC-1321."];
  300: md4_update(suite) ->
  301:     [];
  302: md4_update(Config) when is_list(Config) ->
  303:     ?line Ctx = crypto:md4_init(),
  304:     ?line Ctx1 = crypto:md4_update(Ctx, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"),
  305:     ?line Ctx2 = crypto:md4_update(Ctx1, "abcdefghijklmnopqrstuvwxyz"
  306: 				   "0123456789"),
  307:     ?line m(crypto:md4_final(Ctx2),  
  308: 	    hexstr2bin("043f8582f241db351ce627e153e7f0e4")).
  309: 
  310: %%
  311: %%
  312: sha(doc) ->
  313:     ["Generate SHA message digests and check the result. Examples are "
  314:      "from FIPS-180-1."];
  315: sha(suite) ->
  316:     [];
  317: sha(Config) when is_list(Config) ->
  318:     ?line m(crypto:sha("abc"),
  319: 	     hexstr2bin("A9993E364706816ABA3E25717850C26C9CD0D89D")),
  320:     ?line m(crypto:sha("abcdbcdecdefdefgefghfghighijhijkijkljklmklm"
  321: 		       "nlmnomnopnopq"), 
  322: 		hexstr2bin("84983E441C3BD26EBAAE4AA1F95129E5E54670F1")).
  323: 
  324: 
  325: %%
  326: hmac_update_sha_n(doc) ->
  327:     ["Request a larger-than-allowed SHA1 HMAC using hmac_init, hmac_update, and hmac_final_n. "
  328:      "Expected values for examples are generated using crypto:sha_mac." ];
  329: hmac_update_sha_n(suite) ->
  330:     [];
  331: hmac_update_sha_n(Config) when is_list(Config) ->
  332:     ?line Key = hexstr2bin("00010203101112132021222330313233"
  333:                            "04050607141516172425262734353637"
  334:                            "08090a0b18191a1b28292a2b38393a3b"
  335:                            "0c0d0e0f1c1d1e1f2c2d2e2f3c3d3e3f"),
  336:     ?line Data = "Sampl",
  337:     ?line Data2 = "e #1",
  338:     ?line Ctx = crypto:hmac_init(sha, Key),
  339:     ?line Ctx2 = crypto:hmac_update(Ctx, Data),
  340:     ?line Ctx3 = crypto:hmac_update(Ctx2, Data2),
  341:     ?line Mac = crypto:hmac_final_n(Ctx3, 1024),
  342:     ?line Exp = crypto:sha_mac(Key, lists:flatten([Data, Data2])),
  343:     ?line m(Exp, Mac),
  344:     ?line m(size(Exp), size(Mac)).
  345: 
  346: 
  347: hmac_update_sha(doc) ->
  348:     ["Generate an SHA1 HMAC using hmac_init, hmac_update, and hmac_final. "
  349:      "Expected values for examples are generated using crypto:sha_mac." ];
  350: hmac_update_sha(suite) ->
  351:     [];
  352: hmac_update_sha(Config) when is_list(Config) ->
  353:     ?line Key = hexstr2bin("00010203101112132021222330313233"
  354:                            "04050607141516172425262734353637"
  355:                            "08090a0b18191a1b28292a2b38393a3b"
  356:                            "0c0d0e0f1c1d1e1f2c2d2e2f3c3d3e3f"),
  357:     ?line Data = "Sampl",
  358:     ?line Data2 = "e #1",
  359:     ?line Ctx = crypto:hmac_init(sha, Key),
  360:     ?line Ctx2 = crypto:hmac_update(Ctx, Data),
  361:     ?line Ctx3 = crypto:hmac_update(Ctx2, Data2),
  362:     ?line Mac = crypto:hmac_final(Ctx3),
  363:     ?line Exp = crypto:hmac(sha, Key, lists:flatten([Data, Data2])),
  364:     ?line m(Exp, Mac).
  365: 
  366: hmac_update_sha256(doc) ->
  367:     ["Generate an SHA256 HMAC using hmac_init, hmac_update, and hmac_final. "
  368:      "Expected values for examples are generated using crypto:sha256_mac." ];
  369: hmac_update_sha256(suite) ->
  370:     [];
  371: hmac_update_sha256(Config) when is_list(Config) ->
  372:     if_supported(sha256, fun() -> hmac_update_sha256_do() end).
  373: 
  374: hmac_update_sha256_do() ->
  375:     ?line Key = hexstr2bin("00010203101112132021222330313233"
  376:                            "04050607141516172425262734353637"
  377:                            "08090a0b18191a1b28292a2b38393a3b"
  378:                            "0c0d0e0f1c1d1e1f2c2d2e2f3c3d3e3f"),
  379:     ?line Data = "Sampl",
  380:     ?line Data2 = "e #1",
  381:     ?line Ctx = crypto:hmac_init(sha256, Key),
  382:     ?line Ctx2 = crypto:hmac_update(Ctx, Data),
  383:     ?line Ctx3 = crypto:hmac_update(Ctx2, Data2),
  384:     ?line Mac = crypto:hmac_final(Ctx3),
  385:     ?line Exp = crypto:hmac(sha256, Key, lists:flatten([Data, Data2])),
  386:     ?line m(Exp, Mac).
  387: 
  388: hmac_update_sha512(doc) ->
  389:     ["Generate an SHA512 HMAC using hmac_init, hmac_update, and hmac_final. "
  390:      "Expected values for examples are generated using crypto:sha512_mac." ];
  391: hmac_update_sha512(suite) ->
  392:     [];
  393: hmac_update_sha512(Config) when is_list(Config) ->
  394:     if_supported(sha512, fun() -> hmac_update_sha512_do() end).
  395: 
  396: hmac_update_sha512_do() ->
  397:     ?line Key = hexstr2bin("00010203101112132021222330313233"
  398:                            "04050607141516172425262734353637"
  399:                            "08090a0b18191a1b28292a2b38393a3b"
  400:                            "0c0d0e0f1c1d1e1f2c2d2e2f3c3d3e3f"),
  401:     ?line Data = "Sampl",
  402:     ?line Data2 = "e #1",
  403:     ?line Ctx = crypto:hmac_init(sha512, Key),
  404:     ?line Ctx2 = crypto:hmac_update(Ctx, Data),
  405:     ?line Ctx3 = crypto:hmac_update(Ctx2, Data2),
  406:     ?line Mac = crypto:hmac_final(Ctx3),
  407:     ?line Exp = crypto:hmac(sha512, Key, lists:flatten([Data, Data2])),
  408:     ?line m(Exp, Mac).
  409:     
  410: hmac_update_md5(doc) ->
  411:     ["Generate an MD5 HMAC using hmac_init, hmac_update, and hmac_final. "
  412:      "Expected values for examples are generated using crypto:md5_mac." ];
  413: hmac_update_md5(suite) ->
  414:     [];
  415: hmac_update_md5(Config) when is_list(Config) ->
  416:     % ?line Key2 = ["A fine speach", "by a fine man!"],
  417:     Key2 = "A fine speach by a fine man!",
  418:     ?line Long1 = "Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal.",
  419:     ?line Long2 = "Now we are engaged in a great civil war, testing whether that nation, or any nation, so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this.",
  420:     ?line Long3 = "But, in a larger sense, we can not dedicate, we can not consecrate, we can not hallow this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us-that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion that we here highly resolve that these dead shall not have died in vain-that this nation, under God, shall have a new birth of freedom-and that government of the people, by the people, for the people, shall not perish from the earth.",
  421:     ?line CtxA = crypto:hmac_init(md5, Key2),
  422:     ?line CtxB = crypto:hmac_update(CtxA, Long1),
  423:     ?line CtxC = crypto:hmac_update(CtxB, Long2),
  424:     ?line CtxD = crypto:hmac_update(CtxC, Long3),
  425:     ?line Mac2 = crypto:hmac_final(CtxD),
  426:     ?line Exp2 = crypto:md5_mac(Key2, lists:flatten([Long1, Long2, Long3])), 
  427:     ?line m(Exp2, Mac2).
  428: 
  429: hmac_rfc2202(doc) ->
  430:     ["Generate an HMAC using hmac, md5_mac, and sha_mac."
  431:      "Test vectors are taken from RFC-2202."];
  432: hmac_rfc2202(suite) ->
  433:     [];
  434: hmac_rfc2202(Config) when is_list(Config) ->
  435:     hmac_rfc2202_md5(),
  436:     hmac_rfc2202_sha().
  437: 
  438: hmac_rfc2202_md5() ->
  439:     %% Test case 1
  440:     Case1Key = binary:copy(<<16#0b>>, 16),
  441:     Case1Data = <<"Hi There">>,
  442:     Case1Exp = hexstr2bin("9294727a3638bb1c13f48ef8158bfc9d"),
  443: 
  444:     ?line Case1Mac_1 = crypto:md5_mac(Case1Key, Case1Data),
  445:     ?line Case1Mac_2 = crypto:hmac(md5, Case1Key, Case1Data),
  446:     ?line m(Case1Exp, Case1Mac_1),
  447:     ?line m(Case1Exp, Case1Mac_2),
  448: 
  449:     %% Test case 2
  450:     Case2Key = <<"Jefe">>,
  451:     Case2Data = <<"what do ya want for nothing?">>,
  452:     Case2Exp = hexstr2bin("750c783e6ab0b503eaa86e310a5db738"),
  453: 
  454:     ?line Case2Mac_1 = crypto:md5_mac(Case2Key, Case2Data),
  455:     ?line Case2Mac_2 = crypto:hmac(md5, Case2Key, Case2Data),
  456:     ?line m(Case2Exp, Case2Mac_1),
  457:     ?line m(Case2Exp, Case2Mac_2),
  458: 
  459:     %% Test case 3
  460:     Case3Key = binary:copy(<<16#aa>>, 16),
  461:     Case3Data = binary:copy(<<16#dd>>, 50),
  462:     Case3Exp = hexstr2bin("56be34521d144c88dbb8c733f0e8b3f6"),
  463: 
  464:     ?line Case3Mac_1 = crypto:md5_mac(Case3Key, Case3Data),
  465:     ?line Case3Mac_2 = crypto:hmac(md5, Case3Key, Case3Data),
  466:     ?line m(Case3Exp, Case3Mac_1),
  467:     ?line m(Case3Exp, Case3Mac_2),
  468: 
  469:     %% Test case 4
  470:     Case4Key = list_to_binary(lists:seq(1, 16#19)),
  471:     Case4Data = binary:copy(<<16#cd>>, 50),
  472:     Case4Exp = hexstr2bin("697eaf0aca3a3aea3a75164746ffaa79"),
  473: 
  474:     ?line Case4Mac_1 = crypto:md5_mac(Case4Key, Case4Data),
  475:     ?line Case4Mac_2 = crypto:hmac(md5, Case4Key, Case4Data),
  476:     ?line m(Case4Exp, Case4Mac_1),
  477:     ?line m(Case4Exp, Case4Mac_2),
  478: 
  479:     %% Test case 5
  480:     Case5Key = binary:copy(<<16#0c>>, 16),
  481:     Case5Data = "Test With Truncation",
  482:     Case5Exp = hexstr2bin("56461ef2342edc00f9bab995690efd4c"),
  483:     Case5Exp96 = hexstr2bin("56461ef2342edc00f9bab995"),
  484: 
  485:     ?line Case5Mac_1 = crypto:md5_mac(Case5Key, Case5Data),
  486:     ?line Case5Mac_2 = crypto:hmac(md5, Case5Key, Case5Data),
  487:     ?line Case5Mac96_1 = crypto:md5_mac_96(Case5Key, Case5Data),
  488:     ?line Case5Mac96_2 = crypto:hmac(md5, Case5Key, Case5Data, 12),
  489:     ?line m(Case5Exp, Case5Mac_1),
  490:     ?line m(Case5Exp, Case5Mac_2),
  491:     ?line m(Case5Exp96, Case5Mac96_1),
  492:     ?line m(Case5Exp96, Case5Mac96_2),
  493: 
  494:     %% Test case 6
  495:     Case6Key = binary:copy(<<16#aa>>, 80),
  496:     Case6Data = <<"Test Using Larger Than Block-Size Key - Hash Key First">>,
  497:     Case6Exp = hexstr2bin("6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd"),
  498: 
  499:     ?line Case6Mac_1 = crypto:md5_mac(Case6Key, Case6Data),
  500:     ?line Case6Mac_2 = crypto:hmac(md5, Case6Key, Case6Data),
  501:     ?line m(Case6Exp, Case6Mac_1),
  502:     ?line m(Case6Exp, Case6Mac_2),
  503: 
  504:     %% Test case 7
  505:     Case7Key = binary:copy(<<16#aa>>, 80),
  506:     Case7Data = <<"Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data">>,
  507:     Case7Exp = hexstr2bin("6f630fad67cda0ee1fb1f562db3aa53e"),
  508: 
  509:     ?line Case7Mac_1 = crypto:md5_mac(Case7Key, Case7Data),
  510:     ?line Case7Mac_2 = crypto:hmac(md5, Case7Key, Case7Data),
  511:     ?line m(Case7Exp, Case7Mac_1),
  512:     ?line m(Case7Exp, Case7Mac_2).
  513: 
  514: hmac_rfc2202_sha() ->
  515:     %% Test case 1
  516:     Case1Key = binary:copy(<<16#0b>>, 20),
  517:     Case1Data = <<"Hi There">>,
  518:     Case1Exp = hexstr2bin("b617318655057264e28bc0b6fb378c8ef146be00"),
  519: 
  520:     ?line Case1Mac_1 = crypto:sha_mac(Case1Key, Case1Data),
  521:     ?line Case1Mac_2 = crypto:hmac(sha, Case1Key, Case1Data),
  522:     ?line m(Case1Exp, Case1Mac_1),
  523:     ?line m(Case1Exp, Case1Mac_2),
  524: 
  525:     %% Test case 2
  526:     Case2Key = <<"Jefe">>,
  527:     Case2Data = <<"what do ya want for nothing?">>,
  528:     Case2Exp = hexstr2bin("effcdf6ae5eb2fa2d27416d5f184df9c259a7c79"),
  529: 
  530:     ?line Case2Mac_1 = crypto:sha_mac(Case2Key, Case2Data),
  531:     ?line Case2Mac_2 = crypto:hmac(sha, Case2Key, Case2Data),
  532:     ?line m(Case2Exp, Case2Mac_1),
  533:     ?line m(Case2Exp, Case2Mac_2),
  534: 
  535:     %% Test case 3
  536:     Case3Key = binary:copy(<<16#aa>>, 20),
  537:     Case3Data = binary:copy(<<16#dd>>, 50),
  538:     Case3Exp = hexstr2bin("125d7342b9ac11cd91a39af48aa17b4f63f175d3"),
  539: 
  540:     ?line Case3Mac_1 = crypto:sha_mac(Case3Key, Case3Data),
  541:     ?line Case3Mac_2 = crypto:hmac(sha, Case3Key, Case3Data),
  542:     ?line m(Case3Exp, Case3Mac_1),
  543:     ?line m(Case3Exp, Case3Mac_2),
  544: 
  545:     %% Test case 4
  546:     Case4Key = list_to_binary(lists:seq(1, 16#19)),
  547:     Case4Data = binary:copy(<<16#cd>>, 50),
  548:     Case4Exp = hexstr2bin("4c9007f4026250c6bc8414f9bf50c86c2d7235da"),
  549: 
  550:     ?line Case4Mac_1 = crypto:sha_mac(Case4Key, Case4Data),
  551:     ?line Case4Mac_2 = crypto:hmac(sha, Case4Key, Case4Data),
  552:     ?line m(Case4Exp, Case4Mac_1),
  553:     ?line m(Case4Exp, Case4Mac_2),
  554: 
  555:     %% Test case 5
  556:     Case5Key = binary:copy(<<16#0c>>, 20),
  557:     Case5Data = "Test With Truncation",
  558:     Case5Exp = hexstr2bin("4c1a03424b55e07fe7f27be1d58bb9324a9a5a04"),
  559:     Case5Exp96 = hexstr2bin("4c1a03424b55e07fe7f27be1"),
  560: 
  561:     ?line Case5Mac_1 = crypto:sha_mac(Case5Key, Case5Data),
  562:     ?line Case5Mac_2 = crypto:hmac(sha, Case5Key, Case5Data),
  563:     ?line Case5Mac96_1 = crypto:sha_mac_96(Case5Key, Case5Data),
  564:     ?line Case5Mac96_2 = crypto:hmac(sha, Case5Key, Case5Data, 12),
  565:     ?line m(Case5Exp, Case5Mac_1),
  566:     ?line m(Case5Exp, Case5Mac_2),
  567:     ?line m(Case5Exp96, Case5Mac96_1),
  568:     ?line m(Case5Exp96, Case5Mac96_2),
  569: 
  570:     %% Test case 6
  571:     Case6Key = binary:copy(<<16#aa>>, 80),
  572:     Case6Data = <<"Test Using Larger Than Block-Size Key - Hash Key First">>,
  573:     Case6Exp = hexstr2bin("aa4ae5e15272d00e95705637ce8a3b55ed402112"),
  574: 
  575:     ?line Case6Mac_1 = crypto:sha_mac(Case6Key, Case6Data),
  576:     ?line Case6Mac_2 = crypto:hmac(sha, Case6Key, Case6Data),
  577:     ?line m(Case6Exp, Case6Mac_1),
  578:     ?line m(Case6Exp, Case6Mac_2),
  579: 
  580:     %% Test case 7
  581:     Case7Key = binary:copy(<<16#aa>>, 80),
  582:     Case7Data = <<"Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data">>,
  583:     Case7Exp = hexstr2bin("e8e99d0f45237d786d6bbaa7965c7808bbff1a91"),
  584: 
  585:     ?line Case7Mac_1 = crypto:sha_mac(Case7Key, Case7Data),
  586:     ?line Case7Mac_2 = crypto:hmac(sha, Case7Key, Case7Data),
  587:     ?line m(Case7Exp, Case7Mac_1),
  588:     ?line m(Case7Exp, Case7Mac_2).
  589: 
  590: hmac_rfc4231_sha224(doc) ->
  591:     ["Generate an HMAC using crypto:sha224_mac, hmac, and hmac_init, hmac_update, and hmac_final. "
  592:      "Testvectors are take from RFC4231." ];
  593: hmac_rfc4231_sha224(suite) ->
  594:     [];
  595: hmac_rfc4231_sha224(Config) when is_list(Config) ->
  596:     if_supported(sha224, fun() -> hmac_rfc4231_sha224_do() end).
  597: 
  598: hmac_rfc4231_sha256(doc) ->
  599:     ["Generate an HMAC using crypto:sha256_mac, hmac, and hmac_init, hmac_update, and hmac_final. "
  600:      "Testvectors are take from RFC4231." ];
  601: hmac_rfc4231_sha256(suite) ->
  602:     [];
  603: hmac_rfc4231_sha256(Config) when is_list(Config) ->
  604:     if_supported(sha256, fun() -> hmac_rfc4231_sha256_do() end).
  605: 
  606: hmac_rfc4231_sha384(doc) ->
  607:     ["Generate an HMAC using crypto:sha384_mac, hmac, and hmac_init, hmac_update, and hmac_final. "
  608:      "Testvectors are take from RFC4231." ];
  609: hmac_rfc4231_sha384(suite) ->
  610:     [];
  611: hmac_rfc4231_sha384(Config) when is_list(Config) ->
  612:     if_supported(sha384, fun() -> hmac_rfc4231_sha384_do() end).
  613: 
  614: hmac_rfc4231_sha512(doc) ->
  615:     ["Generate an HMAC using crypto:sha512_mac, hmac, and hmac_init, hmac_update, and hmac_final. "
  616:      "Testvectors are take from RFC4231." ];
  617: hmac_rfc4231_sha512(suite) ->
  618:     [];
  619: hmac_rfc4231_sha512(Config) when is_list(Config) ->
  620:     if_supported(sha512, fun() -> hmac_rfc4231_sha512_do() end).
  621: 
  622: hmac_rfc4231_case(Hash, case1, Exp) ->
  623:     %% Test 1
  624:     Key = binary:copy(<<16#0b>>, 20),
  625:     Data = <<"Hi There">>,
  626:     hmac_rfc4231_case(Hash, Key, Data, Exp);
  627: 
  628: hmac_rfc4231_case(Hash, case2, Exp) ->
  629:     %% Test 2
  630:     Key = <<"Jefe">>,
  631:     Data = <<"what do ya want for nothing?">>,
  632:     hmac_rfc4231_case(Hash, Key, Data, Exp);
  633: 
  634: hmac_rfc4231_case(Hash, case3, Exp) ->
  635:     %% Test 3
  636:     Key = binary:copy(<<16#aa>>, 20),
  637:     Data = binary:copy(<<16#dd>>, 50),
  638:     hmac_rfc4231_case(Hash, Key, Data, Exp);
  639: 
  640: hmac_rfc4231_case(Hash, case4, Exp) ->
  641:     %% Test 4
  642:     Key = list_to_binary(lists:seq(1, 16#19)),
  643:     Data = binary:copy(<<16#cd>>, 50),
  644:     hmac_rfc4231_case(Hash, Key, Data, Exp);
  645: 
  646: hmac_rfc4231_case(Hash, case5, Exp) ->
  647:     %% Test 5
  648:     Key = binary:copy(<<16#0c>>, 20),
  649:     Data = <<"Test With Truncation">>,
  650:     hmac_rfc4231_case(Hash, Key, Data, 16, Exp);
  651: 
  652: hmac_rfc4231_case(Hash, case6, Exp) ->
  653:     %% Test 6
  654:     Key = binary:copy(<<16#aa>>, 131),
  655:     Data = <<"Test Using Larger Than Block-Size Key - Hash Key First">>,
  656:     hmac_rfc4231_case(Hash, Key, Data, Exp);
  657: 
  658: hmac_rfc4231_case(Hash, case7, Exp) ->
  659:     %% Test Case 7
  660:     Key = binary:copy(<<16#aa>>, 131),
  661:     Data = <<"This is a test using a larger than block-size key and a larger t",
  662: 	     "han block-size data. The key needs to be hashed before being use",
  663: 	     "d by the HMAC algorithm.">>,
  664:     hmac_rfc4231_case(Hash, Key, Data, Exp).
  665: 
  666: hmac_rfc4231_case(Hash, Key, Data, Exp) ->
  667:     ?line Ctx = crypto:hmac_init(Hash, Key),
  668:     ?line Ctx2 = crypto:hmac_update(Ctx, Data),
  669:     ?line Mac1 = crypto:hmac_final(Ctx2),
  670:     ?line Mac3 = crypto:hmac(Hash, Key, Data),
  671:     ?line m(Exp, Mac1),
  672:     ?line m(Exp, Mac3).
  673: 
  674: hmac_rfc4231_case(Hash, Key, Data, Trunc, Exp) ->
  675:     ?line Ctx = crypto:hmac_init(Hash, Key),
  676:     ?line Ctx2 = crypto:hmac_update(Ctx, Data),
  677:     ?line Mac1 = crypto:hmac_final_n(Ctx2, Trunc),
  678:     ?line Mac3 = crypto:hmac(Hash, Key, Data, Trunc),
  679:     ?line m(Exp, Mac1),
  680:     ?line m(Exp, Mac3).
  681: 
  682: hmac_rfc4231_sha224_do() ->
  683:     Case1 = hexstr2bin("896fb1128abbdf196832107cd49df33f"
  684: 		       "47b4b1169912ba4f53684b22"),
  685:     Case2 = hexstr2bin("a30e01098bc6dbbf45690f3a7e9e6d0f"
  686: 		       "8bbea2a39e6148008fd05e44"),
  687:     Case3 = hexstr2bin("7fb3cb3588c6c1f6ffa9694d7d6ad264"
  688: 		       "9365b0c1f65d69d1ec8333ea"),
  689:     Case4 = hexstr2bin("6c11506874013cac6a2abc1bb382627c"
  690: 		       "ec6a90d86efc012de7afec5a"),
  691:     Case5 = hexstr2bin("0e2aea68a90c8d37c988bcdb9fca6fa8"),
  692:     Case6 = hexstr2bin("95e9a0db962095adaebe9b2d6f0dbce2"
  693: 		       "d499f112f2d2b7273fa6870e"),
  694:     Case7 = hexstr2bin("3a854166ac5d9f023f54d517d0b39dbd"
  695: 		       "946770db9c2b95c9f6f565d1"),
  696:     hmac_rfc4231_cases_do(sha224, [Case1, Case2, Case3, Case4, Case5, Case6, Case7]).
  697: 
  698: hmac_rfc4231_sha256_do() ->
  699:     Case1 = hexstr2bin("b0344c61d8db38535ca8afceaf0bf12b"
  700: 		       "881dc200c9833da726e9376c2e32cff7"),
  701:     Case2 = hexstr2bin("5bdcc146bf60754e6a042426089575c7"
  702: 		       "5a003f089d2739839dec58b964ec3843"),
  703:     Case3 = hexstr2bin("773ea91e36800e46854db8ebd09181a7"
  704: 		       "2959098b3ef8c122d9635514ced565fe"),
  705:     Case4 = hexstr2bin("82558a389a443c0ea4cc819899f2083a"
  706: 		       "85f0faa3e578f8077a2e3ff46729665b"),
  707:     Case5 = hexstr2bin("a3b6167473100ee06e0c796c2955552b"),
  708:     Case6 = hexstr2bin("60e431591ee0b67f0d8a26aacbf5b77f"
  709: 		       "8e0bc6213728c5140546040f0ee37f54"),
  710:     Case7 = hexstr2bin("9b09ffa71b942fcb27635fbcd5b0e944"
  711: 		       "bfdc63644f0713938a7f51535c3a35e2"),
  712:     hmac_rfc4231_cases_do(sha256, [Case1, Case2, Case3, Case4, Case5, Case6, Case7]).
  713: 
  714: hmac_rfc4231_sha384_do() ->
  715:     Case1 = hexstr2bin("afd03944d84895626b0825f4ab46907f"
  716: 		       "15f9dadbe4101ec682aa034c7cebc59c"
  717: 		       "faea9ea9076ede7f4af152e8b2fa9cb6"),
  718:     Case2 = hexstr2bin("af45d2e376484031617f78d2b58a6b1b"
  719: 		       "9c7ef464f5a01b47e42ec3736322445e"
  720: 		       "8e2240ca5e69e2c78b3239ecfab21649"),
  721:     Case3 = hexstr2bin("88062608d3e6ad8a0aa2ace014c8a86f"
  722: 		       "0aa635d947ac9febe83ef4e55966144b"
  723: 		       "2a5ab39dc13814b94e3ab6e101a34f27"),
  724:     Case4 = hexstr2bin("3e8a69b7783c25851933ab6290af6ca7"
  725: 		       "7a9981480850009cc5577c6e1f573b4e"
  726: 		       "6801dd23c4a7d679ccf8a386c674cffb"),
  727:     Case5 = hexstr2bin("3abf34c3503b2a23a46efc619baef897"),
  728:     Case6 = hexstr2bin("4ece084485813e9088d2c63a041bc5b4"
  729: 		       "4f9ef1012a2b588f3cd11f05033ac4c6"
  730: 		       "0c2ef6ab4030fe8296248df163f44952"),
  731:     Case7 = hexstr2bin("6617178e941f020d351e2f254e8fd32c"
  732: 		       "602420feb0b8fb9adccebb82461e99c5"
  733: 		       "a678cc31e799176d3860e6110c46523e"),
  734:     hmac_rfc4231_cases_do(sha384, [Case1, Case2, Case3, Case4, Case5, Case6, Case7]).
  735: 
  736: hmac_rfc4231_sha512_do() ->
  737:     Case1 = hexstr2bin("87aa7cdea5ef619d4ff0b4241a1d6cb0"
  738: 		       "2379f4e2ce4ec2787ad0b30545e17cde"
  739: 		       "daa833b7d6b8a702038b274eaea3f4e4"
  740: 		       "be9d914eeb61f1702e696c203a126854"),
  741:     Case2 = hexstr2bin("164b7a7bfcf819e2e395fbe73b56e0a3"
  742: 		       "87bd64222e831fd610270cd7ea250554"
  743: 		       "9758bf75c05a994a6d034f65f8f0e6fd"
  744: 		       "caeab1a34d4a6b4b636e070a38bce737"),
  745:     Case3 = hexstr2bin("fa73b0089d56a284efb0f0756c890be9"
  746: 		       "b1b5dbdd8ee81a3655f83e33b2279d39"
  747: 		       "bf3e848279a722c806b485a47e67c807"
  748: 		       "b946a337bee8942674278859e13292fb"),
  749:     Case4 = hexstr2bin("b0ba465637458c6990e5a8c5f61d4af7"
  750: 		       "e576d97ff94b872de76f8050361ee3db"
  751: 		       "a91ca5c11aa25eb4d679275cc5788063"
  752: 		       "a5f19741120c4f2de2adebeb10a298dd"),
  753:     Case5 = hexstr2bin("415fad6271580a531d4179bc891d87a6"),
  754:     Case6 = hexstr2bin("80b24263c7c1a3ebb71493c1dd7be8b4"
  755: 		       "9b46d1f41b4aeec1121b013783f8f352"
  756: 		       "6b56d037e05f2598bd0fd2215d6a1e52"
  757: 		       "95e64f73f63f0aec8b915a985d786598"),
  758:     Case7 = hexstr2bin("e37b6a775dc87dbaa4dfa9f96e5e3ffd"
  759: 		       "debd71f8867289865df5a32d20cdc944"
  760: 		       "b6022cac3c4982b10d5eeb55c3e4de15"
  761: 		       "134676fb6de0446065c97440fa8c6a58"),
  762:     hmac_rfc4231_cases_do(sha512, [Case1, Case2, Case3, Case4, Case5, Case6, Case7]).
  763: 
  764: hmac_rfc4231_cases_do(Hash, CasesData) ->
  765:     hmac_rfc4231_cases_do(Hash, [case1, case2, case3, case4, case5, case6, case7], CasesData).
  766: 
  767: hmac_rfc4231_cases_do(_Hash, _, []) ->
  768:     ok;
  769: hmac_rfc4231_cases_do(Hash, [C|Cases], [D|CasesData]) ->
  770:     hmac_rfc4231_case(Hash, C, D),
  771:     hmac_rfc4231_cases_do(Hash, Cases, CasesData).
  772: 
  773: hmac_update_md5_io(doc) ->
  774:     ["Generate an MD5 HMAC using hmac_init, hmac_update, and hmac_final. "
  775:      "Expected values for examples are generated using crypto:md5_mac." ];
  776: hmac_update_md5_io(suite) ->
  777:     [];
  778: hmac_update_md5_io(Config) when is_list(Config) ->
  779:     ?line Key = ["A fine speach", "by a fine man!"],
  780:     ?line Data = "Sampl",
  781:     ?line Data2 = "e #1",
  782:     ?line Ctx = crypto:hmac_init(md5, Key),
  783:     ?line Ctx2 = crypto:hmac_update(Ctx, Data),
  784:     ?line Ctx3 = crypto:hmac_update(Ctx2, Data2),
  785:     ?line Mac = crypto:hmac_final(Ctx3),
  786:     ?line Exp = crypto:md5_mac(Key, lists:flatten([Data, Data2])), 
  787:     ?line m(Exp, Mac).
  788:     
  789: 
  790: hmac_update_md5_n(doc) ->
  791:     ["Generate a shortened MD5 HMAC using hmac_init, hmac_update, and hmac_final. "
  792:      "Expected values for examples are generated using crypto:md5_mac." ];
  793: hmac_update_md5_n(suite) ->
  794:     [];
  795: hmac_update_md5_n(Config) when is_list(Config) ->
  796:     ?line Key = ["A fine speach", "by a fine man!"],
  797:     ?line Data = "Sampl",
  798:     ?line Data2 = "e #1",
  799:     ?line Ctx = crypto:hmac_init(md5, Key),
  800:     ?line Ctx2 = crypto:hmac_update(Ctx, Data),
  801:     ?line Ctx3 = crypto:hmac_update(Ctx2, Data2),
  802:     ?line Mac = crypto:hmac_final_n(Ctx3, 12),
  803:     ?line Exp = crypto:md5_mac_96(Key, lists:flatten([Data, Data2])), 
  804:     ?line m(Exp, Mac).
  805: %%
  806: %%
  807: ripemd160(doc) ->
  808:     ["Generate RIPEMD160 message digests and check the result."];
  809: ripemd160(suite) ->
  810:     [];
  811: ripemd160(Config) when is_list(Config) ->
  812:     ?line m(crypto:hash(ripemd160,"abc"),
  813: 	    hexstr2bin("8EB208F7E05D987A9B044A8E98C6B087F15A0BFC")),
  814:     ?line m(crypto:hash(ripemd160,"abcdbcdecdefdefgefghfghighijhijkijkljklmklm"
  815: 			  "nlmnomnopnopq"), 
  816: 	    hexstr2bin("12A053384A9C0C88E405A06C27DCF49ADA62EB2B")).
  817: 
  818:    
  819: %%
  820: %%
  821: ripemd160_update(doc) ->
  822:     ["Generate RIPEMD160 message digests by using ripemd160_init,"
  823:      "ripemd160_update, and ripemd160_final and check the result."];
  824: ripemd160_update(suite) ->
  825:     [];
  826: ripemd160_update(Config) when is_list(Config) ->
  827:     ?line Ctx = crypto:hash_init(ripemd160),
  828:     ?line Ctx1 = crypto:hash_update(Ctx, "abcdbcdecdefdefgefghfghighi"),
  829:     ?line Ctx2 = crypto:hash_update(Ctx1, "jhijkijkljklmklmnlmnomnopnopq"),
  830:     ?line m(crypto:hash_final(Ctx2), 
  831:       hexstr2bin("12A053384A9C0C88E405A06C27DCF49ADA62EB2B")).
  832: 
  833: %%
  834: %%
  835: sha_update(doc) ->
  836:     ["Generate SHA message digests by using sha_init, sha_update, and"
  837:      "sha_final, and check the result. Examples are from FIPS-180-1."];
  838: sha_update(suite) ->
  839:     [];
  840: sha_update(Config) when is_list(Config) ->
  841:     ?line Ctx = crypto:sha_init(),
  842:     ?line Ctx1 = crypto:sha_update(Ctx, "abcdbcdecdefdefgefghfghighi"),
  843:     ?line Ctx2 = crypto:sha_update(Ctx1, "jhijkijkljklmklmnlmnomnopnopq"),
  844:     ?line m(crypto:sha_final(Ctx2), 
  845: 	    hexstr2bin("84983E441C3BD26EBAAE4AA1F95129E5E54670F1")).
  846: 
  847: %%
  848: %%
  849: sha256(doc) ->
  850:     ["Generate SHA-256 message digests and check the result. Examples are "
  851:      "from rfc-4634."];
  852: sha256(suite) ->
  853:     [];
  854: sha256(Config) when is_list(Config) ->
  855:     if_supported(sha256, fun() -> sha256_do() end).
  856: 
  857: sha256_do() ->
  858:     ?line m(crypto:hash(sha256, "abc"),
  859: 	    hexstr2bin("BA7816BF8F01CFEA4141"
  860: 		       "40DE5DAE2223B00361A396177A9CB410FF61F20015AD")),
  861:     ?line m(crypto:hash(sha256, "abcdbcdecdefdefgefghfghighijhijkijkljklmklm"
  862: 			  "nlmnomnopnopq"), 
  863: 	    hexstr2bin("248D6A61D20638B8"
  864: 		       "E5C026930C3E6039A33CE45964FF2167F6ECEDD419DB06C1")).
  865: 
  866: %%
  867: %%
  868: sha256_update(doc) ->
  869:     ["Generate SHA256 message digests by using sha256_init, sha256_update, and"
  870:      "sha256_final, and check the result. Examples are from rfc-4634."];
  871: sha256_update(suite) ->
  872:     [];
  873: sha256_update(Config) when is_list(Config) ->
  874:     if_supported(sha256, fun() -> sha256_update_do() end).
  875: 
  876: sha256_update_do() ->
  877:     ?line Ctx = crypto:hash_init(sha256),
  878:     ?line Ctx1 = crypto:hash_update(Ctx, "abcdbcdecdefdefgefghfghighi"),
  879:     ?line Ctx2 = crypto:hash_update(Ctx1, "jhijkijkljklmklmnlmnomnopnopq"),
  880:     ?line m(crypto:hash_final(Ctx2),
  881: 	    hexstr2bin("248D6A61D20638B8"
  882: 		       "E5C026930C3E6039A33CE45964FF2167F6ECEDD419DB06C1")).
  883: 
  884: 
  885: %%
  886: %%
  887: sha512(doc) ->
  888:     ["Generate SHA-512 message digests and check the result. Examples are "
  889:      "from rfc-4634."];
  890: sha512(suite) ->
  891:     [];
  892: sha512(Config) when is_list(Config) ->
  893:     if_supported(sha512, fun() -> sha512_do() end).
  894: 
  895: sha512_do() ->
  896:     ?line m(crypto:hash(sha512, "abc"),
  897: 	    hexstr2bin("DDAF35A193617ABACC417349AE20413112E6FA4E89A97EA2"
  898: 		       "0A9EEEE64B55D39A2192992A274FC1A836BA3C23A3FEEBBD"
  899: 		       "454D4423643CE80E2A9AC94FA54CA49F")),
  900:     ?line m(crypto:hash(sha512, "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
  901: 			  "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"), 
  902: 	    hexstr2bin("8E959B75DAE313DA8CF4F72814FC143F8F7779C6EB9F7FA1"
  903: 		       "7299AEADB6889018501D289E4900F7E4331B99DEC4B5433A"
  904: 		       "C7D329EEB6DD26545E96E55B874BE909")).
  905: 
  906: %%
  907: %%
  908: sha512_update(doc) ->
  909:     ["Generate SHA512 message digests by using sha512_init, sha512_update, and"
  910:      "sha512_final, and check the result. Examples are from rfc=4634."];
  911: sha512_update(suite) ->
  912:     [];
  913: sha512_update(Config) when is_list(Config) ->
  914:     if_supported(sha512, fun() -> sha512_update_do() end).
  915: 
  916: sha512_update_do() ->
  917:     ?line Ctx = crypto:hash_init(sha512),
  918:     ?line Ctx1 = crypto:hash_update(Ctx, "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"),
  919:     ?line Ctx2 = crypto:hash_update(Ctx1, "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"),
  920:     ?line m(crypto:hash_final(Ctx2),
  921: 	    hexstr2bin("8E959B75DAE313DA8CF4F72814FC143F8F7779C6EB9F7FA1"
  922: 		       "7299AEADB6889018501D289E4900F7E4331B99DEC4B5433A"
  923: 		       "C7D329EEB6DD26545E96E55B874BE909")).
  924: 
  925: %%
  926: %%
  927: md5_mac(doc) ->
  928:     ["Generate some HMACs, using MD5, and check the result. Examples are "
  929:      "from RFC-2104."];
  930: md5_mac(suite) ->
  931:     [];
  932: md5_mac(Config) when is_list(Config) ->
  933:     ?line m(crypto:md5_mac(hexstr2bin("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"),
  934: 			   "Hi There"),
  935: 	    hexstr2bin("9294727a3638bb1c13f48ef8158bfc9d")),
  936:     ?line m(crypto:md5_mac(list_to_binary("Jefe"), 
  937: 				     "what do ya want for nothing?"),
  938: 	    hexstr2bin("750c783e6ab0b503eaa86e310a5db738")),
  939:     ?line m(crypto:md5_mac(hexstr2bin("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"),
  940: 			   hexstr2bin("DDDDDDDDDDDDDDDDDDDD"
  941: 				      "DDDDDDDDDDDDDDDDDDDD"
  942: 				      "DDDDDDDDDDDDDDDDDDDD"
  943: 				      "DDDDDDDDDDDDDDDDDDDD"
  944: 				      "DDDDDDDDDDDDDDDDDDDD")),
  945: 	    hexstr2bin("56be34521d144c88dbb8c733f0e8b3f6")).
  946: 
  947: %%
  948: %%
  949: md5_mac_io(doc) ->
  950:     ["Generate some HMACs, using MD5, with Key an IO-list, and check the "
  951:      "result. Examples are from RFC-2104."];
  952: md5_mac_io(suite) ->
  953:     [];
  954: md5_mac_io(Config) when is_list(Config) ->
  955:     ?line Key1 = hexstr2bin("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"),
  956:     ?line {B11, B12} = split_binary(Key1, 4),
  957:     ?line Key11 = [B11,binary_to_list(B12)],
  958:     ?line m(crypto:md5_mac(Key11, "Hi There"),
  959: 	    hexstr2bin("9294727a3638bb1c13f48ef8158bfc9d")).
  960: 
  961: %%
  962: %%
  963: des_cbc(doc) ->
  964:     "Encrypt and decrypt according to CBC DES. and check the result. "
  965: 	"Example are from FIPS-81.";
  966: des_cbc(suite) ->
  967:     [];
  968: des_cbc(Config) when is_list(Config) ->
  969:     ?line Key =  hexstr2bin("0123456789abcdef"),
  970:     ?line IVec = hexstr2bin("1234567890abcdef"),
  971:     ?line Plain = "Now is the time for all ",
  972:     ?line Cipher = crypto:des_cbc_encrypt(Key, IVec, Plain),
  973:     ?line m(Cipher, hexstr2bin("e5c7cdde872bf27c43e934008c389c"
  974: 			       "0f683788499a7c05f6")),
  975:     ?line m(list_to_binary(Plain), 
  976: 	    crypto:des_cbc_decrypt(Key, IVec, Cipher)),
  977:     ?line Plain2 = "7654321 Now is the time for " ++ [0, 0, 0, 0],
  978:     ?line Cipher2 = crypto:des_cbc_encrypt(Key, IVec, Plain2),
  979:     ?line m(Cipher2, hexstr2bin("b9916b8ee4c3da64b4f44e3cbefb9"
  980: 				"9484521388fa59ae67d58d2e77e86062733")),
  981:     ?line m(list_to_binary(Plain2), 
  982: 	    crypto:des_cbc_decrypt(Key, IVec, Cipher2)).
  983: 
  984: %%
  985: %%
  986: des_cbc_iter(doc) ->
  987:         "Encrypt and decrypt according to CBC DES in two steps, and "
  988:     "check the result. Example are from FIPS-81.";
  989: des_cbc_iter(suite) ->
  990:     [];
  991: des_cbc_iter(Config) when is_list(Config) ->
  992:     ?line Key =  hexstr2bin("0123456789abcdef"),
  993:     ?line IVec = hexstr2bin("1234567890abcdef"),
  994:     ?line Plain1 = "Now is the time ",
  995:     ?line Plain2 = "for all ",
  996:     ?line Cipher1 = crypto:des_cbc_encrypt(Key, IVec, Plain1),
  997:     ?line IVec2 = crypto:des_cbc_ivec(Cipher1),
  998:     ?line Cipher2 = crypto:des_cbc_encrypt(Key, IVec2, Plain2),
  999:     ?line Cipher = list_to_binary([Cipher1, Cipher2]),
 1000:     ?line m(Cipher, hexstr2bin("e5c7cdde872bf27c43e934008c389c"
 1001: 			             "0f683788499a7c05f6")).
 1002: 
 1003: %%
 1004: %%
 1005: des_cfb(doc) ->
 1006:     "Encrypt and decrypt according to CFB DES. and check the result. "
 1007: 	"Example is from FIPS-81.";
 1008: des_cfb(suite) ->
 1009:     [];
 1010: des_cfb(Config) when is_list(Config) ->
 1011:     ?line Key =  hexstr2bin("0123456789abcdef"),
 1012:     ?line IVec = hexstr2bin("1234567890abcdef"),
 1013:     ?line Plain = "Now is the",
 1014:     ?line Cipher = crypto:des_cfb_encrypt(Key, IVec, Plain),
 1015:     ?line m(Cipher, hexstr2bin("f31fda07011462ee187f")),
 1016:     ?line m(list_to_binary(Plain),
 1017: 	    crypto:des_cfb_decrypt(Key, IVec, Cipher)).
 1018: 
 1019: %%
 1020: %%
 1021: des_cfb_iter(doc) ->
 1022:         "Encrypt and decrypt according to CFB DES in two steps, and "
 1023:     "check the result. Example is from FIPS-81.";
 1024: des_cfb_iter(suite) ->
 1025:     [];
 1026: des_cfb_iter(Config) when is_list(Config) ->
 1027:     ?line Key =  hexstr2bin("0123456789abcdef"),
 1028:     ?line IVec = hexstr2bin("1234567890abcdef"),
 1029:     ?line Plain1 = "Now i",
 1030:     ?line Plain2 = "s the",
 1031:     ?line Cipher1 = crypto:des_cfb_encrypt(Key, IVec, Plain1),
 1032:     ?line IVec2 = crypto:des_cfb_ivec(IVec, Cipher1),
 1033:     ?line Cipher2 = crypto:des_cfb_encrypt(Key, IVec2, Plain2),
 1034:     ?line Cipher = list_to_binary([Cipher1, Cipher2]),
 1035:     ?line m(Cipher, hexstr2bin("f31fda07011462ee187f")).
 1036: 
 1037: %%
 1038: %%
 1039: des_ecb(doc) ->
 1040:     "Encrypt and decrypt according to ECB DES and check the result. "
 1041:     "Example are from FIPS-81.";
 1042: des_ecb(suite) ->
 1043:     [];
 1044: des_ecb(Config) when is_list(Config) ->
 1045:     ?line Key =  hexstr2bin("0123456789abcdef"),
 1046:     ?line Cipher1 = crypto:des_ecb_encrypt(Key, "Now is t"),
 1047:     ?line m(Cipher1, hexstr2bin("3fa40e8a984d4815")),
 1048:     ?line Cipher2 = crypto:des_ecb_encrypt(Key, "he time "),
 1049:     ?line m(Cipher2, hexstr2bin("6a271787ab8883f9")),
 1050:     ?line Cipher3 = crypto:des_ecb_encrypt(Key, "for all "),
 1051:     ?line m(Cipher3, hexstr2bin("893d51ec4b563b53")),
 1052:     ?line Cipher4 = crypto:des_ecb_decrypt(Key, hexstr2bin("3fa40e8a984d4815")),
 1053:     ?line m(Cipher4, <<"Now is t">>),
 1054:     ?line Cipher5 = crypto:des_ecb_decrypt(Key, hexstr2bin("6a271787ab8883f9")),
 1055:     ?line m(Cipher5, <<"he time ">>),
 1056:     ?line Cipher6 = crypto:des_ecb_decrypt(Key, hexstr2bin("893d51ec4b563b53")),
 1057:     ?line m(Cipher6, <<"for all ">>).
 1058: %%
 1059: %%
 1060: rc2_cbc(doc) ->
 1061:     "Encrypt and decrypt according to RC2 CBC and check the result. "
 1062:     "Example stripped out from public_key application test";
 1063: rc2_cbc(Config) when is_list(Config) ->
 1064:    
 1065:     Key = <<146,210,160,124,215,227,153,239,227,17,222,140,3,93,27,191>>,
 1066:     IV = <<72,91,135,182,25,42,35,210>>,
 1067: 
 1068:     Cipher = <<36,245,206,158,168,230,58,69,148,137,32,192,250,41,237,181,181,251, 192,2,175,135,177,171,57,30,111,117,159,149,15,28,88,158,28,81,28,115, 85,219,241,82,117,222,91,85,73,117,164,25,182,52,191,64,123,57,26,19, 211,27,253,31,194,219,231,104,247,240,172,130,119,21,225,154,101,247, 32,216,42,216,133,169,78,22,97,27,227,26,196,224,172,168,17,9,148,55, 203,91,252,40,61,226,236,221,215,160,78,63,13,181,68,57,196,241,185, 207, 116,129,152,237,60,139,247,153,27,146,161,246,222,98,185,222,152, 187,135, 236,86,34,7,110,91,230,173,34,160,242,202,222,121,127,181,140, 101,203,195, 190,88,250,86,147,127,87,72,126,171,16,71,47,110,248,88, 14,29,143,161,152, 129,236,148,22,152,186,208,119,70,8,174,193,203,100, 193,203,200,117,102,242, 134,142,96,125,135,200,217,190,76,117,50,70, 209,186,101,241,200,91,40,193,54, 90,195,38,47,59,197,38,234,86,223,16, 51,253,204,129,20,171,66,21,241,26,135,216, 196,114,110,91,15,53,40, 164,201,136,113,95,247,51,181,208,241,68,168,98,151,36, 155,72,24,57, 42,191,14,125,204,10,167,214,233,138,115,125,234,121,134,227,26,247, 77,200,117,110,117,111,168,156,206,67,159,149,189,173,150,193,91,199, 216,153,22, 189,137,185,89,160,13,131,132,58,109,28,110,246,252,251,14, 232,91,38,52,29,101,188,69,123,50,0,130,178,93,73,239,118,7,77,35,59, 253,10,159,45,86,142,37,78,232,48>>,
 1069:     Text = <<48,130,1,85,2,1,0,48,13,6,9,42,134,72,134,247,13,1,1,1,5,0,4,130,1,63,48,130, 1,59,2,1,0,2,65,0,222,187,252,44,9,214,27,173,162,169,70,47,36,34,78,84,204, 107,60,192,117,95,21,206,49,142,245,126,121,223,23,2,107,106,133,204,161,36, 40,2,114,69,4,93,242,5,42,50,154,47,154,211,209,123,120,161,5,114,173,155,34, 191,52,59,2,3,1,0,1,2,64,45,144,169,106,220,236,71,39,67,82,123,192,35,21,61, 143,13,110,150,180,12,142,210,40,39,109,70,125,132,51,6,66,159,134,112,85, 155,243,118,221,65,133,127,99,151,194,252,141,149,224,229,62,214,45,228,32, 184,85,67,14,228,161,184,161,2,33,0,255,202,240,131,130,57,49,224,115,255,83, 79,6,165,212,21,179,212,20,188,97,74,69,68,163,223,247,237,39,24,23,235,2,33, 0,222,234,48,36,33,23,219,45,59,136,55,245,143,29,165,48,255,131,207,146,131, 104,13,163,54,131,236,78,88,54,16,241,2,33,0,230,2,99,129,173,176,166,131, 241,106,143,76,9,107,70,41,121,185,228,39,124,200,159,62,216,169,5,180,111, 169,255,159,2,33,0,151,193,70,212,209,210,179,219,175,83,165,4,255,81,103,76, 92,39,24,0,222,132,208,3,244,241,10,198,171,54,227,129,2,32,43,250,20,31,16, 189,168,116,225,1,125,132,94,130,118,124,28,56,232,39,69,218,244,33,240,200, 205,9,215,101,35,135,7,7,7,7,7,7,7>>,
 1070:     
 1071:     Text = crypto:rc2_cbc_decrypt(Key, IV, Cipher),
 1072:     Cipher = crypto:rc2_cbc_encrypt(Key, IV, Text).
 1073: 
 1074: %%
 1075: %%
 1076: des3_cbc(doc) ->
 1077:     "Encrypt and decrypt according to CBC 3DES, and check the result.";
 1078: des3_cbc(suite) ->
 1079:     [];
 1080: des3_cbc(Config) when is_list(Config) ->
 1081:     ?line Key1 = hexstr2bin("0123456789abcdef"),
 1082:     ?line Key2 = hexstr2bin("fedcba9876543210"),
 1083:     ?line Key3 = hexstr2bin("0f2d4b6987a5c3e1"),
 1084:     ?line IVec = hexstr2bin("1234567890abcdef"),
 1085:     ?line Plain = "Now is the time for all ",
 1086:     ?line Cipher = crypto:des3_cbc_encrypt(Key1, Key2, Key3, IVec, Plain),
 1087:     ?line m(Cipher, hexstr2bin("8a2667ee5577267cd9b1af2c5a0480"
 1088: 			       "0bac1ae66970fb2b89")),
 1089:     ?line m(list_to_binary(Plain),
 1090: 	    crypto:des3_cbc_decrypt(Key1, Key2, Key3, IVec, Cipher)),
 1091:     ?line Plain2 = "7654321 Now is the time for " ++ [0, 0, 0, 0],
 1092:     ?line Cipher2 = crypto:des3_cbc_encrypt(Key1, Key2, Key3, IVec, Plain2),
 1093:     ?line m(Cipher2, hexstr2bin("eb33ec6ede2c8e90f6877e77b95d5"
 1094: 				"4c83cee22907f7f0041ca1b7abe202bfafe")),
 1095:     ?line m(list_to_binary(Plain2),
 1096: 	    crypto:des3_cbc_decrypt(Key1, Key2, Key3, IVec, Cipher2)),
 1097: 
 1098:     ?line Key =  hexstr2bin("0123456789abcdef"),
 1099:     ?line DESCipher = crypto:des3_cbc_encrypt(Key, Key, Key, IVec, Plain),
 1100:     ?line m(DESCipher, hexstr2bin("e5c7cdde872bf27c43e934008c389c"
 1101: 			       "0f683788499a7c05f6")),
 1102:     ?line m(list_to_binary(Plain),
 1103: 	    crypto:des3_cbc_decrypt(Key, Key, Key, IVec, DESCipher)),
 1104:     ?line DESCipher2 = crypto:des3_cbc_encrypt(Key, Key, Key, IVec, Plain2),
 1105:     ?line m(DESCipher2, hexstr2bin("b9916b8ee4c3da64b4f44e3cbefb9"
 1106: 				"9484521388fa59ae67d58d2e77e86062733")),
 1107:     ?line m(list_to_binary(Plain2),
 1108: 	    crypto:des3_cbc_decrypt(Key, Key, Key, IVec, DESCipher2)).
 1109: 
 1110: %%
 1111: %%
 1112: des3_cfb(doc) ->
 1113:     "Encrypt and decrypt according to CFB 3DES, and check the result.";
 1114: des3_cfb(suite) ->
 1115:     [];
 1116: des3_cfb(Config) when is_list(Config) ->
 1117:     case openssl_version() of
 1118: 	V when V < 16#90705F -> {skipped,"OpenSSL version too old"};
 1119: 	_ -> des3_cfb_do()
 1120:     end.
 1121: 
 1122: des3_cfb_do() ->
 1123:     ?line Key1 = hexstr2bin("0123456789abcdef"),
 1124:     ?line Key2 = hexstr2bin("fedcba9876543210"),
 1125:     ?line Key3 = hexstr2bin("0f2d4b6987a5c3e1"),
 1126:     ?line IVec = hexstr2bin("1234567890abcdef"),
 1127:     ?line Plain = "Now is the time for all ",
 1128:     ?line Cipher = crypto:des3_cfb_encrypt(Key1, Key2, Key3, IVec, Plain),
 1129:     ?line m(Cipher, hexstr2bin("fc0ba7a20646ba53cc8bff263f0937"
 1130: 			       "1deab42a00666db02c")),
 1131:     ?line m(list_to_binary(Plain),
 1132: 	    crypto:des3_cfb_decrypt(Key1, Key2, Key3, IVec, Cipher)),
 1133:     ?line Plain2 = "7654321 Now is the time for " ++ [0, 0, 0, 0],
 1134:     ?line Cipher2 = crypto:des3_cfb_encrypt(Key1, Key2, Key3, IVec, Plain2),
 1135:     ?line m(Cipher2, hexstr2bin("8582c59ac01897422632c0accb66c"
 1136: 				"e413f5efab838fce7e41e2ba67705bad5bc")),
 1137:     ?line m(list_to_binary(Plain2),
 1138: 	    crypto:des3_cfb_decrypt(Key1, Key2, Key3, IVec, Cipher2)).
 1139: 
 1140: %%
 1141: %%
 1142: aes_cfb(doc) ->
 1143:     "Encrypt and decrypt according to AES CFB 128 bit and check "
 1144: 	"the result. Example are from NIST SP 800-38A.";
 1145: 
 1146: aes_cfb(suite) ->
 1147:     [];
 1148: aes_cfb(Config) when is_list(Config) ->
 1149: 
 1150: %% Sample data from NIST Spec.Publ. 800-38A
 1151: %% F.3.13 CFB128-AES128.Encrypt
 1152: %% Key            2b7e151628aed2a6abf7158809cf4f3c
 1153: %% IV             000102030405060708090a0b0c0d0e0f
 1154: %% Segment #1
 1155: %% Input Block    000102030405060708090a0b0c0d0e0f
 1156: %% Output Block   50fe67cc996d32b6da0937e99bafec60
 1157: %% Plaintext      6bc1bee22e409f96e93d7e117393172a
 1158: %% Ciphertext     3b3fd92eb72dad20333449f8e83cfb4a
 1159: %% Segment #2
 1160: %% Input Block    3b3fd92eb72dad20333449f8e83cfb4a
 1161: %% Output Block   668bcf60beb005a35354a201dab36bda
 1162: %% Plaintext      ae2d8a571e03ac9c9eb76fac45af8e51
 1163: %% Ciphertext     c8a64537a0b3a93fcde3cdad9f1ce58b
 1164: %% Segment #3
 1165: %% Input Block    c8a64537a0b3a93fcde3cdad9f1ce58b
 1166: %% Output Block   16bd032100975551547b4de89daea630
 1167: %% Plaintext      30c81c46a35ce411e5fbc1191a0a52ef
 1168: %% Ciphertext     26751f67a3cbb140b1808cf187a4f4df
 1169: %% Segment #4
 1170: %% Input Block    26751f67a3cbb140b1808cf187a4f4df
 1171: %% Output Block   36d42170a312871947ef8714799bc5f6
 1172: %% Plaintext      f69f2445df4f9b17ad2b417be66c3710
 1173: %% Ciphertext     c04b05357c5d1c0eeac4c66f9ff7f2e6
 1174: 
 1175:     ?line Key =  hexstr2bin("2b7e151628aed2a6abf7158809cf4f3c"),
 1176:     ?line IVec = hexstr2bin("000102030405060708090a0b0c0d0e0f"),
 1177:     ?line Plain = hexstr2bin("6bc1bee22e409f96e93d7e117393172a"),
 1178:     ?line Cipher = hexstr2bin("3b3fd92eb72dad20333449f8e83cfb4a"),
 1179: 
 1180:     %% Try all prefixes of plain and cipher.
 1181:     aes_cfb_do(byte_size(Plain), Plain, Cipher, Key, IVec).
 1182: 
 1183: aes_cfb_do(N, Plain, Cipher, Key, IVec) when N >= 0 ->
 1184:     <<P:N/binary, _/binary>> = Plain,
 1185:     <<C:N/binary, _/binary>> = Cipher,
 1186:     ?line C = crypto:aes_cfb_128_encrypt(Key, IVec, P),
 1187:     ?line P = crypto:aes_cfb_128_decrypt(Key, IVec, C),
 1188:     aes_cfb_do(N-1, Plain, Cipher, Key, IVec);
 1189: aes_cfb_do(_, _, _, _, _) -> ok.
 1190: 
 1191: 
 1192: %%
 1193: %%
 1194: aes_cbc(doc) ->
 1195:     "Encrypt and decrypt according to AES CBC 128 bit. and check the result. "
 1196: 	"Example are from NIST SP 800-38A.";
 1197: 
 1198: aes_cbc(suite) ->
 1199:     [];
 1200: aes_cbc(Config) when is_list(Config) ->
 1201: 
 1202: %% Sample data from NIST Spec.Publ. 800-38A
 1203: %% F.2.1 CBC-AES128.Encrypt
 1204: %% Key 2b7e151628aed2a6abf7158809cf4f3c 
 1205: %% IV 000102030405060708090a0b0c0d0e0f 
 1206: %% Block #1 
 1207: %% Plaintext 6bc1bee22e409f96e93d7e117393172a 
 1208: %% Input Block 6bc0bce12a459991e134741a7f9e1925 
 1209: %% Output Block 7649abac8119b246cee98e9b12e9197d 
 1210: %% Ciphertext 7649abac8119b246cee98e9b12e9197d 
 1211: %% Block #2 
 1212: %% Plaintext ae2d8a571e03ac9c9eb76fac45af8e51 
 1213: %% Input Block d86421fb9f1a1eda505ee1375746972c 
 1214: %% Output Block 5086cb9b507219ee95db113a917678b2 
 1215: %% Ciphertext 5086cb9b507219ee95db113a917678b2 
 1216: %% Block #3 
 1217: %% Plaintext 30c81c46a35ce411e5fbc1191a0a52ef 
 1218: %% Input Block 604ed7ddf32efdff7020d0238b7c2a5d 
 1219: %% Output Block 73bed6b8e3c1743b7116e69e22229516 
 1220: %% Ciphertext 73bed6b8e3c1743b7116e69e22229516 
 1221: %% Block #4 
 1222: %% Plaintext f69f2445df4f9b17ad2b417be66c3710 
 1223: %% Input Block 8521f2fd3c8eef2cdc3da7e5c44ea206 
 1224: %% Output Block 3ff1caa1681fac09120eca307586e1a7 
 1225: %% Ciphertext 3ff1caa1681fac09120eca307586e1a7 
 1226: %%
 1227: %% F.2.2 CBC-AES128.Decrypt 
 1228: %% Key 2b7e151628aed2a6abf7158809cf4f3c 
 1229: %% IV 000102030405060708090a0b0c0d0e0f 
 1230:     %% Block #1 
 1231: %% Ciphertext 7649abac8119b246cee98e9b12e9197d 
 1232: %% Input Block 7649abac8119b246cee98e9b12e9197d 
 1233: %% Output Block 6bc0bce12a459991e134741a7f9e1925 
 1234: %% Plaintext 6bc1bee22e409f96e93d7e117393172a 
 1235: %% Block #2 
 1236: %% Ciphertext 5086cb9b507219ee95db113a917678b2 
 1237: %% Input Block 5086cb9b507219ee95db113a917678b2 
 1238: %% Output Block d86421fb9f1a1eda505ee1375746972c 
 1239: %% Plaintext ae2d8a571e03ac9c9eb76fac45af8e51 
 1240: %% Block #3 
 1241: %% Ciphertext 73bed6b8e3c1743b7116e69e22229516 
 1242: %% Input Block 73bed6b8e3c1743b7116e69e22229516 
 1243: %% Output Block 604ed7ddf32efdff7020d0238b7c2a5d 
 1244: %% Plaintext 30c81c46a35ce411e5fbc1191a0a52ef 
 1245: %% Block #4 
 1246: %% Ciphertext 3ff1caa1681fac09120eca307586e1a7 
 1247: %% Input Block 3ff1caa1681fac09120eca307586e1a7
 1248: %% Output Block 8521f2fd3c8eef2cdc3da7e5c44ea206 
 1249: %% Plaintext f69f2445df4f9b17ad2b417be66c3710
 1250: 
 1251:     ?line Key =  hexstr2bin("2b7e151628aed2a6abf7158809cf4f3c"),
 1252:     ?line IVec = hexstr2bin("000102030405060708090a0b0c0d0e0f"),
 1253:     ?line Plain = hexstr2bin("6bc1bee22e409f96e93d7e117393172a"),
 1254:     ?line Cipher = crypto:aes_cbc_128_encrypt(Key, IVec, Plain),
 1255:     ?line m(Cipher, hexstr2bin("7649abac8119b246cee98e9b12e9197d")),
 1256:     ?line m(Plain, 
 1257: 	    crypto:aes_cbc_128_decrypt(Key, IVec, Cipher)).
 1258: 
 1259: aes_cbc_iter(doc) ->
 1260:     "Encrypt and decrypt according to CBC AES in steps";
 1261: aes_cbc_iter(suite) -> [];
 1262: aes_cbc_iter(Config) when is_list(Config) ->
 1263:     Key = list_to_binary(lists:seq(255,256-16*17,-17)),
 1264:     IVec = list_to_binary(lists:seq(1,16*7,7)),
 1265:     Plain = <<"One, two, three o'clock, four o'clock, rock"
 1266: 	     "Five, six, seven o'clock, eight o'clock, rock"
 1267: 	     "Nine, ten, eleven o'clock, twelve o'clock, rock"
 1268: 	     "We're gonna rock around the clock tonight">>,
 1269:     ?line 0 = size(Plain) rem 16,
 1270: 
 1271:     ?line Cipher = crypto:aes_cbc_128_encrypt(Key, IVec, Plain),
 1272:     ?line Plain = crypto:aes_cbc_128_decrypt(Key, IVec, Cipher),
 1273:     
 1274:     ?line Cipher = aes_cbc_encrypt_iter(Key,IVec,Plain,<<>>),
 1275:     ?line Plain = aes_cbc_decrypt_iter(Key,IVec,Cipher,<<>>),
 1276:     ok.
 1277: 
 1278: aes_cbc_encrypt_iter(_,_,<<>>, Acc) ->
 1279:     Acc;
 1280: aes_cbc_encrypt_iter(Key,IVec,Data, Acc) ->
 1281:     Bytes = 16 * (1 + size(Data) div (16*3)),
 1282:     <<Chunk:Bytes/binary, Rest/binary>> = Data,
 1283:     %%io:format("encrypt iter Chunk=~p Rest=~p\n",[Chunk,Rest]),
 1284:     ?line Cipher = crypto:aes_cbc_128_encrypt(Key, IVec, Chunk),
 1285:     ?line IVec2 = crypto:aes_cbc_ivec(Cipher),
 1286:     aes_cbc_encrypt_iter(Key,IVec2,Rest, <<Acc/binary, Cipher/binary>>).
 1287: 
 1288: aes_cbc_decrypt_iter(_,_,<<>>, Acc) ->
 1289:     Acc;
 1290: aes_cbc_decrypt_iter(Key,IVec,Data, Acc) ->
 1291:     Bytes = 16 * (1 + size(Data) div (16*5)),
 1292:     <<Chunk:Bytes/binary, Rest/binary>> = Data,
 1293:     %%io:format("decrypt iter Chunk=~p Rest=~p\n",[Chunk,Rest]),
 1294:     ?line Plain = crypto:aes_cbc_128_decrypt(Key, IVec, Chunk),
 1295:     ?line IVec2 = crypto:aes_cbc_ivec(Chunk),
 1296:     aes_cbc_decrypt_iter(Key,IVec2,Rest, <<Acc/binary, Plain/binary>>).
 1297: 
 1298: 
 1299: aes_ctr(doc) -> "CTR";
 1300: aes_ctr(Config) when is_list(Config) ->
 1301:     %% Sample data from NIST Spec.Publ. 800-38A
 1302:     %% F.5.1 CTR-AES128.Encrypt
 1303:     Key128 = hexstr2bin("2b7e151628aed2a6abf7158809cf4f3c"),
 1304:     Samples128 = [{"f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff", % Input Block
 1305:                    "6bc1bee22e409f96e93d7e117393172a", % Plaintext
 1306:                    "874d6191b620e3261bef6864990db6ce"},% Ciphertext
 1307:                   {"f0f1f2f3f4f5f6f7f8f9fafbfcfdff00",
 1308:                    "ae2d8a571e03ac9c9eb76fac45af8e51",
 1309:                    "9806f66b7970fdff8617187bb9fffdff"},
 1310:                   {"f0f1f2f3f4f5f6f7f8f9fafbfcfdff01",
 1311:                    "30c81c46a35ce411e5fbc1191a0a52ef",
 1312:                    "5ae4df3edbd5d35e5b4f09020db03eab"},
 1313:                   {"f0f1f2f3f4f5f6f7f8f9fafbfcfdff02",
 1314:                    "f69f2445df4f9b17ad2b417be66c3710",
 1315:                    "1e031dda2fbe03d1792170a0f3009cee"}],
 1316:     lists:foreach(fun(S) -> aes_ctr_do(Key128,S) end, Samples128),
 1317: 
 1318:     %% F.5.3  CTR-AES192.Encrypt
 1319:     Key192 =  hexstr2bin("8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b"),
 1320:     Samples192 = [{"f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff", % Input Block
 1321:                    "6bc1bee22e409f96e93d7e117393172a", % Plaintext
 1322:                    "1abc932417521ca24f2b0459fe7e6e0b"},% Ciphertext
 1323:                   {"f0f1f2f3f4f5f6f7f8f9fafbfcfdff00",
 1324:                    "ae2d8a571e03ac9c9eb76fac45af8e51",
 1325:                    "090339ec0aa6faefd5ccc2c6f4ce8e94"},
 1326:                   {"f0f1f2f3f4f5f6f7f8f9fafbfcfdff01",
 1327:                    "30c81c46a35ce411e5fbc1191a0a52ef",
 1328:                    "1e36b26bd1ebc670d1bd1d665620abf7"},
 1329:                   {"f0f1f2f3f4f5f6f7f8f9fafbfcfdff02",
 1330:                    "f69f2445df4f9b17ad2b417be66c3710",
 1331:                    "4f78a7f6d29809585a97daec58c6b050"}],    
 1332:     lists:foreach(fun(S) -> aes_ctr_do(Key192,S) end, Samples192),
 1333: 
 1334:     %% F.5.5  CTR-AES256.Encrypt
 1335:     Key256 = hexstr2bin("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4"),
 1336:     Samples256 = [{"f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",  % Input Block
 1337:                     "6bc1bee22e409f96e93d7e117393172a", % Plaintext
 1338:                     "601ec313775789a5b7a7f504bbf3d228"},% Ciphertext
 1339:                    {"f0f1f2f3f4f5f6f7f8f9fafbfcfdff00",
 1340:                     "ae2d8a571e03ac9c9eb76fac45af8e51",
 1341:                     "f443e3ca4d62b59aca84e990cacaf5c5"},
 1342:                    {"f0f1f2f3f4f5f6f7f8f9fafbfcfdff01",
 1343:                     "30c81c46a35ce411e5fbc1191a0a52ef",
 1344:                     "2b0930daa23de94ce87017ba2d84988d"},
 1345:                    {"f0f1f2f3f4f5f6f7f8f9fafbfcfdff02",
 1346:                     "f69f2445df4f9b17ad2b417be66c3710",
 1347:                     "dfc9c58db67aada613c2dd08457941a6"}],
 1348:     lists:foreach(fun(S) -> aes_ctr_do(Key256,S) end, Samples256).
 1349: 
 1350: 
 1351: aes_ctr_do(Key,{IVec, Plain, Cipher}) ->
 1352:     ?line I = hexstr2bin(IVec),
 1353:     ?line P = hexstr2bin(Plain),
 1354:     ?line C = crypto:aes_ctr_encrypt(Key, I, P),
 1355:     ?line m(C, hexstr2bin(Cipher)),
 1356:     ?line m(P, crypto:aes_ctr_decrypt(Key, I, C)).
 1357: 
 1358: aes_ctr_stream(doc) -> "CTR Streaming";
 1359: aes_ctr_stream(Config) when is_list(Config) ->
 1360:     %% Sample data from NIST Spec.Publ. 800-38A
 1361:     %% F.5.1 CTR-AES128.Encrypt
 1362:     Key128 = hexstr2bin("2b7e151628aed2a6abf7158809cf4f3c"),
 1363:     Samples128 = [{"f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",       % Input Block
 1364:                    ["6bc1bee22e409f", "96e93d7e117393172a"], % Plaintext
 1365:                    ["874d6191b620e3261bef6864990db6ce"]},    % Ciphertext
 1366:                   {"f0f1f2f3f4f5f6f7f8f9fafbfcfdff00",
 1367:                    ["ae2d8a57", "1e03ac9c", "9eb76fac", "45af8e51"],
 1368:                    ["9806f66b7970fdff","8617187bb9fffdff"]},
 1369:                   {"f0f1f2f3f4f5f6f7f8f9fafbfcfdff01",
 1370:                    ["30c81c46a35c", "e411e5fbc119", "1a0a52ef"],
 1371:                    ["5ae4df3e","dbd5d3","5e5b4f0902","0db03eab"]},
 1372:                   {"f0f1f2f3f4f5f6f7f8f9fafbfcfdff02",
 1373:                    ["f69f2445df4f9b17ad2b417be66c3710"],
 1374:                    ["1e031dda2fbe","03d1792170a0","f3009cee"]}],
 1375:     lists:foreach(fun(S) -> aes_ctr_stream_do(Key128,S) end, Samples128),
 1376: 
 1377:     %% F.5.3  CTR-AES192.Encrypt
 1378:     Key192 =  hexstr2bin("8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b"),
 1379:     Samples192 = [{"f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",   % Input Block
 1380:                    ["6bc1bee22e409f96e93d7e117393172a"], % Plaintext
 1381:                    ["1abc9324","17521c","a24f2b04","59fe7e6e0b"]},  % Ciphertext
 1382:                   {"f0f1f2f3f4f5f6f7f8f9fafbfcfdff00",
 1383:                    ["ae2d8a57", "1e03ac9c9eb76fac", "45af8e51"],
 1384:                    ["090339ec0aa6faefd5ccc2c6f4ce8e94"]},
 1385:                   {"f0f1f2f3f4f5f6f7f8f9fafbfcfdff01",
 1386:                    ["30c81c46a35ce411", "e5fbc1191a0a52ef"],
 1387:                    ["1e36b26bd1","ebc670d1bd1d","665620abf7"]},
 1388:                   {"f0f1f2f3f4f5f6f7f8f9fafbfcfdff02",
 1389:                    ["f69f2445", "df4f9b17ad", "2b417be6", "6c3710"],
 1390:                    ["4f78a7f6d2980958","5a97daec58c6b050"]}],    
 1391:     lists:foreach(fun(S) -> aes_ctr_stream_do(Key192,S) end, Samples192),
 1392: 
 1393:     %% F.5.5  CTR-AES256.Encrypt
 1394:     Key256 = hexstr2bin("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4"),
 1395:     Samples256 = [{"f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",         % Input Block
 1396:                     ["6bc1bee22e409f96", "e93d7e117393172a"],  % Plaintext
 1397:                     ["601ec313775789", "a5b7a7f504bbf3d228"]}, % Ciphertext
 1398:                    {"f0f1f2f3f4f5f6f7f8f9fafbfcfdff00",
 1399:                     ["ae2d8a571e03ac9c9eb76fac45af8e51"],
 1400:                     ["f443e3ca","4d62b59aca84","e990cacaf5c5"]},
 1401:                    {"f0f1f2f3f4f5f6f7f8f9fafbfcfdff01",
 1402:                     ["30c81c46","a35ce411","e5fbc119","1a0a52ef"],
 1403:                     ["2b0930daa23de94ce87017ba2d84988d"]},
 1404:                    {"f0f1f2f3f4f5f6f7f8f9fafbfcfdff02",
 1405:                     ["f69f2445df4f","9b17ad2b41","7be66c3710"],
 1406:                     ["dfc9c5","8db67aada6","13c2dd08","457941a6"]}],
 1407:     lists:foreach(fun(S) -> aes_ctr_stream_do(Key256,S) end, Samples256).
 1408: 
 1409: 
 1410: aes_ctr_stream_do(Key,{IVec, PlainList, CipherList}) ->
 1411:     ?line I = hexstr2bin(IVec),
 1412:     ?line S = crypto:aes_ctr_stream_init(Key, I),
 1413:     ?line C = aes_ctr_stream_do_iter(
 1414:                 S, PlainList, [], 
 1415:                 fun(S2,P) -> crypto:aes_ctr_stream_encrypt(S2, P) end),
 1416:     ?line m(C, hexstr2bin(lists:flatten(CipherList))),
 1417:     ?line P = aes_ctr_stream_do_iter(
 1418:                 S, CipherList, [], 
 1419:                 fun(S2,C2) -> crypto:aes_ctr_stream_decrypt(S2, C2) end),
 1420:     ?line m(P, hexstr2bin(lists:flatten(PlainList))).
 1421: 
 1422: aes_ctr_stream_do_iter(_State, [], Acc, _CipherFun) ->
 1423:     iolist_to_binary(lists:reverse(Acc));
 1424: aes_ctr_stream_do_iter(State, [Plain|Rest], Acc, CipherFun) ->
 1425:     ?line P = hexstr2bin(Plain),
 1426:     ?line {S2, C} = CipherFun(State, P),
 1427:     aes_ctr_stream_do_iter(S2, Rest, [C | Acc], CipherFun).
 1428: 
 1429: %%
 1430: %%
 1431: mod_exp_test(doc) ->
 1432:     "mod_exp testing (A ^ M % P with bignums)";
 1433: mod_exp_test(suite) ->
 1434:     [];
 1435: mod_exp_test(Config) when is_list(Config) ->
 1436:     mod_exp_aux_test(2, 5, 10, 8).
 1437: 
 1438: mod_exp_aux_test(_, _, _, 0) ->
 1439:     ok;
 1440: mod_exp_aux_test(B, E, M, N) ->
 1441:     ?line R1 = crypto:mod_exp(B, E, M),
 1442:     ?line R2 = ipow(B, E, M),
 1443:     ?line m(R1, R2),
 1444:     ?line mod_exp_aux_test(B, E*E+1, M*M+1, N-1).
 1445: 
 1446: %%
 1447: %%
 1448: rand_uniform_test(doc) ->
 1449:     "rand_uniform and random_bytes testing";
 1450: rand_uniform_test(suite) ->
 1451:     [];
 1452: rand_uniform_test(Config) when is_list(Config) ->
 1453:     rand_uniform_aux_test(10),
 1454:     ?line 10 = size(crypto:rand_bytes(10)).
 1455: 
 1456: rand_uniform_aux_test(0) ->
 1457:     ok;
 1458: rand_uniform_aux_test(N) ->
 1459:     ?line L = N*1000,
 1460:     ?line H = N*100000+1,
 1461:     ?line crypto_rand_uniform(L, H),
 1462:     ?line crypto_rand_uniform(-L, L),
 1463:     ?line crypto_rand_uniform(-H, -L),
 1464:     ?line crypto_rand_uniform(-H, L),
 1465:     ?line rand_uniform_aux_test(N-1).
 1466: 
 1467: crypto_rand_uniform(L,H) ->
 1468:     ?line R1 = crypto:rand_uniform(L, H),
 1469:     ?line t(R1 >= L),
 1470:     ?line t(R1 < H).
 1471: 
 1472: 
 1473: %%
 1474: %%
 1475: strong_rand_test(doc) ->
 1476:     "strong_rand_mpint and strong_random_bytes testing";
 1477: strong_rand_test(suite) ->
 1478:     [];
 1479: strong_rand_test(Config) when is_list(Config) ->
 1480:     strong_rand_aux_test(180),
 1481:     ?line 10 = byte_size(crypto:strong_rand_bytes(10)).
 1482: 
 1483: strong_rand_aux_test(0) ->
 1484:     ?line t(crypto:strong_rand_mpint(0,0,0) =:= <<0,0,0,0>>),
 1485:     ok;
 1486: strong_rand_aux_test(1) ->
 1487:     ?line t(crypto:erlint(crypto:strong_rand_mpint(1,0,1)) =:= 1),
 1488:     ?line strong_rand_aux_test(0);
 1489: strong_rand_aux_test(N) ->
 1490:     ?line t(sru_length(crypto:strong_rand_mpint(N,-1,0)) =< N),
 1491:     ?line t(sru_length(crypto:strong_rand_mpint(N,0,0)) =:= N),
 1492:     ?line t(crypto:erlint(crypto:strong_rand_mpint(N,0,1)) band 1 =:= 1),
 1493:     ?line t(crypto:erlint(crypto:strong_rand_mpint(N,1,0)) bsr (N - 2) =:= 2#11),
 1494:     ?line strong_rand_aux_test(N-1).
 1495: 
 1496: sru_length(Mpint) ->
 1497:     I = crypto:erlint(Mpint),
 1498:     length(erlang:integer_to_list(I, 2)).
 1499: 
 1500: %%
 1501: %%
 1502: %%
 1503: %%
 1504: rsa_verify_test(doc) ->
 1505:     "rsa_verify testing (A ^ M % P with bignums)";
 1506: rsa_verify_test(suite) ->
 1507:     [];
 1508: rsa_verify_test(Config) when is_list(Config) ->
 1509:     ?line H = <<178,28,54,104,36,80,144,66,140,201,135,17,36,97,114,124,
 1510: 	       194,164,172,147>>,
 1511:     ?line SigBlob = <<153,44,121,71,132,1,192,159,78,33,29,62,153,64,191,70,
 1512: 		     208,239,166,208,220,167,49,111,128,67,91,253,24,63,194,241,
 1513: 		     97,157,135,226,121,162,150,156,60,49,236,90,151,67,239,23,
 1514: 		     92,103,89,254,17,165,78,181,64,128,13,210,86,111,209,76,
 1515: 		     115,34,107,227,151,47,80,185,143,85,202,55,245,163,226,26,
 1516: 		     139,104,196,6,96,82,108,197,13,0,12,70,153,109,107,180,
 1517: 		     130,246,156,182,56,96,31,220,227,218,136,211,252,43,8,14,
 1518: 		     145,155,191,206,72,194,80,52,54,206,53,27,6,188,195,29>>,
 1519:     ?line BadSigBlob = <<153,44,121,71,132,1,192,159,78,33,29,62,153,64,191,70,
 1520: 			208,239,166,208,220,167,49,111,128,67,91,253,24,63,194,241,
 1521: 			97,157,135,226,121,162,150,156,60,49,236,90,151,67,239,23,
 1522: 			92,103,89,254,17,165,78,181,64,128,13,210,86,111,209,76,
 1523: 			115,107,34,227,151,47,80,185,143,85,202,55,245,163,226,26,
 1524: 			139,104,196,6,96,82,108,197,13,0,12,70,153,109,107,180,
 1525: 			130,246,156,182,56,96,31,220,227,218,136,211,252,43,8,14,
 1526: 			145,155,191,206,72,194,80,52,54,206,53,27,6,188,195,29>>,
 1527:     ?line E = <<35>>,
 1528:     ?line N = <<0,199,209,142,191,86,92,148,103,37,250,217,175,169,109,10,
 1529: 	       130,139,34,237,174,90,97,118,7,185,57,137,252,236,177,193,
 1530: 	       228,16,62,29,153,144,64,207,152,240,152,206,136,89,64,6,
 1531: 	       3,187,89,57,241,219,88,215,75,70,120,20,145,229,37,1,
 1532: 	       67,138,204,17,39,231,249,239,116,142,169,99,149,41,65,123,
 1533: 	       26,225,133,0,41,85,77,181,35,100,162,223,92,220,207,50,
 1534: 	       63,168,193,171,174,199,23,214,201,63,157,76,125,6,54,73,
 1535: 	       76,89,40,33,147,208,189,76,98,24,61,8,10,110,165,119,165>>,
 1536:     ?line Nbad = <<0,199,209,142,191,86,92,148,103,37,250,217,175,169,109,10,
 1537: 		  130,139,34,237,174,90,97,118,7,185,57,137,252,236,177,193,
 1538: 		  228,16,62,29,153,144,64,207,152,240,152,206,136,89,64,6,
 1539: 		  3,187,89,57,241,219,88,215,75,70,120,20,145,229,37,1,
 1540: 		  67,138,204,17,39,231,249,239,116,142,169,99,149,41,65,123,
 1541: 		  26,225,133,0,41,85,77,181,35,100,162,223,92,220,207,50,
 1542: 		  63,168,193,171,174,199,23,214,201,63,157,76,125,6,54,73,
 1543: 		  76,89,40,33,147,189,208,76,98,24,61,8,10,110,165,119,165>>,
 1544:     ?line Ebad = <<77>>,
 1545:     ?line m(crypto:rsa_verify(sized_binary(H), sized_binary(SigBlob),
 1546: 			      [sized_binary(E), sized_binary(N)]), true),
 1547:     ?line m(crypto:rsa_verify(sized_binary(H), sized_binary(SigBlob),
 1548: 			      [sized_binary(Ebad), sized_binary(N)]), false),
 1549:     ?line m(crypto:rsa_verify(sized_binary(H), sized_binary(SigBlob),
 1550: 			      [sized_binary(E), sized_binary(Nbad)]), false),
 1551:     ?line m(crypto:rsa_verify(sized_binary(H), sized_binary(BadSigBlob),
 1552: 			      [sized_binary(E), sized_binary(N)]), false).
 1553: 
 1554: %%
 1555: %%
 1556: dsa_verify_test(doc) ->
 1557:     "dsa_verify testing (A ^ M % P with bignums)";
 1558: dsa_verify_test(suite) ->
 1559:     [];
 1560: dsa_verify_test(Config) when is_list(Config) ->
 1561:     ?line Msg = <<48,130,2,245,160,3,2,1,2,2,1,1,48,9,6,7,42,134,72,206,56,4,3,48,
 1562: 		 58,49,11,48,9,6,3,85,4,6,19,2,85,83,49,26,48,24,6,3,85,4,10,19,17,
 1563: 		 84,101,115,116,32,67,101,114,116,105,102,105,99,97,116,101,115,49,
 1564: 		 15,48,13,6,3,85,4,3,19,6,68,83,65,32,67,65,48,30,23,13,48,49,48,
 1565: 		 52,49,57,49,52,53,55,50,48,90,23,13,49,49,48,52,49,57,49,52,53,55,
 1566: 		 50,48,90,48,93,49,11,48,9,6,3,85,4,6,19,2,85,83,49,26,48,24,6,3,
 1567: 		 85,4,10,19,17,84,101,115,116,32,67,101,114,116,105,102,105,99,97,
 1568: 		 116,101,115,49,50,48,48,6,3,85,4,3,19,41,86,97,108,105,100,32,68,
 1569: 		 83,65,32,83,105,103,110,97,116,117,114,101,115,32,69,69,32,67,101,
 1570: 		 114,116,105,102,105,99,97,116,101,32,84,101,115,116,52,48,130,1,
 1571: 		 182,48,130,1,43,6,7,42,134,72,206,56,4,1,48,130,1,30,2,129,129,0,
 1572: 		 228,139,175,64,140,21,215,61,124,238,3,150,18,104,193,32,5,232,23,
 1573: 		 202,158,116,101,75,154,84,151,42,120,51,218,165,197,114,234,52,
 1574: 		 179,148,104,66,213,27,253,119,240,168,66,158,100,147,144,182,194,
 1575: 		 2,49,70,19,122,3,105,204,152,45,86,157,94,35,95,40,191,173,127,15,
 1576: 		 208,105,149,98,92,26,7,42,94,140,115,73,126,253,18,34,142,85,229,
 1577: 		 86,233,174,114,41,150,135,8,39,215,119,67,240,134,184,9,10,27,20,
 1578: 		 165,230,3,230,69,121,77,233,250,83,95,193,9,189,126,197,195,2,21,
 1579: 		 0,128,63,228,252,243,76,229,62,203,15,23,10,42,84,108,208,103,108,
 1580: 		 13,59,2,129,128,102,212,22,138,32,173,254,209,50,159,165,127,167,
 1581: 		 179,208,234,119,63,235,108,162,228,41,216,216,188,33,221,154,247,
 1582: 		 204,229,180,119,77,223,236,218,162,140,156,117,18,90,31,254,102,
 1583: 		 211,17,194,239,132,67,236,169,136,110,76,186,76,63,53,150,199,103,
 1584: 		 252,153,189,15,153,41,19,145,78,216,2,174,254,107,175,80,86,170,
 1585: 		 47,30,181,42,200,238,34,71,37,120,107,33,221,20,63,206,240,16,129,
 1586: 		 247,150,29,156,65,187,94,68,146,93,46,198,30,184,205,105,200,143,
 1587: 		 63,59,62,208,79,162,206,217,3,129,132,0,2,129,128,15,83,40,172,56,
 1588: 		 47,61,243,17,97,65,195,61,167,214,122,247,246,1,50,211,33,113,16,
 1589: 		 20,213,195,62,77,235,25,162,140,175,158,8,61,65,10,255,204,162,71,
 1590: 		 130,122,86,161,163,253,236,178,139,183,57,181,202,160,25,133,130,
 1591: 		 155,150,104,168,187,107,186,144,164,225,173,101,182,68,49,210,30,
 1592: 		 34,47,83,65,79,250,156,248,47,232,44,67,36,22,126,43,216,100,247,
 1593: 		 100,250,240,121,72,29,185,2,109,144,54,204,235,54,15,242,57,171,
 1594: 		 125,39,236,247,71,111,221,51,196,126,77,238,36,87,163,107,48,105,
 1595: 		 48,29,6,3,85,29,14,4,22,4,20,179,51,215,81,162,4,13,68,251,157,64,
 1596: 		 241,18,98,113,176,83,246,105,13,48,31,6,3,85,29,35,4,24,48,22,128,
 1597: 		 20,116,21,213,36,28,189,94,101,136,31,225,139,9,126,127,234,25,72,
 1598: 		 78,97,48,23,6,3,85,29,32,4,16,48,14,48,12,6,10,96,134,72,1,101,3,
 1599: 		 2,1,48,1,48,14,6,3,85,29,15,1,1,255,4,4,3,2,6,192>>,
 1600: 
 1601:     ?line SigBlob = <<48,45,2,21,0,140,167,200,210,153,212,64,155,249,33,146,104,243,
 1602: 		     39,38,9,115,162,89,24,2,20,76,254,31,128,187,48,128,215,216,
 1603: 		     112,198,78,118,160,217,157,180,246,64,234>>,
 1604:     ?line P_p = 157224271412839155721795253728878055347359513988016145491388196653004661857517720927482198111104095793441029858267073789634147217022008635826863307553453131345099940951090826856271796188522037524757740796268675508118348391218066949174594918958269259937813776150149068811425194955973128428675945283593831134219,
 1605:     ?line Q_p = 1181895316321540581845959276009400765315408342791,
 1606:     ?line G_p = 143872196713149000950547166575757355261637863805587906227228163275557375159769599033632918292482002186641475268486598023281100659643528846513898847919251032731261718358900479488287933293278745715922865499005559197328388506945134386346185262919258658109015074718441639029135304654725637911172671711310801418648,
 1607:     
 1608:     ?line Key = 12603618348903387232593303690286336220738319446775939686476278478034365380027994899970214309288018488811754534229198764622077544117034174589418477472887827980332636062691833965078594576024299807057520016043084384987871640003684704483975314128362610573625803532737054022545217931847268776098203204571431581966,
 1609:     
 1610:     ValidKey = [crypto:mpint(P_p), 
 1611: 		crypto:mpint(Q_p), 
 1612: 		crypto:mpint(G_p),
 1613: 		crypto:mpint(Key)
 1614: 	       ],
 1615:     
 1616:     ?line m(my_dss_verify(sized_binary(Msg), sized_binary(SigBlob),
 1617: 			      ValidKey), true),
 1618: 
 1619:     BadMsg  = one_bit_wrong(Msg),
 1620:     ?line m(my_dss_verify(sized_binary(BadMsg), sized_binary(SigBlob),
 1621: 			      ValidKey), false),
 1622:     BadSig = one_bit_wrong(SigBlob),
 1623:     ?line m(my_dss_verify(sized_binary(Msg), sized_binary(BadSig),
 1624: 			      ValidKey), false),
 1625:     SizeErr = size(SigBlob) - 13,
 1626:     
 1627:     BadArg = (catch my_dss_verify(sized_binary(Msg), <<SizeErr:32, SigBlob/binary>>,
 1628: 				      ValidKey)),
 1629:     badarg = case element(1,element(2,BadArg)) of
 1630: 		 badarg -> badarg;
 1631: 		 function_clause -> badarg;
 1632: 		 X -> X
 1633: 	     end,
 1634:     InValidKey = [crypto:mpint(P_p), 
 1635: 		  crypto:mpint(Q_p), 
 1636: 		  crypto:mpint(G_p),
 1637: 		  crypto:mpint(Key+17)
 1638: 		 ],
 1639:     
 1640:     ?line m(my_dss_verify(sized_binary(Msg), sized_binary(SigBlob),
 1641: 			      InValidKey), false).
 1642: 
 1643: 
 1644: one_bit_wrong(List) when is_list(List) ->
 1645:     lists:map(fun(Bin) -> one_bit_wrong(Bin) end, List);
 1646: one_bit_wrong(Bin) ->
 1647:     Half = size(Bin) div 2,
 1648:     <<First:Half/binary, Byte:8, Last/binary>> = Bin,
 1649:     <<First/binary, (Byte+1):8, Last/binary>>.
 1650: 
 1651: 
 1652: %%
 1653: %%  Sign tests
 1654: 
 1655: rsa_sign_test(doc) ->
 1656:     "rsa_sign testing";
 1657: rsa_sign_test(suite) ->
 1658:     [];
 1659: rsa_sign_test(Config) when is_list(Config) ->
 1660:     PubEx  = 65537,
 1661:     PrivEx = 7531712708607620783801185371644749935066152052780368689827275932079815492940396744378735701395659435842364793962992309884847527234216715366607660219930945,
 1662:     Mod = 7919488123861148172698919999061127847747888703039837999377650217570191053151807772962118671509138346758471459464133273114654252861270845708312601272799123,
 1663:     Msg = <<"7896345786348756234 Hejsan Svejsan, erlang crypto debugger"
 1664: 	   "09812312908312378623487263487623412039812 huagasd">>,
 1665:     
 1666:     PrivKey = [PubEx, Mod, PrivEx],
 1667:     PubKey  = [PubEx, Mod],
 1668:     PubKeyMpint = map_int_to_mpint(PubKey),
 1669:     Sig1 = crypto:rsa_sign(sized_binary(Msg), map_int_to_mpint(PrivKey)),
 1670:     Sig1 = crypto:sign(rsa, sha, Msg, PrivKey),
 1671:     true = crypto:rsa_verify(sized_binary(Msg), sized_binary(Sig1), PubKeyMpint),
 1672:     true = crypto:verify(rsa, sha, Msg, Sig1, PubKey),
 1673:     
 1674:     Sig2 = crypto:rsa_sign(md5, sized_binary(Msg), map_int_to_mpint(PrivKey)),
 1675:     Sig2 = crypto:sign(rsa, md5, Msg, PrivKey),
 1676:     true = crypto:rsa_verify(md5, sized_binary(Msg), sized_binary(Sig2), PubKeyMpint),
 1677:     true = crypto:verify(rsa, md5, Msg, Sig2, PubKey),
 1678:     
 1679:     false = (Sig1 =:= Sig2),
 1680:     false = crypto:rsa_verify(md5, sized_binary(Msg), sized_binary(Sig1), PubKeyMpint),
 1681:     false = crypto:verify(rsa, md5, Msg, Sig1, PubKey),
 1682:     true = crypto:rsa_verify(sha, sized_binary(Msg), sized_binary(Sig1), PubKeyMpint),
 1683:     true = crypto:verify(rsa, sha, Msg, Sig1, PubKey),
 1684: 
 1685:     ok.
 1686: map_int_to_mpint(List) ->
 1687:     lists:map(fun(E) -> crypto:mpint(E) end, List).
 1688: 
 1689: rsa_sign_hash_test(doc) ->
 1690:     "rsa_sign_hash testing";
 1691: rsa_sign_hash_test(suite) ->
 1692:     [];
 1693: rsa_sign_hash_test(Config) when is_list(Config) ->
 1694:     PubEx  = 65537,
 1695:     PrivEx = 7531712708607620783801185371644749935066152052780368689827275932079815492940396744378735701395659435842364793962992309884847527234216715366607660219930945,
 1696:     Mod = 7919488123861148172698919999061127847747888703039837999377650217570191053151807772962118671509138346758471459464133273114654252861270845708312601272799123,
 1697:     Msg = <<"7896345786348756234 Hejsan Svejsan, erlang crypto debugger"
 1698: 	   "09812312908312378623487263487623412039812 huagasd">>,
 1699: 
 1700:     PrivKey = [crypto:mpint(PubEx), crypto:mpint(Mod), crypto:mpint(PrivEx)],
 1701:     PubKey  = [crypto:mpint(PubEx), crypto:mpint(Mod)],
 1702:     MD5 = crypto:md5(sized_binary(Msg)),
 1703:     SHA = crypto:sha(sized_binary(Msg)),
 1704:     ?line Sig1 = crypto:rsa_sign(sha, {digest,SHA}, PrivKey),
 1705:     ?line m(crypto:rsa_verify(sha, {digest,SHA}, sized_binary(Sig1),PubKey), true),
 1706: 
 1707:     ?line Sig2 = crypto:rsa_sign(md5, {digest,MD5}, PrivKey),
 1708:     ?line m(crypto:rsa_verify(md5, {digest,MD5}, sized_binary(Sig2),PubKey), true),
 1709: 
 1710:     ?line m(Sig1 =:= Sig2, false),
 1711:     ?line m(crypto:rsa_verify(md5, {digest,MD5}, sized_binary(Sig1),PubKey), false),
 1712:     ?line m(crypto:rsa_verify(sha, {digest,SHA}, sized_binary(Sig2),PubKey), false),
 1713: 
 1714:     ok.
 1715: 
 1716: dsa_sign_test(doc) ->
 1717:     "dsa_sign testing";
 1718: dsa_sign_test(suite) ->
 1719:     [];
 1720: dsa_sign_test(Config) when is_list(Config) ->
 1721:     Msg = <<"7896345786348756234 Hejsan Svejsan, erlang crypto debugger"
 1722: 	   "09812312908312378623487263487623412039812 huagasd">>,
 1723: 
 1724:     PubKey = _Y = 25854665488880835237281628794585130313500176551981812527054397586638455298000483144002221850980183404910190346416063318160497344811383498859129095184158800144312512447497510551471331451396405348497845813002058423110442376886564659959543650802132345311573634832461635601376738282831340827591903548964194832978,
 1725:     PrivKey = _X = 441502407453038284293378221372000880210588566361,
 1726:     ParamP = 109799869232806890760655301608454668257695818999841877165019612946154359052535682480084145133201304812979481136659521529774182959764860329095546511521488413513097576425638476458000255392402120367876345280670101492199681798674053929238558140260669578407351853803102625390950534052428162468100618240968893110797,
 1727:     ParamQ = 1349199015905534965792122312016505075413456283393,
 1728:     ParamG = 18320614775012672475365915366944922415598782131828709277168615511695849821411624805195787607930033958243224786899641459701930253094446221381818858674389863050420226114787005820357372837321561754462061849169568607689530279303056075793886577588606958623645901271866346406773590024901668622321064384483571751669,
 1729: 
 1730:     Params = [crypto:mpint(ParamP), crypto:mpint(ParamQ), crypto:mpint(ParamG)],
 1731:     ?line Sig1 = my_dss_sign(sized_binary(Msg), Params ++ [crypto:mpint(PrivKey)]),
 1732:     
 1733:     ?line m(my_dss_verify(sized_binary(Msg), Sig1, 
 1734: 			      Params ++ [crypto:mpint(PubKey)]), true),
 1735:     
 1736:     ?line m(my_dss_verify(sized_binary(one_bit_wrong(Msg)), Sig1, 
 1737: 			      Params ++ [crypto:mpint(PubKey)]), false),
 1738:     
 1739:     ?line m(my_dss_verify(sized_binary(Msg), one_bit_wrong(Sig1), 
 1740: 			      Params ++ [crypto:mpint(PubKey)]), false),
 1741: 
 1742:     %%?line Bad = crypto:dss_sign(sized_binary(Msg), [Params, crypto:mpint(PubKey)]),
 1743:     
 1744:     ok.
 1745: 
 1746: dsa_sign_hash_test(doc) ->
 1747:     "dsa_sign_hash testing";
 1748: dsa_sign_hash_test(suite) ->
 1749:     [];
 1750: dsa_sign_hash_test(Config) when is_list(Config) ->
 1751:     Msg = <<"7896345786348756234 Hejsan Svejsan, erlang crypto debugger"
 1752: 	   "09812312908312378623487263487623412039812 huagasd">>,
 1753:     SHA = crypto:sha(sized_binary(Msg)),
 1754: 
 1755:     PubKey = _Y = 25854665488880835237281628794585130313500176551981812527054397586638455298000483144002221850980183404910190346416063318160497344811383498859129095184158800144312512447497510551471331451396405348497845813002058423110442376886564659959543650802132345311573634832461635601376738282831340827591903548964194832978,
 1756:     PrivKey = _X = 441502407453038284293378221372000880210588566361,
 1757:     ParamP = 109799869232806890760655301608454668257695818999841877165019612946154359052535682480084145133201304812979481136659521529774182959764860329095546511521488413513097576425638476458000255392402120367876345280670101492199681798674053929238558140260669578407351853803102625390950534052428162468100618240968893110797,
 1758:     ParamQ = 1349199015905534965792122312016505075413456283393,
 1759:     ParamG = 18320614775012672475365915366944922415598782131828709277168615511695849821411624805195787607930033958243224786899641459701930253094446221381818858674389863050420226114787005820357372837321561754462061849169568607689530279303056075793886577588606958623645901271866346406773590024901668622321064384483571751669,
 1760: 
 1761:     Params = [crypto:mpint(ParamP), crypto:mpint(ParamQ), crypto:mpint(ParamG)],
 1762:     ?line Sig1 = crypto:dss_sign(sha, {digest,SHA}, Params ++ [crypto:mpint(PrivKey)]),
 1763: 
 1764:     ?line m(crypto:dss_verify(none, SHA, sized_binary(Sig1),
 1765: 			      Params ++ [crypto:mpint(PubKey)]), true),
 1766: 
 1767:     ?line m(crypto:dss_verify(sized_binary(one_bit_wrong(Msg)), sized_binary(Sig1),
 1768: 			      Params ++ [crypto:mpint(PubKey)]), false),
 1769: 
 1770:     ?line m(crypto:dss_verify(sized_binary(Msg), sized_binary(one_bit_wrong(Sig1)),
 1771: 			      Params ++ [crypto:mpint(PubKey)]), false),
 1772: 
 1773:     %%?line Bad = crypto:dss_sign(sized_binary(Msg), [Params, crypto:mpint(PubKey)]),
 1774: 
 1775:     ok.
 1776: 
 1777: 
 1778: rsa_encrypt_decrypt(doc) ->
 1779:     ["Test rsa_public_encrypt and rsa_private_decrypt functions."];
 1780: rsa_encrypt_decrypt(suite) -> [];
 1781: rsa_encrypt_decrypt(Config) when is_list(Config) ->
 1782:     PubEx  = 65537,
 1783:     PrivEx = 7531712708607620783801185371644749935066152052780368689827275932079815492940396744378735701395659435842364793962992309884847527234216715366607660219930945,
 1784:     Mod = 7919488123861148172698919999061127847747888703039837999377650217570191053151807772962118671509138346758471459464133273114654252861270845708312601272799123,
 1785:     
 1786:     PrivKey = [PubEx, Mod, PrivEx],
 1787:     PubKey  = [PubEx, Mod],
 1788: 
 1789:     Msg = <<"7896345786348 Asldi">>,
 1790: 
 1791:     ?line PKCS1 = rsa_public_encrypt(Msg, PubKey, rsa_pkcs1_padding),
 1792:     ?line PKCS1Dec = rsa_private_decrypt(PKCS1, PrivKey, rsa_pkcs1_padding),
 1793:     io:format("PKCS1Dec ~p~n",[PKCS1Dec]),
 1794:     ?line Msg = PKCS1Dec,
 1795:     
 1796:     ?line OAEP = rsa_public_encrypt(Msg, PubKey, rsa_pkcs1_oaep_padding),
 1797:     ?line Msg = rsa_private_decrypt(OAEP, PrivKey, rsa_pkcs1_oaep_padding),
 1798: 
 1799:     <<Msg2Len:32,_/binary>> = crypto:mpint(Mod),
 1800:     Msg2 = list_to_binary(lists:duplicate(Msg2Len-1, $X)),
 1801:     ?line NoPad = rsa_public_encrypt(Msg2, PubKey, rsa_no_padding),
 1802:     ?line NoPadDec = rsa_private_decrypt(NoPad, PrivKey, rsa_no_padding),
 1803:     ?line NoPadDec = Msg2,
 1804:     
 1805:     ShouldBeError = (catch rsa_public_encrypt(Msg, PubKey, rsa_no_padding)),
 1806:     ?line {'EXIT', {encrypt_failed,_}} = ShouldBeError,
 1807:     
 1808: %%     ?line SSL = rsa_public_encrypt(Msg, PubKey, rsa_sslv23_padding),
 1809: %%     ?line Msg = rsa_private_decrypt(SSL, PrivKey, rsa_sslv23_padding),
 1810: 
 1811:     ?line PKCS1_2 = rsa_private_encrypt(Msg, PrivKey, rsa_pkcs1_padding),
 1812:     ?line PKCS1_2Dec = rsa_public_decrypt(PKCS1_2, PubKey, rsa_pkcs1_padding),
 1813:     io:format("PKCS2Dec ~p~n",[PKCS1_2Dec]),
 1814:     ?line Msg = PKCS1_2Dec,
 1815: 
 1816:     ?line PKCS1_3 = rsa_private_encrypt(Msg2, PrivKey, rsa_no_padding),
 1817:     ?line PKCS1_3Dec = rsa_public_decrypt(PKCS1_3, PubKey, rsa_no_padding),
 1818:     io:format("PKCS2Dec ~p~n",[PKCS1_3Dec]),
 1819:     ?line Msg2 = PKCS1_3Dec,
 1820:     
 1821:     ?line {'EXIT', {encrypt_failed,_}} = 
 1822: 	(catch rsa_private_encrypt(Msg, PrivKey, rsa_no_padding)),
 1823:     
 1824:     ok.
 1825: 
 1826: rsa_public_encrypt(Msg, Key, Pad) ->
 1827:     C1 = crypto:rsa_public_encrypt(Msg, Key, Pad),
 1828:     C2 = crypto:rsa_public_encrypt(Msg, lists:map(fun(E) -> crypto:mpint(E) end, Key), Pad),
 1829:     {C1,C2}.
 1830: 
 1831: rsa_public_decrypt(Msg, Key, Pad) ->
 1832:     R = crypto:rsa_public_decrypt(Msg, Key, Pad),
 1833:     R = crypto:rsa_public_decrypt(Msg, lists:map(fun(E) -> crypto:mpint(E) end, Key), Pad).
 1834: 
 1835: rsa_private_encrypt(Msg, Key, Pad) ->
 1836:     R = crypto:rsa_private_encrypt(Msg, Key, Pad),
 1837:     R = crypto:rsa_private_encrypt(Msg, lists:map(fun(E) -> crypto:mpint(E) end, Key), Pad).
 1838: 
 1839: rsa_private_decrypt({C1,C2}, Key, Pad) ->
 1840:     R = crypto:rsa_private_decrypt(C1, Key, Pad),
 1841:     R = crypto:rsa_private_decrypt(C2, Key, Pad),
 1842:     R = crypto:rsa_private_decrypt(C1, lists:map(fun(E) -> crypto:mpint(E) end, Key), Pad),
 1843:     R = crypto:rsa_private_decrypt(C2, lists:map(fun(E) -> crypto:mpint(E) end, Key), Pad).
 1844: 
 1845: 
 1846: dh(doc) ->
 1847:     ["Test dh (Diffie-Hellman) functions."];
 1848: dh(suite) -> [];
 1849: dh(Config) when is_list(Config) ->
 1850:     Self = self(),
 1851:     GenP = fun() ->
 1852: 		   %% Gen Param may take arbitrary long time to finish 
 1853: 		   %% That's not a bug in erlang crypto application.
 1854: 		   ?line DHPs = crypto:dh_generate_parameters(512,2),
 1855: 		   ?line ok = crypto:dh_check(DHPs),
 1856: 		   Self ! {param, DHPs}
 1857: 	   end,
 1858:     Pid = spawn(GenP),
 1859:     receive 
 1860: 	{param, DHPs} ->
 1861: 	    timer:sleep(100), 
 1862: 	    io:format("DHP ~p~n", [DHPs]),
 1863: 	    DHPs_mpint = lists:map(fun(E) -> sized_binary(E) end, DHPs),
 1864: 	    ?line {Pub1,Priv1} = crypto:generate_key(dh, DHPs),
 1865: 	    io:format("Key1:~n~p~n~p~n~n", [Pub1,Priv1]),
 1866: 	    ?line {Pub2,Priv2} = crypto:dh_generate_key(DHPs_mpint),
 1867: 	    io:format("Key2:~n~p~n~p~n~n", [Pub2,Priv2]),
 1868: 	    ?line A = crypto:compute_key(dh, Pub1, unsized_binary(Priv2), DHPs),
 1869: 	    ?line A = crypto:dh_compute_key(sized_binary(Pub1), Priv2, DHPs_mpint),
 1870: 	    timer:sleep(100),  %% Get another thread see if that triggers problem
 1871: 	    ?line B = crypto:compute_key(dh, unsized_binary(Pub2), Priv1, DHPs),
 1872: 	    ?line B = crypto:dh_compute_key(Pub2, sized_binary(Priv1), DHPs_mpint),
 1873: 	    io:format("A ~p~n",[A]),
 1874: 	    io:format("B ~p~n",[B]),
 1875: 	    ?line A = B
 1876:     after 50000 ->
 1877: 	    io:format("Killing Param generation which took to long ~p~n",[Pid]),
 1878: 	    exit(Pid, kill)
 1879:     end.
 1880: 
 1881: 
 1882: ec(doc) ->
 1883:     ["Test ec (Ecliptic Curve) functions."];
 1884: ec(suite) -> [];
 1885: ec(Config) when is_list(Config) ->
 1886:     if_supported(ecdh, fun() -> ec_do() end).
 1887: 
 1888: ec_do() ->
 1889:     %% test for a name curve
 1890:     {D2_pub, D2_priv} = crypto:generate_key(ecdh, sect113r2),
 1891:     PrivECDH = [D2_priv, sect113r2],
 1892:     PubECDH = [D2_pub, sect113r2],
 1893:     %%TODO: find a published test case for a EC key
 1894: 
 1895:     %% test for a full specified curve and public key,
 1896:     %% taken from csca-germany_013_self_signed_cer.pem
 1897:     PubKey = <<16#04, 16#4a, 16#94, 16#49, 16#81, 16#77, 16#9d, 16#df,
 1898: 	       16#1d, 16#a5, 16#e7, 16#c5, 16#27, 16#e2, 16#7d, 16#24,
 1899: 	       16#71, 16#a9, 16#28, 16#eb, 16#4d, 16#7b, 16#67, 16#75,
 1900: 	       16#ae, 16#09, 16#0a, 16#51, 16#45, 16#19, 16#9b, 16#d4,
 1901: 	       16#7e, 16#a0, 16#81, 16#e5, 16#5e, 16#d4, 16#a4, 16#3f,
 1902: 	       16#60, 16#7c, 16#6a, 16#50, 16#ee, 16#36, 16#41, 16#8a,
 1903: 	       16#87, 16#ff, 16#cd, 16#a6, 16#10, 16#39, 16#ca, 16#95,
 1904: 	       16#76, 16#7d, 16#ae, 16#ca, 16#c3, 16#44, 16#3f, 16#e3, 16#2c>>,
 1905:     <<P:264/integer>> = <<16#00, 16#a9, 16#fb, 16#57, 16#db, 16#a1, 16#ee, 16#a9,
 1906:                           16#bc, 16#3e, 16#66, 16#0a, 16#90, 16#9d, 16#83, 16#8d,
 1907: 			  16#72, 16#6e, 16#3b, 16#f6, 16#23, 16#d5, 16#26, 16#20,
 1908: 			  16#28, 16#20, 16#13, 16#48, 16#1d, 16#1f, 16#6e, 16#53, 16#77>>,
 1909:     <<A:256/integer>> = <<16#7d, 16#5a, 16#09, 16#75, 16#fc, 16#2c, 16#30, 16#57,
 1910: 			  16#ee, 16#f6, 16#75, 16#30, 16#41, 16#7a, 16#ff, 16#e7,
 1911: 			  16#fb, 16#80, 16#55, 16#c1, 16#26, 16#dc, 16#5c, 16#6c,
 1912: 			  16#e9, 16#4a, 16#4b, 16#44, 16#f3, 16#30, 16#b5, 16#d9>>,
 1913:     <<B:256/integer>> = <<16#26, 16#dc, 16#5c, 16#6c, 16#e9, 16#4a, 16#4b, 16#44,
 1914: 			  16#f3, 16#30, 16#b5, 16#d9, 16#bb, 16#d7, 16#7c, 16#bf,
 1915: 			  16#95, 16#84, 16#16, 16#29, 16#5c, 16#f7, 16#e1, 16#ce,
 1916: 			  16#6b, 16#cc, 16#dc, 16#18, 16#ff, 16#8c, 16#07, 16#b6>>,
 1917:     BasePoint = <<16#04, 16#8b, 16#d2, 16#ae, 16#b9, 16#cb, 16#7e, 16#57,
 1918: 		  16#cb, 16#2c, 16#4b, 16#48, 16#2f, 16#fc, 16#81, 16#b7,
 1919: 		  16#af, 16#b9, 16#de, 16#27, 16#e1, 16#e3, 16#bd, 16#23,
 1920: 		  16#c2, 16#3a, 16#44, 16#53, 16#bd, 16#9a, 16#ce, 16#32,
 1921: 		  16#62, 16#54, 16#7e, 16#f8, 16#35, 16#c3, 16#da, 16#c4,
 1922: 		  16#fd, 16#97, 16#f8, 16#46, 16#1a, 16#14, 16#61, 16#1d,
 1923: 		  16#c9, 16#c2, 16#77, 16#45, 16#13, 16#2d, 16#ed, 16#8e,
 1924: 		  16#54, 16#5c, 16#1d, 16#54, 16#c7, 16#2f, 16#04, 16#69, 16#97>>,
 1925:     <<Order:264/integer>> = <<16#00, 16#a9, 16#fb, 16#57, 16#db, 16#a1, 16#ee, 16#a9,
 1926: 			      16#bc, 16#3e, 16#66, 16#0a, 16#90, 16#9d, 16#83, 16#8d,
 1927: 			      16#71, 16#8c, 16#39, 16#7a, 16#a3, 16#b5, 16#61, 16#a6,
 1928: 			      16#f7, 16#90, 16#1e, 16#0e, 16#82, 16#97, 16#48, 16#56, 16#a7>>,
 1929:     CoFactor = 1,
 1930:     Curve = {{prime_field,P},{A,B,none},BasePoint, Order,CoFactor},
 1931: 
 1932:     Msg = <<99,234,6,64,190,237,201,99,80,248,58,40,70,45,149,218,5,246,242,63>>,
 1933:     Sign = crypto:sign(ecdsa, sha, Msg, PrivECDH),
 1934:     ?line true = crypto:verify(ecdsa, sha, Msg, Sign, PubECDH),
 1935:     ?line false = crypto:verify(ecdsa, sha, Msg, <<10,20>>, PubECDH),
 1936: 
 1937:     ok.
 1938: 
 1939: srp3(doc) ->
 1940:     ["SRP-3 test vectors generated by http://srp.stanford.edu/demo/demo.html"];
 1941: srp3(suite) -> [];
 1942: srp3(Config) when is_list(Config) ->
 1943:     Username = <<"alice">>,
 1944:     Password = <<"password123">>,
 1945:     Salt = hexstr2bin("2857827A19266A1F2BC6"),
 1946:     Prime = hexstr2bin("EEAF0AB9ADB38DD69C33F80AFA8FC5E86072618775FF3C0B9EA2314C"
 1947: 		       "9C256576D674DF7496EA81D3383B4813D692C6E0E0D5D8E250B98BE4"
 1948: 		       "8E495C1D6089DAD15DC7D7B46154D6B6CE8EF4AD69B15D4982559B29"
 1949: 		       "7BCF1885C529F566660E57EC68EDBC3C05726CC02FD4CBF4976EAA9A"
 1950: 		       "FD5138FE8376435B9FC61D2FC0EB06E3"),
 1951:     Generator = <<2>>,
 1952:     Version = '3',
 1953:     Scrambler = hexstr2bin("02E2476A"),
 1954: 
 1955:     %% X = hexstr2bin("96E54AB0CD4C5123EDCFA4A1502918AAD3C9E2A8"),
 1956:     Verifier = hexstr2bin("96EB5F13621D911AA1CA405DE9C64217D4108EEEECAFFE500034FE0E"
 1957: 			  "C031E42C8714667C161BCE0E7996F7DDE1B63824C130D2D7286C08C0"
 1958: 			  "49758420735961347112AE102A3F23B3F687F8FEE0DF2BFAF933C608"
 1959: 			  "D6FE5B5EEE3116FE54016E065BF8E8C9FDBBC08719231AC215149140"
 1960: 			  "519E8FDD9AA4F410C28A58AF42974D2D"),
 1961:     ClientPrivate = hexstr2bin("6411DE75538BED8170677D577D0608F39112BC95B503C447EB6AC945"
 1962: 			  "49C75C7B"),
 1963:     ServerPrivate = hexstr2bin("85E44A6F694DBE676145DB245A045CD37C99F05C562C7840A31F270D"
 1964: 			  "9AADCF8B"),
 1965:     ClientPublic = hexstr2bin("B22B1FFA2244B8CB94F3A9080F419CAEAB0DBA93EA1965B5E84587EE"
 1966: 			 "55C79E7A118865DC59B9D0353362C2A8261E7C1B0D221A0E233C2AD1"
 1967: 			 "640DACBB8664CBC9733EAC392DA7800142860380C3FC573C3C064329"
 1968: 			 "CF54063FD114C7210E9CB3A611EA8002B1844B698F930D95D143899B"
 1969: 			 "948A090E0C25938E5F84067D1883DC63"),
 1970:     ServerPublic = hexstr2bin("93A8C4D8B7F7395ADCFD4ABA37B015124513D3F37B3E85EB23064BE5"
 1971: 			 "F53C0AE32FFB9D8C0AA0DCFFA74D632DD67DEBB5C35AAE9812286CC8"
 1972: 			 "C43CC176ECBC6D3F447594D9554E995B2509127BF88FADDDA4982D03"
 1973: 			 "8EC3001320712D3B1269308CE70F319B2295FA57674F03A2D993CFB1"
 1974: 			 "F84C35B7D0C012FA73CD4C8F7D5A71C7"),
 1975: 
 1976:     SessionKey = hexstr2bin("C29A986C4D521BBC66428ED11D994CD7431574A6184B83CDCC345092"
 1977: 			    "791E75748A1D38CAC4BD14760F0D2694B711236419240FF2F172454C"
 1978: 			    "46ABF4FF39498DAFDD2C82924F7D7BD76CDFCE688C77D93F18A65409"
 1979: 			    "9176A9192615DC0277AE7C12F1F6A7F6563FCA11675D809AF578BDE5"
 1980: 			    "2B51E05D440B63099A017A0B45044801"),
 1981:     UserPassHash = crypto:sha([Salt, crypto:sha([Username, <<$:>>, Password])]),
 1982:     Verifier = crypto:mod_pow(Generator, UserPassHash, Prime), 
 1983:     ClientPublic = crypto:mod_pow(Generator, ClientPrivate, Prime),
 1984: 
 1985:     {ClientPublic, ClientPrivate} = crypto:generate_key(srp, {user, [Generator, Prime, Version]}, ClientPrivate),
 1986:     {ServerPublic, ServerPrivate} = crypto:generate_key(srp, {host, [Verifier, Generator, Prime, Version]}, ServerPrivate),
 1987:     SessionKey = crypto:compute_key(srp, ServerPublic, {ClientPublic, ClientPrivate},
 1988: 				    {user, [UserPassHash, Prime, Generator, Version, Scrambler]}),
 1989:     SessionKey = crypto:compute_key(srp, ClientPublic, {ServerPublic, ServerPrivate},
 1990: 				    {host, [Verifier, Prime, Version, Scrambler]}).
 1991: 
 1992: srp6(doc) ->
 1993:     ["SRP-6 test vectors generated by http://srp.stanford.edu/demo/demo.html"];
 1994: srp6(suite) -> [];
 1995: srp6(Config) when is_list(Config) ->
 1996:     Username = <<"alice">>,
 1997:     Password = <<"password123">>,
 1998:     Salt = hexstr2bin("2857827A19266A1F2BC6"),
 1999:     Prime = hexstr2bin("EEAF0AB9ADB38DD69C33F80AFA8FC5E86072618775FF3C0B9EA2314C"
 2000: 		       "9C256576D674DF7496EA81D3383B4813D692C6E0E0D5D8E250B98BE4"
 2001: 		       "8E495C1D6089DAD15DC7D7B46154D6B6CE8EF4AD69B15D4982559B29"
 2002: 		       "7BCF1885C529F566660E57EC68EDBC3C05726CC02FD4CBF4976EAA9A"
 2003: 		       "FD5138FE8376435B9FC61D2FC0EB06E3"),
 2004:     Generator = <<2>>,
 2005:     Version = '6',
 2006:     Scrambler = hexstr2bin("0A2534C0BF52A0DA9001EEC62CF2A546AB0908A7"),
 2007:     Verifier = hexstr2bin("96EB5F13621D911AA1CA405DE9C64217D4108EEEECAFFE500034FE0E"
 2008: 			  "C031E42C8714667C161BCE0E7996F7DDE1B63824C130D2D7286C08C0"
 2009: 			  "49758420735961347112AE102A3F23B3F687F8FEE0DF2BFAF933C608"
 2010: 			  "D6FE5B5EEE3116FE54016E065BF8E8C9FDBBC08719231AC215149140"
 2011: 			  "519E8FDD9AA4F410C28A58AF42974D2D"),
 2012:     ClientPrivate = hexstr2bin("6411DE75538BED8170677D577D0608F39112BC95B503C447EB6AC945"
 2013: 			  "49C75C7B"),
 2014:     ServerPrivate = hexstr2bin("85E44A6F694DBE676145DB245A045CD37C99F05C562C7840A31F270D"
 2015: 			  "9AADCF8B"),
 2016:     ClientPublic = hexstr2bin("B22B1FFA2244B8CB94F3A9080F419CAEAB0DBA93EA1965B5E84587EE"
 2017: 			 "55C79E7A118865DC59B9D0353362C2A8261E7C1B0D221A0E233C2AD1"
 2018: 			 "640DACBB8664CBC9733EAC392DA7800142860380C3FC573C3C064329"
 2019: 			 "CF54063FD114C7210E9CB3A611EA8002B1844B698F930D95D143899B"
 2020: 			 "948A090E0C25938E5F84067D1883DC63"),
 2021:     ServerPublic = hexstr2bin("D2D07845CE7ECDB9845DD36B10ACD3598CC29049DE9F467F84CE16B6"
 2022: 			 "D97A6DC567AF8B0F9FEDF74962400AD5C357951E64E67B641246F264"
 2023: 			 "C8DE6D9A72E554D6C8D3194548780A0C438A0FCC509CA88A14AA1DEB"
 2024: 			 "C0F09E4B37A965D1545DB4AD361346F3189B0EA569C06D326C4E4797"
 2025: 			 "9E381C748293B7C0591BE0BE419E053E"),
 2026: 
 2027:     SessionKey = hexstr2bin("19D22C19612874EBF1F2581F8EFCFDC44C6FDA3B87B0A73823D7E962"
 2028: 				 "554295D4E48D3A336523ADBDDD0EC8FB0F02687109E97E01C17C93CC"
 2029: 				 "7216F9CD8A4AC39F0429857D8D1023066614BDFCBCB89F59A0FEB81C"
 2030: 				 "72E992AAD89095A84B6A5FADA152369AB1E350A03693BEF044DF3EDF"
 2031: 				 "0C34741F4696C30E9F675D09F58ACBEB"),
 2032:     UserPassHash = crypto:sha([Salt, crypto:sha([Username, <<$:>>, Password])]),
 2033:     Verifier = crypto:mod_pow(Generator, UserPassHash, Prime), 
 2034:     ClientPublic = crypto:mod_pow(Generator, ClientPrivate, Prime),
 2035: 
 2036:     {ClientPublic, ClientPrivate} = crypto:generate_key(srp, {user, [Generator, Prime, Version]}, ClientPrivate),
 2037:     {ServerPublic, ServerPrivate} = crypto:generate_key(srp, {host, [Verifier, Generator, Prime, Version]}, ServerPrivate),
 2038:     SessionKey = crypto:compute_key(srp, ServerPublic, {ClientPublic, ClientPrivate},
 2039: 				    {user, [UserPassHash, Prime, Generator, Version, Scrambler]}),
 2040:     SessionKey = crypto:compute_key(srp, ClientPublic, {ServerPublic, ServerPrivate},
 2041: 				    {host, [Verifier, Prime, Version, Scrambler]}).
 2042: 
 2043: srp6a(doc) ->
 2044:     ["SRP-6a test vectors from RFC5054."];
 2045: srp6a(suite) -> [];
 2046: srp6a(Config) when is_list(Config) ->
 2047:     Username = <<"alice">>,
 2048:     Password = <<"password123">>,
 2049:     Salt = hexstr2bin("BEB25379D1A8581EB5A727673A2441EE"),
 2050:     Prime = hexstr2bin("EEAF0AB9ADB38DD69C33F80AFA8FC5E86072618775FF3C0B9EA2314C"
 2051: 		       "9C256576D674DF7496EA81D3383B4813D692C6E0E0D5D8E250B98BE4"
 2052: 		       "8E495C1D6089DAD15DC7D7B46154D6B6CE8EF4AD69B15D4982559B29"
 2053: 		       "7BCF1885C529F566660E57EC68EDBC3C05726CC02FD4CBF4976EAA9A"
 2054: 		       "FD5138FE8376435B9FC61D2FC0EB06E3"),
 2055:     Generator = <<2>>,
 2056:     Version = '6a',
 2057:     Scrambler = hexstr2bin("CE38B9593487DA98554ED47D70A7AE5F462EF019"),
 2058:     Verifier = hexstr2bin("7E273DE8696FFC4F4E337D05B4B375BEB0DDE1569E8FA00A9886D812"
 2059: 			  "9BADA1F1822223CA1A605B530E379BA4729FDC59F105B4787E5186F5"
 2060: 			  "C671085A1447B52A48CF1970B4FB6F8400BBF4CEBFBB168152E08AB5"
 2061: 			  "EA53D15C1AFF87B2B9DA6E04E058AD51CC72BFC9033B564E26480D78"
 2062: 			  "E955A5E29E7AB245DB2BE315E2099AFB"),
 2063:     ClientPrivate = hexstr2bin("60975527035CF2AD1989806F0407210BC81EDC04E2762A56AFD529DD"
 2064: 			  "DA2D4393"),
 2065:     ServerPrivate = hexstr2bin("E487CB59D31AC550471E81F00F6928E01DDA08E974A004F49E61F5D1"
 2066: 			  "05284D20"),
 2067:     ClientPublic = hexstr2bin("61D5E490F6F1B79547B0704C436F523DD0E560F0C64115BB72557EC4"
 2068: 			 "4352E8903211C04692272D8B2D1A5358A2CF1B6E0BFCF99F921530EC"
 2069: 			 "8E39356179EAE45E42BA92AEACED825171E1E8B9AF6D9C03E1327F44"
 2070: 			 "BE087EF06530E69F66615261EEF54073CA11CF5858F0EDFDFE15EFEA"
 2071: 			 "B349EF5D76988A3672FAC47B0769447B"),
 2072:     ServerPublic = hexstr2bin("BD0C61512C692C0CB6D041FA01BB152D4916A1E77AF46AE105393011"
 2073: 			 "BAF38964DC46A0670DD125B95A981652236F99D9B681CBF87837EC99"
 2074: 			 "6C6DA04453728610D0C6DDB58B318885D7D82C7F8DEB75CE7BD4FBAA"
 2075: 			 "37089E6F9C6059F388838E7A00030B331EB76840910440B1B27AAEAE"
 2076: 			 "EB4012B7D7665238A8E3FB004B117B58"),
 2077:  
 2078:     SessionKey = hexstr2bin("B0DC82BABCF30674AE450C0287745E7990A3381F63B387AAF271A10D"
 2079: 			    "233861E359B48220F7C4693C9AE12B0A6F67809F0876E2D013800D6C"
 2080: 			    "41BB59B6D5979B5C00A172B4A2A5903A0BDCAF8A709585EB2AFAFA8F"
 2081: 			    "3499B200210DCC1F10EB33943CD67FC88A2F39A4BE5BEC4EC0A3212D"
 2082: 			    "C346D7E474B29EDE8A469FFECA686E5A"),
 2083:     UserPassHash = crypto:sha([Salt, crypto:sha([Username, <<$:>>, Password])]),
 2084:     Verifier = crypto:mod_pow(Generator, UserPassHash, Prime), 
 2085: 
 2086:     {ClientPublic, ClientPrivate} = crypto:generate_key(srp, {user, [Generator, Prime, Version]}, ClientPrivate),
 2087:     {ServerPublic, ServerPrivate} = crypto:generate_key(srp, {host, [Verifier, Generator, Prime, Version]}, ServerPrivate),
 2088: 
 2089:     SessionKey = crypto:compute_key(srp, ServerPublic, {ClientPublic, ClientPrivate},
 2090: 				    {user, [UserPassHash, Prime, Generator, Version, Scrambler]}),
 2091:     SessionKey = crypto:compute_key(srp, ClientPublic, {ServerPublic, ServerPrivate},
 2092: 				    {host, [Verifier, Prime, Version, Scrambler]}).
 2093:     
 2094: %%
 2095: %%
 2096: exor_test(doc) ->
 2097:     ["Test the exor function."];
 2098: exor_test(suite) ->
 2099:     [];
 2100: exor_test(Config) when is_list(Config) ->
 2101:     B = <<1, 2, 3, 4, 5, 6, 7, 8, 9, 10>>,
 2102:     Z1 = zero_bin(B),
 2103:     Z1 = crypto:exor(B, B),
 2104:     B1 = crypto:rand_bytes(100),
 2105:     B2 = crypto:rand_bytes(100),
 2106:     Z2 = zero_bin(B1),
 2107:     Z2 = crypto:exor(B1, B1),
 2108:     Z2 = crypto:exor(B2, B2),
 2109:     R = xor_bytes(B1, B2),
 2110:     R = crypto:exor(B1, B2),
 2111:     ok.
 2112: 
 2113: %%
 2114: %%
 2115: rc4_test(doc) ->
 2116:     ["Test rc4 encryption ."];
 2117: rc4_test(suite) ->
 2118:     [];
 2119: rc4_test(Config) when is_list(Config) ->
 2120:     CT1 = <<"Yo baby yo">>,
 2121:     R1 = <<118,122,68,110,157,166,141,212,139,39>>,
 2122:     K = "apaapa",
 2123:     R1 = crypto:rc4_encrypt(K, CT1),
 2124:     CT1 = crypto:rc4_encrypt(K, R1),
 2125:     CT2 = lists:seq(0, 255),
 2126:     R2 = crypto:rc4_encrypt(K, CT2),
 2127:     CT2 = binary_to_list(crypto:rc4_encrypt(K, R2)),
 2128:     ok.
 2129: 
 2130: rc4_stream_test(doc) ->
 2131:     ["Test rc4 stream encryption ."];
 2132: rc4_stream_test(suite) ->
 2133:     [];
 2134: rc4_stream_test(Config) when is_list(Config) ->
 2135:     CT1 = <<"Yo ">>,
 2136:     CT2 = <<"baby yo">>,
 2137:     K = "apaapa",
 2138:     State0 = crypto:rc4_set_key(K),
 2139:     {State1, R1} = crypto:rc4_encrypt_with_state(State0, CT1),
 2140:     {_State2, R2} = crypto:rc4_encrypt_with_state(State1, CT2),
 2141:     R = list_to_binary([R1, R2]),
 2142:     <<118,122,68,110,157,166,141,212,139,39>> = R,
 2143:     ok.
 2144: 
 2145: blowfish_cfb64(doc) -> ["Test Blowfish encrypt/decrypt."];
 2146: blowfish_cfb64(suite) -> [];
 2147: blowfish_cfb64(Config) when is_list(Config) ->   			
 2148:     Key = <<1,35,69,103,137,171,205,239,240,225,210,195,180,165,150,135>>,
 2149: 
 2150:     IVec = <<254,220,186,152,118,84,50,16>>,
 2151:     Plain = <<"7654321 Now is the time for ">>,
 2152:     Enc = <<231,50,20,162,130,33,57,202,242,110,207,109,46,185,231,110,61,163,222,4,209,81,114,0,81,157,87,166>>,
 2153: 
 2154:     Enc = crypto:blowfish_cfb64_encrypt(Key, IVec, Plain),
 2155:     Plain = crypto:blowfish_cfb64_decrypt(Key, IVec, Enc),
 2156: 
 2157:     Key2 = <<"A2B4C">>,
 2158:     IVec2 = <<"12345678">>,
 2159:     Plain2 = <<"badger at my table....!">>,
 2160:     Enc2 = <<173,76,128,155,70,81,79,228,4,162,188,92,119,53,144,89,93,236,28,164,176,16,138>>,
 2161: 
 2162:     Enc2 = crypto:blowfish_cfb64_encrypt(Key2, IVec2, Plain2),
 2163:     Plain2 = crypto:blowfish_cfb64_decrypt(Key2, IVec2, Enc2).
 2164: 
 2165: 
 2166: smp(doc) -> "Check concurrent access to crypto driver";
 2167: smp(suite) -> [];
 2168: smp(Config) ->
 2169:     case erlang:system_info(smp_support) of
 2170: 	true ->
 2171: 	    NumOfProcs = erlang:system_info(schedulers),
 2172: 	    io:format("smp starting ~p workers\n",[NumOfProcs]),
 2173: 	    Seeds = [random:uniform(9999) || _ <- lists:seq(1,NumOfProcs)],
 2174: 	    Parent = self(),
 2175: 	    Pids = [spawn_link(fun()-> worker(Seed,Config,Parent) end)
 2176: 		    || Seed <- Seeds],
 2177: 	    wait_pids(Pids);
 2178: 
 2179: 	false ->
 2180: 	    {skipped,"No smp support"}
 2181:     end.
 2182: 	    
 2183: worker(Seed, Config, Parent) ->
 2184:     io:format("smp worker ~p, seed=~p~n",[self(),Seed]),
 2185:     random:seed(Seed,Seed,Seed),
 2186:     worker_loop(100, Config),
 2187:     %%io:format("worker ~p done\n",[self()]),
 2188:     Parent ! self().
 2189: 
 2190: worker_loop(0, _) ->
 2191:     ok;
 2192: worker_loop(N, Config) ->
 2193:     Funcs = { md5, md5_update, md5_mac, md5_mac_io, sha, sha_update, des_cbc,
 2194: 	      aes_cfb, aes_cbc, des_cbc_iter, rand_uniform_test, strong_rand_test,
 2195: 	      rsa_verify_test, exor_test, rc4_test, rc4_stream_test, mod_exp_test,
 2196:               hmac_update_md5, hmac_update_sha, hmac_update_sha256, hmac_update_sha512,
 2197: 	      hmac_rfc2202, hmac_rfc4231_sha224, hmac_rfc4231_sha256, hmac_rfc4231_sha384,
 2198: 	      hmac_rfc4231_sha512, aes_ctr_stream },
 2199: 
 2200:     F = element(random:uniform(size(Funcs)),Funcs),
 2201:     %%io:format("worker ~p calling ~p\n",[self(),F]),
 2202:     ?MODULE:F(Config),
 2203:     worker_loop(N-1,Config).
 2204:     
 2205: wait_pids([]) -> 
 2206:     ok;
 2207: wait_pids(Pids) ->
 2208:     receive
 2209: 	Pid ->
 2210: 	    ?line true = lists:member(Pid,Pids),
 2211: 	    Others = lists:delete(Pid,Pids),
 2212: 	    io:format("wait_pid got ~p, still waiting for ~p\n",[Pid,Others]),
 2213: 	    wait_pids(Others)
 2214:     end.
 2215: 
 2216: %%
 2217: %% Help functions
 2218: %%
 2219: 
 2220: % match
 2221: m(X, X) ->
 2222:     ?line true.
 2223: t(true) ->
 2224:     true.
 2225: 
 2226: % hexstr2bin
 2227: hexstr2bin(S) ->
 2228:     list_to_binary(hexstr2list(S)).
 2229: 
 2230: hexstr2list([X,Y|T]) ->
 2231:     [mkint(X)*16 + mkint(Y) | hexstr2list(T)];
 2232: hexstr2list([]) ->
 2233:     [].
 2234: 
 2235: mkint(C) when $0 =< C, C =< $9 ->
 2236:     C - $0;
 2237: mkint(C) when $A =< C, C =< $F ->
 2238:     C - $A + 10;
 2239: mkint(C) when $a =< C, C =< $f ->
 2240:     C - $a + 10.
 2241: 
 2242: %% mod_exp in erlang (copied from jungerl's ssh_math.erl)
 2243: ipow(A, B, M) when M > 0, B >= 0 ->
 2244:     if A == 1 -> 
 2245:  	    1;
 2246:        true -> 
 2247:  	    ipow(A, B, M, 1)
 2248:     end.
 2249: 
 2250: ipow(A, 1, M, Prod) ->
 2251:     (A*Prod) rem M;
 2252: ipow(_A, 0, _M, Prod) ->
 2253:     Prod;
 2254: ipow(A, B, M, Prod)  ->
 2255:     B1 = B bsr 1,
 2256:     A1 = (A*A) rem M,
 2257:     if B - B1 == B1 ->
 2258: 	    ipow(A1, B1, M, Prod);
 2259:        true ->
 2260: 	    ipow(A1, B1, M, (A*Prod) rem M)
 2261:     end.
 2262: 
 2263: %%
 2264: %% Invert an element X mod P
 2265: %% Calculated as {1, {A,B}} = egcd(X,P),
 2266: %%   1 == P*A + X*B == X*B (mod P) i.e B is the inverse element
 2267: %%
 2268: %% X > 0, P > 0, X < P   (P should be prime)
 2269: %%
 2270: %% invert(X,P) when X > 0, P > 0, X < P ->
 2271: %%     I = inv(X,P,1,0),
 2272: %%     if 
 2273: %%         I < 0 -> P + I;
 2274: %%         true -> I
 2275: %%     end.
 2276: 
 2277: %% inv(0,_,_,Q) -> Q;
 2278: %% inv(X,P,R1,Q1) ->
 2279: %%     D = P div X,
 2280: %%     inv(P rem X, X, Q1 - D*R1, R1).    
 2281: 
 2282: sized_binary(Binary) when is_binary(Binary) ->
 2283:     <<(size(Binary)):32/integer, Binary/binary>>;
 2284: sized_binary(List) ->
 2285:     sized_binary(list_to_binary(List)).
 2286: 
 2287: unsized_binary(<<Sz:32/integer, Binary:Sz/binary>>) ->
 2288:     Binary.
 2289: 
 2290: xor_bytes(Bin1, Bin2) when is_binary(Bin1), is_binary(Bin2) ->
 2291:     L1 = binary_to_list(Bin1),
 2292:     L2 = binary_to_list(Bin2),
 2293:     list_to_binary(xor_bytes(L1, L2));
 2294: xor_bytes(L1, L2) ->
 2295:     xor_bytes(L1, L2, []).
 2296: 
 2297: xor_bytes([], [], Acc) ->
 2298:     lists:reverse(Acc);
 2299: xor_bytes([N1 | Tl1], [N2 | Tl2], Acc) ->
 2300:     xor_bytes(Tl1, Tl2, [N1 bxor N2 | Acc]).
 2301: 
 2302: zero_bin(N) when is_integer(N) ->
 2303:     N8 = N * 8,
 2304:     <<0:N8/integer>>;
 2305: zero_bin(B) when is_binary(B) ->
 2306:     zero_bin(size(B)).
 2307: 
 2308: my_dss_verify(Data,[Sign|Tail],Key) ->
 2309:     Res = my_dss_verify(Data,sized_binary(Sign),Key),
 2310:     case Tail of
 2311:         [] ->  Res;
 2312:         _ -> ?line Res = my_dss_verify(Data,Tail,Key)
 2313:     end;       
 2314: my_dss_verify(Data,Sign,Key) ->
 2315:     ?line Res = crypto:dss_verify(Data, Sign, Key),
 2316:     ?line Res = crypto:dss_verify(sha, Data, Sign, Key),
 2317:     ?line <<_:32,Raw/binary>> = Data,
 2318:     ?line Res = crypto:dss_verify(none, crypto:sha(Raw), Sign, Key),
 2319:     Res.
 2320: 
 2321: my_dss_sign(Data,Key) ->
 2322:     ?line S1 = crypto:dss_sign(Data, Key),
 2323:     ?line S2 = crypto:dss_sign(sha, Data, Key),
 2324:     ?line <<_:32,Raw/binary>> = Data,
 2325:     ?line S3 = crypto:dss_sign(none, crypto:sha(Raw), Key),
 2326:     [S1,S2,S3].
 2327: 
 2328: openssl_version() ->
 2329:     case crypto:info_lib() of
 2330: 	[{<<"OpenSSL">>,LibVer,_}] when is_integer(LibVer) ->
 2331: 	    LibVer;
 2332: 	_ ->
 2333: 	    undefined
 2334:     end.
 2335: 
 2336: if_supported(Algorithm, Fun) ->
 2337:     case lists:member(Algorithm, lists:append([Algo ||  {_, Algo}  <- crypto:supports()])) of
 2338: 	true ->
 2339: 	    Fun();
 2340: 	_ ->
 2341: 	    {skipped, io:format("~s not spupported", [Algorithm])}
 2342:     end.