📄 rsaencimpl.c
字号:
encryptedData += blockSize;
}
VOLT_CALL_SURRENDER (surrCtx, VT_SURRENDER_FNCT_RSA_ENCRYPT, 3, 3)
} while (0);
if (base != (VoltMpInt *)0)
mpCtx->DestroyMpInt (&base);
if (result != (VoltMpInt *)0)
mpCtx->DestroyMpInt (&result);
VOLT_LOG_ERROR_INFO_COMPARE (
status, 0, algObj, status, 0, 0,
(char *)0, "RSAEncryptUpdate", fnctLine, (char *)0)
return (status);
}
int RSAWrapKey (
VoltAlgorithmObject *algObj,
VtRandomObject random,
VoltKeyObject *keyToWrap,
unsigned char *encryptedKey,
unsigned int bufferSize,
unsigned int *encryptedKeyLen
)
{
return (VT_ERROR_UNIMPLEMENTED);
}
int RSADecryptInit (
VoltAlgorithmObject *algObj,
VoltKeyObject *keyObj
)
{
int status;
unsigned int sign, blockSize;
VoltCipherClassCtx *cipherCtx = (VoltCipherClassCtx *)(algObj->classCtx);
VoltLibCtx *libCtx = (VoltLibCtx *)(algObj->voltObject.libraryCtx);
VtRSAPriKeyInfo *getKeyInfo;
VoltRsaPrivateKey *newData;
VoltMpIntCtx *mpCtx;
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_RSA)
break;
/* Only a private key is allowed to decrypt.
*/
VOLT_SET_FNCT_LINE (fnctLine)
if ((keyObj->keyType & VOLT_KEY_TYPE_PRIVATE) == 0)
break;
VOLT_SET_FNCT_LINE (fnctLine)
if (keyObj->mpCtx == (VoltMpIntCtx *)0)
break;
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = VtGetKeyParam (
(VtKeyObject)keyObj, VtKeyParamRSAPrivateDecrypt, (Pointer *)&getKeyInfo);
if (status != 0)
break;
/* If there's a localCipherCtx, get rid of it.
*/
if ( (cipherCtx->localCipherCtx != (Pointer)0) &&
(cipherCtx->LocalCipherCtxDestroy != (VCtxDestroy)0) )
cipherCtx->LocalCipherCtxDestroy (
(Pointer)algObj, cipherCtx->localCipherCtx);
cipherCtx->localCipherCtx = (Pointer)0;
cipherCtx->LocalCipherCtxDestroy = (VCtxDestroy)0;
/* Clone the mpCtx.
*/
if (algObj->mpCtx != (VoltMpIntCtx *)0)
VtDestroyMpIntCtx ((VtMpIntCtx *)&(algObj->mpCtx));
VOLT_SET_FNCT_LINE (fnctLine)
status = VtCloneObject (
(Pointer)(keyObj->mpCtx), (Pointer *)&(algObj->mpCtx));
if (status != 0)
break;
mpCtx = algObj->mpCtx;
/* Clear the cipherCtx for a new Init.
*/
cipherCtx->unprocessedDataLen = 0;
cipherCtx->plainBlockSize = 0;
cipherCtx->cipherBlockSize = 0;
/* Copy the key data over to the cipherCtx.
* First, we need the struct that holds the elements.
*/
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_MEMORY;
cipherCtx->localCipherCtx = Z2Malloc (sizeof (VoltRsaPrivateKey), 0);
if (cipherCtx->localCipherCtx == (Pointer)0)
break;
Z2Memset (cipherCtx->localCipherCtx, 0, sizeof (VoltRsaPrivateKey));
cipherCtx->LocalCipherCtxDestroy = RSAKeyDataDestroy;
newData = (VoltRsaPrivateKey *)(cipherCtx->localCipherCtx);
newData->type = VOLT_KEY_TYPE_PRIVATE;
VOLT_SET_ERROR_TYPE (errorType, 0)
/* Create and copy CRT mpInts from the key, if they exist.
* If they don't, copy the modulus and private exponent.
*/
if (getKeyInfo->prime1.data != (unsigned char *)0)
{
VOLT_SET_FNCT_LINE (fnctLine)
status = mpCtx->CreateMpInt ((Pointer)mpCtx, &(newData->prime1));
if (status != 0)
break;
VOLT_SET_FNCT_LINE (fnctLine)
status = mpCtx->OctetStringToMpInt (
0, getKeyInfo->prime1.data, getKeyInfo->prime1.len, newData->prime1);
if (status != 0)
break;
VOLT_SET_FNCT_LINE (fnctLine)
status = mpCtx->CreateMpInt ((Pointer)mpCtx, &(newData->prime2));
if (status != 0)
break;
VOLT_SET_FNCT_LINE (fnctLine)
status = mpCtx->OctetStringToMpInt (
0, getKeyInfo->prime2.data, getKeyInfo->prime2.len, newData->prime2);
if (status != 0)
break;
VOLT_SET_FNCT_LINE (fnctLine)
status = mpCtx->CreateMpInt ((Pointer)mpCtx, &(newData->expo1));
if (status != 0)
break;
VOLT_SET_FNCT_LINE (fnctLine)
status = mpCtx->OctetStringToMpInt (
0, getKeyInfo->exponent1.data, getKeyInfo->exponent1.len,
newData->expo1);
if (status != 0)
break;
VOLT_SET_FNCT_LINE (fnctLine)
status = mpCtx->CreateMpInt ((Pointer)mpCtx, &(newData->expo2));
if (status != 0)
break;
VOLT_SET_FNCT_LINE (fnctLine)
status = mpCtx->OctetStringToMpInt (
0, getKeyInfo->exponent2.data, getKeyInfo->exponent2.len,
newData->expo2);
if (status != 0)
break;
VOLT_SET_FNCT_LINE (fnctLine)
status = mpCtx->CreateMpInt ((Pointer)mpCtx, &(newData->coeff));
if (status != 0)
break;
VOLT_SET_FNCT_LINE (fnctLine)
status = mpCtx->OctetStringToMpInt (
0, getKeyInfo->coefficient.data, getKeyInfo->coefficient.len,
newData->coeff);
if (status != 0)
break;
/* Get the block size. Use sign as a temp variable.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = mpCtx->GetBitLength (newData->prime1, &sign);
if (status != 0)
break;
VOLT_SET_FNCT_LINE (fnctLine)
status = mpCtx->GetBitLength (newData->prime2, &blockSize);
if (status != 0)
break;
blockSize += sign;
}
else
{
VOLT_SET_FNCT_LINE (fnctLine)
status = mpCtx->CreateMpInt ((Pointer)mpCtx, &(newData->modulus));
if (status != 0)
break;
VOLT_SET_FNCT_LINE (fnctLine)
status = mpCtx->OctetStringToMpInt (
0, getKeyInfo->modulus.data, getKeyInfo->modulus.len,
newData->modulus);
if (status != 0)
break;
VOLT_SET_FNCT_LINE (fnctLine)
status = mpCtx->CreateMpInt ((Pointer)mpCtx, &(newData->priExpo));
if (status != 0)
break;
VOLT_SET_FNCT_LINE (fnctLine)
status = mpCtx->OctetStringToMpInt (
0, getKeyInfo->priExpo.data, getKeyInfo->priExpo.len,
newData->priExpo);
if (status != 0)
break;
/* Get the block size. We're expecting BUFFER_TOO_SMALL.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = mpCtx->GetBitLength (newData->modulus, &blockSize);
if (status != 0)
break;
}
blockSize = (blockSize + 7) / 8;
cipherCtx->plainBlockSize = blockSize;
cipherCtx->cipherBlockSize = blockSize;
/* We need the unprocessedData buffer to be blockSize bytes big.
*/
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_MEMORY;
cipherCtx->unprocessedData = (unsigned char *)Z2Realloc (
cipherCtx->unprocessedData, blockSize);
if (cipherCtx->unprocessedData == (unsigned char *)0)
break;
status = 0;
} while (0);
VOLT_LOG_ERROR_INFO_COMPARE (
status, 0, algObj, status, 0, errorType,
(char *)0, "RSADecryptInit", fnctLine, (char *)0)
return (status);
}
int RSADecryptUpdate (
VoltAlgorithmObject *algObj,
VtRandomObject random,
unsigned char *dataToDecrypt,
unsigned int dataToDecryptLen,
unsigned char *decryptedData
)
{
int status;
unsigned int blockSize, outputLen, sign;
VoltCipherClassCtx *cipherCtx = (VoltCipherClassCtx *)(algObj->classCtx);
VoltLibCtx *libCtx = (VoltLibCtx *)(algObj->voltObject.libraryCtx);
VoltRsaPrivateKey *keyData =
(VoltRsaPrivateKey *)(cipherCtx->localCipherCtx);
VoltMpIntCtx *mpCtx;
VoltMpInt *base = (VoltMpInt *)0;
VoltMpInt *result = (VoltMpInt *)0;
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_RSA_DECRYPT, 3, 1)
if (keyData->prime1 != (VoltMpInt *)0)
mpCtx = keyData->prime1->mpCtx;
else
mpCtx = keyData->modulus->mpCtx;
blockSize = cipherCtx->cipherBlockSize;
/* Create mpInts to hold the data to decrypt and the decrypted data.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = mpCtx->CreateMpInt ((Pointer)mpCtx, &base);
if (status != 0)
break;
VOLT_SET_FNCT_LINE (fnctLine)
status = mpCtx->CreateMpInt ((Pointer)mpCtx, &result);
if (status != 0)
break;
while (dataToDecryptLen >= blockSize)
{
/* Get the block of data as an mpInt.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = mpCtx->OctetStringToMpInt (0, dataToDecrypt, blockSize, base);
if (status != 0)
break;
/* Decryption is modExp or modExpCRT.
*/
if (keyData->prime1 != (VoltMpInt *)0)
{
VOLT_CALL_SURRENDER (surrCtx, VT_SURRENDER_FNCT_RSA_DECRYPT, 3, 2)
VOLT_SET_FNCT_LINE (fnctLine)
status = mpCtx->ModExpCRT (
base, keyData->prime1, keyData->prime2,
keyData->expo1, keyData->expo2, keyData->coeff, result);
if (status != 0)
break;
}
else
{
VOLT_CALL_SURRENDER (surrCtx, VT_SURRENDER_FNCT_RSA_DECRYPT, 3, 2)
VOLT_SET_FNCT_LINE (fnctLine)
status = mpCtx->ModExp (
base, keyData->priExpo, keyData->modulus, result);
if (status != 0)
break;
}
/* Place the result as an octet string into the output buffer.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = mpCtx->MpIntToOctetString (
result, &sign, decryptedData, blockSize, &outputLen);
if (status != 0)
break;
/* If the outputLen < blockSize, we want to prepend 00 bytes.
*/
if (outputLen < blockSize)
{
/* Use sign as a temp variable.
*/
sign = blockSize - outputLen;
Z2Memmove (decryptedData + sign, decryptedData, outputLen);
Z2Memset (decryptedData, 0, sign);
}
dataToDecryptLen -= blockSize;
dataToDecrypt += blockSize;
decryptedData += blockSize;
}
VOLT_CALL_SURRENDER (surrCtx, VT_SURRENDER_FNCT_RSA_DECRYPT, 3, 3)
} while (0);
if (base != (VoltMpInt *)0)
mpCtx->DestroyMpInt (&base);
if (result != (VoltMpInt *)0)
mpCtx->DestroyMpInt (&result);
VOLT_LOG_ERROR_INFO_COMPARE (
status, 0, algObj, status, 0, 0,
(char *)0, "RSADecryptUpdate", fnctLine, (char *)0)
return (status);
}
int RSAUnwrapKey (
VoltAlgorithmObject *algObj,
VtRandomObject random,
unsigned char *encryptedKey,
unsigned int encryptedKeyLen,
VtKeyParam KeyParam,
VtKeyObject unwrappedKey
)
{
return (VT_ERROR_UNIMPLEMENTED);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -