| // |
| 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 <assert.h> |
| 16 |
| 17 | #include "internal.h" |
| 18 | #include "../bcm_interface.h" |
| 19 |
| 20 |
| 21 | // Be aware that different sets of AES functions use incompatible key |
| 22 | // representations, varying in format of the key schedule, the |AES_KEY.rounds| |
| 23 | // value, or both. Therefore they cannot mix. Also, on AArch64, the plain-C |
| 24 | // code, above, is incompatible with the |aes_hw_*| functions. |
| 25 |
| 26 | bcm_infallible BCM_aes_encrypt(const uint8_t *in, uint8_t *out, |
| 27 | const AES_KEY *key) { |
| 28 | if (hwaes_capable()) { |
| 29 | aes_hw_encrypt(in, out, key); |
| 30 | } else if (vpaes_capable()) { |
| 31 | vpaes_encrypt(in, out, key); |
| 32 | } else { |
| 33 | aes_nohw_encrypt(in, out, key); |
| 34 | } |
| 35 | return bcm_infallible::not_approved; |
| 36 | } |
| 37 |
| 38 | bcm_infallible BCM_aes_decrypt(const uint8_t *in, uint8_t *out, |
| 39 | const AES_KEY *key) { |
| 40 | if (hwaes_capable()) { |
| 41 | aes_hw_decrypt(in, out, key); |
| 42 | } else if (vpaes_capable()) { |
| 43 | vpaes_decrypt(in, out, key); |
| 44 | } else { |
| 45 | aes_nohw_decrypt(in, out, key); |
| 46 | } |
| 47 | return bcm_infallible::not_approved; |
| 48 | } |
| 49 |
| 50 | bcm_status BCM_aes_set_encrypt_key(const uint8_t *key, unsigned bits, |
| 51 | AES_KEY *aeskey) { |
| 52 | int ret = -1; |
| 53 | if (hwaes_capable()) { |
| 54 | ret = aes_hw_set_encrypt_key(key, bits, aeskey); |
| 55 | } else if (vpaes_capable()) { |
| 56 | ret = vpaes_set_encrypt_key(key, bits, aeskey); |
| 57 | } else { |
| 58 | ret = aes_nohw_set_encrypt_key(key, bits, aeskey); |
| 59 | } |
| 60 | if (ret < 0) { |
| 61 | return bcm_status::failure; |
| 62 | } |
| 63 | BSSL_CHECK(ret == 0); |
| 64 | return bcm_status::not_approved; |
| 65 | } |
| 66 |
| 67 | bcm_status BCM_aes_set_decrypt_key(const uint8_t *key, unsigned bits, |
| 68 | AES_KEY *aeskey) { |
| 69 | int ret = -1; |
| 70 | if (hwaes_capable()) { |
| 71 | ret = aes_hw_set_decrypt_key(key, bits, aeskey); |
| 72 | } else if (vpaes_capable()) { |
| 73 | ret = vpaes_set_decrypt_key(key, bits, aeskey); |
| 74 | } else { |
| 75 | ret = aes_nohw_set_decrypt_key(key, bits, aeskey); |
| 76 | } |
| 77 | if (ret < 0) { |
| 78 | return bcm_status::failure; |
| 79 | } |
| 80 | BSSL_CHECK(ret == 0); |
| 81 | return bcm_status::not_approved; |
| 82 | } |
| 83 |
| 84 | #if defined(HWAES) && (defined(OPENSSL_X86) || defined(OPENSSL_X86_64)) |
| 85 | // On x86 and x86_64, |aes_hw_set_decrypt_key|, we implement |
| 86 | // |aes_hw_encrypt_key_to_decrypt_key| in assembly and rely on C code to combine |
| 87 | // the operations. |
| 88 | int aes_hw_set_decrypt_key(const uint8_t *user_key, int bits, AES_KEY *key) { |
| 89 | int ret = aes_hw_set_encrypt_key(user_key, bits, key); |
| 90 | if (ret == 0) { |
| 91 | aes_hw_encrypt_key_to_decrypt_key(key); |
| 92 | } |
| 93 | return ret; |
| 94 | } |
| 95 |
| 96 | int aes_hw_set_encrypt_key(const uint8_t *user_key, int bits, AES_KEY *key) { |
| 97 | if (aes_hw_set_encrypt_key_alt_preferred()) { |
| 98 | return aes_hw_set_encrypt_key_alt(user_key, bits, key); |
| 99 | } else { |
| 100 | return aes_hw_set_encrypt_key_base(user_key, bits, key); |
| 101 | } |
| 102 | } |
| 103 | #endif |
| 104 |
| 105 | #if defined(VPAES) && defined(OPENSSL_X86) |
| 106 | // On x86, there is no |vpaes_ctr32_encrypt_blocks|, so we implement it |
| 107 | // ourselves. This avoids all callers needing to account for a missing function. |
| 108 | void vpaes_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, size_t blocks, |
| 109 | const AES_KEY *key, const uint8_t iv[16]) { |
| 110 | uint32_t ctr = CRYPTO_load_u32_be(iv + 12); |
| 111 | uint8_t iv_buf[16], enc[16]; |
| 112 | OPENSSL_memcpy(iv_buf, iv, 12); |
| 113 | for (size_t i = 0; i < blocks; i++) { |
| 114 | CRYPTO_store_u32_be(iv_buf + 12, ctr); |
| 115 | vpaes_encrypt(iv_buf, enc, key); |
| 116 | CRYPTO_xor16(out, in, enc); |
| 117 | ctr++; |
| 118 | in += 16; |
| 119 | out += 16; |
| 120 | } |
| 121 | } |
| 122 | #endif |
| 123 |
| 124 | #if defined(BSAES) |
| 125 | void vpaes_ctr32_encrypt_blocks_with_bsaes(const uint8_t *in, uint8_t *out, |
| 126 | size_t blocks, const AES_KEY *key, |
| 127 | const uint8_t ivec[16]) { |
| 128 | // |bsaes_ctr32_encrypt_blocks| is faster than |vpaes_ctr32_encrypt_blocks|, |
| 129 | // but it takes at least one full 8-block batch to amortize the conversion. |
| 130 | if (blocks < 8) { |
| 131 | vpaes_ctr32_encrypt_blocks(in, out, blocks, key, ivec); |
| 132 | return; |
| 133 | } |
| 134 |
| 135 | size_t bsaes_blocks = blocks; |
| 136 | if (bsaes_blocks % 8 < 6) { |
| 137 | // |bsaes_ctr32_encrypt_blocks| internally works in 8-block batches. If the |
| 138 | // final batch is too small (under six blocks), it is faster to loop over |
| 139 | // |vpaes_encrypt|. Round |bsaes_blocks| down to a multiple of 8. |
| 140 | bsaes_blocks -= bsaes_blocks % 8; |
| 141 | } |
| 142 |
| 143 | AES_KEY bsaes; |
| 144 | vpaes_encrypt_key_to_bsaes(&bsaes, key); |
| 145 | bsaes_ctr32_encrypt_blocks(in, out, bsaes_blocks, &bsaes, ivec); |
| 146 | OPENSSL_cleanse(&bsaes, sizeof(bsaes)); |
| 147 |
| 148 | in += 16 * bsaes_blocks; |
| 149 | out += 16 * bsaes_blocks; |
| 150 | blocks -= bsaes_blocks; |
| 151 |
| 152 | uint8_t new_ivec[16]; |
| 153 | memcpy(new_ivec, ivec, 12); |
| 154 | uint32_t ctr = CRYPTO_load_u32_be(ivec + 12) + bsaes_blocks; |
| 155 | CRYPTO_store_u32_be(new_ivec + 12, ctr); |
| 156 |
| 157 | // Finish any remaining blocks with |vpaes_ctr32_encrypt_blocks|. |
| 158 | vpaes_ctr32_encrypt_blocks(in, out, blocks, key, new_ivec); |
| 159 | } |
| 160 | #endif // BSAES |
| 161 |
| 162 | ctr128_f aes_ctr_set_key(AES_KEY *aes_key, int *out_is_hwaes, |
| 163 | block128_f *out_block, const uint8_t *key, |
| 164 | size_t key_bytes) { |
| 165 | // This function assumes the key length was previously validated. |
| 166 | assert(key_bytes == 128 / 8 || key_bytes == 192 / 8 || key_bytes == 256 / 8); |
| 167 | if (hwaes_capable()) { |
| 168 | aes_hw_set_encrypt_key(key, (int)key_bytes * 8, aes_key); |
| 169 | if (out_is_hwaes) { |
| 170 | *out_is_hwaes = 1; |
| 171 | } |
| 172 | if (out_block) { |
| 173 | *out_block = aes_hw_encrypt; |
| 174 | } |
| 175 | return aes_hw_ctr32_encrypt_blocks; |
| 176 | } |
| 177 |
| 178 | if (vpaes_capable()) { |
| 179 | vpaes_set_encrypt_key(key, (int)key_bytes * 8, aes_key); |
| 180 | if (out_block) { |
| 181 | *out_block = vpaes_encrypt; |
| 182 | } |
| 183 | if (out_is_hwaes) { |
| 184 | *out_is_hwaes = 0; |
| 185 | } |
| 186 | #if defined(BSAES) |
| 187 | assert(bsaes_capable()); |
| 188 | return vpaes_ctr32_encrypt_blocks_with_bsaes; |
| 189 | #else |
| 190 | return vpaes_ctr32_encrypt_blocks; |
| 191 | #endif |
| 192 | } |
| 193 |
| 194 | aes_nohw_set_encrypt_key(key, (int)key_bytes * 8, aes_key); |
| 195 | if (out_is_hwaes) { |
| 196 | *out_is_hwaes = 0; |
| 197 | } |
| 198 | if (out_block) { |
| 199 | *out_block = aes_nohw_encrypt; |
| 200 | } |
| 201 | return aes_nohw_ctr32_encrypt_blocks; |
| 202 | } |
| 203 |