Back to home page

LXR

 
 

    


0001 /*
0002  * Cryptographic API.
0003  *
0004  * RIPEMD-160 - 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 rmd160_ctx {
0026     u64 byte_count;
0027     u32 state[5];
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 K5  RMD_K5
0036 #define KK1 RMD_K6
0037 #define KK2 RMD_K7
0038 #define KK3 RMD_K8
0039 #define KK4 RMD_K9
0040 #define KK5 RMD_K1
0041 
0042 #define F1(x, y, z) (x ^ y ^ z)     /* XOR */
0043 #define F2(x, y, z) (z ^ (x & (y ^ z))) /* x ? y : z */
0044 #define F3(x, y, z) ((x | ~y) ^ z)
0045 #define F4(x, y, z) (y ^ (z & (x ^ y))) /* z ? x : y */
0046 #define F5(x, y, z) (x ^ (y | ~z))
0047 
0048 #define ROUND(a, b, c, d, e, f, k, x, s)  { \
0049     (a) += f((b), (c), (d)) + le32_to_cpup(&(x)) + (k); \
0050     (a) = rol32((a), (s)) + (e); \
0051     (c) = rol32((c), 10); \
0052 }
0053 
0054 static void rmd160_transform(u32 *state, const __le32 *in)
0055 {
0056     u32 aa, bb, cc, dd, ee, aaa, bbb, ccc, ddd, eee;
0057 
0058     /* Initialize left lane */
0059     aa = state[0];
0060     bb = state[1];
0061     cc = state[2];
0062     dd = state[3];
0063     ee = state[4];
0064 
0065     /* Initialize right lane */
0066     aaa = state[0];
0067     bbb = state[1];
0068     ccc = state[2];
0069     ddd = state[3];
0070     eee = state[4];
0071 
0072     /* round 1: left lane */
0073     ROUND(aa, bb, cc, dd, ee, F1, K1, in[0],  11);
0074     ROUND(ee, aa, bb, cc, dd, F1, K1, in[1],  14);
0075     ROUND(dd, ee, aa, bb, cc, F1, K1, in[2],  15);
0076     ROUND(cc, dd, ee, aa, bb, F1, K1, in[3],  12);
0077     ROUND(bb, cc, dd, ee, aa, F1, K1, in[4],   5);
0078     ROUND(aa, bb, cc, dd, ee, F1, K1, in[5],   8);
0079     ROUND(ee, aa, bb, cc, dd, F1, K1, in[6],   7);
0080     ROUND(dd, ee, aa, bb, cc, F1, K1, in[7],   9);
0081     ROUND(cc, dd, ee, aa, bb, F1, K1, in[8],  11);
0082     ROUND(bb, cc, dd, ee, aa, F1, K1, in[9],  13);
0083     ROUND(aa, bb, cc, dd, ee, F1, K1, in[10], 14);
0084     ROUND(ee, aa, bb, cc, dd, F1, K1, in[11], 15);
0085     ROUND(dd, ee, aa, bb, cc, F1, K1, in[12],  6);
0086     ROUND(cc, dd, ee, aa, bb, F1, K1, in[13],  7);
0087     ROUND(bb, cc, dd, ee, aa, F1, K1, in[14],  9);
0088     ROUND(aa, bb, cc, dd, ee, F1, K1, in[15],  8);
0089 
0090     /* round 2: left lane" */
0091     ROUND(ee, aa, bb, cc, dd, F2, K2, in[7],   7);
0092     ROUND(dd, ee, aa, bb, cc, F2, K2, in[4],   6);
0093     ROUND(cc, dd, ee, aa, bb, F2, K2, in[13],  8);
0094     ROUND(bb, cc, dd, ee, aa, F2, K2, in[1],  13);
0095     ROUND(aa, bb, cc, dd, ee, F2, K2, in[10], 11);
0096     ROUND(ee, aa, bb, cc, dd, F2, K2, in[6],   9);
0097     ROUND(dd, ee, aa, bb, cc, F2, K2, in[15],  7);
0098     ROUND(cc, dd, ee, aa, bb, F2, K2, in[3],  15);
0099     ROUND(bb, cc, dd, ee, aa, F2, K2, in[12],  7);
0100     ROUND(aa, bb, cc, dd, ee, F2, K2, in[0],  12);
0101     ROUND(ee, aa, bb, cc, dd, F2, K2, in[9],  15);
0102     ROUND(dd, ee, aa, bb, cc, F2, K2, in[5],   9);
0103     ROUND(cc, dd, ee, aa, bb, F2, K2, in[2],  11);
0104     ROUND(bb, cc, dd, ee, aa, F2, K2, in[14],  7);
0105     ROUND(aa, bb, cc, dd, ee, F2, K2, in[11], 13);
0106     ROUND(ee, aa, bb, cc, dd, F2, K2, in[8],  12);
0107 
0108     /* round 3: left lane" */
0109     ROUND(dd, ee, aa, bb, cc, F3, K3, in[3],  11);
0110     ROUND(cc, dd, ee, aa, bb, F3, K3, in[10], 13);
0111     ROUND(bb, cc, dd, ee, aa, F3, K3, in[14],  6);
0112     ROUND(aa, bb, cc, dd, ee, F3, K3, in[4],   7);
0113     ROUND(ee, aa, bb, cc, dd, F3, K3, in[9],  14);
0114     ROUND(dd, ee, aa, bb, cc, F3, K3, in[15],  9);
0115     ROUND(cc, dd, ee, aa, bb, F3, K3, in[8],  13);
0116     ROUND(bb, cc, dd, ee, aa, F3, K3, in[1],  15);
0117     ROUND(aa, bb, cc, dd, ee, F3, K3, in[2],  14);
0118     ROUND(ee, aa, bb, cc, dd, F3, K3, in[7],   8);
0119     ROUND(dd, ee, aa, bb, cc, F3, K3, in[0],  13);
0120     ROUND(cc, dd, ee, aa, bb, F3, K3, in[6],   6);
0121     ROUND(bb, cc, dd, ee, aa, F3, K3, in[13],  5);
0122     ROUND(aa, bb, cc, dd, ee, F3, K3, in[11], 12);
0123     ROUND(ee, aa, bb, cc, dd, F3, K3, in[5],   7);
0124     ROUND(dd, ee, aa, bb, cc, F3, K3, in[12],  5);
0125 
0126     /* round 4: left lane" */
0127     ROUND(cc, dd, ee, aa, bb, F4, K4, in[1],  11);
0128     ROUND(bb, cc, dd, ee, aa, F4, K4, in[9],  12);
0129     ROUND(aa, bb, cc, dd, ee, F4, K4, in[11], 14);
0130     ROUND(ee, aa, bb, cc, dd, F4, K4, in[10], 15);
0131     ROUND(dd, ee, aa, bb, cc, F4, K4, in[0],  14);
0132     ROUND(cc, dd, ee, aa, bb, F4, K4, in[8],  15);
0133     ROUND(bb, cc, dd, ee, aa, F4, K4, in[12],  9);
0134     ROUND(aa, bb, cc, dd, ee, F4, K4, in[4],   8);
0135     ROUND(ee, aa, bb, cc, dd, F4, K4, in[13],  9);
0136     ROUND(dd, ee, aa, bb, cc, F4, K4, in[3],  14);
0137     ROUND(cc, dd, ee, aa, bb, F4, K4, in[7],   5);
0138     ROUND(bb, cc, dd, ee, aa, F4, K4, in[15],  6);
0139     ROUND(aa, bb, cc, dd, ee, F4, K4, in[14],  8);
0140     ROUND(ee, aa, bb, cc, dd, F4, K4, in[5],   6);
0141     ROUND(dd, ee, aa, bb, cc, F4, K4, in[6],   5);
0142     ROUND(cc, dd, ee, aa, bb, F4, K4, in[2],  12);
0143 
0144     /* round 5: left lane" */
0145     ROUND(bb, cc, dd, ee, aa, F5, K5, in[4],   9);
0146     ROUND(aa, bb, cc, dd, ee, F5, K5, in[0],  15);
0147     ROUND(ee, aa, bb, cc, dd, F5, K5, in[5],   5);
0148     ROUND(dd, ee, aa, bb, cc, F5, K5, in[9],  11);
0149     ROUND(cc, dd, ee, aa, bb, F5, K5, in[7],   6);
0150     ROUND(bb, cc, dd, ee, aa, F5, K5, in[12],  8);
0151     ROUND(aa, bb, cc, dd, ee, F5, K5, in[2],  13);
0152     ROUND(ee, aa, bb, cc, dd, F5, K5, in[10], 12);
0153     ROUND(dd, ee, aa, bb, cc, F5, K5, in[14],  5);
0154     ROUND(cc, dd, ee, aa, bb, F5, K5, in[1],  12);
0155     ROUND(bb, cc, dd, ee, aa, F5, K5, in[3],  13);
0156     ROUND(aa, bb, cc, dd, ee, F5, K5, in[8],  14);
0157     ROUND(ee, aa, bb, cc, dd, F5, K5, in[11], 11);
0158     ROUND(dd, ee, aa, bb, cc, F5, K5, in[6],   8);
0159     ROUND(cc, dd, ee, aa, bb, F5, K5, in[15],  5);
0160     ROUND(bb, cc, dd, ee, aa, F5, K5, in[13],  6);
0161 
0162     /* round 1: right lane */
0163     ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[5],   8);
0164     ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[14],  9);
0165     ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[7],   9);
0166     ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[0],  11);
0167     ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[9],  13);
0168     ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[2],  15);
0169     ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[11], 15);
0170     ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[4],   5);
0171     ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[13],  7);
0172     ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[6],   7);
0173     ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[15],  8);
0174     ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[8],  11);
0175     ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[1],  14);
0176     ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[10], 14);
0177     ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[3],  12);
0178     ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[12],  6);
0179 
0180     /* round 2: right lane */
0181     ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[6],   9);
0182     ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[11], 13);
0183     ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[3],  15);
0184     ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[7],   7);
0185     ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[0],  12);
0186     ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[13],  8);
0187     ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[5],   9);
0188     ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[10], 11);
0189     ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[14],  7);
0190     ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[15],  7);
0191     ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[8],  12);
0192     ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[12],  7);
0193     ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[4],   6);
0194     ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[9],  15);
0195     ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[1],  13);
0196     ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[2],  11);
0197 
0198     /* round 3: right lane */
0199     ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[15],  9);
0200     ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[5],   7);
0201     ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[1],  15);
0202     ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[3],  11);
0203     ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[7],   8);
0204     ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[14],  6);
0205     ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[6],   6);
0206     ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[9],  14);
0207     ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[11], 12);
0208     ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[8],  13);
0209     ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[12],  5);
0210     ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[2],  14);
0211     ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[10], 13);
0212     ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[0],  13);
0213     ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[4],   7);
0214     ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[13],  5);
0215 
0216     /* round 4: right lane */
0217     ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[8],  15);
0218     ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[6],   5);
0219     ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[4],   8);
0220     ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[1],  11);
0221     ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[3],  14);
0222     ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[11], 14);
0223     ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[15],  6);
0224     ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[0],  14);
0225     ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[5],   6);
0226     ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[12],  9);
0227     ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[2],  12);
0228     ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[13],  9);
0229     ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[9],  12);
0230     ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[7],   5);
0231     ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[10], 15);
0232     ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[14],  8);
0233 
0234     /* round 5: right lane */
0235     ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[12],  8);
0236     ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[15],  5);
0237     ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[10], 12);
0238     ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[4],   9);
0239     ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[1],  12);
0240     ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[5],   5);
0241     ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[8],  14);
0242     ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[7],   6);
0243     ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[6],   8);
0244     ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[2],  13);
0245     ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[13],  6);
0246     ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[14],  5);
0247     ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[0],  15);
0248     ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[3],  13);
0249     ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[9],  11);
0250     ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[11], 11);
0251 
0252     /* combine results */
0253     ddd += cc + state[1];       /* final result for state[0] */
0254     state[1] = state[2] + dd + eee;
0255     state[2] = state[3] + ee + aaa;
0256     state[3] = state[4] + aa + bbb;
0257     state[4] = state[0] + bb + ccc;
0258     state[0] = ddd;
0259 
0260     return;
0261 }
0262 
0263 static int rmd160_init(struct shash_desc *desc)
0264 {
0265     struct rmd160_ctx *rctx = shash_desc_ctx(desc);
0266 
0267     rctx->byte_count = 0;
0268 
0269     rctx->state[0] = RMD_H0;
0270     rctx->state[1] = RMD_H1;
0271     rctx->state[2] = RMD_H2;
0272     rctx->state[3] = RMD_H3;
0273     rctx->state[4] = RMD_H4;
0274 
0275     memset(rctx->buffer, 0, sizeof(rctx->buffer));
0276 
0277     return 0;
0278 }
0279 
0280 static int rmd160_update(struct shash_desc *desc, const u8 *data,
0281              unsigned int len)
0282 {
0283     struct rmd160_ctx *rctx = shash_desc_ctx(desc);
0284     const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
0285 
0286     rctx->byte_count += len;
0287 
0288     /* Enough space in buffer? If so copy and we're done */
0289     if (avail > len) {
0290         memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
0291                data, len);
0292         goto out;
0293     }
0294 
0295     memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
0296            data, avail);
0297 
0298     rmd160_transform(rctx->state, rctx->buffer);
0299     data += avail;
0300     len -= avail;
0301 
0302     while (len >= sizeof(rctx->buffer)) {
0303         memcpy(rctx->buffer, data, sizeof(rctx->buffer));
0304         rmd160_transform(rctx->state, rctx->buffer);
0305         data += sizeof(rctx->buffer);
0306         len -= sizeof(rctx->buffer);
0307     }
0308 
0309     memcpy(rctx->buffer, data, len);
0310 
0311 out:
0312     return 0;
0313 }
0314 
0315 /* Add padding and return the message digest. */
0316 static int rmd160_final(struct shash_desc *desc, u8 *out)
0317 {
0318     struct rmd160_ctx *rctx = shash_desc_ctx(desc);
0319     u32 i, index, padlen;
0320     __le64 bits;
0321     __le32 *dst = (__le32 *)out;
0322     static const u8 padding[64] = { 0x80, };
0323 
0324     bits = cpu_to_le64(rctx->byte_count << 3);
0325 
0326     /* Pad out to 56 mod 64 */
0327     index = rctx->byte_count & 0x3f;
0328     padlen = (index < 56) ? (56 - index) : ((64+56) - index);
0329     rmd160_update(desc, padding, padlen);
0330 
0331     /* Append length */
0332     rmd160_update(desc, (const u8 *)&bits, sizeof(bits));
0333 
0334     /* Store state in digest */
0335     for (i = 0; i < 5; i++)
0336         dst[i] = cpu_to_le32p(&rctx->state[i]);
0337 
0338     /* Wipe context */
0339     memset(rctx, 0, sizeof(*rctx));
0340 
0341     return 0;
0342 }
0343 
0344 static struct shash_alg alg = {
0345     .digestsize =   RMD160_DIGEST_SIZE,
0346     .init       =   rmd160_init,
0347     .update     =   rmd160_update,
0348     .final      =   rmd160_final,
0349     .descsize   =   sizeof(struct rmd160_ctx),
0350     .base       =   {
0351         .cra_name    =  "rmd160",
0352         .cra_flags   =  CRYPTO_ALG_TYPE_SHASH,
0353         .cra_blocksize   =  RMD160_BLOCK_SIZE,
0354         .cra_module  =  THIS_MODULE,
0355     }
0356 };
0357 
0358 static int __init rmd160_mod_init(void)
0359 {
0360     return crypto_register_shash(&alg);
0361 }
0362 
0363 static void __exit rmd160_mod_fini(void)
0364 {
0365     crypto_unregister_shash(&alg);
0366 }
0367 
0368 module_init(rmd160_mod_init);
0369 module_exit(rmd160_mod_fini);
0370 
0371 MODULE_LICENSE("GPL");
0372 MODULE_AUTHOR("Adrian-Ken Rueegsegger <ken@codelabs.ch>");
0373 MODULE_DESCRIPTION("RIPEMD-160 Message Digest");
0374 MODULE_ALIAS_CRYPTO("rmd160");