4 * s390 implementation of the SHA1 Secure Hash Algorithm.
6 * Derived from cryptoapi implementation, adapted for in-place
7 * scatterlist interface. Originally based on the public domain
8 * implementation written by Steve Reid.
11 * Copyright (C) 2003 IBM Deutschland GmbH, IBM Corporation
12 * Author(s): Thomas Spatzier (tspat@de.ibm.com)
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>
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)
25 #include <linux/init.h>
26 #include <linux/module.h>
28 #include <linux/crypto.h>
29 #include <asm/scatterlist.h>
30 #include <asm/byteorder.h>
31 #include "crypt_s390.h"
33 #define SHA1_DIGEST_SIZE 20
34 #define SHA1_BLOCK_SIZE 64
36 struct crypt_s390_sha1_ctx
{
40 u8 buffer
[2 * SHA1_BLOCK_SIZE
];
43 static void sha1_init(struct crypto_tfm
*tfm
)
45 struct crypt_s390_sha1_ctx
*ctx
= crypto_tfm_ctx(tfm
);
46 static const u32 initstate
[5] = {
55 memcpy(ctx
->state
, &initstate
, sizeof(initstate
));
59 static void sha1_update(struct crypto_tfm
*tfm
, const u8
*data
,
62 struct crypt_s390_sha1_ctx
*sctx
;
65 sctx
= crypto_tfm_ctx(tfm
);
66 sctx
->count
+= len
* 8; //message bit length
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
);
73 crypt_s390_kimd(KIMD_SHA_1
, sctx
->state
, sctx
->buffer
,
75 data
+= SHA1_BLOCK_SIZE
- sctx
->buf_len
;
76 len
-= SHA1_BLOCK_SIZE
- sctx
->buf_len
;
80 //rest of data contains full blocks?
81 imd_len
= len
& ~0x3ful
;
83 crypt_s390_kimd(KIMD_SHA_1
, sctx
->state
, data
, imd_len
);
87 //anything left? store in buffer
89 memcpy(sctx
->buffer
+ sctx
->buf_len
, data
, len
);
96 pad_message(struct crypt_s390_sha1_ctx
* sctx
)
100 index
= sctx
->buf_len
;
101 sctx
->buf_len
= (sctx
->buf_len
< 56)?
102 SHA1_BLOCK_SIZE
:2 * SHA1_BLOCK_SIZE
;
104 sctx
->buffer
[index
] = 0x80;
107 memset(sctx
->buffer
+ index
, 0x00, sctx
->buf_len
- index
);
109 memcpy(sctx
->buffer
+ sctx
->buf_len
- 8, &sctx
->count
,
113 /* Add padding and return the message digest. */
114 static void sha1_final(struct crypto_tfm
*tfm
, u8
*out
)
116 struct crypt_s390_sha1_ctx
*sctx
= crypto_tfm_ctx(tfm
);
118 //must perform manual padding
120 crypt_s390_kimd(KIMD_SHA_1
, sctx
->state
, sctx
->buffer
, sctx
->buf_len
);
122 memcpy(out
, sctx
->state
, SHA1_DIGEST_SIZE
);
124 memset(sctx
, 0, sizeof *sctx
);
127 static struct crypto_alg alg
= {
129 .cra_driver_name
= "sha1-s390",
130 .cra_priority
= CRYPT_S390_PRIORITY
,
131 .cra_flags
= CRYPTO_ALG_TYPE_DIGEST
,
132 .cra_blocksize
= SHA1_BLOCK_SIZE
,
133 .cra_ctxsize
= sizeof(struct crypt_s390_sha1_ctx
),
134 .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
} }
148 if (crypt_s390_func_available(KIMD_SHA_1
)){
149 ret
= crypto_register_alg(&alg
);
151 printk(KERN_INFO
"crypt_s390: sha1_s390 loaded.\n");
160 crypto_unregister_alg(&alg
);
166 MODULE_ALIAS("sha1");
168 MODULE_LICENSE("GPL");
169 MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm");