51 broke -E logic completely, rewise it
[tfcrypt.git] / tfpcbc.c
1 #include <string.h>
2 #include "tfdef.h"
3
4 void tf_pcbc_encrypt(const void *key, void *iv, void *out, const void *in, size_t sz)
5 {
6         const TF_BYTE_TYPE *uin = (const TF_BYTE_TYPE *)in;
7         TF_BYTE_TYPE *uout = (TF_BYTE_TYPE *)out;
8         TF_UNIT_TYPE x[TF_NR_BLOCK_UNITS], y[TF_NR_BLOCK_UNITS];
9         TF_UNIT_TYPE *uiv = (TF_UNIT_TYPE *)iv;
10         const TF_UNIT_TYPE *ukey = (const TF_UNIT_TYPE *)key;
11         size_t sl = sz, i;
12
13         if (sl >= TF_BLOCK_SIZE) {
14                 do {
15                         memcpy(x, uin, TF_BLOCK_SIZE);
16                         uin += TF_BLOCK_SIZE;
17                         data_to_words(x, TF_BLOCK_SIZE);
18
19                         for (i = 0; i < TF_NR_BLOCK_UNITS; i++) y[i] = x[i] ^ uiv[i];
20                         tf_encrypt_rawblk(y, y, ukey);
21                         for (i = 0; i < TF_NR_BLOCK_UNITS; i++) uiv[i] = y[i] ^ x[i];
22
23                         data_to_words(y, TF_BLOCK_SIZE);
24                         memcpy(uout, y, TF_BLOCK_SIZE);
25                         uout += TF_BLOCK_SIZE;
26                 } while ((sl -= TF_BLOCK_SIZE) >= TF_BLOCK_SIZE);
27         }
28
29         if (sl) {
30                 memset(x, 0, TF_BLOCK_SIZE);
31                 memcpy(x, uin, sl);
32                 data_to_words(x, TF_BLOCK_SIZE);
33
34                 ctr_inc(uiv, TF_NR_BLOCK_UNITS);
35                 tf_encrypt_rawblk(y, uiv, ukey);
36                 for (i = 0; i < TF_NR_BLOCK_UNITS; i++) y[i] ^= x[i];
37
38                 data_to_words(y, TF_BLOCK_SIZE);
39                 memcpy(uout, y, sl);
40         }
41
42         memset(x, 0, TF_BLOCK_SIZE);
43         memset(y, 0, TF_BLOCK_SIZE);
44 }
45
46 void tf_pcbc_decrypt(const void *key, void *iv, void *out, const void *in, size_t sz)
47 {
48         const TF_BYTE_TYPE *uin = (const TF_BYTE_TYPE *)in;
49         TF_BYTE_TYPE *uout = (TF_BYTE_TYPE *)out;
50         TF_UNIT_TYPE x[TF_NR_BLOCK_UNITS], y[TF_NR_BLOCK_UNITS];
51         TF_UNIT_TYPE *uiv = (TF_UNIT_TYPE *)iv;
52         const TF_UNIT_TYPE *ukey = (const TF_UNIT_TYPE *)key;
53         size_t sl = sz, i;
54
55         if (sl >= TF_BLOCK_SIZE) {
56                 do {
57                         memcpy(x, uin, TF_BLOCK_SIZE);
58                         uin += TF_BLOCK_SIZE;
59                         data_to_words(x, TF_BLOCK_SIZE);
60
61                         tf_decrypt_rawblk(y, x, ukey);
62                         for (i = 0; i < TF_NR_BLOCK_UNITS; i++) y[i] ^= uiv[i];
63                         for (i = 0; i < TF_NR_BLOCK_UNITS; i++) uiv[i] = y[i] ^ x[i];
64
65                         data_to_words(y, TF_BLOCK_SIZE);
66                         memcpy(uout, y, TF_BLOCK_SIZE);
67                         uout += TF_BLOCK_SIZE;
68                 } while ((sl -= TF_BLOCK_SIZE) >= TF_BLOCK_SIZE);
69         }
70
71         if (sl) {
72                 memset(x, 0, TF_BLOCK_SIZE);
73                 memcpy(x, uin, sl);
74                 data_to_words(x, TF_BLOCK_SIZE);
75
76                 ctr_inc(uiv, TF_NR_BLOCK_UNITS);
77                 tf_encrypt_rawblk(y, uiv, ukey);
78                 for (i = 0; i < TF_NR_BLOCK_UNITS; i++) y[i] ^= x[i];
79
80                 data_to_words(y, TF_BLOCK_SIZE);
81                 memcpy(uout, y, sl);
82         }
83
84         memset(x, 0, TF_BLOCK_SIZE);
85         memset(y, 0, TF_BLOCK_SIZE);
86 }