PostgreSQL Source Code  git master
pgp-cfb.c File Reference
#include "postgres.h"
#include "pgp.h"
#include "px.h"
Include dependency graph for pgp-cfb.c:

Go to the source code of this file.

Data Structures

struct  PGP_CFB
 

Typedefs

typedef int(* mix_data_t) (PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
 

Functions

int pgp_cfb_create (PGP_CFB **ctx_p, int algo, const uint8 *key, int key_len, int resync, uint8 *iv)
 
void pgp_cfb_free (PGP_CFB *ctx)
 
static int mix_encrypt_normal (PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
 
static int mix_decrypt_normal (PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
 
static int mix_encrypt_resync (PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
 
static int mix_decrypt_resync (PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
 
static int cfb_process (PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst, mix_data_t mix_data)
 
int pgp_cfb_encrypt (PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
 
int pgp_cfb_decrypt (PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
 

Typedef Documentation

◆ mix_data_t

typedef int(* mix_data_t) (PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)

Definition at line 37 of file pgp-cfb.c.

Function Documentation

◆ cfb_process()

static int cfb_process ( PGP_CFB ctx,
const uint8 data,
int  len,
uint8 dst,
mix_data_t  mix_data 
)
static

Definition at line 197 of file pgp-cfb.c.

References PGP_CFB::block_no, PGP_CFB::block_size, PGP_CFB::ciph, PGP_CFB::encbuf, PGP_CFB::fr, PGP_CFB::fre, PGP_CFB::pos, and px_cipher_encrypt.

Referenced by pgp_cfb_decrypt(), and pgp_cfb_encrypt().

199 {
200  int n;
201  int res;
202 
203  while (len > 0 && ctx->pos > 0)
204  {
205  n = ctx->block_size - ctx->pos;
206  if (len < n)
207  n = len;
208 
209  n = mix_data(ctx, data, n, dst);
210  data += n;
211  dst += n;
212  len -= n;
213 
214  if (ctx->pos == ctx->block_size)
215  {
216  memcpy(ctx->fr, ctx->encbuf, ctx->block_size);
217  ctx->pos = 0;
218  }
219  }
220 
221  while (len > 0)
222  {
223  px_cipher_encrypt(ctx->ciph, ctx->fr, ctx->block_size, ctx->fre);
224  if (ctx->block_no < 5)
225  ctx->block_no++;
226 
227  n = ctx->block_size;
228  if (len < n)
229  n = len;
230 
231  res = mix_data(ctx, data, n, dst);
232  data += res;
233  dst += res;
234  len -= res;
235 
236  if (ctx->pos == ctx->block_size)
237  {
238  memcpy(ctx->fr, ctx->encbuf, ctx->block_size);
239  ctx->pos = 0;
240  }
241  }
242  return 0;
243 }
uint8 encbuf[PGP_MAX_BLOCK]
Definition: pgp-cfb.c:48
int block_no
Definition: pgp-cfb.c:44
PX_Cipher * ciph
Definition: pgp-cfb.c:41
uint8 fr[PGP_MAX_BLOCK]
Definition: pgp-cfb.c:46
int block_size
Definition: pgp-cfb.c:42
uint8 fre[PGP_MAX_BLOCK]
Definition: pgp-cfb.c:47
int pos
Definition: pgp-cfb.c:43
#define px_cipher_encrypt(c, data, dlen, res)
Definition: px.h:210

◆ mix_decrypt_normal()

static int mix_decrypt_normal ( PGP_CFB ctx,
const uint8 data,
int  len,
uint8 dst 
)
static

Definition at line 105 of file pgp-cfb.c.

References PGP_CFB::encbuf, PGP_CFB::fre, i, and PGP_CFB::pos.

Referenced by pgp_cfb_decrypt().

106 {
107  int i;
108 
109  for (i = ctx->pos; i < ctx->pos + len; i++)
110  {
111  ctx->encbuf[i] = *data++;
112  *dst++ = ctx->fre[i] ^ ctx->encbuf[i];
113  }
114  ctx->pos += len;
115  return len;
116 }
uint8 encbuf[PGP_MAX_BLOCK]
Definition: pgp-cfb.c:48
uint8 fre[PGP_MAX_BLOCK]
Definition: pgp-cfb.c:47
int pos
Definition: pgp-cfb.c:43
int i

◆ mix_decrypt_resync()

static int mix_decrypt_resync ( PGP_CFB ctx,
const uint8 data,
int  len,
uint8 dst 
)
static

Definition at line 157 of file pgp-cfb.c.

References PGP_CFB::block_no, PGP_CFB::block_size, PGP_CFB::encbuf, PGP_CFB::fr, PGP_CFB::fre, i, and PGP_CFB::pos.

Referenced by pgp_cfb_decrypt().

158 {
159  int i,
160  n;
161 
162  /* block #2 is 2 bytes long */
163  if (ctx->block_no == 2)
164  {
165  n = 2 - ctx->pos;
166  if (len < n)
167  n = len;
168  for (i = ctx->pos; i < ctx->pos + n; i++)
169  {
170  ctx->encbuf[i] = *data++;
171  *dst++ = ctx->fre[i] ^ ctx->encbuf[i];
172  }
173  ctx->pos += n;
174  len -= n;
175 
176  if (ctx->pos == 2)
177  {
178  memcpy(ctx->fr, ctx->encbuf + 2, ctx->block_size - 2);
179  memcpy(ctx->fr + ctx->block_size - 2, ctx->encbuf, 2);
180  ctx->pos = 0;
181  return n;
182  }
183  }
184  for (i = ctx->pos; i < ctx->pos + len; i++)
185  {
186  ctx->encbuf[i] = *data++;
187  *dst++ = ctx->fre[i] ^ ctx->encbuf[i];
188  }
189  ctx->pos += len;
190  return len;
191 }
uint8 encbuf[PGP_MAX_BLOCK]
Definition: pgp-cfb.c:48
int block_no
Definition: pgp-cfb.c:44
uint8 fr[PGP_MAX_BLOCK]
Definition: pgp-cfb.c:46
int block_size
Definition: pgp-cfb.c:42
uint8 fre[PGP_MAX_BLOCK]
Definition: pgp-cfb.c:47
int pos
Definition: pgp-cfb.c:43
int i

◆ mix_encrypt_normal()

static int mix_encrypt_normal ( PGP_CFB ctx,
const uint8 data,
int  len,
uint8 dst 
)
static

Definition at line 94 of file pgp-cfb.c.

References PGP_CFB::encbuf, PGP_CFB::fre, i, and PGP_CFB::pos.

Referenced by pgp_cfb_encrypt().

95 {
96  int i;
97 
98  for (i = ctx->pos; i < ctx->pos + len; i++)
99  *dst++ = ctx->encbuf[i] = ctx->fre[i] ^ (*data++);
100  ctx->pos += len;
101  return len;
102 }
uint8 encbuf[PGP_MAX_BLOCK]
Definition: pgp-cfb.c:48
uint8 fre[PGP_MAX_BLOCK]
Definition: pgp-cfb.c:47
int pos
Definition: pgp-cfb.c:43
int i

◆ mix_encrypt_resync()

static int mix_encrypt_resync ( PGP_CFB ctx,
const uint8 data,
int  len,
uint8 dst 
)
static

Definition at line 125 of file pgp-cfb.c.

References PGP_CFB::block_no, PGP_CFB::block_size, PGP_CFB::encbuf, PGP_CFB::fr, PGP_CFB::fre, i, and PGP_CFB::pos.

Referenced by pgp_cfb_encrypt().

126 {
127  int i,
128  n;
129 
130  /* block #2 is 2 bytes long */
131  if (ctx->block_no == 2)
132  {
133  n = 2 - ctx->pos;
134  if (len < n)
135  n = len;
136  for (i = ctx->pos; i < ctx->pos + n; i++)
137  *dst++ = ctx->encbuf[i] = ctx->fre[i] ^ (*data++);
138 
139  ctx->pos += n;
140  len -= n;
141 
142  if (ctx->pos == 2)
143  {
144  memcpy(ctx->fr, ctx->encbuf + 2, ctx->block_size - 2);
145  memcpy(ctx->fr + ctx->block_size - 2, ctx->encbuf, 2);
146  ctx->pos = 0;
147  return n;
148  }
149  }
150  for (i = ctx->pos; i < ctx->pos + len; i++)
151  *dst++ = ctx->encbuf[i] = ctx->fre[i] ^ (*data++);
152  ctx->pos += len;
153  return len;
154 }
uint8 encbuf[PGP_MAX_BLOCK]
Definition: pgp-cfb.c:48
int block_no
Definition: pgp-cfb.c:44
uint8 fr[PGP_MAX_BLOCK]
Definition: pgp-cfb.c:46
int block_size
Definition: pgp-cfb.c:42
uint8 fre[PGP_MAX_BLOCK]
Definition: pgp-cfb.c:47
int pos
Definition: pgp-cfb.c:43
int i

◆ pgp_cfb_create()

int pgp_cfb_create ( PGP_CFB **  ctx_p,
int  algo,
const uint8 key,
int  key_len,
int  resync,
uint8 iv 
)

Definition at line 52 of file pgp-cfb.c.

References PGP_CFB::block_size, PGP_CFB::ciph, PGP_CFB::fr, palloc0(), pgp_load_cipher(), px_cipher_block_size, px_cipher_free, px_cipher_init, and PGP_CFB::resync.

Referenced by decrypt_key(), encrypt_init(), parse_symenc_data(), parse_symenc_mdc_data(), process_secret_key(), and symencrypt_sesskey().

54 {
55  int res;
56  PX_Cipher *ciph;
57  PGP_CFB *ctx;
58 
59  res = pgp_load_cipher(algo, &ciph);
60  if (res < 0)
61  return res;
62 
63  res = px_cipher_init(ciph, key, key_len, NULL);
64  if (res < 0)
65  {
66  px_cipher_free(ciph);
67  return res;
68  }
69 
70  ctx = palloc0(sizeof(*ctx));
71  ctx->ciph = ciph;
72  ctx->block_size = px_cipher_block_size(ciph);
73  ctx->resync = resync;
74 
75  if (iv)
76  memcpy(ctx->fr, iv, ctx->block_size);
77 
78  *ctx_p = ctx;
79  return 0;
80 }
#define px_cipher_init(c, k, klen, iv)
Definition: px.h:209
#define px_cipher_free(c)
Definition: px.h:214
PX_Cipher * ciph
Definition: pgp-cfb.c:41
uint8 fr[PGP_MAX_BLOCK]
Definition: pgp-cfb.c:46
int pgp_load_cipher(int code, PX_Cipher **res)
Definition: pgp.c:167
int resync
Definition: pgp-cfb.c:45
int block_size
Definition: pgp-cfb.c:42
#define px_cipher_block_size(c)
Definition: px.h:207
void * palloc0(Size size)
Definition: mcxt.c:981
Definition: px.h:139
const struct ossl_cipher * ciph
Definition: openssl.c:738

◆ pgp_cfb_decrypt()

int pgp_cfb_decrypt ( PGP_CFB ctx,
const uint8 data,
int  len,
uint8 dst 
)

Definition at line 258 of file pgp-cfb.c.

References cfb_process(), mix, mix_decrypt_normal(), mix_decrypt_resync(), and PGP_CFB::resync.

Referenced by decrypt_key(), and decrypt_read().

259 {
261 
262  return cfb_process(ctx, data, len, dst, mix);
263 }
#define mix(a, b, c)
Definition: hashfn.c:82
static int cfb_process(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst, mix_data_t mix_data)
Definition: pgp-cfb.c:197
static int mix_decrypt_normal(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
Definition: pgp-cfb.c:105
int resync
Definition: pgp-cfb.c:45
static int mix_decrypt_resync(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
Definition: pgp-cfb.c:157
int(* mix_data_t)(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
Definition: pgp-cfb.c:37

◆ pgp_cfb_encrypt()

int pgp_cfb_encrypt ( PGP_CFB ctx,
const uint8 data,
int  len,
uint8 dst 
)

Definition at line 250 of file pgp-cfb.c.

References cfb_process(), mix, mix_encrypt_normal(), mix_encrypt_resync(), and PGP_CFB::resync.

Referenced by encrypt_process(), and symencrypt_sesskey().

251 {
253 
254  return cfb_process(ctx, data, len, dst, mix);
255 }
#define mix(a, b, c)
Definition: hashfn.c:82
static int cfb_process(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst, mix_data_t mix_data)
Definition: pgp-cfb.c:197
int resync
Definition: pgp-cfb.c:45
static int mix_encrypt_normal(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
Definition: pgp-cfb.c:94
static int mix_encrypt_resync(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
Definition: pgp-cfb.c:125
int(* mix_data_t)(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
Definition: pgp-cfb.c:37

◆ pgp_cfb_free()

void pgp_cfb_free ( PGP_CFB ctx)

Definition at line 83 of file pgp-cfb.c.

References PGP_CFB::ciph, pfree(), px_cipher_free, and px_memset().

Referenced by decrypt_key(), encrypt_free(), parse_symenc_data(), parse_symenc_mdc_data(), process_secret_key(), and symencrypt_sesskey().

84 {
85  px_cipher_free(ctx->ciph);
86  px_memset(ctx, 0, sizeof(*ctx));
87  pfree(ctx);
88 }
#define px_cipher_free(c)
Definition: px.h:214
PX_Cipher * ciph
Definition: pgp-cfb.c:41
void pfree(void *pointer)
Definition: mcxt.c:1057
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:127