Linux Audio

Check our new training course

Loading...
v4.17
 
  1/* Glue code for SHA1 hashing optimized for sparc64 crypto opcodes.
  2 *
  3 * This is based largely upon arch/x86/crypto/sha1_ssse3_glue.c
  4 *
  5 * Copyright (c) Alan Smithee.
  6 * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
  7 * Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
  8 * Copyright (c) Mathias Krause <minipli@googlemail.com>
  9 */
 10
 11#define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
 12
 13#include <crypto/internal/hash.h>
 14#include <linux/init.h>
 15#include <linux/module.h>
 16#include <linux/mm.h>
 17#include <linux/cryptohash.h>
 18#include <linux/types.h>
 19#include <crypto/sha.h>
 20
 21#include <asm/pstate.h>
 22#include <asm/elf.h>
 23
 24#include "opcodes.h"
 25
 26asmlinkage void sha1_sparc64_transform(u32 *digest, const char *data,
 27				       unsigned int rounds);
 28
 29static int sha1_sparc64_init(struct shash_desc *desc)
 30{
 31	struct sha1_state *sctx = shash_desc_ctx(desc);
 32
 33	*sctx = (struct sha1_state){
 34		.state = { SHA1_H0, SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4 },
 35	};
 36
 37	return 0;
 38}
 39
 40static void __sha1_sparc64_update(struct sha1_state *sctx, const u8 *data,
 41				  unsigned int len, unsigned int partial)
 42{
 43	unsigned int done = 0;
 44
 45	sctx->count += len;
 46	if (partial) {
 47		done = SHA1_BLOCK_SIZE - partial;
 48		memcpy(sctx->buffer + partial, data, done);
 49		sha1_sparc64_transform(sctx->state, sctx->buffer, 1);
 50	}
 51	if (len - done >= SHA1_BLOCK_SIZE) {
 52		const unsigned int rounds = (len - done) / SHA1_BLOCK_SIZE;
 53
 54		sha1_sparc64_transform(sctx->state, data + done, rounds);
 55		done += rounds * SHA1_BLOCK_SIZE;
 56	}
 57
 58	memcpy(sctx->buffer, data + done, len - done);
 59}
 60
 61static int sha1_sparc64_update(struct shash_desc *desc, const u8 *data,
 62			       unsigned int len)
 63{
 64	struct sha1_state *sctx = shash_desc_ctx(desc);
 65	unsigned int partial = sctx->count % SHA1_BLOCK_SIZE;
 66
 67	/* Handle the fast case right here */
 68	if (partial + len < SHA1_BLOCK_SIZE) {
 69		sctx->count += len;
 70		memcpy(sctx->buffer + partial, data, len);
 71	} else
 72		__sha1_sparc64_update(sctx, data, len, partial);
 73
 74	return 0;
 75}
 76
 77/* Add padding and return the message digest. */
 78static int sha1_sparc64_final(struct shash_desc *desc, u8 *out)
 79{
 80	struct sha1_state *sctx = shash_desc_ctx(desc);
 81	unsigned int i, index, padlen;
 82	__be32 *dst = (__be32 *)out;
 83	__be64 bits;
 84	static const u8 padding[SHA1_BLOCK_SIZE] = { 0x80, };
 85
 86	bits = cpu_to_be64(sctx->count << 3);
 87
 88	/* Pad out to 56 mod 64 and append length */
 89	index = sctx->count % SHA1_BLOCK_SIZE;
 90	padlen = (index < 56) ? (56 - index) : ((SHA1_BLOCK_SIZE+56) - index);
 91
 92	/* We need to fill a whole block for __sha1_sparc64_update() */
 93	if (padlen <= 56) {
 94		sctx->count += padlen;
 95		memcpy(sctx->buffer + index, padding, padlen);
 96	} else {
 97		__sha1_sparc64_update(sctx, padding, padlen, index);
 98	}
 99	__sha1_sparc64_update(sctx, (const u8 *)&bits, sizeof(bits), 56);
100
101	/* Store state in digest */
102	for (i = 0; i < 5; i++)
103		dst[i] = cpu_to_be32(sctx->state[i]);
104
105	/* Wipe context */
106	memset(sctx, 0, sizeof(*sctx));
107
108	return 0;
109}
110
111static int sha1_sparc64_export(struct shash_desc *desc, void *out)
112{
113	struct sha1_state *sctx = shash_desc_ctx(desc);
114
115	memcpy(out, sctx, sizeof(*sctx));
116
117	return 0;
118}
119
120static int sha1_sparc64_import(struct shash_desc *desc, const void *in)
121{
122	struct sha1_state *sctx = shash_desc_ctx(desc);
123
124	memcpy(sctx, in, sizeof(*sctx));
125
126	return 0;
127}
128
129static struct shash_alg alg = {
130	.digestsize	=	SHA1_DIGEST_SIZE,
131	.init		=	sha1_sparc64_init,
132	.update		=	sha1_sparc64_update,
133	.final		=	sha1_sparc64_final,
134	.export		=	sha1_sparc64_export,
135	.import		=	sha1_sparc64_import,
136	.descsize	=	sizeof(struct sha1_state),
137	.statesize	=	sizeof(struct sha1_state),
138	.base		=	{
139		.cra_name	=	"sha1",
140		.cra_driver_name=	"sha1-sparc64",
141		.cra_priority	=	SPARC_CR_OPCODE_PRIORITY,
142		.cra_flags	=	CRYPTO_ALG_TYPE_SHASH,
143		.cra_blocksize	=	SHA1_BLOCK_SIZE,
144		.cra_module	=	THIS_MODULE,
145	}
146};
147
148static bool __init sparc64_has_sha1_opcode(void)
149{
150	unsigned long cfr;
151
152	if (!(sparc64_elf_hwcap & HWCAP_SPARC_CRYPTO))
153		return false;
154
155	__asm__ __volatile__("rd %%asr26, %0" : "=r" (cfr));
156	if (!(cfr & CFR_SHA1))
157		return false;
158
159	return true;
160}
161
162static int __init sha1_sparc64_mod_init(void)
163{
164	if (sparc64_has_sha1_opcode()) {
165		pr_info("Using sparc64 sha1 opcode optimized SHA-1 implementation\n");
166		return crypto_register_shash(&alg);
167	}
168	pr_info("sparc64 sha1 opcode not available.\n");
169	return -ENODEV;
170}
171
172static void __exit sha1_sparc64_mod_fini(void)
173{
174	crypto_unregister_shash(&alg);
175}
176
177module_init(sha1_sparc64_mod_init);
178module_exit(sha1_sparc64_mod_fini);
179
180MODULE_LICENSE("GPL");
181MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm, sparc64 sha1 opcode accelerated");
182
183MODULE_ALIAS_CRYPTO("sha1");
184
185#include "crop_devid.c"
v5.4
  1// SPDX-License-Identifier: GPL-2.0-only
  2/* Glue code for SHA1 hashing optimized for sparc64 crypto opcodes.
  3 *
  4 * This is based largely upon arch/x86/crypto/sha1_ssse3_glue.c
  5 *
  6 * Copyright (c) Alan Smithee.
  7 * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
  8 * Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
  9 * Copyright (c) Mathias Krause <minipli@googlemail.com>
 10 */
 11
 12#define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
 13
 14#include <crypto/internal/hash.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 <crypto/sha.h>
 21
 22#include <asm/pstate.h>
 23#include <asm/elf.h>
 24
 25#include "opcodes.h"
 26
 27asmlinkage void sha1_sparc64_transform(u32 *digest, const char *data,
 28				       unsigned int rounds);
 29
 30static int sha1_sparc64_init(struct shash_desc *desc)
 31{
 32	struct sha1_state *sctx = shash_desc_ctx(desc);
 33
 34	*sctx = (struct sha1_state){
 35		.state = { SHA1_H0, SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4 },
 36	};
 37
 38	return 0;
 39}
 40
 41static void __sha1_sparc64_update(struct sha1_state *sctx, const u8 *data,
 42				  unsigned int len, unsigned int partial)
 43{
 44	unsigned int done = 0;
 45
 46	sctx->count += len;
 47	if (partial) {
 48		done = SHA1_BLOCK_SIZE - partial;
 49		memcpy(sctx->buffer + partial, data, done);
 50		sha1_sparc64_transform(sctx->state, sctx->buffer, 1);
 51	}
 52	if (len - done >= SHA1_BLOCK_SIZE) {
 53		const unsigned int rounds = (len - done) / SHA1_BLOCK_SIZE;
 54
 55		sha1_sparc64_transform(sctx->state, data + done, rounds);
 56		done += rounds * SHA1_BLOCK_SIZE;
 57	}
 58
 59	memcpy(sctx->buffer, data + done, len - done);
 60}
 61
 62static int sha1_sparc64_update(struct shash_desc *desc, const u8 *data,
 63			       unsigned int len)
 64{
 65	struct sha1_state *sctx = shash_desc_ctx(desc);
 66	unsigned int partial = sctx->count % SHA1_BLOCK_SIZE;
 67
 68	/* Handle the fast case right here */
 69	if (partial + len < SHA1_BLOCK_SIZE) {
 70		sctx->count += len;
 71		memcpy(sctx->buffer + partial, data, len);
 72	} else
 73		__sha1_sparc64_update(sctx, data, len, partial);
 74
 75	return 0;
 76}
 77
 78/* Add padding and return the message digest. */
 79static int sha1_sparc64_final(struct shash_desc *desc, u8 *out)
 80{
 81	struct sha1_state *sctx = shash_desc_ctx(desc);
 82	unsigned int i, index, padlen;
 83	__be32 *dst = (__be32 *)out;
 84	__be64 bits;
 85	static const u8 padding[SHA1_BLOCK_SIZE] = { 0x80, };
 86
 87	bits = cpu_to_be64(sctx->count << 3);
 88
 89	/* Pad out to 56 mod 64 and append length */
 90	index = sctx->count % SHA1_BLOCK_SIZE;
 91	padlen = (index < 56) ? (56 - index) : ((SHA1_BLOCK_SIZE+56) - index);
 92
 93	/* We need to fill a whole block for __sha1_sparc64_update() */
 94	if (padlen <= 56) {
 95		sctx->count += padlen;
 96		memcpy(sctx->buffer + index, padding, padlen);
 97	} else {
 98		__sha1_sparc64_update(sctx, padding, padlen, index);
 99	}
100	__sha1_sparc64_update(sctx, (const u8 *)&bits, sizeof(bits), 56);
101
102	/* Store state in digest */
103	for (i = 0; i < 5; i++)
104		dst[i] = cpu_to_be32(sctx->state[i]);
105
106	/* Wipe context */
107	memset(sctx, 0, sizeof(*sctx));
108
109	return 0;
110}
111
112static int sha1_sparc64_export(struct shash_desc *desc, void *out)
113{
114	struct sha1_state *sctx = shash_desc_ctx(desc);
115
116	memcpy(out, sctx, sizeof(*sctx));
117
118	return 0;
119}
120
121static int sha1_sparc64_import(struct shash_desc *desc, const void *in)
122{
123	struct sha1_state *sctx = shash_desc_ctx(desc);
124
125	memcpy(sctx, in, sizeof(*sctx));
126
127	return 0;
128}
129
130static struct shash_alg alg = {
131	.digestsize	=	SHA1_DIGEST_SIZE,
132	.init		=	sha1_sparc64_init,
133	.update		=	sha1_sparc64_update,
134	.final		=	sha1_sparc64_final,
135	.export		=	sha1_sparc64_export,
136	.import		=	sha1_sparc64_import,
137	.descsize	=	sizeof(struct sha1_state),
138	.statesize	=	sizeof(struct sha1_state),
139	.base		=	{
140		.cra_name	=	"sha1",
141		.cra_driver_name=	"sha1-sparc64",
142		.cra_priority	=	SPARC_CR_OPCODE_PRIORITY,
 
143		.cra_blocksize	=	SHA1_BLOCK_SIZE,
144		.cra_module	=	THIS_MODULE,
145	}
146};
147
148static bool __init sparc64_has_sha1_opcode(void)
149{
150	unsigned long cfr;
151
152	if (!(sparc64_elf_hwcap & HWCAP_SPARC_CRYPTO))
153		return false;
154
155	__asm__ __volatile__("rd %%asr26, %0" : "=r" (cfr));
156	if (!(cfr & CFR_SHA1))
157		return false;
158
159	return true;
160}
161
162static int __init sha1_sparc64_mod_init(void)
163{
164	if (sparc64_has_sha1_opcode()) {
165		pr_info("Using sparc64 sha1 opcode optimized SHA-1 implementation\n");
166		return crypto_register_shash(&alg);
167	}
168	pr_info("sparc64 sha1 opcode not available.\n");
169	return -ENODEV;
170}
171
172static void __exit sha1_sparc64_mod_fini(void)
173{
174	crypto_unregister_shash(&alg);
175}
176
177module_init(sha1_sparc64_mod_init);
178module_exit(sha1_sparc64_mod_fini);
179
180MODULE_LICENSE("GPL");
181MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm, sparc64 sha1 opcode accelerated");
182
183MODULE_ALIAS_CRYPTO("sha1");
184
185#include "crop_devid.c"