TrustCore SDK NanoCrypto API reference  version 7.0
rsa.h
Go to the documentation of this file.
1 
40 /*------------------------------------------------------------------*/
41 
42 #ifndef __RSA_H__
43 #define __RSA_H__
44 
45 #include "../cap/capdecl.h"
46 
47 #ifdef __ENABLE_MOCANA_CRYPTO_INTERFACE__
48 #include "../crypto_interface/crypto_interface_rsa_priv.h"
49 #endif
50 
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
54 
55 /* Forward declaration */
56 typedef struct MRsaKeyTemplate *MRsaKeyTemplatePtr;
57 
60 #define NUM_RSA_VLONG (7)
61 #define NUM_RSA_MODEXP (2)
62 
63 #if !defined( __DISABLE_MOCANA_RSA_DECRYPTION__) && !defined(__PSOS_RTOS__)
64 typedef struct BlindingHelper
65 {
66  RTOS_MUTEX blindingMutex;
67  vlong* pRE;
68  vlong* pR1;
69  ubyte4 counter;
70 } BlindingHelper;
71 #endif
72 
75 struct RSAKey;
76 
77 #ifdef __ENABLE_MOCANA_HW_SECURITY_MODULE__
78 struct HSMRSAInfo;
79 #endif
80 
81 typedef struct RSAKey
82 {
83  intBoolean privateKey;
84  vlong* v[NUM_RSA_VLONG];
85  ModExpHelper modExp[NUM_RSA_MODEXP];
86 #if !defined(__DISABLE_MOCANA_RSA_DECRYPTION__) && !defined( __PSOS_RTOS__)
87  BlindingHelper blinding;
88 #endif
89 #ifdef __ENABLE_MOCANA_HW_SECURITY_MODULE__
90  struct HSMRSAInfo* hsmInfo;
91 #endif
92  MocAsymKey pPrivateKey;
93  MocAsymKey pPublicKey;
94  ubyte4 enabled;
95 } RSAKey;
96 
98 #define RSA_E(k) ((k)->v[0])
99 
100 #define RSA_N(k) ((k)->v[1])
101 
102 #define RSA_P(k) ((k)->v[2])
103 
104 #define RSA_Q(k) ((k)->v[3])
105 
106 #define RSA_DP(k) ((k)->v[4])
107 
108 #define RSA_DQ(k) ((k)->v[5])
109 
110 #define RSA_QINV(k) ((k)->v[6])
111 
112 #define RSA_MODEXP_P(k) ((k)->modExp[0])
113 
114 #define RSA_MODEXP_Q(k) ((k)->modExp[1])
115 
117 #define RSA_KEYSIZE(k) (VLONG_bitLength(RSA_N(k)))
118 
119 /*------------------------------------------------------------------*/
120 
123 /* RSA primitives defined in PKCS#1 version 2.1 */
124 #if !defined(__DISABLE_MOCANA_RSA_DECRYPTION__)
125 MOC_EXTERN MSTATUS RSA_RSADP(MOC_RSA(hwAccelDescr hwAccelCtx) const RSAKey *pRSAKey, const vlong *pCipherText, vlong **ppMessage, vlong **ppVlongQueue);
126 #endif
127 MOC_EXTERN MSTATUS RSA_RSAEP(MOC_RSA(hwAccelDescr hwAccelCtx) const RSAKey *pPublicRSAKey, const vlong *pMessage, vlong **ppRetCipherText, vlong **ppVlongQueue);
128 
129 #if (!defined(__DISABLE_MOCANA_RSA_DECRYPTION__) && defined(__RSAINT_HARDWARE__) && defined(__ENABLE_MOCANA_PKCS11_CRYPTO__))
130 #define RSA_RSASP1 RSAINT_decrypt
131 #elif (!defined(__DISABLE_MOCANA_RSA_DECRYPTION__))
132 MOC_EXTERN MSTATUS RSA_RSASP1(MOC_RSA(hwAccelDescr hwAccelCtx) const RSAKey *pRSAKey, const vlong *pMessage, RNGFun rngFun, void* rngFunArg, vlong **ppRetSignature, vlong **ppVlongQueue);
133 #endif
134 MOC_EXTERN MSTATUS RSA_RSAVP1(MOC_RSA(hwAccelDescr hwAccelCtx) const RSAKey *pPublicRSAKey, const vlong *pSignature, vlong **ppRetMessage, vlong **ppVlongQueue);
135 
138 /*------------------------------------------------------------------*/
139 
142 /* used for custom RSA blinding implementation */
143 typedef MSTATUS (*RSADecryptFunc)(MOC_RSA(hwAccelDescr hwAccelCtx)
144  const RSAKey *pRSAKey,
145  const vlong *c,
146  vlong **ppRetDecrypt,
147  vlong **ppVlongQueue);
148 
149 typedef MSTATUS (*CustomBlindingFunc)( MOC_RSA(hwAccelDescr hwAccelCtx)
150  const RSAKey* pRSAKeyInt,
151  const vlong* pCipher,
152  RNGFun rngFun, void* rngFunArg,
153  RSADecryptFunc rsaDecryptPrimitive,
154  vlong** ppRetDecrypt,
155  vlong** ppVlongQueue);
156 
159 /*------------------------------------------------------------------*/
160 
169 MOC_EXTERN MSTATUS
170 RSA_generateKeyFipsSteps(MOC_RSA(hwAccelDescr hwAccelCtx) randomContext *pRandomContext,
171  ubyte4 nLen, vlong *e, const vlong *pDebugX, ubyte4 length1, ubyte4 length2,
172  vlong **ppRetP1, vlong **ppRetP2, vlong **ppRetXp, vlong **ppRetPrime,
173  ubyte *pInputSeed, ubyte4 inputSeedLength,
174  ubyte *pRetPrimeSeed1, ubyte *pRetPrimeSeed2,
175  intBoolean *pRetFail,
176  MSTATUS (*completeDigest)(MOC_HASH(hwAccelDescr hwAccelCtx) const ubyte *pData, ubyte4 dataLen, ubyte *pDigestOutput),
177  ubyte4 hashResultSize,
178  vlong **ppVlongQueue);
179 
180 
219 MOC_EXTERN MSTATUS RSA_createKey(RSAKey **pp_RetRSAKey);
220 
260 MOC_EXTERN MSTATUS RSA_freeKey(RSAKey **ppFreeRSAKey, vlong **ppVlongQueue);
261 
299 MOC_EXTERN MSTATUS RSA_cloneKey(MOC_RSA(hwAccelDescr hwAccelCtx) RSAKey **ppNew, const RSAKey *pSrc, vlong **ppVlongQueue);
300 
301 
337 MOC_EXTERN MSTATUS RSA_equalKey(MOC_RSA(hwAccelDescr hwAccelCtx) const RSAKey *pKey1, const RSAKey *pKey2, byteBoolean *pResult);
338 
339 
385 MOC_EXTERN MSTATUS RSA_setPublicKeyParameters(MOC_RSA(hwAccelDescr hwAccelCtx) RSAKey *pKey,
386  ubyte4 exponent,
387  const ubyte* modulus,
388  ubyte4 modulusLen,
389  vlong **ppVlongQueue);
390 
414 MOC_EXTERN MSTATUS RSA_setPublicKeyData (
415  MOC_RSA(hwAccelDescr hwAccelCtx)
416  RSAKey *pKey,
417  ubyte *pPubExpo,
418  ubyte4 pubExpoLen,
419  const ubyte *pModulus,
420  ubyte4 modulusLen,
421  vlong **ppVlongQueue
422  );
423 
474 MOC_EXTERN MSTATUS RSA_setAllKeyParameters(
475  MOC_RSA(hwAccelDescr hwAccelCtx)
476  RSAKey *pKey,
477  ubyte4 exponent,
478  const ubyte *modulus,
479  ubyte4 modulusLen,
480  const ubyte *prime1,
481  ubyte4 prime1Len,
482  const ubyte *prime2,
483  ubyte4 prime2Len,
484  vlong **ppVlongQueue);
485 
517 MOC_EXTERN MSTATUS RSA_setAllKeyData (
518  MOC_RSA(hwAccelDescr hwAccelCtx)
519  RSAKey *pKey,
520  ubyte *pPubExpo,
521  ubyte4 pubExpoLen,
522  const ubyte *pModulus,
523  ubyte4 modulusLen,
524  const ubyte *pPrime1,
525  ubyte4 prime1Len,
526  const ubyte *pPrime2,
527  ubyte4 prime2Len,
528  vlong **ppVlongQueue
529  );
530 
558 MOC_EXTERN MSTATUS RSA_getKeyParametersAlloc (
559  MOC_RSA(hwAccelDescr hwAccelCtx)
560  RSAKey *pKey,
561  MRsaKeyTemplatePtr pTemplate,
562  ubyte keyType
563  );
564 
580 MOC_EXTERN MSTATUS RSA_freeKeyTemplate (
581  RSAKey *pKey,
582  MRsaKeyTemplatePtr pTemplate
583  );
584 
620 MOC_EXTERN MSTATUS RSA_getCipherTextLength(MOC_RSA(hwAccelDescr hwAccelCtx) const RSAKey *pKey, sbyte4 *pCipherTextLen);
621 
622 
684 MOC_EXTERN MSTATUS RSA_encrypt(MOC_RSA(hwAccelDescr hwAccelCtx)
685  const RSAKey *pKey,
686  const ubyte* plainText,
687  ubyte4 plainTextLen,
688  ubyte* cipherText,
689  RNGFun rngFun,
690  void* rngFunArg,
691  vlong **ppVlongQueue);
692 
693 #ifndef __DISABLE_MOCANA_RSA_DECRYPTION__
694 
753 MOC_EXTERN MSTATUS RSA_decrypt(MOC_RSA(hwAccelDescr hwAccelCtx)
754  const RSAKey *pKey,
755  const ubyte* cipherText,
756  ubyte* plainText,
757  ubyte4* plainTextLen,
758  RNGFun rngFun,
759  void* rngFunArg,
760  vlong **ppVlongQueue);
761 #endif
762 
763 
814 MOC_EXTERN MSTATUS RSA_verifySignature(MOC_RSA(hwAccelDescr hwAccelCtx)
815  const RSAKey *pKey,
816  const ubyte* cipherText,
817  ubyte* plainText,
818  ubyte4* plainTextLen,
819  vlong **ppVlongQueue);
820 
855 MOC_EXTERN MSTATUS RSA_verifyDigest(MOC_RSA(hwAccelDescr hwAccelCtx)
856  RSAKey *pKey,
857  ubyte *pMsgDigest,
858  ubyte4 digestLen,
859  ubyte* pSignature,
860  ubyte4 sigLen,
861  intBoolean *pIsValid,
862  vlong **ppVlongQueue);
863 
914 MOC_EXTERN MSTATUS RSA_signMessage(MOC_RSA(hwAccelDescr hwAccelCtx)
915  const RSAKey *pKey,
916  const ubyte* plainText,
917  ubyte4 plainTextLen,
918  ubyte* cipherText,
919  vlong **ppVlongQueue);
920 
926 MOC_EXTERN MSTATUS RSA_generateKeyFIPS(MOC_RSA(hwAccelDescr hwAccelCtx) randomContext *pRandomContext,
927  RSAKey *p_rsaKey, ubyte4 keySize, vlong **Xp, vlong **Xp1, vlong **Xp2,
928  vlong **Xq, vlong **Xq1, vlong **Xq2, vlong **ppVlongQueue);
929 
930 
931 
978 MOC_EXTERN MSTATUS RSA_generateKey(MOC_RSA(hwAccelDescr hwAccelCtx)
979  randomContext *pRandomContext,
980  RSAKey *p_rsaKey,
981  ubyte4 keySize,
982  vlong **ppVlongQueue);
983 
984 
1022 MOC_EXTERN MSTATUS RSA_prepareKey(MOC_RSA(hwAccelDescr hwAccelCtx)
1023  RSAKey *pRSAKey, vlong** ppVlongQueue);
1024 
1069 MOC_EXTERN MSTATUS RSA_keyFromByteString(MOC_RSA(hwAccelDescr hwAccelCtx)
1070  RSAKey **ppKey,
1071  const ubyte* byteString,
1072  ubyte4 len,
1073  vlong **ppVlongQueue);
1119 MOC_EXTERN MSTATUS RSA_byteStringFromKey(MOC_RSA(hwAccelDescr hwAccelCtx)
1120  const RSAKey *pKey, ubyte *pBuffer,
1121  ubyte4 *pRetLen);
1122 
1123 /* The RSAKey struct does not contain the priExpo. So if you need it, compute it
1124  * using this function.
1125  */
1126 MOC_EXTERN MSTATUS RSA_getPrivateExponent (
1127  MOC_RSA(hwAccelDescr hwAccelCtx) RSAKey *pRSAKey,
1128  vlong **ppRetD,
1129  vlong **ppVlongQueue
1130  );
1131 
1152 MOC_EXTERN MSTATUS RSA_applyPublicKey (
1153  MOC_RSA(hwAccelDescr hwAccelCtx)
1154  RSAKey *pPublicKey,
1155  ubyte *pInput,
1156  ubyte4 inputLen,
1157  ubyte **ppOutput,
1158  vlong **ppVlongQueue
1159  );
1160 
1185 MOC_EXTERN MSTATUS RSA_applyPrivateKey (
1186  MOC_RSA(hwAccelDescr hwAccelCtx)
1187  RSAKey *pPrivateKey,
1188  RNGFun rngFun,
1189  void *rngFunArg,
1190  ubyte *pInput,
1191  ubyte4 inputLen,
1192  ubyte **ppOutput,
1193  vlong **ppVlongQueue
1194  );
1195 
1196 #ifdef __cplusplus
1197 }
1198 #endif
1199 
1200 #endif
MOC_EXTERN MSTATUS RSA_verifyDigest(MOC_RSA(hwAccelDescr hwAccelCtx) RSAKey *pKey, ubyte *pMsgDigest, ubyte4 digestLen, ubyte *pSignature, ubyte4 sigLen, intBoolean *pIsValid, vlong **ppVlongQueue)
Verify the digest of a message.
MOC_EXTERN MSTATUS RSA_verifySignature(MOC_RSA(hwAccelDescr hwAccelCtx) const RSAKey *pKey, const ubyte *cipherText, ubyte *plainText, ubyte4 *plainTextLen, vlong **ppVlongQueue)
Verify decrypted buffer's signature.
MOC_EXTERN MSTATUS RSA_freeKey(RSAKey **ppFreeRSAKey, vlong **ppVlongQueue)
Free (delete) an RSA key.
MOC_EXTERN MSTATUS RSA_freeKeyTemplate(RSAKey *pKey, MRsaKeyTemplatePtr pTemplate)
Free the RSA standard key template structure previouslly allocated by RSA_getKeyParametersAlloc.
MOC_EXTERN MSTATUS RSA_decrypt(MOC_RSA(hwAccelDescr hwAccelCtx) const RSAKey *pKey, const ubyte *cipherText, ubyte *plainText, ubyte4 *plainTextLen, RNGFun rngFun, void *rngFunArg, vlong **ppVlongQueue)
Decrypt ciphertext using PKCS #1.
MOC_EXTERN MSTATUS RSA_setAllKeyParameters(MOC_RSA(hwAccelDescr hwAccelCtx) RSAKey *pKey, ubyte4 exponent, const ubyte *modulus, ubyte4 modulusLen, const ubyte *prime1, ubyte4 prime1Len, const ubyte *prime2, ubyte4 prime2Len, vlong **ppVlongQueue)
Set RSA public key parameters.
MOC_EXTERN MSTATUS RSA_signMessage(MOC_RSA(hwAccelDescr hwAccelCtx) const RSAKey *pKey, const ubyte *plainText, ubyte4 plainTextLen, ubyte *cipherText, vlong **ppVlongQueue)
Generate signature for plaintext buffer, using provided RSA private key and PKCS #1.
MOC_EXTERN MSTATUS RSA_encrypt(MOC_RSA(hwAccelDescr hwAccelCtx) const RSAKey *pKey, const ubyte *plainText, ubyte4 plainTextLen, ubyte *cipherText, RNGFun rngFun, void *rngFunArg, vlong **ppVlongQueue)
Encrypt plaintext using PKCS #1.
MOC_EXTERN MSTATUS RSA_cloneKey(MOC_RSA(hwAccelDescr hwAccelCtx) RSAKey **ppNew, const RSAKey *pSrc, vlong **ppVlongQueue)
Clone (copy) an RSA key.
MOC_EXTERN MSTATUS RSA_equalKey(MOC_RSA(hwAccelDescr hwAccelCtx) const RSAKey *pKey1, const RSAKey *pKey2, byteBoolean *pResult)
Determine whether two RSA keys are equal.
MOC_EXTERN MSTATUS RSA_applyPrivateKey(MOC_RSA(hwAccelDescr hwAccelCtx) RSAKey *pPrivateKey, RNGFun rngFun, void *rngFunArg, ubyte *pInput, ubyte4 inputLen, ubyte **ppOutput, vlong **ppVlongQueue)
This function performs an RSA private key operation with no padding.
MOC_EXTERN MSTATUS RSA_generateKey(MOC_RSA(hwAccelDescr hwAccelCtx) randomContext *pRandomContext, RSAKey *p_rsaKey, ubyte4 keySize, vlong **ppVlongQueue)
Generate RSA key pair (private and public keys).
MOC_EXTERN MSTATUS RSA_setPublicKeyParameters(MOC_RSA(hwAccelDescr hwAccelCtx) RSAKey *pKey, ubyte4 exponent, const ubyte *modulus, ubyte4 modulusLen, vlong **ppVlongQueue)
Set RSA public key parameters: exponent and modulus.
sbyte4(* RNGFun)(void *rngFunArg, ubyte4 length, ubyte *buffer)
Function pointer type for a method that produces (pseudo) random bytes.
Definition: random.h:98
MOC_EXTERN MSTATUS RSA_setAllKeyData(MOC_RSA(hwAccelDescr hwAccelCtx) RSAKey *pKey, ubyte *pPubExpo, ubyte4 pubExpoLen, const ubyte *pModulus, ubyte4 modulusLen, const ubyte *pPrime1, ubyte4 prime1Len, const ubyte *pPrime2, ubyte4 prime2Len, vlong **ppVlongQueue)
This is the same as RSA_setAllKeyParameters, except the public exponent is passed in as a canonical i...
MOC_EXTERN MSTATUS RSA_createKey(RSAKey **pp_RetRSAKey)
Create memory storage for an RSA key pair.
MOC_EXTERN MSTATUS RSA_getCipherTextLength(MOC_RSA(hwAccelDescr hwAccelCtx) const RSAKey *pKey, sbyte4 *pCipherTextLen)
Get an RSA public key's ciphertext length.
MOC_EXTERN MSTATUS RSA_applyPublicKey(MOC_RSA(hwAccelDescr hwAccelCtx) RSAKey *pPublicKey, ubyte *pInput, ubyte4 inputLen, ubyte **ppOutput, vlong **ppVlongQueue)
This function performs an RSA public key operation with no padding.
MOC_EXTERN MSTATUS RSA_getKeyParametersAlloc(MOC_RSA(hwAccelDescr hwAccelCtx) RSAKey *pKey, MRsaKeyTemplatePtr pTemplate, ubyte keyType)
Retrieve the following components from an RSA key as byte string buffers:
MOC_EXTERN MSTATUS RSA_byteStringFromKey(MOC_RSA(hwAccelDescr hwAccelCtx) const RSAKey *pKey, ubyte *pBuffer, ubyte4 *pRetLen)
Convert RSA key to a string of (PKCS #1) bytes.
MOC_EXTERN MSTATUS RSA_prepareKey(MOC_RSA(hwAccelDescr hwAccelCtx) RSAKey *pRSAKey, vlong **ppVlongQueue)
Compute RSA private key.
MOC_EXTERN MSTATUS RSA_setPublicKeyData(MOC_RSA(hwAccelDescr hwAccelCtx) RSAKey *pKey, ubyte *pPubExpo, ubyte4 pubExpoLen, const ubyte *pModulus, ubyte4 modulusLen, vlong **ppVlongQueue)
This is the same as RSA_setPublicKeyParameters, except the public exponent is passed in as a canonica...
MOC_EXTERN MSTATUS RSA_keyFromByteString(MOC_RSA(hwAccelDescr hwAccelCtx) RSAKey **ppKey, const ubyte *byteString, ubyte4 len, vlong **ppVlongQueue)
Convert byte string to RSA key, using PKCS #1.