eax.h 5.62 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
/* eax.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

   EAX mode, see http://www.cs.ucdavis.edu/~rogaway/papers/eax.pdf

   Copyright (C) 2013 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/.
*/
Niels Möller's avatar
Niels Möller committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

#ifndef NETTLE_EAX_H_INCLUDED
#define NETTLE_EAX_H_INCLUDED

#include "aes.h"

#ifdef __cplusplus
extern "C" {
#endif

/* Name mangling */
#define eax_set_key nettle_eax_set_key
#define eax_set_nonce nettle_eax_set_nonce
#define eax_update nettle_eax_update
#define eax_encrypt nettle_eax_encrypt
#define eax_decrypt nettle_eax_decrypt
#define eax_digest nettle_eax_digest

51 52 53 54 55 56
#define eax_aes128_set_key nettle_eax_aes128_set_key
#define eax_aes128_set_nonce nettle_eax_aes128_set_nonce
#define eax_aes128_update nettle_eax_aes128_update
#define eax_aes128_encrypt nettle_eax_aes128_encrypt
#define eax_aes128_decrypt nettle_eax_aes128_decrypt
#define eax_aes128_digest nettle_eax_aes128_digest
Niels Möller's avatar
Niels Möller committed
57 58 59 60 61

/* Restricted to block ciphers with 128 bit block size. FIXME: Reflect
   this in naming? */

#define EAX_BLOCK_SIZE 16
62
#define EAX_DIGEST_SIZE 16
63 64
/* FIXME: Reasonable default? */
#define EAX_IV_SIZE 16
Niels Möller's avatar
Niels Möller committed
65 66 67 68

/* Values independent of message and nonce */
struct eax_key
{
69 70
  union nettle_block16 pad_block;
  union nettle_block16 pad_partial;
Niels Möller's avatar
Niels Möller committed
71 72 73 74
};

struct eax_ctx
{
75 76 77 78
  union nettle_block16 omac_nonce;
  union nettle_block16 omac_data;
  union nettle_block16 omac_message;
  union nettle_block16 ctr;
Niels Möller's avatar
Niels Möller committed
79 80 81
};

void
82
eax_set_key (struct eax_key *key, const void *cipher, nettle_cipher_func *f);
Niels Möller's avatar
Niels Möller committed
83 84 85

void
eax_set_nonce (struct eax_ctx *eax, const struct eax_key *key,
86
	       const void *cipher, nettle_cipher_func *f,
Niels Möller's avatar
Niels Möller committed
87 88 89 90
	       size_t nonce_length, const uint8_t *nonce);

void
eax_update (struct eax_ctx *eax, const struct eax_key *key,
91
	    const void *cipher, nettle_cipher_func *f,
Niels Möller's avatar
Niels Möller committed
92 93 94 95
	    size_t data_length, const uint8_t *data);

void
eax_encrypt (struct eax_ctx *eax, const struct eax_key *key,
96
	     const void *cipher, nettle_cipher_func *f,
Niels Möller's avatar
Niels Möller committed
97 98 99 100
	     size_t length, uint8_t *dst, const uint8_t *src);

void
eax_decrypt (struct eax_ctx *eax, const struct eax_key *key,
101
	     const void *cipher, nettle_cipher_func *f,
Niels Möller's avatar
Niels Möller committed
102 103 104 105
	     size_t length, uint8_t *dst, const uint8_t *src);

void
eax_digest (struct eax_ctx *eax, const struct eax_key *key,
Niels Möller's avatar
Niels Möller committed
106
	    const void *cipher, nettle_cipher_func *f,
Niels Möller's avatar
Niels Möller committed
107 108 109 110 111 112 113
	    size_t length, uint8_t *digest);

/* Put the cipher last, to get cipher-independent offsets for the EAX
 * state. */
#define EAX_CTX(type) \
  { struct eax_key key; struct eax_ctx eax; type cipher; }

114
#define EAX_SET_KEY(ctx, set_key, encrypt, data)			\
Niels Möller's avatar
Niels Möller committed
115
  do {									\
116
    (set_key)(&(ctx)->cipher, (data));					\
117 118
    if (0) (encrypt) (&(ctx)->cipher, ~(size_t) 0,			\
		      (uint8_t *) 0, (const uint8_t *) 0);		\
119
    eax_set_key (&(ctx)->key, &(ctx)->cipher, (nettle_cipher_func *) encrypt); \
Niels Möller's avatar
Niels Möller committed
120 121
  } while (0)

122 123 124
#define EAX_SET_NONCE(ctx, encrypt, length, nonce)			\
  (0 ? (encrypt) (&(ctx)->cipher, ~(size_t) 0,				\
		  (uint8_t *) 0, (const uint8_t *) 0)			\
Niels Möller's avatar
Niels Möller committed
125
   : eax_set_nonce (&(ctx)->eax, &(ctx)->key,			\
126
		    &(ctx)->cipher, (nettle_cipher_func *) (encrypt),	\
Niels Möller's avatar
Niels Möller committed
127 128 129
		    (length), (nonce)))

#define EAX_UPDATE(ctx, encrypt, length, data)				\
130 131
  (0 ? (encrypt) (&(ctx)->cipher, ~(size_t) 0,				\
		  (uint8_t *) 0, (const uint8_t *) 0)			\
Niels Möller's avatar
Niels Möller committed
132
   : eax_update (&(ctx)->eax, &(ctx)->key,				\
133
		 &(ctx)->cipher, (nettle_cipher_func *) (encrypt),	\
Niels Möller's avatar
Niels Möller committed
134 135 136
		 (length), (data)))

#define EAX_ENCRYPT(ctx, encrypt, length, dst, src)			\
137 138
  (0 ? (encrypt) (&(ctx)->cipher, ~(size_t) 0,				\
		  (uint8_t *) 0, (const uint8_t *) 0)			\
Niels Möller's avatar
Niels Möller committed
139
   : eax_encrypt (&(ctx)->eax, &(ctx)->key,				\
140
		 &(ctx)->cipher, (nettle_cipher_func *) (encrypt),	\
Niels Möller's avatar
Niels Möller committed
141 142 143
		  (length), (dst), (src)))

#define EAX_DECRYPT(ctx, encrypt, length, dst, src)			\
144 145
  (0 ? (encrypt) (&(ctx)->cipher, ~(size_t) 0,				\
		  (uint8_t *) 0, (const uint8_t *) 0)			\
Niels Möller's avatar
Niels Möller committed
146
   : eax_decrypt (&(ctx)->eax, &(ctx)->key,				\
147
		 &(ctx)->cipher, (nettle_cipher_func *) (encrypt),	\
Niels Möller's avatar
Niels Möller committed
148 149 150
		  (length), (dst), (src)))

#define EAX_DIGEST(ctx, encrypt, length, digest)			\
151 152
  (0 ? (encrypt) (&(ctx)->cipher, ~(size_t) 0,				\
		  (uint8_t *) 0, (const uint8_t *) 0)			\
Niels Möller's avatar
Niels Möller committed
153
   : eax_digest (&(ctx)->eax, &(ctx)->key,				\
154
		 &(ctx)->cipher, (nettle_cipher_func *) (encrypt),	\
Niels Möller's avatar
Niels Möller committed
155 156
		 (length), (digest)))

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
struct eax_aes128_ctx EAX_CTX(struct aes128_ctx);

void
eax_aes128_set_key(struct eax_aes128_ctx *ctx, const uint8_t *key);

void
eax_aes128_set_nonce(struct eax_aes128_ctx *ctx,
		     size_t length, const uint8_t *iv);

void
eax_aes128_update(struct eax_aes128_ctx *ctx,
		  size_t length, const uint8_t *data);

void
eax_aes128_encrypt(struct eax_aes128_ctx *ctx,
		   size_t length, uint8_t *dst, const uint8_t *src);

void
eax_aes128_decrypt(struct eax_aes128_ctx *ctx,
		   size_t length, uint8_t *dst, const uint8_t *src);

void
eax_aes128_digest(struct eax_aes128_ctx *ctx, size_t length, uint8_t *digest);

Niels Möller's avatar
Niels Möller committed
181 182 183 184 185
#ifdef __cplusplus
}
#endif

#endif /* NETTLE_EAX_H_INCLUDED */