aboutsummaryrefslogtreecommitdiff
path: root/sys/netipsec
diff options
context:
space:
mode:
authorJohn Baldwin <jhb@FreeBSD.org>2020-03-27 18:25:23 +0000
committerJohn Baldwin <jhb@FreeBSD.org>2020-03-27 18:25:23 +0000
commitc03414326909ed7a740be3ba63fbbef01fe513a8 (patch)
tree9067f28738df03bb4b685773c52ba32517468212 /sys/netipsec
parent4d94781b4d9e03b8dbd6604d7e2280d342d3cf7e (diff)
downloadsrc-c03414326909ed7a740be3ba63fbbef01fe513a8.tar.gz
src-c03414326909ed7a740be3ba63fbbef01fe513a8.zip
Refactor driver and consumer interfaces for OCF (in-kernel crypto).
- The linked list of cryptoini structures used in session initialization is replaced with a new flat structure: struct crypto_session_params. This session includes a new mode to define how the other fields should be interpreted. Available modes include: - COMPRESS (for compression/decompression) - CIPHER (for simply encryption/decryption) - DIGEST (computing and verifying digests) - AEAD (combined auth and encryption such as AES-GCM and AES-CCM) - ETA (combined auth and encryption using encrypt-then-authenticate) Additional modes could be added in the future (e.g. if we wanted to support TLS MtE for AES-CBC in the kernel we could add a new mode for that. TLS modes might also affect how AAD is interpreted, etc.) The flat structure also includes the key lengths and algorithms as before. However, code doesn't have to walk the linked list and switch on the algorithm to determine which key is the auth key vs encryption key. The 'csp_auth_*' fields are always used for auth keys and settings and 'csp_cipher_*' for cipher. (Compression algorithms are stored in csp_cipher_alg.) - Drivers no longer register a list of supported algorithms. This doesn't quite work when you factor in modes (e.g. a driver might support both AES-CBC and SHA2-256-HMAC separately but not combined for ETA). Instead, a new 'crypto_probesession' method has been added to the kobj interface for symmteric crypto drivers. This method returns a negative value on success (similar to how device_probe works) and the crypto framework uses this value to pick the "best" driver. There are three constants for hardware (e.g. ccr), accelerated software (e.g. aesni), and plain software (cryptosoft) that give preference in that order. One effect of this is that if you request only hardware when creating a new session, you will no longer get a session using accelerated software. Another effect is that the default setting to disallow software crypto via /dev/crypto now disables accelerated software. Once a driver is chosen, 'crypto_newsession' is invoked as before. - Crypto operations are now solely described by the flat 'cryptop' structure. The linked list of descriptors has been removed. A separate enum has been added to describe the type of data buffer in use instead of using CRYPTO_F_* flags to make it easier to add more types in the future if needed (e.g. wired userspace buffers for zero-copy). It will also make it easier to re-introduce separate input and output buffers (in-kernel TLS would benefit from this). Try to make the flags related to IV handling less insane: - CRYPTO_F_IV_SEPARATE means that the IV is stored in the 'crp_iv' member of the operation structure. If this flag is not set, the IV is stored in the data buffer at the 'crp_iv_start' offset. - CRYPTO_F_IV_GENERATE means that a random IV should be generated and stored into the data buffer. This cannot be used with CRYPTO_F_IV_SEPARATE. If a consumer wants to deal with explicit vs implicit IVs, etc. it can always generate the IV however it needs and store partial IVs in the buffer and the full IV/nonce in crp_iv and set CRYPTO_F_IV_SEPARATE. The layout of the buffer is now described via fields in cryptop. crp_aad_start and crp_aad_length define the boundaries of any AAD. Previously with GCM and CCM you defined an auth crd with this range, but for ETA your auth crd had to span both the AAD and plaintext (and they had to be adjacent). crp_payload_start and crp_payload_length define the boundaries of the plaintext/ciphertext. Modes that only do a single operation (COMPRESS, CIPHER, DIGEST) should only use this region and leave the AAD region empty. If a digest is present (or should be generated), it's starting location is marked by crp_digest_start. Instead of using the CRD_F_ENCRYPT flag to determine the direction of the operation, cryptop now includes an 'op' field defining the operation to perform. For digests I've added a new VERIFY digest mode which assumes a digest is present in the input and fails the request with EBADMSG if it doesn't match the internally-computed digest. GCM and CCM already assumed this, and the new AEAD mode requires this for decryption. The new ETA mode now also requires this for decryption, so IPsec and GELI no longer do their own authentication verification. Simple DIGEST operations can also do this, though there are no in-tree consumers. To eventually support some refcounting to close races, the session cookie is now passed to crypto_getop() and clients should no longer set crp_sesssion directly. - Assymteric crypto operation structures should be allocated via crypto_getkreq() and freed via crypto_freekreq(). This permits the crypto layer to track open asym requests and close races with a driver trying to unregister while asym requests are in flight. - crypto_copyback, crypto_copydata, crypto_apply, and crypto_contiguous_subsegment now accept the 'crp' object as the first parameter instead of individual members. This makes it easier to deal with different buffer types in the future as well as separate input and output buffers. It's also simpler for driver writers to use. - bus_dmamap_load_crp() loads a DMA mapping for a crypto buffer. This understands the various types of buffers so that drivers that use DMA do not have to be aware of different buffer types. - Helper routines now exist to build an auth context for HMAC IPAD and OPAD. This reduces some duplicated work among drivers. - Key buffers are now treated as const throughout the framework and in device drivers. However, session key buffers provided when a session is created are expected to remain alive for the duration of the session. - GCM and CCM sessions now only specify a cipher algorithm and a cipher key. The redundant auth information is not needed or used. - For cryptosoft, split up the code a bit such that the 'process' callback now invokes a function pointer in the session. This function pointer is set based on the mode (in effect) though it simplifies a few edge cases that would otherwise be in the switch in 'process'. It does split up GCM vs CCM which I think is more readable even if there is some duplication. - I changed /dev/crypto to support GMAC requests using CRYPTO_AES_NIST_GMAC as an auth algorithm and updated cryptocheck to work with it. - Combined cipher and auth sessions via /dev/crypto now always use ETA mode. The COP_F_CIPHER_FIRST flag is now a no-op that is ignored. This was actually documented as being true in crypto(4) before, but the code had not implemented this before I added the CIPHER_FIRST flag. - I have not yet updated /dev/crypto to be aware of explicit modes for sessions. I will probably do that at some point in the future as well as teach it about IV/nonce and tag lengths for AEAD so we can support all of the NIST KAT tests for GCM and CCM. - I've split up the exising crypto.9 manpage into several pages of which many are written from scratch. - I have converted all drivers and consumers in the tree and verified that they compile, but I have not tested all of them. I have tested the following drivers: - cryptosoft - aesni (AES only) - blake2 - ccr and the following consumers: - cryptodev - IPsec - ktls_ocf - GELI (lightly) I have not tested the following: - ccp - aesni with sha - hifn - kgssapi_krb5 - ubsec - padlock - safe - armv8_crypto (aarch64) - glxsb (i386) - sec (ppc) - cesa (armv7) - cryptocteon (mips64) - nlmsec (mips64) Discussed with: cem Relnotes: yes Sponsored by: Chelsio Communications Differential Revision: https://reviews.freebsd.org/D23677
Notes
Notes: svn path=/head/; revision=359374
Diffstat (limited to 'sys/netipsec')
-rw-r--r--sys/netipsec/xform.h5
-rw-r--r--sys/netipsec/xform_ah.c78
-rw-r--r--sys/netipsec/xform_esp.c173
-rw-r--r--sys/netipsec/xform_ipcomp.c79
4 files changed, 141 insertions, 194 deletions
diff --git a/sys/netipsec/xform.h b/sys/netipsec/xform.h
index 910a88a706f3..85c9b65d1643 100644
--- a/sys/netipsec/xform.h
+++ b/sys/netipsec/xform.h
@@ -107,10 +107,11 @@ void xform_attach(void *);
void xform_detach(void *);
int xform_init(struct secasvar *, u_short);
-struct cryptoini;
+struct crypto_session_params;
/* XF_AH */
int xform_ah_authsize(const struct auth_hash *);
-extern int ah_init0(struct secasvar *, struct xformsw *, struct cryptoini *);
+int ah_init0(struct secasvar *, struct xformsw *,
+ struct crypto_session_params *);
extern int ah_zeroize(struct secasvar *sav);
extern size_t ah_hdrsiz(struct secasvar *);
diff --git a/sys/netipsec/xform_ah.c b/sys/netipsec/xform_ah.c
index 2ed9683a0572..834376634d5a 100644
--- a/sys/netipsec/xform_ah.c
+++ b/sys/netipsec/xform_ah.c
@@ -128,9 +128,7 @@ xform_ah_authsize(const struct auth_hash *esph)
alen = esph->hashsize / 2; /* RFC4868 2.3 */
break;
- case CRYPTO_AES_128_NIST_GMAC:
- case CRYPTO_AES_192_NIST_GMAC:
- case CRYPTO_AES_256_NIST_GMAC:
+ case CRYPTO_AES_NIST_GMAC:
alen = esph->hashsize;
break;
@@ -174,7 +172,8 @@ ah_hdrsiz(struct secasvar *sav)
* NB: public for use by esp_init.
*/
int
-ah_init0(struct secasvar *sav, struct xformsw *xsp, struct cryptoini *cria)
+ah_init0(struct secasvar *sav, struct xformsw *xsp,
+ struct crypto_session_params *csp)
{
const struct auth_hash *thash;
int keylen;
@@ -235,11 +234,10 @@ ah_init0(struct secasvar *sav, struct xformsw *xsp, struct cryptoini *cria)
sav->tdb_authalgxform = thash;
/* Initialize crypto session. */
- bzero(cria, sizeof (*cria));
- cria->cri_alg = sav->tdb_authalgxform->type;
- cria->cri_klen = _KEYBITS(sav->key_auth);
- cria->cri_key = sav->key_auth->key_data;
- cria->cri_mlen = AUTHSIZE(sav);
+ csp->csp_auth_alg = sav->tdb_authalgxform->type;
+ csp->csp_auth_klen = _KEYBITS(sav->key_auth) / 8;
+ csp->csp_auth_key = sav->key_auth->key_data;
+ csp->csp_auth_mlen = AUTHSIZE(sav);
return 0;
}
@@ -250,12 +248,14 @@ ah_init0(struct secasvar *sav, struct xformsw *xsp, struct cryptoini *cria)
static int
ah_init(struct secasvar *sav, struct xformsw *xsp)
{
- struct cryptoini cria;
+ struct crypto_session_params csp;
int error;
- error = ah_init0(sav, xsp, &cria);
+ memset(&csp, 0, sizeof(csp));
+ csp.csp_mode = CSP_MODE_DIGEST;
+ error = ah_init0(sav, xsp, &csp);
return error ? error :
- crypto_newsession(&sav->tdb_cryptoid, &cria, V_crypto_support);
+ crypto_newsession(&sav->tdb_cryptoid, &csp, V_crypto_support);
}
/*
@@ -560,7 +560,6 @@ ah_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
{
IPSEC_DEBUG_DECLARE(char buf[128]);
const struct auth_hash *ahx;
- struct cryptodesc *crda;
struct cryptop *crp;
struct xform_data *xd;
struct newah *ah;
@@ -628,7 +627,7 @@ ah_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
AHSTAT_ADD(ahs_ibytes, m->m_pkthdr.len - skip - hl);
/* Get crypto descriptors. */
- crp = crypto_getreq(1);
+ crp = crypto_getreq(cryptoid, M_NOWAIT);
if (crp == NULL) {
DPRINTF(("%s: failed to acquire crypto descriptor\n",
__func__));
@@ -637,17 +636,9 @@ ah_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
goto bad;
}
- crda = crp->crp_desc;
- IPSEC_ASSERT(crda != NULL, ("null crypto descriptor"));
-
- crda->crd_skip = 0;
- crda->crd_len = m->m_pkthdr.len;
- crda->crd_inject = skip + rplen;
-
- /* Authentication operation. */
- crda->crd_alg = ahx->type;
- crda->crd_klen = _KEYBITS(sav->key_auth);
- crda->crd_key = sav->key_auth->key_data;
+ crp->crp_payload_start = 0;
+ crp->crp_payload_length = m->m_pkthdr.len;
+ crp->crp_digest_start = skip + rplen;
/* Allocate IPsec-specific opaque crypto info. */
xd = malloc(sizeof(*xd) + skip + rplen + authsize, M_XDATA,
@@ -686,13 +677,14 @@ ah_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
/* Crypto operation descriptor. */
crp->crp_ilen = m->m_pkthdr.len; /* Total input length. */
- crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
+ crp->crp_op = CRYPTO_OP_COMPUTE_DIGEST;
+ crp->crp_flags = CRYPTO_F_CBIFSYNC;
if (V_async_crypto)
crp->crp_flags |= CRYPTO_F_ASYNC | CRYPTO_F_ASYNC_KEEPORDER;
- crp->crp_buf = (caddr_t) m;
+ crp->crp_mbuf = m;
+ crp->crp_buf_type = CRYPTO_BUF_MBUF;
crp->crp_callback = ah_input_cb;
- crp->crp_session = cryptoid;
- crp->crp_opaque = (caddr_t) xd;
+ crp->crp_opaque = xd;
/* These are passed as-is to the callback. */
xd->sav = sav;
@@ -725,8 +717,8 @@ ah_input_cb(struct cryptop *crp)
int authsize, rplen, ahsize, error, skip, protoff;
uint8_t nxt;
- m = (struct mbuf *) crp->crp_buf;
- xd = (struct xform_data *) crp->crp_opaque;
+ m = crp->crp_mbuf;
+ xd = crp->crp_opaque;
CURVNET_SET(xd->vnet);
sav = xd->sav;
skip = xd->skip;
@@ -866,7 +858,6 @@ ah_output(struct mbuf *m, struct secpolicy *sp, struct secasvar *sav,
{
IPSEC_DEBUG_DECLARE(char buf[IPSEC_ADDRSTRLEN]);
const struct auth_hash *ahx;
- struct cryptodesc *crda;
struct xform_data *xd;
struct mbuf *mi;
struct cryptop *crp;
@@ -988,7 +979,7 @@ ah_output(struct mbuf *m, struct secpolicy *sp, struct secasvar *sav,
SECASVAR_UNLOCK(sav);
/* Get crypto descriptors. */
- crp = crypto_getreq(1);
+ crp = crypto_getreq(cryptoid, M_NOWAIT);
if (crp == NULL) {
DPRINTF(("%s: failed to acquire crypto descriptors\n",
__func__));
@@ -997,15 +988,9 @@ ah_output(struct mbuf *m, struct secpolicy *sp, struct secasvar *sav,
goto bad;
}
- crda = crp->crp_desc;
- crda->crd_skip = 0;
- crda->crd_inject = skip + rplen;
- crda->crd_len = m->m_pkthdr.len;
-
- /* Authentication operation. */
- crda->crd_alg = ahx->type;
- crda->crd_key = sav->key_auth->key_data;
- crda->crd_klen = _KEYBITS(sav->key_auth);
+ crp->crp_payload_start = 0;
+ crp->crp_payload_length = m->m_pkthdr.len;
+ crp->crp_digest_start = skip + rplen;
/* Allocate IPsec-specific opaque crypto info. */
xd = malloc(sizeof(struct xform_data) + skip, M_XDATA,
@@ -1069,13 +1054,14 @@ ah_output(struct mbuf *m, struct secpolicy *sp, struct secasvar *sav,
/* Crypto operation descriptor. */
crp->crp_ilen = m->m_pkthdr.len; /* Total input length. */
- crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
+ crp->crp_op = CRYPTO_OP_COMPUTE_DIGEST;
+ crp->crp_flags = CRYPTO_F_CBIFSYNC;
if (V_async_crypto)
crp->crp_flags |= CRYPTO_F_ASYNC | CRYPTO_F_ASYNC_KEEPORDER;
- crp->crp_buf = (caddr_t) m;
+ crp->crp_mbuf = m;
+ crp->crp_buf_type = CRYPTO_BUF_MBUF;
crp->crp_callback = ah_output_cb;
- crp->crp_session = cryptoid;
- crp->crp_opaque = (caddr_t) xd;
+ crp->crp_opaque = xd;
/* These are passed as-is to the callback. */
xd->sp = sp;
diff --git a/sys/netipsec/xform_esp.c b/sys/netipsec/xform_esp.c
index 235d87ae1d98..c9c65aef6c4c 100644
--- a/sys/netipsec/xform_esp.c
+++ b/sys/netipsec/xform_esp.c
@@ -137,7 +137,7 @@ static int
esp_init(struct secasvar *sav, struct xformsw *xsp)
{
const struct enc_xform *txform;
- struct cryptoini cria, crie;
+ struct crypto_session_params csp;
int keylen;
int error;
@@ -193,11 +193,13 @@ esp_init(struct secasvar *sav, struct xformsw *xsp)
else
sav->ivlen = txform->ivsize;
+ memset(&csp, 0, sizeof(csp));
+
/*
* Setup AH-related state.
*/
if (sav->alg_auth != 0) {
- error = ah_init0(sav, xsp, &cria);
+ error = ah_init0(sav, xsp, &csp);
if (error)
return error;
}
@@ -231,35 +233,20 @@ esp_init(struct secasvar *sav, struct xformsw *xsp)
keylen, txform->name));
return EINVAL;
}
- bzero(&cria, sizeof(cria));
- cria.cri_alg = sav->tdb_authalgxform->type;
- cria.cri_key = sav->key_enc->key_data;
- cria.cri_klen = _KEYBITS(sav->key_enc) - SAV_ISGCM(sav) * 32;
- }
+ csp.csp_mode = CSP_MODE_AEAD;
+ } else if (sav->alg_auth != 0)
+ csp.csp_mode = CSP_MODE_ETA;
+ else
+ csp.csp_mode = CSP_MODE_CIPHER;
/* Initialize crypto session. */
- bzero(&crie, sizeof(crie));
- crie.cri_alg = sav->tdb_encalgxform->type;
- crie.cri_key = sav->key_enc->key_data;
- crie.cri_klen = _KEYBITS(sav->key_enc) - SAV_ISCTRORGCM(sav) * 32;
-
- if (sav->tdb_authalgxform && sav->tdb_encalgxform) {
- /* init both auth & enc */
- crie.cri_next = &cria;
- error = crypto_newsession(&sav->tdb_cryptoid,
- &crie, V_crypto_support);
- } else if (sav->tdb_encalgxform) {
- error = crypto_newsession(&sav->tdb_cryptoid,
- &crie, V_crypto_support);
- } else if (sav->tdb_authalgxform) {
- error = crypto_newsession(&sav->tdb_cryptoid,
- &cria, V_crypto_support);
- } else {
- /* XXX cannot happen? */
- DPRINTF(("%s: no encoding OR authentication xform!\n",
- __func__));
- error = EINVAL;
- }
+ csp.csp_cipher_alg = sav->tdb_encalgxform->type;
+ csp.csp_cipher_key = sav->key_enc->key_data;
+ csp.csp_cipher_klen = _KEYBITS(sav->key_enc) / 8 -
+ SAV_ISCTRORGCM(sav) * 4;
+ csp.csp_ivlen = txform->ivsize;
+
+ error = crypto_newsession(&sav->tdb_cryptoid, &csp, V_crypto_support);
return error;
}
@@ -289,7 +276,6 @@ esp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
const struct auth_hash *esph;
const struct enc_xform *espx;
struct xform_data *xd;
- struct cryptodesc *crde;
struct cryptop *crp;
struct newesp *esp;
uint8_t *ivp;
@@ -369,7 +355,7 @@ esp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
ESPSTAT_ADD(esps_ibytes, m->m_pkthdr.len - (skip + hlen + alen));
/* Get crypto descriptors */
- crp = crypto_getreq(esph && espx ? 2 : 1);
+ crp = crypto_getreq(cryptoid, M_NOWAIT);
if (crp == NULL) {
DPRINTF(("%s: failed to acquire crypto descriptors\n",
__func__));
@@ -379,7 +365,7 @@ esp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
}
/* Get IPsec-specific opaque pointer */
- xd = malloc(sizeof(*xd) + alen, M_XDATA, M_NOWAIT | M_ZERO);
+ xd = malloc(sizeof(*xd), M_XDATA, M_NOWAIT | M_ZERO);
if (xd == NULL) {
DPRINTF(("%s: failed to allocate xform_data\n", __func__));
ESPSTAT_INC(esps_crypto);
@@ -389,39 +375,24 @@ esp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
}
if (esph != NULL) {
- struct cryptodesc *crda = crp->crp_desc;
-
- IPSEC_ASSERT(crda != NULL, ("null ah crypto descriptor"));
-
- /* Authentication descriptor */
- crda->crd_skip = skip;
+ crp->crp_op = CRYPTO_OP_VERIFY_DIGEST;
+ crp->crp_aad_start = skip;
if (SAV_ISGCM(sav))
- crda->crd_len = 8; /* RFC4106 5, SPI + SN */
+ crp->crp_aad_length = 8; /* RFC4106 5, SPI + SN */
else
- crda->crd_len = m->m_pkthdr.len - (skip + alen);
- crda->crd_inject = m->m_pkthdr.len - alen;
-
- crda->crd_alg = esph->type;
-
- /* Copy the authenticator */
- m_copydata(m, m->m_pkthdr.len - alen, alen,
- (caddr_t) (xd + 1));
-
- /* Chain authentication request */
- crde = crda->crd_next;
- } else {
- crde = crp->crp_desc;
+ crp->crp_aad_length = hlen;
+ crp->crp_digest_start = m->m_pkthdr.len - alen;
}
/* Crypto operation descriptor */
crp->crp_ilen = m->m_pkthdr.len; /* Total input length */
- crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
+ crp->crp_flags = CRYPTO_F_CBIFSYNC;
if (V_async_crypto)
crp->crp_flags |= CRYPTO_F_ASYNC | CRYPTO_F_ASYNC_KEEPORDER;
- crp->crp_buf = (caddr_t) m;
+ crp->crp_mbuf = m;
+ crp->crp_buf_type = CRYPTO_BUF_MBUF;
crp->crp_callback = esp_input_cb;
- crp->crp_session = cryptoid;
- crp->crp_opaque = (caddr_t) xd;
+ crp->crp_opaque = xd;
/* These are passed as-is to the callback */
xd->sav = sav;
@@ -431,13 +402,12 @@ esp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
xd->vnet = curvnet;
/* Decryption descriptor */
- IPSEC_ASSERT(crde != NULL, ("null esp crypto descriptor"));
- crde->crd_skip = skip + hlen;
- crde->crd_len = m->m_pkthdr.len - (skip + hlen + alen);
- crde->crd_inject = skip + hlen - sav->ivlen;
+ crp->crp_op |= CRYPTO_OP_DECRYPT;
+ crp->crp_payload_start = skip + hlen;
+ crp->crp_payload_length = m->m_pkthdr.len - (skip + hlen + alen);
if (SAV_ISCTRORGCM(sav)) {
- ivp = &crde->crd_iv[0];
+ ivp = &crp->crp_iv[0];
/* GCM IV Format: RFC4106 4 */
/* CTR IV Format: RFC3686 4 */
@@ -452,10 +422,9 @@ esp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
}
m_copydata(m, skip + hlen - sav->ivlen, sav->ivlen, &ivp[4]);
- crde->crd_flags |= CRD_F_IV_EXPLICIT;
- }
-
- crde->crd_alg = espx->type;
+ crp->crp_flags |= CRYPTO_F_IV_SEPARATE;
+ } else if (sav->ivlen != 0)
+ crp->crp_iv_start = skip + hlen - sav->ivlen;
return (crypto_dispatch(crp));
bad:
@@ -471,22 +440,17 @@ static int
esp_input_cb(struct cryptop *crp)
{
IPSEC_DEBUG_DECLARE(char buf[128]);
- u_int8_t lastthree[3], aalg[AH_HMAC_MAXHASHLEN];
+ uint8_t lastthree[3];
const struct auth_hash *esph;
struct mbuf *m;
- struct cryptodesc *crd;
struct xform_data *xd;
struct secasvar *sav;
struct secasindex *saidx;
- caddr_t ptr;
crypto_session_t cryptoid;
int hlen, skip, protoff, error, alen;
- crd = crp->crp_desc;
- IPSEC_ASSERT(crd != NULL, ("null crypto descriptor!"));
-
- m = (struct mbuf *) crp->crp_buf;
- xd = (struct xform_data *) crp->crp_opaque;
+ m = crp->crp_mbuf;
+ xd = crp->crp_opaque;
CURVNET_SET(xd->vnet);
sav = xd->sav;
skip = xd->skip;
@@ -505,10 +469,15 @@ esp_input_cb(struct cryptop *crp)
CURVNET_RESTORE();
return (crypto_dispatch(crp));
}
- ESPSTAT_INC(esps_noxform);
- DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
- error = crp->crp_etype;
- goto bad;
+
+ /* EBADMSG indicates authentication failure. */
+ if (!(crp->crp_etype == EBADMSG && esph != NULL)) {
+ ESPSTAT_INC(esps_noxform);
+ DPRINTF(("%s: crypto error %d\n", __func__,
+ crp->crp_etype));
+ error = crp->crp_etype;
+ goto bad;
+ }
}
/* Shouldn't happen... */
@@ -524,12 +493,7 @@ esp_input_cb(struct cryptop *crp)
if (esph != NULL) {
alen = xform_ah_authsize(esph);
AHSTAT_INC(ahs_hist[sav->alg_auth]);
- /* Copy the authenticator from the packet */
- m_copydata(m, m->m_pkthdr.len - alen, alen, aalg);
- ptr = (caddr_t) (xd + 1);
-
- /* Verify authenticator */
- if (timingsafe_bcmp(ptr, aalg, alen) != 0) {
+ if (crp->crp_etype == EBADMSG) {
DPRINTF(("%s: authentication hash mismatch for "
"packet in SA %s/%08lx\n", __func__,
ipsec_address(&saidx->dst, buf, sizeof(buf)),
@@ -666,7 +630,6 @@ esp_output(struct mbuf *m, struct secpolicy *sp, struct secasvar *sav,
u_int idx, int skip, int protoff)
{
IPSEC_DEBUG_DECLARE(char buf[IPSEC_ADDRSTRLEN]);
- struct cryptodesc *crde = NULL, *crda = NULL;
struct cryptop *crp;
const struct auth_hash *esph;
const struct enc_xform *espx;
@@ -825,10 +788,10 @@ esp_output(struct mbuf *m, struct secpolicy *sp, struct secasvar *sav,
prot = IPPROTO_ESP;
m_copyback(m, protoff, sizeof(u_int8_t), (u_char *) &prot);
- /* Get crypto descriptors. */
- crp = crypto_getreq(esph != NULL ? 2 : 1);
+ /* Get crypto descriptor. */
+ crp = crypto_getreq(cryptoid, M_NOWAIT);
if (crp == NULL) {
- DPRINTF(("%s: failed to acquire crypto descriptors\n",
+ DPRINTF(("%s: failed to acquire crypto descriptor\n",
__func__));
ESPSTAT_INC(esps_crypto);
error = ENOBUFS;
@@ -845,19 +808,14 @@ esp_output(struct mbuf *m, struct secpolicy *sp, struct secasvar *sav,
goto bad;
}
- crde = crp->crp_desc;
- crda = crde->crd_next;
-
/* Encryption descriptor. */
- crde->crd_skip = skip + hlen;
- crde->crd_len = m->m_pkthdr.len - (skip + hlen + alen);
- crde->crd_flags = CRD_F_ENCRYPT;
- crde->crd_inject = skip + hlen - sav->ivlen;
+ crp->crp_payload_start = skip + hlen;
+ crp->crp_payload_length = m->m_pkthdr.len - (skip + hlen + alen);
+ crp->crp_op = CRYPTO_OP_ENCRYPT;
/* Encryption operation. */
- crde->crd_alg = espx->type;
if (SAV_ISCTRORGCM(sav)) {
- ivp = &crde->crd_iv[0];
+ ivp = &crp->crp_iv[0];
/* GCM IV Format: RFC4106 4 */
/* CTR IV Format: RFC3686 4 */
@@ -873,7 +831,10 @@ esp_output(struct mbuf *m, struct secpolicy *sp, struct secasvar *sav,
}
m_copyback(m, skip + hlen - sav->ivlen, sav->ivlen, &ivp[4]);
- crde->crd_flags |= CRD_F_IV_EXPLICIT|CRD_F_IV_PRESENT;
+ crp->crp_flags |= CRYPTO_F_IV_SEPARATE;
+ } else if (sav->ivlen != 0) {
+ crp->crp_iv_start = skip + hlen - sav->ivlen;
+ crp->crp_flags |= CRYPTO_F_IV_GENERATE;
}
/* Callback parameters */
@@ -885,23 +846,23 @@ esp_output(struct mbuf *m, struct secpolicy *sp, struct secasvar *sav,
/* Crypto operation descriptor. */
crp->crp_ilen = m->m_pkthdr.len; /* Total input length. */
- crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
+ crp->crp_flags |= CRYPTO_F_CBIFSYNC;
if (V_async_crypto)
crp->crp_flags |= CRYPTO_F_ASYNC | CRYPTO_F_ASYNC_KEEPORDER;
- crp->crp_buf = (caddr_t) m;
+ crp->crp_mbuf = m;
+ crp->crp_buf_type = CRYPTO_BUF_MBUF;
crp->crp_callback = esp_output_cb;
- crp->crp_opaque = (caddr_t) xd;
- crp->crp_session = cryptoid;
+ crp->crp_opaque = xd;
if (esph) {
/* Authentication descriptor. */
- crda->crd_alg = esph->type;
- crda->crd_skip = skip;
+ crp->crp_op |= CRYPTO_OP_COMPUTE_DIGEST;
+ crp->crp_aad_start = skip;
if (SAV_ISGCM(sav))
- crda->crd_len = 8; /* RFC4106 5, SPI + SN */
+ crp->crp_aad_length = 8; /* RFC4106 5, SPI + SN */
else
- crda->crd_len = m->m_pkthdr.len - (skip + alen);
- crda->crd_inject = m->m_pkthdr.len - alen;
+ crp->crp_aad_length = hlen;
+ crp->crp_digest_start = m->m_pkthdr.len - alen;
}
return crypto_dispatch(crp);
diff --git a/sys/netipsec/xform_ipcomp.c b/sys/netipsec/xform_ipcomp.c
index 96cffd6305a4..0529b2dda7c5 100644
--- a/sys/netipsec/xform_ipcomp.c
+++ b/sys/netipsec/xform_ipcomp.c
@@ -156,7 +156,7 @@ static int
ipcomp_init(struct secasvar *sav, struct xformsw *xsp)
{
const struct comp_algo *tcomp;
- struct cryptoini cric;
+ struct crypto_session_params csp;
/* NB: algorithm really comes in alg_enc and not alg_comp! */
tcomp = comp_algorithm_lookup(sav->alg_enc);
@@ -170,10 +170,11 @@ ipcomp_init(struct secasvar *sav, struct xformsw *xsp)
sav->tdb_compalgxform = tcomp;
/* Initialize crypto session */
- bzero(&cric, sizeof (cric));
- cric.cri_alg = sav->tdb_compalgxform->type;
+ memset(&csp, 0, sizeof(csp));
+ csp.csp_mode = CSP_MODE_COMPRESS;
+ csp.csp_cipher_alg = sav->tdb_compalgxform->type;
- return crypto_newsession(&sav->tdb_cryptoid, &cric, V_crypto_support);
+ return crypto_newsession(&sav->tdb_cryptoid, &csp, V_crypto_support);
}
/*
@@ -195,9 +196,9 @@ static int
ipcomp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
{
struct xform_data *xd;
- struct cryptodesc *crdc;
struct cryptop *crp;
struct ipcomp *ipcomp;
+ crypto_session_t cryptoid;
caddr_t addr;
int error, hlen = IPCOMP_HLENGTH;
@@ -222,8 +223,12 @@ ipcomp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
goto bad;
}
+ SECASVAR_LOCK(sav);
+ cryptoid = sav->tdb_cryptoid;
+ SECASVAR_UNLOCK(sav);
+
/* Get crypto descriptors */
- crp = crypto_getreq(1);
+ crp = crypto_getreq(cryptoid, M_NOWAIT);
if (crp == NULL) {
DPRINTF(("%s: no crypto descriptors\n", __func__));
IPCOMPSTAT_INC(ipcomps_crypto);
@@ -237,28 +242,26 @@ ipcomp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
crypto_freereq(crp);
goto bad;
}
- crdc = crp->crp_desc;
-
- crdc->crd_skip = skip + hlen;
- crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
- crdc->crd_inject = skip;
/* Decompression operation */
- crdc->crd_alg = sav->tdb_compalgxform->type;
-
+ crp->crp_op = CRYPTO_OP_DECOMPRESS;
+ crp->crp_payload_start = skip + hlen;
+ crp->crp_payload_length = m->m_pkthdr.len - (skip + hlen);
/* Crypto operation descriptor */
crp->crp_ilen = m->m_pkthdr.len - (skip + hlen);
- crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
- crp->crp_buf = (caddr_t) m;
+ crp->crp_flags = CRYPTO_F_CBIFSYNC;
+ crp->crp_mbuf = m;
+ crp->crp_buf_type = CRYPTO_BUF_MBUF;
crp->crp_callback = ipcomp_input_cb;
- crp->crp_opaque = (caddr_t) xd;
+ crp->crp_opaque = xd;
/* These are passed as-is to the callback */
xd->sav = sav;
xd->protoff = protoff;
xd->skip = skip;
xd->vnet = curvnet;
+ xd->cryptoid = cryptoid;
SECASVAR_LOCK(sav);
crp->crp_session = xd->cryptoid = sav->tdb_cryptoid;
@@ -288,8 +291,8 @@ ipcomp_input_cb(struct cryptop *crp)
int skip, protoff;
uint8_t nproto;
- m = (struct mbuf *) crp->crp_buf;
- xd = (struct xform_data *) crp->crp_opaque;
+ m = crp->crp_mbuf;
+ xd = crp->crp_opaque;
CURVNET_SET(xd->vnet);
sav = xd->sav;
skip = xd->skip;
@@ -396,9 +399,9 @@ ipcomp_output(struct mbuf *m, struct secpolicy *sp, struct secasvar *sav,
{
IPSEC_DEBUG_DECLARE(char buf[IPSEC_ADDRSTRLEN]);
const struct comp_algo *ipcompx;
- struct cryptodesc *crdc;
struct cryptop *crp;
struct xform_data *xd;
+ crypto_session_t cryptoid;
int error, ralen, maxpacketsize;
IPSEC_ASSERT(sav != NULL, ("null SA"));
@@ -466,25 +469,23 @@ ipcomp_output(struct mbuf *m, struct secpolicy *sp, struct secasvar *sav,
}
/* Ok now, we can pass to the crypto processing. */
+ SECASVAR_LOCK(sav);
+ cryptoid = sav->tdb_cryptoid;
+ SECASVAR_UNLOCK(sav);
/* Get crypto descriptors */
- crp = crypto_getreq(1);
+ crp = crypto_getreq(cryptoid, M_NOWAIT);
if (crp == NULL) {
IPCOMPSTAT_INC(ipcomps_crypto);
DPRINTF(("%s: failed to acquire crypto descriptor\n",__func__));
error = ENOBUFS;
goto bad;
}
- crdc = crp->crp_desc;
/* Compression descriptor */
- crdc->crd_skip = skip;
- crdc->crd_len = ralen;
- crdc->crd_flags = CRD_F_COMP;
- crdc->crd_inject = skip;
-
- /* Compression operation */
- crdc->crd_alg = ipcompx->type;
+ crp->crp_op = CRYPTO_OP_COMPRESS;
+ crp->crp_payload_start = skip;
+ crp->crp_payload_length = ralen;
/* IPsec-specific opaque crypto info */
xd = malloc(sizeof(struct xform_data), M_XDATA, M_NOWAIT | M_ZERO);
@@ -502,17 +503,15 @@ ipcomp_output(struct mbuf *m, struct secpolicy *sp, struct secasvar *sav,
xd->skip = skip;
xd->protoff = protoff;
xd->vnet = curvnet;
+ xd->cryptoid = cryptoid;
/* Crypto operation descriptor */
crp->crp_ilen = m->m_pkthdr.len; /* Total input length */
- crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
- crp->crp_buf = (caddr_t) m;
+ crp->crp_flags = CRYPTO_F_CBIFSYNC;
+ crp->crp_mbuf = m;
+ crp->crp_buf_type = CRYPTO_BUF_MBUF;
crp->crp_callback = ipcomp_output_cb;
- crp->crp_opaque = (caddr_t) xd;
-
- SECASVAR_LOCK(sav);
- crp->crp_session = xd->cryptoid = sav->tdb_cryptoid;
- SECASVAR_UNLOCK(sav);
+ crp->crp_opaque = xd;
return crypto_dispatch(crp);
bad:
@@ -538,8 +537,8 @@ ipcomp_output_cb(struct cryptop *crp)
u_int idx;
int error, skip, protoff;
- m = (struct mbuf *) crp->crp_buf;
- xd = (struct xform_data *) crp->crp_opaque;
+ m = crp->crp_mbuf;
+ xd = crp->crp_opaque;
CURVNET_SET(xd->vnet);
idx = xd->idx;
sp = xd->sp;
@@ -572,7 +571,7 @@ ipcomp_output_cb(struct cryptop *crp)
}
IPCOMPSTAT_INC(ipcomps_hist[sav->alg_comp]);
- if (crp->crp_ilen - skip > crp->crp_olen) {
+ if (crp->crp_payload_length > crp->crp_olen) {
struct mbuf *mo;
struct ipcomp *ipcomp;
int roff;
@@ -639,8 +638,8 @@ ipcomp_output_cb(struct cryptop *crp)
} else {
/* Compression was useless, we have lost time. */
IPCOMPSTAT_INC(ipcomps_uncompr);
- DPRINTF(("%s: compressions was useless %d - %d <= %d\n",
- __func__, crp->crp_ilen, skip, crp->crp_olen));
+ DPRINTF(("%s: compressions was useless %d <= %d\n",
+ __func__, crp->crp_payload_length, crp->crp_olen));
/* XXX remember state to not compress the next couple
* of packets, RFC 3173, 2.2. Non-Expansion Policy */
}