MI CIPHER API
1. SUMMARY¶
1.1. Module Description¶
The CIPHER module provides data encryption/decryption function, including AES/RSA/SHA encryption and decryption algorithms.
1.2. Flow Block Diagram¶
Fig 1-1 AES Crypto
Fig 1-2 SHA Crypto
Fig 1-3 RSA Public Encrypt and Private Decrypt
Fig 1-4 RSA Private Encrypt and Public Decrypt
Fig 1-5 RSA Signature and Verification:
1.3. Keyword Description¶
-
AES:
Advanced Encryption Standard in cryptography is a symmetric encryption algorithm.
-
SHA:
Secure Hash Algorithm is a family of cryptographic hash functions, which can calculate digital information of different lengths to generate fixed-length message summary.
-
RSA:
The RSA is an asymmetric encryption algorithm with high reliability.
2. API REFERENCE¶
2.1. API List¶
Name of API | Function |
---|---|
MI_CIPHER_Init | Initialize CIPHER module |
MI_CIPHER_UnInit | Deinitialize CIPHER module |
MI_CIPHER_CreateHandle | Create a CIPHER Handle |
MI_CIPHER_DestroyHandle | Destroy a CIPHER Handle |
MI_CIPHER_ConfigHandle | Configure the CIPHER parameter |
MI_CIPHER_Encrypt | Encrypt data |
MI_CIPHER_Decrypt | Decrypt data |
MI_CIPHER_HashInit | Initialize HASH library |
MI_CIPHER_HashUnInit | Deinitialize HASH library and release resource |
MI_CIPHER_HashUpdate | Update hash value |
MI_CIPHER_HashFinal | Get hash value |
MI_CIPHER_RsaPublicEncrypt | Use RSA public key to encrypt a plaintext |
MI_CIPHER_RsaPrivateDecrypt | Use RSA private key to decrypt a ciphertext |
MI_CIPHER_RsaPrivateEncrypt | Use RSA private key to encrypt a plaintext |
MI_CIPHER_RsaPublicDecrypt | Use RSA public key to decrypt a ciphertext |
MI_CIPHER_RsaSign | Use RSA private key to sign data |
MI_CIPHER_RsaVerify | Use RSA public key to verify data |
MI_CIPHER_InitDev | Initialize CIPHER module |
MI_CIPHER_DeInitDev | Deinitialize CIPHER module |
2.2. MI_CIPHER_Init¶
-
Function
Initialize CIPHER module
-
Syntax
MI_S32 MI_CIPHER_Init(void);
-
Return Value
-
Zero: Successful
-
Non-zero: Failed, see error code for details
-
-
Requirement
-
Header: mi_cipher.h
-
Library: libmi_cipher.a/libmi_cipher.so
-
-
Note
Must be called before using function of this module.
-
Related API
2.3. MI_CIPHER_Uninit¶
-
Function
Deinitialize CIPHER module to release resource
-
Syntax
MI_S32 MI_CIPHER_Uninit (void);
-
Return Value
-
Zero: Successful
-
Non-zero: Failed, see error code for details
-
-
Requirement
-
Header: mi_cipher.h
-
Library: libmi_cipher.a/libmi_cipher.so
-
2.4. MI_CIPHER_CreateHandle¶
-
Function
Create a CIPHER Handle
-
Syntax
MI_S32 MI_CIPHER_CreateHandle(MI_HANDLE *phandle);
-
Parameter
Parameter Description Input/Output phandle Pointer to Cipher handle address Output -
Return Value
-
Zero: Successful
-
Non-zero: Failed, see error code for details
-
-
Requirement
-
Header: mi_cipher.h
-
Library: libmi_cipher.a/libmi_cipher.so
-
-
Example
The following is an example of ecb-aes-128 encryption and decryption:
#include <stdlib.h> #include <stdio.h> #include <string.h> #include <fcntl.h> #include <unistd.h> #include "mi_common.h" #include "mi_cipher.h" #include "mi_cipher_datatype.h" MI_S32 Setconfiginfo(MI_HANDLE chnHandle, MI_CIPHER_ALG_e alg, const MI_U8 u8KeyBuf[16], const MI_U8 u8IVBuf[16]) { MI_S32 s32Ret = MI_SUCCESS; MI_CIPHER_Config_t stconfig; memset(&stconfig, 0, sizeof(MI_CIPHER_Config_t)); stconfig.eAlg = alg; if(alg != MI_CIPHER_ALG_AES_ECB) { memcpy(stconfig.iv, u8IVBuf,16); } stconfig.eKeySize = E_MI_CIPHER_KEY_SIZE_128; memcpy(stconfig.key, u8KeyBuf, 16); s32Ret = MI_CIPHER_ConfigHandle(chnHandle, &stconfig); if (MI_SUCCESS != s32Ret) { s32Ret = -1; } return s32Ret; } int main(int argc, char **argv) { MI_S32 s32Ret = MI_SUCCESS; MI_U32 u32TestSrcDataLen = 16; MI_U32 u32TestDstDataLen = 0; MI_U32 u32Testcached = 0; MI_U8 pInputAddrVir[16]; MI_U8 pOutputAddrVir[16]; MI_HANDLE hTestchnid = -1; MI_U8 aes_key[16] = {0x1B,0x2F,0x38,0x47,0x55,0x6C,0x71,0x89,0x9A,0xE1,0x42,0x93,0x14,0xE5,0x76,0x7D}; MI_U8 aes_src[16] = {0x61,0xE2,0xF3,0xE4,0x85,0x56,0x57,0x88,0xF9,0x3A,0x6B,0x2C,0x6D,0x4E,0x7F,0x6A}; MI_U8 aes_dst[16] = {0x53,0x6D,0x21,0xB8,0x7C,0x68,0xEC,0x31,0xB2,0xA0,0x64,0x72,0x65,0x6E,0xA2,0xDA}; s32Ret = MI_CIPHER_Init(); if(MI_SUCCESS != s32Ret) { return ; } s32Ret = MI_CIPHER_CreateHandle(&hTestchnid); if(MI_SUCCESS != s32Ret) { MI_CIPHER_Uninit(); return ; } s32Ret = Setconfiginfo(hTestchnid, MI_CIPHER_ALG_AES_ECB, aes_key, NULL); if(MI_SUCCESS != s32Ret) { CIPHER_ERR("Set config info failed.\n"); goto __CIPHER_EXIT__; } memset(pInputAddrVir, 0x0, u32TestSrcDataLen); memcpy(pInputAddrVir, aes_src, u32TestSrcDataLen); printBuffer("ECB-AES-128-ORI:", aes_src, sizeof(aes_src)); memset(pOutputAddrVir, 0x0, u32TestSrcDataLen); s32Ret = MI_CIPHER_Encrypt(hTestchnid, pInputAddrVir, pOutputAddrVir, u32TestSrcDataLen, &u32TestDstDataLen); if(MI_SUCCESS != s32Ret) { CIPHER_ERR("Cipher encrypt failed.\n"); s32Ret = -1; goto __CIPHER_EXIT__; } printBuffer("ECB-AES-128-ENC:", pOutputAddrVir, u32TestDstDataLen); /* compare */ if ( 0 != memcmp(pOutputAddrVir, aes_dst, u32TestSrcDataLen) ) { CIPHER_ERR("Memcmp failed!\n"); s32Ret = -1; goto __CIPHER_EXIT__; } /* For decrypt */ memcpy(pInputAddrVir, pOutputAddrVir, u32TestSrcDataLen); memset(pOutputAddrVir, 0x00, u32TestSrcDataLen); s32Ret = Setconfiginfo(hTestchnid, MI_CIPHER_ALG_AES_ECB, aes_key, NULL); if(MI_SUCCESS != s32Ret) { CIPHER_ERR("Set config info failed.\n"); goto __CIPHER_EXIT__; } s32Ret = MI_CIPHER_Decrypt(hTestchnid, pInputAddrVir, pOutputAddrVir, u32TestSrcDataLen, &u32TestDstDataLen); if(MI_SUCCESS != s32Ret) { CIPHER_ERR("Cipher decrypt failed.\n"); s32Ret = -1; goto __CIPHER_EXIT__; } printBuffer("ECB-AES-128-DEC:", pOutputAddrVir, u32TestDstDataLen); /* compare */ if ( 0 != memcmp(pOutputAddrVir, aes_src, u32TestSrcDataLen) ) { CIPHER_ERR("Memcmp failed!\n"); s32Ret = -1; goto __CIPHER_EXIT__; } printf("\033[0;32m""sample ECB_AES128 %s run successfully!\n""\033[0m", __FUNCTION__); MI_CIPHER_DestroyHandle(hTestchnid); MI_CIPHER_Uninit(); return; __CIPHER_EXIT__: MI_CIPHER_DestroyHandle(hTestchnid); MI_CIPHER_Uninit(); CIPHER_ERR("\033[0;32m""sample ECB_AES128 run fail!\n""\033[0m"); return ; }
-
Related API
2.5. MI_CIPHER_DestroyHandle¶
-
Function
Destroy a CIPHER Handle
-
Syntax
MI_S32 MI_CIPHER_DestroyHandle(MI_HANDLE handle);
-
Parameter
Parameter Description Input/Output handle The created Cipher Handle. Input -
Return Value
-
Zero: Successful
-
Non-zero: Failed, see error code for details
-
-
Requirement
-
Header: mi_cipher.h
-
Library: libmi_cipher.a/libmi_cipher.so
-
-
Example
Refer to MI_CIPHER_CreateHandle .
2.6. MI_CIPHER_ConfigHandle¶
-
Function
Configure the Cipher parameter.
-
Syntax
MI_S32 MI_CIPHER_ConfigHandle(MI_HANDLE handle, MI_CIPHER_Config_t *pconfig);
-
Parameter
Parameter Description Input/Output handle The created Cipher Handle. Input pconfig Configuration parameter corresponding to the cipher handle Input -
Return Value
-
Zero: Successful
-
Non-zero: Failed, see error code for details
-
-
Requirement
-
Header: mi_cipher.h
-
Library: libmi_cipher.a/libmi_cipher.so
-
-
Example
Refer to MI_CIPHER_CreateHandle .
2.7. MI_CIPHER_Encrypt¶
-
Function
Encrypt data.
-
Syntax
MI_U32 MI_CIPHER_Encrypt(MI_HANDLE handle, void* srcAddr, void* dstAddr , MI_U32 u32srcByteLen. MI_U32* pu32dstByteLen);
-
Parameter
Parameter Description Input/Output handle The created Cipher Handle Input srcAddr Address of data to be encrypted Input dstAddr Address of data after encryption Output u32srcByteLen Length of encrypted data Output pu32dstByteLen Length of Output data for encryption Output -
Return Value
-
Zero: Successful
-
Non-zero: Failed, see error code for details
-
-
Requirement
-
Header: mi_cipher.h
-
Library: libmi_cipher.a/libmi_cipher.so
-
-
Example
Refer to MI_CIPHER_CreateHandle .
2.8. MI_CIPHER_Decrypt¶
-
Function
Decrypt data.
-
Syntax
MI_CIPHER_Decrypt(MI_HANDLE handle, void* srcAddr, void* dstAddr, MI_U32 u32srcByteLen, MI_U32* pu32dstByteLen) ;
-
Parameter
Parameter Description Input/Output handle The created Cipher Handle Input srcAddr Address of data to be decrypted Input dstAddr Address of data after decryption Output u32srcByteLen Length of decrypted data Output pu32dstByteLen Length of Output data for decryption Output -
Return Value
-
Zero: Successful
-
Non-zero: Failed, see error code for details
-
-
Requirement
-
Header: Mi_cipher.h
-
Library: libmi_cipher.a/libmi_cipher.so
-
-
Example
Refer to MI_CIPHER_CreateHandle .
2.9. MI_CIPHER_HashInit¶
-
Function
Initialize HASH module.
-
Syntax
MI_S32 MI_CIPHER_HashInit(MI_CIPHER_HASH_ALGO_e eHashAlgoType, MI_HANDLE *pHashHandle);
-
Parameter
Parameter Description Input/Output eHashAlgoType Hash algorithm type Input pHashHandle Output hash handle Output -
Return Value
-
Zero: Successful
-
Non-zero: Failed, see error code for details
-
-
Requirement
-
Header: mi_cipher.h
-
Library: libmi_cipher.a/libmi_cipher.so
-
-
Example
The following is an example of SHA1 hash initialization:
static unsigned char sha1_buf[3][128] = { {"abc"}, {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"}, {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqabcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"} }; static const unsigned char sha1_sum[5][20] = { {0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E, 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D}, {0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE, 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1}, {0xaf, 0xc5, 0x3a, 0x4e, 0xa2, 0x08, 0x56, 0xf9, 0x8e, 0x08, 0xdc, 0x6f, 0x3a, 0x5c, 0x98, 0x33, 0x13, 0x77, 0x68, 0xed}, {0x34, 0xaa, 0x97, 0x3c, 0xd4, 0xc4, 0xda, 0xa4, 0xf6, 0x1e, 0xeb, 0x2b, 0xdb, 0xad, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6f}, {0x7d, 0xf9, 0x62, 0x1f, 0x17, 0xad, 0x18, 0xc5, 0x8a, 0x5a, 0xf7, 0x99, 0x1d, 0x12, 0x62, 0x20, 0x0f, 0xaf, 0xa9, 0x0f}, }; static MI_U8 au8Buf[LONG_DATA_SIZE]; int main(int argc, char *argv) { MI_S32 ret = MI_SUCCESS; MI_U8 u8Hash[20]; MI_U32 i = 0,j = 0; MI_HANDLE hHandle[MAX_HASH_HANDLE]; MI_CIPHER_HASH_ALGO_e eHashAlgoType; MI_U32 u32HashOutLen = 0; ret = MI_CIPHER_Init(); if ( MI_SUCCESS != ret ) { return -1; } memset(u8Hash, 0, 20); for(i = 0; i < MAX_HASH_HANDLE; i++) { eHashAlgoType = MI_CIPHER_HASH_ALG_SHA1; ret = MI_CIPHER_HashInit(eHashAlgoType, &hHandle[i]); if ( MI_SUCCESS != ret ) { CIPHER_ERR("hHandle :%d MI_CIPHER_HashUpdate failed \n", hHandle[i]); MI_CIPHER_HashUnInit(hHandle[i]); goto __CIPHER_HASH_EXIT__; } } for(i = 0; i < MAX_HASH_HANDLE; i++) { if(i == 3) { memset(au8Buf, 'a', LONG_DATA_SIZE); for(j=0; j<1000000/LONG_DATA_SIZE; j++) { ret = MI_CIPHER_HashUpdate(hHandle[i], au8Buf, LONG_DATA_SIZE); if ( MI_SUCCESS != ret ) { CIPHER_ERR("hHandle :%d MI_CIPHER_HashUpdate failed \n", hHandle[i]); MI_CIPHER_HashUnInit(hHandle[i]); goto __CIPHER_HASH_EXIT__; } } } else { ret = MI_CIPHER_HashUpdate(hHandle[i], sha1_buf[i], strlen(sha1_buf[i])); if ( MI_SUCCESS != ret ) { CIPHER_ERR("hHandle :%d MI_CIPHER_HashUpdate failed \n", hHandle[i]); MI_CIPHER_HashUnInit(hHandle[i]); goto __CIPHER_HASH_EXIT__; } } } for(i = 0; i < MAX_HASH_HANDLE; i++) { ret = MI_CIPHER_HashFinal(hHandle[i], u8Hash, &u32HashOutLen); if ( MI_SUCCESS != ret ) { CIPHER_ERR("hHandle :%d MI_CIPHER_HashFinal failed \n", hHandle[0]); MI_CIPHER_HashUnInit(hHandle[i]); goto __CIPHER_HASH_EXIT__; } if(memcmp(u8Hash, sha1_sum[i], 20) != 0) { CIPHER_ERR("\033[0;31m" "SHA1 run failed, sample %d!\n" "\033[0m", i); printBuffer("Sha1 result:", u8Hash, 20); printBuffer("golden data:", sha1_sum[i], 20); MI_CIPHER_HashUnInit(hHandle[i]); goto __CIPHER_HASH_EXIT__; return -1; } MI_CIPHER_HashUnInit(hHandle[i]); CIPHER_DBG("SHA1 run success, sample %d!\n", i); } CIPHER_DBG("sample SHA1 run successfully!\n"); return MI_SUCCESS; __CIPHER_HASH_EXIT__: MI_CIPHER_Uninit(); CIPHER_ERR("sample SHA1 run fail!\n"); return -1; }
-
Related API
2.10. MI_CIPHER_HashUnInit¶
-
Function
Deinitialize hash module to release resource.
-
Syntax
MI_S32 MI_CIPHER_HashUnInit(MI_HANDLE hHashHandle);
-
Return Value
-
Zero: Successful
-
Non-zero: Failed, see error code for details
-
-
Requirement
-
Header: mi_cipher.h
-
Library: libmi_cipher.a/libmi_cipher.so
-
-
Example
Refer to MI_CIPHER_HashInit.
-
Related API
2.11. MI_CIPHER_HashUpdate¶
-
Function
Update hash value.
-
Syntax
MI_S32 MI_CIPHER_HashUpdate(MI_HANDLE hHashHandle , MI_U8 *pu8InputData, MI_U32 u32IDataLen);
-
Parameter
Parameter Description Input/Output hHashHandle Hash handle Input pu8InputData Input data buffer Input u32IDataLen Input data length Input -
Return Value
-
Zero: Successful
-
Non-zero: Failed, see error code for details
-
-
Requirement
-
Header: mi_cipher.h
-
Library: libmi_cipher.a/libmi_cipher.so
-
-
Example
Refer to MI_CIPHER_HashInit.
2.12. MI_CIPHER_HashFinal¶
-
Function
Get hash value. After calculating all the data, you can call this interface to get the final hash value. This interface, when called, will close the hash handle. If the calculation is to be suspended halfway, this interface must be called as well, to close the hash handle.
-
Syntax
MI_S32 MI_CIPHER_HashFinal(MI_HANDLE hHashHandle, MI_U8 *pu8OutputHash, MI_U32 *pu32OutputHashLen);
-
Parameter
Parameter Description Input/Output hHashHandle Hash handle Input pu8OutputHash Output hash value Output pu32OutputHashLe Output hash length (byte count) Output -
Return Value
-
Zero: Successful
-
Non-zero: Failed, see error code for details
-
-
Requirement
-
Header: mi_cipher.h
-
Library: libmi_cipher.a/libmi_cipher.so
-
-
Example
Refer to MI_CIPHER_HashInit.
2.13. MI_CIPHER_RsaPublicEncrypt¶
-
Function
Use RSA public key to encrypt data.
-
Syntax
MI_S32 MI_CIPHER_RsaPublicEncrypt(MI_CIPHER_RSA_PUB_ENC_t *pstRsaEncrypt, MI_U8 *pu8Input, MI_U32 u32InLen, MI_U8 *pu8Output, MI_U32 *pu32OutLen));
-
Parameter
Parameter Description Input/Output pstRsaEncrypt Encrypt attribute structure Input pu8Input Data to be encrypted Input u32InLen Length of data to be encrypted Input pu8Output Data after encryption Output pu32OutLen Length of data after encryption Output -
Return Value
-
Zero: Successful
-
Non-zero: Failed, see error code for details
-
-
Requirement
-
Header: mi_cipher.h
-
Library: libmi_cipher.a/libmi_cipher.so
-
-
Example
The following is an example of public key encryption and private key decryption:
MI_S32 PKCS_PUB_ENC(MI_CIPHER_RSA_ENC_SCHEME_E eRsaAlgoType, MI_U8 *pu8Expect) { MI_S32 ret = MI_SUCCESS, i; MI_U8 u8Enc[256]; MI_U8 u8Dec[256]; MI_U32 u32OutLen = 0; MI_CIPHER_RSA_PUB_ENC_t stRsaEncrypt; MI_CIPHER_RSA_PRI_ENC_t stRsaDecrypt; ret = MI_CIPHER_Init(); if ( MI_SUCCESS != ret ) { return -1; } memset(&stRsaEncrypt, 0, sizeof(MI_CIPHER_RSA_PUB_ENC_t)); memset(&stRsaDecrypt, 0, sizeof(MI_CIPHER_RSA_PRI_ENC_t)); stRsaEncrypt.eRsaAlgoType = eRsaAlgoType; stRsaEncrypt.stPubKey.pu8ExpE = E; stRsaEncrypt.stPubKey.pu8ModN = N; stRsaEncrypt.stPubKey.expSize = sizeof(E); stRsaEncrypt.stPubKey.modSize =sizeof(N); if(eRsaAlgoType == MI_CIPHER_RSA_ENC_SCHEME_NO_PADDING) { ret = MI_CIPHER_RsaPublicEncrypt(&stRsaEncrypt, NO_PADDING, 256, u8Enc, &u32OutLen); } else { ret = MI_CIPHER_RsaPublicEncrypt(&stRsaEncrypt, test_data, sizeof(test_data), u8Enc, &u32OutLen); } if ( MI_SUCCESS != ret ) { CIPHER_ERR("MI_CIPHER_RsaPublicEncrypt failed\n"); goto __CIPHER_RSA_EXIT__; } if(pu8Expect != NULL) { if(memcmp(u8Enc, pu8Expect, u32OutLen) != 0) { CIPHER_ERR("MI_CIPHER_RsaPublicEncrypt failed\n"); printBuffer("enc", u8Enc, u32OutLen); printBuffer("expect", pu8Expect, 256); goto __CIPHER_RSA_EXIT__; } } stRsaDecrypt.eRsaAlgoType = eRsaAlgoType; stRsaDecrypt.stPriKey.pu8ModN = N; stRsaDecrypt.stPriKey.pu8ExpD = D; stRsaDecrypt.stPriKey.expSize = sizeof(D); stRsaDecrypt.stPriKey.modSize = sizeof(N); ret = MI_CIPHER_RsaPrivateDecrypt(&stRsaDecrypt, u8Enc, u32OutLen, u8Dec, &u32OutLen); if ( MI_SUCCESS != ret ) { CIPHER_ERR("MI_CIPHER_RsaPrivateDecrypt failed\n"); goto __CIPHER_RSA_EXIT__; } if(eRsaAlgoType == MI_CIPHER_RSA_ENC_SCHEME_NO_PADDING) { if(memcmp(u8Dec, NO_PADDING, 256) != 0) { CIPHER_ERR("MI_CIPHER_RsaPrivateDecrypt failed\n"); printBuffer("dec", u8Dec, u32OutLen); printBuffer("expect", NO_PADDING, 256); goto __CIPHER_RSA_EXIT__; } } else { if(sizeof(test_data) != u32OutLen) { CIPHER_ERR("MI_CIPHER_RsaPrivateDecrypt len error\n"); printf("dec: 0x%x, expect: %zu\n", u32OutLen, sizeof(test_data) - 1); goto __CIPHER_RSA_EXIT__; } if(memcmp(u8Dec, test_data, sizeof(test_data)) != 0) { CIPHER_ERR("MI_CIPHER_RsaPrivateDecrypt failed\n"); printBuffer("enc", u8Enc, u32OutLen); printBuffer("expect", test_data, sizeof(test_data)); goto __CIPHER_RSA_EXIT__; } } CIPHER_DBG("sample PKCS_PUB_ENC run successfully!\n"); MI_CIPHER_Uninit(); return MI_SUCCESS; __CIPHER_RSA_EXIT__: CIPHER_ERR("sample PKCS_PUB_ENC run fail!\n"); MI_CIPHER_Uninit(); return -1; }
2.14. MI_CIPHER_RsaPrivateDecrypt¶
-
Function
Use private key to decrypt data.
-
Syntax
MI_S32 MI_CIPHER_RsaPrivateDecrypt(MI_CIPHER_RSA_PRI_ENC_t * pstRsaDecrypt , MI_U8 *pu8Input, MI_U32 u32InLen, MI_U8 *pu8Output, MI_U32 *pu32OutLen));
-
Parameter
Parameter Description Input/Output pstRsaDecrypt Decrypt attribute structure Input pu8Input Data to be decrypted Input u32InLen Length of data to be decrypted Input pu8Output Data after decryption Output pu32OutLen Length of data after decryption Output -
Return Value
-
Zero: Successful
-
Non-zero: Failed, see error code for details
-
-
Requirement
-
Header: mi_cipher.h
-
Library: libmi_cipher.a/libmi_cipher.so
-
-
Example
Refer to MI_CIPHER_RsaPublicEncrypt.
2.15. MI_CIPHER_RsaPrivateEncrypt¶
-
Function
Use private key to encrypt data.
-
Syntax
MI_S32 MI_CIPHER_RsaPrivateEncrypt(MI_CIPHER_RSA_PRI_ENC_t * pstRsaEncrypt , MI_U8 *pu8Input, MI_U32 u32InLen, MI_U8 *pu8Output, MI_U32 *pu32OutLen));
-
Parameter
Parameter Description Input/Output pstRsaEncrypt Encrypt attribute structure Input pu8Input Data to be encrypted Input u32InLen Length of data to be encrypted Input pu8Output Data after encryption Output pu32OutLen Length of data after encryption Output -
Return Value
-
Zero: Successful
-
Non-zero: Failed, see error code for details
-
-
Requirement
-
Header: mi_cipher.h
-
Library: libmi_cipher.a/libmi_cipher.so
-
-
Example
The following is an example of private key encryption and public key decryption:
MI_S32 PKCS_PRI_ENC(MI_CIPHER_RSA_ENC_SCHEME_E eRsaAlgoType) { MI_S32 ret = MI_SUCCESS; MI_U8 u8Sign[256]; MI_U32 u32SignLen; MI_U8 u8Hash[32]; MI_U32 u32HLen; MI_CIPHER_RSA_PUB_ENC_t stRsaDecrypt; MI_CIPHER_RSA_PRI_ENC_t stRsaEncrypt; ret = MI_CIPHER_Init(); if ( MI_SUCCESS != ret ) { return -1; } memset(&stRsaEncrypt, 0, sizeof(MI_CIPHER_RSA_PRI_ENC_t)); memset(&stRsaDecrypt, 0, sizeof(MI_CIPHER_RSA_PUB_ENC_t)); stRsaEncrypt.eRsaAlgoType = eRsaAlgoType; stRsaDecrypt.eRsaAlgoType = eRsaAlgoType; stRsaEncrypt.stPriKey.pu8ExpD = D; stRsaEncrypt.stPriKey.pu8ModN = N; stRsaEncrypt.stPriKey.expSize = sizeof(D); stRsaEncrypt.stPriKey.modSize = sizeof(N); stRsaDecrypt.stPubKey.pu8ExpE = E; stRsaDecrypt.stPubKey.pu8ModN = N; stRsaDecrypt.stPubKey.expSize = sizeof(E); stRsaDecrypt.stPubKey.modSize =sizeof(N); ret = MI_CIPHER_RsaPrivateEncrypt(&stRsaEncrypt, sha256_sum, 32, u8Sign, &u32SignLen); if ( MI_SUCCESS != ret ) { CIPHER_ERR("MI_CIPHER_RsaPrivateEncrypt failed\n"); goto __CIPHER_RSA_EXIT__; } // printBuffer("sign", u8Sign, u32SignLen); ret = MI_CIPHER_RsaPublicDecrypt(&stRsaDecrypt, u8Sign, u32SignLen, u8Hash, &u32HLen); if ( MI_SUCCESS != ret ) { CIPHER_ERR("MI_CIPHER_RsaPublicDecrypt failed\n"); goto __CIPHER_RSA_EXIT__; } CIPHER_DBG("sample PKCS_PRI_ENC run successfully!\n"); MI_CIPHER_Uninit(); return MI_SUCCESS; __CIPHER_RSA_EXIT__: MI_CIPHER_Uninit(); CIPHER_ERR("sample PKCS_PRI_ENC run fail!\n"); return -1; }
2.16. MI_CIPHER_RsaPublicDecrypt¶
-
Function
Use RSA public key to decrypt data.
-
Syntax
MI_S32 MI_CIPHER_RsaPublicEncrypt(MI_CIPHER_RSA_PUB_ENC_t *pstRsaDecrypt, MI_U8 *pu8Input, MI_U32 u32InLen, MI_U8 *pu8Output, MI_U32 *pu32OutLen));
-
Parameter
Parameter Description Input/Output pstRsaDecrypt Decrypt attribute structure Input pu8Input Data to be decrypted Input u32InLen Length of data to be decrypted Input pu8Output Data after decryption Output pu32OutLen Length of data after decryption Output -
Return Value
-
Zero: Successful
-
Non-zero: Failed, see error code for details
-
-
Requirement
-
Header: mi_cipher.h
-
Library: libmi_cipher.a/libmi_cipher.so
-
-
Note
Refer to MI_CIPHER_RsaPrivateEncrypt.
2.17. MI_CIPHER_RsaSign¶
-
Function
Use RSA private key to sign data.
-
Syntax
MI_S32 MI_CIPHER_RsaSign(MI_CIPHER_RSA_SIGN_t *pstRsaSign, MI_U8 *pu8InHashData, MI_U32 u32HashDataLen, MI_U8 *pu8OutSign, MI_U32 *pu32OutSignLen);
-
Parameter
Parameter Description Input/Output pstRsaSign Signature attribute structure Input pu8InHashData Hash summary of the text to be signed Input u32HashDataLen Input hash data length Input pu8OutSign Signature information Output pu32OutSignLen Length of signature information Output -
Return Value
-
Zero: Successful
-
Non-zero: Failed, see error code for details
-
-
Requirement
-
Header: mi_cipher.h
-
Library: libmi_cipher.a/libmi_cipher.so
-
-
Example
MI_S32 RSA_SIGN_VERIFY(MI_CIPHER_RSA_SIGN_SCHEME_E enScheme) { MI_S32 ret = MI_SUCCESS; MI_U8 u8Sign[256]; MI_U32 u32SignLen; MI_CIPHER_RSA_SIGN_t stRsaSign; MI_CIPHER_RSA_VERIFY_t stRsaVerify; ret = MI_CIPHER_Init(); if ( MI_SUCCESS != ret ) { return -1; } memset(&stRsaSign, 0, sizeof(MI_CIPHER_RSA_SIGN_t)); stRsaSign.eRsaAlgoType = enScheme; stRsaSign.stPriKey.pu8ModN = N; stRsaSign.stPriKey.pu8ExpD = D; stRsaSign.stPriKey.modSize = sizeof(N); stRsaSign.stPriKey.expSize = sizeof(D); ret = MI_CIPHER_RsaSign(&stRsaSign, sha256_sum, sizeof(sha256_sum) , u8Sign, &u32SignLen); if ( MI_SUCCESS != ret ) { CIPHER_ERR("MI_CIPHER_RsaSign failed\n"); goto _RSA_VERIFY_EXIT; } switch(enScheme) { case MI_CIPHER_RSA_SIGN_SCHEME_RSASSA_PKCS1_V15_SHA256: if(memcmp(u8Sign, RES, sizeof(RES)) != 0) { CIPHER_ERR("MI_CIPHER_RsaSign failed\n"); printBuffer("sign", u8Sign, u32SignLen); printBuffer("golden", RES, sizeof(RES)); goto _RSA_VERIFY_EXIT; } break; default: break; } // printBuffer("sign", u8Sign, u32SignLen); memset(&stRsaVerify, 0, sizeof(MI_CIPHER_RSA_VERIFY_t)); stRsaVerify.eRsaAlgoType = enScheme; stRsaVerify.stPubKey.pu8ModN = N; stRsaVerify.stPubKey.pu8ExpE = E; stRsaVerify.stPubKey.modSize = sizeof(N); stRsaVerify.stPubKey.expSize = sizeof(E); ret = MI_CIPHER_RsaVerify(&stRsaVerify, sha256_sum, sizeof(sha256_sum) , u8Sign, u32SignLen); if ( MI_SUCCESS != ret ) { CIPHER_ERR("MI_CIPHER_RsaVerify failed\n"); goto _RSA_VERIFY_EXIT; } CIPHER_DBG("rsa sign && verify sample run successfully!\n"); MI_CIPHER_Uninit(); return MI_SUCCESS; _RSA_VERIFY_EXIT: MI_CIPHER_Uninit(); CIPHER_ERR("rsa sign && verify sample run fail!\n"); return -1; }
2.18. MI_CIPHER_RsaVerify¶
-
Function
Use RSA public key to verify data.
-
Syntax
MI_S32 MI_CIPHER_RsaVerify(MI_CIPHER_RSA_VERIFY_t *pstRsaVerify, MI_U8 *pu8InHashData, MI_U32 u32HashDataLen, MI_U8 *pu8InSign, MI_U32 u32InSignLen);
-
Parameter
Parameter Description Input/Output pstRsaVerify Verification attribute structure Input pu8InHashData Hash summary of the text to be verified Input u32HashDataLen Input hash data length Input pu8InSign Signature information Output u32InSignLen Length of signature information Output -
Return Value
-
Zero: Successful
-
Non-zero: Failed, see error code for details
-
-
Requirement
-
Header: mi_cipher.h
-
Library: libmi_cipher.a/libmi_cipher.so
-
2.19. MI_CIPHER_InitDev¶
-
Function
Initialize CIPHER module
-
Syntax
MI_S32 MI_CIPHER_Init(MI_CIPHER_InitParam_t *pstInitParam);
-
Parameter
Parameter Description Input/Output pstInitParam Cipher Init Parameter. Input -
Return Value
-
Zero: Successful
-
Non-zero: Failed, see error code for details
-
-
Requirement
-
Header: mi_cipher.h
-
Library: libmi_cipher.a/libmi_cipher.so
-
-
Note
-
pstInitParam is not used for now, so please pass in null value.
-
For Version 2.06 and above, we recommend using this interface to replace the original MI_CIPHER_Init interface.
-
-
Related API
2.20. MI_CIPHER_DeInitDev¶
-
Function
Deinitialize CIPHER module to release resource
-
Syntax
MI_S32 MI_CIPHER_DeInitDev (void);
-
Return Value
-
Zero: Successful
-
Non-zero: Failed, see error code for details
-
-
Requirement
-
Header: mi_cipher.h
-
Library: libmi_cipher.a/libmi_cipher.so
-
-
Note
- For Version 2.06 and above, we recommend using this interface to replace the original MI_CIPHER_UnInit interface.
-
Example
Refer to MI_CIPHER_RsaSign.
3. DATA TYPE¶
3.1. Data Type List¶
The Cipher related data type, data structure, and complex are defined in the following table:
Data type | Definition |
---|---|
MI_CIPHER_ALG_e | Define AES encryption/decryption algorithm enumeration type |
MI_CIPHER_HASH_ALGO_e | Define hash algorithm enumeration type |
MI_CIPHER_RSA_ENC_SCHEME_E | Define encryption scheme enumeration type |
MI_CIPHER_RSA_SIGN_SCHEME_E | Define sign scheme enumeration type |
MI_CIPHER_KeySize_e | Define AES key size enumeration type |
MI_CIPHER_Config_t | Define Cipher configuration structure |
MI_CIPHER_RSA_PUB_Key_t | Define public key data structure |
MI_CIPHER_RSA_PRI_Key_t | Define private key data structure |
MI_CIPHER_RSA_PUB_ENC_t | Define Public Key Encrypt structure |
MI_CIPHER_RSA_PRI_ENC_t | Define private key encryption/decryption structure |
MI_CIPHER_RSA_SIGN_t | Define signature structure |
MI_CIPHER_RSA_VERIRY_t | Define verification structure |
MI_CIPHER_InitParam_t | Define Init Param |
3.2. MI_CIPHER_ALG_e¶
-
Description
Define AES Encryption/Decryption algorithm enumeration value
-
Definition
typedef enum { MI_CIPHER_ALG_AES_CBC , MI_CIPHER_ALG_AES_CTR , MI_CIPHER_ALG_AES_ECB , } MI_CIPHER_ALG_e;
-
Member
Member Description MI_CIPHER_ALG_AES_CBC CBC (Cipher Block Chaining) mode AEC algorithm MI_CIPHER_ALG_AES_CTR CTR (Counter) mode AEC algorithm MI_CIPHER_ALG_AES_ECB ECB (Electronic CodeBook) mode AEC algorithm
3.3. MI_CIPHER_HASH_ALGO_e¶
-
Description
Hash algorithm type
-
Definition
typedef enum { MI_CIPHER_HASH_ ALG_SHA1 ; MI_CIPHER_HASH_ ALG_SHA256 ; } MI_CIPHER_HASH_ALGO_e;
-
Member
Member Description MI_CIPHER_HASH_ ALG_SHA1 SHA1 Hash Algorithm MI_CIPHER_HASH_ ALG_SHA256 SHA256 Hash Algorithm
3.4. MI_CIPHER_RSA_ENC_SCHEME_E¶
-
Description
Define RSA encrypt scheme.
-
Definition
typedef enum { MI_CIPHER_RSA_ENC_SCHEME_NO_PADDING, MI_CIPHER_RSA_ENC_SCHEME_RSAES_OAEP_SHA1, MI_CIPHER_RSA_ENC_SCHEME_RSAES_OAEP_SHA256, MI_CIPHER_RSA_ENC_SCHEME_RSAES_PKCS1_V1_5, MI_CIPHER_RSA_ENC_SCHEME_BUTT, }MI_CIPHER_RSA_ENC_SCHEME_E;
-
Member
Member Description MI_CIPHER_RSA_ENC_SCHEME_NO_PADDING Without padding MI_CIPHER_RSA_ENC_SCHEME_RSAES_OAEP_SHA1 PKCS#1 RSAES-OAEP-SHA1 padding MI_CIPHER_RSA_ENC_SCHEME_RSAES_OAEP_SHA256 PKCS#1 RSAES-OAEP-SHA256 padding MI_CIPHER_RSA_ENC_SCHEME_RSAES_PKCS1_V1_5 PKCS#1 RSAES-PKCS1_V1_5 padding MI_CIPHER_RSA_ENC_SCHEME_BUTT Unknown
3.5. MI_CIPHER_RSA_SIGN_SCHEME_E¶
-
Description
Define RSA sign scheme.
-
Definition
typedef enum { MI_CIPHER_RSA_SIGN_SCHEME_RSASSA_PKCS1_V15_SHA1 = 0x100, MI_CIPHER_RSA_SIGN_SCHEME_RSASSA_PKCS1_V15_SHA256, MI_CIPHER_RSA_SIGN_SCHEME_RSASSA_PKCS1_PSS_SHA1, MI_CIPHER_RSA_SIGN_SCHEME_RSASSA_PKCS1_PSS_SHA256, MI_CIPHER_RSA_SIGN_SCHEME_BUTT, }MI_CIPHER_RSA_SIGN_SCHEME_E;
-
Member
Member Description MI_CIPHER_RSA_SIGN_SCHEME_RSASSA_PKCS1_V15_SHA1 PKCS#1 RSASSA_PKCS1_V15_SHA1 signature MI_CIPHER_RSA_SIGN_SCHEME_RSASSA_PKCS1_V15_SHA256 PKCS#1 RSASSA_PKCS1_V15_SHA256 signature MI_CIPHER_RSA_SIGN_SCHEME_RSASSA_PKCS1_PSS_SHA1 PKCS#1 RSASSA_PKCS1_PSS_SHA1 signature MI_CIPHER_RSA_SIGN_SCHEME_RSASSA_PKCS1_PSS_SHA256 PKCS#1 RSASSA_PKCS1_PSS_SHA256 signature MI_CIPHER_RSA_SIGN_SCHEME_BUTT Unknown
3.6. MI_CIPHER_KeySize_e¶
-
Description
The Enum to specify Key Size
-
Definition
typedef enum { E_MI_CIPHER_KEY_SIZE_128 = 0, E_MI_CIPHER_KEY_SIZE_192, E_MI_CIPHER_KEY_SIZE_256, } MI_CIPHER_KeySize_e;
-
Member
Member Description E_MI_CIPHER_KEY_SIZE_128 key size is 128-bit E_MI_CIPHER_KEY_SIZE_192 key size is 192-bit E_MI_CIPHER_KEY_SIZE_256 key size is 256-bit
3.7. MI_CIPHER_Config_t¶
-
Description
Define Cipher configuration structure
-
Definition
typedef struct MI_CIPHER_Config_s { MI_CIPHER_KeySize_e eKeySize; MI_U8 key[MI_CIPHER_KEY_SIZE_MAX]; MI_U8 iv[AES_BLOCK_SIZE]; MI_CIPHER_ALG_e eAlg; } MI_CIPHER_Config_t;
-
Member
Member Description eKeySize The Enum to specify the size of the key key The Array to store the key iv Initialization vector, AES_BLOCK_SIZE is 16 eAlg Encryption/Decryption algorithm type
3.8. MI_CIPHER_RSA_PUB_Key_t¶
-
Description
Define public key data structure
-
Definition
typedef struct MI_CIPHER_RSA_PUB_Key_s { MI_U8* pu8ExpE; MI_U8* pu8ModN; MI_U32 expSize; MI_U32 modSize; } MI_CIPHER_RSA_PUB_Key_t;
-
Member
Member Description pu8ExpE; Pointer to store key Exponent data pu8ModN Pointer to store key Module data expSize Size of exponent data modSize Size of module data
3.9. MI_CIPHER_RSA_PRI_Key_t¶
-
Description
Define private key data structure
-
Definition
typedef struct MI_CIPHER_RSA_PRI_Key_s { MI_U8* pu8ExpD; MI_U8* pu8ModN; MI_U32 expSize; MI_U32 modSize; } MI_CIPHER_RSA_PRI_Key_t;
-
Member
Member Description pu8ExpD Pointer to store key Exponent data pu8ModN Pointer to store key Module data expSize Size of exponent data modSize Size of module data
3.10. MI_CIPHER_RSA_PUB_ENC_t¶
-
Description
Define public key encryption/decryption algorithm parameter structure
-
Definition
typedef struct MI_CIPHER_RSA_PUB_ENC_s { MI_CIPHER_RSA_ENC_SCHEME_E eRsaAlgoType; MI_CIPHER_RSA_PUB_Key_t stPubKey; } MI_CIPHER_RSA_PUB_ENC_t;
-
Member
Member Description eRsaAlgoType Encryption/Decryption algorithm type stPubKey Key data
3.11. MI_CIPHER_RSA_PRI_ENC_t¶
-
Description
Define public key encryption/decryption parameter structure
-
Definition
typedef struct MI_CIPHER_RSA_PRI_ENC_s { MI_CIPHER_RSA_ENC_SCHEME_E eRsaAlgoType; MI_CIPHER_RSA_PRI_Key_t stPriKey; } MI_CIPHER_RSA_PRI_ENC_t;
-
Member
Member Description eRsaAlgoType Encryption/Decryption algorithm type stPriKey Private key data
3.12. MI_CIPHER_RSA_SIGN_t¶
-
Description
Define RSA signature structure
-
Definition
typedef struct MI_CIPHER_RSA_SIGN_s { MI_CIPHER_RSA_SIGN_SCHEME_E eRsaAlgoType; MI_CIPHER_RSA_PRI_Key_t stPriKey; } MI_CIPHER_RSA_SIGN_t;
-
Member
Member Description eRsaAlgoType Encryption/Decryption algorithm type stPriKey Private key data, for signature
3.13. MI_CIPHER_RSA_VERIFY_t¶
-
Description
Define public key encryption/decryption algorithm parameter structure
-
Definition
typedef struct MI_CIPHER_RSA_Veriry_s { MI_CIPHER_RSA_SIGN_SCHEME_E eRsaAlgoType; MI_CIPHER_RSA_PUB_Key_t stPubKey; } MI_CIPHER_RSA_VERIRY_t;
-
Member
Member Description eRsaAlgoType Encryption/Decryption algorithm type stPubKey Public key data, for verification
3.14. MI_CIPHER_InitParam_t¶
-
Description
Define Cipher Init Param.
-
Definition
typedef struct MI_CIPHER_InitParam_s { MI_U32 u32DevId; MI_U8 *u8Data; } MI_CIPHER_InitParam_t;
-
Member
Member Description u32DevId Device Id u8Data Data Pointer
4. ERROR CODE¶
Macro Definition | Description |
---|---|
MI_CIPHER_ERR_INVALID_DEVID | Invalid device number |
MI_CIPHER_ERR_ILLEGAL_PARAM | Invalid parameter setting |
MI_CIPHER_ERR_NOT_ENABLED | Device is not enabled |
MI_CIPHER_ERR_NOT_DISABLED | Device is not disabled |
MI_CIPHER_ERR_NULL_PTR | Using a NULL point |
MI_CIPHER_ERR_INVALID_CHNID | Invalid Channel ID |
MI_CIPHER_ERR_NOT_CONFIG | Device is not configured |
MI_CIPHER_ERR_NOT_SUPPORT | No supported operation |
MI_CIPHER_ERR_NOT_PERM | Operation is not permitted |
MI_CIPHER_ERR_NOMEM | The Device lacks of memory |
MI_CIPHER_ERR_NOBUF | Insufficient buffer |
MI_CIPHER_ERR_BUF_EMPTY | Buffer is empty |
MI_CIPHER_ERR_BUF_FULL | Buffer is full |
MI_CIPHER_ERR_SYS_NOTREADY | System is not initialized |
MI_CIPHER_ERR_BUSY | System is busy |
MI_CIPHER_ERR_MOD_NOTINIT | Module not initialized before use |
MI_CIPHER_ERR_MOD_INITED | Module already initialized |
MI_CIPHER_ERR_FAILED | Unexpected error |