-
Notifications
You must be signed in to change notification settings - Fork 0
/
HMAC.m
177 lines (135 loc) · 4.7 KB
/
HMAC.m
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
function hash = HMAC(key,message,method)
% key: input secret key in char
% message: input message in char
% method: hash method, either:
% 'SHA-1', 'SHA-256', 'SHA-384', 'SHA-512'
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if strcmp(method,'SHA-1') == 1
Blocksize = 64;
elseif strcmp(method,'SHA-256') == 1
Blocksize = 64;
elseif strcmp(method,'SHA-384') == 1
Blocksize = 128;
elseif strcmp(method,'SHA-512') == 1
Blocksize = 128;
end
% if key length > Blocksize calculate Hash and format as binary
if length(key) > Blocksize
Opt.Method = method;
Opt.Format = 'uint8';
Opt.Input = 'bin';
Hash_key = DataHash(uint8(key),Opt)
for i = length(Hash_key):Blocksize
Hash_key(1,i) = 0;
end
key_bin = uint82bin8(Hash_key);
end
% if key length < Blocksize right pad with zeros and format as binary
if (length(key) > 0) && (length(key) < Blocksize)
key_bin = str2bin8(key);
L = length(key);
for j = L+1:Blocksize
key_bin{1,j} = [0 0 0 0 0 0 0 0];
end
end
% if key length = 0 right pad with zeros and format as binary
if length(key) ==0
for j = 1:Blocksize
key_bin{1,j} = [0 0 0 0 0 0 0 0];
end
end
% if key length = Blocksize format key as binary numbers
if length(key) == Blocksize
key_bin = str2bin8(key);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% format inner and outer padding as binary cell arrays
i_pad = [0 0 1 1 0 1 1 0];
o_pad = [0 1 0 1 1 1 0 0];
for i = 1:Blocksize
i_pad_bin{1,i} = i_pad;
o_pad_bin{1,i} = o_pad;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% calculate key xor ipad and key xor opad
i_pad_key_bin = bit8xor(key_bin,i_pad_bin);
o_pad_key_bin = bit8xor(key_bin,o_pad_bin);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Change format to uint8
i_pad_key_hex = bin82hex(i_pad_key_bin);
i_pad_key_uint8 = hex2uint8(i_pad_key_hex);
o_pad_key_hex = bin82hex(o_pad_key_bin);
o_pad_key_uint8 = hex2uint8(o_pad_key_hex);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% concatenate (i_pad_key || message)
concat_i_pad = [i_pad_key_uint8,uint8(message)];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Calculate Hash of i_pad_key||message
Opt.Method = method;
Opt.Format = 'uint8';
Opt.Input = 'bin';
Hash_i_pad_uint8 = DataHash(concat_i_pad,Opt);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% concatenate (o_pad || Hash_i_pad)
concat_o_pad = [o_pad_key_uint8,Hash_i_pad_uint8];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Calculate final Hash in HEX
Opt.Method = method;
Opt.Format = 'HEX';
Opt.Input = 'bin';
hash = DataHash(concat_o_pad,Opt);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% FORMAT HELP FUNCTIONS
function bin = str2bin8(str)
for i = 1:length(str)
temp = dec2bin(str2num(sprintf('%u',str(i))),8);
for j = 1:8
bin{1,i}(1,j) = str2num(temp(j));
end
end
end
function hex = bin82hex(bin)
hex = cell(1,length(bin));
for i = 1:length(bin)
string_bin = num2str(bin{1,i}(1,1)) ;
for j = 2:8
temp = num2str(bin{1,i}(1,j));
string_bin = strcat(string_bin,temp);
end
hex{1,i} = dec2hex(bin2dec(string_bin));
end
end
function v = bit8xor(a,b)
if length(a) == length(b)
for i = 1:length(a)
v{1,i} = xor(a{1,i},b{1,i});
end
else
ERROR = sprintf('%s','Input cells must be the same length')
end
end
function out = hex2uint8(hex)
for i = 1:length(hex)
out(i) = hex2dec(hex{1,i});
end
out = uint8(out);
end
function bin = uint82bin8(in)
bin = cell(1,length(in));
for i = 1:length(in)
temp = dec2bin(in(1,i),8);
for j = 1:8
bin{1,i}(1,j) = str2num(temp(j));
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%