Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/crypto/provider/CipherBlockChaining.java
38922 views
1
/*
2
* Copyright (c) 1997, 2018, 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 java.security.InvalidKeyException;
29
import java.security.ProviderException;
30
import java.util.Objects;
31
32
/**
33
* This class represents ciphers in cipher block chaining (CBC) mode.
34
*
35
* <p>This mode is implemented independently of a particular cipher.
36
* Ciphers to which this mode should apply (e.g., DES) must be
37
* <i>plugged-in</i> using the constructor.
38
*
39
* <p>NOTE: This class does not deal with buffering or padding.
40
*
41
* @author Gigi Ankeny
42
*/
43
44
class CipherBlockChaining extends FeedbackCipher {
45
46
/*
47
* random bytes that are initialized with iv
48
*/
49
protected byte[] r;
50
51
/*
52
* output buffer
53
*/
54
private byte[] k;
55
56
// variables for save/restore calls
57
private byte[] rSave = null;
58
59
CipherBlockChaining(SymmetricCipher embeddedCipher) {
60
super(embeddedCipher);
61
k = new byte[blockSize];
62
r = new byte[blockSize];
63
}
64
65
/**
66
* Gets the name of this feedback mode.
67
*
68
* @return the string <code>CBC</code>
69
*/
70
String getFeedback() {
71
return "CBC";
72
}
73
74
/**
75
* Initializes the cipher in the specified mode with the given key
76
* and iv.
77
*
78
* @param decrypting flag indicating encryption or decryption
79
* @param algorithm the algorithm name
80
* @param key the key
81
* @param iv the iv
82
*
83
* @exception InvalidKeyException if the given key is inappropriate for
84
* initializing this cipher
85
*/
86
void init(boolean decrypting, String algorithm, byte[] key, byte[] iv)
87
throws InvalidKeyException {
88
if ((key == null) || (iv == null) || (iv.length != blockSize)) {
89
throw new InvalidKeyException("Internal error");
90
}
91
this.iv = iv;
92
reset();
93
embeddedCipher.init(decrypting, algorithm, key);
94
}
95
96
/**
97
* Resets the iv to its original value.
98
* This is used when doFinal is called in the Cipher class, so that the
99
* cipher can be reused (with its original iv).
100
*/
101
void reset() {
102
System.arraycopy(iv, 0, r, 0, blockSize);
103
}
104
105
/**
106
* Save the current content of this cipher.
107
*/
108
void save() {
109
if (rSave == null) {
110
rSave = new byte[blockSize];
111
}
112
System.arraycopy(r, 0, rSave, 0, blockSize);
113
}
114
115
/**
116
* Restores the content of this cipher to the previous saved one.
117
*/
118
void restore() {
119
System.arraycopy(rSave, 0, r, 0, blockSize);
120
}
121
122
/**
123
* Performs encryption operation.
124
*
125
* <p>The input plain text <code>plain</code>, starting at
126
* <code>plainOffset</code> and ending at
127
* <code>(plainOffset + plainLen - 1)</code>, is encrypted.
128
* The result is stored in <code>cipher</code>, starting at
129
* <code>cipherOffset</code>.
130
*
131
* @param plain the buffer with the input data to be encrypted
132
* @param plainOffset the offset in <code>plain</code>
133
* @param plainLen the length of the input data
134
* @param cipher the buffer for the result
135
* @param cipherOffset the offset in <code>cipher</code>
136
* @exception ProviderException if <code>len</code> is not
137
* a multiple of the block size
138
* @return the length of the encrypted data
139
*/
140
int encrypt(byte[] plain, int plainOffset, int plainLen,
141
byte[] cipher, int cipherOffset) {
142
if (plainLen <= 0) {
143
return plainLen;
144
}
145
RangeUtil.blockSizeCheck(plainLen, blockSize);
146
RangeUtil.nullAndBoundsCheck(plain, plainOffset, plainLen);
147
RangeUtil.nullAndBoundsCheck(cipher, cipherOffset, plainLen);
148
return implEncrypt(plain, plainOffset, plainLen,
149
cipher, cipherOffset);
150
}
151
152
private int implEncrypt(byte[] plain, int plainOffset, int plainLen,
153
byte[] cipher, int cipherOffset)
154
{
155
int endIndex = plainOffset + plainLen;
156
157
for (; plainOffset < endIndex;
158
plainOffset += blockSize, cipherOffset += blockSize) {
159
for (int i = 0; i < blockSize; i++) {
160
k[i] = (byte)(plain[i + plainOffset] ^ r[i]);
161
}
162
embeddedCipher.encryptBlock(k, 0, cipher, cipherOffset);
163
System.arraycopy(cipher, cipherOffset, r, 0, blockSize);
164
}
165
return plainLen;
166
}
167
168
/**
169
* Performs decryption operation.
170
*
171
* <p>The input cipher text <code>cipher</code>, starting at
172
* <code>cipherOffset</code> and ending at
173
* <code>(cipherOffset + cipherLen - 1)</code>, is decrypted.
174
* The result is stored in <code>plain</code>, starting at
175
* <code>plainOffset</code>.
176
*
177
* <p>It is also the application's responsibility to make sure that
178
* <code>init</code> has been called before this method is called.
179
* (This check is omitted here, to avoid double checking.)
180
*
181
* @param cipher the buffer with the input data to be decrypted
182
* @param cipherOffset the offset in <code>cipherOffset</code>
183
* @param cipherLen the length of the input data
184
* @param plain the buffer for the result
185
* @param plainOffset the offset in <code>plain</code>
186
* @exception ProviderException if <code>len</code> is not
187
* a multiple of the block size
188
* @return the length of the decrypted data
189
*/
190
int decrypt(byte[] cipher, int cipherOffset, int cipherLen,
191
byte[] plain, int plainOffset) {
192
if (cipherLen <= 0) {
193
return cipherLen;
194
}
195
RangeUtil.blockSizeCheck(cipherLen, blockSize);
196
RangeUtil.nullAndBoundsCheck(cipher, cipherOffset, cipherLen);
197
RangeUtil.nullAndBoundsCheck(plain, plainOffset, cipherLen);
198
return implDecrypt(cipher, cipherOffset, cipherLen, plain, plainOffset);
199
}
200
201
private int implDecrypt(byte[] cipher, int cipherOffset, int cipherLen,
202
byte[] plain, int plainOffset)
203
{
204
int endIndex = cipherOffset + cipherLen;
205
206
for (; cipherOffset < endIndex;
207
cipherOffset += blockSize, plainOffset += blockSize) {
208
embeddedCipher.decryptBlock(cipher, cipherOffset, k, 0);
209
for (int i = 0; i < blockSize; i++) {
210
plain[i + plainOffset] = (byte)(k[i] ^ r[i]);
211
}
212
System.arraycopy(cipher, cipherOffset, r, 0, blockSize);
213
}
214
return cipherLen;
215
}
216
}
217
218