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