Path: blob/master/libs/tomcrypt/src/encauth/ocb/ocb_decrypt.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 ocb_decrypt.c11OCB implementation, decrypt data, by Tom St Denis12*/13#include "tomcrypt.h"1415#ifdef LTC_OCB_MODE1617/**18Decrypt a block with OCB.19@param ocb The OCB state20@param ct The ciphertext (length of the block size of the block cipher)21@param pt [out] The plaintext (length of ct)22@return CRYPT_OK if successful23*/24int ocb_decrypt(ocb_state *ocb, const unsigned char *ct, unsigned char *pt)25{26unsigned char Z[MAXBLOCKSIZE], tmp[MAXBLOCKSIZE];27int err, x;2829LTC_ARGCHK(ocb != NULL);30LTC_ARGCHK(pt != NULL);31LTC_ARGCHK(ct != NULL);3233/* check if valid cipher */34if ((err = cipher_is_valid(ocb->cipher)) != CRYPT_OK) {35return err;36}37LTC_ARGCHK(cipher_descriptor[ocb->cipher].ecb_decrypt != NULL);3839/* check length */40if (ocb->block_len != cipher_descriptor[ocb->cipher].block_length) {41return CRYPT_INVALID_ARG;42}4344/* Get Z[i] value */45ocb_shift_xor(ocb, Z);4647/* xor ct in, encrypt, xor Z out */48for (x = 0; x < ocb->block_len; x++) {49tmp[x] = ct[x] ^ Z[x];50}51if ((err = cipher_descriptor[ocb->cipher].ecb_decrypt(tmp, pt, &ocb->key)) != CRYPT_OK) {52return err;53}54for (x = 0; x < ocb->block_len; x++) {55pt[x] ^= Z[x];56}5758/* compute checksum */59for (x = 0; x < ocb->block_len; x++) {60ocb->checksum[x] ^= pt[x];61}626364#ifdef LTC_CLEAN_STACK65zeromem(Z, sizeof(Z));66zeromem(tmp, sizeof(tmp));67#endif68return CRYPT_OK;69}7071#endif727374