3 * Manipulating key data
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
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.
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.
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,
28 #ifndef CATACOMB_KEY_DATA_H
29 #define CATACOMB_KEY_DATA_H
35 /*----- Header files ------------------------------------------------------*/
39 #include <mLib/bits.h>
40 #include <mLib/dstr.h>
43 #ifndef CATACOMB_KEY_ERROR_H
44 # include "key-error.h"
55 /*----- Data structures ---------------------------------------------------*/
57 /* --- Key binary data --- */
59 typedef struct key_bin
{
60 octet
*k
; /* Pointer to key data */
61 size_t sz
; /* Size of the key data (in bytes) */
64 /* --- Key data structure --- */
66 typedef struct key_data
{
67 unsigned e
; /* Encoding type for key data */
68 unsigned ref
; /* Reference counter */
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 */
78 typedef struct key_struct
{
83 typedef struct key_subkeyiter
{ sym_iter i
; } key_subkeyiter
;
85 /* --- Packing and unpacking --- */
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 */
93 typedef struct key_packstruct
{
94 char *name
; /* Pointer to name string */
95 key_packdef kp
; /* Packing structure */
98 /* --- Key binary encoding --- *
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:
105 * @KENC_BINARY@ Binary data.
107 * @KENC_MP@ Octet array interpreted in big-endian byte order.
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.
113 * @KENC_ENCRYPT@ Binary data, format
116 /* --- Key encoding methods and other flags--- */
120 /* --- Bottom two bits are the encoding type --- */
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@) */
130 /* --- Key category bits --- */
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 */
139 /* --- Other flags --- */
141 KF_BURN
= 0x10, /* Burn key after use */
142 KF_OPT
= 0x20, /* Optional key (for @key_unpack@) */
144 /* --- Tag end --- */
146 KENC_MAX
/* Dummy limit constant */
149 /* --- Key locking return codes --- */
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 */
156 /* --- Key flag filtering --- */
158 typedef struct key_filter
{
163 /* --- Matching aginst key selection --- */
165 #define KEY_MATCH(kd, kf) \
167 ((kd)->e & KF_ENCMASK) == KENC_STRUCT || \
168 ((kd)->e & (kf)->m) == (kf)->f)
170 /*----- Key flags and filtering -------------------------------------------*/
172 /* --- @key_readflags@ --- *
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
179 * Returns: Zero if all went well, nonzero if there was an error.
181 * Use: Reads a flag string.
184 extern int key_readflags(const char */
*p*/
, char **/
*pp*/
,
185 unsigned */
*ff*/
, unsigned */
*mm*/
);
187 /* --- @key_writeflags@ --- *
189 * Arguments: @unsigned f@ = flags to write
190 * @dstr *d@ = pointer to destination string
194 * Use: Emits a flags word as a string representation.
197 extern void key_writeflags(unsigned /*f*/, dstr */
*d*/
);
199 /* --- @key_match@ --- *
201 * Arguments: @key_data *k@ = pointer to key data block
202 * @const key_filter *kf@ = pointer to filter block
204 * Returns: Nonzero if the key matches the filter.
206 * Use: Checks whether a key matches a filter.
209 extern int key_match(key_data */
*k*/
, const key_filter */
*kf*/
);
211 /*----- Setting new key data ----------------------------------------------*/
213 /* --- @key_newraw@ --- *
215 * Arguments: @unsigned e@ = encoding type to set
217 * Returns: New key block, not filled in.
220 extern key_data
*key_newraw(unsigned /*e*/);
222 /* --- @key_newbinary@ --- *
224 * Arguments: @unsigned e@ = other encoding flags
225 * @const void *p@ = pointer to key data
226 * @size_t sz@ = size of the key data
228 * Returns: New key data object.
231 extern key_data
*key_newbinary(unsigned /*e*/,
232 const void */
*p*/
, size_t /*sz*/);
234 /* --- @key_newencrypted@ --- *
236 * Arguments: @unsigned e@ = other encoding flags
237 * @const void *p@ = pointer to key data
238 * @size_t sz@ = size of the key data
240 * Returns: New key data object.
243 extern key_data
*key_newencrypted(unsigned /*e*/,
244 const void */
*p*/
, size_t /*sz*/);
246 /* --- @key_newmp@ --- *
248 * Arguments: @unsigned e@ = other encoding flags
249 * @mp *m@ = pointer to the value to set
251 * Returns: New key data object.
254 extern key_data
*key_newmp(unsigned /*e*/, mp */
*m*/
);
256 /* --- @key_newstring@ --- *
258 * Arguments: @unsigned e@ = other encoding flags
259 * @const char *p@ = pointer to the value to set
261 * Returns: New key data object.
264 extern key_data
*key_newstring(unsigned /*e*/, const char */
*p*/
);
266 /* --- @key_newec@ --- *
268 * Arguments: @unsigned e@ = other encoding flags
269 * @const ec *pt@ = pointer to the value to set
271 * Returns: New key data object.
274 extern key_data
*key_newec(unsigned /*e*/, const ec */
*pt*/
);
276 /* --- @key_newstruct@ --- *
280 * Returns: New key data object.
283 extern key_data
*key_newstruct(void);
285 /* --- @key_structfind@ --- *
287 * Arguments: @key_data *k@ = pointer to key data block
288 * @const char *tag@ = pointer to tag string
290 * Returns: Pointer to key data block, or null.
292 * Use: Looks up the tag in a structured key.
295 extern key_data
*key_structfind(key_data */
*k*/
, const char */
*tag*/
);
297 /* --- @key_mksubkeyiter@ --- *
299 * Arguments: @key_subkeyiter *i@ = pointer to iterator block
300 * @key_data *k@ = pointer to key data block
304 * Use: Initializes a subkey iterator.
307 extern void key_mksubkeyiter(key_subkeyiter */
*i*/
, key_data */
*k*/
);
309 /* --- @key_nextsubkey@ --- *
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
315 * Returns: Nonzero if there was another item, zero if we hit the
318 * Use: Collects the next subkey of a structured key.
321 extern int key_nextsubkey(key_subkeyiter */
*i*/
,
322 const char **/
*tag*/
, key_data
**/
*kd*/
);
324 /* --- @key_structset@, @key_structsteal@ --- *
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
332 * Use: Creates a new subkey. Stealing doesn't affect @kd@'s
333 * refcount. If @kd@ is null, the subkey is deleted.
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*/
);
341 /* --- @key_split@ --- *
343 * Arguments: @key_data **kk@ = address of pointer to key data block
347 * Use: Replaces @*kk@ with a pointer to the same key data, but with
348 * just one reference.
351 extern void key_split(key_data
**/
*kk*/
);
353 /*----- Miscellaneous operations ------------------------------------------*/
355 /* --- @key_incref@ --- *
357 * Arguments: @key_data *k@ = pointer to key data
361 * Use: Increments the refcount on a key data block.
364 #define KEY_INCREF(k) ((k)->ref++)
365 extern void key_incref(key_data */
*k*/
);
367 /* --- @key_destroy@ --- *
369 * Arguments: @key_data *k@ = pointer to key data to destroy
373 * Use: Destroys a block of key data, regardless of reference count.
374 * Don't use this unless you know what you're doing.
377 extern void key_destroy(key_data */
*k*/
);
379 /* --- @key_drop@ --- *
381 * Arguments: @key_data *k@ = pointer to key data to destroy
385 * Use: Drops a reference to key data, destroying it if necessary.
388 #define KEY_DROP(k) do { \
395 extern void key_drop(key_data */
*k*/
);
397 /* --- @key_do@ --- *
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
405 * Returns: Nonzero return code from function, or zero.
407 * Use: Runs a function over all the leaves of a key.
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*/
),
415 /* --- @key_copydata@ --- *
417 * Arguments: @key_data *k@ = key data to copy
418 * @const key_filter *kf@ = pointer to filter block
420 * Returns: Pointer to a copy of the data, or null if the root subkey
421 * didn't match the filter.
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
429 extern key_data
*key_copydata(key_data */
*k*/
, const key_filter */
*kf*/
);
431 /*----- Textual encoding --------------------------------------------------*/
433 /* --- @key_read@ --- *
435 * Arguments: @const char *p@ = pointer to textual key representation
436 * @char **pp@ = where to store the end pointer
438 * Returns: The newly-read key data, or null if it failed.
440 * Use: Parses a textual key description.
443 extern key_data
*key_read(const char */
*p*/
, char **/
*pp*/
);
445 /* --- @key_write@ --- *
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
451 * Returns: Nonzero if any items were actually written.
453 * Use: Writes a key in a textual encoding.
456 extern int key_write(key_data */
*k*/
, dstr */
*d*/
, const key_filter */
*kf*/
);
458 /*----- Key binary encoding -----------------------------------------------*/
460 /* --- @key_decode@ --- *
462 * Arguments: @const void *p@ = pointer to buffer to read
463 * @size_t sz@ = size of the buffer
465 * Returns: The newly-read key data, or null if it failed.
467 * Use: Decodes a binary representation of a key.
470 extern key_data
*key_decode(const void */
*p*/
, size_t /*sz*/);
472 /* --- @key_encode@ --- *
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
478 * Returns: Nonzero if any items were actually written.
480 * Use: Encodes a key block as binary data.
483 extern int key_encode(key_data */
*k*/
, dstr */
*d*/
,
484 const key_filter */
*kf*/
);
486 /*----- Packing and unpacking keys ----------------------------------------*/
488 /* --- @key_pack@ --- *
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
494 * Returns: Error code, or zero.
496 * Use: Packs a key from a data structure.
499 extern int key_pack(key_packdef */
*kp*/
, key_data
**/
*kd*/
, dstr */
*d*/
);
501 /* --- @key_unpack@ --- *
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
507 * Returns: Error code, or zero.
509 * Use: Unpacks a key into an appropriate data structure.
512 extern int key_unpack(key_packdef */
*kp*/
, key_data */
*kd*/
, dstr */
*d*/
);
514 /* --- @key_unpackdone@ --- *
516 * Arguments: @key_packdef *kp@ = pointer to packing definition
520 * Use: Frees the key components contained within a packing
521 * definition, created during key unpacking.
524 extern void key_unpackdone(key_packdef */
*kp*/
);
526 /*----- Key encryption ----------------------------------------------------*/
528 /* --- @key_lock@ --- *
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
537 * Use: Encrypts a key data block using a secret.
540 extern void key_lock(key_data
**/
*kt*/
, key_data */
*k*/
,
541 const void */
*e*/
, size_t /*esz*/);
543 /* --- @key_unlock@ --- *
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
550 * Returns: Zero for success, or a @KERR_@ error code.
552 * Use: Unlocks a key using a secret.
555 extern int key_unlock(key_data
**/
*kt*/
, key_data */
*k*/
,
556 const void */
*e*/
, size_t /*esz*/);
558 /* --- @key_plock@ --- *
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
564 * Returns: Zero if successful, a @KERR@ error code on failure.
566 * Use: Locks a key by encrypting it with a passphrase.
569 extern int key_plock(key_data
**/
*kt*/
, key_data */
*k*/
,
570 const char */
*tag*/
);
572 /* --- @key_punlock@ --- *
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
578 * Returns: Zero if successful, a @KERR@ error code on failure.
580 * Use: Unlocks a passphrase-locked key.
583 extern int key_punlock(key_data
**/
*kt*/
, key_data */
*k*/
,
584 const char */
*tag*/
);
586 /*----- That's all, folks -------------------------------------------------*/