root/openpgpsdk/trunk/src/fingerprint.c

Revision 371 (checked in by ben, 7 years ago)

Make sure dmalloc happens last.

Line 
1 /** \file
2  */
3
4 #include <openpgpsdk/packet.h>
5 #include <openpgpsdk/crypto.h>
6 #include <openpgpsdk/create.h>
7 #include <assert.h>
8 #include <string.h>
9
10 #include <openpgpsdk/configure.h>
11 #ifdef HAVE_ALLOCA_H
12 # include <alloca.h>
13 #endif
14
15 #include <openpgpsdk/final.h>
16
17 /**
18  * \ingroup Utils
19  *
20  * Calculate a public key fingerprint.
21  *
22  * \param fp Where to put the calculated fingerprint
23  * \param key The key for which the fingerprint is calculated
24  */
25
26 void ops_fingerprint(ops_fingerprint_t *fp,const ops_public_key_t *key)
27     {
28     if(key->version == 2 || key->version == 3)
29         {
30         unsigned char *bn;
31         int n;
32         ops_hash_t md5;
33
34         assert(key->algorithm == OPS_PKA_RSA
35                || key->algorithm == OPS_PKA_RSA_ENCRYPT_ONLY
36                || key->algorithm == OPS_PKA_RSA_SIGN_ONLY );
37
38         ops_hash_md5(&md5);
39         md5.init(&md5);
40
41         n=BN_num_bytes(key->key.rsa.n);
42         bn=alloca(n);
43         BN_bn2bin(key->key.rsa.n,bn);
44         md5.add(&md5,bn,n);
45
46         n=BN_num_bytes(key->key.rsa.e);
47         bn=alloca(n);
48         BN_bn2bin(key->key.rsa.e,bn);
49         md5.add(&md5,bn,n);
50
51         md5.finish(&md5,fp->fingerprint);
52         fp->length=16;
53         }
54     else
55         {
56         ops_memory_t *mem=ops_memory_new();
57         ops_hash_t sha1;
58         size_t l;
59
60         ops_build_public_key(mem,key,ops_false);
61
62         ops_hash_sha1(&sha1);
63         sha1.init(&sha1);
64
65         l=ops_memory_get_length(mem);
66
67         ops_hash_add_int(&sha1,0x99,1);
68         ops_hash_add_int(&sha1,l,2);
69         sha1.add(&sha1,ops_memory_get_data(mem),l);
70         sha1.finish(&sha1,fp->fingerprint);
71
72         fp->length=20;
73
74         ops_memory_free(mem);
75         }
76     }
77
78 /**
79  * \ingroup Utils
80  *
81  * Calculate the Key ID from the public key.
82  *
83  * \param keyid Space for the calculated ID to be stored
84  * \param key The key for which the ID is calculated
85  */
86
87 void ops_keyid(unsigned char keyid[8],const ops_public_key_t *key)
88     {
89     if(key->version == 2 || key->version == 3)
90         {
91         unsigned char bn[8192];
92         unsigned n=BN_num_bytes(key->key.rsa.n);
93
94         assert(n <= sizeof bn);
95         assert(key->algorithm == OPS_PKA_RSA
96                || key->algorithm == OPS_PKA_RSA_ENCRYPT_ONLY
97                || key->algorithm == OPS_PKA_RSA_SIGN_ONLY );
98         BN_bn2bin(key->key.rsa.n,bn);
99         memcpy(keyid,bn+n-8,8);
100         }
101     else
102         {
103         ops_fingerprint_t fingerprint;
104
105         ops_fingerprint(&fingerprint,key);
106         memcpy(keyid,fingerprint.fingerprint+fingerprint.length-8,8);
107         }
108     }
Note: See TracBrowser for help on using the browser.