~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to contrib/pgcrypto/px.h

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 * $PostgreSQL: pgsql/contrib/pgcrypto/px.h,v 1.9 2003-11-29 22:39:28 pgsql Exp $
 
30
 */
 
31
 
 
32
#ifndef __PX_H
 
33
#define __PX_H
 
34
 
 
35
#include <sys/types.h>
 
36
#include <sys/param.h>
 
37
#ifdef HAVE_ENDIAN_H
 
38
#include <endian.h>
 
39
#endif
 
40
 
 
41
#ifndef BYTE_ORDER
 
42
#error BYTE_ORDER must be defined as LITTLE_ENDIAN or BIG_ENDIAN
 
43
#endif
 
44
 
 
45
 
 
46
#if 1
 
47
 
 
48
#define px_alloc(s) palloc(s)
 
49
#define px_realloc(p, s) prealloc(p, s)
 
50
#define px_free(p)      pfree(p)
 
51
 
 
52
#else
 
53
 
 
54
void       *xalloc(size_t s);
 
55
void       *xrealloc(void *p, size_t s);
 
56
void            xfree(void *p);
 
57
 
 
58
#define px_alloc(s) xalloc(s)
 
59
#define px_realloc(p, s) xrealloc(p, s)
 
60
#define px_free(p)      xfree(p)
 
61
#endif
 
62
 
 
63
/* max len of 'type' parms */
 
64
#define PX_MAX_NAMELEN          128
 
65
 
 
66
/* max salt returned */
 
67
#define PX_MAX_SALT_LEN         128
 
68
 
 
69
 
 
70
typedef struct px_digest PX_MD;
 
71
typedef struct px_alias PX_Alias;
 
72
typedef struct px_hmac PX_HMAC;
 
73
typedef struct px_cipher PX_Cipher;
 
74
typedef struct px_combo PX_Combo;
 
75
 
 
76
struct px_digest
 
77
{
 
78
        unsigned        (*result_size) (PX_MD * h);
 
79
        unsigned        (*block_size) (PX_MD * h);
 
80
        void            (*reset) (PX_MD * h);
 
81
        void            (*update) (PX_MD * h, const uint8 *data, unsigned dlen);
 
82
        void            (*finish) (PX_MD * h, uint8 *dst);
 
83
        void            (*free) (PX_MD * h);
 
84
        /* private */
 
85
        union
 
86
        {
 
87
                unsigned        code;
 
88
                const void *ptr;
 
89
        }                       p;
 
90
};
 
91
 
 
92
struct px_alias
 
93
{
 
94
        char       *alias;
 
95
        char       *name;
 
96
};
 
97
 
 
98
struct px_hmac
 
99
{
 
100
        unsigned        (*result_size) (PX_HMAC * h);
 
101
        unsigned        (*block_size) (PX_HMAC * h);
 
102
        void            (*reset) (PX_HMAC * h);
 
103
        void            (*update) (PX_HMAC * h, const uint8 *data, unsigned dlen);
 
104
        void            (*finish) (PX_HMAC * h, uint8 *dst);
 
105
        void            (*free) (PX_HMAC * h);
 
106
        void            (*init) (PX_HMAC * h, const uint8 *key, unsigned klen);
 
107
 
 
108
        PX_MD      *md;
 
109
        /* private */
 
110
        struct
 
111
        {
 
112
                uint8      *ipad;
 
113
                uint8      *opad;
 
114
        }                       p;
 
115
};
 
116
 
 
117
struct px_cipher
 
118
{
 
119
        unsigned        (*block_size) (PX_Cipher * c);
 
120
        unsigned        (*key_size) (PX_Cipher * c);    /* max key len */
 
121
        unsigned        (*iv_size) (PX_Cipher * c);
 
122
 
 
123
        int                     (*init) (PX_Cipher * c, const uint8 *key, unsigned klen, const uint8 *iv);
 
124
        int                     (*encrypt) (PX_Cipher * c, const uint8 *data, unsigned dlen, uint8 *res);
 
125
        int                     (*decrypt) (PX_Cipher * c, const uint8 *data, unsigned dlen, uint8 *res);
 
126
        void            (*free) (PX_Cipher * c);
 
127
        /* private */
 
128
        void       *ptr;
 
129
        int                     pstat;                  /* mcrypt uses it */
 
130
};
 
