[164] | 1 | /* rsa.h
|
---|
| 2 | *
|
---|
| 3 | * Copyright (C) 2006-2015 wolfSSL Inc.
|
---|
| 4 | *
|
---|
| 5 | * This file is part of wolfSSL. (formerly known as CyaSSL)
|
---|
| 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-1301, USA
|
---|
| 20 | */
|
---|
| 21 |
|
---|
| 22 | #ifndef WOLF_CRYPT_RSA_H
|
---|
| 23 | #define WOLF_CRYPT_RSA_H
|
---|
| 24 |
|
---|
| 25 | #include <wolfssl/wolfcrypt/types.h>
|
---|
| 26 |
|
---|
| 27 | #ifndef NO_RSA
|
---|
| 28 |
|
---|
| 29 | /* allow for user to plug in own crypto */
|
---|
| 30 | #if !defined(HAVE_FIPS) && (defined(HAVE_USER_RSA) || defined(HAVE_FAST_RSA))
|
---|
| 31 | #include "user_rsa.h"
|
---|
| 32 | #else
|
---|
| 33 |
|
---|
| 34 | #ifdef HAVE_FIPS
|
---|
| 35 | /* for fips @wc_fips */
|
---|
| 36 | #include <cyassl/ctaocrypt/rsa.h>
|
---|
| 37 | #if defined(CYASSL_KEY_GEN) && !defined(WOLFSSL_KEY_GEN)
|
---|
| 38 | #define WOLFSSL_KEY_GEN
|
---|
| 39 | #endif
|
---|
| 40 | #else
|
---|
| 41 | #include <wolfssl/wolfcrypt/integer.h>
|
---|
| 42 | #include <wolfssl/wolfcrypt/random.h>
|
---|
| 43 | #endif /* HAVE_FIPS */
|
---|
| 44 |
|
---|
| 45 | #ifdef __cplusplus
|
---|
| 46 | extern "C" {
|
---|
| 47 | #endif
|
---|
| 48 |
|
---|
| 49 | /* avoid redefinition of structs */
|
---|
| 50 | #if !defined(HAVE_FIPS)
|
---|
| 51 | #define WOLFSSL_RSA_CAVIUM_MAGIC 0xBEEF0006
|
---|
| 52 |
|
---|
| 53 | enum {
|
---|
| 54 | RSA_PUBLIC = 0,
|
---|
| 55 | RSA_PRIVATE = 1,
|
---|
| 56 | };
|
---|
| 57 |
|
---|
| 58 |
|
---|
| 59 | /* RSA */
|
---|
| 60 | typedef struct RsaKey {
|
---|
| 61 | mp_int n, e, d, p, q, dP, dQ, u;
|
---|
| 62 | int type; /* public or private */
|
---|
| 63 | void* heap; /* for user memory overrides */
|
---|
| 64 | #ifdef HAVE_CAVIUM
|
---|
| 65 | int devId; /* nitrox device id */
|
---|
| 66 | word32 magic; /* using cavium magic */
|
---|
| 67 | word64 contextHandle; /* nitrox context memory handle */
|
---|
| 68 | byte* c_n; /* cavium byte buffers for key parts */
|
---|
| 69 | byte* c_e;
|
---|
| 70 | byte* c_d;
|
---|
| 71 | byte* c_p;
|
---|
| 72 | byte* c_q;
|
---|
| 73 | byte* c_dP;
|
---|
| 74 | byte* c_dQ;
|
---|
| 75 | byte* c_u; /* sizes in bytes */
|
---|
| 76 | word16 c_nSz, c_eSz, c_dSz, c_pSz, c_qSz, c_dP_Sz, c_dQ_Sz, c_uSz;
|
---|
| 77 | #endif
|
---|
| 78 | } RsaKey;
|
---|
| 79 | #endif /*HAVE_FIPS */
|
---|
| 80 |
|
---|
| 81 | WOLFSSL_API int wc_InitRsaKey(RsaKey* key, void*);
|
---|
| 82 | WOLFSSL_API int wc_FreeRsaKey(RsaKey* key);
|
---|
| 83 |
|
---|
| 84 | WOLFSSL_API int wc_RsaPublicEncrypt(const byte* in, word32 inLen, byte* out,
|
---|
| 85 | word32 outLen, RsaKey* key, WC_RNG* rng);
|
---|
| 86 | WOLFSSL_API int wc_RsaPrivateDecryptInline(byte* in, word32 inLen, byte** out,
|
---|
| 87 | RsaKey* key);
|
---|
| 88 | WOLFSSL_API int wc_RsaPrivateDecrypt(const byte* in, word32 inLen, byte* out,
|
---|
| 89 | word32 outLen, RsaKey* key);
|
---|
| 90 | WOLFSSL_API int wc_RsaSSL_Sign(const byte* in, word32 inLen, byte* out,
|
---|
| 91 | word32 outLen, RsaKey* key, WC_RNG* rng);
|
---|
| 92 | WOLFSSL_API int wc_RsaSSL_VerifyInline(byte* in, word32 inLen, byte** out,
|
---|
| 93 | RsaKey* key);
|
---|
| 94 | WOLFSSL_API int wc_RsaSSL_Verify(const byte* in, word32 inLen, byte* out,
|
---|
| 95 | word32 outLen, RsaKey* key);
|
---|
| 96 | WOLFSSL_API int wc_RsaEncryptSize(RsaKey* key);
|
---|
| 97 |
|
---|
| 98 | #ifndef HAVE_FIPS /* to avoid asn duplicate symbols @wc_fips */
|
---|
| 99 | WOLFSSL_API int wc_RsaPrivateKeyDecode(const byte* input, word32* inOutIdx,
|
---|
| 100 | RsaKey*, word32);
|
---|
| 101 | WOLFSSL_API int wc_RsaPublicKeyDecode(const byte* input, word32* inOutIdx,
|
---|
| 102 | RsaKey*, word32);
|
---|
| 103 | WOLFSSL_API int wc_RsaPublicKeyDecodeRaw(const byte* n, word32 nSz,
|
---|
| 104 | const byte* e, word32 eSz, RsaKey* key);
|
---|
| 105 | #ifdef WOLFSSL_KEY_GEN
|
---|
| 106 | WOLFSSL_API int wc_RsaKeyToDer(RsaKey*, byte* output, word32 inLen);
|
---|
| 107 | #endif
|
---|
| 108 | #endif /* HAVE_FIPS*/
|
---|
| 109 | WOLFSSL_API int wc_RsaFlattenPublicKey(RsaKey*, byte*, word32*, byte*,
|
---|
| 110 | word32*);
|
---|
| 111 |
|
---|
| 112 | #ifdef WOLFSSL_KEY_GEN
|
---|
| 113 | WOLFSSL_API int wc_RsaKeyToPublicDer(RsaKey*, byte* output, word32 inLen);
|
---|
| 114 | WOLFSSL_API int wc_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng);
|
---|
| 115 | #endif
|
---|
| 116 |
|
---|
| 117 | #ifdef HAVE_CAVIUM
|
---|
| 118 | WOLFSSL_API int wc_RsaInitCavium(RsaKey*, int);
|
---|
| 119 | WOLFSSL_API void wc_RsaFreeCavium(RsaKey*);
|
---|
| 120 | #endif
|
---|
| 121 | #endif /* HAVE_USER_RSA */
|
---|
| 122 | #ifdef __cplusplus
|
---|
| 123 | } /* extern "C" */
|
---|
| 124 | #endif
|
---|
| 125 |
|
---|
| 126 | #endif /* NO_RSA */
|
---|
| 127 | #endif /* WOLF_CRYPT_RSA_H */
|
---|
| 128 |
|
---|