1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Cryptographic API. 4 * 5 * MD4 Message Digest Algorithm (RFC1320). 6 * 7 * Implementation derived from Andrew Tridgell and Steve French's 8 * CIFS MD4 implementation, and the cryptoapi implementation 9 * originally based on the public domain implementation written 10 * by Colin Plumb in 1993. 11 * 12 * Copyright (c) Andrew Tridgell 1997-1998. 13 * Modified by Steve French (sfrench@us.ibm.com) 2002 14 * Copyright (c) Cryptoapi developers. 15 * Copyright (c) 2002 David S. Miller (davem@redhat.com) 16 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> 17 * 18 */ 19 #include <linux/init.h> 20 #include <linux/kernel.h> 21 #include <linux/module.h> 22 #include <linux/string.h> 23 #include <linux/types.h> 24 #include <asm/byteorder.h> 25 #include "md4.h" 26 27 MODULE_DESCRIPTION("MD4 Message Digest Algorithm (RFC1320)"); 28 MODULE_LICENSE("GPL"); 29 30 static inline u32 lshift(u32 x, unsigned int s) 31 { 32 x &= 0xFFFFFFFF; 33 return ((x << s) & 0xFFFFFFFF) | (x >> (32 - s)); 34 } 35 36 static inline u32 F(u32 x, u32 y, u32 z) 37 { 38 return (x & y) | ((~x) & z); 39 } 40 41 static inline u32 G(u32 x, u32 y, u32 z) 42 { 43 return (x & y) | (x & z) | (y & z); 44 } 45 46 static inline u32 H(u32 x, u32 y, u32 z) 47 { 48 return x ^ y ^ z; 49 } 50 51 #define ROUND1(a,b,c,d,k,s) (a = lshift(a + F(b,c,d) + k, s)) 52 #define ROUND2(a,b,c,d,k,s) (a = lshift(a + G(b,c,d) + k + (u32)0x5A827999,s)) 53 #define ROUND3(a,b,c,d,k,s) (a = lshift(a + H(b,c,d) + k + (u32)0x6ED9EBA1,s)) 54 55 static void md4_transform(u32 *hash, u32 const *in) 56 { 57 u32 a, b, c, d; 58 59 a = hash[0]; 60 b = hash[1]; 61 c = hash[2]; 62 d = hash[3]; 63 64 ROUND1(a, b, c, d, in[0], 3); 65 ROUND1(d, a, b, c, in[1], 7); 66 ROUND1(c, d, a, b, in[2], 11); 67 ROUND1(b, c, d, a, in[3], 19); 68 ROUND1(a, b, c, d, in[4], 3); 69 ROUND1(d, a, b, c, in[5], 7); 70 ROUND1(c, d, a, b, in[6], 11); 71 ROUND1(b, c, d, a, in[7], 19); 72 ROUND1(a, b, c, d, in[8], 3); 73 ROUND1(d, a, b, c, in[9], 7); 74 ROUND1(c, d, a, b, in[10], 11); 75 ROUND1(b, c, d, a, in[11], 19); 76 ROUND1(a, b, c, d, in[12], 3); 77 ROUND1(d, a, b, c, in[13], 7); 78 ROUND1(c, d, a, b, in[14], 11); 79 ROUND1(b, c, d, a, in[15], 19); 80 81 ROUND2(a, b, c, d, in[0], 3); 82 ROUND2(d, a, b, c, in[4], 5); 83 ROUND2(c, d, a, b, in[8], 9); 84 ROUND2(b, c, d, a, in[12], 13); 85 ROUND2(a, b, c, d, in[1], 3); 86 ROUND2(d, a, b, c, in[5], 5); 87 ROUND2(c, d, a, b, in[9], 9); 88 ROUND2(b, c, d, a, in[13], 13); 89 ROUND2(a, b, c, d, in[2], 3); 90 ROUND2(d, a, b, c, in[6], 5); 91 ROUND2(c, d, a, b, in[10], 9); 92 ROUND2(b, c, d, a, in[14], 13); 93 ROUND2(a, b, c, d, in[3], 3); 94 ROUND2(d, a, b, c, in[7], 5); 95 ROUND2(c, d, a, b, in[11], 9); 96 ROUND2(b, c, d, a, in[15], 13); 97 98 ROUND3(a, b, c, d, in[0], 3); 99 ROUND3(d, a, b, c, in[8], 9); 100 ROUND3(c, d, a, b, in[4], 11); 101 ROUND3(b, c, d, a, in[12], 15); 102 ROUND3(a, b, c, d, in[2], 3); 103 ROUND3(d, a, b, c, in[10], 9); 104 ROUND3(c, d, a, b, in[6], 11); 105 ROUND3(b, c, d, a, in[14], 15); 106 ROUND3(a, b, c, d, in[1], 3); 107 ROUND3(d, a, b, c, in[9], 9); 108 ROUND3(c, d, a, b, in[5], 11); 109 ROUND3(b, c, d, a, in[13], 15); 110 ROUND3(a, b, c, d, in[3], 3); 111 ROUND3(d, a, b, c, in[11], 9); 112 ROUND3(c, d, a, b, in[7], 11); 113 ROUND3(b, c, d, a, in[15], 15); 114 115 hash[0] += a; 116 hash[1] += b; 117 hash[2] += c; 118 hash[3] += d; 119 } 120 121 static inline void md4_transform_helper(struct md4_ctx *ctx) 122 { 123 le32_to_cpu_array(ctx->block, ARRAY_SIZE(ctx->block)); 124 md4_transform(ctx->hash, ctx->block); 125 } 126 127 int cifs_md4_init(struct md4_ctx *mctx) 128 { 129 memset(mctx, 0, sizeof(struct md4_ctx)); 130 mctx->hash[0] = 0x67452301; 131 mctx->hash[1] = 0xefcdab89; 132 mctx->hash[2] = 0x98badcfe; 133 mctx->hash[3] = 0x10325476; 134 mctx->byte_count = 0; 135 136 return 0; 137 } 138 EXPORT_SYMBOL_GPL(cifs_md4_init); 139 140 int cifs_md4_update(struct md4_ctx *mctx, const u8 *data, unsigned int len) 141 { 142 const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f); 143 144 mctx->byte_count += len; 145 146 if (avail > len) { 147 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail), 148 data, len); 149 return 0; 150 } 151 152 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail), 153 data, avail); 154 155 md4_transform_helper(mctx); 156 data += avail; 157 len -= avail; 158 159 while (len >= sizeof(mctx->block)) { 160 memcpy(mctx->block, data, sizeof(mctx->block)); 161 md4_transform_helper(mctx); 162 data += sizeof(mctx->block); 163 len -= sizeof(mctx->block); 164 } 165 166 memcpy(mctx->block, data, len); 167 168 return 0; 169 } 170 EXPORT_SYMBOL_GPL(cifs_md4_update); 171 172 int cifs_md4_final(struct md4_ctx *mctx, u8 *out) 173 { 174 const unsigned int offset = mctx->byte_count & 0x3f; 175 char *p = (char *)mctx->block + offset; 176 int padding = 56 - (offset + 1); 177 178 *p++ = 0x80; 179 if (padding < 0) { 180 memset(p, 0x00, padding + sizeof(u64)); 181 md4_transform_helper(mctx); 182 p = (char *)mctx->block; 183 padding = 56; 184 } 185 186 memset(p, 0, padding); 187 mctx->block[14] = mctx->byte_count << 3; 188 mctx->block[15] = mctx->byte_count >> 29; 189 le32_to_cpu_array(mctx->block, (sizeof(mctx->block) - 190 sizeof(u64)) / sizeof(u32)); 191 md4_transform(mctx->hash, mctx->block); 192 cpu_to_le32_array(mctx->hash, ARRAY_SIZE(mctx->hash)); 193 memcpy(out, mctx->hash, sizeof(mctx->hash)); 194 memset(mctx, 0, sizeof(*mctx)); 195 196 return 0; 197 } 198 EXPORT_SYMBOL_GPL(cifs_md4_final); 199