/
hmac.erl
180 lines (166 loc) · 6.45 KB
/
hmac.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
%% @author Jared Flatow, Steve Vinoski
%% @doc HMAC wrappers for erlsha2
%% @reference
%% Based on the
%% <a href="http://erlang.org/pipermail/erlang-questions/2011-May/058174.html">
%% mailing list response
%% </a>
%% by Steve Davis.
-module(hmac).
-export([hexlify/1,
hexlify/2,
hmac/2,
hmac/4,
hmac224/2,
hmac256/2,
hmac384/2,
hmac512/2]).
-on_load(init/0).
-version("2.2.1").
-ifdef(USE_CRYPTO_SHA_MAC).
-define(CRYPTO_HMAC(Key,Data), crypto:sha_mac(Key,Data)).
-else.
-define(CRYPTO_HMAC(Key,Data), crypto:hmac(sha,Key,Data)).
-endif.
-define(SHA_224_BLOCKSIZE, 64).
-define(SHA_256_BLOCKSIZE, 64).
-define(SHA_384_BLOCKSIZE, 128).
-define(SHA_512_BLOCKSIZE, 128).
-define(HMAC_STRING, 1).
-define(HMAC_UPPER, 2).
-define(DEFAULT_HMAC_FLAGS, (?HMAC_STRING bor ?HMAC_UPPER)).
%% @spec init() -> ok | {error, term()}
%% @doc Initialize hmac NIF.
%%
init() ->
SoName = filename:join(case code:priv_dir(?MODULE) of
{error, bad_name} ->
%% this is here for testing purposes
filename:join(
[filename:dirname(
code:which(?MODULE)),"..","priv"]);
Dir ->
Dir
end, atom_to_list(?MODULE) ++ "_nif"),
erlang:load_nif(SoName, 0).
%% @spec hexlify(binary()) -> list()
%% @doc Convert binary to equivalent hexadecimal string.
%%
hexlify(Binary) when is_binary(Binary) ->
hexlify_nif(Binary, ?DEFAULT_HMAC_FLAGS).
%% @spec hexlify(binary(), hmac_opts()) -> string() | binary()
%% @doc Convert binary to equivalent hexadecimal string or binary depending
%% on the options passed in the second argument. If the options list
%% contains the atom 'string' a string is returned, or if the option
%% 'binary' is passed a binary is returned. If the options list contains
%% the atom 'upper' then the alphabetic hexadecimal characters in the
%% return value are uppercase, or if the option 'lower' is passed then the
%% alphabetic hexadecimal characters in the return value are lowercase. The
%% default if no options are passed is to return an uppercase string.
%%
hexlify(Binary, []) when is_binary(Binary) ->
hexlify_nif(Binary, ?DEFAULT_HMAC_FLAGS);
hexlify(Binary, [string,upper]) when is_binary(Binary) ->
hexlify_nif(Binary, ?DEFAULT_HMAC_FLAGS);
hexlify(Binary, [upper,string]) when is_binary(Binary) ->
hexlify_nif(Binary, ?DEFAULT_HMAC_FLAGS);
hexlify(Binary, [string,lower]) when is_binary(Binary) ->
hexlify_nif(Binary, ?HMAC_STRING);
hexlify(Binary, [lower,string]) when is_binary(Binary) ->
hexlify_nif(Binary, ?HMAC_STRING);
hexlify(Binary, [binary,upper]) when is_binary(Binary) ->
hexlify_nif(Binary, ?HMAC_UPPER);
hexlify(Binary, [upper,binary]) when is_binary(Binary) ->
hexlify_nif(Binary, ?HMAC_UPPER);
hexlify(Binary, [binary,lower]) when is_binary(Binary) ->
hexlify_nif(Binary, 0);
hexlify(Binary, [lower,binary]) when is_binary(Binary) ->
hexlify_nif(Binary, 0);
hexlify(Binary, Opts) when is_binary(Binary), is_list(Opts) ->
Flags = lists:foldl(fun(string, Acc) ->
Acc bor ?HMAC_STRING;
(binary, Acc) ->
Acc band bnot ?HMAC_STRING;
(upper, Acc) ->
Acc bor ?HMAC_UPPER;
(lower, Acc) ->
Acc band bnot ?HMAC_UPPER
end, ?DEFAULT_HMAC_FLAGS, Opts),
hexlify_nif(Binary, Flags).
hexlify_nif(_Bin, _Opts) ->
erlang:nif_error(nif_not_loaded).
%% @spec hmac224(key(), data()) -> mac()
%% where
%% key() = iolist() | binary()
%% data() = iolist() | binary()
%% mac() = binary()
%% @doc Compute a SHA-224 MAC message authentication code from key and data.
%%
hmac224(Key, Data) ->
hmac(Key, Data, fun erlsha2:sha224/1, ?SHA_224_BLOCKSIZE).
%% @spec hmac256(key(), data()) -> mac()
%% where
%% key() = iolist() | binary()
%% data() = iolist() | binary()
%% mac() = binary()
%% @doc Compute a SHA-256 MAC message authentication code from key and data.
%%
hmac256(Key, Data) ->
hmac(Key, Data, fun erlsha2:sha256/1, ?SHA_256_BLOCKSIZE).
%% @spec hmac384(key(), data()) -> mac()
%% where
%% key() = iolist() | binary()
%% data() = iolist() | binary()
%% mac() = binary()
%% @doc Compute a SHA-384 MAC message authentication code from key and data.
%%
hmac384(Key, Data) ->
hmac(Key, Data, fun erlsha2:sha384/1, ?SHA_384_BLOCKSIZE).
%% @spec hmac512(key(), data()) -> mac()
%% where
%% key() = iolist() | binary()
%% data() = iolist() | binary()
%% mac() = binary()
%% @doc Compute a SHA-512 MAC message authentication code from key and data.
%%
hmac512(Key, Data) ->
hmac(Key, Data, fun erlsha2:sha512/1, ?SHA_512_BLOCKSIZE).
%% @spec hmac(key(), data()) -> mac()
%% where
%% key() = iolist() | binary()
%% data() = iolist() | binary()
%% mac() = binary()
%% @doc Compute a SHA MAC message authentication code from key and data.
%%
hmac(Key, Data) ->
?CRYPTO_HMAC(Key, Data).
%% @spec hmac(key(), data(), hash(), blocksize()) -> mac()
%% where
%% key() = iolist() | binary()
%% data() = iolist() | binary()
%% hash() = fun((binary()) -> binary())
%% blocksize() = non_neg_integer()
%% mac() = binary()
%% @doc Compute a SHA MAC message authentication code from key and data using
%% the specified hash function and blocksize.
%%
hmac(Key, Data, Hash, Blocksize) when is_list(Key) ->
hmac(iolist_to_binary(Key), Data, Hash, Blocksize);
hmac(Key, Data, Hash, Blocksize) when is_list(Data) ->
hmac(Key, iolist_to_binary(Data), Hash, Blocksize);
hmac(Key, Data, Hash, Blocksize) when is_binary(Key), is_binary(Data) ->
HashKey =
case Blocksize - byte_size(Key) of
X when X < 0 ->
KeyDigest = Hash(Key),
Pad = Blocksize - byte_size(KeyDigest),
<<KeyDigest/binary, 0:(Pad * 8)>>;
X when X > 0 ->
<<Key/binary, 0:(X * 8)>>;
X when X =:= 0 ->
Key
end,
IPad = binary:copy(<<16#36>>, Blocksize),
OPad = binary:copy(<<16#5c>>, Blocksize),
HVal = Hash(<<(crypto:exor(HashKey, IPad))/binary, Data/binary>>),
Hash(<<(crypto:exor(HashKey, OPad))/binary, HVal/binary>>).