Skip to content

Commit 1216dcd

Browse files
committed
inbound aes cbc decryption working.
1 parent 3a2c519 commit 1216dcd

1 file changed

Lines changed: 98 additions & 0 deletions

File tree

src/wolfesp.c

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,79 @@ esp_const_memcmp(const uint8_t * vec_a, const uint8_t * vec_b, uint32_t len)
342342
return sum;
343343
}
344344

345+
/**
346+
* Get the encryption length for an ESP payload.
347+
* */
348+
#define esp_enc_len(esp_len, iv_len, icv_len) \
349+
(esp_len) - ESP_SPI_LEN - ESP_SEQ_LEN \
350+
- (iv_len) - (icv_len)
351+
352+
/**
353+
* Get pointer to raw encryption ESP IV, skipping ESP header.
354+
* */
355+
#define esp_enc_iv(data, iv_len) \
356+
(data) + ESP_SPI_LEN + ESP_SEQ_LEN
357+
358+
/**
359+
* Get pointer to raw encryption ESP payload, skipping ESP header and IV.
360+
* */
361+
#define esp_enc_payload(data, iv_len) \
362+
(data) + ESP_SPI_LEN + ESP_SEQ_LEN + (iv_len)
363+
364+
static int
365+
esp_aes_rfc3602_dec(const struct wolfIP_esp_sa * esp_sa, uint8_t * esp_data,
366+
uint32_t esp_len)
367+
{
368+
Aes cbc_dec;
369+
int ret = -1;
370+
uint8_t icv_len = esp_sa->icv_len;
371+
uint8_t iv_len = esp_sa->iv_len;
372+
uint8_t * enc_payload = NULL;
373+
uint8_t * iv = NULL;
374+
uint16_t enc_len = 0;
375+
uint8_t inited = 0;
376+
377+
#ifdef WOLFIP_DEBUG_ESP
378+
printf("info: aes cbc dec\n");
379+
#endif /* WOLFIP_DEBUG_ESP */
380+
381+
enc_len = esp_enc_len(esp_len, iv_len, icv_len);
382+
enc_payload = esp_enc_payload(esp_data, iv_len);
383+
iv = esp_enc_iv(esp_data, iv_len);
384+
385+
ret = wc_AesInit(&cbc_dec, NULL, INVALID_DEVID);
386+
387+
if (ret != 0) {
388+
printf("error: wc_AesInit returned: %d\n", ret);
389+
goto aes_dec_out;
390+
}
391+
392+
inited = 1;
393+
ret = wc_AesSetKey(&cbc_dec, esp_sa->enc_key, esp_sa->enc_key_len,
394+
iv, AES_DECRYPTION);
395+
396+
if (ret != 0) {
397+
printf("error: wc_AesSetKey returned: %d\n", ret);
398+
goto aes_dec_out;
399+
}
400+
401+
/* decrypt in place. */
402+
ret = wc_AesCbcDecrypt(&cbc_dec, enc_payload, enc_payload, enc_len);
403+
404+
if (ret != 0) {
405+
printf("error: wc_AesCbcDecrypt returned: %d\n", ret);
406+
goto aes_dec_out;
407+
}
408+
409+
aes_dec_out:
410+
if (inited) {
411+
wc_AesFree(&cbc_dec);
412+
inited = 0;
413+
}
414+
415+
return ret;
416+
}
417+
345418
/**
346419
* esp_data covers from start of ESP header to end of ESP trailer, but does not
347420
* include the ESP ICV after trailer.
@@ -482,6 +555,31 @@ static int esp_unwrap(struct wolfIP *s, struct wolfIP_ip_packet *ip,
482555
}
483556
}
484557

558+
if (esp_sa->iv_len != 0) {
559+
/* Decrypt the payload in place. */
560+
int err = -1;
561+
562+
switch(esp_sa->enc) {
563+
case ESP_ENC_CBC_AES:
564+
err = esp_aes_rfc3602_dec(esp_sa, ip->data, esp_len);
565+
break;
566+
567+
case ESP_ENC_NONE:
568+
default:
569+
printf("error: decrypt: invalid enc: %d\n", esp_sa->enc);
570+
err = -1;
571+
break;
572+
}
573+
574+
if (err) {
575+
printf("error: esp_decrypt(%02x) returned: %d\n", esp_sa->enc, err);
576+
return -1;
577+
}
578+
579+
/* Payload is now decrypted. We can now parse
580+
* the ESP trailer for next header and padding. */
581+
}
582+
485583
/* icv check good, now finish unwrapping esp packet. */
486584
pad_len = *(ip->data + esp_len - esp_sa->icv_len - ESP_NEXT_HEADER_LEN
487585
- ESP_PADDING_LEN);

0 commit comments

Comments
 (0)