blob: af4460ec381f62aede34475b3e9e47fd172329ec [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Cryptographic API.
3 *
Jan Glauberc1e26e12006-01-06 00:19:17 -08004 * s390 implementation of the SHA1 Secure Hash Algorithm.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Derived from cryptoapi implementation, adapted for in-place
7 * scatterlist interface. Originally based on the public domain
8 * implementation written by Steve Reid.
9 *
10 * s390 Version:
Jan Glauber86aa9fc2007-02-05 21:18:14 +010011 * Copyright IBM Corp. 2003,2007
12 * Author(s): Thomas Spatzier
13 * Jan Glauber (jan.glauber@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
15 * Derived from "crypto/sha1.c"
16 * Copyright (c) Alan Smithee.
17 * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
18 * Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
19 *
20 * This program is free software; you can redistribute it and/or modify it
21 * under the terms of the GNU General Public License as published by the Free
22 * Software Foundation; either version 2 of the License, or (at your option)
23 * any later version.
24 *
25 */
26#include <linux/init.h>
27#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/crypto.h>
Jan Glauber131a3952007-04-27 16:01:54 +020029
Jan Glauberc1e26e12006-01-06 00:19:17 -080030#include "crypt_s390.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#define SHA1_DIGEST_SIZE 20
33#define SHA1_BLOCK_SIZE 64
34
Jan Glauber131a3952007-04-27 16:01:54 +020035struct s390_sha1_ctx {
36 u64 count; /* message length */
Jan Glauberc1e26e12006-01-06 00:19:17 -080037 u32 state[5];
Jan Glauber131a3952007-04-27 16:01:54 +020038 u8 buf[2 * SHA1_BLOCK_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070039};
40
Herbert Xu6c2bb982006-05-16 22:09:29 +100041static void sha1_init(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
Jan Glauber131a3952007-04-27 16:01:54 +020043 struct s390_sha1_ctx *sctx = crypto_tfm_ctx(tfm);
Jan Glauber86aa9fc2007-02-05 21:18:14 +010044
Jan Glauber131a3952007-04-27 16:01:54 +020045 sctx->state[0] = 0x67452301;
46 sctx->state[1] = 0xEFCDAB89;
47 sctx->state[2] = 0x98BADCFE;
48 sctx->state[3] = 0x10325476;
49 sctx->state[4] = 0xC3D2E1F0;
50 sctx->count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051}
52
Herbert Xu6c2bb982006-05-16 22:09:29 +100053static void sha1_update(struct crypto_tfm *tfm, const u8 *data,
54 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Jan Glauber131a3952007-04-27 16:01:54 +020056 struct s390_sha1_ctx *sctx = crypto_tfm_ctx(tfm);
57 unsigned int index;
58 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Jan Glauber131a3952007-04-27 16:01:54 +020060 /* how much is already in the buffer? */
61 index = sctx->count & 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Jan Glauber131a3952007-04-27 16:01:54 +020063 sctx->count += len;
64
65 if (index + len < SHA1_BLOCK_SIZE)
66 goto store;
67
68 /* process one stored block */
69 if (index) {
70 memcpy(sctx->buf + index, data, SHA1_BLOCK_SIZE - index);
71 ret = crypt_s390_kimd(KIMD_SHA_1, sctx->state, sctx->buf,
72 SHA1_BLOCK_SIZE);
73 BUG_ON(ret != SHA1_BLOCK_SIZE);
74 data += SHA1_BLOCK_SIZE - index;
75 len -= SHA1_BLOCK_SIZE - index;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 }
77
Jan Glauber131a3952007-04-27 16:01:54 +020078 /* process as many blocks as possible */
79 if (len >= SHA1_BLOCK_SIZE) {
80 ret = crypt_s390_kimd(KIMD_SHA_1, sctx->state, data,
81 len & ~(SHA1_BLOCK_SIZE - 1));
82 BUG_ON(ret != (len & ~(SHA1_BLOCK_SIZE - 1)));
83 data += ret;
84 len -= ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Jan Glauber131a3952007-04-27 16:01:54 +020087store:
88 /* anything left? */
89 if (len)
90 memcpy(sctx->buf + index , data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
93/* Add padding and return the message digest. */
Herbert Xu6c2bb982006-05-16 22:09:29 +100094static void sha1_final(struct crypto_tfm *tfm, u8 *out)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Jan Glauber131a3952007-04-27 16:01:54 +020096 struct s390_sha1_ctx *sctx = crypto_tfm_ctx(tfm);
97 u64 bits;
98 unsigned int index, end;
99 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Jan Glauber86aa9fc2007-02-05 21:18:14 +0100101 /* must perform manual padding */
Jan Glauber131a3952007-04-27 16:01:54 +0200102 index = sctx->count & 0x3f;
103 end = (index < 56) ? SHA1_BLOCK_SIZE : (2 * SHA1_BLOCK_SIZE);
104
105 /* start pad with 1 */
106 sctx->buf[index] = 0x80;
107
108 /* pad with zeros */
109 index++;
110 memset(sctx->buf + index, 0x00, end - index - 8);
111
112 /* append message length */
113 bits = sctx->count * 8;
114 memcpy(sctx->buf + end - 8, &bits, sizeof(bits));
115
116 ret = crypt_s390_kimd(KIMD_SHA_1, sctx->state, sctx->buf, end);
117 BUG_ON(ret != end);
118
Jan Glauber86aa9fc2007-02-05 21:18:14 +0100119 /* copy digest to out */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 memcpy(out, sctx->state, SHA1_DIGEST_SIZE);
Jan Glauber131a3952007-04-27 16:01:54 +0200121
Jan Glauber86aa9fc2007-02-05 21:18:14 +0100122 /* wipe context */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 memset(sctx, 0, sizeof *sctx);
124}
125
126static struct crypto_alg alg = {
127 .cra_name = "sha1",
Jan Glauber86aa9fc2007-02-05 21:18:14 +0100128 .cra_driver_name= "sha1-s390",
Herbert Xu65b75c32006-08-21 21:18:50 +1000129 .cra_priority = CRYPT_S390_PRIORITY,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
131 .cra_blocksize = SHA1_BLOCK_SIZE,
Jan Glauber131a3952007-04-27 16:01:54 +0200132 .cra_ctxsize = sizeof(struct s390_sha1_ctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 .cra_module = THIS_MODULE,
Jan Glauber86aa9fc2007-02-05 21:18:14 +0100134 .cra_list = LIST_HEAD_INIT(alg.cra_list),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 .cra_u = { .digest = {
136 .dia_digestsize = SHA1_DIGEST_SIZE,
Jan Glauber86aa9fc2007-02-05 21:18:14 +0100137 .dia_init = sha1_init,
138 .dia_update = sha1_update,
139 .dia_final = sha1_final } }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140};
141
Jan Glauber86aa9fc2007-02-05 21:18:14 +0100142static int __init init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Jan Glauber86aa9fc2007-02-05 21:18:14 +0100144 if (!crypt_s390_func_available(KIMD_SHA_1))
145 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Jan Glauber86aa9fc2007-02-05 21:18:14 +0100147 return crypto_register_alg(&alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
Jan Glauber86aa9fc2007-02-05 21:18:14 +0100150static void __exit fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 crypto_unregister_alg(&alg);
153}
154
155module_init(init);
156module_exit(fini);
157
158MODULE_ALIAS("sha1");
159
160MODULE_LICENSE("GPL");
161MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm");