Skip to content

Commit

Permalink
dtls_prng_posix.c: Fix random() only support
Browse files Browse the repository at this point in the history
Use of random() requires that _GNU_SOURCE is defined.
Cannot use variables named rand.

Make sure that dtls_prng.o is rebuilt whenever any of the
platform-specific/dtls_prng_*.c files are updated.

Signed-off-by: Jon Shallow <[email protected]>
  • Loading branch information
mrdeep1 authored and boaks committed Apr 24, 2023
1 parent 11c8e7d commit a5fa982
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ dirs: $(SUBDIRS)
$(MAKE) -C $$dir ; \
done

dtls_prng.o:: $(wildcard platform-specific/dtls_prng_*.c)

$(SUB_OBJECTS)::
$(MAKE) -C $(@D) $(@F)

Expand Down
14 changes: 10 additions & 4 deletions platform-specific/dtls_prng_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
*
*******************************************************************************/

#ifdef HAVE_RANDOM
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif /* _GNU_SOURCE */
#endif /* HAVE_RANDOM */

#include "tinydtls.h"
#include "dtls_prng.h"
#include "dtls_debug.h"
Expand All @@ -44,16 +50,16 @@ dtls_prng(unsigned char *buf, size_t len) {
if (len) {
size_t klen = len;
uint8_t byte_counter = RAND_BYTES;
uint32_t rand = random();
uint32_t rand_val = random();
while (1) {
*buf++ = rand & 0xFF;
*buf++ = rand_val & 0xFF;
if (!--klen) {
break;
}
if (--byte_counter) {
rand >>= 8;
rand_val >>= 8;
} else {
rand = random();
rand_val = random();
byte_counter = RAND_BYTES;
}
}
Expand Down

0 comments on commit a5fa982

Please sign in to comment.