summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorMikael Magnusson <mikma@users.sourceforge.net>2021-07-22 00:02:49 +0200
committerMikael Magnusson <mikma@users.sourceforge.net>2021-09-21 22:22:04 +0200
commitd28760aea75c8da6ebe23775b1b81318729516c1 (patch)
treefb0ac6fcd7aa4dfd1cdd1fee30338315e6c25a75
parent2455a48e2bff0ae887ef3a81496db1b37a46eae0 (diff)
prng: add sha-1 based prng using mbedtls
-rw-r--r--CMakeLists.txt8
-rw-r--r--src/prng_mbed.c67
2 files changed, 74 insertions, 1 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 9bd62af..d9bbffd 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -36,7 +36,13 @@ if(${DHCPV4_SUPPORT})
set(EXT_SRC ${EXT_SRC} src/dhcpv4.c)
endif(${DHCPV4_SUPPORT})
-set(EXT_SRC ${EXT_SRC} src/prng_md5.c)
+if(${MBEDTLS})
+ add_definitions(-DWITH_MBEDTLS)
+ set(EXT_SRC ${EXT_SRC} src/prng_mbed.c)
+ set(EXT_LINK ${EXT_LINK} mbedcrypto)
+else(${MBEDTLS})
+ set(EXT_SRC ${EXT_SRC} src/prng_md5.c)
+endif(${MBEDCTLS})
add_executable(odhcpd src/odhcpd.c src/config.c src/router.c src/dhcpv6.c src/ndp.c src/dhcpv6-ia.c src/netlink.c ${EXT_SRC})
target_link_libraries(odhcpd resolv ubox uci ${libnl} ${EXT_LINK})
diff --git a/src/prng_mbed.c b/src/prng_mbed.c
new file mode 100644
index 0000000..ae5d3b8
--- /dev/null
+++ b/src/prng_mbed.c
@@ -0,0 +1,67 @@
+/**
+ * Copyright (C) 2021 Mikael Magnusson <mikma@user.sourceforge.net>
+ *
+ * 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 <stddef.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+
+#include <mbedtls/md.h>
+
+
+/* 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