Renesas / SecureDweet
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers rsa.h Source File

rsa.h

00001 /* rsa.h
00002  *
00003  * Copyright (C) 2006-2016 wolfSSL Inc.
00004  *
00005  * This file is part of wolfSSL.
00006  *
00007  * wolfSSL 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 2 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * wolfSSL 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 this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
00020  */
00021 
00022 
00023 #ifndef WOLF_CRYPT_RSA_H
00024 #define WOLF_CRYPT_RSA_H
00025 
00026 #include <wolfssl/wolfcrypt/types.h>
00027 
00028 #ifndef NO_RSA
00029 
00030 /* allow for user to plug in own crypto */
00031 #if !defined(HAVE_FIPS) && (defined(HAVE_USER_RSA) || defined(HAVE_FAST_RSA))
00032     #include "user_rsa.h"
00033 #else
00034 
00035 #ifdef HAVE_FIPS
00036 /* for fips @wc_fips */
00037 #include <cyassl/ctaocrypt/rsa.h>
00038 #if defined(CYASSL_KEY_GEN) && !defined(WOLFSSL_KEY_GEN)
00039     #define WOLFSSL_KEY_GEN
00040 #endif
00041 #else
00042     #include <wolfssl/wolfcrypt/integer.h>
00043     #include <wolfssl/wolfcrypt/random.h>
00044 #endif /* HAVE_FIPS */
00045 
00046 /* header file needed for OAEP padding */
00047 #include <wolfssl/wolfcrypt/hash.h>
00048 
00049 #ifdef __cplusplus
00050     extern "C" {
00051 #endif
00052 
00053 /* avoid redefinition of structs */
00054 #if !defined(HAVE_FIPS)
00055 #define WOLFSSL_RSA_CAVIUM_MAGIC 0xBEEF0006
00056 
00057 enum {
00058     RSA_PUBLIC   = 0,
00059     RSA_PRIVATE  = 1,
00060 };
00061 
00062 
00063 /* RSA */
00064 typedef struct RsaKey {
00065     mp_int n, e, d, p, q, dP, dQ, u;
00066     int   type;                               /* public or private */
00067     void* heap;                               /* for user memory overrides */
00068 #ifdef HAVE_CAVIUM
00069     int    devId;           /* nitrox device id */
00070     word32 magic;           /* using cavium magic */
00071     word64 contextHandle;   /* nitrox context memory handle */
00072     byte*  c_n;             /* cavium byte buffers for key parts */
00073     byte*  c_e;
00074     byte*  c_d;
00075     byte*  c_p;
00076     byte*  c_q;
00077     byte*  c_dP;
00078     byte*  c_dQ;
00079     byte*  c_u;             /* sizes in bytes */
00080     word16 c_nSz, c_eSz, c_dSz, c_pSz, c_qSz, c_dP_Sz, c_dQ_Sz, c_uSz;
00081 #endif
00082 } RsaKey;
00083 #endif /*HAVE_FIPS */
00084 
00085 WOLFSSL_API int  wc_InitRsaKey(RsaKey* key, void*);
00086 WOLFSSL_API int  wc_FreeRsaKey(RsaKey* key);
00087 
00088 WOLFSSL_API int  wc_RsaPublicEncrypt(const byte* in, word32 inLen, byte* out,
00089                                  word32 outLen, RsaKey* key, WC_RNG* rng);
00090 WOLFSSL_API int  wc_RsaPrivateDecryptInline(byte* in, word32 inLen, byte** out,
00091                                         RsaKey* key);
00092 WOLFSSL_API int  wc_RsaPrivateDecrypt(const byte* in, word32 inLen, byte* out,
00093                                   word32 outLen, RsaKey* key);
00094 WOLFSSL_API int  wc_RsaSSL_Sign(const byte* in, word32 inLen, byte* out,
00095                             word32 outLen, RsaKey* key, WC_RNG* rng);
00096 WOLFSSL_API int  wc_RsaSSL_VerifyInline(byte* in, word32 inLen, byte** out,
00097                                     RsaKey* key);
00098 WOLFSSL_API int  wc_RsaSSL_Verify(const byte* in, word32 inLen, byte* out,
00099                               word32 outLen, RsaKey* key);
00100 WOLFSSL_API int  wc_RsaEncryptSize(RsaKey* key);
00101 
00102 #ifndef HAVE_FIPS /* to avoid asn duplicate symbols @wc_fips */
00103 WOLFSSL_API int  wc_RsaPrivateKeyDecode(const byte* input, word32* inOutIdx,
00104                                                                RsaKey*, word32);
00105 WOLFSSL_API int  wc_RsaPublicKeyDecode(const byte* input, word32* inOutIdx,
00106                                                                RsaKey*, word32);
00107 WOLFSSL_API int  wc_RsaPublicKeyDecodeRaw(const byte* n, word32 nSz,
00108                                         const byte* e, word32 eSz, RsaKey* key);
00109 #ifdef WOLFSSL_KEY_GEN
00110     WOLFSSL_API int wc_RsaKeyToDer(RsaKey*, byte* output, word32 inLen);
00111 #endif
00112 
00113 /*
00114    choice of padding added after fips, so not available when using fips RSA
00115  */
00116 
00117 /* Mask Generation Function Identifiers */
00118 #define WC_MGF1SHA1   26
00119 #define WC_MGF1SHA256 1
00120 #define WC_MGF1SHA384 2
00121 #define WC_MGF1SHA512 3
00122 
00123 /* Padding types */
00124 #define WC_RSA_PKCSV15_PAD 0
00125 #define WC_RSA_OAEP_PAD    1
00126 
00127 WOLFSSL_API int  wc_RsaPublicEncrypt_ex(const byte* in, word32 inLen, byte* out,
00128                    word32 outLen, RsaKey* key, WC_RNG* rng, int type,
00129                    enum wc_HashType hash, int mgf, byte* label, word32 lableSz);
00130 WOLFSSL_API int  wc_RsaPrivateDecrypt_ex(const byte* in, word32 inLen,
00131                    byte* out, word32 outLen, RsaKey* key, int type,
00132                    enum wc_HashType hash, int mgf, byte* label, word32 lableSz);
00133 WOLFSSL_API int  wc_RsaPrivateDecryptInline_ex(byte* in, word32 inLen,
00134                       byte** out, RsaKey* key, int type, enum wc_HashType hash,
00135                       int mgf, byte* label, word32 lableSz);
00136 #endif /* HAVE_FIPS*/
00137 WOLFSSL_API int  wc_RsaFlattenPublicKey(RsaKey*, byte*, word32*, byte*,
00138                                                                        word32*);
00139 
00140 #ifdef WOLFSSL_KEY_GEN
00141     WOLFSSL_API int wc_RsaKeyToPublicDer(RsaKey*, byte* output, word32 inLen);
00142     WOLFSSL_API int wc_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng);
00143 #endif
00144 
00145 #ifdef HAVE_CAVIUM
00146     WOLFSSL_API int  wc_RsaInitCavium(RsaKey*, int);
00147     WOLFSSL_API void wc_RsaFreeCavium(RsaKey*);
00148 #endif
00149 #endif /* HAVE_USER_RSA */
00150 #ifdef __cplusplus
00151     } /* extern "C" */
00152 #endif
00153 
00154 #endif /* NO_RSA */
00155 #endif /* WOLF_CRYPT_RSA_H */
00156 
00157