Path: blob/master/src/java.base/share/classes/com/sun/crypto/provider/DESedeKey.java
41161 views
/*1* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.crypto.provider;2627import java.lang.ref.Reference;28import java.security.MessageDigest;29import java.security.KeyRep;30import java.security.InvalidKeyException;31import javax.crypto.SecretKey;32import javax.crypto.spec.DESedeKeySpec;3334import jdk.internal.ref.CleanerFactory;3536/**37* This class represents a DES-EDE key.38*39* @author Jan Luehe40*41*/4243final class DESedeKey implements SecretKey {4445@java.io.Serial46static final long serialVersionUID = 2463986565756745178L;4748private byte[] key;4950/**51* Creates a DES-EDE key from a given key.52*53* @param key the given key54*55* @exception InvalidKeyException if the given key has a wrong size56*/57DESedeKey(byte[] key) throws InvalidKeyException {58this(key, 0);59}6061/**62* Uses the first 24 bytes in <code>key</code>, beginning at63* <code>offset</code>, as the DES-EDE key64*65* @param key the buffer with the DES-EDE key66* @param offset the offset in <code>key</code>, where the DES-EDE key67* starts68*69* @exception InvalidKeyException if the given key has a wrong size70*/71DESedeKey(byte[] key, int offset) throws InvalidKeyException {7273if (key==null || ((key.length-offset)<DESedeKeySpec.DES_EDE_KEY_LEN)) {74throw new InvalidKeyException("Wrong key size");75}76this.key = new byte[DESedeKeySpec.DES_EDE_KEY_LEN];77System.arraycopy(key, offset, this.key, 0,78DESedeKeySpec.DES_EDE_KEY_LEN);79DESKeyGenerator.setParityBit(this.key, 0);80DESKeyGenerator.setParityBit(this.key, 8);81DESKeyGenerator.setParityBit(this.key, 16);8283// Use the cleaner to zero the key when no longer referenced84final byte[] k = this.key;85CleanerFactory.cleaner().register(this,86() -> java.util.Arrays.fill(k, (byte)0x00));87}8889public byte[] getEncoded() {90// The key is zeroized by finalize()91// The reachability fence ensures finalize() isn't called early92byte[] result = key.clone();93Reference.reachabilityFence(this);94return result;95}9697public String getAlgorithm() {98return "DESede";99}100101public String getFormat() {102return "RAW";103}104105/**106* Calculates a hash code value for the object.107* Objects that are equal will also have the same hashcode.108*/109public int hashCode() {110int retval = 0;111for (int i = 1; i < this.key.length; i++) {112retval += this.key[i] * i;113}114return(retval ^= "desede".hashCode());115}116117public boolean equals(Object obj) {118if (this == obj)119return true;120121if (!(obj instanceof SecretKey))122return false;123124String thatAlg = ((SecretKey)obj).getAlgorithm();125if (!(thatAlg.equalsIgnoreCase("DESede"))126&& !(thatAlg.equalsIgnoreCase("TripleDES")))127return false;128129byte[] thatKey = ((SecretKey)obj).getEncoded();130boolean ret = MessageDigest.isEqual(this.key, thatKey);131java.util.Arrays.fill(thatKey, (byte)0x00);132return ret;133}134135/**136* readObject is called to restore the state of this key from137* a stream.138*/139@java.io.Serial140private void readObject(java.io.ObjectInputStream s)141throws java.io.IOException, ClassNotFoundException142{143s.defaultReadObject();144key = key.clone();145}146147/**148* Replace the DESede key to be serialized.149*150* @return the standard KeyRep object to be serialized151*152* @throws java.io.ObjectStreamException if a new object representing153* this DESede key could not be created154*/155@java.io.Serial156private Object writeReplace() throws java.io.ObjectStreamException {157return new KeyRep(KeyRep.Type.SECRET,158getAlgorithm(),159getFormat(),160key);161}162}163164165