2 * SHA transform algorithm, originally taken from code written by
3 * Peter Gutmann, and placed in the public domain.
7 rol32(uint32_t word
, int shift
)
9 return (word
<< shift
) | (word
>> (32 - shift
));
12 /* The SHA f()-functions. */
14 #define f1(x,y,z) (z ^ (x & (y ^ z))) /* x ? y : z */
15 #define f2(x,y,z) (x ^ y ^ z) /* XOR */
16 #define f3(x,y,z) ((x & y) + (z & (x ^ y))) /* majority */
18 /* The SHA Mysterious Constants */
20 #define K1 0x5A827999L /* Rounds 0-19: sqrt(2) * 2^30 */
21 #define K2 0x6ED9EBA1L /* Rounds 20-39: sqrt(3) * 2^30 */
22 #define K3 0x8F1BBCDCL /* Rounds 40-59: sqrt(5) * 2^30 */
23 #define K4 0xCA62C1D6L /* Rounds 60-79: sqrt(10) * 2^30 */
26 * sha_transform - single block SHA1 transform
28 * @digest: 160 bit digest to update
29 * @data: 512 bits of data to hash
30 * @W: 80 words of workspace (see note)
32 * This function generates a SHA1 digest for a single 512-bit block.
33 * Be warned, it does not handle padding and message digest, do not
34 * confuse it with the full FIPS 180-1 digest algorithm for variable
37 * Note: If the hash is security sensitive, the caller should be sure
38 * to clear the workspace. This is left to the caller to avoid
39 * unnecessary clears between chained hashing operations.
41 static void sha_transform(uint32_t *digest
, const unsigned char *in
, uint32_t *W
)
43 uint32_t a
, b
, c
, d
, e
, t
, i
;
45 for (i
= 0; i
< 16; i
++) {
48 /* word load/store may be unaligned here, so use bytes instead */
56 for (i
= 0; i
< 64; i
++)
57 W
[i
+16] = rol32(W
[i
+13] ^ W
[i
+8] ^ W
[i
+2] ^ W
[i
], 1);
65 for (i
= 0; i
< 20; i
++) {
66 t
= f1(b
, c
, d
) + K1
+ rol32(a
, 5) + e
+ W
[i
];
67 e
= d
; d
= c
; c
= rol32(b
, 30); b
= a
; a
= t
;
70 for (; i
< 40; i
++) {
71 t
= f2(b
, c
, d
) + K2
+ rol32(a
, 5) + e
+ W
[i
];
72 e
= d
; d
= c
; c
= rol32(b
, 30); b
= a
; a
= t
;
75 for (; i
< 60; i
++) {
76 t
= f3(b
, c
, d
) + K3
+ rol32(a
, 5) + e
+ W
[i
];
77 e
= d
; d
= c
; c
= rol32(b
, 30); b
= a
; a
= t
;
80 for (; i
< 80; i
++) {
81 t
= f2(b
, c
, d
) + K4
+ rol32(a
, 5) + e
+ W
[i
];
82 e
= d
; d
= c
; c
= rol32(b
, 30); b
= a
; a
= t
;
93 * sha_init - initialize the vectors for a SHA1 digest
94 * @buf: vector to initialize
96 static void sha_init(uint32_t *buf
)
This page took 0.067191 seconds and 5 git commands to generate.