Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include "postgres.h"
00032
00033 #include "px.h"
00034 #include "mbuf.h"
00035 #include "pgp.h"
00036
00037 int
00038 pgp_mpi_alloc(int bits, PGP_MPI **mpi)
00039 {
00040 PGP_MPI *n;
00041 int len = (bits + 7) / 8;
00042
00043 if (bits < 0 || bits > 0xFFFF)
00044 {
00045 px_debug("pgp_mpi_alloc: unreasonable request: bits=%d", bits);
00046 return PXE_PGP_CORRUPT_DATA;
00047 }
00048 n = px_alloc(sizeof(*n) + len);
00049 n->bits = bits;
00050 n->bytes = len;
00051 n->data = (uint8 *) (n) + sizeof(*n);
00052 *mpi = n;
00053 return 0;
00054 }
00055
00056 int
00057 pgp_mpi_create(uint8 *data, int bits, PGP_MPI **mpi)
00058 {
00059 int res;
00060 PGP_MPI *n;
00061
00062 res = pgp_mpi_alloc(bits, &n);
00063 if (res < 0)
00064 return res;
00065 memcpy(n->data, data, n->bytes);
00066 *mpi = n;
00067 return 0;
00068 }
00069
00070 int
00071 pgp_mpi_free(PGP_MPI *mpi)
00072 {
00073 if (mpi == NULL)
00074 return 0;
00075 memset(mpi, 0, sizeof(*mpi) + mpi->bytes);
00076 px_free(mpi);
00077 return 0;
00078 }
00079
00080 int
00081 pgp_mpi_read(PullFilter *src, PGP_MPI **mpi)
00082 {
00083 int res;
00084 uint8 hdr[2];
00085 int bits;
00086 PGP_MPI *n;
00087
00088 res = pullf_read_fixed(src, 2, hdr);
00089 if (res < 0)
00090 return res;
00091 bits = ((unsigned) hdr[0] << 8) + hdr[1];
00092
00093 res = pgp_mpi_alloc(bits, &n);
00094 if (res < 0)
00095 return res;
00096
00097 res = pullf_read_fixed(src, n->bytes, n->data);
00098 if (res < 0)
00099 pgp_mpi_free(n);
00100 else
00101 *mpi = n;
00102 return res;
00103 }
00104
00105 int
00106 pgp_mpi_write(PushFilter *dst, PGP_MPI *n)
00107 {
00108 int res;
00109 uint8 buf[2];
00110
00111 buf[0] = n->bits >> 8;
00112 buf[1] = n->bits & 0xFF;
00113 res = pushf_write(dst, buf, 2);
00114 if (res >= 0)
00115 res = pushf_write(dst, n->data, n->bytes);
00116 return res;
00117 }
00118
00119 int
00120 pgp_mpi_hash(PX_MD *md, PGP_MPI *n)
00121 {
00122 uint8 buf[2];
00123
00124 buf[0] = n->bits >> 8;
00125 buf[1] = n->bits & 0xFF;
00126 px_md_update(md, buf, 2);
00127 px_md_update(md, n->data, n->bytes);
00128
00129 return 0;
00130 }
00131
00132 unsigned
00133 pgp_mpi_cksum(unsigned cksum, PGP_MPI *n)
00134 {
00135 int i;
00136
00137 cksum += n->bits >> 8;
00138 cksum += n->bits & 0xFF;
00139 for (i = 0; i < n->bytes; i++)
00140 cksum += n->data[i];
00141
00142 return cksum & 0xFFFF;
00143 }