forked from wolfSSL/wolfip
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwolfesp.c
More file actions
1744 lines (1507 loc) · 50 KB
/
Copy pathwolfesp.c
File metadata and controls
1744 lines (1507 loc) · 50 KB
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
/* wolfesp.c
*
* Copyright (C) 2026 wolfSSL Inc.
*
* This file is part of wolfIP TCP/IP stack.
*
* wolfIP is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfIP 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#if defined(WOLFIP_ESP) && !defined(WOLFESP_SRC)
#define WOLFESP_SRC
#include "wolfesp.h"
static WC_RNG wc_rng;
static volatile int rng_inited = 0;
/* security association static pool*/
static wolfIP_esp_sa in_sa_list[WOLFIP_ESP_NUM_SA];
static wolfIP_esp_sa out_sa_list[WOLFIP_ESP_NUM_SA];
static uint16_t in_sa_num = WOLFIP_ESP_NUM_SA;
static uint16_t out_sa_num = WOLFIP_ESP_NUM_SA;
/* for err and important messages */
#define ESP_LOG(fmt, ...) LOG(fmt, ##__VA_ARGS__)
/* for verbose debug */
#ifdef DEBUG_ESP
#define ESP_DEBUG(fmt, ...) LOG(fmt, ##__VA_ARGS__)
#else
#define ESP_DEBUG(fmt, ...) do { } while (0)
#endif /* DEBUG_ESP */
int wolfIP_esp_init(void)
{
int err = 0;
wolfIP_esp_sa_del_all();
/* this callback gets called only if wolfssl is built in FIPS mode. */
#ifdef WC_RNG_SEED_CB
wc_SetSeed_Cb(wc_GenerateSeed);
#endif
if (rng_inited == 0) {
err = wc_InitRng_ex(&wc_rng, NULL, INVALID_DEVID);
if (err) {
ESP_LOG("error: wc_InitRng_ex: %d\n", err);
}
else {
rng_inited = 1;
}
}
return err;
}
void wolfIP_esp_sa_del_all(void)
{
wc_ForceZero(in_sa_list, sizeof(in_sa_list));
wc_ForceZero(out_sa_list, sizeof(out_sa_list));
return;
}
static inline wolfIP_esp_sa *
esp_sa_get(int in, const uint8_t * spi)
{
uint8_t empty_sa[4] = {0x00, 0x00, 0x00, 0x00};
wolfIP_esp_sa * list = NULL;
size_t i = 0;
if (spi == NULL) {
spi = empty_sa;
}
in = (in == 0 ? 0 : 1);
if (in == 1) {
list = in_sa_list;
}
else {
list = out_sa_list;
}
for (i = 0; i < WOLFIP_ESP_NUM_SA; ++i) {
if (memcmp(list[i].spi, spi, ESP_SPI_LEN) == 0) {
return &list[i];
}
}
return NULL;
}
void wolfIP_esp_sa_del(int in, uint8_t * spi)
{
wolfIP_esp_sa * sa = NULL;
sa = esp_sa_get(in, spi);
if (sa != NULL) {
wc_ForceZero(sa, sizeof(*sa));
}
return;
}
/* Configure a new Security Association based on either
* enc = ESP_ENC_GCM_RFC4106 (gcm), or enc = ESP_AUTH_GCM_RFC4543 (gmac).
* */
int wolfIP_esp_sa_new_gcm(int in, uint8_t * spi, ip4 src, ip4 dst,
esp_enc_t enc, uint8_t * enc_key,
uint8_t enc_key_len)
{
wolfIP_esp_sa * new_sa = NULL;
int err = 0;
esp_auth_t auth = 0;
new_sa = esp_sa_get(in, NULL);
if (new_sa == NULL) {
ESP_LOG("error: sa %s pool is full\n", in == 1 ? "in" : "out");
return -1;
}
if (enc_key == NULL) {
ESP_LOG("error: null aes-gcm key\n");
return -1;
}
if (enc_key_len != (AES_128_KEY_SIZE + ESP_GCM_RFC4106_SALT_LEN) &&
enc_key_len != (AES_192_KEY_SIZE + ESP_GCM_RFC4106_SALT_LEN) &&
enc_key_len != (AES_256_KEY_SIZE + ESP_GCM_RFC4106_SALT_LEN)) {
ESP_LOG("error: bad aes-gcm key len: %d\n", enc_key_len);
return -1;
}
switch (enc) {
#if defined(WOLFSSL_AESGCM_STREAM)
case ESP_ENC_GCM_RFC4106:
auth = ESP_AUTH_GCM_RFC4106;
break;
#endif /* WOLFSSL_AESGCM_STREAM */
case ESP_ENC_GCM_RFC4543:
auth = ESP_AUTH_GCM_RFC4543;
break;
default:
ESP_LOG("error: unsupported enc: %d\n", enc);
return -1;
}
memset(new_sa, 0, sizeof(*new_sa));
esp_replay_init(new_sa->replay);
memcpy(new_sa->spi, spi, ESP_SPI_LEN);
memcpy(new_sa->enc_key, enc_key, enc_key_len);
new_sa->src = src;
new_sa->dst = dst;
new_sa->enc = enc;
new_sa->enc_key_len = enc_key_len;
new_sa->auth = auth;
/* rfc4106 and rfc4543 follow the same IV and ICV standards. */
new_sa->icv_len = ESP_GCM_RFC4106_ICV_LEN;
/* Generate pre-iv for gcm. */
err = wc_RNG_GenerateBlock(&wc_rng, new_sa->pre_iv,
ESP_GCM_RFC4106_IV_LEN);
if (err) {
ESP_LOG("error: wc_RNG_GenerateBlock: %d\n", err);
wc_ForceZero(new_sa, sizeof(*new_sa));
err = -1;
}
ESP_DEBUG("info: esp_sa_new_gcm: %s\n", in == 1 ? "in" : "out");
return err;
}
/* Check if valid hmac auth config:
* returns 0 if ok
* returns -1 on err
* */
static inline int
esp_sa_valid_hmac_auth(esp_auth_t auth, uint8_t * auth_key,
uint8_t auth_key_len, uint8_t icv_len)
{
/* auth key is optional, but auth config must be correct if present. */
if (auth_key == NULL) {
/* null auth key is OK if all other fields are none or 0. */
if (auth == ESP_AUTH_NONE && auth_key_len == 0 && icv_len == 0) {
return 0;
}
else {
ESP_LOG("error: null auth key with non zero parameters\n");
return -1;
}
}
switch (auth) {
case ESP_AUTH_MD5_RFC2403:
case ESP_AUTH_SHA1_RFC2404:
if (icv_len != ESP_ICVLEN_HMAC_96) {
ESP_LOG("error: hmac-[md5, sha1]: bad icv_len: %d\n", icv_len);
return -1;
}
break;
case ESP_AUTH_SHA256_RFC4868:
if (icv_len != ESP_ICVLEN_HMAC_96 && icv_len != ESP_ICVLEN_HMAC_128) {
ESP_LOG("error: hmac-sha256: bad icv_len: %d\n", icv_len);
return -1;
}
break;
case ESP_AUTH_NONE:
default:
ESP_LOG("error: unsupported hmac auth: %d\n", auth);
return -1;
}
if (auth_key_len > ESP_MAX_KEY_LEN) {
ESP_LOG("error: bad auth key len: %d\n", auth_key_len);
return -1;
}
return 0;
}
/* Configure a new hmac auth only Security Association.
* */
int wolfIP_esp_sa_new_hmac(int in, uint8_t * spi, ip4 src, ip4 dst,
esp_auth_t auth, uint8_t * auth_key,
uint8_t auth_key_len, uint8_t icv_len)
{
wolfIP_esp_sa * new_sa = NULL;
new_sa = esp_sa_get(in, NULL);
if (new_sa == NULL) {
ESP_LOG("error: sa %s pool is full\n", in == 1 ? "in" : "out");
return -1;
}
if (auth_key == NULL) {
/* auth key not optional for auth only. */
ESP_LOG("error: null auth key with auth only\n");
return -1;
}
if (esp_sa_valid_hmac_auth(auth, auth_key, auth_key_len, icv_len) != 0) {
return -1;
}
memset(new_sa, 0, sizeof(*new_sa));
esp_replay_init(new_sa->replay);
memcpy(new_sa->spi, spi, ESP_SPI_LEN);
memcpy(new_sa->auth_key, auth_key, auth_key_len);
new_sa->src = src;
new_sa->dst = dst;
new_sa->enc = ESP_ENC_NONE;
new_sa->auth = auth;
new_sa->auth_key_len = auth_key_len;
new_sa->icv_len = icv_len;
ESP_DEBUG("info: esp_sa_new_hmac: %s\n", in == 1 ? "in" : "out");
return 0;
}
/* Configure a new Security Association based on aes-cbc with hmac auth.
* - enc is required.
* - auth may be null/none.
* */
int wolfIP_esp_sa_new_cbc_hmac(int in, uint8_t * spi, ip4 src, ip4 dst,
uint8_t * enc_key, uint8_t enc_key_len,
esp_auth_t auth, uint8_t * auth_key,
uint8_t auth_key_len, uint8_t icv_len)
{
wolfIP_esp_sa * new_sa = NULL;
new_sa = esp_sa_get(in, NULL);
if (new_sa == NULL) {
ESP_LOG("error: sa %s pool is full\n", in == 1 ? "in" : "out");
return -1;
}
if (enc_key == NULL) {
ESP_LOG("error: null aes-cbc key\n");
return -1;
}
if (enc_key_len != (AES_128_KEY_SIZE) &&
enc_key_len != (AES_192_KEY_SIZE) &&
enc_key_len != (AES_256_KEY_SIZE)) {
ESP_LOG("error: bad aes key len: %d\n", enc_key_len);
return -1;
}
if (esp_sa_valid_hmac_auth(auth, auth_key, auth_key_len, icv_len) != 0) {
return -1;
}
memset(new_sa, 0, sizeof(*new_sa));
esp_replay_init(new_sa->replay);
memcpy(new_sa->spi, spi, ESP_SPI_LEN);
memcpy(new_sa->enc_key, enc_key, enc_key_len);
if (auth_key != NULL) {
memcpy(new_sa->auth_key, auth_key, auth_key_len);
}
new_sa->src = src;
new_sa->dst = dst;
new_sa->enc = ESP_ENC_CBC_AES;
new_sa->enc_key_len = enc_key_len;
new_sa->auth = auth;
new_sa->auth_key_len = auth_key_len;
new_sa->icv_len = icv_len;
ESP_DEBUG("info: esp_sa_new_cbc_hmac: %s\n", in == 1 ? "in" : "out");
return 0;
}
#ifndef NO_DES3
/* Configure a new Security Association based on des3 with hmac.
* - enc is required.
* - auth may be null/none.
* */
int
wolfIP_esp_sa_new_des3_hmac(int in, uint8_t * spi, ip4 src, ip4 dst,
uint8_t * enc_key, esp_auth_t auth,
uint8_t * auth_key, uint8_t auth_key_len,
uint8_t icv_len)
{
wolfIP_esp_sa * new_sa = NULL;
new_sa = esp_sa_get(in, NULL);
if (new_sa == NULL) {
ESP_LOG("error: sa %s pool is full\n", in == 1 ? "in" : "out");
return -1;
}
if (enc_key == NULL) {
ESP_LOG("error: null des3 key\n");
return -1;
}
if (esp_sa_valid_hmac_auth(auth, auth_key, auth_key_len, icv_len) != 0) {
return -1;
}
memset(new_sa, 0, sizeof(*new_sa));
esp_replay_init(new_sa->replay);
memcpy(new_sa->spi, spi, ESP_SPI_LEN);
memcpy(new_sa->enc_key, enc_key, ESP_DES3_KEY_LEN);
if (auth_key != NULL) {
memcpy(new_sa->auth_key, auth_key, auth_key_len);
}
new_sa->src = src;
new_sa->dst = dst;
new_sa->enc = ESP_ENC_CBC_DES3;
new_sa->enc_key_len = ESP_DES3_KEY_LEN;
new_sa->auth = auth;
new_sa->auth_key_len = auth_key_len;
new_sa->icv_len = icv_len;
ESP_DEBUG("info: esp_sa_new_des3_hmac: %s\n", in == 1 ? "in" : "out");
return 0;
}
#endif /* !NO_DES3 */
static uint8_t
esp_block_len_from_enc(esp_enc_t enc)
{
uint8_t block_len = 0;
switch (enc) {
#ifndef NO_DES3
case ESP_ENC_CBC_DES3:
block_len = DES_BLOCK_SIZE;
break;
#endif /* !NO_DES3 */
case ESP_ENC_CBC_AES:
block_len = AES_BLOCK_SIZE;
break;
#if defined(WOLFSSL_AESGCM_STREAM)
case ESP_ENC_GCM_RFC4106:
#endif /* WOLFSSL_AESGCM_STREAM */
case ESP_ENC_GCM_RFC4543:
case ESP_ENC_NONE:
default:
block_len = 0;
break;
}
return block_len;
}
static uint8_t
esp_iv_len_from_enc(esp_enc_t enc)
{
uint8_t iv_len = 0;
switch (enc) {
#ifndef NO_DES3
case ESP_ENC_CBC_DES3:
iv_len = ESP_DES3_IV_LEN;
break;
#endif /* !NO_DES3 */
case ESP_ENC_CBC_AES:
iv_len = ESP_CBC_RFC3602_IV_LEN;
break;
#if defined(WOLFSSL_AESGCM_STREAM)
case ESP_ENC_GCM_RFC4106:
#endif /* WOLFSSL_AESGCM_STREAM */
case ESP_ENC_GCM_RFC4543:
iv_len = ESP_GCM_RFC4106_IV_LEN;
break;
case ESP_ENC_NONE:
default:
iv_len = 0;
break;
}
return iv_len;
}
#ifdef DEBUG_ESP
#define esp_print_sep \
LOG("+------------------+\n")
#define esp_str_4hex \
"| %02x %02x %02x %02x |"
#define esp_str_skip \
"| .. .. .. .. |"
#define esp_pad_fld \
"| %02x%02x | %02d | 0x%02x |"
static inline void
esp_print_field(const char * fld, const uint8_t * val,
uint32_t val_len)
{
esp_print_sep;
LOG(esp_str_4hex " (%s, %d bytes)\n",
val[0], val[1], val[2], val[3], fld, val_len);
if (val_len > 4) {
for (size_t i = 4; i < val_len; i += 4) {
if (i > 16 || (i + 4) > val_len) {
LOG(esp_str_skip "\n");
break;
}
LOG(esp_str_4hex"\n",
val[0 + i], val[1 + i], val[2 + i], val[3 + i]);
}
}
return;
}
/**
* Print an ESP packet.
* _______________________________________________
* |orig IP hdr | ESP | UDP | | ESP | ESP |
* |(PROTO=50) | hdr | hdr | Data | Trailer | ICV |
* -----------------------------------------------
* |<---- encrypted ----->|
* |<--- integrity checked ---->|
* */
static void wolfIP_print_esp(const wolfIP_esp_sa * esp_sa,
const uint8_t * esp_data, uint32_t esp_len,
uint8_t pad_len, uint8_t nxt_hdr)
{
const uint8_t * spi = esp_data;
const uint8_t * seq = esp_data + ESP_SPI_LEN;
const uint8_t * payload = esp_data + ESP_SPI_LEN + ESP_SEQ_LEN;
const uint8_t * iv = NULL;
const uint8_t * icv = NULL;
uint8_t iv_len = 0;
const uint8_t * padding = NULL;
uint32_t payload_len = esp_len - ESP_SPI_LEN - ESP_SEQ_LEN
- pad_len - ESP_PADDING_LEN
- ESP_NEXT_HEADER_LEN - esp_sa->icv_len;
iv_len = esp_iv_len_from_enc(esp_sa->enc);
if (iv_len) {
iv = payload;
payload += iv_len;
payload_len -= iv_len;
}
if (esp_sa->icv_len) {
icv = esp_data + esp_len - esp_sa->icv_len;
}
/* last 2 bytes of padding */
padding = esp_data + esp_len - esp_sa->icv_len - 4;
LOG("esp packet: (%d bytes)\n", esp_len);
/** ESP header
* ______________
* | SPI | Seq |
* | | Number |
* -------------- */
esp_print_field("spi", spi, ESP_SPI_LEN);
esp_print_field("seq", seq, ESP_SEQ_LEN);
/**
* ESP payload (includes IV).
* */
if (iv) {
esp_print_field("iv", iv, iv_len);
}
esp_print_field("payload", payload, payload_len);
/** ESP trailer
* _____________________________________
* | Padding | Pad | Next |
* | (variable length) | Length | Header |
* ------------------------------------- */
esp_print_sep;
LOG(esp_pad_fld " (padding last 2 bytes, pad len, nxt hdr)\n",
padding[0], padding[1], pad_len, nxt_hdr);
if (icv) {
esp_print_field("icv", icv, esp_sa->icv_len);
}
esp_print_sep;
return;
}
#endif /* DEBUG_ESP */
/*
* esp_data covers from start of ESP header to end of ESP trailer, but does not
* include the ESP ICV after trailer.
* */
static int
esp_calc_icv_hmac(uint8_t * hash, const wolfIP_esp_sa * esp_sa,
const uint8_t * esp_data, uint32_t esp_len)
{
/* SHA1 and MD5 have these digest sizes:
* - WC_SHA_DIGEST_SIZE 20 bytes
* - WC_MD5_DIGEST_SIZE 16 bytes
* */
Hmac hmac;
int err = 0;
int type = 0;
uint32_t auth_len = esp_len;
switch (esp_sa->auth) {
case ESP_AUTH_MD5_RFC2403:
type = WC_MD5;
break;
case ESP_AUTH_SHA1_RFC2404:
type = WC_SHA;
break;
case ESP_AUTH_SHA256_RFC4868:
type = WC_SHA256;
break;
case ESP_AUTH_NONE:
default:
ESP_LOG("error: esp_calc_icv_hmac: invalid auth: %d\n", esp_sa->auth);
return -1;
}
/* the icv is not included in icv calculation. */
auth_len = esp_len - esp_sa->icv_len;
err = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
if (err) {
ESP_LOG("error: wc_HmacSetKey: %d\n", err);
goto calc_icv_hmac_end;
}
err = wc_HmacSetKey(&hmac, type, esp_sa->auth_key, esp_sa->auth_key_len);
if (err) {
ESP_LOG("error: wc_HmacSetKey: %d\n", err);
goto calc_icv_hmac_end;
}
/* Now calculate the ICV. The ICV covers from SPI to Next Header,
* inclusive. */
err = wc_HmacUpdate(&hmac, (const byte *)esp_data, auth_len);
if (err) {
ESP_LOG("error: wc_HmacUpdate: %d\n", err);
goto calc_icv_hmac_end;
}
err = wc_HmacFinal(&hmac, hash);
if (err) {
ESP_LOG("error: wc_HmacFinal: %d\n", err);
goto calc_icv_hmac_end;
}
calc_icv_hmac_end:
wc_HmacFree(&hmac);
return err;
}
/* From wolfcrypt misc.c */
static int
esp_const_memcmp(const uint8_t * vec_a, const uint8_t * vec_b, uint32_t len)
{
uint32_t i = 0;
int sum = 0;
for (i = 0; i < len; i++) {
sum |= vec_a[i] ^ vec_b[i];
}
return sum;
}
/**
* Get the encryption length for an ESP payload.
* */
#define esp_enc_len(esp_len, iv_len, icv_len) \
((esp_len) - ESP_SPI_LEN - ESP_SEQ_LEN \
- (iv_len) - (icv_len))
/**
* Get pointer to raw encryption ESP IV, skipping ESP header.
* */
#define esp_enc_iv(data) \
((data) + ESP_SPI_LEN + ESP_SEQ_LEN)
/**
* Get pointer to raw encryption ESP ICV.
* */
#define esp_enc_icv(data, esp_len, icv_len) \
((data) + (esp_len) - (icv_len))
/**
* Get pointer to raw encryption ESP payload, skipping ESP header and IV.
* */
#define esp_enc_payload(data, iv_len) \
((data) + ESP_SPI_LEN + ESP_SEQ_LEN + (iv_len))
static int
esp_aes_rfc3602_dec(const wolfIP_esp_sa * esp_sa, uint8_t * esp_data,
uint32_t esp_len)
{
Aes cbc_dec;
int ret = -1;
uint8_t icv_len = esp_sa->icv_len;
uint8_t iv_len = ESP_CBC_RFC3602_IV_LEN;
uint8_t * enc_payload = NULL;
uint8_t * iv = NULL;
uint32_t enc_len = 0;
uint8_t inited = 0;
ESP_DEBUG("info: aes cbc dec: %d\n", esp_len);
enc_len = esp_enc_len(esp_len, iv_len, icv_len);
enc_payload = esp_enc_payload(esp_data, iv_len);
iv = esp_enc_iv(esp_data);
ret = wc_AesInit(&cbc_dec, NULL, INVALID_DEVID);
if (ret != 0) {
ESP_LOG("error: wc_AesInit: %d\n", ret);
goto aes_dec_out;
}
inited = 1;
ret = wc_AesSetKey(&cbc_dec, esp_sa->enc_key, esp_sa->enc_key_len,
iv, AES_DECRYPTION);
if (ret != 0) {
ESP_LOG("error: wc_AesSetKey: %d\n", ret);
goto aes_dec_out;
}
/* decrypt in place. */
ret = wc_AesCbcDecrypt(&cbc_dec, enc_payload, enc_payload, enc_len);
if (ret != 0) {
ESP_LOG("error: wc_AesCbcDecrypt: %d\n", ret);
goto aes_dec_out;
}
aes_dec_out:
if (inited) {
wc_AesFree(&cbc_dec);
inited = 0;
}
return ret;
}
static int
esp_aes_rfc3602_enc(const wolfIP_esp_sa * esp_sa, uint8_t * esp_data,
uint32_t esp_len)
{
Aes cbc_enc;
int ret = -1;
uint8_t icv_len = esp_sa->icv_len;
uint8_t iv_len = ESP_CBC_RFC3602_IV_LEN;
uint8_t * enc_payload = NULL;
uint8_t * iv = NULL;
uint32_t enc_len = 0;
uint8_t inited = 0;
ESP_DEBUG("info: aes cbc enc: %d\n", esp_len);
enc_len = esp_enc_len(esp_len, iv_len, icv_len);
enc_payload = esp_enc_payload(esp_data, iv_len);
iv = esp_enc_iv(esp_data);
/* Generate random iv block for cbc method. */
ret = wc_RNG_GenerateBlock(&wc_rng, iv, iv_len);
if (ret) {
ESP_LOG("error: wc_RNG_GenerateBlock: %d\n", ret);
goto aes_enc_out;
}
ret = wc_AesInit(&cbc_enc, NULL, INVALID_DEVID);
if (ret != 0) {
ESP_LOG("error: wc_AesInit: %d\n", ret);
goto aes_enc_out;
}
inited = 1;
ret = wc_AesSetKey(&cbc_enc, esp_sa->enc_key, esp_sa->enc_key_len,
iv, AES_ENCRYPTION);
if (ret != 0) {
ESP_LOG("error: wc_AesSetKey: %d\n", ret);
goto aes_enc_out;
}
ret = wc_AesCbcEncrypt(&cbc_enc, enc_payload, enc_payload, enc_len);
if (ret != 0) {
ESP_LOG("error: wc_AesCbcEncrypt: %d\n", ret);
goto aes_enc_out;
}
aes_enc_out:
if (inited) {
wc_AesFree(&cbc_enc);
inited = 0;
}
return ret;
}
#ifndef NO_DES3
static int
esp_des3_rfc2451_dec(const wolfIP_esp_sa * esp_sa, uint8_t * esp_data,
uint32_t esp_len)
{
Des3 des3_dec;
int ret = -1;
uint8_t icv_len = esp_sa->icv_len;
uint8_t iv_len = ESP_DES3_IV_LEN;
uint8_t * enc_payload = NULL;
uint8_t * iv = NULL;
uint32_t enc_len = 0;
uint8_t inited = 0;
ESP_DEBUG("info: des3 dec: %d\n", esp_len);
if (esp_sa->enc_key_len != ESP_DES3_KEY_LEN) {
ESP_LOG("error: des3_rfc2451_dec: key len = %d, expected %d\n",
esp_sa->enc_key_len, ESP_DES3_KEY_LEN);
goto des3_dec_out;
}
enc_len = esp_enc_len(esp_len, iv_len, icv_len);
enc_payload = esp_enc_payload(esp_data, iv_len);
iv = esp_enc_iv(esp_data);
ret = wc_Des3Init(&des3_dec, NULL, INVALID_DEVID);
if (ret != 0) {
ESP_LOG("error: wc_Des3Init: %d\n", ret);
goto des3_dec_out;
}
inited = 1;
ret = wc_Des3_SetKey(&des3_dec, esp_sa->enc_key, iv, DES_DECRYPTION);
if (ret != 0) {
ESP_LOG("error: wc_Des3_SetKey: %d\n", ret);
goto des3_dec_out;
}
/* decrypt in place. */
ret = wc_Des3_CbcDecrypt(&des3_dec, enc_payload, enc_payload, enc_len);
if (ret != 0) {
ESP_LOG("error: wc_Des3_CbcDecrypt: %d\n", ret);
goto des3_dec_out;
}
des3_dec_out:
if (inited) {
wc_Des3Free(&des3_dec);
inited = 0;
}
return ret;
}
static int
esp_des3_rfc2451_enc(const wolfIP_esp_sa * esp_sa, uint8_t * esp_data,
uint32_t esp_len)
{
Des3 des3_enc;
int ret = -1;
uint8_t icv_len = esp_sa->icv_len;
uint8_t iv_len = ESP_DES3_IV_LEN;
uint8_t * enc_payload = NULL;
uint8_t * iv = NULL;
uint32_t enc_len = 0;
uint8_t inited = 0;
ESP_DEBUG("info: des3 enc: %d\n", esp_len);
if (esp_sa->enc_key_len != ESP_DES3_KEY_LEN) {
ESP_LOG("error: des3_rfc2451_enc: key len = %d, expected %d\n",
esp_sa->enc_key_len, ESP_DES3_KEY_LEN);
goto des3_enc_out;
}
enc_len = esp_enc_len(esp_len, iv_len, icv_len);
enc_payload = esp_enc_payload(esp_data, iv_len);
iv = esp_enc_iv(esp_data);
ret = wc_Des3Init(&des3_enc, NULL, INVALID_DEVID);
if (ret != 0) {
ESP_LOG("error: wc_Des3Init: %d\n", ret);
goto des3_enc_out;
}
inited = 1;
ret = wc_Des3_SetKey(&des3_enc, esp_sa->enc_key, iv, DES_ENCRYPTION);
if (ret != 0) {
ESP_LOG("error: wc_Des3_SetKey: %d\n", ret);
goto des3_enc_out;
}
/* encrypt in place. */
ret = wc_Des3_CbcEncrypt(&des3_enc, enc_payload, enc_payload, enc_len);
if (ret != 0) {
ESP_LOG("error: wc_Des3_CbcEncrypt: %d\n", ret);
goto des3_enc_out;
}
des3_enc_out:
if (inited) {
wc_Des3Free(&des3_enc);
inited = 0;
}
return ret;
}
#endif /* !NO_DES3 */
/**
* AES-GCM-ESP
* The KEYMAT requested for each AES-GCM key is N + 4 octets. The first
* N octets are the AES key, and the remaining four octets are used as the
* salt value in the nonce.
* */
#define esp_rfc4106_salt(esp_sa) ((esp_sa)->enc_key \
+ (esp_sa)->enc_key_len \
- ESP_GCM_RFC4106_SALT_LEN)
/* Deterministic iv construction using pre-iv salt and sequence number.
* NIST SP 800-38D, section 8.2.1 Deterministic Construction, using
* an integer counter. The sequence number is used as a counter, and
* xor'ed with pre-iv salt. Based on linux kernel crypto/seqiv.c.
* */
static inline void
esp_rfc4106_gen_iv(uint8_t * iv, const wolfIP_esp_sa * esp_sa)
{
uint32_t seq_num = 0;
uint8_t * seq_num_u8 = (uint8_t *) &seq_num;
seq_num = ee32(esp_sa->replay.oseq);
/* copy in the pre_iv. */
memcpy(iv, esp_sa->pre_iv, sizeof(esp_sa->pre_iv));
/* xor pre-iv salt with current sequence number. */
for (size_t i = 0; i < sizeof(uint32_t); ++i) {
iv[i + sizeof(uint32_t)] ^= seq_num_u8[i];
}
return;
}
#if defined(WOLFSSL_AESGCM_STREAM)
static int
esp_aes_rfc4106_dec(const wolfIP_esp_sa * esp_sa, uint8_t * esp_data,
uint32_t esp_len)
{
Aes gcm_dec;
int err = -1;
uint8_t * icv = NULL;
uint8_t icv_len = esp_sa->icv_len;
uint8_t iv_len = ESP_GCM_RFC4106_IV_LEN;
uint8_t * enc_payload = NULL;
uint8_t * iv = NULL;
uint32_t enc_len = 0;
uint8_t inited = 0;
uint8_t * aad = NULL;
uint16_t aad_len = ESP_SPI_LEN + ESP_SEQ_LEN;
const uint8_t * salt = NULL;
uint8_t salt_len = ESP_GCM_RFC4106_SALT_LEN;
uint8_t nonce[ESP_GCM_RFC4106_NONCE_LEN]; /* 4 salt + 8 iv */
ESP_DEBUG("info: aes gcm dec: %d\n", esp_len);
/* get enc payload, iv, and icv pointers. */
enc_len = esp_enc_len(esp_len, iv_len, icv_len);
enc_payload = esp_enc_payload(esp_data, iv_len);
aad = esp_data;
iv = esp_enc_iv(esp_data);
icv = esp_enc_icv(esp_data, esp_len, esp_sa->icv_len);
/* Get the salt, and construct nonce. */
salt = esp_rfc4106_salt(esp_sa);
memcpy(nonce, salt, salt_len);
memcpy(nonce + salt_len, iv, iv_len);
err = wc_AesInit(&gcm_dec, NULL, INVALID_DEVID);
if (err != 0) {
ESP_LOG("error: wc_AesInit: %d\n", err);
goto rfc4106_dec_out;
}
inited = 1;
/* subtract 4 byte salt from enc_key_len */
err = wc_AesGcmInit(&gcm_dec, esp_sa->enc_key, esp_sa->enc_key_len - 4,
nonce, sizeof(nonce));
if (err != 0) {
ESP_LOG("error: wc_AesGcmInit: %d\n", err);
goto rfc4106_dec_out;
}
err = wc_AesGcmSetKey(&gcm_dec, esp_sa->enc_key, esp_sa->enc_key_len - 4);
if (err != 0) {
ESP_LOG("error: wc_AesGcmSetKey: %d\n", err);
goto rfc4106_dec_out;
}
err = wc_AesGcmDecrypt(&gcm_dec, enc_payload, enc_payload, enc_len,
nonce, sizeof(nonce), icv, icv_len, aad, aad_len);
if (err != 0) {
ESP_LOG("error: wc_AesGcmDecrypt: %d\n", err);
goto rfc4106_dec_out;
}
rfc4106_dec_out:
if (inited) {
wc_AesFree(&gcm_dec);
inited = 0;
}
return err;
}
static int
esp_aes_rfc4106_enc(const wolfIP_esp_sa * esp_sa, uint8_t * esp_data,
uint32_t esp_len)
{
Aes gcm_enc;
int err = -1;
uint8_t * icv = NULL;
uint8_t icv_len = esp_sa->icv_len;
uint8_t iv_len = ESP_GCM_RFC4106_IV_LEN;
uint8_t * enc_payload = NULL;
uint8_t * iv = NULL;
uint32_t enc_len = 0;
uint8_t inited = 0;
uint8_t * aad = NULL;
uint16_t aad_len = ESP_SPI_LEN + ESP_SEQ_LEN;
const uint8_t * salt = NULL;
uint8_t salt_len = ESP_GCM_RFC4106_SALT_LEN;
uint8_t nonce[ESP_GCM_RFC4106_NONCE_LEN]; /* 4 salt + 8 iv */
ESP_DEBUG("info: aes gcm enc: %d\n", esp_len);
/* get enc payload, iv, and icv pointers. */
enc_len = esp_enc_len(esp_len, iv_len, icv_len);
enc_payload = esp_enc_payload(esp_data, iv_len);
aad = esp_data;
iv = esp_enc_iv(esp_data);
icv = esp_enc_icv(esp_data, esp_len, esp_sa->icv_len);
/* Get the salt, and construct nonce. */
salt = esp_rfc4106_salt(esp_sa);
esp_rfc4106_gen_iv(iv, esp_sa);
memcpy(nonce, salt, salt_len);
memcpy(nonce + salt_len, iv, iv_len);
err = wc_AesInit(&gcm_enc, NULL, INVALID_DEVID);
if (err != 0) {
ESP_LOG("error: wc_AesInit: %d\n", err);
goto rfc4106_enc_out;
}
inited = 1;
/* subtract 4 byte salt from enc_key_len */
err = wc_AesGcmInit(&gcm_enc, esp_sa->enc_key, esp_sa->enc_key_len - 4,
nonce, sizeof(nonce));
if (err != 0) {
ESP_LOG("error: wc_AesGcmInit: %d\n", err);
goto rfc4106_enc_out;
}