2 * keyid.c - Routines to calculate key IDs.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2002 Project Purple
12 #include "keystructs.h"
17 * get_keyid - Given a public key returns the keyid.
18 * @publickey: The key to calculate the fingerprint for.
20 uint64_t get_keyid(struct openpgp_publickey *publickey)
27 unsigned char *buff = NULL;
29 assert(publickey != NULL);
31 switch (publickey->publickey->data[0]) {
35 * For a type 2 or 3 key the keyid is the last 64 bits of the
36 * public modulus n, which is stored as an MPI from offset 8
39 * We need to ensure it's an RSA key.
41 if (publickey->publickey->data[7] == 1) {
42 offset = (publickey->publickey->data[8] << 8) +
43 publickey->publickey->data[9];
44 offset = ((offset + 7) / 8) + 2;
46 for (keyid = 0, i = 0; i < 8; i++) {
48 keyid += publickey->publickey->data[offset++];
51 fputs("Type 2 or 3 key, but not RSA.\n", stderr);
56 * For a type 4 key the keyid is the last 64 bits of the
57 * fingerprint, which is the 160 bit SHA-1 hash of the packet
58 * tag, 2 octet packet length and the public key packet
59 * including version field.
63 * TODO: Can this be 0x99? Are all public key packets old
64 * format with 2 bytes of length data?
67 sha1_write(&sha_ctx, &c, sizeof(c));
68 c = publickey->publickey->length >> 8;
69 sha1_write(&sha_ctx, &c, sizeof(c));
70 c = publickey->publickey->length & 0xFF;
71 sha1_write(&sha_ctx, &c, sizeof(c));
72 sha1_write(&sha_ctx, publickey->publickey->data,
73 publickey->publickey->length);
75 buff = sha1_read(&sha_ctx);
79 for (keyid = 0, i = 12; i < 20; i++) {
86 fprintf(stderr, "Unknown key type: %d\n",
87 publickey->publickey->data[0]);