18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * SHA-256, as specified in 48c2ecf20Sopenharmony_ci * http://csrc.nist.gov/groups/STM/cavp/documents/shs/sha256-384-512.pdf 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * SHA-256 code by Jean-Luc Cooke <jlcooke@certainkey.com>. 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * Copyright (c) Jean-Luc Cooke <jlcooke@certainkey.com> 98c2ecf20Sopenharmony_ci * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk> 108c2ecf20Sopenharmony_ci * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> 118c2ecf20Sopenharmony_ci * Copyright (c) 2014 Red Hat Inc. 128c2ecf20Sopenharmony_ci */ 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#include <linux/bitops.h> 158c2ecf20Sopenharmony_ci#include <linux/export.h> 168c2ecf20Sopenharmony_ci#include <linux/module.h> 178c2ecf20Sopenharmony_ci#include <linux/string.h> 188c2ecf20Sopenharmony_ci#include <crypto/sha.h> 198c2ecf20Sopenharmony_ci#include <asm/unaligned.h> 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_cistatic inline u32 Ch(u32 x, u32 y, u32 z) 228c2ecf20Sopenharmony_ci{ 238c2ecf20Sopenharmony_ci return z ^ (x & (y ^ z)); 248c2ecf20Sopenharmony_ci} 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_cistatic inline u32 Maj(u32 x, u32 y, u32 z) 278c2ecf20Sopenharmony_ci{ 288c2ecf20Sopenharmony_ci return (x & y) | (z & (x | y)); 298c2ecf20Sopenharmony_ci} 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci#define e0(x) (ror32(x, 2) ^ ror32(x, 13) ^ ror32(x, 22)) 328c2ecf20Sopenharmony_ci#define e1(x) (ror32(x, 6) ^ ror32(x, 11) ^ ror32(x, 25)) 338c2ecf20Sopenharmony_ci#define s0(x) (ror32(x, 7) ^ ror32(x, 18) ^ (x >> 3)) 348c2ecf20Sopenharmony_ci#define s1(x) (ror32(x, 17) ^ ror32(x, 19) ^ (x >> 10)) 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_cistatic inline void LOAD_OP(int I, u32 *W, const u8 *input) 378c2ecf20Sopenharmony_ci{ 388c2ecf20Sopenharmony_ci W[I] = get_unaligned_be32((__u32 *)input + I); 398c2ecf20Sopenharmony_ci} 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_cistatic inline void BLEND_OP(int I, u32 *W) 428c2ecf20Sopenharmony_ci{ 438c2ecf20Sopenharmony_ci W[I] = s1(W[I-2]) + W[I-7] + s0(W[I-15]) + W[I-16]; 448c2ecf20Sopenharmony_ci} 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_cistatic void sha256_transform(u32 *state, const u8 *input) 478c2ecf20Sopenharmony_ci{ 488c2ecf20Sopenharmony_ci u32 a, b, c, d, e, f, g, h, t1, t2; 498c2ecf20Sopenharmony_ci u32 W[64]; 508c2ecf20Sopenharmony_ci int i; 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci /* load the input */ 538c2ecf20Sopenharmony_ci for (i = 0; i < 16; i++) 548c2ecf20Sopenharmony_ci LOAD_OP(i, W, input); 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci /* now blend */ 578c2ecf20Sopenharmony_ci for (i = 16; i < 64; i++) 588c2ecf20Sopenharmony_ci BLEND_OP(i, W); 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci /* load the state into our registers */ 618c2ecf20Sopenharmony_ci a = state[0]; b = state[1]; c = state[2]; d = state[3]; 628c2ecf20Sopenharmony_ci e = state[4]; f = state[5]; g = state[6]; h = state[7]; 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci /* now iterate */ 658c2ecf20Sopenharmony_ci t1 = h + e1(e) + Ch(e, f, g) + 0x428a2f98 + W[0]; 668c2ecf20Sopenharmony_ci t2 = e0(a) + Maj(a, b, c); d += t1; h = t1 + t2; 678c2ecf20Sopenharmony_ci t1 = g + e1(d) + Ch(d, e, f) + 0x71374491 + W[1]; 688c2ecf20Sopenharmony_ci t2 = e0(h) + Maj(h, a, b); c += t1; g = t1 + t2; 698c2ecf20Sopenharmony_ci t1 = f + e1(c) + Ch(c, d, e) + 0xb5c0fbcf + W[2]; 708c2ecf20Sopenharmony_ci t2 = e0(g) + Maj(g, h, a); b += t1; f = t1 + t2; 718c2ecf20Sopenharmony_ci t1 = e + e1(b) + Ch(b, c, d) + 0xe9b5dba5 + W[3]; 728c2ecf20Sopenharmony_ci t2 = e0(f) + Maj(f, g, h); a += t1; e = t1 + t2; 738c2ecf20Sopenharmony_ci t1 = d + e1(a) + Ch(a, b, c) + 0x3956c25b + W[4]; 748c2ecf20Sopenharmony_ci t2 = e0(e) + Maj(e, f, g); h += t1; d = t1 + t2; 758c2ecf20Sopenharmony_ci t1 = c + e1(h) + Ch(h, a, b) + 0x59f111f1 + W[5]; 768c2ecf20Sopenharmony_ci t2 = e0(d) + Maj(d, e, f); g += t1; c = t1 + t2; 778c2ecf20Sopenharmony_ci t1 = b + e1(g) + Ch(g, h, a) + 0x923f82a4 + W[6]; 788c2ecf20Sopenharmony_ci t2 = e0(c) + Maj(c, d, e); f += t1; b = t1 + t2; 798c2ecf20Sopenharmony_ci t1 = a + e1(f) + Ch(f, g, h) + 0xab1c5ed5 + W[7]; 808c2ecf20Sopenharmony_ci t2 = e0(b) + Maj(b, c, d); e += t1; a = t1 + t2; 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci t1 = h + e1(e) + Ch(e, f, g) + 0xd807aa98 + W[8]; 838c2ecf20Sopenharmony_ci t2 = e0(a) + Maj(a, b, c); d += t1; h = t1 + t2; 848c2ecf20Sopenharmony_ci t1 = g + e1(d) + Ch(d, e, f) + 0x12835b01 + W[9]; 858c2ecf20Sopenharmony_ci t2 = e0(h) + Maj(h, a, b); c += t1; g = t1 + t2; 868c2ecf20Sopenharmony_ci t1 = f + e1(c) + Ch(c, d, e) + 0x243185be + W[10]; 878c2ecf20Sopenharmony_ci t2 = e0(g) + Maj(g, h, a); b += t1; f = t1 + t2; 888c2ecf20Sopenharmony_ci t1 = e + e1(b) + Ch(b, c, d) + 0x550c7dc3 + W[11]; 898c2ecf20Sopenharmony_ci t2 = e0(f) + Maj(f, g, h); a += t1; e = t1 + t2; 908c2ecf20Sopenharmony_ci t1 = d + e1(a) + Ch(a, b, c) + 0x72be5d74 + W[12]; 918c2ecf20Sopenharmony_ci t2 = e0(e) + Maj(e, f, g); h += t1; d = t1 + t2; 928c2ecf20Sopenharmony_ci t1 = c + e1(h) + Ch(h, a, b) + 0x80deb1fe + W[13]; 938c2ecf20Sopenharmony_ci t2 = e0(d) + Maj(d, e, f); g += t1; c = t1 + t2; 948c2ecf20Sopenharmony_ci t1 = b + e1(g) + Ch(g, h, a) + 0x9bdc06a7 + W[14]; 958c2ecf20Sopenharmony_ci t2 = e0(c) + Maj(c, d, e); f += t1; b = t1 + t2; 968c2ecf20Sopenharmony_ci t1 = a + e1(f) + Ch(f, g, h) + 0xc19bf174 + W[15]; 978c2ecf20Sopenharmony_ci t2 = e0(b) + Maj(b, c, d); e += t1; a = t1 + t2; 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci t1 = h + e1(e) + Ch(e, f, g) + 0xe49b69c1 + W[16]; 1008c2ecf20Sopenharmony_ci t2 = e0(a) + Maj(a, b, c); d += t1; h = t1 + t2; 1018c2ecf20Sopenharmony_ci t1 = g + e1(d) + Ch(d, e, f) + 0xefbe4786 + W[17]; 1028c2ecf20Sopenharmony_ci t2 = e0(h) + Maj(h, a, b); c += t1; g = t1 + t2; 1038c2ecf20Sopenharmony_ci t1 = f + e1(c) + Ch(c, d, e) + 0x0fc19dc6 + W[18]; 1048c2ecf20Sopenharmony_ci t2 = e0(g) + Maj(g, h, a); b += t1; f = t1 + t2; 1058c2ecf20Sopenharmony_ci t1 = e + e1(b) + Ch(b, c, d) + 0x240ca1cc + W[19]; 1068c2ecf20Sopenharmony_ci t2 = e0(f) + Maj(f, g, h); a += t1; e = t1 + t2; 1078c2ecf20Sopenharmony_ci t1 = d + e1(a) + Ch(a, b, c) + 0x2de92c6f + W[20]; 1088c2ecf20Sopenharmony_ci t2 = e0(e) + Maj(e, f, g); h += t1; d = t1 + t2; 1098c2ecf20Sopenharmony_ci t1 = c + e1(h) + Ch(h, a, b) + 0x4a7484aa + W[21]; 1108c2ecf20Sopenharmony_ci t2 = e0(d) + Maj(d, e, f); g += t1; c = t1 + t2; 1118c2ecf20Sopenharmony_ci t1 = b + e1(g) + Ch(g, h, a) + 0x5cb0a9dc + W[22]; 1128c2ecf20Sopenharmony_ci t2 = e0(c) + Maj(c, d, e); f += t1; b = t1 + t2; 1138c2ecf20Sopenharmony_ci t1 = a + e1(f) + Ch(f, g, h) + 0x76f988da + W[23]; 1148c2ecf20Sopenharmony_ci t2 = e0(b) + Maj(b, c, d); e += t1; a = t1 + t2; 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci t1 = h + e1(e) + Ch(e, f, g) + 0x983e5152 + W[24]; 1178c2ecf20Sopenharmony_ci t2 = e0(a) + Maj(a, b, c); d += t1; h = t1 + t2; 1188c2ecf20Sopenharmony_ci t1 = g + e1(d) + Ch(d, e, f) + 0xa831c66d + W[25]; 1198c2ecf20Sopenharmony_ci t2 = e0(h) + Maj(h, a, b); c += t1; g = t1 + t2; 1208c2ecf20Sopenharmony_ci t1 = f + e1(c) + Ch(c, d, e) + 0xb00327c8 + W[26]; 1218c2ecf20Sopenharmony_ci t2 = e0(g) + Maj(g, h, a); b += t1; f = t1 + t2; 1228c2ecf20Sopenharmony_ci t1 = e + e1(b) + Ch(b, c, d) + 0xbf597fc7 + W[27]; 1238c2ecf20Sopenharmony_ci t2 = e0(f) + Maj(f, g, h); a += t1; e = t1 + t2; 1248c2ecf20Sopenharmony_ci t1 = d + e1(a) + Ch(a, b, c) + 0xc6e00bf3 + W[28]; 1258c2ecf20Sopenharmony_ci t2 = e0(e) + Maj(e, f, g); h += t1; d = t1 + t2; 1268c2ecf20Sopenharmony_ci t1 = c + e1(h) + Ch(h, a, b) + 0xd5a79147 + W[29]; 1278c2ecf20Sopenharmony_ci t2 = e0(d) + Maj(d, e, f); g += t1; c = t1 + t2; 1288c2ecf20Sopenharmony_ci t1 = b + e1(g) + Ch(g, h, a) + 0x06ca6351 + W[30]; 1298c2ecf20Sopenharmony_ci t2 = e0(c) + Maj(c, d, e); f += t1; b = t1 + t2; 1308c2ecf20Sopenharmony_ci t1 = a + e1(f) + Ch(f, g, h) + 0x14292967 + W[31]; 1318c2ecf20Sopenharmony_ci t2 = e0(b) + Maj(b, c, d); e += t1; a = t1 + t2; 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci t1 = h + e1(e) + Ch(e, f, g) + 0x27b70a85 + W[32]; 1348c2ecf20Sopenharmony_ci t2 = e0(a) + Maj(a, b, c); d += t1; h = t1 + t2; 1358c2ecf20Sopenharmony_ci t1 = g + e1(d) + Ch(d, e, f) + 0x2e1b2138 + W[33]; 1368c2ecf20Sopenharmony_ci t2 = e0(h) + Maj(h, a, b); c += t1; g = t1 + t2; 1378c2ecf20Sopenharmony_ci t1 = f + e1(c) + Ch(c, d, e) + 0x4d2c6dfc + W[34]; 1388c2ecf20Sopenharmony_ci t2 = e0(g) + Maj(g, h, a); b += t1; f = t1 + t2; 1398c2ecf20Sopenharmony_ci t1 = e + e1(b) + Ch(b, c, d) + 0x53380d13 + W[35]; 1408c2ecf20Sopenharmony_ci t2 = e0(f) + Maj(f, g, h); a += t1; e = t1 + t2; 1418c2ecf20Sopenharmony_ci t1 = d + e1(a) + Ch(a, b, c) + 0x650a7354 + W[36]; 1428c2ecf20Sopenharmony_ci t2 = e0(e) + Maj(e, f, g); h += t1; d = t1 + t2; 1438c2ecf20Sopenharmony_ci t1 = c + e1(h) + Ch(h, a, b) + 0x766a0abb + W[37]; 1448c2ecf20Sopenharmony_ci t2 = e0(d) + Maj(d, e, f); g += t1; c = t1 + t2; 1458c2ecf20Sopenharmony_ci t1 = b + e1(g) + Ch(g, h, a) + 0x81c2c92e + W[38]; 1468c2ecf20Sopenharmony_ci t2 = e0(c) + Maj(c, d, e); f += t1; b = t1 + t2; 1478c2ecf20Sopenharmony_ci t1 = a + e1(f) + Ch(f, g, h) + 0x92722c85 + W[39]; 1488c2ecf20Sopenharmony_ci t2 = e0(b) + Maj(b, c, d); e += t1; a = t1 + t2; 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci t1 = h + e1(e) + Ch(e, f, g) + 0xa2bfe8a1 + W[40]; 1518c2ecf20Sopenharmony_ci t2 = e0(a) + Maj(a, b, c); d += t1; h = t1 + t2; 1528c2ecf20Sopenharmony_ci t1 = g + e1(d) + Ch(d, e, f) + 0xa81a664b + W[41]; 1538c2ecf20Sopenharmony_ci t2 = e0(h) + Maj(h, a, b); c += t1; g = t1 + t2; 1548c2ecf20Sopenharmony_ci t1 = f + e1(c) + Ch(c, d, e) + 0xc24b8b70 + W[42]; 1558c2ecf20Sopenharmony_ci t2 = e0(g) + Maj(g, h, a); b += t1; f = t1 + t2; 1568c2ecf20Sopenharmony_ci t1 = e + e1(b) + Ch(b, c, d) + 0xc76c51a3 + W[43]; 1578c2ecf20Sopenharmony_ci t2 = e0(f) + Maj(f, g, h); a += t1; e = t1 + t2; 1588c2ecf20Sopenharmony_ci t1 = d + e1(a) + Ch(a, b, c) + 0xd192e819 + W[44]; 1598c2ecf20Sopenharmony_ci t2 = e0(e) + Maj(e, f, g); h += t1; d = t1 + t2; 1608c2ecf20Sopenharmony_ci t1 = c + e1(h) + Ch(h, a, b) + 0xd6990624 + W[45]; 1618c2ecf20Sopenharmony_ci t2 = e0(d) + Maj(d, e, f); g += t1; c = t1 + t2; 1628c2ecf20Sopenharmony_ci t1 = b + e1(g) + Ch(g, h, a) + 0xf40e3585 + W[46]; 1638c2ecf20Sopenharmony_ci t2 = e0(c) + Maj(c, d, e); f += t1; b = t1 + t2; 1648c2ecf20Sopenharmony_ci t1 = a + e1(f) + Ch(f, g, h) + 0x106aa070 + W[47]; 1658c2ecf20Sopenharmony_ci t2 = e0(b) + Maj(b, c, d); e += t1; a = t1 + t2; 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ci t1 = h + e1(e) + Ch(e, f, g) + 0x19a4c116 + W[48]; 1688c2ecf20Sopenharmony_ci t2 = e0(a) + Maj(a, b, c); d += t1; h = t1 + t2; 1698c2ecf20Sopenharmony_ci t1 = g + e1(d) + Ch(d, e, f) + 0x1e376c08 + W[49]; 1708c2ecf20Sopenharmony_ci t2 = e0(h) + Maj(h, a, b); c += t1; g = t1 + t2; 1718c2ecf20Sopenharmony_ci t1 = f + e1(c) + Ch(c, d, e) + 0x2748774c + W[50]; 1728c2ecf20Sopenharmony_ci t2 = e0(g) + Maj(g, h, a); b += t1; f = t1 + t2; 1738c2ecf20Sopenharmony_ci t1 = e + e1(b) + Ch(b, c, d) + 0x34b0bcb5 + W[51]; 1748c2ecf20Sopenharmony_ci t2 = e0(f) + Maj(f, g, h); a += t1; e = t1 + t2; 1758c2ecf20Sopenharmony_ci t1 = d + e1(a) + Ch(a, b, c) + 0x391c0cb3 + W[52]; 1768c2ecf20Sopenharmony_ci t2 = e0(e) + Maj(e, f, g); h += t1; d = t1 + t2; 1778c2ecf20Sopenharmony_ci t1 = c + e1(h) + Ch(h, a, b) + 0x4ed8aa4a + W[53]; 1788c2ecf20Sopenharmony_ci t2 = e0(d) + Maj(d, e, f); g += t1; c = t1 + t2; 1798c2ecf20Sopenharmony_ci t1 = b + e1(g) + Ch(g, h, a) + 0x5b9cca4f + W[54]; 1808c2ecf20Sopenharmony_ci t2 = e0(c) + Maj(c, d, e); f += t1; b = t1 + t2; 1818c2ecf20Sopenharmony_ci t1 = a + e1(f) + Ch(f, g, h) + 0x682e6ff3 + W[55]; 1828c2ecf20Sopenharmony_ci t2 = e0(b) + Maj(b, c, d); e += t1; a = t1 + t2; 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci t1 = h + e1(e) + Ch(e, f, g) + 0x748f82ee + W[56]; 1858c2ecf20Sopenharmony_ci t2 = e0(a) + Maj(a, b, c); d += t1; h = t1 + t2; 1868c2ecf20Sopenharmony_ci t1 = g + e1(d) + Ch(d, e, f) + 0x78a5636f + W[57]; 1878c2ecf20Sopenharmony_ci t2 = e0(h) + Maj(h, a, b); c += t1; g = t1 + t2; 1888c2ecf20Sopenharmony_ci t1 = f + e1(c) + Ch(c, d, e) + 0x84c87814 + W[58]; 1898c2ecf20Sopenharmony_ci t2 = e0(g) + Maj(g, h, a); b += t1; f = t1 + t2; 1908c2ecf20Sopenharmony_ci t1 = e + e1(b) + Ch(b, c, d) + 0x8cc70208 + W[59]; 1918c2ecf20Sopenharmony_ci t2 = e0(f) + Maj(f, g, h); a += t1; e = t1 + t2; 1928c2ecf20Sopenharmony_ci t1 = d + e1(a) + Ch(a, b, c) + 0x90befffa + W[60]; 1938c2ecf20Sopenharmony_ci t2 = e0(e) + Maj(e, f, g); h += t1; d = t1 + t2; 1948c2ecf20Sopenharmony_ci t1 = c + e1(h) + Ch(h, a, b) + 0xa4506ceb + W[61]; 1958c2ecf20Sopenharmony_ci t2 = e0(d) + Maj(d, e, f); g += t1; c = t1 + t2; 1968c2ecf20Sopenharmony_ci t1 = b + e1(g) + Ch(g, h, a) + 0xbef9a3f7 + W[62]; 1978c2ecf20Sopenharmony_ci t2 = e0(c) + Maj(c, d, e); f += t1; b = t1 + t2; 1988c2ecf20Sopenharmony_ci t1 = a + e1(f) + Ch(f, g, h) + 0xc67178f2 + W[63]; 1998c2ecf20Sopenharmony_ci t2 = e0(b) + Maj(b, c, d); e += t1; a = t1 + t2; 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci state[0] += a; state[1] += b; state[2] += c; state[3] += d; 2028c2ecf20Sopenharmony_ci state[4] += e; state[5] += f; state[6] += g; state[7] += h; 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci /* clear any sensitive info... */ 2058c2ecf20Sopenharmony_ci a = b = c = d = e = f = g = h = t1 = t2 = 0; 2068c2ecf20Sopenharmony_ci memzero_explicit(W, 64 * sizeof(u32)); 2078c2ecf20Sopenharmony_ci} 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_civoid sha256_update(struct sha256_state *sctx, const u8 *data, unsigned int len) 2108c2ecf20Sopenharmony_ci{ 2118c2ecf20Sopenharmony_ci unsigned int partial, done; 2128c2ecf20Sopenharmony_ci const u8 *src; 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_ci partial = sctx->count & 0x3f; 2158c2ecf20Sopenharmony_ci sctx->count += len; 2168c2ecf20Sopenharmony_ci done = 0; 2178c2ecf20Sopenharmony_ci src = data; 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ci if ((partial + len) > 63) { 2208c2ecf20Sopenharmony_ci if (partial) { 2218c2ecf20Sopenharmony_ci done = -partial; 2228c2ecf20Sopenharmony_ci memcpy(sctx->buf + partial, data, done + 64); 2238c2ecf20Sopenharmony_ci src = sctx->buf; 2248c2ecf20Sopenharmony_ci } 2258c2ecf20Sopenharmony_ci 2268c2ecf20Sopenharmony_ci do { 2278c2ecf20Sopenharmony_ci sha256_transform(sctx->state, src); 2288c2ecf20Sopenharmony_ci done += 64; 2298c2ecf20Sopenharmony_ci src = data + done; 2308c2ecf20Sopenharmony_ci } while (done + 63 < len); 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ci partial = 0; 2338c2ecf20Sopenharmony_ci } 2348c2ecf20Sopenharmony_ci memcpy(sctx->buf + partial, src, len - done); 2358c2ecf20Sopenharmony_ci} 2368c2ecf20Sopenharmony_ciEXPORT_SYMBOL(sha256_update); 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_civoid sha224_update(struct sha256_state *sctx, const u8 *data, unsigned int len) 2398c2ecf20Sopenharmony_ci{ 2408c2ecf20Sopenharmony_ci sha256_update(sctx, data, len); 2418c2ecf20Sopenharmony_ci} 2428c2ecf20Sopenharmony_ciEXPORT_SYMBOL(sha224_update); 2438c2ecf20Sopenharmony_ci 2448c2ecf20Sopenharmony_cistatic void __sha256_final(struct sha256_state *sctx, u8 *out, int digest_words) 2458c2ecf20Sopenharmony_ci{ 2468c2ecf20Sopenharmony_ci __be32 *dst = (__be32 *)out; 2478c2ecf20Sopenharmony_ci __be64 bits; 2488c2ecf20Sopenharmony_ci unsigned int index, pad_len; 2498c2ecf20Sopenharmony_ci int i; 2508c2ecf20Sopenharmony_ci static const u8 padding[64] = { 0x80, }; 2518c2ecf20Sopenharmony_ci 2528c2ecf20Sopenharmony_ci /* Save number of bits */ 2538c2ecf20Sopenharmony_ci bits = cpu_to_be64(sctx->count << 3); 2548c2ecf20Sopenharmony_ci 2558c2ecf20Sopenharmony_ci /* Pad out to 56 mod 64. */ 2568c2ecf20Sopenharmony_ci index = sctx->count & 0x3f; 2578c2ecf20Sopenharmony_ci pad_len = (index < 56) ? (56 - index) : ((64+56) - index); 2588c2ecf20Sopenharmony_ci sha256_update(sctx, padding, pad_len); 2598c2ecf20Sopenharmony_ci 2608c2ecf20Sopenharmony_ci /* Append length (before padding) */ 2618c2ecf20Sopenharmony_ci sha256_update(sctx, (const u8 *)&bits, sizeof(bits)); 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_ci /* Store state in digest */ 2648c2ecf20Sopenharmony_ci for (i = 0; i < digest_words; i++) 2658c2ecf20Sopenharmony_ci put_unaligned_be32(sctx->state[i], &dst[i]); 2668c2ecf20Sopenharmony_ci 2678c2ecf20Sopenharmony_ci /* Zeroize sensitive information. */ 2688c2ecf20Sopenharmony_ci memset(sctx, 0, sizeof(*sctx)); 2698c2ecf20Sopenharmony_ci} 2708c2ecf20Sopenharmony_ci 2718c2ecf20Sopenharmony_civoid sha256_final(struct sha256_state *sctx, u8 *out) 2728c2ecf20Sopenharmony_ci{ 2738c2ecf20Sopenharmony_ci __sha256_final(sctx, out, 8); 2748c2ecf20Sopenharmony_ci} 2758c2ecf20Sopenharmony_ciEXPORT_SYMBOL(sha256_final); 2768c2ecf20Sopenharmony_ci 2778c2ecf20Sopenharmony_civoid sha224_final(struct sha256_state *sctx, u8 *out) 2788c2ecf20Sopenharmony_ci{ 2798c2ecf20Sopenharmony_ci __sha256_final(sctx, out, 7); 2808c2ecf20Sopenharmony_ci} 2818c2ecf20Sopenharmony_ciEXPORT_SYMBOL(sha224_final); 2828c2ecf20Sopenharmony_ci 2838c2ecf20Sopenharmony_civoid sha256(const u8 *data, unsigned int len, u8 *out) 2848c2ecf20Sopenharmony_ci{ 2858c2ecf20Sopenharmony_ci struct sha256_state sctx; 2868c2ecf20Sopenharmony_ci 2878c2ecf20Sopenharmony_ci sha256_init(&sctx); 2888c2ecf20Sopenharmony_ci sha256_update(&sctx, data, len); 2898c2ecf20Sopenharmony_ci sha256_final(&sctx, out); 2908c2ecf20Sopenharmony_ci} 2918c2ecf20Sopenharmony_ciEXPORT_SYMBOL(sha256); 2928c2ecf20Sopenharmony_ci 2938c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 294