2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2011 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,
5 * Swiss federal institute of technology (ETH Zurich)
10 * seccure - Copyright 2009 B. Poettering
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
29 * SECCURE Elliptic Curve Crypto Utility for Reliable Encryption
31 * http://point-at-infinity.org/seccure/
34 * seccure implements a selection of asymmetric algorithms based on
35 * elliptic curve cryptography (ECC). See the manpage or the project's
36 * homepage for further details.
38 * This code links against the GNU gcrypt library "libgcrypt" (which
39 * is part of the GnuPG project). Use the included Makefile to build
42 * Report bugs to: seccure AT point-at-infinity.org
50 #include "serialize.h"
52 /* All ASCII characters in the range 33..126 excluding '\' and all quotes: */
53 const char compact_digits
[COMPACT_DIGITS_COUNT
] = {
54 '!', '#', '$', '%', '&', '(', ')', '*', '+', ',', '-', '.', '/', '0', '1',
55 '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@',
56 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
57 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', ']', '^', '_',
58 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
59 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~'
62 int get_serialization_len(const gcry_mpi_t x
, enum disp_format df
)
68 res
= (gcry_mpi_get_nbits(x
) + 7) / 8;
73 base
= gcry_mpi_set_ui(NULL
, COMPACT_DIGITS_COUNT
);
75 for (res
= 0; gcry_mpi_cmp_ui(Q
, 0); res
++)
76 gcry_mpi_div(Q
, NULL
, Q
, base
, 0);
77 gcry_mpi_release(base
);
88 void serialize_mpi(char *outbuf
, int outlen
, enum disp_format df
,
94 int len
= (gcry_mpi_get_nbits(x
) + 7) / 8;
95 assert(len
<= outlen
);
96 memset(outbuf
, 0, outlen
- len
);
97 gcry_mpi_print(GCRYMPI_FMT_USG
,
98 (unsigned char *)outbuf
+ (outlen
- len
),
104 gcry_mpi_t base
, Q
, R
;
106 base
= gcry_mpi_set_ui(NULL
, COMPACT_DIGITS_COUNT
);
107 Q
= gcry_mpi_copy(x
);
108 R
= gcry_mpi_snew(0);
109 for (i
= outlen
- 1; i
>= 0; i
--) {
110 unsigned char digit
= 0;
111 gcry_mpi_div(Q
, R
, Q
, base
, 0);
112 gcry_mpi_print(GCRYMPI_FMT_USG
, &digit
, 1, NULL
,
114 assert(digit
< COMPACT_DIGITS_COUNT
);
115 outbuf
[i
] = compact_digits
[digit
];
117 assert(!gcry_mpi_cmp_ui(Q
, 0));
118 gcry_mpi_release(base
);
128 int deserialize_mpi(gcry_mpi_t
* x
, enum disp_format df
, const char *buf
,
133 gcry_mpi_scan(x
, GCRYMPI_FMT_USG
, buf
, inlen
, NULL
);
134 gcry_mpi_set_flag(*x
, GCRYMPI_FLAG_SECURE
);
140 *x
= gcry_mpi_snew(0);
141 for (i
= 0; i
< inlen
; i
++) {
144 memchr(compact_digits
, buf
[i
],
145 COMPACT_DIGITS_COUNT
))) {
146 gcry_mpi_release(*x
);
149 gcry_mpi_mul_ui(*x
, *x
, COMPACT_DIGITS_COUNT
);
150 gcry_mpi_add_ui(*x
, *x
, d
- compact_digits
);