Design & Development

The main source of fun for this blogger!

OpenSSL EVP API MD5 implementation in C++

Most examples found online use the old API. The newer versions of OpenSSL recommend using the EVP family of interfaces, so here I am recording MD5 hash implementation code using EVP.

#include "openssl/conf.h"
#include "openssl/evp.h"
#include "openssl/err.h"
#include "openssl/engine.h"

void handle_errors()
{
	ERR_print_errors_fp(stderr);
	abort();
}

std::string md5_str(const std::string & str)
{
	const auto * md = EVP_get_digestbyname("MD5");
	if (nullptr == md)
	{
		std::cerr << "Failed to EVP_get_digestbyname MD5!" << std::endl;
		return "";
	}

	unsigned char buf[EVP_MAX_MD_SIZE] = {};
	unsigned int olen = EVP_MAX_MD_SIZE;

	EVP_MD_CTX * ctx = EVP_MD_CTX_new();
	if (nullptr == ctx)
	{
		std::cerr << "Failed to EVP_MD_CTX_new!" << std::endl;
		return "";
	}
	if (1 != EVP_DigestInit(ctx, md))
	{
		handle_errors();
		return "";
	}
	if (1 != EVP_DigestUpdate(ctx, str.data(), str.length()))
	{
		handle_errors();
		return "";
	}
	if (1 != EVP_DigestFinal(ctx, buf, &olen))
	{
		handle_errors();
		return "";
	}
	EVP_MD_CTX_free(ctx);

	return { reinterpret_cast<char*>(buf), olen };
}

int main()
{
	OpenSSL_add_all_digests();
	const std::string plain = "test1234";
	const std::string hash = md5_str(plain);
	std::cout << "plain: " << plain << " md5 hash: " << hash << std::endl;
	return EXIT_SUCCESS;
}

I will add EVP implementations for AES and others later when I have time.

About the Si4735 SSB Patch (pu2clr open source project)

Recently I have been experimenting with building a digital radio using Arduino + Si4735 + pu2clr/SI4735. The FM functionality is basically working fine. Thanks to Ricardo Lima Caratti, the author of the pu2clr open source project, for providing such a convenient library. With correct circuit connections, the features can be implemented almost instantly.

After some simple testing, I decided to challenge the so‑called SSB single sideband patch. I ran into a small pitfall. Using the pu2clr example code: https://github.com/pu2clr/SI4735/blob/master/examples/TOOLS/SI47XX_09_SAVE_SSB_PATCH_EEPROM/SI47XX_09_SAVE_SSB_PATCH_EEPROM.ino, I attempted to write the SSB patch into an AT24C256 EEPROM. However, after executing the code, the data written was incorrect. All verification reads returned 0xFF. The patch name displayed in Arduino IDE Serial Monitor was garbled, and the size showed as 65535, corresponding to FF error data.

Continue reading…

The source file is different from when the module was built.

*******************************

Source file: D:\Projects\StereoMatch\stereomatcher.cpp

Module: D:\Projects\StereoMatch\Debug\StereoMatch.exe

Process: [4024] StereoMatch.exe

The source file is different from when the module was built. Would you like the debugger to use it anyway?

*******************************

***********************************

At StereoMatcher.cpp, line 166 (‘ComputeCorrespondence()’, line 128)

The breakpoint will not currently be hit. The source code is different from the original version.

Continue reading…

W801 (W80X_SDK_v1.00.10) https request mbedtls error 0x7780 issue and solution record

The issue started when I wanted to use the W801 to detect a GPIO level change and then send a bot notification message through Telegram. However, when calling my own Telegram API reverse proxy (caddy v2.6.4 h1:2hwYqiRwk1tf3VruhMpLcYTg+11fCdr8S3jhNAdnPy8=), the serial log showed the SSL handshake error 0x7780 mentioned in the title.

The code was directly copied from the SDK example demo wm_https_demo.c. The error occurred during the handshake, specifically when calling HTTPWrapperSSLConnect, as shown:

wm_printf("step 1: ssl connect to...\r\n");
ret = HTTPWrapperSSLConnect(&ssl_p, fd, (const struct sockaddr *)&server, sizeof(server), HTTPS_DEMO_SERVER);
if (ret < 0)
{
    wm_printf("https connect error\r\n");
    close(fd);
    break;
}

At first, the serial output only showed step 1: ssl connect to…, then immediately the 0x7780 error, followed by https connect error and request failure. After studying the mbedtls library used internally, I found that more detailed debug logs were available. You need to enable the macro define MBEDTLS_DEBUG_C in SDK src/app/mbedtls/include/mbedtls/config.h (and make sure the makefile project rebuilds the lib target. If using my previous CLion IDE setup, select the lib configuration and rebuild). After enabling it, you can see error messages similar to those mentioned here: https://forums.mbed.com/t/error-0x7780-during-handshake/6883

Continue reading…

A strange reason for QRhiGraphicsPipeline create failure

Recently while learning the Qt RHI graphics rendering framework I encountered a strange problem: QRhiGraphicsPipeline inexplicably returned false on create! During the time this problem appeared, I was debugging the fragment shader, specifically studying HDR display shaders, and frequently toggling Windows HDR on and off. At one point I thought it was a system issue, so I tried switching to SDR, but the error persisted.

Later, after spending some time reverting modifications, I finally isolated the cause…

First, note that the program framework was basically copied from the Qt official documentation example: https://doc.qt.io/qt-6/qtgui-rhiwindow-example.html. The error occurs at the following code location:

Continue reading…

FFmpeg native script building Windows debug version debugging and tracing issues

This time I want to record what feels like a rather hardcore problem. Due to the title length limit, it’s impossible to accurately express all the issues. For example, when debugging FFmpeg code with breakpoints and single‑stepping, the code does not execute in order, jumps around unpredictably, and when checking local variables near a breakpoint, their values cannot be displayed correctly, such as this:

The local variable “offset” cannot show its current value and instead displays “Variable is optimized away and not available.” From the literal meaning, we can guess that the compiler optimized the code, such as inlining and removing intermediate variables, so they can no longer be seen.

However, FFmpeg’s configure parameters for building the debug version do include:

–disable-optimizations –enable-debug –disable-stripping

These officially documented debug build parameters. So why do we still encounter all the debugging obstacles described above, which look like the result of compiler optimizations?

Continue reading…