当前位置:网站首页>MD5 verification based on stm32
MD5 verification based on stm32
2022-06-24 16:08:00 【Domineering ocean】
introduction
We are developing embedded and single-chip products , It is often necessary to check some documents , To ensure that this file is not modified or damaged during transmission . such as IAP When upgrading the program , It is often necessary to verify the upgraded firmware .MD5 Is one of the most commonly used inspection methods . This article uses MD5 Inspection procedure , Yes STM32 Of Flash To test a piece of data in , After verification, it is stored in the string , Can be used to compare or output .
Introduce
MD5 Introduction and use scenario
MD5 check (checksum) It checks the correctness of data by performing hash operation on the received transmission data . A hash function , such as MD5, Is a one-way operation that converts an arbitrary length data string into a short fixed length value . Any two strings should not have the same hash value ( namely , Yes “ It's very likely that ” It's different , And it should be difficult to artificially create two strings with the same hash value ). One MD5 The checksum (checksum) Check the correctness of the data by performing hash operation on the received transmission data . The calculated hash value is compared with the hash value transmitted with the data . If the two values are the same , It indicates that the transmitted data is complete and correct 、 Have not been changed ( Provided that the hash value is not tampered with ), Thus, it can be safely used . MD5 Verification can be applied in many fields , For example, the inspection of confidential information , Verification of downloaded files , Encryption of plaintext password, etc .
MD5 principle
MD5 Encryption process , On the whole , Is to define four values first , Then use these four values , Calculate the original information , And get four new values , Then calculate the original text , Get four new values , So cycle a certain number of times , Finally, the last four values are simply concatenated , You get the final ciphertext . Mainly the following 3 Step :
- Fill in the information Match the length of the original text with digits 512 Seeking remainder , If it doesn't 448, Just fill in 448 position . Filling is the first filling 1, Fill in the back 0.512-448=64, Use the rest 64 position , Record the length of the original text . Finally, I get a completed information ( Chief, = Original length +512 position )
- Get the initial value Four initial values , yes MD5 This algorithm is defined in advance , Namely 4 individual 32 The value of a , Just in total 128 position . We use it ABCD name : A=0xefcdab89 B=0x89ABCDEF C=0x98badcfe D=0x10325476 3、 Real calculations The calculation is divided into multiple cycles , Each cycle , It's all used ABCD And the information completed in the first step of the original text , Calculate , Finally get a new ABCD. Finally, it will be the last time ABCD String , Is the final ciphertext . The loop is divided into main loop first , Each main loop is nested with sub loops . Number of main cycles = Original length /512. Number of sub cycles = 64 Time .
Software implementation
On the network MD5 There are many inspection procedures , But the implementation is in STM32 In fact, there are few that can be used . The program of this article is written by myself , And check the used .
Compression function
a = A, b = B, c = C, d = D;
FF(a, b, c, d, x[0], 7, 0xd76aa478);
FF(d, a, b, c, x[1], 12, 0xe8c7b756);
FF(c, d, a, b, x[2], 17, 0x242070db);
FF(b, c, d, a, x[3], 22, 0xc1bdceee);
FF(a, b, c, d, x[4], 7, 0xf57c0faf);
FF(d, a, b, c, x[5], 12, 0x4787c62a);
FF(c, d, a, b, x[6], 17, 0xa8304613);
FF(b, c, d, a, x[7], 22, 0xfd469501);
FF(a, b, c, d, x[8], 7, 0x698098d8);
FF(d, a, b, c, x[9], 12, 0x8b44f7af);
FF(c, d, a, b, x[10], 17, 0xffff5bb1);
FF(b, c, d, a, x[11], 22, 0x895cd7be);
FF(a, b, c, d, x[12], 7, 0x6b901122);
FF(d, a, b, c, x[13], 12, 0xfd987193);
FF(c, d, a, b, x[14], 17, 0xa679438e);
FF(b, c, d, a, x[15], 22, 0x49b40821);
GG(a, b, c, d, x[1], 5, 0xf61e2562);
GG(d, a, b, c, x[6], 9, 0xc040b340);
GG(c, d, a, b, x[11], 14, 0x265e5a51);
GG(b, c, d, a, x[0], 20, 0xe9b6c7aa);
GG(a, b, c, d, x[5], 5, 0xd62f105d);
GG(d, a, b, c, x[10], 9, 0x02441453);
GG(c, d, a, b, x[15], 14, 0xd8a1e681);
GG(b, c, d, a, x[4], 20, 0xe7d3fbc8);
GG(a, b, c, d, x[9], 5, 0x21e1cde6);
GG(d, a, b, c, x[14], 9, 0xc33707d6);
GG(c, d, a, b, x[3], 14, 0xf4d50d87);
GG(b, c, d, a, x[8], 20, 0x455a14ed);
GG(a, b, c, d, x[13], 5, 0xa9e3e905);
GG(d, a, b, c, x[2], 9, 0xfcefa3f8);
GG(c, d, a, b, x[7], 14, 0x676f02d9);
GG(b, c, d, a, x[12], 20, 0x8d2a4c8a);
HH(a, b, c, d, x[5], 4, 0xfffa3942);
HH(d, a, b, c, x[8], 11, 0x8771f681);
HH(c, d, a, b, x[11], 16, 0x6d9d6122);
HH(b, c, d, a, x[14], 23, 0xfde5380c);
HH(a, b, c, d, x[1], 4, 0xa4beea44);
HH(d, a, b, c, x[4], 11, 0x4bdecfa9);
HH(c, d, a, b, x[7], 16, 0xf6bb4b60);
HH(b, c, d, a, x[10], 23, 0xbebfbc70);
HH(a, b, c, d, x[13], 4, 0x289b7ec6);
HH(d, a, b, c, x[0], 11, 0xeaa127fa);
HH(c, d, a, b, x[3], 16, 0xd4ef3085);
HH(b, c, d, a, x[6], 23, 0x04881d05);
HH(a, b, c, d, x[9], 4, 0xd9d4d039);
HH(d, a, b, c, x[12], 11, 0xe6db99e5);
HH(c, d, a, b, x[15], 16, 0x1fa27cf8);
HH(b, c, d, a, x[2], 23, 0xc4ac5665);
II(a, b, c, d, x[0], 6, 0xf4292244);
II(d, a, b, c, x[7], 10, 0x432aff97);
II(c, d, a, b, x[14], 15, 0xab9423a7);
II(b, c, d, a, x[5], 21, 0xfc93a039);
II(a, b, c, d, x[12], 6, 0x655b59c3);
II(d, a, b, c, x[3], 10, 0x8f0ccc92);
II(c, d, a, b, x[10], 15, 0xffeff47d);
II(b, c, d, a, x[1], 21, 0x85845dd1);
II(a, b, c, d, x[8], 6, 0x6fa87e4f);
II(d, a, b, c, x[15], 10, 0xfe2ce6e0);
II(c, d, a, b, x[6], 15, 0xa3014314);
II(b, c, d, a, x[13], 21, 0x4e0811a1);
II(a, b, c, d, x[4], 6, 0xf7537e82);
II(d, a, b, c, x[11], 10, 0xbd3af235);
II(c, d, a, b, x[2], 15, 0x2ad7d2bb);
II(b, c, d, a, x[9], 21, 0xeb86d391);
A += a;
B += b;
C += c;
D += d;
Packet read
uint8_t j, k;
memset(x, 0, 64);
ulSampleIndex = 0;
for (j = 0; j < 16; j++)
{
for (k = 0; k < 4; k++)
{
if ((ulReadCnt >= ulFlieLength / 1024) && (ulDataIndex >= ulFlieLength % 1024))
break;
((char *)x)[ulSampleIndex] = ucaFlashBuf[ulDataIndex];
ulDataIndex++;
ulSampleIndex++;
}
}
Application function
uint16_t usCnt = 0;
uint32_t ulFileLen[2] = {0};
/* Calculate the integer part */
for (ulReadCnt = 0; ulReadCnt < ulFlieLength / 1024; ulReadCnt++)
{
Read your file 1024 byte ;
ulFlashAdd += 1024;
for (usCnt = 0; usCnt < 16; usCnt++)
{
ReadGroupTempBuf(ulFlieLength);
MD5();
}
ulDataIndex = 0;
}
/* Calculate the remainder */
memset(ucaFlashBuf, 0, 1025);
Read the file length of your file 1024 Divide the remainder by 2 Bytes ;
ReadGroupTempBuf(ulFlieLength);
for (usCnt = 0; usCnt < (ulFlieLength % 1024) / 64; usCnt++)
{
MD5();
ReadGroupTempBuf(ulFlieLength);
}
/* Supplement at the end of the document 1, repair 0 operation ,128 Binary is 10000000 */
((char *)x)[ulFlieLength % 64] = 128;
if (ulFlieLength % 64 > 55)
{
MD5(), memset(x, 0, 64);
}
/* At the end of the document, add the... Of the original document bit length */
ulFileLen[1] = ulFlieLength / 0x20000000;
ulFileLen[0] = (ulFlieLength % 0x20000000) * 8;
memcpy(x + 14, ulFileLen, 8);
MD5();
sprintf(pEsult, "%08X%08X%08X%08X", PP(A), PP(B), PP(C), PP(D));
The whole program
Here's how to get it :
- https://blog.csdn.net/qq_44629109/category_11627212.html
- https://download.csdn.net/download/qq_44629109/85676113
MD5 Tools
Download the following MD5 Tools , You can directly put the MD5 Convert it out , We can use it to test our program MD5 Is the value right . https://download.csdn.net/download/qq_44629109/85677447
边栏推荐
- How to implement SQLSERVER database migration in container
- Golang+redis reentrant lock
- Cap: multiple attention mechanism, interesting fine-grained classification scheme | AAAI 2021
- Paper: Google TPU
- Pytorch transpose convolution
- Implement Domain Driven Design - use ABP framework - domain logic & application logic
- 一文详解JackSon配置信息
- 打破内存墙的新利器成行业“热搜”!持久内存让打工人也能玩转海量数据+高维模型
- One article explains Jackson configuration information in detail
- 2021-05-03: given a non negative integer num, how to avoid circular statements,
猜你喜欢
如何轻松实现在线K歌房,与王心凌合唱《山海》
Linux record -4.22 MySQL 5.37 installation (supplementary)
Recommend several super practical data analysis tools
My network relationship with "apifox"
Still worried about missing measurements? Let's use Jacobo to calculate the code coverage
Logging is not as simple as you think
Why is it easy for enterprises to fail in implementing WMS warehouse management system
Build go command line program tool chain
存在安全隐患 路虎召回部分混动揽运
一文详解JackSon配置信息
随机推荐
April 23, 2021: there are n cities in the TSP problem, and there is a distance between any two cities
clang: warning: argument unused during compilation: ‘-no-pie‘ [-Wunused-command-line-argument]
Here comes Wi Fi 7. How strong is it?
我与“Apifox”的网络情缘
安装ImageMagick7.1库以及php的Imagick扩展
Summary of common tools and usage
存在安全隐患 路虎召回部分混动揽运
存在安全隐患 部分冒险家混动版将召回
用 Oasis 开发一个跳一跳(一)—— 场景搭建
nifi从入门到实战(保姆级教程)——环境篇
一文详解JackSon配置信息
Pytorch 转置卷积
Global and Chinese market of computer protective film 2022-2028: Research Report on technology, participants, trends, market size and share
How does the effective date of SAP PP ECM affect the work order?
Recommend several super practical data analysis tools
Using oasis to develop a hop by hop (I) -- Scene Building
Cap: multiple attention mechanism, interesting fine-grained classification scheme | AAAI 2021
Pytorch transpose convolution
[C language questions -- leetcode 12 questions] take you off and fly into the garbage
Install the imagemagick7.1 library and the imageick extension for PHP