1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
13 LRW_MODE implementation, Encrypt/decrypt blocks, Tom St Denis
19 Process blocks with LRW, since decrypt/encrypt are largely the same they share this code.
20 @param pt The "input" data
21 @param ct [out] The "output" data
22 @param len The length of the input, must be a multiple of 128-bits (16 octets)
23 @param mode LRW_ENCRYPT or LRW_DECRYPT
24 @param lrw The LRW state
25 @return CRYPT_OK if successful
27 int lrw_process(const unsigned char *pt, unsigned char *ct, unsigned long len, int mode, symmetric_LRW *lrw)
29 unsigned char prod[16];
35 LTC_ARGCHK(pt != NULL);
36 LTC_ARGCHK(ct != NULL);
37 LTC_ARGCHK(lrw != NULL);
40 return CRYPT_INVALID_ARG;
45 XMEMCPY(prod, lrw->pad, 16);
48 for (x = 15; x >= 0; x--) {
49 lrw->IV[x] = (lrw->IV[x] + 1) & 255;
57 /* for each byte changed we undo it's affect on the pad then add the new product */
60 for (y = 0; y < 16; y += sizeof(LTC_FAST_TYPE)) {
61 *(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]));
64 for (y = 0; y < 16; y++) {
65 lrw->pad[y] ^= lrw->PC[x][lrw->IV[x]][y] ^ lrw->PC[x][(lrw->IV[x]-1)&255][y];
70 gcm_gf_mult(lrw->tweak, lrw->IV, lrw->pad);
75 for (x = 0; x < 16; x += sizeof(LTC_FAST_TYPE)) {
76 *(LTC_FAST_TYPE_PTR_CAST(ct + x)) = *(LTC_FAST_TYPE_PTR_CAST(pt + x)) ^ *(LTC_FAST_TYPE_PTR_CAST(prod + x));
79 for (x = 0; x < 16; x++) {
80 ct[x] = pt[x] ^ prod[x];
84 /* send through cipher */
85 if (mode == LRW_ENCRYPT) {
86 if ((err = cipher_descriptor[lrw->cipher].ecb_encrypt(ct, ct, &lrw->key)) != CRYPT_OK) {
90 if ((err = cipher_descriptor[lrw->cipher].ecb_decrypt(ct, ct, &lrw->key)) != CRYPT_OK) {
97 for (x = 0; x < 16; x += sizeof(LTC_FAST_TYPE)) {
98 *(LTC_FAST_TYPE_PTR_CAST(ct + x)) = *(LTC_FAST_TYPE_PTR_CAST(ct + x)) ^ *(LTC_FAST_TYPE_PTR_CAST(prod + x));
101 for (x = 0; x < 16; x++) {
102 ct[x] = ct[x] ^ prod[x];
116 /* ref: $Format:%D$ */
117 /* git commit: $Format:%H$ */
118 /* commit time: $Format:%ai$ */