summaryrefslogtreecommitdiff
path: root/cipher/cipher-poly1305.c
blob: fb8177422581d0e723d2295cf227d7e23ae1ea8d (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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/* cipher-poly1305.c  -  Poly1305 based AEAD cipher mode, RFC-7539
 * Copyright (C) 2014 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"
#include "./poly1305-internal.h"


static inline int
poly1305_bytecounter_add (u32 ctr[2], size_t add)
{
  int overflow = 0;

  if (sizeof(add) > sizeof(u32))
    {
      u32 high_add = ((add >> 31) >> 1) & 0xffffffff;
      ctr[1] += high_add;
      if (ctr[1] < high_add)
        overflow = 1;
    }

  ctr[0] += add;
  if (ctr[0] >= add)
    return overflow;

  ctr[1] += 1;
  return (ctr[1] < 1) || overflow;
}


static void
poly1305_fill_bytecounts (gcry_cipher_hd_t c)
{
  u32 lenbuf[4];

  lenbuf[0] = le_bswap32(c->u_mode.poly1305.aadcount[0]);
  lenbuf[1] = le_bswap32(c->u_mode.poly1305.aadcount[1]);
  lenbuf[2] = le_bswap32(c->u_mode.poly1305.datacount[0]);
  lenbuf[3] = le_bswap32(c->u_mode.poly1305.datacount[1]);
  _gcry_poly1305_update (&c->u_mode.poly1305.ctx, (byte*)lenbuf,
			 sizeof(lenbuf));

  wipememory(lenbuf, sizeof(lenbuf));
}


static void
poly1305_do_padding (gcry_cipher_hd_t c, u32 ctr[2])
{
  static const byte zero_padding_buf[15] = {};
  u32 padding_count;

  /* Padding to 16 byte boundary. */
  if (ctr[0] % 16 > 0)
    {
      padding_count = 16 - ctr[0] % 16;

      _gcry_poly1305_update (&c->u_mode.poly1305.ctx, zero_padding_buf,
			     padding_count);
    }
}


static void
poly1305_aad_finish (gcry_cipher_hd_t c)
{
  /* After AAD, feed padding bytes so we get 16 byte alignment. */
  poly1305_do_padding (c, c->u_mode.poly1305.aadcount);

  /* Start of encryption marks end of AAD stream. */
  c->u_mode.poly1305.aad_finalized = 1;

  c->u_mode.poly1305.datacount[0] = 0;
  c->u_mode.poly1305.datacount[1] = 0;
}


static gcry_err_code_t
poly1305_set_zeroiv (gcry_cipher_hd_t c)
{
  byte zero[8] = { 0, };

  return _gcry_cipher_poly1305_setiv (c, zero, sizeof(zero));
}


gcry_err_code_t
_gcry_cipher_poly1305_authenticate (gcry_cipher_hd_t c,
				    const byte * aadbuf, size_t aadbuflen)
{
  if (c->u_mode.poly1305.bytecount_over_limits)
    return GPG_ERR_INV_LENGTH;
  if (c->u_mode.poly1305.aad_finalized)
    return GPG_ERR_INV_STATE;
  if (c->marks.tag)
    return GPG_ERR_INV_STATE;

  if (!c->marks.iv)
    poly1305_set_zeroiv(c);

  if (poly1305_bytecounter_add(c->u_mode.poly1305.aadcount, aadbuflen))
    {
      c->u_mode.poly1305.bytecount_over_limits = 1;
      return GPG_ERR_INV_LENGTH;
    }

  _gcry_poly1305_update (&c->u_mode.poly1305.ctx, aadbuf, aadbuflen);

  return 0;
}


gcry_err_code_t
_gcry_cipher_poly1305_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->u_mode.poly1305.bytecount_over_limits)
    return GPG_ERR_INV_LENGTH;

  if (!c->marks.iv)
    {
      err = poly1305_set_zeroiv(c);
      if (err)
        return err;
    }

  if (!c->u_mode.poly1305.aad_finalized)
    poly1305_aad_finish(c);

  if (poly1305_bytecounter_add(c->u_mode.poly1305.datacount, inbuflen))
    {
      c->u_mode.poly1305.bytecount_over_limits = 1;
      return GPG_ERR_INV_LENGTH;
    }

  c->spec->stencrypt(&c->context.c, outbuf, (byte*)inbuf, inbuflen);

  _gcry_poly1305_update (&c->u_mode.poly1305.ctx, outbuf, inbuflen);

  return 0;
}


