Age Owner TLA Line data Source code
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 :
36 : int
5050 bruce 37 CBC 172 : pgp_mpi_alloc(int bits, PGP_MPI **mpi)
38 : {
39 : PGP_MPI *n;
6385 40 172 : int len = (bits + 7) / 8;
41 :
6482 42 172 : if (bits < 0 || bits > 0xFFFF)
43 : {
6482 bruce 44 UBC 0 : px_debug("pgp_mpi_alloc: unreasonable request: bits=%d", bits);
45 0 : return PXE_PGP_CORRUPT_DATA;
46 : }
926 michael 47 CBC 172 : n = palloc(sizeof(*n) + len);
6482 bruce 48 172 : n->bits = bits;
49 172 : n->bytes = len;
6385 50 172 : n->data = (uint8 *) (n) + sizeof(*n);
6482 51 172 : *mpi = n;
52 172 : return 0;
53 : }
54 :
55 : int
5050 56 6 : pgp_mpi_create(uint8 *data, int bits, PGP_MPI **mpi)
57 : {
58 : int res;
59 : PGP_MPI *n;
60 :
6482 61 6 : res = pgp_mpi_alloc(bits, &n);
62 6 : if (res < 0)
6482 bruce 63 UBC 0 : return res;
6482 bruce 64 CBC 6 : memcpy(n->data, data, n->bytes);
65 6 : *mpi = n;
66 6 : return 0;
67 : }
68 :
69 : int
5050 70 198 : pgp_mpi_free(PGP_MPI *mpi)
71 : {
6448 72 198 : if (mpi == NULL)
73 29 : return 0;
3279 74 169 : px_memset(mpi, 0, sizeof(*mpi) + mpi->bytes);
926 michael 75 169 : pfree(mpi);
6482 bruce 76 169 : return 0;
77 : }
78 :
79 : int
5050 80 142 : pgp_mpi_read(PullFilter *src, PGP_MPI **mpi)
81 : {
82 : int res;
83 : uint8 hdr[2];
84 : int bits;
85 : PGP_MPI *n;
86 :
6482 87 142 : res = pullf_read_fixed(src, 2, hdr);
88 142 : if (res < 0)
6482 bruce 89 UBC 0 : return res;
6385 bruce 90 CBC 142 : bits = ((unsigned) hdr[0] << 8) + hdr[1];
91 :
6482 92 142 : res = pgp_mpi_alloc(bits, &n);
93 142 : if (res < 0)
6482 bruce 94 UBC 0 : return res;
95 :
6482 bruce 96 CBC 142 : res = pullf_read_fixed(src, n->bytes, n->data);
97 142 : if (res < 0)
98 2 : pgp_mpi_free(n);
99 : else
100 140 : *mpi = n;
101 142 : return res;
102 : }
103 :
104 : int
5050 105 11 : pgp_mpi_write(PushFilter *dst, PGP_MPI *n)
106 : {
107 : int res;
108 : uint8 buf[2];
109 :
6482 110 11 : buf[0] = n->bits >> 8;
111 11 : buf[1] = n->bits & 0xFF;
112 11 : res = pushf_write(dst, buf, 2);
113 11 : if (res >= 0)
114 11 : res = pushf_write(dst, n->data, n->bytes);
115 11 : return res;
116 : }
117 :
118 : int
5050 119 96 : pgp_mpi_hash(PX_MD *md, PGP_MPI *n)
120 : {
121 : uint8 buf[2];
122 :
6482 123 96 : buf[0] = n->bits >> 8;
124 96 : buf[1] = n->bits & 0xFF;
125 96 : px_md_update(md, buf, 2);
126 96 : px_md_update(md, n->data, n->bytes);
127 :
128 96 : return 0;
129 : }
130 :
131 : unsigned
5050 132 21 : pgp_mpi_cksum(unsigned cksum, PGP_MPI *n)
133 : {
134 : int i;
135 :
6482 136 21 : cksum += n->bits >> 8;
137 21 : cksum += n->bits & 0xFF;
138 2310 : for (i = 0; i < n->bytes; i++)
139 2289 : cksum += n->data[i];
140 :
6448 141 21 : return cksum & 0xFFFF;
142 : }
|