/** * Copyright (C) 2021 Mikael Magnusson * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License v2 as published by * the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * */ #include "prng.h" #include #include #include #include #include /* PRNG using SHA1 from mbedtls. */ struct prng_context_s { mbedtls_md_context_t md; }; prng_context_t *prng_alloc() { prng_context_t *ctx = calloc(1, sizeof(prng_context_t)); mbedtls_md_init(&ctx->md); return ctx; } void prng_setup(prng_context_t *ctx) { mbedtls_md_type_t md_type = MBEDTLS_MD_SHA1; mbedtls_md_setup(&ctx->md, mbedtls_md_info_from_type(md_type), 0); } void prng_starts(prng_context_t *ctx) { mbedtls_md_starts(&ctx->md); } void prng_update(prng_context_t *ctx, const uint8_t *input, size_t ilen) { mbedtls_md_update(&ctx->md, (const unsigned char *) input, ilen); } void prng_finish(prng_context_t *ctx, uint8_t *output) { uint8_t tmp[160]; mbedtls_md_finish(&ctx->md, tmp); memcpy(output, tmp, 8); } #if 0 void prng_free() { mbedtls_md_free(ctx); } #endif