|
1 | 1 | #if defined(WOLFIP_ESP) && !defined(WOLFESP_SRC) |
2 | 2 | #define WOLFESP_SRC |
3 | 3 |
|
4 | | -#define ESP_SPI_LEN 4 |
5 | | -#define ESP_SEQ_LEN 4 |
6 | | -#define ESP_PADDING_LEN 1 |
7 | | -#define ESP_NEXT_HEADER_LEN 1 |
8 | | -#define ESP_ICV_ALIGNMENT 4 |
9 | | -/* hmac-[sha256, sha1, md5]-96*/ |
10 | | -#define ESP_ICVLEN_HMAC_96 12 |
11 | | -#define ESP_ICVLEN_HMAC_128 16 |
12 | | -#define WOLFIP_ESP_NUM_SA 2 |
13 | | - |
14 | | -/* aes-128 */ |
15 | | -#define ESP_128_KEY_LEN 16 |
16 | | -#define ESP_128_IV_LEN 16 |
17 | | - |
18 | | -typedef enum { |
19 | | - ESP_ENC_NONE = 0, |
20 | | - ESP_ENC_CBC_AES, |
21 | | - ESP_ENC_CBC_DES3, |
22 | | - ESP_ENC_GCM_RFC4106, |
23 | | - ESP_ENC_GCM_RFC4543, /* placeholder to indicate gmac auth. */ |
24 | | -} esp_enc_t; |
25 | | - |
26 | | -typedef enum { |
27 | | - ESP_AUTH_NONE = 0, |
28 | | - ESP_AUTH_MD5_RFC2403, /* hmac(md5)-96 */ |
29 | | - ESP_AUTH_SHA1_RFC2404, /* hmac(sha1)-96 */ |
30 | | - ESP_AUTH_SHA256_RFC4868, /* hmac(sha256)-N, N=96,128 */ |
31 | | - ESP_AUTH_GCM_RFC4106, /* placeholder to indicate gcm auth. */ |
32 | | - ESP_AUTH_GCM_RFC4543 /* rfc4543 gmac */ |
33 | | -} esp_auth_t; |
34 | | - |
35 | | -/* Minimal ESP Security Association structure. |
36 | | - * Supports only transport mode. |
37 | | - * */ |
38 | | -struct wolfIP_esp_sa { |
39 | | - uint8_t spi[ESP_SPI_LEN]; /* security parameter index */ |
40 | | - ip4 src; /* ip src and dst in network byte order */ |
41 | | - ip4 dst; |
42 | | - uint32_t oseq; /* outbound sequence number */ |
43 | | - uint32_t seq; /* inbound sequence number */ |
44 | | - uint8_t iv_len; |
45 | | - esp_enc_t enc; |
46 | | - uint8_t enc_key[32]; |
47 | | - uint8_t enc_key_len; |
48 | | - esp_auth_t auth; |
49 | | - uint8_t auth_key[32]; |
50 | | - uint8_t auth_key_len; |
51 | | - uint8_t icv_len; |
52 | | -}; |
| 4 | +#include "wolfesp.h" |
53 | 5 |
|
54 | 6 | static WC_RNG wc_rng; |
55 | 7 | static uint8_t rng_inited = 0; |
56 | 8 |
|
57 | | -static void |
| 9 | +int |
58 | 10 | esp_init(void) |
59 | 11 | { |
| 12 | + int ret = 0; |
| 13 | + |
60 | 14 | if (rng_inited == 0) { |
61 | | - int ret = wc_InitRng_ex(&wc_rng, NULL, INVALID_DEVID); |
| 15 | + ret = wc_InitRng_ex(&wc_rng, NULL, INVALID_DEVID); |
62 | 16 |
|
63 | 17 | if (ret) { |
64 | 18 | printf("error: wc_InitRng_ex returned: %d\n", ret); |
65 | 19 | } |
66 | | - |
67 | | - rng_inited = 1; |
| 20 | + else { |
| 21 | + rng_inited = 1; |
| 22 | + } |
68 | 23 | } |
69 | 24 |
|
70 | | - return; |
| 25 | + return ret; |
71 | 26 | } |
72 | 27 |
|
73 | | -/* Static pre-defined SA lists.*/ |
74 | 28 |
|
75 | | -struct wolfIP_esp_sa in_sa_list[WOLFIP_ESP_NUM_SA] = |
76 | | -{ |
77 | | - { |
78 | | - {0x2f, 0xa9, 0xd8, 0xc8}, /* spi */ |
79 | | - 0x010A0A0A, /* src */ |
80 | | - 0x020A0A0A, /* dst */ |
81 | | - 0,0, /* oseq, seq */ |
82 | | - 0, /* iv len */ |
83 | | - 0, {0}, 0, /* null enc */ |
84 | | - ESP_AUTH_SHA256_RFC4868, |
85 | | - {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, |
86 | | - 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, |
87 | | - 16, |
88 | | - ESP_ICVLEN_HMAC_128 |
89 | | - }, |
90 | | - { |
91 | | - {0x76, 0x4f, 0x47, 0xc9}, /* spi */ |
92 | | - 0x010A0A0A, /* src */ |
93 | | - 0x020A0A0A, /* dst */ |
94 | | - 0,0, /* oseq, seq */ |
95 | | - ESP_128_IV_LEN, /* iv len */ |
96 | | - ESP_ENC_CBC_AES, |
97 | | - {0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, |
98 | | - 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03}, |
99 | | - 16, |
100 | | - ESP_AUTH_SHA256_RFC4868, |
101 | | - {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, |
102 | | - 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}, |
103 | | - 16, |
104 | | - ESP_ICVLEN_HMAC_128 |
105 | | - }, |
106 | | -}; |
107 | | - |
108 | | -struct wolfIP_esp_sa out_sa_list[WOLFIP_ESP_NUM_SA] = |
| 29 | +static struct wolfIP_esp_sa * in_sa_list = NULL; |
| 30 | +static struct wolfIP_esp_sa * out_sa_list = NULL; |
| 31 | +static uint16_t in_sa_num = 0; |
| 32 | +static uint16_t out_sa_num = 0; |
| 33 | + |
| 34 | +void |
| 35 | +esp_load_sa_list(struct wolfIP_esp_sa * sa_list, uint16_t num, uint16_t in) |
109 | 36 | { |
110 | | - { |
111 | | - {0xf6, 0xe9, 0xb8, 0x0d}, /* spi */ |
112 | | - 0x020A0A0A, /* src */ |
113 | | - 0x030A0A0A, /* dst */ |
114 | | - 0,0, /* oseq, seq */ |
115 | | - 0, /* iv len */ |
116 | | - 0, {0}, 0, /* null enc */ |
117 | | - ESP_AUTH_SHA256_RFC4868, |
118 | | - {0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, |
119 | | - 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02}, |
120 | | - 16, |
121 | | - ESP_ICVLEN_HMAC_128 |
122 | | - }, |
| 37 | + if (in == 1) { |
| 38 | + in_sa_list = sa_list; |
| 39 | + in_sa_num = num; |
| 40 | + } |
| 41 | + else { |
| 42 | + out_sa_list = sa_list; |
| 43 | + out_sa_num = num; |
| 44 | + } |
123 | 45 |
|
124 | | - { |
125 | | - {0x49, 0xeb, 0xfd, 0xd4}, /* spi */ |
126 | | - 0x020A0A0A, /* src */ |
127 | | - 0x010A0A0A, /* dst */ |
128 | | - 0,0, /* oseq, seq */ |
129 | | - ESP_128_IV_LEN, /* iv len */ |
130 | | - ESP_ENC_CBC_AES, |
131 | | - {0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, |
132 | | - 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04}, |
133 | | - 16, |
134 | | - ESP_AUTH_SHA256_RFC4868, |
135 | | - {0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, |
136 | | - 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02}, |
137 | | - 16, |
138 | | - ESP_ICVLEN_HMAC_128 |
139 | | - }, |
140 | | - |
141 | | -}; |
| 46 | + return; |
| 47 | +} |
142 | 48 |
|
143 | 49 | #ifdef WOLFIP_DEBUG_ESP |
144 | 50 | #ifdef WOLFIP_DEBUG_ESP_VERBOSE |
@@ -617,7 +523,7 @@ static int esp_unwrap(struct wolfIP *s, struct wolfIP_ip_packet *ip, |
617 | 523 | memcpy(&seq, ip->data + ESP_SPI_LEN, sizeof(seq)); |
618 | 524 | seq = ee32(seq); |
619 | 525 |
|
620 | | - for (size_t i = 0; i < WOLFIP_ESP_NUM_SA; ++i) { |
| 526 | + for (size_t i = 0; i < in_sa_num; ++i) { |
621 | 527 | if (memcmp(spi, in_sa_list[i].spi, sizeof(spi)) == 0) { |
622 | 528 | #ifdef WOLFIP_DEBUG_ESP |
623 | 529 | printf("info: found sa: 0x%02x%02x%02x%02x\n", |
@@ -762,7 +668,7 @@ static int esp_wrap(struct wolfIP_ip_packet *ip, uint16_t * ip_len) |
762 | 668 | struct wolfIP_esp_sa * esp_sa = NULL; |
763 | 669 |
|
764 | 670 | /* TODO: priority, tcp/udp port-filtering? */ |
765 | | - for (size_t i = 0; i < WOLFIP_ESP_NUM_SA; ++i) { |
| 671 | + for (size_t i = 0; i < out_sa_num; ++i) { |
766 | 672 | if (ip->dst == out_sa_list[i].dst) { |
767 | 673 | esp_sa = &out_sa_list[i]; |
768 | 674 | #ifdef WOLFIP_DEBUG_ESP |
|
0 commit comments