📄 bbencimpl.c
字号:
/* 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 "cipher.h"
#include "ibe.h"
#include "surrender.h"
#include "errorctx.h"
int BBType1IBEGetOutputSize (
VoltAlgorithmObject *obj,
unsigned int callFlag,
unsigned char *input,
unsigned int inputLen,
unsigned int *outputSize,
unsigned int *leftovers,
VtRandomObject random
)
{
int status;
unsigned int theSize;
VoltCipherClassCtx *cipherCtx = (VoltCipherClassCtx *)(obj->classCtx);
VoltBBType1IBECtx *ibeCtx = (VoltBBType1IBECtx *)(cipherCtx->localCipherCtx);
VOLT_DECLARE_FNCT_LINE (fnctLine)
/* Initialize result to 0. If this call works, we'll change it
* (leftovers is always 0).
*/
*outputSize = 0;
*leftovers = 0;
do
{
if (callFlag == VOLT_CALLER_ENCRYPT_FINAL)
{
VOLT_SET_FNCT_LINE (fnctLine)
theSize = bb1GetRawEncryptedSize (inputLen, ibeCtx->bbCtx);
status = VT_ERROR_INVALID_INPUT;
if (theSize == 0)
break;
/* We can now set the plain and cipher block sizes.
*/
*outputSize = (unsigned int)theSize;
cipherCtx->plainBlockSize = inputLen;
cipherCtx->cipherBlockSize = (unsigned int)theSize;
status = 0;
break;
}
/* Only EncryptFinal or DecryptFinal can make this call.
* If we reach this point, we know the caller wasn't EncryptFinal. If
* not DecryptFinal, error.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_CALL_ORDER;
if (callFlag != VOLT_CALLER_DECRYPT_FINAL)
break;
VOLT_SET_FNCT_LINE (fnctLine)
theSize = bb1GetRawDecryptedSize (inputLen, ibeCtx->bbCtx);
status = VT_ERROR_INVALID_INPUT;
if (theSize == 0)
break;
/* We can now set the plain and cipher block sizes.
*/
*outputSize = (unsigned int)theSize;
cipherCtx->plainBlockSize = (unsigned int)theSize;
cipherCtx->cipherBlockSize = inputLen;
status = 0;
} while (0);
VOLT_LOG_ERROR_COMPARE (
status, obj->voltObject.libraryCtx, status, VT_ERROR_TYPE_PRIMARY,
fnctLine, "BBType1IBEGetOutputSize", (char *)0)
return (status);
}
int BBType1IBEPubEncryptInit (
VoltAlgorithmObject *algObj,
VoltKeyObject *keyObj
)
{
int status;
VoltLibCtx *libCtx = (VoltLibCtx *)(algObj->voltObject.libraryCtx);
VoltCipherClassCtx *cipherCtx = (VoltCipherClassCtx *)(algObj->classCtx);
VoltBBType1IBECtx *encCtx = (VoltBBType1IBECtx *)(cipherCtx->localCipherCtx);
VoltBBType1IBEPubKeyData *keyData =
(VoltBBType1IBEPubKeyData *)(keyObj->keyData);
VtBBType1IBEParamInfo *paramData = (VtBBType1IBEParamInfo *)0;
VOLT_DECLARE_ERROR_TYPE (errorType)
VOLT_DECLARE_FNCT_LINE (fnctLine)
do
{
/* Make sure the key matches the algorithm object.
*/
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_KEY_OBJ;
if ((keyObj->keyType & VOLT_KEY_TYPE_MASK_ASYM_ALG) !=
VOLT_KEY_ALG_BB_TYPE_1)
break;
if ( ((keyObj->keyType & VOLT_KEY_TYPE_PRIVATE) == 0) &&
((keyObj->keyType & VOLT_KEY_TYPE_PUBLIC) == 0) )
break;
/* We need an mpCtx from the key.
*/
VOLT_SET_FNCT_LINE (fnctLine)
if (keyObj->mpCtx == (VoltMpIntCtx *)0)
break;
/* Clone the mpCtx.
*/
if (algObj->mpCtx != (VoltMpIntCtx *)0)
VtDestroyMpIntCtx ((VtMpIntCtx *)&(algObj->mpCtx));
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = VtCloneObject (
(Pointer)(keyObj->mpCtx), (Pointer *)&(algObj->mpCtx));
if (status != 0)
break;
/* We need data (status is still set to VT_ERROR_INVALID_KEY_OBJ if we
* can't get the data out).
*/
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_KEY_OBJ;
if ((keyObj->keyType & VOLT_KEY_TYPE_MASK_DATA) != VOLT_KEY_TYPE_DATA)
{
if (keyObj->GetKeyData == (VGetKeyData)0)
break;
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = keyObj->GetKeyData ((VtKeyObject)keyObj, (Pointer *)&keyData);
if (status != 0)
break;
}
/* Get the IBE params out of the parameter object in the key data.
*/
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = VtGetParameterParam
(keyData->ibeParams, VtParameterParamBBType1IBEParams,
(Pointer *)¶mData);
if (status != 0)
break;
/* Get the bfCtx in the encCtx.
*/
VoltReleaseBbCtx (libCtx, &(encCtx->bbCtx));
VOLT_SET_FNCT_LINE (fnctLine)
status = VoltGetBbCtxFromIBEParams (
libCtx, (VtMpIntCtx)(keyObj->mpCtx), paramData,
0, (unsigned char *)0, (unsigned char *)0, (unsigned char *)0,
&(encCtx->bbCtx));
if (status != 0)
break;
/* Copy the encoded identity into this object.
*/
if (encCtx->encodedId.data != (unsigned char *)0)
Z2Free (encCtx->encodedId.data);
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_MEMORY;
encCtx->encodedId.data = (unsigned char *)Z2Malloc (
keyData->encodedId.len, 0);
if (encCtx->encodedId.data == (unsigned char *)0)
break;
Z2Memcpy (
encCtx->encodedId.data, keyData->encodedId.data,
keyData->encodedId.len);
encCtx->encodedId.len = keyData->encodedId.len;
status = 0;
} while (0);
VOLT_LOG_ERROR_COMPARE (
status, (VtLibCtx)libCtx, status, errorType, fnctLine,
"BBType1IBEPubEncryptInit", (char *)0)
return (status);
}
int BBType1IBEPubEncryptUpdate (
VoltAlgorithmObject *algObj,
VtRandomObject random,
unsigned char *dataToEncrypt,
unsigned int dataToEncryptLen,
unsigned char *encryptedData
)
{
int status, count;
unsigned int callNum, outputLen;
VoltCipherClassCtx *cipherCtx = (VoltCipherClassCtx *)(algObj->classCtx);
VoltBBType1IBECtx *ibeCtx = (VoltBBType1IBECtx *)(cipherCtx->localCipherCtx);
VoltSurrenderCtx *surrCtx = (VoltSurrenderCtx *)0;
VOLT_DECLARE_FNCT_LINE (fnctLine)
/* If we reach this point in the code, the buffer is big enough.
* Call bb1RawEncrypt. If the return is not 0, try again, we may have
* gotten unlucky with a random generation. But only try so many
* times.
*/
count = 0;
do
{
/* If there's a surrender ctx, call the Surrender function.
*/
if (count == 0)
{
VOLT_GET_OBJECT_SURR_CTX (surrCtx, algObj);
VOLT_CALL_SURRENDER (
surrCtx, VT_SURRENDER_FNCT_BB_TYPE1_IBE_ENCRYPT, 0, 1)
}
/* If the call to Encrypt is successful, we're done, break out.
*/
VOLT_SET_FNCT_LINE (fnctLine)
callNum = 2;
status = (unsigned int)bb1RawEncrypt (
encryptedData, cipherCtx->cipherBlockSize, &outputLen,
ibeCtx->encodedId.data, ibeCtx->encodedId.len,
dataToEncrypt, dataToEncryptLen, surrCtx,
VT_SURRENDER_FNCT_BB_TYPE1_IBE_ENCRYPT, &callNum, random, ibeCtx->bbCtx);
if (status == 0)
{
/* If there's a surrender ctx, call it for the last time.
*/
VOLT_CALL_SURRENDER (
surrCtx, VT_SURRENDER_FNCT_BB_TYPE1_IBE_ENCRYPT, 0, 0)
break;
}
if ( (count >= 10) || (status == VT_ERROR_RANDOM_OBJECT) ||
(status == VT_ERROR_BUFFER_TOO_SMALL) )
break;
count++;
} while (1);
VOLT_LOG_ERROR_COMPARE (
status, algObj->voltObject.libraryCtx, status, 0, fnctLine,
"BBType1IBEPubEncryptUpdate", (char *)0)
return (status);
}
int BBType1IBEPriDecryptInit (
VoltAlgorithmObject *algObj,
VoltKeyObject *keyObj
)
{
int status;
unsigned int primeLen2, index, bufferSize, offset;
unsigned char *bufD0, *bufD1;
VoltLibCtx *libCtx = (VoltLibCtx *)(algObj->voltObject.libraryCtx);
VoltCipherClassCtx *cipherCtx = (VoltCipherClassCtx *)(algObj->classCtx);
VoltBBType1IBECtx *decCtx = (VoltBBType1IBECtx *)(cipherCtx->localCipherCtx);
VoltIBEPriKeyData *keyData = (VoltIBEPriKeyData *)(keyObj->keyData);
VtBBType1IBEParamInfo *paramData = (VtBBType1IBEParamInfo *)0;
VOLT_DECLARE_ERROR_TYPE (errorType)
VOLT_DECLARE_FNCT_LINE (fnctLine)
do
{
/* Make sure the key matches the algorithm object.
*/
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_KEY_OBJ;
if ((keyObj->keyType & VOLT_KEY_TYPE_MASK_ASYM_ALG) !=
VOLT_KEY_ALG_BB_TYPE_1)
break;
if ((keyObj->keyType & VOLT_KEY_TYPE_PRIVATE) == 0)
break;
/* We need an mpCtx from the key.
*/
VOLT_SET_FNCT_LINE (fnctLine)
if (keyObj->mpCtx == (VoltMpIntCtx *)0)
break;
/* Clone the mpCtx.
*/
if (algObj->mpCtx != (VoltMpIntCtx *)0)
VtDestroyMpIntCtx ((VtMpIntCtx *)&(algObj->mpCtx));
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = VtCloneObject (
(Pointer)(keyObj->mpCtx), (Pointer *)&(algObj->mpCtx));
if (status != 0)
break;
/* We need data (status is still set to VT_ERROR_INVALID_KEY_OBJ if we
* can't get the data out).
*/
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
if ((keyObj->keyType & VOLT_KEY_TYPE_MASK_DATA) != VOLT_KEY_TYPE_DATA)
{
if (keyObj->GetKeyData == (VGetKeyData)0)
break;
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = keyObj->GetKeyData ((VtKeyObject)keyObj, (Pointer *)&keyData);
if (status != 0)
break;
}
/* Get the IBE params out of the parameter object in the key data.
*/
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = VtGetParameterParam (
keyData->bbKeyInfo.ibeParams, VtParameterParamBBType1IBEParams,
(Pointer *)¶mData);
if (status != 0)
break;
/* Build the bbCtx in the decCtx.
*/
VoltReleaseBbCtx (libCtx, &(decCtx->bbCtx));
VOLT_SET_FNCT_LINE (fnctLine)
status = VoltGetBbCtxFromIBEParams (
libCtx, (VtMpIntCtx)(keyObj->mpCtx), paramData,
0, (unsigned char *)0, (unsigned char *)0, (unsigned char *)0,
&(decCtx->bbCtx));
if (status != 0)
break;
/* Build the private value.
*/
if (decCtx->privateValue.data != (unsigned char *)0)
Z2Free (decCtx->privateValue.data);
primeLen2 = paramData->primeP.len * 2;
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_MEMORY;
bufferSize = (2 * primeLen2) + 2;
decCtx->privateValue.data = (unsigned char *)Z2Malloc (
bufferSize, VOLT_MEMORY_SENSITIVE);
if (decCtx->privateValue.data == (unsigned char *)0)
break;
Z2Memset (decCtx->privateValue.data, 0, bufferSize);
/* Build the xCoords as a compressed point.
*/
bufD0 = decCtx->privateValue.data + primeLen2;
bufD1 = bufD0 + paramData->primeP.len + 1;
offset =
paramData->primeP.len - keyData->bbKeyInfo.privatePointD0.xCoord.len;
index = keyData->bbKeyInfo.privatePointD0.yCoord.len - 1;
bufD0[0] = keyData->bbKeyInfo.privatePointD0.yCoord.data[index];
bufD0[0] &= 1;
bufD0[0] += 2;
Z2Memcpy (
bufD0 + offset + 1, keyData->bbKeyInfo.privatePointD0.xCoord.data,
keyData->bbKeyInfo.privatePointD0.xCoord.len);
offset =
paramData->primeP.len - keyData->bbKeyInfo.privatePointD1.xCoord.len;
index = keyData->bbKeyInfo.privatePointD1.yCoord.len - 1;
bufD1[0] = keyData->bbKeyInfo.privatePointD1.yCoord.data[index];
bufD1[0] &= 1;
bufD1[0] += 2;
Z2Memcpy (
bufD1 + offset + 1, keyData->bbKeyInfo.privatePointD1.xCoord.data,
keyData->bbKeyInfo.privatePointD1.xCoord.len);
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = bb1PrivateKeyImportFromPoints (
decCtx->privateValue.data, primeLen2, &(decCtx->privateValue.len),
bufD0, bufD1, (int)(paramData->primeP.len + 1), decCtx->bbCtx);
} while (0);
VOLT_LOG_ERROR_COMPARE (
status, (VtLibCtx)libCtx, status, errorType, fnctLine,
"BBType1IBEPriDecryptInit", (char *)0)
return (status);
}
int BBType1IBEPriDecryptUpdate (
VoltAlgorithmObject *algObj,
VtRandomObject random,
unsigned char *dataToDecrypt,
unsigned int dataToDecryptLen,
unsigned char *decryptedData
)
{
int status;
unsigned int callNum, outputLen;
VoltCipherClassCtx *cipherCtx = (VoltCipherClassCtx *)(algObj->classCtx);
VoltBBType1IBECtx *ibeCtx = (VoltBBType1IBECtx *)(cipherCtx->localCipherCtx);
VoltSurrenderCtx *surrCtx = (VoltSurrenderCtx *)0;
VOLT_DECLARE_FNCT_LINE (fnctLine)
do
{
/* If there's a surrender ctx, call the Surrender function.
*/
VOLT_GET_OBJECT_SURR_CTX (surrCtx, algObj);
VOLT_CALL_SURRENDER (
surrCtx, VT_SURRENDER_FNCT_BB_TYPE1_IBE_DECRYPT, 0, 1)
/* If we reach this point in the code, the buffer is big enough.
*/
VOLT_SET_FNCT_LINE (fnctLine)
callNum = 1;
status = bb1RawDecrypt (
decryptedData, cipherCtx->plainBlockSize, &outputLen,
ibeCtx->privateValue.data, ibeCtx->privateValue.len,
dataToDecrypt, dataToDecryptLen, surrCtx,
VT_SURRENDER_FNCT_BB_TYPE1_IBE_DECRYPT, &callNum, ibeCtx->bbCtx);
/* If there's a surrender ctx, call it for the last time.
*/
if (status == 0)
{
VOLT_CALL_SURRENDER (
surrCtx, VT_SURRENDER_FNCT_BB_TYPE1_IBE_DECRYPT, 0, 0)
}
} while (0);
VOLT_LOG_ERROR_COMPARE (
status, algObj->voltObject.libraryCtx, status, 0, fnctLine,
"BBType1IBEPriDecryptUpdate", (char *)0)
return (status);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -