嵌入式系统:用mbedtls 3.6实现RSA - 2048 PKCS1验签功能的C代码
# RSA-2048 PKCS1 验签实现 (mbedtls 3.6) 以下是一个使用mbedtls 3.6库实现RSA-2048 PKCS1验签功能的C语言代码示例。这个代码适用于嵌入式系统,公钥以模数(n)和指数(e)的uint8数组形式给出。 ```c #include <string.h> #include "mbedtls/pk.h" #include "mbedtls/md.h" #include "mbedtls/error.h" #define RSA_KEY_SIZE 256 // 2048 bits = 256 bytes int rsa_verify_pkcs1( const uint8_t *n, // RSA modulus (big-endian) const uint8_t *e, // RSA public exponent (big-endian) size_t e_len, // Length of public exponent const uint8_t *msg, // Message to verify size_t msg_len, // Message length const uint8_t *sig, // Signature to verify size_t sig_len, // Signature length (must be RSA_KEY_SIZE) int hash_alg // MBEDTLS_MD_XXX (e.g. MBEDTLS_MD_SHA256) ) { int ret = 0; mbedtls_pk_context pk; mbedtls_mpi mpi_n, mpi_e; // Initialize structures mbedtls_pk_init(&pk); mbedtls_mpi_init(&mpi_n); mbedtls_mpi_init(&mpi_e); // Import modulus (n) and exponent (e) into MPI if ((ret = mbedtls_mpi_read_binary(&mpi_n, n, RSA_KEY_SIZE)) != 0) { printf("Failed to read modulus: -0x%04X\n", -ret); goto cleanup; } if ((ret = mbedtls_mpi_read_binary(&mpi_e, e, e_len)) != 0) { printf("Failed to read exponent: -0x%04X\n", -ret); goto cleanup; } // Setup PK context with RSA key if ((ret = mbedtls_pk_setup(&pk, mbedtls_pk_info_from_type(MBEDTLS_PK_RSA))) != 0) { printf("Failed to setup PK context: -0x%04X\n", -ret); goto cleanup; } // Set the RSA key components (only n and e needed for verification) if ((ret = mbedtls_rsa_import(mbedtls_pk_rsa(pk), &mpi_n, NULL, NULL, NULL, &mpi_e)) != 0) { printf("Failed to import RSA key: -0x%04X\n", -ret); goto cleanup; } // Complete the RSA key setup if ((ret = mbedtls_rsa_complete(mbedtls_pk_rsa(pk))) != 0) { printf("Failed to complete RSA key: -0x%04X\n", -ret); goto cleanup; } // Set padding to PKCS1 v1.5 if ((ret = mbedtls_rsa_set_padding(mbedtls_pk_rsa(pk), MBEDTLS_RSA_PKCS_V15, hash_alg)) != 0) { printf("Failed to set padding: -0x%04X\n", -ret); goto cleanup; } // Verify the signature ret = mbedtls_pk_verify(&pk, hash_alg, msg, msg_len, sig, sig_len); if (ret != 0) { printf("Verification failed: -0x%04X\n", -ret); } cleanup: mbedtls_mpi_free(&mpi_n); mbedtls_mpi_free(&mpi_e); mbedtls_pk_free(&pk); return ret; } // Example usage: int example_usage() { uint8_t n[RSA_KEY_SIZE] = { /* your modulus here */ }; uint8_t e[] = { /* your exponent here, typically 0x01, 0x00, 0x01 for 65537 */ }; uint8_t message[] = "This is the message to verify"; uint8_t signature[RSA_KEY_SIZE] = { /* the signature to verify */ }; int ret = rsa_verify_pkcs1( n, e, sizeof(e), message, sizeof(message) - 1, // -1 to exclude null terminator signature, sizeof(signature), MBEDTLS_MD_SHA256 ); if (ret == 0) { printf("Signature is valid\n"); } else { printf("Signature is invalid\n"); } return ret; } ``` ## 注意事项 1. 确保你的mbedtls配置启用了RSA和相应的哈希算法(如SHA-256),通常在`mbedtls_config.h`中配置。 2. 输入的公钥模数(n)和指数(e)必须是大端格式(big-endian),这是mbedtls的默认格式。 3. 签名长度必须与RSA密钥大小匹配(2048位=256字节),否则验签会失败。 ######[AI写代码神器 | 1210点数解答 | 2025-07-10 10:09:35]
- Python 实现简易密码设置系统:多种功能交互与安全要点揭秘(字节豆包 | 286点数解答 | 2025-03-28 10:43:21)375
- C语言实现多功能密码设置系统:含代码及安全注意要点(字节豆包 | 414点数解答 | 2025-03-28 10:43:55)362
- C 语言实现:超简单密码设置系统,含代码与注意要点!(字节豆包 | 519点数解答 | 2025-03-28 12:42:02)399
- C语言实现密码管理系统:含功能代码与安全注意事项(字节豆包 | 409点数解答 | 2025-04-05 18:09:56)348
- C语言实现多功能密码系统:初始化、确认、设置、显示、重置与退出(字节豆包 | 454点数解答 | 2025-04-05 18:48:42)346
- C语言实现密码管理系统:含初始化、确认、设置、显示、重置及退出功能(字节豆包 | 413点数解答 | 2025-04-06 15:37:16)293
- C 语言实现密码管理系统:含初始化、确认、设置、显示、重置及退出功能(字节豆包 | 403点数解答 | 2025-04-06 15:37:54)307
- 嵌入式系统:用mbedtls 3.6实现RSA - 2048 PKCS1验签功能的C代码(DeepSeek | 1210点数解答 | 2025-07-10 10:09:35)144
- 嵌入式系统:用mbedtls 3.6实现RSA - 2048 PKCS1验签功能(公钥以十六进制字符串给出)(DeepSeek | 1538点数解答 | 2025-07-10 10:11:44)71
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)362
- 2048 游戏 HTML 代码框架解析与优化建议(字节豆包 | 138点数解答 | 2025-03-05 15:45:36)389
- 2048 游戏 HTML 框架搭建,后续开发建议大揭秘!(字节豆包 | 117点数解答 | 2025-03-11 00:06:25)340