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