-
Notifications
You must be signed in to change notification settings - Fork 0
/
SHA512.m
172 lines (171 loc) · 6.26 KB
/
SHA512.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
%
% SHA-512
%
clear;
clc;
%
% K constant, SHA-384, SHA-512, SHA-512/224, SHA-512/256 use the same sequence
% of eight 64-bit words, K0, K1, ..., K79. These words represent the first
% 64-bits of the fractional parts of the cube roots of the first 8 prime numbers.
%
KT = ['428a2f98d728ae22'; '7137449123ef65cd'; 'b5c0fbcfec4d3b2f'; 'e9b5dba58189dbbc';
'3956c25bf348b538'; '59f111f1b605d019'; '923f82a4af194f9b'; 'ab1c5ed5da6d8118';
'd807aa98a3030242'; '12835b0145706fbe'; '243185be4ee4b28c'; '550c7dc3d5ffb4e2';
'72be5d74f27b896f'; '80deb1fe3b1696b1'; '9bdc06a725c71235'; 'c19bf174cf692694';
'e49b69c19ef14ad2'; 'efbe4786384f25e3'; '0fc19dc68b8cd5b5'; '240ca1cc77ac9c65';
'2de92c6f592b0275'; '4a7484aa6ea6e483'; '5cb0a9dcbd41fbd4'; '76f988da831153b5';
'983e5152ee66dfab'; 'a831c66d2db43210'; 'b00327c898fb213f'; 'bf597fc7beef0ee4';
'c6e00bf33da88fc2'; 'd5a79147930aa725'; '06ca6351e003826f'; '142929670a0e6e70';
'27b70a8546d22ffc'; '2e1b21385c26c926'; '4d2c6dfc5ac42aed'; '53380d139d95b3df';
'650a73548baf63de'; '766a0abb3c77b2a8'; '81c2c92e47edaee6'; '92722c851482353b';
'a2bfe8a14cf10364'; 'a81a664bbc423001'; 'c24b8b70d0f89791'; 'c76c51a30654be30';
'd192e819d6ef5218'; 'd69906245565a910'; 'f40e35855771202a'; '106aa07032bbd1b8';
'19a4c116b8d2d0c8'; '1e376c085141ab53'; '2748774cdf8eeb99'; '34b0bcb5e19b48a8';
'391c0cb3c5c95a63'; '4ed8aa4ae3418acb'; '5b9cca4f7763e373'; '682e6ff3d6b2b8a3';
'748f82ee5defb2fc'; '78a5636f43172f60'; '84c87814a1f0ab72'; '8cc702081a6439ec';
'90befffa23631e28'; 'a4506cebde82bde9'; 'bef9a3f7b2c67915'; 'c67178f2e372532b';
'ca273eceea26619c'; 'd186b8c721c0c207'; 'eada7dd6cde0eb1e'; 'f57d4f7fee6ed178';
'06f067aa72176fba'; '0a637dc5a2c898a6'; '113f9804bef90dae'; '1b710b35131c471b';
'28db77f523047d84'; '32caab7b40c72493'; '3c9ebe0a15c9bebc'; '431d67c49c100d4c';
'4cc5d4becb3e42b6'; '597f299cfc657e2a'; '5fcb6fab3ad6faec'; '6c44198c4a475817'];
%
% initial hash value, H^(0), consist of eight 64-bit words
%
H_512 = ['6a09e667f3bcc908';
'bb67ae8584caa73b';
'3c6ef372fe94f82b';
'a54ff53a5f1d36f1';
'510e527fade682d1';
'9b05688c2b3e6c1f';
'1f83d9abfb41bd6b';
'5be0cd19137e2179'];
H = H_512;
%
% Input data to be hashed
%
input_str = 'abc';
%
% indices
% 128 == 1024 / 8
% 16 == 128 / 8
% 111 == 128 - 16 - 1
%
% padding
%
str_len = length(input_str);
Nr = mod(str_len, 128);
if Nr <= 111
N = floor(str_len/128) + 1;
else
N = floor(str_len/128) + 2;
end
%
w = char();
for i = 1 : str_len
w = strcat(w, dec2hex(uint8(double(input_str(i))), 2));
end
w = strcat(w, dec2hex(uint8(128), 2)); % append 1000 0000
w = strcat(w, dec2hex(8*str_len, 2*(128*N - str_len - 1)));
%
% parse the message
%
ww = char();
for in = 1 : N
ww(in, :) = w((in-1)*256 + 1 : in*256);
end
%
% outer round for N blocks
%
W = char();
for in = 1 : N
%
% internal round for single 1024-bit block
%
for iw = 1 : 16
% W(iw, :) = ww(in, (iw-1)*16 + 1 : iw*16);
W(iw, :) = w((in-1)*256 + (iw-1)*16 + 1 : (in-1)*256 + iw*16);
end
%
%
for iw = 17 : 80
% sigma1_512 = bitxor_64(bitxor_64(bitshift_circular_right_64(W(iw-2, :), 19), ...
% bitshift_circular_right_64(W(iw-2, :), 61)), bitshift_right_64(W(iw-2, :), 6));
% sigma0_512 = bitxor_64(bitxor_64(bitshift_circular_right_64(W(iw-15, :), 1), ...
% bitshift_circular_right_64(W(iw-15, :), 8)), bitshift_right_64(W(iw-15, :), 7));
% W(iw, :) = mod_add_64(mod_add_64(mod_add_64(sigma1_512, W(iw - 7, :)), sigma0_512), W(iw - 16, :));
%
ROTR9 = bitshift_circular_right_64(W(iw-2, :), 19);
ROTR61 = bitshift_circular_right_64(W(iw-2, :), 61);
SHR6 = bitshift_right_64(W(iw-2, :), 6);
sigma1 = bitxor_64(bitxor_64(ROTR9, ROTR61), SHR6);
%
ROTR1 = bitshift_circular_right_64(W(iw-15, :), 1);
ROTR8 = bitshift_circular_right_64(W(iw-15, :), 8);
SHR7 = bitshift_right_64(W(iw-15, :), 7);
sigma0 = bitxor_64(bitxor_64(ROTR1, ROTR8), SHR7);
W(iw, :) = mod_add_64(mod_add_64(mod_add_64(sigma1, W(iw-7, :)), sigma0), W(iw-16, :));
%
end
%
%
a = H(1,:);
b = H(2,:);
c = H(3,:);
d = H(4,:);
e = H(5,:);
f = H(6,:);
g = H(7,:);
h = H(8,:);
%
%
for iround = 1 : 80
% sigma_e = bitxor_64(bitxor_64(bitshift_circular_right_64(e, 14), bitshift_circular_right_64(e, 18)), bitshift_circular_right_64(e, 41));
% sigma_a = bitxor_64(bitxor_64(bitshift_circular_right_64(a, 28), bitshift_circular_right_64(a, 34)), bitshift_circular_right_64(a, 39));
%
chefg = bitxor_64(bitand_64(e, f), bitand_64(bitcmp_64(e), g));
ROTR14 = bitshift_circular_right_64(e, 14);
ROTR18 = bitshift_circular_right_64(e, 18);
ROTR41 = bitshift_circular_right_64(e, 41);
sigma_e = bitxor_64(bitxor_64(ROTR14, ROTR18), ROTR41);
%
ROTR28 = bitshift_circular_right_64(a, 28);
ROTR34 = bitshift_circular_right_64(a, 34);
ROTR39 = bitshift_circular_right_64(a, 39);
sigma_a = bitxor_64(bitxor_64(ROTR28, ROTR34), ROTR39);
%
maj_abc = bitxor_64(bitxor_64(bitand_64(a, b), bitand_64(a, c)), bitand_64(b, c));
T1 = mod_add_64(mod_add_64(mod_add_64(mod_add_64(h, chefg), sigma_e), W(iround, :)), KT(iround, :));
T2 = mod_add_64(sigma_a, maj_abc);
%
%
h = g;
g = f;
f = e;
e = mod_add_64(d, T1);
d = c;
c = b;
b = a;
a = mod_add_64(T1, T2);
%
%
end
H(1, :) = mod_add_64(a, H(1, :));
H(2, :) = mod_add_64(b, H(2, :));
H(3, :) = mod_add_64(c, H(3, :));
H(4, :) = mod_add_64(d, H(4, :));
H(5, :) = mod_add_64(e, H(5, :));
H(6, :) = mod_add_64(f, H(6, :));
H(7, :) = mod_add_64(g, H(7, :));
H(8, :) = mod_add_64(h, H(8, :));
end
%
%
HASH = H(1, :);
for i = 2 : 8
HASH = strcat(HASH, H(i, :));
end
HASH = lower(HASH);
fprintf('The input string is: %s\n', input_str);
fprintf('The SHA-512 HASH value is: %s\n', HASH);
%
%