sha1.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 18 Jun 2017 19:50:43 -0400
changeset 57 4974e5368a29
parent 53 ea43dbad123b
permissions -rw-r--r--
Minor type cleanup ("unsigned char" -> "uint8_t").
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/*	$OpenBSD: sha1.c,v 1.9 2011/01/11 15:50:40 deraadt Exp $	*/
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
/*
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
 * SHA-1 in C
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
 * By Steve Reid <steve@edmweb.com>
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
 * 100% Public Domain
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
 * 
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
 * Test Vectors (from FIPS PUB 180-1)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
 * "abc"
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
 *   A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
 * "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
 *   84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
 * A million repetitions of "a"
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
 *   34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
*/
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
/* #define LITTLE_ENDIAN * This should be #define'd already, if true. */
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
/* #define SHA1HANDSOFF * Copies data before messing with it. */
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
#define SHA1HANDSOFF
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
#include <string.h>
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
#include <sys/param.h>
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
//#include <sys/systm.h>
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
#include "sha1.h"
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
/* blk0() and blk() perform the initial expand. */
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
/* I got the idea of expanding during the round function from SSLeay */
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
#if PLATFORM_LITTLEENDIAN
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
#define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
    |(rol(block->l[i],8)&0x00FF00FF))
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
#else
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
#define blk0(i) block->l[i]
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
#endif
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
    ^block->l[(i+2)&15]^block->l[i&15],1))
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
#define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
#define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
#define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
#define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
#define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
/* Hash a single 512-bit block. This is the core of the algorithm. */
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
void
52
7f21abb9ff17 Made the SHA1 code more stdint-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
    51
SHA1Transform(uint32_t state[5], const uint8_t buffer[SHA1_BLOCK_LENGTH])
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
{
52
7f21abb9ff17 Made the SHA1 code more stdint-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
    53
    uint32_t a, b, c, d, e;
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
    typedef union {
52
7f21abb9ff17 Made the SHA1 code more stdint-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
    55
        uint8_t c[64];
7f21abb9ff17 Made the SHA1 code more stdint-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
    56
        uint32_t l[16];
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
    } CHAR64LONG16;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
    CHAR64LONG16* block;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
#ifdef SHA1HANDSOFF
52
7f21abb9ff17 Made the SHA1 code more stdint-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
    60
    uint8_t workspace[SHA1_BLOCK_LENGTH];
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
    block = (CHAR64LONG16 *)workspace;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
    memcpy(block, buffer, SHA1_BLOCK_LENGTH);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
#else
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
    block = (CHAR64LONG16 *)buffer;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
#endif
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
    /* Copy context->state[] to working vars */
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
    a = state[0];
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
    b = state[1];
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
    c = state[2];
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
    d = state[3];
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
    e = state[4];
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
    /* 4 rounds of 20 operations each. Loop unrolled. */
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
    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);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
    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);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
    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);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
    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);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
    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);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
    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);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
    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);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
    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);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
    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);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
    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);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
    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);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
    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);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
    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);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
    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);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
    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);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
    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);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
    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);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
    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);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
    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);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
    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);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
    /* Add the working vars back into context.state[] */
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
    state[0] += a;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
    state[1] += b;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
    state[2] += c;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
    state[3] += d;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
    state[4] += e;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
    /* Wipe variables */
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
    a = b = c = d = e = 0;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
}
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
/* SHA1Init - Initialize new context */
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
void
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
SHA1Init(SHA1_CTX *context)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
{
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
    /* SHA1 initialization constants */
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
    context->count = 0;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
    context->state[0] = 0x67452301;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   115
    context->state[1] = 0xEFCDAB89;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
    context->state[2] = 0x98BADCFE;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
    context->state[3] = 0x10325476;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
    context->state[4] = 0xC3D2E1F0;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
}
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   120
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   122
/* Run your data through this. */
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
void
52
7f21abb9ff17 Made the SHA1 code more stdint-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   125
SHA1Update(SHA1_CTX *context, const uint8_t *data, const uint32_t len)
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   126
{
52
7f21abb9ff17 Made the SHA1 code more stdint-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   127
    uint32_t i;
7f21abb9ff17 Made the SHA1 code more stdint-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   128
    uint32_t j;
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   129
52
7f21abb9ff17 Made the SHA1 code more stdint-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   130
    j = (uint32_t)((context->count >> 3) & 63);
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   131
    context->count += (len << 3);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
    if ((j + len) > 63) {
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   133
        memcpy(&context->buffer[j], data, (i = 64 - j));
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   134
        SHA1Transform(context->state, context->buffer);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   135
        for ( ; i + 63 < len; i += 64) {
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   136
            SHA1Transform(context->state, &data[i]);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
        }
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
        j = 0;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
    }
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
    else i = 0;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   141
    memcpy(&context->buffer[j], &data[i], len - i);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   142
}
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   143
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
/* Add padding and return the message digest. */
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   147
void
52
7f21abb9ff17 Made the SHA1 code more stdint-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   148
SHA1Final(uint8_t digest[SHA1_DIGEST_LENGTH], SHA1_CTX *context)
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   149
{
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
    unsigned int i;
52
7f21abb9ff17 Made the SHA1 code more stdint-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   151
    uint8_t finalcount[8];
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   152
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   153
    for (i = 0; i < 8; i++) {
52
7f21abb9ff17 Made the SHA1 code more stdint-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   154
        finalcount[i] = (uint8_t)((context->count >>
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   155
            ((7 - (i & 7)) * 8)) & 255);  /* Endian independent */
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   156
    }
52
7f21abb9ff17 Made the SHA1 code more stdint-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   157
    SHA1Update(context, (uint8_t *)"\200", 1);
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
    while ((context->count & 504) != 448) {
52
7f21abb9ff17 Made the SHA1 code more stdint-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   159
        SHA1Update(context, (uint8_t *)"\0", 1);
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   160
    }
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
    SHA1Update(context, finalcount, 8);  /* Should cause a SHA1Transform() */
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   162
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   163
    if (digest)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   164
        for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
52
7f21abb9ff17 Made the SHA1 code more stdint-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   165
            digest[i] = (uint8_t)((context->state[i >> 2] >>
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   166
                ((3 - (i & 3)) * 8)) & 255);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   167
      }
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   168
    memset(finalcount, '\0', 8);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   169
