On Wed, Oct 12, 2016 at 10:46 AM, Alexander Bluhm <[email protected]> wrote: > Hi, > > My idea of the p5-BSD-arc4random port in OpenBSD is to keep the API > of the BSD::arc4random Perl module, but use our libc functions > internally. This has been done with BSD::arc4random::arc4random() > a while ago, but BSD::arc4random::arc4random_bytes() and > BSD::arc4random::arc4random_uniform() still use a pure Perl > implementation. I think that should be replaced with our > arc4random_buf(3) and arc4random_uniform(3) in libc. > > Any objections if I take maintainer? Most of the code is contained > in my patches anyway.
No problems, thank you, I'm not qualified enough to review those patches. regards > > ok? > > bluhm > > Index: devel/p5-BSD-arc4random/Makefile > =================================================================== > RCS file: /data/mirror/openbsd/cvs/ports/devel/p5-BSD-arc4random/Makefile,v > retrieving revision 1.8 > diff -u -p -r1.8 Makefile > --- devel/p5-BSD-arc4random/Makefile 20 Mar 2016 19:56:13 -0000 1.8 > +++ devel/p5-BSD-arc4random/Makefile 12 Oct 2016 15:40:29 -0000 > @@ -4,9 +4,9 @@ COMMENT = perl interface to the arc4 ra > > MODULES = cpan > DISTNAME = BSD-arc4random-1.50 > -REVISION = 3 > +REVISION = 4 > CATEGORIES = devel > -MAINTAINER = Abel Abraham Camarillo Ojeda <[email protected]> > +MAINTAINER = Alexander Bluhm <[email protected]> > > # MirOS Licence > PERMIT_PACKAGE_CDROM = Yes > Index: devel/p5-BSD-arc4random/patches/patch-MANIFEST > =================================================================== > RCS file: devel/p5-BSD-arc4random/patches/patch-MANIFEST > diff -N devel/p5-BSD-arc4random/patches/patch-MANIFEST > --- /dev/null 1 Jan 1970 00:00:00 -0000 > +++ devel/p5-BSD-arc4random/patches/patch-MANIFEST 12 Oct 2016 12:50:31 > -0000 > @@ -0,0 +1,15 @@ > +$OpenBSD$ > + > +The file arc4random.c is removed in post-extract as everything is > +implemented in OpenBSD libc. Avoid a build warning about manifest. > + > +--- MANIFEST.orig Sun May 17 15:05:04 2009 > ++++ MANIFEST Wed Oct 12 14:09:10 2016 > +@@ -3,7 +3,6 @@ META.yml > + Makefile.PL > + README > + TODO > +-arc4random.c > + arc4rnd_xs.c > + lib/BSD/arc4random.pm > + t/check.t > Index: devel/p5-BSD-arc4random/patches/patch-arc4rnd_xs_c > =================================================================== > RCS file: > /data/mirror/openbsd/cvs/ports/devel/p5-BSD-arc4random/patches/patch-arc4rnd_xs_c,v > retrieving revision 1.1 > diff -u -p -r1.1 patch-arc4rnd_xs_c > --- devel/p5-BSD-arc4random/patches/patch-arc4rnd_xs_c 16 May 2014 23:08:31 > -0000 1.1 > +++ devel/p5-BSD-arc4random/patches/patch-arc4rnd_xs_c 12 Oct 2016 15:34:36 > -0000 > @@ -1,10 +1,43 @@ > $OpenBSD: patch-arc4rnd_xs_c,v 1.1 2014/05/16 23:08:31 bluhm Exp $ > + > +Implement Perl XS wrapper for OpenBSD libc arc4random(3), > +arc4random_buf(3), arc4random_uniform(3), remove everything else. > + > --- arc4rnd_xs.c.orig Sun Oct 11 00:44:17 2009 > -+++ arc4rnd_xs.c Fri May 16 22:18:56 2014 > -@@ -54,11 +54,6 @@ __RCSID("$MirOS: contrib/hosted/tg/code/BSD::arc4rando > - #define u_int32_t uint32_t > ++++ arc4rnd_xs.c Wed Oct 12 17:33:31 2016 > +@@ -1,4 +1,5 @@ > + /*- > ++ * Copyright (c) 2016 Alexander Bluhm <[email protected]> > + * Copyright (c) 2008, 2009 > + * Thorsten Glaser <[email protected]> > + * > +@@ -18,15 +19,8 @@ > + * of said person's immediate fault when using the work as intended. > + */ > + > +-#include <sys/types.h> > + #include <stdlib.h> > + > +-#if defined(HAVE_STDINT_H) && HAVE_STDINT_H > +-#include <stdint.h> > +-#elif defined(USE_INTTYPES) > +-#include <inttypes.h> > +-#endif > +- > + #include "EXTERN.h" > + #include "perl.h" > + #include "XSUB.h" > +@@ -48,17 +42,8 @@ > + #define __RCSID(x) __IDSTRING(rcsid,x) > #endif > > +-__RCSID("$MirOS: contrib/hosted/tg/code/BSD::arc4random/arc4rnd_xs.c,v 1.5 > 2009/10/10 22:43:53 tg Exp $"); > ++__RCSID("$OpenBSD$"); > + > +-#ifdef REDEF_USCORETYPES > +-#define u_int32_t uint32_t > +-#endif > +- > -#ifdef NEED_ARC4RANDOM_DECL > -u_int32_t arc4random(void); > -void arc4random_addrandom(u_char *, int); > @@ -13,14 +46,16 @@ $OpenBSD: patch-arc4rnd_xs_c,v 1.1 2014/ > XS(XS_BSD__arc4random_arc4random_xs); > XS(XS_BSD__arc4random_arc4random_xs) > { > -@@ -74,142 +69,14 @@ XS(XS_BSD__arc4random_arc4random_xs) > +@@ -74,142 +59,62 @@ XS(XS_BSD__arc4random_arc4random_xs) > XSRETURN(1); > } > > -XS(XS_BSD__arc4random_stir_xs); > -XS(XS_BSD__arc4random_stir_xs) > --{ > -- dXSARGS; > ++XS(XS_BSD__arc4random_arc4random_uniform_xs); > ++XS(XS_BSD__arc4random_arc4random_uniform_xs) > + { > + dXSARGS; > - > - arc4random_stir(); > - > @@ -31,19 +66,22 @@ $OpenBSD: patch-arc4rnd_xs_c,v 1.1 2014/ > -XS(XS_BSD__arc4random_arc4random_addrandom_xs) > -{ > - dXSARGS; > -- dXSTARG; > -- SV *sv; > + dXSTARG; > + SV *sv; > - char *buf; > - STRLEN len; > -- uint32_t rv; > -- > -- sv = ST(0); > ++ uint32_t upper_bound; > + uint32_t rv; > + > + sv = ST(0); > - buf = SvPV(sv, len); > - arc4random_addrandom((unsigned char *)buf, (int)len); > - rv = arc4random(); > - XSprePUSH; > - PUSHu((UV)rv); > -- > ++ upper_bound = SvUV(sv); > ++ rv = arc4random_uniform(upper_bound); > + > - XSRETURN(1); > -} > - > @@ -65,11 +103,11 @@ $OpenBSD: patch-arc4rnd_xs_c,v 1.1 2014/ > - sv = ST(0); > - buf = SvPV(sv, len); > - rv = arc4random_pushb((void *)buf, (size_t)len); > -- XSprePUSH; > -- PUSHu((UV)rv); > -- > -- XSRETURN(1); > --} > + XSprePUSH; > + PUSHu((UV)rv); > + > + XSRETURN(1); > + } > -#elif defined(arc4random_pushk) > -#define XS_BSD__arc4random_arc4random_pushb_xs \ > - XS_BSD__arc4random_arc4random_pushk_xs > @@ -77,26 +115,39 @@ $OpenBSD: patch-arc4rnd_xs_c,v 1.1 2014/ > -#define XS_BSD__arc4random_arc4random_pushb_xs \ > - XS_BSD__arc4random_arc4random_addrandom_xs > -#endif > -- > + > -#if defined(arc4random_pushk) > -XS(XS_BSD__arc4random_arc4random_pushk_xs); > -XS(XS_BSD__arc4random_arc4random_pushk_xs) > --{ > -- dXSARGS; > -- dXSTARG; > -- SV *sv; > -- char *buf; > ++XS(XS_BSD__arc4random_arc4random_buf_xs); > ++XS(XS_BSD__arc4random_arc4random_buf_xs) > + { > + dXSARGS; > + dXSTARG; > + SV *sv; > + char *buf; > - STRLEN len; > - uint32_t rv; > -- > -- sv = ST(0); > ++ size_t nbytes; > + > + sv = ST(0); > - buf = SvPV(sv, len); > - rv = arc4random_pushk((void *)buf, (size_t)len); > -- XSprePUSH; > ++ 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; > - PUSHu((UV)rv); > -- > -- XSRETURN(1); > --} > ++ PUSHs(sv); > + > + XSRETURN(1); > + } > -#elif HAVE_ARC4RANDOM_PUSHB > -#define XS_BSD__arc4random_arc4random_pushk_xs \ > - XS_BSD__arc4random_arc4random_pushb_xs > @@ -104,7 +155,7 @@ $OpenBSD: patch-arc4rnd_xs_c,v 1.1 2014/ > -#define XS_BSD__arc4random_arc4random_pushk_xs \ > - XS_BSD__arc4random_arc4random_addrandom_xs > -#endif > -- > + > -#undef HAVE_ARC4RANDOM_KINTF > -#if HAVE_ARC4RANDOM_PUSHB || defined(arc4random_pushk) > -#define HAVE_ARC4RANDOM_KINTF 1 > @@ -142,6 +193,8 @@ $OpenBSD: patch-arc4rnd_xs_c,v 1.1 2014/ > - IDT_ARC4RANDOM_PUSHB > - IDT_arc4random_pushk > - IDT_ARC4RANDOM_KINTF > ++ " arc4random_uniform" > ++ " arc4random_buf" > " }"); > > > @@ -153,10 +206,12 @@ $OpenBSD: patch-arc4rnd_xs_c,v 1.1 2014/ > -static char func_a4rpk[] = "BSD::arc4random::arc4random_pushk_xs"; > -static char func_astir[] = "BSD::arc4random::arc4random_stir_xs"; > -static char func_kintf[] = "BSD::arc4random::have_kintf"; > ++static char func_a4r_uniform[] = "BSD::arc4random::arc4random_uniform_xs"; > ++static char func_a4r_buf[] = "BSD::arc4random::arc4random_buf_xs"; > > #ifdef __cplusplus > extern "C" > -@@ -222,12 +89,6 @@ XS(boot_BSD__arc4random) > +@@ -222,12 +127,8 @@ XS(boot_BSD__arc4random) > XS_VERSION_BOOTCHECK; > > newXS(func_a4r, XS_BSD__arc4random_arc4random_xs, file); > @@ -166,6 +221,8 @@ $OpenBSD: patch-arc4rnd_xs_c,v 1.1 2014/ > - newXS(func_astir, XS_BSD__arc4random_stir_xs, file); > - > - newCONSTSUB(NULL, func_kintf, newSViv(HAVE_ARC4RANDOM_KINTF)); > ++ newXS(func_a4r_uniform, XS_BSD__arc4random_arc4random_uniform_xs, > file); > ++ newXS(func_a4r_buf, XS_BSD__arc4random_arc4random_buf_xs, file); > > XSRETURN_YES; > } > Index: devel/p5-BSD-arc4random/patches/patch-lib_BSD_arc4random_pm > =================================================================== > RCS file: > /data/mirror/openbsd/cvs/ports/devel/p5-BSD-arc4random/patches/patch-lib_BSD_arc4random_pm,v > retrieving revision 1.1 > diff -u -p -r1.1 patch-lib_BSD_arc4random_pm > --- devel/p5-BSD-arc4random/patches/patch-lib_BSD_arc4random_pm 16 May 2014 > 23:08:31 -0000 1.1 > +++ devel/p5-BSD-arc4random/patches/patch-lib_BSD_arc4random_pm 12 Oct 2016 > 15:34:32 -0000 > @@ -1,7 +1,23 @@ > $OpenBSD: patch-lib_BSD_arc4random_pm,v 1.1 2014/05/16 23:08:31 bluhm Exp $ > + > +Do nothing special for arc4random_addrandom(), arc4random_pushb(), > +arc4random_pushk(), arc4random_stir(), they are not implemented in > +OpenBSD libc. > + > +Replace the pure Perl arc4random_bytes(), arc4random_uniform() > +implementation with arc4random_buf(3), arc4random_uniform(3) Perl > +XS wrapper for OpenBSD libc functions. > + > --- lib/BSD/arc4random.pm.orig Mon Jun 6 01:19:28 2011 > -+++ lib/BSD/arc4random.pm Fri May 16 21:46:22 2014 > -@@ -73,35 +73,24 @@ arc4random() > ++++ lib/BSD/arc4random.pm Wed Oct 12 17:34:11 2016 > +@@ -1,5 +1,6 @@ > + # $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 <[email protected]> > + # Copyright (c) 2008, 2009, 2010, 2011 > + # Thorsten Glaser <[email protected]> > + # Copyright (c) 2009 > +@@ -73,35 +74,24 @@ arc4random() > sub > arc4random_addrandom($) > { > @@ -40,3 +56,88 @@ $OpenBSD: patch-lib_BSD_arc4random_pm,v > return; > } > > +@@ -109,80 +99,18 @@ sub > + arc4random_bytes($;$) > + { > + my ($len, $buf) = @_; > +- my $val; > +- my $vleft = 0; > +- my $rv = ''; > +- my $idx = 0; > + > +- if (defined($buf)) { > +- $val = arc4random_pushb($buf); > +- $vleft = 4; > +- } > +- while (($len - $idx) >= 4) { > +- if ($vleft < 4) { > +- $val = arc4random(); > +- $vleft = 4; > +- } > +- vec($rv, $idx / 4, 32) = $val; > +- $idx += 4; > +- $vleft = 0; > +- } > +- while ($idx < $len) { > +- if ($vleft == 0) { > +- $val = arc4random(); > +- $vleft = 4; > +- } > +- vec($rv, $idx, 8) = $val & 0xFF; > +- $idx++; > +- $val >>= 8; > +- $vleft--; > +- } > +- return $rv; > ++ lock($arcfour_lock) if $have_threadlock; > ++ return &arc4random_buf_xs($len); > + } > + > +-# Perl implementation of arc4random_uniform(3) > +-# C implementation contributed by [email protected], [email protected] > +-# > +-# Calculate a uniformly distributed random number less than upper_bound > +-# avoiding "modulo bias". > +-# > +-# Uniformity is achieved by generating new random numbers until the one > +-# returned is outside the range [0, 2**32 % upper_bound). This > +-# guarantees the selected random number will be inside > +-# [2**32 % upper_bound, 2**32) which maps back to [0, upper_bound) > +-# after reduction modulo upper_bound. > +- > + sub > + arc4random_uniform($) > + { > + my $upper_bound = shift; > +- my $r; > +- my $min; > + > +- return 0 unless defined($upper_bound); > +- # convert upper_bound to 32-bit UV (unsigned integer value) > +- $upper_bound &= 0xFFFFFFFF; > +- return 0 if $upper_bound < 2 || $upper_bound > 0xFFFFFFFF; > +- > +- # Calculate (2**32 % upper_bound) avoiding 64-bit math > +- if ($upper_bound > 0x80000000) { > +- # 2**32 - upper_bound (only one "value area") > +- $min = 1 + (~$upper_bound & 0xFFFFFFFF); > +- } else { > +- # (2**32 - x) % x == 2**32 % x when x <= 2**31 > +- $min = (0xFFFFFFFF - $upper_bound + 1) % $upper_bound; > +- } > +- > +- # This could theoretically loop forever but each retry has > +- # p > 0.5 (worst case, usually far better) of selecting a > +- # number inside the range we need, so it should rarely need > +- # to re-roll. > +- while (1) { > +- $r = arc4random(); > +- last if $r >= $min; > +- } > +- > +- return ($r % $upper_bound); > ++ lock($arcfour_lock) if $have_threadlock; > ++ return &arc4random_uniform_xs($upper_bound); > + } > + > + # private implementation for a tied $RANDOM variable > Index: devel/p5-BSD-arc4random/pkg/PLIST > =================================================================== > RCS file: /data/mirror/openbsd/cvs/ports/devel/p5-BSD-arc4random/pkg/PLIST,v > retrieving revision 1.3 > diff -u -p -r1.3 PLIST > --- devel/p5-BSD-arc4random/pkg/PLIST 20 Mar 2015 22:47:40 -0000 1.3 > +++ devel/p5-BSD-arc4random/pkg/PLIST 12 Oct 2016 11:57:41 -0000 > @@ -2,7 +2,6 @@ > ${P5ARCH}/ > ${P5ARCH}/BSD/ > ${P5ARCH}/BSD/arc4random.pm > -${P5ARCH}/BSD/arc4random.pm.orig > ${P5ARCH}/auto/ > ${P5ARCH}/auto/BSD/ > ${P5ARCH}/auto/BSD/arc4random/
