1 /* sha.c
2 **
3 ** Copyright 2013, 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 // Optimized for minimal code size.
29 
30 #include <lib/mincrypt/sha.h>
31 
32 #include <stdio.h>
33 #include <string.h>
34 #include <stdint.h>
35 
36 #define rol(bits, value) (((value) << (bits)) | ((value) >> (32 - (bits))))
37 
SHA1_Transform(SHA_CTX * ctx)38 static void SHA1_Transform(SHA_CTX *ctx)
39 {
40     uint32_t W[80];
41     uint32_t A, B, C, D, E;
42     uint8_t *p = ctx->buf;
43     int t;
44 
45     for (t = 0; t < 16; ++t) {
46         uint32_t tmp =  *p++ << 24;
47         tmp |= *p++ << 16;
48         tmp |= *p++ << 8;
49         tmp |= *p++;
50         W[t] = tmp;
51     }
52 
53     for (; t < 80; t++) {
54         W[t] = rol(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
55     }
56 
57     A = ctx->state[0];
58     B = ctx->state[1];
59     C = ctx->state[2];
60     D = ctx->state[3];
61     E = ctx->state[4];
62 
63     for (t = 0; t < 80; t++) {
64         uint32_t tmp = rol(5,A) + E + W[t];
65 
66         if (t < 20)
67             tmp += (D^(B&(C^D))) + 0x5A827999;
68         else if ( t < 40)
69             tmp += (B^C^D) + 0x6ED9EBA1;
70         else if ( t < 60)
71             tmp += ((B&C)|(D&(B|C))) + 0x8F1BBCDC;
72         else
73             tmp += (B^C^D) + 0xCA62C1D6;
74 
75         E = D;
76         D = C;
77         C = rol(30,B);
78         B = A;
79         A = tmp;
80     }
81 
82     ctx->state[0] += A;
83     ctx->state[1] += B;
84     ctx->state[2] += C;
85     ctx->state[3] += D;
86     ctx->state[4] += E;
87 }
88 
89 static const HASH_VTAB SHA_VTAB = {
90     SHA_init,
91     SHA_update,
92     SHA_final,
93     SHA_hash,
94     SHA_DIGEST_SIZE
95 };
96 
SHA_init(SHA_CTX * ctx)97 void SHA_init(SHA_CTX *ctx)
98 {
99     ctx->f = &SHA_VTAB;
100     ctx->state[0] = 0x67452301;
101     ctx->state[1] = 0xEFCDAB89;
102     ctx->state[2] = 0x98BADCFE;
103     ctx->state[3] = 0x10325476;
104     ctx->state[4] = 0xC3D2E1F0;
105     ctx->count = 0;
106 }
107 
108 
SHA_update(SHA_CTX * ctx,const void * data,int len)109 void SHA_update(SHA_CTX *ctx, const void *data, int len)
110 {
111     int i = (int) (ctx->count & 63);
112     const uint8_t *p = (const uint8_t *)data;
113 
114     ctx->count += len;
115 
116     while (len--) {
117         ctx->buf[i++] = *p++;
118         if (i == 64) {
119             SHA1_Transform(ctx);
120             i = 0;
121         }
122     }
123 }
124 
125 
SHA_final(SHA_CTX * ctx)126 const uint8_t *SHA_final(SHA_CTX *ctx)
127 {
128     uint8_t *p = ctx->buf;
129     uint64_t cnt = ctx->count * 8;
130     int i;
131 
132     SHA_update(ctx, (uint8_t *)"\x80", 1);
133     while ((ctx->count & 63) != 56) {
134         SHA_update(ctx, (uint8_t *)"\0", 1);
135     }
136     for (i = 0; i < 8; ++i) {
137         uint8_t tmp = (uint8_t) (cnt >> ((7 - i) * 8));
138         SHA_update(ctx, &tmp, 1);
139     }
140 
141     for (i = 0; i < 5; i++) {
142         uint32_t tmp = ctx->state[i];
143         *p++ = tmp >> 24;
144         *p++ = tmp >> 16;
145         *p++ = tmp >> 8;
146         *p++ = tmp >> 0;
147     }
148 
149     return ctx->buf;
150 }
151 
152 /* Convenience function */
SHA_hash(const void * data,int len,uint8_t * digest)153 const uint8_t *SHA_hash(const void *data, int len, uint8_t *digest)
154 {
155     SHA_CTX ctx;
156     SHA_init(&ctx);
157     SHA_update(&ctx, data, len);
158     memcpy(digest, SHA_final(&ctx), SHA_DIGEST_SIZE);
159     return digest;
160 }
161