added ecc routines, exported tundev routines
[netsniff-ng.git] / src / serialize.c
bloba1456f46d898c31f22dd7ff18444074341389656
1 /*
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)
6 * Subject to the GPL.
7 */
9 /*
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
25 * 02111-1307 USA
28 /*
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
40 * the binary.
42 * Report bugs to: seccure AT point-at-infinity.org
46 #include <string.h>
47 #include <gcrypt.h>
48 #include <assert.h>
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)
64 int res;
66 switch (df) {
67 case DF_BIN:
68 res = (gcry_mpi_get_nbits(x) + 7) / 8;
69 break;
70 case DF_COMPACT:
71 do {
72 gcry_mpi_t base, Q;
73 base = gcry_mpi_set_ui(NULL, COMPACT_DIGITS_COUNT);
74 Q = gcry_mpi_copy(x);
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);
78 gcry_mpi_release(Q);
79 } while (0);
80 break;
81 default:
82 assert(0);
85 return res;
88 void serialize_mpi(char *outbuf, int outlen, enum disp_format df,
89 const gcry_mpi_t x)
91 switch (df) {
92 case DF_BIN:
93 do {
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),
99 len, NULL, x);
100 } while (0);
101 break;
102 case DF_COMPACT:
103 do {
104 gcry_mpi_t base, Q, R;
105 int i;
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);
119 gcry_mpi_release(Q);
120 gcry_mpi_release(R);
121 } while (0);
122 break;
123 default:
124 assert(0);
128 int deserialize_mpi(gcry_mpi_t * x, enum disp_format df, const char *buf,
129 int inlen)
131 switch (df) {
132 case DF_BIN:
133 gcry_mpi_scan(x, GCRYMPI_FMT_USG, buf, inlen, NULL);
134 gcry_mpi_set_flag(*x, GCRYMPI_FLAG_SECURE);
135 break;
136 case DF_COMPACT:
137 do {
138 char *d;
139 int i;
140 *x = gcry_mpi_snew(0);
141 for (i = 0; i < inlen; i++) {
142 if (!
143 (d =
144 memchr(compact_digits, buf[i],
145 COMPACT_DIGITS_COUNT))) {
146 gcry_mpi_release(*x);
147 return 0;
149 gcry_mpi_mul_ui(*x, *x, COMPACT_DIGITS_COUNT);
150 gcry_mpi_add_ui(*x, *x, d - compact_digits);
152 } while (0);
153 break;
154 default:
155 assert(0);
158 return 1;