OLD | NEW |
| (Empty) |
1 // Copyright 2007-2009 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 // | |
16 // Optimized for minimal code size. | |
17 | |
18 #include "md5.h" | |
19 | |
20 #include <stdio.h> | |
21 #include <string.h> | |
22 #include <inttypes.h> | |
23 | |
24 #define rol(bits, value) (((value) << (bits)) | ((value) >> (32 - (bits)))) | |
25 | |
26 static const char Kr[64] = | |
27 { | |
28 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, | |
29 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, | |
30 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, | |
31 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21 | |
32 }; | |
33 | |
34 static const int KK[64] = | |
35 { | |
36 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, | |
37 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, | |
38 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, | |
39 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, | |
40 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, | |
41 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, | |
42 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, | |
43 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a, | |
44 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, | |
45 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, | |
46 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, | |
47 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, | |
48 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, | |
49 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1, | |
50 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, | |
51 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391 | |
52 }; | |
53 | |
54 static void MD5_Transform(MD5_CTX* ctx) { | |
55 uint32_t W[64]; | |
56 uint32_t A, B, C, D; | |
57 uint8_t* p = ctx->buf; | |
58 int t; | |
59 | |
60 for(t = 0; t < 16; ++t) { | |
61 uint32_t tmp = *p++; | |
62 tmp |= *p++ << 8; | |
63 tmp |= *p++ << 16; | |
64 tmp |= *p++ << 24; | |
65 W[t] = tmp; | |
66 } | |
67 | |
68 A = ctx->state[0]; | |
69 B = ctx->state[1]; | |
70 C = ctx->state[2]; | |
71 D = ctx->state[3]; | |
72 | |
73 for(t = 0; t < 64; t++) { | |
74 uint32_t f, tmp; | |
75 int g; | |
76 | |
77 if (t < 16) { | |
78 f = (D^(B&(C^D))); | |
79 g = t; | |
80 } else if ( t < 32) { | |
81 f = (C^(D&(B^C))); | |
82 g = (5*t + 1) & 15; | |
83 } else if ( t < 48) { | |
84 f = (B^C^D); | |
85 g = (3*t + 5) & 15; | |
86 } else { | |
87 f = (C^(B|(~D))); | |
88 g = (7*t) & 15; | |
89 } | |
90 | |
91 tmp = D; | |
92 D = C; | |
93 C = B; | |
94 B = B + rol(Kr[t], (A+f+KK[t]+W[g])); | |
95 A = tmp; | |
96 } | |
97 | |
98 ctx->state[0] += A; | |
99 ctx->state[1] += B; | |
100 ctx->state[2] += C; | |
101 ctx->state[3] += D; | |
102 } | |
103 | |
104 static const HASH_VTAB MD5_VTAB = { | |
105 MD5_init, | |
106 MD5_update, | |
107 MD5_final, | |
108 MD5, | |
109 MD5_DIGEST_SIZE | |
110 }; | |
111 | |
112 void MD5_init(MD5_CTX* ctx) { | |
113 ctx->f = &MD5_VTAB; | |
114 ctx->state[0] = 0x67452301; | |
115 ctx->state[1] = 0xEFCDAB89; | |
116 ctx->state[2] = 0x98BADCFE; | |
117 ctx->state[3] = 0x10325476; | |
118 ctx->count = 0; | |
119 } | |
120 | |
121 | |
122 void MD5_update(MD5_CTX* ctx, const void* data, int len) { | |
123 int i = ctx->count & 63; | |
124 const uint8_t* p = (const uint8_t*)data; | |
125 | |
126 ctx->count += len; | |
127 | |
128 while (len--) { | |
129 ctx->buf[i++] = *p++; | |
130 if (i == 64) { | |
131 MD5_Transform(ctx); | |
132 i = 0; | |
133 } | |
134 } | |
135 } | |
136 | |
137 | |
138 const uint8_t* MD5_final(MD5_CTX* ctx) { | |
139 uint8_t* p = ctx->buf; | |
140 uint64_t cnt = ctx->count * 8; | |
141 int i; | |
142 | |
143 MD5_update(ctx, (uint8_t*)"\x80", 1); | |
144 while ((ctx->count & 63) != 56) { | |
145 MD5_update(ctx, (uint8_t*)"\0", 1); | |
146 } | |
147 for (i = 0; i < 8; ++i) { | |
148 uint8_t tmp = cnt >> (i * 8); | |
149 MD5_update(ctx, &tmp, 1); | |
150 } | |
151 | |
152 for (i = 0; i < 4; i++) { | |
153 uint32_t tmp = ctx->state[i]; | |
154 *p++ = tmp; | |
155 *p++ = tmp >> 8; | |
156 *p++ = tmp >> 16; | |
157 *p++ = tmp >> 24; | |
158 } | |
159 | |
160 return ctx->buf; | |
161 } | |
162 | |
163 | |
164 /* Convenience function */ | |
165 const uint8_t* MD5(const void* data, int len, uint8_t* digest) { | |
166 MD5_CTX ctx; | |
167 MD5_init(&ctx); | |
168 MD5_update(&ctx, data, len); | |
169 memcpy(digest, MD5_final(&ctx), MD5_DIGEST_SIZE); | |
170 return digest; | |
171 } | |
OLD | NEW |