[PATCH] s390: sha256 support
[linux-2.6/mini2440.git] / arch / s390 / crypto / sha256_s390.c
blobb75bdbd476c7384c2dbc891964ee8e9c52f2b117
1 /*
2 * Cryptographic API.
4 * s390 implementation of the SHA256 Secure Hash Algorithm.
6 * s390 Version:
7 * Copyright (C) 2005 IBM Deutschland GmbH, IBM Corporation
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)
16 * any later version.
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;
30 u32 state[8];
31 u8 buf[2 * SHA256_BLOCK_SIZE];
34 static void sha256_init(void *ctx)
36 struct s390_sha256_ctx *sctx = ctx;
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;
47 memset(sctx->buf, 0, sizeof(sctx->buf));
50 static void sha256_update(void *ctx, const u8 *data, unsigned int len)
52 struct s390_sha256_ctx *sctx = ctx;
53 unsigned int index;
55 /* how much is already in the buffer? */
56 index = sctx->count / 8 & 0x3f;
58 /* update message bit length */
59 sctx->count += len * 8;
61 /* process one block */
62 if ((index + len) >= SHA256_BLOCK_SIZE) {
63 memcpy(sctx->buf + index, data, SHA256_BLOCK_SIZE - index);
64 crypt_s390_kimd(KIMD_SHA_256, sctx->state, sctx->buf,
65 SHA256_BLOCK_SIZE);
66 data += SHA256_BLOCK_SIZE - index;
67 len -= SHA256_BLOCK_SIZE - index;
70 /* anything left? */
71 if (len)
72 memcpy(sctx->buf + index , data, len);
75 static void pad_message(struct s390_sha256_ctx* sctx)
77 int index, end;
79 index = sctx->count / 8 & 0x3f;
80 end = index < 56 ? SHA256_BLOCK_SIZE : 2 * SHA256_BLOCK_SIZE;
82 /* start pad with 1 */
83 sctx->buf[index] = 0x80;
85 /* pad with zeros */
86 index++;
87 memset(sctx->buf + index, 0x00, end - index - 8);
89 /* append message length */
90 memcpy(sctx->buf + end - 8, &sctx->count, sizeof sctx->count);
92 sctx->count = end * 8;
95 /* Add padding and return the message digest */
96 static void sha256_final(void* ctx, u8 *out)
98 struct s390_sha256_ctx *sctx = ctx;
100 /* must perform manual padding */
101 pad_message(sctx);
103 crypt_s390_kimd(KIMD_SHA_256, sctx->state, sctx->buf,
104 sctx->count / 8);
106 /* copy digest to out */
107 memcpy(out, sctx->state, SHA256_DIGEST_SIZE);
109 /* wipe context */
110 memset(sctx, 0, sizeof *sctx);
113 static struct crypto_alg alg = {
114 .cra_name = "sha256",
115 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
116 .cra_blocksize = SHA256_BLOCK_SIZE,
117 .cra_ctxsize = sizeof(struct s390_sha256_ctx),
118 .cra_module = THIS_MODULE,
119 .cra_list = LIST_HEAD_INIT(alg.cra_list),
120 .cra_u = { .digest = {
121 .dia_digestsize = SHA256_DIGEST_SIZE,
122 .dia_init = sha256_init,
123 .dia_update = sha256_update,
124 .dia_final = sha256_final } }
127 static int init(void)
129 int ret;
131 if (!crypt_s390_func_available(KIMD_SHA_256))
132 return -ENOSYS;
134 ret = crypto_register_alg(&alg);
135 if (ret != 0)
136 printk(KERN_INFO "crypt_s390: sha256_s390 couldn't be loaded.");
137 return ret;
140 static void __exit fini(void)
142 crypto_unregister_alg(&alg);
145 module_init(init);
146 module_exit(fini);
148 MODULE_ALIAS("sha256");
150 MODULE_LICENSE("GPL");
151 MODULE_DESCRIPTION("SHA256 Secure Hash Algorithm");