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
25namespace lime {
26 namespace double_ratchet_protocol {
34 template <typename Curve>
35 constexpr size_t headerSize() noexcept {
37 }
38
46 template <typename Curve>
47 constexpr size_t X3DHinitSize(bool haveOPk) noexcept {
48 return 1 + DSA<Curve, lime::DSAtype::publicKey>::ssize() + X<Curve, lime::Xtype::publicKey>::ssize() + 4 // size of X3DH init message without OPk
49 + (haveOPk?4:0); // if there is an OPk, we must add 4 for the OPk id
50 }
51
52 template <typename Curve>
53 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;
54 template <typename Curve>
55 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;
56
57 template <typename Curve>
58 bool parseMessage_get_X3DHinit(const std::vector<uint8_t> &message, std::vector<uint8_t> &X3DH_initMessage) noexcept;
59
60 template <typename Curve>
61 void buildMessage_header(std::vector<uint8_t> &header, const uint16_t Ns, const uint16_t PN, const X<Curve, lime::Xtype::publicKey> &DHs, const std::vector<uint8_t> X3DH_initMessage, const bool payloadDirectEncryption) noexcept;
62
67 template <typename Curve>
68 class DRHeader {
69 private:
70 uint16_t m_Ns,m_PN;
72 bool m_valid;
73 size_t m_size;
74 bool m_payload_direct_encryption;
76 public:
78 uint16_t Ns(void) const {return m_Ns;}
80 uint16_t PN(void) const {return m_PN;}
82 const X<Curve, lime::Xtype::publicKey> &DHs(void) const {return m_DHs;}
84 bool valid(void) const {return m_valid;}
86 bool payloadDirectEncryption(void) const {return m_payload_direct_encryption;}
88 size_t size(void) {return m_size;}
89
90 /* ctor/dtor */
91 DRHeader() = delete;
92 DRHeader(const std::vector<uint8_t> header);
94 };
95
96 /* this templates are intanciated in lime_double_ratchet_procotocol.cpp, do not re-instanciate it anywhere else */
97#ifdef EC25519_ENABLED
98 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;
99 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;
100 extern template bool parseMessage_get_X3DHinit<C255>(const std::vector<uint8_t> &message, std::vector<uint8_t> &X3DH_initMessage) noexcept;
101 extern template void buildMessage_header<C255>(std::vector<uint8_t> &header, const uint16_t Ns, const uint16_t PN, const X<C255, lime::Xtype::publicKey> &DHs, const std::vector<uint8_t> X3DH_initMessage, const bool payloadDirectEncryption) noexcept;
102 extern template class DRHeader<C255>;
103#endif
104
105#ifdef EC448_ENABLED
106 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;
107 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;
108 extern template bool parseMessage_get_X3DHinit<C448>(const std::vector<uint8_t> &message, std::vector<uint8_t> &X3DH_initMessage) noexcept;
109 extern template void buildMessage_header<C448>(std::vector<uint8_t> &header, const uint16_t Ns, const uint16_t PN, const X<C448, lime::Xtype::publicKey> &DHs, const std::vector<uint8_t> X3DH_initMessage, const bool payloadDirectEncryption) noexcept;
110 extern template class DRHeader<C448>;
111#endif
112 /* These constants are needed only for tests purpose, otherwise their usage is internal only to double_ratchet_protocol.hpp */
114 constexpr std::uint8_t DR_v01=0x01;
115
130 enum class DR_message_type : uint8_t{
131 X3DH_init_flag=0x01,
133 };
134
137 enum class DR_X3DH_OPk_flag : uint8_t{
138 withoutOPk=0x00,
139 withOPk=0x01
140 };
141
142 } // namespace double_ratchet_protocol
143}// namespace lime
144#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:100
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
helper class and functions to parse Double Ratchet message header and access its components
Definition: lime_double_ratchet_protocol.hpp:68
uint16_t Ns(void) const
read-only accessor to Sender Chain index (Ns)
Definition: lime_double_ratchet_protocol.hpp:78
bool valid(void) const
is this header valid? (property is set by constructor/parser)
Definition: lime_double_ratchet_protocol.hpp:84
const X< Curve, lime::Xtype::publicKey > & DHs(void) const
read-only accessor to peer Double Ratchet public key
Definition: lime_double_ratchet_protocol.hpp:82
size_t size(void)
read-only accessor to the size of parsed header
Definition: lime_double_ratchet_protocol.hpp:88
bool payloadDirectEncryption(void) const
what encryption mode is advertised in this header
Definition: lime_double_ratchet_protocol.hpp:86
uint16_t PN(void) const
read-only accessor to Previous Sender Chain index (PN)
Definition: lime_double_ratchet_protocol.hpp:80
~DRHeader()
Definition: lime_double_ratchet_protocol.hpp:93
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:157
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.
Definition: lime_double_ratchet_protocol.cpp:124
DR_X3DH_OPk_flag
haveOPk byte from X3DH init message mapping
Definition: lime_double_ratchet_protocol.hpp:137
constexpr std::uint8_t DR_v01
Definition: lime_double_ratchet_protocol.hpp:114
void buildMessage_header(std::vector< uint8_t > &header, const uint16_t Ns, const uint16_t PN, const X< Curve, lime::Xtype::publicKey > &DHs, const std::vector< uint8_t > X3DH_initMessage, const bool payloadDirectEncryption) noexcept
Build a header string from needed info.
Definition: lime_double_ratchet_protocol.cpp:213
constexpr size_t headerSize() noexcept
return the size of the double ratchet packet header
Definition: lime_double_ratchet_protocol.hpp:35
DR_message_type
DR message type byte bit mapping.
Definition: lime_double_ratchet_protocol.hpp:130
constexpr size_t X3DHinitSize(bool haveOPk) noexcept
return the size of the X3DH init packet included in the double ratchet packet header
Definition: lime_double_ratchet_protocol.hpp:47
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
Definition: lime_double_ratchet_protocol.cpp:85
Definition: lime.cpp:30