summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authordimitri staessens <[email protected]>2017-04-16 12:04:59 +0200
committerdimitri staessens <[email protected]>2017-04-16 13:40:12 +0200
commit97ef24340da7d3f55a39ba16b400d13f9bbe9e60 (patch)
treee13656c267343db47a4f79d2098222909222736d /src/lib
parentc884fb9d6b3d3ea59f5fc7c5d709bfff4fcf1849 (diff)
downloadouroboros-97ef24340da7d3f55a39ba16b400d13f9bbe9e60.tar.gz
ouroboros-97ef24340da7d3f55a39ba16b400d13f9bbe9e60.zip
ipcpd: Allow specifying the hash algorithm to use
Currently CRC32, MD5, and SHA3 (224, 256, 384 and 512 bit) are supported.
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/hash.c69
-rw-r--r--src/lib/ipcp_config.proto2
-rw-r--r--src/lib/irm.c3
3 files changed, 64 insertions, 10 deletions
diff --git a/src/lib/hash.c b/src/lib/hash.c
index f4fd75ea..e7806d94 100644
--- a/src/lib/hash.c
+++ b/src/lib/hash.c
@@ -28,16 +28,69 @@
#include <ouroboros/hash.h>
#include <string.h>
+#include <assert.h>
+#include <stdbool.h>
-void get_hash(uint8_t buf[],
- const char * name)
+uint16_t hash_len(enum hash_algo algo)
{
- /* currently we only support 256 bit SHA-3 */
- struct sha3_ctx ctx;
-
- rhash_sha3_256_init(&ctx);
+ switch (algo) {
+ case HASH_CRC32:
+ return CRC32_HASH_LEN;
+ case HASH_MD5:
+ return MD5_HASH_LEN;
+ case HASH_SHA3_224:
+ return SHA3_224_HASH_LEN;
+ case HASH_SHA3_256:
+ return SHA3_256_HASH_LEN;
+ case HASH_SHA3_384:
+ return SHA3_384_HASH_LEN;
+ case HASH_SHA3_512:
+ return SHA3_512_HASH_LEN;
+ default:
+ assert(false);
+ break;
+ }
+}
- rhash_sha3_update(&ctx, name, strlen(name));
+void str_hash(enum hash_algo algo,
+ void * buf,
+ const char * str)
+{
+ struct sha3_ctx sha3_ctx;
+ struct md5_ctx md5_ctx;
- rhash_sha3_final(&ctx, buf);
+ switch (algo) {
+ case HASH_CRC32:
+ memset(buf, 0, CRC32_HASH_LEN);
+ crc32((uint32_t *) buf, str, strlen(str));
+ break;
+ case HASH_MD5:
+ rhash_md5_init(&md5_ctx);
+ rhash_md5_update(&md5_ctx, str, strlen(str));
+ rhash_md5_final(&md5_ctx, (uint8_t *) buf);
+ break;
+ case HASH_SHA3_224:
+ rhash_sha3_224_init(&sha3_ctx);
+ rhash_sha3_update(&sha3_ctx, str, strlen(str));
+ rhash_sha3_final(&sha3_ctx, (uint8_t *) buf);
+ break;
+ case HASH_SHA3_256:
+ rhash_sha3_256_init(&sha3_ctx);
+ rhash_sha3_update(&sha3_ctx, str, strlen(str));
+ rhash_sha3_final(&sha3_ctx, (uint8_t *) buf);
+ break;
+ case HASH_SHA3_384:
+ rhash_sha3_384_init(&sha3_ctx);
+ rhash_sha3_update(&sha3_ctx, str, strlen(str));
+ rhash_sha3_final(&sha3_ctx, (uint8_t *) buf);
+ break;
+ case HASH_SHA3_512:
+ rhash_sha3_512_init(&sha3_ctx);
+ rhash_sha3_update(&sha3_ctx, str, strlen(str));
+ rhash_sha3_final(&sha3_ctx, (uint8_t *) buf);
+ break;
+ default:
+ assert(false);
+ break;
+ }
}
diff --git a/src/lib/ipcp_config.proto b/src/lib/ipcp_config.proto
index a0c14c41..d5ff75d6 100644
--- a/src/lib/ipcp_config.proto
+++ b/src/lib/ipcp_config.proto
@@ -25,7 +25,7 @@ syntax = "proto2";
message ipcp_config_msg {
required string dif_name = 1;
- required uint32 dir_hash_len = 2;
+ required uint32 dir_hash_algo = 2;
required int32 ipcp_type = 3;
// Config for normal IPCP
optional uint32 addr_size = 4;
diff --git a/src/lib/irm.c b/src/lib/irm.c
index a2fd5d0b..12d8e8f7 100644
--- a/src/lib/irm.c
+++ b/src/lib/irm.c
@@ -23,6 +23,7 @@
#include <ouroboros/config.h>
#include <ouroboros/errno.h>
+#include <ouroboros/hash.h>
#include <ouroboros/irm.h>
#include <ouroboros/utils.h>
#include <ouroboros/sockets.h>
@@ -105,7 +106,7 @@ int irm_bootstrap_ipcp(pid_t api,
msg.conf = &config;
config.dif_name = conf->dif_name;
config.ipcp_type = conf->type;
- config.dir_hash_len = (uint16_t) conf->dir_hash_len;
+ config.dir_hash_algo = (enum hash_algo) conf->dir_hash_algo;
switch (conf->type) {
case IPCP_NORMAL: