xref: /openbmc/linux/arch/s390/crypto/sha_common.c (revision 8795a739)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Cryptographic API.
4  *
5  * s390 generic implementation of the SHA Secure Hash Algorithms.
6  *
7  * Copyright IBM Corp. 2007
8  * Author(s): Jan Glauber (jang@de.ibm.com)
9  */
10 
11 #include <crypto/internal/hash.h>
12 #include <linux/module.h>
13 #include <asm/cpacf.h>
14 #include "sha.h"
15 
16 int s390_sha_update(struct shash_desc *desc, const u8 *data, unsigned int len)
17 {
18 	struct s390_sha_ctx *ctx = shash_desc_ctx(desc);
19 	unsigned int bsize = crypto_shash_blocksize(desc->tfm);
20 	unsigned int index, n;
21 
22 	/* how much is already in the buffer? */
23 	index = ctx->count % bsize;
24 	ctx->count += len;
25 
26 	if ((index + len) < bsize)
27 		goto store;
28 
29 	/* process one stored block */
30 	if (index) {
31 		memcpy(ctx->buf + index, data, bsize - index);
32 		cpacf_kimd(ctx->func, ctx->state, ctx->buf, bsize);
33 		data += bsize - index;
34 		len -= bsize - index;
35 		index = 0;
36 	}
37 
38 	/* process as many blocks as possible */
39 	if (len >= bsize) {
40 		n = (len / bsize) * bsize;
41 		cpacf_kimd(ctx->func, ctx->state, data, n);
42 		data += n;
43 		len -= n;
44 	}
45 store:
46 	if (len)
47 		memcpy(ctx->buf + index , data, len);
48 
49 	return 0;
50 }
51 EXPORT_SYMBOL_GPL(s390_sha_update);
52 
53 static int s390_crypto_shash_parmsize(int func)
54 {
55 	switch (func) {
56 	case CPACF_KLMD_SHA_1:
57 		return 20;
58 	case CPACF_KLMD_SHA_256:
59 		return 32;
60 	case CPACF_KLMD_SHA_512:
61 		return 64;
62 	case CPACF_KLMD_SHA3_224:
63 	case CPACF_KLMD_SHA3_256:
64 	case CPACF_KLMD_SHA3_384:
65 	case CPACF_KLMD_SHA3_512:
66 		return 200;
67 	default:
68 		return -EINVAL;
69 	}
70 }
71 
72 int s390_sha_final(struct shash_desc *desc, u8 *out)
73 {
74 	struct s390_sha_ctx *ctx = shash_desc_ctx(desc);
75 	unsigned int bsize = crypto_shash_blocksize(desc->tfm);
76 	u64 bits;
77 	unsigned int n, mbl_offset;
78 
79 	n = ctx->count % bsize;
80 	bits = ctx->count * 8;
81 	mbl_offset = s390_crypto_shash_parmsize(ctx->func) / sizeof(u32);
82 	if (mbl_offset < 0)
83 		return -EINVAL;
84 
85 	/* set total msg bit length (mbl) in CPACF parmblock */
86 	switch (ctx->func) {
87 	case CPACF_KLMD_SHA_1:
88 	case CPACF_KLMD_SHA_256:
89 		memcpy(ctx->state + mbl_offset, &bits, sizeof(bits));
90 		break;
91 	case CPACF_KLMD_SHA_512:
92 		/*
93 		 * the SHA512 parmblock has a 128-bit mbl field, clear
94 		 * high-order u64 field, copy bits to low-order u64 field
95 		 */
96 		memset(ctx->state + mbl_offset, 0x00, sizeof(bits));
97 		mbl_offset += sizeof(u64) / sizeof(u32);
98 		memcpy(ctx->state + mbl_offset, &bits, sizeof(bits));
99 		break;
100 	case CPACF_KLMD_SHA3_224:
101 	case CPACF_KLMD_SHA3_256:
102 	case CPACF_KLMD_SHA3_384:
103 	case CPACF_KLMD_SHA3_512:
104 		break;
105 	default:
106 		return -EINVAL;
107 	}
108 
109 	cpacf_klmd(ctx->func, ctx->state, ctx->buf, n);
110 
111 	/* copy digest to out */
112 	memcpy(out, ctx->state, crypto_shash_digestsize(desc->tfm));
113 	/* wipe context */
114 	memset(ctx, 0, sizeof *ctx);
115 
116 	return 0;
117 }
118 EXPORT_SYMBOL_GPL(s390_sha_final);
119 
120 MODULE_LICENSE("GPL");
121 MODULE_DESCRIPTION("s390 SHA cipher common functions");
122