summaryrefslogtreecommitdiff
path: root/cipher/ac.c
blob: ffc80daf61f05f7bdeb2122b9a42bae656070300 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
/* ac.c - Alternative interface for asymmetric cryptography.
   Copyright (C) 2003 Free Software Foundation, Inc.
 
   This file is part of Libgcrypt.
  
   Libgcrypt is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser general Public License as
   published by the Free Software Foundation; either version 2.1 of
   the License, or (at your option) any later version.
  
   Libgcrypt is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Lesser General Public License for more details.
  
   You should have received a copy of the GNU Lesser General Public
   License along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */

#include <config.h>
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stddef.h>

#include "g10lib.h"
#include "cipher.h"



/* At the moment the ac interface is a wrapper around the pk
   interface, but this might change somewhen in the future, depending
   on how much people prefer the ac interface.  */

/* Mapping of flag numbers to the according strings as it is expected
   for S-expressions.  */
struct number_string
{
  int number;
  const char *string;
} gcry_ac_flags[] =
  {
    { GCRY_AC_FLAG_DATA_NO_BLINDING, "no-blinding" },
    { 0, NULL },
  };

/* The positions in this list correspond to the values contained in
   the gcry_ac_key_type_t enumeration list.  */
const char *ac_key_identifiers[] =
  {
    "private-key",
    "public-key",
  };

/* These specifications are needed for key-pair generation; the caller
   is allowed to pass additional, algorithm-specific `specs' to
   gcry_ac_key_pair_generate.  This list is used for decoding the
   provided values according to the selected algorithm.  */
struct gcry_ac_key_generate_spec
{
  int algorithm;		/* Algorithm for which this flag is
				   relevant.  */
  const char *name;		/* Name of this flag.  */
  size_t offset;		/* Offset in the cipher-specific spec
				   structure at which the MPI value
				   associated with this flag is to be
				   found.  */
} gcry_ac_key_generate_specs[] =
  {
    { GCRY_AC_RSA, "rsa-use-e", offsetof (gcry_ac_key_spec_rsa_t, e) },
    { 0 },
  };

/* Handle structure.  */
struct gcry_ac_handle
{
  int algorithm;		/* Algorithm ID associated with this
				   handle.  */
  const char *algorithm_name;	/* Name of the algorithm.  */
  unsigned int flags;		/* Flags, not used yet.  */
  gcry_module_t module;	        /* Reference to the algorithm
				   module.  */
};

/* A named MPI value.  */
typedef struct gcry_ac_mpi
{
  const char *name;
  gcry_mpi_t mpi;
} gcry_ac_mpi_t;

/* A data set, that is simply a list of named MPI values.  */
struct gcry_ac_data
{
  gcry_ac_mpi_t *data;		/* List of named values.      */
  unsigned int data_n;		/* Number of values in DATA.  */
};

/* The key in `native' ac form and as an S-expression. */
struct gcry_ac_key
{
  gcry_ac_data_t data;		/* Data in native ac structure.  */
  gcry_sexp_t data_sexp;	/* Data as an S-expression.      */
  gcry_ac_key_type_t type;	/* Type of the key.              */
};

/* Two keys.  */
struct gcry_ac_key_pair
{
  gcry_ac_key_t public;
  gcry_ac_key_t secret;
};



/*
 * Primitive functions for the manipulation of `data sets'.
 */

/* Return in AC_MPI a pointer to the named MPI contained in DATA that
   is labelled with NAME or NULL in case there is no MPI with the that
   name.  */
static void
gcry_ac_data_search (gcry_ac_data_t data,
		     const char *name,
		     gcry_ac_mpi_t **ac_mpi)
{
  gcry_ac_mpi_t *ac_mpi_found = NULL;
  int i;

  /* Search.  */
  for (i = 0; i < data->data_n; i++)
    if (! strcmp (name, data->data[i].name))
      ac_mpi_found = &data->data[i];

  *ac_mpi = ac_mpi_found;
}

/* Add MPI to DATA, with the label being NAME.  */
static gcry_err_code_t
gcry_ac_data_add (gcry_ac_data_t data,
		  const char *name, gcry_mpi_t mpi)
{
  gcry_err_code_t err = GPG_ERR_NO_ERROR;
  gcry_ac_mpi_t *ac_mpis = NULL;

  /* Allocate.  */
  ac_mpis = realloc (data->data,
		     sizeof (gcry_ac_mpi_t) * (data->data_n + 1));
  if (! ac_mpis)
    err = gpg_err_code_from_errno (errno);

  if (! err)
    {
      /* Fill. */
      if (ac_mpis != data->data)
	data->data = ac_mpis;
      data->data[data->data_n].name = name;
      data->data[data->data_n].mpi = mpi;
      data->data_n++;
    }

  return err;
}

