diff options
Diffstat (limited to 'contrib/pgcrypto/internal.c')
-rw-r--r-- | contrib/pgcrypto/internal.c | 280 |
1 files changed, 165 insertions, 115 deletions
diff --git a/contrib/pgcrypto/internal.c b/contrib/pgcrypto/internal.c index 421f5f17392..00ff81e5b7a 100644 --- a/contrib/pgcrypto/internal.c +++ b/contrib/pgcrypto/internal.c @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: internal.c,v 1.5 2001/10/15 19:12:48 tgl Exp $ + * $Id: internal.c,v 1.6 2001/10/25 05:49:19 momjian Exp $ */ @@ -61,11 +61,18 @@ static struct int_digest { char *name; void (*init) (PX_MD * h); -} int_digest_list[] = +} int_digest_list[] = + { - { "md5", init_md5 }, - { "sha1", init_sha1 }, - { NULL, NULL } + { + "md5", init_md5 + }, + { + "sha1", init_sha1 + }, + { + NULL, NULL + } }; /* MD5 */ @@ -83,7 +90,7 @@ int_md5_block_len(PX_MD * h) } static void -int_md5_update(PX_MD * h, const uint8 * data, uint dlen) +int_md5_update(PX_MD * h, const uint8 *data, uint dlen) { MD5_CTX *ctx = (MD5_CTX *) h->p.ptr; @@ -99,7 +106,7 @@ int_md5_reset(PX_MD * h) } static void -int_md5_finish(PX_MD * h, uint8 * dst) +int_md5_finish(PX_MD * h, uint8 *dst) { MD5_CTX *ctx = (MD5_CTX *) h->p.ptr; @@ -130,7 +137,7 @@ int_sha1_block_len(PX_MD * h) } static void -int_sha1_update(PX_MD * h, const uint8 * data, uint dlen) +int_sha1_update(PX_MD * h, const uint8 *data, uint dlen) { SHA1_CTX *ctx = (SHA1_CTX *) h->p.ptr; @@ -146,7 +153,7 @@ int_sha1_reset(PX_MD * h) } static void -int_sha1_finish(PX_MD * h, uint8 * dst) +int_sha1_finish(PX_MD * h, uint8 *dst) { SHA1_CTX *ctx = (SHA1_CTX *) h->p.ptr; @@ -209,22 +216,27 @@ init_sha1(PX_MD * md) #define INT_MAX_KEY (512/8) #define INT_MAX_IV (128/8) -struct int_ctx { - uint8 keybuf[INT_MAX_KEY]; - uint8 iv[INT_MAX_IV]; - union { - blf_ctx bf; +struct int_ctx +{ + uint8 keybuf[INT_MAX_KEY]; + uint8 iv[INT_MAX_IV]; + union + { + blf_ctx bf; rijndael_ctx rj; - } ctx; - uint keylen; - int is_init; - int mode; + } ctx; + uint keylen; + int is_init; + int mode; }; -static void intctx_free(PX_Cipher *c) +static void +intctx_free(PX_Cipher * c) { - struct int_ctx *cx = (struct int_ctx *)c->ptr; - if (cx) { + struct int_ctx *cx = (struct int_ctx *) c->ptr; + + if (cx) + { memset(cx, 0, sizeof *cx); px_free(cx); } @@ -238,78 +250,88 @@ static void intctx_free(PX_Cipher *c) #define MODE_ECB 0 #define MODE_CBC 1 -static uint rj_block_size(PX_Cipher *c) +static uint +rj_block_size(PX_Cipher * c) { - return 128/8; + return 128 / 8; } -static uint rj_key_size(PX_Cipher *c) +static uint +rj_key_size(PX_Cipher * c) { - return 256/8; + return 256 / 8; } -static uint rj_iv_size(PX_Cipher *c) +static uint +rj_iv_size(PX_Cipher * c) { - return 128/8; + return 128 / 8; } -static int rj_init(PX_Cipher *c, const uint8 *key, uint klen, const uint8 *iv) +static int +rj_init(PX_Cipher * c, const uint8 *key, uint klen, const uint8 *iv) { - struct int_ctx *cx = (struct int_ctx *)c->ptr; + struct int_ctx *cx = (struct int_ctx *) c->ptr; - if (klen <= 128/8) - cx->keylen = 128/8; - else if (klen <= 192/8) - cx->keylen = 192/8; - else if (klen <= 256/8) - cx->keylen = 256/8; + if (klen <= 128 / 8) + cx->keylen = 128 / 8; + else if (klen <= 192 / 8) + cx->keylen = 192 / 8; + else if (klen <= 256 / 8) + cx->keylen = 256 / 8; else return -1; memcpy(&cx->keybuf, key, klen); if (iv) - memcpy(cx->iv, iv, 128/8); + memcpy(cx->iv, iv, 128 / 8); return 0; } -static int rj_real_init(struct int_ctx *cx, int dir) +static int +rj_real_init(struct int_ctx * cx, int dir) { - aes_set_key(&cx->ctx.rj, cx->keybuf, cx->keylen*8, dir); + aes_set_key(&cx->ctx.rj, cx->keybuf, cx->keylen * 8, dir); return 0; } -static int rj_encrypt(PX_Cipher *c, const uint8 *data, uint dlen, uint8 *res) +static int +rj_encrypt(PX_Cipher * c, const uint8 *data, uint dlen, uint8 *res) { - struct int_ctx *cx = (struct int_ctx *)c->ptr; - - if (!cx->is_init) { + struct int_ctx *cx = (struct int_ctx *) c->ptr; + + if (!cx->is_init) + { if (rj_real_init(cx, 1)) return -1; } - + if (dlen == 0) return 0; - if ((dlen & 15) || (((unsigned)res) & 3)) + if ((dlen & 15) || (((unsigned) res) & 3)) return -1; memcpy(res, data, dlen); - if (cx->mode == MODE_CBC) { + if (cx->mode == MODE_CBC) + { aes_cbc_encrypt(&cx->ctx.rj, cx->iv, res, dlen); memcpy(cx->iv, res + dlen - 16, 16); - } else + } + else aes_ecb_encrypt(&cx->ctx.rj, res, dlen); - + return 0; } -static int rj_decrypt(PX_Cipher *c, const uint8 *data, uint dlen, uint8 *res) +static int +rj_decrypt(PX_Cipher * c, const uint8 *data, uint dlen, uint8 *res) { - struct int_ctx *cx = (struct int_ctx *)c->ptr; - + struct int_ctx *cx = (struct int_ctx *) c->ptr; + if (!cx->is_init) if (rj_real_init(cx, 0)) return -1; @@ -317,17 +339,19 @@ static int rj_decrypt(PX_Cipher *c, const uint8 *data, uint dlen, uint8 *res) if (dlen == 0) return 0; - if ((dlen & 15) || (((unsigned)res) & 3)) + if ((dlen & 15) || (((unsigned) res) & 3)) return -1; memcpy(res, data, dlen); - if (cx->mode == MODE_CBC) { + if (cx->mode == MODE_CBC) + { aes_cbc_decrypt(&cx->ctx.rj, cx->iv, res, dlen); memcpy(cx->iv, data + dlen - 16, 16); - } else + } + else aes_ecb_decrypt(&cx->ctx.rj, res, dlen); - + return 0; } @@ -335,11 +359,12 @@ static int rj_decrypt(PX_Cipher *c, const uint8 *data, uint dlen, uint8 *res) * initializers */ -static PX_Cipher * rj_load(int mode) +static PX_Cipher * +rj_load(int mode) { - PX_Cipher *c; + PX_Cipher *c; struct int_ctx *cx; - + c = px_alloc(sizeof *c); memset(c, 0, sizeof *c); @@ -363,24 +388,28 @@ static PX_Cipher * rj_load(int mode) * blowfish */ -static uint bf_block_size(PX_Cipher *c) +static uint +bf_block_size(PX_Cipher * c) { return 8; } -static uint bf_key_size(PX_Cipher *c) +static uint +bf_key_size(PX_Cipher * c) { return BLF_MAXKEYLEN; } -static uint bf_iv_size(PX_Cipher *c) +static uint +bf_iv_size(PX_Cipher * c) { return 8; } -static int bf_init(PX_Cipher *c, const uint8 *key, uint klen, const uint8 *iv) +static int +bf_init(PX_Cipher * c, const uint8 *key, uint klen, const uint8 *iv) { - struct int_ctx *cx = (struct int_ctx *)c->ptr; + struct int_ctx *cx = (struct int_ctx *) c->ptr; blf_key(&cx->ctx.bf, key, klen); if (iv) @@ -389,55 +418,60 @@ static int bf_init(PX_Cipher *c, const uint8 *key, uint klen, const uint8 *iv) return 0; } -static int bf_encrypt(PX_Cipher *c, const uint8 *data, uint dlen, uint8 *res) +static int +bf_encrypt(PX_Cipher * c, const uint8 *data, uint dlen, uint8 *res) { - struct int_ctx *cx = (struct int_ctx *)c->ptr; + struct int_ctx *cx = (struct int_ctx *) c->ptr; if (dlen == 0) return 0; - - if ((dlen & 7) || (((unsigned)res) & 3)) + + if ((dlen & 7) || (((unsigned) res) & 3)) return -1; memcpy(res, data, dlen); - switch (cx->mode) { - case MODE_ECB: - blf_ecb_encrypt(&cx->ctx.bf, res, dlen); - break; - case MODE_CBC: - blf_cbc_encrypt(&cx->ctx.bf, cx->iv, res, dlen); - memcpy(cx->iv, res + dlen - 8, 8); + switch (cx->mode) + { + case MODE_ECB: + blf_ecb_encrypt(&cx->ctx.bf, res, dlen); + break; + case MODE_CBC: + blf_cbc_encrypt(&cx->ctx.bf, cx->iv, res, dlen); + memcpy(cx->iv, res + dlen - 8, 8); } return 0; } -static int bf_decrypt(PX_Cipher *c, const uint8 *data, uint dlen, uint8 *res) +static int +bf_decrypt(PX_Cipher * c, const uint8 *data, uint dlen, uint8 *res) { - struct int_ctx *cx = (struct int_ctx *)c->ptr; + struct int_ctx *cx = (struct int_ctx *) c->ptr; if (dlen == 0) return 0; - if ((dlen & 7) || (((unsigned)res) & 3)) + if ((dlen & 7) || (((unsigned) res) & 3)) return -1; memcpy(res, data, dlen); - switch (cx->mode) { - case MODE_ECB: - blf_ecb_decrypt(&cx->ctx.bf, res, dlen); - break; - case MODE_CBC: - blf_cbc_decrypt(&cx->ctx.bf, cx->iv, res, dlen); - memcpy(cx->iv, data + dlen - 8, 8); + switch (cx->mode) + { + case MODE_ECB: + blf_ecb_decrypt(&cx->ctx.bf, res, dlen); + break; + case MODE_CBC: + blf_cbc_decrypt(&cx->ctx.bf, cx->iv, res, dlen); + memcpy(cx->iv, data + dlen - 8, 8); } return 0; } -static PX_Cipher * bf_load(int mode) +static PX_Cipher * +bf_load(int mode) { - PX_Cipher *c; + PX_Cipher *c; struct int_ctx *cx; - + c = px_alloc(sizeof *c); memset(c, 0, sizeof *c); @@ -458,47 +492,64 @@ static PX_Cipher * bf_load(int mode) /* ciphers */ -static PX_Cipher * rj_128_ecb() +static PX_Cipher * +rj_128_ecb() { return rj_load(MODE_ECB); } -static PX_Cipher * rj_128_cbc() +static PX_Cipher * +rj_128_cbc() { return rj_load(MODE_CBC); } -static PX_Cipher * bf_ecb_load() +static PX_Cipher * +bf_ecb_load() { return bf_load(MODE_ECB); } -static PX_Cipher * bf_cbc_load() +static PX_Cipher * +bf_cbc_load() { return bf_load(MODE_CBC); } -static struct { - char *name; - PX_Cipher *(*load)(void); -} int_ciphers [] = { - { "bf-cbc", bf_cbc_load }, - { "bf-ecb", bf_ecb_load }, - { "aes-128-cbc", rj_128_cbc }, - { "aes-128-ecb", rj_128_ecb }, - { NULL, NULL } +static struct +{ + char *name; + PX_Cipher *(*load) (void); +} int_ciphers[] = + +{ + { + "bf-cbc", bf_cbc_load + }, + { + "bf-ecb", bf_ecb_load + }, + { + "aes-128-cbc", rj_128_cbc + }, + { + "aes-128-ecb", rj_128_ecb + }, + { + NULL, NULL + } }; -static PX_Alias int_aliases [] = { - { "bf", "bf-cbc" }, - { "blowfish", "bf-cbc" }, - { "aes", "aes-128-cbc" }, - { "aes-ecb", "aes-128-ecb" }, - { "aes-cbc", "aes-128-cbc" }, - { "aes-128", "aes-128-cbc" }, - { "rijndael", "aes-128-cbc" }, - { "rijndael-128", "aes-128-cbc" }, - { NULL, NULL } +static PX_Alias int_aliases[] = { + {"bf", "bf-cbc"}, + {"blowfish", "bf-cbc"}, + {"aes", "aes-128-cbc"}, + {"aes-ecb", "aes-128-ecb"}, + {"aes-cbc", "aes-128-cbc"}, + {"aes-128", "aes-128-cbc"}, + {"rijndael", "aes-128-cbc"}, + {"rijndael-128", "aes-128-cbc"}, + {NULL, NULL} }; /* PUBLIC FUNCTIONS */ @@ -523,15 +574,16 @@ 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) { - int i; - PX_Cipher *c = NULL; + int i; + PX_Cipher *c = NULL; name = px_resolve_alias(int_aliases, name); for (i = 0; int_ciphers[i].name; i++) - if (!strcmp(int_ciphers[i].name, name)) { + if (!strcmp(int_ciphers[i].name, name)) + { c = int_ciphers[i].load(); break; } @@ -542,5 +594,3 @@ px_find_cipher(const char *name, PX_Cipher **res) *res = c; return 0; } - - |