gcrypt: Simplify init_random_seed_or_die().
[paraslash.git] / gcrypt.c
1 /*
2 * Copyright (C) 2011 Andre Noll <maan@tuebingen.mpg.de>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file gcrypt.c Libgrcypt-based encryption/decryption routines. */
8
9 #include <regex.h>
10 #include <gcrypt.h>
11
12 #include "para.h"
13 #include "error.h"
14 #include "string.h"
15 #include "crypt.h"
16 #include "crypt_backend.h"
17 #include "fd.h"
18 #include "base64.h"
19
20 //#define GCRYPT_DEBUG 1
21
22 #ifdef GCRYPT_DEBUG
23 static void dump_buffer(const char *msg, unsigned char *buf, int len)
24 {
25 int i;
26
27 fprintf(stderr, "%s (%d bytes): ", msg, len);
28 for (i = 0; i < len; i++)
29 fprintf(stderr, "%02x ", buf[i]);
30 fprintf(stderr, "\n");
31 }
32 #else
33 /** Empty. Define GCRYPT_DEBUG to dump buffers. */
34 #define dump_buffer(a, b, c)
35 #endif
36
37 void hash_function(const char *data, unsigned long len, unsigned char *hash)
38 {
39 gcry_error_t gret;
40 gcry_md_hd_t handle;
41 unsigned char *md;
42
43 gret = gcry_md_open(&handle, GCRY_MD_SHA1, 0);
44 assert(gret == 0);
45 gcry_md_write(handle, data, (size_t)len);
46 gcry_md_final(handle);
47 md = gcry_md_read(handle, GCRY_MD_SHA1);
48 assert(md);
49 memcpy(hash, md, HASH_SIZE);
50 gcry_md_close(handle);
51 }
52
53 void get_random_bytes_or_die(unsigned char *buf, int num)
54 {
55 gcry_randomize(buf, (size_t)num, GCRY_STRONG_RANDOM);
56 }
57
58 /*
59 * This is called at the beginning of every program that uses libgcrypt. We
60 * don't have to initialize any random seed here, but we must initialize the
61 * gcrypt library. This task is performed by gcry_check_version() which can
62 * also check that the gcrypt library version is at least the minimal required
63 * version.
64 */
65 void init_random_seed_or_die(void)
66 {
67 const char *req_ver = "1.5.0";
68
69 if (gcry_check_version(req_ver))
70 return;
71 PARA_EMERG_LOG("fatal: need at least libgcrypt-%s, have: %s\n",
72 req_ver, gcry_check_version(NULL));
73 exit(EXIT_FAILURE);
74 }
75
76 /** S-expression for the public part of an RSA key. */
77 #define RSA_PUBKEY_SEXP "(public-key (rsa (n %m) (e %m)))"
78 /** S-expression for a private RSA key. */
79 #define RSA_PRIVKEY_SEXP "(private-key (rsa (n %m) (e %m) (d %m) (p %m) (q %m) (u %m)))"
80 /** S-expression for decryption. */
81 #define RSA_DECRYPT_SEXP "(enc-val(flags oaep)(rsa(a %m)))"
82
83 struct asymmetric_key {
84 gcry_sexp_t sexp;
85 int num_bytes;
86 };
87
88 static const char *gcrypt_strerror(gcry_error_t gret)
89 {
90 return gcry_strerror(gcry_err_code(gret));
91 }
92
93 static int decode_key(const char *key_file, const char *header_str,
94 const char *footer_str, unsigned char **result)
95 {
96 int ret, ret2, i, j;
97 void *map;
98 size_t map_size, key_size, blob_size;
99 unsigned char *blob = NULL;
100 char *begin, *footer, *key;
101
102 ret = mmap_full_file(key_file, O_RDONLY, &map, &map_size, NULL);
103 if (ret < 0)
104 goto out;
105 ret = -E_KEY_MARKER;
106 if (strncmp(map, header_str, strlen(header_str)))
107 goto unmap;
108 footer = strstr(map, footer_str);
109 ret = -E_KEY_MARKER;
110 if (!footer)
111 goto unmap;
112 begin = map + strlen(header_str);
113 /* skip whitespace at the beginning */
114 for (; begin < footer; begin++) {
115 if (para_isspace(*begin))
116 continue;
117 break;
118 }
119 ret = -E_KEY_MARKER;
120 if (begin >= footer)
121 goto unmap;
122
123 key_size = footer - begin;
124 key = para_malloc(key_size + 1);
125 for (i = 0, j = 0; begin + i < footer; i++) {
126 if (para_isspace(begin[i]))
127 continue;
128 key[j++] = begin[i];
129 }
130 key[j] = '\0';
131 ret = base64_decode(key, j, (char **)&blob, &blob_size);
132 free(key);
133 if (ret < 0)
134 goto free_unmap;
135 ret = blob_size;
136 goto unmap;
137 free_unmap:
138 free(blob);
139 blob = NULL;
140 unmap:
141 ret2 = para_munmap(map, map_size);
142 if (ret >= 0 && ret2 < 0)
143 ret = ret2;
144 if (ret < 0) {
145 free(blob);
146 blob = NULL;
147 }
148 out:
149 *result = blob;
150 return ret;
151 }
152
153 /** ASN Types and their code. */
154 enum asn1_types {
155 /** The next object is an integer. */
156 ASN1_TYPE_INTEGER = 0x2,
157 /** Bit string object. */
158 ASN1_TYPE_BIT_STRING = 0x03,
159 /** Keys start with one big type sequence. */
160 ASN1_TYPE_SEQUENCE = 0x30,
161 };
162
163 /* bit 6 has value 0 */
164 static inline bool is_primitive(unsigned char c)
165 {
166 return (c & (1<<6)) == 0;
167 }
168
169 static inline bool is_primitive_integer(unsigned char c)
170 {
171 if (!is_primitive(c))
172 return false;
173 return (c & 0x1f) == ASN1_TYPE_INTEGER;
174 }
175
176 /* Bit 8 is zero (and bits 7-1 give the length) */
177 static inline bool is_short_form(unsigned char c)
178 {
179 return (c & 0x80) == 0;
180 }
181
182 static inline int get_short_form_length(unsigned char c)
183 {
184 return c & 0x7f;
185 }
186
187 static inline int get_long_form_num_length_bytes(unsigned char c)
188 {
189 return c & 0x7f;
190 }
191
192 static int find_pubkey_bignum_offset(const unsigned char *data, int len)
193 {
194 const unsigned char *p = data, *end = data + len;
195
196 /* the whole thing starts with one sequence */
197 if (*p != ASN1_TYPE_SEQUENCE)
198 return -E_ASN1_PARSE;
199 p++;
200 if (p >= end)
201 return -E_ASN1_PARSE;
202 if (is_short_form(*p))
203 p++;
204 else
205 p += 1 + get_long_form_num_length_bytes(*p);
206 if (p >= end)
207 return -E_ASN1_PARSE;
208 /* another sequence containing the object id, skip it */
209 if (*p != ASN1_TYPE_SEQUENCE)
210 return -E_ASN1_PARSE;
211 p++;
212 if (p >= end)
213 return -E_ASN1_PARSE;
214 if (!is_short_form(*p))
215 return -E_ASN1_PARSE;
216 p += 1 + get_short_form_length(*p);
217 if (p >= end)
218 return -E_ASN1_PARSE;
219 /* all numbers are wrapped in a bit string object that follows */
220 if (*p != ASN1_TYPE_BIT_STRING)
221 return -E_ASN1_PARSE;
222 p++;
223 if (p >= end)
224 return -E_ASN1_PARSE;
225 if (is_short_form(*p))
226 p++;
227 else
228 p += 1 + get_long_form_num_length_bytes(*p);
229 p++; /* skip number of unused bits in the bit string */
230 if (p >= end)
231 return -E_ASN1_PARSE;
232
233 /* next, we have a sequence of two integers (n and e) */
234 if (*p != ASN1_TYPE_SEQUENCE)
235 return -E_ASN1_PARSE;
236 p++;
237 if (p >= end)
238 return -E_ASN1_PARSE;
239 if (is_short_form(*p))
240 p++;
241 else
242 p += 1 + get_long_form_num_length_bytes(*p);
243 if (p >= end)
244 return -E_ASN1_PARSE;
245 if (*p != ASN1_TYPE_INTEGER)
246 return -E_ASN1_PARSE;
247 return p - data;
248 }
249
250 /*
251 * Returns: Number of bytes scanned. This may differ from the value returned via
252 * bn_bytes because the latter does not include the ASN.1 prefix and a leading
253 * zero is not considered as an additional byte for bn_bytes.
254 */
255 static int read_bignum(unsigned char *start, unsigned char *end, gcry_mpi_t *bn,
256 int *bn_bytes)
257 {
258 int i, bn_size;
259 gcry_error_t gret;
260 unsigned char *cp = start;
261
262 if (!is_primitive_integer(*cp))
263 return -E_BAD_PRIVATE_KEY;
264 cp++;
265 if (is_short_form(*cp)) {
266 bn_size = get_short_form_length(*cp);
267 cp++;
268 } else {
269 int num_bytes = get_long_form_num_length_bytes(*cp);
270 if (cp + num_bytes > end)
271 return -E_BAD_PRIVATE_KEY;
272 if (num_bytes > 4) /* nobody has such a large modulus */
273 return -E_BAD_PRIVATE_KEY;
274 cp++;
275 bn_size = 0;
276 for (i = 0; i < num_bytes; i++, cp++)
277 bn_size = (bn_size << 8) + *cp;
278 }
279 PARA_DEBUG_LOG("bn_size %d (0x%x)\n", bn_size, (unsigned)bn_size);
280 gret = gcry_mpi_scan(bn, GCRYMPI_FMT_STD, cp, bn_size, NULL);
281 if (gret) {
282 PARA_ERROR_LOG("%s while scanning n\n",
283 gcry_strerror(gcry_err_code(gret)));
284 return-E_MPI_SCAN;
285 }
286 /*
287 * Don't take the first leading zero into account for the size of the
288 * bignum.
289 */
290 if (*cp == '\0') {
291 cp++;
292 bn_size--;
293 }
294 if (bn_bytes)
295 *bn_bytes = bn_size;
296 cp += bn_size;
297 // unsigned char *buf;
298 // gcry_mpi_aprint(GCRYMPI_FMT_HEX, &buf, NULL, *bn);
299 // PARA_CRIT_LOG("bn: %s\n", buf);
300 return cp - start;
301 }
302
303 static int find_privkey_bignum_offset(const unsigned char *data, int len)
304 {
305 const unsigned char *p = data, *end = data + len;
306
307 /* like the public key, the whole thing is contained in a sequence */
308 if (*p != ASN1_TYPE_SEQUENCE)
309 return -E_ASN1_PARSE;
310 p++;
311 if (p >= end)
312 return -E_ASN1_PARSE;
313 if (is_short_form(*p))
314 p++;
315 else
316 p += 1 + get_long_form_num_length_bytes(*p);
317 if (p >= end)
318 return -E_ASN1_PARSE;
319
320 /* skip next integer */
321 if (*p != ASN1_TYPE_INTEGER)
322 return -E_ASN1_PARSE;
323 p++;
324 if (p >= end)
325 return -E_ASN1_PARSE;
326 if (is_short_form(*p))
327 p += 1 + get_short_form_length(*p);
328 else
329 p += 1 + get_long_form_num_length_bytes(*p);
330 if (p >= end)
331 return -E_ASN1_PARSE;
332 return p - data;
333 }
334
335 /** Private keys start with this header. */
336 #define PRIVATE_KEY_HEADER "-----BEGIN RSA PRIVATE KEY-----"
337 /** Private keys end with this footer. */
338 #define PRIVATE_KEY_FOOTER "-----END RSA PRIVATE KEY-----"
339
340 static int get_private_key(const char *key_file, struct asymmetric_key **result)
341 {
342 gcry_mpi_t n = NULL, e = NULL, d = NULL, p = NULL, q = NULL,
343 u = NULL;
344 unsigned char *blob, *cp, *end;
345 int blob_size, ret, n_size;
346 gcry_error_t gret;
347 size_t erroff;
348 gcry_sexp_t sexp;
349 struct asymmetric_key *key;
350
351 ret = decode_key(key_file, PRIVATE_KEY_HEADER, PRIVATE_KEY_FOOTER,
352 &blob);
353 if (ret < 0)
354 return ret;
355 blob_size = ret;
356 end = blob + blob_size;
357 ret = find_privkey_bignum_offset(blob, blob_size);
358 if (ret < 0)
359 goto free_blob;
360 PARA_INFO_LOG("reading RSA params at offset %d\n", ret);
361 cp = blob + ret;
362
363 ret = read_bignum(cp, end, &n, &n_size);
364 if (ret < 0)
365 goto free_blob;
366 cp += ret;
367
368 ret = read_bignum(cp, end, &e, NULL);
369 if (ret < 0)
370 goto release_n;
371 cp += ret;
372
373 ret = read_bignum(cp, end, &d, NULL);
374 if (ret < 0)
375 goto release_e;
376 cp += ret;
377
378 ret = read_bignum(cp, end, &p, NULL);
379 if (ret < 0)
380 goto release_d;
381 cp += ret;
382
383 ret = read_bignum(cp, end, &q, NULL);
384 if (ret < 0)
385 goto release_p;
386 cp += ret;
387 ret = read_bignum(cp, end, &u, NULL);
388 if (ret < 0)
389 goto release_q;
390 /*
391 * OpenSSL uses slightly different parameters than gcrypt. To use these
392 * parameters we need to swap the values of p and q and recompute u.
393 */
394 if (gcry_mpi_cmp(p, q) > 0) {
395 gcry_mpi_swap(p, q);
396 gcry_mpi_invm(u, p, q);
397 }
398 gret = gcry_sexp_build(&sexp, &erroff, RSA_PRIVKEY_SEXP,
399 n, e, d, p, q, u);
400
401 if (gret) {
402 PARA_ERROR_LOG("offset %zu: %s\n", erroff,
403 gcry_strerror(gcry_err_code(gret)));
404 ret = -E_SEXP_BUILD;
405 goto release_u;
406 }
407 key = para_malloc(sizeof(*key));
408 key->sexp = sexp;
409 *result = key;
410 ret = n_size * 8;
411 PARA_INFO_LOG("succesfully read %d bit private key\n", ret);
412 release_u:
413 gcry_mpi_release(u);
414 release_q:
415 gcry_mpi_release(q);
416 release_p:
417 gcry_mpi_release(p);
418 release_d:
419 gcry_mpi_release(d);
420 release_e:
421 gcry_mpi_release(e);
422 release_n:
423 gcry_mpi_release(n);
424 free_blob:
425 free(blob);
426 return ret;
427 }
428
429 /** Public keys start with this header. */
430 #define PUBLIC_KEY_HEADER "-----BEGIN PUBLIC KEY-----"
431 /** Public keys end with this footer. */
432 #define PUBLIC_KEY_FOOTER "-----END PUBLIC KEY-----"
433
434 static int get_asn_public_key(const char *key_file, struct asymmetric_key **result)
435 {
436 gcry_mpi_t n = NULL, e = NULL;
437 unsigned char *blob, *cp, *end;
438 int blob_size, ret, n_size;
439 gcry_error_t gret;
440 size_t erroff;
441 gcry_sexp_t sexp;
442 struct asymmetric_key *key;
443
444 ret = decode_key(key_file, PUBLIC_KEY_HEADER, PUBLIC_KEY_FOOTER,
445 &blob);
446 if (ret < 0)
447 return ret;
448 blob_size = ret;
449 end = blob + blob_size;
450 ret = find_pubkey_bignum_offset(blob, blob_size);
451 if (ret < 0)
452 goto free_blob;
453 PARA_DEBUG_LOG("decoding public RSA params at offset %d\n", ret);
454 cp = blob + ret;
455
456 ret = read_bignum(cp, end, &n, &n_size);
457 if (ret < 0)
458 goto free_blob;
459 cp += ret;
460
461 ret = read_bignum(cp, end, &e, NULL);
462 if (ret < 0)
463 goto release_n;
464
465 gret = gcry_sexp_build(&sexp, &erroff, RSA_PUBKEY_SEXP, n, e);
466 if (gret) {
467 PARA_ERROR_LOG("offset %zu: %s\n", erroff,
468 gcry_strerror(gcry_err_code(gret)));
469 ret = -E_SEXP_BUILD;
470 goto release_e;
471 }
472 key = para_malloc(sizeof(*key));
473 key->sexp = sexp;
474 key->num_bytes = n_size;
475 *result = key;
476 ret = n_size;
477 PARA_INFO_LOG("successfully read %d bit asn public key\n", n_size * 8);
478
479 release_e:
480 gcry_mpi_release(e);
481 release_n:
482 gcry_mpi_release(n);
483 free_blob:
484 free(blob);
485 return ret;
486 }
487
488 static int get_ssh_public_key(unsigned char *data, int size, gcry_sexp_t *result)
489 {
490 int ret;
491 gcry_error_t gret;
492 unsigned char *blob = NULL, *p, *end;
493 size_t nr_scanned, erroff, decoded_size;
494 gcry_mpi_t e = NULL, n = NULL;
495
496 PARA_DEBUG_LOG("decoding %d byte public rsa-ssh key\n", size);
497 ret = uudecode((char *)data, size, (char **)&blob, &decoded_size);
498 if (ret < 0)
499 goto free_blob;
500 end = blob + decoded_size;
501 dump_buffer("decoded key", blob, decoded_size);
502 ret = check_ssh_key_header(blob, decoded_size);
503 if (ret < 0)
504 goto free_blob;
505 p = blob + ret;
506 ret = -E_SSH_PARSE;
507 if (p >= end)
508 goto free_blob;
509 PARA_DEBUG_LOG("scanning modulus and public exponent\n");
510 gret = gcry_mpi_scan(&e, GCRYMPI_FMT_SSH, p, end - p, &nr_scanned);
511 if (gret) {
512 ret = -E_MPI_SCAN;
513 PARA_CRIT_LOG("%s\n", gcry_strerror(gcry_err_code(gret)));
514 goto free_blob;
515 }
516 PARA_DEBUG_LOG("scanned e (%zu bytes)\n", nr_scanned);
517 // gcry_mpi_aprint(GCRYMPI_FMT_HEX, &buf, NULL, rsa_e);
518 // PARA_CRIT_LOG("e: %s\n", buf);
519 p += nr_scanned;
520 if (p >= end)
521 goto release_e;
522 gret = gcry_mpi_scan(&n, GCRYMPI_FMT_SSH, p, end - p, &nr_scanned);
523 if (gret) {
524 ret = -E_MPI_SCAN;
525 PARA_ERROR_LOG("%s\n", gcry_strerror(gcry_err_code(gret)));
526 goto release_e;
527 }
528 PARA_DEBUG_LOG("scanned n (%zu bytes)\n", nr_scanned);
529 // gcry_mpi_aprint(GCRYMPI_FMT_HEX, &buf, NULL, rsa_n);
530 // PARA_CRIT_LOG("n: %s\n", buf);
531 gret = gcry_sexp_build(result, &erroff, RSA_PUBKEY_SEXP, n, e);
532 if (gret) {
533 PARA_ERROR_LOG("offset %zu: %s\n", erroff,
534 gcry_strerror(gcry_err_code(gret)));
535 ret = -E_SEXP_BUILD;
536 goto release_n;
537 }
538 ret = nr_scanned / 32 * 32;
539 PARA_INFO_LOG("successfully read %d bit ssh public key\n", ret * 8);
540 release_n:
541 gcry_mpi_release(n);
542 release_e:
543 gcry_mpi_release(e);
544 free_blob:
545 free(blob);
546 return ret;
547 }
548
549 int get_asymmetric_key(const char *key_file, int private,
550 struct asymmetric_key **result)
551 {
552 int ret, ret2;
553 void *map;
554 size_t map_size;
555 unsigned char *start, *end;
556 gcry_sexp_t sexp;
557 struct asymmetric_key *key;
558
559 if (private)
560 return get_private_key(key_file, result);
561 ret = mmap_full_file(key_file, O_RDONLY, &map, &map_size, NULL);
562 if (ret < 0)
563 return ret;
564 ret = is_ssh_rsa_key(map, map_size);
565 if (!ret) {
566 ret = para_munmap(map, map_size);
567 if (ret < 0)
568 return ret;
569 return get_asn_public_key(key_file, result);
570 }
571 start = map + ret;
572 end = map + map_size;
573 ret = -E_SSH_PARSE;
574 if (start >= end)
575 goto unmap;
576 ret = get_ssh_public_key(start, end - start, &sexp);
577 if (ret < 0)
578 goto unmap;
579 key = para_malloc(sizeof(*key));
580 key->num_bytes = ret;
581 key->sexp = sexp;
582 *result = key;
583 unmap:
584 ret2 = para_munmap(map, map_size);
585 if (ret >= 0 && ret2 < 0)
586 ret = ret2;
587 return ret;
588 }
589
590 void free_asymmetric_key(struct asymmetric_key *key)
591 {
592 if (!key)
593 return;
594 gcry_sexp_release(key->sexp);
595 free(key);
596 }
597
598 static int decode_rsa(gcry_sexp_t sexp, unsigned char *outbuf, size_t *nbytes)
599 {
600 const char *p = gcry_sexp_nth_data(sexp, 1, nbytes);
601
602 if (!p)
603 return -E_RSA_DECODE;
604 memcpy(outbuf, p, *nbytes);
605 return 1;
606 }
607
608 int priv_decrypt(const char *key_file, unsigned char *outbuf,
609 unsigned char *inbuf, int inlen)
610 {
611 gcry_error_t gret;
612 int ret;
613 struct asymmetric_key *priv;
614 gcry_mpi_t in_mpi = NULL;
615 gcry_sexp_t in, out, priv_key;
616 size_t nbytes;
617
618 ret = check_key_file(key_file, true);
619 if (ret < 0)
620 return ret;
621 PARA_INFO_LOG("decrypting %d byte input\n", inlen);
622 /* key_file -> asymmetric key priv */
623 ret = get_private_key(key_file, &priv);
624 if (ret < 0)
625 return ret;
626
627 /* asymmetric key priv -> sexp priv_key */
628 ret = -E_SEXP_FIND;
629 priv_key = gcry_sexp_find_token(priv->sexp, "private-key", 0);
630 if (!priv_key)
631 goto free_key;
632
633 /* inbuf -> in_mpi */
634 gret = gcry_mpi_scan(&in_mpi, GCRYMPI_FMT_USG, inbuf,
635 inlen, NULL);
636 if (gret) {
637 PARA_ERROR_LOG("%s\n", gcrypt_strerror(gret));
638 ret = -E_MPI_SCAN;
639 goto key_release;
640 }
641 /* in_mpi -> in sexp */
642 gret = gcry_sexp_build(&in, NULL, RSA_DECRYPT_SEXP, in_mpi);
643 if (gret) {
644 PARA_ERROR_LOG("%s\n", gcrypt_strerror(gret));
645 ret = -E_SEXP_BUILD;
646 goto in_mpi_release;
647 }
648
649 /* rsa decryption: in sexp -> out sexp */
650 gret = gcry_pk_decrypt(&out, in, priv_key);
651 if (gret) {
652 PARA_ERROR_LOG("decrypt: %s\n", gcrypt_strerror(gret));
653 ret = -E_SEXP_DECRYPT;
654 goto in_release;
655 }
656 ret = decode_rsa(out, outbuf, &nbytes);
657 if (ret < 0)
658 goto out_release;
659 PARA_INFO_LOG("successfully decrypted %zu byte message\n", nbytes);
660 ret = nbytes;
661 out_release:
662 gcry_sexp_release(out);
663 in_release:
664 gcry_sexp_release(in);
665 in_mpi_release:
666 gcry_mpi_release(in_mpi);
667 key_release:
668 gcry_sexp_release(priv_key);
669 free_key:
670 free_asymmetric_key(priv);
671 return ret;
672 }
673
674 int pub_encrypt(struct asymmetric_key *pub, unsigned char *inbuf,
675 unsigned len, unsigned char *outbuf)
676 {
677 gcry_error_t gret;
678 gcry_sexp_t pub_key, in, out, out_a;
679 gcry_mpi_t out_mpi = NULL;
680 size_t nbytes;
681 int ret;
682
683 PARA_INFO_LOG("encrypting %u byte input with %d-byte key\n", len, pub->num_bytes);
684
685 /* get pub key */
686 pub_key = gcry_sexp_find_token(pub->sexp, "public-key", 0);
687 if (!pub_key)
688 return -E_SEXP_FIND;
689 gret = gcry_sexp_build(&in, NULL, "(data(flags oaep)(value %b))", len, inbuf);
690 if (gret) {
691 PARA_ERROR_LOG("%s\n", gcrypt_strerror(gret));
692 ret = -E_SEXP_BUILD;
693 goto key_release;
694 }
695 /* rsa sexp encryption: in -> out */
696 gret = gcry_pk_encrypt(&out, in, pub_key);
697 if (gret) {
698 PARA_ERROR_LOG("%s\n", gcrypt_strerror(gret));
699 ret = -E_SEXP_ENCRYPT;
700 goto in_release;
701 }
702 /* extract a, an MPI with the result of the RSA operation */
703 ret = -E_SEXP_FIND;
704 out_a = gcry_sexp_find_token(out, "a", 0);
705 if (!out_a)
706 goto out_release;
707 /* convert sexp out_a -> out_mpi */
708 out_mpi = gcry_sexp_nth_mpi(out_a, 1, GCRYMPI_FMT_USG);
709 if (!out_mpi) {
710 ret = -E_SEXP_FIND;
711 goto out_a_release;
712 }
713 gret = gcry_mpi_print(GCRYMPI_FMT_USG, outbuf, 512 /* FIXME */, &nbytes, out_mpi);
714 if (gret) {
715 PARA_ERROR_LOG("%s\n", gcrypt_strerror(gret));
716 ret = -E_SEXP_ENCRYPT;
717 goto out_mpi_release;
718 }
719 PARA_INFO_LOG("encrypted buffer is %zu bytes\n", nbytes);
720 dump_buffer("enc buf", outbuf, nbytes);
721 ret = nbytes;
722
723 out_mpi_release:
724 gcry_mpi_release(out_mpi);
725 out_a_release:
726 gcry_sexp_release(out_a);
727 out_release:
728 gcry_sexp_release(out);
729 in_release:
730 gcry_sexp_release(in);
731 key_release:
732 gcry_sexp_release(pub_key);
733 return ret;
734 }
735
736 struct stream_cipher {
737 gcry_cipher_hd_t handle;
738 };
739
740 struct stream_cipher *sc_new(const unsigned char *data, int len,
741 bool use_aes)
742 {
743 gcry_error_t gret;
744 struct stream_cipher *sc = para_malloc(sizeof(*sc));
745
746 if (use_aes) {
747 assert(len >= 2 * AES_CRT128_BLOCK_SIZE);
748 gret = gcry_cipher_open(&sc->handle, GCRY_CIPHER_AES128,
749 GCRY_CIPHER_MODE_CTR, 0);
750 assert(gret == 0);
751 gret = gcry_cipher_setkey(sc->handle, data,
752 AES_CRT128_BLOCK_SIZE);
753 assert(gret == 0);
754 gret = gcry_cipher_setctr(sc->handle,
755 data + AES_CRT128_BLOCK_SIZE, AES_CRT128_BLOCK_SIZE);
756 assert(gret == 0);
757 return sc;
758 }
759 gret = gcry_cipher_open(&sc->handle, GCRY_CIPHER_ARCFOUR,
760 GCRY_CIPHER_MODE_STREAM, 0);
761 if (gret) {
762 PARA_ERROR_LOG("%s\n", gcrypt_strerror(gret));
763 free(sc);
764 return NULL;
765 }
766 gret = gcry_cipher_setkey(sc->handle, data, (size_t)len);
767 assert(gret == 0);
768 return sc;
769 }
770
771 void sc_free(struct stream_cipher *sc)
772 {
773 if (!sc)
774 return;
775 gcry_cipher_close(sc->handle);
776 free(sc);
777 }
778
779 void sc_crypt(struct stream_cipher *sc, struct iovec *src, struct iovec *dst)
780 {
781 gcry_cipher_hd_t handle = sc->handle;
782 gcry_error_t gret;
783
784 /* perform in-place encryption */
785 *dst = *src;
786 gret = gcry_cipher_encrypt(handle, src->iov_base, src->iov_len,
787 NULL, 0);
788 assert(gret == 0);
789 }