3
* Wrapper for builtin functions
5
* Copyright (c) 2001 Marko Kreen
8
* Redistribution and use in source and binary forms, with or without
9
* modification, are permitted provided that the following conditions
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.
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
29
* $PostgreSQL: pgsql/contrib/pgcrypto/internal.c,v 1.14 2004-10-25 02:15:02 tgl Exp $
42
#ifndef MD5_DIGEST_LENGTH
43
#define MD5_DIGEST_LENGTH 16
46
#ifndef SHA1_DIGEST_LENGTH
48
#define SHA1_DIGEST_LENGTH SHA1_RESULTLEN
50
#define SHA1_DIGEST_LENGTH 20
54
#define SHA1_BLOCK_SIZE 64
55
#define MD5_BLOCK_SIZE 64
57
static void init_md5(PX_MD * h);
58
static void init_sha1(PX_MD * h);
60
static struct int_digest
63
void (*init) (PX_MD * h);
81
int_md5_len(PX_MD * h)
83
return MD5_DIGEST_LENGTH;
87
int_md5_block_len(PX_MD * h)
89
return MD5_BLOCK_SIZE;
93
int_md5_update(PX_MD * h, const uint8 *data, unsigned dlen)
95
MD5_CTX *ctx = (MD5_CTX *) h->p.ptr;
97
MD5Update(ctx, data, dlen);
101
int_md5_reset(PX_MD * h)
103
MD5_CTX *ctx = (MD5_CTX *) h->p.ptr;
109
int_md5_finish(PX_MD * h, uint8 *dst)
111
MD5_CTX *ctx = (MD5_CTX *) h->p.ptr;
117
int_md5_free(PX_MD * h)
119
MD5_CTX *ctx = (MD5_CTX *) h->p.ptr;
128
int_sha1_len(PX_MD * h)
130
return SHA1_DIGEST_LENGTH;
134
int_sha1_block_len(PX_MD * h)
136
return SHA1_BLOCK_SIZE;
140
int_sha1_update(PX_MD * h, const uint8 *data, unsigned dlen)
142
SHA1_CTX *ctx = (SHA1_CTX *) h->p.ptr;
144
SHA1Update(ctx, data, dlen);
148
int_sha1_reset(PX_MD * h)
150
SHA1_CTX *ctx = (SHA1_CTX *) h->p.ptr;
156
int_sha1_finish(PX_MD * h, uint8 *dst)
158
SHA1_CTX *ctx = (SHA1_CTX *) h->p.ptr;
164
int_sha1_free(PX_MD * h)
166
SHA1_CTX *ctx = (SHA1_CTX *) h->p.ptr;
179
ctx = px_alloc(sizeof(*ctx));
183
md->result_size = int_md5_len;
184
md->block_size = int_md5_block_len;
185
md->reset = int_md5_reset;
186
md->update = int_md5_update;
187
md->finish = int_md5_finish;
188
md->free = int_md5_free;
194
init_sha1(PX_MD * md)
198
ctx = px_alloc(sizeof(*ctx));
202
md->result_size = int_sha1_len;
203
md->block_size = int_sha1_block_len;
204
md->reset = int_sha1_reset;
205
md->update = int_sha1_update;
206
md->finish = int_sha1_finish;
207
md->free = int_sha1_free;
216
#define INT_MAX_KEY (512/8)
217
#define INT_MAX_IV (128/8)
221
uint8 keybuf[INT_MAX_KEY];
222
uint8 iv[INT_MAX_IV];
234
intctx_free(PX_Cipher * c)
236
struct int_ctx *cx = (struct int_ctx *) c->ptr;
240
memset(cx, 0, sizeof *cx);
254
rj_block_size(PX_Cipher * c)
260
rj_key_size(PX_Cipher * c)
266
rj_iv_size(PX_Cipher * c)
272
rj_init(PX_Cipher * c, const uint8 *key, unsigned klen, const uint8 *iv)
274
struct int_ctx *cx = (struct int_ctx *) c->ptr;
277
cx->keylen = 128 / 8;
278
else if (klen <= 192 / 8)
279
cx->keylen = 192 / 8;
280
else if (klen <= 256 / 8)
281
cx->keylen = 256 / 8;
285
memcpy(&cx->keybuf, key, klen);
288
memcpy(cx->iv, iv, 128 / 8);
294
rj_real_init(struct int_ctx * cx, int dir)
296
aes_set_key(&cx->ctx.rj, cx->keybuf, cx->keylen * 8, dir);
301
rj_encrypt(PX_Cipher * c, const uint8 *data, unsigned dlen, uint8 *res)
303
struct int_ctx *cx = (struct int_ctx *) c->ptr;
307
if (rj_real_init(cx, 1))
317
memcpy(res, data, dlen);
319
if (cx->mode == MODE_CBC)
321
aes_cbc_encrypt(&cx->ctx.rj, cx->iv, res, dlen);
322
memcpy(cx->iv, res + dlen - 16, 16);
325
aes_ecb_encrypt(&cx->ctx.rj, res, dlen);
331
rj_decrypt(PX_Cipher * c, const uint8 *data, unsigned dlen, uint8 *res)
333
struct int_ctx *cx = (struct int_ctx *) c->ptr;
336
if (rj_real_init(cx, 0))
345
memcpy(res, data, dlen);
347
if (cx->mode == MODE_CBC)
349
aes_cbc_decrypt(&cx->ctx.rj, cx->iv, res, dlen);
350
memcpy(cx->iv, data + dlen - 16, 16);
353
aes_ecb_decrypt(&cx->ctx.rj, res, dlen);
368
c = px_alloc(sizeof *c);
369
memset(c, 0, sizeof *c);
371
c->block_size = rj_block_size;
372
c->key_size = rj_key_size;
373
c->iv_size = rj_iv_size;
375
c->encrypt = rj_encrypt;
376
c->decrypt = rj_decrypt;
377
c->free = intctx_free;
379
cx = px_alloc(sizeof *cx);
380
memset(cx, 0, sizeof *cx);
392
bf_block_size(PX_Cipher * c)
398
bf_key_size(PX_Cipher * c)
400
return BLF_MAXKEYLEN;
404
bf_iv_size(PX_Cipher * c)
410
bf_init(PX_Cipher * c, const uint8 *key, unsigned klen, const uint8 *iv)
412
struct int_ctx *cx = (struct int_ctx *) c->ptr;
414
blf_key(&cx->ctx.bf, key, klen);
416
memcpy(cx->iv, iv, 8);
422
bf_encrypt(PX_Cipher * c, const uint8 *data, unsigned dlen, uint8 *res)
424
struct int_ctx *cx = (struct int_ctx *) c->ptr;
432
memcpy(res, data, dlen);
436
blf_ecb_encrypt(&cx->ctx.bf, res, dlen);
439
blf_cbc_encrypt(&cx->ctx.bf, cx->iv, res, dlen);
440
memcpy(cx->iv, res + dlen - 8, 8);
446
bf_decrypt(PX_Cipher * c, const uint8 *data, unsigned dlen, uint8 *res)
448
struct int_ctx *cx = (struct int_ctx *) c->ptr;
456
memcpy(res, data, dlen);
460
blf_ecb_decrypt(&cx->ctx.bf, res, dlen);
463
blf_cbc_decrypt(&cx->ctx.bf, cx->iv, res, dlen);
464
memcpy(cx->iv, data + dlen - 8, 8);
475
c = px_alloc(sizeof *c);
476
memset(c, 0, sizeof *c);
478
c->block_size = bf_block_size;
479
c->key_size = bf_key_size;
480
c->iv_size = bf_iv_size;
482
c->encrypt = bf_encrypt;
483
c->decrypt = bf_decrypt;
484
c->free = intctx_free;
486
cx = px_alloc(sizeof *cx);
487
memset(cx, 0, sizeof *cx);
498
return rj_load(MODE_ECB);
504
return rj_load(MODE_CBC);
510
return bf_load(MODE_ECB);
516
return bf_load(MODE_CBC);
522
PX_Cipher *(*load) (void);
527
"bf-cbc", bf_cbc_load
530
"bf-ecb", bf_ecb_load
533
"aes-128-cbc", rj_128_cbc
536
"aes-128-ecb", rj_128_ecb
543
static PX_Alias int_aliases[] = {
545
{"blowfish", "bf-cbc"},
546
{"aes", "aes-128-cbc"},
547
{"aes-ecb", "aes-128-ecb"},
548
{"aes-cbc", "aes-128-cbc"},
549
{"aes-128", "aes-128-cbc"},
550
{"rijndael", "aes-128-cbc"},
551
{"rijndael-128", "aes-128-cbc"},
555
/* PUBLIC FUNCTIONS */
558
px_find_digest(const char *name, PX_MD ** res)
560
struct int_digest *p;
563
for (p = int_digest_list; p->name; p++)
564
if (pg_strcasecmp(p->name, name) == 0)
566
h = px_alloc(sizeof(*h));
577
px_find_cipher(const char *name, PX_Cipher ** res)
582
name = px_resolve_alias(int_aliases, name);
584
for (i = 0; int_ciphers[i].name; i++)
585
if (!strcmp(int_ciphers[i].name, name))
587
c = int_ciphers[i].load();