PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pgp-mpi.c File Reference
#include "postgres.h"
#include "px.h"
#include "pgp.h"
Include dependency graph for pgp-mpi.c:

Go to the source code of this file.

Functions

int pgp_mpi_alloc (int bits, PGP_MPI **mpi)
 
int pgp_mpi_create (uint8 *data, int bits, PGP_MPI **mpi)
 
int pgp_mpi_free (PGP_MPI *mpi)
 
int pgp_mpi_read (PullFilter *src, PGP_MPI **mpi)
 
int pgp_mpi_write (PushFilter *dst, PGP_MPI *n)
 
int pgp_mpi_hash (PX_MD *md, PGP_MPI *n)
 
unsigned pgp_mpi_cksum (unsigned cksum, PGP_MPI *n)
 

Function Documentation

int pgp_mpi_alloc ( int  bits,
PGP_MPI **  mpi 
)

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

References PGP_MPI::bits, PGP_MPI::bytes, PGP_MPI::data, px_alloc, px_debug(), and PXE_PGP_CORRUPT_DATA.

Referenced by bn_to_mpi(), pgp_mpi_create(), and pgp_mpi_read().

38 {
39  PGP_MPI *n;
40  int len = (bits + 7) / 8;
41 
42  if (bits < 0 || bits > 0xFFFF)
43  {
44  px_debug("pgp_mpi_alloc: unreasonable request: bits=%d", bits);
45  return PXE_PGP_CORRUPT_DATA;
46  }
47  n = px_alloc(sizeof(*n) + len);
48  n->bits = bits;
49  n->bytes = len;
50  n->data = (uint8 *) (n) + sizeof(*n);
51  *mpi = n;
52  return 0;
53 }
#define PXE_PGP_CORRUPT_DATA
Definition: px.h:81
uint8 * data
Definition: pgp.h:181
unsigned char uint8
Definition: c.h:266
int bytes
Definition: pgp.h:183
Definition: pgp.h:179
void px_debug(const char *fmt,...)
Definition: px.c:160
#define px_alloc(s)
Definition: px.h:44
int bits
Definition: pgp.h:182
unsigned pgp_mpi_cksum ( unsigned  cksum,
PGP_MPI n 
)

Definition at line 132 of file pgp-mpi.c.

References PGP_MPI::bits, PGP_MPI::bytes, PGP_MPI::data, and i.

Referenced by check_key_cksum().

133 {
134  int i;
135 
136  cksum += n->bits >> 8;
137  cksum += n->bits & 0xFF;
138  for (i = 0; i < n->bytes; i++)
139  cksum += n->data[i];
140 
141  return cksum & 0xFFFF;
142 }
uint8 * data
Definition: pgp.h:181
int bytes
Definition: pgp.h:183
int i
int bits
Definition: pgp.h:182
int pgp_mpi_create ( uint8 data,
int  bits,
PGP_MPI **  mpi 
)

Definition at line 56 of file pgp-mpi.c.

References PGP_MPI::bytes, PGP_MPI::data, and pgp_mpi_alloc().

Referenced by create_secmsg().

57 {
58  int res;
59  PGP_MPI *n;
60 
61  res = pgp_mpi_alloc(bits, &n);
62  if (res < 0)
63  return res;
64  memcpy(n->data, data, n->bytes);
65  *mpi = n;
66  return 0;
67 }
uint8 * data
Definition: pgp.h:181
int bytes
Definition: pgp.h:183
int pgp_mpi_alloc(int bits, PGP_MPI **mpi)
Definition: pgp-mpi.c:37
Definition: pgp.h:179
int pgp_mpi_free ( PGP_MPI mpi)

Definition at line 70 of file pgp-mpi.c.

References PGP_MPI::bytes, NULL, px_free, and px_memset().

Referenced by bn_to_mpi(), decrypt_elgamal(), decrypt_rsa(), encrypt_and_write_elgamal(), encrypt_and_write_rsa(), pgp_key_free(), pgp_mpi_read(), and pgp_parse_pubenc_sesskey().

71 {
72  if (mpi == NULL)
73  return 0;
74  px_memset(mpi, 0, sizeof(*mpi) + mpi->bytes);
75  px_free(mpi);
76  return 0;
77 }
#define px_free(p)
Definition: px.h:46
int bytes
Definition: pgp.h:183
#define NULL
Definition: c.h:229
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:134
int pgp_mpi_hash ( PX_MD md,
PGP_MPI n 
)

Definition at line 119 of file pgp-mpi.c.

References PGP_MPI::bits, buf, PGP_MPI::bytes, PGP_MPI::data, and px_md_update.

Referenced by calc_key_id(), and check_key_sha1().

120 {
121  uint8 buf[2];
122 
123  buf[0] = n->bits >> 8;
124  buf[1] = n->bits & 0xFF;
125  px_md_update(md, buf, 2);
126  px_md_update(md, n->data, n->bytes);
127 
128  return 0;
129 }
uint8 * data
Definition: pgp.h:181
#define px_md_update(md, data, dlen)
Definition: px.h:208
unsigned char uint8
Definition: c.h:266
int bytes
Definition: pgp.h:183
static char * buf
Definition: pg_test_fsync.c:66
int bits
Definition: pgp.h:182
int pgp_mpi_read ( PullFilter src,
PGP_MPI **  mpi 
)

Definition at line 80 of file pgp-mpi.c.

References PGP_MPI::bytes, PGP_MPI::data, pgp_mpi_alloc(), pgp_mpi_free(), and pullf_read_fixed().

Referenced by _pgp_read_public_key(), decrypt_elgamal(), decrypt_rsa(), and process_secret_key().

81 {
82  int res;
83  uint8 hdr[2];
84  int bits;
85  PGP_MPI *n;
86 
87  res = pullf_read_fixed(src, 2, hdr);
88  if (res < 0)
89  return res;
90  bits = ((unsigned) hdr[0] << 8) + hdr[1];
91 
92  res = pgp_mpi_alloc(bits, &n);
93  if (res < 0)
94  return res;
95 
96  res = pullf_read_fixed(src, n->bytes, n->data);
97  if (res < 0)
98  pgp_mpi_free(n);
99  else
100  *mpi = n;
101  return res;
102 }
uint8 * data
Definition: pgp.h:181
int pullf_read_fixed(PullFilter *src, int len, uint8 *dst)
Definition: mbuf.c:317
unsigned char uint8
Definition: c.h:266
int bytes
Definition: pgp.h:183
int pgp_mpi_alloc(int bits, PGP_MPI **mpi)
Definition: pgp-mpi.c:37
Definition: pgp.h:179
int pgp_mpi_free(PGP_MPI *mpi)
Definition: pgp-mpi.c:70
int pgp_mpi_write ( PushFilter dst,
PGP_MPI n 
)

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

References PGP_MPI::bits, buf, PGP_MPI::bytes, PGP_MPI::data, and pushf_write().

Referenced by encrypt_and_write_elgamal(), and encrypt_and_write_rsa().

106 {
107  int res;
108  uint8 buf[2];
109 
110  buf[0] = n->bits >> 8;
111  buf[1] = n->bits & 0xFF;
112  res = pushf_write(dst, buf, 2);
113  if (res >= 0)
114  res = pushf_write(dst, n->data, n->bytes);
115  return res;
116 }
uint8 * data
Definition: pgp.h:181
unsigned char uint8
Definition: c.h:266
int bytes
Definition: pgp.h:183
static char * buf
Definition: pg_test_fsync.c:66
int pushf_write(PushFilter *mp, const uint8 *data, int len)
Definition: mbuf.c:456
int bits
Definition: pgp.h:182