File Coverage

Random.xs
Criterion Covered Total %
statement 16 23 69.5
branch 11 26 42.3
condition n/a
subroutine n/a
pod n/a
total 27 49 55.1


line stmt bran cond sub pod time code
1             #include "EXTERN.h"
2             #include "perl.h"
3             #include "XSUB.h"
4              
5             #include
6              
7             #define PACKAGE_NAME "Crypt::OpenSSL::Random"
8              
9             MODULE = Crypt::OpenSSL::Random PACKAGE = Crypt::OpenSSL::Random
10             PROTOTYPES: DISABLE
11              
12             void
13             random_bytes(num_bytes_SV)
14             SV * num_bytes_SV;
15             PPCODE:
16             {
17             unsigned char *rand_bytes;
18 1 50         int num_bytes = SvIV(num_bytes_SV);
19 1 50         if(New(0,rand_bytes, num_bytes, unsigned char) == NULL)
20             {
21 0           croak ("unable to allocate buffer for random bytes in package "
22             PACKAGE_NAME);
23             }
24              
25 1 50         if(RAND_bytes(rand_bytes, num_bytes))
26             {
27 1 50         XPUSHs(sv_2mortal(newSVpv((const char*)rand_bytes, num_bytes)));
28 1           Safefree(rand_bytes);
29 1           XSRETURN(1);
30             }
31             else
32             {
33 0           XSRETURN_NO;
34             }
35             }
36              
37             void
38             random_pseudo_bytes(num_bytes_SV)
39             SV * num_bytes_SV;
40             PPCODE:
41             {
42             unsigned char *rand_bytes;
43 1 50         int num_bytes = SvIV(num_bytes_SV);
44 1 50         if(New(0,rand_bytes, num_bytes, unsigned char) == NULL)
45             {
46 0           croak ("unable to allocate buffer for random bytes in package "
47             PACKAGE_NAME);
48             }
49              
50 1 50         if(RAND_bytes(rand_bytes, num_bytes))
51             {
52 1 50         XPUSHs(sv_2mortal(newSVpv((const char*)rand_bytes, num_bytes)));
53 1           Safefree(rand_bytes);
54 1           XSRETURN(1);
55             }
56             else
57             {
58 0           XSRETURN_NO;
59             }
60             }
61              
62             # Seed the PRNG with user-provided bytes; returns true if the
63             # seeding was sufficient.
64              
65             void
66             random_seed(random_bytes_SV)
67             SV * random_bytes_SV;
68             PPCODE:
69             {
70             Size_t random_bytes_length;
71             char *random_bytes;
72 1 50         random_bytes = SvPV(random_bytes_SV, random_bytes_length);
73 1           RAND_seed(random_bytes, random_bytes_length);
74 1 50         XPUSHs( sv_2mortal( newSViv( RAND_status() ) ) );
75             }
76              
77             # Seed the PRNG with data from the indicated EntropyGatheringDaemon;
78             # returns the number of bytes gathered, or -1 if there was a
79             # connection failure or if the PRNG is still insufficiently seeded.
80              
81             #ifndef OPENSSL_NO_EGD
82              
83             void
84             random_egd(egd_SV)
85             SV * egd_SV;
86             PPCODE:
87             {
88             Size_t egd_length;
89             int status;
90 0 0         char *egd = SvPV(egd_SV, egd_length);
91 0           status = RAND_egd(egd);
92 0 0         XPUSHs( sv_2mortal( newSViv( status ) ) );
93             }
94              
95             #endif
96              
97             # Returns true if the PRNG has enough seed data
98              
99             void
100             random_status()
101             PPCODE:
102             {
103 1 50         XPUSHs( sv_2mortal( newSViv( RAND_status() ) ) );
104             }