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

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

Committer:
wolfSSL
Date:
Thu Apr 28 00:57:21 2016 +0000
Revision:
4:1b0d80432c79
wolfSSL 3.9.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 4:1b0d80432c79 1 /* rsa.h
wolfSSL 4:1b0d80432c79 2 *
wolfSSL 4:1b0d80432c79 3 * Copyright (C) 2006-2016 wolfSSL Inc.
wolfSSL 4:1b0d80432c79 4 *
wolfSSL 4:1b0d80432c79 5 * This file is part of wolfSSL.
wolfSSL 4:1b0d80432c79 6 *
wolfSSL 4:1b0d80432c79 7 * wolfSSL is free software; you can redistribute it and/or modify
wolfSSL 4:1b0d80432c79 8 * it under the terms of the GNU General Public License as published by
wolfSSL 4:1b0d80432c79 9 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 4:1b0d80432c79 10 * (at your option) any later version.
wolfSSL 4:1b0d80432c79 11 *
wolfSSL 4:1b0d80432c79 12 * wolfSSL is distributed in the hope that it will be useful,
wolfSSL 4:1b0d80432c79 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 4:1b0d80432c79 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 4:1b0d80432c79 15 * GNU General Public License for more details.
wolfSSL 4:1b0d80432c79 16 *
wolfSSL 4:1b0d80432c79 17 * You should have received a copy of the GNU General Public License
wolfSSL 4:1b0d80432c79 18 * along with this program; if not, write to the Free Software
wolfSSL 4:1b0d80432c79 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
wolfSSL 4:1b0d80432c79 20 */
wolfSSL 4:1b0d80432c79 21
wolfSSL 4:1b0d80432c79 22
wolfSSL 4:1b0d80432c79 23 #ifndef WOLF_CRYPT_RSA_H
wolfSSL 4:1b0d80432c79 24 #define WOLF_CRYPT_RSA_H
wolfSSL 4:1b0d80432c79 25
wolfSSL 4:1b0d80432c79 26 #include <wolfssl/wolfcrypt/types.h>
wolfSSL 4:1b0d80432c79 27
wolfSSL 4:1b0d80432c79 28 #ifndef NO_RSA
wolfSSL 4:1b0d80432c79 29
wolfSSL 4:1b0d80432c79 30 /* allow for user to plug in own crypto */
wolfSSL 4:1b0d80432c79 31 #if !defined(HAVE_FIPS) && (defined(HAVE_USER_RSA) || defined(HAVE_FAST_RSA))
wolfSSL 4:1b0d80432c79 32 #include "user_rsa.h"
wolfSSL 4:1b0d80432c79 33 #else
wolfSSL 4:1b0d80432c79 34
wolfSSL 4:1b0d80432c79 35 #ifdef HAVE_FIPS
wolfSSL 4:1b0d80432c79 36 /* for fips @wc_fips */
wolfSSL 4:1b0d80432c79 37 #include <cyassl/ctaocrypt/rsa.h>
wolfSSL 4:1b0d80432c79 38 #if defined(CYASSL_KEY_GEN) && !defined(WOLFSSL_KEY_GEN)
wolfSSL 4:1b0d80432c79 39 #define WOLFSSL_KEY_GEN
wolfSSL 4:1b0d80432c79 40 #endif
wolfSSL 4:1b0d80432c79 41 #else
wolfSSL 4:1b0d80432c79 42 #include <wolfssl/wolfcrypt/integer.h>
wolfSSL 4:1b0d80432c79 43 #include <wolfssl/wolfcrypt/random.h>
wolfSSL 4:1b0d80432c79 44 #endif /* HAVE_FIPS */
wolfSSL 4:1b0d80432c79 45
wolfSSL 4:1b0d80432c79 46 /* header file needed for OAEP padding */
wolfSSL 4:1b0d80432c79 47 #include <wolfssl/wolfcrypt/hash.h>
wolfSSL 4:1b0d80432c79 48
wolfSSL 4:1b0d80432c79 49 #ifdef __cplusplus
wolfSSL 4:1b0d80432c79 50 extern "C" {
wolfSSL 4:1b0d80432c79 51 #endif
wolfSSL 4:1b0d80432c79 52
wolfSSL 4:1b0d80432c79 53 /* avoid redefinition of structs */
wolfSSL 4:1b0d80432c79 54 #if !defined(HAVE_FIPS)
wolfSSL 4:1b0d80432c79 55 #define WOLFSSL_RSA_CAVIUM_MAGIC 0xBEEF0006
wolfSSL 4:1b0d80432c79 56
wolfSSL 4:1b0d80432c79 57 enum {
wolfSSL 4:1b0d80432c79 58 RSA_PUBLIC = 0,
wolfSSL 4:1b0d80432c79 59 RSA_PRIVATE = 1,
wolfSSL 4:1b0d80432c79 60 };
wolfSSL 4:1b0d80432c79 61
wolfSSL 4:1b0d80432c79 62
wolfSSL 4:1b0d80432c79 63 /* RSA */
wolfSSL 4:1b0d80432c79 64 typedef struct RsaKey {
wolfSSL 4:1b0d80432c79 65 mp_int n, e, d, p, q, dP, dQ, u;
wolfSSL 4:1b0d80432c79 66 int type; /* public or private */
wolfSSL 4:1b0d80432c79 67 void* heap; /* for user memory overrides */
wolfSSL 4:1b0d80432c79 68 #ifdef HAVE_CAVIUM
wolfSSL 4:1b0d80432c79 69 int devId; /* nitrox device id */
wolfSSL 4:1b0d80432c79 70 word32 magic; /* using cavium magic */
wolfSSL 4:1b0d80432c79 71 word64 contextHandle; /* nitrox context memory handle */
wolfSSL 4:1b0d80432c79 72 byte* c_n; /* cavium byte buffers for key parts */
wolfSSL 4:1b0d80432c79 73 byte* c_e;
wolfSSL 4:1b0d80432c79 74 byte* c_d;
wolfSSL 4:1b0d80432c79 75 byte* c_p;
wolfSSL 4:1b0d80432c79 76 byte* c_q;
wolfSSL 4:1b0d80432c79 77 byte* c_dP;
wolfSSL 4:1b0d80432c79 78 byte* c_dQ;
wolfSSL 4:1b0d80432c79 79 byte* c_u; /* sizes in bytes */
wolfSSL 4:1b0d80432c79 80 word16 c_nSz, c_eSz, c_dSz, c_pSz, c_qSz, c_dP_Sz, c_dQ_Sz, c_uSz;
wolfSSL 4:1b0d80432c79 81 #endif
wolfSSL 4:1b0d80432c79 82 } RsaKey;
wolfSSL 4:1b0d80432c79 83 #endif /*HAVE_FIPS */
wolfSSL 4:1b0d80432c79 84
wolfSSL 4:1b0d80432c79 85 WOLFSSL_API int wc_InitRsaKey(RsaKey* key, void*);
wolfSSL 4:1b0d80432c79 86 WOLFSSL_API int wc_FreeRsaKey(RsaKey* key);
wolfSSL 4:1b0d80432c79 87
wolfSSL 4:1b0d80432c79 88 WOLFSSL_API int wc_RsaPublicEncrypt(const byte* in, word32 inLen, byte* out,
wolfSSL 4:1b0d80432c79 89 word32 outLen, RsaKey* key, WC_RNG* rng);
wolfSSL 4:1b0d80432c79 90 WOLFSSL_API int wc_RsaPrivateDecryptInline(byte* in, word32 inLen, byte** out,
wolfSSL 4:1b0d80432c79 91 RsaKey* key);
wolfSSL 4:1b0d80432c79 92 WOLFSSL_API int wc_RsaPrivateDecrypt(const byte* in, word32 inLen, byte* out,
wolfSSL 4:1b0d80432c79 93 word32 outLen, RsaKey* key);
wolfSSL 4:1b0d80432c79 94 WOLFSSL_API int wc_RsaSSL_Sign(const byte* in, word32 inLen, byte* out,
wolfSSL 4:1b0d80432c79 95 word32 outLen, RsaKey* key, WC_RNG* rng);
wolfSSL 4:1b0d80432c79 96 WOLFSSL_API int wc_RsaSSL_VerifyInline(byte* in, word32 inLen, byte** out,
wolfSSL 4:1b0d80432c79 97 RsaKey* key);
wolfSSL 4:1b0d80432c79 98 WOLFSSL_API int wc_RsaSSL_Verify(const byte* in, word32 inLen, byte* out,
wolfSSL 4:1b0d80432c79 99 word32 outLen, RsaKey* key);
wolfSSL 4:1b0d80432c79 100 WOLFSSL_API int wc_RsaEncryptSize(RsaKey* key);
wolfSSL 4:1b0d80432c79 101
wolfSSL 4:1b0d80432c79 102 #ifndef HAVE_FIPS /* to avoid asn duplicate symbols @wc_fips */
wolfSSL 4:1b0d80432c79 103 WOLFSSL_API int wc_RsaPrivateKeyDecode(const byte* input, word32* inOutIdx,
wolfSSL 4:1b0d80432c79 104 RsaKey*, word32);
wolfSSL 4:1b0d80432c79 105 WOLFSSL_API int wc_RsaPublicKeyDecode(const byte* input, word32* inOutIdx,
wolfSSL 4:1b0d80432c79 106 RsaKey*, word32);
wolfSSL 4:1b0d80432c79 107 WOLFSSL_API int wc_RsaPublicKeyDecodeRaw(const byte* n, word32 nSz,
wolfSSL 4:1b0d80432c79 108 const byte* e, word32 eSz, RsaKey* key);
wolfSSL 4:1b0d80432c79 109 #ifdef WOLFSSL_KEY_GEN
wolfSSL 4:1b0d80432c79 110 WOLFSSL_API int wc_RsaKeyToDer(RsaKey*, byte* output, word32 inLen);
wolfSSL 4:1b0d80432c79 111 #endif
wolfSSL 4:1b0d80432c79 112
wolfSSL 4:1b0d80432c79 113 /*
wolfSSL 4:1b0d80432c79 114 choice of padding added after fips, so not available when using fips RSA
wolfSSL 4:1b0d80432c79 115 */
wolfSSL 4:1b0d80432c79 116
wolfSSL 4:1b0d80432c79 117 /* Mask Generation Function Identifiers */
wolfSSL 4:1b0d80432c79 118 #define WC_MGF1SHA1 26
wolfSSL 4:1b0d80432c79 119 #define WC_MGF1SHA256 1
wolfSSL 4:1b0d80432c79 120 #define WC_MGF1SHA384 2
wolfSSL 4:1b0d80432c79 121 #define WC_MGF1SHA512 3
wolfSSL 4:1b0d80432c79 122
wolfSSL 4:1b0d80432c79 123 /* Padding types */
wolfSSL 4:1b0d80432c79 124 #define WC_RSA_PKCSV15_PAD 0
wolfSSL 4:1b0d80432c79 125 #define WC_RSA_OAEP_PAD 1
wolfSSL 4:1b0d80432c79 126
wolfSSL 4:1b0d80432c79 127 WOLFSSL_API int wc_RsaPublicEncrypt_ex(const byte* in, word32 inLen, byte* out,
wolfSSL 4:1b0d80432c79 128 word32 outLen, RsaKey* key, WC_RNG* rng, int type,
wolfSSL 4:1b0d80432c79 129 enum wc_HashType hash, int mgf, byte* label, word32 lableSz);
wolfSSL 4:1b0d80432c79 130 WOLFSSL_API int wc_RsaPrivateDecrypt_ex(const byte* in, word32 inLen,
wolfSSL 4:1b0d80432c79 131 byte* out, word32 outLen, RsaKey* key, int type,
wolfSSL 4:1b0d80432c79 132 enum wc_HashType hash, int mgf, byte* label, word32 lableSz);
wolfSSL 4:1b0d80432c79 133 WOLFSSL_API int wc_RsaPrivateDecryptInline_ex(byte* in, word32 inLen,
wolfSSL 4:1b0d80432c79 134 byte** out, RsaKey* key, int type, enum wc_HashType hash,
wolfSSL 4:1b0d80432c79 135 int mgf, byte* label, word32 lableSz);
wolfSSL 4:1b0d80432c79 136 #endif /* HAVE_FIPS*/
wolfSSL 4:1b0d80432c79 137 WOLFSSL_API int wc_RsaFlattenPublicKey(RsaKey*, byte*, word32*, byte*,
wolfSSL 4:1b0d80432c79 138 word32*);
wolfSSL 4:1b0d80432c79 139
wolfSSL 4:1b0d80432c79 140 #ifdef WOLFSSL_KEY_GEN
wolfSSL 4:1b0d80432c79 141 WOLFSSL_API int wc_RsaKeyToPublicDer(RsaKey*, byte* output, word32 inLen);
wolfSSL 4:1b0d80432c79 142 WOLFSSL_API int wc_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng);
wolfSSL 4:1b0d80432c79 143 #endif
wolfSSL 4:1b0d80432c79 144
wolfSSL 4:1b0d80432c79 145 #ifdef HAVE_CAVIUM
wolfSSL 4:1b0d80432c79 146 WOLFSSL_API int wc_RsaInitCavium(RsaKey*, int);
wolfSSL 4:1b0d80432c79 147 WOLFSSL_API void wc_RsaFreeCavium(RsaKey*);
wolfSSL 4:1b0d80432c79 148 #endif
wolfSSL 4:1b0d80432c79 149 #endif /* HAVE_USER_RSA */
wolfSSL 4:1b0d80432c79 150 #ifdef __cplusplus
wolfSSL 4:1b0d80432c79 151 } /* extern "C" */
wolfSSL 4:1b0d80432c79 152 #endif
wolfSSL 4:1b0d80432c79 153
wolfSSL 4:1b0d80432c79 154 #endif /* NO_RSA */
wolfSSL 4:1b0d80432c79 155 #endif /* WOLF_CRYPT_RSA_H */
wolfSSL 4:1b0d80432c79 156
wolfSSL 4:1b0d80432c79 157