From 918546531fafb9a20170fc1ba1a2c876a46b3cc1 Mon Sep 17 00:00:00 2001 From: Daniel White Date: Thu, 27 Sep 2012 00:11:00 +1000 Subject: crypto: Document all types currently available for hmac_init --- lib/crypto/doc/src/crypto.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/crypto/doc/src/crypto.xml b/lib/crypto/doc/src/crypto.xml index 3e533158c8..0f21441748 100644 --- a/lib/crypto/doc/src/crypto.xml +++ b/lib/crypto/doc/src/crypto.xml @@ -346,7 +346,7 @@ Mpint() = >]]> hmac_init(Type, Key) -> Context - Type = sha | md5 | ripemd160 + Type = md5 | ripemd160 | sha | sha224 | sha256 | sha384 | sha512 Key = iolist() | binary() Context = binary() -- cgit v1.2.3 From ffef856e205bcaf42a1a9a8cc5fb4965fd459edc Mon Sep 17 00:00:00 2001 From: Daniel White Date: Thu, 27 Sep 2012 02:40:11 +1000 Subject: crypto: Provide a generic interface for HMAC generation --- lib/crypto/doc/src/crypto.xml | 17 +++++++++++++++++ lib/crypto/src/crypto.erl | 21 +++++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/crypto/doc/src/crypto.xml b/lib/crypto/doc/src/crypto.xml index 0f21441748..5e2a7acb97 100644 --- a/lib/crypto/doc/src/crypto.xml +++ b/lib/crypto/doc/src/crypto.xml @@ -342,6 +342,23 @@ Mpint() = >]]> is 96 bits (12 bytes).

+ + hmac(Type, Key, Data) -> Mac + hmac(Type, Key, Data, MacLength) -> Mac + + + Type = md5 | sha | sha224 | sha256 | sha384 | sha512 + Key = iodata() + Data = iodata() + MacLength = integer() + Mac = binary() + + +

Computes a HMAC of type Type from Data using + Key as the authentication key.

