PostgreSQL Source Code git master
pgp-mpi.c
Go to the documentation of this file.
1/*
2 * pgp-mpi.c
3 * OpenPGP MPI helper functions.
4 *
5 * Copyright (c) 2005 Marko Kreen
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * contrib/pgcrypto/pgp-mpi.c
30 */
31#include "postgres.h"
32
33#include "pgp.h"
34#include "px.h"
35
36int
37pgp_mpi_alloc(int bits, PGP_MPI **mpi)
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);
46 }
47 n = palloc(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}
54
55int
56pgp_mpi_create(uint8 *data, int bits, PGP_MPI **mpi)
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}
68
69int
71{
72 if (mpi == NULL)
73 return 0;
74 px_memset(mpi, 0, sizeof(*mpi) + mpi->bytes);
75 pfree(mpi);
76 return 0;
77}
78
79int
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}
103
104int
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}
117
118int
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}
130
131unsigned
132pgp_mpi_cksum(unsigned cksum, PGP_MPI *n)
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_t uint8
Definition: c.h:486
int i
Definition: isn.c:72
int pullf_read_fixed(PullFilter *src, int len, uint8 *dst)
Definition: mbuf.c:301
int pushf_write(PushFilter *mp, const uint8 *data, int len)
Definition: mbuf.c:439
void pfree(void *pointer)
Definition: mcxt.c:1521
void * palloc(Size size)
Definition: mcxt.c:1317
const void size_t len
const void * data
static char * buf
Definition: pg_test_fsync.c:72
int pgp_mpi_create(uint8 *data, int bits, PGP_MPI **mpi)
Definition: pgp-mpi.c:56
int pgp_mpi_alloc(int bits, PGP_MPI **mpi)
Definition: pgp-mpi.c:37
int pgp_mpi_free(PGP_MPI *mpi)
Definition: pgp-mpi.c:70
int pgp_mpi_write(PushFilter *dst, PGP_MPI *n)
Definition: pgp-mpi.c:105
int pgp_mpi_hash(PX_MD *md, PGP_MPI *n)
Definition: pgp-mpi.c:119
int pgp_mpi_read(PullFilter *src, PGP_MPI **mpi)
Definition: pgp-mpi.c:80
unsigned pgp_mpi_cksum(unsigned cksum, PGP_MPI *n)
Definition: pgp-mpi.c:132
void px_debug(const char *fmt,...)
Definition: px.c:149
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:123
#define PXE_PGP_CORRUPT_DATA
Definition: px.h:67
#define px_md_update(md, data, dlen)
Definition: px.h:205
Definition: pgp.h:180
int bits
Definition: pgp.h:182
int bytes
Definition: pgp.h:183
uint8 * data
Definition: pgp.h:181
Definition: px.h:108