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)
72 (protected-at <isotimestamp>)
74 (uri http://foo.bar x-foo:whatever_you_want)
79 In this scheme the encrypted_octet_string is encrypted according to
80 the algorithm described after the keyword protected; most protection
81 algorithms need some parameters, which are given in a list before the
82 encrypted_octet_string. The result of the decryption process is a
83 list of the secret key parameters. The protected-at expression is
84 optional; the isotimestamp is 15 bytes long (e.g. "19610711T172000").
86 The only available protection mode for now is
88 openpgp-s2k3-sha1-aes-cbc
90 which describes an algorithm using using AES in CBC mode for
91 encryption, SHA-1 for integrity protection and the String to Key
92 algorithm 3 from OpenPGP (rfc2440).
96 (protected openpgp-s2k3-sha1-aes-cbc
97 ((sha1 16byte_salt no_of_iterations) 16byte_iv)
98 encrypted_octet_string
101 The encrypted_octet string should yield this S-Exp (in canonical
102 representation) after decryption:
106 (d #046129F..[some bytes not shown]..81#)
107 (p #00e861b..[some bytes not shown]..f1#)
108 (q #00f7a7c..[some bytes not shown]..61#)
109 (u #304559a..[some bytes not shown]..9b#)
111 (hash sha1 #...[hashvalue]...#)
114 For padding reasons, random bytes are appended to this list - they can
115 easily be stripped by looking for the end of the list.
117 The hash is calculated on the concatenation of the public key and
118 secret key parameter lists: i.e it is required to hash the
119 concatenation of these 6 canonical encoded lists for RSA, including
120 the parenthesis, the algorithm keyword and (if used) the protected-at
124 (n #00e0ce9..[some bytes not shown]..51#)
126 (d #046129F..[some bytes not shown]..81#)
127 (p #00e861b..[some bytes not shown]..f1#)
128 (q #00f7a7c..[some bytes not shown]..61#)
129 (u #304559a..[some bytes not shown]..9b#)
130 (protected-at "18950523T000000")
133 After decryption the hash must be recalculated and compared against
134 the stored one - If they don't match the integrity of the key is not
138 Shadowed Private Key Format
139 ============================
140 To keep track of keys stored on IC cards we use a third format for
141 private kyes which are called shadow keys as they are only a reference
142 to keys stored on a token:
144 (shadowed-private-key
146 (n #00e0ce9..[some bytes not shown]..51#)
148 (shadowed protocol (info))
150 (uri http://foo.bar x-foo:whatever_you_want)
154 The currently used protocol is "ti-v1" (token info version 1). The
155 second list with the information has this layout:
157 (card_serial_number id_string_of_key)
159 More items may be added to the list.
168 [1] I usually use the terms private and secret key exchangeable but prefer the
169 term secret key because it can be visually be better distinguished
170 from the term public key.
172 [2] The keygrip is a unique identifier for a key pair, it is
173 independent of any protocol, so that the same key can be used with
174 different protocols. PKCS-15 calls this a subjectKeyHash; it can be
175 calculated using Libgcrypt's gcry_pk_get_keygrip ().
177 [3] Even when canonical representation are required we will show the
178 S-expression here in a more readable representation.