ia64/linux-2.6.18-xen.hg

annotate crypto/md4.c @ 749:2892ca2b9c17

linux/x86: cleanup IO-APIC code

- get 32-bit code in sync with 64-bit wrt ExtINT pin detection being
unnecessary
- eliminate build warnings resulting from c/s 725

Signed-off-by: Jan Beulich <jbeulich@novell.com>
author Keir Fraser <keir.fraser@citrix.com>
date Fri Nov 28 13:31:21 2008 +0000 (2008-11-28)
parents 831230e53067
children
rev   line source
ian@0 1 /*
ian@0 2 * Cryptographic API.
ian@0 3 *
ian@0 4 * MD4 Message Digest Algorithm (RFC1320).
ian@0 5 *
ian@0 6 * Implementation derived from Andrew Tridgell and Steve French's
ian@0 7 * CIFS MD4 implementation, and the cryptoapi implementation
ian@0 8 * originally based on the public domain implementation written
ian@0 9 * by Colin Plumb in 1993.
ian@0 10 *
ian@0 11 * Copyright (c) Andrew Tridgell 1997-1998.
ian@0 12 * Modified by Steve French (sfrench@us.ibm.com) 2002
ian@0 13 * Copyright (c) Cryptoapi developers.
ian@0 14 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
ian@0 15 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
ian@0 16 *
ian@0 17 * This program is free software; you can redistribute it and/or modify
ian@0 18 * it under the terms of the GNU General Public License as published by
ian@0 19 * the Free Software Foundation; either version 2 of the License, or
ian@0 20 * (at your option) any later version.
ian@0 21 *
ian@0 22 */
ian@0 23 #include <linux/init.h>
ian@0 24 #include <linux/crypto.h>
ian@0 25 #include <linux/kernel.h>
ian@0 26 #include <linux/string.h>
ian@0 27 #include <linux/types.h>
ian@0 28 #include <asm/byteorder.h>
ian@0 29
ian@0 30 #define MD4_DIGEST_SIZE 16
ian@0 31 #define MD4_HMAC_BLOCK_SIZE 64
ian@0 32 #define MD4_BLOCK_WORDS 16
ian@0 33 #define MD4_HASH_WORDS 4
ian@0 34
ian@0 35 struct md4_ctx {
ian@0 36 u32 hash[MD4_HASH_WORDS];
ian@0 37 u32 block[MD4_BLOCK_WORDS];
ian@0 38 u64 byte_count;
ian@0 39 };
ian@0 40
ian@0 41 static inline u32 lshift(u32 x, unsigned int s)
ian@0 42 {
ian@0 43 x &= 0xFFFFFFFF;
ian@0 44 return ((x << s) & 0xFFFFFFFF) | (x >> (32 - s));
ian@0 45 }
ian@0 46
ian@0 47 static inline u32 F(u32 x, u32 y, u32 z)
ian@0 48 {
ian@0 49 return (x & y) | ((~x) & z);
ian@0 50 }
ian@0 51
ian@0 52 static inline u32 G(u32 x, u32 y, u32 z)
ian@0 53 {
ian@0 54 return (x & y) | (x & z) | (y & z);
ian@0 55 }
ian@0 56
ian@0 57 static inline u32 H(u32 x, u32 y, u32 z)
ian@0 58 {
ian@0 59 return x ^ y ^ z;
ian@0 60 }
ian@0 61
ian@0 62 #define ROUND1(a,b,c,d,k,s) (a = lshift(a + F(b,c,d) + k, s))
ian@0 63 #define ROUND2(a,b,c,d,k,s) (a = lshift(a + G(b,c,d) + k + (u32)0x5A827999,s))
ian@0 64 #define ROUND3(a,b,c,d,k,s) (a = lshift(a + H(b,c,d) + k + (u32)0x6ED9EBA1,s))
ian@0 65
ian@0 66 /* XXX: this stuff can be optimized */
ian@0 67 static inline void le32_to_cpu_array(u32 *buf, unsigned int words)
ian@0 68 {
ian@0 69 while (words--) {
ian@0 70 __le32_to_cpus(buf);
ian@0 71 buf++;
ian@0 72 }
ian@0 73 }
ian@0 74
ian@0 75 static inline void cpu_to_le32_array(u32 *buf, unsigned int words)
ian@0 76 {
ian@0 77 while (words--) {
ian@0 78 __cpu_to_le32s(buf);
ian@0 79 buf++;
ian@0 80 }
ian@0 81 }
ian@0 82
ian@0 83 static void md4_transform(u32 *hash, u32 const *in)
ian@0 84 {
ian@0 85 u32 a, b, c, d;
ian@0 86
ian@0 87 a = hash[0];
ian@0 88 b = hash[1];
ian@0 89 c = hash[2];
ian@0 90 d = hash[3];
ian@0 91
ian@0 92 ROUND1(a, b, c, d, in[0], 3);
ian@0 93 ROUND1(d, a, b, c, in[1], 7);
ian@0 94 ROUND1(c, d, a, b, in[2], 11);
ian@0 95 ROUND1(b, c, d, a, in[3], 19);
ian@0 96 ROUND1(a, b, c, d, in[4], 3);
ian@0 97 ROUND1(d, a, b, c, in[5], 7);
ian@0 98 ROUND1(c, d, a, b, in[6], 11);
ian@0 99 ROUND1(b, c, d, a, in[7], 19);
ian@0 100 ROUND1(a, b, c, d, in[8], 3);
ian@0 101 ROUND1(d, a, b, c, in[9], 7);
ian@0 102 ROUND1(c, d, a, b, in[10], 11);
ian@0 103 ROUND1(b, c, d, a, in[11], 19);
ian@0 104 ROUND1(a, b, c, d, in[12], 3);
ian@0 105 ROUND1(d, a, b, c, in[13], 7);
ian@0 106 ROUND1(c, d, a, b, in[14], 11);
ian@0 107 ROUND1(b, c, d, a, in[15], 19);
ian@0 108
ian@0 109 ROUND2(a, b, c, d,in[ 0], 3);
ian@0 110 ROUND2(d, a, b, c, in[4], 5);
ian@0 111 ROUND2(c, d, a, b, in[8], 9);
ian@0 112 ROUND2(b, c, d, a, in[12], 13);
ian@0 113 ROUND2(a, b, c, d, in[1], 3);
ian@0 114 ROUND2(d, a, b, c, in[5], 5);
ian@0 115 ROUND2(c, d, a, b, in[9], 9);
ian@0 116 ROUND2(b, c, d, a, in[13], 13);
ian@0 117 ROUND2(a, b, c, d, in[2], 3);
ian@0 118 ROUND2(d, a, b, c, in[6], 5);
ian@0 119 ROUND2(c, d, a, b, in[10], 9);
ian@0 120 ROUND2(b, c, d, a, in[14], 13);
ian@0 121 ROUND2(a, b, c, d, in[3], 3);
ian@0 122 ROUND2(d, a, b, c, in[7], 5);
ian@0 123 ROUND2(c, d, a, b, in[11], 9);
ian@0 124 ROUND2(b, c, d, a, in[15], 13);
ian@0 125
ian@0 126 ROUND3(a, b, c, d,in[ 0], 3);
ian@0 127 ROUND3(d, a, b, c, in[8], 9);
ian@0 128 ROUND3(c, d, a, b, in[4], 11);
ian@0 129 ROUND3(b, c, d, a, in[12], 15);
ian@0 130 ROUND3(a, b, c, d, in[2], 3);
ian@0 131 ROUND3(d, a, b, c, in[10], 9);
ian@0 132 ROUND3(c, d, a, b, in[6], 11);
ian@0 133 ROUND3(b, c, d, a, in[14], 15);
ian@0 134 ROUND3(a, b, c, d, in[1], 3);
ian@0 135 ROUND3(d, a, b, c, in[9], 9);
ian@0 136 ROUND3(c, d, a, b, in[5], 11);
ian@0 137 ROUND3(b, c, d, a, in[13], 15);
ian@0 138 ROUND3(a, b, c, d, in[3], 3);
ian@0 139 ROUND3(d, a, b, c, in[11], 9);
ian@0 140 ROUND3(c, d, a, b, in[7], 11);
ian@0 141 ROUND3(b, c, d, a, in[15], 15);
ian@0 142
ian@0 143 hash[0] += a;
ian@0 144 hash[1] += b;
ian@0 145 hash[2] += c;
ian@0 146 hash[3] += d;
ian@0 147 }
ian@0 148
ian@0 149 static inline void md4_transform_helper(struct md4_ctx *ctx)
ian@0 150 {
ian@0 151 le32_to_cpu_array(ctx->block, sizeof(ctx->block) / sizeof(u32));
ian@0 152 md4_transform(ctx->hash, ctx->block);
ian@0 153 }
ian@0 154
ian@0 155 static void md4_init(struct crypto_tfm *tfm)
ian@0 156 {
ian@0 157 struct md4_ctx *mctx = crypto_tfm_ctx(tfm);
ian@0 158
ian@0 159 mctx->hash[0] = 0x67452301;
ian@0 160 mctx->hash[1] = 0xefcdab89;
ian@0 161 mctx->hash[2] = 0x98badcfe;
ian@0 162 mctx->hash[3] = 0x10325476;
ian@0 163 mctx->byte_count = 0;
ian@0 164 }
ian@0 165
ian@0 166 static void md4_update(struct crypto_tfm *tfm, const u8 *data, unsigned int len)
ian@0 167 {
ian@0 168 struct md4_ctx *mctx = crypto_tfm_ctx(tfm);
ian@0 169 const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
ian@0 170
ian@0 171 mctx->byte_count += len;
ian@0 172
ian@0 173 if (avail > len) {
ian@0 174 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
ian@0 175 data, len);
ian@0 176 return;
ian@0 177 }
ian@0 178
ian@0 179 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
ian@0 180 data, avail);
ian@0 181
ian@0 182 md4_transform_helper(mctx);
ian@0 183 data += avail;
ian@0 184 len -= avail;
ian@0 185
ian@0 186 while (len >= sizeof(mctx->block)) {
ian@0 187 memcpy(mctx->block, data, sizeof(mctx->block));
ian@0 188 md4_transform_helper(mctx);
ian@0 189 data += sizeof(mctx->block);
ian@0 190 len -= sizeof(mctx->block);
ian@0 191 }
ian@0 192
ian@0 193 memcpy(mctx->block, data, len);
ian@0 194 }
ian@0 195
ian@0 196 static void md4_final(struct crypto_tfm *tfm, u8 *out)
ian@0 197 {
ian@0 198 struct md4_ctx *mctx = crypto_tfm_ctx(tfm);
ian@0 199 const unsigned int offset = mctx->byte_count & 0x3f;
ian@0 200 char *p = (char *)mctx->block + offset;
ian@0 201 int padding = 56 - (offset + 1);
ian@0 202
ian@0 203 *p++ = 0x80;
ian@0 204 if (padding < 0) {
ian@0 205 memset(p, 0x00, padding + sizeof (u64));
ian@0 206 md4_transform_helper(mctx);
ian@0 207 p = (char *)mctx->block;
ian@0 208 padding = 56;
ian@0 209 }
ian@0 210
ian@0 211 memset(p, 0, padding);
ian@0 212 mctx->block[14] = mctx->byte_count << 3;
ian@0 213 mctx->block[15] = mctx->byte_count >> 29;
ian@0 214 le32_to_cpu_array(mctx->block, (sizeof(mctx->block) -
ian@0 215 sizeof(u64)) / sizeof(u32));
ian@0 216 md4_transform(mctx->hash, mctx->block);
ian@0 217 cpu_to_le32_array(mctx->hash, sizeof(mctx->hash) / sizeof(u32));
ian@0 218 memcpy(out, mctx->hash, sizeof(mctx->hash));
ian@0 219 memset(mctx, 0, sizeof(*mctx));
ian@0 220 }
ian@0 221
ian@0 222 static struct crypto_alg alg = {
ian@0 223 .cra_name = "md4",
ian@0 224 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
ian@0 225 .cra_blocksize = MD4_HMAC_BLOCK_SIZE,
ian@0 226 .cra_ctxsize = sizeof(struct md4_ctx),
ian@0 227 .cra_module = THIS_MODULE,
ian@0 228 .cra_list = LIST_HEAD_INIT(alg.cra_list),
ian@0 229 .cra_u = { .digest = {
ian@0 230 .dia_digestsize = MD4_DIGEST_SIZE,
ian@0 231 .dia_init = md4_init,
ian@0 232 .dia_update = md4_update,
ian@0 233 .dia_final = md4_final } }
ian@0 234 };
ian@0 235
ian@0 236 static int __init init(void)
ian@0 237 {
ian@0 238 return crypto_register_alg(&alg);
ian@0 239 }
ian@0 240
ian@0 241 static void __exit fini(void)
ian@0 242 {
ian@0 243 crypto_unregister_alg(&alg);
ian@0 244 }
ian@0 245
ian@0 246 module_init(init);
ian@0 247 module_exit(fini);
ian@0 248
ian@0 249 MODULE_LICENSE("GPL");
ian@0 250 MODULE_DESCRIPTION("MD4 Message Digest Algorithm");
ian@0 251