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