Coverage Report

Created: 2024-06-20 06:28

/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
899
 * and reset the state of the hash.
900
 *
901
 * Since: 2.10.0
902
 **/
903
void gnutls_hash_output(gnutls_hash_hd_t handle, void *digest)
904
0
{
905
0
  _gnutls_hash_output((digest_hd_st *)handle, digest);
906
0
}
907
908
/**
909
 * gnutls_hash_deinit:
910
 * @handle: is a #gnutls_hash_hd_t type
911
 * @digest: is the output value of the hash
912
 *
913
 * This function will deinitialize all resources occupied by
914
 * the given hash context.
915
 *
916
 * Since: 2.10.0
917
 **/
918
void gnutls_hash_deinit(gnutls_hash_hd_t handle, void *digest)
919
0
{
920
0
  _gnutls_hash_deinit((digest_hd_st *)handle, digest);
921
0
  gnutls_free(handle);
922
0
}
923
924
/**
925
 * gnutls_hash_get_len:
926
 * @algorithm: the hash algorithm to use
927
 *
928
 * This function will return the length of the output data
929
 * of the given hash algorithm.
930
 *
931
 * Returns: The length or zero on error.
932
 *
933
 * Since: 2.10.0
934
 **/
935
unsigned gnutls_hash_get_len(gnutls_digest_algorithm_t algorithm)
936
0
{
937
0
  return _gnutls_hash_get_algo_len(hash_to_entry(algorithm));
938
0
}
939
940
/**
941
 * gnutls_hash_fast:
942
 * @algorithm: the hash algorithm to use
943
 * @ptext: the data to hash
944
 * @ptext_len: the length of data to hash
945
 * @digest: is the output value of the hash
946
 *
947
 * This convenience function will hash the given data and return output
948
 * on a single call.
949
 *
950
 * Returns: Zero or a negative error code on error.
951
 *
952
 * Since: 2.10.0
953
 **/
954
int gnutls_hash_fast(gnutls_digest_algorithm_t algorithm, const void *ptext,
955
         size_t ptext_len, void *digest)
956
0
{
957
0
  int ret;
958
0
  bool not_approved = false;
959
960
0
  if (!is_mac_algo_allowed(DIG_TO_MAC(algorithm))) {
961
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
962
0
    return gnutls_assert_val(GNUTLS_E_UNWANTED_ALGORITHM);
963
0
  } else if (!is_mac_algo_approved_in_fips(DIG_TO_MAC(algorithm))) {
964
0
    not_approved = true;
965
0
  }
966
967
0
  ret = _gnutls_hash_fast(algorithm, ptext, ptext_len, digest);
968
0
  if (ret < 0) {
969
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
970
0
  } else if (not_approved) {
971
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_NOT_APPROVED);
972
0
  }
973
974
0
  return ret;
975
0
}
976
977
/**
978
 * gnutls_hash_copy:
979
 * @handle: is a #gnutls_hash_hd_t type
980
 *
981
 * This function will create a copy of Message Digest context, containing all
982
 * its current state. Copying contexts for Message Digests registered using
983
 * gnutls_crypto_register_digest() is not supported and will always result in
984
 * an error. In addition to that, some of the Message Digest implementations do
985
 * not support this operation. Applications should check the return value and
986
 * provide a proper fallback.
987
 *
988
 * Returns: new Message Digest context or NULL in case of an error.
989
 *
990
 * Since: 3.6.9
991
 */
992
gnutls_hash_hd_t gnutls_hash_copy(gnutls_hash_hd_t handle)
993
0
{
994
0
  gnutls_hash_hd_t dig;
995
996
0
  dig = gnutls_malloc(sizeof(digest_hd_st));
997
0
  if (dig == NULL) {
998
0
    gnutls_assert();
999
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1000
0
    return NULL;
1001
0
  }
1002
1003
0
  if (_gnutls_hash_copy((const digest_hd_st *)handle,
1004
0
            (digest_hd_st *)dig) != GNUTLS_E_SUCCESS) {
1005
0
    gnutls_assert();
1006
0
    gnutls_free(dig);
1007
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1008
0
    return NULL;
1009
0
  }
1010
1011
0
  return dig;
1012
0
}
1013
1014
/**
1015
 * gnutls_key_generate:
1016
 * @key: is a pointer to a #gnutls_datum_t which will contain a newly
1017
 * created key
1018
 * @key_size: the number of bytes of the key
1019
 *
1020
 * Generates a random key of @key_size bytes.
1021
 *
1022
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, or an
1023
 * error code.
1024
 *
1025
 * Since: 3.0
1026
 **/
1027
int gnutls_key_generate(gnutls_datum_t *key, unsigned int key_size)
1028
0
{
1029
0
  int ret;
1030
0
  bool not_approved = false;
1031
1032
0
  FAIL_IF_LIB_ERROR;
1033
1034
#ifdef ENABLE_FIPS140
1035
  /* The FIPS140 approved RNGs are not allowed to be used
1036
   * to extract key sizes longer than their original seed.
1037
   */
1038
  if (_gnutls_fips_mode_enabled() != 0 && key_size > FIPS140_RND_KEY_SIZE)
1039
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1040
#endif
1041
1042
0
  key->size = key_size;
1043
0
  key->data = gnutls_malloc(key->size);
1044
0
  if (!key->data) {
1045
0
    gnutls_assert();
1046
0
    ret = GNUTLS_E_MEMORY_ERROR;
1047
0
    goto error;
1048
0
  }
1049
1050
  /* Key lengths of less than 112 bits are not approved */
1051
0
  if (key_size < 14) {
1052
0
    not_approved = true;
1053
0
  }
1054
1055
0
  ret = gnutls_rnd(GNUTLS_RND_RANDOM, key->data, key->size);
1056
0
  if (ret < 0) {
1057
0
    gnutls_assert();
1058
0
    _gnutls_free_datum(key);
1059
0
    goto error;
1060
0
  }
1061
1062
0
error:
1063
0
  if (ret < 0) {
1064
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1065
0
  } else if (not_approved) {
1066
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_NOT_APPROVED);
1067
0
  } else {
1068
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_APPROVED);
1069
0
  }
1070
0
  return ret;
1071
0
}
1072
1073
/* AEAD API */
1074
1075
/**
1076
 * gnutls_aead_cipher_init:
1077
 * @handle: is a #gnutls_aead_cipher_hd_t type.
1078
 * @cipher: the authenticated-encryption algorithm to use
1079
 * @key: The key to be used for encryption
1080
 *
1081
 * This function will initialize an context that can be used for
1082
 * encryption/decryption of data. This will effectively use the
1083
 * current crypto backend in use by gnutls or the cryptographic
1084
 * accelerator in use.
1085
 *
1086
 * Returns: Zero or a negative error code on error.
1087
 *
1088
 * Since: 3.4.0
1089
 **/
