📄 dh.h
字号:
/* Copyright 2003-2006, Voltage Security, all rights reserved.
*/
#include "vibecrypto.h"
#include "environment.h"
#include "base.h"
#include "libctx.h"
#include "paramobj.h"
#include "algobj.h"
#include "keyobj.h"
#include "mpint.h"
#include "surrender.h"
#ifndef _DH_H
#define _DH_H
#ifdef __cplusplus
extern "C" {
#endif
typedef struct
{
unsigned int primeSizeBits;
unsigned int subprimeSizeBits;
} VoltDHParamGenCtx;
/* Implements VGenerateParameters
*/
int VOLT_CALLING_CONV DHGenerateParameters VOLT_PROTO_LIST ((
VtParameterObject paramObj,
VtRandomObject random
));
/* This function generates prime P, subprime Q, and base G following
* the instructions in the X9.42 document. The primes will be chosen
* such that subprimeQ is a divisor of (primeP - 1).
* <p>The SEED, and counter values are returned because the caller may
* be doing some FIPS work and will need them. The caller must pass a
* SEED buffer and it must be big enough to hold subprimeSizeBits. This
* function will not check the arguments, it is the responsibility of
* the caller to pass a valid SEED buffer (not NULL) that is big
* enough, along with valid pointers seedLen and counter.
* <p>The length of the subprime must be less than the length of the
* prime. The base will be the same size as the prime.
* <p>The caller can pass a surrender context if one is available (NULL
* is allowed). The surrFlag is a VT_SURRENDER_FNCT_ flag (such as
* VT_SURRENDER_FNCT_DH_PARAM_GEN) indicating who wants the PQG.
*
* @param mpCtx The mpCtx to use for multi-precision operations.
* @param surrCtx This can be NULL, but if not, the surrender ctx to
* call at various intervals.
* @param surrFlag A VT_SURRENDER_FNCT_ flag indicating what operation
* is running, to tell the surrender function who is calling it.
* @param callNumber The current callNumber of the surrender ctx. The
* caller should pass the current value at the address given, the
* function will return the number it eventually reaches at that
* address.
* @param primeSizeBits The size, in bits, of the prime to generate.
* @param subprimeSizeBits The size, in bits, of the subprime to generate.
* @param random A random object, the source of any random bytes needed.
* @param primeP Where the resulting prime will be deposited.
* @param subprimeQ Where the resulting subprime will be deposited.
* @param baseG Where the resulting base will be deposited.
* @param SEED The buffer into which the FIPS SEED value will be placed.
* @param seedLen The address where the function will deposit the
* length, in bytes, of the value deposited into the SEED buffer.
* @param counter The address where the function will deposit the FIPS
* counter value.
* @return an int, 0 if the function completed successfully or a
* non-zero error code.
*/
int VOLT_CALLING_CONV VoltGeneratePQGX942 VOLT_PROTO_LIST ((
VoltMpIntCtx *mpCtx,
VoltSurrenderCtx *surrCtx,
unsigned int surrFlag,
unsigned int *callNumber,
unsigned int primeSizeBits,
unsigned int subprimeSizeBits,
VtRandomObject random,
VoltMpInt *primeP,
VoltMpInt *subprimeQ,
VoltMpInt *baseG,
unsigned char *SEED,
unsigned int *seedLen,
unsigned int *counter
));
typedef struct
{
VtMpIntCtx mpCtx;
unsigned int primeSizeBits;
VtItem primeP;
VtItem subprimeQ;
VtItem baseG;
} VoltDHKeyGenCtx;
/* Implements VGenerateKey
*/
int VOLT_CALLING_CONV DHGenerateKeyPair VOLT_PROTO_LIST ((
VtKeyObject priKey,
VtKeyObject pubKey,
VtRandomObject random
));
#define VOLT_DH_PRI_VAL_LEN 20
/* This is part of FIPS random x and k generation.
*/
#define VOLT_DH_XKEY_LEN 32
#define VOLT_DH_XSEED_LEN 32
/* This is how params are stored internally.
*/
typedef struct
{
VoltMpInt *primeP;
VoltMpInt *subprimeQ;
VoltMpInt *baseG;
VtDHParamInfo paramInfo;
VtDHParamFipsInfo fipsInfo;
} VoltDHParams;
/* If the DH param info is given by MpInt's, call this routine to store
* the info in the object.
* <p>If the FIPS info (SEED, counter) is available, pass it in. If
* not, don't.
* <p>This function does not check the args, it is the responsibility
* of the caller to pass in a valid parameter object with an mpCtx
* inside, valid MpInts (the subprimeQ can be NULL), a valid SEED (or
* NULL with length 0), and the correct counter (or 0).
* <p>In fact, it is the responsibility of the caller to make sure the
* args match. That is, if the SEED is NULL, then the seedLen MUST be
* 0 and the counter MUST be 0. If the counter is 0, then there can be
* no SEED.
* <p>If there is a SEED and counter, there MUST be a subprime.
*/
int VOLT_CALLING_CONV VoltAddDHParametersMpInt VOLT_PROTO_LIST ((
VoltParameterObject *obj,
VoltMpInt *primeP,
VoltMpInt *subprimeQ,
VoltMpInt *baseG,
unsigned char *SEED,
unsigned int seedLen,
unsigned int counter
));
/* This is how DH key data is stored internally.
* If you build a key and store the key data, it must be this format.
* That is, if the VOLT_KEY_TYPE_DATA bit in the keyObject->keyType
* field is set, the keyData must point to this struct.
* Each of the types, public, private and key pair, must begin with the
* keyType field, a flag so we can know what the struct is. It will be
* set to one of the following values.
*
* VOLT_KEY_TYPE_PUBLIC
* VOLT_KEY_TYPE_PRIVATE
* VOLT_KEY_TYPE_PAIR
*
* The public and private key types must look alike from top to bottom,
* except for the priValX at the bottom. They can both be dereferenced
* as a public key and the keyItems and params will be in the same
* place.
*/
typedef struct
{
unsigned int type;
VtDHPriKeyInfo *keyItems;
VoltMpInt *primeP;
VoltMpInt *subprimeQ;
VoltMpInt *baseG;
VoltMpInt *pubValY;
VoltMpInt *priValX;
} VoltDHPrivateKey;
typedef struct
{
unsigned int type;
VtDHPubKeyInfo *keyItems;
VoltMpInt *primeP;
VoltMpInt *subprimeQ;
VoltMpInt *baseG;
VoltMpInt *pubValY;
} VoltDHPublicKey;
typedef struct
{
unsigned int type;
VtKeyObject pubKey;
VtKeyObject priKey;
} VoltDHKeyPair;
/* Implements VGenerateKey
*/
int VOLT_CALLING_CONV DHGenerateKeyPair VOLT_PROTO_LIST ((
VtKeyObject priKey,
VtKeyObject pubKey,
VtRandomObject random
));
/* Implements VGenerateSharedSecret
*/
int VOLT_CALLING_CONV VoltDHGenerateSharedSecret VOLT_PROTO_LIST ((
VoltAlgorithmObject *obj,
VtKeyObject otherPartyPubKey,
VtKeyObject myPriKey,
VtKeyObject sharedSecret,
VtRandomObject random
));
/* Perform Diffie-Hellman. This determines if a pub and pri key are
* indeed DH partners.
* <p>If the two are partners, this function will return 0. If the keys
* are not, the function will return an error code.
* <p>This routine assumes that the random object is valid.
*
* @param libCtx The libCtx to use.
* @param surrCtx If not NULL, pass it on to any function that takes a
* surrender ctx.
* @param pubKey The alleged public key.
* @param priKey The alleged private key.
* @param random A random object to use as a source of any random bytes
* if needed.
* @return an int, 0 if the function completed successfully or a
* non-zero error code.
*/
int VOLT_CALLING_CONV VoltTestDHKeyPair VOLT_PROTO_LIST ((
VoltLibCtx *libCtx,
VoltSurrenderCtx *surrCtx,
VtKeyObject pubKey,
VtKeyObject priKey,
VtRandomObject random
));
/* Implements VCtxDestroy.
*/
void VOLT_CALLING_CONV DHParameterDataDestroy VOLT_PROTO_LIST ((
Pointer obj,
Pointer ctx
));
/* Implements VCopyParams.
*/
int VOLT_CALLING_CONV DHCopyParams VOLT_PROTO_LIST ((
Pointer sourceParamObj,
Pointer destParamObj
));
/* Implements VCtxDestroy.
*/
void VOLT_CALLING_CONV DHKeyDataDestroy VOLT_PROTO_LIST ((
Pointer obj,
Pointer ctx
));
/* Implements VCloneObject.
*/
int VOLT_CALLING_CONV VoltCloneDHPubKey VOLT_PROTO_LIST ((
Pointer sourceObject,
Pointer *destObject
));
/* Implements VCloneObject.
*/
int VOLT_CALLING_CONV VoltCloneDHPriKey VOLT_PROTO_LIST ((
Pointer sourceObject,
Pointer *destObject
));
/* Implements VCloneObject.
*/
int VOLT_CALLING_CONV VoltCloneDHSharedSecret VOLT_PROTO_LIST ((
Pointer sourceObject,
Pointer *destObject
));
#ifdef __cplusplus
}
#endif
#endif /* _DH_H */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -