gcm.h 4.38 KB
Newer Older
Nikos Mavrogiannopoulos's avatar
Nikos Mavrogiannopoulos committed
1
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/* gcm.h
 *
 * Galois counter mode, specified by NIST,
 * http://csrc.nist.gov/publications/nistpubs/800-38D/SP-800-38D.pdf
 *
 */

/* NOTE: Tentative interface, subject to change. No effort will be
   made to avoid incompatible changes. */

/* nettle, low-level cryptographics library
 *
 * Copyright (C) 2011 Niels Möller
 * Copyright (C) 2011 Katholieke Universiteit Leuven
 * 
 * Contributed by Nikos Mavrogiannopoulos
 *
 * The nettle library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * The nettle library 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 Lesser General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with the nettle library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA.
 */

#ifndef NETTLE_GCM_H_INCLUDED
#define NETTLE_GCM_H_INCLUDED

#include "nettle-types.h"

#ifdef __cplusplus
extern "C" {
#endif

/* Name mangling */
#define gcm_set_key nettle_gcm_set_key
#define gcm_set_iv nettle_gcm_set_iv
#define gcm_auth nettle_gcm_auth
#define gcm_encrypt nettle_gcm_encrypt
#define gcm_decrypt nettle_gcm_decrypt
#define gcm_digest nettle_gcm_digest

#define GCM_BLOCK_SIZE 16
#define GCM_IV_SIZE (GCM_BLOCK_SIZE - 4)

Niels Möller's avatar
Niels Möller committed
54
#define GCM_TABLE_BITS 4
Nikos Mavrogiannopoulos's avatar
Nikos Mavrogiannopoulos committed
55

56
57
58
59
60
61
62
/* To make sure that we have proper alignment. */
union gcm_block
{
  uint8_t b[GCM_BLOCK_SIZE];
  unsigned long w[1];
};

Nikos Mavrogiannopoulos's avatar
Nikos Mavrogiannopoulos committed
63
64
65
struct gcm_ctx {
  /* Key-dependent state. */
  /* Hashing subkey */
66
  union gcm_block h[1 << GCM_TABLE_BITS];
Nikos Mavrogiannopoulos's avatar
Nikos Mavrogiannopoulos committed
67
68
  /* Per-message state, depending on the iv */
  /* Original counter block */
69
  union gcm_block iv;
Nikos Mavrogiannopoulos's avatar
Nikos Mavrogiannopoulos committed
70
  /* Updated for each block. */
71
  union gcm_block ctr;
Nikos Mavrogiannopoulos's avatar
Nikos Mavrogiannopoulos committed
72
  /* Hashing state */
73
  union gcm_block x;
Nikos Mavrogiannopoulos's avatar
Nikos Mavrogiannopoulos committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
  uint64_t auth_size;
  uint64_t data_size;
};

/* FIXME: Should use const for the cipher context. Then needs const for
   nettle_crypt_func, which also rules out using that abstraction for
   arcfour. */
void
gcm_set_key(struct gcm_ctx *ctx,
	    void *cipher, nettle_crypt_func *f);

void
gcm_set_iv(struct gcm_ctx *ctx, unsigned length, const uint8_t *iv);

void
gcm_auth(struct gcm_ctx *ctx, unsigned length, const uint8_t *data);

void
gcm_encrypt(struct gcm_ctx *ctx, void *cipher, nettle_crypt_func *f,
	    unsigned length, uint8_t *dst, const uint8_t *src);

void
gcm_decrypt(struct gcm_ctx *ctx, void *cipher, nettle_crypt_func *f,
	    unsigned length, uint8_t *dst, const uint8_t *src);

void
gcm_digest(struct gcm_ctx *ctx, void *cipher, nettle_crypt_func *f,
	   unsigned length, uint8_t *digest);

#if 0
104
/* FIXME: Is this macrology useful? */
Nikos Mavrogiannopoulos's avatar
Nikos Mavrogiannopoulos committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#define GCM_KEY(type) \
{ type cipher; struct gcm_ctx gcm; }

#define GCM_SET_KEY(ctx, set_key, encrypt, length, data)	\
  do {								\
    (set_key)(&(ctx)->cipher, (length), (data));		\
    gcm_set_key(&(ctx)->gcm, &(ctx)->cipher, (encrypt));	\
  } while (0)

#define GCM_AUTH(ctx, encrypt, length, data)	    \
  gcm_auth((ctx)->gcm, &(ctx)->cipher, (encrypt),   \
	   (length), (data))

#define GCM_ENCRYPT(ctx, encrypt, length, dst, src)       \
  gcm_encrypt((ctx)->gcm, &(ctx)->cipher, (encrypt),	  \
	      (length), (dst), (src))

#define GCM_DECRYPT(ctx, key, encrypt, length, dst, src)       \
  gcm_decrypt((ctx)->gcm, &(ctx)->cipher, (encrypt),	       \
	      (length), (dst), (src))

#define GCM_DIGEST(ctx, key, encrypt, length, digest)		\
  gcm_digest((ctx)->gcm, &(ctx)->cipher, (encrypt),		\
	     (length), (digest))

struct gcm_aes_ctx GCM_CTX(struct aes_ctx);

void
gcm_aes_set_key(struct gcm_aes_ctx *ctx,
		unsigned length, const uint8_t *key);

void
gcm_aes_set_iv(struct gcm_aes_ctx *ctx,
	       unsigned length, const uint8_t *iv);

void
gcm_aes_auth(struct gcm_aes_ctx *ctx,
	     unsigned length, const uint8_t *data);

void
gcm_aes_encrypt(struct gcm_aes_ctx *ctx,
		unsigned length, uint8_t *dst, const uint8_t *src);

void
gcm_aes_decrypt(struct gcm_aes_ctx *ctx,
		unsigned length, uint8_t *dst, const uint8_t *src);

void
gcm_aes_digest(struct gcm_aes_ctx *ctx,
	       unsigned length, uint8_t *digest);
#endif

#ifdef __cplusplus
}
#endif

#endif /* NETTLE_GCM_H_INCLUDED */