Ansi-x99mac algorithm and the PBoC's 3DES mac algorithm

Source: Internet
Author: User

As long as there are standard DES encryption and decryption algorithms, similar to the ANSI-X99MAC algorithm and pboc3des algorithm is very good implementation. They are all using DES algorithm and then through a layer of algorithm implementation. The principle of implementation to see the picture can be understood. The implementation of the 3DES algorithm is much simpler. Is the DES algorithm and decryption once more.

/************************************************************************************************************** Function prototype: void Curcalc_3des_encrypt (U08 *inkey, U08 *indata, U08 *outdata) * * Function function: 3DES encryption * * Function input: inkey16 byte password *indata8 bytes need to be added Secret data * Function output: outdata8 byte encryption result output * * function return: none * ***************************************************************** /void Curcalc_3des_encrypt (U08 *inkey, U08 *indata, U08 *outdata) {U08 LKEY[8]; U08 Rkey[8]; U08 Tmpdest[8]; Mycopy (Lkey, Inkey, 8); Mycopy (Rkey, inkey+8, 8); Curcalc_des_encrypt (Lkey, Indata, outdata);//Plus Curcalc_des_decrypt (Rkey, Outdata, tmpdest);//Solution Curcalc_des_encrypt ( Lkey, Tmpdest, outdata);//Plus}/*********************************************************************************** Function prototypes: void Curcalc_3des_decrypt (U08 *inkey, U08 *indata, U08 *outdata) * * Function function: 3DES decryption * * function Input: inkey8 byte password *indata8 byte required decrypted data * function output: outdata8 byte decryption result output * * function returned: none * * * */void Curcalc_3des_decrypt (U08 *inkey, U08 *indata, U08 *outdata) {U08 lkey[8]; U08 Rkey[8]; U08 Tmpdest[8]; Mycopy (Lkey, Inkey, 8); Mycopy (Rkey, inkey+8, 8); Curcalc_des_decrypt (Lkey, Indata, outdata);//Solution Curcalc_des_encrypt (Rkey, Outdata, tmpdest);//Plus Curcalc_des_decrypt ( Lkey, Tmpdest, outdata);//Solution}
/************ Name: Get message Mac value * Function: Message mac Algorithm * Entry: * *buf, data buffer to calculate, buf_size, length of calculation data * *key, key (8B) * Export: MAC_BUF, calculated Mac value (8B) ANSI x9.9 mac algorithm ********************************************************/void ansi99x    _get_mac (U08 *buf, U32 buf_size, U08 *key, U08 *mac_buf) {U08 val[8],xor[8];    U08 block[512];    U16 X,n;    U16 i,j=0; Preparation work memcpy (block, buf, buf_size); Assigns the input data to the temporary variable Blockx = BUF_SIZE/8; Calculates the number of complete blocks n = buf_size% 8; Computes the last block with several bytes if (n! = 0)//y not 0, then complements 0x00 ...    {memset (&block[x*8+n], 0x00, 8-n); x + = 1;    Add this piece plus go to}//to start Operation Memset (Val, 0x00, 8), for (i = 0; i < x; i++)//How many blocks of loops {Dataxor (val,&block[j], 8,xor); Curcalc_des_encrypt (key,xor,val);//des encryption J + = 8; Used to remove a piece of data}memcpy (Mac_buf,val, 8);} 
/************ XOR/********************** /void Dataxor (U08 *source, U08 *dest,   U32 size, U08 *out) {int i; for (i = 0; i < size; i++) {out[i] = Dest[i] ^ source[i];}} 
/*************************************************** Pboc-3des mac Compute ******************************************** /void Pboc_3des_mac (U08 *buf, U32 buf_size, U08 *key, U08 *mac_buf) {U08 val[8],xor[8];    U08 Keyl[8],keyr[8];    U08 block[512];    U16 X,n;    U16 i;memcpy (keyl,key,8);    memcpy (keyr,&key[8],8); Preparation work memcpy (block, buf, buf_size); Assigns the input data to the temporary variable Blockx = BUF_SIZE/8; Calculates the number of complete blocks n = buf_size% 8; Computes the last block with several bytes if (n! = 0)//y not 0, then complements 0x00 ... {memset (&block[x*8+n], 0x00, 8-n); block[x*8+n]=0x80;}    Else{memset (&block[x*8], 0x00, 8);//If the last piece of length is 8 bytes, then add 80 00 at the end. block[x*8]=0x80;} Start Operation Memset (Val, 0x00, 8);//Initial Vector memcpy (val, uppan,8);D Ataxor (val,&block[0], 8,xor); for (i = 1; i < x+1; i++)    How many blocks of loops {curcalc_des_encrypt (keyl,xor,val);//des encryption Dataxor (Val,&block[i*8], 8,xor); j + = 8; Used to remove a piece of data}curcalc_des_encrypt (Keyl,xor,val); Curcalc_des_decrypt (Keyr,val,xor); Curcalc_des_encrypt (Keyl,xor,val); memcpy (Mac_buf,val, 8);}
The principle of implementing Mac algorithms can refer to the CPU Card instruction manual or the PBOC specification.

Calculation of the Mac:

3DES Algorithm Computing Mac

Ansi-x99mac algorithm and the PBoC's 3DES mac algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.