Factor bounds checking annotations into source code.
[platform/upstream/rpm.git] / rpmio / rpmpgp.h
1 #ifndef H_RPMPGP
2 #define H_RPMPGP
3 /*@-bounds@*/
4
5 /** \ingroup rpmio
6  * \file rpmio/rpmpgp.h
7  *
8  * OpenPGP constants and structures from RFC-2440.
9  *
10  * Text from RFC-2440 in comments is
11  *      Copyright (C) The Internet Society (1998).  All Rights Reserved.
12  */
13
14 #include "types.h"
15 #include "base64.h"
16 #include "dsa.h"
17 #include "endianness.h"
18 #include "mp32.h"
19 #include "rsa.h"
20 #include "rsapk.h"
21
22 /**
23  */
24 typedef /*@abstract@*/ struct DIGEST_CTX_s * DIGEST_CTX;
25
26 /**
27  */
28 typedef const struct pgpValTbl_s {
29     int val;
30 /*@observer@*/ const char * str;
31 } * pgpValTbl;
32  
33 /**
34  * 4.3. Packet Tags
35  * 
36  * The packet tag denotes what type of packet the body holds. Note that
37  * old format headers can only have tags less than 16, whereas new
38  * format headers can have tags as great as 63.
39  */
40 typedef enum pgpTag_e {
41     PGPTAG_RESERVED             =  0, /*!< Reserved/Invalid */
42     PGPTAG_PUBLIC_SESSION_KEY   =  1, /*!< Public-Key Encrypted Session Key */
43     PGPTAG_SIGNATURE            =  2, /*!< Signature */
44     PGPTAG_SYMMETRIC_SESSION_KEY=  3, /*!< Symmetric-Key Encrypted Session Key*/
45     PGPTAG_ONEPASS_SIGNATURE    =  4, /*!< One-Pass Signature */
46     PGPTAG_SECRET_KEY           =  5, /*!< Secret Key */
47     PGPTAG_PUBLIC_KEY           =  6, /*!< Public Key */
48     PGPTAG_SECRET_SUBKEY        =  7, /*!< Secret Subkey */
49     PGPTAG_COMPRESSED_DATA      =  8, /*!< Compressed Data */
50     PGPTAG_SYMMETRIC_DATA       =  9, /*!< Symmetrically Encrypted Data */
51     PGPTAG_MARKER               = 10, /*!< Marker */
52     PGPTAG_LITERAL_DATA         = 11, /*!< Literal Data */
53     PGPTAG_TRUST                = 12, /*!< Trust */
54     PGPTAG_USER_ID              = 13, /*!< User ID */
55     PGPTAG_PUBLIC_SUBKEY        = 14, /*!< Public Subkey */
56     PGPTAG_COMMENT_OLD          = 16, /*!< Comment (from OpenPGP draft) */
57     PGPTAG_PHOTOID              = 17, /*!< PGP's photo ID */
58     PGPTAG_ENCRYPTED_MDC        = 18, /*!< Integrity protected encrypted data */
59     PGPTAG_MDC                  = 19, /*!< Manipulaion detection code packet */
60     PGPTAG_PRIVATE_60           = 60, /*!< Private or Experimental Values */
61     PGPTAG_COMMENT              = 61, /*!< Comment */
62     PGPTAG_PRIVATE_62           = 62, /*!< Private or Experimental Values */
63     PGPTAG_CONTROL              = 63 /*!< Control (GPG) */
64 } pgpTag;
65
66 /**
67  */
68 /*@observer@*/ /*@unchecked@*/ /*@unused@*/
69 extern struct pgpValTbl_s pgpTagTbl[];
70
71 /**
72  * 5.1. Public-Key Encrypted Session Key Packets (Tag 1)
73  *
74  * A Public-Key Encrypted Session Key packet holds the session key used
75  * to encrypt a message. Zero or more Encrypted Session Key packets
76  * (either Public-Key or Symmetric-Key) may precede a Symmetrically
77  * Encrypted Data Packet, which holds an encrypted message.  The message
78  * is encrypted with the session key, and the session key is itself
79  * encrypted and stored in the Encrypted Session Key packet(s).  The
80  * Symmetrically Encrypted Data Packet is preceded by one Public-Key
81  * Encrypted Session Key packet for each OpenPGP key to which the
82  * message is encrypted.  The recipient of the message finds a session
83  * key that is encrypted to their public key, decrypts the session key,
84  * and then uses the session key to decrypt the message.
85  *
86  * The body of this packet consists of:
87  *   - A one-octet number giving the version number of the packet type.
88  *     The currently defined value for packet version is 3. An
89  *     implementation should accept, but not generate a version of 2,
90  *     which is equivalent to V3 in all other respects.
91  *   - An eight-octet number that gives the key ID of the public key
92  *     that the session key is encrypted to.
93  *   - A one-octet number giving the public key algorithm used.
94  *   - A string of octets that is the encrypted session key. This string
95  *     takes up the remainder of the packet, and its contents are
96  *     dependent on the public key algorithm used.
97  *
98  * Algorithm Specific Fields for RSA encryption
99  *   - multiprecision integer (MPI) of RSA encrypted value m**e mod n.
100  *
101  * Algorithm Specific Fields for Elgamal encryption:
102  *   - MPI of Elgamal (Diffie-Hellman) value g**k mod p.
103  *   - MPI of Elgamal (Diffie-Hellman) value m * y**k mod p.
104  */
105 typedef struct pgpPktPubkey_s {
106     byte version;       /*!< version number (generate 3, accept 2). */
107     byte keyid[8];      /*!< key ID of the public key for session key. */
108     byte algo;          /*!< public key algorithm used. */
109 } pgpPktPubkey;
110
111
112 /**
113  * 5.2.1. Signature Types
114  * 
115  * There are a number of possible meanings for a signature, which are
116  * specified in a signature type octet in any given signature.
117  */
118 /*@-typeuse@*/
119 typedef enum pgpSigType_e {
120     PGPSIGTYPE_BINARY            = 0x00, /*!< Binary document */
121     PGPSIGTYPE_TEXT              = 0x01, /*!< Canonical text document */
122     PGPSIGTYPE_STANDALONE        = 0x02, /*!< Standalone */
123     PGPSIGTYPE_GENERIC_CERT      = 0x10,
124                 /*!< Generic certification of a User ID & Public Key */
125     PGPSIGTYPE_PERSONA_CERT      = 0x11,
126                 /*!< Persona certification of a User ID & Public Key */
127     PGPSIGTYPE_CASUAL_CERT       = 0x12,
128                 /*!< Casual certification of a User ID & Public Key */
129     PGPSIGTYPE_POSITIVE_CERT     = 0x13,
130                 /*!< Positive certification of a User ID & Public Key */
131     PGPSIGTYPE_SUBKEY_BINDING    = 0x18, /*!< Subkey Binding */
132     PGPSIGTYPE_SIGNED_KEY        = 0x1F, /*!< Signature directly on a key */
133     PGPSIGTYPE_KEY_REVOKE        = 0x20, /*!< Key revocation */
134     PGPSIGTYPE_SUBKEY_REVOKE     = 0x28, /*!< Subkey revocation */
135     PGPSIGTYPE_CERT_REVOKE       = 0x30, /*!< Certification revocation */
136     PGPSIGTYPE_TIMESTAMP         = 0x40  /*!< Timestamp */
137 } pgpSigType;
138 /*@=typeuse@*/
139
140 /**
141  */
142 /*@observer@*/ /*@unchecked@*/ /*@unused@*/
143 extern struct pgpValTbl_s pgpSigTypeTbl[];
144
145 /**
146  * 9.1. Public Key Algorithms
147  *
148 \verbatim
149        ID           Algorithm
150        --           ---------
151        1          - RSA (Encrypt or Sign)
152        2          - RSA Encrypt-Only
153        3          - RSA Sign-Only
154        16         - Elgamal (Encrypt-Only), see [ELGAMAL]
155        17         - DSA (Digital Signature Standard)
156        18         - Reserved for Elliptic Curve
157        19         - Reserved for ECDSA
158        20         - Elgamal (Encrypt or Sign)
159        21         - Reserved for Diffie-Hellman (X9.42,
160                     as defined for IETF-S/MIME)
161        100 to 110 - Private/Experimental algorithm.
162 \endverbatim
163  *
164  * Implementations MUST implement DSA for signatures, and Elgamal for
165  * encryption. Implementations SHOULD implement RSA keys.
166  * Implementations MAY implement any other algorithm.
167  */
168 /*@-typeuse@*/
169 typedef enum pgpPubkeyAlgo_e {
170     PGPPUBKEYALGO_RSA           =  1,   /*!< RSA */
171     PGPPUBKEYALGO_RSA_ENCRYPT   =  2,   /*!< RSA(Encrypt-Only) */
172     PGPPUBKEYALGO_RSA_SIGN      =  3,   /*!< RSA(Sign-Only) */
173     PGPPUBKEYALGO_ELGAMAL_ENCRYPT= 16,  /*!< Elgamal(Encrypt-Only) */
174     PGPPUBKEYALGO_DSA           = 17,   /*!< DSA */
175     PGPPUBKEYALGO_EC            = 18,   /*!< Elliptic Curve */
176     PGPPUBKEYALGO_ECDSA         = 19,   /*!< ECDSA */
177     PGPPUBKEYALGO_ELGAMAL       = 20,   /*!< Elgamal */
178     PGPPUBKEYALGO_DH            = 21    /*!< Diffie-Hellman (X9.42) */
179 } pgpPubkeyAlgo;
180 /*@=typeuse@*/
181
182 /**
183  */
184 /*@observer@*/ /*@unchecked@*/ /*@unused@*/
185 extern struct pgpValTbl_s pgpPubkeyTbl[];
186
187 /**
188  * 9.2. Symmetric Key Algorithms
189  *
190 \verbatim
191        ID           Algorithm
192        --           ---------
193        0          - Plaintext or unencrypted data
194        1          - IDEA [IDEA]
195        2          - Triple-DES (DES-EDE, as per spec -
196                     168 bit key derived from 192)
197        3          - CAST5 (128 bit key, as per RFC 2144)
198        4          - Blowfish (128 bit key, 16 rounds) [BLOWFISH]
199        5          - SAFER-SK128 (13 rounds) [SAFER]
200        6          - Reserved for DES/SK
201        7          - Reserved for AES with 128-bit key
202        8          - Reserved for AES with 192-bit key
203        9          - Reserved for AES with 256-bit key
204        100 to 110 - Private/Experimental algorithm.
205 \endverbatim
206  *
207  * Implementations MUST implement Triple-DES. Implementations SHOULD
208  * implement IDEA and CAST5. Implementations MAY implement any other
209  * algorithm.
210  */
211 /*@-typeuse@*/
212 typedef enum pgpSymkeyAlgo_e {
213     PGPSYMKEYALGO_PLAINTEXT     =  0,   /*!< Plaintext */
214     PGPSYMKEYALGO_IDEA          =  1,   /*!< IDEA */
215     PGPSYMKEYALGO_TRIPLE_DES    =  2,   /*!< 3DES */
216     PGPSYMKEYALGO_CAST5         =  3,   /*!< CAST5 */
217     PGPSYMKEYALGO_BLOWFISH      =  4,   /*!< BLOWFISH */
218     PGPSYMKEYALGO_SAFER         =  5,   /*!< SAFER */
219     PGPSYMKEYALGO_DES_SK        =  6,   /*!< DES/SK */
220     PGPSYMKEYALGO_AES_128       =  7,   /*!< AES(128-bit key) */
221     PGPSYMKEYALGO_AES_192       =  8,   /*!< AES(192-bit key) */
222     PGPSYMKEYALGO_AES_256       =  9,   /*!< AES(256-bit key) */
223     PGPSYMKEYALGO_TWOFISH       = 10    /*!< TWOFISH */
224 } pgpSymkeyAlgo;
225 /*@=typeuse@*/
226
227 /**
228  */
229 /*@observer@*/ /*@unchecked@*/ /*@unused@*/
230 extern struct pgpValTbl_s pgpSymkeyTbl[];
231
232 /**
233  * 9.3. Compression Algorithms
234  *
235 \verbatim
236        ID           Algorithm
237        --           ---------
238        0          - Uncompressed
239        1          - ZIP (RFC 1951)
240        2          - ZLIB (RFC 1950)
241        100 to 110 - Private/Experimental algorithm.
242 \endverbatim
243  *
244  * Implementations MUST implement uncompressed data. Implementations
245  * SHOULD implement ZIP. Implementations MAY implement ZLIB.
246  */
247 /*@-typeuse@*/
248 typedef enum pgpCompressAlgo_e {
249     PGPCOMPRESSALGO_NONE        =  0,   /*!< Uncompressed */
250     PGPCOMPRESSALGO_ZIP         =  1,   /*!< ZIP */
251     PGPCOMPRESSALGO_ZLIB        =  2    /*!< ZLIB */
252 } pgpCompressAlgo;
253 /*@=typeuse@*/
254
255 /**
256  */
257 /*@observer@*/ /*@unchecked@*/ /*@unused@*/
258 extern struct pgpValTbl_s pgpCompressionTbl[];
259
260 /**
261  * 9.4. Hash Algorithms
262  *
263 \verbatim
264        ID           Algorithm                              Text Name
265        --           ---------                              ---- ----
266        1          - MD5                                    "MD5"
267        2          - SHA-1                                  "SHA1"
268        3          - RIPE-MD/160                            "RIPEMD160"
269        4          - Reserved for double-width SHA (experimental)
270        5          - MD2                                    "MD2"
271        6          - Reserved for TIGER/192                 "TIGER192"
272        7          - Reserved for HAVAL (5 pass, 160-bit)
273        "HAVAL-5-160"
274        100 to 110 - Private/Experimental algorithm.
275 \endverbatim
276  *
277  * Implementations MUST implement SHA-1. Implementations SHOULD
278  * implement MD5.
279  * @todo Add SHA256.
280  */
281 typedef enum pgpHashAlgo_e {
282     PGPHASHALGO_MD5             = 1,    /*!< MD5 */
283     PGPHASHALGO_SHA1            = 2,    /*!< SHA1 */
284     PGPHASHALGO_RIPEMD160       = 3,    /*!< RIPEMD160 */
285     PGPHASHALGO_MD2             = 5,    /*!< MD2 */
286     PGPHASHALGO_TIGER192        = 6,    /*!< TIGER192 */
287     PGPHASHALGO_HAVAL_5_160     = 7     /*!< HAVAL-5-160 */
288 } pgpHashAlgo;
289
290 /**
291  */
292 /*@observer@*/ /*@unchecked@*/ /*@unused@*/
293 extern struct pgpValTbl_s pgpHashTbl[];
294
295 /**
296  * 5.2.2. Version 3 Signature Packet Format
297  * 
298  * The body of a version 3 Signature Packet contains:
299  *   - One-octet version number (3).
300  *   - One-octet length of following hashed material.  MUST be 5.
301  *       - One-octet signature type.
302  *       - Four-octet creation time.
303  *   - Eight-octet key ID of signer.
304  *   - One-octet public key algorithm.
305  *   - One-octet hash algorithm.
306  *   - Two-octet field holding left 16 bits of signed hash value.
307  *   - One or more multi-precision integers comprising the signature.
308  *
309  * Algorithm Specific Fields for RSA signatures:
310  *   - multiprecision integer (MPI) of RSA signature value m**d.
311  *
312  * Algorithm Specific Fields for DSA signatures:
313  *   - MPI of DSA value r.
314  *   - MPI of DSA value s.
315  */
316 typedef struct pgpPktSigV3_s {
317     byte version;       /*!< version number (3). */
318     byte hashlen;       /*!< length of following hashed material. MUST be 5. */
319     byte sigtype;       /*!< signature type. */
320     byte time[4];       /*!< 4 byte creation time. */
321     byte signid[8];     /*!< key ID of signer. */
322     byte pubkey_algo;   /*!< public key algorithm. */
323     byte hash_algo;     /*!< hash algorithm. */
324     byte signhash16[2]; /*!< left 16 bits of signed hash value. */
325 } * pgpPktSigV3;
326
327 /**
328  * 5.2.3. Version 4 Signature Packet Format
329  * 
330  * The body of a version 4 Signature Packet contains:
331  *   - One-octet version number (4).
332  *   - One-octet signature type.
333  *   - One-octet public key algorithm.
334  *   - One-octet hash algorithm.
335  *   - Two-octet scalar octet count for following hashed subpacket
336  *     data. Note that this is the length in octets of all of the hashed
337  *     subpackets; a pointer incremented by this number will skip over
338  *     the hashed subpackets.
339  *   - Hashed subpacket data. (zero or more subpackets)
340  *   - Two-octet scalar octet count for following unhashed subpacket
341  *     data. Note that this is the length in octets of all of the
342  *     unhashed subpackets; a pointer incremented by this number will
343  *     skip over the unhashed subpackets.
344  *   - Unhashed subpacket data. (zero or more subpackets)
345  *   - Two-octet field holding left 16 bits of signed hash value.
346  *   - One or more multi-precision integers comprising the signature.
347  */
348 typedef struct pgpPktSigV4_s {
349     byte version;       /*!< version number (4). */
350     byte sigtype;       /*!< signature type. */
351     byte pubkey_algo;   /*!< public key algorithm. */
352     byte hash_algo;     /*!< hash algorithm. */
353     byte hashlen[2];    /*!< length of following hashed material. */
354 } * pgpPktSigV4;
355
356 /**
357  * 5.2.3.1. Signature Subpacket Specification
358  * 
359  * The subpacket fields consist of zero or more signature subpackets.
360  * Each set of subpackets is preceded by a two-octet scalar count of the
361  * length of the set of subpackets.
362  *
363  * Each subpacket consists of a subpacket header and a body.  The header
364  * consists of:
365  *   - the subpacket length (1,  2, or 5 octets)
366  *   - the subpacket type (1 octet)
367  * and is followed by the subpacket specific data.
368  *
369  * The length includes the type octet but not this length. Its format is
370  * similar to the "new" format packet header lengths, but cannot have
371  * partial body lengths. That is:
372 \verbatim
373        if the 1st octet <  192, then
374            lengthOfLength = 1
375            subpacketLen = 1st_octet
376
377        if the 1st octet >= 192 and < 255, then
378            lengthOfLength = 2
379            subpacketLen = ((1st_octet - 192) << 8) + (2nd_octet) + 192
380
381        if the 1st octet = 255, then
382            lengthOfLength = 5
383            subpacket length = [four-octet scalar starting at 2nd_octet]
384 \endverbatim
385  *
386  * The value of the subpacket type octet may be:
387  *
388 \verbatim
389        2 = signature creation time
390        3 = signature expiration time
391        4 = exportable certification
392        5 = trust signature
393        6 = regular expression
394        7 = revocable
395        9 = key expiration time
396        10 = placeholder for backward compatibility
397        11 = preferred symmetric algorithms
398        12 = revocation key
399        16 = issuer key ID
400        20 = notation data
401        21 = preferred hash algorithms
402        22 = preferred compression algorithms
403        23 = key server preferences
404        24 = preferred key server
405        25 = primary user id
406        26 = policy URL
407        27 = key flags
408        28 = signer's user id
409        29 = reason for revocation
410        100 to 110 = internal or user-defined
411 \endverbatim
412  *
413  * An implementation SHOULD ignore any subpacket of a type that it does
414  * not recognize.
415  *
416  * Bit 7 of the subpacket type is the "critical" bit.  If set, it
417  * denotes that the subpacket is one that is critical for the evaluator
418  * of the signature to recognize.  If a subpacket is encountered that is
419  * marked critical but is unknown to the evaluating software, the
420  * evaluator SHOULD consider the signature to be in error.
421  */
422 /*@-typeuse@*/
423 typedef enum pgpSubType_e {
424     PGPSUBTYPE_SIG_CREATE_TIME  =   2, /*!< signature creation time */
425     PGPSUBTYPE_SIG_EXPIRE_TIME  =   3, /*!< signature expiration time */
426     PGPSUBTYPE_EXPORTABLE_CERT  =   4, /*!< exportable certification */
427     PGPSUBTYPE_TRUST_SIG        =   5, /*!< trust signature */
428     PGPSUBTYPE_REGEX            =   6, /*!< regular expression */
429     PGPSUBTYPE_REVOCABLE        =   7, /*!< revocable */
430     PGPSUBTYPE_KEY_EXPIRE_TIME  =   9, /*!< key expiration time */
431     PGPSUBTYPE_BACKWARD_COMPAT  =  10, /*!< placeholder for backward compatibility */
432     PGPSUBTYPE_PREFER_SYMKEY    =  11, /*!< preferred symmetric algorithms */
433     PGPSUBTYPE_REVOKE_KEY       =  12, /*!< revocation key */
434     PGPSUBTYPE_ISSUER_KEYID     =  16, /*!< issuer key ID */
435     PGPSUBTYPE_NOTATION         =  20, /*!< notation data */
436     PGPSUBTYPE_PREFER_HASH      =  21, /*!< preferred hash algorithms */
437     PGPSUBTYPE_PREFER_COMPRESS  =  22, /*!< preferred compression algorithms */
438     PGPSUBTYPE_KEYSERVER_PREFERS=  23, /*!< key server preferences */
439     PGPSUBTYPE_PREFER_KEYSERVER =  24, /*!< preferred key server */
440     PGPSUBTYPE_PRIMARY_USERID   =  25, /*!< primary user id */
441     PGPSUBTYPE_POLICY_URL       =  26, /*!< policy URL */
442     PGPSUBTYPE_KEY_FLAGS        =  27, /*!< key flags */
443     PGPSUBTYPE_SIGNER_USERID    =  28, /*!< signer's user id */
444     PGPSUBTYPE_REVOKE_REASON    =  29, /*!< reason for revocation */
445     PGPSUBTYPE_INTERNAL_100     = 100, /*!< internal or user-defined */
446     PGPSUBTYPE_INTERNAL_101     = 101, /*!< internal or user-defined */
447     PGPSUBTYPE_INTERNAL_102     = 102, /*!< internal or user-defined */
448     PGPSUBTYPE_INTERNAL_103     = 103, /*!< internal or user-defined */
449     PGPSUBTYPE_INTERNAL_104     = 104, /*!< internal or user-defined */
450     PGPSUBTYPE_INTERNAL_105     = 105, /*!< internal or user-defined */
451     PGPSUBTYPE_INTERNAL_106     = 106, /*!< internal or user-defined */
452     PGPSUBTYPE_INTERNAL_107     = 107, /*!< internal or user-defined */
453     PGPSUBTYPE_INTERNAL_108     = 108, /*!< internal or user-defined */
454     PGPSUBTYPE_INTERNAL_109     = 109, /*!< internal or user-defined */
455     PGPSUBTYPE_INTERNAL_110     = 110 /*!< internal or user-defined */
456 } pgpSubType;
457 /*@=typeuse@*/
458
459 /**
460  */
461 /*@observer@*/ /*@unchecked@*/ /*@unused@*/
462 extern struct pgpValTbl_s pgpSubTypeTbl[];
463
464 /**
465  * 5.2. Signature Packet (Tag 2)
466  *
467  * A signature packet describes a binding between some public key and
468  * some data. The most common signatures are a signature of a file or a
469  * block of text, and a signature that is a certification of a user ID.
470  *
471  * Two versions of signature packets are defined.  Version 3 provides
472  * basic signature information, while version 4 provides an expandable
473  * format with subpackets that can specify more information about the
474  * signature. PGP 2.6.x only accepts version 3 signatures.
475  *
476  * Implementations MUST accept V3 signatures. Implementations SHOULD
477  * generate V4 signatures.  Implementations MAY generate a V3 signature
478  * that can be verified by PGP 2.6.x.
479  *
480  * Note that if an implementation is creating an encrypted and signed
481  * message that is encrypted to a V3 key, it is reasonable to create a
482  * V3 signature.
483  */
484 typedef union pgpPktSig_u {
485     struct pgpPktSigV3_s v3;
486     struct pgpPktSigV4_s v4;
487 } * pgpPktSig;
488
489 /**
490  * 5.3. Symmetric-Key Encrypted Session-Key Packets (Tag 3)
491  *
492  * The Symmetric-Key Encrypted Session Key packet holds the symmetric-
493  * key encryption of a session key used to encrypt a message.  Zero or
494  * more Encrypted Session Key packets and/or Symmetric-Key Encrypted
495  * Session Key packets may precede a Symmetrically Encrypted Data Packet
496  * that holds an encrypted message.  The message is encrypted with a
497  * session key, and the session key is itself encrypted and stored in
498  * the Encrypted Session Key packet or the Symmetric-Key Encrypted
499  * Session Key packet.
500  *
501  * If the Symmetrically Encrypted Data Packet is preceded by one or more
502  * Symmetric-Key Encrypted Session Key packets, each specifies a
503  * passphrase that may be used to decrypt the message.  This allows a
504  * message to be encrypted to a number of public keys, and also to one
505  * or more pass phrases. This packet type is new, and is not generated
506  * by PGP 2.x or PGP 5.0.
507  *
508  * The body of this packet consists of:
509  *   - A one-octet version number. The only currently defined version
510  *     is 4.
511  *   - A one-octet number describing the symmetric algorithm used.
512  *   - A string-to-key (S2K) specifier, length as defined above.
513  *   - Optionally, the encrypted session key itself, which is decrypted
514  *     with the string-to-key object.
515  *
516  */
517 typedef struct pgpPktSymkey_s {
518     byte version;       /*!< version number (4). */
519     byte symkey_algo;
520     byte s2k[1];
521 } pgpPktSymkey;
522
523 /**
524  * 5.4. One-Pass Signature Packets (Tag 4)
525  *
526  * The One-Pass Signature packet precedes the signed data and contains
527  * enough information to allow the receiver to begin calculating any
528  * hashes needed to verify the signature.  It allows the Signature
529  * Packet to be placed at the end of the message, so that the signer can
530  * compute the entire signed message in one pass.
531  *
532  * A One-Pass Signature does not interoperate with PGP 2.6.x or earlier.
533  *
534  * The body of this packet consists of:
535  *   - A one-octet version number. The current version is 3.
536  *   - A one-octet signature type. Signature types are described in
537  *     section 5.2.1.
538  *   - A one-octet number describing the hash algorithm used.
539  *   - A one-octet number describing the public key algorithm used.
540  *   - An eight-octet number holding the key ID of the signing key.
541  *   - A one-octet number holding a flag showing whether the signature
542  *     is nested.  A zero value indicates that the next packet is
543  *     another One-Pass Signature packet that describes another
544  *     signature to be applied to the same message data.
545  *
546  * Note that if a message contains more than one one-pass signature,
547  * then the signature packets bracket the message; that is, the first
548  * signature packet after the message corresponds to the last one-pass
549  * packet and the final signature packet corresponds to the first one-
550  * pass packet.
551  */
552 typedef struct pgpPktOnepass_s {
553     byte version;       /*!< version number (3). */
554     byte sigtype;       /*!< signature type. */
555     byte hash_algo;     /*!< hash algorithm. */
556     byte pubkey_algo;   /*!< public key algorithm. */
557     byte signid[8];     /*!< key ID of signer. */
558     byte nested;
559 } * pgpPktOnepass;
560
561 /**
562  * 5.5.1. Key Packet Variants
563  *
564  * 5.5.1.1. Public Key Packet (Tag 6)
565  *
566  * A Public Key packet starts a series of packets that forms an OpenPGP
567  * key (sometimes called an OpenPGP certificate).
568  *
569  * 5.5.1.2. Public Subkey Packet (Tag 14)
570  *
571  * A Public Subkey packet (tag 14) has exactly the same format as a
572  * Public Key packet, but denotes a subkey. One or more subkeys may be
573  * associated with a top-level key.  By convention, the top-level key
574  * provides signature services, and the subkeys provide encryption
575  * services.
576  *
577  * Note: in PGP 2.6.x, tag 14 was intended to indicate a comment packet.
578  * This tag was selected for reuse because no previous version of PGP
579  * ever emitted comment packets but they did properly ignore them.
580  * Public Subkey packets are ignored by PGP 2.6.x and do not cause it to
581  * fail, providing a limited degree of backward compatibility.
582  *
583  * 5.5.1.3. Secret Key Packet (Tag 5)
584  *
585  * A Secret Key packet contains all the information that is found in a
586  * Public Key packet, including the public key material, but also
587  * includes the secret key material after all the public key fields.
588  *
589  * 5.5.1.4. Secret Subkey Packet (Tag 7)
590  *
591  * A Secret Subkey packet (tag 7) is the subkey analog of the Secret Key
592  * packet, and has exactly the same format.
593  *
594  * 5.5.2. Public Key Packet Formats
595  *
596  * There are two versions of key-material packets. Version 3 packets
597  * were first generated by PGP 2.6. Version 2 packets are identical in
598  * format to Version 3 packets, but are generated by PGP 2.5 or before.
599  * V2 packets are deprecated and they MUST NOT be generated.  PGP 5.0
600  * introduced version 4 packets, with new fields and semantics.  PGP
601  * 2.6.x will not accept key-material packets with versions greater than
602  * 3.
603  *
604  * OpenPGP implementations SHOULD create keys with version 4 format. An
605  * implementation MAY generate a V3 key to ensure interoperability with
606  * old software; note, however, that V4 keys correct some security
607  * deficiencies in V3 keys. These deficiencies are described below. An
608  * implementation MUST NOT create a V3 key with a public key algorithm
609  * other than RSA.
610  *
611  * A version 3 public key or public subkey packet contains:
612  *   - A one-octet version number (3).
613  *   - A four-octet number denoting the time that the key was created.
614  *   - A two-octet number denoting the time in days that this key is
615  *     valid. If this number is zero, then it does not expire.
616  *   - A one-octet number denoting the public key algorithm of this key
617  *   - A series of multi-precision integers comprising the key
618  *     material:
619  *       - a multiprecision integer (MPI) of RSA public modulus n;
620  *       - an MPI of RSA public encryption exponent e.
621  *
622  * V3 keys SHOULD only be used for backward compatibility because of
623  * three weaknesses in them. First, it is relatively easy to construct a
624  * V3 key that has the same key ID as any other key because the key ID
625  * is simply the low 64 bits of the public modulus. Secondly, because
626  * the fingerprint of a V3 key hashes the key material, but not its
627  * length, which increases the opportunity for fingerprint collisions.
628  * Third, there are minor weaknesses in the MD5 hash algorithm that make
629  * developers prefer other algorithms. See below for a fuller discussion
630  * of key IDs and fingerprints.
631  *
632  */
633 typedef struct pgpPktKeyV3_s {
634     byte version;       /*!< version number (3). */
635     byte time[4];       /*!< time that the key was created. */
636     byte valid[2];      /*!< time in days that this key is valid. */
637     byte pubkey_algo;   /*!< public key algorithm. */
638 } * pgpPktKeyV3;
639
640 /**
641  * The version 4 format is similar to the version 3 format except for
642  * the absence of a validity period.  This has been moved to the
643  * signature packet.  In addition, fingerprints of version 4 keys are
644  * calculated differently from version 3 keys, as described in section
645  * "Enhanced Key Formats."
646  *
647  * A version 4 packet contains:
648  *   - A one-octet version number (4).
649  *   - A four-octet number denoting the time that the key was created.
650  *   - A one-octet number denoting the public key algorithm of this key
651  *   - A series of multi-precision integers comprising the key
652  *     material.  This algorithm-specific portion is:
653  *
654  *     Algorithm Specific Fields for RSA public keys:
655  *       - multiprecision integer (MPI) of RSA public modulus n;
656  *       - MPI of RSA public encryption exponent e.
657  *
658  *     Algorithm Specific Fields for DSA public keys:
659  *       - MPI of DSA prime p;
660  *       - MPI of DSA group order q (q is a prime divisor of p-1);
661  *       - MPI of DSA group generator g;
662  *       - MPI of DSA public key value y (= g**x where x is secret).
663  *
664  *     Algorithm Specific Fields for Elgamal public keys:
665  *       - MPI of Elgamal prime p;
666  *       - MPI of Elgamal group generator g;
667  *       - MPI of Elgamal public key value y (= g**x where x is
668  *         secret).
669  *
670  */
671 typedef struct pgpPktKeyV4_s {
672     byte version;       /*!< version number (4). */
673     byte time[4];       /*!< time that the key was created. */
674     byte pubkey_algo;   /*!< public key algorithm. */
675 } * pgpPktKeyV4;
676
677 /**
678  * 5.5.3. Secret Key Packet Formats
679  *
680  * The Secret Key and Secret Subkey packets contain all the data of the
681  * Public Key and Public Subkey packets, with additional algorithm-
682  * specific secret key data appended, in encrypted form.
683  *
684  * The packet contains:
685  *   - A Public Key or Public Subkey packet, as described above
686  *   - One octet indicating string-to-key usage conventions.  0
687  *     indicates that the secret key data is not encrypted.  255
688  *     indicates that a string-to-key specifier is being given.  Any
689  *     other value is a symmetric-key encryption algorithm specifier.
690  *   - [Optional] If string-to-key usage octet was 255, a one-octet
691  *     symmetric encryption algorithm.
692  *   - [Optional] If string-to-key usage octet was 255, a string-to-key
693  *     specifier.  The length of the string-to-key specifier is implied
694  *     by its type, as described above.
695  *   - [Optional] If secret data is encrypted, eight-octet Initial
696  *     Vector (IV).
697  *   - Encrypted multi-precision integers comprising the secret key
698  *     data. These algorithm-specific fields are as described below.
699  *   - Two-octet checksum of the plaintext of the algorithm-specific
700  *     portion (sum of all octets, mod 65536).
701  *
702  *     Algorithm Specific Fields for RSA secret keys:
703  *     - multiprecision integer (MPI) of RSA secret exponent d.
704  *     - MPI of RSA secret prime value p.
705  *     - MPI of RSA secret prime value q (p < q).
706  *     - MPI of u, the multiplicative inverse of p, mod q.
707  *
708  *     Algorithm Specific Fields for DSA secret keys:
709  *     - MPI of DSA secret exponent x.
710  *
711  *     Algorithm Specific Fields for Elgamal secret keys:
712  *     - MPI of Elgamal secret exponent x.
713  *
714  * Secret MPI values can be encrypted using a passphrase.  If a string-
715  * to-key specifier is given, that describes the algorithm for
716  * converting the passphrase to a key, else a simple MD5 hash of the
717  * passphrase is used.  Implementations SHOULD use a string-to-key
718  * specifier; the simple hash is for backward compatibility. The cipher
719  * for encrypting the MPIs is specified in the secret key packet.
720  *
721  * Encryption/decryption of the secret data is done in CFB mode using
722  * the key created from the passphrase and the Initial Vector from the
723  * packet. A different mode is used with V3 keys (which are only RSA)
724  * than with other key formats. With V3 keys, the MPI bit count prefix
725  * (i.e., the first two octets) is not encrypted.  Only the MPI non-
726  * prefix data is encrypted.  Furthermore, the CFB state is
727  * resynchronized at the beginning of each new MPI value, so that the
728  * CFB block boundary is aligned with the start of the MPI data.
729  *
730  * With V4 keys, a simpler method is used.  All secret MPI values are
731  * encrypted in CFB mode, including the MPI bitcount prefix.
732  *
733  * The 16-bit checksum that follows the algorithm-specific portion is
734  * the algebraic sum, mod 65536, of the plaintext of all the algorithm-
735  * specific octets (including MPI prefix and data).  With V3 keys, the
736  * checksum is stored in the clear.  With V4 keys, the checksum is
737  * encrypted like the algorithm-specific data.  This value is used to
738  * check that the passphrase was correct.
739  *
740  */
741 typedef union pgpPktKey_u {
742     struct pgpPktKeyV3_s v3;
743     struct pgpPktKeyV4_s v4;
744 } pgpPktKey;
745
746 /*
747  * 5.6. Compressed Data Packet (Tag 8)
748  *
749  * The Compressed Data packet contains compressed data. Typically, this
750  * packet is found as the contents of an encrypted packet, or following
751  * a Signature or One-Pass Signature packet, and contains literal data
752  * packets.
753  *
754  * The body of this packet consists of:
755  *   - One octet that gives the algorithm used to compress the packet.
756  *   - The remainder of the packet is compressed data.
757  *
758  * A Compressed Data Packet's body contains an block that compresses
759  * some set of packets. See section "Packet Composition" for details on
760  * how messages are formed.
761  *
762  * ZIP-compressed packets are compressed with raw RFC 1951 DEFLATE
763  * blocks. Note that PGP V2.6 uses 13 bits of compression. If an
764  * implementation uses more bits of compression, PGP V2.6 cannot
765  * decompress it.
766  *
767  * ZLIB-compressed packets are compressed with RFC 1950 ZLIB-style
768  * blocks.
769  */
770 typedef struct pgpPktCdata_s {
771     byte compressalgo;
772     byte data[1];
773 } pgpPktCdata;
774
775 /*
776  * 5.7. Symmetrically Encrypted Data Packet (Tag 9)
777  *
778  * The Symmetrically Encrypted Data packet contains data encrypted with
779  * a symmetric-key algorithm. When it has been decrypted, it will
780  * typically contain other packets (often literal data packets or
781  * compressed data packets).
782  *
783  * The body of this packet consists of:
784  *   - Encrypted data, the output of the selected symmetric-key cipher
785  *     operating in PGP's variant of Cipher Feedback (CFB) mode.
786  *
787  * The symmetric cipher used may be specified in an Public-Key or
788  * Symmetric-Key Encrypted Session Key packet that precedes the
789  * Symmetrically Encrypted Data Packet.  In that case, the cipher
790  * algorithm octet is prefixed to the session key before it is
791  * encrypted.  If no packets of these types precede the encrypted data,
792  * the IDEA algorithm is used with the session key calculated as the MD5
793  * hash of the passphrase.
794  *
795  * The data is encrypted in CFB mode, with a CFB shift size equal to the
796  * cipher's block size.  The Initial Vector (IV) is specified as all
797  * zeros.  Instead of using an IV, OpenPGP prefixes a 10-octet string to
798  * the data before it is encrypted.  The first eight octets are random,
799  * and the 9th and 10th octets are copies of the 7th and 8th octets,
800  * respectively. After encrypting the first 10 octets, the CFB state is
801  * resynchronized if the cipher block size is 8 octets or less.  The
802  * last 8 octets of ciphertext are passed through the cipher and the
803  * block boundary is reset.
804  *
805  * The repetition of 16 bits in the 80 bits of random data prefixed to
806  * the message allows the receiver to immediately check whether the
807  * session key is incorrect.
808  */
809 typedef struct pgpPktEdata_s {
810     byte data[1];
811 } pgpPktEdata;
812
813 /*
814  * 5.8. Marker Packet (Obsolete Literal Packet) (Tag 10)
815  *
816  * An experimental version of PGP used this packet as the Literal
817  * packet, but no released version of PGP generated Literal packets with
818  * this tag. With PGP 5.x, this packet has been re-assigned and is
819  * reserved for use as the Marker packet.
820  *
821  * The body of this packet consists of:
822  *   - The three octets 0x50, 0x47, 0x50 (which spell "PGP" in UTF-8).
823  *
824  * Such a packet MUST be ignored when received.  It may be placed at the
825  * beginning of a message that uses features not available in PGP 2.6.x
826  * in order to cause that version to report that newer software is
827  * necessary to process the message.
828  */
829 /*
830  * 5.9. Literal Data Packet (Tag 11)
831  *
832  * A Literal Data packet contains the body of a message; data that is
833  * not to be further interpreted.
834  *
835  * The body of this packet consists of:
836  *   - A one-octet field that describes how the data is formatted.
837  *
838  * If it is a 'b' (0x62), then the literal packet contains binary data.
839  * If it is a 't' (0x74), then it contains text data, and thus may need
840  * line ends converted to local form, or other text-mode changes.  RFC
841  * 1991 also defined a value of 'l' as a 'local' mode for machine-local
842  * conversions.  This use is now deprecated.
843  *   - File name as a string (one-octet length, followed by file name),
844  *     if the encrypted data should be saved as a file.
845  *
846  * If the special name "_CONSOLE" is used, the message is considered to
847  * be "for your eyes only".  This advises that the message data is
848  * unusually sensitive, and the receiving program should process it more
849  * carefully, perhaps avoiding storing the received data to disk, for
850  * example.
851  *   - A four-octet number that indicates the modification date of the
852  *     file, or the creation time of the packet, or a zero that
853  *     indicates the present time.
854  *   - The remainder of the packet is literal data.
855  *
856  * Text data is stored with <CR><LF> text endings (i.e. network-normal
857  * line endings).  These should be converted to native line endings by
858  * the receiving software.
859  */
860 typedef struct pgpPktLdata_s {
861     byte format;
862     byte filenamelen;
863     byte filename[1];
864 } pgpPktLdata;
865
866 /*
867  * 5.10. Trust Packet (Tag 12)
868  *
869  * The Trust packet is used only within keyrings and is not normally
870  * exported.  Trust packets contain data that record the user's
871  * specifications of which key holders are trustworthy introducers,
872  * along with other information that implementing software uses for
873  * trust information.
874  *
875  * Trust packets SHOULD NOT be emitted to output streams that are
876  * transferred to other users, and they SHOULD be ignored on any input
877  * other than local keyring files.
878  */
879 typedef struct pgpPktTrust_s {
880     byte flag;
881 } pgpPktTrust;
882
883 /*
884  * 5.11. User ID Packet (Tag 13)
885  *
886  * A User ID packet consists of data that is intended to represent the
887  * name and email address of the key holder.  By convention, it includes
888  * an RFC 822 mail name, but there are no restrictions on its content.
889  * The packet length in the header specifies the length of the user id.
890  * If it is text, it is encoded in UTF-8.
891  *
892  */
893 typedef struct pgpPktUid_s {
894     byte userid[1];
895 } pgpPktUid;
896
897 /**
898  */
899 union pgpPktPre_u {
900     pgpPktPubkey pubkey;        /*!< 5.1. Public-Key Encrypted Session Key */
901     pgpPktSig sig;              /*!< 5.2. Signature */
902     pgpPktSymkey symkey;        /*!< 5.3. Symmetric-Key Encrypted Session-Key */
903     pgpPktOnepass onepass;      /*!< 5.4. One-Pass Signature */
904     pgpPktKey key;              /*!< 5.5. Key Material */
905     pgpPktCdata cdata;          /*!< 5.6. Compressed Data */
906     pgpPktEdata edata;          /*!< 5.7. Symmetrically Encrypted Data */
907                                 /*!< 5.8. Marker (obsolete) */
908     pgpPktLdata ldata;          /*!< 5.9. Literal Data */
909     pgpPktTrust tdata;          /*!< 5.10. Trust */
910     pgpPktUid uid;              /*!< 5.11. User ID */
911 };
912
913 /**
914  */
915 /*@-typeuse@*/
916 typedef enum pgpArmor_e {
917     PGPARMOR_ERROR              = -1,
918     PGPARMOR_NONE               =  0,
919     PGPARMOR_MESSAGE            =  1, /*!< MESSAGE */
920     PGPARMOR_PUBKEY             =  2, /*!< PUBLIC KEY BLOCK */
921     PGPARMOR_SIGNATURE          =  3, /*!< SIGNATURE */
922     PGPARMOR_SIGNED_MESSAGE     =  4, /*!< SIGNED MESSAGE */
923     PGPARMOR_FILE               =  5, /*!< ARMORED FILE */
924     PGPARMOR_PRIVKEY            =  6, /*!< PRIVATE KEY BLOCK */
925     PGPARMOR_SECKEY             =  7 /*!< SECRET KEY BLOCK */
926 } pgpArmor;
927 /*@=typeuse@*/
928
929 /**
930  */
931 /*@observer@*/ /*@unchecked@*/ /*@unused@*/
932 extern struct pgpValTbl_s pgpArmorTbl[];
933
934 /**
935  */
936 /*@-typeuse@*/
937 typedef enum pgpArmorKey_e {
938     PGPARMORKEY_VERSION         = 1, /*!< Version: */
939     PGPARMORKEY_COMMENT         = 2, /*!< Comment: */
940     PGPARMORKEY_MESSAGEID       = 3, /*!< MessageID: */
941     PGPARMORKEY_HASH            = 4, /*!< Hash: */
942     PGPARMORKEY_CHARSET         = 5  /*!< Charset: */
943 } pgpArmorKey;
944 /*@=typeuse@*/
945
946 /**
947  */
948 /*@observer@*/ /*@unchecked@*/ /*@unused@*/
949 extern struct pgpValTbl_s pgpArmorKeyTbl[];
950
951 /**
952  */
953 struct pgpDigParams_s {
954 /*@only@*/ /*@null@*/
955     const char * userid;
956 /*@only@*/ /*@null@*/
957     const byte * hash;
958     const char * params[4];
959     byte tag;
960
961     byte version;       /*!< version number. */
962     byte time[4];       /*!< time that the key was created. */
963     byte pubkey_algo;   /*!< public key algorithm. */
964
965     byte hash_algo;
966     byte sigtype;
967     byte hashlen;
968     byte signhash16[2];
969     byte signid[8];
970     byte saved;
971 #define PGPDIG_SAVED_TIME       (1 << 0)
972 #define PGPDIG_SAVED_ID         (1 << 1)
973
974 };
975
976 /**
977  */
978 struct pgpDig_s {
979     struct pgpDigParams_s signature;
980     struct pgpDigParams_s pubkey;
981
982     size_t nbytes;              /*!< No. bytes of plain text. */
983
984 /*@only@*/ /*@null@*/
985     DIGEST_CTX sha1ctx;         /*!< (dsa) sha1 hash context. */
986 /*@only@*/ /*@null@*/
987     DIGEST_CTX hdrsha1ctx;      /*!< (dsa) header sha1 hash context. */
988 /*@only@*/ /*@null@*/
989     void * sha1;                /*!< (dsa) V3 signature hash. */
990     size_t sha1len;             /*!< (dsa) V3 signature hash length. */
991
992 /*@only@*/ /*@null@*/
993     DIGEST_CTX md5ctx;          /*!< (rsa) md5 hash context. */
994 /*@only@*/ /*@null@*/
995     void * md5;                 /*!< (rsa) V3 signature hash. */
996     size_t md5len;              /*!< (rsa) V3 signature hash length. */
997
998     /* DSA parameters. */
999     mp32barrett p;
1000     mp32barrett q;
1001     mp32number g;
1002     mp32number y;
1003     mp32number hm;
1004     mp32number r;
1005     mp32number s;
1006
1007     /* RSA parameters. */
1008     rsapk rsa_pk;
1009     mp32number m;
1010     mp32number c;
1011     mp32number rsahm;
1012 };
1013
1014
1015 /*@-fcnuse@*/
1016 #ifdef __cplusplus
1017 extern "C" {
1018 #endif
1019
1020 /**
1021  */
1022 /*@unused@*/ static inline
1023 unsigned int pgpGrab(const byte *s, int nbytes)
1024         /*@*/
1025 {
1026     unsigned int i = 0;
1027     int nb = (nbytes <= sizeof(i) ? nbytes : sizeof(i));
1028     while (nb--)
1029         i = (i << 8) | *s++;
1030     return i;
1031 }
1032
1033 /**
1034  */
1035 /*@unused@*/ static inline
1036 int pgpLen(const byte *s, /*@out@*/ unsigned int *lenp)
1037         /*@modifies *lenp @*/
1038 {
1039     if (*s < 192) {
1040         (*lenp) = *s++;
1041         return 1;
1042     } else if (*s < 255) {
1043         (*lenp) = ((((unsigned)s[0]) - 192) << 8) + s[1] + 192;
1044         return 2;
1045     } else {
1046         (*lenp) = pgpGrab(s+1, 4);
1047         return 5;
1048     }
1049 }
1050
1051 /**
1052  */
1053 /*@unused@*/ static inline
1054 unsigned int pgpMpiBits(const byte *p)
1055         /*@*/
1056 {
1057     return ((p[0] << 8) | p[1]);
1058 }
1059
1060 /**
1061  */
1062 /*@unused@*/ static inline
1063 unsigned int pgpMpiLen(const byte *p)
1064         /*@*/
1065 {
1066     return (2 + ((pgpMpiBits(p)+7)>>3));
1067 }
1068         
1069 /**
1070  */
1071 /*@unused@*/ static inline
1072 char * pgpHexCvt(/*@returned@*/ char *t, const byte *s, int nbytes)
1073         /*@modifies *t @*/
1074 {
1075     static char hex[] = "0123456789abcdef";
1076     while (nbytes-- > 0) {
1077         unsigned int i;
1078         i = *s++;
1079         *t++ = hex[ (i >> 4) & 0xf ];
1080         *t++ = hex[ (i     ) & 0xf ];
1081     }
1082     *t = '\0';
1083     return t;
1084 }
1085
1086 /**
1087  */
1088 /*@unused@*/ static inline /*@observer@*/
1089 char * pgpHexStr(const byte *p, unsigned int plen)
1090         /*@*/
1091 {
1092     static char prbuf[2048];
1093     char *t = prbuf;
1094     t = pgpHexCvt(t, p, plen);
1095     return prbuf;
1096 }
1097
1098 /**
1099  */
1100 /*@unused@*/ static inline /*@observer@*/
1101 const char * pgpMpiStr(const byte *p)
1102         /*@*/
1103 {
1104     static char prbuf[2048];
1105     char *t = prbuf;
1106     sprintf(t, "[%4u]: ", pgpGrab(p, 2));
1107     t += strlen(t);
1108     t = pgpHexCvt(t, p+2, pgpMpiLen(p)-2);
1109     return prbuf;
1110 }
1111
1112 /**
1113  */
1114 /*@unused@*/ static inline /*@observer@*/
1115 const char * pgpValStr(pgpValTbl vs, byte val)
1116         /*@*/
1117 {
1118     do {
1119         if (vs->val == val)
1120             break;
1121     } while ((++vs)->val != -1);
1122     return vs->str;
1123 }
1124
1125 /**
1126  */
1127 /*@unused@*/ static inline
1128 int pgpValTok(pgpValTbl vs, const char * s, const char * se)
1129         /*@*/
1130 {
1131     do {
1132         int vlen = strlen(vs->str);
1133         if (vlen <= (se-s) && !strncmp(s, vs->str, vlen))
1134             break;
1135     } while ((++vs)->val != -1);
1136     return vs->val;
1137 }
1138
1139 /*@-exportlocal@*/
1140 /**
1141  */
1142 void pgpPrtVal(const char * pre, pgpValTbl vs, byte val)
1143         /*@globals fileSystem @*/
1144         /*@modifies fileSystem @*/;
1145
1146 /**
1147  */
1148 int pgpPrtSubType(const byte *h, unsigned int hlen)
1149         /*@globals fileSystem @*/
1150         /*@modifies fileSystem @*/;
1151
1152 /**
1153  */
1154 int pgpPrtSig(pgpTag tag, const byte *h, unsigned int hlen)
1155         /*@globals fileSystem @*/
1156         /*@modifies fileSystem @*/;
1157
1158 /**
1159  */
1160 int pgpPrtKey(pgpTag tag, const byte *h, unsigned int hlen)
1161         /*@globals fileSystem @*/
1162         /*@modifies fileSystem @*/;
1163
1164 /**
1165  */
1166 int pgpPrtUserID(pgpTag tag, const byte *h, unsigned int hlen)
1167         /*@globals fileSystem @*/
1168         /*@modifies fileSystem @*/;
1169
1170 /**
1171  */
1172 int pgpPrtComment(pgpTag tag, const byte *h, unsigned int hlen)
1173         /*@globals fileSystem @*/
1174         /*@modifies fileSystem @*/;
1175
1176 /**
1177  */
1178 int pgpPrtPkt(const byte *pkt)
1179         /*@globals fileSystem @*/
1180         /*@modifies fileSystem @*/;
1181 /*@=exportlocal@*/
1182
1183 /**
1184  */
1185 int pgpPrtPkts(const byte *pkts, unsigned int plen, struct pgpDig_s *dig, int printing)
1186         /*@globals fileSystem @*/
1187         /*@modifies fileSystem @*/;
1188
1189 /**
1190  */
1191 pgpArmor pgpReadPkts(const char * fn,
1192                 /*@out@*/ const byte ** pkt, /*@out@*/ size_t * pktlen)
1193         /*@globals fileSystem @*/
1194         /*@modifies *pkt, *pktlen, fileSystem @*/;
1195
1196 /**
1197  */
1198 /*@only@*/
1199 struct pgpDig_s * pgpNewDig(void)
1200         /*@*/;
1201
1202 /**
1203  */
1204 void pgpCleanDig(/*@null@*/ struct pgpDig_s * dig)
1205         /*@modifies dig @*/;
1206
1207 /**
1208  */
1209 /*@only@*/ /*@null@*/
1210 struct pgpDig_s * pgpFreeDig(/*@only@*/ /*@null@*/ struct pgpDig_s * dig)
1211         /*@modifies *dig @*/;
1212
1213 /**
1214  */
1215 /*@unused@*/ static inline
1216 int pgpIsPkt(const byte * p)
1217         /*@*/
1218 {
1219     unsigned int val = *p++;
1220     pgpTag tag;
1221     int rc;
1222
1223     /* XXX can't deal with these. */
1224     if (!(val & 0x80))
1225         return 0;
1226
1227     if (val & 0x40)
1228         tag = (val & 0x3f);
1229     else
1230         tag = (val >> 2) & 0xf;
1231
1232     switch (tag) {
1233     case PGPTAG_MARKER:
1234     case PGPTAG_SYMMETRIC_SESSION_KEY:
1235     case PGPTAG_ONEPASS_SIGNATURE:
1236     case PGPTAG_PUBLIC_KEY:
1237     case PGPTAG_SECRET_KEY:
1238     case PGPTAG_PUBLIC_SESSION_KEY:
1239     case PGPTAG_SIGNATURE:
1240     case PGPTAG_COMMENT:
1241     case PGPTAG_COMMENT_OLD:
1242     case PGPTAG_LITERAL_DATA:
1243     case PGPTAG_COMPRESSED_DATA:
1244     case PGPTAG_SYMMETRIC_DATA:
1245         rc = 1;
1246         break;
1247     case PGPTAG_PUBLIC_SUBKEY:
1248     case PGPTAG_SECRET_SUBKEY:
1249     case PGPTAG_USER_ID:
1250     case PGPTAG_RESERVED:
1251     case PGPTAG_TRUST:
1252     case PGPTAG_PHOTOID:
1253     case PGPTAG_ENCRYPTED_MDC:
1254     case PGPTAG_MDC:
1255     case PGPTAG_PRIVATE_60:
1256     case PGPTAG_PRIVATE_62:
1257     case PGPTAG_CONTROL:
1258     default:
1259         rc = 0;
1260         break;
1261     }
1262
1263     return rc;
1264 }
1265
1266 #define CRC24_INIT      0xb704ce
1267 #define CRC24_POLY      0x1864cfb
1268
1269 /**
1270  */
1271 /*@unused@*/ static inline
1272 uint32 pgpCRC(const byte *octets, size_t len)
1273         /*@*/
1274 {
1275     uint32 crc = CRC24_INIT;
1276     int i;
1277
1278     while (len--) {
1279         crc ^= (*octets++) << 16;
1280         for (i = 0; i < 8; i++) {
1281             crc <<= 1;
1282             if (crc & 0x1000000)
1283                 crc ^= CRC24_POLY;
1284         }
1285     }
1286     return crc & 0xffffff;
1287 }
1288
1289 #ifdef __cplusplus
1290 }
1291 #endif
1292 /*@=fcnuse@*/
1293
1294 /*@=bounds@*/
1295 #endif  /* H_RPMPGP */