Jan Glauber | 0a497c17 | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Cryptographic API. |
| 3 | * |
| 4 | * s390 implementation of the SHA256 Secure Hash Algorithm. |
| 5 | * |
| 6 | * s390 Version: |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 7 | * Copyright IBM Corp. 2005,2007 |
Jan Glauber | 0a497c17 | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 8 | * Author(s): Jan Glauber (jang@de.ibm.com) |
| 9 | * |
Sebastian Siewior | ad5d278 | 2007-10-08 11:45:10 +0800 | [diff] [blame] | 10 | * Derived from "crypto/sha256_generic.c" |
Jan Glauber | 0a497c17 | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 11 | * and "arch/s390/crypto/sha1_s390.c" |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify it |
| 14 | * under the terms of the GNU General Public License as published by the Free |
| 15 | * Software Foundation; either version 2 of the License, or (at your option) |
| 16 | * any later version. |
| 17 | * |
| 18 | */ |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/crypto.h> |
Jan Glauber | 5265eeb | 2007-10-09 22:43:13 +0800 | [diff] [blame] | 22 | #include <crypto/sha.h> |
Jan Glauber | 0a497c17 | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 23 | |
| 24 | #include "crypt_s390.h" |
| 25 | |
Jan Glauber | 0a497c17 | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 26 | struct s390_sha256_ctx { |
Jan Glauber | 131a395 | 2007-04-27 16:01:54 +0200 | [diff] [blame] | 27 | u64 count; /* message length */ |
Jan Glauber | 0a497c17 | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 28 | u32 state[8]; |
| 29 | u8 buf[2 * SHA256_BLOCK_SIZE]; |
| 30 | }; |
| 31 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 32 | static void sha256_init(struct crypto_tfm *tfm) |
Jan Glauber | 0a497c17 | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 33 | { |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 34 | struct s390_sha256_ctx *sctx = crypto_tfm_ctx(tfm); |
Jan Glauber | 0a497c17 | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 35 | |
Jan Glauber | 5265eeb | 2007-10-09 22:43:13 +0800 | [diff] [blame] | 36 | sctx->state[0] = SHA256_H0; |
| 37 | sctx->state[1] = SHA256_H1; |
| 38 | sctx->state[2] = SHA256_H2; |
| 39 | sctx->state[3] = SHA256_H3; |
| 40 | sctx->state[4] = SHA256_H4; |
| 41 | sctx->state[5] = SHA256_H5; |
| 42 | sctx->state[6] = SHA256_H6; |
| 43 | sctx->state[7] = SHA256_H7; |
Jan Glauber | 0a497c17 | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 44 | sctx->count = 0; |
Jan Glauber | 0a497c17 | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 47 | static void sha256_update(struct crypto_tfm *tfm, const u8 *data, |
| 48 | unsigned int len) |
Jan Glauber | 0a497c17 | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 49 | { |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 50 | struct s390_sha256_ctx *sctx = crypto_tfm_ctx(tfm); |
Jan Glauber | 0a497c17 | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 51 | unsigned int index; |
Jan Glauber | 7ffbc9d | 2006-01-14 13:20:56 -0800 | [diff] [blame] | 52 | int ret; |
Jan Glauber | 0a497c17 | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 53 | |
| 54 | /* how much is already in the buffer? */ |
Jan Glauber | 131a395 | 2007-04-27 16:01:54 +0200 | [diff] [blame] | 55 | index = sctx->count & 0x3f; |
Jan Glauber | 0a497c17 | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 56 | |
Jan Glauber | 131a395 | 2007-04-27 16:01:54 +0200 | [diff] [blame] | 57 | sctx->count += len; |
Jan Glauber | 0a497c17 | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 58 | |
Jan Glauber | 7ffbc9d | 2006-01-14 13:20:56 -0800 | [diff] [blame] | 59 | if ((index + len) < SHA256_BLOCK_SIZE) |
| 60 | goto store; |
| 61 | |
| 62 | /* process one stored block */ |
| 63 | if (index) { |
Jan Glauber | 0a497c17 | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 64 | memcpy(sctx->buf + index, data, SHA256_BLOCK_SIZE - index); |
Jan Glauber | 7ffbc9d | 2006-01-14 13:20:56 -0800 | [diff] [blame] | 65 | ret = crypt_s390_kimd(KIMD_SHA_256, sctx->state, sctx->buf, |
| 66 | SHA256_BLOCK_SIZE); |
| 67 | BUG_ON(ret != SHA256_BLOCK_SIZE); |
Jan Glauber | 0a497c17 | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 68 | data += SHA256_BLOCK_SIZE - index; |
| 69 | len -= SHA256_BLOCK_SIZE - index; |
| 70 | } |
| 71 | |
Jan Glauber | 7ffbc9d | 2006-01-14 13:20:56 -0800 | [diff] [blame] | 72 | /* process as many blocks as possible */ |
| 73 | if (len >= SHA256_BLOCK_SIZE) { |
| 74 | ret = crypt_s390_kimd(KIMD_SHA_256, sctx->state, data, |
| 75 | len & ~(SHA256_BLOCK_SIZE - 1)); |
| 76 | BUG_ON(ret != (len & ~(SHA256_BLOCK_SIZE - 1))); |
| 77 | data += ret; |
| 78 | len -= ret; |
| 79 | } |
| 80 | |
| 81 | store: |
Jan Glauber | 0a497c17 | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 82 | /* anything left? */ |
| 83 | if (len) |
| 84 | memcpy(sctx->buf + index , data, len); |
| 85 | } |
| 86 | |
Jan Glauber | 131a395 | 2007-04-27 16:01:54 +0200 | [diff] [blame] | 87 | /* Add padding and return the message digest */ |
| 88 | static void sha256_final(struct crypto_tfm *tfm, u8 *out) |
Jan Glauber | 0a497c17 | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 89 | { |
Jan Glauber | 131a395 | 2007-04-27 16:01:54 +0200 | [diff] [blame] | 90 | struct s390_sha256_ctx *sctx = crypto_tfm_ctx(tfm); |
| 91 | u64 bits; |
| 92 | unsigned int index, end; |
| 93 | int ret; |
Jan Glauber | 0a497c17 | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 94 | |
Jan Glauber | 131a395 | 2007-04-27 16:01:54 +0200 | [diff] [blame] | 95 | /* must perform manual padding */ |
| 96 | index = sctx->count & 0x3f; |
| 97 | end = (index < 56) ? SHA256_BLOCK_SIZE : (2 * SHA256_BLOCK_SIZE); |
Jan Glauber | 0a497c17 | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 98 | |
| 99 | /* start pad with 1 */ |
| 100 | sctx->buf[index] = 0x80; |
| 101 | |
| 102 | /* pad with zeros */ |
| 103 | index++; |
| 104 | memset(sctx->buf + index, 0x00, end - index - 8); |
| 105 | |
| 106 | /* append message length */ |
Jan Glauber | 131a395 | 2007-04-27 16:01:54 +0200 | [diff] [blame] | 107 | bits = sctx->count * 8; |
| 108 | memcpy(sctx->buf + end - 8, &bits, sizeof(bits)); |
Jan Glauber | 0a497c17 | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 109 | |
Jan Glauber | 131a395 | 2007-04-27 16:01:54 +0200 | [diff] [blame] | 110 | ret = crypt_s390_kimd(KIMD_SHA_256, sctx->state, sctx->buf, end); |
| 111 | BUG_ON(ret != end); |
Jan Glauber | 0a497c17 | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 112 | |
| 113 | /* copy digest to out */ |
| 114 | memcpy(out, sctx->state, SHA256_DIGEST_SIZE); |
| 115 | |
| 116 | /* wipe context */ |
| 117 | memset(sctx, 0, sizeof *sctx); |
| 118 | } |
| 119 | |
| 120 | static struct crypto_alg alg = { |
| 121 | .cra_name = "sha256", |
Herbert Xu | 65b75c3 | 2006-08-21 21:18:50 +1000 | [diff] [blame] | 122 | .cra_driver_name = "sha256-s390", |
| 123 | .cra_priority = CRYPT_S390_PRIORITY, |
Jan Glauber | 0a497c17 | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 124 | .cra_flags = CRYPTO_ALG_TYPE_DIGEST, |
| 125 | .cra_blocksize = SHA256_BLOCK_SIZE, |
| 126 | .cra_ctxsize = sizeof(struct s390_sha256_ctx), |
| 127 | .cra_module = THIS_MODULE, |
| 128 | .cra_list = LIST_HEAD_INIT(alg.cra_list), |
| 129 | .cra_u = { .digest = { |
| 130 | .dia_digestsize = SHA256_DIGEST_SIZE, |
Jan Glauber | 7ffbc9d | 2006-01-14 13:20:56 -0800 | [diff] [blame] | 131 | .dia_init = sha256_init, |
| 132 | .dia_update = sha256_update, |
| 133 | .dia_final = sha256_final } } |
Jan Glauber | 0a497c17 | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | static int init(void) |
| 137 | { |
Jan Glauber | 0a497c17 | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 138 | if (!crypt_s390_func_available(KIMD_SHA_256)) |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 139 | return -EOPNOTSUPP; |
Jan Glauber | 0a497c17 | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 140 | |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 141 | return crypto_register_alg(&alg); |
Jan Glauber | 0a497c17 | 2006-01-06 00:19:18 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | static void __exit fini(void) |
| 145 | { |
| 146 | crypto_unregister_alg(&alg); |
| 147 | } |
| 148 | |
| 149 | module_init(init); |
| 150 | module_exit(fini); |
| 151 | |
| 152 | MODULE_ALIAS("sha256"); |
| 153 | |
| 154 | MODULE_LICENSE("GPL"); |
| 155 | MODULE_DESCRIPTION("SHA256 Secure Hash Algorithm"); |