zrtp_crypto.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. /*
  2. * libZRTP SDK library, implements the ZRTP secure VoIP protocol.
  3. * Copyright (c) 2006-2009 Philip R. Zimmermann. All rights reserved.
  4. * Contact: http://philzimmermann.com
  5. * For licensing and other legal details, see the file zrtp_legal.c.
  6. *
  7. * Viktor Krykun <v.krikun at zfoneproject.com>
  8. */
  9. #ifndef __ZRTP_CRYPTO_H__
  10. #define __ZRTP_CRYPTO_H__
  11. #include "bn.h"
  12. #include "zrtp_types.h"
  13. #include "zrtp_error.h"
  14. #include "zrtp_engine.h"
  15. #include "zrtp_config_user.h"
  16. #include "zrtp_ec.h"
  17. /*!
  18. * \defgroup crypto Library crypto-components
  19. * \ingroup zrtp_dev
  20. *
  21. * This section describes functions and data types for managing crypto-components.
  22. * All these functions and structures are used by the libZRTP kernel for the
  23. * built-in crypt-components management. The developer has the option of
  24. * implementing and integrating her own components into the library. This is not
  25. * a full manual on creating crypto-components. Its purpose is only to elucidate
  26. * the library functionality.
  27. *
  28. * The concept behind crypto components is similar to that of classes in object
  29. * oriented programming. The components are defined as structures and
  30. * manipulated by functions. Component attributes are stored in 'contexts', and
  31. * are defined during initialization. Resources allocated at initialization are
  32. * freed with the 'free' function.
  33. *
  34. * Components are divided into 5 functional groups (component types):
  35. * - ciphers;
  36. * - hash/hmac components;
  37. * - public key exchange schemes;
  38. * - components defined SRTP authentication scheme;
  39. * - SAS calculation schemes.
  40. * Within a group, components are distinguished by integer identifiers and by
  41. * their defined functionality. So to fully identify a component, you need to
  42. * know its type and its identifier. (For example an AES cipher with a 128 bit
  43. * key is defined as: ZRTP_CC_CIPHER, zrtp_cipher_id_t::ZRTP_CIPHER_AES128).
  44. * The high number of components means that every component must have a minimal
  45. * set of attributes and functions: type identifier, and function initialization
  46. * and deinitialization. The base type of all components is zrtp_comp_t. Every
  47. * new component MUST start with definitions of this structure strictly in the
  48. * given order.
  49. * \warning
  50. * Every crypto-component included in libZRTP was developed and tested by
  51. * professionals. Its presence is functionally based. Using only the built-in
  52. * components gives you 100% crypto-strength and the guarantee of the fully
  53. * tested code. Never use your own components without strong reasons. If you
  54. * have noticed the absence of any important component in the library, contact
  55. * the developers. Reasonable offers will be considered for implementation in
  56. * the following versions.
  57. * \{
  58. */
  59. /*============================================================================*/
  60. /* Types of libZRTP crypto-components definitions */
  61. /*============================================================================*/
  62. /*!
  63. * \brief Enumeration for crypto-components types definition
  64. */
  65. typedef enum zrtp_crypto_comp_t
  66. {
  67. ZRTP_CC_HASH = 1, /*!< hash calculation schemes */
  68. ZRTP_CC_SAS = 2, /*!< short autentification scheme components */
  69. ZRTP_CC_CIPHER = 3, /*!< ciphers */
  70. ZRTP_CC_PKT = 4, /*!< public key exchange scheme */
  71. ZRTP_CC_ATL = 5,
  72. }zrtp_crypto_comp_t;
  73. /*!
  74. * This ID with code 0 is used as an error signal by all crypto-components
  75. * groups to indicate a wrongly defined component identifier.
  76. */
  77. #define ZRTP_COMP_UNKN 0
  78. /*! Defines types of hash functions */
  79. typedef enum zrtp_hash_id_t
  80. {
  81. ZRTP_HASH_SHA256 = 1,
  82. ZRTP_HASH_SHA384 = 2
  83. } zrtp_hash_id_t;
  84. /*! Defines types of ciphers */
  85. typedef enum zrtp_cipher_id_t
  86. {
  87. ZRTP_CIPHER_AES128 = 1,
  88. ZRTP_CIPHER_AES256 = 2
  89. } zrtp_cipher_id_t;
  90. /*! Defines SRTP authentication schemes */
  91. typedef enum zrtp_atl_id_t
  92. {
  93. ZRTP_ATL_HS32 = 1,
  94. ZRTP_ATL_HS80 = 2
  95. } zrtp_atl_id_t;
  96. /*! Defines public key exchange schemes */
  97. /* WARNING! don't change order of the PK components definitions! */
  98. typedef enum zrtp_pktype_id_t
  99. {
  100. ZRTP_PKTYPE_PRESH = 1,
  101. ZRTP_PKTYPE_MULT = 2,
  102. ZRTP_PKTYPE_DH2048 = 3,
  103. ZRTP_PKTYPE_EC256P = 4,
  104. ZRTP_PKTYPE_DH3072 = 5,
  105. ZRTP_PKTYPE_EC384P = 6,
  106. ZRTP_PKTYPE_EC521P = 7,
  107. ZRTP_PKTYPE_DH4096 = 8
  108. } zrtp_pktype_id_t;
  109. /*! Defines modes of short authentication scheme calculation */
  110. typedef enum zrtp_sas_id
  111. {
  112. ZRTP_SAS_BASE32 = 1,
  113. ZRTP_SAS_BASE256 = 2
  114. } zrtp_sas_id_t;
  115. /*!
  116. * \brief Global structure for all crypto-component types.
  117. * \warning All developed components must have these 4 fields at the beginning.
  118. */
  119. typedef struct zrtp_comp_t
  120. {
  121. zrtp_uchar4_t type; /*!< 4-character symbolic name defined by ZRTP Draft */
  122. uint8_t id; /*!< Integer component identifier */
  123. zrtp_global_t* zrtp;/*!< ZRTP global context */
  124. /*!
  125. * \brief Component initiation function.
  126. * This function body is for holding component initialization code. libzrtp
  127. * calls the function before using a component, at its registration. If the
  128. * component does not require additional actions for initialization, the
  129. * value of this field can be NULL.
  130. * \param self - self-pointer for fast access to structure data.
  131. * \return
  132. * - zrtp_status_ok - if initialized successfully;
  133. * - one of \ref zrtp_status_t errors - if initialization failed.
  134. */
  135. zrtp_status_t (*init)(void* self);
  136. /*!
  137. * \brief Component deinitializtion function.
  138. * This function body is for holding component deinitialization code and
  139. * all code for releasing allocated resources. libzrtp calls the function
  140. * at the end of component use, at context deinitialization. If the component
  141. * does not require additional actions for deinitialization, the value of
  142. * this field can be NULL.
  143. * \param self - pointer to component structure for deinitialization.
  144. * \return
  145. * - zrtp_status_ok - if deinitialized successfully;
  146. * - one of \ref zrtp_status_t errors - if deinitialization failed.
  147. */
  148. zrtp_status_t (*free)(void* self);
  149. } zrtp_comp_t;
  150. /*!
  151. * \brief Structure for defining the hash-value computing scheme
  152. * The ZRTP context field zrtp_stream#_hash is initialized by the given type
  153. * value and used for all hash calculations within the ZRTP sessions. Having
  154. * implemented a structure of this type, it is possible to integrate new hash
  155. * calculation schemes into libzrtp.
  156. */
  157. struct zrtp_hash_t
  158. {
  159. zrtp_comp_t base;
  160. /*!
  161. * \brief Begin hash computation with update support.
  162. * The following set of functions ( zrtp_hash#hash_begin, zrtp_hash#hash_update,
  163. * zrtp_hash#hash_end) implements a standard hash calculation scheme with
  164. * accumulation. The functions perform the required actions to start
  165. * calculations and to allocate hash-contexts for preserving intermediate
  166. * results and other required information. The allocated context will be
  167. * passed-to by the subsequent calls zrtp_hash#hash_update and zrtp_hash#hash_end.
  168. * \param self - self-pointer for fast access to structure data
  169. * \return
  170. * - pointer to allocated hash-context if successful;
  171. * - NULL if error.
  172. */
  173. void* (*hash_begin)(zrtp_hash_t *self);
  174. /*!
  175. * \brief Process more input data for hash calculation
  176. * This function is called in the hash-building chain to obtain additional
  177. * data that it then processes and recalculates intermediate values.
  178. * \param self - self-pointer for fast access to structure data;
  179. * \param ctx - hash-context for current hash-value calculation;
  180. * \param msg - additional source data for processing;
  181. * \param length - length of additional data in bytes.
  182. * \return
  183. * - zrtp_status_ok - if successfully processed;
  184. * - one of \ref zrtp_status_t errors - if error.
  185. */
  186. zrtp_status_t (*hash_update)( zrtp_hash_t *self,
  187. void *ctx,
  188. const int8_t*msg,
  189. uint32_t length );
  190. /*!
  191. * \brief Completes the computation of the current hash-value
  192. * This function completes the computation of the hash-value with accumul.
  193. * After completion, the hash-context previously allocated by the call to
  194. * zrtp_hash#hash_begin, must be destroyed. The size of the calculated
  195. * value must be kept in the parameter digest field zrtp_string#length.
  196. * \param self - self-pointer for fast access to structure data;
  197. * \param ctx - hash-context for current hash-value calculation;
  198. * \param digest - buffer for storing result.
  199. * \return
  200. * - zrtp_status_ok - if computing finished successfully;
  201. * - one of \ref zrtp_status_t errors - if error.
  202. */
  203. zrtp_status_t (*hash_end)( zrtp_hash_t *self,
  204. void *ctx,
  205. zrtp_stringn_t *digest );
  206. /*!
  207. * \brief Calculate hash-value for current message
  208. * This function implicitly calls the previous 3 functions. The only
  209. * difference is that initial data for hash value construction is gathered
  210. * in a single buffer and is passed to the function in the \c msg argument.
  211. * The calculated value size must be stored in the digest zrtp_string#length
  212. * parameter
  213. * \param self - self-pointer for fast access to structure data;
  214. * \param msg - source data buffer for hash computing;
  215. * \param digest - buffer for storing result.
  216. * \return
  217. * - zrtp_status_ok - if computing finished successfully;
  218. * - one of \ref zrtp_status_t errors - if error.
  219. */
  220. zrtp_status_t (*hash)( zrtp_hash_t *self,
  221. const zrtp_stringn_t *msg,
  222. zrtp_stringn_t *digest );
  223. /*! \brief Analogue of zrtp_hash::hash for C-string */
  224. zrtp_status_t (*hash_c)( zrtp_hash_t *self,
  225. const char* msg,
  226. uint32_t msg_len,
  227. zrtp_stringn_t *digest );
  228. /*!
  229. * \brief HASH self-test.
  230. * This function implements hmac self-tests using pre-defined test vectors.
  231. * \param self - self-pointer for fast access to structure data;
  232. * \return
  233. * - zrtp_status_ok - if tests have been passed successfully;
  234. * - one of \ref zrtp_status_t errors - if one or more tests have
  235. * failed.
  236. */
  237. zrtp_status_t (*hash_self_test)(zrtp_hash_t *self);
  238. /*!
  239. * \brief Begin HMAC computation with update support.
  240. * The zrtp_hash#hmac_begin, zrtp_hash#hmac_update and zrtp_hash#hmac_end
  241. * functions implement the HMAC calculation scheme with accumulation. The
  242. * function performs all actions required before beginning the calculation
  243. * and allocates a hash-context to store intermediate values. The allocated
  244. * hash-context will be passed to successive hash_update and hash_end calls
  245. * \param self - self-pointer for fast access to structure data;
  246. * \param key - secret key for hmac-value protection.
  247. * \return
  248. * - pointer to allocated hmac-context if successful;
  249. * - NULL - if error.
  250. */
  251. void* (*hmac_begin)(zrtp_hash_t *self, const zrtp_stringn_t *key);
  252. /*! \brief Analogue of zrtp_hash::hmac_begin for C-string */
  253. void* (*hmac_begin_c)(zrtp_hash_t *self, const char *key, uint32_t length);
  254. /*!
  255. * \brief Process more input data for HMAC calculation
  256. * This function is called to transfer additional data to the HMAC hash-
  257. * calculation. Processes new data and recalculates intermediate values.
  258. * \param self - self-pointer for fast access to structure data;
  259. * \param ctx - hmac-context for current hmac-value calculation;
  260. * \param msg - additional source data for processing;
  261. * \param length - additional data length in bytes.
  262. * \return
  263. * - zrtp_status_ok - if successfully processed;
  264. * - one of \ref zrtp_status_t errors - if error.
  265. */
  266. zrtp_status_t (*hmac_update)( zrtp_hash_t *self,
  267. void *ctx,
  268. const char *msg,
  269. uint32_t length );
  270. /*!
  271. * \brief Complete current HMAC-value computation
  272. * This function completes the hmac calculation. After the final iteration
  273. * \a the hash_context allocated by zrtp_hash#hmac_begin is destroyed. The
  274. * argument \c len holds the HMAC size. If the buffer contains more than \c
  275. * length characters then only the first \c length are copied to \c digest.
  276. * The calculated value size is stored in the digest parameter length.
  277. * \param self - self-pointer for fast access to structure data;
  278. * \param ctx - hmac-context for current hmac-value calculation;
  279. * \param digest - buffer for storing result;
  280. * \param len - required hmac-value size.
  281. * \return
  282. * - zrtp_status_ok - if computing finished successfully;
  283. * - one of \ref zrtp_status_t errors - if error.
  284. */
  285. zrtp_status_t (*hmac_end)( zrtp_hash_t *self,
  286. void *ctx,
  287. zrtp_stringn_t *digest,
  288. uint32_t len);
  289. /*!
  290. * \brief Calculate hmac-value for current message
  291. * The function implicitly calls the previous 3 functions
  292. * (zrtp_hash#hmac_begin, zrtp_hash#hmac_update and zrtp_hash#hmac_end). The
  293. * difference is that the initial data for hash value construction is
  294. * gathered in a single buffer and is passed to the function in the \a msg
  295. * argument. The calculated value size must be stored in the \a digest
  296. * zrtp_string#length parameter
  297. * \param self - self-pointer for fast access to structure data;
  298. * \param key - key for protecting hmac;
  299. * \param msg - source data buffer for hash computing;
  300. * \param digest - buffer for storing result.
  301. * \return
  302. * - zrtp_status_ok - if computing finished successfully;
  303. * - one of \ref zrtp_status_t errors - if error.
  304. */
  305. zrtp_status_t (*hmac)( zrtp_hash_t *self,
  306. const zrtp_stringn_t *key,
  307. const zrtp_stringn_t *msg,
  308. zrtp_stringn_t *digest );
  309. /*! \brief Analogue of zrtp_hash::hmac for C-string */
  310. zrtp_status_t (*hmac_c)( zrtp_hash_t *self,
  311. const char *key,
  312. const uint32_t key_len,
  313. const char *msg,
  314. const uint32_t msg_len,
  315. zrtp_stringn_t *digest );
  316. /*!
  317. * \brief Truncated Hmac-calculation version
  318. * This function acts just like the previous \a hmac except it returns the
  319. * first \a length bytes of the calculated value in the digest.
  320. * \param self - self-pointer for fast access to structure data;
  321. * \param key - key for hmac protection;
  322. * \param msg - source data buffer for hash computing;
  323. * \param digest - buffer for storing result;
  324. * \param len - required hmac-value size.
  325. * \return
  326. * - zrtp_status_ok - if computed successfully;
  327. * - one of \ref zrtp_status_t errors - if error.
  328. */
  329. zrtp_status_t (*hmac_truncated)( zrtp_hash_t *self,
  330. const zrtp_stringn_t *key,
  331. const zrtp_stringn_t *msg,
  332. uint32_t len,
  333. zrtp_stringn_t *digest );
  334. /*! \brief Analogue of zrtp_hash::hmac_truncated for C-string */
  335. zrtp_status_t (*hmac_truncated_c)( zrtp_hash_t *self,
  336. const char *key,
  337. const uint32_t key_len,
  338. const char *msg,
  339. const uint32_t msg_len,
  340. uint32_t necessary_len,
  341. zrtp_stringn_t *digest );
  342. /*!
  343. * \brief HMAC self-test.
  344. * This function implements the hmac self-tests using pre-defined test vectors.
  345. * \param self - self-pointer for fast access to structure data; .
  346. * \return
  347. * - zrtp_status_ok - if tests have passed successfully;
  348. * - one of \ref zrtp_status_t errors - if one or more tests have failed.
  349. */
  350. zrtp_status_t (*hmac_self_test)( zrtp_hash_t *self);
  351. uint32_t digest_length;
  352. uint32_t block_length;
  353. mlist_t mlist;
  354. };
  355. /*!
  356. * \brief Structure for defining the SRTP authentication scheme
  357. * The ZRTP context field zrtp_stream#_authtaglength is initialized by the
  358. * given type value and used for SRTP encryption configuration.
  359. */
  360. struct zrtp_auth_tag_length_t
  361. {
  362. zrtp_comp_t base;
  363. uint32_t tag_length;
  364. mlist_t mlist;
  365. };
  366. /**
  367. * @brief Structure for describing the public key scheme
  368. * The ZRTP context field zrtp_stream#_pubkeyscheme is initialized by the given
  369. * type value and used by libzrtp in public key exchange.
  370. */
  371. struct zrtp_pk_scheme_t
  372. {
  373. zrtp_comp_t base;
  374. /** Generate Diffie-Hellman secret value and Calculate public value */
  375. zrtp_status_t (*initialize)( zrtp_pk_scheme_t *self,
  376. zrtp_dh_crypto_context_t *dh_cc );
  377. /** Calculate Diffie-Hellman result (ZRTP Internet Draft) */
  378. zrtp_status_t (*compute)( zrtp_pk_scheme_t *self,
  379. zrtp_dh_crypto_context_t *dh_cc,
  380. struct BigNum *dhresult,
  381. struct BigNum *pv);
  382. /** Validate Diffie-Hellman public value */
  383. zrtp_status_t (*validate)(zrtp_pk_scheme_t *self, struct BigNum *pv);
  384. /** Diffie-Hellman self-test routine. */
  385. zrtp_status_t (*self_test)(zrtp_pk_scheme_t *self);
  386. /** Diffie-Hellman secret value size in bytes */
  387. uint32_t sv_length;
  388. /** Diffie-Hellman public value size in bytes */
  389. uint32_t pv_length;
  390. mlist_t mlist;
  391. };
  392. /*!
  393. * \brief Structure for defining SAS generation scheme
  394. * The type of the ZRTP context's field zrtp_stream#_sasscheme. It is used
  395. * to generate short authentication strings. LibZRTP functionality can be augmented
  396. * with a new SAS scheme by supplying your own instance of zrtp_sas_scheme.
  397. */
  398. struct zrtp_sas_scheme_t
  399. {
  400. zrtp_comp_t base;
  401. /*!
  402. * \brief Generate short authentication strings
  403. * This function computes SAS values according to the specified scheme. It
  404. * can use base32 or base256 algorithms. It stores the generated SAS values
  405. * as a zrtp_sas_values_t structure (string and binary representation).
  406. * \param self - self-pointer for fast access to structure data;
  407. * \param session - ZRTP session context for additional data;
  408. * \param hash - hmac component to be used for SAS calculation;
  409. * \param is_transferred - if this flag is equal to 1 new SAS value should
  410. * not be computed. It is already in sas->bin buffer and rendering only
  411. * is required.
  412. * \return
  413. * - zrtp_status_ok - if generation successful;
  414. * - one of zrtp_status_t errors - if generation failed.
  415. */
  416. zrtp_status_t (*compute)( zrtp_sas_scheme_t *self,
  417. zrtp_stream_t *stream,
  418. zrtp_hash_t *hash,
  419. uint8_t is_transferred );
  420. mlist_t mlist;
  421. };
  422. #include "aes.h"
  423. /*! Defines block cipher modes. */
  424. typedef enum zrtp_cipher_mode_values_t
  425. {
  426. ZRTP_CIPHER_MODE_CTR = 1,
  427. ZRTP_CIPHER_MODE_CFB = 2
  428. } zrtp_cipher_mode_values_t;
  429. typedef struct zrtp_cipher_mode_t
  430. {
  431. uint8_t mode;
  432. } zrtp_cipher_mode_t;
  433. /* \brief Structure for cipher definition */
  434. struct zrtp_cipher_t
  435. {
  436. zrtp_comp_t base;
  437. /*!
  438. * \brief Start cipher.
  439. * This function performs all actions necessary to allocate the cipher context
  440. * for holding intermediate results and other required information. The allocated
  441. * context should be related to the given key. It will be passed to the
  442. * zrtp_cipher#set_iv, zrtp_cipher#encrypt and zrtp_cipher#decrypt functions.
  443. * \param self - self-pointer for fast access to structure data;
  444. * \param key - cipher key;
  445. * \param extra_data - additional data necessary for cipher initialization;
  446. * \param mode - cipher mode (one of \ref zrtp_cipher_mode_values_t values).
  447. * \return
  448. * - pointer to allocated cipher context;
  449. * - NULL if error.
  450. */
  451. void* (*start)( zrtp_cipher_t *self,
  452. void *key,
  453. void *extra_data, uint8_t mode );
  454. /*!
  455. * \brief Set Initialization Vector.
  456. * Function resets the previous state of the cipher context and sets the new IV.
  457. * \param self - self-pointer for fast access to structure data;
  458. * \param cipher_ctx - cipher context for current key value;
  459. * \param iv - new initialization vector value.
  460. * \return
  461. * - zrtp_status_ok - if vector has been set successfully;
  462. * - one of \ref zrtp_status_t errors - if operation failed.
  463. */
  464. zrtp_status_t (*set_iv)( zrtp_cipher_t *self,
  465. void *cipher_ctx,
  466. zrtp_v128_t *iv );
  467. /*!
  468. * \brief Encrypt data.
  469. * Implements the encryption engine.
  470. * \param self - self-pointer for fast access to structure data;
  471. * \param cipher_ctx - cipher context for current key value;
  472. * \param buf - buffer with data for encryption. If successful this
  473. * buffer contains the resulting encrypted text;
  474. * \param len - length of plain/encrypted data.
  475. * \return
  476. * - zrtp_status_ok - if data has been encrypted successfully;
  477. * - one of \ref zrtp_status_t errors - if encryption failed.
  478. */
  479. zrtp_status_t (*encrypt)( zrtp_cipher_t *self,
  480. void *cipher_ctx,
  481. unsigned char *buf,
  482. int len );
  483. /*!
  484. * \brief Decrypt data.
  485. * Implements the decryption engine.
  486. * \param self - self-pointer for fast access to structure data;
  487. * \param cipher_ctx - cipher context for current key value;
  488. * \param buf - buffer with data for decryption. If successful this buffer
  489. * contains the resulting plain text;
  490. * \param len - length of encrypted/plain data.
  491. * \return
  492. * - zrtp_status_ok - if data has been decrypted successfully;
  493. * - one of \ref zrtp_status_t errors - if decryption failed.
  494. */
  495. zrtp_status_t (*decrypt)( zrtp_cipher_t *self,
  496. void *cipher_ctx,
  497. unsigned char *buf,
  498. int len );
  499. /*!
  500. * \brief Cipher self-test.
  501. * Implements cipher self-tests using pre-defined test vectors.
  502. * \param self - self-pointer for fast access to structure data;
  503. * \param mode - cipher mode (one of \ref zrtp_cipher_mode_values_t values).
  504. * \return
  505. * - zrtp_status_ok - if tests have passed successfully;
  506. * - one of \ref zrtp_status_t errors - if one or more tests have failed.
  507. */
  508. zrtp_status_t (*self_test)(zrtp_cipher_t *self, uint8_t mode);
  509. /*!
  510. * \brief Destroy cipher context.
  511. * Deallocs the cipher context previously allocated by a call to zrtp_cipher#start.
  512. * \param self - self-pointer for fast access to structure data;
  513. * \param cipher_ctx - cipher context for current key value.
  514. * \return
  515. * - zrtp_status_ok - if the context has been deallocated
  516. * successfully;
  517. * - one of \ref zrtp_status_t errors - if deallocation failed.
  518. */
  519. zrtp_status_t (*stop)(zrtp_cipher_t *self, void* cipher_ctx);
  520. mlist_t mlist;
  521. };
  522. #if defined(__cplusplus)
  523. extern "C"
  524. {
  525. #endif
  526. /*============================================================================*/
  527. /* Crypto-components management Private part */
  528. /*============================================================================*/
  529. /*!
  530. * \brief Destroy components buffer
  531. * This function clears the list of components of the specified type, destroys
  532. * all components and releases all allocated resources. It is used on libzrtp
  533. * down. zrtp_comp_done calls zrtp_comp_t#free() if it isn't NULL.
  534. * \param zrtp - the ZRTP global context where components are stored;
  535. * \param type - specifies the component pool type for destroying.
  536. * \return
  537. * - zrtp_status_ok - if clearing successful;
  538. * - zrtp_status_fail - if error.
  539. */
  540. zrtp_status_t zrtp_comp_done(zrtp_crypto_comp_t type, zrtp_global_t* zrtp);
  541. /*!
  542. * \brief Registering a new crypto-component
  543. * Correctness of values in the necessary structure is the developer's
  544. * responsibility. zrtp_comp_register calls zrtp_comp_t#init() if it isn't NULL.
  545. * \param type - type of registred component;
  546. * \param comp - registered crypto-component;
  547. * \param zrtp - the ZRTP global context where components are stored.
  548. * \return
  549. * - zrtp_status_ok if registration successful;
  550. * - zrtp_status_fail if error (conflicts with other components).
  551. */
  552. zrtp_status_t zrtp_comp_register( zrtp_crypto_comp_t type,
  553. void *comp,
  554. zrtp_global_t* zrtp);
  555. /*!
  556. * \brief Search component by ID
  557. * \param type - type of sought component;
  558. * \param zrtp - the ZRTP global context where components are stored;
  559. * \param id - integer identifier of the necessary element.
  560. * \return
  561. * - the found structure if successful;
  562. * - NULL if the element with the specified ID can't be found or
  563. * other error.
  564. */
  565. void* zrtp_comp_find( zrtp_crypto_comp_t type,
  566. uint8_t id,
  567. zrtp_global_t* zrtp);
  568. /*! Converts a component's integer ID to a symbolic ZRTP name */
  569. char* zrtp_comp_id2type(zrtp_crypto_comp_t type, uint8_t id);
  570. /*! Converts a component's ZRTP symbolic name to an integer ID */
  571. uint8_t zrtp_comp_type2id(zrtp_crypto_comp_t type, char* name);
  572. /*! \} */
  573. #if defined(__cplusplus)
  574. }
  575. #endif
  576. #endif /*__ZRTP_CRYPTO_H__ */