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