Commit 36023f33 authored by Davide Galassi's avatar Davide Galassi

RSA keygen first commit and test

parent 63090f52
Pipeline #50231703 passed with stages
in 1 minute and 29 seconds
......@@ -32,6 +32,9 @@ int cry_rsa_encrypt(cry_rsa_ctx *ctx, unsigned char **out, size_t *outlen,
int cry_rsa_decrypt(cry_rsa_ctx *ctx, unsigned char **out, size_t *outlen,
const unsigned char *in, size_t inlen);
int cry_rsa_keygen(cry_rsa_ctx *ctx, unsigned int bits);
#ifdef __cplusplus
}
#endif
......
......@@ -196,3 +196,51 @@ int cry_rsa_decrypt(cry_rsa_ctx *ctx, unsigned char **out, size_t *out_siz,
}
return res;
}
#define MAX_ITER 10000
int cry_rsa_keygen(cry_rsa_ctx *ctx, unsigned int bits)
{
int res;
cry_mpi phi, p, q, p1, q1, one;
cry_mpi_digit one_dig;
int hbits = bits >> 1;
unsigned int i;
if ((res = cry_mpi_init_list(&ctx->d, &ctx->e, &ctx->m, NULL)) != 0)
return res;
if ((res = cry_mpi_init_list(&p, &q, &p1, &q1, &phi, NULL)) != 0)
goto e;
i = MAX_ITER;
if ((res = cry_mpi_prime(&p, hbits, &i)) != 0)
goto e;
i = MAX_ITER;
if ((res = cry_mpi_prime(&q, hbits, &i)) != 0)
goto e;
if ((res = cry_mpi_mul(&ctx->m, &p, &q)) != 0)
goto e;
one.alloc = 1;
one.used = 1;
one.sign = 0;
one.data = &one_dig;
if ((res = cry_mpi_sub(&p1, &p, &one)) != 0)
goto e;
if ((res = cry_mpi_sub(&q1, &q, &one)) != 0)
goto e;
if ((res = cry_mpi_mul(&phi, &p1, &q1)) != 0)
goto e;
/* Find key */
for (i = 0; i < MAX_ITER; i++) {
cry_mpi_rand(&ctx->e, bits);
if ((res = cry_mpi_inv(&ctx->d, &ctx->e, &phi)) == 0)
break;
}
e: cry_mpi_clear_list(&p, &q, &p1, &q1, &phi, NULL);
if (res != 0)
cry_mpi_clear_list(&ctx->d, &ctx->e, &ctx->m, NULL);
return res;
}
......@@ -20,11 +20,11 @@ objs := \
hill_test.o \
affine_test.o \
crc_test.o \
rsa_test.o \
# md5_test.o \
sha256_test.o \
cmac_test.o \
sum_test.o \
rsa_test.o \
rand_test.o \
dh_test.o \
dsa_test.o \
......
#include "test.h"
#include <cry/rsa.h>
static void keygen(void)
{
cry_rsa_ctx rsa;
ASSERT_OK(cry_rsa_keygen(&rsa, 1024));
}
static const unsigned char modulus[] = {
0xC4, 0xF8, 0xE9, 0xE1, 0x5D, 0xCA, 0xDF, 0x2B,
0x96, 0xC7, 0x63, 0xD9, 0x81, 0x00, 0x6A, 0x64,
......@@ -47,7 +56,7 @@ static const unsigned char cipher_text[] = {
#define PLAIN_LEN sizeof(plain_text)
#define CIPHER_LEN sizeof(cipher_text)
void rsa_test(void)
static void encrypt_decrypt(void)
{
cry_rsa_ctx rsa;
size_t outlen;
......@@ -58,57 +67,60 @@ void rsa_test(void)
cry_mpi_init_bin(&rsa.e, public, sizeof(public));
cry_mpi_init_bin(&rsa.d, private, sizeof(private));
TRACE("m-bits: %d\n", cry_mpi_count_bits(&rsa.m));
TRACE("e-bits: %d\n", cry_mpi_count_bits(&rsa.e));
TRACE("d-bits: %d\n", cry_mpi_count_bits(&rsa.d));
PRINT_MPI("m", &rsa.m, 16);
PRINT_MPI("e", &rsa.e, 16);
PRINT_MPI("d", &rsa.d, 16);
/*
* ES-PKCS1-v1.5
*/
rsa.flags = 0;
ASSERT_OK(cry_rsa_encrypt(&rsa, &cipher_buf, &outlen,
plain_text, PLAIN_LEN));
if (cipher_buf) {
PRINT_HEX("ciphertext", cipher_buf, outlen);
ASSERT_EQ(outlen, CIPHER_LEN);
ASSERT_OK(cry_rsa_decrypt(&rsa, &plain_buf, &outlen,
cipher_buf, outlen));
if (plain_buf) {
PRINT_HEX("plaintext ", plain_buf, outlen);
ASSERT_EQ(outlen, PLAIN_LEN);
ASSERT_EQ_BUF(plain_buf, plain_text, outlen);
free(plain_buf);
}
free(cipher_buf);
}
cry_mpi_clear_list(&rsa.m, &rsa.e, &rsa.d, NULL);
}
/*
* SSA-PKCS1-v1.5
*/
static void sign_verify(void)
{
cry_rsa_ctx rsa;
size_t outlen;
unsigned char *cipher_buf;
unsigned char *plain_buf;
cry_mpi_init_bin(&rsa.m, modulus, sizeof(modulus));
cry_mpi_init_bin(&rsa.e, public, sizeof(public));
cry_mpi_init_bin(&rsa.d, private, sizeof(private));
rsa.flags = CRY_RSA_FLAG_SIGN;
ASSERT_OK(cry_rsa_encrypt(&rsa, &cipher_buf, &outlen,
plain_text, PLAIN_LEN));
if (cipher_buf) {
PRINT_HEX("ciphertext", cipher_buf, outlen);
ASSERT_EQ(outlen, CIPHER_LEN);
ASSERT_EQ_BUF(cipher_buf, cipher_text, outlen);
ASSERT_OK(cry_rsa_decrypt(&rsa, &plain_buf, &outlen,
cipher_buf, outlen));
if (plain_buf) {
PRINT_HEX("plaintext ", plain_buf, outlen);
ASSERT_EQ(outlen, PLAIN_LEN);
ASSERT_EQ_BUF(plain_buf, plain_text, outlen);
free(plain_buf);
}
free(cipher_buf);
}
cry_mpi_clear_list(&rsa.m, &rsa.e, &rsa.d, NULL);
}
void rsa_test(void)
{
printf("* RSA Test\n");
run("keygen", keygen, NULL, NULL);
run("encrypt-decrypt", encrypt_decrypt, NULL, NULL);
run("sign-verify", sign_verify, NULL, NULL);
printf("\n");
}
......@@ -11,6 +11,7 @@ void trivium_test(void);
void hill_test(void);
void affine_test(void);
void crc_test(void);
void rsa_test(void);
static int g_runs;
......@@ -28,7 +29,6 @@ struct sub_test {
struct sub_test g_tests[] = {
SUB_TEST(version),
#if 1
SUB_TEST(memxor),
SUB_TEST(base64),
SUB_TEST(mpi),
......@@ -39,7 +39,7 @@ struct sub_test g_tests[] = {
SUB_TEST(hill),
SUB_TEST(affine),
SUB_TEST(crc),
#endif
SUB_TEST(rsa),
#if 0
TEST_ELEM(md5),
TEST_ELEM(sha256),
......@@ -57,7 +57,6 @@ struct sub_test g_tests[] = {
TEST_ELEM(sha256),
TEST_ELEM(cmac),
TEST_ELEM(sum),
TEST_ELEM(rsa),
TEST_ELEM(rand),
TEST_ELEM(dh),
TEST_ELEM(dsa),
......
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