summaryrefslogtreecommitdiff
path: root/lib/gnutls_cipher_int.c
blob: 5814d5127c4529a189467add06f6f7dec4ac1cd7 (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
/*
 * Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 *
 * Author: Nikos Mavrogiannopoulos
 *
 * This file is part of GnuTLS.
 *
 * The GnuTLS 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 3 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 program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

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

#define SR(x, cleanup) if ( (x)<0 ) { \
  gnutls_assert(); \
  ret = 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 enc)
{
  int ret = GNUTLS_E_INTERNAL_ERROR;
  const gnutls_crypto_cipher_st *cc = NULL;

  if (cipher == GNUTLS_CIPHER_NULL)
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);

  handle->is_aead = _gnutls_cipher_algo_is_aead(cipher);
  if (handle->is_aead)
     handle->tag_size = gnutls_cipher_get_block_size(cipher);

  /* check if a cipher has been registered
   */
  cc = _gnutls_get_crypto_cipher (cipher);
  if (cc != NULL)
    {
      handle->encrypt = cc->encrypt;
      handle->decrypt = cc->decrypt;
      handle->deinit = cc->deinit;
      handle->auth = cc->auth;
      handle->tag = cc->tag;
      handle->setiv = cc->setiv;

      SR (cc->init (cipher, &handle->handle, enc), cc_cleanup);
      SR (cc->setkey( handle->handle, key->data, key->size), cc_cleanup);
      if (iv)
        {
          SR (cc->setiv( handle->handle, iv->data, iv->size), cc_cleanup);
        }

      return 0;
    }

  handle->encrypt = _gnutls_cipher_ops.encrypt;
  handle->decrypt = _gnutls_cipher_ops.decrypt;
  handle->deinit = _gnutls_cipher_ops.deinit;
  handle->auth = _gnutls_cipher_ops.auth;
  handle->tag = _gnutls_cipher_ops.tag;
  handle->setiv = _gnutls_cipher_ops.setiv;

  /* otherwise use generic cipher interface
   */
  ret = _gnutls_cipher_ops.init (cipher, &handle->handle, enc);
  if (ret < 0)
    {
      gnutls_assert ();
      return ret;
    }

  ret = _gnutls_cipher_ops.setkey(handle->handle, key->data, key->size);
  if (ret < 0)
    {
      gnutls_assert ();
      goto cc_cleanup;
    }

  if (iv)
    {
      ret = _gnutls_cipher_ops.setiv(handle->handle, iv->data, iv->size);
      if (ret < 0)
        {
          gnutls_assert ();
          goto cc_cleanup;
        }
    }

  return 0;

cc_cleanup:

  if (handle->handle)
    handle->deinit (handle->handle);

  return ret;
}

/* Auth_cipher API 
 */
int _gnutls_auth_cipher_init (auth_cipher_hd_st * handle, 
  gnutls_cipher_algorithm_t cipher,
  const gnutls_datum_t * cipher_key,
  const gnutls_datum_t * iv,
  gnutls_mac_algorithm_t mac,
  const gnutls_datum_t * mac_key,
  int ssl_hmac, int enc)
{
int ret;

  memset(handle, 0, sizeof(*handle));

  if (cipher != GNUTLS_CIPHER_NULL)
    {
      ret = _gnutls_cipher_init(&handle->cipher, cipher, cipher_key, iv, enc);
      if (ret < 0)
        return gnutls_assert_val(ret);
    }
  else
    handle->is_null = 1;

  if (mac != GNUTLS_MAC_AEAD)
    {
      handle->is_mac = 1;
      handle->ssl_hmac = ssl_hmac;

      if (ssl_hmac)
        ret = _gnutls_mac_init_ssl3(&handle->mac, mac, mac_key->data, mac_key->size);
      else
        ret = _gnutls_hmac_init(&handle->mac, mac, mac_key->data, mac_key->size);
      if (ret < 0)
        {
          gnutls_assert();
          goto cleanup;
        }

      handle->tag_size = _gnutls_hash_get_algo_len(mac);
    }
  else if (_gnutls_cipher_is_aead(&handle->cipher))
    handle->tag_size = _gnutls_cipher_tag_len(&handle->cipher);

  return 0;
cleanup:
  if (handle->is_null == 0)
    _gnutls_cipher_deinit(&handle->cipher);
  return ret;

}

