]>
git.ipfire.org Git - thirdparty/bird.git/blob - lib/sha1.c
2 * BIRD Library -- SHA-1 Hash Function (FIPS 180-1, RFC 3174) and HMAC-SHA-1
4 * (c) 2015 CZ.NIC z.s.p.o.
6 * Based on the code from libucw-6.4
7 * (c) 2008--2009 Martin Mares <mj@ucw.cz>
9 * Based on the code from libgcrypt-1.2.3, which is
10 * (c) 1998, 2001, 2002, 2003 Free Software Foundation, Inc.
12 * Can be freely distributed and used under the terms of the GNU GPL.
16 #include "lib/unaligned.h"
20 sha1_init(struct sha1_context
*ctx
)
33 * Transform the message X which consists of 16 32-bit-words
36 sha1_transform(struct sha1_context
*ctx
, const byte
*data
)
41 /* Get values from the chaining vars. */
52 for (i
= 0; i
< 16; i
++)
53 x
[i
] = get_u32(data
+4*i
);
56 #define K1 0x5A827999L
57 #define K2 0x6ED9EBA1L
58 #define K3 0x8F1BBCDCL
59 #define K4 0xCA62C1D6L
60 #define F1(x,y,z) ( z ^ ( x & ( y ^ z ) ) )
61 #define F2(x,y,z) ( x ^ y ^ z )
62 #define F3(x,y,z) ( ( x & y ) | ( z & ( x | y ) ) )
63 #define F4(x,y,z) ( x ^ y ^ z )
65 #define M(i) (tm = x[i&0x0f] ^ x[(i-14)&0x0f] ^ x[(i-8)&0x0f] ^ x[(i-3)&0x0f], (x[i&0x0f] = ROL(tm, 1)))
67 /* Bitwise rotation of an unsigned int to the left **/
68 #define ROL(x, bits) (((x) << (bits)) | ((uint)(x) >> (sizeof(uint)*8 - (bits))))
70 #define R(a, b, c, d, e, f, k, m) \
73 e += ROL(a, 5) + f(b, c, d) + k + m; \
77 R( a
, b
, c
, d
, e
, F1
, K1
, x
[ 0] );
78 R( e
, a
, b
, c
, d
, F1
, K1
, x
[ 1] );
79 R( d
, e
, a
, b
, c
, F1
, K1
, x
[ 2] );
80 R( c
, d
, e
, a
, b
, F1
, K1
, x
[ 3] );
81 R( b
, c
, d
, e
, a
, F1
, K1
, x
[ 4] );
82 R( a
, b
, c
, d
, e
, F1
, K1
, x
[ 5] );
83 R( e
, a
, b
, c
, d
, F1
, K1
, x
[ 6] );
84 R( d
, e
, a
, b
, c
, F1
, K1
, x
[ 7] );
85 R( c
, d
, e
, a
, b
, F1
, K1
, x
[ 8] );
86 R( b
, c
, d
, e
, a
, F1
, K1
, x
[ 9] );
87 R( a
, b
, c
, d
, e
, F1
, K1
, x
[10] );
88 R( e
, a
, b
, c
, d
, F1
, K1
, x
[11] );
89 R( d
, e
, a
, b
, c
, F1
, K1
, x
[12] );
90 R( c
, d
, e
, a
, b
, F1
, K1
, x
[13] );
91 R( b
, c
, d
, e
, a
, F1
, K1
, x
[14] );
92 R( a
, b
, c
, d
, e
, F1
, K1
, x
[15] );
93 R( e
, a
, b
, c
, d
, F1
, K1
, M(16) );
94 R( d
, e
, a
, b
, c
, F1
, K1
, M(17) );
95 R( c
, d
, e
, a
, b
, F1
, K1
, M(18) );
96 R( b
, c
, d
, e
, a
, F1
, K1
, M(19) );
97 R( a
, b
, c
, d
, e
, F2
, K2
, M(20) );
98 R( e
, a
, b
, c
, d
, F2
, K2
, M(21) );
99 R( d
, e
, a
, b
, c
, F2
, K2
, M(22) );
100 R( c
, d
, e
, a
, b
, F2
, K2
, M(23) );
101 R( b
, c
, d
, e
, a
, F2
, K2
, M(24) );
102 R( a
, b
, c
, d
, e
, F2
, K2
, M(25) );
103 R( e
, a
, b
, c
, d
, F2
, K2
, M(26) );
104 R( d
, e
, a
, b
, c
, F2
, K2
, M(27) );
105 R( c
, d
, e
, a
, b
, F2
, K2
, M(28) );
106 R( b
, c
, d
, e
, a
, F2
, K2
, M(29) );
107 R( a
, b
, c
, d
, e
, F2
, K2
, M(30) );
108 R( e
, a
, b
, c
, d
, F2
, K2
, M(31) );
109 R( d
, e
, a
, b
, c
, F2
, K2
, M(32) );
110 R( c
, d
, e
, a
, b
, F2
, K2
, M(33) );
111 R( b
, c
, d
, e
, a
, F2
, K2
, M(34) );
112 R( a
, b
, c
, d
, e
, F2
, K2
, M(35) );
113 R( e
, a
, b
, c
, d
, F2
, K2
, M(36) );
114 R( d
, e
, a
, b
, c
, F2
, K2
, M(37) );
115 R( c
, d
, e
, a
, b
, F2
, K2
, M(38) );
116 R( b
, c
, d
, e
, a
, F2
, K2
, M(39) );
117 R( a
, b
, c
, d
, e
, F3
, K3
, M(40) );
118 R( e
, a
, b
, c
, d
, F3
, K3
, M(41) );
119 R( d
, e
, a
, b
, c
, F3
, K3
, M(42) );
120 R( c
, d
, e
, a
, b
, F3
, K3
, M(43) );
121 R( b
, c
, d
, e
, a
, F3
, K3
, M(44) );
122 R( a
, b
, c
, d
, e
, F3
, K3
, M(45) );
123 R( e
, a
, b
, c
, d
, F3
, K3
, M(46) );
124 R( d
, e
, a
, b
, c
, F3
, K3
, M(47) );
125 R( c
, d
, e
, a
, b
, F3
, K3
, M(48) );
126 R( b
, c
, d
, e
, a
, F3
, K3
, M(49) );
127 R( a
, b
, c
, d
, e
, F3
, K3
, M(50) );
128 R( e
, a
, b
, c
, d
, F3
, K3
, M(51) );
129 R( d
, e
, a
, b
, c
, F3
, K3
, M(52) );
130 R( c
, d
, e
, a
, b
, F3
, K3
, M(53) );
131 R( b
, c
, d
, e
, a
, F3
, K3
, M(54) );
132 R( a
, b
, c
, d
, e
, F3
, K3
, M(55) );
133 R( e
, a
, b
, c
, d
, F3
, K3
, M(56) );
134 R( d
, e
, a
, b
, c
, F3
, K3
, M(57) );
135 R( c
, d
, e
, a
, b
, F3
, K3
, M(58) );
136 R( b
, c
, d
, e
, a
, F3
, K3
, M(59) );
137 R( a
, b
, c
, d
, e
, F4
, K4
, M(60) );
138 R( e
, a
, b
, c
, d
, F4
, K4
, M(61) );
139 R( d
, e
, a
, b
, c
, F4
, K4
, M(62) );
140 R( c
, d
, e
, a
, b
, F4
, K4
, M(63) );
141 R( b
, c
, d
, e
, a
, F4
, K4
, M(64) );
142 R( a
, b
, c
, d
, e
, F4
, K4
, M(65) );
143 R( e
, a
, b
, c
, d
, F4
, K4
, M(66) );
144 R( d
, e
, a
, b
, c
, F4
, K4
, M(67) );
145 R( c
, d
, e
, a
, b
, F4
, K4
, M(68) );
146 R( b
, c
, d
, e
, a
, F4
, K4
, M(69) );
147 R( a
, b
, c
, d
, e
, F4
, K4
, M(70) );
148 R( e
, a
, b
, c
, d
, F4
, K4
, M(71) );
149 R( d
, e
, a
, b
, c
, F4
, K4
, M(72) );
150 R( c
, d
, e
, a
, b
, F4
, K4
, M(73) );
151 R( b
, c
, d
, e
, a
, F4
, K4
, M(74) );
152 R( a
, b
, c
, d
, e
, F4
, K4
, M(75) );
153 R( e
, a
, b
, c
, d
, F4
, K4
, M(76) );
154 R( d
, e
, a
, b
, c
, F4
, K4
, M(77) );
155 R( c
, d
, e
, a
, b
, F4
, K4
, M(78) );
156 R( b
, c
, d
, e
, a
, F4
, K4
, M(79) );
158 /* Update chaining vars. */
167 * Update the message digest with the contents of BUF with length LEN.
170 sha1_update(struct sha1_context
*ctx
, const byte
*buf
, uint len
)
174 /* Fill rest of internal buffer */
175 for (; len
&& ctx
->count
< SHA1_BLOCK_SIZE
; len
--)
176 ctx
->buf
[ctx
->count
++] = *buf
++;
178 if (ctx
->count
< SHA1_BLOCK_SIZE
)
181 /* Process data from internal buffer */
182 sha1_transform(ctx
, ctx
->buf
);
190 /* Process data from input buffer */
191 while (len
>= SHA1_BLOCK_SIZE
)
193 sha1_transform(ctx
, buf
);
195 buf
+= SHA1_BLOCK_SIZE
;
196 len
-= SHA1_BLOCK_SIZE
;
199 /* Copy remaining data to internal buffer */
200 memcpy(ctx
->buf
, buf
, len
);
205 * The routine final terminates the computation and returns the digest. The
206 * handle is prepared for a new cycle, but adding bytes to the handle will the
207 * destroy the returned buffer.
209 * Returns: 20 bytes representing the digest.
212 sha1_final(struct sha1_context
*ctx
)
216 sha1_update(ctx
, NULL
, 0); /* flush */
219 /* multiply by 64 to make a byte count */
224 if ((lsb
+= ctx
->count
) < t
)
226 /* multiply by 8 to make a bit count */
235 ctx
->buf
[ctx
->count
++] = 0x80; /* pad */
236 while (ctx
->count
< 56)
237 ctx
->buf
[ctx
->count
++] = 0; /* pad */
241 /* need one extra block */
242 ctx
->buf
[ctx
->count
++] = 0x80; /* pad character */
243 while (ctx
->count
< 64)
244 ctx
->buf
[ctx
->count
++] = 0;
245 sha1_update(ctx
, NULL
, 0); /* flush */
246 memset(ctx
->buf
, 0, 56); /* fill next block with zeroes */
249 /* append the 64 bit count */
250 ctx
->buf
[56] = msb
>> 24;
251 ctx
->buf
[57] = msb
>> 16;
252 ctx
->buf
[58] = msb
>> 8;
254 ctx
->buf
[60] = lsb
>> 24;
255 ctx
->buf
[61] = lsb
>> 16;
256 ctx
->buf
[62] = lsb
>> 8;
258 sha1_transform(ctx
, ctx
->buf
);
261 #define X(a) do { put_u32(p, ctx->h##a); p += 4; } while(0)
278 * Shortcut function which puts the hash value of the supplied buffer
279 * into outbuf which must have a size of 20 bytes.
282 sha1_hash_buffer(byte
*outbuf
, const byte
*buffer
, uint length
)
284 struct sha1_context ctx
;
287 sha1_update(&ctx
, buffer
, length
);
288 memcpy(outbuf
, sha1_final(&ctx
), SHA1_SIZE
);
292 sha1_hmac_init(struct sha1_hmac_context
*ctx
, const byte
*key
, uint keylen
)
294 byte keybuf
[SHA1_BLOCK_SIZE
], buf
[SHA1_BLOCK_SIZE
];
296 /* Hash the key if necessary */
297 if (keylen
<= SHA1_BLOCK_SIZE
)
299 memcpy(keybuf
, key
, keylen
);
300 memset(keybuf
+ keylen
, 0, SHA1_BLOCK_SIZE
- keylen
);
304 sha1_hash_buffer(keybuf
, key
, keylen
);
305 memset(keybuf
+ SHA1_SIZE
, 0, SHA1_BLOCK_SIZE
- SHA1_SIZE
);
308 /* Initialize the inner digest */
309 sha1_init(&ctx
->ictx
);
311 for (i
= 0; i
< SHA1_BLOCK_SIZE
; i
++)
312 buf
[i
] = keybuf
[i
] ^ 0x36;
313 sha1_update(&ctx
->ictx
, buf
, SHA1_BLOCK_SIZE
);
315 /* Initialize the outer digest */
316 sha1_init(&ctx
->octx
);
317 for (i
= 0; i
< SHA1_BLOCK_SIZE
; i
++)
318 buf
[i
] = keybuf
[i
] ^ 0x5c;
319 sha1_update(&ctx
->octx
, buf
, SHA1_BLOCK_SIZE
);
323 sha1_hmac_update(struct sha1_hmac_context
*ctx
, const byte
*data
, uint datalen
)
325 /* Just update the inner digest */
326 sha1_update(&ctx
->ictx
, data
, datalen
);
330 sha1_hmac_final(struct sha1_hmac_context
*ctx
)
332 /* Finish the inner digest */
333 byte
*isha
= sha1_final(&ctx
->ictx
);
335 /* Finish the outer digest */
336 sha1_update(&ctx
->octx
, isha
, SHA1_SIZE
);
337 return sha1_final(&ctx
->octx
);
341 sha1_hmac(byte
*outbuf
, const byte
*key
, uint keylen
, const byte
*data
, uint datalen
)
343 struct sha1_hmac_context ctx
;
345 sha1_hmac_init(&ctx
, key
, keylen
);
346 sha1_hmac_update(&ctx
, data
, datalen
);
347 memcpy(outbuf
, sha1_hmac_final(&ctx
), SHA1_SIZE
);