/* Create a copy of the data set DATA and store it in DATA_CP.  */
static gcry_err_code_t
gcry_ac_data_copy_internal (gcry_ac_data_t *data_cp, gcry_ac_data_t data)
{
  gcry_err_code_t err = GPG_ERR_NO_ERROR;
  gcry_ac_data_t data_new = NULL;
  int i = 0;

  /* Allocate data set.  */
  err = _gcry_malloc (sizeof (struct gcry_ac_data), 0, (void **) &data_new);
  if (! err)
    data_new->data_n = data->data_n;

  if (! err)
    /* Allocate space for named MPIs.  */
    err = _gcry_malloc (sizeof (gcry_ac_mpi_t) * data->data_n, 0,
			(void **) &data_new->data);

  if (! err)
    {
      /* Copy named MPIs.  */
      
      for (i = 0; i < data_new->data_n && (! err); i++)
	{
	  data_new->data[i].name = NULL;
	  data_new->data[i].mpi = NULL;

	  /* Name.  */
	  data_new->data[i].name = strdup (data->data[i].name);
	  if (! data_new->data[i].name)
	    err = gpg_err_code_from_errno (errno);

	  if (! err)
	    {
	      /* MPI.  */
	      data_new->data[i].mpi = gcry_mpi_copy (data->data[i].mpi);
	      if (! data_new->data[i].mpi)
		err = gpg_err_code_from_errno (errno);
	    }
	}
    }

  if (! err)
    /* Copy out.  */
    *data_cp = data_new;
  else
    {
      /* Deallocate resources.  */
      if (data_new)
	{
	  if (data_new->data)
	    {
	      for (; i >= 0; i--)
		{
		  if (data_new->data[i].name)
		    free ((void *) data_new->data[i].name);
		  if (data_new->data[i].mpi)
		    gcry_mpi_release (data_new->data[i].mpi);
		}
	      gcry_free (data_new->data);
	    }
	  gcry_free (data_new);
	}
    }

  return err;
}



/* 
 * Functions for converting data between the native ac and the
 * S-expression structure.
 */

/* Extract the S-Expression DATA_SEXP into DATA under the control of
   TYPE and NAME.  This function assumes that S-Expressions are of the
   following structure:

     (IDENTIFIER <data to be ignored>
                 (ALGORITHM <list of named MPI values>))

  IDENTIFIER is one of `private-key', `public-key', `enc-val',
  `sig-val'; ALGORITHM is the name of the algorithm used.  */
static gcry_err_code_t
gcry_ac_data_extract (const char *identifier, const char *algorithm,
		      gcry_sexp_t data_sexp, gcry_ac_data_t *data)
{
  gcry_err_code_t err = GPG_ERR_NO_ERROR;
  gcry_sexp_t data_element_sexp = NULL;
  gcry_sexp_t inner_data_sexp = NULL;
  size_t inner_data_n;

  const char *name;
  size_t name_n;

  gcry_mpi_t data_elem_mpi = NULL;
  char *data_elem_name = NULL;

  gcry_ac_data_t data_new = NULL;

  int i = 0;

  /* Verify that the S-expression contains the correct identifier.  */
  name = gcry_sexp_nth_data (data_sexp, 0, &name_n);
  if (! name)
    err = GPG_ERR_INTERNAL;
  else if (strncmp (identifier, name, name_n))
    err = GPG_ERR_INTERNAL;

  if (! err)
    {
      /* Extract inner S-expression.  */
      inner_data_sexp = gcry_sexp_find_token (data_sexp, algorithm, 0);
      if (! inner_data_sexp)
	err = GPG_ERR_INTERNAL;
      else
	/* Count data elements, this includes the name of the
	   algorithm.  */
	inner_data_n = gcry_sexp_length (inner_data_sexp);
    }

  if (! err)
    {
      /* Allocate new data set.  */
      data_new = gcry_malloc (sizeof (struct gcry_ac_data));
      if (! data_new)
	err = gpg_err_code_from_errno (errno);
      else
	{
	  data_new->data = gcry_malloc (sizeof (gcry_ac_mpi_t) * (inner_data_n - 1));
	  if (! data_new->data)
	    err = gpg_err_code_from_errno (errno);
	}
    }

  if (! err)
    {
      /* Iterate through list of data elements and add them to the
	 data set.  */

      for (i = 1; i < inner_data_n; i++)
	{
	  data_new->data[i - 1].name = NULL;
	  data_new->data[i - 1].mpi = NULL;

	  /* Get the S-expression of the named MPI, that contains the
	     name and the MPI value.  */
	  data_element_sexp = gcry_sexp_nth (inner_data_sexp, i);
	  if (! data_element_sexp)
	    err = GPG_ERR_INTERNAL;

	  if (! err)
	    {
	      /* Extract the name.  */
	      name = gcry_sexp_nth_data (data_element_sexp, 0, &name_n);
	      if (! name)
		err = GPG_ERR_INTERNAL;
	    }

	  if (! err)
	    {
	      /* Extract the MPI value.  */
	      data_elem_mpi = gcry_sexp_nth_mpi (data_element_sexp, 1,
						 GCRYMPI_FMT_USG);
	      if (! data_elem_mpi)
		err = GPG_ERR_INTERNAL;
	    }

	  if (! err)
	    {
	      /* Duplicate the name.  */
	      data_elem_name = gcry_malloc (name_n + 1);
	      if (! data_elem_name)
		
		err = gpg_err_code_from_errno (errno);
	      else
		{
		  strncpy (data_elem_name, name, name_n);
		  data_elem_name[name_n] = 0;
		}
	    }

	  /* Done.  */

	  if (data_element_sexp)
	    gcry_sexp_release (data_element_sexp);

	  if (! err)
	    {
	      data_new->data[i - 1].name = data_elem_name;
	      data_new->data[i - 1].mpi = data_elem_mpi;
	    }
	  else
	    break;
	}
    }

  if (! err)
    {
      /* Copy out.  */
      data_new->data_n = inner_data_n - 1;
      *data = data_new;
    }
  else
    {
      /* Deallocate resources.  */

      if (data_new)
	{
	  if (data_new->data)
	    {
	      int j;
	     
	      for (j = 0; j < i - 1; j++)
		{
		  if (data_new->data[j].name)
		    gcry_free ((void *) data_new->data[j].name);
		  if (data_new->data[j].mpi)
		    gcry_mpi_release (data_new->data[j].mpi);
		}

	      gcry_free (data_new->data);
	    }
	  gcry_free (data_new);
	}
    }

  return err;
}

