diff options
Diffstat (limited to 'src/util/crypto_mpi.c')
-rw-r--r-- | src/util/crypto_mpi.c | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/src/util/crypto_mpi.c b/src/util/crypto_mpi.c new file mode 100644 index 000000000..8e52424cf --- /dev/null +++ b/src/util/crypto_mpi.c | |||
@@ -0,0 +1,132 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2012, 2013 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file util/crypto_mpi.c | ||
23 | * @brief Helper functions for libgcrypt MPIs | ||
24 | * @author Christian Grothoff | ||
25 | * @author Florian Dold | ||
26 | */ | ||
27 | #include "platform.h" | ||
28 | #include <gcrypt.h> | ||
29 | #include "gnunet_util_lib.h" | ||
30 | |||
31 | |||
32 | #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) | ||
33 | |||
34 | /** | ||
35 | * Log an error message at log-level 'level' that indicates | ||
36 | * a failure of the command 'cmd' with the message given | ||
37 | * by gcry_strerror(rc). | ||
38 | */ | ||
39 | #define LOG_GCRY(level, cmd, rc) do { LOG(level, _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, gcry_strerror(rc)); } while(0) | ||
40 | |||
41 | |||
42 | /** | ||
43 | * If target != size, move @a target bytes to the end of the size-sized | ||
44 | * buffer and zero out the first @a target - @a size bytes. | ||
45 | * | ||
46 | * @param buf original buffer | ||
47 | * @param size number of bytes in @a buf | ||
48 | * @param target target size of the buffer | ||
49 | */ | ||
50 | static void | ||
51 | adjust (void *buf, | ||
52 | size_t size, | ||
53 | size_t target) | ||
54 | { | ||
55 | if (size < target) | ||
56 | { | ||
57 | memmove (&buf[target - size], buf, size); | ||
58 | memset (buf, 0, target - size); | ||
59 | } | ||
60 | } | ||
61 | |||
62 | |||
63 | /** | ||
64 | * Output the given MPI value to the given buffer in | ||
65 | * network byte order. | ||
66 | * The MPI @a val may not be negative. | ||
67 | * | ||
68 | * @param buf where to output to | ||
69 | * @param size number of bytes in @a buf | ||
70 | * @param val value to write to @a buf | ||
71 | */ | ||
72 | void | ||
73 | GNUNET_CRYPTO_mpi_print_unsigned (void *buf, | ||
74 | size_t size, | ||
75 | gcry_mpi_t val) | ||
76 | { | ||
77 | size_t rsize; | ||
78 | |||
79 | if (gcry_mpi_get_flag (val, GCRYMPI_FLAG_OPAQUE)) | ||
80 | { | ||
81 | /* Store opaque MPIs left aligned into the buffer. */ | ||
82 | unsigned int nbits; | ||
83 | const void *p; | ||
84 | |||
85 | p = gcry_mpi_get_opaque (val, &nbits); | ||
86 | GNUNET_assert (p); | ||
87 | rsize = (nbits+7)/8; | ||
88 | if (rsize > size) | ||
89 | rsize = size; | ||
90 | memcpy (buf, p, rsize); | ||
91 | if (rsize < size) | ||
92 | memset (buf+rsize, 0, size - rsize); | ||
93 | } | ||
94 | else | ||
95 | { | ||
96 | /* Store regular MPIs as unsigned integers right aligned into | ||
97 | the buffer. */ | ||
98 | rsize = size; | ||
99 | GNUNET_assert (0 == | ||
100 | gcry_mpi_print (GCRYMPI_FMT_USG, buf, rsize, &rsize, | ||
101 | val)); | ||
102 | adjust (buf, rsize, size); | ||
103 | } | ||
104 | } | ||
105 | |||
106 | |||
107 | /** | ||
108 | * Convert data buffer into MPI value. | ||
109 | * The buffer is interpreted as network | ||
110 | * byte order, unsigned integer. | ||
111 | * | ||
112 | * @param result where to store MPI value (allocated) | ||
113 | * @param data raw data (GCRYMPI_FMT_USG) | ||
114 | * @param size number of bytes in @a data | ||
115 | */ | ||
116 | void | ||
117 | GNUNET_CRYPTO_mpi_scan_unsigned (gcry_mpi_t *result, | ||
118 | const void *data, | ||
119 | size_t size) | ||
120 | { | ||
121 | int rc; | ||
122 | |||
123 | if (0 != (rc = gcry_mpi_scan (result, | ||
124 | GCRYMPI_FMT_USG, | ||
125 | data, size, &size))) | ||
126 | { | ||
127 | LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); | ||
128 | GNUNET_assert (0); | ||
129 | } | ||
130 | } | ||
131 | |||
132 | /* end of crypto_mpi.c */ | ||