1 /****************************************************************************
3 * Nintendo Wii/GameCube SMB implementation
6 ****************************************************************************/
11 /* Structure to save state of computation between the single steps. */
24 # define MD4_DIGEST_SIZE 16
27 (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
29 #define BLOCKSIZE 4096
30 #if BLOCKSIZE % 64 != 0
31 # error "invalid BLOCKSIZE"
34 /* MD4 round constants */
38 /* Round functions. */
39 #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
40 #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
41 #define H(x, y, z) ((x) ^ (y) ^ (z))
42 #define rol(x, n) (((x) << (n)) | ((uint32_t) (x) >> (32 - (n))))
43 #define R1(a,b,c,d,k,s) a=rol(a+F(b,c,d)+x[k],s);
44 #define R2(a,b,c,d,k,s) a=rol(a+G(b,c,d)+x[k]+K1,s);
45 #define R3(a,b,c,d,k,s) a=rol(a+H(b,c,d)+x[k]+K2,s);
47 /* This array contains the bytes used to pad the buffer to the next
48 64-byte boundary. (RFC 1320, 3.1: Step 1) */
49 static const unsigned char fillbuf
[64] =
50 { 0x80, 0 /* , 0, 0, ... */};
52 /* Initialize structure containing state of computation.
53 (RFC 1320, 3.3: Step 3) */
54 static void md4_init_ctx(struct md4_ctx
*ctx
)
61 ctx
->total
[0] = ctx
->total
[1] = 0;
65 /* Copy the 4 byte value from v into the memory location pointed to by *cp,
66 If your architecture allows unaligned access this is equivalent to
67 * (uint32_t *) cp = v */
68 static inline void set_uint32(char *cp
, uint32_t v
)
70 memcpy(cp
, &v
, sizeof v
);
73 /* Put result from CTX in first 16 bytes following RESBUF. The result
74 must be in little endian byte order. */
76 md4_read_ctx(const struct md4_ctx
*ctx
, void *resbuf
)
79 set_uint32(r
+ 0 * sizeof ctx
->A
, SWAP (ctx
->A
));
80 set_uint32(r
+ 1 * sizeof ctx
->B
, SWAP (ctx
->B
));
81 set_uint32(r
+ 2 * sizeof ctx
->C
, SWAP (ctx
->C
));
82 set_uint32(r
+ 3 * sizeof ctx
->D
, SWAP (ctx
->D
));
87 /* Process LEN bytes of BUFFER, accumulating context into CTX.
88 It is assumed that LEN % 64 == 0. */
90 static void md4_process_block(const void *buffer
, size_t len
,
93 const uint32_t *words
= buffer
;
94 size_t nwords
= len
/ sizeof(uint32_t);
95 const uint32_t *endp
= words
+ nwords
;
102 /* First increment the byte count. RFC 1320 specifies the possible
103 length of the file up to 2^64 bits. Here we only compute the
104 number of bytes. Do a double word increment. */
105 ctx
->total
[0] += len
;
106 if (ctx
->total
[0] < len
)
109 /* Process all bytes in the buffer with 64 bytes in each round of
114 for (t
= 0; t
< 16; t
++)
116 x
[t
] = SWAP (*words
);
121 R1 (A
, B
, C
, D
, 0, 3);
122 R1 (D
, A
, B
, C
, 1, 7);
123 R1 (C
, D
, A
, B
, 2, 11);
124 R1 (B
, C
, D
, A
, 3, 19);
125 R1 (A
, B
, C
, D
, 4, 3);
126 R1 (D
, A
, B
, C
, 5, 7);
127 R1 (C
, D
, A
, B
, 6, 11);
128 R1 (B
, C
, D
, A
, 7, 19);
129 R1 (A
, B
, C
, D
, 8, 3);
130 R1 (D
, A
, B
, C
, 9, 7);
131 R1 (C
, D
, A
, B
, 10, 11);
132 R1 (B
, C
, D
, A
, 11, 19);
133 R1 (A
, B
, C
, D
, 12, 3);
134 R1 (D
, A
, B
, C
, 13, 7);
135 R1 (C
, D
, A
, B
, 14, 11);
136 R1 (B
, C
, D
, A
, 15, 19);
139 R2 (A
, B
, C
, D
, 0, 3);
140 R2 (D
, A
, B
, C
, 4, 5);
141 R2 (C
, D
, A
, B
, 8, 9);
142 R2 (B
, C
, D
, A
, 12, 13);
143 R2 (A
, B
, C
, D
, 1, 3);
144 R2 (D
, A
, B
, C
, 5, 5);
145 R2 (C
, D
, A
, B
, 9, 9);
146 R2 (B
, C
, D
, A
, 13, 13);
147 R2 (A
, B
, C
, D
, 2, 3);
148 R2 (D
, A
, B
, C
, 6, 5);
149 R2 (C
, D
, A
, B
, 10, 9);
150 R2 (B
, C
, D
, A
, 14, 13);
151 R2 (A
, B
, C
, D
, 3, 3);
152 R2 (D
, A
, B
, C
, 7, 5);
153 R2 (C
, D
, A
, B
, 11, 9);
154 R2 (B
, C
, D
, A
, 15, 13);
157 R3 (A
, B
, C
, D
, 0, 3);
158 R3 (D
, A
, B
, C
, 8, 9);
159 R3 (C
, D
, A
, B
, 4, 11);
160 R3 (B
, C
, D
, A
, 12, 15);
161 R3 (A
, B
, C
, D
, 2, 3);
162 R3 (D
, A
, B
, C
, 10, 9);
163 R3 (C
, D
, A
, B
, 6, 11);
164 R3 (B
, C
, D
, A
, 14, 15);
165 R3 (A
, B
, C
, D
, 1, 3);
166 R3 (D
, A
, B
, C
, 9, 9);
167 R3 (C
, D
, A
, B
, 5, 11);
168 R3 (B
, C
, D
, A
, 13, 15);
169 R3 (A
, B
, C
, D
, 3, 3);
170 R3 (D
, A
, B
, C
, 11, 9);
171 R3 (C
, D
, A
, B
, 7, 11);
172 R3 (B
, C
, D
, A
, 15, 15);
181 /* Process the remaining bytes in the internal buffer and the usual
182 prolog according to the standard and write the result to RESBUF. */
184 md4_finish_ctx(struct md4_ctx
*ctx
, void *resbuf
)
186 /* Take yet unprocessed bytes into account. */
187 uint32_t bytes
= ctx
->buflen
;
190 /* Now count remaining bytes. */
191 ctx
->total
[0] += bytes
;
192 if (ctx
->total
[0] < bytes
)
195 pad
= bytes
>= 56 ? 64 + 56 - bytes
: 56 - bytes
;
196 memcpy(&((char*) ctx
->buffer
)[bytes
], fillbuf
, pad
);
198 /* Put the 64-bit file length in *bits* at the end of the buffer. */
199 ctx
->buffer
[(bytes
+ pad
) / 4] = SWAP (ctx
->total
[0] << 3);
200 ctx
->buffer
[(bytes
+ pad
) / 4 + 1] = SWAP ((ctx
->total
[1] << 3) |
201 (ctx
->total
[0] >> 29));
203 /* Process last bytes. */
204 md4_process_block(ctx
->buffer
, bytes
+ pad
+ 8, ctx
);
206 return md4_read_ctx(ctx
, resbuf
);
209 static void md4_process_bytes(const void *buffer
, size_t len
,
212 /* When we already have some bits in our internal buffer concatenate
213 both inputs first. */
214 if (ctx
->buflen
!= 0)
216 size_t left_over
= ctx
->buflen
;
217 size_t add
= 128 - left_over
> len
? len
: 128 - left_over
;
219 memcpy(&((char*) ctx
->buffer
)[left_over
], buffer
, add
);
222 if (ctx
->buflen
> 64)
224 md4_process_block(ctx
->buffer
, ctx
->buflen
& ~63, ctx
);
227 /* The regions in the following copy operation cannot overlap. */
229 &((char*) ctx
->buffer
)[(left_over
+ add
) & ~63],
233 buffer
= (const char *) buffer
+ add
;
237 /* Process available complete blocks. */
240 #if !_STRING_ARCH_unaligned
241 /* To check alignment gcc has an appropriate operator. Other
244 # define UNALIGNED_P(p) (((uintptr_t) p) % __alignof__ (uint32_t) != 0)
246 # define alignof(type) offsetof (struct { char c; type x; }, x)
247 # define UNALIGNED_P(p) (((size_t) p) % alignof (uint32_t) != 0)
250 UNALIGNED_P (buffer
))
253 md4_process_block (memcpy (ctx
->buffer
, buffer
, 64), 64, ctx
);
254 buffer
= (const char *) buffer
+ 64;
260 md4_process_block (buffer
, len
& ~63, ctx
);
261 buffer
= (const char *) buffer
+ (len
& ~63);
266 /* Move remaining bytes in internal buffer. */
269 size_t left_over
= ctx
->buflen
;
271 memcpy(&((char*) ctx
->buffer
)[left_over
], buffer
, len
);
275 md4_process_block(ctx
->buffer
, 64, ctx
);
277 memcpy(ctx
->buffer
, &ctx
->buffer
[16], left_over
);
279 ctx
->buflen
= left_over
;
283 /* Compute MD4 message digest for LEN bytes beginning at BUFFER. The
284 result is always in little endian byte order, so that a byte-wise
285 output yields to the wanted ASCII representation of the message
288 md4_buffer(const char *buffer
, size_t len
, void *resblock
)
292 /* Initialize the computation context. */
295 /* Process whole buffer but last len % 64 bytes. */
296 md4_process_bytes(buffer
, len
, &ctx
);
298 /* Put result in desired memory area. */
299 return md4_finish_ctx(&ctx
, resblock
);