From 807fc6ec56e3e7a65d9de5884cf6ac9927e5c1aa Mon Sep 17 00:00:00 2001 From: Sverker Eriksson Date: Wed, 27 Jun 2012 20:53:34 +0200 Subject: crypto: Cleanup code for sha256 and sha512 --- lib/crypto/c_src/crypto.c | 16 ++++++++++------ lib/crypto/src/crypto.erl | 35 +++++++++++++---------------------- 2 files changed, 23 insertions(+), 28 deletions(-) diff --git a/lib/crypto/c_src/crypto.c b/lib/crypto/c_src/crypto.c index a1f2614f69..3b412e3c1b 100644 --- a/lib/crypto/c_src/crypto.c +++ b/lib/crypto/c_src/crypto.c @@ -149,8 +149,8 @@ static ERL_NIF_TERM md4_update(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv static ERL_NIF_TERM md4_final(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); static ERL_NIF_TERM md5_mac_n(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); static ERL_NIF_TERM sha_mac_n(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); -static ERL_NIF_TERM sha256_mac_n_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); -static ERL_NIF_TERM sha512_mac_n_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); +static ERL_NIF_TERM sha256_mac_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); +static ERL_NIF_TERM sha512_mac_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); static ERL_NIF_TERM hmac_init(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); static ERL_NIF_TERM hmac_update(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); static ERL_NIF_TERM hmac_final(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); @@ -247,8 +247,8 @@ static ErlNifFunc nif_funcs[] = { {"md4_final", 1, md4_final}, {"md5_mac_n", 3, md5_mac_n}, {"sha_mac_n", 3, sha_mac_n}, - {"sha256_mac_n_nif", 3, sha256_mac_n_nif}, - {"sha512_mac_n_nif", 3, sha512_mac_n_nif}, + {"sha256_mac_nif", 3, sha256_mac_nif}, + {"sha512_mac_nif", 3, sha512_mac_nif}, {"hmac_init", 2, hmac_init}, {"hmac_update", 2, hmac_update}, {"hmac_final", 1, hmac_final}, @@ -780,7 +780,7 @@ static ERL_NIF_TERM sha_mac_n(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[ return ret; } -static ERL_NIF_TERM sha256_mac_n_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) +static ERL_NIF_TERM sha256_mac_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {/* (Key, Data, MacSize) */ #ifdef HAVE_SHA256 unsigned char hmacbuf[SHA256_DIGEST_LENGTH]; @@ -802,7 +802,7 @@ static ERL_NIF_TERM sha256_mac_n_nif(ErlNifEnv* env, int argc, const ERL_NIF_TER #endif } -static ERL_NIF_TERM sha512_mac_n_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) +static ERL_NIF_TERM sha512_mac_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {/* (Key, Data, MacSize) */ #ifdef HAVE_SHA512 unsigned char hmacbuf[SHA512_DIGEST_LENGTH]; @@ -832,8 +832,12 @@ static ERL_NIF_TERM hmac_init(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[ const EVP_MD *md; if (argv[0] == atom_sha) md = EVP_sha1(); +#ifdef HAVE_SHA256 else if (argv[0] == atom_sha256) md = EVP_sha256(); +#endif +#ifdef HAVE_SHA512 else if (argv[0] == atom_sha512) md = EVP_sha512(); +#endif else if (argv[0] == atom_md5) md = EVP_md5(); else if (argv[0] == atom_ripemd160) md = EVP_ripemd160(); else goto badarg; diff --git a/lib/crypto/src/crypto.erl b/lib/crypto/src/crypto.erl index 0f14092f87..cb1173f6be 100644 --- a/lib/crypto/src/crypto.erl +++ b/lib/crypto/src/crypto.erl @@ -29,7 +29,8 @@ -export([sha256/1, sha256_init/0, sha256_update/2, sha256_final/1]). -export([sha512/1, sha512_init/0, sha512_update/2, sha512_final/1]). -export([md5_mac/2, md5_mac_96/2, sha_mac/2, sha_mac/3, sha_mac_96/2]). --export([sha256_mac/2, sha256_mac_96/2, sha512_mac/2, sha512_mac/3, sha512_mac_96/2]). +-export([sha256_mac/2, sha256_mac/3]). +-export([sha512_mac/2, sha512_mac/3]). -export([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]). @@ -70,8 +71,7 @@ sha512, sha512_init, sha512_update, sha512_final, md5_mac, md5_mac_96, sha_mac, sha_mac_96, - sha256_mac, sha256_mac_96, - sha512_mac, sha512_mac_96, + sha256_mac, sha512_mac, sha_mac_init, sha_mac_update, sha_mac_final, des_cbc_encrypt, des_cbc_decrypt, des_cfb_encrypt, des_cfb_decrypt, @@ -353,47 +353,38 @@ sha_mac_n(_Key,_Data,_MacSz) -> ?nif_stub. %% SHA256_MAC %% -spec sha256_mac(iodata(), iodata()) -> binary(). --spec sha256_mac_96(iodata(), iodata()) -> binary(). sha256_mac(Key, Data) -> - sha256_mac_n(Key,Data,32). + sha256_mac(Key, Data, 256 div 8). sha256_mac(Key, Data, Size) -> - sha256_mac_n(Key, Data, Size). + case sha256_mac_nif(Key, Data, Size) of + notsup -> erlang:error(notsup); + Bin -> Bin + end. -sha256_mac_96(Key, Data) -> - sha256_mac_n(Key,Data,12). +sha256_mac_nif(_Key,_Data,_MacSz) -> ?nif_stub. -sha256_mac_n(Key, Data, MacSz) -> - case sha256_mac_n_nif(Key, Data, MacSz) of notsup -> erlang:error(notsup); Bin -> Bin end. -sha256_mac_n_nif(_Key,_Data,_MacSz) -> ?nif_stub. %% %% SHA512_MAC %% -spec sha512_mac(iodata(), iodata()) -> binary(). --spec sha512_mac_96(iodata(), iodata()) -> binary(). sha512_mac(Key, Data) -> - sha512_mac_n(Key,Data,64). - -sha512_mac(Key, Data, Size) -> - sha512_mac_n(Key, Data, Size). - -sha512_mac_96(Key, Data) -> - sha512_mac_n(Key,Data,12). + sha512_mac(Key, Data, 512 div 8). -sha512_mac_n(Key, Data, MacSz) -> - case sha512_mac_n_nif(Key, Data, MacSz) of +sha512_mac(Key, Data, MacSz) -> + case sha512_mac_nif(Key, Data, MacSz) of notsup -> erlang:error(notsup); Bin -> Bin end. -sha512_mac_n_nif(_Key,_Data,_MacSz) -> ?nif_stub. +sha512_mac_nif(_Key,_Data,_MacSz) -> ?nif_stub. %% %% CRYPTO FUNCTIONS -- cgit v1.2.3