include/pmcp/pmcp_rand.h

filter: Statementstmt 96.8% 31 / 32 Branchbran 83.3% 10 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 93.1% 41 / 44 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/pmcp/pmcp_page.h ↑ Summary include/pmcp/pmcp_abi.h »
1 /* pmcp_rand.h - random bytes and the ids made from them.
2 *
3 * Needs: pmcp.h.
4 * Gives: pmcp_random_bytes, pmcp_rand_id.
5 *
6 * getentropy where Makefile.PL's probe linked it, otherwise a cached
7 * /dev/urandom descriptor (Punk's punk_csrf.h does the same). The probe builds
8 * an executable, not an object: FreeBSD before 12 has no getentropy and an old
9 * compiler takes the implicit declaration as a warning, so only the link tells
10 * (the Punk 0.10 FreeBSD lesson). Croaks rather than falling back to anything
11 * weaker: a guessable session id is worse than a server that will not start.
12 */
13  
14 #ifndef PMCP_RAND_H
15 #define PMCP_RAND_H
16  
17 #include <errno.h>
18 #ifdef PMCP_HAVE_GETENTROPY
19 # ifdef PMCP_GETENTROPY_SYS_RANDOM
20 # include <sys/random.h>
21 # else
22 # include <unistd.h>
23 # endif
24 #endif
25  
26 #define PMCP_ID_BYTES 16
27 #define PMCP_ID_CHARS 22 /* base64url of 16 bytes, unpadded */
28  
29 #ifndef PMCP_HAVE_GETENTROPY
30 static int pmcp_urandom_fd = -1;
31 #endif
32  
33 1066 static void pmcp_random_bytes(pTHX_ unsigned char *out, size_t n)
34 {
35 1066 size_t off = 0;
36 #ifdef PMCP_HAVE_GETENTROPY
37 PERL_UNUSED_CONTEXT;
38 2132 ▶ while (off < n) {
Branch: 1 branch
BranchTrueFalseTotal
while (off < n) { 1066 1066 2132
39 1066 size_t want = n - off;
40 1066 ▶ if (want > 256) want = 256; /* getentropy's documented maximum */
Branch: 1 branch
BranchTrueFalseTotal
if (want > 256) want = 256; /* getentropy's documented maximum */ 0 1066 1066
41 1066 ▶ if (getentropy(out + off, want) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (getentropy(out + off, want) != 0) 0 1066 1066
42 0 croak(PMCP_DIST ": getentropy failed: %s", Strerror(errno));
43 1066 off += want;
44 }
45 #else
46 if (pmcp_urandom_fd < 0) {
47 pmcp_urandom_fd = PerlLIO_open3("/dev/urandom", O_RDONLY, 0);
48 if (pmcp_urandom_fd < 0)
49 croak(PMCP_DIST ": cannot open /dev/urandom: %s", Strerror(errno));
50 }
51 while (off < n) {
52 SSize_t got = PerlLIO_read(pmcp_urandom_fd, out + off, n - off);
53 if (got <= 0) croak(PMCP_DIST ": short read from /dev/urandom");
54 off += (size_t)got;
55 }
56 #endif
57 1066 }
58  
59 /* base64url, no padding, of n bytes into out, which holds 4 * ceil(n / 3)
60 * bytes. Returns the length written. */
61 1099 static size_t pmcp_b64url(const unsigned char *in, size_t n, char *out)
62 {
63 static const char tbl[] =
64 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
65 1099 size_t i = 0, o = 0;
66  
67 6433 ▶ while (i + 3 <= n) {
Branch: 1 branch
BranchTrueFalseTotal
while (i + 3 <= n) { 5334 1099 6433
68 5334 U32 v = ((U32)in[i] << 16) | ((U32)in[i + 1] << 8) | in[i + 2];
69 5334 out[o++] = tbl[(v >> 18) & 63];
70 5334 out[o++] = tbl[(v >> 12) & 63];
71 5334 out[o++] = tbl[(v >> 6) & 63];
72 5334 out[o++] = tbl[v & 63];
73 5334 i += 3;
74 }
75 1099 ▶ if (n - i == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (n - i == 1) { 1066 33 1099
76 1066 U32 v = (U32)in[i] << 16;
77 1066 out[o++] = tbl[(v >> 18) & 63];
78 1066 out[o++] = tbl[(v >> 12) & 63];
79 }
80 33 ▶ else if (n - i == 2) {
Branch: 1 branch
BranchTrueFalseTotal
else if (n - i == 2) { 29 4 33
81 29 U32 v = ((U32)in[i] << 16) | ((U32)in[i + 1] << 8);
82 29 out[o++] = tbl[(v >> 18) & 63];
83 29 out[o++] = tbl[(v >> 12) & 63];
84 29 out[o++] = tbl[(v >> 6) & 63];
85 }
86 1099 return o;
87 }
88  
89 /* A fresh id: 16 random bytes as 22 base64url characters. Returns +1. */
90 1066 static SV *pmcp_rand_id(pTHX)
91 {
92 unsigned char raw[PMCP_ID_BYTES];
93 char txt[24];
94 size_t len;
95  
96 1066 pmcp_random_bytes(aTHX_ raw, sizeof raw);
97 1066 len = pmcp_b64url(raw, sizeof raw, txt);
98 1066 return newSVpvn(txt, len);
99 }
100  
101 #endif /* PMCP_RAND_H */
« include/pmcp/pmcp_page.h ↑ Summary include/pmcp/pmcp_abi.h »