Path: blob/master/libs/tomcrypt/src/mac/pmac/pmac_done.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_done.c12PMAC implementation, terminate a session, by Tom St Denis13*/1415#ifdef LTC_PMAC1617int pmac_done(pmac_state *state, unsigned char *out, unsigned long *outlen)18{19int err, x;2021LTC_ARGCHK(state != NULL);22LTC_ARGCHK(out != NULL);23if ((err = cipher_is_valid(state->cipher_idx)) != CRYPT_OK) {24return err;25}2627if ((state->buflen > (int)sizeof(state->block)) || (state->buflen < 0) ||28(state->block_len > (int)sizeof(state->block)) || (state->buflen > state->block_len)) {29return CRYPT_INVALID_ARG;30}313233/* handle padding. If multiple xor in L/x */3435if (state->buflen == state->block_len) {36/* xor Lr against the checksum */37for (x = 0; x < state->block_len; x++) {38state->checksum[x] ^= state->block[x] ^ state->Lr[x];39}40} else {41/* otherwise xor message bytes then the 0x80 byte */42for (x = 0; x < state->buflen; x++) {43state->checksum[x] ^= state->block[x];44}45state->checksum[x] ^= 0x80;46}4748/* encrypt it */49if ((err = cipher_descriptor[state->cipher_idx].ecb_encrypt(state->checksum, state->checksum, &state->key)) != CRYPT_OK) {50return err;51}52cipher_descriptor[state->cipher_idx].done(&state->key);5354/* store it */55for (x = 0; x < state->block_len && x < (int)*outlen; x++) {56out[x] = state->checksum[x];57}58*outlen = x;5960#ifdef LTC_CLEAN_STACK61zeromem(state, sizeof(*state));62#endif63return CRYPT_OK;64}6566#endif676869