chacha-poly1305.c 4.12 KB
Newer Older
1
/* chacha-poly1305.c
2 3 4

   AEAD mechanism based on chacha and poly1305.

5
   Copyright (C) 2014, 2015 Niels Möller
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

   This file is part of GNU Nettle.

   GNU Nettle is free software: you can redistribute it and/or
   modify it under the terms of either:

     * the GNU Lesser General Public License as published by the Free
       Software Foundation; either version 3 of the License, or (at your
       option) any later version.

   or

     * the GNU General Public License as published by the Free
       Software Foundation; either version 2 of the License, or (at your
       option) any later version.

   or both in parallel, as here.

   GNU Nettle 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
   General Public License for more details.

   You should have received copies of the GNU General Public License and
   the GNU Lesser General Public License along with this program.  If
   not, see http://www.gnu.org/licenses/.
*/
Niels Möller's avatar
Niels Möller committed
33

34 35 36 37 38 39 40 41 42 43 44 45 46 47
/* This implements chacha-poly1305 according to
   draft-irtf-cfrg-chacha20-poly1305-08. The inputs to poly1305 are:

     associated data
     zero padding
     ciphertext
     zero padding
     length of associated data (64-bit, little endian)
     length of ciphertext (64-bit, little endian)

   where the padding fields are 0-15 zero bytes, filling up to a
   16-byte boundary.
*/

Niels Möller's avatar
Niels Möller committed
48 49 50 51 52 53 54
#if HAVE_CONFIG_H
# include "config.h"
#endif

#include <assert.h>
#include <string.h>

55
#include "chacha-internal.h"
Niels Möller's avatar
Niels Möller committed
56 57 58 59 60 61
#include "chacha-poly1305.h"

#include "macros.h"

#define CHACHA_ROUNDS 20

62 63
/* FIXME: Also set nonce to zero, and implement nonce
   auto-increment? */
Niels Möller's avatar
Niels Möller committed
64 65 66 67
void
chacha_poly1305_set_key (struct chacha_poly1305_ctx *ctx,
			 const uint8_t *key)
{
68
  chacha_set_key (&ctx->chacha, key);
Niels Möller's avatar
Niels Möller committed
69 70 71 72 73 74 75 76 77 78 79
}

void
chacha_poly1305_set_nonce (struct chacha_poly1305_ctx *ctx,
			   const uint8_t *nonce)
{
  union {
    uint32_t x[_CHACHA_STATE_LENGTH];
    uint8_t subkey[32];
  } u;

80
  chacha_set_nonce96 (&ctx->chacha, nonce);
Niels Möller's avatar
Niels Möller committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
  /* Generate authentication key */
  _chacha_core (u.x, ctx->chacha.state, CHACHA_ROUNDS);
  poly1305_set_key (&ctx->poly1305, u.subkey);  
  /* For final poly1305 processing */
  memcpy (ctx->s.b, u.subkey + 16, 16);
  /* Increment block count */
  ctx->chacha.state[12] = 1;

  ctx->auth_size = ctx->data_size = ctx->index = 0;
}

/* FIXME: Duplicated in poly1305-aes128.c */
#define COMPRESS(ctx, data) _poly1305_block(&(ctx)->poly1305, (data), 1)

static void
poly1305_update (struct chacha_poly1305_ctx *ctx,
		 size_t length, const uint8_t *data)
{
  MD_UPDATE (ctx, length, data, COMPRESS, (void) 0);
}

102 103 104 105 106 107 108 109 110 111 112
static void
poly1305_pad (struct chacha_poly1305_ctx *ctx)
{
  if (ctx->index)
    {
      memset (ctx->block + ctx->index, 0,
	      POLY1305_BLOCK_SIZE - ctx->index);
      _poly1305_block(&ctx->poly1305, ctx->block, 1);
      ctx->index = 0;
    }
}
Niels Möller's avatar
Niels Möller committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
void
chacha_poly1305_update (struct chacha_poly1305_ctx *ctx,
			size_t length, const uint8_t *data)
{
  assert (ctx->data_size == 0);  
  poly1305_update (ctx, length, data);
  ctx->auth_size += length;
}


void
chacha_poly1305_encrypt (struct chacha_poly1305_ctx *ctx,
			 size_t length, uint8_t *dst, const uint8_t *src)
{
  if (!length)
    return;

  assert (ctx->data_size % CHACHA_POLY1305_BLOCK_SIZE == 0);
131 132
  poly1305_pad (ctx);

Niels Möller's avatar
Niels Möller committed
133 134 135 136 137 138 139 140 141 142 143 144 145
  chacha_crypt (&ctx->chacha, length, dst, src);
  poly1305_update (ctx, length, dst);
  ctx->data_size += length;
}
			 
void
chacha_poly1305_decrypt (struct chacha_poly1305_ctx *ctx,
			 size_t length, uint8_t *dst, const uint8_t *src)
{
  if (!length)
    return;

  assert (ctx->data_size % CHACHA_POLY1305_BLOCK_SIZE == 0);
146 147
  poly1305_pad (ctx);

Niels Möller's avatar
Niels Möller committed
148 149 150 151 152 153 154 155 156
  poly1305_update (ctx, length, src);
  chacha_crypt (&ctx->chacha, length, dst, src);
  ctx->data_size += length;
}
			 
void
chacha_poly1305_digest (struct chacha_poly1305_ctx *ctx,
			size_t length, uint8_t *digest)
{
157
  uint8_t buf[16];
Niels Möller's avatar
Niels Möller committed
158

159 160 161
  poly1305_pad (ctx);
  LE_WRITE_UINT64 (buf, ctx->auth_size);
  LE_WRITE_UINT64 (buf + 8, ctx->data_size);
Niels Möller's avatar
Niels Möller committed
162

163
  _poly1305_block (&ctx->poly1305, buf, 1);
Niels Möller's avatar
Niels Möller committed
164 165 166 167

  poly1305_digest (&ctx->poly1305, &ctx->s);
  memcpy (digest, &ctx->s.b, length);
}