1 keyformat.txt (wk 2001-12-18)
2 -----------------------------
5 Some notes on the format of the secret keys used with gpg-agent.
9 The secret keys[1] are stored on a per file basis in a directory below
10 the ~/.gnupg home directory. This directory is named
14 and should have permissions 700.
16 The secret keys are stored in files with a name matching the
17 hexadecimal representation of the keygrip[2].
19 Unprotected Private Key Format
20 ==============================
21 The content of the file is an S-Expression like the ones used with
22 Libgcrypt. Here is an example of an unprotected file:
26 (n #00e0ce9..[some bytes not shown]..51#)
28 (d #046129F..[some bytes not shown]..81#)
29 (p #00e861b..[some bytes not shown]..f1#)
30 (q #00f7a7c..[some bytes not shown]..61#)
31 (u #304559a..[some bytes not shown]..9b#)
33 (created-at timestamp)
34 (uri http://foo.bar x-foo:whatever_you_want)
38 "comment", "created-at" and "uri" are optional. "comment" is
39 currently used to keep track of ssh key comments. "created-at" is used
40 to keep track of the creation time stamp used with OpenPGP keys; it is
41 optional but required for some operations to calculate the fingerprint
42 of the key. This timestamp should be a string with the number of
43 seconds since Epoch or an ISO time string (yyyymmddThhmmss).
45 Actually this form should not be used for regular purposes and only
46 accepted by gpg-agent with the configuration option:
47 --allow-non-canonical-key-format. The regular way to represent the
48 keys is in canonical representation[3]:
52 (n #00e0ce9..[some bytes not shown]..51#)
54 (d #046129F..[some bytes not shown]..81#)
55 (p #00e861b..[some bytes not shown]..f1#)
56 (q #00f7a7c..[some bytes not shown]..61#)
57 (u #304559a..[some bytes not shown]..9b#)
59 (uri http://foo.bar x-foo:whatever_you_want)
63 Protected Private Key Format
64 ==============================
65 A protected key is like this:
67 (protected-private-key
69 (n #00e0ce9..[some bytes not shown]..51#)
71 (protected mode (parms) encrypted_octet_string)
73 (uri http://foo.bar x-foo:whatever_you_want)
78 In this scheme the encrypted_octet_string is encrypted according to
79 the algorithm described after the keyword protected; most protection
80 algorithms need some parameters, which are given in a list before the
81 encrypted_octet_string. The result of the decryption process is a
82 list of the secret key parameters.
84 The only available protection mode for now is
86 openpgp-s2k3-sha1-aes-cbc
88 which describes an algorithm using using AES in CBC mode for
89 encryption, SHA-1 for integrity protection and the String to Key
90 algorithm 3 from OpenPGP (rfc2440).
94 (protected openpgp-s2k3-sha1-aes-cbc
95 ((sha1 16byte_salt no_of_iterations) 16byte_iv)
96 encrypted_octet_string
99 The encrypted_octet string should yield this S-Exp (in canonical
100 representation) after decryption:
104 (d #046129F..[some bytes not shown]..81#)
105 (p #00e861b..[some bytes not shown]..f1#)
106 (q #00f7a7c..[some bytes not shown]..61#)
107 (u #304559a..[some bytes not shown]..9b#)
109 (hash sha1 #...[hashvalue]...#)
112 For padding reasons, random bytes are appended to this list - they can
113 easily be stripped by looking for the end of the list.
115 The hash is calculated on the concatenation of the public key and
116 secret key parameter lists: i.e it is required to hash the
117 concatenation of these 6 canonical encoded lists for RSA, including
118 the parenthesis and the algorithm keyword.
121 (n #00e0ce9..[some bytes not shown]..51#)
123 (d #046129F..[some bytes not shown]..81#)
124 (p #00e861b..[some bytes not shown]..f1#)
125 (q #00f7a7c..[some bytes not shown]..61#)
126 (u #304559a..[some bytes not shown]..9b#)
129 After decryption the hash must be recalculated and compared against
130 the stored one - If they don't match the integrity of the key is not
134 Shadowed Private Key Format
135 ============================
136 To keep track of keys stored on IC cards we use a third format for
137 private kyes which are called shadow keys as they are only a reference
138 to keys stored on a token:
140 (shadowed-private-key
142 (n #00e0ce9..[some bytes not shown]..51#)
144 (shadowed protocol (info))
146 (uri http://foo.bar x-foo:whatever_you_want)
150 The currently used protocol is "ti-v1" (token info version 1). The
151 second list with the information has this layout:
153 (card_serial_number id_string_of_key)
155 More items may be added to the list.
164 [1] I usually use the terms private and secret key exchangeable but prefer the
165 term secret key because it can be visually be better distinguished
166 from the term public key.
168 [2] The keygrip is a unique identifier for a key pair, it is
169 independent of any protocol, so that the same key can be used with
170 different protocols. PKCS-15 calls this a subjectKeyHash; it can be
171 calculated using Libgcrypt's gcry_pk_get_keygrip ().
173 [3] Even when canonical representation are required we will show the
174 S-expression here in a more readable representation.