You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
libsxmp/sxt/ppkp_ops.c

134 lines
2.9 KiB
C

/*
* 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 <avdolainen@zoho.com>
*
* 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 <http://www.gnu.org/licenses/>.";
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <fcntl.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/engine.h>
#include <sxt/errno.h>
#include <sxt/sxtkey.h>
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;
}