Hello Freedom

This commit is contained in:
infidel
2022-02-01 23:45:47 +07:00
commit 7009cb27c4
964 changed files with 513364 additions and 0 deletions

View File

@@ -0,0 +1,174 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/stat.h>
#include "ntru_crypto.h"
#include "ntru_crypto_drbg.h"
#include "test_common.h"
int main()
{
uint8_t *public_key;
uint8_t *private_key;
// uint8_t *message;
char message[87];
uint8_t *ciphertext;
uint8_t *plaintext;
uint16_t max_msg_len; uint16_t public_key_len; /* no. of octets in public key */
uint16_t private_key_len; /* no. of octets in private key */
uint16_t ciphertext_len; /* no. of octets in ciphertext */
uint16_t plaintext_len; /* no. of octets in plaintext */
DRBG_HANDLE drbg; /* handle for instantiated DRBG */
uint32_t rc; /* return code */
clock_t clk;
FILE *Handle=NULL; /* File Handler */
struct stat st = {0}; /* Dir Handler */
NTRU_ENCRYPT_PARAM_SET_ID param_set_id;
printf("------------------------------------------------\n");
printf("Enter Message \t\t: ");
// message = "Hello Bastard...";
fgets(message, 86, stdin);
printf("Your message is \t: %s\n",message);
param_set_id = PARAM_SET_IDS[14]; /* 0 : 401; 1 : 449; 14 : 593; */
fprintf(stderr, "Testing parameter set \t: %s\n", ntru_encrypt_get_param_set_name(param_set_id));
printf("------------------------------------------------\n");
fflush (stderr);
rc = ntru_crypto_drbg_external_instantiate(
(RANDOM_BYTES_FN) &randombytes, &drbg);
if (rc != DRBG_OK)
{
fprintf(stderr,"\tError: An error occurred instantiating the DRBG\n");
}
rc = ntru_crypto_ntru_encrypt_keygen(drbg, param_set_id, &public_key_len,
NULL, &private_key_len, NULL);
if (rc != NTRU_OK)
{
ntru_crypto_drbg_uninstantiate(drbg);
fprintf(stderr,"\tError: An error occurred getting the key lengths\n");
}
public_key = (uint8_t *)malloc(public_key_len * sizeof(uint8_t));
private_key = (uint8_t *)malloc(private_key_len * sizeof(uint8_t));
clk = clock();
rc = ntru_crypto_ntru_encrypt_keygen(drbg, param_set_id, &public_key_len,
public_key,
&private_key_len,
private_key);
clk = clock() - clk;
if (stat("./keys", &st) == -1) {
mkdir("./keys", 0700);
printf("Key directory created...\n");
}
Handle=fopen("keys/key-593.pub", "wb");
if(Handle!=NULL) {
printf("-> Writing Pub Key...\n");
fwrite(public_key, public_key_len, 1, Handle);
printf("-> Pub Key written...\n");
fclose(Handle);
}
Handle=fopen("keys/key-593.priv", "wb");
if(Handle!=NULL) {
printf("-> Writing Priv Key...\n");
fwrite(private_key, private_key_len, 1, Handle);
printf("-> Private Key written...\n");
fclose(Handle);
}
if (rc != NTRU_OK)
{
ntru_crypto_drbg_uninstantiate(drbg);
free(public_key);
free(private_key);
fprintf(stderr,"\tError: An error occurred during key generation\n");
}
rc = ntru_crypto_ntru_encrypt(drbg, public_key_len, public_key, 0, NULL,
&ciphertext_len, NULL);
if (rc != NTRU_OK)
{
fprintf(stderr,"\tError: Bad public key");
}
rc = ntru_crypto_ntru_decrypt(private_key_len, private_key, 0, NULL,
&max_msg_len, NULL);
if (rc != NTRU_OK)
{
fprintf(stderr,"\tError: Bad private key");
}
//message = (uint8_t *) malloc(max_msg_len * sizeof(uint8_t));
printf("------------------------------------------------\n");
printf("Max message block \t: %d\n", max_msg_len * sizeof(uint8_t));
ciphertext = (uint8_t *) malloc(ciphertext_len * sizeof(uint8_t));
plaintext = (uint8_t *) malloc(max_msg_len * sizeof(uint8_t));
plaintext_len = max_msg_len;
//randombytes(message, max_msg_len);
//randombytes(ciphertext, ciphertext_len);
//randombytes(plaintext, plaintext_len);
clk = clock();
rc = ntru_crypto_ntru_encrypt(drbg, public_key_len, public_key,
max_msg_len, message, &ciphertext_len, ciphertext);
clk = clock() - clk;
if (rc != NTRU_OK){
fprintf(stderr, "\tError: Encryption error %x\n", rc);
}
//printf("Cipher %s\n", ciphertext);
clk = clock();
rc = ntru_crypto_ntru_decrypt(private_key_len, private_key,
ciphertext_len, ciphertext,
&plaintext_len, plaintext);
clk = clock() - clk;
if (rc != NTRU_OK)
{
fprintf(stderr, "\tError: Decryption error %x\n", rc);
}
printf("Decryption result \t: %s\n", plaintext);
if(plaintext_len != max_msg_len || memcmp(plaintext,message,max_msg_len))
{
fprintf(stderr,
"\tError: Decryption result does not match original plaintext\n");
}
ntru_crypto_drbg_uninstantiate(drbg);
//free(message);
//free(public_key);
//free(private_key);
//free(plaintext);
//free(ciphertext);
fprintf(stderr, "pk %d, sk %d, ct %d bytes\n",
public_key_len, private_key_len, ciphertext_len);
printf("------------------------------------------------\n");
fprintf(stderr, "\n");
}

