X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=rsa.c;h=1c37ef98acf3abec9e641267d532e720efa74f27;hb=1b8af2f7f86131a5364f2270865895ea597c591e;hp=28a4c1b3225c3caefbb5a296f7582a149b63d245;hpb=5355a1fb140fcb8e5836ba90ed87ccb85d76f40b;p=secnet.git diff --git a/rsa.c b/rsa.c index 28a4c1b..1c37ef9 100644 --- a/rsa.c +++ b/rsa.c @@ -42,7 +42,6 @@ #define mpp(s,n) do { char *p = mpz_get_str(NULL,16,n); printf("%s 0x%sL\n", s, p); free(p); } while (0) struct rsacommon { - struct hash_if *hashi; uint8_t *hashbuf; }; @@ -74,25 +73,22 @@ struct rsapub { static const char *hexchars="0123456789abcdef"; -static void rsa_sethash(struct rsacommon *c, struct hash_if *hash) +static void rsa_sethash(struct rsacommon *c, struct hash_if *hash, + const struct hash_if **in_ops) { free(c->hashbuf); c->hashbuf=safe_malloc(hash->hlen, "generate_msg"); - c->hashi=hash; + *in_ops=hash; } static void rsa_pub_sethash(void *sst, struct hash_if *hash) { struct rsapub *st=sst; - rsa_sethash(&st->common, hash); + rsa_sethash(&st->common, hash, &st->ops.hash); } static void rsa_priv_sethash(void *sst, struct hash_if *hash) { struct rsapriv *st=sst; - rsa_sethash(&st->common, hash); -} -static void rsa_hash(struct rsacommon *c, const uint8_t *buf, int32_t len) -{ - hash_hash(c->hashi,buf,len,c->hashbuf); + rsa_sethash(&st->common, hash, &st->ops.hash); } static void emsa_pkcs1(MP_INT *n, MP_INT *m, @@ -122,7 +118,7 @@ static void emsa_pkcs1(MP_INT *n, MP_INT *m, msize=mpz_sizeinbase(n, 16); if (datalen*2+6>=msize) { - fatal("rsa_sign: message too big"); + fatal("rsa: message too big"); } strcpy(buff,"0001"); @@ -154,9 +150,9 @@ static bool_t rsa_sign(void *sst, uint8_t *data, int32_t datalen, mpz_init(&a); mpz_init(&b); - rsa_hash(&st->common,data,datalen); + hash_hash(st->ops.hash,data,datalen,st->common.hashbuf); /* Construct the message representative. */ - emsa_pkcs1(&st->n, &a, st->common.hashbuf, st->common.hashi->hlen); + emsa_pkcs1(&st->n, &a, st->common.hashbuf, st->ops.hash->hlen); /* * Produce an RSA signature (a^d mod n) using the Chinese @@ -242,8 +238,8 @@ static bool_t rsa_sig_check(void *sst, uint8_t *data, int32_t datalen, mpz_init(&b); mpz_init(&c); - rsa_hash(&st->common,data,datalen); - emsa_pkcs1(&st->n, &a, st->common.hashbuf, st->common.hashi->hlen); + hash_hash(st->ops.hash,data,datalen,st->common.hashbuf); + emsa_pkcs1(&st->n, &a, st->common.hashbuf, st->ops.hash->hlen); /* Terminate signature with a '0' - already checked that this will fit */ int save = sig->start[sig->len]; @@ -279,6 +275,8 @@ static list_t *rsapub_apply(closure_t *self, struct cloc loc, dict_t *context, st->common.hashbuf=NULL; st->ops.unpick=rsa_sig_unpick; st->ops.check=rsa_sig_check; + st->ops.hash=0; + st->ops.dispose=0; /* xxx */ st->loc=loc; i=list_elem(args,0); @@ -359,6 +357,8 @@ static list_t *rsapriv_apply(closure_t *self, struct cloc loc, dict_t *context, st->ops.sethash=rsa_priv_sethash; st->common.hashbuf=NULL; st->ops.sign=rsa_sign; + st->ops.hash=0; + st->ops.dispose=0; /* xxx */ st->loc=loc; /* Argument is filename pointing to SSH1 private key file */