- 6A4860EA723098A2E2BA703D8748F6D26C22D1AE72647A5CCCA67AB93976390B0D992BA870796BE3DA3B736B1A11B36F700A93214AE5926EC2652D1EDEE390A3
+ 86D94794C314885A11E11ADBAEB4F4C4E4576BDECD18E35FA758AC471D151322E051D57488DF4910BDF315B1670F5B3E0D6E1423856209FC6EC05CAE1704E6B8
mpi/mpicoder.c
(1 . 31)(1 . 33)
4171 /* mpicoder.c - Coder for the external representation of MPIs
4172 * Copyright (C) 1998, 1999, 2005 Free Software Foundation, Inc.
4173 * Modified by No Such Labs. (C) 2015. See README.
4174 *
4175 * This file is part of GnuPG.
4176 * This file was originally part of Gnu Privacy Guard (GPG), ver. 1.4.10,
4177 * SHA256(gnupg-1.4.10.tar.gz):
4178 * 0bfd74660a2f6cedcf7d8256db4a63c996ffebbcdc2cf54397bfb72878c5a85a
4179 * (C) 1994-2005 Free Software Foundation, Inc.
4180 *
4181 * GnuPG is free software; you can redistribute it and/or modify
4182 * This program is free software: you can redistribute it and/or modify
4183 * it under the terms of the GNU General Public License as published by
4184 * the Free Software Foundation; either version 3 of the License, or
4185 * the Free Software Foundation, either version 3 of the License, or
4186 * (at your option) any later version.
4187 *
4188 * GnuPG is distributed in the hope that it will be useful,
4189 * This program is distributed in the hope that it will be useful,
4190 * but WITHOUT ANY WARRANTY; without even the implied warranty of
4191 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4192 * GNU General Public License for more details.
4193 *
4194 * You should have received a copy of the GNU General Public License
4195 * along with this program; if not, see <http://www.gnu.org/licenses/>.
4196 * along with this program. If not, see <http://www.gnu.org/licenses/>.
4197 */
4198
4199 #include <config.h>
4200 #include <stdio.h>
4201 #include <string.h>
4202 #include <stdlib.h>
4203 #include <assert.h>
4204
4205 #include "knobs.h"
4206 #include "mpi.h"
4207 #include "mpi-internal.h"
4208 #include "iobuf.h"
4209 #include "memory.h"
4210 #include "util.h"
4211
(35 . 110)(37 . 6)
4213
4214 #define MAX_EXTERN_MPI_BITS 16384
4215
4216 /****************
4217 * write an mpi to out.
4218 */
4219 int
4220 mpi_write( IOBUF out, MPI a )
4221 {
4222 int rc;
4223 unsigned nbits = mpi_get_nbits(a);
4224 byte *p, *buf;
4225 unsigned n;
4226
4227 if( nbits > MAX_EXTERN_MPI_BITS )
4228 log_bug("mpi_encode: mpi too large (%u bits)\n", nbits);
4229
4230 iobuf_put(out, (nbits >>8) );
4231 iobuf_put(out, (nbits) );
4232
4233 p = buf = mpi_get_buffer( a, &n, NULL );
4234 rc = iobuf_write( out, p, n );
4235 xfree(buf);
4236 return rc;
4237 }
4238
4239
4240 /****************
4241 * Read an external representation of an mpi and return the MPI
4242 * The external format is a 16 bit unsigned value stored in network byte order,
4243 * giving the number of bits for the following integer. The integer is stored
4244 * with MSB first (left padded with zeroes to align on a byte boundary).
4245 */
4246 MPI
4247 #ifdef M_DEBUG
4248 mpi_debug_read(IOBUF inp, unsigned *ret_nread, int secure, const char *info)
4249 #else
4250 mpi_read(IOBUF inp, unsigned *ret_nread, int secure)
4251 #endif
4252 {
4253 int c, i, j;
4254 unsigned int nmax = *ret_nread;
4255 unsigned nbits, nbytes, nlimbs, nread=0;
4256 mpi_limb_t a;
4257 MPI val = NULL;
4258
4259 if (nread == nmax)
4260 goto overflow;
4261 if( (c = iobuf_get(inp)) == -1 )
4262 goto leave;
4263 nread++;
4264 nbits = c << 8;
4265
4266 if (nread == nmax)
4267 goto overflow;
4268 if( (c = iobuf_get(inp)) == -1 )
4269 goto leave;
4270 nread++;
4271 nbits |= c;
4272
4273 if( nbits > MAX_EXTERN_MPI_BITS ) {
4274 log_error("mpi too large for this implementation (%u bits)\n", nbits);
4275 goto leave;
4276 }
4277
4278 nbytes = (nbits+7) / 8;
4279 nlimbs = (nbytes+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB;
4280 #ifdef M_DEBUG
4281 val = secure? mpi_debug_alloc_secure( nlimbs, info )
4282 : mpi_debug_alloc( nlimbs, info );
4283 #else
4284 val = secure? mpi_alloc_secure( nlimbs )
4285 : mpi_alloc( nlimbs );
4286 #endif
4287 i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
4288 i %= BYTES_PER_MPI_LIMB;
4289 val->nbits = nbits;
4290 j= val->nlimbs = nlimbs;
4291 val->sign = 0;
4292 for( ; j > 0; j-- ) {
4293 a = 0;
4294 for(; i < BYTES_PER_MPI_LIMB; i++ ) {
4295 if (nread == nmax) {
4296 #ifdef M_DEBUG
4297 mpi_debug_free (val);
4298 #else
4299 mpi_free (val);
4300 #endif
4301 val = NULL;
4302 goto overflow;
4303 }
4304 a <<= 8;
4305 a |= iobuf_get(inp) & 0xff; nread++;
4306 }
4307 i = 0;
4308 val->d[j-1] = a;
4309 }
4310
4311 leave:
4312 *ret_nread = nread;
4313 return val;
4314 overflow:
4315 log_error ("mpi larger than indicated length (%u bytes)\n", nmax);
4316 *ret_nread = nread;
4317 return val;
4318 }
4319
4320
4321 MPI
4322 mpi_read_from_buffer(byte *buffer, unsigned int *ret_nread, int secure)
(309 . 6)(207 . 7)
4324 }
4325
4326
4327 /*
4328 void
4329 g10_log_mpidump( const char *text, MPI a )
4330 {
(318 . 31)(217 . 7)
4332 mpi_print(fp, a, 1 );
4333 fputc('\n', fp);
4334 }
4335
4336 /****************
4337 * Special function to get the low 8 bytes from an mpi.
4338 * This can be used as a keyid; KEYID is an 2 element array.
4339 * Return the low 4 bytes.
4340 */
4341 u32
4342 mpi_get_keyid( MPI a, u32 *keyid )
4343 {
4344 #if BYTES_PER_MPI_LIMB == 4
4345 if( keyid ) {
4346 keyid[0] = a->nlimbs >= 2? a->d[1] : 0;
4347 keyid[1] = a->nlimbs >= 1? a->d[0] : 0;
4348 }
4349 return a->nlimbs >= 1? a->d[0] : 0;
4350 #elif BYTES_PER_MPI_LIMB == 8
4351 if( keyid ) {
4352 keyid[0] = a->nlimbs? (u32)(a->d[0] >> 32) : 0;
4353 keyid[1] = a->nlimbs? (u32)(a->d[0] & 0xffffffff) : 0;
4354 }
4355 return a->nlimbs? (u32)(a->d[0] & 0xffffffff) : 0;
4356 #else
4357 #error Make this function work with other LIMB sizes
4358 #endif
4359 }
4360 */
4361
4362
4363 /****************