blob: 72c3df8370801f0391394f1029a37bdc5416b7e1 [file] [log] [blame]
Stefan Berger4e57a542021-06-14 13:35:48 -04001/*****************************************************************************
2 * Copyright (c) 2015-2020 IBM Corporation
3 * All rights reserved.
4 * This program and the accompanying materials
5 * are made available under the terms of the BSD License
6 * which accompanies this distribution, and is available at
7 * http://www.opensource.org/licenses/bsd-license.php
8 *
9 * Contributors:
10 * IBM Corporation - initial implementation
11 *****************************************************************************/
12
13/*
14 * See: NIST standard for SHA-256 in FIPS PUB 180-4
15 */
16
17#include "config.h"
18#include "byteorder.h"
19#include "sha.h"
20#include "string.h"
21#include "x86.h"
22
23typedef struct _sha256_ctx {
24 u32 h[8];
25} sha256_ctx;
26
27static inline u32 Ch(u32 x, u32 y, u32 z)
28{
29 return (x & y) | ((x ^ 0xffffffff) & z);
30}
31
32static inline u32 Maj(u32 x, u32 y, u32 z)
33{
34 return (x & y) | (x & z) | (y & z);
35}
36
37static inline u32 sum0(u32 x)
38{
39 return ror(x, 2) ^ ror(x, 13) ^ ror(x, 22);
40}
41
42static inline u32 sum1(u32 x)
43{
44 return ror(x, 6) ^ ror(x, 11) ^ ror(x, 25);
45}
46
47static inline u32 sigma0(u32 x)
48{
49 return ror(x, 7) ^ ror(x, 18) ^ (x >> 3);
50}
51
52static inline u32 sigma1(u32 x)
53{
54 return ror(x, 17) ^ ror(x, 19) ^ (x >> 10);
55}
56
57static void sha256_block(u32 *w, sha256_ctx *ctx)
58{
59 u32 t;
60 u32 a, b, c, d, e, f, g, h;
61 u32 T1, T2;
62
63 /*
64 * FIPS 180-4 4.2.2: SHA256 Constants
65 */
66 static const u32 sha_ko[64] = {
67 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
68 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
69 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
70 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
71 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
72 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
73 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
74 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
75 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
76 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
77 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
78 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
79 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
80 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
81 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
82 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
83 };
84
85 /*
86 * FIPS 180-4 6.2.2: step 1
87 *
88 * 0 <= i <= 15:
89 * W(t) = M(t)
90 * 16 <= i <= 63:
91 * W(t) = sigma1(W(t-2)) + W(t-7) + sigma0(W(t-15)) + W(t-16)
92 */
93
94 /* w(0)..w(15) are in big endian format */
95 for (t = 0; t <= 15; t++)
96 w[t] = be32_to_cpu(w[t]);
97
98 for (t = 16; t <= 63; t++)
99 w[t] = sigma1(w[t-2]) + w[t-7] + sigma0(w[t-15]) + w[t-16];
100
101 /*
102 * step 2: a = H0, b = H1, c = H2, d = H3, e = H4, f = H5, g = H6, h = H7
103 */
104 a = ctx->h[0];
105 b = ctx->h[1];
106 c = ctx->h[2];
107 d = ctx->h[3];
108 e = ctx->h[4];
109 f = ctx->h[5];
110 g = ctx->h[6];
111 h = ctx->h[7];
112
113 /*
114 * step 3: For i = 0 to 63:
115 * T1 = h + sum1(e) + Ch(e,f,g) + K(t) + W(t);
116 * T2 = sum0(a) + Maj(a,b,c)
117 * h = g; g = f; f = e; e = d + T1; d = c; c = b; b = a; a + T1 + T2
118 */
119 for (t = 0; t <= 63; t++) {
120 T1 = h + sum1(e) + Ch(e, f, g) + sha_ko[t] + w[t];
121 T2 = sum0(a) + Maj(a, b, c);
122 h = g;
123 g = f;
124 f = e;
125 e = d + T1;
126 d = c;
127 c = b;
128 b = a;
129 a = T1 + T2;
130 }
131
132 /*
133 * step 4:
134 * H0 = a + H0, H1 = b + H1, H2 = c + H2, H3 = d + H3, H4 = e + H4
135 */
136 ctx->h[0] += a;
137 ctx->h[1] += b;
138 ctx->h[2] += c;
139 ctx->h[3] += d;
140 ctx->h[4] += e;
141 ctx->h[5] += f;
142 ctx->h[6] += g;
143 ctx->h[7] += h;
144}
145
146static void sha256_do(sha256_ctx *ctx, const u8 *data32, u32 length)
147{
148 u32 offset;
149 u16 num;
150 u32 bits = 0;
151 u32 w[64];
152 u64 tmp;
153
154 /* treat data in 64-byte chunks */
155 for (offset = 0; length - offset >= 64; offset += 64) {
156 memcpy(w, data32 + offset, 64);
157 sha256_block((u32 *)w, ctx);
158 bits += (64 * 8);
159 }
160
161 /* last block with less than 64 bytes */
162 num = length - offset;
163 bits += (num << 3);
164
165 memcpy(w, data32 + offset, num);
166 /*
167 * FIPS 180-4 5.1: Padding the Message
168 */
169 ((u8 *)w)[num] = 0x80;
170 if (64 - (num + 1) > 0)
171 memset( &((u8 *)w)[num + 1], 0, 64 - (num + 1));
172
173 if (num >= 56) {
174 /* cannot append number of bits here */
175 sha256_block((u32 *)w, ctx);
176 memset(w, 0, 60);
177 }
178
179 /* write number of bits to end of block */
180 tmp = cpu_to_be64(bits);
181 memcpy(&w[14], &tmp, 8);
182
183 sha256_block(w, ctx);
184
185 /* need to switch result's endianness */
186 for (num = 0; num < 8; num++)
187 ctx->h[num] = cpu_to_be32(ctx->h[num]);
188}
189
190void sha256(const u8 *data, u32 length, u8 *hash)
191{
192 sha256_ctx ctx = {
193 .h = {
194 /*
195 * FIPS 180-4: 6.2.1
196 * -> 5.3.3: initial hash value
197 */
198 0x6a09e667,
199 0xbb67ae85,
200 0x3c6ef372,
201 0xa54ff53a,
202 0x510e527f,
203 0x9b05688c,
204 0x1f83d9ab,
205 0x5be0cd19
206 }
207 };
208
209 sha256_do(&ctx, data, length);
210 memcpy(hash, ctx.h, sizeof(ctx.h));
211}