1090
int gnutls_aead_cipher_init(gnutls_aead_cipher_hd_t *handle,
1091
          gnutls_cipher_algorithm_t cipher,
1092
          const gnutls_datum_t *key)
1093
0
{
1094
0
  api_aead_cipher_hd_st *h;
1095
0
  const cipher_entry_st *e;
1096
0
  int ret;
1097
0
  bool not_approved = false;
1098
1099
0
  if (!is_cipher_algo_allowed(cipher)) {
1100
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1101
0
    return gnutls_assert_val(GNUTLS_E_UNWANTED_ALGORITHM);
1102
0
  } else if (!is_cipher_algo_approved_in_fips(cipher)) {
1103
0
    not_approved = true;
1104
0
  }
1105
1106
0
  e = cipher_to_entry(cipher);
1107
0
  if (e == NULL || e->type != CIPHER_AEAD) {
1108
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1109
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1110
0
  }
1111
1112
0
  h = gnutls_calloc(1, sizeof(api_aead_cipher_hd_st));
1113
0
  if (h == NULL) {
1114
0
    gnutls_assert();
1115
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1116
0
    return GNUTLS_E_MEMORY_ERROR;
1117
0
  }
1118
1119
0
  ret = _gnutls_aead_cipher_init(h, cipher, key);
1120
0
  if (ret < 0) {
1121
0
    gnutls_free(h);
1122
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1123
0
    return ret;
1124
0
  }
1125
1126
0
  *handle = h;
1127
1128
0
  if (not_approved) {
1129
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_NOT_APPROVED);
1130
0
  } else {
1131
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_APPROVED);
1132
0
  }
1133
1134
0
  return ret;
1135
0
}
1136
1137
/**
1138
 * gnutls_aead_cipher_set_key:
1139
 * @handle: is a #gnutls_aead_cipher_hd_t type.
1140
 * @key: The key to be used for encryption
1141
 *
1142
 * This function will set a new key without re-initializing the
1143
 * context.
1144
 *
1145
 * Returns: Zero or a negative error code on error.
1146
 *
1147
 * Since: 3.7.5
1148
 **/
1149
int gnutls_aead_cipher_set_key(gnutls_aead_cipher_hd_t handle,
1150
             const gnutls_datum_t *key)
1151
0
{
1152
0
  const cipher_entry_st *e;
1153
0
  int ret;
1154
1155
0
  e = cipher_to_entry(handle->ctx_enc.e->id);
1156
0
  if (e == NULL || e->type != CIPHER_AEAD) {
1157
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1158
0
  }
1159
1160
0
  ret = handle->ctx_enc.setkey(handle->ctx_enc.handle, key->data,
1161
0
             key->size);
1162
0
  if (ret < 0) {
1163
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1164
0
  }
1165
1166
0
  return ret;
1167
0
}
1168
1169
/**
1170
 * gnutls_aead_cipher_decrypt:
1171
 * @handle: is a #gnutls_aead_cipher_hd_t type.
1172
 * @nonce: the nonce to set
1173
 * @nonce_len: The length of the nonce
1174
 * @auth: additional data to be authenticated
1175
 * @auth_len: The length of the data
1176
 * @tag_size: The size of the tag to use (use zero for the default)
1177
 * @ctext: the data to decrypt (including the authentication tag)
1178
 * @ctext_len: the length of data to decrypt (includes tag size)
1179
 * @ptext: the decrypted data
1180
 * @ptext_len: the length of decrypted data (initially must hold the maximum available size)
1181
 *
1182
 * This function will decrypt the given data using the algorithm
1183
 * specified by the context. This function must be provided the complete
1184
 * data to be decrypted, including the authentication tag. On several
1185
 * AEAD ciphers, the authentication tag is appended to the ciphertext,
1186
 * though this is not a general rule. This function will fail if
1187
 * the tag verification fails.
1188
 *
1189
 * Returns: Zero or a negative error code on verification failure or other error.
1190
 *
1191
 * Since: 3.4.0
1192
 **/
1193
int gnutls_aead_cipher_decrypt(gnutls_aead_cipher_hd_t handle,
1194
             const void *nonce, size_t nonce_len,
1195
             const void *auth, size_t auth_len,
1196
             size_t tag_size, const void *ctext,
1197
             size_t ctext_len, void *ptext, size_t *ptext_len)
1198
0
{
1199
0
  int ret;
1200
0
  api_aead_cipher_hd_st *h = handle;
1201
1202
0
  if (tag_size == 0)
1203
0
    tag_size = _gnutls_cipher_get_tag_size(h->ctx_enc.e);
1204
0
  else if (tag_size >
1205
0
     (unsigned)_gnutls_cipher_get_tag_size(h->ctx_enc.e)) {
1206
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1207
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1208
0
  }
1209
1210
0
  if (unlikely(ctext_len < tag_size)) {
1211
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1212
0
    return gnutls_assert_val(GNUTLS_E_DECRYPTION_FAILED);
1213
0
  }
1214
1215
0
  ret = _gnutls_aead_cipher_decrypt(&h->ctx_enc, nonce, nonce_len, auth,
1216
0
            auth_len, tag_size, ctext, ctext_len,
1217
0
            ptext, *ptext_len);
1218
0
  if (unlikely(ret < 0)) {
1219
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1220
0
    return gnutls_assert_val(ret);
1221
0
  } else {
1222
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_APPROVED);
1223
0
  }
1224
1225
  /* That assumes that AEAD ciphers are stream */
1226
0
  *ptext_len = ctext_len - tag_size;
1227
1228
0
  return 0;
1229
0
}
1230
1231
/**
1232
 * gnutls_aead_cipher_encrypt:
1233
 * @handle: is a #gnutls_aead_cipher_hd_t type.
1234
 * @nonce: the nonce to set
1235
 * @nonce_len: The length of the nonce
1236
 * @auth: additional data to be authenticated
1237
 * @auth_len: The length of the data
1238
 * @tag_size: The size of the tag to use (use zero for the default)
1239
 * @ptext: the data to encrypt
1240
 * @ptext_len: The length of data to encrypt
1241
 * @ctext: the encrypted data including authentication tag
1242
 * @ctext_len: the length of encrypted data (initially must hold the maximum available size, including space for tag)
1243
 *
1244
 * This function will encrypt the given data using the algorithm
1245
 * specified by the context. The output data will contain the
1246
 * authentication tag.
1247
 *
1248
 * Returns: Zero or a negative error code on error.
1249
 *
1250
 * Since: 3.4.0
1251
 **/