gcry_err_code_t
_gcry_cipher_poly1305_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->u_mode.poly1305.bytecount_over_limits)
    return GPG_ERR_INV_LENGTH;

  if (!c->marks.iv)
    {
      err = poly1305_set_zeroiv(c);
      if (err)
        return err;
    }

  if (!c->u_mode.poly1305.aad_finalized)
    poly1305_aad_finish(c);

  if (poly1305_bytecounter_add(c->u_mode.poly1305.datacount, inbuflen))
    {
      c->u_mode.poly1305.bytecount_over_limits = 1;
      return GPG_ERR_INV_LENGTH;
    }

  _gcry_poly1305_update (&c->u_mode.poly1305.ctx, inbuf, inbuflen);

  c->spec->stdecrypt(&c->context.c, outbuf, (byte*)inbuf, inbuflen);
  return 0;
}


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

  if (outbuflen < POLY1305_TAGLEN)
    return GPG_ERR_BUFFER_TOO_SHORT;
  if (c->u_mode.poly1305.bytecount_over_limits)
    return GPG_ERR_INV_LENGTH;

  if (!c->marks.iv)
    {
      err = poly1305_set_zeroiv(c);
      if (err)
        return err;
    }

  if (!c->u_mode.poly1305.aad_finalized)
    poly1305_aad_finish(c);

  if (!c->marks.tag)
    {
      /* After data, feed padding bytes so we get 16 byte alignment. */
      poly1305_do_padding (c, c->u_mode.poly1305.datacount);

      /* Write byte counts to poly1305. */
      poly1305_fill_bytecounts(c);

      _gcry_poly1305_finish(&c->u_mode.poly1305.ctx, c->u_iv.iv);

      c->marks.tag = 1;
    }

  if (check)
    return buf_eq_const(outbuf, c->u_iv.iv, POLY1305_TAGLEN) ?
           GPG_ERR_NO_ERROR : GPG_ERR_CHECKSUM;

  memcpy (outbuf, c->u_iv.iv, POLY1305_TAGLEN);
  return GPG_ERR_NO_ERROR;
}


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

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


void
_gcry_cipher_poly1305_setkey (gcry_cipher_hd_t c)
{
  c->u_mode.poly1305.aadcount[0] = 0;
  c->u_mode.poly1305.aadcount[1] = 0;

  c->u_mode.poly1305.datacount[0] = 0;
  c->u_mode.poly1305.datacount[1] = 0;

  c->u_mode.poly1305.bytecount_over_limits = 0;
  c->u_mode.poly1305.aad_finalized = 0;
  c->marks.tag = 0;
  c->marks.iv = 0;
}


gcry_err_code_t
_gcry_cipher_poly1305_setiv (gcry_cipher_hd_t c, const byte *iv, size_t ivlen)
{
  byte tmpbuf[64]; /* size of ChaCha20 block */
  gcry_err_code_t err;

  /* IV must be 96-bits */
  if (!iv && ivlen != (96 / 8))
    return GPG_ERR_INV_ARG;

  memset(&c->u_mode.poly1305.ctx, 0, sizeof(c->u_mode.poly1305.ctx));

  c->u_mode.poly1305.aadcount[0] = 0;
  c->u_mode.poly1305.aadcount[1] = 0;

  c->u_mode.poly1305.datacount[0] = 0;
  c->u_mode.poly1305.datacount[1] = 0;

  c->u_mode.poly1305.bytecount_over_limits = 0;
  c->u_mode.poly1305.aad_finalized = 0;
  c->marks.tag = 0;
  c->marks.iv = 0;

  /* Set up IV for stream cipher. */
  c->spec->setiv (&c->context.c, iv, ivlen);

  /* Get the first block from ChaCha20. */
  memset(tmpbuf, 0, sizeof(tmpbuf));
  c->spec->stencrypt(&c->context.c, tmpbuf, tmpbuf, sizeof(tmpbuf));

  /* Use the first 32-bytes as Poly1305 key. */
  err = _gcry_poly1305_init (&c->u_mode.poly1305.ctx, tmpbuf, POLY1305_KEYLEN);

  wipememory(tmpbuf, sizeof(tmpbuf));

  if (err)
    return err;

  c->marks.iv = 1;
  return 0;
}