aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Glauber <jan.glauber@de.ibm.com>2006-01-06 03:19:18 -0500
committerLinus Torvalds <torvalds@g5.osdl.org>2006-01-06 11:33:50 -0500
commit0a497c17fee428604e06320272ff74415eacdc31 (patch)
treeb7ebb455fc908489a783a32f6171a3c36ccdcc4f
parentc1e26e1ef7ab50f30e5fbf004fe96ed44321ca78 (diff)
[PATCH] s390: sha256 support
Add support for the hardware accelerated sha256 crypto algorithm. Signed-off-by: Jan Glauber <jan.glauber@de.ibm.com> Signed-off-by: Martin Schwidefsky <schwidefsky@de.ibm.com> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
-rw-r--r--arch/s390/crypto/Makefile1
-rw-r--r--arch/s390/crypto/crypt_s390.h2
-rw-r--r--arch/s390/crypto/crypt_s390_query.c6
-rw-r--r--arch/s390/crypto/sha256_s390.c151
-rw-r--r--arch/s390/defconfig1
-rw-r--r--crypto/Kconfig11
6 files changed, 171 insertions, 1 deletions
diff --git a/arch/s390/crypto/Makefile b/arch/s390/crypto/Makefile
index 50843f8d0951..3fccf61ee1d1 100644
--- a/arch/s390/crypto/Makefile
+++ b/arch/s390/crypto/Makefile
@@ -3,6 +3,7 @@
3# 3#
4 4
5obj-$(CONFIG_CRYPTO_SHA1_S390) += sha1_s390.o 5obj-$(CONFIG_CRYPTO_SHA1_S390) += sha1_s390.o
6obj-$(CONFIG_CRYPTO_SHA256_S390) += sha256_s390.o
6obj-$(CONFIG_CRYPTO_DES_S390) += des_s390.o des_check_key.o 7obj-$(CONFIG_CRYPTO_DES_S390) += des_s390.o des_check_key.o
7 8
8obj-$(CONFIG_CRYPTO_TEST) += crypt_s390_query.o 9obj-$(CONFIG_CRYPTO_TEST) += crypt_s390_query.o
diff --git a/arch/s390/crypto/crypt_s390.h b/arch/s390/crypto/crypt_s390.h
index 4d24f6689755..b70a410ac311 100644
--- a/arch/s390/crypto/crypt_s390.h
+++ b/arch/s390/crypto/crypt_s390.h
@@ -61,6 +61,7 @@ enum crypt_s390_kmc_func {
61enum crypt_s390_kimd_func { 61enum crypt_s390_kimd_func {
62 KIMD_QUERY = CRYPT_S390_KIMD | 0, 62 KIMD_QUERY = CRYPT_S390_KIMD | 0,
63 KIMD_SHA_1 = CRYPT_S390_KIMD | 1, 63 KIMD_SHA_1 = CRYPT_S390_KIMD | 1,
64 KIMD_SHA_256 = CRYPT_S390_KIMD | 2,
64}; 65};
65 66
66/* function codes for KLMD (COMPUTE LAST MESSAGE DIGEST) 67/* function codes for KLMD (COMPUTE LAST MESSAGE DIGEST)
@@ -69,6 +70,7 @@ enum crypt_s390_kimd_func {
69enum crypt_s390_klmd_func { 70enum crypt_s390_klmd_func {
70 KLMD_QUERY = CRYPT_S390_KLMD | 0, 71 KLMD_QUERY = CRYPT_S390_KLMD | 0,
71 KLMD_SHA_1 = CRYPT_S390_KLMD | 1, 72 KLMD_SHA_1 = CRYPT_S390_KLMD | 1,
73 KLMD_SHA_256 = CRYPT_S390_KLMD | 2,
72}; 74};
73 75
74/* function codes for KMAC (COMPUTE MESSAGE AUTHENTICATION CODE) 76/* function codes for KMAC (COMPUTE MESSAGE AUTHENTICATION CODE)
diff --git a/arch/s390/crypto/crypt_s390_query.c b/arch/s390/crypto/crypt_s390_query.c
index 0fa6bdf197cf..67081b81a4a3 100644
--- a/arch/s390/crypto/crypt_s390_query.c
+++ b/arch/s390/crypto/crypt_s390_query.c
@@ -48,16 +48,20 @@ static void query_available_functions(void)
48 crypt_s390_func_available(KIMD_QUERY)); 48 crypt_s390_func_available(KIMD_QUERY));
49 printk(KERN_INFO "KIMD_SHA_1: %d\n", 49 printk(KERN_INFO "KIMD_SHA_1: %d\n",
50 crypt_s390_func_available(KIMD_SHA_1)); 50 crypt_s390_func_available(KIMD_SHA_1));
51 printk(KERN_INFO "KIMD_SHA_256: %d\n",
52 crypt_s390_func_available(KIMD_SHA_256));
51 53
52 /* query available KLMD functions */ 54 /* query available KLMD functions */
53 printk(KERN_INFO "KLMD_QUERY: %d\n", 55 printk(KERN_INFO "KLMD_QUERY: %d\n",
54 crypt_s390_func_available(KLMD_QUERY)); 56 crypt_s390_func_available(KLMD_QUERY));
55 printk(KERN_INFO "KLMD_SHA_1: %d\n", 57 printk(KERN_INFO "KLMD_SHA_1: %d\n",
56 crypt_s390_func_available(KLMD_SHA_1)); 58 crypt_s390_func_available(KLMD_SHA_1));
59 printk(KERN_INFO "KLMD_SHA_256: %d\n",
60 crypt_s390_func_available(KLMD_SHA_256));
57 61
58 /* query available KMAC functions */ 62 /* query available KMAC functions */
59 printk(KERN_INFO "KMAC_QUERY: %d\n", 63 printk(KERN_INFO "KMAC_QUERY: %d\n",
60 crypt_s3990_func_available(KMAC_QUERY)); 64 crypt_s390_func_available(KMAC_QUERY));
61 printk(KERN_INFO "KMAC_DEA: %d\n", 65 printk(KERN_INFO "KMAC_DEA: %d\n",
62 crypt_s390_func_available(KMAC_DEA)); 66 crypt_s390_func_available(KMAC_DEA));
63 printk(KERN_INFO "KMAC_TDEA_128: %d\n", 67 printk(KERN_INFO "KMAC_TDEA_128: %d\n",
diff --git a/arch/s390/crypto/sha256_s390.c b/arch/s390/crypto/sha256_s390.c
new file mode 100644
index 000000000000..b75bdbd476c7
--- /dev/null
+++ b/arch/s390/crypto/sha256_s390.c
@@ -0,0 +1,151 @@
1/*
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;
47 memset(sctx->buf, 0, sizeof(sctx->buf));
48}
49
50static void sha256_update(void *ctx, const u8 *data, unsigned int len)
51{
52 struct s390_sha256_ctx *sctx = ctx;
53 unsigned int index;
54
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
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;
68 }
69
70 /* anything left? */
71 if (len)
72 memcpy(sctx->buf + index , data, len);
73}
74
75static void pad_message(struct s390_sha256_ctx* sctx)
76{
77 int index, end;
78
79 index = sctx->count / 8 & 0x3f;
80 end = index < 56 ? SHA256_BLOCK_SIZE : 2 * SHA256_BLOCK_SIZE;
81
82 /* start pad with 1 */
83 sctx->buf[index] = 0x80;
84
85 /* pad with zeros */
86 index++;
87 memset(sctx->buf + index, 0x00, end - index - 8);
88
89 /* append message length */
90 memcpy(sctx->buf + end - 8, &sctx->count, sizeof sctx->count);
91
92 sctx->count = end * 8;
93}
94
95/* Add padding and return the message digest */
96static void sha256_final(void* ctx, u8 *out)
97{
98 struct s390_sha256_ctx *sctx = ctx;
99
100 /* must perform manual padding */
101 pad_message(sctx);
102
103 crypt_s390_kimd(KIMD_SHA_256, sctx->state, sctx->buf,
104 sctx->count / 8);
105
106 /* copy digest to out */
107 memcpy(out, sctx->state, SHA256_DIGEST_SIZE);
108
109 /* wipe context */
110 memset(sctx, 0, sizeof *sctx);
111}
112
113static 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 } }
125};
126
127static int init(void)
128{
129 int ret;
130
131 if (!crypt_s390_func_available(KIMD_SHA_256))
132 return -ENOSYS;
133
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;
138}
139
140static void __exit fini(void)
141{
142 crypto_unregister_alg(&alg);
143}
144
145module_init(init);
146module_exit(fini);
147
148MODULE_ALIAS("sha256");
149
150MODULE_LICENSE("GPL");
151MODULE_DESCRIPTION("SHA256 Secure Hash Algorithm");
diff --git a/arch/s390/defconfig b/arch/s390/defconfig
index 0cb2995bf21c..fd00a9f53d53 100644
--- a/arch/s390/defconfig
+++ b/arch/s390/defconfig
@@ -634,6 +634,7 @@ CONFIG_CRYPTO=y
634# CONFIG_CRYPTO_SHA1 is not set 634# CONFIG_CRYPTO_SHA1 is not set
635# CONFIG_CRYPTO_SHA1_S390 is not set 635# CONFIG_CRYPTO_SHA1_S390 is not set
636# CONFIG_CRYPTO_SHA256 is not set 636# CONFIG_CRYPTO_SHA256 is not set
637# CONFIG_CRYPTO_SHA256_S390 is not set
637# CONFIG_CRYPTO_SHA512 is not set 638# CONFIG_CRYPTO_SHA512 is not set
638# CONFIG_CRYPTO_WP512 is not set 639# CONFIG_CRYPTO_WP512 is not set
639# CONFIG_CRYPTO_TGR192 is not set 640# CONFIG_CRYPTO_TGR192 is not set
diff --git a/crypto/Kconfig b/crypto/Kconfig
index 85af57100923..9fdab742b10f 100644
--- a/crypto/Kconfig
+++ b/crypto/Kconfig
@@ -44,6 +44,7 @@ config CRYPTO_SHA1_S390
44 tristate "SHA1 digest algorithm (s390)" 44 tristate "SHA1 digest algorithm (s390)"
45 depends on CRYPTO && ARCH_S390 45 depends on CRYPTO && ARCH_S390
46 help 46 help
47 This is the s390 hardware accelerated implementation of the
47 SHA-1 secure hash standard (FIPS 180-1/DFIPS 180-2). 48 SHA-1 secure hash standard (FIPS 180-1/DFIPS 180-2).
48 49
49config CRYPTO_SHA256 50config CRYPTO_SHA256
@@ -55,6 +56,16 @@ config CRYPTO_SHA256
55 This version of SHA implements a 256 bit hash with 128 bits of 56 This version of SHA implements a 256 bit hash with 128 bits of
56 security against collision attacks. 57 security against collision attacks.
57 58
59config CRYPTO_SHA256_S390
60 tristate "SHA256 digest algorithm (s390)"
61 depends on CRYPTO && ARCH_S390
62 help
63 This is the s390 hardware accelerated implementation of the
64 SHA256 secure hash standard (DFIPS 180-2).
65
66 This version of SHA implements a 256 bit hash with 128 bits of
67 security against collision attacks.
68
58config CRYPTO_SHA512 69config CRYPTO_SHA512
59 tristate "SHA384 and SHA512 digest algorithms" 70 tristate "SHA384 and SHA512 digest algorithms"
60 depends on CRYPTO 71 depends on CRYPTO