4 * SHA-3, as specified in
5 * http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.202.pdf
7 * SHA-3 code by Jeff Garzik <jeff@garzik.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)•
15 #include <crypto/internal/hash.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <crypto/sha3.h>
20 #include <asm/byteorder.h>
21 #include <asm/unaligned.h>
23 #define KECCAK_ROUNDS 24
25 #define ROTL64(x, y) (((x) << (y)) | ((x) >> (64 - (y))))
27 static const u64 keccakf_rndc
[24] = {
28 0x0000000000000001ULL
, 0x0000000000008082ULL
, 0x800000000000808aULL
,
29 0x8000000080008000ULL
, 0x000000000000808bULL
, 0x0000000080000001ULL
,
30 0x8000000080008081ULL
, 0x8000000000008009ULL
, 0x000000000000008aULL
,
31 0x0000000000000088ULL
, 0x0000000080008009ULL
, 0x000000008000000aULL
,
32 0x000000008000808bULL
, 0x800000000000008bULL
, 0x8000000000008089ULL
,
33 0x8000000000008003ULL
, 0x8000000000008002ULL
, 0x8000000000000080ULL
,
34 0x000000000000800aULL
, 0x800000008000000aULL
, 0x8000000080008081ULL
,
35 0x8000000000008080ULL
, 0x0000000080000001ULL
, 0x8000000080008008ULL
38 static const int keccakf_rotc
[24] = {
39 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 2, 14,
40 27, 41, 56, 8, 25, 43, 62, 18, 39, 61, 20, 44
43 static const int keccakf_piln
[24] = {
44 10, 7, 11, 17, 18, 3, 5, 16, 8, 21, 24, 4,
45 15, 23, 19, 13, 12, 2, 20, 14, 22, 9, 6, 1
48 /* update the state with given number of rounds */
50 static void keccakf(u64 st
[25])
55 for (round
= 0; round
< KECCAK_ROUNDS
; round
++) {
58 for (i
= 0; i
< 5; i
++)
59 bc
[i
] = st
[i
] ^ st
[i
+ 5] ^ st
[i
+ 10] ^ st
[i
+ 15]
62 for (i
= 0; i
< 5; i
++) {
63 t
= bc
[(i
+ 4) % 5] ^ ROTL64(bc
[(i
+ 1) % 5], 1);
64 for (j
= 0; j
< 25; j
+= 5)
70 for (i
= 0; i
< 24; i
++) {
73 st
[j
] = ROTL64(t
, keccakf_rotc
[i
]);
78 for (j
= 0; j
< 25; j
+= 5) {
79 for (i
= 0; i
< 5; i
++)
81 for (i
= 0; i
< 5; i
++)
82 st
[j
+ i
] ^= (~bc
[(i
+ 1) % 5]) &
87 st
[0] ^= keccakf_rndc
[round
];
91 static void sha3_init(struct sha3_state
*sctx
, unsigned int digest_sz
)
93 memset(sctx
, 0, sizeof(*sctx
));
94 sctx
->md_len
= digest_sz
;
95 sctx
->rsiz
= 200 - 2 * digest_sz
;
96 sctx
->rsizw
= sctx
->rsiz
/ 8;
99 static int sha3_224_init(struct shash_desc
*desc
)
101 struct sha3_state
*sctx
= shash_desc_ctx(desc
);
103 sha3_init(sctx
, SHA3_224_DIGEST_SIZE
);
107 static int sha3_256_init(struct shash_desc
*desc
)
109 struct sha3_state
*sctx
= shash_desc_ctx(desc
);
111 sha3_init(sctx
, SHA3_256_DIGEST_SIZE
);
115 static int sha3_384_init(struct shash_desc
*desc
)
117 struct sha3_state
*sctx
= shash_desc_ctx(desc
);
119 sha3_init(sctx
, SHA3_384_DIGEST_SIZE
);
123 static int sha3_512_init(struct shash_desc
*desc
)
125 struct sha3_state
*sctx
= shash_desc_ctx(desc
);
127 sha3_init(sctx
, SHA3_512_DIGEST_SIZE
);
131 static int sha3_update(struct shash_desc
*desc
, const u8
*data
,
134 struct sha3_state
*sctx
= shash_desc_ctx(desc
);
141 if ((sctx
->partial
+ len
) > (sctx
->rsiz
- 1)) {
143 done
= -sctx
->partial
;
144 memcpy(sctx
->buf
+ sctx
->partial
, data
,
152 for (i
= 0; i
< sctx
->rsizw
; i
++)
153 sctx
->st
[i
] ^= get_unaligned_le64(src
+ 8 * i
);
158 } while (done
+ (sctx
->rsiz
- 1) < len
);
162 memcpy(sctx
->buf
+ sctx
->partial
, src
, len
- done
);
163 sctx
->partial
+= (len
- done
);
168 static int sha3_final(struct shash_desc
*desc
, u8
*out
)
170 struct sha3_state
*sctx
= shash_desc_ctx(desc
);
171 unsigned int i
, inlen
= sctx
->partial
;
173 sctx
->buf
[inlen
++] = 0x06;
174 memset(sctx
->buf
+ inlen
, 0, sctx
->rsiz
- inlen
);
175 sctx
->buf
[sctx
->rsiz
- 1] |= 0x80;
177 for (i
= 0; i
< sctx
->rsizw
; i
++)
178 sctx
->st
[i
] ^= get_unaligned_le64(sctx
->buf
+ 8 * i
);
182 for (i
= 0; i
< sctx
->rsizw
; i
++)
183 sctx
->st
[i
] = cpu_to_le64(sctx
->st
[i
]);
185 memcpy(out
, sctx
->st
, sctx
->md_len
);
187 memset(sctx
, 0, sizeof(*sctx
));
191 static struct shash_alg sha3_224
= {
192 .digestsize
= SHA3_224_DIGEST_SIZE
,
193 .init
= sha3_224_init
,
194 .update
= sha3_update
,
196 .descsize
= sizeof(struct sha3_state
),
198 .cra_name
= "sha3-224",
199 .cra_driver_name
= "sha3-224-generic",
200 .cra_flags
= CRYPTO_ALG_TYPE_SHASH
,
201 .cra_blocksize
= SHA3_224_BLOCK_SIZE
,
202 .cra_module
= THIS_MODULE
,
206 static struct shash_alg sha3_256
= {
207 .digestsize
= SHA3_256_DIGEST_SIZE
,
208 .init
= sha3_256_init
,
209 .update
= sha3_update
,
211 .descsize
= sizeof(struct sha3_state
),
213 .cra_name
= "sha3-256",
214 .cra_driver_name
= "sha3-256-generic",
215 .cra_flags
= CRYPTO_ALG_TYPE_SHASH
,
216 .cra_blocksize
= SHA3_256_BLOCK_SIZE
,
217 .cra_module
= THIS_MODULE
,
221 static struct shash_alg sha3_384
= {
222 .digestsize
= SHA3_384_DIGEST_SIZE
,
223 .init
= sha3_384_init
,
224 .update
= sha3_update
,
226 .descsize
= sizeof(struct sha3_state
),
228 .cra_name
= "sha3-384",
229 .cra_driver_name
= "sha3-384-generic",
230 .cra_flags
= CRYPTO_ALG_TYPE_SHASH
,
231 .cra_blocksize
= SHA3_384_BLOCK_SIZE
,
232 .cra_module
= THIS_MODULE
,
236 static struct shash_alg sha3_512
= {
237 .digestsize
= SHA3_512_DIGEST_SIZE
,
238 .init
= sha3_512_init
,
239 .update
= sha3_update
,
241 .descsize
= sizeof(struct sha3_state
),
243 .cra_name
= "sha3-512",
244 .cra_driver_name
= "sha3-512-generic",
245 .cra_flags
= CRYPTO_ALG_TYPE_SHASH
,
246 .cra_blocksize
= SHA3_512_BLOCK_SIZE
,
247 .cra_module
= THIS_MODULE
,
251 static int __init
sha3_generic_mod_init(void)
255 ret
= crypto_register_shash(&sha3_224
);
258 ret
= crypto_register_shash(&sha3_256
);
261 ret
= crypto_register_shash(&sha3_384
);
264 ret
= crypto_register_shash(&sha3_512
);
271 crypto_unregister_shash(&sha3_384
);
273 crypto_unregister_shash(&sha3_256
);
275 crypto_unregister_shash(&sha3_224
);
280 static void __exit
sha3_generic_mod_fini(void)
282 crypto_unregister_shash(&sha3_224
);
283 crypto_unregister_shash(&sha3_256
);
284 crypto_unregister_shash(&sha3_384
);
285 crypto_unregister_shash(&sha3_512
);
288 module_init(sha3_generic_mod_init
);
289 module_exit(sha3_generic_mod_fini
);
291 MODULE_LICENSE("GPL");
292 MODULE_DESCRIPTION("SHA-3 Secure Hash Algorithm");
294 MODULE_ALIAS_CRYPTO("sha3-224");
295 MODULE_ALIAS_CRYPTO("sha3-224-generic");
296 MODULE_ALIAS_CRYPTO("sha3-256");
297 MODULE_ALIAS_CRYPTO("sha3-256-generic");
298 MODULE_ALIAS_CRYPTO("sha3-384");
299 MODULE_ALIAS_CRYPTO("sha3-384-generic");
300 MODULE_ALIAS_CRYPTO("sha3-512");
301 MODULE_ALIAS_CRYPTO("sha3-512-generic");