Skip to content

Latest commit

 

History

History
191 lines (148 loc) · 4.2 KB

block.md

File metadata and controls

191 lines (148 loc) · 4.2 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/block.h>

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

    maid_block *bl = maid_block_new(maid_aes_256, key, iv);

    u8 data[64] = {0};
    if (bl)
        maid_block_ctr(bl, data, sizeof(data));

    maid_block_del(bl);

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

    return EXIT_SUCCESS;
}

Without installation:

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

With installation:

cc example.c -lmaid