View File

@@ -0,0 +1,78 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include "ntru_crypto.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;
}
char * main(char * user_input)
{
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[16]; /* size of AES-128 key */
uint16_t plaintext_len; /* no. of octets in plaintext */
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 buffer[891];
char *buffer2 = 0;
char *c = malloc(86);
char *d = malloc(816);
int r;
int s;
double cpu_time_used;
rc = ntru_crypto_drbg_uninstantiate(drbg);
FILE *f=fopen("keys/key-593.priv", "rb");
r = fread(buffer, 1,891, f);
fclose(f);
rc = ntru_crypto_ntru_decrypt(r, buffer, 816, user_input, &plaintext_len,
NULL);
if (rc != NTRU_OK)
printf("ERROR 1\n");
rc = ntru_crypto_ntru_decrypt(r, buffer, 816, user_input, &plaintext_len,
plaintext);
if (rc != NTRU_OK)
printf("ERROR 2\n");
// printf("C Log DEC : your plain: %s\n", plaintext);
// printf("C Log DEC : your plain LEN: %d\n", plaintext_len);
// printf("your time spent: %lf\n", cpu_time_used);
// snprintf(c, sizeof(c), "%s", plaintext);
strcpy(c, plaintext);
return c;
error:
printf("PROBLEM BUDDY %d\n", rc);
exit(EXIT_FAILURE);
return 0;
}

View File

@@ -0,0 +1,139 @@
#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;
}

View File

