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(-) 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