• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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>
17 #include <crypto/ghash.h>
18 #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 
ghash_init(struct shash_desc * desc)24 static 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 
ghash_setkey(struct crypto_shash * tfm,const u8 * key,unsigned int keylen)33 static int ghash_setkey(struct crypto_shash *tfm,
34 			const u8 *key, unsigned int keylen)
35 {
36 	struct ghash_ctx *ctx = crypto_shash_ctx(tfm);
37 	be128 k;
38 
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);
46 
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 
52 	if (!ctx->gf128)
53 		return -ENOMEM;
54 
55 	return 0;
56 }
57 
ghash_update(struct shash_desc * desc,const u8 * src,unsigned int srclen)58 static 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 
65 	if (dctx->bytes) {
66 		int n = min(srclen, dctx->bytes);
67 		u8 *pos = dst + (GHASH_BLOCK_SIZE - dctx->bytes);
68 
69 		dctx->bytes -= n;
70 		srclen -= n;
71 
72 		while (n--)
73 			*pos++ ^= *src++;
74 
75 		if (!dctx->bytes)
76 			gf128mul_4k_lle((be128 *)dst, ctx->gf128);
77 	}
78 
79 	while (srclen >= GHASH_BLOCK_SIZE) {
80 		crypto_xor(dst, src, GHASH_BLOCK_SIZE);
81 		gf128mul_4k_lle((be128 *)dst, ctx->gf128);
82 		src += GHASH_BLOCK_SIZE;
83 		srclen -= GHASH_BLOCK_SIZE;
84 	}
85 
86 	if (srclen) {
87 		dctx->bytes = GHASH_BLOCK_SIZE - srclen;
88 		while (srclen--)
89 			*dst++ ^= *src++;
90 	}
91 
92 	return 0;
93 }
94 
ghash_flush(struct ghash_ctx * ctx,struct ghash_desc_ctx * dctx)95 static void ghash_flush(struct ghash_ctx *ctx, struct ghash_desc_ctx *dctx)
96 {
97 	u8 *dst = dctx->buffer;
98 
99 	if (dctx->bytes) {
100 		u8 *tmp = dst + (GHASH_BLOCK_SIZE - dctx->bytes);
101 
102 		while (dctx->bytes--)
103 			*tmp++ ^= 0;
104 
105 		gf128mul_4k_lle((be128 *)dst, ctx->gf128);
106 	}
107 
108 	dctx->bytes = 0;
109 }
110 
ghash_final(struct shash_desc * desc,u8 * dst)111 static int ghash_final(struct shash_desc *desc, u8 *dst)
112 {
113 	struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
114 	struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
115 	u8 *buf = dctx->buffer;
116 
117 	ghash_flush(ctx, dctx);
118 	memcpy(dst, buf, GHASH_BLOCK_SIZE);
119 
120 	return 0;
121 }
122 
ghash_exit_tfm(struct crypto_tfm * tfm)123 static void ghash_exit_tfm(struct crypto_tfm *tfm)
124 {
125 	struct ghash_ctx *ctx = crypto_tfm_ctx(tfm);
126 	if (ctx->gf128)
127 		gf128mul_free_4k(ctx->gf128);
128 }
129 
130 static struct shash_alg ghash_alg = {
131 	.digestsize	= GHASH_DIGEST_SIZE,
132 	.init		= ghash_init,
133 	.update		= ghash_update,
134 	.final		= ghash_final,
135 	.setkey		= ghash_setkey,
136 	.descsize	= sizeof(struct ghash_desc_ctx),
137 	.base		= {
138 		.cra_name		= "ghash",
139 		.cra_driver_name	= "ghash-generic",
140 		.cra_priority		= 100,
141 		.cra_blocksize		= GHASH_BLOCK_SIZE,
142 		.cra_ctxsize		= sizeof(struct ghash_ctx),
143 		.cra_module		= THIS_MODULE,
144 		.cra_exit		= ghash_exit_tfm,
145 	},
146 };
147 
ghash_mod_init(void)148 static int __init ghash_mod_init(void)
149 {
150 	return crypto_register_shash(&ghash_alg);
151 }
152 
ghash_mod_exit(void)153 static void __exit ghash_mod_exit(void)
154 {
155 	crypto_unregister_shash(&ghash_alg);
156 }
157 
158 module_init(ghash_mod_init);
159 module_exit(ghash_mod_exit);
160 
161 MODULE_LICENSE("GPL");
162 MODULE_DESCRIPTION("GHASH Message Digest Algorithm");
163 MODULE_ALIAS_CRYPTO("ghash");
164 MODULE_ALIAS_CRYPTO("ghash-generic");
165