Path: blob/master/libs/tomcrypt/src/mac/pmac/pmac_process.c
5972 views
/* LibTomCrypt, modular cryptographic library -- Tom St Denis1*2* LibTomCrypt is a library that provides various cryptographic3* algorithms in a highly modular and flexible manner.4*5* The library is free for all purposes without any express6* guarantee it works.7*/8#include "tomcrypt.h"910/**11@file pmac_process.c12PMAC implementation, process data, by Tom St Denis13*/141516#ifdef LTC_PMAC1718/**19Process data in a PMAC stream20@param pmac The PMAC state21@param in The data to send through PMAC22@param inlen The length of the data to send through PMAC23@return CRYPT_OK if successful24*/25int pmac_process(pmac_state *pmac, const unsigned char *in, unsigned long inlen)26{27int err, n;28unsigned long x;29unsigned char Z[MAXBLOCKSIZE];3031LTC_ARGCHK(pmac != NULL);32LTC_ARGCHK(in != NULL);33if ((err = cipher_is_valid(pmac->cipher_idx)) != CRYPT_OK) {34return err;35}3637if ((pmac->buflen > (int)sizeof(pmac->block)) || (pmac->buflen < 0) ||38(pmac->block_len > (int)sizeof(pmac->block)) || (pmac->buflen > pmac->block_len)) {39return CRYPT_INVALID_ARG;40}4142#ifdef LTC_FAST43if (pmac->buflen == 0 && inlen > 16) {44unsigned long y;45for (x = 0; x < (inlen - 16); x += 16) {46pmac_shift_xor(pmac);47for (y = 0; y < 16; y += sizeof(LTC_FAST_TYPE)) {48*(LTC_FAST_TYPE_PTR_CAST(&Z[y])) = *(LTC_FAST_TYPE_PTR_CAST(&in[y])) ^ *(LTC_FAST_TYPE_PTR_CAST(&pmac->Li[y]));49}50if ((err = cipher_descriptor[pmac->cipher_idx].ecb_encrypt(Z, Z, &pmac->key)) != CRYPT_OK) {51return err;52}53for (y = 0; y < 16; y += sizeof(LTC_FAST_TYPE)) {54*(LTC_FAST_TYPE_PTR_CAST(&pmac->checksum[y])) ^= *(LTC_FAST_TYPE_PTR_CAST(&Z[y]));55}56in += 16;57}58inlen -= x;59}60#endif6162while (inlen != 0) {63/* ok if the block is full we xor in prev, encrypt and replace prev */64if (pmac->buflen == pmac->block_len) {65pmac_shift_xor(pmac);66for (x = 0; x < (unsigned long)pmac->block_len; x++) {67Z[x] = pmac->Li[x] ^ pmac->block[x];68}69if ((err = cipher_descriptor[pmac->cipher_idx].ecb_encrypt(Z, Z, &pmac->key)) != CRYPT_OK) {70return err;71}72for (x = 0; x < (unsigned long)pmac->block_len; x++) {73pmac->checksum[x] ^= Z[x];74}75pmac->buflen = 0;76}7778/* add bytes */79n = MIN(inlen, (unsigned long)(pmac->block_len - pmac->buflen));80XMEMCPY(pmac->block + pmac->buflen, in, n);81pmac->buflen += n;82inlen -= n;83in += n;84}8586#ifdef LTC_CLEAN_STACK87zeromem(Z, sizeof(Z));88#endif8990return CRYPT_OK;91}9293#endif949596