Coverage Report

Created: 2024-07-23 07:36

/src/gnutls/lib/crypto-api.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C) 2000-2016 Free Software Foundation, Inc.
3
 * Copyright (C) 2016 Red Hat, Inc.
4
 *
5
 * Author: Nikos Mavrogiannopoulos
6
 *
7
 * This file is part of GnuTLS.
8
 *
9
 * The GnuTLS is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU Lesser General Public License
11
 * as published by the Free Software Foundation; either version 2.1 of
12
 * the License, or (at your option) any later version.
13
 *
14
 * This library is distributed in the hope that it will be useful, but
15
 * WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 * Lesser General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public License
20
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
21
 *
22
 */
23
24
#include "gnutls_int.h"
25
#include "errors.h"
26
#include "cipher_int.h"
27
#include "datum.h"
28
#include <gnutls/crypto.h>
29
#include "algorithms.h"
30
#include "random.h"
31
#include "crypto.h"
32
#include "fips.h"
33
#include "crypto-api.h"
34
#include "iov.h"
35
#include "intprops.h"
36
37
typedef struct api_cipher_hd_st {
38
  cipher_hd_st ctx_enc;
39
  cipher_hd_st ctx_dec;
40
} api_cipher_hd_st;
41
42
/**
43
 * gnutls_cipher_init:
44
 * @handle: is a #gnutls_cipher_hd_t type
45
 * @cipher: the encryption algorithm to use
46
 * @key: the key to be used for encryption/decryption
47
 * @iv: the IV to use (if not applicable set NULL)
48
 *
49
 * This function will initialize the @handle context to be usable
50
 * for encryption/decryption of data. This will effectively use the
51
 * current crypto backend in use by gnutls or the cryptographic
52
 * accelerator in use.
53
 *
54
 * Returns: Zero or a negative error code on error.
55
 *
56
 * Since: 2.10.0
57
 **/
58
int gnutls_cipher_init(gnutls_cipher_hd_t *handle,
59
           gnutls_cipher_algorithm_t cipher,
60
           const gnutls_datum_t *key, const gnutls_datum_t *iv)
61
0
{
62
0
  api_cipher_hd_st *h;
63
0
  int ret;
64
0
  const cipher_entry_st *e;
65
0
  bool not_approved = false;
66
67
0
  if (!is_cipher_algo_allowed(cipher)) {
68
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
69
0
    return gnutls_assert_val(GNUTLS_E_UNWANTED_ALGORITHM);
70
0
  } else if (!is_cipher_algo_approved_in_fips(cipher)) {
71
0
    not_approved = true;
72
0
  }
73
74
0
  e = cipher_to_entry(cipher);
75
0
  if (e == NULL || (e->flags & GNUTLS_CIPHER_FLAG_ONLY_AEAD)) {
76
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
77
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
78
0
  }
79
80
0
  h = gnutls_calloc(1, sizeof(api_cipher_hd_st));
81
0
  if (h == NULL) {
82
0
    gnutls_assert();
83
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
84
0
    return GNUTLS_E_MEMORY_ERROR;
85
0
  }
86
87
0
  ret = _gnutls_cipher_init(&h->ctx_enc, e, key, iv, 1);
88
0
  if (ret < 0) {
89
0
    gnutls_free(h);
90
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
91
0
    return ret;
92
0
  }
93
94
0
  if (_gnutls_cipher_type(e) == CIPHER_BLOCK) {
95
0
    ret = _gnutls_cipher_init(&h->ctx_dec, e, key, iv, 0);
96
0
    if (ret < 0) {
97
0
      gnutls_free(h);
98
0
      _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
99
0
      return ret;
100
0
    }
101
0
  }
102
103
0
  *handle = h;
104
105
0
  if (not_approved) {
106
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_NOT_APPROVED);
107
0
  } else {
108
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_APPROVED);
109
0
  }
110
111
0
  return ret;
112
0
}
113
114
/**
115
 * gnutls_cipher_tag:
116
 * @handle: is a #gnutls_cipher_hd_t type
117
 * @tag: will hold the tag
118
 * @tag_size: the length of the tag to return
119
 *
120
 * This function operates on authenticated encryption with
121
 * associated data (AEAD) ciphers and will return the
122
 * output tag.
123
 *
124
 * Returns: Zero or a negative error code on error.
125
 *
126
 * Since: 3.0
127
 **/
128
int gnutls_cipher_tag(gnutls_cipher_hd_t handle, void *tag, size_t tag_size)
129
0
{
130
0
  api_cipher_hd_st *h = handle;
131
132
0
  if (_gnutls_cipher_is_aead(&h->ctx_enc) == 0)
133
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
134
135
0
  _gnutls_cipher_tag(&h->ctx_enc, tag, tag_size);
136
137
0
  return 0;
138
0
}
139
140
/**
141
 * gnutls_cipher_add_auth:
142
 * @handle: is a #gnutls_cipher_hd_t type
143
 * @ptext: the data to be authenticated
144
 * @ptext_size: the length of the data
145
 *
146
 * This function operates on authenticated encryption with
147
 * associated data (AEAD) ciphers and authenticate the
148
 * input data. This function can only be called once
149
 * and before any encryption operations.
150
 *
151
 * Returns: Zero or a negative error code on error.
152
 *
153
 * Since: 3.0
154
 **/
155
int gnutls_cipher_add_auth(gnutls_cipher_hd_t handle, const void *ptext,
156
         size_t ptext_size)
157
0
{
158
0
  api_cipher_hd_st *h = handle;
159
0
  int ret;
160
161
0
  if (_gnutls_cipher_is_aead(&h->ctx_enc) == 0) {
162
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
163
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
164
0
  }
165
166
0
  ret = _gnutls_cipher_auth(&h->ctx_enc, ptext, ptext_size);
167
0
  if (ret < 0) {
168
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
169
0
  }
170
0
  return ret;
171
0
}
172
173
/**
174
 * gnutls_cipher_set_iv:
175
 * @handle: is a #gnutls_cipher_hd_t type
176
 * @iv: the IV to set
177
 * @ivlen: the length of the IV
178
 *
179
 * This function will set the IV to be used for the next
180
 * encryption block.
181
 *
182
 * Since: 3.0
183
 **/
184
void gnutls_cipher_set_iv(gnutls_cipher_hd_t handle, void *iv, size_t ivlen)
185
0
{
186
0
  api_cipher_hd_st *h = handle;
187
188
0
  if (_gnutls_cipher_setiv(&h->ctx_enc, iv, ivlen) < 0) {
189
0
    _gnutls_switch_lib_state(LIB_STATE_ERROR);
190
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
191
0
  }
192
193
0
  if (_gnutls_cipher_type(h->ctx_enc.e) == CIPHER_BLOCK) {
194
0
    if (_gnutls_cipher_setiv(&h->ctx_dec, iv, ivlen) < 0) {
195
0
      _gnutls_switch_lib_state(LIB_STATE_ERROR);
196
0
      _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
197
0
    }
198
0
  }
199
0
}
200
201
/*-
202
 * _gnutls_cipher_get_iv:
203
 * @handle: is a #gnutls_cipher_hd_t type
204
 * @iv: the IV to set
205
 * @ivlen: the length of the IV
206
 *
207
 * This function will retrieve the internally calculated IV value. It is
208
 * intended to be used  for modes like CFB. @iv must have @ivlen length
209
 * at least.
210
 *
211
 * This is solely for validation purposes of our crypto
212
 * implementation.  For other purposes, the IV can be typically
213
 * calculated from the initial IV value and the subsequent ciphertext
214
 * values.  As such, this function only works with the internally
215
 * registered ciphers.
216
 *
217
 * Returns: The length of IV or a negative error code on error.
218
 *
219
 * Since: 3.6.8
220
 -*/
221
int _gnutls_cipher_get_iv(gnutls_cipher_hd_t handle, void *iv, size_t ivlen)
222
0
{
223
0
  api_cipher_hd_st *h = handle;
224
225
0
  return _gnutls_cipher_getiv(&h->ctx_enc, iv, ivlen);
226
0
}
227
228
/*-
229
 * _gnutls_cipher_set_key:
230
 * @handle: is a #gnutls_cipher_hd_t type
231
 * @key: the key to set
232
 * @keylen: the length of the key
233
 *
234
 * This function will set the key used by the cipher
235
 *
236
 * This is solely for validation purposes of our crypto
237
 * implementation.  For other purposes, the key should be set at the time of
238
 * cipher setup.  As such, this function only works with the internally
239
 * registered ciphers.
240
 *
241
 * Returns: Zero or a negative error code on error.
242
 *
243
 * Since: 3.6.14
244
 -*/
245
int _gnutls_cipher_set_key(gnutls_cipher_hd_t handle, void *key, size_t keylen)
246
0
{
247
0
  api_cipher_hd_st *h = handle;
248
0
  int ret;
249
250
0
  ret = _gnutls_cipher_setkey(&h->ctx_enc, key, keylen);
251
252
0
  if (ret < 0) {
253
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
254
0
  }
255
0
  return ret;
256
0
}
257
258
/**
259
 * gnutls_cipher_encrypt:
260
 * @handle: is a #gnutls_cipher_hd_t type
261
 * @ptext: the data to encrypt
262
 * @ptext_len: the length of data to encrypt
263
 *
264
 * This function will encrypt the given data using the algorithm
265
 * specified by the context.
266
 *
267
 * Returns: Zero or a negative error code on error.
268
 *
269
 * Since: 2.10.0
270
 **/
271
int gnutls_cipher_encrypt(gnutls_cipher_hd_t handle, void *ptext,
272
        size_t ptext_len)
273
0
{
274
0
  api_cipher_hd_st *h = handle;
275
0
  int ret;
276
277
0
  ret = _gnutls_cipher_encrypt(&h->ctx_enc, ptext, ptext_len);
278
0
  if (ret < 0) {
279
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
280
0
  } else {
281
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_APPROVED);
282
0
  }
283
0
  return ret;
284
0
}
285
286
/**
287
 * gnutls_cipher_decrypt:
288
 * @handle: is a #gnutls_cipher_hd_t type
289
 * @ctext: the data to decrypt
290
 * @ctext_len: the length of data to decrypt
291
 *
292
 * This function will decrypt the given data using the algorithm
293
 * specified by the context.
294
 *
295
 * Note that in AEAD ciphers, this will not check the tag. You will
296
 * need to compare the tag sent with the value returned from gnutls_cipher_tag().
297
 *
298
 * Returns: Zero or a negative error code on error.
299
 *
300
 * Since: 2.10.0
301
 **/
302
int gnutls_cipher_decrypt(gnutls_cipher_hd_t handle, void *ctext,
303
        size_t ctext_len)
