Back to home page

LXR

 
 

    


0001 /*
0002  * Cryptographic API.
0003  *
0004  * RIPEMD-128 - RACE Integrity Primitives Evaluation Message Digest.
0005  *
0006  * Based on the reference implementation by Antoon Bosselaers, ESAT-COSIC
0007  *
0008  * Copyright (c) 2008 Adrian-Ken Rueegsegger <ken@codelabs.ch>
0009  *
0010  * This program is free software; you can redistribute it and/or modify it
0011  * under the terms of the GNU General Public License as published by the Free
0012  * Software Foundation; either version 2 of the License, or (at your option)
0013  * any later version.
0014  *
0015  */
0016 #include <crypto/internal/hash.h>
0017 #include <linux/init.h>
0018 #include <linux/module.h>
0019 #include <linux/mm.h>
0020 #include <linux/types.h>
0021 #include <asm/byteorder.h>
0022 
0023 #include "ripemd.h"
0024 
0025 struct rmd128_ctx {
0026     u64 byte_count;
0027     u32 state[4];
0028     __le32 buffer[16];
0029 };
0030 
0031 #define K1  RMD_K1
0032 #define K2  RMD_K2
0033 #define K3  RMD_K3
0034 #define K4  RMD_K4
0035 #define KK1 RMD_K6
0036 #define KK2 RMD_K7
0037 #define KK3 RMD_K8
0038 #define KK4 RMD_K1
0039 
0040 #define F1(x, y, z) (x ^ y ^ z)     /* XOR */
0041 #define F2(x, y, z) (z ^ (x & (y ^ z))) /* x ? y : z */
0042 #define F3(x, y, z) ((x | ~y) ^ z)
0043 #define F4(x, y, z) (y ^ (z & (x ^ y))) /* z ? x : y */
0044 
0045 #define ROUND(a, b, c, d, f, k, x, s)  { \
0046     (a) += f((b), (c), (d)) + le32_to_cpup(&(x)) + (k); \
0047     (a) = rol32((a), (s)); \
0048 }
0049 
0050 static void rmd128_transform(u32 *state, const __le32 *in)
0051 {
0052     u32 aa, bb, cc, dd, aaa, bbb, ccc, ddd;
0053 
0054     /* Initialize left lane */
0055     aa = state[0];
0056     bb = state[1];
0057     cc = state[2];
0058     dd = state[3];
0059 
0060     /* Initialize right lane */
0061     aaa = state[0];
0062     bbb = state[1];
0063     ccc = state[2];
0064     ddd = state[3];
0065 
0066     /* round 1: left lane */
0067     ROUND(aa, bb, cc, dd, F1, K1, in[0],  11);
0068     ROUND(dd, aa, bb, cc, F1, K1, in[1],  14);
0069     ROUND(cc, dd, aa, bb, F1, K1, in[2],  15);
0070     ROUND(bb, cc, dd, aa, F1, K1, in[3],  12);
0071     ROUND(aa, bb, cc, dd, F1, K1, in[4],   5);
0072     ROUND(dd, aa, bb, cc, F1, K1, in[5],   8);
0073     ROUND(cc, dd, aa, bb, F1, K1, in[6],   7);
0074     ROUND(bb, cc, dd, aa, F1, K1, in[7],   9);
0075     ROUND(aa, bb, cc, dd, F1, K1, in[8],  11);
0076     ROUND(dd, aa, bb, cc, F1, K1, in[9],  13);
0077     ROUND(cc, dd, aa, bb, F1, K1, in[10], 14);
0078     ROUND(bb, cc, dd, aa, F1, K1, in[11], 15);
0079     ROUND(aa, bb, cc, dd, F1, K1, in[12],  6);
0080     ROUND(dd, aa, bb, cc, F1, K1, in[13],  7);
0081     ROUND(cc, dd, aa, bb, F1, K1, in[14],  9);
0082     ROUND(bb, cc, dd, aa, F1, K1, in[15],  8);
0083 
0084     /* round 2: left lane */
0085     ROUND(aa, bb, cc, dd, F2, K2, in[7],   7);
0086     ROUND(dd, aa, bb, cc, F2, K2, in[4],   6);
0087     ROUND(cc, dd, aa, bb, F2, K2, in[13],  8);
0088     ROUND(bb, cc, dd, aa, F2, K2, in[1],  13);
0089     ROUND(aa, bb, cc, dd, F2, K2, in[10], 11);
0090     ROUND(dd, aa, bb, cc, F2, K2, in[6],   9);
0091     ROUND(cc, dd, aa, bb, F2, K2, in[15],  7);
0092     ROUND(bb, cc, dd, aa, F2, K2, in[3],  15);
0093     ROUND(aa, bb, cc, dd, F2, K2, in[12],  7);
0094     ROUND(dd, aa, bb, cc, F2, K2, in[0],  12);
0095     ROUND(cc, dd, aa, bb, F2, K2, in[9],  15);
0096     ROUND(bb, cc, dd, aa, F2, K2, in[5],   9);
0097     ROUND(aa, bb, cc, dd, F2, K2, in[2],  11);
0098     ROUND(dd, aa, bb, cc, F2, K2, in[14],  7);
0099     ROUND(cc, dd, aa, bb, F2, K2, in[11], 13);
0100     ROUND(bb, cc, dd, aa, F2, K2, in[8],  12);
0101 
0102     /* round 3: left lane */
0103     ROUND(aa, bb, cc, dd, F3, K3, in[3],  11);
0104     ROUND(dd, aa, bb, cc, F3, K3, in[10], 13);
0105     ROUND(cc, dd, aa, bb, F3, K3, in[14],  6);
0106     ROUND(bb, cc, dd, aa, F3, K3, in[4],   7);
0107     ROUND(aa, bb, cc, dd, F3, K3, in[9],  14);
0108     ROUND(dd, aa, bb, cc, F3, K3, in[15],  9);
0109     ROUND(cc, dd, aa, bb, F3, K3, in[8],  13);
0110     ROUND(bb, cc, dd, aa, F3, K3, in[1],  15);
0111     ROUND(aa, bb, cc, dd, F3, K3, in[2],  14);
0112     ROUND(dd, aa, bb, cc, F3, K3, in[7],   8);
0113     ROUND(cc, dd, aa, bb, F3, K3, in[0],  13);
0114     ROUND(bb, cc, dd, aa, F3, K3, in[6],   6);
0115     ROUND(aa, bb, cc, dd, F3, K3, in[13],  5);
0116     ROUND(dd, aa, bb, cc, F3, K3, in[11], 12);
0117     ROUND(cc, dd, aa, bb, F3, K3, in[5],   7);
0118     ROUND(bb, cc, dd, aa, F3, K3, in[12],  5);
0119 
0120     /* round 4: left lane */
0121     ROUND(aa, bb, cc, dd, F4, K4, in[1],  11);
0122     ROUND(dd, aa, bb, cc, F4, K4, in[9],  12);
0123     ROUND(cc, dd, aa, bb, F4, K4, in[11], 14);
0124     ROUND(bb, cc, dd, aa, F4, K4, in[10], 15);
0125     ROUND(aa, bb, cc, dd, F4, K4, in[0],  14);
0126     ROUND(dd, aa, bb, cc, F4, K4, in[8],  15);
0127     ROUND(cc, dd, aa, bb, F4, K4, in[12],  9);
0128     ROUND(bb, cc, dd, aa, F4, K4, in[4],   8);
0129     ROUND(aa, bb, cc, dd, F4, K4, in[13],  9);
0130     ROUND(dd, aa, bb, cc, F4, K4, in[3],  14);
0131     ROUND(cc, dd, aa, bb, F4, K4, in[7],   5);
0132     ROUND(bb, cc, dd, aa, F4, K4, in[15],  6);
0133     ROUND(aa, bb, cc, dd, F4, K4, in[14],  8);
0134     ROUND(dd, aa, bb, cc, F4, K4, in[5],   6);
0135     ROUND(cc, dd, aa, bb, F4, K4, in[6],   5);
0136     ROUND(bb, cc, dd, aa, F4, K4, in[2],  12);
0137 
0138     /* round 1: right lane */
0139     ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[5],   8);
0140     ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[14],  9);
0141     ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[7],   9);
0142     ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[0],  11);
0143     ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[9],  13);
0144     ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[2],  15);
0145     ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[11], 15);
0146     ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[4],   5);
0147     ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[13],  7);
0148     ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[6],   7);
0149     ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[15],  8);
0150     ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[8],  11);
0151     ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[1],  14);
0152     ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[10], 14);
0153     ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[3],  12);
0154     ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[12],  6);
0155 
0156     /* round 2: right lane */
0157     ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[6],   9);
0158     ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[11], 13);
0159     ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[3],  15);
0160     ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[7],   7);
0161     ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[0],  12);
0162     ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[13],  8);
0163     ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[5],   9);
0164     ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[10], 11);
0165     ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[14],  7);
0166     ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[15],  7);
0167     ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[8],  12);
0168     ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[12],  7);
0169     ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[4],   6);
0170     ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[9],  15);
0171     ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[1],  13);
0172     ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[2],  11);
0173 
0174     /* round 3: right lane */
0175     ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[15],  9);
0176     ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[5],   7);
0177     ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[1],  15);
0178     ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[3],  11);
0179     ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[7],   8);
0180     ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[14],  6);
0181     ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[6],   6);
0182     ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[9],  14);
0183     ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[11], 12);
0184     ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[8],  13);
0185     ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[12],  5);
0186     ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[2],  14);
0187     ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[10], 13);
0188     ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[0],  13);
0189     ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[4],   7);
0190     ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[13],  5);
0191 
0192     /* round 4: right lane */
0193     ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[8],  15);
0194     ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[6],   5);
0195     ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[4],   8);
0196     ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[1],  11);
0197     ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[3],  14);
0198     ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[11], 14);
0199     ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[15],  6);
0200     ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[0],  14);
0201     ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[5],   6);
0202     ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[12],  9);
0203     ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[2],  12);
0204     ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[13],  9);
0205     ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[9],  12);
0206     ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[7],   5);
0207     ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[10], 15);
0208     ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[14],  8);
0209 
0210     /* combine results */
0211     ddd += cc + state[1];       /* final result for state[0] */
0212     state[1] = state[2] + dd + aaa;
0213     state[2] = state[3] + aa + bbb;
0214     state[3] = state[0] + bb + ccc;
0215     state[0] = ddd;
0216 
0217     return;
0218 }
0219 
0220 static int rmd128_init(struct shash_desc *desc)
0221 {
0222     struct rmd128_ctx *rctx = shash_desc_ctx(desc);
0223 
0224     rctx->byte_count = 0;
0225 
0226     rctx->state[0] = RMD_H0;
0227     rctx->state[1] = RMD_H1;
0228     rctx->state[2] = RMD_H2;
0229     rctx->state[3] = RMD_H3;
0230 
0231     memset(rctx->buffer, 0, sizeof(rctx->buffer));
0232 
0233     return 0;
0234 }
0235 
0236 static int rmd128_update(struct shash_desc *desc, const u8 *data,
0237              unsigned int len)
0238 {
0239     struct rmd128_ctx *rctx = shash_desc_ctx(desc);
0240     const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
0241 
0242     rctx->byte_count += len;
0243 
0244     /* Enough space in buffer? If so copy and we're done */
0245     if (avail > len) {
0246         memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
0247                data, len);
0248         goto out;
0249     }
0250 
0251     memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
0252            data, avail);
0253 
0254     rmd128_transform(rctx->state, rctx->buffer);
0255     data += avail;
0256     len -= avail;
0257 
0258     while (len >= sizeof(rctx->buffer)) {
0259         memcpy(rctx->buffer, data, sizeof(rctx->buffer));
0260         rmd128_transform(rctx->state, rctx->buffer);
0261         data += sizeof(rctx->buffer);
0262         len -= sizeof(rctx->buffer);
0263     }
0264 
0265     memcpy(rctx->buffer, data, len);
0266 
0267 out:
0268     return 0;
0269 }
0270 
0271 /* Add padding and return the message digest. */
0272 static int rmd128_final(struct shash_desc *desc, u8 *out)
0273 {
0274     struct rmd128_ctx *rctx = shash_desc_ctx(desc);
0275     u32 i, index, padlen;
0276     __le64 bits;
0277     __le32 *dst = (__le32 *)out;
0278     static const u8 padding[64] = { 0x80, };
0279 
0280     bits = cpu_to_le64(rctx->byte_count << 3);
0281 
0282     /* Pad out to 56 mod 64 */
0283     index = rctx->byte_count & 0x3f;
0284     padlen = (index < 56) ? (56 - index) : ((64+56) - index);
0285     rmd128_update(desc, padding, padlen);
0286 
0287     /* Append length */
0288     rmd128_update(desc, (const u8 *)&bits, sizeof(bits));
0289 
0290     /* Store state in digest */
0291     for (i = 0; i < 4; i++)
0292         dst[i] = cpu_to_le32p(&rctx->state[i]);
0293 
0294     /* Wipe context */
0295     memset(rctx, 0, sizeof(*rctx));
0296 
0297     return 0;
0298 }
0299 
0300 static struct shash_alg alg = {
0301     .digestsize =   RMD128_DIGEST_SIZE,
0302     .init       =   rmd128_init,
0303     .update     =   rmd128_update,
0304     .final      =   rmd128_final,
0305     .descsize   =   sizeof(struct rmd128_ctx),
0306     .base       =   {
0307         .cra_name    =  "rmd128",
0308         .cra_flags   =  CRYPTO_ALG_TYPE_SHASH,
0309         .cra_blocksize   =  RMD128_BLOCK_SIZE,
0310         .cra_module  =  THIS_MODULE,
0311     }
0312 };
0313 
0314 static int __init rmd128_mod_init(void)
0315 {
0316     return crypto_register_shash(&alg);
0317 }
0318 
0319 static void __exit rmd128_mod_fini(void)
0320 {
0321     crypto_unregister_shash(&alg);
0322 }
0323 
0324 module_init(rmd128_mod_init);
0325 module_exit(rmd128_mod_fini);
0326 
0327 MODULE_LICENSE("GPL");
0328 MODULE_AUTHOR("Adrian-Ken Rueegsegger <ken@codelabs.ch>");
0329 MODULE_DESCRIPTION("RIPEMD-128 Message Digest");
0330 MODULE_ALIAS_CRYPTO("rmd128");