1 /* user_rsa.h
2  *
3  * Copyright (C) 2006-2021 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 /*
24  Created to use intel's IPP see their license for linking to intel's IPP library
25  */
26 
27 #ifndef USER_WOLF_CRYPT_RSA_H
28 #define USER_WOLF_CRYPT_RSA_H
29 
30 #include <wolfssl/wolfcrypt/settings.h>
31 
32 #ifndef NO_RSA
33 
34 #include <wolfssl/wolfcrypt/types.h>
35 #include <wolfssl/wolfcrypt/random.h>
36 
37 /* intels crypto */
38 #include <ipp.h>
39 #include <ippcp.h>
40 
41 #ifdef __cplusplus
42     extern "C" {
43 #endif
44 
45 /* needed for WOLFSSL_RSA type but use macro guard against redefine */
46 #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TYPES_DEFINED) \
47         && !defined(WOLFSSL_RSA_TYPE_DEFINED)
48     struct WOLFSSL_RSA;
49     typedef struct WOLFSSL_RSA WOLFSSL_RSA;
50     #define WOLFSSL_RSA_TYPE_DEFINED
51 #endif
52 
53 
54 enum {
55     RSA_PUBLIC   = 0,
56     RSA_PRIVATE  = 1,
57 };
58 
59 /* RSA */
60 struct RsaKey {
61     IppsBigNumState* n;
62     IppsBigNumState* e;
63     IppsBigNumState* dipp;
64     IppsBigNumState* pipp;
65     IppsBigNumState* qipp;
66     IppsBigNumState* dPipp;
67     IppsBigNumState* dQipp;
68     IppsBigNumState* uipp;
69     int nSz, eSz, dSz;
70     IppsRSAPublicKeyState*  pPub;
71     IppsRSAPrivateKeyState* pPrv;
72     word32 prvSz; /* size of private key */
73     word32 sz;    /* size of signature */
74     int   type;                               /* public or private */
75     void* heap;                               /* for user memory overrides */
76 };
77 
78 #ifndef WC_RSAKEY_TYPE_DEFINED
79     typedef struct RsaKey RsaKey;
80     #define WC_RSAKEY_TYPE_DEFINED
81 #endif
82 
83 WOLFSSL_API int  wc_InitRsaKey(RsaKey* key, void*);
84 WOLFSSL_API int  wc_InitRsaKey_ex(RsaKey* key, void* heap, int devId);
85 WOLFSSL_API int  wc_FreeRsaKey(RsaKey* key);
86 
87 WOLFSSL_API int  wc_RsaPublicEncrypt(const byte* in, word32 inLen, byte* out,
88                                      word32 outLen, RsaKey* key, WC_RNG* rng);
89 WOLFSSL_API int  wc_RsaPrivateDecryptInline(byte* in, word32 inLen, byte** out,
90                                             RsaKey* key);
91 WOLFSSL_API int  wc_RsaPrivateDecrypt(const byte* in, word32 inLen, byte* out,
92                                       word32 outLen, RsaKey* key);
93 WOLFSSL_API int  wc_RsaSSL_Sign(const byte* in, word32 inLen, byte* out,
94                                 word32 outLen, RsaKey* key, WC_RNG* rng);
95 WOLFSSL_API int  wc_RsaSSL_VerifyInline(byte* in, word32 inLen, byte** out,
96                                         RsaKey* key);
97 WOLFSSL_API int  wc_RsaSSL_Verify(const byte* in, word32 inLen, byte* out,
98                                   word32 outLen, RsaKey* key);
99 WOLFSSL_API int  wc_RsaEncryptSize(RsaKey* key);
100 
101 WOLFSSL_API int  wc_RsaPrivateKeyDecode(const byte* input, word32* inOutIdx,
102                                                                RsaKey*, word32);
103 WOLFSSL_API int  wc_RsaPublicKeyDecode_ex(const byte* input, word32* inOutIdx,
104         word32 inSz, const byte** n, word32* nSz, const byte** e, word32* eSz);
105 WOLFSSL_API int  wc_RsaPublicKeyDecode(const byte* input, word32* inOutIdx,
106                                                                RsaKey*, word32);
107 WOLFSSL_API int  wc_RsaPublicKeyDecodeRaw(const byte* n, word32 nSz,
108                                         const byte* e, word32 eSz, RsaKey* key);
109 WOLFSSL_API int wc_RsaKeyToDer(RsaKey*, byte* output, word32 inLen);
110 WOLFSSL_API int wc_RsaKeyToPublicDer(RsaKey*, byte* output, word32 inLen);
111 #ifdef WOLFSSL_KEY_GEN
112     WOLFSSL_API int wc_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng);
113 #endif
114 WOLFSSL_API int  wc_RsaFlattenPublicKey(RsaKey*, byte*, word32*, byte*,
115                                                                        word32*);
116 WOLFSSL_API int  wc_RsaSetRNG(RsaKey* key, WC_RNG* rng);
117 
118 
119 #if defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA)
120         /* abstracted BN operations with RSA key */
121     WOLFSSL_API int wc_Rsa_leading_bit(void* BN);
122     WOLFSSL_API int wc_Rsa_unsigned_bin_size(void* BN);
123 
124         /* return MP_OKAY on success */
125     WOLFSSL_API int wc_Rsa_to_unsigned_bin(void* BN, byte* in, int inLen);
126 #endif
127 
128 #ifdef OPENSSL_EXTRA /* abstracted functions to deal with rsa key */
129     WOLFSSL_API int SetRsaExternal(WOLFSSL_RSA* rsa);
130     WOLFSSL_API int SetRsaInternal(WOLFSSL_RSA* rsa);
131 #endif
132 #ifdef __cplusplus
133     } /* extern "C" */
134 #endif
135 
136 #endif /* NO_RSA */
137 #endif /* USER_WOLF_CRYPT_RSA_H */
138