summaryrefslogtreecommitdiff
path: root/utils/tomcrypt/src/modes
diff options
context:
space:
mode:
Diffstat (limited to 'utils/tomcrypt/src/modes')
-rw-r--r--utils/tomcrypt/src/modes/cbc/cbc_decrypt.c95
-rw-r--r--utils/tomcrypt/src/modes/cbc/cbc_encrypt.c96
-rw-r--r--utils/tomcrypt/src/modes/cbc/cbc_start.c60
3 files changed, 251 insertions, 0 deletions
diff --git a/utils/tomcrypt/src/modes/cbc/cbc_decrypt.c b/utils/tomcrypt/src/modes/cbc/cbc_decrypt.c
new file mode 100644
index 0000000000..685aff80b0
--- /dev/null
+++ b/utils/tomcrypt/src/modes/cbc/cbc_decrypt.c
@@ -0,0 +1,95 @@
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 cbc_decrypt.c
13 CBC implementation, encrypt block, Tom St Denis
14*/
15
16
17#ifdef LTC_CBC_MODE
18
19/**
20 CBC decrypt
21 @param ct Ciphertext
22 @param pt [out] Plaintext
23 @param len The number of bytes to process (must be multiple of block length)
24 @param cbc CBC state
25 @return CRYPT_OK if successful
26*/
27int cbc_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_CBC *cbc)
28{
29 int x, err;
30 unsigned char tmp[16];
31#ifdef LTC_FAST
32 LTC_FAST_TYPE tmpy;
33#else
34 unsigned char tmpy;
35#endif
36
37 LTC_ARGCHK(pt != NULL);
38 LTC_ARGCHK(ct != NULL);
39 LTC_ARGCHK(cbc != NULL);
40
41 if ((err = cipher_is_valid(cbc->cipher)) != CRYPT_OK) {
42 return err;
43 }
44
45 /* is blocklen valid? */
46 if (cbc->blocklen < 1 || cbc->blocklen > (int)sizeof(cbc->IV) || cbc->blocklen > (int)sizeof(tmp)) {
47 return CRYPT_INVALID_ARG;
48 }
49
50 if (len % cbc->blocklen) {
51 return CRYPT_INVALID_ARG;
52 }
53#ifdef LTC_FAST
54 if (cbc->blocklen % sizeof(LTC_FAST_TYPE)) {
55 return CRYPT_INVALID_ARG;
56 }
57#endif
58
59 if (cipher_descriptor[cbc->cipher].accel_cbc_decrypt != NULL) {
60 return cipher_descriptor[cbc->cipher].accel_cbc_decrypt(ct, pt, len / cbc->blocklen, cbc->IV, &cbc->key);
61 } else {
62 while (len) {
63 /* decrypt */
64 if ((err = cipher_descriptor[cbc->cipher].ecb_decrypt(ct, tmp, &cbc->key)) != CRYPT_OK) {
65 return err;
66 }
67
68 /* xor IV against plaintext */
69 #if defined(LTC_FAST)
70 for (x = 0; x < cbc->blocklen; x += sizeof(LTC_FAST_TYPE)) {
71 tmpy = *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)cbc->IV + x)) ^ *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)tmp + x));
72 *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)cbc->IV + x)) = *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)ct + x));
73 *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)pt + x)) = tmpy;
74 }
75 #else
76 for (x = 0; x < cbc->blocklen; x++) {
77 tmpy = tmp[x] ^ cbc->IV[x];
78 cbc->IV[x] = ct[x];
79 pt[x] = tmpy;
80 }
81 #endif
82
83 ct += cbc->blocklen;
84 pt += cbc->blocklen;
85 len -= cbc->blocklen;
86 }
87 }
88 return CRYPT_OK;
89}
90
91#endif
92
93/* ref: HEAD -> master, tag: v1.18.2 */
94/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
95/* commit time: 2018-07-01 22:49:01 +0200 */
diff --git a/utils/tomcrypt/src/modes/cbc/cbc_encrypt.c b/utils/tomcrypt/src/modes/cbc/cbc_encrypt.c
new file mode 100644
index 0000000000..52f339de14
--- /dev/null
+++ b/utils/tomcrypt/src/modes/cbc/cbc_encrypt.c
@@ -0,0 +1,96 @@
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 cbc_encrypt.c
13 CBC implementation, encrypt block, Tom St Denis
14*/
15
16
17#ifdef LTC_CBC_MODE
18
19/**
20 CBC encrypt
21 @param pt Plaintext
22 @param ct [out] Ciphertext
23 @param len The number of bytes to process (must be multiple of block length)
24 @param cbc CBC state
25 @return CRYPT_OK if successful
26*/
27int cbc_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_CBC *cbc)
28{
29 int x, err;
30
31 LTC_ARGCHK(pt != NULL);
32 LTC_ARGCHK(ct != NULL);
33 LTC_ARGCHK(cbc != NULL);
34
35 if ((err = cipher_is_valid(cbc->cipher)) != CRYPT_OK) {
36 return err;
37 }
38
39 /* is blocklen valid? */
40 if (cbc->blocklen < 1 || cbc->blocklen > (int)sizeof(cbc->IV)) {
41 return CRYPT_INVALID_ARG;
42 }
43
44 if (len % cbc->blocklen) {
45 return CRYPT_INVALID_ARG;
46 }
47#ifdef LTC_FAST
48 if (cbc->blocklen % sizeof(LTC_FAST_TYPE)) {
49 return CRYPT_INVALID_ARG;
50 }
51#endif
52
53 if (cipher_descriptor[cbc->cipher].accel_cbc_encrypt != NULL) {
54 return cipher_descriptor[cbc->cipher].accel_cbc_encrypt(pt, ct, len / cbc->blocklen, cbc->IV, &cbc->key);
55 } else {
56 while (len) {
57 /* xor IV against plaintext */
58 #if defined(LTC_FAST)
59 for (x = 0; x < cbc->blocklen; x += sizeof(LTC_FAST_TYPE)) {
60 *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)cbc->IV + x)) ^= *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)pt + x));
61 }
62 #else
63 for (x = 0; x < cbc->blocklen; x++) {
64 cbc->IV[x] ^= pt[x];
65 }
66 #endif
67
68 /* encrypt */
69 if ((err = cipher_descriptor[cbc->cipher].ecb_encrypt(cbc->IV, ct, &cbc->key)) != CRYPT_OK) {
70 return err;
71 }
72
73 /* store IV [ciphertext] for a future block */
74 #if defined(LTC_FAST)
75 for (x = 0; x < cbc->blocklen; x += sizeof(LTC_FAST_TYPE)) {
76 *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)cbc->IV + x)) = *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)ct + x));
77 }
78 #else
79 for (x = 0; x < cbc->blocklen; x++) {
80 cbc->IV[x] = ct[x];
81 }
82 #endif
83
84 ct += cbc->blocklen;
85 pt += cbc->blocklen;
86 len -= cbc->blocklen;
87 }
88 }
89 return CRYPT_OK;
90}
91
92#endif
93
94/* ref: HEAD -> master, tag: v1.18.2 */
95/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
96/* commit time: 2018-07-01 22:49:01 +0200 */
diff --git a/utils/tomcrypt/src/modes/cbc/cbc_start.c b/utils/tomcrypt/src/modes/cbc/cbc_start.c
new file mode 100644
index 0000000000..08483e2b8f
--- /dev/null
+++ b/utils/tomcrypt/src/modes/cbc/cbc_start.c
@@ -0,0 +1,60 @@
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 cbc_start.c
13 CBC implementation, start chain, Tom St Denis
14*/
15
16#ifdef LTC_CBC_MODE
17
18/**
19 Initialize a CBC context
20 @param cipher The index of the cipher desired
21 @param IV The initialization vector
22 @param key The secret key
23 @param keylen The length of the secret key (octets)
24 @param num_rounds Number of rounds in the cipher desired (0 for default)
25 @param cbc The CBC state to initialize
26 @return CRYPT_OK if successful
27*/
28int cbc_start(int cipher, const unsigned char *IV, const unsigned char *key,
29 int keylen, int num_rounds, symmetric_CBC *cbc)
30{
31 int x, err;
32
33 LTC_ARGCHK(IV != NULL);
34 LTC_ARGCHK(key != NULL);
35 LTC_ARGCHK(cbc != NULL);
36
37 /* bad param? */
38 if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
39 return err;
40 }
41
42 /* setup cipher */
43 if ((err = cipher_descriptor[cipher].setup(key, keylen, num_rounds, &cbc->key)) != CRYPT_OK) {
44 return err;
45 }
46
47 /* copy IV */
48 cbc->blocklen = cipher_descriptor[cipher].block_length;
49 cbc->cipher = cipher;
50 for (x = 0; x < cbc->blocklen; x++) {
51 cbc->IV[x] = IV[x];
52 }
53 return CRYPT_OK;
54}
55
56#endif
57
58/* ref: HEAD -> master, tag: v1.18.2 */
59/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
60/* commit time: 2018-07-01 22:49:01 +0200 */