Adrian-Ken Rueegsegger | c555c28 | 2008-05-09 21:27:02 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Cryptographic API. |
| 3 | * |
| 4 | * RIPEMD-320 - RACE Integrity Primitives Evaluation Message Digest. |
| 5 | * |
| 6 | * Based on the reference implementation by Antoon Bosselaers, ESAT-COSIC |
| 7 | * |
Adrian-Ken Rueegsegger | 3181c22 | 2011-01-04 15:35:51 +1100 | [diff] [blame] | 8 | * Copyright (c) 2008 Adrian-Ken Rueegsegger <ken@codelabs.ch> |
Adrian-Ken Rueegsegger | c555c28 | 2008-05-09 21:27:02 +0800 | [diff] [blame] | 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify it |
| 11 | * under the terms of the GNU General Public License as published by the Free |
| 12 | * Software Foundation; either version 2 of the License, or (at your option) |
| 13 | * any later version. |
| 14 | * |
| 15 | */ |
Herbert Xu | 3b8efb4 | 2008-11-08 10:11:09 +0800 | [diff] [blame] | 16 | #include <crypto/internal/hash.h> |
Adrian-Ken Rueegsegger | c555c28 | 2008-05-09 21:27:02 +0800 | [diff] [blame] | 17 | #include <linux/init.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/mm.h> |
Adrian-Ken Rueegsegger | c555c28 | 2008-05-09 21:27:02 +0800 | [diff] [blame] | 20 | #include <linux/types.h> |
| 21 | #include <asm/byteorder.h> |
| 22 | |
| 23 | #include "ripemd.h" |
| 24 | |
| 25 | struct rmd320_ctx { |
| 26 | u64 byte_count; |
| 27 | u32 state[10]; |
Harvey Harrison | caee168 | 2008-07-04 19:48:58 +0800 | [diff] [blame] | 28 | __le32 buffer[16]; |
Adrian-Ken Rueegsegger | c555c28 | 2008-05-09 21:27:02 +0800 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | #define K1 RMD_K1 |
| 32 | #define K2 RMD_K2 |
| 33 | #define K3 RMD_K3 |
| 34 | #define K4 RMD_K4 |
| 35 | #define K5 RMD_K5 |
| 36 | #define KK1 RMD_K6 |
| 37 | #define KK2 RMD_K7 |
| 38 | #define KK3 RMD_K8 |
| 39 | #define KK4 RMD_K9 |
| 40 | #define KK5 RMD_K1 |
| 41 | |
| 42 | #define F1(x, y, z) (x ^ y ^ z) /* XOR */ |
| 43 | #define F2(x, y, z) (z ^ (x & (y ^ z))) /* x ? y : z */ |
| 44 | #define F3(x, y, z) ((x | ~y) ^ z) |
| 45 | #define F4(x, y, z) (y ^ (z & (x ^ y))) /* z ? x : y */ |
| 46 | #define F5(x, y, z) (x ^ (y | ~z)) |
| 47 | |
| 48 | #define ROUND(a, b, c, d, e, f, k, x, s) { \ |
Herbert Xu | 5cdcc22 | 2008-06-02 21:30:38 +1000 | [diff] [blame] | 49 | (a) += f((b), (c), (d)) + le32_to_cpup(&(x)) + (k); \ |
Adrian-Ken Rueegsegger | c555c28 | 2008-05-09 21:27:02 +0800 | [diff] [blame] | 50 | (a) = rol32((a), (s)) + (e); \ |
| 51 | (c) = rol32((c), 10); \ |
| 52 | } |
| 53 | |
Harvey Harrison | caee168 | 2008-07-04 19:48:58 +0800 | [diff] [blame] | 54 | static void rmd320_transform(u32 *state, const __le32 *in) |
Adrian-Ken Rueegsegger | c555c28 | 2008-05-09 21:27:02 +0800 | [diff] [blame] | 55 | { |
Gustavo A. R. Silva | a478908 | 2018-07-18 12:19:08 -0500 | [diff] [blame] | 56 | u32 aa, bb, cc, dd, ee, aaa, bbb, ccc, ddd, eee; |
Adrian-Ken Rueegsegger | c555c28 | 2008-05-09 21:27:02 +0800 | [diff] [blame] | 57 | |
| 58 | /* Initialize left lane */ |
| 59 | aa = state[0]; |
| 60 | bb = state[1]; |
| 61 | cc = state[2]; |
| 62 | dd = state[3]; |
| 63 | ee = state[4]; |
| 64 | |
| 65 | /* Initialize right lane */ |
| 66 | aaa = state[5]; |
| 67 | bbb = state[6]; |
| 68 | ccc = state[7]; |
| 69 | ddd = state[8]; |
| 70 | eee = state[9]; |
| 71 | |
| 72 | /* round 1: left lane */ |
| 73 | ROUND(aa, bb, cc, dd, ee, F1, K1, in[0], 11); |
| 74 | ROUND(ee, aa, bb, cc, dd, F1, K1, in[1], 14); |
| 75 | ROUND(dd, ee, aa, bb, cc, F1, K1, in[2], 15); |
| 76 | ROUND(cc, dd, ee, aa, bb, F1, K1, in[3], 12); |
| 77 | ROUND(bb, cc, dd, ee, aa, F1, K1, in[4], 5); |
| 78 | ROUND(aa, bb, cc, dd, ee, F1, K1, in[5], 8); |
| 79 | ROUND(ee, aa, bb, cc, dd, F1, K1, in[6], 7); |
| 80 | ROUND(dd, ee, aa, bb, cc, F1, K1, in[7], 9); |
| 81 | ROUND(cc, dd, ee, aa, bb, F1, K1, in[8], 11); |
| 82 | ROUND(bb, cc, dd, ee, aa, F1, K1, in[9], 13); |
| 83 | ROUND(aa, bb, cc, dd, ee, F1, K1, in[10], 14); |
| 84 | ROUND(ee, aa, bb, cc, dd, F1, K1, in[11], 15); |
| 85 | ROUND(dd, ee, aa, bb, cc, F1, K1, in[12], 6); |
| 86 | ROUND(cc, dd, ee, aa, bb, F1, K1, in[13], 7); |
| 87 | ROUND(bb, cc, dd, ee, aa, F1, K1, in[14], 9); |
| 88 | ROUND(aa, bb, cc, dd, ee, F1, K1, in[15], 8); |
| 89 | |
| 90 | /* round 1: right lane */ |
| 91 | ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[5], 8); |
| 92 | ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[14], 9); |
| 93 | ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[7], 9); |
| 94 | ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[0], 11); |
| 95 | ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[9], 13); |
| 96 | ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[2], 15); |
| 97 | ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[11], 15); |
| 98 | ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[4], 5); |
| 99 | ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[13], 7); |
| 100 | ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[6], 7); |
| 101 | ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[15], 8); |
| 102 | ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[8], 11); |
| 103 | ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[1], 14); |
| 104 | ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[10], 14); |
| 105 | ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[3], 12); |
| 106 | ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[12], 6); |
| 107 | |
| 108 | /* Swap contents of "a" registers */ |
Gustavo A. R. Silva | a478908 | 2018-07-18 12:19:08 -0500 | [diff] [blame] | 109 | swap(aa, aaa); |
Adrian-Ken Rueegsegger | c555c28 | 2008-05-09 21:27:02 +0800 | [diff] [blame] | 110 | |
| 111 | /* round 2: left lane" */ |
| 112 | ROUND(ee, aa, bb, cc, dd, F2, K2, in[7], 7); |
| 113 | ROUND(dd, ee, aa, bb, cc, F2, K2, in[4], 6); |
| 114 | ROUND(cc, dd, ee, aa, bb, F2, K2, in[13], 8); |
| 115 | ROUND(bb, cc, dd, ee, aa, F2, K2, in[1], 13); |
| 116 | ROUND(aa, bb, cc, dd, ee, F2, K2, in[10], 11); |
| 117 | ROUND(ee, aa, bb, cc, dd, F2, K2, in[6], 9); |
| 118 | ROUND(dd, ee, aa, bb, cc, F2, K2, in[15], 7); |
| 119 | ROUND(cc, dd, ee, aa, bb, F2, K2, in[3], 15); |
| 120 | ROUND(bb, cc, dd, ee, aa, F2, K2, in[12], 7); |
| 121 | ROUND(aa, bb, cc, dd, ee, F2, K2, in[0], 12); |
| 122 | ROUND(ee, aa, bb, cc, dd, F2, K2, in[9], 15); |
| 123 | ROUND(dd, ee, aa, bb, cc, F2, K2, in[5], 9); |
| 124 | ROUND(cc, dd, ee, aa, bb, F2, K2, in[2], 11); |
| 125 | ROUND(bb, cc, dd, ee, aa, F2, K2, in[14], 7); |
| 126 | ROUND(aa, bb, cc, dd, ee, F2, K2, in[11], 13); |
| 127 | ROUND(ee, aa, bb, cc, dd, F2, K2, in[8], 12); |
| 128 | |
| 129 | /* round 2: right lane */ |
| 130 | ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[6], 9); |
| 131 | ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[11], 13); |
| 132 | ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[3], 15); |
| 133 | ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[7], 7); |
| 134 | ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[0], 12); |
| 135 | ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[13], 8); |
| 136 | ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[5], 9); |
| 137 | ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[10], 11); |
| 138 | ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[14], 7); |
| 139 | ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[15], 7); |
| 140 | ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[8], 12); |
| 141 | ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[12], 7); |
| 142 | ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[4], 6); |
| 143 | ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[9], 15); |
| 144 | ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[1], 13); |
| 145 | ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[2], 11); |
| 146 | |
| 147 | /* Swap contents of "b" registers */ |
Gustavo A. R. Silva | a478908 | 2018-07-18 12:19:08 -0500 | [diff] [blame] | 148 | swap(bb, bbb); |
Adrian-Ken Rueegsegger | c555c28 | 2008-05-09 21:27:02 +0800 | [diff] [blame] | 149 | |
| 150 | /* round 3: left lane" */ |
| 151 | ROUND(dd, ee, aa, bb, cc, F3, K3, in[3], 11); |
| 152 | ROUND(cc, dd, ee, aa, bb, F3, K3, in[10], 13); |
| 153 | ROUND(bb, cc, dd, ee, aa, F3, K3, in[14], 6); |
| 154 | ROUND(aa, bb, cc, dd, ee, F3, K3, in[4], 7); |
| 155 | ROUND(ee, aa, bb, cc, dd, F3, K3, in[9], 14); |
| 156 | ROUND(dd, ee, aa, bb, cc, F3, K3, in[15], 9); |
| 157 | ROUND(cc, dd, ee, aa, bb, F3, K3, in[8], 13); |
| 158 | ROUND(bb, cc, dd, ee, aa, F3, K3, in[1], 15); |
| 159 | ROUND(aa, bb, cc, dd, ee, F3, K3, in[2], 14); |
| 160 | ROUND(ee, aa, bb, cc, dd, F3, K3, in[7], 8); |
| 161 | ROUND(dd, ee, aa, bb, cc, F3, K3, in[0], 13); |
| 162 | ROUND(cc, dd, ee, aa, bb, F3, K3, in[6], 6); |
| 163 | ROUND(bb, cc, dd, ee, aa, F3, K3, in[13], 5); |
| 164 | ROUND(aa, bb, cc, dd, ee, F3, K3, in[11], 12); |
| 165 | ROUND(ee, aa, bb, cc, dd, F3, K3, in[5], 7); |
| 166 | ROUND(dd, ee, aa, bb, cc, F3, K3, in[12], 5); |
| 167 | |
| 168 | /* round 3: right lane */ |
| 169 | ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[15], 9); |
| 170 | ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[5], 7); |
| 171 | ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[1], 15); |
| 172 | ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[3], 11); |
| 173 | ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[7], 8); |
| 174 | ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[14], 6); |
| 175 | ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[6], 6); |
| 176 | ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[9], 14); |
| 177 | ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[11], 12); |
| 178 | ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[8], 13); |
| 179 | ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[12], 5); |
| 180 | ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[2], 14); |
| 181 | ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[10], 13); |
| 182 | ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[0], 13); |
| 183 | ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[4], 7); |
| 184 | ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[13], 5); |
| 185 | |
| 186 | /* Swap contents of "c" registers */ |
Gustavo A. R. Silva | a478908 | 2018-07-18 12:19:08 -0500 | [diff] [blame] | 187 | swap(cc, ccc); |
Adrian-Ken Rueegsegger | c555c28 | 2008-05-09 21:27:02 +0800 | [diff] [blame] | 188 | |
| 189 | /* round 4: left lane" */ |
| 190 | ROUND(cc, dd, ee, aa, bb, F4, K4, in[1], 11); |
| 191 | ROUND(bb, cc, dd, ee, aa, F4, K4, in[9], 12); |
| 192 | ROUND(aa, bb, cc, dd, ee, F4, K4, in[11], 14); |
| 193 | ROUND(ee, aa, bb, cc, dd, F4, K4, in[10], 15); |
| 194 | ROUND(dd, ee, aa, bb, cc, F4, K4, in[0], 14); |
| 195 | ROUND(cc, dd, ee, aa, bb, F4, K4, in[8], 15); |
| 196 | ROUND(bb, cc, dd, ee, aa, F4, K4, in[12], 9); |
| 197 | ROUND(aa, bb, cc, dd, ee, F4, K4, in[4], 8); |
| 198 | ROUND(ee, aa, bb, cc, dd, F4, K4, in[13], 9); |
| 199 | ROUND(dd, ee, aa, bb, cc, F4, K4, in[3], 14); |
| 200 | ROUND(cc, dd, ee, aa, bb, F4, K4, in[7], 5); |
| 201 | ROUND(bb, cc, dd, ee, aa, F4, K4, in[15], 6); |
| 202 | ROUND(aa, bb, cc, dd, ee, F4, K4, in[14], 8); |
| 203 | ROUND(ee, aa, bb, cc, dd, F4, K4, in[5], 6); |
| 204 | ROUND(dd, ee, aa, bb, cc, F4, K4, in[6], 5); |
| 205 | ROUND(cc, dd, ee, aa, bb, F4, K4, in[2], 12); |
| 206 | |
| 207 | /* round 4: right lane */ |
| 208 | ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[8], 15); |
| 209 | ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[6], 5); |
| 210 | ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[4], 8); |
| 211 | ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[1], 11); |
| 212 | ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[3], 14); |
| 213 | ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[11], 14); |
| 214 | ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[15], 6); |
| 215 | ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[0], 14); |
| 216 | ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[5], 6); |
| 217 | ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[12], 9); |
| 218 | ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[2], 12); |
| 219 | ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[13], 9); |
| 220 | ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[9], 12); |
| 221 | ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[7], 5); |
| 222 | ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[10], 15); |
| 223 | ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[14], 8); |
| 224 | |
| 225 | /* Swap contents of "d" registers */ |
Gustavo A. R. Silva | a478908 | 2018-07-18 12:19:08 -0500 | [diff] [blame] | 226 | swap(dd, ddd); |
Adrian-Ken Rueegsegger | c555c28 | 2008-05-09 21:27:02 +0800 | [diff] [blame] | 227 | |
| 228 | /* round 5: left lane" */ |
| 229 | ROUND(bb, cc, dd, ee, aa, F5, K5, in[4], 9); |
| 230 | ROUND(aa, bb, cc, dd, ee, F5, K5, in[0], 15); |
| 231 | ROUND(ee, aa, bb, cc, dd, F5, K5, in[5], 5); |
| 232 | ROUND(dd, ee, aa, bb, cc, F5, K5, in[9], 11); |
| 233 | ROUND(cc, dd, ee, aa, bb, F5, K5, in[7], 6); |
| 234 | ROUND(bb, cc, dd, ee, aa, F5, K5, in[12], 8); |
| 235 | ROUND(aa, bb, cc, dd, ee, F5, K5, in[2], 13); |
| 236 | ROUND(ee, aa, bb, cc, dd, F5, K5, in[10], 12); |
| 237 | ROUND(dd, ee, aa, bb, cc, F5, K5, in[14], 5); |
| 238 | ROUND(cc, dd, ee, aa, bb, F5, K5, in[1], 12); |
| 239 | ROUND(bb, cc, dd, ee, aa, F5, K5, in[3], 13); |
| 240 | ROUND(aa, bb, cc, dd, ee, F5, K5, in[8], 14); |
| 241 | ROUND(ee, aa, bb, cc, dd, F5, K5, in[11], 11); |
| 242 | ROUND(dd, ee, aa, bb, cc, F5, K5, in[6], 8); |
| 243 | ROUND(cc, dd, ee, aa, bb, F5, K5, in[15], 5); |
| 244 | ROUND(bb, cc, dd, ee, aa, F5, K5, in[13], 6); |
| 245 | |
| 246 | /* round 5: right lane */ |
| 247 | ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[12], 8); |
| 248 | ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[15], 5); |
| 249 | ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[10], 12); |
| 250 | ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[4], 9); |
| 251 | ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[1], 12); |
| 252 | ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[5], 5); |
| 253 | ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[8], 14); |
| 254 | ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[7], 6); |
| 255 | ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[6], 8); |
| 256 | ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[2], 13); |
| 257 | ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[13], 6); |
| 258 | ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[14], 5); |
| 259 | ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[0], 15); |
| 260 | ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[3], 13); |
| 261 | ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[9], 11); |
| 262 | ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[11], 11); |
| 263 | |
| 264 | /* Swap contents of "e" registers */ |
Gustavo A. R. Silva | a478908 | 2018-07-18 12:19:08 -0500 | [diff] [blame] | 265 | swap(ee, eee); |
Adrian-Ken Rueegsegger | c555c28 | 2008-05-09 21:27:02 +0800 | [diff] [blame] | 266 | |
| 267 | /* combine results */ |
| 268 | state[0] += aa; |
| 269 | state[1] += bb; |
| 270 | state[2] += cc; |
| 271 | state[3] += dd; |
| 272 | state[4] += ee; |
| 273 | state[5] += aaa; |
| 274 | state[6] += bbb; |
| 275 | state[7] += ccc; |
| 276 | state[8] += ddd; |
| 277 | state[9] += eee; |
Adrian-Ken Rueegsegger | c555c28 | 2008-05-09 21:27:02 +0800 | [diff] [blame] | 278 | } |
| 279 | |
Herbert Xu | 3b8efb4 | 2008-11-08 10:11:09 +0800 | [diff] [blame] | 280 | static int rmd320_init(struct shash_desc *desc) |
Adrian-Ken Rueegsegger | c555c28 | 2008-05-09 21:27:02 +0800 | [diff] [blame] | 281 | { |
Herbert Xu | 3b8efb4 | 2008-11-08 10:11:09 +0800 | [diff] [blame] | 282 | struct rmd320_ctx *rctx = shash_desc_ctx(desc); |
Adrian-Ken Rueegsegger | c555c28 | 2008-05-09 21:27:02 +0800 | [diff] [blame] | 283 | |
| 284 | rctx->byte_count = 0; |
| 285 | |
| 286 | rctx->state[0] = RMD_H0; |
| 287 | rctx->state[1] = RMD_H1; |
| 288 | rctx->state[2] = RMD_H2; |
| 289 | rctx->state[3] = RMD_H3; |
| 290 | rctx->state[4] = RMD_H4; |
| 291 | rctx->state[5] = RMD_H5; |
| 292 | rctx->state[6] = RMD_H6; |
| 293 | rctx->state[7] = RMD_H7; |
| 294 | rctx->state[8] = RMD_H8; |
| 295 | rctx->state[9] = RMD_H9; |
| 296 | |
| 297 | memset(rctx->buffer, 0, sizeof(rctx->buffer)); |
Herbert Xu | 3b8efb4 | 2008-11-08 10:11:09 +0800 | [diff] [blame] | 298 | |
| 299 | return 0; |
Adrian-Ken Rueegsegger | c555c28 | 2008-05-09 21:27:02 +0800 | [diff] [blame] | 300 | } |
| 301 | |
Herbert Xu | 3b8efb4 | 2008-11-08 10:11:09 +0800 | [diff] [blame] | 302 | static int rmd320_update(struct shash_desc *desc, const u8 *data, |
| 303 | unsigned int len) |
Adrian-Ken Rueegsegger | c555c28 | 2008-05-09 21:27:02 +0800 | [diff] [blame] | 304 | { |
Herbert Xu | 3b8efb4 | 2008-11-08 10:11:09 +0800 | [diff] [blame] | 305 | struct rmd320_ctx *rctx = shash_desc_ctx(desc); |
Adrian-Ken Rueegsegger | c555c28 | 2008-05-09 21:27:02 +0800 | [diff] [blame] | 306 | const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f); |
| 307 | |
| 308 | rctx->byte_count += len; |
| 309 | |
| 310 | /* Enough space in buffer? If so copy and we're done */ |
| 311 | if (avail > len) { |
| 312 | memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail), |
| 313 | data, len); |
Herbert Xu | 3b8efb4 | 2008-11-08 10:11:09 +0800 | [diff] [blame] | 314 | goto out; |
Adrian-Ken Rueegsegger | c555c28 | 2008-05-09 21:27:02 +0800 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail), |
| 318 | data, avail); |
| 319 | |
Adrian-Ken Rueegsegger | feedfda | 2008-05-26 20:54:34 +1000 | [diff] [blame] | 320 | rmd320_transform(rctx->state, rctx->buffer); |
Adrian-Ken Rueegsegger | c555c28 | 2008-05-09 21:27:02 +0800 | [diff] [blame] | 321 | data += avail; |
| 322 | len -= avail; |
| 323 | |
| 324 | while (len >= sizeof(rctx->buffer)) { |
| 325 | memcpy(rctx->buffer, data, sizeof(rctx->buffer)); |
Adrian-Ken Rueegsegger | feedfda | 2008-05-26 20:54:34 +1000 | [diff] [blame] | 326 | rmd320_transform(rctx->state, rctx->buffer); |
Adrian-Ken Rueegsegger | c555c28 | 2008-05-09 21:27:02 +0800 | [diff] [blame] | 327 | data += sizeof(rctx->buffer); |
| 328 | len -= sizeof(rctx->buffer); |
| 329 | } |
| 330 | |
| 331 | memcpy(rctx->buffer, data, len); |
Herbert Xu | 3b8efb4 | 2008-11-08 10:11:09 +0800 | [diff] [blame] | 332 | |
| 333 | out: |
| 334 | return 0; |
Adrian-Ken Rueegsegger | c555c28 | 2008-05-09 21:27:02 +0800 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | /* Add padding and return the message digest. */ |
Herbert Xu | 3b8efb4 | 2008-11-08 10:11:09 +0800 | [diff] [blame] | 338 | static int rmd320_final(struct shash_desc *desc, u8 *out) |
Adrian-Ken Rueegsegger | c555c28 | 2008-05-09 21:27:02 +0800 | [diff] [blame] | 339 | { |
Herbert Xu | 3b8efb4 | 2008-11-08 10:11:09 +0800 | [diff] [blame] | 340 | struct rmd320_ctx *rctx = shash_desc_ctx(desc); |
Adrian-Ken Rueegsegger | feedfda | 2008-05-26 20:54:34 +1000 | [diff] [blame] | 341 | u32 i, index, padlen; |
Harvey Harrison | caee168 | 2008-07-04 19:48:58 +0800 | [diff] [blame] | 342 | __le64 bits; |
| 343 | __le32 *dst = (__le32 *)out; |
Adrian-Ken Rueegsegger | c555c28 | 2008-05-09 21:27:02 +0800 | [diff] [blame] | 344 | static const u8 padding[64] = { 0x80, }; |
Adrian-Ken Rueegsegger | feedfda | 2008-05-26 20:54:34 +1000 | [diff] [blame] | 345 | |
| 346 | bits = cpu_to_le64(rctx->byte_count << 3); |
Adrian-Ken Rueegsegger | c555c28 | 2008-05-09 21:27:02 +0800 | [diff] [blame] | 347 | |
| 348 | /* Pad out to 56 mod 64 */ |
| 349 | index = rctx->byte_count & 0x3f; |
| 350 | padlen = (index < 56) ? (56 - index) : ((64+56) - index); |
Herbert Xu | 3b8efb4 | 2008-11-08 10:11:09 +0800 | [diff] [blame] | 351 | rmd320_update(desc, padding, padlen); |
Adrian-Ken Rueegsegger | c555c28 | 2008-05-09 21:27:02 +0800 | [diff] [blame] | 352 | |
| 353 | /* Append length */ |
Herbert Xu | 3b8efb4 | 2008-11-08 10:11:09 +0800 | [diff] [blame] | 354 | rmd320_update(desc, (const u8 *)&bits, sizeof(bits)); |
Adrian-Ken Rueegsegger | c555c28 | 2008-05-09 21:27:02 +0800 | [diff] [blame] | 355 | |
| 356 | /* Store state in digest */ |
Adrian-Ken Rueegsegger | feedfda | 2008-05-26 20:54:34 +1000 | [diff] [blame] | 357 | for (i = 0; i < 10; i++) |
Herbert Xu | 5cdcc22 | 2008-06-02 21:30:38 +1000 | [diff] [blame] | 358 | dst[i] = cpu_to_le32p(&rctx->state[i]); |
Adrian-Ken Rueegsegger | c555c28 | 2008-05-09 21:27:02 +0800 | [diff] [blame] | 359 | |
| 360 | /* Wipe context */ |
| 361 | memset(rctx, 0, sizeof(*rctx)); |
Herbert Xu | 3b8efb4 | 2008-11-08 10:11:09 +0800 | [diff] [blame] | 362 | |
| 363 | return 0; |
Adrian-Ken Rueegsegger | c555c28 | 2008-05-09 21:27:02 +0800 | [diff] [blame] | 364 | } |
| 365 | |
Herbert Xu | 3b8efb4 | 2008-11-08 10:11:09 +0800 | [diff] [blame] | 366 | static struct shash_alg alg = { |
| 367 | .digestsize = RMD320_DIGEST_SIZE, |
| 368 | .init = rmd320_init, |
| 369 | .update = rmd320_update, |
| 370 | .final = rmd320_final, |
| 371 | .descsize = sizeof(struct rmd320_ctx), |
| 372 | .base = { |
| 373 | .cra_name = "rmd320", |
Herbert Xu | 3b8efb4 | 2008-11-08 10:11:09 +0800 | [diff] [blame] | 374 | .cra_blocksize = RMD320_BLOCK_SIZE, |
| 375 | .cra_module = THIS_MODULE, |
| 376 | } |
Adrian-Ken Rueegsegger | c555c28 | 2008-05-09 21:27:02 +0800 | [diff] [blame] | 377 | }; |
| 378 | |
| 379 | static int __init rmd320_mod_init(void) |
| 380 | { |
Herbert Xu | 3b8efb4 | 2008-11-08 10:11:09 +0800 | [diff] [blame] | 381 | return crypto_register_shash(&alg); |
Adrian-Ken Rueegsegger | c555c28 | 2008-05-09 21:27:02 +0800 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | static void __exit rmd320_mod_fini(void) |
| 385 | { |
Herbert Xu | 3b8efb4 | 2008-11-08 10:11:09 +0800 | [diff] [blame] | 386 | crypto_unregister_shash(&alg); |
Adrian-Ken Rueegsegger | c555c28 | 2008-05-09 21:27:02 +0800 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | module_init(rmd320_mod_init); |
| 390 | module_exit(rmd320_mod_fini); |
| 391 | |
| 392 | MODULE_LICENSE("GPL"); |
Adrian-Ken Rueegsegger | 3181c22 | 2011-01-04 15:35:51 +1100 | [diff] [blame] | 393 | MODULE_AUTHOR("Adrian-Ken Rueegsegger <ken@codelabs.ch>"); |
Adrian-Ken Rueegsegger | c555c28 | 2008-05-09 21:27:02 +0800 | [diff] [blame] | 394 | MODULE_DESCRIPTION("RIPEMD-320 Message Digest"); |
Kees Cook | 5d26a10 | 2014-11-20 17:05:53 -0800 | [diff] [blame] | 395 | MODULE_ALIAS_CRYPTO("rmd320"); |