1 // Copyright (c) 2017, Google Inc.
2 //
3 // Permission to use, copy, modify, and/or distribute this software for any
4 // purpose with or without fee is hereby granted, provided that the above
5 // copyright notice and this permission notice appear in all copies.
6 //
7 // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 // SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
15 #include <openssl/cipher.h>
16
17 #include <string.h>
18
19 #include <openssl/aes.h>
20 #include <openssl/obj.h>
21
22 #include "../../crypto/fipsmodule/cipher/internal.h"
23 #include "../../crypto/internal.h"
24
25 typedef struct {
26 AES_KEY ks;
27 } EVP_CFB_CTX;
28
aes_cfb_init_key(EVP_CIPHER_CTX * ctx,const uint8_t * key,const uint8_t * iv,int enc)29 static int aes_cfb_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key,
30 const uint8_t *iv, int enc) {
31 if (key) {
32 EVP_CFB_CTX *cfb_ctx = ctx->cipher_data;
33 AES_set_encrypt_key(key, ctx->key_len * 8, &cfb_ctx->ks);
34 }
35
36 return 1;
37 }
38
aes_cfb128_cipher(EVP_CIPHER_CTX * ctx,uint8_t * out,const uint8_t * in,size_t len)39 static int aes_cfb128_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out,
40 const uint8_t *in, size_t len) {
41 if (!out || !in) {
42 return 0;
43 }
44
45 EVP_CFB_CTX *cfb_ctx = ctx->cipher_data;
46 int num = ctx->num;
47 AES_cfb128_encrypt(in, out, len, &cfb_ctx->ks, ctx->iv, &num,
48 ctx->encrypt ? AES_ENCRYPT : AES_DECRYPT);
49 ctx->num = num;
50
51 return 1;
52 }
53
54 static const EVP_CIPHER aes_128_cfb128 = {
55 NID_aes_128_cfb128, 1 /* block_size */, 16 /* key_size */,
56 16 /* iv_len */, sizeof(EVP_CFB_CTX), EVP_CIPH_CFB_MODE,
57 NULL /* app_data */, aes_cfb_init_key, aes_cfb128_cipher,
58 NULL /* cleanup */, NULL /* ctrl */,
59 };
60
61 static const EVP_CIPHER aes_192_cfb128 = {
62 NID_aes_192_cfb128, 1 /* block_size */, 24 /* key_size */,
63 16 /* iv_len */, sizeof(EVP_CFB_CTX), EVP_CIPH_CFB_MODE,
64 NULL /* app_data */, aes_cfb_init_key, aes_cfb128_cipher,
65 NULL /* cleanup */, NULL /* ctrl */,
66 };
67
68 static const EVP_CIPHER aes_256_cfb128 = {
69 NID_aes_256_cfb128, 1 /* block_size */, 32 /* key_size */,
70 16 /* iv_len */, sizeof(EVP_CFB_CTX), EVP_CIPH_CFB_MODE,
71 NULL /* app_data */, aes_cfb_init_key, aes_cfb128_cipher,
72 NULL /* cleanup */, NULL /* ctrl */,
73 };
74
EVP_aes_128_cfb128(void)75 const EVP_CIPHER *EVP_aes_128_cfb128(void) { return &aes_128_cfb128; }
EVP_aes_128_cfb(void)76 const EVP_CIPHER *EVP_aes_128_cfb(void) { return &aes_128_cfb128; }
EVP_aes_192_cfb128(void)77 const EVP_CIPHER *EVP_aes_192_cfb128(void) { return &aes_192_cfb128; }
EVP_aes_192_cfb(void)78 const EVP_CIPHER *EVP_aes_192_cfb(void) { return &aes_192_cfb128; }
EVP_aes_256_cfb128(void)79 const EVP_CIPHER *EVP_aes_256_cfb128(void) { return &aes_256_cfb128; }
EVP_aes_256_cfb(void)80 const EVP_CIPHER *EVP_aes_256_cfb(void) { return &aes_256_cfb128; }
81