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/CounterMode.java
38922 views
1
/*
2
* Copyright (c) 2002, 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
30
/**
31
* This class represents ciphers in counter (CTR) mode.
32
*
33
* <p>This mode is implemented independently of a particular cipher.
34
* Ciphers to which this mode should apply (e.g., DES) must be
35
* <i>plugged-in</i> using the constructor.
36
*
37
* <p>NOTE: This class does not deal with buffering or padding.
38
*
39
* @author Andreas Sterbenz
40
* @since 1.4.2
41
*/
42
final class CounterMode extends FeedbackCipher {
43
44
// current counter value
45
private final byte[] counter;
46
47
// encrypted bytes of the previous counter value
48
private final byte[] encryptedCounter;
49
50
// number of bytes in encryptedCounter already used up
51
private int used;
52
53
// variables for save/restore calls
54
private byte[] counterSave = null;
55
private byte[] encryptedCounterSave = null;
56
private int usedSave = 0;
57
58
CounterMode(SymmetricCipher embeddedCipher) {
59
super(embeddedCipher);
60
counter = new byte[blockSize];
61
encryptedCounter = new byte[blockSize];
62
}
63
64
/**
65
* Gets the name of the feedback mechanism
66
*
67
* @return the name of the feedback mechanism
68
*/
69
String getFeedback() {
70
return "CTR";
71
}
72
73
/**
74
* Resets the iv to its original value.
75
* This is used when doFinal is called in the Cipher class, so that the
76
* cipher can be reused (with its original iv).
77
*/
78
void reset() {
79
System.arraycopy(iv, 0, counter, 0, blockSize);
80
used = blockSize;
81
}
82
83
/**
84
* Save the current content of this cipher.
85
*/
86
void save() {
87
if (counterSave == null) {
88
counterSave = new byte[blockSize];
89
encryptedCounterSave = new byte[blockSize];
90
}
91
System.arraycopy(counter, 0, counterSave, 0, blockSize);
92
System.arraycopy(encryptedCounter, 0, encryptedCounterSave, 0,
93
blockSize);
94
usedSave = used;
95
}
96
97
/**
98
* Restores the content of this cipher to the previous saved one.
99
*/
100
void restore() {
101
System.arraycopy(counterSave, 0, counter, 0, blockSize);
102
System.arraycopy(encryptedCounterSave, 0, encryptedCounter, 0,
103
blockSize);
104
used = usedSave;
105
}
106
107
/**
108
* Initializes the cipher in the specified mode with the given key
109
* and iv.
110
*
111
* @param decrypting flag indicating encryption or decryption
112
* @param algorithm the algorithm name
113
* @param key the key
114
* @param iv the iv
115
*
116
* @exception InvalidKeyException if the given key is inappropriate for
117
* initializing this cipher
118
*/
119
void init(boolean decrypting, String algorithm, byte[] key, byte[] iv)
120
throws InvalidKeyException {
121
if ((key == null) || (iv == null) || (iv.length != blockSize)) {
122
throw new InvalidKeyException("Internal error");
123
}
124
this.iv = iv;
125
reset();
126
// always encrypt mode for embedded cipher
127
embeddedCipher.init(false, algorithm, key);
128
}
129
130
/**
131
* Performs encryption operation.
132
*
133
* <p>The input plain text <code>plain</code>, starting at
134
* <code>plainOffset</code> and ending at
135
* <code>(plainOffset + len - 1)</code>, is encrypted.
136
* The result is stored in <code>cipher</code>, starting at
137
* <code>cipherOffset</code>.
138
*
139
* @param in the buffer with the input data to be encrypted
140
* @param inOffset the offset in <code>plain</code>
141
* @param len the length of the input data
142
* @param out the buffer for the result
143
* @param outOff the offset in <code>cipher</code>
144
* @return the length of the encrypted data
145
*/
146
int encrypt(byte[] in, int inOff, int len, byte[] out, int outOff) {
147
return crypt(in, inOff, len, out, outOff);
148
}
149
150
// CTR encrypt and decrypt are identical
151
int decrypt(byte[] in, int inOff, int len, byte[] out, int outOff) {
152
return crypt(in, inOff, len, out, outOff);
153
}
154
155
/**
156
* Increment the counter value.
157
*/
158
private static void increment(byte[] b) {
159
int n = b.length - 1;
160
while ((n >= 0) && (++b[n] == 0)) {
161
n--;
162
}
163
}
164
165
/**
166
* Do the actual encryption/decryption operation.
167
* Essentially we XOR the input plaintext/ciphertext stream with a
168
* keystream generated by encrypting the counter values. Counter values
169
* are encrypted on demand.
170
*/
171
private int crypt(byte[] in, int inOff, int len, byte[] out, int outOff) {
172
if (len == 0) {
173
return 0;
174
}
175
176
RangeUtil.nullAndBoundsCheck(in, inOff, len);
177
RangeUtil.nullAndBoundsCheck(out, outOff, len);
178
179
int result = len;
180
while (len-- > 0) {
181
if (used >= blockSize) {
182
embeddedCipher.encryptBlock(counter, 0, encryptedCounter, 0);
183
increment(counter);
184
used = 0;
185
}
186
out[outOff++] = (byte)(in[inOff++] ^ encryptedCounter[used++]);
187
}
188
return result;
189
}
190
}
191
192