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

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

Committer:
wolfSSL
Date:
Tue May 02 08:44:47 2017 +0000
Revision:
7:481bce714567
wolfSSL3.10.2

Who changed what in which revision?

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