DPDK patches and discussions
 help / color / mirror / Atom feed
From: Sergio Gonzalez Monroy <sergio.gonzalez.monroy@intel.com>
To: dev@dpdk.org
Subject: [dpdk-dev] [PATCH 1/3] examples/ipsec-secgw: change CBC IV generation
Date: Tue, 23 Aug 2016 17:59:32 +0100	[thread overview]
Message-ID: <1471971574-108998-2-git-send-email-sergio.gonzalez.monroy@intel.com> (raw)
In-Reply-To: <1471971574-108998-1-git-send-email-sergio.gonzalez.monroy@intel.com>

NIST SP800-38A recommends two methods to generate unpredictable IVs
(Initilisation Vector) for CBC mode:
1) Apply the forward function to a nonce (ie. counter)
2) Use a FIPS-approved random number generator

This patch implements the first recommended method by using the forward
function to generate the IV.

Signed-off-by: Sergio Gonzalez Monroy <sergio.gonzalez.monroy@intel.com>
---
 examples/ipsec-secgw/esp.c   | 99 +++++++++++++++++++++++++-------------------
 examples/ipsec-secgw/ipsec.h | 33 ++++++++++++++-
 2 files changed, 88 insertions(+), 44 deletions(-)

diff --git a/examples/ipsec-secgw/esp.c b/examples/ipsec-secgw/esp.c
index 05caa77..d640ab2 100644
--- a/examples/ipsec-secgw/esp.c
+++ b/examples/ipsec-secgw/esp.c
@@ -50,21 +50,6 @@
 #include "esp.h"
 #include "ipip.h"
 
-static inline void
-random_iv_u64(uint64_t *buf, uint16_t n)
-{
-	uint32_t left = n & 0x7;
-	uint32_t i;
-
-	RTE_ASSERT((n & 0x3) == 0);
-
-	for (i = 0; i < (n >> 3); i++)
-		buf[i] = rte_rand();
-
-	if (left)
-		*((uint32_t *)&buf[i]) = (uint32_t)lrand48();
-}
-
 int
 esp_inbound(struct rte_mbuf *m, struct ipsec_sa *sa,
 		struct rte_crypto_op *cop)
@@ -98,22 +83,32 @@ esp_inbound(struct rte_mbuf *m, struct ipsec_sa *sa,
 		return -EINVAL;
 	}
 
-	sym_cop = (struct rte_crypto_sym_op *)(cop + 1);
+	sym_cop = get_sym_cop(cop);
 
 	sym_cop->m_src = m;
 	sym_cop->cipher.data.offset =  ip_hdr_len + sizeof(struct esp_hdr) +
 		sa->iv_len;
 	sym_cop->cipher.data.length = payload_len;
 
-	sym_cop->cipher.iv.data = rte_pktmbuf_mtod_offset(m, void*,
-			 ip_hdr_len + sizeof(struct esp_hdr));
-	sym_cop->cipher.iv.phys_addr = rte_pktmbuf_mtophys_offset(m,
-			 ip_hdr_len + sizeof(struct esp_hdr));
-	sym_cop->cipher.iv.length = sa->iv_len;
+	uint8_t *iv = RTE_PTR_ADD(ip4, ip_hdr_len + sizeof(struct esp_hdr));
+
+	switch (sa->cipher_algo) {
+	case RTE_CRYPTO_CIPHER_NULL:
+	case RTE_CRYPTO_CIPHER_AES_CBC:
+		sym_cop->cipher.iv.data = iv;
+		sym_cop->cipher.iv.phys_addr = rte_pktmbuf_mtophys_offset(m,
+				 ip_hdr_len + sizeof(struct esp_hdr));
+		sym_cop->cipher.iv.length = sa->iv_len;
 
-	sym_cop->auth.data.offset = ip_hdr_len;
-	sym_cop->auth.data.length = sizeof(struct esp_hdr) +
-		sa->iv_len + payload_len;
+		sym_cop->auth.data.offset = ip_hdr_len;
+		sym_cop->auth.data.length = sizeof(struct esp_hdr) +
+			sa->iv_len + payload_len;
+		break;
+	default:
+		RTE_LOG(ERR, IPSEC_ESP, "unsupported cipher algorithm %u\n",
+				sa->cipher_algo);
+		return -EINVAL;
+	}
 
 	sym_cop->auth.digest.data = rte_pktmbuf_mtod_offset(m, void*,
 			rte_pktmbuf_pkt_len(m) - sa->digest_len);
