cipher.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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-2010, 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_AES,
  47. POLARSSL_CIPHER_ID_DES,
  48. POLARSSL_CIPHER_ID_3DES,
  49. POLARSSL_CIPHER_ID_CAMELLIA,
  50. } cipher_id_t;
  51. typedef enum {
  52. POLARSSL_CIPHER_NONE = 0,
  53. POLARSSL_CIPHER_AES_128_CBC,
  54. POLARSSL_CIPHER_AES_192_CBC,
  55. POLARSSL_CIPHER_AES_256_CBC,
  56. POLARSSL_CIPHER_AES_128_CFB128,
  57. POLARSSL_CIPHER_AES_192_CFB128,
  58. POLARSSL_CIPHER_AES_256_CFB128,
  59. POLARSSL_CIPHER_AES_128_CTR,
  60. POLARSSL_CIPHER_AES_192_CTR,
  61. POLARSSL_CIPHER_AES_256_CTR,
  62. POLARSSL_CIPHER_CAMELLIA_128_CBC,
  63. POLARSSL_CIPHER_CAMELLIA_192_CBC,
  64. POLARSSL_CIPHER_CAMELLIA_256_CBC,
  65. POLARSSL_CIPHER_CAMELLIA_128_CFB128,
  66. POLARSSL_CIPHER_CAMELLIA_192_CFB128,
  67. POLARSSL_CIPHER_CAMELLIA_256_CFB128,
  68. POLARSSL_CIPHER_CAMELLIA_128_CTR,
  69. POLARSSL_CIPHER_CAMELLIA_192_CTR,
  70. POLARSSL_CIPHER_CAMELLIA_256_CTR,
  71. POLARSSL_CIPHER_DES_CBC,
  72. POLARSSL_CIPHER_DES_EDE_CBC,
  73. POLARSSL_CIPHER_DES_EDE3_CBC
  74. } cipher_type_t;
  75. typedef enum {
  76. POLARSSL_MODE_NONE = 0,
  77. POLARSSL_MODE_CBC,
  78. POLARSSL_MODE_CFB128,
  79. POLARSSL_MODE_OFB,
  80. POLARSSL_MODE_CTR,
  81. } cipher_mode_t;
  82. typedef enum {
  83. POLARSSL_DECRYPT = 0,
  84. POLARSSL_ENCRYPT,
  85. } operation_t;
  86. enum {
  87. /** Undefined key length */
  88. POLARSSL_KEY_LENGTH_NONE = 0,
  89. /** Key length, in bits, for DES keys */
  90. POLARSSL_KEY_LENGTH_DES = 56,
  91. /** Key length, in bits, for DES in two key EDE */
  92. POLARSSL_KEY_LENGTH_DES_EDE = 112,
  93. /** Key length, in bits, for DES in three-key EDE */
  94. POLARSSL_KEY_LENGTH_DES_EDE3 = 168,
  95. /** Maximum length of any IV, in bytes */
  96. POLARSSL_MAX_IV_LENGTH = 16,
  97. };
  98. /**
  99. * Base cipher information. The non-mode specific functions and values.
  100. */
  101. typedef struct {
  102. /** Base Cipher type (e.g. POLARSSL_CIPHER_ID_AES) */
  103. cipher_id_t cipher;
  104. /** Encrypt using CBC */
  105. int (*cbc_func)( void *ctx, operation_t mode, size_t length, unsigned char *iv,
  106. const unsigned char *input, unsigned char *output );
  107. /** Encrypt using CFB128 */
  108. int (*cfb128_func)( void *ctx, operation_t mode, size_t length, size_t *iv_off,
  109. unsigned char *iv, const unsigned char *input, unsigned char *output );
  110. /** Encrypt using CTR */
  111. int (*ctr_func)( void *ctx, size_t length, size_t *nc_off, unsigned char *nonce_counter,
  112. unsigned char *stream_block, const unsigned char *input, unsigned char *output );
  113. /** Set key for encryption purposes */
  114. int (*setkey_enc_func)( void *ctx, const unsigned char *key, unsigned int key_length);
  115. /** Set key for decryption purposes */
  116. int (*setkey_dec_func)( void *ctx, const unsigned char *key, unsigned int key_length);
  117. /** Allocate a new context */
  118. void * (*ctx_alloc_func)( void );
  119. /** Free the given context */
  120. void (*ctx_free_func)( void *ctx );
  121. } cipher_base_t;
  122. /**
  123. * Cipher information. Allows cipher functions to be called in a generic way.
  124. */
  125. typedef struct {
  126. /** Full cipher identifier (e.g. POLARSSL_CIPHER_AES_256_CBC) */
  127. cipher_type_t type;
  128. /** Cipher mode (e.g. POLARSSL_CIPHER_MODE_CBC) */
  129. cipher_mode_t mode;
  130. /** Cipher key length, in bits (default length for variable sized ciphers) */
  131. unsigned int key_length;
  132. /** Name of the cipher */
  133. const char * name;
  134. /** IV size, in bytes */
  135. unsigned int iv_size;
  136. /** block size, in bytes */
  137. unsigned int block_size;
  138. /** Base cipher information and functions */
  139. const cipher_base_t *base;
  140. } cipher_info_t;
  141. /**
  142. * Generic message digest context.
  143. */
  144. typedef struct {
  145. /** Information about the associated cipher */
  146. const cipher_info_t *cipher_info;
  147. /** Key length to use */
  148. int key_length;
  149. /** Operation that the context's key has been initialised for */
  150. operation_t operation;
  151. /** Buffer for data that hasn't been encrypted yet */
  152. unsigned char unprocessed_data[POLARSSL_MAX_IV_LENGTH];
  153. /** Number of bytes that still need processing */
  154. size_t unprocessed_len;
  155. /** Current IV or NONCE_COUNTER for CTR-mode */
  156. unsigned char iv[POLARSSL_MAX_IV_LENGTH];
  157. /** Cipher-specific context */
  158. void *cipher_ctx;
  159. } cipher_context_t;
  160. #ifdef __cplusplus
  161. extern "C" {
  162. #endif
  163. /**
  164. * \brief Returns the list of ciphers supported by the generic cipher module.
  165. *
  166. * \return a statically allocated array of ciphers, the last entry
  167. * is 0.
  168. */
  169. const int *cipher_list( void );
  170. /**
  171. * \brief Returns the cipher information structure associated
  172. * with the given cipher name.
  173. *
  174. * \param cipher_name Name of the cipher to search for.
  175. *
  176. * \return the cipher information structure associated with the
  177. * given cipher_name, or NULL if not found.
  178. */
  179. const cipher_info_t *cipher_info_from_string( const char *cipher_name );
  180. /**
  181. * \brief Returns the cipher information structure associated
  182. * with the given cipher type.
  183. *
  184. * \param cipher_type Type of the cipher to search for.
  185. *
  186. * \return the cipher information structure associated with the
  187. * given cipher_type, or NULL if not found.
  188. */
  189. const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type );
  190. /**
  191. * \brief Initialises and fills the cipher context structure with
  192. * the appropriate values.
  193. *
  194. * \param ctx context to initialise. May not be NULL.
  195. * \param cipher_info cipher to use.
  196. *
  197. * \return \c 0 on success,
  198. * \c POLARSSL_ERR_CIPHER_BAD_INPUT_DATA on parameter failure,
  199. * \c POLARSSL_ERR_CIPHER_ALLOC_FAILED if allocation of the
  200. * cipher-specific context failed.
  201. */
  202. int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info );
  203. /**
  204. * \brief Free the cipher-specific context of ctx. Freeing ctx
  205. * itself remains the responsibility of the caller.
  206. *
  207. * \param ctx Free the cipher-specific context
  208. *
  209. * \returns 0 on success, POLARSSL_ERR_CIPHER_BAD_INPUT_DATA if
  210. * parameter verification fails.
  211. */
  212. int cipher_free_ctx( cipher_context_t *ctx );
  213. /**
  214. * \brief Returns the block size of the given cipher.
  215. *
  216. * \param ctx cipher's context. Must have been initialised.
  217. *
  218. * \return size of the cipher's blocks, or 0 if ctx has not been
  219. * initialised.
  220. */
  221. static inline unsigned int cipher_get_block_size( const cipher_context_t *ctx )
  222. {
  223. if( NULL == ctx || NULL == ctx->cipher_info )
  224. return 0;
  225. return ctx->cipher_info->block_size;
  226. }
  227. /**
  228. * \brief Returns the size of the cipher's IV.
  229. *
  230. * \param ctx cipher's context. Must have been initialised.
  231. *
  232. * \return size of the cipher's IV, or 0 if ctx has not been
  233. * initialised.
  234. */
  235. static inline int cipher_get_iv_size( const cipher_context_t *ctx )
  236. {
  237. if( NULL == ctx || NULL == ctx->cipher_info )
  238. return 0;
  239. return ctx->cipher_info->iv_size;
  240. }
  241. /**
  242. * \brief Returns the type of the given cipher.
  243. *
  244. * \param ctx cipher's context. Must have been initialised.
  245. *
  246. * \return type of the cipher, or POLARSSL_CIPHER_NONE if ctx has
  247. * not been initialised.
  248. */
  249. static inline cipher_type_t cipher_get_type( const cipher_context_t *ctx )
  250. {
  251. if( NULL == ctx || NULL == ctx->cipher_info )
  252. return (cipher_type_t)0;
  253. return ctx->cipher_info->type;
  254. }
  255. /**
  256. * \brief Returns the name of the given cipher, as a string.
  257. *
  258. * \param ctx cipher's context. Must have been initialised.
  259. *
  260. * \return name of the cipher, or NULL if ctx was not initialised.
  261. */
  262. static inline const char *cipher_get_name( const cipher_context_t *ctx )
  263. {
  264. if( NULL == ctx || NULL == ctx->cipher_info )
  265. return 0;
  266. return ctx->cipher_info->name;
  267. }
  268. /**
  269. * \brief Returns the key length of the cipher.
  270. *
  271. * \param ctx cipher's context. Must have been initialised.
  272. *
  273. * \return cipher's key length, in bits, or
  274. * POLARSSL_KEY_LENGTH_NONE if ctx has not been
  275. * initialised.
  276. */
  277. static inline int cipher_get_key_size ( const cipher_context_t *ctx )
  278. {
  279. if( NULL == ctx )
  280. return POLARSSL_KEY_LENGTH_NONE;
  281. return ctx->key_length;
  282. }
  283. /**
  284. * \brief Set the key to use with the given context.
  285. *
  286. * \param ctx generic cipher context. May not be NULL. Must have been
  287. * initialised using cipher_context_from_type or
  288. * cipher_context_from_string.
  289. * \param key The key to use.
  290. * \param key_length key length to use, in bits.
  291. * \param operation Operation that the key will be used for, either
  292. * POLARSSL_ENCRYPT or POLARSSL_DECRYPT.
  293. *
  294. * \returns 0 on success, POLARSSL_ERR_CIPHER_BAD_INPUT_DATA if
  295. * parameter verification fails or a cipher specific
  296. * error code.
  297. */
  298. int cipher_setkey( cipher_context_t *ctx, const unsigned char *key, int key_length,
  299. const operation_t operation );
  300. /**
  301. * \brief Reset the given context, setting the IV to iv
  302. *
  303. * \param ctx generic cipher context
  304. * \param iv IV to use or NONCE_COUNTER in the case of a CTR-mode cipher
  305. *
  306. * \returns 0 on success, POLARSSL_ERR_CIPHER_BAD_INPUT_DATA
  307. * if parameter verification fails.
  308. */
  309. int cipher_reset( cipher_context_t *ctx, const unsigned char *iv );
  310. /**
  311. * \brief Generic cipher update function. Encrypts/decrypts
  312. * using the given cipher context. Writes as many block
  313. * size'd blocks of data as possible to output. Any data
  314. * that cannot be written immediately will either be added
  315. * to the next block, or flushed when cipher_final is
  316. * called.
  317. *
  318. * \param ctx generic cipher context
  319. * \param input buffer holding the input data
  320. * \param ilen length of the input data
  321. * \param output buffer for the output data. Should be able to hold at
  322. * least ilen + block_size. Cannot be the same buffer as
  323. * input!
  324. * \param olen length of the output data, will be filled with the
  325. * actual number of bytes written.
  326. *
  327. * \returns 0 on success, POLARSSL_ERR_CIPHER_BAD_INPUT_DATA if
  328. * parameter verification fails,
  329. * POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE on an
  330. * unsupported mode for a cipher or a cipher specific
  331. * error code.
  332. */
  333. int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ilen,
  334. unsigned char *output, size_t *olen );
  335. /**
  336. * \brief Generic cipher finalisation function. If data still
  337. * needs to be flushed from an incomplete block, data
  338. * contained within it will be padded with the size of
  339. * the last block, and written to the output buffer.
  340. *
  341. * \param ctx Generic message digest context
  342. * \param output buffer to write data to. Needs block_size data available.
  343. * \param olen length of the data written to the output buffer.
  344. *
  345. * \returns 0 on success, POLARSSL_ERR_CIPHER_BAD_INPUT_DATA if
  346. * parameter verification fails,
  347. * POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED if decryption
  348. * expected a full block but was not provided one,
  349. * POLARSSL_ERR_CIPHER_INVALID_PADDING on invalid padding
  350. * while decrypting or a cipher specific error code.
  351. */
  352. int cipher_finish( cipher_context_t *ctx, unsigned char *output, size_t *olen);
  353. /**
  354. * \brief Checkup routine
  355. *
  356. * \return 0 if successful, or 1 if the test failed
  357. */
  358. int cipher_self_test( int verbose );
  359. #ifdef __cplusplus
  360. }
  361. #endif
  362. #endif /* POLARSSL_MD_H */