blob: 2c76e7bee41ca95191df3a9bbefb5414c0eed06e [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:
7 * Copyright (C) 2005 IBM Deutschland GmbH, IBM Corporation
8 * Author(s): Jan Glauber (jang@de.ibm.com)
9 *
10 * Derived from "crypto/sha256.c"
11 * 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 {
29 u64 count;
30 u32 state[8];
31 u8 buf[2 * SHA256_BLOCK_SIZE];
32};
33
34static void sha256_init(void *ctx)
35{
36 struct s390_sha256_ctx *sctx = ctx;
37
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
49static void sha256_update(void *ctx, const u8 *data, unsigned int len)
50{
51 struct s390_sha256_ctx *sctx = ctx;
52 unsigned int index;
Jan Glauber7ffbc9d2006-01-14 13:20:56 -080053 int ret;
Jan Glauber0a497c172006-01-06 00:19:18 -080054
55 /* how much is already in the buffer? */
56 index = sctx->count / 8 & 0x3f;
57
58 /* update message bit length */
59 sctx->count += len * 8;
60
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
89static void pad_message(struct s390_sha256_ctx* sctx)
90{
91 int index, end;
92
93 index = sctx->count / 8 & 0x3f;
94 end = index < 56 ? SHA256_BLOCK_SIZE : 2 * SHA256_BLOCK_SIZE;
95
96 /* start pad with 1 */
97 sctx->buf[index] = 0x80;
98
99 /* pad with zeros */
100 index++;
101 memset(sctx->buf + index, 0x00, end - index - 8);
102
103 /* append message length */
104 memcpy(sctx->buf + end - 8, &sctx->count, sizeof sctx->count);
105
106 sctx->count = end * 8;
107}
108
109/* Add padding and return the message digest */
110static void sha256_final(void* ctx, u8 *out)
111{
112 struct s390_sha256_ctx *sctx = ctx;
113
114 /* must perform manual padding */
115 pad_message(sctx);
116
117 crypt_s390_kimd(KIMD_SHA_256, sctx->state, sctx->buf,
118 sctx->count / 8);
119
120 /* copy digest to out */
121 memcpy(out, sctx->state, SHA256_DIGEST_SIZE);
122
123 /* wipe context */
124 memset(sctx, 0, sizeof *sctx);
125}
126
127static struct crypto_alg alg = {
128 .cra_name = "sha256",
129 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
130 .cra_blocksize = SHA256_BLOCK_SIZE,
131 .cra_ctxsize = sizeof(struct s390_sha256_ctx),
132 .cra_module = THIS_MODULE,
133 .cra_list = LIST_HEAD_INIT(alg.cra_list),
134 .cra_u = { .digest = {
135 .dia_digestsize = SHA256_DIGEST_SIZE,
Jan Glauber7ffbc9d2006-01-14 13:20:56 -0800136 .dia_init = sha256_init,
137 .dia_update = sha256_update,
138 .dia_final = sha256_final } }
Jan Glauber0a497c172006-01-06 00:19:18 -0800139};
140
141static int init(void)
142{
143 int ret;
144
145 if (!crypt_s390_func_available(KIMD_SHA_256))
146 return -ENOSYS;
147
148 ret = crypto_register_alg(&alg);
149 if (ret != 0)
150 printk(KERN_INFO "crypt_s390: sha256_s390 couldn't be loaded.");
151 return ret;
152}
153
154static void __exit fini(void)
155{
156 crypto_unregister_alg(&alg);
157}
158
159module_init(init);
160module_exit(fini);
161
162MODULE_ALIAS("sha256");
163
164MODULE_LICENSE("GPL");
165MODULE_DESCRIPTION("SHA256 Secure Hash Algorithm");