PostgreSQL Source Code  git master
internal.c
Go to the documentation of this file.
1 /*
2  * internal.c
3  * Wrapper for builtin functions
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  * contrib/pgcrypto/internal.c
30  */
31 
32 #include "postgres.h"
33 
34 #include <time.h>
35 
36 #include "blf.h"
37 #include "md5.h"
38 #include "px.h"
39 #include "rijndael.h"
40 #include "sha1.h"
41 
42 #ifndef MD5_DIGEST_LENGTH
43 #define MD5_DIGEST_LENGTH 16
44 #endif
45 
46 #ifndef SHA1_DIGEST_LENGTH
47 #ifdef SHA1_RESULTLEN
48 #define SHA1_DIGEST_LENGTH SHA1_RESULTLEN
49 #else
50 #define SHA1_DIGEST_LENGTH 20
51 #endif
52 #endif
53 
54 #define SHA1_BLOCK_SIZE 64
55 #define MD5_BLOCK_SIZE 64
56 
57 static void init_md5(PX_MD *h);
58 static void init_sha1(PX_MD *h);
59 
60 void init_sha224(PX_MD *h);
61 void init_sha256(PX_MD *h);
62 void init_sha384(PX_MD *h);
63 void init_sha512(PX_MD *h);
64 
65 struct int_digest
66 {
67  char *name;
68  void (*init) (PX_MD *h);
69 };
70 
71 static const struct int_digest
73  {"md5", init_md5},
74  {"sha1", init_sha1},
75  {"sha224", init_sha224},
76  {"sha256", init_sha256},
77  {"sha384", init_sha384},
78  {"sha512", init_sha512},
79  {NULL, NULL}
80 };
81 
82 /* MD5 */
83 
84 static unsigned
86 {
87  return MD5_DIGEST_LENGTH;
88 }
89 
90 static unsigned
92 {
93  return MD5_BLOCK_SIZE;
94 }
95 
96 static void
97 int_md5_update(PX_MD *h, const uint8 *data, unsigned dlen)
98 {
99  MD5_CTX *ctx = (MD5_CTX *) h->p.ptr;
100 
101  MD5Update(ctx, data, dlen);
102 }
103 
104 static void
106 {
107  MD5_CTX *ctx = (MD5_CTX *) h->p.ptr;
108 
109  MD5Init(ctx);
110 }
111 
112 static void
114 {
115  MD5_CTX *ctx = (MD5_CTX *) h->p.ptr;
116 
117  MD5Final(dst, ctx);
118 }
119 
120 static void
122 {
123  MD5_CTX *ctx = (MD5_CTX *) h->p.ptr;
124 
125  px_memset(ctx, 0, sizeof(*ctx));
126  px_free(ctx);
127  px_free(h);
128 }
129 
130 /* SHA1 */
131 
132 static unsigned
134 {
135  return SHA1_DIGEST_LENGTH;
136 }
137 
138 static unsigned
140 {
141  return SHA1_BLOCK_SIZE;
142 }
143 
144 static void
145 int_sha1_update(PX_MD *h, const uint8 *data, unsigned dlen)
146 {
147  SHA1_CTX *ctx = (SHA1_CTX *) h->p.ptr;
148 
149  SHA1Update(ctx, data, dlen);
150 }
151 
152 static void
154 {
155  SHA1_CTX *ctx = (SHA1_CTX *) h->p.ptr;
156 
157  SHA1Init(ctx);
158 }
159 
160 static void
162 {
163  SHA1_CTX *ctx = (SHA1_CTX *) h->p.ptr;
164 
165  SHA1Final(dst, ctx);
166 }
167 
168 static void
170 {
171  SHA1_CTX *ctx = (SHA1_CTX *) h->p.ptr;
172 
173  px_memset(ctx, 0, sizeof(*ctx));
174  px_free(ctx);
175  px_free(h);
176 }
177 
178 /* init functions */
179 
180 static void
182 {
183  MD5_CTX *ctx;
184 
185  ctx = px_alloc(sizeof(*ctx));
186  memset(ctx, 0, sizeof(*ctx));
187 
188  md->p.ptr = ctx;
189 
190  md->result_size = int_md5_len;
192  md->reset = int_md5_reset;
193  md->update = int_md5_update;
194  md->finish = int_md5_finish;
195  md->free = int_md5_free;
196 
197  md->reset(md);
198 }
199 
200 static void
202 {
203  SHA1_CTX *ctx;
204 
205  ctx = px_alloc(sizeof(*ctx));
206  memset(ctx, 0, sizeof(*ctx));
207 
208  md->p.ptr = ctx;
209 
212  md->reset = int_sha1_reset;
213  md->update = int_sha1_update;
214  md->finish = int_sha1_finish;
215  md->free = int_sha1_free;
216 
217  md->reset(md);
218 }
219 
220 /*
221  * ciphers generally
222  */
223 
224 #define INT_MAX_KEY (512/8)
225 #define INT_MAX_IV (128/8)
226 
227 struct int_ctx
228 {
231  union
232  {
235  } ctx;
236  unsigned keylen;
237  int is_init;
238  int mode;
239 };
240 
241 static void
243 {
244  struct int_ctx *cx = (struct int_ctx *) c->ptr;
245 
246  if (cx)
247  {
248  px_memset(cx, 0, sizeof *cx);
249  px_free(cx);
250  }
251  px_free(c);
252 }
253 
254 /*
255  * AES/rijndael
256  */
257 
258 #define MODE_ECB 0
259 #define MODE_CBC 1
260 
261 static unsigned
263 {
264  return 128 / 8;
265 }
266 
267 static unsigned
269 {
270  return 256 / 8;
271 }
272 
273 static unsigned
275 {
276  return 128 / 8;
277 }
278 
279 static int
280 rj_init(PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
281 {
282  struct int_ctx *cx = (struct int_ctx *) c->ptr;
283 
284  if (klen <= 128 / 8)
285  cx->keylen = 128 / 8;
286  else if (klen <= 192 / 8)
287  cx->keylen = 192 / 8;
288  else if (klen <= 256 / 8)
289  cx->keylen = 256 / 8;
290  else
291  return PXE_KEY_TOO_BIG;
292 
293  memcpy(&cx->keybuf, key, klen);
294 
295  if (iv)
296  memcpy(cx->iv, iv, 128 / 8);
297 
298  return 0;
299 }
300 
301 static int
302 rj_real_init(struct int_ctx *cx, int dir)
303 {
304  aes_set_key(&cx->ctx.rj, cx->keybuf, cx->keylen * 8, dir);
305  return 0;
306 }
307 
308 static int
309 rj_encrypt(PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
310 {
311  struct int_ctx *cx = (struct int_ctx *) c->ptr;
312 
313  if (!cx->is_init)
314  {
315  if (rj_real_init(cx, 1))
316  return PXE_CIPHER_INIT;
317  }
318 
319  if (dlen == 0)
320  return 0;
321 
322  if (dlen & 15)
323  return PXE_NOTBLOCKSIZE;
324 
325  memcpy(res, data, dlen);
326 
327  if (cx->mode == MODE_CBC)
328  {
329  aes_cbc_encrypt(&cx->ctx.rj, cx->iv, res, dlen);
330  memcpy(cx->iv, res + dlen - 16, 16);
331  }
332  else
333  aes_ecb_encrypt(&cx->ctx.rj, res, dlen);
334 
335  return 0;
336 }
337 
338 static int
339 rj_decrypt(PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
340 {
341  struct int_ctx *cx = (struct int_ctx *) c->ptr;
342 
343  if (!cx->is_init)
344  if (rj_real_init(cx, 0))
345  return PXE_CIPHER_INIT;
346 
347  if (dlen == 0)
348  return 0;
349 
350  if (dlen & 15)
351  return PXE_NOTBLOCKSIZE;
352 
353  memcpy(res, data, dlen);
354 
355  if (cx->mode == MODE_CBC)
356  {
357  aes_cbc_decrypt(&cx->ctx.rj, cx->iv, res, dlen);
358  memcpy(cx->iv, data + dlen - 16, 16);
359  }
360  else
361  aes_ecb_decrypt(&cx->ctx.rj, res, dlen);
362 
363  return 0;
364 }
365 
366 /*
367  * initializers
368  */
369 
370 static PX_Cipher *
372 {
373  PX_Cipher *c;
374  struct int_ctx *cx;
375 
376  c = px_alloc(sizeof *c);
377  memset(c, 0, sizeof *c);
378 
380  c->key_size = rj_key_size;
381  c->iv_size = rj_iv_size;
382  c->init = rj_init;
383  c->encrypt = rj_encrypt;
384  c->decrypt = rj_decrypt;
385  c->free = intctx_free;
386 
387  cx = px_alloc(sizeof *cx);
388  memset(cx, 0, sizeof *cx);
389  cx->mode = mode;
390 
391  c->ptr = cx;
392  return c;
393 }
394 
395 /*
396  * blowfish
397  */
398 
399 static unsigned
401 {
402  return 8;
403 }
404 
405 static unsigned
407 {
408  return 448 / 8;
409 }
410 
411 static unsigned
413 {
414  return 8;
415 }
416 
417 static int
418 bf_init(PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
419 {
420  struct int_ctx *cx = (struct int_ctx *) c->ptr;
421 
422  blowfish_setkey(&cx->ctx.bf, key, klen);
423  if (iv)
424  blowfish_setiv(&cx->ctx.bf, iv);
425 
426  return 0;
427 }
428 
429 static int
430 bf_encrypt(PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
431 {
432  struct int_ctx *cx = (struct int_ctx *) c->ptr;
433  BlowfishContext *bfctx = &cx->ctx.bf;
434 
435  if (dlen == 0)
436  return 0;
437 
438  if (dlen & 7)
439  return PXE_NOTBLOCKSIZE;
440 
441  memcpy(res, data, dlen);
442  switch (cx->mode)
443  {
444  case MODE_ECB:
445  blowfish_encrypt_ecb(res, dlen, bfctx);
446  break;
447  case MODE_CBC:
448  blowfish_encrypt_cbc(res, dlen, bfctx);
449  break;
450  }
451  return 0;
452 }
453 
454 static int
455 bf_decrypt(PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
456 {
457  struct int_ctx *cx = (struct int_ctx *) c->ptr;
458  BlowfishContext *bfctx = &cx->ctx.bf;
459 
460  if (dlen == 0)
461  return 0;
462 
463  if (dlen & 7)
464  return PXE_NOTBLOCKSIZE;
465 
466  memcpy(res, data, dlen);
467  switch (cx->mode)
468  {
469  case MODE_ECB:
470  blowfish_decrypt_ecb(res, dlen, bfctx);
471  break;
472  case MODE_CBC:
473  blowfish_decrypt_cbc(res, dlen, bfctx);
474  break;
475  }
476  return 0;
477 }
478 
479 static PX_Cipher *
481 {
482  PX_Cipher *c;
483  struct int_ctx *cx;
484 
485  c = px_alloc(sizeof *c);
486  memset(c, 0, sizeof *c);
487 
489  c->key_size = bf_key_size;
490  c->iv_size = bf_iv_size;
491  c->init = bf_init;
492  c->encrypt = bf_encrypt;
493  c->decrypt = bf_decrypt;
494  c->free = intctx_free;
495 
496  cx = px_alloc(sizeof *cx);
497  memset(cx, 0, sizeof *cx);
498  cx->mode = mode;
499  c->ptr = cx;
500  return c;
501 }
502 
503 /* ciphers */
504 
505 static PX_Cipher *
507 {
508  return rj_load(MODE_ECB);
509 }
510 
511 static PX_Cipher *
513 {
514  return rj_load(MODE_CBC);
515 }
516 
517 static PX_Cipher *
519 {
520  return bf_load(MODE_ECB);
521 }
522 
523 static PX_Cipher *
525 {
526  return bf_load(MODE_CBC);
527 }
528 
530 {
531  char *name;
532  PX_Cipher *(*load) (void);
533 };
534 
535 static const struct int_cipher
537  {"bf-cbc", bf_cbc_load},
538  {"bf-ecb", bf_ecb_load},
539  {"aes-128-cbc", rj_128_cbc},
540  {"aes-128-ecb", rj_128_ecb},
541  {NULL, NULL}
542 };
543 
544 static const PX_Alias int_aliases[] = {
545  {"bf", "bf-cbc"},
546  {"blowfish", "bf-cbc"},
547  {"aes", "aes-128-cbc"},
548  {"aes-ecb", "aes-128-ecb"},
549  {"aes-cbc", "aes-128-cbc"},
550  {"aes-128", "aes-128-cbc"},
551  {"rijndael", "aes-128-cbc"},
552  {"rijndael-128", "aes-128-cbc"},
553  {NULL, NULL}
554 };
555 
556 /* PUBLIC FUNCTIONS */
557 
558 int
559 px_find_digest(const char *name, PX_MD **res)
560 {
561  const struct int_digest *p;
562  PX_MD *h;
563 
564  for (p = int_digest_list; p->name; p++)
565  if (pg_strcasecmp(p->name, name) == 0)
566  {
567  h = px_alloc(sizeof(*h));
568  p->init(h);
569 
570  *res = h;
571 
572  return 0;
573  }
574  return PXE_NO_HASH;
575 }
576 
577 int
578 px_find_cipher(const char *name, PX_Cipher **res)
579 {
580  int i;
581  PX_Cipher *c = NULL;
582 
583  name = px_resolve_alias(int_aliases, name);
584 
585  for (i = 0; int_ciphers[i].name; i++)
586  if (strcmp(int_ciphers[i].name, name) == 0)
587  {
588  c = int_ciphers[i].load();
589  break;
590  }
591 
592  if (c == NULL)
593  return PXE_NO_CIPHER;
594 
595  *res = c;
596  return 0;
597 }
static const struct int_cipher int_ciphers[]
Definition: internal.c:536
static PgChecksumMode mode
Definition: pg_checksums.c:61
void aes_cbc_encrypt(rijndael_ctx *ctx, uint8 *iva, uint8 *data, unsigned len)
Definition: rijndael.c:545
#define MD5Final(x, y)
Definition: md5.h:73
void * ptr
Definition: px.h:122
#define MD5_CTX
Definition: md5.h:70
void init_sha384(PX_MD *h)
#define MD5_BLOCK_SIZE
Definition: internal.c:55
PX_Cipher *(* load)(void)
Definition: internal.c:532
static PX_Cipher * bf_cbc_load(void)
Definition: internal.c:524
#define INT_MAX_KEY
Definition: internal.c:224
static int bf_decrypt(PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
Definition: internal.c:455
static PX_Cipher * bf_ecb_load(void)
Definition: internal.c:518
void(* free)(PX_MD *h)
Definition: px.h:117
static const struct int_digest int_digest_list[]
Definition: internal.c:72
static void intctx_free(PX_Cipher *c)
Definition: internal.c:242
BlowfishContext bf
Definition: internal.c:233
static unsigned int_sha1_block_len(PX_MD *h)
Definition: internal.c:139
char * name
Definition: internal.c:531
static PX_Cipher * bf_load(int mode)
Definition: internal.c:480
#define px_free(p)
Definition: px.h:46
unsigned(* block_size)(PX_Cipher *c)
Definition: px.h:153
unsigned char uint8
Definition: c.h:357
void aes_cbc_decrypt(rijndael_ctx *ctx, uint8 *iva, uint8 *data, unsigned len)
Definition: rijndael.c:567
#define MD5Init(x)
Definition: md5.h:71
void blowfish_encrypt_cbc(uint8 *blk, int len, BlowfishContext *ctx)
Definition: blf.c:321
int(* decrypt)(PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
Definition: px.h:159
const char * px_resolve_alias(const PX_Alias *list, const char *name)
Definition: px.c:132
#define SHA1Init(x)
Definition: sha1.h:69
void * ptr
Definition: px.h:162
uint8 keybuf[INT_MAX_KEY]
Definition: internal.c:229
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
#define MODE_CBC
Definition: internal.c:259
void(* free)(PX_Cipher *c)
Definition: px.h:160
Definition: px.h:126
uint8 iv[INT_MAX_IV]
Definition: internal.c:230
static void int_sha1_free(PX_MD *h)
Definition: internal.c:169
static void int_md5_reset(PX_MD *h)
Definition: internal.c:105
#define SHA1_BLOCK_SIZE
Definition: internal.c:54
void aes_set_key(rijndael_ctx *ctx, const uint8 *key, unsigned keybits, int enc)
Definition: rijndael.c:504
static unsigned rj_block_size(PX_Cipher *c)
Definition: internal.c:262
int cx(PlannerInfo *root, Gene *tour1, Gene *tour2, Gene *offspring, int num_gene, City *city_table)
void blowfish_decrypt_ecb(uint8 *blk, int len, BlowfishContext *ctx)
Definition: blf.c:408
unsigned(* block_size)(PX_MD *h)
Definition: px.h:113
static int rj_encrypt(PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
Definition: internal.c:309
static void int_sha1_update(PX_MD *h, const uint8 *data, unsigned dlen)
Definition: internal.c:145
#define MD5Update(x, y, z)
Definition: md5.h:72
void aes_ecb_decrypt(rijndael_ctx *ctx, uint8 *data, unsigned len)
Definition: rijndael.c:529
Definition: px.h:110
static int rj_init(PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
Definition: internal.c:280
#define PXE_NO_HASH
Definition: px.h:61
Definition: sha1.h:41
void(* update)(PX_MD *h, const uint8 *data, unsigned dlen)
Definition: px.h:115
static unsigned rj_key_size(PX_Cipher *c)
Definition: internal.c:268
void(* finish)(PX_MD *h, uint8 *dst)
Definition: px.h:116
unsigned(* iv_size)(PX_Cipher *c)
Definition: px.h:155
void blowfish_setiv(BlowfishContext *ctx, const uint8 *iv)
Definition: blf.c:495
char * c
union int_ctx::@2 ctx
void init_sha224(PX_MD *h)
static void init_md5(PX_MD *h)
Definition: internal.c:181
static unsigned int_md5_len(PX_MD *h)
Definition: internal.c:85
static unsigned int_sha1_len(PX_MD *h)
Definition: internal.c:133
#define SHA1Final(x, y)
Definition: sha1.h:71
unsigned(* key_size)(PX_Cipher *c)
Definition: px.h:154
void init_sha256(PX_MD *h)
static unsigned rj_iv_size(PX_Cipher *c)
Definition: internal.c:274
static int bf_init(PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
Definition: internal.c:418
#define INT_MAX_IV
Definition: internal.c:225
int(* encrypt)(PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
Definition: px.h:158
unsigned(* result_size)(PX_MD *h)
Definition: px.h:112
static void int_md5_finish(PX_MD *h, uint8 *dst)
Definition: internal.c:113
static int rj_real_init(struct int_ctx *cx, int dir)
Definition: internal.c:302
static unsigned int_md5_block_len(PX_MD *h)
Definition: internal.c:91
#define SHA1_DIGEST_LENGTH
Definition: internal.c:48
union px_digest::@13 p
void blowfish_setkey(BlowfishContext *ctx, const uint8 *key, short keybytes)
Definition: blf.c:429
char * name
Definition: internal.c:67
static unsigned bf_key_size(PX_Cipher *c)
Definition: internal.c:406
static int bf_encrypt(PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
Definition: internal.c:430
#define PXE_KEY_TOO_BIG
Definition: px.h:66
void init_sha512(PX_MD *h)
#define PXE_CIPHER_INIT
Definition: px.h:67
void(* reset)(PX_MD *h)
Definition: px.h:114
static int rj_decrypt(PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
Definition: internal.c:339
int(* init)(PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
Definition: px.h:157
unsigned keylen
Definition: internal.c:236
void aes_ecb_encrypt(rijndael_ctx *ctx, uint8 *data, unsigned len)
Definition: rijndael.c:513
#define MODE_ECB
Definition: internal.c:258
#define PXE_NOTBLOCKSIZE
Definition: px.h:63
Definition: px.h:151
void blowfish_encrypt_ecb(uint8 *blk, int len, BlowfishContext *ctx)
Definition: blf.c:387
rijndael_ctx rj
Definition: internal.c:234
static unsigned bf_block_size(PX_Cipher *c)
Definition: internal.c:400
#define px_alloc(s)
Definition: px.h:44
#define SHA1Update(x, y, z)
Definition: sha1.h:70
static void int_sha1_reset(PX_MD *h)
Definition: internal.c:153
static PX_Cipher * rj_128_ecb(void)
Definition: internal.c:506
int i
static void int_sha1_finish(PX_MD *h, uint8 *dst)
Definition: internal.c:161
static PX_Cipher * rj_load(int mode)
Definition: internal.c:371
#define PXE_NO_CIPHER
Definition: px.h:62
void blowfish_decrypt_cbc(uint8 *blk, int len, BlowfishContext *ctx)
Definition: blf.c:354
#define MD5_DIGEST_LENGTH
Definition: internal.c:43
static void init_sha1(PX_MD *h)
Definition: internal.c:201
static unsigned bf_iv_size(PX_Cipher *c)
Definition: internal.c:412
static PX_Cipher * rj_128_cbc(void)
Definition: internal.c:512
static const PX_Alias int_aliases[]
Definition: internal.c:544
int mode
Definition: internal.c:238
void(* init)(PX_MD *h)
Definition: internal.c:68
static void int_md5_free(PX_MD *h)
Definition: internal.c:121
int px_find_digest(const char *name, PX_MD **res)
Definition: internal.c:559
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:126
static void int_md5_update(PX_MD *h, const uint8 *data, unsigned dlen)
Definition: internal.c:97
int px_find_cipher(const char *name, PX_Cipher **res)
Definition: internal.c:578
int is_init
Definition: internal.c:237