You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2009/10/05 15:15:11 UTC
svn commit: r821788 - in /commons/sandbox/runtime/trunk/src/main/native: ./
include/ include/arch/hpux/ include/arch/linux/ include/arch/solaris/
include/arch/windows/ port/
Author: mturk
Date: Mon Oct 5 13:15:10 2009
New Revision: 821788
URL: http://svn.apache.org/viewvc?rev=821788&view=rev
Log:
Add arc4random to the port
Added:
commons/sandbox/runtime/trunk/src/main/native/port/arc4random.c (with props)
Modified:
commons/sandbox/runtime/trunk/src/main/native/Makefile.in
commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in
commons/sandbox/runtime/trunk/src/main/native/include/acr_port.h
commons/sandbox/runtime/trunk/src/main/native/include/arch/hpux/acr_arch_private.h
commons/sandbox/runtime/trunk/src/main/native/include/arch/linux/acr_arch_private.h
commons/sandbox/runtime/trunk/src/main/native/include/arch/solaris/acr_arch_private.h
commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch_private.h
Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.in
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.in?rev=821788&r1=821787&r2=821788&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.in Mon Oct 5 13:15:10 2009
@@ -219,6 +219,7 @@
$(SRCDIR)/os/hpux/os.$(OBJ)
PPORT_OBJS=\
+ $(SRCDIR)/port/arc4random.$(OBJ) \
$(SRCDIR)/port/basename.$(OBJ) \
$(SRCDIR)/port/dirname.$(OBJ) \
$(SRCDIR)/port/fnmatch.$(OBJ) \
Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in?rev=821788&r1=821787&r2=821788&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in Mon Oct 5 13:15:10 2009
@@ -137,6 +137,7 @@
$(SRCDIR)/os/win32/wusec.$(OBJ)
PPORT_OBJS=\
+ $(SRCDIR)/port/arc4random.$(OBJ) \
$(SRCDIR)/port/basename.$(OBJ) \
$(SRCDIR)/port/dirname.$(OBJ) \
$(SRCDIR)/port/fnmatch.$(OBJ) \
Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_port.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_port.h?rev=821788&r1=821787&r2=821788&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_port.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_port.h Mon Oct 5 13:15:10 2009
@@ -69,6 +69,9 @@
#if defined(ACR_WANT_WCSLCPY)
size_t wcslcpy(wchar_t *, const wchar_t *, size_t);
#endif
+#if defined(ACR_WANT_ARC4RANDOM)
+unsigned int arc4random(void);
+#endif
/**
* pmatch():
Modified: commons/sandbox/runtime/trunk/src/main/native/include/arch/hpux/acr_arch_private.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/arch/hpux/acr_arch_private.h?rev=821788&r1=821787&r2=821788&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/arch/hpux/acr_arch_private.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/arch/hpux/acr_arch_private.h Mon Oct 5 13:15:10 2009
@@ -52,6 +52,7 @@
#define ACR_WANT_WCSLCAT 1
#define ACR_WANT_WCSLCPY 1
#define ACR_WANT_FNMATCH 1
+#define ACR_WANT_ARC4RANDOM 1
/**
* Temporary APR flags
Modified: commons/sandbox/runtime/trunk/src/main/native/include/arch/linux/acr_arch_private.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/arch/linux/acr_arch_private.h?rev=821788&r1=821787&r2=821788&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/arch/linux/acr_arch_private.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/arch/linux/acr_arch_private.h Mon Oct 5 13:15:10 2009
@@ -51,6 +51,7 @@
#define ACR_WANT_STRLCPY 1
#define ACR_WANT_WCSLCAT 1
#define ACR_WANT_WCSLCPY 1
+#define ACR_WANT_ARC4RANDOM 1
/**
* Temporary APR flags
Modified: commons/sandbox/runtime/trunk/src/main/native/include/arch/solaris/acr_arch_private.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/arch/solaris/acr_arch_private.h?rev=821788&r1=821787&r2=821788&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/arch/solaris/acr_arch_private.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/arch/solaris/acr_arch_private.h Mon Oct 5 13:15:10 2009
@@ -53,6 +53,7 @@
#define ACR_WANT_WCSLCAT 1
#define ACR_WANT_WCSLCPY 1
#define ACR_WANT_FNMATCH 1
+#define ACR_WANT_ARC4RANDOM 1
/**
* Temporary APR flags
Modified: commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch_private.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch_private.h?rev=821788&r1=821787&r2=821788&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch_private.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch_private.h Mon Oct 5 13:15:10 2009
@@ -105,6 +105,7 @@
#define ACR_WANT_WCSLCPY 1
#define ACR_WANT_ISBLANK 1
#define ACR_WANT_FNMATCH 1
+#define ACR_WANT_ARC4RANDOM 1
#define ACR_OVERLAPPED_ADD(O, V) \
Added: commons/sandbox/runtime/trunk/src/main/native/port/arc4random.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/port/arc4random.c?rev=821788&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/port/arc4random.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/port/arc4random.c Mon Oct 5 13:15:10 2009
@@ -0,0 +1,184 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*
+ * Arc4 random number generator for OpenBSD.
+ * Copyright 1996 David Mazieres <dm...@lcs.mit.edu>.
+ *
+ * Modification and redistribution in source and binary forms is
+ * permitted provided that due credit is given to the author and the
+ * OpenBSD project by leaving this copyright notice intact.
+ */
+
+/*
+ * This code is derived from section 17.1 of Applied Cryptography,
+ * second edition, which describes a stream cipher allegedly
+ * compatible with RSA Labs "RC4" cipher (the actual description of
+ * which is a trade secret). The same algorithm is used as a stream
+ * cipher called "arcfour" in Tatu Ylonen's ssh package.
+ *
+ * Here the stream cipher has been modified always to include the time
+ * when initializing the state. That makes it impossible to
+ * regenerate the same random sequence twice, so this can't be used
+ * for encryption, but will generate good random numbers.
+ *
+ * RC4 is a registered trademark of RSA Laboratories.
+ */
+
+#include "acr.h"
+#include "acr_private.h"
+#include "acr_arch.h"
+#include "acr_port.h"
+
+#include <sys/time.h>
+
+#if defined(ACR_WANT_ARC4RANDOM)
+struct arc4_stream {
+ unsigned char i;
+ unsigned char j;
+ unsigned char s[256];
+};
+
+static int rs_initialized;
+static struct arc4_stream rs;
+
+static ACR_INLINE void arc4_init(struct arc4_stream *);
+static ACR_INLINE void arc4_addrandom(struct arc4_stream *, unsigned char *, int);
+static void arc4_stir(struct arc4_stream *);
+static ACR_INLINE char arc4_getbyte(struct arc4_stream *);
+static ACR_INLINE acr_uint32_t arc4_getword(struct arc4_stream *);
+
+static ACR_INLINE void
+arc4_init(struct arc4_stream *as)
+{
+ int n;
+
+ for (n = 0; n < 256; n++)
+ as->s[n] = n;
+ as->i = 0;
+ as->j = 0;
+}
+
+static ACR_INLINE void
+arc4_addrandom(struct arc4_stream *as, unsigned char *dat, int datlen)
+{
+ int n;
+ unsigned char si;
+
+ as->i--;
+ for (n = 0; n < 256; n++) {
+ as->i = (as->i + 1);
+ si = as->s[as->i];
+ as->j = (as->j + si + dat[n % datlen]);
+ as->s[as->i] = as->s[as->j];
+ as->s[as->j] = si;
+ }
+ as->j = as->i;
+}
+
+static void arc4_stir(struct arc4_stream *as)
+{
+ int n;
+
+#if defined (WIN32)
+ struct {
+ SYSTEMTIME tv;
+ unsigned int rnd[(128 - sizeof(SYSTEMTIME)) / sizeof(unsigned int)];
+ } rdat;
+ HCRYPTPROV hProv;
+
+ GetSystemTime(&rdat.tv);
+ if (CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL,
+ CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
+ /* XXX: An ugly hack for Win64, randomness is such that noone should
+ * ever expect > 2^31 bytes of data at once without the prng
+ * coming to a complete halt.
+ */
+ CryptGenRandom(hProv, (DWORD)sizeof(rdat.rnd), rdat.rnd);
+ CryptReleaseContext(hProv, 0);
+ }
+#else
+ int fd;
+ struct {
+ struct timeval tv;
+ unsigned int rnd[(128 - sizeof(struct timeval)) / sizeof(unsigned int)];
+ } rdat;
+
+ gettimeofday(&rdat.tv, NULL);
+ fd = open("/dev/urandom", O_RDONLY);
+ if (fd != -1) {
+ read(fd, rdat.rnd, sizeof(rdat.rnd));
+ close(fd);
+ }
+#endif
+ /* fd < 0 or failed sysctl ? Ah, what the heck. We'll just take
+ * whatever was on the stack... */
+
+ arc4_addrandom(as, (void *) &rdat, sizeof(rdat));
+
+ /*
+ * Throw away the first N words of output, as suggested in the
+ * paper "Weaknesses in the Key Scheduling Algorithm of RC4"
+ * by Fluher, Mantin, and Shamir. (N = 256 in our case.)
+ */
+ for (n = 0; n < 256 * 4; n++)
+ arc4_getbyte(as);
+}
+
+static ACR_INLINE char arc4_getbyte(struct arc4_stream *as)
+{
+ unsigned char si, sj;
+
+ as->i = (as->i + 1);
+ si = as->s[as->i];
+ as->j = (as->j + si);
+ sj = as->s[as->j];
+ as->s[as->i] = sj;
+ as->s[as->j] = si;
+ return (as->s[(si + sj) & 0xff]);
+}
+
+static ACR_INLINE acr_uint32_t arc4_getword(struct arc4_stream *as)
+{
+ acr_uint32_t val;
+ val = arc4_getbyte(as) << 24;
+ val |= arc4_getbyte(as) << 16;
+ val |= arc4_getbyte(as) << 8;
+ val |= arc4_getbyte(as);
+ return val;
+}
+
+static void arc4random_stir()
+{
+ if (!rs_initialized) {
+ arc4_init(&rs);
+ rs_initialized = 1;
+ }
+ arc4_stir(&rs);
+}
+
+unsigned int arc4random()
+{
+ if (!rs_initialized)
+ arc4random_stir();
+ return arc4_getword(&rs);
+}
+
+#else
+
+UNUSED_SOURCE_FILE(arc4random);
+
+#endif /* ACR_WANT_ARC4RANDOM */
Propchange: commons/sandbox/runtime/trunk/src/main/native/port/arc4random.c
------------------------------------------------------------------------------
svn:eol-style = native