1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Glue code for SHA256 hashing optimized for sparc64 crypto opcodes.
4 * This is based largely upon crypto/sha256_generic.c
6 * Copyright (c) Jean-Luc Cooke <jlcooke@certainkey.com>
7 * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
8 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
9 * SHA224 Support Copyright 2007 Intel Corporation <jonathan.lynch@intel.com>
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <crypto/internal/hash.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <crypto/sha2.h>
20 #include <crypto/sha256_base.h>
22 #include <asm/pstate.h>
27 asmlinkage void sha256_sparc64_transform(u32 *digest, const char *data,
30 static void __sha256_sparc64_update(struct sha256_state *sctx, const u8 *data,
31 unsigned int len, unsigned int partial)
33 unsigned int done = 0;
37 done = SHA256_BLOCK_SIZE - partial;
38 memcpy(sctx->buf + partial, data, done);
39 sha256_sparc64_transform(sctx->state, sctx->buf, 1);
41 if (len - done >= SHA256_BLOCK_SIZE) {
42 const unsigned int rounds = (len - done) / SHA256_BLOCK_SIZE;
44 sha256_sparc64_transform(sctx->state, data + done, rounds);
45 done += rounds * SHA256_BLOCK_SIZE;
48 memcpy(sctx->buf, data + done, len - done);
51 static int sha256_sparc64_update(struct shash_desc *desc, const u8 *data,
54 struct sha256_state *sctx = shash_desc_ctx(desc);
55 unsigned int partial = sctx->count % SHA256_BLOCK_SIZE;
57 /* Handle the fast case right here */
58 if (partial + len < SHA256_BLOCK_SIZE) {
60 memcpy(sctx->buf + partial, data, len);
62 __sha256_sparc64_update(sctx, data, len, partial);
67 static int sha256_sparc64_final(struct shash_desc *desc, u8 *out)
69 struct sha256_state *sctx = shash_desc_ctx(desc);
70 unsigned int i, index, padlen;
71 __be32 *dst = (__be32 *)out;
73 static const u8 padding[SHA256_BLOCK_SIZE] = { 0x80, };
75 bits = cpu_to_be64(sctx->count << 3);
77 /* Pad out to 56 mod 64 and append length */
78 index = sctx->count % SHA256_BLOCK_SIZE;
79 padlen = (index < 56) ? (56 - index) : ((SHA256_BLOCK_SIZE+56) - index);
81 /* We need to fill a whole block for __sha256_sparc64_update() */
83 sctx->count += padlen;
84 memcpy(sctx->buf + index, padding, padlen);
86 __sha256_sparc64_update(sctx, padding, padlen, index);
88 __sha256_sparc64_update(sctx, (const u8 *)&bits, sizeof(bits), 56);
90 /* Store state in digest */
91 for (i = 0; i < 8; i++)
92 dst[i] = cpu_to_be32(sctx->state[i]);
95 memset(sctx, 0, sizeof(*sctx));
100 static int sha224_sparc64_final(struct shash_desc *desc, u8 *hash)
102 u8 D[SHA256_DIGEST_SIZE];
104 sha256_sparc64_final(desc, D);
106 memcpy(hash, D, SHA224_DIGEST_SIZE);
107 memzero_explicit(D, SHA256_DIGEST_SIZE);
112 static int sha256_sparc64_export(struct shash_desc *desc, void *out)
114 struct sha256_state *sctx = shash_desc_ctx(desc);
116 memcpy(out, sctx, sizeof(*sctx));
120 static int sha256_sparc64_import(struct shash_desc *desc, const void *in)
122 struct sha256_state *sctx = shash_desc_ctx(desc);
124 memcpy(sctx, in, sizeof(*sctx));
128 static struct shash_alg sha256_alg = {
129 .digestsize = SHA256_DIGEST_SIZE,
130 .init = sha256_base_init,
131 .update = sha256_sparc64_update,
132 .final = sha256_sparc64_final,
133 .export = sha256_sparc64_export,
134 .import = sha256_sparc64_import,
135 .descsize = sizeof(struct sha256_state),
136 .statesize = sizeof(struct sha256_state),
138 .cra_name = "sha256",
139 .cra_driver_name= "sha256-sparc64",
140 .cra_priority = SPARC_CR_OPCODE_PRIORITY,
141 .cra_blocksize = SHA256_BLOCK_SIZE,
142 .cra_module = THIS_MODULE,
146 static struct shash_alg sha224_alg = {
147 .digestsize = SHA224_DIGEST_SIZE,
148 .init = sha224_base_init,
149 .update = sha256_sparc64_update,
150 .final = sha224_sparc64_final,
151 .descsize = sizeof(struct sha256_state),
153 .cra_name = "sha224",
154 .cra_driver_name= "sha224-sparc64",
155 .cra_priority = SPARC_CR_OPCODE_PRIORITY,
156 .cra_blocksize = SHA224_BLOCK_SIZE,
157 .cra_module = THIS_MODULE,
161 static bool __init sparc64_has_sha256_opcode(void)
165 if (!(sparc64_elf_hwcap & HWCAP_SPARC_CRYPTO))
168 __asm__ __volatile__("rd %%asr26, %0" : "=r" (cfr));
169 if (!(cfr & CFR_SHA256))
175 static int __init sha256_sparc64_mod_init(void)
177 if (sparc64_has_sha256_opcode()) {
178 int ret = crypto_register_shash(&sha224_alg);
182 ret = crypto_register_shash(&sha256_alg);
184 crypto_unregister_shash(&sha224_alg);
188 pr_info("Using sparc64 sha256 opcode optimized SHA-256/SHA-224 implementation\n");
191 pr_info("sparc64 sha256 opcode not available.\n");
195 static void __exit sha256_sparc64_mod_fini(void)
197 crypto_unregister_shash(&sha224_alg);
198 crypto_unregister_shash(&sha256_alg);
201 module_init(sha256_sparc64_mod_init);
202 module_exit(sha256_sparc64_mod_fini);
204 MODULE_LICENSE("GPL");
205 MODULE_DESCRIPTION("SHA-224 and SHA-256 Secure Hash Algorithm, sparc64 sha256 opcode accelerated");
207 MODULE_ALIAS_CRYPTO("sha224");
208 MODULE_ALIAS_CRYPTO("sha256");
210 #include "crop_devid.c"