Path: blob/master/libs/tomcrypt/src/encauth/ocb/s_ocb_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*/89/**10@file s_ocb_done.c11OCB implementation, internal helper, by Tom St Denis12*/13#include "tomcrypt.h"1415#ifdef LTC_OCB_MODE1617/* Since the last block is encrypted in CTR mode the same code can18* be used to finish a decrypt or encrypt stream. The only difference19* is we XOR the final ciphertext into the checksum so we have to xor it20* before we CTR [decrypt] or after [encrypt]21*22* the names pt/ptlen/ct really just mean in/inlen/out but this is the way I wrote it...23*/2425/**26Shared code to finish an OCB stream27@param ocb The OCB state28@param pt The remaining plaintext [or input]29@param ptlen The length of the input (octets)30@param ct [out] The output buffer31@param tag [out] The destination for the authentication tag32@param taglen [in/out] The max size and resulting size of the authentication tag33@param mode The mode we are terminating, 0==encrypt, 1==decrypt34@return CRYPT_OK if successful35*/36int s_ocb_done(ocb_state *ocb, const unsigned char *pt, unsigned long ptlen,37unsigned char *ct, unsigned char *tag, unsigned long *taglen, int mode)3839{40unsigned char *Z, *Y, *X;41int err, x;4243LTC_ARGCHK(ocb != NULL);44LTC_ARGCHK(pt != NULL);45LTC_ARGCHK(ct != NULL);46LTC_ARGCHK(tag != NULL);47LTC_ARGCHK(taglen != NULL);48if ((err = cipher_is_valid(ocb->cipher)) != CRYPT_OK) {49return err;50}51if (ocb->block_len != cipher_descriptor[ocb->cipher].block_length ||52(int)ptlen > ocb->block_len || (int)ptlen < 0) {53return CRYPT_INVALID_ARG;54}5556/* allocate ram */57Z = XMALLOC(MAXBLOCKSIZE);58Y = XMALLOC(MAXBLOCKSIZE);59X = XMALLOC(MAXBLOCKSIZE);60if (X == NULL || Y == NULL || Z == NULL) {61if (X != NULL) {62XFREE(X);63}64if (Y != NULL) {65XFREE(Y);66}67if (Z != NULL) {68XFREE(Z);69}70return CRYPT_MEM;71}7273/* compute X[m] = len(pt[m]) XOR Lr XOR Z[m] */74ocb_shift_xor(ocb, X);75XMEMCPY(Z, X, ocb->block_len);7677X[ocb->block_len-1] ^= (ptlen*8)&255;78X[ocb->block_len-2] ^= ((ptlen*8)>>8)&255;79for (x = 0; x < ocb->block_len; x++) {80X[x] ^= ocb->Lr[x];81}8283/* Y[m] = E(X[m])) */84if ((err = cipher_descriptor[ocb->cipher].ecb_encrypt(X, Y, &ocb->key)) != CRYPT_OK) {85goto error;86}8788if (mode == 1) {89/* decrypt mode, so let's xor it first */90/* xor C[m] into checksum */91for (x = 0; x < (int)ptlen; x++) {92ocb->checksum[x] ^= ct[x];93}94}9596/* C[m] = P[m] xor Y[m] */97for (x = 0; x < (int)ptlen; x++) {98ct[x] = pt[x] ^ Y[x];99}100101if (mode == 0) {102/* encrypt mode */103/* xor C[m] into checksum */104for (x = 0; x < (int)ptlen; x++) {105ocb->checksum[x] ^= ct[x];106}107}108109/* xor Y[m] and Z[m] into checksum */110for (x = 0; x < ocb->block_len; x++) {111ocb->checksum[x] ^= Y[x] ^ Z[x];112}113114/* encrypt checksum, er... tag!! */115if ((err = cipher_descriptor[ocb->cipher].ecb_encrypt(ocb->checksum, X, &ocb->key)) != CRYPT_OK) {116goto error;117}118cipher_descriptor[ocb->cipher].done(&ocb->key);119120/* now store it */121for (x = 0; x < ocb->block_len && x < (int)*taglen; x++) {122tag[x] = X[x];123}124*taglen = x;125126#ifdef LTC_CLEAN_STACK127zeromem(X, MAXBLOCKSIZE);128zeromem(Y, MAXBLOCKSIZE);129zeromem(Z, MAXBLOCKSIZE);130zeromem(ocb, sizeof(*ocb));131#endif132error:133XFREE(X);134XFREE(Y);135XFREE(Z);136137return err;138}139140#endif141142143