summaryrefslogtreecommitdiff
path: root/cipher/cipher-eax.c
blob: 08f815a9e490f19e90b77762838111d139133c77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/* cipher-eax.c  -  EAX implementation
 * Copyright (C) 2018 Jussi Kivilinna <jussi.kivilinna@iki.fi>
 *
 * This file is part of Libgcrypt.
 *
 * Libgcrypt is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser general Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * Libgcrypt is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "g10lib.h"
#include "cipher.h"
#include "bufhelp.h"
#include "./cipher-internal.h"


gcry_err_code_t
_gcry_cipher_eax_encrypt (gcry_cipher_hd_t c,
                          byte *outbuf, size_t outbuflen,
                          const byte *inbuf, size_t inbuflen)
{
  gcry_err_code_t err;

  if (outbuflen < inbuflen)
    return GPG_ERR_BUFFER_TOO_SHORT;
  if (c->marks.tag)
    return GPG_ERR_INV_STATE;

  if (!c->marks.iv)
    {
      err = _gcry_cipher_eax_set_nonce (c, NULL, 0);
      if (err != 0)
	return err;
    }

  while (inbuflen)
    {
      size_t currlen = inbuflen;

      /* Since checksumming is done after encryption, process input in 24KiB
       * chunks to keep data loaded in L1 cache for checksumming. */
      if (currlen > 24 * 1024)
	currlen = 24 * 1024;

      err = _gcry_cipher_ctr_encrypt (c, outbuf, outbuflen, inbuf, currlen);
      if (err != 0)
	return err;

      err = _gcry_cmac_write (c, &c->u_mode.eax.cmac_ciphertext, outbuf,
			      currlen);
      if (err != 0)
	return err;

      outbuf += currlen;
      inbuf += currlen;
      outbuflen -= currlen;
      inbuflen -= currlen;
    }

  return 0;
}


gcry_err_code_t
_gcry_cipher_eax_decrypt (gcry_cipher_hd_t c,
                          byte *outbuf, size_t outbuflen,
                          const byte *inbuf, size_t inbuflen)
{
  gcry_err_code_t err;

  if (outbuflen < inbuflen)
    return GPG_ERR_BUFFER_TOO_SHORT;
  if (c->marks.tag)
    return GPG_ERR_INV_STATE;

  if (!c->marks.iv)
    {
      err = _gcry_cipher_eax_set_nonce (c, NULL, 0);
      if (err != 0)
	return err;
    }

  while (inbuflen)
    {
      size_t currlen = inbuflen;

      /* Since checksumming is done before decryption, process input in 24KiB
       * chunks to keep data loaded in L1 cache for decryption. */
      if (currlen > 24 * 1024)
	currlen = 24 * 1024;

      err = _gcry_cmac_write (c, &c->u_mode.eax.cmac_ciphertext, inbuf,
			      currlen);
      if (err != 0)
	return err;

      err = _gcry_cipher_ctr_encrypt (c, outbuf, outbuflen, inbuf, currlen);
      if (err != 0)
	return err;

      outbuf += currlen;
      inbuf += currlen;
      outbuflen -= currlen;
      inbuflen -= currlen;
    }

  return 0;
}


gcry_err_code_t
_gcry_cipher_eax_authenticate (gcry_cipher_hd_t c,
                               const byte * aadbuf, size_t aadbuflen)
{
  gcry_err_code_t err;

  if (c->marks.tag)
    return GPG_ERR_INV_STATE;

  if (!c->marks.iv)
    {
      err = _gcry_cipher_eax_set_nonce (c, NULL, 0);
      if (err != 0)
	return err;
    }

  return _gcry_cmac_write (c, &c->u_mode.eax.cmac_header, aadbuf, aadbuflen);
}


gcry_err_code_t
_gcry_cipher_eax_setkey (gcry_cipher_hd_t c)
{
  gcry_err_code_t err;

  err = _gcry_cmac_generate_subkeys (c, &c->u_mode.eax.cmac_header);
  if (err != 0)
    return err;

  buf_cpy (c->u_mode.eax.cmac_ciphertext.subkeys,
	   c->u_mode.eax.cmac_header.subkeys,
	   sizeof(c->u_mode.eax.cmac_header.subkeys));

  return 0;
}


