/* * ipsecmb.c - Intel IPSec Multi-buffer library Crypto Engine * * Copyright (c) 2019 Cisco Systemss * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include #include #include #include #include #define HMAC_MAX_BLOCK_SIZE SHA_512_BLOCK_SIZE #define EXPANDED_KEY_N_BYTES (16 * 15) typedef struct { CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); MB_MGR *mgr; __m128i cbc_iv; } ipsecmb_per_thread_data_t; typedef struct { u16 data_size; u8 block_size; aes_gcm_pre_t aes_gcm_pre; keyexp_t keyexp; hash_one_block_t hash_one_block; hash_fn_t hash_fn; } ipsecmb_alg_data_t; typedef struct ipsecmb_main_t_ { ipsecmb_per_thread_data_t *per_thread_data; ipsecmb_alg_data_t alg_data[VNET_CRYPTO_N_ALGS]; void **key_data; } ipsecmb_main_t; typedef struct { u8 enc_key_exp[EXPANDED_KEY_N_BYTES]; u8 dec_key_exp[EXPANDED_KEY_N_BYTES]; } ipsecmb_aes_cbc_key_data_t; static ipsecmb_main_t ipsecmb_main = { }; /* * (Alg, JOB_HASH_ALG, fn, block-size-bytes, hash-size-bytes, digest-size-bytes) */ #define foreach_ipsecmb_hmac_op \ _(SHA1, SHA1, sha1, 64, 20, 20) \ _(SHA224, SHA_224, sha224, 64, 32, 28) \ _(SHA256, SHA_256, sha256, 64, 32, 32) \ _(SHA384, SHA_384, sha384, 128, 64, 48) \ _(SHA512, SHA_512, sha512, 128, 64, 64) /* * (Alg, key-len-bits) */ #define foreach_ipsecmb_cbc_cipher_op \ _(AES_128_CBC, 128) \ _(AES_192_CBC, 192) \ _(AES_256_CBC, 256) /* * (Alg, key-len-bytes, iv-len-bytes) */ #define foreach_ipsecmb_gcm_cipher_op \ _(AES_128_GCM, 128) \ _(AES_192_GCM, 192) \ _(AES_256_GCM, 256) always_inline void ipsecmb_retire_hmac_job (JOB_AES_HMAC * job, u32 * n_fail, u32 digest_size) { vnet_crypto_op_t *op = job->user_data; u32 len = op->digest_len ? op->digest_len : digest_size; if (STS_COMPLETED != job->status) { op->status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC; *n_fail = *n_fail + 1; return; } if (op->flags & VNET_CRYPTO_OP_FLAG_HMAC_CHECK) { if ((memcmp (op->digest, job->auth_tag_output, len))) { *n_fail = *n_fail + 1; op->status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC; return; } } else if (len == digest_size) clib_memcpy_fast (op->digest, job->auth_tag_output, digest_size); else clib_memcpy_fast (op->digest, job->auth_tag_output, len); op->status = VNET_CRYPTO_OP_STATUS_COMPLETED; } static_always_inline u32 ipsecmb_ops_hmac_inline (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops, u32 block_size, u32 hash_size, u32 digest_size, JOB_HASH_ALG alg) { ipsecmb_main_t *imbm = &ipsecmb_main; ipsecmb_per_thread_data_t *ptd = vec_elt_at_index (imbm->per_thread_data, vm->thread_index); JOB_AES_HMAC *job; u32 i, n_fail = 0; u8 scratch[n_ops][digest_size]; /* * queue all the jobs first ... */ for (i = 0; i < n_ops; i++) { vnet_crypto_op_t *op = ops[i]; u8 *kd = (u8 *) imbm->key_data[op->key_index]; job = IMB_GET_NEXT_JOB (ptd->mgr); job->src = op->src; job->hash_start_src_offset_in_bytes = 0; job->msg_len_to_hash_in_bytes = op->len; job->hash_alg = alg; job->auth_tag_output_len_in_bytes = digest_size; job->auth_tag_output = scratch[i]; job->cipher_mode = NULL_CIPHER; job->cipher_direction = DECRYPT; job->chain_order = HASH_CIPHER; job->u.HMAC._hashed_auth_key_xor_ipad = kd; job->u.HMAC._hashed_auth_key_xor_opad = kd + hash_size; job->user_data = op; job = IMB_SUBMIT_JOB (ptd->mgr); if (job) ipsecmb_retire_hmac_job (job, &n_fail, digest_size); } while ((job = IMB_FLUSH_JOB (ptd->mgr))) ipsecmb_retire_hmac_job (job, &n_fail, digest_size); return n_ops - n_fail; } #define _(a, b, c, d, e, f) \ static_always_inline u32 \ ipsecmb_ops_hmac_##a (vlib_main_t * vm, \ vnet_crypto_op_t * ops[], \ u32 n_ops) \ { return ipsecmb_ops_hmac_inline (vm, ops, n_ops, d, e, f, b); } \ foreach_ipsecmb_hmac_op; #undef _ always_inline void ipsecmb_retire_cipher_job (JOB_AES_HMAC * job, u32 * n_fail) { vnet_crypto_op_t *op = job->user_data; if (STS_COMPLETED != job->status) { op->status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC; *n_fail = *n_fail + 1; } else op->status = VNET_CRYPTO_OP_STATUS_COMPLETED; } static_always_inline u32 ipsecmb_ops_cbc_cipher_inline (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops, u32 key_len, JOB_CIPHER_DIRECTION direction) { ipsecmb_main_t *imbm = &ipsecmb_main; ipsecmb_per_thread_data_t *ptd = vec_elt_at_index (imbm->per_thread_data, vm->thread_index); JOB_AES_HMAC *job; u32 i, n_fail = 0; for (i = 0; i < n_ops; i++) { ipsecmb_aes_cbc_key_data_t *kd; vnet_crypto_op_t *op = ops[i]; kd = (ipsecmb_aes_cbc_key_data_t *) imbm->key_data[op->key_index]; __m128i iv; job = IMB_GET_NEXT_JOB (ptd->mgr); job->src = op->src; job->dst = op->dst; job->msg_len_to_cipher_in_bytes = op->len; job->cipher_start_src_offset_in_bytes = 0; job->hash_alg = NULL_HASH; job->cipher_mode = CBC; job->cipher_direction = direction; job->chain_order = (direction == ENCRYPT ? CIPHER_HASH : HASH_CIPHER); if ((direction == ENCRYPT) && (op->flags & VNET_CRYPTO_OP_FLAG_INIT_IV)) { i
/*
 * Copyright (c) 2015 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
  Copyright (c) 2005 Eliot Dresselhaus

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files (the
  "Software"), to deal in the Software without restriction, including
  without limitation the rights to use, copy, modify, merge, publish,
  distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so, subject to
  the following conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#ifndef included_clib_unix_h
#define included_clib_unix_h

#include <vppinfra/error.h>

/* Number of bytes in a Unix file. */
clib_error_t *clib_file_n_bytes (char *file, uword * result);

