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

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,140 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.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;
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;
NTRU_ENCRYPT_PARAM_SET_ID param_set_id;
message = "AAAAAAAAAAAAAAAAAAAA";
printf("Mess %s\n",message);
param_set_id = PARAM_SET_IDS[0];
fprintf(stderr, "Testing parameter set %s... \n", ntru_encrypt_get_param_set_name(param_set_id));
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 (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("Max Len : %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);
printf("Current Message: %s\n", message);
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("Plain %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",
public_key_len, private_key_len-public_key_len, ciphertext_len);
fprintf(stderr, "\n");
}

View File

@@ -0,0 +1,71 @@
#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[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];
char *buffer2 = 0;
char *c = malloc(86);
char *d = malloc(816);
int r;
int s;
double cpu_time_used;
Handle=fopen("EES593/EES593-ntru-priv.raw", "rb");
r = fread(buffer, 1,891, Handle);
fclose(Handle);
printf("C Log DEC: %d bytes Private Key\n", r);
printf("C Log DEC: user_input Len %d\n", sizeof(user_input));
strcpy(d, user_input);
rc = ntru_crypto_ntru_decrypt(r, buffer, 816,user_input, &plaintext_len, NULL);
rc = ntru_crypto_ntru_decrypt(r, buffer, 816,user_input, &plaintext_len, plaintext);
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;
}

Binary file not shown.

View File

@@ -0,0 +1,94 @@
#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[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 plainrtext[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 = "/tmp/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;
FILE *f = fopen("EES593/EES593-ntru-pub.raw", "rb");
r = fread(buffer, 1, 821, f);
fclose(f);
rc = ntru_crypto_drbg_external_instantiate(&get_rand, &drbg);
if (rc != DRBG_OK)
{
printf("Error 1");
goto error;
}
rc = ntru_crypto_ntru_encrypt(drbg, r, buffer, 16, 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, 16, 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");
fwrite(ciphertext, ciphertext_len, 1, Handle);
fclose(Handle);
return cpu_time_used;
error:
printf("ERROR %x\n", rc);
return 0;
}

Binary file not shown.

Binary file not shown.

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;
}