1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2010 Amaury Pouly
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 /* Based on http://en.wikipedia.org/wiki/SHA-1 */
24 static uint32_t rot_left(uint32_t val
, int rot
)
26 return (val
<< rot
) | (val
>> (32 - rot
));
29 static inline void byte_swapxx(byte
*ptr
, int size
)
31 for(int i
= 0; i
< size
/ 2; i
++)
34 ptr
[i
] = ptr
[size
- i
- 1];
35 ptr
[size
- i
- 1] = c
;
39 static void byte_swap32(uint32_t *v
)
41 byte_swapxx((byte
*)v
, 4);
44 void sha_1_init(struct sha_1_params_t
*params
)
46 params
->hash
[0] = 0x67452301;
47 params
->hash
[1] = 0xEFCDAB89;
48 params
->hash
[2] = 0x98BADCFE;
49 params
->hash
[3] = 0x10325476;
50 params
->hash
[4] = 0xC3D2E1F0;
51 params
->buffer_nr_bits
= 0;
54 void sha_1_update(struct sha_1_params_t
*params
, byte
*buffer
, int size
)
56 int buffer_nr_bytes
= (params
->buffer_nr_bits
/ 8) % 64;
57 params
->buffer_nr_bits
+= 8 * size
;
59 if(buffer_nr_bytes
+ size
>= 64)
61 pos
= 64 - buffer_nr_bytes
;
62 memcpy((byte
*)(params
->w
) + buffer_nr_bytes
, buffer
, 64 - buffer_nr_bytes
);
63 sha_1_block(params
, params
->hash
, (byte
*)params
->w
);
64 for(; pos
+ 64 <= size
; pos
+= 64)
65 sha_1_block(params
, params
->hash
, buffer
+ pos
);
68 memcpy((byte
*)(params
->w
) + buffer_nr_bytes
, buffer
+ pos
, size
- pos
);
71 void sha_1_finish(struct sha_1_params_t
*params
)
73 /* length (in bits) in big endian BEFORE preprocessing */
74 byte length_big_endian
[8];
75 memcpy(length_big_endian
, ¶ms
->buffer_nr_bits
, 8);
76 byte_swapxx(length_big_endian
, 8);
77 /* append '1' and then '0's to the message to get 448 bit length for the last block */
79 sha_1_update(params
, &b
, 1);
81 while((params
->buffer_nr_bits
% 512) != 448)
82 sha_1_update(params
, &b
, 1);
84 sha_1_update(params
, length_big_endian
, 8);
85 /* go back to big endian */
86 for(int i
= 0; i
< 5; i
++)
87 byte_swap32(¶ms
->hash
[i
]);
90 void sha_1_output(struct sha_1_params_t
*params
, byte
*out
)
92 memcpy(out
, params
->hash
, 20);
95 void sha_1_block(struct sha_1_params_t
*params
, uint32_t cur_hash
[5], byte
*data
)
97 uint32_t a
, b
, c
, d
, e
;
107 for(int i
= 0; i
< 16; i
++)
110 for(int i
= 16; i
<= 79; i
++)
111 w
[i
] = rot_left(w
[i
- 3] ^ w
[i
- 8] ^ w
[i
- 14] ^ w
[i
- 16], 1);
113 for(int i
= 0; i
<= 79; i
++)
118 f
= (b
& c
) | ((~b
) & d
);
128 f
= (b
& c
) | (b
& d
) | (c
& d
);
136 uint32_t temp
= rot_left(a
, 5) + f
+ e
+ k
+ w
[i
];