blob: c728bd0ae1ed987b8b222f8df7ce02a2fabcabe8 [file] [log] [blame]
Jan Glauber0a497c172006-01-06 00:19:18 -08001/*
2 * Cryptographic API.
3 *
4 * s390 implementation of the SHA256 Secure Hash Algorithm.
5 *
6 * s390 Version:
Jan Glauber86aa9fc2007-02-05 21:18:14 +01007 * Copyright IBM Corp. 2005,2007
Jan Glauber0a497c172006-01-06 00:19:18 -08008 * Author(s): Jan Glauber (jang@de.ibm.com)
9 *
Sebastian Siewiorad5d2782007-10-08 11:45:10 +080010 * Derived from "crypto/sha256_generic.c"
Jan Glauber0a497c172006-01-06 00:19:18 -080011 * and "arch/s390/crypto/sha1_s390.c"
12 *
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)
16 * any later version.
17 *
18 */
19#include <linux/init.h>
20#include <linux/module.h>
21#include <linux/crypto.h>
22
23#include "crypt_s390.h"
24
25#define SHA256_DIGEST_SIZE 32
26#define SHA256_BLOCK_SIZE 64
27
28struct s390_sha256_ctx {
Jan Glauber131a3952007-04-27 16:01:54 +020029 u64 count; /* message length */
Jan Glauber0a497c172006-01-06 00:19:18 -080030 u32 state[8];
31 u8 buf[2 * SHA256_BLOCK_SIZE];
32};
33
Herbert Xu6c2bb982006-05-16 22:09:29 +100034static void sha256_init(struct crypto_tfm *tfm)
Jan Glauber0a497c172006-01-06 00:19:18 -080035{
Herbert Xu6c2bb982006-05-16 22:09:29 +100036 struct s390_sha256_ctx *sctx = crypto_tfm_ctx(tfm);
Jan Glauber0a497c172006-01-06 00:19:18 -080037
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;
46 sctx->count = 0;
Jan Glauber0a497c172006-01-06 00:19:18 -080047}
48
Herbert Xu6c2bb982006-05-16 22:09:29 +100049static void sha256_update(struct crypto_tfm *tfm, const u8 *data,
50 unsigned int len)
Jan Glauber0a497c172006-01-06 00:19:18 -080051{
Herbert Xu6c2bb982006-05-16 22:09:29 +100052 struct s390_sha256_ctx *sctx = crypto_tfm_ctx(tfm);
Jan Glauber0a497c172006-01-06 00:19:18 -080053 unsigned int index;
Jan Glauber7ffbc9d2006-01-14 13:20:56 -080054 int ret;
Jan Glauber0a497c172006-01-06 00:19:18 -080055
56 /* how much is already in the buffer? */
Jan Glauber131a3952007-04-27 16:01:54 +020057 index = sctx->count & 0x3f;
Jan Glauber0a497c172006-01-06 00:19:18 -080058
Jan Glauber131a3952007-04-27 16:01:54 +020059 sctx->count += len;
Jan Glauber0a497c172006-01-06 00:19:18 -080060
Jan Glauber7ffbc9d2006-01-14 13:20:56 -080061 if ((index + len) < SHA256_BLOCK_SIZE)
62 goto store;
63
64 /* process one stored block */
65 if (index) {
Jan Glauber0a497c172006-01-06 00:19:18 -080066 memcpy(sctx->buf + index, data, SHA256_BLOCK_SIZE - index);
Jan Glauber7ffbc9d2006-01-14 13:20:56 -080067 ret = crypt_s390_kimd(KIMD_SHA_256, sctx->state, sctx->buf,
68 SHA256_BLOCK_SIZE);
69 BUG_ON(ret != SHA256_BLOCK_SIZE);
Jan Glauber0a497c172006-01-06 00:19:18 -080070 data += SHA256_BLOCK_SIZE - index;
71 len -= SHA256_BLOCK_SIZE - index;
72 }
73
Jan Glauber7ffbc9d2006-01-14 13:20:56 -080074 /* 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)));
79 data += ret;
80 len -= ret;
81 }
82
83store:
Jan Glauber0a497c172006-01-06 00:19:18 -080084 /* anything left? */
85 if (len)
86 memcpy(sctx->buf + index , data, len);
87}
88
Jan Glauber131a3952007-04-27 16:01:54 +020089/* Add padding and return the message digest */
90static void sha256_final(struct crypto_tfm *tfm, u8 *out)
Jan Glauber0a497c172006-01-06 00:19:18 -080091{
Jan Glauber131a3952007-04-27 16:01:54 +020092 struct s390_sha256_ctx *sctx = crypto_tfm_ctx(tfm);
93 u64 bits;
94 unsigned int index, end;
95 int ret;
Jan Glauber0a497c172006-01-06 00:19:18 -080096
Jan Glauber131a3952007-04-27 16:01:54 +020097 /* must perform manual padding */
98 index = sctx->count & 0x3f;
99 end = (index < 56) ? SHA256_BLOCK_SIZE : (2 * SHA256_BLOCK_SIZE);
Jan Glauber0a497c172006-01-06 00:19:18 -0800100
101 /* start pad with 1 */
102 sctx->buf[index] = 0x80;
103
104 /* pad with zeros */
105 index++;
106 memset(sctx->buf + index, 0x00, end - index - 8);
107
108 /* append message length */
Jan Glauber131a3952007-04-27 16:01:54 +0200109 bits = sctx->count * 8;
110 memcpy(sctx->buf + end - 8, &bits, sizeof(bits));
Jan Glauber0a497c172006-01-06 00:19:18 -0800111
Jan Glauber131a3952007-04-27 16:01:54 +0200112 ret = crypt_s390_kimd(KIMD_SHA_256, sctx->state, sctx->buf, end);
113 BUG_ON(ret != end);
Jan Glauber0a497c172006-01-06 00:19:18 -0800114
115 /* copy digest to out */
116 memcpy(out, sctx->state, SHA256_DIGEST_SIZE);
117
118 /* wipe context */
119 memset(sctx, 0, sizeof *sctx);
120}
121
122static struct crypto_alg alg = {
123 .cra_name = "sha256",
Herbert Xu65b75c32006-08-21 21:18:50 +1000124 .cra_driver_name = "sha256-s390",
125 .cra_priority = CRYPT_S390_PRIORITY,
Jan Glauber0a497c172006-01-06 00:19:18 -0800126 .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,
Jan Glauber7ffbc9d2006-01-14 13:20:56 -0800133 .dia_init = sha256_init,
134 .dia_update = sha256_update,
135 .dia_final = sha256_final } }
Jan Glauber0a497c172006-01-06 00:19:18 -0800136};
137
138static int init(void)
139{
Jan Glauber0a497c172006-01-06 00:19:18 -0800140 if (!crypt_s390_func_available(KIMD_SHA_256))
Jan Glauber86aa9fc2007-02-05 21:18:14 +0100141 return -EOPNOTSUPP;
Jan Glauber0a497c172006-01-06 00:19:18 -0800142
Jan Glauber86aa9fc2007-02-05 21:18:14 +0100143 return crypto_register_alg(&alg);
Jan Glauber0a497c172006-01-06 00:19:18 -0800144}
145
146static void __exit fini(void)
147{
148 crypto_unregister_alg(&alg);
149}
150
151module_init(init);
152module_exit(fini);
153
154MODULE_ALIAS("sha256");
155
156MODULE_LICENSE("GPL");
157MODULE_DESCRIPTION("SHA256 Secure Hash Algorithm");