From cabcb72dbbb2496d0383454a32bc4e8517b996e0 Mon Sep 17 00:00:00 2001 From: Alexander Vdolainen Date: Mon, 6 Jun 2016 00:12:06 +0300 Subject: [PATCH] sxt: added few key ops; --- include/sxt/sxtkey.h | 4 ++ sxt/ppkp_ops.c | 105 +++++++++++++++++++++++++++++++++++++------ 2 files changed, 96 insertions(+), 13 deletions(-) diff --git a/include/sxt/sxtkey.h b/include/sxt/sxtkey.h index d758d8e..56f85bc 100644 --- a/include/sxt/sxtkey.h +++ b/include/sxt/sxtkey.h @@ -37,6 +37,10 @@ typedef uint8_t ed25519_pubkey[ED25519_PK_LEN]; typedef uint8_t ed25519_privkey[ED25519_SK_LEN]; typedef uint8_t ed25519_signature[ED25519_SIG_LEN]; +/* flags */ +#define SXT_PPKP_PRIVATE (1 << 2) +#define SXT_PPKP_PUBLIC (1 << 1) + /* here the supported types in SXT */ #define PPKP_ED25519 0xa diff --git a/sxt/ppkp_ops.c b/sxt/ppkp_ops.c index 9844385..9ae0541 100644 --- a/sxt/ppkp_ops.c +++ b/sxt/ppkp_ops.c @@ -26,29 +26,108 @@ #include #include +#include #include #include -#include #include #include #include -#include #include -#ifdef WIN32 -#include -#define EBADE 1 -#define NETDB_SUCCESS 0 -#else -#include -#include -#include -#include -#endif - #include #include #include +#include #include +int sxt_key_generate(sxtkey_t *key, int type, int opt) +{ + int r = 0; + + if(!key) return SXT_EINVAL; + + /* set defaults */ + key->type = type; + key->flags = SXT_PPKP_PRIVATE | SXT_PPKP_PUBLIC; + + switch(type) { + case PPKP_ED25519: + key->pubkey = malloc(sizeof(ed25519_pubkey)); + if(!key->pubkey) { + r = SXT_ENOMEM; + goto __fall; + } + key->privkey = malloc(sizeof(ed25519_privkey)); + if(!key->privkey) { + r = SXT_ENOMEM; + goto __safefall0; + } + + r = crypto_sign_ed25519_keypair(*key->pubkey, *key->privkey); + if(r) { + r = SXT_ECRYPTO; + goto __safefall0; + } + break; + default: + return SXT_EINVAL; + } + + if(!r) return SXT_SUCCESS; + + __safefall0: + if(key->pubkey) free(key->pubkey); + if(key->privkey) free(key->privkey); + + __fall: + return r; +} + +sxtkey_t *sxt_key_alloc(void) +{ + sxtkey_t *key = malloc(sizeof(sxtkey_t)); + + if(!key) return NULL; + + return key; +} + +void sxt_key_burn(sxtkey_t *key) +{ + if(!key) return; + + key->priv = NULL; + + switch(key->type) { + case PPKP_ED25519: + if(key->pubkey) memset(key->pubkey, 0, sizeof(ed25519_pubkey)); + if(key->privkey) memset(key->privkey, 0, sizeof(ed25519_privkey)); + break; + default: + return; + } + + key->type = 0; + key->flags = 0; + + return; +} + +void sxt_key_free(sxtkey_t *key) +{ + if(!key) return; + + switch(key->type) { + case PPKP_ED25519: + if(key->pubkey) free(key->pubkey); + if(key->privkey) free(key->privkey); + break; + default: return; /* cannot free unrecognized key due to the + * potential memleak + */ + } + + free(key); + return; +}