#include #include #include "api.h" #include "blockcipher.h" //Message-Blocksize #define MSZ 16 typedef unsigned char u8; //used for Byte-Arrays typedef unsigned int u32; //used for regular counters typedef unsigned long long ull; //used for long counters void E(u8 *ct, const u8 *key, const u8 *pt){ blockcipher_encrypt(ct, pt, key); return; } void derive(u8 *K_E, u8 *K_MAC, u8 *N2, u8 *N1, const u8 *k, const u8 *npub){ //N with first byte for counting u8 N_pad[MSZ] = { 0 }; memcpy(&N_pad[1], npub, CRYPTO_NPUBBYTES); u8 T0[MSZ] = { 0 }; u8 N2N1[MSZ] = { 0 }; //(a) K_E E(K_E, k, N_pad); //(b) K_MAC N_pad[0] = 1; E(K_MAC, k, N_pad); //(c) [N2, N1] N_pad[0] = 2; E(N2N1, k, N_pad); for(u8 i=0; i 0){ //Truncate memset(&T_C[(mlen_blocks-1)*MSZ+r_C], 0, MSZ-r_C); } } /*** Authenticate ***/ //Padding of M if last block is not full if(r_C != 0){ memset(&T_C[*mlen], 1, 1); } u8 X_temp[MSZ] = { 0 }; u8 TMP[MSZ] = { 0 }; //Set higher MSZ/2 Bytes of TMP to N2 memcpy(&TMP[MSZ/2], N2, MSZ/2); //CBCMAC-IV for(ull j=0; j is_auth = 1 is_auth = 1; } else { //if T' =/= T -> is_auth = 0 is_auth = 0; } free(T_C); free(T_A); if(is_auth){ return 0; } else { return -1; } }