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 | * |
| 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> |
| 28 | #include <linux/mm.h> |
| 29 | #include <linux/crypto.h> |
| 30 | #include <asm/scatterlist.h> |
| 31 | #include <asm/byteorder.h> |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 32 | #include "crypt_s390.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | #define SHA1_DIGEST_SIZE 20 |
| 35 | #define SHA1_BLOCK_SIZE 64 |
| 36 | |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 37 | struct crypt_s390_sha1_ctx { |
| 38 | u64 count; |
| 39 | u32 state[5]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | u32 buf_len; |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 41 | u8 buffer[2 * SHA1_BLOCK_SIZE]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 44 | static void sha1_init(struct crypto_tfm *tfm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | { |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 46 | struct crypt_s390_sha1_ctx *ctx = crypto_tfm_ctx(tfm); |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame^] | 47 | |
| 48 | ctx->state[0] = 0x67452301; |
| 49 | ctx->state[1] = 0xEFCDAB89; |
| 50 | ctx->state[2] = 0x98BADCFE; |
| 51 | ctx->state[3] = 0x10325476; |
| 52 | ctx->state[4] = 0xC3D2E1F0; |
Herbert Xu | 4360010 | 2006-05-16 22:06:54 +1000 | [diff] [blame] | 53 | |
| 54 | ctx->count = 0; |
Herbert Xu | 4360010 | 2006-05-16 22:06:54 +1000 | [diff] [blame] | 55 | ctx->buf_len = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 58 | static void sha1_update(struct crypto_tfm *tfm, const u8 *data, |
| 59 | unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | { |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 61 | struct crypt_s390_sha1_ctx *sctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | long imd_len; |
| 63 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 64 | sctx = crypto_tfm_ctx(tfm); |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame^] | 65 | sctx->count += len * 8; /* message bit length */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame^] | 67 | /* anything in buffer yet? -> must be completed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | if (sctx->buf_len && (sctx->buf_len + len) >= SHA1_BLOCK_SIZE) { |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame^] | 69 | /* complete full block and hash */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | memcpy(sctx->buffer + sctx->buf_len, data, |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame^] | 71 | SHA1_BLOCK_SIZE - sctx->buf_len); |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 72 | crypt_s390_kimd(KIMD_SHA_1, sctx->state, sctx->buffer, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | SHA1_BLOCK_SIZE); |
| 74 | data += SHA1_BLOCK_SIZE - sctx->buf_len; |
| 75 | len -= SHA1_BLOCK_SIZE - sctx->buf_len; |
| 76 | sctx->buf_len = 0; |
| 77 | } |
| 78 | |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame^] | 79 | /* rest of data contains full blocks? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | imd_len = len & ~0x3ful; |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame^] | 81 | if (imd_len) { |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 82 | crypt_s390_kimd(KIMD_SHA_1, sctx->state, data, imd_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | data += imd_len; |
| 84 | len -= imd_len; |
| 85 | } |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame^] | 86 | /* anything left? store in buffer */ |
| 87 | if (len) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | memcpy(sctx->buffer + sctx->buf_len , data, len); |
| 89 | sctx->buf_len += len; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame^] | 94 | static void pad_message(struct crypt_s390_sha1_ctx* sctx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | { |
| 96 | int index; |
| 97 | |
| 98 | index = sctx->buf_len; |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame^] | 99 | sctx->buf_len = (sctx->buf_len < 56) ? |
| 100 | SHA1_BLOCK_SIZE:2 * SHA1_BLOCK_SIZE; |
| 101 | /* start pad with 1 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | sctx->buffer[index] = 0x80; |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame^] | 103 | /* pad with zeros */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | index++; |
| 105 | memset(sctx->buffer + index, 0x00, sctx->buf_len - index); |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame^] | 106 | /* append length */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | memcpy(sctx->buffer + sctx->buf_len - 8, &sctx->count, |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame^] | 108 | sizeof sctx->count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /* Add padding and return the message digest. */ |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 112 | static void sha1_final(struct crypto_tfm *tfm, u8 *out) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | { |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 114 | struct crypt_s390_sha1_ctx *sctx = crypto_tfm_ctx(tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame^] | 116 | /* must perform manual padding */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | pad_message(sctx); |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 118 | crypt_s390_kimd(KIMD_SHA_1, sctx->state, sctx->buffer, sctx->buf_len); |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame^] | 119 | /* copy digest to out */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | memcpy(out, sctx->state, SHA1_DIGEST_SIZE); |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame^] | 121 | /* wipe context */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | memset(sctx, 0, sizeof *sctx); |
| 123 | } |
| 124 | |
| 125 | static struct crypto_alg alg = { |
| 126 | .cra_name = "sha1", |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame^] | 127 | .cra_driver_name= "sha1-s390", |
Herbert Xu | 65b75c3 | 2006-08-21 21:18:50 +1000 | [diff] [blame] | 128 | .cra_priority = CRYPT_S390_PRIORITY, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | .cra_flags = CRYPTO_ALG_TYPE_DIGEST, |
| 130 | .cra_blocksize = SHA1_BLOCK_SIZE, |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 131 | .cra_ctxsize = sizeof(struct crypt_s390_sha1_ctx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | .cra_module = THIS_MODULE, |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame^] | 133 | .cra_list = LIST_HEAD_INIT(alg.cra_list), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | .cra_u = { .digest = { |
| 135 | .dia_digestsize = SHA1_DIGEST_SIZE, |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame^] | 136 | .dia_init = sha1_init, |
| 137 | .dia_update = sha1_update, |
| 138 | .dia_final = sha1_final } } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | }; |
| 140 | |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame^] | 141 | static int __init init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | { |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame^] | 143 | if (!crypt_s390_func_available(KIMD_SHA_1)) |
| 144 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame^] | 146 | return crypto_register_alg(&alg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame^] | 149 | static void __exit fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | { |
| 151 | crypto_unregister_alg(&alg); |
| 152 | } |
| 153 | |
| 154 | module_init(init); |
| 155 | module_exit(fini); |
| 156 | |
| 157 | MODULE_ALIAS("sha1"); |
| 158 | |
| 159 | MODULE_LICENSE("GPL"); |
| 160 | MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm"); |