comparison src/c/urweb.c @ 1486:7f8ddcf12b20

Change Basis.rand to use cryptographically secure generation
author Adam Chlipala <adam@chlipala.net>
date Sun, 03 Jul 2011 16:50:17 -0400
parents 4300592e6803
children dcc8abbc6dfd
comparison
equal deleted inserted replaced
1485:4300592e6803 1486:7f8ddcf12b20
12 #include <limits.h> 12 #include <limits.h>
13 #include <stdint.h> 13 #include <stdint.h>
14 #include <sys/types.h> 14 #include <sys/types.h>
15 #include <sys/socket.h> 15 #include <sys/socket.h>
16 #include <openssl/des.h> 16 #include <openssl/des.h>
17 #include <openssl/rand.h>
17 #include <time.h> 18 #include <time.h>
18 19
19 #include <pthread.h> 20 #include <pthread.h>
20 21
21 #include "types.h" 22 #include "types.h"
3775 fprintf(stderr, "%s\n", s); 3776 fprintf(stderr, "%s\n", s);
3776 return uw_unit_v; 3777 return uw_unit_v;
3777 } 3778 }
3778 3779
3779 uw_Basis_int uw_Basis_rand(uw_context ctx) { 3780 uw_Basis_int uw_Basis_rand(uw_context ctx) {
3780 uw_Basis_int n = abs(rand()); 3781 uw_Basis_int ret;
3781 return n; 3782 int r = RAND_bytes((unsigned char *)&ret, sizeof ret);
3783
3784 if (r)
3785 return abs(ret);
3786 else
3787 uw_error(ctx, FATAL, "Random number generation failed");
3782 } 3788 }
3783 3789
3784 void uw_noPostBody(uw_context ctx) { 3790 void uw_noPostBody(uw_context ctx) {
3785 ctx->hasPostBody = 0; 3791 ctx->hasPostBody = 0;
3786 } 3792 }