int _gnutls_auth_cipher_add_auth (auth_cipher_hd_st * handle, const void *text,
                             int textlen)
{
  if (handle->is_mac)
    {
      if (handle->ssl_hmac)
        return _gnutls_hash(&handle->mac, text, textlen);
      else
        return _gnutls_hmac(&handle->mac, text, textlen);
    }
  else if (_gnutls_cipher_is_aead(&handle->cipher))
    return _gnutls_cipher_auth(&handle->cipher, text, textlen);
  else
    return 0;
}

int _gnutls_auth_cipher_encrypt2_tag (auth_cipher_hd_st * handle, const uint8_t *text,
                      int textlen, void *ciphertext, int ciphertextlen, 
                      void* tag_ptr, int tag_size,
                      int auth_size)
{
int ret;

  if (handle->is_mac)
    {
      if (handle->ssl_hmac)
        ret = _gnutls_hash(&handle->mac, text, auth_size);
      else
        ret = _gnutls_hmac(&handle->mac, text, auth_size);
      if (ret < 0)
        {
          gnutls_assert();
          return ret;
        }
      ret = _gnutls_auth_cipher_tag(handle, tag_ptr, tag_size);
      if (ret < 0)
        return gnutls_assert_val(ret);

      if (handle->is_null==0)
        {
          ret = _gnutls_cipher_encrypt2(&handle->cipher, text, textlen, ciphertext, ciphertextlen);
          if (ret < 0)
            return gnutls_assert_val(ret);
        }
    }
  else if (_gnutls_cipher_is_aead(&handle->cipher))
    {
      ret = _gnutls_cipher_encrypt2(&handle->cipher, text, textlen, ciphertext, ciphertextlen);
      if (ret < 0)
        return gnutls_assert_val(ret);

      ret = _gnutls_auth_cipher_tag(handle, tag_ptr, tag_size);
      if (ret < 0)
        return gnutls_assert_val(ret);
    }

  return 0;
}

int _gnutls_auth_cipher_decrypt2 (auth_cipher_hd_st * handle,
                             const void *ciphertext, int ciphertextlen,
                             void *text, int textlen)
{
int ret;

  if (handle->is_null==0)
    {
      ret = _gnutls_cipher_decrypt2(&handle->cipher, ciphertext, ciphertextlen, 
        text, textlen);
      if (ret < 0)
        return gnutls_assert_val(ret);
    }

  if (handle->is_mac)
    {
      /* The MAC is not to be hashed */
      textlen -= handle->tag_size;

      if (handle->ssl_hmac)
        return _gnutls_hash(&handle->mac, text, textlen);
      else
        return _gnutls_hmac(&handle->mac, text, textlen);
    }

  return 0;
}

int _gnutls_auth_cipher_tag(auth_cipher_hd_st * handle, void* tag, int tag_size)
{
int ret = 0;
  if (handle->is_mac)
    {
      if (handle->ssl_hmac)
        {
          ret = _gnutls_mac_output_ssl3 (&handle->mac, tag);
          if (ret < 0)
            return gnutls_assert_val(ret);

          _gnutls_mac_reset_ssl3 (&handle->mac);
        }
      else
        {
          _gnutls_hmac_output (&handle->mac, tag);
          _gnutls_hmac_reset (&handle->mac);
        }
    }
  else if (_gnutls_cipher_is_aead(&handle->cipher))
    {
      _gnutls_cipher_tag(&handle->cipher, tag, tag_size);
    }
    
  return 0;
}

void _gnutls_auth_cipher_deinit (auth_cipher_hd_st * handle)
{
  if (handle->is_mac)
    {
      if (handle->ssl_hmac) /* failure here doesn't matter */
        _gnutls_mac_deinit_ssl3 (&handle->mac, NULL);
      else
        _gnutls_hmac_deinit(&handle->mac, NULL);
    }
  if (handle->is_null==0)
    _gnutls_cipher_deinit(&handle->cipher);
}