304
0
{
305
0
  api_cipher_hd_st *h = handle;
306
0
  int ret;
307
308
0
  if (_gnutls_cipher_type(h->ctx_enc.e) != CIPHER_BLOCK) {
309
0
    ret = _gnutls_cipher_decrypt(&h->ctx_enc, ctext, ctext_len);
310
0
  } else {
311
0
    ret = _gnutls_cipher_decrypt(&h->ctx_dec, ctext, ctext_len);
312
0
  }
313
314
0
  if (ret < 0) {
315
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
316
0
  } else {
317
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_APPROVED);
318
0
  }
319
0
  return ret;
320
0
}
321
322
/**
323
 * gnutls_cipher_encrypt2:
324
 * @handle: is a #gnutls_cipher_hd_t type
325
 * @ptext: the data to encrypt
326
 * @ptext_len: the length of data to encrypt
327
 * @ctext: the encrypted data
328
 * @ctext_len: the available length for encrypted data
329
 *
330
 * This function will encrypt the given data using the algorithm
331
 * specified by the context. For block ciphers the @ptext_len must be
332
 * a multiple of the block size. For the supported ciphers the encrypted
333
 * data length will equal the plaintext size.
334
 *
335
 * Returns: Zero or a negative error code on error.
336
 *
337
 * Since: 2.12.0
338
 **/
339
int gnutls_cipher_encrypt2(gnutls_cipher_hd_t handle, const void *ptext,
340
         size_t ptext_len, void *ctext, size_t ctext_len)
341
0
{
342
0
  api_cipher_hd_st *h = handle;
343
0
  int ret;
344
345
0
  ret = _gnutls_cipher_encrypt2(&h->ctx_enc, ptext, ptext_len, ctext,
346
0
              ctext_len);
347
0
  if (ret < 0) {
348
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
349
0
  } else {
350
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_APPROVED);
351
0
  }
352
0
  return ret;
353
0
}
354
355
/**
356
 * gnutls_cipher_decrypt2:
357
 * @handle: is a #gnutls_cipher_hd_t type
358
 * @ctext: the data to decrypt
359
 * @ctext_len: the length of data to decrypt
360
 * @ptext: the decrypted data
361
 * @ptext_len: the available length for decrypted data
362
 *
363
 * This function will decrypt the given data using the algorithm
364
 * specified by the context. For block ciphers the @ctext_len must be
365
 * a multiple of the block size. For the supported ciphers the plaintext
366
 * data length will equal the ciphertext size.
367
 *
368
 * Note that in AEAD ciphers, this will not check the tag. You will
369
 * need to compare the tag sent with the value returned from gnutls_cipher_tag().
370
 *
371
 * Returns: Zero or a negative error code on error.
372
 *
373
 * Since: 2.12.0
374
 **/
375
int gnutls_cipher_decrypt2(gnutls_cipher_hd_t handle, const void *ctext,
376
         size_t ctext_len, void *ptext, size_t ptext_len)
377
0
{
378
0
  api_cipher_hd_st *h = handle;
379
0
  int ret;
380
381
0
  if (_gnutls_cipher_type(h->ctx_enc.e) != CIPHER_BLOCK) {
382
0
    ret = _gnutls_cipher_decrypt2(&h->ctx_enc, ctext, ctext_len,
383
0
                ptext, ptext_len);
384
0
  } else {
385
0
    ret = _gnutls_cipher_decrypt2(&h->ctx_dec, ctext, ctext_len,
386
0
                ptext, ptext_len);
387
0
  }
388
389
0
  if (ret < 0) {
390
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
391
0
  } else {
392
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_APPROVED);
393
0
  }
394
0
  return ret;
395
0
}
396
397
/**
398
 * gnutls_cipher_encrypt3:
399
 * @handle: is a #gnutls_cipher_hd_t type
400
 * @ptext: the data to encrypt
401
 * @ptext_len: the length of data to encrypt
402
 * @ctext: the encrypted data
403
 * @ctext_len: the length of encrypted data (initially must hold the maximum available size)
404
 * @flags: flags for padding
405
 *
406
 * This function will encrypt the given data using the algorithm
407
 * specified by the context. For block ciphers, @ptext_len is
408
 * typically a multiple of the block size. If not, the caller can
409
 * instruct the function to pad the last block according to @flags.
410
 * Currently, the only available padding scheme is
411
 * %GNUTLS_CIPHER_PADDING_PKCS7.
412
 *
413
 * If @ctext is not %NULL, it must hold enough space to store
414
 * resulting cipher text. To check the required size, this function
415
 * can be called with @ctext set to %NULL. Then @ctext_len will be
416
 * updated without performing actual encryption.
417
 *
418
 * Returns: Zero or a negative error code on error.
419
 *
420
 * Since: 3.7.7
421
 **/
422
int gnutls_cipher_encrypt3(gnutls_cipher_hd_t handle, const void *ptext,
423
         size_t ptext_len, void *ctext, size_t *ctext_len,
424
         unsigned flags)
425
0
{
426
0
  api_cipher_hd_st *h = handle;
427
0
  const cipher_entry_st *e = h->ctx_enc.e;
428
0
  int block_size = _gnutls_cipher_get_block_size(e);
429
0
  int ret = 0;
430
431
0
  if (unlikely(ctext_len == NULL)) {
432
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
433
0
  }
434
435
0
  if (_gnutls_cipher_type(e) == CIPHER_BLOCK &&
436
0
      (flags & GNUTLS_CIPHER_PADDING_PKCS7)) {
437
0
    size_t n, r;
438
0
    uint8_t last_block[MAX_CIPHER_BLOCK_SIZE];
439
0
    const uint8_t *p = ptext;
440
0
    uint8_t *c = ctext;
441
442
0
    if (!INT_ADD_OK(ptext_len, block_size, &n)) {
443
0
      return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
444
0
    }
445
446
0
    n = (n / block_size) * block_size;
447
448
0
    if (!ctext) {
449
0
      *ctext_len = n;
450
0
      return 0;
451
0
    }
452
453
0
    if (*ctext_len < n) {
454
0
      return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER);
455
0
    }
456
457
    /* Encrypt up to the last complete block */
458
0
    r = ptext_len % block_size;
459
460
0
    ret = _gnutls_cipher_encrypt2(&h->ctx_enc, ptext, ptext_len - r,
461
0
                ctext, ptext_len - r);
462
0
    if (ret < 0) {
463
0
      goto error;
464
0
    }
465
466
    /* Encrypt the last block with padding */
467
0
    gnutls_memset(last_block, block_size - r, sizeof(last_block));
468
0
    if (r > 0) {
469
0
      memcpy(last_block, &p[ptext_len - r], r);
470
0
    }
471
0
    ret = _gnutls_cipher_encrypt2(&h->ctx_enc, last_block,
472
0
                block_size, &c[ptext_len - r],
473
0
                block_size);
474
0
    if (ret < 0) {
475
0
      goto error;
476
0
    }
477
0
    *ctext_len = n;
478
0
  } else {
479
0
    if (!ctext) {
480
0
      *ctext_len = ptext_len;
481
0
      return 0;
482
0
    }
483
484
0
    ret = _gnutls_cipher_encrypt2(&h->ctx_enc, ptext, ptext_len,
485
0
                ctext, *ctext_len);
486
0
    if (ret < 0) {
487
0
      goto error;
488
0
    }
489
0
    *ctext_len = ptext_len;
490
0
  }
491
492
0
error:
493
0
  if (ret < 0) {
494
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
495
0
  } else {
496
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_APPROVED);
497
0
  }
498
0
  return ret;
499
0
}
500
501
/**
502
 * gnutls_cipher_decrypt3:
503
 * @handle: is a #gnutls_cipher_hd_t type
504
 * @ctext: the data to decrypt
505
 * @ctext_len: the length of data to decrypt
506
 * @ptext: the decrypted data
507
 * @ptext_len: the available length for decrypted data
508
 * @flags: flags for padding
509
 *
510
 * This function will decrypt the given data using the algorithm
511
 * specified by the context. If @flags is specified, padding for the
512
 * decrypted data will be removed accordingly and @ptext_len will be
513
 * updated.
514
 *
515
 * Returns: Zero or a negative error code on error.
516
 *
517
 * Since: 3.7.7
518
 **/
519
int gnutls_cipher_decrypt3(gnutls_cipher_hd_t handle, const void *ctext,
520
         size_t ctext_len, void *ptext, size_t *ptext_len,
521
         unsigned flags)
522
0
{
523
0
  api_cipher_hd_st *h = handle;
524
0
  int ret;
525
526
0
  ret = gnutls_cipher_decrypt2(handle, ctext, ctext_len, ptext,
527
0
             *ptext_len);
528
0
  if (ret < 0) {
529
0
    return ret;
530
0
  }
531
532
0
  if (_gnutls_cipher_type(h->ctx_enc.e) == CIPHER_BLOCK &&
533
0
      (flags & GNUTLS_CIPHER_PADDING_PKCS7)) {
534
0
    uint8_t *p = ptext;
535
0
    uint8_t padding = p[*ptext_len - 1];
536
0
    if (!padding ||
537
0
        padding > _gnutls_cipher_get_block_size(h->ctx_enc.e)) {
538
0
      return gnutls_assert_val(GNUTLS_E_DECRYPTION_FAILED);
539
0
    }
540
    /* Check that the prior bytes are all PADDING */
541
0
    for (size_t i = *ptext_len - padding; i < *ptext_len; i++) {
542
0
      if (padding != p[*ptext_len - 1]) {
543
0
        return gnutls_assert_val(
544
0
          GNUTLS_E_DECRYPTION_FAILED);
545
0
      }
546
0
    }
547
0
    *ptext_len -= padding;
548
0
  }
549
550
0
  return 0;
551
0
}
552
553
/**
554
 * gnutls_cipher_deinit:
555
 * @handle: is a #gnutls_cipher_hd_t type
556
 *
557
 * This function will deinitialize all resources occupied by the given
558
 * encryption context.
559
 *
560
 * Since: 2.10.0
561
 **/
562
void gnutls_cipher_deinit(gnutls_cipher_hd_t handle)
563
0
{
564
0
  api_cipher_hd_st *h = handle;
565
566
0
  _gnutls_cipher_deinit(&h->ctx_enc);
567
0
  if (_gnutls_cipher_type(h->ctx_enc.e) == CIPHER_BLOCK)
568
0
    _gnutls_cipher_deinit(&h->ctx_dec);
569
0
  gnutls_free(handle);
570
0
}
571
572
/* HMAC */
573
574
/**
575
 * gnutls_hmac_init:
576
 * @dig: is a #gnutls_hmac_hd_t type
577
 * @algorithm: the HMAC algorithm to use
578
 * @key: the key to be used for encryption
579
 * @keylen: the length of the key
580
 *
581
 * This function will initialize an context that can be used to
582
 * produce a Message Authentication Code (MAC) of data.  This will
583
 * effectively use the current crypto backend in use by gnutls or the
584
 * cryptographic accelerator in use.
585
 *
586
 * Note that despite the name of this function, it can be used
587
 * for other MAC algorithms than HMAC.
588
 *
589
 * Returns: Zero or a negative error code on error.
590
 *
591
 * Since: 2.10.0
592
 **/
