[sr-dev] git:master:4f514a79: tls: option to set use fastrand for rand_engine

Daniel-Constantin Mierla miconda at gmail.com
Thu Oct 3 11:37:21 CEST 2019


Module: kamailio
Branch: master
Commit: 4f514a7956534f1afc1ef30d8332f5dc3547de4b
URL: https://github.com/kamailio/kamailio/commit/4f514a7956534f1afc1ef30d8332f5dc3547de4b

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date: 2019-10-03T11:36:28+02:00

tls: option to set use fastrand for rand_engine

---

Modified: src/modules/tls/tls_mod.c
Modified: src/modules/tls/tls_rand.c
Modified: src/modules/tls/tls_rand.h

---

Diff:  https://github.com/kamailio/kamailio/commit/4f514a7956534f1afc1ef30d8332f5dc3547de4b.diff
Patch: https://github.com/kamailio/kamailio/commit/4f514a7956534f1afc1ef30d8332f5dc3547de4b.patch

---

diff --git a/src/modules/tls/tls_mod.c b/src/modules/tls/tls_mod.c
index 52a45353d6..87759249b2 100644
--- a/src/modules/tls/tls_mod.c
+++ b/src/modules/tls/tls_mod.c
@@ -449,7 +449,10 @@ int ksr_rand_engine_param(modparam_t type, void* val)
 	LM_DBG("random engine: %.*s\n", reng->len, reng->s);
 	if(reng->len == 5 && strncasecmp(reng->s, "krand", 5) == 0) {
 		LM_DBG("setting krand random engine\n");
-		RAND_set_rand_method(RAND_ksr_method());
+		RAND_set_rand_method(RAND_ksr_krand_method());
+	} else if(reng->len == 8 && strncasecmp(reng->s, "fastrand", 8) == 0) {
+		LM_DBG("setting fastrand random engine\n");
+		RAND_set_rand_method(RAND_ksr_fastrand_method());
 	}
 #endif
 	return 0;
diff --git a/src/modules/tls/tls_rand.c b/src/modules/tls/tls_rand.c
index 7be0d335f7..3f78cb93e9 100644
--- a/src/modules/tls/tls_rand.c
+++ b/src/modules/tls/tls_rand.c
@@ -27,8 +27,9 @@
 
 #include "../../core/dprint.h"
 #include "../../core/rand/kam_rand.h"
+#include "../../core/rand/fastrand.h"
 
-static int ksr_rand_bytes(unsigned char *outdata, int size)
+static int ksr_krand_bytes(unsigned char *outdata, int size)
 {
 	int r;
 
@@ -51,28 +52,75 @@ static int ksr_rand_bytes(unsigned char *outdata, int size)
 	return 1;
 }
 
-static int ksr_rand_pseudorand(unsigned char *outdata, int size)
+static int ksr_krand_pseudorand(unsigned char *outdata, int size)
 {
-    return ksr_rand_bytes(outdata, size);
+    return ksr_krand_bytes(outdata, size);
 }
 
-static int ksr_rand_status(void)
+static int ksr_krand_status(void)
 {
     return 1;
 }
 
-const RAND_METHOD _ksr_rand_method = {
+const RAND_METHOD _ksr_krand_method = {
     NULL,
-    ksr_rand_bytes,
+    ksr_krand_bytes,
     NULL,
     NULL,
-    ksr_rand_pseudorand,
-    ksr_rand_status
+    ksr_krand_pseudorand,
+    ksr_krand_status
 };
 
-const RAND_METHOD *RAND_ksr_method(void)
+const RAND_METHOD *RAND_ksr_krand_method(void)
 {
-    return &_ksr_rand_method;
+    return &_ksr_krand_method;
+}
+
+static int ksr_fastrand_bytes(unsigned char *outdata, int size)
+{
+	int r;
+
+	if (size < 0) {
+		return 0;
+	} else if (size == 0) {
+		return 1;
+	}
+
+	while(size >= sizeof(int)) {
+		r = kam_rand();
+		memcpy(outdata, &r, sizeof(int));
+		size -= sizeof(int);
+		outdata += sizeof(int);
+	}
+	if(size>0) {
+		r = kam_rand();
+		memcpy(outdata, &r, size);
+	}
+	return 1;
+}
+
+static int ksr_fastrand_pseudorand(unsigned char *outdata, int size)
+{
+    return ksr_fastrand_bytes(outdata, size);
+}
+
+static int ksr_fastrand_status(void)
+{
+    return 1;
+}
+
+const RAND_METHOD _ksr_fastrand_method = {
+    NULL,
+    ksr_fastrand_bytes,
+    NULL,
+    NULL,
+    ksr_fastrand_pseudorand,
+    ksr_fastrand_status
+};
+
+const RAND_METHOD *RAND_ksr_fastrand_method(void)
+{
+    return &_ksr_fastrand_method;
 }
 
 #endif /* OPENSSL_VERSION_NUMBER >= 0x10100000L */
diff --git a/src/modules/tls/tls_rand.h b/src/modules/tls/tls_rand.h
index 20b5a20e9c..d1a3f0d37f 100644
--- a/src/modules/tls/tls_rand.h
+++ b/src/modules/tls/tls_rand.h
@@ -25,7 +25,8 @@
 
 #include <openssl/rand.h>
 
-const RAND_METHOD *RAND_ksr_method(void);
+const RAND_METHOD *RAND_ksr_krand_method(void);
+const RAND_METHOD *RAND_ksr_fastrand_method(void);
 
 #endif /* OPENSSL_VERSION_NUMBER >= 0x10100000L */
 #endif




More information about the sr-dev mailing list