added iOS source code
[wl-app.git] / iOS / Pods / Realm / include / core / realm / util / aes_cryptor.hpp
1 /*************************************************************************
2  *
3  * Copyright 2016 Realm Inc.
4  *
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
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  **************************************************************************/
18
19 #include <cstddef>
20 #include <memory>
21 #include <realm/util/features.h>
22 #include <cstdint>
23 #include <vector>
24 #include <realm/util/file.hpp>
25
26 #if REALM_ENABLE_ENCRYPTION
27
28 #if REALM_PLATFORM_APPLE
29 #include <CommonCrypto/CommonCrypto.h>
30 #elif defined(_WIN32)
31 #include <windows.h>
32 #include <stdio.h>
33 #include <bcrypt.h>
34 #pragma comment(lib, "bcrypt.lib")
35 #else
36 #include <openssl/aes.h>
37 #include <openssl/sha.h>
38 #endif
39
40 namespace realm {
41 namespace util {
42
43 struct iv_table;
44 class EncryptedFileMapping;
45
46 class AESCryptor {
47 public:
48     AESCryptor(const uint8_t* key);
49     ~AESCryptor() noexcept;
50
51     void set_file_size(off_t new_size);
52
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;
55
56 private:
57     enum EncryptionMode {
58 #if REALM_PLATFORM_APPLE
59         mode_Encrypt = kCCEncrypt,
60         mode_Decrypt = kCCDecrypt
61 #elif defined(_WIN32)
62         mode_Encrypt = 0,
63         mode_Decrypt = 1
64 #else
65         mode_Encrypt = AES_ENCRYPT,
66         mode_Decrypt = AES_DECRYPT
67 #endif
68     };
69
70 #if REALM_PLATFORM_APPLE
71     CCCryptorRef m_encr;
72     CCCryptorRef m_decr;
73 #elif defined(_WIN32)
74     BCRYPT_KEY_HANDLE m_aes_key_handle;
75 #else
76     AES_KEY m_ectx;
77     AES_KEY m_dctx;
78 #endif
79
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;
84
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;
89 };
90
91 struct SharedFileInfo {
92     FileDesc fd;
93     AESCryptor cryptor;
94     std::vector<EncryptedFileMapping*> mappings;
95
96     SharedFileInfo(const uint8_t* key, FileDesc file_descriptor);
97 };
98 }
99 }
100
101 #endif // REALM_ENABLE_ENCRYPTION