2001-08-21 09:32:01 +08:00
|
|
|
/*
|
|
|
|
* px.h
|
|
|
|
* Header file for pgcrypto.
|
2001-10-25 13:50:21 +08:00
|
|
|
*
|
2001-08-21 09:32:01 +08:00
|
|
|
* Copyright (c) 2001 Marko Kreen
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
2001-10-25 13:50:21 +08:00
|
|
|
* notice, this list of conditions and the following disclaimer.
|
2001-08-21 09:32:01 +08:00
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
2001-10-25 13:50:21 +08:00
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2001-08-21 09:32:01 +08:00
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
2001-10-25 13:50:21 +08:00
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
2001-08-21 09:32:01 +08:00
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
2005-07-10 11:55:28 +08:00
|
|
|
* $PostgreSQL: pgsql/contrib/pgcrypto/px.h,v 1.13 2005/07/10 03:55:28 momjian Exp $
|
2001-08-21 09:32:01 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __PX_H
|
|
|
|
#define __PX_H
|
|
|
|
|
2001-12-01 01:39:20 +08:00
|
|
|
#include <sys/types.h>
|
2002-10-22 02:56:30 +08:00
|
|
|
#include <sys/param.h>
|
2001-11-30 03:40:37 +08:00
|
|
|
#ifdef HAVE_ENDIAN_H
|
|
|
|
#include <endian.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef BYTE_ORDER
|
|
|
|
#error BYTE_ORDER must be defined as LITTLE_ENDIAN or BIG_ENDIAN
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2005-03-21 13:18:46 +08:00
|
|
|
#ifndef PX_OWN_ALLOC
|
2001-08-21 09:32:01 +08:00
|
|
|
|
|
|
|
#define px_alloc(s) palloc(s)
|
2005-03-21 13:18:46 +08:00
|
|
|
#define px_realloc(p, s) repalloc(p, s)
|
2001-10-25 13:50:21 +08:00
|
|
|
#define px_free(p) pfree(p)
|
2001-08-21 09:32:01 +08:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2005-03-21 13:18:46 +08:00
|
|
|
void *px_alloc(size_t s);
|
|
|
|
void *px_realloc(void *p, size_t s);
|
|
|
|
void px_free(void *p);
|
2001-08-21 09:32:01 +08:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* max len of 'type' parms */
|
2001-10-25 13:50:21 +08:00
|
|
|
#define PX_MAX_NAMELEN 128
|
2001-08-21 09:32:01 +08:00
|
|
|
|
|
|
|
/* max salt returned */
|
|
|
|
#define PX_MAX_SALT_LEN 128
|
|
|
|
|
2005-03-21 13:19:55 +08:00
|
|
|
/*
|
|
|
|
* PX error codes
|
|
|
|
*/
|
|
|
|
#define PXE_OK 0
|
|
|
|
#define PXE_ERR_GENERIC -1
|
|
|
|
#define PXE_NO_HASH -2
|
|
|
|
#define PXE_NO_CIPHER -3
|
|
|
|
#define PXE_NOTBLOCKSIZE -4
|
|
|
|
#define PXE_BAD_OPTION -5
|
|
|
|
#define PXE_BAD_FORMAT -6
|
|
|
|
#define PXE_KEY_TOO_BIG -7
|
|
|
|
#define PXE_CIPHER_INIT -8
|
|
|
|
#define PXE_HASH_UNUSABLE_FOR_HMAC -9
|
|
|
|
#define PXE_DEV_READ_ERROR -10
|
|
|
|
#define PXE_OSSL_RAND_ERROR -11
|
|
|
|
#define PXE_BUG -12
|
|
|
|
#define PXE_ARGUMENT_ERROR -13
|
|
|
|
#define PXE_UNKNOWN_SALT_ALGO -14
|
|
|
|
#define PXE_BAD_SALT_ROUNDS -15
|
|
|
|
#define PXE_MCRYPT_INTERNAL -16
|
2005-03-21 13:22:14 +08:00
|
|
|
#define PXE_NO_RANDOM -17
|
2001-08-21 09:32:01 +08:00
|
|
|
|
|
|
|
typedef struct px_digest PX_MD;
|
|
|
|
typedef struct px_alias PX_Alias;
|
|
|
|
typedef struct px_hmac PX_HMAC;
|
|
|
|
typedef struct px_cipher PX_Cipher;
|
|
|
|
typedef struct px_combo PX_Combo;
|
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
struct px_digest
|
|
|
|
{
|
2001-11-20 23:50:53 +08:00
|
|
|
unsigned (*result_size) (PX_MD * h);
|
|
|
|
unsigned (*block_size) (PX_MD * h);
|
2001-10-25 13:50:21 +08:00
|
|
|
void (*reset) (PX_MD * h);
|
2001-11-20 23:50:53 +08:00
|
|
|
void (*update) (PX_MD * h, const uint8 *data, unsigned dlen);
|
2001-10-25 13:50:21 +08:00
|
|
|
void (*finish) (PX_MD * h, uint8 *dst);
|
|
|
|
void (*free) (PX_MD * h);
|
2001-08-21 09:32:01 +08:00
|
|
|
/* private */
|
2001-10-25 13:50:21 +08:00
|
|
|
union
|
|
|
|
{
|
2001-11-20 23:50:53 +08:00
|
|
|
unsigned code;
|
2001-08-21 09:32:01 +08:00
|
|
|
const void *ptr;
|
2001-10-25 13:50:21 +08:00
|
|
|
} p;
|
2001-08-21 09:32:01 +08:00
|
|
|
};
|
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
struct px_alias
|
|
|
|
{
|
|
|
|
char *alias;
|
|
|
|
char *name;
|
2001-08-21 09:32:01 +08:00
|
|
|
};
|
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
struct px_hmac
|
|
|
|
{
|
2001-11-20 23:50:53 +08:00
|
|
|
unsigned (*result_size) (PX_HMAC * h);
|
|
|
|
unsigned (*block_size) (PX_HMAC * h);
|
2001-10-25 13:50:21 +08:00
|
|
|
void (*reset) (PX_HMAC * h);
|
2001-11-20 23:50:53 +08:00
|
|
|
void (*update) (PX_HMAC * h, const uint8 *data, unsigned dlen);
|
2001-10-25 13:50:21 +08:00
|
|
|
void (*finish) (PX_HMAC * h, uint8 *dst);
|
|
|
|
void (*free) (PX_HMAC * h);
|
2001-11-20 23:50:53 +08:00
|
|
|
void (*init) (PX_HMAC * h, const uint8 *key, unsigned klen);
|
2001-10-25 13:50:21 +08:00
|
|
|
|
|
|
|
PX_MD *md;
|
2001-08-21 09:32:01 +08:00
|
|
|
/* private */
|
2001-10-25 13:50:21 +08:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
uint8 *ipad;
|
|
|
|
uint8 *opad;
|
|
|
|
} p;
|
2001-08-21 09:32:01 +08:00
|
|
|
};
|
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
struct px_cipher
|
|
|
|
{
|
2001-11-20 23:50:53 +08:00
|
|
|
unsigned (*block_size) (PX_Cipher * c);
|
|
|
|
unsigned (*key_size) (PX_Cipher * c); /* max key len */
|
|
|
|
unsigned (*iv_size) (PX_Cipher * c);
|
2001-10-25 13:50:21 +08:00
|
|
|
|
2001-11-20 23:50:53 +08:00
|
|
|
int (*init) (PX_Cipher * c, const uint8 *key, unsigned klen, const uint8 *iv);
|
|
|
|
int (*encrypt) (PX_Cipher * c, const uint8 *data, unsigned dlen, uint8 *res);
|
|
|
|
int (*decrypt) (PX_Cipher * c, const uint8 *data, unsigned dlen, uint8 *res);
|
2001-10-25 13:50:21 +08:00
|
|
|
void (*free) (PX_Cipher * c);
|
2001-08-21 09:32:01 +08:00
|
|
|
/* private */
|
2001-10-25 13:50:21 +08:00
|
|
|
void *ptr;
|
|
|
|
int pstat; /* mcrypt uses it */
|
2001-08-21 09:32:01 +08:00
|
|
|
};
|
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
struct px_combo
|
|
|
|
{
|
2001-11-20 23:50:53 +08:00
|
|
|
int (*init) (PX_Combo * cx, const uint8 *key, unsigned klen,
|
|
|
|
const uint8 *iv, unsigned ivlen);
|
|
|
|
int (*encrypt) (PX_Combo * cx, const uint8 *data, unsigned dlen,
|
|
|
|
uint8 *res, unsigned *rlen);
|
|
|
|
int (*decrypt) (PX_Combo * cx, const uint8 *data, unsigned dlen,
|
|
|
|
uint8 *res, unsigned *rlen);
|
|
|
|
unsigned (*encrypt_len) (PX_Combo * cx, unsigned dlen);
|
|
|
|
unsigned (*decrypt_len) (PX_Combo * cx, unsigned dlen);
|
2001-10-25 13:50:21 +08:00
|
|
|
void (*free) (PX_Combo * cx);
|
|
|
|
|
|
|
|
PX_Cipher *cipher;
|
2001-11-20 23:50:53 +08:00
|
|
|
unsigned padding;
|
2001-08-21 09:32:01 +08:00
|
|
|
};
|
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
int px_find_digest(const char *name, PX_MD ** res);
|
|
|
|
int px_find_hmac(const char *name, PX_HMAC ** res);
|
|
|
|
int px_find_cipher(const char *name, PX_Cipher ** res);
|
|
|
|
int px_find_combo(const char *name, PX_Combo ** res);
|
2001-08-21 09:32:01 +08:00
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
int px_get_random_bytes(uint8 *dst, unsigned count);
|
2005-03-21 13:22:14 +08:00
|
|
|
int px_get_pseudo_random_bytes(uint8 *dst, unsigned count);
|
2005-07-10 11:55:28 +08:00
|
|
|
int px_add_entropy(const uint8 *data, unsigned count);
|
|
|
|
|
|
|
|
unsigned px_acquire_system_randomness(uint8 *dst);
|
2001-09-23 12:12:44 +08:00
|
|
|
|
2005-03-21 13:19:55 +08:00
|
|
|
const char *px_strerror(int err);
|
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
const char *px_resolve_alias(const PX_Alias * aliases, const char *name);
|
2001-08-21 09:32:01 +08:00
|
|
|
|
|
|
|
#define px_md_result_size(md) (md)->result_size(md)
|
|
|
|
#define px_md_block_size(md) (md)->block_size(md)
|
|
|
|
#define px_md_reset(md) (md)->reset(md)
|
|
|
|
#define px_md_update(md, data, dlen) (md)->update(md, data, dlen)
|
|
|
|
#define px_md_finish(md, buf) (md)->finish(md, buf)
|
|
|
|
#define px_md_free(md) (md)->free(md)
|
|
|
|
|
|
|
|
#define px_hmac_result_size(hmac) (hmac)->result_size(hmac)
|
|
|
|
#define px_hmac_block_size(hmac) (hmac)->block_size(hmac)
|
|
|
|
#define px_hmac_reset(hmac) (hmac)->reset(hmac)
|
|
|
|
#define px_hmac_init(hmac, key, klen) (hmac)->init(hmac, key, klen)
|
|
|
|
#define px_hmac_update(hmac, data, dlen) (hmac)->update(hmac, data, dlen)
|
|
|
|
#define px_hmac_finish(hmac, buf) (hmac)->finish(hmac, buf)
|
|
|
|
#define px_hmac_free(hmac) (hmac)->free(hmac)
|
|
|
|
|
|
|
|
|
|
|
|
#define px_cipher_key_size(c) (c)->key_size(c)
|
|
|
|
#define px_cipher_block_size(c) (c)->block_size(c)
|
|
|
|
#define px_cipher_iv_size(c) (c)->iv_size(c)
|
|
|
|
#define px_cipher_init(c, k, klen, iv) (c)->init(c, k, klen, iv)
|
|
|
|
#define px_cipher_encrypt(c, data, dlen, res) \
|
|
|
|
(c)->encrypt(c, data, dlen, res)
|
|
|
|
#define px_cipher_decrypt(c, data, dlen, res) \
|
|
|
|
(c)->decrypt(c, data, dlen, res)
|
|
|
|
#define px_cipher_free(c) (c)->free(c)
|
|
|
|
|
|
|
|
|
|
|
|
#define px_combo_encrypt_len(c, dlen) (c)->encrypt_len(c, dlen)
|
|
|
|
#define px_combo_decrypt_len(c, dlen) (c)->decrypt_len(c, dlen)
|
|
|
|
#define px_combo_init(c, key, klen, iv, ivlen) \
|
|
|
|
(c)->init(c, key, klen, iv, ivlen)
|
|
|
|
#define px_combo_encrypt(c, data, dlen, res, rlen) \
|
|
|
|
(c)->encrypt(c, data, dlen, res, rlen)
|
|
|
|
#define px_combo_decrypt(c, data, dlen, res, rlen) \
|
|
|
|
(c)->decrypt(c, data, dlen, res, rlen)
|
|
|
|
#define px_combo_free(c) (c)->free(c)
|
|
|
|
|
2001-11-06 01:46:40 +08:00
|
|
|
#endif /* __PX_H */
|