/* Read file contents into given buffer. */
clib_error_t *clib_file_read_contents (char *file, u8 * result,
				       uword n_bytes);

/* Read and return contents of Unix file. */
clib_error_t *clib_file_contents (char *file, u8 ** result);

/* As above but for /proc file system on Linux. */
clib_error_t *unix_proc_file_contents (char *file, u8 ** result);

#endif /* included_clib_unix_h */

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
ock_qw; i++) pad[i] = key_hash[i] ^ 0x5c5c5c5c5c5c5c5c; ad->hash_one_block (pad, ((u8 *) kd) + (ad->data_size / 2)); return; } } static clib_error_t * crypto_ipsecmb_init (vlib_main_t * vm) { ipsecmb_main_t *imbm = &ipsecmb_main; ipsecmb_alg_data_t *ad; ipsecmb_per_thread_data_t *ptd; vlib_thread_main_t *tm = vlib_get_thread_main (); clib_error_t *error; MB_MGR *m = 0; u32 eidx; u8 *name; if (!clib_cpu_supports_aes ()) return 0; /* * A priority that is better than OpenSSL but worse than VPP natvie */ name = format (0, "Intel(R) Multi-Buffer Crypto for IPsec Library %s%c", IMB_VERSION_STR, 0); eidx = vnet_crypto_register_engine (vm, "ipsecmb", 80, (char *) name); vec_validate_aligned (imbm->per_thread_data, tm->n_vlib_mains - 1, CLIB_CACHE_LINE_BYTES); /* *INDENT-OFF* */ vec_foreach (ptd, imbm->per_thread_data) { ptd->mgr = alloc_mb_mgr (0); if (clib_cpu_supports_avx512f ()) init_mb_mgr_avx512 (ptd->mgr); else if (clib_cpu_supports_avx2 ()) init_mb_mgr_avx2 (ptd->mgr); else init_mb_mgr_sse (ptd->mgr); if (ptd == imbm->per_thread_data) m = ptd->mgr; } /* *INDENT-ON* */ if (clib_cpu_supports_x86_aes () && (error = crypto_ipsecmb_iv_init (imbm))) return (error); #define _(a, b, c, d, e, f) \ vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_HMAC, \ ipsecmb_ops_hmac_##a); \ ad = imbm->alg_data + VNET_CRYPTO_ALG_HMAC_##a; \ ad->block_size = d; \ ad->data_size = e * 2; \ ad->hash_one_block = m-> c##_one_block; \ ad->hash_fn = m-> c; \ foreach_ipsecmb_hmac_op; #undef _ #define _(a, b) \ vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_ENC, \ ipsecmb_ops_cbc_cipher_enc_##a); \ vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_DEC, \ ipsecmb_ops_cbc_cipher_dec_##a); \ ad = imbm->alg_data + VNET_CRYPTO_ALG_##a; \ ad->data_size = sizeof (ipsecmb_aes_cbc_key_data_t); \ ad->keyexp = m->keyexp_##b; \ foreach_ipsecmb_cbc_cipher_op; #undef _ #define _(a, b) \ vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_ENC, \ ipsecmb_ops_gcm_cipher_enc_##a); \ vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_DEC, \ ipsecmb_ops_gcm_cipher_dec_##a); \ vnet_crypto_register_chained_ops_handler \ (vm, eidx, VNET_CRYPTO_OP_##a##_ENC, \ ipsecmb_ops_gcm_cipher_enc_##a##_chained); \ vnet_crypto_register_chained_ops_handler \ (vm, eidx, VNET_CRYPTO_OP_##a##_DEC, \ ipsecmb_ops_gcm_cipher_dec_##a##_chained); \ ad = imbm->alg_data + VNET_CRYPTO_ALG_##a; \ ad->data_size = sizeof (struct gcm_key_data); \ ad->aes_gcm_pre = m->gcm##b##_pre; \ foreach_ipsecmb_gcm_cipher_op; #undef _ vnet_crypto_register_key_handler (vm, eidx, crypto_ipsecmb_key_handler); return (NULL); } /* *INDENT-OFF* */ VLIB_INIT_FUNCTION (crypto_ipsecmb_init) = { .runs_after = VLIB_INITS ("vnet_crypto_init"), }; /* *INDENT-ON* */ /* *INDENT-OFF* */ VLIB_PLUGIN_REGISTER () = { .version = VPP_BUILD_VER, .description = "Intel IPSEC Multi-buffer Crypto Engine", }; /* *INDENT-ON* */ /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */