sha1.c revision c1aef54e14bb92518b1c062ba8c0292a7cb949cb
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar * Copyright (C) 2004, 2005, 2007, 2009, 2011 Internet Systems Consortium, Inc. ("ISC")
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar * Copyright (C) 2000, 2001, 2003 Internet Software Consortium.
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar * Permission to use, copy, modify, and/or distribute this software for any
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar * purpose with or without fee is hereby granted, provided that the above
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar * copyright notice and this permission notice appear in all copies.
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar * PERFORMANCE OF THIS SOFTWARE.
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar/* $Id: sha1.c,v 1.22 2011/03/12 04:59:49 tbox Exp $ */
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar/* $NetBSD: sha1.c,v 1.5 2000/01/22 22:19:14 mycroft Exp $ */
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar/* $OpenBSD: sha1.c,v 1.9 1997/07/23 21:12:32 kstailey Exp $ */
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar * \author By Steve Reid <steve@edmweb.com>
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar * 100% Public Domain
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar * Test Vectors (from FIPS PUB 180-1)
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar * A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar * "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar * 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar * A million repetitions of "a"
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar * 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar * \endverbatim
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikarisc_sha1_update(isc_sha1_t *context, const unsigned char *data,
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar unsigned int len)
a307732568c3d861c38b0342ae32434226d10e94Pramod Gunjikar EVP_DigestUpdate(context, (const void *) data, (size_t) len);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikarisc_sha1_final(isc_sha1_t *context, unsigned char *digest) {
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar * blk0() and blk() perform the initial expand.
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar * I got the idea of expanding during the round function from SSLeay
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar (block->l[i] = (rol(block->l[i], 24) & 0xFF00FF00) \
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar (block->l[i & 15] = rol(block->l[(i + 13) & 15] \
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar * (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar#define R0(v,w,x,y,z,i) \
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar z += ((w & (x ^ y)) ^ y) + blk0(i) + 0x5A827999 + rol(v, 5); \
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar#define R1(v,w,x,y,z,i) \
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar z += ((w & (x ^ y)) ^ y) + blk(i) + 0x5A827999 + rol(v, 5); \
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar#define R2(v,w,x,y,z,i) \
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar z += (w ^ x ^ y) + blk(i) + 0x6ED9EBA1 + rol(v, 5); \
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar#define R3(v,w,x,y,z,i) \
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar z += (((w | x) & y) | (w & x)) + blk(i) + 0x8F1BBCDC + rol(v, 5); \
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar#define R4(v,w,x,y,z,i) \
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar z += (w ^ x ^ y) + blk(i) + 0xCA62C1D6 + rol(v, 5); \
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikartypedef union {
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar unsigned char c[64];
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar unsigned int l[16];
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikarstatic void do_R01(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c,
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar isc_uint32_t *d, isc_uint32_t *e, CHAR64LONG16 *);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikarstatic void do_R2(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c,
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar isc_uint32_t *d, isc_uint32_t *e, CHAR64LONG16 *);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikarstatic void do_R3(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c,
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar isc_uint32_t *d, isc_uint32_t *e, CHAR64LONG16 *);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikarstatic void do_R4(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c,
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar isc_uint32_t *d, isc_uint32_t *e, CHAR64LONG16 *);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar#define nR0(v,w,x,y,z,i) R0(*v,*w,*x,*y,*z,i)
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar#define nR1(v,w,x,y,z,i) R1(*v,*w,*x,*y,*z,i)
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar#define nR2(v,w,x,y,z,i) R2(*v,*w,*x,*y,*z,i)
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar#define nR3(v,w,x,y,z,i) R3(*v,*w,*x,*y,*z,i)
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar#define nR4(v,w,x,y,z,i) R4(*v,*w,*x,*y,*z,i)
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikardo_R01(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c, isc_uint32_t *d,
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar nR0(a,b,c,d,e, 0); nR0(e,a,b,c,d, 1); nR0(d,e,a,b,c, 2);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar nR0(c,d,e,a,b, 3); nR0(b,c,d,e,a, 4); nR0(a,b,c,d,e, 5);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar nR0(e,a,b,c,d, 6); nR0(d,e,a,b,c, 7); nR0(c,d,e,a,b, 8);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar nR0(b,c,d,e,a, 9); nR0(a,b,c,d,e,10); nR0(e,a,b,c,d,11);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar nR0(d,e,a,b,c,12); nR0(c,d,e,a,b,13); nR0(b,c,d,e,a,14);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar nR0(a,b,c,d,e,15); nR1(e,a,b,c,d,16); nR1(d,e,a,b,c,17);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikardo_R2(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c, isc_uint32_t *d,
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar nR2(a,b,c,d,e,20); nR2(e,a,b,c,d,21); nR2(d,e,a,b,c,22);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar nR2(c,d,e,a,b,23); nR2(b,c,d,e,a,24); nR2(a,b,c,d,e,25);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar nR2(e,a,b,c,d,26); nR2(d,e,a,b,c,27); nR2(c,d,e,a,b,28);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar nR2(b,c,d,e,a,29); nR2(a,b,c,d,e,30); nR2(e,a,b,c,d,31);
a307732568c3d861c38b0342ae32434226d10e94Pramod Gunjikar nR2(d,e,a,b,c,32); nR2(c,d,e,a,b,33); nR2(b,c,d,e,a,34);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar nR2(a,b,c,d,e,35); nR2(e,a,b,c,d,36); nR2(d,e,a,b,c,37);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikardo_R3(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c, isc_uint32_t *d,
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar nR3(a,b,c,d,e,40); nR3(e,a,b,c,d,41); nR3(d,e,a,b,c,42);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar nR3(c,d,e,a,b,43); nR3(b,c,d,e,a,44); nR3(a,b,c,d,e,45);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar nR3(e,a,b,c,d,46); nR3(d,e,a,b,c,47); nR3(c,d,e,a,b,48);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar nR3(b,c,d,e,a,49); nR3(a,b,c,d,e,50); nR3(e,a,b,c,d,51);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar nR3(d,e,a,b,c,52); nR3(c,d,e,a,b,53); nR3(b,c,d,e,a,54);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar nR3(a,b,c,d,e,55); nR3(e,a,b,c,d,56); nR3(d,e,a,b,c,57);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikardo_R4(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c, isc_uint32_t *d,
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar nR4(a,b,c,d,e,60); nR4(e,a,b,c,d,61); nR4(d,e,a,b,c,62);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar nR4(c,d,e,a,b,63); nR4(b,c,d,e,a,64); nR4(a,b,c,d,e,65);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar nR4(e,a,b,c,d,66); nR4(d,e,a,b,c,67); nR4(c,d,e,a,b,68);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar nR4(b,c,d,e,a,69); nR4(a,b,c,d,e,70); nR4(e,a,b,c,d,71);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar nR4(d,e,a,b,c,72); nR4(c,d,e,a,b,73); nR4(b,c,d,e,a,74);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar nR4(a,b,c,d,e,75); nR4(e,a,b,c,d,76); nR4(d,e,a,b,c,77);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar * Hash a single 512-bit block. This is the core of the algorithm.
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikartransform(isc_uint32_t state[5], const unsigned char buffer[64]) {
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar /* Copy context->state[] to working vars */
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar /* 4 rounds of 20 operations each. Loop unrolled. */
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar 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);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar 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);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar 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);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar 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);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar 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);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar 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);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar 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);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar 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);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar 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);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar 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);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar 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);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar 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);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar 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);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar 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);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar 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);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar 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);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar 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);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar 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);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar 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);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar 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);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar /* Add the working vars back into context.state[] */
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar /* Wipe variables */
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar a = b = c = d = e = 0;
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar /* Avoid compiler warnings */
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar POST(a); POST(b); POST(c); POST(d); POST(e);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar * isc_sha1_init - Initialize new context
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar /* SHA1 initialization constants */
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar * Run your data through this.
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikarisc_sha1_update(isc_sha1_t *context, const unsigned char *data,
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar unsigned int len)
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar unsigned int i, j;
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar (void)memcpy(&context->buffer[j], data, (i = 64 - j));
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar (void)memcpy(&context->buffer[j], &data[i], len - i);
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar * Add padding and return the message digest.
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikarstatic const unsigned char final_0 = 0;
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikarisc_sha1_final(isc_sha1_t *context, unsigned char *digest) {
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar unsigned int i;
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar for (i = 0; i < 8; i++) {
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar /* Endian independent */
a307732568c3d861c38b0342ae32434226d10e94Pramod Gunjikar finalcount[i] = (unsigned char)
c39526b769298791ff5b0b6c5e761f49aabaeb4ePramod Gunjikar /* The next Update should cause a transform() */
fffafeb2cc01732fd6a28ed530e4424094685eceJohnny Cheung for (i = 0; i < 20; i++)
fffafeb2cc01732fd6a28ed530e4424094685eceJohnny Cheung digest[i] = (unsigned char)