1 /* sha256.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/sha256.h>
31
32 #include <stdio.h>
33 #include <string.h>
34 #include <stdint.h>
35
36 #define ror(value, bits) (((value) >> (bits)) | ((value) << (32 - (bits))))
37 #define shr(value, bits) ((value) >> (bits))
38
39 static const uint32_t K[64] = {
40 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
41 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
42 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
43 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
44 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
45 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
46 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
47 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
48 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
49 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
50 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
51 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
52 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
53 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
54 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
55 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
56 };
57
SHA256_Transform(SHA256_CTX * ctx)58 static void SHA256_Transform(SHA256_CTX *ctx)
59 {
60 uint32_t W[64];
61 uint32_t A, B, C, D, E, F, G, H;
62 uint8_t *p = ctx->buf;
63 int t;
64
65 for (t = 0; t < 16; ++t) {
66 uint32_t tmp = *p++ << 24;
67 tmp |= *p++ << 16;
68 tmp |= *p++ << 8;
69 tmp |= *p++;
70 W[t] = tmp;
71 }
72
73 for (; t < 64; t++) {
74 uint32_t s0 = ror(W[t-15], 7) ^ ror(W[t-15], 18) ^ shr(W[t-15], 3);
75 uint32_t s1 = ror(W[t-2], 17) ^ ror(W[t-2], 19) ^ shr(W[t-2], 10);
76 W[t] = W[t-16] + s0 + W[t-7] + s1;
77 }
78
79 A = ctx->state[0];
80 B = ctx->state[1];
81 C = ctx->state[2];
82 D = ctx->state[3];
83 E = ctx->state[4];
84 F = ctx->state[5];
85 G = ctx->state[6];
86 H = ctx->state[7];
87
88 for (t = 0; t < 64; t++) {
89 uint32_t s0 = ror(A, 2) ^ ror(A, 13) ^ ror(A, 22);
90 uint32_t maj = (A & B) ^ (A & C) ^ (B & C);
91 uint32_t t2 = s0 + maj;
92 uint32_t s1 = ror(E, 6) ^ ror(E, 11) ^ ror(E, 25);
93 uint32_t ch = (E & F) ^ ((~E) & G);
94 uint32_t t1 = H + s1 + ch + K[t] + W[t];
95
96 H = G;
97 G = F;
98 F = E;
99 E = D + t1;
100 D = C;
101 C = B;
102 B = A;
103 A = t1 + t2;
104 }
105
106 ctx->state[0] += A;
107 ctx->state[1] += B;
108 ctx->state[2] += C;
109 ctx->state[3] += D;
110 ctx->state[4] += E;
111 ctx->state[5] += F;
112 ctx->state[6] += G;
113 ctx->state[7] += H;
114 }
115
116 static const HASH_VTAB SHA256_VTAB = {
117 SHA256_init,
118 SHA256_update,
119 SHA256_final,
120 SHA256_hash,
121 SHA256_DIGEST_SIZE
122 };
123
SHA256_init(SHA256_CTX * ctx)124 void SHA256_init(SHA256_CTX *ctx)
125 {
126 ctx->f = &SHA256_VTAB;
127 ctx->state[0] = 0x6a09e667;
128 ctx->state[1] = 0xbb67ae85;
129 ctx->state[2] = 0x3c6ef372;
130 ctx->state[3] = 0xa54ff53a;
131 ctx->state[4] = 0x510e527f;
132 ctx->state[5] = 0x9b05688c;
133 ctx->state[6] = 0x1f83d9ab;
134 ctx->state[7] = 0x5be0cd19;
135 ctx->count = 0;
136 }
137
138
SHA256_update(SHA256_CTX * ctx,const void * data,int len)139 void SHA256_update(SHA256_CTX *ctx, const void *data, int len)
140 {
141 int i = (int) (ctx->count & 63);
142 const uint8_t *p = (const uint8_t *)data;
143
144 ctx->count += len;
145
146 while (len--) {
147 ctx->buf[i++] = *p++;
148 if (i == 64) {
149 SHA256_Transform(ctx);
150 i = 0;
151 }
152 }
153 }
154
155
SHA256_final(SHA256_CTX * ctx)156 const uint8_t *SHA256_final(SHA256_CTX *ctx)
157 {
158 uint8_t *p = ctx->buf;
159 uint64_t cnt = ctx->count * 8;
160 int i;
161
162 SHA256_update(ctx, (uint8_t *)"\x80", 1);
163 while ((ctx->count & 63) != 56) {
164 SHA256_update(ctx, (uint8_t *)"\0", 1);
165 }
166 for (i = 0; i < 8; ++i) {
167 uint8_t tmp = (uint8_t) (cnt >> ((7 - i) * 8));
168 SHA256_update(ctx, &tmp, 1);
169 }
170
171 for (i = 0; i < 8; i++) {
172 uint32_t tmp = ctx->state[i];
173 *p++ = tmp >> 24;
174 *p++ = tmp >> 16;
175 *p++ = tmp >> 8;
176 *p++ = tmp >> 0;
177 }
178
179 return ctx->buf;
180 }
181
182 /* Convenience function */
SHA256_hash(const void * data,int len,uint8_t * digest)183 const uint8_t *SHA256_hash(const void *data, int len, uint8_t *digest)
184 {
185 SHA256_CTX ctx;
186 SHA256_init(&ctx);
187 SHA256_update(&ctx, data, len);
188 memcpy(digest, SHA256_final(&ctx), SHA256_DIGEST_SIZE);
189 return digest;
190 }
191