blob: d359ec7fdd6bfa8621e1fd822377f9d77e7f32c3 [file] [log] [blame]
Greg Kroah-Hartman20a884f2017-11-24 15:00:34 +01001// SPDX-License-Identifier: GPL-2.0+
Jan Glauber604973f2008-03-06 19:50:20 +08002/*
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 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 2 of the License, or (at your option)
13 * any later version.
14 *
15 */
16
Herbert Xu563f3462009-01-18 20:33:33 +110017#include <crypto/internal/hash.h>
Heiko Carstens3a4c5d52011-07-30 09:25:15 +020018#include <linux/module.h>
Martin Schwidefskyc7d4d252016-03-17 15:22:12 +010019#include <asm/cpacf.h>
Jan Glauber604973f2008-03-06 19:50:20 +080020#include "sha.h"
Jan Glauber604973f2008-03-06 19:50:20 +080021
Herbert Xu563f3462009-01-18 20:33:33 +110022int s390_sha_update(struct shash_desc *desc, const u8 *data, unsigned int len)
Jan Glauber604973f2008-03-06 19:50:20 +080023{
Herbert Xu563f3462009-01-18 20:33:33 +110024 struct s390_sha_ctx *ctx = shash_desc_ctx(desc);
25 unsigned int bsize = crypto_shash_blocksize(desc->tfm);
Martin Schwidefsky0177db02016-08-15 10:41:52 +020026 unsigned int index, n;
Jan Glauber604973f2008-03-06 19:50:20 +080027
28 /* how much is already in the buffer? */
29 index = ctx->count & (bsize - 1);
30 ctx->count += len;
31
32 if ((index + len) < bsize)
33 goto store;
34
35 /* process one stored block */
36 if (index) {
37 memcpy(ctx->buf + index, data, bsize - index);
Martin Schwidefsky0177db02016-08-15 10:41:52 +020038 cpacf_kimd(ctx->func, ctx->state, ctx->buf, bsize);
Jan Glauber604973f2008-03-06 19:50:20 +080039 data += bsize - index;
40 len -= bsize - index;
Herbert Xu9d20b572011-02-07 20:26:06 +110041 index = 0;
Jan Glauber604973f2008-03-06 19:50:20 +080042 }
43
44 /* process as many blocks as possible */
45 if (len >= bsize) {
Martin Schwidefsky0177db02016-08-15 10:41:52 +020046 n = len & ~(bsize - 1);
47 cpacf_kimd(ctx->func, ctx->state, data, n);
48 data += n;
49 len -= n;
Jan Glauber604973f2008-03-06 19:50:20 +080050 }
51store:
52 if (len)
53 memcpy(ctx->buf + index , data, len);
Herbert Xu563f3462009-01-18 20:33:33 +110054
55 return 0;
Jan Glauber604973f2008-03-06 19:50:20 +080056}
57EXPORT_SYMBOL_GPL(s390_sha_update);
58
Herbert Xu563f3462009-01-18 20:33:33 +110059int s390_sha_final(struct shash_desc *desc, u8 *out)
Jan Glauber604973f2008-03-06 19:50:20 +080060{
Herbert Xu563f3462009-01-18 20:33:33 +110061 struct s390_sha_ctx *ctx = shash_desc_ctx(desc);
62 unsigned int bsize = crypto_shash_blocksize(desc->tfm);
Jan Glauber604973f2008-03-06 19:50:20 +080063 u64 bits;
Jan Glauber291dc7c2008-03-06 19:52:00 +080064 unsigned int index, end, plen;
Jan Glauber604973f2008-03-06 19:50:20 +080065
Jan Glauber291dc7c2008-03-06 19:52:00 +080066 /* SHA-512 uses 128 bit padding length */
67 plen = (bsize > SHA256_BLOCK_SIZE) ? 16 : 8;
68
Jan Glauber604973f2008-03-06 19:50:20 +080069 /* must perform manual padding */
70 index = ctx->count & (bsize - 1);
Jan Glauber291dc7c2008-03-06 19:52:00 +080071 end = (index < bsize - plen) ? bsize : (2 * bsize);
Jan Glauber604973f2008-03-06 19:50:20 +080072
73 /* start pad with 1 */
74 ctx->buf[index] = 0x80;
75 index++;
76
77 /* pad with zeros */
78 memset(ctx->buf + index, 0x00, end - index - 8);
79
Jan Glauber291dc7c2008-03-06 19:52:00 +080080 /*
Daniel Mack1537a362010-01-29 15:57:49 +080081 * Append message length. Well, SHA-512 wants a 128 bit length value,
Jan Glauber291dc7c2008-03-06 19:52:00 +080082 * nevertheless we use u64, should be enough for now...
83 */
Jan Glauber604973f2008-03-06 19:50:20 +080084 bits = ctx->count * 8;
85 memcpy(ctx->buf + end - 8, &bits, sizeof(bits));
Martin Schwidefsky0177db02016-08-15 10:41:52 +020086 cpacf_kimd(ctx->func, ctx->state, ctx->buf, end);
Jan Glauber604973f2008-03-06 19:50:20 +080087
88 /* copy digest to out */
Herbert Xu563f3462009-01-18 20:33:33 +110089 memcpy(out, ctx->state, crypto_shash_digestsize(desc->tfm));
Jan Glauber604973f2008-03-06 19:50:20 +080090 /* wipe context */
91 memset(ctx, 0, sizeof *ctx);
Herbert Xu563f3462009-01-18 20:33:33 +110092
93 return 0;
Jan Glauber604973f2008-03-06 19:50:20 +080094}
95EXPORT_SYMBOL_GPL(s390_sha_final);
96
97MODULE_LICENSE("GPL");
98MODULE_DESCRIPTION("s390 SHA cipher common functions");