Jussi Kivilinna | 6046825 | 2014-07-29 17:14:14 +0100 | [diff] [blame] | 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 <linux/init.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/mm.h> |
| 25 | #include <linux/cryptohash.h> |
| 26 | #include <linux/types.h> |
| 27 | #include <crypto/sha.h> |
Ard Biesheuvel | 51e515f | 2015-04-09 12:55:40 +0200 | [diff] [blame] | 28 | #include <crypto/sha1_base.h> |
Jussi Kivilinna | 6046825 | 2014-07-29 17:14:14 +0100 | [diff] [blame] | 29 | #include <asm/neon.h> |
| 30 | #include <asm/simd.h> |
Jussi Kivilinna | 6046825 | 2014-07-29 17:14:14 +0100 | [diff] [blame] | 31 | |
Ard Biesheuvel | 90451d6 | 2015-04-09 12:55:39 +0200 | [diff] [blame] | 32 | #include "sha1.h" |
Jussi Kivilinna | 6046825 | 2014-07-29 17:14:14 +0100 | [diff] [blame] | 33 | |
| 34 | asmlinkage void sha1_transform_neon(void *state_h, const char *data, |
| 35 | unsigned int rounds); |
| 36 | |
Jussi Kivilinna | 6046825 | 2014-07-29 17:14:14 +0100 | [diff] [blame] | 37 | static int sha1_neon_update(struct shash_desc *desc, const u8 *data, |
Ard Biesheuvel | 51e515f | 2015-04-09 12:55:40 +0200 | [diff] [blame] | 38 | unsigned int len) |
Jussi Kivilinna | 6046825 | 2014-07-29 17:14:14 +0100 | [diff] [blame] | 39 | { |
| 40 | struct sha1_state *sctx = shash_desc_ctx(desc); |
Jussi Kivilinna | 6046825 | 2014-07-29 17:14:14 +0100 | [diff] [blame] | 41 | |
Ard Biesheuvel | 51e515f | 2015-04-09 12:55:40 +0200 | [diff] [blame] | 42 | if (!may_use_simd() || |
| 43 | (sctx->count % SHA1_BLOCK_SIZE) + len < SHA1_BLOCK_SIZE) |
| 44 | return sha1_update_arm(desc, data, len); |
Jussi Kivilinna | 6046825 | 2014-07-29 17:14:14 +0100 | [diff] [blame] | 45 | |
Ard Biesheuvel | 51e515f | 2015-04-09 12:55:40 +0200 | [diff] [blame] | 46 | kernel_neon_begin(); |
| 47 | sha1_base_do_update(desc, data, len, |
| 48 | (sha1_block_fn *)sha1_transform_neon); |
| 49 | kernel_neon_end(); |
Jussi Kivilinna | 6046825 | 2014-07-29 17:14:14 +0100 | [diff] [blame] | 50 | |
Ard Biesheuvel | 51e515f | 2015-04-09 12:55:40 +0200 | [diff] [blame] | 51 | return 0; |
Jussi Kivilinna | 6046825 | 2014-07-29 17:14:14 +0100 | [diff] [blame] | 52 | } |
| 53 | |
Ard Biesheuvel | 51e515f | 2015-04-09 12:55:40 +0200 | [diff] [blame] | 54 | static int sha1_neon_finup(struct shash_desc *desc, const u8 *data, |
| 55 | unsigned int len, u8 *out) |
| 56 | { |
| 57 | if (!may_use_simd()) |
| 58 | return sha1_finup_arm(desc, data, len, out); |
Jussi Kivilinna | 6046825 | 2014-07-29 17:14:14 +0100 | [diff] [blame] | 59 | |
Ard Biesheuvel | 51e515f | 2015-04-09 12:55:40 +0200 | [diff] [blame] | 60 | kernel_neon_begin(); |
| 61 | if (len) |
| 62 | sha1_base_do_update(desc, data, len, |
| 63 | (sha1_block_fn *)sha1_transform_neon); |
| 64 | sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_transform_neon); |
| 65 | kernel_neon_end(); |
| 66 | |
| 67 | return sha1_base_finish(desc, out); |
| 68 | } |
| 69 | |
Jussi Kivilinna | 6046825 | 2014-07-29 17:14:14 +0100 | [diff] [blame] | 70 | static int sha1_neon_final(struct shash_desc *desc, u8 *out) |
| 71 | { |
Ard Biesheuvel | 51e515f | 2015-04-09 12:55:40 +0200 | [diff] [blame] | 72 | return sha1_neon_finup(desc, NULL, 0, out); |
Jussi Kivilinna | 6046825 | 2014-07-29 17:14:14 +0100 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static struct shash_alg alg = { |
| 76 | .digestsize = SHA1_DIGEST_SIZE, |
Ard Biesheuvel | 51e515f | 2015-04-09 12:55:40 +0200 | [diff] [blame] | 77 | .init = sha1_base_init, |
Jussi Kivilinna | 6046825 | 2014-07-29 17:14:14 +0100 | [diff] [blame] | 78 | .update = sha1_neon_update, |
| 79 | .final = sha1_neon_final, |
Ard Biesheuvel | 51e515f | 2015-04-09 12:55:40 +0200 | [diff] [blame] | 80 | .finup = sha1_neon_finup, |
Jussi Kivilinna | 6046825 | 2014-07-29 17:14:14 +0100 | [diff] [blame] | 81 | .descsize = sizeof(struct sha1_state), |
Jussi Kivilinna | 6046825 | 2014-07-29 17:14:14 +0100 | [diff] [blame] | 82 | .base = { |
| 83 | .cra_name = "sha1", |
| 84 | .cra_driver_name = "sha1-neon", |
| 85 | .cra_priority = 250, |
| 86 | .cra_flags = CRYPTO_ALG_TYPE_SHASH, |
| 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"); |
Kees Cook | 5d26a10 | 2014-11-20 17:05:53 -0800 | [diff] [blame] | 110 | MODULE_ALIAS_CRYPTO("sha1"); |