All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.commons.crypto.cipher.OpenSsl Maven / Gradle / Ivy

Go to download

Apache Commons Crypto is a cryptographic library optimized with AES-NI (Advanced Encryption Standard New Instructions). It provides Java API for both cipher level and Java stream level. Developers can use it to implement high performance AES encryption/decryption with the minimum code and effort. Please note that Crypto doesn't implement the cryptographic algorithm such as AES directly. It wraps to Openssl or JCE which implement the algorithms. Features -------- 1. Cipher API for low level cryptographic operations. 2. Java stream API (CryptoInputStream/CryptoOutputStream) for high level stream encyrption/decryption. 3. Both optimized with high performance AES encryption/decryption. (1400 MB/s - 1700 MB/s throughput in modern Xeon processors). 4. JNI-based implementation to achieve comparable performance to the native C++ version based on OpenSsl. 5. Portable across various operating systems (currently only Linux/MacOSX/Windows); Apache Commons Crypto loads the library according to your machine environment (it checks system properties, `os.name` and `os.arch`). 6. Simple usage. Add the commons-crypto-(version).jar file to your classpath. Export restrictions ------------------- This distribution includes cryptographic software. The country in which you currently reside may have restrictions on the import, possession, use, and/or re-export to another country, of encryption software. BEFORE using any encryption software, please check your country's laws, regulations and policies concerning the import, possession, or use, and re-export of encryption software, to see if this is permitted. See <http://www.wassenaar.org/> for more information. The U.S. Government Department of Commerce, Bureau of Industry and Security (BIS), has classified this software as Export Commodity Control Number (ECCN) 5D002.C.1, which includes information security software using or performing cryptographic functions with asymmetric algorithms. The form and manner of this Apache Software Foundation distribution makes it eligible for export under the License Exception ENC Technology Software Unrestricted (TSU) exception (see the BIS Export Administration Regulations, Section 740.13) for both object code and source code. The following provides more details on the included cryptographic software: * Commons Crypto use [Java Cryptography Extension](http://docs.oracle.com/javase/8/docs/technotes/guides/security/crypto/CryptoSpec.html) provided by Java * Commons Crypto link to and use [OpenSSL](https://www.openssl.org/) ciphers

There is a newer version: 1.2.0
Show newest version
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.commons.crypto.cipher;

import java.nio.ByteBuffer;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
import java.util.StringTokenizer;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.ShortBufferException;

import org.apache.commons.crypto.Crypto;
import org.apache.commons.crypto.utils.Utils;

/**
 * OpenSSL cryptographic wrapper using JNI. Currently only AES-CTR is supported.
 * It's flexible to add other crypto algorithms/modes.
 */
final class OpenSsl {

    // Mode constant defined by OpenSsl JNI
    public static final int ENCRYPT_MODE = 1;
    public static final int DECRYPT_MODE = 0;

    private final OpenSslFeedbackCipher opensslBlockCipher;

    /** Currently only support AES/CTR/NoPadding. */
    private enum AlgorithmMode {
        AES_CTR, AES_CBC, AES_GCM;

        /**
         * Gets the mode.
         *
         * @param algorithm the algorithm.
         * @param mode the mode.
         * @return the Algorithm mode.
         * @throws NoSuchAlgorithmException if the algorithm is not available.
         */
        static int get(final String algorithm, final String mode)
                throws NoSuchAlgorithmException {
            try {
                return AlgorithmMode.valueOf(algorithm + "_" + mode).ordinal();
            } catch (final Exception e) {
                throw new NoSuchAlgorithmException(
                        "Doesn't support algorithm: " + algorithm
                                + " and mode: " + mode);
            }
        }
    }

    private enum Padding {
        NoPadding, PKCS5Padding;

        /**
         * Gets the Padding instance.
         *
         * @param padding the padding.
         * @return the value of Padding.
         * @throws NoSuchPaddingException if the padding is not available.
         */
        static int get(final String padding) throws NoSuchPaddingException {
            try {
                return Padding.valueOf(padding).ordinal();
            } catch (final Exception e) {
                throw new NoSuchPaddingException("Doesn't support padding: "
                        + padding);
            }
        }
    }

    private static final Throwable loadingFailureReason;

    static {
        Throwable loadingFailure = null;
        try {
            if (Crypto.isNativeCodeLoaded()) {
                OpenSslNative.initIDs();
            } else {
                loadingFailure = Crypto.getLoadingError();
            }
        } catch (final Exception t) {
            loadingFailure = t;
        } catch (final UnsatisfiedLinkError t) {
            loadingFailure = t;
        } finally {
            loadingFailureReason = loadingFailure;
        }
    }

