wolfSSL SSL/TLS library, support up to TLS1.3

Dependents:   CyaSSL-Twitter-OAuth4Tw Example-client-tls-cert TwitterReader TweetTest ... more

Committer:
wolfSSL
Date:
Tue Aug 22 10:48:22 2017 +0000
Revision:
13:f67a6c6013ca
wolfSSL3.12.0 with TLS1.3

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 13:f67a6c6013ca 1 /* rsa.h
wolfSSL 13:f67a6c6013ca 2 *
wolfSSL 13:f67a6c6013ca 3 * Copyright (C) 2006-2016 wolfSSL Inc.
wolfSSL 13:f67a6c6013ca 4 *
wolfSSL 13:f67a6c6013ca 5 * This file is part of wolfSSL.
wolfSSL 13:f67a6c6013ca 6 *
wolfSSL 13:f67a6c6013ca 7 * wolfSSL is free software; you can redistribute it and/or modify
wolfSSL 13:f67a6c6013ca 8 * it under the terms of the GNU General Public License as published by
wolfSSL 13:f67a6c6013ca 9 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 13:f67a6c6013ca 10 * (at your option) any later version.
wolfSSL 13:f67a6c6013ca 11 *
wolfSSL 13:f67a6c6013ca 12 * wolfSSL is distributed in the hope that it will be useful,
wolfSSL 13:f67a6c6013ca 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 13:f67a6c6013ca 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 13:f67a6c6013ca 15 * GNU General Public License for more details.
wolfSSL 13:f67a6c6013ca 16 *
wolfSSL 13:f67a6c6013ca 17 * You should have received a copy of the GNU General Public License
wolfSSL 13:f67a6c6013ca 18 * along with this program; if not, write to the Free Software
wolfSSL 13:f67a6c6013ca 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
wolfSSL 13:f67a6c6013ca 20 */
wolfSSL 13:f67a6c6013ca 21
wolfSSL 13:f67a6c6013ca 22
wolfSSL 13:f67a6c6013ca 23 #ifndef WOLF_CRYPT_RSA_H
wolfSSL 13:f67a6c6013ca 24 #define WOLF_CRYPT_RSA_H
wolfSSL 13:f67a6c6013ca 25
wolfSSL 13:f67a6c6013ca 26 #include <wolfssl/wolfcrypt/types.h>
wolfSSL 13:f67a6c6013ca 27
wolfSSL 13:f67a6c6013ca 28 #ifndef NO_RSA
wolfSSL 13:f67a6c6013ca 29
wolfSSL 13:f67a6c6013ca 30 /* allow for user to plug in own crypto */
wolfSSL 13:f67a6c6013ca 31 #if !defined(HAVE_FIPS) && (defined(HAVE_USER_RSA) || defined(HAVE_FAST_RSA))
wolfSSL 13:f67a6c6013ca 32 #include "user_rsa.h"
wolfSSL 13:f67a6c6013ca 33 #else
wolfSSL 13:f67a6c6013ca 34
wolfSSL 13:f67a6c6013ca 35 #ifdef HAVE_FIPS
wolfSSL 13:f67a6c6013ca 36 /* for fips @wc_fips */
wolfSSL 13:f67a6c6013ca 37 #include <cyassl/ctaocrypt/rsa.h>
wolfSSL 13:f67a6c6013ca 38 #if defined(CYASSL_KEY_GEN) && !defined(WOLFSSL_KEY_GEN)
wolfSSL 13:f67a6c6013ca 39 #define WOLFSSL_KEY_GEN
wolfSSL 13:f67a6c6013ca 40 #endif
wolfSSL 13:f67a6c6013ca 41 #else
wolfSSL 13:f67a6c6013ca 42 #include <wolfssl/wolfcrypt/integer.h>
wolfSSL 13:f67a6c6013ca 43 #include <wolfssl/wolfcrypt/random.h>
wolfSSL 13:f67a6c6013ca 44 #endif /* HAVE_FIPS */
wolfSSL 13:f67a6c6013ca 45
wolfSSL 13:f67a6c6013ca 46 /* header file needed for OAEP padding */
wolfSSL 13:f67a6c6013ca 47 #include <wolfssl/wolfcrypt/hash.h>
wolfSSL 13:f67a6c6013ca 48
wolfSSL 13:f67a6c6013ca 49 #ifdef WOLFSSL_XILINX_CRYPT
wolfSSL 13:f67a6c6013ca 50 #include "xsecure_rsa.h"
wolfSSL 13:f67a6c6013ca 51 #endif
wolfSSL 13:f67a6c6013ca 52
wolfSSL 13:f67a6c6013ca 53 #ifdef __cplusplus
wolfSSL 13:f67a6c6013ca 54 extern "C" {
wolfSSL 13:f67a6c6013ca 55 #endif
wolfSSL 13:f67a6c6013ca 56
wolfSSL 13:f67a6c6013ca 57 /* avoid redefinition of structs */
wolfSSL 13:f67a6c6013ca 58 #if !defined(HAVE_FIPS)
wolfSSL 13:f67a6c6013ca 59
wolfSSL 13:f67a6c6013ca 60 #ifdef WOLFSSL_ASYNC_CRYPT
wolfSSL 13:f67a6c6013ca 61 #include <wolfssl/wolfcrypt/async.h>
wolfSSL 13:f67a6c6013ca 62 #ifdef WOLFSSL_CERT_GEN
wolfSSL 13:f67a6c6013ca 63 #include <wolfssl/wolfcrypt/asn.h>
wolfSSL 13:f67a6c6013ca 64 #endif
wolfSSL 13:f67a6c6013ca 65 #endif
wolfSSL 13:f67a6c6013ca 66
wolfSSL 13:f67a6c6013ca 67 enum {
wolfSSL 13:f67a6c6013ca 68 RSA_PUBLIC = 0,
wolfSSL 13:f67a6c6013ca 69 RSA_PRIVATE = 1,
wolfSSL 13:f67a6c6013ca 70
wolfSSL 13:f67a6c6013ca 71 RSA_TYPE_UNKNOWN = -1,
wolfSSL 13:f67a6c6013ca 72 RSA_PUBLIC_ENCRYPT = 0,
wolfSSL 13:f67a6c6013ca 73 RSA_PUBLIC_DECRYPT = 1,
wolfSSL 13:f67a6c6013ca 74 RSA_PRIVATE_ENCRYPT = 2,
wolfSSL 13:f67a6c6013ca 75 RSA_PRIVATE_DECRYPT = 3,
wolfSSL 13:f67a6c6013ca 76
wolfSSL 13:f67a6c6013ca 77 RSA_BLOCK_TYPE_1 = 1,
wolfSSL 13:f67a6c6013ca 78 RSA_BLOCK_TYPE_2 = 2,
wolfSSL 13:f67a6c6013ca 79
wolfSSL 13:f67a6c6013ca 80 RSA_MIN_SIZE = 512,
wolfSSL 13:f67a6c6013ca 81 RSA_MAX_SIZE = 4096,
wolfSSL 13:f67a6c6013ca 82
wolfSSL 13:f67a6c6013ca 83 RSA_MIN_PAD_SZ = 11, /* separator + 0 + pad value + 8 pads */
wolfSSL 13:f67a6c6013ca 84
wolfSSL 13:f67a6c6013ca 85 RSA_PSS_PAD_SZ = 8
wolfSSL 13:f67a6c6013ca 86 };
wolfSSL 13:f67a6c6013ca 87
wolfSSL 13:f67a6c6013ca 88
wolfSSL 13:f67a6c6013ca 89 /* RSA */
wolfSSL 13:f67a6c6013ca 90 struct RsaKey {
wolfSSL 13:f67a6c6013ca 91 mp_int n, e, d, p, q, dP, dQ, u;
wolfSSL 13:f67a6c6013ca 92 void* heap; /* for user memory overrides */
wolfSSL 13:f67a6c6013ca 93 byte* data; /* temp buffer for async RSA */
wolfSSL 13:f67a6c6013ca 94 int type; /* public or private */
wolfSSL 13:f67a6c6013ca 95 int state;
wolfSSL 13:f67a6c6013ca 96 word32 dataLen;
wolfSSL 13:f67a6c6013ca 97 #ifdef WC_RSA_BLINDING
wolfSSL 13:f67a6c6013ca 98 WC_RNG* rng; /* for PrivateDecrypt blinding */
wolfSSL 13:f67a6c6013ca 99 #endif
wolfSSL 13:f67a6c6013ca 100 #ifdef WOLFSSL_ASYNC_CRYPT
wolfSSL 13:f67a6c6013ca 101 WC_ASYNC_DEV asyncDev;
wolfSSL 13:f67a6c6013ca 102 #ifdef WOLFSSL_CERT_GEN
wolfSSL 13:f67a6c6013ca 103 CertSignCtx certSignCtx; /* context info for cert sign (MakeSignature) */
wolfSSL 13:f67a6c6013ca 104 #endif
wolfSSL 13:f67a6c6013ca 105 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 13:f67a6c6013ca 106 #ifdef WOLFSSL_XILINX_CRYPT
wolfSSL 13:f67a6c6013ca 107 word32 pubExp; /* to keep values in scope they are here in struct */
wolfSSL 13:f67a6c6013ca 108 byte* mod;
wolfSSL 13:f67a6c6013ca 109 XSecure_Rsa xRsa;
wolfSSL 13:f67a6c6013ca 110 #endif
wolfSSL 13:f67a6c6013ca 111 byte dataIsAlloc;
wolfSSL 13:f67a6c6013ca 112 };
wolfSSL 13:f67a6c6013ca 113
wolfSSL 13:f67a6c6013ca 114 #ifndef WC_RSAKEY_TYPE_DEFINED
wolfSSL 13:f67a6c6013ca 115 typedef struct RsaKey RsaKey;
wolfSSL 13:f67a6c6013ca 116 #define WC_RSAKEY_TYPE_DEFINED
wolfSSL 13:f67a6c6013ca 117 #endif
wolfSSL 13:f67a6c6013ca 118
wolfSSL 13:f67a6c6013ca 119 #endif /*HAVE_FIPS */
wolfSSL 13:f67a6c6013ca 120
wolfSSL 13:f67a6c6013ca 121 WOLFSSL_API int wc_InitRsaKey(RsaKey* key, void* heap);
wolfSSL 13:f67a6c6013ca 122 WOLFSSL_API int wc_InitRsaKey_ex(RsaKey* key, void* heap, int devId);
wolfSSL 13:f67a6c6013ca 123 WOLFSSL_API int wc_FreeRsaKey(RsaKey* key);
wolfSSL 13:f67a6c6013ca 124 #ifdef WOLFSSL_XILINX_CRYPT
wolfSSL 13:f67a6c6013ca 125 WOLFSSL_LOCAL int wc_InitRsaHw(RsaKey* key);
wolfSSL 13:f67a6c6013ca 126 #endif /* WOLFSSL_XILINX_CRYPT */
wolfSSL 13:f67a6c6013ca 127
wolfSSL 13:f67a6c6013ca 128 WOLFSSL_LOCAL int wc_RsaFunction(const byte* in, word32 inLen, byte* out,
wolfSSL 13:f67a6c6013ca 129 word32* outLen, int type, RsaKey* key, WC_RNG* rng);
wolfSSL 13:f67a6c6013ca 130
wolfSSL 13:f67a6c6013ca 131 WOLFSSL_API int wc_RsaPublicEncrypt(const byte* in, word32 inLen, byte* out,
wolfSSL 13:f67a6c6013ca 132 word32 outLen, RsaKey* key, WC_RNG* rng);
wolfSSL 13:f67a6c6013ca 133 WOLFSSL_API int wc_RsaPrivateDecryptInline(byte* in, word32 inLen, byte** out,
wolfSSL 13:f67a6c6013ca 134 RsaKey* key);
wolfSSL 13:f67a6c6013ca 135 WOLFSSL_API int wc_RsaPrivateDecrypt(const byte* in, word32 inLen, byte* out,
wolfSSL 13:f67a6c6013ca 136 word32 outLen, RsaKey* key);
wolfSSL 13:f67a6c6013ca 137 WOLFSSL_API int wc_RsaSSL_Sign(const byte* in, word32 inLen, byte* out,
wolfSSL 13:f67a6c6013ca 138 word32 outLen, RsaKey* key, WC_RNG* rng);
wolfSSL 13:f67a6c6013ca 139 WOLFSSL_API int wc_RsaPSS_Sign(const byte* in, word32 inLen, byte* out,
wolfSSL 13:f67a6c6013ca 140 word32 outLen, enum wc_HashType hash, int mgf,
wolfSSL 13:f67a6c6013ca 141 RsaKey* key, WC_RNG* rng);
wolfSSL 13:f67a6c6013ca 142 WOLFSSL_API int wc_RsaSSL_VerifyInline(byte* in, word32 inLen, byte** out,
wolfSSL 13:f67a6c6013ca 143 RsaKey* key);
wolfSSL 13:f67a6c6013ca 144 WOLFSSL_API int wc_RsaSSL_Verify(const byte* in, word32 inLen, byte* out,
wolfSSL 13:f67a6c6013ca 145 word32 outLen, RsaKey* key);
wolfSSL 13:f67a6c6013ca 146 WOLFSSL_API int wc_RsaPSS_VerifyInline(byte* in, word32 inLen, byte** out,
wolfSSL 13:f67a6c6013ca 147 enum wc_HashType hash, int mgf,
wolfSSL 13:f67a6c6013ca 148 RsaKey* key);
wolfSSL 13:f67a6c6013ca 149 WOLFSSL_API int wc_RsaPSS_CheckPadding(const byte* in, word32 inLen, byte* sig,
wolfSSL 13:f67a6c6013ca 150 word32 sigSz,
wolfSSL 13:f67a6c6013ca 151 enum wc_HashType hashType);
wolfSSL 13:f67a6c6013ca 152
wolfSSL 13:f67a6c6013ca 153 WOLFSSL_API int wc_RsaEncryptSize(RsaKey* key);
wolfSSL 13:f67a6c6013ca 154
wolfSSL 13:f67a6c6013ca 155 #ifndef HAVE_FIPS /* to avoid asn duplicate symbols @wc_fips */
wolfSSL 13:f67a6c6013ca 156 WOLFSSL_API int wc_RsaPrivateKeyDecode(const byte* input, word32* inOutIdx,
wolfSSL 13:f67a6c6013ca 157 RsaKey*, word32);
wolfSSL 13:f67a6c6013ca 158 WOLFSSL_API int wc_RsaPublicKeyDecode(const byte* input, word32* inOutIdx,
wolfSSL 13:f67a6c6013ca 159 RsaKey*, word32);
wolfSSL 13:f67a6c6013ca 160 WOLFSSL_API int wc_RsaPublicKeyDecodeRaw(const byte* n, word32 nSz,
wolfSSL 13:f67a6c6013ca 161 const byte* e, word32 eSz, RsaKey* key);
wolfSSL 13:f67a6c6013ca 162 #ifdef WOLFSSL_KEY_GEN
wolfSSL 13:f67a6c6013ca 163 WOLFSSL_API int wc_RsaKeyToDer(RsaKey*, byte* output, word32 inLen);
wolfSSL 13:f67a6c6013ca 164 #endif
wolfSSL 13:f67a6c6013ca 165
wolfSSL 13:f67a6c6013ca 166 WOLFSSL_API int wc_RsaSetRNG(RsaKey* key, WC_RNG* rng);
wolfSSL 13:f67a6c6013ca 167
wolfSSL 13:f67a6c6013ca 168 /*
wolfSSL 13:f67a6c6013ca 169 choice of padding added after fips, so not available when using fips RSA
wolfSSL 13:f67a6c6013ca 170 */
wolfSSL 13:f67a6c6013ca 171
wolfSSL 13:f67a6c6013ca 172 /* Mask Generation Function Identifiers */
wolfSSL 13:f67a6c6013ca 173 #define WC_MGF1NONE 0
wolfSSL 13:f67a6c6013ca 174 #define WC_MGF1SHA1 26
wolfSSL 13:f67a6c6013ca 175 #define WC_MGF1SHA224 4
wolfSSL 13:f67a6c6013ca 176 #define WC_MGF1SHA256 1
wolfSSL 13:f67a6c6013ca 177 #define WC_MGF1SHA384 2
wolfSSL 13:f67a6c6013ca 178 #define WC_MGF1SHA512 3
wolfSSL 13:f67a6c6013ca 179
wolfSSL 13:f67a6c6013ca 180 /* Padding types */
wolfSSL 13:f67a6c6013ca 181 #define WC_RSA_PKCSV15_PAD 0
wolfSSL 13:f67a6c6013ca 182 #define WC_RSA_OAEP_PAD 1
wolfSSL 13:f67a6c6013ca 183 #define WC_RSA_PSS_PAD 2
wolfSSL 13:f67a6c6013ca 184
wolfSSL 13:f67a6c6013ca 185 WOLFSSL_API int wc_RsaPublicEncrypt_ex(const byte* in, word32 inLen, byte* out,
wolfSSL 13:f67a6c6013ca 186 word32 outLen, RsaKey* key, WC_RNG* rng, int type,
wolfSSL 13:f67a6c6013ca 187 enum wc_HashType hash, int mgf, byte* label, word32 lableSz);
wolfSSL 13:f67a6c6013ca 188 WOLFSSL_API int wc_RsaPrivateDecrypt_ex(const byte* in, word32 inLen,
wolfSSL 13:f67a6c6013ca 189 byte* out, word32 outLen, RsaKey* key, int type,
wolfSSL 13:f67a6c6013ca 190 enum wc_HashType hash, int mgf, byte* label, word32 lableSz);
wolfSSL 13:f67a6c6013ca 191 WOLFSSL_API int wc_RsaPrivateDecryptInline_ex(byte* in, word32 inLen,
wolfSSL 13:f67a6c6013ca 192 byte** out, RsaKey* key, int type, enum wc_HashType hash,
wolfSSL 13:f67a6c6013ca 193 int mgf, byte* label, word32 lableSz);
wolfSSL 13:f67a6c6013ca 194 #endif /* HAVE_FIPS*/
wolfSSL 13:f67a6c6013ca 195 WOLFSSL_API int wc_RsaFlattenPublicKey(RsaKey*, byte*, word32*, byte*,
wolfSSL 13:f67a6c6013ca 196 word32*);
wolfSSL 13:f67a6c6013ca 197
wolfSSL 13:f67a6c6013ca 198 #ifdef WOLFSSL_KEY_GEN
wolfSSL 13:f67a6c6013ca 199 WOLFSSL_API int wc_RsaKeyToPublicDer(RsaKey*, byte* output, word32 inLen);
wolfSSL 13:f67a6c6013ca 200 WOLFSSL_API int wc_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng);
wolfSSL 13:f67a6c6013ca 201 #endif
wolfSSL 13:f67a6c6013ca 202
wolfSSL 13:f67a6c6013ca 203 #endif /* HAVE_USER_RSA */
wolfSSL 13:f67a6c6013ca 204
wolfSSL 13:f67a6c6013ca 205 #ifdef __cplusplus
wolfSSL 13:f67a6c6013ca 206 } /* extern "C" */
wolfSSL 13:f67a6c6013ca 207 #endif
wolfSSL 13:f67a6c6013ca 208
wolfSSL 13:f67a6c6013ca 209 #endif /* NO_RSA */
wolfSSL 13:f67a6c6013ca 210 #endif /* WOLF_CRYPT_RSA_H */
wolfSSL 13:f67a6c6013ca 211
wolfSSL 13:f67a6c6013ca 212