593
int gnutls_hmac_init(gnutls_hmac_hd_t *dig, gnutls_mac_algorithm_t algorithm,
594
         const void *key, size_t keylen)
595
0
{
596
0
  int ret;
597
0
  bool not_approved = false;
598
599
  /* MD5 is only allowed internally for TLS */
600
0
  if (!is_mac_algo_allowed(algorithm)) {
601
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
602
0
    return gnutls_assert_val(GNUTLS_E_UNWANTED_ALGORITHM);
603
0
  } else if (!is_mac_algo_approved_in_fips(algorithm)) {
604
0
    not_approved = true;
605
0
  }
606
607
  /* Key lengths of less than 112 bits are not approved */
608
0
  if (keylen < 14) {
609
0
    not_approved = true;
610
0
  }
611
612
0
  *dig = gnutls_malloc(sizeof(mac_hd_st));
613
0
  if (*dig == NULL) {
614
0
    gnutls_assert();
615
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
616
0
    return GNUTLS_E_MEMORY_ERROR;
617
0
  }
618
619
0
  ret = _gnutls_mac_init(((mac_hd_st *)*dig), mac_to_entry(algorithm),
620
0
             key, keylen);
621
0
  if (ret < 0) {
622
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
623
0
  } else if (not_approved) {
624
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_NOT_APPROVED);
625
0
  } else {
626
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_APPROVED);
627
0
  }
628
0
  return ret;
629
0
}
630
631
/**
632
 * gnutls_hmac_set_nonce:
633
 * @handle: is a #gnutls_hmac_hd_t type
634
 * @nonce: the data to set as nonce
635
 * @nonce_len: the length of data
636
 *
637
 * This function will set the nonce in the MAC algorithm.
638
 *
639
 * Since: 3.2.0
640
 **/
641
void gnutls_hmac_set_nonce(gnutls_hmac_hd_t handle, const void *nonce,
642
         size_t nonce_len)
643
0
{
644
0
  _gnutls_mac_set_nonce((mac_hd_st *)handle, nonce, nonce_len);
645
0
}
646
647
/**
648
 * gnutls_hmac:
649
 * @handle: is a #gnutls_hmac_hd_t type
650
 * @ptext: the data to hash
651
 * @ptext_len: the length of data to hash
652
 *
653
 * This function will hash the given data using the algorithm
654
 * specified by the context.
655
 *
656
 * Returns: Zero or a negative error code on error.
657
 *
658
 * Since: 2.10.0
659
 **/
660
int gnutls_hmac(gnutls_hmac_hd_t handle, const void *ptext, size_t ptext_len)
661
0
{
662
0
  int ret;
663
664
0
  ret = _gnutls_mac((mac_hd_st *)handle, ptext, ptext_len);
665
0
  if (ret < 0) {
666
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
667
0
  } else {
668
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_APPROVED);
669
0
  }
670
0
  return ret;
671
0
}
672
673
/**
674
 * gnutls_hmac_output:
675
 * @handle: is a #gnutls_hmac_hd_t type
676
 * @digest: is the output value of the MAC
677
 *
678
 * This function will output the current MAC value
679
 * and reset the state of the MAC.
680
 *
681
 * Since: 2.10.0
682
 **/
683
void gnutls_hmac_output(gnutls_hmac_hd_t handle, void *digest)
684
0
{
685
0
  _gnutls_mac_output((mac_hd_st *)handle, digest);
686
0
}
687
688
/**
689
 * gnutls_hmac_deinit:
690
 * @handle: is a #gnutls_hmac_hd_t type
691
 * @digest: is the output value of the MAC
692
 *
693
 * This function will deinitialize all resources occupied by
694
 * the given hmac context.
695
 *
696
 * Since: 2.10.0
697
 **/
698
void gnutls_hmac_deinit(gnutls_hmac_hd_t handle, void *digest)
699
0
{
700
0
  _gnutls_mac_deinit((mac_hd_st *)handle, digest);
701
0
  gnutls_free(handle);
702
0
}
703
704
/**
705
 * gnutls_hmac_get_len:
706
 * @algorithm: the hmac algorithm to use
707
 *
708
 * This function will return the length of the output data
709
 * of the given hmac algorithm.
710
 *
711
 * Returns: The length or zero on error.
712
 *
713
 * Since: 2.10.0
714
 **/
715
unsigned gnutls_hmac_get_len(gnutls_mac_algorithm_t algorithm)
716
0
{
717
0
  return _gnutls_mac_get_algo_len(mac_to_entry(algorithm));
718
0
}
719
720
/**
721
 * gnutls_hmac_get_key_size:
722
 * @algorithm: the mac algorithm to use
723
 *
724
 * This function will return the size of the key to be used with this
725
 * algorithm. On the algorithms which may accept arbitrary key sizes,
726
 * the returned size is the MAC key size used in the TLS protocol.
727
 *
728
 * Returns: The key size or zero on error.
729
 *
730
 * Since: 3.6.12
731
 **/
732
unsigned gnutls_hmac_get_key_size(gnutls_mac_algorithm_t algorithm)
733
0
{
734
0
  return _gnutls_mac_get_key_size(mac_to_entry(algorithm));
735
0
}
736
737
/**
738
 * gnutls_hmac_fast:
739
 * @algorithm: the hash algorithm to use
740
 * @key: the key to use
741
 * @keylen: the length of the key
742
 * @ptext: the data to hash
743
 * @ptext_len: the length of data to hash
744
 * @digest: is the output value of the hash
745
 *
746
 * This convenience function will hash the given data and return output
747
 * on a single call. Note, this call will not work for MAC algorithms
748
 * that require nonce (like UMAC or GMAC).
749
 *
750
 * Returns: Zero or a negative error code on error.
751
 *
752
 * Since: 2.10.0
753
 **/
754
int gnutls_hmac_fast(gnutls_mac_algorithm_t algorithm, const void *key,
755
         size_t keylen, const void *ptext, size_t ptext_len,
756
         void *digest)
757
0
{
758
0
  int ret;
759
0
  bool not_approved = false;
760
761
0
  if (!is_mac_algo_allowed(algorithm)) {
762
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
763
0
    return gnutls_assert_val(GNUTLS_E_UNWANTED_ALGORITHM);
764
0
  } else if (!is_mac_algo_approved_in_fips(algorithm)) {
765
0
    not_approved = true;
766
0
  }
767
768
  /* Key lengths of less than 112 bits are not approved */
769
0
  if (keylen < 14) {
770
0
    not_approved = true;
771
0
  }
772
773
0
  ret = _gnutls_mac_fast(algorithm, key, keylen, ptext, ptext_len,
774
0
             digest);
775
0
  if (ret < 0) {
776
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
777
0
  } else if (not_approved) {
778
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_NOT_APPROVED);
779
0
  } else {
780
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_APPROVED);
781
0
  }
782
0
  return ret;
783
0
}
784
785
/**
786
 * gnutls_hmac_copy:
787
 * @handle: is a #gnutls_hmac_hd_t type
788
 *
789
 * This function will create a copy of MAC context, containing all its current
790
 * state. Copying contexts for MACs registered using
791
 * gnutls_crypto_register_mac() is not supported and will always result in an
792
 * error. In addition to that, some of the MAC implementations do not support
793
 * this operation. Applications should check the return value and provide a
794
 * proper fallback.
795
 *
796
 * Returns: new MAC context or NULL in case of an error.
797
 *
798
 * Since: 3.6.9
799
 */
800
gnutls_hmac_hd_t gnutls_hmac_copy(gnutls_hmac_hd_t handle)
801
0
{
802
0
  gnutls_hmac_hd_t dig;
803
804
0
  dig = gnutls_malloc(sizeof(mac_hd_st));
805
0
  if (dig == NULL) {
806
0
    gnutls_assert();
807
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
808
0
    return NULL;
809
0
  }
810
811
0
  if (_gnutls_mac_copy((const mac_hd_st *)handle, (mac_hd_st *)dig) !=
812
0
      GNUTLS_E_SUCCESS) {
813
0
    gnutls_assert();
814
0
    gnutls_free(dig);
815
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
816
0
    return NULL;
817
0
  }
818
819
0
  return dig;
820
0
}
821
822
/* HASH */
823
824
/**
825
 * gnutls_hash_init:
826
 * @dig: is a #gnutls_hash_hd_t type
827
 * @algorithm: the hash algorithm to use
828
 *
829
 * This function will initialize an context that can be used to
830
 * produce a Message Digest of data.  This will effectively use the
831
 * current crypto backend in use by gnutls or the cryptographic
832
 * accelerator in use.
833
 *
834
 * Returns: Zero or a negative error code on error.
835
 *
836
 * Since: 2.10.0
837
 **/
838
int gnutls_hash_init(gnutls_hash_hd_t *dig, gnutls_digest_algorithm_t algorithm)
839
0
{
840
0
  int ret;
841
0
  bool not_approved = false;
842
843
0
  if (!is_mac_algo_allowed(DIG_TO_MAC(algorithm))) {
844
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
845
0
    return gnutls_assert_val(GNUTLS_E_UNWANTED_ALGORITHM);
846
0
  } else if (!is_mac_algo_approved_in_fips(DIG_TO_MAC(algorithm))) {
847
0
    not_approved = true;
848
0
  }
849
850
0
  *dig = gnutls_malloc(sizeof(digest_hd_st));
851
0
  if (*dig == NULL) {
852
0
    gnutls_assert();
853
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
854
0
    return GNUTLS_E_MEMORY_ERROR;
855
0
  }
856
857
0
  ret = _gnutls_hash_init(((digest_hd_st *)*dig),
858
0
        hash_to_entry(algorithm));
859
0
  if (ret < 0) {
860
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
861
0
  } else if (not_approved) {
862
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_NOT_APPROVED);
863
0
  } else {
864
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_APPROVED);
865
0
  }
866
0
  return ret;
867
0
}
868
869
/**
870
 * gnutls_hash:
871
 * @handle: is a #gnutls_hash_hd_t type
872
 * @ptext: the data to hash
873
 * @ptext_len: the length of data to hash
874
 *
875
 * This function will hash the given data using the algorithm
876
 * specified by the context.
877
 *
878
 * Returns: Zero or a negative error code on error.
879
 *
880
 * Since: 2.10.0
881
 **/