1252
int gnutls_aead_cipher_encrypt(gnutls_aead_cipher_hd_t handle,
1253
             const void *nonce, size_t nonce_len,
1254
             const void *auth, size_t auth_len,
1255
             size_t tag_size, const void *ptext,
1256
             size_t ptext_len, void *ctext, size_t *ctext_len)
1257
0
{
1258
0
  api_aead_cipher_hd_st *h = handle;
1259
0
  int ret;
1260
1261
0
  if (tag_size == 0)
1262
0
    tag_size = _gnutls_cipher_get_tag_size(h->ctx_enc.e);
1263
0
  else if (tag_size >
1264
0
     (unsigned)_gnutls_cipher_get_tag_size(h->ctx_enc.e)) {
1265
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1266
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1267
0
  }
1268
1269
0
  if (unlikely(*ctext_len < ptext_len + tag_size)) {
1270
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1271
0
    return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER);
1272
0
  }
1273
1274
0
  ret = _gnutls_aead_cipher_encrypt(&h->ctx_enc, nonce, nonce_len, auth,
1275
0
            auth_len, tag_size, ptext, ptext_len,
1276
0
            ctext, *ctext_len);
1277
0
  if (unlikely(ret < 0)) {
1278
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1279
0
    return gnutls_assert_val(ret);
1280
0
  } else {
1281
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_APPROVED);
1282
0
  }
1283
1284
  /* That assumes that AEAD ciphers are stream */
1285
0
  *ctext_len = ptext_len + tag_size;
1286
1287
0
  return 0;
1288
0
}
1289
1290
struct iov_store_st {
1291
  void *data;
1292
  size_t length;
1293
  size_t capacity;
1294
};
1295
1296
static void iov_store_free(struct iov_store_st *s)
1297
0
{
1298
0
  gnutls_free(s->data);
1299
0
}
1300
1301
static int iov_store_grow(struct iov_store_st *s, size_t length)
1302
0
{
1303
0
  void *new_data;
1304
0
  size_t new_capacity = s->capacity;
1305
1306
0
  if (INT_ADD_OVERFLOW(new_capacity, length)) {
1307
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1308
0
  }
1309
0
  new_capacity += length;
1310
0
  new_data = gnutls_realloc(s->data, new_capacity);
1311
0
  if (!new_data) {
1312
0
    return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
1313
0
  }
1314
0
  s->data = new_data;
1315
0
  s->capacity = new_capacity;
1316
0
  return 0;
1317
0
}
1318
1319
static int append_from_iov(struct iov_store_st *dst, const giovec_t *iov,
1320
         int iovcnt)
1321
0
{
1322
0
  if (iovcnt > 0) {
1323
0
    int i;
1324
0
    uint8_t *p;
1325
0
    void *new_data;
1326
0
    size_t new_capacity = dst->capacity;
1327
1328
0
    for (i = 0; i < iovcnt; i++) {
1329
0
      if (INT_ADD_OVERFLOW(new_capacity, iov[i].iov_len)) {
1330
0
        return gnutls_assert_val(
1331
0
          GNUTLS_E_INVALID_REQUEST);
1332
0
      }
1333
0
      new_capacity += iov[i].iov_len;
1334
0
    }
1335
0
    new_data = gnutls_realloc(dst->data, new_capacity);
1336
0
    if (!new_data) {
1337
0
      return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
1338
0
    }
1339
0
    dst->data = new_data;
1340
0
    dst->capacity = new_capacity;
1341
1342
0
    p = (uint8_t *)dst->data + dst->length;
1343
0
    for (i = 0; i < iovcnt; i++) {
1344
0
      if (iov[i].iov_len > 0) {
1345
0
        memcpy(p, iov[i].iov_base, iov[i].iov_len);
1346
0
      }
1347
0
      p += iov[i].iov_len;
1348
0
      dst->length += iov[i].iov_len;
1349
0
    }
1350
0
  }
1351
0
  return 0;
1352
0
}
1353
1354
static int copy_to_iov(const uint8_t *data, size_t size, const giovec_t *iov,
1355
           int iovcnt)
1356
0
{
1357
0
  size_t offset = 0;
1358
0
  int i;
1359
1360
0
  for (i = 0; i < iovcnt && size > 0; i++) {
1361
0
    size_t to_copy = MIN(size, iov[i].iov_len);
1362
0
    memcpy(iov[i].iov_base, (uint8_t *)data + offset, to_copy);
1363
0
    offset += to_copy;
1364
0
    size -= to_copy;
1365
0
  }
1366
0
  if (size > 0)
1367
0
    return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER);
1368
0
  return 0;
1369
0
}
1370
1371
#define IOV_STORE_INIT     \
1372
0
  {                  \
1373
0
    NULL, 0, 0 \
1374
0
  }
1375
1376
static int aead_cipher_encryptv_fallback(gnutls_aead_cipher_hd_t handle,
1377
           const void *nonce, size_t nonce_len,
1378
           const giovec_t *auth_iov,
1379
           int auth_iovcnt, size_t tag_size,
1380
           const giovec_t *iov, int iovcnt,
1381
           void *ctext, size_t *ctext_len)
1382
0
{
1383
0
  struct iov_store_st auth = IOV_STORE_INIT;
1384
0
  struct iov_store_st ptext = IOV_STORE_INIT;
1385
0
  int ret;
1386
1387
0
  if (tag_size == 0)
1388
0
    tag_size = _gnutls_cipher_get_tag_size(handle->ctx_enc.e);
1389
0
  else if (tag_size >
1390
0
     (unsigned)_gnutls_cipher_get_tag_size(handle->ctx_enc.e)) {
1391
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1392
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1393
0
  }
1394
1395
0
  ret = append_from_iov(&auth, auth_iov, auth_iovcnt);
1396
0
  if (ret < 0) {
1397
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1398
0
    return gnutls_assert_val(ret);
1399
0
  }
1400
1401
0
  ret = append_from_iov(&ptext, iov, iovcnt);
1402
0
  if (ret < 0) {
1403
0
    iov_store_free(&auth);
1404
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1405
0
    return gnutls_assert_val(ret);
1406
0
  }
1407
1408
0
  ret = gnutls_aead_cipher_encrypt(handle, nonce, nonce_len, auth.data,
1409
0
           auth.length, tag_size, ptext.data,
1410
0
           ptext.length, ctext, ctext_len);
1411
0
  iov_store_free(&auth);
1412
0
  iov_store_free(&ptext);
1413
1414
  /* FIPS operation state is set by gnutls_aead_cipher_encrypt */
1415
0
  return ret;
1416
0
}
1417
1418
static int aead_cipher_encryptv(gnutls_aead_cipher_hd_t handle,
1419
        const void *nonce, size_t nonce_len,
1420
        const giovec_t *auth_iov, int auth_iovcnt,
1421
        size_t tag_size, const giovec_t *iov,
1422
        int iovcnt, void *ctext, size_t *ctext_len)