    /**
     * Gets the failure reason when loading OpenSsl native.
     *
     * @return the failure reason; null if it was loaded and initialized successfully
     */
    public static Throwable getLoadingFailureReason() {
        return loadingFailureReason;
    }

    /**
     * Constructs a {@link OpenSsl} instance based on context, algorithm and padding.
     *
     * @param context the context.
     * @param algorithm the algorithm.
     * @param padding the padding.
     */
    private OpenSsl(final long context, final int algorithm, final int padding) {
        if (algorithm == AlgorithmMode.AES_GCM.ordinal()) {
            opensslBlockCipher = new OpenSslGaloisCounterMode(context, algorithm, padding);
        } else {
            opensslBlockCipher = new OpenSslCommonMode(context, algorithm, padding);
        }
    }

    /**
     * Return an {@code OpenSslCipher} object that implements the specified
     * transformation.
     *
     * @param transformation the name of the transformation, e.g.,
     *        AES/CTR/NoPadding.
     * @return OpenSslCipher an {@code OpenSslCipher} object
     * @throws NoSuchAlgorithmException if {@code transformation} is null,
     *         empty, in an invalid format, or if OpenSsl doesn't implement the
     *         specified algorithm.
     * @throws NoSuchPaddingException if {@code transformation} contains a
     *         padding scheme that is not available.
     * @throws IllegalStateException if native code cannot be initialized
     */
    public static OpenSsl getInstance(final String transformation)
            throws NoSuchAlgorithmException, NoSuchPaddingException {
        if (loadingFailureReason != null) {
            throw new IllegalStateException(loadingFailureReason);
        }
        final Transform transform = tokenizeTransformation(transformation);
        final int algorithmMode = AlgorithmMode.get(transform.algorithm,
                transform.mode);
        final int padding = Padding.get(transform.padding);
        final long context = OpenSslNative.initContext(algorithmMode, padding);
        return new OpenSsl(context, algorithmMode, padding);
    }

    /** Nested class for algorithm, mode and padding. */
    private static class Transform {
        final String algorithm;
        final String mode;
        final String padding;

        /**
         * Constructs a {@link Transform} based on the algorithm, mode and padding.
         *
         * @param algorithm the algorithm
         * @param mode the mode.
         * @param padding the padding.
         */
        public Transform(final String algorithm, final String mode, final String padding) {
            this.algorithm = algorithm;
            this.mode = mode;
            this.padding = padding;
        }
    }

    /**
     * Gets the tokens of transformation.
     *
     * @param transformation the transformation.
     * @return the {@link Transform} instance.
     * @throws NoSuchAlgorithmException if the transformation is null.
     */
    private static Transform tokenizeTransformation(final String transformation)
            throws NoSuchAlgorithmException {
        if (transformation == null) {
            throw new NoSuchAlgorithmException("No transformation given.");
        }

        /*
         * Array containing the components of a Cipher transformation: index 0:
         * algorithm (e.g., AES) index 1: mode (e.g., CTR) index 2: padding
         * (e.g., NoPadding)
         */
        final String[] parts = new String[3];
        int count = 0;
        final StringTokenizer parser = new StringTokenizer(transformation, "/");
        while (parser.hasMoreTokens() && count < 3) {
            parts[count++] = parser.nextToken().trim();
        }
        if (count != 3 || parser.hasMoreTokens()) {
            throw new NoSuchAlgorithmException(
                    "Invalid transformation format: " + transformation);
        }
        return new Transform(parts[0], parts[1], parts[2]);
    }

    /**
     * Initialize this cipher with a key and IV.
     *
     * @param mode {@link #ENCRYPT_MODE} or {@link #DECRYPT_MODE}
     * @param key crypto key
     * @param params the algorithm parameters
     * @throws InvalidAlgorithmParameterException if IV length is wrong
     */
    public void init(final int mode, final byte[] key, final AlgorithmParameterSpec params)
            throws InvalidAlgorithmParameterException {
        opensslBlockCipher.init(mode, key, params);
    }

    /**
     * 

* Continues a multiple-part encryption or decryption operation. The data is * encrypted or decrypted, depending on how this cipher was initialized. *

* *

* All {@code input.remaining()} bytes starting at * {@code input.position()} are processed. The result is stored in the * output buffer. *

* *

* Upon return, the input buffer's position will be equal to its limit; its * limit will not have changed. The output buffer's position will have * advanced by n, when n is the value returned by this method; the output * buffer's limit will not have changed. *

* * If {@code output.remaining()} bytes are insufficient to hold the * result, a {@code ShortBufferException} is thrown. * * @param input the input ByteBuffer * @param output the output ByteBuffer * @return int number of bytes stored in {@code output} * @throws ShortBufferException if there is insufficient space in the output * buffer */ public int update(final ByteBuffer input, final ByteBuffer output) throws ShortBufferException { Utils.checkArgument(input.isDirect() && output.isDirect(), "Direct buffers are required."); return opensslBlockCipher.update(input, output); } /** * Continues a multiple-part encryption/decryption operation. The data is * encrypted or decrypted, depending on how this cipher was initialized. * * @param input the input byte array * @param inputOffset the offset in input where the input starts * @param inputLen the input length * @param output the byte array for the result * @param outputOffset the offset in output where the result is stored * @return the number of bytes stored in output * @throws ShortBufferException if there is insufficient space in the output * byte array */ public int update(final byte[] input, final int inputOffset, final int inputLen, final byte[] output, final int outputOffset) throws ShortBufferException { return opensslBlockCipher.update(input, inputOffset, inputLen, output, outputOffset); } /** * Encrypts or decrypts data in a single-part operation, or finishes a * multiple-part operation. * * @param input the input byte array * @param inputOffset the offset in input where the input starts * @param inputLen the input length * @param output the byte array for the result * @param outputOffset the offset in output where the result is stored * @return the number of bytes stored in output * @throws ShortBufferException if the given output byte array is too small * to hold the result * @throws BadPaddingException if this cipher is in decryption mode, and * (un)padding has been requested, but the decrypted data is not * bounded by the appropriate padding bytes * @throws IllegalBlockSizeException if this cipher is a block cipher, no * padding has been requested (only in encryption mode), and the * total input length of the data processed by this cipher is not a * multiple of block size; or if this encryption algorithm is unable * to process the input data provided. */ public int doFinal(final byte[] input, final int inputOffset, final int inputLen, final byte[] output, final int outputOffset) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException{ return opensslBlockCipher.doFinal(input, inputOffset, inputLen, output, outputOffset); } /** *

* Finishes a multiple-part operation. The data is encrypted or decrypted, * depending on how this cipher was initialized. *

* *

* The result is stored in the output buffer. Upon return, the output * buffer's position will have advanced by n, where n is the value returned * by this method; the output buffer's limit will not have changed. *

* *

* If {@code output.remaining()} bytes are insufficient to hold the * result, a {@code ShortBufferException} is thrown. *

* *

* Upon finishing, this method resets this cipher object to the state it was * in when previously initialized. That is, the object is available to * encrypt or decrypt more data. *

* * If any exception is thrown, this cipher object need to be reset before it * can be used again. * * @param input the input ByteBuffer * @param output the output ByteBuffer * @return int number of bytes stored in {@code output} * @throws ShortBufferException if the given output byte array is too small * to hold the result. * @throws IllegalBlockSizeException if this cipher is a block cipher, no * padding has been requested (only in encryption mode), and the * total input length of the data processed by this cipher is not a * multiple of block size; or if this encryption algorithm is unable * to process the input data provided. * @throws BadPaddingException if this cipher is in decryption mode, and * (un)padding has been requested, but the decrypted data is not * bounded by the appropriate padding bytes */ public int doFinal(final ByteBuffer input, final ByteBuffer output) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException { Utils.checkArgument(output.isDirect(), "Direct buffer is required."); return opensslBlockCipher.doFinal(input, output); } /** * Continues a multi-part update of the Additional Authentication * Data (AAD). *

* Calls to this method provide AAD to the cipher when operating in * modes such as AEAD (GCM). If this cipher is operating in * either GCM mode, all AAD must be supplied before beginning * operations on the ciphertext (via the {@code update} and * {@code doFinal} methods). * * @param aad the buffer containing the Additional Authentication Data * */ public void updateAAD(final byte[] aad) { this.opensslBlockCipher.updateAAD(aad); } /** Forcibly clean the context. */ public void clean() { if (opensslBlockCipher != null) { opensslBlockCipher.clean(); } } @Override protected void finalize() throws Throwable { clean(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy