6 #define _RPMBC_INTERNAL 7 #define _RPMPGP_INTERNAL 25 #define SPEW(_t, _rc, _dig) \ 26 { if ((_t) || _rpmbc_debug || _pgp_debug < 0) \ 27 fprintf(stderr, "<-- %s(%p) %s\t%s\n", __FUNCTION__, (_dig), \ 28 ((_rc) ? "OK" : "BAD"), (_dig)->pubkey_algoN); \ 50 if (c >=
'0' && c <=
'9')
51 return (
unsigned char) (c -
'0');
52 if (c >=
'A' && c <=
'F')
53 return (
unsigned char)((int)(c -
'A') + 10);
54 if (c >=
'a' && c <=
'f')
55 return (
unsigned char)((int)(c -
'a') + 10);
56 return (
unsigned char)
'\0';
59 #define _spewMPB(_N, _MPB) \ 60 { mpbarrett * mpb = &(_MPB); \ 61 fprintf(stderr, "\t" _N ": "); mpfprintln(stderr, mpb->size, mpb->modl); \ 64 #define _spewMPN(_N, _MPN) \ 65 { mpnumber * mpn = &(_MPN); \ 66 fprintf(stderr, "\t" _N ": "); mpfprintln(stderr, mpn->size, mpn->data); \ 70 static void rpmbcDumpRSA(
const char * msg,
rpmbc bc)
72 if (msg) fprintf(stderr,
"========== %s\n", msg);
89 static void rpmbcDumpDSA(
const char * msg,
rpmbc bc)
91 if (msg) fprintf(stderr,
"========== %s\n", msg);
94 _spewMPB(
" p", bc->dsa_keypair.param.p);
95 _spewMPB(
" q", bc->dsa_keypair.param.q);
96 _spewMPN(
" g", bc->dsa_keypair.param.g);
134 nbits = (unsigned) MP_WORDS_TO_BITS(bc->c.size);
135 else if (bc->rsa_keypair.n.size)
136 nbits = (unsigned) MP_WORDS_TO_BITS(bc->rsa_keypair.n.size);
137 nb = (nbits + 7) >> 3;
138 if (nb < 64/8 || nb > 65536/8)
143 bc->digest =
_free(bc->digest);
145 xx =
rpmDigestFinal(ctx, (
void **)&bc->digest, &bc->digestlen, 1);
147 hexstr = tt = (
char *)
xmalloc(2 * nb + 1);
148 memset(tt, (
int)
'f', (2 * nb));
149 tt[0] =
'0'; tt[1] =
'0';
150 tt[2] =
'0'; tt[3] =
'1';
151 tt += (2 * nb) - strlen(
prefix) - strlen((
char *)bc->digest) - 2;
152 *tt++ =
'0'; *tt++ =
'0';
154 tt =
stpcpy(tt, (
char *)bc->digest);
158 mpnzero(&bc->hm); (void) mpnsethex(&bc->hm, hexstr);
161 hexstr =
_free(hexstr);
164 {
const char *str = (
const char *) bc->digest;
169 rc = memcmp(s, t,
sizeof(sigp->signhash16));
188 rc = rsavrfy(&bc->rsa_keypair.n, &bc->rsa_keypair.e, &bc->c, &bc->hm);
205 xx = rsapri(&bc->rsa_keypair.n, &bc->rsa_keypair.d, &bc->hm, &bc->c);
208 xx = rsapricrt(&bc->rsa_keypair.n, &bc->rsa_keypair.p, &bc->rsa_keypair.q,
209 &bc->rsa_keypair.dp, &bc->rsa_keypair.dq, &bc->rsa_keypair.qi,
214 rc = (failures == 0);
229 if (bc->nbits == 0) bc->nbits = 1024;
231 xx = randomGeneratorContextInit(&bc->rngc, randomGeneratorDefault());
233 rsakpFree(&bc->rsa_keypair);
234 xx = rsakpMake(&bc->rsa_keypair, &bc->rngc, bc->nbits);
239 mpbnrnd(&bc->rsa_keypair.n, &bc->rngc, &bc->m);
241 rc = (failures == 0);
258 bc->digest =
_free(bc->digest);
260 rc =
rpmDigestFinal(ctx, (
void **)&bc->digest, &bc->digestlen, 0);
263 rc = mpnsetbin(&bc->hm, (byte *) bc->digest,
264 (bc->digestlen > 160/8 ? 160/8 : bc->digestlen));
265 rc = memcmp(bc->digest, sigp->signhash16,
sizeof(sigp->signhash16));
280 xx = dsavrfy(&bc->dsa_keypair.param.p, &bc->dsa_keypair.param.q,
281 &bc->dsa_keypair.param.g, &bc->hm, &bc->dsa_keypair.y,
285 rc = (failures == 0);
302 xx = dsasign(&bc->dsa_keypair.param.p, &bc->dsa_keypair.param.q,
303 &bc->dsa_keypair.param.g, &bc->rngc, &bc->hm,
304 &bc->dsa_keypair.x, &bc->r, &bc->s);
307 rc = (failures == 0);
322 if (bc->nbits == 0) bc->nbits = 1024;
324 xx = randomGeneratorContextInit(&bc->rngc, randomGeneratorDefault());
326 xx = dlkp_pInit(&bc->dsa_keypair);
328 xx = dsaparamMake(&bc->dsa_keypair.param, &bc->rngc, bc->nbits);
331 xx = dldp_pPair(&bc->dsa_keypair.param, &bc->rngc, &bc->dsa_keypair.x,
335 rc = (failures == 0);
362 int rpmbcVerifyELG(
pgpDig dig)
370 xx = elgv1vrfy(&bc->elg_keypair.param.p, &bc->elg_keypair.param.n,
371 &bc->elg_keypair.param.g, &bc->hm, &bc->elg_keypair.y,
375 rc = (failures == 0);
382 int rpmbcSignELG(
pgpDig dig)
392 xx = elgv1sign(&bc->elg_keypair.param.p, &bc->elg_keypair.param.n,
393 &bc->elg_keypair.param.g, &bc->rngc, &bc->hm,
394 &bc->elg_keypair.x, &bc->r, &bc->s);
397 rc = (failures == 0);
404 int rpmbcGenerateELG(
pgpDig dig)
407 static const char P_2048[] =
"fd12e8b7e096a28a00fb548035953cf0eba64ceb5dff0f5672d376d59c196da729f6b5586f18e6f3f1a86c73c5b15662f59439613b309e52aa257488619e5f76a7c4c3f7a426bdeac66bf88343482941413cef06256b39c62744dcb97e7b78e36ec6b885b143f6f3ad0a1cd8a5713e338916613892a264d4a47e72b583fbdaf5bce2bbb0097f7e65cbc86d684882e5bb8196d522dcacd6ad00dfbcd8d21613bdb59c485a65a58325d792272c09ad1173e12c98d865adb4c4d676ada79830c58c37c42dff8536e28f148a23f296513816d3dfed0397a3d4d6e1fa24f07e1b01643a68b4274646a3b876e810206eddacea2b9ef7636a1da5880ef654288b857ea3";
408 static const char P_1024[] =
"e64a3deeddb723e2e4db54c2b09567d196367a86b3b302be07e43ffd7f2e016f866de5135e375bdd2fba6ea9b4299010fafa36dc6b02ba3853cceea07ee94bfe30e0cc82a69c73163be26e0c4012dfa0b2839c97d6cd71eee59a303d6177c6a6740ca63bd04c1ba084d6c369dc2fbfaeebe951d58a4824de52b580442d8cae77";
415 xx = randomGeneratorContextInit(&bc->rngc, randomGeneratorDefault());
419 xx = dlkp_pInit(&bc->elg_keypair);
423 xx = dldp_pInit(&bc->elg_keypair.param);
430 mpbsethex(&bc->elg_keypair.param.p, P_2048);
434 mpbsethex(&bc->elg_keypair.param.p, P_1024);
438 xx = dldp_pgonMakeSafe(&bc->elg_keypair.param, &bc->rngc, bc->nbits);
442 if (bc->elg_keypair.param.q.size == 0) {
447 mpnset(&q, bc->elg_keypair.param.p.size, bc->elg_keypair.param.p.modl);
448 mpdivtwo(q.size, q.data);
449 mpbset(&bc->elg_keypair.param.q, q.size, q.data);
451 mpnsetw(&bc->elg_keypair.param.r, 2);
454 xx = dldp_pgonGenerator(&bc->elg_keypair.param, &bc->rngc);
460 xx = dldp_pPair(&bc->elg_keypair.param, &bc->rngc,
461 &bc->elg_keypair.x, &bc->elg_keypair.y);
469 dldp_pFree(&bc->elg_params);
472 dlkp_pFree(&bc->elg_keypair);
474 rc = (failures == 0);
502 int rpmbcVerifyECDSA(
pgpDig dig)
514 int rpmbcSignECDSA(
pgpDig dig)
526 int rpmbcGenerateECDSA(
pgpDig dig)
534 if (bc->rngc == NULL)
535 xx = randomGeneratorContextInit(&bc->rngc, randomGeneratorDefault());
537 rc = (failures == 0);
547 rpmgc gc = dig->impl;
549 rc = (gcry_err_code(gc->err) != expected);
551 fail(
"%s failed: %s\n", msg, gpg_strerror(gc->err));
563 rc = rpmgbcvailable(dig->impl, algo,
573 rc = rpmgbcvailable(dig->impl, algo,
583 rc = rpmbcAvailable(dig->impl, algo, gcry_pk_test_algo(algo));
596 switch (pubp->pubkey_algo) {
607 rc = rpmbcVerifyELG(dig);
610 rc = rpmbcVerifyECDSA(dig);
626 switch (pubp->pubkey_algo) {
637 rc = rpmbcSignELG(dig);
640 rc = rpmbcSignECDSA(dig);
656 switch (pubp->pubkey_algo) {
667 rc = rpmbcGenerateELG(dig);
670 rc = rpmbcGenerateECDSA(dig);
685 char * t = (
char *)
xmalloc(2*nb + 1);
700 mpnumber * mpn = (mpnumber *) dest;
707 if (pend != NULL && (p + ((mbits+7) >> 3)) > pend)
713 nbits = (lbits > mbits ? lbits : mbits);
714 nbytes = ((nbits + 7) >> 3);
715 t = (
char *)
xmalloc(2*nbytes+1);
716 ix = 2 * ((nbits - mbits) >> 3);
719 fprintf(stderr,
"*** mbits %u nbits %u nbytes %u t %p[%d] ix %u\n", mbits, nbits, nbytes, t, (2*nbytes+1), ix);
720 if (ix > 0) memset(t, (
int)
'0', ix);
726 fprintf(stderr,
"*** %s %s\n", pre, t);
727 (void) mpnsethex(mpn, t);
739 const char * s = NULL;
751 (void) mpnsethex(&bc->c, s =
pgpMpiHex(p));
756 rc =
pgpMpiSet(pre, 160, &bc->r, p, pend);
761 rc =
pgpMpiSet(pre, 160, &bc->s, p, pend);
766 (void) mpbsethex(&bc->rsa_keypair.n, s =
pgpMpiHex(p));
768 _spewMPB(
" n", bc->dsa_keypair.param.n);
771 (void) mpnsethex(&bc->rsa_keypair.e, s =
pgpMpiHex(p));
776 (void) mpbsethex(&bc->dsa_keypair.param.p, s =
pgpMpiHex(p));
778 _spewMPB(
" p", bc->dsa_keypair.param.p);
781 (void) mpbsethex(&bc->dsa_keypair.param.q, s =
pgpMpiHex(p));
783 _spewMPB(
" q", bc->dsa_keypair.param.q);
786 (void) mpnsethex(&bc->dsa_keypair.param.g, s =
pgpMpiHex(p));
788 _spewMPN(
" g", bc->dsa_keypair.param.g);
791 (void) mpnsethex(&bc->dsa_keypair.y, s =
pgpMpiHex(p));
810 bc->digest =
_free(bc->digest);
813 randomGeneratorContextFree(&bc->rngc);
815 rsakpFree(&bc->rsa_keypair);
817 dlkp_pFree(&bc->dsa_keypair);
819 dlkp_pFree(&bc->elg_keypair);
821 dldp_pFree(&bc->elg_params);
869 time_t now = time(NULL);
884 *be++ = pubp->pubkey_algo;
886 bn = mpbits(bc->dsa_keypair.param.p.size, bc->dsa_keypair.param.p.modl);
888 *be++ = (bn >> 8); *be++ = (bn );
889 xx = i2osp(be, bn/8, bc->dsa_keypair.param.p.modl, bc->dsa_keypair.param.p.size);
892 bn = mpbits(bc->dsa_keypair.param.q.size, bc->dsa_keypair.param.q.modl);
894 *be++ = (bn >> 8); *be++ = (bn );
895 xx = i2osp(be, bn/8, bc->dsa_keypair.param.q.modl, bc->dsa_keypair.param.q.size);
898 bn = mpbits(bc->dsa_keypair.param.g.size, bc->dsa_keypair.param.g.data);
900 *be++ = (bn >> 8); *be++ = (bn );
901 xx = i2osp(be, bn/8, bc->dsa_keypair.param.g.data, bc->dsa_keypair.param.g.size);
904 bn = mpbits(bc->dsa_keypair.y.size, bc->dsa_keypair.y.data);
906 *be++ = (bn >> 8); *be++ = (bn );
907 xx = i2osp(be, bn/8, bc->dsa_keypair.y.data, bc->dsa_keypair.y.size);
917 dig->pub = memcpy(
xmalloc(pktlen), pkt, pktlen);
918 dig->publen = pktlen;
929 time_t now = time(NULL);
938 *be++ = 0x80 | (sigp->tag << 2) | 0x01;
942 *be++ = sigp->version = 0x04;
944 *be++ = sigp->pubkey_algo = pubp->pubkey_algo;
945 *be++ = sigp->hash_algo;
953 *be++ = sigp->time[0] = (bt >> 24);
954 *be++ = sigp->time[1] = (bt >> 16);
955 *be++ = sigp->time[2] = (bt >> 8);
956 *be++ = sigp->time[3] = (bt );
960 bt = 30 * 24 * 60 * 60;
961 *be++ = sigp->expire[0] = (bt >> 24);
962 *be++ = sigp->expire[1] = (bt >> 16);
963 *be++ = sigp->expire[2] = (bt >> 8);
964 *be++ = sigp->expire[3] = (bt );
978 sigp->hashlen = (be - h);
979 h[-2] = (sigp->hashlen >> 8);
980 h[-1] = (sigp->hashlen );
983 if (sigp->hash != NULL)
988 trailer[0] = sigp->version;
990 trailer[2] = (sigp->hashlen >> 24);
991 trailer[3] = (sigp->hashlen >> 16);
992 trailer[4] = (sigp->hashlen >> 8);
993 trailer[5] = (sigp->hashlen );
997 sigp->signhash16[0] = 0x00;
998 sigp->signhash16[1] = 0x00;
1000 h = (uint8_t *) bc->digest;
1001 sigp->signhash16[0] = h[0];
1002 sigp->signhash16[1] = h[1];
1012 *be++ = pubp->signid[0];
1013 *be++ = pubp->signid[1];
1014 *be++ = pubp->signid[2];
1015 *be++ = pubp->signid[3];
1016 *be++ = pubp->signid[4];
1017 *be++ = pubp->signid[5];
1018 *be++ = pubp->signid[6];
1019 *be++ = pubp->signid[7];
1025 *be++ = sigp->signhash16[0];
1026 *be++ = sigp->signhash16[1];
1028 bn = mpbits(bc->r.size, bc->r.data);
1032 xx = i2osp(be, bn/8, bc->r.data, bc->r.size);
1035 bn = mpbits(bc->s.size, bc->s.data);
1039 xx = i2osp(be, bn/8, bc->s.data, bc->s.size);
1042 pktlen = (be - pkt);
1047 dig->sig = memcpy(
xmalloc(pktlen), pkt, pktlen);
1048 dig->siglen = pktlen;
pgpDigParams pgpGetPubkey(pgpDig dig)
Return OpenPGP pubkey parameters.
struct pgpImplVecs_s rpmbcImplVecs
static const char * pgpValStr(pgpValTbl vs, rpmuint8_t val)
Return string representation of am OpenPGP value.
static const char * _pgpHashAlgo2Name(uint32_t algo)
static void * rpmbcFree(void *impl)
struct pgpDigParams_s * pgpDigParams
struct pgpValTbl_s pgpHashTbl[]
Hash (string, value) pairs.
static int rpmbcSetECDSA(DIGEST_CTX ctx, pgpDig dig, pgpDigParams sigp)
static int rpmbcGenerateDSA(pgpDig dig)
static int pgpImplSign(pgpDig dig)
static int rpmbcSign(pgpDig dig)
static int pgpImplSetDSA(DIGEST_CTX ctx, pgpDig dig, pgpDigParams sigp)
static int rpmbcSetRSA(DIGEST_CTX ctx, pgpDig dig, pgpDigParams sigp)
static int rpmbcErrChk(pgpDig dig, const char *msg, int rc, unsigned expected)
void * xcalloc(size_t nmemb, size_t size)
int rpmDigestUpdate(DIGEST_CTX ctx, const void *data, size_t len)
Update context with next plain text buffer.
unsigned char rpmuint8_t
Private int typedefs to avoid C99 portability issues.
static int rpmbcAvailableCipher(pgpDig dig, int algo)
static int rpmbcAvailableDigest(pgpDig dig, int algo)
static unsigned int pgpMpiBits(const rpmuint8_t *p)
Return no.
pgpHashAlgo rpmDigestAlgo(DIGEST_CTX ctx)
Return digest algorithm identifier.
static char * pgpHexCvt(char *t, const rpmuint8_t *s, size_t nbytes)
Convert to hex.
pgpDigParams pgpGetSignature(pgpDig dig)
Return OpenPGP signature parameters.
static void rpmbcClean(void *impl)
static int rpmbcGenerate(pgpDig dig)
#define _spewMPN(_N, _MPN)
static int rpmbcVerifyRSA(pgpDig dig)
int rpmbcExportSignature(pgpDig dig, DIGEST_CTX ctx)
struct pgpValTbl_s pgpPubkeyTbl[]
static int rpmbcSetDSA(DIGEST_CTX ctx, pgpDig dig, pgpDigParams sigp)
static int rpmbcGenerateRSA(pgpDig dig)
static int rpmbcSetELG(DIGEST_CTX ctx, pgpDig dig, pgpDigParams sigp)
static unsigned int pgpMpiLen(const rpmuint8_t *p)
Return no.
static const char * prefix[]
Tables for prefixing and suffixing patterns, according to the -w, -x, and -F options.
int pgpPubkeyFingerprint(const rpmuint8_t *pkt, size_t pktlen, rpmuint8_t *keyid)
Print/parse an OpenPGP subtype packet.
static char * pgpMpiHex(const rpmuint8_t *p)
char * stpcpy(char *dest, const char *src)
static void * rpmbcInit(void)
static void * _free(const void *p)
Wrapper to free(3), hides const compilation noise, permit NULL, return NULL.
#define _spewMPB(_N, _MPB)
#define SPEW(_t, _rc, _dig)
static int rpmbcVerify(pgpDig dig)
const char * rpmDigestASN1(DIGEST_CTX ctx)
Return digest ASN1 oid string.
static int rpmbcMpiItem(const char *pre, pgpDig dig, int itemno, const rpmuint8_t *p, const rpmuint8_t *pend)
int rpmDigestFinal(DIGEST_CTX ctx, void *datap, size_t *lenp, int asAscii)
Return digest and destroy context.
static int rpmbcAvailablePubkey(pgpDig dig, int algo)
static int rpmbcSignRSA(pgpDig dig)
int rpmbcExportPubkey(pgpDig dig)
static const char * _pgpPubkeyAlgo2Name(uint32_t algo)
static int pgpMpiSet(const char *pre, unsigned int lbits, void *dest, const rpmuint8_t *p, const rpmuint8_t *pend)
static int rpmbcSignDSA(pgpDig dig)
static int rpmbcVerifyDSA(pgpDig dig)
static unsigned char nibble(char c)
Convert hex to binary nibble.