Loading...
Searching...
No Matches
lime_double_ratchet_protocol.hpp
Go to the documentation of this file.
/*
lime_x3dh_protocol.hpp
@author Johan Pascal
@copyright Copyright (C) 2017 Belledonne Communications SARL
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef lime_double_ratchet_protocol_hpp
#define lime_double_ratchet_protocol_hpp
#include "lime_crypto_primitives.hpp"
namespace lime {
namespace double_ratchet_protocol {
template <typename Curve>
constexpr size_t headerSize() noexcept {
return 7 + X<Curve, lime::Xtype::publicKey>::ssize();
}
template <typename Curve>
constexpr size_t X3DHinitSize(bool haveOPk) noexcept {
return 1 + DSA<Curve, lime::DSAtype::publicKey>::ssize() + X<Curve, lime::Xtype::publicKey>::ssize() + 4 // size of X3DH init message without OPk
+ (haveOPk?4:0); // if there is an OPk, we must add 4 for the OPk id
}
template <typename Curve>
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;
template <typename Curve>
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;
template <typename Curve>
bool parseMessage_get_X3DHinit(const std::vector<uint8_t> &message, std::vector<uint8_t> &X3DH_initMessage) noexcept;
template <typename Curve>
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;
template <typename Curve>
class DRHeader {
private:
uint16_t m_Ns,m_PN;
X<Curve, lime::Xtype::publicKey> m_DHs;
bool m_valid;
size_t m_size;
bool m_payload_direct_encryption;
public:
uint16_t Ns(void) const {return m_Ns;}
uint16_t PN(void) const {return m_PN;}
const X<Curve, lime::Xtype::publicKey> &DHs(void) const {return m_DHs;}
bool valid(void) const {return m_valid;}
bool payloadDirectEncryption(void) const {return m_payload_direct_encryption;}
size_t size(void) {return m_size;}
/* ctor/dtor */
DRHeader() = delete;
DRHeader(const std::vector<uint8_t> header);
~DRHeader() {};
};
/* this templates are intanciated in lime_double_ratchet_procotocol.cpp, do not re-instanciate it anywhere else */
#ifdef EC25519_ENABLED
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;
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;
extern template bool parseMessage_get_X3DHinit<C255>(const std::vector<uint8_t> &message, std::vector<uint8_t> &X3DH_initMessage) noexcept;
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;
extern template class DRHeader<C255>;
#endif
#ifdef EC448_ENABLED
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;
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;
extern template bool parseMessage_get_X3DHinit<C448>(const std::vector<uint8_t> &message, std::vector<uint8_t> &X3DH_initMessage) noexcept;
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;
extern template class DRHeader<C448>;
#endif
/* These constants are needed only for tests purpose, otherwise their usage is internal only to double_ratchet_protocol.hpp */
constexpr uint8_t DR_v01=0x01;
enum class DR_message_type : uint8_t{
X3DH_init_flag=0x01,
payload_direct_encryption_flag=0x02
};
enum class DR_X3DH_OPk_flag : uint8_t{
withoutOPk=0x00,
withOPk=0x01
};
} // namespace double_ratchet_protocol
}// namespace lime
#endif // lime_double_ratchet_protocol_hpp
lime::DSA
Base buffer definition for DSA data structure.
Definition lime_crypto_primitives.hpp:97
lime::DSA::ssize
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
lime::X
Base buffer definition for Key Exchange data structure.
Definition lime_crypto_primitives.hpp:56
lime::X::ssize
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
lime::double_ratchet_protocol::DRHeader::Ns
uint16_t Ns(void) const
read-only accessor to Sender Chain index (Ns)
Definition lime_double_ratchet_protocol.hpp:78
lime::double_ratchet_protocol::DRHeader::valid
bool valid(void) const
is this header valid? (property is set by constructor/parser)
Definition lime_double_ratchet_protocol.hpp:84
lime::double_ratchet_protocol::DRHeader::DHs
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
lime::double_ratchet_protocol::DRHeader::size
size_t size(void)
read-only accessor to the size of parsed header
Definition lime_double_ratchet_protocol.hpp:88
lime::double_ratchet_protocol::DRHeader::payloadDirectEncryption
bool payloadDirectEncryption(void) const
what encryption mode is advertised in this header
Definition lime_double_ratchet_protocol.hpp:86
lime::double_ratchet_protocol::DRHeader::DRHeader
DRHeader()=delete
lime::double_ratchet_protocol::DRHeader::PN
uint16_t PN(void) const
read-only accessor to Previous Sender Chain index (PN)
Definition lime_double_ratchet_protocol.hpp:80
lime::double_ratchet_protocol::DRHeader::~DRHeader
~DRHeader()
Definition lime_double_ratchet_protocol.hpp:93
lime_crypto_primitives.hpp
lime::double_ratchet_protocol
Group in this namespace all the functions related to building or parsing double ratchet packets.
Definition lime_double_ratchet_protocol.cpp:63
lime::double_ratchet_protocol::DR_v01
constexpr uint8_t DR_v01
Definition lime_double_ratchet_protocol.hpp:114
lime::double_ratchet_protocol::parseMessage_get_X3DHinit
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
lime::double_ratchet_protocol::parseMessage_X3DHinit
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
lime::double_ratchet_protocol::DR_X3DH_OPk_flag
DR_X3DH_OPk_flag
haveOPk byte from X3DH init message mapping
Definition lime_double_ratchet_protocol.hpp:137
lime::double_ratchet_protocol::DR_X3DH_OPk_flag::withOPk
@ withOPk
Definition lime_double_ratchet_protocol.hpp:139
lime::double_ratchet_protocol::DR_X3DH_OPk_flag::withoutOPk
@ withoutOPk
Definition lime_double_ratchet_protocol.hpp:138
lime::double_ratchet_protocol::buildMessage_header
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
lime::double_ratchet_protocol::headerSize
constexpr size_t headerSize() noexcept
return the size of the double ratchet packet header
Definition lime_double_ratchet_protocol.hpp:35
lime::double_ratchet_protocol::DR_message_type
DR_message_type
DR message type byte bit mapping.
Definition lime_double_ratchet_protocol.hpp:130
lime::double_ratchet_protocol::DR_message_type::X3DH_init_flag
@ X3DH_init_flag
Definition lime_double_ratchet_protocol.hpp:131
lime::double_ratchet_protocol::DR_message_type::payload_direct_encryption_flag
@ payload_direct_encryption_flag
Definition lime_double_ratchet_protocol.hpp:132
lime::double_ratchet_protocol::X3DHinitSize
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
lime::double_ratchet_protocol::buildMessage_X3DHinit
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
lime
Definition lime.cpp:30