/* Construct an S-expression from the DATA and store it in
   DATA_SEXP. The S-expression will be of the following structure:

     (IDENTIFIER [(flags [...])]
                 (ALGORITHM <list of named MPI values>))  */
static gcry_err_code_t
gcry_ac_data_construct (const char *identifier, int include_flags,
			unsigned int flags, const char *algorithm,
			gcry_ac_data_t data, gcry_sexp_t *data_sexp)
{
  gcry_err_code_t err = GPG_ERR_NO_ERROR;
  void **arg_list = NULL;

  gcry_sexp_t data_sexp_new = NULL;

  size_t data_format_n = 0;
  char *data_format = NULL;

  int i;

  /* We build a list of arguments to pass to
     gcry_sexp_build_array().  */
  arg_list = gcry_malloc (sizeof (void *) * data->data_n);
  if (! arg_list)
    err = gpg_err_code_from_errno (errno);
  else
    /* Fill list with MPIs.  */
    for (i = 0; i < data->data_n; i++)
      arg_list[i] = (void *) &data->data[i].mpi;

  if (! err)
    {
      /* Calculate size of format string.  */

      data_format_n = 5 + (include_flags ? 7 : 0) + strlen (identifier) + strlen (algorithm);
      for (i = 0; i < data->data_n; i++)
	/* Per-element sizes.  */
	data_format_n += 4 + strlen (data->data[i].name);

      if (include_flags)
	/* Add flags.  */
	for (i = 0; gcry_ac_flags[i].number; i++)
	  if (flags & gcry_ac_flags[i].number)
	    data_format_n += strlen (gcry_ac_flags[i].string) + 1;

      /* Done.  */
      data_format = gcry_malloc (data_format_n);
      if (! data_format)
	err = gpg_err_code_from_errno (errno);
    }

  if (! err)
    {
      /* Construct the format string.  */

      *data_format = 0;
      strcat (data_format, "(");
      strcat (data_format, identifier);
      if (include_flags)
	{
	  strcat (data_format, "(flags");
	  for (i = 0; gcry_ac_flags[i].number; i++)
	    if (flags & gcry_ac_flags[i].number)
	      {
		strcat (data_format, " ");
		strcat (data_format, gcry_ac_flags[i].string);
	      }
	  strcat (data_format, ")");
	}
      strcat (data_format, "(");
      strcat (data_format, algorithm);
      for (i = 0; i < data->data_n; i++)
	{
	  strcat (data_format, "(");
	  strcat (data_format, data->data[i].name);
	  strcat (data_format, "%m)");
	}
      strcat (data_format, "))");

      /* Create final S-expression.  */
      err = gcry_sexp_build_array (&data_sexp_new, NULL,
				   data_format, arg_list);
    }

  if (err)
    {
      /* Deallocate resources.  */

      if (arg_list)
	gcry_free (arg_list);
      if (data_format)
	gcry_free (data_format);
      if (data_sexp_new)
	gcry_sexp_release (data_sexp_new);
    }

  else
    /* Copy-out.  */
    *data_sexp = data_sexp_new;

  return err;
}



/* 
 * Functions for working with data sets.
 */

/* Creates a new, empty data set and stores it in DATA.  */
gcry_error_t
gcry_ac_data_new (gcry_ac_data_t *data)
{
  gcry_err_code_t err = GPG_ERR_NO_ERROR;
  gcry_ac_data_t data_new = NULL;

  data_new = gcry_malloc (sizeof (struct gcry_ac_data));
  if (! data_new)
    err = gpg_err_code_from_errno (errno);

  if (! err)
    {
      data_new->data = NULL;
      data_new->data_n = 0;
      *data = data_new;
    }

  return gcry_error (err);
}

