eadc4057fe89c0f7fffc3961057002b8ce02057d
[platform/upstream/nettle.git] / poly1305.h
1 /* poly1305.h
2
3    Poly1305 message authentication code.
4
5    Copyright (C) 2013 Nikos Mavrogiannopoulos
6    Copyright (C) 2013, 2014 Niels Möller
7
8    This file is part of GNU Nettle.
9
10    GNU Nettle is free software: you can redistribute it and/or
11    modify it under the terms of either:
12
13      * the GNU Lesser General Public License as published by the Free
14        Software Foundation; either version 3 of the License, or (at your
15        option) any later version.
16
17    or
18
19      * the GNU General Public License as published by the Free
20        Software Foundation; either version 2 of the License, or (at your
21        option) any later version.
22
23    or both in parallel, as here.
24
25    GNU Nettle is distributed in the hope that it will be useful,
26    but WITHOUT ANY WARRANTY; without even the implied warranty of
27    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
28    General Public License for more details.
29
30    You should have received copies of the GNU General Public License and
31    the GNU Lesser General Public License along with this program.  If
32    not, see http://www.gnu.org/licenses/.
33 */
34
35 #ifndef NETTLE_POLY1305_H_INCLUDED
36 #define NETTLE_POLY1305_H_INCLUDED
37
38 #include "aes.h"
39
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43
44 /* Name mangling */
45 #define poly1305_set_key nettle_poly1305_set_key
46 #define poly1305_digest nettle_poly1305_digest
47 #define _poly1305_block _nettle_poly1305_block
48
49 #define poly1305_aes_set_key nettle_poly1305_aes_set_key
50 #define poly1305_aes_set_nonce nettle_poly1305_aes_set_nonce
51 #define poly1305_aes_update nettle_poly1305_aes_update
52 #define poly1305_aes_digest nettle_poly1305_aes_digest
53
54 /* Low level functions/macros for the poly1305 construction. */
55
56 #define POLY1305_DIGEST_SIZE 16
57 #define POLY1305_BLOCK_SIZE 16
58 #define POLY1305_KEY_SIZE 16
59
60 struct poly1305_ctx {
61   /* Key, 128-bit value and some cached multiples. */
62   union
63   {
64     uint32_t r32[6];
65     uint64_t r64[3];
66   } r;
67   uint32_t s32[3];
68   /* State, represented as words of 26, 32 or 64 bits, depending on
69      implementation. */
70   /* High bits first, to maintain alignment. */
71   uint32_t hh;
72   union
73   {
74     uint32_t h32[4];
75     uint64_t h64[2];
76   } 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 /* poly1305-aes */
88
89 #define POLY1305_AES_KEY_SIZE 32
90 #define POLY1305_AES_DIGEST_SIZE 16
91 #define POLY1305_AES_NONCE_SIZE 16
92
93 struct poly1305_aes_ctx
94 {
95   /* Keep aes context last, to make it possible to use a general
96      poly1305_update if other variants are added. */
97   struct poly1305_ctx pctx;
98   uint8_t block[POLY1305_BLOCK_SIZE];
99   unsigned index;
100   uint8_t nonce[POLY1305_BLOCK_SIZE];
101   struct aes128_ctx aes;
102 };
103
104 /* Also initialize the nonce to zero. */
105 void
106 poly1305_aes_set_key (struct poly1305_aes_ctx *ctx, const uint8_t *key);
107
108 /* Optional, if not used, messages get incrementing nonces starting
109    from zero. */
110 void
111 poly1305_aes_set_nonce (struct poly1305_aes_ctx *ctx,
112                         const uint8_t *nonce);
113
114 /* Update is not aes-specific, but since this is the only implemented
115    variant, we need no more general poly1305_update. */
116 void
117 poly1305_aes_update (struct poly1305_aes_ctx *ctx, size_t length, const uint8_t *data);
118
119 /* Also increments the nonce */
120 void
121 poly1305_aes_digest (struct poly1305_aes_ctx *ctx,
122                      size_t length, uint8_t *digest);
123
124 #ifdef __cplusplus
125 }
126 #endif
127
128 #endif /* NETTLE_POLY1305_H_INCLUDED */