Path: blob/master/libs/tomcrypt/src/mac/omac/omac_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 omac_process.c12OMAC1 support, process data, Tom St Denis13*/141516#ifdef LTC_OMAC1718/**19Process data through OMAC20@param omac The OMAC state21@param in The input data to send through OMAC22@param inlen The length of the input (octets)23@return CRYPT_OK if successful24*/25int omac_process(omac_state *omac, const unsigned char *in, unsigned long inlen)26{27unsigned long n, x;28int err;2930LTC_ARGCHK(omac != NULL);31LTC_ARGCHK(in != NULL);32if ((err = cipher_is_valid(omac->cipher_idx)) != CRYPT_OK) {33return err;34}3536if ((omac->buflen > (int)sizeof(omac->block)) || (omac->buflen < 0) ||37(omac->blklen > (int)sizeof(omac->block)) || (omac->buflen > omac->blklen)) {38return CRYPT_INVALID_ARG;39}4041#ifdef LTC_FAST42{43unsigned long blklen = cipher_descriptor[omac->cipher_idx].block_length;4445if (omac->buflen == 0 && inlen > blklen) {46unsigned long y;47for (x = 0; x < (inlen - blklen); x += blklen) {48for (y = 0; y < blklen; y += sizeof(LTC_FAST_TYPE)) {49*(LTC_FAST_TYPE_PTR_CAST(&omac->prev[y])) ^= *(LTC_FAST_TYPE_PTR_CAST(&in[y]));50}51in += blklen;52if ((err = cipher_descriptor[omac->cipher_idx].ecb_encrypt(omac->prev, omac->prev, &omac->key)) != CRYPT_OK) {53return err;54}55}56inlen -= x;57}58}59#endif6061while (inlen != 0) {62/* ok if the block is full we xor in prev, encrypt and replace prev */63if (omac->buflen == omac->blklen) {64for (x = 0; x < (unsigned long)omac->blklen; x++) {65omac->block[x] ^= omac->prev[x];66}67if ((err = cipher_descriptor[omac->cipher_idx].ecb_encrypt(omac->block, omac->prev, &omac->key)) != CRYPT_OK) {68return err;69}70omac->buflen = 0;71}7273/* add bytes */74n = MIN(inlen, (unsigned long)(omac->blklen - omac->buflen));75XMEMCPY(omac->block + omac->buflen, in, n);76omac->buflen += n;77inlen -= n;78in += n;79}8081return CRYPT_OK;82}8384#endif858687