Skip Menu |

This queue is for tickets about the BSD-arc4random CPAN distribution.

Report information
The Basics
Id: 118384
Status: new
Priority: 0/
Queue: BSD-arc4random

People
Owner: Nobody in particular
Requestors: alexander.bluhm [...] gmx.net
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: (no value)
Fixed in: (no value)



Subject: XS wrapper for arc4random_uniform and arc4random_buf
Hi, I maintain the p5-BSD-arc4random Port in OpenBSD. As the arc4random functions are originally from there, you might be interested what has been done. As we only trust the arc4random(3) functions in our libc, I have implemented wrappers for them. BSD::arc4random must use them, I have deleted everything else. Especially we don't need or have a possibility to stir the entropy pool from user programs. As you mention in your TODO List that you plan to use arc4random_buf(), I like to show you the XS wrapper I have written for arc4random_uniform(3) and arc4random_buf(3). bluhm
Subject: arc4random.pm
# $MirOS: contrib/hosted/tg/code/BSD::arc4random/lib/BSD/arc4random.pm,v 1.10 2011/06/05 23:19:04 tg Exp $ #- # Copyright (c) 2016 Alexander Bluhm <bluhm@openbsd.org> # Copyright (c) 2008, 2009, 2010, 2011 # Thorsten Glaser <tg@mirbsd.org> # Copyright (c) 2009 # Benny Siegert <bsiegert@mirbsd.org> # # Provided that these terms and disclaimer and all copyright notices # are retained or reproduced in an accompanying document, permission # is granted to deal in this work without restriction, including un- # limited rights to use, publicly perform, distribute, sell, modify, # merge, give away, or sublicence. # # This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to # the utmost extent permitted by applicable law, neither express nor # implied; without malicious intent or gross negligence. In no event # may a licensor, author or contributor be held liable for indirect, # direct, other damage, loss, or other issues arising in any way out # of dealing in the work, even if advised of the possibility of such # damage or existence of a defect, except proven that it results out # of said person's immediate fault when using the work as intended. package BSD::arc4random; use strict; use warnings; BEGIN { require Exporter; require DynaLoader; use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); $VERSION = "1.50"; @ISA = qw(Exporter DynaLoader); @EXPORT = qw(); @EXPORT_OK = qw( $RANDOM &arc4random &arc4random_addrandom &arc4random_bytes &arc4random_pushb &arc4random_pushk &arc4random_stir &arc4random_uniform ); %EXPORT_TAGS = ( all => [ @EXPORT_OK ], ); } use vars qw($RANDOM); # public tied integer variable sub have_kintf() {} # public constant function, prototyped my $have_threadlock = 1; my $arcfour_lock; eval { require threads::shared; }; if ($@) { $have_threadlock = 0; # module not available } else { # private thread lock threads::shared::share($arcfour_lock); }; bootstrap BSD::arc4random $BSD::arc4random::VERSION; # public thread-safe functions sub arc4random() { lock($arcfour_lock) if $have_threadlock; return &arc4random_xs(); } sub arc4random_addrandom($) { goto &arc4random; } sub arc4random_pushb($) { goto &arc4random; } sub arc4random_pushk($) { goto &arc4random; } sub arc4random_stir() { return; } sub arc4random_bytes($;$) { my ($len, $buf) = @_; lock($arcfour_lock) if $have_threadlock; return &arc4random_buf_xs($len); } sub arc4random_uniform($) { my $upper_bound = shift; lock($arcfour_lock) if $have_threadlock; return &arc4random_uniform_xs($upper_bound); } # private implementation for a tied $RANDOM variable sub TIESCALAR { my $class = shift; my $max = shift; if (!defined($max) || ($max = int($max)) > 0xFFFFFFFE || $max < 0) { $max = 0; } return bless \$max, $class; } sub FETCH { my $self = shift; return ($$self == 0 ? arc4random() : arc4random_uniform($$self + 1)); } sub STORE { my $self = shift; my $value = shift; arc4random_pushb($value); } # tie the public $RANDOM variable to an mksh-style implementation tie $RANDOM, 'BSD::arc4random', 0x7FFF; # we are nice and re-seed perl's internal PRNG as well srand(arc4random_pushb(pack("F*", rand(), rand(), rand(), rand()))); 1; __END__ =head1 NAME BSD::arc4random - Perl interface to the arc4 random number generator =head1 SYNOPSIS use BSD::arc4random qw(:all); $v = arc4random(); $v = arc4random_uniform($hz); if (!BSD::arc4random::have_kintf()) { $v = arc4random_addrandom("entropy to pass to the system"); } else { $v = arc4random_pushb("entropy to pass to the system"); $v = arc4random_pushk("entropy to pass to the kernel"); } $s = arc4random_bytes(16, "entropy to pass to libc"); arc4random_stir(); $s = arc4random_bytes(16); print $RANDOM; =head1 DESCRIPTION This set of functions maps the L<arc4random(3)> family of libc functions into Perl code. All functions listed below are ithreads-safe. The internal XS functions are not, but you are not supposed to call them, either. On module load, perl's internal PRNG is re-seeded, as a bonus, using B<srand> with an argument calculated from using B<arc4random_pushb> on some entropy returned from B<rand>'s previous state. =head2 LOW-LEVEL FUNCTIONS =over 4 =item B<arc4random>() This function returns an unsigned 32-bit integer random value. =item B<arc4random_addrandom>(I<pbuf>) This function adds the entropy from I<pbuf> into the libc pool and returns an unsigned 32-bit integer random value from it. =item B<arc4random_pushb>(I<pbuf>) This function first pushes the I<pbuf> argument to the kernel if possible, then the entropy returned by the kernel into the libc pool, then returns an unsigned 32-bit integer random value from it. =item B<arc4random_pushk>(I<pbuf>) This function first pushes the I<pbuf> argument to the kernel if possible, then returns an unsigned 32-bit integer random value from the kernel. This function is deprecated. Use B<arc4random_pushb> instead. =item B<arc4random_stir>() This procedure attempts to retrieve new entropy from the kernel and add it to the libc pool. Usually, this means you must have access to the L<urandom(4)> device; create it inside L<chroot(2)> jails first if you use them. =item B<have_kintf>() This constant function returns 1 if B<arc4random_pushb> and/or B<arc4random_pushk> actually call the kernel interfaces, 0 if they merely map to B<arc4random_addrandom> instead. =back =head2 HIGH-LEVEL FUNCTIONS =over 4 =item B<arc4random_bytes>(I<num>[, I<pbuf>]) This function returns a string containing as many random bytes as requested by the integral argument I<num>. An optional I<pbuf> argument is passed to the system first. =item B<arc4random_uniform>(I<upper_bound>) Calculate a uniformly distributed random number less than upper_bound avoiding "modulo bias". =back =head2 PACKAGE VARIABLES =over 4 =item B<$RANDOM> The B<$RANDOM> returns a random value in the range S<[0; 32767]> on each read attempt and pushes any value it is assigned to the kernel. It is tied at module load time. =item tie I<variable>, 'BSD::arc4random'[, I<max>] You can tie any scalar variable to this package; the I<max> argument is the maximum number returned; if undefined, 0 or S<E<62>= 0xFFFFFFFF>, no bound is used, and values in the range S<[0; 2**32-1]> are returned. They will behave like B<$RANDOM>. =back =head1 AUTHOR Thorsten Glaser E<lt>tg@mirbsd.deE<gt> =head1 SEE ALSO The L<arc4random(3)> manual page, available online at: L<https://www.mirbsd.org/man/arc4random.3> Perl's L<rand> and L<srand> functions via L<perlfunc> and L<perlfaq4>. The B<randex.pl> plugin for Irssi, implementing the MirOS RANDEX protocol (entropy exchange over IRC), with CVSweb at: L<http://cvs.mirbsd.de/ports/net/irssi/files/randex.pl> L<https://www.mirbsd.org/a4rp5bsd.htm> when it's done being written. =head1 COPYRIGHT AND LICENSE Copyright (c) 2008, 2009, 2010, 2011 Thorsten "mirabilos" Glaser Copyright (c) 2009 Benny Siegert Credits to Sebastian "Vutral" Schwarz This module is covered by the MirOS Licence: L<http://mirbsd.de/MirOS-Licence> The original C implementation of arc4random_uniform was contributed by Damien Miller from OpenBSD, with simplifications by Jinmei Tatuya. =cut
Subject: arc4rnd_xs.c
/*- * Copyright (c) 2016 Alexander Bluhm <bluhm@openbsd.org> * Copyright (c) 2008, 2009 * Thorsten Glaser <tg@mirbsd.org> * * Provided that these terms and disclaimer and all copyright notices * are retained or reproduced in an accompanying document, permission * is granted to deal in this work without restriction, including un- * limited rights to use, publicly perform, distribute, sell, modify, * merge, give away, or sublicence. * * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to * the utmost extent permitted by applicable law, neither express nor * implied; without malicious intent or gross negligence. In no event * may a licensor, author or contributor be held liable for indirect, * direct, other damage, loss, or other issues arising in any way out * of dealing in the work, even if advised of the possibility of such * damage or existence of a defect, except proven that it results out * of said person's immediate fault when using the work as intended. */ #include <stdlib.h> #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #if !defined(__attribute__) && (!defined(__GNUC__) || (__GNUC__ < 1) || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)) #define __attribute__(x) /* nothing */ #endif #if !defined(__RCSID) || !defined(__IDSTRING) #undef __RCSID #undef __IDSTRING #undef __IDSTRING_CONCAT #undef __IDSTRING_EXPAND #define __IDSTRING_CONCAT(l,p) __LINTED__ ## l ## _ ## p #define __IDSTRING_EXPAND(l,p) __IDSTRING_CONCAT(l,p) #define __IDSTRING(prefix, string) \ static const char __IDSTRING_EXPAND(__LINE__,prefix) [] \ __attribute__((used)) = "@(""#)" #prefix ": " string #define __RCSID(x) __IDSTRING(rcsid,x) #endif __RCSID("$OpenBSD: patch-arc4rnd_xs_c,v 1.2 2016/10/12 17:47:23 bluhm Exp $"); XS(XS_BSD__arc4random_arc4random_xs); XS(XS_BSD__arc4random_arc4random_xs) { dXSARGS; dXSTARG; uint32_t rv; rv = arc4random(); XSprePUSH; PUSHu((UV)rv); XSRETURN(1); } XS(XS_BSD__arc4random_arc4random_uniform_xs); XS(XS_BSD__arc4random_arc4random_uniform_xs) { dXSARGS; dXSTARG; SV *sv; uint32_t upper_bound; uint32_t rv; sv = ST(0); upper_bound = SvUV(sv); rv = arc4random_uniform(upper_bound); XSprePUSH; PUSHu((UV)rv); XSRETURN(1); } XS(XS_BSD__arc4random_arc4random_buf_xs); XS(XS_BSD__arc4random_arc4random_buf_xs) { dXSARGS; dXSTARG; SV *sv; char *buf; size_t nbytes; sv = ST(0); nbytes = SvUV(sv); sv = sv_newmortal(); if (nbytes == SIZE_T_MAX) nbytes--; Newx(buf, nbytes + 1, char); arc4random_buf(buf, nbytes); buf[nbytes] = '\0'; sv_usepvn_flags(sv, buf, nbytes, SV_SMAGIC | SV_HAS_TRAILING_NUL); XSprePUSH; PUSHs(sv); XSRETURN(1); } __IDSTRING(api_text, "BSD::arc4random " XS_VERSION " with {" " arc4random" " arc4random_uniform" " arc4random_buf" " }"); /* the Perl API is not const clean */ static char file[] = __FILE__; static char func_a4r[] = "BSD::arc4random::arc4random_xs"; static char func_a4r_uniform[] = "BSD::arc4random::arc4random_uniform_xs"; static char func_a4r_buf[] = "BSD::arc4random::arc4random_buf_xs"; #ifdef __cplusplus extern "C" #endif XS(boot_BSD__arc4random); XS(boot_BSD__arc4random) { dXSARGS; XS_VERSION_BOOTCHECK; newXS(func_a4r, XS_BSD__arc4random_arc4random_xs, file); newXS(func_a4r_uniform, XS_BSD__arc4random_arc4random_uniform_xs, file); newXS(func_a4r_buf, XS_BSD__arc4random_arc4random_buf_xs, file); XSRETURN_YES; }