Path: blob/master/libs/tomcrypt/src/modes/lrw/lrw_setiv.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*/8#include "tomcrypt.h"910/**11@file lrw_setiv.c12LRW_MODE implementation, Set the current IV, Tom St Denis13*/1415#ifdef LTC_LRW_MODE1617/**18Set the IV for LRW19@param IV The IV, must be 16 octets20@param len Length ... must be 16 :-)21@param lrw The LRW state to update22@return CRYPT_OK if successful23*/24int lrw_setiv(const unsigned char *IV, unsigned long len, symmetric_LRW *lrw)25{26int err;27#ifdef LTC_LRW_TABLES28unsigned char T[16];29int x, y;30#endif31LTC_ARGCHK(IV != NULL);32LTC_ARGCHK(lrw != NULL);3334if (len != 16) {35return CRYPT_INVALID_ARG;36}3738if ((err = cipher_is_valid(lrw->cipher)) != CRYPT_OK) {39return err;40}4142/* copy the IV */43XMEMCPY(lrw->IV, IV, 16);4445/* check if we have to actually do work */46if (cipher_descriptor[lrw->cipher].accel_lrw_encrypt != NULL && cipher_descriptor[lrw->cipher].accel_lrw_decrypt != NULL) {47/* we have accelerators, let's bail since they don't use lrw->pad anyways */48return CRYPT_OK;49}5051#ifdef LTC_LRW_TABLES52XMEMCPY(T, &lrw->PC[0][IV[0]][0], 16);53for (x = 1; x < 16; x++) {54#ifdef LTC_FAST55for (y = 0; y < 16; y += sizeof(LTC_FAST_TYPE)) {56*(LTC_FAST_TYPE_PTR_CAST(T + y)) ^= *(LTC_FAST_TYPE_PTR_CAST(&lrw->PC[x][IV[x]][y]));57}58#else59for (y = 0; y < 16; y++) {60T[y] ^= lrw->PC[x][IV[x]][y];61}62#endif63}64XMEMCPY(lrw->pad, T, 16);65#else66gcm_gf_mult(lrw->tweak, IV, lrw->pad);67#endif6869return CRYPT_OK;70}717273#endif747576