在mbedtls中使用mbedtls_ctr_drbg_seed 函数需要开启那些宏定义,怎么验证函数是否好用
时间: 2024-03-30 15:33:47 浏览: 344
要使用 mbedtls_ctr_drbg_seed 函数,需要开启以下宏定义:
- MBEDTLS_CTR_DRBG_C:开启 CTR_DRBG 随机数生成器模块;
- MBEDTLS_ENTROPY_C:开启熵源模块;
- MBEDTLS_SHA256_C:开启 SHA-256 哈希算法模块(可选,用于增加熵源的安全性)。
可以在 mbedtls 的 `config.h` 文件中进行宏定义开启。
要验证 mbedtls_ctr_drbg_seed 函数是否好用,可以进行以下测试:
1. 使用 mbedtls_entropy_add_source 函数添加熵源;
2. 使用 mbedtls_ctr_drbg_seed 函数初始化随机数生成器;
3. 使用 mbedtls_ctr_drbg_random 函数生成随机数;
4. 验证生成的随机数是否满足要求,例如是否符合随机性要求、是否符合期望分布等。
可以编写测试用例进行验证。同时,也可以参考 mbedtls 提供的测试用例进行测试,以确保 mbedtls_ctr_drbg_seed 函数的正确性和可靠性。
相关问题
MBEDTLS_CTR_DRBG_MAX_SEED_INPUT
MBEDTLS_CTR_DRBG_MAX_SEED_INPUT是一个常量,用于定义mbed TLS中的CTR_DRBG伪随机数生成器的最大种子输入长度。CTR_DRBG是一种基于计数器的确定性随机比特生成器,用于生成伪随机数。
该常量的值表示CTR_DRBG种子输入的最大长度,超过这个长度的输入将被截断或拒绝。具体的数值取决于mbed TLS库的版本和配置。
CTR_DRBG的种子输入是用于初始化生成伪随机数的内部状态的数据。种子输入应该是足够随机和安全的,以确保生成的伪随机数具有高度的不可预测性和安全性。
写一个基于mbed TLS实现"MBEDTLS_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256"cipher suite的示例代码
以下是基于mbed TLS实现"MBEDTLS_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256"cipher suite的示例代码:
```c
#include "mbedtls/config.h"
#include "mbedtls/platform.h"
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/ssl.h"
#include "mbedtls/net_sockets.h"
#include "mbedtls/error.h"
#include "mbedtls/x509_crt.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SERVER_NAME "www.example.com"
#define SERVER_PORT "443"
int main(int argc, char *argv[])
{
int ret;
mbedtls_entropy_context entropy;
mbedtls_ctr_drbg_context ctr_drbg;
mbedtls_ssl_context ssl;
mbedtls_ssl_config ssl_conf;
mbedtls_x509_crt cacert;
mbedtls_net_context server_fd;
char buf[1024];
const char *pers = "mbedtls_tls_dhe_rsa_with_aes_128_gcm_sha256";
mbedtls_ssl_init(&ssl);
mbedtls_ssl_config_init(&ssl_conf);
mbedtls_ctr_drbg_init(&ctr_drbg);
mbedtls_entropy_init(&entropy);
mbedtls_x509_crt_init(&cacert);
// Initialize the entropy pool and the random number generator
if ((ret = mbedtls_entropy_add_source(&entropy, NULL, 0, MBEDTLS_ENTROPY_SOURCE_STRONG)) != 0) {
printf("mbedtls_entropy_add_source returned %d\n", ret);
goto exit;
}
if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, (const unsigned char *) pers, strlen(pers))) != 0) {
printf("mbedtls_ctr_drbg_seed returned %d\n", ret);
goto exit;
}
// Load the trusted CA certificates
if ((ret = mbedtls_x509_crt_parse_file(&cacert, "ca.crt")) != 0) {
printf("mbedtls_x509_crt_parse_file returned %d\n", ret);
goto exit;
}
// Initialize the SSL/TLS context and configure it for client use
if ((ret = mbedtls_ssl_config_defaults(&ssl_conf, MBEDTLS_SSL_IS_CLIENT, MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
printf("mbedtls_ssl_config_defaults returned %d\n", ret);
goto exit;
}
mbedtls_ssl_conf_authmode(&ssl_conf, MBEDTLS_SSL_VERIFY_REQUIRED);
mbedtls_ssl_conf_ca_chain(&ssl_conf, &cacert, NULL);
mbedtls_ssl_conf_rng(&ssl_conf, mbedtls_ctr_drbg_random, &ctr_drbg);
mbedtls_ssl_conf_ciphersuites(&ssl_conf, mbedtls_ssl_list_ciphersuites());
// Set up the SSL/TLS context for the connection to the server
if ((ret = mbedtls_ssl_setup(&ssl, &ssl_conf)) != 0) {
printf("mbedtls_ssl_setup returned %d\n", ret);
goto exit;
}
if ((ret = mbedtls_ssl_set_hostname(&ssl, SERVER_NAME)) != 0) {
printf("mbedtls_ssl_set_hostname returned %d\n", ret);
goto exit;
}
// Connect to the server
if ((ret = mbedtls_net_connect(&server_fd, SERVER_NAME, SERVER_PORT, MBEDTLS_NET_PROTO_TCP)) != 0) {
printf("mbedtls_net_connect returned %d\n", ret);
goto exit;
}
mbedtls_ssl_set_bio(&ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL);
// Perform the SSL/TLS handshake with the server
while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) {
if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
printf("mbedtls_ssl_handshake returned %d\n", ret);
goto exit;
}
}
// Send a request to the server
sprintf(buf, "GET / HTTP/1.1\r\nHost: %s\r\n\r\n", SERVER_NAME);
if ((ret = mbedtls_ssl_write(&ssl, (const unsigned char *) buf, strlen(buf))) != strlen(buf)) {
printf("mbedtls_ssl_write returned %d\n", ret);
goto exit;
}
// Receive the response from the server
do {
memset(buf, 0, sizeof(buf));
ret = mbedtls_ssl_read(&ssl, (unsigned char *) buf, sizeof(buf) - 1);
if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
continue;
}
if (ret <= 0) {
break;
}
printf("%s", buf);
} while (1);
exit:
mbedtls_x509_crt_free(&cacert);
mbedtls_ssl_config_free(&ssl_conf);
mbedtls_ssl_free(&ssl);
mbedtls_ctr_drbg_free(&ctr_drbg);
mbedtls_entropy_free(&entropy);
mbedtls_net_free(&server_fd);
return ret;
}
```
注意:在运行该示例代码前,需要将ca.crt替换为实际的CA证书文件。
阅读全文