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