1423
0
{
1424
0
  int ret;
1425
0
  uint8_t *dst;
1426
0
  size_t dst_size, total = 0;
1427
0
  uint8_t *p;
1428
0
  size_t len;
1429
0
  size_t blocksize = handle->ctx_enc.e->blocksize;
1430
0
  struct iov_iter_st iter;
1431
1432
0
  if (tag_size == 0)
1433
0
    tag_size = _gnutls_cipher_get_tag_size(handle->ctx_enc.e);
1434
0
  else if (tag_size >
1435
0
     (unsigned)_gnutls_cipher_get_tag_size(handle->ctx_enc.e)) {
1436
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1437
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1438
0
  }
1439
1440
0
  ret = _gnutls_cipher_setiv(&handle->ctx_enc, nonce, nonce_len);
1441
0
  if (unlikely(ret < 0)) {
1442
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1443
0
    return gnutls_assert_val(ret);
1444
0
  }
1445
1446
0
  ret = _gnutls_iov_iter_init(&iter, auth_iov, auth_iovcnt, blocksize);
1447
0
  if (unlikely(ret < 0)) {
1448
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1449
0
    return gnutls_assert_val(ret);
1450
0
  }
1451
0
  while (1) {
1452
0
    ret = _gnutls_iov_iter_next(&iter, &p);
1453
0
    if (unlikely(ret < 0)) {
1454
0
      _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1455
0
      return gnutls_assert_val(ret);
1456
0
    }
1457
0
    if (ret == 0)
1458
0
      break;
1459
0
    ret = _gnutls_cipher_auth(&handle->ctx_enc, p, ret);
1460
0
    if (unlikely(ret < 0)) {
1461
0
      _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1462
0
      return gnutls_assert_val(ret);
1463
0
    }
1464
0
  }
1465
1466
0
  dst = ctext;
1467
0
  dst_size = *ctext_len;
1468
1469
0
  ret = _gnutls_iov_iter_init(&iter, iov, iovcnt, blocksize);
1470
0
  if (unlikely(ret < 0)) {
1471
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1472
0
    return gnutls_assert_val(ret);
1473
0
  }
1474
0
  while (1) {
1475
0
    ret = _gnutls_iov_iter_next(&iter, &p);
1476
0
    if (unlikely(ret < 0)) {
1477
0
      _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1478
0
      return gnutls_assert_val(ret);
1479
0
    }
1480
0
    if (ret == 0)
1481
0
      break;
1482
0
    len = ret;
1483
0
    ret = _gnutls_cipher_encrypt2(&handle->ctx_enc, p, len, dst,
1484
0
                dst_size);
1485
0
    if (unlikely(ret < 0)) {
1486
0
      _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1487
0
      return gnutls_assert_val(ret);
1488
0
    }
1489
1490
0
    DECR_LEN(dst_size, len);
1491
0
    dst += len;
1492
0
    total += len;
1493
0
  }
1494
1495
0
  if (dst_size < tag_size) {
1496
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1497
0
    return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER);
1498
0
  }
1499
1500
0
  _gnutls_cipher_tag(&handle->ctx_enc, dst, tag_size);
1501
1502
0
  total += tag_size;
1503
0
  *ctext_len = total;
1504
1505
0
  _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_APPROVED);
1506
0
  return 0;
1507
0
}
1508
1509
/**
1510
 * gnutls_aead_cipher_encryptv:
1511
 * @handle: is a #gnutls_aead_cipher_hd_t type.
1512
 * @nonce: the nonce to set
1513
 * @nonce_len: The length of the nonce
1514
 * @auth_iov: additional data to be authenticated
1515
 * @auth_iovcnt: The number of buffers in @auth_iov
1516
 * @tag_size: The size of the tag to use (use zero for the default)
1517
 * @iov: the data to be encrypted
1518
 * @iovcnt: The number of buffers in @iov
1519
 * @ctext: the encrypted data including authentication tag
1520
 * @ctext_len: the length of encrypted data (initially must hold the maximum available size, including space for tag)
1521
 *
1522
 * This function will encrypt the provided data buffers using the algorithm
1523
 * specified by the context. The output data will contain the
1524
 * authentication tag.
1525
 *
1526
 * Returns: Zero or a negative error code on error.
1527
 *
1528
 * Since: 3.6.3
1529
 **/
1530
int gnutls_aead_cipher_encryptv(gnutls_aead_cipher_hd_t handle,
1531
        const void *nonce, size_t nonce_len,
1532
        const giovec_t *auth_iov, int auth_iovcnt,
1533
        size_t tag_size, const giovec_t *iov,
1534
        int iovcnt, void *ctext, size_t *ctext_len)
1535
0
{
1536
  /* Limitation: this function provides an optimization under the internally registered
1537
   * AEAD ciphers. When an AEAD cipher is used registered with gnutls_crypto_register_aead_cipher(),
1538
   * then this becomes a convenience function as it missed the lower-level primitives
1539
   * necessary for piecemeal encryption. */
1540
0
  if ((handle->ctx_enc.e->flags & GNUTLS_CIPHER_FLAG_ONLY_AEAD) ||
1541
0
      handle->ctx_enc.encrypt == NULL) {
1542
0
    return aead_cipher_encryptv_fallback(handle, nonce, nonce_len,
1543
0
                 auth_iov, auth_iovcnt,
1544
0
                 tag_size, iov, iovcnt,
1545
0
                 ctext, ctext_len);
1546
0
  } else {
1547
0
    return aead_cipher_encryptv(handle, nonce, nonce_len, auth_iov,
1548
0
              auth_iovcnt, tag_size, iov, iovcnt,
1549
0
              ctext, ctext_len);
1550
0
  }
1551
0
}
1552
1553
static int aead_cipher_encryptv2_fallback(gnutls_aead_cipher_hd_t handle,
1554
            const void *nonce, size_t nonce_len,
1555
            const giovec_t *auth_iov,
1556
            int auth_iovcnt, const giovec_t *iov,
1557
            int iovcnt, void *tag,
1558
            size_t *tag_size)
