blob: 7c15542d3685479713a06643e883503789d7b7a5 [file] [log] [blame]
Martin Schwidefsky9fa1db42017-12-04 10:39:38 +01001// SPDX-License-Identifier: GPL-2.0+
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Cryptographic API.
4 *
Jan Glauberc1e26e12006-01-06 00:19:17 -08005 * s390 implementation of the SHA1 Secure Hash Algorithm.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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 Carstensa53c8fa2012-07-20 11:15:04 +020012 * Copyright IBM Corp. 2003, 2007
Jan Glauber86aa9fc2007-02-05 21:18:14 +010013 * Author(s): Thomas Spatzier
14 * Jan Glauber (jan.glauber@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 *
Sebastian Siewiorad5d2782007-10-08 11:45:10 +080016 * Derived from "crypto/sha1_generic.c"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * Copyright (c) Alan Smithee.
18 * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
19 * Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 */
Herbert Xu563f3462009-01-18 20:33:33 +110021#include <crypto/internal/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
23#include <linux/module.h>
Hendrik Bruecknerd05377c2015-02-19 17:34:07 +010024#include <linux/cpufeature.h>
Jan Glauber5265eeb2007-10-09 22:43:13 +080025#include <crypto/sha.h>
Martin Schwidefskyc7d4d252016-03-17 15:22:12 +010026#include <asm/cpacf.h>
Jan Glauber131a3952007-04-27 16:01:54 +020027
Jan Glauber604973f2008-03-06 19:50:20 +080028#include "sha.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Herbert Xu563f3462009-01-18 20:33:33 +110030static int sha1_init(struct shash_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
Herbert Xu563f3462009-01-18 20:33:33 +110032 struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
Jan Glauber86aa9fc2007-02-05 21:18:14 +010033
Jan Glauber5265eeb2007-10-09 22:43:13 +080034 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 Glauber131a3952007-04-27 16:01:54 +020039 sctx->count = 0;
Martin Schwidefskyc7d4d252016-03-17 15:22:12 +010040 sctx->func = CPACF_KIMD_SHA_1;
Herbert Xu563f3462009-01-18 20:33:33 +110041
42 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043}
44
Herbert Xu406f1042009-07-10 13:18:26 +080045static 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 Glauber81bd5f62009-09-05 16:27:35 +100056static int sha1_import(struct shash_desc *desc, const void *in)
Herbert Xu406f1042009-07-10 13:18:26 +080057{
Sachin Sant2a549c32009-07-16 19:58:42 +080058 struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
Jan Glauber81bd5f62009-09-05 16:27:35 +100059 const struct sha1_state *ictx = in;
Herbert Xu406f1042009-07-10 13:18:26 +080060
61 sctx->count = ictx->count;
62 memcpy(sctx->state, ictx->state, sizeof(ictx->state));
63 memcpy(sctx->buf, ictx->buffer, sizeof(ictx->buffer));
Martin Schwidefskyc7d4d252016-03-17 15:22:12 +010064 sctx->func = CPACF_KIMD_SHA_1;
Herbert Xu406f1042009-07-10 13:18:26 +080065 return 0;
66}
67
Herbert Xu563f3462009-01-18 20:33:33 +110068static struct shash_alg alg = {
69 .digestsize = SHA1_DIGEST_SIZE,
70 .init = sha1_init,
71 .update = s390_sha_update,
72 .final = s390_sha_final,
Herbert Xu406f1042009-07-10 13:18:26 +080073 .export = sha1_export,
74 .import = sha1_import,
Herbert Xu563f3462009-01-18 20:33:33 +110075 .descsize = sizeof(struct s390_sha_ctx),
Herbert Xu406f1042009-07-10 13:18:26 +080076 .statesize = sizeof(struct sha1_state),
Herbert Xu563f3462009-01-18 20:33:33 +110077 .base = {
78 .cra_name = "sha1",
79 .cra_driver_name= "sha1-s390",
Martin Schwidefskyc7d4d252016-03-17 15:22:12 +010080 .cra_priority = 300,
Herbert Xu563f3462009-01-18 20:33:33 +110081 .cra_blocksize = SHA1_BLOCK_SIZE,
82 .cra_module = THIS_MODULE,
83 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070084};
85
Heiko Carstens9f7819c2008-04-17 07:46:17 +020086static int __init sha1_s390_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Martin Schwidefsky69c0e362016-08-18 12:59:46 +020088 if (!cpacf_query_func(CPACF_KIMD, CPACF_KIMD_SHA_1))
David Hildenbrand45488c42019-06-12 15:33:06 +020089 return -ENODEV;
Herbert Xu563f3462009-01-18 20:33:33 +110090 return crypto_register_shash(&alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
Heiko Carstens9f7819c2008-04-17 07:46:17 +020093static void __exit sha1_s390_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Herbert Xu563f3462009-01-18 20:33:33 +110095 crypto_unregister_shash(&alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
Hendrik Bruecknerd05377c2015-02-19 17:34:07 +010098module_cpu_feature_match(MSA, sha1_s390_init);
Heiko Carstens9f7819c2008-04-17 07:46:17 +020099module_exit(sha1_s390_fini);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Kees Cook5d26a102014-11-20 17:05:53 -0800101MODULE_ALIAS_CRYPTO("sha1");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102MODULE_LICENSE("GPL");
103MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm");