|
Java example source code file (DHKeyAgreement.java)
The DHKeyAgreement.java Java example source code/* * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package com.sun.crypto.provider; import java.util.*; import java.lang.*; import java.math.BigInteger; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.Key; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.ProviderException; import java.security.spec.AlgorithmParameterSpec; import java.security.spec.InvalidKeySpecException; import javax.crypto.KeyAgreementSpi; import javax.crypto.ShortBufferException; import javax.crypto.SecretKey; import javax.crypto.spec.*; import sun.security.util.KeyUtil; /** * This class implements the Diffie-Hellman key agreement protocol between * any number of parties. * * @author Jan Luehe * */ public final class DHKeyAgreement extends KeyAgreementSpi { private boolean generateSecret = false; private BigInteger init_p = null; private BigInteger init_g = null; private BigInteger x = BigInteger.ZERO; // the private value private BigInteger y = BigInteger.ZERO; /** * Empty constructor */ public DHKeyAgreement() { } /** * Initializes this key agreement with the given key and source of * randomness. The given key is required to contain all the algorithm * parameters required for this key agreement. * * <p> If the key agreement algorithm requires random bytes, it gets them * from the given source of randomness, <code>random. * However, if the underlying * algorithm implementation does not require any random bytes, * <code>random is ignored. * * @param key the party's private information. For example, in the case * of the Diffie-Hellman key agreement, this would be the party's own * Diffie-Hellman private key. * @param random the source of randomness * * @exception InvalidKeyException if the given key is * inappropriate for this key agreement, e.g., is of the wrong type or * has an incompatible algorithm type. */ protected void engineInit(Key key, SecureRandom random) throws InvalidKeyException { try { engineInit(key, null, random); } catch (InvalidAlgorithmParameterException e) { // never happens, because we did not pass any parameters } } /** * Initializes this key agreement with the given key, set of * algorithm parameters, and source of randomness. * * @param key the party's private information. For example, in the case * of the Diffie-Hellman key agreement, this would be the party's own * Diffie-Hellman private key. * @param params the key agreement parameters * @param random the source of randomness * * @exception InvalidKeyException if the given key is * inappropriate for this key agreement, e.g., is of the wrong type or * has an incompatible algorithm type. * @exception InvalidAlgorithmParameterException if the given parameters * are inappropriate for this key agreement. */ protected void engineInit(Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException { // ignore "random" parameter, because our implementation does not // require any source of randomness generateSecret = false; init_p = null; init_g = null; if ((params != null) && !(params instanceof DHParameterSpec)) { throw new InvalidAlgorithmParameterException ("Diffie-Hellman parameters expected"); } if (!(key instanceof javax.crypto.interfaces.DHPrivateKey)) { throw new InvalidKeyException("Diffie-Hellman private key " + "expected"); } javax.crypto.interfaces.DHPrivateKey dhPrivKey; dhPrivKey = (javax.crypto.interfaces.DHPrivateKey)key; // check if private key parameters are compatible with // initialized ones if (params != null) { init_p = ((DHParameterSpec)params).getP(); init_g = ((DHParameterSpec)params).getG(); } BigInteger priv_p = dhPrivKey.getParams().getP(); BigInteger priv_g = dhPrivKey.getParams().getG(); if (init_p != null && priv_p != null && !(init_p.equals(priv_p))) { throw new InvalidKeyException("Incompatible parameters"); } if (init_g != null && priv_g != null && !(init_g.equals(priv_g))) { throw new InvalidKeyException("Incompatible parameters"); } if ((init_p == null && priv_p == null) || (init_g == null && priv_g == null)) { throw new InvalidKeyException("Missing parameters"); } init_p = priv_p; init_g = priv_g; // store the x value this.x = dhPrivKey.getX(); } /** * Executes the next phase of this key agreement with the given * key that was received from one of the other parties involved in this key * agreement. * * @param key the key for this phase. For example, in the case of * Diffie-Hellman between 2 parties, this would be the other party's * Diffie-Hellman public key. * @param lastPhase flag which indicates whether or not this is the last * phase of this key agreement. * * @return the (intermediate) key resulting from this phase, or null if * this phase does not yield a key * * @exception InvalidKeyException if the given key is inappropriate for * this phase. * @exception IllegalStateException if this key agreement has not been * initialized. */ protected Key engineDoPhase(Key key, boolean lastPhase) throws InvalidKeyException, IllegalStateException { if (!(key instanceof javax.crypto.interfaces.DHPublicKey)) { throw new InvalidKeyException("Diffie-Hellman public key " + "expected"); } javax.crypto.interfaces.DHPublicKey dhPubKey; dhPubKey = (javax.crypto.interfaces.DHPublicKey)key; if (init_p == null || init_g == null) { throw new IllegalStateException("Not initialized"); } // check if public key parameters are compatible with // initialized ones BigInteger pub_p = dhPubKey.getParams().getP(); BigInteger pub_g = dhPubKey.getParams().getG(); if (pub_p != null && !(init_p.equals(pub_p))) { throw new InvalidKeyException("Incompatible parameters"); } if (pub_g != null && !(init_g.equals(pub_g))) { throw new InvalidKeyException("Incompatible parameters"); } // validate the Diffie-Hellman public key KeyUtil.validate(dhPubKey); // store the y value this.y = dhPubKey.getY(); // we've received a public key (from one of the other parties), // so we are ready to create the secret, which may be an // intermediate secret, in which case we wrap it into a // Diffie-Hellman public key object and return it. generateSecret = true; if (lastPhase == false) { byte[] intermediate = engineGenerateSecret(); return new DHPublicKey(new BigInteger(1, intermediate), init_p, init_g); } else { return null; } } /** * Generates the shared secret and returns it in a new buffer. * * <p>This method resets this Other Java examples (source code examples)Here is a short list of links related to this Java DHKeyAgreement.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.