blob: 105d76c917e778b8cfd000ca2d47910d0d44394b [file] [log] [blame]
nagendra modaduguc0fe8362016-12-08 14:43:06 -08001// 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/sha512.h"
15
16#include <stdint.h>
17#include <string.h>
18
19#ifndef SHA512_SUPPORT
20#error "-DSHA512_SUPPORT must be specified to enable SHA-384/512"
21#endif
22
23#define ror(value, bits) (((value) >> (bits)) | ((value) << (64 - (bits))))
24#define shr(value, bits) ((value) >> (bits))
25
26static const uint64_t K[80] = {
27 0x428A2F98D728AE22ll, 0x7137449123EF65CDll, 0xB5C0FBCFEC4D3B2Fll,
28 0xE9B5DBA58189DBBCll, 0x3956C25BF348B538ll, 0x59F111F1B605D019ll,
29 0x923F82A4AF194F9Bll, 0xAB1C5ED5DA6D8118ll, 0xD807AA98A3030242ll,
30 0x12835B0145706FBEll, 0x243185BE4EE4B28Cll, 0x550C7DC3D5FFB4E2ll,
31 0x72BE5D74F27B896Fll, 0x80DEB1FE3B1696B1ll, 0x9BDC06A725C71235ll,
32 0xC19BF174CF692694ll, 0xE49B69C19EF14AD2ll, 0xEFBE4786384F25E3ll,
33 0x0FC19DC68B8CD5B5ll, 0x240CA1CC77AC9C65ll, 0x2DE92C6F592B0275ll,
34 0x4A7484AA6EA6E483ll, 0x5CB0A9DCBD41FBD4ll, 0x76F988DA831153B5ll,
35 0x983E5152EE66DFABll, 0xA831C66D2DB43210ll, 0xB00327C898FB213Fll,
36 0xBF597FC7BEEF0EE4ll, 0xC6E00BF33DA88FC2ll, 0xD5A79147930AA725ll,
37 0x06CA6351E003826Fll, 0x142929670A0E6E70ll, 0x27B70A8546D22FFCll,
38 0x2E1B21385C26C926ll, 0x4D2C6DFC5AC42AEDll, 0x53380D139D95B3DFll,
39 0x650A73548BAF63DEll, 0x766A0ABB3C77B2A8ll, 0x81C2C92E47EDAEE6ll,
40 0x92722C851482353Bll, 0xA2BFE8A14CF10364ll, 0xA81A664BBC423001ll,
41 0xC24B8B70D0F89791ll, 0xC76C51A30654BE30ll, 0xD192E819D6EF5218ll,
42 0xD69906245565A910ll, 0xF40E35855771202All, 0x106AA07032BBD1B8ll,
43 0x19A4C116B8D2D0C8ll, 0x1E376C085141AB53ll, 0x2748774CDF8EEB99ll,
44 0x34B0BCB5E19B48A8ll, 0x391C0CB3C5C95A63ll, 0x4ED8AA4AE3418ACBll,
45 0x5B9CCA4F7763E373ll, 0x682E6FF3D6B2B8A3ll, 0x748F82EE5DEFB2FCll,
46 0x78A5636F43172F60ll, 0x84C87814A1F0AB72ll, 0x8CC702081A6439ECll,
47 0x90BEFFFA23631E28ll, 0xA4506CEBDE82BDE9ll, 0xBEF9A3F7B2C67915ll,
48 0xC67178F2E372532Bll, 0xCA273ECEEA26619Cll, 0xD186B8C721C0C207ll,
49 0xEADA7DD6CDE0EB1Ell, 0xF57D4F7FEE6ED178ll, 0x06F067AA72176FBAll,
50 0x0A637DC5A2C898A6ll, 0x113F9804BEF90DAEll, 0x1B710B35131C471Bll,
51 0x28DB77F523047D84ll, 0x32CAAB7B40C72493ll, 0x3C9EBE0A15C9BEBCll,
52 0x431D67C49C100D4Cll, 0x4CC5D4BECB3E42B6ll, 0x597F299CFC657E2All,
53 0x5FCB6FAB3AD6FAECll, 0x6C44198C4A475817ll
54};
55
56static void SHA512_Transform(LITE_SHA512_CTX* ctx) {
57 uint64_t W[80];
58 uint64_t A, B, C, D, E, F, G, H;
59 uint8_t* p = ctx->buf;
60 int t;
61
62 for(t = 0; t < 16; t++) {
63 uint64_t tmp = (uint64_t)(*p++) << 56;
64 tmp |= (uint64_t)(*p++) << 48;
65 tmp |= (uint64_t)(*p++) << 40;
66 tmp |= (uint64_t)(*p++) << 32;
67 tmp |= (uint64_t)(*p++) << 24;
68 tmp |= (uint64_t)(*p++) << 16;
69 tmp |= (uint64_t)(*p++) << 8;
70 tmp |= (uint64_t)(*p++);
71 W[t] = tmp;
72 }
73
74 for(; t < 80; t++) {
75 uint64_t Wt2 = W[t - 2];
76 uint64_t Wt7 = W[t - 7];
77 uint64_t Wt15 = W[t - 15];
78 uint64_t Wt16 = W[t - 16];
79
80 uint64_t s0 = ror(Wt15, 1) ^ ror(Wt15, 8) ^ shr(Wt15, 7);
81 uint64_t s1 = ror(Wt2, 19) ^ ror(Wt2, 61) ^ shr(Wt2, 6);
82
83 W[t] = s1 + Wt7 + s0 + Wt16;
84 }
85
86 A = ctx->state[0];
87 B = ctx->state[1];
88 C = ctx->state[2];
89 D = ctx->state[3];
90 E = ctx->state[4];
91 F = ctx->state[5];
92 G = ctx->state[6];
93 H = ctx->state[7];
94
95 for(t = 0; t < 80; t++) {
96 uint64_t s0 = ror(A, 28) ^ ror(A, 34) ^ ror(A, 39);
97 uint64_t maj = (A & B) ^ (A & C) ^ (B & C);
98 uint64_t t2 = s0 + maj;
99 uint64_t s1 = ror(E, 14) ^ ror(E, 18) ^ ror(E, 41);
100 uint64_t ch = (E & F) ^ ((~E) & G);
101 uint64_t t1 = H + s1 + ch + K[t] + W[t];
102
103 H = G;
104 G = F;
105 F = E;
106 E = D + t1;
107 D = C;
108 C = B;
109 B = A;
110 A = t1 + t2;
111 }
112
113 ctx->state[0] += A;
114 ctx->state[1] += B;
115 ctx->state[2] += C;
116 ctx->state[3] += D;
117 ctx->state[4] += E;
118 ctx->state[5] += F;
119 ctx->state[6] += G;
120 ctx->state[7] += H;
121}
122
123static const HASH_VTAB SHA512_VTAB = {
124 SHA512_init,
125 SHA512_update,
126 SHA512_final,
127 SHA512_hash,
nagendra modadugu793cf592019-10-09 14:07:05 -0700128 SHA512_DIGEST_SIZE,
129 SHA512_BLOCK_SIZE
nagendra modaduguc0fe8362016-12-08 14:43:06 -0800130};
131
132void SHA512_init(LITE_SHA512_CTX* ctx) {
133 ctx->f = &SHA512_VTAB;
134 ctx->state[0] = 0x6a09e667f3bcc908ll;
135 ctx->state[1] = 0xbb67ae8584caa73bll;
136 ctx->state[2] = 0x3c6ef372fe94f82bll;
137 ctx->state[3] = 0xa54ff53a5f1d36f1ll;
138 ctx->state[4] = 0x510e527fade682d1ll;
139 ctx->state[5] = 0x9b05688c2b3e6c1fll;
140 ctx->state[6] = 0x1f83d9abfb41bd6bll;
141 ctx->state[7] = 0x5be0cd19137e2179ll;
142 ctx->count = 0;
143}
144
145void SHA512_update(LITE_SHA512_CTX* ctx, const void* data, size_t len) {
146 int i = (int) (ctx->count & 127);
147 const uint8_t* p = (const uint8_t*)data;
148
149 ctx->count += len;
150
151 while (len--) {
152 ctx->buf[i++] = *p++;
153 if (i == 128) {
154 SHA512_Transform(ctx);
155 i = 0;
156 }
157 }
158}
159
160const uint8_t* SHA512_final(LITE_SHA512_CTX* ctx) {
161 uint8_t *p = ctx->buf;
162 uint64_t cnt = ctx->count * 8;
163 int i;
164
165 SHA512_update(ctx, (uint8_t*)"\x80", 1);
166 while ((ctx->count & 127) != 112) {
167 SHA512_update(ctx, (uint8_t*)"\0", 1);
168 }
169 for (i = 0; i < 8; ++i) {
170 SHA512_update(ctx, (uint8_t*)"\0", 1);
171 }
172 for (i = 0; i < 8; ++i) {
173 uint8_t tmp = (uint8_t) (cnt >> 56);
174 cnt <<= 8;
175 SHA512_update(ctx, &tmp, 1);
176 }
177
178 for (i = 0; i < 8; i++) {
179 uint64_t tmp = ctx->state[i];
180 *p++ = (uint8_t)(tmp >> 56);
181 *p++ = (uint8_t)(tmp >> 48);
182 *p++ = (uint8_t)(tmp >> 40);
183 *p++ = (uint8_t)(tmp >> 32);
184 *p++ = (uint8_t)(tmp >> 24);
185 *p++ = (uint8_t)(tmp >> 16);
186 *p++ = (uint8_t)(tmp >> 8);
187 *p++ = (uint8_t)(tmp >> 0);
188 }
189
190 return ctx->buf;
191}
192
193/* Convenience function */
194const uint8_t* SHA512_hash(const void* data, size_t len,
195 uint8_t* digest) {
196 LITE_SHA512_CTX ctx;
197 SHA512_init(&ctx);
198 SHA512_update(&ctx, data, len);
199 memcpy(digest, SHA512_final(&ctx), SHA512_DIGEST_SIZE);
200 return digest;
201}