GNU Linux-libre 4.19.314-gnu1
[releases.git] / arch / arm / crypto / sha512-glue.c
1 /*
2  * sha512-glue.c - accelerated SHA-384/512 for ARM
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/sha512_base.h>
14 #include <linux/crypto.h>
15 #include <linux/module.h>
16
17 #include <asm/hwcap.h>
18 #include <asm/neon.h>
19
20 #include "sha512.h"
21
22 MODULE_DESCRIPTION("Accelerated SHA-384/SHA-512 secure hash for ARM");
23 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
24 MODULE_LICENSE("GPL v2");
25
26 MODULE_ALIAS_CRYPTO("sha384");
27 MODULE_ALIAS_CRYPTO("sha512");
28 MODULE_ALIAS_CRYPTO("sha384-arm");
29 MODULE_ALIAS_CRYPTO("sha512-arm");
30
31 asmlinkage void sha512_block_data_order(struct sha512_state *state,
32                                         u8 const *src, int blocks);
33
34 int sha512_arm_update(struct shash_desc *desc, const u8 *data,
35                       unsigned int len)
36 {
37         return sha512_base_do_update(desc, data, len, sha512_block_data_order);
38 }
39
40 int sha512_arm_final(struct shash_desc *desc, u8 *out)
41 {
42         sha512_base_do_finalize(desc, sha512_block_data_order);
43         return sha512_base_finish(desc, out);
44 }
45
46 int sha512_arm_finup(struct shash_desc *desc, const u8 *data,
47                      unsigned int len, u8 *out)
48 {
49         sha512_base_do_update(desc, data, len, sha512_block_data_order);
50         return sha512_arm_final(desc, out);
51 }
52
53 static struct shash_alg sha512_arm_algs[] = { {
54         .init                   = sha384_base_init,
55         .update                 = sha512_arm_update,
56         .final                  = sha512_arm_final,
57         .finup                  = sha512_arm_finup,
58         .descsize               = sizeof(struct sha512_state),
59         .digestsize             = SHA384_DIGEST_SIZE,
60         .base                   = {
61                 .cra_name               = "sha384",
62                 .cra_driver_name        = "sha384-arm",
63                 .cra_priority           = 250,
64                 .cra_blocksize          = SHA512_BLOCK_SIZE,
65                 .cra_module             = THIS_MODULE,
66         }
67 },  {
68         .init                   = sha512_base_init,
69         .update                 = sha512_arm_update,
70         .final                  = sha512_arm_final,
71         .finup                  = sha512_arm_finup,
72         .descsize               = sizeof(struct sha512_state),
73         .digestsize             = SHA512_DIGEST_SIZE,
74         .base                   = {
75                 .cra_name               = "sha512",
76                 .cra_driver_name        = "sha512-arm",
77                 .cra_priority           = 250,
78                 .cra_blocksize          = SHA512_BLOCK_SIZE,
79                 .cra_module             = THIS_MODULE,
80         }
81 } };
82
83 static int __init sha512_arm_mod_init(void)
84 {
85         int err;
86
87         err = crypto_register_shashes(sha512_arm_algs,
88                                       ARRAY_SIZE(sha512_arm_algs));
89         if (err)
90                 return err;
91
92         if (IS_ENABLED(CONFIG_KERNEL_MODE_NEON) && cpu_has_neon()) {
93                 err = crypto_register_shashes(sha512_neon_algs,
94                                               ARRAY_SIZE(sha512_neon_algs));
95                 if (err)
96                         goto err_unregister;
97         }
98         return 0;
99
100 err_unregister:
101         crypto_unregister_shashes(sha512_arm_algs,
102                                   ARRAY_SIZE(sha512_arm_algs));
103
104         return err;
105 }
106
107 static void __exit sha512_arm_mod_fini(void)
108 {
109         crypto_unregister_shashes(sha512_arm_algs,
110                                   ARRAY_SIZE(sha512_arm_algs));
111         if (IS_ENABLED(CONFIG_KERNEL_MODE_NEON) && cpu_has_neon())
112                 crypto_unregister_shashes(sha512_neon_algs,
113                                           ARRAY_SIZE(sha512_neon_algs));
114 }
115
116 module_init(sha512_arm_mod_init);
117 module_exit(sha512_arm_mod_fini);