Gerald Schaefer | df1309c | 2011-04-19 21:29:18 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Cryptographic API. |
| 3 | * |
| 4 | * s390 implementation of the GHASH algorithm for GCM (Galois/Counter Mode). |
| 5 | * |
| 6 | * Copyright IBM Corp. 2011 |
| 7 | * Author(s): Gerald Schaefer <gerald.schaefer@de.ibm.com> |
| 8 | */ |
| 9 | |
| 10 | #include <crypto/internal/hash.h> |
| 11 | #include <linux/module.h> |
Hendrik Brueckner | d05377c | 2015-02-19 17:34:07 +0100 | [diff] [blame] | 12 | #include <linux/cpufeature.h> |
Martin Schwidefsky | c7d4d25 | 2016-03-17 15:22:12 +0100 | [diff] [blame] | 13 | #include <asm/cpacf.h> |
Gerald Schaefer | df1309c | 2011-04-19 21:29:18 +0200 | [diff] [blame] | 14 | |
| 15 | #define GHASH_BLOCK_SIZE 16 |
| 16 | #define GHASH_DIGEST_SIZE 16 |
| 17 | |
| 18 | struct ghash_ctx { |
Harald Freudenberger | a1cae34 | 2015-05-21 10:01:11 +0200 | [diff] [blame] | 19 | u8 key[GHASH_BLOCK_SIZE]; |
Gerald Schaefer | df1309c | 2011-04-19 21:29:18 +0200 | [diff] [blame] | 20 | }; |
| 21 | |
| 22 | struct ghash_desc_ctx { |
Harald Freudenberger | a1cae34 | 2015-05-21 10:01:11 +0200 | [diff] [blame] | 23 | u8 icv[GHASH_BLOCK_SIZE]; |
| 24 | u8 key[GHASH_BLOCK_SIZE]; |
Gerald Schaefer | df1309c | 2011-04-19 21:29:18 +0200 | [diff] [blame] | 25 | u8 buffer[GHASH_BLOCK_SIZE]; |
| 26 | u32 bytes; |
| 27 | }; |
| 28 | |
| 29 | static int ghash_init(struct shash_desc *desc) |
| 30 | { |
| 31 | struct ghash_desc_ctx *dctx = shash_desc_ctx(desc); |
Harald Freudenberger | a1cae34 | 2015-05-21 10:01:11 +0200 | [diff] [blame] | 32 | struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm); |
Gerald Schaefer | df1309c | 2011-04-19 21:29:18 +0200 | [diff] [blame] | 33 | |
| 34 | memset(dctx, 0, sizeof(*dctx)); |
Harald Freudenberger | a1cae34 | 2015-05-21 10:01:11 +0200 | [diff] [blame] | 35 | memcpy(dctx->key, ctx->key, GHASH_BLOCK_SIZE); |
Gerald Schaefer | df1309c | 2011-04-19 21:29:18 +0200 | [diff] [blame] | 36 | |
| 37 | return 0; |
| 38 | } |
| 39 | |
| 40 | static int ghash_setkey(struct crypto_shash *tfm, |
| 41 | const u8 *key, unsigned int keylen) |
| 42 | { |
| 43 | struct ghash_ctx *ctx = crypto_shash_ctx(tfm); |
| 44 | |
| 45 | if (keylen != GHASH_BLOCK_SIZE) { |
| 46 | crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN); |
| 47 | return -EINVAL; |
| 48 | } |
| 49 | |
| 50 | memcpy(ctx->key, key, GHASH_BLOCK_SIZE); |
Gerald Schaefer | df1309c | 2011-04-19 21:29:18 +0200 | [diff] [blame] | 51 | |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | static int ghash_update(struct shash_desc *desc, |
| 56 | const u8 *src, unsigned int srclen) |
| 57 | { |
| 58 | struct ghash_desc_ctx *dctx = shash_desc_ctx(desc); |
Gerald Schaefer | df1309c | 2011-04-19 21:29:18 +0200 | [diff] [blame] | 59 | unsigned int n; |
| 60 | u8 *buf = dctx->buffer; |
Gerald Schaefer | df1309c | 2011-04-19 21:29:18 +0200 | [diff] [blame] | 61 | |
| 62 | if (dctx->bytes) { |
| 63 | u8 *pos = buf + (GHASH_BLOCK_SIZE - dctx->bytes); |
| 64 | |
| 65 | n = min(srclen, dctx->bytes); |
| 66 | dctx->bytes -= n; |
| 67 | srclen -= n; |
| 68 | |
| 69 | memcpy(pos, src, n); |
| 70 | src += n; |
| 71 | |
| 72 | if (!dctx->bytes) { |
Martin Schwidefsky | 0177db0 | 2016-08-15 10:41:52 +0200 | [diff] [blame] | 73 | cpacf_kimd(CPACF_KIMD_GHASH, dctx, buf, |
| 74 | GHASH_BLOCK_SIZE); |
Gerald Schaefer | df1309c | 2011-04-19 21:29:18 +0200 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
| 78 | n = srclen & ~(GHASH_BLOCK_SIZE - 1); |
| 79 | if (n) { |
Martin Schwidefsky | 0177db0 | 2016-08-15 10:41:52 +0200 | [diff] [blame] | 80 | cpacf_kimd(CPACF_KIMD_GHASH, dctx, src, n); |
Gerald Schaefer | df1309c | 2011-04-19 21:29:18 +0200 | [diff] [blame] | 81 | src += n; |
| 82 | srclen -= n; |
| 83 | } |
| 84 | |
| 85 | if (srclen) { |
| 86 | dctx->bytes = GHASH_BLOCK_SIZE - srclen; |
| 87 | memcpy(buf, src, srclen); |
| 88 | } |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
Harald Freudenberger | a1cae34 | 2015-05-21 10:01:11 +0200 | [diff] [blame] | 93 | static int ghash_flush(struct ghash_desc_ctx *dctx) |
Gerald Schaefer | df1309c | 2011-04-19 21:29:18 +0200 | [diff] [blame] | 94 | { |
| 95 | u8 *buf = dctx->buffer; |
Gerald Schaefer | df1309c | 2011-04-19 21:29:18 +0200 | [diff] [blame] | 96 | |
| 97 | if (dctx->bytes) { |
| 98 | u8 *pos = buf + (GHASH_BLOCK_SIZE - dctx->bytes); |
| 99 | |
| 100 | memset(pos, 0, dctx->bytes); |
Martin Schwidefsky | 0177db0 | 2016-08-15 10:41:52 +0200 | [diff] [blame] | 101 | cpacf_kimd(CPACF_KIMD_GHASH, dctx, buf, GHASH_BLOCK_SIZE); |
Harald Freudenberger | a1cae34 | 2015-05-21 10:01:11 +0200 | [diff] [blame] | 102 | dctx->bytes = 0; |
Gerald Schaefer | df1309c | 2011-04-19 21:29:18 +0200 | [diff] [blame] | 103 | } |
| 104 | |
Jan Glauber | 36eb2ca | 2012-10-26 15:06:12 +0200 | [diff] [blame] | 105 | return 0; |
Gerald Schaefer | df1309c | 2011-04-19 21:29:18 +0200 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | static int ghash_final(struct shash_desc *desc, u8 *dst) |
| 109 | { |
| 110 | struct ghash_desc_ctx *dctx = shash_desc_ctx(desc); |
Jan Glauber | 36eb2ca | 2012-10-26 15:06:12 +0200 | [diff] [blame] | 111 | int ret; |
Gerald Schaefer | df1309c | 2011-04-19 21:29:18 +0200 | [diff] [blame] | 112 | |
Harald Freudenberger | a1cae34 | 2015-05-21 10:01:11 +0200 | [diff] [blame] | 113 | ret = ghash_flush(dctx); |
Jan Glauber | 36eb2ca | 2012-10-26 15:06:12 +0200 | [diff] [blame] | 114 | if (!ret) |
Harald Freudenberger | a1cae34 | 2015-05-21 10:01:11 +0200 | [diff] [blame] | 115 | memcpy(dst, dctx->icv, GHASH_BLOCK_SIZE); |
Jan Glauber | 36eb2ca | 2012-10-26 15:06:12 +0200 | [diff] [blame] | 116 | return ret; |
Gerald Schaefer | df1309c | 2011-04-19 21:29:18 +0200 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static struct shash_alg ghash_alg = { |
| 120 | .digestsize = GHASH_DIGEST_SIZE, |
| 121 | .init = ghash_init, |
| 122 | .update = ghash_update, |
| 123 | .final = ghash_final, |
| 124 | .setkey = ghash_setkey, |
| 125 | .descsize = sizeof(struct ghash_desc_ctx), |
| 126 | .base = { |
| 127 | .cra_name = "ghash", |
| 128 | .cra_driver_name = "ghash-s390", |
Martin Schwidefsky | c7d4d25 | 2016-03-17 15:22:12 +0100 | [diff] [blame] | 129 | .cra_priority = 300, |
Gerald Schaefer | df1309c | 2011-04-19 21:29:18 +0200 | [diff] [blame] | 130 | .cra_flags = CRYPTO_ALG_TYPE_SHASH, |
| 131 | .cra_blocksize = GHASH_BLOCK_SIZE, |
| 132 | .cra_ctxsize = sizeof(struct ghash_ctx), |
| 133 | .cra_module = THIS_MODULE, |
Gerald Schaefer | df1309c | 2011-04-19 21:29:18 +0200 | [diff] [blame] | 134 | }, |
| 135 | }; |
| 136 | |
| 137 | static int __init ghash_mod_init(void) |
| 138 | { |
Martin Schwidefsky | 69c0e36 | 2016-08-18 12:59:46 +0200 | [diff] [blame] | 139 | if (!cpacf_query_func(CPACF_KIMD, CPACF_KIMD_GHASH)) |
Gerald Schaefer | df1309c | 2011-04-19 21:29:18 +0200 | [diff] [blame] | 140 | return -EOPNOTSUPP; |
| 141 | |
| 142 | return crypto_register_shash(&ghash_alg); |
| 143 | } |
| 144 | |
| 145 | static void __exit ghash_mod_exit(void) |
| 146 | { |
| 147 | crypto_unregister_shash(&ghash_alg); |
| 148 | } |
| 149 | |
Hendrik Brueckner | d05377c | 2015-02-19 17:34:07 +0100 | [diff] [blame] | 150 | module_cpu_feature_match(MSA, ghash_mod_init); |
Gerald Schaefer | df1309c | 2011-04-19 21:29:18 +0200 | [diff] [blame] | 151 | module_exit(ghash_mod_exit); |
| 152 | |
Kees Cook | 5d26a10 | 2014-11-20 17:05:53 -0800 | [diff] [blame] | 153 | MODULE_ALIAS_CRYPTO("ghash"); |
Gerald Schaefer | df1309c | 2011-04-19 21:29:18 +0200 | [diff] [blame] | 154 | |
| 155 | MODULE_LICENSE("GPL"); |
| 156 | MODULE_DESCRIPTION("GHASH Message Digest Algorithm, s390 implementation"); |