Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/com/sun/crypto/provider/CipherTextStealing.java
41161 views
1
/*
2
* Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package com.sun.crypto.provider;
27
28
import javax.crypto.IllegalBlockSizeException;
29
import javax.crypto.ShortBufferException;
30
31
/**
32
* This class represents ciphers in cipher text stealing (CTS) mode.
33
* <br>CTS provides a way to allow block ciphers to operate on partial
34
* blocks without padding, and all bits of the message go through
35
* the encryption algorithm, rather than simply being XOR'd.
36
* <br>More details can be found in RFC 2040 section 8 "Description
37
* of RC5-CTS".
38
*
39
* <p>This mode is implemented independently of a particular cipher.
40
* Ciphers to which this mode should apply (e.g., DES) must be
41
* <i>plugged-in</i> using the constructor.
42
*
43
* <p>NOTE#1: CTS requires the input data to be at least one block
44
* long. Thus, callers of this class has to buffer the input data
45
* to make sure the input data passed to encryptFinal()/decryptFinal()
46
* is not shorter than a block.
47
* <p>NOTE#2: This class does not deal with buffering or padding
48
* just like all other cipher mode implementations.
49
*
50
* @author Valerie Peng
51
*/
52
53
final class CipherTextStealing extends CipherBlockChaining {
54
55
CipherTextStealing(SymmetricCipher embeddedCipher) {
56
super(embeddedCipher);
57
}
58
59
/**
60
* Gets the name of this feedback mode.
61
*
62
* @return the string <code>CBC</code>
63
*/
64
String getFeedback() {
65
return "CTS";
66
}
67
68
/**
69
* Performs the last encryption operation.
70
*
71
* <p>The input plain text <code>plain</code>, starting at
72
* <code>plainOffset</code> and ending at
73
* <code>(plainOffset + len - 1)</code>, is encrypted.
74
* The result is stored in <code>cipher</code>, starting at
75
* <code>cipherOffset</code>.
76
*
77
* <p>It is the application's responsibility to make sure that
78
* <code>plainLen</code> is a multiple of the embedded cipher's block size,
79
* as any excess bytes are ignored.
80
*
81
* @param plain the buffer with the input data to be encrypted
82
* @param plainOffset the offset in <code>plain</code>
83
* @param plainLen the length of the input data
84
* @param cipher the buffer for the result
85
* @param cipherOffset the offset in <code>cipher</code>
86
* @return the number of bytes placed into <code>cipher</code>
87
*/
88
int encryptFinal(byte[] plain, int plainOffset, int plainLen,
89
byte[] cipher, int cipherOffset)
90
throws IllegalBlockSizeException {
91
92
if (plainLen < blockSize) {
93
throw new IllegalBlockSizeException("input is too short!");
94
} else if (plainLen == blockSize) {
95
encrypt(plain, plainOffset, plainLen, cipher, cipherOffset);
96
} else {
97
// number of bytes in the last block
98
int nLeft = plainLen % blockSize;
99
if (nLeft == 0) {
100
encrypt(plain, plainOffset, plainLen, cipher, cipherOffset);
101
// swap the last two blocks after encryption
102
int lastBlkIndex = cipherOffset + plainLen - blockSize;
103
int nextToLastBlkIndex = lastBlkIndex - blockSize;
104
byte[] tmp = new byte[blockSize];
105
System.arraycopy(cipher, lastBlkIndex, tmp, 0, blockSize);
106
System.arraycopy(cipher, nextToLastBlkIndex,
107
cipher, lastBlkIndex, blockSize);
108
System.arraycopy(tmp, 0, cipher, nextToLastBlkIndex,
109
blockSize);
110
} else {
111
int newPlainLen = plainLen - (blockSize + nLeft);
112
if (newPlainLen > 0) {
113
encrypt(plain, plainOffset, newPlainLen, cipher,
114
cipherOffset);
115
plainOffset += newPlainLen;
116
cipherOffset += newPlainLen;
117
}
118
119
// Do final CTS step for last two blocks (the second of which
120
// may or may not be incomplete).
121
byte[] tmp = new byte[blockSize];
122
// now encrypt the next-to-last block
123
for (int i = 0; i < blockSize; i++) {
124
tmp[i] = (byte) (plain[plainOffset+i] ^ r[i]);
125
}
126
byte[] tmp2 = new byte[blockSize];
127
embeddedCipher.encryptBlock(tmp, 0, tmp2, 0);
128
System.arraycopy(tmp2, 0, cipher,
129
cipherOffset+blockSize, nLeft);
130
// encrypt the last block
131
for (int i=0; i<nLeft; i++) {
132
tmp2[i] = (byte)
133
(plain[plainOffset+blockSize+i] ^ tmp2[i]);
134
}
135
embeddedCipher.encryptBlock(tmp2, 0, cipher, cipherOffset);
136
}
137
}
138
return plainLen;
139
}
140
141
/**
142
* Performs decryption operation.
143
*
144
* <p>The input cipher text <code>cipher</code>, starting at
145
* <code>cipherOffset</code> and ending at
146
* <code>(cipherOffset + len - 1)</code>, is decrypted.
147
* The result is stored in <code>plain</code>, starting at
148
* <code>plainOffset</code>.
149
*
150
* <p>It is the application's responsibility to make sure that
151
* <code>cipherLen</code> is a multiple of the embedded cipher's block
152
* size, as any excess bytes are ignored.
153
*
154
* <p>It is also the application's responsibility to make sure that
155
* <code>init</code> has been called before this method is called.
156
* (This check is omitted here, to avoid double checking.)
157
*
158
* @param cipher the buffer with the input data to be decrypted
159
* @param cipherOffset the offset in <code>cipherOffset</code>
160
* @param cipherLen the length of the input data
161
* @param plain the buffer for the result
162
* @param plainOffset the offset in <code>plain</code>
163
* @return the number of bytes placed into <code>plain</code>
164
*/
165
int decryptFinal(byte[] cipher, int cipherOffset, int cipherLen,
166
byte[] plain, int plainOffset)
167
throws IllegalBlockSizeException {
168
if (cipherLen < blockSize) {
169
throw new IllegalBlockSizeException("input is too short!");
170
} else if (cipherLen == blockSize) {
171
decrypt(cipher, cipherOffset, cipherLen, plain, plainOffset);
172
} else {
173
// number of bytes in the last block
174
int nLeft = cipherLen % blockSize;
175
if (nLeft == 0) {
176
// swap the last two blocks before decryption
177
int lastBlkIndex = cipherOffset + cipherLen - blockSize;
178
int nextToLastBlkIndex =
179
cipherOffset + cipherLen - 2*blockSize;
180
byte[] tmp = new byte[2*blockSize];
181
System.arraycopy(cipher, lastBlkIndex, tmp, 0, blockSize);
182
System.arraycopy(cipher, nextToLastBlkIndex,
183
tmp, blockSize, blockSize);
184
int cipherLen2 = cipherLen-2*blockSize;
185
decrypt(cipher, cipherOffset, cipherLen2, plain, plainOffset);
186
decrypt(tmp, 0, 2*blockSize, plain, plainOffset+cipherLen2);
187
} else {
188
int newCipherLen = cipherLen-(blockSize+nLeft);
189
if (newCipherLen > 0) {
190
decrypt(cipher, cipherOffset, newCipherLen, plain,
191
plainOffset);
192
cipherOffset += newCipherLen;
193
plainOffset += newCipherLen;
194
}
195
// Do final CTS step for last two blocks (the second of which
196
// may or may not be incomplete).
197
198
// now decrypt the next-to-last block
199
byte[] tmp = new byte[blockSize];
200
embeddedCipher.decryptBlock(cipher, cipherOffset, tmp, 0);
201
for (int i = 0; i < nLeft; i++) {
202
plain[plainOffset+blockSize+i] =
203
(byte) (cipher[cipherOffset+blockSize+i] ^ tmp[i]);
204
}
205
206
// decrypt the last block
207
System.arraycopy(cipher, cipherOffset+blockSize, tmp, 0,
208
nLeft);
209
embeddedCipher.decryptBlock(tmp, 0, plain, plainOffset);
210
//System.arraycopy(r, 0, tmp, 0, r.length);
211
for (int i=0; i<blockSize; i++) {
212
plain[plainOffset+i] = (byte)
213
(plain[plainOffset+i]^r[i]);
214
}
215
}
216
}
217
return cipherLen;
218
}
219
}
220
221