X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb/blobdiff_plain/0f00dc4c8eb47e67bc0f148c2dd109f73a451e0a..141c12847a1c2f8cc8db03d420551584e689fb87:/pub/rsa-pub.c diff --git a/pub/rsa-pub.c b/pub/rsa-pub.c index 78793a92..bb402a3a 100644 --- a/pub/rsa-pub.c +++ b/pub/rsa-pub.c @@ -29,6 +29,7 @@ #include #include #include +#include #include "mp.h" #include "mpmont.h" @@ -48,7 +49,7 @@ void rsa_pubcreate(rsa_pubctx *rd, rsa_pub *rp) { - rd->rp = rp; + rd->rp = rp; mp_shrink(rp->e); mpmont_create(&rd->mm, rp->n); } @@ -79,6 +80,31 @@ void rsa_pubdestroy(rsa_pubctx *rd) mp *rsa_pubop(rsa_pubctx *rd, mp *d, mp *p) { + mp *e = rd->rp->e; + unsigned i; + + if (MP_EQ(e, MP_THREE)) { + MP_COPY(p); + d = mpmont_mul(&rd->mm, d, p, rd->mm.r2); + d = mp_sqr(d, d); d = mpmont_reduce(&rd->mm, d, d); + d = mpmont_mul(&rd->mm, d, d, p); + MP_DROP(p); + return (d); + } +#if MPW_BITS > 16 + if (MP_LEN(e) == 1 && e->v[0] == 65537) +#else + if (0 && MP_LEN(e) == 2 && e->v[0] == 1 && e->v[1] == (1 << (16 - MPW_BITS))) +#endif + { + MP_COPY(p); + d = mpmont_mul(&rd->mm, d, p, rd->mm.r2); + for (i = 0; i < 16; i++) + { d = mp_sqr(d, d); d = mpmont_reduce(&rd->mm, d, d); } + d = mpmont_mul(&rd->mm, d, d, p); + MP_DROP(p); + return (d); + } return (mpmont_exp(&rd->mm, d, p, rd->rp->e)); } @@ -175,7 +201,7 @@ int rsa_verify(rsa_pubctx *rp, mp *s, const void *m, size_t msz, dstr_ensure(d, n); rc = e(p, m, msz, (octet *)d->buf + d->len, n, nb, earg); if (rc > 0 && m) { - if (rc != msz || memcmp(d->buf + d->len, m, msz) != 0) + if (rc != msz || MEMCMP(d->buf + d->len, !=, m, msz)) rc = -1; else rc = 0;