Path: blob/master/libs/tomcrypt/src/encauth/ocb3/ocb3_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 ocb3_encrypt.c11OCB implementation, encrypt data, by Tom St Denis12*/13#include "tomcrypt.h"1415#ifdef LTC_OCB3_MODE1617/**18Encrypt blocks of data with OCB19@param ocb The OCB state20@param pt The plaintext (length multiple of the block size of the block cipher)21@param ptlen The length of the input (octets)22@param ct [out] The ciphertext (same size as the pt)23@return CRYPT_OK if successful24*/25int ocb3_encrypt(ocb3_state *ocb, const unsigned char *pt, unsigned long ptlen, unsigned char *ct)26{27unsigned char tmp[MAXBLOCKSIZE];28int err, i, full_blocks;29unsigned char *pt_b, *ct_b;3031LTC_ARGCHK(ocb != NULL);32if (ptlen == 0) return CRYPT_OK; /* no data, nothing to do */33LTC_ARGCHK(pt != NULL);34LTC_ARGCHK(ct != NULL);3536if ((err = cipher_is_valid(ocb->cipher)) != CRYPT_OK) {37return err;38}39if (ocb->block_len != cipher_descriptor[ocb->cipher].block_length) {40return CRYPT_INVALID_ARG;41}4243if (ptlen % ocb->block_len) { /* ptlen has to bu multiple of block_len */44return CRYPT_INVALID_ARG;45}4647full_blocks = ptlen/ocb->block_len;48for(i=0; i<full_blocks; i++) {49pt_b = (unsigned char *)pt+i*ocb->block_len;50ct_b = (unsigned char *)ct+i*ocb->block_len;5152/* ocb->Offset_current[] = ocb->Offset_current[] ^ Offset_{ntz(block_index)} */53ocb3_int_xor_blocks(ocb->Offset_current, ocb->Offset_current, ocb->L_[ocb3_int_ntz(ocb->block_index)], ocb->block_len);5455/* tmp[] = pt[] XOR ocb->Offset_current[] */56ocb3_int_xor_blocks(tmp, pt_b, ocb->Offset_current, ocb->block_len);5758/* encrypt */59if ((err = cipher_descriptor[ocb->cipher].ecb_encrypt(tmp, tmp, &ocb->key)) != CRYPT_OK) {60goto LBL_ERR;61}6263/* ct[] = tmp[] XOR ocb->Offset_current[] */64ocb3_int_xor_blocks(ct_b, tmp, ocb->Offset_current, ocb->block_len);6566/* ocb->checksum[] = ocb->checksum[] XOR pt[] */67ocb3_int_xor_blocks(ocb->checksum, ocb->checksum, pt_b, ocb->block_len);6869ocb->block_index++;70}7172err = CRYPT_OK;7374LBL_ERR:75#ifdef LTC_CLEAN_STACK76zeromem(tmp, sizeof(tmp));77#endif78return err;79}8081#endif828384