Path: blob/master/src/java.base/share/classes/com/sun/crypto/provider/DESKey.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.DESKeySpec;3334import jdk.internal.ref.CleanerFactory;3536/**37* This class represents a DES key.38*39* @author Jan Luehe40*41*/4243final class DESKey implements SecretKey {4445@java.io.Serial46static final long serialVersionUID = 7724971015953279128L;4748private byte[] key;4950/**51* Uses the first 8 bytes of the given key as the DES key.52*53* @param key the buffer with the DES key bytes.54*55* @exception InvalidKeyException if less than 8 bytes are available for56* the key.57*/58DESKey(byte[] key) throws InvalidKeyException {59this(key, 0);60}6162/**63* Uses the first 8 bytes in <code>key</code>, beginning at64* <code>offset</code>, as the DES key65*66* @param key the buffer with the DES key bytes.67* @param offset the offset in <code>key</code>, where the DES key bytes68* start.69*70* @exception InvalidKeyException if less than 8 bytes are available for71* the key.72*/73DESKey(byte[] key, int offset) throws InvalidKeyException {74if (key == null || key.length - offset < DESKeySpec.DES_KEY_LEN) {75throw new InvalidKeyException("Wrong key size");76}77this.key = new byte[DESKeySpec.DES_KEY_LEN];78System.arraycopy(key, offset, this.key, 0, DESKeySpec.DES_KEY_LEN);79DESKeyGenerator.setParityBit(this.key, 0);8081// Use the cleaner to zero the key when no longer referenced82final byte[] k = this.key;83CleanerFactory.cleaner().register(this,84() -> java.util.Arrays.fill(k, (byte)0x00));85}8687public byte[] getEncoded() {88// Return a copy of the key, rather than a reference,89// so that the key data cannot be modified from outside9091// The key is zeroized by finalize()92// The reachability fence ensures finalize() isn't called early93byte[] result = key.clone();94Reference.reachabilityFence(this);95return result;96}9798public String getAlgorithm() {99return "DES";100}101102public String getFormat() {103return "RAW";104}105106/**107* Calculates a hash code value for the object.108* Objects that are equal will also have the same hashcode.109*/110public int hashCode() {111int retval = 0;112for (int i = 1; i < this.key.length; i++) {113retval += this.key[i] * i;114}115return(retval ^= "des".hashCode());116}117118public boolean equals(Object obj) {119if (this == obj)120return true;121122if (!(obj instanceof SecretKey))123return false;124125String thatAlg = ((SecretKey)obj).getAlgorithm();126if (!(thatAlg.equalsIgnoreCase("DES")))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 DES 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 DES 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