Linux-libre 3.16.85-gnu
[librecmc/linux-libre.git] / crypto / ghash-generic.c
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/internal/hash.h>
18 #include <linux/crypto.h>
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22
23 #define GHASH_BLOCK_SIZE        16
24 #define GHASH_DIGEST_SIZE       16
25
26 struct ghash_ctx {
27         struct gf128mul_4k *gf128;
28 };
29
30 struct ghash_desc_ctx {
31         u8 buffer[GHASH_BLOCK_SIZE];
32         u32 bytes;
33 };
34
35 static int ghash_init(struct shash_desc *desc)
36 {
37         struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
38
39         memset(dctx, 0, sizeof(*dctx));
40
41         return 0;
42 }
43
44 static int ghash_setkey(struct crypto_shash *tfm,
45                         const u8 *key, unsigned int keylen)
46 {
47         struct ghash_ctx *ctx = crypto_shash_ctx(tfm);
48         be128 k;
49
50         if (keylen != GHASH_BLOCK_SIZE) {
51                 crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
52                 return -EINVAL;
53         }
54
55         if (ctx->gf128)
56                 gf128mul_free_4k(ctx->gf128);
57
58         BUILD_BUG_ON(sizeof(k) != GHASH_BLOCK_SIZE);
59         memcpy(&k, key, GHASH_BLOCK_SIZE); /* avoid violating alignment rules */
60         ctx->gf128 = gf128mul_init_4k_lle(&k);
61         memzero_explicit(&k, GHASH_BLOCK_SIZE);
62
63         if (!ctx->gf128)
64                 return -ENOMEM;
65
66         return 0;
67 }
68
69 static int ghash_update(struct shash_desc *desc,
70                          const u8 *src, unsigned int srclen)
71 {
72         struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
73         struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
74         u8 *dst = dctx->buffer;
75
76         if (!ctx->gf128)
77                 return -ENOKEY;
78
79         if (dctx->bytes) {
80                 int n = min(srclen, dctx->bytes);
81                 u8 *pos = dst + (GHASH_BLOCK_SIZE - dctx->bytes);
82
83                 dctx->bytes -= n;
84                 srclen -= n;
85
86                 while (n--)
87                         *pos++ ^= *src++;
88
89                 if (!dctx->bytes)
90                         gf128mul_4k_lle((be128 *)dst, ctx->gf128);
91         }
92
93         while (srclen >= GHASH_BLOCK_SIZE) {
94                 crypto_xor(dst, src, GHASH_BLOCK_SIZE);
95                 gf128mul_4k_lle((be128 *)dst, ctx->gf128);
96                 src += GHASH_BLOCK_SIZE;
97                 srclen -= GHASH_BLOCK_SIZE;
98         }
99
100         if (srclen) {
101                 dctx->bytes = GHASH_BLOCK_SIZE - srclen;
102                 while (srclen--)
103                         *dst++ ^= *src++;
104         }
105
106         return 0;
107 }
108
109 static void ghash_flush(struct ghash_ctx *ctx, struct ghash_desc_ctx *dctx)
110 {
111         u8 *dst = dctx->buffer;
112
113         if (dctx->bytes) {
114                 u8 *tmp = dst + (GHASH_BLOCK_SIZE - dctx->bytes);
115
116                 while (dctx->bytes--)
117                         *tmp++ ^= 0;
118
119                 gf128mul_4k_lle((be128 *)dst, ctx->gf128);
120         }
121
122         dctx->bytes = 0;
123 }
124
125 static int ghash_final(struct shash_desc *desc, u8 *dst)
126 {
127         struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
128         struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
129         u8 *buf = dctx->buffer;
130
131         if (!ctx->gf128)
132                 return -ENOKEY;
133
134         ghash_flush(ctx, dctx);
135         memcpy(dst, buf, GHASH_BLOCK_SIZE);
136
137         return 0;
138 }
139
140 static void ghash_exit_tfm(struct crypto_tfm *tfm)
141 {
142         struct ghash_ctx *ctx = crypto_tfm_ctx(tfm);
143         if (ctx->gf128)
144                 gf128mul_free_4k(ctx->gf128);
145 }
146
147 static struct shash_alg ghash_alg = {
148         .digestsize     = GHASH_DIGEST_SIZE,
149         .init           = ghash_init,
150         .update         = ghash_update,
151         .final          = ghash_final,
152         .setkey         = ghash_setkey,
153         .descsize       = sizeof(struct ghash_desc_ctx),
154         .base           = {
155                 .cra_name               = "ghash",
156                 .cra_driver_name        = "ghash-generic",
157                 .cra_priority           = 100,
158                 .cra_flags              = CRYPTO_ALG_TYPE_SHASH,
159                 .cra_blocksize          = GHASH_BLOCK_SIZE,
160                 .cra_ctxsize            = sizeof(struct ghash_ctx),
161                 .cra_module             = THIS_MODULE,
162                 .cra_exit               = ghash_exit_tfm,
163         },
164 };
165
166 static int __init ghash_mod_init(void)
167 {
168         return crypto_register_shash(&ghash_alg);
169 }
170
171 static void __exit ghash_mod_exit(void)
172 {
173         crypto_unregister_shash(&ghash_alg);
174 }
175
176 module_init(ghash_mod_init);
177 module_exit(ghash_mod_exit);
178
179 MODULE_LICENSE("GPL");
180 MODULE_DESCRIPTION("GHASH Message Digest Algorithm");
181 MODULE_ALIAS_CRYPTO("ghash");
182 MODULE_ALIAS_CRYPTO("ghash-generic");