Path: blob/main/contrib/bearssl/inc/bearssl_aead.h
39586 views
/*1* Copyright (c) 2017 Thomas Pornin <[email protected]>2*3* Permission is hereby granted, free of charge, to any person obtaining4* a copy of this software and associated documentation files (the5* "Software"), to deal in the Software without restriction, including6* without limitation the rights to use, copy, modify, merge, publish,7* distribute, sublicense, and/or sell copies of the Software, and to8* permit persons to whom the Software is furnished to do so, subject to9* the following conditions:10*11* The above copyright notice and this permission notice shall be12* included in all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,15* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF16* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND17* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS18* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN19* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN20* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*/2324#ifndef BR_BEARSSL_AEAD_H__25#define BR_BEARSSL_AEAD_H__2627#include <stddef.h>28#include <stdint.h>2930#include "bearssl_block.h"31#include "bearssl_hash.h"3233#ifdef __cplusplus34extern "C" {35#endif3637/** \file bearssl_aead.h38*39* # Authenticated Encryption with Additional Data40*41* This file documents the API for AEAD encryption.42*43*44* ## Procedural API45*46* An AEAD algorithm processes messages and provides confidentiality47* (encryption) and checked integrity (MAC). It uses the following48* parameters:49*50* - A symmetric key. Exact size depends on the AEAD algorithm.51*52* - A nonce (IV). Size depends on the AEAD algorithm; for most53* algorithms, it is crucial for security that any given nonce54* value is never used twice for the same key and distinct55* messages.56*57* - Data to encrypt and protect.58*59* - Additional authenticated data, which is covered by the MAC but60* otherwise left untouched (i.e. not encrypted).61*62* The AEAD algorithm encrypts the data, and produces an authentication63* tag. It is assumed that the encrypted data, the tag, the additional64* authenticated data and the nonce are sent to the receiver; the65* additional data and the nonce may be implicit (e.g. using elements of66* the underlying transport protocol, such as record sequence numbers).67* The receiver will recompute the tag value and compare it with the one68* received; if they match, then the data is correct, and can be69* decrypted and used; otherwise, at least one of the elements was70* altered in transit, normally leading to wholesale rejection of the71* complete message.72*73* For each AEAD algorithm, identified by a symbolic name (hereafter74* denoted as "`xxx`"), the following functions are defined:75*76* - `br_xxx_init()`77*78* Initialise the AEAD algorithm, on a provided context structure.79* Exact parameters depend on the algorithm, and may include80* pointers to extra implementations and context structures. The81* secret key is provided at this point, either directly or82* indirectly.83*84* - `br_xxx_reset()`85*86* Start a new AEAD computation. The nonce value is provided as87* parameter to this function.88*89* - `br_xxx_aad_inject()`90*91* Inject some additional authenticated data. Additional data may92* be provided in several chunks of arbitrary length.93*94* - `br_xxx_flip()`95*96* This function MUST be called after injecting all additional97* authenticated data, and before beginning to encrypt the plaintext98* (or decrypt the ciphertext).99*100* - `br_xxx_run()`101*102* Process some plaintext (to encrypt) or ciphertext (to decrypt).103* Encryption/decryption is done in place. Data may be provided in104* several chunks of arbitrary length.105*106* - `br_xxx_get_tag()`107*108* Compute the authentication tag. All message data (encrypted or109* decrypted) must have been injected at that point. Also, this110* call may modify internal context elements, so it may be called111* only once for a given AEAD computation.112*113* - `br_xxx_check_tag()`114*115* An alternative to `br_xxx_get_tag()`, meant to be used by the116* receiver: the authentication tag is internally recomputed, and117* compared with the one provided as parameter.118*119* This API makes the following assumptions on the AEAD algorithm:120*121* - Encryption does not expand the size of the ciphertext; there is122* no padding. This is true of most modern AEAD modes such as GCM.123*124* - The additional authenticated data must be processed first,125* before the encrypted/decrypted data.126*127* - Nonce, plaintext and additional authenticated data all consist128* in an integral number of bytes. There is no provision to use129* elements whose length in bits is not a multiple of 8.130*131* Each AEAD algorithm has its own requirements and limits on the sizes132* of additional data and plaintext. This API does not provide any133* way to report invalid usage; it is up to the caller to ensure that134* the provided key, nonce, and data elements all fit the algorithm's135* requirements.136*137*138* ## Object-Oriented API139*140* Each context structure begins with a field (called `vtable`) that141* points to an instance of a structure that references the relevant142* functions through pointers. Each such structure contains the143* following:144*145* - `reset`146*147* Pointer to the reset function, that allows starting a new148* computation.149*150* - `aad_inject`151*152* Pointer to the additional authenticated data injection function.153*154* - `flip`155*156* Pointer to the function that transitions from additional data157* to main message data processing.158*159* - `get_tag`160*161* Pointer to the function that computes and returns the tag.162*163* - `check_tag`164*165* Pointer to the function that computes and verifies the tag against166* a received value.167*168* Note that there is no OOP method for context initialisation: the169* various AEAD algorithms have different requirements that would not170* map well to a single initialisation API.171*172* The OOP API is not provided for CCM, due to its specific requirements173* (length of plaintext must be known in advance).174*/175176/**177* \brief Class type of an AEAD algorithm.178*/179typedef struct br_aead_class_ br_aead_class;180struct br_aead_class_ {181182/**183* \brief Size (in bytes) of authentication tags created by184* this AEAD algorithm.185*/186size_t tag_size;187188/**189* \brief Reset an AEAD context.190*191* This function resets an already initialised AEAD context for192* a new computation run. Implementations and keys are193* conserved. This function can be called at any time; it194* cancels any ongoing AEAD computation that uses the provided195* context structure.196197* The provided IV is a _nonce_. Each AEAD algorithm has its198* own requirements on IV size and contents; for most of them,199* it is crucial to security that each nonce value is used200* only once for a given secret key.201*202* \param cc AEAD context structure.203* \param iv AEAD nonce to use.204* \param len AEAD nonce length (in bytes).205*/206void (*reset)(const br_aead_class **cc, const void *iv, size_t len);207208/**209* \brief Inject additional authenticated data.210*211* The provided data is injected into a running AEAD212* computation. Additional data must be injected _before_ the213* call to `flip()`. Additional data can be injected in several214* chunks of arbitrary length.215*216* \param cc AEAD context structure.217* \param data pointer to additional authenticated data.218* \param len length of additional authenticated data (in bytes).219*/220void (*aad_inject)(const br_aead_class **cc,221const void *data, size_t len);222223/**224* \brief Finish injection of additional authenticated data.225*226* This function MUST be called before beginning the actual227* encryption or decryption (with `run()`), even if no228* additional authenticated data was injected. No additional229* authenticated data may be injected after this function call.230*231* \param cc AEAD context structure.232*/233void (*flip)(const br_aead_class **cc);234235/**236* \brief Encrypt or decrypt some data.237*238* Data encryption or decryption can be done after `flip()` has239* been called on the context. If `encrypt` is non-zero, then240* the provided data shall be plaintext, and it is encrypted in241* place. Otherwise, the data shall be ciphertext, and it is242* decrypted in place.243*244* Data may be provided in several chunks of arbitrary length.245*246* \param cc AEAD context structure.247* \param encrypt non-zero for encryption, zero for decryption.248* \param data data to encrypt or decrypt.249* \param len data length (in bytes).250*/251void (*run)(const br_aead_class **cc, int encrypt,252void *data, size_t len);253254/**255* \brief Compute authentication tag.256*257* Compute the AEAD authentication tag. The tag length depends258* on the AEAD algorithm; it is written in the provided `tag`259* buffer. This call terminates the AEAD run: no data may be260* processed with that AEAD context afterwards, until `reset()`261* is called to initiate a new AEAD run.262*263* The tag value must normally be sent along with the encrypted264* data. When decrypting, the tag value must be recomputed and265* compared with the received tag: if the two tag values differ,266* then either the tag or the encrypted data was altered in267* transit. As an alternative to this function, the268* `check_tag()` function may be used to compute and check the269* tag value.270*271* Tag length depends on the AEAD algorithm.272*273* \param cc AEAD context structure.274* \param tag destination buffer for the tag.275*/276void (*get_tag)(const br_aead_class **cc, void *tag);277278/**279* \brief Compute and check authentication tag.280*281* This function is an alternative to `get_tag()`, and is282* normally used on the receiving end (i.e. when decrypting283* messages). The tag value is recomputed and compared with the284* provided tag value. If they match, 1 is returned; on285* mismatch, 0 is returned. A returned value of 0 means that the286* data or the tag was altered in transit, normally leading to287* wholesale rejection of the complete message.288*289* Tag length depends on the AEAD algorithm.290*291* \param cc AEAD context structure.292* \param tag tag value to compare with.293* \return 1 on success (exact match of tag value), 0 otherwise.294*/295uint32_t (*check_tag)(const br_aead_class **cc, const void *tag);296297/**298* \brief Compute authentication tag (with truncation).299*300* This function is similar to `get_tag()`, except that the tag301* length is provided. Some AEAD algorithms allow several tag302* lengths, usually by truncating the normal tag. Shorter tags303* mechanically increase success probability of forgeries.304* The range of allowed tag lengths depends on the algorithm.305*306* \param cc AEAD context structure.307* \param tag destination buffer for the tag.308* \param len tag length (in bytes).309*/310void (*get_tag_trunc)(const br_aead_class **cc, void *tag, size_t len);311312/**313* \brief Compute and check authentication tag (with truncation).314*315* This function is similar to `check_tag()` except that it316* works over an explicit tag length. See `get_tag()` for a317* discussion of explicit tag lengths; the range of allowed tag318* lengths depends on the algorithm.319*320* \param cc AEAD context structure.321* \param tag tag value to compare with.322* \param len tag length (in bytes).323* \return 1 on success (exact match of tag value), 0 otherwise.324*/325uint32_t (*check_tag_trunc)(const br_aead_class **cc,326const void *tag, size_t len);327};328329/**330* \brief Context structure for GCM.331*332* GCM is an AEAD mode that combines a block cipher in CTR mode with a333* MAC based on GHASH, to provide authenticated encryption:334*335* - Any block cipher with 16-byte blocks can be used with GCM.336*337* - The nonce can have any length, from 0 up to 2^64-1 bits; however,338* 96-bit nonces (12 bytes) are recommended (nonces with a length339* distinct from 12 bytes are internally hashed, which risks reusing340* nonce value with a small but not always negligible probability).341*342* - Additional authenticated data may have length up to 2^64-1 bits.343*344* - Message length may range up to 2^39-256 bits at most.345*346* - The authentication tag has length 16 bytes.347*348* The GCM initialisation function receives as parameter an349* _initialised_ block cipher implementation context, with the secret350* key already set. A pointer to that context will be kept within the351* GCM context structure. It is up to the caller to allocate and352* initialise that block cipher context.353*/354typedef struct {355/** \brief Pointer to vtable for this context. */356const br_aead_class *vtable;357358#ifndef BR_DOXYGEN_IGNORE359const br_block_ctr_class **bctx;360br_ghash gh;361unsigned char h[16];362unsigned char j0_1[12];363unsigned char buf[16];364unsigned char y[16];365uint32_t j0_2, jc;366uint64_t count_aad, count_ctr;367#endif368} br_gcm_context;369370/**371* \brief Initialize a GCM context.372*373* A block cipher implementation, with its initialised context structure,374* is provided. The block cipher MUST use 16-byte blocks in CTR mode,375* and its secret key MUST have been already set in the provided context.376* A GHASH implementation must also be provided. The parameters are linked377* in the GCM context.378*379* After this function has been called, the `br_gcm_reset()` function must380* be called, to provide the IV for GCM computation.381*382* \param ctx GCM context structure.383* \param bctx block cipher context (already initialised with secret key).384* \param gh GHASH implementation.385*/386void br_gcm_init(br_gcm_context *ctx,387const br_block_ctr_class **bctx, br_ghash gh);388389/**390* \brief Reset a GCM context.391*392* This function resets an already initialised GCM context for a new393* computation run. Implementations and keys are conserved. This function394* can be called at any time; it cancels any ongoing GCM computation that395* uses the provided context structure.396*397* The provided IV is a _nonce_. It is critical to GCM security that IV398* values are not repeated for the same encryption key. IV can have399* arbitrary length (up to 2^64-1 bits), but the "normal" length is400* 96 bits (12 bytes).401*402* \param ctx GCM context structure.403* \param iv GCM nonce to use.404* \param len GCM nonce length (in bytes).405*/406void br_gcm_reset(br_gcm_context *ctx, const void *iv, size_t len);407408/**409* \brief Inject additional authenticated data into GCM.410*411* The provided data is injected into a running GCM computation. Additional412* data must be injected _before_ the call to `br_gcm_flip()`.413* Additional data can be injected in several chunks of arbitrary length;414* the maximum total size of additional authenticated data is 2^64-1415* bits.416*417* \param ctx GCM context structure.418* \param data pointer to additional authenticated data.419* \param len length of additional authenticated data (in bytes).420*/421void br_gcm_aad_inject(br_gcm_context *ctx, const void *data, size_t len);422423/**424* \brief Finish injection of additional authenticated data into GCM.425*426* This function MUST be called before beginning the actual encryption427* or decryption (with `br_gcm_run()`), even if no additional authenticated428* data was injected. No additional authenticated data may be injected429* after this function call.430*431* \param ctx GCM context structure.432*/433void br_gcm_flip(br_gcm_context *ctx);434435/**436* \brief Encrypt or decrypt some data with GCM.437*438* Data encryption or decryption can be done after `br_gcm_flip()`439* has been called on the context. If `encrypt` is non-zero, then the440* provided data shall be plaintext, and it is encrypted in place.441* Otherwise, the data shall be ciphertext, and it is decrypted in place.442*443* Data may be provided in several chunks of arbitrary length. The maximum444* total length for data is 2^39-256 bits, i.e. about 65 gigabytes.445*446* \param ctx GCM context structure.447* \param encrypt non-zero for encryption, zero for decryption.448* \param data data to encrypt or decrypt.449* \param len data length (in bytes).450*/451void br_gcm_run(br_gcm_context *ctx, int encrypt, void *data, size_t len);452453/**454* \brief Compute GCM authentication tag.455*456* Compute the GCM authentication tag. The tag is a 16-byte value which457* is written in the provided `tag` buffer. This call terminates the458* GCM run: no data may be processed with that GCM context afterwards,459* until `br_gcm_reset()` is called to initiate a new GCM run.460*461* The tag value must normally be sent along with the encrypted data.462* When decrypting, the tag value must be recomputed and compared with463* the received tag: if the two tag values differ, then either the tag464* or the encrypted data was altered in transit. As an alternative to465* this function, the `br_gcm_check_tag()` function can be used to466* compute and check the tag value.467*468* \param ctx GCM context structure.469* \param tag destination buffer for the tag (16 bytes).470*/471void br_gcm_get_tag(br_gcm_context *ctx, void *tag);472473/**474* \brief Compute and check GCM authentication tag.475*476* This function is an alternative to `br_gcm_get_tag()`, normally used477* on the receiving end (i.e. when decrypting value). The tag value is478* recomputed and compared with the provided tag value. If they match, 1479* is returned; on mismatch, 0 is returned. A returned value of 0 means480* that the data or the tag was altered in transit, normally leading to481* wholesale rejection of the complete message.482*483* \param ctx GCM context structure.484* \param tag tag value to compare with (16 bytes).485* \return 1 on success (exact match of tag value), 0 otherwise.486*/487uint32_t br_gcm_check_tag(br_gcm_context *ctx, const void *tag);488489/**490* \brief Compute GCM authentication tag (with truncation).491*492* This function is similar to `br_gcm_get_tag()`, except that it allows493* the tag to be truncated to a smaller length. The intended tag length494* is provided as `len` (in bytes); it MUST be no more than 16, but495* it may be smaller. Note that decreasing tag length mechanically makes496* forgeries easier; NIST SP 800-38D specifies that the tag length shall497* lie between 12 and 16 bytes (inclusive), but may be truncated down to498* 4 or 8 bytes, for specific applications that can tolerate it. It must499* also be noted that successful forgeries leak information on the500* authentication key, making subsequent forgeries easier. Therefore,501* tag truncation, and in particular truncation to sizes lower than 12502* bytes, shall be envisioned only with great care.503*504* The tag is written in the provided `tag` buffer. This call terminates505* the GCM run: no data may be processed with that GCM context506* afterwards, until `br_gcm_reset()` is called to initiate a new GCM507* run.508*509* The tag value must normally be sent along with the encrypted data.510* When decrypting, the tag value must be recomputed and compared with511* the received tag: if the two tag values differ, then either the tag512* or the encrypted data was altered in transit. As an alternative to513* this function, the `br_gcm_check_tag_trunc()` function can be used to514* compute and check the tag value.515*516* \param ctx GCM context structure.517* \param tag destination buffer for the tag.518* \param len tag length (16 bytes or less).519*/520void br_gcm_get_tag_trunc(br_gcm_context *ctx, void *tag, size_t len);521522/**523* \brief Compute and check GCM authentication tag (with truncation).524*525* This function is an alternative to `br_gcm_get_tag_trunc()`, normally used526* on the receiving end (i.e. when decrypting value). The tag value is527* recomputed and compared with the provided tag value. If they match, 1528* is returned; on mismatch, 0 is returned. A returned value of 0 means529* that the data or the tag was altered in transit, normally leading to530* wholesale rejection of the complete message.531*532* Tag length MUST be 16 bytes or less. The normal GCM tag length is 16533* bytes. See `br_check_tag_trunc()` for some discussion on the potential534* perils of truncating authentication tags.535*536* \param ctx GCM context structure.537* \param tag tag value to compare with.538* \param len tag length (in bytes).539* \return 1 on success (exact match of tag value), 0 otherwise.540*/541uint32_t br_gcm_check_tag_trunc(br_gcm_context *ctx,542const void *tag, size_t len);543544/**545* \brief Class instance for GCM.546*/547extern const br_aead_class br_gcm_vtable;548549/**550* \brief Context structure for EAX.551*552* EAX is an AEAD mode that combines a block cipher in CTR mode with553* CBC-MAC using the same block cipher and the same key, to provide554* authenticated encryption:555*556* - Any block cipher with 16-byte blocks can be used with EAX557* (technically, other block sizes are defined as well, but this558* is not implemented by these functions; shorter blocks also559* imply numerous security issues).560*561* - The nonce can have any length, as long as nonce values are562* not reused (thus, if nonces are randomly selected, the nonce563* size should be such that reuse probability is negligible).564*565* - Additional authenticated data length is unlimited.566*567* - Message length is unlimited.568*569* - The authentication tag has length 16 bytes.570*571* The EAX initialisation function receives as parameter an572* _initialised_ block cipher implementation context, with the secret573* key already set. A pointer to that context will be kept within the574* EAX context structure. It is up to the caller to allocate and575* initialise that block cipher context.576*/577typedef struct {578/** \brief Pointer to vtable for this context. */579const br_aead_class *vtable;580581#ifndef BR_DOXYGEN_IGNORE582const br_block_ctrcbc_class **bctx;583unsigned char L2[16];584unsigned char L4[16];585unsigned char nonce[16];586unsigned char head[16];587unsigned char ctr[16];588unsigned char cbcmac[16];589unsigned char buf[16];590size_t ptr;591#endif592} br_eax_context;593594/**595* \brief EAX captured state.596*597* Some internal values computed by EAX may be captured at various598* points, and reused for another EAX run with the same secret key,599* for lower per-message overhead. Captured values do not depend on600* the nonce.601*/602typedef struct {603#ifndef BR_DOXYGEN_IGNORE604unsigned char st[3][16];605#endif606} br_eax_state;607608/**609* \brief Initialize an EAX context.610*611* A block cipher implementation, with its initialised context612* structure, is provided. The block cipher MUST use 16-byte blocks in613* CTR + CBC-MAC mode, and its secret key MUST have been already set in614* the provided context. The parameters are linked in the EAX context.615*616* After this function has been called, the `br_eax_reset()` function must617* be called, to provide the nonce for EAX computation.618*619* \param ctx EAX context structure.620* \param bctx block cipher context (already initialised with secret key).621*/622void br_eax_init(br_eax_context *ctx, const br_block_ctrcbc_class **bctx);623624/**625* \brief Capture pre-AAD state.626*627* This function precomputes key-dependent data, and stores it in the628* provided `st` structure. This structure should then be used with629* `br_eax_reset_pre_aad()`, or updated with `br_eax_get_aad_mac()`630* and then used with `br_eax_reset_post_aad()`.631*632* The EAX context structure is unmodified by this call.633*634* \param ctx EAX context structure.635* \param st recipient for captured state.636*/637void br_eax_capture(const br_eax_context *ctx, br_eax_state *st);638639/**640* \brief Reset an EAX context.641*642* This function resets an already initialised EAX context for a new643* computation run. Implementations and keys are conserved. This function644* can be called at any time; it cancels any ongoing EAX computation that645* uses the provided context structure.646*647* It is critical to EAX security that nonce values are not repeated for648* the same encryption key. Nonces can have arbitrary length. If nonces649* are randomly generated, then a nonce length of at least 128 bits (16650* bytes) is recommended, to make nonce reuse probability sufficiently651* low.652*653* \param ctx EAX context structure.654* \param nonce EAX nonce to use.655* \param len EAX nonce length (in bytes).656*/657void br_eax_reset(br_eax_context *ctx, const void *nonce, size_t len);658659/**660* \brief Reset an EAX context with a pre-AAD captured state.661*662* This function is an alternative to `br_eax_reset()`, that reuses a663* previously captured state structure for lower per-message overhead.664* The state should have been populated with `br_eax_capture_state()`665* but not updated with `br_eax_get_aad_mac()`.666*667* After this function is called, additional authenticated data MUST668* be injected. At least one byte of additional authenticated data669* MUST be provided with `br_eax_aad_inject()`; computation result will670* be incorrect if `br_eax_flip()` is called right away.671*672* After injection of the AAD and call to `br_eax_flip()`, at least673* one message byte must be provided. Empty messages are not supported674* with this reset mode.675*676* \param ctx EAX context structure.677* \param st pre-AAD captured state.678* \param nonce EAX nonce to use.679* \param len EAX nonce length (in bytes).680*/681void br_eax_reset_pre_aad(br_eax_context *ctx, const br_eax_state *st,682const void *nonce, size_t len);683684/**685* \brief Reset an EAX context with a post-AAD captured state.686*687* This function is an alternative to `br_eax_reset()`, that reuses a688* previously captured state structure for lower per-message overhead.689* The state should have been populated with `br_eax_capture_state()`690* and then updated with `br_eax_get_aad_mac()`.691*692* After this function is called, message data MUST be injected. The693* `br_eax_flip()` function MUST NOT be called. At least one byte of694* message data MUST be provided with `br_eax_run()`; empty messages695* are not supported with this reset mode.696*697* \param ctx EAX context structure.698* \param st post-AAD captured state.699* \param nonce EAX nonce to use.700* \param len EAX nonce length (in bytes).701*/702void br_eax_reset_post_aad(br_eax_context *ctx, const br_eax_state *st,703const void *nonce, size_t len);704705/**706* \brief Inject additional authenticated data into EAX.707*708* The provided data is injected into a running EAX computation. Additional709* data must be injected _before_ the call to `br_eax_flip()`.710* Additional data can be injected in several chunks of arbitrary length;711* the total amount of additional authenticated data is unlimited.712*713* \param ctx EAX context structure.714* \param data pointer to additional authenticated data.715* \param len length of additional authenticated data (in bytes).716*/717void br_eax_aad_inject(br_eax_context *ctx, const void *data, size_t len);718719/**720* \brief Finish injection of additional authenticated data into EAX.721*722* This function MUST be called before beginning the actual encryption723* or decryption (with `br_eax_run()`), even if no additional authenticated724* data was injected. No additional authenticated data may be injected725* after this function call.726*727* \param ctx EAX context structure.728*/729void br_eax_flip(br_eax_context *ctx);730731/**732* \brief Obtain a copy of the MAC on additional authenticated data.733*734* This function may be called only after `br_eax_flip()`; it copies the735* AAD-specific MAC value into the provided state. The MAC value depends736* on the secret key and the additional data itself, but not on the737* nonce. The updated state `st` is meant to be used as parameter for a738* further `br_eax_reset_post_aad()` call.739*740* \param ctx EAX context structure.741* \param st captured state to update.742*/743static inline void744br_eax_get_aad_mac(const br_eax_context *ctx, br_eax_state *st)745{746memcpy(st->st[1], ctx->head, sizeof ctx->head);747}748749/**750* \brief Encrypt or decrypt some data with EAX.751*752* Data encryption or decryption can be done after `br_eax_flip()`753* has been called on the context. If `encrypt` is non-zero, then the754* provided data shall be plaintext, and it is encrypted in place.755* Otherwise, the data shall be ciphertext, and it is decrypted in place.756*757* Data may be provided in several chunks of arbitrary length.758*759* \param ctx EAX context structure.760* \param encrypt non-zero for encryption, zero for decryption.761* \param data data to encrypt or decrypt.762* \param len data length (in bytes).763*/764void br_eax_run(br_eax_context *ctx, int encrypt, void *data, size_t len);765766/**767* \brief Compute EAX authentication tag.768*769* Compute the EAX authentication tag. The tag is a 16-byte value which770* is written in the provided `tag` buffer. This call terminates the771* EAX run: no data may be processed with that EAX context afterwards,772* until `br_eax_reset()` is called to initiate a new EAX run.773*774* The tag value must normally be sent along with the encrypted data.775* When decrypting, the tag value must be recomputed and compared with776* the received tag: if the two tag values differ, then either the tag777* or the encrypted data was altered in transit. As an alternative to778* this function, the `br_eax_check_tag()` function can be used to779* compute and check the tag value.780*781* \param ctx EAX context structure.782* \param tag destination buffer for the tag (16 bytes).783*/784void br_eax_get_tag(br_eax_context *ctx, void *tag);785786/**787* \brief Compute and check EAX authentication tag.788*789* This function is an alternative to `br_eax_get_tag()`, normally used790* on the receiving end (i.e. when decrypting value). The tag value is791* recomputed and compared with the provided tag value. If they match, 1792* is returned; on mismatch, 0 is returned. A returned value of 0 means793* that the data or the tag was altered in transit, normally leading to794* wholesale rejection of the complete message.795*796* \param ctx EAX context structure.797* \param tag tag value to compare with (16 bytes).798* \return 1 on success (exact match of tag value), 0 otherwise.799*/800uint32_t br_eax_check_tag(br_eax_context *ctx, const void *tag);801802/**803* \brief Compute EAX authentication tag (with truncation).804*805* This function is similar to `br_eax_get_tag()`, except that it allows806* the tag to be truncated to a smaller length. The intended tag length807* is provided as `len` (in bytes); it MUST be no more than 16, but808* it may be smaller. Note that decreasing tag length mechanically makes809* forgeries easier; NIST SP 800-38D specifies that the tag length shall810* lie between 12 and 16 bytes (inclusive), but may be truncated down to811* 4 or 8 bytes, for specific applications that can tolerate it. It must812* also be noted that successful forgeries leak information on the813* authentication key, making subsequent forgeries easier. Therefore,814* tag truncation, and in particular truncation to sizes lower than 12815* bytes, shall be envisioned only with great care.816*817* The tag is written in the provided `tag` buffer. This call terminates818* the EAX run: no data may be processed with that EAX context819* afterwards, until `br_eax_reset()` is called to initiate a new EAX820* run.821*822* The tag value must normally be sent along with the encrypted data.823* When decrypting, the tag value must be recomputed and compared with824* the received tag: if the two tag values differ, then either the tag825* or the encrypted data was altered in transit. As an alternative to826* this function, the `br_eax_check_tag_trunc()` function can be used to827* compute and check the tag value.828*829* \param ctx EAX context structure.830* \param tag destination buffer for the tag.831* \param len tag length (16 bytes or less).832*/833void br_eax_get_tag_trunc(br_eax_context *ctx, void *tag, size_t len);834835/**836* \brief Compute and check EAX authentication tag (with truncation).837*838* This function is an alternative to `br_eax_get_tag_trunc()`, normally used839* on the receiving end (i.e. when decrypting value). The tag value is840* recomputed and compared with the provided tag value. If they match, 1841* is returned; on mismatch, 0 is returned. A returned value of 0 means842* that the data or the tag was altered in transit, normally leading to843* wholesale rejection of the complete message.844*845* Tag length MUST be 16 bytes or less. The normal EAX tag length is 16846* bytes. See `br_check_tag_trunc()` for some discussion on the potential847* perils of truncating authentication tags.848*849* \param ctx EAX context structure.850* \param tag tag value to compare with.851* \param len tag length (in bytes).852* \return 1 on success (exact match of tag value), 0 otherwise.853*/854uint32_t br_eax_check_tag_trunc(br_eax_context *ctx,855const void *tag, size_t len);856857/**858* \brief Class instance for EAX.859*/860extern const br_aead_class br_eax_vtable;861862/**863* \brief Context structure for CCM.864*865* CCM is an AEAD mode that combines a block cipher in CTR mode with866* CBC-MAC using the same block cipher and the same key, to provide867* authenticated encryption:868*869* - Any block cipher with 16-byte blocks can be used with CCM870* (technically, other block sizes are defined as well, but this871* is not implemented by these functions; shorter blocks also872* imply numerous security issues).873*874* - The authentication tag length, and plaintext length, MUST be875* known when starting processing data. Plaintext and ciphertext876* can still be provided by chunks, but the total size must match877* the value provided upon initialisation.878*879* - The nonce length is constrained between 7 and 13 bytes (inclusive).880* Furthermore, the plaintext length, when encoded, must fit over881* 15-nonceLen bytes; thus, if the nonce has length 13 bytes, then882* the plaintext length cannot exceed 65535 bytes.883*884* - Additional authenticated data length is practically unlimited885* (formal limit is at 2^64 bytes).886*887* - The authentication tag has length 4 to 16 bytes (even values only).888*889* The CCM initialisation function receives as parameter an890* _initialised_ block cipher implementation context, with the secret891* key already set. A pointer to that context will be kept within the892* CCM context structure. It is up to the caller to allocate and893* initialise that block cipher context.894*/895typedef struct {896#ifndef BR_DOXYGEN_IGNORE897const br_block_ctrcbc_class **bctx;898unsigned char ctr[16];899unsigned char cbcmac[16];900unsigned char tagmask[16];901unsigned char buf[16];902size_t ptr;903size_t tag_len;904#endif905} br_ccm_context;906907/**908* \brief Initialize a CCM context.909*910* A block cipher implementation, with its initialised context911* structure, is provided. The block cipher MUST use 16-byte blocks in912* CTR + CBC-MAC mode, and its secret key MUST have been already set in913* the provided context. The parameters are linked in the CCM context.914*915* After this function has been called, the `br_ccm_reset()` function must916* be called, to provide the nonce for CCM computation.917*918* \param ctx CCM context structure.919* \param bctx block cipher context (already initialised with secret key).920*/921void br_ccm_init(br_ccm_context *ctx, const br_block_ctrcbc_class **bctx);922923/**924* \brief Reset a CCM context.925*926* This function resets an already initialised CCM context for a new927* computation run. Implementations and keys are conserved. This function928* can be called at any time; it cancels any ongoing CCM computation that929* uses the provided context structure.930*931* The `aad_len` parameter contains the total length, in bytes, of the932* additional authenticated data. It may be zero. That length MUST be933* exact.934*935* The `data_len` parameter contains the total length, in bytes, of the936* data that will be injected (plaintext or ciphertext). That length MUST937* be exact. Moreover, that length MUST be less than 2^(8*(15-nonce_len)).938*939* The nonce length (`nonce_len`), in bytes, must be in the 7..13 range940* (inclusive).941*942* The tag length (`tag_len`), in bytes, must be in the 4..16 range, and943* be an even integer. Short tags mechanically allow for higher forgery944* probabilities; hence, tag sizes smaller than 12 bytes shall be used only945* with care.946*947* It is critical to CCM security that nonce values are not repeated for948* the same encryption key. Random generation of nonces is not generally949* recommended, due to the relatively small maximum nonce value.950*951* Returned value is 1 on success, 0 on error. An error is reported if952* the tag or nonce length is out of range, or if the953* plaintext/ciphertext length cannot be encoded with the specified954* nonce length.955*956* \param ctx CCM context structure.957* \param nonce CCM nonce to use.958* \param nonce_len CCM nonce length (in bytes, 7 to 13).959* \param aad_len additional authenticated data length (in bytes).960* \param data_len plaintext/ciphertext length (in bytes).961* \param tag_len tag length (in bytes).962* \return 1 on success, 0 on error.963*/964int br_ccm_reset(br_ccm_context *ctx, const void *nonce, size_t nonce_len,965uint64_t aad_len, uint64_t data_len, size_t tag_len);966967/**968* \brief Inject additional authenticated data into CCM.969*970* The provided data is injected into a running CCM computation. Additional971* data must be injected _before_ the call to `br_ccm_flip()`.972* Additional data can be injected in several chunks of arbitrary length,973* but the total amount MUST exactly match the value which was provided974* to `br_ccm_reset()`.975*976* \param ctx CCM context structure.977* \param data pointer to additional authenticated data.978* \param len length of additional authenticated data (in bytes).979*/980void br_ccm_aad_inject(br_ccm_context *ctx, const void *data, size_t len);981982/**983* \brief Finish injection of additional authenticated data into CCM.984*985* This function MUST be called before beginning the actual encryption986* or decryption (with `br_ccm_run()`), even if no additional authenticated987* data was injected. No additional authenticated data may be injected988* after this function call.989*990* \param ctx CCM context structure.991*/992void br_ccm_flip(br_ccm_context *ctx);993994/**995* \brief Encrypt or decrypt some data with CCM.996*997* Data encryption or decryption can be done after `br_ccm_flip()`998* has been called on the context. If `encrypt` is non-zero, then the999* provided data shall be plaintext, and it is encrypted in place.1000* Otherwise, the data shall be ciphertext, and it is decrypted in place.1001*1002* Data may be provided in several chunks of arbitrary length, provided1003* that the total length exactly matches the length provided to the1004* `br_ccm_reset()` call.1005*1006* \param ctx CCM context structure.1007* \param encrypt non-zero for encryption, zero for decryption.1008* \param data data to encrypt or decrypt.1009* \param len data length (in bytes).1010*/1011void br_ccm_run(br_ccm_context *ctx, int encrypt, void *data, size_t len);10121013/**1014* \brief Compute CCM authentication tag.1015*1016* Compute the CCM authentication tag. This call terminates the CCM1017* run: all data must have been injected with `br_ccm_run()` (in zero,1018* one or more successive calls). After this function has been called,1019* no more data can br processed; a `br_ccm_reset()` call is required1020* to start a new message.1021*1022* The tag length was provided upon context initialisation (last call1023* to `br_ccm_reset()`); it is returned by this function.1024*1025* The tag value must normally be sent along with the encrypted data.1026* When decrypting, the tag value must be recomputed and compared with1027* the received tag: if the two tag values differ, then either the tag1028* or the encrypted data was altered in transit. As an alternative to1029* this function, the `br_ccm_check_tag()` function can be used to1030* compute and check the tag value.1031*1032* \param ctx CCM context structure.1033* \param tag destination buffer for the tag (up to 16 bytes).1034* \return the tag length (in bytes).1035*/1036size_t br_ccm_get_tag(br_ccm_context *ctx, void *tag);10371038/**1039* \brief Compute and check CCM authentication tag.1040*1041* This function is an alternative to `br_ccm_get_tag()`, normally used1042* on the receiving end (i.e. when decrypting value). The tag value is1043* recomputed and compared with the provided tag value. If they match, 11044* is returned; on mismatch, 0 is returned. A returned value of 0 means1045* that the data or the tag was altered in transit, normally leading to1046* wholesale rejection of the complete message.1047*1048* \param ctx CCM context structure.1049* \param tag tag value to compare with (up to 16 bytes).1050* \return 1 on success (exact match of tag value), 0 otherwise.1051*/1052uint32_t br_ccm_check_tag(br_ccm_context *ctx, const void *tag);10531054#ifdef __cplusplus1055}1056#endif10571058#endif105910601061