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 "common/cryptohash.h"
#include "common/md5.h"
#include "common/sha1.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_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 217 of file internal.c.

◆ INT_MAX_KEY

#define INT_MAX_KEY   (512/8)

Definition at line 216 of file internal.c.

◆ MD5_BLOCK_SIZE

#define MD5_BLOCK_SIZE   64

Definition at line 45 of file internal.c.

Referenced by int_md5_block_len(), and pg_hmac_create().

◆ MODE_CBC

#define MODE_CBC   1

Definition at line 251 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 250 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 44 of file internal.c.

Referenced by int_sha1_block_len(), and pg_hmac_create().

Function Documentation

◆ bf_block_size()

static unsigned bf_block_size ( PX_Cipher c)
static

Definition at line 390 of file internal.c.

Referenced by bf_load().

391 {
392  return 8;
393 }

◆ bf_cbc_load()

static PX_Cipher* bf_cbc_load ( void  )
static

Definition at line 512 of file internal.c.

References bf_load(), and MODE_CBC.

513 {
514  return bf_load(MODE_CBC);
515 }
static PX_Cipher * bf_load(int mode)
Definition: internal.c:470
#define MODE_CBC
Definition: internal.c:251

◆ bf_decrypt()

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

Definition at line 445 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().

446 {
447  struct int_ctx *cx = (struct int_ctx *) c->ptr;
448  BlowfishContext *bfctx = &cx->ctx.bf;
449 
450  if (dlen == 0)
451  return 0;
452 
453  if (dlen & 7)
454  return PXE_NOTBLOCKSIZE;
455 
456  memcpy(res, data, dlen);
457  switch (cx->mode)
458  {
459  case MODE_ECB:
460  blowfish_decrypt_ecb(res, dlen, bfctx);
461  break;
462  case MODE_CBC:
463  blowfish_decrypt_cbc(res, dlen, bfctx);
464  break;
465  }
466  return 0;
467 }
union int_ctx::@0 ctx
BlowfishContext bf
Definition: internal.c:225
void * ptr
Definition: px.h:150
#define MODE_CBC
Definition: internal.c:251
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:250
#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:230

◆ bf_ecb_load()

static PX_Cipher* bf_ecb_load ( void  )
static

Definition at line 506 of file internal.c.

References bf_load(), and MODE_ECB.

507 {
508  return bf_load(MODE_ECB);
509 }
static PX_Cipher * bf_load(int mode)
Definition: internal.c:470
#define MODE_ECB
Definition: internal.c:250

◆ bf_encrypt()

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

Definition at line 420 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().

421 {
422  struct int_ctx *cx = (struct int_ctx *) c->ptr;
423  BlowfishContext *bfctx = &cx->ctx.bf;
424 
425  if (dlen == 0)
426  return 0;
427 
428  if (dlen & 7)
429  return PXE_NOTBLOCKSIZE;
430 
431  memcpy(res, data, dlen);
432  switch (cx->mode)
433  {
434  case MODE_ECB:
435  blowfish_encrypt_ecb(res, dlen, bfctx);
436  break;
437  case MODE_CBC:
438  blowfish_encrypt_cbc(res, dlen, bfctx);
439  break;
440  }
441  return 0;
442 }
union int_ctx::@0 ctx
BlowfishContext bf
Definition: internal.c:225
void blowfish_encrypt_cbc(uint8 *blk, int len, BlowfishContext *ctx)
Definition: blf.c:321
void * ptr
Definition: px.h:150
#define MODE_CBC
Definition: internal.c:251
int cx(PlannerInfo *root, Gene *tour1, Gene *tour2, Gene *offspring, int num_gene, City *city_table)
#define MODE_ECB
Definition: internal.c:250
#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:230

◆ bf_init()

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

Definition at line 408 of file internal.c.

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

Referenced by bf_load().

409 {
410  struct int_ctx *cx = (struct int_ctx *) c->ptr;
411 
412  blowfish_setkey(&cx->ctx.bf, key, klen);
413  if (iv)
414  blowfish_setiv(&cx->ctx.bf, iv);
415 
416  return 0;
417 }
union int_ctx::@0 ctx
BlowfishContext bf
Definition: internal.c:225
void * ptr
Definition: px.h:150
uint8 iv[INT_MAX_IV]
Definition: internal.c:222
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 402 of file internal.c.

Referenced by bf_load().

403 {
404  return 8;
405 }

◆ bf_key_size()

static unsigned bf_key_size ( PX_Cipher c)
static

Definition at line 396 of file internal.c.

Referenced by bf_load().

397 {
398  return 448 / 8;
399 }

◆ bf_load()

