modes.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. /*
  2. * cifra - embedded cryptography library
  3. * Written in 2014 by Joseph Birr-Pixton <jpixton@gmail.com>
  4. *
  5. * To the extent possible under law, the author(s) have dedicated all
  6. * copyright and related and neighboring rights to this software to the
  7. * public domain worldwide. This software is distributed without any
  8. * warranty.
  9. *
  10. * You should have received a copy of the CC0 Public Domain Dedication
  11. * along with this software. If not, see
  12. * <http://creativecommons.org/publicdomain/zero/1.0/>.
  13. */
  14. #ifndef MODES_H
  15. #define MODES_H
  16. #include <stddef.h>
  17. #include <stdint.h>
  18. #include "gf128.h"
  19. #include "prp.h"
  20. /**
  21. * Block cipher modes
  22. * ==================
  23. */
  24. /**
  25. * CBC mode
  26. * --------
  27. * This implementation allows encryption or decryption of whole
  28. * blocks in CBC mode. It does not offer a byte-wise incremental
  29. * interface, or do any padding.
  30. *
  31. * This mode provides no useful integrity and should not be used
  32. * directly.
  33. */
  34. /* .. c:type:: cf_cbc
  35. * This structure binds together the things needed to encrypt/decrypt whole
  36. * blocks in CBC mode.
  37. *
  38. * .. c:member:: cf_cbc.prp
  39. * How to encrypt or decrypt blocks. This could be, for example, :c:data:`cf_aes`.
  40. *
  41. * .. c:member:: cf_cbc.prpctx
  42. * Private data for prp functions. For a `prp` of `cf_aes`, this would be a
  43. * pointer to a :c:type:`cf_aes_context` instance.
  44. *
  45. * .. c:member:: cf_cbc.block
  46. * The IV or last ciphertext block.
  47. */
  48. typedef struct
  49. {
  50. const cf_prp *prp;
  51. void *prpctx;
  52. uint8_t block[CF_MAXBLOCK];
  53. } cf_cbc;
  54. /* .. c:function:: $DECL
  55. * Initialise CBC encryption/decryption context using selected prp, prp context and IV. */
  56. void cf_cbc_init(cf_cbc *ctx, const cf_prp *prp, void *prpctx, const uint8_t iv[CF_MAXBLOCK]);
  57. /* .. c:function:: $DECL
  58. * Encrypt blocks in CBC mode. input and output
  59. * must point to blocks * ctx->prp->blocksz bytes of storage (and may alias). */
  60. void cf_cbc_encrypt(cf_cbc *ctx, const uint8_t *input, uint8_t *output, size_t blocks);
  61. /* .. c:function:: $DECL
  62. * Decrypt blocks in CBC mode. input and output
  63. * must point to blocks * ctx->prp->blocksz bytes of storage (and may alias). */
  64. void cf_cbc_decrypt(cf_cbc *ctx, const uint8_t *input, uint8_t *output, size_t blocks);
  65. /**
  66. * Counter mode
  67. * ------------
  68. * This implementation allows incremental encryption/decryption of
  69. * messages. Encryption and decryption are the same operation.
  70. *
  71. * The counter is always big-endian, but has configurable location
  72. * and size within the nonce block. The counter wraps, so you
  73. * should make sure the length of a message with a given nonce
  74. * doesn't cause nonce reuse.
  75. *
  76. * This mode provides no integrity and should not be used directly.
  77. */
  78. /* .. c:type:: cf_ctr
  79. *
  80. * .. c:member:: cf_ctr.prp
  81. * How to encrypt or decrypt blocks. This could be, for example, :c:data:`cf_aes`.
  82. *
  83. * .. c:member:: cf_ctr.prpctx
  84. * Private data for prp functions. For a `prp` of `cf_aes`, this would be a
  85. * pointer to a :c:type:`cf_aes_context` instance.
  86. *
  87. * .. c:member:: cf_ctr.nonce
  88. * The next block to encrypt to get another block of key stream.
  89. *
  90. * .. c:member:: cf_ctr.keymat
  91. * The current block of key stream.
  92. *
  93. * .. c:member:: cf_ctr.nkeymat
  94. * The number of bytes at the end of :c:member:`keymat` that are so-far unused.
  95. * If this is zero, all the bytes are used up and/or of undefined value.
  96. *
  97. * .. c:member:: cf_ctr.counter_offset
  98. * The offset (in bytes) of the counter block within the nonce.
  99. *
  100. * .. c:member:: cf_ctr.counter_width
  101. * The width (in bytes) of the counter block in the nonce.
  102. */
  103. typedef struct
  104. {
  105. const cf_prp *prp;
  106. void *prpctx;
  107. uint8_t nonce[CF_MAXBLOCK];
  108. uint8_t keymat[CF_MAXBLOCK];
  109. size_t nkeymat;
  110. size_t counter_offset;
  111. size_t counter_width;
  112. } cf_ctr;
  113. /* .. c:function:: $DECL
  114. * Initialise CTR encryption/decryption context using selected prp and nonce.
  115. * (nb, this only increments the whole nonce as a big endian block) */
  116. void cf_ctr_init(cf_ctr *ctx, const cf_prp *prp, void *prpctx, const uint8_t nonce[CF_MAXBLOCK]);
  117. /* .. c:function:: $DECL
  118. * Set the location and width of the nonce counter.
  119. *
  120. * eg. offset = 12, width = 4 means the counter is mod 2^32 and placed
  121. * at the end of the nonce. */
  122. void cf_ctr_custom_counter(cf_ctr *ctx, size_t offset, size_t width);
  123. /* .. c:function:: $DECL
  124. * Encrypt or decrypt bytes in CTR mode.
  125. * input and output may alias and must point to specified number of bytes. */
  126. void cf_ctr_cipher(cf_ctr *ctx, const uint8_t *input, uint8_t *output, size_t bytes);
  127. /* .. c:function:: $DECL
  128. * Discards the rest of this block of key stream. */
  129. void cf_ctr_discard_block(cf_ctr *ctx);
  130. /**
  131. * CBC-MAC
  132. * -------
  133. * This is a incremental interface to computing a CBC-MAC tag over a message.
  134. *
  135. * It optionally pads the message with PKCS#5/PKCS#7 padding -- if you don't
  136. * do this, messages must be an exact number of blocks long.
  137. *
  138. * You shouldn't use this directly because it isn't secure for variable-length
  139. * messages. Use CMAC instead.
  140. */
  141. /* .. c:type:: cf_cbcmac_stream
  142. * Stream interface to CBC-MAC signing.
  143. *
  144. * .. c:member:: cf_cbcmac.prp
  145. * How to encrypt or decrypt blocks. This could be, for example, :c:data:`cf_aes`.
  146. *
  147. * .. c:member:: cf_cbcmac.prpctx
  148. * Private data for prp functions. For a `prp` of `cf_aes`, this would be a
  149. * pointer to a :c:type:`cf_aes_context` instance.
  150. *
  151. * .. c:member:: cf_cbcmac.cbc
  152. * CBC data.
  153. *
  154. * .. c:member:: cf_cbcmac.buffer
  155. * Buffer for data which can't be processed until we have a full block.
  156. *
  157. * .. c:member:: cf_cbcmac.used
  158. * How many bytes at the front of :c:member:`buffer` are valid.
  159. */
  160. typedef struct
  161. {
  162. const cf_prp *prp;
  163. void *prpctx;
  164. cf_cbc cbc;
  165. uint8_t buffer[CF_MAXBLOCK];
  166. size_t used;
  167. } cf_cbcmac_stream;
  168. /* .. c:function:: $DECL
  169. * Initialise CBC-MAC signing context using selected prp. */
  170. void cf_cbcmac_stream_init(cf_cbcmac_stream *ctx, const cf_prp *prp, void *prpctx);
  171. /* .. c:function:: $DECL
  172. * Reset the streaming signing context, to sign a new message. */
  173. void cf_cbcmac_stream_reset(cf_cbcmac_stream *ctx);
  174. /* .. c:function:: $DECL
  175. * Process ndata bytes at data. */
  176. void cf_cbcmac_stream_update(cf_cbcmac_stream *ctx, const uint8_t *data, size_t ndata);
  177. /* .. c:function:: $DECL
  178. * Finish the current block of data by adding zeroes. Does nothing if there
  179. * are no bytes awaiting processing. */
  180. void cf_cbcmac_stream_finish_block_zero(cf_cbcmac_stream *ctx);
  181. /* .. c:function:: $DECL
  182. * Output the MAC to ctx->prp->blocksz bytes at out.
  183. * ctx->used must be zero: the inputed message must be an exact number of
  184. * blocks. */
  185. void cf_cbcmac_stream_nopad_final(cf_cbcmac_stream *ctx, uint8_t out[CF_MAXBLOCK]);
  186. /* .. c:function:: $DECL
  187. * Output the MAC to ctx->prp->blocksz bytes at out.
  188. *
  189. * The message is padded with PKCS#5 padding. */
  190. void cf_cbcmac_stream_pad_final(cf_cbcmac_stream *ctx, uint8_t out[CF_MAXBLOCK]);
  191. /**
  192. * CMAC
  193. * ----
  194. * This is both a one-shot and incremental interface to
  195. * computing a CMAC tag over a message.
  196. *
  197. * The one-shot interface separates out the per-key computation,
  198. * so if you need to compute lots of MACs with one key you don't
  199. * pay that cost more than once.
  200. *
  201. * CMAC is a good choice for a symmetric MAC.
  202. */
  203. /* .. c:type:: cf_cmac
  204. * One-shot interface to CMAC signing.
  205. *
  206. * .. c:member:: cf_cmac.prp
  207. * How to encrypt or decrypt blocks. This could be, for example, :c:data:`cf_aes`.
  208. *
  209. * .. c:member:: cf_cmac.prpctx
  210. * Private data for prp functions. For a `prp` of `cf_aes`, this would be a
  211. * pointer to a :c:type:`cf_aes_context` instance.
  212. *
  213. * .. c:member:: cf_cmac.B
  214. * The XOR offset for the last message block if it is a complete block
  215. * (also known as K\ :sub:`1`).
  216. *
  217. * .. c:member:: cf_cmac.P
  218. * The XOR offset for the last message block if it is a partial block
  219. * (also known as K\ :sub:`2`).
  220. */
  221. typedef struct
  222. {
  223. const cf_prp *prp;
  224. void *prpctx;
  225. uint8_t B[CF_MAXBLOCK];
  226. uint8_t P[CF_MAXBLOCK];
  227. } cf_cmac;
  228. /* .. c:function:: $DECL
  229. * Initialise CMAC signing context using selected prp. */
  230. void cf_cmac_init(cf_cmac *ctx, const cf_prp *prp, void *prpctx);
  231. /* .. c:function:: $DECL
  232. * CMAC sign the given data. The MAC is written to ctx->prp->blocksz
  233. * bytes at out. This is a one-shot function. */
  234. void cf_cmac_sign(cf_cmac *ctx, const uint8_t *data, size_t bytes,
  235. uint8_t out[CF_MAXBLOCK]);
  236. /* .. c:type:: cf_cmac_stream
  237. * Stream interface to CMAC signing.
  238. *
  239. * Input data in arbitrary chunks using :c:func:`cf_cmac_stream_update`.
  240. * The last bit of data must be signalled with the `isfinal` flag to
  241. * that function, and the data cannot be zero length unless the whole
  242. * message is empty.
  243. *
  244. * .. c:member:: cf_cmac_stream.cmac
  245. * CMAC one-shot data.
  246. *
  247. * .. c:member:: cf_cmac_stream.cbc
  248. * CBC block encryption data.
  249. *
  250. * .. c:member:: cf_cmac_stream.buffer
  251. * Buffer for data which can't be processed until we have a full block.
  252. *
  253. * .. c:member:: cf_cmac_stream.used
  254. * How many bytes at the front of :c:member:`buffer` are valid.
  255. *
  256. * .. c:member:: cf_cmac_stream.processed
  257. * How many bytes in total we've processed. This is used to correctly
  258. * process empty messages.
  259. *
  260. * .. c:member:: cf_cmac_stream.finalised
  261. * A flag set when the final chunk of the message has been processed.
  262. * Only when this flag is set can you get the MAC out.
  263. */
  264. typedef struct
  265. {
  266. cf_cmac cmac;
  267. cf_cbc cbc;
  268. uint8_t buffer[CF_MAXBLOCK];
  269. size_t used;
  270. size_t processed;
  271. int finalised;
  272. } cf_cmac_stream;
  273. /* .. c:function:: $DECL
  274. * Initialise CMAC streaming signing context using selected prp. */
  275. void cf_cmac_stream_init(cf_cmac_stream *ctx, const cf_prp *prp, void *prpctx);
  276. /* .. c:function:: $DECL
  277. * Reset the streaming signing context, to sign a new message. */
  278. void cf_cmac_stream_reset(cf_cmac_stream *ctx);
  279. /* .. c:function:: $DECL
  280. * Process ndata bytes at data. isfinal is non-zero if this is the last piece
  281. * of data. */
  282. void cf_cmac_stream_update(cf_cmac_stream *ctx, const uint8_t *data, size_t ndata,
  283. int isfinal);
  284. /* .. c:function:: $DECL
  285. * Output the MAC to ctx->cmac->prp->blocksz bytes at out.
  286. * cf_cmac_stream_update with isfinal non-zero must have been called
  287. * since the last _init/_reset. */
  288. void cf_cmac_stream_final(cf_cmac_stream *ctx, uint8_t out[CF_MAXBLOCK]);
  289. /**
  290. * EAX
  291. * ---
  292. *
  293. * The EAX authenticated encryption mode. This is a one-shot
  294. * interface.
  295. *
  296. * EAX is a pretty respectable and fast AEAD mode.
  297. */
  298. /* .. c:function:: $DECL
  299. * EAX authenticated encryption.
  300. *
  301. * This function does not fail.
  302. *
  303. * :param prp/prpctx: describe the block cipher to use.
  304. * :param plain: message plaintext.
  305. * :param nplain: length of message. May be zero.
  306. * :param header: additionally authenticated data (AAD).
  307. * :param nheader: length of AAD. May be zero.
  308. * :param nonce: nonce. This must not repeat for a given key.
  309. * :param nnonce: length of nonce. The nonce can be any length.
  310. * :param cipher: ciphertext output. `nplain` bytes are written here.
  311. * :param tag: authentication tag. `ntag` bytes are written here.
  312. * :param ntag: authentication tag length. This must be non-zero and no greater than `prp->blocksz`.
  313. */
  314. void cf_eax_encrypt(const cf_prp *prp, void *prpctx,
  315. const uint8_t *plain, size_t nplain,
  316. const uint8_t *header, size_t nheader,
  317. const uint8_t *nonce, size_t nnonce,
  318. uint8_t *cipher,
  319. uint8_t *tag, size_t ntag);
  320. /* .. c:function:: $DECL
  321. * EAX authenticated decryption.
  322. *
  323. * :return: 0 on success, non-zero on error. Nothing is written to plain on error.
  324. *
  325. * :param prp/prpctx: describe the block cipher to use.
  326. * :param cipher: message ciphertext.
  327. * :param ncipher: message length.
  328. * :param header: additionally authenticated data (AAD).
  329. * :param nheader: length of AAD.
  330. * :param nonce: nonce.
  331. * :param nnonce: length of nonce.
  332. * :param tag: authentication tag. `ntag` bytes are read from here.
  333. * :param ntag: authentication tag length.
  334. * :param plain: plaintext output. `ncipher` bytes are written here.
  335. */
  336. int cf_eax_decrypt(const cf_prp *prp, void *prpctx,
  337. const uint8_t *cipher, size_t ncipher,
  338. const uint8_t *header, size_t nheader,
  339. const uint8_t *nonce, size_t nnonce,
  340. const uint8_t *tag, size_t ntag,
  341. uint8_t *plain);
  342. /**
  343. * GCM
  344. * ---
  345. * The GCM ('Galois counter mode') authenticated encryption mode.
  346. * This is a one-shot interface.
  347. *
  348. * GCM is a reasonably respectable AEAD mode. It's somewhat more
  349. * complex than EAX, and side channel-free implementations can
  350. * be quite slow.
  351. */
  352. /* .. c:function:: $DECL
  353. * GCM authenticated encryption.
  354. *
  355. * This function does not fail.
  356. *
  357. * :param prp/prpctx: describe the block cipher to use.
  358. * :param plain: message plaintext.
  359. * :param nplain: length of message. May be zero.
  360. * :param header: additionally authenticated data (AAD).
  361. * :param nheader: length of AAD. May be zero.
  362. * :param nonce: nonce. This must not repeat for a given key.
  363. * :param nnonce: length of nonce. The nonce can be any length, but 12 bytes is strongly recommended.
  364. * :param cipher: ciphertext output. `nplain` bytes are written here.
  365. * :param tag: authentication tag. `ntag` bytes are written here.
  366. * :param ntag: authentication tag length. This must be non-zero and no greater than `prp->blocksz`.
  367. *
  368. * This function does not fail.
  369. */
  370. void cf_gcm_encrypt(const cf_prp *prp, void *prpctx,
  371. const uint8_t *plain, size_t nplain,
  372. const uint8_t *header, size_t nheader,
  373. const uint8_t *nonce, size_t nnonce,
  374. uint8_t *cipher,
  375. uint8_t *tag, size_t ntag);
  376. /* Incremental GHASH computation. */
  377. typedef struct
  378. {
  379. cf_gf128 H;
  380. cf_gf128 Y;
  381. uint8_t buffer[16];
  382. size_t buffer_used;
  383. uint64_t len_aad;
  384. uint64_t len_cipher;
  385. unsigned state;
  386. } ghash_ctx;
  387. typedef struct
  388. {
  389. cf_ctr ctr;
  390. ghash_ctx gh;
  391. uint8_t Y0[16];
  392. uint8_t e_Y0[16];
  393. } cf_gcm_ctx;
  394. void cf_gcm_encrypt_init(const cf_prp *prp, void *prpctx, cf_gcm_ctx *gcmctx,
  395. const uint8_t *header, size_t nheader,
  396. const uint8_t *nonce, size_t nnonce);
  397. void cf_gcm_encrypt_update(cf_gcm_ctx *gcmctx, const uint8_t *plain, size_t nplain, uint8_t *cipher);
  398. void cf_gcm_encrypt_final(cf_gcm_ctx *gcmctx, uint8_t *tag, size_t ntag);
  399. /* .. c:function:: $DECL
  400. * GCM authenticated decryption.
  401. *
  402. * :return: 0 on success, non-zero on error. Nothing is written to plain on error.
  403. *
  404. * :param prp: describe the block cipher to use.
  405. * :param prpctx: describe the block cipher to use.
  406. * :param cipher: message ciphertext.
  407. * :param ncipher: message length.
  408. * :param header: additionally authenticated data (AAD).
  409. * :param nheader: length of AAD.
  410. * :param nonce: nonce.
  411. * :param nnonce: length of nonce.
  412. * :param tag: authentication tag. `ntag` bytes are read from here.
  413. * :param ntag: authentication tag length.
  414. * :param plain: plaintext output. `ncipher` bytes are written here.
  415. */
  416. int cf_gcm_decrypt(const cf_prp *prp, void *prpctx,
  417. const uint8_t *cipher, size_t ncipher,
  418. const uint8_t *header, size_t nheader,
  419. const uint8_t *nonce, size_t nnonce,
  420. const uint8_t *tag, size_t ntag,
  421. uint8_t *plain);
  422. /**
  423. * CCM
  424. * ---
  425. *
  426. * The CCM ('Counter with CBC-MAC') authenticated encryption mode.
  427. * CCM is a widely used AEAD mode (in IPSec, WPA2, Bluetooth, etc.)
  428. *
  429. * It works (at a high level) by just gluing together CTR and CBC-MAC
  430. * modes (in MAC-then-encrypt mode) and then fixing the problems inherent
  431. * with CBC-MAC in over-complicated ways.
  432. *
  433. * This is a one-shot interface, which is good because the underlying
  434. * mechanism isn't actually online: you need to know the message length
  435. * before you start, or do everything in two passes.
  436. */
  437. /* .. c:function:: $DECL
  438. * CCM authenticated encryption.
  439. *
  440. * This function does not fail.
  441. *
  442. * :param prp/prpctx: describe the block cipher to use.
  443. * :param plain: message plaintext.
  444. * :param nplain: length of message. May be zero. Must meet the constraints placed on it by `L`.
  445. * :param L: length of the message length encoding. This must be in the interval `[2,8]` and gives a maximum message size of 2\ :sup:`8L` bytes.
  446. * :param header: additionally authenticated data (AAD).
  447. * :param nheader: length of AAD. May be zero.
  448. * :param nonce: nonce. This must not repeat for a given key.
  449. * :param nnonce: length of nonce. Must be exactly `15 - L` bytes for a 128-bit block cipher.
  450. * :param cipher: ciphertext output. `nplain` bytes are written here.
  451. * :param tag: authentication tag. `ntag` bytes are written here.
  452. * :param ntag: authentication tag length. This must be 4, 6, 8, 10, 12, 14 or 16.
  453. */
  454. void cf_ccm_encrypt(const cf_prp *prp, void *prpctx,
  455. const uint8_t *plain, size_t nplain, size_t L,
  456. const uint8_t *header, size_t nheader,
  457. const uint8_t *nonce, size_t nnonce,
  458. uint8_t *cipher,
  459. uint8_t *tag, size_t ntag);
  460. /* .. c:function:: $DECL
  461. * CCM authenticated decryption.
  462. *
  463. * :return: 0 on success, non-zero on error. Plain is cleared on error.
  464. *
  465. * :param prp: describe the block cipher to use.
  466. * :param prpctx: describe the block cipher to use.
  467. * :param cipher: message ciphertext.
  468. * :param ncipher: length of message.
  469. * :param L: length of the message length encoding. See :c:func:`cf_ccm_encrypt`.
  470. * :param header: additionally authenticated data (AAD).
  471. * :param nheader: length of AAD.
  472. * :param nonce: nonce.
  473. * :param nnonce: length of nonce.
  474. * :param tag: authentication tag. `ntag` bytes are read from here.
  475. * :param ntag: authentication tag length. This must be 4, 6, 8, 10, 12, 14 or 16.
  476. * :param plain: plaintext output. `ncipher` bytes are written here.
  477. */
  478. int cf_ccm_decrypt(const cf_prp *prp, void *prpctx,
  479. const uint8_t *cipher, size_t ncipher, size_t L,
  480. const uint8_t *header, size_t nheader,
  481. const uint8_t *nonce, size_t nnonce,
  482. const uint8_t *tag, size_t ntag,
  483. uint8_t *plain);
  484. /**
  485. * OCB
  486. * ---
  487. *
  488. * OCB is an authenticated encryption mode by Phil Rogaway.
  489. *
  490. * This is version 3, as standardised in RFC7253. It's defined
  491. * only for block ciphers with a 128-bit block size.
  492. *
  493. * This is a one-shot interface.
  494. */
  495. /* .. c:function:: $DECL
  496. * OCB authenticated encryption.
  497. *
  498. * This function does not fail.
  499. *
  500. * :param prp/prpctx: describe the block cipher to use.
  501. * :param plain: message plaintext.
  502. * :param nplain: length of message. May be zero.
  503. * :param header: additionally authenticated data (AAD).
  504. * :param nheader: length of AAD. May be zero.
  505. * :param nonce: nonce. This must not repeat for a given key.
  506. * :param nnonce: length of nonce. Must be 15 or fewer bytes.
  507. * :param cipher: ciphertext output. `nplain` bytes are written here.
  508. * :param tag: authentication tag. `ntag` bytes are written here.
  509. * :param ntag: authentication tag length. Must be 16 or fewer bytes.
  510. */
  511. void cf_ocb_encrypt(const cf_prp *prp, void *prpctx,
  512. const uint8_t *plain, size_t nplain,
  513. const uint8_t *header, size_t nheader,
  514. const uint8_t *nonce, size_t nnonce,
  515. uint8_t *cipher,
  516. uint8_t *tag, size_t ntag);
  517. /* .. c:function:: $DECL
  518. * OCB authenticated decryption.
  519. *
  520. * :return: 0 on success, non-zero on error. `plain` is cleared on error.
  521. *
  522. * :param prp: describe the block cipher to use.
  523. * :param prpctx: describe the block cipher to use.
  524. * :param cipher: message ciphertext.
  525. * :param ncipher: length of message.
  526. * :param header: additionally authenticated data (AAD).
  527. * :param nheader: length of AAD.
  528. * :param nonce: nonce.
  529. * :param nnonce: length of nonce.
  530. * :param tag: authentication tag. `ntag` bytes are read from here.
  531. * :param ntag: authentication tag length.
  532. * :param plain: plaintext output. `ncipher` bytes are written here.
  533. */
  534. int cf_ocb_decrypt(const cf_prp *prp, void *prpctx,
  535. const uint8_t *cipher, size_t ncipher,
  536. const uint8_t *header, size_t nheader,
  537. const uint8_t *nonce, size_t nnonce,
  538. const uint8_t *tag, size_t ntag,
  539. uint8_t *plain);
  540. #endif