12e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan// Copyright (c) 2017, Google Inc. 22e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan// 32e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan// Permission to use, copy, modify, and/or distribute this software for any 42e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan// purpose with or without fee is hereby granted, provided that the above 52e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan// copyright notice and this permission notice appear in all copies. 62e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan// 72e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 82e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 92e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 102e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 112e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 122e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 132e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ 142e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan 152e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan#include <openssl/cipher.h> 162e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan 172e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan#include <string.h> 182e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan 192e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan#include <openssl/aes.h> 202e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan#include <openssl/obj.h> 212e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan 222e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan#include "../../crypto/internal.h" 232e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan 242e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloantypedef struct { 252e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan AES_KEY ks; 262e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan} EVP_CFB_CTX; 272e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan 282e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloanstatic int aes_cfb_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key, 292e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan const uint8_t *iv, int enc) { 302e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan if (key) { 312e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan EVP_CFB_CTX *cfb_ctx = ctx->cipher_data; 322e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan AES_set_encrypt_key(key, ctx->key_len * 8, &cfb_ctx->ks); 332e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan } 342e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan 352e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan return 1; 362e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan} 372e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan 382e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloanstatic int aes_cfb128_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, 392e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan const uint8_t *in, size_t len) { 402e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan if (!out || !in) { 412e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan return 0; 422e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan } 432e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan 442e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan EVP_CFB_CTX *cfb_ctx = ctx->cipher_data; 452e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan int num = ctx->num; 462e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan AES_cfb128_encrypt(in, out, len, &cfb_ctx->ks, ctx->iv, &num, 472e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan ctx->encrypt ? AES_ENCRYPT : AES_DECRYPT); 482e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan ctx->num = num; 492e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan 502e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan return 1; 512e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan} 522e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan 532e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloanstatic const EVP_CIPHER aes_128_cfb128 = { 542e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan NID_aes_128_cfb128, 1 /* block_size */, 16 /* key_size */, 552e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan 16 /* iv_len */, sizeof(EVP_CFB_CTX), EVP_CIPH_CFB_MODE, 562e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan NULL /* app_data */, aes_cfb_init_key, aes_cfb128_cipher, 572e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan NULL /* cleanup */, NULL /* ctrl */, 582e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan}; 592e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloan 602e9e66ad8f35ee615e79da22ff91b0fa94607ca8Robert Sloanconst EVP_CIPHER *EVP_aes_128_cfb128(void) { return &aes_128_cfb128; } 61