blob: 61a7db372121fc382727a568800934155ddd7eb6 [file] [log] [blame]
Jan Glauber0a497c172006-01-06 00:19:18 -08001/*
2 * Cryptographic API.
3 *
4 * s390 implementation of the SHA256 Secure Hash Algorithm.
5 *
6 * s390 Version:
Jan Glauber86aa9fc2007-02-05 21:18:14 +01007 * Copyright IBM Corp. 2005,2007
Jan Glauber0a497c172006-01-06 00:19:18 -08008 * Author(s): Jan Glauber (jang@de.ibm.com)
9 *
Sebastian Siewiorad5d2782007-10-08 11:45:10 +080010 * Derived from "crypto/sha256_generic.c"
Jan Glauber0a497c172006-01-06 00:19:18 -080011 * 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 */
Herbert Xu563f3462009-01-18 20:33:33 +110019#include <crypto/internal/hash.h>
Jan Glauber0a497c172006-01-06 00:19:18 -080020#include <linux/init.h>
21#include <linux/module.h>
Jan Glauber5265eeb2007-10-09 22:43:13 +080022#include <crypto/sha.h>
Jan Glauber0a497c172006-01-06 00:19:18 -080023
24#include "crypt_s390.h"
Jan Glauber604973f2008-03-06 19:50:20 +080025#include "sha.h"
Jan Glauber0a497c172006-01-06 00:19:18 -080026
Herbert Xu563f3462009-01-18 20:33:33 +110027static int sha256_init(struct shash_desc *desc)
Jan Glauber0a497c172006-01-06 00:19:18 -080028{
Herbert Xu563f3462009-01-18 20:33:33 +110029 struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
Jan Glauber0a497c172006-01-06 00:19:18 -080030
Jan Glauber5265eeb2007-10-09 22:43:13 +080031 sctx->state[0] = SHA256_H0;
32 sctx->state[1] = SHA256_H1;
33 sctx->state[2] = SHA256_H2;
34 sctx->state[3] = SHA256_H3;
35 sctx->state[4] = SHA256_H4;
36 sctx->state[5] = SHA256_H5;
37 sctx->state[6] = SHA256_H6;
38 sctx->state[7] = SHA256_H7;
Jan Glauber0a497c172006-01-06 00:19:18 -080039 sctx->count = 0;
Jan Glauber604973f2008-03-06 19:50:20 +080040 sctx->func = KIMD_SHA_256;
Herbert Xu563f3462009-01-18 20:33:33 +110041
42 return 0;
Jan Glauber0a497c172006-01-06 00:19:18 -080043}
44
Herbert Xuf63559b2009-07-10 13:20:32 +080045static int sha256_export(struct shash_desc *desc, void *out)
46{
47 struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
48 struct sha256_state *octx = out;
49
50 octx->count = sctx->count;
51 memcpy(octx->state, sctx->state, sizeof(octx->state));
52 memcpy(octx->buf, sctx->buf, sizeof(octx->buf));
53 return 0;
54}
55
Jan Glauber81bd5f62009-09-05 16:27:35 +100056static int sha256_import(struct shash_desc *desc, const void *in)
Herbert Xuf63559b2009-07-10 13:20:32 +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 sha256_state *ictx = in;
Herbert Xuf63559b2009-07-10 13:20:32 +080060
61 sctx->count = ictx->count;
62 memcpy(sctx->state, ictx->state, sizeof(ictx->state));
63 memcpy(sctx->buf, ictx->buf, sizeof(ictx->buf));
64 sctx->func = KIMD_SHA_256;
65 return 0;
66}
67
Herbert Xu563f3462009-01-18 20:33:33 +110068static struct shash_alg alg = {
69 .digestsize = SHA256_DIGEST_SIZE,
70 .init = sha256_init,
71 .update = s390_sha_update,
72 .final = s390_sha_final,
Herbert Xuf63559b2009-07-10 13:20:32 +080073 .export = sha256_export,
74 .import = sha256_import,
Herbert Xu563f3462009-01-18 20:33:33 +110075 .descsize = sizeof(struct s390_sha_ctx),
Herbert Xuf63559b2009-07-10 13:20:32 +080076 .statesize = sizeof(struct sha256_state),
Herbert Xu563f3462009-01-18 20:33:33 +110077 .base = {
78 .cra_name = "sha256",
79 .cra_driver_name= "sha256-s390",
80 .cra_priority = CRYPT_S390_PRIORITY,
81 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
82 .cra_blocksize = SHA256_BLOCK_SIZE,
83 .cra_module = THIS_MODULE,
84 }
Jan Glauber0a497c172006-01-06 00:19:18 -080085};
86
Heiko Carstens9f7819c2008-04-17 07:46:17 +020087static int sha256_s390_init(void)
Jan Glauber0a497c172006-01-06 00:19:18 -080088{
Jan Glauber0a497c172006-01-06 00:19:18 -080089 if (!crypt_s390_func_available(KIMD_SHA_256))
Jan Glauber86aa9fc2007-02-05 21:18:14 +010090 return -EOPNOTSUPP;
Jan Glauber0a497c172006-01-06 00:19:18 -080091
Herbert Xu563f3462009-01-18 20:33:33 +110092 return crypto_register_shash(&alg);
Jan Glauber0a497c172006-01-06 00:19:18 -080093}
94
Heiko Carstens9f7819c2008-04-17 07:46:17 +020095static void __exit sha256_s390_fini(void)
Jan Glauber0a497c172006-01-06 00:19:18 -080096{
Herbert Xu563f3462009-01-18 20:33:33 +110097 crypto_unregister_shash(&alg);
Jan Glauber0a497c172006-01-06 00:19:18 -080098}
99
Heiko Carstens9f7819c2008-04-17 07:46:17 +0200100module_init(sha256_s390_init);
101module_exit(sha256_s390_fini);
Jan Glauber0a497c172006-01-06 00:19:18 -0800102
103MODULE_ALIAS("sha256");
Jan Glauber0a497c172006-01-06 00:19:18 -0800104MODULE_LICENSE("GPL");
105MODULE_DESCRIPTION("SHA256 Secure Hash Algorithm");