1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Cryptographic API.
4 *
5 * s390 implementation of the SHA1 Secure Hash Algorithm.
6 *
7 * Derived from cryptoapi implementation, adapted for in-place
8 * scatterlist interface. Originally based on the public domain
9 * implementation written by Steve Reid.
10 *
11 * s390 Version:
12 * Copyright IBM Corp. 2003, 2007
13 * Author(s): Thomas Spatzier
14 * Jan Glauber (jan.glauber@de.ibm.com)
15 *
16 * Derived from "crypto/sha1_generic.c"
17 * Copyright (c) Alan Smithee.
18 * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
19 * Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
20 */
21 #include <crypto/internal/hash.h>
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/cpufeature.h>
25 #include <crypto/sha1.h>
26 #include <asm/cpacf.h>
27
28 #include "sha.h"
29
s390_sha1_init(struct shash_desc * desc)30 static int s390_sha1_init(struct shash_desc *desc)
31 {
32 struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
33
34 sctx->state[0] = SHA1_H0;
35 sctx->state[1] = SHA1_H1;
36 sctx->state[2] = SHA1_H2;
37 sctx->state[3] = SHA1_H3;
38 sctx->state[4] = SHA1_H4;
39 sctx->count = 0;
40 sctx->func = CPACF_KIMD_SHA_1;
41 sctx->first_message_part = 0;
42
43 return 0;
44 }
45
s390_sha1_export(struct shash_desc * desc,void * out)46 static int s390_sha1_export(struct shash_desc *desc, void *out)
47 {
48 struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
49 struct sha1_state *octx = out;
50
51 octx->count = sctx->count;
52 memcpy(octx->state, sctx->state, sizeof(octx->state));
53 memcpy(octx->buffer, sctx->buf, sizeof(octx->buffer));
54 return 0;
55 }
56
s390_sha1_import(struct shash_desc * desc,const void * in)57 static int s390_sha1_import(struct shash_desc *desc, const void *in)
58 {
59 struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
60 const struct sha1_state *ictx = in;
61
62 sctx->count = ictx->count;
63 memcpy(sctx->state, ictx->state, sizeof(ictx->state));
64 memcpy(sctx->buf, ictx->buffer, sizeof(ictx->buffer));
65 sctx->func = CPACF_KIMD_SHA_1;
66 sctx->first_message_part = 0;
67 return 0;
68 }
69
70 static struct shash_alg alg = {
71 .digestsize = SHA1_DIGEST_SIZE,
72 .init = s390_sha1_init,
73 .update = s390_sha_update,
74 .final = s390_sha_final,
75 .export = s390_sha1_export,
76 .import = s390_sha1_import,
77 .descsize = sizeof(struct s390_sha_ctx),
78 .statesize = sizeof(struct sha1_state),
79 .base = {
80 .cra_name = "sha1",
81 .cra_driver_name= "sha1-s390",
82 .cra_priority = 300,
83 .cra_blocksize = SHA1_BLOCK_SIZE,
84 .cra_module = THIS_MODULE,
85 }
86 };
87
sha1_s390_init(void)88 static int __init sha1_s390_init(void)
89 {
90 if (!cpacf_query_func(CPACF_KIMD, CPACF_KIMD_SHA_1))
91 return -ENODEV;
92 return crypto_register_shash(&alg);
93 }
94
sha1_s390_fini(void)95 static void __exit sha1_s390_fini(void)
96 {
97 crypto_unregister_shash(&alg);
98 }
99
100 module_cpu_feature_match(S390_CPU_FEATURE_MSA, sha1_s390_init);
101 module_exit(sha1_s390_fini);
102
103 MODULE_ALIAS_CRYPTO("sha1");
104 MODULE_LICENSE("GPL");
105 MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm");
106