[PATCH] libosmocore[master]: Add osmo_rand() function

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Max gerrit-no-reply at lists.osmocom.org
Tue Jan 3 10:45:56 UTC 2017


Review at  https://gerrit.osmocom.org/1526

Add osmo_rand() function

Add osmo_rand() as a tiny wrapper around corresponding GnuTLS function
and use it for osmo-auc-gen tool (with fallback to weak random numbers
used previously).

Change-Id: I0241b814ea4c4ce1458f7ad76e31d390383c2048
Related: OS#1694
---
M configure.ac
M debian/control
M include/osmocom/gsm/gsm_utils.h
M src/gsm/Makefile.am
M src/gsm/gsm_utils.c
M src/gsm/libosmogsm.map
M tests/Makefile.am
M utils/osmo-auc-gen.c
8 files changed, 36 insertions(+), 13 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/26/1526/1

diff --git a/configure.ac b/configure.ac
index ec03c26..cf4e868 100644
--- a/configure.ac
+++ b/configure.ac
@@ -35,6 +35,10 @@
 fi
 PKG_PROG_PKG_CONFIG([0.20])
 
+PKG_CHECK_MODULES(LIBGNUTLS, gnutls >= 3.3.0)
+AC_SUBST(LIBGNUTLS_CFLAGS)
+AC_SUBST(LIBGNUTLS_LIBS)
+
 dnl check os: some linker flags not available on osx
 case $host in
 *-darwin*)
diff --git a/debian/control b/debian/control
index 4cdb672..d61fdd3 100644
--- a/debian/control
+++ b/debian/control
@@ -12,6 +12,7 @@
                git,
                doxygen,
                libpcsclite-dev,
+               libgnutls28-dev,
                pkg-config,
                libtalloc-dev,
                python (>= 2.7.6)
diff --git a/include/osmocom/gsm/gsm_utils.h b/include/osmocom/gsm/gsm_utils.h
index 1ffe579..b41f992 100644
--- a/include/osmocom/gsm/gsm_utils.h
+++ b/include/osmocom/gsm/gsm_utils.h
@@ -103,6 +103,8 @@
  */
 int gsm_7bit_encode_n_ussd(uint8_t *result, size_t n, const char *data, int *octets_written);
 
+int osmo_rand(uint8_t *out, size_t len);
+
 /* the four functions below are helper functions and here for the unit test */
 int gsm_septets2octets(uint8_t *result, const uint8_t *rdata, uint8_t septet_len, uint8_t padding);
 int gsm_septet_encode(uint8_t *result, const char *data);
diff --git a/src/gsm/Makefile.am b/src/gsm/Makefile.am
index 4ec441f..92da071 100644
--- a/src/gsm/Makefile.am
+++ b/src/gsm/Makefile.am
@@ -3,7 +3,7 @@
 # before making any modifications: https://www.gnu.org/software/libtool/manual/html_node/Versioning.html
 LIBVERSION=7:0:1
 
-AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_builddir)/include $(TALLOC_CFLAGS)
+AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_builddir)/include $(TALLOC_CFLAGS) $(LIBGNUTLS_CFLAGS)
 AM_CFLAGS = -Wall ${GCC_FVISIBILITY_HIDDEN}
 
 # FIXME: this should eventually go into a milenage/Makefile.am
@@ -24,11 +24,11 @@
 			milenage/aes-internal.c milenage/aes-internal-enc.c \
 			milenage/milenage.c gan.c ipa.c gsm0341.c apn.c \
 			gsup.c gprs_gea.c gsm0503_conv.c oap.c
-libgsmint_la_LDFLAGS = -no-undefined
+libgsmint_la_LDFLAGS = $(LIBGNUTLS_LIBS) -no-undefined
 libgsmint_la_LIBADD = $(top_builddir)/src/libosmocore.la
 
 libosmogsm_la_SOURCES =