1559
0
{
1560
0
  struct iov_store_st auth = IOV_STORE_INIT;
1561
0
  struct iov_store_st ptext = IOV_STORE_INIT;
1562
0
  uint8_t *ptext_data;
1563
0
  size_t ptext_size;
1564
0
  uint8_t *ctext_data;
1565
0
  size_t ctext_size;
1566
0
  uint8_t *_tag;
1567
0
  size_t _tag_size;
1568
0
  int ret;
1569
1570
0
  if (tag_size == NULL || *tag_size == 0)
1571
0
    _tag_size = _gnutls_cipher_get_tag_size(handle->ctx_enc.e);
1572
0
  else
1573
0
    _tag_size = *tag_size;
1574
1575
0
  if (_tag_size >
1576
0
      (unsigned)_gnutls_cipher_get_tag_size(handle->ctx_enc.e)) {
1577
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1578
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1579
0
  }
1580
1581
0
  ret = append_from_iov(&auth, auth_iov, auth_iovcnt);
1582
0
  if (ret < 0) {
1583
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1584
0
    return gnutls_assert_val(ret);
1585
0
  }
1586
1587
0
  if (handle->ctx_enc.e->flags & GNUTLS_CIPHER_FLAG_TAG_PREFIXED) {
1588
    /* prepend space for tag */
1589
0
    ret = iov_store_grow(&ptext, _tag_size);
1590
0
    if (ret < 0) {
1591
0
      gnutls_assert();
1592
0
      goto error;
1593
0
    }
1594
0
    ptext.length = _tag_size;
1595
1596
0
    ret = append_from_iov(&ptext, iov, iovcnt);
1597
0
    if (ret < 0) {
1598
0
      gnutls_assert();
1599
0
      goto error;
1600
0
    }
1601
1602
    /* We must set ptext_data after the above
1603
     * grow/append operations, otherwise it will point to an invalid pointer after realloc.
1604
     */
1605
0
    ptext_data = (uint8_t *)ptext.data + _tag_size;
1606
0
    ptext_size = ptext.length - _tag_size;
1607
0
  } else {
1608
0
    ret = append_from_iov(&ptext, iov, iovcnt);
1609
0
    if (ret < 0) {
1610
0
      gnutls_assert();
1611
0
      goto error;
1612
0
    }
1613
1614
    /* append space for tag */
1615
0
    ret = iov_store_grow(&ptext, _tag_size);
1616
0
    if (ret < 0) {
1617
0
      gnutls_assert();
1618
0
      goto error;
1619
0
    }
1620
1621
    /* We must set ptext_data after the above
1622
     * grow/append operations, otherwise it will point to an invalid pointer after realloc.
1623
     */
1624
0
    ptext_data = ptext.data;
1625
0
    ptext_size = ptext.length;
1626
0
  }
1627
1628
0
  ctext_size = ptext.capacity;
1629
0
  ret = gnutls_aead_cipher_encrypt(handle, nonce, nonce_len, auth.data,
1630
0
           auth.length, _tag_size, ptext_data,
1631
0
           ptext_size, ptext.data, &ctext_size);
1632
0
  if (ret < 0) {
1633
0
    gnutls_assert();
1634
0
    goto error;
1635
0
  }
1636
1637
0
  if (handle->ctx_enc.e->flags & GNUTLS_CIPHER_FLAG_TAG_PREFIXED) {
1638
0
    ctext_data = (uint8_t *)ptext.data + _tag_size;
1639
0
    _tag = ptext.data;
1640
0
  } else {
1641
0
    ctext_data = ptext.data;
1642
0
    _tag = (uint8_t *)ptext.data + ptext_size;
1643
0
  }
1644
1645
0
  ret = copy_to_iov(ctext_data, ptext_size, iov, iovcnt);
1646
0
  if (ret < 0) {
1647
0
    gnutls_assert();
1648
0
    goto error;
1649
0
  }
1650
1651
0
  if (tag != NULL) {
1652
0
    memcpy(tag, _tag, _tag_size);
1653
0
  }
1654
0
  if (tag_size != NULL) {
1655
0
    *tag_size = _tag_size;
1656
0
  }
1657
1658
0
error:
1659
0
  iov_store_free(&auth);
1660
0
  iov_store_free(&ptext);
1661
1662
0
  if (ret < 0) {
1663
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1664
0
  }
1665
  /* FIPS operation state is set by gnutls_aead_cipher_encrypt */
1666
0
  return ret;
1667
0
}
1668
1669
static int aead_cipher_encryptv2(gnutls_aead_cipher_hd_t handle,
1670
         const void *nonce, size_t nonce_len,
1671
         const giovec_t *auth_iov, int auth_iovcnt,
1672
         const giovec_t *iov, int iovcnt, void *tag,
1673
         size_t *tag_size)
1674
0
{
1675
0
  api_aead_cipher_hd_st *h = handle;
1676
0
  int ret;
1677
0
  uint8_t *p;
1678
0
  size_t len;
1679
0
  size_t blocksize = handle->ctx_enc.e->blocksize;
1680
0
  struct iov_iter_st iter;
1681
0
  size_t _tag_size;
1682
1683
0
  if (tag_size == NULL || *tag_size == 0)
1684
0
    _tag_size = _gnutls_cipher_get_tag_size(h->ctx_enc.e);
1685
0
  else
1686
0
    _tag_size = *tag_size;
1687
1688
0
  if (_tag_size > (unsigned)_gnutls_cipher_get_tag_size(h->ctx_enc.e)) {
1689
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1690
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1691
0
  }
1692
1693
0
  ret = _gnutls_cipher_setiv(&handle->ctx_enc, nonce, nonce_len);
1694
0
  if (unlikely(ret < 0)) {
1695
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1696
0
    return gnutls_assert_val(ret);
1697
0
  }
1698
1699
0
  ret = _gnutls_iov_iter_init(&iter, auth_iov, auth_iovcnt, blocksize);
1700
0
  if (unlikely(ret < 0)) {
1701
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1702
0
    return gnutls_assert_val(ret);
1703
0
  }
1704
0
  while (1) {
1705
0
    ret = _gnutls_iov_iter_next(&iter, &p);
1706
0
    if (unlikely(ret < 0)) {
1707
0
      _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1708
0
      return gnutls_assert_val(ret);
1709
0
    }
1710
0
    if (ret == 0)
1711
0
      break;
1712
0
    ret = _gnutls_cipher_auth(&handle->ctx_enc, p, ret);
1713
0
    if (unlikely(ret < 0)) {
1714
0
      _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1715
0
      return gnutls_assert_val(ret);
1716
0
    }
1717
0
  }
1718
1719
0
  ret = _gnutls_iov_iter_init(&iter, iov, iovcnt, blocksize);
1720
0
  if (unlikely(ret < 0))
1721
0
    return gnutls_assert_val(ret);
1722
0
  while (1) {
1723
0
    ret = _gnutls_iov_iter_next(&iter, &p);
1724
0
    if (unlikely(ret < 0)) {
1725
0
      _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1726
0
      return gnutls_assert_val(ret);
1727
0
    }
1728
0
    if (ret == 0)
1729
0
      break;
1730
1731
0
    len = ret;
1732
0
    ret = _gnutls_cipher_encrypt2(&handle->ctx_enc, p, len, p, len);
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
1738
0
    ret = _gnutls_iov_iter_sync(&iter, p, len);
1739
0
    if (unlikely(ret < 0)) {
1740
0
      _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1741
0
      return gnutls_assert_val(ret);
1742
0
    }
1743
0
  }
1744
1745
0
  if (tag != NULL)
1746
0
    _gnutls_cipher_tag(&handle->ctx_enc, tag, _tag_size);
1747
0
  if (tag_size != NULL)
1748
0
    *tag_size = _tag_size;
1749
1750
0
  _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_APPROVED);
