1 /* 2 * Copyright (c) 2001-2007, Tom St Denis 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 /* LibTomCrypt, modular cryptographic library -- Tom St Denis 29 * 30 * LibTomCrypt is a library that provides various cryptographic 31 * algorithms in a highly modular and flexible manner. 32 * 33 * The library is free for all purposes without any express 34 * guarantee it works. 35 * 36 * Tom St Denis, tomstdenis@gmail.com, http://libtom.org 37 */ 38 #include "tomcrypt.h" 39 40 /** 41 @file sha1.c 42 LTC_SHA1 code by Tom St Denis 43 */ 44 45 46 #ifdef LTC_SHA1 47 48 const struct ltc_hash_descriptor sha1_desc = 49 { 50 "sha1", 51 2, 52 20, 53 64, 54 55 /* OID */ 56 { 1, 3, 14, 3, 2, 26, }, 57 6, 58 59 &sha1_init, 60 &sha1_process, 61 &sha1_done, 62 &sha1_test, 63 NULL 64 }; 65 66 #define F0(x,y,z) (z ^ (x & (y ^ z))) 67 #define F1(x,y,z) (x ^ y ^ z) 68 #define F2(x,y,z) ((x & y) | (z & (x | y))) 69 #define F3(x,y,z) (x ^ y ^ z) 70 71 #ifdef LTC_CLEAN_STACK 72 static int _sha1_compress(hash_state *md, unsigned char *buf) 73 #else 74 static int sha1_compress(hash_state *md, unsigned char *buf) 75 #endif 76 { 77 ulong32 a,b,c,d,e,W[80],i; 78 #ifdef LTC_SMALL_CODE 79 ulong32 t; 80 #endif 81 82 /* copy the state into 512-bits into W[0..15] */ 83 for (i = 0; i < 16; i++) { 84 LOAD32H(W[i], buf + (4*i)); 85 } 86 87 /* copy state */ 88 a = md->sha1.state[0]; 89 b = md->sha1.state[1]; 90 c = md->sha1.state[2]; 91 d = md->sha1.state[3]; 92 e = md->sha1.state[4]; 93 94 /* expand it */ 95 for (i = 16; i < 80; i++) { 96 W[i] = ROL(W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16], 1); 97 } 98 99 /* compress */ 100 /* round one */ 101 #define FF0(a,b,c,d,e,i) e = (ROLc(a, 5) + F0(b,c,d) + e + W[i] + 0x5a827999UL); b = ROLc(b, 30); 102 #define FF1(a,b,c,d,e,i) e = (ROLc(a, 5) + F1(b,c,d) + e + W[i] + 0x6ed9eba1UL); b = ROLc(b, 30); 103 #define FF2(a,b,c,d,e,i) e = (ROLc(a, 5) + F2(b,c,d) + e + W[i] + 0x8f1bbcdcUL); b = ROLc(b, 30); 104 #define FF3(a,b,c,d,e,i) e = (ROLc(a, 5) + F3(b,c,d) + e + W[i] + 0xca62c1d6UL); b = ROLc(b, 30); 105 106 #ifdef LTC_SMALL_CODE 107 108 for (i = 0; i < 20; ) { 109 FF0(a,b,c,d,e,i++); t = e; e = d; d = c; c = b; b = a; a = t; 110 } 111 112 for (; i < 40; ) { 113 FF1(a,b,c,d,e,i++); t = e; e = d; d = c; c = b; b = a; a = t; 114 } 115 116 for (; i < 60; ) { 117 FF2(a,b,c,d,e,i++); t = e; e = d; d = c; c = b; b = a; a = t; 118 } 119 120 for (; i < 80; ) { 121 FF3(a,b,c,d,e,i++); t = e; e = d; d = c; c = b; b = a; a = t; 122 } 123 124 #else 125 126 for (i = 0; i < 20; ) { 127 FF0(a,b,c,d,e,i++); 128 FF0(e,a,b,c,d,i++); 129 FF0(d,e,a,b,c,i++); 130 FF0(c,d,e,a,b,i++); 131 FF0(b,c,d,e,a,i++); 132 } 133 134 /* round two */ 135 for (; i < 40; ) { 136 FF1(a,b,c,d,e,i++); 137 FF1(e,a,b,c,d,i++); 138 FF1(d,e,a,b,c,i++); 139 FF1(c,d,e,a,b,i++); 140 FF1(b,c,d,e,a,i++); 141 } 142 143 /* round three */ 144 for (; i < 60; ) { 145 FF2(a,b,c,d,e,i++); 146 FF2(e,a,b,c,d,i++); 147 FF2(d,e,a,b,c,i++); 148 FF2(c,d,e,a,b,i++); 149 FF2(b,c,d,e,a,i++); 150 } 151 152 /* round four */ 153 for (; i < 80; ) { 154 FF3(a,b,c,d,e,i++); 155 FF3(e,a,b,c,d,i++); 156 FF3(d,e,a,b,c,i++); 157 FF3(c,d,e,a,b,i++); 158 FF3(b,c,d,e,a,i++); 159 } 160 #endif 161 162 #undef FF0 163 #undef FF1 164 #undef FF2 165 #undef FF3 166 167 /* store */ 168 md->sha1.state[0] = md->sha1.state[0] + a; 169 md->sha1.state[1] = md->sha1.state[1] + b; 170 md->sha1.state[2] = md->sha1.state[2] + c; 171 md->sha1.state[3] = md->sha1.state[3] + d; 172 md->sha1.state[4] = md->sha1.state[4] + e; 173 174 return CRYPT_OK; 175 } 176 177 #ifdef LTC_CLEAN_STACK 178 static int sha1_compress(hash_state *md, unsigned char *buf) 179 { 180 int err; 181 err = _sha1_compress(md, buf); 182 burn_stack(sizeof(ulong32) * 87); 183 return err; 184 } 185 #endif 186 187 /** 188 Initialize the hash state 189 @param md The hash state you wish to initialize 190 @return CRYPT_OK if successful 191 */ 192 int sha1_init(hash_state * md) 193 { 194 LTC_ARGCHK(md != NULL); 195 md->sha1.state[0] = 0x67452301UL; 196 md->sha1.state[1] = 0xefcdab89UL; 197 md->sha1.state[2] = 0x98badcfeUL; 198 md->sha1.state[3] = 0x10325476UL; 199 md->sha1.state[4] = 0xc3d2e1f0UL; 200 md->sha1.curlen = 0; 201 md->sha1.length = 0; 202 return CRYPT_OK; 203 } 204 205 /** 206 Process a block of memory though the hash 207 @param md The hash state 208 @param in The data to hash 209 @param inlen The length of the data (octets) 210 @return CRYPT_OK if successful 211 */ 212 HASH_PROCESS(sha1_process, sha1_compress, sha1, 64) 213 214 /** 215 Terminate the hash to get the digest 216 @param md The hash state 217 @param out [out] The destination of the hash (20 bytes) 218 @return CRYPT_OK if successful 219 */ 220 int sha1_done(hash_state * md, unsigned char *out) 221 { 222 int i; 223 224 LTC_ARGCHK(md != NULL); 225 LTC_ARGCHK(out != NULL); 226 227 if (md->sha1.curlen >= sizeof(md->sha1.buf)) { 228 return CRYPT_INVALID_ARG; 229 } 230 231 /* increase the length of the message */ 232 md->sha1.length += md->sha1.curlen * 8; 233 234 /* append the '1' bit */ 235 md->sha1.buf[md->sha1.curlen++] = (unsigned char)0x80; 236 237 /* if the length is currently above 56 bytes we append zeros 238 * then compress. Then we can fall back to padding zeros and length 239 * encoding like normal. 240 */ 241 if (md->sha1.curlen > 56) { 242 while (md->sha1.curlen < 64) { 243 md->sha1.buf[md->sha1.curlen++] = (unsigned char)0; 244 } 245 sha1_compress(md, md->sha1.buf); 246 md->sha1.curlen = 0; 247 } 248 249 /* pad upto 56 bytes of zeroes */ 250 while (md->sha1.curlen < 56) { 251 md->sha1.buf[md->sha1.curlen++] = (unsigned char)0; 252 } 253 254 /* store length */ 255 STORE64H(md->sha1.length, md->sha1.buf+56); 256 sha1_compress(md, md->sha1.buf); 257 258 /* copy output */ 259 for (i = 0; i < 5; i++) { 260 STORE32H(md->sha1.state[i], out+(4*i)); 261 } 262 #ifdef LTC_CLEAN_STACK 263 zeromem(md, sizeof(hash_state)); 264 #endif 265 return CRYPT_OK; 266 } 267 268 /** 269 Self-test the hash 270 @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled 271 */ 272 int sha1_test(void) 273 { 274 #ifndef LTC_TEST 275 return CRYPT_NOP; 276 #else 277 static const struct { 278 const char *msg; 279 unsigned char hash[20]; 280 } tests[] = { 281 { "abc", 282 { 0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a, 283 0xba, 0x3e, 0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c, 284 0x9c, 0xd0, 0xd8, 0x9d } 285 }, 286 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 287 { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 288 0xBA, 0xAE, 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 289 0xE5, 0x46, 0x70, 0xF1 } 290 } 291 }; 292 293 int i; 294 unsigned char tmp[20]; 295 hash_state md; 296 297 for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) { 298 sha1_init(&md); 299 sha1_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg)); 300 sha1_done(&md, tmp); 301 if (XMEMCMP(tmp, tests[i].hash, 20) != 0) { 302 return CRYPT_FAIL_TESTVECTOR; 303 } 304 } 305 return CRYPT_OK; 306 #endif 307 } 308 309 #endif 310 311 312 313 /* $Source: /cvs/libtom/libtomcrypt/src/hashes/sha1.c,v $ */ 314 /* $Revision: 1.10 $ */ 315 /* $Date: 2007/05/12 14:25:28 $ */ 316