12c98833aSArd Biesheuvel /* 22c98833aSArd Biesheuvel * sha1-ce-glue.c - SHA-1 secure hash using ARMv8 Crypto Extensions 32c98833aSArd Biesheuvel * 4*0771f323SArd Biesheuvel * Copyright (C) 2014 - 2017 Linaro Ltd <ard.biesheuvel@linaro.org> 52c98833aSArd Biesheuvel * 62c98833aSArd Biesheuvel * This program is free software; you can redistribute it and/or modify 72c98833aSArd Biesheuvel * it under the terms of the GNU General Public License version 2 as 82c98833aSArd Biesheuvel * published by the Free Software Foundation. 92c98833aSArd Biesheuvel */ 102c98833aSArd Biesheuvel 112c98833aSArd Biesheuvel #include <asm/neon.h> 12*0771f323SArd Biesheuvel #include <asm/simd.h> 132c98833aSArd Biesheuvel #include <asm/unaligned.h> 142c98833aSArd Biesheuvel #include <crypto/internal/hash.h> 152c98833aSArd Biesheuvel #include <crypto/sha.h> 1607eb54d3SArd Biesheuvel #include <crypto/sha1_base.h> 172c98833aSArd Biesheuvel #include <linux/cpufeature.h> 182c98833aSArd Biesheuvel #include <linux/crypto.h> 192c98833aSArd Biesheuvel #include <linux/module.h> 202c98833aSArd Biesheuvel 212c98833aSArd Biesheuvel MODULE_DESCRIPTION("SHA1 secure hash using ARMv8 Crypto Extensions"); 222c98833aSArd Biesheuvel MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>"); 232c98833aSArd Biesheuvel MODULE_LICENSE("GPL v2"); 242c98833aSArd Biesheuvel 2507eb54d3SArd Biesheuvel struct sha1_ce_state { 2607eb54d3SArd Biesheuvel struct sha1_state sst; 2707eb54d3SArd Biesheuvel u32 finalize; 282c98833aSArd Biesheuvel }; 292c98833aSArd Biesheuvel 3007eb54d3SArd Biesheuvel asmlinkage void sha1_ce_transform(struct sha1_ce_state *sst, u8 const *src, 3107eb54d3SArd Biesheuvel int blocks); 3207eb54d3SArd Biesheuvel 33f4857f4cSArd Biesheuvel const u32 sha1_ce_offsetof_count = offsetof(struct sha1_ce_state, sst.count); 34f4857f4cSArd Biesheuvel const u32 sha1_ce_offsetof_finalize = offsetof(struct sha1_ce_state, finalize); 35f4857f4cSArd Biesheuvel 3607eb54d3SArd Biesheuvel static int sha1_ce_update(struct shash_desc *desc, const u8 *data, 372c98833aSArd Biesheuvel unsigned int len) 382c98833aSArd Biesheuvel { 3907eb54d3SArd Biesheuvel struct sha1_ce_state *sctx = shash_desc_ctx(desc); 402c98833aSArd Biesheuvel 41*0771f323SArd Biesheuvel if (!may_use_simd()) 42*0771f323SArd Biesheuvel return crypto_sha1_update(desc, data, len); 43*0771f323SArd Biesheuvel 4407eb54d3SArd Biesheuvel sctx->finalize = 0; 45*0771f323SArd Biesheuvel kernel_neon_begin(); 4607eb54d3SArd Biesheuvel sha1_base_do_update(desc, data, len, 4707eb54d3SArd Biesheuvel (sha1_block_fn *)sha1_ce_transform); 482c98833aSArd Biesheuvel kernel_neon_end(); 492c98833aSArd Biesheuvel 502c98833aSArd Biesheuvel return 0; 512c98833aSArd Biesheuvel } 522c98833aSArd Biesheuvel 5307eb54d3SArd Biesheuvel static int sha1_ce_finup(struct shash_desc *desc, const u8 *data, 542c98833aSArd Biesheuvel unsigned int len, u8 *out) 552c98833aSArd Biesheuvel { 5607eb54d3SArd Biesheuvel struct sha1_ce_state *sctx = shash_desc_ctx(desc); 5707eb54d3SArd Biesheuvel bool finalize = !sctx->sst.count && !(len % SHA1_BLOCK_SIZE); 582c98833aSArd Biesheuvel 59*0771f323SArd Biesheuvel if (!may_use_simd()) 60*0771f323SArd Biesheuvel return crypto_sha1_finup(desc, data, len, out); 61*0771f323SArd Biesheuvel 622c98833aSArd Biesheuvel /* 6307eb54d3SArd Biesheuvel * Allow the asm code to perform the finalization if there is no 6407eb54d3SArd Biesheuvel * partial data and the input is a round multiple of the block size. 652c98833aSArd Biesheuvel */ 6607eb54d3SArd Biesheuvel sctx->finalize = finalize; 672c98833aSArd Biesheuvel 68*0771f323SArd Biesheuvel kernel_neon_begin(); 6907eb54d3SArd Biesheuvel sha1_base_do_update(desc, data, len, 7007eb54d3SArd Biesheuvel (sha1_block_fn *)sha1_ce_transform); 7107eb54d3SArd Biesheuvel if (!finalize) 7207eb54d3SArd Biesheuvel sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform); 732c98833aSArd Biesheuvel kernel_neon_end(); 7407eb54d3SArd Biesheuvel return sha1_base_finish(desc, out); 752c98833aSArd Biesheuvel } 762c98833aSArd Biesheuvel 7707eb54d3SArd Biesheuvel static int sha1_ce_final(struct shash_desc *desc, u8 *out) 782c98833aSArd Biesheuvel { 79bf7883ebSArd Biesheuvel struct sha1_ce_state *sctx = shash_desc_ctx(desc); 80bf7883ebSArd Biesheuvel 81*0771f323SArd Biesheuvel if (!may_use_simd()) 82*0771f323SArd Biesheuvel return crypto_sha1_finup(desc, NULL, 0, out); 83*0771f323SArd Biesheuvel 84bf7883ebSArd Biesheuvel sctx->finalize = 0; 85*0771f323SArd Biesheuvel kernel_neon_begin(); 8607eb54d3SArd Biesheuvel sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform); 8707eb54d3SArd Biesheuvel kernel_neon_end(); 8807eb54d3SArd Biesheuvel return sha1_base_finish(desc, out); 892c98833aSArd Biesheuvel } 902c98833aSArd Biesheuvel 912c98833aSArd Biesheuvel static struct shash_alg alg = { 9207eb54d3SArd Biesheuvel .init = sha1_base_init, 9307eb54d3SArd Biesheuvel .update = sha1_ce_update, 9407eb54d3SArd Biesheuvel .final = sha1_ce_final, 9507eb54d3SArd Biesheuvel .finup = sha1_ce_finup, 9607eb54d3SArd Biesheuvel .descsize = sizeof(struct sha1_ce_state), 972c98833aSArd Biesheuvel .digestsize = SHA1_DIGEST_SIZE, 982c98833aSArd Biesheuvel .base = { 992c98833aSArd Biesheuvel .cra_name = "sha1", 1002c98833aSArd Biesheuvel .cra_driver_name = "sha1-ce", 1012c98833aSArd Biesheuvel .cra_priority = 200, 1022c98833aSArd Biesheuvel .cra_flags = CRYPTO_ALG_TYPE_SHASH, 1032c98833aSArd Biesheuvel .cra_blocksize = SHA1_BLOCK_SIZE, 1042c98833aSArd Biesheuvel .cra_module = THIS_MODULE, 1052c98833aSArd Biesheuvel } 1062c98833aSArd Biesheuvel }; 1072c98833aSArd Biesheuvel 1082c98833aSArd Biesheuvel static int __init sha1_ce_mod_init(void) 1092c98833aSArd Biesheuvel { 1102c98833aSArd Biesheuvel return crypto_register_shash(&alg); 1112c98833aSArd Biesheuvel } 1122c98833aSArd Biesheuvel 1132c98833aSArd Biesheuvel static void __exit sha1_ce_mod_fini(void) 1142c98833aSArd Biesheuvel { 1152c98833aSArd Biesheuvel crypto_unregister_shash(&alg); 1162c98833aSArd Biesheuvel } 1172c98833aSArd Biesheuvel 1182c98833aSArd Biesheuvel module_cpu_feature_match(SHA1, sha1_ce_mod_init); 1192c98833aSArd Biesheuvel module_exit(sha1_ce_mod_fini); 120