/* * Secure eXtended Message Passing framework * Secure eXtended Transport layer implementation: (libsxt) * - very similar to SSH2/TLS * - using already proven and tested crypto algos * - better than TLS for message passing * * PublicPrivateKeyPairs operation API * * (c) Alexander Vdolainen 2016 * * libsxmp is free software: you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published * by the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * libsxmp 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see ."; * */ #include #include #include #include #include #include #include #include #include #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; }