/* Destroys the data set DATA.  */
void
gcry_ac_data_destroy (gcry_ac_data_t data)
{
  int i;

  for (i = 0; i < data->data_n; i++)
    {
      gcry_free ((void *) data->data[i].name);
      gcry_mpi_release (data->data[i].mpi);
    }
  gcry_free (data->data);
  gcry_free (data);
}

/* Adds the value MPI to the data set DATA with the label NAME.  If
   there is already a value with that label, it is replaced, otherwise
   a new value is added. */
gcry_error_t
gcry_ac_data_set (gcry_ac_data_t data,
		  const char *name, gcry_mpi_t mpi)
{
  gcry_err_code_t err = GPG_ERR_NO_ERROR;
  gcry_ac_mpi_t *ac_mpi;

  gcry_ac_data_search (data, name, &ac_mpi);
  if (ac_mpi)
    {
      /* An entry for NAME does already exist, replace it.  */
      if (ac_mpi->mpi != mpi)
	{
	  gcry_mpi_release (ac_mpi->mpi);
	  ac_mpi->mpi = mpi;
	}
    }
  else
    {
      /* Create a new entry.  */

      gcry_mpi_t mpi_cp = NULL;
      char *name_cp = NULL;

      name_cp = strdup (name);
      if (name_cp)
	mpi_cp = gcry_mpi_copy (mpi);
      if (! (name_cp && mpi_cp))
	err = gpg_err_code_from_errno (errno);

      if (! err)
	err = gcry_ac_data_add (data, name_cp, mpi_cp);

      if (err)
	{
	  if (name_cp)
	    gcry_free (name_cp);
	  if (mpi_cp)
	    gcry_mpi_release (mpi_cp);
	}
    }

  return gcry_error (err);
}

/* Create a copy of the data set DATA and store it in DATA_CP.  */
gcry_error_t
gcry_ac_data_copy (gcry_ac_data_t *data_cp, gcry_ac_data_t data)
{
  gcry_err_code_t err = GPG_ERR_NO_ERROR;

  err = gcry_ac_data_copy_internal (data_cp, data);

  return gcry_error (err);
}

/* Returns the number of named MPI values inside of the data set
   DATA.  */
unsigned int
gcry_ac_data_length (gcry_ac_data_t data)
{
  return data->data_n;
}

/* Stores the value labelled with NAME found in the data set DATA in
   MPI.  The returned MPI value will be released in case
   gcry_ac_data_set is used to associate the label NAME with a
   different MPI value.  */
gcry_error_t
gcry_ac_data_get_name (gcry_ac_data_t data, const char *name,
		       gcry_mpi_t *mpi)
{
  gcry_err_code_t err = GPG_ERR_NO_DATA;
  gcry_mpi_t mpi_found = NULL;
  int i;
  
  for (i = 0; i < data->data_n && (! mpi_found); i++)
    if (! strcmp (data->data[i].name, name))
      {
	mpi_found = data->data[i].mpi;
	err = GPG_ERR_NO_ERROR;
      }

  if (! err)
    *mpi = mpi_found;

  return gcry_error (err);
}

/* Stores in NAME and MPI the named MPI value contained in the data
   set DATA with the index INDEX.  NAME or MPI may be NULL.  The
   returned MPI value will be released in case gcry_ac_data_set is
   used to associate the label NAME with a different MPI value.  */
gcry_error_t
gcry_ac_data_get_index (gcry_ac_data_t data, unsigned int index,
			const char **name, gcry_mpi_t *mpi)
{
  gcry_err_code_t err = GPG_ERR_NO_ERROR;

  if (index < data->data_n)
    {
      if (name)
	*name = data->data[index].name;
      if (mpi)
	*mpi = data->data[index].mpi;
    }
  else
    err = GPG_ERR_NO_DATA;

  return gcry_error (err);
}

/* Destroys any values contained in the data set DATA.  */
void
gcry_ac_data_clear (gcry_ac_data_t data)
{
  gcry_free (data->data);
  data->data = NULL;
  data->data_n = 0;
}



/*
 * Handle management.
 */

/* Creates a new handle for the algorithm ALGORITHM and store it in
   HANDLE.  FLAGS is not used yet.  */
