Skip to content

Files

Latest commit

 

History

History
200 lines (156 loc) · 4.38 KB

block.md

File metadata and controls

200 lines (156 loc) · 4.38 KB

Block ciphers

#include <maid/block.h>

Internal Interface

struct maid_block_def Type that defines a block cipher algorithm

External Interface

maid_block Opaque type that contains the state of a block cipher
maid_block *maid_block_new(struct maid_block_def def, const u8 *restrict key, const u8 *restrict iv) Creates a block cipher instance

Parameters

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

Return value

case description
Success maid_block instance
Failure NULL
maid_block *maid_block_del(maid_block *bl) Deletes a block cipher instance

Parameters

name description
bl maid_block instance

Return value

case description
Always NULL
void maid_block_renew(maid_block *bl,const u8 *restrict key, const u8 *restrict iv) Recreates a block cipher instance

Parameters

name description
bl maid_block instance
key Algorithm-dependent
iv Algorithm-dependent
void maid_block_ecb(maid_block *bl, u8 *buffer, bool decrypt) Applies ECB mode (doesn't change the iv)

Parameters

name description
bl maid_block instance
buffer Block to be ciphered
decrypt Encrypt/Decrypt operation
void maid_block_ctr(maid_block *bl, u8 *buffer, size_t size) Applies CTR mode (increases iv accordingly)

Parameters

name description
bl maid_block instance
buffer Memory to be ciphered
size Size of the operation

External Algorithms

const struct maid_block_def maid_aes_128 AES-128 block cipher (NIST)

Parameters

name description
key 128-bit key
iv 128-bit iv
const struct maid_block_def maid_aes_192 AES-192 block cipher (NIST)

Parameters

name description
key 192-bit key
iv 128-bit iv
const struct maid_block_def maid_aes_256 AES-256 block cipher (NIST)

Parameters

name description
key 256-bit key
iv 128-bit iv

Example Code

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

#include <maid/mem.h>

#include <maid/block.h>

int main(void)
{
    int ret = EXIT_FAILURE;

    u8  key[32] = {0};
    u8   iv[16] = {0};
    u8 data[64] = {0};

    maid_block *bl = maid_block_new(maid_aes_256, key, iv);
    if (bl)
    {
        maid_block_ctr(bl, data, sizeof(data));
        for (size_t i = 0; i < sizeof(data); i++)
            printf("%02x", data[i]);
        printf("\n");

        ret = EXIT_SUCCESS;
    }
    else
        fprintf(stderr, "Out of memory\n");
    maid_block_del(bl);

    maid_mem_clear(key,  sizeof(key));

    return ret;
}

Without installation:

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

With installation:

cc example.c -lmaid