md5.c (96916090f488986a4ebb8e9ffa6a3b50881d5ccd) md5.c (14b75ba70da925a9f040a7575cb46ad7d394b117)
1/*
2 * Cryptographic API.
3 *
4 * MD5 Message Digest Algorithm (RFC1321).
5 *
6 * Derived from cryptoapi implementation, originally based on the
7 * public domain implementation written by Colin Plumb in 1993.
8 *
9 * Copyright (c) Cryptoapi developers.
10 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 2 of the License, or (at your option)
15 * any later version.
16 *
17 */
1/*
2 * Cryptographic API.
3 *
4 * MD5 Message Digest Algorithm (RFC1321).
5 *
6 * Derived from cryptoapi implementation, originally based on the
7 * public domain implementation written by Colin Plumb in 1993.
8 *
9 * Copyright (c) Cryptoapi developers.
10 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 2 of the License, or (at your option)
15 * any later version.
16 *
17 */
18#include <crypto/internal/hash.h>
18#include <linux/init.h>
19#include <linux/module.h>
20#include <linux/string.h>
19#include <linux/init.h>
20#include <linux/module.h>
21#include <linux/string.h>
21#include <linux/crypto.h>
22#include <linux/types.h>
23#include <asm/byteorder.h>
24
25#define MD5_DIGEST_SIZE 16
26#define MD5_HMAC_BLOCK_SIZE 64
27#define MD5_BLOCK_WORDS 16
28#define MD5_HASH_WORDS 4
29

--- 112 unchanged lines hidden (view full) ---

142}
143
144static inline void md5_transform_helper(struct md5_ctx *ctx)
145{
146 le32_to_cpu_array(ctx->block, sizeof(ctx->block) / sizeof(u32));
147 md5_transform(ctx->hash, ctx->block);
148}
149
22#include <linux/types.h>
23#include <asm/byteorder.h>
24
25#define MD5_DIGEST_SIZE 16
26#define MD5_HMAC_BLOCK_SIZE 64
27#define MD5_BLOCK_WORDS 16
28#define MD5_HASH_WORDS 4
29

--- 112 unchanged lines hidden (view full) ---

