blob: 49ca8690ee39b7d53e116274b74bd883698a2e01 [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:
11 * Copyright (C) 2003 IBM Deutschland GmbH, IBM Corporation
12 * Author(s): Thomas Spatzier (tspat@de.ibm.com)
13 *
14 * Derived from "crypto/sha1.c"
15 * Copyright (c) Alan Smithee.
16 * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
17 * Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
18 *
19 * This program is free software; you can redistribute it and/or modify it
20 * under the terms of the GNU General Public License as published by the Free
21 * Software Foundation; either version 2 of the License, or (at your option)
22 * any later version.
23 *
24 */
25#include <linux/init.h>
26#include <linux/module.h>
27#include <linux/mm.h>
28#include <linux/crypto.h>
29#include <asm/scatterlist.h>
30#include <asm/byteorder.h>
Jan Glauberc1e26e12006-01-06 00:19:17 -080031#include "crypt_s390.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#define SHA1_DIGEST_SIZE 20
34#define SHA1_BLOCK_SIZE 64
35
Jan Glauberc1e26e12006-01-06 00:19:17 -080036struct crypt_s390_sha1_ctx {
37 u64 count;
38 u32 state[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 u32 buf_len;
Jan Glauberc1e26e12006-01-06 00:19:17 -080040 u8 buffer[2 * SHA1_BLOCK_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070041};
42
Herbert Xu6c2bb982006-05-16 22:09:29 +100043static void sha1_init(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
Herbert Xu6c2bb982006-05-16 22:09:29 +100045 struct crypt_s390_sha1_ctx *ctx = crypto_tfm_ctx(tfm);
Herbert Xu43600102006-05-16 22:06:54 +100046 static const u32 initstate[5] = {
47 0x67452301,
48 0xEFCDAB89,
49 0x98BADCFE,
50 0x10325476,
51 0xC3D2E1F0
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 };
Herbert Xu43600102006-05-16 22:06:54 +100053
54 ctx->count = 0;
55 memcpy(ctx->state, &initstate, sizeof(initstate));
56 ctx->buf_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057}
58
Herbert Xu6c2bb982006-05-16 22:09:29 +100059static void sha1_update(struct crypto_tfm *tfm, const u8 *data,
60 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Jan Glauberc1e26e12006-01-06 00:19:17 -080062 struct crypt_s390_sha1_ctx *sctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 long imd_len;
64
Herbert Xu6c2bb982006-05-16 22:09:29 +100065 sctx = crypto_tfm_ctx(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 sctx->count += len * 8; //message bit length
67
68 //anything in buffer yet? -> must be completed
69 if (sctx->buf_len && (sctx->buf_len + len) >= SHA1_BLOCK_SIZE) {
70 //complete full block and hash
71 memcpy(sctx->buffer + sctx->buf_len, data,
72 SHA1_BLOCK_SIZE - sctx->buf_len);
Jan Glauberc1e26e12006-01-06 00:19:17 -080073 crypt_s390_kimd(KIMD_SHA_1, sctx->state, sctx->buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 SHA1_BLOCK_SIZE);
75 data += SHA1_BLOCK_SIZE - sctx->buf_len;
76 len -= SHA1_BLOCK_SIZE - sctx->buf_len;
77 sctx->buf_len = 0;
78 }
79
80 //rest of data contains full blocks?
81 imd_len = len & ~0x3ful;
82 if (imd_len){
Jan Glauberc1e26e12006-01-06 00:19:17 -080083 crypt_s390_kimd(KIMD_SHA_1, sctx->state, data, imd_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 data += imd_len;
85 len -= imd_len;
86 }
87 //anything left? store in buffer
88 if (len){
89 memcpy(sctx->buffer + sctx->buf_len , data, len);
90 sctx->buf_len += len;
91 }
92}
93
94
95static void
Jan Glauberc1e26e12006-01-06 00:19:17 -080096pad_message(struct crypt_s390_sha1_ctx* sctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
98 int index;
99
100 index = sctx->buf_len;
101 sctx->buf_len = (sctx->buf_len < 56)?
102 SHA1_BLOCK_SIZE:2 * SHA1_BLOCK_SIZE;
103 //start pad with 1
104 sctx->buffer[index] = 0x80;
105 //pad with zeros
106 index++;
107 memset(sctx->buffer + index, 0x00, sctx->buf_len - index);
108 //append length
109 memcpy(sctx->buffer + sctx->buf_len - 8, &sctx->count,
110 sizeof sctx->count);
111}
112
113/* Add padding and return the message digest. */
Herbert Xu6c2bb982006-05-16 22:09:29 +1000114static void sha1_final(struct crypto_tfm *tfm, u8 *out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Herbert Xu6c2bb982006-05-16 22:09:29 +1000116 struct crypt_s390_sha1_ctx *sctx = crypto_tfm_ctx(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 //must perform manual padding
119 pad_message(sctx);
Jan Glauberc1e26e12006-01-06 00:19:17 -0800120 crypt_s390_kimd(KIMD_SHA_1, sctx->state, sctx->buffer, sctx->buf_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 //copy digest to out
122 memcpy(out, sctx->state, SHA1_DIGEST_SIZE);
123 /* Wipe context */
124 memset(sctx, 0, sizeof *sctx);
125}
126
127static struct crypto_alg alg = {
128 .cra_name = "sha1",
Herbert Xu65b75c32006-08-21 21:18:50 +1000129 .cra_driver_name = "sha1-s390",
130 .cra_priority = CRYPT_S390_PRIORITY,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
132 .cra_blocksize = SHA1_BLOCK_SIZE,
Jan Glauberc1e26e12006-01-06 00:19:17 -0800133 .cra_ctxsize = sizeof(struct crypt_s390_sha1_ctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 .cra_module = THIS_MODULE,
135 .cra_list = LIST_HEAD_INIT(alg.cra_list),
136 .cra_u = { .digest = {
137 .dia_digestsize = SHA1_DIGEST_SIZE,
138 .dia_init = sha1_init,
139 .dia_update = sha1_update,
140 .dia_final = sha1_final } }
141};
142
143static int
144init(void)
145{
146 int ret = -ENOSYS;
147
Jan Glauberc1e26e12006-01-06 00:19:17 -0800148 if (crypt_s390_func_available(KIMD_SHA_1)){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 ret = crypto_register_alg(&alg);
150 if (ret == 0){
Jan Glauberc1e26e12006-01-06 00:19:17 -0800151 printk(KERN_INFO "crypt_s390: sha1_s390 loaded.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 }
153 }
154 return ret;
155}
156
157static void __exit
158fini(void)
159{
160 crypto_unregister_alg(&alg);
161}
162
163module_init(init);
164module_exit(fini);
165
166MODULE_ALIAS("sha1");
167
168MODULE_LICENSE("GPL");
169MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm");