lime
Lime is a C++ library implementing Open Whisper System Signal protocol
lime_keys.hpp
Go to the documentation of this file.
1 /*
2  lime_keys.hpp
3  @author Johan Pascal
4  @copyright Copyright (C) 2017 Belledonne Communications SARL
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #ifndef lime_keys_hpp
21 #define lime_keys_hpp
22 
23 #include <algorithm> //std::copy_n
24 #include <array>
25 #include <iterator>
26 #include "lime/lime.hpp"
27 #include "bctoolbox/crypto.hh"
28 #ifdef HAVE_BCTBXPQ
29 #include "postquantumcryptoengine/crypto.hh"
30 #endif /* HAVE_BCTBXPQ */
31 
32 namespace lime {
33  // Data structure type enumerations
44 
50 
51  /* define needed constant for the curves: self identificatio(used in DB and as parameter from lib users, data structures sizes)*/
52  /* These structure are used as template argument to enable support for different key Exchznge and signature Algorithms */
53 
59  struct C255 {
60  using EC = C255;
62  static const std::string Id() {return std::string("CURVE25519");};
64  static constexpr lime::CurveId curveId() {return lime::CurveId::c25519;};
66  static constexpr size_t Xsize(lime::Xtype dataType) {return BCTBX_ECDH_X25519_PUBLIC_SIZE;};
68  static constexpr size_t DSAsize(lime::DSAtype dataType) {return (dataType != lime::DSAtype::signature)?BCTBX_EDDSA_25519_PUBLIC_SIZE:BCTBX_EDDSA_25519_SIGNATURE_SIZE;};
69  };
70 
74  struct C448 {
75  using EC = C448;
77  static const std::string Id() {return std::string("CURVE448");};
79  static constexpr lime::CurveId curveId() {return lime::CurveId::c448;};
81  static constexpr size_t Xsize(lime::Xtype dataType) {return BCTBX_ECDH_X448_PUBLIC_SIZE;};
83  static constexpr size_t DSAsize(lime::DSAtype dataType) {return (dataType != lime::DSAtype::signature)?BCTBX_EDDSA_448_PUBLIC_SIZE:BCTBX_EDDSA_448_SIGNATURE_SIZE;};
84  };
85 
86  struct genericKEM {
87  static constexpr lime::CurveId curveId() {return lime::CurveId::unset;}; // KEM cannot be used directly in DR or X3DH -> no curveId
88  };
89 #ifdef HAVE_BCTBXPQ
90 
93  struct K512: public genericKEM {
95  static const std::string Id(void) {return std::string("KYBER512");};
97  static constexpr size_t Ksize(lime::Ktype dataType) {
98  switch (dataType) {
100  return bctoolbox::KYBER512::kPkSize;
101  break;
103  return bctoolbox::KYBER512::kSkSize;
104  break;
106  return bctoolbox::KYBER512::kCtSize;
107  break;
109  return bctoolbox::KYBER512::kSsSize;
110  break;
111  }
112  return 0; // make compiler happy
113  };
114  };
115 
119  struct MLK512: public genericKEM {
121  static const std::string Id(void) {return std::string("MLKEM512");};
123  static constexpr size_t Ksize(lime::Ktype dataType) {
124  switch (dataType) {
126  return bctoolbox::MLKEM512::kPkSize;
127  break;
129  return bctoolbox::MLKEM512::kSkSize;
130  break;
132  return bctoolbox::MLKEM512::kCtSize;
133  break;
135  return bctoolbox::MLKEM512::kSsSize;
136  break;
137  }
138  return 0; // make compiler happy
139  };
140  };
141 
145  struct MLK1024: public genericKEM {
147  static const std::string Id(void) {return std::string("MLKEM1024");};
149  static constexpr size_t Ksize(lime::Ktype dataType) {
150  switch (dataType) {
152  return bctoolbox::MLKEM1024::kPkSize;
153  break;
155  return bctoolbox::MLKEM1024::kSkSize;
156  break;
158  return bctoolbox::MLKEM1024::kCtSize;
159  break;
161  return bctoolbox::MLKEM1024::kSsSize;
162  break;
163  }
164  return 0; // make compiler happy
165  };
166  };
167 
168  // Hybrids: c25519/kyber512 c25519/mlkem512, c448/mlkem1024
169  struct C255K512: public C255,K512 {
170  static constexpr lime::CurveId curveId(void) {return lime::CurveId::c25519k512;};
171  using EC = C255;
172  using KEM = K512;
173  };
174  struct C255MLK512: public C255,MLK512 {
175  static constexpr lime::CurveId curveId(void) {return lime::CurveId::c25519mlk512;};
176  using EC = C255;
177  using KEM = MLK512;
178  };
179  struct C448MLK1024: public C448,MLK1024 {
180  static constexpr lime::CurveId curveId(void) {return lime::CurveId::c448mlk1024;};
181  using EC = C448;
182  using KEM = MLK1024;
183  };
184 #endif // HAVE_BCTBXPQ
185 
186  // Hash function defines
190  struct SHA512 {
192  static constexpr size_t ssize() {return 64;}
193  };
194 
195  // AEAD function defines
199  struct AES256GCM {
201  static constexpr size_t keySize(void) {return 32;};
203  static constexpr size_t tagSize(void) {return 16;};
204  };
205 }
206 
207 #endif /* lime_keys_hpp */
static constexpr lime::CurveId curveId()
Definition: lime_keys.hpp:87
static const std::string Id()
a string to indentify this curve
Definition: lime_keys.hpp:62
DSAtype
List of data types used by Signature algorithm.
Definition: lime_keys.hpp:49
static const std::string Id()
a string to indentify this curve
Definition: lime_keys.hpp:77
SHA512 buffer size definition.
Definition: lime_keys.hpp:190
static constexpr size_t ssize()
maximum output size for SHA512 is 64 bytes
Definition: lime_keys.hpp:192
static constexpr size_t keySize(void)
key size is 32 bytes
Definition: lime_keys.hpp:201
curve 25519 data types size definition
Definition: lime_keys.hpp:59
Ktype
List of data types used by key encapsulation mechanism algorithm.
Definition: lime_keys.hpp:43
static constexpr size_t Xsize(lime::Xtype dataType)
for X448, public, private and shared secret have the same length 56 bytes
Definition: lime_keys.hpp:81
static constexpr size_t Xsize(lime::Xtype dataType)
for X25519, public, private and shared secret have the same length: 32 bytes
Definition: lime_keys.hpp:66
Xtype
List of data types used by key Echange algorithm.
Definition: lime_keys.hpp:38
static constexpr lime::CurveId curveId()
the C448 curve id using the CurveId enumeration
Definition: lime_keys.hpp:79
static constexpr size_t DSAsize(lime::DSAtype dataType)
for Ed25519, public and private key have the same length: 32 bytes, signature is 64 bytes long ...
Definition: lime_keys.hpp:68
Definition: lime_keys.hpp:86
Definition: lime.cpp:33
static constexpr size_t tagSize(void)
we use authentication tag size of 16 bytes
Definition: lime_keys.hpp:203
curve 448-goldilocks data types size definition
Definition: lime_keys.hpp:74
static constexpr size_t DSAsize(lime::DSAtype dataType)
for Ed448, public and private key have the same length 57 bytes, signature is 114 bytes long ...
Definition: lime_keys.hpp:83
static constexpr lime::CurveId curveId()
the C25519 curve id using the CurveId enumeration
Definition: lime_keys.hpp:64
AES256GCM buffers size definition.
Definition: lime_keys.hpp:199
CurveId
Definition: lime.hpp:37