1751
0
  return 0;
1752
0
}
1753
1754
/**
1755
 * gnutls_aead_cipher_encryptv2:
1756
 * @handle: is a #gnutls_aead_cipher_hd_t type.
1757
 * @nonce: the nonce to set
1758
 * @nonce_len: The length of the nonce
1759
 * @auth_iov: additional data to be authenticated
1760
 * @auth_iovcnt: The number of buffers in @auth_iov
1761
 * @iov: the data to be encrypted
1762
 * @iovcnt: The number of buffers in @iov
1763
 * @tag: The authentication tag
1764
 * @tag_size: The size of the tag to use (use zero for the default)
1765
 *
1766
 * This is similar to gnutls_aead_cipher_encrypt(), but it performs
1767
 * in-place encryption on the provided data buffers.
1768
 *
1769
 * Returns: Zero or a negative error code on error.
1770
 *
1771
 * Since: 3.6.10
1772
 **/
1773
int gnutls_aead_cipher_encryptv2(gnutls_aead_cipher_hd_t handle,
1774
         const void *nonce, size_t nonce_len,
1775
         const giovec_t *auth_iov, int auth_iovcnt,
1776
         const giovec_t *iov, int iovcnt, void *tag,
1777
         size_t *tag_size)
1778
0
{
1779
  /* Limitation: this function provides an optimization under the internally registered
1780
   * AEAD ciphers. When an AEAD cipher is used registered with gnutls_crypto_register_aead_cipher(),
1781
   * then this becomes a convenience function as it missed the lower-level primitives
1782
   * necessary for piecemeal encryption. */
1783
0
  if ((handle->ctx_enc.e->flags & GNUTLS_CIPHER_FLAG_ONLY_AEAD) ||
1784
0
      handle->ctx_enc.encrypt == NULL) {
1785
0
    return aead_cipher_encryptv2_fallback(handle, nonce, nonce_len,
1786
0
                  auth_iov, auth_iovcnt,
1787
0
                  iov, iovcnt, tag,
1788
0
                  tag_size);
1789
0
  } else {
1790
0
    return aead_cipher_encryptv2(handle, nonce, nonce_len, auth_iov,
1791
0
               auth_iovcnt, iov, iovcnt, tag,
1792
0
               tag_size);
1793
0
  }
1794
0
}
1795
1796
static int aead_cipher_decryptv2_fallback(gnutls_aead_cipher_hd_t handle,
1797
            const void *nonce, size_t nonce_len,
1798
            const giovec_t *auth_iov,
1799
            int auth_iovcnt, const giovec_t *iov,
1800
            int iovcnt, void *tag,
1801
            size_t tag_size)
1802
0
{
1803
0
  struct iov_store_st auth = IOV_STORE_INIT;
1804
0
  struct iov_store_st ctext = IOV_STORE_INIT;
1805
0
  uint8_t *ctext_data;
1806
0
  size_t ptext_size;
1807
0
  int ret;
1808
1809
0
  if (tag_size == 0)
1810
0
    tag_size = _gnutls_cipher_get_tag_size(handle->ctx_enc.e);
1811
0
  else if (tag_size >
1812
0
     (unsigned)_gnutls_cipher_get_tag_size(handle->ctx_enc.e)) {
1813
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1814
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1815
0
  }
1816
1817
0
  ret = append_from_iov(&auth, auth_iov, auth_iovcnt);
1818
0
  if (ret < 0) {
1819
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1820
0
    return gnutls_assert_val(ret);
1821
0
  }
1822
1823
0
  if (handle->ctx_enc.e->flags & GNUTLS_CIPHER_FLAG_TAG_PREFIXED) {
1824
    /* prepend tag */
1825
0
    ret = iov_store_grow(&ctext, tag_size);
1826
0
    if (ret < 0) {
1827
0
      gnutls_assert();
1828
0
      goto error;
1829
0
    }
1830
0
    memcpy(ctext.data, tag, tag_size);
1831
0
    ctext.length += tag_size;
1832
1833
0
    ret = append_from_iov(&ctext, iov, iovcnt);
1834
0
    if (ret < 0) {
1835
0
      gnutls_assert();
1836
0
      goto error;
1837
0
    }
1838
1839
    /* We must set ctext_data after the above
1840
     * grow/append operations, otherwise it will point to an invalid pointer after realloc.
1841
     */
1842
0
    ctext_data = (uint8_t *)ctext.data + tag_size;
1843
0
  } else {
1844
0
    ret = append_from_iov(&ctext, iov, iovcnt);
1845
0
    if (ret < 0) {
1846
0
      gnutls_assert();
1847
0
      goto error;
1848
0
    }
1849
1850
    /* append tag */
1851
0
    ret = iov_store_grow(&ctext, tag_size);
1852
0
    if (ret < 0) {
1853
0
      gnutls_assert();
1854
0
      goto error;
1855
0
    }
1856
0
    memcpy((uint8_t *)ctext.data + ctext.length, tag, tag_size);
1857
0
    ctext.length += tag_size;
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 = ctext.data;
1863
0
  }
1864
1865
0
  ptext_size = ctext.capacity;
1866
0
  ret = gnutls_aead_cipher_decrypt(handle, nonce, nonce_len, auth.data,
1867
0
           auth.length, tag_size, ctext.data,
1868
0
           ctext.length, ctext_data, &ptext_size);
1869
0
  if (ret < 0) {
1870
0
    gnutls_assert();
1871
0
    goto error;
1872
0
  }
1873
1874
0
  ret = copy_to_iov(ctext.data, ptext_size, iov, iovcnt);
1875
0
  if (ret < 0) {
1876
0
    gnutls_assert();
1877
0
    goto error;
1878
0
  }
1879
1880
0
error:
1881
0
  iov_store_free(&auth);
1882
0
  iov_store_free(&ctext);
1883
1884
0
  if (ret < 0) {
1885
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1886
0
  }
1887
  /* FIPS operation state is set by gnutls_aead_cipher_decrypt */
1888
0
  return ret;
1889
0
}
1890
1891
static int aead_cipher_decryptv2(gnutls_aead_cipher_hd_t handle,
1892
         const void *nonce, size_t nonce_len,
1893
         const giovec_t *auth_iov, int auth_iovcnt,
1894
         const giovec_t *iov, int iovcnt, void *tag,
1895
         size_t tag_size)
