-#define DBG_LAYER_BEG NETDBG_CODE(DBG_NETIPSEC, 1)
-#define DBG_LAYER_END NETDBG_CODE(DBG_NETIPSEC, 3)
-#define DBG_FNC_ESPAUTH NETDBG_CODE(DBG_NETIPSEC, (8 << 8))
-
-static int esp_null_mature __P((struct secasvar *));
-static int esp_null_decrypt __P((struct mbuf *, size_t,
- struct secasvar *, const struct esp_algorithm *, int));
-static int esp_null_encrypt __P((struct mbuf *, size_t, size_t,
- struct secasvar *, const struct esp_algorithm *, int));
-static int esp_descbc_mature __P((struct secasvar *));
-static int esp_descbc_ivlen __P((const struct esp_algorithm *,
- struct secasvar *));
-static int esp_des_schedule __P((const struct esp_algorithm *,
- struct secasvar *));
-static int esp_des_schedlen __P((const struct esp_algorithm *));
-static int esp_des_blockdecrypt __P((const struct esp_algorithm *,
- struct secasvar *, u_int8_t *, u_int8_t *));
-static int esp_des_blockencrypt __P((const struct esp_algorithm *,
- struct secasvar *, u_int8_t *, u_int8_t *));
-static int esp_cbc_mature __P((struct secasvar *));
-static int esp_blowfish_schedule __P((const struct esp_algorithm *,
- struct secasvar *));
-static int esp_blowfish_schedlen __P((const struct esp_algorithm *));
-static int esp_blowfish_blockdecrypt __P((const struct esp_algorithm *,
- struct secasvar *, u_int8_t *, u_int8_t *));
-static int esp_blowfish_blockencrypt __P((const struct esp_algorithm *,
- struct secasvar *, u_int8_t *, u_int8_t *));
-static int esp_cast128_schedule __P((const struct esp_algorithm *,
- struct secasvar *));
-static int esp_cast128_schedlen __P((const struct esp_algorithm *));
-static int esp_cast128_blockdecrypt __P((const struct esp_algorithm *,
- struct secasvar *, u_int8_t *, u_int8_t *));
-static int esp_cast128_blockencrypt __P((const struct esp_algorithm *,
- struct secasvar *, u_int8_t *, u_int8_t *));
-static int esp_3des_schedule __P((const struct esp_algorithm *,
- struct secasvar *));
-static int esp_3des_schedlen __P((const struct esp_algorithm *));
-static int esp_3des_blockdecrypt __P((const struct esp_algorithm *,
- struct secasvar *, u_int8_t *, u_int8_t *));
-static int esp_3des_blockencrypt __P((const struct esp_algorithm *,
- struct secasvar *, u_int8_t *, u_int8_t *));
-static int esp_common_ivlen __P((const struct esp_algorithm *,
- struct secasvar *));
-static int esp_cbc_decrypt __P((struct mbuf *, size_t,
- struct secasvar *, const struct esp_algorithm *, int));
-static int esp_cbc_encrypt __P((struct mbuf *, size_t, size_t,
- struct secasvar *, const struct esp_algorithm *, int));
-
-#define MAXIVLEN 16
-
-static const struct esp_algorithm esp_algorithms[] = {
- { 8, -1, esp_descbc_mature, 64, 64, esp_des_schedlen,
- "des-cbc",
- esp_descbc_ivlen, esp_cbc_decrypt,
- esp_cbc_encrypt, esp_des_schedule,
- esp_des_blockdecrypt, esp_des_blockencrypt, },
- { 8, 8, esp_cbc_mature, 192, 192, esp_3des_schedlen,
- "3des-cbc",
- esp_common_ivlen, esp_cbc_decrypt,
- esp_cbc_encrypt, esp_3des_schedule,
- esp_3des_blockdecrypt, esp_3des_blockencrypt, },
- { 1, 0, esp_null_mature, 0, 2048, 0, "null",
- esp_common_ivlen, esp_null_decrypt,
- esp_null_encrypt, NULL, },
- { 8, 8, esp_cbc_mature, 40, 448, esp_blowfish_schedlen, "blowfish-cbc",
- esp_common_ivlen, esp_cbc_decrypt,
- esp_cbc_encrypt, esp_blowfish_schedule,
- esp_blowfish_blockdecrypt, esp_blowfish_blockencrypt, },
- { 8, 8, esp_cbc_mature, 40, 128, esp_cast128_schedlen,
- "cast128-cbc",
- esp_common_ivlen, esp_cbc_decrypt,
- esp_cbc_encrypt, esp_cast128_schedule,
- esp_cast128_blockdecrypt, esp_cast128_blockencrypt, },
- { 16, 16, esp_cbc_mature, 128, 256, esp_rijndael_schedlen,
- "rijndael-cbc",
- esp_common_ivlen, esp_cbc_decrypt,
- esp_cbc_encrypt, esp_rijndael_schedule,
- esp_rijndael_blockdecrypt, esp_rijndael_blockencrypt },
+#define DBG_LAYER_BEG NETDBG_CODE(DBG_NETIPSEC, 1)
+#define DBG_LAYER_END NETDBG_CODE(DBG_NETIPSEC, 3)
+#define DBG_FNC_ESPAUTH NETDBG_CODE(DBG_NETIPSEC, (8 << 8))
+#define MAX_SBUF_LEN 2000
+
+extern lck_mtx_t *sadb_mutex;
+os_log_t esp_mpkl_log_object = NULL;
+
+static int esp_null_mature(struct secasvar *);
+static int esp_null_decrypt(struct mbuf *, size_t,
+ struct secasvar *, const struct esp_algorithm *, int);
+static int esp_null_encrypt(struct mbuf *, size_t, size_t,
+ struct secasvar *, const struct esp_algorithm *, int);
+static int esp_descbc_mature(struct secasvar *);
+static int esp_descbc_ivlen(const struct esp_algorithm *,
+ struct secasvar *);
+static int esp_des_schedule(const struct esp_algorithm *,
+ struct secasvar *);
+static int esp_des_schedlen(const struct esp_algorithm *);
+static int esp_des_blockdecrypt(const struct esp_algorithm *,
+ struct secasvar *, u_int8_t *, u_int8_t *);
+static int esp_des_blockencrypt(const struct esp_algorithm *,
+ struct secasvar *, u_int8_t *, u_int8_t *);
+static int esp_cbc_mature(struct secasvar *);
+static int esp_3des_schedule(const struct esp_algorithm *,
+ struct secasvar *);
+static int esp_3des_schedlen(const struct esp_algorithm *);
+static int esp_3des_blockdecrypt(const struct esp_algorithm *,
+ struct secasvar *, u_int8_t *, u_int8_t *);
+static int esp_3des_blockencrypt(const struct esp_algorithm *,
+ struct secasvar *, u_int8_t *, u_int8_t *);
+static int esp_common_ivlen(const struct esp_algorithm *,
+ struct secasvar *);
+static int esp_cbc_decrypt(struct mbuf *, size_t,
+ struct secasvar *, const struct esp_algorithm *, int);
+static int esp_cbc_encrypt(struct mbuf *, size_t, size_t,
+ struct secasvar *, const struct esp_algorithm *, int);
+static int esp_gcm_mature(struct secasvar *);
+
+#define MAXIVLEN 16
+
+#define ESP_AESGCM_KEYLEN128 160 // 16-bytes key + 4 bytes salt
+#define ESP_AESGCM_KEYLEN192 224 // 24-bytes key + 4 bytes salt
+#define ESP_AESGCM_KEYLEN256 288 // 32-bytes key + 4 bytes salt
+
+static const struct esp_algorithm des_cbc = {
+ .padbound = 8,
+ .ivlenval = -1,
+ .mature = esp_descbc_mature,
+ .keymin = 64,
+ .keymax = 64,
+ .schedlen = esp_des_schedlen,
+ .name = "des-cbc",
+ .ivlen = esp_descbc_ivlen,
+ .decrypt = esp_cbc_decrypt,
+ .encrypt = esp_cbc_encrypt,
+ .schedule = esp_des_schedule,
+ .blockdecrypt = esp_des_blockdecrypt,
+ .blockencrypt = esp_des_blockencrypt,
+ .icvlen = 0,
+ .finalizedecrypt = NULL,
+ .finalizeencrypt = NULL
+};
+
+static const struct esp_algorithm des3_cbc = {
+ .padbound = 8,
+ .ivlenval = 8,
+ .mature = esp_cbc_mature,
+ .keymin = 192,
+ .keymax = 192,
+ .schedlen = esp_3des_schedlen,
+ .name = "3des-cbc",
+ .ivlen = esp_common_ivlen,
+ .decrypt = esp_cbc_decrypt,
+ .encrypt = esp_cbc_encrypt,
+ .schedule = esp_3des_schedule,
+ .blockdecrypt = esp_3des_blockdecrypt,
+ .blockencrypt = esp_3des_blockencrypt,
+ .icvlen = 0,
+ .finalizedecrypt = NULL,
+ .finalizeencrypt = NULL
+};
+
+static const struct esp_algorithm null_esp = {
+ .padbound = 1,
+ .ivlenval = 0,
+ .mature = esp_null_mature,
+ .keymin = 0,
+ .keymax = 2048,
+ .schedlen = NULL,
+ .name = "null",
+ .ivlen = esp_common_ivlen,
+ .decrypt = esp_null_decrypt,
+ .encrypt = esp_null_encrypt,
+ .schedule = NULL,
+ .blockdecrypt = NULL,
+ .blockencrypt = NULL,
+ .icvlen = 0,
+ .finalizedecrypt = NULL,
+ .finalizeencrypt = NULL
+};
+
+static const struct esp_algorithm aes_cbc = {
+ .padbound = 16,
+ .ivlenval = 16,
+ .mature = esp_cbc_mature,
+ .keymin = 128,
+ .keymax = 256,
+ .schedlen = esp_aes_schedlen,
+ .name = "aes-cbc",
+ .ivlen = esp_common_ivlen,
+ .decrypt = esp_cbc_decrypt_aes,
+ .encrypt = esp_cbc_encrypt_aes,
+ .schedule = esp_aes_schedule,
+ .blockdecrypt = NULL,
+ .blockencrypt = NULL,
+ .icvlen = 0,
+ .finalizedecrypt = NULL,
+ .finalizeencrypt = NULL
+};
+
+static const struct esp_algorithm aes_gcm = {
+ .padbound = 4,
+ .ivlenval = 8,
+ .mature = esp_gcm_mature,
+ .keymin = ESP_AESGCM_KEYLEN128,
+ .keymax = ESP_AESGCM_KEYLEN256,
+ .schedlen = esp_gcm_schedlen,
+ .name = "aes-gcm",
+ .ivlen = esp_common_ivlen,
+ .decrypt = esp_gcm_decrypt_aes,
+ .encrypt = esp_gcm_encrypt_aes,
+ .schedule = esp_gcm_schedule,
+ .blockdecrypt = NULL,
+ .blockencrypt = NULL,
+ .icvlen = 16,
+ .finalizedecrypt = esp_gcm_decrypt_finalize,
+ .finalizeencrypt = esp_gcm_encrypt_finalize
+};
+
+static const struct esp_algorithm chacha_poly = {
+ .padbound = ESP_CHACHAPOLY_PAD_BOUND,
+ .ivlenval = ESP_CHACHAPOLY_IV_LEN,
+ .mature = esp_chachapoly_mature,
+ .keymin = ESP_CHACHAPOLY_KEYBITS_WITH_SALT,
+ .keymax = ESP_CHACHAPOLY_KEYBITS_WITH_SALT,
+ .schedlen = esp_chachapoly_schedlen,
+ .name = "chacha-poly",
+ .ivlen = esp_chachapoly_ivlen,
+ .decrypt = esp_chachapoly_decrypt,
+ .encrypt = esp_chachapoly_encrypt,
+ .schedule = esp_chachapoly_schedule,
+ .blockdecrypt = NULL,
+ .blockencrypt = NULL,
+ .icvlen = ESP_CHACHAPOLY_ICV_LEN,
+ .finalizedecrypt = esp_chachapoly_decrypt_finalize,
+ .finalizeencrypt = esp_chachapoly_encrypt_finalize
+};
+
+static const struct esp_algorithm *esp_algorithms[] = {
+ &des_cbc,
+ &des3_cbc,
+ &null_esp,
+ &aes_cbc,
+ &aes_gcm,
+ &chacha_poly,