SeComLib
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Macros Pages
crypto_provider.h
Go to the documentation of this file.
1 /*
2 SeComLib
3 Copyright 2012-2013 TU Delft, Information Security & Privacy Lab (http://isplab.tudelft.nl/)
4 
5 Contributors:
6 Inald Lagendijk (R.L.Lagendijk@TUDelft.nl)
7 Mihai Todor (todormihai@gmail.com)
8 Thijs Veugen (P.J.M.Veugen@tudelft.nl)
9 Zekeriya Erkin (z.erkin@tudelft.nl)
10 
11 Licensed under the Apache License, Version 2.0 (the "License");
12 you may not use this file except in compliance with the License.
13 You may obtain a copy of the License at
14 
15 http://www.apache.org/licenses/LICENSE-2.0
16 
17 Unless required by applicable law or agreed to in writing, software
18 distributed under the License is distributed on an "AS IS" BASIS,
19 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 See the License for the specific language governing permissions and
21 limitations under the License.
22 */
29 #ifndef CRYPTO_PROVIDER_HEADER_GUARD
30 #define CRYPTO_PROVIDER_HEADER_GUARD
31 
32 #include "big_integer.h"
33 #include "ciphertext_base.h"
35 #include "randomizer_container.h"
36 #include "randomizer_base.h"
37 #include "randomizer_cache.h"
38 
39 //include C++ headers
40 #include <memory>
41 #include <stdexcept>
42 
43 namespace SeComLib {
44 namespace Core {
45  //uncomment this to enable homomorphic operations on ciphertexts via crypto providers
46  //#define ENABLE_CRYPTO_PROVIDER_HOMOMORPHIC_OPERATIONS
47 
56  template <typename T_PublicKey, typename T_PrivateKey, typename T_Ciphertext, typename T_Randomizer>
58  public:
60  typedef T_Ciphertext Ciphertext;
61 
63  typedef T_Randomizer Randomizer;
64 
66  CryptoProvider (const unsigned int keyLength);
67 
69  CryptoProvider (const T_PublicKey &publicKey, const unsigned int keyLength);
70 
72  CryptoProvider (const T_PublicKey &publicKey, const T_PrivateKey &privateKey, const unsigned int keyLength);
73 
75  virtual ~CryptoProvider () {}
76 
78  virtual bool GenerateKeys () = 0;
79 
81  virtual T_Ciphertext EncryptInteger (const BigInteger &plaintext) const;
82 
84  virtual BigInteger DecryptInteger (const T_Ciphertext &ciphertext) const = 0;
85 
87  virtual T_Ciphertext EncryptIntegerNonrandom (const BigInteger &plaintext) const = 0;
88 
90  virtual T_Randomizer GetRandomizer () const = 0;
91 
93  virtual T_Ciphertext RandomizeCiphertext (const T_Ciphertext &ciphertext) const = 0;
94 
95  #ifdef ENABLE_CRYPTO_PROVIDER_HOMOMORPHIC_OPERATIONS
96  T_Ciphertext HomomorphicAdd (const T_Ciphertext &lhs, const T_Ciphertext &rhs) const;
98 
100  T_Ciphertext GetHomomorphicInverse (const T_Ciphertext &input) const;
101 
103  T_Ciphertext HomomorphicSubtract (const T_Ciphertext &lhs, const T_Ciphertext &rhs) const;
104 
106  T_Ciphertext HomomorphicMultiply (const T_Ciphertext &lhs, const BigInteger &rhs) const;
107  #endif
108 
110  const BigInteger &GetEncryptionModulus () const;
111 
113  virtual const BigInteger &GetMessageSpaceUpperBound () const = 0;
114 
116  virtual const BigInteger &GetPositiveNegativeBoundary () const;
117 
119  virtual size_t GetMessageSpaceSize () const = 0;
120 
122  const T_PublicKey &GetPublicKey () const;
123 
125  const T_PrivateKey &GetPrivateKey () const;
126 
128  Ciphertext GetEncryptedZero (const bool randomized = true) const;
129 
131  Ciphertext GetEncryptedOne (const bool randomized = true) const;
132 
133  protected:
136 
138  std::unique_ptr<RandomizerCacheType> randomizerCache;
139 
141  T_PublicKey publicKey;
142 
144  T_PrivateKey privateKey;
145 
147  unsigned int keyLength;
148 
150  std::shared_ptr<BigInteger> encryptionModulus;
151 
154 
157 
160 
162  T_Ciphertext encryptedZero;
163 
165  T_Ciphertext encryptedOne;
166 
168  virtual void validateParameters () = 0;
169 
171  virtual void doPrecomputations () = 0;
172 
173  private:
175  //CryptoProvider (const CryptoProvider<T_PublicKey, T_PrivateKey, T_Ciphertext> &);//need C++11 delete to disable this
176 
178  //CryptoProvider<T_PublicKey, T_PrivateKey, T_Ciphertext> operator= (const CryptoProvider<T_PublicKey, T_PrivateKey, T_Ciphertext> &);//need C++11 delete to disable this
179  };
180 }//namespace Core
181 }//namespace SeComLib
182 
183 //Separate the implementation from the declaration
184 #include "crypto_provider.hpp"
185 
186 #endif//CRYPTO_PROVIDER_HEADER_GUARD
T_Ciphertext Ciphertext
Provide public access to the T_Ciphertext type.
Ciphertext GetEncryptedOne(const bool randomized=true) const
Returns [1].
Ciphertext GetEncryptedZero(const bool randomized=true) const
Returns [0].
const T_PublicKey & GetPublicKey() const
Public key getter.
RandomizerCache< RandomizerContainer< CryptoProvider< T_PublicKey, T_PrivateKey, T_Ciphertext, T_Randomizer >, RandomizerCacheParameters > > RandomizerCacheType
Data type of the randomizer cache.
unsigned int keyLength
The key length in bits.
Definition of template class RandomizerCache.
Defines BigInteger.
virtual size_t GetMessageSpaceSize() const =0
Returns the message space bit size.
Randomizer cache parameter container struct.
virtual T_Ciphertext EncryptInteger(const BigInteger &plaintext) const
Encrypt an integer and apply randomization.
T_PrivateKey privateKey
Private key container.
virtual const BigInteger & GetPositiveNegativeBoundary() const
Returns the biggest positive number that can be encrypted without overflowing.
T_Ciphertext encryptedZero
Contains [0] used as initializer for homomorphic addition accumulators. Precompute it for optimizatio...
Definition of struct RandomizerContainer.
const T_PrivateKey & GetPrivateKey() const
Private key getter.
Definition of struct RandomizerBase.
virtual BigInteger DecryptInteger(const T_Ciphertext &ciphertext) const =0
Decrypt an integer.
std::unique_ptr< RandomizerCacheType > randomizerCache
Lazy loading randomizer cache.
virtual ~CryptoProvider()
Destructor.
Implementation of template abstract class CryptoProvider. To be included in crypto_provider.h.
Definition of struct RandomizerCacheParameters.
bool precomputeSpeedupValues
Boolean flag that indicates wether doPrecomputations() should precompute certain values.
virtual const BigInteger & GetMessageSpaceUpperBound() const =0
Returns the message space upper bound.
virtual T_Randomizer GetRandomizer() const =0
Compute the random factor required for the encryption operation.
T_Ciphertext encryptedOne
Contains [1].
BigInteger positiveNegativeBoundary
Contains the delimiter between positive and negative values in the message space (usually ) ...
T_Randomizer Randomizer
Provide public access to the T_Randomizer type.
const BigInteger & GetEncryptionModulus() const
Returns the modulus required for reducing the encryption after randomization.
T_PublicKey publicKey
Public key container.
Template abstract base class for homomorphic encryption primitives.
virtual void doPrecomputations()=0
Computes the required precomputed values.
virtual bool GenerateKeys()=0
Generate the public and private keys required by the encryption primitive.
Definition of template class CiphertextBase.
std::shared_ptr< BigInteger > encryptionModulus
The encryption modulus.
virtual void validateParameters()=0
Validates configuration parameters.
bool hasPrivateKey
Boolean flag that enables decryption if the private key is present.
CryptoProvider(const unsigned int keyLength)
Constructor.
virtual T_Ciphertext EncryptIntegerNonrandom(const BigInteger &plaintext) const =0
Encrypt number without randomization.
virtual T_Ciphertext RandomizeCiphertext(const T_Ciphertext &ciphertext) const =0
Randomize encrypted number with a self-generated random value.