Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Cryptographic API.
  4 *
  5 * powerpc implementation of the SHA1 Secure Hash Algorithm.
  6 *
  7 * Derived from cryptoapi implementation, adapted for in-place
  8 * scatterlist interface.
  9 *
 10 * Derived from "crypto/sha1.c"
 11 * Copyright (c) Alan Smithee.
 12 * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
 13 * Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
 
 
 
 
 
 
 14 */
 15#include <crypto/internal/hash.h>
 16#include <linux/init.h>
 17#include <linux/module.h>
 18#include <linux/mm.h>
 
 19#include <linux/types.h>
 20#include <crypto/sha1.h>
 21#include <crypto/sha1_base.h>
 22#include <asm/byteorder.h>
 23
 24void powerpc_sha_transform(u32 *state, const u8 *src);
 
 
 
 
 
 
 
 
 
 
 
 25
 26static int powerpc_sha1_update(struct shash_desc *desc, const u8 *data,
 27			       unsigned int len)
 28{
 29	struct sha1_state *sctx = shash_desc_ctx(desc);
 30	unsigned int partial, done;
 31	const u8 *src;
 32
 33	partial = sctx->count & 0x3f;
 34	sctx->count += len;
 35	done = 0;
 36	src = data;
 37
 38	if ((partial + len) > 63) {
 
 39
 40		if (partial) {
 41			done = -partial;
 42			memcpy(sctx->buffer + partial, data, done + 64);
 43			src = sctx->buffer;
 44		}
 45
 46		do {
 47			powerpc_sha_transform(sctx->state, src);
 48			done += 64;
 49			src = data + done;
 50		} while (done + 63 < len);
 51
 
 52		partial = 0;
 53	}
 54	memcpy(sctx->buffer + partial, src, len - done);
 55
 56	return 0;
 57}
 58
 59
 60/* Add padding and return the message digest. */
 61static int powerpc_sha1_final(struct shash_desc *desc, u8 *out)
 62{
 63	struct sha1_state *sctx = shash_desc_ctx(desc);
 64	__be32 *dst = (__be32 *)out;
 65	u32 i, index, padlen;
 66	__be64 bits;
 67	static const u8 padding[64] = { 0x80, };
 68
 69	bits = cpu_to_be64(sctx->count << 3);
 70
 71	/* Pad out to 56 mod 64 */
 72	index = sctx->count & 0x3f;
 73	padlen = (index < 56) ? (56 - index) : ((64+56) - index);
 74	powerpc_sha1_update(desc, padding, padlen);
 75
 76	/* Append length */
 77	powerpc_sha1_update(desc, (const u8 *)&bits, sizeof(bits));
 78
 79	/* Store state in digest */
 80	for (i = 0; i < 5; i++)
 81		dst[i] = cpu_to_be32(sctx->state[i]);
 82
 83	/* Wipe context */
 84	memset(sctx, 0, sizeof *sctx);
 85
 86	return 0;
 87}
 88
 89static int powerpc_sha1_export(struct shash_desc *desc, void *out)
 90{
 91	struct sha1_state *sctx = shash_desc_ctx(desc);
 92
 93	memcpy(out, sctx, sizeof(*sctx));
 94	return 0;
 95}
 96
 97static int powerpc_sha1_import(struct shash_desc *desc, const void *in)
 98{
 99	struct sha1_state *sctx = shash_desc_ctx(desc);
100
101	memcpy(sctx, in, sizeof(*sctx));
102	return 0;
103}
104
105static struct shash_alg alg = {
106	.digestsize	=	SHA1_DIGEST_SIZE,
107	.init		=	sha1_base_init,
108	.update		=	powerpc_sha1_update,
109	.final		=	powerpc_sha1_final,
110	.export		=	powerpc_sha1_export,
111	.import		=	powerpc_sha1_import,
112	.descsize	=	sizeof(struct sha1_state),
113	.statesize	=	sizeof(struct sha1_state),
114	.base		=	{
115		.cra_name	=	"sha1",
116		.cra_driver_name=	"sha1-powerpc",
 
117		.cra_blocksize	=	SHA1_BLOCK_SIZE,
118		.cra_module	=	THIS_MODULE,
119	}
120};
121
122static int __init sha1_powerpc_mod_init(void)
123{
124	return crypto_register_shash(&alg);
125}
126
127static void __exit sha1_powerpc_mod_fini(void)
128{
129	crypto_unregister_shash(&alg);
130}
131
132module_init(sha1_powerpc_mod_init);
133module_exit(sha1_powerpc_mod_fini);
134
135MODULE_LICENSE("GPL");
136MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm");
137
138MODULE_ALIAS_CRYPTO("sha1");
139MODULE_ALIAS_CRYPTO("sha1-powerpc");
v4.17
 
  1/*
  2 * Cryptographic API.
  3 *
  4 * powerpc implementation of the SHA1 Secure Hash Algorithm.
  5 *
  6 * Derived from cryptoapi implementation, adapted for in-place
  7 * scatterlist interface.
  8 *
  9 * Derived from "crypto/sha1.c"
 10 * Copyright (c) Alan Smithee.
 11 * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
 12 * Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
 13 *
 14 * This program is free software; you can redistribute it and/or modify it
 15 * under the terms of the GNU General Public License as published by the Free
 16 * Software Foundation; either version 2 of the License, or (at your option)
 17 * any later version.
 18 *
 19 */
 20#include <crypto/internal/hash.h>
 21#include <linux/init.h>
 22#include <linux/module.h>
 23#include <linux/mm.h>
 24#include <linux/cryptohash.h>
 25#include <linux/types.h>
 26#include <crypto/sha.h>
 
 27#include <asm/byteorder.h>
 28
 29extern void powerpc_sha_transform(u32 *state, const u8 *src, u32 *temp);
 30
 31static int sha1_init(struct shash_desc *desc)
 32{
 33	struct sha1_state *sctx = shash_desc_ctx(desc);
 34
 35	*sctx = (struct sha1_state){
 36		.state = { SHA1_H0, SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4 },
 37	};
 38
 39	return 0;
 40}
 41
 42static int sha1_update(struct shash_desc *desc, const u8 *data,
 43			unsigned int len)
 44{
 45	struct sha1_state *sctx = shash_desc_ctx(desc);
 46	unsigned int partial, done;
 47	const u8 *src;
 48
 49	partial = sctx->count & 0x3f;
 50	sctx->count += len;
 51	done = 0;
 52	src = data;
 53
 54	if ((partial + len) > 63) {
 55		u32 temp[SHA_WORKSPACE_WORDS];
 56
 57		if (partial) {
 58			done = -partial;
 59			memcpy(sctx->buffer + partial, data, done + 64);
 60			src = sctx->buffer;
 61		}
 62
 63		do {
 64			powerpc_sha_transform(sctx->state, src, temp);
 65			done += 64;
 66			src = data + done;
 67		} while (done + 63 < len);
 68
 69		memzero_explicit(temp, sizeof(temp));
 70		partial = 0;
 71	}
 72	memcpy(sctx->buffer + partial, src, len - done);
 73
 74	return 0;
 75}
 76
 77
 78/* Add padding and return the message digest. */
 79static int sha1_final(struct shash_desc *desc, u8 *out)
 80{
 81	struct sha1_state *sctx = shash_desc_ctx(desc);
 82	__be32 *dst = (__be32 *)out;
 83	u32 i, index, padlen;
 84	__be64 bits;
 85	static const u8 padding[64] = { 0x80, };
 86
 87	bits = cpu_to_be64(sctx->count << 3);
 88
 89	/* Pad out to 56 mod 64 */
 90	index = sctx->count & 0x3f;
 91	padlen = (index < 56) ? (56 - index) : ((64+56) - index);
 92	sha1_update(desc, padding, padlen);
 93
 94	/* Append length */
 95	sha1_update(desc, (const u8 *)&bits, sizeof(bits));
 96
 97	/* Store state in digest */
 98	for (i = 0; i < 5; i++)
 99		dst[i] = cpu_to_be32(sctx->state[i]);
100
101	/* Wipe context */
102	memset(sctx, 0, sizeof *sctx);
103
104	return 0;
105}
106
107static int sha1_export(struct shash_desc *desc, void *out)
108{
109	struct sha1_state *sctx = shash_desc_ctx(desc);
110
111	memcpy(out, sctx, sizeof(*sctx));
112	return 0;
113}
114
115static int sha1_import(struct shash_desc *desc, const void *in)
116{
117	struct sha1_state *sctx = shash_desc_ctx(desc);
118
119	memcpy(sctx, in, sizeof(*sctx));
120	return 0;
121}
122
123static struct shash_alg alg = {
124	.digestsize	=	SHA1_DIGEST_SIZE,
125	.init		=	sha1_init,
126	.update		=	sha1_update,
127	.final		=	sha1_final,
128	.export		=	sha1_export,
129	.import		=	sha1_import,
130	.descsize	=	sizeof(struct sha1_state),
131	.statesize	=	sizeof(struct sha1_state),
132	.base		=	{
133		.cra_name	=	"sha1",
134		.cra_driver_name=	"sha1-powerpc",
135		.cra_flags	=	CRYPTO_ALG_TYPE_SHASH,
136		.cra_blocksize	=	SHA1_BLOCK_SIZE,
137		.cra_module	=	THIS_MODULE,
138	}
139};
140
141static int __init sha1_powerpc_mod_init(void)
142{
143	return crypto_register_shash(&alg);
144}
145
146static void __exit sha1_powerpc_mod_fini(void)
147{
148	crypto_unregister_shash(&alg);
149}
150
151module_init(sha1_powerpc_mod_init);
152module_exit(sha1_powerpc_mod_fini);
153
154MODULE_LICENSE("GPL");
155MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm");
156
157MODULE_ALIAS_CRYPTO("sha1");
158MODULE_ALIAS_CRYPTO("sha1-powerpc");