blob: 6425b9cd718e6a69e3074b011acedef2deaa0271 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Huang Ying2cdc6892009-08-06 15:32:38 +10002/*
3 * GHASH: digest algorithm for GCM (Galois/Counter Mode).
4 *
5 * Copyright (c) 2007 Nokia Siemens Networks - Mikko Herranen <mh1@iki.fi>
6 * Copyright (c) 2009 Intel Corp.
7 * Author: Huang Ying <ying.huang@intel.com>
8 *
9 * The algorithm implementation is copied from gcm.c.
Huang Ying2cdc6892009-08-06 15:32:38 +100010 */
11
12#include <crypto/algapi.h>
13#include <crypto/gf128mul.h>
Marcelo Cerria397ba82016-09-28 13:42:09 -030014#include <crypto/ghash.h>
Huang Ying2cdc6892009-08-06 15:32:38 +100015#include <crypto/internal/hash.h>
16#include <linux/crypto.h>
17#include <linux/init.h>
18#include <linux/kernel.h>
19#include <linux/module.h>
20
Huang Ying2cdc6892009-08-06 15:32:38 +100021static int ghash_init(struct shash_desc *desc)
22{
23 struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
24
25 memset(dctx, 0, sizeof(*dctx));
26
27 return 0;
28}
29
30static int ghash_setkey(struct crypto_shash *tfm,
31 const u8 *key, unsigned int keylen)
32{
33 struct ghash_ctx *ctx = crypto_shash_ctx(tfm);
34
35 if (keylen != GHASH_BLOCK_SIZE) {
36 crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
37 return -EINVAL;
38 }
39
40 if (ctx->gf128)
41 gf128mul_free_4k(ctx->gf128);
42 ctx->gf128 = gf128mul_init_4k_lle((be128 *)key);
43 if (!ctx->gf128)
44 return -ENOMEM;
45
46 return 0;
47}
48
49static int ghash_update(struct shash_desc *desc,
50 const u8 *src, unsigned int srclen)
51{
52 struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
53 struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
54 u8 *dst = dctx->buffer;
55
56 if (dctx->bytes) {
57 int n = min(srclen, dctx->bytes);
58 u8 *pos = dst + (GHASH_BLOCK_SIZE - dctx->bytes);
59
60 dctx->bytes -= n;
61 srclen -= n;
62
63 while (n--)
64 *pos++ ^= *src++;
65
66 if (!dctx->bytes)
67 gf128mul_4k_lle((be128 *)dst, ctx->gf128);
68 }
69
70 while (srclen >= GHASH_BLOCK_SIZE) {
71 crypto_xor(dst, src, GHASH_BLOCK_SIZE);
72 gf128mul_4k_lle((be128 *)dst, ctx->gf128);
73 src += GHASH_BLOCK_SIZE;
74 srclen -= GHASH_BLOCK_SIZE;
75 }
76
77 if (srclen) {
78 dctx->bytes = GHASH_BLOCK_SIZE - srclen;
79 while (srclen--)
80 *dst++ ^= *src++;
81 }
82
83 return 0;
84}
85
86static void ghash_flush(struct ghash_ctx *ctx, struct ghash_desc_ctx *dctx)
87{
88 u8 *dst = dctx->buffer;
89
90 if (dctx->bytes) {
91 u8 *tmp = dst + (GHASH_BLOCK_SIZE - dctx->bytes);
92
93 while (dctx->bytes--)
94 *tmp++ ^= 0;
95
96 gf128mul_4k_lle((be128 *)dst, ctx->gf128);
97 }
98
99 dctx->bytes = 0;
100}
101
102static int ghash_final(struct shash_desc *desc, u8 *dst)
103{
104 struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
105 struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
106 u8 *buf = dctx->buffer;
107
Huang Ying2cdc6892009-08-06 15:32:38 +1000108 ghash_flush(ctx, dctx);
109 memcpy(dst, buf, GHASH_BLOCK_SIZE);
110
111 return 0;
112}
113
114static void ghash_exit_tfm(struct crypto_tfm *tfm)
115{
116 struct ghash_ctx *ctx = crypto_tfm_ctx(tfm);
117 if (ctx->gf128)
118 gf128mul_free_4k(ctx->gf128);
119}
120
121static struct shash_alg ghash_alg = {
122 .digestsize = GHASH_DIGEST_SIZE,
123 .init = ghash_init,
124 .update = ghash_update,
125 .final = ghash_final,
126 .setkey = ghash_setkey,
127 .descsize = sizeof(struct ghash_desc_ctx),
128 .base = {
129 .cra_name = "ghash",
130 .cra_driver_name = "ghash-generic",
131 .cra_priority = 100,
Huang Ying2cdc6892009-08-06 15:32:38 +1000132 .cra_blocksize = GHASH_BLOCK_SIZE,
133 .cra_ctxsize = sizeof(struct ghash_ctx),
134 .cra_module = THIS_MODULE,
Huang Ying2cdc6892009-08-06 15:32:38 +1000135 .cra_exit = ghash_exit_tfm,
136 },
137};
138
139static int __init ghash_mod_init(void)
140{
141 return crypto_register_shash(&ghash_alg);
142}
143
144static void __exit ghash_mod_exit(void)
145{
146 crypto_unregister_shash(&ghash_alg);
147}
148
Eric Biggersc4741b22019-04-11 21:57:42 -0700149subsys_initcall(ghash_mod_init);
Huang Ying2cdc6892009-08-06 15:32:38 +1000150module_exit(ghash_mod_exit);
151
152MODULE_LICENSE("GPL");
153MODULE_DESCRIPTION("GHASH Message Digest Algorithm");
Kees Cook5d26a102014-11-20 17:05:53 -0800154MODULE_ALIAS_CRYPTO("ghash");
Mathias Krause3e14dcf2015-01-11 18:17:42 +0100155MODULE_ALIAS_CRYPTO("ghash-generic");