#ifndef SIGSUM_KEY_MAP_H_INCLUDED
#define SIGSUM_KEY_MAP_H_INCLUDED

#include "ascii.h"
#include "crypto.h"

/* A public key and its hash. Intended to aid lookup of keys by hash,
   e.g., to match a cosignature key hash to the corresponding witness
   public key. */
struct sigsum_key_with_hash
{
  struct sigsum_hash hash;
  struct sigsum_pubkey key;
};

struct sigsum_key_map
{
  size_t num_keys;
  /* Entries must be sorted lexicographically by key hash. */
  const struct sigsum_key_with_hash *keys;
};

/* Looks up a public key by key hash. Returns NULL if not found. If
   IDX is non-NULL, matching index is stored there on success. */
const struct sigsum_pubkey *
sigsum_key_map_lookup (const struct sigsum_key_map *map,
		       const struct sigsum_hash *key_hash,
		       size_t *idx);

/* Initializes the key map, from an OpenSSH public key file. Empty
   lines and lines starting with the '#' character are ignored.
   Remaining lines must be of the form:

     ssh-ed25519 BASE64-KEYBLOB [COMMENT]

   Requires that there's at least one public key, that all key are of
   type ssh-ed25519, and that there are no duplicates. Allocates
   storage for the parsed keys.
*/
bool
sigsum_key_map_parse_ssh (struct sigsum_key_map *map,
			  size_t size, const char *data,
			  /* If non-NULL, line number and a statically
			     allocated error message is returned on
			     failure. */
			  struct sigsum_error *err);

/* Clears a key map initialized using sigsum_key_map_parse_ssh, and
   deallocates storage. */
void
sigsum_key_map_clear (struct sigsum_key_map *map);

#endif /* SIGSUM_KEY_MAP_H_INCLUDED */
