NTRU_IoT/infidel-code/EES593/URG_encrypt.c

140 lines
4.1 KiB
C
Raw Normal View History

2022-02-01 11:45:47 -05:00
#include <stdio.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include "ntru_crypto.h"
#include "ntru_crypto_drbg.h"
#include "test_common.h"
#include <time.h>
uint8_t new_rand(uint8_t *out, uint32_t num_bytes)
{
int rng;
FILE *fpointer;
fpointer = fopen("/dev/random", "rb");
fread(&rng, sizeof(int), 1, fpointer);
*out = rng;
fclose(fpointer);
return 0;
}
uint32_t get_rand(uint8_t *out, uint32_t num_bytes)
{
int rng = 50;
int urnd = open("/dev/random", O_RDONLY);
read(urnd, &rng, sizeof(int));
*out = urnd;
close(urnd);
return 0;
}
double main(char user_input[], char *i )
{
uint8_t public_key[821]; /* sized for EES401EP2 */
uint16_t public_key_len; /* no. of octets in public key */
uint8_t private_key[891]; /* sized for EES401EP2 */
uint16_t private_key_len; /* no. of octets in private key */
uint16_t expected_private_key_len;
uint16_t expected_encoded_public_key_len;
uint8_t encoded_public_key[855]; /* sized for EES401EP2 */
uint16_t encoded_public_key_len; /* no. of octets in encoded public key */
uint8_t ciphertext[816]; /* sized fof EES401EP2 */
uint16_t ciphertext_len; /* no. of octets in ciphertext */
uint8_t plaintext[86]; /* sized fof EES401EP2 */
uint16_t plaintext_len; /* no. of octets in ciphertext */
char *ret_str = ciphertext;
uint8_t *next = NULL; /* points to next cert field to parse */
uint32_t next_len; /* no. of octets it next */
DRBG_HANDLE drbg; /* handle for instantiated DRBG */
uint32_t rc; /* return code */
bool error = FALSE; /* records if error occurred */
FILE *Handle=NULL; /* File Handle for writing NTRU key to file */
char *filename[33];
char **ptr = filename;
char buffer[821];
char *c;
char *f_name = ".cipher/ees593/cipher_";
// char *f_name = "./cipher/cipher_EES593_";
char *f_ext = ".dat";
char f_spec[strlen(f_name)+strlen(f_ext)+5];
int r;
double cpu_time_used;
clock_t time_s, time_e;
NTRU_ENCRYPT_PARAM_SET_ID param_set_id;
param_set_id = PARAM_SET_IDS[14]; /* 0 : 401; 1 : 449; 14 : 593; */
struct stat st = {0}; /* Dir Handler */
FILE *f = fopen("keys/key-593.pub", "rb");
r = fread(buffer, 1, 821, f);
fclose(f);
if (stat(".cipher/ees593", &st) == -1) {
mkdir(".cipher/ees593", 0700);
printf("Cipher directory created...\n");
}
// rc = ntru_crypto_drbg_external_instantiate(&new_rand, &drbg); /* urandom random gen */
fprintf(stderr, "Testing parameter set \t: %s\n", ntru_encrypt_get_param_set_name(param_set_id));
rc = ntru_crypto_drbg_external_instantiate(
(RANDOM_BYTES_FN) &randombytes, &drbg);
if (rc != DRBG_OK)
{
printf("Error 1");
goto error;
}
rc = ntru_crypto_ntru_encrypt(drbg, r, buffer, 86, user_input, &ciphertext_len, NULL);
if (rc != DRBG_OK)
{
printf("Error 1");
goto error;
}
time_s = clock();
rc = ntru_crypto_ntru_encrypt(drbg, r, buffer, 86, user_input, &ciphertext_len, ciphertext);
time_e = clock();
if (rc != DRBG_OK)
{
printf("Error 1");
goto error;
}
cpu_time_used = (float)(time_e - time_s) / CLOCKS_PER_SEC;
if (rc != DRBG_OK)
{
printf("Error 1");
goto error;
}
snprintf(f_spec, sizeof(f_spec), "%s%s%s", f_name, i, f_ext);
Handle=fopen(f_spec, "wb");
if(Handle!=NULL) {
fwrite(ciphertext, ciphertext_len, 1, Handle);
fclose(Handle);
} else {
printf("\t*******************************\n\n");
fprintf(stderr, "\tNo Dir Found ...\n\tProcess ABORTED...");
printf("\n\t*******************************\n");
return 0;
}
fprintf(stderr, "Cipher size : %d bytes\n",
ciphertext_len);
printf("------------------------------------------------\n");
fprintf(stderr, "\n");
return cpu_time_used;
error:
printf("ERROR %x\n", rc);
return 0;
}