Path: blob/master/libs/tomcrypt/src/modes/lrw/lrw_start.c
8729 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_start.c12LRW_MODE implementation, start mode, Tom St Denis13*/1415#ifdef LTC_LRW_MODE1617/**18Initialize the LRW context19@param cipher The cipher desired, must be a 128-bit block cipher20@param IV The index value, must be 128-bits21@param key The cipher key22@param keylen The length of the cipher key in octets23@param tweak The tweak value (second key), must be 128-bits24@param num_rounds The number of rounds for the cipher (0 == default)25@param lrw [out] The LRW state26@return CRYPT_OK on success.27*/28int lrw_start( int cipher,29const unsigned char *IV,30const unsigned char *key, int keylen,31const unsigned char *tweak,32int num_rounds,33symmetric_LRW *lrw)34{35int err;36#ifdef LTC_LRW_TABLES37unsigned char B[16];38int x, y, z, t;39#endif4041LTC_ARGCHK(IV != NULL);42LTC_ARGCHK(key != NULL);43LTC_ARGCHK(tweak != NULL);44LTC_ARGCHK(lrw != NULL);4546#ifdef LTC_FAST47if (16 % sizeof(LTC_FAST_TYPE)) {48return CRYPT_INVALID_ARG;49}50#endif5152/* is cipher valid? */53if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {54return err;55}56if (cipher_descriptor[cipher].block_length != 16) {57return CRYPT_INVALID_CIPHER;58}5960/* schedule key */61if ((err = cipher_descriptor[cipher].setup(key, keylen, num_rounds, &lrw->key)) != CRYPT_OK) {62return err;63}64lrw->cipher = cipher;6566/* copy the IV and tweak */67XMEMCPY(lrw->tweak, tweak, 16);6869#ifdef LTC_LRW_TABLES70/* setup tables */71/* generate the first table as it has no shifting (from which we make the other tables) */72zeromem(B, 16);73for (y = 0; y < 256; y++) {74B[0] = y;75gcm_gf_mult(tweak, B, &lrw->PC[0][y][0]);76}7778/* now generate the rest of the tables based the previous table */79for (x = 1; x < 16; x++) {80for (y = 0; y < 256; y++) {81/* now shift it right by 8 bits */82t = lrw->PC[x-1][y][15];83for (z = 15; z > 0; z--) {84lrw->PC[x][y][z] = lrw->PC[x-1][y][z-1];85}86lrw->PC[x][y][0] = gcm_shift_table[t<<1];87lrw->PC[x][y][1] ^= gcm_shift_table[(t<<1)+1];88}89}90#endif9192/* generate first pad */93return lrw_setiv(IV, 16, lrw);94}959697#endif9899100