summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/lib/libcrypto/crypto_internal.h33
-rw-r--r--src/lib/libcrypto/md5/md5.c91
2 files changed, 67 insertions, 57 deletions
diff --git a/src/lib/libcrypto/crypto_internal.h b/src/lib/libcrypto/crypto_internal.h
index 4fe868e9a1..e5742657d5 100644
--- a/src/lib/libcrypto/crypto_internal.h
+++ b/src/lib/libcrypto/crypto_internal.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: crypto_internal.h,v 1.6 2023/05/27 09:18:17 jsing Exp $ */ 1/* $OpenBSD: crypto_internal.h,v 1.7 2023/08/15 08:39:27 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -87,6 +87,37 @@ crypto_store_htobe64(uint8_t *dst, uint64_t v)
87} 87}
88#endif 88#endif
89 89
90/*
91 * crypto_load_le32toh() loads a 32 bit unsigned little endian value as a 32 bit
92 * unsigned host endian value, from the specified address in memory. The memory
93 * address may have any alignment.
94 */
95#ifndef HAVE_CRYPTO_LOAD_BE32TOH
96static inline uint32_t
97crypto_load_le32toh(const uint8_t *src)
98{
99 uint32_t v;
100
101 memcpy(&v, src, sizeof(v));
102
103 return le32toh(v);
104}
105#endif
106
107/*
108 * crypto_store_htole32() stores a 32 bit unsigned host endian value as a 32 bit
109 * unsigned little endian value, at the specified address in memory. The memory
110 * address may have any alignment.
111 */
112#ifndef HAVE_CRYPTO_STORE_HTOBE32
113static inline void
114crypto_store_htole32(uint8_t *dst, uint32_t v)
115{
116 v = htole32(v);
117 memcpy(dst, &v, sizeof(v));
118}
119#endif
120
90#ifndef HAVE_CRYPTO_ROL_U32 121#ifndef HAVE_CRYPTO_ROL_U32
91static inline uint32_t 122static inline uint32_t
92crypto_rol_u32(uint32_t v, size_t shift) 123crypto_rol_u32(uint32_t v, size_t shift)
diff --git a/src/lib/libcrypto/md5/md5.c b/src/lib/libcrypto/md5/md5.c
index 746c46f7ca..c2ee2958df 100644
--- a/src/lib/libcrypto/md5/md5.c
+++ b/src/lib/libcrypto/md5/md5.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: md5.c,v 1.17 2023/08/15 08:35:33 jsing Exp $ */ 1/* $OpenBSD: md5.c,v 1.18 2023/08/15 08:39:27 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -64,6 +64,11 @@
64 64
65#include <openssl/md5.h> 65#include <openssl/md5.h>
66 66
67#include "crypto_internal.h"
68
69/* Ensure that MD5_LONG and uint32_t are equivalent size. */
70CTASSERT(sizeof(MD5_LONG) == sizeof(uint32_t));
71
67#ifdef MD5_ASM 72#ifdef MD5_ASM
68void md5_block_asm_data_order(MD5_CTX *c, const void *p, size_t num); 73void md5_block_asm_data_order(MD5_CTX *c, const void *p, size_t num);
69#define md5_block_data_order md5_block_asm_data_order 74#define md5_block_data_order md5_block_asm_data_order
@@ -119,15 +124,14 @@ void md5_block_asm_data_order(MD5_CTX *c, const void *p, size_t num);
119 a=ROTATE(a,s); \ 124 a=ROTATE(a,s); \
120 a+=b; }; 125 a+=b; };
121 126
122/* Implemented from RFC1321 The MD5 Message-Digest Algorithm 127/* Implemented from RFC1321 The MD5 Message-Digest Algorithm. */
123 */
124 128
125#ifndef MD5_ASM 129#ifndef MD5_ASM
126static void 130static void
127md5_block_data_order(MD5_CTX *c, const void *data_, size_t num) 131md5_block_data_order(MD5_CTX *c, const void *_in, size_t num)
128{ 132{
129 const unsigned char *data = data_; 133 const uint8_t *in = _in;
130 MD5_LONG A, B, C, D, l; 134 MD5_LONG A, B, C, D;
131 MD5_LONG X0, X1, X2, X3, X4, X5, X6, X7, 135 MD5_LONG X0, X1, X2, X3, X4, X5, X6, X7,
132 X8, X9, X10, X11, X12, X13, X14, X15; 136 X8, X9, X10, X11, X12, X13, X14, X15;
133 137
@@ -137,53 +141,39 @@ md5_block_data_order(MD5_CTX *c, const void *data_, size_t num)
137 D = c->D; 141 D = c->D;
138 142
139 for (; num--; ) { 143 for (; num--; ) {
140 HOST_c2l(data, l); 144 X0 = crypto_load_le32toh(&in[0 * 4]);
141 X0 = l; 145 X1 = crypto_load_le32toh(&in[1 * 4]);
142 HOST_c2l(data, l); 146 X2 = crypto_load_le32toh(&in[2 * 4]);
143 X1 = l; 147 X3 = crypto_load_le32toh(&in[3 * 4]);
148 X4 = crypto_load_le32toh(&in[4 * 4]);
149 X5 = crypto_load_le32toh(&in[5 * 4]);
150 X6 = crypto_load_le32toh(&in[6 * 4]);
151 X7 = crypto_load_le32toh(&in[7 * 4]);
152 X8 = crypto_load_le32toh(&in[8 * 4]);
153 X9 = crypto_load_le32toh(&in[9 * 4]);
154 X10 = crypto_load_le32toh(&in[10 * 4]);
155 X11 = crypto_load_le32toh(&in[11 * 4]);
156 X12 = crypto_load_le32toh(&in[12 * 4]);
157 X13 = crypto_load_le32toh(&in[13 * 4]);
158 X14 = crypto_load_le32toh(&in[14 * 4]);
159 X15 = crypto_load_le32toh(&in[15 * 4]);
160 in += MD5_CBLOCK;
161
144 /* Round 0 */ 162 /* Round 0 */
145 R0(A, B, C, D, X0, 7, 0xd76aa478L); 163 R0(A, B, C, D, X0, 7, 0xd76aa478L);
146 HOST_c2l(data, l);
147 X2 = l;
148 R0(D, A, B, C, X1, 12, 0xe8c7b756L); 164 R0(D, A, B, C, X1, 12, 0xe8c7b756L);
149 HOST_c2l(data, l);
150 X3 = l;
151 R0(C, D, A, B, X2, 17, 0x242070dbL); 165 R0(C, D, A, B, X2, 17, 0x242070dbL);
152 HOST_c2l(data, l);
153 X4 = l;
154 R0(B, C, D, A, X3, 22, 0xc1bdceeeL); 166 R0(B, C, D, A, X3, 22, 0xc1bdceeeL);
155 HOST_c2l(data, l);
156 X5 = l;
157 R0(A, B, C, D, X4, 7, 0xf57c0fafL); 167 R0(A, B, C, D, X4, 7, 0xf57c0fafL);
158 HOST_c2l(data, l);
159 X6 = l;
160 R0(D, A, B, C, X5, 12, 0x4787c62aL); 168 R0(D, A, B, C, X5, 12, 0x4787c62aL);
161 HOST_c2l(data, l);
162 X7 = l;
163 R0(C, D, A, B, X6, 17, 0xa8304613L); 169 R0(C, D, A, B, X6, 17, 0xa8304613L);
164 HOST_c2l(data, l);
165 X8 = l;
166 R0(B, C, D, A, X7, 22, 0xfd469501L); 170 R0(B, C, D, A, X7, 22, 0xfd469501L);
167 HOST_c2l(data, l);
168 X9 = l;
169 R0(A, B, C, D, X8, 7, 0x698098d8L); 171 R0(A, B, C, D, X8, 7, 0x698098d8L);
170 HOST_c2l(data, l);
171 X10 = l;
172 R0(D, A, B, C, X9, 12, 0x8b44f7afL); 172 R0(D, A, B, C, X9, 12, 0x8b44f7afL);
173 HOST_c2l(data, l);
174 X11 = l;
175 R0(C, D, A, B, X10, 17, 0xffff5bb1L); 173 R0(C, D, A, B, X10, 17, 0xffff5bb1L);
176 HOST_c2l(data, l);
177 X12 = l;
178 R0(B, C, D, A, X11, 22, 0x895cd7beL); 174 R0(B, C, D, A, X11, 22, 0x895cd7beL);
179 HOST_c2l(data, l);
180 X13 = l;
181 R0(A, B, C, D, X12, 7, 0x6b901122L); 175 R0(A, B, C, D, X12, 7, 0x6b901122L);
182 HOST_c2l(data, l);
183 X14 = l;
184 R0(D, A, B, C, X13, 12, 0xfd987193L); 176 R0(D, A, B, C, X13, 12, 0xfd987193L);
185 HOST_c2l(data, l);
186 X15 = l;
187 R0(C, D, A, B, X14, 17, 0xa679438eL); 177 R0(C, D, A, B, X14, 17, 0xa679438eL);
188 R0(B, C, D, A, X15, 22, 0x49b40821L); 178 R0(B, C, D, A, X15, 22, 0x49b40821L);
189 /* Round 1 */ 179 /* Round 1 */
@@ -326,7 +316,6 @@ int
326MD5_Final(unsigned char *md, MD5_CTX *c) 316MD5_Final(unsigned char *md, MD5_CTX *c)
327{ 317{
328 unsigned char *p = (unsigned char *)c->data; 318 unsigned char *p = (unsigned char *)c->data;
329 unsigned long ll;
330 size_t n = c->num; 319 size_t n = c->num;
331 320
332 p[n] = 0x80; /* there is always room for one */ 321 p[n] = 0x80; /* there is always room for one */
@@ -337,29 +326,19 @@ MD5_Final(unsigned char *md, MD5_CTX *c)
337 n = 0; 326 n = 0;
338 md5_block_data_order(c, p, 1); 327 md5_block_data_order(c, p, 1);
339 } 328 }
329
340 memset(p + n, 0, MD5_CBLOCK - 8 - n); 330 memset(p + n, 0, MD5_CBLOCK - 8 - n);
331 c->data[MD5_LBLOCK - 2] = htole32(c->Nl);
332 c->data[MD5_LBLOCK - 1] = htole32(c->Nh);
341 333
342 p += MD5_CBLOCK - 8;
343#if defined(DATA_ORDER_IS_BIG_ENDIAN)
344 HOST_l2c(c->Nh, p);
345 HOST_l2c(c->Nl, p);
346#elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
347 HOST_l2c(c->Nl, p);
348 HOST_l2c(c->Nh, p);
349#endif
350 p -= MD5_CBLOCK;
351 md5_block_data_order(c, p, 1); 334 md5_block_data_order(c, p, 1);
352 c->num = 0; 335 c->num = 0;
353 memset(p, 0, MD5_CBLOCK); 336 memset(p, 0, MD5_CBLOCK);
354 337
355 ll = c->A; 338 crypto_store_htole32(&md[0 * 4], c->A);
356 HOST_l2c(ll, md); 339 crypto_store_htole32(&md[1 * 4], c->B);
357 ll = c->B; 340 crypto_store_htole32(&md[2 * 4], c->C);
358 HOST_l2c(ll, md); 341 crypto_store_htole32(&md[3 * 4], c->D);
359 ll = c->C;
360 HOST_l2c(ll, md);
361 ll = c->D;
362 HOST_l2c(ll, md);
363 342
364 return 1; 343 return 1;
365} 344}