Delete nettle-stdint.h
[gd/nettle] / eax.c
1 /* eax.c
2
3    EAX mode, see http://www.cs.ucdavis.edu/~rogaway/papers/eax.pdf
4
5    Copyright (C) 2013 Niels Möller
6
7    This file is part of GNU Nettle.
8
9    GNU Nettle is free software: you can redistribute it and/or
10    modify it under the terms of either:
11
12      * the GNU Lesser General Public License as published by the Free
13        Software Foundation; either version 3 of the License, or (at your
14        option) any later version.
15
16    or
17
18      * the GNU General Public License as published by the Free
19        Software Foundation; either version 2 of the License, or (at your
20        option) any later version.
21
22    or both in parallel, as here.
23
24    GNU Nettle is distributed in the hope that it will be useful,
25    but WITHOUT ANY WARRANTY; without even the implied warranty of
26    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
27    General Public License for more details.
28
29    You should have received copies of the GNU General Public License and
30    the GNU Lesser General Public License along with this program.  If
31    not, see http://www.gnu.org/licenses/.
32 */
33
34 #if HAVE_CONFIG_H
35 # include "config.h"
36 #endif
37
38 #include <assert.h>
39 #include <string.h>
40
41 #include "eax.h"
42
43 #include "ctr.h"
44 #include "memxor.h"
45
46 static void
47 omac_init (union nettle_block16 *state, unsigned t)
48 {
49   memset (state->b, 0, EAX_BLOCK_SIZE - 1);
50   state->b[EAX_BLOCK_SIZE - 1] = t;
51 }
52
53 /* Almost the same as gcm_gf_add */
54 static void
55 block16_xor (union nettle_block16 *dst, const union nettle_block16 *src)
56 {
57   dst->w[0] ^= src->w[0];
58   dst->w[1] ^= src->w[1];
59 #if SIZEOF_LONG == 4
60   dst->w[2] ^= src->w[2];
61   dst->w[3] ^= src->w[3];
62 #endif
63 }
64
65 static void
66 omac_update (union nettle_block16 *state, const struct eax_key *key,
67              const void *cipher, nettle_cipher_func *f,
68              size_t length, const uint8_t *data)
69 {
70   for (; length >= EAX_BLOCK_SIZE;
71        length -= EAX_BLOCK_SIZE, data += EAX_BLOCK_SIZE)
72     {
73       f (cipher, EAX_BLOCK_SIZE, state->b, state->b);
74       memxor (state->b, data, EAX_BLOCK_SIZE);
75     }
76   if (length > 0)
77     {
78       /* Allowed only for the last call */
79       f (cipher, EAX_BLOCK_SIZE, state->b, state->b);
80       memxor (state->b, data, length);
81       state->b[length] ^= 0x80;
82       /* XOR with (P ^ B), since the digest processing
83        * unconditionally XORs with B */
84       block16_xor (state, &key->pad_partial);
85     }
86 }
87
88 static void
89 omac_final (union nettle_block16 *state, const struct eax_key *key,
90             const void *cipher, nettle_cipher_func *f)
91 {
92   block16_xor (state, &key->pad_block);
93   f (cipher, EAX_BLOCK_SIZE, state->b, state->b);
94 }
95
96 /* Allows r == a */
97 static void
98 gf2_double (uint8_t *r, const uint8_t *a)
99 {
100   unsigned high = - (a[0] >> 7);
101   unsigned i;
102   /* Shift left */
103   for (i = 0; i < EAX_BLOCK_SIZE - 1; i++)
104     r[i] = (a[i] << 1) + (a[i+1] >> 7);
105
106   /* Wrap around for x^{128} = x^7 + x^2 + x + 1 */
107   r[EAX_BLOCK_SIZE - 1] = (a[EAX_BLOCK_SIZE - 1] << 1) ^ (high & 0x87);
108 }
109
110 void
111 eax_set_key (struct eax_key *key, const void *cipher, nettle_cipher_func *f)
112 {
113   static const union nettle_block16 zero_block;
114   f (cipher, EAX_BLOCK_SIZE, key->pad_block.b, zero_block.b);
115   gf2_double (key->pad_block.b, key->pad_block.b);
116   gf2_double (key->pad_partial.b, key->pad_block.b);
117   block16_xor (&key->pad_partial, &key->pad_block);
118 }
119
120 void
121 eax_set_nonce (struct eax_ctx *eax, const struct eax_key *key,
122                const void *cipher, nettle_cipher_func *f,
123                size_t nonce_length, const uint8_t *nonce)
124 {
125   omac_init (&eax->omac_nonce, 0);
126   omac_update (&eax->omac_nonce, key, cipher, f, nonce_length, nonce);
127   omac_final (&eax->omac_nonce, key, cipher, f);
128   memcpy (eax->ctr.b, eax->omac_nonce.b, EAX_BLOCK_SIZE);
129
130   omac_init (&eax->omac_data, 1);
131   omac_init (&eax->omac_message, 2);
132 }
133
134 void
135 eax_update (struct eax_ctx *eax, const struct eax_key *key,
136             const void *cipher, nettle_cipher_func *f,
137             size_t data_length, const uint8_t *data)
138 {
139   omac_update (&eax->omac_data, key, cipher, f, data_length, data);
140 }
141
142 void
143 eax_encrypt (struct eax_ctx *eax, const struct eax_key *key,
144              const void *cipher, nettle_cipher_func *f,
145              size_t length, uint8_t *dst, const uint8_t *src)
146 {
147   ctr_crypt (cipher, f, EAX_BLOCK_SIZE, eax->ctr.b, length, dst, src);
148   omac_update (&eax->omac_message, key, cipher, f, length, dst);
149 }
150
151 void
152 eax_decrypt (struct eax_ctx *eax, const struct eax_key *key,
153              const void *cipher, nettle_cipher_func *f,
154              size_t length, uint8_t *dst, const uint8_t *src)
155 {
156   omac_update (&eax->omac_message, key, cipher, f, length, src);
157   ctr_crypt (cipher, f, EAX_BLOCK_SIZE, eax->ctr.b, length, dst, src);
158 }
159
160 void
161 eax_digest (struct eax_ctx *eax, const struct eax_key *key,
162             const void *cipher, nettle_cipher_func *f,
163             size_t length, uint8_t *digest)
164 {
165   assert (length > 0);
166   assert (length <= EAX_BLOCK_SIZE);
167   omac_final (&eax->omac_data, key, cipher, f);
168   omac_final (&eax->omac_message, key, cipher, f);
169
170   block16_xor (&eax->omac_nonce, &eax->omac_data);
171   memxor3 (digest, eax->omac_nonce.b, eax->omac_message.b, length);
172 }