TrustCore SDK NanoCrypto API reference  version 7.0
dh.h
Go to the documentation of this file.
1 /*
2  * dh.h
3  *
4  * Diffie-Hellman Key Exchange
5  *
6  * Copyright 2019-2024 DigiCert, Inc. All Rights Reserved.
7  * Proprietary and Confidential Material.
8  *
9  */
10 
26 /*------------------------------------------------------------------*/
27 
28 #ifndef __KEYEX_DH_HEADER__
29 #define __KEYEX_DH_HEADER__
30 
31 #include "../cap/capdecl.h"
32 
33 #ifdef __ENABLE_MOCANA_CRYPTO_INTERFACE__
34 #include "../crypto_interface/crypto_interface_dh_priv.h"
35 #endif
36 
37 #include "../cap/capasym.h"
38 #include "../crypto/ffc.h"
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 #define DH_GROUP_TBD 0
45 #define DH_GROUP_1 1
46 #define DH_GROUP_2 2
47 #define DH_GROUP_5 5
48 #define DH_GROUP_14 14
49 #define DH_GROUP_15 15
50 #define DH_GROUP_16 16
51 #define DH_GROUP_17 17
52 #define DH_GROUP_18 18
53 #define DH_GROUP_24 24
54 
55 #define DH_GROUP_19 19
56 #define DH_GROUP_20 20
57 #define DH_GROUP_21 21
58 #define DH_GROUP_25 25
59 #define DH_GROUP_26 26
60 #define DH_GROUP_31 31
61 #define DH_GROUP_32 32
62 
63 #define DH_GROUP_FFDHE2048 0x100
64 #define DH_GROUP_FFDHE3072 0x101
65 #define DH_GROUP_FFDHE4096 0x102
66 #define DH_GROUP_FFDHE6144 0x103
67 #define DH_GROUP_FFDHE8192 0x104
68 
69 #define COMPUTED_VLONG_G(X) (X)->dh_g
70 #define COMPUTED_VLONG_Y(X) (X)->dh_y
71 #define COMPUTED_VLONG_F(X) (X)->dh_f
72 #define COMPUTED_VLONG_E(X) (X)->dh_e
73 #define COMPUTED_VLONG_K(X) (X)->dh_k
74 #define COMPUTED_VLONG_P(X) (X)->dh_p
75 #define COMPUTED_VLONG_Q(X) (X)->dh_q
76 
77 #ifndef __DISABLE_MOCANA_DH_BLINDING__
78 #define COMPUTED_VLONG_VF(X) (X)->dh_vf
79 #define COMPUTED_VLONG_VI(X) (X)->dh_vi
80 #define COMPUTED_VLONG_PY(X) (X)->dh_py
81 #endif
82 
83 /* for SSH context */
84 #define DIFFIEHELLMAN_CONTEXT(X) (X)->p_dhContext
85 
86 /*------------------------------------------------------------------*/
87 
98 typedef struct diffieHellmanContext
99 {
104  vlong* dh_g; /* generator */
109  vlong* dh_p; /* big prime */
114  vlong* dh_q; /* prime divisor */
119  vlong* dh_y; /* random number - private key */
124  vlong* dh_f; /* sent by the server - public key */
129  vlong* dh_e; /* sent by the client - public key */
134  vlong* dh_k; /* shared secret */
135 
136  MocAsymKey pPrivateKey;
137  MocAsymKey pPublicKey;
138  ubyte4 enabled;
139 
140 #ifndef __DISABLE_MOCANA_DH_BLINDING__
141 
142  vlong* dh_vi; /* the blinding value */
143  vlong* dh_vf; /* the unblinding value */
144  vlong* dh_py; /* the previous value of y */
145 
146 #endif
147 
149 
150 /*------------------------------------------------------------------*/
151 
195 MOC_EXTERN MSTATUS DH_getG(ubyte4 groupNum, vlong **ppRetG);
196 
197 
242 MOC_EXTERN MSTATUS DH_getP(ubyte4 groupNum, vlong **ppRetP);
243 
244 
273 MOC_EXTERN MSTATUS DH_getPByteString(ubyte4 groupNum, const ubyte** ppBytes, sbyte4* pLen);
274 
275 
321 MOC_EXTERN MSTATUS DH_allocate(diffieHellmanContext **pp_dhContext);
322 
323 
393 MOC_EXTERN MSTATUS DH_allocateServer(MOC_DH(hwAccelDescr hwAccelCtx) randomContext *pRandomContext, diffieHellmanContext **pp_dhContext, ubyte4 groupNum);
394 
395 
462 MOC_EXTERN MSTATUS DH_allocateClient(MOC_DH(hwAccelDescr hwAccelCtx) randomContext *pRandomContext, diffieHellmanContext **pp_dhContext, ubyte4 groupNum);
463 
464 
533 MOC_EXTERN MSTATUS DH_allocateClientAux(MOC_DH(hwAccelDescr hwAccelCtx) randomContext *pRandomContext, diffieHellmanContext **pp_dhContext, ubyte4 groupNum);
534 
535 
609 MOC_EXTERN MSTATUS DH_setPG(MOC_DH(hwAccelDescr hwAccelCtx) randomContext *pRandomContext, ubyte4 lengthY, diffieHellmanContext *p_dhContext, const vlong *P, const vlong *G);
610 
611 
688 MOC_EXTERN MSTATUS DH_setPGQ(MOC_DH(hwAccelDescr hwAccelCtx) randomContext *pRandomContext, ubyte4 lengthY, diffieHellmanContext *p_dhContext, const vlong *P, const vlong *G, const vlong *Q);
689 
690 
738 MOC_EXTERN MSTATUS DH_freeDhContext(diffieHellmanContext **pp_dhContext, vlong **ppVlongQueue);
739 
740 
797 MOC_EXTERN MSTATUS DH_computeKeyExchange(MOC_DH(hwAccelDescr hwAccelCtx) diffieHellmanContext *p_dhContext, vlong** ppVlongQueue);
798 
799 
816 MOC_EXTERN MSTATUS DH_setKeyParameters(MOC_DH(hwAccelDescr hwAccelCtx) diffieHellmanContext *pTargetCtx, MDhKeyTemplate *pSrcTemplate);
817 
818 
834 MOC_EXTERN MSTATUS DH_getKeyParametersAlloc(MOC_DH(hwAccelDescr hwAccelCtx) MDhKeyTemplate *pTargetTemplate, diffieHellmanContext *pSrcCtx, ubyte keyType);
835 
836 
847 MOC_EXTERN MSTATUS DH_freeKeyTemplate(diffieHellmanContext *pCtx, MDhKeyTemplate *pTemplate);
848 
849 
863 MOC_EXTERN MSTATUS DH_generateKeyPair(MOC_DH(hwAccelDescr hwAccelCtx) diffieHellmanContext *pCtx, randomContext *pRandomContext, ubyte4 numBytes);
864 
865 
879 MOC_EXTERN MSTATUS DH_getPublicKey(MOC_DH(hwAccelDescr hwAccelCtx) diffieHellmanContext *pCtx, ubyte **ppPublicKey, ubyte4 *pPublicKeyLen);
880 
881 
901 MOC_EXTERN MSTATUS DH_computeKeyExchangeEx(MOC_DH(hwAccelDescr hwAccelCtx) diffieHellmanContext *pCtx, randomContext *pRandomContext, ubyte *pOtherPartysPublicKey, ubyte4 publicKeyLen,
902  ubyte **ppSharedSecret, ubyte4 *pSharedSecretLen);
903 
904 
905 /* mode macros for DH Key Agreement Schemes */
906 #define DH_HYBRID1 0
907 #define MQV2 1
908 #define DH_EPHEMERAL 2
909 #define DH_HYBRID_ONE_FLOW_U 3
910 #define DH_HYBRID_ONE_FLOW_V 4
911 #define MQV1_U 5
912 #define MQV1_V 6
913 #define DH_ONE_FLOW_U 7
914 #define DH_ONE_FLOW_V 8
915 #define DH_STATIC 9
916 
953 MOC_EXTERN MSTATUS DH_keyAgreementScheme(
954  MOC_DH(hwAccelDescr hwAccelCtx)
955  ubyte4 mode,
956  randomContext *pRandomContext,
957  diffieHellmanContext *pStatic,
958  diffieHellmanContext *pEphemeral,
959  ubyte *pOtherPartysStatic,
960  ubyte4 otherStaticLen,
961  ubyte *pOtherPartysEphemeral,
962  ubyte4 otherEphemeralLen,
963  ubyte **ppSharedSecret,
964  ubyte4 *pSharedSecretLen);
965 
966 
1008 MOC_EXTERN MSTATUS DH_validateDomainParams(MOC_DH(hwAccelDescr hwAccelCtx) randomContext* pFipsRngCtx,
1009  diffieHellmanContext *pCtx, FFCHashType hashType, ubyte4 C,
1010  ubyte *pSeed, ubyte4 seedSize, intBoolean *pIsValid, ubyte4 *pPriKeyLen, vlong **ppVlongQueue);
1011 
1039 MOC_EXTERN MSTATUS DH_verifySafePG(diffieHellmanContext *pCtx, intBoolean *pIsValid, ubyte4 *pPriKeyLen, vlong **ppVlongQueue);
1040 
1078 MOC_EXTERN MSTATUS DH_verifyPQ_FIPS1864(MOC_DH(hwAccelDescr hwAccelCtx) randomContext* pFipsRngCtx,
1079  diffieHellmanContext *pCtx, FFCHashType hashType, ubyte4 C,
1080  ubyte *pSeed, ubyte4 seedSize, intBoolean *pIsValid, vlong **ppVlongQueue);
1081 
1107 MOC_EXTERN MSTATUS DH_verifyG(MOC_DH(hwAccelDescr hwAccelCtx) diffieHellmanContext *pCtx, intBoolean *pIsValid, vlong **ppVlongQueue);
1108 
1109 
1110 /* Ext versions of all the above methods that will be used in crypto interface */
1111 
1115 MOC_EXTERN MSTATUS DH_allocateExt(diffieHellmanContext **pp_dhContext, void *pExtCtx);
1116 MOC_EXTERN MSTATUS DH_allocateServerExt(MOC_DH(hwAccelDescr hwAccelCtx) randomContext *pRandomContext, diffieHellmanContext **pp_dhContext, ubyte4 groupNum, void *pExtCtx);
1117 MOC_EXTERN MSTATUS DH_allocateClientAuxExt(MOC_DH(hwAccelDescr hwAccelCtx) randomContext *pRandomContext, diffieHellmanContext **pp_dhContext, ubyte4 groupNum, void *pExtCtx);
1118 MOC_EXTERN MSTATUS DH_freeDhContextExt(diffieHellmanContext **pp_dhContext, vlong **ppVlongQueue, void *pExtCtx);
1119 MOC_EXTERN MSTATUS DH_setKeyParametersExt(MOC_DH(hwAccelDescr hwAccelCtx) diffieHellmanContext *pTargetCtx, MDhKeyTemplate *pSrcTemplate, void *pExtCtx);
1120 MOC_EXTERN MSTATUS DH_getKeyParametersAllocExt(MOC_DH(hwAccelDescr hwAccelCtx) MDhKeyTemplate *pTargetTemplate, diffieHellmanContext *pSrcCtx, ubyte keyType, void *pExtCtx);
1121 MOC_EXTERN MSTATUS DH_freeKeyTemplateExt(diffieHellmanContext *pCtx, MDhKeyTemplate *pTemplate, void *pExtCtx);
1122 MOC_EXTERN MSTATUS DH_generateKeyPairExt(MOC_DH(hwAccelDescr hwAccelCtx) diffieHellmanContext *pCtx, randomContext *pRandomContext, ubyte4 numBytes, void *pExtCtx);
1123 MOC_EXTERN MSTATUS DH_getPublicKeyExt(MOC_DH(hwAccelDescr hwAccelCtx) diffieHellmanContext *pCtx, ubyte **ppPublicKey, ubyte4 *pPublicKeyLen, void *pExtCtx);
1124 MOC_EXTERN MSTATUS DH_computeKeyExchangeExExt(MOC_DH(hwAccelDescr hwAccelCtx) diffieHellmanContext *pCtx, randomContext *pRandomContext, ubyte *pOtherPartysPublicKey,
1125  ubyte4 publicKeyLen, ubyte **ppSharedSecret, ubyte4 *pSharedSecretLen, void *pExtCtx);
1126 
1130 #ifdef __cplusplus
1131 }
1132 #endif
1133 
1134 #endif /* __KEYEX_DH_HEADER__ */
MOC_EXTERN MSTATUS DH_getG(ubyte4 groupNum, vlong **ppRetG)
Get a Generator value for DH calculations.
Context information for an exercise of the DH protocol.
Definition: dh.h:98
vlong * dh_q
Prime Divisor value; see DH_setPGQ().
Definition: dh.h:114
MOC_EXTERN MSTATUS DH_freeKeyTemplate(diffieHellmanContext *pCtx, MDhKeyTemplate *pTemplate)
Zeros and frees each parameter stored in pTemplate.
MOC_EXTERN MSTATUS DH_keyAgreementScheme(MOC_DH(hwAccelDescr hwAccelCtx) ubyte4 mode, randomContext *pRandomContext, diffieHellmanContext *pStatic, diffieHellmanContext *pEphemeral, ubyte *pOtherPartysStatic, ubyte4 otherStaticLen, ubyte *pOtherPartysEphemeral, ubyte4 otherEphemeralLen, ubyte **ppSharedSecret, ubyte4 *pSharedSecretLen)
Generates a Diffie-Hellman shared secret via one of the major modes.
MOC_EXTERN MSTATUS DH_verifySafePG(diffieHellmanContext *pCtx, intBoolean *pIsValid, ubyte4 *pPriKeyLen, vlong **ppVlongQueue)
Validates that the P and G domain parameters come from one of the pre approved safe prime groups...
MOC_EXTERN MSTATUS DH_setPG(MOC_DH(hwAccelDescr hwAccelCtx) randomContext *pRandomContext, ubyte4 lengthY, diffieHellmanContext *p_dhContext, const vlong *P, const vlong *G)
Assign a prime and a generator to an allocated DH context.
vlong * dh_e
Received public key of the remote peer.
Definition: dh.h:129
MOC_EXTERN MSTATUS DH_computeKeyExchangeEx(MOC_DH(hwAccelDescr hwAccelCtx) diffieHellmanContext *pCtx, randomContext *pRandomContext, ubyte *pOtherPartysPublicKey, ubyte4 publicKeyLen, ubyte **ppSharedSecret, ubyte4 *pSharedSecretLen)
Generates a shared secret from the domain parameters and our private key stored in the context...
MOC_EXTERN MSTATUS DH_getPByteString(ubyte4 groupNum, const ubyte **ppBytes, sbyte4 *pLen)
Get a large prime number to use as your DH private key as a Big Endian byte array.
MOC_EXTERN MSTATUS DH_getPublicKey(MOC_DH(hwAccelDescr hwAccelCtx) diffieHellmanContext *pCtx, ubyte **ppPublicKey, ubyte4 *pPublicKeyLen)
This method will allocate a buffer and fill it with our public key in Big Endian binary.
MOC_EXTERN MSTATUS DH_allocate(diffieHellmanContext **pp_dhContext)
Allocate and initialize a diffieHellmanContext structure.
MOC_EXTERN MSTATUS DH_verifyG(MOC_DH(hwAccelDescr hwAccelCtx) diffieHellmanContext *pCtx, intBoolean *pIsValid, vlong **ppVlongQueue)
Verifies the domain parameter G is valid with respect to the P and Q parameters in a diffieHellmanCon...
vlong * dh_f
Public key of the local peer; see DH_setPG().
Definition: dh.h:124
MOC_EXTERN MSTATUS DH_allocateServer(MOC_DH(hwAccelDescr hwAccelCtx) randomContext *pRandomContext, diffieHellmanContext **pp_dhContext, ubyte4 groupNum)
Allocate and initialize resources for a DH server.
MOC_EXTERN MSTATUS DH_allocateClientAux(MOC_DH(hwAccelDescr hwAccelCtx) randomContext *pRandomContext, diffieHellmanContext **pp_dhContext, ubyte4 groupNum)
Allocate and initialize resources for a DH client with the group generator G.
MOC_EXTERN MSTATUS DH_allocateClient(MOC_DH(hwAccelDescr hwAccelCtx) randomContext *pRandomContext, diffieHellmanContext **pp_dhContext, ubyte4 groupNum)
Allocate and initialize resources for a DH client.
vlong * dh_g
Generator value; see DH_getG().
Definition: dh.h:104
MOC_EXTERN MSTATUS DH_freeDhContext(diffieHellmanContext **pp_dhContext, vlong **ppVlongQueue)
Free the memory allocated for a diffieHellmanContext structure.
vlong * dh_y
Private key of the local peer; see DH_setPG().
Definition: dh.h:119
MOC_EXTERN MSTATUS DH_getKeyParametersAlloc(MOC_DH(hwAccelDescr hwAccelCtx) MDhKeyTemplate *pTargetTemplate, diffieHellmanContext *pSrcCtx, ubyte keyType)
For each parameter value g, p, q, y, f that is stored in the pSrcCtx, this method will allocate space...
MOC_EXTERN MSTATUS DH_getP(ubyte4 groupNum, vlong **ppRetP)
Get a large prime number to use as your DH private key.
MOC_EXTERN MSTATUS DH_validateDomainParams(MOC_DH(hwAccelDescr hwAccelCtx) randomContext *pFipsRngCtx, diffieHellmanContext *pCtx, FFCHashType hashType, ubyte4 C, ubyte *pSeed, ubyte4 seedSize, intBoolean *pIsValid, ubyte4 *pPriKeyLen, vlong **ppVlongQueue)
Validates the Diffie-Hellman domain parameters.
vlong * dh_k
Shared secret; see DH_computeKeyExchange().
Definition: dh.h:134
MOC_EXTERN MSTATUS DH_verifyPQ_FIPS1864(MOC_DH(hwAccelDescr hwAccelCtx) randomContext *pFipsRngCtx, diffieHellmanContext *pCtx, FFCHashType hashType, ubyte4 C, ubyte *pSeed, ubyte4 seedSize, intBoolean *pIsValid, vlong **ppVlongQueue)
Verifies the domain parameters P and Q in a context come from the FIPS 186-4 algorithm.
MOC_EXTERN MSTATUS DH_setPGQ(MOC_DH(hwAccelDescr hwAccelCtx) randomContext *pRandomContext, ubyte4 lengthY, diffieHellmanContext *p_dhContext, const vlong *P, const vlong *G, const vlong *Q)
Assign a prime and a generator to an allocated DH context.
MOC_EXTERN MSTATUS DH_computeKeyExchange(MOC_DH(hwAccelDescr hwAccelCtx) diffieHellmanContext *p_dhContext, vlong **ppVlongQueue)
Compute the shared secret.
vlong * dh_p
Large Prime modulus value; see DH_getP().
Definition: dh.h:109
MOC_EXTERN MSTATUS DH_setKeyParameters(MOC_DH(hwAccelDescr hwAccelCtx) diffieHellmanContext *pTargetCtx, MDhKeyTemplate *pSrcTemplate)
Sets the key parameters stored in pSrcTemplate in the pTargetCtx.
MOC_EXTERN MSTATUS DH_generateKeyPair(MOC_DH(hwAccelDescr hwAccelCtx) diffieHellmanContext *pCtx, randomContext *pRandomContext, ubyte4 numBytes)
This method generates a key pair (y,f) within a context that has already had had the domain params p ...