4 * s390 implementation of the SHA256 Secure Hash Algorithm.
7 * Copyright IBM Corp. 2005,2007
8 * Author(s): Jan Glauber (jang@de.ibm.com)
10 * Derived from "crypto/sha256.c"
11 * and "arch/s390/crypto/sha1_s390.c"
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 2 of the License, or (at your option)
19 #include <linux/init.h>
20 #include <linux/module.h>
21 #include <linux/crypto.h>
23 #include "crypt_s390.h"
25 #define SHA256_DIGEST_SIZE 32
26 #define SHA256_BLOCK_SIZE 64
28 struct s390_sha256_ctx {
29 u64 count; /* message length */
31 u8 buf[2 * SHA256_BLOCK_SIZE];
34 static void sha256_init(struct crypto_tfm *tfm)
36 struct s390_sha256_ctx *sctx = crypto_tfm_ctx(tfm);
38 sctx->state[0] = 0x6a09e667;
39 sctx->state[1] = 0xbb67ae85;
40 sctx->state[2] = 0x3c6ef372;
41 sctx->state[3] = 0xa54ff53a;
42 sctx->state[4] = 0x510e527f;
43 sctx->state[5] = 0x9b05688c;
44 sctx->state[6] = 0x1f83d9ab;
45 sctx->state[7] = 0x5be0cd19;
49 static void sha256_update(struct crypto_tfm *tfm, const u8 *data,
52 struct s390_sha256_ctx *sctx = crypto_tfm_ctx(tfm);
56 /* how much is already in the buffer? */
57 index = sctx->count & 0x3f;
61 if ((index + len) < SHA256_BLOCK_SIZE)
64 /* process one stored block */
66 memcpy(sctx->buf + index, data, SHA256_BLOCK_SIZE - index);
67 ret = crypt_s390_kimd(KIMD_SHA_256, sctx->state, sctx->buf,
69 BUG_ON(ret != SHA256_BLOCK_SIZE);
70 data += SHA256_BLOCK_SIZE - index;
71 len -= SHA256_BLOCK_SIZE - index;
74 /* process as many blocks as possible */
75 if (len >= SHA256_BLOCK_SIZE) {
76 ret = crypt_s390_kimd(KIMD_SHA_256, sctx->state, data,
77 len & ~(SHA256_BLOCK_SIZE - 1));
78 BUG_ON(ret != (len & ~(SHA256_BLOCK_SIZE - 1)));
86 memcpy(sctx->buf + index , data, len);
89 /* Add padding and return the message digest */
90 static void sha256_final(struct crypto_tfm *tfm, u8 *out)
92 struct s390_sha256_ctx *sctx = crypto_tfm_ctx(tfm);
94 unsigned int index, end;
97 /* must perform manual padding */
98 index = sctx->count & 0x3f;
99 end = (index < 56) ? SHA256_BLOCK_SIZE : (2 * SHA256_BLOCK_SIZE);
101 /* start pad with 1 */
102 sctx->buf[index] = 0x80;
106 memset(sctx->buf + index, 0x00, end - index - 8);
108 /* append message length */
109 bits = sctx->count * 8;
110 memcpy(sctx->buf + end - 8, &bits, sizeof(bits));
112 ret = crypt_s390_kimd(KIMD_SHA_256, sctx->state, sctx->buf, end);
115 /* copy digest to out */
116 memcpy(out, sctx->state, SHA256_DIGEST_SIZE);
119 memset(sctx, 0, sizeof *sctx);
122 static struct crypto_alg alg = {
123 .cra_name = "sha256",
124 .cra_driver_name = "sha256-s390",
125 .cra_priority = CRYPT_S390_PRIORITY,
126 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
127 .cra_blocksize = SHA256_BLOCK_SIZE,
128 .cra_ctxsize = sizeof(struct s390_sha256_ctx),
129 .cra_module = THIS_MODULE,
130 .cra_list = LIST_HEAD_INIT(alg.cra_list),
131 .cra_u = { .digest = {
132 .dia_digestsize = SHA256_DIGEST_SIZE,
133 .dia_init = sha256_init,
134 .dia_update = sha256_update,
135 .dia_final = sha256_final } }
138 static int init(void)
140 if (!crypt_s390_func_available(KIMD_SHA_256))
143 return crypto_register_alg(&alg);
146 static void __exit fini(void)
148 crypto_unregister_alg(&alg);
154 MODULE_ALIAS("sha256");
156 MODULE_LICENSE("GPL");
157 MODULE_DESCRIPTION("SHA256 Secure Hash Algorithm");