blob: 0755b2d657f347847cc48eab55d5ce01c38bb76f [file] [log] [blame]
Ard Biesheuvel006d0622015-03-10 09:47:46 +01001/*
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>
Ard Biesheuvel9205b942015-04-09 12:55:43 +020013#include <crypto/sha256_base.h>
Ard Biesheuvel006d0622015-03-10 09:47:46 +010014#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
Ard Biesheuvel9205b942015-04-09 12:55:43 +020022#include "sha256_glue.h"
23
Ard Biesheuvel006d0622015-03-10 09:47:46 +010024MODULE_DESCRIPTION("SHA-224/SHA-256 secure hash using ARMv8 Crypto Extensions");
25MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
26MODULE_LICENSE("GPL v2");
27
Ard Biesheuvel9205b942015-04-09 12:55:43 +020028asmlinkage void sha2_ce_transform(struct sha256_state *sst, u8 const *src,
29 int blocks);
Ard Biesheuvel006d0622015-03-10 09:47:46 +010030
Ard Biesheuvel9205b942015-04-09 12:55:43 +020031static int sha2_ce_update(struct shash_desc *desc, const u8 *data,
32 unsigned int len)
Ard Biesheuvel006d0622015-03-10 09:47:46 +010033{
34 struct sha256_state *sctx = shash_desc_ctx(desc);
35
Ard Biesheuvel9205b942015-04-09 12:55:43 +020036 if (!may_use_simd() ||
37 (sctx->count % SHA256_BLOCK_SIZE) + len < SHA256_BLOCK_SIZE)
38 return crypto_sha256_arm_update(desc, data, len);
39
40 kernel_neon_begin();
41 sha256_base_do_update(desc, data, len,
42 (sha256_block_fn *)sha2_ce_transform);
43 kernel_neon_end();
44
Ard Biesheuvel006d0622015-03-10 09:47:46 +010045 return 0;
46}
47
Ard Biesheuvel9205b942015-04-09 12:55:43 +020048static int sha2_ce_finup(struct shash_desc *desc, const u8 *data,
49 unsigned int len, u8 *out)
Ard Biesheuvel006d0622015-03-10 09:47:46 +010050{
Ard Biesheuvel006d0622015-03-10 09:47:46 +010051 if (!may_use_simd())
Ard Biesheuvel9205b942015-04-09 12:55:43 +020052 return crypto_sha256_arm_finup(desc, data, len, out);
Ard Biesheuvel006d0622015-03-10 09:47:46 +010053
Ard Biesheuvel9205b942015-04-09 12:55:43 +020054 kernel_neon_begin();
Ard Biesheuvel006d0622015-03-10 09:47:46 +010055 if (len)
Ard Biesheuvel9205b942015-04-09 12:55:43 +020056 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();
60
61 return sha256_base_finish(desc, out);
Ard Biesheuvel006d0622015-03-10 09:47:46 +010062}
63
Ard Biesheuvel9205b942015-04-09 12:55:43 +020064static int sha2_ce_final(struct shash_desc *desc, u8 *out)
Ard Biesheuvel006d0622015-03-10 09:47:46 +010065{
Ard Biesheuvel9205b942015-04-09 12:55:43 +020066 return sha2_ce_finup(desc, NULL, 0, out);
Ard Biesheuvel006d0622015-03-10 09:47:46 +010067}
68
69static struct shash_alg algs[] = { {
Ard Biesheuvel9205b942015-04-09 12:55:43 +020070 .init = sha224_base_init,
71 .update = sha2_ce_update,
72 .final = sha2_ce_final,
73 .finup = sha2_ce_finup,
Ard Biesheuvel006d0622015-03-10 09:47:46 +010074 .descsize = sizeof(struct sha256_state),
75 .digestsize = SHA224_DIGEST_SIZE,
Ard Biesheuvel006d0622015-03-10 09:47:46 +010076 .base = {
77 .cra_name = "sha224",
78 .cra_driver_name = "sha224-ce",
Sami Tolvanenf2f770d2015-04-03 18:03:40 +080079 .cra_priority = 300,
Ard Biesheuvel006d0622015-03-10 09:47:46 +010080 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
81 .cra_blocksize = SHA256_BLOCK_SIZE,
82 .cra_module = THIS_MODULE,
83 }
84}, {
Ard Biesheuvel9205b942015-04-09 12:55:43 +020085 .init = sha256_base_init,
86 .update = sha2_ce_update,
87 .final = sha2_ce_final,
88 .finup = sha2_ce_finup,
Ard Biesheuvel006d0622015-03-10 09:47:46 +010089 .descsize = sizeof(struct sha256_state),
90 .digestsize = SHA256_DIGEST_SIZE,
Ard Biesheuvel006d0622015-03-10 09:47:46 +010091 .base = {
92 .cra_name = "sha256",
93 .cra_driver_name = "sha256-ce",
Sami Tolvanenf2f770d2015-04-03 18:03:40 +080094 .cra_priority = 300,
Ard Biesheuvel006d0622015-03-10 09:47:46 +010095 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
96 .cra_blocksize = SHA256_BLOCK_SIZE,
97 .cra_module = THIS_MODULE,
98 }
99} };
100
101static 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
108static void __exit sha2_ce_mod_fini(void)
109{
110 crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
111}
112
113module_init(sha2_ce_mod_init);
114module_exit(sha2_ce_mod_fini);