1896
0
{
1897
0
  int ret;
1898
0
  uint8_t *p;
1899
0
  size_t len;
1900
0
  ssize_t blocksize = handle->ctx_enc.e->blocksize;
1901
0
  struct iov_iter_st iter;
1902
0
  uint8_t _tag[MAX_HASH_SIZE];
1903
1904
0
  if (tag_size == 0)
1905
0
    tag_size = _gnutls_cipher_get_tag_size(handle->ctx_enc.e);
1906
0
  else if (tag_size >
1907
0
     (unsigned)_gnutls_cipher_get_tag_size(handle->ctx_enc.e)) {
1908
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1909
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1910
0
  }
1911
1912
0
  ret = _gnutls_cipher_setiv(&handle->ctx_enc, nonce, nonce_len);
1913
0
  if (unlikely(ret < 0)) {
1914
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1915
0
    return gnutls_assert_val(ret);
1916
0
  }
1917
1918
0
  ret = _gnutls_iov_iter_init(&iter, auth_iov, auth_iovcnt, blocksize);
1919
0
  if (unlikely(ret < 0)) {
1920
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1921
0
    return gnutls_assert_val(ret);
1922
0
  }
1923
0
  while (1) {
1924
0
    ret = _gnutls_iov_iter_next(&iter, &p);
1925
0
    if (unlikely(ret < 0)) {
1926
0
      _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1927
0
      return gnutls_assert_val(ret);
1928
0
    }
1929
0
    if (ret == 0)
1930
0
      break;
1931
0
    ret = _gnutls_cipher_auth(&handle->ctx_enc, p, ret);
1932
0
    if (unlikely(ret < 0)) {
1933
0
      _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1934
0
      return gnutls_assert_val(ret);
1935
0
    }
1936
0
  }
1937
1938
0
  ret = _gnutls_iov_iter_init(&iter, iov, 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
1952
0
    len = ret;
1953
0
    ret = _gnutls_cipher_decrypt2(&handle->ctx_enc, p, len, p, len);
1954
0
    if (unlikely(ret < 0)) {
1955
0
      _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1956
0
      return gnutls_assert_val(ret);
1957
0
    }
1958
1959
0
    ret = _gnutls_iov_iter_sync(&iter, p, len);
1960
0
    if (unlikely(ret < 0)) {
1961
0
      _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1962
0
      return gnutls_assert_val(ret);
1963
0
    }
1964
0
  }
1965
1966
0
  if (tag != NULL) {
1967
0
    _gnutls_cipher_tag(&handle->ctx_enc, _tag, tag_size);
1968
0
    if (gnutls_memcmp(_tag, tag, tag_size) != 0) {
1969
0
      _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1970
0
      return gnutls_assert_val(GNUTLS_E_DECRYPTION_FAILED);
1971
0
    }
1972
0
  }
1973
1974
0
  _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_APPROVED);
1975
0
  return 0;
1976
0
}
1977
1978
/**
1979
 * gnutls_aead_cipher_decryptv2:
1980
 * @handle: is a #gnutls_aead_cipher_hd_t type.
1981
 * @nonce: the nonce to set
1982
 * @nonce_len: The length of the nonce
1983
 * @auth_iov: additional data to be authenticated
1984
 * @auth_iovcnt: The number of buffers in @auth_iov
1985
 * @iov: the data to decrypt
1986
 * @iovcnt: The number of buffers in @iov
1987
 * @tag: The authentication tag
1988
 * @tag_size: The size of the tag to use (use zero for the default)
1989
 *
1990
 * This is similar to gnutls_aead_cipher_decrypt(), but it performs
1991
 * in-place encryption on the provided data buffers.
1992
 *
1993
 * Returns: Zero or a negative error code on error.
1994
 *
1995
 * Since: 3.6.10
1996
 **/
1997
int gnutls_aead_cipher_decryptv2(gnutls_aead_cipher_hd_t handle,
1998
         const void *nonce, size_t nonce_len,
1999
         const giovec_t *auth_iov, int auth_iovcnt,
2000
         const giovec_t *iov, int iovcnt, void *tag,
2001
         size_t tag_size)
2002
0
{
2003
  /* Limitation: this function provides an optimization under the internally registered
2004
   * AEAD ciphers. When an AEAD cipher is used registered with gnutls_crypto_register_aead_cipher(),
2005
   * then this becomes a convenience function as it missed the lower-level primitives
2006
   * necessary for piecemeal encryption. */
2007
0
  if ((handle->ctx_enc.e->flags & GNUTLS_CIPHER_FLAG_ONLY_AEAD) ||
2008
0
      handle->ctx_enc.encrypt == NULL) {
2009
0
    return aead_cipher_decryptv2_fallback(handle, nonce, nonce_len,
2010
0
                  auth_iov, auth_iovcnt,
2011
0
                  iov, iovcnt, tag,
2012
0
                  tag_size);
2013
0
  } else {
2014
0
    return aead_cipher_decryptv2(handle, nonce, nonce_len, auth_iov,
2015
0
               auth_iovcnt, iov, iovcnt, tag,
2016
0
               tag_size);
2017
0
  }
2018
0
}
2019
2020
/**
2021
 * gnutls_aead_cipher_deinit:
2022
 * @handle: is a #gnutls_aead_cipher_hd_t type.
2023
 *
2024
 * This function will deinitialize all resources occupied by the given
2025
 * authenticated-encryption context.
2026
 *
2027
 * Since: 3.4.0
2028
 **/
