lime
Lime is a C++ library implementing Open Whisper System Signal protocol
lime_double_ratchet_protocol.hpp
Go to the documentation of this file.
1 /*
2  lime_x3dh_protocol.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_double_ratchet_protocol_hpp
21 #define lime_double_ratchet_protocol_hpp
22 
24 #include "lime_double_ratchet.hpp"
25 
26 namespace lime {
27  namespace double_ratchet_protocol {
28  /* These constants are needed in the header only for tests purpose, otherwise their usage is internal only to double_ratchet_protocol.hpp */
30  constexpr uint8_t DR_v01=0x01;
31 
48  enum class DR_message_type : uint8_t{
49  X3DH_init_flag = 0x01,
51  KEM_pk_index = 0x04
52  };
53 
56  enum class DR_X3DH_OPk_flag : uint8_t{
57  withoutOPk=0x00,
58  withOPk=0x01
59  };
60 
68  template <typename Curve>
69  constexpr size_t headerSize(uint8_t messageType) noexcept {
70  if (messageType & static_cast<uint8_t>(lime::double_ratchet_protocol::DR_message_type::KEM_pk_index)) {
72  } else {
74  }
75  }
76 
85  template <typename Curve>
86  constexpr size_t X3DHinitSize(bool haveOPk, typename std::enable_if_t<!std::is_base_of_v<genericKEM, Curve>, bool> = true) noexcept {
87  return 1 + DSA<Curve, lime::DSAtype::publicKey>::ssize() + X<Curve, lime::Xtype::publicKey>::ssize() + 4 // size of X3DH init message without OPk
88  + (haveOPk?4:0); // if there is an OPk, we must add 4 for the OPk id
89  }
98  template <typename Algo>
99  constexpr size_t X3DHinitSize(bool haveOPk, typename std::enable_if_t<std::is_base_of_v<genericKEM, Algo>, bool> = true) noexcept {
100  return 1
104  + 4 // size of X3DH init message without OPk
105  + (haveOPk?4:0); // if there is an OPk, we must add 4 for the OPk id
106  }
107 
108  template <typename Curve>
109  void buildMessage_X3DHinit(std::vector<uint8_t> &message, const DSA<Curve, lime::DSAtype::publicKey> &Ik, const X<Curve, lime::Xtype::publicKey> &Ek, const uint32_t SPk_id, const uint32_t OPk_id, const bool OPk_flag) noexcept;
110  template <typename Algo>
111  void buildMessage_X3DHinit(std::vector<uint8_t> &message, const DSA<typename Algo::EC, lime::DSAtype::publicKey> &Ik, const X<typename Algo::EC, lime::Xtype::publicKey> &Ek, const K<typename Algo::KEM, lime::Ktype::cipherText> &Ct, const uint32_t SPk_id, const uint32_t OPk_id, const bool OPk_flag) noexcept;
112  template <typename Curve>
113  void parseMessage_X3DHinit(const std::vector<uint8_t>message, DSA<Curve, lime::DSAtype::publicKey> &Ik, X<Curve, lime::Xtype::publicKey> &Ek, uint32_t &SPk_id, uint32_t &OPk_id, bool &OPk_flag) noexcept;
114  template <typename Algo>
115  void parseMessage_X3DHinit(const std::vector<uint8_t>message, DSA<typename Algo::EC, lime::DSAtype::publicKey> &Ik, X<typename Algo::EC, lime::Xtype::publicKey> &Ek, K<typename Algo::KEM, lime::Ktype::cipherText> &Ct, uint32_t &SPk_id, uint32_t &OPk_id, bool &OPk_flag) noexcept;
116 
117  template <typename Curve>
118  bool parseMessage_get_X3DHinit(const std::vector<uint8_t> &message, std::vector<uint8_t> &X3DH_initMessage) noexcept;
119 
120 
121  /* this templates are intanciated in lime_double_ratchet_procotocol.cpp, do not re-instanciate it anywhere else */
122 #ifdef EC25519_ENABLED
123  extern template void buildMessage_X3DHinit<C255>(std::vector<uint8_t> &message, const DSA<C255, lime::DSAtype::publicKey> &Ik, const X<C255, lime::Xtype::publicKey> &Ek, const uint32_t SPk_id, const uint32_t OPk_id, const bool OPk_flag) noexcept;
124  extern template void parseMessage_X3DHinit<C255>(const std::vector<uint8_t>message, DSA<C255, lime::DSAtype::publicKey> &Ik, X<C255, lime::Xtype::publicKey> &Ek, uint32_t &SPk_id, uint32_t &OPk_id, bool &OPk_flag) noexcept;
125  extern template bool parseMessage_get_X3DHinit<C255>(const std::vector<uint8_t> &message, std::vector<uint8_t> &X3DH_initMessage) noexcept;
126 #endif
127 
128 #ifdef EC448_ENABLED
129  extern template void buildMessage_X3DHinit<C448>(std::vector<uint8_t> &message, const DSA<C448, lime::DSAtype::publicKey> &Ik, const X<C448, lime::Xtype::publicKey> &Ek, const uint32_t SPk_id, const uint32_t OPk_id, const bool OPk_flag) noexcept;
130  extern template void parseMessage_X3DHinit<C448>(const std::vector<uint8_t>message, DSA<C448, lime::DSAtype::publicKey> &Ik, X<C448, lime::Xtype::publicKey> &Ek, uint32_t &SPk_id, uint32_t &OPk_id, bool &OPk_flag) noexcept;
131  extern template bool parseMessage_get_X3DHinit<C448>(const std::vector<uint8_t> &message, std::vector<uint8_t> &X3DH_initMessage) noexcept;
132 
133 #endif
134 
135 #ifdef HAVE_BCTBXPQ
136 #ifdef EC25519_ENABLED
137  extern template void buildMessage_X3DHinit<C255K512>(std::vector<uint8_t> &message, const DSA<C255K512::EC, lime::DSAtype::publicKey> &Ik, const X<C255K512::EC, lime::Xtype::publicKey> &Ek, const K<C255K512::KEM, lime::Ktype::cipherText> &Ct, const uint32_t SPk_id, const uint32_t OPk_id, const bool OPk_flag) noexcept;
138  extern template void parseMessage_X3DHinit<C255K512>(const std::vector<uint8_t>message, DSA<C255K512::EC, lime::DSAtype::publicKey> &Ik, X<C255K512::EC, lime::Xtype::publicKey> &Ek, K<C255K512::KEM, lime::Ktype::cipherText> &Ct, uint32_t &SPk_id, uint32_t &OPk_id, bool &OPk_flag) noexcept;
139  extern template bool parseMessage_get_X3DHinit<C255K512>(const std::vector<uint8_t> &message, std::vector<uint8_t> &X3DH_initMessage) noexcept;
140 
141  extern template void buildMessage_X3DHinit<C255MLK512>(std::vector<uint8_t> &message, const DSA<C255MLK512::EC, lime::DSAtype::publicKey> &Ik, const X<C255MLK512::EC, lime::Xtype::publicKey> &Ek, const K<C255MLK512::KEM, lime::Ktype::cipherText> &Ct, const uint32_t SPk_id, const uint32_t OPk_id, const bool OPk_flag) noexcept;
142  extern template void parseMessage_X3DHinit<C255MLK512>(const std::vector<uint8_t>message, DSA<C255MLK512::EC, lime::DSAtype::publicKey> &Ik, X<C255MLK512::EC, lime::Xtype::publicKey> &Ek, K<C255MLK512::KEM, lime::Ktype::cipherText> &Ct, uint32_t &SPk_id, uint32_t &OPk_id, bool &OPk_flag) noexcept;
143  extern template bool parseMessage_get_X3DHinit<C255MLK512>(const std::vector<uint8_t> &message, std::vector<uint8_t> &X3DH_initMessage) noexcept;
144 #endif
145 #ifdef EC448_ENABLED
146  extern template void buildMessage_X3DHinit<C448MLK1024>(std::vector<uint8_t> &message, const DSA<C448MLK1024::EC, lime::DSAtype::publicKey> &Ik, const X<C448MLK1024::EC, lime::Xtype::publicKey> &Ek, const K<C448MLK1024::KEM, lime::Ktype::cipherText> &Ct, const uint32_t SPk_id, const uint32_t OPk_id, const bool OPk_flag) noexcept;
147  extern template void parseMessage_X3DHinit<C448MLK1024>(const std::vector<uint8_t>message, DSA<C448MLK1024::EC, lime::DSAtype::publicKey> &Ik, X<C448MLK1024::EC, lime::Xtype::publicKey> &Ek, K<C448MLK1024::KEM, lime::Ktype::cipherText> &Ct, uint32_t &SPk_id, uint32_t &OPk_id, bool &OPk_flag) noexcept;
148  extern template bool parseMessage_get_X3DHinit<C448MLK1024>(const std::vector<uint8_t> &message, std::vector<uint8_t> &X3DH_initMessage) noexcept;
149 #endif
150 #endif //HAVE_BCTBXPQ
151 
152 
153  } // namespace double_ratchet_protocol
154 }// namespace lime
155 #endif // lime_double_ratchet_protocol_hpp
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
void buildMessage_X3DHinit(std::vector< uint8_t > &message, const DSA< Curve, lime::DSAtype::publicKey > &Ik, const X< Curve, lime::Xtype::publicKey > &Ek, const uint32_t SPk_id, const uint32_t OPk_id, const bool OPk_flag) noexcept
build an X3DH init message to insert in DR header EC only version
Definition: lime_double_ratchet_protocol.cpp:87
Definition: lime_double_ratchet.hpp:46
Base buffer definition for KEM data structure.
Definition: lime_crypto_primitives.hpp:102
constexpr size_t headerSize(uint8_t messageType) noexcept
return the size of the double ratchet packet header
Definition: lime_double_ratchet_protocol.hpp:69
DR_message_type
DR message type byte bit mapping.
Definition: lime_double_ratchet_protocol.hpp:48
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
constexpr size_t X3DHinitSize(bool haveOPk, typename std::enable_if_t<!std::is_base_of_v< genericKEM, Curve >, bool >=true) noexcept
return the size of the X3DH init packet included in the double ratchet packet header ...
Definition: lime_double_ratchet_protocol.hpp:86
void parseMessage_X3DHinit(const std::vector< uint8_t >message, DSA< Curve, lime::DSAtype::publicKey > &Ik, X< Curve, lime::Xtype::publicKey > &Ek, uint32_t &SPk_id, uint32_t &OPk_id, bool &OPk_flag) noexcept
Parse the X3DH init message and extract peer Ik, peer Ek, self SPk id and seld OPk id if present This...
Definition: lime_double_ratchet_protocol.cpp:166
Definition: lime.cpp:33
DR_X3DH_OPk_flag
haveOPk byte from X3DH init message mapping
Definition: lime_double_ratchet_protocol.hpp:56
constexpr size_t DRPkIndexSize
Definition: lime_defines.hpp:81
bool parseMessage_get_X3DHinit(const std::vector< uint8_t > &message, std::vector< uint8_t > &X3DH_initMessage) noexcept
check the message for presence of X3DH init in the header, extract it if there is one ...
Definition: lime_double_ratchet_protocol.cpp:248
constexpr uint8_t DR_v01
Definition: lime_double_ratchet_protocol.hpp:30