18d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/* 28d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * SHA1 hash implementation and interface functions 38d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi> 48d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * 5c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt * This software may be distributed under the terms of the BSD license. 6c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt * See README for more details. 78d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 88d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 98d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "includes.h" 108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "common.h" 128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "sha1.h" 138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "sha1_i.h" 148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "md5.h" 158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "crypto.h" 168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidttypedef struct SHA1Context SHA1_CTX; 188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtvoid SHA1Transform(u32 state[5], const unsigned char buffer[64]); 208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/** 238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * sha1_vector - SHA-1 hash for data vector 248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @num_elem: Number of elements in the data vector 258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @addr: Pointers to the data areas 268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @len: Lengths of the data blocks 278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @mac: Buffer for the hash 288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Returns: 0 on success, -1 of failure 298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtint sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac) 318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt SHA1_CTX ctx; 338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt size_t i; 348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt SHA1Init(&ctx); 368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt for (i = 0; i < num_elem; i++) 378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt SHA1Update(&ctx, addr[i], len[i]); 388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt SHA1Final(mac, &ctx); 398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return 0; 408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/* ===== start - public domain SHA1 implementation ===== */ 448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/* 468d520ff1dc2da35cdca849e982051b86468016d8Dmitry ShmidtSHA-1 in C 478d520ff1dc2da35cdca849e982051b86468016d8Dmitry ShmidtBy Steve Reid <sreid@sea-to-sky.net> 488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt100% Public Domain 498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt----------------- 518d520ff1dc2da35cdca849e982051b86468016d8Dmitry ShmidtModified 7/98 528d520ff1dc2da35cdca849e982051b86468016d8Dmitry ShmidtBy James H. Brown <jbrown@burgoyne.com> 538d520ff1dc2da35cdca849e982051b86468016d8Dmitry ShmidtStill 100% Public Domain 548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 558d520ff1dc2da35cdca849e982051b86468016d8Dmitry ShmidtCorrected a problem which generated improper hash values on 16 bit machines 568d520ff1dc2da35cdca849e982051b86468016d8Dmitry ShmidtRoutine SHA1Update changed from 578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt void SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned int 588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtlen) 598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtto 608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt void SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned 618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtlong len) 628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 638d520ff1dc2da35cdca849e982051b86468016d8Dmitry ShmidtThe 'len' parameter was declared an int which works fine on 32 bit machines. 648d520ff1dc2da35cdca849e982051b86468016d8Dmitry ShmidtHowever, on 16 bit machines an int is too small for the shifts being done 658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtagainst 668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtit. This caused the hash function to generate incorrect values if len was 678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtgreater than 8191 (8K - 1) due to the 'len << 3' on line 3 of SHA1Update(). 688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 698d520ff1dc2da35cdca849e982051b86468016d8Dmitry ShmidtSince the file IO in main() reads 16K at a time, any file 8K or larger would 708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtbe guaranteed to generate the wrong hash (e.g. Test Vector #3, a million 718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt"a"s). 728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 738d520ff1dc2da35cdca849e982051b86468016d8Dmitry ShmidtI also changed the declaration of variables i & j in SHA1Update to 748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtunsigned long from unsigned int for the same reason. 758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 768d520ff1dc2da35cdca849e982051b86468016d8Dmitry ShmidtThese changes should make no difference to any 32 bit implementations since 778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtan 788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtint and a long are the same size in those environments. 798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt-- 818d520ff1dc2da35cdca849e982051b86468016d8Dmitry ShmidtI also corrected a few compiler warnings generated by Borland C. 828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt1. Added #include <process.h> for exit() prototype 838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt2. Removed unused variable 'j' in SHA1Final 848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt3. Changed exit(0) to return(0) at end of main. 858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 868d520ff1dc2da35cdca849e982051b86468016d8Dmitry ShmidtALL changes I made can be located by searching for comments containing 'JHB' 878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt----------------- 888d520ff1dc2da35cdca849e982051b86468016d8Dmitry ShmidtModified 8/98 898d520ff1dc2da35cdca849e982051b86468016d8Dmitry ShmidtBy Steve Reid <sreid@sea-to-sky.net> 908d520ff1dc2da35cdca849e982051b86468016d8Dmitry ShmidtStill 100% public domain 918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt1- Removed #include <process.h> and used return() instead of exit() 938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt2- Fixed overwriting of finalcount in SHA1Final() (discovered by Chris Hall) 948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt3- Changed email address from steve@edmweb.com to sreid@sea-to-sky.net 958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt----------------- 978d520ff1dc2da35cdca849e982051b86468016d8Dmitry ShmidtModified 4/01 988d520ff1dc2da35cdca849e982051b86468016d8Dmitry ShmidtBy Saul Kravitz <Saul.Kravitz@celera.com> 998d520ff1dc2da35cdca849e982051b86468016d8Dmitry ShmidtStill 100% PD 1008d520ff1dc2da35cdca849e982051b86468016d8Dmitry ShmidtModified to run on Compaq Alpha hardware. 1018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt----------------- 1038d520ff1dc2da35cdca849e982051b86468016d8Dmitry ShmidtModified 4/01 1048d520ff1dc2da35cdca849e982051b86468016d8Dmitry ShmidtBy Jouni Malinen <j@w1.fi> 1058d520ff1dc2da35cdca849e982051b86468016d8Dmitry ShmidtMinor changes to match the coding style used in Dynamics. 1068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1078d520ff1dc2da35cdca849e982051b86468016d8Dmitry ShmidtModified September 24, 2004 1088d520ff1dc2da35cdca849e982051b86468016d8Dmitry ShmidtBy Jouni Malinen <j@w1.fi> 1098d520ff1dc2da35cdca849e982051b86468016d8Dmitry ShmidtFixed alignment issue in SHA1Transform when SHA1HANDSOFF is defined. 1108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt*/ 1128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/* 1148d520ff1dc2da35cdca849e982051b86468016d8Dmitry ShmidtTest Vectors (from FIPS PUB 180-1) 1158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt"abc" 1168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D 1178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" 1188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 1198d520ff1dc2da35cdca849e982051b86468016d8Dmitry ShmidtA million repetitions of "a" 1208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F 1218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt*/ 1228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define SHA1HANDSOFF 1248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) 1268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/* blk0() and blk() perform the initial expand. */ 1288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/* I got the idea of expanding during the round function from SSLeay */ 1298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifndef WORDS_BIGENDIAN 1308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define blk0(i) (block->l[i] = (rol(block->l[i], 24) & 0xFF00FF00) | \ 1318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt (rol(block->l[i], 8) & 0x00FF00FF)) 1328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#else 1338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define blk0(i) block->l[i] 1348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif 1358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define blk(i) (block->l[i & 15] = rol(block->l[(i + 13) & 15] ^ \ 1368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt block->l[(i + 8) & 15] ^ block->l[(i + 2) & 15] ^ block->l[i & 15], 1)) 1378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */ 1398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define R0(v,w,x,y,z,i) \ 1408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt z += ((w & (x ^ y)) ^ y) + blk0(i) + 0x5A827999 + rol(v, 5); \ 1418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt w = rol(w, 30); 1428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define R1(v,w,x,y,z,i) \ 1438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt z += ((w & (x ^ y)) ^ y) + blk(i) + 0x5A827999 + rol(v, 5); \ 1448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt w = rol(w, 30); 1458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define R2(v,w,x,y,z,i) \ 1468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt z += (w ^ x ^ y) + blk(i) + 0x6ED9EBA1 + rol(v, 5); w = rol(w, 30); 1478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define R3(v,w,x,y,z,i) \ 1488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt z += (((w | x) & y) | (w & x)) + blk(i) + 0x8F1BBCDC + rol(v, 5); \ 1498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt w = rol(w, 30); 1508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define R4(v,w,x,y,z,i) \ 1518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt z += (w ^ x ^ y) + blk(i) + 0xCA62C1D6 + rol(v, 5); \ 1528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt w=rol(w, 30); 1538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef VERBOSE /* SAK */ 1568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtvoid SHAPrintContext(SHA1_CTX *context, char *msg) 1578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 1588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt printf("%s (%d,%d) %x %x %x %x %x\n", 1598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt msg, 1608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt context->count[0], context->count[1], 1618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt context->state[0], 1628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt context->state[1], 1638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt context->state[2], 1648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt context->state[3], 1658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt context->state[4]); 1668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 1678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif 1688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/* Hash a single 512-bit block. This is the core of the algorithm. */ 1708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtvoid SHA1Transform(u32 state[5], const unsigned char buffer[64]) 1728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 1738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt u32 a, b, c, d, e; 1748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt typedef union { 1758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt unsigned char c[64]; 1768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt u32 l[16]; 1778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } CHAR64LONG16; 1788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt CHAR64LONG16* block; 1798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef SHA1HANDSOFF 1808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt CHAR64LONG16 workspace; 1818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt block = &workspace; 1828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_memcpy(block, buffer, 64); 1838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#else 1848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt block = (CHAR64LONG16 *) buffer; 1858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif 1868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* Copy context->state[] to working vars */ 1878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt a = state[0]; 1888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt b = state[1]; 1898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt c = state[2]; 1908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt d = state[3]; 1918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt e = state[4]; 1928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* 4 rounds of 20 operations each. Loop unrolled. */ 1938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 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); 1948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 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); 1958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 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); 1968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 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); 1978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 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); 1988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 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); 1998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 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); 2008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 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); 2018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 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); 2028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 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); 2038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 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); 2048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 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); 2058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 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); 2068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 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); 2078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 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); 2088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 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); 2098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 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); 2108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 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); 2118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 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); 2128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 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); 2138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* Add the working vars back into context.state[] */ 2148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt state[0] += a; 2158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt state[1] += b; 2168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt state[2] += c; 2178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt state[3] += d; 2188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt state[4] += e; 2198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* Wipe variables */ 2208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt a = b = c = d = e = 0; 2218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef SHA1HANDSOFF 2228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_memset(block, 0, 64); 2238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif 2248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 2258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 2268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 2278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/* SHA1Init - Initialize new context */ 2288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 2298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtvoid SHA1Init(SHA1_CTX* context) 2308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 2318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* SHA1 initialization constants */ 2328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt context->state[0] = 0x67452301; 2338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt context->state[1] = 0xEFCDAB89; 2348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt context->state[2] = 0x98BADCFE; 2358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt context->state[3] = 0x10325476; 2368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt context->state[4] = 0xC3D2E1F0; 2378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt context->count[0] = context->count[1] = 0; 2388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 2398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 2408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 2418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/* Run your data through this. */ 2428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 2438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtvoid SHA1Update(SHA1_CTX* context, const void *_data, u32 len) 2448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 2458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt u32 i, j; 2468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt const unsigned char *data = _data; 2478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 2488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef VERBOSE 2498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt SHAPrintContext(context, "before"); 2508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif 2518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt j = (context->count[0] >> 3) & 63; 2528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if ((context->count[0] += len << 3) < (len << 3)) 2538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt context->count[1]++; 2548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt context->count[1] += (len >> 29); 2558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if ((j + len) > 63) { 2568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_memcpy(&context->buffer[j], data, (i = 64-j)); 2578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt SHA1Transform(context->state, context->buffer); 2588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt for ( ; i + 63 < len; i += 64) { 2598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt SHA1Transform(context->state, &data[i]); 2608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 2618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt j = 0; 2628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 2638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt else i = 0; 2648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_memcpy(&context->buffer[j], &data[i], len - i); 2658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef VERBOSE 2668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt SHAPrintContext(context, "after "); 2678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif 2688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 2698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 2708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 2718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/* Add padding and return the message digest. */ 2728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 2738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtvoid SHA1Final(unsigned char digest[20], SHA1_CTX* context) 2748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 2758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt u32 i; 2768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt unsigned char finalcount[8]; 2778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 2788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt for (i = 0; i < 8; i++) { 2798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt finalcount[i] = (unsigned char) 2808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ((context->count[(i >= 4 ? 0 : 1)] >> 2818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ((3-(i & 3)) * 8) ) & 255); /* Endian independent */ 2828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 2838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt SHA1Update(context, (unsigned char *) "\200", 1); 2848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt while ((context->count[0] & 504) != 448) { 2858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt SHA1Update(context, (unsigned char *) "\0", 1); 2868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 2878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() 2888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 2898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt for (i = 0; i < 20; i++) { 2908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt digest[i] = (unsigned char) 2918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ((context->state[i >> 2] >> ((3 - (i & 3)) * 8)) & 2928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 255); 2938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 2948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* Wipe variables */ 2958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt i = 0; 2968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_memset(context->buffer, 0, 64); 2978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_memset(context->state, 0, 20); 2988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_memset(context->count, 0, 8); 2998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_memset(finalcount, 0, 8); 3008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 3018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 3028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/* ===== end - public domain SHA1 implementation ===== */ 303