12c98833aSArd Biesheuvel /* 22c98833aSArd Biesheuvel * sha1-ce-glue.c - SHA-1 secure hash using ARMv8 Crypto Extensions 32c98833aSArd Biesheuvel * 42c98833aSArd Biesheuvel * Copyright (C) 2014 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> 122c98833aSArd Biesheuvel #include <asm/unaligned.h> 132c98833aSArd Biesheuvel #include <crypto/internal/hash.h> 142c98833aSArd Biesheuvel #include <crypto/sha.h> 1507eb54d3SArd Biesheuvel #include <crypto/sha1_base.h> 162c98833aSArd Biesheuvel #include <linux/cpufeature.h> 172c98833aSArd Biesheuvel #include <linux/crypto.h> 182c98833aSArd Biesheuvel #include <linux/module.h> 192c98833aSArd Biesheuvel 2007eb54d3SArd Biesheuvel #define ASM_EXPORT(sym, val) \ 2107eb54d3SArd Biesheuvel asm(".globl " #sym "; .set " #sym ", %0" :: "I"(val)); 2207eb54d3SArd Biesheuvel 232c98833aSArd Biesheuvel MODULE_DESCRIPTION("SHA1 secure hash using ARMv8 Crypto Extensions"); 242c98833aSArd Biesheuvel MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>"); 252c98833aSArd Biesheuvel MODULE_LICENSE("GPL v2"); 262c98833aSArd Biesheuvel 2707eb54d3SArd Biesheuvel struct sha1_ce_state { 2807eb54d3SArd Biesheuvel struct sha1_state sst; 2907eb54d3SArd Biesheuvel u32 finalize; 302c98833aSArd Biesheuvel }; 312c98833aSArd Biesheuvel 3207eb54d3SArd Biesheuvel asmlinkage void sha1_ce_transform(struct sha1_ce_state *sst, u8 const *src, 3307eb54d3SArd Biesheuvel int blocks); 3407eb54d3SArd Biesheuvel 3507eb54d3SArd Biesheuvel static int sha1_ce_update(struct shash_desc *desc, const u8 *data, 362c98833aSArd Biesheuvel unsigned int len) 372c98833aSArd Biesheuvel { 3807eb54d3SArd Biesheuvel struct sha1_ce_state *sctx = shash_desc_ctx(desc); 392c98833aSArd Biesheuvel 4007eb54d3SArd Biesheuvel sctx->finalize = 0; 412c98833aSArd Biesheuvel kernel_neon_begin_partial(16); 4207eb54d3SArd Biesheuvel sha1_base_do_update(desc, data, len, 4307eb54d3SArd Biesheuvel (sha1_block_fn *)sha1_ce_transform); 442c98833aSArd Biesheuvel kernel_neon_end(); 452c98833aSArd Biesheuvel 462c98833aSArd Biesheuvel return 0; 472c98833aSArd Biesheuvel } 482c98833aSArd Biesheuvel 4907eb54d3SArd Biesheuvel static int sha1_ce_finup(struct shash_desc *desc, const u8 *data, 502c98833aSArd Biesheuvel unsigned int len, u8 *out) 512c98833aSArd Biesheuvel { 5207eb54d3SArd Biesheuvel struct sha1_ce_state *sctx = shash_desc_ctx(desc); 5307eb54d3SArd Biesheuvel bool finalize = !sctx->sst.count && !(len % SHA1_BLOCK_SIZE); 542c98833aSArd Biesheuvel 5507eb54d3SArd Biesheuvel ASM_EXPORT(sha1_ce_offsetof_count, 5607eb54d3SArd Biesheuvel offsetof(struct sha1_ce_state, sst.count)); 5707eb54d3SArd Biesheuvel ASM_EXPORT(sha1_ce_offsetof_finalize, 5807eb54d3SArd Biesheuvel offsetof(struct sha1_ce_state, finalize)); 592c98833aSArd Biesheuvel 602c98833aSArd Biesheuvel /* 6107eb54d3SArd Biesheuvel * Allow the asm code to perform the finalization if there is no 6207eb54d3SArd Biesheuvel * partial data and the input is a round multiple of the block size. 632c98833aSArd Biesheuvel */ 6407eb54d3SArd Biesheuvel sctx->finalize = finalize; 652c98833aSArd Biesheuvel 662c98833aSArd Biesheuvel kernel_neon_begin_partial(16); 6707eb54d3SArd Biesheuvel sha1_base_do_update(desc, data, len, 6807eb54d3SArd Biesheuvel (sha1_block_fn *)sha1_ce_transform); 6907eb54d3SArd Biesheuvel if (!finalize) 7007eb54d3SArd Biesheuvel sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform); 712c98833aSArd Biesheuvel kernel_neon_end(); 7207eb54d3SArd Biesheuvel return sha1_base_finish(desc, out); 732c98833aSArd Biesheuvel } 742c98833aSArd Biesheuvel 7507eb54d3SArd Biesheuvel static int sha1_ce_final(struct shash_desc *desc, u8 *out) 762c98833aSArd Biesheuvel { 77*bf7883ebSArd Biesheuvel struct sha1_ce_state *sctx = shash_desc_ctx(desc); 78*bf7883ebSArd Biesheuvel 79*bf7883ebSArd Biesheuvel sctx->finalize = 0; 8007eb54d3SArd Biesheuvel kernel_neon_begin_partial(16); 8107eb54d3SArd Biesheuvel sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform); 8207eb54d3SArd Biesheuvel kernel_neon_end(); 8307eb54d3SArd Biesheuvel return sha1_base_finish(desc, out); 842c98833aSArd Biesheuvel } 852c98833aSArd Biesheuvel 862c98833aSArd Biesheuvel static struct shash_alg alg = { 8707eb54d3SArd Biesheuvel .init = sha1_base_init, 8807eb54d3SArd Biesheuvel .update = sha1_ce_update, 8907eb54d3SArd Biesheuvel .final = sha1_ce_final, 9007eb54d3SArd Biesheuvel .finup = sha1_ce_finup, 9107eb54d3SArd Biesheuvel .descsize = sizeof(struct sha1_ce_state), 922c98833aSArd Biesheuvel .digestsize = SHA1_DIGEST_SIZE, 932c98833aSArd Biesheuvel .base = { 942c98833aSArd Biesheuvel .cra_name = "sha1", 952c98833aSArd Biesheuvel .cra_driver_name = "sha1-ce", 962c98833aSArd Biesheuvel .cra_priority = 200, 972c98833aSArd Biesheuvel .cra_flags = CRYPTO_ALG_TYPE_SHASH, 982c98833aSArd Biesheuvel .cra_blocksize = SHA1_BLOCK_SIZE, 992c98833aSArd Biesheuvel .cra_module = THIS_MODULE, 1002c98833aSArd Biesheuvel } 1012c98833aSArd Biesheuvel }; 1022c98833aSArd Biesheuvel 1032c98833aSArd Biesheuvel static int __init sha1_ce_mod_init(void) 1042c98833aSArd Biesheuvel { 1052c98833aSArd Biesheuvel return crypto_register_shash(&alg); 1062c98833aSArd Biesheuvel } 1072c98833aSArd Biesheuvel 1082c98833aSArd Biesheuvel static void __exit sha1_ce_mod_fini(void) 1092c98833aSArd Biesheuvel { 1102c98833aSArd Biesheuvel crypto_unregister_shash(&alg); 1112c98833aSArd Biesheuvel } 1122c98833aSArd Biesheuvel 1132c98833aSArd Biesheuvel module_cpu_feature_match(SHA1, sha1_ce_mod_init); 1142c98833aSArd Biesheuvel module_exit(sha1_ce_mod_fini); 115