Merge tag 's390-5.4-2' of git://git.kernel.org/pub/scm/linux/kernel/git/s390/linux
[linux-2.6-block.git] / arch / s390 / crypto / sha256_s390.c
CommitLineData
20a884f5 1// SPDX-License-Identifier: GPL-2.0+
0a497c17
JG
2/*
3 * Cryptographic API.
4 *
e3b4f515 5 * s390 implementation of the SHA256 and SHA224 Secure Hash Algorithm.
0a497c17
JG
6 *
7 * s390 Version:
a53c8fab 8 * Copyright IBM Corp. 2005, 2011
0a497c17 9 * Author(s): Jan Glauber (jang@de.ibm.com)
0a497c17 10 */
563f346d 11#include <crypto/internal/hash.h>
0a497c17
JG
12#include <linux/init.h>
13#include <linux/module.h>
d05377c1 14#include <linux/cpufeature.h>
5265eeb2 15#include <crypto/sha.h>
c7d4d259 16#include <asm/cpacf.h>
0a497c17 17
604973f1 18#include "sha.h"
0a497c17 19
b86fc489 20static int s390_sha256_init(struct shash_desc *desc)
0a497c17 21{
563f346d 22 struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
0a497c17 23
5265eeb2
JG
24 sctx->state[0] = SHA256_H0;
25 sctx->state[1] = SHA256_H1;
26 sctx->state[2] = SHA256_H2;
27 sctx->state[3] = SHA256_H3;
28 sctx->state[4] = SHA256_H4;
29 sctx->state[5] = SHA256_H5;
30 sctx->state[6] = SHA256_H6;
31 sctx->state[7] = SHA256_H7;
0a497c17 32 sctx->count = 0;
c7d4d259 33 sctx->func = CPACF_KIMD_SHA_256;
563f346d
HX
34
35 return 0;
0a497c17
JG
36}
37
f63559be
HX
38static int sha256_export(struct shash_desc *desc, void *out)
39{
40 struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
41 struct sha256_state *octx = out;
42
43 octx->count = sctx->count;
44 memcpy(octx->state, sctx->state, sizeof(octx->state));
45 memcpy(octx->buf, sctx->buf, sizeof(octx->buf));
46 return 0;
47}
48
81bd5f6c 49static int sha256_import(struct shash_desc *desc, const void *in)
f63559be 50{
2a549c36 51 struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
81bd5f6c 52 const struct sha256_state *ictx = in;
f63559be
HX
53
54 sctx->count = ictx->count;
55 memcpy(sctx->state, ictx->state, sizeof(ictx->state));
56 memcpy(sctx->buf, ictx->buf, sizeof(ictx->buf));
c7d4d259 57 sctx->func = CPACF_KIMD_SHA_256;
f63559be
HX
58 return 0;
59}
60
e3b4f515 61static struct shash_alg sha256_alg = {
563f346d 62 .digestsize = SHA256_DIGEST_SIZE,
b86fc489 63 .init = s390_sha256_init,
563f346d
HX
64 .update = s390_sha_update,
65 .final = s390_sha_final,
f63559be
HX
66 .export = sha256_export,
67 .import = sha256_import,
563f346d 68 .descsize = sizeof(struct s390_sha_ctx),
f63559be 69 .statesize = sizeof(struct sha256_state),
563f346d
HX
70 .base = {
71 .cra_name = "sha256",
72 .cra_driver_name= "sha256-s390",
c7d4d259 73 .cra_priority = 300,
563f346d
HX
74 .cra_blocksize = SHA256_BLOCK_SIZE,
75 .cra_module = THIS_MODULE,
76 }
0a497c17
JG
77};
78
b86fc489 79static int s390_sha224_init(struct shash_desc *desc)
0a497c17 80{
e3b4f515
JG
81 struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
82
83 sctx->state[0] = SHA224_H0;
84 sctx->state[1] = SHA224_H1;
85 sctx->state[2] = SHA224_H2;
86 sctx->state[3] = SHA224_H3;
87 sctx->state[4] = SHA224_H4;
88 sctx->state[5] = SHA224_H5;
89 sctx->state[6] = SHA224_H6;
90 sctx->state[7] = SHA224_H7;
91 sctx->count = 0;
c7d4d259 92 sctx->func = CPACF_KIMD_SHA_256;
e3b4f515
JG
93
94 return 0;
95}
96
97static struct shash_alg sha224_alg = {
98 .digestsize = SHA224_DIGEST_SIZE,
b86fc489 99 .init = s390_sha224_init,
e3b4f515
JG
100 .update = s390_sha_update,
101 .final = s390_sha_final,
102 .export = sha256_export,
103 .import = sha256_import,
104 .descsize = sizeof(struct s390_sha_ctx),
105 .statesize = sizeof(struct sha256_state),
106 .base = {
107 .cra_name = "sha224",
108 .cra_driver_name= "sha224-s390",
c7d4d259 109 .cra_priority = 300,
e3b4f515
JG
110 .cra_blocksize = SHA224_BLOCK_SIZE,
111 .cra_module = THIS_MODULE,
112 }
113};
114
115static int __init sha256_s390_init(void)
116{
117 int ret;
118
69c0e360 119 if (!cpacf_query_func(CPACF_KIMD, CPACF_KIMD_SHA_256))
45488c48 120 return -ENODEV;
e3b4f515
JG
121 ret = crypto_register_shash(&sha256_alg);
122 if (ret < 0)
123 goto out;
124 ret = crypto_register_shash(&sha224_alg);
125 if (ret < 0)
126 crypto_unregister_shash(&sha256_alg);
127out:
128 return ret;
0a497c17
JG
129}
130
9f7819c1 131static void __exit sha256_s390_fini(void)
0a497c17 132{
e3b4f515
JG
133 crypto_unregister_shash(&sha224_alg);
134 crypto_unregister_shash(&sha256_alg);
0a497c17
JG
135}
136
d05377c1 137module_cpu_feature_match(MSA, sha256_s390_init);
9f7819c1 138module_exit(sha256_s390_fini);
0a497c17 139
5d26a105
KC
140MODULE_ALIAS_CRYPTO("sha256");
141MODULE_ALIAS_CRYPTO("sha224");
0a497c17 142MODULE_LICENSE("GPL");
e3b4f515 143MODULE_DESCRIPTION("SHA256 and SHA224 Secure Hash Algorithm");