Changeset View
Changeset View
Standalone View
Standalone View
sys/opencrypto/ktls_ocf.c
| Show First 20 Lines • Show All 100 Lines • ▼ Show 20 Lines | SYSCTL_COUNTER_U64(_kern_ipc_tls_stats_ocf, OID_AUTO, tls12_chacha20_decrypts, | ||||
| CTLFLAG_RD, &ocf_tls12_chacha20_decrypts, | CTLFLAG_RD, &ocf_tls12_chacha20_decrypts, | ||||
| "Total number of OCF TLS 1.2 Chacha20-Poly1305 decryption operations"); | "Total number of OCF TLS 1.2 Chacha20-Poly1305 decryption operations"); | ||||
| static COUNTER_U64_DEFINE_EARLY(ocf_tls12_chacha20_encrypts); | static COUNTER_U64_DEFINE_EARLY(ocf_tls12_chacha20_encrypts); | ||||
| SYSCTL_COUNTER_U64(_kern_ipc_tls_stats_ocf, OID_AUTO, tls12_chacha20_encrypts, | SYSCTL_COUNTER_U64(_kern_ipc_tls_stats_ocf, OID_AUTO, tls12_chacha20_encrypts, | ||||
| CTLFLAG_RD, &ocf_tls12_chacha20_encrypts, | CTLFLAG_RD, &ocf_tls12_chacha20_encrypts, | ||||
| "Total number of OCF TLS 1.2 Chacha20-Poly1305 encryption operations"); | "Total number of OCF TLS 1.2 Chacha20-Poly1305 encryption operations"); | ||||
| static COUNTER_U64_DEFINE_EARLY(ocf_tls13_gcm_decrypts); | |||||
| SYSCTL_COUNTER_U64(_kern_ipc_tls_stats_ocf, OID_AUTO, tls13_gcm_decrypts, | |||||
| CTLFLAG_RD, &ocf_tls13_gcm_decrypts, | |||||
| "Total number of OCF TLS 1.3 GCM decryption operations"); | |||||
| static COUNTER_U64_DEFINE_EARLY(ocf_tls13_gcm_encrypts); | static COUNTER_U64_DEFINE_EARLY(ocf_tls13_gcm_encrypts); | ||||
| SYSCTL_COUNTER_U64(_kern_ipc_tls_stats_ocf, OID_AUTO, tls13_gcm_encrypts, | SYSCTL_COUNTER_U64(_kern_ipc_tls_stats_ocf, OID_AUTO, tls13_gcm_encrypts, | ||||
| CTLFLAG_RD, &ocf_tls13_gcm_encrypts, | CTLFLAG_RD, &ocf_tls13_gcm_encrypts, | ||||
| "Total number of OCF TLS 1.3 GCM encryption operations"); | "Total number of OCF TLS 1.3 GCM encryption operations"); | ||||
| static COUNTER_U64_DEFINE_EARLY(ocf_tls13_chacha20_decrypts); | |||||
| SYSCTL_COUNTER_U64(_kern_ipc_tls_stats_ocf, OID_AUTO, tls13_chacha20_decrypts, | |||||
| CTLFLAG_RD, &ocf_tls13_chacha20_decrypts, | |||||
| "Total number of OCF TLS 1.3 Chacha20-Poly1305 decryption operations"); | |||||
| static COUNTER_U64_DEFINE_EARLY(ocf_tls13_chacha20_encrypts); | static COUNTER_U64_DEFINE_EARLY(ocf_tls13_chacha20_encrypts); | ||||
| SYSCTL_COUNTER_U64(_kern_ipc_tls_stats_ocf, OID_AUTO, tls13_chacha20_encrypts, | SYSCTL_COUNTER_U64(_kern_ipc_tls_stats_ocf, OID_AUTO, tls13_chacha20_encrypts, | ||||
| CTLFLAG_RD, &ocf_tls13_chacha20_encrypts, | CTLFLAG_RD, &ocf_tls13_chacha20_encrypts, | ||||
| "Total number of OCF TLS 1.3 Chacha20-Poly1305 encryption operations"); | "Total number of OCF TLS 1.3 Chacha20-Poly1305 encryption operations"); | ||||
| static COUNTER_U64_DEFINE_EARLY(ocf_inplace); | static COUNTER_U64_DEFINE_EARLY(ocf_inplace); | ||||
| SYSCTL_COUNTER_U64(_kern_ipc_tls_stats_ocf, OID_AUTO, inplace, | SYSCTL_COUNTER_U64(_kern_ipc_tls_stats_ocf, OID_AUTO, inplace, | ||||
| CTLFLAG_RD, &ocf_inplace, | CTLFLAG_RD, &ocf_inplace, | ||||
| ▲ Show 20 Lines • Show All 473 Lines • ▼ Show 20 Lines | ktls_ocf_tls13_aead_encrypt(struct ktls_ocf_encrypt_state *state, | ||||
| if (tls->sync_dispatch) { | if (tls->sync_dispatch) { | ||||
| error = ktls_ocf_dispatch(os, crp); | error = ktls_ocf_dispatch(os, crp); | ||||
| crypto_destroyreq(crp); | crypto_destroyreq(crp); | ||||
| } else | } else | ||||
| error = ktls_ocf_dispatch_async(state, crp); | error = ktls_ocf_dispatch_async(state, crp); | ||||
| return (error); | return (error); | ||||
| } | } | ||||
| static int | |||||
| ktls_ocf_tls13_aead_decrypt(struct ktls_session *tls, | |||||
| const struct tls_record_layer *hdr, struct mbuf *m, uint64_t seqno, | |||||
| int *trailer_len) | |||||
| { | |||||
| struct tls_aead_data_13 ad; | |||||
| struct cryptop crp; | |||||
| struct ktls_ocf_session *os; | |||||
| int error; | |||||
| u_int tag_len; | |||||
| os = tls->ocf_session; | |||||
| tag_len = tls->params.tls_tlen - 1; | |||||
| /* Payload must contain at least one byte for the record type. */ | |||||
| if (ntohs(hdr->tls_length) < tag_len + 1) | |||||
| return (EBADMSG); | |||||
| crypto_initreq(&crp, os->sid); | |||||
| /* Setup the nonce. */ | |||||
| memcpy(crp.crp_iv, tls->params.iv, tls->params.iv_len); | |||||
| *(uint64_t *)(crp.crp_iv + 4) ^= htobe64(seqno); | |||||
| /* Setup the AAD. */ | |||||
| ad.type = hdr->tls_type; | |||||
| ad.tls_vmajor = hdr->tls_vmajor; | |||||
| ad.tls_vminor = hdr->tls_vminor; | |||||
| ad.tls_length = hdr->tls_length; | |||||
| crp.crp_aad = &ad; | |||||
| crp.crp_aad_length = sizeof(ad); | |||||
| crp.crp_payload_start = tls->params.tls_hlen; | |||||
| crp.crp_payload_length = ntohs(hdr->tls_length) - tag_len; | |||||
| crp.crp_digest_start = crp.crp_payload_start + crp.crp_payload_length; | |||||
| crp.crp_op = CRYPTO_OP_DECRYPT | CRYPTO_OP_VERIFY_DIGEST; | |||||
| crp.crp_flags = CRYPTO_F_CBIMM | CRYPTO_F_IV_SEPARATE; | |||||
| crypto_use_mbuf(&crp, m); | |||||
| if (tls->params.cipher_algorithm == CRYPTO_AES_NIST_GCM_16) | |||||
| counter_u64_add(ocf_tls13_gcm_decrypts, 1); | |||||
| else | |||||
| counter_u64_add(ocf_tls13_chacha20_decrypts, 1); | |||||
| error = ktls_ocf_dispatch(os, &crp); | |||||
| crypto_destroyreq(&crp); | |||||
| *trailer_len = tag_len; | |||||
| return (error); | |||||
| } | |||||
| void | void | ||||
| ktls_ocf_free(struct ktls_session *tls) | ktls_ocf_free(struct ktls_session *tls) | ||||
| { | { | ||||
jhb: For NIC TLS RX support we may end up making this bit of code a helper routine that can be… | |||||
Not Done Inline ActionsSounds like a good idea, to factor this bit out. Then you don't really need two separate decryption functions. hselasky: Sounds like a good idea, to factor this bit out. Then you don't really need two separate… | |||||
Done Inline ActionsYou would still need separate decryption functions as some of the other details are different such as the AAD. I think splitting out this routine is probably something sensible to do in a future commit in a series adding 1.3 NIC TLS RX, but I might move it back to sys/kern/uipc_ktls.c. I had started with doing it in uipc_ktls.c but found it simpler to do it here instead. jhb: You would still need separate decryption functions as some of the other details are different… | |||||
Not Done Inline ActionsShould we have another callback function into OCF, which handle already decrypted traffic, to get the trailer length and header type fields correct? hselasky: Should we have another callback function into OCF, which handle already decrypted traffic, to… | |||||
| struct ktls_ocf_session *os; | struct ktls_ocf_session *os; | ||||
| os = tls->ocf_session; | os = tls->ocf_session; | ||||
| crypto_freesession(os->sid); | crypto_freesession(os->sid); | ||||
| mtx_destroy(&os->lock); | mtx_destroy(&os->lock); | ||||
| zfree(os, M_KTLS_OCF); | zfree(os, M_KTLS_OCF); | ||||
| } | } | ||||
| Show All 20 Lines | case CRYPTO_AES_NIST_GCM_16: | ||||
| } | } | ||||
| /* Only TLS 1.2 and 1.3 are supported. */ | /* Only TLS 1.2 and 1.3 are supported. */ | ||||
| if (tls->params.tls_vmajor != TLS_MAJOR_VER_ONE || | if (tls->params.tls_vmajor != TLS_MAJOR_VER_ONE || | ||||
| tls->params.tls_vminor < TLS_MINOR_VER_TWO || | tls->params.tls_vminor < TLS_MINOR_VER_TWO || | ||||
| tls->params.tls_vminor > TLS_MINOR_VER_THREE) | tls->params.tls_vminor > TLS_MINOR_VER_THREE) | ||||
| return (EPROTONOSUPPORT); | return (EPROTONOSUPPORT); | ||||
| /* TLS 1.3 is not yet supported for receive. */ | |||||
| if (direction == KTLS_RX && | |||||
| tls->params.tls_vminor == TLS_MINOR_VER_THREE) | |||||
| return (EPROTONOSUPPORT); | |||||
| csp.csp_flags |= CSP_F_SEPARATE_OUTPUT | CSP_F_SEPARATE_AAD; | csp.csp_flags |= CSP_F_SEPARATE_OUTPUT | CSP_F_SEPARATE_AAD; | ||||
| csp.csp_mode = CSP_MODE_AEAD; | csp.csp_mode = CSP_MODE_AEAD; | ||||
| csp.csp_cipher_alg = CRYPTO_AES_NIST_GCM_16; | csp.csp_cipher_alg = CRYPTO_AES_NIST_GCM_16; | ||||
| csp.csp_cipher_key = tls->params.cipher_key; | csp.csp_cipher_key = tls->params.cipher_key; | ||||
| csp.csp_cipher_klen = tls->params.cipher_key_len; | csp.csp_cipher_klen = tls->params.cipher_key_len; | ||||
| csp.csp_ivlen = AES_GCM_IV_LEN; | csp.csp_ivlen = AES_GCM_IV_LEN; | ||||
| break; | break; | ||||
| case CRYPTO_AES_CBC: | case CRYPTO_AES_CBC: | ||||
| ▲ Show 20 Lines • Show All 51 Lines • ▼ Show 20 Lines | case CRYPTO_CHACHA20_POLY1305: | ||||
| } | } | ||||
| /* Only TLS 1.2 and 1.3 are supported. */ | /* Only TLS 1.2 and 1.3 are supported. */ | ||||
| if (tls->params.tls_vmajor != TLS_MAJOR_VER_ONE || | if (tls->params.tls_vmajor != TLS_MAJOR_VER_ONE || | ||||
| tls->params.tls_vminor < TLS_MINOR_VER_TWO || | tls->params.tls_vminor < TLS_MINOR_VER_TWO || | ||||
| tls->params.tls_vminor > TLS_MINOR_VER_THREE) | tls->params.tls_vminor > TLS_MINOR_VER_THREE) | ||||
| return (EPROTONOSUPPORT); | return (EPROTONOSUPPORT); | ||||
| /* TLS 1.3 is not yet supported for receive. */ | |||||
| if (direction == KTLS_RX && | |||||
| tls->params.tls_vminor == TLS_MINOR_VER_THREE) | |||||
| return (EPROTONOSUPPORT); | |||||
| csp.csp_flags |= CSP_F_SEPARATE_OUTPUT | CSP_F_SEPARATE_AAD; | csp.csp_flags |= CSP_F_SEPARATE_OUTPUT | CSP_F_SEPARATE_AAD; | ||||
| csp.csp_mode = CSP_MODE_AEAD; | csp.csp_mode = CSP_MODE_AEAD; | ||||
| csp.csp_cipher_alg = CRYPTO_CHACHA20_POLY1305; | csp.csp_cipher_alg = CRYPTO_CHACHA20_POLY1305; | ||||
| csp.csp_cipher_key = tls->params.cipher_key; | csp.csp_cipher_key = tls->params.cipher_key; | ||||
| csp.csp_cipher_klen = tls->params.cipher_key_len; | csp.csp_cipher_klen = tls->params.cipher_key_len; | ||||
| csp.csp_ivlen = CHACHA20_POLY1305_IV_LEN; | csp.csp_ivlen = CHACHA20_POLY1305_IV_LEN; | ||||
| break; | break; | ||||
| default: | default: | ||||
| Show All 27 Lines | ktls_ocf_try(struct socket *so, struct ktls_session *tls, int direction) | ||||
| if (tls->params.cipher_algorithm == CRYPTO_AES_NIST_GCM_16 || | if (tls->params.cipher_algorithm == CRYPTO_AES_NIST_GCM_16 || | ||||
| tls->params.cipher_algorithm == CRYPTO_CHACHA20_POLY1305) { | tls->params.cipher_algorithm == CRYPTO_CHACHA20_POLY1305) { | ||||
| if (direction == KTLS_TX) { | if (direction == KTLS_TX) { | ||||
| if (tls->params.tls_vminor == TLS_MINOR_VER_THREE) | if (tls->params.tls_vminor == TLS_MINOR_VER_THREE) | ||||
| tls->sw_encrypt = ktls_ocf_tls13_aead_encrypt; | tls->sw_encrypt = ktls_ocf_tls13_aead_encrypt; | ||||
| else | else | ||||
| tls->sw_encrypt = ktls_ocf_tls12_aead_encrypt; | tls->sw_encrypt = ktls_ocf_tls12_aead_encrypt; | ||||
| } else { | } else { | ||||
| if (tls->params.tls_vminor == TLS_MINOR_VER_THREE) | |||||
| tls->sw_decrypt = ktls_ocf_tls13_aead_decrypt; | |||||
| else | |||||
| tls->sw_decrypt = ktls_ocf_tls12_aead_decrypt; | tls->sw_decrypt = ktls_ocf_tls12_aead_decrypt; | ||||
| } | } | ||||
| } else { | } else { | ||||
| tls->sw_encrypt = ktls_ocf_tls_cbc_encrypt; | tls->sw_encrypt = ktls_ocf_tls_cbc_encrypt; | ||||
| if (tls->params.tls_vminor == TLS_MINOR_VER_ZERO) { | if (tls->params.tls_vminor == TLS_MINOR_VER_ZERO) { | ||||
| os->implicit_iv = true; | os->implicit_iv = true; | ||||
| memcpy(os->iv, tls->params.iv, AES_BLOCK_LEN); | memcpy(os->iv, tls->params.iv, AES_BLOCK_LEN); | ||||
| #ifdef INVARIANTS | #ifdef INVARIANTS | ||||
| os->next_seqno = tls->next_seqno; | os->next_seqno = tls->next_seqno; | ||||
| Show All 13 Lines | |||||
For NIC TLS RX support we may end up making this bit of code a helper routine that can be shared with the NIC TLS RX path.