2029
void gnutls_aead_cipher_deinit(gnutls_aead_cipher_hd_t handle)
2030
0
{
2031
0
  _gnutls_aead_cipher_deinit(handle);
2032
0
  gnutls_free(handle);
2033
0
}
2034
2035
extern gnutls_crypto_kdf_st _gnutls_kdf_ops;
2036
2037
/* Same as @gnutls_hkdf_extract but without changing FIPS context */
2038
int _gnutls_hkdf_extract(gnutls_mac_algorithm_t mac, const gnutls_datum_t *key,
2039
       const gnutls_datum_t *salt, void *output)
2040
0
{
2041
  /* MD5 is only allowed internally for TLS */
2042
0
  if (!is_mac_algo_allowed(mac)) {
2043
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
2044
0
    return gnutls_assert_val(GNUTLS_E_UNWANTED_ALGORITHM);
2045
0
  }
2046
2047
  /* We don't check whether MAC is approved, because HKDF is
2048
   * only approved in TLS, which is handled separately. */
2049
2050
0
  return _gnutls_kdf_ops.hkdf_extract(mac, key->data, key->size,
2051
0
              salt ? salt->data : NULL,
2052
0
              salt ? salt->size : 0, output);
2053
0
}
2054
2055
/**
2056
 * gnutls_hkdf_extract:
2057
 * @mac: the mac algorithm used internally
2058
 * @key: the initial keying material
2059
 * @salt: the optional salt
2060
 * @output: the output value of the extract operation
2061
 *
2062
 * This function will derive a fixed-size key using the HKDF-Extract
2063
 * function as defined in RFC 5869.
2064
 *
2065
 * Returns: Zero or a negative error code on error.
2066
 *
2067
 * Since: 3.6.13
2068
 */
2069
int gnutls_hkdf_extract(gnutls_mac_algorithm_t mac, const gnutls_datum_t *key,
2070
      const gnutls_datum_t *salt, void *output)
2071
0
{
2072
0
  int ret;
2073
2074
0
  ret = _gnutls_hkdf_extract(mac, key, salt, output);
2075
0
  if (ret < 0)
2076
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
2077
0
  else
2078
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_NOT_APPROVED);
2079
2080
0
  return ret;
2081
0
}
2082
2083
/* Same as @gnutls_hkdf_expand but without changing FIPS context */
2084
int _gnutls_hkdf_expand(gnutls_mac_algorithm_t mac, const gnutls_datum_t *key,
2085
      const gnutls_datum_t *info, void *output, size_t length)
2086
0
{
2087
  /* MD5 is only allowed internally for TLS */
2088
0
  if (!is_mac_algo_allowed(mac)) {
2089
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
2090
0
    return gnutls_assert_val(GNUTLS_E_UNWANTED_ALGORITHM);
2091
0
  }
2092
2093
  /* We don't check whether MAC is approved, because HKDF is
2094
   * only approved in TLS, which is handled separately. */
2095
2096
0
  return _gnutls_kdf_ops.hkdf_expand(mac, key->data, key->size,
2097
0
             info->data, info->size, output,
2098
0
             length);
2099
0
}
2100
2101
/**
2102
 * gnutls_hkdf_expand:
2103
 * @mac: the mac algorithm used internally
2104
 * @key: the pseudorandom key created with HKDF-Extract
2105
 * @info: the optional informational data
2106
 * @output: the output value of the expand operation
2107
 * @length: the desired length of the output key
2108
 *
2109
 * This function will derive a variable length keying material from
2110
 * the pseudorandom key using the HKDF-Expand function as defined in
2111
 * RFC 5869.
2112
 *
2113
 * Returns: Zero or a negative error code on error.
2114
 *
2115
 * Since: 3.6.13
2116
 */
2117
int gnutls_hkdf_expand(gnutls_mac_algorithm_t mac, const gnutls_datum_t *key,
2118
           const gnutls_datum_t *info, void *output, size_t length)
2119
0
{
2120
0
  int ret;
2121
2122
0
  ret = _gnutls_hkdf_expand(mac, key, info, output, length);
2123
0
  if (ret < 0)
2124
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
2125
0
  else
2126
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_NOT_APPROVED);
2127
2128
0
  return ret;
2129
0
}
2130
2131
/**
2132
 * gnutls_pbkdf2:
2133
 * @mac: the mac algorithm used internally
2134
 * @key: the initial keying material
2135
 * @salt: the salt
2136
 * @iter_count: the iteration count
2137
 * @output: the output value
2138
 * @length: the desired length of the output key
2139
 *
2140
 * This function will derive a variable length keying material from
2141
 * a password according to PKCS #5 PBKDF2.
2142
 *
2143
 * Returns: Zero or a negative error code on error.
2144
 *
2145
 * Since: 3.6.13
2146
 */
2147
int gnutls_pbkdf2(gnutls_mac_algorithm_t mac, const gnutls_datum_t *key,
2148
      const gnutls_datum_t *salt, unsigned iter_count, void *output,
2149
      size_t length)
2150
0
{
2151
0
  int ret;
2152
0
  bool not_approved = false;
2153
2154
  /* MD5 is only allowed internally for TLS */
2155
0
  if (!is_mac_algo_allowed(mac)) {
2156
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
2157
0
    return gnutls_assert_val(GNUTLS_E_UNWANTED_ALGORITHM);
2158
0
  } else if (!is_mac_algo_hmac_approved_in_fips(mac)) {
2159
    /* ACVP only allows HMAC used with PBKDF2:
2160
     * https://pages.nist.gov/ACVP/draft-celi-acvp-pbkdf.html
2161
     */
2162
0
    not_approved = true;
2163
0
  }
2164
2165
  /* Key lengths and output sizes of less than 112 bits are not approved */
2166
0
  if (key->size < 14 || length < 14) {
2167
0
    not_approved = true;
2168
0
  }
2169
2170
  /* Minimum salt length of 128 bits (SP 800-132 5.1) */
2171
0
  if (salt->size < 16) {
2172
0
    not_approved = true;
2173
0
  }
2174
2175
  /* Minimum iterations bound (SP 800-132 5.2) */
2176
0
  if (iter_count < 1000) {
2177
0
    not_approved = true;
2178
0
  }
2179
2180
0
  ret = _gnutls_kdf_ops.pbkdf2(mac, key->data, key->size, salt->data,
2181
0
             salt->size, iter_count, output, length);
2182
0
  if (ret < 0) {
2183
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
2184
0
  } else if (not_approved) {
2185
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_NOT_APPROVED);
2186
0
  } else {
2187
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_APPROVED);
2188
0
  }
2189
0
  return ret;
2190
0
}