The following examples show how to use C++11 to retrieve random data from an AlphaRNG device with the AlphaRNG Software API.
Terms used in this document:
RSA1024, RSA2048, RSA3072, RSA4096 - asymmetric ciphers used for AlphaRNG device authentication and for establishing a connection session with the device. The public key is provided in the AlphaRNG Software API source code and the corresponding private key is embedded in the device. RSA3072 and RSA4096 require an AlphaRNG V1.2 or later device and are selected with RsaKeySize::rsa3072 and RsaKeySize::rsa4096.
AES-256-GCM, AES-128-GCM - symmetric ciphers used for securing data transmissions within a connection session.
HMAC-SHA256, HMAC-SHA160, HMAC-MD5 - authentication algorithms used for additional data authentication within a connection session.
Factory supplied RSA public key - a unique public RSA key that can be used exclusively with one AlphaRNG device, shipped with the device as a PEM file. Every device comes with a 2048-bit key, and AlphaRNG V1.2 and later devices also come with a 3072-bit key and a 4096-bit key. Its corresponding private key is securely embedded in the device during manufacturing.
Retrieving random bytes using RSA-2048, AES-256-GCM and HMAC-SHA256
#include <AlphaRngApi.h>
#include <array>
using namespace std;
using namespace alpharng;
/**
* @return 0 if ran successfully
*/
int main() {
std::array<uint8_t, 10> rnd_array { };
// Create a new AlphaRngApi instance for using with RSA2048, AES-256-GCM and HMAC-SHA256 algorithms for strong security.
// Note: AES-GCM algorithm also ensures the authenticity of the original data before encryption.
// HMAC-SHA256 algorithm is used here to authenticate the data after encryption.
AlphaRngApi rng { AlphaRngConfig { MacType::hmacSha256, RsaKeySize::rsa2048, KeySize::k256, "" } };
// Connecting to the first AlphaRNG device found
if (!rng.connect(0)) {
cerr << rng.get_last_error() << endl;
return -1;
}
cout << endl << "AlphaRNG device open successfully" << endl << endl;
cout << "*** Retrieving " << rnd_array.size() << " random bytes ***" << endl;
// Retrieve random numbers from device
if (!rng.get_entropy(rnd_array.data(), sizeof(rnd_array))) {
cerr << rng.get_last_error() << endl;
return -1;
}
for (auto el : rnd_array) {
cout << "random byte : " << (int) el << endl;
}
return 0;
}
Retrieving random bytes using a factory-supplied RSA-2048 public key
For maximum security, each AlphaRNG device is shipped with a unique public RSA 2048-bit key that can be used exclusively with that device. The following example shows how to authenticate and connect to an AlphaRNG device using the factory-supplied RSA-2048 public key.
AlphaRNG V1.2 and later devices are also shipped with a unique 3072-bit and a unique 4096-bit public key. To use one of them, pass the name of its PEM file in the configuration and set the key size to match the key in the file: RsaKeySize::rsa3072 or RsaKeySize::rsa4096.
#include <AlphaRngApi.h>
#include <array>
using namespace std;
using namespace alpharng;
/**
* @return 0 if ran successfully
*/
int main() {
std::array<uint8_t, 10> rnd_array { };
// Create a new AlphaRngApi instance for using with RSA2048, AES-256-GCM and HMAC-SHA256 algorithms.
// Use a factory supplied unique RSA-2048 public key (as a file) to achieve maximum security.
// The supplied key can be used to uniquely identify an AlphaRNG device.
AlphaRngApi rng { AlphaRngConfig { MacType::hmacSha256, RsaKeySize::rsa2048, KeySize::k256, "public_key-123456789012345.pem" } };
// Connecting to the first AlphaRNG device found
if (!rng.connect(0)) {
cerr << rng.get_last_error() << endl;
return -1;
}
cout << endl << "AlphaRNG device open successfully" << endl << endl;
cout << "*** Retrieving " << rnd_array.size() << " random bytes ***" << endl;
// Retrieve random bytes from device
if (!rng.get_entropy(rnd_array.data(), sizeof(rnd_array))) {
cerr << rng.get_last_error() << endl;
return -1;
}
for (auto el : rnd_array) {
cout << "random byte : " << (int)el << endl;
}
return 0;
}
Retrieving random integers using RSA-2048 and AES-256-GCM without a MAC
#include <AlphaRngApi.h>
#include <array>
using namespace std;
using namespace alpharng;
/**
* @return 0 if ran successfully
*/
int main() {
std::array<int, 10> rnd_array { };
// Create a new AlphaRngApi instance for using with RSA2048 and AES-256-GCM algorithms.
// Note: To improve performance, we are not using any MAC algorithm in this example.
AlphaRngApi rng { AlphaRngConfig { MacType::None, RsaKeySize::rsa2048, KeySize::k256, "" } };
// Connecting to the first AlphaRNG device found
if (!rng.connect(0)) {
cerr << rng.get_last_error() << endl;
return -1;
}
cout << endl << "AlphaRNG device open successfully" << endl << endl;
cout << "*** Retrieving " << rnd_array.size() << " random integers ***" << endl;
// Retrieve random integers from device
if (!rng.get_entropy((unsigned char*) rnd_array.data(), sizeof(rnd_array))) {
cerr << rng.get_last_error() << endl;
return -1;
}
for (auto el : rnd_array) {
cout << "random integer : " << el << endl;
}
return 0;
}
Retrieving random data using RSA-2048 and HMAC-SHA256 without encryption
#include <AlphaRngApi.h>
#include <array>
using namespace std;
using namespace alpharng;
/**
* @return 0 if ran successfully
*/
int main() {
std::array<int, 10> rnd_array { };
// Create a new AlphaRngApi instance for using with RSA2048 and MAC-SHA256 algorithms.
// This is the case when encryption is not required and we only need to ensure
// the authenticity of the data.
// Note: To increase performance, use MAC-SHA160 or HMAC-MD5 if accepted.
AlphaRngApi rng { AlphaRngConfig { MacType::hmacSha256, RsaKeySize::rsa2048, KeySize::None, "" } };
// Connecting to the first AlphaRNG device found
if (!rng.connect(0)) {
cerr << rng.get_last_error() << endl;
return -1;
}
cout << endl << "AlphaRNG device open successfully" << endl << endl;
cout << "*** Retrieving " << rnd_array.size() << " random integers ***" << endl;
// Retrieve random integers from device
if (!rng.get_entropy((unsigned char*) rnd_array.data(), sizeof(rnd_array))) {
cerr << rng.get_last_error() << endl;
return -1;
}
for (auto el : rnd_array) {
cout << "random integer : " << el << endl;
}
return 0;
}
Retrieving random data without symmetric encryption or a MAC
#include <AlphaRngApi.h>
#include <array>
using namespace std;
using namespace alpharng;
/**
* @return 0 if ran successfully
*/
int main() {
std::array<int, 10> rnd_array { };
// Create a new AlphaRngApi instance with no symmetric encryption or MAC algorithm.
// Note: RSA algorithm (1024, 2048, 3072 or 4096-bit) is still used to authenticate the device.
AlphaRngApi rng { AlphaRngConfig { MacType::None, RsaKeySize::rsa2048, KeySize::None, "" } };
// Connecting to the first AlphaRNG device found
if (!rng.connect(0)) {
cerr << rng.get_last_error() << endl;
return -1;
}
cout << endl << "AlphaRNG device open successfully" << endl << endl;
cout << "*** Retrieving " << rnd_array.size() << " random integers ***" << endl;
// Retrieve random integers from device
if (!rng.get_entropy((unsigned char*) rnd_array.data(), sizeof(rnd_array))) {
cerr << rng.get_last_error() << endl;
return -1;
}
for (auto el : rnd_array) {
cout << "random integer : " << el << endl;
}
return 0;
}
Retrieving raw bytes from the noise sources
This example retrieves raw (unprocessed) random bytes from the two independent noise sources using the default security settings. It can be used to evaluate the quality of the noise circuits used by AlphaRNG devices.
#include <AlphaRngApi.h>
#include <array>
using namespace std;
using namespace alpharng;
/**
* @return 0 if ran successfully
*/
int main() {
std::array<uint8_t, 10> rnd_array { };
// Create a new AlphaRngApi instance using default constructor (HMAC-SHA256, RSA2048 and AES-256-GCM)
AlphaRngApi rng { };
// Connecting to the first AlphaRNG device found
if (!rng.connect(0)) {
cerr << rng.get_last_error() << endl;
return -1;
}
cout << endl << "AlphaRNG device open successfully" << endl << endl;
cout << "*** Retrieving " << rnd_array.size() << " random bytes from the noise source one ***" << endl;
// Retrieve random integers from the noise source one
if (!rng.get_noise_source_1(rnd_array.data(), sizeof(rnd_array))) {
cerr << rng.get_last_error() << endl;
return -1;
}
for (auto el : rnd_array) {
cout << "random integer : " << (int) el << endl;
}
cout << endl;
cout << "*** Retrieving " << rnd_array.size() << " random bytes from the noise source two ***" << endl;
// Retrieve random integers from the noise source two
if (!rng.get_noise_source_2(rnd_array.data(), sizeof(rnd_array))) {
cerr << rng.get_last_error() << endl;
return -1;
}
for (auto el : rnd_array) {
cout << "random integer : " << (int) el << endl;
}
return 0;
}
Retrieving real-time device information
#include <AlphaRngApi.h>
#include <array>
using namespace std;
using namespace alpharng;
/**
* @return 0 if ran successfully
*/
int main() {
// Create a new AlphaRngApi instance using default constructor (HMAC-SHA256, RSA2048 and AES-256-GCM)
AlphaRngApi rng { };
// Connecting to the first AlphaRNG device found
if (!rng.connect(0)) {
cerr << rng.get_last_error() << endl;
return -1;
}
cout << endl << "AlphaRNG device open successfully" << endl << endl;
cout << "*** Retrieving device information ***" << endl;
string id;
if (!rng.retrieve_device_id(id)) {
cerr << rng.get_last_error() << endl;
return -1;
}
cout << "Device identifier: " << id << endl;
string model;
if (!rng.retrieve_device_model(model)) {
cerr << rng.get_last_error() << endl;
return -1;
}
cout << "Device model: " << model << endl;
unsigned char major;
unsigned char minor;
rng.retrieve_device_major_version(&major);
rng.retrieve_device_minor_version(&minor);
cout << "Device version: " << (int) major << "." << (int) minor << endl;
unsigned char status;
if (!rng.retrieve_rng_status(&status)) {
cerr << rng.get_last_error() << endl;
return -1;
}
cout << "Device status: " << (status == 0 ? "healthy" : "error") << endl;
cout << "Device last error: " << rng.get_last_error() << endl;
HealthTests ht = rng.get_health_tests();
cout << "Max APT test failures per block: " << ht.get_max_apt_failures() << endl;
cout << "Max RCT test failures per block: " << ht.get_max_rct_failures() << endl;
cout << "Operations retried: " << rng.get_operation_retry_count() << endl;
return 0;
}
Running the internal health test
#include <AlphaRngApi.h>
#include <array>
using namespace std;
using namespace alpharng;
/**
* @return 0 if ran successfully
*/
int main() {
// Create a new AlphaRngApi instance using default constructor (using HMAC-SHA256, RSA2048, and AES-256-GCM)
AlphaRngApi rng { };
// Connecting to the first AlphaRNG device found
if (!rng.connect(0)) {
cerr << rng.get_last_error() << endl;
return -1;
}
cout << endl << "AlphaRNG device open successfully" << endl << endl;
cout << "*** Invoking device internal health diagnostics ***" << endl;
if (!rng.run_health_test()) {
cerr << rng.get_last_error() << endl;
return -1;
}
cout << "Device internal diagnostics ran successfully" << endl;
return 0;
}
Retrieving entropy bytes and saving them to a file
#include <AlphaRngApi.h>
#include <array>
using namespace std;
using namespace alpharng;
/**
* @return 0 if ran successfully
*/
int main() {
// Create a new AlphaRngApi instance using default constructor (using HMAC-SHA256, RSA2048, and AES-256-GCM)
AlphaRngApi rng { };
// Connecting to the first AlphaRNG device found
if (!rng.connect(0)) {
cerr << rng.get_last_error() << endl;
return -1;
}
cout << endl << "AlphaRNG device open successfully" << endl << endl;
cout << "*** Retrieving 256 bytes of entropy from AlphaRNG device and storing to entropy_bytes.bin file ***"
<< endl;
if (!rng.entropy_to_file("entropy_bytes.bin", 256)) {
cerr << rng.get_last_error() << endl;
return -1;
}
return 0;
}
Retrieving entropy bytes using the SHA-256 and SHA-512 entropy extractors
#include <AlphaRngApi.h>
#include <array>
using namespace std;
using namespace alpharng;
/**
* @return 0 if ran successfully
*/
int main() {
// A sample code for retrieving entropy bytes from an AlphaRNG device using SHA-256 and SHA-512 entropy extractors
std::array<uint8_t, 10> rnd_array{ };
// Create a new AlphaRngApi instance using default constructor (using HMAC-SHA256, RSA2048, and AES-256-GCM)
AlphaRngApi rng { };
// Connecting to the first AlphaRNG device found
if (!rng.connect(0)) {
cerr << rng.get_last_error() << endl;
return -1;
}
cout << endl << "AlphaRNG device open successfully" << endl << endl;
cout << "*** extracting " << rnd_array.size() << " bytes of entropy using SHA-256 entropy extractor ***" << endl;
// Extracting entropy bytes
if (!rng.extract_sha256_entropy(rnd_array.data(), sizeof(rnd_array))) {
cerr << rng.get_last_error() << endl;
return -1;
}
for (auto el : rnd_array) {
cout << "entropy byte : " << (int)el << endl;
}
cout << endl;
cout << "*** extracting " << rnd_array.size() << " bytes of entropy using SHA-512 entropy extractor ***" << endl;
// Extracting entropy bytes
if (!rng.extract_sha512_entropy(rnd_array.data(), sizeof(rnd_array))) {
cerr << rng.get_last_error() << endl;
return -1;
}
for (auto el : rnd_array) {
cout << "entropy byte : " << (int)el << endl;
}
return 0;
}