bZRTP
bZRTP is an opensource implementation of ZRTP keys exchange protocol
Macros | Functions
packetParser.c File Reference

Macros

#define ZRTP_MIN_PACKET_LENGTH   28
 
#define ZRTP_MAX_PACKET_LENGTH   3072
 
#define ZRTP_MESSAGE_HEADER_LENGTH   12
 
#define ZRTP_HELLOMESSAGE_FIXED_LENGTH   88
 
#define ZRTP_HELLOACKMESSAGE_FIXED_LENGTH   12
 
#define ZRTP_COMMITMESSAGE_FIXED_LENGTH   84
 
#define ZRTP_DHPARTMESSAGE_FIXED_LENGTH   84
 
#define ZRTP_CONFIRMMESSAGE_FIXED_LENGTH   76
 
#define ZRTP_CONF2ACKMESSAGE_FIXED_LENGTH   12
 
#define ZRTP_ERRORMESSAGE_FIXED_LENGTH   16
 
#define ZRTP_ERRORACKMESSAGE_FIXED_LENGTH   12
 
#define ZRTP_GOCLEARMESSAGE_FIXED_LENGTH   20
 
#define ZRTP_CLEARACKMESSAGE_FIXED_LENGTH   12
 
#define ZRTP_SASRELAYMESSAGE_FIXED_LENGTH   76
 
#define ZRTP_RELAYACKMESSAGE_FIXED_LENGTH   12
 
#define ZRTP_PINGMESSAGE_FIXED_LENGTH   24
 
#define ZRTP_PINGACKMESSAGE_FIXED_LENGTH   36
 

Functions

uint16_t computeKeyAgreementPrivateValueLength (uint8_t keyAgreementAlgo)
 
uint8_tmessageTypeInttoString (uint32_t messageType)
 Retrieve the 8 char string value message type from the int32_t code. More...
 
int32_t messageTypeStringtoInt (uint8_t messageTypeString[8])
 Map the 8 char string value message type to an int32_t. More...
 
void zrtpMessageSetHeader (uint8_t *outputBuffer, uint16_t messageLength, uint8_t messageType[8])
 Write the message header(preambule, length, message type) into the given output buffer. More...
 
bzrtpPacket_tbzrtp_packetCheck (const uint8_t *input, uint16_t inputLength, uint16_t lastValidSequenceNumber, int *exitCode)
 Parse a string which shall be a valid ZRTP packet Check validity and allocate the bzrtpPacket structure but do not parse the message except for type and length. messageData structure field is not allocated by this function (use then bzrtp_packetParse for that). The packet check and actual message parsing are split in two functions to avoid useless parsing when message is to be discarded as the check will give message type (in case of message repetition for example) More...
 
int bzrtp_packetParser (bzrtpContext_t *zrtpContext, bzrtpChannelContext_t *zrtpChannelContext, const uint8_t *input, uint16_t inputLength, bzrtpPacket_t *zrtpPacket)
 Parse the packet to extract the message and allocate the matching message structure if needed. More...
 
int bzrtp_packetBuild (bzrtpContext_t *zrtpContext, bzrtpChannelContext_t *zrtpChannelContext, bzrtpPacket_t *zrtpPacket, uint16_t sequenceNumber)
 Create a ZRTP packet string from the ZRTP packet values present in the structure messageType, messageData and sourceIdentifier in zrtpPacket must have been correctly set before calling this function. More...
 
bzrtpPacket_tbzrtp_createZrtpPacket (bzrtpContext_t *zrtpContext, bzrtpChannelContext_t *zrtpChannelContext, uint32_t messageType, int *exitCode)
 Create an empty packet and allocate the messageData according to requested packetType. More...
 
void bzrtp_freeZrtpPacket (bzrtpPacket_t *zrtpPacket)
 Deallocate zrtp Packet. More...
 
int bzrtp_packetUpdateSequenceNumber (bzrtpPacket_t *zrtpPacket, uint16_t sequenceNumber)
 Modify the current sequence number of the packet in the packetString and sequenceNumber fields The CRC at the end of packetString is also updated. More...
 

Macro Definition Documentation

#define ZRTP_CLEARACKMESSAGE_FIXED_LENGTH   12
#define ZRTP_COMMITMESSAGE_FIXED_LENGTH   84
#define ZRTP_CONF2ACKMESSAGE_FIXED_LENGTH   12
#define ZRTP_CONFIRMMESSAGE_FIXED_LENGTH   76
#define ZRTP_DHPARTMESSAGE_FIXED_LENGTH   84
#define ZRTP_ERRORACKMESSAGE_FIXED_LENGTH   12
#define ZRTP_ERRORMESSAGE_FIXED_LENGTH   16
#define ZRTP_GOCLEARMESSAGE_FIXED_LENGTH   20
#define ZRTP_HELLOACKMESSAGE_FIXED_LENGTH   12
#define ZRTP_HELLOMESSAGE_FIXED_LENGTH   88
#define ZRTP_MAX_PACKET_LENGTH   3072
#define ZRTP_MESSAGE_HEADER_LENGTH   12
#define ZRTP_MIN_PACKET_LENGTH   28
#define ZRTP_PINGACKMESSAGE_FIXED_LENGTH   36
#define ZRTP_PINGMESSAGE_FIXED_LENGTH   24
#define ZRTP_RELAYACKMESSAGE_FIXED_LENGTH   12
#define ZRTP_SASRELAYMESSAGE_FIXED_LENGTH   76

