Back to index

openldap  2.4.31
sha1.c
Go to the documentation of this file.
00001 /* $OpenLDAP$ */
00002 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00003  *
00004  * Copyright 1998-2012 The OpenLDAP Foundation.
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted only as authorized by the OpenLDAP
00009  * Public License.
00010  *
00011  * A copy of this license is available in the file LICENSE in the
00012  * top-level directory of the distribution or, alternatively, at
00013  * <http://www.OpenLDAP.org/license.html>.
00014  */
00015 /* This work was derived from code developed by Steve Reid and
00016  * adapted for use in OpenLDAP by Kurt D. Zeilenga.
00017  */
00018 
00019 
00020 /*     Acquired from:
00021  *     $OpenBSD: sha1.c,v 1.9 1997/07/23 21:12:32 kstailey Exp $      */
00022 /*
00023  * SHA-1 in C
00024  * By Steve Reid <steve@edmweb.com>
00025  * 100% Public Domain
00026  *
00027  * Test Vectors (from FIPS PUB 180-1)
00028  * "abc"
00029  *   A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
00030  * "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
00031  *   84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
00032  * A million repetitions of "a"
00033  *   34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
00034  */
00035 /*
00036  * This code assumes uint32 is 32 bits and char is 8 bits
00037  */
00038 
00039 #include "portable.h"
00040 #include <ac/param.h>
00041 #include <ac/string.h>
00042 #include <ac/socket.h>
00043 #include <ac/bytes.h>
00044 
00045 #include "lutil_sha1.h"
00046 
00047 #ifdef LUTIL_SHA1_BYTES
00048 
00049 /* undefining this will cause pointer alignment errors */
00050 #define SHA1HANDSOFF        /* Copies data before messing with it. */
00051 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
00052 
00053 /*
00054  * blk0() and blk() perform the initial expand.
00055  * I got the idea of expanding during the round function from SSLeay
00056  */
00057 #if BYTE_ORDER == LITTLE_ENDIAN
00058 # define blk0(i) (block[i] = (rol(block[i],24)&0xFF00FF00) \
00059     |(rol(block[i],8)&0x00FF00FF))
00060 #else
00061 # define blk0(i) block[i]
00062 #endif
00063 #define blk(i) (block[i&15] = rol(block[(i+13)&15]^block[(i+8)&15] \
00064     ^block[(i+2)&15]^block[i&15],1))
00065 
00066 /*
00067  * (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1
00068  */
00069 #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
00070 #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
00071 #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
00072 #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
00073 #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
00074 
00075 
00076 /*
00077  * Hash a single 512-bit block. This is the core of the algorithm.
00078  */
00079 void
00080 lutil_SHA1Transform( uint32 *state, const unsigned char *buffer )
00081 {
00082     uint32 a, b, c, d, e;
00083 
00084 #ifdef SHA1HANDSOFF
00085     uint32 block[16];
00086     (void)AC_MEMCPY(block, buffer, 64);
00087 #else
00088     uint32 *block = (u_int32 *) buffer;
00089 #endif
00090 
00091     /* Copy context->state[] to working vars */
00092     a = state[0];
00093     b = state[1];
00094     c = state[2];
00095     d = state[3];
00096     e = state[4];
00097 
00098     /* 4 rounds of 20 operations each. Loop unrolled. */
00099     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);
00100     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);
00101     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);
00102     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);
00103     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);
00104     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);
00105     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);
00106     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);
00107     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);
00108     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);
00109     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);
00110     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);
00111     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);
00112     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);
00113     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);
00114     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);
00115     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);
00116     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);
00117     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);
00118     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);
00119 
00120     /* Add the working vars back into context.state[] */
00121     state[0] += a;
00122     state[1] += b;
00123     state[2] += c;
00124     state[3] += d;
00125     state[4] += e;
00126 
00127     /* Wipe variables */
00128     a = b = c = d = e = 0;
00129 }
00130 
00131 
00132 /*
00133  * lutil_SHA1Init - Initialize new context
00134  */
00135 void
00136 lutil_SHA1Init( lutil_SHA1_CTX *context )
00137 {
00138 
00139     /* SHA1 initialization constants */
00140     context->state[0] = 0x67452301;
00141     context->state[1] = 0xEFCDAB89;
00142     context->state[2] = 0x98BADCFE;
00143     context->state[3] = 0x10325476;
00144     context->state[4] = 0xC3D2E1F0;
00145     context->count[0] = context->count[1] = 0;
00146 }
00147 
00148 
00149 /*
00150  * Run your data through this.
00151  */
00152 void
00153 lutil_SHA1Update(
00154     lutil_SHA1_CTX   *context,
00155     const unsigned char     *data,
00156     uint32           len
00157 )
00158 {
00159     u_int i, j;
00160 
00161     j = context->count[0];
00162     if ((context->count[0] += len << 3) < j)
00163        context->count[1] += (len>>29)+1;
00164     j = (j >> 3) & 63;
00165     if ((j + len) > 63) {
00166        (void)AC_MEMCPY(&context->buffer[j], data, (i = 64-j));
00167        lutil_SHA1Transform(context->state, context->buffer);
00168        for ( ; i + 63 < len; i += 64)
00169            lutil_SHA1Transform(context->state, &data[i]);
00170        j = 0;
00171     } else {
00172        i = 0;
00173     }
00174     (void)AC_MEMCPY(&context->buffer[j], &data[i], len - i);
00175 }
00176 
00177 
00178 /*
00179  * Add padding and return the message digest.
00180  */
00181 void
00182 lutil_SHA1Final( unsigned char *digest, lutil_SHA1_CTX *context )
00183 {
00184     u_int i;
00185     unsigned char finalcount[8];
00186 
00187     for (i = 0; i < 8; i++) {
00188        finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)]
00189         >> ((3-(i & 3)) * 8) ) & 255);     /* Endian independent */
00190     }
00191     lutil_SHA1Update(context, (unsigned char *)"\200", 1);
00192     while ((context->count[0] & 504) != 448)
00193        lutil_SHA1Update(context, (unsigned char *)"\0", 1);
00194     lutil_SHA1Update(context, finalcount, 8);  /* Should cause a SHA1Transform() */
00195 
00196     if (digest) {
00197        for (i = 0; i < 20; i++)
00198            digest[i] = (unsigned char)
00199               ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
00200     }
00201 }
00202 
00203 
00204 /* sha1hl.c
00205  * ----------------------------------------------------------------------------
00206  * "THE BEER-WARE LICENSE" (Revision 42):
00207  * <phk@login.dkuug.dk> wrote this file.  As long as you retain this notice you
00208  * can do whatever you want with this stuff. If we meet some day, and you think
00209  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
00210  * ----------------------------------------------------------------------------
00211  */
00212 
00213 #if defined(LIBC_SCCS) && !defined(lint)
00214 static char rcsid[] = "$OpenBSD: sha1hl.c,v 1.1 1997/07/12 20:06:03 millert Exp $";
00215 #endif /* LIBC_SCCS and not lint */
00216 
00217 #include <stdio.h>
00218 #include <ac/stdlib.h>
00219 
00220 #include <ac/errno.h>
00221 #include <ac/unistd.h>
00222 
00223 #ifdef HAVE_SYS_FILE_H
00224 #include <sys/file.h>
00225 #endif
00226 
00227 #ifdef HAVE_IO_H
00228 #include <io.h>
00229 #endif
00230 
00231 #ifdef HAVE_FCNTL_H
00232 #include <fcntl.h>
00233 #endif
00234 
00235 
00236 /* ARGSUSED */
00237 char *
00238 lutil_SHA1End( lutil_SHA1_CTX *ctx, char *buf )
00239 {
00240     int i;
00241     char *p = buf;
00242     unsigned char digest[20];
00243     static const char hex[]="0123456789abcdef";
00244 
00245     if (p == NULL && (p = malloc(41)) == NULL)
00246        return 0;
00247 
00248     lutil_SHA1Final(digest,ctx);
00249     for (i = 0; i < 20; i++) {
00250        p[i + i] = hex[digest[i] >> 4];
00251        p[i + i + 1] = hex[digest[i] & 0x0f];
00252     }
00253     p[i + i] = '\0';
00254     return(p);
00255 }
00256 
00257 char *
00258 lutil_SHA1File( char *filename, char *buf )
00259 {
00260     unsigned char buffer[BUFSIZ];
00261     lutil_SHA1_CTX ctx;
00262     int fd, num, oerrno;
00263 
00264     lutil_SHA1Init(&ctx);
00265 
00266     if ((fd = open(filename,O_RDONLY)) < 0)
00267        return(0);
00268 
00269     while ((num = read(fd, buffer, sizeof(buffer))) > 0)
00270        lutil_SHA1Update(&ctx, buffer, num);
00271 
00272     oerrno = errno;
00273     close(fd);
00274     errno = oerrno;
00275     return(num < 0 ? 0 : lutil_SHA1End(&ctx, buf));
00276 }
00277 
00278 char *
00279 lutil_SHA1Data( const unsigned char *data, size_t len, char *buf )
00280 {
00281     lutil_SHA1_CTX ctx;
00282 
00283     lutil_SHA1Init(&ctx);
00284     lutil_SHA1Update(&ctx, data, len);
00285     return(lutil_SHA1End(&ctx, buf));
00286 }
00287 
00288 #endif