static PX_Cipher* bf_load ( int  mode)
static

Definition at line 470 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().

471 {
472  PX_Cipher *c;
473  struct int_ctx *cx;
474 
475  c = palloc0(sizeof *c);
476 
478  c->key_size = bf_key_size;
479  c->iv_size = bf_iv_size;
480  c->init = bf_init;
481  c->encrypt = bf_encrypt;
482  c->decrypt = bf_decrypt;
483  c->free = intctx_free;
484 
485  cx = palloc0(sizeof *cx);
486  cx->mode = mode;
487  c->ptr = cx;
488  return c;
489 }
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:445
static void intctx_free(PX_Cipher *c)
Definition: internal.c:234
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:408
int(* encrypt)(PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
Definition: px.h:146
void * palloc0(Size size)
Definition: mcxt.c:1093
static unsigned bf_key_size(PX_Cipher *c)
Definition: internal.c:396
static int bf_encrypt(PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
Definition: internal.c:420
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:390
static unsigned bf_iv_size(PX_Cipher *c)
Definition: internal.c:402
int mode
Definition: internal.c:230

◆ init_md5()

static void init_md5 ( PX_MD h)
static

Definition at line 175 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.

176 {
178 
180 
181  md->p.ptr = ctx;
182 
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;
189 
190  md->reset(md);
191 }
union int_ctx::@0 ctx
static void int_md5_reset(PX_MD *h)
Definition: internal.c:96
static unsigned int_md5_len(PX_MD *h)
Definition: internal.c:75
static void int_md5_finish(PX_MD *h, uint8 *dst)
Definition: internal.c:105
static unsigned int_md5_block_len(PX_MD *h)
Definition: internal.c:81
pg_cryptohash_ctx * pg_cryptohash_create(pg_cryptohash_type type)
Definition: cryptohash.c:66
static void int_md5_free(PX_MD *h)
Definition: internal.c:114
static void int_md5_update(PX_MD *h, const uint8 *data, unsigned dlen)
Definition: internal.c:87

◆ init_sha1()

static void init_sha1 ( PX_MD h)
static

Definition at line 194 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, pg_cryptohash_create(), PG_SHA1, px_digest::ptr, px_digest::reset, px_digest::result_size, and px_digest::update.

195 {
197 
199 
200  md->p.ptr = ctx;
201 
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;
208 
209  md->reset(md);
210 }
union int_ctx::@0 ctx
static unsigned int_sha1_block_len(PX_MD *h)
Definition: internal.c:131
static void int_sha1_free(PX_MD *h)
Definition: internal.c:164
static void int_sha1_update(PX_MD *h, const uint8 *data, unsigned dlen)
Definition: internal.c:137
static unsigned int_sha1_len(PX_MD *h)
Definition: internal.c:125
pg_cryptohash_ctx * pg_cryptohash_create(pg_cryptohash_type type)
Definition: cryptohash.c:66
static void int_sha1_reset(PX_MD *h)
Definition: internal.c:146
static void int_sha1_finish(PX_MD *h, uint8 *dst)
Definition: internal.c:155

◆ 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::@0 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:66
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::@0 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:66
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::@0 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:66
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)
union int_ctx::@0 ctx
static unsigned int_sha512_len(PX_MD *h)
Definition: internal-sha2.c:86
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:66

◆ int_md5_block_len()

static unsigned int_md5_block_len ( PX_MD h)
static

Definition at line 81 of file internal.c.

References MD5_BLOCK_SIZE.

Referenced by init_md5().

82 {
83  return MD5_BLOCK_SIZE;
84 }
#define MD5_BLOCK_SIZE
Definition: internal.c:45

◆ int_md5_finish()

static void int_md5_finish ( PX_MD h,
uint8 dst 
)
static

Definition at line 105 of file internal.c.

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

Referenced by init_md5().

106 {
108 
109  if (pg_cryptohash_final(ctx, dst, h->result_size(h)) < 0)
110  elog(ERROR, "could not finalize %s context", "MD5");
111 }
int pg_cryptohash_final(pg_cryptohash_ctx *ctx, uint8 *dest, size_t len)
Definition: cryptohash.c:168
void * ptr
Definition: px.h:110
union int_ctx::@0 ctx
#define ERROR
Definition: elog.h:46
union px_digest::@9 p
unsigned(* result_size)(PX_MD *h)
Definition: px.h:100
#define elog(elevel,...)
Definition: elog.h:232

◆ int_md5_free()

static void int_md5_free ( PX_MD h)
static

Definition at line 114 of file internal.c.

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

Referenced by init_md5().

115 {
117 
118  pg_cryptohash_free(ctx);
119  pfree(h);
120 }
void * ptr
Definition: px.h:110
union int_ctx::@0 ctx
void pfree(void *pointer)
Definition: mcxt.c:1169
union px_digest::@9 p
void pg_cryptohash_free(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:216

◆ int_md5_len()

static unsigned int_md5_len ( PX_MD h)
static

Definition at line 75 of file internal.c.

References MD5_DIGEST_LENGTH.

Referenced by init_md5().

76 {
77  return MD5_DIGEST_LENGTH;
78 }
#define MD5_DIGEST_LENGTH
Definition: md5.h:20

◆ int_md5_reset()

static void int_md5_reset ( PX_MD h)
static

Definition at line 96 of file internal.c.

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

Referenced by init_md5().

97 {
99 
100  if (pg_cryptohash_init(ctx) < 0)
101  elog(ERROR, "could not initialize %s context", "MD5");
102 }
void * ptr
Definition: px.h:110
union int_ctx::@0 ctx
#define ERROR
Definition: elog.h:46
union px_digest::@9 p
int pg_cryptohash_init(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:92
#define elog(elevel,...)
Definition: elog.h:232

◆ int_md5_update()

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

Definition at line 87 of file internal.c.

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

Referenced by init_md5().

88 {
90 
91  if (pg_cryptohash_update(ctx, data, dlen) < 0)
92  elog(ERROR, "could not update %s context", "MD5");
93 }
void * ptr
Definition: px.h:110
union int_ctx::@0 ctx
#define ERROR
Definition: elog.h:46
union px_digest::@9 p
int pg_cryptohash_update(pg_cryptohash_ctx *ctx, const uint8 *data, size_t len)
Definition: cryptohash.c:130
#define elog(elevel,...)
Definition: elog.h:232

◆ int_sha1_block_len()

static unsigned int_sha1_block_len ( PX_MD h)
static

Definition at line 131 of file internal.c.

References SHA1_BLOCK_SIZE.

Referenced by init_sha1().

132 {
133  return SHA1_BLOCK_SIZE;
134 }
#define SHA1_BLOCK_SIZE
Definition: internal.c:44

◆ int_sha1_finish()

static void int_sha1_finish ( PX_MD h,
uint8 dst 
)
static

Definition at line 155 of file internal.c.

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

Referenced by init_sha1().

156 {
158 
159  if (pg_cryptohash_final(ctx, dst, h->result_size(h)) < 0)
160  elog(ERROR, "could not finalize %s context", "SHA1");
161 }
int pg_cryptohash_final(pg_cryptohash_ctx *ctx, uint8 *dest, size_t len)
Definition: cryptohash.c:168
void * ptr
Definition: px.h:110
union int_ctx::@0 ctx
#define ERROR
Definition: elog.h:46
union px_digest::@9 p
unsigned(* result_size)(PX_MD *h)
Definition: px.h:100
#define elog(elevel,...)
Definition: elog.h:232

◆ int_sha1_free()

static void int_sha1_free ( PX_MD h)
static

Definition at line 164 of file internal.c.

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

Referenced by init_sha1().

165 {
167 
168  pg_cryptohash_free(ctx);
169  pfree(h);
170 }
void * ptr
Definition: px.h:110
union int_ctx::@0 ctx
void pfree(void *pointer)
Definition: mcxt.c:1169
union px_digest::@9 p
void pg_cryptohash_free(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:216

◆ int_sha1_len()

static unsigned int_sha1_len ( PX_MD h)
static

Definition at line 125 of file internal.c.

References SHA1_DIGEST_LENGTH.

Referenced by init_sha1().

126 {
127  return SHA1_DIGEST_LENGTH;
128 }
#define SHA1_DIGEST_LENGTH
Definition: sha1.h:17

◆ int_sha1_reset()

static void int_sha1_reset ( PX_MD h)
static

Definition at line 146 of file internal.c.

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

Referenced by init_sha1().

147 {
149 
150  if (pg_cryptohash_init(ctx) < 0)
151  elog(ERROR, "could not initialize %s context", "SHA1");
152 }
void * ptr
Definition: px.h:110
union int_ctx::@0 ctx
#define ERROR
Definition: elog.h:46
union px_digest::@9 p
int pg_cryptohash_init(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:92
#define elog(elevel,...)
Definition: elog.h:232

◆ int_sha1_update()

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

Definition at line 137 of file internal.c.

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

Referenced by init_sha1().

138 {
140 
141  if (pg_cryptohash_update(ctx, data, dlen) < 0)
142  elog(ERROR, "could not update %s context", "SHA1");
143 }
void * ptr
Definition: px.h:110
union int_ctx::@0 ctx
#define ERROR
Definition: elog.h:46
union px_digest::@9 p
int pg_cryptohash_update(pg_cryptohash_ctx *ctx, const uint8 *data, size_t len)
Definition: cryptohash.c:130
#define elog(elevel,...)
Definition: elog.h:232

◆ intctx_free()

static void intctx_free ( PX_Cipher c)
static

Definition at line 234 of file internal.c.

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

Referenced by bf_load(), and rj_load().

235 {
236  struct int_ctx *cx = (struct int_ctx *) c->ptr;
237 
238  if (cx)
239  {
240  px_memset(cx, 0, sizeof *cx);
241  pfree(cx);
242  }
243  pfree(c);
244 }
void * ptr
Definition: px.h:150
void pfree(void *pointer)
Definition: mcxt.c:1169
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 566 of file internal.c.

Referenced by pgp_load_cipher(), and px_find_combo().

567 {
568  int i;
569  PX_Cipher *c = NULL;
570 
572 
573  for (i = 0; int_ciphers[i].name; i++)
574  if (strcmp(int_ciphers[i].name, name) == 0)
575  {
576  c = int_ciphers[i].load();
577  break;
578  }
579 
580  if (c == NULL)
581  return PXE_NO_CIPHER;
582 
583  *res = c;
584  return 0;
585 }
static const struct int_cipher int_ciphers[]
Definition: internal.c:524
PX_Cipher *(* load)(void)
Definition: internal.c:520
char * name
Definition: internal.c:519
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:532

◆ px_find_digest()

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

Definition at line 547 of file internal.c.

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

548 {
549  const struct int_digest *p;
550  PX_MD *h;
551 
552  for (p = int_digest_list; p->name; p++)
553  if (pg_strcasecmp(p->name, name) == 0)
554  {
555  h = palloc(sizeof(*h));
556  p->init(h);
557 
558  *res = h;
559 
560  return 0;
561  }
562  return PXE_NO_HASH;
563 }
static const struct int_digest int_digest_list[]
Definition: internal.c:62
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:57
const char * name
Definition: encode.c:515
void * palloc(Size size)
Definition: mcxt.c:1062
void(* init)(PX_MD *h)
Definition: internal.c:58

◆ rj_128_cbc()

static PX_Cipher* rj_128_cbc ( void  )
static

Definition at line 500 of file internal.c.

References MODE_CBC, and rj_load().

501 {
502  return rj_load(MODE_CBC);
503 }
#define MODE_CBC
Definition: internal.c:251
static PX_Cipher * rj_load(int mode)
Definition: internal.c:363

◆ rj_128_ecb()

static PX_Cipher* rj_128_ecb ( void  )
static

Definition at line 494 of file internal.c.

References MODE_ECB, and rj_load().

495 {
496  return rj_load(MODE_ECB);
497 }
#define MODE_ECB
Definition: internal.c:250
static PX_Cipher * rj_load(int mode)
Definition: internal.c:363

◆ rj_block_size()

static unsigned rj_block_size ( PX_Cipher c)
static

Definition at line 254 of file internal.c.

Referenced by rj_load().

255 {
256  return 128 / 8;
257 }

◆ rj_decrypt()

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

Definition at line 331 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().

332 {
333  struct int_ctx *cx = (struct int_ctx *) c->ptr;
334 
335  if (!cx->is_init)
336  if (rj_real_init(cx, 0))
337  return PXE_CIPHER_INIT;
338 
339  if (dlen == 0)
340  return 0;
341 
342  if (dlen & 15)
343  return PXE_NOTBLOCKSIZE;
344 
345  memcpy(res, data, dlen);
346 
347  if (cx->mode == MODE_CBC)
348  {
349  aes_cbc_decrypt(&cx->ctx.rj, cx->iv, res, dlen);
350  memcpy(cx->iv, data + dlen - 16, 16);
351  }
352  else
353  aes_ecb_decrypt(&cx->ctx.rj, res, dlen);
354 
355  return 0;
356 }
union int_ctx::@0 ctx
void aes_cbc_decrypt(rijndael_ctx *ctx, uint8 *iva, uint8 *data, unsigned len)
Definition: rijndael.c:567
void * ptr
Definition: px.h:150
#define MODE_CBC
Definition: internal.c:251
uint8 iv[INT_MAX_IV]
Definition: internal.c:222
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:294
#define PXE_CIPHER_INIT
Definition: px.h:54
#define PXE_NOTBLOCKSIZE
Definition: px.h:50
rijndael_ctx rj
Definition: internal.c:226
int mode
Definition: internal.c:230
int is_init
Definition: internal.c:229

◆ rj_encrypt()

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

Definition at line 301 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().

302 {
303  struct int_ctx *cx = (struct int_ctx *) c->ptr;
304 
305  if (!cx->is_init)
306  {
307  if (rj_real_init(cx, 1))
308  return PXE_CIPHER_INIT;
309  }
310 
311  if (dlen == 0)
312  return 0;
313 
314  if (dlen & 15)
315  return PXE_NOTBLOCKSIZE;
316 
317  memcpy(res, data, dlen);
318 
319  if (cx->mode == MODE_CBC)
320  {
321  aes_cbc_encrypt(&cx->ctx.rj, cx->iv, res, dlen);
322  memcpy(cx->iv, res + dlen - 16, 16);
323  }
324  else
325  aes_ecb_encrypt(&cx->ctx.rj, res, dlen);
326 
327  return 0;
328 }
void aes_cbc_encrypt(rijndael_ctx *ctx, uint8 *iva, uint8 *data, unsigned len)
Definition: rijndael.c:545
union int_ctx::@0 ctx
void * ptr
Definition: px.h:150
#define MODE_CBC
Definition: internal.c:251
uint8 iv[INT_MAX_IV]
Definition: internal.c:222
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:294
#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:226
int mode
Definition: internal.c:230
int is_init
Definition: internal.c:229

◆ rj_init()

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

Definition at line 272 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().

273 {
274  struct int_ctx *cx = (struct int_ctx *) c->ptr;
275 
276  if (klen <= 128 / 8)
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;
282  else
283  return PXE_KEY_TOO_BIG;
284 
285  memcpy(&cx->keybuf, key, klen);
286 
287  if (iv)
288  memcpy(cx->iv, iv, 128 / 8);
289 
290  return 0;
291 }
void * ptr
Definition: px.h:150
uint8 keybuf[INT_MAX_KEY]
Definition: internal.c:221
uint8 iv[INT_MAX_IV]
Definition: internal.c:222
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:228

◆ rj_iv_size()

static unsigned rj_iv_size ( PX_Cipher c)
static

Definition at line 266 of file internal.c.

Referenced by rj_load().

267 {
268  return 128 / 8;
269 }

◆ rj_key_size()

static unsigned rj_key_size ( PX_Cipher c)
static

Definition at line 260 of file internal.c.

Referenced by rj_load().

261 {
262  return 256 / 8;
263 }

◆ rj_load()

static PX_Cipher* rj_load ( int  mode)
static

Definition at line 363 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().

364 {
365  PX_Cipher *c;
366  struct int_ctx *cx;
367 
368  c = palloc0(sizeof *c);
369 
371  c->key_size = rj_key_size;
372  c->iv_size = rj_iv_size;
373  c->init = rj_init;
374  c->encrypt = rj_encrypt;
375  c->decrypt = rj_decrypt;
376  c->free = intctx_free;
377 
378  cx = palloc0(sizeof *cx);
379  cx->mode = mode;
380 
381  c->ptr = cx;
382  return c;
383 }
static PgChecksumMode mode
Definition: pg_checksums.c:61
static void intctx_free(PX_Cipher *c)
Definition: internal.c:234
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:254
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:301
static int rj_init(PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
Definition: internal.c:272
static unsigned rj_key_size(PX_Cipher *c)
Definition: internal.c:260
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:266
int(* encrypt)(PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
Definition: px.h:146
void * palloc0(Size size)
Definition: mcxt.c:1093
static int rj_decrypt(PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
Definition: internal.c:331
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:230

◆ rj_real_init()

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

Definition at line 294 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().

295 {
296  aes_set_key(&cx->ctx.rj, cx->keybuf, cx->keylen * 8, dir);
297  return 0;
298 }
union int_ctx::@0 ctx
uint8 keybuf[INT_MAX_KEY]
Definition: internal.c:221
void aes_set_key(rijndael_ctx *ctx, const uint8 *key, unsigned keybits, int enc)
Definition: rijndael.c:504
unsigned keylen
Definition: internal.c:228
rijndael_ctx rj
Definition: internal.c:226

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 532 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:512
static PX_Cipher * bf_ecb_load(void)
Definition: internal.c:506
static PX_Cipher * rj_128_ecb(void)
Definition: internal.c:494
static PX_Cipher * rj_128_cbc(void)
Definition: internal.c:500

Definition at line 524 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:175
void init_sha256(PX_MD *h)
void init_sha512(PX_MD *h)
static void init_sha1(PX_MD *h)
Definition: internal.c:194

Definition at line 62 of file internal.c.