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