blob: 73b56f2f44f187903dbdbc785e1eb240b76d3d34 [file] [log] [blame]
Huang Ying2cdc6892009-08-06 15:32:38 +10001/*
2 * GHASH: digest algorithm for GCM (Galois/Counter Mode).
3 *
4 * Copyright (c) 2007 Nokia Siemens Networks - Mikko Herranen <mh1@iki.fi>
5 * Copyright (c) 2009 Intel Corp.
6 * Author: Huang Ying <ying.huang@intel.com>
7 *
8 * The algorithm implementation is copied from gcm.c.
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License version 2 as published
12 * by the Free Software Foundation.
13 */
14
15#include <crypto/algapi.h>
16#include <crypto/gf128mul.h>
Marcelo Cerria397ba82016-09-28 13:42:09 -030017#include <crypto/ghash.h>
Huang Ying2cdc6892009-08-06 15:32:38 +100018#include <crypto/internal/hash.h>
19#include <linux/crypto.h>
20#include <linux/init.h>
21#include <linux/kernel.h>
22#include <linux/module.h>
23
Huang Ying2cdc6892009-08-06 15:32:38 +100024static int ghash_init(struct shash_desc *desc)
25{
26 struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
27
28 memset(dctx, 0, sizeof(*dctx));
29
30 return 0;
31}
32
33static int ghash_setkey(struct crypto_shash *tfm,
34 const u8 *key, unsigned int keylen)
35{
36 struct ghash_ctx *ctx = crypto_shash_ctx(tfm);
Eric Biggersdee24082019-05-30 10:50:39 -070037 be128 k;
Huang Ying2cdc6892009-08-06 15:32:38 +100038
39 if (keylen != GHASH_BLOCK_SIZE) {
40 crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
41 return -EINVAL;
42 }
43
44 if (ctx->gf128)
45 gf128mul_free_4k(ctx->gf128);
Eric Biggersdee24082019-05-30 10:50:39 -070046
47 BUILD_BUG_ON(sizeof(k) != GHASH_BLOCK_SIZE);
48 memcpy(&k, key, GHASH_BLOCK_SIZE); /* avoid violating alignment rules */
49 ctx->gf128 = gf128mul_init_4k_lle(&k);
50 memzero_explicit(&k, GHASH_BLOCK_SIZE);
51
Huang Ying2cdc6892009-08-06 15:32:38 +100052 if (!ctx->gf128)
53 return -ENOMEM;
54
55 return 0;
56}
57
58static int ghash_update(struct shash_desc *desc,
59 const u8 *src, unsigned int srclen)
60{
61 struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
62 struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
63 u8 *dst = dctx->buffer;
64
Nick Bowler7ed47b72011-10-20 14:16:55 +020065 if (!ctx->gf128)
66 return -ENOKEY;
67
Huang Ying2cdc6892009-08-06 15:32:38 +100068 if (dctx->bytes) {
69 int n = min(srclen, dctx->bytes);
70 u8 *pos = dst + (GHASH_BLOCK_SIZE - dctx->bytes);
71
72 dctx->bytes -= n;
73 srclen -= n;
74
75 while (n--)
76 *pos++ ^= *src++;
77
78 if (!dctx->bytes)
79 gf128mul_4k_lle((be128 *)dst, ctx->gf128);
80 }
81
82 while (srclen >= GHASH_BLOCK_SIZE) {
83 crypto_xor(dst, src, GHASH_BLOCK_SIZE);
84 gf128mul_4k_lle((be128 *)dst, ctx->gf128);
85 src += GHASH_BLOCK_SIZE;
86 srclen -= GHASH_BLOCK_SIZE;
87 }
88
89 if (srclen) {
90 dctx->bytes = GHASH_BLOCK_SIZE - srclen;
91 while (srclen--)
92 *dst++ ^= *src++;
93 }
94
95 return 0;
96}
97
98static void ghash_flush(struct ghash_ctx *ctx, struct ghash_desc_ctx *dctx)
99{
100 u8 *dst = dctx->buffer;
101
102 if (dctx->bytes) {
103 u8 *tmp = dst + (GHASH_BLOCK_SIZE - dctx->bytes);
104
105 while (dctx->bytes--)
106 *tmp++ ^= 0;
107
108 gf128mul_4k_lle((be128 *)dst, ctx->gf128);
109 }
110
111 dctx->bytes = 0;
112}
113
114static int ghash_final(struct shash_desc *desc, u8 *dst)
115{
116 struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
117 struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
118 u8 *buf = dctx->buffer;
119
Nick Bowler7ed47b72011-10-20 14:16:55 +0200120 if (!ctx->gf128)
121 return -ENOKEY;
122
Huang Ying2cdc6892009-08-06 15:32:38 +1000123 ghash_flush(ctx, dctx);
124 memcpy(dst, buf, GHASH_BLOCK_SIZE);
125
126 return 0;
127}
128
129static void ghash_exit_tfm(struct crypto_tfm *tfm)
130{
131 struct ghash_ctx *ctx = crypto_tfm_ctx(tfm);
132 if (ctx->gf128)
133 gf128mul_free_4k(ctx->gf128);
134}
135
136static struct shash_alg ghash_alg = {
137 .digestsize = GHASH_DIGEST_SIZE,
138 .init = ghash_init,
139 .update = ghash_update,
140 .final = ghash_final,
141 .setkey = ghash_setkey,
142 .descsize = sizeof(struct ghash_desc_ctx),
143 .base = {
144 .cra_name = "ghash",
145 .cra_driver_name = "ghash-generic",
146 .cra_priority = 100,
147 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
148 .cra_blocksize = GHASH_BLOCK_SIZE,
149 .cra_ctxsize = sizeof(struct ghash_ctx),
150 .cra_module = THIS_MODULE,
Huang Ying2cdc6892009-08-06 15:32:38 +1000151 .cra_exit = ghash_exit_tfm,
152 },
153};
154
155static int __init ghash_mod_init(void)
156{
157 return crypto_register_shash(&ghash_alg);
158}
159
160static void __exit ghash_mod_exit(void)
161{
162 crypto_unregister_shash(&ghash_alg);
163}
164
165module_init(ghash_mod_init);
166module_exit(ghash_mod_exit);
167
168MODULE_LICENSE("GPL");
169MODULE_DESCRIPTION("GHASH Message Digest Algorithm");
Kees Cook5d26a102014-11-20 17:05:53 -0800170MODULE_ALIAS_CRYPTO("ghash");
Mathias Krause3e14dcf2015-01-11 18:17:42 +0100171MODULE_ALIAS_CRYPTO("ghash-generic");