#if 0	/* We want to use this for "keyfill" */
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   170
    /* Wipe variables */
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   171
    i = 0;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   172
    bzero(context->buffer, 64);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   173
    bzero(context->state, 20);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   174
    bzero(context->count, 8);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   175
#ifdef SHA1HANDSOFF  /* make SHA1Transform overwrite its own static vars */
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   176
    SHA1Transform(context->state, context->buffer);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   177
#endif
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   178
#endif
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   179
}
53
ea43dbad123b Implemented SHA1Hmac().
Ryan C. Gordon <icculus@icculus.org>
parents: 52
diff changeset
   180
ea43dbad123b Implemented SHA1Hmac().
Ryan C. Gordon <icculus@icculus.org>
parents: 52
diff changeset
   181
ea43dbad123b Implemented SHA1Hmac().
Ryan C. Gordon <icculus@icculus.org>
parents: 52
diff changeset
   182
/* https://www.ietf.org/rfc/rfc2104.txt */
ea43dbad123b Implemented SHA1Hmac().
Ryan C. Gordon <icculus@icculus.org>
parents: 52
diff changeset
   183
void SHA1Hmac(const uint8_t *key, const uint32_t keylen, const uint8_t *msg, const uint32_t msglen, uint8_t digest[SHA1_DIGEST_LENGTH])
ea43dbad123b Implemented SHA1Hmac().
Ryan C. Gordon <icculus@icculus.org>
parents: 52
diff changeset
   184
{
ea43dbad123b Implemented SHA1Hmac().
Ryan C. Gordon <icculus@icculus.org>
parents: 52
diff changeset
   185
    SHA1_CTX sha1;
ea43dbad123b Implemented SHA1Hmac().
Ryan C. Gordon <icculus@icculus.org>
parents: 52
diff changeset
   186
    uint8_t block[64];  // 512 bits.
ea43dbad123b Implemented SHA1Hmac().
Ryan C. Gordon <icculus@icculus.org>
parents: 52
diff changeset
   187
    uint8_t xori[sizeof (block)];
ea43dbad123b Implemented SHA1Hmac().
Ryan C. Gordon <icculus@icculus.org>
parents: 52
diff changeset
   188
    uint8_t xoro[sizeof (block)];
ea43dbad123b Implemented SHA1Hmac().
Ryan C. Gordon <icculus@icculus.org>
parents: 52
diff changeset
   189
    int i;
ea43dbad123b Implemented SHA1Hmac().
Ryan C. Gordon <icculus@icculus.org>
parents: 52
diff changeset
   190
ea43dbad123b Implemented SHA1Hmac().
Ryan C. Gordon <icculus@icculus.org>
parents: 52
diff changeset
   191
    memset(block, '\0', sizeof (block));
ea43dbad123b Implemented SHA1Hmac().
Ryan C. Gordon <icculus@icculus.org>
parents: 52
diff changeset
   192
    if (keylen <= sizeof (block)) {
ea43dbad123b Implemented SHA1Hmac().
Ryan C. Gordon <icculus@icculus.org>
parents: 52
diff changeset
   193
        memcpy(block, key, keylen);
ea43dbad123b Implemented SHA1Hmac().
Ryan C. Gordon <icculus@icculus.org>
parents: 52
diff changeset
   194
    } else {
ea43dbad123b Implemented SHA1Hmac().
Ryan C. Gordon <icculus@icculus.org>
parents: 52
diff changeset
   195
        /* SHA-1 the key itself to shrink it down. */
ea43dbad123b Implemented SHA1Hmac().
Ryan C. Gordon <icculus@icculus.org>
parents: 52
diff changeset
   196
        SHA1Init(&sha1);
ea43dbad123b Implemented SHA1Hmac().
Ryan C. Gordon <icculus@icculus.org>
parents: 52
diff changeset
   197
        SHA1Update(&sha1, key, keylen);
ea43dbad123b Implemented SHA1Hmac().
Ryan C. Gordon <icculus@icculus.org>
parents: 52
diff changeset
   198
        SHA1Final(block, &sha1);
ea43dbad123b Implemented SHA1Hmac().
Ryan C. Gordon <icculus@icculus.org>
parents: 52
diff changeset
   199
    }
ea43dbad123b Implemented SHA1Hmac().
Ryan C. Gordon <icculus@icculus.org>
parents: 52
diff changeset
   200
ea43dbad123b Implemented SHA1Hmac().
Ryan C. Gordon <icculus@icculus.org>
parents: 52
diff changeset
   201
    for (i = 0; i < sizeof (block); i++) {
ea43dbad123b Implemented SHA1Hmac().
Ryan C. Gordon <icculus@icculus.org>
parents: 52
diff changeset
   202
        const uint8_t b = block[i];
ea43dbad123b Implemented SHA1Hmac().
Ryan C. Gordon <icculus@icculus.org>
parents: 52
diff changeset
   203
        xori[i] = b ^ 0x36;  /* XOR block vs ipad value */
ea43dbad123b Implemented SHA1Hmac().
Ryan C. Gordon <icculus@icculus.org>
parents: 52
diff changeset
   204
        xoro[i] = b ^ 0x5C;  /* XOR block vs opad value */
ea43dbad123b Implemented SHA1Hmac().
Ryan C. Gordon <icculus@icculus.org>
parents: 52
diff changeset
   205
    }
ea43dbad123b Implemented SHA1Hmac().
Ryan C. Gordon <icculus@icculus.org>
parents: 52
diff changeset
   206
ea43dbad123b Implemented SHA1Hmac().
Ryan C. Gordon <icculus@icculus.org>
parents: 52
diff changeset
   207
    SHA1Init(&sha1);
ea43dbad123b Implemented SHA1Hmac().
Ryan C. Gordon <icculus@icculus.org>
parents: 52
diff changeset
   208
    SHA1Update(&sha1, xori, sizeof (xori));
ea43dbad123b Implemented SHA1Hmac().
Ryan C. Gordon <icculus@icculus.org>
parents: 52
diff changeset
   209
    SHA1Update(&sha1, msg, msglen);
ea43dbad123b Implemented SHA1Hmac().
Ryan C. Gordon <icculus@icculus.org>
parents: 52
diff changeset
   210
    SHA1Final(block, &sha1);
ea43dbad123b Implemented SHA1Hmac().
Ryan C. Gordon <icculus@icculus.org>
parents: 52
diff changeset
   211
ea43dbad123b Implemented SHA1Hmac().
Ryan C. Gordon <icculus@icculus.org>
parents: 52
diff changeset
   212
    SHA1Init(&sha1);
ea43dbad123b Implemented SHA1Hmac().
Ryan C. Gordon <icculus@icculus.org>
parents: 52
diff changeset
   213
    SHA1Update(&sha1, xoro, sizeof (xoro));
ea43dbad123b Implemented SHA1Hmac().
Ryan C. Gordon <icculus@icculus.org>
parents: 52
diff changeset
   214
    SHA1Update(&sha1, block, SHA1_DIGEST_LENGTH);
ea43dbad123b Implemented SHA1Hmac().
Ryan C. Gordon <icculus@icculus.org>
parents: 52
diff changeset
   215
    SHA1Final(digest, &sha1);
ea43dbad123b Implemented SHA1Hmac().
Ryan C. Gordon <icculus@icculus.org>
parents: 52
diff changeset
   216
}
ea43dbad123b Implemented SHA1Hmac().
Ryan C. Gordon <icculus@icculus.org>
parents: 52
diff changeset
   217