Function Documentation

bzrtpPacket_t* bzrtp_createZrtpPacket ( bzrtpContext_t zrtpContext,
bzrtpChannelContext_t zrtpChannelContext,
uint32_t  messageType,
int *  exitCode 
)

Create an empty packet and allocate the messageData according to requested packetType.

Parameters
[in]zrtpContextThe current ZRTP context, some data (H chain or others, may be needed to create messages)
[in]zrtpChannelContextThe channel context this packet is intended to
[in]messageTypeThe 32bit integer mapped to the message type to be created
[out]exitCode0 on success, error code otherwise
Returns
An empty packet initialised to get data for the requested paquet tyep. NULL on error
void bzrtp_freeZrtpPacket ( bzrtpPacket_t zrtpPacket)

Deallocate zrtp Packet.

Parameters
[in]zrtpPacketThe packet to be freed
int bzrtp_packetBuild ( bzrtpContext_t zrtpContext,
bzrtpChannelContext_t zrtpChannelContext,
bzrtpPacket_t zrtpPacket,
uint16_t  sequenceNumber 
)

Create a ZRTP packet string from the ZRTP packet values present in the structure messageType, messageData and sourceIdentifier in zrtpPacket must have been correctly set before calling this function.

Parameters
[in]zrtpContextA zrtp context where to find H0-H3 to compute MAC requested by some paquets or encryption's key for commit/SASRelay packet
[in]zrtpChannelContextThe channel context this packet is intended to
[in,out]zrtpPacketThe zrtpPacket structure containing the message Data structure, output is stored in ->packetString
[in]sequenceNumberSequence number of this packet
Returns
0 on success, error code otherwise
bzrtpPacket_t* bzrtp_packetCheck ( const uint8_t input,
uint16_t  inputLength,
uint16_t  lastValidSequenceNumber,
int *  exitCode 
)

Parse a string which shall be a valid ZRTP packet Check validity and allocate the bzrtpPacket structure but do not parse the message except for type and length. messageData structure field is not allocated by this function (use then bzrtp_packetParse for that). The packet check and actual message parsing are split in two functions to avoid useless parsing when message is to be discarded as the check will give message type (in case of message repetition for example)

Parameters
[in]inputThe string buffer storing the complete ZRTP packet
[in]inputLengthInput length in bytes
[in]lastValidSequenceNumberIf the sequence number of this packet is smaller than this param, packet will be discarded and an error code returned
[out]exitCode0 on success, error code otherwise
Returns
The create bzrtpPacket structure(to be freed using bzrtp_freeZrtpPacket). NULL on error
int bzrtp_packetParser ( bzrtpContext_t zrtpContext,
bzrtpChannelContext_t zrtpChannelContext,
const uint8_t input,
uint16_t  inputLength,
bzrtpPacket_t zrtpPacket 
)

Parse the packet to extract the message and allocate the matching message structure if needed.

Parameters
[in]zrtpContextThe current ZRTP context, some parameters(key agreement algorithm) may be needed to parse packet.
[in]zrtpChannelContextThe channel context this packet is intended to(channel context and packet must match peer SSRC).
[in]inputThe string buffer storing the complete ZRTP packet
[in]inputLengthInput length in bytes
[in]zrtpPacketThe zrtpPacket structure allocated by previous call to bzrtpPacketCheck
Returns
0 on sucess, error code otherwise
int bzrtp_packetUpdateSequenceNumber ( bzrtpPacket_t zrtpPacket,
uint16_t  sequenceNumber 
)

Modify the current sequence number of the packet in the packetString and sequenceNumber fields The CRC at the end of packetString is also updated.

param[in,out] zrtpPacket The zrtpPacket to modify, the packetString must have been generated by a call to bzrtp_packetBuild on this packet param[in] sequenceNumber The new sequence number to insert in the packetString

return 0 on succes, error code otherwise

uint16_t computeKeyAgreementPrivateValueLength ( uint8_t  keyAgreementAlgo)

Return the variable private value length in bytes according to given key agreement algorythm

Parameters
[in]keyAgreementAlgoThe key agreement algo mapped to an integer as defined in cryptoWrapper.h
Returns
the private value length in bytes
uint8_t * messageTypeInttoString ( uint32_t  messageType)

Retrieve the 8 char string value message type from the int32_t code.

Parameters
[in]messageTypeThe messageType code
Returns
an 9 char string : 8 chars message type as specified in rfc section 5.1.1 + string terminating char
int32_t messageTypeStringtoInt ( uint8_t  messageTypeString[8])

Map the 8 char string value message type to an int32_t.

Parameters
[in]messageTypeStringan 8 bytes string matching a zrtp message type
Returns
a 32-bits unsigned integer mapping the message type
void zrtpMessageSetHeader ( uint8_t outputBuffer,
uint16_t  messageLength,
uint8_t  messageType[8] 
)

Write the message header(preambule, length, message type) into the given output buffer.

Parameters
[out]outputBufferMessage starts at the begining of this buffer
[in]messageLengthMessage length in bytes! To be converted into 32bits words before being inserted in the message header
[in]messageTypeAn 8 chars string for the message type (validity is not checked by this function)