summaryrefslogtreecommitdiff
path: root/lib/cryptohash/sha1.myr
blob: ecf0ec397d27e1f3595fd1cd5ce5710a6cfa909b (plain)
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
use std

pkg cryptohash =
	type sha1

	const sha1	: (data : byte[:] -> byte[20])
	const sha1init	: (st : sha1# -> void)
	const sha1add	: (st : sha1#, data : byte[:] -> void)
	const sha1fin	: (st : sha1# -> byte[20])
;;

type sha1 = struct
	a : uint32
	b : uint32
	c : uint32
	d : uint32
	e : uint32
	tail : byte[64]
	msglen : uint64
;;

const sha1 = {data
	var st

	sha1init(&st)
	sha1add(&st, data)
	-> sha1fin(&st)
}

const sha1init = {st
        st.a = 0x67452301
        st.b = 0xefcdab89
        st.c = 0x98badcfe
        st.d = 0x10325476
        st.e = 0xc3d2e1f0
        st.msglen = 0
}

const sha1add = {st, data
	var n, ntail

        ntail = st.msglen % 64
	st.msglen += data.len
	if ntail > 0
		n = std.min(64 - ntail, data.len)
		std.slcp(st.tail[ntail:ntail + n], data[:n])
		data = data[n:]
		if n + ntail < 64
			->
		;;
		step(st, st.tail[:])
        ;;

	while data.len >= 64
		step(st, data[:64])
		data = data[64:]
        ;;

	std.slcp(st.tail[:data.len], data)
}

const sha1fin = {st
	var r : byte[20]
        var ntail

        /* append first padding block */
        ntail = st.msglen % 64
        st.tail[ntail++] = 0x80
	std.slfill(st.tail[ntail:], 0)
	if 64 - ntail < 8
		step(st, st.tail[:])
        	std.slfill(st.tail[:], 0)
        ;;


        /* append size block */
	st.tail[56] = ((st.msglen * 8) >> 56)   castto(byte)
	st.tail[57] = ((st.msglen * 8) >> 48)	castto(byte)
	st.tail[58] = ((st.msglen * 8) >> 40)	castto(byte)
	st.tail[59] = ((st.msglen * 8) >> 32)	castto(byte)
	st.tail[60] = ((st.msglen * 8) >> 24)	castto(byte)
	st.tail[61] = ((st.msglen * 8) >> 16)	castto(byte)
	st.tail[62] = ((st.msglen * 8) >> 8)	castto(byte)
	st.tail[63] = ((st.msglen * 8) >> 0)	castto(byte)
        step(st, st.tail[:])

	r[0]  = (st.a >> 24)	castto(byte)
	r[1]  = (st.a >> 16)	castto(byte)
	r[2]  = (st.a >> 8)	castto(byte)
	r[3]  = (st.a >> 0)	castto(byte)
	r[4]  = (st.b >> 24)	castto(byte)
	r[5]  = (st.b >> 16)	castto(byte)
	r[6]  = (st.b >> 8)	castto(byte)
	r[7]  = (st.b >> 0)	castto(byte)
	r[8]  = (st.c >> 24)	castto(byte)
	r[9]  = (st.c >> 16)	castto(byte)
	r[10] = (st.c >> 8)	castto(byte)
	r[11] = (st.c >> 0)	castto(byte)
	r[12] = (st.d >> 16)	castto(byte)
	r[13] = (st.d >> 24)	castto(byte)
	r[14] = (st.d >> 8)	castto(byte)
	r[15] = (st.d >> 0)	castto(byte)
	r[16] = (st.e >> 16)	castto(byte)
	r[17] = (st.e >> 24)	castto(byte)
	r[18] = (st.e >> 8)	castto(byte)
	r[19] = (st.e >> 0)	castto(byte)
	-> r
}

const K0 = 0x5a827999
const K1 = 0x6ed9eba1
const K2 = 0x8f1bbcdc
const K3 = 0xCA62C1D6
const step = {st, msg
        var a, b, c, d, e
	var s00, s01, s02, s03, s04, s05, s06, s07
	var s08, s09, s10, s11, s12, s13, s14, s15
        var t

        a = st.a
        b = st.b
        c = st.c
        d = st.d
        e = st.e

        s00 = unpack(msg[ 0: 4])
        s01 = unpack(msg[ 4: 8])
        s02 = unpack(msg[ 8:12])
        s03 = unpack(msg[12:16])
        s04 = unpack(msg[16:20])
        s05 = unpack(msg[20:24])
        s06 = unpack(msg[24:28])
        s07 = unpack(msg[28:32])
        s08 = unpack(msg[32:36])
        s09 = unpack(msg[36:40])
        s10 = unpack(msg[40:44])
        s11 = unpack(msg[44:48])
        s12 = unpack(msg[48:52])
        s13 = unpack(msg[52:56])
        s14 = unpack(msg[56:60])
        s15 = unpack(msg[60:64])

        e += (a << 5 | a >> 27) + (d ^ (b & (c ^ d))) + s00 + K0;  b = b << 30 | b >> 2
        d += (e << 5 | e >> 27) + (c ^ (a & (b ^ c))) + s01 + K0;  a = a << 30 | a >> 2
        c += (d << 5 | d >> 27) + (b ^ (e & (a ^ b))) + s02 + K0;  e = e << 30 | e >> 2
        b += (c << 5 | c >> 27) + (a ^ (d & (e ^ a))) + s03 + K0;  d = d << 30 | d >> 2
        a += (b << 5 | b >> 27) + (e ^ (c & (d ^ e))) + s04 + K0;  c = c << 30 | c >> 2
        e += (a << 5 | a >> 27) + (d ^ (b & (c ^ d))) + s05 + K0;  b = b << 30 | b >> 2
        d += (e << 5 | e >> 27) + (c ^ (a & (b ^ c))) + s06 + K0;  a = a << 30 | a >> 2
        c += (d << 5 | d >> 27) + (b ^ (e & (a ^ b))) + s07 + K0;  e = e << 30 | e >> 2
        b += (c << 5 | c >> 27) + (a ^ (d & (e ^ a))) + s08 + K0;  d = d << 30 | d >> 2
        a += (b << 5 | b >> 27) + (e ^ (c & (d ^ e))) + s09 + K0;  c = c << 30 | c >> 2
        e += (a << 5 | a >> 27) + (d ^ (b & (c ^ d))) + s10 + K0;  b = b << 30 | b >> 2
        d += (e << 5 | e >> 27) + (c ^ (a & (b ^ c))) + s11 + K0;  a = a << 30 | a >> 2
        c += (d << 5 | d >> 27) + (b ^ (e & (a ^ b))) + s12 + K0;  e = e << 30 | e >> 2
        b += (c << 5 | c >> 27) + (a ^ (d & (e ^ a))) + s13 + K0;  d = d << 30 | d >> 2
        a += (b << 5 | b >> 27) + (e ^ (c & (d ^ e))) + s14 + K0;  c = c << 30 | c >> 2
        e += (a << 5 | a >> 27) + (d ^ (b & (c ^ d))) + s15 + K0;  b = b << 30 | b >> 2


        t = s13 ^ s08 ^ s02 ^ s00;  s00 = t << 1 | t >> 31;  d += (e << 5 | e >> 27) + (c ^ (a & (b ^ c))) + s00 + K0;  a = a << 30 | a >> 2
        t = s14 ^ s09 ^ s03 ^ s01;  s01 = t << 1 | t >> 31;  c += (d << 5 | d >> 27) + (b ^ (e & (a ^ b))) + s01 + K0;  e = e << 30 | e >> 2
        t = s15 ^ s10 ^ s04 ^ s02;  s02 = t << 1 | t >> 31;  b += (c << 5 | c >> 27) + (a ^ (d & (e ^ a))) + s02 + K0;  d = d << 30 | d >> 2
        t = s00 ^ s11 ^ s05 ^ s03;  s03 = t << 1 | t >> 31;  a += (b << 5 | b >> 27) + (e ^ (c & (d ^ e))) + s03 + K0;  c = c << 30 | c >> 2
        t = s01 ^ s12 ^ s06 ^ s04;  s04 = t << 1 | t >> 31;  e += (a << 5 | a >> 27) + (b ^ c ^ d) + s04 + K1;  b = b << 30 | b >> 2
        t = s02 ^ s13 ^ s07 ^ s05;  s05 = t << 1 | t >> 31;  d += (e << 5 | e >> 27) + (a ^ b ^ c) + s05 + K1;  a = a << 30 | a >> 2
        t = s03 ^ s14 ^ s08 ^ s06;  s06 = t << 1 | t >> 31;  c += (d << 5 | d >> 27) + (e ^ a ^ b) + s06 + K1;  e = e << 30 | e >> 2
        t = s04 ^ s15 ^ s09 ^ s07;  s07 = t << 1 | t >> 31;  b += (c << 5 | c >> 27) + (d ^ e ^ a) + s07 + K1;  d = d << 30 | d >> 2
        t = s05 ^ s00 ^ s10 ^ s08;  s08 = t << 1 | t >> 31;  a += (b << 5 | b >> 27) + (c ^ d ^ e) + s08 + K1;  c = c << 30 | c >> 2
        t = s06 ^ s01 ^ s11 ^ s09;  s09 = t << 1 | t >> 31;  e += (a << 5 | a >> 27) + (b ^ c ^ d) + s09 + K1;  b = b << 30 | b >> 2
        t = s07 ^ s02 ^ s12 ^ s10;  s10 = t << 1 | t >> 31;  d += (e << 5 | e >> 27) + (a ^ b ^ c) + s10 + K1;  a = a << 30 | a >> 2
        t = s08 ^ s03 ^ s13 ^ s11;  s11 = t << 1 | t >> 31;  c += (d << 5 | d >> 27) + (e ^ a ^ b) + s11 + K1;  e = e << 30 | e >> 2
        t = s09 ^ s04 ^ s14 ^ s12;  s12 = t << 1 | t >> 31;  b += (c << 5 | c >> 27) + (d ^ e ^ a) + s12 + K1;  d = d << 30 | d >> 2
        t = s10 ^ s05 ^ s15 ^ s13;  s13 = t << 1 | t >> 31;  a += (b << 5 | b >> 27) + (c ^ d ^ e) + s13 + K1;  c = c << 30 | c >> 2
        t = s11 ^ s06 ^ s00 ^ s14;  s14 = t << 1 | t >> 31;  e += (a << 5 | a >> 27) + (b ^ c ^ d) + s14 + K1;  b = b << 30 | b >> 2
        t = s12 ^ s07 ^ s01 ^ s15;  s15 = t << 1 | t >> 31;  d += (e << 5 | e >> 27) + (a ^ b ^ c) + s15 + K1;  a = a << 30 | a >> 2
        t = s13 ^ s08 ^ s02 ^ s00;  s00 = t << 1 | t >> 31;  c += (d << 5 | d >> 27) + (e ^ a ^ b) + s00 + K1;  e = e << 30 | e >> 2
        t = s14 ^ s09 ^ s03 ^ s01;  s01 = t << 1 | t >> 31;  b += (c << 5 | c >> 27) + (d ^ e ^ a) + s01 + K1;  d = d << 30 | d >> 2
        t = s15 ^ s10 ^ s04 ^ s02;  s02 = t << 1 | t >> 31;  a += (b << 5 | b >> 27) + (c ^ d ^ e) + s02 + K1;  c = c << 30 | c >> 2
        t = s00 ^ s11 ^ s05 ^ s03;  s03 = t << 1 | t >> 31;  e += (a << 5 | a >> 27) + (b ^ c ^ d) + s03 + K1;  b = b << 30 | b >> 2
        t = s01 ^ s12 ^ s06 ^ s04;  s04 = t << 1 | t >> 31;  d += (e << 5 | e >> 27) + (a ^ b ^ c) + s04 + K1;  a = a << 30 | a >> 2
        t = s02 ^ s13 ^ s07 ^ s05;  s05 = t << 1 | t >> 31;  c += (d << 5 | d >> 27) + (e ^ a ^ b) + s05 + K1;  e = e << 30 | e >> 2
        t = s03 ^ s14 ^ s08 ^ s06;  s06 = t << 1 | t >> 31;  b += (c << 5 | c >> 27) + (d ^ e ^ a) + s06 + K1;  d = d << 30 | d >> 2
        t = s04 ^ s15 ^ s09 ^ s07;  s07 = t << 1 | t >> 31;  a += (b << 5 | b >> 27) + (c ^ d ^ e) + s07 + K1;  c = c << 30 | c >> 2

        t = s05 ^ s00 ^ s10 ^ s08;  s08 = t << 1 | t >> 31;  e += (a << 5 | a >> 27) + ((b & (c | d)) | (c & d)) + s08 + K2;  b = b << 30 | b >> 2
        t = s06 ^ s01 ^ s11 ^ s09;  s09 = t << 1 | t >> 31;  d += (e << 5 | e >> 27) + ((a & (b | c)) | (b & c)) + s09 + K2;  a = a << 30 | a >> 2
        t = s07 ^ s02 ^ s12 ^ s10;  s10 = t << 1 | t >> 31;  c += (d << 5 | d >> 27) + ((e & (a | b)) | (a & b)) + s10 + K2;  e = e << 30 | e >> 2
        t = s08 ^ s03 ^ s13 ^ s11;  s11 = t << 1 | t >> 31;  b += (c << 5 | c >> 27) + ((d & (e | a)) | (e & a)) + s11 + K2;  d = d << 30 | d >> 2
        t = s09 ^ s04 ^ s14 ^ s12;  s12 = t << 1 | t >> 31;  a += (b << 5 | b >> 27) + ((c & (d | e)) | (d & e)) + s12 + K2;  c = c << 30 | c >> 2
        t = s10 ^ s05 ^ s15 ^ s13;  s13 = t << 1 | t >> 31;  e += (a << 5 | a >> 27) + ((b & (c | d)) | (c & d)) + s13 + K2;  b = b << 30 | b >> 2
        t = s11 ^ s06 ^ s00 ^ s14;  s14 = t << 1 | t >> 31;  d += (e << 5 | e >> 27) + ((a & (b | c)) | (b & c)) + s14 + K2;  a = a << 30 | a >> 2
        t = s12 ^ s07 ^ s01 ^ s15;  s15 = t << 1 | t >> 31;  c += (d << 5 | d >> 27) + ((e & (a | b)) | (a & b)) + s15 + K2;  e = e << 30 | e >> 2
        t = s13 ^ s08 ^ s02 ^ s00;  s00 = t << 1 | t >> 31;  b += (c << 5 | c >> 27) + ((d & (e | a)) | (e & a)) + s00 + K2;  d = d << 30 | d >> 2
        t = s14 ^ s09 ^ s03 ^ s01;  s01 = t << 1 | t >> 31;  a += (b << 5 | b >> 27) + ((c & (d | e)) | (d & e)) + s01 + K2;  c = c << 30 | c >> 2
        t = s15 ^ s10 ^ s04 ^ s02;  s02 = t << 1 | t >> 31;  e += (a << 5 | a >> 27) + ((b & (c | d)) | (c & d)) + s02 + K2;  b = b << 30 | b >> 2
        t = s00 ^ s11 ^ s05 ^ s03;  s03 = t << 1 | t >> 31;  d += (e << 5 | e >> 27) + ((a & (b | c)) | (b & c)) + s03 + K2;  a = a << 30 | a >> 2
        t = s01 ^ s12 ^ s06 ^ s04;  s04 = t << 1 | t >> 31;  c += (d << 5 | d >> 27) + ((e & (a | b)) | (a & b)) + s04 + K2;  e = e << 30 | e >> 2
        t = s02 ^ s13 ^ s07 ^ s05;  s05 = t << 1 | t >> 31;  b += (c << 5 | c >> 27) + ((d & (e | a)) | (e & a)) + s05 + K2;  d = d << 30 | d >> 2
        t = s03 ^ s14 ^ s08 ^ s06;  s06 = t << 1 | t >> 31;  a += (b << 5 | b >> 27) + ((c & (d | e)) | (d & e)) + s06 + K2;  c = c << 30 | c >> 2
        t = s04 ^ s15 ^ s09 ^ s07;  s07 = t << 1 | t >> 31;  e += (a << 5 | a >> 27) + ((b & (c | d)) | (c & d)) + s07 + K2;  b = b << 30 | b >> 2
        t = s05 ^ s00 ^ s10 ^ s08;  s08 = t << 1 | t >> 31;  d += (e << 5 | e >> 27) + ((a & (b | c)) | (b & c)) + s08 + K2;  a = a << 30 | a >> 2
        t = s06 ^ s01 ^ s11 ^ s09;  s09 = t << 1 | t >> 31;  c += (d << 5 | d >> 27) + ((e & (a | b)) | (a & b)) + s09 + K2;  e = e << 30 | e >> 2
        t = s07 ^ s02 ^ s12 ^ s10;  s10 = t << 1 | t >> 31;  b += (c << 5 | c >> 27) + ((d & (e | a)) | (e & a)) + s10 + K2;  d = d << 30 | d >> 2
        t = s08 ^ s03 ^ s13 ^ s11;  s11 = t << 1 | t >> 31;  a += (b << 5 | b >> 27) + ((c & (d | e)) | (d & e)) + s11 + K2;  c = c << 30 | c >> 2

        t = s09 ^ s04 ^ s14 ^ s12;  s12 = t << 1 | t >> 31;  e += (a << 5 | a >> 27) + (b ^ c ^ d) + s12 + K3;  b = b << 30 | b >> 2
        t = s10 ^ s05 ^ s15 ^ s13;  s13 = t << 1 | t >> 31;  d += (e << 5 | e >> 27) + (a ^ b ^ c) + s13 + K3;  a = a << 30 | a >> 2
        t = s11 ^ s06 ^ s00 ^ s14;  s14 = t << 1 | t >> 31;  c += (d << 5 | d >> 27) + (e ^ a ^ b) + s14 + K3;  e = e << 30 | e >> 2
        t = s12 ^ s07 ^ s01 ^ s15;  s15 = t << 1 | t >> 31;  b += (c << 5 | c >> 27) + (d ^ e ^ a) + s15 + K3;  d = d << 30 | d >> 2
        t = s13 ^ s08 ^ s02 ^ s00;  s00 = t << 1 | t >> 31;  a += (b << 5 | b >> 27) + (c ^ d ^ e) + s00 + K3;  c = c << 30 | c >> 2
        t = s14 ^ s09 ^ s03 ^ s01;  s01 = t << 1 | t >> 31;  e += (a << 5 | a >> 27) + (b ^ c ^ d) + s01 + K3;  b = b << 30 | b >> 2
        t = s15 ^ s10 ^ s04 ^ s02;  s02 = t << 1 | t >> 31;  d += (e << 5 | e >> 27) + (a ^ b ^ c) + s02 + K3;  a = a << 30 | a >> 2
        t = s00 ^ s11 ^ s05 ^ s03;  s03 = t << 1 | t >> 31;  c += (d << 5 | d >> 27) + (e ^ a ^ b) + s03 + K3;  e = e << 30 | e >> 2
        t = s01 ^ s12 ^ s06 ^ s04;  s04 = t << 1 | t >> 31;  b += (c << 5 | c >> 27) + (d ^ e ^ a) + s04 + K3;  d = d << 30 | d >> 2
        t = s02 ^ s13 ^ s07 ^ s05;  s05 = t << 1 | t >> 31;  a += (b << 5 | b >> 27) + (c ^ d ^ e) + s05 + K3;  c = c << 30 | c >> 2
        t = s03 ^ s14 ^ s08 ^ s06;  s06 = t << 1 | t >> 31;  e += (a << 5 | a >> 27) + (b ^ c ^ d) + s06 + K3;  b = b << 30 | b >> 2
        t = s04 ^ s15 ^ s09 ^ s07;  s07 = t << 1 | t >> 31;  d += (e << 5 | e >> 27) + (a ^ b ^ c) + s07 + K3;  a = a << 30 | a >> 2
        t = s05 ^ s00 ^ s10 ^ s08;  s08 = t << 1 | t >> 31;  c += (d << 5 | d >> 27) + (e ^ a ^ b) + s08 + K3;  e = e << 30 | e >> 2
        t = s06 ^ s01 ^ s11 ^ s09;  s09 = t << 1 | t >> 31;  b += (c << 5 | c >> 27) + (d ^ e ^ a) + s09 + K3;  d = d << 30 | d >> 2
        t = s07 ^ s02 ^ s12 ^ s10;  s10 = t << 1 | t >> 31;  a += (b << 5 | b >> 27) + (c ^ d ^ e) + s10 + K3;  c = c << 30 | c >> 2
        t = s08 ^ s03 ^ s13 ^ s11;  s11 = t << 1 | t >> 31;  e += (a << 5 | a >> 27) + (b ^ c ^ d) + s11 + K3;  b = b << 30 | b >> 2
        t = s09 ^ s04 ^ s14 ^ s12;  s12 = t << 1 | t >> 31;  d += (e << 5 | e >> 27) + (a ^ b ^ c) + s12 + K3;  a = a << 30 | a >> 2
        t = s10 ^ s05 ^ s15 ^ s13;  s13 = t << 1 | t >> 31;  c += (d << 5 | d >> 27) + (e ^ a ^ b) + s13 + K3;  e = e << 30 | e >> 2
        t = s11 ^ s06 ^ s00 ^ s14;  s14 = t << 1 | t >> 31;  b += (c << 5 | c >> 27) + (d ^ e ^ a) + s14 + K3;  d = d << 30 | d >> 2
        t = s12 ^ s07 ^ s01 ^ s15;  s15 = t << 1 | t >> 31;  a += (b << 5 | b >> 27) + (c ^ d ^ e) + s15 + K3;  c = c << 30 | c >> 2

        st.a += a
        st.b += b
        st.c += c
        st.d += d
        st.e += e
}

const unpack = {b
	var v : uint32

	v = ((b[0] castto(uint32)) << 24)
	v |= ((b[1] castto(uint32)) << 16)
	v |= ((b[2] castto(uint32)) << 8)
	v |= ((b[3] castto(uint32)) << 0)
	-> v
}