Ard Biesheuvel | 2c98833 | 2014-03-06 16:23:33 +0800 | [diff] [blame] | 1 | /* |
| 2 | * sha1-ce-glue.c - SHA-1 secure hash using ARMv8 Crypto Extensions |
| 3 | * |
| 4 | * Copyright (C) 2014 Linaro Ltd <ard.biesheuvel@linaro.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | |
| 11 | #include <asm/neon.h> |
| 12 | #include <asm/unaligned.h> |
| 13 | #include <crypto/internal/hash.h> |
| 14 | #include <crypto/sha.h> |
Ard Biesheuvel | 07eb54d | 2015-04-09 12:55:44 +0200 | [diff] [blame] | 15 | #include <crypto/sha1_base.h> |
Ard Biesheuvel | 2c98833 | 2014-03-06 16:23:33 +0800 | [diff] [blame] | 16 | #include <linux/cpufeature.h> |
| 17 | #include <linux/crypto.h> |
| 18 | #include <linux/module.h> |
| 19 | |
| 20 | MODULE_DESCRIPTION("SHA1 secure hash using ARMv8 Crypto Extensions"); |
| 21 | MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>"); |
| 22 | MODULE_LICENSE("GPL v2"); |
| 23 | |
Ard Biesheuvel | 07eb54d | 2015-04-09 12:55:44 +0200 | [diff] [blame] | 24 | struct sha1_ce_state { |
| 25 | struct sha1_state sst; |
| 26 | u32 finalize; |
| 27 | }; |
Ard Biesheuvel | 2c98833 | 2014-03-06 16:23:33 +0800 | [diff] [blame] | 28 | |
Ard Biesheuvel | 07eb54d | 2015-04-09 12:55:44 +0200 | [diff] [blame] | 29 | asmlinkage void sha1_ce_transform(struct sha1_ce_state *sst, u8 const *src, |
| 30 | int blocks); |
| 31 | |
Ard Biesheuvel | 0a953c0 | 2017-04-26 17:11:32 +0100 | [diff] [blame] | 32 | const u32 sha1_ce_offsetof_count = offsetof(struct sha1_ce_state, sst.count); |
| 33 | const u32 sha1_ce_offsetof_finalize = offsetof(struct sha1_ce_state, finalize); |
| 34 | |
Ard Biesheuvel | 07eb54d | 2015-04-09 12:55:44 +0200 | [diff] [blame] | 35 | static int sha1_ce_update(struct shash_desc *desc, const u8 *data, |
| 36 | unsigned int len) |
Ard Biesheuvel | 2c98833 | 2014-03-06 16:23:33 +0800 | [diff] [blame] | 37 | { |
Ard Biesheuvel | 07eb54d | 2015-04-09 12:55:44 +0200 | [diff] [blame] | 38 | struct sha1_ce_state *sctx = shash_desc_ctx(desc); |
Ard Biesheuvel | 2c98833 | 2014-03-06 16:23:33 +0800 | [diff] [blame] | 39 | |
Ard Biesheuvel | 07eb54d | 2015-04-09 12:55:44 +0200 | [diff] [blame] | 40 | sctx->finalize = 0; |
Ard Biesheuvel | 2c98833 | 2014-03-06 16:23:33 +0800 | [diff] [blame] | 41 | kernel_neon_begin_partial(16); |
Ard Biesheuvel | 07eb54d | 2015-04-09 12:55:44 +0200 | [diff] [blame] | 42 | sha1_base_do_update(desc, data, len, |
| 43 | (sha1_block_fn *)sha1_ce_transform); |
Ard Biesheuvel | 2c98833 | 2014-03-06 16:23:33 +0800 | [diff] [blame] | 44 | kernel_neon_end(); |
| 45 | |
Ard Biesheuvel | 2c98833 | 2014-03-06 16:23:33 +0800 | [diff] [blame] | 46 | return 0; |
| 47 | } |
| 48 | |
Ard Biesheuvel | 07eb54d | 2015-04-09 12:55:44 +0200 | [diff] [blame] | 49 | static int sha1_ce_finup(struct shash_desc *desc, const u8 *data, |
| 50 | unsigned int len, u8 *out) |
Ard Biesheuvel | 2c98833 | 2014-03-06 16:23:33 +0800 | [diff] [blame] | 51 | { |
Ard Biesheuvel | 07eb54d | 2015-04-09 12:55:44 +0200 | [diff] [blame] | 52 | struct sha1_ce_state *sctx = shash_desc_ctx(desc); |
| 53 | bool finalize = !sctx->sst.count && !(len % SHA1_BLOCK_SIZE); |
Ard Biesheuvel | 2c98833 | 2014-03-06 16:23:33 +0800 | [diff] [blame] | 54 | |
Ard Biesheuvel | 07eb54d | 2015-04-09 12:55:44 +0200 | [diff] [blame] | 55 | /* |
| 56 | * Allow the asm code to perform the finalization if there is no |
| 57 | * partial data and the input is a round multiple of the block size. |
| 58 | */ |
| 59 | sctx->finalize = finalize; |
| 60 | |
| 61 | kernel_neon_begin_partial(16); |
| 62 | sha1_base_do_update(desc, data, len, |
| 63 | (sha1_block_fn *)sha1_ce_transform); |
| 64 | if (!finalize) |
| 65 | sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform); |
| 66 | kernel_neon_end(); |
| 67 | return sha1_base_finish(desc, out); |
Ard Biesheuvel | 2c98833 | 2014-03-06 16:23:33 +0800 | [diff] [blame] | 68 | } |
| 69 | |
Ard Biesheuvel | 07eb54d | 2015-04-09 12:55:44 +0200 | [diff] [blame] | 70 | static int sha1_ce_final(struct shash_desc *desc, u8 *out) |
Ard Biesheuvel | 2c98833 | 2014-03-06 16:23:33 +0800 | [diff] [blame] | 71 | { |
Ard Biesheuvel | bf7883e | 2015-05-06 15:54:31 +0200 | [diff] [blame] | 72 | struct sha1_ce_state *sctx = shash_desc_ctx(desc); |
| 73 | |
| 74 | sctx->finalize = 0; |
Ard Biesheuvel | 07eb54d | 2015-04-09 12:55:44 +0200 | [diff] [blame] | 75 | kernel_neon_begin_partial(16); |
| 76 | sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform); |
| 77 | kernel_neon_end(); |
| 78 | return sha1_base_finish(desc, out); |
Ard Biesheuvel | 2c98833 | 2014-03-06 16:23:33 +0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | static struct shash_alg alg = { |
Ard Biesheuvel | 07eb54d | 2015-04-09 12:55:44 +0200 | [diff] [blame] | 82 | .init = sha1_base_init, |
| 83 | .update = sha1_ce_update, |
| 84 | .final = sha1_ce_final, |
| 85 | .finup = sha1_ce_finup, |
| 86 | .descsize = sizeof(struct sha1_ce_state), |
Ard Biesheuvel | 2c98833 | 2014-03-06 16:23:33 +0800 | [diff] [blame] | 87 | .digestsize = SHA1_DIGEST_SIZE, |
Ard Biesheuvel | 2c98833 | 2014-03-06 16:23:33 +0800 | [diff] [blame] | 88 | .base = { |
| 89 | .cra_name = "sha1", |
| 90 | .cra_driver_name = "sha1-ce", |
| 91 | .cra_priority = 200, |
| 92 | .cra_flags = CRYPTO_ALG_TYPE_SHASH, |
| 93 | .cra_blocksize = SHA1_BLOCK_SIZE, |
| 94 | .cra_module = THIS_MODULE, |
| 95 | } |
| 96 | }; |
| 97 | |
| 98 | static int __init sha1_ce_mod_init(void) |
| 99 | { |
| 100 | return crypto_register_shash(&alg); |
| 101 | } |
| 102 | |
| 103 | static void __exit sha1_ce_mod_fini(void) |
| 104 | { |
| 105 | crypto_unregister_shash(&alg); |
| 106 | } |
| 107 | |
| 108 | module_cpu_feature_match(SHA1, sha1_ce_mod_init); |
| 109 | module_exit(sha1_ce_mod_fini); |