sdf 测试-2-龙脉智能钥匙

任务详情

在openEuler(推荐)或Ubuntu或Windows(不推荐)中完成下面任务,参考网内容 和AI要给出详细过程,否则不得分。使用git管理你的代码

  1. 根据gmt0018标准,如何调用接口实现基于SM4的加密解密?(5‘)
  2. 使用龙脉智能钥匙实现SDF接口中的加密解密运算接口,至少支持SM4算法,把相关函数集成到src中的sdf.c中,补充必要的函数(5')
  3. 在test中的main.c调用进行测试,至少测试计算你的学号姓名的SM4加密解密。(5‘)
  4. 提交代码(或代码链接)和运行结果截图和git log截图 (5‘)

作业提交要求使用Markdown格式,同时提交Markdown转化的PDF 或者使用Word

任务0

  1. 初始化连接
  2. 选择算法,需要调用特定的接口
  3. 准备密钥
  4. 加密操作,调用加密接口,传入要加密的数据、密钥等
  5. 解密操作,调用解密接口,传入要解密的数据、密钥等
  6. 关闭连接

任务1

点击查看代码
#include <stdio.h>
 #include <string.h>
 #include "sdf.h" 
int main() { 
// 定义SM4密钥和明文 
uint8_t key[16] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 
0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10 }; 
uint8_t plaintext[16] = "Hello, World!!!"; 
uint8_t ciphertext[16]; 
uint8_t decryptedtext[16]; 
uint32_t len; 
// 打开设备和会话 
    DEVICE_HANDLE hDevice = SDF_OpenDevice(); 
    SESSION_HANDLE hSession = SDF_OpenSession(hDevice);
    KEY_HANDLE hKey = SDF_ImportKey(hSession, key, sizeof(key)); 
// 加密 
    SDF_Encrypt(hSession, hKey, plaintext, sizeof(plaintext), ciphertext, &len); 
printf("Ciphertext: "); 
for (int i = 0; i < len; i++) { 
printf("%02x ", ciphertext[i]); 
    } 
printf("\n"); 
// 解密 
    SDF_Decrypt(hSession, hKey, ciphertext, len, decryptedtext, &len); 
printf("Decrypted text: %s\n", decryptedtext); 
// 销毁密钥和关闭会话、设备 
    SDF_DestroyKey(hSession, hKey); 
    SDF_CloseSession(hSession); 
    SDF_CloseDevice(hDevice); 
return 0; 
}

任务2

点击查看代码
#include "sdf.h"
 #include <string.h>
 #include <openssl/sm4.h> 
typedef struct { 
    SM4_KEY sm4Key; 
} KEY_CONTEXT; 
DEVICE_HANDLE SDF_OpenDevice() { 
// 模拟设备句柄 
return (DEVICE_HANDLE)1; 
} 
int SDF_CloseDevice(DEVICE_HANDLE hDevice) { 
// 关闭设备 
return 0; 
} 
SESSION_HANDLE SDF_OpenSession(DEVICE_HANDLE hDevice) { 
// 模拟会话句柄 
return (SESSION_HANDLE)1; 
} 
int SDF_CloseSession(SESSION_HANDLE hSession) { 
// 关闭会话 
return 0; 
} 
KEY_HANDLE SDF_ImportKey(SESSION_HANDLE hSession, const uint8_t* key, uint32_t 
keyLen) { 
if (keyLen != 16) { 
return NULL; 
    } 
    KEY_CONTEXT* keyCtx = (KEY_CONTEXT*)malloc(sizeof(KEY_CONTEXT)); 
if (keyCtx == NULL) { 
return NULL; 
    } 
    SM4_set_key(key, &(keyCtx->sm4Key)); 
return (KEY_HANDLE)keyCtx; 
} 
int SDF_DestroyKey(SESSION_HANDLE hSession, KEY_HANDLE hKey) { 
free(hKey); 
return 0; 
}
 int SDF_Encrypt(SESSION_HANDLE hSession, KEY_HANDLE hKey, const uint8_t* 
plaintext, uint32_t plaintextLen, uint8_t* ciphertext, uint32_t* ciphertextLen) { 
if (plaintextLen % 16 != 0) { 
return -1; 
    } 
    KEY_CONTEXT* keyCtx = (KEY_CONTEXT*)hKey; 
for (uint32_t i = 0; i < plaintextLen; i += 16) { 
        SM4_encrypt(plaintext + i, ciphertext + i, &(keyCtx->sm4Key)); 
    } 
    *ciphertextLen = plaintextLen; 
return 0; 
} 
int SDF_Decrypt(SESSION_HANDLE hSession, KEY_HANDLE hKey, const uint8_t* 
ciphertext, uint32_t ciphertextLen, uint8_t* plaintext, uint32_t* plaintextLen) { 
if (ciphertextLen % 16 != 0) { 
return -1; 
    } 
    KEY_CONTEXT* keyCtx = (KEY_CONTEXT*)hKey; 
for (uint32_t i = 0; i < ciphertextLen; i += 16) { 
        SM4_decrypt(ciphertext + i, plaintext + i, &(keyCtx->sm4Key)); 
    } 
    *plaintextLen = ciphertextLen; 
return 0; 
} 
posted @ 2024-05-23 17:45  20211316郭佳昊  阅读(15)  评论(0编辑  收藏  举报