Commit | Line | Data |
---|---|---|
006d0624 AB |
1 | /* |
2 | * sha2-ce-glue.c - SHA-224/SHA-256 using ARMv8 Crypto Extensions | |
3 | * | |
4 | * Copyright (C) 2015 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 <crypto/internal/hash.h> | |
12 | #include <crypto/sha.h> | |
9205b949 | 13 | #include <crypto/sha256_base.h> |
006d0624 AB |
14 | #include <linux/crypto.h> |
15 | #include <linux/module.h> | |
16 | ||
17 | #include <asm/hwcap.h> | |
18 | #include <asm/simd.h> | |
19 | #include <asm/neon.h> | |
20 | #include <asm/unaligned.h> | |
21 | ||
9205b949 AB |
22 | #include "sha256_glue.h" |
23 | ||
006d0624 AB |
24 | MODULE_DESCRIPTION("SHA-224/SHA-256 secure hash using ARMv8 Crypto Extensions"); |
25 | MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>"); | |
26 | MODULE_LICENSE("GPL v2"); | |
27 | ||
9205b949 AB |
28 | asmlinkage void sha2_ce_transform(struct sha256_state *sst, u8 const *src, |
29 | int blocks); | |
006d0624 | 30 | |
9205b949 AB |
31 | static int sha2_ce_update(struct shash_desc *desc, const u8 *data, |
32 | unsigned int len) | |
006d0624 AB |
33 | { |
34 | struct sha256_state *sctx = shash_desc_ctx(desc); | |
35 | ||
9205b949 AB |
36 | if (!may_use_simd() || |
37 | (sctx->count % SHA256_BLOCK_SIZE) + len < SHA256_BLOCK_SIZE) | |
38 | return crypto_sha256_arm_update(desc, data, len); | |
006d0624 | 39 | |
9205b949 AB |
40 | kernel_neon_begin(); |
41 | sha256_base_do_update(desc, data, len, | |
42 | (sha256_block_fn *)sha2_ce_transform); | |
43 | kernel_neon_end(); | |
006d0624 | 44 | |
006d0624 AB |
45 | return 0; |
46 | } | |
47 | ||
9205b949 AB |
48 | static int sha2_ce_finup(struct shash_desc *desc, const u8 *data, |
49 | unsigned int len, u8 *out) | |
006d0624 | 50 | { |
006d0624 | 51 | if (!may_use_simd()) |
9205b949 | 52 | return crypto_sha256_arm_finup(desc, data, len, out); |
006d0624 | 53 | |
9205b949 | 54 | kernel_neon_begin(); |
006d0624 | 55 | if (len) |
9205b949 AB |
56 | sha256_base_do_update(desc, data, len, |
57 | (sha256_block_fn *)sha2_ce_transform); | |
58 | sha256_base_do_finalize(desc, (sha256_block_fn *)sha2_ce_transform); | |
59 | kernel_neon_end(); | |
006d0624 | 60 | |
9205b949 | 61 | return sha256_base_finish(desc, out); |
006d0624 AB |
62 | } |
63 | ||
9205b949 | 64 | static int sha2_ce_final(struct shash_desc *desc, u8 *out) |
006d0624 | 65 | { |
9205b949 | 66 | return sha2_ce_finup(desc, NULL, 0, out); |
006d0624 AB |
67 | } |
68 | ||
69 | static struct shash_alg algs[] = { { | |
9205b949 AB |
70 | .init = sha224_base_init, |
71 | .update = sha2_ce_update, | |
72 | .final = sha2_ce_final, | |
73 | .finup = sha2_ce_finup, | |
006d0624 AB |
74 | .descsize = sizeof(struct sha256_state), |
75 | .digestsize = SHA224_DIGEST_SIZE, | |
006d0624 AB |
76 | .base = { |
77 | .cra_name = "sha224", | |
78 | .cra_driver_name = "sha224-ce", | |
f2f770d7 | 79 | .cra_priority = 300, |
006d0624 AB |
80 | .cra_flags = CRYPTO_ALG_TYPE_SHASH, |
81 | .cra_blocksize = SHA256_BLOCK_SIZE, | |
82 | .cra_module = THIS_MODULE, | |
83 | } | |
84 | }, { | |
9205b949 AB |
85 | .init = sha256_base_init, |
86 | .update = sha2_ce_update, | |
87 | .final = sha2_ce_final, | |
88 | .finup = sha2_ce_finup, | |
006d0624 AB |
89 | .descsize = sizeof(struct sha256_state), |
90 | .digestsize = SHA256_DIGEST_SIZE, | |
006d0624 AB |
91 | .base = { |
92 | .cra_name = "sha256", | |
93 | .cra_driver_name = "sha256-ce", | |
f2f770d7 | 94 | .cra_priority = 300, |
006d0624 AB |
95 | .cra_flags = CRYPTO_ALG_TYPE_SHASH, |
96 | .cra_blocksize = SHA256_BLOCK_SIZE, | |
97 | .cra_module = THIS_MODULE, | |
98 | } | |
99 | } }; | |
100 | ||
101 | static int __init sha2_ce_mod_init(void) | |
102 | { | |
103 | if (!(elf_hwcap2 & HWCAP2_SHA2)) | |
104 | return -ENODEV; | |
105 | return crypto_register_shashes(algs, ARRAY_SIZE(algs)); | |
106 | } | |
107 | ||
108 | static void __exit sha2_ce_mod_fini(void) | |
109 | { | |
110 | crypto_unregister_shashes(algs, ARRAY_SIZE(algs)); | |
111 | } | |
112 | ||
113 | module_init(sha2_ce_mod_init); | |
114 | module_exit(sha2_ce_mod_fini); |