/src/nettle-with-mini-gmp/eax.c
Line  | Count  | Source  | 
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 "block-internal.h"  | 
44  |  | #include "ctr.h"  | 
45  |  | #include "memxor.h"  | 
46  |  |  | 
47  |  | static void  | 
48  |  | omac_init (union nettle_block16 *state, unsigned t)  | 
49  | 2.82k  | { | 
50  | 2.82k  |   memset (state->b, 0, EAX_BLOCK_SIZE - 1);  | 
51  | 2.82k  |   state->b[EAX_BLOCK_SIZE - 1] = t;  | 
52  | 2.82k  | }  | 
53  |  |  | 
54  |  | static void  | 
55  |  | omac_update (union nettle_block16 *state, const struct eax_key *key,  | 
56  |  |        const void *cipher, nettle_cipher_func *f,  | 
57  |  |        size_t length, const uint8_t *data)  | 
58  | 14.8k  | { | 
59  | 32.7k  |   for (; length >= EAX_BLOCK_SIZE;  | 
60  | 17.9k  |        length -= EAX_BLOCK_SIZE, data += EAX_BLOCK_SIZE)  | 
61  | 17.9k  |     { | 
62  | 17.9k  |       f (cipher, EAX_BLOCK_SIZE, state->b, state->b);  | 
63  | 17.9k  |       memxor (state->b, data, EAX_BLOCK_SIZE);  | 
64  | 17.9k  |     }  | 
65  | 14.8k  |   if (length > 0)  | 
66  | 1.69k  |     { | 
67  |  |       /* Allowed only for the last call */  | 
68  | 1.69k  |       f (cipher, EAX_BLOCK_SIZE, state->b, state->b);  | 
69  | 1.69k  |       memxor (state->b, data, length);  | 
70  | 1.69k  |       state->b[length] ^= 0x80;  | 
71  |  |       /* XOR with (P ^ B), since the digest processing  | 
72  |  |        * unconditionally XORs with B */  | 
73  | 1.69k  |       block16_xor (state, &key->pad_partial);  | 
74  | 1.69k  |     }  | 
75  | 14.8k  | }  | 
76  |  |  | 
77  |  | static void  | 
78  |  | omac_final (union nettle_block16 *state, const struct eax_key *key,  | 
79  |  |       const void *cipher, nettle_cipher_func *f)  | 
80  | 2.82k  | { | 
81  | 2.82k  |   block16_xor (state, &key->pad_block);  | 
82  | 2.82k  |   f (cipher, EAX_BLOCK_SIZE, state->b, state->b);  | 
83  | 2.82k  | }  | 
84  |  |  | 
85  |  | void  | 
86  |  | eax_set_key (struct eax_key *key, const void *cipher, nettle_cipher_func *f)  | 
87  | 942  | { | 
88  | 942  |   static const union nettle_block16 zero_block;  | 
89  | 942  |   f (cipher, EAX_BLOCK_SIZE, key->pad_block.b, zero_block.b);  | 
90  | 942  |   block16_mulx_be (&key->pad_block, &key->pad_block);  | 
91  | 942  |   block16_mulx_be (&key->pad_partial, &key->pad_block);  | 
92  | 942  |   block16_xor (&key->pad_partial, &key->pad_block);  | 
93  | 942  | }  | 
94  |  |  | 
95  |  | void  | 
96  |  | eax_set_nonce (struct eax_ctx *eax, const struct eax_key *key,  | 
97  |  |          const void *cipher, nettle_cipher_func *f,  | 
98  |  |          size_t nonce_length, const uint8_t *nonce)  | 
99  | 942  | { | 
100  | 942  |   omac_init (&eax->omac_nonce, 0);  | 
101  | 942  |   omac_update (&eax->omac_nonce, key, cipher, f, nonce_length, nonce);  | 
102  | 942  |   omac_final (&eax->omac_nonce, key, cipher, f);  | 
103  | 942  |   memcpy (eax->ctr.b, eax->omac_nonce.b, EAX_BLOCK_SIZE);  | 
104  |  |  | 
105  | 942  |   omac_init (&eax->omac_data, 1);  | 
106  | 942  |   omac_init (&eax->omac_message, 2);  | 
107  | 942  | }  | 
108  |  |  | 
109  |  | void  | 
110  |  | eax_update (struct eax_ctx *eax, const struct eax_key *key,  | 
111  |  |       const void *cipher, nettle_cipher_func *f,  | 
112  |  |       size_t data_length, const uint8_t *data)  | 
113  | 4.98k  | { | 
114  | 4.98k  |   omac_update (&eax->omac_data, key, cipher, f, data_length, data);  | 
115  | 4.98k  | }  | 
116  |  |  | 
117  |  | void  | 
118  |  | eax_encrypt (struct eax_ctx *eax, const struct eax_key *key,  | 
119  |  |        const void *cipher, nettle_cipher_func *f,  | 
120  |  |        size_t length, uint8_t *dst, const uint8_t *src)  | 
121  | 4.45k  | { | 
122  | 4.45k  |   ctr_crypt (cipher, f, EAX_BLOCK_SIZE, eax->ctr.b, length, dst, src);  | 
123  | 4.45k  |   omac_update (&eax->omac_message, key, cipher, f, length, dst);  | 
124  | 4.45k  | }  | 
125  |  |  | 
126  |  | void  | 
127  |  | eax_decrypt (struct eax_ctx *eax, const struct eax_key *key,  | 
128  |  |        const void *cipher, nettle_cipher_func *f,  | 
129  |  |        size_t length, uint8_t *dst, const uint8_t *src)  | 
130  | 4.43k  | { | 
131  | 4.43k  |   omac_update (&eax->omac_message, key, cipher, f, length, src);  | 
132  | 4.43k  |   ctr_crypt (cipher, f, EAX_BLOCK_SIZE, eax->ctr.b, length, dst, src);  | 
133  | 4.43k  | }  | 
134  |  |  | 
135  |  | void  | 
136  |  | eax_digest (struct eax_ctx *eax, const struct eax_key *key,  | 
137  |  |       const void *cipher, nettle_cipher_func *f,  | 
138  |  |       size_t length, uint8_t *digest)  | 
139  | 942  | { | 
140  | 942  |   assert (length > 0);  | 
141  | 942  |   assert (length <= EAX_BLOCK_SIZE);  | 
142  | 942  |   omac_final (&eax->omac_data, key, cipher, f);  | 
143  | 942  |   omac_final (&eax->omac_message, key, cipher, f);  | 
144  |  |  | 
145  | 942  |   block16_xor (&eax->omac_nonce, &eax->omac_data);  | 
146  | 942  |   memxor3 (digest, eax->omac_nonce.b, eax->omac_message.b, length);  | 
147  | 942  | }  |