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 switch (publickey->publickey->data[0]) {
33 * For a type 2 or 3 key the keyid is the last 64 bits of the
34 * public modulus n, which is stored as an MPI from offset 8
37 * We need to ensure it's an RSA key.
39 if (publickey->publickey->data[7] == 1) {
40 offset = (publickey->publickey->data[8] << 8) +
41 publickey->publickey->data[9];
42 offset = ((offset + 7) / 8) + 2;
44 for (keyid = 0, i = 0; i < 8; i++) {
46 keyid += publickey->publickey->data[offset++];
49 fputs("Type 2 or 3 key, but not RSA.\n", stderr);
54 * For a type 4 key the keyid is the last 64 bits of the
55 * fingerprint, which is the 160 bit SHA-1 hash of the packet
56 * tag, 2 octet packet length and the public key packet
57 * including version field.
61 * TODO: Can this be 0x99? Are all public key packets old
62 * format with 2 bytes of length data?
65 sha1_write(&sha_ctx, &c, sizeof(c));
66 c = publickey->publickey->length >> 8;
67 sha1_write(&sha_ctx, &c, sizeof(c));
68 c = publickey->publickey->length & 0xFF;
69 sha1_write(&sha_ctx, &c, sizeof(c));
70 sha1_write(&sha_ctx, publickey->publickey->data,
71 publickey->publickey->length);
73 buff = sha1_read(&sha_ctx);
77 for (keyid = 0, i = 12; i < 20; i++) {
84 fprintf(stderr, "Unknown key type: %d\n",
85 publickey->publickey->data[0]);