sha.c revision 4f6e8d7a00cbeda1e70cc15be9c4af1018bdad53
1/* sha.c
2**
3** Copyright 2008, The Android Open Source Project
4**
5** Redistribution and use in source and binary forms, with or without
6** modification, are permitted provided that the following conditions are met:
7**     * Redistributions of source code must retain the above copyright
8**       notice, this list of conditions and the following disclaimer.
9**     * Redistributions in binary form must reproduce the above copyright
10**       notice, this list of conditions and the following disclaimer in the
11**       documentation and/or other materials provided with the distribution.
12**     * Neither the name of Google Inc. nor the names of its contributors may
13**       be used to endorse or promote products derived from this software
14**       without specific prior written permission.
15**
16** THIS SOFTWARE IS PROVIDED BY Google Inc. ``AS IS'' AND ANY EXPRESS OR
17** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19** EVENT SHALL Google Inc. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28#include "mincrypt/sha.h"
29
30#define rol(bits, value) (((value) << (bits)) | ((value) >> (32 - (bits))))
31
32static void SHA1_transform(SHA_CTX *ctx) {
33    uint32_t W[80];
34    uint32_t A, B, C, D, E;
35    uint8_t *p = ctx->buf;
36    int t;
37
38    for(t = 0; t < 16; ++t) {
39        uint32_t tmp =  *p++ << 24;
40        tmp |= *p++ << 16;
41        tmp |= *p++ << 8;
42        tmp |= *p++;
43        W[t] = tmp;
44    }
45
46    for(; t < 80; t++) {
47        W[t] = rol(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
48    }
49
50    A = ctx->state[0];
51    B = ctx->state[1];
52    C = ctx->state[2];
53    D = ctx->state[3];
54    E = ctx->state[4];
55
56    for(t = 0; t < 80; t++) {
57        uint32_t tmp = rol(5,A) + E + W[t];
58
59        if (t < 20)
60            tmp += (D^(B&(C^D))) + 0x5A827999;
61        else if ( t < 40)
62            tmp += (B^C^D) + 0x6ED9EBA1;
63        else if ( t < 60)
64            tmp += ((B&C)|(D&(B|C))) + 0x8F1BBCDC;
65        else
66            tmp += (B^C^D) + 0xCA62C1D6;
67
68        E = D;
69        D = C;
70        C = rol(30,B);
71        B = A;
72        A = tmp;
73    }
74
75    ctx->state[0] += A;
76    ctx->state[1] += B;
77    ctx->state[2] += C;
78    ctx->state[3] += D;
79    ctx->state[4] += E;
80}
81
82void SHA_init(SHA_CTX *ctx) {
83    ctx->state[0] = 0x67452301;
84    ctx->state[1] = 0xEFCDAB89;
85    ctx->state[2] = 0x98BADCFE;
86    ctx->state[3] = 0x10325476;
87    ctx->state[4] = 0xC3D2E1F0;
88    ctx->count = 0;
89}
90
91void SHA_update(SHA_CTX *ctx, const void *data, int len) {
92    int i = ctx->count % sizeof(ctx->buf);
93    const uint8_t* p = (const uint8_t*)data;
94
95    ctx->count += len;
96
97    while (len--) {
98        ctx->buf[i++] = *p++;
99        if (i == sizeof(ctx->buf)) {
100            SHA1_transform(ctx);
101            i = 0;
102        }
103    }
104}
105const uint8_t *SHA_final(SHA_CTX *ctx) {
106    uint8_t *p = ctx->buf;
107    uint64_t cnt = ctx->count * 8;
108    int i;
109
110    SHA_update(ctx, (uint8_t*)"\x80", 1);
111    while ((ctx->count % sizeof(ctx->buf)) != (sizeof(ctx->buf) - 8)) {
112        SHA_update(ctx, (uint8_t*)"\0", 1);
113    }
114    for (i = 0; i < 8; ++i) {
115        uint8_t tmp = cnt >> ((7 - i) * 8);
116        SHA_update(ctx, &tmp, 1);
117    }
118
119    for (i = 0; i < 5; i++) {
120        uint32_t tmp = ctx->state[i];
121        *p++ = tmp >> 24;
122        *p++ = tmp >> 16;
123        *p++ = tmp >> 8;
124        *p++ = tmp >> 0;
125    }
126
127    return ctx->buf;
128}
129
130/* Convenience function */
131const uint8_t* SHA(const void *data, int len, uint8_t *digest) {
132    const uint8_t *p;
133    int i;
134    SHA_CTX ctx;
135    SHA_init(&ctx);
136    SHA_update(&ctx, data, len);
137    p = SHA_final(&ctx);
138    for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
139        digest[i] = *p++;
140    }
141    return digest;
142}
143