| 1 |
|
| MODULE = CryptX PACKAGE = Crypt::PRNG |
| 2 |
|
| |
| 3 |
|
| PROTOTYPES: DISABLE |
| 4 |
|
| |
| 5 |
|
| #define CRYPTX_PRNG_MAX_OUTPUT_LEN 1000000000UL /* below ULONG_MAX/4, prevents base64 sizing overflow on 32bit */ |
| 6 |
|
| |
| 7 |
|
| Crypt::PRNG |
| 8 |
|
| new(char * class, ...) |
| 9 |
|
| CODE: |
| 10 |
|
| { |
| 11 |
57 |
| IV curpid = (IV)PerlProc_getpid(); |
| 12 |
|
| int rv, id, idx; |
| 13 |
57 |
| unsigned char *ent=NULL; |
| 14 |
57 |
| STRLEN ent_len=0; |
| 15 |
|
| unsigned char entropy_buf[40]; |
| 16 |
57 |
| char *prng_name = (char *)"ChaCha20"; |
| 17 |
57 |
| STRLEN prng_name_len = 0; |
| 18 |
57 |
| SV *entropy = &PL_sv_undef; |
| 19 |
57 |
| SV *prng_name_sv = NULL; |
| 20 |
|
| |
| 21 |
|
| /* we need to handle: |
| 22 |
|
| Crypt::PRNG->new('RC4'); |
| 23 |
|
| Crypt::Cipher::RC4->new(); |
| 24 |
|
| */ |
| 25 |
57 |
| idx = strcmp("Crypt::PRNG", class) == 0 ? 1 : 0; |
| 26 |
57 |
▶ | if (idx == 1 && idx + 1 <= items) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (idx == 1 && idx + 1 <= items) { |
57 |
0 |
57 |
| if (idx == 1 && idx + 1 <= items) { |
51 |
6 |
57 |
|
| 27 |
51 |
| prng_name_sv = ST(idx); |
| 28 |
51 |
▶ | if (!SvPOK_spec(prng_name_sv)) croak("FATAL: invalid PRNG name"); |
Branch: 5 branches
| Branch | True | False | Total |
| if (!SvPOK_spec(prng_name_sv)) croak("FATAL: invalid PRNG name"); |
50 |
1 |
51 |
| if (!SvPOK_spec(prng_name_sv)) croak("FATAL: invalid PRNG name"); |
2 |
48 |
50 |
| if (!SvPOK_spec(prng_name_sv)) croak("FATAL: invalid PRNG name"); |
2 |
0 |
2 |
| if (!SvPOK_spec(prng_name_sv)) croak("FATAL: invalid PRNG name"); |
1 |
1 |
2 |
| if (!SvPOK_spec(prng_name_sv)) croak("FATAL: invalid PRNG name"); |
0 |
1 |
1 |
|
| 29 |
49 |
| prng_name = SvPVbyte(prng_name_sv, prng_name_len); |
| 30 |
|
| } |
| 31 |
6 |
▶ | else if (idx == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (idx == 0) { |
0 |
6 |
6 |
|
| 32 |
0 |
| prng_name = class; /* cryptx_internal_find_prng strips the prefix */ |
| 33 |
|
| } |
| 34 |
55 |
▶ | if (idx + 2 <= items) entropy = ST(idx + 1); |
Branch: 1 branch
| Branch | True | False | Total |
| if (idx + 2 <= items) entropy = ST(idx + 1); |
32 |
23 |
55 |
|
| 35 |
|
| |
| 36 |
55 |
| Newz(0, RETVAL, 1, struct prng_struct); |
| 37 |
55 |
▶ | if (!RETVAL) croak("FATAL: Newz failed"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!RETVAL) croak("FATAL: Newz failed"); |
0 |
55 |
55 |
|
| 38 |
|
| |
| 39 |
55 |
| id = cryptx_internal_find_prng(prng_name); |
| 40 |
55 |
▶ | if (id == -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (id == -1) { |
1 |
54 |
55 |
|
| 41 |
1 |
| Safefree(RETVAL); |
| 42 |
1 |
| croak("FATAL: find_prng failed for '%s'", prng_name); |
| 43 |
|
| } |
| 44 |
54 |
| RETVAL->last_pid = curpid; |
| 45 |
54 |
| RETVAL->desc = &prng_descriptor[id]; |
| 46 |
|
| |
| 47 |
54 |
| rv = RETVAL->desc->start(&RETVAL->state); |
| 48 |
54 |
▶ | if (rv != CRYPT_OK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rv != CRYPT_OK) { |
0 |
54 |
54 |
|
| 49 |
0 |
| Safefree(RETVAL); |
| 50 |
0 |
| croak("FATAL: PRNG_start failed: %s", error_to_string(rv)); |
| 51 |
|
| } |
| 52 |
|
| |
| 53 |
54 |
▶ | if (SvOK(entropy)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(entropy)) { |
32 |
22 |
54 |
|
| 54 |
32 |
| ent = (unsigned char *) SvPVbyte(entropy, ent_len); |
| 55 |
32 |
| rv = RETVAL->desc->add_entropy(ent, (unsigned long)ent_len, &RETVAL->state); |
| 56 |
32 |
▶ | if (rv != CRYPT_OK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rv != CRYPT_OK) { |
5 |
27 |
32 |
|
| 57 |
5 |
| cryptx_internal_prng_done(RETVAL); |
| 58 |
5 |
| Safefree(RETVAL); |
| 59 |
5 |
| croak("FATAL: PRNG_add_entropy failed: %s", error_to_string(rv)); |
| 60 |
|
| } |
| 61 |
|
| } |
| 62 |
|
| else { |
| 63 |
22 |
▶ | if (rng_get_bytes(entropy_buf, 40, NULL) != 40) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rng_get_bytes(entropy_buf, 40, NULL) != 40) { |
0 |
22 |
22 |
|
| 64 |
0 |
| cryptx_internal_prng_done(RETVAL); |
| 65 |
0 |
| Safefree(RETVAL); |
| 66 |
0 |
| croak("FATAL: rng_get_bytes failed"); |
| 67 |
|
| } |
| 68 |
22 |
| rv = RETVAL->desc->add_entropy(entropy_buf, 40, &RETVAL->state); |
| 69 |
22 |
| zeromem(entropy_buf, 40); |
| 70 |
22 |
▶ | if (rv != CRYPT_OK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rv != CRYPT_OK) { |
0 |
22 |
22 |
|
| 71 |
0 |
| cryptx_internal_prng_done(RETVAL); |
| 72 |
0 |
| Safefree(RETVAL); |
| 73 |
0 |
| croak("FATAL: PRNG_add_entropy failed: %s", error_to_string(rv)); |
| 74 |
|
| } |
| 75 |
|
| } |
| 76 |
49 |
| rv = RETVAL->desc->ready(&RETVAL->state); |
| 77 |
49 |
▶ | if (rv != CRYPT_OK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rv != CRYPT_OK) { |
4 |
45 |
49 |
|
| 78 |
4 |
| cryptx_internal_prng_done(RETVAL); |
| 79 |
4 |
| Safefree(RETVAL); |
| 80 |
4 |
| croak("FATAL: PRNG_ready failed: %s", error_to_string(rv)); |
| 81 |
|
| } |
| 82 |
|
| } |
| 83 |
|
| OUTPUT: |
| 84 |
|
| RETVAL |
| 85 |
|
| |
| 86 |
|
| void |
| 87 |
|
| DESTROY(Crypt::PRNG self) |
| 88 |
|
| CODE: |
| 89 |
|
| { |
| 90 |
45 |
▶ | if (self != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self != NULL) { |
45 |
0 |
45 |
|
| 91 |
45 |
| cryptx_internal_prng_done(self); |
| 92 |
45 |
| Safefree(self); |
| 93 |
|
| } |
| 94 |
|
| } |
| 95 |
|
| |
| 96 |
|
| void |
| 97 |
|
| add_entropy(Crypt::PRNG self, SV * entropy=&PL_sv_undef) |
| 98 |
|
| CODE: |
| 99 |
|
| { |
| 100 |
2 |
| STRLEN in_len=0; |
| 101 |
2 |
| unsigned char *in_buffer=NULL; |
| 102 |
|
| unsigned char entropy_buf[40]; |
| 103 |
|
| int rv; |
| 104 |
2 |
▶ | if (SvOK(entropy)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(entropy)) { |
1 |
1 |
2 |
|
| 105 |
1 |
| in_buffer = (unsigned char *) SvPVbyte(entropy, in_len); |
| 106 |
1 |
| rv = self->desc->add_entropy(in_buffer, (unsigned long)in_len, &self->state); |
| 107 |
1 |
▶ | if (rv != CRYPT_OK) croak("FATAL: PRNG_add_entropy failed: %s", error_to_string(rv)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (rv != CRYPT_OK) croak("FATAL: PRNG_add_entropy failed: %s", error_to_string(rv)); |
0 |
1 |
1 |
|
| 108 |
|
| } |
| 109 |
|
| else { |
| 110 |
1 |
▶ | if (rng_get_bytes(entropy_buf, 40, NULL) != 40) croak("FATAL: rng_get_bytes failed"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (rng_get_bytes(entropy_buf, 40, NULL) != 40) croak("FATAL: rng_get_bytes failed"); |
0 |
1 |
1 |
|
| 111 |
1 |
| rv = self->desc->add_entropy(entropy_buf, 40, &self->state); |
| 112 |
1 |
| zeromem(entropy_buf, 40); |
| 113 |
1 |
▶ | if (rv != CRYPT_OK) croak("FATAL: PRNG_add_entropy failed: %s", error_to_string(rv)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (rv != CRYPT_OK) croak("FATAL: PRNG_add_entropy failed: %s", error_to_string(rv)); |
0 |
1 |
1 |
|
| 114 |
|
| } |
| 115 |
2 |
| rv = self->desc->ready(&self->state); |
| 116 |
2 |
▶ | if (rv != CRYPT_OK) croak("FATAL: PRNG_ready failed: %s", error_to_string(rv)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (rv != CRYPT_OK) croak("FATAL: PRNG_ready failed: %s", error_to_string(rv)); |
0 |
2 |
2 |
|
| 117 |
|
| } |
| 118 |
|
| |
| 119 |
|
| SV * |
| 120 |
|
| bytes(Crypt::PRNG self, unsigned long output_len) |
| 121 |
|
| ALIAS: |
| 122 |
|
| bytes_hex = 1 |
| 123 |
|
| bytes_b64 = 2 |
| 124 |
|
| bytes_b64u = 3 |
| 125 |
|
| CODE: |
| 126 |
|
| { |
| 127 |
|
| int rv_len, rv; |
| 128 |
|
| unsigned long len, chunks; |
| 129 |
168 |
| unsigned char *tmp = NULL; |
| 130 |
|
| char *rdata; |
| 131 |
|
| |
| 132 |
168 |
▶ | if (output_len > CRYPTX_PRNG_MAX_OUTPUT_LEN) croak("FATAL: output_len too large"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (output_len > CRYPTX_PRNG_MAX_OUTPUT_LEN) croak("FATAL: output_len too large"); |
16 |
152 |
168 |
|
| 133 |
|
| |
| 134 |
152 |
▶ | if (output_len == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (output_len == 0) { |
8 |
144 |
152 |
|
| 135 |
8 |
| RETVAL = newSVpvn("", 0); |
| 136 |
|
| } |
| 137 |
|
| else { |
| 138 |
144 |
| cryptx_internal_prng_reseed(self); |
| 139 |
144 |
▶ | if (ix == 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ix == 1) { |
28 |
116 |
144 |
|
| 140 |
|
| /* HEX */ |
| 141 |
28 |
| Newz(0, tmp, output_len, unsigned char); |
| 142 |
28 |
▶ | if (tmp == NULL) croak("FATAL: Newz failed"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (tmp == NULL) croak("FATAL: Newz failed"); |
0 |
28 |
28 |
|
| 143 |
28 |
| rv_len = (self->desc->read)(tmp, (unsigned long)output_len, &self->state); |
| 144 |
28 |
▶ | if ((UV)rv_len != output_len) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((UV)rv_len != output_len) { |
0 |
28 |
28 |
|
| 145 |
0 |
| Safefree(tmp); |
| 146 |
0 |
| croak("FATAL: PRNG_read failed"); |
| 147 |
|
| } |
| 148 |
28 |
| RETVAL = NEWSV(0, output_len * 2 + 1); /* avoid zero! */ |
| 149 |
28 |
| SvPOK_only(RETVAL); |
| 150 |
28 |
| SvCUR_set(RETVAL, output_len * 2 + 1); |
| 151 |
28 |
| rdata = SvPVX(RETVAL); |
| 152 |
28 |
| len = output_len * 2 + 1; |
| 153 |
28 |
| rv = base16_encode(tmp, output_len, rdata, &len, 0); |
| 154 |
28 |
| SvCUR_set(RETVAL, len); |
| 155 |
28 |
| Safefree(tmp); |
| 156 |
28 |
▶ | if (rv != CRYPT_OK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rv != CRYPT_OK) { |
0 |
28 |
28 |
|
| 157 |
0 |
| SvREFCNT_dec(RETVAL); |
| 158 |
0 |
| croak("FATAL: base16_encode failed"); |
| 159 |
|
| } |
| 160 |
|
| } |
| 161 |
116 |
▶ | else if (ix == 2 || ix == 3) { |
Branch: 2 branches
| Branch | True | False | Total |
| else if (ix == 2 || ix == 3) { |
98 |
18 |
116 |
| else if (ix == 2 || ix == 3) { |
18 |
80 |
98 |
|
| 162 |
|
| /* BASE64 or BASE64URL */ |
| 163 |
36 |
| chunks = (output_len + 2) / 3; |
| 164 |
36 |
| Newz(0, tmp, output_len, unsigned char); |
| 165 |
36 |
▶ | if (tmp == NULL) croak("FATAL: Newz failed"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (tmp == NULL) croak("FATAL: Newz failed"); |
0 |
36 |
36 |
|
| 166 |
36 |
| rv_len = (self->desc->read)(tmp, (unsigned long)output_len, &self->state); |
| 167 |
36 |
▶ | if ((UV)rv_len != output_len) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((UV)rv_len != output_len) { |
0 |
36 |
36 |
|
| 168 |
0 |
| Safefree(tmp); |
| 169 |
0 |
| croak("FATAL: PRNG_read failed"); |
| 170 |
|
| } |
| 171 |
36 |
| len = chunks * 4 + 1; |
| 172 |
36 |
| RETVAL = NEWSV(0, len); /* avoid zero! */ |
| 173 |
36 |
| SvPOK_only(RETVAL); |
| 174 |
36 |
| SvCUR_set(RETVAL, len); |
| 175 |
36 |
| rdata = SvPVX(RETVAL); |
| 176 |
36 |
▶ | rv = ix == 3 ? base64url_encode(tmp, output_len, rdata, &len) : |
Branch: 1 branch
| Branch | True | False | Total |
| rv = ix == 3 ? base64url_encode(tmp, output_len, rdata, &len) : |
18 |
18 |
36 |
|
| 177 |
18 |
| base64_encode(tmp, output_len, rdata, &len); |
| 178 |
36 |
| SvCUR_set(RETVAL, len); |
| 179 |
36 |
| Safefree(tmp); |
| 180 |
36 |
▶ | if (rv != CRYPT_OK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rv != CRYPT_OK) { |
0 |
36 |
36 |
|
| 181 |
0 |
| SvREFCNT_dec(RETVAL); |
| 182 |
0 |
▶ | croak(ix == 3 ? "FATAL: base64url_encode failed" : "FATAL: base64_encode failed"); |
Branch: 1 branch
| Branch | True | False | Total |
| croak(ix == 3 ? "FATAL: base64url_encode failed" : "FATAL: base64_encode failed"); |
0 |
0 |
0 |
|
| 183 |
|
| } |
| 184 |
|
| } |
| 185 |
|
| else { |
| 186 |
|
| /* RAW BYTES */ |
| 187 |
80 |
| RETVAL = NEWSV(0, output_len); /* avoid zero! */ |
| 188 |
80 |
| SvPOK_only(RETVAL); |
| 189 |
80 |
| SvCUR_set(RETVAL, output_len); |
| 190 |
80 |
| *SvEND(RETVAL) = '\0'; |
| 191 |
80 |
| rdata = SvPVX(RETVAL); |
| 192 |
80 |
| rv_len = (self->desc->read)((unsigned char*)rdata, (unsigned long)output_len, &self->state); |
| 193 |
80 |
▶ | if ((UV)rv_len != output_len) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((UV)rv_len != output_len) { |
0 |
80 |
80 |
|
| 194 |
0 |
| SvREFCNT_dec(RETVAL); |
| 195 |
0 |
| croak("FATAL: PRNG_read failed"); |
| 196 |
|
| } |
| 197 |
|
| } |
| 198 |
|
| } |
| 199 |
|
| } |
| 200 |
|
| OUTPUT: |
| 201 |
|
| RETVAL |
| 202 |
|
| |
| 203 |
|
| UV |
| 204 |
|
| int32(Crypt::PRNG self) |
| 205 |
|
| CODE: |
| 206 |
|
| { |
| 207 |
|
| int i; |
| 208 |
|
| unsigned char rdata[4]; |
| 209 |
|
| |
| 210 |
12000 |
| cryptx_internal_prng_reseed(self); |
| 211 |
|
| |
| 212 |
12000 |
| i = (self->desc->read)(rdata, 4, &self->state); |
| 213 |
12000 |
▶ | if (i != 4) croak("FATAL: PRNG_read failed"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (i != 4) croak("FATAL: PRNG_read failed"); |
0 |
12000 |
12000 |
|
| 214 |
12000 |
▶ | RETVAL = ((UV)(rdata[0])<<24) + ((UV)(rdata[1])<<16) + ((UV)(rdata[2])<<8) + ((UV)(rdata[3])); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = ((UV)(rdata[0])<<24) + ((UV)(rdata[1])<<16) + ((UV)(rdata[2])<<8) + ((UV)(rdata[3])); |
11988 |
12 |
12000 |
|
| 215 |
|
| } |
| 216 |
|
| OUTPUT: |
| 217 |
|
| RETVAL |
| 218 |
|
| |
| 219 |
|
| NV |
| 220 |
|
| double(Crypt::PRNG self, SV * limit_sv = NULL) |
| 221 |
|
| CODE: |
| 222 |
|
| { |
| 223 |
|
| int i; |
| 224 |
|
| unsigned long a, b; /* 32bit is enough */ |
| 225 |
|
| unsigned char rdata[7]; /* for double we need 53 bits */ |
| 226 |
|
| |
| 227 |
24000 |
| cryptx_internal_prng_reseed(self); |
| 228 |
|
| |
| 229 |
24000 |
| i = (self->desc->read)(rdata, 7, &self->state); |
| 230 |
24000 |
▶ | if (i != 7) croak("FATAL: PRNG_read failed"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (i != 7) croak("FATAL: PRNG_read failed"); |
0 |
24000 |
24000 |
|
| 231 |
24000 |
| a = (((unsigned long)(rdata[0])<<16) + ((unsigned long)(rdata[1])<<8) + ((unsigned long)(rdata[2]))) & 0x1FFFFF; /* 21 bits */ |
| 232 |
24000 |
| b = ((unsigned long)(rdata[3])<<24) + ((unsigned long)(rdata[4])<<16) + ((unsigned long)(rdata[5])<<8) + ((unsigned long)(rdata[6])); /* 32 bits */ |
| 233 |
24000 |
| RETVAL = ( (NV)a * 4294967296.0 + (NV)b ) / 9007199254740992.0; /* (a * 2^32 + b) / 2^53 */ |
| 234 |
24000 |
▶ | if (limit_sv && SvOK(limit_sv)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (limit_sv && SvOK(limit_sv)) { |
18000 |
6000 |
24000 |
| if (limit_sv && SvOK(limit_sv)) { |
18000 |
0 |
18000 |
|
| 235 |
18000 |
| NV limit = SvNV(limit_sv); |
| 236 |
18000 |
▶ | if (limit > 0 || limit < 0) RETVAL = RETVAL * limit; |
Branch: 2 branches
| Branch | True | False | Total |
| if (limit > 0 || limit < 0) RETVAL = RETVAL * limit; |
12000 |
6000 |
18000 |
| if (limit > 0 || limit < 0) RETVAL = RETVAL * limit; |
12000 |
0 |
12000 |
|
| 237 |
|
| } |
| 238 |
|
| } |
| 239 |
|
| OUTPUT: |
| 240 |
|
| RETVAL |