bZRTP
bZRTP is an opensource implementation of ZRTP keys exchange protocol
cryptoUtils.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2019 Belledonne Communications SARL.
3  *
4  * This file is part of bzrtp.
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 #ifndef CRYPTOUTILS_H
20 #define CRYPTOUTILS_H
21 
22 #include "typedef.h"
23 #include "packetParser.h"
24 
25 #ifdef __cplusplus
26 extern "C"{
27 #endif
28 
38 uint8_t bzrtpUtils_getAvailableCryptoTypes(uint8_t algoType, uint8_t availableTypes[7]);
39 
44 uint8_t bzrtpUtils_getAllAvailableCryptoTypes(uint8_t algoType, uint8_t availableTypes[256]);
45 
70 BZRTP_EXPORT int bzrtp_keyDerivationFunction(const uint8_t *key, const size_t keyLength,
71  const uint8_t *label, const size_t labelLength,
72  const uint8_t *context, const size_t contextLength,
73  const uint8_t hmacLength,
74  void (*hmacFunction)(const uint8_t *, size_t, const uint8_t *, size_t, uint8_t, uint8_t *),
75  uint8_t *output);
76 
77 
87 void bzrtp_base32(uint32_t sas, char *output, int outputSize);
88 
98 void bzrtp_base256(uint32_t sas, char *output, int outputSize);
99 
114 
115 /* error code for the cryptoAlgoAgreement and function pointer update functions */
116 #define ZRTP_CRYPTOAGREEMENT_INVALIDCONTEXT 0x1001
117 #define ZRTP_CRYPTOAGREEMENT_INVALIDMESSAGE 0x1002
118 #define ZRTP_CRYPTOAGREEMENT_INVALIDSELFALGO 0x1003
119 #define ZRTP_CRYPTOAGREEMENT_NOCOMMONALGOFOUND 0x1004
120 #define ZRTP_CRYPTOAGREEMENT_INVALIDCIPHER 0x1005
121 #define ZRTP_CRYPTOAGREEMENT_INVALIDHASH 0x1006
122 #define ZRTP_CRYPTOAGREEMENT_INVALIDAUTHTAG 0x1007
123 #define ZRTP_CRYPTOAGREEMENT_INVALIDSAS 0x1008
124 
131 
146 BZRTP_EXPORT int bzrtp_cryptoAlgoAgreement(bzrtpContext_t *zrtpContext, bzrtpChannelContext_t *zrtpChannelContext, bzrtpHelloMessage_t *peerHelloMessage);
147 
156 
170 uint8_t bzrtp_selectCommonAlgo(uint8_t masterArray[7], uint8_t masterArrayLength, uint8_t *slaveArray, uint8_t slaveArrayLength, uint8_t commonArray[7]);
171 
184 BZRTP_EXPORT void bzrtp_addMandatoryCryptoTypesIfNeeded(uint8_t algoType, uint8_t algoTypes[7], uint8_t *algoTypesCount);
185 
195 
202 BZRTP_EXPORT void bzrtp_cryptoAlgoTypeIntToString(uint8_t algoTypeInt, uint8_t algoTypeString[4]);
203 
213 BZRTP_EXPORT void bzrtp_DestroyKey(uint8_t *key, size_t keyLength, void *rngContext);
214 
221 void bzrtp_destroyKeyMaterial(bzrtpContext_t *zrtpContext, bzrtpChannelContext_t *zrtpChannelContext);
222 
237 
247 
255 
265 
266 /* have a C interface to the KEM defined in c++ in bctoolbox
267  TODO: build bzrtp in c++ and directly use the bctoolbox interface */
268 /* Forward declaration of KEM context */
269 typedef struct bzrtp_KEMContext_struct bzrtp_KEMContext_t;
270 
282 bzrtp_KEMContext_t *bzrtp_createKEMContext(uint8_t keyAgreementAlgo, uint8_t hashAlgo);
283 
290 
299 
307 int bzrtp_KEM_getSharedSecret(bzrtp_KEMContext_t *ctx, uint8_t *sharedSecret);
308 
317 int bzrtp_KEM_encaps(bzrtp_KEMContext_t *ctx, uint8_t *publicKey, uint8_t *cipherText);
318 
327 int bzrtp_KEM_decaps(bzrtp_KEMContext_t *ctx, uint8_t *cipherText);
328 
336 #ifdef __cplusplus
337 }
338 #endif
339 
340 #endif /* CRYPTOUTILS_H */
#define BZRTP_EXPORT
Definition: bzrtp.h:41
uint16_t bzrtp_computeCommitMessageVariableLength(uint8_t keyAgreementAlgo)
Definition: cryptoUtils.cc:1320
int bzrtp_destroyKEMContext(bzrtp_KEMContext_t *ctx)
Definition: cryptoUtils.cc:1552
int bzrtp_KEM_generateKeyPair(bzrtp_KEMContext_t *ctx)
Definition: cryptoUtils.cc:1532
void bzrtp_destroyKeyMaterial(bzrtpContext_t *zrtpContext, bzrtpChannelContext_t *zrtpChannelContext)
Destroy all the key material of a channel context.
Definition: cryptoUtils.cc:1076
unsigned short uint16_t
Definition: stdint.h:79
struct bzrtp_KEMContext_struct bzrtp_KEMContext_t
Definition: cryptoUtils.h:269
unsigned char uint8_t
Definition: stdint.h:78
int bzrtp_KEM_getPublicKey(bzrtp_KEMContext_t *ctx, uint8_t *publicKey)
Definition: cryptoUtils.cc:1536
int bzrtp_KEM_getSharedSecret(bzrtp_KEMContext_t *ctx, uint8_t *sharedSecret)
Definition: cryptoUtils.cc:1540
int bzrtp_KEM_encaps(bzrtp_KEMContext_t *ctx, uint8_t *publicKey, uint8_t *cipherText)
Definition: cryptoUtils.cc:1544
uint8_t keyAgreementAlgo
Definition: typedef.h:239
void bzrtp_base256(uint32_t sas, char *output, int outputSize)
SAS rendering from 32 bits to pgp word list Function defined in rfc section 5.1.6.
Definition: cryptoUtils.cc:286
BZRTP_EXPORT int bzrtp_cryptoAlgoAgreement(bzrtpContext_t *zrtpContext, bzrtpChannelContext_t *zrtpChannelContext, bzrtpHelloMessage_t *peerHelloMessage)
select a key agreement algorithm from the one available in context and the one provided by peer in He...
Definition: cryptoUtils.cc:427
BZRTP_EXPORT int bzrtp_updateCryptoFunctionPointers(bzrtpChannelContext_t *zrtpChannelContext)
Update context crypto function pointer according to related values of choosen algorithms fields (hash...
Definition: cryptoUtils.cc:588
structure of the ZRTP engine context Store current state, timers, HMAC and encryption keys ...
Definition: typedef.h:235
unsigned int uint32_t
Definition: stdint.h:80
bzrtp_KEMContext_t * bzrtp_createKEMContext(uint8_t keyAgreementAlgo, uint8_t hashAlgo)
Definition: cryptoUtils.cc:1529
bool_t bzrtp_isPostQuantum(uint8_t keyAgreementAlgo)
Check if the keyAgreementAlgo is a post quantum algorithm.
Definition: cryptoUtils.cc:394
BZRTP_EXPORT uint16_t bzrtp_computeKeyAgreementPublicValueLength(uint8_t keyAgreementAlgo, uint8_t messageTyoe)
Definition: cryptoUtils.cc:1209
uint8_t bzrtpUtils_getAllAvailableCryptoTypes(uint8_t algoType, uint8_t availableTypes[256])
Definition: cryptoUtils.cc:39
BZRTP_EXPORT void bzrtp_cryptoAlgoTypeIntToString(uint8_t algoTypeInt, uint8_t algoTypeString[4])
Unmap the string description of algo type to an int defined in cryptoWrapper.h.
Definition: cryptoUtils.cc:931
BZRTP_EXPORT uint32_t bzrtp_CRC32(uint8_t *input, uint16_t length)
CRC32 as defined in RFC4960 Appendix B - Polynomial is 0x1EDC6F41.
Definition: cryptoUtils.cc:363
bool_t bzrtp_isKem(uint8_t keyAgreementAlgo)
Definition: cryptoUtils.cc:1298
int bzrtp_KEM_decaps(bzrtp_KEMContext_t *ctx, uint8_t *cipherText)
Definition: cryptoUtils.cc:1548
BZRTP_EXPORT void bzrtp_addMandatoryCryptoTypesIfNeeded(uint8_t algoType, uint8_t algoTypes[7], uint8_t *algoTypesCount)
add mandatory crypto functions if they are not already included
Definition: cryptoUtils.cc:750
Hello Message rfc 5.2.
Definition: packetParser.h:152
uint8_t bzrtp_selectCommonAlgo(uint8_t masterArray[7], uint8_t masterArrayLength, uint8_t *slaveArray, uint8_t slaveArrayLength, uint8_t commonArray[7])
Select common algorithm from the given array where algo are represented by their 4 chars string defin...
Definition: cryptoUtils.cc:713
void bzrtp_base32(uint32_t sas, char *output, int outputSize)
SAS rendering from 32 bits to 4 characters Function defined in rfc section 5.1.6. ...
Definition: cryptoUtils.cc:274
BZRTP_EXPORT uint8_t bzrtp_cryptoAlgoTypeStringToInt(uint8_t algoType[4], uint8_t algoFamily)
Map the string description of algo type to an int defined in cryptoWrapper.h.
Definition: cryptoUtils.cc:801
The zrtp context of a channel.
Definition: typedef.h:157
BZRTP_EXPORT int bzrtp_keyDerivationFunction(const uint8_t *key, const size_t keyLength, const uint8_t *label, const size_t labelLength, const uint8_t *context, const size_t contextLength, const uint8_t hmacLength, void(*hmacFunction)(const uint8_t *, size_t, const uint8_t *, size_t, uint8_t, uint8_t *), uint8_t *output)
ZRTP Key Derivation Function as in rfc 4.5.1.
Definition: cryptoUtils.cc:227
uint8_t bzrtpUtils_getAvailableCryptoTypes(uint8_t algoType, uint8_t availableTypes[7])
Definition: cryptoUtils.cc:175
uint16_t bzrtp_computeKeyAgreementSharedSecretLength(uint8_t keyAgreementAlgo, uint8_t hashLength)
Definition: cryptoUtils.cc:1252
BZRTP_EXPORT void bzrtp_DestroyKey(uint8_t *key, size_t keyLength, void *rngContext)
Destroy a key by setting it to a random number Key is not freed, caller must deal with memory managem...
Definition: cryptoUtils.cc:1070