Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Cryptographic API. |
| 3 | * |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 4 | * s390 implementation of the SHA1 Secure Hash Algorithm. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
| 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 Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 11 | * Copyright IBM Corp. 2003,2007 |
| 12 | * Author(s): Thomas Spatzier |
| 13 | * Jan Glauber (jan.glauber@de.ibm.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * |
Sebastian Siewior | ad5d278 | 2007-10-08 11:45:10 +0800 | [diff] [blame] | 15 | * Derived from "crypto/sha1_generic.c" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/crypto.h> |
Jan Glauber | 5265eeb | 2007-10-09 22:43:13 +0800 | [diff] [blame] | 29 | #include <crypto/sha.h> |
Jan Glauber | 131a395 | 2007-04-27 16:01:54 +0200 | [diff] [blame] | 30 | |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 31 | #include "crypt_s390.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
Jan Glauber | 131a395 | 2007-04-27 16:01:54 +0200 | [diff] [blame] | 33 | struct s390_sha1_ctx { |
| 34 | u64 count; /* message length */ |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 35 | u32 state[5]; |
Jan Glauber | 131a395 | 2007-04-27 16:01:54 +0200 | [diff] [blame] | 36 | u8 buf[2 * SHA1_BLOCK_SIZE]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 39 | static void sha1_init(struct crypto_tfm *tfm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | { |
Jan Glauber | 131a395 | 2007-04-27 16:01:54 +0200 | [diff] [blame] | 41 | struct s390_sha1_ctx *sctx = crypto_tfm_ctx(tfm); |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 42 | |
Jan Glauber | 5265eeb | 2007-10-09 22:43:13 +0800 | [diff] [blame] | 43 | sctx->state[0] = SHA1_H0; |
| 44 | sctx->state[1] = SHA1_H1; |
| 45 | sctx->state[2] = SHA1_H2; |
| 46 | sctx->state[3] = SHA1_H3; |
| 47 | sctx->state[4] = SHA1_H4; |
Jan Glauber | 131a395 | 2007-04-27 16:01:54 +0200 | [diff] [blame] | 48 | sctx->count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 51 | static void sha1_update(struct crypto_tfm *tfm, const u8 *data, |
| 52 | unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | { |
Jan Glauber | 131a395 | 2007-04-27 16:01:54 +0200 | [diff] [blame] | 54 | struct s390_sha1_ctx *sctx = crypto_tfm_ctx(tfm); |
| 55 | unsigned int index; |
| 56 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Jan Glauber | 131a395 | 2007-04-27 16:01:54 +0200 | [diff] [blame] | 58 | /* how much is already in the buffer? */ |
| 59 | index = sctx->count & 0x3f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
Jan Glauber | 131a395 | 2007-04-27 16:01:54 +0200 | [diff] [blame] | 61 | sctx->count += len; |
| 62 | |
| 63 | if (index + len < SHA1_BLOCK_SIZE) |
| 64 | goto store; |
| 65 | |
| 66 | /* process one stored block */ |
| 67 | if (index) { |
| 68 | memcpy(sctx->buf + index, data, SHA1_BLOCK_SIZE - index); |
| 69 | ret = crypt_s390_kimd(KIMD_SHA_1, sctx->state, sctx->buf, |
| 70 | SHA1_BLOCK_SIZE); |
| 71 | BUG_ON(ret != SHA1_BLOCK_SIZE); |
| 72 | data += SHA1_BLOCK_SIZE - index; |
| 73 | len -= SHA1_BLOCK_SIZE - index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Jan Glauber | 131a395 | 2007-04-27 16:01:54 +0200 | [diff] [blame] | 76 | /* process as many blocks as possible */ |
| 77 | if (len >= SHA1_BLOCK_SIZE) { |
| 78 | ret = crypt_s390_kimd(KIMD_SHA_1, sctx->state, data, |
| 79 | len & ~(SHA1_BLOCK_SIZE - 1)); |
| 80 | BUG_ON(ret != (len & ~(SHA1_BLOCK_SIZE - 1))); |
| 81 | data += ret; |
| 82 | len -= ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
Jan Glauber | 131a395 | 2007-04-27 16:01:54 +0200 | [diff] [blame] | 85 | store: |
| 86 | /* anything left? */ |
| 87 | if (len) |
| 88 | memcpy(sctx->buf + index , data, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /* Add padding and return the message digest. */ |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 92 | static void sha1_final(struct crypto_tfm *tfm, u8 *out) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | { |
Jan Glauber | 131a395 | 2007-04-27 16:01:54 +0200 | [diff] [blame] | 94 | struct s390_sha1_ctx *sctx = crypto_tfm_ctx(tfm); |
| 95 | u64 bits; |
| 96 | unsigned int index, end; |
| 97 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 99 | /* must perform manual padding */ |
Jan Glauber | 131a395 | 2007-04-27 16:01:54 +0200 | [diff] [blame] | 100 | index = sctx->count & 0x3f; |
| 101 | end = (index < 56) ? SHA1_BLOCK_SIZE : (2 * SHA1_BLOCK_SIZE); |
| 102 | |
| 103 | /* start pad with 1 */ |
| 104 | sctx->buf[index] = 0x80; |
| 105 | |
| 106 | /* pad with zeros */ |
| 107 | index++; |
| 108 | memset(sctx->buf + index, 0x00, end - index - 8); |
| 109 | |
| 110 | /* append message length */ |
| 111 | bits = sctx->count * 8; |
| 112 | memcpy(sctx->buf + end - 8, &bits, sizeof(bits)); |
| 113 | |
| 114 | ret = crypt_s390_kimd(KIMD_SHA_1, sctx->state, sctx->buf, end); |
| 115 | BUG_ON(ret != end); |
| 116 | |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 117 | /* copy digest to out */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | memcpy(out, sctx->state, SHA1_DIGEST_SIZE); |
Jan Glauber | 131a395 | 2007-04-27 16:01:54 +0200 | [diff] [blame] | 119 | |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 120 | /* wipe context */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | memset(sctx, 0, sizeof *sctx); |
| 122 | } |
| 123 | |
| 124 | static struct crypto_alg alg = { |
| 125 | .cra_name = "sha1", |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 126 | .cra_driver_name= "sha1-s390", |
Herbert Xu | 65b75c3 | 2006-08-21 21:18:50 +1000 | [diff] [blame] | 127 | .cra_priority = CRYPT_S390_PRIORITY, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | .cra_flags = CRYPTO_ALG_TYPE_DIGEST, |
| 129 | .cra_blocksize = SHA1_BLOCK_SIZE, |
Jan Glauber | 131a395 | 2007-04-27 16:01:54 +0200 | [diff] [blame] | 130 | .cra_ctxsize = sizeof(struct s390_sha1_ctx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | .cra_module = THIS_MODULE, |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 132 | .cra_list = LIST_HEAD_INIT(alg.cra_list), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | .cra_u = { .digest = { |
| 134 | .dia_digestsize = SHA1_DIGEST_SIZE, |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 135 | .dia_init = sha1_init, |
| 136 | .dia_update = sha1_update, |
| 137 | .dia_final = sha1_final } } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | }; |
| 139 | |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 140 | static int __init init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | { |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 142 | if (!crypt_s390_func_available(KIMD_SHA_1)) |
| 143 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 145 | return crypto_register_alg(&alg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 148 | static void __exit fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | { |
| 150 | crypto_unregister_alg(&alg); |
| 151 | } |
| 152 | |
| 153 | module_init(init); |
| 154 | module_exit(fini); |
| 155 | |
| 156 | MODULE_ALIAS("sha1"); |
| 157 | |
| 158 | MODULE_LICENSE("GPL"); |
| 159 | MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm"); |