1 /*
2  * Copyright 2025 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 
10 /*
11  * This file uses the low level AES functions (which are deprecated for
12  * non-internal use) in order to implement provider AES ciphers.
13  */
14 #include "internal/deprecated.h"
15 
16 #include <openssl/proverr.h>
17 #include "cipher_aes.h"
18 #include "cipher_aes_cfb.h"
19 
cipher_hw_aes_initkey(PROV_CIPHER_CTX * dat,const unsigned char * key,size_t keylen)20 static int cipher_hw_aes_initkey(PROV_CIPHER_CTX *dat,
21                                  const unsigned char *key, size_t keylen)
22 {
23     int ret;
24     PROV_AES_CTX *adat = (PROV_AES_CTX *)dat;
25     AES_KEY *ks = &adat->ks.ks;
26 
27     dat->ks = ks;
28 
29 #ifdef HWAES_CAPABLE
30     if (HWAES_CAPABLE) {
31         ret = HWAES_set_encrypt_key(key, (int)(keylen * 8), ks);
32         dat->block = (block128_f)HWAES_encrypt;
33         dat->stream.cbc = NULL;
34     } else {
35 #endif
36 #ifdef VPAES_CAPABLE
37         if (VPAES_CAPABLE) {
38             ret = vpaes_set_encrypt_key(key, (int)(keylen * 8), ks);
39             dat->block = (block128_f)vpaes_encrypt;
40             dat->stream.cbc = NULL;
41         } else {
42 #endif
43             {
44                 ret = AES_set_encrypt_key(key, (int)(keylen * 8), ks);
45                 dat->block = (block128_f)AES_encrypt;
46                 dat->stream.cbc = NULL;
47             }
48 #ifdef VPAES_CAPABLE
49         }
50 #endif
51 #ifdef HWAES_CAPABLE
52     }
53 #endif
54 
55     if (ret < 0) {
56         ERR_raise(ERR_LIB_PROV, PROV_R_KEY_SETUP_FAILED);
57         return 0;
58     }
59 
60     return 1;
61 }
62 
63 IMPLEMENT_CIPHER_HW_COPYCTX(cipher_hw_aes_copyctx, PROV_AES_CTX)
64 
65 #define PROV_CIPHER_HW_aes_mode(mode)                                          \
66     static const PROV_CIPHER_HW aes_##mode = {                                 \
67         cipher_hw_aes_initkey,                                                 \
68         ossl_cipher_hw_generic_##mode,                                         \
69         cipher_hw_aes_copyctx                                                  \
70     };                                                                         \
71     PROV_CIPHER_HW_declare(mode)                                               \
72     const PROV_CIPHER_HW *ossl_prov_cipher_hw_aes_##mode(size_t keybits)       \
73     {                                                                          \
74         PROV_CIPHER_HW_select(mode)                                            \
75         return &aes_##mode;                                                    \
76     }
77 
78 #if defined(AESNI_CAPABLE)
79 # include "cipher_aes_cfb_hw_aesni.inc"
80 #elif defined(SPARC_AES_CAPABLE)
81 # include "cipher_aes_hw_t4.inc"
82 #elif defined(S390X_aes_128_CAPABLE)
83 # include "cipher_aes_cfb_hw_s390x.inc"
84 #elif defined(OPENSSL_CPUID_OBJ) && defined(__riscv) && __riscv_xlen == 64
85 # include "cipher_aes_hw_rv64i.inc"
86 #elif defined(OPENSSL_CPUID_OBJ) && defined(__riscv) && __riscv_xlen == 32
87 # include "cipher_aes_hw_rv32i.inc"
88 #elif defined(ARMv8_HWAES_CAPABLE)
89 # include "cipher_aes_hw_armv8.inc"
90 #else
91 /* The generic case */
92 # define PROV_CIPHER_HW_declare(mode)
93 # define PROV_CIPHER_HW_select(mode)
94 #endif
95 
96 PROV_CIPHER_HW_aes_mode(cfb128)
97 PROV_CIPHER_HW_aes_mode(cfb1)
98 PROV_CIPHER_HW_aes_mode(cfb8)
99