3 * $Id: key-data.h,v 1.1 2000/02/12 18:21:23 mdw Exp $
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 /*----- Revision history --------------------------------------------------*
32 * $Log: key-data.h,v $
33 * Revision 1.1 2000/02/12 18:21:23 mdw
34 * Overhaul of key management (again).
38 #ifndef CATACOMB_KEY_DATA_H
39 #define CATACOMB_KEY_DATA_H
45 /*----- Header files ------------------------------------------------------*/
49 #include <mLib/bits.h>
50 #include <mLib/dstr.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 */
71 key_bin k
; /* Binary key data */
72 mp
*m
; /* Multiprecision integer */
73 sym_table s
; /* Structured key data */
77 typedef struct key_struct
{
82 /* --- Key binary encoding --- *
84 * The binary encoding consists of a header containing a 16-bit encoding type
85 * and a 16-bit length, followed immediately by the key data, followed by
86 * between zero and three zero bytes to make the total length a multiple of
87 * four. The format of the following data depends on the encoding type:
89 * @KENC_BINARY@ Binary data.
91 * @KENC_MP@ Octet array interpreted in big-endian byte order.
93 * @KENC_STRUCT@ An array of pairs, each containing a string (8-bit
94 * length followed by data and zero-padding to 4-byte
95 * boundary) and key binary encodings.
97 * @KENC_ENCRYPT@ Binary data, format
100 /* --- Key encoding methods and other flags--- */
104 /* --- Bottom two bits are the encoding type --- */
106 KF_ENCMASK
= 0x03, /* Encoding mask */
107 KENC_BINARY
= 0x00, /* Plain binary key (@k@) */
108 KENC_MP
= 0x01, /* Multiprecision integer (@i@) */
109 KENC_STRUCT
= 0x02, /* Structured key data (@s@) */
110 KENC_ENCRYPT
= 0x03, /* Encrypted key type (@k@) */
112 /* --- Key category bits --- */
114 KF_CATMASK
= 0x0c, /* Category mask */
115 KCAT_SYMM
= 0x00, /* Symmetric encryption key */
116 KCAT_PRIV
= 0x04, /* Private (asymmetric) key */
117 KCAT_PUB
= 0x08, /* Public (asymmetric) key */
118 KCAT_SHARE
= 0x0c, /* Shared (asymmetric) key */
119 KF_NONSECRET
= 0x08, /* Bit flag for non-secret keys */
121 /* --- Other flags --- */
123 KF_BURN
= 0x10, /* Burn key after use */
124 KF_TEMP
= 0x20, /* Temporary copy flag */
126 /* --- Tag end --- */
128 KENC_MAX
/* Dummy limit constant */
131 /* --- Key flag filtering --- */
133 typedef struct key_filter
{
138 /* --- Matching aginst key selection --- */
140 #define KEY_MATCH(kd, kf) \
142 ((kd)->e & KF_ENCMASK) == KENC_STRUCT || \
143 ((kd)->e & (kf)->m) == (kf)->f)
145 /*----- Key flags and filtering -------------------------------------------*/
147 /* --- @key_readflags@ --- *
149 * Arguments: @const char *p@ = pointer to string to read
150 * @char **pp@ = where to store the end pointer
151 * @unsigned *ff@ = where to store the flags
152 * @unsigned *mm@ = where to store the mask
154 * Returns: Zero if all went well, nonzero if there was an error.
156 * Use: Reads a flag string.
159 extern int key_readflags(const char */
*p*/
, char **/
*pp*/
,
160 unsigned */
*ff*/
, unsigned */
*mm*/
);
162 /* --- @key_writeflags@ --- *
164 * Arguments: @unsigned f@ = flags to write
165 * @dstr *d@ = pointer to destination string
169 * Use: Emits a flags word as a string representation.
172 extern void key_writeflags(unsigned /*f*/, dstr */
*d*/
);
174 /* --- @key_match@ --- *
176 * Arguments: @key_data *k@ = pointer to key data block
177 * @const key_filter *kf@ = pointer to filter block
179 * Returns: Nonzero if the key matches the filter.
181 * Use: Checks whether a key matches a filter.
184 extern int key_match(key_data */
*k*/
, const key_filter */
*kf*/
);
186 /*----- Setting new key data ----------------------------------------------*/
188 /* --- @key_binary@ --- *
190 * Arguments: @key_data *k@ = pointer to key data block
191 * @const void *p@ = pointer to key data
192 * @size_t sz@ = size of the key data
196 * Use: Sets a binary key in a key data block.
199 extern void key_binary(key_data */
*k*/
, const void */
*p*/
, size_t /*sz*/);
201 /* --- @key_encrypted@ --- *
203 * Arguments: @key_data *k@ = pointer to key data block
204 * @const void *p@ = pointer to key data
205 * @size_t sz@ = size of the key data
209 * Use: Sets an encrypted key in a key data block.
212 extern void key_encrypted(key_data */
*k*/
, const void */
*p*/
, size_t /*sz*/);
214 /* --- @key_mp@ --- *
216 * Arguments: @key_data *k@ = pointer to key data block
217 * @mp *m@ = pointer to the value to set
221 * Use: Sets a multiprecision integer key in a key block.
224 extern void key_mp(key_data */
*k*/
, mp */
*m*/
);
226 /* --- @key_structure@ --- *
228 * Arguments: @key_data *k@ = pointer to key data block
232 * Use: Initializes a structured key type.
235 extern void key_structure(key_data */
*k*/
);
237 /* --- @key_structfind@ --- *
239 * Arguments: @key_data *k@ = pointer to key data block
240 * @const char *tag@ = pointer to tag string
242 * Returns: Pointer to key data block, or null.
244 * Use: Looks up the tag in a structured key.
247 extern key_data
*key_structfind(key_data */
*k*/
, const char */
*tag*/
);
249 /* --- @key_structcreate@ --- *
251 * Arguments: @key_data *k@ = pointer to key data block
252 * @const char *tag@ = pointer to tag string
254 * Returns: Pointer to newly created key data.
256 * Use: Creates a new uninitialized subkey.
259 extern key_data
*key_structcreate(key_data */
*k*/
, const char */
*tag*/
);
261 /*----- Miscellaneous operations ------------------------------------------*/
263 /* --- @key_destroy@ --- *
265 * Arguments: @key_data *k@ = pointer to key data to destroy
269 * Use: Destroys a lump of key data.
272 extern void key_destroy(key_data */
*k*/
);
274 /* --- @key_do@ --- *
276 * Arguments: @key_data *k@ = pointer to key data block
277 * @const key_filter *kf@ = pointer to filter block
278 * @dstr *d@ = pointer to base string
279 * @int (*func)(key_data *kd, dstr *d, void *p@ = function
280 * @void *p@ = argument to function
282 * Returns: Nonzero return code from function, or zero.
284 * Use: Runs a function over all the leaves of a key.
287 extern int key_do(key_data */
*k*/
, const key_filter */
*kf*/
, dstr */
*d*/
,
288 int (*/
*func*/
)(key_data */
*kd*/
,
289 dstr */
*d*/
, void */
*p*/
),
292 /* --- @key_copy@ --- *
294 * Arguments: @key_data *kd@ = pointer to destination data block
295 * @key_data *k@ = pointer to source data block
296 * @const key_filter *kf@ = pointer to filter block
298 * Returns: Nonzero if an item was actually copied.
300 * Use: Copies a chunk of key data from one place to another.
303 extern int key_copy(key_data */
*kd*/
, key_data */
*k*/
,
304 const key_filter */
*kf*/
);
306 /*----- Textual encoding --------------------------------------------------*/
308 /* --- @key_read@ --- *
310 * Arguments: @const char *p@ = pointer to textual key representation
311 * @key_data *k@ = pointer to output block for key data
312 * @char **pp@ = where to store the end pointer
314 * Returns: Zero if all went well, nonzero if there was a problem.
316 * Use: Parses a textual key description.
319 extern int key_read(const char */
*p*/
, key_data */
*k*/
, char **/
*pp*/
);
321 /* --- @key_write@ --- *
323 * Arguments: @key_data *k@ = pointer to key data
324 * @dstr *d@ = destination string to write on
325 * @const key_filter *kf@ = pointer to key selection block
327 * Returns: Nonzero if any items were actually written.
329 * Use: Writes a key in a textual encoding.
332 extern int key_write(key_data */
*k*/
, dstr */
*d*/
,
333 const key_filter */
*kf*/
);
335 /*----- Key binary encoding -----------------------------------------------*/
337 /* --- @key_decode@ --- *
339 * Arguments: @const void *p@ = pointer to buffer to read
340 * @size_t sz@ = size of the buffer
341 * @key_data *k@ = pointer to key data block to write to
343 * Returns: Zero if everything worked, nonzero otherwise.
345 * Use: Decodes a binary representation of a key.
348 extern int key_decode(const void */
*p*/
, size_t /*sz*/, key_data */
*k*/
);
350 /* --- @key_encode@ --- *
352 * Arguments: @key_data *k@ = pointer to key data block
353 * @dstr *d@ = pointer to destination string
354 * @const key_filter *kf@ = pointer to key selection block
356 * Returns: Nonzero if any items were actually written.
358 * Use: Encodes a key block as binary data.
361 extern int key_encode(key_data */
*k*/
, dstr */
*d*/
,
362 const key_filter */
*kf*/
);
364 /*----- Passphrase encryption ---------------------------------------------*/
366 /* --- @key_plock@ --- *
368 * Arguments: @const char *tag@ = tag to use for passphrase
369 * @key_data *k@ = source key data block
370 * @key_data *kt@ = target key data block
372 * Returns: Zero if successful, nonzero if there was a problem.
374 * Use: Locks a key by encrypting it with a passphrase.
377 extern int key_plock(const char */
*tag*/
, key_data */
*k*/
, key_data */
*kt*/
);
379 /* --- @key_punlock@ --- *
381 * Arguments: @const char *tag@ = tag to use for passphrase
382 * @key_data *k@ = source key data block
383 * @key_data *kt@ = target key data block
385 * Returns: Zero if it worked, nonzero if it didn't.
387 * Use: Unlocks a passphrase-locked key.
390 extern int key_punlock(const char */
*tag*/
,
391 key_data */
*k*/
, key_data */
*kt*/
);
393 /*----- That's all, folks -------------------------------------------------*/