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

TOMOYO Linux Cross Reference
Linux/arch/sparc/crypto/sha256_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/sha256_glue.c (Architecture m68k) and /arch/sparc/crypto/sha256_glue.c (Architecture sparc)


  1 // SPDX-License-Identifier: GPL-2.0-only            1 // SPDX-License-Identifier: GPL-2.0-only
  2 /* Glue code for SHA256 hashing optimized for       2 /* Glue code for SHA256 hashing optimized for sparc64 crypto opcodes.
  3  *                                                  3  *
  4  * This is based largely upon crypto/sha256_ge      4  * This is based largely upon crypto/sha256_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) 2002 James Morris <jmorris@in      8  * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
  9  * SHA224 Support Copyright 2007 Intel Corpora      9  * SHA224 Support Copyright 2007 Intel Corporation <jonathan.lynch@intel.com>
 10  */                                                10  */
 11                                                    11 
 12 #define pr_fmt(fmt)     KBUILD_MODNAME ": " fm     12 #define pr_fmt(fmt)     KBUILD_MODNAME ": " fmt
 13                                                    13 
 14 #include <crypto/internal/hash.h>                  14 #include <crypto/internal/hash.h>
 15 #include <linux/init.h>                            15 #include <linux/init.h>
 16 #include <linux/module.h>                          16 #include <linux/module.h>
 17 #include <linux/mm.h>                              17 #include <linux/mm.h>
 18 #include <linux/types.h>                           18 #include <linux/types.h>
 19 #include <crypto/sha2.h>                           19 #include <crypto/sha2.h>
 20 #include <crypto/sha256_base.h>                    20 #include <crypto/sha256_base.h>
 21                                                    21 
 22 #include <asm/pstate.h>                            22 #include <asm/pstate.h>
 23 #include <asm/elf.h>                               23 #include <asm/elf.h>
 24                                                    24 
 25 #include "opcodes.h"                               25 #include "opcodes.h"
 26                                                    26 
 27 asmlinkage void sha256_sparc64_transform(u32 *     27 asmlinkage void sha256_sparc64_transform(u32 *digest, const char *data,
 28                                          unsig     28                                          unsigned int rounds);
 29                                                    29 
 30 static void __sha256_sparc64_update(struct sha     30 static void __sha256_sparc64_update(struct sha256_state *sctx, const u8 *data,
 31                                     unsigned i     31                                     unsigned int len, unsigned int partial)
 32 {                                                  32 {
 33         unsigned int done = 0;                     33         unsigned int done = 0;
 34                                                    34 
 35         sctx->count += len;                        35         sctx->count += len;
 36         if (partial) {                             36         if (partial) {
 37                 done = SHA256_BLOCK_SIZE - par     37                 done = SHA256_BLOCK_SIZE - partial;
 38                 memcpy(sctx->buf + partial, da     38                 memcpy(sctx->buf + partial, data, done);
 39                 sha256_sparc64_transform(sctx-     39                 sha256_sparc64_transform(sctx->state, sctx->buf, 1);
 40         }                                          40         }
 41         if (len - done >= SHA256_BLOCK_SIZE) {     41         if (len - done >= SHA256_BLOCK_SIZE) {
 42                 const unsigned int rounds = (l     42                 const unsigned int rounds = (len - done) / SHA256_BLOCK_SIZE;
 43                                                    43 
 44                 sha256_sparc64_transform(sctx-     44                 sha256_sparc64_transform(sctx->state, data + done, rounds);
 45                 done += rounds * SHA256_BLOCK_     45                 done += rounds * SHA256_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 sha256_sparc64_update(struct shash_     51 static int sha256_sparc64_update(struct shash_desc *desc, const u8 *data,
 52                                  unsigned int      52                                  unsigned int len)
 53 {                                                  53 {
 54         struct sha256_state *sctx = shash_desc     54         struct sha256_state *sctx = shash_desc_ctx(desc);
 55         unsigned int partial = sctx->count % S     55         unsigned int partial = sctx->count % SHA256_BLOCK_SIZE;
 56                                                    56 
 57         /* Handle the fast case right here */      57         /* Handle the fast case right here */
 58         if (partial + len < SHA256_BLOCK_SIZE)     58         if (partial + len < SHA256_BLOCK_SIZE) {
 59                 sctx->count += len;                59                 sctx->count += len;
 60                 memcpy(sctx->buf + partial, da     60                 memcpy(sctx->buf + partial, data, len);
 61         } else                                     61         } else
 62                 __sha256_sparc64_update(sctx,      62                 __sha256_sparc64_update(sctx, data, len, partial);
 63                                                    63 
 64         return 0;                                  64         return 0;
 65 }                                                  65 }
 66                                                    66 
 67 static int sha256_sparc64_final(struct shash_d     67 static int sha256_sparc64_final(struct shash_desc *desc, u8 *out)
 68 {                                                  68 {
 69         struct sha256_state *sctx = shash_desc     69         struct sha256_state *sctx = shash_desc_ctx(desc);
 70         unsigned int i, index, padlen;             70         unsigned int i, index, padlen;
 71         __be32 *dst = (__be32 *)out;               71         __be32 *dst = (__be32 *)out;
 72         __be64 bits;                               72         __be64 bits;
 73         static const u8 padding[SHA256_BLOCK_S     73         static const u8 padding[SHA256_BLOCK_SIZE] = { 0x80, };
 74                                                    74 
 75         bits = cpu_to_be64(sctx->count << 3);      75         bits = cpu_to_be64(sctx->count << 3);
 76                                                    76 
 77         /* Pad out to 56 mod 64 and append len     77         /* Pad out to 56 mod 64 and append length */
 78         index = sctx->count % SHA256_BLOCK_SIZ     78         index = sctx->count % SHA256_BLOCK_SIZE;
 79         padlen = (index < 56) ? (56 - index) :     79         padlen = (index < 56) ? (56 - index) : ((SHA256_BLOCK_SIZE+56) - index);
 80                                                    80 
 81         /* We need to fill a whole block for _     81         /* We need to fill a whole block for __sha256_sparc64_update() */
 82         if (padlen <= 56) {                        82         if (padlen <= 56) {
 83                 sctx->count += padlen;             83                 sctx->count += padlen;
 84                 memcpy(sctx->buf + index, padd     84                 memcpy(sctx->buf + index, padding, padlen);
 85         } else {                                   85         } else {
 86                 __sha256_sparc64_update(sctx,      86                 __sha256_sparc64_update(sctx, padding, padlen, index);
 87         }                                          87         }
 88         __sha256_sparc64_update(sctx, (const u     88         __sha256_sparc64_update(sctx, (const u8 *)&bits, sizeof(bits), 56);
 89                                                    89 
 90         /* Store state in digest */                90         /* Store state in digest */
 91         for (i = 0; i < 8; i++)                    91         for (i = 0; i < 8; i++)
 92                 dst[i] = cpu_to_be32(sctx->sta     92                 dst[i] = cpu_to_be32(sctx->state[i]);
 93                                                    93 
 94         /* Wipe context */                         94         /* Wipe context */
 95         memset(sctx, 0, sizeof(*sctx));            95         memset(sctx, 0, sizeof(*sctx));
 96                                                    96 
 97         return 0;                                  97         return 0;
 98 }                                                  98 }
 99                                                    99 
100 static int sha224_sparc64_final(struct shash_d    100 static int sha224_sparc64_final(struct shash_desc *desc, u8 *hash)
101 {                                                 101 {
102         u8 D[SHA256_DIGEST_SIZE];                 102         u8 D[SHA256_DIGEST_SIZE];
103                                                   103 
104         sha256_sparc64_final(desc, D);            104         sha256_sparc64_final(desc, D);
105                                                   105 
106         memcpy(hash, D, SHA224_DIGEST_SIZE);      106         memcpy(hash, D, SHA224_DIGEST_SIZE);
107         memzero_explicit(D, SHA256_DIGEST_SIZE    107         memzero_explicit(D, SHA256_DIGEST_SIZE);
108                                                   108 
109         return 0;                                 109         return 0;
110 }                                                 110 }
111                                                   111 
112 static int sha256_sparc64_export(struct shash_    112 static int sha256_sparc64_export(struct shash_desc *desc, void *out)
113 {                                                 113 {
114         struct sha256_state *sctx = shash_desc    114         struct sha256_state *sctx = shash_desc_ctx(desc);
115                                                   115 
116         memcpy(out, sctx, sizeof(*sctx));         116         memcpy(out, sctx, sizeof(*sctx));
117         return 0;                                 117         return 0;
118 }                                                 118 }
119                                                   119 
120 static int sha256_sparc64_import(struct shash_    120 static int sha256_sparc64_import(struct shash_desc *desc, const void *in)
121 {                                                 121 {
122         struct sha256_state *sctx = shash_desc    122         struct sha256_state *sctx = shash_desc_ctx(desc);
123                                                   123 
124         memcpy(sctx, in, sizeof(*sctx));          124         memcpy(sctx, in, sizeof(*sctx));
125         return 0;                                 125         return 0;
126 }                                                 126 }
127                                                   127 
128 static struct shash_alg sha256_alg = {            128 static struct shash_alg sha256_alg = {
129         .digestsize     =       SHA256_DIGEST_    129         .digestsize     =       SHA256_DIGEST_SIZE,
130         .init           =       sha256_base_in    130         .init           =       sha256_base_init,
131         .update         =       sha256_sparc64    131         .update         =       sha256_sparc64_update,
132         .final          =       sha256_sparc64    132         .final          =       sha256_sparc64_final,
133         .export         =       sha256_sparc64    133         .export         =       sha256_sparc64_export,
134         .import         =       sha256_sparc64    134         .import         =       sha256_sparc64_import,
135         .descsize       =       sizeof(struct     135         .descsize       =       sizeof(struct sha256_state),
136         .statesize      =       sizeof(struct     136         .statesize      =       sizeof(struct sha256_state),
137         .base           =       {                 137         .base           =       {
138                 .cra_name       =       "sha25    138                 .cra_name       =       "sha256",
139                 .cra_driver_name=       "sha25    139                 .cra_driver_name=       "sha256-sparc64",
140                 .cra_priority   =       SPARC_    140                 .cra_priority   =       SPARC_CR_OPCODE_PRIORITY,
141                 .cra_blocksize  =       SHA256    141                 .cra_blocksize  =       SHA256_BLOCK_SIZE,
142                 .cra_module     =       THIS_M    142                 .cra_module     =       THIS_MODULE,
143         }                                         143         }
144 };                                                144 };
145                                                   145 
146 static struct shash_alg sha224_alg = {            146 static struct shash_alg sha224_alg = {
147         .digestsize     =       SHA224_DIGEST_    147         .digestsize     =       SHA224_DIGEST_SIZE,
148         .init           =       sha224_base_in    148         .init           =       sha224_base_init,
149         .update         =       sha256_sparc64    149         .update         =       sha256_sparc64_update,
150         .final          =       sha224_sparc64    150         .final          =       sha224_sparc64_final,
151         .descsize       =       sizeof(struct     151         .descsize       =       sizeof(struct sha256_state),
152         .base           =       {                 152         .base           =       {
153                 .cra_name       =       "sha22    153                 .cra_name       =       "sha224",
154                 .cra_driver_name=       "sha22    154                 .cra_driver_name=       "sha224-sparc64",
155                 .cra_priority   =       SPARC_    155                 .cra_priority   =       SPARC_CR_OPCODE_PRIORITY,
156                 .cra_blocksize  =       SHA224    156                 .cra_blocksize  =       SHA224_BLOCK_SIZE,
157                 .cra_module     =       THIS_M    157                 .cra_module     =       THIS_MODULE,
158         }                                         158         }
159 };                                                159 };
160                                                   160 
161 static bool __init sparc64_has_sha256_opcode(v    161 static bool __init sparc64_has_sha256_opcode(void)
162 {                                                 162 {
163         unsigned long cfr;                        163         unsigned long cfr;
164                                                   164 
165         if (!(sparc64_elf_hwcap & HWCAP_SPARC_    165         if (!(sparc64_elf_hwcap & HWCAP_SPARC_CRYPTO))
166                 return false;                     166                 return false;
167                                                   167 
168         __asm__ __volatile__("rd %%asr26, %0"     168         __asm__ __volatile__("rd %%asr26, %0" : "=r" (cfr));
169         if (!(cfr & CFR_SHA256))                  169         if (!(cfr & CFR_SHA256))
170                 return false;                     170                 return false;
171                                                   171 
172         return true;                              172         return true;
173 }                                                 173 }
174                                                   174 
175 static int __init sha256_sparc64_mod_init(void    175 static int __init sha256_sparc64_mod_init(void)
176 {                                                 176 {
177         if (sparc64_has_sha256_opcode()) {        177         if (sparc64_has_sha256_opcode()) {
178                 int ret = crypto_register_shas    178                 int ret = crypto_register_shash(&sha224_alg);
179                 if (ret < 0)                      179                 if (ret < 0)
180                         return ret;               180                         return ret;
181                                                   181 
182                 ret = crypto_register_shash(&s    182                 ret = crypto_register_shash(&sha256_alg);
183                 if (ret < 0) {                    183                 if (ret < 0) {
184                         crypto_unregister_shas    184                         crypto_unregister_shash(&sha224_alg);
185                         return ret;               185                         return ret;
186                 }                                 186                 }
187                                                   187 
188                 pr_info("Using sparc64 sha256     188                 pr_info("Using sparc64 sha256 opcode optimized SHA-256/SHA-224 implementation\n");
189                 return 0;                         189                 return 0;
190         }                                         190         }
191         pr_info("sparc64 sha256 opcode not ava    191         pr_info("sparc64 sha256 opcode not available.\n");
192         return -ENODEV;                           192         return -ENODEV;
193 }                                                 193 }
194                                                   194 
195 static void __exit sha256_sparc64_mod_fini(voi    195 static void __exit sha256_sparc64_mod_fini(void)
196 {                                                 196 {
197         crypto_unregister_shash(&sha224_alg);     197         crypto_unregister_shash(&sha224_alg);
198         crypto_unregister_shash(&sha256_alg);     198         crypto_unregister_shash(&sha256_alg);
199 }                                                 199 }
200                                                   200 
201 module_init(sha256_sparc64_mod_init);             201 module_init(sha256_sparc64_mod_init);
202 module_exit(sha256_sparc64_mod_fini);             202 module_exit(sha256_sparc64_mod_fini);
203                                                   203 
204 MODULE_LICENSE("GPL");                            204 MODULE_LICENSE("GPL");
205 MODULE_DESCRIPTION("SHA-224 and SHA-256 Secure    205 MODULE_DESCRIPTION("SHA-224 and SHA-256 Secure Hash Algorithm, sparc64 sha256 opcode accelerated");
206                                                   206 
207 MODULE_ALIAS_CRYPTO("sha224");                    207 MODULE_ALIAS_CRYPTO("sha224");
208 MODULE_ALIAS_CRYPTO("sha256");                    208 MODULE_ALIAS_CRYPTO("sha256");
209                                                   209 
210 #include "crop_devid.c"                           210 #include "crop_devid.c"
211                                                   211 

~ [ 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