diff options
Diffstat (limited to 'src/include/taler_util.h')
-rw-r--r-- | src/include/taler_util.h | 341 |
1 files changed, 7 insertions, 334 deletions
diff --git a/src/include/taler_util.h b/src/include/taler_util.h index 00f139286..f2f319720 100644 --- a/src/include/taler_util.h +++ b/src/include/taler_util.h | |||
@@ -18,11 +18,15 @@ | |||
18 | * @brief Interface for common utility functions | 18 | * @brief Interface for common utility functions |
19 | * @author Sree Harsha Totakura <sreeharsha@totakura.in> | 19 | * @author Sree Harsha Totakura <sreeharsha@totakura.in> |
20 | */ | 20 | */ |
21 | #ifndef TALER_UTIL_H_ | 21 | #ifndef TALER_UTIL_H |
22 | #define TALER_UTIL_H_ | 22 | #define TALER_UTIL_H |
23 | 23 | ||
24 | #include <gnunet/gnunet_util_lib.h> | 24 | #include <gnunet/gnunet_util_lib.h> |
25 | #include <gcrypt.h> | 25 | #include "taler_amount_lib.h" |
26 | #include "taler_crypto_lib.h" | ||
27 | #include "taler_json_lib.h" | ||
28 | |||
29 | |||
26 | 30 | ||
27 | /* Define logging functions */ | 31 | /* Define logging functions */ |
28 | #define LOG_DEBUG(...) \ | 32 | #define LOG_DEBUG(...) \ |
@@ -50,7 +54,6 @@ | |||
50 | } while(0) | 54 | } while(0) |
51 | 55 | ||
52 | 56 | ||
53 | |||
54 | /** | 57 | /** |
55 | * Log an error message at log-level 'level' that indicates | 58 | * Log an error message at log-level 'level' that indicates |
56 | * a failure of the command 'cmd' with the message given | 59 | * a failure of the command 'cmd' with the message given |
@@ -63,7 +66,6 @@ | |||
63 | do {int rc; rc = cmd; if (!rc) break; LOG_ERROR("A Gcrypt call failed at %s:%d with error: %s\n", __FILE__, __LINE__, gcry_strerror(rc)); abort(); } while (0) | 66 | do {int rc; rc = cmd; if (!rc) break; LOG_ERROR("A Gcrypt call failed at %s:%d with error: %s\n", __FILE__, __LINE__, gcry_strerror(rc)); abort(); } while (0) |
64 | 67 | ||
65 | 68 | ||
66 | |||
67 | /** | 69 | /** |
68 | * Initialize Gcrypt library. | 70 | * Initialize Gcrypt library. |
69 | */ | 71 | */ |
@@ -83,333 +85,4 @@ TALER_config_load (const char *base_dir); | |||
83 | 85 | ||
84 | 86 | ||
85 | 87 | ||
86 | /* *********************** Amount management ****************** */ | ||
87 | |||
88 | |||
89 | /** | ||
90 | * Number of characters (plus 1 for 0-termination) we use to | ||
91 | * represent currency names (i.e. EUR, USD, etc.). We use | ||
92 | * 8 for alignment (!). | ||
93 | */ | ||
94 | #define TALER_CURRENCY_LEN 8 | ||
95 | |||
96 | |||
97 | GNUNET_NETWORK_STRUCT_BEGIN | ||
98 | |||
99 | /** | ||
100 | * Amount, encoded for network transmission. | ||
101 | */ | ||
102 | struct TALER_AmountNBO | ||
103 | { | ||
104 | /** | ||
105 | * Value in the main currency, in NBO. | ||
106 | */ | ||
107 | uint32_t value; | ||
108 | |||
109 | /** | ||
110 | * Additinal fractional value, in NBO. | ||
111 | */ | ||
112 | uint32_t fraction; | ||
113 | |||
114 | /** | ||
115 | * Type of the currency being represented. | ||
116 | */ | ||
117 | char currency[TALER_CURRENCY_LEN]; | ||
118 | }; | ||
119 | |||
120 | GNUNET_NETWORK_STRUCT_END | ||
121 | |||
122 | |||
123 | /** | ||
124 | * Representation of monetary value in a given currency. | ||
125 | */ | ||
126 | struct TALER_Amount | ||
127 | { | ||
128 | /** | ||
129 | * Value (numerator of fraction) | ||
130 | */ | ||
131 | uint32_t value; | ||
132 | |||
133 | /** | ||
134 | * Fraction (denominator of fraction) | ||
135 | */ | ||
136 | uint32_t fraction; | ||
137 | |||
138 | /** | ||
139 | * Currency string, left adjusted and padded with zeros. | ||
140 | */ | ||
141 | char currency[TALER_CURRENCY_LEN]; | ||
142 | }; | ||
143 | |||
144 | |||
145 | /** | ||
146 | * Parse denomination description, in the format "T : V : F". | ||
147 | * | ||
148 | * @param str denomination description | ||
149 | * @param denom denomination to write the result to | ||
150 | * @return #GNUNET_OK if the string is a valid denomination specification, | ||
151 | * #GNUNET_SYSERR if it is invalid. | ||
152 | */ | ||
153 | int | ||
154 | TALER_string_to_amount (const char *str, | ||
155 | struct TALER_Amount *denom); | ||
156 | |||
157 | |||
158 | /** | ||
159 | * Convert amount from host to network representation. | ||
160 | * | ||
161 | * @param d amount in host representation | ||
162 | * @return amount in network representation | ||
163 | */ | ||
164 | struct TALER_AmountNBO | ||
165 | TALER_amount_hton (struct TALER_Amount d); | ||
166 | |||
167 | |||
168 | /** | ||
169 | * Convert amount from network to host representation. | ||
170 | * | ||
171 | * @param d amount in network representation | ||
172 | * @return amount in host representation | ||
173 | */ | ||
174 | struct TALER_Amount | ||
175 | TALER_amount_ntoh (struct TALER_AmountNBO dn); | ||
176 | |||
177 | |||
178 | /** | ||
179 | * Compare the value/fraction of two amounts. Does not compare the currency, | ||
180 | * i.e. comparing amounts with the same value and fraction but different | ||
181 | * currency would return 0. | ||
182 | * | ||
183 | * @param a1 first amount | ||
184 | * @param a2 second amount | ||
185 | * @return result of the comparison | ||
186 | */ | ||
187 | int | ||
188 | TALER_amount_cmp (struct TALER_Amount a1, | ||
189 | struct TALER_Amount a2); | ||
190 | |||
191 | |||
192 | /** | ||
193 | * Perform saturating subtraction of amounts. | ||
194 | * | ||
195 | * @param a1 amount to subtract from | ||
196 | * @param a2 amount to subtract | ||
197 | * @return (a1-a2) or 0 if a2>=a1 | ||
198 | */ | ||
199 | struct TALER_Amount | ||
200 | TALER_amount_subtract (struct TALER_Amount a1, | ||
201 | struct TALER_Amount a2); | ||
202 | |||
203 | |||
204 | /** | ||
205 | * Perform saturating addition of amounts | ||
206 | * | ||
207 | * @param a1 first amount to add | ||
208 | * @param a2 second amount to add | ||
209 | * @return sum of a1 and a2 | ||
210 | */ | ||
211 | struct TALER_Amount | ||
212 | TALER_amount_add (struct TALER_Amount a1, | ||
213 | struct TALER_Amount a2); | ||
214 | |||
215 | |||
216 | /** | ||
217 | * Normalize the given amount. | ||
218 | * | ||
219 | * @param amout amount to normalize | ||
220 | * @return normalized amount | ||
221 | */ | ||
222 | struct TALER_Amount | ||
223 | TALER_amount_normalize (struct TALER_Amount amount); | ||
224 | |||
225 | |||
226 | /** | ||
227 | * Convert amount to string. | ||
228 | * | ||
229 | * @param amount amount to convert to string | ||
230 | * @return freshly allocated string representation | ||
231 | */ | ||
232 | char * | ||
233 | TALER_amount_to_string (struct TALER_Amount amount); | ||
234 | |||
235 | |||
236 | /* ****************** Coin crypto primitives ************* */ | ||
237 | |||
238 | /** | ||
239 | * Public information about a coin (including the public key | ||
240 | * of the coin, the denomination key and the signature with | ||
241 | * the denomination key). | ||
242 | */ | ||
243 | struct TALER_CoinPublicInfo | ||
244 | { | ||
245 | /** | ||
246 | * The coin's public key. | ||
247 | */ | ||
248 | struct GNUNET_CRYPTO_EcdsaPublicKey coin_pub; | ||
249 | |||
250 | /** | ||
251 | * Public key representing the denomination of the coin | ||
252 | * that is being deposited. | ||
253 | */ | ||
254 | struct GNUNET_CRYPTO_rsa_PublicKey *denom_pub; | ||
255 | |||
256 | /** | ||
257 | * (Unblinded) signature over @e coin_pub with @e denom_pub, | ||
258 | * which demonstrates that the coin is valid. | ||
259 | */ | ||
260 | struct GNUNET_CRYPTO_rsa_Signature *denom_sig; | ||
261 | }; | ||
262 | |||
263 | |||
264 | /** | ||
265 | * Check if a coin is valid; that is, whether the denomination key exists, | ||
266 | * is not expired, and the signature is correct. | ||
267 | * | ||
268 | * @param coin_public_info the coin public info to check for validity | ||
269 | * @return #GNUNET_YES if the coin is valid, | ||
270 | * #GNUNET_NO if it is invalid | ||
271 | * #GNUNET_SYSERROR if an internal error occured | ||
272 | */ | ||
273 | int | ||
274 | TALER_test_coin_valid (const struct TALER_CoinPublicInfo *coin_public_info); | ||
275 | |||
276 | |||
277 | /* ****************** Refresh crypto primitives ************* */ | ||
278 | |||
279 | /** | ||
280 | * Secret used to decrypt the key to decrypt link secrets. | ||
281 | */ | ||
282 | struct TALER_TransferSecret | ||
283 | { | ||
284 | /** | ||
285 | * Secret used to encrypt/decrypt the `struct TALER_LinkSecret`. | ||
286 | * Must be (currently) a hash as this is what | ||
287 | * #GNUNET_CRYPTO_ecc_ecdh() returns to us. | ||
288 | */ | ||
289 | struct GNUNET_HashCode key; | ||
290 | }; | ||
291 | |||
292 | |||
293 | /** | ||
294 | * Secret used to decrypt refresh links. | ||
295 | */ | ||
296 | struct TALER_LinkSecret | ||
297 | { | ||
298 | /** | ||
299 | * Secret used to decrypt the refresh link data. | ||
300 | */ | ||
301 | char key[sizeof (struct GNUNET_HashCode)]; | ||
302 | }; | ||
303 | |||
304 | |||
305 | /** | ||
306 | * Encrypted secret used to decrypt refresh links. | ||
307 | */ | ||
308 | struct TALER_EncryptedLinkSecret | ||
309 | { | ||
310 | /** | ||
311 | * Encrypted secret, must be the given size! | ||
312 | */ | ||
313 | char enc[sizeof (struct TALER_LinkSecret)]; | ||
314 | }; | ||
315 | |||
316 | |||
317 | /** | ||
318 | * Representation of an encrypted refresh link. | ||
319 | */ | ||
320 | struct TALER_RefreshLinkEncrypted | ||
321 | { | ||
322 | |||
323 | /** | ||
324 | * Encrypted blinding key with @e blinding_key_enc_size bytes, | ||
325 | * must be allocated at the end of this struct. | ||
326 | */ | ||
327 | const char *blinding_key_enc; | ||
328 | |||
329 | /** | ||
330 | * Number of bytes in @e blinding_key_enc. | ||
331 | */ | ||
332 | size_t blinding_key_enc_size; | ||
333 | |||
334 | /** | ||
335 | * Encrypted private key of the coin. | ||
336 | */ | ||
337 | char coin_priv_enc[sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)]; | ||
338 | |||
339 | }; | ||
340 | |||
341 | |||
342 | /** | ||
343 | * Representation of an refresh link in cleartext. | ||
344 | */ | ||
345 | struct TALER_RefreshLinkDecrypted | ||
346 | { | ||
347 | |||
348 | /** | ||
349 | * Private key of the coin. | ||
350 | */ | ||
351 | struct GNUNET_CRYPTO_EcdsaPrivateKey coin_priv; | ||
352 | |||
353 | /** | ||
354 | * Blinding key with @e blinding_key_enc_size bytes. | ||
355 | */ | ||
356 | struct GNUNET_CRYPTO_rsa_BlindingKey *blinding_key; | ||
357 | |||
358 | }; | ||
359 | |||
360 | |||
361 | /** | ||
362 | * Use the @a trans_sec (from ECDHE) to decrypt the @a secret_enc | ||
363 | * to obtain the @a secret to decrypt the linkage data. | ||
364 | * | ||
365 | * @param secret_enc encrypted secret | ||
366 | * @param trans_sec transfer secret | ||
367 | * @param secret shared secret for refresh link decryption | ||
368 | * @return #GNUNET_OK on success | ||
369 | */ | ||
370 | int | ||
371 | TALER_transfer_decrypt (const struct TALER_EncryptedLinkSecret *secret_enc, | ||
372 | const struct TALER_TransferSecret *trans_sec, | ||
373 | struct TALER_LinkSecret *secret); | ||
374 | |||
375 | |||
376 | /** | ||
377 | * Use the @a trans_sec (from ECDHE) to encrypt the @a secret | ||
378 | * to obtain the @a secret_enc. | ||
379 | * | ||
380 | * @param secret shared secret for refresh link decryption | ||
381 | * @param trans_sec transfer secret | ||
382 | * @param secret_enc[out] encrypted secret | ||
383 | * @return #GNUNET_OK on success | ||
384 | */ | ||
385 | int | ||
386 | TALER_transfer_encrypt (const struct TALER_LinkSecret *secret, | ||
387 | const struct TALER_TransferSecret *trans_sec, | ||
388 | struct TALER_EncryptedLinkSecret *secret_enc); | ||
389 | |||
390 | |||
391 | /** | ||
392 | * Decrypt refresh link information. | ||
393 | * | ||
394 | * @param input encrypted refresh link data | ||
395 | * @param secret shared secret to use for decryption | ||
396 | * @return NULL on error | ||
397 | */ | ||
398 | struct TALER_RefreshLinkDecrypted * | ||
399 | TALER_refresh_decrypt (const struct TALER_RefreshLinkEncrypted *input, | ||
400 | const struct TALER_LinkSecret *secret); | ||
401 | |||
402 | |||
403 | /** | ||
404 | * Encrypt refresh link information. | ||
405 | * | ||
406 | * @param input plaintext refresh link data | ||
407 | * @param secret shared secret to use for encryption | ||
408 | * @return NULL on error (should never happen) | ||
409 | */ | ||
410 | struct TALER_RefreshLinkEncrypted * | ||
411 | TALER_refresh_encrypt (const struct TALER_RefreshLinkDecrypted *input, | ||
412 | const struct TALER_LinkSecret *secret); | ||
413 | |||
414 | |||
415 | #endif | 88 | #endif |