blob: 44263a44d8207df9cd68bb517070bf10fa88ef6d [file] [log] [blame]
nagendra modadugu4fae5422016-05-10 16:11:54 -07001// Copyright 2016 Google Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14#include "cryptoc/sha256.h"
15
16#include <string.h>
17#include <stdint.h>
18
19#define ror(value, bits) (((value) >> (bits)) | ((value) << (32 - (bits))))
20#define shr(value, bits) ((value) >> (bits))
21
22static const uint32_t K[64] = {
23 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
24 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
25 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
26 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
27 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
28 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
29 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
30 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
31 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
32 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
33 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
34 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
35 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
36 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
37 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
38 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 };
39
40static void SHA256_Transform(LITE_SHA256_CTX* ctx) {
41 uint32_t W[64];
42 uint32_t A, B, C, D, E, F, G, H;
43 uint8_t* p = ctx->buf;
44 int t;
45
46 for(t = 0; t < 16; ++t) {
nagendra modadugu793cf592019-10-09 14:07:05 -070047 uint32_t tmp = (uint32_t)*p++ << 24;
48 tmp |= (uint32_t)*p++ << 16;
49 tmp |= (uint32_t)*p++ << 8;
50 tmp |= (uint32_t)*p++;
nagendra modadugu4fae5422016-05-10 16:11:54 -070051 W[t] = tmp;
52 }
53
54 for(; t < 64; t++) {
55 uint32_t s0 = ror(W[t-15], 7) ^ ror(W[t-15], 18) ^ shr(W[t-15], 3);
56 uint32_t s1 = ror(W[t-2], 17) ^ ror(W[t-2], 19) ^ shr(W[t-2], 10);
57 W[t] = W[t-16] + s0 + W[t-7] + s1;
58 }
59
60 A = ctx->state[0];
61 B = ctx->state[1];
62 C = ctx->state[2];
63 D = ctx->state[3];
64 E = ctx->state[4];
65 F = ctx->state[5];
66 G = ctx->state[6];
67 H = ctx->state[7];
68
69 for(t = 0; t < 64; t++) {
70 uint32_t s0 = ror(A, 2) ^ ror(A, 13) ^ ror(A, 22);
71 uint32_t maj = (A & B) ^ (A & C) ^ (B & C);
72 uint32_t t2 = s0 + maj;
73 uint32_t s1 = ror(E, 6) ^ ror(E, 11) ^ ror(E, 25);
74 uint32_t ch = (E & F) ^ ((~E) & G);
75 uint32_t t1 = H + s1 + ch + K[t] + W[t];
76
77 H = G;
78 G = F;
79 F = E;
80 E = D + t1;
81 D = C;
82 C = B;
83 B = A;
84 A = t1 + t2;
85 }
86
87 ctx->state[0] += A;
88 ctx->state[1] += B;
89 ctx->state[2] += C;
90 ctx->state[3] += D;
91 ctx->state[4] += E;
92 ctx->state[5] += F;
93 ctx->state[6] += G;
94 ctx->state[7] += H;
95}
96
97static const HASH_VTAB SHA256_VTAB = {
98 SHA256_init,
99 SHA256_update,
100 SHA256_final,
101 SHA256_hash,
nagendra modadugu793cf592019-10-09 14:07:05 -0700102 SHA256_DIGEST_SIZE,
103#ifdef SHA512_SUPPORT
104 SHA256_BLOCK_SIZE,
105#endif
nagendra modadugu4fae5422016-05-10 16:11:54 -0700106};
107
108void SHA256_init(LITE_SHA256_CTX* ctx) {
109 ctx->f = &SHA256_VTAB;
110 ctx->state[0] = 0x6a09e667;
111 ctx->state[1] = 0xbb67ae85;
112 ctx->state[2] = 0x3c6ef372;
113 ctx->state[3] = 0xa54ff53a;
114 ctx->state[4] = 0x510e527f;
115 ctx->state[5] = 0x9b05688c;
116 ctx->state[6] = 0x1f83d9ab;
117 ctx->state[7] = 0x5be0cd19;
118 ctx->count = 0;
119}
120
121
nagendra modadugu810f8b82016-10-18 15:07:43 -0700122void SHA256_update(LITE_SHA256_CTX* ctx, const void* data, size_t len) {
nagendra modadugu4fae5422016-05-10 16:11:54 -0700123 int i = (int) (ctx->count & 63);
124 const uint8_t* p = (const uint8_t*)data;
125
126 ctx->count += len;
127
128 while (len--) {
129 ctx->buf[i++] = *p++;
130 if (i == 64) {
131 SHA256_Transform(ctx);
132 i = 0;
133 }
134 }
135}
136
137
138const uint8_t* SHA256_final(LITE_SHA256_CTX* ctx) {
139 uint8_t *p = ctx->buf;
nagendra modadugu793cf592019-10-09 14:07:05 -0700140 uint64_t cnt = LITE_LShiftU64(ctx->count, 3);
nagendra modadugu4fae5422016-05-10 16:11:54 -0700141 int i;
142
143 SHA256_update(ctx, (uint8_t*)"\x80", 1);
144 while ((ctx->count & 63) != 56) {
145 SHA256_update(ctx, (uint8_t*)"\0", 1);
146 }
147 for (i = 0; i < 8; ++i) {
nagendra modadugu793cf592019-10-09 14:07:05 -0700148 uint8_t tmp = (uint8_t)LITE_RShiftU64(cnt, 56);
149 cnt = LITE_LShiftU64(cnt, 8);
nagendra modadugu4fae5422016-05-10 16:11:54 -0700150 SHA256_update(ctx, &tmp, 1);
151 }
152
153 for (i = 0; i < 8; i++) {
154 uint32_t tmp = ctx->state[i];
155 *p++ = (uint8_t)(tmp >> 24);
156 *p++ = (uint8_t)(tmp >> 16);
157 *p++ = (uint8_t)(tmp >> 8);
158 *p++ = (uint8_t)(tmp >> 0);
159 }
160
161 return ctx->buf;
162}
163
164/* Convenience function */
nagendra modadugu810f8b82016-10-18 15:07:43 -0700165const uint8_t* SHA256_hash(const void* data, size_t len,
nagendra modadugu4fae5422016-05-10 16:11:54 -0700166 uint8_t* digest) {
167 LITE_SHA256_CTX ctx;
168 SHA256_init(&ctx);
169 SHA256_update(&ctx, data, len);
170 memcpy(digest, SHA256_final(&ctx), SHA256_DIGEST_SIZE);
171 return digest;
172}