1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
|
/*
* Copyright (C) 2000-2016 Free Software Foundation, Inc.
* Copyright (C) 2016 Red Hat, Inc.
*
* Author: Nikos Mavrogiannopoulos
*
* This file is part of GnuTLS.
*
* The GnuTLS is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>
*
*/
#include "gnutls_int.h"
#include "errors.h"
#include <cipher_int.h>
#include <datum.h>
#include <gnutls/crypto.h>
#include <algorithms.h>
#include <random.h>
#include <crypto.h>
#include <fips.h>
#include "crypto-api.h"
typedef struct api_cipher_hd_st {
cipher_hd_st ctx_enc;
cipher_hd_st ctx_dec;
} api_cipher_hd_st;
/**
* gnutls_cipher_init:
* @handle: is a #gnutls_cipher_hd_t type
* @cipher: the encryption algorithm to use
* @key: the key to be used for encryption/decryption
* @iv: the IV to use (if not applicable set NULL)
*
* This function will initialize the @handle context to be usable
* for encryption/decryption of data. This will effectively use the
* current crypto backend in use by gnutls or the cryptographic
* accelerator in use.
*
* Returns: Zero or a negative error code on error.
*
* Since: 2.10.0
**/
int
gnutls_cipher_init(gnutls_cipher_hd_t * handle,
gnutls_cipher_algorithm_t cipher,
const gnutls_datum_t * key, const gnutls_datum_t * iv)
{
api_cipher_hd_st *h;
int ret;
const cipher_entry_st* e;
if (is_cipher_algo_forbidden(cipher))
return gnutls_assert_val(GNUTLS_E_UNWANTED_ALGORITHM);
e = cipher_to_entry(cipher);
if (e == NULL || e->only_aead)
return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
*handle = gnutls_calloc(1, sizeof(api_cipher_hd_st));
if (*handle == NULL) {
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
}
h = *handle;
ret =
_gnutls_cipher_init(&h->ctx_enc, e, key,
iv, 1);
if (ret >= 0 && _gnutls_cipher_type(e) == CIPHER_BLOCK)
ret =
_gnutls_cipher_init(&h->ctx_dec, e, key, iv, 0);
return ret;
}
/**
* gnutls_cipher_tag:
* @handle: is a #gnutls_cipher_hd_t type
* @tag: will hold the tag
* @tag_size: the length of the tag to return
*
* This function operates on authenticated encryption with
* associated data (AEAD) ciphers and will return the
* output tag.
*
* Returns: Zero or a negative error code on error.
*
* Since: 3.0
**/
int
gnutls_cipher_tag(gnutls_cipher_hd_t handle, void *tag, size_t tag_size)
{
api_cipher_hd_st *h = handle;
if (_gnutls_cipher_is_aead(&h->ctx_enc) == 0)
return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
_gnutls_cipher_tag(&h->ctx_enc, tag, tag_size);
return 0;
}
/**
* gnutls_cipher_add_auth:
* @handle: is a #gnutls_cipher_hd_t type
* @ptext: the data to be authenticated
* @ptext_size: the length of the data
*
* This function operates on authenticated encryption with
* associated data (AEAD) ciphers and authenticate the
* input data. This function can only be called once
* and before any encryption operations.
*
* Returns: Zero or a negative error code on error.
*
* Since: 3.0
**/
int
gnutls_cipher_add_auth(gnutls_cipher_hd_t handle, const void *ptext,
size_t ptext_size)
{
api_cipher_hd_st *h = handle;
if (_gnutls_cipher_is_aead(&h->ctx_enc) == 0)
return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
return _gnutls_cipher_auth(&h->ctx_enc, ptext, ptext_size);
}
/**
* gnutls_cipher_set_iv:
* @handle: is a #gnutls_cipher_hd_t type
* @iv: the IV to set
* @ivlen: the length of the IV
*
* This function will set the IV to be used for the next
* encryption block.
*
* Since: 3.0
**/
void
gnutls_cipher_set_iv(gnutls_cipher_hd_t handle, void *iv, size_t ivlen)
{
api_cipher_hd_st *h = handle;
if (_gnutls_cipher_setiv(&h->ctx_enc, iv, ivlen) < 0) {
_gnutls_switch_lib_state(LIB_STATE_ERROR);
}
if (_gnutls_cipher_type(h->ctx_enc.e) == CIPHER_BLOCK)
if (_gnutls_cipher_setiv(&h->ctx_dec, iv, ivlen) < 0) {
_gnutls_switch_lib_state(LIB_STATE_ERROR);
}
}
/**
* gnutls_cipher_encrypt:
* @handle: is a #gnutls_cipher_hd_t type
* @ptext: the data to encrypt
* @ptext_len: the length of data to encrypt
*
* This function will encrypt the given data using the algorithm
* specified by the context.
*
* Returns: Zero or a negative error code on error.
*
* Since: 2.10.0
**/
int
gnutls_cipher_encrypt(gnutls_cipher_hd_t handle, void *ptext,
size_t ptext_len)
{
api_cipher_hd_st *h = handle;
return _gnutls_cipher_encrypt(&h->ctx_enc, ptext, ptext_len);
}
/**
* gnutls_cipher_decrypt:
* @handle: is a #gnutls_cipher_hd_t type
* @ctext: the data to decrypt
* @ctext_len: the length of data to decrypt
*
* This function will decrypt the given data using the algorithm
* specified by the context.
*
* Note that in AEAD ciphers, this will not check the tag. You will
* need to compare the tag sent with the value returned from gnutls_cipher_tag().
*
* Returns: Zero or a negative error code on error.
*
* Since: 2.10.0
**/
int
gnutls_cipher_decrypt(gnutls_cipher_hd_t handle, void *ctext,
size_t ctext_len)
{
api_cipher_hd_st *h = handle;
if (_gnutls_cipher_type(h->ctx_enc.e) != CIPHER_BLOCK)
return _gnutls_cipher_decrypt(&h->ctx_enc, ctext,
ctext_len);
else
return _gnutls_cipher_decrypt(&h->ctx_dec, ctext,
ctext_len);
}
/**
* gnutls_cipher_encrypt2:
* @handle: is a #gnutls_cipher_hd_t type
* @ptext: the data to encrypt
* @ptext_len: the length of data to encrypt
* @ctext: the encrypted data
* @ctext_len: the available length for encrypted data
*
* This function will encrypt the given data using the algorithm
* specified by the context. For block ciphers the @ptext_len must be
* a multiple of the block size. For the supported ciphers the encrypted
* data length will equal the plaintext size.
*
* Returns: Zero or a negative error code on error.
*
* Since: 2.12.0
**/
int
gnutls_cipher_encrypt2(gnutls_cipher_hd_t handle, const void *ptext,
size_t ptext_len, void *ctext,
size_t ctext_len)
{
api_cipher_hd_st *h = handle;
return _gnutls_cipher_encrypt2(&h->ctx_enc, ptext, ptext_len,
ctext, ctext_len);
}
/**
* gnutls_cipher_decrypt2:
* @handle: is a #gnutls_cipher_hd_t type
* @ctext: the data to decrypt
* @ctext_len: the length of data to decrypt
* @ptext: the decrypted data
* @ptext_len: the available length for decrypted data
*
* This function will decrypt the given data using the algorithm
* specified by the context. For block ciphers the @ctext_len must be
* a multiple of the block size. For the supported ciphers the plaintext
* data length will equal the ciphertext size.
*
* Note that in AEAD ciphers, this will not check the tag. You will
* need to compare the tag sent with the value returned from gnutls_cipher_tag().
*
* Returns: Zero or a negative error code on error.
*
* Since: 2.12.0
**/
int
gnutls_cipher_decrypt2(gnutls_cipher_hd_t handle, const void *ctext,
size_t ctext_len, void *ptext, size_t ptext_len)
{
api_cipher_hd_st *h = handle;
if (_gnutls_cipher_type(h->ctx_enc.e) != CIPHER_BLOCK)
return _gnutls_cipher_decrypt2(&h->ctx_enc, ctext,
ctext_len, ptext,
ptext_len);
else
return _gnutls_cipher_decrypt2(&h->ctx_dec, ctext,
ctext_len, ptext,
ptext_len);
}
/**
* gnutls_cipher_deinit:
* @handle: is a #gnutls_cipher_hd_t type
*
* This function will deinitialize all resources occupied by the given
* encryption context.
*
* Since: 2.10.0
**/
void gnutls_cipher_deinit(gnutls_cipher_hd_t handle)
{
api_cipher_hd_st *h = handle;
_gnutls_cipher_deinit(&h->ctx_enc);
if (_gnutls_cipher_type(h->ctx_enc.e) == CIPHER_BLOCK)
_gnutls_cipher_deinit(&h->ctx_dec);
gnutls_free(handle);
}
/* HMAC */
/**
* gnutls_hmac_init:
* @dig: is a #gnutls_hmac_hd_t type
* @algorithm: the HMAC algorithm to use
* @key: the key to be used for encryption
* @keylen: the length of the key
*
* This function will initialize an context that can be used to
* produce a Message Authentication Code (MAC) of data. This will
* effectively use the current crypto backend in use by gnutls or the
* cryptographic accelerator in use.
*
* Note that despite the name of this function, it can be used
* for other MAC algorithms than HMAC.
*
* Returns: Zero or a negative error code on error.
*
* Since: 2.10.0
**/
int
gnutls_hmac_init(gnutls_hmac_hd_t * dig,
gnutls_mac_algorithm_t algorithm,
const void *key, size_t keylen)
{
/* MD5 is only allowed internally for TLS */
if (is_mac_algo_forbidden(algorithm))
return gnutls_assert_val(GNUTLS_E_UNWANTED_ALGORITHM);
*dig = gnutls_malloc(sizeof(mac_hd_st));
if (*dig == NULL) {
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
}
return _gnutls_mac_init(((mac_hd_st *) * dig),
mac_to_entry(algorithm), key, keylen);
}
/**
* gnutls_hmac_set_nonce:
* @handle: is a #gnutls_hmac_hd_t type
* @nonce: the data to set as nonce
* @nonce_len: the length of data
*
* This function will set the nonce in the MAC algorithm.
*
* Since: 3.2.0
**/
void
gnutls_hmac_set_nonce(gnutls_hmac_hd_t handle, const void *nonce,
size_t nonce_len)
{
_gnutls_mac_set_nonce((mac_hd_st *) handle, nonce, nonce_len);
}
/**
* gnutls_hmac:
* @handle: is a #gnutls_hmac_hd_t type
* @ptext: the data to hash
* @ptext_len: the length of data to hash
*
* This function will hash the given data using the algorithm
* specified by the context.
*
* Returns: Zero or a negative error code on error.
*
* Since: 2.10.0
**/
int gnutls_hmac(gnutls_hmac_hd_t handle, const void *ptext, size_t ptext_len)
{
return _gnutls_mac((mac_hd_st *) handle, ptext, ptext_len);
}
/**
* gnutls_hmac_output:
* @handle: is a #gnutls_hmac_hd_t type
* @digest: is the output value of the MAC
*
* This function will output the current MAC value
* and reset the state of the MAC.
*
* Since: 2.10.0
**/
void gnutls_hmac_output(gnutls_hmac_hd_t handle, void *digest)
{
_gnutls_mac_output((mac_hd_st *) handle, digest);
}
/**
* gnutls_hmac_deinit:
* @handle: is a #gnutls_hmac_hd_t type
* @digest: is the output value of the MAC
*
* This function will deinitialize all resources occupied by
* the given hmac context.
*
* Since: 2.10.0
**/
void gnutls_hmac_deinit(gnutls_hmac_hd_t handle, void *digest)
{
_gnutls_mac_deinit((mac_hd_st *) handle, digest);
gnutls_free(handle);
}
/**
* gnutls_hmac_get_len:
* @algorithm: the hmac algorithm to use
*
* This function will return the length of the output data
* of the given hmac algorithm.
*
* Returns: The length or zero on error.
*
* Since: 2.10.0
**/
unsigned gnutls_hmac_get_len(gnutls_mac_algorithm_t algorithm)
{
return _gnutls_mac_get_algo_len(mac_to_entry(algorithm));
}
/**
* gnutls_hmac_fast:
* @algorithm: the hash algorithm to use
* @key: the key to use
* @keylen: the length of the key
* @ptext: the data to hash
* @ptext_len: the length of data to hash
* @digest: is the output value of the hash
*
* This convenience function will hash the given data and return output
* on a single call.
*
* Returns: Zero or a negative error code on error.
*
* Since: 2.10.0
**/
int
gnutls_hmac_fast(gnutls_mac_algorithm_t algorithm,
const void *key, size_t keylen,
const void *ptext, size_t ptext_len, void *digest)
{
if (is_mac_algo_forbidden(algorithm))
return gnutls_assert_val(GNUTLS_E_UNWANTED_ALGORITHM);
return _gnutls_mac_fast(algorithm, key, keylen, ptext, ptext_len,
digest);
}
/* HASH */
/**
* gnutls_hash_init:
* @dig: is a #gnutls_hash_hd_t type
* @algorithm: the hash algorithm to use
*
* This function will initialize an context that can be used to
* produce a Message Digest of data. This will effectively use the
* current crypto backend in use by gnutls or the cryptographic
* accelerator in use.
*
* Returns: Zero or a negative error code on error.
*
* Since: 2.10.0
**/
int
gnutls_hash_init(gnutls_hash_hd_t * dig,
gnutls_digest_algorithm_t algorithm)
{
if (is_mac_algo_forbidden(algorithm))
return gnutls_assert_val(GNUTLS_E_UNWANTED_ALGORITHM);
*dig = gnutls_malloc(sizeof(digest_hd_st));
if (*dig == NULL) {
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
}
return _gnutls_hash_init(((digest_hd_st *) * dig),
hash_to_entry(algorithm));
}
/**
* gnutls_hash:
* @handle: is a #gnutls_hash_hd_t type
* @ptext: the data to hash
* @ptext_len: the length of data to hash
*
* This function will hash the given data using the algorithm
* specified by the context.
*
* Returns: Zero or a negative error code on error.
*
* Since: 2.10.0
**/
int gnutls_hash(gnutls_hash_hd_t handle, const void *ptext, size_t ptext_len)
{
return _gnutls_hash((digest_hd_st *) handle, ptext, ptext_len);
}
/**
* gnutls_hash_output:
* @handle: is a #gnutls_hash_hd_t type
* @digest: is the output value of the hash
*
* This function will output the current hash value
* and reset the state of the hash.
*
* Since: 2.10.0
**/
void gnutls_hash_output(gnutls_hash_hd_t handle, void *digest)
{
_gnutls_hash_output((digest_hd_st *) handle, digest);
}
/**
* gnutls_hash_deinit:
* @handle: is a #gnutls_hash_hd_t type
* @digest: is the output value of the hash
*
* This function will deinitialize all resources occupied by
* the given hash context.
*
* Since: 2.10.0
**/
void gnutls_hash_deinit(gnutls_hash_hd_t handle, void *digest)
{
_gnutls_hash_deinit((digest_hd_st *) handle, digest);
gnutls_free(handle);
}
/**
* gnutls_hash_get_len:
* @algorithm: the hash algorithm to use
*
* This function will return the length of the output data
* of the given hash algorithm.
*
* Returns: The length or zero on error.
*
* Since: 2.10.0
**/
unsigned gnutls_hash_get_len(gnutls_digest_algorithm_t algorithm)
{
return _gnutls_hash_get_algo_len(hash_to_entry(algorithm));
}
/**
* gnutls_hash_fast:
* @algorithm: the hash algorithm to use
* @ptext: the data to hash
* @ptext_len: the length of data to hash
* @digest: is the output value of the hash
*
* This convenience function will hash the given data and return output
* on a single call.
*
* Returns: Zero or a negative error code on error.
*
* Since: 2.10.0
**/
int
gnutls_hash_fast(gnutls_digest_algorithm_t algorithm,
const void *ptext, size_t ptext_len, void *digest)
{
if (is_mac_algo_forbidden(algorithm))
return gnutls_assert_val(GNUTLS_E_UNWANTED_ALGORITHM);
return _gnutls_hash_fast(algorithm, ptext, ptext_len, digest);
}
/**
* gnutls_key_generate:
* @key: is a pointer to a #gnutls_datum_t which will contain a newly
* created key
* @key_size: the number of bytes of the key
*
* Generates a random key of @key_size bytes.
*
* Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, or an
* error code.
*
* Since: 3.0
**/
int gnutls_key_generate(gnutls_datum_t * key, unsigned int key_size)
{
int ret;
FAIL_IF_LIB_ERROR;
#ifdef ENABLE_FIPS140
/* The FIPS140 approved RNGs are not allowed to be used
* to extract key sizes longer than their original seed.
*/
if (_gnutls_fips_mode_enabled() != 0 &&
key_size > FIPS140_RND_KEY_SIZE)
return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
#endif
key->size = key_size;
key->data = gnutls_malloc(key->size);
if (!key->data) {
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
}
ret = gnutls_rnd(GNUTLS_RND_RANDOM, key->data, key->size);
if (ret < 0) {
gnutls_assert();
_gnutls_free_datum(key);
return ret;
}
return 0;
}
/* AEAD API */
/**
* gnutls_aead_cipher_init:
* @handle: is a #gnutls_aead_cipher_hd_t type.
* @cipher: the authenticated-encryption algorithm to use
* @key: The key to be used for encryption
*
* This function will initialize an context that can be used for
* encryption/decryption of data. This will effectively use the
* current crypto backend in use by gnutls or the cryptographic
* accelerator in use.
*
* Returns: Zero or a negative error code on error.
*
* Since: 3.4.0
**/
int gnutls_aead_cipher_init(gnutls_aead_cipher_hd_t *handle,
gnutls_cipher_algorithm_t cipher,
const gnutls_datum_t *key)
{
api_aead_cipher_hd_st *h;
const cipher_entry_st *e;
if (is_cipher_algo_forbidden(cipher))
return gnutls_assert_val(GNUTLS_E_UNWANTED_ALGORITHM);
e = cipher_to_entry(cipher);
if (e == NULL || e->type != CIPHER_AEAD)
return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
*handle = gnutls_calloc(1, sizeof(api_aead_cipher_hd_st));
if (*handle == NULL) {
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
}
h = *handle;
return _gnutls_aead_cipher_init(h, cipher, key);
}
/**
* gnutls_aead_cipher_decrypt:
* @handle: is a #gnutls_aead_cipher_hd_t type.
* @nonce: the nonce to set
* @nonce_len: The length of the nonce
* @auth: additional data to be authenticated
* @auth_len: The length of the data
* @tag_size: The size of the tag to use (use zero for the default)
* @ctext: the data to decrypt (including the authentication tag)
* @ctext_len: the length of data to decrypt (includes tag size)
* @ptext: the decrypted data
* @ptext_len: the length of decrypted data (initially must hold the maximum available size)
*
* This function will decrypt the given data using the algorithm
* specified by the context. This function must be provided the complete
* data to be decrypted, including the authentication tag. On several
* AEAD ciphers, the authentication tag is appended to the ciphertext,
* though this is not a general rule. This function will fail if
* the tag verification fails.
*
* Returns: Zero or a negative error code on verification failure or other error.
*
* Since: 3.4.0
**/
int
gnutls_aead_cipher_decrypt(gnutls_aead_cipher_hd_t handle,
const void *nonce, size_t nonce_len,
const void *auth, size_t auth_len,
size_t tag_size,
const void *ctext, size_t ctext_len,
void *ptext, size_t *ptext_len)
{
int ret;
api_aead_cipher_hd_st *h = handle;
if (tag_size == 0)
tag_size = _gnutls_cipher_get_tag_size(h->ctx_enc.e);
else if (tag_size > (unsigned)_gnutls_cipher_get_tag_size(h->ctx_enc.e))
return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
if (unlikely(ctext_len < tag_size))
return gnutls_assert_val(GNUTLS_E_DECRYPTION_FAILED);
ret = _gnutls_aead_cipher_decrypt(&h->ctx_enc,
nonce, nonce_len,
auth, auth_len,
tag_size,
ctext, ctext_len,
ptext, *ptext_len);
if (unlikely(ret < 0))
return gnutls_assert_val(ret);
/* That assumes that AEAD ciphers are stream */
*ptext_len = ctext_len - tag_size;
return 0;
}
/**
* gnutls_aead_cipher_encrypt:
* @handle: is a #gnutls_aead_cipher_hd_t type.
* @nonce: the nonce to set
* @nonce_len: The length of the nonce
* @auth: additional data to be authenticated
* @auth_len: The length of the data
* @tag_size: The size of the tag to use (use zero for the default)
* @ptext: the data to encrypt
* @ptext_len: The length of data to encrypt
* @ctext: the encrypted data including authentication tag
* @ctext_len: the length of encrypted data (initially must hold the maximum available size, including space for tag)
*
* This function will encrypt the given data using the algorithm
* specified by the context. The output data will contain the
* authentication tag.
*
* Returns: Zero or a negative error code on error.
*
* Since: 3.4.0
**/
int
gnutls_aead_cipher_encrypt(gnutls_aead_cipher_hd_t handle,
const void *nonce, size_t nonce_len,
const void *auth, size_t auth_len,
size_t tag_size,
const void *ptext, size_t ptext_len,
void *ctext, size_t *ctext_len)
{
api_aead_cipher_hd_st *h = handle;
int ret;
if (tag_size == 0)
tag_size = _gnutls_cipher_get_tag_size(h->ctx_enc.e);
else if (tag_size > (unsigned)_gnutls_cipher_get_tag_size(h->ctx_enc.e))
return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
if (unlikely(*ctext_len < ptext_len + tag_size))
return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER);
ret = _gnutls_aead_cipher_encrypt(&h->ctx_enc,
nonce, nonce_len,
auth, auth_len,
tag_size,
ptext, ptext_len,
ctext, *ctext_len);
if (unlikely(ret < 0))
return gnutls_assert_val(ret);
/* That assumes that AEAD ciphers are stream */
*ctext_len = ptext_len + tag_size;
return 0;
}
struct iov_store_st {
void *data;
size_t size;
unsigned allocated;
};
static void iov_store_free(struct iov_store_st *s)
{
if (s->allocated) {
gnutls_free(s->data);
s->allocated = 0;
}
}
static int copy_iov(struct iov_store_st *dst, const giovec_t *iov, int iovcnt)
{
memset(dst, 0, sizeof(*dst));
if (iovcnt == 0) {
return 0;
} else if (iovcnt == 1) {
dst->data = iov[0].iov_base;
dst->size = iov[0].iov_len;
/* implies: dst->allocated = 0; */
return 0;
} else {
int i;
uint8_t *p;
dst->size = 0;
for (i=0;i<iovcnt;i++)
dst->size += iov[i].iov_len;
dst->data = gnutls_malloc(dst->size);
if (dst->data == NULL)
return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
p = dst->data;
for (i=0;i<iovcnt;i++) {
memcpy(p, iov[i].iov_base, iov[i].iov_len);
p += iov[i].iov_len;
}
dst->allocated = 1;
return 0;
}
}
#define AUTH_UPDATE_FINAL(ctx) do { \
if (index) { \
ret = _gnutls_cipher_auth(ctx, cache, index); \
if (unlikely(ret < 0)) \
return gnutls_assert_val(ret); \
} \
} while(0)
#define AUTH_UPDATE(ctx, data, length) do { \
if (index) { \
ssize_t left = blocksize - index; \
if (length < left) { \
memcpy(cache+index, data, \
length); \
index += length; \
goto __update_done; \
} else { \
memcpy(cache+index, data, left); \
ret = _gnutls_cipher_auth(ctx, cache, blocksize); \
if (unlikely(ret < 0)) \
return gnutls_assert_val(ret); \
data += left; \
length -= left; \
} \
} \
if (length >= blocksize) { \
ssize_t to_proc = (length/blocksize)*blocksize; \
ret = _gnutls_cipher_auth(ctx, data, to_proc); \
if (unlikely(ret < 0)) \
return gnutls_assert_val(ret); \
data += to_proc; \
length -= to_proc; \
} \
if (length) \
memcpy(cache, data, length); \
index = length; \
__update_done: \
; \
} while(0)
#define ENCRYPT_FINAL(ctx, dst, dst_size) do { \
if (index) { \
if (unlikely(dst_size < (ssize_t)index)) \
return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER); \
ret = _gnutls_cipher_encrypt2(ctx, cache, index, dst, dst_size); \
if (unlikely(ret < 0)) \
return gnutls_assert_val(ret); \
dst += index; \
dst_size -= index; \
} \
} while(0)
#define ENCRYPT(ctx, data, length, dst, dst_size) do { \
if (index) { \
ssize_t left = blocksize - index; \
if (length < left) { \
memcpy(cache+index, data, \
length); \
index += length; \
goto __encrypt_done; \
} else { \
if (unlikely(dst_size < blocksize)) \
return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER); \
memcpy(cache+index, data, left); \
ret = _gnutls_cipher_encrypt2(ctx, cache, blocksize, dst, dst_size); \
if (unlikely(ret < 0)) \
return gnutls_assert_val(ret); \
data += left; \
length -= left; \
dst += blocksize; \
dst_size -= blocksize; \
} \
} \
if (length >= blocksize) { \
ssize_t to_proc = (length/blocksize)*blocksize; \
if (unlikely(dst_size < to_proc)) \
return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER); \
ret = _gnutls_cipher_encrypt2(ctx, data, to_proc, dst, dst_size); \
if (unlikely(ret < 0)) \
return gnutls_assert_val(ret); \
data += to_proc; \
length -= to_proc; \
dst += to_proc; \
dst_size -= to_proc; \
} \
if (length) \
memcpy(cache, data, length); \
index = length; \
__encrypt_done: \
; \
} while(0)
/**
* gnutls_aead_cipher_encryptv:
* @handle: is a #gnutls_aead_cipher_hd_t type.
* @nonce: the nonce to set
* @nonce_len: The length of the nonce
* @auth_iov: additional data to be authenticated
* @auth_iovcnt: The number of buffers in @auth_iov
* @tag_size: The size of the tag to use (use zero for the default)
* @iov: the data to be encrypted
* @iovcnt: The number of buffers in @iov
* @ctext: the encrypted data including authentication tag
* @ctext_len: the length of encrypted data (initially must hold the maximum available size, including space for tag)
*
* This function will encrypt the provided data buffers using the algorithm
* specified by the context. The output data will contain the
* authentication tag.
*
* Returns: Zero or a negative error code on error.
*
* Since: 3.6.3
**/
int
gnutls_aead_cipher_encryptv(gnutls_aead_cipher_hd_t handle,
const void *nonce, size_t nonce_len,
const giovec_t *auth_iov, int auth_iovcnt,
size_t tag_size,
const giovec_t *iov, int iovcnt,
void *ctext, size_t *ctext_len)
{
api_aead_cipher_hd_st *h = handle;
int ret;
uint8_t *dst;
ssize_t dst_size, total = 0, len;
uint8_t *p;
unsigned i;
uint8_t cache[MAX_CIPHER_BLOCK_SIZE];
unsigned index;
ssize_t blocksize = handle->ctx_enc.e->blocksize;
/* Limitation: this function provides an optimization under the internally registered
* AEAD ciphers. When an AEAD cipher is used registered with gnutls_crypto_register_aead_cipher(),
* then this becomes a convenience function as it missed the lower-level primitives
* necessary for piecemeal encryption. */
if (tag_size == 0)
tag_size = _gnutls_cipher_get_tag_size(h->ctx_enc.e);
else if (tag_size > (unsigned)_gnutls_cipher_get_tag_size(h->ctx_enc.e))
return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
if (handle->ctx_enc.e->only_aead || handle->ctx_enc.encrypt == NULL) {
/* ciphertext cannot be produced in a piecemeal approach */
struct iov_store_st auth;
struct iov_store_st ptext;
ret = copy_iov(&auth, auth_iov, auth_iovcnt);
if (ret < 0)
return gnutls_assert_val(ret);
ret = copy_iov(&ptext, iov, iovcnt);
if (ret < 0) {
iov_store_free(&auth);
return gnutls_assert_val(ret);
}
ret = gnutls_aead_cipher_encrypt(handle, nonce, nonce_len,
auth.data, auth.size,
tag_size,
ptext.data, ptext.size,
ctext, ctext_len);
iov_store_free(&auth);
iov_store_free(&ptext);
return ret;
}
ret = _gnutls_cipher_setiv(&handle->ctx_enc, nonce, nonce_len);
if (unlikely(ret < 0))
return gnutls_assert_val(ret);
index = 0;
for (i = 0; i < (unsigned)auth_iovcnt; i++) {
p = auth_iov[i].iov_base;
len = auth_iov[i].iov_len;
AUTH_UPDATE(&handle->ctx_enc, p, len);
}
AUTH_UPDATE_FINAL(&handle->ctx_enc);
dst = ctext;
dst_size = *ctext_len;
index = 0;
for (i = 0; i < (unsigned)iovcnt; i++) {
p = iov[i].iov_base;
len = iov[i].iov_len;
ENCRYPT(&handle->ctx_enc, p, len, dst, dst_size);
total += iov[i].iov_len;
}
ENCRYPT_FINAL(&handle->ctx_enc, dst, dst_size);
if ((size_t)dst_size < tag_size)
return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER);
_gnutls_cipher_tag(&handle->ctx_enc, dst, tag_size);
total += tag_size;
*ctext_len = total;
return 0;
}
/**
* gnutls_aead_cipher_deinit:
* @handle: is a #gnutls_aead_cipher_hd_t type.
*
* This function will deinitialize all resources occupied by the given
* authenticated-encryption context.
*
* Since: 3.4.0
**/
void gnutls_aead_cipher_deinit(gnutls_aead_cipher_hd_t handle)
{
_gnutls_aead_cipher_deinit(handle);
gnutls_free(handle);
}
|