Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6615299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:26:42+00:00 2026-05-25T20:26:42+00:00

For unit tests of a cryptographic utility, I would like to be able to

  • 0

For unit tests of a cryptographic utility, I would like to be able to force OpenSSL’s cryptographic random number generator (both RAND_bytes and RAND_pseudo_bytes) to return predictable, repeatable byte sequences, so that various ciphertexts are in turn predictable and can be baked into test vectors. (All other key material is under my control.)

I know this totally defeats security. This will only be used for unit tests.

I cannot simply call RAND_seed with a fixed seed before each test, because (it appears) the RNG automatically seeds itself from /dev/urandom whether I want it to or not, and anyway RAND_seed doesn’t reset the RNG, it only adds the seed to the entropy pool.

Is there any way to do this? (In extremis, it looks like I could write my own PRNG engine, but I’d like to think there’s a simpler option.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:26:42+00:00Added an answer on May 25, 2026 at 8:26 pm

    You can force the FIPS ANSI X9.31 RNG into a test mode at runtime, but not the SSLeay RNG (the default). If you recompile OpenSSL with -DPREDICT, the default RNG will output a predictable sequence of numbers, but that’s not very convenient.

    The RAND_pseudo_bytes function generates a predictable series of numbers, meaning it does not add entropy to itself automatically like RAND_bytes. But like you noticed it’s only possible to add entropy to the seed, not provide the seed explicitly, so between runs of the program you’ll get different numbers. Also not helpful.

    But writing your own predictable RNG engine is not difficult. In fact, I’ll take you through it by making a rand engine with stdlib’s rand() at its core:

    #include <cstdio>
    #include <cstdlib>
    #include <cassert>
    #include <openssl/rand.h>
    
    // These don't need to do anything if you don't have anything for them to do.
    static void stdlib_rand_cleanup() {}
    static void stdlib_rand_add(const void *buf, int num, double add_entropy) {}
    static int stdlib_rand_status() { return 1; }
    
    // Seed the RNG.  srand() takes an unsigned int, so we just use the first
    // sizeof(unsigned int) bytes in the buffer to seed the RNG.
    static void stdlib_rand_seed(const void *buf, int num)
    {
            assert(num >= sizeof(unsigned int));
            srand( *((unsigned int *) buf) );
    }
    
    // Fill the buffer with random bytes.  For each byte in the buffer, we generate
    // a random number and clamp it to the range of a byte, 0-255.
    static int stdlib_rand_bytes(unsigned char *buf, int num)
    {
            for( int index = 0; index < num; ++index )
            {
                    buf[index] = rand() % 256;
            }
            return 1;
    }
    
    // Create the table that will link OpenSSL's rand API to our functions.
    RAND_METHOD stdlib_rand_meth = {
            stdlib_rand_seed,
            stdlib_rand_bytes,
            stdlib_rand_cleanup,
            stdlib_rand_add,
            stdlib_rand_bytes,
            stdlib_rand_status
    };
    
    // This is a public-scope accessor method for our table.
    RAND_METHOD *RAND_stdlib() { return &stdlib_rand_meth; }
    
    int main()
    {
            // If we're in test mode, tell OpenSSL to use our special RNG.  If we
            // don't call this function, OpenSSL uses the SSLeay RNG.
            int test_mode = 1;
            if( test_mode )
            {
                    RAND_set_rand_method(RAND_stdlib());
            }
    
            unsigned int seed = 0x00beef00;
            unsigned int rnum[5];
    
            RAND_seed(&seed, sizeof(seed));
            RAND_bytes((unsigned char *)&rnum[0], sizeof(rnum));
            printf("%u %u %u %u %u\n", rnum[0], rnum[1], rnum[2], rnum[3], rnum[4]);
    
            return 0;
    }
    

    Every time you run this program, it seeds srand() with the same number and therefore gives you the same sequence of random numbers every time.

    corruptor:scratch indiv$ g++ rand.cpp -o r -lcrypto -g
    corruptor:scratch indiv$ ./r
    1547399009 981369121 2368920148 925292993 788088604
    corruptor:scratch indiv$ ./r
    1547399009 981369121 2368920148 925292993 788088604
    corruptor:scratch indiv$ 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my Mac Cocoa unit tests, I would like to output some files as
For unit tests, demonstrations and Hibernate tasks I would like to use a small
For unit tests I would like to mimic different commandline arguments. How do I
I'm writing unit tests with NUnit and the TestDriven.NET plugin. I'd like to provide
I like the unit tests naming convention recommended by Roy Osherove where unit tests
I'm writing unit tests using nose , and I'd like to check whether a
I want that my unit tests to cover my POCO's. How should I test
I have been writing unit tests using NUnit and Moq with my Silverlight code
We have some unit tests running against a SQL server 2000 database using the
I am writing unit tests with C#, NUnit and Rhino Mocks. Here are the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.