comparison libtomcrypt/src/modes/xts/xts_encrypt.c @ 1435:f849a5ca2efc

update to libtomcrypt 1.17 (with Dropbear changes)
author Matt Johnston <matt@ucc.asn.au>
date Sat, 24 Jun 2017 17:50:50 +0800
parents
children 6dba84798cd5
comparison
equal deleted inserted replaced
1434:27b9ddb06b09 1435:f849a5ca2efc
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 * Tom St Denis, [email protected], http://libtom.org
10 */
11 #include "tomcrypt.h"
12
13 /**
14 Source donated by Elliptic Semiconductor Inc (www.ellipticsemi.com) to the LibTom Projects
15 */
16
17 #ifdef LTC_XTS_MODE
18
19 static int tweak_crypt(const unsigned char *P, unsigned char *C, unsigned char *T, symmetric_xts *xts)
20 {
21 unsigned long x;
22 int err;
23
24 /* tweak encrypt block i */
25 #ifdef LTC_FAST
26 for (x = 0; x < 16; x += sizeof(LTC_FAST_TYPE)) {
27 *((LTC_FAST_TYPE*)&C[x]) = *((LTC_FAST_TYPE*)&P[x]) ^ *((LTC_FAST_TYPE*)&T[x]);
28 }
29 #else
30 for (x = 0; x < 16; x++) {
31 C[x] = P[x] ^ T[x];
32 }
33 #endif
34
35 if ((err = cipher_descriptor[xts->cipher].ecb_encrypt(C, C, &xts->key1)) != CRYPT_OK) {
36 return err;
37 }
38
39 #ifdef LTC_FAST
40 for (x = 0; x < 16; x += sizeof(LTC_FAST_TYPE)) {
41 *((LTC_FAST_TYPE*)&C[x]) ^= *((LTC_FAST_TYPE*)&T[x]);
42 }
43 #else
44 for (x = 0; x < 16; x++) {
45 C[x] = C[x] ^ T[x];
46 }
47 #endif
48
49 /* LFSR the tweak */
50 xts_mult_x(T);
51
52 return CRYPT_OK;
53 }
54
55 /** XTS Encryption
56 @param pt [in] Plaintext
57 @param ptlen Length of plaintext (and ciphertext)
58 @param ct [out] Ciphertext
59 @param tweak [in] The 128--bit encryption tweak (e.g. sector number)
60 @param xts The XTS structure
61 Returns CRYPT_OK upon success
62 */
63 int xts_encrypt(
64 const unsigned char *pt, unsigned long ptlen,
65 unsigned char *ct,
66 const unsigned char *tweak,
67 symmetric_xts *xts)
68 {
69 unsigned char PP[16], CC[16], T[16];
70 unsigned long i, m, mo, lim;
71 int err;
72
73 /* check inputs */
74 LTC_ARGCHK(pt != NULL);
75 LTC_ARGCHK(ct != NULL);
76 LTC_ARGCHK(tweak != NULL);
77 LTC_ARGCHK(xts != NULL);
78
79 /* check if valid */
80 if ((err = cipher_is_valid(xts->cipher)) != CRYPT_OK) {
81 return err;
82 }
83
84 /* get number of blocks */
85 m = ptlen >> 4;
86 mo = ptlen & 15;
87
88 /* must have at least one full block */
89 if (m == 0) {
90 return CRYPT_INVALID_ARG;
91 }
92
93 /* encrypt the tweak */
94 if ((err = cipher_descriptor[xts->cipher].ecb_encrypt(tweak, T, &xts->key2)) != CRYPT_OK) {
95 return err;
96 }
97
98 /* for i = 0 to m-2 do */
99 if (mo == 0) {
100 lim = m;
101 } else {
102 lim = m - 1;
103 }
104
105 for (i = 0; i < lim; i++) {
106 err = tweak_crypt(pt, ct, T, xts);
107 ct += 16;
108 pt += 16;
109 }
110
111 /* if ptlen not divide 16 then */
112 if (mo > 0) {
113 /* CC = tweak encrypt block m-1 */
114 if ((err = tweak_crypt(pt, CC, T, xts)) != CRYPT_OK) {
115 return err;
116 }
117
118 /* Cm = first ptlen % 16 bytes of CC */
119 for (i = 0; i < mo; i++) {
120 PP[i] = pt[16+i];
121 ct[16+i] = CC[i];
122 }
123
124 for (; i < 16; i++) {
125 PP[i] = CC[i];
126 }
127
128 /* Cm-1 = Tweak encrypt PP */
129 if ((err = tweak_crypt(PP, ct, T, xts)) != CRYPT_OK) {
130 return err;
131 }
132 }
133
134 return err;
135 }
136
137 #endif
138
139 /* $Source$ */
140 /* $Revision$ */
141 /* $Date$ */
142