rsa.c:
/* Includes ------------------------------------------------------------------*/
#include "rsa.h"
/** @addtogroup STM32_Crypto_Examples
* @{
*/
/* Private typedef -----------------------------------------------------------*/
typedef enum
{
FAILED = 0, PASSED = !FAILED
} TestStatus;
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
uint8_t preallocated_buffer[4096]; /* buffer required for internal allocation of memory */
/******************************************************************************/
/************************** RSA 2048 Test Vector ****************************/
/******************************************************************************/
const uint8_t Message[] =
{ 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33,
0x34, 0x35, 0x36, 0x00};
const uint8_t Modulus[] =
{ 0xB5, 0x05, 0xFC, 0xA2, 0xCA, 0x33, 0x48, 0xD5, 0x9B, 0xF3, 0x00, 0x5F, 0x7C,
0xFD, 0xC4, 0x56, 0x4C, 0x25, 0x07, 0x67, 0xE9, 0xC9, 0x40, 0x24, 0x69,
0x79, 0x61, 0x41, 0x98, 0x1D, 0x6A, 0xF5, 0x6A, 0x1A, 0x84, 0xB5, 0xA9,
0xA4, 0xB3, 0x33, 0x5F, 0xA0, 0x25, 0xA8, 0x7F, 0x4B, 0x4D, 0x0B, 0xA0,
0x60, 0xB8, 0xBE, 0xF9, 0x34, 0x0B, 0xE4, 0x5F, 0xDB, 0x05, 0x76, 0x20,
0x38, 0x90, 0xA0, 0x71, 0xCE, 0xE9, 0xB0, 0x59, 0x4B, 0x95, 0x12, 0x7B,
0xB4, 0x80, 0xED, 0xC7, 0x43, 0xBD, 0xCE, 0x27, 0xFD, 0x2B, 0xEC, 0xD0,
0x33, 0x00, 0x24, 0x32, 0x9E, 0xED, 0xAF, 0x3C, 0x1A, 0x12, 0x13, 0xB2,
0x8D, 0x32, 0xD1, 0x83, 0xEA, 0xF4, 0x1A, 0x9A, 0x46, 0x3A, 0x08, 0x8C,
0xD4, 0xBA, 0x67, 0xDA, 0x91, 0x26, 0x79, 0x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, 0x81, 0xEF, 0xB4, 0xD3, 0x5E, 0x72, 0xC8,
0x89, 0x64, 0xBE, 0x54, 0xEC, 0xFB, 0x38, 0x85, 0xB4, 0x62, 0x39, 0xA6,
0xCC, 0xC4, 0x68, 0x0C, 0xDF, 0xA4, 0x5A, 0x9D, 0x34, 0x31, 0x2A, 0x0C,
0x3B, 0x52, 0xCF, 0x13, 0xF3, 0xE8, 0x5A, 0x0C, 0xEA, 0x41, 0x94, 0xD5,
0x25, 0xAA, 0xC0, 0x2B, 0xC8, 0xB2, 0x04, 0xA6, 0xCD, 0x26, 0xF6, 0x02,
0x98, 0x89, 0x79, 0x62, 0x76, 0x76, 0xEF, 0xF4, 0x3C, 0x09, 0x16, 0x4B,
0x1A, 0x9C, 0xCA, 0x4F, 0x42, 0x9A, 0xA2, 0x4B, 0x98, 0xF8, 0xFF, 0xBE,
0xBF, 0xE4, 0xA0, 0x0F, 0xEB, 0xC1, 0xDB, 0x69, 0x4D, 0x93, 0x16, 0x5F,
0x3D, 0xBF, 0xA1, 0xD8, 0x4D, 0x05, 0x21, 0xD1, 0xB4, 0xDA, 0x13, 0x4C,
0x27, 0x8E, 0xB2, 0x4F, 0x57, 0x07, 0xCC, 0xA6, 0xA1, 0x0F, 0x52, 0xD5,
0x72, 0x16, 0x9D };
/* String of entropy */
uint8_t entropy_data[32] =
{ 0x91, 0x20, 0x1a, 0x18, 0x9b, 0x6d, 0x1a, 0xa7, 0x0e, 0x69, 0x57, 0x6f, 0x36,
0xb6, 0xaa, 0x88, 0x55, 0xfd, 0x4a, 0x7f, 0x97, 0xe9, 0x72, 0x69, 0xb6,
0x60, 0x88, 0x78, 0xe1, 0x9c, 0x8c, 0xa5 };
uint8_t output[2048 / 8];
int32_t outputSize = 0;
/* Private function prototypes -----------------------------------------------*/
int32_t RSA_Encrypt(RSApubKey_stt *P_pPubKey, const uint8_t *P_pInputMessage,
int32_t P_InputSize, uint8_t *P_pOutput);
int32_t RSA_Decrypt(RSAprivKey_stt *P_pPrivKey, const uint8_t *P_pInputMessage,
uint8_t *P_pOutput, int32_t *P_OutputSize);
TestStatus Buffercmp(const uint8_t *pBuffer, uint8_t *pBuffer1,
uint16_t BufferLength);
/* Private functions ---------------------------------------------------------*/
/**
* @brief Main program
* @param None
* @retval None
*/
void rsa_test(void)
{
int32_t status = RSA_ERR_GENERIC;
RSApubKey_stt PubKey_st;
RSAprivKey_stt PrivKey_st;
__CRC_CLK_ENABLE();
// 输出源数据信息
LCD_Printf("------original data------");
LCD_Printf(Message);
/* Testing Encryption and Decryption */
/* Preparing for Encryption */
PubKey_st.mExponentSize = sizeof(PublicExponent);
PubKey_st.mModulusSize = sizeof(Modulus);
PubKey_st.pmExponent = (uint8_t*) PublicExponent;
PubKey_st.pmModulus = (uint8_t*) Modulus;
status = RSA_Encrypt(&PubKey_st, Message, sizeof(Message), output);
// 输出加密数据信息
LCD_Printf("------encode data------");
LCD_Printf(output);
if (status == RSA_SUCCESS)
{
/* Now we will test decryption */
PrivKey_st.mExponentSize = sizeof(PrivateExponent);
PrivKey_st.mModulusSize = sizeof(Modulus);
PrivKey_st.pmExponent = (uint8_t*) PrivateExponent;
PrivKey_st.pmModulus = (uint8_t*) Modulus;
status = RSA_Decrypt(&PrivKey_st, output, output, &outputSize);
if (status == RSA_SUCCESS)
{
/* Check expected output */
if ((Buffercmp(Message, output, sizeof(Message)) == PASSED)
&& (outputSize == sizeof(Message)))
{
/* add application traitment in case of Encryption/Decryption is passed */
// 输出解密数据信息
LCD_Printf("------decode data------");
LCD_Printf(output);
}
else
{
Error_Handler();
}
}
else
{
Error_Handler();
}
}
else
{
Error_Handler();
}
}
/**
* @brief RSA Encryption with PKCS#1v1.5
* @param P_pPubKey The RSA public key structure, already initialized
* @param P_pInputMessage Input Message to be signed
* @param P_MessageSize Size of input message
* @param P_pOutput Pointer to output buffer
* @retval error status: can be RSA_SUCCESS if success or one of
* RSA_ERR_BAD_PARAMETER, RSA_ERR_MESSAGE_TOO_LONG, RSA_ERR_BAD_OPERATION
*/
int32_t RSA_Encrypt(RSApubKey_stt *P_pPubKey, const uint8_t *P_pInputMessage,
int32_t P_InputSize, uint8_t *P_pOutput)
{
int32_t status = RNG_SUCCESS;
RNGstate_stt RNGstate;
RNGinitInput_stt RNGinit_st;
RNGinit_st.pmEntropyData = entropy_data;
RNGinit_st.mEntropyDataSize = sizeof(entropy_data);
RNGinit_st.mPersDataSize = 0;
RNGinit_st.mNonceSize = 0;
status = RNGinit(&RNGinit_st, &RNGstate);
if (status == RNG_SUCCESS)
{
RSAinOut_stt inOut_st;
membuf_stt mb;
mb.mSize = sizeof(preallocated_buffer);
mb.mUsed = 0;
mb.pmBuf = preallocated_buffer;
/* Fill the RSAinOut_stt */
inOut_st.pmInput = P_pInputMessage;
inOut_st.mInputSize = P_InputSize;
inOut_st.pmOutput = P_pOutput;
/* Encrypt the message, this function will write sizeof(modulus) data */
status = RSA_PKCS1v15_Encrypt(P_pPubKey, &inOut_st, &RNGstate, &mb);
}
return (status);
}
/**
* @brief RSA Decryption with PKCS#1v1.5
* @param P_pPrivKey The RSA private key structure, already initialized
* @param P_pInputMessage Input Message to be signed
* @param P_MessageSize Size of input message
* @param P_pOutput Pointer to output buffer
* @retval error status: can be RSA_SUCCESS if success or RSA_ERR_GENERIC in case of fail
*/
int32_t RSA_Decrypt(RSAprivKey_stt *P_pPrivKey, const uint8_t *P_pInputMessage,
uint8_t *P_pOutput, int32_t *P_OutputSize)
{
int32_t status = RSA_SUCCESS;
RSAinOut_stt inOut_st;
membuf_stt mb;
mb.mSize = sizeof(preallocated_buffer);
mb.mUsed = 0;
mb.pmBuf = preallocated_buffer;
/* Fill the RSAinOut_stt */
inOut_st.pmInput = P_pInputMessage;
inOut_st.mInputSize = P_pPrivKey->mModulusSize;
inOut_st.pmOutput = P_pOutput;
/* Encrypt the message, this function will write sizeof(modulus) data */
status = RSA_PKCS1v15_Decrypt(P_pPrivKey, &inOut_st, P_OutputSize, &mb);
return (status);
}
/**
* @brief Compares two buffers.
* @param pBuffer, pBuffer1: buffers to be compared.
* @param BufferLength: buffer's length
* @retval PASSED: pBuffer identical to pBuffer1
* FAILED: pBuffer differs from pBuffer1
*/
TestStatus Buffercmp(const uint8_t *pBuffer, uint8_t *pBuffer1,
uint16_t BufferLength)
{
while (BufferLength--)
{
if (*pBuffer != *pBuffer1)
{
return FAILED;
}
pBuffer++;
pBuffer1++;
}
return PASSED;
}
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
rsa.h:
#ifndef __RSA_H__
#define __RSA_H__
#include "main.h"
#include "crypto.h"
void rsa_test(void);
#endif
链接: https://pan.baidu.com/s/15SyTkza0Xtw_fYjeVZ3Dlw 提取码: uetf