Commit ede42e51 authored by Niels Möller's avatar Niels Möller
Browse files

* src/publickey_crypto.c (make_group_zn): Renamed from make_zn.

(make_ring_zn): New function.
(make_ssh_ring_srp_1): New function.

Rev: src/publickey_crypto.c:1.41
parent acc44b57
......@@ -42,7 +42,7 @@
#include "publickey_crypto.h.x"
#undef GABA_DEFINE
/* #include "publickey_crypto.c.x" */
#include "publickey_crypto.c.x"
struct keypair *
make_keypair(UINT32 type,
......@@ -57,15 +57,20 @@ make_keypair(UINT32 type,
return self;
}
/* Groups */
/* GABA:
(class
(name group_zn)
(super abstract_group)
(vars
(modulo bignum)))
*/
static int
zn_range(struct abstract_group *c, mpz_t x)
{
CAST(group_zn, closure, c);
/* FIXME: As we are really working in a cyclic subgroup, we should
* also try raising the element to the group order and check that we
* get 1. Without that test, some numbers in the range [1, modulo-1]
* will pass as members even if they are not generated by g. */
return ( (mpz_sgn(x) == 1) && (mpz_cmp(x, closure->modulo) < 0) );
}
......@@ -129,8 +134,8 @@ zn_small_power(struct abstract_group *c, mpz_t res, mpz_t g, UINT32 e)
}
/* Assumes p is a prime number */
struct group_zn *
make_zn(mpz_t p, mpz_t g, mpz_t order)
struct abstract_group *
make_group_zn(mpz_t p, mpz_t g, mpz_t order)
{
NEW(group_zn, res);
......@@ -139,32 +144,58 @@ make_zn(mpz_t p, mpz_t g, mpz_t order)
res->super.combine = zn_combine;
res->super.power = zn_power; /* Pretty Mutation! Magical Recall! */
res->super.small_power = zn_small_power;
res->super.add = NULL;
res->super.subtract = NULL;
mpz_init_set(res->modulo, p);
mpz_init_set(res->super.generator, g);
mpz_init_set(res->super.order, order);
return res;
return &res->super;
}
/* These are not really operations on the group, but they are needed
* for SRP. */
void
static int
zn_ring_add(struct abstract_group *s,
mpz_t res, mpz_t a, mpz_t b)
{
CAST(group_zn, self, s);
mpz_add(res, a, b);
mpz_fdiv_r(res, res, self->modulo);
return mpz_sgn(res);
}
void
static int
zn_ring_subtract(struct abstract_group *s,
mpz_t res, mpz_t a, mpz_t b)
{
CAST(group_zn, self, s);
mpz_sub(res, a, b);
mpz_fdiv_r(res, res, self->modulo);
return mpz_sgn(res);
}
/* Assumes p is a prime number, and g a primitive root. */
struct abstract_group *
make_ring_zn(mpz_t p, mpz_t g)
{
NEW(group_zn, res);
res->super.range = zn_range;
res->super.invert = zn_invert;
res->super.combine = zn_combine;
res->super.power = zn_power;
res->super.small_power = zn_small_power;
res->super.add = zn_ring_add;
res->super.subtract = zn_ring_subtract;
mpz_init_set(res->modulo, p);
mpz_init_set(res->super.generator, g);
mpz_init_set(res->super.order, p);
mpz_sub_ui(res->super.order, res->super.order, 1);
return &res->super;
}
struct abstract_group *
......@@ -174,6 +205,9 @@ make_ssh_group1(void)
mpz_t p;
mpz_t g;
mpz_t order;
/* Prime and generator as defined in
* draft-ietf-secsh-transport-07.txt. */
mpz_init_set_str(p,
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1"
......@@ -188,7 +222,7 @@ make_ssh_group1(void)
mpz_fdiv_q_2exp(order, order, 1);
mpz_init_set_ui(g, 2);
G = &make_zn(p, g, order)->super;
G = make_group_zn(p, g, order);
mpz_clear(p);
mpz_clear(g);
......@@ -196,3 +230,31 @@ make_ssh_group1(void)
return G;
}
struct abstract_group *
make_ssh_ring_srp_1(void)
{
struct abstract_group *G;
mpz_t p;
mpz_t g;
/* Same prime as in draft-ietf-secsh-transport-07.txt, but a
* different generator */
mpz_init_set_str(p,
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1"
"29024E088A67CC74020BBEA63B139B22514A08798E3404DD"
"EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245"
"E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED"
"EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381"
"FFFFFFFFFFFFFFFF", 16);
/* 7 is a primitive root */
mpz_init_set_ui(g, 7);
G = make_ring_zn(p, g);
mpz_clear(p);
mpz_clear(g);
return G;
}
Supports Markdown
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