keypair.c (4791B)
1 #include <stdlib.h> 2 #include <string.h> 3 4 #include "sfinx.h" 5 6 // Keypair {{{ 7 // 8 // The secret is the seed, the public key is derived on demand 9 // init sets the default hash, free releases both halves 10 11 void sfinx_key_init(sfinx_key *key) { 12 if (!key) return; 13 memset(key, 0, sizeof(*key)); 14 key->hash = SFINX_HASH_DEFAULT; 15 key->path_len = -1; 16 } 17 18 void sfinx_key_free(sfinx_key *key) { 19 if (!key) return; 20 free(key->seed); 21 free(key->pub); 22 memset(key, 0, sizeof(*key)); 23 } 24 25 sfinx_status sfinx_key_derive(sfinx_key *key) { 26 size_t len; 27 uint8_t *pub; 28 sfinx_status status; 29 30 if (!key) { 31 return SFINX_ERR_ARGS; 32 } 33 if (key->pub) { 34 return SFINX_OK; 35 } 36 if (!key->seed) { 37 return SFINX_ERR_ARGS; 38 } 39 len = sfinx_hash_len(key->hash); 40 if (len == 0) { 41 return SFINX_ERR_ARGS; 42 } 43 if (key->path_len < 0) { 44 key->path_len = SFINX_PATH_LEN_DEFAULT; 45 } 46 pub = malloc(len); 47 if (!pub) { 48 return SFINX_ERR_NOSPACE; 49 } 50 status = sfinx_tree_public_key(key->hash, key->seed, key->seed_len, (size_t)key->path_len, pub, len, NULL); 51 if (status != SFINX_OK) { 52 free(pub); 53 return status; 54 } 55 key->pub = pub; 56 key->pub_len = len; 57 return SFINX_OK; 58 } 59 // }}} 60 61 // Registry {{{ 62 // 63 // Formats prepend themselves on load, so the newest wins ties in search 64 65 static sfinx_format *sfinx_formats = NULL; 66 67 void sfinx_format_register(sfinx_format *format) { 68 if (!format) return; 69 format->next = sfinx_formats; 70 sfinx_formats = format; 71 } 72 73 const sfinx_format *sfinx_format_find(const char *name) { 74 const sfinx_format *fmt; 75 if (!name) return NULL; 76 for (fmt = sfinx_formats; fmt; fmt = fmt->next) { 77 if (fmt->name && strcmp(fmt->name, name) == 0) { 78 return fmt; 79 } 80 } 81 return NULL; 82 } 83 84 static const sfinx_format *sfinx_format_detect_key(const uint8_t *data, size_t len) { 85 const sfinx_format *fmt; 86 for (fmt = sfinx_formats; fmt; fmt = fmt->next) { 87 if (fmt->key_detect && fmt->key_detect(data, len)) { 88 return fmt; 89 } 90 } 91 return NULL; 92 } 93 94 static const sfinx_format *sfinx_format_detect_sig(const uint8_t *data, size_t len) { 95 const sfinx_format *fmt; 96 for (fmt = sfinx_formats; fmt; fmt = fmt->next) { 97 if (fmt->sig_detect && fmt->sig_detect(data, len)) { 98 return fmt; 99 } 100 } 101 return NULL; 102 } 103 // }}} 104 105 // Dispatch {{{ 106 // 107 // Key decode mallocs into the key, every encode writes into the caller buffer 108 109 sfinx_status sfinx_key_decode(const uint8_t *data, size_t len, const char *format, sfinx_key *out) { 110 const sfinx_format *fmt; 111 sfinx_status status; 112 113 if (!data || !out) { 114 return SFINX_ERR_ARGS; 115 } 116 fmt = format ? sfinx_format_find(format) : sfinx_format_detect_key(data, len); 117 if (!fmt || !fmt->key_decode) { 118 return SFINX_ERR_FORMAT; 119 } 120 sfinx_key_init(out); 121 status = fmt->key_decode(data, len, out); 122 if (status != SFINX_OK) { 123 sfinx_key_free(out); 124 } 125 return status; 126 } 127 128 size_t sfinx_key_encode_len(const sfinx_key *key, const char *format) { 129 const sfinx_format *fmt; 130 if (!key || !format) { 131 return 0; 132 } 133 fmt = sfinx_format_find(format); 134 if (!fmt || !fmt->key_encode_len) { 135 return 0; 136 } 137 return fmt->key_encode_len(key); 138 } 139 140 sfinx_status sfinx_key_encode(const sfinx_key *key, const char *format, uint8_t *out, size_t cap, size_t *len_out) { 141 const sfinx_format *fmt; 142 if (!key || !format || !out) { 143 return SFINX_ERR_ARGS; 144 } 145 fmt = sfinx_format_find(format); 146 if (!fmt || !fmt->key_encode) { 147 return SFINX_ERR_ARGS; 148 } 149 return fmt->key_encode(key, out, cap, len_out); 150 } 151 152 sfinx_status sfinx_sig_decode(const uint8_t *data, size_t len, const char *format, uint8_t *out, size_t cap, 153 size_t *len_out) { 154 const sfinx_format *fmt; 155 if (!data || !out) { 156 return SFINX_ERR_ARGS; 157 } 158 if (format) { 159 fmt = sfinx_format_find(format); 160 } else { 161 fmt = sfinx_format_detect_sig(data, len); 162 if (!fmt) fmt = sfinx_format_find("raw"); 163 } 164 if (!fmt || !fmt->sig_decode) { 165 return SFINX_ERR_FORMAT; 166 } 167 return fmt->sig_decode(data, len, out, cap, len_out); 168 } 169 170 size_t sfinx_sig_encode_len(const uint8_t *sig, size_t sig_len, const char *format) { 171 const sfinx_format *fmt; 172 if (!sig || !format) { 173 return 0; 174 } 175 fmt = sfinx_format_find(format); 176 if (!fmt || !fmt->sig_encode_len) { 177 return 0; 178 } 179 return fmt->sig_encode_len(sig, sig_len); 180 } 181 182 sfinx_status sfinx_sig_encode(const uint8_t *sig, size_t sig_len, const char *format, uint8_t *out, size_t cap, 183 size_t *len_out) { 184 const sfinx_format *fmt; 185 if (!sig || !format || !out) { 186 return SFINX_ERR_ARGS; 187 } 188 fmt = sfinx_format_find(format); 189 if (!fmt || !fmt->sig_encode) { 190 return SFINX_ERR_ARGS; 191 } 192 return fmt->sig_encode(sig, sig_len, out, cap, len_out); 193 } 194 // }}} 195 196 // vim:fdm=marker:fdl=0