Ruby 1.9.3p327(2012-11-10revision37606)
|
00001 /* $NetBSD: sha1.c,v 1.2 2001/03/22 09:51:48 agc Exp $ */ 00002 /* $OpenBSD: sha1.c,v 1.9 1997/07/23 21:12:32 kstailey Exp $ */ 00003 /* $RoughId: sha1.c,v 1.2 2001/07/13 19:49:10 knu Exp $ */ 00004 /* $Id: sha1.c 25189 2009-10-02 12:04:37Z akr $ */ 00005 00006 /* 00007 * SHA-1 in C 00008 * By Steve Reid <steve@edmweb.com> 00009 * 100% Public Domain 00010 * 00011 * Test Vectors (from FIPS PUB 180-1) 00012 * "abc" 00013 * A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D 00014 * "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" 00015 * 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 00016 * A million repetitions of "a" 00017 * 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F 00018 */ 00019 00020 #include "sha1.h" 00021 00022 #define SHA1HANDSOFF /* Copies data before messing with it. */ 00023 00024 #if defined(_KERNEL) || defined(_STANDALONE) 00025 #include <sys/param.h> 00026 #include <sys/systm.h> 00027 #define _DIAGASSERT(x) (void)0 00028 #else 00029 /* #include "namespace.h" */ 00030 #include <assert.h> 00031 #include <string.h> 00032 #endif 00033 00034 #ifndef _DIAGASSERT 00035 #define _DIAGASSERT(cond) assert(cond) 00036 #endif 00037 00038 /* 00039 * XXX Kludge until there is resolution regarding mem*() functions 00040 * XXX in the kernel. 00041 */ 00042 #if defined(_KERNEL) || defined(_STANDALONE) 00043 #define memcpy(s, d, l) bcopy((d), (s), (l)) 00044 #endif 00045 00046 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) 00047 00048 /* 00049 * blk0() and blk() perform the initial expand. 00050 * I got the idea of expanding during the round function from SSLeay 00051 */ 00052 #ifndef WORDS_BIGENDIAN 00053 # define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \ 00054 |(rol(block->l[i],8)&0x00FF00FF)) 00055 #else 00056 # define blk0(i) block->l[i] 00057 #endif 00058 #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \ 00059 ^block->l[(i+2)&15]^block->l[i&15],1)) 00060 00061 /* 00062 * (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1 00063 */ 00064 #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30); 00065 #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30); 00066 #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30); 00067 #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30); 00068 #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30); 00069 00070 00071 typedef union { 00072 uint8_t c[64]; 00073 uint32_t l[16]; 00074 } CHAR64LONG16; 00075 00076 #ifdef __sparc_v9__ 00077 void do_R01(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *); 00078 void do_R2(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *); 00079 void do_R3(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *); 00080 void do_R4(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *); 00081 00082 #define nR0(v,w,x,y,z,i) R0(*v,*w,*x,*y,*z,i) 00083 #define nR1(v,w,x,y,z,i) R1(*v,*w,*x,*y,*z,i) 00084 #define nR2(v,w,x,y,z,i) R2(*v,*w,*x,*y,*z,i) 00085 #define nR3(v,w,x,y,z,i) R3(*v,*w,*x,*y,*z,i) 00086 #define nR4(v,w,x,y,z,i) R4(*v,*w,*x,*y,*z,i) 00087 00088 void 00089 do_R01(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *block) 00090 { 00091 nR0(a,b,c,d,e, 0); nR0(e,a,b,c,d, 1); nR0(d,e,a,b,c, 2); nR0(c,d,e,a,b, 3); 00092 nR0(b,c,d,e,a, 4); nR0(a,b,c,d,e, 5); nR0(e,a,b,c,d, 6); nR0(d,e,a,b,c, 7); 00093 nR0(c,d,e,a,b, 8); nR0(b,c,d,e,a, 9); nR0(a,b,c,d,e,10); nR0(e,a,b,c,d,11); 00094 nR0(d,e,a,b,c,12); nR0(c,d,e,a,b,13); nR0(b,c,d,e,a,14); nR0(a,b,c,d,e,15); 00095 nR1(e,a,b,c,d,16); nR1(d,e,a,b,c,17); nR1(c,d,e,a,b,18); nR1(b,c,d,e,a,19); 00096 } 00097 00098 void 00099 do_R2(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *block) 00100 { 00101 nR2(a,b,c,d,e,20); nR2(e,a,b,c,d,21); nR2(d,e,a,b,c,22); nR2(c,d,e,a,b,23); 00102 nR2(b,c,d,e,a,24); nR2(a,b,c,d,e,25); nR2(e,a,b,c,d,26); nR2(d,e,a,b,c,27); 00103 nR2(c,d,e,a,b,28); nR2(b,c,d,e,a,29); nR2(a,b,c,d,e,30); nR2(e,a,b,c,d,31); 00104 nR2(d,e,a,b,c,32); nR2(c,d,e,a,b,33); nR2(b,c,d,e,a,34); nR2(a,b,c,d,e,35); 00105 nR2(e,a,b,c,d,36); nR2(d,e,a,b,c,37); nR2(c,d,e,a,b,38); nR2(b,c,d,e,a,39); 00106 } 00107 00108 void 00109 do_R3(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *block) 00110 { 00111 nR3(a,b,c,d,e,40); nR3(e,a,b,c,d,41); nR3(d,e,a,b,c,42); nR3(c,d,e,a,b,43); 00112 nR3(b,c,d,e,a,44); nR3(a,b,c,d,e,45); nR3(e,a,b,c,d,46); nR3(d,e,a,b,c,47); 00113 nR3(c,d,e,a,b,48); nR3(b,c,d,e,a,49); nR3(a,b,c,d,e,50); nR3(e,a,b,c,d,51); 00114 nR3(d,e,a,b,c,52); nR3(c,d,e,a,b,53); nR3(b,c,d,e,a,54); nR3(a,b,c,d,e,55); 00115 nR3(e,a,b,c,d,56); nR3(d,e,a,b,c,57); nR3(c,d,e,a,b,58); nR3(b,c,d,e,a,59); 00116 } 00117 00118 void 00119 do_R4(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *block) 00120 { 00121 nR4(a,b,c,d,e,60); nR4(e,a,b,c,d,61); nR4(d,e,a,b,c,62); nR4(c,d,e,a,b,63); 00122 nR4(b,c,d,e,a,64); nR4(a,b,c,d,e,65); nR4(e,a,b,c,d,66); nR4(d,e,a,b,c,67); 00123 nR4(c,d,e,a,b,68); nR4(b,c,d,e,a,69); nR4(a,b,c,d,e,70); nR4(e,a,b,c,d,71); 00124 nR4(d,e,a,b,c,72); nR4(c,d,e,a,b,73); nR4(b,c,d,e,a,74); nR4(a,b,c,d,e,75); 00125 nR4(e,a,b,c,d,76); nR4(d,e,a,b,c,77); nR4(c,d,e,a,b,78); nR4(b,c,d,e,a,79); 00126 } 00127 #endif 00128 00129 /* 00130 * Hash a single 512-bit block. This is the core of the algorithm. 00131 */ 00132 void SHA1_Transform(uint32_t state[5], const uint8_t buffer[64]) 00133 { 00134 uint32_t a, b, c, d, e; 00135 CHAR64LONG16 *block; 00136 00137 #ifdef SHA1HANDSOFF 00138 CHAR64LONG16 workspace; 00139 #endif 00140 00141 _DIAGASSERT(buffer != 0); 00142 _DIAGASSERT(state != 0); 00143 00144 #ifdef SHA1HANDSOFF 00145 block = &workspace; 00146 (void)memcpy(block, buffer, 64); 00147 #else 00148 block = (CHAR64LONG16 *)(void *)buffer; 00149 #endif 00150 00151 /* Copy context->state[] to working vars */ 00152 a = state[0]; 00153 b = state[1]; 00154 c = state[2]; 00155 d = state[3]; 00156 e = state[4]; 00157 00158 #ifdef __sparc_v9__ 00159 do_R01(&a, &b, &c, &d, &e, block); 00160 do_R2(&a, &b, &c, &d, &e, block); 00161 do_R3(&a, &b, &c, &d, &e, block); 00162 do_R4(&a, &b, &c, &d, &e, block); 00163 #else 00164 /* 4 rounds of 20 operations each. Loop unrolled. */ 00165 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); 00166 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); 00167 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); 00168 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); 00169 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); 00170 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); 00171 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); 00172 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); 00173 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); 00174 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); 00175 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); 00176 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); 00177 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); 00178 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); 00179 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); 00180 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); 00181 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); 00182 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); 00183 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); 00184 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); 00185 #endif 00186 00187 /* Add the working vars back into context.state[] */ 00188 state[0] += a; 00189 state[1] += b; 00190 state[2] += c; 00191 state[3] += d; 00192 state[4] += e; 00193 00194 /* Wipe variables */ 00195 a = b = c = d = e = 0; 00196 } 00197 00198 00199 /* 00200 * SHA1_Init - Initialize new context 00201 */ 00202 void SHA1_Init(SHA1_CTX *context) 00203 { 00204 00205 _DIAGASSERT(context != 0); 00206 00207 /* SHA1 initialization constants */ 00208 context->state[0] = 0x67452301; 00209 context->state[1] = 0xEFCDAB89; 00210 context->state[2] = 0x98BADCFE; 00211 context->state[3] = 0x10325476; 00212 context->state[4] = 0xC3D2E1F0; 00213 context->count[0] = context->count[1] = 0; 00214 } 00215 00216 00217 /* 00218 * Run your data through this. 00219 */ 00220 void SHA1_Update(SHA1_CTX *context, const uint8_t *data, size_t len) 00221 { 00222 uint32_t i, j; 00223 00224 _DIAGASSERT(context != 0); 00225 _DIAGASSERT(data != 0); 00226 00227 j = context->count[0]; 00228 if ((context->count[0] += len << 3) < j) 00229 context->count[1] += (len>>29)+1; 00230 j = (j >> 3) & 63; 00231 if ((j + len) > 63) { 00232 (void)memcpy(&context->buffer[j], data, (i = 64-j)); 00233 SHA1_Transform(context->state, context->buffer); 00234 for ( ; i + 63 < len; i += 64) 00235 SHA1_Transform(context->state, &data[i]); 00236 j = 0; 00237 } else { 00238 i = 0; 00239 } 00240 (void)memcpy(&context->buffer[j], &data[i], len - i); 00241 } 00242 00243 00244 /* 00245 * Add padding and return the message digest. 00246 */ 00247 void SHA1_Finish(SHA1_CTX* context, uint8_t digest[20]) 00248 { 00249 size_t i; 00250 uint8_t finalcount[8]; 00251 00252 _DIAGASSERT(digest != 0); 00253 _DIAGASSERT(context != 0); 00254 00255 for (i = 0; i < 8; i++) { 00256 finalcount[i] = (uint8_t)((context->count[(i >= 4 ? 0 : 1)] 00257 >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */ 00258 } 00259 SHA1_Update(context, (const uint8_t *)"\200", 1); 00260 while ((context->count[0] & 504) != 448) 00261 SHA1_Update(context, (const uint8_t *)"\0", 1); 00262 SHA1_Update(context, finalcount, 8); /* Should cause a SHA1_Transform() */ 00263 00264 if (digest) { 00265 for (i = 0; i < 20; i++) 00266 digest[i] = (uint8_t) 00267 ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); 00268 } 00269 } 00270