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/DESedeKey.java
41161 views
1
/*
2
* Copyright (c) 1997, 2021, 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.lang.ref.Reference;
29
import java.security.MessageDigest;
30
import java.security.KeyRep;
31
import java.security.InvalidKeyException;
32
import javax.crypto.SecretKey;
33
import javax.crypto.spec.DESedeKeySpec;
34
35
import jdk.internal.ref.CleanerFactory;
36
37
/**
38
* This class represents a DES-EDE key.
39
*
40
* @author Jan Luehe
41
*
42
*/
43
44
final class DESedeKey implements SecretKey {
45
46
@java.io.Serial
47
static final long serialVersionUID = 2463986565756745178L;
48
49
private byte[] key;
50
51
/**
52
* Creates a DES-EDE key from a given key.
53
*
54
* @param key the given key
55
*
56
* @exception InvalidKeyException if the given key has a wrong size
57
*/
58
DESedeKey(byte[] key) throws InvalidKeyException {
59
this(key, 0);
60
}
61
62
/**
63
* Uses the first 24 bytes in <code>key</code>, beginning at
64
* <code>offset</code>, as the DES-EDE key
65
*
66
* @param key the buffer with the DES-EDE key
67
* @param offset the offset in <code>key</code>, where the DES-EDE key
68
* starts
69
*
70
* @exception InvalidKeyException if the given key has a wrong size
71
*/
72
DESedeKey(byte[] key, int offset) throws InvalidKeyException {
73
74
if (key==null || ((key.length-offset)<DESedeKeySpec.DES_EDE_KEY_LEN)) {
75
throw new InvalidKeyException("Wrong key size");
76
}
77
this.key = new byte[DESedeKeySpec.DES_EDE_KEY_LEN];
78
System.arraycopy(key, offset, this.key, 0,
79
DESedeKeySpec.DES_EDE_KEY_LEN);
80
DESKeyGenerator.setParityBit(this.key, 0);
81
DESKeyGenerator.setParityBit(this.key, 8);
82
DESKeyGenerator.setParityBit(this.key, 16);
83
84
// Use the cleaner to zero the key when no longer referenced
85
final byte[] k = this.key;
86
CleanerFactory.cleaner().register(this,
87
() -> java.util.Arrays.fill(k, (byte)0x00));
88
}
89
90
public byte[] getEncoded() {
91
// The key is zeroized by finalize()
92
// The reachability fence ensures finalize() isn't called early
93
byte[] result = key.clone();
94
Reference.reachabilityFence(this);
95
return result;
96
}
97
98
public String getAlgorithm() {
99
return "DESede";
100
}
101
102
public String getFormat() {
103
return "RAW";
104
}
105
106
/**
107
* Calculates a hash code value for the object.
108
* Objects that are equal will also have the same hashcode.
109
*/
110
public int hashCode() {
111
int retval = 0;
112
for (int i = 1; i < this.key.length; i++) {
113
retval += this.key[i] * i;
114
}
115
return(retval ^= "desede".hashCode());
116
}
117
118
public boolean equals(Object obj) {
119
if (this == obj)
120
return true;
121
122
if (!(obj instanceof SecretKey))
123
return false;
124
125
String thatAlg = ((SecretKey)obj).getAlgorithm();
126
if (!(thatAlg.equalsIgnoreCase("DESede"))
127
&& !(thatAlg.equalsIgnoreCase("TripleDES")))
128
return false;
129
130
byte[] thatKey = ((SecretKey)obj).getEncoded();
131
boolean ret = MessageDigest.isEqual(this.key, thatKey);
132
java.util.Arrays.fill(thatKey, (byte)0x00);
133
return ret;
134
}
135
136
/**
137
* readObject is called to restore the state of this key from
138
* a stream.
139
*/
140
@java.io.Serial
141
private void readObject(java.io.ObjectInputStream s)
142
throws java.io.IOException, ClassNotFoundException
143
{
144
s.defaultReadObject();
145
key = key.clone();
146
}
147
148
/**
149
* Replace the DESede key to be serialized.
150
*
151
* @return the standard KeyRep object to be serialized
152
*
153
* @throws java.io.ObjectStreamException if a new object representing
154
* this DESede key could not be created
155
*/
156
@java.io.Serial
157
private Object writeReplace() throws java.io.ObjectStreamException {
158
return new KeyRep(KeyRep.Type.SECRET,
159
getAlgorithm(),
160
getFormat(),
161
key);
162
}
163
}
164
165