gcry_err_code_t
_gcry_cipher_eax_set_nonce (gcry_cipher_hd_t c, const byte *nonce,
			    size_t noncelen)
{
  gcry_cmac_context_t nonce_cmac;
  unsigned char initbuf[MAX_BLOCKSIZE];
  gcry_err_code_t err;

  c->marks.iv = 0;
  c->marks.tag = 0;

  _gcry_cmac_reset (&c->u_mode.eax.cmac_header);
  _gcry_cmac_reset (&c->u_mode.eax.cmac_ciphertext);

  /* Calculate nonce CMAC */

  memset(&nonce_cmac, 0, sizeof(nonce_cmac));
  memset(&initbuf, 0, sizeof(initbuf));

  buf_cpy (&nonce_cmac.subkeys, c->u_mode.eax.cmac_header.subkeys,
	   sizeof(c->u_mode.eax.cmac_header.subkeys));

  err = _gcry_cmac_write (c, &nonce_cmac, initbuf, c->spec->blocksize);
  if (err != 0)
    return err;

  if (noncelen != 0)
    {
      err = _gcry_cmac_write (c, &nonce_cmac, nonce, noncelen);
      if (err != 0)
        return err;
    }

  err = _gcry_cmac_final (c, &nonce_cmac);
  if (err != 0)
    return err;

  cipher_block_cpy (c->u_iv.iv, nonce_cmac.u_iv.iv, MAX_BLOCKSIZE);
  cipher_block_cpy (c->u_ctr.ctr, nonce_cmac.u_iv.iv, MAX_BLOCKSIZE);

  wipememory (&nonce_cmac, sizeof(nonce_cmac));

  /* Prepare header CMAC */

  initbuf[c->spec->blocksize - 1] = 1;
  err = _gcry_cmac_write (c, &c->u_mode.eax.cmac_header, initbuf,
			  c->spec->blocksize);
  if (err != 0)
    return err;

  /* Prepare ciphertext CMAC */

  initbuf[c->spec->blocksize - 1] = 2;
  err = _gcry_cmac_write (c, &c->u_mode.eax.cmac_ciphertext, initbuf,
			  c->spec->blocksize);
  if (err != 0)
    return err;

  c->marks.iv = 1;
  c->marks.tag = 0;

  return 0;
}


static gcry_err_code_t
_gcry_cipher_eax_tag (gcry_cipher_hd_t c,
                      byte *outbuf, size_t outbuflen, int check)
{
  gcry_err_code_t err;

  if (!c->marks.tag)
    {
      err = _gcry_cmac_final (c, &c->u_mode.eax.cmac_header);
      if (err != 0)
	return err;

      err = _gcry_cmac_final (c, &c->u_mode.eax.cmac_ciphertext);
      if (err != 0)
	return err;

      cipher_block_xor_1 (c->u_iv.iv, c->u_mode.eax.cmac_header.u_iv.iv,
                          MAX_BLOCKSIZE);
      cipher_block_xor_1 (c->u_iv.iv, c->u_mode.eax.cmac_ciphertext.u_iv.iv,
                          MAX_BLOCKSIZE);

      _gcry_cmac_reset (&c->u_mode.eax.cmac_header);
      _gcry_cmac_reset (&c->u_mode.eax.cmac_ciphertext);

      c->marks.tag = 1;
    }

  if (!check)
    {
      if (outbuflen > c->spec->blocksize)
        outbuflen = c->spec->blocksize;

      /* NB: We already checked that OUTBUF is large enough to hold
       * the result or has valid truncated length.  */
      memcpy (outbuf, c->u_iv.iv, outbuflen);
    }
  else
    {
      /* OUTBUFLEN gives the length of the user supplied tag in OUTBUF
       * and thus we need to compare its length first.  */
      if (!(outbuflen <= c->spec->blocksize)
          || !buf_eq_const (outbuf, c->u_iv.iv, outbuflen))
        return GPG_ERR_CHECKSUM;
    }

  return 0;
}


gcry_err_code_t
_gcry_cipher_eax_get_tag (gcry_cipher_hd_t c, unsigned char *outtag,
                          size_t taglen)
{
  return _gcry_cipher_eax_tag (c, outtag, taglen, 0);
}

gcry_err_code_t
_gcry_cipher_eax_check_tag (gcry_cipher_hd_t c, const unsigned char *intag,
                            size_t taglen)
{
  return _gcry_cipher_eax_tag (c, (unsigned char *) intag, taglen, 1);
}