PostgreSQL Source Code  git master
internal.c File Reference
#include "postgres.h"
#include <time.h>
#include "blf.h"
#include "px.h"
#include "rijndael.h"
#include "sha1.h"
#include "common/cryptohash.h"
#include "common/md5.h"
Include dependency graph for internal.c:

Go to the source code of this file.

Data Structures

struct  int_digest
 
struct  int_ctx
 
struct  int_cipher
 

Macros

#define SHA1_DIGEST_LENGTH   SHA1_RESULTLEN
 
#define SHA1_BLOCK_SIZE   64
 
#define MD5_BLOCK_SIZE   64
 
#define INT_MAX_KEY   (512/8)
 
#define INT_MAX_IV   (128/8)
 
#define MODE_ECB   0
 
#define MODE_CBC   1
 

Functions

static void init_md5 (PX_MD *h)
 
static void init_sha1 (PX_MD *h)
 
void init_sha224 (PX_MD *h)
 
void init_sha256 (PX_MD *h)
 
void init_sha384 (PX_MD *h)
 
void init_sha512 (PX_MD *h)
 
static unsigned int_md5_len (PX_MD *h)
 
static unsigned int_md5_block_len (PX_MD *h)
 
static void int_md5_update (PX_MD *h, const uint8 *data, unsigned dlen)
 
static void int_md5_reset (PX_MD *h)
 
static void int_md5_finish (PX_MD *h, uint8 *dst)
 
static void int_md5_free (PX_MD *h)
 
static unsigned int_sha1_len (PX_MD *h)
 
static unsigned int_sha1_block_len (PX_MD *h)
 
static void int_sha1_update (PX_MD *h, const uint8 *data, unsigned dlen)
 
static void int_sha1_reset (PX_MD *h)
 
static void int_sha1_finish (PX_MD *h, uint8 *dst)
 
static void int_sha1_free (PX_MD *h)
 
static void intctx_free (PX_Cipher *c)
 
static unsigned rj_block_size (PX_Cipher *c)
 
static unsigned rj_key_size (PX_Cipher *c)
 
static unsigned rj_iv_size (PX_Cipher *c)
 
