Commit 22a1838f authored by Davide Galassi's avatar Davide Galassi

Modular inverse for 'long' types moved to internal utilities

Code cleanup
parent 900b6ad9
Pipeline #32711927 passed with stages
in 1 minute and 16 seconds
......@@ -2,26 +2,6 @@
#include "misc.h"
#include <string.h>
long cry_inverse(unsigned long a, unsigned long m)
{
long r0, r1, s0, s1, q;
r0 = a;
r1 = m;
s0 = 1;
s1 = 0;
while (r1 != 0) {
/* r2 = r0 - q*r1 */
q = r0 / r1;
r0 %= r1;
CRY_SWAP(r0, r1);
/* s2 = s0 - q*s1 */
s0 -= q*s1;
CRY_SWAP(s0, s1);
}
return s0;
}
void cry_affine_encrypt(struct cry_affine_ctx *ctx, unsigned char *out,
const unsigned char *in, size_t len)
{
......@@ -54,7 +34,6 @@ 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));
......@@ -70,11 +49,7 @@ int cry_affine_init(struct cry_affine_ctx *ctx, const unsigned char *keya,
res = -1;
break;
}
inv = cry_inverse(ctx->keya[i], 256);
inv %= 256;
if (inv < 0)
inv += 256;
ctx->inva[i] = (unsigned char)inv;
ctx->inva[i] = (unsigned char)cry_long_inv(ctx->keya[i], 256);
}
return res;
}
......
......@@ -34,7 +34,7 @@ static unsigned char mtx_det(unsigned char *mtx, size_t n)
det = 0;
for (i = 0; i < n; i++) {
mtx_minor(b, mtx, 0, i, n);
det += mtx[i] * ((i % 2) ? -1 : 1) * mtx_det(b, n-1);
det += mtx[i] * ((i & 1) ? -1 : 1) * mtx_det(b, n-1);
det %= 256;
if (det < 0)
det += 256;
......@@ -77,7 +77,7 @@ static void mtx_invert(unsigned char *imtx, unsigned char *mtx,
for (j = 0; j < n; j++) {
mtx_minor(b, t, i, j, n);
c = mtx_det(b, n-1);
if ((i+j) % 2)
if (((i+j) & 1))
c = -c;
c *= idet;
c %= 256;
......@@ -171,16 +171,15 @@ static int keygen(unsigned char *key, unsigned char *ikey, size_t keylen,
}
det = mtx_det(key, n);
if (det != 0) {
for (idet = 1; idet < 256; idet++) {
if ((idet * det) % 256 == 1)
break;
}
if (idet != 256) {
mtx_invert(ikey, key, n, idet);
res = 0;
trials = 0;
}
/*
* Determinant should be non-zero (to be invertible) and
* should be odd, since if gcd(det, 256)=1 -> is invertible
*/
if ((det & 1) != 0) {
idet = cry_long_inv(det, 256);
mtx_invert(ikey, key, n, idet);
res = 0;
trials = 0;
}
} while (trials > 0);
......
#include "misc.h"
unsigned long cry_long_inv(unsigned long val, unsigned long mod)
{
unsigned long r0, r1, q;
long s0, s1;
r0 = val;
r1 = mod;
s0 = 1;
s1 = 0;
while (r1 != 0) {
/* r2 = r0 - q*r1 */
q = r0 / r1;
r0 %= r1;
CRY_SWAP(r0, r1);
/* s2 = s0 - q*s1 */
s0 -= q*s1;
CRY_SWAP(s0, s1);
}
if (r0 != 1)
s0 = 0;
else if (s0 < 0)
s0 += mod;
return (unsigned long)s0;
}
......@@ -2,8 +2,8 @@
* A bounch of helper macros and functions meant for internal use.
*/
#ifndef _CRY_MISC_H_
#define _CRY_MISC_H_
#ifndef CRY_MISC_H_
#define CRY_MISC_H_
#include <stdint.h>
......@@ -93,4 +93,14 @@
#define CRY_ROTL32(val, bits) \
CRY_ROTL(val, 32, bits)
#endif /* _CRY_MISC_H_ */
/**
* Modular inverse using Euclid algorithm.
*
* @param val Value.
* @param mod Module.
* @return On success returns the positive modular inverse.
* If the value is not invertible returns 0.
*/
unsigned long cry_long_inv(unsigned long val, unsigned long mod);
#endif /* CRY_MISC_H_ */
......@@ -16,7 +16,8 @@ objs-y := version.o \
dh.o \
ecdh.o \
dsa.o \
ecdsa.o
ecdsa.o \
misc.o
subdirs-y += mpi crc prng sum ecp classic
......@@ -77,6 +77,7 @@ static void dispatch(int argc, char *argv[])
printf("Test '%s' not defined\n", test);
}
void hill_test(void)
{
printf("* HILL Test\n");
......
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