Wed, 31 Dec 2014 07:16:47 +0100
Revert simplistic fix pending revisit of Mozilla integration attempt.
michael@0 | 1 | /* vim:set ts=2 sw=2 et cindent: */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | /* |
michael@0 | 7 | * "clean room" MD4 implementation (see RFC 1320) |
michael@0 | 8 | */ |
michael@0 | 9 | |
michael@0 | 10 | #include <string.h> |
michael@0 | 11 | #include "md4.h" |
michael@0 | 12 | |
michael@0 | 13 | typedef uint32_t Uint32; |
michael@0 | 14 | typedef uint8_t Uint8; |
michael@0 | 15 | |
michael@0 | 16 | /* the "conditional" function */ |
michael@0 | 17 | #define F(x,y,z) (((x) & (y)) | (~(x) & (z))) |
michael@0 | 18 | |
michael@0 | 19 | /* the "majority" function */ |
michael@0 | 20 | #define G(x,y,z) (((x) & (y)) | ((x) & (z)) | ((y) & (z))) |
michael@0 | 21 | |
michael@0 | 22 | /* the "parity" function */ |
michael@0 | 23 | #define H(x,y,z) ((x) ^ (y) ^ (z)) |
michael@0 | 24 | |
michael@0 | 25 | /* rotate n-bits to the left */ |
michael@0 | 26 | #define ROTL(x,n) (((x) << (n)) | ((x) >> (0x20 - n))) |
michael@0 | 27 | |
michael@0 | 28 | /* round 1: [abcd k s]: a = (a + F(b,c,d) + X[k]) <<< s */ |
michael@0 | 29 | #define RD1(a,b,c,d,k,s) a += F(b,c,d) + X[k]; a = ROTL(a,s) |
michael@0 | 30 | |
michael@0 | 31 | /* round 2: [abcd k s]: a = (a + G(b,c,d) + X[k] + MAGIC) <<< s */ |
michael@0 | 32 | #define RD2(a,b,c,d,k,s) a += G(b,c,d) + X[k] + 0x5A827999; a = ROTL(a,s) |
michael@0 | 33 | |
michael@0 | 34 | /* round 3: [abcd k s]: a = (a + H(b,c,d) + X[k] + MAGIC) <<< s */ |
michael@0 | 35 | #define RD3(a,b,c,d,k,s) a += H(b,c,d) + X[k] + 0x6ED9EBA1; a = ROTL(a,s) |
michael@0 | 36 | |
michael@0 | 37 | /* converts from word array to byte array, len is number of bytes */ |
michael@0 | 38 | static void w2b(Uint8 *out, const Uint32 *in, Uint32 len) |
michael@0 | 39 | { |
michael@0 | 40 | Uint8 *bp; const Uint32 *wp, *wpend; |
michael@0 | 41 | |
michael@0 | 42 | bp = out; |
michael@0 | 43 | wp = in; |
michael@0 | 44 | wpend = wp + (len >> 2); |
michael@0 | 45 | |
michael@0 | 46 | for (; wp != wpend; ++wp, bp += 4) |
michael@0 | 47 | { |
michael@0 | 48 | bp[0] = (Uint8) ((*wp ) & 0xFF); |
michael@0 | 49 | bp[1] = (Uint8) ((*wp >> 8) & 0xFF); |
michael@0 | 50 | bp[2] = (Uint8) ((*wp >> 16) & 0xFF); |
michael@0 | 51 | bp[3] = (Uint8) ((*wp >> 24) & 0xFF); |
michael@0 | 52 | } |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | /* converts from byte array to word array, len is number of bytes */ |
michael@0 | 56 | static void b2w(Uint32 *out, const Uint8 *in, Uint32 len) |
michael@0 | 57 | { |
michael@0 | 58 | Uint32 *wp; const Uint8 *bp, *bpend; |
michael@0 | 59 | |
michael@0 | 60 | wp = out; |
michael@0 | 61 | bp = in; |
michael@0 | 62 | bpend = in + len; |
michael@0 | 63 | |
michael@0 | 64 | for (; bp != bpend; bp += 4, ++wp) |
michael@0 | 65 | { |
michael@0 | 66 | *wp = (Uint32) (bp[0] ) | |
michael@0 | 67 | (Uint32) (bp[1] << 8) | |
michael@0 | 68 | (Uint32) (bp[2] << 16) | |
michael@0 | 69 | (Uint32) (bp[3] << 24); |
michael@0 | 70 | } |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | /* update state: data is 64 bytes in length */ |
michael@0 | 74 | static void md4step(Uint32 state[4], const Uint8 *data) |
michael@0 | 75 | { |
michael@0 | 76 | Uint32 A, B, C, D, X[16]; |
michael@0 | 77 | |
michael@0 | 78 | b2w(X, data, 64); |
michael@0 | 79 | |
michael@0 | 80 | A = state[0]; |
michael@0 | 81 | B = state[1]; |
michael@0 | 82 | C = state[2]; |
michael@0 | 83 | D = state[3]; |
michael@0 | 84 | |
michael@0 | 85 | 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); |
michael@0 | 86 | 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); |
michael@0 | 87 | 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); |
michael@0 | 88 | 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); |
michael@0 | 89 | |
michael@0 | 90 | 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); |
michael@0 | 91 | 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); |
michael@0 | 92 | 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); |
michael@0 | 93 | 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); |
michael@0 | 94 | |
michael@0 | 95 | 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); |
michael@0 | 96 | 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); |
michael@0 | 97 | 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); |
michael@0 | 98 | 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); |
michael@0 | 99 | |
michael@0 | 100 | state[0] += A; |
michael@0 | 101 | state[1] += B; |
michael@0 | 102 | state[2] += C; |
michael@0 | 103 | state[3] += D; |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | void md4sum(const Uint8 *input, Uint32 inputLen, Uint8 *result) |
michael@0 | 107 | { |
michael@0 | 108 | Uint8 final[128]; |
michael@0 | 109 | Uint32 i, n, m, state[4]; |
michael@0 | 110 | |
michael@0 | 111 | /* magic initial states */ |
michael@0 | 112 | state[0] = 0x67452301; |
michael@0 | 113 | state[1] = 0xEFCDAB89; |
michael@0 | 114 | state[2] = 0x98BADCFE; |
michael@0 | 115 | state[3] = 0x10325476; |
michael@0 | 116 | |
michael@0 | 117 | /* compute number of complete 64-byte segments contained in input */ |
michael@0 | 118 | m = inputLen >> 6; |
michael@0 | 119 | |
michael@0 | 120 | /* digest first m segments */ |
michael@0 | 121 | for (i=0; i<m; ++i) |
michael@0 | 122 | md4step(state, (input + (i << 6))); |
michael@0 | 123 | |
michael@0 | 124 | /* build final buffer */ |
michael@0 | 125 | n = inputLen % 64; |
michael@0 | 126 | memcpy(final, input + (m << 6), n); |
michael@0 | 127 | final[n] = 0x80; |
michael@0 | 128 | memset(final + n + 1, 0, 120 - (n + 1)); |
michael@0 | 129 | |
michael@0 | 130 | inputLen = inputLen << 3; |
michael@0 | 131 | w2b(final + (n >= 56 ? 120 : 56), &inputLen, 4); |
michael@0 | 132 | |
michael@0 | 133 | md4step(state, final); |
michael@0 | 134 | if (n >= 56) |
michael@0 | 135 | md4step(state, final + 64); |
michael@0 | 136 | |
michael@0 | 137 | /* copy state to result */ |
michael@0 | 138 | w2b(result, state, 16); |
michael@0 | 139 | } |