ssh

Dependents:   OS

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers keygen.c Source File

keygen.c

00001 /* keygen.c
00002  *
00003  * Copyright (C) 2014-2016 wolfSSL Inc.
00004  *
00005  * This file is part of wolfSSH.
00006  *
00007  * wolfSSH is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 3 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * wolfSSH is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with wolfSSH.  If not, see <http://www.gnu.org/licenses/>.
00019  */
00020 
00021 
00022 /*
00023  * The keygen module contains utility functions wrapping the wolfCrypt
00024  * key generation functions to product SSH friendly keys.
00025  */
00026 
00027 
00028 #ifdef HAVE_CONFIG_H
00029     #include <config.h>
00030 #endif
00031 
00032 #include <wolfssl/options.h>
00033 #include <wolfcrypt/random.h>
00034 #include <wolfcrypt/rsa.h>
00035 #include <wolfssh/error.h>
00036 #include <wolfssh/keygen.h>
00037 #include <wolfssh/log.h>
00038 
00039 #ifdef WOLFSSH_KEYGEN
00040 
00041 #ifdef NO_INLINE
00042     #include <wolfssh/misc.h>
00043 #else
00044     #define WOLFSSH_MISC_INCLUDED
00045     #include "src/misc.c"
00046 #endif
00047 
00048 
00049 int wolfSSH_MakeRsaKey(byte* out, word32 outSz,
00050                        word32 size, word32 e)
00051 {
00052     int ret = WS_SUCCESS;
00053     WC_RNG rng;
00054 
00055     WLOG(WS_LOG_DEBUG, "Entering wolfSSH_MakeRsaKey()");
00056 
00057     if (wc_InitRng(&rng) != 0) {
00058         WLOG(WS_LOG_DEBUG, "Couldn't create RNG");
00059         ret = WS_CRYPTO_FAILED;
00060     }
00061 
00062     if (ret == WS_SUCCESS) {
00063         RsaKey key;
00064 
00065         if (wc_InitRsaKey(&key, NULL) != 0)
00066             ret = WS_CRYPTO_FAILED;
00067 
00068         if (ret == WS_SUCCESS) {
00069             if (wc_MakeRsaKey(&key, size, e, &rng) != 0) {
00070                 WLOG(WS_LOG_DEBUG, "RSA key generation failed");
00071                 ret = WS_CRYPTO_FAILED;
00072             }
00073         }
00074 
00075         if (ret == WS_SUCCESS) {
00076             int keySz;
00077 
00078             keySz = wc_RsaKeyToDer(&key, out, outSz);
00079             if (keySz < 0) {
00080                 WLOG(WS_LOG_DEBUG, "RSA key to DER failed");
00081                 ret = WS_CRYPTO_FAILED;
00082             }
00083             else
00084                 ret = keySz;
00085         }
00086 
00087         if (wc_FreeRsaKey(&key) != 0) {
00088             WLOG(WS_LOG_DEBUG, "RSA key free failed");
00089             ret = WS_CRYPTO_FAILED;
00090         }
00091 
00092         if (wc_FreeRng(&rng) != 0) {
00093             WLOG(WS_LOG_DEBUG, "Couldn't free RNG");
00094             ret = WS_CRYPTO_FAILED;
00095         }
00096     }
00097 
00098     WLOG(WS_LOG_DEBUG, "Leaving wolfSSH_MakeRsaKey(), ret = %d", ret);
00099     return ret;
00100 }
00101 
00102 #endif