882
int gnutls_hash(gnutls_hash_hd_t handle, const void *ptext, size_t ptext_len)
883
0
{
884
0
  int ret;
885
886
0
  ret = _gnutls_hash((digest_hd_st *)handle, ptext, ptext_len);
887
0
  if (ret < 0) {
888
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
889
0
  }
890
0
  return ret;
891
0
}
892
893
/**
894
 * gnutls_hash_output:
895
 * @handle: is a #gnutls_hash_hd_t type
896
 * @digest: is the output value of the hash
897
 *
898
 * This function will output the current hash value and reset the
899
 * state of the hash. If @digest is %NULL, it only resets the state of
900
 * the hash.
901
 *
902
 * Since: 2.10.0
903
 **/
904
void gnutls_hash_output(gnutls_hash_hd_t handle, void *digest)
905
0
{
906
0
  _gnutls_hash_output((digest_hd_st *)handle, digest);
907
0
}
908
909
/**
910
 * gnutls_hash_deinit:
911
 * @handle: is a #gnutls_hash_hd_t type
912
 * @digest: is the output value of the hash
913
 *
914
 * This function will deinitialize all resources occupied by
915
 * the given hash context.
916
 *
917
 * Since: 2.10.0
918
 **/
919
void gnutls_hash_deinit(gnutls_hash_hd_t handle, void *digest)
920
0
{
921
0
  _gnutls_hash_deinit((digest_hd_st *)handle, digest);
922
0
  gnutls_free(handle);
923
0
}
924
925
/**
926
 * gnutls_hash_get_len:
927
 * @algorithm: the hash algorithm to use
928
 *
929
 * This function will return the length of the output data
930
 * of the given hash algorithm.
931
 *
932
 * Returns: The length or zero on error.
933
 *
934
 * Since: 2.10.0
935
 **/
936
unsigned gnutls_hash_get_len(gnutls_digest_algorithm_t algorithm)
937
0
{
938
0
  return _gnutls_hash_get_algo_len(hash_to_entry(algorithm));
939
0
}
940
941
/**
942
 * gnutls_hash_fast:
943
 * @algorithm: the hash algorithm to use
944
 * @ptext: the data to hash
945
 * @ptext_len: the length of data to hash
946
 * @digest: is the output value of the hash
947
 *
948
 * This convenience function will hash the given data and return output
949
 * on a single call.
950
 *
951
 * Returns: Zero or a negative error code on error.
952
 *
953
 * Since: 2.10.0
954
 **/
955
int gnutls_hash_fast(gnutls_digest_algorithm_t algorithm, const void *ptext,
956
         size_t ptext_len, void *digest)
957
0
{
958
0
  int ret;
959
0
  bool not_approved = false;
960
961
0
  if (!is_mac_algo_allowed(DIG_TO_MAC(algorithm))) {
962
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
963
0
    return gnutls_assert_val(GNUTLS_E_UNWANTED_ALGORITHM);
964
0
  } else if (!is_mac_algo_approved_in_fips(DIG_TO_MAC(algorithm))) {
965
0
    not_approved = true;
966
0
  }
967
968
0
  ret = _gnutls_hash_fast(algorithm, ptext, ptext_len, digest);
969
0
  if (ret < 0) {
970
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
971
0
  } else if (not_approved) {
972
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_NOT_APPROVED);
973
0
  }
974
975
0
  return ret;
976
0
}
977
978
/**
979
 * gnutls_hash_copy:
980
 * @handle: is a #gnutls_hash_hd_t type
981
 *
982
 * This function will create a copy of Message Digest context, containing all
983
 * its current state. Copying contexts for Message Digests registered using
984
 * gnutls_crypto_register_digest() is not supported and will always result in
985
 * an error. In addition to that, some of the Message Digest implementations do
986
 * not support this operation. Applications should check the return value and
987
 * provide a proper fallback.
988
 *
989
 * Returns: new Message Digest context or NULL in case of an error.
990
 *
991
 * Since: 3.6.9
992
 */
993
gnutls_hash_hd_t gnutls_hash_copy(gnutls_hash_hd_t handle)
994
0
{
995
0
  gnutls_hash_hd_t dig;
996
997
0
  dig = gnutls_malloc(sizeof(digest_hd_st));
998
0
  if (dig == NULL) {
999
0
    gnutls_assert();
1000
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1001
0
    return NULL;
1002
0
  }
1003
1004
0
  if (_gnutls_hash_copy((const digest_hd_st *)handle,
1005
0
            (digest_hd_st *)dig) != GNUTLS_E_SUCCESS) {
1006
0
    gnutls_assert();
1007
0
    gnutls_free(dig);
1008
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1009
0
    return NULL;
1010
0
  }
1011
1012
0
  return dig;
1013
0
}
1014
1015
/**
1016
 * gnutls_hash_squeeze:
1017
 * @handle: a #gnutls_hash_hd_t
1018
 * @output: destination to store the output; must be equal to or larger than @length
1019
 * @length: length of @output
1020
 *
1021
 * This function will extract digest output of @length bytes. The @handle must
1022
 * be initialized with gnutls_hash_init() as an extended output function (XOF),
1023
 * such as %GNUTLS_DIG_SHAKE_128 or %GNUTLS_DIG_SHAKE_256.
1024
 *
1025
 * This function can be called multiple times. To reset the state of @handle,
1026
 * call gnutls_hash_deinit() with %NULL as the digest argument.
1027
 *
1028
 * Returns: %GNUTLS_E_SUCCESS (0) on success; negative error code otherwise.
1029
 *
1030
 * Since: 3.8.6
1031
 */
1032
int gnutls_hash_squeeze(gnutls_hash_hd_t handle, void *output, size_t length)
1033
0
{
1034
0
  return _gnutls_hash_squeeze((digest_hd_st *)handle, output, length);
1035
0
}
1036
1037
/**
1038
 * gnutls_key_generate:
1039
 * @key: is a pointer to a #gnutls_datum_t which will contain a newly
1040
 * created key
1041
 * @key_size: the number of bytes of the key
1042
 *
1043
 * Generates a random key of @key_size bytes.
1044
 *
1045
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, or an
1046
 * error code.
1047
 *
1048
 * Since: 3.0
1049
 **/
1050
int gnutls_key_generate(gnutls_datum_t *key, unsigned int key_size)
1051
0
{
1052
0
  int ret;
1053
0
  bool not_approved = false;
1054
1055
0
  FAIL_IF_LIB_ERROR;
1056
1057
#ifdef ENABLE_FIPS140
1058
  /* The FIPS140 approved RNGs are not allowed to be used
1059
   * to extract key sizes longer than their original seed.
1060
   */
1061
  if (_gnutls_fips_mode_enabled() != 0 && key_size > FIPS140_RND_KEY_SIZE)
1062
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1063
#endif
1064
1065
0
  key->size = key_size;
1066
0
  key->data = gnutls_malloc(key->size);
1067
0
  if (!key->data) {
1068
0
    gnutls_assert();
1069
0
    ret = GNUTLS_E_MEMORY_ERROR;
1070
0
    goto error;
1071
0
  }
1072
1073
  /* Key lengths of less than 112 bits are not approved */
1074
0
  if (key_size < 14) {
1075
0
    not_approved = true;
1076
0
  }
1077
1078
0
  ret = gnutls_rnd(GNUTLS_RND_RANDOM, key->data, key->size);
1079
0
  if (ret < 0) {
1080
0
    gnutls_assert();
1081
0
    _gnutls_free_datum(key);
1082
0
    goto error;
1083
0
  }
1084
1085
0
error:
1086
0
  if (ret < 0) {
1087
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1088
0
  } else if (not_approved) {
1089
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_NOT_APPROVED);
1090
0
  } else {
1091
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_APPROVED);
1092
0
  }
1093
0
  return ret;
1094
0
}
1095
1096
/* AEAD API */
1097
1098
/**
1099
 * gnutls_aead_cipher_init:
1100
 * @handle: is a #gnutls_aead_cipher_hd_t type.
1101
 * @cipher: the authenticated-encryption algorithm to use
1102
 * @key: The key to be used for encryption
1103
 *
1104
 * This function will initialize an context that can be used for
1105
 * encryption/decryption of data. This will effectively use the
1106
 * current crypto backend in use by gnutls or the cryptographic
1107
 * accelerator in use.
1108
 *
1109
 * Returns: Zero or a negative error code on error.
1110
 *
1111
 * Since: 3.4.0
1112
 **/
1113
int gnutls_aead_cipher_init(gnutls_aead_cipher_hd_t *handle,
1114
          gnutls_cipher_algorithm_t cipher,
1115
          const gnutls_datum_t *key)
1116
0
{
1117
0
  api_aead_cipher_hd_st *h;
1118
0
  const cipher_entry_st *e;
1119
0
  int ret;
1120
0
  bool not_approved = false;
1121
1122
0
  if (!is_cipher_algo_allowed(cipher)) {
1123
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1124
0
    return gnutls_assert_val(GNUTLS_E_UNWANTED_ALGORITHM);
1125
0
  } else if (!is_cipher_algo_approved_in_fips(cipher)) {
1126
0
    not_approved = true;
1127
0
  }
1128
1129
0
  e = cipher_to_entry(cipher);
1130
0
  if (e == NULL || e->type != CIPHER_AEAD) {
1131
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1132
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1133
0
  }
1134
1135
0
  h = gnutls_calloc(1, sizeof(api_aead_cipher_hd_st));
1136
0
  if (h == NULL) {
1137
0
    gnutls_assert();
1138
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1139
0
    return GNUTLS_E_MEMORY_ERROR;
1140
0
  }
1141
1142
0
  ret = _gnutls_aead_cipher_init(h, cipher, key);
1143
0
  if (ret < 0) {
1144
0
    gnutls_free(h);
1145
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1146
0
    return ret;
1147
0
  }
1148
1149
0
  *handle = h;
1150
1151
0
  if (not_approved) {
1152
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_NOT_APPROVED);
1153
0
  } else {
1154
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_APPROVED);
1155
0
  }
1156
1157
0
  return ret;
1158
0
}
1159
1160
/**
1161
 * gnutls_aead_cipher_set_key:
1162
 * @handle: is a #gnutls_aead_cipher_hd_t type.
1163
 * @key: The key to be used for encryption
1164
 *
1165
 * This function will set a new key without re-initializing the
1166
 * context.
1167
 *
1168
 * Returns: Zero or a negative error code on error.
1169
 *
1170
 * Since: 3.7.5
1171
 **/
1172
int gnutls_aead_cipher_set_key(gnutls_aead_cipher_hd_t handle,
1173
             const gnutls_datum_t *key)
