The following demonstrates how to use C language and the C wrapper for retrieving random data from an AlphaRNG device when used with AlphaRNG Software API.
A comprehensive sample_c.c
working example can be found as part of the AlphaRNG Software Kit that can be downloaded at
this address
A C sample code for retrieving entropy bytes from an AlphaRNG device using RSA2048, AES-256-GCM and HMAC-SHA256 algorithms:
#include <AlphaRngApiCWrapper.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
/*
Create a new AlphaRngApi instance for using with RSA-2048, AES-256-GCM and HMAC-SHA256 algorithms for strong security.
Notes: AES-GCM cipher algorithm also ensures the authenticity of the original data before encryption.
HMAC-SHA256 algorithm is used in this context to authenticate the encrypted data.
RSA-2048 algorithm is used in this context to create initial session of the connection.
Connect to an AlphaRNG device, retrieve 16 bytes of entropy and print those on standard output.
*/
int main(void) {
struct alrng_context *ctxt;
unsigned char entropy_buffer[16];
int ret_value;
int i;
ctxt = alrng_create_ctxt(rsa_2048_key, hmac_sha_160, aes_256_gcm, "");
if (NULL == ctxt) {
fprintf(stderr, "Could not create context\n");
return -1;
}
/* Connect to the first AlphaRNG device found */
ret_value = alrng_connect(ctxt, 0);
if (ret_value) {
alrng_destroy_ctxt(ctxt);
fprintf(stderr, "Could not connect to device\n");
return ret_value;
}
/* Retrieve entropy */
ret_value = alrng_get_entropy(ctxt, entropy_buffer, sizeof(entropy_buffer));
if (ret_value) {
fprintf(stderr, "Could not retrieve entropy from device\n");
alrng_destroy_ctxt(ctxt);
return ret_value;
}
/* Print random bytes */
for (i = 0; i < (int)sizeof(entropy_buffer); i++) {
printf("entropy[%d]: %d\n", i, (int)entropy_buffer[i]);
}
/* Close any active connection and destroy allocated context */
alrng_destroy_ctxt(ctxt);
return 0;
}
The output may look similar to this:
entropy[0]: 121
entropy[1]: 3
entropy[2]: 60
entropy[3]: 123
entropy[4]: 119
entropy[5]: 199
entropy[6]: 220
entropy[7]: 173
entropy[8]: 246
entropy[9]: 235
entropy[10]: 93
entropy[11]: 171
entropy[12]: 152
entropy[13]: 67
entropy[14]: 57
entropy[15]: 126
A C sample code that retrieves entropy bytes from an AlphaRNG device and loads those to a file:
#include <AlphaRngApiCWrapper.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
/*
Create a new AlphaRngApi instance using default security context.
Connect to an AlphaRNG device, retrieve 256 bytes of entropy and save the bytes into 'random.bin' file.
*/
int main(void) {
struct alrng_context *ctxt;
int ret_value;
char error_message[128];
ctxt = alrng_create_default_ctxt();
if (NULL == ctxt) {
fprintf(stderr, "Could not create context\n");
return -1;
}
/* Connect to the first AlphaRNG device found */
ret_value = alrng_connect(ctxt, 0);
if (ret_value) {
alrng_destroy_ctxt(ctxt);
fprintf(stderr, "Could not connect to device\n");
return ret_value;
}
/* Retrieve entropy and save the retrieved bytes to a file */
ret_value = alrng_entropy_to_file(ctxt, "random.bin", 256);
if (ret_value) {
/* Retrieve the error message */
ret_value = alrng_get_last_error(ctxt, error_message, sizeof(error_message));
if (!ret_value) {
fprintf(stderr, "Error: %s\n", error_message);
}
alrng_destroy_ctxt(ctxt);
return ret_value;
}
/* Close any active connection and destroy allocated context */
alrng_destroy_ctxt(ctxt);
return 0;
}
A C sample code that establishes a connection to an AlphaRNG device using a custom and unique RSA 2048 public key supplied by the manufacturer:
#include <AlphaRngApiCWrapper.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
/*
Create a new AlphaRngApi instance for using with RSA-2048, AES-256-GCM and HMAC-SHA256 algorithms.
Utilize the custom and unique RSA 2048 public key that was supplied by the manufacturer.
The unique public RSA 2048-bit key can be exclusively used with one AlphaRNG device.
Connect to an AlphaRNG device, retrieve the device identifier (serial number) and print it on standard output.
*/
int main(void) {
struct alrng_context *ctxt;
int ret_value;
char device_id[16];
char error_message[128];
ctxt = alrng_create_ctxt(rsa_2048_key, hmac_sha_160, aes_256_gcm, "public_key-123456789012345.pem");
if (NULL == ctxt) {
fprintf(stderr, "Could not create context\n");
return -1;
}
/* Connect to the first AlphaRNG device found */
ret_value = alrng_connect(ctxt, 0);
if (ret_value) {
alrng_destroy_ctxt(ctxt);
fprintf(stderr, "Could not connect to device\n");
return ret_value;
}
/* Retrieve device id */
ret_value = alrng_retrieve_device_id(ctxt, device_id, sizeof(device_id));
if (ret_value) {
/* Retrieve the error message */
ret_value = alrng_get_last_error(ctxt, error_message, sizeof(error_message));
if (!ret_value) {
fprintf(stderr, "Error: %s\n", error_message);
}
alrng_destroy_ctxt(ctxt);
return ret_value;
}
printf("device id: %s\n", device_id);
/* Close any active connection and destroy allocated context */
alrng_destroy_ctxt(ctxt);
return 0;
}