blob: 555f72b5e659bb5d6cff2de9e9215b551f8680d7 [file] [log] [blame]
Ard Biesheuvel864cbee2015-03-10 09:47:45 +01001/*
2 * sha1-ce-glue.c - SHA-1 secure hash 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>
Ard Biesheuveldde00982015-04-09 12:55:41 +020013#include <crypto/sha1_base.h>
Ard Biesheuvelbd56f952017-05-21 10:23:38 +000014#include <linux/cpufeature.h>
Ard Biesheuvel864cbee2015-03-10 09:47:45 +010015#include <linux/crypto.h>
16#include <linux/module.h>
17
Ard Biesheuvel864cbee2015-03-10 09:47:45 +010018#include <asm/hwcap.h>
19#include <asm/neon.h>
20#include <asm/simd.h>
Ard Biesheuvel864cbee2015-03-10 09:47:45 +010021
Ard Biesheuvel90451d62015-04-09 12:55:39 +020022#include "sha1.h"
23
Ard Biesheuvel864cbee2015-03-10 09:47:45 +010024MODULE_DESCRIPTION("SHA1 secure hash using ARMv8 Crypto Extensions");
25MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
26MODULE_LICENSE("GPL v2");
27
Ard Biesheuveldde00982015-04-09 12:55:41 +020028asmlinkage void sha1_ce_transform(struct sha1_state *sst, u8 const *src,
29 int blocks);
Ard Biesheuvel864cbee2015-03-10 09:47:45 +010030
Ard Biesheuveldde00982015-04-09 12:55:41 +020031static int sha1_ce_update(struct shash_desc *desc, const u8 *data,
32 unsigned int len)
Ard Biesheuvel864cbee2015-03-10 09:47:45 +010033{
34 struct sha1_state *sctx = shash_desc_ctx(desc);
35
Ard Biesheuveldde00982015-04-09 12:55:41 +020036 if (!may_use_simd() ||
37 (sctx->count % SHA1_BLOCK_SIZE) + len < SHA1_BLOCK_SIZE)
Ard Biesheuvel864cbee2015-03-10 09:47:45 +010038 return sha1_update_arm(desc, data, len);
39
Ard Biesheuveldde00982015-04-09 12:55:41 +020040 kernel_neon_begin();
41 sha1_base_do_update(desc, data, len, sha1_ce_transform);
42 kernel_neon_end();
Ard Biesheuvel864cbee2015-03-10 09:47:45 +010043
Ard Biesheuveldde00982015-04-09 12:55:41 +020044 return 0;
45}
Ard Biesheuvel864cbee2015-03-10 09:47:45 +010046
Ard Biesheuveldde00982015-04-09 12:55:41 +020047static int sha1_ce_finup(struct shash_desc *desc, const u8 *data,
48 unsigned int len, u8 *out)
49{
50 if (!may_use_simd())
51 return sha1_finup_arm(desc, data, len, out);
Ard Biesheuvel864cbee2015-03-10 09:47:45 +010052
Ard Biesheuveldde00982015-04-09 12:55:41 +020053 kernel_neon_begin();
Ard Biesheuvel864cbee2015-03-10 09:47:45 +010054 if (len)
Ard Biesheuveldde00982015-04-09 12:55:41 +020055 sha1_base_do_update(desc, data, len, sha1_ce_transform);
56 sha1_base_do_finalize(desc, sha1_ce_transform);
57 kernel_neon_end();
58
59 return sha1_base_finish(desc, out);
Ard Biesheuvel864cbee2015-03-10 09:47:45 +010060}
61
Ard Biesheuveldde00982015-04-09 12:55:41 +020062static int sha1_ce_final(struct shash_desc *desc, u8 *out)
Ard Biesheuvel864cbee2015-03-10 09:47:45 +010063{
Ard Biesheuveldde00982015-04-09 12:55:41 +020064 return sha1_ce_finup(desc, NULL, 0, out);
Ard Biesheuvel864cbee2015-03-10 09:47:45 +010065}
66
67static struct shash_alg alg = {
Ard Biesheuveldde00982015-04-09 12:55:41 +020068 .init = sha1_base_init,
69 .update = sha1_ce_update,
70 .final = sha1_ce_final,
71 .finup = sha1_ce_finup,
Ard Biesheuvel864cbee2015-03-10 09:47:45 +010072 .descsize = sizeof(struct sha1_state),
73 .digestsize = SHA1_DIGEST_SIZE,
Ard Biesheuvel864cbee2015-03-10 09:47:45 +010074 .base = {
75 .cra_name = "sha1",
76 .cra_driver_name = "sha1-ce",
77 .cra_priority = 200,
78 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
79 .cra_blocksize = SHA1_BLOCK_SIZE,
80 .cra_module = THIS_MODULE,
81 }
82};
83
84static int __init sha1_ce_mod_init(void)
85{
Ard Biesheuvel864cbee2015-03-10 09:47:45 +010086 return crypto_register_shash(&alg);
87}
88
89static void __exit sha1_ce_mod_fini(void)
90{
91 crypto_unregister_shash(&alg);
92}
93
Ard Biesheuvelbd56f952017-05-21 10:23:38 +000094module_cpu_feature_match(SHA1, sha1_ce_mod_init);
Ard Biesheuvel864cbee2015-03-10 09:47:45 +010095module_exit(sha1_ce_mod_fini);