aes.h 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  1. /*!
  2. \ingroup AES
  3. \brief This function initializes an AES structure by setting the key and
  4. then setting the initialization vector.
  5. \return 0 On successfully setting key and initialization vector.
  6. \return BAD_FUNC_ARG Returned if key length is invalid.
  7. \param aes pointer to the AES structure to modify
  8. \param key 16, 24, or 32 byte secret key for encryption and decryption
  9. \param len length of the key passed in
  10. \param iv pointer to the initialization vector used to initialize the key
  11. \param dir Cipher direction. Set AES_ENCRYPTION to encrypt, or
  12. AES_DECRYPTION to decrypt. Direction for some modes (CFB and CTR) is
  13. always AES_ENCRYPTION.
  14. _Example_
  15. \code
  16. Aes enc;
  17. int ret = 0;
  18. byte key[] = { some 16, 24 or 32 byte key };
  19. byte iv[] = { some 16 byte iv };
  20. if (ret = wc_AesSetKey(&enc, key, AES_BLOCK_SIZE, iv,
  21. AES_ENCRYPTION) != 0) {
  22. // failed to set aes key
  23. }
  24. \endcode
  25. \sa wc_AesSetKeyDirect
  26. \sa wc_AesSetIV
  27. */
  28. int wc_AesSetKey(Aes* aes, const byte* key, word32 len,
  29. const byte* iv, int dir);
  30. /*!
  31. \ingroup AES
  32. \brief This function sets the initialization vector for a
  33. particular AES object. The AES object should be initialized before
  34. calling this function.
  35. \return 0 On successfully setting initialization vector.
  36. \return BAD_FUNC_ARG Returned if AES pointer is NULL.
  37. \param aes pointer to the AES structure on which to set the
  38. initialization vector
  39. \param iv initialization vector used to initialize the AES structure.
  40. If the value is NULL, the default action initializes the iv to 0.
  41. _Example_
  42. \code
  43. Aes enc;
  44. // set enc key
  45. byte iv[] = { some 16 byte iv };
  46. if (ret = wc_AesSetIV(&enc, iv) != 0) {
  47. // failed to set aes iv
  48. }
  49. \endcode
  50. \sa wc_AesSetKeyDirect
  51. \sa wc_AesSetKey
  52. */
  53. int wc_AesSetIV(Aes* aes, const byte* iv);
  54. /*!
  55. \ingroup AES
  56. \brief Encrypts a plaintext message from the input buffer in, and places
  57. the resulting cipher text in the output buffer out using cipher block
  58. chaining with AES. This function requires that the AES object has been
  59. initialized by calling AesSetKey before a message is able to be encrypted.
  60. This function assumes that the input message is AES block length aligned,
  61. and expects the input length to be a multiple of the block length, which
  62. will optionally be checked and enforced if WOLFSSL_AES_CBC_LENGTH_CHECKS
  63. is defined in the build configuration. In order to assure block-multiple
  64. input, PKCS#7 style padding should be added beforehand. This differs from
  65. the OpenSSL AES-CBC methods which add the padding for you. To make the
  66. wolfSSL and corresponding OpenSSL functions interoperate, one should specify
  67. the -nopad option in the OpenSSL command line function so that it behaves
  68. like the wolfSSL AesCbcEncrypt method and does not add extra padding
  69. during encryption.
  70. \return 0 On successfully encrypting message.
  71. \return BAD_ALIGN_E: may be returned on block align error
  72. \return BAD_LENGTH_E will be returned if the input length isn't a
  73. multiple of the AES block length, when the library is built with
  74. WOLFSSL_AES_CBC_LENGTH_CHECKS.
  75. \param aes pointer to the AES object used to encrypt data
  76. \param out pointer to the output buffer in which to store the ciphertext
  77. of the encrypted message
  78. \param in pointer to the input buffer containing message to be encrypted
  79. \param sz size of input message
  80. _Example_
  81. \code
  82. Aes enc;
  83. int ret = 0;
  84. // initialize enc with AesSetKey, using direction AES_ENCRYPTION
  85. byte msg[AES_BLOCK_SIZE * n]; // multiple of 16 bytes
  86. // fill msg with data
  87. byte cipher[AES_BLOCK_SIZE * n]; // Some multiple of 16 bytes
  88. if ((ret = wc_AesCbcEncrypt(&enc, cipher, message, sizeof(msg))) != 0 ) {
  89. // block align error
  90. }
  91. \endcode
  92. \sa wc_AesSetKey
  93. \sa wc_AesSetIV
  94. \sa wc_AesCbcDecrypt
  95. */
  96. int wc_AesCbcEncrypt(Aes* aes, byte* out,
  97. const byte* in, word32 sz);
  98. /*!
  99. \ingroup AES
  100. \brief Decrypts a cipher from the input buffer in, and places the
  101. resulting plain text in the output buffer out using cipher block chaining
  102. with AES. This function requires that the AES structure has been
  103. initialized by calling AesSetKey before a message is able to be decrypted.
  104. This function assumes that the original message was AES block length
  105. aligned, and expects the input length to be a multiple of the block length,
  106. which will optionally be checked and enforced if
  107. WOLFSSL_AES_CBC_LENGTH_CHECKS is defined in the build configuration.
  108. This differs from the OpenSSL AES-CBC methods, which add PKCS#7 padding
  109. automatically, and so do not require block-multiple input. To make the
  110. wolfSSL function and equivalent OpenSSL functions interoperate, one
  111. should specify the -nopad option in the OpenSSL command line function
  112. so that it behaves like the wolfSSL AesCbcEncrypt method and does not
  113. create errors during decryption.
  114. \return 0 On successfully decrypting message.
  115. \return BAD_ALIGN_E may be returned on block align error.
  116. \return BAD_LENGTH_E will be returned if the input length isn't a
  117. multiple of the AES block length, when the library is built with
  118. WOLFSSL_AES_CBC_LENGTH_CHECKS.
  119. \param aes pointer to the AES object used to decrypt data.
  120. \param out pointer to the output buffer in which to store the plain text
  121. of the decrypted message.
  122. \param in pointer to the input buffer containing cipher text to be
  123. decrypted.
  124. \param sz size of input message.
  125. _Example_
  126. \code
  127. Aes dec;
  128. int ret = 0;
  129. // initialize dec with AesSetKey, using direction AES_DECRYPTION
  130. byte cipher[AES_BLOCK_SIZE * n]; // some multiple of 16 bytes
  131. // fill cipher with cipher text
  132. byte plain [AES_BLOCK_SIZE * n];
  133. if ((ret = wc_AesCbcDecrypt(&dec, plain, cipher, sizeof(cipher))) != 0 ) {
  134. // block align error
  135. }
  136. \endcode
  137. \sa wc_AesSetKey
  138. \sa wc_AesCbcEncrypt
  139. */
  140. int wc_AesCbcDecrypt(Aes* aes, byte* out,
  141. const byte* in, word32 sz);
  142. /*!
  143. \ingroup AES
  144. \brief Encrypts/Decrypts a message from the input buffer in, and places
  145. the resulting cipher text in the output buffer out using CTR mode with
  146. AES. This function is only enabled if WOLFSSL_AES_COUNTER is enabled at
  147. compile time. The AES structure should be initialized through AesSetKey
  148. before calling this function. Note that this function is used for both
  149. decryption and encryption. _NOTE:_ Regarding using same API for encryption
  150. and decryption. User should differentiate between Aes structures
  151. for encrypt/decrypt.
  152. \return int integer values corresponding to wolfSSL error or success
  153. status
  154. \param aes pointer to the AES object used to decrypt data
  155. \param out pointer to the output buffer in which to store the cipher
  156. text of the encrypted message
  157. \param in pointer to the input buffer containing plain text to be encrypted
  158. \param sz size of the input plain text
  159. _Example_
  160. \code
  161. Aes enc;
  162. Aes dec;
  163. // initialize enc and dec with AesSetKeyDirect, using direction
  164. AES_ENCRYPTION
  165. // since the underlying API only calls Encrypt and by default calling
  166. encrypt on
  167. // a cipher results in a decryption of the cipher
  168. byte msg[AES_BLOCK_SIZE * n]; //n being a positive integer making msg
  169. some multiple of 16 bytes
  170. // fill plain with message text
  171. byte cipher[AES_BLOCK_SIZE * n];
  172. byte decrypted[AES_BLOCK_SIZE * n];
  173. wc_AesCtrEncrypt(&enc, cipher, msg, sizeof(msg)); // encrypt plain
  174. wc_AesCtrEncrypt(&dec, decrypted, cipher, sizeof(cipher));
  175. // decrypt cipher text
  176. \endcode
  177. \sa wc_AesSetKey
  178. */
  179. int wc_AesCtrEncrypt(Aes* aes, byte* out,
  180. const byte* in, word32 sz);
  181. /*!
  182. \ingroup AES
  183. \brief This function is a one-block encrypt of the input block, in, into
  184. the output block, out. It uses the key of the provided AES structure, which
  185. should be initialized with wc_AesSetKey before calling this function.
  186. wc_AesSetKey should have been called with the iv set to NULL. This is only
  187. enabled if the configure option WOLFSSL_AES_DIRECT is enabled. __Warning:__
  188. In nearly all use cases ECB mode is considered to be less secure. Please
  189. avoid using ECB API’s directly whenever possible.
  190. \return int integer values corresponding to wolfSSL error or success
  191. status
  192. \param aes pointer to the AES object used to encrypt data
  193. \param out pointer to the output buffer in which to store the cipher
  194. text of the encrypted message
  195. \param in pointer to the input buffer containing plain text to be encrypted
  196. _Example_
  197. \code
  198. Aes enc;
  199. // initialize enc with AesSetKey, using direction AES_ENCRYPTION
  200. byte msg [AES_BLOCK_SIZE]; // 16 bytes
  201. // initialize msg with plain text to encrypt
  202. byte cipher[AES_BLOCK_SIZE];
  203. wc_AesEncryptDirect(&enc, cipher, msg);
  204. \endcode
  205. \sa wc_AesDecryptDirect
  206. \sa wc_AesSetKeyDirect
  207. */
  208. int wc_AesEncryptDirect(Aes* aes, byte* out, const byte* in);
  209. /*!
  210. \ingroup AES
  211. \brief This function is a one-block decrypt of the input block, in, into
  212. the output block, out. It uses the key of the provided AES structure, which
  213. should be initialized with wc_AesSetKey before calling this function.
  214. wc_AesSetKey should have been called with the iv set to NULL. This is only
  215. enabled if the configure option WOLFSSL_AES_DIRECT is enabled. __Warning:__
  216. In nearly all use cases ECB mode is considered to be less secure. Please
  217. avoid using ECB API’s directly whenever possible.
  218. \return int integer values corresponding to wolfSSL error or success
  219. status
  220. \param aes pointer to the AES object used to encrypt data
  221. \param out pointer to the output buffer in which to store the plain
  222. text of the decrypted cipher text
  223. \param in pointer to the input buffer containing cipher text to be
  224. decrypted
  225. _Example_
  226. \code
  227. Aes dec;
  228. // initialize enc with AesSetKey, using direction AES_DECRYPTION
  229. byte cipher [AES_BLOCK_SIZE]; // 16 bytes
  230. // initialize cipher with cipher text to decrypt
  231. byte msg[AES_BLOCK_SIZE];
  232. wc_AesDecryptDirect(&dec, msg, cipher);
  233. \endcode
  234. \sa wc_AesEncryptDirect
  235. \sa wc_AesSetKeyDirect
  236. */
  237. int wc_AesDecryptDirect(Aes* aes, byte* out, const byte* in);
  238. /*!
  239. \ingroup AES
  240. \brief This function is used to set the AES keys for CTR mode with AES.
  241. It initializes an AES object with the given key, iv
  242. (initialization vector), and encryption dir (direction). It is only
  243. enabled if the configure option WOLFSSL_AES_DIRECT is enabled.
  244. Currently wc_AesSetKeyDirect uses wc_AesSetKey internally. __Warning:__ In
  245. nearly all use cases ECB mode is considered to be less secure. Please avoid
  246. using ECB API’s directly whenever possible
  247. \return 0 On successfully setting the key.
  248. \return BAD_FUNC_ARG Returned if the given key is an invalid length.
  249. \param aes pointer to the AES object used to encrypt data
  250. \param key 16, 24, or 32 byte secret key for encryption and decryption
  251. \param len length of the key passed in
  252. \param iv initialization vector used to initialize the key
  253. \param dir Cipher direction. Set AES_ENCRYPTION to encrypt, or
  254. AES_DECRYPTION to decrypt. (See enum in wolfssl/wolfcrypt/aes.h)
  255. (NOTE: If using wc_AesSetKeyDirect with Aes Counter mode (Stream cipher)
  256. only use AES_ENCRYPTION for both encrypting and decrypting)
  257. _Example_
  258. \code
  259. Aes enc;
  260. int ret = 0;
  261. byte key[] = { some 16, 24, or 32 byte key };
  262. byte iv[] = { some 16 byte iv };
  263. if (ret = wc_AesSetKeyDirect(&enc, key, sizeof(key), iv,
  264. AES_ENCRYPTION) != 0) {
  265. // failed to set aes key
  266. }
  267. \endcode
  268. \sa wc_AesEncryptDirect
  269. \sa wc_AesDecryptDirect
  270. \sa wc_AesSetKey
  271. */
  272. int wc_AesSetKeyDirect(Aes* aes, const byte* key, word32 len,
  273. const byte* iv, int dir);
  274. /*!
  275. \ingroup AES
  276. \brief This function is used to set the key for AES GCM
  277. (Galois/Counter Mode). It initializes an AES object with the
  278. given key. It is only enabled if the configure option
  279. HAVE_AESGCM is enabled at compile time.
  280. \return 0 On successfully setting the key.
  281. \return BAD_FUNC_ARG Returned if the given key is an invalid length.
  282. \param aes pointer to the AES object used to encrypt data
  283. \param key 16, 24, or 32 byte secret key for encryption and decryption
  284. \param len length of the key passed in
  285. _Example_
  286. \code
  287. Aes enc;
  288. int ret = 0;
  289. byte key[] = { some 16, 24,32 byte key };
  290. if (ret = wc_AesGcmSetKey(&enc, key, sizeof(key)) != 0) {
  291. // failed to set aes key
  292. }
  293. \endcode
  294. \sa wc_AesGcmEncrypt
  295. \sa wc_AesGcmDecrypt
  296. */
  297. int wc_AesGcmSetKey(Aes* aes, const byte* key, word32 len);
  298. /*!
  299. \ingroup AES
  300. \brief This function encrypts the input message, held in the buffer in,
  301. and stores the resulting cipher text in the output buffer out. It
  302. requires a new iv (initialization vector) for each call to encrypt.
  303. It also encodes the input authentication vector, authIn, into the
  304. authentication tag, authTag.
  305. \return 0 On successfully encrypting the input message
  306. \param aes - pointer to the AES object used to encrypt data
  307. \param out pointer to the output buffer in which to store the cipher text
  308. \param in pointer to the input buffer holding the message to encrypt
  309. \param sz length of the input message to encrypt
  310. \param iv pointer to the buffer containing the initialization vector
  311. \param ivSz length of the initialization vector
  312. \param authTag pointer to the buffer in which to store the
  313. authentication tag
  314. \param authTagSz length of the desired authentication tag
  315. \param authIn pointer to the buffer containing the input
  316. authentication vector
  317. \param authInSz length of the input authentication vector
  318. _Example_
  319. \code
  320. Aes enc;
  321. // initialize aes structure by calling wc_AesGcmSetKey
  322. byte plain[AES_BLOCK_LENGTH * n]; //n being a positive integer
  323. making plain some multiple of 16 bytes
  324. // initialize plain with msg to encrypt
  325. byte cipher[sizeof(plain)];
  326. byte iv[] = // some 16 byte iv
  327. byte authTag[AUTH_TAG_LENGTH];
  328. byte authIn[] = // Authentication Vector
  329. wc_AesGcmEncrypt(&enc, cipher, plain, sizeof(cipher), iv, sizeof(iv),
  330. authTag, sizeof(authTag), authIn, sizeof(authIn));
  331. \endcode
  332. \sa wc_AesGcmSetKey
  333. \sa wc_AesGcmDecrypt
  334. */
  335. int wc_AesGcmEncrypt(Aes* aes, byte* out,
  336. const byte* in, word32 sz,
  337. const byte* iv, word32 ivSz,
  338. byte* authTag, word32 authTagSz,
  339. const byte* authIn, word32 authInSz);
  340. /*!
  341. \ingroup AES
  342. \brief This function decrypts the input cipher text, held in the buffer
  343. in, and stores the resulting message text in the output buffer out.
  344. It also checks the input authentication vector, authIn, against the
  345. supplied authentication tag, authTag.
  346. \return 0 On successfully decrypting the input message
  347. \return AES_GCM_AUTH_E If the authentication tag does not match the
  348. supplied authentication code vector, authTag.
  349. \param aes pointer to the AES object used to encrypt data
  350. \param out pointer to the output buffer in which to store the message text
  351. \param in pointer to the input buffer holding the cipher text to decrypt
  352. \param sz length of the cipher text to decrypt
  353. \param iv pointer to the buffer containing the initialization vector
  354. \param ivSz length of the initialization vector
  355. \param authTag pointer to the buffer containing the authentication tag
  356. \param authTagSz length of the desired authentication tag
  357. \param authIn pointer to the buffer containing the input
  358. authentication vector
  359. \param authInSz length of the input authentication vector
  360. _Example_
  361. \code
  362. Aes enc; //can use the same struct as was passed to wc_AesGcmEncrypt
  363. // initialize aes structure by calling wc_AesGcmSetKey if not already done
  364. byte cipher[AES_BLOCK_LENGTH * n]; //n being a positive integer
  365. making cipher some multiple of 16 bytes
  366. // initialize cipher with cipher text to decrypt
  367. byte output[sizeof(cipher)];
  368. byte iv[] = // some 16 byte iv
  369. byte authTag[AUTH_TAG_LENGTH];
  370. byte authIn[] = // Authentication Vector
  371. wc_AesGcmDecrypt(&enc, output, cipher, sizeof(cipher), iv, sizeof(iv),
  372. authTag, sizeof(authTag), authIn, sizeof(authIn));
  373. \endcode
  374. \sa wc_AesGcmSetKey
  375. \sa wc_AesGcmEncrypt
  376. */
  377. int wc_AesGcmDecrypt(Aes* aes, byte* out,
  378. const byte* in, word32 sz,
  379. const byte* iv, word32 ivSz,
  380. const byte* authTag, word32 authTagSz,
  381. const byte* authIn, word32 authInSz);
  382. /*!
  383. \ingroup AES
  384. \brief This function initializes and sets the key for a GMAC object
  385. to be used for Galois Message Authentication.
  386. \return 0 On successfully setting the key
  387. \return BAD_FUNC_ARG Returned if key length is invalid.
  388. \param gmac pointer to the gmac object used for authentication
  389. \param key 16, 24, or 32 byte secret key for authentication
  390. \param len length of the key
  391. _Example_
  392. \code
  393. Gmac gmac;
  394. key[] = { some 16, 24, or 32 byte length key };
  395. wc_AesInit(gmac.aes, HEAP_HINT, INVALID_DEVID); // Make sure devId updated
  396. wc_GmacSetKey(&gmac, key, sizeof(key));
  397. \endcode
  398. \sa wc_GmacUpdate
  399. \sa wc_AesInit
  400. */
  401. int wc_GmacSetKey(Gmac* gmac, const byte* key, word32 len);
  402. /*!
  403. \ingroup AES
  404. \brief This function generates the Gmac hash of the authIn input and
  405. stores the result in the authTag buffer. After running wc_GmacUpdate,
  406. one should compare the generated authTag to a known authentication tag
  407. to verify the authenticity of a message.
  408. \return 0 On successfully computing the Gmac hash.
  409. \param gmac pointer to the gmac object used for authentication
  410. \param iv initialization vector used for the hash
  411. \param ivSz size of the initialization vector used
  412. \param authIn pointer to the buffer containing the authentication
  413. vector to verify
  414. \param authInSz size of the authentication vector
  415. \param authTag pointer to the output buffer in which to store the Gmac hash
  416. \param authTagSz the size of the output buffer used to store the Gmac hash
  417. _Example_
  418. \code
  419. Gmac gmac;
  420. key[] = { some 16, 24, or 32 byte length key };
  421. iv[] = { some 16 byte length iv };
  422. wc_AesInit(gmac.aes, HEAP_HINT, INVALID_DEVID); // Make sure devId updated
  423. wc_GmacSetKey(&gmac, key, sizeof(key));
  424. authIn[] = { some 16 byte authentication input };
  425. tag[AES_BLOCK_SIZE]; // will store authentication code
  426. wc_GmacUpdate(&gmac, iv, sizeof(iv), authIn, sizeof(authIn), tag,
  427. sizeof(tag));
  428. \endcode
  429. \sa wc_GmacSetKey
  430. \sa wc_AesInit
  431. */
  432. int wc_GmacUpdate(Gmac* gmac, const byte* iv, word32 ivSz,
  433. const byte* authIn, word32 authInSz,
  434. byte* authTag, word32 authTagSz);
  435. /*!
  436. \ingroup AES
  437. \brief This function sets the key for an AES object using CCM
  438. (Counter with CBC-MAC). It takes a pointer to an AES structure and
  439. initializes it with supplied key.
  440. \return none
  441. \param aes aes structure in which to store the supplied key
  442. \param key 16, 24, or 32 byte secret key for encryption and decryption
  443. \param keySz size of the supplied key
  444. _Example_
  445. \code
  446. Aes enc;
  447. key[] = { some 16, 24, or 32 byte length key };
  448. wc_AesCcmSetKey(&aes, key, sizeof(key));
  449. \endcode
  450. \sa wc_AesCcmEncrypt
  451. \sa wc_AesCcmDecrypt
  452. */
  453. int wc_AesCcmSetKey(Aes* aes, const byte* key, word32 keySz);
  454. /*!
  455. \ingroup AES
  456. \brief This function encrypts the input message, in, into the output
  457. buffer, out, using CCM (Counter with CBC-MAC). It subsequently
  458. calculates and stores the authorization tag, authTag, from the
  459. authIn input.
  460. \return none
  461. \param aes pointer to the AES object used to encrypt data
  462. \param out pointer to the output buffer in which to store the cipher text
  463. \param in pointer to the input buffer holding the message to encrypt
  464. \param sz length of the input message to encrypt
  465. \param nonce pointer to the buffer containing the nonce
  466. (number only used once)
  467. \param nonceSz length of the nonce
  468. \param authTag pointer to the buffer in which to store the
  469. authentication tag
  470. \param authTagSz length of the desired authentication tag
  471. \param authIn pointer to the buffer containing the input
  472. authentication vector
  473. \param authInSz length of the input authentication vector
  474. _Example_
  475. \code
  476. Aes enc;
  477. // initialize enc with wc_AesCcmSetKey
  478. nonce[] = { initialize nonce };
  479. plain[] = { some plain text message };
  480. cipher[sizeof(plain)];
  481. authIn[] = { some 16 byte authentication input };
  482. tag[AES_BLOCK_SIZE]; // will store authentication code
  483. wc_AesCcmEncrypt(&enc, cipher, plain, sizeof(plain), nonce, sizeof(nonce),
  484. tag, sizeof(tag), authIn, sizeof(authIn));
  485. \endcode
  486. \sa wc_AesCcmSetKey
  487. \sa wc_AesCcmDecrypt
  488. */
  489. int wc_AesCcmEncrypt(Aes* aes, byte* out,
  490. const byte* in, word32 inSz,
  491. const byte* nonce, word32 nonceSz,
  492. byte* authTag, word32 authTagSz,
  493. const byte* authIn, word32 authInSz);
  494. /*!
  495. \ingroup AES
  496. \brief This function decrypts the input cipher text, in, into
  497. the output buffer, out, using CCM (Counter with CBC-MAC). It
  498. subsequently calculates the authorization tag, authTag, from the
  499. authIn input. If the authorization tag is invalid, it sets the
  500. output buffer to zero and returns the error: AES_CCM_AUTH_E.
  501. \return 0 On successfully decrypting the input message
  502. \return AES_CCM_AUTH_E If the authentication tag does not match the
  503. supplied authentication code vector, authTag.
  504. \param aes pointer to the AES object used to encrypt data
  505. \param out pointer to the output buffer in which to store the cipher text
  506. \param in pointer to the input buffer holding the message to encrypt
  507. \param sz length of the input cipher text to decrypt
  508. \param nonce pointer to the buffer containing the nonce
  509. (number only used once)
  510. \param nonceSz length of the nonce
  511. \param authTag pointer to the buffer in which to store the
  512. authentication tag
  513. \param authTagSz length of the desired authentication tag
  514. \param authIn pointer to the buffer containing the input
  515. authentication vector
  516. \param authInSz length of the input authentication vector
  517. _Example_
  518. \code
  519. Aes dec;
  520. // initialize dec with wc_AesCcmSetKey
  521. nonce[] = { initialize nonce };
  522. cipher[] = { encrypted message };
  523. plain[sizeof(cipher)];
  524. authIn[] = { some 16 byte authentication input };
  525. tag[AES_BLOCK_SIZE] = { authentication tag received for verification };
  526. int return = wc_AesCcmDecrypt(&dec, plain, cipher, sizeof(cipher),
  527. nonce, sizeof(nonce),tag, sizeof(tag), authIn, sizeof(authIn));
  528. if(return != 0) {
  529. // decrypt error, invalid authentication code
  530. }
  531. \endcode
  532. \sa wc_AesCcmSetKey
  533. \sa wc_AesCcmEncrypt
  534. */
  535. int wc_AesCcmDecrypt(Aes* aes, byte* out,
  536. const byte* in, word32 inSz,
  537. const byte* nonce, word32 nonceSz,
  538. const byte* authTag, word32 authTagSz,
  539. const byte* authIn, word32 authInSz);
  540. /*!
  541. \ingroup AES
  542. \brief This is to help with setting keys to correct encrypt or
  543. decrypt type. It is up to user to call wc_AesXtsFree on aes key when done.
  544. \return 0 Success
  545. \param aes AES keys for encrypt/decrypt process
  546. \param key buffer holding aes key | tweak key
  547. \param len length of key buffer in bytes. Should be twice that of
  548. key size.
  549. i.e. 32 for a 16 byte key.
  550. \param dir direction, either AES_ENCRYPTION or AES_DECRYPTION
  551. \param heap heap hint to use for memory. Can be NULL
  552. \param devId id to use with async crypto. Can be 0
  553. _Example_
  554. \code
  555. XtsAes aes;
  556. if(wc_AesXtsSetKey(&aes, key, sizeof(key), AES_ENCRYPTION, NULL, 0) != 0)
  557. {
  558. // Handle error
  559. }
  560. wc_AesXtsFree(&aes);
  561. \endcode
  562. \sa wc_AesXtsEncrypt
  563. \sa wc_AesXtsDecrypt
  564. \sa wc_AesXtsFree
  565. */
  566. int wc_AesXtsSetKey(XtsAes* aes, const byte* key,
  567. word32 len, int dir, void* heap, int devId);
  568. /*!
  569. \ingroup AES
  570. \brief Same process as wc_AesXtsEncrypt but uses a word64 type as the tweak
  571. value instead of a byte array. This just converts the word64 to a
  572. byte array and calls wc_AesXtsEncrypt.
  573. \return 0 Success
  574. \param aes AES keys to use for block encrypt/decrypt
  575. \param out output buffer to hold cipher text
  576. \param in input plain text buffer to encrypt
  577. \param sz size of both out and in buffers
  578. \param sector value to use for tweak
  579. _Example_
  580. \code
  581. XtsAes aes;
  582. unsigned char plain[SIZE];
  583. unsigned char cipher[SIZE];
  584. word64 s = VALUE;
  585. //set up keys with AES_ENCRYPTION as dir
  586. if(wc_AesXtsEncryptSector(&aes, cipher, plain, SIZE, s) != 0)
  587. {
  588. // Handle error
  589. }
  590. wc_AesXtsFree(&aes);
  591. \endcode
  592. \sa wc_AesXtsEncrypt
  593. \sa wc_AesXtsDecrypt
  594. \sa wc_AesXtsSetKey
  595. \sa wc_AesXtsFree
  596. */
  597. int wc_AesXtsEncryptSector(XtsAes* aes, byte* out,
  598. const byte* in, word32 sz, word64 sector);
  599. /*!
  600. \ingroup AES
  601. \brief Same process as wc_AesXtsDecrypt but uses a word64 type as the tweak
  602. value instead of a byte array. This just converts the word64 to a
  603. byte array.
  604. \return 0 Success
  605. \param aes AES keys to use for block encrypt/decrypt
  606. \param out output buffer to hold plain text
  607. \param in input cipher text buffer to decrypt
  608. \param sz size of both out and in buffers
  609. \param sector value to use for tweak
  610. _Example_
  611. \code
  612. XtsAes aes;
  613. unsigned char plain[SIZE];
  614. unsigned char cipher[SIZE];
  615. word64 s = VALUE;
  616. //set up aes key with AES_DECRYPTION as dir and tweak with AES_ENCRYPTION
  617. if(wc_AesXtsDecryptSector(&aes, plain, cipher, SIZE, s) != 0)
  618. {
  619. // Handle error
  620. }
  621. wc_AesXtsFree(&aes);
  622. \endcode
  623. \sa wc_AesXtsEncrypt
  624. \sa wc_AesXtsDecrypt
  625. \sa wc_AesXtsSetKey
  626. \sa wc_AesXtsFree
  627. */
  628. int wc_AesXtsDecryptSector(XtsAes* aes, byte* out,
  629. const byte* in, word32 sz, word64 sector);
  630. /*!
  631. \ingroup AES
  632. \brief AES with XTS mode. (XTS) XEX encryption with Tweak and cipher text
  633. Stealing.
  634. \return 0 Success
  635. \param aes AES keys to use for block encrypt/decrypt
  636. \param out output buffer to hold cipher text
  637. \param in input plain text buffer to encrypt
  638. \param sz size of both out and in buffers
  639. \param i value to use for tweak
  640. \param iSz size of i buffer, should always be AES_BLOCK_SIZE but having
  641. this input adds a sanity check on how the user calls the
  642. function.
  643. _Example_
  644. \code
  645. XtsAes aes;
  646. unsigned char plain[SIZE];
  647. unsigned char cipher[SIZE];
  648. unsigned char i[AES_BLOCK_SIZE];
  649. //set up key with AES_ENCRYPTION as dir
  650. if(wc_AesXtsEncrypt(&aes, cipher, plain, SIZE, i, sizeof(i)) != 0)
  651. {
  652. // Handle error
  653. }
  654. wc_AesXtsFree(&aes);
  655. \endcode
  656. \sa wc_AesXtsDecrypt
  657. \sa wc_AesXtsSetKey
  658. \sa wc_AesXtsFree
  659. */
  660. int wc_AesXtsEncrypt(XtsAes* aes, byte* out,
  661. const byte* in, word32 sz, const byte* i, word32 iSz);
  662. /*!
  663. \ingroup AES
  664. \brief Same process as encryption but Aes key is AES_DECRYPTION type.
  665. \return 0 Success
  666. \param aes AES keys to use for block encrypt/decrypt
  667. \param out output buffer to hold plain text
  668. \param in input cipher text buffer to decrypt
  669. \param sz size of both out and in buffers
  670. \param i value to use for tweak
  671. \param iSz size of i buffer, should always be AES_BLOCK_SIZE but having
  672. this input adds a sanity check on how the user calls the
  673. function.
  674. _Example_
  675. \code
  676. XtsAes aes;
  677. unsigned char plain[SIZE];
  678. unsigned char cipher[SIZE];
  679. unsigned char i[AES_BLOCK_SIZE];
  680. //set up key with AES_DECRYPTION as dir and tweak with AES_ENCRYPTION
  681. if(wc_AesXtsDecrypt(&aes, plain, cipher, SIZE, i, sizeof(i)) != 0)
  682. {
  683. // Handle error
  684. }
  685. wc_AesXtsFree(&aes);
  686. \endcode
  687. \sa wc_AesXtsEncrypt
  688. \sa wc_AesXtsSetKey
  689. \sa wc_AesXtsFree
  690. */
  691. int wc_AesXtsDecrypt(XtsAes* aes, byte* out,
  692. const byte* in, word32 sz, const byte* i, word32 iSz);
  693. /*!
  694. \ingroup AES
  695. \brief This is to free up any resources used by the XtsAes structure
  696. \return 0 Success
  697. \param aes AES keys to free
  698. _Example_
  699. \code
  700. XtsAes aes;
  701. if(wc_AesXtsSetKey(&aes, key, sizeof(key), AES_ENCRYPTION, NULL, 0) != 0)
  702. {
  703. // Handle error
  704. }
  705. wc_AesXtsFree(&aes);
  706. \endcode
  707. \sa wc_AesXtsEncrypt
  708. \sa wc_AesXtsDecrypt
  709. \sa wc_AesXtsSetKey
  710. */
  711. int wc_AesXtsFree(XtsAes* aes);
  712. /*!
  713. \ingroup AES
  714. \brief Initialize Aes structure. Sets heap hint to be used and ID for use
  715. with async hardware. It is up to the user to call wc_AesFree on the Aes
  716. structure when done.
  717. \return 0 Success
  718. \param aes aes structure in to initialize
  719. \param heap heap hint to use for malloc / free if needed
  720. \param devId ID to use with async hardware
  721. _Example_
  722. \code
  723. Aes enc;
  724. void* hint = NULL;
  725. int devId = INVALID_DEVID; //if not using async INVALID_DEVID is default
  726. //heap hint could be set here if used
  727. wc_AesInit(&enc, hint, devId);
  728. \endcode
  729. \sa wc_AesSetKey
  730. \sa wc_AesSetIV
  731. \sa wc_AesFree
  732. */
  733. int wc_AesInit(Aes* aes, void* heap, int devId);
  734. /*!
  735. \ingroup AES
  736. \brief free resources associated with the Aes structure when applicable.
  737. Internally may sometimes be a no-op but still recommended to call in all
  738. cases as a general best-practice (IE if application code is ported for use
  739. on new environments where the call is applicable).
  740. \return no return (void function)
  741. \param aes aes structure in to free
  742. _Example_
  743. \code
  744. Aes enc;
  745. void* hint = NULL;
  746. int devId = INVALID_DEVID; //if not using async INVALID_DEVID is default
  747. //heap hint could be set here if used
  748. wc_AesInit(&enc, hint, devId);
  749. // ... do some interesting things ...
  750. wc_AesFree(&enc);
  751. \endcode
  752. \sa wc_AesInit
  753. */
  754. int wc_AesFree(Aes* aes);
  755. /*!
  756. \ingroup AES
  757. \brief AES with CFB mode.
  758. \return 0 Success and negative error values on failure
  759. \param aes AES keys to use for block encrypt/decrypt
  760. \param out output buffer to hold cipher text must be at least as large
  761. as inputbuffer)
  762. \param in input plain text buffer to encrypt
  763. \param sz size of input buffer
  764. _Example_
  765. \code
  766. Aes aes;
  767. unsigned char plain[SIZE];
  768. unsigned char cipher[SIZE];
  769. //set up key with AES_ENCRYPTION as dir for both encrypt and decrypt
  770. if(wc_AesCfbEncrypt(&aes, cipher, plain, SIZE) != 0)
  771. {
  772. // Handle error
  773. }
  774. \endcode
  775. \sa wc_AesCfbDecrypt
  776. \sa wc_AesSetKey
  777. */
  778. int wc_AesCfbEncrypt(Aes* aes, byte* out, const byte* in, word32 sz);
  779. /*!
  780. \ingroup AES
  781. \brief AES with CFB mode.
  782. \return 0 Success and negative error values on failure
  783. \param aes AES keys to use for block encrypt/decrypt
  784. \param out output buffer to hold decrypted text must be at least as large
  785. as inputbuffer)
  786. \param in input buffer to decrypt
  787. \param sz size of input buffer
  788. _Example_
  789. \code
  790. Aes aes;
  791. unsigned char plain[SIZE];
  792. unsigned char cipher[SIZE];
  793. //set up key with AES_ENCRYPTION as dir for both encrypt and decrypt
  794. if(wc_AesCfbDecrypt(&aes, plain, cipher, SIZE) != 0)
  795. {
  796. // Handle error
  797. }
  798. \endcode
  799. \sa wc_AesCfbEncrypt
  800. \sa wc_AesSetKey
  801. */
  802. int wc_AesCfbDecrypt(Aes* aes, byte* out, const byte* in, word32 sz);
  803. /*!
  804. \ingroup AES
  805. \brief This function performs SIV (synthetic initialization vector)
  806. encryption as described in RFC 5297.
  807. \return 0 On successful encryption.
  808. \return BAD_FUNC_ARG If key, SIV, or output buffer are NULL. Also returned
  809. if the key size isn't 32, 48, or 64 bytes.
  810. \return Other Other negative error values returned if AES or CMAC operations
  811. fail.
  812. \param key Byte buffer containing the key to use.
  813. \param keySz Length of the key buffer in bytes.
  814. \param assoc Additional, authenticated associated data (AD).
  815. \param assocSz Length of AD buffer in bytes.
  816. \param nonce A number used once. Used by the algorithm in the same manner as
  817. the AD.
  818. \param nonceSz Length of nonce buffer in bytes.
  819. \param in Plaintext buffer to encrypt.
  820. \param inSz Length of plaintext buffer.
  821. \param siv The SIV output by S2V (see RFC 5297 2.4).
  822. \param out Buffer to hold the ciphertext. Should be the same length as the
  823. plaintext buffer.
  824. _Example_
  825. \code
  826. byte key[] = { some 32, 48, or 64 byte key };
  827. byte assoc[] = {0x01, 0x2, 0x3};
  828. byte nonce[] = {0x04, 0x5, 0x6};
  829. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  830. byte siv[AES_BLOCK_SIZE];
  831. byte cipherText[sizeof(plainText)];
  832. if (wc_AesSivEncrypt(key, sizeof(key), assoc, sizeof(assoc), nonce,
  833. sizeof(nonce), plainText, sizeof(plainText), siv, cipherText) != 0) {
  834. // failed to encrypt
  835. }
  836. \endcode
  837. \sa wc_AesSivDecrypt
  838. */
  839. int wc_AesSivEncrypt(const byte* key, word32 keySz, const byte* assoc,
  840. word32 assocSz, const byte* nonce, word32 nonceSz,
  841. const byte* in, word32 inSz, byte* siv, byte* out);
  842. /*!
  843. \ingroup AES
  844. \brief This function performs SIV (synthetic initialization vector)
  845. decryption as described in RFC 5297.
  846. \return 0 On successful decryption.
  847. \return BAD_FUNC_ARG If key, SIV, or output buffer are NULL. Also returned
  848. if the key size isn't 32, 48, or 64 bytes.
  849. \return AES_SIV_AUTH_E If the SIV derived by S2V doesn't match the input
  850. SIV (see RFC 5297 2.7).
  851. \return Other Other negative error values returned if AES or CMAC operations
  852. fail.
  853. \param key Byte buffer containing the key to use.
  854. \param keySz Length of the key buffer in bytes.
  855. \param assoc Additional, authenticated associated data (AD).
  856. \param assocSz Length of AD buffer in bytes.
  857. \param nonce A number used once. Used by the underlying algorithm in the
  858. same manner as the AD.
  859. \param nonceSz Length of nonce buffer in bytes.
  860. \param in Ciphertext buffer to decrypt.
  861. \param inSz Length of ciphertext buffer.
  862. \param siv The SIV that accompanies the ciphertext (see RFC 5297 2.4).
  863. \param out Buffer to hold the decrypted plaintext. Should be the same length
  864. as the ciphertext buffer.
  865. _Example_
  866. \code
  867. byte key[] = { some 32, 48, or 64 byte key };
  868. byte assoc[] = {0x01, 0x2, 0x3};
  869. byte nonce[] = {0x04, 0x5, 0x6};
  870. byte cipherText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  871. byte siv[AES_BLOCK_SIZE] = { the SIV that came with the ciphertext };
  872. byte plainText[sizeof(cipherText)];
  873. if (wc_AesSivDecrypt(key, sizeof(key), assoc, sizeof(assoc), nonce,
  874. sizeof(nonce), cipherText, sizeof(cipherText), siv, plainText) != 0) {
  875. // failed to decrypt
  876. }
  877. \endcode
  878. \sa wc_AesSivEncrypt
  879. */
  880. int wc_AesSivDecrypt(const byte* key, word32 keySz, const byte* assoc,
  881. word32 assocSz, const byte* nonce, word32 nonceSz,
  882. const byte* in, word32 inSz, byte* siv, byte* out);