20 #ifndef lime_crypto_primitives_hpp
21 #define lime_crypto_primitives_hpp
42 struct sBuffer :
public std::array<uint8_t, T> {
55 template <
typename Curve, lime::Xtype dataType>
56 class X :
public sBuffer<static_cast<size_t>(Curve::Xsize(dataType))>{
59 constexpr
static size_t ssize(
void) {
return Curve::Xsize(dataType);};
61 X(
const std::vector<uint8_t>::const_iterator buffer) {std::copy_n(buffer,
ssize(), this->begin());}
63 X(
const uint8_t *
const buffer) {std::copy_n(buffer,
ssize(), this->begin());}
67 void assign(
const std::vector<uint8_t>::const_iterator buffer) {std::copy_n(buffer,
ssize(), this->begin());}
73 template <
typename Curve>
87 Xpair() :m_pubKey{},m_privKey{}{};
101 template <
typename Algo, lime::Ktype dataType>
102 class K :
public sBuffer<static_cast<size_t>(Algo::Ksize(dataType))>{
105 constexpr
static size_t ssize(
void) {
return Algo::Ksize(dataType);};
107 K(
const std::vector<uint8_t>::const_iterator buffer) {std::copy_n(buffer,
ssize(), this->begin());}
109 K(
const uint8_t *
const buffer) {std::copy_n(buffer,
ssize(), this->begin());}
111 K() {this->fill(0);};
113 void assign(
const std::vector<uint8_t>::const_iterator buffer) {std::copy_n(buffer,
ssize(), this->begin());}
119 template <
typename Algo>
147 template <
typename Curve, lime::DSAtype dataType>
148 class DSA :
public sBuffer<static_cast<size_t>(Curve::DSAsize(dataType))>{
151 constexpr
static size_t ssize(
void) {
return Curve::DSAsize(dataType);};
153 DSA(
const std::vector<uint8_t>::const_iterator buffer) {std::copy_n(buffer,
ssize(), this->begin());}
157 void assign(
const std::vector<uint8_t>::const_iterator buffer) {std::copy_n(buffer,
ssize(), this->begin());}
163 template <
typename Curve>
212 virtual void randomize(uint8_t *buffer,
const size_t size) = 0;
214 virtual ~RNG() =
default;
223 template <
typename Curve>
264 virtual void createKeyPair(std::shared_ptr<lime::RNG> rng) = 0;
283 template <
typename Algo>
312 virtual ~KEM() =
default;
320 template <
typename Curve>
339 virtual void createKeyPair(std::shared_ptr<lime::RNG> rng) = 0;
390 template <
typename hashAlgo>
391 void HMAC(
const uint8_t *
const key,
const size_t keySize,
const uint8_t *
const input,
const size_t inputSize, uint8_t *hash,
size_t hashSize);
393 template <>
void HMAC<SHA512>(
const uint8_t *
const key,
const size_t keySize,
const uint8_t *
const input,
const size_t inputSize, uint8_t *hash,
size_t hashSize);
425 template <
typename hashAlgo>
426 void HMAC_KDF(
const uint8_t *
const salt,
const size_t saltSize,
const uint8_t *
const ikm,
const size_t ikmSize,
const char *info,
const size_t infoSize, uint8_t *output,
size_t outputSize);
428 template <>
void HMAC_KDF<SHA512>(
const uint8_t *
const salt,
const size_t saltSize,
const uint8_t *
const ikm,
const size_t ikmSize,
const char * info,
const size_t infoSize, uint8_t *output,
size_t outputSize);
448 template <
typename AEADAlgo>
449 void AEAD_encrypt(
const uint8_t *
const key,
const size_t keySize,
const uint8_t *
const IV,
const size_t IVSize,
450 const uint8_t *
const plain,
const size_t plainSize,
const uint8_t *
const AD,
const size_t ADSize,
451 uint8_t *tag,
const size_t tagSize, uint8_t *cipher);
470 template <
typename AEADAlgo>
471 bool AEAD_decrypt(
const uint8_t *
const key,
const size_t keySize,
const uint8_t *
const IV,
const size_t IVSize,
472 const uint8_t *
const cipher,
const size_t cipherSize,
const uint8_t *
const AD,
const size_t ADSize,
473 const uint8_t *
const tag,
const size_t tagSize, uint8_t *plain);
476 template <>
void AEAD_encrypt<AES256GCM>(
const uint8_t *
const key,
const size_t keySize,
const uint8_t *
const IV,
const size_t IVSize,
477 const uint8_t *
const plain,
const size_t plainSize,
const uint8_t *
const AD,
const size_t ADSize,
478 uint8_t *tag,
const size_t tagSize, uint8_t *cipher);
480 template <>
bool AEAD_decrypt<AES256GCM>(
const uint8_t *
const key,
const size_t keySize,
const uint8_t *
const IV,
const size_t IVSize,
481 const uint8_t *
const cipher,
const size_t cipherSize,
const uint8_t *
const AD,
const size_t ADSize,
482 const uint8_t *
const tag,
const size_t tagSize, uint8_t *plain);
491 template <
typename Curve>
494 template <
typename Curve>
497 template <
typename Algo>
498 std::shared_ptr<KEM<Algo>>
make_KEM();
505 #ifdef EC25519_ENABLED
507 extern template std::shared_ptr<Signature<C255>>
make_Signature();
516 #endif // EC25519_ENABLED
520 extern template std::shared_ptr<Signature<C448>>
make_Signature();
529 #endif // EC448_ENABLED
533 extern template std::shared_ptr<KEM<K512>>
make_KEM();
540 extern template std::shared_ptr<KEM<MLK512>>
make_KEM();
547 extern template std::shared_ptr<KEM<MLK1024>>
make_KEM();
553 #endif //HAVE_BCTBXPQ
556 #endif //lime_crypto_primitives_hpp
K(const uint8_t *const buffer)
construct from uint8_t *
Definition: lime_crypto_primitives.hpp:109
static constexpr size_t ssize(void)
provide a static size function to be able to call the function not on an object
Definition: lime_crypto_primitives.hpp:105
static constexpr size_t ssize(void)
provide a static size function to be able to call the function not on an object
Definition: lime_crypto_primitives.hpp:59
X()
default initialise value to 0
Definition: lime_crypto_primitives.hpp:65
virtual const X< Curve, lime::Xtype::publicKey > get_peerPublic(void)=0
get Peer Public key
Key Encapsulation Mechanism interface.
Definition: lime_crypto_primitives.hpp:284
virtual uint32_t randomize()=0
Generate a 32 bits unsigned integer(used to generate keys Id) The MSbit is forced to 0 to avoid deali...
virtual const X< Curve, lime::Xtype::sharedSecret > get_sharedSecret(void)=0
get shared secret when exchange is completed
X< Curve, lime::Xtype::privateKey > & privateKey(void)
access the private key
Definition: lime_crypto_primitives.hpp:80
DSA()
default initialise value to 0
Definition: lime_crypto_primitives.hpp:155
virtual void set_selfPublic(const X< Curve, lime::Xtype::publicKey > &selfPublic)=0
set Self Public key
virtual void set_secret(const X< Curve, lime::Xtype::privateKey > &secret)=0
set Secret key
Digital Signature interface.
Definition: lime_crypto_primitives.hpp:321
bool AEAD_decrypt(const uint8_t *const key, const size_t keySize, const uint8_t *const IV, const size_t IVSize, const uint8_t *const cipher, const size_t cipherSize, const uint8_t *const AD, const size_t ADSize, const uint8_t *const tag, const size_t tagSize, uint8_t *plain)
Authenticate and Decrypt using scheme given as template parameter.
Definition: lime_crypto_primitives.cpp:471
std::shared_ptr< RNG > make_RNG()
Definition: lime_crypto_primitives.cpp:91
Base buffer definition for KEM data structure.
Definition: lime_crypto_primitives.hpp:102
DSA(const std::vector< uint8_t >::const_iterator buffer)
contruct from a std::vector
Definition: lime_crypto_primitives.hpp:153
bool operator==(Kpair< Algo > b) const
== operator assert that public and private keys are the same
Definition: lime_crypto_primitives.hpp:135
Kpair()
Definition: lime_crypto_primitives.hpp:133
Base buffer definition for DSA data structure.
Definition: lime_crypto_primitives.hpp:148
virtual const DSA< Curve, lime::DSAtype::privateKey > get_secret(void)=0
Secret key.
Key pair structure for DSA algorithm.
Definition: lime_crypto_primitives.hpp:164
Key pair structure for key exchange algorithm.
Definition: lime_crypto_primitives.hpp:74
virtual void encaps(const K< Algo, lime::Ktype::publicKey > &publicKey, K< Algo, lime::Ktype::cipherText > &cipherText, K< Algo, lime::Ktype::sharedSecret > &sharedSecret)=0
Generate and encapsulate a shared secret for a given public key.
void cleanBuffer(uint8_t *buffer, size_t size)
force a buffer values to zero in a way that shall prevent the compiler from optimizing it out ...
Definition: lime_crypto_primitives.cpp:535
virtual void set_secret(const DSA< Curve, lime::DSAtype::privateKey > &secretKey)=0
Secret key.
const DSA< Curve, lime::DSAtype::privateKey > & cprivateKey(void) const
Definition: lime_crypto_primitives.hpp:171
const X< Curve, lime::Xtype::publicKey > & cpublicKey(void) const
Definition: lime_crypto_primitives.hpp:84
Xpair(const X< Curve, lime::Xtype::publicKey > &pub, const X< Curve, lime::Xtype::privateKey > &priv)
copy construct a key pair from public and private keys (no verification on validity of keys is perfor...
Definition: lime_crypto_primitives.hpp:86
void AEAD_encrypt(const uint8_t *const key, const size_t keySize, const uint8_t *const IV, const size_t IVSize, const uint8_t *const plain, const size_t plainSize, const uint8_t *const AD, const size_t ADSize, uint8_t *tag, const size_t tagSize, uint8_t *cipher)
Encrypt and tag using scheme given as template parameter.
Definition: lime_crypto_primitives.cpp:463
virtual void derivePublic(void)=0
Compute the public key using the secret already set in context.
virtual ~keyExchange()=default
void assign(const std::vector< uint8_t >::const_iterator buffer)
copy from a std::vector
Definition: lime_crypto_primitives.hpp:67
const K< Algo, lime::Ktype::privateKey > & cprivateKey(void) const
Definition: lime_crypto_primitives.hpp:127
void HMAC_KDF< SHA512 >(const uint8_t *const salt, const size_t saltSize, const uint8_t *const ikm, const size_t ikmSize, const char *info, const size_t infoSize, uint8_t *output, size_t outputSize)
Definition: lime_crypto_primitives.cpp:457
std::shared_ptr< keyExchange< Curve > > make_keyExchange()
Definition: lime_crypto_primitives.cpp:422
K< Algo, lime::Ktype::privateKey > & privateKey(void)
access the private key
Definition: lime_crypto_primitives.hpp:126
virtual void createKeyPair(Kpair< Algo > &keyPair)=0
generate a new random key pair
K< Algo, lime::Ktype::publicKey > & publicKey(void)
access the public key
Definition: lime_crypto_primitives.hpp:129
virtual void set_peerPublic(const X< Curve, lime::Xtype::publicKey > &peerPublic)=0
set Peer Public key
X(const std::vector< uint8_t >::const_iterator buffer)
construct from a std::vector
Definition: lime_crypto_primitives.hpp:61
virtual ~Signature()=default
K()
default initialise value to 0
Definition: lime_crypto_primitives.hpp:111
X(const uint8_t *const buffer)
construct from a uint8_t *
Definition: lime_crypto_primitives.hpp:63
const K< Algo, lime::Ktype::publicKey > & cpublicKey(void) const
Definition: lime_crypto_primitives.hpp:130
virtual void decaps(const K< Algo, lime::Ktype::privateKey > &privateKey, const K< Algo, lime::Ktype::cipherText > &cipherText, K< Algo, lime::Ktype::sharedSecret > &sharedSecret)=0
decapsulate a shared secret from a cipher text using a private key
void AEAD_encrypt< AES256GCM >(const uint8_t *const key, const size_t keySize, const uint8_t *const IV, const size_t IVSize, const uint8_t *const plain, const size_t plainSize, const uint8_t *const AD, const size_t ADSize, uint8_t *tag, const size_t tagSize, uint8_t *cipher)
Definition: lime_crypto_primitives.cpp:480
DSApair(const DSA< Curve, lime::DSAtype::publicKey > &pub, const DSA< Curve, lime::DSAtype::privateKey > &priv)
copy construct a key pair from public and private keys (no verification on validity of keys is perfor...
Definition: lime_crypto_primitives.hpp:176
void assign(const std::vector< uint8_t >::const_iterator buffer)
copy from a std::vector
Definition: lime_crypto_primitives.hpp:113
std::shared_ptr< KEM< Algo > > make_KEM()
virtual void sign(const std::vector< uint8_t > &message, DSA< Curve, lime::DSAtype::signature > &signature)=0
Sign a message using the key pair previously set in the object.
bool operator==(Xpair< Curve > b) const
== operator assert that public and private keys are the same
Definition: lime_crypto_primitives.hpp:89
virtual const X< Curve, lime::Xtype::privateKey > get_secret(void)=0
get Secret key
std::shared_ptr< Signature< Curve > > make_Signature()
Definition: lime_crypto_primitives.cpp:427
virtual const DSA< Curve, lime::DSAtype::publicKey > get_public(void)=0
Public key.
DSA< Curve, lime::DSAtype::publicKey > & publicKey(void)
access the public key
Definition: lime_crypto_primitives.hpp:173
Key pair structure for key exchange algorithm.
Definition: lime_crypto_primitives.hpp:120
static constexpr size_t ssize(void)
provide a static size function to be able to call the function not on an object
Definition: lime_crypto_primitives.hpp:151
virtual bool verify(const std::vector< uint8_t > &message, const DSA< Curve, lime::DSAtype::signature > &signature)=0
Verify a message signature using the public key previously set in the object.
void HMAC< SHA512 >(const uint8_t *const key, const size_t keySize, const uint8_t *const input, const size_t inputSize, uint8_t *hash, size_t hashSize)
Definition: lime_crypto_primitives.cpp:447
void HMAC(const uint8_t *const key, const size_t keySize, const uint8_t *const input, const size_t inputSize, uint8_t *hash, size_t hashSize)
templated HMAC
Definition: lime_crypto_primitives.cpp:441
const DSA< Curve, lime::DSAtype::publicKey > & cpublicKey(void) const
Definition: lime_crypto_primitives.hpp:174
Kpair(const K< Algo, lime::Ktype::publicKey > &pub, const K< Algo, lime::Ktype::privateKey > &priv)
copy construct a key pair from public and private keys (no verification on validity of keys is perfor...
Definition: lime_crypto_primitives.hpp:132
virtual void set_public(const DSA< Curve, lime::DSAtype::publicKey > &publicKey)=0
Public key.
DSApair()
Definition: lime_crypto_primitives.hpp:177
bool operator==(DSApair< Curve > b) const
== operator assert that public and private keys are the same
Definition: lime_crypto_primitives.hpp:179
X< Curve, lime::Xtype::publicKey > & publicKey(void)
access the public key
Definition: lime_crypto_primitives.hpp:83
virtual void computeSharedSecret(void)=0
Perform the shared secret computation, it is then available in the object via get_sharedSecret.
DSA< Curve, lime::DSAtype::privateKey > & privateKey(void)
access the private key
Definition: lime_crypto_primitives.hpp:170
K(const std::vector< uint8_t >::const_iterator buffer)
construct from a std::vector
Definition: lime_crypto_primitives.hpp:107
Xpair()
Definition: lime_crypto_primitives.hpp:87
auto clean fixed size buffer(std::array based)
Definition: lime_crypto_primitives.hpp:42
bool AEAD_decrypt< AES256GCM >(const uint8_t *const key, const size_t keySize, const uint8_t *const IV, const size_t IVSize, const uint8_t *const cipher, const size_t cipherSize, const uint8_t *const AD, const size_t ADSize, const uint8_t *const tag, const size_t tagSize, uint8_t *plain)
Definition: lime_crypto_primitives.cpp:493
Base buffer definition for Key Exchange data structure.
Definition: lime_crypto_primitives.hpp:56
virtual void deriveSelfPublic(void)=0
Compute the self public key using the secret already set in context.
void HMAC_KDF(const uint8_t *const salt, const size_t saltSize, const uint8_t *const ikm, const size_t ikmSize, const char *info, const size_t infoSize, uint8_t *output, size_t outputSize)
HKDF as described in RFC5869.
Definition: lime_crypto_primitives.cpp:452
~sBuffer()
zeroise all buffer when done
Definition: lime_crypto_primitives.hpp:44
virtual const X< Curve, lime::Xtype::publicKey > get_selfPublic(void)=0
get Self Public key
const X< Curve, lime::Xtype::privateKey > & cprivateKey(void) const
Definition: lime_crypto_primitives.hpp:81
virtual void createKeyPair(std::shared_ptr< lime::RNG > rng)=0
generate a new random key pair
Random number generator interface.
Definition: lime_crypto_primitives.hpp:196
Key exchange interface.
Definition: lime_crypto_primitives.hpp:224
void assign(const std::vector< uint8_t >::const_iterator buffer)
copy from a std::vector
Definition: lime_crypto_primitives.hpp:157
virtual void createKeyPair(std::shared_ptr< lime::RNG > rng)=0
generate a new random EdDSA key pair