3 EAX mode, see http://www.cs.ucdavis.edu/~rogaway/papers/eax.pdf
5 Copyright (C) 2013 Niels Möller
7 This file is part of GNU Nettle.
9 GNU Nettle is free software: you can redistribute it and/or
10 modify it under the terms of either:
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.
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.
22 or both in parallel, as here.
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.
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/.
47 omac_init (union nettle_block16 *state, unsigned t)
49 memset (state->b, 0, EAX_BLOCK_SIZE - 1);
50 state->b[EAX_BLOCK_SIZE - 1] = t;
53 /* Almost the same as gcm_gf_add */
55 block16_xor (union nettle_block16 *dst, const union nettle_block16 *src)
57 dst->w[0] ^= src->w[0];
58 dst->w[1] ^= src->w[1];
60 dst->w[2] ^= src->w[2];
61 dst->w[3] ^= src->w[3];
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)
70 for (; length >= EAX_BLOCK_SIZE;
71 length -= EAX_BLOCK_SIZE, data += EAX_BLOCK_SIZE)
73 f (cipher, EAX_BLOCK_SIZE, state->b, state->b);
74 memxor (state->b, data, EAX_BLOCK_SIZE);
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);
89 omac_final (union nettle_block16 *state, const struct eax_key *key,
90 const void *cipher, nettle_cipher_func *f)
92 block16_xor (state, &key->pad_block);
93 f (cipher, EAX_BLOCK_SIZE, state->b, state->b);
98 gf2_double (uint8_t *r, const uint8_t *a)
100 unsigned high = - (a[0] >> 7);
103 for (i = 0; i < EAX_BLOCK_SIZE - 1; i++)
104 r[i] = (a[i] << 1) + (a[i+1] >> 7);
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);
111 eax_set_key (struct eax_key *key, const void *cipher, nettle_cipher_func *f)
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);
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)
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);
130 omac_init (&eax->omac_data, 1);
131 omac_init (&eax->omac_message, 2);
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)
139 omac_update (&eax->omac_data, key, cipher, f, data_length, data);
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)
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);
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)
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);
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)
166 assert (length <= EAX_BLOCK_SIZE);
167 omac_final (&eax->omac_data, key, cipher, f);
168 omac_final (&eax->omac_message, key, cipher, f);
170 block16_xor (&eax->omac_nonce, &eax->omac_data);
171 memxor3 (digest, eax->omac_nonce.b, eax->omac_message.b, length);