1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/manager/ssl/src/md4.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,139 @@ 1.4 +/* vim:set ts=2 sw=2 et cindent: */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* 1.10 + * "clean room" MD4 implementation (see RFC 1320) 1.11 + */ 1.12 + 1.13 +#include <string.h> 1.14 +#include "md4.h" 1.15 + 1.16 +typedef uint32_t Uint32; 1.17 +typedef uint8_t Uint8; 1.18 + 1.19 +/* the "conditional" function */ 1.20 +#define F(x,y,z) (((x) & (y)) | (~(x) & (z))) 1.21 + 1.22 +/* the "majority" function */ 1.23 +#define G(x,y,z) (((x) & (y)) | ((x) & (z)) | ((y) & (z))) 1.24 + 1.25 +/* the "parity" function */ 1.26 +#define H(x,y,z) ((x) ^ (y) ^ (z)) 1.27 + 1.28 +/* rotate n-bits to the left */ 1.29 +#define ROTL(x,n) (((x) << (n)) | ((x) >> (0x20 - n))) 1.30 + 1.31 +/* round 1: [abcd k s]: a = (a + F(b,c,d) + X[k]) <<< s */ 1.32 +#define RD1(a,b,c,d,k,s) a += F(b,c,d) + X[k]; a = ROTL(a,s) 1.33 + 1.34 +/* round 2: [abcd k s]: a = (a + G(b,c,d) + X[k] + MAGIC) <<< s */ 1.35 +#define RD2(a,b,c,d,k,s) a += G(b,c,d) + X[k] + 0x5A827999; a = ROTL(a,s) 1.36 + 1.37 +/* round 3: [abcd k s]: a = (a + H(b,c,d) + X[k] + MAGIC) <<< s */ 1.38 +#define RD3(a,b,c,d,k,s) a += H(b,c,d) + X[k] + 0x6ED9EBA1; a = ROTL(a,s) 1.39 + 1.40 +/* converts from word array to byte array, len is number of bytes */ 1.41 +static void w2b(Uint8 *out, const Uint32 *in, Uint32 len) 1.42 +{ 1.43 + Uint8 *bp; const Uint32 *wp, *wpend; 1.44 + 1.45 + bp = out; 1.46 + wp = in; 1.47 + wpend = wp + (len >> 2); 1.48 + 1.49 + for (; wp != wpend; ++wp, bp += 4) 1.50 + { 1.51 + bp[0] = (Uint8) ((*wp ) & 0xFF); 1.52 + bp[1] = (Uint8) ((*wp >> 8) & 0xFF); 1.53 + bp[2] = (Uint8) ((*wp >> 16) & 0xFF); 1.54 + bp[3] = (Uint8) ((*wp >> 24) & 0xFF); 1.55 + } 1.56 +} 1.57 + 1.58 +/* converts from byte array to word array, len is number of bytes */ 1.59 +static void b2w(Uint32 *out, const Uint8 *in, Uint32 len) 1.60 +{ 1.61 + Uint32 *wp; const Uint8 *bp, *bpend; 1.62 + 1.63 + wp = out; 1.64 + bp = in; 1.65 + bpend = in + len; 1.66 + 1.67 + for (; bp != bpend; bp += 4, ++wp) 1.68 + { 1.69 + *wp = (Uint32) (bp[0] ) | 1.70 + (Uint32) (bp[1] << 8) | 1.71 + (Uint32) (bp[2] << 16) | 1.72 + (Uint32) (bp[3] << 24); 1.73 + } 1.74 +} 1.75 + 1.76 +/* update state: data is 64 bytes in length */ 1.77 +static void md4step(Uint32 state[4], const Uint8 *data) 1.78 +{ 1.79 + Uint32 A, B, C, D, X[16]; 1.80 + 1.81 + b2w(X, data, 64); 1.82 + 1.83 + A = state[0]; 1.84 + B = state[1]; 1.85 + C = state[2]; 1.86 + D = state[3]; 1.87 + 1.88 + RD1(A,B,C,D, 0,3); RD1(D,A,B,C, 1,7); RD1(C,D,A,B, 2,11); RD1(B,C,D,A, 3,19); 1.89 + RD1(A,B,C,D, 4,3); RD1(D,A,B,C, 5,7); RD1(C,D,A,B, 6,11); RD1(B,C,D,A, 7,19); 1.90 + RD1(A,B,C,D, 8,3); RD1(D,A,B,C, 9,7); RD1(C,D,A,B,10,11); RD1(B,C,D,A,11,19); 1.91 + RD1(A,B,C,D,12,3); RD1(D,A,B,C,13,7); RD1(C,D,A,B,14,11); RD1(B,C,D,A,15,19); 1.92 + 1.93 + RD2(A,B,C,D, 0,3); RD2(D,A,B,C, 4,5); RD2(C,D,A,B, 8, 9); RD2(B,C,D,A,12,13); 1.94 + RD2(A,B,C,D, 1,3); RD2(D,A,B,C, 5,5); RD2(C,D,A,B, 9, 9); RD2(B,C,D,A,13,13); 1.95 + RD2(A,B,C,D, 2,3); RD2(D,A,B,C, 6,5); RD2(C,D,A,B,10, 9); RD2(B,C,D,A,14,13); 1.96 + RD2(A,B,C,D, 3,3); RD2(D,A,B,C, 7,5); RD2(C,D,A,B,11, 9); RD2(B,C,D,A,15,13); 1.97 + 1.98 + RD3(A,B,C,D, 0,3); RD3(D,A,B,C, 8,9); RD3(C,D,A,B, 4,11); RD3(B,C,D,A,12,15); 1.99 + RD3(A,B,C,D, 2,3); RD3(D,A,B,C,10,9); RD3(C,D,A,B, 6,11); RD3(B,C,D,A,14,15); 1.100 + RD3(A,B,C,D, 1,3); RD3(D,A,B,C, 9,9); RD3(C,D,A,B, 5,11); RD3(B,C,D,A,13,15); 1.101 + RD3(A,B,C,D, 3,3); RD3(D,A,B,C,11,9); RD3(C,D,A,B, 7,11); RD3(B,C,D,A,15,15); 1.102 + 1.103 + state[0] += A; 1.104 + state[1] += B; 1.105 + state[2] += C; 1.106 + state[3] += D; 1.107 +} 1.108 + 1.109 +void md4sum(const Uint8 *input, Uint32 inputLen, Uint8 *result) 1.110 +{ 1.111 + Uint8 final[128]; 1.112 + Uint32 i, n, m, state[4]; 1.113 + 1.114 + /* magic initial states */ 1.115 + state[0] = 0x67452301; 1.116 + state[1] = 0xEFCDAB89; 1.117 + state[2] = 0x98BADCFE; 1.118 + state[3] = 0x10325476; 1.119 + 1.120 + /* compute number of complete 64-byte segments contained in input */ 1.121 + m = inputLen >> 6; 1.122 + 1.123 + /* digest first m segments */ 1.124 + for (i=0; i<m; ++i) 1.125 + md4step(state, (input + (i << 6))); 1.126 + 1.127 + /* build final buffer */ 1.128 + n = inputLen % 64; 1.129 + memcpy(final, input + (m << 6), n); 1.130 + final[n] = 0x80; 1.131 + memset(final + n + 1, 0, 120 - (n + 1)); 1.132 + 1.133 + inputLen = inputLen << 3; 1.134 + w2b(final + (n >= 56 ? 120 : 56), &inputLen, 4); 1.135 + 1.136 + md4step(state, final); 1.137 + if (n >= 56) 1.138 + md4step(state, final + 64); 1.139 + 1.140 + /* copy state to result */ 1.141 + w2b(result, state, 16); 1.142 +}