~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/arch/sparc/crypto/sha512_glue.c

Version: ~ [ linux-6.11-rc3 ] ~ [ linux-6.10.4 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.45 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.104 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.164 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.223 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.281 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.319 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /arch/sparc/crypto/sha512_glue.c (Architecture m68k) and /arch/sparc/crypto/sha512_glue.c (Architecture sparc)


  1 // SPDX-License-Identifier: GPL-2.0-only            1 // SPDX-License-Identifier: GPL-2.0-only
  2 /* Glue code for SHA512 hashing optimized for       2 /* Glue code for SHA512 hashing optimized for sparc64 crypto opcodes.
  3  *                                                  3  *
  4  * This is based largely upon crypto/sha512_ge      4  * This is based largely upon crypto/sha512_generic.c
  5  *                                                  5  *
  6  * Copyright (c) Jean-Luc Cooke <jlcooke@certa      6  * Copyright (c) Jean-Luc Cooke <jlcooke@certainkey.com>
  7  * Copyright (c) Andrew McDonald <andrew@mcdon      7  * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
  8  * Copyright (c) 2003 Kyle McMartin <kyle@debi      8  * Copyright (c) 2003 Kyle McMartin <kyle@debian.org>
  9  */                                                 9  */
 10                                                    10 
 11 #define pr_fmt(fmt)     KBUILD_MODNAME ": " fm     11 #define pr_fmt(fmt)     KBUILD_MODNAME ": " fmt
 12                                                    12 
 13 #include <crypto/internal/hash.h>                  13 #include <crypto/internal/hash.h>
 14 #include <linux/init.h>                            14 #include <linux/init.h>
 15 #include <linux/module.h>                          15 #include <linux/module.h>
 16 #include <linux/mm.h>                              16 #include <linux/mm.h>
 17 #include <linux/types.h>                           17 #include <linux/types.h>
 18 #include <crypto/sha2.h>                           18 #include <crypto/sha2.h>
 19 #include <crypto/sha512_base.h>                    19 #include <crypto/sha512_base.h>
 20                                                    20 
 21 #include <asm/pstate.h>                            21 #include <asm/pstate.h>
 22 #include <asm/elf.h>                               22 #include <asm/elf.h>
 23                                                    23 
 24 #include "opcodes.h"                               24 #include "opcodes.h"
 25                                                    25 
 26 asmlinkage void sha512_sparc64_transform(u64 *     26 asmlinkage void sha512_sparc64_transform(u64 *digest, const char *data,
 27                                          unsig     27                                          unsigned int rounds);
 28                                                    28 
 29 static void __sha512_sparc64_update(struct sha     29 static void __sha512_sparc64_update(struct sha512_state *sctx, const u8 *data,
 30                                     unsigned i     30                                     unsigned int len, unsigned int partial)
 31 {                                                  31 {
 32         unsigned int done = 0;                     32         unsigned int done = 0;
 33                                                    33 
 34         if ((sctx->count[0] += len) < len)         34         if ((sctx->count[0] += len) < len)
 35                 sctx->count[1]++;                  35                 sctx->count[1]++;
 36         if (partial) {                             36         if (partial) {
 37                 done = SHA512_BLOCK_SIZE - par     37                 done = SHA512_BLOCK_SIZE - partial;
 38                 memcpy(sctx->buf + partial, da     38                 memcpy(sctx->buf + partial, data, done);
 39                 sha512_sparc64_transform(sctx-     39                 sha512_sparc64_transform(sctx->state, sctx->buf, 1);
 40         }                                          40         }
 41         if (len - done >= SHA512_BLOCK_SIZE) {     41         if (len - done >= SHA512_BLOCK_SIZE) {
 42                 const unsigned int rounds = (l     42                 const unsigned int rounds = (len - done) / SHA512_BLOCK_SIZE;
 43                                                    43 
 44                 sha512_sparc64_transform(sctx-     44                 sha512_sparc64_transform(sctx->state, data + done, rounds);
 45                 done += rounds * SHA512_BLOCK_     45                 done += rounds * SHA512_BLOCK_SIZE;
 46         }                                          46         }
 47                                                    47 
 48         memcpy(sctx->buf, data + done, len - d     48         memcpy(sctx->buf, data + done, len - done);
 49 }                                                  49 }
 50                                                    50 
 51 static int sha512_sparc64_update(struct shash_     51 static int sha512_sparc64_update(struct shash_desc *desc, const u8 *data,
 52                                  unsigned int      52                                  unsigned int len)
 53 {                                                  53 {
 54         struct sha512_state *sctx = shash_desc     54         struct sha512_state *sctx = shash_desc_ctx(desc);
 55         unsigned int partial = sctx->count[0]      55         unsigned int partial = sctx->count[0] % SHA512_BLOCK_SIZE;
 56                                                    56 
 57         /* Handle the fast case right here */      57         /* Handle the fast case right here */
 58         if (partial + len < SHA512_BLOCK_SIZE)     58         if (partial + len < SHA512_BLOCK_SIZE) {
 59                 if ((sctx->count[0] += len) <      59                 if ((sctx->count[0] += len) < len)
 60                         sctx->count[1]++;          60                         sctx->count[1]++;
 61                 memcpy(sctx->buf + partial, da     61                 memcpy(sctx->buf + partial, data, len);
 62         } else                                     62         } else
 63                 __sha512_sparc64_update(sctx,      63                 __sha512_sparc64_update(sctx, data, len, partial);
 64                                                    64 
 65         return 0;                                  65         return 0;
 66 }                                                  66 }
 67                                                    67 
 68 static int sha512_sparc64_final(struct shash_d     68 static int sha512_sparc64_final(struct shash_desc *desc, u8 *out)
 69 {                                                  69 {
 70         struct sha512_state *sctx = shash_desc     70         struct sha512_state *sctx = shash_desc_ctx(desc);
 71         unsigned int i, index, padlen;             71         unsigned int i, index, padlen;
 72         __be64 *dst = (__be64 *)out;               72         __be64 *dst = (__be64 *)out;
 73         __be64 bits[2];                            73         __be64 bits[2];
 74         static const u8 padding[SHA512_BLOCK_S     74         static const u8 padding[SHA512_BLOCK_SIZE] = { 0x80, };
 75                                                    75 
 76         /* Save number of bits */                  76         /* Save number of bits */
 77         bits[1] = cpu_to_be64(sctx->count[0] <     77         bits[1] = cpu_to_be64(sctx->count[0] << 3);
 78         bits[0] = cpu_to_be64(sctx->count[1] <     78         bits[0] = cpu_to_be64(sctx->count[1] << 3 | sctx->count[0] >> 61);
 79                                                    79 
 80         /* Pad out to 112 mod 128 and append l     80         /* Pad out to 112 mod 128 and append length */
 81         index = sctx->count[0] % SHA512_BLOCK_     81         index = sctx->count[0] % SHA512_BLOCK_SIZE;
 82         padlen = (index < 112) ? (112 - index)     82         padlen = (index < 112) ? (112 - index) : ((SHA512_BLOCK_SIZE+112) - index);
 83                                                    83 
 84         /* We need to fill a whole block for _     84         /* We need to fill a whole block for __sha512_sparc64_update() */
 85         if (padlen <= 112) {                       85         if (padlen <= 112) {
 86                 if ((sctx->count[0] += padlen)     86                 if ((sctx->count[0] += padlen) < padlen)
 87                         sctx->count[1]++;          87                         sctx->count[1]++;
 88                 memcpy(sctx->buf + index, padd     88                 memcpy(sctx->buf + index, padding, padlen);
 89         } else {                                   89         } else {
 90                 __sha512_sparc64_update(sctx,      90                 __sha512_sparc64_update(sctx, padding, padlen, index);
 91         }                                          91         }
 92         __sha512_sparc64_update(sctx, (const u     92         __sha512_sparc64_update(sctx, (const u8 *)&bits, sizeof(bits), 112);
 93                                                    93 
 94         /* Store state in digest */                94         /* Store state in digest */
 95         for (i = 0; i < 8; i++)                    95         for (i = 0; i < 8; i++)
 96                 dst[i] = cpu_to_be64(sctx->sta     96                 dst[i] = cpu_to_be64(sctx->state[i]);
 97                                                    97 
 98         /* Wipe context */                         98         /* Wipe context */
 99         memset(sctx, 0, sizeof(*sctx));            99         memset(sctx, 0, sizeof(*sctx));
100                                                   100 
101         return 0;                                 101         return 0;
102 }                                                 102 }
103                                                   103 
104 static int sha384_sparc64_final(struct shash_d    104 static int sha384_sparc64_final(struct shash_desc *desc, u8 *hash)
105 {                                                 105 {
106         u8 D[64];                                 106         u8 D[64];
107                                                   107 
108         sha512_sparc64_final(desc, D);            108         sha512_sparc64_final(desc, D);
109                                                   109 
110         memcpy(hash, D, 48);                      110         memcpy(hash, D, 48);
111         memzero_explicit(D, 64);                  111         memzero_explicit(D, 64);
112                                                   112 
113         return 0;                                 113         return 0;
114 }                                                 114 }
115                                                   115 
116 static struct shash_alg sha512 = {                116 static struct shash_alg sha512 = {
117         .digestsize     =       SHA512_DIGEST_    117         .digestsize     =       SHA512_DIGEST_SIZE,
118         .init           =       sha512_base_in    118         .init           =       sha512_base_init,
119         .update         =       sha512_sparc64    119         .update         =       sha512_sparc64_update,
120         .final          =       sha512_sparc64    120         .final          =       sha512_sparc64_final,
121         .descsize       =       sizeof(struct     121         .descsize       =       sizeof(struct sha512_state),
122         .base           =       {                 122         .base           =       {
123                 .cra_name       =       "sha51    123                 .cra_name       =       "sha512",
124                 .cra_driver_name=       "sha51    124                 .cra_driver_name=       "sha512-sparc64",
125                 .cra_priority   =       SPARC_    125                 .cra_priority   =       SPARC_CR_OPCODE_PRIORITY,
126                 .cra_blocksize  =       SHA512    126                 .cra_blocksize  =       SHA512_BLOCK_SIZE,
127                 .cra_module     =       THIS_M    127                 .cra_module     =       THIS_MODULE,
128         }                                         128         }
129 };                                                129 };
130                                                   130 
131 static struct shash_alg sha384 = {                131 static struct shash_alg sha384 = {
132         .digestsize     =       SHA384_DIGEST_    132         .digestsize     =       SHA384_DIGEST_SIZE,
133         .init           =       sha384_base_in    133         .init           =       sha384_base_init,
134         .update         =       sha512_sparc64    134         .update         =       sha512_sparc64_update,
135         .final          =       sha384_sparc64    135         .final          =       sha384_sparc64_final,
136         .descsize       =       sizeof(struct     136         .descsize       =       sizeof(struct sha512_state),
137         .base           =       {                 137         .base           =       {
138                 .cra_name       =       "sha38    138                 .cra_name       =       "sha384",
139                 .cra_driver_name=       "sha38    139                 .cra_driver_name=       "sha384-sparc64",
140                 .cra_priority   =       SPARC_    140                 .cra_priority   =       SPARC_CR_OPCODE_PRIORITY,
141                 .cra_blocksize  =       SHA384    141                 .cra_blocksize  =       SHA384_BLOCK_SIZE,
142                 .cra_module     =       THIS_M    142                 .cra_module     =       THIS_MODULE,
143         }                                         143         }
144 };                                                144 };
145                                                   145 
146 static bool __init sparc64_has_sha512_opcode(v    146 static bool __init sparc64_has_sha512_opcode(void)
147 {                                                 147 {
148         unsigned long cfr;                        148         unsigned long cfr;
149                                                   149 
150         if (!(sparc64_elf_hwcap & HWCAP_SPARC_    150         if (!(sparc64_elf_hwcap & HWCAP_SPARC_CRYPTO))
151                 return false;                     151                 return false;
152                                                   152 
153         __asm__ __volatile__("rd %%asr26, %0"     153         __asm__ __volatile__("rd %%asr26, %0" : "=r" (cfr));
154         if (!(cfr & CFR_SHA512))                  154         if (!(cfr & CFR_SHA512))
155                 return false;                     155                 return false;
156                                                   156 
157         return true;                              157         return true;
158 }                                                 158 }
159                                                   159 
160 static int __init sha512_sparc64_mod_init(void    160 static int __init sha512_sparc64_mod_init(void)
161 {                                                 161 {
162         if (sparc64_has_sha512_opcode()) {        162         if (sparc64_has_sha512_opcode()) {
163                 int ret = crypto_register_shas    163                 int ret = crypto_register_shash(&sha384);
164                 if (ret < 0)                      164                 if (ret < 0)
165                         return ret;               165                         return ret;
166                                                   166 
167                 ret = crypto_register_shash(&s    167                 ret = crypto_register_shash(&sha512);
168                 if (ret < 0) {                    168                 if (ret < 0) {
169                         crypto_unregister_shas    169                         crypto_unregister_shash(&sha384);
170                         return ret;               170                         return ret;
171                 }                                 171                 }
172                                                   172 
173                 pr_info("Using sparc64 sha512     173                 pr_info("Using sparc64 sha512 opcode optimized SHA-512/SHA-384 implementation\n");
174                 return 0;                         174                 return 0;
175         }                                         175         }
176         pr_info("sparc64 sha512 opcode not ava    176         pr_info("sparc64 sha512 opcode not available.\n");
177         return -ENODEV;                           177         return -ENODEV;
178 }                                                 178 }
179                                                   179 
180 static void __exit sha512_sparc64_mod_fini(voi    180 static void __exit sha512_sparc64_mod_fini(void)
181 {                                                 181 {
182         crypto_unregister_shash(&sha384);         182         crypto_unregister_shash(&sha384);
183         crypto_unregister_shash(&sha512);         183         crypto_unregister_shash(&sha512);
184 }                                                 184 }
185                                                   185 
186 module_init(sha512_sparc64_mod_init);             186 module_init(sha512_sparc64_mod_init);
187 module_exit(sha512_sparc64_mod_fini);             187 module_exit(sha512_sparc64_mod_fini);
188                                                   188 
189 MODULE_LICENSE("GPL");                            189 MODULE_LICENSE("GPL");
190 MODULE_DESCRIPTION("SHA-384 and SHA-512 Secure    190 MODULE_DESCRIPTION("SHA-384 and SHA-512 Secure Hash Algorithm, sparc64 sha512 opcode accelerated");
191                                                   191 
192 MODULE_ALIAS_CRYPTO("sha384");                    192 MODULE_ALIAS_CRYPTO("sha384");
193 MODULE_ALIAS_CRYPTO("sha512");                    193 MODULE_ALIAS_CRYPTO("sha512");
194                                                   194 
195 #include "crop_devid.c"                           195 #include "crop_devid.c"
196                                                   196 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php