@@ -282,10 +277,25 @@ esp_outbound(struct rte_mbuf *m, struct ipsec_sa *sa,
 
 	sa->seq++;
 	esp->spi = rte_cpu_to_be_32(sa->spi);
-	esp->seq = rte_cpu_to_be_32(sa->seq);
+	esp->seq = rte_cpu_to_be_32((uint32_t)sa->seq);
 
-	if (sa->cipher_algo == RTE_CRYPTO_CIPHER_AES_CBC)
-		random_iv_u64((uint64_t *)(esp + 1), sa->iv_len);
+	uint64_t *iv = (uint64_t *)(esp + 1);
+
+	sym_cop = get_sym_cop(cop);
+	sym_cop->m_src = m;
+	switch (sa->cipher_algo) {
+	case RTE_CRYPTO_CIPHER_NULL:
+	case RTE_CRYPTO_CIPHER_AES_CBC:
+		memset(iv, 0, sa->iv_len);
+		sym_cop->cipher.data.offset = ip_hdr_len +
+			sizeof(struct esp_hdr);
+		sym_cop->cipher.data.length = pad_payload_len + sa->iv_len;
+		break;
+	default:
+		RTE_LOG(ERR, IPSEC_ESP, "unsupported cipher algorithm %u\n",
+				sa->cipher_algo);
+		return -EINVAL;
+	}
 
 	/* Fill pad_len using default sequential scheme */
 	for (i = 0; i < pad_len - 2; i++)
@@ -293,22 +303,27 @@ esp_outbound(struct rte_mbuf *m, struct ipsec_sa *sa,
 	padding[pad_len - 2] = pad_len - 2;
 	padding[pad_len - 1] = nlp;
 
-	sym_cop = (struct rte_crypto_sym_op *)(cop + 1);
-
-	sym_cop->m_src = m;
-	sym_cop->cipher.data.offset = ip_hdr_len + sizeof(struct esp_hdr) +
-			sa->iv_len;
-	sym_cop->cipher.data.length = pad_payload_len;
-
-	sym_cop->cipher.iv.data = rte_pktmbuf_mtod_offset(m, uint8_t *,
-			 ip_hdr_len + sizeof(struct esp_hdr));
+	struct cnt_blk *nonce = get_cnt_blk(m);
+	nonce->salt = sa->salt;
+	nonce->iv = sa->seq;
+	sym_cop->cipher.iv.data = (uint8_t *)nonce;
 	sym_cop->cipher.iv.phys_addr = rte_pktmbuf_mtophys_offset(m,
-			 ip_hdr_len + sizeof(struct esp_hdr));
-	sym_cop->cipher.iv.length = sa->iv_len;
-
-	sym_cop->auth.data.offset = ip_hdr_len;
-	sym_cop->auth.data.length = sizeof(struct esp_hdr) + sa->iv_len +
-		pad_payload_len;
+			 (uint8_t *)nonce - rte_pktmbuf_mtod(m, uint8_t *));
+	/* FIXME should be 12 for GCM, but API needs 16 */
+	sym_cop->cipher.iv.length = 16;
+
+	switch (sa->cipher_algo) {
+	case RTE_CRYPTO_CIPHER_NULL:
+	case RTE_CRYPTO_CIPHER_AES_CBC:
+		sym_cop->auth.data.offset = ip_hdr_len;
+		sym_cop->auth.data.length = sizeof(struct esp_hdr) +
+			sa->iv_len + pad_payload_len;
+		break;
+	default:
+		RTE_LOG(ERR, IPSEC_ESP, "unsupported cipher algorithm %u\n",
+				sa->cipher_algo);
+		return -EINVAL;
+	}
 
 	sym_cop->auth.digest.data = rte_pktmbuf_mtod_offset(m, uint8_t *,
 			rte_pktmbuf_pkt_len(m) - sa->digest_len);
diff --git a/examples/ipsec-secgw/ipsec.h b/examples/ipsec-secgw/ipsec.h
index 4cc316c..d950df3 100644
--- a/examples/ipsec-secgw/ipsec.h
+++ b/examples/ipsec-secgw/ipsec.h
@@ -95,8 +95,9 @@ struct ip_addr {
 struct ipsec_sa {
 	uint32_t spi;
 	uint32_t cdev_id_qp;
+	uint64_t seq;
+	uint32_t salt;
 	struct rte_cryptodev_sym_session *crypto_session;
-	uint32_t seq;
 	enum rte_crypto_cipher_algorithm cipher_algo;
 	enum rte_crypto_auth_algorithm auth_algo;
 	uint16_t digest_len;
@@ -116,10 +117,11 @@ struct ipsec_sa {
 } __rte_cache_aligned;
 
 struct ipsec_mbuf_metadata {
+	uint8_t buf[32];
 	struct ipsec_sa *sa;
 	struct rte_crypto_op cop;
 	struct rte_crypto_sym_op sym_cop;
-};
+} __rte_cache_aligned;
 
 struct cdev_qp {
 	uint16_t id;
@@ -157,6 +159,11 @@ struct socket_ctx {
 	struct rte_mempool *mbuf_pool;
 };
 
+struct cnt_blk {
+	uint32_t salt;
+	uint64_t iv;
+};
+
 uint16_t
 ipsec_inbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
 		uint16_t nb_pkts, uint16_t len);
@@ -177,6 +184,28 @@ get_priv(struct rte_mbuf *m)
 	return RTE_PTR_ADD(m, sizeof(struct rte_mbuf));
 }
 
+static inline void *
+get_cnt_blk(struct rte_mbuf *m)
+{
+	struct ipsec_mbuf_metadata *priv = get_priv(m);
+
+	return &priv->buf[0];
+}
+
+static inline void *
+get_cop(struct rte_mbuf *m)
+{
+	struct ipsec_mbuf_metadata *priv = get_priv(m);
+
+	return &priv->cop;
+}
+
+static inline void *
+get_sym_cop(struct rte_crypto_op *cop)
+{
+	return (cop + 1);
+}
+
 int
 inbound_sa_check(struct sa_ctx *sa_ctx, struct rte_mbuf *m, uint32_t sa_idx);
 
-- 
2.5.5

  reply	other threads:[~2016-08-23 16:59 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-08-23 16:59 [dpdk-dev] [PATCH 0/3] IPSec AES-GCM support Sergio Gonzalez Monroy
2016-08-23 16:59 ` Sergio Gonzalez Monroy [this message]
2016-08-23 16:59 ` [dpdk-dev] [PATCH 2/3] examples/ipsec-secgw: reset crypto operation status Sergio Gonzalez Monroy
2016-08-23 16:59 ` [dpdk-dev] [PATCH 3/3] examples/ipsec-secgw: add AES-GCM support Sergio Gonzalez Monroy
2016-09-07 18:11   ` De Lara Guarch, Pablo
2016-09-23  7:45 ` [dpdk-dev] [PATCH v2 0/8] IPsec Enhancements Sergio Gonzalez Monroy
2016-09-23  7:45   ` [dpdk-dev] [PATCH v2 1/7] examples/ipsec-secgw: change CBC IV generation Sergio Gonzalez Monroy
2016-09-28  3:51     ` De Lara Guarch, Pablo
2016-09-29 14:39       ` Sergio Gonzalez Monroy
2016-09-23  7:45   ` [dpdk-dev] [PATCH v2 2/7] examples/ipsec-secgw: reset crypto operation status Sergio Gonzalez Monroy
2016-09-23  7:45   ` [dpdk-dev] [PATCH v2 3/7] examples/ipsec-secgw: add AES-GCM support Sergio Gonzalez Monroy
2016-09-23  7:45   ` [dpdk-dev] [PATCH v2 4/7] examples/ipsec-secgw: enable AES-CTR mode Sergio Gonzalez Monroy
2016-09-23  7:45   ` [dpdk-dev] [PATCH v2 5/7] examples/ipsec-secgw: check sp only when setup Sergio Gonzalez Monroy
2016-09-23  7:45   ` [dpdk-dev] [PATCH v2 6/7] examples/ipsec-secgw: cryptodev queue size macro Sergio Gonzalez Monroy
2016-09-28  3:50     ` De Lara Guarch, Pablo
2016-09-23  7:45   ` [dpdk-dev] [PATCH v2 7/7] examples/ipsec-secgw: initialize sa salt Sergio Gonzalez Monroy
2016-09-23  7:45   ` [dpdk-dev] [PATCH v2 8/8] examples/ipsec-secgw: update release notes Sergio Gonzalez Monroy
2016-09-28  4:05     ` De Lara Guarch, Pablo
2016-09-29 14:32       ` Sergio Gonzalez Monroy
2016-09-29  2:23   ` [dpdk-dev] [PATCH v2 0/8] IPsec Enhancements Chen, Zhaoyan
2016-09-29 15:44   ` [dpdk-dev] [PATCH v3 0/9] " Sergio Gonzalez Monroy
2016-09-29 15:44     ` [dpdk-dev] [PATCH v3 1/9] examples/ipsec-secgw: change CBC IV generation Sergio Gonzalez Monroy
2016-09-29 15:44     ` [dpdk-dev] [PATCH v3 2/9] examples/ipsec-secgw: reset crypto operation status Sergio Gonzalez Monroy
2016-09-29 15:44     ` [dpdk-dev] [PATCH v3 3/9] examples/ipsec-secgw: add AES-GCM support Sergio Gonzalez Monroy
2016-09-29 15:44     ` [dpdk-dev] [PATCH v3 4/9] examples/ipsec-secgw: enable AES-CTR mode Sergio Gonzalez Monroy
2016-09-29 15:44     ` [dpdk-dev] [PATCH v3 5/9] examples/ipsec-secgw: check sp only when setup Sergio Gonzalez Monroy
2016-09-29 15:44     ` [dpdk-dev] [PATCH v3 6/9] examples/ipsec-secgw: add cryptodev queue size macro Sergio Gonzalez Monroy
2016-09-29 15:44     ` [dpdk-dev] [PATCH v3 7/9] examples/ipsec-secgw: initialize sa salt Sergio Gonzalez Monroy
2016-09-29 15:44     ` [dpdk-dev] [PATCH v3 8/9] doc: ipsec-secgw release notes Sergio Gonzalez Monroy
2016-09-30 15:04       ` Mcnamara, John
2016-09-29 15:44     ` [dpdk-dev] [PATCH v3 8/9] examples/ipsec-secgw: update " Sergio Gonzalez Monroy
2016-09-29 15:44     ` [dpdk-dev] [PATCH v3 9/9] doc: update ipsec-secgw sample app guide Sergio Gonzalez Monroy
2016-09-30 15:04       ` Mcnamara, John
2016-09-29 15:44     ` [dpdk-dev] [PATCH v3 9/9] examples/ipsec-secgw: update ipsec-secgw guide Sergio Gonzalez Monroy
2016-09-30  0:27     ` [dpdk-dev] [PATCH v3 0/9] IPsec Enhancements De Lara Guarch, Pablo
2016-10-05  0:43       ` De Lara Guarch, Pablo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1471971574-108998-2-git-send-email-sergio.gonzalez.monroy@intel.com \
    --to=sergio.gonzalez.monroy@intel.com \
    --cc=dev@dpdk.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).