PostgreSQL Source Code  git master
pgp-cfb.c File Reference
#include "postgres.h"
#include "px.h"
#include "pgp.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 198 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().

200 {
201  int n;
202  int res;
203 
204  while (len > 0 && ctx->pos > 0)
205  {
206  n = ctx->block_size - ctx->pos;
207  if (len < n)
208  n = len;
209 
210  n = mix_data(ctx, data, n, dst);
211  data += n;
212  dst += n;
213  len -= n;
214 
215  if (ctx->pos == ctx->block_size)
216  {
217  memcpy(ctx->fr, ctx->encbuf, ctx->block_size);
218  ctx->pos = 0;
219  }
220  }
221 
222  while (len > 0)
223  {
224  px_cipher_encrypt(ctx->ciph, ctx->fr, ctx->block_size, ctx->fre);
225  if (ctx->block_no < 5)
226  ctx->block_no++;
227 
228  n = ctx->block_size;
229  if (len < n)
230  n = len;
231 
232  res = mix_data(ctx, data, n, dst);
233  data += res;
234  dst += res;
235  len -= res;
236 
237  if (ctx->pos == ctx->block_size)
238  {
239  memcpy(ctx->fr, ctx->encbuf, ctx->block_size);
240  ctx->pos = 0;
241  }
242  }
243  return 0;
244 }
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:225

◆ mix_decrypt_normal()

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

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

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

Referenced by pgp_cfb_decrypt().

107 {
108  int i;
109 
110  for (i = ctx->pos; i < ctx->pos + len; i++)
111  {
112  ctx->encbuf[i] = *data++;
113  *dst++ = ctx->fre[i] ^ ctx->encbuf[i];
114  }
115  ctx->pos += len;
116  return len;
117 }
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 158 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().

159 {
160  int i,
161  n;
162 
163  /* block #2 is 2 bytes long */
164  if (ctx->block_no == 2)
165  {
166  n = 2 - ctx->pos;
167  if (len < n)
168  n = len;
169  for (i = ctx->pos; i < ctx->pos + n; i++)
170  {
171  ctx->encbuf[i] = *data++;
172  *dst++ = ctx->fre[i] ^ ctx->encbuf[i];
173  }
174  ctx->pos += n;
175  len -= n;
176 
177  if (ctx->pos == 2)
178  {
179  memcpy(ctx->fr, ctx->encbuf + 2, ctx->block_size - 2);
180  memcpy(ctx->fr + ctx->block_size - 2, ctx->encbuf, 2);
181  ctx->pos = 0;
182  return n;
183  }
184  }
185  for (i = ctx->pos; i < ctx->pos + len; i++)
186  {
187  ctx->encbuf[i] = *data++;
188  *dst++ = ctx->fre[i] ^ ctx->encbuf[i];
189  }
190  ctx->pos += len;
191  return len;
192 }
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 95 of file pgp-cfb.c.

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

Referenced by pgp_cfb_encrypt().

96 {
97  int i;
98 
99  for (i = ctx->pos; i < ctx->pos + len; i++)
100  *dst++ = ctx->encbuf[i] = ctx->fre[i] ^ (*data++);
101  ctx->pos += len;
102  return len;
103 }
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 126 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().

127 {
128  int i,
129  n;
130 
131  /* block #2 is 2 bytes long */
132  if (ctx->block_no == 2)
133  {
134  n = 2 - ctx->pos;
135  if (len < n)
136  n = len;
137  for (i = ctx->pos; i < ctx->pos + n; i++)
138  *dst++ = ctx->encbuf[i] = ctx->fre[i] ^ (*data++);
139 
140  ctx->pos += n;
141  len -= n;
142 
143  if (ctx->pos == 2)
144  {
145  memcpy(ctx->fr, ctx->encbuf + 2, ctx->block_size - 2);
146  memcpy(ctx->fr + ctx->block_size - 2, ctx->encbuf, 2);
147  ctx->pos = 0;
148  return n;
149  }
150  }
151  for (i = ctx->pos; i < ctx->pos + len; i++)
152  *dst++ = ctx->encbuf[i] = ctx->fre[i] ^ (*data++);
153  ctx->pos += len;
154  return len;
155 }
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, pgp_load_cipher(), px_alloc, 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 = px_alloc(sizeof(*ctx));
71  memset(ctx, 0, sizeof(*ctx));
72  ctx->ciph = ciph;
73  ctx->block_size = px_cipher_block_size(ciph);
74  ctx->resync = resync;
75 
76  if (iv)
77  memcpy(ctx->fr, iv, ctx->block_size);
78 
79  *ctx_p = ctx;
80  return 0;
81 }
#define px_cipher_init(c, k, klen, iv)
Definition: px.h:224
#define px_cipher_free(c)
Definition: px.h:229
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:168
int resync
Definition: pgp-cfb.c:45
int block_size
Definition: pgp-cfb.c:42
#define px_cipher_block_size(c)
Definition: px.h:222
Definition: px.h:154
#define px_alloc(s)
Definition: px.h:44
const struct ossl_cipher * ciph
Definition: openssl.c:727

◆ pgp_cfb_decrypt()

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

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

260 {
262 
263  return cfb_process(ctx, data, len, dst, mix);
264 }
static int cfb_process(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst, mix_data_t mix_data)
Definition: pgp-cfb.c:198
static int mix_decrypt_normal(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
Definition: pgp-cfb.c:106
int resync
Definition: pgp-cfb.c:45
#define mix(a, b, c)
Definition: hashfunc.c:364
static int mix_decrypt_resync(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
Definition: pgp-cfb.c:158
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 251 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().

252 {
254 
255  return cfb_process(ctx, data, len, dst, mix);
256 }
static int cfb_process(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst, mix_data_t mix_data)
Definition: pgp-cfb.c:198
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:95
#define mix(a, b, c)
Definition: hashfunc.c:364
static int mix_encrypt_resync(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
Definition: pgp-cfb.c:126
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 84 of file pgp-cfb.c.

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

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

85 {
86  px_cipher_free(ctx->ciph);
87  px_memset(ctx, 0, sizeof(*ctx));
88  px_free(ctx);
89 }
#define px_free(p)
Definition: px.h:46
#define px_cipher_free(c)
Definition: px.h:229
PX_Cipher * ciph
Definition: pgp-cfb.c:41
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:134