Module: kamailio
Branch: 5.2
Commit: f73aa7727607c18cf6d6bcb11c51510c8d3660c9
URL:
https://github.com/kamailio/kamailio/commit/f73aa7727607c18cf6d6bcb11c51510…
Author: Daniel-Constantin Mierla <miconda(a)gmail.com>
Committer: Daniel-Constantin Mierla <miconda(a)gmail.com>
Date: 2019-10-07T11:43:55+02:00
tls: option to set use fastrand for rand_engine
(cherry picked from commit 4f514a7956534f1afc1ef30d8332f5dc3547de4b)
(cherry picked from commit 43aa779ee9e43805e6a7da2c12562fdc89c5c413)
---
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/f73aa7727607c18cf6d6bcb11c51510…
Patch:
https://github.com/kamailio/kamailio/commit/f73aa7727607c18cf6d6bcb11c51510…
---
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 0f283306dd..9101251dd7 100644
--- a/src/modules/tls/tls_rand.c
+++ b/src/modules/tls/tls_rand.c
@@ -27,9 +27,10 @@
#include "../../core/dprint.h"
#include "../../core/rand/kam_rand.h"
+#include "../../core/rand/fastrand.h"
#include "fortuna/random.h"
-static int ksr_rand_bytes(unsigned char *outdata, int size)
+static int ksr_krand_bytes(unsigned char *outdata, int size)
{
int r;
@@ -57,28 +58,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