cryptlib  3.4.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros
pgp.h
Go to the documentation of this file.
1 /****************************************************************************
2 * *
3 * PGP Definitions Header File *
4 * Copyright Peter Gutmann 1996-2007 *
5 * *
6 ****************************************************************************/
7 
8 #ifndef _PGP_DEFINED
9 
10 #define _PGP_DEFINED
11 
12 #ifndef _STREAM_DEFINED
13  #if defined( INC_ALL )
14  #include "stream.h"
15  #else
16  #include "io/stream.h"
17  #endif /* Compiler-specific includes */
18 #endif /* _STREAM_DEFINED */
19 
20 /* PGP packet types, encoded into the CTB */
21 
22 typedef enum {
23  PGP_PACKET_NONE, /* No packet type */
24  PGP_PACKET_PKE, /* PKC-encrypted session key */
25  PGP_PACKET_SIGNATURE, /* Signature */
26  PGP_PACKET_SKE, /* Secret-key-encrypted session key */
27  PGP_PACKET_SIGNATURE_ONEPASS,/* One-pass signature */
28  PGP_PACKET_SECKEY, /* Secret key */
29  PGP_PACKET_PUBKEY, /* Public key */
30  PGP_PACKET_SECKEY_SUB, /* Secret key subkey */
31  PGP_PACKET_COPR, /* Compressed data */
32  PGP_PACKET_ENCR, /* Encrypted data */
33  PGP_PACKET_MARKER, /* Obsolete marker packet */
34  PGP_PACKET_DATA, /* Raw data */
35  PGP_PACKET_TRUST, /* Trust information */
36  PGP_PACKET_USERID, /* Userid */
37  PGP_PACKET_PUBKEY_SUB, /* Public key subkey */
38  PGP_PACKET_DUMMY1, PGP_PACKET_DUMMY2, /* 15, 16 unused */
39  PGP_PACKET_USERATTR, /* User attributes */
40  PGP_PACKET_ENCR_MDC, /* Encrypted data with MDC */
41  PGP_PACKET_MDC, /* MDC */
42  PGP_PACKET_LAST /* Last possible PGP packet type */
44 
45 /* PGP signature subpacket types */
46 
47 #define PGP_SUBPACKET_TIME 2 /* Signing time */
48 #define PGP_SUBPACKET_KEYID 16 /* Key ID */
49 #define PGP_SUBPACKET_TYPEANDVALUE 20 /* Type-and-value pairs */
50 #define PGP_SUBPACKET_LAST 29 /* Last valid subpacket type */
51 
52 /* A special-case packet type that denotes a signature that follows on from
53  a one-pass signature packet. When generating a signature of this type PGP
54  splits the information in the normal signature packet across the one-pass
55  signature packet and the signature packet itself, so we have to read the
56  data on two parts, with half the information in the one-pass packet and
57  the other half in the signature packet */
58 
59 #define PGP_PACKET_SIGNATURE_SPECIAL 1000
60 
61 /* The PGP packet format (via the CTB) is:
62 
63  +---------------+
64  |7 6 5 4 3 2 1 0|
65  +---------------+
66 
67  Bit 7: Always one
68  Bit 6: OpenPGP (new) format if set
69 
70  PGP 2.x: OpenPGP:
71  Bits 5-2: Packet type Bits 5-0: Packet type
72  Bits 1-0: Length type
73 
74  All CTBs have the MSB set, and OpenPGP CTBs have the next-to-MSB set. We
75  also have a special-case CTB that's used for indefinite-length compressed
76  data */
77 
78 #define PGP_CTB 0x80 /* PGP 2.x CTB template */
79 #define PGP_CTB_OPENPGP 0xC0 /* OpenPGP CTB template */
80 #define PGP_CTB_COMPRESSED 0xA3 /* Compressed indef-length data */
81 
82 /* Macros to extract packet information from the CTB */
83 
84 #define pgpIsCTB( ctb ) ( ( ctb ) & PGP_CTB )
85 #define pgpGetPacketVersion( ctb ) \
86  ( ( ( ( ctb ) & PGP_CTB_OPENPGP ) == PGP_CTB_OPENPGP ) ? \
87  PGP_VERSION_OPENPGP : PGP_VERSION_2 )
88 #define pgpGetPacketType( ctb ) \
89  ( ( ( ( ctb ) & PGP_CTB_OPENPGP ) == PGP_CTB_OPENPGP ) ? \
90  ( ( ctb ) & 0x3F ) : ( ( ( ctb ) >> 2 ) & 0x0F ) )
91 #define pgpIsReservedPacket( type ) ( ( type ) >= 60 && ( type ) <= 63 )
92 
93 /* Version information */
94 
95 #define PGP_VERSION_2 2 /* Version number byte for PGP 2.0 */
96 #define PGP_VERSION_3 3 /* Version number byte for legal-kludged PGP 2.0 */
97 #define PGP_VERSION_OPENPGP 4 /* Version number for OpenPGP */
98 
99 /* Public-key algorithms */
100 
101 #define PGP_ALGO_RSA 1 /* RSA */
102 #define PGP_ALGO_RSA_ENCRYPT 2 /* RSA encrypt-only */
103 #define PGP_ALGO_RSA_SIGN 3 /* RSA sign-only */
104 #define PGP_ALGO_ELGAMAL 16 /* ElGamal */
105 #define PGP_ALGO_DSA 17 /* DSA */
106 #define PGP_ALGO_ECC_RES 18 /* Reserved for "ECC" */
107 #define PGP_ALGO_ECDSA_RES 19 /* Reserved for ECDSA */
108 #define PGP_ALGO_PKC_RES1 20 /* Reserved, formerly Elgamal sign */
109 #define PGP_ALGO_PKC_RES2 21 /* Reserved for "X9.42" */
110 
111 /* Conventional encryption algorithms */
112 
113 #define PGP_ALGO_NONE 0 /* No CKE algorithm */
114 #define PGP_ALGO_IDEA 1 /* IDEA */
115 #define PGP_ALGO_3DES 2 /* Triple DES */
116 #define PGP_ALGO_CAST5 3 /* CAST-128 */
117 #define PGP_ALGO_BLOWFISH 4 /* Blowfish */
118 #define PGP_ALGO_ENC_RES1 5 /* Reserved, formerly Safer-SK */
119 #define PGP_ALGO_ENC_RES2 6 /* Reserved */
120 #define PGP_ALGO_AES_128 7 /* AES with 128-bit key */
121 #define PGP_ALGO_AES_192 8 /* AES with 192-bit key */
122 #define PGP_ALGO_AES_256 9 /* AES with 256-bit key */
123 #define PGP_ALGO_TWOFISH 10 /* Twofish */
124 
125 /* Hash algorithms */
126 
127 #define PGP_ALGO_MD5 1 /* MD5 */
128 #define PGP_ALGO_SHA 2 /* SHA-1 */
129 #define PGP_ALGO_RIPEMD160 3 /* RIPEMD-160 */
130 #define PGP_ALGO_HASH_RES1 4 /* Reserved */
131 #define PGP_ALGO_HASH_RES2 5 /* Reserved, formerly MD2 */
132 #define PGP_ALGO_HASH_RES3 6 /* Reserved, formerly Tiger/192 */
133 #define PGP_ALGO_HASH_RES4 7 /* Reserved, formerly Haval */
134 #define PGP_ALGO_SHA2_256 8 /* SHA-2 256bit */
135 #define PGP_ALGO_SHA2_384 9 /* SHA-2 384bit */
136 #define PGP_ALGO_SHA2_512 10 /* SHA-2 512bit */
137 #define PGP_ALGO_SHA2_224 11 /* SHA-2 224bit */
138 
139 /* Compression algorithms */
140 
141 #define PGP_ALGO_ZIP 1 /* ZIP compression */
142 #define PGP_ALGO_ZLIB 2 /* zlib compression */
143 #define PGP_ALGO_BZIP2 3 /* Bzip2 compression */
144 
145 /* Highest possible algorithm value, for range checking */
146 
147 #define PGP_ALGO_LAST PGP_ALGO_DSA
148 
149 /* S2K specifier */
150 
151 #define PGP_S2K 0xFF /* Standard S2K */
152 #define PGP_S2K_HASHED 0xFE /* S2K with hashed key */
153 
154 /* Signature types */
155 
156 #define PGP_SIG_NONE 0x00 /* Same as PGP_SIG_DATA, for range chk.*/
157 #define PGP_SIG_DATA 0x00 /* Binary data */
158 #define PGP_SIG_TEXT 0x01 /* Canonicalised text data */
159 #define PGP_SIG_STANDALONE 0x02 /* Unknown purpose, from RFC 4880 */
160 #define PGP_SIG_CERT0 0x10 /* Key certificate, unknown assurance */
161 #define PGP_SIG_CERT1 0x11 /* Key certificate, no assurance */
162 #define PGP_SIG_CERT2 0x12 /* Key certificate, casual assurance */
163 #define PGP_SIG_CERT3 0x13 /* Key certificate, strong assurance */
164 #define PGP_SIG_SUBKEY 0x18 /* Subkey binding signature */
165 #define PGP_SIG_PRIMKEY 0x19 /* Primary key binding signature */
166 #define PGP_SIG_DIRECTKEY 0x1F /* Key self-signature */
167 #define PGP_SIG_KEYREV 0x20 /* Key revocation */
168 #define PGP_SIG_SUBKEYREV 0x28 /* Subkey revocation */
169 #define PGP_SIG_CRL 0x30 /* Certificate revocation */
170 #define PGP_SIG_TS 0x40 /* Timestamp signature */
171 #define PGP_SIG_COUNTERSIG 0x50 /* Third-party countersignature */
172 #define PGP_SIG_LAST 0x51 /* Last possible signature type */
173 
174 /* The size of the PGP version ID and algorithm ID */
175 
176 #define PGP_VERSION_SIZE 1
177 #define PGP_ALGOID_SIZE 1
178 
179 /* The maximum size of a PGP user ID. Note that this is larger than the
180  cryptlib-wide maximum user ID size */
181 
182 #define PGP_MAX_USERIDSIZE 256
183 
184 /* The size of the salt used for password hashing and the number of
185  setup "iterations". This isn't a true iteration count but the number of
186  salt+password bytes hashed, and in fact it isn't even that but the
187  actual count scaled by dividing it by 64, which is how PGP encodes the
188  count in the data packet */
189 
190 #define PGP_SALTSIZE 8
191 #define PGP_ITERATIONS 1024
192 
193 /* Various PGP packet header sizes, used to estimate how much data we still
194  need to process */
195 
196 #define PGP_MIN_HEADER_SIZE 2 /* CTB + length */
197 #define PGP_MAX_HEADER_SIZE 6 /* CTB + 0xFF + 4-byte length */
198 #define PGP_DATA_HEADER "b\x00\x00\x00\x00\x00"
199 #define PGP_DATA_HEADER_SIZE ( 1 + 1 + 4 )
200 #define PGP_MDC_PACKET_SIZE ( 1 + 1 + 20 ) /* Size of MDC packet */
201 
202 /* Since PGP only provides a subset of cryptlib's algorithm types and uses
203  different identifiers, we have to both check that there's a mapping
204  possible and map from one to the other. When going from PGP -> cryptlib
205  we specify both the algorithm ID and the algorithm class we expect to
206  find it in to allow type checking */
207 
208 typedef enum {
209  PGP_ALGOCLASS_NONE, /* No algorithm class */
210  PGP_ALGOCLASS_CRYPT, /* Conventional encryption algorithms */
211  PGP_ALGOCLASS_PWCRYPT, /* Password-based encryption algorithms */
212  PGP_ALGOCLASS_PKCCRYPT, /* PKC algorithms */
213  PGP_ALGOCLASS_SIGN, /* Signature algorithms */
214  PGP_ALGOCLASS_HASH, /* Hash algorithms */
215  PGP_ALGOCLASS_LAST /* Last possible algorithm class */
217 
219 int pgpToCryptlibAlgo( IN_RANGE( PGP_ALGO_NONE, 0xFF ) \
220  const int pgpAlgo,
221  IN_ENUM( PGP_ALGOCLASS ) \
222  const PGP_ALGOCLASS_TYPE pgpAlgoClass,
225 int cryptlibToPgpAlgo( IN_ALGO const CRYPT_ALGO_TYPE cryptlibAlgo,
227  int *pgpAlgo );
228 CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
229 int readPgpAlgo( INOUT STREAM *stream,
231  IN_ENUM( PGP_ALGOCLASS ) \
232  const PGP_ALGOCLASS_TYPE pgpAlgoClass );
233 
234 /* Prototypes for functions in pgp_misc.c */
235 
237 int pgpPasswordToKey( IN_HANDLE const CRYPT_CONTEXT iCryptContext,
240  const char *password,
243  IN_BUFFER_OPT( saltSize )
244  const BYTE *salt,
246  const int saltSize,
247  IN_INT const int iterations );
249 int pgpProcessIV( IN_HANDLE const CRYPT_CONTEXT iCryptContext,
250  INOUT_BUFFER_FIXED( ivInfoSize ) \
251  BYTE *ivInfo,
252  IN_RANGE( 8 + 2, CRYPT_MAX_IVSIZE + 2 ) \
253  const int ivInfoSize,
255  const BOOLEAN isEncrypt,
256  const BOOLEAN resyncIV );
257 
258 #endif /* _PGP_DEFINED */