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