Rearrange the file tree.
[u/mdw/catacomb] / key / key-data.h
1 /* -*-c-*-
2 *
3 * Manipulating key data
4 *
5 * (c) 1999 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of Catacomb.
11 *
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
16 *
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
21 *
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA.
26 */
27
28 #ifndef CATACOMB_KEY_DATA_H
29 #define CATACOMB_KEY_DATA_H
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 /*----- Header files ------------------------------------------------------*/
36
37 #include <stddef.h>
38
39 #include <mLib/bits.h>
40 #include <mLib/dstr.h>
41 #include <mLib/sym.h>
42
43 #ifndef CATACOMB_KEY_ERROR_H
44 # include "key-error.h"
45 #endif
46
47 #ifndef CATACOMB_MP_H
48 # include "mp.h"
49 #endif
50
51 #ifndef CATACOMB_EC_H
52 # include "ec.h"
53 #endif
54
55 /*----- Data structures ---------------------------------------------------*/
56
57 /* --- Key binary data --- */
58
59 typedef struct key_bin {
60 octet *k; /* Pointer to key data */
61 size_t sz; /* Size of the key data (in bytes) */
62 } key_bin;
63
64 /* --- Key data structure --- */
65
66 typedef struct key_data {
67 unsigned e; /* Encoding type for key data */
68 unsigned ref; /* Reference counter */
69 union {
70 key_bin k; /* Binary key data */
71 mp *m; /* Multiprecision integer */
72 sym_table s; /* Structured key data */
73 char *p; /* String pointer */
74 ec e; /* Elliptic curve point */
75 } u;
76 } key_data;
77
78 typedef struct key_struct {
79 sym_base _b;
80 key_data *k;
81 } key_struct;
82
83 typedef struct key_subkeyiter { sym_iter i; } key_subkeyiter;
84
85 /* --- Packing and unpacking --- */
86
87 typedef struct key_packdef {
88 unsigned e; /* Key data encoding type */
89 void *p; /* Pointer to the destination */
90 key_data *kd; /* Key data block */
91 } key_packdef;
92
93 typedef struct key_packstruct {
94 char *name; /* Pointer to name string */
95 key_packdef kp; /* Packing structure */
96 } key_packstruct;
97
98 /* --- Key binary encoding --- *
99 *
100 * The binary encoding consists of a header containing a 16-bit encoding type
101 * and a 16-bit length, followed immediately by the key data, followed by
102 * between zero and three zero bytes to make the total length a multiple of
103 * four. The format of the following data depends on the encoding type:
104 *
105 * @KENC_BINARY@ Binary data.
106 *
107 * @KENC_MP@ Octet array interpreted in big-endian byte order.
108 *
109 * @KENC_STRUCT@ An array of pairs, each containing a string (8-bit
110 * length followed by data and zero-padding to 4-byte
111 * boundary) and key binary encodings.
112 *
113 * @KENC_ENCRYPT@ Binary data, format
114 */
115
116 /* --- Key encoding methods and other flags--- */
117
118 enum {
119
120 /* --- Bottom two bits are the encoding type --- */
121
122 KF_ENCMASK = 0x83, /* Encoding mask */
123 KENC_BINARY = 0x00, /* Plain binary key (@k@) */
124 KENC_MP = 0x01, /* Multiprecision integer (@i@) */
125 KENC_STRUCT = 0x02, /* Structured key data (@s@) */
126 KENC_ENCRYPT = 0x03, /* Encrypted key type (@k@) */
127 KENC_STRING = 0x80, /* ASCII string (@p@) */
128 KENC_EC = 0x81, /* Elliptic curve point (@e@) */
129
130 /* --- Key category bits --- */
131
132 KF_CATMASK = 0x0c, /* Category mask */
133 KCAT_SYMM = 0x00, /* Symmetric encryption key */
134 KCAT_PRIV = 0x04, /* Private (asymmetric) key */
135 KCAT_PUB = 0x08, /* Public (asymmetric) key */
136 KCAT_SHARE = 0x0c, /* Shared (asymmetric) key */
137 KF_NONSECRET = 0x08, /* Bit flag for non-secret keys */
138
139 /* --- Other flags --- */
140
141 KF_BURN = 0x10, /* Burn key after use */
142 KF_OPT = 0x20, /* Optional key (for @key_unpack@) */
143
144 /* --- Tag end --- */
145
146 KENC_MAX /* Dummy limit constant */
147 };
148
149 /* --- Key locking return codes --- */
150
151 #define KL_OK 0 /* All good */
152 #define KL_IOERR -1 /* I/O problem (e.g., getting pp) */
153 #define KL_KEYERR -2 /* Wrong key supplied */
154 #define KL_DATAERR -3 /* Data format error */
155
156 /* --- Key flag filtering --- */
157
158 typedef struct key_filter {
159 unsigned f;
160 unsigned m;
161 } key_filter;
162
163 /* --- Matching aginst key selection --- */
164
165 #define KEY_MATCH(kd, kf) \
166 (!(kf) || \
167 ((kd)->e & KF_ENCMASK) == KENC_STRUCT || \
168 ((kd)->e & (kf)->m) == (kf)->f)
169
170 /*----- Key flags and filtering -------------------------------------------*/
171
172 /* --- @key_readflags@ --- *
173 *
174 * Arguments: @const char *p@ = pointer to string to read
175 * @char **pp@ = where to store the end pointer
176 * @unsigned *ff@ = where to store the flags
177 * @unsigned *mm@ = where to store the mask
178 *
179 * Returns: Zero if all went well, nonzero if there was an error.
180 *
181 * Use: Reads a flag string.
182 */
183
184 extern int key_readflags(const char */*p*/, char **/*pp*/,
185 unsigned */*ff*/, unsigned */*mm*/);
186
187 /* --- @key_writeflags@ --- *
188 *
189 * Arguments: @unsigned f@ = flags to write
190 * @dstr *d@ = pointer to destination string
191 *
192 * Returns: ---
193 *
194 * Use: Emits a flags word as a string representation.
195 */
196
197 extern void key_writeflags(unsigned /*f*/, dstr */*d*/);
198
199 /* --- @key_match@ --- *
200 *
201 * Arguments: @key_data *k@ = pointer to key data block
202 * @const key_filter *kf@ = pointer to filter block
203 *
204 * Returns: Nonzero if the key matches the filter.
205 *
206 * Use: Checks whether a key matches a filter.
207 */
208
209 extern int key_match(key_data */*k*/, const key_filter */*kf*/);
210
211 /*----- Setting new key data ----------------------------------------------*/
212
213 /* --- @key_newraw@ --- *
214 *
215 * Arguments: @unsigned e@ = encoding type to set
216 *
217 * Returns: New key block, not filled in.
218 */
219
220 extern key_data *key_newraw(unsigned /*e*/);
221
222 /* --- @key_newbinary@ --- *
223 *
224 * Arguments: @unsigned e@ = other encoding flags
225 * @const void *p@ = pointer to key data
226 * @size_t sz@ = size of the key data
227 *
228 * Returns: New key data object.
229 */
230
231 extern key_data *key_newbinary(unsigned /*e*/,
232 const void */*p*/, size_t /*sz*/);
233
234 /* --- @key_newencrypted@ --- *
235 *
236 * Arguments: @unsigned e@ = other encoding flags
237 * @const void *p@ = pointer to key data
238 * @size_t sz@ = size of the key data
239 *
240 * Returns: New key data object.
241 */
242
243 extern key_data *key_newencrypted(unsigned /*e*/,
244 const void */*p*/, size_t /*sz*/);
245
246 /* --- @key_newmp@ --- *
247 *
248 * Arguments: @unsigned e@ = other encoding flags
249 * @mp *m@ = pointer to the value to set
250 *
251 * Returns: New key data object.
252 */
253
254 extern key_data *key_newmp(unsigned /*e*/, mp */*m*/);
255
256 /* --- @key_newstring@ --- *
257 *
258 * Arguments: @unsigned e@ = other encoding flags
259 * @const char *p@ = pointer to the value to set
260 *
261 * Returns: New key data object.
262 */
263
264 extern key_data *key_newstring(unsigned /*e*/, const char */*p*/);
265
266 /* --- @key_newec@ --- *
267 *
268 * Arguments: @unsigned e@ = other encoding flags
269 * @const ec *pt@ = pointer to the value to set
270 *
271 * Returns: New key data object.
272 */
273
274 extern key_data *key_newec(unsigned /*e*/, const ec */*pt*/);
275
276 /* --- @key_newstruct@ --- *
277 *
278 * Arguments: ---
279 *
280 * Returns: New key data object.
281 */
282
283 extern key_data *key_newstruct(void);
284
285 /* --- @key_structfind@ --- *
286 *
287 * Arguments: @key_data *k@ = pointer to key data block
288 * @const char *tag@ = pointer to tag string
289 *
290 * Returns: Pointer to key data block, or null.
291 *
292 * Use: Looks up the tag in a structured key.
293 */
294
295 extern key_data *key_structfind(key_data */*k*/, const char */*tag*/);
296
297 /* --- @key_mksubkeyiter@ --- *
298 *
299 * Arguments: @key_subkeyiter *i@ = pointer to iterator block
300 * @key_data *k@ = pointer to key data block
301 *
302 * Returns: ---
303 *
304 * Use: Initializes a subkey iterator.
305 */
306
307 extern void key_mksubkeyiter(key_subkeyiter */*i*/, key_data */*k*/);
308
309 /* --- @key_nextsubkey@ --- *
310 *
311 * Arguments: @key_structiter *i@ = pointer to iterator block
312 * @const char **tag@ = where to put the tag pointer, or null
313 * @key_data **kd@ = where to put the key data pointer, or null
314 *
315 * Returns: Nonzero if there was another item, zero if we hit the
316 * end-stop.
317 *
318 * Use: Collects the next subkey of a structured key.
319 */
320
321 extern int key_nextsubkey(key_subkeyiter */*i*/,
322 const char **/*tag*/, key_data **/*kd*/);
323
324 /* --- @key_structset@, @key_structsteal@ --- *
325 *
326 * Arguments: @key_data *k@ = pointer to key data block
327 * @const char *tag@ = pointer to tag string
328 * @key_data *kd@ = new key data to store
329 *
330 * Returns: ---
331 *
332 * Use: Creates a new subkey. Stealing doesn't affect @kd@'s
333 * refcount. If @kd@ is null, the subkey is deleted.
334 */
335
336 extern void key_structset(key_data */*k*/,
337 const char */*tag*/, key_data */*kd*/);
338 extern void key_structsteal(key_data */*k*/,
339 const char */*tag*/, key_data */*kd*/);
340
341 /* --- @key_split@ --- *
342 *
343 * Arguments: @key_data **kk@ = address of pointer to key data block
344 *
345 * Returns: ---
346 *
347 * Use: Replaces @*kk@ with a pointer to the same key data, but with
348 * just one reference.
349 */
350
351 extern void key_split(key_data **/*kk*/);
352
353 /*----- Miscellaneous operations ------------------------------------------*/
354
355 /* --- @key_incref@ --- *
356 *
357 * Arguments: @key_data *k@ = pointer to key data
358 *
359 * Returns: ---
360 *
361 * Use: Increments the refcount on a key data block.
362 */
363
364 #define KEY_INCREF(k) ((k)->ref++)
365 extern void key_incref(key_data */*k*/);
366
367 /* --- @key_destroy@ --- *
368 *
369 * Arguments: @key_data *k@ = pointer to key data to destroy
370 *
371 * Returns: ---
372 *
373 * Use: Destroys a block of key data, regardless of reference count.
374 * Don't use this unless you know what you're doing.
375 */
376
377 extern void key_destroy(key_data */*k*/);
378
379 /* --- @key_drop@ --- *
380 *
381 * Arguments: @key_data *k@ = pointer to key data to destroy
382 *
383 * Returns: ---
384 *
385 * Use: Drops a reference to key data, destroying it if necessary.
386 */
387
388 #define KEY_DROP(k) do { \
389 key_data *_k = k; \
390 _k->ref--; \
391 if (_k->ref == 0) \
392 key_destroy(_k); \
393 } while (0)
394
395 extern void key_drop(key_data */*k*/);
396
397 /* --- @key_do@ --- *
398 *
399 * Arguments: @key_data *k@ = pointer to key data block
400 * @const key_filter *kf@ = pointer to filter block
401 * @dstr *d@ = pointer to base string
402 * @int (*func)(key_data *kd, dstr *d, void *p@ = function
403 * @void *p@ = argument to function
404 *
405 * Returns: Nonzero return code from function, or zero.
406 *
407 * Use: Runs a function over all the leaves of a key.
408 */
409
410 extern int key_do(key_data */*k*/, const key_filter */*kf*/, dstr */*d*/,
411 int (*/*func*/)(key_data */*kd*/,
412 dstr */*d*/, void */*p*/),
413 void */*p*/);
414
415 /* --- @key_copydata@ --- *
416 *
417 * Arguments: @key_data *k@ = key data to copy
418 * @const key_filter *kf@ = pointer to filter block
419 *
420 * Returns: Pointer to a copy of the data, or null if the root subkey
421 * didn't match the filter.
422 *
423 * Use: Copies a chunk of key data. Subkeys, whether they're
424 * structured or leaves, which don't match the filter aren't
425 * copied. The copy may or may not have structure in common
426 * with the original.
427 */
428
429 extern key_data *key_copydata(key_data */*k*/, const key_filter */*kf*/);
430
431 /*----- Textual encoding --------------------------------------------------*/
432
433 /* --- @key_read@ --- *
434 *
435 * Arguments: @const char *p@ = pointer to textual key representation
436 * @char **pp@ = where to store the end pointer
437 *
438 * Returns: The newly-read key data, or null if it failed.
439 *
440 * Use: Parses a textual key description.
441 */
442
443 extern key_data *key_read(const char */*p*/, char **/*pp*/);
444
445 /* --- @key_write@ --- *
446 *
447 * Arguments: @key_data *k@ = pointer to key data
448 * @dstr *d@ = destination string to write on
449 * @const key_filter *kf@ = pointer to key selection block
450 *
451 * Returns: Nonzero if any items were actually written.
452 *
453 * Use: Writes a key in a textual encoding.
454 */
455
456 extern int key_write(key_data */*k*/, dstr */*d*/, const key_filter */*kf*/);
457
458 /*----- Key binary encoding -----------------------------------------------*/
459
460 /* --- @key_decode@ --- *
461 *
462 * Arguments: @const void *p@ = pointer to buffer to read
463 * @size_t sz@ = size of the buffer
464 *
465 * Returns: The newly-read key data, or null if it failed.
466 *
467 * Use: Decodes a binary representation of a key.
468 */
469
470 extern key_data *key_decode(const void */*p*/, size_t /*sz*/);
471
472 /* --- @key_encode@ --- *
473 *
474 * Arguments: @key_data *k@ = pointer to key data block
475 * @dstr *d@ = pointer to destination string
476 * @const key_filter *kf@ = pointer to key selection block
477 *
478 * Returns: Nonzero if any items were actually written.
479 *
480 * Use: Encodes a key block as binary data.
481 */
482
483 extern int key_encode(key_data */*k*/, dstr */*d*/,
484 const key_filter */*kf*/);
485
486 /*----- Packing and unpacking keys ----------------------------------------*/
487
488 /* --- @key_pack@ --- *
489 *
490 * Arguments: @key_packdef *kp@ = pointer to packing structure
491 * @key_data **kd@ = where to put the key data pointer
492 * @dstr *d@ = pointer to tag string for the key data
493 *
494 * Returns: Error code, or zero.
495 *
496 * Use: Packs a key from a data structure.
497 */
498
499 extern int key_pack(key_packdef */*kp*/, key_data **/*kd*/, dstr */*d*/);
500
501 /* --- @key_unpack@ --- *
502 *
503 * Arguments: @key_packdef *kp@ = pointer to packing structure
504 * @key_data *kd@ = pointer to source key data
505 * @dstr *d@ = pointer to tag string for the key data
506 *
507 * Returns: Error code, or zero.
508 *
509 * Use: Unpacks a key into an appropriate data structure.
510 */
511
512 extern int key_unpack(key_packdef */*kp*/, key_data */*kd*/, dstr */*d*/);
513
514 /* --- @key_unpackdone@ --- *
515 *
516 * Arguments: @key_packdef *kp@ = pointer to packing definition
517 *
518 * Returns: ---
519 *
520 * Use: Frees the key components contained within a packing
521 * definition, created during key unpacking.
522 */
523
524 extern void key_unpackdone(key_packdef */*kp*/);
525
526 /*----- Key encryption ----------------------------------------------------*/
527
528 /* --- @key_lock@ --- *
529 *
530 * Arguments: @key_data **kt@ = where to store the destination pointer
531 * @key_data *k@ = source key data block or null to use @*kt@
532 * @const void *e@ = secret to encrypt key with
533 * @size_t esz@ = size of the secret
534 *
535 * Returns: ---
536 *
537 * Use: Encrypts a key data block using a secret.
538 */
539
540 extern void key_lock(key_data **/*kt*/, key_data */*k*/,
541 const void */*e*/, size_t /*esz*/);
542
543 /* --- @key_unlock@ --- *
544 *
545 * Arguments: @key_data **kt@ = where to store the destination pointer
546 * @key_data *k@ = source key data block or null to use @*kt@
547 * @const void *e@ = secret to decrypt the block with
548 * @size_t esz@ = size of the secret
549 *
550 * Returns: Zero for success, or a @KERR_@ error code.
551 *
552 * Use: Unlocks a key using a secret.
553 */
554
555 extern int key_unlock(key_data **/*kt*/, key_data */*k*/,
556 const void */*e*/, size_t /*esz*/);
557
558 /* --- @key_plock@ --- *
559 *
560 * Arguments: @key_data **kt@ = where to store the destination pointer
561 * @key_data *k@ = source key data block or null to use @*kt@
562 * @const char *tag@ = tag to use for passphrase
563 *
564 * Returns: Zero if successful, a @KERR@ error code on failure.
565 *
566 * Use: Locks a key by encrypting it with a passphrase.
567 */
568
569 extern int key_plock(key_data **/*kt*/, key_data */*k*/,
570 const char */*tag*/);
571
572 /* --- @key_punlock@ --- *
573 *
574 * Arguments: @key_data **kt@ = where to store the destination pointer
575 * @key_data *k@ = source key data block or null to use @*kt@
576 * @const char *tag@ = tag to use for passphrase
577 *
578 * Returns: Zero if successful, a @KERR@ error code on failure.
579 *
580 * Use: Unlocks a passphrase-locked key.
581 */
582
583 extern int key_punlock(key_data **/*kt*/, key_data */*k*/,
584 const char */*tag*/);
585
586 /*----- That's all, folks -------------------------------------------------*/
587
588 #ifdef __cplusplus
589 }
590 #endif
591
592 #endif