summaryrefslogtreecommitdiff
path: root/lib/gnutls_cipher_int.c
blob: b9cc304b988152fede44f133cd112f1aa11e10fa (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
/*
 * Copyright (C) 2000, 2004, 2005, 2008 Free Software Foundation
 *
 * Author: Nikos Mavrogiannopoulos
 *
 * This file is part of GNUTLS.
 *
 * The GNUTLS 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.
 *
 * This 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 this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
 * USA
 *
 */

#include <gnutls_int.h>
#include <gnutls_errors.h>
#include <gnutls_cipher_int.h>
#include <gnutls_datum.h>
#include <gnutls/crypto.h>
#include <crypto.h>

#define SR(x, cleanup) if ( (x)<0 ) { \
  gnutls_assert(); \
  err = GNUTLS_E_INTERNAL_ERROR; \
  goto cleanup; \
  }

int
_gnutls_cipher_init (cipher_hd_st* handle, gnutls_cipher_algorithm_t cipher,
		     const gnutls_datum_t * key, const gnutls_datum_t * iv)
{
  int err = GC_INVALID_CIPHER;	/* doesn't matter */
  gnutls_crypto_cipher_st * cc = NULL;

  /* check if a cipher has been registered 
   */
  cc = _gnutls_get_crypto_cipher( cipher);
  if (cc != NULL) {
    handle->registered = 1;
    handle->hd.rh.cc = cc;
    SR( cc->init(&handle->hd.rh.ctx), cc_cleanup );
    SR(cc->setkey( handle->hd.rh.ctx, key->data, key->size), cc_cleanup);
    if (iv->data && iv->size && cc->setiv)
      SR(cc->setiv( handle->hd.rh.ctx, iv->data, iv->size), cc_cleanup);
    return 0;
  }

  handle->registered = 0;  
  /* otherwise use included ciphers 
   */
  switch (cipher)
    {
    case GNUTLS_CIPHER_AES_128_CBC:
      err = gc_cipher_open (GC_AES128, GC_CBC, &handle->hd.gc);
      break;

    case GNUTLS_CIPHER_AES_256_CBC:
      err = gc_cipher_open (GC_AES256, GC_CBC, &handle->hd.gc);
      break;

    case GNUTLS_CIPHER_3DES_CBC:
      err = gc_cipher_open (GC_3DES, GC_CBC, &handle->hd.gc);
      break;

    case GNUTLS_CIPHER_DES_CBC:
      err = gc_cipher_open (GC_DES, GC_CBC, &handle->hd.gc);
      break;

    case GNUTLS_CIPHER_ARCFOUR_128:
      err = gc_cipher_open (GC_ARCFOUR128, GC_STREAM, &handle->hd.gc);
      break;

    case GNUTLS_CIPHER_ARCFOUR_40:
      err = gc_cipher_open (GC_ARCFOUR40, GC_STREAM, &handle->hd.gc);
      break;

    case GNUTLS_CIPHER_RC2_40_CBC:
      err = gc_cipher_open (GC_ARCTWO40, GC_CBC, &handle->hd.gc);
      break;

#ifdef	ENABLE_CAMELLIA
    case GNUTLS_CIPHER_CAMELLIA_128_CBC:
      err = gc_cipher_open (GC_CAMELLIA128, GC_CBC, &handle->hd.gc);
      break;

    case GNUTLS_CIPHER_CAMELLIA_256_CBC:
      err = gc_cipher_open (GC_CAMELLIA256, GC_CBC, &handle->hd.gc);
      break;
#endif

    default:
      gnutls_assert();
      return GNUTLS_E_INVALID_REQUEST;
    }

  if (err == 0)
    {
      gc_cipher_setkey (handle->hd.gc, key->size, key->data);
      if (iv->data != NULL && iv->size > 0)
	gc_cipher_setiv (handle->hd.gc, iv->size, iv->data);
    }
  else if (cipher != GNUTLS_CIPHER_NULL)
    {
      gnutls_assert ();
      _gnutls_x509_log ("Crypto cipher[%d] error: %d\n", cipher, err);
      return GNUTLS_E_INTERNAL_ERROR;
      /* FIXME: gc_strerror */
    }

  return 0;

cc_cleanup:

  if (handle->hd.rh.cc)
    cc->deinit(handle->hd.rh.ctx);
  
  return err;
}

int
_gnutls_cipher_encrypt (const cipher_hd_st* handle, void *text, int textlen)
{
  if (handle != NULL)
    {
      if (handle->registered) {
        if (handle->hd.rh.ctx == NULL) return 0;
        return handle->hd.rh.cc->encrypt( handle->hd.rh.ctx, text, textlen, text, textlen);
      }
      
      if (handle->hd.gc == NULL) return 0;
      if (gc_cipher_encrypt_inline (handle->hd.gc, textlen, text) != 0)
	{
	  gnutls_assert ();
	  return GNUTLS_E_INTERNAL_ERROR;
	}
    }
  return 0;
}

int
_gnutls_cipher_decrypt (const cipher_hd_st *handle, void *ciphertext,
			int ciphertextlen)
{
  if (handle != NULL)
    {
      if (handle->registered) {
        if (handle->hd.rh.ctx == NULL) return 0;
        return handle->hd.rh.cc->decrypt( handle->hd.rh.ctx, ciphertext, ciphertextlen, ciphertext, ciphertextlen);
      }

      if (handle->hd.gc == NULL) return 0;
      if (gc_cipher_decrypt_inline (handle->hd.gc, ciphertextlen, ciphertext) != 0)
	{
	  gnutls_assert ();
	  return GNUTLS_E_INTERNAL_ERROR;
	}
    }
  return 0;
}

void
_gnutls_cipher_deinit (cipher_hd_st* handle)
{
  if (handle != NULL)
    {
      if (handle->registered && handle->hd.rh.ctx != NULL)
	{
	  handle->hd.rh.cc->deinit( handle->hd.rh.ctx);
	  return;
	}
      }
      gc_cipher_close (handle->hd.gc);
    }
}