Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/tomcrypt/src/modes/f8/f8_encrypt.c
5972 views
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 f8_encrypt.c
13
F8 implementation, encrypt data, Tom St Denis
14
*/
15
16
#ifdef LTC_F8_MODE
17
18
/**
19
F8 encrypt
20
@param pt Plaintext
21
@param ct [out] Ciphertext
22
@param len Length of plaintext (octets)
23
@param f8 F8 state
24
@return CRYPT_OK if successful
25
*/
26
int f8_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_F8 *f8)
27
{
28
int err, x;
29
unsigned char buf[MAXBLOCKSIZE];
30
LTC_ARGCHK(pt != NULL);
31
LTC_ARGCHK(ct != NULL);
32
LTC_ARGCHK(f8 != NULL);
33
if ((err = cipher_is_valid(f8->cipher)) != CRYPT_OK) {
34
return err;
35
}
36
37
/* is blocklen/padlen valid? */
38
if (f8->blocklen < 0 || f8->blocklen > (int)sizeof(f8->IV) ||
39
f8->padlen < 0 || f8->padlen > (int)sizeof(f8->IV)) {
40
return CRYPT_INVALID_ARG;
41
}
42
43
zeromem(buf, sizeof(buf));
44
45
/* make sure the pad is empty */
46
if (f8->padlen == f8->blocklen) {
47
/* xor of IV, MIV and blockcnt == what goes into cipher */
48
STORE32H(f8->blockcnt, (buf+(f8->blocklen-4)));
49
++(f8->blockcnt);
50
for (x = 0; x < f8->blocklen; x++) {
51
f8->IV[x] ^= f8->MIV[x] ^ buf[x];
52
}
53
if ((err = cipher_descriptor[f8->cipher].ecb_encrypt(f8->IV, f8->IV, &f8->key)) != CRYPT_OK) {
54
return err;
55
}
56
f8->padlen = 0;
57
}
58
59
#ifdef LTC_FAST
60
if (f8->padlen == 0) {
61
while (len >= (unsigned long)f8->blocklen) {
62
STORE32H(f8->blockcnt, (buf+(f8->blocklen-4)));
63
++(f8->blockcnt);
64
for (x = 0; x < f8->blocklen; x += sizeof(LTC_FAST_TYPE)) {
65
*(LTC_FAST_TYPE_PTR_CAST(&ct[x])) = *(LTC_FAST_TYPE_PTR_CAST(&pt[x])) ^ *(LTC_FAST_TYPE_PTR_CAST(&f8->IV[x]));
66
*(LTC_FAST_TYPE_PTR_CAST(&f8->IV[x])) ^= *(LTC_FAST_TYPE_PTR_CAST(&f8->MIV[x])) ^ *(LTC_FAST_TYPE_PTR_CAST(&buf[x]));
67
}
68
if ((err = cipher_descriptor[f8->cipher].ecb_encrypt(f8->IV, f8->IV, &f8->key)) != CRYPT_OK) {
69
return err;
70
}
71
len -= x;
72
pt += x;
73
ct += x;
74
}
75
}
76
#endif
77
78
while (len > 0) {
79
if (f8->padlen == f8->blocklen) {
80
/* xor of IV, MIV and blockcnt == what goes into cipher */
81
STORE32H(f8->blockcnt, (buf+(f8->blocklen-4)));
82
++(f8->blockcnt);
83
for (x = 0; x < f8->blocklen; x++) {
84
f8->IV[x] ^= f8->MIV[x] ^ buf[x];
85
}
86
if ((err = cipher_descriptor[f8->cipher].ecb_encrypt(f8->IV, f8->IV, &f8->key)) != CRYPT_OK) {
87
return err;
88
}
89
f8->padlen = 0;
90
}
91
*ct++ = *pt++ ^ f8->IV[f8->padlen++];
92
--len;
93
}
94
return CRYPT_OK;
95
}
96
97
#endif
98
99