blob: efbeb3e0dcfb048099095a971512d8a9a1f62595 [file] [log] [blame]
Ard Biesheuvel2c988332014-03-06 16:23:33 +08001/*
2 * sha1-ce-glue.c - SHA-1 secure hash using ARMv8 Crypto Extensions
3 *
Ard Biesheuvel0771f322017-07-24 11:28:08 +01004 * Copyright (C) 2014 - 2017 Linaro Ltd <ard.biesheuvel@linaro.org>
Ard Biesheuvel2c988332014-03-06 16:23:33 +08005 *
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>
Ard Biesheuvel0771f322017-07-24 11:28:08 +010012#include <asm/simd.h>
Ard Biesheuvel2c988332014-03-06 16:23:33 +080013#include <asm/unaligned.h>
14#include <crypto/internal/hash.h>
15#include <crypto/sha.h>
Ard Biesheuvel07eb54d2015-04-09 12:55:44 +020016#include <crypto/sha1_base.h>
Ard Biesheuvel2c988332014-03-06 16:23:33 +080017#include <linux/cpufeature.h>
18#include <linux/crypto.h>
19#include <linux/module.h>
20
21MODULE_DESCRIPTION("SHA1 secure hash using ARMv8 Crypto Extensions");
22MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
23MODULE_LICENSE("GPL v2");
24
Ard Biesheuvel07eb54d2015-04-09 12:55:44 +020025struct sha1_ce_state {
26 struct sha1_state sst;
27 u32 finalize;
28};
Ard Biesheuvel2c988332014-03-06 16:23:33 +080029
Ard Biesheuvel07eb54d2015-04-09 12:55:44 +020030asmlinkage void sha1_ce_transform(struct sha1_ce_state *sst, u8 const *src,
31 int blocks);
32
Ard Biesheuvelf4857f42017-04-26 17:11:32 +010033const u32 sha1_ce_offsetof_count = offsetof(struct sha1_ce_state, sst.count);
34const u32 sha1_ce_offsetof_finalize = offsetof(struct sha1_ce_state, finalize);
35
Ard Biesheuvel07eb54d2015-04-09 12:55:44 +020036static int sha1_ce_update(struct shash_desc *desc, const u8 *data,
37 unsigned int len)
Ard Biesheuvel2c988332014-03-06 16:23:33 +080038{
Ard Biesheuvel07eb54d2015-04-09 12:55:44 +020039 struct sha1_ce_state *sctx = shash_desc_ctx(desc);
Ard Biesheuvel2c988332014-03-06 16:23:33 +080040
Ard Biesheuvel0771f322017-07-24 11:28:08 +010041 if (!may_use_simd())
42 return crypto_sha1_update(desc, data, len);
43
Ard Biesheuvel07eb54d2015-04-09 12:55:44 +020044 sctx->finalize = 0;
Ard Biesheuvel0771f322017-07-24 11:28:08 +010045 kernel_neon_begin();
Ard Biesheuvel07eb54d2015-04-09 12:55:44 +020046 sha1_base_do_update(desc, data, len,
47 (sha1_block_fn *)sha1_ce_transform);
Ard Biesheuvel2c988332014-03-06 16:23:33 +080048 kernel_neon_end();
49
Ard Biesheuvel2c988332014-03-06 16:23:33 +080050 return 0;
51}
52
Ard Biesheuvel07eb54d2015-04-09 12:55:44 +020053static int sha1_ce_finup(struct shash_desc *desc, const u8 *data,
54 unsigned int len, u8 *out)
Ard Biesheuvel2c988332014-03-06 16:23:33 +080055{
Ard Biesheuvel07eb54d2015-04-09 12:55:44 +020056 struct sha1_ce_state *sctx = shash_desc_ctx(desc);
57 bool finalize = !sctx->sst.count && !(len % SHA1_BLOCK_SIZE);
Ard Biesheuvel2c988332014-03-06 16:23:33 +080058
Ard Biesheuvel0771f322017-07-24 11:28:08 +010059 if (!may_use_simd())
60 return crypto_sha1_finup(desc, data, len, out);
61
Ard Biesheuvel07eb54d2015-04-09 12:55:44 +020062 /*
63 * Allow the asm code to perform the finalization if there is no
64 * partial data and the input is a round multiple of the block size.
65 */
66 sctx->finalize = finalize;
67
Ard Biesheuvel0771f322017-07-24 11:28:08 +010068 kernel_neon_begin();
Ard Biesheuvel07eb54d2015-04-09 12:55:44 +020069 sha1_base_do_update(desc, data, len,
70 (sha1_block_fn *)sha1_ce_transform);
71 if (!finalize)
72 sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform);
73 kernel_neon_end();
74 return sha1_base_finish(desc, out);
Ard Biesheuvel2c988332014-03-06 16:23:33 +080075}
76
Ard Biesheuvel07eb54d2015-04-09 12:55:44 +020077static int sha1_ce_final(struct shash_desc *desc, u8 *out)
Ard Biesheuvel2c988332014-03-06 16:23:33 +080078{
Ard Biesheuvelbf7883e2015-05-06 15:54:31 +020079 struct sha1_ce_state *sctx = shash_desc_ctx(desc);
80
Ard Biesheuvel0771f322017-07-24 11:28:08 +010081 if (!may_use_simd())
82 return crypto_sha1_finup(desc, NULL, 0, out);
83
Ard Biesheuvelbf7883e2015-05-06 15:54:31 +020084 sctx->finalize = 0;
Ard Biesheuvel0771f322017-07-24 11:28:08 +010085 kernel_neon_begin();
Ard Biesheuvel07eb54d2015-04-09 12:55:44 +020086 sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform);
87 kernel_neon_end();
88 return sha1_base_finish(desc, out);
Ard Biesheuvel2c988332014-03-06 16:23:33 +080089}
90
91static struct shash_alg alg = {
Ard Biesheuvel07eb54d2015-04-09 12:55:44 +020092 .init = sha1_base_init,
93 .update = sha1_ce_update,
94 .final = sha1_ce_final,
95 .finup = sha1_ce_finup,
96 .descsize = sizeof(struct sha1_ce_state),
Ard Biesheuvel2c988332014-03-06 16:23:33 +080097 .digestsize = SHA1_DIGEST_SIZE,
Ard Biesheuvel2c988332014-03-06 16:23:33 +080098 .base = {
99 .cra_name = "sha1",
100 .cra_driver_name = "sha1-ce",
101 .cra_priority = 200,
102 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
103 .cra_blocksize = SHA1_BLOCK_SIZE,
104 .cra_module = THIS_MODULE,
105 }
106};
107
108static int __init sha1_ce_mod_init(void)
109{
110 return crypto_register_shash(&alg);
111}
112
113static void __exit sha1_ce_mod_fini(void)
114{
115 crypto_unregister_shash(&alg);
116}
117
118module_cpu_feature_match(SHA1, sha1_ce_mod_init);
119module_exit(sha1_ce_mod_fini);