gcry_error_t
gcry_ac_open (gcry_ac_handle_t *handle,
	      gcry_ac_id_t algorithm, unsigned int flags)
{
  gcry_err_code_t err = GPG_ERR_NO_ERROR;
  gcry_module_t module = NULL;
  gcry_ac_handle_t handle_new;
  const char *algorithm_name;

  /* Get name.  */
  algorithm_name = _gcry_pk_aliased_algo_name (algorithm);
  if (! *algorithm_name)
    err = GPG_ERR_PUBKEY_ALGO;

  if (! err)
    /* Acquire reference to the pubkey module.  */
    err = _gcry_pk_module_lookup (algorithm, &module);
  
  if (! err)
    {
      /* Allocate.  */
      handle_new = gcry_malloc (sizeof (struct gcry_ac_handle));
      if (! handle_new)
	err = gpg_err_code_from_errno (errno);
    }

  if (! err)
    {
      /* Done.  */
      handle_new->algorithm = algorithm;
      handle_new->algorithm_name = algorithm_name;
      handle_new->flags = flags;
      handle_new->module = module;
      *handle = handle_new;
    }
  else
    {
      /* Deallocate resources.  */
      if (module)
	_gcry_pk_module_release (module);
    }

  return gcry_error (err);
}

/* Destroys the handle HANDLE.  */
void
gcry_ac_close (gcry_ac_handle_t handle)
{
  /* Release reference to pubkey module.  */
  _gcry_pk_module_release (handle->module);
  gcry_free (handle);
}



/* 
 * Key management.
 */

/* Creates a new key of type TYPE, consisting of the MPI values
   contained in the data set DATA and stores it in KEY.  */
gcry_error_t
gcry_ac_key_init (gcry_ac_key_t *key,
		  gcry_ac_handle_t handle,
		  gcry_ac_key_type_t type,
		  gcry_ac_data_t data)
{
  gcry_err_code_t err = GPG_ERR_NO_ERROR;
  gcry_ac_data_t data_new = NULL;
  gcry_sexp_t data_sexp = NULL;
  gcry_ac_key_t key_new = NULL;

  /* Allocate.  */
  key_new = gcry_malloc (sizeof (struct gcry_ac_key));
  if (! key_new)
    err = gpg_err_code_from_errno (errno);

  if (! err)
    /* Create S-expression from data set.  */
    err = gcry_ac_data_construct (ac_key_identifiers[type], 0, 0,
				  handle->algorithm_name, data, &data_sexp);

  if (! err)
    /* Copy data set.  */
    err = gcry_ac_data_copy_internal (&data_new, data);

  if (! err)
    {
      /* Done.  */
      key_new->data_sexp = data_sexp;
      key_new->data = data_new;
      key_new->type = type;
      *key = key_new;
    }
  else
    {
      /* Deallocate resources.  */
      if (key_new)
	gcry_free (key_new);
      if (data_sexp)
	gcry_sexp_release (data_sexp);
    }

  return gcry_error (err);
}

/* Generates a new key pair via the handle HANDLE of NBITS bits and
   stores it in KEY_PAIR.  In case non-standard settings are wanted, a
   pointer to a structure of type gcry_ac_key_spec_<algorithm>_t,
   matching the selected algorithm, can be given as KEY_SPEC.  */
