5 * Manipulating key data
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 #ifndef CATACOMB_KEY_DATA_H
31 #define CATACOMB_KEY_DATA_H
37 /*----- Header files ------------------------------------------------------*/
41 #include <mLib/bits.h>
42 #include <mLib/dstr.h>
45 #ifndef CATACOMB_KEY_ERROR_H
46 # include "key-error.h"
57 /*----- Data structures ---------------------------------------------------*/
59 /* --- Key binary data --- */
61 typedef struct key_bin {
62 octet *k; /* Pointer to key data */
63 size_t sz; /* Size of the key data (in bytes) */
66 /* --- Key data structure --- */
68 typedef struct key_data {
69 unsigned e; /* Encoding type for key data */
70 unsigned ref; /* Reference counter */
72 key_bin k; /* Binary key data */
73 mp *m; /* Multiprecision integer */
74 sym_table s; /* Structured key data */
75 char *p; /* String pointer */
76 ec e; /* Elliptic curve point */
80 typedef struct key_struct {
85 typedef struct key_subkeyiter { sym_iter i; } key_subkeyiter;
87 /* --- Packing and unpacking --- */
89 typedef struct key_packdef {
90 unsigned e; /* Key data encoding type */
91 void *p; /* Pointer to the destination */
92 key_data *kd; /* Key data block */
95 typedef struct key_packstruct {
96 char *name; /* Pointer to name string */
97 key_packdef kp; /* Packing structure */
100 /* --- Key binary encoding --- *
102 * The binary encoding consists of a header containing a 16-bit encoding type
103 * and a 16-bit length, followed immediately by the key data, followed by
104 * between zero and three zero bytes to make the total length a multiple of
105 * four. The format of the following data depends on the encoding type:
107 * @KENC_BINARY@ Binary data.
109 * @KENC_MP@ Octet array interpreted in big-endian byte order.
111 * @KENC_STRUCT@ An array of pairs, each containing a string (8-bit
112 * length followed by data and zero-padding to 4-byte
113 * boundary) and key binary encodings.
115 * @KENC_ENCRYPT@ Binary data, format
118 /* --- Key encoding methods and other flags--- */
122 /* --- Bottom two bits are the encoding type --- */
124 KF_ENCMASK = 0x83, /* Encoding mask */
125 KENC_BINARY = 0x00, /* Plain binary key (@k@) */
126 KENC_MP = 0x01, /* Multiprecision integer (@i@) */
127 KENC_STRUCT = 0x02, /* Structured key data (@s@) */
128 KENC_ENCRYPT = 0x03, /* Encrypted key type (@k@) */
129 KENC_STRING = 0x80, /* ASCII string (@p@) */
130 KENC_EC = 0x81, /* Elliptic curve point (@e@) */
132 /* --- Key category bits --- */
134 KF_CATMASK = 0x0c, /* Category mask */
135 KCAT_SYMM = 0x00, /* Symmetric encryption key */
136 KCAT_PRIV = 0x04, /* Private (asymmetric) key */
137 KCAT_PUB = 0x08, /* Public (asymmetric) key */
138 KCAT_SHARE = 0x0c, /* Shared (asymmetric) key */
139 KF_NONSECRET = 0x08, /* Bit flag for non-secret keys */
141 /* --- Other flags --- */
143 KF_BURN = 0x10, /* Burn key after use */
144 KF_OPT = 0x20, /* Optional key (for @key_unpack@) */
146 /* --- Tag end --- */
148 KENC_MAX /* Dummy limit constant */
151 /* --- Key locking return codes --- */
153 #define KL_OK 0 /* All good */
154 #define KL_IOERR -1 /* I/O problem (e.g., getting pp) */
155 #define KL_KEYERR -2 /* Wrong key supplied */
156 #define KL_DATAERR -3 /* Data format error */
158 /* --- Key flag filtering --- */
160 typedef struct key_filter {
165 /* --- Matching aginst key selection --- */
167 #define KEY_MATCH(kd, kf) \
169 ((kd)->e & KF_ENCMASK) == KENC_STRUCT || \
170 ((kd)->e & (kf)->m) == (kf)->f)
172 /*----- Key flags and filtering -------------------------------------------*/
174 /* --- @key_readflags@ --- *
176 * Arguments: @const char *p@ = pointer to string to read
177 * @char **pp@ = where to store the end pointer
178 * @unsigned *ff@ = where to store the flags
179 * @unsigned *mm@ = where to store the mask
181 * Returns: Zero if all went well, nonzero if there was an error.
183 * Use: Reads a flag string.
186 extern int key_readflags(const char */*p*/, char **/*pp*/,
187 unsigned */*ff*/, unsigned */*mm*/);
189 /* --- @key_writeflags@ --- *
191 * Arguments: @unsigned f@ = flags to write
192 * @dstr *d@ = pointer to destination string
196 * Use: Emits a flags word as a string representation.
199 extern void key_writeflags(unsigned /*f*/, dstr */*d*/);
201 /* --- @key_match@ --- *
203 * Arguments: @key_data *k@ = pointer to key data block
204 * @const key_filter *kf@ = pointer to filter block
206 * Returns: Nonzero if the key matches the filter.
208 * Use: Checks whether a key matches a filter.
211 extern int key_match(key_data */*k*/, const key_filter */*kf*/);
213 /*----- Setting new key data ----------------------------------------------*/
215 /* --- @key_newraw@ --- *
217 * Arguments: @unsigned e@ = encoding type to set
219 * Returns: New key block, not filled in.
222 extern key_data *key_newraw(unsigned /*e*/);
224 /* --- @key_newbinary@ --- *
226 * Arguments: @unsigned e@ = other encoding flags
227 * @const void *p@ = pointer to key data
228 * @size_t sz@ = size of the key data
230 * Returns: New key data object.
233 extern key_data *key_newbinary(unsigned /*e*/,
234 const void */*p*/, size_t /*sz*/);
236 /* --- @key_newencrypted@ --- *
238 * Arguments: @unsigned e@ = other encoding flags
239 * @const void *p@ = pointer to key data
240 * @size_t sz@ = size of the key data
242 * Returns: New key data object.
245 extern key_data *key_newencrypted(unsigned /*e*/,
246 const void */*p*/, size_t /*sz*/);
248 /* --- @key_newmp@ --- *
250 * Arguments: @unsigned e@ = other encoding flags
251 * @mp *m@ = pointer to the value to set
253 * Returns: New key data object.
256 extern key_data *key_newmp(unsigned /*e*/, mp */*m*/);
258 /* --- @key_newstring@ --- *
260 * Arguments: @unsigned e@ = other encoding flags
261 * @const char *p@ = pointer to the value to set
263 * Returns: New key data object.
266 extern key_data *key_newstring(unsigned /*e*/, const char */*p*/);
268 /* --- @key_newec@ --- *
270 * Arguments: @unsigned e@ = other encoding flags
271 * @const ec *pt@ = pointer to the value to set
273 * Returns: New key data object.
276 extern key_data *key_newec(unsigned /*e*/, const ec */*pt*/);
278 /* --- @key_newstruct@ --- *
282 * Returns: New key data object.
285 extern key_data *key_newstruct(void);
287 /* --- @key_structfind@ --- *
289 * Arguments: @key_data *k@ = pointer to key data block
290 * @const char *tag@ = pointer to tag string
292 * Returns: Pointer to key data block, or null.
294 * Use: Looks up the tag in a structured key.
297 extern key_data *key_structfind(key_data */*k*/, const char */*tag*/);
299 /* --- @key_mksubkeyiter@ --- *
301 * Arguments: @key_subkeyiter *i@ = pointer to iterator block
302 * @key_data *k@ = pointer to key data block
306 * Use: Initializes a subkey iterator.
309 extern void key_mksubkeyiter(key_subkeyiter */*i*/, key_data */*k*/);
311 /* --- @key_nextsubkey@ --- *
313 * Arguments: @key_structiter *i@ = pointer to iterator block
314 * @const char **tag@ = where to put the tag pointer, or null
315 * @key_data **kd@ = where to put the key data pointer, or null
317 * Returns: Nonzero if there was another item, zero if we hit the
320 * Use: Collects the next subkey of a structured key.
323 extern int key_nextsubkey(key_subkeyiter */*i*/,
324 const char **/*tag*/, key_data **/*kd*/);
326 /* --- @key_structset@, @key_structsteal@ --- *
328 * Arguments: @key_data *k@ = pointer to key data block
329 * @const char *tag@ = pointer to tag string
330 * @key_data *kd@ = new key data to store
334 * Use: Creates a new subkey. Stealing doesn't affect @kd@'s
335 * refcount. If @kd@ is null, the subkey is deleted.
338 extern void key_structset(key_data */*k*/,
339 const char */*tag*/, key_data */*kd*/);
340 extern void key_structsteal(key_data */*k*/,
341 const char */*tag*/, key_data */*kd*/);
343 /* --- @key_split@ --- *
345 * Arguments: @key_data **kk@ = address of pointer to key data block
349 * Use: Replaces @*kk@ with a pointer to the same key data, but with
350 * just one reference.
353 extern void key_split(key_data **/*kk*/);
355 /*----- Miscellaneous operations ------------------------------------------*/
357 /* --- @key_incref@ --- *
359 * Arguments: @key_data *k@ = pointer to key data
363 * Use: Increments the refcount on a key data block.
366 #define KEY_INCREF(k) ((k)->ref++)
367 extern void key_incref(key_data */*k*/);
369 /* --- @key_destroy@ --- *
371 * Arguments: @key_data *k@ = pointer to key data to destroy
375 * Use: Destroys a block of key data, regardless of reference count.
376 * Don't use this unless you know what you're doing.
379 extern void key_destroy(key_data */*k*/);
381 /* --- @key_drop@ --- *
383 * Arguments: @key_data *k@ = pointer to key data to destroy
387 * Use: Drops a reference to key data, destroying it if necessary.
390 #define KEY_DROP(k) do { \
397 extern void key_drop(key_data */*k*/);
399 /* --- @key_do@ --- *
401 * Arguments: @key_data *k@ = pointer to key data block
402 * @const key_filter *kf@ = pointer to filter block
403 * @dstr *d@ = pointer to base string
404 * @int (*func)(key_data *kd, dstr *d, void *p@ = function
405 * @void *p@ = argument to function
407 * Returns: Nonzero return code from function, or zero.
409 * Use: Runs a function over all the leaves of a key.
412 extern int key_do(key_data */*k*/, const key_filter */*kf*/, dstr */*d*/,
413 int (*/*func*/)(key_data */*kd*/,
414 dstr */*d*/, void */*p*/),
417 /* --- @key_copydata@ --- *
419 * Arguments: @key_data *k@ = key data to copy
420 * @const key_filter *kf@ = pointer to filter block
422 * Returns: Pointer to a copy of the data, or null if the root subkey
423 * didn't match the filter.
425 * Use: Copies a chunk of key data. Subkeys, whether they're
426 * structured or leaves, which don't match the filter aren't
427 * copied. The copy may or may not have structure in common
431 extern key_data *key_copydata(key_data */*k*/, const key_filter */*kf*/);
433 /*----- Textual encoding --------------------------------------------------*/
435 /* --- @key_read@ --- *
437 * Arguments: @const char *p@ = pointer to textual key representation
438 * @char **pp@ = where to store the end pointer
440 * Returns: The newly-read key data, or null if it failed.
442 * Use: Parses a textual key description.
445 extern key_data *key_read(const char */*p*/, char **/*pp*/);
447 /* --- @key_write@ --- *
449 * Arguments: @key_data *k@ = pointer to key data
450 * @dstr *d@ = destination string to write on
451 * @const key_filter *kf@ = pointer to key selection block
453 * Returns: Nonzero if any items were actually written.
455 * Use: Writes a key in a textual encoding.
458 extern int key_write(key_data */*k*/, dstr */*d*/, const key_filter */*kf*/);
460 /*----- Key binary encoding -----------------------------------------------*/
462 /* --- @key_decode@ --- *
464 * Arguments: @const void *p@ = pointer to buffer to read
465 * @size_t sz@ = size of the buffer
467 * Returns: The newly-read key data, or null if it failed.
469 * Use: Decodes a binary representation of a key.
472 extern key_data *key_decode(const void */*p*/, size_t /*sz*/);
474 /* --- @key_encode@ --- *
476 * Arguments: @key_data *k@ = pointer to key data block
477 * @dstr *d@ = pointer to destination string
478 * @const key_filter *kf@ = pointer to key selection block
480 * Returns: Nonzero if any items were actually written.
482 * Use: Encodes a key block as binary data.
485 extern int key_encode(key_data */*k*/, dstr */*d*/,
486 const key_filter */*kf*/);
488 /*----- Packing and unpacking keys ----------------------------------------*/
490 /* --- @key_pack@ --- *
492 * Arguments: @key_packdef *kp@ = pointer to packing structure
493 * @key_data **kd@ = where to put the key data pointer
494 * @dstr *d@ = pointer to tag string for the key data
496 * Returns: Error code, or zero.
498 * Use: Packs a key from a data structure.
501 extern int key_pack(key_packdef */*kp*/, key_data **/*kd*/, dstr */*d*/);
503 /* --- @key_unpack@ --- *
505 * Arguments: @key_packdef *kp@ = pointer to packing structure
506 * @key_data *kd@ = pointer to source key data
507 * @dstr *d@ = pointer to tag string for the key data
509 * Returns: Error code, or zero.
511 * Use: Unpacks a key into an appropriate data structure.
514 extern int key_unpack(key_packdef */*kp*/, key_data */*kd*/, dstr */*d*/);
516 /* --- @key_unpackdone@ --- *
518 * Arguments: @key_packdef *kp@ = pointer to packing definition
522 * Use: Frees the key components contained within a packing
523 * definition, created during key unpacking.
526 extern void key_unpackdone(key_packdef */*kp*/);
528 /*----- Key encryption ----------------------------------------------------*/
530 /* --- @key_lock@ --- *
532 * Arguments: @key_data **kt@ = where to store the destination pointer
533 * @key_data *k@ = source key data block or null to use @*kt@
534 * @const void *e@ = secret to encrypt key with
535 * @size_t esz@ = size of the secret
539 * Use: Encrypts a key data block using a secret.
542 extern void key_lock(key_data **/*kt*/, key_data */*k*/,
543 const void */*e*/, size_t /*esz*/);
545 /* --- @key_unlock@ --- *
547 * Arguments: @key_data **kt@ = where to store the destination pointer
548 * @key_data *k@ = source key data block or null to use @*kt@
549 * @const void *e@ = secret to decrypt the block with
550 * @size_t esz@ = size of the secret
552 * Returns: Zero for success, or a @KERR_@ error code.
554 * Use: Unlocks a key using a secret.
557 extern int key_unlock(key_data **/*kt*/, key_data */*k*/,
558 const void */*e*/, size_t /*esz*/);
560 /* --- @key_plock@ --- *
562 * Arguments: @key_data **kt@ = where to store the destination pointer
563 * @key_data *k@ = source key data block or null to use @*kt@
564 * @const char *tag@ = tag to use for passphrase
566 * Returns: Zero if successful, a @KERR@ error code on failure.
568 * Use: Locks a key by encrypting it with a passphrase.
571 extern int key_plock(key_data **/*kt*/, key_data */*k*/,
572 const char */*tag*/);
574 /* --- @key_punlock@ --- *
576 * Arguments: @key_data **kt@ = where to store the destination pointer
577 * @key_data *k@ = source key data block or null to use @*kt@
578 * @const char *tag@ = tag to use for passphrase
580 * Returns: Zero if successful, a @KERR@ error code on failure.
582 * Use: Unlocks a passphrase-locked key.
585 extern int key_punlock(key_data **/*kt*/, key_data */*k*/,
586 const char */*tag*/);
588 /*----- That's all, folks -------------------------------------------------*/