2 * Copyright (c) 2013, SUSE Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
8 /* simple and slow rsa/dsa verification code. */
15 #include "solv_pgpvrfy.h"
17 typedef unsigned int mp_t;
18 typedef unsigned long long mp2_t;
21 #define MP_T_BITS (MP_T_BYTES * 8)
24 mpzero(int len, mp_t *target)
26 memset(target, 0, MP_T_BYTES * len);
32 return solv_calloc(len, MP_T_BYTES);
36 mpcpy(int len, mp_t *target, mp_t *source)
38 memcpy(target, source, len * MP_T_BYTES);
42 static void mpdump(int l, mp_t *a, char *s)
46 fprintf(stderr, "%s", s);
47 for (i = l - 1; i >= 0; i--)
48 fprintf(stderr, "%0*x", MP_T_BYTES * 2, a[i]);
49 fprintf(stderr, "\n");
53 /* target[len] = x, target = target % mod
54 * assumes that target < (mod << MP_T_BITS)! */
56 mpdomod(int len, mp_t *target, mp2_t x, mp_t *mod)
59 for (i = len - 1; i >= 0; i--)
61 x = (x << MP_T_BITS) | target[i];
68 while (x >= 2 * (mp2_t)mod[i])
71 mp2_t z = x / ((mp2_t)mod[i] + 1);
73 if ((z >> MP_T_BITS) != 0)
74 z = (mp2_t)1 << MP_T_BITS; /* just in case... */
75 for (j = 0; j < i; j++)
94 for (j = i - 1; j >= 0; j--)
95 if (target[j] < mod[j])
97 else if (target[j] > mod[j])
100 /* target >= mod, subtract mod */
102 for (j = 0; j <= i; j++)
104 mp2_t n2 = mod[j] + n;
105 n = n2 > target[j] ? 1 : 0;
106 target[j] -= (mp_t)n2;
111 /* target += src * m */
113 mpmult_add_int(int len, mp_t *target, mp_t *src, mp2_t m, mp_t *mod)
117 for (i = 0; i < len; i++)
119 x += src[i] * m + target[i];
123 mpdomod(len, target, x, mod);
126 /* target = target << MP_T_BITS */
128 mpshift(int len, mp_t *target, mp_t *mod)
135 memmove(target + 1, target, (len - 1) * MP_T_BYTES);
137 mpdomod(len, target, x, mod);
140 /* target += m1 * m2 */
142 mpmult_add(int len, mp_t *target, mp_t *m1, int m2len, mp_t *m2, mp_t *tmp, mp_t *mod)
145 for (j = m2len - 1; j >= 0; j--)
151 for (i = 0; i < j; i++)
154 mpmult_add_int(len, target, tmp, m2[i], mod);
155 mpshift(len, tmp, mod);
158 mpmult_add_int(len, target, tmp, m2[i], mod);
161 /* target = target * m */
163 mpmult_inplace(int len, mp_t *target, mp_t *m, mp_t *tmp1, mp_t *tmp2, mp_t *mod)
166 mpmult_add(len, tmp1, target, len, m, tmp2, mod);
167 mpcpy(len, target, tmp1);
170 /* target = target ^ 16 * b ^ e */
172 mppow_int(int len, mp_t *target, mp_t *t, mp_t *mod, int e)
174 mp_t *t2 = t + len * 16;
175 mpmult_inplace(len, target, target, t, t2, mod);
176 mpmult_inplace(len, target, target, t, t2, mod);
177 mpmult_inplace(len, target, target, t, t2, mod);
178 mpmult_inplace(len, target, target, t, t2, mod);
180 mpmult_inplace(len, target, t + len * e, t, t2, mod);
183 /* target = b ^ e (b has to be < mod) */
185 mppow(int len, mp_t *target, mp_t *b, int elen, mp_t *e, mp_t *mod)
191 for (i = elen - 1; i >= 0; i--)
197 mpcpy(len, t + len, b);
198 for (j = 2; j < 16; j++)
199 mpmult_add(len, t + len * j, b, len, t + len * j - len, t + len * 16, mod);
203 mppow_int(len, target, t, mod, (e[i] >> 28) & 0x0f);
204 mppow_int(len, target, t, mod, (e[i] >> 24) & 0x0f);
205 mppow_int(len, target, t, mod, (e[i] >> 20) & 0x0f);
206 mppow_int(len, target, t, mod, (e[i] >> 16) & 0x0f);
207 mppow_int(len, target, t, mod, (e[i] >> 12) & 0x0f);
208 mppow_int(len, target, t, mod, (e[i] >> 8) & 0x0f);
209 mppow_int(len, target, t, mod, (e[i] >> 4) & 0x0f);
210 mppow_int(len, target, t, mod, e[i] & 0x0f);
211 #elif MP_T_BYTES == 1
212 mppow_int(len, target, t, mod, (e[i] >> 4) & 0x0f);
213 mppow_int(len, target, t, mod, e[i] & 0x0f);
219 /* target = m1 * m2 (m1 has to be < mod) */
221 mpmult(int len, mp_t *target, mp_t *m1, int m2len, mp_t *m2, mp_t *mod)
223 mp_t *tmp = mpnew(len);
225 mpmult_add(len, target, m1, m2len, m2, tmp, mod);
230 mpisless(int len, mp_t *a, mp_t *b)
233 for (i = len - 1; i >= 0; i--)
236 else if (a[i] > b[i])
242 mpiszero(int len, mp_t *a)
245 for (i = 0; i < len; i++)
252 mpdec(int len, mp_t *a)
255 for (i = 0; i < len; i++)
263 mpdsa(int pl, mp_t *p, int ql, mp_t *q, mp_t *g, mp_t *y, mp_t *r, mp_t *s, int hl, mp_t *h)
270 mpdump(pl, p, "p = ");
271 mpdump(ql, q, "q = ");
272 mpdump(pl, g, "g = ");
273 mpdump(pl, y, "y = ");
274 mpdump(ql, r, "r = ");
275 mpdump(ql, s, "s = ");
276 mpdump(hl, h, "h = ");
278 if (pl < ql || !mpisless(pl, g, p) || !mpisless(pl, y, p))
279 return 0; /* hmm, bad pubkey? */
280 if (!mpisless(ql, r, q) || mpiszero(ql, r))
282 if (!mpisless(ql, s, q) || mpiszero(ql, s))
284 tmp = mpnew(pl); /* note pl */
285 mpcpy(ql, tmp, q); /* tmp = q */
286 mpdec(ql, tmp); /* tmp-- */
287 mpdec(ql, tmp); /* tmp-- */
289 mppow(ql, w, s, ql, tmp, q); /* w = s ^ tmp (s ^ -1) */
290 u1 = mpnew(pl); /* note pl */
291 /* order is important here: h can be >= q */
292 mpmult(ql, u1, w, hl, h, q); /* u1 = w * h */
293 u2 = mpnew(ql); /* u2 = 0 */
294 mpmult(ql, u2, w, ql, r, q); /* u2 = w * r */
298 mppow(pl, gu1, g, ql, u1, p); /* gu1 = g ^ u1 */
299 mppow(pl, yu2, y, ql, u2, p); /* yu2 = y ^ u2 */
300 mpmult(pl, u1, gu1, pl, yu2, p); /* u1 = gu1 * yu2 */
304 u2[0] = 1; /* u2 = 1 */
305 mpmult(ql, tmp, u2, pl, u1, q); /* tmp = u2 * u1 */
309 mpdump(ql, tmp, "res = ");
311 if (memcmp(tmp, r, ql * MP_T_BYTES) != 0)
321 mprsa(int nl, mp_t *n, int el, mp_t *e, mp_t *m, mp_t *c)
325 mpdump(nl, n, "n = ");
326 mpdump(el, e, "e = ");
327 mpdump(nl, m, "m = ");
328 mpdump(nl, c, "c = ");
330 if (!mpisless(nl, m, n))
332 if (!mpisless(nl, c, n))
335 mppow(nl, tmp, m, el, e, n); /* tmp = m ^ e */
337 mpdump(nl, tmp, "res = ");
339 if (memcmp(tmp, c, nl * MP_T_BYTES) != 0)
348 /* create mp with size tbits from data with size dbits */
350 mpbuild(const unsigned char *d, int dbits, int tbits, int *mplp)
352 int l = (tbits + MP_T_BITS - 1) / MP_T_BITS;
355 mp_t *out = mpnew(l ? l : 1);
358 dl = (dbits + 7) / 8;
361 dl = (tbits + 7) / 8;
362 for (i = 0; dl > 0; dl--, i++)
365 out[i / MP_T_BYTES] |= x << (8 * (i % MP_T_BYTES));
370 static const unsigned char *
371 findmpi(const unsigned char **mpip, int *mpilp, int maxbits, int *outlen)
374 const unsigned char *mpi = *mpip;
380 bits = mpi[0] << 8 | mpi[1];
381 l = 2 + (bits + 7) / 8;
382 if (bits > maxbits || mpil < l || (bits && !mpi[2]))
394 solv_pgpvrfy(const unsigned char *pub, int publ, const unsigned char *sig, int sigl)
397 unsigned char *oid = 0;
398 const unsigned char *mpi;
402 if (!pub || !sig || publ < 1 || sigl < 2)
404 if (pub[0] != sig[0])
405 return 0; /* key algo mismatch */
409 hashl = 16; /* MD5 */
410 oid = (unsigned char *)"\022\060\040\060\014\006\010\052\206\110\206\367\015\002\005\005\000\004\020";
413 hashl = 20; /* SHA-1 */
414 oid = (unsigned char *)"\017\060\041\060\011\006\005\053\016\003\002\032\005\000\004\024";
417 hashl = 32; /* SHA-256 */
418 oid = (unsigned char *)"\023\060\061\060\015\006\011\140\206\110\001\145\003\004\002\001\005\000\004\040";
421 hashl = 48; /* SHA-384 */
422 oid = (unsigned char *)"\023\060\101\060\015\006\011\140\206\110\001\145\003\004\002\002\005\000\004\060";
425 hashl = 64; /* SHA-512 */
426 oid = (unsigned char *)"\023\060\121\060\015\006\011\140\206\110\001\145\003\004\002\003\005\000\004\100";
429 hashl = 28; /* SHA-224 */
430 oid = (unsigned char *)"\023\060\061\060\015\006\011\140\206\110\001\145\003\004\002\004\005\000\004\034";
433 return 0; /* unsupported hash algo */
435 if (sigl < 2 + hashl)
441 const unsigned char *n, *e, *m;
443 int nlen, elen, mlen, clen;
444 mp_t *nx, *ex, *mx, *cx;
449 n = findmpi(&mpi, &mpil, 8192, &nlen);
450 e = findmpi(&mpi, &mpil, 1024, &elen);
451 mpi = sig + 2 + hashl;
452 mpil = sigl - (2 + hashl);
453 m = findmpi(&mpi, &mpil, nlen, &mlen);
454 if (!n || !e || !m || !nlen || !elen)
456 /* build padding block */
457 clen = (nlen - 1) / 8;
458 if (hashl + *oid + 2 > clen)
460 c = solv_malloc(clen);
461 memset(c, 0xff, clen);
463 memcpy(c + clen - hashl, sig + 2, hashl);
464 memcpy(c + clen - hashl - *oid, oid + 1, *oid);
465 c[clen - hashl - *oid - 1] = 0;
466 clen = clen * 8 - 7; /* always <= nlen */
467 nx = mpbuild(n, nlen, nlen, &nxl);
468 ex = mpbuild(e, elen, elen, &exl);
469 mx = mpbuild(m, mlen, nlen, 0);
470 cx = mpbuild(c, clen, nlen, 0);
472 res = mprsa(nxl, nx, exl, ex, mx, cx);
481 const unsigned char *p, *q, *g, *y, *r, *s;
482 int plen, qlen, glen, ylen, rlen, slen, hlen;
483 mp_t *px, *qx, *gx, *yx, *rx, *sx, *hx;
488 p = findmpi(&mpi, &mpil, 8192, &plen);
489 q = findmpi(&mpi, &mpil, 1024, &qlen);
490 g = findmpi(&mpi, &mpil, plen, &glen);
491 y = findmpi(&mpi, &mpil, plen, &ylen);
492 mpi = sig + 2 + hashl;
493 mpil = sigl - (2 + hashl);
494 r = findmpi(&mpi, &mpil, qlen, &rlen);
495 s = findmpi(&mpi, &mpil, qlen, &slen);
496 if (!p || !q || !g || !y || !r || !s || !plen || !qlen)
498 hlen = (qlen + 7) & ~7;
499 if (hlen > hashl * 8)
501 px = mpbuild(p, plen, plen, &pxl);
502 qx = mpbuild(q, qlen, qlen, &qxl);
503 gx = mpbuild(g, glen, plen, 0);
504 yx = mpbuild(y, ylen, plen, 0);
505 rx = mpbuild(r, rlen, qlen, 0);
506 sx = mpbuild(s, slen, qlen, 0);
507 hx = mpbuild(sig + 2, hlen, hlen, &hxl);
508 res = mpdsa(pxl, px, qxl, qx, gx, yx, rx, sx, hxl, hx);
519 return 0; /* unsupported pubkey algo */