Changeset 407 for branches/thune

Show
Ignore:
Timestamp:
06/08/07 23:06:09 (18 months ago)
Author:
krobillard
Message:

Thune - Fixed SHA1 checksum.

Location:
branches/thune/thread_safe
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • branches/thune/thread_safe/files.c

    r389 r407  
    781781                bin->used = 20; 
    782782 
    783                 SHA1Init( &context ); 
    784                 SHA1Update( &context, (uint8_t*) cpA, cpB - cpA ); 
    785                 SHA1Final( bin->ptr.b, &context ); 
    786                  
     783                SHA1_Init( &context ); 
     784                SHA1_Update( &context, (uint8_t*) cpA, cpB - cpA ); 
     785                SHA1_Final( &context, bin->ptr.b ); 
     786 
    787787                ur_initType(res, UT_BINARY); 
    788788                ur_setSeries(res, binN, 0); 
  • branches/thune/thread_safe/support/sha1.c

    r299 r407  
    11/* 
    22SHA-1 in C 
    3 By Steve Reid <steve@edmweb.com> 
     3By Steve Reid <sreid@sea-to-sky.net> 
    44100% Public Domain 
    55 
     6----------------- 
     7Modified 7/98  
     8By James H. Brown <jbrown@burgoyne.com> 
     9Still 100% Public Domain 
     10 
     11Corrected a problem which generated improper hash values on 16 bit machines 
     12Routine SHA1Update changed from 
     13        void SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned int 
     14len) 
     15to 
     16        void SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned 
     17long len) 
     18 
     19The 'len' parameter was declared an int which works fine on 32 bit machines. 
     20However, on 16 bit machines an int is too small for the shifts being done 
     21against 
     22it.  This caused the hash function to generate incorrect values if len was 
     23greater than 8191 (8K - 1) due to the 'len << 3' on line 3 of SHA1Update(). 
     24 
     25Since the file IO in main() reads 16K at a time, any file 8K or larger would 
     26be guaranteed to generate the wrong hash (e.g. Test Vector #3, a million 
     27"a"s). 
     28 
     29I also changed the declaration of variables i & j in SHA1Update to  
     30unsigned long from unsigned int for the same reason. 
     31 
     32These changes should make no difference to any 32 bit implementations since 
     33an 
     34int and a long are the same size in those environments. 
     35 
     36-- 
     37I also corrected a few compiler warnings generated by Borland C. 
     381. Added #include <process.h> for exit() prototype 
     392. Removed unused variable 'j' in SHA1Final 
     403. Changed exit(0) to return(0) at end of main. 
     41 
     42ALL changes I made can be located by searching for comments containing 'JHB' 
     43----------------- 
     44Modified 8/98 
     45By Steve Reid <sreid@sea-to-sky.net> 
     46Still 100% public domain 
     47 
     481- Removed #include <process.h> and used return() instead of exit() 
     492- Fixed overwriting of finalcount in SHA1Final() (discovered by Chris Hall) 
     503- Changed email address from steve@edmweb.com to sreid@sea-to-sky.net 
     51 
     52----------------- 
     53Modified 4/01 
     54By Saul Kravitz <Saul.Kravitz@celera.com> 
     55Still 100% PD 
     56Modified to run on Compaq Alpha hardware.   
     57 
     58----------------- 
     59Modified 07/2002 
     60By Ralph Giles <giles@artofcode.com> 
     61Still 100% public domain 
     62modified for use with stdint types, autoconf 
     63code cleanup, removed attribution comments 
     64switched SHA1Final() argument order for consistency 
     65use SHA1_ prefix for public api 
     66move public api to sha1.h 
     67*/ 
     68 
     69/* 
    670Test Vectors (from FIPS PUB 180-1) 
    771"abc" 
     
    1377*/ 
    1478 
    15 /* #define LITTLE_ENDIAN * This should be #define'd if true. */ 
     79/* #define __BIG_ENDIAN__ * This should be #define'd if true. */ 
    1680/* #define SHA1HANDSOFF * Copies data before messing with it. */ 
    1781 
    18 #include <stdio.h> 
     82 
    1983#include <string.h> 
     84#include <stdint.h> 
     85 
    2086 
    2187typedef struct { 
    22     unsigned long state[5]; 
    23     unsigned long count[2]; 
    24     unsigned char buffer[64]; 
     88    uint32_t state[5]; 
     89    uint32_t count[2]; 
     90    uint8_t buffer[64]; 
    2591} SHA1_CTX; 
    2692 
    27 void SHA1Transform(unsigned long state[5], unsigned char buffer[64]); 
    28 void SHA1Init(SHA1_CTX* context); 
    29 void SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned int len); 
    30 void SHA1Final(unsigned char digest[20], SHA1_CTX* context); 
     93 
     94#define SHA1_DIGEST_SIZE 20 
    3195 
    3296#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) 
     
    3498/* blk0() and blk() perform the initial expand. */ 
    3599/* I got the idea of expanding during the round function from SSLeay */ 
    36 #ifdef LITTLE_ENDIAN 
     100/* FIXME: can we do this in an endian-proof way? */ 
     101#ifdef __BIG_ENDIAN__ 
     102#define blk0(i) block->l[i] 
     103#else 
    37104#define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \ 
    38105    |(rol(block->l[i],8)&0x00FF00FF)) 
    39 #else 
    40 #define blk0(i) block->l[i] 
    41106#endif 
    42107#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \ 
     
    52117 
    53118/* Hash a single 512-bit block. This is the core of the algorithm. */ 
    54  
    55 void SHA1Transform(unsigned long state[5], unsigned char buffer[64]) 
    56 { 
    57 unsigned long a, b, c, d, e; 
    58 typedef union { 
    59     unsigned char c[64]; 
    60     unsigned long l[16]; 
    61 } CHAR64LONG16; 
    62 CHAR64LONG16* block; 
     119void SHA1_Transform(uint32_t state[5], const uint8_t buffer[64]) 
     120{ 
     121    uint32_t a, b, c, d, e; 
     122    typedef union { 
     123        uint8_t c[64]; 
     124        uint32_t l[16]; 
     125    } CHAR64LONG16; 
     126    CHAR64LONG16* block; 
     127 
    63128#ifdef SHA1HANDSOFF 
    64 static unsigned char workspace[64]; 
     129    static uint8_t workspace[64]; 
    65130    block = (CHAR64LONG16*)workspace; 
    66131    memcpy(block, buffer, 64); 
     
    68133    block = (CHAR64LONG16*)buffer; 
    69134#endif 
     135 
    70136    /* Copy context->state[] to working vars */ 
    71137    a = state[0]; 
     
    74140    d = state[3]; 
    75141    e = state[4]; 
     142 
    76143    /* 4 rounds of 20 operations each. Loop unrolled. */ 
    77144    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); 
     
    95162    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); 
    96163    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); 
     164 
    97165    /* Add the working vars back into context.state[] */ 
    98166    state[0] += a; 
     
    101169    state[3] += d; 
    102170    state[4] += e; 
     171 
    103172    /* Wipe variables */ 
    104173    a = b = c = d = e = 0; 
     
    107176 
    108177/* SHA1Init - Initialize new context */ 
    109  
    110 void SHA1Init(SHA1_CTX* context) 
     178void SHA1_Init(SHA1_CTX* context) 
    111179{ 
    112180    /* SHA1 initialization constants */ 
     
    121189 
    122190/* Run your data through this. */ 
    123  
    124 void SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned int len) 
    125 { 
    126 unsigned int i, j; 
     191void SHA1_Update(SHA1_CTX* context, const uint8_t* data, const size_t len) 
     192{ 
     193    size_t i, j; 
    127194 
    128195    j = (context->count[0] >> 3) & 63; 
     
    131198    if ((j + len) > 63) { 
    132199        memcpy(&context->buffer[j], data, (i = 64-j)); 
    133         SHA1Transform(context->state, context->buffer); 
     200        SHA1_Transform(context->state, context->buffer); 
    134201        for ( ; i + 63 < len; i += 64) { 
    135             SHA1Transform(context->state, &data[i]); 
     202            SHA1_Transform(context->state, data + i); 
    136203        } 
    137204        j = 0; 
     
    143210 
    144211/* Add padding and return the message digest. */ 
    145  
    146 void SHA1Final(unsigned char digest[20], SHA1_CTX* context) 
    147 { 
    148 unsigned long i, j; 
    149 unsigned char finalcount[8]; 
     212void SHA1_Final(SHA1_CTX* context, uint8_t digest[SHA1_DIGEST_SIZE]) 
     213{ 
     214    uint32_t i; 
     215    uint8_t  finalcount[8]; 
    150216 
    151217    for (i = 0; i < 8; i++) { 
     
    153219         >> ((3-(i & 3)) * 8) ) & 255);  /* Endian independent */ 
    154220    } 
    155     SHA1Update(context, (unsigned char *)"\200", 1); 
     221    SHA1_Update(context, (uint8_t *)"\200", 1); 
    156222    while ((context->count[0] & 504) != 448) { 
    157         SHA1Update(context, (unsigned char *)"\0", 1); 
    158     } 
    159     SHA1Update(context, finalcount, 8);  /* Should cause a SHA1Transform() */ 
    160     for (i = 0; i < 20; i++) { 
    161         digest[i] = (unsigned char) 
     223        SHA1_Update(context, (uint8_t *)"\0", 1); 
     224    } 
     225    SHA1_Update(context, finalcount, 8);  /* Should cause a SHA1_Transform() */ 
     226    for (i = 0; i < SHA1_DIGEST_SIZE; i++) { 
     227        digest[i] = (uint8_t) 
    162228         ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); 
    163229    } 
     230     
    164231    /* Wipe variables */ 
    165     i = j = 0; 
     232    i = 0; 
    166233    memset(context->buffer, 0, 64); 
    167234    memset(context->state, 0, 20); 
    168235    memset(context->count, 0, 8); 
    169     memset(&finalcount, 0, 8); 
    170 #ifdef SHA1HANDSOFF  /* make SHA1Transform overwrite it's own static vars */ 
    171     SHA1Transform(context->state, context->buffer); 
    172 #endif 
    173 } 
    174  
    175  
     236    memset(finalcount, 0, 8);   /* SWR */ 
     237 
     238#ifdef SHA1HANDSOFF  /* make SHA1Transform overwrite its own static vars */ 
     239    SHA1_Transform(context->state, context->buffer); 
     240#endif 
     241} 
     242   
    176243/*************************************************************/ 
    177244 
    178  
    179245#ifdef UNIT_TEST 
     246#include <stdio.h> 
     247 
    180248int main(int argc, char** argv) 
    181249{ 
    182250int i, j; 
    183251SHA1_CTX context; 
    184 unsigned char digest[20], buffer[16384]; 
     252unsigned char digest[SHA1_DIGEST_SIZE], buffer[16384]; 
    185253FILE* file; 
    186254 
    187255    if (argc > 2) { 
    188         puts("Public domain SHA-1 implementation - by Steve Reid <steve@edmweb.com>"); 
     256        puts("Public domain SHA-1 implementation - by Steve Reid <sreid@sea-to-sky.net>"); 
     257        puts("Modified for 16 bit environments 7/98 - by James H. Brown <jbrown@burgoyne.com>");        /* JHB */ 
    189258        puts("Produces the SHA-1 hash of a file, or stdin if no file is specified."); 
    190         exit(0); 
     259        return(0); 
    191260    } 
    192261    if (argc < 2) { 
     
    196265        if (!(file = fopen(argv[1], "rb"))) { 
    197266            fputs("Unable to open file.", stderr); 
    198             exit(-1); 
     267            return(-1); 
    199268        } 
    200269    }  
    201     SHA1Init(&context); 
     270    SHA1_Init(&context); 
    202271    while (!feof(file)) {  /* note: what if ferror(file) */ 
    203272        i = fread(buffer, 1, 16384, file); 
    204         SHA1Update(&context, buffer, i); 
    205     } 
    206     SHA1Final(digest, &context); 
     273        SHA1_Update(&context, buffer, i); 
     274    } 
     275    SHA1_Final(&context, digest); 
    207276    fclose(file); 
    208     for (i = 0; i < 5; i++) { 
     277    for (i = 0; i < SHA1_DIGEST_SIZE/4; i++) { 
    209278        for (j = 0; j < 4; j++) { 
    210279            printf("%02X", digest[i*4+j]); 
     
    213282    } 
    214283    putchar('\n'); 
    215     exit(0); 
    216 } 
    217 #endif 
     284    return(0);  /* JHB */ 
     285} 
     286#endif