GNU Linux-libre 5.4.274-gnu1
[releases.git] / arch / arm / crypto / sha256_glue.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Glue code for the SHA256 Secure Hash Algorithm assembly implementation
4  * using optimized ARM assembler and NEON instructions.
5  *
6  * Copyright © 2015 Google Inc.
7  *
8  * This file is based on sha256_ssse3_glue.c:
9  *   Copyright (C) 2013 Intel Corporation
10  *   Author: Tim Chen <tim.c.chen@linux.intel.com>
11  */
12
13 #include <crypto/internal/hash.h>
14 #include <linux/crypto.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/mm.h>
18 #include <linux/cryptohash.h>
19 #include <linux/types.h>
20 #include <linux/string.h>
21 #include <crypto/sha.h>
22 #include <crypto/sha256_base.h>
23 #include <asm/simd.h>
24 #include <asm/neon.h>
25
26 #include "sha256_glue.h"
27
28 asmlinkage void sha256_block_data_order(struct sha256_state *state,
29                                         const u8 *data, int num_blks);
30
31 int crypto_sha256_arm_update(struct shash_desc *desc, const u8 *data,
32                              unsigned int len)
33 {
34         /* make sure casting to sha256_block_fn() is safe */
35         BUILD_BUG_ON(offsetof(struct sha256_state, state) != 0);
36
37         return sha256_base_do_update(desc, data, len, sha256_block_data_order);
38 }
39 EXPORT_SYMBOL(crypto_sha256_arm_update);
40
41 static int crypto_sha256_arm_final(struct shash_desc *desc, u8 *out)
42 {
43         sha256_base_do_finalize(desc, sha256_block_data_order);
44         return sha256_base_finish(desc, out);
45 }
46
47 int crypto_sha256_arm_finup(struct shash_desc *desc, const u8 *data,
48                             unsigned int len, u8 *out)
49 {
50         sha256_base_do_update(desc, data, len, sha256_block_data_order);
51         return crypto_sha256_arm_final(desc, out);
52 }
53 EXPORT_SYMBOL(crypto_sha256_arm_finup);
54
55 static struct shash_alg algs[] = { {
56         .digestsize     =       SHA256_DIGEST_SIZE,
57         .init           =       sha256_base_init,
58         .update         =       crypto_sha256_arm_update,
59         .final          =       crypto_sha256_arm_final,
60         .finup          =       crypto_sha256_arm_finup,
61         .descsize       =       sizeof(struct sha256_state),
62         .base           =       {
63                 .cra_name       =       "sha256",
64                 .cra_driver_name =      "sha256-asm",
65                 .cra_priority   =       150,
66                 .cra_blocksize  =       SHA256_BLOCK_SIZE,
67                 .cra_module     =       THIS_MODULE,
68         }
69 }, {
70         .digestsize     =       SHA224_DIGEST_SIZE,
71         .init           =       sha224_base_init,
72         .update         =       crypto_sha256_arm_update,
73         .final          =       crypto_sha256_arm_final,
74         .finup          =       crypto_sha256_arm_finup,
75         .descsize       =       sizeof(struct sha256_state),
76         .base           =       {
77                 .cra_name       =       "sha224",
78                 .cra_driver_name =      "sha224-asm",
79                 .cra_priority   =       150,
80                 .cra_blocksize  =       SHA224_BLOCK_SIZE,
81                 .cra_module     =       THIS_MODULE,
82         }
83 } };
84
85 static int __init sha256_mod_init(void)
86 {
87         int res = crypto_register_shashes(algs, ARRAY_SIZE(algs));
88
89         if (res < 0)
90                 return res;
91
92         if (IS_ENABLED(CONFIG_KERNEL_MODE_NEON) && cpu_has_neon()) {
93                 res = crypto_register_shashes(sha256_neon_algs,
94                                               ARRAY_SIZE(sha256_neon_algs));
95
96                 if (res < 0)
97                         crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
98         }
99
100         return res;
101 }
102
103 static void __exit sha256_mod_fini(void)
104 {
105         crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
106
107         if (IS_ENABLED(CONFIG_KERNEL_MODE_NEON) && cpu_has_neon())
108                 crypto_unregister_shashes(sha256_neon_algs,
109                                           ARRAY_SIZE(sha256_neon_algs));
110 }
111
112 module_init(sha256_mod_init);
113 module_exit(sha256_mod_fini);
114
115 MODULE_LICENSE("GPL");
116 MODULE_DESCRIPTION("SHA256 Secure Hash Algorithm (ARM), including NEON");
117
118 MODULE_ALIAS_CRYPTO("sha256");