OSDN Git Service

Add missing file to GETTEXT_FILES
[pg-rex/syncrep.git] / contrib / pgcrypto / px.h
1 /*
2  * px.h
3  *              Header file for pgcrypto.
4  *
5  * Copyright (c) 2001 Marko Kreen
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *        notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *        notice, this list of conditions and the following disclaimer in the
15  *        documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.      IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * contrib/pgcrypto/px.h
30  */
31
32 #ifndef __PX_H
33 #define __PX_H
34
35 #include <sys/types.h>
36 #include <sys/param.h>
37
38 /* keep debug messages? */
39 #define PX_DEBUG
40
41 /* a way to disable palloc
42  * - useful if compiled into standalone
43  */
44 #ifndef PX_OWN_ALLOC
45 #define px_alloc(s) palloc(s)
46 #define px_realloc(p, s) repalloc(p, s)
47 #define px_free(p)      pfree(p)
48 #else
49 void       *px_alloc(size_t s);
50 void       *px_realloc(void *p, size_t s);
51 void            px_free(void *p);
52 #endif
53
54 /* max len of 'type' parms */
55 #define PX_MAX_NAMELEN          128
56
57 /* max salt returned */
58 #define PX_MAX_SALT_LEN         128
59
60 /*
61  * PX error codes
62  */
63 #define PXE_OK                                          0
64 #define PXE_ERR_GENERIC                         -1
65 #define PXE_NO_HASH                                     -2
66 #define PXE_NO_CIPHER                           -3
67 #define PXE_NOTBLOCKSIZE                        -4
68 #define PXE_BAD_OPTION                          -5
69 #define PXE_BAD_FORMAT                          -6
70 #define PXE_KEY_TOO_BIG                         -7
71 #define PXE_CIPHER_INIT                         -8
72 #define PXE_HASH_UNUSABLE_FOR_HMAC      -9
73 #define PXE_DEV_READ_ERROR                      -10
74 #define PXE_OSSL_RAND_ERROR                     -11
75 #define PXE_BUG                                         -12
76 #define PXE_ARGUMENT_ERROR                      -13
77 #define PXE_UNKNOWN_SALT_ALGO           -14
78 #define PXE_BAD_SALT_ROUNDS                     -15
79 #define PXE_MCRYPT_INTERNAL                     -16
80 #define PXE_NO_RANDOM                           -17
81 #define PXE_DECRYPT_FAILED                      -18
82
83 #define PXE_MBUF_SHORT_READ                     -50
84
85 #define PXE_PGP_CORRUPT_DATA            -100
86 #define PXE_PGP_CORRUPT_ARMOR           -101
87 #define PXE_PGP_UNSUPPORTED_COMPR       -102
88 #define PXE_PGP_UNSUPPORTED_CIPHER      -103
89 #define PXE_PGP_UNSUPPORTED_HASH        -104
90 #define PXE_PGP_COMPRESSION_ERROR       -105
91 #define PXE_PGP_NOT_TEXT                        -106
92 #define PXE_PGP_UNEXPECTED_PKT          -107
93 #define PXE_PGP_NO_BIGNUM                       -108
94 #define PXE_PGP_MATH_FAILED                     -109
95 #define PXE_PGP_SHORT_ELGAMAL_KEY       -110
96 #define PXE_PGP_RSA_UNSUPPORTED         -111
97 #define PXE_PGP_UNKNOWN_PUBALGO         -112
98 #define PXE_PGP_WRONG_KEY                       -113
99 #define PXE_PGP_MULTIPLE_KEYS           -114
100 #define PXE_PGP_EXPECT_PUBLIC_KEY       -115
101 #define PXE_PGP_EXPECT_SECRET_KEY       -116
102 #define PXE_PGP_NOT_V4_KEYPKT           -117
103 #define PXE_PGP_KEYPKT_CORRUPT          -118
104 #define PXE_PGP_NO_USABLE_KEY           -119
105 #define PXE_PGP_NEED_SECRET_PSW         -120
106 #define PXE_PGP_BAD_S2K_MODE            -121
107 #define PXE_PGP_UNSUPPORTED_PUBALGO -122
108 #define PXE_PGP_MULTIPLE_SUBKEYS        -123
109
110
111 typedef struct px_digest PX_MD;
112 typedef struct px_alias PX_Alias;
113 typedef struct px_hmac PX_HMAC;
114 typedef struct px_cipher PX_Cipher;
115 typedef struct px_combo PX_Combo;
116
117 struct px_digest
118 {
119         unsigned        (*result_size) (PX_MD *h);
120         unsigned        (*block_size) (PX_MD *h);
121         void            (*reset) (PX_MD *h);
122         void            (*update) (PX_MD *h, const uint8 *data, unsigned dlen);
123         void            (*finish) (PX_MD *h, uint8 *dst);
124         void            (*free) (PX_MD *h);
125         /* private */
126         union
127         {
128                 unsigned        code;
129                 void       *ptr;
130         }                       p;
131 };
132
133 struct px_alias
134 {
135         char       *alias;
136         char       *name;
137 };
138
139 struct px_hmac
140 {
141         unsigned        (*result_size) (PX_HMAC *h);
142         unsigned        (*block_size) (PX_HMAC *h);
143         void            (*reset) (PX_HMAC *h);
144         void            (*update) (PX_HMAC *h, const uint8 *data, unsigned dlen);
145         void            (*finish) (PX_HMAC *h, uint8 *dst);
146         void            (*free) (PX_HMAC *h);
147         void            (*init) (PX_HMAC *h, const uint8 *key, unsigned klen);
148
149         PX_MD      *md;
150         /* private */
151         struct
152         {
153                 uint8      *ipad;
154                 uint8      *opad;
155         }                       p;
156 };
157
158 struct px_cipher
159 {
160         unsigned        (*block_size) (PX_Cipher *c);
161         unsigned        (*key_size) (PX_Cipher *c);             /* max key len */
162         unsigned        (*iv_size) (PX_Cipher *c);
163
164         int                     (*init) (PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv);
165         int                     (*encrypt) (PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res);
166         int                     (*decrypt) (PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res);
167         void            (*free) (PX_Cipher *c);
168         /* private */
169         void       *ptr;
170         int                     pstat;                  /* mcrypt uses it */
171 };
172
173 struct px_combo
174 {
175         int                     (*init) (PX_Combo *cx, const uint8 *key, unsigned klen,
176                                                                          const uint8 *iv, unsigned ivlen);
177         int                     (*encrypt) (PX_Combo *cx, const uint8 *data, unsigned dlen,
178                                                                                 uint8 *res, unsigned *rlen);
179         int                     (*decrypt) (PX_Combo *cx, const uint8 *data, unsigned dlen,
180                                                                                 uint8 *res, unsigned *rlen);
181         unsigned        (*encrypt_len) (PX_Combo *cx, unsigned dlen);
182         unsigned        (*decrypt_len) (PX_Combo *cx, unsigned dlen);
183         void            (*free) (PX_Combo *cx);
184
185         PX_Cipher  *cipher;
186         unsigned        padding;
187 };
188
189 int                     px_find_digest(const char *name, PX_MD **res);
190 int                     px_find_hmac(const char *name, PX_HMAC **res);
191 int                     px_find_cipher(const char *name, PX_Cipher **res);
192 int                     px_find_combo(const char *name, PX_Combo **res);
193
194 int                     px_get_random_bytes(uint8 *dst, unsigned count);
195 int                     px_get_pseudo_random_bytes(uint8 *dst, unsigned count);
196 int                     px_add_entropy(const uint8 *data, unsigned count);
197
198 unsigned        px_acquire_system_randomness(uint8 *dst);
199
200 const char *px_strerror(int err);
201
202 const char *px_resolve_alias(const PX_Alias *aliases, const char *name);
203
204 void            px_set_debug_handler(void (*handler) (const char *));
205
206 #ifdef PX_DEBUG
207 void            px_debug(const char *fmt,...);
208 #else
209 #define px_debug(...)
210 #endif
211
212 #define px_md_result_size(md)           (md)->result_size(md)
213 #define px_md_block_size(md)            (md)->block_size(md)
214 #define px_md_reset(md)                 (md)->reset(md)
215 #define px_md_update(md, data, dlen)    (md)->update(md, data, dlen)
216 #define px_md_finish(md, buf)           (md)->finish(md, buf)
217 #define px_md_free(md)                  (md)->free(md)
218
219 #define px_hmac_result_size(hmac)       (hmac)->result_size(hmac)
220 #define px_hmac_block_size(hmac)        (hmac)->block_size(hmac)
221 #define px_hmac_reset(hmac)             (hmac)->reset(hmac)
222 #define px_hmac_init(hmac, key, klen)   (hmac)->init(hmac, key, klen)
223 #define px_hmac_update(hmac, data, dlen) (hmac)->update(hmac, data, dlen)
224 #define px_hmac_finish(hmac, buf)       (hmac)->finish(hmac, buf)
225 #define px_hmac_free(hmac)              (hmac)->free(hmac)
226
227
228 #define px_cipher_key_size(c)           (c)->key_size(c)
229 #define px_cipher_block_size(c)         (c)->block_size(c)
230 #define px_cipher_iv_size(c)            (c)->iv_size(c)
231 #define px_cipher_init(c, k, klen, iv)  (c)->init(c, k, klen, iv)
232 #define px_cipher_encrypt(c, data, dlen, res) \
233                                         (c)->encrypt(c, data, dlen, res)
234 #define px_cipher_decrypt(c, data, dlen, res) \
235                                         (c)->decrypt(c, data, dlen, res)
236 #define px_cipher_free(c)               (c)->free(c)
237
238
239 #define px_combo_encrypt_len(c, dlen)   (c)->encrypt_len(c, dlen)
240 #define px_combo_decrypt_len(c, dlen)   (c)->decrypt_len(c, dlen)
241 #define px_combo_init(c, key, klen, iv, ivlen) \
242                                         (c)->init(c, key, klen, iv, ivlen)
243 #define px_combo_encrypt(c, data, dlen, res, rlen) \
244                                         (c)->encrypt(c, data, dlen, res, rlen)
245 #define px_combo_decrypt(c, data, dlen, res, rlen) \
246                                         (c)->decrypt(c, data, dlen, res, rlen)
247 #define px_combo_free(c)                (c)->free(c)
248
249 #endif   /* __PX_H */