PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/include/polarssl/cipher.h

https://github.com/leg0/polarssl
C Header | 463 lines | 164 code | 62 blank | 237 comment | 28 complexity | 1c77d5e4ef13e89227f816e5b00ee599 MD5 | raw file
Possible License(s): GPL-2.0
  1. /**
  2. * \file cipher.h
  3. *
  4. * \brief Generic cipher wrapper.
  5. *
  6. * \author Adriaan de Jong <dejong@fox-it.com>
  7. *
  8. * Copyright (C) 2006-2012, Brainspark B.V.
  9. *
  10. * This file is part of PolarSSL (http://www.polarssl.org)
  11. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  12. *
  13. * All rights reserved.
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License along
  26. * with this program; if not, write to the Free Software Foundation, Inc.,
  27. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  28. */
  29. #ifndef POLARSSL_CIPHER_H
  30. #define POLARSSL_CIPHER_H
  31. #include <string.h>
  32. #if defined(_MSC_VER) && !defined(inline)
  33. #define inline _inline
  34. #else
  35. #if defined(__ARMCC_VERSION) && !defined(inline)
  36. #define inline __inline
  37. #endif /* __ARMCC_VERSION */
  38. #endif /*_MSC_VER */
  39. #define POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE -0x6080 /**< The selected feature is not available. */
  40. #define POLARSSL_ERR_CIPHER_BAD_INPUT_DATA -0x6100 /**< Bad input parameters to function. */
  41. #define POLARSSL_ERR_CIPHER_ALLOC_FAILED -0x6180 /**< Failed to allocate memory. */
  42. #define POLARSSL_ERR_CIPHER_INVALID_PADDING -0x6200 /**< Input data contains invalid padding and is rejected. */
  43. #define POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED -0x6280 /**< Decryption of block requires a full block. */
  44. typedef enum {
  45. POLARSSL_CIPHER_ID_NONE = 0,
  46. POLARSSL_CIPHER_ID_NULL,
  47. POLARSSL_CIPHER_ID_AES,
  48. POLARSSL_CIPHER_ID_DES,
  49. POLARSSL_CIPHER_ID_3DES,
  50. POLARSSL_CIPHER_ID_CAMELLIA,
  51. POLARSSL_CIPHER_ID_BLOWFISH,
  52. } cipher_id_t;
  53. typedef enum {
  54. POLARSSL_CIPHER_NONE = 0,
  55. POLARSSL_CIPHER_NULL,
  56. POLARSSL_CIPHER_AES_128_CBC,
  57. POLARSSL_CIPHER_AES_192_CBC,
  58. POLARSSL_CIPHER_AES_256_CBC,
  59. POLARSSL_CIPHER_AES_128_CFB128,
  60. POLARSSL_CIPHER_AES_192_CFB128,
  61. POLARSSL_CIPHER_AES_256_CFB128,
  62. POLARSSL_CIPHER_AES_128_CTR,
  63. POLARSSL_CIPHER_AES_192_CTR,
  64. POLARSSL_CIPHER_AES_256_CTR,
  65. POLARSSL_CIPHER_CAMELLIA_128_CBC,
  66. POLARSSL_CIPHER_CAMELLIA_192_CBC,
  67. POLARSSL_CIPHER_CAMELLIA_256_CBC,
  68. POLARSSL_CIPHER_CAMELLIA_128_CFB128,
  69. POLARSSL_CIPHER_CAMELLIA_192_CFB128,
  70. POLARSSL_CIPHER_CAMELLIA_256_CFB128,
  71. POLARSSL_CIPHER_CAMELLIA_128_CTR,
  72. POLARSSL_CIPHER_CAMELLIA_192_CTR,
  73. POLARSSL_CIPHER_CAMELLIA_256_CTR,
  74. POLARSSL_CIPHER_DES_CBC,
  75. POLARSSL_CIPHER_DES_EDE_CBC,
  76. POLARSSL_CIPHER_DES_EDE3_CBC,
  77. POLARSSL_CIPHER_BLOWFISH_CBC,
  78. POLARSSL_CIPHER_BLOWFISH_CFB64,
  79. POLARSSL_CIPHER_BLOWFISH_CTR,
  80. } cipher_type_t;
  81. typedef enum {
  82. POLARSSL_MODE_NONE = 0,
  83. POLARSSL_MODE_NULL,
  84. POLARSSL_MODE_CBC,
  85. POLARSSL_MODE_CFB,
  86. POLARSSL_MODE_OFB,
  87. POLARSSL_MODE_CTR,
  88. } cipher_mode_t;
  89. typedef enum {
  90. POLARSSL_OPERATION_NONE = -1,
  91. POLARSSL_DECRYPT = 0,
  92. POLARSSL_ENCRYPT,
  93. } operation_t;
  94. enum {
  95. /** Undefined key length */
  96. POLARSSL_KEY_LENGTH_NONE = 0,
  97. /** Key length, in bits (including parity), for DES keys */
  98. POLARSSL_KEY_LENGTH_DES = 64,
  99. /** Key length, in bits (including parity), for DES in two key EDE */
  100. POLARSSL_KEY_LENGTH_DES_EDE = 128,
  101. /** Key length, in bits (including parity), for DES in three-key EDE */
  102. POLARSSL_KEY_LENGTH_DES_EDE3 = 192,
  103. /** Maximum length of any IV, in bytes */
  104. POLARSSL_MAX_IV_LENGTH = 16,
  105. };
  106. /**
  107. * Base cipher information. The non-mode specific functions and values.
  108. */
  109. typedef struct {
  110. /** Base Cipher type (e.g. POLARSSL_CIPHER_ID_AES) */
  111. cipher_id_t cipher;
  112. /** Encrypt using CBC */
  113. int (*cbc_func)( void *ctx, operation_t mode, size_t length, unsigned char *iv,
  114. const unsigned char *input, unsigned char *output );
  115. /** Encrypt using CFB (Full length) */
  116. int (*cfb_func)( void *ctx, operation_t mode, size_t length, size_t *iv_off,
  117. unsigned char *iv, const unsigned char *input, unsigned char *output );
  118. /** Encrypt using CTR */
  119. int (*ctr_func)( void *ctx, size_t length, size_t *nc_off, unsigned char *nonce_counter,
  120. unsigned char *stream_block, const unsigned char *input, unsigned char *output );
  121. /** Set key for encryption purposes */
  122. int (*setkey_enc_func)( void *ctx, const unsigned char *key, unsigned int key_length);
  123. /** Set key for decryption purposes */
  124. int (*setkey_dec_func)( void *ctx, const unsigned char *key, unsigned int key_length);
  125. /** Allocate a new context */
  126. void * (*ctx_alloc_func)( void );
  127. /** Free the given context */
  128. void (*ctx_free_func)( void *ctx );
  129. } cipher_base_t;
  130. /**
  131. * Cipher information. Allows cipher functions to be called in a generic way.
  132. */
  133. typedef struct {
  134. /** Full cipher identifier (e.g. POLARSSL_CIPHER_AES_256_CBC) */
  135. cipher_type_t type;
  136. /** Cipher mode (e.g. POLARSSL_MODE_CBC) */
  137. cipher_mode_t mode;
  138. /** Cipher key length, in bits (default length for variable sized ciphers)
  139. * (Includes parity bits for ciphers like DES) */
  140. unsigned int key_length;
  141. /** Name of the cipher */
  142. const char * name;
  143. /** IV size, in bytes */
  144. unsigned int iv_size;
  145. /** block size, in bytes */
  146. unsigned int block_size;
  147. /** Base cipher information and functions */
  148. const cipher_base_t *base;
  149. } cipher_info_t;
  150. /**
  151. * Generic cipher context.
  152. */
  153. typedef struct {
  154. /** Information about the associated cipher */
  155. const cipher_info_t *cipher_info;
  156. /** Key length to use */
  157. int key_length;
  158. /** Operation that the context's key has been initialised for */
  159. operation_t operation;
  160. /** Buffer for data that hasn't been encrypted yet */
  161. unsigned char unprocessed_data[POLARSSL_MAX_IV_LENGTH];
  162. /** Number of bytes that still need processing */
  163. size_t unprocessed_len;
  164. /** Current IV or NONCE_COUNTER for CTR-mode */
  165. unsigned char iv[POLARSSL_MAX_IV_LENGTH];
  166. /** Cipher-specific context */
  167. void *cipher_ctx;
  168. } cipher_context_t;
  169. #ifdef __cplusplus
  170. extern "C" {
  171. #endif
  172. /**
  173. * \brief Returns the list of ciphers supported by the generic cipher module.
  174. *
  175. * \return a statically allocated array of ciphers, the last entry
  176. * is 0.
  177. */
  178. const int *cipher_list( void );
  179. /**
  180. * \brief Returns the cipher information structure associated
  181. * with the given cipher name.
  182. *
  183. * \param cipher_name Name of the cipher to search for.
  184. *
  185. * \return the cipher information structure associated with the
  186. * given cipher_name, or NULL if not found.
  187. */
  188. const cipher_info_t *cipher_info_from_string( const char *cipher_name );
  189. /**
  190. * \brief Returns the cipher information structure associated
  191. * with the given cipher type.
  192. *
  193. * \param cipher_type Type of the cipher to search for.
  194. *
  195. * \return the cipher information structure associated with the
  196. * given cipher_type, or NULL if not found.
  197. */
  198. const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type );
  199. /**
  200. * \brief Initialises and fills the cipher context structure with
  201. * the appropriate values.
  202. *
  203. * \param ctx context to initialise. May not be NULL.
  204. * \param cipher_info cipher to use.
  205. *
  206. * \return \c 0 on success,
  207. * \c POLARSSL_ERR_CIPHER_BAD_INPUT_DATA on parameter failure,
  208. * \c POLARSSL_ERR_CIPHER_ALLOC_FAILED if allocation of the
  209. * cipher-specific context failed.
  210. */
  211. int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info );
  212. /**
  213. * \brief Free the cipher-specific context of ctx. Freeing ctx
  214. * itself remains the responsibility of the caller.
  215. *
  216. * \param ctx Free the cipher-specific context
  217. *
  218. * \returns 0 on success, POLARSSL_ERR_CIPHER_BAD_INPUT_DATA if
  219. * parameter verification fails.
  220. */
  221. int cipher_free_ctx( cipher_context_t *ctx );
  222. /**
  223. * \brief Returns the block size of the given cipher.
  224. *
  225. * \param ctx cipher's context. Must have been initialised.
  226. *
  227. * \return size of the cipher's blocks, or 0 if ctx has not been
  228. * initialised.
  229. */
  230. static inline unsigned int cipher_get_block_size( const cipher_context_t *ctx )
  231. {
  232. if( NULL == ctx || NULL == ctx->cipher_info )
  233. return 0;
  234. return ctx->cipher_info->block_size;
  235. }
  236. /**
  237. * \brief Returns the mode of operation for the cipher.
  238. * (e.g. POLARSSL_MODE_CBC)
  239. *
  240. * \param ctx cipher's context. Must have been initialised.
  241. *
  242. * \return mode of operation, or POLARSSL_MODE_NONE if ctx
  243. * has not been initialised.
  244. */
  245. static inline cipher_mode_t cipher_get_cipher_mode( const cipher_context_t *ctx )
  246. {
  247. if( NULL == ctx || NULL == ctx->cipher_info )
  248. return POLARSSL_MODE_NONE;
  249. return ctx->cipher_info->mode;
  250. }
  251. /**
  252. * \brief Returns the size of the cipher's IV.
  253. *
  254. * \param ctx cipher's context. Must have been initialised.
  255. *
  256. * \return size of the cipher's IV, or 0 if ctx has not been
  257. * initialised.
  258. */
  259. static inline int cipher_get_iv_size( const cipher_context_t *ctx )
  260. {
  261. if( NULL == ctx || NULL == ctx->cipher_info )
  262. return 0;
  263. return ctx->cipher_info->iv_size;
  264. }
  265. /**
  266. * \brief Returns the type of the given cipher.
  267. *
  268. * \param ctx cipher's context. Must have been initialised.
  269. *
  270. * \return type of the cipher, or POLARSSL_CIPHER_NONE if ctx has
  271. * not been initialised.
  272. */
  273. static inline cipher_type_t cipher_get_type( const cipher_context_t *ctx )
  274. {
  275. if( NULL == ctx || NULL == ctx->cipher_info )
  276. return POLARSSL_CIPHER_NONE;
  277. return ctx->cipher_info->type;
  278. }
  279. /**
  280. * \brief Returns the name of the given cipher, as a string.
  281. *
  282. * \param ctx cipher's context. Must have been initialised.
  283. *
  284. * \return name of the cipher, or NULL if ctx was not initialised.
  285. */
  286. static inline const char *cipher_get_name( const cipher_context_t *ctx )
  287. {
  288. if( NULL == ctx || NULL == ctx->cipher_info )
  289. return 0;
  290. return ctx->cipher_info->name;
  291. }
  292. /**
  293. * \brief Returns the key length of the cipher.
  294. *
  295. * \param ctx cipher's context. Must have been initialised.
  296. *
  297. * \return cipher's key length, in bits, or
  298. * POLARSSL_KEY_LENGTH_NONE if ctx has not been
  299. * initialised.
  300. */
  301. static inline int cipher_get_key_size ( const cipher_context_t *ctx )
  302. {
  303. if( NULL == ctx )
  304. return POLARSSL_KEY_LENGTH_NONE;
  305. return ctx->key_length;
  306. }
  307. /**
  308. * \brief Returns the operation of the given cipher.
  309. *
  310. * \param ctx cipher's context. Must have been initialised.
  311. *
  312. * \return operation (POLARSSL_ENCRYPT or POLARSSL_DECRYPT),
  313. * or POLARSSL_OPERATION_NONE if ctx has not been
  314. * initialised.
  315. */
  316. static inline operation_t cipher_get_operation( const cipher_context_t *ctx )
  317. {
  318. if( NULL == ctx || NULL == ctx->cipher_info )
  319. return POLARSSL_OPERATION_NONE;
  320. return ctx->operation;
  321. }
  322. /**
  323. * \brief Set the key to use with the given context.
  324. *
  325. * \param ctx generic cipher context. May not be NULL. Must have been
  326. * initialised using cipher_context_from_type or
  327. * cipher_context_from_string.
  328. * \param key The key to use.
  329. * \param key_length key length to use, in bits.
  330. * \param operation Operation that the key will be used for, either
  331. * POLARSSL_ENCRYPT or POLARSSL_DECRYPT.
  332. *
  333. * \returns 0 on success, POLARSSL_ERR_CIPHER_BAD_INPUT_DATA if
  334. * parameter verification fails or a cipher specific
  335. * error code.
  336. */
  337. int cipher_setkey( cipher_context_t *ctx, const unsigned char *key, int key_length,
  338. const operation_t operation );
  339. /**
  340. * \brief Reset the given context, setting the IV to iv
  341. *
  342. * \param ctx generic cipher context
  343. * \param iv IV to use or NONCE_COUNTER in the case of a CTR-mode cipher
  344. *
  345. * \returns 0 on success, POLARSSL_ERR_CIPHER_BAD_INPUT_DATA
  346. * if parameter verification fails.
  347. */
  348. int cipher_reset( cipher_context_t *ctx, const unsigned char *iv );
  349. /**
  350. * \brief Generic cipher update function. Encrypts/decrypts
  351. * using the given cipher context. Writes as many block
  352. * size'd blocks of data as possible to output. Any data
  353. * that cannot be written immediately will either be added
  354. * to the next block, or flushed when cipher_final is
  355. * called.
  356. *
  357. * \param ctx generic cipher context
  358. * \param input buffer holding the input data
  359. * \param ilen length of the input data
  360. * \param output buffer for the output data. Should be able to hold at
  361. * least ilen + block_size. Cannot be the same buffer as
  362. * input!
  363. * \param olen length of the output data, will be filled with the
  364. * actual number of bytes written.
  365. *
  366. * \returns 0 on success, POLARSSL_ERR_CIPHER_BAD_INPUT_DATA if
  367. * parameter verification fails,
  368. * POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE on an
  369. * unsupported mode for a cipher or a cipher specific
  370. * error code.
  371. */
  372. int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ilen,
  373. unsigned char *output, size_t *olen );
  374. /**
  375. * \brief Generic cipher finalisation function. If data still
  376. * needs to be flushed from an incomplete block, data
  377. * contained within it will be padded with the size of
  378. * the last block, and written to the output buffer.
  379. *
  380. * \param ctx Generic cipher context
  381. * \param output buffer to write data to. Needs block_size data available.
  382. * \param olen length of the data written to the output buffer.
  383. *
  384. * \returns 0 on success, POLARSSL_ERR_CIPHER_BAD_INPUT_DATA if
  385. * parameter verification fails,
  386. * POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED if decryption
  387. * expected a full block but was not provided one,
  388. * POLARSSL_ERR_CIPHER_INVALID_PADDING on invalid padding
  389. * while decrypting or a cipher specific error code.
  390. */
  391. int cipher_finish( cipher_context_t *ctx, unsigned char *output, size_t *olen);
  392. /**
  393. * \brief Checkup routine
  394. *
  395. * \return 0 if successful, or 1 if the test failed
  396. */
  397. int cipher_self_test( int verbose );
  398. #ifdef __cplusplus
  399. }
  400. #endif
  401. #endif /* POLARSSL_MD_H */