Path: blob/master/libs/tomcrypt/src/modes/lrw/lrw_process.c
8695 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_process.c12LRW_MODE implementation, Encrypt/decrypt blocks, Tom St Denis13*/1415#ifdef LTC_LRW_MODE1617/**18Process blocks with LRW, since decrypt/encrypt are largely the same they share this code.19@param pt The "input" data20@param ct [out] The "output" data21@param len The length of the input, must be a multiple of 128-bits (16 octets)22@param mode LRW_ENCRYPT or LRW_DECRYPT23@param lrw The LRW state24@return CRYPT_OK if successful25*/26int lrw_process(const unsigned char *pt, unsigned char *ct, unsigned long len, int mode, symmetric_LRW *lrw)27{28unsigned char prod[16];29int x, err;30#ifdef LTC_LRW_TABLES31int y;32#endif3334LTC_ARGCHK(pt != NULL);35LTC_ARGCHK(ct != NULL);36LTC_ARGCHK(lrw != NULL);3738if (len & 15) {39return CRYPT_INVALID_ARG;40}4142while (len) {43/* copy pad */44XMEMCPY(prod, lrw->pad, 16);4546/* increment IV */47for (x = 15; x >= 0; x--) {48lrw->IV[x] = (lrw->IV[x] + 1) & 255;49if (lrw->IV[x]) {50break;51}52}5354/* update pad */55#ifdef LTC_LRW_TABLES56/* for each byte changed we undo it's affect on the pad then add the new product */57for (; x < 16; x++) {58#ifdef LTC_FAST59for (y = 0; y < 16; y += sizeof(LTC_FAST_TYPE)) {60*(LTC_FAST_TYPE_PTR_CAST(lrw->pad + y)) ^= *(LTC_FAST_TYPE_PTR_CAST(&lrw->PC[x][lrw->IV[x]][y])) ^ *(LTC_FAST_TYPE_PTR_CAST(&lrw->PC[x][(lrw->IV[x]-1)&255][y]));61}62#else63for (y = 0; y < 16; y++) {64lrw->pad[y] ^= lrw->PC[x][lrw->IV[x]][y] ^ lrw->PC[x][(lrw->IV[x]-1)&255][y];65}66#endif67}68#else69gcm_gf_mult(lrw->tweak, lrw->IV, lrw->pad);70#endif7172/* xor prod */73#ifdef LTC_FAST74for (x = 0; x < 16; x += sizeof(LTC_FAST_TYPE)) {75*(LTC_FAST_TYPE_PTR_CAST(ct + x)) = *(LTC_FAST_TYPE_PTR_CAST(pt + x)) ^ *(LTC_FAST_TYPE_PTR_CAST(prod + x));76}77#else78for (x = 0; x < 16; x++) {79ct[x] = pt[x] ^ prod[x];80}81#endif8283/* send through cipher */84if (mode == LRW_ENCRYPT) {85if ((err = cipher_descriptor[lrw->cipher].ecb_encrypt(ct, ct, &lrw->key)) != CRYPT_OK) {86return err;87}88} else {89if ((err = cipher_descriptor[lrw->cipher].ecb_decrypt(ct, ct, &lrw->key)) != CRYPT_OK) {90return err;91}92}9394/* xor prod */95#ifdef LTC_FAST96for (x = 0; x < 16; x += sizeof(LTC_FAST_TYPE)) {97*(LTC_FAST_TYPE_PTR_CAST(ct + x)) = *(LTC_FAST_TYPE_PTR_CAST(ct + x)) ^ *(LTC_FAST_TYPE_PTR_CAST(prod + x));98}99#else100for (x = 0; x < 16; x++) {101ct[x] = ct[x] ^ prod[x];102}103#endif104105/* move to next */106pt += 16;107ct += 16;108len -= 16;109}110111return CRYPT_OK;112}113114#endif115116117