xref: /isa-l_crypto/mh_sha256/sha256_for_mh_sha256.c (revision e3f7d4fb1bd3dcbfb75b87e0c61870e751d613aa)
1 /**********************************************************************
2   Copyright(c) 2011-2017 Intel Corporation All rights reserved.
3 
4   Redistribution and use in source and binary forms, with or without
5   modification, are permitted provided that the following conditions
6   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
11       the documentation and/or other materials provided with the
12       distribution.
13     * Neither the name of Intel Corporation nor the names of its
14       contributors may be used to endorse or promote products derived
15       from this software without specific prior written permission.
16 
17   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 **********************************************************************/
29 
30 #include "mh_sha256_internal.h"
31 #include <string.h>
32 
33 ////////////////////////////////////////////////////////////////////////
34 ////////////////////////////////////////////////////////////////////////
35 // Reference SHA256 Functions for mh_sha256
36 ////////////////////////////////////////////////////////////////////////
37 ////////////////////////////////////////////////////////////////////////
38 
39 #define W(x) w[(x) & 15]
40 
41 #define step(i,a,b,c,d,e,f,g,h,k) \
42 	if (i<16) W(i) = to_be32(ww[i]); \
43 	else \
44 	W(i) = W(i-16) + S0(W(i-15)) + W(i-7) + S1(W(i-2)); \
45 	t2 = s0(a) + maj(a,b,c); \
46 	t1 = h + s1(e) + ch(e,f,g) + k + W(i); \
47 	d += t1; \
48 	h = t1 + t2;
49 
50 void sha256_single_for_mh_sha256(const uint8_t * data, uint32_t digest[])
51 {
52 	uint32_t a, b, c, d, e, f, g, h, t1, t2;
53 	uint32_t w[16];
54 	uint32_t *ww = (uint32_t *) data;
55 
56 	a = digest[0];
57 	b = digest[1];
58 	c = digest[2];
59 	d = digest[3];
60 	e = digest[4];
61 	f = digest[5];
62 	g = digest[6];
63 	h = digest[7];
64 
65 	step(0, a, b, c, d, e, f, g, h, 0x428a2f98);
66 	step(1, h, a, b, c, d, e, f, g, 0x71374491);
67 	step(2, g, h, a, b, c, d, e, f, 0xb5c0fbcf);
68 	step(3, f, g, h, a, b, c, d, e, 0xe9b5dba5);
69 	step(4, e, f, g, h, a, b, c, d, 0x3956c25b);
70 	step(5, d, e, f, g, h, a, b, c, 0x59f111f1);
71 	step(6, c, d, e, f, g, h, a, b, 0x923f82a4);
72 	step(7, b, c, d, e, f, g, h, a, 0xab1c5ed5);
73 	step(8, a, b, c, d, e, f, g, h, 0xd807aa98);
74 	step(9, h, a, b, c, d, e, f, g, 0x12835b01);
75 	step(10, g, h, a, b, c, d, e, f, 0x243185be);
76 	step(11, f, g, h, a, b, c, d, e, 0x550c7dc3);
77 	step(12, e, f, g, h, a, b, c, d, 0x72be5d74);
78 	step(13, d, e, f, g, h, a, b, c, 0x80deb1fe);
79 	step(14, c, d, e, f, g, h, a, b, 0x9bdc06a7);
80 	step(15, b, c, d, e, f, g, h, a, 0xc19bf174);
81 	step(16, a, b, c, d, e, f, g, h, 0xe49b69c1);
82 	step(17, h, a, b, c, d, e, f, g, 0xefbe4786);
83 	step(18, g, h, a, b, c, d, e, f, 0x0fc19dc6);
84 	step(19, f, g, h, a, b, c, d, e, 0x240ca1cc);
85 	step(20, e, f, g, h, a, b, c, d, 0x2de92c6f);
86 	step(21, d, e, f, g, h, a, b, c, 0x4a7484aa);
87 	step(22, c, d, e, f, g, h, a, b, 0x5cb0a9dc);
88 	step(23, b, c, d, e, f, g, h, a, 0x76f988da);
89 	step(24, a, b, c, d, e, f, g, h, 0x983e5152);
90 	step(25, h, a, b, c, d, e, f, g, 0xa831c66d);
91 	step(26, g, h, a, b, c, d, e, f, 0xb00327c8);
92 	step(27, f, g, h, a, b, c, d, e, 0xbf597fc7);
93 	step(28, e, f, g, h, a, b, c, d, 0xc6e00bf3);
94 	step(29, d, e, f, g, h, a, b, c, 0xd5a79147);
95 	step(30, c, d, e, f, g, h, a, b, 0x06ca6351);
96 	step(31, b, c, d, e, f, g, h, a, 0x14292967);
97 	step(32, a, b, c, d, e, f, g, h, 0x27b70a85);
98 	step(33, h, a, b, c, d, e, f, g, 0x2e1b2138);
99 	step(34, g, h, a, b, c, d, e, f, 0x4d2c6dfc);
100 	step(35, f, g, h, a, b, c, d, e, 0x53380d13);
101 	step(36, e, f, g, h, a, b, c, d, 0x650a7354);
102 	step(37, d, e, f, g, h, a, b, c, 0x766a0abb);
103 	step(38, c, d, e, f, g, h, a, b, 0x81c2c92e);
104 	step(39, b, c, d, e, f, g, h, a, 0x92722c85);
105 	step(40, a, b, c, d, e, f, g, h, 0xa2bfe8a1);
106 	step(41, h, a, b, c, d, e, f, g, 0xa81a664b);
107 	step(42, g, h, a, b, c, d, e, f, 0xc24b8b70);
108 	step(43, f, g, h, a, b, c, d, e, 0xc76c51a3);
109 	step(44, e, f, g, h, a, b, c, d, 0xd192e819);
110 	step(45, d, e, f, g, h, a, b, c, 0xd6990624);
111 	step(46, c, d, e, f, g, h, a, b, 0xf40e3585);
112 	step(47, b, c, d, e, f, g, h, a, 0x106aa070);
113 	step(48, a, b, c, d, e, f, g, h, 0x19a4c116);
114 	step(49, h, a, b, c, d, e, f, g, 0x1e376c08);
115 	step(50, g, h, a, b, c, d, e, f, 0x2748774c);
116 	step(51, f, g, h, a, b, c, d, e, 0x34b0bcb5);
117 	step(52, e, f, g, h, a, b, c, d, 0x391c0cb3);
118 	step(53, d, e, f, g, h, a, b, c, 0x4ed8aa4a);
119 	step(54, c, d, e, f, g, h, a, b, 0x5b9cca4f);
120 	step(55, b, c, d, e, f, g, h, a, 0x682e6ff3);
121 	step(56, a, b, c, d, e, f, g, h, 0x748f82ee);
122 	step(57, h, a, b, c, d, e, f, g, 0x78a5636f);
123 	step(58, g, h, a, b, c, d, e, f, 0x84c87814);
124 	step(59, f, g, h, a, b, c, d, e, 0x8cc70208);
125 	step(60, e, f, g, h, a, b, c, d, 0x90befffa);
126 	step(61, d, e, f, g, h, a, b, c, 0xa4506ceb);
127 	step(62, c, d, e, f, g, h, a, b, 0xbef9a3f7);
128 	step(63, b, c, d, e, f, g, h, a, 0xc67178f2);
129 
130 	digest[0] += a;
131 	digest[1] += b;
132 	digest[2] += c;
133 	digest[3] += d;
134 	digest[4] += e;
135 	digest[5] += f;
136 	digest[6] += g;
137 	digest[7] += h;
138 }
139 
140 void sha256_for_mh_sha256(const uint8_t * input_data, uint32_t * digest, const uint32_t len)
141 {
142 	uint32_t i, j;
143 	uint8_t buf[2 * SHA256_BLOCK_SIZE];
144 
145 	digest[0] = MH_SHA256_H0;
146 	digest[1] = MH_SHA256_H1;
147 	digest[2] = MH_SHA256_H2;
148 	digest[3] = MH_SHA256_H3;
149 	digest[4] = MH_SHA256_H4;
150 	digest[5] = MH_SHA256_H5;
151 	digest[6] = MH_SHA256_H6;
152 	digest[7] = MH_SHA256_H7;
153 
154 	i = len;
155 	while (i >= SHA256_BLOCK_SIZE) {
156 		sha256_single_for_mh_sha256(input_data, digest);
157 		input_data += SHA256_BLOCK_SIZE;
158 		i -= SHA256_BLOCK_SIZE;
159 	}
160 
161 	memcpy(buf, input_data, i);
162 	buf[i++] = 0x80;
163 	for (j = i; j < ((2 * SHA256_BLOCK_SIZE) - 8); j++)
164 		buf[j] = 0;
165 
166 	if (i > SHA256_BLOCK_SIZE - 8)
167 		i = 2 * SHA256_BLOCK_SIZE;
168 	else
169 		i = SHA256_BLOCK_SIZE;
170 
171 	*(uint64_t *) (buf + i - 8) = to_be64((uint64_t) len * 8);
172 
173 	sha256_single_for_mh_sha256(buf, digest);
174 	if (i == (2 * SHA256_BLOCK_SIZE))
175 		sha256_single_for_mh_sha256(buf + SHA256_BLOCK_SIZE, digest);
176 }
177