Skip to content

Latest commit

 

History

History
247 lines (192 loc) · 5.58 KB

aead.md

File metadata and controls

247 lines (192 loc) · 5.58 KB

Authenticated Encryption (with Additional Data)

#include <maid/aead.h>

Internal Interface

struct maid_aead_def Type that defines a AEAD construction

External Interface

maid_aead Opaque type that contains the state of a AEAD
maid_aead *maid_aead_new(struct maid_aead_def def, const u8 *restrict key, const u8 *restrict nonce) Creates an AEAD instance

Parameters

name description
def Algorithm definition
key Algorithm-dependent
nonce Algorithm-dependent

Return value

case description
Success maid_aead instance
Failure NULL
maid_aead *maid_aead_del(maid_aead *ae) Deletes an AEAD instance

Parameters

name description
ae maid_aead instance

Return value

case description
Always NULL
void maid_aead_renew(maid_aead *ae, const u8 *restrict key, const u8 *restrict nonce) Recreates an AEAD instance

Parameters

name description
ae maid_aead instance
key Algorithm-dependent
nonce Algorithm-dependent
void maid_aead_update(maid_aead *ae, const u8 *buffer, size_t size) Updates the AEAD state with additional data (Step 1)

Parameters

name description
ae maid_aead instance
buffer Data to be read
size Size of the operation
void maid_aead_crypt(maid_aead *ae, u8 *buffer, size_t size, bool decrypt) Encrypts/Decrypts data, and updates the AEAD state (Step 2, locks Step 1)

Parameters

name description
ae maid_aead instance
buffer Memory to be ciphered
size Size of the operation
decrypt Encrypt/Decrypt operation
void maid_aead_digest(maid_aead *ae, u8 *output) Outputs the authentication tag (Step 3, ending the AEAD instance)

Parameters

name description
ae maid_aead instance
output Block to be written on

External Algorithms

const struct maid_aead_def maid_aes_gcm_128 AES-128 on GCM mode (NIST)

Parameters

name description
key 128-bit key
nonce 96-bit nonce
const struct maid_aead_def maid_aes_gcm_192 AES-192 on GCM mode (NIST)

Parameters

name description
key 192-bit key
nonce 96-bit nonce
const struct maid_aead_def maid_aes_gcm_256 AES-256 on GCM mode (NIST)

Parameters

name description
key 256-bit key
nonce 96-bit nonce
const struct maid_aead_def maid_chacha20poly1305 Chacha20 with Poly1305 (IETF)

Parameters

name description
key 256-bit key
nonce 96-bit nonce

Example Code

#include <stdio.h>
#include <stdlib.h>

#include <maid/aead.h>

int main(void)
{
    u8 key[32] = {0};
    u8  iv[12] = {0};

    /* Encryption */

    maid_aead *ae = maid_aead_new(maid_aes_gcm_256, key, iv);

    u8   ad[32] = {0};
    u8 data[64] = {0};
    u8  tag[16] = {0};
    if (ae)
    {
        maid_aead_update(ae, ad, sizeof(ad));
        maid_aead_crypt(ae, data, sizeof(data), false);
        maid_aead_digest(ae, tag);
    }

    for (size_t i = 0; i < sizeof(data); i++)
        printf("%02x", data[i]);
    printf("\n");

    for (size_t i = 0; i < sizeof(tag); i++)
        printf("%02x", tag[i]);
    printf("\n");

    /* Decryption */

    maid_aead_renew(ae, key, iv);

    u8 tag2[16] = {0};
    if (ae)
    {
        maid_aead_update(ae, ad, sizeof(ad));
        maid_aead_crypt(ae, data, sizeof(data), true);
        maid_aead_digest(ae, tag2);
    }

    maid_aead_del(ae);

    for (size_t i = 0; i < sizeof(data); i++)
        printf("%02x", data[i]);
    printf("\n");

    for (size_t i = 0; i < sizeof(tag2); i++)
        printf("%02x", tag2[i]);
    printf("\n");

    return EXIT_SUCCESS;
}

Without installation:

cc -static -Iinclude example.c -Lbuild -lmaid

With installation:

cc example.c -lmaid