4 void tf_cbc_encrypt(const void *key, void *iv, void *out, const void *in, size_t sz)
6 const TF_BYTE_TYPE *uin = in;
7 TF_BYTE_TYPE *uout = out;
8 TF_UNIT_TYPE x[TF_NR_BLOCK_UNITS], y[TF_NR_BLOCK_UNITS];
9 TF_UNIT_TYPE *uiv = iv;
10 const TF_UNIT_TYPE *ukey = key;
13 if (sl >= TF_BLOCK_SIZE) {
15 memcpy(x, uin, TF_BLOCK_SIZE);
17 data_to_words(x, TF_BLOCK_SIZE);
19 for (i = 0; i < TF_NR_BLOCK_UNITS; i++) y[i] = x[i] ^ uiv[i];
20 tf_encrypt_rawblk(x, y, ukey);
21 memcpy(iv, x, TF_BLOCK_SIZE);
23 data_to_words(x, TF_BLOCK_SIZE);
24 memcpy(uout, x, TF_BLOCK_SIZE);
25 uout += TF_BLOCK_SIZE;
26 } while ((sl -= TF_BLOCK_SIZE) >= TF_BLOCK_SIZE);
30 memset(x, 0, TF_BLOCK_SIZE);
32 data_to_words(x, TF_BLOCK_SIZE);
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];
38 data_to_words(y, TF_BLOCK_SIZE);
42 memset(x, 0, TF_BLOCK_SIZE);
43 memset(y, 0, TF_BLOCK_SIZE);
46 void tf_cbc_decrypt(const void *key, void *iv, void *out, const void *in, size_t sz)
48 const TF_BYTE_TYPE *uin = in;
49 TF_BYTE_TYPE *uout = out;
50 TF_UNIT_TYPE x[TF_NR_BLOCK_UNITS], y[TF_NR_BLOCK_UNITS], t[TF_NR_BLOCK_UNITS];
51 TF_UNIT_TYPE *uiv = iv;
52 const TF_UNIT_TYPE *ukey = key;
55 if (sl >= TF_BLOCK_SIZE) {
57 memcpy(x, uin, TF_BLOCK_SIZE);
59 data_to_words(x, TF_BLOCK_SIZE);
61 memcpy(t, x, TF_BLOCK_SIZE);
62 tf_decrypt_rawblk(y, x, ukey);
63 for (i = 0; i < TF_NR_BLOCK_UNITS; i++) y[i] ^= uiv[i];
64 memcpy(iv, t, TF_BLOCK_SIZE);
66 data_to_words(y, TF_BLOCK_SIZE);
67 memcpy(uout, y, TF_BLOCK_SIZE);
68 uout += TF_BLOCK_SIZE;
69 } while ((sl -= TF_BLOCK_SIZE) >= TF_BLOCK_SIZE);
73 memset(x, 0, TF_BLOCK_SIZE);
75 data_to_words(x, TF_BLOCK_SIZE);
77 ctr_inc(uiv, TF_NR_BLOCK_UNITS);
78 tf_encrypt_rawblk(y, uiv, ukey);
79 for (i = 0; i < TF_NR_BLOCK_UNITS; i++) y[i] ^= x[i];
81 data_to_words(y, TF_BLOCK_SIZE);
85 memset(t, 0, TF_BLOCK_SIZE);
86 memset(x, 0, TF_BLOCK_SIZE);
87 memset(y, 0, TF_BLOCK_SIZE);