cryptodev: pass IV as offset
[dpdk.git] / examples / ipsec-secgw / esp.c
index 21b2f02..387ce4f 100644 (file)
@@ -1,7 +1,7 @@
 /*-
  *   BSD LICENSE
  *
- *   Copyright(c) 2016 Intel Corporation. All rights reserved.
+ *   Copyright(c) 2016-2017 Intel Corporation. All rights reserved.
  *   All rights reserved.
  *
  *   Redistribution and use in source and binary forms, with or without
@@ -50,6 +50,9 @@
 #include "esp.h"
 #include "ipip.h"
 
+#define IV_OFFSET              (sizeof(struct rte_crypto_op) + \
+                               sizeof(struct rte_crypto_sym_op))
+
 int
 esp_inbound(struct rte_mbuf *m, struct ipsec_sa *sa,
                struct rte_crypto_op *cop)
@@ -78,7 +81,7 @@ esp_inbound(struct rte_mbuf *m, struct ipsec_sa *sa,
                sizeof(struct esp_hdr) - sa->iv_len - sa->digest_len;
 
        if ((payload_len & (sa->block_size - 1)) || (payload_len <= 0)) {
-               RTE_LOG(DEBUG, IPSEC_ESP, "payload %d not multiple of %u\n",
+               RTE_LOG_DP(DEBUG, IPSEC_ESP, "payload %d not multiple of %u\n",
                                payload_len, sa->block_size);
                return -EINVAL;
        }
@@ -90,23 +93,54 @@ esp_inbound(struct rte_mbuf *m, struct ipsec_sa *sa,
                sa->iv_len;
        sym_cop->cipher.data.length = payload_len;
 
+       struct cnt_blk *icb;
+       uint8_t *aad;
        uint8_t *iv = RTE_PTR_ADD(ip4, ip_hdr_len + sizeof(struct esp_hdr));
+       uint8_t *iv_ptr = rte_crypto_op_ctod_offset(cop,
+                               uint8_t *, IV_OFFSET);
 
        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));
+               /* Copy IV at the end of crypto operation */
+               rte_memcpy(iv_ptr, iv, sa->iv_len);
+               sym_cop->cipher.iv.offset = IV_OFFSET;
                sym_cop->cipher.iv.length = sa->iv_len;
+               break;
+       case RTE_CRYPTO_CIPHER_AES_CTR:
+       case RTE_CRYPTO_CIPHER_AES_GCM:
+               icb = get_cnt_blk(m);
+               icb->salt = sa->salt;
+               memcpy(&icb->iv, iv, 8);
+               icb->cnt = rte_cpu_to_be_32(1);
+               sym_cop->cipher.iv.offset = IV_OFFSET;
+               sym_cop->cipher.iv.length = 16;
+               break;
+       default:
+               RTE_LOG(ERR, IPSEC_ESP, "unsupported cipher algorithm %u\n",
+                               sa->cipher_algo);
+               return -EINVAL;
+       }
 
+       switch (sa->auth_algo) {
+       case RTE_CRYPTO_AUTH_NULL:
+       case RTE_CRYPTO_AUTH_SHA1_HMAC:
+       case RTE_CRYPTO_AUTH_SHA256_HMAC:
                sym_cop->auth.data.offset = ip_hdr_len;
                sym_cop->auth.data.length = sizeof(struct esp_hdr) +
                        sa->iv_len + payload_len;
                break;
+       case RTE_CRYPTO_AUTH_AES_GCM:
+               aad = get_aad(m);
+               memcpy(aad, iv - sizeof(struct esp_hdr), 8);
+               sym_cop->auth.aad.data = aad;
+               sym_cop->auth.aad.phys_addr = rte_pktmbuf_mtophys_offset(m,
+                               aad - rte_pktmbuf_mtod(m, uint8_t *));
+               sym_cop->auth.aad.length = 8;
+               break;
        default:
-               RTE_LOG(ERR, IPSEC_ESP, "unsupported cipher algorithm %u\n",
-                               sa->cipher_algo);
+               RTE_LOG(ERR, IPSEC_ESP, "unsupported auth algorithm %u\n",
+                               sa->auth_algo);
                return -EINVAL;
        }
 
@@ -291,6 +325,13 @@ esp_outbound(struct rte_mbuf *m, struct ipsec_sa *sa,
                        sizeof(struct esp_hdr);
                sym_cop->cipher.data.length = pad_payload_len + sa->iv_len;
                break;
+       case RTE_CRYPTO_CIPHER_AES_CTR:
+       case RTE_CRYPTO_CIPHER_AES_GCM:
+               *iv = sa->seq;
+               sym_cop->cipher.data.offset = ip_hdr_len +
+                       sizeof(struct esp_hdr) + sa->iv_len;
+               sym_cop->cipher.data.length = pad_payload_len;
+               break;
        default:
                RTE_LOG(ERR, IPSEC_ESP, "unsupported cipher algorithm %u\n",
                                sa->cipher_algo);
@@ -307,21 +348,30 @@ esp_outbound(struct rte_mbuf *m, struct ipsec_sa *sa,
        icb->salt = sa->salt;
        icb->iv = sa->seq;
        icb->cnt = rte_cpu_to_be_32(1);
-       sym_cop->cipher.iv.data = (uint8_t *)icb;
-       sym_cop->cipher.iv.phys_addr = rte_pktmbuf_mtophys_offset(m,
-                        (uint8_t *)icb - rte_pktmbuf_mtod(m, uint8_t *));
+       sym_cop->cipher.iv.offset = IV_OFFSET;
        sym_cop->cipher.iv.length = 16;
 
-       switch (sa->cipher_algo) {
-       case RTE_CRYPTO_CIPHER_NULL:
-       case RTE_CRYPTO_CIPHER_AES_CBC:
+       uint8_t *aad;
+
+       switch (sa->auth_algo) {
+       case RTE_CRYPTO_AUTH_NULL:
+       case RTE_CRYPTO_AUTH_SHA1_HMAC:
+       case RTE_CRYPTO_AUTH_SHA256_HMAC:
                sym_cop->auth.data.offset = ip_hdr_len;
                sym_cop->auth.data.length = sizeof(struct esp_hdr) +
                        sa->iv_len + pad_payload_len;
                break;
+       case RTE_CRYPTO_AUTH_AES_GCM:
+               aad = get_aad(m);
+               memcpy(aad, esp, 8);
+               sym_cop->auth.aad.data = aad;
+               sym_cop->auth.aad.phys_addr = rte_pktmbuf_mtophys_offset(m,
+                               aad - rte_pktmbuf_mtod(m, uint8_t *));
+               sym_cop->auth.aad.length = 8;
+               break;
        default:
-               RTE_LOG(ERR, IPSEC_ESP, "unsupported cipher algorithm %u\n",
-                               sa->cipher_algo);
+               RTE_LOG(ERR, IPSEC_ESP, "unsupported auth algorithm %u\n",
+                               sa->auth_algo);
                return -EINVAL;
        }