PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
px.h
Go to the documentation of this file.
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/param.h>
36 
37 /* keep debug messages? */
38 #define PX_DEBUG
39 
40 /* a way to disable palloc
41  * - useful if compiled into standalone
42  */
43 #ifndef PX_OWN_ALLOC
44 #define px_alloc(s) palloc(s)
45 #define px_realloc(p, s) repalloc(p, s)
46 #define px_free(p) pfree(p)
47 #else
48 void *px_alloc(size_t s);
49 void *px_realloc(void *p, size_t s);
50 void px_free(void *p);
51 #endif
52 
53 /* max len of 'type' parms */
54 #define PX_MAX_NAMELEN 128
55 
56 /* max salt returned */
57 #define PX_MAX_SALT_LEN 128
58 
59 /*
60  * PX error codes
61  */
62 #define PXE_OK 0
63 #define PXE_ERR_GENERIC -1
64 #define PXE_NO_HASH -2
65 #define PXE_NO_CIPHER -3
66 #define PXE_NOTBLOCKSIZE -4
67 #define PXE_BAD_OPTION -5
68 #define PXE_BAD_FORMAT -6
69 #define PXE_KEY_TOO_BIG -7
70 #define PXE_CIPHER_INIT -8
71 #define PXE_HASH_UNUSABLE_FOR_HMAC -9
72 #define PXE_DEV_READ_ERROR -10
73 #define PXE_BUG -12
74 #define PXE_ARGUMENT_ERROR -13
75 #define PXE_UNKNOWN_SALT_ALGO -14
76 #define PXE_BAD_SALT_ROUNDS -15
77 #define PXE_MCRYPT_INTERNAL -16
78 #define PXE_NO_RANDOM -17
79 #define PXE_DECRYPT_FAILED -18
80 
81 #define PXE_PGP_CORRUPT_DATA -100
82 #define PXE_PGP_CORRUPT_ARMOR -101
83 #define PXE_PGP_UNSUPPORTED_COMPR -102
84 #define PXE_PGP_UNSUPPORTED_CIPHER -103
85 #define PXE_PGP_UNSUPPORTED_HASH -104
86 #define PXE_PGP_COMPRESSION_ERROR -105
87 #define PXE_PGP_NOT_TEXT -106
88 #define PXE_PGP_UNEXPECTED_PKT -107
89 /* -108 is unused */
90 #define PXE_PGP_MATH_FAILED -109
91 #define PXE_PGP_SHORT_ELGAMAL_KEY -110
92 /* -111 is unused */
93 #define PXE_PGP_UNKNOWN_PUBALGO -112
94 #define PXE_PGP_WRONG_KEY -113
95 #define PXE_PGP_MULTIPLE_KEYS -114
96 #define PXE_PGP_EXPECT_PUBLIC_KEY -115
97 #define PXE_PGP_EXPECT_SECRET_KEY -116
98 #define PXE_PGP_NOT_V4_KEYPKT -117
99 #define PXE_PGP_KEYPKT_CORRUPT -118
100 #define PXE_PGP_NO_USABLE_KEY -119
101 #define PXE_PGP_NEED_SECRET_PSW -120
102 #define PXE_PGP_BAD_S2K_MODE -121
103 #define PXE_PGP_UNSUPPORTED_PUBALGO -122
104 #define PXE_PGP_MULTIPLE_SUBKEYS -123
105 
106 
107 typedef struct px_digest PX_MD;
108 typedef struct px_alias PX_Alias;
109 typedef struct px_hmac PX_HMAC;
110 typedef struct px_cipher PX_Cipher;
111 typedef struct px_combo PX_Combo;
112 
113 struct px_digest
114 {
115  unsigned (*result_size) (PX_MD *h);
116  unsigned (*block_size) (PX_MD *h);
117  void (*reset) (PX_MD *h);
118  void (*update) (PX_MD *h, const uint8 *data, unsigned dlen);
119  void (*finish) (PX_MD *h, uint8 *dst);
120  void (*free) (PX_MD *h);
121  /* private */
122  union
123  {
124  unsigned code;
125  void *ptr;
126  } p;
127 };
128 
129 struct px_alias
130 {
131  char *alias;
132  char *name;
133 };
134 
135 struct px_hmac
136 {
137  unsigned (*result_size) (PX_HMAC *h);
138  unsigned (*block_size) (PX_HMAC *h);
139  void (*reset) (PX_HMAC *h);
140  void (*update) (PX_HMAC *h, const uint8 *data, unsigned dlen);
141  void (*finish) (PX_HMAC *h, uint8 *dst);
142  void (*free) (PX_HMAC *h);
143  void (*init) (PX_HMAC *h, const uint8 *key, unsigned klen);
144 
146  /* private */
147  struct
148  {
151  } p;
152 };
153 
154 struct px_cipher
155 {
156  unsigned (*block_size) (PX_Cipher *c);
157  unsigned (*key_size) (PX_Cipher *c); /* max key len */
158  unsigned (*iv_size) (PX_Cipher *c);
159 
160  int (*init) (PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv);
161  int (*encrypt) (PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res);
162  int (*decrypt) (PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res);
163  void (*free) (PX_Cipher *c);
164  /* private */
165  void *ptr;
166  int pstat; /* mcrypt uses it */
167 };
168 
169 struct px_combo
170 {
171  int (*init) (PX_Combo *cx, const uint8 *key, unsigned klen,
172  const uint8 *iv, unsigned ivlen);
173  int (*encrypt) (PX_Combo *cx, const uint8 *data, unsigned dlen,
174  uint8 *res, unsigned *rlen);
175  int (*decrypt) (PX_Combo *cx, const uint8 *data, unsigned dlen,
176  uint8 *res, unsigned *rlen);
177  unsigned (*encrypt_len) (PX_Combo *cx, unsigned dlen);
178  unsigned (*decrypt_len) (PX_Combo *cx, unsigned dlen);
179  void (*free) (PX_Combo *cx);
180 
182  unsigned padding;
183 };
184 
185 int px_find_digest(const char *name, PX_MD **res);
186 int px_find_hmac(const char *name, PX_HMAC **res);
187 int px_find_cipher(const char *name, PX_Cipher **res);
188 int px_find_combo(const char *name, PX_Combo **res);
189 
190 void px_THROW_ERROR(int err) pg_attribute_noreturn();
191 const char *px_strerror(int err);
192 
193 const char *px_resolve_alias(const PX_Alias *aliases, const char *name);
194 
195 void px_set_debug_handler(void (*handler) (const char *));
196 
197 void px_memset(void *ptr, int c, size_t len);
198 
199 #ifdef PX_DEBUG
200 void px_debug(const char *fmt,...) pg_attribute_printf(1, 2);
201 #else
202 #define px_debug(...)
203 #endif
204 
205 #define px_md_result_size(md) (md)->result_size(md)
206 #define px_md_block_size(md) (md)->block_size(md)
207 #define px_md_reset(md) (md)->reset(md)
208 #define px_md_update(md, data, dlen) (md)->update(md, data, dlen)
209 #define px_md_finish(md, buf) (md)->finish(md, buf)
210 #define px_md_free(md) (md)->free(md)
211 
212 #define px_hmac_result_size(hmac) (hmac)->result_size(hmac)
213 #define px_hmac_block_size(hmac) (hmac)->block_size(hmac)
214 #define px_hmac_reset(hmac) (hmac)->reset(hmac)
215 #define px_hmac_init(hmac, key, klen) (hmac)->init(hmac, key, klen)
216 #define px_hmac_update(hmac, data, dlen) (hmac)->update(hmac, data, dlen)
217 #define px_hmac_finish(hmac, buf) (hmac)->finish(hmac, buf)
218 #define px_hmac_free(hmac) (hmac)->free(hmac)
219 
220 
221 #define px_cipher_key_size(c) (c)->key_size(c)
222 #define px_cipher_block_size(c) (c)->block_size(c)
223 #define px_cipher_iv_size(c) (c)->iv_size(c)
224 #define px_cipher_init(c, k, klen, iv) (c)->init(c, k, klen, iv)
225 #define px_cipher_encrypt(c, data, dlen, res) \
226  (c)->encrypt(c, data, dlen, res)
227 #define px_cipher_decrypt(c, data, dlen, res) \
228  (c)->decrypt(c, data, dlen, res)
229 #define px_cipher_free(c) (c)->free(c)
230 
231 
232 #define px_combo_encrypt_len(c, dlen) (c)->encrypt_len(c, dlen)
233 #define px_combo_decrypt_len(c, dlen) (c)->decrypt_len(c, dlen)
234 #define px_combo_init(c, key, klen, iv, ivlen) \
235  (c)->init(c, key, klen, iv, ivlen)
236 #define px_combo_encrypt(c, data, dlen, res, rlen) \
237  (c)->encrypt(c, data, dlen, res, rlen)
238 #define px_combo_decrypt(c, data, dlen, res, rlen) \
239  (c)->decrypt(c, data, dlen, res, rlen)
240 #define px_combo_free(c) (c)->free(c)
241 
242 #endif /* __PX_H */
void px_THROW_ERROR(int err) pg_attribute_noreturn()
Definition: px.c:96
void(* free)(PX_Cipher *c)
Definition: px.h:163
unsigned code
Definition: px.h:124
unsigned(* key_size)(PX_Cipher *c)
Definition: px.h:157
void * ptr
Definition: px.h:125
int px_find_digest(const char *name, PX_MD **res)
Definition: internal.c:578
unsigned(* result_size)(PX_MD *h)
Definition: px.h:115
unsigned padding
Definition: px.h:182
int(* decrypt)(PX_Combo *cx, const uint8 *data, unsigned dlen, uint8 *res, unsigned *rlen)
Definition: px.h:175
#define px_free(p)
Definition: px.h:46
struct px_hmac::@14 p
unsigned char uint8
Definition: c.h:256
int px_find_hmac(const char *name, PX_HMAC **res)
Definition: px-hmac.c:143
Definition: px.h:169
void * ptr
Definition: px.h:165
int pstat
Definition: px.h:166
void px_debug(const char *fmt,...) pg_attribute_printf(1
unsigned(* decrypt_len)(PX_Combo *cx, unsigned dlen)
Definition: px.h:178
PX_Cipher * cipher
Definition: px.h:181
Definition: px.h:129
void(* update)(PX_MD *h, const uint8 *data, unsigned dlen)
Definition: px.h:118
#define pg_attribute_printf(f, a)
Definition: c.h:627
void(* init)(PX_HMAC *h, const uint8 *key, unsigned klen)
Definition: px.h:143
PX_MD * md
Definition: px.h:145
void(* free)(PX_Combo *cx)
Definition: px.h:179
int cx(PlannerInfo *root, Gene *tour1, Gene *tour2, Gene *offspring, int num_gene, City *city_table)
unsigned(* block_size)(PX_HMAC *h)
Definition: px.h:138
void(* free)(PX_MD *h)
Definition: px.h:120
void(* update)(PX_HMAC *h, const uint8 *data, unsigned dlen)
Definition: px.h:140
int px_find_combo(const char *name, PX_Combo **res)
Definition: px.c:410
Definition: px.h:113
int px_find_cipher(const char *name, PX_Cipher **res)
Definition: internal.c:597
char * c
int(* init)(PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
Definition: px.h:160
unsigned(* iv_size)(PX_Cipher *c)
Definition: px.h:158
int(* encrypt)(PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
Definition: px.h:161
int(* encrypt)(PX_Combo *cx, const uint8 *data, unsigned dlen, uint8 *res, unsigned *rlen)
Definition: px.h:173
void(* finish)(PX_MD *h, uint8 *dst)
Definition: px.h:119
#define px_realloc(p, s)
Definition: px.h:45
uint8 * opad
Definition: px.h:150
void px_set_debug_handler(void(*handler)(const char *))
Definition: px.c:154
union px_digest::@13 p
const char * px_strerror(int err)
Definition: px.c:122
char * alias
Definition: px.h:131
const char * px_resolve_alias(const PX_Alias *aliases, const char *name)
Definition: px.c:140
unsigned(* result_size)(PX_HMAC *h)
Definition: px.h:137
void(* finish)(PX_HMAC *h, uint8 *dst)
Definition: px.h:141
Definition: px.h:154
const char * name
Definition: encode.c:521
#define pg_attribute_noreturn()
Definition: c.h:642
#define px_alloc(s)
Definition: px.h:44
uint8 * ipad
Definition: px.h:149
void(* reset)(PX_MD *h)
Definition: px.h:117
char * name
Definition: px.h:132
int(* init)(PX_Combo *cx, const uint8 *key, unsigned klen, const uint8 *iv, unsigned ivlen)
Definition: px.h:171
Definition: px.h:135
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:134
unsigned(* block_size)(PX_Cipher *c)
Definition: px.h:156
unsigned(* block_size)(PX_MD *h)
Definition: px.h:116
void(* reset)(PX_HMAC *h)
Definition: px.h:139
int(* decrypt)(PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
Definition: px.h:162
void(* free)(PX_HMAC *h)
Definition: px.h:142
unsigned(* encrypt_len)(PX_Combo *cx, unsigned dlen)
Definition: px.h:177