1 /*************************************************************************
3 * Copyright 2016 Realm Inc.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 **************************************************************************/
21 #include <realm/util/features.h>
24 #include <realm/util/file.hpp>
26 #if REALM_ENABLE_ENCRYPTION
28 #if REALM_PLATFORM_APPLE
29 #include <CommonCrypto/CommonCrypto.h>
34 #pragma comment(lib, "bcrypt.lib")
36 #include <openssl/aes.h>
37 #include <openssl/sha.h>
44 class EncryptedFileMapping;
48 AESCryptor(const uint8_t* key);
49 ~AESCryptor() noexcept;
51 void set_file_size(off_t new_size);
53 bool read(FileDesc fd, off_t pos, char* dst, size_t size);
54 void write(FileDesc fd, off_t pos, const char* src, size_t size) noexcept;
58 #if REALM_PLATFORM_APPLE
59 mode_Encrypt = kCCEncrypt,
60 mode_Decrypt = kCCDecrypt
65 mode_Encrypt = AES_ENCRYPT,
66 mode_Decrypt = AES_DECRYPT
70 #if REALM_PLATFORM_APPLE
74 BCRYPT_KEY_HANDLE m_aes_key_handle;
80 uint8_t m_hmacKey[32];
81 std::vector<iv_table> m_iv_buffer;
82 std::unique_ptr<char[]> m_rw_buffer;
83 std::unique_ptr<char[]> m_dst_buffer;
85 void calc_hmac(const void* src, size_t len, uint8_t* dst, const uint8_t* key) const;
86 bool check_hmac(const void* data, size_t len, const uint8_t* hmac) const;
87 void crypt(EncryptionMode mode, off_t pos, char* dst, const char* src, const char* stored_iv) noexcept;
88 iv_table& get_iv_table(FileDesc fd, off_t data_pos) noexcept;
91 struct SharedFileInfo {
94 std::vector<EncryptedFileMapping*> mappings;
96 SharedFileInfo(const uint8_t* key, FileDesc file_descriptor);
101 #endif // REALM_ENABLE_ENCRYPTION