Back to home page

LXR

 
 

    


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