Back to home page

LXR

 
 

    


0001 /*
0002  * Cryptographic API.
0003  *
0004  * RIPEMD-320 - 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 rmd320_ctx {
0026     u64 byte_count;
0027     u32 state[10];
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 rmd320_transform(u32 *state, const __le32 *in)
0055 {
0056     u32 aa, bb, cc, dd, ee, aaa, bbb, ccc, ddd, eee, tmp;
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[5];
0067     bbb = state[6];
0068     ccc = state[7];
0069     ddd = state[8];
0070     eee = state[9];
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 1: right lane */
0091     ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[5],   8);
0092     ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[14],  9);
0093     ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[7],   9);
0094     ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[0],  11);
0095     ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[9],  13);
0096     ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[2],  15);
0097     ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[11], 15);
0098     ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[4],   5);
0099     ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[13],  7);
0100     ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[6],   7);
0101     ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[15],  8);
0102     ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[8],  11);
0103     ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[1],  14);
0104     ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[10], 14);
0105     ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[3],  12);
0106     ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[12],  6);
0107 
0108     /* Swap contents of "a" registers */
0109     tmp = aa; aa = aaa; aaa = tmp;
0110 
0111     /* round 2: left lane" */
0112     ROUND(ee, aa, bb, cc, dd, F2, K2, in[7],   7);
0113     ROUND(dd, ee, aa, bb, cc, F2, K2, in[4],   6);
0114     ROUND(cc, dd, ee, aa, bb, F2, K2, in[13],  8);
0115     ROUND(bb, cc, dd, ee, aa, F2, K2, in[1],  13);
0116     ROUND(aa, bb, cc, dd, ee, F2, K2, in[10], 11);
0117     ROUND(ee, aa, bb, cc, dd, F2, K2, in[6],   9);
0118     ROUND(dd, ee, aa, bb, cc, F2, K2, in[15],  7);
0119     ROUND(cc, dd, ee, aa, bb, F2, K2, in[3],  15);
0120     ROUND(bb, cc, dd, ee, aa, F2, K2, in[12],  7);
0121     ROUND(aa, bb, cc, dd, ee, F2, K2, in[0],  12);
0122     ROUND(ee, aa, bb, cc, dd, F2, K2, in[9],  15);
0123     ROUND(dd, ee, aa, bb, cc, F2, K2, in[5],   9);
0124     ROUND(cc, dd, ee, aa, bb, F2, K2, in[2],  11);
0125     ROUND(bb, cc, dd, ee, aa, F2, K2, in[14],  7);
0126     ROUND(aa, bb, cc, dd, ee, F2, K2, in[11], 13);
0127     ROUND(ee, aa, bb, cc, dd, F2, K2, in[8],  12);
0128 
0129     /* round 2: right lane */
0130     ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[6],   9);
0131     ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[11], 13);
0132     ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[3],  15);
0133     ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[7],   7);
0134     ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[0],  12);
0135     ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[13],  8);
0136     ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[5],   9);
0137     ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[10], 11);
0138     ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[14],  7);
0139     ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[15],  7);
0140     ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[8],  12);
0141     ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[12],  7);
0142     ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[4],   6);
0143     ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[9],  15);
0144     ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[1],  13);
0145     ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[2],  11);
0146 
0147     /* Swap contents of "b" registers */
0148     tmp = bb; bb = bbb; bbb = tmp;
0149 
0150     /* round 3: left lane" */
0151     ROUND(dd, ee, aa, bb, cc, F3, K3, in[3],  11);
0152     ROUND(cc, dd, ee, aa, bb, F3, K3, in[10], 13);
0153     ROUND(bb, cc, dd, ee, aa, F3, K3, in[14],  6);
0154     ROUND(aa, bb, cc, dd, ee, F3, K3, in[4],   7);
0155     ROUND(ee, aa, bb, cc, dd, F3, K3, in[9],  14);
0156     ROUND(dd, ee, aa, bb, cc, F3, K3, in[15],  9);
0157     ROUND(cc, dd, ee, aa, bb, F3, K3, in[8],  13);
0158     ROUND(bb, cc, dd, ee, aa, F3, K3, in[1],  15);
0159     ROUND(aa, bb, cc, dd, ee, F3, K3, in[2],  14);
0160     ROUND(ee, aa, bb, cc, dd, F3, K3, in[7],   8);
0161     ROUND(dd, ee, aa, bb, cc, F3, K3, in[0],  13);
0162     ROUND(cc, dd, ee, aa, bb, F3, K3, in[6],   6);
0163     ROUND(bb, cc, dd, ee, aa, F3, K3, in[13],  5);
0164     ROUND(aa, bb, cc, dd, ee, F3, K3, in[11], 12);
0165     ROUND(ee, aa, bb, cc, dd, F3, K3, in[5],   7);
0166     ROUND(dd, ee, aa, bb, cc, F3, K3, in[12],  5);
0167 
0168     /* round 3: right lane */
0169     ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[15],  9);
0170     ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[5],   7);
0171     ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[1],  15);
0172     ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[3],  11);
0173     ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[7],   8);
0174     ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[14],  6);
0175     ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[6],   6);
0176     ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[9],  14);
0177     ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[11], 12);
0178     ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[8],  13);
0179     ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[12],  5);
0180     ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[2],  14);
0181     ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[10], 13);
0182     ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[0],  13);
0183     ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[4],   7);
0184     ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[13],  5);
0185 
0186     /* Swap contents of "c" registers */
0187     tmp = cc; cc = ccc; ccc = tmp;
0188 
0189     /* round 4: left lane" */
0190     ROUND(cc, dd, ee, aa, bb, F4, K4, in[1],  11);
0191     ROUND(bb, cc, dd, ee, aa, F4, K4, in[9],  12);
0192     ROUND(aa, bb, cc, dd, ee, F4, K4, in[11], 14);
0193     ROUND(ee, aa, bb, cc, dd, F4, K4, in[10], 15);
0194     ROUND(dd, ee, aa, bb, cc, F4, K4, in[0],  14);
0195     ROUND(cc, dd, ee, aa, bb, F4, K4, in[8],  15);
0196     ROUND(bb, cc, dd, ee, aa, F4, K4, in[12],  9);
0197     ROUND(aa, bb, cc, dd, ee, F4, K4, in[4],   8);
0198     ROUND(ee, aa, bb, cc, dd, F4, K4, in[13],  9);
0199     ROUND(dd, ee, aa, bb, cc, F4, K4, in[3],  14);
0200     ROUND(cc, dd, ee, aa, bb, F4, K4, in[7],   5);
0201     ROUND(bb, cc, dd, ee, aa, F4, K4, in[15],  6);
0202     ROUND(aa, bb, cc, dd, ee, F4, K4, in[14],  8);
0203     ROUND(ee, aa, bb, cc, dd, F4, K4, in[5],   6);
0204     ROUND(dd, ee, aa, bb, cc, F4, K4, in[6],   5);
0205     ROUND(cc, dd, ee, aa, bb, F4, K4, in[2],  12);
0206 
0207     /* round 4: right lane */
0208     ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[8],  15);
0209     ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[6],   5);
0210     ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[4],   8);
0211     ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[1],  11);
0212     ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[3],  14);
0213     ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[11], 14);
0214     ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[15],  6);
0215     ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[0],  14);
0216     ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[5],   6);
0217     ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[12],  9);
0218     ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[2],  12);
0219     ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[13],  9);
0220     ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[9],  12);
0221     ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[7],   5);
0222     ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[10], 15);
0223     ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[14],  8);
0224 
0225     /* Swap contents of "d" registers */
0226     tmp = dd; dd = ddd; ddd = tmp;
0227 
0228     /* round 5: left lane" */
0229     ROUND(bb, cc, dd, ee, aa, F5, K5, in[4],   9);
0230     ROUND(aa, bb, cc, dd, ee, F5, K5, in[0],  15);
0231     ROUND(ee, aa, bb, cc, dd, F5, K5, in[5],   5);
0232     ROUND(dd, ee, aa, bb, cc, F5, K5, in[9],  11);
0233     ROUND(cc, dd, ee, aa, bb, F5, K5, in[7],   6);
0234     ROUND(bb, cc, dd, ee, aa, F5, K5, in[12],  8);
0235     ROUND(aa, bb, cc, dd, ee, F5, K5, in[2],  13);
0236     ROUND(ee, aa, bb, cc, dd, F5, K5, in[10], 12);
0237     ROUND(dd, ee, aa, bb, cc, F5, K5, in[14],  5);
0238     ROUND(cc, dd, ee, aa, bb, F5, K5, in[1],  12);
0239     ROUND(bb, cc, dd, ee, aa, F5, K5, in[3],  13);
0240     ROUND(aa, bb, cc, dd, ee, F5, K5, in[8],  14);
0241     ROUND(ee, aa, bb, cc, dd, F5, K5, in[11], 11);
0242     ROUND(dd, ee, aa, bb, cc, F5, K5, in[6],   8);
0243     ROUND(cc, dd, ee, aa, bb, F5, K5, in[15],  5);
0244     ROUND(bb, cc, dd, ee, aa, F5, K5, in[13],  6);
0245 
0246     /* round 5: right lane */
0247     ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[12],  8);
0248     ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[15],  5);
0249     ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[10], 12);
0250     ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[4],   9);
0251     ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[1],  12);
0252     ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[5],   5);
0253     ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[8],  14);
0254     ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[7],   6);
0255     ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[6],   8);
0256     ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[2],  13);
0257     ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[13],  6);
0258     ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[14],  5);
0259     ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[0],  15);
0260     ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[3],  13);
0261     ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[9],  11);
0262     ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[11], 11);
0263 
0264     /* Swap contents of "e" registers */
0265     tmp = ee; ee = eee; eee = tmp;
0266 
0267     /* combine results */
0268     state[0] += aa;
0269     state[1] += bb;
0270     state[2] += cc;
0271     state[3] += dd;
0272     state[4] += ee;
0273     state[5] += aaa;
0274     state[6] += bbb;
0275     state[7] += ccc;
0276     state[8] += ddd;
0277     state[9] += eee;
0278 
0279     return;
0280 }
0281 
0282 static int rmd320_init(struct shash_desc *desc)
0283 {
0284     struct rmd320_ctx *rctx = shash_desc_ctx(desc);
0285 
0286     rctx->byte_count = 0;
0287 
0288     rctx->state[0] = RMD_H0;
0289     rctx->state[1] = RMD_H1;
0290     rctx->state[2] = RMD_H2;
0291     rctx->state[3] = RMD_H3;
0292     rctx->state[4] = RMD_H4;
0293     rctx->state[5] = RMD_H5;
0294     rctx->state[6] = RMD_H6;
0295     rctx->state[7] = RMD_H7;
0296     rctx->state[8] = RMD_H8;
0297     rctx->state[9] = RMD_H9;
0298 
0299     memset(rctx->buffer, 0, sizeof(rctx->buffer));
0300 
0301     return 0;
0302 }
0303 
0304 static int rmd320_update(struct shash_desc *desc, const u8 *data,
0305              unsigned int len)
0306 {
0307     struct rmd320_ctx *rctx = shash_desc_ctx(desc);
0308     const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
0309 
0310     rctx->byte_count += len;
0311 
0312     /* Enough space in buffer? If so copy and we're done */
0313     if (avail > len) {
0314         memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
0315                data, len);
0316         goto out;
0317     }
0318 
0319     memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
0320            data, avail);
0321 
0322     rmd320_transform(rctx->state, rctx->buffer);
0323     data += avail;
0324     len -= avail;
0325 
0326     while (len >= sizeof(rctx->buffer)) {
0327         memcpy(rctx->buffer, data, sizeof(rctx->buffer));
0328         rmd320_transform(rctx->state, rctx->buffer);
0329         data += sizeof(rctx->buffer);
0330         len -= sizeof(rctx->buffer);
0331     }
0332 
0333     memcpy(rctx->buffer, data, len);
0334 
0335 out:
0336     return 0;
0337 }
0338 
0339 /* Add padding and return the message digest. */
0340 static int rmd320_final(struct shash_desc *desc, u8 *out)
0341 {
0342     struct rmd320_ctx *rctx = shash_desc_ctx(desc);
0343     u32 i, index, padlen;
0344     __le64 bits;
0345     __le32 *dst = (__le32 *)out;
0346     static const u8 padding[64] = { 0x80, };
0347 
0348     bits = cpu_to_le64(rctx->byte_count << 3);
0349 
0350     /* Pad out to 56 mod 64 */
0351     index = rctx->byte_count & 0x3f;
0352     padlen = (index < 56) ? (56 - index) : ((64+56) - index);
0353     rmd320_update(desc, padding, padlen);
0354 
0355     /* Append length */
0356     rmd320_update(desc, (const u8 *)&bits, sizeof(bits));
0357 
0358     /* Store state in digest */
0359     for (i = 0; i < 10; i++)
0360         dst[i] = cpu_to_le32p(&rctx->state[i]);
0361 
0362     /* Wipe context */
0363     memset(rctx, 0, sizeof(*rctx));
0364 
0365     return 0;
0366 }
0367 
0368 static struct shash_alg alg = {
0369     .digestsize =   RMD320_DIGEST_SIZE,
0370     .init       =   rmd320_init,
0371     .update     =   rmd320_update,
0372     .final      =   rmd320_final,
0373     .descsize   =   sizeof(struct rmd320_ctx),
0374     .base       =   {
0375         .cra_name    =  "rmd320",
0376         .cra_flags   =  CRYPTO_ALG_TYPE_SHASH,
0377         .cra_blocksize   =  RMD320_BLOCK_SIZE,
0378         .cra_module  =  THIS_MODULE,
0379     }
0380 };
0381 
0382 static int __init rmd320_mod_init(void)
0383 {
0384     return crypto_register_shash(&alg);
0385 }
0386 
0387 static void __exit rmd320_mod_fini(void)
0388 {
0389     crypto_unregister_shash(&alg);
0390 }
0391 
0392 module_init(rmd320_mod_init);
0393 module_exit(rmd320_mod_fini);
0394 
0395 MODULE_LICENSE("GPL");
0396 MODULE_AUTHOR("Adrian-Ken Rueegsegger <ken@codelabs.ch>");
0397 MODULE_DESCRIPTION("RIPEMD-320 Message Digest");
0398 MODULE_ALIAS_CRYPTO("rmd320");