Skip to content

Latest commit

 

History

History
172 lines (132 loc) · 3.79 KB

rng.md

File metadata and controls

172 lines (132 loc) · 3.79 KB

Random Number Generators

#include <maid/rng.h>

Internal Interface

struct maid_rng_def Type that defines a RNG algorithm

External Interface

maid_rng Opaque type that contains the state of a RNG
maid_rng *maid_rng_new(struct maid_rng_def def, const u8 *entropy) Creates a RNG instance

Parameters

name description
def Algorithm definition
entropy Algorithm-dependent

Return value

case description
Success maid_rng instance
Failure NULL
maid_rng *maid_rng_del(maid_rng *g) Deletes a RNG instance

Parameters

name description
g maid_rng instance

Return value

case description
Always NULL
void maid_rng_renew(maid_rng *g, const u8 *entropy) Recreates a RNG instance

Parameters

name description
g maid_rng instance
entropy Algorithm-dependent
void maid_rng_generate(maid_rng *g, u8 *buffer, size_t size) Generates pseudorandom bytes

Parameters

name description
g maid_rng instance
buffer Memory to be written on
size Size of the operation

External Algorithms

const struct maid_rng_def maid_ctr_drbg_aes_128 CTR-DRBG with AES-128 (NIST)

Parameters

name description
entropy 32 bytes
const struct maid_rng_def maid_ctr_drbg_aes_192 CTR-DRBG with AES-192 (NIST)

Parameters

name description
entropy 40 bytes
const struct maid_rng_def maid_ctr_drbg_aes_256 CTR-DRBG with AES-256 (NIST)

Parameters

name description
entropy 48 bytes

Example Code

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

#include <maid/rng.h>

int main(void)
{
    u8 entropy[32] = {0xc2, 0xae, 0x5a, 0x05, 0x39, 0x3a, 0x57, 0xf6,
                      0x2b, 0xa3, 0xc2, 0xec, 0x80, 0x4a, 0x23, 0xda,
                      0x37, 0x81, 0xa6, 0xa0, 0x94, 0x4a, 0xe7, 0xbf,
                      0xd4, 0xe5, 0xda, 0xc9, 0x29, 0x14, 0x83, 0x65};

    maid_rng *g = maid_rng_new(maid_ctr_drbg_aes_128, entropy);

    u8 data[64] = {0};
    if (g)
        maid_rng_generate(g, data, sizeof(data));

    maid_rng_del(g);

    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