131
 
 
132
struct px_combo
 
133
{
 
134
        int                     (*init) (PX_Combo * cx, const uint8 *key, unsigned klen,
 
135
                                                                         const uint8 *iv, unsigned ivlen);
 
136
        int                     (*encrypt) (PX_Combo * cx, const uint8 *data, unsigned dlen,
 
137
                                                                                uint8 *res, unsigned *rlen);
 
138
        int                     (*decrypt) (PX_Combo * cx, const uint8 *data, unsigned dlen,
 
139
                                                                                uint8 *res, unsigned *rlen);
 
140
        unsigned        (*encrypt_len) (PX_Combo * cx, unsigned dlen);
 
141
        unsigned        (*decrypt_len) (PX_Combo * cx, unsigned dlen);
 
142
        void            (*free) (PX_Combo * cx);
 
143
 
 
144
        PX_Cipher  *cipher;
 
145
        unsigned        padding;
 
146
};
 
147
 
 
148
int                     px_find_digest(const char *name, PX_MD ** res);
 
149
int                     px_find_hmac(const char *name, PX_HMAC ** res);
 
150
int                     px_find_cipher(const char *name, PX_Cipher ** res);
 
151
int                     px_find_combo(const char *name, PX_Combo ** res);
 
152
 
 
153
int                     px_get_random_bytes(uint8 *dst, unsigned count);
 
154
 
 
155
const char *px_resolve_alias(const PX_Alias * aliases, const char *name);
 
156
 
 
157
#define px_md_result_size(md)           (md)->result_size(md)
 
158
#define px_md_block_size(md)            (md)->block_size(md)
 
159
#define px_md_reset(md)                 (md)->reset(md)
 
160
#define px_md_update(md, data, dlen)    (md)->update(md, data, dlen)
 
161
#define px_md_finish(md, buf)           (md)->finish(md, buf)
 
162
#define px_md_free(md)                  (md)->free(md)
 
163
 
 
164
#define px_hmac_result_size(hmac)       (hmac)->result_size(hmac)
 
165
#define px_hmac_block_size(hmac)        (hmac)->block_size(hmac)
 
166
#define px_hmac_reset(hmac)             (hmac)->reset(hmac)
 
167
#define px_hmac_init(hmac, key, klen)   (hmac)->init(hmac, key, klen)
 
168
#define px_hmac_update(hmac, data, dlen) (hmac)->update(hmac, data, dlen)
 
169
#define px_hmac_finish(hmac, buf)       (hmac)->finish(hmac, buf)
 
170
#define px_hmac_free(hmac)              (hmac)->free(hmac)
 
171
 
 
172
 
 
173
#define px_cipher_key_size(c)           (c)->key_size(c)
 
174
#define px_cipher_block_size(c)         (c)->block_size(c)
 
175
#define px_cipher_iv_size(c)            (c)->iv_size(c)
 
176
#define px_cipher_init(c, k, klen, iv)  (c)->init(c, k, klen, iv)
 
177
#define px_cipher_encrypt(c, data, dlen, res) \
 
178
                                        (c)->encrypt(c, data, dlen, res)
 
179
#define px_cipher_decrypt(c, data, dlen, res) \
 
180
                                        (c)->decrypt(c, data, dlen, res)
 
181
#define px_cipher_free(c)               (c)->free(c)
 
182
 
 
183
 
 
184
#define px_combo_encrypt_len(c, dlen)   (c)->encrypt_len(c, dlen)
 
185
#define px_combo_decrypt_len(c, dlen)   (c)->decrypt_len(c, dlen)
 
186
#define px_combo_init(c, key, klen, iv, ivlen) \
 
187
                                        (c)->init(c, key, klen, iv, ivlen)
 
188
#define px_combo_encrypt(c, data, dlen, res, rlen) \
 
189
                                        (c)->encrypt(c, data, dlen, res, rlen)
 
190
#define px_combo_decrypt(c, data, dlen, res, rlen) \
 
191
                                        (c)->decrypt(c, data, dlen, res, rlen)
 
192
#define px_combo_free(c)                (c)->free(c)
 
193
 
 
194
#endif   /* __PX_H */