static int aes_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key, const uint8_t *iv, int enc) { int ret, mode; EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; mode = ctx->cipher->flags & EVP_CIPH_MODE_MASK; if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE) && !enc) { if (bsaes_capable() && mode == EVP_CIPH_CBC_MODE) { ret = AES_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks); dat->block = (block128_f)AES_decrypt; dat->stream.cbc = (cbc128_f)bsaes_cbc_encrypt; } else if (vpaes_capable()) { ret = vpaes_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks); dat->block = (block128_f)vpaes_decrypt; dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? (cbc128_f)vpaes_cbc_encrypt : NULL; } else { ret = AES_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks); dat->block = (block128_f)AES_decrypt; dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? (cbc128_f)AES_cbc_encrypt : NULL; } } else if (bsaes_capable() && mode == EVP_CIPH_CTR_MODE) { ret = AES_set_encrypt_key(key, ctx->key_len * 8, &dat->ks.ks); dat->block = (block128_f)AES_encrypt; dat->stream.ctr = (ctr128_f)bsaes_ctr32_encrypt_blocks; } else if (vpaes_capable()) { ret = vpaes_set_encrypt_key(key, ctx->key_len * 8, &dat->ks.ks); dat->block = (block128_f)vpaes_encrypt; dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? (cbc128_f)vpaes_cbc_encrypt : NULL; } else { ret = AES_set_encrypt_key(key, ctx->key_len * 8, &dat->ks.ks); dat->block = (block128_f)AES_encrypt; dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? (cbc128_f)AES_cbc_encrypt : NULL; } if (ret < 0) { OPENSSL_PUT_ERROR(CIPHER, aes_init_key, CIPHER_R_AES_KEY_SETUP_FAILED); return 0; } return 1; }
ctr128_f aes_ctr_set_key(AES_KEY *aes_key, GCM128_CONTEXT *gcm_ctx, block128_f *out_block, const uint8_t *key, size_t key_bytes) { if (aesni_capable()) { aesni_set_encrypt_key(key, key_bytes * 8, aes_key); if (gcm_ctx != NULL) { CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)aesni_encrypt, 1); } if (out_block) { *out_block = (block128_f) aesni_encrypt; } return (ctr128_f)aesni_ctr32_encrypt_blocks; } if (hwaes_capable()) { aes_hw_set_encrypt_key(key, key_bytes * 8, aes_key); if (gcm_ctx != NULL) { CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)aes_hw_encrypt, 0); } if (out_block) { *out_block = (block128_f) aes_hw_encrypt; } return (ctr128_f)aes_hw_ctr32_encrypt_blocks; } if (bsaes_capable()) { AES_set_encrypt_key(key, key_bytes * 8, aes_key); if (gcm_ctx != NULL) { CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)AES_encrypt, 0); } if (out_block) { *out_block = (block128_f) AES_encrypt; } return (ctr128_f)bsaes_ctr32_encrypt_blocks; } if (vpaes_capable()) { vpaes_set_encrypt_key(key, key_bytes * 8, aes_key); if (out_block) { *out_block = (block128_f) vpaes_encrypt; } if (gcm_ctx != NULL) { CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)vpaes_encrypt, 0); } return NULL; } AES_set_encrypt_key(key, key_bytes * 8, aes_key); if (gcm_ctx != NULL) { CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)AES_encrypt, 0); } if (out_block) { *out_block = (block128_f) AES_encrypt; } return NULL; }
ctr128_f aes_ctr_set_key(AES_KEY *aes_key, GCM128_KEY *gcm_key, block128_f *out_block, const uint8_t *key, size_t key_bytes) { if (hwaes_capable()) { aes_hw_set_encrypt_key(key, key_bytes * 8, aes_key); if (gcm_key != NULL) { CRYPTO_gcm128_init_key(gcm_key, aes_key, aes_hw_encrypt, 1); } if (out_block) { *out_block = aes_hw_encrypt; } return aes_hw_ctr32_encrypt_blocks; } if (bsaes_capable()) { aes_nohw_set_encrypt_key(key, key_bytes * 8, aes_key); if (gcm_key != NULL) { CRYPTO_gcm128_init_key(gcm_key, aes_key, aes_nohw_encrypt, 0); } if (out_block) { *out_block = aes_nohw_encrypt; } return bsaes_ctr32_encrypt_blocks; } if (vpaes_capable()) { vpaes_set_encrypt_key(key, key_bytes * 8, aes_key); if (out_block) { *out_block = vpaes_encrypt; } if (gcm_key != NULL) { CRYPTO_gcm128_init_key(gcm_key, aes_key, vpaes_encrypt, 0); } #if defined(VPAES_CTR32) return vpaes_ctr32_encrypt_blocks; #else return NULL; #endif } aes_nohw_set_encrypt_key(key, key_bytes * 8, aes_key); if (gcm_key != NULL) { CRYPTO_gcm128_init_key(gcm_key, aes_key, aes_nohw_encrypt, 0); } if (out_block) { *out_block = aes_nohw_encrypt; } return NULL; }
static ctr128_f aes_gcm_set_key(AES_KEY *aes_key, GCM128_CONTEXT *gcm_ctx, const uint8_t *key, size_t key_len) { if (bsaes_capable()) { AES_set_encrypt_key(key, key_len * 8, aes_key); CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)AES_encrypt); return (ctr128_f)bsaes_ctr32_encrypt_blocks; } if (vpaes_capable()) { vpaes_set_encrypt_key(key, key_len * 8, aes_key); CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)vpaes_encrypt); return NULL; } AES_set_encrypt_key(key, key_len * 8, aes_key); CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)AES_encrypt); return NULL; }
static ctr128_f aes_ctr_set_key(AES_KEY *aes_key, GCM128_CONTEXT *gcm_ctx, block128_f *out_block, const uint8_t *key, size_t key_len) { #if defined(AESNI) if (aesni_capable()) { aesni_set_encrypt_key(key, key_len * 8, aes_key); if (gcm_ctx != NULL) { CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)aesni_encrypt); } if (out_block) { *out_block = (block128_f) aesni_encrypt; } return (ctr128_f)aesni_ctr32_encrypt_blocks; } #endif #if defined(HWAES) if (hwaes_capable()) { aes_v8_set_encrypt_key(key, key_len * 8, aes_key); if (gcm_ctx != NULL) { CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)aes_v8_encrypt); } if (out_block) { *out_block = (block128_f) aes_v8_encrypt; } return (ctr128_f)aes_v8_ctr32_encrypt_blocks; } #endif #if defined(BSAES) if (bsaes_capable()) { AES_set_encrypt_key(key, key_len * 8, aes_key); if (gcm_ctx != NULL) { CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)AES_encrypt); } if (out_block) { *out_block = (block128_f) AES_encrypt; } return (ctr128_f)bsaes_ctr32_encrypt_blocks; } #endif #if defined(VPAES) if (vpaes_capable()) { vpaes_set_encrypt_key(key, key_len * 8, aes_key); if (out_block) { *out_block = (block128_f) vpaes_encrypt; } if (gcm_ctx != NULL) { CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)vpaes_encrypt); } return NULL; } #endif AES_set_encrypt_key(key, key_len * 8, aes_key); if (gcm_ctx != NULL) { CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)AES_encrypt); } if (out_block) { *out_block = (block128_f) AES_encrypt; } return NULL; }