Annotation of parser3/src/main/pa_random.C, revision 1.17

1.1       misha       1: /** @file
                      2:        Parser: random related functions.
                      3: 
1.17    ! moko        4:        Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com)
1.15      moko        5:        Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
1.1       misha       6: */
                      7: 
                      8: // includes
                      9: 
1.8       moko       10: #include "pa_common.h"
1.1       misha      11: #include "pa_random.h"
                     12: #include "pa_exception.h"
                     13: #include "pa_threads.h"
                     14: 
1.17    ! moko       15: volatile const char * IDENT_PA_RANDOM_C="$Id: pa_random.C,v 1.16 2024/11/04 03:53:25 moko Exp $" IDENT_PA_RANDOM_H;
1.3       moko       16: 
1.5       moko       17: #ifdef _MSC_VER
1.4       moko       18: #include <windows.h>
1.1       misha      19: 
                     20: class Random_provider {
                     21:        HCRYPTPROV fhProv;
                     22:        
                     23:        void acquire() {
                     24:                SYNCHRONIZED;
                     25: 
                     26:                if(fhProv)
                     27:                        return;
                     28: 
                     29:                if(!CryptAcquireContext(&fhProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
1.7       moko       30:                        throw Exception(0, 0, "CryptAcquireContext failed");
1.1       misha      31:        }
                     32:        void release() {
                     33:                if(fhProv)
                     34:                        CryptReleaseContext(fhProv, 0);
                     35:        }
                     36:        
                     37: public:
                     38:        Random_provider(): fhProv(0) {}
                     39:        ~Random_provider() { release(); }
                     40:        void generate(void *buffer, size_t size) {
                     41:                acquire();
                     42: 
                     43:                if(!CryptGenRandom(fhProv, size, (BYTE*)buffer))
1.7       moko       44:                        throw Exception(0, 0, "CryptGenRandom failed");
1.1       misha      45:        }
1.12      moko       46: } random_provider;
                     47: 
                     48: int gettimeofday(struct timeval * tp, void *);
1.1       misha      49: 
                     50: #else
                     51: 
                     52: /// from gen_uuid.c
                     53: static int get_random_fd(void)
                     54: {
                     55:         struct timeval  tv;
                     56:         static int      fd = -2;
                     57:         int             i;
                     58: 
                     59:         if (fd == -2) {
                     60:                 gettimeofday(&tv, 0);
                     61:                 fd = open("/dev/urandom", O_RDONLY);
                     62:                 if (fd == -1)
                     63:                         fd = open("/dev/random", O_RDONLY | O_NONBLOCK);
                     64:                 srand((getpid() << 16) ^ getuid() ^ tv.tv_sec ^ tv.tv_usec);
                     65:         }
                     66:         /* Crank the random number generator a few times */
                     67:         gettimeofday(&tv, 0);
                     68:         for (i = (tv.tv_sec ^ tv.tv_usec) & 0x1F; i > 0; i--)
                     69:                 rand();
                     70:         return fd;
                     71: }
                     72: 
                     73: 
                     74: /*
                     75:  * Generate a series of random bytes.  Use /dev/urandom if possible,
                     76:  * and if not, use srandom/random.
                     77:  */
                     78: static void get_random_bytes(void *buf, int nbytes)
                     79: {
                     80:         int i, fd = get_random_fd();
                     81:         int lose_counter = 0;
                     82:         char *cp = (char *) buf;
                     83: 
                     84:         if (fd >= 0) {
                     85:                 while (nbytes > 0) {
                     86:                         i = read(fd, cp, nbytes);
                     87:                         if (i <= 0) {
                     88:                                 if (lose_counter++ > 16)
                     89:                                         break;
                     90:                                 continue;
                     91:                         }
                     92:                         nbytes -= i;
                     93:                         cp += i;
                     94:                         lose_counter = 0;
                     95:                 }
                     96:         }
                     97: 
                     98:         /* XXX put something better here if no /dev/random! */
                     99:         for (i = 0; i < nbytes; i++)
                    100:                 *cp++ = rand() & 0xFF;
                    101:         return;
                    102: }
                    103: 
                    104: 
                    105: #endif
                    106: 
                    107: void random(void *buffer, size_t size) {
1.5       moko      108: #ifdef _MSC_VER
1.1       misha     109:        random_provider.generate(buffer, size);
                    110: #else
                    111:        get_random_bytes(buffer, size);
                    112: #endif
                    113: }
                    114: 
1.7       moko      115: /// to hell with extra bytes on 64bit platforms
                    116: struct uuid {
                    117:        unsigned int    time_low;
                    118:        unsigned short  time_mid;
                    119:        unsigned short  time_hi_and_version;
                    120:        unsigned short  clock_seq;
                    121:        unsigned char   node[6];
                    122: };
                    123: 
                    124: static uuid get_uuid() {
1.1       misha     125:        // random
                    126:        uuid uuid;
                    127:        random(&uuid, sizeof(uuid));
                    128: 
                    129:        // http://www.opengroup.org/onlinepubs/9629399/apdxa.htm#tagtcjh_35
                    130:        // ~
                    131:        // version = DCE Security version, with embedded POSIX UIDs.  
                    132:        // variant = DCE
                    133:        //
                    134:        // DCE=Distributed Computing Environment
                    135:        // http://www.opengroup.org/dce/
                    136:        //
                    137:        // they say this influences comparison&such,
                    138:        // but could not figure out how, hence structure layout specified strictly
                    139:        // anyhow, uuidgen on Win32 yield those values
                    140:        // 
                    141:        // xxxxxxxx-xxxx-4xxx-{8,9,A,B}xxx-xxxxxxxxxxxx
                    142:        uuid.clock_seq = (uuid.clock_seq & 0x3FFF) | 0x8000;
                    143:         uuid.time_hi_and_version = (uuid.time_hi_and_version & 0x0FFF) | 0x4000;
                    144: 
                    145:        return uuid;
                    146: }
                    147: 
1.11      moko      148: char *get_uuid_cstr(bool lower, bool solid) {
1.7       moko      149:        uuid uuid=get_uuid();
                    150: 
                    151:        const size_t bufsize=36+1/*zero-teminator*/+1/*for faulty snprintfs*/;
                    152:        char* cstr=new(PointerFreeGC) char[bufsize];
                    153: 
1.11      moko      154:        const char *format[] = {
                    155:                "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
                    156:                "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
                    157:                "%08X%04X%04X%02X%02X%02X%02X%02X%02X%02X%02X",
                    158:                "%08x%04x%04x%02x%02x%02x%02x%02x%02x%02x%02x"
                    159:        };
                    160: 
1.7       moko      161:        snprintf(cstr, bufsize,
1.11      moko      162:                format[(lower ? 1:0) + (solid ? 2:0)],
1.7       moko      163:                uuid.time_low, uuid.time_mid, uuid.time_hi_and_version,
                    164:                uuid.clock_seq >> 8, uuid.clock_seq & 0xFF,
                    165:                uuid.node[0], uuid.node[1], uuid.node[2],
                    166:                uuid.node[3], uuid.node[4], uuid.node[5]);
                    167:        return cstr;
                    168: }
                    169: 
                    170: char *get_uuid_boundary() {
                    171:        uuid uuid=get_uuid();
                    172: 
                    173:        const int boundary_bufsize=10+32+1/*for zero-teminator*/+1/*for faulty snprintfs*/;
                    174:        char* boundary=new(PointerFreeGC) char[boundary_bufsize];
                    175: 
                    176:        snprintf(boundary, boundary_bufsize,
                    177:                "----------%08X%04X%04X%02X%02X%02X%02X%02X%02X%02X%02X",
                    178:                uuid.time_low, uuid.time_mid, uuid.time_hi_and_version,
                    179:                uuid.clock_seq >> 8, uuid.clock_seq & 0xFF,
                    180:                uuid.node[0], uuid.node[1], uuid.node[2],
                    181:                uuid.node[3], uuid.node[4], uuid.node[5]);
                    182:        return boundary;
                    183: }
1.12      moko      184: 
                    185: // UUID version 7
                    186: // https://datatracker.ietf.org/doc/draft-ietf-uuidrev-rfc4122bis/
                    187: //
                    188: // UUID version 7 features a time-ordered value field derived from the
                    189: // widely implemented and well known Unix Epoch timestamp source, the
                    190: // number of milliseconds since midnight 1 Jan 1970 UTC, leap seconds
                    191: // excluded.  UUIDv7 generally has improved entropy characteristics over
                    192: // UUIDv1 or UUIDv6.
                    193: //
                    194: //  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
                    195: // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
                    196: // |                           unix_ts_ms                          |
                    197: // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
                    198: // |          unix_ts_ms           |  ver  |       rand_a          |
                    199: // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
                    200: // |var|                        rand_b                             |
                    201: // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
                    202: // |                            rand_b                             |
                    203: // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
                    204: //
                    205: // unix_ts_ms:
                    206: //    48 bit big-endian unsigned number of Unix epoch timestamp in
                    207: //    milliseconds.  Occupies bits 0 through 47 (octets 0-5).
                    208: // ver:
                    209: //    The 4 bit version field, set to 0b0111 (7).
                    210: //    Occupies bits 48 through 51 of octet 6.
                    211: // rand_a:
                    212: //    12 bits pseudo-random data to provide uniqueness as per
                    213: //    Section 6.8 and/or optional constructs to guarantee additional
                    214: //    monotonicity. Occupies bits 52 through 63 (octets 6-7).
                    215: // var:
                    216: //    The 2 bit variant field as defined by Section 4.1, set to 0b10.
                    217: //    Occupies bits 64 and 65 of octet 8.
                    218: // rand_b:
                    219: //    The final 62 bits of pseudo-random data to provide uniqueness as
                    220: //    per Section 6.8 and/or an optional counter to guarantee additional
                    221: //    monotonicity. Occupies bits 66 through 127 (octets 8-15).
                    222: 
                    223: char *get_uuid7_cstr(bool lower, bool solid) {
                    224:        unsigned char uuid[16];
                    225:        random(&uuid[8], 8);
                    226: 
                    227:        struct timeval tv;
                    228:        gettimeofday(&tv, 0);
                    229: 
                    230:        // 48 bit big-endian unsigned number of Unix epoch timestamp in milliseconds
                    231:        uint64_t unix_ts_ms = (uint64_t)tv.tv_sec * 1000 + (uint64_t)tv.tv_usec / 1000;
1.14      moko      232: 
                    233:        // 12 bit monotonicity counter
                    234:        static int seq = 0;
                    235:        static uint64_t ms_previous=0;
                    236: 
                    237:        if(unix_ts_ms == ms_previous){
                    238:                seq++;
                    239:                unix_ts_ms += seq >> 12;
                    240:        } else {
                    241:                seq = 0;
                    242:                ms_previous = unix_ts_ms;
                    243:        }
                    244: 
                    245:        uuid[7] = (unsigned char) (seq);
                    246:        uuid[6] = (unsigned char) (seq >> 8);
                    247: 
1.12      moko      248:        uuid[5] = (unsigned char) (unix_ts_ms); unix_ts_ms >>= 8;
                    249:        uuid[4] = (unsigned char) (unix_ts_ms); unix_ts_ms >>= 8;
                    250:        uuid[3] = (unsigned char) (unix_ts_ms); unix_ts_ms >>= 8;
                    251:        uuid[2] = (unsigned char) (unix_ts_ms); unix_ts_ms >>= 8;
                    252:        uuid[1] = (unsigned char) (unix_ts_ms); unix_ts_ms >>= 8;
                    253:        uuid[0] = (unsigned char) (unix_ts_ms);
                    254: 
                    255:        // Set magic numbers for a "version 7" UUID, see
                    256:        // https://www.ietf.org/archive/id/draft-ietf-uuidrev-rfc4122bis-00.html#name-uuid-version-7
                    257:        uuid[6] = (uuid[6] & 0x0f) | 0x70; /* 4 bit version [0111] */
                    258:        uuid[8] = (uuid[8] & 0x3f) | 0x80; /* 2 bit variant [10]   */
                    259: 
                    260:        const size_t bufsize=36+1/*zero-teminator*/+1/*for faulty snprintfs*/;
                    261:        char* cstr=new(PointerFreeGC) char[bufsize];
                    262: 
                    263:        const char *format[] = {
                    264:                "%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
                    265:                "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
                    266:                "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
                    267:                "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
                    268:        };
                    269: 
                    270:        snprintf(cstr, bufsize, format[(lower ? 1:0) + (solid ? 2:0)],
                    271:                uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], uuid[6], uuid[7],
                    272:                uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], uuid[15]
                    273:        );
                    274: 
                    275:        return cstr;
                    276: }

E-mail: