void cipher_des_encrypt_ecb(const unsigned char key[DES_KEY_LENGTH], unsigned char *src, unsigned char *dst) { mbedtls_des_context ctx; ASSERT(mbed_ok(mbedtls_des_setkey_enc(&ctx, key))); ASSERT(mbed_ok(mbedtls_des_crypt_ecb(&ctx, src, dst))); }
int cipher_ctx_reset(mbedtls_cipher_context_t *ctx, const uint8_t *iv_buf) { if (!mbed_ok(mbedtls_cipher_reset(ctx))) { return 0; } if (!mbed_ok(mbedtls_cipher_set_iv(ctx, iv_buf, ctx->cipher_info->iv_size))) { return 0; } return 1; }
int pkcs11_init_tls_session(pkcs11h_certificate_t certificate, struct tls_root_ctx * const ssl_ctx) { int ret = 1; ASSERT (NULL != ssl_ctx); ALLOC_OBJ_CLEAR (ssl_ctx->crt_chain, mbedtls_x509_crt); if (mbedtls_pkcs11_x509_cert_bind(ssl_ctx->crt_chain, certificate)) { msg (M_FATAL, "PKCS#11: Cannot retrieve mbed TLS certificate object"); goto cleanup; } ALLOC_OBJ_CLEAR (ssl_ctx->priv_key_pkcs11, mbedtls_pkcs11_context); if (mbedtls_pkcs11_priv_key_bind(ssl_ctx->priv_key_pkcs11, certificate)) { msg (M_FATAL, "PKCS#11: Cannot initialize mbed TLS private key object"); goto cleanup; } ALLOC_OBJ_CLEAR (ssl_ctx->priv_key, mbedtls_pk_context); if (!mbed_ok(mbedtls_pk_setup_rsa_alt(ssl_ctx->priv_key, ssl_ctx->priv_key_pkcs11, mbedtls_ssl_pkcs11_decrypt, mbedtls_ssl_pkcs11_sign, mbedtls_ssl_pkcs11_key_len))) { goto cleanup; } ret = 0; cleanup: return ret; }
static bool pkcs11_get_x509_cert(pkcs11h_certificate_t pkcs11_cert, mbedtls_x509_crt *cert) { unsigned char *cert_blob = NULL; size_t cert_blob_size = 0; bool ret = false; if (pkcs11h_certificate_getCertificateBlob(pkcs11_cert, NULL, &cert_blob_size) != CKR_OK) { msg(M_WARN, "PKCS#11: Cannot retrieve certificate object size"); goto cleanup; } check_malloc_return((cert_blob = calloc(1, cert_blob_size))); if (pkcs11h_certificate_getCertificateBlob(pkcs11_cert, cert_blob, &cert_blob_size) != CKR_OK) { msg(M_WARN, "PKCS#11: Cannot retrieve certificate object"); goto cleanup; } if (!mbed_ok(mbedtls_x509_crt_parse(cert, cert_blob, cert_blob_size))) { msg(M_WARN, "PKCS#11: Could not parse certificate"); goto cleanup; } ret = true; cleanup: free(cert_blob); return ret; }
/* * Initialise the given ctr_drbg context, using a personalisation string and an * entropy gathering function. */ mbedtls_ctr_drbg_context * rand_ctx_get() { static mbedtls_entropy_context ec = {0}; static mbedtls_ctr_drbg_context cd_ctx = {0}; static bool rand_initialised = false; if (!rand_initialised) { struct gc_arena gc = gc_new(); struct buffer pers_string = alloc_buf_gc(100, &gc); /* * Personalisation string, should be as unique as possible (see NIST * 800-90 section 8.7.1). We have very little information at this stage. * Include Program Name, memory address of the context and PID. */ buf_printf(&pers_string, "OpenVPN %0u %p %s", platform_getpid(), &cd_ctx, time_string(0, 0, 0, &gc)); /* Initialise mbed TLS RNG, and built-in entropy sources */ mbedtls_entropy_init(&ec); mbedtls_ctr_drbg_init(&cd_ctx); if (!mbed_ok(mbedtls_ctr_drbg_seed(&cd_ctx, mbedtls_entropy_func, &ec, BPTR(&pers_string), BLEN(&pers_string)))) { msg(M_FATAL, "Failed to initialize random generator"); } gc_free(&gc); rand_initialised = true; } return &cd_ctx; }
void cipher_ctx_init(mbedtls_cipher_context_t *ctx, const uint8_t *key, int key_len, const mbedtls_cipher_info_t *kt, const mbedtls_operation_t operation) { ASSERT(NULL != kt && NULL != ctx); CLEAR(*ctx); if (!mbed_ok(mbedtls_cipher_setup(ctx, kt))) { msg(M_FATAL, "mbed TLS cipher context init #1"); } if (!mbed_ok(mbedtls_cipher_setkey(ctx, key, key_len*8, operation))) { msg(M_FATAL, "mbed TLS cipher set key"); } /* make sure we used a big enough key */ ASSERT(ctx->key_bitlen <= key_len*8); }
int cipher_ctx_final_check_tag(mbedtls_cipher_context_t *ctx, uint8_t *dst, int *dst_len, uint8_t *tag, size_t tag_len) { #ifdef HAVE_AEAD_CIPHER_MODES size_t olen = 0; if (MBEDTLS_DECRYPT != ctx->operation) { return 0; } if (tag_len > SIZE_MAX) { return 0; } if (!mbed_ok(mbedtls_cipher_finish(ctx, dst, &olen))) { msg(D_CRYPT_ERRORS, "%s: cipher_ctx_final() failed", __func__); return 0; } if (olen > INT_MAX) { return 0; } *dst_len = olen; if (!mbed_ok(mbedtls_cipher_check_tag(ctx, (const unsigned char *) tag, tag_len))) { return 0; } return 1; #else /* ifdef HAVE_AEAD_CIPHER_MODES */ ASSERT(0); #endif /* HAVE_AEAD_CIPHER_MODES */ }
int cipher_ctx_final(mbedtls_cipher_context_t *ctx, uint8_t *dst, int *dst_len) { size_t s_dst_len = *dst_len; if (!mbed_ok(mbedtls_cipher_finish(ctx, dst, &s_dst_len))) { return 0; } *dst_len = s_dst_len; return 1; }
int cipher_ctx_update(mbedtls_cipher_context_t *ctx, uint8_t *dst, int *dst_len, uint8_t *src, int src_len) { size_t s_dst_len = *dst_len; if (!mbed_ok(mbedtls_cipher_update(ctx, src, (size_t) src_len, dst, &s_dst_len))) { return 0; } *dst_len = s_dst_len; return 1; }
int cipher_ctx_update_ad(cipher_ctx_t *ctx, const uint8_t *src, int src_len) { #ifdef HAVE_AEAD_CIPHER_MODES if (src_len > SIZE_MAX) { return 0; } if (!mbed_ok(mbedtls_cipher_update_ad(ctx, src, src_len))) { return 0; } return 1; #else /* ifdef HAVE_AEAD_CIPHER_MODES */ ASSERT(0); #endif /* HAVE_AEAD_CIPHER_MODES */ }
int cipher_ctx_get_tag(cipher_ctx_t *ctx, uint8_t *tag, int tag_len) { #ifdef HAVE_AEAD_CIPHER_MODES if (tag_len > SIZE_MAX) { return 0; } if (!mbed_ok(mbedtls_cipher_write_tag(ctx, (unsigned char *) tag, tag_len))) { return 0; } return 1; #else /* ifdef HAVE_AEAD_CIPHER_MODES */ ASSERT(0); #endif /* HAVE_AEAD_CIPHER_MODES */ }