Ard Biesheuvel | 6ba6c74 | 2014-03-20 15:35:40 +0100 | [diff] [blame] | 1 | /* |
| 2 | * sha2-ce-glue.c - SHA-224/SHA-256 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 | 03802f6 | 2015-04-09 12:55:45 +0200 | [diff] [blame] | 15 | #include <crypto/sha256_base.h> |
Ard Biesheuvel | 6ba6c74 | 2014-03-20 15:35:40 +0100 | [diff] [blame] | 16 | #include <linux/cpufeature.h> |
| 17 | #include <linux/crypto.h> |
| 18 | #include <linux/module.h> |
| 19 | |
| 20 | MODULE_DESCRIPTION("SHA-224/SHA-256 secure hash using ARMv8 Crypto Extensions"); |
| 21 | MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>"); |
| 22 | MODULE_LICENSE("GPL v2"); |
| 23 | |
Ard Biesheuvel | 03802f6 | 2015-04-09 12:55:45 +0200 | [diff] [blame] | 24 | struct sha256_ce_state { |
| 25 | struct sha256_state sst; |
| 26 | u32 finalize; |
| 27 | }; |
Ard Biesheuvel | 6ba6c74 | 2014-03-20 15:35:40 +0100 | [diff] [blame] | 28 | |
Ard Biesheuvel | 03802f6 | 2015-04-09 12:55:45 +0200 | [diff] [blame] | 29 | asmlinkage void sha2_ce_transform(struct sha256_ce_state *sst, u8 const *src, |
| 30 | int blocks); |
Sami Tolvanen | d6da00d | 2017-08-10 09:39:53 -0700 | [diff] [blame] | 31 | #ifdef CONFIG_CFI_CLANG |
| 32 | static inline void __cfi_sha2_ce_transform(struct sha256_state *sst, |
| 33 | u8 const *src, int blocks) |
| 34 | { |
| 35 | sha2_ce_transform((struct sha256_ce_state *)sst, src, blocks); |
| 36 | } |
| 37 | #define sha2_ce_transform __cfi_sha2_ce_transform |
| 38 | #endif |
Ard Biesheuvel | 03802f6 | 2015-04-09 12:55:45 +0200 | [diff] [blame] | 39 | |
Ard Biesheuvel | e2b2ca9 | 2017-04-26 17:11:32 +0100 | [diff] [blame] | 40 | const u32 sha256_ce_offsetof_count = offsetof(struct sha256_ce_state, |
| 41 | sst.count); |
| 42 | const u32 sha256_ce_offsetof_finalize = offsetof(struct sha256_ce_state, |
| 43 | finalize); |
| 44 | |
Ard Biesheuvel | 03802f6 | 2015-04-09 12:55:45 +0200 | [diff] [blame] | 45 | static int sha256_ce_update(struct shash_desc *desc, const u8 *data, |
| 46 | unsigned int len) |
Ard Biesheuvel | 6ba6c74 | 2014-03-20 15:35:40 +0100 | [diff] [blame] | 47 | { |
Ard Biesheuvel | 03802f6 | 2015-04-09 12:55:45 +0200 | [diff] [blame] | 48 | struct sha256_ce_state *sctx = shash_desc_ctx(desc); |
Ard Biesheuvel | 6ba6c74 | 2014-03-20 15:35:40 +0100 | [diff] [blame] | 49 | |
Ard Biesheuvel | 03802f6 | 2015-04-09 12:55:45 +0200 | [diff] [blame] | 50 | sctx->finalize = 0; |
| 51 | kernel_neon_begin_partial(28); |
| 52 | sha256_base_do_update(desc, data, len, |
| 53 | (sha256_block_fn *)sha2_ce_transform); |
| 54 | kernel_neon_end(); |
| 55 | |
Ard Biesheuvel | 6ba6c74 | 2014-03-20 15:35:40 +0100 | [diff] [blame] | 56 | return 0; |
| 57 | } |
| 58 | |
Ard Biesheuvel | 03802f6 | 2015-04-09 12:55:45 +0200 | [diff] [blame] | 59 | static int sha256_ce_finup(struct shash_desc *desc, const u8 *data, |
| 60 | unsigned int len, u8 *out) |
Ard Biesheuvel | 6ba6c74 | 2014-03-20 15:35:40 +0100 | [diff] [blame] | 61 | { |
Ard Biesheuvel | 03802f6 | 2015-04-09 12:55:45 +0200 | [diff] [blame] | 62 | struct sha256_ce_state *sctx = shash_desc_ctx(desc); |
| 63 | bool finalize = !sctx->sst.count && !(len % SHA256_BLOCK_SIZE); |
Ard Biesheuvel | 6ba6c74 | 2014-03-20 15:35:40 +0100 | [diff] [blame] | 64 | |
Ard Biesheuvel | 6ba6c74 | 2014-03-20 15:35:40 +0100 | [diff] [blame] | 65 | /* |
Ard Biesheuvel | 03802f6 | 2015-04-09 12:55:45 +0200 | [diff] [blame] | 66 | * Allow the asm code to perform the finalization if there is no |
| 67 | * partial data and the input is a round multiple of the block size. |
Ard Biesheuvel | 6ba6c74 | 2014-03-20 15:35:40 +0100 | [diff] [blame] | 68 | */ |
Ard Biesheuvel | 03802f6 | 2015-04-09 12:55:45 +0200 | [diff] [blame] | 69 | sctx->finalize = finalize; |
Ard Biesheuvel | 6ba6c74 | 2014-03-20 15:35:40 +0100 | [diff] [blame] | 70 | |
| 71 | kernel_neon_begin_partial(28); |
Ard Biesheuvel | 03802f6 | 2015-04-09 12:55:45 +0200 | [diff] [blame] | 72 | sha256_base_do_update(desc, data, len, |
| 73 | (sha256_block_fn *)sha2_ce_transform); |
| 74 | if (!finalize) |
| 75 | sha256_base_do_finalize(desc, |
| 76 | (sha256_block_fn *)sha2_ce_transform); |
Ard Biesheuvel | 6ba6c74 | 2014-03-20 15:35:40 +0100 | [diff] [blame] | 77 | kernel_neon_end(); |
Ard Biesheuvel | 03802f6 | 2015-04-09 12:55:45 +0200 | [diff] [blame] | 78 | return sha256_base_finish(desc, out); |
Ard Biesheuvel | 6ba6c74 | 2014-03-20 15:35:40 +0100 | [diff] [blame] | 79 | } |
| 80 | |
Ard Biesheuvel | 03802f6 | 2015-04-09 12:55:45 +0200 | [diff] [blame] | 81 | static int sha256_ce_final(struct shash_desc *desc, u8 *out) |
Ard Biesheuvel | 6ba6c74 | 2014-03-20 15:35:40 +0100 | [diff] [blame] | 82 | { |
Ard Biesheuvel | ec59a65 | 2015-05-06 15:54:32 +0200 | [diff] [blame] | 83 | struct sha256_ce_state *sctx = shash_desc_ctx(desc); |
| 84 | |
| 85 | sctx->finalize = 0; |
Ard Biesheuvel | 03802f6 | 2015-04-09 12:55:45 +0200 | [diff] [blame] | 86 | kernel_neon_begin_partial(28); |
| 87 | sha256_base_do_finalize(desc, (sha256_block_fn *)sha2_ce_transform); |
| 88 | kernel_neon_end(); |
| 89 | return sha256_base_finish(desc, out); |
Ard Biesheuvel | 6ba6c74 | 2014-03-20 15:35:40 +0100 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | static struct shash_alg algs[] = { { |
Ard Biesheuvel | 03802f6 | 2015-04-09 12:55:45 +0200 | [diff] [blame] | 93 | .init = sha224_base_init, |
| 94 | .update = sha256_ce_update, |
| 95 | .final = sha256_ce_final, |
| 96 | .finup = sha256_ce_finup, |
| 97 | .descsize = sizeof(struct sha256_ce_state), |
Ard Biesheuvel | 6ba6c74 | 2014-03-20 15:35:40 +0100 | [diff] [blame] | 98 | .digestsize = SHA224_DIGEST_SIZE, |
Ard Biesheuvel | 6ba6c74 | 2014-03-20 15:35:40 +0100 | [diff] [blame] | 99 | .base = { |
| 100 | .cra_name = "sha224", |
| 101 | .cra_driver_name = "sha224-ce", |
| 102 | .cra_priority = 200, |
| 103 | .cra_flags = CRYPTO_ALG_TYPE_SHASH, |
| 104 | .cra_blocksize = SHA256_BLOCK_SIZE, |
| 105 | .cra_module = THIS_MODULE, |
| 106 | } |
| 107 | }, { |
Ard Biesheuvel | 03802f6 | 2015-04-09 12:55:45 +0200 | [diff] [blame] | 108 | .init = sha256_base_init, |
| 109 | .update = sha256_ce_update, |
| 110 | .final = sha256_ce_final, |
| 111 | .finup = sha256_ce_finup, |
| 112 | .descsize = sizeof(struct sha256_ce_state), |
Ard Biesheuvel | 6ba6c74 | 2014-03-20 15:35:40 +0100 | [diff] [blame] | 113 | .digestsize = SHA256_DIGEST_SIZE, |
Ard Biesheuvel | 6ba6c74 | 2014-03-20 15:35:40 +0100 | [diff] [blame] | 114 | .base = { |
| 115 | .cra_name = "sha256", |
| 116 | .cra_driver_name = "sha256-ce", |
| 117 | .cra_priority = 200, |
| 118 | .cra_flags = CRYPTO_ALG_TYPE_SHASH, |
| 119 | .cra_blocksize = SHA256_BLOCK_SIZE, |
| 120 | .cra_module = THIS_MODULE, |
| 121 | } |
| 122 | } }; |
| 123 | |
| 124 | static int __init sha2_ce_mod_init(void) |
| 125 | { |
| 126 | return crypto_register_shashes(algs, ARRAY_SIZE(algs)); |
| 127 | } |
| 128 | |
| 129 | static void __exit sha2_ce_mod_fini(void) |
| 130 | { |
| 131 | crypto_unregister_shashes(algs, ARRAY_SIZE(algs)); |
| 132 | } |
| 133 | |
| 134 | module_cpu_feature_match(SHA2, sha2_ce_mod_init); |
| 135 | module_exit(sha2_ce_mod_fini); |