@@ -0,0 +1,241 @@
#include <stdio.h>
#include <time.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"
//typedef uint32_t (*urnd)(uint8_t *out, uint32_t num_bytes);
static uint8_t const aes_key[] = "Decraaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
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;
}
int main(void) {
//uint8_t public_key[557];
double cpu_time_used;
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]; /* size of AES-128 key */
uint16_t plaintext_len; /* no. of octets in plaintext */
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 buffer[891];
clock_t time_s, time_e;
rc = ntru_crypto_drbg_external_instantiate(&get_rand, &drbg);
if (rc != DRBG_OK)
{
printf("ERROR 1");
goto error;
}
rc = ntru_crypto_ntru_encrypt_keygen(drbg, NTRU_EES593EP1, &public_key_len, NULL, &private_key_len, NULL);
if (rc != NTRU_OK)
{
printf("ERROR 2");
error = TRUE;
}
expected_private_key_len=private_key_len;
time_s = clock();
rc = ntru_crypto_ntru_encrypt_keygen(drbg, NTRU_EES593EP1, &public_key_len, public_key, &private_key_len, private_key);
time_e = clock();
if (rc != NTRU_OK)
{
printf("ERROR 3");
error = TRUE;
}
if (expected_private_key_len!=private_key_len)
{
fprintf(stderr, "PRivate key length is different\n");
error = TRUE;
}
printf("Key sucessfully generated. \n");
rc = ntru_crypto_drbg_uninstantiate(drbg);
if ((rc != DRBG_OK) || error)
{
printf("ERROR 4");
error = TRUE;
}
printf("KEY DRBG Success. \n");
Handle=fopen("EES593/EES593-ntru-priv.raw","wb");
if (Handle!=NULL){
printf("Writing Pirvate key\n");
fwrite(private_key, private_key_len, 1, Handle);
fclose(Handle);
}
Handle=fopen("EES593/EES593-ntru-pub.raw","wb");
if(Handle!=NULL){
printf("Writing Public key\n");
printf("Public Key : \n %s\n", public_key);
fwrite(public_key, public_key_len, 1, Handle);
fclose(Handle);
}
rc = ntru_crypto_ntru_encrypt_publicKey2SubjectPublicKeyInfo(public_key_len, public_key, &encoded_public_key_len, NULL);
if (rc != NTRU_OK)
goto error;
printf("DER encoded, sized requierd %d . \n", encoded_public_key_len);
expected_encoded_public_key_len = encoded_public_key_len;
rc = ntru_crypto_ntru_encrypt_publicKey2SubjectPublicKeyInfo(public_key_len, public_key, &encoded_public_key_len, encoded_public_key);
if (expected_encoded_public_key_len!=encoded_public_key_len)
{
fprintf(stderr, "Different encoded pub key detected\n");
error = TRUE;
}
next = encoded_public_key;
next_len = encoded_public_key_len;
rc = ntru_crypto_ntru_encrypt_subjectPublicKeyInfo2PublicKey(next, &public_key_len, NULL, &next, &next_len);
if (rc != NTRU_OK)
{
printf("ERROR 5");
goto error;
}
printf("Pub key buffer must %d\n", public_key_len);
rc = ntru_crypto_ntru_encrypt_subjectPublicKeyInfo2PublicKey(next, &public_key_len, public_key, &next, &next_len);
if (rc != NTRU_OK)
{
printf("ERROR 6");
goto error;
}
printf("Pub DER Encoding success\n");
printf("Encryption Phase \n");
rc = ntru_crypto_drbg_external_instantiate(&get_rand, &drbg);
if (rc != DRBG_OK)
{
printf("ERROR 7");
goto error;
}
printf("Sucess encrypt\n");
rc = ntru_crypto_ntru_encrypt(drbg, public_key_len, public_key, sizeof(aes_key), aes_key, &ciphertext_len, NULL);
if (rc != NTRU_OK)
{
printf("ERROR 7");
goto error;
}
printf("String to be encrypted: %s\n", aes_key);
rc = ntru_crypto_ntru_encrypt(drbg, public_key_len, public_key, sizeof(aes_key), aes_key, &ciphertext_len, ciphertext);
if (rc != NTRU_OK)
{
printf("ERROR 8");
goto error;
}
//printf("Ciphertext : %s\n", ciphertext);
rc = ntru_crypto_drbg_uninstantiate(drbg);
printf("Done BLyat!!!!\n");
if (rc != NTRU_OK)
{
printf("ERROR 9");
goto error;
}
Handle=fopen("EES593/EES593-ntru-priv.raw","rb");
if(Handle!=NULL){
printf("Read Public key\n");
fread(buffer, 891, 1, Handle);
fclose(Handle);
}
rc = ntru_crypto_ntru_decrypt(private_key_len, buffer, ciphertext_len,
ciphertext, &plaintext_len, NULL);
if (rc != NTRU_OK)
/* An error occurred requesting the buffer size needed. */
goto error;
printf("Maximum plaintext buffer size required: %d octets.\n",
plaintext_len);
/* Now we could allocate a buffer of length plaintext_len to hold the
* plaintext, but note that plaintext_len has the maximum plaintext
* size for the EES401EP2 parameter set. Since we know that we've
* received an encrypted AES-128 key in this example, and since we
* already have a plaintext buffer as a local variable, we'll just
* supply the length of that plaintext buffer for decryption.
*/
plaintext_len = sizeof(plaintext);
rc = ntru_crypto_ntru_decrypt(private_key_len, buffer, ciphertext_len,
ciphertext, &plaintext_len, plaintext);
if (rc != NTRU_OK)
{
fprintf(stderr,"Error: An error occurred decrypting the AES-128 key.\n");
return 1;
}
printf("AES-128 key decrypted successfully.\n");
printf("Decoded plaintext length: %d octets\n",plaintext_len);
if(plaintext_len!=sizeof(aes_key))
{
fprintf(stderr,"Error: Decrypted length does not match original plaintext length\n");
return 1;
}
if(memcmp(plaintext,aes_key,sizeof(aes_key)))
{
fprintf(stderr,"Error: Decrypted plaintext does not match original plaintext\n");
return 1;
}
Handle=fopen("sample-decoded-plaintext.bin","wb");
if(Handle!=NULL)
{
printf("Writing decoded plaintext to decoded-plaintext.bin\n");
printf("Plain text : %s\n", plaintext);
fwrite(plaintext,plaintext_len,1,Handle);
fclose(Handle);
}
cpu_time_used = (float)(time_e - time_s) / CLOCKS_PER_SEC;
printf("Time Keygen NTRU : %lf\n", cpu_time_used);
/* And now the plaintext buffer holds the decrypted AES-128 key. */
printf("Sample code completed successfully.\n");
error:
printf("ERROR %x\n", rc);
return 1;
}