1 // Copyright 2021 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://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 /////////////////////////////////////////////////////////////////////////////// 16 #ifndef TINK_INTERNAL_AES_UTIL_H_ 17 #define TINK_INTERNAL_AES_UTIL_H_ 18 19 #include <cstdint> 20 #include <memory> 21 22 #include "absl/strings/string_view.h" 23 #include "absl/types/span.h" 24 #include "openssl/aes.h" 25 #include "tink/util/secret_data.h" 26 #include "tink/util/status.h" 27 #include "tink/util/statusor.h" 28 29 namespace crypto { 30 namespace tink { 31 namespace internal { 32 33 // AES block size in bytes. AesBlockSize()34constexpr int AesBlockSize() { return 16; } 35 36 // Wrapper for BoringSSL/OpenSSL low-level functions that encrypt/decrypt (same 37 // operation in CTR mode) `data`, with IV `iv` and key `key`. The result is 38 // written to `out`. `out` may fully overlap with `data`; partial overlaps will 39 // result in an kInvalidArgument error. `iv` is incremented of the number of 40 // blocks that were encrypted/decrypted. 41 crypto::tink::util::Status AesCtr128Crypt(absl::string_view data, 42 uint8_t iv[AesBlockSize()], 43 const AES_KEY* key, 44 absl::Span<char> out); 45 46 // Returns a pointer to an AES-CTR EVP_CIPHER for the given key size 47 // `key_size_in_bytes`. 48 util::StatusOr<const EVP_CIPHER*> GetAesCtrCipherForKeySize( 49 uint32_t key_size_in_bytes); 50 51 // Returns a pointer to an AES-CBC EVP_CIPHER for the given key size 52 // `key_size_in_bytes`. 53 util::StatusOr<const EVP_CIPHER*> GetAesCbcCipherForKeySize( 54 uint32_t key_size_in_bytes); 55 56 } // namespace internal 57 } // namespace tink 58 } // namespace crypto 59 60 #endif // TINK_INTERNAL_AES_UTIL_H_ 61