diff options
Diffstat (limited to 'lib/crypto')
-rw-r--r-- | lib/crypto/c_src/crypto.c | 58 | ||||
-rwxr-xr-x[-rw-r--r--] | lib/crypto/doc/src/crypto.xml | 23 | ||||
-rw-r--r-- | lib/crypto/src/crypto.erl | 102 | ||||
-rw-r--r-- | lib/crypto/test/crypto_SUITE.erl | 294 |
4 files changed, 435 insertions, 42 deletions
diff --git a/lib/crypto/c_src/crypto.c b/lib/crypto/c_src/crypto.c index 72c9e5b8e8..e77e5fb8f0 100644 --- a/lib/crypto/c_src/crypto.c +++ b/lib/crypto/c_src/crypto.c @@ -44,6 +44,7 @@ #include <openssl/md5.h> #include <openssl/md4.h> #include <openssl/sha.h> +#include <openssl/ripemd.h> #include <openssl/bn.h> #include <openssl/objects.h> #include <openssl/rc4.h> @@ -139,6 +140,10 @@ static ERL_NIF_TERM md5(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); static ERL_NIF_TERM md5_init(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); static ERL_NIF_TERM md5_update(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); static ERL_NIF_TERM md5_final(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); +static ERL_NIF_TERM ripemd160(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); +static ERL_NIF_TERM ripemd160_init(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); +static ERL_NIF_TERM ripemd160_update(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); +static ERL_NIF_TERM ripemd160_final(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); static ERL_NIF_TERM sha(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); static ERL_NIF_TERM sha_init(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); static ERL_NIF_TERM sha_update(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); @@ -246,6 +251,10 @@ static ErlNifFunc nif_funcs[] = { {"md5_init", 0, md5_init}, {"md5_update", 2, md5_update}, {"md5_final", 1, md5_final}, + {"ripemd160", 1, ripemd160}, + {"ripemd160_init", 0, ripemd160_init}, + {"ripemd160_update", 2, ripemd160_update}, + {"ripemd160_final", 1, ripemd160_final}, {"sha", 1, sha}, {"sha_init", 0, sha_init}, {"sha_update", 2, sha_update}, @@ -326,6 +335,8 @@ ERL_NIF_INIT(crypto,nif_funcs,load,NULL,upgrade,unload) #define MD5_LEN_96 12 #define MD4_CTX_LEN (sizeof(MD4_CTX)) #define MD4_LEN 16 +#define RIPEMD160_CTX_LEN (sizeof(RIPEMD160_CTX)) +#define RIPEMD160_LEN 20 #define SHA_CTX_LEN (sizeof(SHA_CTX)) #define SHA_LEN 20 #define SHA_LEN_96 12 @@ -600,6 +611,53 @@ static ERL_NIF_TERM md5_final(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[ return ret; } +static ERL_NIF_TERM ripemd160(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) +{/* (Data) */ + ErlNifBinary ibin; + ERL_NIF_TERM ret; + + if (!enif_inspect_iolist_as_binary(env, argv[0], &ibin)) { + return enif_make_badarg(env); + } + RIPEMD160((unsigned char *) ibin.data, ibin.size, + enif_make_new_binary(env,RIPEMD160_LEN, &ret)); + return ret; +} +static ERL_NIF_TERM ripemd160_init(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) +{/* () */ + ERL_NIF_TERM ret; + RIPEMD160_Init((RIPEMD160_CTX *) enif_make_new_binary(env, RIPEMD160_CTX_LEN, &ret)); + return ret; +} +static ERL_NIF_TERM ripemd160_update(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) +{/* (Context, Data) */ + RIPEMD160_CTX* new_ctx; + ErlNifBinary ctx_bin, data_bin; + ERL_NIF_TERM ret; + if (!enif_inspect_binary(env, argv[0], &ctx_bin) + || ctx_bin.size != RIPEMD160_CTX_LEN + || !enif_inspect_iolist_as_binary(env, argv[1], &data_bin)) { + return enif_make_badarg(env); + } + new_ctx = (RIPEMD160_CTX*) enif_make_new_binary(env,RIPEMD160_CTX_LEN, &ret); + memcpy(new_ctx, ctx_bin.data, RIPEMD160_CTX_LEN); + RIPEMD160_Update(new_ctx, data_bin.data, data_bin.size); + return ret; +} +static ERL_NIF_TERM ripemd160_final(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) +{/* (Context) */ + ErlNifBinary ctx_bin; + RIPEMD160_CTX ctx_clone; + ERL_NIF_TERM ret; + if (!enif_inspect_binary(env, argv[0], &ctx_bin) || ctx_bin.size != RIPEMD160_CTX_LEN) { + return enif_make_badarg(env); + } + memcpy(&ctx_clone, ctx_bin.data, RIPEMD160_CTX_LEN); /* writable */ + RIPEMD160_Final(enif_make_new_binary(env, RIPEMD160_LEN, &ret), &ctx_clone); + return ret; +} + + static ERL_NIF_TERM sha(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {/* (Data) */ ErlNifBinary ibin; diff --git a/lib/crypto/doc/src/crypto.xml b/lib/crypto/doc/src/crypto.xml index 3e533158c8..14c77c873f 100644..100755 --- a/lib/crypto/doc/src/crypto.xml +++ b/lib/crypto/doc/src/crypto.xml @@ -265,7 +265,7 @@ Mpint() = <![CDATA[<<ByteLen:32/integer-big, Bytes:ByteLen/binary>>]]> <name>hash(Type, Data) -> Digest</name> <fsummary></fsummary> <type> - <v>Type = md4 | md5 | sha | sha224 | sha256 | sha384 | sha512</v> + <v>Type = md4 | md5 | ripemd160 | sha | sha224 | sha256 | sha384 | sha512</v> <v>Data = iodata()</v> <v>Digest = binary()</v> </type> @@ -279,7 +279,7 @@ Mpint() = <![CDATA[<<ByteLen:32/integer-big, Bytes:ByteLen/binary>>]]> <name>hash_init(Type) -> Context</name> <fsummary></fsummary> <type> - <v>Type = md4 | md5 | sha | sha224 | sha256 | sha384 | sha512</v> + <v>Type = md4 | md5 | ripemd160 | sha | sha224 | sha256 | sha384 | sha512</v> </type> <desc> <p>Initializes the context for streaming hash operations. <c>Type</c> determines @@ -343,10 +343,27 @@ Mpint() = <![CDATA[<<ByteLen:32/integer-big, Bytes:ByteLen/binary>>]]> </desc> </func> <func> + <name>hmac(Type, Key, Data) -> Mac</name> + <name>hmac(Type, Key, Data, MacLength) -> Mac</name> + <fsummary></fsummary> + <type> + <v>Type = md5 | sha | sha224 | sha256 | sha384 | sha512</v> + <v>Key = iodata()</v> + <v>Data = iodata()</v> + <v>MacLength = integer()</v> + <v>Mac = binary()</v> + </type> + <desc> + <p>Computes a HMAC of type <c>Type</c> from <c>Data</c> using + <c>Key</c> as the authentication key.</p> <c>MacLength</c> + will limit the size of the resultant <c>Mac</c>. + </desc> + </func> + <func> <name>hmac_init(Type, Key) -> Context</name> <fsummary></fsummary> <type> - <v>Type = sha | md5 | ripemd160</v> + <v>Type = md5 | ripemd160 | sha | sha224 | sha256 | sha384 | sha512</v> <v>Key = iolist() | binary()</v> <v>Context = binary()</v> </type> diff --git a/lib/crypto/src/crypto.erl b/lib/crypto/src/crypto.erl index 461558a79e..aa89f6cc61 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]). @@ -197,43 +197,48 @@ version() -> ?CRYPTO_VSN. %% -spec hash(_, iodata()) -> binary(). -hash(md5, Data) -> md5(Data); -hash(md4, Data) -> md4(Data); -hash(sha, Data) -> sha(Data); -hash(sha224, Data) -> sha224(Data); -hash(sha256, Data) -> sha256(Data); -hash(sha384, Data) -> sha384(Data); -hash(sha512, Data) -> sha512(Data). - --spec hash_init('md5'|'md4'|'sha'|'sha224'|'sha256'|'sha384'|'sha512') -> any(). - -hash_init(md5) -> {md5, md5_init()}; -hash_init(md4) -> {md4, md4_init()}; -hash_init(sha) -> {sha, sha_init()}; -hash_init(sha224) -> {sha224, sha224_init()}; -hash_init(sha256) -> {sha256, sha256_init()}; -hash_init(sha384) -> {sha384, sha384_init()}; -hash_init(sha512) -> {sha512, sha512_init()}. +hash(md5, Data) -> md5(Data); +hash(md4, Data) -> md4(Data); +hash(sha, Data) -> sha(Data); +hash(ripemd160, Data) -> ripemd160(Data); +hash(sha224, Data) -> sha224(Data); +hash(sha256, Data) -> sha256(Data); +hash(sha384, Data) -> sha384(Data); +hash(sha512, Data) -> sha512(Data). + +-spec hash_init('md5'|'md4'|'ripemd160'| + 'sha'|'sha224'|'sha256'|'sha384'|'sha512') -> any(). + +hash_init(md5) -> {md5, md5_init()}; +hash_init(md4) -> {md4, md4_init()}; +hash_init(sha) -> {sha, sha_init()}; +hash_init(ripemd160) -> {ripemd160, ripemd160_init()}; +hash_init(sha224) -> {sha224, sha224_init()}; +hash_init(sha256) -> {sha256, sha256_init()}; +hash_init(sha384) -> {sha384, sha384_init()}; +hash_init(sha512) -> {sha512, sha512_init()}. -spec hash_update(_, iodata()) -> any(). -hash_update({md5,Context}, Data) -> {md5, md5_update(Context,Data)}; -hash_update({md4,Context}, Data) -> {md4, md4_update(Context,Data)}; -hash_update({sha,Context}, Data) -> {sha, sha_update(Context,Data)}; -hash_update({sha224,Context}, Data) -> {sha224, sha224_update(Context,Data)}; -hash_update({sha256,Context}, Data) -> {sha256, sha256_update(Context,Data)}; -hash_update({sha384,Context}, Data) -> {sha384, sha384_update(Context,Data)}; -hash_update({sha512,Context}, Data) -> {sha512, sha512_update(Context,Data)}. +hash_update({md5,Context}, Data) -> {md5, md5_update(Context,Data)}; +hash_update({md4,Context}, Data) -> {md4, md4_update(Context,Data)}; +hash_update({sha,Context}, Data) -> {sha, sha_update(Context,Data)}; +hash_update({ripemd160,Context}, Data) -> {ripemd160, ripemd160_update(Context,Data)}; +hash_update({sha224,Context}, Data) -> {sha224, sha224_update(Context,Data)}; +hash_update({sha256,Context}, Data) -> {sha256, sha256_update(Context,Data)}; +hash_update({sha384,Context}, Data) -> {sha384, sha384_update(Context,Data)}; +hash_update({sha512,Context}, Data) -> {sha512, sha512_update(Context,Data)}. -spec hash_final(_) -> binary(). -hash_final({md5,Context}) -> md5_final(Context); -hash_final({md4,Context}) -> md4_final(Context); -hash_final({sha,Context}) -> sha_final(Context); -hash_final({sha224,Context}) -> sha224_final(Context); -hash_final({sha256,Context}) -> sha256_final(Context); -hash_final({sha384,Context}) -> sha384_final(Context); -hash_final({sha512,Context}) -> sha512_final(Context). +hash_final({md5,Context}) -> md5_final(Context); +hash_final({md4,Context}) -> md4_final(Context); +hash_final({sha,Context}) -> sha_final(Context); +hash_final({ripemd160,Context}) -> ripemd160_final(Context); +hash_final({sha224,Context}) -> sha224_final(Context); +hash_final({sha256,Context}) -> sha256_final(Context); +hash_final({sha384,Context}) -> sha384_final(Context); +hash_final({sha512,Context}) -> sha512_final(Context). %% %% MD5 @@ -263,6 +268,20 @@ md4_update(_Context, _Data) -> ?nif_stub. md4_final(_Context) -> ?nif_stub. %% +%% RIPEMD160 +%% + +-spec ripemd160(iodata()) -> binary(). +-spec ripemd160_init() -> binary(). +-spec ripemd160_update(binary(), iodata()) -> binary(). +-spec ripemd160_final(binary()) -> binary(). + +ripemd160(_Data) -> ?nif_stub. +ripemd160_init() -> ?nif_stub. +ripemd160_update(_Context, _Data) -> ?nif_stub. +ripemd160_final(_Context) -> ?nif_stub. + +%% %% SHA %% -spec sha(iodata()) -> binary(). @@ -418,11 +437,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. diff --git a/lib/crypto/test/crypto_SUITE.erl b/lib/crypto/test/crypto_SUITE.erl index 8965ab6b94..142f06677a 100644 --- a/lib/crypto/test/crypto_SUITE.erl +++ b/lib/crypto/test/crypto_SUITE.erl @@ -38,7 +38,10 @@ hmac_update_md5/1, hmac_update_md5_io/1, hmac_update_md5_n/1, + hmac_rfc2202/1, hmac_rfc4231/1, + ripemd160/1, + ripemd160_update/1, sha256/1, sha256_update/1, sha512/1, @@ -86,11 +89,11 @@ groups() -> [{info, [sequence],[info, {group, rest}]}, {rest, [], [md5, md5_update, md4, md4_update, md5_mac, - md5_mac_io, sha, sha_update, + md5_mac_io, ripemd160, ripemd160_update, sha, sha_update, 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,8 +421,169 @@ 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. " + ["Generate an HMAC using crypto:shaXXX_mac, hmac, and hmac_init, hmac_update, and hmac_final. " "Testvectors are take from RFC4231." ]; hmac_rfc4231(suite) -> []; @@ -446,29 +610,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">>, @@ -489,29 +661,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), @@ -532,29 +712,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)), @@ -575,29 +763,81 @@ 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 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), @@ -618,29 +858,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), @@ -663,29 +911,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. " @@ -719,7 +975,33 @@ hmac_update_md5_n(Config) when is_list(Config) -> ?line Mac = crypto:hmac_final_n(Ctx3, 12), ?line Exp = crypto:md5_mac_96(Key, lists:flatten([Data, Data2])), ?line m(Exp, Mac). - +%% +%% +ripemd160(doc) -> + ["Generate RIPEMD160 message digests and check the result."]; +ripemd160(suite) -> + []; +ripemd160(Config) when is_list(Config) -> + ?line m(crypto:hash(ripemd160,"abc"), + hexstr2bin("8EB208F7E05D987A9B044A8E98C6B087F15A0BFC")), + ?line m(crypto:hash(ripemd160,"abcdbcdecdefdefgefghfghighijhijkijkljklmklm" + "nlmnomnopnopq"), + hexstr2bin("12A053384A9C0C88E405A06C27DCF49ADA62EB2B")). + + +%% +%% +ripemd160_update(doc) -> + ["Generate RIPEMD160 message digests by using ripemd160_init," + "ripemd160_update, and ripemd160_final and check the result."]; +ripemd160_update(suite) -> + []; +ripemd160_update(Config) when is_list(Config) -> + ?line Ctx = crypto:hash_init(ripemd160), + ?line Ctx1 = crypto:hash_update(Ctx, "abcdbcdecdefdefgefghfghighi"), + ?line Ctx2 = crypto:hash_update(Ctx1, "jhijkijkljklmklmnlmnomnopnopq"), + ?line m(crypto:hash_final(Ctx2), + hexstr2bin("12A053384A9C0C88E405A06C27DCF49ADA62EB2B")). %% %% @@ -1838,7 +2120,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), |