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