libdrizzle Developer Documentation

sha1.c
Go to the documentation of this file.
1 
6 /*
7  * SHA-1 in C
8  * By Steve Reid <steve@edmweb.com>
9  * 100% Public Domain
10  *
11  * Test Vectors (from FIPS PUB 180-1)
12  * "abc"
13  * A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
14  * "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
15  * 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
16  * A million repetitions of "a"
17  * 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
18  */
19 
20 #include "common.h"
21 
22 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
23 
24 /* Solaris + gcc don't always define this. */
25 #ifndef BYTE_ORDER
26 # define LITTLE_ENDIAN 1234
27 # define BIG_ENDIAN 4321
28 # if defined(sparc) || defined(__sparc) || defined(__sparc__)
29 # define BYTE_ORDER BIG_ENDIAN
30 # endif
31 #endif /* BYTE_ORDER */
32 
33 /*
34  * blk0() and blk() perform the initial expand.
35  * I got the idea of expanding during the round function from SSLeay
36  */
37 #if BYTE_ORDER == LITTLE_ENDIAN
38 # define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
39  |(rol(block->l[i],8)&0x00FF00FF))
40 #else
41 # define blk0(i) block->l[i]
42 #endif
43 #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
44  ^block->l[(i+2)&15]^block->l[i&15],1))
45 
46 /*
47  * (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1
48  */
49 #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
50 #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
51 #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
52 #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
53 #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
54 
55 /*
56  * Hash a single 512-bit block. This is the core of the algorithm.
57  */
58 void
59 SHA1Transform(uint32_t state[5], const uint8_t buffer[SHA1_BLOCK_LENGTH])
60 {
61  uint32_t a, b, c, d, e;
62  typedef union {
63  uint8_t c[64];
64  uint32_t l[16];
65  } CHAR64LONG16;
66  CHAR64LONG16 realBlock;
67  CHAR64LONG16 *block= &realBlock;
68 
69  (void)memcpy(block, buffer, SHA1_BLOCK_LENGTH);
70 
71  /* Copy context->state[] to working vars */
72  a = state[0];
73  b = state[1];
74  c = state[2];
75  d = state[3];
76  e = state[4];
77 
78  /* 4 rounds of 20 operations each. Loop unrolled. */
79  R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);
80  R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
81  R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);
82  R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);
83  R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
84  R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
85  R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
86  R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
87  R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
88  R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
89  R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
90  R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
91  R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
92  R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
93  R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
94  R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
95  R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
96  R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
97  R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
98  R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
99 
100  /* Add the working vars back into context.state[] */
101  state[0] += a;
102  state[1] += b;
103  state[2] += c;
104  state[3] += d;
105  state[4] += e;
106 
107  /* Wipe variables */
108  a = b = c = d = e = 0;
109 }
110 
111 
112 /*
113  * SHA1Init - Initialize new context
114  */
115 void
117 {
118 
119  /* SHA1 initialization constants */
120  context->count = 0;
121  context->state[0] = 0x67452301;
122  context->state[1] = 0xEFCDAB89;
123  context->state[2] = 0x98BADCFE;
124  context->state[3] = 0x10325476;
125  context->state[4] = 0xC3D2E1F0;
126 }
127 
128 
129 /*
130  * Run your data through this.
131  */
132 void
133 SHA1Update(SHA1_CTX *context, const uint8_t *data, size_t len)
134 {
135  size_t i, j;
136 
137  j = (size_t)((context->count >> 3) & 63);
138  context->count += (len << 3);
139  if ((j + len) > 63) {
140  (void)memcpy(&context->buffer[j], data, (i = 64-j));
141  SHA1Transform(context->state, context->buffer);
142  for ( ; i + 63 < len; i += 64)
143  SHA1Transform(context->state, (uint8_t *)&data[i]);
144  j = 0;
145  } else {
146  i = 0;
147  }
148  (void)memcpy(&context->buffer[j], &data[i], len - i);
149 }
150 
151 
152 /*
153  * Add padding and return the message digest.
154  */
155 void
156 SHA1Pad(SHA1_CTX *context)
157 {
158  uint8_t finalcount[8];
159  u_int i;
160 
161  for (i = 0; i < 8; i++) {
162  finalcount[i] = (uint8_t)((context->count >>
163  ((7 - (i & 7)) * 8)) & 255); /* Endian independent */
164  }
165  SHA1Update(context, (uint8_t *)"\200", 1);
166  while ((context->count & 504) != 448)
167  SHA1Update(context, (uint8_t *)"\0", 1);
168  SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */
169 }
170 
171 void
172 SHA1Final(uint8_t digest[SHA1_DIGEST_LENGTH], SHA1_CTX *context)
173 {
174  u_int i;
175 
176  SHA1Pad(context);
177  if (digest) {
178  for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
179  digest[i] = (uint8_t)
180  ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
181  }
182  memset(context, 0, sizeof(*context));
183  }
184 }