blob: aefda9868627bde843227d1074e7cc9b17004298 [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 *
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 Biesheuvel07eb54d2015-04-09 12:55:44 +020015#include <crypto/sha1_base.h>
Ard Biesheuvel2c988332014-03-06 16:23:33 +080016#include <linux/cpufeature.h>
17#include <linux/crypto.h>
18#include <linux/module.h>
19
Ard Biesheuvel07eb54d2015-04-09 12:55:44 +020020#define ASM_EXPORT(sym, val) \
21 asm(".globl " #sym "; .set " #sym ", %0" :: "I"(val));
22
Ard Biesheuvel2c988332014-03-06 16:23:33 +080023MODULE_DESCRIPTION("SHA1 secure hash using ARMv8 Crypto Extensions");
24MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
25MODULE_LICENSE("GPL v2");
26
Ard Biesheuvel07eb54d2015-04-09 12:55:44 +020027struct sha1_ce_state {
28 struct sha1_state sst;
29 u32 finalize;
30};
Ard Biesheuvel2c988332014-03-06 16:23:33 +080031
Ard Biesheuvel07eb54d2015-04-09 12:55:44 +020032asmlinkage void sha1_ce_transform(struct sha1_ce_state *sst, u8 const *src,
33 int blocks);
34
35static int sha1_ce_update(struct shash_desc *desc, const u8 *data,
36 unsigned int len)
Ard Biesheuvel2c988332014-03-06 16:23:33 +080037{
Ard Biesheuvel07eb54d2015-04-09 12:55:44 +020038 struct sha1_ce_state *sctx = shash_desc_ctx(desc);
Ard Biesheuvel2c988332014-03-06 16:23:33 +080039
Ard Biesheuvel07eb54d2015-04-09 12:55:44 +020040 sctx->finalize = 0;
Ard Biesheuvel2c988332014-03-06 16:23:33 +080041 kernel_neon_begin_partial(16);
Ard Biesheuvel07eb54d2015-04-09 12:55:44 +020042 sha1_base_do_update(desc, data, len,
43 (sha1_block_fn *)sha1_ce_transform);
Ard Biesheuvel2c988332014-03-06 16:23:33 +080044 kernel_neon_end();
45
Ard Biesheuvel2c988332014-03-06 16:23:33 +080046 return 0;
47}
48
Ard Biesheuvel07eb54d2015-04-09 12:55:44 +020049static int sha1_ce_finup(struct shash_desc *desc, const u8 *data,
50 unsigned int len, u8 *out)
Ard Biesheuvel2c988332014-03-06 16:23:33 +080051{
Ard Biesheuvel07eb54d2015-04-09 12:55:44 +020052 struct sha1_ce_state *sctx = shash_desc_ctx(desc);
53 bool finalize = !sctx->sst.count && !(len % SHA1_BLOCK_SIZE);
Ard Biesheuvel2c988332014-03-06 16:23:33 +080054
Ard Biesheuvel07eb54d2015-04-09 12:55:44 +020055 ASM_EXPORT(sha1_ce_offsetof_count,
56 offsetof(struct sha1_ce_state, sst.count));
57 ASM_EXPORT(sha1_ce_offsetof_finalize,
58 offsetof(struct sha1_ce_state, finalize));
59
60 /*
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.
63 */
64 sctx->finalize = finalize;
65
66 kernel_neon_begin_partial(16);
67 sha1_base_do_update(desc, data, len,
68 (sha1_block_fn *)sha1_ce_transform);
69 if (!finalize)
70 sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform);
71 kernel_neon_end();
72 return sha1_base_finish(desc, out);
Ard Biesheuvel2c988332014-03-06 16:23:33 +080073}
74
Ard Biesheuvel07eb54d2015-04-09 12:55:44 +020075static int sha1_ce_final(struct shash_desc *desc, u8 *out)
Ard Biesheuvel2c988332014-03-06 16:23:33 +080076{
Ard Biesheuvelbf7883e2015-05-06 15:54:31 +020077 struct sha1_ce_state *sctx = shash_desc_ctx(desc);
78
79 sctx->finalize = 0;
Ard Biesheuvel07eb54d2015-04-09 12:55:44 +020080 kernel_neon_begin_partial(16);
81 sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform);
82 kernel_neon_end();
83 return sha1_base_finish(desc, out);
Ard Biesheuvel2c988332014-03-06 16:23:33 +080084}
85
86static struct shash_alg alg = {
Ard Biesheuvel07eb54d2015-04-09 12:55:44 +020087 .init = sha1_base_init,
88 .update = sha1_ce_update,
89 .final = sha1_ce_final,
90 .finup = sha1_ce_finup,
91 .descsize = sizeof(struct sha1_ce_state),
Ard Biesheuvel2c988332014-03-06 16:23:33 +080092 .digestsize = SHA1_DIGEST_SIZE,
Ard Biesheuvel2c988332014-03-06 16:23:33 +080093 .base = {
94 .cra_name = "sha1",
95 .cra_driver_name = "sha1-ce",
96 .cra_priority = 200,
97 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
98 .cra_blocksize = SHA1_BLOCK_SIZE,
99 .cra_module = THIS_MODULE,
100 }
101};
102
103static int __init sha1_ce_mod_init(void)
104{
105 return crypto_register_shash(&alg);
106}
107
108static void __exit sha1_ce_mod_fini(void)
109{
110 crypto_unregister_shash(&alg);
111}
112
113module_cpu_feature_match(SHA1, sha1_ce_mod_init);
114module_exit(sha1_ce_mod_fini);