gcry_error_t
gcry_ac_key_pair_generate (gcry_ac_handle_t handle,
			   gcry_ac_key_pair_t *key_pair,
			   unsigned int nbits,
			   void *key_spec)
{
  gcry_err_code_t err = GPG_ERR_NO_ERROR;

  gcry_ac_key_pair_t key_pair_new = NULL;

  gcry_sexp_t genkey_sexp_request = NULL;
  gcry_sexp_t genkey_sexp_reply = NULL;

  char *genkey_format = NULL;
  size_t genkey_format_n = 0;

  void **arg_list = NULL;
  size_t arg_list_n = 0;

  unsigned int i = 0;

  /* Allocate key pair.  */
  key_pair_new = gcry_malloc (sizeof (struct gcry_ac_key_pair));
  if (! key_pair_new)
    err = gpg_err_code_from_errno (errno);

  if (! err)
    {
      /* Allocate keys.  */
      key_pair_new->secret = gcry_malloc (sizeof (struct gcry_ac_key));
      key_pair_new->public = gcry_malloc (sizeof (struct gcry_ac_key));

      if (! (key_pair_new->secret || key_pair_new->public))
 	err = gpg_err_code_from_errno (errno);
      else
	{
	  key_pair_new->secret->type = GCRY_AC_KEY_SECRET;
	  key_pair_new->public->type = GCRY_AC_KEY_PUBLIC;
	  key_pair_new->secret->data_sexp = NULL;
	  key_pair_new->public->data_sexp = NULL;
	  key_pair_new->secret->data = NULL;
	  key_pair_new->public->data = NULL;
	}
    }

  if (! err)
    {
      /* Calculate size of the format string, that is used for
	 creating the request S-expression.  */
      genkey_format_n = 23;

      /* Respect any relevant algorithm specific commands.  */
      if (key_spec)
	for (i = 0; gcry_ac_key_generate_specs[i].algorithm; i++)
	  if (handle->algorithm == gcry_ac_key_generate_specs[i].algorithm)
	    genkey_format_n += 6;

      /* Create format string.  */
      genkey_format = gcry_malloc (genkey_format_n);
      if (! genkey_format)
	err = gpg_err_code_from_errno (errno);
      else
	{
	  /* Fill format string.  */
	  *genkey_format = 0;
	  strcat (genkey_format, "(genkey(%s(nbits%d)");
	  if (key_spec)
	    for (i = 0; gcry_ac_key_generate_specs[i].algorithm; i++)
	      if (handle->algorithm == gcry_ac_key_generate_specs[i].algorithm)
		strcat (genkey_format, "(%s%m)");
	  strcat (genkey_format, "))");
	}
    }

  if (! err)
    {
      /* Build list of argument pointers, the algorithm name and the
	 nbits are needed always.  */
      arg_list_n = 2;

      /* Now the algorithm specific arguments.  */
      if (key_spec)
	for (i = 0; gcry_ac_key_generate_specs[i].algorithm; i++)
	  if (handle->algorithm == gcry_ac_key_generate_specs[i].algorithm)
	    arg_list_n += 2;

      /* Allocate list.  */
      arg_list = gcry_malloc (sizeof (void *) * arg_list_n);
      if (! arg_list)
	err = gpg_err_code_from_errno (errno);
      else
	{
	  /* Fill argument list. */
	  
	  int j;

	  arg_list[0] = (void *) &handle->algorithm_name;
	  arg_list[1] = (void *) &nbits;

	  if (key_spec)
	    for (j = 2, i = 0; gcry_ac_key_generate_specs[i].algorithm; i++)
	      if (handle->algorithm == gcry_ac_key_generate_specs[i].algorithm)
		{
		  /* Add name of this specification flag and the
		     according member of the spec strucuture.  */
		  arg_list[j++] = (void *) (&gcry_ac_key_generate_specs[i].name);
		  arg_list[j++] = (void *) (((char *) key_spec)
					    + gcry_ac_key_generate_specs[i].offset);
		}
	}
    }

  if (! err)
    /* Construct final request S-expression.  */
    err = gcry_err_code (gcry_sexp_build_array (&genkey_sexp_request, NULL,
					       genkey_format, arg_list));

  if (! err)
    /* Perform genkey operation.  */
    err = gcry_err_code (gcry_pk_genkey (&genkey_sexp_reply,
					genkey_sexp_request));

  /* Split keys.  */
  if (! err)
    {
      key_pair_new->secret->data_sexp = gcry_sexp_find_token (genkey_sexp_reply,
							      "private-key", 0);
      if (! key_pair_new->secret->data_sexp)
	err = GPG_ERR_INTERNAL;
    }
  if (! err)
    {
      key_pair_new->public->data_sexp = gcry_sexp_find_token (genkey_sexp_reply,
							      "public-key", 0);
      if (! key_pair_new->public->data_sexp)
	err = GPG_ERR_INTERNAL;
    }

  /* Extract key material.  */
  if (! err)
    err = gcry_ac_data_extract ("private-key", handle->algorithm_name,
				key_pair_new->secret->data_sexp,
				&key_pair_new->secret->data);
  if (! err)
    err = gcry_ac_data_extract ("public-key", handle->algorithm_name,
				key_pair_new->public->data_sexp,
				&key_pair_new->public->data);

  /* Done.  */

  if (! err)
    *key_pair = key_pair_new;
  else
    {
      /* Deallocate resources.  */

      if (key_pair_new)
	{
	  if (key_pair_new->secret)
	    gcry_ac_key_destroy (key_pair_new->secret);
	  if (key_pair_new->public)
	    gcry_ac_key_destroy (key_pair_new->public);

	  gcry_free (key_pair_new);
	}

      if (arg_list)
	gcry_free (arg_list);

      if (genkey_format)
	gcry_free (genkey_format);

      if (genkey_sexp_request)
	gcry_sexp_release (genkey_sexp_request);
      if (genkey_sexp_reply)
	gcry_sexp_release (genkey_sexp_reply);
    }

  return gcry_error (err);
}

/* Returns the key of type WHICH out of the key pair KEY_PAIR.  */
gcry_ac_key_t
gcry_ac_key_pair_extract (gcry_ac_key_pair_t key_pair,
			  gcry_ac_key_type_t witch)
{
  gcry_ac_key_t key = NULL;

  switch (witch)
    {
    case GCRY_AC_KEY_SECRET:
      key = key_pair->secret;
      break;

    case GCRY_AC_KEY_PUBLIC:
      key = key_pair->public;
      break;
    }

  return key;
}

/* Destroys the key KEY.  */
void
gcry_ac_key_destroy (gcry_ac_key_t key)
{
  int i;

  if (key->data)
    {
      for (i = 0; i < key->data->data_n; i++)
	if (key->data->data[i].mpi != NULL)
	  gcry_mpi_release (key->data->data[i].mpi);
      gcry_free (key->data);
    }
  if (key->data_sexp)
    gcry_sexp_release (key->data_sexp);
  gcry_free (key);
}

/* Destroys the key pair KEY_PAIR.  */
void
gcry_ac_key_pair_destroy (gcry_ac_key_pair_t key_pair)
{
  gcry_ac_key_destroy (key_pair->secret);
  gcry_ac_key_destroy (key_pair->public);
  gcry_free (key_pair);
}

