1258945Sroberto/* 2280849Scy * Copyright (C) 2004, 2005, 2007, 2009, 2011, 2012 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 18280849Scy/* $Id$ */ 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> 41280849Scy#include <isc/platform.h> 42258945Sroberto#include <isc/sha1.h> 43258945Sroberto#include <isc/string.h> 44258945Sroberto#include <isc/types.h> 45258945Sroberto#include <isc/util.h> 46258945Sroberto 47280849Scy#ifdef ISC_PLATFORM_OPENSSLHASH 48280849Scy 49280849Scyvoid 50280849Scyisc_sha1_init(isc_sha1_t *context) 51280849Scy{ 52280849Scy INSIST(context != NULL); 53280849Scy 54280849Scy EVP_DigestInit(context, EVP_sha1()); 55280849Scy} 56280849Scy 57280849Scyvoid 58280849Scyisc_sha1_invalidate(isc_sha1_t *context) { 59280849Scy EVP_MD_CTX_cleanup(context); 60280849Scy} 61280849Scy 62280849Scyvoid 63280849Scyisc_sha1_update(isc_sha1_t *context, const unsigned char *data, 64280849Scy unsigned int len) 65280849Scy{ 66280849Scy INSIST(context != 0); 67280849Scy INSIST(data != 0); 68280849Scy 69280849Scy EVP_DigestUpdate(context, (const void *) data, (size_t) len); 70280849Scy} 71280849Scy 72280849Scyvoid 73280849Scyisc_sha1_final(isc_sha1_t *context, unsigned char *digest) { 74280849Scy INSIST(digest != 0); 75280849Scy INSIST(context != 0); 76280849Scy 77280849Scy EVP_DigestFinal(context, digest, NULL); 78280849Scy} 79280849Scy 80280849Scy#else 81280849Scy 82258945Sroberto#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) 83258945Sroberto 84258945Sroberto/*@{*/ 85258945Sroberto/*! 86258945Sroberto * blk0() and blk() perform the initial expand. 87258945Sroberto * I got the idea of expanding during the round function from SSLeay 88258945Sroberto */ 89258945Sroberto#if !defined(WORDS_BIGENDIAN) 90258945Sroberto# define blk0(i) \ 91258945Sroberto (block->l[i] = (rol(block->l[i], 24) & 0xFF00FF00) \ 92258945Sroberto | (rol(block->l[i], 8) & 0x00FF00FF)) 93258945Sroberto#else 94258945Sroberto# define blk0(i) block->l[i] 95258945Sroberto#endif 96258945Sroberto#define blk(i) \ 97258945Sroberto (block->l[i & 15] = rol(block->l[(i + 13) & 15] \ 98258945Sroberto ^ block->l[(i + 8) & 15] \ 99258945Sroberto ^ block->l[(i + 2) & 15] \ 100258945Sroberto ^ block->l[i & 15], 1)) 101258945Sroberto 102258945Sroberto/*@}*/ 103258945Sroberto/*@{*/ 104258945Sroberto/*! 105258945Sroberto * (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1 106258945Sroberto */ 107258945Sroberto#define R0(v,w,x,y,z,i) \ 108258945Sroberto z += ((w & (x ^ y)) ^ y) + blk0(i) + 0x5A827999 + rol(v, 5); \ 109258945Sroberto w = rol(w, 30); 110258945Sroberto#define R1(v,w,x,y,z,i) \ 111258945Sroberto z += ((w & (x ^ y)) ^ y) + blk(i) + 0x5A827999 + rol(v, 5); \ 112258945Sroberto w = rol(w, 30); 113258945Sroberto#define R2(v,w,x,y,z,i) \ 114258945Sroberto z += (w ^ x ^ y) + blk(i) + 0x6ED9EBA1 + rol(v, 5); \ 115258945Sroberto w = rol(w, 30); 116258945Sroberto#define R3(v,w,x,y,z,i) \ 117258945Sroberto z += (((w | x) & y) | (w & x)) + blk(i) + 0x8F1BBCDC + rol(v, 5); \ 118258945Sroberto w = rol(w, 30); 119258945Sroberto#define R4(v,w,x,y,z,i) \ 120258945Sroberto z += (w ^ x ^ y) + blk(i) + 0xCA62C1D6 + rol(v, 5); \ 121258945Sroberto w = rol(w, 30); 122258945Sroberto 123258945Sroberto/*@}*/ 124258945Sroberto 125258945Srobertotypedef union { 126258945Sroberto unsigned char c[64]; 127258945Sroberto unsigned int l[16]; 128258945Sroberto} CHAR64LONG16; 129258945Sroberto 130258945Sroberto#ifdef __sparc_v9__ 131258945Srobertostatic void do_R01(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c, 132258945Sroberto isc_uint32_t *d, isc_uint32_t *e, CHAR64LONG16 *); 133258945Srobertostatic void do_R2(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c, 134258945Sroberto isc_uint32_t *d, isc_uint32_t *e, CHAR64LONG16 *); 135258945Srobertostatic void do_R3(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c, 136258945Sroberto isc_uint32_t *d, isc_uint32_t *e, CHAR64LONG16 *); 137258945Srobertostatic void do_R4(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c, 138258945Sroberto isc_uint32_t *d, isc_uint32_t *e, CHAR64LONG16 *); 139258945Sroberto 140258945Sroberto#define nR0(v,w,x,y,z,i) R0(*v,*w,*x,*y,*z,i) 141258945Sroberto#define nR1(v,w,x,y,z,i) R1(*v,*w,*x,*y,*z,i) 142258945Sroberto#define nR2(v,w,x,y,z,i) R2(*v,*w,*x,*y,*z,i) 143258945Sroberto#define nR3(v,w,x,y,z,i) R3(*v,*w,*x,*y,*z,i) 144258945Sroberto#define nR4(v,w,x,y,z,i) R4(*v,*w,*x,*y,*z,i) 145258945Sroberto 146258945Srobertostatic void 147258945Srobertodo_R01(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c, isc_uint32_t *d, 148258945Sroberto isc_uint32_t *e, CHAR64LONG16 *block) 149258945Sroberto{ 150258945Sroberto nR0(a,b,c,d,e, 0); nR0(e,a,b,c,d, 1); nR0(d,e,a,b,c, 2); 151258945Sroberto nR0(c,d,e,a,b, 3); nR0(b,c,d,e,a, 4); nR0(a,b,c,d,e, 5); 152258945Sroberto nR0(e,a,b,c,d, 6); nR0(d,e,a,b,c, 7); nR0(c,d,e,a,b, 8); 153258945Sroberto nR0(b,c,d,e,a, 9); nR0(a,b,c,d,e,10); nR0(e,a,b,c,d,11); 154258945Sroberto nR0(d,e,a,b,c,12); nR0(c,d,e,a,b,13); nR0(b,c,d,e,a,14); 155258945Sroberto nR0(a,b,c,d,e,15); nR1(e,a,b,c,d,16); nR1(d,e,a,b,c,17); 156258945Sroberto nR1(c,d,e,a,b,18); nR1(b,c,d,e,a,19); 157258945Sroberto} 158258945Sroberto 159258945Srobertostatic void 160258945Srobertodo_R2(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c, isc_uint32_t *d, 161258945Sroberto isc_uint32_t *e, CHAR64LONG16 *block) 162258945Sroberto{ 163258945Sroberto nR2(a,b,c,d,e,20); nR2(e,a,b,c,d,21); nR2(d,e,a,b,c,22); 164258945Sroberto nR2(c,d,e,a,b,23); nR2(b,c,d,e,a,24); nR2(a,b,c,d,e,25); 165258945Sroberto nR2(e,a,b,c,d,26); nR2(d,e,a,b,c,27); nR2(c,d,e,a,b,28); 166258945Sroberto nR2(b,c,d,e,a,29); nR2(a,b,c,d,e,30); nR2(e,a,b,c,d,31); 167258945Sroberto nR2(d,e,a,b,c,32); nR2(c,d,e,a,b,33); nR2(b,c,d,e,a,34); 168258945Sroberto nR2(a,b,c,d,e,35); nR2(e,a,b,c,d,36); nR2(d,e,a,b,c,37); 169258945Sroberto nR2(c,d,e,a,b,38); nR2(b,c,d,e,a,39); 170258945Sroberto} 171258945Sroberto 172258945Srobertostatic void 173258945Srobertodo_R3(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c, isc_uint32_t *d, 174258945Sroberto isc_uint32_t *e, CHAR64LONG16 *block) 175258945Sroberto{ 176258945Sroberto nR3(a,b,c,d,e,40); nR3(e,a,b,c,d,41); nR3(d,e,a,b,c,42); 177258945Sroberto nR3(c,d,e,a,b,43); nR3(b,c,d,e,a,44); nR3(a,b,c,d,e,45); 178258945Sroberto nR3(e,a,b,c,d,46); nR3(d,e,a,b,c,47); nR3(c,d,e,a,b,48); 179258945Sroberto nR3(b,c,d,e,a,49); nR3(a,b,c,d,e,50); nR3(e,a,b,c,d,51); 180258945Sroberto nR3(d,e,a,b,c,52); nR3(c,d,e,a,b,53); nR3(b,c,d,e,a,54); 181258945Sroberto nR3(a,b,c,d,e,55); nR3(e,a,b,c,d,56); nR3(d,e,a,b,c,57); 182258945Sroberto nR3(c,d,e,a,b,58); nR3(b,c,d,e,a,59); 183258945Sroberto} 184258945Sroberto 185258945Srobertostatic void 186258945Srobertodo_R4(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c, isc_uint32_t *d, 187258945Sroberto isc_uint32_t *e, CHAR64LONG16 *block) 188258945Sroberto{ 189258945Sroberto nR4(a,b,c,d,e,60); nR4(e,a,b,c,d,61); nR4(d,e,a,b,c,62); 190258945Sroberto nR4(c,d,e,a,b,63); nR4(b,c,d,e,a,64); nR4(a,b,c,d,e,65); 191258945Sroberto nR4(e,a,b,c,d,66); nR4(d,e,a,b,c,67); nR4(c,d,e,a,b,68); 192258945Sroberto nR4(b,c,d,e,a,69); nR4(a,b,c,d,e,70); nR4(e,a,b,c,d,71); 193258945Sroberto nR4(d,e,a,b,c,72); nR4(c,d,e,a,b,73); nR4(b,c,d,e,a,74); 194258945Sroberto nR4(a,b,c,d,e,75); nR4(e,a,b,c,d,76); nR4(d,e,a,b,c,77); 195258945Sroberto nR4(c,d,e,a,b,78); nR4(b,c,d,e,a,79); 196258945Sroberto} 197258945Sroberto#endif 198258945Sroberto 199258945Sroberto/*! 200258945Sroberto * Hash a single 512-bit block. This is the core of the algorithm. 201258945Sroberto */ 202258945Srobertostatic void 203258945Srobertotransform(isc_uint32_t state[5], const unsigned char buffer[64]) { 204258945Sroberto isc_uint32_t a, b, c, d, e; 205258945Sroberto CHAR64LONG16 *block; 206258945Sroberto CHAR64LONG16 workspace; 207258945Sroberto 208258945Sroberto INSIST(buffer != NULL); 209258945Sroberto INSIST(state != NULL); 210258945Sroberto 211258945Sroberto block = &workspace; 212258945Sroberto (void)memcpy(block, buffer, 64); 213258945Sroberto 214258945Sroberto /* Copy context->state[] to working vars */ 215258945Sroberto a = state[0]; 216258945Sroberto b = state[1]; 217258945Sroberto c = state[2]; 218258945Sroberto d = state[3]; 219258945Sroberto e = state[4]; 220258945Sroberto 221258945Sroberto#ifdef __sparc_v9__ 222258945Sroberto do_R01(&a, &b, &c, &d, &e, block); 223258945Sroberto do_R2(&a, &b, &c, &d, &e, block); 224258945Sroberto do_R3(&a, &b, &c, &d, &e, block); 225258945Sroberto do_R4(&a, &b, &c, &d, &e, block); 226258945Sroberto#else 227258945Sroberto /* 4 rounds of 20 operations each. Loop unrolled. */ 228258945Sroberto 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); 229258945Sroberto 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); 230258945Sroberto 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); 231258945Sroberto 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); 232258945Sroberto 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); 233258945Sroberto 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); 234258945Sroberto 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); 235258945Sroberto 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); 236258945Sroberto 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); 237258945Sroberto 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); 238258945Sroberto 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); 239258945Sroberto 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); 240258945Sroberto 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); 241258945Sroberto 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); 242258945Sroberto 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); 243258945Sroberto 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); 244258945Sroberto 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); 245258945Sroberto 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); 246258945Sroberto 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); 247258945Sroberto 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); 248258945Sroberto#endif 249258945Sroberto 250258945Sroberto /* Add the working vars back into context.state[] */ 251258945Sroberto state[0] += a; 252258945Sroberto state[1] += b; 253258945Sroberto state[2] += c; 254258945Sroberto state[3] += d; 255258945Sroberto state[4] += e; 256258945Sroberto 257258945Sroberto /* Wipe variables */ 258258945Sroberto a = b = c = d = e = 0; 259280849Scy /* Avoid compiler warnings */ 260280849Scy POST(a); POST(b); POST(c); POST(d); POST(e); 261258945Sroberto} 262258945Sroberto 263258945Sroberto 264258945Sroberto/*! 265258945Sroberto * isc_sha1_init - Initialize new context 266258945Sroberto */ 267258945Srobertovoid 268258945Srobertoisc_sha1_init(isc_sha1_t *context) 269258945Sroberto{ 270258945Sroberto INSIST(context != NULL); 271258945Sroberto 272258945Sroberto /* SHA1 initialization constants */ 273258945Sroberto context->state[0] = 0x67452301; 274258945Sroberto context->state[1] = 0xEFCDAB89; 275258945Sroberto context->state[2] = 0x98BADCFE; 276258945Sroberto context->state[3] = 0x10325476; 277258945Sroberto context->state[4] = 0xC3D2E1F0; 278258945Sroberto context->count[0] = 0; 279258945Sroberto context->count[1] = 0; 280258945Sroberto} 281258945Sroberto 282258945Srobertovoid 283258945Srobertoisc_sha1_invalidate(isc_sha1_t *context) { 284258945Sroberto memset(context, 0, sizeof(isc_sha1_t)); 285258945Sroberto} 286258945Sroberto 287258945Sroberto/*! 288258945Sroberto * Run your data through this. 289258945Sroberto */ 290258945Srobertovoid 291258945Srobertoisc_sha1_update(isc_sha1_t *context, const unsigned char *data, 292258945Sroberto unsigned int len) 293258945Sroberto{ 294258945Sroberto unsigned int i, j; 295258945Sroberto 296258945Sroberto INSIST(context != 0); 297258945Sroberto INSIST(data != 0); 298258945Sroberto 299258945Sroberto j = context->count[0]; 300258945Sroberto if ((context->count[0] += len << 3) < j) 301258945Sroberto context->count[1] += (len >> 29) + 1; 302258945Sroberto j = (j >> 3) & 63; 303258945Sroberto if ((j + len) > 63) { 304258945Sroberto (void)memcpy(&context->buffer[j], data, (i = 64 - j)); 305258945Sroberto transform(context->state, context->buffer); 306258945Sroberto for (; i + 63 < len; i += 64) 307258945Sroberto transform(context->state, &data[i]); 308258945Sroberto j = 0; 309258945Sroberto } else { 310258945Sroberto i = 0; 311258945Sroberto } 312258945Sroberto 313258945Sroberto (void)memcpy(&context->buffer[j], &data[i], len - i); 314258945Sroberto} 315258945Sroberto 316258945Sroberto 317258945Sroberto/*! 318258945Sroberto * Add padding and return the message digest. 319258945Sroberto */ 320258945Sroberto 321258945Srobertostatic const unsigned char final_200 = 128; 322258945Srobertostatic const unsigned char final_0 = 0; 323258945Sroberto 324258945Srobertovoid 325258945Srobertoisc_sha1_final(isc_sha1_t *context, unsigned char *digest) { 326258945Sroberto unsigned int i; 327258945Sroberto unsigned char finalcount[8]; 328258945Sroberto 329258945Sroberto INSIST(digest != 0); 330258945Sroberto INSIST(context != 0); 331258945Sroberto 332258945Sroberto for (i = 0; i < 8; i++) { 333258945Sroberto /* Endian independent */ 334258945Sroberto finalcount[i] = (unsigned char) 335258945Sroberto ((context->count[(i >= 4 ? 0 : 1)] 336258945Sroberto >> ((3 - (i & 3)) * 8)) & 255); 337258945Sroberto } 338258945Sroberto 339258945Sroberto isc_sha1_update(context, &final_200, 1); 340258945Sroberto while ((context->count[0] & 504) != 448) 341258945Sroberto isc_sha1_update(context, &final_0, 1); 342258945Sroberto /* The next Update should cause a transform() */ 343258945Sroberto isc_sha1_update(context, finalcount, 8); 344258945Sroberto 345258945Sroberto if (digest) { 346258945Sroberto for (i = 0; i < 20; i++) 347258945Sroberto digest[i] = (unsigned char) 348258945Sroberto ((context->state[i >> 2] 349258945Sroberto >> ((3 - (i & 3)) * 8)) & 255); 350258945Sroberto } 351258945Sroberto 352258945Sroberto memset(context, 0, sizeof(isc_sha1_t)); 353258945Sroberto} 354280849Scy#endif 355