1 /* ppp-md4.c - MD4 Digest implementation
3 * Copyright (c) 2022 Eivind Næss. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
17 * 3. The name(s) of the authors of this software must not be used to
18 * endorse or promote products derived from this software without
19 * prior written permission.
21 * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
22 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
23 * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
24 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
25 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
26 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
27 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29 * Sections of this code holds different copyright information.
38 #include "ppp-crypto-priv.h"
41 #ifdef OPENSSL_HAVE_MD4
42 #include <openssl/evp.h>
44 #if OPENSSL_VERSION_NUMBER < 0x10100000L
45 #define EVP_MD_CTX_free EVP_MD_CTX_destroy
46 #define EVP_MD_CTX_new EVP_MD_CTX_create
50 static int md4_init(PPP_MD_CTX *ctx)
53 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
55 if (EVP_DigestInit(mctx, EVP_md4())) {
59 EVP_MD_CTX_free(mctx);
65 static int md4_update(PPP_MD_CTX *ctx, const void *data, size_t len)
67 if (EVP_DigestUpdate((EVP_MD_CTX*) ctx->priv, data, len)) {
73 static int md4_final(PPP_MD_CTX *ctx, unsigned char *out, unsigned int *len)
75 if (EVP_DigestFinal((EVP_MD_CTX*) ctx->priv, out, len)) {
81 static void md4_clean(PPP_MD_CTX *ctx)
84 EVP_MD_CTX_free(ctx->priv);
90 #else // !OPENSSL_HAVE_MD4
96 ** ********************************************************************
97 ** md4.c -- Implementation of MD4 Message Digest Algorithm **
98 ** Updated: 2/16/90 by Ronald L. Rivest **
99 ** (C) 1990 RSA Data Security, Inc. **
100 ** ********************************************************************
105 ** -- Include md4.h in your program
106 ** -- Declare an MDstruct MD to hold the state of the digest
108 ** -- Initialize MD using MDbegin(&MD)
109 ** -- For each full block (64 bytes) X you wish to process, call
110 ** MD4Update(&MD,X,512)
111 ** (512 is the number of bits in a full block.)
112 ** -- For the last block (less than 64 bytes) you wish to process,
113 ** MD4Update(&MD,X,n)
114 ** where n is the number of bits in the partial block. A partial
115 ** block terminates the computation, so every MD computation
116 ** should terminate by processing a partial block, even if it
118 ** -- The message digest is available in MD.buffer[0] ...
119 ** MD.buffer[3]. (Least-significant byte of each word
120 ** should be output first.)
121 ** -- You can print out the digest using MDprint(&MD)
124 /* Implementation notes:
125 ** This implementation assumes that ints are 32-bit quantities.
128 /* MDstruct is the data structure for a message digest computation.
131 unsigned int buffer[4]; /* Holds 4-word result of MD computation */
132 unsigned char count[8]; /* Number of bits processed so far */
133 unsigned int done; /* Nonzero means MD computation finished */
137 /* Compile-time declarations of MD4 "magic constants".
139 #define I0 0x67452301 /* Initial values for MD buffer */
140 #define I1 0xefcdab89
141 #define I2 0x98badcfe
142 #define I3 0x10325476
143 #define C2 013240474631 /* round 2 constant = sqrt(2) in octal */
144 #define C3 015666365641 /* round 3 constant = sqrt(3) in octal */
145 /* C2 and C3 are from Knuth, The Art of Programming, Volume 2
146 ** (Seminumerical Algorithms), Second Edition (1981), Addison-Wesley.
147 ** Table 2, page 660.
150 #define fs1 3 /* round 1 shift amounts */
154 #define gs1 3 /* round 2 shift amounts */
158 #define hs1 3 /* round 3 shift amounts */
163 /* Compile-time macro declarations for MD4.
164 ** Note: The "rot" operator uses the variable "tmp".
165 ** It assumes tmp is declared as unsigned int, so that the >>
166 ** operator will shift in zeros rather than extending the sign bit.
168 #define f(X,Y,Z) ((X&Y) | ((~X)&Z))
169 #define g(X,Y,Z) ((X&Y) | (X&Z) | (Y&Z))
170 #define h(X,Y,Z) (X^Y^Z)
171 #define rot(X,S) (tmp=X,(tmp<<S) | (tmp>>(32-S)))
172 #define ff(A,B,C,D,i,s) A = rot((A + f(B,C,D) + X[i]),s)
173 #define gg(A,B,C,D,i,s) A = rot((A + g(B,C,D) + X[i] + C2),s)
174 #define hh(A,B,C,D,i,s) A = rot((A + h(B,C,D) + X[i] + C3),s)
177 ** Print message digest buffer MDp as 32 hexadecimal digits.
178 ** Order is from low-order byte of buffer[0] to high-order byte of
180 ** Each byte is printed with high-order hexadecimal digit first.
181 ** This is a user-callable routine.
184 MD4Print(MD4_CTX *MDp)
189 printf("%02x",(MDp->buffer[i]>>j) & 0xFF);
193 ** Initialize message digest buffer MDp.
194 ** This is a user-callable routine.
197 MD4Init(MD4_CTX *MDp)
204 for (i=0;i<8;i++) MDp->count[i] = 0;
209 ** Update message digest buffer MDp->buffer using 16-word data block X.
210 ** Assumes all 16 words of X are full of data.
211 ** Does not update MDp->count.
212 ** This routine is not user-callable.
215 MDblock(MD4_CTX *MDp, unsigned char *Xb)
217 register unsigned int tmp, A, B, C, D;
221 for (i = 0; i < 16; ++i) {
222 X[i] = Xb[0] + (Xb[1] << 8) + (Xb[2] << 16) + (Xb[3] << 24);
230 /* Update the message digest buffer */
231 ff(A , B , C , D , 0 , fs1); /* Round 1 */
232 ff(D , A , B , C , 1 , fs2);
233 ff(C , D , A , B , 2 , fs3);
234 ff(B , C , D , A , 3 , fs4);
235 ff(A , B , C , D , 4 , fs1);
236 ff(D , A , B , C , 5 , fs2);
237 ff(C , D , A , B , 6 , fs3);
238 ff(B , C , D , A , 7 , fs4);
239 ff(A , B , C , D , 8 , fs1);
240 ff(D , A , B , C , 9 , fs2);
241 ff(C , D , A , B , 10 , fs3);
242 ff(B , C , D , A , 11 , fs4);
243 ff(A , B , C , D , 12 , fs1);
244 ff(D , A , B , C , 13 , fs2);
245 ff(C , D , A , B , 14 , fs3);
246 ff(B , C , D , A , 15 , fs4);
247 gg(A , B , C , D , 0 , gs1); /* Round 2 */
248 gg(D , A , B , C , 4 , gs2);
249 gg(C , D , A , B , 8 , gs3);
250 gg(B , C , D , A , 12 , gs4);
251 gg(A , B , C , D , 1 , gs1);
252 gg(D , A , B , C , 5 , gs2);
253 gg(C , D , A , B , 9 , gs3);
254 gg(B , C , D , A , 13 , gs4);
255 gg(A , B , C , D , 2 , gs1);
256 gg(D , A , B , C , 6 , gs2);
257 gg(C , D , A , B , 10 , gs3);
258 gg(B , C , D , A , 14 , gs4);
259 gg(A , B , C , D , 3 , gs1);
260 gg(D , A , B , C , 7 , gs2);
261 gg(C , D , A , B , 11 , gs3);
262 gg(B , C , D , A , 15 , gs4);
263 hh(A , B , C , D , 0 , hs1); /* Round 3 */
264 hh(D , A , B , C , 8 , hs2);
265 hh(C , D , A , B , 4 , hs3);
266 hh(B , C , D , A , 12 , hs4);
267 hh(A , B , C , D , 2 , hs1);
268 hh(D , A , B , C , 10 , hs2);
269 hh(C , D , A , B , 6 , hs3);
270 hh(B , C , D , A , 14 , hs4);
271 hh(A , B , C , D , 1 , hs1);
272 hh(D , A , B , C , 9 , hs2);
273 hh(C , D , A , B , 5 , hs3);
274 hh(B , C , D , A , 13 , hs4);
275 hh(A , B , C , D , 3 , hs1);
276 hh(D , A , B , C , 11 , hs2);
277 hh(C , D , A , B , 7 , hs3);
278 hh(B , C , D , A , 15 , hs4);
285 /* MD4Update(MDp,X,count)
286 ** Input: X -- a pointer to an array of unsigned characters.
287 ** count -- the number of bits of X to use.
288 ** (if not a multiple of 8, uses high bits of last byte.)
289 ** Update MDp using the number of bits of X given by count.
290 ** This is the basic input routine for an MD4 user.
291 ** The routine completes the MD computation when count < 512, so
292 ** every MD computation should end with one call to MD4Update with a
293 ** count less than 512. A call with count 0 will be ignored if the
294 ** MD has already been terminated (done != 0), so an extra call with
295 ** count 0 can be given as a "courtesy close" to force termination
299 MD4Update(MD4_CTX *MDp, unsigned char *X, unsigned int count)
301 unsigned int i, tmp, bit, byte, mask;
302 unsigned char XX[64];
305 /* return with no error if this is a courtesy close with count
306 ** zero and MDp->done is true.
308 if (count == 0 && MDp->done) return;
309 /* check to see if MD is already done and report error */
311 { printf("\nError: MD4Update MD already done."); return; }
313 /* Add count to MDp->count */
324 { /* Full block of data to handle */
327 else if (count > 512) /* Check for count too large */
329 printf("\nError: MD4Update called with illegal count value %d.",
333 else /* partial block -- must be last block so finish up */
335 /* Find out how many bytes and residual bits there are */
338 /* Copy X into XX since we need to modify it */
340 for (i=0;i<=byte;i++) XX[i] = X[i];
341 for (i=byte+1;i<64;i++) XX[i] = 0;
342 /* Add padding '1' bit and low-order zeros in last byte */
343 mask = 1 << (7 - bit);
344 XX[byte] = (XX[byte] | mask) & ~( mask - 1);
345 /* If room for bit count, finish up with this block */
348 for (i=0;i<8;i++) XX[56+i] = MDp->count[i];
351 else /* need to do two blocks to finish up */
354 for (i=0;i<56;i++) XX[i] = 0;
355 for (i=0;i<8;i++) XX[56+i] = MDp->count[i];
358 /* Set flag saying we're done with MD computation */
364 ** Finish up MD4 computation and return message digest.
367 MD4Final(unsigned char *buf, MD4_CTX *MD)
372 MD4Update(MD, NULL, 0);
373 for (i = 0; i < 4; ++i) {
375 for (j = 0; j < 4; ++j) {
384 ****************************(cut)***********************************/
386 static int md4_init(PPP_MD_CTX *ctx)
389 MD4_CTX *mctx = calloc(1, sizeof(MD4_CTX));
399 static int md4_update(PPP_MD_CTX *ctx, const void *data, size_t len)
401 #if defined(__NetBSD__)
402 /* NetBSD uses the libc md4 routines which take bytes instead of bits */
408 /* Internal MD4Update can take at most 64 bytes at a time */
409 while (mdlen > 512) {
410 MD4Update((MD4_CTX*) ctx->priv, (unsigned char*) data, 512);
414 MD4Update((MD4_CTX*) ctx->priv, (unsigned char*) data, mdlen);
418 static int md4_final(PPP_MD_CTX *ctx, unsigned char *out, unsigned int *len)
420 MD4Final(out, (MD4_CTX*) ctx->priv);
424 static void md4_clean(PPP_MD_CTX *ctx)
434 static PPP_MD ppp_md4 = {
436 .update_fn = md4_update,
437 .final_fn = md4_final,
438 .clean_fn = md4_clean,
441 const PPP_MD *PPP_md4(void)