diff options
Diffstat (limited to 'contrib/pgcrypto/openssl.c')
-rw-r--r-- | contrib/pgcrypto/openssl.c | 242 |
1 files changed, 145 insertions, 97 deletions
diff --git a/contrib/pgcrypto/openssl.c b/contrib/pgcrypto/openssl.c index 6b2ae9c98a..f9769b7e20 100644 --- a/contrib/pgcrypto/openssl.c +++ b/contrib/pgcrypto/openssl.c @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: openssl.c,v 1.5 2001/09/23 04:12:44 momjian Exp $ + * $Id: openssl.c,v 1.6 2001/10/25 05:49:19 momjian Exp $ */ #include <postgres.h> @@ -60,7 +60,7 @@ digest_reset(PX_MD * h) } static void -digest_update(PX_MD * h, const uint8 * data, uint dlen) +digest_update(PX_MD * h, const uint8 *data, uint dlen) { EVP_MD_CTX *ctx = (EVP_MD_CTX *) h->p.ptr; @@ -68,7 +68,7 @@ digest_update(PX_MD * h, const uint8 * data, uint dlen) } static void -digest_finish(PX_MD * h, uint8 * dst) +digest_finish(PX_MD * h, uint8 *dst) { EVP_MD_CTX *ctx = (EVP_MD_CTX *) h->p.ptr; @@ -94,50 +94,57 @@ digest_free(PX_MD * h) */ -typedef struct { - union { - struct { - BF_KEY key; - int num; - } bf; +typedef struct +{ + union + { + struct + { + BF_KEY key; + int num; + } bf; EVP_CIPHER_CTX evp_ctx; - } u; + } u; const EVP_CIPHER *evp_ciph; - uint8 key[EVP_MAX_KEY_LENGTH]; - uint8 iv[EVP_MAX_IV_LENGTH]; - uint klen; - uint init; -} ossldata; + uint8 key[EVP_MAX_KEY_LENGTH]; + uint8 iv[EVP_MAX_IV_LENGTH]; + uint klen; + uint init; +} ossldata; /* generic EVP */ static uint -gen_evp_block_size(PX_Cipher *c) +gen_evp_block_size(PX_Cipher * c) { - ossldata *od = (ossldata *)c->ptr; + ossldata *od = (ossldata *) c->ptr; + return EVP_CIPHER_block_size(od->evp_ciph); } static uint -gen_evp_key_size(PX_Cipher *c) +gen_evp_key_size(PX_Cipher * c) { - ossldata *od = (ossldata *)c->ptr; + ossldata *od = (ossldata *) c->ptr; + return EVP_CIPHER_key_length(od->evp_ciph); } static uint -gen_evp_iv_size(PX_Cipher *c) +gen_evp_iv_size(PX_Cipher * c) { - uint ivlen; - ossldata *od = (ossldata *)c->ptr; + uint ivlen; + ossldata *od = (ossldata *) c->ptr; + ivlen = EVP_CIPHER_iv_length(od->evp_ciph); return ivlen; } static void -gen_evp_free(PX_Cipher *c) +gen_evp_free(PX_Cipher * c) { - ossldata *od = (ossldata*)c->ptr; + ossldata *od = (ossldata *) c->ptr; + memset(od, 0, sizeof(*od)); pfree(od); pfree(c); @@ -146,13 +153,14 @@ gen_evp_free(PX_Cipher *c) /* fun */ static int -gen_evp_init(PX_Cipher *c, const uint8 *key, uint klen, const uint8 *iv) +gen_evp_init(PX_Cipher * c, const uint8 *key, uint klen, const uint8 *iv) { - ossldata *od = (ossldata*)c->ptr; - uint bs = gen_evp_block_size(c); - if (iv) { + ossldata *od = (ossldata *) c->ptr; + uint bs = gen_evp_block_size(c); + + if (iv) memcpy(od->iv, iv, bs); - } else + else memset(od->iv, 0, bs); memcpy(od->key, key, klen); od->klen = klen; @@ -161,19 +169,20 @@ gen_evp_init(PX_Cipher *c, const uint8 *key, uint klen, const uint8 *iv) } static void -_gen_init(PX_Cipher *c, int enc) +_gen_init(PX_Cipher * c, int enc) { - ossldata *od = c->ptr; - + ossldata *od = c->ptr; + od->evp_ciph->init(&od->u.evp_ctx, od->key, od->iv, enc); od->init = 1; od->u.evp_ctx.encrypt = enc; } static int -gen_evp_encrypt(PX_Cipher *c, const uint8 *data, uint dlen, uint8 *res) +gen_evp_encrypt(PX_Cipher * c, const uint8 *data, uint dlen, uint8 *res) { - ossldata *od = c->ptr; + ossldata *od = c->ptr; + if (!od->init) _gen_init(c, 1); od->evp_ciph->do_cipher(&od->u.evp_ctx, res, data, dlen); @@ -181,9 +190,10 @@ gen_evp_encrypt(PX_Cipher *c, const uint8 *data, uint dlen, uint8 *res) } static int -gen_evp_decrypt(PX_Cipher *c, const uint8 *data, uint dlen, uint8 *res) +gen_evp_decrypt(PX_Cipher * c, const uint8 *data, uint dlen, uint8 *res) { - ossldata *od = c->ptr; + ossldata *od = c->ptr; + if (!od->init) _gen_init(c, 0); od->evp_ciph->do_cipher(&od->u.evp_ctx, res, data, dlen); @@ -193,84 +203,95 @@ gen_evp_decrypt(PX_Cipher *c, const uint8 *data, uint dlen, uint8 *res) /* Blowfish */ static int -bf_init(PX_Cipher *c, const uint8 *key, uint klen, const uint8 *iv) +bf_init(PX_Cipher * c, const uint8 *key, uint klen, const uint8 *iv) { - ossldata *od = c->ptr; + ossldata *od = c->ptr; + BF_set_key(&od->u.bf.key, klen, key); - if (iv) { + if (iv) memcpy(od->iv, iv, BF_BLOCK); - } else + else memset(od->iv, 0, BF_BLOCK); od->u.bf.num = 0; return 0; } static int -bf_ecb_encrypt(PX_Cipher *c, const uint8 *data, uint dlen, uint8 *res) +bf_ecb_encrypt(PX_Cipher * c, const uint8 *data, uint dlen, uint8 *res) { - uint bs = gen_evp_block_size(c), i; - ossldata *od = c->ptr; + uint bs = gen_evp_block_size(c), + i; + ossldata *od = c->ptr; + for (i = 0; i < dlen / bs; i++) - BF_ecb_encrypt(data+i*bs, res+i*bs, &od->u.bf.key, BF_ENCRYPT); + BF_ecb_encrypt(data + i * bs, res + i * bs, &od->u.bf.key, BF_ENCRYPT); return 0; } static int -bf_ecb_decrypt(PX_Cipher *c, const uint8 *data, uint dlen, uint8 *res) +bf_ecb_decrypt(PX_Cipher * c, const uint8 *data, uint dlen, uint8 *res) { - uint bs = gen_evp_block_size(c), i; - ossldata *od = c->ptr; + uint bs = gen_evp_block_size(c), + i; + ossldata *od = c->ptr; + for (i = 0; i < dlen / bs; i++) - BF_ecb_encrypt(data+i*bs, res+i*bs, &od->u.bf.key, BF_DECRYPT); + BF_ecb_encrypt(data + i * bs, res + i * bs, &od->u.bf.key, BF_DECRYPT); return 0; } static int -bf_cbc_encrypt(PX_Cipher *c, const uint8 *data, uint dlen, uint8 *res) +bf_cbc_encrypt(PX_Cipher * c, const uint8 *data, uint dlen, uint8 *res) { - ossldata *od = c->ptr; + ossldata *od = c->ptr; + BF_cbc_encrypt(data, res, dlen, &od->u.bf.key, od->iv, BF_ENCRYPT); return 0; } static int -bf_cbc_decrypt(PX_Cipher *c, const uint8 *data, uint dlen, uint8 *res) +bf_cbc_decrypt(PX_Cipher * c, const uint8 *data, uint dlen, uint8 *res) { - ossldata *od = c->ptr; + ossldata *od = c->ptr; + BF_cbc_encrypt(data, res, dlen, &od->u.bf.key, od->iv, BF_DECRYPT); return 0; } static int -bf_cfb64_encrypt(PX_Cipher *c, const uint8 *data, uint dlen, uint8 *res) +bf_cfb64_encrypt(PX_Cipher * c, const uint8 *data, uint dlen, uint8 *res) { - ossldata *od = c->ptr; + ossldata *od = c->ptr; + BF_cfb64_encrypt(data, res, dlen, &od->u.bf.key, od->iv, - &od->u.bf.num, BF_ENCRYPT); + &od->u.bf.num, BF_ENCRYPT); return 0; } static int -bf_cfb64_decrypt(PX_Cipher *c, const uint8 *data, uint dlen, uint8 *res) +bf_cfb64_decrypt(PX_Cipher * c, const uint8 *data, uint dlen, uint8 *res) { - ossldata *od = c->ptr; + ossldata *od = c->ptr; + BF_cfb64_encrypt(data, res, dlen, &od->u.bf.key, od->iv, - &od->u.bf.num, BF_DECRYPT); + &od->u.bf.num, BF_DECRYPT); return 0; } static int -bf_ofb64_encrypt(PX_Cipher *c, const uint8 *data, uint dlen, uint8 *res) +bf_ofb64_encrypt(PX_Cipher * c, const uint8 *data, uint dlen, uint8 *res) { - ossldata *od = c->ptr; + ossldata *od = c->ptr; + BF_ofb64_encrypt(data, res, dlen, &od->u.bf.key, od->iv, &od->u.bf.num); return 0; } static int -bf_ofb64_decrypt(PX_Cipher *c, const uint8 *data, uint dlen, uint8 *res) +bf_ofb64_decrypt(PX_Cipher * c, const uint8 *data, uint dlen, uint8 *res) { - ossldata *od = c->ptr; + ossldata *od = c->ptr; + BF_ofb64_encrypt(data, res, dlen, &od->u.bf.key, od->iv, &od->u.bf.num); return 0; } @@ -279,14 +300,14 @@ bf_ofb64_decrypt(PX_Cipher *c, const uint8 *data, uint dlen, uint8 *res) * aliases */ -static PX_Alias ossl_aliases [] = { - { "bf", "bf-cbc" }, - { "blowfish", "bf-cbc" }, - { "blowfish-cbc", "bf-cbc" }, - { "blowfish-ecb", "bf-ecb" }, - { "blowfish-cfb", "bf-cfb" }, - { "blowfish-ofb", "bf-ofb" }, - { NULL } +static PX_Alias ossl_aliases[] = { + {"bf", "bf-cbc"}, + {"blowfish", "bf-cbc"}, + {"blowfish-cbc", "bf-cbc"}, + {"blowfish-ecb", "bf-ecb"}, + {"blowfish-cfb", "bf-cfb"}, + {"blowfish-ofb", "bf-ofb"}, + {NULL} }; /* @@ -299,19 +320,44 @@ static PX_Alias ossl_mode_aliases [] = { /* * Special handlers */ -struct { - char *name; - PX_Cipher cf; -} spec_types [] = { - { "bf-cbc", { gen_evp_block_size, gen_evp_key_size, gen_evp_iv_size, - bf_init, bf_cbc_encrypt, bf_cbc_decrypt, gen_evp_free}}, - { "bf-ecb", { gen_evp_block_size, gen_evp_key_size, gen_evp_iv_size, - bf_init, bf_ecb_encrypt, bf_ecb_decrypt, gen_evp_free}}, - { "bf-cfb", { gen_evp_block_size, gen_evp_key_size, gen_evp_iv_size, - bf_init, bf_cfb64_encrypt, bf_cfb64_decrypt, gen_evp_free}}, - { "bf-ofb", { gen_evp_block_size, gen_evp_key_size, gen_evp_iv_size, - bf_init, bf_ofb64_encrypt, bf_ofb64_decrypt, gen_evp_free}}, - { NULL } +struct +{ + char *name; + PX_Cipher cf; +} spec_types[] = + +{ + { + "bf-cbc", + { + gen_evp_block_size, gen_evp_key_size, gen_evp_iv_size, + bf_init, bf_cbc_encrypt, bf_cbc_decrypt, gen_evp_free + } + }, + { + "bf-ecb", + { + gen_evp_block_size, gen_evp_key_size, gen_evp_iv_size, + bf_init, bf_ecb_encrypt, bf_ecb_decrypt, gen_evp_free + } + }, + { + "bf-cfb", + { + gen_evp_block_size, gen_evp_key_size, gen_evp_iv_size, + bf_init, bf_cfb64_encrypt, bf_cfb64_decrypt, gen_evp_free + } + }, + { + "bf-ofb", + { + gen_evp_block_size, gen_evp_key_size, gen_evp_iv_size, + bf_init, bf_ofb64_encrypt, bf_ofb64_decrypt, gen_evp_free + } + }, + { + NULL + } }; /* @@ -322,7 +368,7 @@ static PX_Cipher gen_evp_handler = { gen_evp_init, gen_evp_encrypt, gen_evp_decrypt, gen_evp_free }; -static int px_openssl_initialized = 0; +static int px_openssl_initialized = 0; /* ATM not needed static void *o_alloc(uint s) { return px_alloc(s); } @@ -333,7 +379,7 @@ static void o_free(void *p) { px_free(p); } /* PUBLIC functions */ int -px_find_digest(const char *name, PX_MD **res) +px_find_digest(const char *name, PX_MD ** res) { const EVP_MD *md; EVP_MD_CTX *ctx; @@ -342,7 +388,7 @@ px_find_digest(const char *name, PX_MD **res) if (!px_openssl_initialized) { px_openssl_initialized = 1; - /*CRYPTO_set_mem_functions(o_alloc, o_realloc, o_free);*/ + /* CRYPTO_set_mem_functions(o_alloc, o_realloc, o_free); */ OpenSSL_add_all_algorithms(); } @@ -368,17 +414,19 @@ px_find_digest(const char *name, PX_MD **res) int -px_find_cipher(const char *name, PX_Cipher **res) +px_find_cipher(const char *name, PX_Cipher ** res) { - uint i; - PX_Cipher *c = NULL, *csrc; - ossldata *od; + uint i; + PX_Cipher *c = NULL, + *csrc; + ossldata *od; const EVP_CIPHER *evp_c; - if (!px_openssl_initialized) { + if (!px_openssl_initialized) + { px_openssl_initialized = 1; - /*CRYPTO_set_mem_functions(o_alloc, o_realloc, o_free);*/ + /* CRYPTO_set_mem_functions(o_alloc, o_realloc, o_free); */ OpenSSL_add_all_algorithms(); } @@ -390,23 +438,23 @@ px_find_cipher(const char *name, PX_Cipher **res) od = px_alloc(sizeof(*od)); memset(od, 0, sizeof(*od)); od->evp_ciph = evp_c; - + csrc = NULL; for (i = 0; spec_types[i].name; i++) - if (!strcmp(name, spec_types[i].name)) { + if (!strcmp(name, spec_types[i].name)) + { csrc = &spec_types[i].cf; break; } - + if (csrc == NULL) csrc = &gen_evp_handler; c = px_alloc(sizeof(*c)); memcpy(c, csrc, sizeof(*c)); c->ptr = od; - + *res = c; return 0; } - |