static int rj_init (PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
 
static int rj_real_init (struct int_ctx *cx, int dir)
 
static int rj_encrypt (PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
 
static int rj_decrypt (PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
 
static PX_Cipherrj_load (int mode)
 
static unsigned bf_block_size (PX_Cipher *c)
 
static unsigned bf_key_size (PX_Cipher *c)
 
static unsigned bf_iv_size (PX_Cipher *c)
 
static int bf_init (PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
 
static int bf_encrypt (PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
 
static int bf_decrypt (PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
 
static PX_Cipherbf_load (int mode)
 
static PX_Cipherrj_128_ecb (void)
 
static PX_Cipherrj_128_cbc (void)
 
static PX_Cipherbf_ecb_load (void)
 
static PX_Cipherbf_cbc_load (void)
 
int px_find_digest (const char *name, PX_MD **res)
 
int px_find_cipher (const char *name, PX_Cipher **res)
 

Variables

static const struct int_digest int_digest_list []
 
static const struct int_cipher int_ciphers []
 
static const PX_Alias int_aliases []
 

Macro Definition Documentation

◆ INT_MAX_IV

#define INT_MAX_IV   (128/8)

Definition at line 223 of file internal.c.

◆ INT_MAX_KEY

#define INT_MAX_KEY   (512/8)

Definition at line 222 of file internal.c.

◆ MD5_BLOCK_SIZE

#define MD5_BLOCK_SIZE   64

Definition at line 53 of file internal.c.

Referenced by int_md5_block_len().

◆ MODE_CBC

#define MODE_CBC   1

Definition at line 257 of file internal.c.

Referenced by bf_cbc_load(), bf_decrypt(), bf_encrypt(), rj_128_cbc(), rj_decrypt(), and rj_encrypt().

◆ MODE_ECB

#define MODE_ECB   0

Definition at line 256 of file internal.c.

Referenced by bf_decrypt(), bf_ecb_load(), bf_encrypt(), and rj_128_ecb().

◆ SHA1_BLOCK_SIZE

#define SHA1_BLOCK_SIZE   64

Definition at line 52 of file internal.c.

Referenced by int_sha1_block_len().

◆ SHA1_DIGEST_LENGTH

#define SHA1_DIGEST_LENGTH   SHA1_RESULTLEN

Definition at line 46 of file internal.c.

Referenced by int_sha1_len().

Function Documentation

◆ bf_block_size()

static unsigned bf_block_size ( PX_Cipher c)
static

Definition at line 396 of file internal.c.

Referenced by bf_load().

397 {
398  return 8;
399 }

◆ bf_cbc_load()

static PX_Cipher* bf_cbc_load ( void  )
static

Definition at line 518 of file internal.c.

References bf_load(), and MODE_CBC.

519 {
520  return bf_load(MODE_CBC);
521 }
static PX_Cipher * bf_load(int mode)
Definition: internal.c:476
#define MODE_CBC
Definition: internal.c:257

◆ bf_decrypt()

static int bf_decrypt ( PX_Cipher c,
const uint8 data,
unsigned  dlen,
uint8 res 
)
static

Definition at line 451 of file internal.c.

References int_ctx::bf, blowfish_decrypt_cbc(), blowfish_decrypt_ecb(), int_ctx::ctx, cx(), int_ctx::mode, MODE_CBC, MODE_ECB, px_cipher::ptr, and PXE_NOTBLOCKSIZE.

Referenced by bf_load().

452 {
453  struct int_ctx *cx = (struct int_ctx *) c->ptr;
454  BlowfishContext *bfctx = &cx->ctx.bf;
455 
456  if (dlen == 0)
457  return 0;
458 
459  if (dlen & 7)
460  return PXE_NOTBLOCKSIZE;
461 
462  memcpy(res, data, dlen);
463  switch (cx->mode)
464  {
465  case MODE_ECB:
466  blowfish_decrypt_ecb(res, dlen, bfctx);
467  break;
468  case MODE_CBC:
469  blowfish_decrypt_cbc(res, dlen, bfctx);
470  break;
471  }
472  return 0;
473 }
BlowfishContext bf
Definition: internal.c:231
void * ptr
Definition: px.h:150
union int_ctx::@1 ctx
#define MODE_CBC
Definition: internal.c:257
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
#define MODE_ECB
Definition: internal.c:256
#define PXE_NOTBLOCKSIZE
Definition: px.h:50
void blowfish_decrypt_cbc(uint8 *blk, int len, BlowfishContext *ctx)
Definition: blf.c:354
int mode
Definition: internal.c:236

◆ bf_ecb_load()

static PX_Cipher* bf_ecb_load ( void  )
static

Definition at line 512 of file internal.c.

References bf_load(), and MODE_ECB.

513 {
514  return bf_load(MODE_ECB);
515 }
static PX_Cipher * bf_load(int mode)
Definition: internal.c:476
#define MODE_ECB
Definition: internal.c:256

◆ bf_encrypt()

static int bf_encrypt ( PX_Cipher c,
const uint8 data,
unsigned  dlen,
uint8 res 
)
static

Definition at line 426 of file internal.c.

References int_ctx::bf, blowfish_encrypt_cbc(), blowfish_encrypt_ecb(), int_ctx::ctx, cx(), int_ctx::mode, MODE_CBC, MODE_ECB, px_cipher::ptr, and PXE_NOTBLOCKSIZE.

Referenced by bf_load().

427 {
428  struct int_ctx *cx = (struct int_ctx *) c->ptr;
429  BlowfishContext *bfctx = &cx->ctx.bf;
430 
431  if (dlen == 0)
432  return 0;
433 
434  if (dlen & 7)
435  return PXE_NOTBLOCKSIZE;
436 
437  memcpy(res, data, dlen);
438  switch (cx->mode)
439  {
440  case MODE_ECB:
441  blowfish_encrypt_ecb(res, dlen, bfctx);
442  break;
443  case MODE_CBC:
444  blowfish_encrypt_cbc(res, dlen, bfctx);
445  break;
446  }
447  return 0;
448 }
BlowfishContext bf
Definition: internal.c:231
void blowfish_encrypt_cbc(uint8 *blk, int len, BlowfishContext *ctx)
Definition: blf.c:321
void * ptr
Definition: px.h:150
union int_ctx::@1 ctx
#define MODE_CBC
Definition: internal.c:257
int cx(PlannerInfo *root, Gene *tour1, Gene *tour2, Gene *offspring, int num_gene, City *city_table)
#define MODE_ECB
Definition: internal.c:256
#define PXE_NOTBLOCKSIZE
Definition: px.h:50
void blowfish_encrypt_ecb(uint8 *blk, int len, BlowfishContext *ctx)
Definition: blf.c:387
int mode
Definition: internal.c:236

◆ bf_init()

static int bf_init ( PX_Cipher c,
const uint8 key,
unsigned  klen,
const uint8 iv 
)
static

Definition at line 414 of file internal.c.

References int_ctx::bf, blowfish_setiv(), blowfish_setkey(), int_ctx::ctx, cx(), and px_cipher::ptr.

Referenced by bf_load().

415 {
416  struct int_ctx *cx = (struct int_ctx *) c->ptr;
417 
418  blowfish_setkey(&cx->ctx.bf, key, klen);
419  if (iv)
420  blowfish_setiv(&cx->ctx.bf, iv);
421 
422  return 0;
423 }
BlowfishContext bf
Definition: internal.c:231
void * ptr
Definition: px.h:150
union int_ctx::@1 ctx
uint8 iv[INT_MAX_IV]
Definition: internal.c:228
int cx(PlannerInfo *root, Gene *tour1, Gene *tour2, Gene *offspring, int num_gene, City *city_table)
void blowfish_setiv(BlowfishContext *ctx, const uint8 *iv)
Definition: blf.c:495
void blowfish_setkey(BlowfishContext *ctx, const uint8 *key, short keybytes)
Definition: blf.c:429

◆ bf_iv_size()

static unsigned bf_iv_size ( PX_Cipher c)
static

Definition at line 408 of file internal.c.

Referenced by bf_load().

409 {
410  return 8;
411 }

◆ bf_key_size()

static unsigned bf_key_size ( PX_Cipher c)
static

Definition at line 402 of file internal.c.

Referenced by bf_load().

403 {
404  return 448 / 8;
405 }

◆ bf_load()

static PX_Cipher* bf_load ( int  mode)
static

Definition at line 476 of file internal.c.

References bf_block_size(), bf_decrypt(), bf_encrypt(), bf_init(), bf_iv_size(), bf_key_size(), px_cipher::block_size, cx(), px_cipher::decrypt, px_cipher::encrypt, px_cipher::free, px_cipher::init, intctx_free(), px_cipher::iv_size, px_cipher::key_size, mode, int_ctx::mode, palloc0(), and px_cipher::ptr.

Referenced by bf_cbc_load(), and bf_ecb_load().

477 {
478  PX_Cipher *c;
479  struct int_ctx *cx;
480 
481  c = palloc0(sizeof *c);
482 
484  c->key_size = bf_key_size;
485  c->iv_size = bf_iv_size;
486  c->init = bf_init;
487  c->encrypt = bf_encrypt;
488  c->decrypt = bf_decrypt;
489  c->free = intctx_free;
490 
491  cx = palloc0(sizeof *cx);
492  cx->mode = mode;
493  c->ptr = cx;
494  return c;
495 }
static PgChecksumMode mode
Definition: pg_checksums.c:61
static int bf_decrypt(PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
Definition: internal.c:451
static void intctx_free(PX_Cipher *c)
Definition: internal.c:240
unsigned(* block_size)(PX_Cipher *c)
Definition: px.h:141
int(* decrypt)(PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
Definition: px.h:147
void * ptr
Definition: px.h:150
void(* free)(PX_Cipher *c)
Definition: px.h:148
int cx(PlannerInfo *root, Gene *tour1, Gene *tour2, Gene *offspring, int num_gene, City *city_table)
unsigned(* iv_size)(PX_Cipher *c)
Definition: px.h:143
char * c
unsigned(* key_size)(PX_Cipher *c)
Definition: px.h:142
static int bf_init(PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
Definition: internal.c:414
int(* encrypt)(PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
Definition: px.h:146
void * palloc0(Size size)
Definition: mcxt.c:981
static unsigned bf_key_size(PX_Cipher *c)
Definition: internal.c:402
static int bf_encrypt(PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
Definition: internal.c:426
int(* init)(PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
Definition: px.h:145
Definition: px.h:139
static unsigned bf_block_size(PX_Cipher *c)
Definition: internal.c:396
static unsigned bf_iv_size(PX_Cipher *c)
Definition: internal.c:408
int mode
Definition: internal.c:236

◆ init_md5()

static void init_md5 ( PX_MD h)
static

Definition at line 181 of file internal.c.

References px_digest::block_size, px_digest::finish, px_digest::free, int_md5_block_len(), int_md5_finish(), int_md5_free(), int_md5_len(), int_md5_reset(), int_md5_update(), px_digest::p, pg_cryptohash_create(), PG_MD5, px_digest::ptr, px_digest::reset, px_digest::result_size, and px_digest::update.

182 {
184 
186 
187  md->p.ptr = ctx;
188 
189  md->result_size = int_md5_len;
190  md->block_size = int_md5_block_len;
191  md->reset = int_md5_reset;
192  md->update = int_md5_update;
193  md->finish = int_md5_finish;
194  md->free = int_md5_free;
195 
196  md->reset(md);
197 }
union int_ctx::@1 ctx
static void int_md5_reset(PX_MD *h)
Definition: internal.c:104
static unsigned int_md5_len(PX_MD *h)
Definition: internal.c:83
static void int_md5_finish(PX_MD *h, uint8 *dst)
Definition: internal.c:113
static unsigned int_md5_block_len(PX_MD *h)
Definition: internal.c:89
pg_cryptohash_ctx * pg_cryptohash_create(pg_cryptohash_type type)
Definition: cryptohash.c:64
static void int_md5_free(PX_MD *h)
Definition: internal.c:122
static void int_md5_update(PX_MD *h, const uint8 *data, unsigned dlen)
Definition: internal.c:95

◆ init_sha1()

static void init_sha1 ( PX_MD h)
static

Definition at line 200 of file internal.c.

References px_digest::block_size, px_digest::finish, px_digest::free, int_sha1_block_len(), int_sha1_finish(), int_sha1_free(), int_sha1_len(), int_sha1_reset(), int_sha1_update(), px_digest::p, palloc0(), px_digest::ptr, px_digest::reset, px_digest::result_size, and px_digest::update.

201 {
202  SHA1_CTX *ctx;
203 
204  ctx = palloc0(sizeof(*ctx));
205 
206  md->p.ptr = ctx;
207 
208  md->result_size = int_sha1_len;
209  md->block_size = int_sha1_block_len;
210  md->reset = int_sha1_reset;
211  md->update = int_sha1_update;
212  md->finish = int_sha1_finish;
213  md->free = int_sha1_free;
214 
215  md->reset(md);
216 }
static unsigned int_sha1_block_len(PX_MD *h)
Definition: internal.c:139
union int_ctx::@1 ctx
static void int_sha1_free(PX_MD *h)
Definition: internal.c:169
static void int_sha1_update(PX_MD *h, const uint8 *data, unsigned dlen)
Definition: internal.c:145
Definition: sha1.h:41
static unsigned int_sha1_len(PX_MD *h)
Definition: internal.c:133
void * palloc0(Size size)
Definition: mcxt.c:981
static void int_sha1_reset(PX_MD *h)
Definition: internal.c:153
static void int_sha1_finish(PX_MD *h, uint8 *dst)
Definition: internal.c:161

◆ init_sha224()

void init_sha224 ( PX_MD h)

Definition at line 137 of file internal-sha2.c.

References px_digest::block_size, px_digest::finish, px_digest::free, int_sha224_block_len(), int_sha224_len(), int_sha2_finish(), int_sha2_free(), int_sha2_reset(), int_sha2_update(), px_digest::p, pg_cryptohash_create(), PG_SHA224, px_digest::ptr, px_digest::reset, px_digest::result_size, and px_digest::update.

138 {
140 
142  md->p.ptr = ctx;
143 
144  md->result_size = int_sha224_len;
145  md->block_size = int_sha224_block_len;
146  md->reset = int_sha2_reset;
147  md->update = int_sha2_update;
148  md->finish = int_sha2_finish;
149  md->free = int_sha2_free;
150 
151  md->reset(md);
152 }
static void int_sha2_reset(PX_MD *h)
union int_ctx::@1 ctx
static unsigned int_sha224_block_len(PX_MD *h)
Definition: internal-sha2.c:53
static void int_sha2_free(PX_MD *h)
static void int_sha2_update(PX_MD *h, const uint8 *data, unsigned dlen)
Definition: internal-sha2.c:99
static void int_sha2_finish(PX_MD *h, uint8 *dst)
pg_cryptohash_ctx * pg_cryptohash_create(pg_cryptohash_type type)
Definition: cryptohash.c:64
static unsigned int_sha224_len(PX_MD *h)
Definition: internal-sha2.c:47

◆ init_sha256()

void init_sha256 ( PX_MD h)

Definition at line 155 of file internal-sha2.c.

References px_digest::block_size, px_digest::finish, px_digest::free, int_sha256_block_len(), int_sha256_len(), int_sha2_finish(), int_sha2_free(), int_sha2_reset(), int_sha2_update(), px_digest::p, pg_cryptohash_create(), PG_SHA256, px_digest::ptr, px_digest::reset, px_digest::result_size, and px_digest::update.

156 {
158 
160  md->p.ptr = ctx;
161 
162  md->result_size = int_sha256_len;
163  md->block_size = int_sha256_block_len;
164  md->reset = int_sha2_reset;
165  md->update = int_sha2_update;
166  md->finish = int_sha2_finish;
167  md->free = int_sha2_free;
168 
169  md->reset(md);
170 }
static void int_sha2_reset(PX_MD *h)
union int_ctx::@1 ctx
static unsigned int_sha256_len(PX_MD *h)
Definition: internal-sha2.c:60
static void int_sha2_free(PX_MD *h)
static void int_sha2_update(PX_MD *h, const uint8 *data, unsigned dlen)
Definition: internal-sha2.c:99
static void int_sha2_finish(PX_MD *h, uint8 *dst)
pg_cryptohash_ctx * pg_cryptohash_create(pg_cryptohash_type type)
Definition: cryptohash.c:64
static unsigned int_sha256_block_len(PX_MD *h)
Definition: internal-sha2.c:66

◆ init_sha384()

void init_sha384 ( PX_MD h)

Definition at line 173 of file internal-sha2.c.

References px_digest::block_size, px_digest::finish, px_digest::free, int_sha2_finish(), int_sha2_free(), int_sha2_reset(), int_sha2_update(), int_sha384_block_len(), int_sha384_len(), px_digest::p, pg_cryptohash_create(), PG_SHA384, px_digest::ptr, px_digest::reset, px_digest::result_size, and px_digest::update.

174 {
176 
178  md->p.ptr = ctx;
179 
180  md->result_size = int_sha384_len;
181  md->block_size = int_sha384_block_len;
182  md->reset = int_sha2_reset;
183  md->update = int_sha2_update;
184  md->finish = int_sha2_finish;
185  md->free = int_sha2_free;
186 
187  md->reset(md);
188 }
static void int_sha2_reset(PX_MD *h)
union int_ctx::@1 ctx
static unsigned int_sha384_block_len(PX_MD *h)
Definition: internal-sha2.c:79
static void int_sha2_free(PX_MD *h)
static void int_sha2_update(PX_MD *h, const uint8 *data, unsigned dlen)
Definition: internal-sha2.c:99
static void int_sha2_finish(PX_MD *h, uint8 *dst)
pg_cryptohash_ctx * pg_cryptohash_create(pg_cryptohash_type type)
Definition: cryptohash.c:64
static unsigned int_sha384_len(PX_MD *h)
Definition: internal-sha2.c:73

◆ init_sha512()

void init_sha512 ( PX_MD h)

Definition at line 191 of file internal-sha2.c.

References px_digest::block_size, px_digest::finish, px_digest::free, int_sha2_finish(), int_sha2_free(), int_sha2_reset(), int_sha2_update(), int_sha512_block_len(), int_sha512_len(), px_digest::p, pg_cryptohash_create(), PG_SHA512, px_digest::ptr, px_digest::reset, px_digest::result_size, and px_digest::update.

192 {
194 
196  md->p.ptr = ctx;
197 
198  md->result_size = int_sha512_len;
199  md->block_size = int_sha512_block_len;
200  md->reset = int_sha2_reset;
201  md->update = int_sha2_update;
202  md->finish = int_sha2_finish;
203  md->free = int_sha2_free;
204 
205  md->reset(md);
206 }
static void int_sha2_reset(PX_MD *h)
static unsigned int_sha512_len(PX_MD *h)
Definition: internal-sha2.c:86
union int_ctx::@1 ctx
static void int_sha2_free(PX_MD *h)
static void int_sha2_update(PX_MD *h, const uint8 *data, unsigned dlen)
Definition: internal-sha2.c:99
static unsigned int_sha512_block_len(PX_MD *h)
Definition: internal-sha2.c:92
static void int_sha2_finish(PX_MD *h, uint8 *dst)
pg_cryptohash_ctx * pg_cryptohash_create(pg_cryptohash_type type)
Definition: cryptohash.c:64

◆ int_md5_block_len()

static unsigned int_md5_block_len ( PX_MD h)
static

Definition at line 89 of file internal.c.

References MD5_BLOCK_SIZE.

Referenced by init_md5().

90 {
91  return MD5_BLOCK_SIZE;
92 }
#define MD5_BLOCK_SIZE
Definition: internal.c:53

◆ int_md5_finish()

static void int_md5_finish ( PX_MD h,
uint8 dst 
)
static

Definition at line 113 of file internal.c.

References elog, ERROR, px_digest::p, pg_cryptohash_final(), and px_digest::ptr.

Referenced by init_md5().

114 {
116 
117  if (pg_cryptohash_final(ctx, dst) < 0)
118  elog(ERROR, "could not finalize %s context", "MD5");
119 }
void * ptr
Definition: px.h:110
union int_ctx::@1 ctx
#define ERROR
Definition: elog.h:45
union px_digest::@10 p
int pg_cryptohash_final(pg_cryptohash_ctx *ctx, uint8 *dest)
Definition: cryptohash.c:160
#define elog(elevel,...)
Definition: elog.h:228

◆ int_md5_free()

static void int_md5_free ( PX_MD h)
static

Definition at line 122 of file internal.c.

References px_digest::p, pfree(), pg_cryptohash_free(), and px_digest::ptr.

Referenced by init_md5().

123 {
125 
126  pg_cryptohash_free(ctx);
127  pfree(h);
128 }
void * ptr
Definition: px.h:110
union int_ctx::@1 ctx
void pfree(void *pointer)
Definition: mcxt.c:1057
union px_digest::@10 p
void pg_cryptohash_free(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:193

◆ int_md5_len()

static unsigned int_md5_len ( PX_MD h)
static

Definition at line 83 of file internal.c.

References MD5_DIGEST_LENGTH.

Referenced by init_md5().

84 {
85  return MD5_DIGEST_LENGTH;
86 }
#define MD5_DIGEST_LENGTH
Definition: md5.h:20

◆ int_md5_reset()

static void int_md5_reset ( PX_MD h)
static

Definition at line 104 of file internal.c.

References elog, ERROR, px_digest::p, pg_cryptohash_init(), and px_digest::ptr.

Referenced by init_md5().

105 {
107 
108  if (pg_cryptohash_init(ctx) < 0)
109  elog(ERROR, "could not initialize %s context", "MD5");
110 }
void * ptr
Definition: px.h:110
union int_ctx::@1 ctx
#define ERROR
Definition: elog.h:45
union px_digest::@10 p
int pg_cryptohash_init(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:90
#define elog(elevel,...)
Definition: elog.h:228

◆ int_md5_update()

static void int_md5_update ( PX_MD h,
const uint8 data,
unsigned  dlen 
)
static

Definition at line 95 of file internal.c.

References elog, ERROR, px_digest::p, pg_cryptohash_update(), and px_digest::ptr.

Referenced by init_md5().

96 {
98 
99  if (pg_cryptohash_update(ctx, data, dlen) < 0)
100  elog(ERROR, "could not update %s context", "MD5");
101 }
void * ptr
Definition: px.h:110
union int_ctx::@1 ctx
#define ERROR
Definition: elog.h:45
union px_digest::@10 p
int pg_cryptohash_update(pg_cryptohash_ctx *ctx, const uint8 *data, size_t len)
Definition: cryptohash.c:125
#define elog(elevel,...)
Definition: elog.h:228

◆ int_sha1_block_len()

static unsigned int_sha1_block_len ( PX_MD h)
static

Definition at line 139 of file internal.c.

References SHA1_BLOCK_SIZE.

Referenced by init_sha1().

140 {
141  return SHA1_BLOCK_SIZE;
142 }
#define SHA1_BLOCK_SIZE
Definition: internal.c:52

◆ int_sha1_finish()

static void int_sha1_finish ( PX_MD h,
uint8 dst 
)
static

Definition at line 161 of file internal.c.

References px_digest::p, px_digest::ptr, and SHA1Final.

Referenced by init_sha1().

162 {
163  SHA1_CTX *ctx = (SHA1_CTX *) h->p.ptr;
164 
165  SHA1Final(dst, ctx);
166 }
void * ptr
Definition: px.h:110
union int_ctx::@1 ctx
Definition: sha1.h:41
union px_digest::@10 p
#define SHA1Final(x, y)
Definition: sha1.h:71

◆ int_sha1_free()

static void int_sha1_free ( PX_MD h)
static

Definition at line 169 of file internal.c.

References px_digest::p, pfree(), px_digest::ptr, and px_memset().

Referenced by init_sha1().

170 {
171  SHA1_CTX *ctx = (SHA1_CTX *) h->p.ptr;
172 
173  px_memset(ctx, 0, sizeof(*ctx));
174  pfree(ctx);
175  pfree(h);
176 }
void * ptr
Definition: px.h:110
union int_ctx::@1 ctx
void pfree(void *pointer)
Definition: mcxt.c:1057
Definition: sha1.h:41
union px_digest::@10 p
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:127

◆ int_sha1_len()

static unsigned int_sha1_len ( PX_MD h)
static

Definition at line 133 of file internal.c.

References SHA1_DIGEST_LENGTH.

Referenced by init_sha1().

134 {
135  return SHA1_DIGEST_LENGTH;
136 }
#define SHA1_DIGEST_LENGTH
Definition: internal.c:46

◆ int_sha1_reset()

static void int_sha1_reset ( PX_MD h)
static

Definition at line 153 of file internal.c.

References px_digest::p, px_digest::ptr, and SHA1Init.

Referenced by init_sha1().

154 {
155  SHA1_CTX *ctx = (SHA1_CTX *) h->p.ptr;
156 
157  SHA1Init(ctx);
158 }
void * ptr
Definition: px.h:110
#define SHA1Init(x)
Definition: sha1.h:69
union int_ctx::@1 ctx
Definition: sha1.h:41
union px_digest::@10 p

◆ int_sha1_update()

static void int_sha1_update ( PX_MD h,
const uint8 data,
unsigned  dlen 
)
static

Definition at line 145 of file internal.c.

References px_digest::p, px_digest::ptr, and SHA1Update.

Referenced by init_sha1().

146 {
147  SHA1_CTX *ctx = (SHA1_CTX *) h->p.ptr;
148 
149  SHA1Update(ctx, data, dlen);
150 }
void * ptr
Definition: px.h:110
union int_ctx::@1 ctx
Definition: sha1.h:41
union px_digest::@10 p
#define SHA1Update(x, y, z)
Definition: sha1.h:70

◆ intctx_free()

static void intctx_free ( PX_Cipher c)
static

Definition at line 240 of file internal.c.

References cx(), pfree(), px_cipher::ptr, and px_memset().

Referenced by bf_load(), and rj_load().

241 {
242  struct int_ctx *cx = (struct int_ctx *) c->ptr;
243 
244  if (cx)
245  {
246  px_memset(cx, 0, sizeof *cx);
247  pfree(cx);
248  }
249  pfree(c);
250 }
void * ptr
Definition: px.h:150
void pfree(void *pointer)
Definition: mcxt.c:1057
int cx(PlannerInfo *root, Gene *tour1, Gene *tour2, Gene *offspring, int num_gene, City *city_table)
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:127

◆ px_find_cipher()

int px_find_cipher ( const char *  name,
PX_Cipher **  res 
)

Definition at line 572 of file internal.c.

Referenced by pgp_load_cipher(), and px_find_combo().

573 {
574  int i;
575  PX_Cipher *c = NULL;
576 
578 
579  for (i = 0; int_ciphers[i].name; i++)
580  if (strcmp(int_ciphers[i].name, name) == 0)
581  {
582  c = int_ciphers[i].load();
583  break;
584  }
585 
586  if (c == NULL)
587  return PXE_NO_CIPHER;
588 
589  *res = c;
590  return 0;
591 }
static const struct int_cipher int_ciphers[]
Definition: internal.c:530
PX_Cipher *(* load)(void)
Definition: internal.c:526
char * name
Definition: internal.c:525
const char * px_resolve_alias(const PX_Alias *list, const char *name)
Definition: px.c:133
char * c
Definition: px.h:139
const char * name
Definition: encode.c:515
int i
#define PXE_NO_CIPHER
Definition: px.h:49
static const PX_Alias int_aliases[]
Definition: internal.c:538

◆ px_find_digest()

int px_find_digest ( const char *  name,
PX_MD **  res 
)

Definition at line 553 of file internal.c.

Referenced by pg_digest(), pgp_load_digest(), px_crypt_md5(), and px_find_hmac().

554 {
555  const struct int_digest *p;
556  PX_MD *h;
557 
558  for (p = int_digest_list; p->name; p++)
559  if (pg_strcasecmp(p->name, name) == 0)
560  {
561  h = palloc(sizeof(*h));
562  p->init(h);
563 
564  *res = h;
565 
566  return 0;
567  }
568  return PXE_NO_HASH;
569 }
static const struct int_digest int_digest_list[]
Definition: internal.c:70
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
Definition: px.h:98
#define PXE_NO_HASH
Definition: px.h:48
char * name
Definition: internal.c:65
const char * name
Definition: encode.c:515
void * palloc(Size size)
Definition: mcxt.c:950
void(* init)(PX_MD *h)
Definition: internal.c:66

◆ rj_128_cbc()

static PX_Cipher* rj_128_cbc ( void  )
static

Definition at line 506 of file internal.c.

References MODE_CBC, and rj_load().

507 {
508  return rj_load(MODE_CBC);
509 }
#define MODE_CBC
Definition: internal.c:257
static PX_Cipher * rj_load(int mode)
Definition: internal.c:369

◆ rj_128_ecb()

static PX_Cipher* rj_128_ecb ( void  )
static

Definition at line 500 of file internal.c.

References MODE_ECB, and rj_load().

501 {
502  return rj_load(MODE_ECB);
503 }
#define MODE_ECB
Definition: internal.c:256
static PX_Cipher * rj_load(int mode)
Definition: internal.c:369

◆ rj_block_size()

static unsigned rj_block_size ( PX_Cipher c)
static

Definition at line 260 of file internal.c.

Referenced by rj_load().

261 {
262  return 128 / 8;
263 }

◆ rj_decrypt()

static int rj_decrypt ( PX_Cipher c,
const uint8 data,
unsigned  dlen,
uint8 res 
)
static

Definition at line 337 of file internal.c.

References aes_cbc_decrypt(), aes_ecb_decrypt(), int_ctx::ctx, cx(), int_ctx::is_init, int_ctx::iv, int_ctx::mode, MODE_CBC, px_cipher::ptr, PXE_CIPHER_INIT, PXE_NOTBLOCKSIZE, int_ctx::rj, and rj_real_init().

Referenced by rj_load().

338 {
339  struct int_ctx *cx = (struct int_ctx *) c->ptr;
340 
341  if (!cx->is_init)
342  if (rj_real_init(cx, 0))
343  return PXE_CIPHER_INIT;
344 
345  if (dlen == 0)
346  return 0;
347 
348  if (dlen & 15)
349  return PXE_NOTBLOCKSIZE;
350 
351  memcpy(res, data, dlen);
352 
353  if (cx->mode == MODE_CBC)
354  {
355  aes_cbc_decrypt(&cx->ctx.rj, cx->iv, res, dlen);
356  memcpy(cx->iv, data + dlen - 16, 16);
357  }
358  else
359  aes_ecb_decrypt(&cx->ctx.rj, res, dlen);
360 
361  return 0;
362 }
void aes_cbc_decrypt(rijndael_ctx *ctx, uint8 *iva, uint8 *data, unsigned len)
Definition: rijndael.c:567
void * ptr
Definition: px.h:150
union int_ctx::@1 ctx
#define MODE_CBC
Definition: internal.c:257
uint8 iv[INT_MAX_IV]
Definition: internal.c:228
int cx(PlannerInfo *root, Gene *tour1, Gene *tour2, Gene *offspring, int num_gene, City *city_table)
void aes_ecb_decrypt(rijndael_ctx *ctx, uint8 *data, unsigned len)
Definition: rijndael.c:529
static int rj_real_init(struct int_ctx *cx, int dir)
Definition: internal.c:300
#define PXE_CIPHER_INIT
Definition: px.h:54
#define PXE_NOTBLOCKSIZE
Definition: px.h:50
rijndael_ctx rj
Definition: internal.c:232
int mode
Definition: internal.c:236
int is_init
Definition: internal.c:235

◆ rj_encrypt()

static int rj_encrypt ( PX_Cipher c,
const uint8 data,
unsigned  dlen,
uint8 res 
)
static

Definition at line 307 of file internal.c.

References aes_cbc_encrypt(), aes_ecb_encrypt(), int_ctx::ctx, cx(), int_ctx::is_init, int_ctx::iv, int_ctx::mode, MODE_CBC, px_cipher::ptr, PXE_CIPHER_INIT, PXE_NOTBLOCKSIZE, int_ctx::rj, and rj_real_init().

Referenced by rj_load().

308 {
309  struct int_ctx *cx = (struct int_ctx *) c->ptr;
310 
311  if (!cx->is_init)
312  {
313  if (rj_real_init(cx, 1))
314  return PXE_CIPHER_INIT;
315  }
316 
317  if (dlen == 0)
318  return 0;
319 
320  if (dlen & 15)
321  return PXE_NOTBLOCKSIZE;
322 
323  memcpy(res, data, dlen);
324 
325  if (cx->mode == MODE_CBC)
326  {
327  aes_cbc_encrypt(&cx->ctx.rj, cx->iv, res, dlen);
328  memcpy(cx->iv, res + dlen - 16, 16);
329  }
330  else
331  aes_ecb_encrypt(&cx->ctx.rj, res, dlen);
332 
333  return 0;
334 }
void aes_cbc_encrypt(rijndael_ctx *ctx, uint8 *iva, uint8 *data, unsigned len)
Definition: rijndael.c:545
void * ptr
Definition: px.h:150
union int_ctx::@1 ctx
#define MODE_CBC
Definition: internal.c:257
uint8 iv[INT_MAX_IV]
Definition: internal.c:228
int cx(PlannerInfo *root, Gene *tour1, Gene *tour2, Gene *offspring, int num_gene, City *city_table)
static int rj_real_init(struct int_ctx *cx, int dir)
Definition: internal.c:300
#define PXE_CIPHER_INIT
Definition: px.h:54
void aes_ecb_encrypt(rijndael_ctx *ctx, uint8 *data, unsigned len)
Definition: rijndael.c:513
#define PXE_NOTBLOCKSIZE
Definition: px.h:50
rijndael_ctx rj
Definition: internal.c:232
int mode
Definition: internal.c:236
int is_init
Definition: internal.c:235

◆ rj_init()

static int rj_init ( PX_Cipher c,
const uint8 key,
unsigned  klen,
const uint8 iv 
)
static

Definition at line 278 of file internal.c.

References cx(), int_ctx::iv, int_ctx::keybuf, int_ctx::keylen, px_cipher::ptr, and PXE_KEY_TOO_BIG.

Referenced by rj_load().

279 {
280  struct int_ctx *cx = (struct int_ctx *) c->ptr;
281 
282  if (klen <= 128 / 8)
283  cx->keylen = 128 / 8;
284  else if (klen <= 192 / 8)
285  cx->keylen = 192 / 8;
286  else if (klen <= 256 / 8)
287  cx->keylen = 256 / 8;
288  else
289  return PXE_KEY_TOO_BIG;
290 
291  memcpy(&cx->keybuf, key, klen);
292 
293  if (iv)
294  memcpy(cx->iv, iv, 128 / 8);
295 
296  return 0;
297 }
void * ptr
Definition: px.h:150
uint8 keybuf[INT_MAX_KEY]
Definition: internal.c:227
uint8 iv[INT_MAX_IV]
Definition: internal.c:228
int cx(PlannerInfo *root, Gene *tour1, Gene *tour2, Gene *offspring, int num_gene, City *city_table)
#define PXE_KEY_TOO_BIG
Definition: px.h:53
unsigned keylen
Definition: internal.c:234

◆ rj_iv_size()

static unsigned rj_iv_size ( PX_Cipher c)
static

Definition at line 272 of file internal.c.

Referenced by rj_load().

273 {
274  return 128 / 8;
275 }

◆ rj_key_size()

static unsigned rj_key_size ( PX_Cipher c)
static

Definition at line 266 of file internal.c.

Referenced by rj_load().

267 {
268  return 256 / 8;
269 }

◆ rj_load()

static PX_Cipher* rj_load ( int  mode)
static

Definition at line 369 of file internal.c.

References px_cipher::block_size, cx(), px_cipher::decrypt, px_cipher::encrypt, px_cipher::free, px_cipher::init, intctx_free(), px_cipher::iv_size, px_cipher::key_size, mode, int_ctx::mode, palloc0(), px_cipher::ptr, rj_block_size(), rj_decrypt(), rj_encrypt(), rj_init(), rj_iv_size(), and rj_key_size().

Referenced by rj_128_cbc(), and rj_128_ecb().

370 {
371  PX_Cipher *c;
372  struct int_ctx *cx;
373 
374  c = palloc0(sizeof *c);
375 
377  c->key_size = rj_key_size;
378  c->iv_size = rj_iv_size;
379  c->init = rj_init;
380  c->encrypt = rj_encrypt;
381  c->decrypt = rj_decrypt;
382  c->free = intctx_free;
383 
384  cx = palloc0(sizeof *cx);
385  cx->mode = mode;
386 
387  c->ptr = cx;
388  return c;
389 }
static PgChecksumMode mode
Definition: pg_checksums.c:61
static void intctx_free(PX_Cipher *c)
Definition: internal.c:240
unsigned(* block_size)(PX_Cipher *c)
Definition: px.h:141
int(* decrypt)(PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
Definition: px.h:147
void * ptr
Definition: px.h:150
void(* free)(PX_Cipher *c)
Definition: px.h:148
static unsigned rj_block_size(PX_Cipher *c)
Definition: internal.c:260
int cx(PlannerInfo *root, Gene *tour1, Gene *tour2, Gene *offspring, int num_gene, City *city_table)
static int rj_encrypt(PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
Definition: internal.c:307
static int rj_init(PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
Definition: internal.c:278
static unsigned rj_key_size(PX_Cipher *c)
Definition: internal.c:266
unsigned(* iv_size)(PX_Cipher *c)
Definition: px.h:143
char * c
unsigned(* key_size)(PX_Cipher *c)
Definition: px.h:142
static unsigned rj_iv_size(PX_Cipher *c)
Definition: internal.c:272
int(* encrypt)(PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
Definition: px.h:146
void * palloc0(Size size)
Definition: mcxt.c:981
static int rj_decrypt(PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
Definition: internal.c:337
int(* init)(PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
Definition: px.h:145
Definition: px.h:139
int mode
Definition: internal.c:236

◆ rj_real_init()

static int rj_real_init ( struct int_ctx cx,
int  dir 
)
static

Definition at line 300 of file internal.c.

References aes_set_key(), int_ctx::ctx, int_ctx::keybuf, int_ctx::keylen, and int_ctx::rj.

Referenced by rj_decrypt(), and rj_encrypt().

301 {
302  aes_set_key(&cx->ctx.rj, cx->keybuf, cx->keylen * 8, dir);
303  return 0;
304 }
uint8 keybuf[INT_MAX_KEY]
Definition: internal.c:227
union int_ctx::@1 ctx
void aes_set_key(rijndael_ctx *ctx, const uint8 *key, unsigned keybits, int enc)
Definition: rijndael.c:504
unsigned keylen
Definition: internal.c:234
rijndael_ctx rj
Definition: internal.c:232

Variable Documentation

◆ int_aliases

const PX_Alias int_aliases[]
static
Initial value:
= {
{"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}
}

Definition at line 538 of file internal.c.

◆ int_ciphers

const struct int_cipher int_ciphers[]
static
Initial value:
= {
{"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_Cipher * bf_cbc_load(void)
Definition: internal.c:518
static PX_Cipher * bf_ecb_load(void)
Definition: internal.c:512
static PX_Cipher * rj_128_ecb(void)
Definition: internal.c:500
static PX_Cipher * rj_128_cbc(void)
Definition: internal.c:506

Definition at line 530 of file internal.c.

◆ int_digest_list

const struct int_digest int_digest_list[]
static
Initial value:
= {
{"md5", init_md5},
{"sha1", init_sha1},
{"sha224", init_sha224},
{"sha256", init_sha256},
{"sha384", init_sha384},
{"sha512", init_sha512},
{NULL, NULL}
}
void init_sha384(PX_MD *h)
void init_sha224(PX_MD *h)
static void init_md5(PX_MD *h)
Definition: internal.c:181
void init_sha256(PX_MD *h)
void init_sha512(PX_MD *h)
static void init_sha1(PX_MD *h)
Definition: internal.c:200

Definition at line 70 of file internal.c.