sha_common.c 2.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Cryptographic API.
 *
 * s390 generic implementation of the SHA Secure Hash Algorithms.
 *
 * Copyright IBM Corp. 2007
 * Author(s): Jan Glauber (jang@de.ibm.com)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 */

H
Herbert Xu 已提交
16
#include <crypto/internal/hash.h>
17 18 19
#include "sha.h"
#include "crypt_s390.h"

H
Herbert Xu 已提交
20
int s390_sha_update(struct shash_desc *desc, const u8 *data, unsigned int len)
21
{
H
Herbert Xu 已提交
22 23
	struct s390_sha_ctx *ctx = shash_desc_ctx(desc);
	unsigned int bsize = crypto_shash_blocksize(desc->tfm);
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
	unsigned int index;
	int ret;

	/* how much is already in the buffer? */
	index = ctx->count & (bsize - 1);
	ctx->count += len;

	if ((index + len) < bsize)
		goto store;

	/* process one stored block */
	if (index) {
		memcpy(ctx->buf + index, data, bsize - index);
		ret = crypt_s390_kimd(ctx->func, ctx->state, ctx->buf, bsize);
		BUG_ON(ret != bsize);
		data += bsize - index;
		len -= bsize - index;
	}

	/* process as many blocks as possible */
	if (len >= bsize) {
		ret = crypt_s390_kimd(ctx->func, ctx->state, data,
				      len & ~(bsize - 1));
		BUG_ON(ret != (len & ~(bsize - 1)));
		data += ret;
		len -= ret;
	}
store:
	if (len)
		memcpy(ctx->buf + index , data, len);
H
Herbert Xu 已提交
54 55

	return 0;
56 57 58
}
EXPORT_SYMBOL_GPL(s390_sha_update);

H
Herbert Xu 已提交
59
int s390_sha_final(struct shash_desc *desc, u8 *out)
60
{
H
Herbert Xu 已提交
61 62
	struct s390_sha_ctx *ctx = shash_desc_ctx(desc);
	unsigned int bsize = crypto_shash_blocksize(desc->tfm);
63
	u64 bits;
64
	unsigned int index, end, plen;
65 66
	int ret;

67 68 69
	/* SHA-512 uses 128 bit padding length */
	plen = (bsize > SHA256_BLOCK_SIZE) ? 16 : 8;

70 71
	/* must perform manual padding */
	index = ctx->count & (bsize - 1);
72
	end = (index < bsize - plen) ? bsize : (2 * bsize);
73 74 75 76 77 78 79 80

	/* start pad with 1 */
	ctx->buf[index] = 0x80;
	index++;

	/* pad with zeros */
	memset(ctx->buf + index, 0x00, end - index - 8);

81 82 83 84
	/*
	 * Append message length. Well, SHA-512 wants a 128 bit lenght value,
	 * nevertheless we use u64, should be enough for now...
	 */
85 86 87 88 89 90 91
	bits = ctx->count * 8;
	memcpy(ctx->buf + end - 8, &bits, sizeof(bits));

	ret = crypt_s390_kimd(ctx->func, ctx->state, ctx->buf, end);
	BUG_ON(ret != end);

	/* copy digest to out */
H
Herbert Xu 已提交
92
	memcpy(out, ctx->state, crypto_shash_digestsize(desc->tfm));
93 94
	/* wipe context */
	memset(ctx, 0, sizeof *ctx);
H
Herbert Xu 已提交
95 96

	return 0;
97 98 99 100 101
}
EXPORT_SYMBOL_GPL(s390_sha_final);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("s390 SHA cipher common functions");