evp.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*!
  2. \ingroup openSSL
  3. \brief Getter functions for the respective WOLFSSL_EVP_CIPHER pointers.
  4. wolfSSL_EVP_init() must be called once in the program first to populate
  5. these cipher strings. WOLFSSL_DES_ECB macro must be defined for
  6. wolfSSL_EVP_des_ede3_ecb().
  7. \return pointer Returns a WOLFSSL_EVP_CIPHER pointer for DES EDE3 operations.
  8. \param none No parameters.
  9. _Example_
  10. \code
  11. printf("block size des ede3 cbc = %d\n",
  12. wolfSSL_EVP_CIPHER_block_size(wolfSSL_EVP_des_ede3_cbc()));
  13. printf("block size des ede3 ecb = %d\n",
  14. wolfSSL_EVP_CIPHER_block_size(wolfSSL_EVP_des_ede3_ecb()));
  15. \endcode
  16. \sa wolfSSL_EVP_CIPHER_CTX_init
  17. */
  18. WOLFSSL_API const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_des_ede3_ecb(void);
  19. /*!
  20. \ingroup openSSL
  21. \brief Getter functions for the respective WOLFSSL_EVP_CIPHER pointers.
  22. wolfSSL_EVP_init() must be called once in the program first to populate
  23. these cipher strings. WOLFSSL_DES_ECB macro must be defined for
  24. wolfSSL_EVP_des_ecb().
  25. \return pointer Returns a WOLFSSL_EVP_CIPHER pointer for DES operations.
  26. \param none No parameters.
  27. _Example_
  28. \code
  29. WOLFSSL_EVP_CIPHER* cipher;
  30. cipher = wolfSSL_EVP_des_cbc();
  31. \endcode
  32. \sa wolfSSL_EVP_CIPHER_CTX_init
  33. */
  34. WOLFSSL_API const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_des_cbc(void);
  35. /*!
  36. \ingroup openSSL
  37. \brief Function for initializing WOLFSSL_EVP_MD_CTX. This function is a
  38. wrapper for wolfSSL_EVP_DigestInit() because wolfSSL does not
  39. use WOLFSSL_ENGINE.
  40. \return SSL_SUCCESS If successfully set.
  41. \return SSL_FAILURE If not successful.
  42. \param ctx structure to initialize.
  43. \param type type of hash to do, for example SHA.
  44. \param impl engine to use. N/A for wolfSSL, can be NULL.
  45. _Example_
  46. \code
  47. WOLFSSL_EVP_MD_CTX* md = NULL;
  48. wolfCrypt_Init();
  49. md = wolfSSL_EVP_MD_CTX_new();
  50. if (md == NULL) {
  51. printf("error setting md\n");
  52. return -1;
  53. }
  54. printf("cipher md init ret = %d\n", wolfSSL_EVP_DigestInit_ex(md,
  55. wolfSSL_EVP_sha1(), e));
  56. //free resources
  57. \endcode
  58. \sa wolfSSL_EVP_MD_CTX_new
  59. \sa wolfCrypt_Init
  60. \sa wolfSSL_EVP_MD_CTX_free
  61. */
  62. WOLFSSL_API int wolfSSL_EVP_DigestInit_ex(WOLFSSL_EVP_MD_CTX* ctx,
  63. const WOLFSSL_EVP_MD* type,
  64. WOLFSSL_ENGINE *impl);
  65. /*!
  66. \ingroup openSSL
  67. \brief Function for initializing WOLFSSL_EVP_CIPHER_CTX. This function is a
  68. wrapper for wolfSSL_CipherInit() because wolfSSL does not
  69. use WOLFSSL_ENGINE.
  70. \return SSL_SUCCESS If successfully set.
  71. \return SSL_FAILURE If not successful.
  72. \param ctx structure to initialize.
  73. \param type type of encryption/decryption to do, for example AES.
  74. \param impl engine to use. N/A for wolfSSL, can be NULL.
  75. \param key key to set .
  76. \param iv iv if needed by algorithm.
  77. \param enc encryption (1) or decryption (0) flag.
  78. _Example_
  79. \code
  80. WOLFSSL_EVP_CIPHER_CTX* ctx = NULL;
  81. WOLFSSL_ENGINE* e = NULL;
  82. unsigned char key[16];
  83. unsigned char iv[12];
  84. wolfCrypt_Init();
  85. ctx = wolfSSL_EVP_CIPHER_CTX_new();
  86. if (ctx == NULL) {
  87. printf("issue creating ctx\n");
  88. return -1;
  89. }
  90. printf("cipher init ex error ret = %d\n", wolfSSL_EVP_CipherInit_ex(NULL,
  91. EVP_aes_128_ cbc(), e, key, iv, 1));
  92. printf("cipher init ex success ret = %d\n", wolfSSL_EVP_CipherInit_ex(ctx,
  93. EVP_aes_128_c bc(), e, key, iv, 1));
  94. // free resources
  95. \endcode
  96. \sa wolfSSL_EVP_CIPHER_CTX_new
  97. \sa wolfCrypt_Init
  98. \sa wolfSSL_EVP_CIPHER_CTX_free
  99. */
  100. WOLFSSL_API int wolfSSL_EVP_CipherInit_ex(WOLFSSL_EVP_CIPHER_CTX* ctx,
  101. const WOLFSSL_EVP_CIPHER* type,
  102. WOLFSSL_ENGINE *impl,
  103. const unsigned char* key,
  104. const unsigned char* iv,
  105. int enc);
  106. /*!
  107. \ingroup openSSL
  108. \brief Function for initializing WOLFSSL_EVP_CIPHER_CTX. This function is a
  109. wrapper for wolfSSL_EVP_CipherInit() because wolfSSL does not use
  110. WOLFSSL_ENGINE. Sets encrypt flag to be encrypt.
  111. \return SSL_SUCCESS If successfully set.
  112. \return SSL_FAILURE If not successful.
  113. \param ctx structure to initialize.
  114. \param type type of encryption to do, for example AES.
  115. \param impl engine to use. N/A for wolfSSL, can be NULL.
  116. \param key key to use.
  117. \param iv iv to use.
  118. _Example_
  119. \code
  120. WOLFSSL_EVP_CIPHER_CTX* ctx = NULL;
  121. wolfCrypt_Init();
  122. ctx = wolfSSL_EVP_CIPHER_CTX_new();
  123. if (ctx == NULL) {
  124. printf("error setting ctx\n");
  125. return -1;
  126. }
  127. printf("cipher ctx init ret = %d\n", wolfSSL_EVP_EncryptInit_ex(ctx,
  128. wolfSSL_EVP_aes_128_cbc(), e, key, iv));
  129. //free resources
  130. \endcode
  131. \sa wolfSSL_EVP_CIPHER_CTX_new
  132. \sa wolfCrypt_Init
  133. \sa wolfSSL_EVP_CIPHER_CTX_free
  134. */
  135. WOLFSSL_API int wolfSSL_EVP_EncryptInit_ex(WOLFSSL_EVP_CIPHER_CTX* ctx,
  136. const WOLFSSL_EVP_CIPHER* type,
  137. WOLFSSL_ENGINE *impl,
  138. const unsigned char* key,
  139. const unsigned char* iv);
  140. /*!
  141. \ingroup openSSL
  142. \brief Function for initializing WOLFSSL_EVP_CIPHER_CTX. This function is a
  143. wrapper for wolfSSL_EVP_CipherInit() because wolfSSL does not use
  144. WOLFSSL_ENGINE. Sets encrypt flag to be decrypt.
  145. \return SSL_SUCCESS If successfully set.
  146. \return SSL_FAILURE If not successful.
  147. \param ctx structure to initialize.
  148. \param type type of encryption/decryption to do, for example AES.
  149. \param impl engine to use. N/A for wolfSSL, can be NULL.
  150. \param key key to set .
  151. \param iv iv if needed by algorithm.
  152. \param enc encryption (1) or decryption (0) flag.
  153. _Example_
  154. \code
  155. WOLFSSL_EVP_CIPHER_CTX* ctx = NULL;
  156. WOLFSSL_ENGINE* e = NULL;
  157. unsigned char key[16];
  158. unsigned char iv[12];
  159. wolfCrypt_Init();
  160. ctx = wolfSSL_EVP_CIPHER_CTX_new();
  161. if (ctx == NULL) {
  162. printf("issue creating ctx\n");
  163. return -1;
  164. }
  165. printf("cipher init ex error ret = %d\n", wolfSSL_EVP_DecryptInit_ex(NULL,
  166. EVP_aes_128_ cbc(), e, key, iv, 1));
  167. printf("cipher init ex success ret = %d\n", wolfSSL_EVP_DecryptInit_ex(ctx,
  168. EVP_aes_128_c bc(), e, key, iv, 1));
  169. // free resources
  170. \endcode
  171. \sa wolfSSL_EVP_CIPHER_CTX_new
  172. \sa wolfCrypt_Init
  173. \sa wolfSSL_EVP_CIPHER_CTX_free
  174. */
  175. WOLFSSL_API int wolfSSL_EVP_DecryptInit_ex(WOLFSSL_EVP_CIPHER_CTX* ctx,
  176. const WOLFSSL_EVP_CIPHER* type,
  177. WOLFSSL_ENGINE *impl,
  178. const unsigned char* key,
  179. const unsigned char* iv);
  180. /*!
  181. \ingroup openSSL
  182. \brief Function for encrypting/decrypting data. In buffer is added to be
  183. encrypted or decrypted and out buffer holds the results. outl will be the
  184. length of encrypted/decrypted information.
  185. \return SSL_SUCCESS If successfull.
  186. \return SSL_FAILURE If not successful.
  187. \param ctx structure to get cipher type from.
  188. \param out buffer to hold output.
  189. \param outl adjusted to be size of output.
  190. \param in buffer to perform operation on.
  191. \param inl length of input buffer.
  192. _Example_
  193. \code
  194. WOLFSSL_EVP_CIPHER_CTX* ctx = NULL;
  195. unsigned char out[100];
  196. int outl;
  197. unsigned char in[100];
  198. int inl = 100;
  199. ctx = wolfSSL_EVP_CIPHER_CTX_new();
  200. // set up ctx
  201. ret = wolfSSL_EVP_CipherUpdate(ctx, out, outl, in, inl);
  202. // check ret value
  203. // buffer out holds outl bytes of data
  204. // free resources
  205. \endcode
  206. \sa wolfSSL_EVP_CIPHER_CTX_new
  207. \sa wolfCrypt_Init
  208. \sa wolfSSL_EVP_CIPHER_CTX_free
  209. */
  210. WOLFSSL_API int wolfSSL_EVP_CipherUpdate(WOLFSSL_EVP_CIPHER_CTX *ctx,
  211. unsigned char *out, int *outl,
  212. const unsigned char *in, int inl);
  213. /*!
  214. \ingroup openSSL
  215. \brief This function performs the final cipher operations adding in
  216. padding. If WOLFSSL_EVP_CIPH_NO_PADDING flag is set in
  217. WOLFSSL_EVP_CIPHER_CTX structure then 1 is returned and no
  218. encryption/decryption is done. If padding flag is seti padding is added and
  219. encrypted when ctx is set to encrypt, padding values are checked when set
  220. to decrypt.
  221. \return 1 Returned on success.
  222. \return 0 If encountering a failure.
  223. \param ctx structure to decrypt/encrypt with.
  224. \param out buffer for final decrypt/encrypt.
  225. \param out1 size of out buffer when data has been added by function.
  226. _Example_
  227. \code
  228. WOLFSSL_EVP_CIPHER_CTX* ctx;
  229. int out1;
  230. unsigned char out[64];
  231. // create ctx
  232. wolfSSL_EVP_CipherFinal(ctx, out, &out1);
  233. \endcode
  234. \sa wolfSSL_EVP_CIPHER_CTX_new
  235. */
  236. WOLFSSL_API int wolfSSL_EVP_CipherFinal(WOLFSSL_EVP_CIPHER_CTX *ctx,
  237. unsigned char *out, int *outl);
  238. /*!
  239. \ingroup openSSL
  240. \brief Setter function for WOLFSSL_EVP_CIPHER_CTX structure key length.
  241. \return SSL_SUCCESS If successfully set.
  242. \return SSL_FAILURE If failed to set key length.
  243. \param ctx structure to set key length.
  244. \param keylen key length.
  245. _Example_
  246. \code
  247. WOLFSSL_EVP_CIPHER_CTX* ctx;
  248. int keylen;
  249. // create ctx
  250. wolfSSL_EVP_CIPHER_CTX_set_key_length(ctx, keylen);
  251. \endcode
  252. \sa wolfSSL_EVP_CIPHER_flags
  253. */
  254. WOLFSSL_API int wolfSSL_EVP_CIPHER_CTX_set_key_length(WOLFSSL_EVP_CIPHER_CTX* ctx,
  255. int keylen);
  256. /*!
  257. \ingroup openSSL
  258. \brief This is a getter function for the ctx block size.
  259. \return size Returns ctx->block_size.
  260. \param ctx the cipher ctx to get block size of.
  261. _Example_
  262. \code
  263. const WOLFSSL_CVP_CIPHER_CTX* ctx;
  264. //set up ctx
  265. printf(“block size = %d\n”, wolfSSL_EVP_CIPHER_CTX_block_size(ctx));
  266. \endcode
  267. \sa wolfSSL_EVP_CIPHER_block_size
  268. */
  269. WOLFSSL_API int wolfSSL_EVP_CIPHER_CTX_block_size(const WOLFSSL_EVP_CIPHER_CTX *ctx);
  270. /*!
  271. \ingroup openSSL
  272. \brief This is a getter function for the block size of cipher.
  273. \return size returns the block size.
  274. \param cipher cipher to get block size of.
  275. _Example_
  276. \code
  277. printf(“block size = %d\n”,
  278. wolfSSL_EVP_CIPHER_block_size(wolfSSL_EVP_aes_256_ecb()));
  279. \endcode
  280. \sa wolfSSL_EVP_aes_256_ctr
  281. */
  282. WOLFSSL_API int wolfSSL_EVP_CIPHER_block_size(const WOLFSSL_EVP_CIPHER *cipher);
  283. /*!
  284. \ingroup openSSL
  285. \brief Setter function for WOLFSSL_EVP_CIPHER_CTX structure.
  286. \return none No returns.
  287. \param ctx structure to set flag.
  288. \param flag flag to set in structure.
  289. _Example_
  290. \code
  291. WOLFSSL_EVP_CIPHER_CTX* ctx;
  292. int flag;
  293. // create ctx
  294. wolfSSL_EVP_CIPHER_CTX_set_flags(ctx, flag);
  295. \endcode
  296. \sa wolfSSL_EVP_CIPHER_flags
  297. */
  298. WOLFSSL_API void wolfSSL_EVP_CIPHER_CTX_set_flags(WOLFSSL_EVP_CIPHER_CTX *ctx, int flags);
  299. /*!
  300. \ingroup openSSL
  301. \brief Setter function for WOLFSSL_EVP_CIPHER_CTX structure to use padding.
  302. \return SSL_SUCCESS If successfully set.
  303. \return BAD_FUNC_ARG If null argument passed in.
  304. \param ctx structure to set padding flag.
  305. \param padding 0 for not setting padding, 1 for setting padding.
  306. _Example_
  307. \code
  308. WOLFSSL_EVP_CIPHER_CTX* ctx;
  309. // create ctx
  310. wolfSSL_EVP_CIPHER_CTX_set_padding(ctx, 1);
  311. \endcode
  312. \sa wolfSSL_EVP_CIPHER_flags
  313. */
  314. WOLFSSL_API int wolfSSL_EVP_CIPHER_CTX_set_padding(WOLFSSL_EVP_CIPHER_CTX *c, int pad);