/* Returns the data set contained in the key KEY.  */
gcry_ac_data_t
gcry_ac_key_data_get (gcry_ac_key_t key)
{
  return  key->data;
}

/* Verifies that the key KEY is sane.  */
gcry_error_t
gcry_ac_key_test (gcry_ac_key_t key)
{
  gcry_err_code_t err = GPG_ERR_NO_ERROR;

  err = gcry_err_code (gcry_pk_testkey (key->data_sexp));

  return gcry_error (err);
}

/* Stores the number of bits of the key KEY in NBITS.  */
gcry_error_t
gcry_ac_key_get_nbits (gcry_ac_key_t key, unsigned int *nbits)
{
  gcry_err_code_t err = GPG_ERR_NO_ERROR;
  unsigned int n;

  n = gcry_pk_get_nbits (key->data_sexp);
  if (n)
    *nbits = n;
  else
    err = GPG_ERR_PUBKEY_ALGO;

  return gcry_error (err);
}

/* Writes the 20 byte long key grip of the key KEY to KEY_GRIP.  */
gcry_error_t
gcry_ac_key_get_grip (gcry_ac_key_t key, unsigned char *key_grip)
{
  gcry_err_code_t err = GPG_ERR_NO_ERROR;
  unsigned char *ret;

  ret = gcry_pk_get_keygrip (key->data_sexp, key_grip);
  if (! ret)
    err = GPG_ERR_INTERNAL; 	/* FIXME.  */

  return gcry_error (err);
}



/* 
 * Functions performing cryptographic operations.
 */

/* Encrypts the plain text MPI value DATA_PLAIN with the key public
   KEY under the control of the flags FLAGS and stores the resulting
   data set into DATA_ENCRYPTED.  */
gcry_error_t
gcry_ac_data_encrypt (gcry_ac_handle_t handle,
		      unsigned int flags,
		      gcry_ac_key_t key,
		      gcry_mpi_t data_plain,
		      gcry_ac_data_t *data_encrypted)
{
  gcry_err_code_t err = GPG_ERR_NO_ERROR;
  gcry_sexp_t sexp_request = NULL;
  gcry_sexp_t sexp_reply = NULL;
  char *request_format = NULL;
  size_t request_format_n = 0;
  gcry_ac_data_t data;
  
  int i;

  if (key->type != GCRY_AC_KEY_PUBLIC)
    err = GPG_ERR_WRONG_KEY_USAGE;

  if (! err)
    {
      /* Calculate request format string.  */

      request_format_n += 23;
      for (i = 0; gcry_ac_flags[i].number; i++)
	if (flags & gcry_ac_flags[i].number)
	  request_format_n += strlen (gcry_ac_flags[i].string) + 1;

      /* Allocate request format string.  */
      request_format = gcry_malloc (request_format_n);
      if (! request_format)
	err = gpg_err_code_from_errno (errno);
    }

  if (! err)
    {
      /* Fill format string.  */
      *request_format = 0;
      strcat (request_format, "(data(flags");
      for (i = 0; gcry_ac_flags[i].number; i++)
	if (flags & gcry_ac_flags[i].number)
	  {
	    strcat (request_format, " ");
	    strcat (request_format, gcry_ac_flags[i].string);
	  }
      strcat (request_format, ")(value%m))");

      /* Create S-expression.  */
      err = gcry_sexp_build (&sexp_request, NULL,
			     request_format, data_plain);
    }

  if (! err)
    /* Encrypt.  */
    err = gcry_pk_encrypt (&sexp_reply, sexp_request, key->data_sexp);

  if (! err)
    /* Extract data.  */
    err = gcry_ac_data_extract ("enc-val", handle->algorithm_name,
				sexp_reply, &data);

  /* Deallocate resources.  */

  if (sexp_request)
    gcry_sexp_release (sexp_request);
  if (sexp_reply)
    gcry_sexp_release (sexp_reply);

  if (! err)
    /* Copy out.  */
    *data_encrypted = data;

  return gcry_error (err);
}

/* Decrypts the encrypted data contained in the data set
   DATA_ENCRYPTED with the secret key KEY under the control of the
   flags FLAGS and stores the resulting plain text MPI value in
   DATA_PLAIN.  */