1174
0
{
1175
0
  const cipher_entry_st *e;
1176
0
  int ret;
1177
1178
0
  e = cipher_to_entry(handle->ctx_enc.e->id);
1179
0
  if (e == NULL || e->type != CIPHER_AEAD) {
1180
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1181
0
  }
1182
1183
0
  ret = handle->ctx_enc.setkey(handle->ctx_enc.handle, key->data,
1184
0
             key->size);
1185
0
  if (ret < 0) {
1186
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1187
0
  }
1188
1189
0
  return ret;
1190
0
}
1191
1192
/**
1193
 * gnutls_aead_cipher_decrypt:
1194
 * @handle: is a #gnutls_aead_cipher_hd_t type.
1195
 * @nonce: the nonce to set
1196
 * @nonce_len: The length of the nonce
1197
 * @auth: additional data to be authenticated
1198
 * @auth_len: The length of the data
1199
 * @tag_size: The size of the tag to use (use zero for the default)
1200
 * @ctext: the data to decrypt (including the authentication tag)
1201
 * @ctext_len: the length of data to decrypt (includes tag size)
1202
 * @ptext: the decrypted data
1203
 * @ptext_len: the length of decrypted data (initially must hold the maximum available size)
1204
 *
1205
 * This function will decrypt the given data using the algorithm
1206
 * specified by the context. This function must be provided the complete
1207
 * data to be decrypted, including the authentication tag. On several
1208
 * AEAD ciphers, the authentication tag is appended to the ciphertext,
1209
 * though this is not a general rule. This function will fail if
1210
 * the tag verification fails.
1211
 *
1212
 * Returns: Zero or a negative error code on verification failure or other error.
1213
 *
1214
 * Since: 3.4.0
1215
 **/
1216
int gnutls_aead_cipher_decrypt(gnutls_aead_cipher_hd_t handle,
1217
             const void *nonce, size_t nonce_len,
1218
             const void *auth, size_t auth_len,
1219
             size_t tag_size, const void *ctext,
1220
             size_t ctext_len, void *ptext, size_t *ptext_len)
1221
0
{
1222
0
  int ret;
1223
0
  api_aead_cipher_hd_st *h = handle;
1224
1225
0
  if (tag_size == 0)
1226
0
    tag_size = _gnutls_cipher_get_tag_size(h->ctx_enc.e);
1227
0
  else if (tag_size >
1228
0
     (unsigned)_gnutls_cipher_get_tag_size(h->ctx_enc.e)) {
1229
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1230
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1231
0
  }
1232
1233
0
  if (unlikely(ctext_len < tag_size)) {
1234
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1235
0
    return gnutls_assert_val(GNUTLS_E_DECRYPTION_FAILED);
1236
0
  }
1237
1238
0
  ret = _gnutls_aead_cipher_decrypt(&h->ctx_enc, nonce, nonce_len, auth,
1239
0
            auth_len, tag_size, ctext, ctext_len,
1240
0
            ptext, *ptext_len);
1241
0
  if (unlikely(ret < 0)) {
1242
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1243
0
    return gnutls_assert_val(ret);
1244
0
  } else {
1245
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_APPROVED);
1246
0
  }
1247
1248
  /* That assumes that AEAD ciphers are stream */
1249
0
  *ptext_len = ctext_len - tag_size;
1250
1251
0
  return 0;
1252
0
}
1253
1254
/**
1255
 * gnutls_aead_cipher_encrypt:
1256
 * @handle: is a #gnutls_aead_cipher_hd_t type.
1257
 * @nonce: the nonce to set
1258
 * @nonce_len: The length of the nonce
1259
 * @auth: additional data to be authenticated
1260
 * @auth_len: The length of the data
1261
 * @tag_size: The size of the tag to use (use zero for the default)
1262
 * @ptext: the data to encrypt
1263
 * @ptext_len: The length of data to encrypt
1264
 * @ctext: the encrypted data including authentication tag
1265
 * @ctext_len: the length of encrypted data (initially must hold the maximum available size, including space for tag)
1266
 *
1267
 * This function will encrypt the given data using the algorithm
1268
 * specified by the context. The output data will contain the
1269
 * authentication tag.
1270
 *
1271
 * Returns: Zero or a negative error code on error.
1272
 *
1273
 * Since: 3.4.0
1274
 **/
1275
int gnutls_aead_cipher_encrypt(gnutls_aead_cipher_hd_t handle,
1276
             const void *nonce, size_t nonce_len,
1277
             const void *auth, size_t auth_len,
1278
             size_t tag_size, const void *ptext,
1279
             size_t ptext_len, void *ctext, size_t *ctext_len)
1280
0
{
1281
0
  api_aead_cipher_hd_st *h = handle;
1282
0
  int ret;
1283
1284
0
  if (tag_size == 0)
1285
0
    tag_size = _gnutls_cipher_get_tag_size(h->ctx_enc.e);
1286
0
  else if (tag_size >
1287
0
     (unsigned)_gnutls_cipher_get_tag_size(h->ctx_enc.e)) {
1288
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1289
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1290
0
  }
1291
1292
0
  if (unlikely(*ctext_len < ptext_len + tag_size)) {
1293
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1294
0
    return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER);
1295
0
  }
1296
1297
0
  ret = _gnutls_aead_cipher_encrypt(&h->ctx_enc, nonce, nonce_len, auth,
1298
0
            auth_len, tag_size, ptext, ptext_len,
1299
0
            ctext, *ctext_len);
1300
0
  if (unlikely(ret < 0)) {
1301
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1302
0
    return gnutls_assert_val(ret);
1303
0
  } else {
1304
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_APPROVED);
1305
0
  }
1306
1307
  /* That assumes that AEAD ciphers are stream */
1308
0
  *ctext_len = ptext_len + tag_size;
1309
1310
0
  return 0;
1311
0
}
1312
1313
struct iov_store_st {
1314
  void *data;
1315
  size_t length;
1316
  size_t capacity;
1317
};
1318
1319
static void iov_store_free(struct iov_store_st *s)
1320
0
{
1321
0
  gnutls_free(s->data);
1322
0
}
1323
1324
static int iov_store_grow(struct iov_store_st *s, size_t length)
1325
0
{
1326
0
  void *new_data;
1327
0
  size_t new_capacity = s->capacity;
1328
1329
0
  if (INT_ADD_OVERFLOW(new_capacity, length)) {
1330
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1331
0
  }
1332
0
  new_capacity += length;
1333
0
  new_data = gnutls_realloc(s->data, new_capacity);
1334
0
  if (!new_data) {
1335
0
    return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
1336
0
  }
1337
0
  s->data = new_data;
1338
0
  s->capacity = new_capacity;
1339
0
  return 0;
1340
0
}
1341
1342
static int append_from_iov(struct iov_store_st *dst, const giovec_t *iov,
1343
         int iovcnt)
1344
0
{
1345
0
  if (iovcnt > 0) {
1346
0
    int i;
1347
0
    uint8_t *p;
1348
0
    void *new_data;
1349
0
    size_t new_capacity = dst->capacity;
1350
1351
0
    for (i = 0; i < iovcnt; i++) {
1352
0
      if (INT_ADD_OVERFLOW(new_capacity, iov[i].iov_len)) {
1353
0
        return gnutls_assert_val(
1354
0
          GNUTLS_E_INVALID_REQUEST);
1355
0
      }
1356
0
      new_capacity += iov[i].iov_len;
1357
0
    }
1358
0
    new_data = gnutls_realloc(dst->data, new_capacity);
1359
0
    if (!new_data) {
1360
0
      return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
1361
0
    }
1362
0
    dst->data = new_data;
1363
0
    dst->capacity = new_capacity;
1364
1365
0
    p = (uint8_t *)dst->data + dst->length;
1366
0
    for (i = 0; i < iovcnt; i++) {
1367
0
      if (iov[i].iov_len > 0) {
1368
0
        memcpy(p, iov[i].iov_base, iov[i].iov_len);
1369
0
      }
1370
0
      p += iov[i].iov_len;
1371
0
      dst->length += iov[i].iov_len;
1372
0
    }
1373
0
  }
1374
0
  return 0;
1375
0
}
1376
1377
static int copy_to_iov(const uint8_t *data, size_t size, const giovec_t *iov,
1378
           int iovcnt)
1379
0
{
1380
0
  size_t offset = 0;
1381
0
  int i;
1382
1383
0
  for (i = 0; i < iovcnt && size > 0; i++) {
1384
0
    size_t to_copy = MIN(size, iov[i].iov_len);
1385
0
    memcpy(iov[i].iov_base, (uint8_t *)data + offset, to_copy);
1386
0
    offset += to_copy;
1387
0
    size -= to_copy;
1388
0
  }
1389
0
  if (size > 0)
1390
0
    return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER);
1391
0
  return 0;
1392
0
}
1393
1394
0
#define IOV_STORE_INIT { NULL, 0, 0 }
1395
1396
static int aead_cipher_encryptv_fallback(gnutls_aead_cipher_hd_t handle,
1397
           const void *nonce, size_t nonce_len,
1398
           const giovec_t *auth_iov,
1399
           int auth_iovcnt, size_t tag_size,
1400
           const giovec_t *iov, int iovcnt,
1401
           void *ctext, size_t *ctext_len)
1402
0
{
1403
0
  struct iov_store_st auth = IOV_STORE_INIT;
1404
0
  struct iov_store_st ptext = IOV_STORE_INIT;
1405
0
  int ret;
1406
1407
0
  if (tag_size == 0)
1408
0
    tag_size = _gnutls_cipher_get_tag_size(handle->ctx_enc.e);
1409
0
  else if (tag_size >
1410
0
     (unsigned)_gnutls_cipher_get_tag_size(handle->ctx_enc.e)) {
1411
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1412
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1413
0
  }
1414
1415
0
  ret = append_from_iov(&auth, auth_iov, auth_iovcnt);
1416
0
  if (ret < 0) {
1417
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1418
0
    return gnutls_assert_val(ret);
1419
0
  }
1420
1421
0
  ret = append_from_iov(&ptext, iov, iovcnt);
1422
0
  if (ret < 0) {
1423
0
    iov_store_free(&auth);
1424
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1425
0
    return gnutls_assert_val(ret);
1426
0
  }
1427
1428
0
  ret = gnutls_aead_cipher_encrypt(handle, nonce, nonce_len, auth.data,
1429
0
           auth.length, tag_size, ptext.data,
1430
0
           ptext.length, ctext, ctext_len);
1431
0
  iov_store_free(&auth);
1432
0
  iov_store_free(&ptext);
1433
1434
  /* FIPS operation state is set by gnutls_aead_cipher_encrypt */
1435
0
  return ret;
1436
0
}
1437
1438
static int aead_cipher_encryptv(gnutls_aead_cipher_hd_t handle,
1439
        const void *nonce, size_t nonce_len,
1440
        const giovec_t *auth_iov, int auth_iovcnt,
1441
        size_t tag_size, const giovec_t *iov,
1442
        int iovcnt, void *ctext, size_t *ctext_len)
