1 /* 2 * Glue code for the SHA1 Secure Hash Algorithm assembler implementation using 3 * ARM NEON instructions. 4 * 5 * Copyright © 2014 Jussi Kivilinna <jussi.kivilinna@iki.fi> 6 * 7 * This file is based on sha1_generic.c and sha1_ssse3_glue.c: 8 * Copyright (c) Alan Smithee. 9 * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk> 10 * Copyright (c) Jean-Francois Dive <jef@linuxbe.org> 11 * Copyright (c) Mathias Krause <minipli@googlemail.com> 12 * Copyright (c) Chandramouli Narayanan <mouli@linux.intel.com> 13 * 14 * This program is free software; you can redistribute it and/or modify it 15 * under the terms of the GNU General Public License as published by the Free 16 * Software Foundation; either version 2 of the License, or (at your option) 17 * any later version. 18 * 19 */ 20 21 #include <crypto/internal/hash.h> 22 #include <crypto/internal/simd.h> 23 #include <linux/init.h> 24 #include <linux/module.h> 25 #include <linux/mm.h> 26 #include <linux/cryptohash.h> 27 #include <linux/types.h> 28 #include <crypto/sha.h> 29 #include <crypto/sha1_base.h> 30 #include <asm/neon.h> 31 #include <asm/simd.h> 32 33 #include "sha1.h" 34 35 asmlinkage void sha1_transform_neon(void *state_h, const char *data, 36 unsigned int rounds); 37 38 static int sha1_neon_update(struct shash_desc *desc, const u8 *data, 39 unsigned int len) 40 { 41 struct sha1_state *sctx = shash_desc_ctx(desc); 42 43 if (!crypto_simd_usable() || 44 (sctx->count % SHA1_BLOCK_SIZE) + len < SHA1_BLOCK_SIZE) 45 return sha1_update_arm(desc, data, len); 46 47 kernel_neon_begin(); 48 sha1_base_do_update(desc, data, len, 49 (sha1_block_fn *)sha1_transform_neon); 50 kernel_neon_end(); 51 52 return 0; 53 } 54 55 static int sha1_neon_finup(struct shash_desc *desc, const u8 *data, 56 unsigned int len, u8 *out) 57 { 58 if (!crypto_simd_usable()) 59 return sha1_finup_arm(desc, data, len, out); 60 61 kernel_neon_begin(); 62 if (len) 63 sha1_base_do_update(desc, data, len, 64 (sha1_block_fn *)sha1_transform_neon); 65 sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_transform_neon); 66 kernel_neon_end(); 67 68 return sha1_base_finish(desc, out); 69 } 70 71 static int sha1_neon_final(struct shash_desc *desc, u8 *out) 72 { 73 return sha1_neon_finup(desc, NULL, 0, out); 74 } 75 76 static struct shash_alg alg = { 77 .digestsize = SHA1_DIGEST_SIZE, 78 .init = sha1_base_init, 79 .update = sha1_neon_update, 80 .final = sha1_neon_final, 81 .finup = sha1_neon_finup, 82 .descsize = sizeof(struct sha1_state), 83 .base = { 84 .cra_name = "sha1", 85 .cra_driver_name = "sha1-neon", 86 .cra_priority = 250, 87 .cra_blocksize = SHA1_BLOCK_SIZE, 88 .cra_module = THIS_MODULE, 89 } 90 }; 91 92 static int __init sha1_neon_mod_init(void) 93 { 94 if (!cpu_has_neon()) 95 return -ENODEV; 96 97 return crypto_register_shash(&alg); 98 } 99 100 static void __exit sha1_neon_mod_fini(void) 101 { 102 crypto_unregister_shash(&alg); 103 } 104 105 module_init(sha1_neon_mod_init); 106 module_exit(sha1_neon_mod_fini); 107 108 MODULE_LICENSE("GPL"); 109 MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm, NEON accelerated"); 110 MODULE_ALIAS_CRYPTO("sha1"); 111