]> pd.if.org Git - zpackage/blob - libtomcrypt/src/modes/lrw/lrw_process.c
0896bc6413284440ac4dbbdb3e1268d6c27df747
[zpackage] / libtomcrypt / src / modes / lrw / lrw_process.c
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2  *
3  * LibTomCrypt is a library that provides various cryptographic
4  * algorithms in a highly modular and flexible manner.
5  *
6  * The library is free for all purposes without any express
7  * guarantee it works.
8  */
9 #include "tomcrypt.h"
10
11 /**
12    @file lrw_process.c
13    LRW_MODE implementation, Encrypt/decrypt blocks, Tom St Denis
14 */
15
16 #ifdef LTC_LRW_MODE
17
18 /**
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
26 */
27 int lrw_process(const unsigned char *pt, unsigned char *ct, unsigned long len, int mode, symmetric_LRW *lrw)
28 {
29    unsigned char prod[16];
30    int           x, err;
31 #ifdef LTC_LRW_TABLES
32    int           y;
33 #endif
34
35    LTC_ARGCHK(pt  != NULL);
36    LTC_ARGCHK(ct  != NULL);
37    LTC_ARGCHK(lrw != NULL);
38
39    if (len & 15) {
40       return CRYPT_INVALID_ARG;
41    }
42
43    while (len) {
44       /* copy pad */
45       XMEMCPY(prod, lrw->pad, 16);
46
47       /* increment IV */
48       for (x = 15; x >= 0; x--) {
49           lrw->IV[x] = (lrw->IV[x] + 1) & 255;
50           if (lrw->IV[x]) {
51               break;
52           }
53       }
54
55       /* update pad */
56 #ifdef LTC_LRW_TABLES
57       /* for each byte changed we undo it's affect on the pad then add the new product */
58       for (; x < 16; x++) {
59 #ifdef LTC_FAST
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]));
62           }
63 #else
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];
66           }
67 #endif
68       }
69 #else
70       gcm_gf_mult(lrw->tweak, lrw->IV, lrw->pad);
71 #endif
72
73       /* xor prod */
74 #ifdef LTC_FAST
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));
77       }
78 #else
79       for (x = 0; x < 16; x++) {
80          ct[x] = pt[x] ^ prod[x];
81       }
82 #endif
83
84       /* send through cipher */
85       if (mode == LRW_ENCRYPT) {
86          if ((err = cipher_descriptor[lrw->cipher].ecb_encrypt(ct, ct, &lrw->key)) != CRYPT_OK) {
87             return err;
88          }
89       } else {
90          if ((err = cipher_descriptor[lrw->cipher].ecb_decrypt(ct, ct, &lrw->key)) != CRYPT_OK) {
91             return err;
92          }
93       }
94
95       /* xor prod */
96 #ifdef LTC_FAST
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));
99       }
100 #else
101       for (x = 0; x < 16; x++) {
102          ct[x] = ct[x] ^ prod[x];
103       }
104 #endif
105
106       /* move to next */
107       pt  += 16;
108       ct  += 16;
109       len -= 16;
110    }
111
112    return CRYPT_OK;
113 }
114
115 #endif
116 /* ref:         $Format:%D$ */
117 /* git commit:  $Format:%H$ */
118 /* commit time: $Format:%ai$ */