+/* -*-c-*-
+ *
+ * $Id: key-data.h,v 1.1 2000/02/12 18:21:23 mdw Exp $
+ *
+ * Manipulating key data
+ *
+ * (c) 1999 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------*
+ *
+ * This file is part of Catacomb.
+ *
+ * Catacomb is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Library General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * Catacomb 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 Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with Catacomb; if not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
+ */
+
+/*----- Revision history --------------------------------------------------*
+ *
+ * $Log: key-data.h,v $
+ * Revision 1.1 2000/02/12 18:21:23 mdw
+ * Overhaul of key management (again).
+ *
+ */
+
+#ifndef CATACOMB_KEY_DATA_H
+#define CATACOMB_KEY_DATA_H
+
+#ifdef __cplusplus
+ extern "C" {
+#endif
+
+/*----- Header files ------------------------------------------------------*/
+
+#include <stddef.h>
+
+#include <mLib/bits.h>
+#include <mLib/dstr.h>
+#include <mLib/sym.h>
+
+#ifndef CATACOMB_MP_H
+# include "mp.h"
+#endif
+
+/*----- Data structures ---------------------------------------------------*/
+
+/* --- Key binary data --- */
+
+typedef struct key_bin {
+ octet *k; /* Pointer to key data */
+ size_t sz; /* Size of the key data (in bytes) */
+} key_bin;
+
+/* --- Key data structure --- */
+
+typedef struct key_data {
+ unsigned e; /* Encoding type for key data */
+ union {
+ key_bin k; /* Binary key data */
+ mp *m; /* Multiprecision integer */
+ sym_table s; /* Structured key data */
+ } u;
+} key_data;
+
+typedef struct key_struct {
+ sym_base _b;
+ key_data k;
+} key_struct;
+
+/* --- Key binary encoding --- *
+ *
+ * The binary encoding consists of a header containing a 16-bit encoding type
+ * and a 16-bit length, followed immediately by the key data, followed by
+ * between zero and three zero bytes to make the total length a multiple of
+ * four. The format of the following data depends on the encoding type:
+ *
+ * @KENC_BINARY@ Binary data.
+ *
+ * @KENC_MP@ Octet array interpreted in big-endian byte order.
+ *
+ * @KENC_STRUCT@ An array of pairs, each containing a string (8-bit
+ * length followed by data and zero-padding to 4-byte
+ * boundary) and key binary encodings.
+ *
+ * @KENC_ENCRYPT@ Binary data, format
+ */
+
+/* --- Key encoding methods and other flags--- */
+
+enum {
+
+ /* --- Bottom two bits are the encoding type --- */
+
+ KF_ENCMASK = 0x03, /* Encoding mask */
+ KENC_BINARY = 0x00, /* Plain binary key (@k@) */
+ KENC_MP = 0x01, /* Multiprecision integer (@i@) */
+ KENC_STRUCT = 0x02, /* Structured key data (@s@) */
+ KENC_ENCRYPT = 0x03, /* Encrypted key type (@k@) */
+
+ /* --- Key category bits --- */
+
+ KF_CATMASK = 0x0c, /* Category mask */
+ KCAT_SYMM = 0x00, /* Symmetric encryption key */
+ KCAT_PRIV = 0x04, /* Private (asymmetric) key */
+ KCAT_PUB = 0x08, /* Public (asymmetric) key */
+ KCAT_SHARE = 0x0c, /* Shared (asymmetric) key */
+ KF_NONSECRET = 0x08, /* Bit flag for non-secret keys */
+
+ /* --- Other flags --- */
+
+ KF_BURN = 0x10, /* Burn key after use */
+ KF_TEMP = 0x20, /* Temporary copy flag */
+
+ /* --- Tag end --- */
+
+ KENC_MAX /* Dummy limit constant */
+};
+
+/* --- Key flag filtering --- */
+
+typedef struct key_filter {
+ unsigned f;
+ unsigned m;
+} key_filter;
+
+/* --- Matching aginst key selection --- */
+
+#define KEY_MATCH(kd, kf) \
+ (!(kf) || \
+ ((kd)->e & KF_ENCMASK) == KENC_STRUCT || \
+ ((kd)->e & (kf)->m) == (kf)->f)
+
+/*----- Key flags and filtering -------------------------------------------*/
+
+/* --- @key_readflags@ --- *
+ *
+ * Arguments: @const char *p@ = pointer to string to read
+ * @char **pp@ = where to store the end pointer
+ * @unsigned *ff@ = where to store the flags
+ * @unsigned *mm@ = where to store the mask
+ *
+ * Returns: Zero if all went well, nonzero if there was an error.
+ *
+ * Use: Reads a flag string.
+ */
+
+extern int key_readflags(const char */*p*/, char **/*pp*/,
+ unsigned */*ff*/, unsigned */*mm*/);
+
+/* --- @key_writeflags@ --- *
+ *
+ * Arguments: @unsigned f@ = flags to write
+ * @dstr *d@ = pointer to destination string
+ *
+ * Returns: ---
+ *
+ * Use: Emits a flags word as a string representation.
+ */
+
+extern void key_writeflags(unsigned /*f*/, dstr */*d*/);
+
+/* --- @key_match@ --- *
+ *
+ * Arguments: @key_data *k@ = pointer to key data block
+ * @const key_filter *kf@ = pointer to filter block
+ *
+ * Returns: Nonzero if the key matches the filter.
+ *
+ * Use: Checks whether a key matches a filter.
+ */
+
+extern int key_match(key_data */*k*/, const key_filter */*kf*/);
+
+/*----- Setting new key data ----------------------------------------------*/
+
+/* --- @key_binary@ --- *
+ *
+ * Arguments: @key_data *k@ = pointer to key data block
+ * @const void *p@ = pointer to key data
+ * @size_t sz@ = size of the key data
+ *
+ * Returns: ---
+ *
+ * Use: Sets a binary key in a key data block.
+ */
+
+extern void key_binary(key_data */*k*/, const void */*p*/, size_t /*sz*/);
+
+/* --- @key_encrypted@ --- *
+ *
+ * Arguments: @key_data *k@ = pointer to key data block
+ * @const void *p@ = pointer to key data
+ * @size_t sz@ = size of the key data
+ *
+ * Returns: ---
+ *
+ * Use: Sets an encrypted key in a key data block.
+ */
+
+extern void key_encrypted(key_data */*k*/, const void */*p*/, size_t /*sz*/);
+
+/* --- @key_mp@ --- *
+ *
+ * Arguments: @key_data *k@ = pointer to key data block
+ * @mp *m@ = pointer to the value to set
+ *
+ * Returns: ---
+ *
+ * Use: Sets a multiprecision integer key in a key block.
+ */
+
+extern void key_mp(key_data */*k*/, mp */*m*/);
+
+/* --- @key_structure@ --- *
+ *
+ * Arguments: @key_data *k@ = pointer to key data block
+ *
+ * Returns: ---
+ *
+ * Use: Initializes a structured key type.
+ */
+
+extern void key_structure(key_data */*k*/);
+
+/* --- @key_structfind@ --- *
+ *
+ * Arguments: @key_data *k@ = pointer to key data block
+ * @const char *tag@ = pointer to tag string
+ *
+ * Returns: Pointer to key data block, or null.
+ *
+ * Use: Looks up the tag in a structured key.
+ */
+
+extern key_data *key_structfind(key_data */*k*/, const char */*tag*/);
+
+/* --- @key_structcreate@ --- *
+ *
+ * Arguments: @key_data *k@ = pointer to key data block
+ * @const char *tag@ = pointer to tag string
+ *
+ * Returns: Pointer to newly created key data.
+ *
+ * Use: Creates a new uninitialized subkey.
+ */
+
+extern key_data *key_structcreate(key_data */*k*/, const char */*tag*/);
+
+/*----- Miscellaneous operations ------------------------------------------*/
+
+/* --- @key_destroy@ --- *
+ *
+ * Arguments: @key_data *k@ = pointer to key data to destroy
+ *
+ * Returns: ---
+ *
+ * Use: Destroys a lump of key data.
+ */
+
+extern void key_destroy(key_data */*k*/);
+
+/* --- @key_do@ --- *
+ *
+ * Arguments: @key_data *k@ = pointer to key data block
+ * @const key_filter *kf@ = pointer to filter block
+ * @dstr *d@ = pointer to base string
+ * @int (*func)(key_data *kd, dstr *d, void *p@ = function
+ * @void *p@ = argument to function
+ *
+ * Returns: Nonzero return code from function, or zero.
+ *
+ * Use: Runs a function over all the leaves of a key.
+ */
+
+extern int key_do(key_data */*k*/, const key_filter */*kf*/, dstr */*d*/,
+ int (*/*func*/)(key_data */*kd*/,
+ dstr */*d*/, void */*p*/),
+ void */*p*/);
+
+/* --- @key_copy@ --- *
+ *
+ * Arguments: @key_data *kd@ = pointer to destination data block
+ * @key_data *k@ = pointer to source data block
+ * @const key_filter *kf@ = pointer to filter block
+ *
+ * Returns: Nonzero if an item was actually copied.
+ *
+ * Use: Copies a chunk of key data from one place to another.
+ */
+
+extern int key_copy(key_data */*kd*/, key_data */*k*/,
+ const key_filter */*kf*/);
+
+/*----- Textual encoding --------------------------------------------------*/
+
+/* --- @key_read@ --- *
+ *
+ * Arguments: @const char *p@ = pointer to textual key representation
+ * @key_data *k@ = pointer to output block for key data
+ * @char **pp@ = where to store the end pointer
+ *
+ * Returns: Zero if all went well, nonzero if there was a problem.
+ *
+ * Use: Parses a textual key description.
+ */
+
+extern int key_read(const char */*p*/, key_data */*k*/, char **/*pp*/);
+
+/* --- @key_write@ --- *
+ *
+ * Arguments: @key_data *k@ = pointer to key data
+ * @dstr *d@ = destination string to write on
+ * @const key_filter *kf@ = pointer to key selection block
+ *
+ * Returns: Nonzero if any items were actually written.
+ *
+ * Use: Writes a key in a textual encoding.
+ */
+
+extern int key_write(key_data */*k*/, dstr */*d*/,
+ const key_filter */*kf*/);
+
+/*----- Key binary encoding -----------------------------------------------*/
+
+/* --- @key_decode@ --- *
+ *
+ * Arguments: @const void *p@ = pointer to buffer to read
+ * @size_t sz@ = size of the buffer
+ * @key_data *k@ = pointer to key data block to write to
+ *
+ * Returns: Zero if everything worked, nonzero otherwise.
+ *
+ * Use: Decodes a binary representation of a key.
+ */
+
+extern int key_decode(const void */*p*/, size_t /*sz*/, key_data */*k*/);
+
+/* --- @key_encode@ --- *
+ *
+ * Arguments: @key_data *k@ = pointer to key data block
+ * @dstr *d@ = pointer to destination string
+ * @const key_filter *kf@ = pointer to key selection block
+ *
+ * Returns: Nonzero if any items were actually written.
+ *
+ * Use: Encodes a key block as binary data.
+ */
+
+extern int key_encode(key_data */*k*/, dstr */*d*/,
+ const key_filter */*kf*/);
+
+/*----- Passphrase encryption ---------------------------------------------*/
+
+/* --- @key_plock@ --- *
+ *
+ * Arguments: @const char *tag@ = tag to use for passphrase
+ * @key_data *k@ = source key data block
+ * @key_data *kt@ = target key data block
+ *
+ * Returns: Zero if successful, nonzero if there was a problem.
+ *
+ * Use: Locks a key by encrypting it with a passphrase.
+ */
+
+extern int key_plock(const char */*tag*/, key_data */*k*/, key_data */*kt*/);
+
+/* --- @key_punlock@ --- *
+ *
+ * Arguments: @const char *tag@ = tag to use for passphrase
+ * @key_data *k@ = source key data block
+ * @key_data *kt@ = target key data block
+ *
+ * Returns: Zero if it worked, nonzero if it didn't.
+ *
+ * Use: Unlocks a passphrase-locked key.
+ */
+
+extern int key_punlock(const char */*tag*/,
+ key_data */*k*/, key_data */*kt*/);
+
+/*----- That's all, folks -------------------------------------------------*/
+
+#ifdef __cplusplus
+ }
+#endif
+
+#endif