MBEDTLS_ENTROPY_HARDWARE_ALT
时间: 2024-04-23 15:20:26 浏览: 24
MBEDTLS_ENTROPY_HARDWARE_ALT是mbed TLS库中的一个选项,用于指定使用硬件熵源增加随机性。在密码学中,随机性是非常重要的,因为它用于生成密钥、初始化向量等。mbed TLS库提供了多种方法来获取随机性,其中之一就是使用硬件熵源。
硬件熵源是指基于物理过程的随机性源,例如温度变化、电子噪声等。相比于软件生成的随机数,硬件熵源提供的随机性更高,更难以预测和猜测。
当开发者选择使用MBEDTLS_ENTROPY_HARDWARE_ALT选项时,mbed TLS库将尝试使用硬件熵源来增加随机性。这需要硬件平台支持,并且需要在库的配置中进行相应的设置。
相关问题
platform entropy sources only work on unix and windows, see mbedtls_no_platf
platform entropy sources only work on unix and windows,see mbedtls_no_platform_entropy_sources
平台熵源仅在Unix和Windows上运行,参见mbedtls_no_platform_entropy_sources。
mbedtls_no_platform_entropy_sources是一个用于mbed TLS库的选项或宏定义。该选项指示在无法使用平台提供的熵源时,禁用库内部的平台熵源。
在Unix和Windows操作系统上,mbed TLS库能够利用操作系统提供的熵源来生成随机数。这些熵源可能包括硬件设备的噪声、键盘敲击、鼠标移动等。这些熵源产生的随机数可用于加密、生成密钥或其他需要高质量随机数的安全操作。
然而,并非所有操作系统都提供了可靠的平台熵源。对于这些操作系统,mbed TLS库的mbedtls_no_platform_entropy_sources选项允许用户禁用平台熵源,并通过其他手段提供随机数。
在使用mbedtls_no_platform_entropy_sources选项时,用户需要提供其他可靠的熵源,以便库能够生成高质量的随机数。这些熵源可以是硬件设备的噪声、外部硬件随机数发生器、专用的随机数模块等。
总之,platform_entropy_sources_only_work_on_unix_and_windows选项/宏定义告诉我们,mbed TLS库的平台熵源仅在Unix和Windows操作系统上运行。对于其他操作系统,用户可以使用mbedtls_no_platform_entropy_sources选项来禁用平台熵源,并提供其他可靠的熵源来满足随机数生成的需求。
写一个基于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证书文件。