blob: 4e22f122f96661e362b2c9e5703d25469e063ce7 [file] [log] [blame]
Jussi Kivilinna60468252014-07-29 17:14:14 +01001/*
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 Biesheuvel51e515f2015-04-09 12:55:40 +020028#include <crypto/sha1_base.h>
Jussi Kivilinna60468252014-07-29 17:14:14 +010029#include <asm/neon.h>
30#include <asm/simd.h>
Jussi Kivilinna60468252014-07-29 17:14:14 +010031
Ard Biesheuvel90451d62015-04-09 12:55:39 +020032#include "sha1.h"
Jussi Kivilinna60468252014-07-29 17:14:14 +010033
34asmlinkage void sha1_transform_neon(void *state_h, const char *data,
35 unsigned int rounds);
36
Jussi Kivilinna60468252014-07-29 17:14:14 +010037static int sha1_neon_update(struct shash_desc *desc, const u8 *data,
Ard Biesheuvel51e515f2015-04-09 12:55:40 +020038 unsigned int len)
Jussi Kivilinna60468252014-07-29 17:14:14 +010039{
40 struct sha1_state *sctx = shash_desc_ctx(desc);
Jussi Kivilinna60468252014-07-29 17:14:14 +010041
Ard Biesheuvel51e515f2015-04-09 12:55:40 +020042 if (!may_use_simd() ||
43 (sctx->count % SHA1_BLOCK_SIZE) + len < SHA1_BLOCK_SIZE)
44 return sha1_update_arm(desc, data, len);
Jussi Kivilinna60468252014-07-29 17:14:14 +010045
Ard Biesheuvel51e515f2015-04-09 12:55:40 +020046 kernel_neon_begin();
47 sha1_base_do_update(desc, data, len,
48 (sha1_block_fn *)sha1_transform_neon);
49 kernel_neon_end();
Jussi Kivilinna60468252014-07-29 17:14:14 +010050
Ard Biesheuvel51e515f2015-04-09 12:55:40 +020051 return 0;
Jussi Kivilinna60468252014-07-29 17:14:14 +010052}
53
Ard Biesheuvel51e515f2015-04-09 12:55:40 +020054static 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 Kivilinna60468252014-07-29 17:14:14 +010059
Ard Biesheuvel51e515f2015-04-09 12:55:40 +020060 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 Kivilinna60468252014-07-29 17:14:14 +010070static int sha1_neon_final(struct shash_desc *desc, u8 *out)
71{
Ard Biesheuvel51e515f2015-04-09 12:55:40 +020072 return sha1_neon_finup(desc, NULL, 0, out);
Jussi Kivilinna60468252014-07-29 17:14:14 +010073}
74
75static struct shash_alg alg = {
76 .digestsize = SHA1_DIGEST_SIZE,
Ard Biesheuvel51e515f2015-04-09 12:55:40 +020077 .init = sha1_base_init,
Jussi Kivilinna60468252014-07-29 17:14:14 +010078 .update = sha1_neon_update,
79 .final = sha1_neon_final,
Ard Biesheuvel51e515f2015-04-09 12:55:40 +020080 .finup = sha1_neon_finup,
Jussi Kivilinna60468252014-07-29 17:14:14 +010081 .descsize = sizeof(struct sha1_state),
Jussi Kivilinna60468252014-07-29 17:14:14 +010082 .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
92static 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
100static void __exit sha1_neon_mod_fini(void)
101{
102 crypto_unregister_shash(&alg);
103}
104
105module_init(sha1_neon_mod_init);
106module_exit(sha1_neon_mod_fini);
107
108MODULE_LICENSE("GPL");
109MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm, NEON accelerated");
Kees Cook5d26a102014-11-20 17:05:53 -0800110MODULE_ALIAS_CRYPTO("sha1");