summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjsing <>2023-05-27 18:39:03 +0000
committerjsing <>2023-05-27 18:39:03 +0000
commitd4cbaebb201580abb6146f23b603e976b071aa6c (patch)
treeb3cdedd04ddc7a7851fe850e11e9a1030e7aa5a0
parent4de21666f8205fc59325362100bde121ed1b1bf2 (diff)
downloadopenbsd-d4cbaebb201580abb6146f23b603e976b071aa6c.tar.gz
openbsd-d4cbaebb201580abb6146f23b603e976b071aa6c.tar.bz2
openbsd-d4cbaebb201580abb6146f23b603e976b071aa6c.zip
Implement SHA256_{Update,Transform,Final}() directly in sha256.c.
m32_common.h is a typical OpenSSL macro horror show - copy the update, transform and final functions from md32_common.h, manually expanding the macros for SHA256. This will allow for further clean up to occur. No change in generated assembly. ok beck@ tb@
-rw-r--r--src/lib/libcrypto/sha/sha256.c107
1 files changed, 103 insertions, 4 deletions
diff --git a/src/lib/libcrypto/sha/sha256.c b/src/lib/libcrypto/sha/sha256.c
index 6c58321756..d1a16c0446 100644
--- a/src/lib/libcrypto/sha/sha256.c
+++ b/src/lib/libcrypto/sha/sha256.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: sha256.c,v 1.15 2023/03/29 05:34:01 jsing Exp $ */ 1/* $OpenBSD: sha256.c,v 1.16 2023/05/27 18:39:03 jsing Exp $ */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 1998-2011 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 1998-2011 The OpenSSL Project. All rights reserved.
4 * 4 *
@@ -184,17 +184,116 @@ SHA224_Final(unsigned char *md, SHA256_CTX *c)
184 } \ 184 } \
185 } while (0) 185 } while (0)
186 186
187#define HASH_UPDATE SHA256_Update
188#define HASH_TRANSFORM SHA256_Transform
189#define HASH_FINAL SHA256_Final
190#define HASH_BLOCK_DATA_ORDER sha256_block_data_order 187#define HASH_BLOCK_DATA_ORDER sha256_block_data_order
191#ifndef SHA256_ASM 188#ifndef SHA256_ASM
192static 189static
193#endif 190#endif
194void sha256_block_data_order (SHA256_CTX *ctx, const void *in, size_t num); 191void sha256_block_data_order (SHA256_CTX *ctx, const void *in, size_t num);
195 192
193#define HASH_NO_UPDATE
194#define HASH_NO_TRANSFORM
195#define HASH_NO_FINAL
196
196#include "md32_common.h" 197#include "md32_common.h"
197 198
199int
200SHA256_Update(HASH_CTX *c, const void *data_, size_t len)
201{
202 const unsigned char *data = data_;
203 unsigned char *p;
204 SHA_LONG l;
205 size_t n;
206
207 if (len == 0)
208 return 1;
209
210 l = (c->Nl + (((SHA_LONG)len) << 3))&0xffffffffUL;
211 /* 95-05-24 eay Fixed a bug with the overflow handling, thanks to
212 * Wei Dai <weidai@eskimo.com> for pointing it out. */
213 if (l < c->Nl) /* overflow */
214 c->Nh++;
215 c->Nh+=(SHA_LONG)(len>>29); /* might cause compiler warning on 16-bit */
216 c->Nl = l;
217
218 n = c->num;
219 if (n != 0) {
220 p = (unsigned char *)c->data;
221
222 if (len >= SHA_CBLOCK || len + n >= SHA_CBLOCK) {
223 memcpy (p + n, data, SHA_CBLOCK - n);
224 sha256_block_data_order(c, p, 1);
225 n = SHA_CBLOCK - n;
226 data += n;
227 len -= n;
228 c->num = 0;
229 memset (p,0,SHA_CBLOCK); /* keep it zeroed */
230 } else {
231 memcpy (p + n, data, len);
232 c->num += (unsigned int)len;
233 return 1;
234 }
235 }
236
237 n = len/SHA_CBLOCK;
238 if (n > 0) {
239 sha256_block_data_order(c, data, n);
240 n *= SHA_CBLOCK;
241 data += n;
242 len -= n;
243 }
244
245 if (len != 0) {
246 p = (unsigned char *)c->data;
247 c->num = (unsigned int)len;
248 memcpy (p, data, len);
249 }
250 return 1;
251}
252
253void
254SHA256_Transform(HASH_CTX *c, const unsigned char *data)
255{
256 sha256_block_data_order(c, data, 1);
257}
258
259int
260SHA256_Final(unsigned char *md, HASH_CTX *c)
261{
262 unsigned char *p = (unsigned char *)c->data;
263 size_t n = c->num;
264
265 p[n] = 0x80; /* there is always room for one */
266 n++;
267
268 if (n > (SHA_CBLOCK - 8)) {
269 memset (p + n, 0, SHA_CBLOCK - n);
270 n = 0;
271 sha256_block_data_order(c, p, 1);
272 }
273 memset (p + n, 0, SHA_CBLOCK - 8 - n);
274
275 p += SHA_CBLOCK - 8;
276#if defined(DATA_ORDER_IS_BIG_ENDIAN)
277 HOST_l2c(c->Nh, p);
278 HOST_l2c(c->Nl, p);
279#elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
280 HOST_l2c(c->Nl, p);
281 HOST_l2c(c->Nh, p);
282#endif
283 p -= SHA_CBLOCK;
284 sha256_block_data_order(c, p, 1);
285 c->num = 0;
286 memset (p, 0, SHA_CBLOCK);
287
288#ifndef HASH_MAKE_STRING
289#error "HASH_MAKE_STRING must be defined!"
290#else
291 HASH_MAKE_STRING(c, md);
292#endif
293
294 return 1;
295}
296
198#ifndef SHA256_ASM 297#ifndef SHA256_ASM
199static const SHA_LONG K256[64] = { 298static const SHA_LONG K256[64] = {
200 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 299 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,