poly1305.h 3.62 KB
Newer Older
1
/* poly1305.h
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

   Poly1305 message authentication code.

   Copyright (C) 2013 Nikos Mavrogiannopoulos
   Copyright (C) 2013, 2014 Niels Möller

   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/.
*/
34 35 36 37

#ifndef NETTLE_POLY1305_H_INCLUDED
#define NETTLE_POLY1305_H_INCLUDED

38
#include "aes.h"
Niels Möller's avatar
Niels Möller committed
39

40 41 42 43
#ifdef __cplusplus
extern "C" {
#endif

Niels Möller's avatar
Niels Möller committed
44 45 46
/* Name mangling */
#define poly1305_set_key nettle_poly1305_set_key
#define poly1305_digest nettle_poly1305_digest
47
#define _poly1305_block _nettle_poly1305_block
48

49 50
#define poly1305_aes_set_key nettle_poly1305_aes_set_key
#define poly1305_aes_set_nonce nettle_poly1305_aes_set_nonce
51
#define poly1305_aes_update nettle_poly1305_aes_update
52 53
#define poly1305_aes_digest nettle_poly1305_aes_digest

Niels Möller's avatar
Niels Möller committed
54
/* Low level functions/macros for the poly1305 construction. */
55

56 57 58 59
#define POLY1305_DIGEST_SIZE 16
#define POLY1305_BLOCK_SIZE 16
#define POLY1305_KEY_SIZE 16

60
struct poly1305_ctx {
61 62 63 64 65 66 67 68 69
  /* Key, 128-bit value and some cached multiples. */
  union
  {
    uint32_t r32[6];
    uint64_t r64[3];
  } r;
  uint32_t s32[3];
  /* State, represented as words of 26, 32 or 64 bits, depending on
     implementation. */
70
  /* High bits first, to maintain alignment. */
71 72 73 74 75 76
  uint32_t hh;
  union
  {
    uint32_t h32[4];
    uint64_t h64[2];
  } h;
77 78
};

79
/* Low-level internal interface. */
80
void poly1305_set_key(struct poly1305_ctx *ctx, const uint8_t key[POLY1305_KEY_SIZE]);
81
/* Extracts digest, and adds it to s, the encrypted nonce. */
82
void poly1305_digest (struct poly1305_ctx *ctx, union nettle_block16 *s);
83
/* Internal function. Process one block. */
84
void _poly1305_block (struct poly1305_ctx *ctx, const uint8_t *m,
85
		      unsigned high);
86

87 88 89 90
/* poly1305-aes */

#define POLY1305_AES_KEY_SIZE 32
#define POLY1305_AES_DIGEST_SIZE 16
91
#define POLY1305_AES_NONCE_SIZE 16
92 93 94

struct poly1305_aes_ctx
{
95 96
  /* Keep aes context last, to make it possible to use a general
     poly1305_update if other variants are added. */
97
  struct poly1305_ctx pctx;
98 99
  uint8_t block[POLY1305_BLOCK_SIZE];
  unsigned index;
100
  uint8_t nonce[POLY1305_BLOCK_SIZE];
101 102 103 104 105 106 107
  struct aes128_ctx aes;
};

/* Also initialize the nonce to zero. */
void
poly1305_aes_set_key (struct poly1305_aes_ctx *ctx, const uint8_t *key);

108 109
/* Optional, if not used, messages get incrementing nonces starting
   from zero. */
110 111 112 113
void
poly1305_aes_set_nonce (struct poly1305_aes_ctx *ctx,
		        const uint8_t *nonce);

114 115 116 117
/* Update is not aes-specific, but since this is the only implemented
   variant, we need no more general poly1305_update. */
void
poly1305_aes_update (struct poly1305_aes_ctx *ctx, size_t length, const uint8_t *data);
118

119 120 121 122
/* Also increments the nonce */
void
poly1305_aes_digest (struct poly1305_aes_ctx *ctx,
	       	     size_t length, uint8_t *digest);
123 124 125 126 127 128

#ifdef __cplusplus
}
#endif

#endif /* NETTLE_POLY1305_H_INCLUDED */