sha1.c revision 258945
1258945Sroberto/* 2258945Sroberto * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC") 3258945Sroberto * Copyright (C) 2000, 2001, 2003 Internet Software Consortium. 4258945Sroberto * 5258945Sroberto * Permission to use, copy, modify, and/or distribute this software for any 6258945Sroberto * purpose with or without fee is hereby granted, provided that the above 7258945Sroberto * copyright notice and this permission notice appear in all copies. 8258945Sroberto * 9258945Sroberto * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 10258945Sroberto * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 11258945Sroberto * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 12258945Sroberto * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 13258945Sroberto * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 14258945Sroberto * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 15258945Sroberto * PERFORMANCE OF THIS SOFTWARE. 16258945Sroberto */ 17258945Sroberto 18258945Sroberto/* $Id: sha1.c,v 1.18 2007/06/19 23:47:17 tbox Exp $ */ 19258945Sroberto 20258945Sroberto/* $NetBSD: sha1.c,v 1.5 2000/01/22 22:19:14 mycroft Exp $ */ 21258945Sroberto/* $OpenBSD: sha1.c,v 1.9 1997/07/23 21:12:32 kstailey Exp $ */ 22258945Sroberto 23258945Sroberto/*! \file 24258945Sroberto * SHA-1 in C 25258945Sroberto * \author By Steve Reid <steve@edmweb.com> 26258945Sroberto * 100% Public Domain 27258945Sroberto * \verbatim 28258945Sroberto * Test Vectors (from FIPS PUB 180-1) 29258945Sroberto * "abc" 30258945Sroberto * A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D 31258945Sroberto * "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" 32258945Sroberto * 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 33258945Sroberto * A million repetitions of "a" 34258945Sroberto * 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F 35258945Sroberto * \endverbatim 36258945Sroberto */ 37258945Sroberto 38258945Sroberto#include "config.h" 39258945Sroberto 40258945Sroberto#include <isc/assertions.h> 41258945Sroberto#include <isc/sha1.h> 42258945Sroberto#include <isc/string.h> 43258945Sroberto#include <isc/types.h> 44258945Sroberto#include <isc/util.h> 45258945Sroberto 46258945Sroberto#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) 47258945Sroberto 48258945Sroberto/*@{*/ 49258945Sroberto/*! 50258945Sroberto * blk0() and blk() perform the initial expand. 51258945Sroberto * I got the idea of expanding during the round function from SSLeay 52258945Sroberto */ 53258945Sroberto#if !defined(WORDS_BIGENDIAN) 54258945Sroberto# define blk0(i) \ 55258945Sroberto (block->l[i] = (rol(block->l[i], 24) & 0xFF00FF00) \ 56258945Sroberto | (rol(block->l[i], 8) & 0x00FF00FF)) 57258945Sroberto#else 58258945Sroberto# define blk0(i) block->l[i] 59258945Sroberto#endif 60258945Sroberto#define blk(i) \ 61258945Sroberto (block->l[i & 15] = rol(block->l[(i + 13) & 15] \ 62258945Sroberto ^ block->l[(i + 8) & 15] \ 63258945Sroberto ^ block->l[(i + 2) & 15] \ 64258945Sroberto ^ block->l[i & 15], 1)) 65258945Sroberto 66258945Sroberto/*@}*/ 67258945Sroberto/*@{*/ 68258945Sroberto/*! 69258945Sroberto * (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1 70258945Sroberto */ 71258945Sroberto#define R0(v,w,x,y,z,i) \ 72258945Sroberto z += ((w & (x ^ y)) ^ y) + blk0(i) + 0x5A827999 + rol(v, 5); \ 73258945Sroberto w = rol(w, 30); 74258945Sroberto#define R1(v,w,x,y,z,i) \ 75258945Sroberto z += ((w & (x ^ y)) ^ y) + blk(i) + 0x5A827999 + rol(v, 5); \ 76258945Sroberto w = rol(w, 30); 77258945Sroberto#define R2(v,w,x,y,z,i) \ 78258945Sroberto z += (w ^ x ^ y) + blk(i) + 0x6ED9EBA1 + rol(v, 5); \ 79258945Sroberto w = rol(w, 30); 80258945Sroberto#define R3(v,w,x,y,z,i) \ 81258945Sroberto z += (((w | x) & y) | (w & x)) + blk(i) + 0x8F1BBCDC + rol(v, 5); \ 82258945Sroberto w = rol(w, 30); 83258945Sroberto#define R4(v,w,x,y,z,i) \ 84258945Sroberto z += (w ^ x ^ y) + blk(i) + 0xCA62C1D6 + rol(v, 5); \ 85258945Sroberto w = rol(w, 30); 86258945Sroberto 87258945Sroberto/*@}*/ 88258945Sroberto 89258945Srobertotypedef union { 90258945Sroberto unsigned char c[64]; 91258945Sroberto unsigned int l[16]; 92258945Sroberto} CHAR64LONG16; 93258945Sroberto 94258945Sroberto#ifdef __sparc_v9__ 95258945Srobertostatic void do_R01(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c, 96258945Sroberto isc_uint32_t *d, isc_uint32_t *e, CHAR64LONG16 *); 97258945Srobertostatic void do_R2(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c, 98258945Sroberto isc_uint32_t *d, isc_uint32_t *e, CHAR64LONG16 *); 99258945Srobertostatic void do_R3(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c, 100258945Sroberto isc_uint32_t *d, isc_uint32_t *e, CHAR64LONG16 *); 101258945Srobertostatic void do_R4(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c, 102258945Sroberto isc_uint32_t *d, isc_uint32_t *e, CHAR64LONG16 *); 103258945Sroberto 104258945Sroberto#define nR0(v,w,x,y,z,i) R0(*v,*w,*x,*y,*z,i) 105258945Sroberto#define nR1(v,w,x,y,z,i) R1(*v,*w,*x,*y,*z,i) 106258945Sroberto#define nR2(v,w,x,y,z,i) R2(*v,*w,*x,*y,*z,i) 107258945Sroberto#define nR3(v,w,x,y,z,i) R3(*v,*w,*x,*y,*z,i) 108258945Sroberto#define nR4(v,w,x,y,z,i) R4(*v,*w,*x,*y,*z,i) 109258945Sroberto 110258945Srobertostatic void 111258945Srobertodo_R01(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c, isc_uint32_t *d, 112258945Sroberto isc_uint32_t *e, CHAR64LONG16 *block) 113258945Sroberto{ 114258945Sroberto nR0(a,b,c,d,e, 0); nR0(e,a,b,c,d, 1); nR0(d,e,a,b,c, 2); 115258945Sroberto nR0(c,d,e,a,b, 3); nR0(b,c,d,e,a, 4); nR0(a,b,c,d,e, 5); 116258945Sroberto nR0(e,a,b,c,d, 6); nR0(d,e,a,b,c, 7); nR0(c,d,e,a,b, 8); 117258945Sroberto nR0(b,c,d,e,a, 9); nR0(a,b,c,d,e,10); nR0(e,a,b,c,d,11); 118258945Sroberto nR0(d,e,a,b,c,12); nR0(c,d,e,a,b,13); nR0(b,c,d,e,a,14); 119258945Sroberto nR0(a,b,c,d,e,15); nR1(e,a,b,c,d,16); nR1(d,e,a,b,c,17); 120258945Sroberto nR1(c,d,e,a,b,18); nR1(b,c,d,e,a,19); 121258945Sroberto} 122258945Sroberto 123258945Srobertostatic void 124258945Srobertodo_R2(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c, isc_uint32_t *d, 125258945Sroberto isc_uint32_t *e, CHAR64LONG16 *block) 126258945Sroberto{ 127258945Sroberto nR2(a,b,c,d,e,20); nR2(e,a,b,c,d,21); nR2(d,e,a,b,c,22); 128258945Sroberto nR2(c,d,e,a,b,23); nR2(b,c,d,e,a,24); nR2(a,b,c,d,e,25); 129258945Sroberto nR2(e,a,b,c,d,26); nR2(d,e,a,b,c,27); nR2(c,d,e,a,b,28); 130258945Sroberto nR2(b,c,d,e,a,29); nR2(a,b,c,d,e,30); nR2(e,a,b,c,d,31); 131258945Sroberto nR2(d,e,a,b,c,32); nR2(c,d,e,a,b,33); nR2(b,c,d,e,a,34); 132258945Sroberto nR2(a,b,c,d,e,35); nR2(e,a,b,c,d,36); nR2(d,e,a,b,c,37); 133258945Sroberto nR2(c,d,e,a,b,38); nR2(b,c,d,e,a,39); 134258945Sroberto} 135258945Sroberto 136258945Srobertostatic void 137258945Srobertodo_R3(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c, isc_uint32_t *d, 138258945Sroberto isc_uint32_t *e, CHAR64LONG16 *block) 139258945Sroberto{ 140258945Sroberto nR3(a,b,c,d,e,40); nR3(e,a,b,c,d,41); nR3(d,e,a,b,c,42); 141258945Sroberto nR3(c,d,e,a,b,43); nR3(b,c,d,e,a,44); nR3(a,b,c,d,e,45); 142258945Sroberto nR3(e,a,b,c,d,46); nR3(d,e,a,b,c,47); nR3(c,d,e,a,b,48); 143258945Sroberto nR3(b,c,d,e,a,49); nR3(a,b,c,d,e,50); nR3(e,a,b,c,d,51); 144258945Sroberto nR3(d,e,a,b,c,52); nR3(c,d,e,a,b,53); nR3(b,c,d,e,a,54); 145258945Sroberto nR3(a,b,c,d,e,55); nR3(e,a,b,c,d,56); nR3(d,e,a,b,c,57); 146258945Sroberto nR3(c,d,e,a,b,58); nR3(b,c,d,e,a,59); 147258945Sroberto} 148258945Sroberto 149258945Srobertostatic void 150258945Srobertodo_R4(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c, isc_uint32_t *d, 151258945Sroberto isc_uint32_t *e, CHAR64LONG16 *block) 152258945Sroberto{ 153258945Sroberto nR4(a,b,c,d,e,60); nR4(e,a,b,c,d,61); nR4(d,e,a,b,c,62); 154258945Sroberto nR4(c,d,e,a,b,63); nR4(b,c,d,e,a,64); nR4(a,b,c,d,e,65); 155258945Sroberto nR4(e,a,b,c,d,66); nR4(d,e,a,b,c,67); nR4(c,d,e,a,b,68); 156258945Sroberto nR4(b,c,d,e,a,69); nR4(a,b,c,d,e,70); nR4(e,a,b,c,d,71); 157258945Sroberto nR4(d,e,a,b,c,72); nR4(c,d,e,a,b,73); nR4(b,c,d,e,a,74); 158258945Sroberto nR4(a,b,c,d,e,75); nR4(e,a,b,c,d,76); nR4(d,e,a,b,c,77); 159258945Sroberto nR4(c,d,e,a,b,78); nR4(b,c,d,e,a,79); 160258945Sroberto} 161258945Sroberto#endif 162258945Sroberto 163258945Sroberto/*! 164258945Sroberto * Hash a single 512-bit block. This is the core of the algorithm. 165258945Sroberto */ 166258945Srobertostatic void 167258945Srobertotransform(isc_uint32_t state[5], const unsigned char buffer[64]) { 168258945Sroberto isc_uint32_t a, b, c, d, e; 169258945Sroberto CHAR64LONG16 *block; 170258945Sroberto CHAR64LONG16 workspace; 171258945Sroberto 172258945Sroberto INSIST(buffer != NULL); 173258945Sroberto INSIST(state != NULL); 174258945Sroberto 175258945Sroberto block = &workspace; 176258945Sroberto (void)memcpy(block, buffer, 64); 177258945Sroberto 178258945Sroberto /* Copy context->state[] to working vars */ 179258945Sroberto a = state[0]; 180258945Sroberto b = state[1]; 181258945Sroberto c = state[2]; 182258945Sroberto d = state[3]; 183258945Sroberto e = state[4]; 184258945Sroberto 185258945Sroberto#ifdef __sparc_v9__ 186258945Sroberto do_R01(&a, &b, &c, &d, &e, block); 187258945Sroberto do_R2(&a, &b, &c, &d, &e, block); 188258945Sroberto do_R3(&a, &b, &c, &d, &e, block); 189258945Sroberto do_R4(&a, &b, &c, &d, &e, block); 190258945Sroberto#else 191258945Sroberto /* 4 rounds of 20 operations each. Loop unrolled. */ 192258945Sroberto 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); 193258945Sroberto 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); 194258945Sroberto 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); 195258945Sroberto 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); 196258945Sroberto 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); 197258945Sroberto 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); 198258945Sroberto 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); 199258945Sroberto 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); 200258945Sroberto 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); 201258945Sroberto 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); 202258945Sroberto 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); 203258945Sroberto 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); 204258945Sroberto 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); 205258945Sroberto 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); 206258945Sroberto 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); 207258945Sroberto 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); 208258945Sroberto 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); 209258945Sroberto 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); 210258945Sroberto 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); 211258945Sroberto 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); 212258945Sroberto#endif 213258945Sroberto 214258945Sroberto /* Add the working vars back into context.state[] */ 215258945Sroberto state[0] += a; 216258945Sroberto state[1] += b; 217258945Sroberto state[2] += c; 218258945Sroberto state[3] += d; 219258945Sroberto state[4] += e; 220258945Sroberto 221258945Sroberto /* Wipe variables */ 222258945Sroberto a = b = c = d = e = 0; 223258945Sroberto} 224258945Sroberto 225258945Sroberto 226258945Sroberto/*! 227258945Sroberto * isc_sha1_init - Initialize new context 228258945Sroberto */ 229258945Srobertovoid 230258945Srobertoisc_sha1_init(isc_sha1_t *context) 231258945Sroberto{ 232258945Sroberto INSIST(context != NULL); 233258945Sroberto 234258945Sroberto /* SHA1 initialization constants */ 235258945Sroberto context->state[0] = 0x67452301; 236258945Sroberto context->state[1] = 0xEFCDAB89; 237258945Sroberto context->state[2] = 0x98BADCFE; 238258945Sroberto context->state[3] = 0x10325476; 239258945Sroberto context->state[4] = 0xC3D2E1F0; 240258945Sroberto context->count[0] = 0; 241258945Sroberto context->count[1] = 0; 242258945Sroberto} 243258945Sroberto 244258945Srobertovoid 245258945Srobertoisc_sha1_invalidate(isc_sha1_t *context) { 246258945Sroberto memset(context, 0, sizeof(isc_sha1_t)); 247258945Sroberto} 248258945Sroberto 249258945Sroberto/*! 250258945Sroberto * Run your data through this. 251258945Sroberto */ 252258945Srobertovoid 253258945Srobertoisc_sha1_update(isc_sha1_t *context, const unsigned char *data, 254258945Sroberto unsigned int len) 255258945Sroberto{ 256258945Sroberto unsigned int i, j; 257258945Sroberto 258258945Sroberto INSIST(context != 0); 259258945Sroberto INSIST(data != 0); 260258945Sroberto 261258945Sroberto j = context->count[0]; 262258945Sroberto if ((context->count[0] += len << 3) < j) 263258945Sroberto context->count[1] += (len >> 29) + 1; 264258945Sroberto j = (j >> 3) & 63; 265258945Sroberto if ((j + len) > 63) { 266258945Sroberto (void)memcpy(&context->buffer[j], data, (i = 64 - j)); 267258945Sroberto transform(context->state, context->buffer); 268258945Sroberto for (; i + 63 < len; i += 64) 269258945Sroberto transform(context->state, &data[i]); 270258945Sroberto j = 0; 271258945Sroberto } else { 272258945Sroberto i = 0; 273258945Sroberto } 274258945Sroberto 275258945Sroberto (void)memcpy(&context->buffer[j], &data[i], len - i); 276258945Sroberto} 277258945Sroberto 278258945Sroberto 279258945Sroberto/*! 280258945Sroberto * Add padding and return the message digest. 281258945Sroberto */ 282258945Sroberto 283258945Srobertostatic const unsigned char final_200 = 128; 284258945Srobertostatic const unsigned char final_0 = 0; 285258945Sroberto 286258945Srobertovoid 287258945Srobertoisc_sha1_final(isc_sha1_t *context, unsigned char *digest) { 288258945Sroberto unsigned int i; 289258945Sroberto unsigned char finalcount[8]; 290258945Sroberto 291258945Sroberto INSIST(digest != 0); 292258945Sroberto INSIST(context != 0); 293258945Sroberto 294258945Sroberto for (i = 0; i < 8; i++) { 295258945Sroberto /* Endian independent */ 296258945Sroberto finalcount[i] = (unsigned char) 297258945Sroberto ((context->count[(i >= 4 ? 0 : 1)] 298258945Sroberto >> ((3 - (i & 3)) * 8)) & 255); 299258945Sroberto } 300258945Sroberto 301258945Sroberto isc_sha1_update(context, &final_200, 1); 302258945Sroberto while ((context->count[0] & 504) != 448) 303258945Sroberto isc_sha1_update(context, &final_0, 1); 304258945Sroberto /* The next Update should cause a transform() */ 305258945Sroberto isc_sha1_update(context, finalcount, 8); 306258945Sroberto 307258945Sroberto if (digest) { 308258945Sroberto for (i = 0; i < 20; i++) 309258945Sroberto digest[i] = (unsigned char) 310258945Sroberto ((context->state[i >> 2] 311258945Sroberto >> ((3 - (i & 3)) * 8)) & 255); 312258945Sroberto } 313258945Sroberto 314258945Sroberto memset(context, 0, sizeof(isc_sha1_t)); 315258945Sroberto} 316