blob: 0ad05bb5cd12cd72a896befe5371ac95ceffe7f7 [file] [log] [blame]
Amaury Pouly76125592010-11-29 14:15:06 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2010 Amaury Pouly
11 *
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.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21/* Based on http://en.wikipedia.org/wiki/SHA-1 */
22#include "crypto.h"
23
24static uint32_t rot_left(uint32_t val, int rot)
25{
26 return (val << rot) | (val >> (32 - rot));
27}
28
29static inline void byte_swapxx(byte *ptr, int size)
30{
31 for(int i = 0; i < size / 2; i++)
32 {
33 byte c = ptr[i];
34 ptr[i] = ptr[size - i - 1];
35 ptr[size - i - 1] = c;
36 }
37}
38
39static void byte_swap32(uint32_t *v)
40{
41 byte_swapxx((byte *)v, 4);
42}
43
44void sha_1_init(struct sha_1_params_t *params)
45{
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;
52}
53
54void sha_1_update(struct sha_1_params_t *params, byte *buffer, int size)
55{
56 int buffer_nr_bytes = (params->buffer_nr_bits / 8) % 64;
57 params->buffer_nr_bits += 8 * size;
58 int pos = 0;
59 if(buffer_nr_bytes + size >= 64)
60 {
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);
66 buffer_nr_bytes = 0;
67 }
68 memcpy((byte *)(params->w) + buffer_nr_bytes, buffer + pos, size - pos);
69}
70
71void sha_1_finish(struct sha_1_params_t *params)
72{
73 /* length (in bits) in big endian BEFORE preprocessing */
74 byte length_big_endian[8];
75 memcpy(length_big_endian, &params->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 */
78 byte b = 0x80;
79 sha_1_update(params, &b, 1);
80 b = 0;
81 while((params->buffer_nr_bits % 512) != 448)
82 sha_1_update(params, &b, 1);
83 /* append length */
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(&params->hash[i]);
88}
89
90void sha_1_output(struct sha_1_params_t *params, byte *out)
91{
92 memcpy(out, params->hash, 20);
93}
94
95void sha_1_block(struct sha_1_params_t *params, uint32_t cur_hash[5], byte *data)
96{
97 uint32_t a, b, c, d, e;
98 a = cur_hash[0];
99 b = cur_hash[1];
100 c = cur_hash[2];
101 d = cur_hash[3];
102 e = cur_hash[4];
103
104 #define w params->w
105
Amaury Poulybdb8f4c2011-05-13 12:20:49 +0000106 memmove(w, data, 64);
Amaury Pouly76125592010-11-29 14:15:06 +0000107 for(int i = 0; i < 16; i++)
108 byte_swap32(&w[i]);
109
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);
112
113 for(int i = 0; i<= 79; i++)
114 {
115 uint32_t f, k;
116 if(i <= 19)
117 {
118 f = (b & c) | ((~b) & d);
119 k = 0x5A827999;
120 }
121 else if(i <= 39)
122 {
123 f = b ^ c ^ d;
124 k = 0x6ED9EBA1;
125 }
126 else if(i <= 59)
127 {
128 f = (b & c) | (b & d) | (c & d);
129 k = 0x8F1BBCDC;
130 }
131 else
132 {
133 f = b ^ c ^ d;
134 k = 0xCA62C1D6;
135 }
136 uint32_t temp = rot_left(a, 5) + f + e + k + w[i];
137 e = d;
138 d = c;
139 c = rot_left(b, 30);
140 b = a;
141 a = temp;
142 }
143 #undef w
144
145 cur_hash[0] += a;
146 cur_hash[1] += b;
147 cur_hash[2] += c;
148 cur_hash[3] += d;
149 cur_hash[4] += e;
150}