Avoid redundant calculations on pubkey fingerprint retrieval
authorPanu Matilainen <pmatilai@redhat.com>
Tue, 25 Oct 2011 11:22:07 +0000 (14:22 +0300)
committerPanu Matilainen <pmatilai@redhat.com>
Tue, 25 Oct 2011 11:22:07 +0000 (14:22 +0300)
- In pgpPrtPkt() we just calculated the packet body and length,
  avoid redoing it for the fingerprint by splitting the actual
  fingerprint calculation out of pgpPubkeyFingerprint() into a helper
  function and calling that instead.

rpmio/rpmpgp.c

index 31ddbb1..0a3c858 100644 (file)
@@ -1048,27 +1048,10 @@ static int pgpPrtComment(pgpTag tag, const uint8_t *h, size_t hlen)
     return 0;
 }
 
-int pgpPubkeyFingerprint(const uint8_t * pkt, size_t pktlen, pgpKeyID_t keyid)
+static int getFingerprint(const uint8_t *h, size_t hlen, pgpKeyID_t keyid)
 {
-    unsigned int val = *pkt;
-    size_t plen, hlen;
-    const uint8_t *se, *h;
-    DIGEST_CTX ctx;
-    int rc = -1;       /* assume failure. */
-
-    if (!(val & 0x80) || pktlen < 2)
-       return rc;
-
-    if (val & 0x40) {
-       plen = pgpLen(pkt+1, &hlen);
-    } else {
-       plen = (1 << (val & 0x3));
-       hlen = pgpGrab(pkt+1, plen);
-    }
-    if (pktlen > 0 && 1 + plen + hlen > pktlen)
-       return rc;
-    
-    h = pkt + 1 + plen;
+    int rc = -1; /* assume failure */
+    const uint8_t *se;
 
     switch (h[0]) {
     case 3:
@@ -1086,6 +1069,7 @@ int pgpPubkeyFingerprint(const uint8_t * pkt, size_t pktlen, pgpKeyID_t keyid)
       }        break;
     case 4:
       {        pgpPktKeyV4 v = (pgpPktKeyV4) (h);
+       DIGEST_CTX ctx;
        uint8_t * d = NULL;
        uint8_t in[3];
        size_t dlen;
@@ -1123,6 +1107,27 @@ int pgpPubkeyFingerprint(const uint8_t * pkt, size_t pktlen, pgpKeyID_t keyid)
     return rc;
 }
 
+int pgpPubkeyFingerprint(const uint8_t * pkt, size_t pktlen, pgpKeyID_t keyid)
+{
+    unsigned int val = *pkt;
+    size_t plen, hlen;
+    int rc = -1;       /* assume failure. */
+
+    if (!(val & 0x80) || pktlen < 2)
+       return rc;
+
+    if (val & 0x40) {
+       plen = pgpLen(pkt+1, &hlen);
+    } else {
+       plen = (1 << (val & 0x3));
+       hlen = pgpGrab(pkt+1, plen);
+    }
+    if (pktlen > 0 && 1 + plen + hlen > pktlen)
+       return rc;
+    
+    return getFingerprint(pkt + 1 + plen, hlen, keyid);
+}
+
 int pgpExtractPubkeyFingerprint(const char * b64pkt, pgpKeyID_t keyid)
 {
     uint8_t * pkt;
@@ -1175,7 +1180,7 @@ static int pgpPrtPkt(const uint8_t *pkt, size_t pleft,
     case PGPTAG_PUBLIC_KEY:
        /* Get the public key fingerprint. */
        if (_digp) {
-           if (!pgpPubkeyFingerprint(pkt, pktlen, _digp->signid))
+           if (!getFingerprint(h, hlen, _digp->signid))
                _digp->saved |= PGPDIG_SAVED_ID;
            else
                memset(_digp->signid, 0, sizeof(_digp->signid));