00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef __PX_H
00033 #define __PX_H
00034
00035 #include <sys/types.h>
00036 #include <sys/param.h>
00037
00038
00039 #define PX_DEBUG
00040
00041
00042
00043
00044 #ifndef PX_OWN_ALLOC
00045 #define px_alloc(s) palloc(s)
00046 #define px_realloc(p, s) repalloc(p, s)
00047 #define px_free(p) pfree(p)
00048 #else
00049 void *px_alloc(size_t s);
00050 void *px_realloc(void *p, size_t s);
00051 void px_free(void *p);
00052 #endif
00053
00054
00055 #define PX_MAX_NAMELEN 128
00056
00057
00058 #define PX_MAX_SALT_LEN 128
00059
00060
00061
00062
00063 #define PXE_OK 0
00064 #define PXE_ERR_GENERIC -1
00065 #define PXE_NO_HASH -2
00066 #define PXE_NO_CIPHER -3
00067 #define PXE_NOTBLOCKSIZE -4
00068 #define PXE_BAD_OPTION -5
00069 #define PXE_BAD_FORMAT -6
00070 #define PXE_KEY_TOO_BIG -7
00071 #define PXE_CIPHER_INIT -8
00072 #define PXE_HASH_UNUSABLE_FOR_HMAC -9
00073 #define PXE_DEV_READ_ERROR -10
00074 #define PXE_OSSL_RAND_ERROR -11
00075 #define PXE_BUG -12
00076 #define PXE_ARGUMENT_ERROR -13
00077 #define PXE_UNKNOWN_SALT_ALGO -14
00078 #define PXE_BAD_SALT_ROUNDS -15
00079 #define PXE_MCRYPT_INTERNAL -16
00080 #define PXE_NO_RANDOM -17
00081 #define PXE_DECRYPT_FAILED -18
00082
00083 #define PXE_MBUF_SHORT_READ -50
00084
00085 #define PXE_PGP_CORRUPT_DATA -100
00086 #define PXE_PGP_CORRUPT_ARMOR -101
00087 #define PXE_PGP_UNSUPPORTED_COMPR -102
00088 #define PXE_PGP_UNSUPPORTED_CIPHER -103
00089 #define PXE_PGP_UNSUPPORTED_HASH -104
00090 #define PXE_PGP_COMPRESSION_ERROR -105
00091 #define PXE_PGP_NOT_TEXT -106
00092 #define PXE_PGP_UNEXPECTED_PKT -107
00093 #define PXE_PGP_NO_BIGNUM -108
00094 #define PXE_PGP_MATH_FAILED -109
00095 #define PXE_PGP_SHORT_ELGAMAL_KEY -110
00096 #define PXE_PGP_RSA_UNSUPPORTED -111
00097 #define PXE_PGP_UNKNOWN_PUBALGO -112
00098 #define PXE_PGP_WRONG_KEY -113
00099 #define PXE_PGP_MULTIPLE_KEYS -114
00100 #define PXE_PGP_EXPECT_PUBLIC_KEY -115
00101 #define PXE_PGP_EXPECT_SECRET_KEY -116
00102 #define PXE_PGP_NOT_V4_KEYPKT -117
00103 #define PXE_PGP_KEYPKT_CORRUPT -118
00104 #define PXE_PGP_NO_USABLE_KEY -119
00105 #define PXE_PGP_NEED_SECRET_PSW -120
00106 #define PXE_PGP_BAD_S2K_MODE -121
00107 #define PXE_PGP_UNSUPPORTED_PUBALGO -122
00108 #define PXE_PGP_MULTIPLE_SUBKEYS -123
00109
00110
00111 typedef struct px_digest PX_MD;
00112 typedef struct px_alias PX_Alias;
00113 typedef struct px_hmac PX_HMAC;
00114 typedef struct px_cipher PX_Cipher;
00115 typedef struct px_combo PX_Combo;
00116
00117 struct px_digest
00118 {
00119 unsigned (*result_size) (PX_MD *h);
00120 unsigned (*block_size) (PX_MD *h);
00121 void (*reset) (PX_MD *h);
00122 void (*update) (PX_MD *h, const uint8 *data, unsigned dlen);
00123 void (*finish) (PX_MD *h, uint8 *dst);
00124 void (*free) (PX_MD *h);
00125
00126 union
00127 {
00128 unsigned code;
00129 void *ptr;
00130 } p;
00131 };
00132
00133 struct px_alias
00134 {
00135 char *alias;
00136 char *name;
00137 };
00138
00139 struct px_hmac
00140 {
00141 unsigned (*result_size) (PX_HMAC *h);
00142 unsigned (*block_size) (PX_HMAC *h);
00143 void (*reset) (PX_HMAC *h);
00144 void (*update) (PX_HMAC *h, const uint8 *data, unsigned dlen);
00145 void (*finish) (PX_HMAC *h, uint8 *dst);
00146 void (*free) (PX_HMAC *h);
00147 void (*init) (PX_HMAC *h, const uint8 *key, unsigned klen);
00148
00149 PX_MD *md;
00150
00151 struct
00152 {
00153 uint8 *ipad;
00154 uint8 *opad;
00155 } p;
00156 };
00157
00158 struct px_cipher
00159 {
00160 unsigned (*block_size) (PX_Cipher *c);
00161 unsigned (*key_size) (PX_Cipher *c);
00162 unsigned (*iv_size) (PX_Cipher *c);
00163
00164 int (*init) (PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv);
00165 int (*encrypt) (PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res);
00166 int (*decrypt) (PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res);
00167 void (*free) (PX_Cipher *c);
00168
00169 void *ptr;
00170 int pstat;
00171 };
00172
00173 struct px_combo
00174 {
00175 int (*init) (PX_Combo *cx, const uint8 *key, unsigned klen,
00176 const uint8 *iv, unsigned ivlen);
00177 int (*encrypt) (PX_Combo *cx, const uint8 *data, unsigned dlen,
00178 uint8 *res, unsigned *rlen);
00179 int (*decrypt) (PX_Combo *cx, const uint8 *data, unsigned dlen,
00180 uint8 *res, unsigned *rlen);
00181 unsigned (*encrypt_len) (PX_Combo *cx, unsigned dlen);
00182 unsigned (*decrypt_len) (PX_Combo *cx, unsigned dlen);
00183 void (*free) (PX_Combo *cx);
00184
00185 PX_Cipher *cipher;
00186 unsigned padding;
00187 };
00188
00189 int px_find_digest(const char *name, PX_MD **res);
00190 int px_find_hmac(const char *name, PX_HMAC **res);
00191 int px_find_cipher(const char *name, PX_Cipher **res);
00192 int px_find_combo(const char *name, PX_Combo **res);
00193
00194 int px_get_random_bytes(uint8 *dst, unsigned count);
00195 int px_get_pseudo_random_bytes(uint8 *dst, unsigned count);
00196 int px_add_entropy(const uint8 *data, unsigned count);
00197
00198 unsigned px_acquire_system_randomness(uint8 *dst);
00199
00200 const char *px_strerror(int err);
00201
00202 const char *px_resolve_alias(const PX_Alias *aliases, const char *name);
00203
00204 void px_set_debug_handler(void (*handler) (const char *));
00205
00206 #ifdef PX_DEBUG
00207 void
00208 px_debug(const char *fmt,...)
00209 __attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));
00210 #else
00211 #define px_debug(...)
00212 #endif
00213
00214 #define px_md_result_size(md) (md)->result_size(md)
00215 #define px_md_block_size(md) (md)->block_size(md)
00216 #define px_md_reset(md) (md)->reset(md)
00217 #define px_md_update(md, data, dlen) (md)->update(md, data, dlen)
00218 #define px_md_finish(md, buf) (md)->finish(md, buf)
00219 #define px_md_free(md) (md)->free(md)
00220
00221 #define px_hmac_result_size(hmac) (hmac)->result_size(hmac)
00222 #define px_hmac_block_size(hmac) (hmac)->block_size(hmac)
00223 #define px_hmac_reset(hmac) (hmac)->reset(hmac)
00224 #define px_hmac_init(hmac, key, klen) (hmac)->init(hmac, key, klen)
00225 #define px_hmac_update(hmac, data, dlen) (hmac)->update(hmac, data, dlen)
00226 #define px_hmac_finish(hmac, buf) (hmac)->finish(hmac, buf)
00227 #define px_hmac_free(hmac) (hmac)->free(hmac)
00228
00229
00230 #define px_cipher_key_size(c) (c)->key_size(c)
00231 #define px_cipher_block_size(c) (c)->block_size(c)
00232 #define px_cipher_iv_size(c) (c)->iv_size(c)
00233 #define px_cipher_init(c, k, klen, iv) (c)->init(c, k, klen, iv)
00234 #define px_cipher_encrypt(c, data, dlen, res) \
00235 (c)->encrypt(c, data, dlen, res)
00236 #define px_cipher_decrypt(c, data, dlen, res) \
00237 (c)->decrypt(c, data, dlen, res)
00238 #define px_cipher_free(c) (c)->free(c)
00239
00240
00241 #define px_combo_encrypt_len(c, dlen) (c)->encrypt_len(c, dlen)
00242 #define px_combo_decrypt_len(c, dlen) (c)->decrypt_len(c, dlen)
00243 #define px_combo_init(c, key, klen, iv, ivlen) \
00244 (c)->init(c, key, klen, iv, ivlen)
00245 #define px_combo_encrypt(c, data, dlen, res, rlen) \
00246 (c)->encrypt(c, data, dlen, res, rlen)
00247 #define px_combo_decrypt(c, data, dlen, res, rlen) \
00248 (c)->decrypt(c, data, dlen, res, rlen)
00249 #define px_combo_free(c) (c)->free(c)
00250
00251 #endif