0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019 #include <linux/init.h>
0020 #include <linux/kernel.h>
0021 #include <linux/module.h>
0022 #include <linux/string.h>
0023 #include <linux/types.h>
0024 #include <asm/byteorder.h>
0025 #include "md4.h"
0026
0027 MODULE_LICENSE("GPL");
0028
0029 static inline u32 lshift(u32 x, unsigned int s)
0030 {
0031 x &= 0xFFFFFFFF;
0032 return ((x << s) & 0xFFFFFFFF) | (x >> (32 - s));
0033 }
0034
0035 static inline u32 F(u32 x, u32 y, u32 z)
0036 {
0037 return (x & y) | ((~x) & z);
0038 }
0039
0040 static inline u32 G(u32 x, u32 y, u32 z)
0041 {
0042 return (x & y) | (x & z) | (y & z);
0043 }
0044
0045 static inline u32 H(u32 x, u32 y, u32 z)
0046 {
0047 return x ^ y ^ z;
0048 }
0049
0050 #define ROUND1(a,b,c,d,k,s) (a = lshift(a + F(b,c,d) + k, s))
0051 #define ROUND2(a,b,c,d,k,s) (a = lshift(a + G(b,c,d) + k + (u32)0x5A827999,s))
0052 #define ROUND3(a,b,c,d,k,s) (a = lshift(a + H(b,c,d) + k + (u32)0x6ED9EBA1,s))
0053
0054 static void md4_transform(u32 *hash, u32 const *in)
0055 {
0056 u32 a, b, c, d;
0057
0058 a = hash[0];
0059 b = hash[1];
0060 c = hash[2];
0061 d = hash[3];
0062
0063 ROUND1(a, b, c, d, in[0], 3);
0064 ROUND1(d, a, b, c, in[1], 7);
0065 ROUND1(c, d, a, b, in[2], 11);
0066 ROUND1(b, c, d, a, in[3], 19);
0067 ROUND1(a, b, c, d, in[4], 3);
0068 ROUND1(d, a, b, c, in[5], 7);
0069 ROUND1(c, d, a, b, in[6], 11);
0070 ROUND1(b, c, d, a, in[7], 19);
0071 ROUND1(a, b, c, d, in[8], 3);
0072 ROUND1(d, a, b, c, in[9], 7);
0073 ROUND1(c, d, a, b, in[10], 11);
0074 ROUND1(b, c, d, a, in[11], 19);
0075 ROUND1(a, b, c, d, in[12], 3);
0076 ROUND1(d, a, b, c, in[13], 7);
0077 ROUND1(c, d, a, b, in[14], 11);
0078 ROUND1(b, c, d, a, in[15], 19);
0079
0080 ROUND2(a, b, c, d, in[0], 3);
0081 ROUND2(d, a, b, c, in[4], 5);
0082 ROUND2(c, d, a, b, in[8], 9);
0083 ROUND2(b, c, d, a, in[12], 13);
0084 ROUND2(a, b, c, d, in[1], 3);
0085 ROUND2(d, a, b, c, in[5], 5);
0086 ROUND2(c, d, a, b, in[9], 9);
0087 ROUND2(b, c, d, a, in[13], 13);
0088 ROUND2(a, b, c, d, in[2], 3);
0089 ROUND2(d, a, b, c, in[6], 5);
0090 ROUND2(c, d, a, b, in[10], 9);
0091 ROUND2(b, c, d, a, in[14], 13);
0092 ROUND2(a, b, c, d, in[3], 3);
0093 ROUND2(d, a, b, c, in[7], 5);
0094 ROUND2(c, d, a, b, in[11], 9);
0095 ROUND2(b, c, d, a, in[15], 13);
0096
0097 ROUND3(a, b, c, d, in[0], 3);
0098 ROUND3(d, a, b, c, in[8], 9);
0099 ROUND3(c, d, a, b, in[4], 11);
0100 ROUND3(b, c, d, a, in[12], 15);
0101 ROUND3(a, b, c, d, in[2], 3);
0102 ROUND3(d, a, b, c, in[10], 9);
0103 ROUND3(c, d, a, b, in[6], 11);
0104 ROUND3(b, c, d, a, in[14], 15);
0105 ROUND3(a, b, c, d, in[1], 3);
0106 ROUND3(d, a, b, c, in[9], 9);
0107 ROUND3(c, d, a, b, in[5], 11);
0108 ROUND3(b, c, d, a, in[13], 15);
0109 ROUND3(a, b, c, d, in[3], 3);
0110 ROUND3(d, a, b, c, in[11], 9);
0111 ROUND3(c, d, a, b, in[7], 11);
0112 ROUND3(b, c, d, a, in[15], 15);
0113
0114 hash[0] += a;
0115 hash[1] += b;
0116 hash[2] += c;
0117 hash[3] += d;
0118 }
0119
0120 static inline void md4_transform_helper(struct md4_ctx *ctx)
0121 {
0122 le32_to_cpu_array(ctx->block, ARRAY_SIZE(ctx->block));
0123 md4_transform(ctx->hash, ctx->block);
0124 }
0125
0126 int cifs_md4_init(struct md4_ctx *mctx)
0127 {
0128 memset(mctx, 0, sizeof(struct md4_ctx));
0129 mctx->hash[0] = 0x67452301;
0130 mctx->hash[1] = 0xefcdab89;
0131 mctx->hash[2] = 0x98badcfe;
0132 mctx->hash[3] = 0x10325476;
0133 mctx->byte_count = 0;
0134
0135 return 0;
0136 }
0137 EXPORT_SYMBOL_GPL(cifs_md4_init);
0138
0139 int cifs_md4_update(struct md4_ctx *mctx, const u8 *data, unsigned int len)
0140 {
0141 const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
0142
0143 mctx->byte_count += len;
0144
0145 if (avail > len) {
0146 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
0147 data, len);
0148 return 0;
0149 }
0150
0151 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
0152 data, avail);
0153
0154 md4_transform_helper(mctx);
0155 data += avail;
0156 len -= avail;
0157
0158 while (len >= sizeof(mctx->block)) {
0159 memcpy(mctx->block, data, sizeof(mctx->block));
0160 md4_transform_helper(mctx);
0161 data += sizeof(mctx->block);
0162 len -= sizeof(mctx->block);
0163 }
0164
0165 memcpy(mctx->block, data, len);
0166
0167 return 0;
0168 }
0169 EXPORT_SYMBOL_GPL(cifs_md4_update);
0170
0171 int cifs_md4_final(struct md4_ctx *mctx, u8 *out)
0172 {
0173 const unsigned int offset = mctx->byte_count & 0x3f;
0174 char *p = (char *)mctx->block + offset;
0175 int padding = 56 - (offset + 1);
0176
0177 *p++ = 0x80;
0178 if (padding < 0) {
0179 memset(p, 0x00, padding + sizeof(u64));
0180 md4_transform_helper(mctx);
0181 p = (char *)mctx->block;
0182 padding = 56;
0183 }
0184
0185 memset(p, 0, padding);
0186 mctx->block[14] = mctx->byte_count << 3;
0187 mctx->block[15] = mctx->byte_count >> 29;
0188 le32_to_cpu_array(mctx->block, (sizeof(mctx->block) -
0189 sizeof(u64)) / sizeof(u32));
0190 md4_transform(mctx->hash, mctx->block);
0191 cpu_to_le32_array(mctx->hash, ARRAY_SIZE(mctx->hash));
0192 memcpy(out, mctx->hash, sizeof(mctx->hash));
0193 memset(mctx, 0, sizeof(*mctx));
0194
0195 return 0;
0196 }
0197 EXPORT_SYMBOL_GPL(cifs_md4_final);