forked from skristiansson/linux
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CRYPTO] s390: Generic sha_update and sha_final
The sha_{update|final} functions are similar for every sha variant. Since that is error-prone and redundant replace these functions by a shared generic implementation for s390. Signed-off-by: Jan Glauber <jang@linux.vnet.ibm.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
- Loading branch information
Showing
5 changed files
with
138 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* 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. | ||
* | ||
*/ | ||
#ifndef _CRYPTO_ARCH_S390_SHA_H | ||
#define _CRYPTO_ARCH_S390_SHA_H | ||
|
||
#include <linux/crypto.h> | ||
#include <crypto/sha.h> | ||
|
||
/* must be big enough for the largest SHA variant */ | ||
#define SHA_MAX_BLOCK_SIZE SHA256_BLOCK_SIZE | ||
|
||
struct s390_sha_ctx { | ||
u64 count; /* message length in bytes */ | ||
u32 state[8]; | ||
u8 buf[2 * SHA_MAX_BLOCK_SIZE]; | ||
int func; /* KIMD function to use */ | ||
}; | ||
|
||
void s390_sha_update(struct crypto_tfm *tfm, const u8 *data, unsigned int len); | ||
void s390_sha_final(struct crypto_tfm *tfm, u8 *out); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* 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. | ||
* | ||
*/ | ||
|
||
#include <linux/crypto.h> | ||
#include "sha.h" | ||
#include "crypt_s390.h" | ||
|
||
void s390_sha_update(struct crypto_tfm *tfm, const u8 *data, unsigned int len) | ||
{ | ||
struct s390_sha_ctx *ctx = crypto_tfm_ctx(tfm); | ||
unsigned int bsize = crypto_tfm_alg_blocksize(tfm); | ||
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); | ||
} | ||
EXPORT_SYMBOL_GPL(s390_sha_update); | ||
|
||
void s390_sha_final(struct crypto_tfm *tfm, u8 *out) | ||
{ | ||
struct s390_sha_ctx *ctx = crypto_tfm_ctx(tfm); | ||
unsigned int bsize = crypto_tfm_alg_blocksize(tfm); | ||
u64 bits; | ||
unsigned int index, end; | ||
int ret; | ||
|
||
/* must perform manual padding */ | ||
index = ctx->count & (bsize - 1); | ||
end = (index < bsize - 8) ? bsize : (2 * bsize); | ||
|
||
/* start pad with 1 */ | ||
ctx->buf[index] = 0x80; | ||
index++; | ||
|
||
/* pad with zeros */ | ||
memset(ctx->buf + index, 0x00, end - index - 8); | ||
|
||
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 */ | ||
memcpy(out, ctx->state, crypto_hash_digestsize(crypto_hash_cast(tfm))); | ||
/* wipe context */ | ||
memset(ctx, 0, sizeof *ctx); | ||
} | ||
EXPORT_SYMBOL_GPL(s390_sha_final); | ||
|
||
MODULE_LICENSE("GPL"); | ||
MODULE_DESCRIPTION("s390 SHA cipher common functions"); |