wolfssl-examples/wolfCLU/clu_src/genkey/clu_genkey.c

190 lines
6.5 KiB
C

/* clu_genkey.c
*
* Copyright (C) 2006-2017 wolfSSL Inc.
*
* This file is part of wolfSSL. (formerly known as CyaSSL)
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "clu_include/clu_header_main.h"
#include "clu_include/genkey/clu_genkey.h"
#include "clu_include/x509/clu_cert.h" /* PER_FORM/DER_FORM */
#include <wolfssl/wolfcrypt/asn_public.h> /* wc_DerToPem */
#ifdef HAVE_ED25519
int wolfCLU_genKey_ED25519(WC_RNG* rng, char* fOutNm, int directive, int format)
{
int ret = -1; /* return value */
int fOutNmSz = XSTRLEN(fOutNm); /* file name without append */
int fOutNmAppendSz = 6; /* # of bytes to append to file name */
int flag_outputPub = 0; /* set if outputting both priv/pub */
char privAppend[6] = ".priv\0"; /* last part of the priv file name */
char pubAppend[6] = ".pub\0\0"; /* last part of the pub file name*/
byte privKeyBuf[ED25519_KEY_SIZE*2]; /* will hold public & private parts */
byte pubKeyBuf[ED25519_KEY_SIZE]; /* holds just the public key part */
word32 privKeySz; /* size of private key */
word32 pubKeySz; /* size of public key */
ed25519_key edKeyOut; /* the ed25519 key structure */
char* finalOutFNm; /* file name + append */
FILE* file; /* file stream */
printf("fOutNm = %s\n", fOutNm);
/*--------------- INIT ---------------------*/
ret = wc_ed25519_init(&edKeyOut);
if (ret != 0)
return ret;
/*--------------- MAKE KEY ---------------------*/
ret = wc_ed25519_make_key(rng, ED25519_KEY_SIZE, &edKeyOut);
if (ret != 0)
return ret;
/*--------------- GET KEY SIZES ---------------------*/
privKeySz = wc_ed25519_priv_size(&edKeyOut);
if (privKeySz <= 0)
return WC_KEY_SIZE_E;
pubKeySz = wc_ed25519_pub_size(&edKeyOut);
if (pubKeySz <= 0)
return WC_KEY_SIZE_E;
/*--------------- EXPORT KEYS TO BUFFERS ---------------------*/
ret = wc_ed25519_export_key(&edKeyOut, privKeyBuf, &privKeySz, pubKeyBuf,
&pubKeySz);
/*--------------- CONVERT TO PEM IF APPLICABLE ---------------------*/
if (format == PEM_FORM) {
printf("Der to Pem for ed25519 key not yet implemented\n");
printf("FEATURE COMING SOON!\n");
return FEATURE_COMING_SOON;
}
/*--------------- OUTPUT KEYS TO FILE(S) ---------------------*/
finalOutFNm = (char*) XMALLOC( (fOutNmSz + fOutNmAppendSz), HEAP_HINT,
DYNAMIC_TYPE_TMP_BUFFER);
if (finalOutFNm == NULL)
return MEMORY_E;
/* get the first part of the file name setup */
XMEMSET(finalOutFNm, 0, fOutNmSz + fOutNmAppendSz);
XMEMCPY(finalOutFNm, fOutNm, fOutNmSz);
switch(directive) {
case PRIV_AND_PUB:
flag_outputPub = 1;
/* Fall through to PRIV_ONLY */
case PRIV_ONLY:
/* add on the final part of the file name ".priv" */
XMEMCPY(finalOutFNm+fOutNmSz, privAppend, fOutNmAppendSz);
printf("finalOutFNm = %s\n", finalOutFNm);
file = fopen(finalOutFNm, "wb");
if (!file) {
XFREE(finalOutFNm, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
return OUTPUT_FILE_ERROR;
}
ret = (int) fwrite(privKeyBuf, 1, privKeySz, file);
if (ret <= 0) {
fclose(file);
XFREE(finalOutFNm, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
return OUTPUT_FILE_ERROR;
}
fclose(file);
if (flag_outputPub == 0) {
break;
} /* else fall through to PUB_ONLY if flag_outputPub == 1*/
case PUB_ONLY:
/* add on the final part of the file name ".pub" */
XMEMCPY(finalOutFNm+fOutNmSz, pubAppend, fOutNmAppendSz);
printf("finalOutFNm = %s\n", finalOutFNm);
file = fopen(finalOutFNm, "wb");
if (!file) {
XFREE(finalOutFNm, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
return OUTPUT_FILE_ERROR;
}
ret = (int) fwrite(pubKeyBuf, 1, pubKeySz, file);
if (ret <= 0) {
fclose(file);
XFREE(finalOutFNm, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
return OUTPUT_FILE_ERROR;
}
fclose(file);
break;
default:
printf("Invalid directive\n");
XFREE(finalOutFNm, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
return BAD_FUNC_ARG;
}
XFREE(finalOutFNm, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
if (ret > 0) {
/* ret > 0 indicates a successful file write, set to zero for return */
ret = 0;
}
return ret;
}
#endif /* HAVE_ED25519 */
int wolfCLU_genKey_ECC()
{
int ret = FEATURE_COMING_SOON;
printf("Creating a ECC key: FEATURE COMING SOON\n");
return ret;
}
int wolfCLU_genKey_RSA()
{
int ret = FEATURE_COMING_SOON;
printf("Creating a RSA key: FEATURE COMING SOON\n");
return ret;
}
/*
* makes a cyptographically secure key by stretching a user entered pwdKey
*/
int wolfCLU_genKey_PWDBASED(RNG* rng, byte* pwdKey, int size, byte* salt, int pad)
{
int ret; /* return variable */
/* randomly generates salt */
ret = wc_RNG_GenerateBlock(rng, salt, SALT_SIZE-1);
if (ret != 0)
return ret;
/* set first value of salt to let us know
* if message has padding or not
*/
if (pad == 0)
salt[0] = 0;
/* stretches pwdKey */
ret = (int) wc_PBKDF2(pwdKey, pwdKey, (int) strlen((const char*)pwdKey), salt, SALT_SIZE,
4096, size, SHA256);
if (ret != 0)
return ret;
return 0;
}