1443
0
{
1444
0
  int ret;
1445
0
  uint8_t *dst;
1446
0
  size_t dst_size, total = 0;
1447
0
  uint8_t *p;
1448
0
  size_t len;
1449
0
  size_t blocksize = handle->ctx_enc.e->blocksize;
1450
0
  struct iov_iter_st iter;
1451
1452
0
  if (tag_size == 0)
1453
0
    tag_size = _gnutls_cipher_get_tag_size(handle->ctx_enc.e);
1454
0
  else if (tag_size >
1455
0
     (unsigned)_gnutls_cipher_get_tag_size(handle->ctx_enc.e)) {
1456
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1457
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1458
0
  }
1459
1460
0
  ret = _gnutls_cipher_setiv(&handle->ctx_enc, nonce, nonce_len);
1461
0
  if (unlikely(ret < 0)) {
1462
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1463
0
    return gnutls_assert_val(ret);
1464
0
  }
1465
1466
0
  ret = _gnutls_iov_iter_init(&iter, auth_iov, auth_iovcnt, blocksize);
1467
0
  if (unlikely(ret < 0)) {
1468
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1469
0
    return gnutls_assert_val(ret);
1470
0
  }
1471
0
  while (1) {
1472
0
    ret = _gnutls_iov_iter_next(&iter, &p);
1473
0
    if (unlikely(ret < 0)) {
1474
0
      _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1475
0
      return gnutls_assert_val(ret);
1476
0
    }
1477
0
    if (ret == 0)
1478
0
      break;
1479
0
    ret = _gnutls_cipher_auth(&handle->ctx_enc, p, ret);
1480
0
    if (unlikely(ret < 0)) {
1481
0
      _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1482
0
      return gnutls_assert_val(ret);
1483
0
    }
1484
0
  }
1485
1486
0
  dst = ctext;
1487
0
  dst_size = *ctext_len;
1488
1489
0
  ret = _gnutls_iov_iter_init(&iter, iov, iovcnt, blocksize);
1490
0
  if (unlikely(ret < 0)) {
1491
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1492
0
    return gnutls_assert_val(ret);
1493
0
  }
1494
0
  while (1) {
1495
0
    ret = _gnutls_iov_iter_next(&iter, &p);
1496
0
    if (unlikely(ret < 0)) {
1497
0
      _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1498
0
      return gnutls_assert_val(ret);
1499
0
    }
1500
0
    if (ret == 0)
1501
0
      break;
1502
0
    len = ret;
1503
0
    ret = _gnutls_cipher_encrypt2(&handle->ctx_enc, p, len, dst,
1504
0
                dst_size);
1505
0
    if (unlikely(ret < 0)) {
1506
0
      _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1507
0
      return gnutls_assert_val(ret);
1508
0
    }
1509
1510
0
    DECR_LEN(dst_size, len);
1511
0
    dst += len;
1512
0
    total += len;
1513
0
  }
1514
1515
0
  if (dst_size < tag_size) {
1516
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1517
0
    return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER);
1518
0
  }
1519
1520
0
  _gnutls_cipher_tag(&handle->ctx_enc, dst, tag_size);
1521
1522
0
  total += tag_size;
1523
0
  *ctext_len = total;
1524
1525
0
  _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_APPROVED);
1526
0
  return 0;
1527
0
}
1528
1529
/**
1530
 * gnutls_aead_cipher_encryptv:
1531
 * @handle: is a #gnutls_aead_cipher_hd_t type.
1532
 * @nonce: the nonce to set
1533
 * @nonce_len: The length of the nonce
1534
 * @auth_iov: additional data to be authenticated
1535
 * @auth_iovcnt: The number of buffers in @auth_iov
1536
 * @tag_size: The size of the tag to use (use zero for the default)
1537
 * @iov: the data to be encrypted
1538
 * @iovcnt: The number of buffers in @iov
1539
 * @ctext: the encrypted data including authentication tag
1540
 * @ctext_len: the length of encrypted data (initially must hold the maximum available size, including space for tag)
1541
 *
1542
 * This function will encrypt the provided data buffers using the algorithm
1543
 * specified by the context. The output data will contain the
1544
 * authentication tag.
1545
 *
1546
 * Returns: Zero or a negative error code on error.
1547
 *
1548
 * Since: 3.6.3
1549
 **/
1550
int gnutls_aead_cipher_encryptv(gnutls_aead_cipher_hd_t handle,
1551
        const void *nonce, size_t nonce_len,
1552
        const giovec_t *auth_iov, int auth_iovcnt,
1553
        size_t tag_size, const giovec_t *iov,
1554
        int iovcnt, void *ctext, size_t *ctext_len)
1555
0
{
1556
  /* Limitation: this function provides an optimization under the internally registered
1557
   * AEAD ciphers. When an AEAD cipher is used registered with gnutls_crypto_register_aead_cipher(),
1558
   * then this becomes a convenience function as it missed the lower-level primitives
1559
   * necessary for piecemeal encryption. */
1560
0
  if ((handle->ctx_enc.e->flags & GNUTLS_CIPHER_FLAG_ONLY_AEAD) ||
1561
0
      handle->ctx_enc.encrypt == NULL) {
1562
0
    return aead_cipher_encryptv_fallback(handle, nonce, nonce_len,
1563
0
                 auth_iov, auth_iovcnt,
1564
0
                 tag_size, iov, iovcnt,
1565
0
                 ctext, ctext_len);
1566
0
  } else {
1567
0
    return aead_cipher_encryptv(handle, nonce, nonce_len, auth_iov,
1568
0
              auth_iovcnt, tag_size, iov, iovcnt,
1569
0
              ctext, ctext_len);
1570
0
  }
1571
0
}
1572
1573
static int aead_cipher_encryptv2_fallback(gnutls_aead_cipher_hd_t handle,
1574
            const void *nonce, size_t nonce_len,
1575
            const giovec_t *auth_iov,
1576
            int auth_iovcnt, const giovec_t *iov,
1577
            int iovcnt, void *tag,
1578
            size_t *tag_size)
1579
0
{
1580
0
  struct iov_store_st auth = IOV_STORE_INIT;
1581
0
  struct iov_store_st ptext = IOV_STORE_INIT;
1582
0
  uint8_t *ptext_data;
1583
0
  size_t ptext_size;
1584
0
  uint8_t *ctext_data;
1585
0
  size_t ctext_size;
1586
0
  uint8_t *_tag;
1587
0
  size_t _tag_size;
1588
0
  int ret;
1589
1590
0
  if (tag_size == NULL || *tag_size == 0)
1591
0
    _tag_size = _gnutls_cipher_get_tag_size(handle->ctx_enc.e);
1592
0
  else
1593
0
    _tag_size = *tag_size;
1594
1595
0
  if (_tag_size >
1596
0
      (unsigned)_gnutls_cipher_get_tag_size(handle->ctx_enc.e)) {
1597
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1598
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1599
0
  }
1600
1601
0
  ret = append_from_iov(&auth, auth_iov, auth_iovcnt);
1602
0
  if (ret < 0) {
1603
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1604
0
    return gnutls_assert_val(ret);
1605
0
  }
1606
1607
0
  if (handle->ctx_enc.e->flags & GNUTLS_CIPHER_FLAG_TAG_PREFIXED) {
1608
    /* prepend space for tag */
1609
0
    ret = iov_store_grow(&ptext, _tag_size);
1610
0
    if (ret < 0) {
1611
0
      gnutls_assert();
1612
0
      goto error;
1613
0
    }
1614
0
    ptext.length = _tag_size;
1615
1616
0
    ret = append_from_iov(&ptext, iov, iovcnt);
1617
0
    if (ret < 0) {
1618
0
      gnutls_assert();
1619
0
      goto error;
1620
0
    }
1621
1622
    /* We must set ptext_data after the above
1623
     * grow/append operations, otherwise it will point to an invalid pointer after realloc.
1624
     */
1625
0
    ptext_data = (uint8_t *)ptext.data + _tag_size;
1626
0
    ptext_size = ptext.length - _tag_size;
1627
0
  } else {
1628
0
    ret = append_from_iov(&ptext, iov, iovcnt);
1629
0
    if (ret < 0) {
1630
0
      gnutls_assert();
1631
0
      goto error;
1632
0
    }
1633
1634
    /* append space for tag */
1635
0
    ret = iov_store_grow(&ptext, _tag_size);
1636
0
    if (ret < 0) {
1637
0
      gnutls_assert();
1638
0
      goto error;
1639
0
    }
1640
1641
    /* We must set ptext_data after the above
1642
     * grow/append operations, otherwise it will point to an invalid pointer after realloc.
1643
     */
1644
0
    ptext_data = ptext.data;
1645
0
    ptext_size = ptext.length;
1646
0
  }
1647
1648
0
  ctext_size = ptext.capacity;
1649
0
  ret = gnutls_aead_cipher_encrypt(handle, nonce, nonce_len, auth.data,
1650
0
           auth.length, _tag_size, ptext_data,
1651
0
           ptext_size, ptext.data, &ctext_size);
1652
0
  if (ret < 0) {
1653
0
    gnutls_assert();
1654
0
    goto error;
1655
0
  }
1656
1657
0
  if (handle->ctx_enc.e->flags & GNUTLS_CIPHER_FLAG_TAG_PREFIXED) {
1658
0
    ctext_data = (uint8_t *)ptext.data + _tag_size;
1659
0
    _tag = ptext.data;
1660
0
  } else {
1661
0
    ctext_data = ptext.data;
1662
0
    _tag = (uint8_t *)ptext.data + ptext_size;
1663
0
  }
1664
1665
0
  ret = copy_to_iov(ctext_data, ptext_size, iov, iovcnt);
1666
0
  if (ret < 0) {
1667
0
    gnutls_assert();
1668
0
    goto error;
1669
0
  }
1670
1671
0
  if (tag != NULL) {
1672
0
    memcpy(tag, _tag, _tag_size);
1673
0
  }
1674
0
  if (tag_size != NULL) {
1675
0
    *tag_size = _tag_size;
1676
0
  }
1677
1678
0
error:
1679
0
  iov_store_free(&auth);
1680
0
  iov_store_free(&ptext);
1681
1682
0
  if (ret < 0) {
1683
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1684
0
  }
1685
  /* FIPS operation state is set by gnutls_aead_cipher_encrypt */
1686
0
  return ret;
1687
0
}
1688
1689
static int aead_cipher_encryptv2(gnutls_aead_cipher_hd_t handle,
1690
         const void *nonce, size_t nonce_len,
1691
         const giovec_t *auth_iov, int auth_iovcnt,
1692
         const giovec_t *iov, int iovcnt, void *tag,
1693
         size_t *tag_size)
