📄 ibeparamber.c
字号:
/* Copyright 2003-2005, Voltage Security, all rights reserved.
*/
#include "vibe.h"
#include "environment.h"
#include "base.h"
#include "libctx.h"
#include "paramobj.h"
#include "oidlist.h"
#include "algid.h"
#include "ecparamsder.h"
#include "ibe.h"
/* The IBE Parameter Data (IBEPublicParameters).
*/
typedef struct
{
ASN1_INTEGER *version;
Asn1ObjectId *oid;
Asn1EcPoint *pubPoint;
Asn1EcParameters *ecParams;
} Asn1IBEParams;
DECLARE_ASN1_FUNCTIONS (Asn1IBEParams)
/* Set up the OpenSSL ASN.1 templates.
*/
ASN1_SEQUENCE (Asn1IBEParams) =
{
ASN1_OPT (Asn1IBEParams, version, ASN1_INTEGER),
ASN1_SIMPLE (Asn1IBEParams, oid, Asn1ObjectId),
ASN1_SIMPLE (Asn1IBEParams, pubPoint, Asn1EcPoint),
ASN1_SIMPLE (Asn1IBEParams, ecParams, Asn1EcParameters)
} ASN1_SEQUENCE_END (Asn1IBEParams);
IMPLEMENT_ASN1_FUNCTIONS (Asn1IBEParams)
/* Encode the IBE params.
* <p>This function will place the encoding into the paramsDer field of
* the VoltParameterObject. That is, upon return (if no error), find the
* encoding in the paramsDer VtItem.
*
* @param obj The object with the param data to encode.
* @param paramData The param data (if hardware params, the data might
* not be in the object).
* @return an int, 0 if the function completed successfully or a
* non-zero error code (e.g., key and object don't match).
*/
static int VOLT_CALLING_CONV EncodeIBEParams VOLT_PROTO_LIST ((
VoltParameterObject *obj,
VoltBFType1IBEParams *params
));
/* Decode a IBE params. This function creates a new Asn1IBEParams
* "object", it is the responsibility of the caller to destroy it (by
* calling Asn1IBEParams_free).
*
* @param libCtx The library context to use.
* @param encoding The buffer containing the alleged IBE params.
* @param maxEncodingLen The max number of bytes to decode.
* @param ibeParams The address where this function will deposit the
* created Asn1IBEParams "object".
* @return an int, 0 if the function completed successfully or a
* non-zero error code.
*/
static int VOLT_CALLING_CONV DecodeIBEParamsCreate VOLT_PROTO_LIST ((
VoltLibCtx *libCtx,
unsigned char *encoding,
unsigned int maxEncodingLen,
Asn1IBEParams **ibeParams
));
int VtDerCoderBFType1IBEParams (
VtDerCoderInfo *coderInfo,
Pointer object,
unsigned int flag
)
{
int status;
unsigned int bufferSize;
VoltLibCtx *libCtx;
VoltParameterObject *obj = (VoltParameterObject *)object;
VoltDerCoderEncodeData *encodeData = &(coderInfo->info.encodeData);
VoltDerCoderGetAlgData *getAlgData = &(coderInfo->info.getAlgData);
VoltDerCoderDecodeData *decodeData = &(coderInfo->info.decodeData);
VoltBFType1IBEParams *paramData;
VtSetParamBerInfo *setBerInfo;
Asn1IBEParams *ibeParams = (Asn1IBEParams *)0;
VtBFType1IBEParamInfo paramInfo;
unsigned char curveTypeOid[VoltBFType1IBECurveOidBytesLen] =
{ VoltBFType1IBECurveOidBytes };
switch (flag)
{
default:
status = VT_ERROR_INVALID_TYPE;
break;
case VOLT_DER_TYPE_ENCODE_FLAG:
/* If the flag is ENCODE, return the IBEPublicParameters.
*/
/* Check the args.
*/
status = VT_ERROR_NON_NULL_ARG;
if (object != (Pointer)0)
break;
/* We need a place to drop the length.
*/
status = VT_ERROR_NULL_ARG;
if (encodeData->encodingLen == (unsigned int *)0)
break;
/* The info should be a param object.
*/
status = VT_ERROR_NULL_ARG;
if (encodeData->info == (Pointer)0)
break;
status = VT_ERROR_INVALID_PARAM_OBJ;
obj = (VoltParameterObject *)(encodeData->info);
if (VOLT_OBJECT_TYPE_NOT_EQUAL (obj, VOLT_OBJECT_TYPE_PARAMETER))
break;
if ((obj->paramType & VOLT_PARAM_TYPE_MASK_ALG) !=
VOLT_PARAM_ALG_IBE_TYPE_1)
break;
/* We need the param data.
*/
paramData = (VoltBFType1IBEParams *)(obj->paramData);
if ((obj->paramType & VOLT_PARAM_TYPE_MASK_DATA) != VOLT_PARAM_TYPE_DATA)
{
if (obj->GetParamData == (VGetParamData)0)
break;
status = obj->GetParamData (
(VtParameterObject)obj, (Pointer *)¶mData);
if (status != 0)
break;
}
bufferSize = encodeData->bufferSize;
if (encodeData->encoding == (unsigned char *)0)
bufferSize = 0;
/* Build the params.
*/
status = EncodeIBEParams (obj, paramData);
if (status != 0)
break;
/* Is the buffer big enough?
*/
status = VT_ERROR_BUFFER_TOO_SMALL;
*(encodeData->encodingLen) = obj->paramsDer.len;
if (bufferSize < obj->paramsDer.len)
break;
libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
Z2Memcpy (encodeData->encoding, obj->paramsDer.data, obj->paramsDer.len);
status = 0;
break;
case VOLT_DER_TYPE_DECODE_FLAG:
/* If the flag is decode, set the param object with the param
* info.
*/
/* Check the args, the type should be VOLT_DER_TYPE_PARAM_FLAG.
*/
status = VT_ERROR_UNKNOWN_BER;
if (decodeData->type != VOLT_DER_TYPE_PARAM_FLAG)
break;
setBerInfo = (VtSetParamBerInfo *)(decodeData->info);
libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
status = DecodeIBEParamsCreate (
libCtx, setBerInfo->berEncoding, setBerInfo->maxEncodingLen,
&ibeParams);
if (status != 0)
break;
paramInfo.curve.primeP.data = ibeParams->ecParams->fieldId->prime->data;
paramInfo.curve.primeP.len = ibeParams->ecParams->fieldId->prime->length;
paramInfo.curve.subprimeQ.data = ibeParams->ecParams->subprime->data;
paramInfo.curve.subprimeQ.len = ibeParams->ecParams->subprime->length;
paramInfo.curve.basePointG.isInfinity = 0;
paramInfo.curve.basePointG.xCoord.data =
ibeParams->ecParams->basePoint->xCoord.data;
paramInfo.curve.basePointG.xCoord.len =
ibeParams->ecParams->basePoint->xCoord.len;
paramInfo.curve.basePointG.yCoord.data =
ibeParams->ecParams->basePoint->yCoord.data;
paramInfo.curve.basePointG.yCoord.len =
ibeParams->ecParams->basePoint->yCoord.len;
paramInfo.pubPointP.isInfinity = 0;
paramInfo.pubPointP.xCoord.data = ibeParams->pubPoint->xCoord.data;
paramInfo.pubPointP.xCoord.len = ibeParams->pubPoint->xCoord.len;
paramInfo.pubPointP.yCoord.data = ibeParams->pubPoint->yCoord.data;
paramInfo.pubPointP.yCoord.len = ibeParams->pubPoint->yCoord.len;
status = VtSetParameterParam (
(VtParameterObject)obj, VtParameterParamBFType1IBEParams,
(Pointer)¶mInfo);
break;
case VOLT_DER_TYPE_GET_ALG_FLAG:
/* If the flag is get alg, check the input to see if it's the
* IBE encryption OID.
*/
/* Check the args.
*/
status = VT_ERROR_NON_NULL_ARG;
if (object != (Pointer)0)
break;
status = VT_ERROR_NULL_ARG;
if ( (getAlgData->algorithm == (unsigned int *)0) ||
(getAlgData->oid == (unsigned char *)0) )
break;
status = VT_ERROR_UNKNOWN_BER;
if (getAlgData->oidLen != VoltBFType1IBECurveOidBytesLen)
break;
libCtx = getAlgData->libCtx;
if (Z2Memcmp (
getAlgData->oid, curveTypeOid, VoltBFType1IBECurveOidBytesLen) != 0 )
break;
/* The OID matches, the algorithm is IBE encryption.
*/
*(getAlgData->algorithm) = VT_OID_BF_TYPE1_IBE_PARAMS;
getAlgData->SymKeyParam = (VtKeyParam *)0;
getAlgData->DigestImpl = (VtAlgorithmImpl *)0;
status = 0;
}
return (status);
}
static int EncodeIBEParams (
VoltParameterObject *obj,
VoltBFType1IBEParams *params
)
{
int status;
unsigned int primeLen, encodingLen;
VoltLibCtx *libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
Asn1IBEParams *ibeParams =(Asn1IBEParams *)0;
unsigned char *buffer = (unsigned char *)0;
unsigned char *temp;
unsigned char curveTypeOid[VoltBFType1IBECurveOidBytesLen] =
{ VoltBFType1IBECurveOidBytes };
unsigned char primeFieldOid[VoltPrimeFieldOidBytesLen] =
{ VoltPrimeFieldOidBytes };
/* Is the encoding already in the object?
*/
if (obj->paramsDer.data != (unsigned char *)0)
return (0);
do
{
primeLen = params->paramInfo.curve.primeP.len;
/* Build the Asn1 "object".
*/
status = VT_ERROR_MEMORY;
ibeParams = Asn1IBEParams_new ();
if (ibeParams == (Asn1IBEParams *)0)
break;
/* Set the fields. Don't set the version in the ibeParams, it's
* DEFAULT. Set the version in the ECParameters, though.
*/
if (Asn1ObjectId_set (
ibeParams->oid, curveTypeOid, VoltBFType1IBECurveOidBytesLen) != 1)
break;
if (ASN1_INTEGER_set (
ibeParams->ecParams->version, 1) != 1)
break;
if (Asn1ObjectId_set (
ibeParams->ecParams->fieldId->oid, primeFieldOid,
VoltPrimeFieldOidBytesLen) != 1)
break;
if (ASN1_STRING_set (
ibeParams->ecParams->fieldId->prime,
params->paramInfo.curve.primeP.data, primeLen) != 1)
break;
/* Set coeffA = 0 and coeffB = 1. They are OCTET STRING's the same
* size as the prime.
*/
buffer = (unsigned char *)Z2Malloc (primeLen, 0);
if (buffer == (unsigned char *)0)
break;
Z2Memset (buffer, 0, primeLen);
if (ASN1_OCTET_STRING_set (
ibeParams->ecParams->curve->coeffA, buffer, primeLen) != 1)
break;
buffer[primeLen - 1] = 1;
if (ASN1_OCTET_STRING_set (
ibeParams->ecParams->curve->coeffB, buffer, primeLen) != 1)
break;
if (Asn1EcPoint_set (
ibeParams->ecParams->basePoint, primeLen,
params->paramInfo.curve.basePointG.xCoord.data,
params->paramInfo.curve.basePointG.yCoord.len,
params->paramInfo.curve.basePointG.yCoord.data,
params->paramInfo.curve.basePointG.yCoord.len) != 1)
break;
if (ASN1_STRING_set (
ibeParams->ecParams->subprime,
params->paramInfo.curve.subprimeQ.data,
params->paramInfo.curve.subprimeQ.len) != 1)
break;
if (Asn1EcPoint_set (
ibeParams->pubPoint, primeLen,
params->paramInfo.pubPointP.xCoord.data,
params->paramInfo.pubPointP.xCoord.len,
params->paramInfo.pubPointP.yCoord.data,
params->paramInfo.pubPointP.yCoord.len) != 1)
break;
/* How big does the buffer need to be?
*/
status = VT_ERROR_INVALID_INPUT;
encodingLen = i2d_Asn1IBEParams (ibeParams, (unsigned char **)0);
if (encodingLen == 0)
break;
status = VT_ERROR_MEMORY;
Z2Free (buffer);
buffer = (unsigned char *)Z2Malloc (encodingLen, 0);
if (buffer == (unsigned char *)0)
break;
/* Now encode into the buffer.
*/
status = VT_ERROR_INVALID_INPUT;
temp = buffer;
encodingLen = i2d_Asn1IBEParams (ibeParams, &temp);
if (encodingLen == 0)
break;
/* If everything worked, store the encoding in the object.
*/
obj->paramsDer.data = buffer;
obj->paramsDer.len = encodingLen;
status = 0;
} while (0);
if (ibeParams != (Asn1IBEParams *)0)
Asn1IBEParams_free (ibeParams);
/* If successful, we're done.
*/
if (status == 0)
return (0);
/* If there was an error, free the buffer.
*/
if (buffer != (unsigned char *)0)
Z2Free (buffer);
return (status);
}
static int DecodeIBEParamsCreate (
VoltLibCtx *libCtx,
unsigned char *encoding,
unsigned int maxEncodingLen,
Asn1IBEParams **ibeParams
)
{
int status;
unsigned char *temp;
Asn1IBEParams *newIBEParams = (Asn1IBEParams *)0;
unsigned char curveTypeOid[VoltBFType1IBECurveOidBytesLen] =
{ VoltBFType1IBECurveOidBytes };
do
{
/* Create the object.
*/
status = VT_ERROR_MEMORY;
newIBEParams = Asn1IBEParams_new ();
if (newIBEParams == (Asn1IBEParams *)0)
break;
/* Decode.
*/
status = VT_ERROR_UNKNOWN_BER;
temp = encoding;
d2i_Asn1IBEParams (&newIBEParams, &temp, maxEncodingLen);
/* Did it work?
*/
if (newIBEParams == (Asn1IBEParams *)0)
break;
/* Check the OID, expected?
*/
status = VT_ERROR_UNKNOWN_BER;
if (newIBEParams->oid->base.length != VoltBFType1IBECurveOidBytesLen)
break;
if (Z2Memcmp (
newIBEParams->oid->base.data, curveTypeOid,
VoltBFType1IBECurveOidBytesLen) != 0)
break;
/* If successful, return the object.
*/
*ibeParams = newIBEParams;
status = 0;
} while (0);
/* If no error, we're done.
*/
if (status == 0)
return (0);
/* If error, destroy what we created.
*/
if (newIBEParams != (Asn1IBEParams *)0)
Asn1IBEParams_free (newIBEParams);
return (status);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -