/* * encrypt.c * * Created on: 22 Feb 2019 * Author: mrz */ #include "crypto_aead.h" #include "led.h" #include "cilipadi.h" #include "api.h" #include #include // malloc() and free() /* * pad the message and AD if necessary */ int pad( const unsigned char *m, unsigned long long mlen, const unsigned char *ad, unsigned long long adlen, unsigned char *mx, unsigned long long mxlen, unsigned char *adx, unsigned long long adxlen) { int i; // original message for (i = 0; i < mlen; ++i) { mx[i] = m[i]; } // pad only if length is not a multiple of r bits if (mxlen > mlen) { // bit 1 followed by zeros mx[mlen] = 0x80; for (i = mlen+1; i < mxlen; ++i) { mx[i] = 0; } } // original AD for (i = 0; i < adlen; ++i) { adx[i] = ad[i]; } // pad only if length is not a multiple of r bits if (adxlen > adlen) { // bit 1 followed by zeros adx[adlen] = 0x80; for (i = adlen+1; i < adxlen; ++i) { adx[i] = 0; } } return 0; } /* * Check the length of the message and AD whether they need padding */ int padding_len_check( unsigned long long mlen, unsigned long long adlen, unsigned long long *mxlen, unsigned long long *adxlen) { unsigned long long x; // check padding on the plaintext x = mlen % BYTERATE; // if mlen is already a multiple of r bits, i.e. x=0, then set x = r to offset BYTERATE in the equation below if (x==0) x = BYTERATE; *mxlen = mlen + BYTERATE - x; // check padding on the AD x = adlen % BYTERATE; // if length is already a multiple of r bits, i.e. x=0, then set x = r to offset BYTERATE in the equation below if (x==0) x = BYTERATE; *adxlen = adlen + BYTERATE - x; return 0; } /* * Initializataion Phase */ int init_phase(unsigned char *state, const unsigned char *npub, const unsigned char *k) { int i; // fill in the key for (i=0; i