1694
0
{
1695
0
  api_aead_cipher_hd_st *h = handle;
1696
0
  int ret;
1697
0
  uint8_t *p;
1698
0
  size_t len;
1699
0
  size_t blocksize = handle->ctx_enc.e->blocksize;
1700
0
  struct iov_iter_st iter;
1701
0
  size_t _tag_size;
1702
1703
0
  if (tag_size == NULL || *tag_size == 0)
1704
0
    _tag_size = _gnutls_cipher_get_tag_size(h->ctx_enc.e);
1705
0
  else
1706
0
    _tag_size = *tag_size;
1707
1708
0
  if (_tag_size > (unsigned)_gnutls_cipher_get_tag_size(h->ctx_enc.e)) {
1709
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1710
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1711
0
  }
1712
1713
0
  ret = _gnutls_cipher_setiv(&handle->ctx_enc, nonce, nonce_len);
1714
0
  if (unlikely(ret < 0)) {
1715
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1716
0
    return gnutls_assert_val(ret);
1717
0
  }
1718
1719
0
  ret = _gnutls_iov_iter_init(&iter, auth_iov, auth_iovcnt, blocksize);
1720
0
  if (unlikely(ret < 0)) {
1721
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1722
0
    return gnutls_assert_val(ret);
1723
0
  }
1724
0
  while (1) {
1725
0
    ret = _gnutls_iov_iter_next(&iter, &p);
1726
0
    if (unlikely(ret < 0)) {
1727
0
      _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1728
0
      return gnutls_assert_val(ret);
1729
0
    }
1730
0
    if (ret == 0)
1731
0
      break;
1732
0
    ret = _gnutls_cipher_auth(&handle->ctx_enc, p, ret);
1733
0
    if (unlikely(ret < 0)) {
1734
0
      _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1735
0
      return gnutls_assert_val(ret);
1736
0
    }
1737
0
  }
1738
1739
0
  ret = _gnutls_iov_iter_init(&iter, iov, iovcnt, blocksize);
1740
0
  if (unlikely(ret < 0))
1741
0
    return gnutls_assert_val(ret);
1742
0
  while (1) {
1743
0
    ret = _gnutls_iov_iter_next(&iter, &p);
1744
0
    if (unlikely(ret < 0)) {
1745
0
      _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1746
0
      return gnutls_assert_val(ret);
1747
0
    }
1748
0
    if (ret == 0)
1749
0
      break;
1750
1751
0
    len = ret;
1752
0
    ret = _gnutls_cipher_encrypt2(&handle->ctx_enc, p, len, p, len);
1753
0
    if (unlikely(ret < 0)) {
1754
0
      _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1755
0
      return gnutls_assert_val(ret);
1756
0
    }
1757
1758
0
    ret = _gnutls_iov_iter_sync(&iter, p, len);
1759
0
    if (unlikely(ret < 0)) {
1760
0
      _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1761
0
      return gnutls_assert_val(ret);
1762
0
    }
1763
0
  }
1764
1765
0
  if (tag != NULL)
1766
0
    _gnutls_cipher_tag(&handle->ctx_enc, tag, _tag_size);
1767
0
  if (tag_size != NULL)
1768
0
    *tag_size = _tag_size;
1769
1770
0
  _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_APPROVED);
1771
0
  return 0;
1772
0
}
1773
1774
/**
1775
 * gnutls_aead_cipher_encryptv2:
1776
 * @handle: is a #gnutls_aead_cipher_hd_t type.
1777
 * @nonce: the nonce to set
1778
 * @nonce_len: The length of the nonce
1779
 * @auth_iov: additional data to be authenticated
1780
 * @auth_iovcnt: The number of buffers in @auth_iov
1781
 * @iov: the data to be encrypted
1782
 * @iovcnt: The number of buffers in @iov
1783
 * @tag: The authentication tag
1784
 * @tag_size: The size of the tag to use (use zero for the default)
1785
 *
1786
 * This is similar to gnutls_aead_cipher_encrypt(), but it performs
1787
 * in-place encryption on the provided data buffers.
1788
 *
1789
 * Returns: Zero or a negative error code on error.
1790
 *
1791
 * Since: 3.6.10
1792
 **/
1793
int gnutls_aead_cipher_encryptv2(gnutls_aead_cipher_hd_t handle,
1794
         const void *nonce, size_t nonce_len,
1795
         const giovec_t *auth_iov, int auth_iovcnt,
1796
         const giovec_t *iov, int iovcnt, void *tag,
1797
         size_t *tag_size)
1798
0
{
1799
  /* Limitation: this function provides an optimization under the internally registered
1800
   * AEAD ciphers. When an AEAD cipher is used registered with gnutls_crypto_register_aead_cipher(),
1801
   * then this becomes a convenience function as it missed the lower-level primitives
1802
   * necessary for piecemeal encryption. */
1803
0
  if ((handle->ctx_enc.e->flags & GNUTLS_CIPHER_FLAG_ONLY_AEAD) ||
1804
0
      handle->ctx_enc.encrypt == NULL) {
1805
0
    return aead_cipher_encryptv2_fallback(handle, nonce, nonce_len,
1806
0
                  auth_iov, auth_iovcnt,
1807
0
                  iov, iovcnt, tag,
1808
0
                  tag_size);
1809
0
  } else {
1810
0
    return aead_cipher_encryptv2(handle, nonce, nonce_len, auth_iov,
1811
0
               auth_iovcnt, iov, iovcnt, tag,
1812
0
               tag_size);
1813
0
  }
1814
0
}
1815
1816
static int aead_cipher_decryptv2_fallback(gnutls_aead_cipher_hd_t handle,
1817
            const void *nonce, size_t nonce_len,
1818
            const giovec_t *auth_iov,
1819
            int auth_iovcnt, const giovec_t *iov,
1820
            int iovcnt, void *tag,
1821
            size_t tag_size)
1822
0
{
1823
0
  struct iov_store_st auth = IOV_STORE_INIT;
1824
0
  struct iov_store_st ctext = IOV_STORE_INIT;
1825
0
  uint8_t *ctext_data;
1826
0
  size_t ptext_size;
1827
0
  int ret;
1828
1829
0
  if (tag_size == 0)
1830
0
    tag_size = _gnutls_cipher_get_tag_size(handle->ctx_enc.e);
1831
0
  else if (tag_size >
1832
0
     (unsigned)_gnutls_cipher_get_tag_size(handle->ctx_enc.e)) {
1833
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1834
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1835
0
  }
1836
1837
0
  ret = append_from_iov(&auth, auth_iov, auth_iovcnt);
1838
0
  if (ret < 0) {
1839
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1840
0
    return gnutls_assert_val(ret);
1841
0
  }
1842
1843
0
  if (handle->ctx_enc.e->flags & GNUTLS_CIPHER_FLAG_TAG_PREFIXED) {
1844
    /* prepend tag */
1845
0
    ret = iov_store_grow(&ctext, tag_size);
1846
0
    if (ret < 0) {
1847
0
      gnutls_assert();
1848
0
      goto error;
1849
0
    }
1850
0
    memcpy(ctext.data, tag, tag_size);
1851
0
    ctext.length += tag_size;
1852
1853
0
    ret = append_from_iov(&ctext, iov, iovcnt);
1854
0
    if (ret < 0) {
1855
0
      gnutls_assert();
1856
0
      goto error;
1857
0
    }
1858
1859
    /* We must set ctext_data after the above
1860
     * grow/append operations, otherwise it will point to an invalid pointer after realloc.
1861
     */
1862
0
    ctext_data = (uint8_t *)ctext.data + tag_size;
1863
0
  } else {
1864
0
    ret = append_from_iov(&ctext, iov, iovcnt);
1865
0
    if (ret < 0) {
1866
0
      gnutls_assert();
1867
0
      goto error;
1868
0
    }
1869
1870
    /* append tag */
1871
0
    ret = iov_store_grow(&ctext, tag_size);
1872
0
    if (ret < 0) {
1873
0
      gnutls_assert();
1874
0
      goto error;
1875
0
    }
1876
0
    memcpy((uint8_t *)ctext.data + ctext.length, tag, tag_size);
1877
0
    ctext.length += tag_size;
1878
1879
    /* We must set ctext_data after the above
1880
     * grow/append operations, otherwise it will point to an invalid pointer after realloc.
1881
     */
1882
0
    ctext_data = ctext.data;
1883
0
  }
1884
1885
0
  ptext_size = ctext.capacity;
1886
0
  ret = gnutls_aead_cipher_decrypt(handle, nonce, nonce_len, auth.data,
1887
0
           auth.length, tag_size, ctext.data,
1888
0
           ctext.length, ctext_data, &ptext_size);
1889
0
  if (ret < 0) {
1890
0
    gnutls_assert();
1891
0
    goto error;
1892
0
  }
1893
1894
0
  ret = copy_to_iov(ctext.data, ptext_size, iov, iovcnt);
1895
0
  if (ret < 0) {
1896
0
    gnutls_assert();
1897
0
    goto error;
1898
0
  }
1899
1900
0
error:
1901
0
  iov_store_free(&auth);
1902
0
  iov_store_free(&ctext);
1903
1904
0
  if (ret < 0) {
1905
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1906
0
  }
1907
  /* FIPS operation state is set by gnutls_aead_cipher_decrypt */
1908
0
  return ret;
1909
0
}
1910
1911
static int aead_cipher_decryptv2(gnutls_aead_cipher_hd_t handle,
1912
         const void *nonce, size_t nonce_len,
1913
         const giovec_t *auth_iov, int auth_iovcnt,
1914
         const giovec_t *iov, int iovcnt, void *tag,
1915
         size_t tag_size)
