Martin Schwidefsky | 9fa1db4 | 2017-12-04 10:39:38 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Cryptographic API. |
| 4 | * |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 5 | * s390 implementation of the SHA1 Secure Hash Algorithm. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * Derived from cryptoapi implementation, adapted for in-place |
| 8 | * scatterlist interface. Originally based on the public domain |
| 9 | * implementation written by Steve Reid. |
| 10 | * |
| 11 | * s390 Version: |
Heiko Carstens | a53c8fa | 2012-07-20 11:15:04 +0200 | [diff] [blame] | 12 | * Copyright IBM Corp. 2003, 2007 |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 13 | * Author(s): Thomas Spatzier |
| 14 | * Jan Glauber (jan.glauber@de.ibm.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | * |
Sebastian Siewior | ad5d278 | 2007-10-08 11:45:10 +0800 | [diff] [blame] | 16 | * Derived from "crypto/sha1_generic.c" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | * Copyright (c) Alan Smithee. |
| 18 | * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk> |
| 19 | * Copyright (c) Jean-Francois Dive <jef@linuxbe.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | */ |
Herbert Xu | 563f346 | 2009-01-18 20:33:33 +1100 | [diff] [blame] | 21 | #include <crypto/internal/hash.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/init.h> |
| 23 | #include <linux/module.h> |
Hendrik Brueckner | d05377c | 2015-02-19 17:34:07 +0100 | [diff] [blame] | 24 | #include <linux/cpufeature.h> |
Jan Glauber | 5265eeb | 2007-10-09 22:43:13 +0800 | [diff] [blame] | 25 | #include <crypto/sha.h> |
Martin Schwidefsky | c7d4d25 | 2016-03-17 15:22:12 +0100 | [diff] [blame] | 26 | #include <asm/cpacf.h> |
Jan Glauber | 131a395 | 2007-04-27 16:01:54 +0200 | [diff] [blame] | 27 | |
Jan Glauber | 604973f | 2008-03-06 19:50:20 +0800 | [diff] [blame] | 28 | #include "sha.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
Herbert Xu | 563f346 | 2009-01-18 20:33:33 +1100 | [diff] [blame] | 30 | static int sha1_init(struct shash_desc *desc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | { |
Herbert Xu | 563f346 | 2009-01-18 20:33:33 +1100 | [diff] [blame] | 32 | struct s390_sha_ctx *sctx = shash_desc_ctx(desc); |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 33 | |
Jan Glauber | 5265eeb | 2007-10-09 22:43:13 +0800 | [diff] [blame] | 34 | sctx->state[0] = SHA1_H0; |
| 35 | sctx->state[1] = SHA1_H1; |
| 36 | sctx->state[2] = SHA1_H2; |
| 37 | sctx->state[3] = SHA1_H3; |
| 38 | sctx->state[4] = SHA1_H4; |
Jan Glauber | 131a395 | 2007-04-27 16:01:54 +0200 | [diff] [blame] | 39 | sctx->count = 0; |
Martin Schwidefsky | c7d4d25 | 2016-03-17 15:22:12 +0100 | [diff] [blame] | 40 | sctx->func = CPACF_KIMD_SHA_1; |
Herbert Xu | 563f346 | 2009-01-18 20:33:33 +1100 | [diff] [blame] | 41 | |
| 42 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Herbert Xu | 406f104 | 2009-07-10 13:18:26 +0800 | [diff] [blame] | 45 | static int sha1_export(struct shash_desc *desc, void *out) |
| 46 | { |
| 47 | struct s390_sha_ctx *sctx = shash_desc_ctx(desc); |
| 48 | struct sha1_state *octx = out; |
| 49 | |
| 50 | octx->count = sctx->count; |
| 51 | memcpy(octx->state, sctx->state, sizeof(octx->state)); |
| 52 | memcpy(octx->buffer, sctx->buf, sizeof(octx->buffer)); |
| 53 | return 0; |
| 54 | } |
| 55 | |
Jan Glauber | 81bd5f6 | 2009-09-05 16:27:35 +1000 | [diff] [blame] | 56 | static int sha1_import(struct shash_desc *desc, const void *in) |
Herbert Xu | 406f104 | 2009-07-10 13:18:26 +0800 | [diff] [blame] | 57 | { |
Sachin Sant | 2a549c3 | 2009-07-16 19:58:42 +0800 | [diff] [blame] | 58 | struct s390_sha_ctx *sctx = shash_desc_ctx(desc); |
Jan Glauber | 81bd5f6 | 2009-09-05 16:27:35 +1000 | [diff] [blame] | 59 | const struct sha1_state *ictx = in; |
Herbert Xu | 406f104 | 2009-07-10 13:18:26 +0800 | [diff] [blame] | 60 | |
| 61 | sctx->count = ictx->count; |
| 62 | memcpy(sctx->state, ictx->state, sizeof(ictx->state)); |
| 63 | memcpy(sctx->buf, ictx->buffer, sizeof(ictx->buffer)); |
Martin Schwidefsky | c7d4d25 | 2016-03-17 15:22:12 +0100 | [diff] [blame] | 64 | sctx->func = CPACF_KIMD_SHA_1; |
Herbert Xu | 406f104 | 2009-07-10 13:18:26 +0800 | [diff] [blame] | 65 | return 0; |
| 66 | } |
| 67 | |
Herbert Xu | 563f346 | 2009-01-18 20:33:33 +1100 | [diff] [blame] | 68 | static struct shash_alg alg = { |
| 69 | .digestsize = SHA1_DIGEST_SIZE, |
| 70 | .init = sha1_init, |
| 71 | .update = s390_sha_update, |
| 72 | .final = s390_sha_final, |
Herbert Xu | 406f104 | 2009-07-10 13:18:26 +0800 | [diff] [blame] | 73 | .export = sha1_export, |
| 74 | .import = sha1_import, |
Herbert Xu | 563f346 | 2009-01-18 20:33:33 +1100 | [diff] [blame] | 75 | .descsize = sizeof(struct s390_sha_ctx), |
Herbert Xu | 406f104 | 2009-07-10 13:18:26 +0800 | [diff] [blame] | 76 | .statesize = sizeof(struct sha1_state), |
Herbert Xu | 563f346 | 2009-01-18 20:33:33 +1100 | [diff] [blame] | 77 | .base = { |
| 78 | .cra_name = "sha1", |
| 79 | .cra_driver_name= "sha1-s390", |
Martin Schwidefsky | c7d4d25 | 2016-03-17 15:22:12 +0100 | [diff] [blame] | 80 | .cra_priority = 300, |
Herbert Xu | 563f346 | 2009-01-18 20:33:33 +1100 | [diff] [blame] | 81 | .cra_blocksize = SHA1_BLOCK_SIZE, |
| 82 | .cra_module = THIS_MODULE, |
| 83 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | }; |
| 85 | |
Heiko Carstens | 9f7819c | 2008-04-17 07:46:17 +0200 | [diff] [blame] | 86 | static int __init sha1_s390_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | { |
Martin Schwidefsky | 69c0e36 | 2016-08-18 12:59:46 +0200 | [diff] [blame] | 88 | if (!cpacf_query_func(CPACF_KIMD, CPACF_KIMD_SHA_1)) |
David Hildenbrand | 45488c4 | 2019-06-12 15:33:06 +0200 | [diff] [blame] | 89 | return -ENODEV; |
Herbert Xu | 563f346 | 2009-01-18 20:33:33 +1100 | [diff] [blame] | 90 | return crypto_register_shash(&alg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Heiko Carstens | 9f7819c | 2008-04-17 07:46:17 +0200 | [diff] [blame] | 93 | static void __exit sha1_s390_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | { |
Herbert Xu | 563f346 | 2009-01-18 20:33:33 +1100 | [diff] [blame] | 95 | crypto_unregister_shash(&alg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Hendrik Brueckner | d05377c | 2015-02-19 17:34:07 +0100 | [diff] [blame] | 98 | module_cpu_feature_match(MSA, sha1_s390_init); |
Heiko Carstens | 9f7819c | 2008-04-17 07:46:17 +0200 | [diff] [blame] | 99 | module_exit(sha1_s390_fini); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
Kees Cook | 5d26a10 | 2014-11-20 17:05:53 -0800 | [diff] [blame] | 101 | MODULE_ALIAS_CRYPTO("sha1"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | MODULE_LICENSE("GPL"); |
| 103 | MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm"); |