1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.crypto.hpke; 18 19 import java.security.spec.EncodedKeySpec; 20 import java.util.Arrays; 21 import java.util.Objects; 22 import libcore.util.NonNull; 23 24 /** 25 * External Diffie–Hellman (XDH) key spec holding either a public or private key. 26 * <p> 27 * Subclasses {@code EncodedKeySpec} using the non-Standard "raw" format. The XdhKeyFactory 28 * class utilises this in order to create XDH keys from raw bytes and to return them 29 * as an XdhKeySpec allowing the raw key material to be extracted from an XDH key. 30 */ 31 public final class XdhKeySpec extends EncodedKeySpec { 32 /** 33 * Creates an instance of {@link XdhKeySpec} by passing a public or private key in its raw 34 * format. 35 */ XdhKeySpec(@onNull byte[] encoded)36 public XdhKeySpec(@NonNull byte[] encoded) { 37 super(encoded); 38 } 39 40 @Override getFormat()41 @NonNull public String getFormat() { 42 return "raw"; 43 } 44 45 /** 46 * Returns the public or private key in its raw format. 47 * 48 * @return key in its raw format. 49 */ getKey()50 @NonNull public byte[] getKey() { 51 return getEncoded(); 52 } 53 54 @Override equals(Object o)55 public boolean equals(Object o) { 56 if (this == o) return true; 57 if (!(o instanceof EncodedKeySpec)) return false; 58 EncodedKeySpec that = (EncodedKeySpec) o; 59 return (getFormat().equals(that.getFormat()) 60 && (Arrays.equals(getEncoded(), that.getEncoded()))); 61 } 62 63 @Override hashCode()64 public int hashCode() { 65 return Objects.hash(getFormat(), Arrays.hashCode(getEncoded())); 66 } 67 } 68