NTRU_IoT/legacy-code/sample/URG_encrypt.c

101 lines
3.2 KiB
C
Raw Normal View History

2022-02-01 11:45:47 -05:00
#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 = "./cipher/cipher_";
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("Nino-ntru-pubkey.raw", "r");
r = fread(buffer, 1, 557, f);
//size_t bytes_read = getdelim(&c, &len, 1, f);
fclose(f);
//public_key[557] = c;
//printf("Current Plain : %s\n", user_input);
//printf("Current INDEX : %s\n", i);
//printf("%d bytes Public Key\n", r);
//printf("Size of USER INPU %d\n", sizeof(user_input));
//printf("Buffer:\n %s\n", &buffer);
rc = ntru_crypto_drbg_external_instantiate(&get_rand, &drbg);
//if (rc != DRBG_OK)
// goto error;
rc = ntru_crypto_ntru_encrypt(drbg, public_key_len, buffer, 16, user_input, &ciphertext_len, NULL);
//if (rc != DRBG_OK)
// goto error;
time_s = clock();
rc = ntru_crypto_ntru_encrypt(drbg, public_key_len, buffer, 16, user_input, &ciphertext_len, ciphertext);
time_e = clock();
cpu_time_used = (float)(time_e - time_s) / CLOCKS_PER_SEC;
if (rc != DRBG_OK)
goto error;
//printf("your cipher: %s", ciphertext);
//char *rtnCip = ciphertext;
//for (int i = 0; i < 32; i++)
//{
// printf("Fucken i : %d\n", i);
// f_fin = *(filename + i);
// Handle=fopen(f_fin, "wb");
// printf("File detected %s\n", f_fin);
// //printf("File detected %s\n", ptr[i]);
// fwrite(ciphertext, sizeof(ciphertext), 1, Handle);
// fclose(Handle);
//}
snprintf(f_spec, sizeof(f_spec), "%s%s%s", f_name, i, f_ext);
//printf("CURRENT file %s\n", f_spec);
Handle=fopen(f_spec, "wb");
//printf("Entered Cipher %s\n", ciphertext);
fwrite(ciphertext, sizeof(ciphertext), 1, Handle);
fclose(Handle);
return cpu_time_used;
error:
printf("ERROR %x\n", rc);
return 0;
}