lime
Lime is a C++ library implementing Open Whisper System Signal protocol
lime_x3dh_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_x3dh_protocol_hpp
21 #define lime_x3dh_protocol_hpp
22 
24 
25 namespace lime {
26 
31  enum class X3DHKeyBundleFlag : uint8_t {
32  noOPk=0,
33  OPk=1,
34  noBundle=2};
40  template <typename Curve>
41  struct X3DH_peerBundle {
42  const std::string deviceId;
45  const uint32_t SPk_id;
49  const uint32_t OPk_id;
62  X3DH_peerBundle(std::string &&deviceId, std::vector<uint8_t>::const_iterator Ik, std::vector<uint8_t>::const_iterator SPk, uint32_t SPk_id, std::vector<uint8_t>::const_iterator SPk_sig, std::vector<uint8_t>::const_iterator OPk, uint32_t OPk_id) :
63  deviceId{deviceId}, Ik{Ik}, SPk{SPk}, SPk_id{SPk_id}, SPk_sig{SPk_sig}, bundleFlag{lime::X3DHKeyBundleFlag::OPk}, OPk{OPk}, OPk_id{OPk_id} {};
68  X3DH_peerBundle(std::string &&deviceId, std::vector<uint8_t>::const_iterator Ik, std::vector<uint8_t>::const_iterator SPk, uint32_t SPk_id, std::vector<uint8_t>::const_iterator SPk_sig) :
69  deviceId{deviceId}, Ik{Ik}, SPk{SPk}, SPk_id{SPk_id}, SPk_sig{SPk_sig}, bundleFlag{lime::X3DHKeyBundleFlag::noOPk}, OPk{}, OPk_id{0} {};
74  X3DH_peerBundle(std::string &&deviceId) :
75  deviceId{deviceId}, Ik{}, SPk{}, SPk_id{0}, SPk_sig{}, bundleFlag{lime::X3DHKeyBundleFlag::noBundle}, OPk{}, OPk_id{0} {};
76  };
77 
78  namespace x3dh_protocol {
79  template <typename Curve>
80  void buildMessage_registerUser(std::vector<uint8_t> &message, const DSA<Curve, lime::DSAtype::publicKey> &Ik, const X<Curve, lime::Xtype::publicKey> &SPk, const DSA<Curve, lime::DSAtype::signature> &Sig, const uint32_t SPk_id, const std::vector<X<Curve, lime::Xtype::publicKey>> &OPks, const std::vector<uint32_t> &OPk_ids) noexcept;
81 
82  template <typename Curve>
83  void buildMessage_deleteUser(std::vector<uint8_t> &message) noexcept;
84 
85  template <typename Curve>
86  void buildMessage_publishSPk(std::vector<uint8_t> &message, const X<Curve, lime::Xtype::publicKey> &SPk, const DSA<Curve, lime::DSAtype::signature> &Sig, const uint32_t SPk_id) noexcept;
87 
88  template <typename Curve>
89  void buildMessage_publishOPks(std::vector<uint8_t> &message, const std::vector<X<Curve, lime::Xtype::publicKey>> &OPks, const std::vector<uint32_t> &OPk_ids) noexcept;
90 
91  template <typename Curve>
92  void buildMessage_getPeerBundles(std::vector<uint8_t> &message, std::vector<std::string> &peer_device_ids) noexcept;
93 
94  template <typename Curve>
95  void buildMessage_getSelfOPks(std::vector<uint8_t> &message) noexcept;
96 
97  /* this templates are intanciated in lime_x3dh_procotocol.cpp, do not re-instanciate it anywhere else */
98 #ifdef EC25519_ENABLED
99  extern template void buildMessage_registerUser<C255>(std::vector<uint8_t> &message, const DSA<C255, lime::DSAtype::publicKey> &Ik, const X<C255, lime::Xtype::publicKey> &SPk, const DSA<C255, lime::DSAtype::signature> &Sig, const uint32_t SPk_id, const std::vector<X<C255, lime::Xtype::publicKey>> &OPks, const std::vector<uint32_t> &OPk_ids) noexcept;
100  extern template void buildMessage_deleteUser<C255>(std::vector<uint8_t> &message) noexcept;
101  extern template void buildMessage_publishSPk<C255>(std::vector<uint8_t> &message, const X<C255, lime::Xtype::publicKey> &SPk, const DSA<C255, lime::DSAtype::signature> &Sig, const uint32_t SPk_id) noexcept;
102  extern template void buildMessage_publishOPks<C255>(std::vector<uint8_t> &message, const std::vector<X<C255, lime::Xtype::publicKey>> &OPks, const std::vector<uint32_t> &OPk_ids) noexcept;
103  extern template void buildMessage_getPeerBundles<C255>(std::vector<uint8_t> &message, std::vector<std::string> &peer_device_ids) noexcept;
104  extern template void buildMessage_getSelfOPks<C255>(std::vector<uint8_t> &message) noexcept;
105 #endif
106 
107 #ifdef EC448_ENABLED
108  extern template void buildMessage_registerUser<C448>(std::vector<uint8_t> &message, const DSA<C448, lime::DSAtype::publicKey> &Ik, const X<C448, lime::Xtype::publicKey> &SPk, const DSA<C448, lime::DSAtype::signature> &Sig, const uint32_t SPk_id, const std::vector<X<C448, lime::Xtype::publicKey>> &OPks, const std::vector<uint32_t> &OPk_ids) noexcept;
109  extern template void buildMessage_deleteUser<C448>(std::vector<uint8_t> &message) noexcept;
110  extern template void buildMessage_publishSPk<C448>(std::vector<uint8_t> &message, const X<C448, lime::Xtype::publicKey> &SPk, const DSA<C448, lime::DSAtype::signature> &Sig, const uint32_t SPk_id) noexcept;
111  extern template void buildMessage_publishOPks<C448>(std::vector<uint8_t> &message, const std::vector<X<C448, lime::Xtype::publicKey>> &OPks, const std::vector<uint32_t> &OPk_ids) noexcept;
112  extern template void buildMessage_getPeerBundles<C448>(std::vector<uint8_t> &message, std::vector<std::string> &peer_device_ids) noexcept;
113  extern template void buildMessage_getSelfOPks<C448>(std::vector<uint8_t> &message) noexcept;
114 #endif
115 
116  } // namespace x3dh_protocol
117 } // namespace lime
118 
119 #endif /* lime_x3dh_protocol_hpp */
X3DHKeyBundleFlag
Set possible values for a flag in the keyBundle X3DH packet.
Definition: lime_x3dh_protocol.hpp:31
const X< Curve, lime::Xtype::publicKey > SPk
Definition: lime_x3dh_protocol.hpp:44
void buildMessage_registerUser(std::vector< uint8_t > &message, const DSA< Curve, lime::DSAtype::publicKey > &Ik, const X< Curve, lime::Xtype::publicKey > &SPk, const DSA< Curve, lime::DSAtype::signature > &Sig, const uint32_t SPk_id, const std::vector< X< Curve, lime::Xtype::publicKey >> &OPks, const std::vector< uint32_t > &OPk_ids) noexcept
build a registerUser message : Identity Key<EDDSA Public Key length>
Definition: lime_x3dh_protocol.cpp:180
void buildMessage_deleteUser(std::vector< uint8_t > &message) noexcept
build a deleteUser message
Definition: lime_x3dh_protocol.cpp:248
const std::string deviceId
Definition: lime_x3dh_protocol.hpp:42
Holds everything found in a key bundle received from X3DH server.
Definition: lime_x3dh_protocol.hpp:41
const X< Curve, lime::Xtype::publicKey > OPk
Definition: lime_x3dh_protocol.hpp:48
X3DH_peerBundle(std::string &&deviceId)
Definition: lime_x3dh_protocol.hpp:74
X3DH_peerBundle(std::string &&deviceId, std::vector< uint8_t >::const_iterator Ik, std::vector< uint8_t >::const_iterator SPk, uint32_t SPk_id, std::vector< uint8_t >::const_iterator SPk_sig)
Definition: lime_x3dh_protocol.hpp:68
void buildMessage_publishSPk(std::vector< uint8_t > &message, const X< Curve, lime::Xtype::publicKey > &SPk, const DSA< Curve, lime::DSAtype::signature > &Sig, const uint32_t SPk_id) noexcept
build a postSPk message
Definition: lime_x3dh_protocol.cpp:267
void buildMessage_getPeerBundles(std::vector< uint8_t > &message, std::vector< std::string > &peer_device_ids) noexcept
build a getPeerBundle message
Definition: lime_x3dh_protocol.cpp:353
const uint32_t OPk_id
Definition: lime_x3dh_protocol.hpp:49
void buildMessage_getSelfOPks(std::vector< uint8_t > &message) noexcept
build a getSelfOPks message
Definition: lime_x3dh_protocol.cpp:396
Definition: lime.cpp:30
X3DH_peerBundle(std::string &&deviceId, std::vector< uint8_t >::const_iterator Ik, std::vector< uint8_t >::const_iterator SPk, uint32_t SPk_id, std::vector< uint8_t >::const_iterator SPk_sig, std::vector< uint8_t >::const_iterator OPk, uint32_t OPk_id)
Definition: lime_x3dh_protocol.hpp:62
const uint32_t SPk_id
Definition: lime_x3dh_protocol.hpp:45
const lime::X3DHKeyBundleFlag bundleFlag
Definition: lime_x3dh_protocol.hpp:47
const DSA< Curve, lime::DSAtype::publicKey > Ik
Definition: lime_x3dh_protocol.hpp:43
void buildMessage_publishOPks(std::vector< uint8_t > &message, const std::vector< X< Curve, lime::Xtype::publicKey >> &OPks, const std::vector< uint32_t > &OPk_ids) noexcept
build a postOPks message
Definition: lime_x3dh_protocol.cpp:304
const DSA< Curve, lime::DSAtype::signature > SPk_sig
Definition: lime_x3dh_protocol.hpp:46