1916
0
{
1917
0
  int ret;
1918
0
  uint8_t *p;
1919
0
  size_t len;
1920
0
  ssize_t blocksize = handle->ctx_enc.e->blocksize;
1921
0
  struct iov_iter_st iter;
1922
0
  uint8_t _tag[MAX_HASH_SIZE];
1923
1924
0
  if (tag_size == 0)
1925
0
    tag_size = _gnutls_cipher_get_tag_size(handle->ctx_enc.e);
1926
0
  else if (tag_size >
1927
0
     (unsigned)_gnutls_cipher_get_tag_size(handle->ctx_enc.e)) {
1928
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1929
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1930
0
  }
1931
1932
0
  ret = _gnutls_cipher_setiv(&handle->ctx_enc, nonce, nonce_len);
1933
0
  if (unlikely(ret < 0)) {
1934
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1935
0
    return gnutls_assert_val(ret);
1936
0
  }
1937
1938
0
  ret = _gnutls_iov_iter_init(&iter, auth_iov, auth_iovcnt, blocksize);
1939
0
  if (unlikely(ret < 0)) {
1940
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1941
0
    return gnutls_assert_val(ret);
1942
0
  }
1943
0
  while (1) {
1944
0
    ret = _gnutls_iov_iter_next(&iter, &p);
1945
0
    if (unlikely(ret < 0)) {
1946
0
      _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1947
0
      return gnutls_assert_val(ret);
1948
0
    }
1949
0
    if (ret == 0)
1950
0
      break;
1951
0
    ret = _gnutls_cipher_auth(&handle->ctx_enc, p, ret);
1952
0
    if (unlikely(ret < 0)) {
1953
0
      _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1954
0
      return gnutls_assert_val(ret);
1955
0
    }
1956
0
  }
1957
1958
0
  ret = _gnutls_iov_iter_init(&iter, iov, iovcnt, blocksize);
1959
0
  if (unlikely(ret < 0)) {
1960
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1961
0
    return gnutls_assert_val(ret);
1962
0
  }
1963
0
  while (1) {
1964
0
    ret = _gnutls_iov_iter_next(&iter, &p);
1965
0
    if (unlikely(ret < 0)) {
1966
0
      _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1967
0
      return gnutls_assert_val(ret);
1968
0
    }
1969
0
    if (ret == 0)
1970
0
      break;
1971
1972
0
    len = ret;
1973
0
    ret = _gnutls_cipher_decrypt2(&handle->ctx_enc, p, len, p, len);
1974
0
    if (unlikely(ret < 0)) {
1975
0
      _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1976
0
      return gnutls_assert_val(ret);
1977
0
    }
1978
1979
0
    ret = _gnutls_iov_iter_sync(&iter, p, len);
1980
0
    if (unlikely(ret < 0)) {
1981
0
      _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1982
0
      return gnutls_assert_val(ret);
1983
0
    }
1984
0
  }
1985
1986
0
  if (tag != NULL) {
1987
0
    _gnutls_cipher_tag(&handle->ctx_enc, _tag, tag_size);
1988
0
    if (gnutls_memcmp(_tag, tag, tag_size) != 0) {
1989
0
      _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1990
0
      return gnutls_assert_val(GNUTLS_E_DECRYPTION_FAILED);
1991
0
    }
1992
0
  }
1993
1994
0
  _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_APPROVED);
1995
0
  return 0;
1996
0
}
1997
1998
/**
1999
 * gnutls_aead_cipher_decryptv2:
2000
 * @handle: is a #gnutls_aead_cipher_hd_t type.
2001
 * @nonce: the nonce to set
2002
 * @nonce_len: The length of the nonce
2003
 * @auth_iov: additional data to be authenticated
2004
 * @auth_iovcnt: The number of buffers in @auth_iov
2005
 * @iov: the data to decrypt
2006
 * @iovcnt: The number of buffers in @iov
2007
 * @tag: The authentication tag
2008
 * @tag_size: The size of the tag to use (use zero for the default)
2009
 *
2010
 * This is similar to gnutls_aead_cipher_decrypt(), but it performs
2011
 * in-place encryption on the provided data buffers.
2012
 *
2013
 * Returns: Zero or a negative error code on error.
2014
 *
2015
 * Since: 3.6.10
2016
 **/
2017
int gnutls_aead_cipher_decryptv2(gnutls_aead_cipher_hd_t handle,
2018
         const void *nonce, size_t nonce_len,
2019
         const giovec_t *auth_iov, int auth_iovcnt,
2020
         const giovec_t *iov, int iovcnt, void *tag,
2021
         size_t tag_size)
2022
0
{
2023
  /* Limitation: this function provides an optimization under the internally registered
2024
   * AEAD ciphers. When an AEAD cipher is used registered with gnutls_crypto_register_aead_cipher(),
2025
   * then this becomes a convenience function as it missed the lower-level primitives
2026
   * necessary for piecemeal encryption. */
2027
0
  if ((handle->ctx_enc.e->flags & GNUTLS_CIPHER_FLAG_ONLY_AEAD) ||
2028
0
      handle->ctx_enc.encrypt == NULL) {
2029
0
    return aead_cipher_decryptv2_fallback(handle, nonce, nonce_len,
2030
0
                  auth_iov, auth_iovcnt,
2031
0
                  iov, iovcnt, tag,
2032
0
                  tag_size);
2033
0
  } else {
2034
0
    return aead_cipher_decryptv2(handle, nonce, nonce_len, auth_iov,
2035
0
               auth_iovcnt, iov, iovcnt, tag,
2036
0
               tag_size);
2037
0
  }
2038
0
}
2039
2040
/**
2041
 * gnutls_aead_cipher_deinit:
2042
 * @handle: is a #gnutls_aead_cipher_hd_t type.
2043
 *
2044
 * This function will deinitialize all resources occupied by the given
2045
 * authenticated-encryption context.
2046
 *
2047
 * Since: 3.4.0
2048
 **/
2049
void gnutls_aead_cipher_deinit(gnutls_aead_cipher_hd_t handle)
2050
0
{
2051
0
  _gnutls_aead_cipher_deinit(handle);
2052
0
  gnutls_free(handle);
2053
0
}
2054
2055
extern gnutls_crypto_kdf_st _gnutls_kdf_ops;
2056
2057
/* Same as @gnutls_hkdf_extract but without changing FIPS context */
2058
int _gnutls_hkdf_extract(gnutls_mac_algorithm_t mac, const gnutls_datum_t *key,
2059
       const gnutls_datum_t *salt, void *output)
2060
0
{
2061
  /* MD5 is only allowed internally for TLS */
2062
0
  if (!is_mac_algo_allowed(mac)) {
2063
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
2064
0
    return gnutls_assert_val(GNUTLS_E_UNWANTED_ALGORITHM);
2065
0
  }
2066
2067
  /* We don't check whether MAC is approved, because HKDF is
2068
   * only approved in TLS, which is handled separately. */
2069
2070
0
  return _gnutls_kdf_ops.hkdf_extract(mac, key->data, key->size,
2071
0
              salt ? salt->data : NULL,
2072
0
              salt ? salt->size : 0, output);
2073
0
}
2074
2075
/**
2076
 * gnutls_hkdf_extract:
2077
 * @mac: the mac algorithm used internally
2078
 * @key: the initial keying material
2079
 * @salt: the optional salt
2080
 * @output: the output value of the extract operation
2081
 *
2082
 * This function will derive a fixed-size key using the HKDF-Extract
2083
 * function as defined in RFC 5869.
2084
 *
2085
 * Returns: Zero or a negative error code on error.
2086
 *
2087
 * Since: 3.6.13
2088
 */
2089
int gnutls_hkdf_extract(gnutls_mac_algorithm_t mac, const gnutls_datum_t *key,
2090
      const gnutls_datum_t *salt, void *output)
2091
0
{
2092
0
  int ret;
2093
2094
0
  ret = _gnutls_hkdf_extract(mac, key, salt, output);
2095
0
  if (ret < 0)
2096
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
2097
0
  else
2098
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_NOT_APPROVED);
2099
2100
0
  return ret;
2101
0
}
2102
2103
/* Same as @gnutls_hkdf_expand but without changing FIPS context */
2104
int _gnutls_hkdf_expand(gnutls_mac_algorithm_t mac, const gnutls_datum_t *key,
2105
      const gnutls_datum_t *info, void *output, size_t length)
2106
0
{
2107
  /* MD5 is only allowed internally for TLS */
2108
0
  if (!is_mac_algo_allowed(mac)) {
2109
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
2110
0
    return gnutls_assert_val(GNUTLS_E_UNWANTED_ALGORITHM);
2111
0
  }
2112
2113
  /* We don't check whether MAC is approved, because HKDF is
2114
   * only approved in TLS, which is handled separately. */
2115
2116
0
  return _gnutls_kdf_ops.hkdf_expand(mac, key->data, key->size,
2117
0
             info->data, info->size, output,
2118
0
             length);
2119
0
}
2120
2121
/**
2122
 * gnutls_hkdf_expand:
2123
 * @mac: the mac algorithm used internally
2124
 * @key: the pseudorandom key created with HKDF-Extract
2125
 * @info: the optional informational data
2126
 * @output: the output value of the expand operation
2127
 * @length: the desired length of the output key
2128
 *
2129
 * This function will derive a variable length keying material from
2130
 * the pseudorandom key using the HKDF-Expand function as defined in
2131
 * RFC 5869.
2132
 *
2133
 * Returns: Zero or a negative error code on error.
2134
 *
2135
 * Since: 3.6.13
2136
 */
2137
int gnutls_hkdf_expand(gnutls_mac_algorithm_t mac, const gnutls_datum_t *key,
2138
           const gnutls_datum_t *info, void *output, size_t length)
2139
0
{
2140
0
  int ret;
2141
2142
0
  ret = _gnutls_hkdf_expand(mac, key, info, output, length);
2143
0
  if (ret < 0)
2144
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
2145
0
  else
2146
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_NOT_APPROVED);
2147
2148
0
  return ret;
2149
0
}
2150
2151
/**
2152
 * gnutls_pbkdf2:
2153
 * @mac: the mac algorithm used internally
2154
 * @key: the initial keying material
2155
 * @salt: the salt
2156
 * @iter_count: the iteration count
2157
 * @output: the output value
2158
 * @length: the desired length of the output key
2159
 *
2160
 * This function will derive a variable length keying material from
2161
 * a password according to PKCS #5 PBKDF2.
2162
 *
2163
 * Returns: Zero or a negative error code on error.
2164
 *
2165
 * Since: 3.6.13
2166
 */
2167
int gnutls_pbkdf2(gnutls_mac_algorithm_t mac, const gnutls_datum_t *key,
2168
      const gnutls_datum_t *salt, unsigned iter_count, void *output,
2169
      size_t length)
2170
0
{
2171
0
  int ret;
2172
0
  bool not_approved = false;
2173
2174
  /* MD5 is only allowed internally for TLS */
2175
0
  if (!is_mac_algo_allowed(mac)) {
2176
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
2177
0
    return gnutls_assert_val(GNUTLS_E_UNWANTED_ALGORITHM);
2178
0
  } else if (!is_mac_algo_hmac_approved_in_fips(mac)) {
2179
    /* ACVP only allows HMAC used with PBKDF2:
2180
     * https://pages.nist.gov/ACVP/draft-celi-acvp-pbkdf.html
2181
     */
2182
0
    not_approved = true;
2183
0
  }
2184
2185
  /* Key lengths and output sizes of less than 112 bits are not approved */
2186
0
  if (key->size < 14 || length < 14) {
2187
0
    not_approved = true;
2188
0
  }
2189
2190
  /* Minimum salt length of 128 bits (SP 800-132 5.1) */
2191
0
  if (salt->size < 16) {
2192
0
    not_approved = true;
2193
0
  }
2194
2195
  /* Minimum iterations bound (SP 800-132 5.2) */
2196
0
  if (iter_count < 1000) {
2197
0
    not_approved = true;
2198
0
  }
2199
2200
0
  ret = _gnutls_kdf_ops.pbkdf2(mac, key->data, key->size, salt->data,
2201
0
             salt->size, iter_count, output, length);
2202
0
  if (ret < 0) {
2203
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
2204
0
  } else if (not_approved) {
2205
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_NOT_APPROVED);
2206
0
  } else {
2207
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_APPROVED);
2208
0
  }
2209
0
  return ret;
2210
0
}