1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
/*
* seccure - Copyright 2006 B. Poettering
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA
*/
/*
* SECCURE Elliptic Curve Crypto Utility for Reliable Encryption
*
* http://point-at-infinity.org/seccure/
*
*
* seccure implements a selection of asymmetric algorithms based on
* elliptic curve cryptography (ECC). See the manpage or the project's
* homepage for further details.
*
* This code links against the GNU gcrypt library "libgcrypt" (which is
* part of the GnuPG project). The code compiles successfully with
* libgcrypt 1.2.2. Use the included Makefile to build the binary.
*
* Compile with -D NOMEMLOCK if your machine doesn't support memory
* locking.
*
* Report bugs to: seccure AT point-at-infinity.org
*
*/
#include <string.h>
#include <gcrypt.h>
#include <assert.h>
#include "serialize.h"
/******************************************************************************/
const char compact_digits[COMPACT_DIGITS_COUNT] = {
'!', '#', '$', '%', '&', '(', ')', '*', '+', ',', '-', '.', '/', '0', '1',
'2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', ']', '^', '_',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~' };
int get_serialization_len(const gcry_mpi_t x, enum disp_format df)
{
int res;
switch(df) {
case DF_BIN:
res = (gcry_mpi_get_nbits(x) + 7) / 8;
break;
case DF_COMPACT: {
gcry_mpi_t base, Q;
base = gcry_mpi_set_ui(NULL, COMPACT_DIGITS_COUNT);
Q = gcry_mpi_copy(x);
for(res = 0; gcry_mpi_cmp_ui(Q, 0); res++)
gcry_mpi_div(Q, NULL, Q, base, 0);
gcry_mpi_release(base);
gcry_mpi_release(Q);
}
break;
default:
assert(0);
}
return res;
}
void serialize_mpi(char *outbuf, int outlen, enum disp_format df,
const gcry_mpi_t x)
{
switch(df) {
case DF_BIN: {
int len = (gcry_mpi_get_nbits(x) + 7) / 8;
assert(len <= outlen);
memset(outbuf, 0, outlen - len);
gcry_mpi_print(GCRYMPI_FMT_USG, (unsigned char*)outbuf + (outlen - len),
len, NULL, x);
}
break;
case DF_COMPACT: {
gcry_mpi_t base, Q, R;
int i;
base = gcry_mpi_set_ui(NULL, COMPACT_DIGITS_COUNT);
Q = gcry_mpi_copy(x);
R = gcry_mpi_new(0);
for(i = outlen - 1; i >= 0; i--) {
unsigned char digit = 0;
gcry_mpi_div(Q, R, Q, base, 0);
gcry_mpi_print(GCRYMPI_FMT_USG, &digit, 1, NULL, R);
assert(digit < COMPACT_DIGITS_COUNT);
outbuf[i] = compact_digits[digit];
}
assert(! gcry_mpi_cmp_ui(Q, 0));
gcry_mpi_release(base);
gcry_mpi_release(Q);
gcry_mpi_release(R);
}
break;
default:
assert(0);
}
}
int deserialize_mpi(gcry_mpi_t *x, enum disp_format df, const char *buf,
int inlen)
{
switch(df) {
case DF_BIN:
gcry_mpi_scan(x, GCRYMPI_FMT_USG, buf, inlen, NULL);
break;
case DF_COMPACT: {
char *d;
int i;
*x = gcry_mpi_new(0);
for(i = 0; i < inlen; i++) {
if (! (d = memchr(compact_digits, buf[i], COMPACT_DIGITS_COUNT))) {
gcry_mpi_release(*x);
return 0;
}
gcry_mpi_mul_ui(*x, *x, COMPACT_DIGITS_COUNT);
gcry_mpi_add_ui(*x, *x, d - compact_digits);
}
}
break;
default:
assert(0);
}
return 1;
}
|