[tcp] Enable AF_INET6 transport for tcp connections
[gpxe.git] / src / crypto / md5.c
blob8c606398e1489c8bfab8d6526d021d7dd0a30392
1 /*
2 * Cryptographic API.
4 * MD5 Message Digest Algorithm (RFC1321).
6 * Derived from cryptoapi implementation, originally based on the
7 * public domain implementation written by Colin Plumb in 1993.
9 * Reduced object size by around 50% compared to the original Linux
10 * version for use in Etherboot by Michael Brown.
12 * Copyright (c) Cryptoapi developers.
13 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
14 * Copyright (c) 2006 Michael Brown <mbrown@fensystems.co.uk>
16 * This program is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License as published by the Free
18 * Software Foundation; either version 2 of the License, or (at your option)
19 * any later version.
23 FILE_LICENCE ( GPL2_OR_LATER );
25 #include <stdint.h>
26 #include <string.h>
27 #include <byteswap.h>
28 #include <gpxe/crypto.h>
29 #include <gpxe/md5.h>
31 struct md5_step {
32 u32 ( * f ) ( u32 b, u32 c, u32 d );
33 u8 coefficient;
34 u8 constant;
37 static u32 f1(u32 b, u32 c, u32 d)
39 return ( d ^ ( b & ( c ^ d ) ) );
42 static u32 f2(u32 b, u32 c, u32 d)
44 return ( c ^ ( d & ( b ^ c ) ) );
47 static u32 f3(u32 b, u32 c, u32 d)
49 return ( b ^ c ^ d );
52 static u32 f4(u32 b, u32 c, u32 d)
54 return ( c ^ ( b | ~d ) );
57 static struct md5_step md5_steps[4] = {
59 .f = f1,
60 .coefficient = 1,
61 .constant = 0,
64 .f = f2,
65 .coefficient = 5,
66 .constant = 1,
69 .f = f3,
70 .coefficient = 3,
71 .constant = 5,
74 .f = f4,
75 .coefficient = 7,
76 .constant = 0,
80 static const u8 r[64] = {
81 7,12,17,22,7,12,17,22,7,12,17,22,7,12,17,22,
82 5,9,14,20,5,9,14,20,5,9,14,20,5,9,14,20,
83 4,11,16,23,4,11,16,23,4,11,16,23,4,11,16,23,
84 6,10,15,21,6,10,15,21,6,10,15,21,6,10,15,21
87 static const u32 k[64] = {
88 0xd76aa478UL, 0xe8c7b756UL, 0x242070dbUL, 0xc1bdceeeUL,
89 0xf57c0fafUL, 0x4787c62aUL, 0xa8304613UL, 0xfd469501UL,
90 0x698098d8UL, 0x8b44f7afUL, 0xffff5bb1UL, 0x895cd7beUL,
91 0x6b901122UL, 0xfd987193UL, 0xa679438eUL, 0x49b40821UL,
92 0xf61e2562UL, 0xc040b340UL, 0x265e5a51UL, 0xe9b6c7aaUL,
93 0xd62f105dUL, 0x02441453UL, 0xd8a1e681UL, 0xe7d3fbc8UL,
94 0x21e1cde6UL, 0xc33707d6UL, 0xf4d50d87UL, 0x455a14edUL,
95 0xa9e3e905UL, 0xfcefa3f8UL, 0x676f02d9UL, 0x8d2a4c8aUL,
96 0xfffa3942UL, 0x8771f681UL, 0x6d9d6122UL, 0xfde5380cUL,
97 0xa4beea44UL, 0x4bdecfa9UL, 0xf6bb4b60UL, 0xbebfbc70UL,
98 0x289b7ec6UL, 0xeaa127faUL, 0xd4ef3085UL, 0x04881d05UL,
99 0xd9d4d039UL, 0xe6db99e5UL, 0x1fa27cf8UL, 0xc4ac5665UL,
100 0xf4292244UL, 0x432aff97UL, 0xab9423a7UL, 0xfc93a039UL,
101 0x655b59c3UL, 0x8f0ccc92UL, 0xffeff47dUL, 0x85845dd1UL,
102 0x6fa87e4fUL, 0xfe2ce6e0UL, 0xa3014314UL, 0x4e0811a1UL,
103 0xf7537e82UL, 0xbd3af235UL, 0x2ad7d2bbUL, 0xeb86d391UL,
106 static void md5_transform(u32 *hash, const u32 *in)
108 u32 a, b, c, d, f, g, temp;
109 int i;
110 struct md5_step *step;
112 a = hash[0];
113 b = hash[1];
114 c = hash[2];
115 d = hash[3];
117 for ( i = 0 ; i < 64 ; i++ ) {
118 step = &md5_steps[i >> 4];
119 f = step->f ( b, c, d );
120 g = ( ( i * step->coefficient + step->constant ) & 0xf );
121 temp = d;
122 d = c;
123 c = b;
124 a += ( f + k[i] + in[g] );
125 a = ( ( a << r[i] ) | ( a >> ( 32-r[i] ) ) );
126 b += a;
127 a = temp;
130 hash[0] += a;
131 hash[1] += b;
132 hash[2] += c;
133 hash[3] += d;
136 /* XXX: this stuff can be optimized */
137 static inline void le32_to_cpu_array(u32 *buf, unsigned int words)
139 while (words--) {
140 le32_to_cpus(buf);
141 buf++;
145 static inline void cpu_to_le32_array(u32 *buf, unsigned int words)
147 while (words--) {
148 cpu_to_le32s(buf);
149 buf++;
153 static inline void md5_transform_helper(struct md5_ctx *ctx)
155 le32_to_cpu_array(ctx->block, sizeof(ctx->block) / sizeof(u32));
156 md5_transform(ctx->hash, ctx->block);
159 static void md5_init(void *context)
161 struct md5_ctx *mctx = context;
163 mctx->hash[0] = 0x67452301;
164 mctx->hash[1] = 0xefcdab89;
165 mctx->hash[2] = 0x98badcfe;
166 mctx->hash[3] = 0x10325476;
167 mctx->byte_count = 0;
170 static void md5_update(void *context, const void *data, size_t len)
172 struct md5_ctx *mctx = context;
173 const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
175 mctx->byte_count += len;
177 if (avail > len) {
178 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
179 data, len);
180 return;
183 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
184 data, avail);
186 md5_transform_helper(mctx);
187 data += avail;
188 len -= avail;
190 while (len >= sizeof(mctx->block)) {
191 memcpy(mctx->block, data, sizeof(mctx->block));
192 md5_transform_helper(mctx);
193 data += sizeof(mctx->block);
194 len -= sizeof(mctx->block);
197 memcpy(mctx->block, data, len);
200 static void md5_final(void *context, void *out)
202 struct md5_ctx *mctx = context;
203 const unsigned int offset = mctx->byte_count & 0x3f;
204 char *p = (char *)mctx->block + offset;
205 int padding = 56 - (offset + 1);
207 *p++ = 0x80;
208 if (padding < 0) {
209 memset(p, 0x00, padding + sizeof (u64));
210 md5_transform_helper(mctx);
211 p = (char *)mctx->block;
212 padding = 56;
215 memset(p, 0, padding);
216 mctx->block[14] = mctx->byte_count << 3;
217 mctx->block[15] = mctx->byte_count >> 29;
218 le32_to_cpu_array(mctx->block, (sizeof(mctx->block) -
219 sizeof(u64)) / sizeof(u32));
220 md5_transform(mctx->hash, mctx->block);
221 cpu_to_le32_array(mctx->hash, sizeof(mctx->hash) / sizeof(u32));
222 memcpy(out, mctx->hash, sizeof(mctx->hash));
223 memset(mctx, 0, sizeof(*mctx));
226 struct digest_algorithm md5_algorithm = {
227 .name = "md5",
228 .ctxsize = MD5_CTX_SIZE,
229 .blocksize = ( MD5_BLOCK_WORDS * 4 ),
230 .digestsize = MD5_DIGEST_SIZE,
231 .init = md5_init,
232 .update = md5_update,
233 .final = md5_final,