blob: 9208eadae9f09fe33792e4c735e20d14d0aa2ac8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Cryptographic API.
3 *
Jan Glauberc1e26e12006-01-06 00:19:17 -08004 * s390 implementation of the SHA1 Secure Hash Algorithm.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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:
Heiko Carstensa53c8fa2012-07-20 11:15:04 +020011 * Copyright IBM Corp. 2003, 2007
Jan Glauber86aa9fc2007-02-05 21:18:14 +010012 * Author(s): Thomas Spatzier
13 * Jan Glauber (jan.glauber@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
Sebastian Siewiorad5d2782007-10-08 11:45:10 +080015 * Derived from "crypto/sha1_generic.c"
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * 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 */
Herbert Xu563f3462009-01-18 20:33:33 +110026#include <crypto/internal/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/init.h>
28#include <linux/module.h>
Hendrik Bruecknerd05377c2015-02-19 17:34:07 +010029#include <linux/cpufeature.h>
Jan Glauber5265eeb2007-10-09 22:43:13 +080030#include <crypto/sha.h>
Jan Glauber131a3952007-04-27 16:01:54 +020031
Jan Glauberc1e26e12006-01-06 00:19:17 -080032#include "crypt_s390.h"
Jan Glauber604973f2008-03-06 19:50:20 +080033#include "sha.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Herbert Xu563f3462009-01-18 20:33:33 +110035static int sha1_init(struct shash_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
Herbert Xu563f3462009-01-18 20:33:33 +110037 struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
Jan Glauber86aa9fc2007-02-05 21:18:14 +010038
Jan Glauber5265eeb2007-10-09 22:43:13 +080039 sctx->state[0] = SHA1_H0;
40 sctx->state[1] = SHA1_H1;
41 sctx->state[2] = SHA1_H2;
42 sctx->state[3] = SHA1_H3;
43 sctx->state[4] = SHA1_H4;
Jan Glauber131a3952007-04-27 16:01:54 +020044 sctx->count = 0;
Jan Glauber604973f2008-03-06 19:50:20 +080045 sctx->func = KIMD_SHA_1;
Herbert Xu563f3462009-01-18 20:33:33 +110046
47 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048}
49
Herbert Xu406f1042009-07-10 13:18:26 +080050static int sha1_export(struct shash_desc *desc, void *out)
51{
52 struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
53 struct sha1_state *octx = out;
54
55 octx->count = sctx->count;
56 memcpy(octx->state, sctx->state, sizeof(octx->state));
57 memcpy(octx->buffer, sctx->buf, sizeof(octx->buffer));
58 return 0;
59}
60
Jan Glauber81bd5f62009-09-05 16:27:35 +100061static int sha1_import(struct shash_desc *desc, const void *in)
Herbert Xu406f1042009-07-10 13:18:26 +080062{
Sachin Sant2a549c32009-07-16 19:58:42 +080063 struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
Jan Glauber81bd5f62009-09-05 16:27:35 +100064 const struct sha1_state *ictx = in;
Herbert Xu406f1042009-07-10 13:18:26 +080065
66 sctx->count = ictx->count;
67 memcpy(sctx->state, ictx->state, sizeof(ictx->state));
68 memcpy(sctx->buf, ictx->buffer, sizeof(ictx->buffer));
69 sctx->func = KIMD_SHA_1;
70 return 0;
71}
72
Herbert Xu563f3462009-01-18 20:33:33 +110073static struct shash_alg alg = {
74 .digestsize = SHA1_DIGEST_SIZE,
75 .init = sha1_init,
76 .update = s390_sha_update,
77 .final = s390_sha_final,
Herbert Xu406f1042009-07-10 13:18:26 +080078 .export = sha1_export,
79 .import = sha1_import,
Herbert Xu563f3462009-01-18 20:33:33 +110080 .descsize = sizeof(struct s390_sha_ctx),
Herbert Xu406f1042009-07-10 13:18:26 +080081 .statesize = sizeof(struct sha1_state),
Herbert Xu563f3462009-01-18 20:33:33 +110082 .base = {
83 .cra_name = "sha1",
84 .cra_driver_name= "sha1-s390",
85 .cra_priority = CRYPT_S390_PRIORITY,
86 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
87 .cra_blocksize = SHA1_BLOCK_SIZE,
88 .cra_module = THIS_MODULE,
89 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070090};
91
Heiko Carstens9f7819c2008-04-17 07:46:17 +020092static int __init sha1_s390_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Jan Glauber1822bc92011-04-19 21:29:14 +020094 if (!crypt_s390_func_available(KIMD_SHA_1, CRYPT_S390_MSA))
Jan Glauber86aa9fc2007-02-05 21:18:14 +010095 return -EOPNOTSUPP;
Herbert Xu563f3462009-01-18 20:33:33 +110096 return crypto_register_shash(&alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
Heiko Carstens9f7819c2008-04-17 07:46:17 +020099static void __exit sha1_s390_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Herbert Xu563f3462009-01-18 20:33:33 +1100101 crypto_unregister_shash(&alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
Hendrik Bruecknerd05377c2015-02-19 17:34:07 +0100104module_cpu_feature_match(MSA, sha1_s390_init);
Heiko Carstens9f7819c2008-04-17 07:46:17 +0200105module_exit(sha1_s390_fini);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Kees Cook5d26a102014-11-20 17:05:53 -0800107MODULE_ALIAS_CRYPTO("sha1");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108MODULE_LICENSE("GPL");
109MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm");