aboutsummaryrefslogtreecommitdiff
path: root/src/util/crypto_mpi.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/crypto_mpi.c')
-rw-r--r--src/util/crypto_mpi.c111
1 files changed, 57 insertions, 54 deletions
diff --git a/src/util/crypto_mpi.c b/src/util/crypto_mpi.c
index 2c402e95a..5f1b456a5 100644
--- a/src/util/crypto_mpi.c
+++ b/src/util/crypto_mpi.c
@@ -29,14 +29,17 @@
29#include "gnunet_crypto_lib.h" 29#include "gnunet_crypto_lib.h"
30 30
31 31
32#define LOG(kind, ...) GNUNET_log_from(kind, "util-crypto-mpi", __VA_ARGS__) 32#define LOG(kind, ...) GNUNET_log_from (kind, "util-crypto-mpi", __VA_ARGS__)
33 33
34/** 34/**
35 * Log an error message at log-level 'level' that indicates 35 * Log an error message at log-level 'level' that indicates
36 * a failure of the command 'cmd' with the message given 36 * a failure of the command 'cmd' with the message given
37 * by gcry_strerror(rc). 37 * by gcry_strerror(rc).
38 */ 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) 39#define LOG_GCRY(level, cmd, rc) do { LOG (level, _ ( \
40 "`%s' failed at %s:%d with error: %s\n"), \
41 cmd, __FILE__, __LINE__, \
42 gcry_strerror (rc)); } while (0)
40 43
41 44
42/** 45/**
@@ -48,17 +51,17 @@
48 * @param target target size of the buffer 51 * @param target target size of the buffer
49 */ 52 */
50static void 53static void
51adjust(void *buf, 54adjust (void *buf,
52 size_t size, 55 size_t size,
53 size_t target) 56 size_t target)
54{ 57{
55 char *p = buf; 58 char *p = buf;
56 59
57 if (size < target) 60 if (size < target)
58 { 61 {
59 memmove(&p[target - size], buf, size); 62 memmove (&p[target - size], buf, size);
60 memset(buf, 0, target - size); 63 memset (buf, 0, target - size);
61 } 64 }
62} 65}
63 66
64 67
@@ -72,46 +75,46 @@ adjust(void *buf,
72 * @param val value to write to @a buf 75 * @param val value to write to @a buf
73 */ 76 */
74void 77void
75GNUNET_CRYPTO_mpi_print_unsigned(void *buf, 78GNUNET_CRYPTO_mpi_print_unsigned (void *buf,
76 size_t size, 79 size_t size,
77 gcry_mpi_t val) 80 gcry_mpi_t val)
78{ 81{
79 size_t rsize; 82 size_t rsize;
80 int rc; 83 int rc;
81 84
82 if (gcry_mpi_get_flag(val, GCRYMPI_FLAG_OPAQUE)) 85 if (gcry_mpi_get_flag (val, GCRYMPI_FLAG_OPAQUE))
83 { 86 {
84 /* Store opaque MPIs left aligned into the buffer. */ 87 /* Store opaque MPIs left aligned into the buffer. */
85 unsigned int nbits; 88 unsigned int nbits;
86 const void *p; 89 const void *p;
87 90
88 p = gcry_mpi_get_opaque(val, &nbits); 91 p = gcry_mpi_get_opaque (val, &nbits);
89 GNUNET_assert(p); 92 GNUNET_assert (p);
90 rsize = (nbits + 7) / 8; 93 rsize = (nbits + 7) / 8;
91 if (rsize > size) 94 if (rsize > size)
92 rsize = size; 95 rsize = size;
93 GNUNET_memcpy(buf, p, rsize); 96 GNUNET_memcpy (buf, p, rsize);
94 if (rsize < size) 97 if (rsize < size)
95 memset(buf + rsize, 0, size - rsize); 98 memset (buf + rsize, 0, size - rsize);
96 } 99 }
97 else 100 else
101 {
102 /* Store regular MPIs as unsigned integers right aligned into
103 the buffer. */
104 rsize = size;
105 if (0 !=
106 (rc = gcry_mpi_print (GCRYMPI_FMT_USG,
107 buf,
108 rsize, &rsize,
109 val)))
98 { 110 {
99 /* Store regular MPIs as unsigned integers right aligned into 111 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR,
100 the buffer. */ 112 "gcry_mpi_print",
101 rsize = size; 113 rc);
102 if (0 != 114 GNUNET_assert (0);
103 (rc = gcry_mpi_print(GCRYMPI_FMT_USG,
104 buf,
105 rsize, &rsize,
106 val)))
107 {
108 LOG_GCRY(GNUNET_ERROR_TYPE_ERROR,
109 "gcry_mpi_print",
110 rc);
111 GNUNET_assert(0);
112 }
113 adjust(buf, rsize, size);
114 } 115 }
116 adjust (buf, rsize, size);
117 }
115} 118}
116 119
117 120
@@ -125,21 +128,21 @@ GNUNET_CRYPTO_mpi_print_unsigned(void *buf,
125 * @param size number of bytes in @a data 128 * @param size number of bytes in @a data
126 */ 129 */
127void 130void
128GNUNET_CRYPTO_mpi_scan_unsigned(gcry_mpi_t *result, 131GNUNET_CRYPTO_mpi_scan_unsigned (gcry_mpi_t *result,
129 const void *data, 132 const void *data,
130 size_t size) 133 size_t size)
131{ 134{
132 int rc; 135 int rc;
133 136
134 if (0 != (rc = gcry_mpi_scan(result, 137 if (0 != (rc = gcry_mpi_scan (result,
135 GCRYMPI_FMT_USG, 138 GCRYMPI_FMT_USG,
136 data, size, &size))) 139 data, size, &size)))
137 { 140 {
138 LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, 141 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR,
139 "gcry_mpi_scan", 142 "gcry_mpi_scan",
140 rc); 143 rc);
141 GNUNET_assert(0); 144 GNUNET_assert (0);
142 } 145 }
143} 146}
144 147
145/* end of crypto_mpi.c */ 148/* end of crypto_mpi.c */