1 /* 1 /* 2 * Cryptographic API. 2 * Cryptographic API. 3 * 3 * 4 * MD4 Message Digest Algorithm (RFC1320). 4 * MD4 Message Digest Algorithm (RFC1320). 5 * 5 * 6 * Implementation derived from Andrew Tridgell 6 * Implementation derived from Andrew Tridgell and Steve French's 7 * CIFS MD4 implementation, and the cryptoapi 7 * CIFS MD4 implementation, and the cryptoapi implementation 8 * originally based on the public domain imple 8 * originally based on the public domain implementation written 9 * by Colin Plumb in 1993. 9 * by Colin Plumb in 1993. 10 * 10 * 11 * Copyright (c) Andrew Tridgell 1997-1998. 11 * Copyright (c) Andrew Tridgell 1997-1998. 12 * Modified by Steve French (sfrench@us.ibm.co 12 * Modified by Steve French (sfrench@us.ibm.com) 2002 13 * Copyright (c) Cryptoapi developers. 13 * Copyright (c) Cryptoapi developers. 14 * Copyright (c) 2002 David S. Miller (davem@r 14 * Copyright (c) 2002 David S. Miller (davem@redhat.com) 15 * Copyright (c) 2002 James Morris <jmorris@in 15 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> 16 * 16 * 17 * This program is free software; you can redi 17 * This program is free software; you can redistribute it and/or modify 18 * it under the terms of the GNU General Publi 18 * it under the terms of the GNU General Public License as published by 19 * the Free Software Foundation; either versio 19 * the Free Software Foundation; either version 2 of the License, or 20 * (at your option) any later version. 20 * (at your option) any later version. 21 * 21 * 22 */ 22 */ 23 #include <crypto/internal/hash.h> 23 #include <crypto/internal/hash.h> 24 #include <linux/init.h> 24 #include <linux/init.h> 25 #include <linux/kernel.h> 25 #include <linux/kernel.h> 26 #include <linux/module.h> 26 #include <linux/module.h> 27 #include <linux/string.h> 27 #include <linux/string.h> 28 #include <linux/types.h> 28 #include <linux/types.h> 29 #include <asm/byteorder.h> 29 #include <asm/byteorder.h> 30 30 31 #define MD4_DIGEST_SIZE 16 31 #define MD4_DIGEST_SIZE 16 32 #define MD4_HMAC_BLOCK_SIZE 64 32 #define MD4_HMAC_BLOCK_SIZE 64 33 #define MD4_BLOCK_WORDS 16 33 #define MD4_BLOCK_WORDS 16 34 #define MD4_HASH_WORDS 4 34 #define MD4_HASH_WORDS 4 35 35 36 struct md4_ctx { 36 struct md4_ctx { 37 u32 hash[MD4_HASH_WORDS]; 37 u32 hash[MD4_HASH_WORDS]; 38 u32 block[MD4_BLOCK_WORDS]; 38 u32 block[MD4_BLOCK_WORDS]; 39 u64 byte_count; 39 u64 byte_count; 40 }; 40 }; 41 41 42 static inline u32 lshift(u32 x, unsigned int s 42 static inline u32 lshift(u32 x, unsigned int s) 43 { 43 { 44 x &= 0xFFFFFFFF; 44 x &= 0xFFFFFFFF; 45 return ((x << s) & 0xFFFFFFFF) | (x >> 45 return ((x << s) & 0xFFFFFFFF) | (x >> (32 - s)); 46 } 46 } 47 47 48 static inline u32 F(u32 x, u32 y, u32 z) 48 static inline u32 F(u32 x, u32 y, u32 z) 49 { 49 { 50 return (x & y) | ((~x) & z); 50 return (x & y) | ((~x) & z); 51 } 51 } 52 52 53 static inline u32 G(u32 x, u32 y, u32 z) 53 static inline u32 G(u32 x, u32 y, u32 z) 54 { 54 { 55 return (x & y) | (x & z) | (y & z); 55 return (x & y) | (x & z) | (y & z); 56 } 56 } 57 57 58 static inline u32 H(u32 x, u32 y, u32 z) 58 static inline u32 H(u32 x, u32 y, u32 z) 59 { 59 { 60 return x ^ y ^ z; 60 return x ^ y ^ z; 61 } 61 } 62 62 63 #define ROUND1(a,b,c,d,k,s) (a = lshift(a + F( 63 #define ROUND1(a,b,c,d,k,s) (a = lshift(a + F(b,c,d) + k, s)) 64 #define ROUND2(a,b,c,d,k,s) (a = lshift(a + G( 64 #define ROUND2(a,b,c,d,k,s) (a = lshift(a + G(b,c,d) + k + (u32)0x5A827999,s)) 65 #define ROUND3(a,b,c,d,k,s) (a = lshift(a + H( 65 #define ROUND3(a,b,c,d,k,s) (a = lshift(a + H(b,c,d) + k + (u32)0x6ED9EBA1,s)) 66 66 67 static void md4_transform(u32 *hash, u32 const 67 static void md4_transform(u32 *hash, u32 const *in) 68 { 68 { 69 u32 a, b, c, d; 69 u32 a, b, c, d; 70 70 71 a = hash[0]; 71 a = hash[0]; 72 b = hash[1]; 72 b = hash[1]; 73 c = hash[2]; 73 c = hash[2]; 74 d = hash[3]; 74 d = hash[3]; 75 75 76 ROUND1(a, b, c, d, in[0], 3); 76 ROUND1(a, b, c, d, in[0], 3); 77 ROUND1(d, a, b, c, in[1], 7); 77 ROUND1(d, a, b, c, in[1], 7); 78 ROUND1(c, d, a, b, in[2], 11); 78 ROUND1(c, d, a, b, in[2], 11); 79 ROUND1(b, c, d, a, in[3], 19); 79 ROUND1(b, c, d, a, in[3], 19); 80 ROUND1(a, b, c, d, in[4], 3); 80 ROUND1(a, b, c, d, in[4], 3); 81 ROUND1(d, a, b, c, in[5], 7); 81 ROUND1(d, a, b, c, in[5], 7); 82 ROUND1(c, d, a, b, in[6], 11); 82 ROUND1(c, d, a, b, in[6], 11); 83 ROUND1(b, c, d, a, in[7], 19); 83 ROUND1(b, c, d, a, in[7], 19); 84 ROUND1(a, b, c, d, in[8], 3); 84 ROUND1(a, b, c, d, in[8], 3); 85 ROUND1(d, a, b, c, in[9], 7); 85 ROUND1(d, a, b, c, in[9], 7); 86 ROUND1(c, d, a, b, in[10], 11); 86 ROUND1(c, d, a, b, in[10], 11); 87 ROUND1(b, c, d, a, in[11], 19); 87 ROUND1(b, c, d, a, in[11], 19); 88 ROUND1(a, b, c, d, in[12], 3); 88 ROUND1(a, b, c, d, in[12], 3); 89 ROUND1(d, a, b, c, in[13], 7); 89 ROUND1(d, a, b, c, in[13], 7); 90 ROUND1(c, d, a, b, in[14], 11); 90 ROUND1(c, d, a, b, in[14], 11); 91 ROUND1(b, c, d, a, in[15], 19); 91 ROUND1(b, c, d, a, in[15], 19); 92 92 93 ROUND2(a, b, c, d,in[ 0], 3); 93 ROUND2(a, b, c, d,in[ 0], 3); 94 ROUND2(d, a, b, c, in[4], 5); 94 ROUND2(d, a, b, c, in[4], 5); 95 ROUND2(c, d, a, b, in[8], 9); 95 ROUND2(c, d, a, b, in[8], 9); 96 ROUND2(b, c, d, a, in[12], 13); 96 ROUND2(b, c, d, a, in[12], 13); 97 ROUND2(a, b, c, d, in[1], 3); 97 ROUND2(a, b, c, d, in[1], 3); 98 ROUND2(d, a, b, c, in[5], 5); 98 ROUND2(d, a, b, c, in[5], 5); 99 ROUND2(c, d, a, b, in[9], 9); 99 ROUND2(c, d, a, b, in[9], 9); 100 ROUND2(b, c, d, a, in[13], 13); 100 ROUND2(b, c, d, a, in[13], 13); 101 ROUND2(a, b, c, d, in[2], 3); 101 ROUND2(a, b, c, d, in[2], 3); 102 ROUND2(d, a, b, c, in[6], 5); 102 ROUND2(d, a, b, c, in[6], 5); 103 ROUND2(c, d, a, b, in[10], 9); 103 ROUND2(c, d, a, b, in[10], 9); 104 ROUND2(b, c, d, a, in[14], 13); 104 ROUND2(b, c, d, a, in[14], 13); 105 ROUND2(a, b, c, d, in[3], 3); 105 ROUND2(a, b, c, d, in[3], 3); 106 ROUND2(d, a, b, c, in[7], 5); 106 ROUND2(d, a, b, c, in[7], 5); 107 ROUND2(c, d, a, b, in[11], 9); 107 ROUND2(c, d, a, b, in[11], 9); 108 ROUND2(b, c, d, a, in[15], 13); 108 ROUND2(b, c, d, a, in[15], 13); 109 109 110 ROUND3(a, b, c, d,in[ 0], 3); 110 ROUND3(a, b, c, d,in[ 0], 3); 111 ROUND3(d, a, b, c, in[8], 9); 111 ROUND3(d, a, b, c, in[8], 9); 112 ROUND3(c, d, a, b, in[4], 11); 112 ROUND3(c, d, a, b, in[4], 11); 113 ROUND3(b, c, d, a, in[12], 15); 113 ROUND3(b, c, d, a, in[12], 15); 114 ROUND3(a, b, c, d, in[2], 3); 114 ROUND3(a, b, c, d, in[2], 3); 115 ROUND3(d, a, b, c, in[10], 9); 115 ROUND3(d, a, b, c, in[10], 9); 116 ROUND3(c, d, a, b, in[6], 11); 116 ROUND3(c, d, a, b, in[6], 11); 117 ROUND3(b, c, d, a, in[14], 15); 117 ROUND3(b, c, d, a, in[14], 15); 118 ROUND3(a, b, c, d, in[1], 3); 118 ROUND3(a, b, c, d, in[1], 3); 119 ROUND3(d, a, b, c, in[9], 9); 119 ROUND3(d, a, b, c, in[9], 9); 120 ROUND3(c, d, a, b, in[5], 11); 120 ROUND3(c, d, a, b, in[5], 11); 121 ROUND3(b, c, d, a, in[13], 15); 121 ROUND3(b, c, d, a, in[13], 15); 122 ROUND3(a, b, c, d, in[3], 3); 122 ROUND3(a, b, c, d, in[3], 3); 123 ROUND3(d, a, b, c, in[11], 9); 123 ROUND3(d, a, b, c, in[11], 9); 124 ROUND3(c, d, a, b, in[7], 11); 124 ROUND3(c, d, a, b, in[7], 11); 125 ROUND3(b, c, d, a, in[15], 15); 125 ROUND3(b, c, d, a, in[15], 15); 126 126 127 hash[0] += a; 127 hash[0] += a; 128 hash[1] += b; 128 hash[1] += b; 129 hash[2] += c; 129 hash[2] += c; 130 hash[3] += d; 130 hash[3] += d; 131 } 131 } 132 132 133 static inline void md4_transform_helper(struct 133 static inline void md4_transform_helper(struct md4_ctx *ctx) 134 { 134 { 135 le32_to_cpu_array(ctx->block, ARRAY_SI 135 le32_to_cpu_array(ctx->block, ARRAY_SIZE(ctx->block)); 136 md4_transform(ctx->hash, ctx->block); 136 md4_transform(ctx->hash, ctx->block); 137 } 137 } 138 138 139 static int md4_init(struct shash_desc *desc) 139 static int md4_init(struct shash_desc *desc) 140 { 140 { 141 struct md4_ctx *mctx = shash_desc_ctx( 141 struct md4_ctx *mctx = shash_desc_ctx(desc); 142 142 143 mctx->hash[0] = 0x67452301; 143 mctx->hash[0] = 0x67452301; 144 mctx->hash[1] = 0xefcdab89; 144 mctx->hash[1] = 0xefcdab89; 145 mctx->hash[2] = 0x98badcfe; 145 mctx->hash[2] = 0x98badcfe; 146 mctx->hash[3] = 0x10325476; 146 mctx->hash[3] = 0x10325476; 147 mctx->byte_count = 0; 147 mctx->byte_count = 0; 148 148 149 return 0; 149 return 0; 150 } 150 } 151 151 152 static int md4_update(struct shash_desc *desc, 152 static int md4_update(struct shash_desc *desc, const u8 *data, unsigned int len) 153 { 153 { 154 struct md4_ctx *mctx = shash_desc_ctx( 154 struct md4_ctx *mctx = shash_desc_ctx(desc); 155 const u32 avail = sizeof(mctx->block) 155 const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f); 156 156 157 mctx->byte_count += len; 157 mctx->byte_count += len; 158 158 159 if (avail > len) { 159 if (avail > len) { 160 memcpy((char *)mctx->block + ( 160 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail), 161 data, len); 161 data, len); 162 return 0; 162 return 0; 163 } 163 } 164 164 165 memcpy((char *)mctx->block + (sizeof(m 165 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail), 166 data, avail); 166 data, avail); 167 167 168 md4_transform_helper(mctx); 168 md4_transform_helper(mctx); 169 data += avail; 169 data += avail; 170 len -= avail; 170 len -= avail; 171 171 172 while (len >= sizeof(mctx->block)) { 172 while (len >= sizeof(mctx->block)) { 173 memcpy(mctx->block, data, size 173 memcpy(mctx->block, data, sizeof(mctx->block)); 174 md4_transform_helper(mctx); 174 md4_transform_helper(mctx); 175 data += sizeof(mctx->block); 175 data += sizeof(mctx->block); 176 len -= sizeof(mctx->block); 176 len -= sizeof(mctx->block); 177 } 177 } 178 178 179 memcpy(mctx->block, data, len); 179 memcpy(mctx->block, data, len); 180 180 181 return 0; 181 return 0; 182 } 182 } 183 183 184 static int md4_final(struct shash_desc *desc, 184 static int md4_final(struct shash_desc *desc, u8 *out) 185 { 185 { 186 struct md4_ctx *mctx = shash_desc_ctx( 186 struct md4_ctx *mctx = shash_desc_ctx(desc); 187 const unsigned int offset = mctx->byte 187 const unsigned int offset = mctx->byte_count & 0x3f; 188 char *p = (char *)mctx->block + offset 188 char *p = (char *)mctx->block + offset; 189 int padding = 56 - (offset + 1); 189 int padding = 56 - (offset + 1); 190 190 191 *p++ = 0x80; 191 *p++ = 0x80; 192 if (padding < 0) { 192 if (padding < 0) { 193 memset(p, 0x00, padding + size 193 memset(p, 0x00, padding + sizeof (u64)); 194 md4_transform_helper(mctx); 194 md4_transform_helper(mctx); 195 p = (char *)mctx->block; 195 p = (char *)mctx->block; 196 padding = 56; 196 padding = 56; 197 } 197 } 198 198 199 memset(p, 0, padding); 199 memset(p, 0, padding); 200 mctx->block[14] = mctx->byte_count << 200 mctx->block[14] = mctx->byte_count << 3; 201 mctx->block[15] = mctx->byte_count >> 201 mctx->block[15] = mctx->byte_count >> 29; 202 le32_to_cpu_array(mctx->block, (sizeof 202 le32_to_cpu_array(mctx->block, (sizeof(mctx->block) - 203 sizeof(u64)) / sizeo 203 sizeof(u64)) / sizeof(u32)); 204 md4_transform(mctx->hash, mctx->block) 204 md4_transform(mctx->hash, mctx->block); 205 cpu_to_le32_array(mctx->hash, ARRAY_SI 205 cpu_to_le32_array(mctx->hash, ARRAY_SIZE(mctx->hash)); 206 memcpy(out, mctx->hash, sizeof(mctx->h 206 memcpy(out, mctx->hash, sizeof(mctx->hash)); 207 memset(mctx, 0, sizeof(*mctx)); 207 memset(mctx, 0, sizeof(*mctx)); 208 208 209 return 0; 209 return 0; 210 } 210 } 211 211 212 static struct shash_alg alg = { 212 static struct shash_alg alg = { 213 .digestsize = MD4_DIGEST_SIZ 213 .digestsize = MD4_DIGEST_SIZE, 214 .init = md4_init, 214 .init = md4_init, 215 .update = md4_update, 215 .update = md4_update, 216 .final = md4_final, 216 .final = md4_final, 217 .descsize = sizeof(struct 217 .descsize = sizeof(struct md4_ctx), 218 .base = { 218 .base = { 219 .cra_name = "md4", !! 219 .cra_name = "md4", 220 .cra_driver_name = "md4-g !! 220 .cra_blocksize = MD4_HMAC_BLOCK_SIZE, 221 .cra_blocksize = MD4_HM !! 221 .cra_module = THIS_MODULE, 222 .cra_module = THIS_M << 223 } 222 } 224 }; 223 }; 225 224 226 static int __init md4_mod_init(void) 225 static int __init md4_mod_init(void) 227 { 226 { 228 return crypto_register_shash(&alg); 227 return crypto_register_shash(&alg); 229 } 228 } 230 229 231 static void __exit md4_mod_fini(void) 230 static void __exit md4_mod_fini(void) 232 { 231 { 233 crypto_unregister_shash(&alg); 232 crypto_unregister_shash(&alg); 234 } 233 } 235 234 236 subsys_initcall(md4_mod_init); !! 235 module_init(md4_mod_init); 237 module_exit(md4_mod_fini); 236 module_exit(md4_mod_fini); 238 237 239 MODULE_LICENSE("GPL"); 238 MODULE_LICENSE("GPL"); 240 MODULE_DESCRIPTION("MD4 Message Digest Algorit 239 MODULE_DESCRIPTION("MD4 Message Digest Algorithm"); 241 MODULE_ALIAS_CRYPTO("md4"); 240 MODULE_ALIAS_CRYPTO("md4"); 242 241
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.