Use a uniform random number mod an RSA composites for both
[oweals/gnunet.git] / src / my / my_query_helper.c
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2016 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20 /**
21  * @file my/my_query_helper.c
22  * @brief library to help with access to a MySQL database
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include <mysql/mysql.h>
27 #include "gnunet_my_lib.h"
28
29
30 /**
31  * Function called to convert input argument into SQL parameters.
32  *
33  * @param cls closure
34  * @param pq data about the query
35  * @param qbind array of parameters to initialize
36  * @return -1 on error
37  */
38 static int
39 my_conv_fixed_size (void *cls,
40                     const struct GNUNET_MY_QueryParam *qp,
41                     MYSQL_BIND *qbind)
42 {
43   GNUNET_assert (1 == qp->num_params);
44   qbind->buffer = (void *) qp->data;
45   qbind->buffer_length = qp->data_len;
46   qbind->length = (unsigned long *) &qp->data_len;
47   qbind->buffer_type = 1;
48   return 0;
49 }
50
51
52 /**
53  * Generate query parameter for a buffer @a ptr of
54  * @a ptr_size bytes.
55  *
56  * @param ptr pointer to the query parameter to pass
57  * @param ptr_size number of bytes in @a ptr
58  */
59 struct GNUNET_MY_QueryParam
60 GNUNET_MY_query_param_fixed_size (const void *ptr,
61                                   size_t ptr_size)
62 {
63   struct GNUNET_MY_QueryParam qp = {
64     &my_conv_fixed_size,
65     NULL,
66     1,
67     ptr,
68     (unsigned long) ptr_size
69   };
70   return qp;
71 }
72
73
74 /**
75   * Generate query parameter for a string
76   *
77   *@param ptr pointer to the string query parameter to pass
78   */
79 struct GNUNET_MY_QueryParam
80 GNUNET_MY_query_param_string (const char *ptr)
81 {
82   return GNUNET_MY_query_param_fixed_size(ptr,
83                                          strlen(ptr));
84 }
85
86 /**
87   * Function called to convert input argument into SQL parameters
88   *
89   *@param cls closure
90   *@param pq data about the query
91  * @param qbind array of parameters to initialize
92   *@return -1 on error 
93   */
94 static int
95 my_conv_uint16 (void *cls,
96                 const struct GNUNET_MY_QueryParam * qp,
97                 MYSQL_BIND *qbind)
98 {
99   const uint16_t *u_hbo = qp->data;
100   uint16_t *u_nbo;
101
102   GNUNET_assert (1 == qp->num_params);
103
104   u_nbo = GNUNET_new (uint16_t);
105   *u_nbo = htons (*u_hbo);
106   qbind->buffer = (void *) u_nbo;
107   qbind->buffer_length = sizeof(uint16_t);
108   qbind->buffer_type = 1;
109
110   return 1;
111 }
112
113 /**
114   * Generate query parameter for an uint16_t in host byte order.
115   *
116   * @param x pointer to the query parameter to pass
117   */
118 struct GNUNET_MY_QueryParam
119 GNUNET_MY_query_param_uint16 (const uint16_t *x)
120 {
121   struct GNUNET_MY_QueryParam res = { 
122       &my_conv_uint16,
123       NULL,
124       1,
125       x,
126       sizeof (*x)
127     };
128
129   return res;
130 }
131
132 /**
133   * Function called to convert input argument into SQL parameters
134   *
135   *@param cls closure
136   *@param pq data about the query
137  * @param qbind array of parameters to initialize
138   *@return -1 on error 
139   */
140 static int 
141 my_conv_uint32 (void *cls,
142                 const struct GNUNET_MY_QueryParam *qp,
143                 MYSQL_BIND *qbind)
144 {
145   const uint32_t *u_hbo = qp->data;
146   uint32_t * u_nbo;
147
148   GNUNET_assert (1 == qp->num_params);
149
150   u_nbo = GNUNET_new (uint32_t);
151   *u_nbo = htonl (*u_hbo);
152
153   qbind->buffer = (void *) u_nbo;
154   qbind->buffer_length = sizeof(uint32_t);
155   qbind->buffer_type = 1;
156
157   return 1;
158 }
159
160 /**
161   * Generate query parameter for an uint32_t in host byte order
162   *
163   *@param x pointer to the query parameter to pass
164   */
165 struct GNUNET_MY_QueryParam
166 GNUNET_MY_query_param_uint32 (const uint32_t *x)
167 {
168   struct GNUNET_MY_QueryParam res = {
169     &my_conv_uint32,
170     NULL,
171     1,
172     x, 
173     sizeof (*x)
174   };
175   
176   return res;
177 }
178
179 /**
180   * Function called to convert input argument into SQL parameters
181   *
182   *@param cls closure
183   *@param pq data about the query
184  * @param qbind array of parameters to initialize
185   *@return -1 on error 
186   */
187 static int
188 my_conv_uint64 (void *cls,
189               const struct GNUNET_MY_QueryParam *qp,
190               MYSQL_BIND * qbind)
191 {
192   const uint64_t * u_hbo = qp->data;
193   uint64_t *u_nbo;
194
195   GNUNET_assert (1 == qp->num_params);
196
197   u_nbo = GNUNET_new(uint64_t);
198   *u_nbo = GNUNET_htonll (*u_hbo);
199
200   qbind->buffer = (void *) u_nbo;
201   qbind->buffer_length = sizeof (uint64_t);
202   qbind->buffer_type = 1;
203
204   return 1;  
205 }
206
207 /**
208   * Generate query parameter for an uint64_t in host byte order
209   *
210   *@param x pointer to the query parameter to pass
211   */
212 struct GNUNET_MY_QueryParam
213 GNUNET_MY_query_param_uint64 (const uint64_t *x)
214 {
215   struct GNUNET_MY_QueryParam res = {
216     &my_conv_uint64,
217     NULL,
218     1,
219     x,
220     sizeof(*x)
221   };
222
223   return res;
224 }
225
226 /**
227   * Function called to convert input argument into SQL parameters
228   *
229   *@param cls closure
230   *@param pq data about the query
231  * @param qbind array of parameters to initialize
232   *@return -1 on error 
233   */
234 static int
235 my_conv_rsa_public_key (void *cls,
236                         const struct GNUNET_MY_QueryParam *qp,
237                         MYSQL_BIND * qbind)
238   {
239     const struct GNUNET_CRYPTO_RsaPublicKey *rsa = qp->data;
240     char *buf;
241     size_t buf_size;
242
243     GNUNET_assert(1 == qp->num_params);
244
245     buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa, &buf);
246
247     qbind->buffer = (void *)buf;
248     qbind->buffer_length = buf_size - 1;
249     qbind->buffer_type = 1;
250
251     return 1;
252   }
253
254   /**
255     * Generate query parameter for an RSA public key. The
256     * database must contain a BLOB type in the respective position.
257     *
258     * @param x the query parameter to pass
259     * @return array entry for the query parameters to use
260     */
261 struct GNUNET_MY_QueryParam
262 GNUNET_MY_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x)
263 {
264   struct GNUNET_MY_QueryParam res = {
265     &my_conv_rsa_public_key,
266     NULL,
267     1,
268     x,
269     0
270   };
271
272   return res;
273 }
274
275 /**
276   * Function called to convert input argument into SQL parameters
277   *
278   *@param cls closure
279   *@param pq data about the query
280  * @param qbind array of parameters to initialize
281   *@return -1 on error 
282   */
283 static int 
284 my_conv_rsa_signature (void *cls,
285                       const struct GNUNET_MY_QueryParam *qp,
286                       MYSQL_BIND * qbind)
287 {
288   const struct GNUNET_CRYPTO_RsaSignature *sig = qp->data;
289   char *buf;
290   size_t buf_size;
291
292   GNUNET_assert(1 == qp->num_params);
293
294   buf_size = GNUNET_CRYPTO_rsa_signature_encode(sig,
295                                                 &buf);
296
297   qbind->buffer = (void *)buf;
298   qbind->buffer_length = buf_size - 1;
299   qbind->buffer_type = 1;
300
301   return 1;
302 }
303
304 /**
305   * Generate query parameter for an RSA signature. The
306   * database must contain a BLOB type in the respective position
307   *
308   *@param x the query parameter to pass
309   *@return array entry for the query parameters to use
310   */
311 struct GNUNET_MY_QueryParam
312 GNUNET_MY_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
313 {
314   struct GNUNET_MY_QueryParam res = {
315     &my_conv_rsa_signature,
316     NULL,
317     1,
318     (x),
319     0
320   };
321   return res;
322 }
323
324 /**
325   * Generate query parameter for an absolute time value.
326   * The database must store a 64-bit integer.
327   *
328   *@param x pointer to the query parameter to pass
329   *@return array entry for the query parameters to use
330   */
331 struct GNUNET_MY_QueryParam
332 GNUNET_MY_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
333 {
334   return GNUNET_MY_query_param_uint64 (&x->abs_value_us);
335 }
336
337 /**
338   * Generate query parameter for an absolute time value.
339   * The database must store a 64-bit integer.
340   *
341   *@param x pointer to the query parameter to pass
342   */
343 struct GNUNET_MY_QueryParam
344 GNUNET_MY_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x)
345 {
346   return GNUNET_MY_query_param_auto_from_type (&x->abs_value_us__);
347 }
348
349 /* end of my_query_helper.c */