You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ta...@apache.org on 2010/06/04 22:12:59 UTC
svn commit: r951550 - in /activemq/activemq-cpp/trunk/activemq-cpp/src/main:
./ decaf/internal/security/ decaf/internal/security/unix/
decaf/internal/security/windows/ decaf/security/ decaf/util/
Author: tabish
Date: Fri Jun 4 20:12:59 2010
New Revision: 951550
URL: http://svn.apache.org/viewvc?rev=951550&view=rev
Log:
Adds an implementation of a Secure Random number generator that attempts to get random seeds from the platform's own sources before falling back to software based random number generators.
Added:
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/security/
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/security/unix/
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/security/unix/SecureRandomImpl.cpp (with props)
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/security/unix/SecureRandomImpl.h (with props)
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/security/windows/
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/security/windows/SecureRandomImpl.cpp (with props)
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/security/windows/SecureRandomImpl.h (with props)
Modified:
activemq/activemq-cpp/trunk/activemq-cpp/src/main/Makefile.am
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/security/SecureRandom.cpp
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/security/SecureRandom.h
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/security/SecureRandomSpi.cpp
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/security/SecureRandomSpi.h
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/Random.cpp
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/Random.h
Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/Makefile.am
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/Makefile.am?rev=951550&r1=951549&r2=951550&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/Makefile.am (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/Makefile.am Fri Jun 4 20:12:59 2010
@@ -517,6 +517,7 @@ cc_sources = \
decaf/internal/nio/IntArrayBuffer.cpp \
decaf/internal/nio/LongArrayBuffer.cpp \
decaf/internal/nio/ShortArrayBuffer.cpp \
+ decaf/internal/security/unix/SecureRandomImpl.cpp \
decaf/internal/util/ByteArrayAdapter.cpp \
decaf/internal/util/HexStringParser.cpp \
decaf/internal/util/Resource.cpp \
@@ -1210,6 +1211,8 @@ h_sources = \
decaf/internal/nio/IntArrayBuffer.h \
decaf/internal/nio/LongArrayBuffer.h \
decaf/internal/nio/ShortArrayBuffer.h \
+ decaf/internal/security/unix/SecureRandomImpl.h \
+ decaf/internal/security/windows/SecureRandomImpl.h \
decaf/internal/util/ByteArrayAdapter.h \
decaf/internal/util/GenericResource.h \
decaf/internal/util/HexStringParser.h \
Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/security/unix/SecureRandomImpl.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/security/unix/SecureRandomImpl.cpp?rev=951550&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/security/unix/SecureRandomImpl.cpp (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/security/unix/SecureRandomImpl.cpp Fri Jun 4 20:12:59 2010
@@ -0,0 +1,153 @@
+/*
+ * 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.
+ */
+
+#include "SecureRandomImpl.h"
+
+#include <decaf/lang/Exception.h>
+#include <decaf/lang/exceptions/RuntimeException.h>
+#include <decaf/lang/exceptions/NullPointerException.h>
+#include <decaf/lang/exceptions/IllegalArgumentException.h>
+#include <decaf/util/Random.h>
+
+#include <decaf/internal/AprPool.h>
+
+#include <memory>
+#include <apr_file_io.h>
+
+using namespace decaf;
+using namespace decaf::lang;
+using namespace decaf::lang::exceptions;
+using namespace decaf::util;
+using namespace decaf::security;
+using namespace decaf::internal;
+using namespace decaf::internal::security;
+
+////////////////////////////////////////////////////////////////////////////////
+namespace decaf {
+namespace internal {
+namespace security {
+
+ class SRNGData {
+ public:
+
+ AprPool pool;
+ apr_file_t* randFile;
+ std::auto_ptr<Random> random;
+
+ SRNGData() : randFile( NULL ), random() {
+ }
+
+ };
+
+}}}
+
+////////////////////////////////////////////////////////////////////////////////
+SecureRandomImpl::SecureRandomImpl() {
+
+ this->config = new SRNGData();
+
+ try{
+
+ const char* files[] = { "/dev/urandom", "/dev/random" };
+ int index = 0;
+ apr_status_t result = APR_SUCCESS;
+
+ do {
+ // Attempt to find an OS source for secure random bytes.
+ result = apr_file_open( &config->randFile, files[index],
+ APR_READ, APR_OS_DEFAULT,
+ config->pool.getAprPool() );
+ } while( index < 2 && result != APR_SUCCESS );
+
+ // Defaults to the Decaf version.
+ if( result != APR_SUCCESS ) {
+ this->config->random.reset( new Random() );
+ }
+ }
+ DECAF_CATCH_RETHROW( Exception )
+ DECAF_CATCHALL_THROW( Exception )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+SecureRandomImpl::~SecureRandomImpl() {
+ try{
+ delete this->config;
+ }
+ DECAF_CATCH_NOTHROW( Exception )
+ DECAF_CATCHALL_NOTHROW()
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void SecureRandomImpl::providerSetSeed( const unsigned char* seed, int size ) {
+
+ // Only seed the default random, the other sources don't need a seed.
+ if( this->config->random.get() != NULL ) {
+
+ for( int i = 0; i < size; i++ ) {
+ this->config->random->setSeed( (long long)seed[i] );
+ }
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void SecureRandomImpl::providerNextBytes( unsigned char* bytes, int numBytes ) {
+
+ if( bytes == NULL ) {
+ throw NullPointerException(
+ __FILE__, __LINE__, "Byte Buffer passed cannot be NULL." );
+ }
+
+ if( numBytes < 0 ) {
+ throw IllegalArgumentException(
+ __FILE__, __LINE__, "Number of bytes to read was negative: %d", numBytes );
+ }
+
+ if( this->config->randFile != NULL ) {
+
+ apr_status_t result = APR_EOF;
+ apr_size_t bytesRead = 0;
+
+ for(;;) {
+
+ // Instruct APR to read it all.
+ result = apr_file_read_full( this->config->randFile, (void*)bytes, numBytes, &bytesRead );
+
+ // Since the dev random files are special OS random sources we should never get
+ // an EOF or other error, if so its bad.
+ if( result != APR_SUCCESS ) {
+ throw RuntimeException(
+ __FILE__, __LINE__,
+ "Unexpected error while reading random bytes from system resources." );
+ }
+ }
+
+ } else {
+ this->config->random->nextBytes( bytes, numBytes );
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+unsigned char* SecureRandomImpl::providerGenerateSeed( int numBytes ) {
+
+ if( numBytes == 0 ) {
+ return NULL;
+ }
+
+ unsigned char* buffer = new unsigned char[numBytes];
+ providerNextBytes( buffer, numBytes );
+ return buffer;
+}
Propchange: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/security/unix/SecureRandomImpl.cpp
------------------------------------------------------------------------------
svn:eol-style = native
Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/security/unix/SecureRandomImpl.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/security/unix/SecureRandomImpl.h?rev=951550&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/security/unix/SecureRandomImpl.h (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/security/unix/SecureRandomImpl.h Fri Jun 4 20:12:59 2010
@@ -0,0 +1,65 @@
+/*
+ * 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.
+ */
+
+#ifndef _DECAF_INTERNAL_SECURITY_SECURERANDOMIMPL_H_
+#define _DECAF_INTERNAL_SECURITY_SECURERANDOMIMPL_H_
+
+#include <decaf/util/Config.h>
+
+#include <decaf/security/SecureRandomSpi.h>
+
+namespace decaf {
+namespace internal {
+namespace security {
+
+ class SRNGData;
+
+ /**
+ * Secure Random Number Generator for Unix based platforms that attempts to obtain
+ * secure bytes with high entropy from known sources. If the platform does not have
+ * a source of secure bytes then the platform random number generator is used if one
+ * exists otherwise the Decaf RNG is used as a last resort.
+ *
+ * @since 1.0
+ */
+ class DECAF_API SecureRandomImpl : public decaf::security::SecureRandomSpi {
+ private:
+
+ SecureRandomImpl( const SecureRandomImpl& );
+ SecureRandomImpl& operator= ( const SecureRandomImpl& );
+
+ private:
+
+ SRNGData* config;
+
+ public:
+
+ SecureRandomImpl();
+
+ virtual ~SecureRandomImpl();
+
+ virtual void providerSetSeed( const unsigned char* seed, int size );
+
+ virtual void providerNextBytes( unsigned char* bytes, int numBytes );
+
+ virtual unsigned char* providerGenerateSeed( int numBytes );
+
+ };
+
+}}}
+
+#endif /* _DECAF_INTERNAL_SECURITY_SECURERANDOMIMPL_H_ */
Propchange: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/security/unix/SecureRandomImpl.h
------------------------------------------------------------------------------
svn:eol-style = native
Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/security/windows/SecureRandomImpl.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/security/windows/SecureRandomImpl.cpp?rev=951550&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/security/windows/SecureRandomImpl.cpp (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/security/windows/SecureRandomImpl.cpp Fri Jun 4 20:12:59 2010
@@ -0,0 +1,46 @@
+/*
+ * 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.
+ */
+
+#include "SecureRandomImpl.h"
+
+using namespace decaf;
+using namespace decaf::security;
+using namespace decaf::internal;
+using namespace decaf::internal::security;
+
+////////////////////////////////////////////////////////////////////////////////
+SecureRandomImpl::SecureRandomImpl() {
+}
+
+////////////////////////////////////////////////////////////////////////////////
+SecureRandomImpl::~SecureRandomImpl() {
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void SecureRandomImpl::providerSetSeed( const unsigned char* seed, int size ) {
+
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void SecureRandomImpl::providerNextBytes( unsigned char* bytes, int numBytes ) {
+
+}
+
+////////////////////////////////////////////////////////////////////////////////
+unsigned char* SecureRandomImpl::providerGenerateSeed( int numBytes ) {
+
+}
Propchange: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/security/windows/SecureRandomImpl.cpp
------------------------------------------------------------------------------
svn:eol-style = native
Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/security/windows/SecureRandomImpl.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/security/windows/SecureRandomImpl.h?rev=951550&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/security/windows/SecureRandomImpl.h (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/security/windows/SecureRandomImpl.h Fri Jun 4 20:12:59 2010
@@ -0,0 +1,51 @@
+/*
+ * 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.
+ */
+
+#ifndef _DECAF_INTERNAL_SECURITY_SECURERANDOMIMPL_H_
+#define _DECAF_INTERNAL_SECURITY_SECURERANDOMIMPL_H_
+
+#include <decaf/util/Config.h>
+
+#include <decaf/security/SecureRandomSpi.h>
+
+namespace decaf {
+namespace internal {
+namespace security {
+
+ class DECAF_API SecureRandomImpl : public decaf::security::SecureRandomSpi {
+ private:
+
+ SecureRandomImpl( const SecureRandomImpl& );
+ SecureRandomImpl& operator= ( const SecureRandomImpl& );
+
+ public:
+
+ SecureRandomImpl();
+
+ virtual ~SecureRandomImpl();
+
+ virtual void providerSetSeed( const unsigned char* seed, int size );
+
+ virtual void providerNextBytes( unsigned char* bytes, int numBytes );
+
+ virtual unsigned char* providerGenerateSeed( int numBytes );
+
+ };
+
+}}}
+
+#endif /* _DECAF_INTERNAL_SECURITY_SECURERANDOMIMPL_H_ */
Propchange: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/security/windows/SecureRandomImpl.h
------------------------------------------------------------------------------
svn:eol-style = native
Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/security/SecureRandom.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/security/SecureRandom.cpp?rev=951550&r1=951549&r2=951550&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/security/SecureRandom.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/security/SecureRandom.cpp Fri Jun 4 20:12:59 2010
@@ -17,15 +17,53 @@
#include "SecureRandom.h"
+#include <decaf/lang/exceptions/NullPointerException.h>
+#include <decaf/lang/exceptions/IllegalArgumentException.h>
+
+#ifdef HAVE_PTHREAD_H
+#include <decaf/internal/security/unix/SecureRandomImpl.h>
+#else
+#include <decaf/internal/security/windows/SecureRandomImpl.h>
+#endif
+
using namespace decaf;
+using namespace decaf::lang;
+using namespace decaf::lang::exceptions;
using namespace decaf::security;
+using namespace decaf::internal;
+using namespace decaf::internal::security;
////////////////////////////////////////////////////////////////////////////////
SecureRandom::SecureRandom() {
+ this->secureRandom.reset( new SecureRandomImpl() );
}
////////////////////////////////////////////////////////////////////////////////
-SecureRandom::SecureRandom( const std::vector<unsigned char>& seed DECAF_UNUSED ) {
+SecureRandom::SecureRandom( const std::vector<unsigned char>& seed ) {
+
+ this->secureRandom.reset( new SecureRandomImpl() );
+
+ if( !seed.empty() ) {
+ this->secureRandom->providerSetSeed( &seed[0], (int)seed.size() );
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+SecureRandom::SecureRandom( const unsigned char* seed, int size ) {
+
+ if( seed == NULL ) {
+ throw NullPointerException( __FILE__, __LINE__, "Seed buffer pointer passed was NULL" );
+ }
+
+ if( size < 0 ) {
+ throw IllegalArgumentException( __FILE__, __LINE__, "Passed buffer size was negative." );
+ }
+
+ this->secureRandom.reset( new SecureRandomImpl() );
+
+ if( size > 0 ) {
+ this->secureRandom->providerSetSeed( seed, size );
+ }
}
////////////////////////////////////////////////////////////////////////////////
@@ -34,21 +72,74 @@ SecureRandom::~SecureRandom() {
////////////////////////////////////////////////////////////////////////////////
void SecureRandom::nextBytes( std::vector<unsigned char>& buf ) {
- Random::nextBytes( buf );
+ if( !buf.empty() ) {
+ this->secureRandom->providerNextBytes( &buf[0], (int)buf.size() );
+ }
}
////////////////////////////////////////////////////////////////////////////////
-void SecureRandom::setSeed( unsigned long long seed ) {
+void SecureRandom::nextBytes( unsigned char* buf, int size ) {
+
+ if( buf == NULL ) {
+ throw NullPointerException( __FILE__, __LINE__, "Buffer pointer passed was NULL" );
+ }
- Random::setSeed( seed );
+ if( size < 0 ) {
+ throw IllegalArgumentException( __FILE__, __LINE__, "Passed buffer size was negative." );
+ }
+
+ if( size > 0 ) {
+ this->secureRandom->providerNextBytes( buf, size );
+ }
}
////////////////////////////////////////////////////////////////////////////////
-void SecureRandom::setSeed( const std::vector<unsigned char>& seed DECAF_UNUSED ) {
+void SecureRandom::setSeed( unsigned long long seed ) {
+
+ if( seed == 0 ) {
+ return;
+ }
+
+ unsigned char byteSeed[] = { (unsigned char)( ( seed >> 56 ) & 0xFF ),
+ (unsigned char)( ( seed >> 48 ) & 0xFF ),
+ (unsigned char)( ( seed >> 40 ) & 0xFF ),
+ (unsigned char)( ( seed >> 32 ) & 0xFF ),
+ (unsigned char)( ( seed >> 24 ) & 0xFF ),
+ (unsigned char)( ( seed >> 16 ) & 0xFF ),
+ (unsigned char)( ( seed >> 8 ) & 0xFF ),
+ (unsigned char)( ( seed ) & 0xFF ) };
+ this->secureRandom->providerSetSeed( byteSeed, 8 );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void SecureRandom::setSeed( const std::vector<unsigned char>& seed ) {
+ if( !seed.empty() ) {
+ this->secureRandom->providerSetSeed( &seed[0], (int)seed.size() );
+ }
}
////////////////////////////////////////////////////////////////////////////////
-int SecureRandom::next( int bits ) {
- return Random::next( bits );
+int SecureRandom::next( int numBits ) {
+
+ if( numBits < 0 ) {
+ numBits = 0;
+ } else if(numBits > 32 ) {
+ numBits = 32;
+ }
+
+ int bytes = ( numBits + 7 ) / 8;
+ unsigned char* next = new unsigned char[bytes];
+ unsigned int ret = 0;
+
+ this->nextBytes( next, bytes );
+ for( int i = 0; i < bytes; i++ ) {
+ ret = ( next[i] & 0xFF ) | ( ret << 8 );
+ }
+
+ ret = ret >> ( ( bytes * 8 ) - numBits );
+
+ delete [] next;
+
+ return ret;
}
Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/security/SecureRandom.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/security/SecureRandom.h?rev=951550&r1=951549&r2=951550&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/security/SecureRandom.h (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/security/SecureRandom.h Fri Jun 4 20:12:59 2010
@@ -21,6 +21,9 @@
#include <decaf/util/Config.h>
#include <decaf/util/Random.h>
+#include <decaf/security/SecureRandomSpi.h>
+
+#include <memory>
namespace decaf {
namespace security {
@@ -31,6 +34,8 @@ namespace security {
class DECAF_API SecureRandom : public decaf::util::Random {
private:
+ std::auto_ptr<SecureRandomSpi> secureRandom;
+
public:
/**
@@ -55,6 +60,23 @@ namespace security {
*/
SecureRandom( const std::vector<unsigned char>& seed );
+ /**
+ * Creates a new instance of a secure random number generator that implements the
+ * default random number algorithm.
+ *
+ * The SecureRandom instance created by this constructor is seeded using the passed
+ * byte array.
+ *
+ * @param seed
+ * The seed bytes to use to seed this secure random number generator.
+ * @param size
+ * The number of bytes in the seed buffer.
+ *
+ * @throw NullPointerException if the seed buffer is NULL.
+ * @throw IllegalArgumentException if the size value is negative.
+ */
+ SecureRandom( const unsigned char* seed, int size );
+
virtual ~SecureRandom();
public: // Virtual Methods
@@ -67,6 +89,11 @@ namespace security {
/**
* {@inheritDoc}
*/
+ virtual void nextBytes( unsigned char* buf, int size );
+
+ /**
+ * {@inheritDoc}
+ */
virtual void setSeed( unsigned long long seed );
/**
Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/security/SecureRandomSpi.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/security/SecureRandomSpi.cpp?rev=951550&r1=951549&r2=951550&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/security/SecureRandomSpi.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/security/SecureRandomSpi.cpp Fri Jun 4 20:12:59 2010
@@ -21,5 +21,9 @@ using namespace decaf;
using namespace decaf::security;
////////////////////////////////////////////////////////////////////////////////
+SecureRandomSpi::SecureRandomSpi() {
+}
+
+////////////////////////////////////////////////////////////////////////////////
SecureRandomSpi::~SecureRandomSpi() {
}
Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/security/SecureRandomSpi.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/security/SecureRandomSpi.h?rev=951550&r1=951549&r2=951550&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/security/SecureRandomSpi.h (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/security/SecureRandomSpi.h Fri Jun 4 20:12:59 2010
@@ -32,6 +32,8 @@ namespace security {
class DECAF_API SecureRandomSpi {
public:
+ SecureRandomSpi();
+
virtual ~SecureRandomSpi();
/**
@@ -43,7 +45,7 @@ namespace security {
* @param size
* The size of the passed byte array.
*/
- virtual void providerSetSeed( unsigned char* seed, int size ) = 0;
+ virtual void providerSetSeed( const unsigned char* seed, int size ) = 0;
/**
* Generates the number of random bytes specified by the size parameter and write them to
Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/Random.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/Random.cpp?rev=951550&r1=951549&r2=951550&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/Random.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/Random.cpp Fri Jun 4 20:12:59 2010
@@ -19,9 +19,13 @@
#include <decaf/lang/System.h>
+#include <decaf/lang/exceptions/NullPointerException.h>
+#include <decaf/lang/exceptions/IllegalArgumentException.h>
+
using namespace decaf;
using namespace decaf::util;
using namespace decaf::lang;
+using namespace decaf::lang::exceptions;
unsigned long long Random::multiplier = 0x5deece66dLL;
@@ -42,9 +46,29 @@ bool Random::nextBoolean() {
////////////////////////////////////////////////////////////////////////////////
void Random::nextBytes( std::vector<unsigned char>& buf ) {
+
+ try{
+ this->nextBytes( &buf[0], (int)buf.size() );
+ }
+ DECAF_CATCH_RETHROW( NullPointerException )
+ DECAF_CATCH_RETHROW( IllegalArgumentException )
+ DECAF_CATCHALL_THROW( Exception )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void Random::nextBytes( unsigned char* buf, int size ) {
+
+ if( buf == NULL ) {
+ throw NullPointerException( __FILE__, __LINE__, "Buffer passed cannot be NULL." );
+ }
+
+ if( size < 0 ) {
+ throw IllegalArgumentException( __FILE__, __LINE__, "Specified buffer size was negative." );
+ }
+
int rand = 0;
- std::size_t count = 0, loop = 0;
- while( count < buf.size() ) {
+ int count = 0, loop = 0;
+ while( count < size ) {
if( loop == 0 ) {
rand = nextInt();
loop = 3;
@@ -99,7 +123,7 @@ int Random::nextInt() {
}
////////////////////////////////////////////////////////////////////////////////
-int Random::nextInt( int n ) throw( exceptions::IllegalArgumentException ) {
+int Random::nextInt( int n ) {
if( n > 0 ) {
Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/Random.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/Random.h?rev=951550&r1=951549&r2=951550&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/Random.h (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/Random.h Fri Jun 4 20:12:59 2010
@@ -135,12 +135,14 @@ namespace util{
* is uniformly distributed between 0 (inclusively) and the value
* of <code>n</code> (exclusively).
*
- * @return int
- * @param n int
+ * @param n
+ * The int value that defines the max value of the return.
*
- * @throws IllegalArgumentException
+ * @return the next pseudo random int value.
+ *
+ * @throws IllegalArgumentException if n is less than or equal to zero.
*/
- int nextInt( int n ) throw( lang::exceptions::IllegalArgumentException );
+ int nextInt( int n );
/**
* Generates a uniformly distributed 64-bit <code>int</code> value
@@ -167,6 +169,19 @@ namespace util{
virtual void nextBytes( std::vector<unsigned char>& buf );
/**
+ * Modifies the byte array by a random sequence of bytes generated
+ * by this random number generator.
+ *
+ * @param buf non-null array to contain the new random bytes
+ *
+ * @see #next
+ *
+ * @throw NullPointerException if buff is NULL
+ * @throw IllegalArgumentException if size is negative
+ */
+ virtual void nextBytes( unsigned char* buf, int size );
+
+ /**
* Modifies the seed using linear congruential formula presented
* in <i>The Art of Computer Programming, Volume 2</i>, Section
* 3.2.1.