88 lines
2.7 KiB
C
88 lines
2.7 KiB
C
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <string.h>
|
||
|
#include <unistd.h>
|
||
|
#include <fcntl.h>
|
||
|
#include "ntru_crypto.h"
|
||
|
#include <time.h>
|
||
|
|
||
|
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[557]; /* sized for EES401EP2 */
|
||
|
uint16_t public_key_len; /* no. of octets in public key */
|
||
|
uint8_t private_key[607]; /* 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[593]; /* sized for EES401EP2 */
|
||
|
uint16_t encoded_public_key_len; /* no. of octets in encoded public key */
|
||
|
uint8_t ciphertext[552]; /* sized fof EES401EP2 */
|
||
|
char *ret_str = ciphertext;
|
||
|
uint16_t ciphertext_len; /* no. of octets in 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[557];
|
||
|
char *c;
|
||
|
char *f_name = "/tmp/cipher_EES401_";
|
||
|
char *f_ext = ".dat";
|
||
|
char *f_fin;
|
||
|
char f_spec[strlen(f_name)+strlen(f_ext)+5];
|
||
|
int r;
|
||
|
double cpu_time_used;
|
||
|
clock_t time_s, time_e;
|
||
|
|
||
|
FILE *f = fopen("EES401/EES401-ntru-pub.raw", "r");
|
||
|
r = fread(buffer, 1, 557, f);
|
||
|
fclose(f);
|
||
|
|
||
|
rc = ntru_crypto_drbg_external_instantiate(&get_rand, &drbg);
|
||
|
if (rc != DRBG_OK)
|
||
|
{
|
||
|
printf("Error 1\n");
|
||
|
goto error;
|
||
|
}
|
||
|
rc = ntru_crypto_ntru_encrypt(drbg, r, buffer, 32, user_input, &ciphertext_len, NULL);
|
||
|
if (rc != DRBG_OK)
|
||
|
{
|
||
|
printf("Error 2\n");
|
||
|
goto error;
|
||
|
}
|
||
|
time_s = clock();
|
||
|
rc = ntru_crypto_ntru_encrypt(drbg, r, buffer, 32, user_input, &ciphertext_len, ciphertext);
|
||
|
|
||
|
time_e = clock();
|
||
|
cpu_time_used = (float)(time_e - time_s) / CLOCKS_PER_SEC;
|
||
|
if (rc != DRBG_OK)
|
||
|
{
|
||
|
printf("Error 3\n");
|
||
|
goto error;
|
||
|
}
|
||
|
|
||
|
snprintf(f_spec, sizeof(f_spec), "%s%s%s", f_name, i, f_ext);
|
||
|
Handle=fopen(f_spec, "wb");
|
||
|
fwrite(ciphertext, sizeof(ciphertext), 1, Handle);
|
||
|
fclose(Handle);
|
||
|
return cpu_time_used;
|
||
|
|
||
|
error:
|
||
|
printf("ERROR %x\n", rc);
|
||
|
return 0;
|
||
|
}
|
||
|
|