Path: blob/master/libs/tomcrypt/src/encauth/ocb3/ocb3_decrypt_last.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*/89/**10@file ocb3_decrypt_last.c11OCB implementation, internal helper, by Karel Miko12*/13#include "tomcrypt.h"1415#ifdef LTC_OCB3_MODE1617/**18Finish an OCB (decryption) stream19@param ocb The OCB state20@param ct The remaining ciphertext21@param ctlen The length of the ciphertext (octets)22@param pt [out] The output buffer23@return CRYPT_OK if successful24*/25int ocb3_decrypt_last(ocb3_state *ocb, const unsigned char *ct, unsigned long ctlen, unsigned char *pt)26{27unsigned char iOffset_star[MAXBLOCKSIZE];28unsigned char iPad[MAXBLOCKSIZE];29int err, x, full_blocks, full_blocks_len, last_block_len;3031LTC_ARGCHK(ocb != NULL);32if (ct == NULL) LTC_ARGCHK(ctlen == 0);33if (ctlen != 0) {34LTC_ARGCHK(ct != NULL);35LTC_ARGCHK(pt != NULL);36}3738if ((err = cipher_is_valid(ocb->cipher)) != CRYPT_OK) {39goto LBL_ERR;40}4142full_blocks = ctlen/ocb->block_len;43full_blocks_len = full_blocks * ocb->block_len;44last_block_len = ctlen - full_blocks_len;4546/* process full blocks first */47if (full_blocks>0) {48if ((err = ocb3_decrypt(ocb, ct, full_blocks_len, pt)) != CRYPT_OK) {49goto LBL_ERR;50}51}5253if (last_block_len>0) {54/* Offset_* = Offset_m xor L_* */55ocb3_int_xor_blocks(iOffset_star, ocb->Offset_current, ocb->L_star, ocb->block_len);5657/* Pad = ENCIPHER(K, Offset_*) */58if ((err = cipher_descriptor[ocb->cipher].ecb_encrypt(iOffset_star, iPad, &ocb->key)) != CRYPT_OK) {59goto LBL_ERR;60}6162/* P_* = C_* xor Pad[1..bitlen(C_*)] */63ocb3_int_xor_blocks(pt+full_blocks_len, (unsigned char *)ct+full_blocks_len, iPad, last_block_len);6465/* Checksum_* = Checksum_m xor (P_* || 1 || zeros(127-bitlen(P_*))) */66ocb3_int_xor_blocks(ocb->checksum, ocb->checksum, pt+full_blocks_len, last_block_len);67for(x=last_block_len; x<ocb->block_len; x++) {68if (x == last_block_len)69ocb->checksum[x] ^= 0x80;70else71ocb->checksum[x] ^= 0x00;72}7374/* Tag = ENCIPHER(K, Checksum_* xor Offset_* xor L_$) xor HASH(K,A) */75/* at this point we calculate only: Tag_part = ENCIPHER(K, Checksum_* xor Offset_* xor L_$) */76for(x=0; x<ocb->block_len; x++) {77ocb->tag_part[x] = (ocb->checksum[x] ^ iOffset_star[x]) ^ ocb->L_dollar[x];78}79if ((err = cipher_descriptor[ocb->cipher].ecb_encrypt(ocb->tag_part, ocb->tag_part, &ocb->key)) != CRYPT_OK) {80goto LBL_ERR;81}82}83else {84/* Tag = ENCIPHER(K, Checksum_m xor Offset_m xor L_$) xor HASH(K,A) */85/* at this point we calculate only: Tag_part = ENCIPHER(K, Checksum_m xor Offset_m xor L_$) */86for(x=0; x<ocb->block_len; x++) {87ocb->tag_part[x] = (ocb->checksum[x] ^ ocb->Offset_current[x]) ^ ocb->L_dollar[x];88}89if ((err = cipher_descriptor[ocb->cipher].ecb_encrypt(ocb->tag_part, ocb->tag_part, &ocb->key)) != CRYPT_OK) {90goto LBL_ERR;91}92}9394err = CRYPT_OK;9596LBL_ERR:97#ifdef LTC_CLEAN_STACK98zeromem(iOffset_star, MAXBLOCKSIZE);99zeromem(iPad, MAXBLOCKSIZE);100#endif101102return err;103}104105#endif106107108