Location and solution of cryptographic library under different bits

Promote the cause of problem solving

  1. Because the project is to do data security, SO the corresponding business module code using C++ to achieve, and to generate SO (dynamic) library arch Java to call. Compatibility for 64-bit environments was not covered in previous versions of the project and has been used under Android’s Armabi and Armeabi-V7A. Since Google Marketplace updated the requirement that GooglePlay must provide support for 64-bit dynamic libraries after 20190801, the issue was delayed because the issue was not clearly identified, as it did not affect domestic use.
  2. As the domestic application market is clearly demanding mandatory 64-bit support, see the following figure:

  1. Therefore, the priority of solving this problem is also ranked in the forefront, in line with the idea that there is no problem that can not be solved, to concentrate on deep digging, will eventually be solved.

Methods for locating problems

  • Firstly, the C++ code involved in the encryption library is built easily, and the business is only concerned with encryption. Then, the debugging is carried out, and the memory data is analyzed when the C++ code is running through LLDB (LowLevelDebugger) dynamic debugging tool in AndroidStuido. As shown in figure:

  • Memory read –size 4 –format x –count 8 0x29a9e420 Memory read-s4-FX-c8 0x29a9e420

1. Size: represents the number of bytes per displayed value. Use 1, 2, 4, 8, etc., but match the displayed data. Format: indicates the display type. X indicates the hexadecimal format. You can also specify f (float), d (int), u(unsigned int), etc. 3. Count: Indicates the number of displayed values. 4. 0x29A9E420: indicates the memory address value to be debugged.Copy the code

Encrypting values in 32 bits versus 64 bits

Points to note

  • It is important to know whether the number of bits of the handset processor is 32 or 64 when debugging the C++ code dynamically, because 32-bit phones can be dynamically debugged in armeabi-v7a, but not in arm64-v8a, and vice versa. So this feature needs to be taken care of so as not to cause unnecessary confusion. The relative configuration is shown as follows:

Troubleshooting and Locating

  • To prepare the corresponding test data according to the encryption rules, here I prepared a 16byte file, the purpose is to see how its value in the 32-bit environment is different from that in the 64-bit! Test data is shown in figure:

  • After the test data was ready, the dynamic debugging of the encryption process began. After the debugging of mobile phones with different bits, it was found that the ULBUF variable holding the encryption value was different. The DEFINITION of ULBUF is shown in the figure:

  • In the implementationSMS4F functionBefore,uint32 ulbuf[36]The initial value of is the same in 32-bit as in 64-bit.Ulbuf Specifies the number in memory before the 32-bit SMS4F function is executedAs shown in figure:

Ulbuf Number in memory before 64-bit SMS4F function executionAs shown in figure:

  • Ulbuf after the SMS4F function is executed in 32-bit

Ulbuf memory values after 64-bit SMS4F function execution are shown in the following figure:

  • Ulbuf is defined as a uint32 (defined as an unsigned long int in macros). The loop condition is defined as I < 32. So just look at the first 128 bytes.

Solution and Summary

  • After comparison, the ulBUF of 32-bit and 64-bit after the execution of SMS4F function is significantly different after 0x52 in the original text. Through the code ulbuf[I +4], it is found that every execution of SMS4F function is promoted by 4byte. At this point, it becomes clear that the unsigned long int defined by the uint32 macro is a bit tricky, since it has a value of 4byte and 8byte in 32-bit and 64-bit environments, respectively. The previous definition is shown in figure:

  • The revised macro definition uint32 is shown in the following figure:

  • The previous steps locate the core problem, and the problem that the encryption and decryption cannot be normal in the scenario where the 64-bit processor (ARM64-V8A) is configured is solved. Also found C/C++ in AndroidStudio strange new troubleshooting and solutions, not only to view Variables parameters, Log and other schemes, as well as through the LLDB tool to locate and resolve “difficult diseases”.

  • I hope this article will help and inspire you to locate similar problems. If you have any deficiencies or modifications, please leave a message and let me know. Thank you very much.

    For a long time there was Ling Yunzhi, heavy on Jinggangshan. A thousand miles to find the old place, the old face is new. Everywhere warbler yan Dance, more babbling water, high road into the clouds. Beyond the yellow sea, danger is not to see. The wind is thundering, banners are ringing, it is human. Thirty-eight years have passed. Can be on the nine days to catch the moon, under the five seas turtle, laugh and triumph also. Nothing is difficult to the man who will try.Copy the code