user_rsa.h
1 /* user_rsa.h
2  *
3  * Copyright (C) 2006-2020 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 
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 */
WOLFSSL_API int wc_RsaEncryptSize(RsaKey *key)
Returns the encryption size for the provided key structure.
Definition: rsa.c:168
WOLFSSL_API int wc_RsaPublicKeyDecodeRaw(const byte *n, word32 nSz, const byte *e, word32 eSz, RsaKey *key)
This function decodes the raw elements of an RSA public key, taking in the public modulus (n) and exp...
Definition: asn.c:4340
WOLFSSL_API int wc_FreeRsaKey(RsaKey *key)
This function frees a provided RsaKey struct using mp_clear.
Definition: rsa.c:99
Definition: rsa.h:66
WOLFSSL_API int wc_RsaPublicEncrypt(const byte *in, word32 inLen, byte *out, word32 outLen, RsaKey *key, WC_RNG *rng)
This function encrypts a message from in and stores the result in out. It requires an initialized pub...
Definition: rsa.c:106
WOLFSSL_API int wc_RsaSSL_VerifyInline(byte *in, word32 inLen, byte **out, RsaKey *key)
Used to verify that the message was signed by RSA key. The output uses the same byte array as the inp...
Definition: rsa.c:149
WOLFSSL_API int wc_RsaPrivateKeyDecode(const byte *input, word32 *inOutIdx, RsaKey *, word32)
This function parses a DER-formatted RSA private key, extracts the private key and stores it in the g...
Definition: asn.c:2567
WOLFSSL_API int wc_RsaPublicKeyDecode(const byte *input, word32 *inOutIdx, RsaKey *, word32)
This function parses a DER-formatted RSA public key, extracts the public key and stores it in the giv...
Definition: asn.c:4321
WOLFSSL_API int wc_RsaKeyToDer(RsaKey *, byte *output, word32 inLen)
This function converts an RsaKey key to DER format. The result is written to output and it returns th...
Definition: asn.c:11179
WOLFSSL_API int wc_RsaFlattenPublicKey(RsaKey *, byte *, word32 *, byte *, word32 *)
Flattens the RsaKey structure into individual elements (e, n) used for the RSA algorithm.
Definition: rsa.c:178
WOLFSSL_API int wc_RsaPrivateDecrypt(const byte *in, word32 inLen, byte *out, word32 outLen, RsaKey *key)
This functions provides private RSA decryption.
Definition: rsa.c:128
WOLFSSL_API int wc_RsaSSL_Sign(const byte *in, word32 inLen, byte *out, word32 outLen, RsaKey *key, WC_RNG *rng)
Signs the provided array with the private key.
Definition: rsa.c:138
WOLFSSL_API int wc_RsaKeyToPublicDer(RsaKey *, byte *output, word32 inLen)
Convert Rsa Public key to DER format. Writes to output, and returns count of bytes written...
Definition: asn.c:11254
WOLFSSL_API int wc_RsaSSL_Verify(const byte *in, word32 inLen, byte *out, word32 outLen, RsaKey *key)
Used to verify that the message was signed by key.
Definition: rsa.c:158
WOLFSSL_API int wc_InitRsaKey(RsaKey *key, void *)
This function initializes a provided RsaKey struct. It also takes in a heap identifier, for use with user defined memory overrides (see XMALLOC, XFREE, XREALLOC).
Definition: rsa.c:79
Definition: random.h:153
WOLFSSL_API int wc_MakeRsaKey(RsaKey *key, int size, long e, WC_RNG *rng)
This function generates a RSA private key of length size (in bits) and given exponent (e)...
Definition: rsa.c:189
Definition: user_rsa.h:60
WOLFSSL_API int wc_RsaPrivateDecryptInline(byte *in, word32 inLen, byte **out, RsaKey *key)
This functions is utilized by the wc_RsaPrivateDecrypt function for decrypting.
Definition: rsa.c:118