summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorjsing <>2024-03-27 06:15:18 +0000
committerjsing <>2024-03-27 06:15:18 +0000
commit2fe478ba1a2a8eea9d2cde4c54ab6bc109741ed7 (patch)
tree18d362ab618a6944d0b65846e93fb5dd733e5611 /src/lib
parent9b053635b6df53639cb40a0a0d5d55a030353509 (diff)
downloadopenbsd-2fe478ba1a2a8eea9d2cde4c54ab6bc109741ed7.tar.gz
openbsd-2fe478ba1a2a8eea9d2cde4c54ab6bc109741ed7.tar.bz2
openbsd-2fe478ba1a2a8eea9d2cde4c54ab6bc109741ed7.zip
Demacro md4.
Use static inline functions instead of macros. Also number rounds as per the RFC. ok joshua@ tb@
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/libcrypto/md4/md4.c184
1 files changed, 84 insertions, 100 deletions
diff --git a/src/lib/libcrypto/md4/md4.c b/src/lib/libcrypto/md4/md4.c
index 1889c6facb..f25595c155 100644
--- a/src/lib/libcrypto/md4/md4.c
+++ b/src/lib/libcrypto/md4/md4.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: md4.c,v 1.15 2024/03/26 12:23:02 jsing Exp $ */ 1/* $OpenBSD: md4.c,v 1.16 2024/03/27 06:15:18 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 *
@@ -57,6 +57,7 @@
57 */ 57 */
58 58
59#include <stdio.h> 59#include <stdio.h>
60#include <stdint.h>
60#include <stdlib.h> 61#include <stdlib.h>
61#include <string.h> 62#include <string.h>
62 63
@@ -69,61 +70,46 @@
69/* Ensure that MD4_LONG and uint32_t are equivalent size. */ 70/* Ensure that MD4_LONG and uint32_t are equivalent size. */
70CTASSERT(sizeof(MD4_LONG) == sizeof(uint32_t)); 71CTASSERT(sizeof(MD4_LONG) == sizeof(uint32_t));
71 72
72__BEGIN_HIDDEN_DECLS 73static inline uint32_t
73 74md4_f(uint32_t x, uint32_t y, uint32_t z)
74void md4_block_data_order (MD4_CTX *c, const void *p, size_t num); 75{
75 76 return (x & y) | (~x & z);
76__END_HIDDEN_DECLS 77}
77
78#define DATA_ORDER_IS_LITTLE_ENDIAN
79
80#define HASH_LONG MD4_LONG
81#define HASH_CTX MD4_CTX
82#define HASH_CBLOCK MD4_CBLOCK
83#define HASH_UPDATE MD4_Update
84#define HASH_TRANSFORM MD4_Transform
85#define HASH_FINAL MD4_Final
86#define HASH_BLOCK_DATA_ORDER md4_block_data_order
87
88#define HASH_NO_UPDATE
89#define HASH_NO_TRANSFORM
90#define HASH_NO_FINAL
91
92#include "md32_common.h"
93
94/*
95#define F(x,y,z) (((x) & (y)) | ((~(x)) & (z)))
96#define G(x,y,z) (((x) & (y)) | ((x) & ((z))) | ((y) & ((z))))
97*/
98 78
99/* As pointed out by Wei Dai <weidai@eskimo.com>, the above can be 79static inline uint32_t
100 * simplified to the code below. Wei attributes these optimizations 80md4_g(uint32_t x, uint32_t y, uint32_t z)
101 * to Peter Gutmann's SHS code, and he attributes it to Rich Schroeppel. 81{
102 */ 82 return (x & y) | (x & z) | (y & z);
103#define F(b,c,d) ((((c) ^ (d)) & (b)) ^ (d)) 83}
104#define G(b,c,d) (((b) & (c)) | ((b) & (d)) | ((c) & (d)))
105#define H(b,c,d) ((b) ^ (c) ^ (d))
106 84
107#define R0(a,b,c,d,k,s,t) { \ 85static inline uint32_t
108 a+=((k)+(t)+F((b),(c),(d))); \ 86md4_h(uint32_t x, uint32_t y, uint32_t z)
109 a=ROTATE(a,s); }; 87{
88 return x ^ y ^ z;
89}
110 90
111#define R1(a,b,c,d,k,s,t) { \ 91static inline void
112 a+=((k)+(t)+G((b),(c),(d))); \ 92md4_round1(uint32_t *a, uint32_t b, uint32_t c, uint32_t d, uint32_t x,
113 a=ROTATE(a,s); };\ 93 uint32_t s)
94{
95 *a = crypto_rol_u32(*a + md4_f(b, c, d) + x, s);
96}
114 97
115#define R2(a,b,c,d,k,s,t) { \ 98static inline void
116 a+=((k)+(t)+H((b),(c),(d))); \ 99md4_round2(uint32_t *a, uint32_t b, uint32_t c, uint32_t d, uint32_t x,
117 a=ROTATE(a,s); }; 100 uint32_t s)
101{
102 *a = crypto_rol_u32(*a + md4_g(b, c, d) + x + 0x5a827999UL, s);
103}
118 104
119/* Implemented from RFC1186 The MD4 Message-Digest Algorithm 105static inline void
120 */ 106md4_round3(uint32_t *a, uint32_t b, uint32_t c, uint32_t d, uint32_t x,
107 uint32_t s)
108{
109 *a = crypto_rol_u32(*a + md4_h(b, c, d) + x + 0x6ed9eba1UL, s);
110}
121 111
122#ifndef md4_block_data_order 112static void
123#ifdef X
124#undef X
125#endif
126void
127md4_block_data_order(MD4_CTX *c, const void *_in, size_t num) 113md4_block_data_order(MD4_CTX *c, const void *_in, size_t num)
128{ 114{
129 const uint8_t *in = _in; 115 const uint8_t *in = _in;
@@ -178,59 +164,58 @@ md4_block_data_order(MD4_CTX *c, const void *_in, size_t num)
178 } 164 }
179 in += MD4_CBLOCK; 165 in += MD4_CBLOCK;
180 166
181 /* Round 0 */ 167 md4_round1(&A, B, C, D, X0, 3);
182 R0(A, B, C, D, X0, 3, 0); 168 md4_round1(&D, A, B, C, X1, 7);
183 R0(D, A, B, C, X1, 7, 0); 169 md4_round1(&C, D, A, B, X2, 11);
184 R0(C, D, A, B, X2, 11, 0); 170 md4_round1(&B, C, D, A, X3, 19);
185 R0(B, C, D, A, X3, 19, 0); 171 md4_round1(&A, B, C, D, X4, 3);
186 R0(A, B, C, D, X4, 3, 0); 172 md4_round1(&D, A, B, C, X5, 7);
187 R0(D, A, B, C, X5, 7, 0); 173 md4_round1(&C, D, A, B, X6, 11);
188 R0(C, D, A, B, X6, 11, 0); 174 md4_round1(&B, C, D, A, X7, 19);
189 R0(B, C, D, A, X7, 19, 0); 175 md4_round1(&A, B, C, D, X8, 3);
190 R0(A, B, C, D, X8, 3, 0); 176 md4_round1(&D, A, B, C, X9, 7);
191 R0(D, A,B, C,X9, 7, 0); 177 md4_round1(&C, D, A, B, X10, 11);
192 R0(C, D,A, B,X10, 11, 0); 178 md4_round1(&B, C, D, A, X11, 19);
193 R0(B, C,D, A,X11, 19, 0); 179 md4_round1(&A, B, C, D, X12, 3);
194 R0(A, B,C, D,X12, 3, 0); 180 md4_round1(&D, A, B, C, X13, 7);
195 R0(D, A,B, C,X13, 7, 0); 181 md4_round1(&C, D, A, B, X14, 11);
196 R0(C, D,A, B,X14, 11, 0); 182 md4_round1(&B, C, D, A, X15, 19);
197 R0(B, C,D, A,X15, 19, 0);
198 183
199 /* Round 1 */ 184 /* Round 1 */
200 R1(A, B, C, D, X0, 3, 0x5A827999L); 185 md4_round2(&A, B, C, D, X0, 3);
201 R1(D, A, B, C, X4, 5, 0x5A827999L); 186 md4_round2(&D, A, B, C, X4, 5);
202 R1(C, D, A, B, X8, 9, 0x5A827999L); 187 md4_round2(&C, D, A, B, X8, 9);
203 R1(B, C, D, A, X12, 13, 0x5A827999L); 188 md4_round2(&B, C, D, A, X12, 13);
204 R1(A, B, C, D, X1, 3, 0x5A827999L); 189 md4_round2(&A, B, C, D, X1, 3);
205 R1(D, A, B, C, X5, 5, 0x5A827999L); 190 md4_round2(&D, A, B, C, X5, 5);
206 R1(C, D, A, B, X9, 9, 0x5A827999L); 191 md4_round2(&C, D, A, B, X9, 9);
207 R1(B, C, D, A, X13, 13, 0x5A827999L); 192 md4_round2(&B, C, D, A, X13, 13);
208 R1(A, B, C, D, X2, 3, 0x5A827999L); 193 md4_round2(&A, B, C, D, X2, 3);
209 R1(D, A, B, C, X6, 5, 0x5A827999L); 194 md4_round2(&D, A, B, C, X6, 5);
210 R1(C, D, A, B, X10, 9, 0x5A827999L); 195 md4_round2(&C, D, A, B, X10, 9);
211 R1(B, C, D, A, X14, 13, 0x5A827999L); 196 md4_round2(&B, C, D, A, X14, 13);
212 R1(A, B, C, D, X3, 3, 0x5A827999L); 197 md4_round2(&A, B, C, D, X3, 3);
213 R1(D, A, B, C, X7, 5, 0x5A827999L); 198 md4_round2(&D, A, B, C, X7, 5);
214 R1(C, D, A, B, X11, 9, 0x5A827999L); 199 md4_round2(&C, D, A, B, X11, 9);
215 R1(B, C, D, A, X15, 13, 0x5A827999L); 200 md4_round2(&B, C, D, A, X15, 13);
216 201
217 /* Round 2 */ 202 /* Round 2 */
218 R2(A, B, C, D, X0, 3, 0x6ED9EBA1L); 203 md4_round3(&A, B, C, D, X0, 3);
219 R2(D, A, B, C, X8, 9, 0x6ED9EBA1L); 204 md4_round3(&D, A, B, C, X8, 9);
220 R2(C, D, A, B, X4, 11, 0x6ED9EBA1L); 205 md4_round3(&C, D, A, B, X4, 11);
221 R2(B, C, D, A, X12, 15, 0x6ED9EBA1L); 206 md4_round3(&B, C, D, A, X12, 15);
222 R2(A, B, C, D, X2, 3, 0x6ED9EBA1L); 207 md4_round3(&A, B, C, D, X2, 3);
223 R2(D, A, B, C, X10, 9, 0x6ED9EBA1L); 208 md4_round3(&D, A, B, C, X10, 9);
224 R2(C, D, A, B, X6, 11, 0x6ED9EBA1L); 209 md4_round3(&C, D, A, B, X6, 11);
225 R2(B, C, D, A, X14, 15, 0x6ED9EBA1L); 210 md4_round3(&B, C, D, A, X14, 15);
226 R2(A, B, C, D, X1, 3, 0x6ED9EBA1L); 211 md4_round3(&A, B, C, D, X1, 3);
227 R2(D, A, B, C, X9, 9, 0x6ED9EBA1L); 212 md4_round3(&D, A, B, C, X9, 9);
228 R2(C, D, A, B, X5, 11, 0x6ED9EBA1L); 213 md4_round3(&C, D, A, B, X5, 11);
229 R2(B, C, D, A, X13, 15, 0x6ED9EBA1L); 214 md4_round3(&B, C, D, A, X13, 15);
230 R2(A, B, C, D, X3, 3, 0x6ED9EBA1L); 215 md4_round3(&A, B, C, D, X3, 3);
231 R2(D, A, B, C, X11, 9, 0x6ED9EBA1L); 216 md4_round3(&D, A, B, C, X11, 9);
232 R2(C, D, A, B, X7, 11, 0x6ED9EBA1L); 217 md4_round3(&C, D, A, B, X7, 11);
233 R2(B, C, D, A, X15, 15, 0x6ED9EBA1L); 218 md4_round3(&B, C, D, A, X15, 15);
234 219
235 A = c->A += A; 220 A = c->A += A;
236 B = c->B += B; 221 B = c->B += B;
@@ -238,7 +223,6 @@ md4_block_data_order(MD4_CTX *c, const void *_in, size_t num)
238 D = c->D += D; 223 D = c->D += D;
239 } 224 }
240} 225}
241#endif
242 226
243int 227int
244MD4_Init(MD4_CTX *c) 228MD4_Init(MD4_CTX *c)