4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
33 * \brief SHA-1 Hashing algorithm.
34 * \author Giovanni Bajo <rasky@develer.com>
40 * By Steve Reid <steve@edmweb.com>
44 /* #define LITTLE_ENDIAN * This should be #define'd if true. */
45 /* #define SHA1HANDSOFF * Copies data before messing with it. */
49 #include <cfg/compiler.h>
50 #include <cfg/debug.h>
51 #include <cfg/macros.h>
52 #include <cpu/byteorder.h> // CPU_BYTE_ORDER
57 #define SHA1_BLOCK_LEN 64
58 #define SHA1_DIGEST_LEN 20
60 static void SHA1Transform(uint32_t state[5], const uint8_t buffer[64]);
62 #define rol(value, bits) ROTL(value, bits)
64 /* blk0() and blk() perform the initial expand. */
65 /* I got the idea of expanding during the round function from SSLeay */
66 #define blk0(i) (block[i] = be32_to_cpu(block[i]))
67 #define blk(i) (block[i&15] = rol(block[(i+13)&15]^block[(i+8)&15] \
68 ^block[(i+2)&15]^block[i&15],1))
70 /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
71 #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
72 #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
73 #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
74 #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
75 #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
78 /* Hash a single 512-bit block. This is the core of the algorithm. */
79 static void SHA1Transform(uint32_t state[5], const uint8_t buffer[64])
81 uint32_t a, b, c, d, e;
84 memcpy(&block, buffer, 64);
86 /* Copy context->state[] to working vars */
92 /* 4 rounds of 20 operations each. Loop unrolled. */
173 /* Add the working vars back into context.state[] */
180 a = b = c = d = e = 0;
183 static void SHA1_begin(Hash* h)
185 SHA1_Context *context = (SHA1_Context*)h;
186 /* SHA1 initialization constants */
187 context->state[0] = 0x67452301;
188 context->state[1] = 0xEFCDAB89;
189 context->state[2] = 0x98BADCFE;
190 context->state[3] = 0x10325476;
191 context->state[4] = 0xC3D2E1F0;
192 context->count[0] = context->count[1] = 0;
195 /* Run your data through this. */
197 static void SHA1_update(Hash* h, const void* vdata, size_t len)
199 SHA1_Context *context = (SHA1_Context*)h;
200 const uint8_t *data = (const uint8_t*)vdata;
203 j = (context->count[0] >> 3) & 63;
204 if ((context->count[0] += len << 3) < (len << 3))
206 context->count[1] += (len >> 29);
207 if ((j + len) > 63) {
208 memcpy(&context->buffer[j], data, (i = 64-j));
209 SHA1Transform(context->state, context->buffer);
210 for ( ; i + 63 < len; i += 64) {
211 SHA1Transform(context->state, &data[i]);
216 memcpy(&context->buffer[j], &data[i], len - i);
220 /* Add padding and return the message digest. */
222 static uint8_t *SHA1_final(Hash* h)
224 SHA1_Context *context = (SHA1_Context*)h;
226 uint8_t finalcount[8];
228 for (i = 0; i < 8; i++)
229 finalcount[i] = (uint8_t)((context->count[(i >= 4 ? 0 : 1)]
230 >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */
232 SHA1_update(h, "\200", 1);
233 while ((context->count[0] & 504) != 448)
234 SHA1_update(h, "\0", 1);
235 SHA1_update(h, finalcount, 8); /* Should cause a SHA1Transform() */
237 for (i = 0; i < 20; i++)
238 context->buffer[i] = (uint8_t)
239 ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
243 return context->buffer;
247 /*************************************************************/
249 void SHA1_init(SHA1_Context* ctx)
251 ctx->h.block_len = SHA1_BLOCK_LEN;
252 ctx->h.digest_len = SHA1_DIGEST_LEN;
253 ctx->h.begin = SHA1_begin;
254 ctx->h.update = SHA1_update;
255 ctx->h.final = SHA1_final;