md5.c (5a2dd72abdae75ea2960145e0549635ce4e0be96) | md5.c (7d6f75eb21b84cdc5dfb09789974f02b42a89058) |
---|---|
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> | 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> |
19#include <crypto/md5.h> |
|
19#include <linux/init.h> 20#include <linux/module.h> 21#include <linux/string.h> 22#include <linux/types.h> 23#include <asm/byteorder.h> 24 | 20#include <linux/init.h> 21#include <linux/module.h> 22#include <linux/string.h> 23#include <linux/types.h> 24#include <asm/byteorder.h> 25 |
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 | |
30#define F1(x, y, z) (z ^ (x & (y ^ z))) 31#define F2(x, y, z) F1(z, x, y) 32#define F3(x, y, z) (x ^ y ^ z) 33#define F4(x, y, z) (y ^ (x | ~z)) 34 35#define MD5STEP(f, w, x, y, z, in, s) \ 36 (w += f(x, y, z) + in, w = (w<<s | w>>(32-s)) + x) 37 | 26#define F1(x, y, z) (z ^ (x & (y ^ z))) 27#define F2(x, y, z) F1(z, x, y) 28#define F3(x, y, z) (x ^ y ^ z) 29#define F4(x, y, z) (y ^ (x | ~z)) 30 31#define MD5STEP(f, w, x, y, z, in, s) \ 32 (w += f(x, y, z) + in, w = (w<<s | w>>(32-s)) + x) 33 |
38struct md5_ctx { 39 u32 hash[MD5_HASH_WORDS]; 40 u32 block[MD5_BLOCK_WORDS]; 41 u64 byte_count; 42}; 43 | |
44static void md5_transform(u32 *hash, u32 const *in) 45{ 46 u32 a, b, c, d; 47 48 a = hash[0]; 49 b = hash[1]; 50 c = hash[2]; 51 d = hash[3]; --- 84 unchanged lines hidden (view full) --- 136static inline void cpu_to_le32_array(u32 *buf, unsigned int words) 137{ 138 while (words--) { 139 __cpu_to_le32s(buf); 140 buf++; 141 } 142} 143 | 34static void md5_transform(u32 *hash, u32 const *in) 35{ 36 u32 a, b, c, d; 37 38 a = hash[0]; 39 b = hash[1]; 40 c = hash[2]; 41 d = hash[3]; --- 84 unchanged lines hidden (view full) --- 126static inline void cpu_to_le32_array(u32 *buf, unsigned int words) 127{ 128 while (words--) { 129 __cpu_to_le32s(buf); 130 buf++; 131 } 132} 133 |
144static inline void md5_transform_helper(struct md5_ctx *ctx) | 134static inline void md5_transform_helper(struct md5_state *ctx) |
145{ 146 le32_to_cpu_array(ctx->block, sizeof(ctx->block) / sizeof(u32)); 147 md5_transform(ctx->hash, ctx->block); 148} 149 150static int md5_init(struct shash_desc *desc) 151{ | 135{ 136 le32_to_cpu_array(ctx->block, sizeof(ctx->block) / sizeof(u32)); 137 md5_transform(ctx->hash, ctx->block); 138} 139 140static int md5_init(struct shash_desc *desc) 141{ |
152 struct md5_ctx *mctx = shash_desc_ctx(desc); | 142 struct md5_state *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; 159 160 return 0; 161} 162 163static int md5_update(struct shash_desc *desc, const u8 *data, unsigned int len) 164{ | 143 144 mctx->hash[0] = 0x67452301; 145 mctx->hash[1] = 0xefcdab89; 146 mctx->hash[2] = 0x98badcfe; 147 mctx->hash[3] = 0x10325476; 148 mctx->byte_count = 0; 149 150 return 0; 151} 152 153static int md5_update(struct shash_desc *desc, const u8 *data, unsigned int len) 154{ |
165 struct md5_ctx *mctx = shash_desc_ctx(desc); | 155 struct md5_state *mctx = shash_desc_ctx(desc); |
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); 173 return 0; --- 15 unchanged lines hidden (view full) --- 189 190 memcpy(mctx->block, data, len); 191 192 return 0; 193} 194 195static int md5_final(struct shash_desc *desc, u8 *out) 196{ | 156 const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f); 157 158 mctx->byte_count += len; 159 160 if (avail > len) { 161 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail), 162 data, len); 163 return 0; --- 15 unchanged lines hidden (view full) --- 179 180 memcpy(mctx->block, data, len); 181 182 return 0; 183} 184 185static int md5_final(struct shash_desc *desc, u8 *out) 186{ |
197 struct md5_ctx *mctx = shash_desc_ctx(desc); | 187 struct md5_state *mctx = shash_desc_ctx(desc); |
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); --- 9 unchanged lines hidden (view full) --- 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; 221} 222 | 188 const unsigned int offset = mctx->byte_count & 0x3f; 189 char *p = (char *)mctx->block + offset; 190 int padding = 56 - (offset + 1); 191 192 *p++ = 0x80; 193 if (padding < 0) { 194 memset(p, 0x00, padding + sizeof (u64)); 195 md5_transform_helper(mctx); --- 9 unchanged lines hidden (view full) --- 205 md5_transform(mctx->hash, mctx->block); 206 cpu_to_le32_array(mctx->hash, sizeof(mctx->hash) / sizeof(u32)); 207 memcpy(out, mctx->hash, sizeof(mctx->hash)); 208 memset(mctx, 0, sizeof(*mctx)); 209 210 return 0; 211} 212 |
213static int md5_export(struct shash_desc *desc, void *out) 214{ 215 struct md5_state *ctx = shash_desc_ctx(desc); 216 217 memcpy(out, ctx, sizeof(*ctx)); 218 return 0; 219} 220 221static int md5_import(struct shash_desc *desc, const void *in) 222{ 223 struct md5_state *ctx = shash_desc_ctx(desc); 224 225 memcpy(ctx, in, sizeof(*ctx)); 226 return 0; 227} 228 |
|
223static struct shash_alg alg = { 224 .digestsize = MD5_DIGEST_SIZE, 225 .init = md5_init, 226 .update = md5_update, 227 .final = md5_final, | 229static struct shash_alg alg = { 230 .digestsize = MD5_DIGEST_SIZE, 231 .init = md5_init, 232 .update = md5_update, 233 .final = md5_final, |
228 .descsize = sizeof(struct md5_ctx), | 234 .export = md5_export, 235 .import = md5_import, 236 .descsize = sizeof(struct md5_state), |
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 } 235}; 236 --- 15 unchanged lines hidden --- | 237 .base = { 238 .cra_name = "md5", 239 .cra_flags = CRYPTO_ALG_TYPE_SHASH, 240 .cra_blocksize = MD5_HMAC_BLOCK_SIZE, 241 .cra_module = THIS_MODULE, 242 } 243}; 244 --- 15 unchanged lines hidden --- |