gcry_error_t
gcry_ac_data_decrypt (gcry_ac_handle_t handle,
		      unsigned int flags,
		      gcry_ac_key_t key,
		      gcry_mpi_t *data_plain,
		      gcry_ac_data_t data_encrypted)
{
  gcry_err_code_t err = GPG_ERR_NO_ERROR;
  gcry_mpi_t data_decrypted = NULL;
  gcry_sexp_t sexp_request = NULL;
  gcry_sexp_t sexp_reply = NULL;

  if (key->type != GCRY_AC_KEY_SECRET)
    err = GPG_ERR_WRONG_KEY_USAGE;

  if (! err)
    /* Create S-expression from data.  */
    err = gcry_ac_data_construct ("enc-val", 1, flags, handle->algorithm_name,
				  data_encrypted, &sexp_request);

  if (! err)
    /* Decrypt.  */
    err = gcry_pk_decrypt (&sexp_reply, sexp_request, key->data_sexp);

  if (! err)
    {
      /* Extract plain text. */

      gcry_sexp_t l;

      l = gcry_sexp_find_token (sexp_reply, "value", 0);
      if (! l)
	err = GPG_ERR_GENERAL;
      else
	{
	  data_decrypted = gcry_sexp_nth_mpi (l, 1, GCRYMPI_FMT_USG);
	  if (! data_decrypted)
	    err = GPG_ERR_GENERAL;
	  gcry_sexp_release (l);
	}
    }

  /* Done.  */

  if (err)
    {
      /* Deallocate resources.  */
      if (sexp_request)
	gcry_sexp_release (sexp_request);
      if (sexp_reply)
	gcry_sexp_release (sexp_reply);
    }
  else
    *data_plain = data_decrypted;

  return gcry_error (err);

}

/* Signs the data contained in DATA with the secret key KEY and stores
   the resulting signature data set in DATA_SIGNATURE.  */
gcry_error_t
gcry_ac_data_sign (gcry_ac_handle_t handle,
		   gcry_ac_key_t key,
		   gcry_mpi_t data,
		   gcry_ac_data_t *data_signature)
{
  gcry_err_code_t err = GPG_ERR_NO_ERROR;
  gcry_sexp_t sexp_request = NULL;
  gcry_sexp_t sexp_reply = NULL;
  gcry_ac_data_t ac_data;

  if (key->type != GCRY_AC_KEY_SECRET)
    err = GPG_ERR_WRONG_KEY_USAGE;

  if (! err)
    /* Create S-expression holding the data.  */
    err = gcry_sexp_build (&sexp_request, NULL,
			   "(data(flags)(value%m))", data);
  if (! err)
    /* Sign.  */
    err = gcry_pk_sign (&sexp_reply, sexp_request, key->data_sexp);

  if (! err)
    /* Extract data.  */
    err = gcry_ac_data_extract ("sig-val", handle->algorithm_name,
				sexp_reply, &ac_data);

  /* Done.  */

  if (sexp_request)
    gcry_sexp_release (sexp_request);
  if (sexp_reply)
    gcry_sexp_release (sexp_reply);

  if (! err)
    *data_signature = ac_data;

  return gcry_error (err);
}

/* Verifies that the signature contained in the data set
   DATA_SIGNATURE is indeed the result of signing the data contained
   in DATA with the secret key belonging to the public key KEY.  */
gcry_error_t
gcry_ac_data_verify (gcry_ac_handle_t handle,
		     gcry_ac_key_t key,
		     gcry_mpi_t data,
		     gcry_ac_data_t data_signature)
{
  gcry_err_code_t err = GPG_ERR_NO_ERROR;
  gcry_sexp_t sexp_request = NULL;
  gcry_sexp_t sexp_data = NULL;

  if (key->type != GCRY_AC_KEY_PUBLIC)
    err = GPG_ERR_WRONG_KEY_USAGE;

  if (! err)
    /* Construct S-expression holding the signature data.  */
    err = gcry_ac_data_construct ("sig-val", 1, 0, handle->algorithm_name,
				  data_signature, &sexp_request);

  if (! err)
    /* Construct S-expression holding the data.  */
    err = gcry_sexp_build (&sexp_data, NULL,
			   "(data(flags)(value%m))", data);

  if (! err)
    /* Verify signature.  */
    err = gcry_pk_verify (sexp_request, sexp_data, key->data_sexp);

  /* Done.  */

  if (sexp_request)
    gcry_sexp_release (sexp_request);
  if (sexp_data)
    gcry_sexp_release (sexp_data);

  return gcry_error (err);
}



/* 
 * General functions.
 */

/* Stores the textual representation of the algorithm whose id is
   given in ALGORITHM in NAME.  */
gcry_error_t
gcry_ac_id_to_name (gcry_ac_id_t algorithm, const char **name)
{
  gcry_err_code_t err = GPG_ERR_NO_ERROR;
  const char *n;

  n = gcry_pk_algo_name (algorithm);
  if (*n)
    *name = n;
  else
    err = GPG_ERR_PUBKEY_ALGO;

  return gcry_error (err);
}

/* Stores the numeric ID of the algorithm whose textual representation
   is contained in NAME in ALGORITHM.  */
gcry_error_t
gcry_ac_name_to_id (const char *name, gcry_ac_id_t *algorithm)
{
  gcry_err_code_t err = GPG_ERR_NO_ERROR;
  int algo;

  algo = gcry_pk_map_name (name);
  if (algo)
    *algorithm = algo;
  else
    err = GPG_ERR_PUBKEY_ALGO;
    
  return gcry_error (err);
}