summaryrefslogtreecommitdiff
path: root/crypto/gc-nettle.c
blob: 68d05775fae5c689e5114ccf98c17d2bb59aa842 (plain)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
104
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/* gc-nettle.c --- Crypto wrappers around Nettle for GC.
 * Copyright (C) 2002, 2003, 2004  Simon Josefsson
 *
 * This file is part of GC.
 *
 * GC 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.
 *
 * GC 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 License along with GC; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 *
 */

/* Note: This file is only built if GC uses Nettle. */

#if HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdlib.h>

/* Get prototype. */
#include <gc.h>

/* For randomize. */
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

/* XXX GnuTLS still need Libgcrypt. */
#include <gcrypt.h>

int
gc_init (void)
{
  gcry_error_t err;

  /* XXX GnuTLS still need Libgcrypt. */

  err = gcry_control (GCRYCTL_ANY_INITIALIZATION_P);
  if (err == GPG_ERR_NO_ERROR)
    {
      if (gcry_check_version (GCRYPT_VERSION) == NULL)
	return GC_INIT_ERROR;

      err = gcry_control (GCRYCTL_INITIALIZATION_FINISHED, NULL, 0);
      if (err != GPG_ERR_NO_ERROR)
	return GC_INIT_ERROR;
    }

  return GC_OK;
}

void
gc_done (void)
{
  return;
}

/* Randomness. */

static int
randomize (int level, char *data, size_t datalen)
{
  int fd;
  char *device;
  size_t len = 0;
  int rc;

  switch (level)
    {
    case 0:
      device = NAME_OF_NONCE_DEVICE;
      break;

    case 1:
      device = NAME_OF_PSEUDO_RANDOM_DEVICE;
      break;

    default:
      device = NAME_OF_RANDOM_DEVICE;
      break;
    }

  fd = open (device, O_RDONLY);
  if (fd < 0)
    return GC_RANDOM_ERROR;

  do
    {
      ssize_t tmp;

      tmp = read (fd, data, datalen);

      if (tmp < 0)
	return GC_RANDOM_ERROR;

      len += tmp;
    }
  while (len < datalen);

  rc = close (fd);
  if (rc < 0)
    return GC_RANDOM_ERROR;

  return GC_OK;
}

int
gc_nonce (char *data, size_t datalen)
{
  return randomize (0, data, datalen);
}

int
gc_pseudo_random (char *data, size_t datalen)
{
  return randomize (1, data, datalen);
}

int
gc_random (char *data, size_t datalen)
{
  return randomize (2, data, datalen);
}

/* Memory allocation. */

void
gc_set_allocators (gc_malloc_t func_malloc,
		   gc_malloc_t secure_malloc,
		   gc_secure_check_t secure_check,
		   gc_realloc_t func_realloc, gc_free_t func_free)
{
  /* XXX GnuTLS still need Libgcrypt. */
  gcry_set_allocation_handler (func_malloc, secure_malloc, secure_check,
			       func_realloc, func_free);
}

/* Ciphers. */

#include "nettle-meta.h"
#include "aes.h"

#define MAX_BLOCK_SIZE 64

typedef struct cipher_info {
  int alg;
  int mode;
  const struct nettle_cipher *info;
  void *encrypt_context;
  void *decrypt_context;
  char encrypt_iv[MAX_BLOCK_SIZE];
  char decrypt_iv[MAX_BLOCK_SIZE];
} cinfo;

int
gc_cipher_open (int alg, int mode, gc_cipher * outhandle)
{
  cinfo *cinf;

  cinf = malloc (sizeof (*cinf));
  if (!cinf)
    return GC_MALLOC_ERROR;

  cinf->alg = alg;
  cinf->mode = mode;

  switch (alg)
    {
    case GC_AES256:
      cinf->info = &nettle_aes256;
      break;

    case GC_AES128:
      cinf->info = &nettle_aes128;
      break;

    case GC_3DES:
      cinf->info = &nettle_des3;
      break;

    case GC_DES:
      cinf->info = &nettle_des;
      break;

    case GC_ARCFOUR128:
    case GC_ARCFOUR40:
      cinf->info = &nettle_arcfour128;
      break;

      /* FIXME: ARCTWO-40. */

    default:
      free (cinf);
      return GC_INVALID_CIPHER;
    }

  cinf->encrypt_context = malloc (cinf->info->context_size);
  if (!cinf->encrypt_context)
    {
      free (cinf);
      return GC_MALLOC_ERROR;
    }

  cinf->decrypt_context = malloc (cinf->info->context_size);
  if (!cinf->decrypt_context)
    {
      free (cinf->encrypt_context);
      free (cinf);
      return GC_MALLOC_ERROR;
    }

  memset (cinf->encrypt_context, 0, cinf->info->context_size);
  memset (cinf->decrypt_context, 0, cinf->info->context_size);

  *outhandle = cinf;

  return GC_OK;
}

int
gc_cipher_setkey (gc_cipher handle, size_t keylen, char *key)
{
  cinfo *cinf = (cinfo*) handle;

  cinf->info->set_encrypt_key (cinf->encrypt_context, keylen, key);
  cinf->info->set_decrypt_key (cinf->decrypt_context, keylen, key);

  return GC_OK;
}

int
gc_cipher_setiv (gc_cipher handle, size_t ivlen, char *iv)
{
  cinfo *cinf = (cinfo*) handle;

  if (ivlen != cinf->info->block_size)
    return GC_INVALID_CIPHER;

  memcpy (cinf->encrypt_iv, iv, ivlen);
  memcpy (cinf->decrypt_iv, iv, ivlen);

  return GC_OK;
}

int
gc_cipher_encrypt_inline (gc_cipher handle, size_t len, char *data)
{
  cinfo *cinf = (cinfo*) handle;

  if (cinf->mode == GC_CBC)
    cbc_encrypt (cinf->encrypt_context, cinf->info->encrypt,
		 cinf->info->block_size, cinf->encrypt_iv,
		 len, data, data);
  else
    cinf->info->encrypt (cinf->encrypt_context, len, data, data);

  return GC_OK;
}

int
gc_cipher_decrypt_inline (gc_cipher handle, size_t len, char *data)
{
  cinfo *cinf = (cinfo*) handle;

  if (cinf->mode == GC_CBC)
    cbc_decrypt (cinf->decrypt_context, cinf->info->decrypt,
		 cinf->info->block_size, cinf->decrypt_iv,
		 len, data, data);
  else
    cinf->info->decrypt (cinf->decrypt_context, len, data, data);

  return GC_OK;
}

int
gc_cipher_close (gc_cipher handle)
{
  cinfo *cinf = (cinfo*) handle;

  free (cinf->encrypt_context);
  free (cinf->decrypt_context);
  free (cinf);

  return GC_OK;
}