| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 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 |
| 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 = reinterpret_cast<EVP_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 |
| 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 = reinterpret_cast<EVP_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= */ NID_aes_128_cfb128, |
| 56 | /* block_size= */ 1, |
| 57 | /* key_len= */ 16, |
| 58 | /* iv_len= */ 16, |
| 59 | /* ctx_size= */ sizeof(EVP_CFB_CTX), |
| 60 | /* flags= */ EVP_CIPH_CFB_MODE, |
| 61 | /* init= */ aes_cfb_init_key, |
| 62 | /* cipher= */ aes_cfb128_cipher, |
| 63 | /* cleanup= */ nullptr, |
| 64 | /* ctrl= */ nullptr, |
| 65 | }; |
| 66 |
| 67 | static const EVP_CIPHER aes_192_cfb128 = { |
| 68 | /* nid= */ NID_aes_192_cfb128, |
| 69 | /* block_size= */ 1, |
| 70 | /* key_len= */ 24, |
| 71 | /* iv_len= */ 16, |
| 72 | /* ctx_size= */ sizeof(EVP_CFB_CTX), |
| 73 | /* flags= */ EVP_CIPH_CFB_MODE, |
| 74 | /* init= */ aes_cfb_init_key, |
| 75 | /* cipher= */ aes_cfb128_cipher, |
| 76 | /* cleanup= */ nullptr, |
| 77 | /* ctrl= */ nullptr, |
| 78 | }; |
| 79 |
| 80 | static const EVP_CIPHER aes_256_cfb128 = { |
| 81 | /* nid= */ NID_aes_256_cfb128, |
| 82 | /* block_size= */ 1, |
| 83 | /* key_len= */ 32, |
| 84 | /* iv_len= */ 16, |
| 85 | /* ctx_size= */ sizeof(EVP_CFB_CTX), |
| 86 | /* flags= */ EVP_CIPH_CFB_MODE, |
| 87 | /* init= */ aes_cfb_init_key, |
| 88 | /* cipher= */ aes_cfb128_cipher, |
| 89 | /* cleanup= */ nullptr, |
| 90 | /* ctrl= */ nullptr, |
| 91 | }; |
| 92 |
| 93 | const EVP_CIPHER *EVP_aes_128_cfb128(void) { return &aes_128_cfb128; } |
| 94 | const EVP_CIPHER *EVP_aes_128_cfb(void) { return &aes_128_cfb128; } |
| 95 | const EVP_CIPHER *EVP_aes_192_cfb128(void) { return &aes_192_cfb128; } |
| 96 | const EVP_CIPHER *EVP_aes_192_cfb(void) { return &aes_192_cfb128; } |
| 97 | const EVP_CIPHER *EVP_aes_256_cfb128(void) { return &aes_256_cfb128; } |
| 98 | const EVP_CIPHER *EVP_aes_256_cfb(void) { return &aes_256_cfb128; } |
| 99 |