MacLength + will limit the size of the resultant Mac. +
+
hmac_init(Type, Key) -> Context diff --git a/lib/crypto/src/crypto.erl b/lib/crypto/src/crypto.erl index 461558a79e..5e7f1a948a 100644 --- a/lib/crypto/src/crypto.erl +++ b/lib/crypto/src/crypto.erl @@ -35,7 +35,7 @@ -export([sha256_mac/2, sha256_mac/3]). -export([sha384_mac/2, sha384_mac/3]). -export([sha512_mac/2, sha512_mac/3]). --export([hmac_init/2, hmac_update/2, hmac_final/1, hmac_final_n/2]). +-export([hmac/3, hmac/4, hmac_init/2, hmac_update/2, hmac_final/1, hmac_final_n/2]). -export([des_cbc_encrypt/3, des_cbc_decrypt/3, des_cbc_ivec/1]). -export([des_ecb_encrypt/2, des_ecb_decrypt/2]). -export([des_cfb_encrypt/3, des_cfb_decrypt/3, des_cfb_ivec/2]). @@ -107,7 +107,7 @@ blowfish_ecb_encrypt, blowfish_ecb_decrypt, blowfish_ofb64_encrypt, des_cbc_ivec, des_cfb_ivec, erlint, mpint, hash, hash_init, hash_update, hash_final, - hmac_init, hmac_update, hmac_final, hmac_final_n, info, + hmac, hmac_init, hmac_update, hmac_final, hmac_final_n, info, rc2_cbc_encrypt, rc2_cbc_decrypt, info_lib]). @@ -418,11 +418,28 @@ sha512_final_nif(_Context) -> ?nif_stub. %% %% HMAC (multiple hash options) %% + +-spec hmac(_, iodata(), iodata()) -> binary(). +-spec hmac(_, iodata(), iodata(), integer()) -> binary(). -spec hmac_init(atom(), iodata()) -> binary(). -spec hmac_update(binary(), iodata()) -> binary(). -spec hmac_final(binary()) -> binary(). -spec hmac_final_n(binary(), integer()) -> binary(). +hmac(md5, Key, Data) -> md5_mac(Key, Data); +hmac(sha, Key, Data) -> sha_mac(Key, Data); +hmac(sha224, Key, Data) -> sha224_mac(Key, Data); +hmac(sha256, Key, Data) -> sha256_mac(Key, Data); +hmac(sha384, Key, Data) -> sha384_mac(Key, Data); +hmac(sha512, Key, Data) -> sha512_mac(Key, Data). + +hmac(md5, Key, Data, Size) -> md5_mac_n(Key, Data, Size); +hmac(sha, Key, Data, Size) -> sha_mac(Key, Data, Size); +hmac(sha224, Key, Data, Size) -> sha224_mac(Key, Data, Size); +hmac(sha256, Key, Data, Size) -> sha256_mac(Key, Data, Size); +hmac(sha384, Key, Data, Size) -> sha384_mac(Key, Data, Size); +hmac(sha512, Key, Data, Size) -> sha512_mac(Key, Data, Size). + hmac_init(_Type, _Key) -> ?nif_stub. hmac_update(_Context, _Data) -> ? nif_stub. hmac_final(_Context) -> ? nif_stub. -- cgit v1.2.3 From a2c2716caafdc1f479ea4a20e5844ccc90d87c14 Mon Sep 17 00:00:00 2001 From: Daniel White Date: Sat, 29 Sep 2012 18:10:10 +1000 Subject: crypto: Add RFC-2202 test vectors for HMAC-MD5 and HMAC-SHA1 --- lib/crypto/test/crypto_SUITE.erl | 166 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 164 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/crypto/test/crypto_SUITE.erl b/lib/crypto/test/crypto_SUITE.erl index 8965ab6b94..9cdbd52e24 100644 --- a/lib/crypto/test/crypto_SUITE.erl +++ b/lib/crypto/test/crypto_SUITE.erl @@ -38,6 +38,7 @@ hmac_update_md5/1, hmac_update_md5_io/1, hmac_update_md5_n/1, + hmac_rfc2202/1, hmac_rfc4231/1, sha256/1, sha256_update/1, @@ -90,7 +91,7 @@ groups() -> sha256, sha256_update, sha512, sha512_update, hmac_update_sha, hmac_update_sha_n, hmac_update_sha256, hmac_update_sha512, hmac_update_md5_n, hmac_update_md5_io, hmac_update_md5, - hmac_rfc4231, + hmac_rfc2202, hmac_rfc4231, des_cbc, aes_cfb, aes_cbc, des_cfb, des_cfb_iter, des3_cbc, des3_cfb, rc2_cbc, aes_cbc_iter, aes_ctr, aes_ctr_stream, des_cbc_iter, des_ecb, @@ -418,6 +419,167 @@ hmac_update_md5(Config) when is_list(Config) -> ?line Exp2 = crypto:md5_mac(Key2, lists:flatten([Long1, Long2, Long3])), ?line m(Exp2, Mac2). +hmac_rfc2202(doc) -> + ["Generate an HMAC using hmac, md5_mac, and sha_mac." + "Test vectors are taken from RFC-2202."]; +hmac_rfc2202(suite) -> + []; +hmac_rfc2202(Config) when is_list(Config) -> + hmac_rfc2202_md5(), + hmac_rfc2202_sha(). + +hmac_rfc2202_md5() -> + %% Test case 1 + Case1Key = binary:copy(<<16#0b>>, 16), + Case1Data = <<"Hi There">>, + Case1Exp = hexstr2bin("9294727a3638bb1c13f48ef8158bfc9d"), + + ?line Case1Mac_1 = crypto:md5_mac(Case1Key, Case1Data), + ?line Case1Mac_2 = crypto:hmac(md5, Case1Key, Case1Data), + ?line m(Case1Exp, Case1Mac_1), + ?line m(Case1Exp, Case1Mac_2), + + %% Test case 2 + Case2Key = <<"Jefe">>, + Case2Data = <<"what do ya want for nothing?">>, + Case2Exp = hexstr2bin("750c783e6ab0b503eaa86e310a5db738"), + + ?line Case2Mac_1 = crypto:md5_mac(Case2Key, Case2Data), + ?line Case2Mac_2 = crypto:hmac(md5, Case2Key, Case2Data), + ?line m(Case2Exp, Case2Mac_1), + ?line m(Case2Exp, Case2Mac_2), + + %% Test case 3 + Case3Key = binary:copy(<<16#aa>>, 16), + Case3Data = binary:copy(<<16#dd>>, 50), + Case3Exp = hexstr2bin("56be34521d144c88dbb8c733f0e8b3f6"), + + ?line Case3Mac_1 = crypto:md5_mac(Case3Key, Case3Data), + ?line Case3Mac_2 = crypto:hmac(md5, Case3Key, Case3Data), + ?line m(Case3Exp, Case3Mac_1), + ?line m(Case3Exp, Case3Mac_2), + + %% Test case 4 + Case4Key = list_to_binary(lists:seq(1, 16#19)), + Case4Data = binary:copy(<<16#cd>>, 50), + Case4Exp = hexstr2bin("697eaf0aca3a3aea3a75164746ffaa79"), + + ?line Case4Mac_1 = crypto:md5_mac(Case4Key, Case4Data), + ?line Case4Mac_2 = crypto:hmac(md5, Case4Key, Case4Data), + ?line m(Case4Exp, Case4Mac_1), + ?line m(Case4Exp, Case4Mac_2), + + %% Test case 5 + Case5Key = binary:copy(<<16#0c>>, 16), + Case5Data = "Test With Truncation", + Case5Exp = hexstr2bin("56461ef2342edc00f9bab995690efd4c"), + Case5Exp96 = hexstr2bin("56461ef2342edc00f9bab995"), + + ?line Case5Mac_1 = crypto:md5_mac(Case5Key, Case5Data), + ?line Case5Mac_2 = crypto:hmac(md5, Case5Key, Case5Data), + ?line Case5Mac96_1 = crypto:md5_mac_96(Case5Key, Case5Data), + ?line Case5Mac96_2 = crypto:hmac(md5, Case5Key, Case5Data, 12), + ?line m(Case5Exp, Case5Mac_1), + ?line m(Case5Exp, Case5Mac_2), + ?line m(Case5Exp96, Case5Mac96_1), + ?line m(Case5Exp96, Case5Mac96_2), + + %% Test case 6 + Case6Key = binary:copy(<<16#aa>>, 80), + Case6Data = <<"Test Using Larger Than Block-Size Key - Hash Key First">>, + Case6Exp = hexstr2bin("6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd"), + + ?line Case6Mac_1 = crypto:md5_mac(Case6Key, Case6Data), + ?line Case6Mac_2 = crypto:hmac(md5, Case6Key, Case6Data), + ?line m(Case6Exp, Case6Mac_1), + ?line m(Case6Exp, Case6Mac_2), + + %% Test case 7 + Case7Key = binary:copy(<<16#aa>>, 80), + Case7Data = <<"Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data">>, + Case7Exp = hexstr2bin("6f630fad67cda0ee1fb1f562db3aa53e"), + + ?line Case7Mac_1 = crypto:md5_mac(Case7Key, Case7Data), + ?line Case7Mac_2 = crypto:hmac(md5, Case7Key, Case7Data), + ?line m(Case7Exp, Case7Mac_1), + ?line m(Case7Exp, Case7Mac_2). + +hmac_rfc2202_sha() -> + %% Test case 1 + Case1Key = binary:copy(<<16#0b>>, 20), + Case1Data = <<"Hi There">>, + Case1Exp = hexstr2bin("b617318655057264e28bc0b6fb378c8ef146be00"), + + ?line Case1Mac_1 = crypto:sha_mac(Case1Key, Case1Data), + ?line Case1Mac_2 = crypto:hmac(sha, Case1Key, Case1Data), + ?line m(Case1Exp, Case1Mac_1), + ?line m(Case1Exp, Case1Mac_2), + + %% Test case 2 + Case2Key = <<"Jefe">>, + Case2Data = <<"what do ya want for nothing?">>, + Case2Exp = hexstr2bin("effcdf6ae5eb2fa2d27416d5f184df9c259a7c79"), + + ?line Case2Mac_1 = crypto:sha_mac(Case2Key, Case2Data), + ?line Case2Mac_2 = crypto:hmac(sha, Case2Key, Case2Data), + ?line m(Case2Exp, Case2Mac_1), + ?line m(Case2Exp, Case2Mac_2), + + %% Test case 3 + Case3Key = binary:copy(<<16#aa>>, 20), + Case3Data = binary:copy(<<16#dd>>, 50), + Case3Exp = hexstr2bin("125d7342b9ac11cd91a39af48aa17b4f63f175d3"), + + ?line Case3Mac_1 = crypto:sha_mac(Case3Key, Case3Data), + ?line Case3Mac_2 = crypto:hmac(sha, Case3Key, Case3Data), + ?line m(Case3Exp, Case3Mac_1), + ?line m(Case3Exp, Case3Mac_2), + + %% Test case 4 + Case4Key = list_to_binary(lists:seq(1, 16#19)), + Case4Data = binary:copy(<<16#cd>>, 50), + Case4Exp = hexstr2bin("4c9007f4026250c6bc8414f9bf50c86c2d7235da"), + + ?line Case4Mac_1 = crypto:sha_mac(Case4Key, Case4Data), + ?line Case4Mac_2 = crypto:hmac(sha, Case4Key, Case4Data), + ?line m(Case4Exp, Case4Mac_1), + ?line m(Case4Exp, Case4Mac_2), + + %% Test case 5 + Case5Key = binary:copy(<<16#0c>>, 20), + Case5Data = "Test With Truncation", + Case5Exp = hexstr2bin("4c1a03424b55e07fe7f27be1d58bb9324a9a5a04"), + Case5Exp96 = hexstr2bin("4c1a03424b55e07fe7f27be1"), + + ?line Case5Mac_1 = crypto:sha_mac(Case5Key, Case5Data), + ?line Case5Mac_2 = crypto:hmac(sha, Case5Key, Case5Data), + ?line Case5Mac96_1 = crypto:sha_mac_96(Case5Key, Case5Data), + ?line Case5Mac96_2 = crypto:hmac(sha, Case5Key, Case5Data, 12), + ?line m(Case5Exp, Case5Mac_1), + ?line m(Case5Exp, Case5Mac_2), + ?line m(Case5Exp96, Case5Mac96_1), + ?line m(Case5Exp96, Case5Mac96_2), + + %% Test case 6 + Case6Key = binary:copy(<<16#aa>>, 80), + Case6Data = <<"Test Using Larger Than Block-Size Key - Hash Key First">>, + Case6Exp = hexstr2bin("aa4ae5e15272d00e95705637ce8a3b55ed402112"), + + ?line Case6Mac_1 = crypto:sha_mac(Case6Key, Case6Data), + ?line Case6Mac_2 = crypto:hmac(sha, Case6Key, Case6Data), + ?line m(Case6Exp, Case6Mac_1), + ?line m(Case6Exp, Case6Mac_2), + + %% Test case 7 + Case7Key = binary:copy(<<16#aa>>, 80), + Case7Data = <<"Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data">>, + Case7Exp = hexstr2bin("e8e99d0f45237d786d6bbaa7965c7808bbff1a91"), + + ?line Case7Mac_1 = crypto:sha_mac(Case7Key, Case7Data), + ?line Case7Mac_2 = crypto:hmac(sha, Case7Key, Case7Data), + ?line m(Case7Exp, Case7Mac_1), + ?line m(Case7Exp, Case7Mac_2). + hmac_rfc4231(doc) -> ["Generate an HMAC using crypto:shaXXX_mac and hmac_init, hmac_update, and hmac_final. " "Testvectors are take from RFC4231." ]; @@ -1838,7 +2000,7 @@ worker_loop(N, Config) -> aes_cfb, aes_cbc, des_cbc_iter, rand_uniform_test, strong_rand_test, rsa_verify_test, exor_test, rc4_test, rc4_stream_test, mod_exp_test, hmac_update_md5, hmac_update_sha, hmac_update_sha256, hmac_update_sha512, - hmac_rfc4231, + hmac_rfc2202, hmac_rfc4231, aes_ctr_stream }, F = element(random:uniform(size(Funcs)),Funcs), -- cgit v1.2.3 From f6a299fc45ad381f3d28c160f2d084d6797a2cec Mon Sep 17 00:00:00 2001 From: Daniel White Date: Sat, 29 Sep 2012 18:11:39 +1000 Subject: crypto: Add test cases for the generic hmac interface in the RFC-4231 tests --- lib/crypto/test/crypto_SUITE.erl | 52 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/crypto/test/crypto_SUITE.erl b/lib/crypto/test/crypto_SUITE.erl index 9cdbd52e24..a3d3bb1ede 100644 --- a/lib/crypto/test/crypto_SUITE.erl +++ b/lib/crypto/test/crypto_SUITE.erl @@ -581,7 +581,7 @@ hmac_rfc2202_sha() -> ?line m(Case7Exp, Case7Mac_2). hmac_rfc4231(doc) -> - ["Generate an HMAC using crypto:shaXXX_mac and hmac_init, hmac_update, and hmac_final. " + ["Generate an HMAC using crypto:shaXXX_mac, hmac, and hmac_init, hmac_update, and hmac_final. " "Testvectors are take from RFC4231." ]; hmac_rfc4231(suite) -> []; @@ -608,29 +608,37 @@ hmac_rfc4231_do() -> ?line Case1Ctx224_2 = crypto:hmac_update(Case1Ctx224, Case1Data), ?line Case1Mac224_1 = crypto:hmac_final(Case1Ctx224_2), ?line Case1Mac224_2 = crypto:sha224_mac(Case1Key, Case1Data), + ?line Case1Mac224_3 = crypto:hmac(sha224, Case1Key, Case1Data), ?line m(Case1Exp224, Case1Mac224_1), ?line m(Case1Exp224, Case1Mac224_2), + ?line m(Case1Exp224, Case1Mac224_3), ?line Case1Ctx256 = crypto:hmac_init(sha256, Case1Key), ?line Case1Ctx256_2 = crypto:hmac_update(Case1Ctx256, Case1Data), ?line Case1Mac256_1 = crypto:hmac_final(Case1Ctx256_2), ?line Case1Mac256_2 = crypto:sha256_mac(Case1Key, Case1Data), + ?line Case1Mac256_3 = crypto:hmac(sha256, Case1Key, Case1Data), ?line m(Case1Exp256, Case1Mac256_1), ?line m(Case1Exp256, Case1Mac256_2), + ?line m(Case1Exp256, Case1Mac256_3), ?line Case1Ctx384 = crypto:hmac_init(sha384, Case1Key), ?line Case1Ctx384_2 = crypto:hmac_update(Case1Ctx384, Case1Data), ?line Case1Mac384_1 = crypto:hmac_final(Case1Ctx384_2), ?line Case1Mac384_2 = crypto:sha384_mac(Case1Key, Case1Data), + ?line Case1Mac384_3 = crypto:hmac(sha384, Case1Key, Case1Data), ?line m(Case1Exp384, Case1Mac384_1), ?line m(Case1Exp384, Case1Mac384_2), + ?line m(Case1Exp384, Case1Mac384_3), ?line Case1Ctx512 = crypto:hmac_init(sha512, Case1Key), ?line Case1Ctx512_2 = crypto:hmac_update(Case1Ctx512, Case1Data), ?line Case1Mac512_1 = crypto:hmac_final(Case1Ctx512_2), ?line Case1Mac512_2 = crypto:sha512_mac(Case1Key, Case1Data), + ?line Case1Mac512_3 = crypto:hmac(sha512, Case1Key, Case1Data), ?line m(Case1Exp512, Case1Mac512_1), ?line m(Case1Exp512, Case1Mac512_2), + ?line m(Case1Exp512, Case1Mac512_3), %% Test Case 2 Case2Key = <<"Jefe">>, @@ -651,29 +659,37 @@ hmac_rfc4231_do() -> ?line Case2Ctx224_2 = crypto:hmac_update(Case2Ctx224, Case2Data), ?line Case2Mac224_1 = crypto:hmac_final(Case2Ctx224_2), ?line Case2Mac224_2 = crypto:sha224_mac(Case2Key, Case2Data), + ?line Case2Mac224_3 = crypto:hmac(sha224, Case2Key, Case2Data), ?line m(Case2Exp224, Case2Mac224_1), ?line m(Case2Exp224, Case2Mac224_2), + ?line m(Case2Exp224, Case2Mac224_3), ?line Case2Ctx256 = crypto:hmac_init(sha256, Case2Key), ?line Case2Ctx256_2 = crypto:hmac_update(Case2Ctx256, Case2Data), ?line Case2Mac256_1 = crypto:hmac_final(Case2Ctx256_2), ?line Case2Mac256_2 = crypto:sha256_mac(Case2Key, Case2Data), + ?line Case2Mac256_3 = crypto:hmac(sha256, Case2Key, Case2Data), ?line m(Case2Exp256, Case2Mac256_1), ?line m(Case2Exp256, Case2Mac256_2), + ?line m(Case2Exp256, Case2Mac256_3), ?line Case2Ctx384 = crypto:hmac_init(sha384, Case2Key), ?line Case2Ctx384_2 = crypto:hmac_update(Case2Ctx384, Case2Data), ?line Case2Mac384_1 = crypto:hmac_final(Case2Ctx384_2), ?line Case2Mac384_2 = crypto:sha384_mac(Case2Key, Case2Data), + ?line Case2Mac384_3 = crypto:hmac(sha384, Case2Key, Case2Data), ?line m(Case2Exp384, Case2Mac384_1), ?line m(Case2Exp384, Case2Mac384_2), + ?line m(Case2Exp384, Case2Mac384_3), ?line Case2Ctx512 = crypto:hmac_init(sha512, Case2Key), ?line Case2Ctx512_2 = crypto:hmac_update(Case2Ctx512, Case2Data), ?line Case2Mac512_1 = crypto:hmac_final(Case2Ctx512_2), ?line Case2Mac512_2 = crypto:sha512_mac(Case2Key, Case2Data), + ?line Case2Mac512_3 = crypto:hmac(sha512, Case2Key, Case2Data), ?line m(Case2Exp512, Case2Mac512_1), ?line m(Case2Exp512, Case2Mac512_2), + ?line m(Case2Exp512, Case2Mac512_3), %% Test Case 3 Case3Key = binary:copy(<<16#aa>>, 20), @@ -694,29 +710,37 @@ hmac_rfc4231_do() -> ?line Case3Ctx224_2 = crypto:hmac_update(Case3Ctx224, Case3Data), ?line Case3Mac224_1 = crypto:hmac_final(Case3Ctx224_2), ?line Case3Mac224_2 = crypto:sha224_mac(Case3Key, Case3Data), + ?line Case3Mac224_3 = crypto:hmac(sha224, Case3Key, Case3Data), ?line m(Case3Exp224, Case3Mac224_1), ?line m(Case3Exp224, Case3Mac224_2), + ?line m(Case3Exp224, Case3Mac224_3), ?line Case3Ctx256 = crypto:hmac_init(sha256, Case3Key), ?line Case3Ctx256_2 = crypto:hmac_update(Case3Ctx256, Case3Data), ?line Case3Mac256_1 = crypto:hmac_final(Case3Ctx256_2), ?line Case3Mac256_2 = crypto:sha256_mac(Case3Key, Case3Data), + ?line Case3Mac256_3 = crypto:hmac(sha256, Case3Key, Case3Data), ?line m(Case3Exp256, Case3Mac256_1), ?line m(Case3Exp256, Case3Mac256_2), + ?line m(Case3Exp256, Case3Mac256_3), ?line Case3Ctx384 = crypto:hmac_init(sha384, Case3Key), ?line Case3Ctx384_2 = crypto:hmac_update(Case3Ctx384, Case3Data), ?line Case3Mac384_1 = crypto:hmac_final(Case3Ctx384_2), ?line Case3Mac384_2 = crypto:sha384_mac(Case3Key, Case3Data), + ?line Case3Mac384_3 = crypto:hmac(sha384, Case3Key, Case3Data), ?line m(Case3Exp384, Case3Mac384_1), ?line m(Case3Exp384, Case3Mac384_2), + ?line m(Case3Exp384, Case3Mac384_3), ?line Case3Ctx512 = crypto:hmac_init(sha512, Case3Key), ?line Case3Ctx512_2 = crypto:hmac_update(Case3Ctx512, Case3Data), ?line Case3Mac512_1 = crypto:hmac_final(Case3Ctx512_2), ?line Case3Mac512_2 = crypto:sha512_mac(Case3Key, Case3Data), + ?line Case3Mac512_3 = crypto:hmac(sha512, Case3Key, Case3Data), ?line m(Case3Exp512, Case3Mac512_1), ?line m(Case3Exp512, Case3Mac512_2), + ?line m(Case3Exp512, Case3Mac512_3), %% Test Case 4 Case4Key = list_to_binary(lists:seq(1, 16#19)), @@ -737,29 +761,37 @@ hmac_rfc4231_do() -> ?line Case4Ctx224_2 = crypto:hmac_update(Case4Ctx224, Case4Data), ?line Case4Mac224_1 = crypto:hmac_final(Case4Ctx224_2), ?line Case4Mac224_2 = crypto:sha224_mac(Case4Key, Case4Data), + ?line Case4Mac224_3 = crypto:hmac(sha224, Case4Key, Case4Data), ?line m(Case4Exp224, Case4Mac224_1), ?line m(Case4Exp224, Case4Mac224_2), + ?line m(Case4Exp224, Case4Mac224_3), ?line Case4Ctx256 = crypto:hmac_init(sha256, Case4Key), ?line Case4Ctx256_2 = crypto:hmac_update(Case4Ctx256, Case4Data), ?line Case4Mac256_1 = crypto:hmac_final(Case4Ctx256_2), ?line Case4Mac256_2 = crypto:sha256_mac(Case4Key, Case4Data), + ?line Case4Mac256_3 = crypto:hmac(sha256, Case4Key, Case4Data), ?line m(Case4Exp256, Case4Mac256_1), ?line m(Case4Exp256, Case4Mac256_2), + ?line m(Case4Exp256, Case4Mac256_3), ?line Case4Ctx384 = crypto:hmac_init(sha384, Case4Key), ?line Case4Ctx384_2 = crypto:hmac_update(Case4Ctx384, Case4Data), ?line Case4Mac384_1 = crypto:hmac_final(Case4Ctx384_2), ?line Case4Mac384_2 = crypto:sha384_mac(Case4Key, Case4Data), + ?line Case4Mac384_3 = crypto:hmac(sha384, Case4Key, Case4Data), ?line m(Case4Exp384, Case4Mac384_1), ?line m(Case4Exp384, Case4Mac384_2), + ?line m(Case4Exp384, Case4Mac384_3), ?line Case4Ctx512 = crypto:hmac_init(sha512, Case4Key), ?line Case4Ctx512_2 = crypto:hmac_update(Case4Ctx512, Case4Data), ?line Case4Mac512_1 = crypto:hmac_final(Case4Ctx512_2), ?line Case4Mac512_2 = crypto:sha512_mac(Case4Key, Case4Data), + ?line Case4Mac512_3 = crypto:hmac(sha512, Case4Key, Case4Data), ?line m(Case4Exp512, Case4Mac512_1), ?line m(Case4Exp512, Case4Mac512_2), + ?line m(Case4Exp512, Case4Mac512_3), %% Test Case 6 Case6Key = binary:copy(<<16#aa>>, 131), @@ -780,29 +812,37 @@ hmac_rfc4231_do() -> ?line Case6Ctx224_2 = crypto:hmac_update(Case6Ctx224, Case6Data), ?line Case6Mac224_1 = crypto:hmac_final(Case6Ctx224_2), ?line Case6Mac224_2 = crypto:sha224_mac(Case6Key, Case6Data), + ?line Case6Mac224_3 = crypto:hmac(sha224, Case6Key, Case6Data), ?line m(Case6Exp224, Case6Mac224_1), ?line m(Case6Exp224, Case6Mac224_2), + ?line m(Case6Exp224, Case6Mac224_3), ?line Case6Ctx256 = crypto:hmac_init(sha256, Case6Key), ?line Case6Ctx256_2 = crypto:hmac_update(Case6Ctx256, Case6Data), ?line Case6Mac256_1 = crypto:hmac_final(Case6Ctx256_2), ?line Case6Mac256_2 = crypto:sha256_mac(Case6Key, Case6Data), + ?line Case6Mac256_3 = crypto:hmac(sha256, Case6Key, Case6Data), ?line m(Case6Exp256, Case6Mac256_1), ?line m(Case6Exp256, Case6Mac256_2), + ?line m(Case6Exp256, Case6Mac256_3), ?line Case6Ctx384 = crypto:hmac_init(sha384, Case6Key), ?line Case6Ctx384_2 = crypto:hmac_update(Case6Ctx384, Case6Data), ?line Case6Mac384_1 = crypto:hmac_final(Case6Ctx384_2), ?line Case6Mac384_2 = crypto:sha384_mac(Case6Key, Case6Data), + ?line Case6Mac384_3 = crypto:hmac(sha384, Case6Key, Case6Data), ?line m(Case6Exp384, Case6Mac384_1), ?line m(Case6Exp384, Case6Mac384_2), + ?line m(Case6Exp384, Case6Mac384_3), ?line Case6Ctx512 = crypto:hmac_init(sha512, Case6Key), ?line Case6Ctx512_2 = crypto:hmac_update(Case6Ctx512, Case6Data), ?line Case6Mac512_1 = crypto:hmac_final(Case6Ctx512_2), ?line Case6Mac512_2 = crypto:sha512_mac(Case6Key, Case6Data), + ?line Case6Mac512_3 = crypto:hmac(sha512, Case6Key, Case6Data), ?line m(Case6Exp512, Case6Mac512_1), ?line m(Case6Exp512, Case6Mac512_2), + ?line m(Case6Exp512, Case6Mac512_3), %% Test Case 7 Case7Key = binary:copy(<<16#aa>>, 131), @@ -825,29 +865,37 @@ hmac_rfc4231_do() -> ?line Case7Ctx224_2 = crypto:hmac_update(Case7Ctx224, Case7Data), ?line Case7Mac224_1 = crypto:hmac_final(Case7Ctx224_2), ?line Case7Mac224_2 = crypto:sha224_mac(Case7Key, Case7Data), + ?line Case7Mac224_3 = crypto:hmac(sha224, Case7Key, Case7Data), ?line m(Case7Exp224, Case7Mac224_1), ?line m(Case7Exp224, Case7Mac224_2), + ?line m(Case7Exp224, Case7Mac224_3), ?line Case7Ctx256 = crypto:hmac_init(sha256, Case7Key), ?line Case7Ctx256_2 = crypto:hmac_update(Case7Ctx256, Case7Data), ?line Case7Mac256_1 = crypto:hmac_final(Case7Ctx256_2), ?line Case7Mac256_2 = crypto:sha256_mac(Case7Key, Case7Data), + ?line Case7Mac256_3 = crypto:hmac(sha256, Case7Key, Case7Data), ?line m(Case7Exp256, Case7Mac256_1), ?line m(Case7Exp256, Case7Mac256_2), + ?line m(Case7Exp256, Case7Mac256_3), ?line Case7Ctx384 = crypto:hmac_init(sha384, Case7Key), ?line Case7Ctx384_2 = crypto:hmac_update(Case7Ctx384, Case7Data), ?line Case7Mac384_1 = crypto:hmac_final(Case7Ctx384_2), ?line Case7Mac384_2 = crypto:sha384_mac(Case7Key, Case7Data), + ?line Case7Mac384_3 = crypto:hmac(sha384, Case7Key, Case7Data), ?line m(Case7Exp384, Case7Mac384_1), ?line m(Case7Exp384, Case7Mac384_2), + ?line m(Case7Exp384, Case7Mac384_3), ?line Case7Ctx512 = crypto:hmac_init(sha512, Case7Key), ?line Case7Ctx512_2 = crypto:hmac_update(Case7Ctx512, Case7Data), ?line Case7Mac512_1 = crypto:hmac_final(Case7Ctx512_2), ?line Case7Mac512_2 = crypto:sha512_mac(Case7Key, Case7Data), + ?line Case7Mac512_3 = crypto:hmac(sha512, Case7Key, Case7Data), ?line m(Case7Exp512, Case7Mac512_1), - ?line m(Case7Exp512, Case7Mac512_2). + ?line m(Case7Exp512, Case7Mac512_2), + ?line m(Case7Exp512, Case7Mac512_3). hmac_update_md5_io(doc) -> ["Generate an MD5 HMAC using hmac_init, hmac_update, and hmac_final. " -- cgit v1.2.3 From 1afbb47ebc0fbde5547146eb21e072c3bc14a034 Mon Sep 17 00:00:00 2001 From: Daniel White Date: Sat, 29 Sep 2012 18:30:36 +1000 Subject: crypto: Add RFC-4231 test vectors for output truncation --- lib/crypto/test/crypto_SUITE.erl | 44 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'lib') diff --git a/lib/crypto/test/crypto_SUITE.erl b/lib/crypto/test/crypto_SUITE.erl index a3d3bb1ede..027881957c 100644 --- a/lib/crypto/test/crypto_SUITE.erl +++ b/lib/crypto/test/crypto_SUITE.erl @@ -793,6 +793,50 @@ hmac_rfc4231_do() -> ?line m(Case4Exp512, Case4Mac512_2), ?line m(Case4Exp512, Case4Mac512_3), + %% Test Case 5 + Case5Key = binary:copy(<<16#0c>>, 20), + Case5Data = <<"Test With Truncation">>, + Case5Exp224 = hexstr2bin("0e2aea68a90c8d37c988bcdb9fca6fa8"), + Case5Exp256 = hexstr2bin("a3b6167473100ee06e0c796c2955552b"), + Case5Exp384 = hexstr2bin("3abf34c3503b2a23a46efc619baef897"), + Case5Exp512 = hexstr2bin("415fad6271580a531d4179bc891d87a6"), + + ?line Case5Ctx224 = crypto:hmac_init(sha224, Case5Key), + ?line Case5Ctx224_2 = crypto:hmac_update(Case5Ctx224, Case5Data), + ?line Case5Mac224_1 = crypto:hmac_final_n(Case5Ctx224_2, 16), + ?line Case5Mac224_2 = crypto:sha224_mac(Case5Key, Case5Data, 16), + ?line Case5Mac224_3 = crypto:hmac(sha224, Case5Key, Case5Data, 16), + ?line m(Case5Exp224, Case5Mac224_1), + ?line m(Case5Exp224, Case5Mac224_2), + ?line m(Case5Exp224, Case5Mac224_3), + + ?line Case5Ctx256 = crypto:hmac_init(sha256, Case5Key), + ?line Case5Ctx256_2 = crypto:hmac_update(Case5Ctx256, Case5Data), + ?line Case5Mac256_1 = crypto:hmac_final_n(Case5Ctx256_2, 16), + ?line Case5Mac256_2 = crypto:sha256_mac(Case5Key, Case5Data, 16), + ?line Case5Mac256_3 = crypto:hmac(sha256, Case5Key, Case5Data, 16), + ?line m(Case5Exp256, Case5Mac256_1), + ?line m(Case5Exp256, Case5Mac256_2), + ?line m(Case5Exp256, Case5Mac256_3), + + ?line Case5Ctx384 = crypto:hmac_init(sha384, Case5Key), + ?line Case5Ctx384_2 = crypto:hmac_update(Case5Ctx384, Case5Data), + ?line Case5Mac384_1 = crypto:hmac_final_n(Case5Ctx384_2, 16), + ?line Case5Mac384_2 = crypto:sha384_mac(Case5Key, Case5Data, 16), + ?line Case5Mac384_3 = crypto:hmac(sha384, Case5Key, Case5Data, 16), + ?line m(Case5Exp384, Case5Mac384_1), + ?line m(Case5Exp384, Case5Mac384_2), + ?line m(Case5Exp384, Case5Mac384_3), + + ?line Case5Ctx512 = crypto:hmac_init(sha512, Case5Key), + ?line Case5Ctx512_2 = crypto:hmac_update(Case5Ctx512, Case5Data), + ?line Case5Mac512_1 = crypto:hmac_final_n(Case5Ctx512_2, 16), + ?line Case5Mac512_2 = crypto:sha512_mac(Case5Key, Case5Data, 16), + ?line Case5Mac512_3 = crypto:hmac(sha512, Case5Key, Case5Data, 16), + ?line m(Case5Exp512, Case5Mac512_1), + ?line m(Case5Exp512, Case5Mac512_2), + ?line m(Case5Exp512, Case5Mac512_3), + %% Test Case 6 Case6Key = binary:copy(<<16#aa>>, 131), Case6Data = <<"Test Using Larger Than Block-Size Key - Hash Key First">>, -- cgit v1.2.3