Tea encrypted communication on Single Chip Microcomputer
For more information, see http://blog.csdn.net/jdh99, :jdh.
Environment:
HOST: win7
Development Environment: mdk4.72
MCU: stm32
Note:
In the project, the single-chip microcomputer will communicate with the server over the network. to encrypt the communication, I chose the tea encryption algorithm.
Source code:
Tea. h
/*************************************** * ****************************** Tea algorithm header file * (c) copyright 2013, jdh * all right reserved * file name: tea. H * programmer: jdh *************************************** *******************************//******* **************************************** * *********************** note: tea encryption and decryption algorithm * tea (Tiny Encryption Algorithm) is a simple and efficient encryption algorithm. It is famous for its fast encryption and decryption speed and its * implementation is simple. * The algorithm is very simple. The tea algorithm can operate 64-bit (8-byte) each time and uses 128-bit (16-byte) as the key. * The algorithm uses an iterative method, * The number of recommended iterations is 64, with a minimum of 32. **************************************** * ****************************/# Ifndef _ tea_h _ # DEFINE _ tea_h _/************************************** ******* **************************************** * **********************/# include "header. H "/************************************* ****** **************************************** ************************//************** **************************************** * tea encryption * parameter: v: The data to be encrypted. The length is 8 bytes * K: The key used for encryption, the length is 16 bytes ************************************ * *******************************/static void tea_encrypt (uint32_t * V, uint32_t * k ); /*************************************** * tea decryption * parameter: v: The data to be decrypted. The length is 8 bytes * K: The key used for decryption, the length is 16 bytes ************************************ * *******************************/static void tea_decrypt (uint32_t * V, uint32_t * k ); /*************************************** * encryption algorithm * parameters: SRC: source data, which occupies a multiple of 8 bytes. after encryption, the ciphertext is also stored in the * size_src: size of the source data, in bytes * Key: Key, 16 bytes *, and return: number of ciphertext bytes ************************************ * *******************************/uint16_t encrypt (uint8_t * SRC, uint16_t size_src, uint8_t * Key ); /***************************************: SRC: source data, which occupies a multiple of 8 bytes. after decryption, the plaintext is also stored in this * size_src: size of the source data, in bytes * Key: Key, 16 bytes * return: the number of bytes of the plaintext. If the decryption fails, returns 0 ************************************** * *****************************/uint16_t decrypt (uint8_t * SRC, uint16_t size_src, uint8_t * Key); # endif
Tea. c
/*************************************** * ******************************* Main tea algorithm file * (c) copyright 2013, jdh * all right reserved * file name: hash. C * programmers: jdh *************************************** *******************************//******* **************************************** * ********************** header file **************** **************************************** * *************/# include "tea. H "/*************************** **************************************** ************************************ **********************************//**** **************************************** * ************************** Tea encryption * parameter: v: The data to be encrypted. The length is 8 bytes * K: The key used for encryption, the length is 16 bytes ************************************ * *******************************/static void tea_encrypt (uint32_t * V, uint32_t * k) {uint32_t y = V [0], Z = V [1], sum = 0, I; uint32_t de LTA = 0x9e3779b9; uint32_t A = K [0], B = K [1], c = K [2], D = K [3]; for (I = 0; I <32; I ++) {sum + = delta; y + = (z <4) + a) ^ (Z + sum) ^ (z> 5) + B); Z + = (Y <4) + C) ^ (Y + sum) ^ (Y> 5) + d);} V [0] = y; V [1] = z ;} /*************************************** * tea decryption * parameter: v: The data to be decrypted. The length is 8 bytes * K: The key used for decryption, the length is 16 bytes ************************************ ****************************** * ***/Static void tea_decrypt (uint32_t * V, uint32_t * k) {uint32_t y = V [0], Z = V [1], sum = 0xc6ef3720, I; uint32_t Delta = 0x9e3779b9; uint32_t A = K [0], B = K [1], c = K [2], D = K [3]; for (I = 0; I <32; I ++) {z-= (Y <4) + C) ^ (Y + sum) ^ (Y> 5) + d ); y-= (z <4) + a) ^ (Z + sum) ^ (z> 5) + B); sum-= delta ;} V [0] = y; V [1] = z ;} /*************************************** ************************** * Encryption algorithm * parameter: SRC: source data. The occupied space must be a multiple of 8 bytes. after encryption, the ciphertext is also stored in the * size_src: size of the source data, in bytes * Key: Key, 16 bytes *, and return: number of ciphertext bytes ************************************ * *******************************/uint16_t encrypt (uint8_t * SRC, uint16_t size_src, uint8_t * Key) {uint8_t a = 0; uint16_t I = 0; uint16_t num = 0; // fill the plaintext with a multiple of 8 bytes a = size_src % 8; if (! = 0) {for (I = 0; I <8-A; I ++) {SRC [size_src ++] = 0 ;}} // encrypt num = size_src/8; for (I = 0; I <num; I ++) {tea_encrypt (uint32_t *) (SRC + I * 8 ), (uint32_t *) Key);} return size_src ;} /***************************************: SRC: source data, which occupies a multiple of 8 bytes. after decryption, the plaintext is also stored in this * size_src: size of the source data, in bytes * Key: Key, 16 bytes * return: the number of bytes of the plaintext. If the decryption fails, returns 0 ************************************** ************************ * ******/Uint16_t decrypt (uint8_t * SRC, uint16_t size_src, uint8_t * Key) {uint16_t I = 0; uint16_t num = 0; // determine whether the length is a multiple of 8 If (size_src % 8! = 0) {return 0 ;}// decrypt num = size_src/8; for (I = 0; I <num; I ++) {tea_decrypt (uint32_t *) (SRC + I * 8), (uint32_t *) Key);} return size_src ;}
Sample encryption code:
I = 0; // arr [I ++] = device> 8; arr [I ++] = device; // command word arr [I ++] = node. CMD> 8; arr [I ++] = node. CMD; // serial number if (node. index) {arr [I ++] = node. index> 8; arr [I ++] = node. index;} else {arr [I ++] = tx_index> 8; arr [I ++] = tx_index; tx_index ++ ;} // user ID: arr [I ++] = user_id> 24; arr [I ++] = user_id> 16; arr [I ++] = user_id> 8; arr [I ++] = user_id; // password // determine whether to confirm password change if (node. CMD! = Pai_net_confirm_edit_password) {arr [I ++] = PASSWORD> 24; arr [I ++] = PASSWORD> 16; arr [I ++] = PASSWORD> 8; arr [I ++] = password;} else {arr [I ++] = (uint8_t) (password_backdoor> 24); arr [I ++] = (uint8_t) (password_backdoor> 16); arr [I ++] = (uint8_t) (password_backdoor> 8); arr [I ++] = (uint8_t) password_backdoor ;} // message length: arr [I ++] = node. size> 8; arr [I ++] = node. size; // packet CRC = crc_code (node. buf, node. size); arr [I ++] = CRC> 8; arr [I ++] = CRC; // message memcpy (ARR + len_frame_head_net, node. buf, node. size); // the size of the encrypted message = encrypt (ARR, node. size + len_frame_head_net, (uint8_t *) Key); // send inf_w5100_write_data (socket0, arr, size, node. IP, port_server );
Decryption sample code:
// Decrypt data if (decrypt (BUF, MSG. socket_msg [I]. Size, (uint8_t *) Key) = 0) {// decrypt failed break ;}