X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/b817bfc642225b8c3c0b6a7e42d1fb949b61a606..20fa0f6976d598481208c0583d72b2ccef637be9:/pkcs1.c diff --git a/pkcs1.c b/pkcs1.c index 3460467..47c135f 100644 --- a/pkcs1.c +++ b/pkcs1.c @@ -7,7 +7,7 @@ * (c) 2000 Straylight/Edgeware */ -/*----- Licensing notice --------------------------------------------------* +/*----- Licensing notice --------------------------------------------------* * * This file is part of Catacomb. * @@ -15,12 +15,12 @@ * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. - * + * * Catacomb is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. - * + * * You should have received a copy of the GNU Library General Public * License along with Catacomb; if not, write to the Free * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, @@ -34,6 +34,7 @@ #include #include +#include "ct.h" #include "grand.h" #include "rsa.h" @@ -90,7 +91,7 @@ mp *pkcs1_cryptencode(mp *d, const void *m, size_t msz, octet *b, size_t sz, assert(q == b + sz); /* --- Collect the result --- */ - + return (mp_loadb(d, b, sz)); } @@ -109,24 +110,13 @@ mp *pkcs1_cryptencode(mp *d, const void *m, size_t msz, octet *b, size_t sz, * in PKCS#1 v. 2.0 (RFC2437). */ -static int memeq(const void *xx, const void *yy, size_t sz) -{ - int eq = 1; - const octet *x = xx, *y = yy; - while (sz) { /* Always check every byte */ - if (*x++ != *y++) eq = 0; - sz--; - } - return (eq); -} - int pkcs1_cryptdecode(mp *m, octet *b, size_t sz, unsigned long nbits, void *p) { pkcs1 *pp = p; const octet *q, *qq; size_t n, i; - int bad = 0; + uint32 goodp = 1; /* --- Check the size of the block looks sane --- */ @@ -138,26 +128,29 @@ int pkcs1_cryptdecode(mp *m, octet *b, size_t sz, /* --- Ensure that the block looks OK --- */ - bad |= (*q++ != 0x00 || *q++ != 0x02); + goodp &= ct_inteq(*q++, 0); + goodp &= ct_inteq(*q++, 2); /* --- Check the nonzero padding --- */ i = 0; while (*q != 0 && q < qq) i++, q++; - bad |= (i < 8 || qq - q < pp->epsz + 1); + goodp &= ct_intle(8, i); + goodp &= ~ct_intle(qq - q, pp->epsz + 1); q++; /* --- Check the encoding parameters --- */ - bad |= (pp->ep && !memeq(bad ? b : q, pp->ep, pp->epsz)); + if (pp->ep) + goodp &= ct_memeq(b + ct_pick(goodp, 0, q - b), pp->ep, pp->epsz); q += pp->epsz; /* --- Done --- */ n = qq - q; - memmove(b, bad ? b + 1 : q, n); - return (bad ? -1 : n); + memmove(b, b + ct_pick(goodp, 1, q - b), n); + return (goodp ? n : -1); } /* --- @pkcs1_sigencode@ --- *