Commit 900b6ad9 authored by Davide Galassi's avatar Davide Galassi

Classical Affine implementation finished.

Using the context it can be used to implement the Caesar, RotX, Vigenere,
Affine and Affine Vigenere ciphers.
parent 33fe5038
Pipeline #32708614 passed with stages
in 1 minute and 14 seconds
......@@ -11,12 +11,12 @@ long cry_inverse(unsigned long a, unsigned long m)
s0 = 1;
s1 = 0;
while (r1 != 0) {
/* r2 = r0 - q1-r1 */
/* r2 = r0 - q*r1 */
q = r0 / r1;
r0 %= r1;
CRY_SWAP(r0, r1);
/* s2 = s0 - q1*s1 */
s0 = s0 - q*s1;
/* s2 = s0 - q*s1 */
s0 -= q*s1;
CRY_SWAP(s0, s1);
}
return s0;
......@@ -54,6 +54,8 @@ int cry_affine_init(struct cry_affine_ctx *ctx, const unsigned char *keya,
const unsigned char *keyb, size_t keylen)
{
size_t i;
long inv;
int res = 0;
memset(ctx, 0, sizeof(*ctx));
if (keylen > CRY_AFFINE_KEYMAX)
......@@ -63,9 +65,17 @@ int cry_affine_init(struct cry_affine_ctx *ctx, const unsigned char *keya,
ctx->keylen = keylen;
/* Compute key inverse */
for (i = 0; i < keylen; i++) {
ctx->inva[i] = cry_inverse(ctx->keya[i], 256);
/* Check */
/* sould be odd to have gcd(256, val)=1 */
if ((ctx->keya[i] & 1) != 1) {
res = -1;
break;
}
inv = cry_inverse(ctx->keya[i], 256);
inv %= 256;
if (inv < 0)
inv += 256;
ctx->inva[i] = (unsigned char)inv;
}
return 0;
return res;
}
......@@ -15,6 +15,7 @@ objs := \
mpi_test.o \
aes_test.o \
hill_test.o \
affine_test.o
# gcm_test.o \
des_test.o \
crc_test.o \
......
#include "test.h"
#include <cry/affine.h>
#define RAW_MAX 100
struct affine_param {
unsigned char keya[CRY_AFFINE_KEYMAX];
unsigned char keyb[CRY_AFFINE_KEYMAX];
unsigned char src[CRY_AFFINE_KEYMAX];
unsigned char dst[CRY_AFFINE_KEYMAX];
size_t keylen;
size_t srclen;
size_t dstlen;
};
int raw_init(unsigned char *raw, unsigned int rawlen, const char *asc);
static void param_init(struct affine_param *par, char *argv[])
{
memset(par, 0, sizeof(*par));
par->keylen = raw_init(par->keya, sizeof(par->keya), argv[0]);
par->keylen = raw_init(par->keyb, sizeof(par->keyb), argv[1]);
par->srclen = raw_init(par->src, sizeof(par->src), argv[2]);
par->dstlen = raw_init(par->dst, sizeof(par->dst), argv[3]);
}
static void affine_encrypt(int argc, char *argv[])
{
struct cry_affine_ctx ctx;
struct affine_param par;
unsigned char dst[32];
ASSERT(argc == 4);
param_init(&par, argv);
cry_affine_init(&ctx, par.keya, par.keyb, par.keylen);
cry_affine_encrypt(&ctx, dst, par.src, par.srclen);
ASSERT_EQ_BUF(dst, par.dst, par.dstlen);
}
static void affine_decrypt(int argc, char *argv[])
{
struct cry_affine_ctx ctx;
struct affine_param par;
unsigned char dst[32];
ASSERT(argc == 4);
param_init(&par, argv);
cry_affine_init(&ctx, par.keya, par.keyb, par.keylen);
cry_affine_decrypt(&ctx, dst, par.src, par.srclen);
ASSERT_EQ_BUF(dst, par.dst, par.dstlen);
}
static void dispatch(int argc, char *argv[])
{
char *test = *argv;
argv++;
argc--;
if (strcmp(test, "affine_encrypt") == 0)
affine_encrypt(argc, argv);
else if (strcmp(test, "affine_decrypt") == 0)
affine_decrypt(argc, argv);
else
printf("Test '%s' not defined\n", test);
}
void affine_test(void)
{
struct cry_affine_ctx ctx;
unsigned char keya[] = { 1, 1, 1 };
unsigned char keyb[] = { 19, 10, 201 };
unsigned char ciphertext[100] = {0};
unsigned char cleartext[100] = {0};
cry_affine_init(&ctx, keya, keyb, sizeof(keya));
cry_affine_encrypt(&ctx, ciphertext, (unsigned char *)"HelloWorld", 10);
cry_affine_decrypt(&ctx, cleartext, ciphertext, 10);
printf("* Affine Test\n");
func_test("affine_test.data", dispatch);
printf("\n");
}
################################################################################
# Encode
# p0 : encrypt key a
# p1 : decrypt key b
# p2 : clear text
# p3 : cipher text
################################################################################
Caesar Encode <akey={1}, bkey={3}>
affine_encrypt
01
03
48656c6c6f576f726c64
4b686f6f725a72756f67
Rot13 Encode <akey={1}, bkey={13}>
affine_encrypt
01
0d
48656c6c6f576f726c64
557279797c647c7f7971
Affine Encode <akey={5}, bkey={0}>
affine_encrypt
05
00
48656c6c6f576f726c64
68f91c1c2bb32b3a1cf4
Affine Encode <akey={5}, bkey={3}> (Affine)
affine_encrypt
05
03
48656c6c6f576f726c64
6bfc1f1f2eb62e3d1ff7
Vigenere Encode <akey={1,1,1}, bkey={19, 10, 201}>
affine_encrypt
010101
130ac9
48656c6c6f576f726c64
5b6f357f7920827c3577
Polyalphabetic Affine Encode <akey={199,205,475}, bkey={8,92,107}>
affine_encrypt
05
03
48656c6c6f576f726c64
6bfc1f1f2eb62e3d1ff7
################################################################################
# Decode
# p0 : encrypt key
# p1 : decrypt key
# p2 : cipher text
# p3 : clear text
################################################################################
Caesar Decode <akey={1}, bkey={3}>
affine_decrypt
01
03
4b686f6f725a72756f67
48656c6c6f576f726c64
Rot13 Decode <akey={1}, bkey={13}>
affine_decrypt
01
0d
557279797c647c7f7971
48656c6c6f576f726c64
Affine Decode <akey={5}, bkey={0}>
affine_decrypt
05
00
68f91c1c2bb32b3a1cf4
48656c6c6f576f726c64
Affine Decode <akey={5}, bkey={3}> (Affine)
affine_decrypt
05
03
6bfc1f1f2eb62e3d1ff7
48656c6c6f576f726c64
Vigenere Decode <akey={1,1,1}, bkey={19, 10, 201}>
affine_decrypt
010101
130ac9
5b6f357f7920827c3577
48656c6c6f576f726c64
Polyalphabetic Affine Decode <akey={199,205,475}, bkey={8,92,107}>
affine_decrypt
05
03
6bfc1f1f2eb62e3d1ff7
48656c6c6f576f726c64
......@@ -6,6 +6,7 @@ void base64_test(void);
void mpi_test(void);
void aes_test(void);
void hill_test(void);
void affine_test(void);
static int g_runs;
......@@ -20,6 +21,7 @@ static const char *g_test_str[] = {
"mpi",
"aes",
"hill",
"affine",
};
static char g_test_skip[ARLEN(g_test_str)];
......@@ -32,6 +34,7 @@ static test_func_t g_test_func[] = {
mpi_test,
aes_test,
hill_test,
affine_test,
#if 0
TEST_ELEM(cbc),
TEST_ELEM(ctr),
......
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