-libosmogsm_la_LDFLAGS = $(LTLDFLAGS_OSMOGSM) -version-info $(LIBVERSION) -no-undefined $(TALLOC_LIBS)
+libosmogsm_la_LDFLAGS = $(LTLDFLAGS_OSMOGSM) -version-info $(LIBVERSION) -no-undefined $(TALLOC_LIBS) $(LIBGNUTLS_LIBS)
 libosmogsm_la_LIBADD = libgsmint.la
 
 EXTRA_DIST = libosmogsm.map
diff --git a/src/gsm/gsm_utils.c b/src/gsm/gsm_utils.c
index 7365ab7..1174023 100644
--- a/src/gsm/gsm_utils.c
+++ b/src/gsm/gsm_utils.c
@@ -77,6 +77,9 @@
 #include <errno.h>
 #include <ctype.h>
 
+#include <gnutls/gnutls.h>
+#include <gnutls/crypto.h>
+
 #include "../../config.h"
 
 /* ETSI GSM 03.38 6.2.1 and 6.2.1.1 default alphabet
@@ -662,6 +665,16 @@
 	return arfcn;
 }
 
+/*! \brief Generate random bytes
+ *  \param[out] out Buffer to be filled with random data
+ *  \param[in] len Number of random bytes required
+ *  \returns Zero on success, or a negative error code on error.
+ */
+int osmo_rand(uint8_t *out, size_t len)
+{
+	return gnutls_rnd(GNUTLS_RND_NONCE, out, len);
+}
+
 void gsm_fn2gsmtime(struct gsm_time *time, uint32_t fn)
 {
 	time->fn = fn;
diff --git a/src/gsm/libosmogsm.map b/src/gsm/libosmogsm.map
index a3d224f..1c3046b 100644
--- a/src/gsm/libosmogsm.map
+++ b/src/gsm/libosmogsm.map
@@ -26,6 +26,7 @@
 
 osmo_sitype_strs;
 osmo_c4;
+osmo_rand;
 bitvec_add_range1024;
 comp128;
 dbm2rxlev;
diff --git a/tests/Makefile.am b/tests/Makefile.am
index b9eb8f2..743e42a 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,6 +1,6 @@
 AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_builddir)/include
 AM_CFLAGS = -Wall $(TALLOC_CFLAGS)
-AM_LDFLAGS = $(TALLOC_LIBS)
+AM_LDFLAGS = $(TALLOC_LIBS) $(LIBGNUTLS_LIBS)
 
 check_PROGRAMS = timer/timer_test sms/sms_test ussd/ussd_test		\
                  smscb/smscb_test bits/bitrev_test a5/a5_test		\
diff --git a/utils/osmo-auc-gen.c b/utils/osmo-auc-gen.c
index 3b3e557..99341a8 100644
--- a/utils/osmo-auc-gen.c
+++ b/utils/osmo-auc-gen.c
@@ -33,6 +33,7 @@
 
 #include <osmocom/crypt/auth.h>
 #include <osmocom/core/utils.h>
+#include <osmocom/gsm/gsm_utils.h>
 
 static void dump_triplets_dat(struct osmo_auth_vector *vec)
 {
@@ -214,17 +215,18 @@
 		}
 	}
 
-	if (!rand_is_set) {
-		int i;
-		printf("WARNING: We're using really weak random numbers!\n\n");
-		srand(time(NULL));
+	if (!rand_is_set)
+		if (osmo_rand(_rand, 16) < 0) {
+			int i;
+			printf("WARNING: We're using really weak random numbers!\n\n");
+			srand(time(NULL));
 
-		for (i = 0; i < 4; ++i) {
-			uint32_t r;
-			r = rand();
-			memcpy(&_rand[i*4], &r, 4);
+			for (i = 0; i < 4; ++i) {
+				uint32_t r;
+				r = rand();
+				memcpy(&_rand[i*4], &r, 4);
+			}
 		}
-	}
 
 	if (test_aud.type == OSMO_AUTH_TYPE_NONE ||
 	    test_aud.algo == OSMO_AUTH_ALG_NONE) {

-- 
To view, visit https://gerrit.osmocom.org/1526
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I0241b814ea4c4ce1458f7ad76e31d390383c2048
Gerrit-PatchSet: 1
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Max <msuraev at sysmocom.de>



More information about the gerrit-log mailing list