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