File: Random.xs

package info (click to toggle)
libcrypt-openssl-random-perl 0.15-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 172 kB
  • sloc: perl: 19; makefile: 3
file content (104 lines) | stat: -rw-r--r-- 2,253 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

#include <openssl/rand.h>

#define PACKAGE_NAME "Crypt::OpenSSL::Random"

MODULE = Crypt::OpenSSL::Random		PACKAGE = Crypt::OpenSSL::Random
PROTOTYPES: DISABLE

void
random_bytes(num_bytes_SV)
    SV * num_bytes_SV;
PPCODE:
{
    unsigned char *rand_bytes;
    int num_bytes = SvIV(num_bytes_SV);
    if(New(0,rand_bytes, num_bytes, unsigned char) == NULL)
    {
        croak ("unable to allocate buffer for random bytes in package "
            PACKAGE_NAME);
    }

    if(RAND_bytes(rand_bytes, num_bytes))
    {
      XPUSHs(sv_2mortal(newSVpv((const char*)rand_bytes, num_bytes)));
        Safefree(rand_bytes);
        XSRETURN(1);
    }
    else
    {
        XSRETURN_NO;
    }
}

void
random_pseudo_bytes(num_bytes_SV)
    SV * num_bytes_SV;
PPCODE:
{
    unsigned char *rand_bytes;
    int num_bytes = SvIV(num_bytes_SV);
    if(New(0,rand_bytes, num_bytes, unsigned char) == NULL)
    {
        croak ("unable to allocate buffer for random bytes in package "
            PACKAGE_NAME);
    }

    if(RAND_bytes(rand_bytes, num_bytes))
    {
        XPUSHs(sv_2mortal(newSVpv((const char*)rand_bytes, num_bytes)));
        Safefree(rand_bytes);
        XSRETURN(1);
    }
    else
    {
        XSRETURN_NO;
    }
}

 # Seed the PRNG with user-provided bytes; returns true if the
 # seeding was sufficient.

void
random_seed(random_bytes_SV)
    SV * random_bytes_SV;
PPCODE:
{
    Size_t random_bytes_length;
    char *random_bytes;
    random_bytes = SvPV(random_bytes_SV, random_bytes_length);
    RAND_seed(random_bytes, random_bytes_length);
    XPUSHs( sv_2mortal( newSViv( RAND_status() ) ) );
}

 # Seed the PRNG with data from the indicated EntropyGatheringDaemon;
 # returns the number of bytes gathered, or -1 if there was a
 # connection failure or if the PRNG is still insufficiently seeded.

#ifndef OPENSSL_NO_EGD

void
random_egd(egd_SV)
    SV * egd_SV;
PPCODE:
{
    Size_t egd_length;
    int status;
    char *egd = SvPV(egd_SV, egd_length);
    status = RAND_egd(egd);
    XPUSHs( sv_2mortal( newSViv( status ) ) );
}

#endif

 # Returns true if the PRNG has enough seed data

void
random_status()
PPCODE:
{
    XPUSHs( sv_2mortal( newSViv( RAND_status() ) ) );
}