Commit e2277361 authored by g3gg0's avatar g3gg0
Browse files

io_crypt: updated for new RNG interface

--HG--
branch : unified
parent f44253449619
# Makefile for the io_decrypt software
HOST_OBJ=io_decrypt.host.o crypt_lfsr64.host.o hash_password.host.o
HOST_OBJ=io_decrypt.host.o crypt_lfsr64.host.o crypt_rsa.host.o hash_password.host.o bigdigits.host.o bigd.host.o
HOST_BIN=io_decrypt
HOSTCC=gcc
HOST_CFLAGS=-m32 -mno-ms-bitfields -O2 -Wall -I$(SRC_DIR) -I. -D_FILE_OFFSET_BITS=64 -fno-strict-aliasing -std=c99 -DHOST_PROGRAM
HOST_CFLAGS=-m32 -ggdb -mno-ms-bitfields -O2 -Wall -I$(SRC_DIR) -I. -D_FILE_OFFSET_BITS=64 -fno-strict-aliasing -std=c99 -DHOST_PROGRAM
HOST_LDFLAGS=-lm -m32
HOST_DEPS=crypt_lfsr64.h
HOST=host
......
......@@ -26,7 +26,9 @@ This notice must always be retained in any copy.
#include "bigd.h"
#include "bigdigits.h"
#ifdef MODULE
#include "mem.h"
#endif
#define assert(x) do {} while(0)
......
......@@ -26,7 +26,10 @@ This notice must always be retained in any copy.
#include <time.h>
#include "bigdigits.h"
#ifdef MODULE
#include "mem.h"
#endif
/* For debugging - these are NOOPs */
#define DPRINTF0(s)
#define DPRINTF1(s, a1)
......
#ifdef MODULE
#include <dryos.h>
#include <property.h>
#include <bmp.h>
#include <menu.h>
#include "../trace/trace.h"
#ifdef HOST_PROGRAM
#else
#include <stdio.h>
#include <stdlib.h>
......@@ -8,14 +14,6 @@
#define trace_write(x,...) do { printf(__VA_ARGS__); printf("\n"); } while (0)
#else
#include <dryos.h>
#include <property.h>
#include <bmp.h>
#include <menu.h>
#include "../trace/trace.h"
#endif
#include "io_crypt.h"
......
#ifdef HOST_PROGRAM
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define trace_write(x,...) do { printf(__VA_ARGS__); printf("\n"); } while (0)
#else
#ifdef MODULE
#include <dryos.h>
#include <property.h>
......@@ -16,8 +8,23 @@
#include <menu.h>
#include "../trace/trace.h"
#else
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define trace_write(x,...) do { printf(__VA_ARGS__); printf("\n"); } while (0)
#define NotifyBox(x,...) do { printf(__VA_ARGS__); printf("\n"); } while (0)
#define beep() do { } while(0)
#define beep_times(x) do { } while(0)
#define msleep(x) usleep((x)*1000)
#define task_create(a,b,c,d,e) do { d(e); } while(0)
#endif
#include <rand.h>
#include "io_crypt.h"
#include "crypt_rsa.h"
......@@ -50,68 +57,35 @@ static bdigit_t small_primes[] = {
};
#define N_SMALL_PRIMES (sizeof(small_primes)/sizeof(bdigit_t))
char *strdup(const char*str)
{
char *ret = malloc(strlen(str) + 1);
strcpy(ret, str);
return ret;
}
int time()
{
return rand();
}
int clock()
{
return rand();
}
uint64_t rand_lfsr = 0xDEADBEEFDEADBEEF;
int rand()
static int my_rand(unsigned char *bytes, size_t nbytes, const unsigned char *seed, size_t seedlen)
{
int max_clocks = 512;
for(int clocks = 0; clocks < max_clocks; clocks++)
if(0 && seed)
{
/* maximum length LFSR according to http://www.xilinx.com/support/documentation/application_notes/xapp052.pdf */
int bit = ((rand_lfsr >> 63) ^ (rand_lfsr >> 62) ^ (rand_lfsr >> 60) ^ (rand_lfsr >> 59)) & 1;
rand_lfsr = (rand_lfsr << 1) | bit;
for(size_t pos = 0; pos < seedlen / 4; pos++)
{
rand_seed(((uint32_t *)seed)[pos]);
}
}
return (int)rand_lfsr;
}
void srand(unsigned int value)
{
rand_lfsr ^= value;
rand();
rand_lfsr ^= get_us_clock_value();
rand();
}
void *calloc(size_t nmemb, size_t size)
{
void *ret = malloc(nmemb * size);
memset(ret, 0x00, nmemb * size);
return ret;
}
while(((uint32_t)bytes % 4) && nbytes)
{
uint32_t rn = 0;
rand_fill(&rn, 1);
*bytes++ = rn & 0xFF;
nbytes--;
}
void *realloc(void *ptr, size_t size)
{
void *ret = malloc(size);
memcpy(ret, ptr, size);
free(ptr);
uint32_t words = (nbytes / 4);
uint32_t remain = (nbytes % 4);
return ret;
}
static int my_rand(unsigned char *bytes, size_t nbytes, const unsigned char *seed, size_t seedlen)
{
while (nbytes--)
rand_fill(bytes, words);
for(int pos = 0; pos < remain; pos++)
{
*bytes++ = rand() & 0xFF;
uint32_t rn = 0;
rand_fill(&rn, 1);
bytes[words * 4 + pos] = (rn & 0xFF);
}
return 0;
......@@ -170,7 +144,6 @@ int generateRSAPrime(BIGD p, size_t nbits, bdigit_t e, size_t ntests,
if (overflow)
break;
// give_a_sign('.');
count++;
/* Each time 2 is added to the current candidate
......@@ -202,7 +175,6 @@ int generateRSAPrime(BIGD p, size_t nbits, bdigit_t e, size_t ntests,
continue;
/* Do expensive primality test */
// give_a_sign('*');
if (bdRabinMiller(p, ntests))
{ /* Success! - we have a prime */
done = 1;
......@@ -212,10 +184,8 @@ int generateRSAPrime(BIGD p, size_t nbits, bdigit_t e, size_t ntests,
}
}
/* Clear up */
bdFree(&u);
// printf("\n");
return (done ? count : -1);
}
......@@ -333,7 +303,7 @@ int crypt_rsa_generate(int nbits, t_crypt_key *priv_key, t_crypt_key *pub_key)
if (res != 0)
{
printf("Failed to generate RSA key!\n");
NotifyBox(5000, "Failed to generate RSA key!\n");
goto clean_up;
}
......@@ -342,19 +312,25 @@ int crypt_rsa_generate(int nbits, t_crypt_key *priv_key, t_crypt_key *pub_key)
priv_key->name = strdup ( "new key" );
pub_key->name = strdup ( "new key" );
buffer = (char*)malloc ( bdSizeof(n)*8+1 );
bdConvToHex ( n, buffer, 32768 );
size_t nchars = bdConvToHex(n, NULL, 0);
buffer = malloc(nchars+1);
nchars = bdConvToHex(n, buffer, nchars+1);
priv_key->primefac = (char*)strdup ( (char *)buffer );
pub_key->primefac = (char*)strdup ( (char *)buffer );
free ( buffer );
buffer = (char*)malloc ( bdSizeof(d)*8+1 );
bdConvToHex ( d, buffer, 32768 );
nchars = bdConvToHex(d, NULL, 0);
buffer = malloc(nchars+1);
nchars = bdConvToHex(d, buffer, nchars+1);
priv_key->key = (char*)strdup ( (char *)buffer );
free ( buffer );
buffer = (char*)malloc ( bdSizeof(e)*8+1 );
bdConvToHex ( e, buffer, 32768 );
nchars = bdConvToHex(e, NULL, 0);
buffer = malloc(nchars+1);
nchars = bdConvToHex(e, buffer, nchars+1);
pub_key->key = (char*)strdup ( (char *)buffer );
free ( buffer );
......@@ -378,7 +354,7 @@ int crypt_rsa_generate(int nbits, t_crypt_key *priv_key, t_crypt_key *pub_key)
bdConvToOctets ( source, tmp_buf, bdSizeof(source)*4 );
if ( strcmp ( (char*)"Test", (char*)tmp_buf ) )
{
printf ( " -------------- Key check FAILED!! -----------------\n" );
printf ( "Key check FAILED!!\n" );
beep();
}
......@@ -394,7 +370,6 @@ clean_up:
bdFree(&dQ);
bdFree(&qInv);
return 0;
}
......@@ -446,7 +421,7 @@ static void crypt_rsa_deinit(void **crypt_ctx)
}
}
static uint32_t crypt_rsa_test(int size, t_crypt_key *priv_key, t_crypt_key *pub_key)
static uint32_t crypt_rsa_testfunc(int size, t_crypt_key *priv_key, t_crypt_key *pub_key)
{
uint32_t ret = 0;
......@@ -486,17 +461,45 @@ static uint32_t crypt_rsa_test(int size, t_crypt_key *priv_key, t_crypt_key *pub
{
ret = 1;
trace_write(iocrypt_trace_ctx, " post-decrypt: FAILED at pos %d", pos);
NotifyBox(5000, "Test failed, check log!\n");
beep();
break;
}
}
free(data);
free(data_orig);
beep();
return ret;
}
void crypt_rsa_test()
{
/* dome some tests */
t_crypt_key priv_key;
t_crypt_key pub_key;
uint32_t ret = 0;
ret |= crypt_rsa_testfunc(128, &priv_key, &pub_key);
ret |= crypt_rsa_testfunc(256, &priv_key, &pub_key);
ret |= crypt_rsa_testfunc(512, &priv_key, &pub_key);
ret |= crypt_rsa_testfunc(1024, &priv_key, &pub_key);
msleep(5000);
beep();
if(ret)
{
NotifyBox(5000, "Test failed, check log!\n");
beep_times(5);
}
else
{
NotifyBox(5000, "Test finished successfully\n");
}
}
/* allocate and initialize an RSA cipher ctx and save to pointer */
void crypt_rsa_init(void **crypt_ctx, uint64_t password)
{
......@@ -514,23 +517,9 @@ void crypt_rsa_init(void **crypt_ctx, uint64_t password)
ctx->cipher.encrypt = &crypt_rsa_encrypt;
ctx->cipher.decrypt = &crypt_rsa_decrypt;
ctx->cipher.deinit = &crypt_rsa_deinit;
ctx->password = password;
ctx->current_block = 0xFFFFFFFF;
*crypt_ctx = ctx;
trace_write(iocrypt_trace_ctx, "crypt_rsa_init: initialized");
/* dome some tests */
t_crypt_key priv_key;
t_crypt_key pub_key;
crypt_rsa_test(128, &priv_key, &pub_key);
crypt_rsa_test(256, &priv_key, &pub_key);
crypt_rsa_test(512, &priv_key, &pub_key);
crypt_rsa_test(1024, &priv_key, &pub_key);
crypt_rsa_test(2048, &priv_key, &pub_key);
crypt_rsa_test(4096, &priv_key, &pub_key);
crypt_rsa_test(8192, &priv_key, &pub_key);
}
......
......@@ -14,10 +14,10 @@ typedef struct
crypt_cipher_t cipher;
t_crypt_key pub_key;
t_crypt_key priv_key;
uint64_t password;
uint32_t current_block;
} rsa_ctx_t;
void crypt_rsa_init(void **crypt_ctx, uint64_t password);
void crypt_rsa_test();
#endif
......@@ -233,8 +233,7 @@ static MENU_SELECT_FUNC(iocrypt_enter_pw_select)
static MENU_SELECT_FUNC(iocrypt_test_rsa)
{
crypt_cipher_t *crypt_ctx;
crypt_rsa_init(&crypt_ctx, 0xDEADBEEF11223344);
task_create("rsa_test", 0x1e, 0x1000, crypt_rsa_test, (void*)0);
}
static struct menu_entry iocrypt_menus[] =
......@@ -316,7 +315,6 @@ static unsigned int iocrypt_init()
/* any file operation is routed through us now */
menu_add("Debug", iocrypt_menus, COUNT(iocrypt_menus) );
return 0;
}
......
......@@ -6,14 +6,48 @@
#include "io_crypt.h"
#include "crypt_lfsr64.h"
#include "crypt_rsa.h"
#include "hash_password.h"
#define BLOCKSIZE (8 * 1024)
static uint32_t lfsr113[] = { 0x00009821, 0x00098722, 0x00986332, 0x961FEFA7 };
void rand_fill(uint32_t *buffer, uint32_t length)
{
for(uint32_t pos = 0; pos < length; pos++)
{
lfsr113[0] = ((lfsr113[0] & 0xFFFFFFFE) << 18) ^ (((lfsr113[0] << 6) ^ lfsr113[0]) >> 13);
lfsr113[1] = ((lfsr113[1] & 0xFFFFFFF8) << 2) ^ (((lfsr113[1] << 2) ^ lfsr113[1]) >> 27);
lfsr113[2] = ((lfsr113[2] & 0xFFFFFFF0) << 7) ^ (((lfsr113[2] << 13) ^ lfsr113[2]) >> 21);
lfsr113[3] = ((lfsr113[3] & 0xFFFFFF80) << 13) ^ (((lfsr113[3] << 3) ^ lfsr113[3]) >> 12);
buffer[pos] = lfsr113[0] ^ lfsr113[1] ^ lfsr113[2] ^ lfsr113[3];
}
}
void rand_seed(uint32_t seed)
{
uint32_t tmp = 0;
/* apply seed to internal states and do a few rounds to equally distribute seed bits */
for(int loops = 0; loops < 128; loops++)
{
lfsr113[loops%4] ^= seed;
rand_fill(&tmp, 1);
}
}
int main(int argc, char *argv[])
{
if(argc == 2 && !strcmp(argv[1], "-t"))
{
crypt_rsa_test();
return;
}
if(argc != 4)
{
printf("Usage: '%s <infile> <outfile> <password>\n", argv[0]);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment