sha1.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004, 2005, 2007, 2009, 2011, 2012, 2014  Internet Systems Consortium, Inc. ("ISC")
00003  * Copyright (C) 2000, 2001, 2003  Internet Software Consortium.
00004  *
00005  * Permission to use, copy, modify, and/or distribute this software for any
00006  * purpose with or without fee is hereby granted, provided that the above
00007  * copyright notice and this permission notice appear in all copies.
00008  *
00009  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00010  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00011  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00012  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00013  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00014  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00015  * PERFORMANCE OF THIS SOFTWARE.
00016  */
00017 
00018 /* $Id$ */
00019 
00020 /*      $NetBSD: sha1.c,v 1.5 2000/01/22 22:19:14 mycroft Exp $ */
00021 /*      $OpenBSD: sha1.c,v 1.9 1997/07/23 21:12:32 kstailey Exp $       */
00022 
00023 /*! \file
00024  * SHA-1 in C
00025  * \author By Steve Reid <steve@edmweb.com>
00026  * 100% Public Domain
00027  * \verbatim
00028  * Test Vectors (from FIPS PUB 180-1)
00029  * "abc"
00030  *   A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
00031  * "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
00032  *   84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
00033  * A million repetitions of "a"
00034  *   34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
00035  * \endverbatim
00036  */
00037 
00038 #include "config.h"
00039 
00040 #include <isc/assertions.h>
00041 #include <isc/platform.h>
00042 #include <isc/sha1.h>
00043 #include <isc/string.h>
00044 #include <isc/types.h>
00045 #include <isc/util.h>
00046 
00047 #if PKCS11CRYPTO
00048 #include <pk11/internal.h>
00049 #include <pk11/pk11.h>
00050 #endif
00051 
00052 #ifdef ISC_PLATFORM_OPENSSLHASH
00053 void
00054 isc_sha1_init(isc_sha1_t *context)
00055 {
00056         INSIST(context != NULL);
00057 
00058         RUNTIME_CHECK(EVP_DigestInit(context, EVP_sha1()) == 1);
00059 }
00060 
00061 void
00062 isc_sha1_invalidate(isc_sha1_t *context) {
00063         EVP_MD_CTX_cleanup(context);
00064 }
00065 
00066 void
00067 isc_sha1_update(isc_sha1_t *context, const unsigned char *data,
00068                 unsigned int len)
00069 {
00070         INSIST(context != 0);
00071         INSIST(data != 0);
00072 
00073         RUNTIME_CHECK(EVP_DigestUpdate(context,
00074                                        (const void *) data,
00075                                        (size_t) len) == 1);
00076 }
00077 
00078 void
00079 isc_sha1_final(isc_sha1_t *context, unsigned char *digest) {
00080         INSIST(digest != 0);
00081         INSIST(context != 0);
00082 
00083         RUNTIME_CHECK(EVP_DigestFinal(context, digest, NULL) == 1);
00084 }
00085 
00086 #elif PKCS11CRYPTO
00087 
00088 void
00089 isc_sha1_init(isc_sha1_t *ctx) {
00090         CK_RV rv;
00091         CK_MECHANISM mech = { CKM_SHA_1, NULL, 0 };
00092 
00093         RUNTIME_CHECK(pk11_get_session(ctx, OP_DIGEST, ISC_TRUE, ISC_FALSE,
00094                                        ISC_FALSE, NULL, 0) == ISC_R_SUCCESS);
00095         PK11_FATALCHECK(pkcs_C_DigestInit, (ctx->session, &mech));
00096 }
00097 
00098 void
00099 isc_sha1_invalidate(isc_sha1_t *ctx) {
00100         CK_BYTE garbage[ISC_SHA1_DIGESTLENGTH];
00101         CK_ULONG len = ISC_SHA1_DIGESTLENGTH;
00102 
00103         if (ctx->handle == NULL)
00104                 return;
00105         (void) pkcs_C_DigestFinal(ctx->session, garbage, &len);
00106         memset(garbage, 0, sizeof(garbage));
00107         pk11_return_session(ctx);
00108 }
00109 
00110 void
00111 isc_sha1_update(isc_sha1_t *ctx, const unsigned char *buf, unsigned int len) {
00112         CK_RV rv;
00113         CK_BYTE_PTR pPart;
00114 
00115         DE_CONST(buf, pPart);
00116         PK11_FATALCHECK(pkcs_C_DigestUpdate,
00117                         (ctx->session, pPart, (CK_ULONG) len));
00118 }
00119 
00120 void
00121 isc_sha1_final(isc_sha1_t *ctx, unsigned char *digest) {
00122         CK_RV rv;
00123         CK_ULONG len = ISC_SHA1_DIGESTLENGTH;
00124 
00125         PK11_FATALCHECK(pkcs_C_DigestFinal,
00126                         (ctx->session, (CK_BYTE_PTR) digest, &len));
00127         pk11_return_session(ctx);
00128 }
00129 
00130 #else
00131 
00132 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
00133 
00134 /*@{*/
00135 /*!
00136  * blk0() and blk() perform the initial expand.
00137  * I got the idea of expanding during the round function from SSLeay
00138  */
00139 #if !defined(WORDS_BIGENDIAN)
00140 # define blk0(i) \
00141         (block->l[i] = (rol(block->l[i], 24) & 0xFF00FF00) \
00142          | (rol(block->l[i], 8) & 0x00FF00FF))
00143 #else
00144 # define blk0(i) block->l[i]
00145 #endif
00146 #define blk(i) \
00147         (block->l[i & 15] = rol(block->l[(i + 13) & 15] \
00148                                 ^ block->l[(i + 8) & 15] \
00149                                 ^ block->l[(i + 2) & 15] \
00150                                 ^ block->l[i & 15], 1))
00151 
00152 /*@}*/
00153 /*@{*/
00154 /*!
00155  * (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1
00156  */
00157 #define R0(v,w,x,y,z,i) \
00158         z += ((w & (x ^ y)) ^ y) + blk0(i) + 0x5A827999 + rol(v, 5); \
00159         w = rol(w, 30);
00160 #define R1(v,w,x,y,z,i) \
00161         z += ((w & (x ^ y)) ^ y) + blk(i) + 0x5A827999 + rol(v, 5); \
00162         w = rol(w, 30);
00163 #define R2(v,w,x,y,z,i) \
00164         z += (w ^ x ^ y) + blk(i) + 0x6ED9EBA1 + rol(v, 5); \
00165         w = rol(w, 30);
00166 #define R3(v,w,x,y,z,i) \
00167         z += (((w | x) & y) | (w & x)) + blk(i) + 0x8F1BBCDC + rol(v, 5); \
00168         w = rol(w, 30);
00169 #define R4(v,w,x,y,z,i) \
00170         z += (w ^ x ^ y) + blk(i) + 0xCA62C1D6 + rol(v, 5); \
00171         w = rol(w, 30);
00172 
00173 /*@}*/
00174 
00175 typedef union {
00176         unsigned char c[64];
00177         unsigned int l[16];
00178 } CHAR64LONG16;
00179 
00180 #ifdef __sparc_v9__
00181 static void do_R01(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c,
00182                    isc_uint32_t *d, isc_uint32_t *e, CHAR64LONG16 *);
00183 static void do_R2(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c,
00184                   isc_uint32_t *d, isc_uint32_t *e, CHAR64LONG16 *);
00185 static void do_R3(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c,
00186                   isc_uint32_t *d, isc_uint32_t *e, CHAR64LONG16 *);
00187 static void do_R4(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c,
00188                   isc_uint32_t *d, isc_uint32_t *e, CHAR64LONG16 *);
00189 
00190 #define nR0(v,w,x,y,z,i) R0(*v,*w,*x,*y,*z,i)
00191 #define nR1(v,w,x,y,z,i) R1(*v,*w,*x,*y,*z,i)
00192 #define nR2(v,w,x,y,z,i) R2(*v,*w,*x,*y,*z,i)
00193 #define nR3(v,w,x,y,z,i) R3(*v,*w,*x,*y,*z,i)
00194 #define nR4(v,w,x,y,z,i) R4(*v,*w,*x,*y,*z,i)
00195 
00196 static void
00197 do_R01(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c, isc_uint32_t *d,
00198        isc_uint32_t *e, CHAR64LONG16 *block)
00199 {
00200         nR0(a,b,c,d,e, 0); nR0(e,a,b,c,d, 1); nR0(d,e,a,b,c, 2);
00201         nR0(c,d,e,a,b, 3); nR0(b,c,d,e,a, 4); nR0(a,b,c,d,e, 5);
00202         nR0(e,a,b,c,d, 6); nR0(d,e,a,b,c, 7); nR0(c,d,e,a,b, 8);
00203         nR0(b,c,d,e,a, 9); nR0(a,b,c,d,e,10); nR0(e,a,b,c,d,11);
00204         nR0(d,e,a,b,c,12); nR0(c,d,e,a,b,13); nR0(b,c,d,e,a,14);
00205         nR0(a,b,c,d,e,15); nR1(e,a,b,c,d,16); nR1(d,e,a,b,c,17);
00206         nR1(c,d,e,a,b,18); nR1(b,c,d,e,a,19);
00207 }
00208 
00209 static void
00210 do_R2(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c, isc_uint32_t *d,
00211       isc_uint32_t *e, CHAR64LONG16 *block)
00212 {
00213         nR2(a,b,c,d,e,20); nR2(e,a,b,c,d,21); nR2(d,e,a,b,c,22);
00214         nR2(c,d,e,a,b,23); nR2(b,c,d,e,a,24); nR2(a,b,c,d,e,25);
00215         nR2(e,a,b,c,d,26); nR2(d,e,a,b,c,27); nR2(c,d,e,a,b,28);
00216         nR2(b,c,d,e,a,29); nR2(a,b,c,d,e,30); nR2(e,a,b,c,d,31);
00217         nR2(d,e,a,b,c,32); nR2(c,d,e,a,b,33); nR2(b,c,d,e,a,34);
00218         nR2(a,b,c,d,e,35); nR2(e,a,b,c,d,36); nR2(d,e,a,b,c,37);
00219         nR2(c,d,e,a,b,38); nR2(b,c,d,e,a,39);
00220 }
00221 
00222 static void
00223 do_R3(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c, isc_uint32_t *d,
00224       isc_uint32_t *e, CHAR64LONG16 *block)
00225 {
00226         nR3(a,b,c,d,e,40); nR3(e,a,b,c,d,41); nR3(d,e,a,b,c,42);
00227         nR3(c,d,e,a,b,43); nR3(b,c,d,e,a,44); nR3(a,b,c,d,e,45);
00228         nR3(e,a,b,c,d,46); nR3(d,e,a,b,c,47); nR3(c,d,e,a,b,48);
00229         nR3(b,c,d,e,a,49); nR3(a,b,c,d,e,50); nR3(e,a,b,c,d,51);
00230         nR3(d,e,a,b,c,52); nR3(c,d,e,a,b,53); nR3(b,c,d,e,a,54);
00231         nR3(a,b,c,d,e,55); nR3(e,a,b,c,d,56); nR3(d,e,a,b,c,57);
00232         nR3(c,d,e,a,b,58); nR3(b,c,d,e,a,59);
00233 }
00234 
00235 static void
00236 do_R4(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c, isc_uint32_t *d,
00237       isc_uint32_t *e, CHAR64LONG16 *block)
00238 {
00239         nR4(a,b,c,d,e,60); nR4(e,a,b,c,d,61); nR4(d,e,a,b,c,62);
00240         nR4(c,d,e,a,b,63); nR4(b,c,d,e,a,64); nR4(a,b,c,d,e,65);
00241         nR4(e,a,b,c,d,66); nR4(d,e,a,b,c,67); nR4(c,d,e,a,b,68);
00242         nR4(b,c,d,e,a,69); nR4(a,b,c,d,e,70); nR4(e,a,b,c,d,71);
00243         nR4(d,e,a,b,c,72); nR4(c,d,e,a,b,73); nR4(b,c,d,e,a,74);
00244         nR4(a,b,c,d,e,75); nR4(e,a,b,c,d,76); nR4(d,e,a,b,c,77);
00245         nR4(c,d,e,a,b,78); nR4(b,c,d,e,a,79);
00246 }
00247 #endif
00248 
00249 /*!
00250  * Hash a single 512-bit block. This is the core of the algorithm.
00251  */
00252 static void
00253 transform(isc_uint32_t state[5], const unsigned char buffer[64]) {
00254         isc_uint32_t a, b, c, d, e;
00255         CHAR64LONG16 *block;
00256         CHAR64LONG16 workspace;
00257 
00258         INSIST(buffer != NULL);
00259         INSIST(state != NULL);
00260 
00261         block = &workspace;
00262         (void)memmove(block, buffer, 64);
00263 
00264         /* Copy context->state[] to working vars */
00265         a = state[0];
00266         b = state[1];
00267         c = state[2];
00268         d = state[3];
00269         e = state[4];
00270 
00271 #ifdef __sparc_v9__
00272         do_R01(&a, &b, &c, &d, &e, block);
00273         do_R2(&a, &b, &c, &d, &e, block);
00274         do_R3(&a, &b, &c, &d, &e, block);
00275         do_R4(&a, &b, &c, &d, &e, block);
00276 #else
00277         /* 4 rounds of 20 operations each. Loop unrolled. */
00278         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);
00279         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);
00280         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);
00281         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);
00282         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);
00283         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);
00284         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);
00285         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);
00286         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);
00287         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);
00288         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);
00289         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);
00290         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);
00291         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);
00292         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);
00293         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);
00294         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);
00295         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);
00296         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);
00297         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);
00298 #endif
00299 
00300         /* Add the working vars back into context.state[] */
00301         state[0] += a;
00302         state[1] += b;
00303         state[2] += c;
00304         state[3] += d;
00305         state[4] += e;
00306 
00307         /* Wipe variables */
00308         a = b = c = d = e = 0;
00309         /* Avoid compiler warnings */
00310         POST(a); POST(b); POST(c); POST(d); POST(e);
00311 }
00312 
00313 
00314 /*!
00315  * isc_sha1_init - Initialize new context
00316  */
00317 void
00318 isc_sha1_init(isc_sha1_t *context)
00319 {
00320         INSIST(context != NULL);
00321 
00322         /* SHA1 initialization constants */
00323         context->state[0] = 0x67452301;
00324         context->state[1] = 0xEFCDAB89;
00325         context->state[2] = 0x98BADCFE;
00326         context->state[3] = 0x10325476;
00327         context->state[4] = 0xC3D2E1F0;
00328         context->count[0] = 0;
00329         context->count[1] = 0;
00330 }
00331 
00332 void
00333 isc_sha1_invalidate(isc_sha1_t *context) {
00334         memset(context, 0, sizeof(isc_sha1_t));
00335 }
00336 
00337 /*!
00338  * Run your data through this.
00339  */
00340 void
00341 isc_sha1_update(isc_sha1_t *context, const unsigned char *data,
00342                 unsigned int len)
00343 {
00344         unsigned int i, j;
00345 
00346         INSIST(context != 0);
00347         INSIST(data != 0);
00348 
00349         j = context->count[0];
00350         if ((context->count[0] += len << 3) < j)
00351                 context->count[1] += (len >> 29) + 1;
00352         j = (j >> 3) & 63;
00353         if ((j + len) > 63) {
00354                 (void)memmove(&context->buffer[j], data, (i = 64 - j));
00355                 transform(context->state, context->buffer);
00356                 for (; i + 63 < len; i += 64)
00357                         transform(context->state, &data[i]);
00358                 j = 0;
00359         } else {
00360                 i = 0;
00361         }
00362 
00363         (void)memmove(&context->buffer[j], &data[i], len - i);
00364 }
00365 
00366 
00367 /*!
00368  * Add padding and return the message digest.
00369  */
00370 
00371 static const unsigned char final_200 = 128;
00372 static const unsigned char final_0 = 0;
00373 
00374 void
00375 isc_sha1_final(isc_sha1_t *context, unsigned char *digest) {
00376         unsigned int i;
00377         unsigned char finalcount[8];
00378 
00379         INSIST(digest != 0);
00380         INSIST(context != 0);
00381 
00382         for (i = 0; i < 8; i++) {
00383                 /* Endian independent */
00384                 finalcount[i] = (unsigned char)
00385                         ((context->count[(i >= 4 ? 0 : 1)]
00386                           >> ((3 - (i & 3)) * 8)) & 255);
00387         }
00388 
00389         isc_sha1_update(context, &final_200, 1);
00390         while ((context->count[0] & 504) != 448)
00391                 isc_sha1_update(context, &final_0, 1);
00392         /* The next Update should cause a transform() */
00393         isc_sha1_update(context, finalcount, 8);
00394 
00395         if (digest) {
00396                 for (i = 0; i < 20; i++)
00397                         digest[i] = (unsigned char)
00398                                 ((context->state[i >> 2]
00399                                   >> ((3 - (i & 3)) * 8)) & 255);
00400         }
00401 
00402         memset(context, 0, sizeof(isc_sha1_t));
00403 }
00404 #endif

Generated on Tue Apr 28 17:41:05 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha