Mercurial > hg > orthanc-object-storage
annotate Common/EncryptionHelpers.h @ 203:55fab84a0674
back to mainline
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 26 Jun 2024 13:16:28 +0200 |
parents | 6dd8bb916573 |
children |
rev | line source |
---|---|
1 | 1 /** |
2 * Cloud storage plugins for Orthanc | |
145
3c7e0374f28e
updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
3 * Copyright (C) 2020-2023 Osimis S.A., Belgium |
3c7e0374f28e
updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
4 * Copyright (C) 2024-2024 Orthanc Team SRL, Belgium |
3c7e0374f28e
updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
5 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
1 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU Affero General Public License | |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
56
b922ae86bbe1
full static linking against AWS SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
37
diff
changeset
|
20 |
b922ae86bbe1
full static linking against AWS SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
37
diff
changeset
|
21 |
1 | 22 #pragma once |
23 | |
24 #include <memory.h> | |
25 #include <cryptopp/secblock.h> | |
30
662b9d3f217d
fix missing definition of "byte" from CryptoPP
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
26 #include <cryptopp/osrng.h> |
1 | 27 #include <boost/thread/mutex.hpp> |
15 | 28 #include <MultiThreading/Semaphore.h> |
1 | 29 |
30 class EncryptionException : public std::runtime_error | |
31 { | |
32 public: | |
153 | 33 explicit EncryptionException(const std::string& what) |
1 | 34 : std::runtime_error(what) |
35 { | |
36 } | |
37 }; | |
38 | |
39 class EncryptionHelpers | |
40 { | |
41 public: | |
42 static const size_t HEADER_VERSION_SIZE = 2; | |
43 static const size_t MASTER_KEY_ID_SIZE = 4; | |
44 static const size_t AES_KEY_SIZE = 32; // length of AES keys (in bytes) | |
45 static const size_t IV_SIZE = 32; // length of IVs (in bytes) | |
46 static const size_t INTEGRITY_CHECK_TAG_SIZE = 16; // length of the TAG that is used to check the integrity of data (in bytes) | |
47 | |
48 static const size_t OVERHEAD_SIZE = HEADER_VERSION_SIZE + MASTER_KEY_ID_SIZE + AES_KEY_SIZE + IV_SIZE + INTEGRITY_CHECK_TAG_SIZE; | |
49 | |
50 | |
51 static const std::string HEADER_VERSION; | |
52 | |
53 private: | |
54 Orthanc::Semaphore concurrentInputSizeSemaphore_; | |
55 size_t maxConcurrentInputSize_; | |
56 | |
57 CryptoPP::AutoSeededRandomPool randomGenerator_; | |
58 | |
59 CryptoPP::SecByteBlock encryptionMasterKey_; // at a given time, there's only one master key that is used for encryption | |
60 std::string encryptionMasterKeyId_; | |
61 | |
62 std::map<std::string, CryptoPP::SecByteBlock> previousMasterKeys_; // for decryption, we might use older master keys too | |
63 | |
64 public: | |
65 | |
66 // since the memory used during encryption/decryption can grow up to a bit more than 2 times the input, | |
67 // we want to limit the number of threads doing concurrent processing according to the available memory | |
68 // instead of the number of concurrent threads | |
69 EncryptionHelpers(size_t maxConcurrentInputSize = 1024*1024*1024); | |
70 | |
71 void SetCurrentMasterKey(uint32_t id, const std::string& path); | |
72 | |
73 void SetCurrentMasterKey(uint32_t id, const CryptoPP::SecByteBlock& key); | |
74 | |
75 void AddPreviousMasterKey(uint32_t id, const std::string& path); | |
76 | |
77 void AddPreviousMasterKey(uint32_t id, const CryptoPP::SecByteBlock& key); | |
78 | |
79 // input: plain text data | |
80 // output: prefix/encrypted data/integrity check tag | |
81 void Encrypt(std::string& output, const std::string& input); | |
82 void Encrypt(std::string& output, const char* data, size_t size); | |
83 | |
84 // input: prefix/encrypted data/integrity check tag | |
85 // output: plain text data | |
86 void Decrypt(std::string& output, const std::string& input); | |
87 void Decrypt(char* output, const char* data, size_t size); | |
88 | |
89 static void GenerateKey(CryptoPP::SecByteBlock& key); | |
90 | |
91 private: | |
92 | |
93 void EncryptInternal(std::string& output, const char* data, size_t size, const CryptoPP::SecByteBlock& masterKey); | |
94 | |
95 void DecryptInternal(char* output, const char* data, size_t size, const CryptoPP::SecByteBlock& masterKey); | |
96 | |
97 void EncryptPrefixSecBlock(std::string& output, const CryptoPP::SecByteBlock& input, const CryptoPP::SecByteBlock& masterKey); | |
98 | |
99 void DecryptPrefixSecBlock(CryptoPP::SecByteBlock& output, const std::string& input, const CryptoPP::SecByteBlock& masterKey); | |
100 | |
101 std::string GetMasterKeyIdentifier(const CryptoPP::SecByteBlock& masterKey); | |
102 | |
103 const CryptoPP::SecByteBlock& GetMasterKey(const std::string& keyId); | |
104 | |
105 public: | |
106 | |
30
662b9d3f217d
fix missing definition of "byte" from CryptoPP
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
107 static std::string ToHexString(const void* block, size_t size); |
1 | 108 static std::string ToHexString(const std::string& block); |
109 static std::string ToHexString(const CryptoPP::SecByteBlock& block); | |
110 static std::string ToString(const CryptoPP::SecByteBlock& block); | |
111 static std::string ToString(uint32_t value); | |
112 | |
113 static void ReadKey(CryptoPP::SecByteBlock& key, const std::string& path); | |
114 //static void EncryptionHelpers::Encrypt(std::string& output, const std::string& input, const std::string& key, const std::string& iv); | |
115 }; | |
116 |