142}
143
144static inline void md5_transform_helper(struct md5_ctx *ctx)
145{
146 le32_to_cpu_array(ctx->block, sizeof(ctx->block) / sizeof(u32));
147 md5_transform(ctx->hash, ctx->block);
148}
149
150static void md5_init(struct crypto_tfm *tfm)
150static int md5_init(struct shash_desc *desc)
151{
151{
152 struct md5_ctx *mctx = crypto_tfm_ctx(tfm);
152 struct md5_ctx *mctx = shash_desc_ctx(desc);
153
154 mctx->hash[0] = 0x67452301;
155 mctx->hash[1] = 0xefcdab89;
156 mctx->hash[2] = 0x98badcfe;
157 mctx->hash[3] = 0x10325476;
158 mctx->byte_count = 0;
153
154 mctx->hash[0] = 0x67452301;
155 mctx->hash[1] = 0xefcdab89;
156 mctx->hash[2] = 0x98badcfe;
157 mctx->hash[3] = 0x10325476;
158 mctx->byte_count = 0;
159
160 return 0;
159}
160
161}
162
161static void md5_update(struct crypto_tfm *tfm, const u8 *data, unsigned int len)
163static int md5_update(struct shash_desc *desc, const u8 *data, unsigned int len)
162{
164{
163 struct md5_ctx *mctx = crypto_tfm_ctx(tfm);
165 struct md5_ctx *mctx = shash_desc_ctx(desc);
164 const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
165
166 mctx->byte_count += len;
167
168 if (avail > len) {
169 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
170 data, len);
166 const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
167
168 mctx->byte_count += len;
169
170 if (avail > len) {
171 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
172 data, len);
171 return;
173 return 0;
172 }
173
174 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
175 data, avail);
176
177 md5_transform_helper(mctx);
178 data += avail;
179 len -= avail;
180
181 while (len >= sizeof(mctx->block)) {
182 memcpy(mctx->block, data, sizeof(mctx->block));
183 md5_transform_helper(mctx);
184 data += sizeof(mctx->block);
185 len -= sizeof(mctx->block);
186 }
187
188 memcpy(mctx->block, data, len);
174 }
175
176 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
177 data, avail);
178
179 md5_transform_helper(mctx);
180 data += avail;
181 len -= avail;
182
183 while (len >= sizeof(mctx->block)) {
184 memcpy(mctx->block, data, sizeof(mctx->block));
185 md5_transform_helper(mctx);
186 data += sizeof(mctx->block);
187 len -= sizeof(mctx->block);
188 }
189
190 memcpy(mctx->block, data, len);
191
192 return 0;
189}
190
193}
194
191static void md5_final(struct crypto_tfm *tfm, u8 *out)
195static int md5_final(struct shash_desc *desc, u8 *out)
192{
196{
193 struct md5_ctx *mctx = crypto_tfm_ctx(tfm);
197 struct md5_ctx *mctx = shash_desc_ctx(desc);
194 const unsigned int offset = mctx->byte_count & 0x3f;
195 char *p = (char *)mctx->block + offset;
196 int padding = 56 - (offset + 1);
197
198 *p++ = 0x80;
199 if (padding < 0) {
200 memset(p, 0x00, padding + sizeof (u64));
201 md5_transform_helper(mctx);

--- 5 unchanged lines hidden (view full) ---

207 mctx->block[14] = mctx->byte_count << 3;
208 mctx->block[15] = mctx->byte_count >> 29;
209 le32_to_cpu_array(mctx->block, (sizeof(mctx->block) -
210 sizeof(u64)) / sizeof(u32));
211 md5_transform(mctx->hash, mctx->block);
212 cpu_to_le32_array(mctx->hash, sizeof(mctx->hash) / sizeof(u32));
213 memcpy(out, mctx->hash, sizeof(mctx->hash));
214 memset(mctx, 0, sizeof(*mctx));
198 const unsigned int offset = mctx->byte_count & 0x3f;
199 char *p = (char *)mctx->block + offset;
200 int padding = 56 - (offset + 1);
201
202 *p++ = 0x80;
203 if (padding < 0) {
204 memset(p, 0x00, padding + sizeof (u64));
205 md5_transform_helper(mctx);

--- 5 unchanged lines hidden (view full) ---

211 mctx->block[14] = mctx->byte_count << 3;
212 mctx->block[15] = mctx->byte_count >> 29;
213 le32_to_cpu_array(mctx->block, (sizeof(mctx->block) -
214 sizeof(u64)) / sizeof(u32));
215 md5_transform(mctx->hash, mctx->block);
216 cpu_to_le32_array(mctx->hash, sizeof(mctx->hash) / sizeof(u32));
217 memcpy(out, mctx->hash, sizeof(mctx->hash));
218 memset(mctx, 0, sizeof(*mctx));
219
220 return 0;
215}
216
221}
222
217static struct crypto_alg alg = {
218 .cra_name = "md5",
219 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
220 .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
221 .cra_ctxsize = sizeof(struct md5_ctx),
222 .cra_module = THIS_MODULE,
223 .cra_list = LIST_HEAD_INIT(alg.cra_list),
224 .cra_u = { .digest = {
225 .dia_digestsize = MD5_DIGEST_SIZE,
226 .dia_init = md5_init,
227 .dia_update = md5_update,
228 .dia_final = md5_final } }
223static struct shash_alg alg = {
224 .digestsize = MD5_DIGEST_SIZE,
225 .init = md5_init,
226 .update = md5_update,
227 .final = md5_final,
228 .descsize = sizeof(struct md5_ctx),
229 .base = {
230 .cra_name = "md5",
231 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
232 .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
233 .cra_module = THIS_MODULE,
234 }
229};
230
231static int __init md5_mod_init(void)
232{
235};
236
237static int __init md5_mod_init(void)
238{
233 return crypto_register_alg(&alg);
239 return crypto_register_shash(&alg);
234}
235
236static void __exit md5_mod_fini(void)
237{
240}
241
242static void __exit md5_mod_fini(void)
243{
238 crypto_unregister_alg(&alg);
244 crypto_unregister_shash(&alg);
239}
240
241module_init(md5_mod_init);
242module_exit(md5_mod_fini);
243
244MODULE_LICENSE("GPL");
245MODULE_DESCRIPTION("MD5 Message Digest Algorithm");
245}
246
247module_init(md5_mod_init);
248module_exit(md5_mod_fini);
249
250MODULE_LICENSE("GPL");
251MODULE_DESCRIPTION("MD5 Message Digest Algorithm");