diff options
author | Damjan Marion <damarion@cisco.com> | 2019-04-24 15:20:35 +0200 |
---|---|---|
committer | Neale Ranns <nranns@cisco.com> | 2019-04-25 01:36:12 +0000 |
commit | d1bed687231bb64cf7761da37431ba61bc32b6d8 (patch) | |
tree | 891af80a873db9dda53c18e95f5eeb9366a1cb07 /src/vnet/ipsec | |
parent | 20bc56ab58189ad9fa24feaaca3e76ea8e636140 (diff) |
crypto: improve key handling
Change-Id: If96f661d507305da4b96cac7b1a8f14ba90676ad
Signed-off-by: Damjan Marion <damarion@cisco.com>
Diffstat (limited to 'src/vnet/ipsec')
-rw-r--r-- | src/vnet/ipsec/esp.h | 3 | ||||
-rw-r--r-- | src/vnet/ipsec/esp_decrypt.c | 5 | ||||
-rw-r--r-- | src/vnet/ipsec/esp_encrypt.c | 5 | ||||
-rw-r--r-- | src/vnet/ipsec/ipsec.c | 13 | ||||
-rw-r--r-- | src/vnet/ipsec/ipsec.h | 2 | ||||
-rw-r--r-- | src/vnet/ipsec/ipsec_if.c | 13 | ||||
-rw-r--r-- | src/vnet/ipsec/ipsec_sa.c | 18 | ||||
-rw-r--r-- | src/vnet/ipsec/ipsec_sa.h | 10 |
8 files changed, 58 insertions, 11 deletions
diff --git a/src/vnet/ipsec/esp.h b/src/vnet/ipsec/esp.h index 052bbfc1d88..070726faa34 100644 --- a/src/vnet/ipsec/esp.h +++ b/src/vnet/ipsec/esp.h @@ -112,8 +112,7 @@ hmac_calc (vlib_main_t * vm, ipsec_sa_t * sa, u8 * data, int data_len, return 0; vnet_crypto_op_init (op, sa->integ_op_id); - op->key = sa->integ_key.data; - op->key_len = sa->integ_key.len; + op->key_index = sa->integ_key_index; op->src = data; op->len = data_len; op->digest = signature; diff --git a/src/vnet/ipsec/esp_decrypt.c b/src/vnet/ipsec/esp_decrypt.c index e74c1bb908a..5d1f20604d5 100644 --- a/src/vnet/ipsec/esp_decrypt.c +++ b/src/vnet/ipsec/esp_decrypt.c @@ -203,8 +203,7 @@ esp_decrypt_inline (vlib_main_t * vm, vec_add2_aligned (ptd->integ_ops, op, 1, CLIB_CACHE_LINE_BYTES); vnet_crypto_op_init (op, sa0->integ_op_id); - op->key = sa0->integ_key.data; - op->key_len = sa0->integ_key.len; + op->key_index = sa0->integ_key_index; op->src = payload; op->flags = VNET_CRYPTO_OP_FLAG_HMAC_CHECK; op->user_data = b - bufs; @@ -231,7 +230,7 @@ esp_decrypt_inline (vlib_main_t * vm, vnet_crypto_op_t *op; vec_add2_aligned (ptd->crypto_ops, op, 1, CLIB_CACHE_LINE_BYTES); vnet_crypto_op_init (op, sa0->crypto_dec_op_id); - op->key = sa0->crypto_key.data; + op->key_index = sa0->crypto_key_index; op->iv = payload; if (ipsec_sa_is_set_IS_AEAD (sa0)) diff --git a/src/vnet/ipsec/esp_encrypt.c b/src/vnet/ipsec/esp_encrypt.c index 5db10b520e3..af56302bafb 100644 --- a/src/vnet/ipsec/esp_encrypt.c +++ b/src/vnet/ipsec/esp_encrypt.c @@ -441,7 +441,7 @@ esp_encrypt_inline (vlib_main_t * vm, vlib_node_runtime_t * node, vnet_crypto_op_init (op, sa0->crypto_enc_op_id); op->iv = payload - iv_sz; op->src = op->dst = payload; - op->key = sa0->crypto_key.data; + op->key_index = sa0->crypto_key_index; op->len = payload_len - icv_sz; op->flags = VNET_CRYPTO_OP_FLAG_INIT_IV; op->user_data = b - bufs; @@ -469,8 +469,7 @@ esp_encrypt_inline (vlib_main_t * vm, vlib_node_runtime_t * node, vnet_crypto_op_init (op, sa0->integ_op_id); op->src = payload - iv_sz - sizeof (esp_header_t); op->digest = payload + payload_len - icv_sz; - op->key = sa0->integ_key.data; - op->key_len = sa0->integ_key.len; + op->key_index = sa0->integ_key_index; op->digest_len = icv_sz; op->len = payload_len - icv_sz + iv_sz + sizeof (esp_header_t); op->user_data = b - bufs; diff --git a/src/vnet/ipsec/ipsec.c b/src/vnet/ipsec/ipsec.c index 73c5cf4d7ab..c530a44e870 100644 --- a/src/vnet/ipsec/ipsec.c +++ b/src/vnet/ipsec/ipsec.c @@ -264,43 +264,51 @@ ipsec_init (vlib_main_t * vm) a = im->crypto_algs + IPSEC_CRYPTO_ALG_DES_CBC; a->enc_op_id = VNET_CRYPTO_OP_DES_CBC_ENC; a->dec_op_id = VNET_CRYPTO_OP_DES_CBC_DEC; + a->alg = VNET_CRYPTO_ALG_DES_CBC; a->iv_size = a->block_size = 8; a = im->crypto_algs + IPSEC_CRYPTO_ALG_3DES_CBC; a->enc_op_id = VNET_CRYPTO_OP_3DES_CBC_ENC; a->dec_op_id = VNET_CRYPTO_OP_3DES_CBC_DEC; + a->alg = VNET_CRYPTO_ALG_3DES_CBC; a->iv_size = a->block_size = 8; a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_128; a->enc_op_id = VNET_CRYPTO_OP_AES_128_CBC_ENC; a->dec_op_id = VNET_CRYPTO_OP_AES_128_CBC_DEC; + a->alg = VNET_CRYPTO_ALG_AES_128_CBC; a->iv_size = a->block_size = 16; a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_192; a->enc_op_id = VNET_CRYPTO_OP_AES_192_CBC_ENC; a->dec_op_id = VNET_CRYPTO_OP_AES_192_CBC_DEC; + a->alg = VNET_CRYPTO_ALG_AES_192_CBC; a->iv_size = a->block_size = 16; a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_256; a->enc_op_id = VNET_CRYPTO_OP_AES_256_CBC_ENC; a->dec_op_id = VNET_CRYPTO_OP_AES_256_CBC_DEC; + a->alg = VNET_CRYPTO_ALG_AES_256_CBC; a->iv_size = a->block_size = 16; a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_GCM_128; a->enc_op_id = VNET_CRYPTO_OP_AES_128_GCM_ENC; a->dec_op_id = VNET_CRYPTO_OP_AES_128_GCM_DEC; + a->alg = VNET_CRYPTO_ALG_AES_128_GCM; a->iv_size = a->block_size = 8; a->icv_size = 16; a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_GCM_192; a->enc_op_id = VNET_CRYPTO_OP_AES_192_GCM_ENC; a->dec_op_id = VNET_CRYPTO_OP_AES_192_GCM_DEC; + a->alg = VNET_CRYPTO_ALG_AES_192_GCM; a->iv_size = a->block_size = 8; a->icv_size = 16; a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_GCM_256; a->enc_op_id = VNET_CRYPTO_OP_AES_256_GCM_ENC; a->dec_op_id = VNET_CRYPTO_OP_AES_256_GCM_DEC; + a->alg = VNET_CRYPTO_ALG_AES_256_GCM; a->iv_size = a->block_size = 8; a->icv_size = 16; @@ -309,22 +317,27 @@ ipsec_init (vlib_main_t * vm) i = &im->integ_algs[IPSEC_INTEG_ALG_SHA1_96]; i->op_id = VNET_CRYPTO_OP_SHA1_HMAC; + i->alg = VNET_CRYPTO_ALG_HMAC_SHA1; i->icv_size = 12; i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_256_96]; i->op_id = VNET_CRYPTO_OP_SHA1_HMAC; + i->alg = VNET_CRYPTO_ALG_HMAC_SHA256; i->icv_size = 12; i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_256_128]; i->op_id = VNET_CRYPTO_OP_SHA256_HMAC; + i->alg = VNET_CRYPTO_ALG_HMAC_SHA256; i->icv_size = 16; i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_384_192]; i->op_id = VNET_CRYPTO_OP_SHA384_HMAC; + i->alg = VNET_CRYPTO_ALG_HMAC_SHA512; i->icv_size = 24; i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_512_256]; i->op_id = VNET_CRYPTO_OP_SHA512_HMAC; + i->alg = VNET_CRYPTO_ALG_HMAC_SHA512; i->icv_size = 32; vec_validate_aligned (im->ptd, vlib_num_workers (), CLIB_CACHE_LINE_BYTES); diff --git a/src/vnet/ipsec/ipsec.h b/src/vnet/ipsec/ipsec.h index 110bbcb3e5a..232390c0690 100644 --- a/src/vnet/ipsec/ipsec.h +++ b/src/vnet/ipsec/ipsec.h @@ -68,6 +68,7 @@ typedef struct { vnet_crypto_op_id_t enc_op_id; vnet_crypto_op_id_t dec_op_id; + vnet_crypto_alg_t alg; u8 iv_size; u8 block_size; u8 icv_size; @@ -76,6 +77,7 @@ typedef struct typedef struct { vnet_crypto_op_id_t op_id; + vnet_crypto_alg_t alg; u8 icv_size; } ipsec_main_integ_alg_t; diff --git a/src/vnet/ipsec/ipsec_if.c b/src/vnet/ipsec/ipsec_if.c index 8d0eef21ae0..9f64634c51e 100644 --- a/src/vnet/ipsec/ipsec_if.c +++ b/src/vnet/ipsec/ipsec_if.c @@ -510,6 +510,7 @@ int ipsec_set_interface_key (vnet_main_t * vnm, u32 hw_if_index, ipsec_if_set_key_type_t type, u8 alg, u8 * key) { + vlib_main_t *vm = vlib_get_main (); ipsec_main_t *im = &ipsec_main; vnet_hw_interface_t *hi; ipsec_tunnel_if_t *t; @@ -526,24 +527,36 @@ ipsec_set_interface_key (vnet_main_t * vnm, u32 hw_if_index, sa = pool_elt_at_index (im->sad, t->output_sa_index); ipsec_sa_set_crypto_alg (sa, alg); ipsec_mk_key (&sa->crypto_key, key, vec_len (key)); + sa->crypto_calg = im->crypto_algs[alg].alg; + vnet_crypto_key_modify (vm, sa->crypto_key_index, sa->crypto_calg, + key, vec_len (key)); } else if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG) { sa = pool_elt_at_index (im->sad, t->output_sa_index); ipsec_sa_set_integ_alg (sa, alg); ipsec_mk_key (&sa->integ_key, key, vec_len (key)); + sa->integ_calg = im->integ_algs[alg].alg; + vnet_crypto_key_modify (vm, sa->integ_key_index, sa->integ_calg, + key, vec_len (key)); } else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO) { sa = pool_elt_at_index (im->sad, t->input_sa_index); ipsec_sa_set_crypto_alg (sa, alg); ipsec_mk_key (&sa->crypto_key, key, vec_len (key)); + sa->crypto_calg = im->crypto_algs[alg].alg; + vnet_crypto_key_modify (vm, sa->crypto_key_index, sa->crypto_calg, + key, vec_len (key)); } else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG) { sa = pool_elt_at_index (im->sad, t->input_sa_index); ipsec_sa_set_integ_alg (sa, alg); ipsec_mk_key (&sa->integ_key, key, vec_len (key)); + sa->integ_calg = im->integ_algs[alg].alg; + vnet_crypto_key_modify (vm, sa->integ_key_index, sa->integ_calg, + key, vec_len (key)); } else return VNET_API_ERROR_INVALID_VALUE; diff --git a/src/vnet/ipsec/ipsec_sa.c b/src/vnet/ipsec/ipsec_sa.c index 8f41c95159b..2d2e90a1d27 100644 --- a/src/vnet/ipsec/ipsec_sa.c +++ b/src/vnet/ipsec/ipsec_sa.c @@ -100,6 +100,7 @@ ipsec_sa_set_crypto_alg (ipsec_sa_t * sa, ipsec_crypto_alg_t crypto_alg) sa->crypto_block_size = im->crypto_algs[crypto_alg].block_size; sa->crypto_enc_op_id = im->crypto_algs[crypto_alg].enc_op_id; sa->crypto_dec_op_id = im->crypto_algs[crypto_alg].dec_op_id; + sa->crypto_calg = im->crypto_algs[crypto_alg].alg; ASSERT (sa->crypto_iv_size <= ESP_MAX_IV_SIZE); ASSERT (sa->crypto_block_size <= ESP_MAX_BLOCK_SIZE); if (IPSEC_CRYPTO_ALG_IS_GCM (crypto_alg)) @@ -116,6 +117,7 @@ ipsec_sa_set_integ_alg (ipsec_sa_t * sa, ipsec_integ_alg_t integ_alg) sa->integ_alg = integ_alg; sa->integ_icv_size = im->integ_algs[integ_alg].icv_size; sa->integ_op_id = im->integ_algs[integ_alg].op_id; + sa->integ_calg = im->integ_algs[integ_alg].alg; ASSERT (sa->integ_icv_size <= ESP_MAX_ICV_SIZE); } @@ -133,6 +135,7 @@ ipsec_sa_add (u32 id, const ip46_address_t * tun_src, const ip46_address_t * tun_dst, u32 * sa_out_index) { + vlib_main_t *vm = vlib_get_main (); ipsec_main_t *im = &ipsec_main; clib_error_t *err; ipsec_sa_t *sa; @@ -164,6 +167,13 @@ ipsec_sa_add (u32 id, ip46_address_copy (&sa->tunnel_src_addr, tun_src); ip46_address_copy (&sa->tunnel_dst_addr, tun_dst); + sa->crypto_key_index = vnet_crypto_key_add (vm, + im->crypto_algs[crypto_alg].alg, + (u8 *) ck->data, ck->len); + sa->integ_key_index = vnet_crypto_key_add (vm, + im->integ_algs[integ_alg].alg, + (u8 *) ik->data, ik->len); + err = ipsec_check_support_cb (im, sa); if (err) { @@ -253,6 +263,7 @@ ipsec_sa_add (u32 id, u32 ipsec_sa_del (u32 id) { + vlib_main_t *vm = vlib_get_main (); ipsec_main_t *im = &ipsec_main; ipsec_sa_t *sa = 0; uword *p; @@ -286,6 +297,8 @@ ipsec_sa_del (u32 id) dpo_reset (&sa->dpo[IPSEC_PROTOCOL_AH]); dpo_reset (&sa->dpo[IPSEC_PROTOCOL_ESP]); } + vnet_crypto_key_del (vm, sa->crypto_key_index); + vnet_crypto_key_del (vm, sa->integ_key_index); pool_put (im->sad, sa); return 0; } @@ -320,6 +333,7 @@ ipsec_is_sa_used (u32 sa_index) int ipsec_set_sa_key (u32 id, const ipsec_key_t * ck, const ipsec_key_t * ik) { + vlib_main_t *vm = vlib_get_main (); ipsec_main_t *im = &ipsec_main; uword *p; u32 sa_index; @@ -337,12 +351,16 @@ ipsec_set_sa_key (u32 id, const ipsec_key_t * ck, const ipsec_key_t * ik) if (ck) { clib_memcpy (&sa->crypto_key, ck, sizeof (sa->crypto_key)); + vnet_crypto_key_modify (vm, sa->crypto_key_index, sa->crypto_calg, + (u8 *) ck->data, ck->len); } /* new integ key */ if (ik) { clib_memcpy (&sa->integ_key, 0, sizeof (sa->integ_key)); + vnet_crypto_key_modify (vm, sa->integ_key_index, sa->integ_calg, + (u8 *) ik->data, ik->len); } if (ck || ik) diff --git a/src/vnet/ipsec/ipsec_sa.h b/src/vnet/ipsec/ipsec_sa.h index d1b44c3165f..40850766ce1 100644 --- a/src/vnet/ipsec/ipsec_sa.h +++ b/src/vnet/ipsec/ipsec_sa.h @@ -125,9 +125,11 @@ typedef struct u32 last_seq_hi; u64 replay_window; - vnet_crypto_op_id_t crypto_enc_op_id; - vnet_crypto_op_id_t crypto_dec_op_id; - vnet_crypto_op_id_t integ_op_id; + vnet_crypto_key_index_t crypto_key_index; + vnet_crypto_key_index_t integ_key_index; + vnet_crypto_op_id_t crypto_enc_op_id:16; + vnet_crypto_op_id_t crypto_dec_op_id:16; + vnet_crypto_op_id_t integ_op_id:16; dpo_id_t dpo[IPSEC_N_PROTOCOLS]; @@ -149,9 +151,11 @@ typedef struct ipsec_crypto_alg_t crypto_alg; ipsec_key_t crypto_key; + vnet_crypto_alg_t crypto_calg; ipsec_integ_alg_t integ_alg; ipsec_key_t integ_key; + vnet_crypto_alg_t integ_calg; ip46_address_t tunnel_src_addr; ip46_address_t tunnel_dst_addr; |