1 /* 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> 13 #include <crypto/sha256_base.h> 14 #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 22 #include "sha256_glue.h" 23 24 MODULE_DESCRIPTION("SHA-224/SHA-256 secure hash using ARMv8 Crypto Extensions"); 25 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>"); 26 MODULE_LICENSE("GPL v2"); 27 28 asmlinkage void sha2_ce_transform(struct sha256_state *sst, u8 const *src, 29 int blocks); 30 31 static int sha2_ce_update(struct shash_desc *desc, const u8 *data, 32 unsigned int len) 33 { 34 struct sha256_state *sctx = shash_desc_ctx(desc); 35 36 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 45 return 0; 46 } 47 48 static int sha2_ce_finup(struct shash_desc *desc, const u8 *data, 49 unsigned int len, u8 *out) 50 { 51 if (!may_use_simd()) 52 return crypto_sha256_arm_finup(desc, data, len, out); 53 54 kernel_neon_begin(); 55 if (len) 56 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); 62 } 63 64 static int sha2_ce_final(struct shash_desc *desc, u8 *out) 65 { 66 return sha2_ce_finup(desc, NULL, 0, out); 67 } 68 69 static struct shash_alg algs[] = { { 70 .init = sha224_base_init, 71 .update = sha2_ce_update, 72 .final = sha2_ce_final, 73 .finup = sha2_ce_finup, 74 .descsize = sizeof(struct sha256_state), 75 .digestsize = SHA224_DIGEST_SIZE, 76 .base = { 77 .cra_name = "sha224", 78 .cra_driver_name = "sha224-ce", 79 .cra_priority = 300, 80 .cra_flags = CRYPTO_ALG_TYPE_SHASH, 81 .cra_blocksize = SHA256_BLOCK_SIZE, 82 .cra_module = THIS_MODULE, 83 } 84 }, { 85 .init = sha256_base_init, 86 .update = sha2_ce_update, 87 .final = sha2_ce_final, 88 .finup = sha2_ce_finup, 89 .descsize = sizeof(struct sha256_state), 90 .digestsize = SHA256_DIGEST_SIZE, 91 .base = { 92 .cra_name = "sha256", 93 .cra_driver_name = "sha256-ce", 94 .cra_priority = 300, 95 .cra_flags = CRYPTO_ALG_TYPE_SHASH, 96 .cra_blocksize = SHA256_BLOCK_SIZE, 97 .cra_module = THIS_MODULE, 98 } 99 } }; 100 101 static 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 108 static void __exit sha2_ce_mod_fini(void) 109 { 110 crypto_unregister_shashes(algs, ARRAY_SIZE(algs)); 111 } 112 113 module_init(sha2_ce_mod_init); 114 module_exit(sha2_ce_mod_fini); 115