CRYPTO-119: Fix checkstyle issues
Project: http://git-wip-us.apache.org/repos/asf/commons-crypto/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-crypto/commit/ed1b6097 Tree: http://git-wip-us.apache.org/repos/asf/commons-crypto/tree/ed1b6097 Diff: http://git-wip-us.apache.org/repos/asf/commons-crypto/diff/ed1b6097 Branch: refs/heads/CRYPTO-1.0.0 Commit: ed1b6097c6cfe79dc4e23ac4e9c75fa4b3ccbb04 Parents: 5a3bf99 Author: Sun Dapeng <s...@apache.org> Authored: Fri Jul 22 14:40:55 2016 +0800 Committer: Sun Dapeng <s...@apache.org> Committed: Fri Jul 22 14:40:55 2016 +0800 ---------------------------------------------------------------------- .../java/org/apache/commons/crypto/Crypto.java | 10 + .../commons/crypto/OpenSslInfoNative.java | 22 ++- .../crypto/cipher/CryptoCipherFactory.java | 4 + .../apache/commons/crypto/jna/OpenSslJna.java | 12 ++ .../commons/crypto/jna/OpenSslJnaCipher.java | 40 +++- .../crypto/jna/OpenSslJnaCryptoRandom.java | 10 +- .../commons/crypto/jna/OpenSslNativeJna.java | 195 ++++++++++++++++++- .../crypto/random/CryptoRandomFactory.java | 4 + 8 files changed, 281 insertions(+), 16 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/ed1b6097/src/main/java/org/apache/commons/crypto/Crypto.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/crypto/Crypto.java b/src/main/java/org/apache/commons/crypto/Crypto.java index d72fa4e..48069f4 100644 --- a/src/main/java/org/apache/commons/crypto/Crypto.java +++ b/src/main/java/org/apache/commons/crypto/Crypto.java @@ -56,6 +56,11 @@ public final class Crypto { private static class ComponentPropertiesHolder { static final Properties PROPERTIES = getComponentProperties(); + /** + * Get component properties from component.properties. + * + * @return Properties contains project version. + */ private static Properties getComponentProperties() { URL url = Crypto.class.getResource("/org/apache/commons/crypto/component.properties"); if (url != null) { @@ -116,6 +121,11 @@ public final class Crypto { return ComponentPropertiesHolder.PROPERTIES.getProperty("NAME"); } + /** + * The Main of Crypto + * @param args don't use the args + * @throws Exception if getCryptoRandom or getCryptoCipher get error. + */ public static void main(String args[]) throws Exception { System.out.println(getComponentName() + " " + getComponentVersion()); if (isNativeCodeLoaded()) { http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/ed1b6097/src/main/java/org/apache/commons/crypto/OpenSslInfoNative.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/crypto/OpenSslInfoNative.java b/src/main/java/org/apache/commons/crypto/OpenSslInfoNative.java index 3aa394f..8ca2fde 100644 --- a/src/main/java/org/apache/commons/crypto/OpenSslInfoNative.java +++ b/src/main/java/org/apache/commons/crypto/OpenSslInfoNative.java @@ -18,7 +18,7 @@ package org.apache.commons.crypto; /** - * JNI interface of {@link CryptoRandom} implementation for OpenSSL. + * JNI interface of {@see CryptoRandom} implementation for OpenSSL. * The native method in this class is defined in * OpenSslCryptoRandomNative.h (generated at build time by javah) * and implemented in the file @@ -26,16 +26,36 @@ package org.apache.commons.crypto; */ class OpenSslInfoNative { + /** + * Makes the constructor private. + */ private OpenSslInfoNative() { } + /** + * @return version of native + */ public static native String NativeVersion(); + /** + * @return name of native + */ public static native String NativeName(); + /** + * @return timestamp of native + */ public static native String NativeTimeStamp(); + /** + * @return the value of SSLEAY_VERSION_NUMBER. + */ public static native long SSLeay(); + /** + * Returns SSLeay_version according the version type. + * @param type The version type + * @return The text variant of the version number and the release date. + */ public static native String SSLeayVersion(int type); } http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/ed1b6097/src/main/java/org/apache/commons/crypto/cipher/CryptoCipherFactory.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/crypto/cipher/CryptoCipherFactory.java b/src/main/java/org/apache/commons/crypto/cipher/CryptoCipherFactory.java index 2fa6346..7de06ce 100644 --- a/src/main/java/org/apache/commons/crypto/cipher/CryptoCipherFactory.java +++ b/src/main/java/org/apache/commons/crypto/cipher/CryptoCipherFactory.java @@ -85,6 +85,10 @@ public class CryptoCipherFactory { private final String className; + /** + * The private constructor. + * @param klass the Class of CryptoCipher + */ private CipherProvider(Class<? extends CryptoCipher> klass) { this.klass = klass; this.className = klass.getName(); http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/ed1b6097/src/main/java/org/apache/commons/crypto/jna/OpenSslJna.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/crypto/jna/OpenSslJna.java b/src/main/java/org/apache/commons/crypto/jna/OpenSslJna.java index b5c90a1..14b9d34 100644 --- a/src/main/java/org/apache/commons/crypto/jna/OpenSslJna.java +++ b/src/main/java/org/apache/commons/crypto/jna/OpenSslJna.java @@ -25,18 +25,30 @@ import org.apache.commons.crypto.random.CryptoRandom; */ public final class OpenSslJna { + /** + * @return The cipher class of JNA implementation + */ public static Class<? extends CryptoCipher> getCipherClass() { return OpenSslJnaCipher.class; } + /** + * @return The random class of JNA implementation + */ public static Class<? extends CryptoRandom> getRandomClass() { return OpenSslJnaCryptoRandom.class; } + /** + * @return true if JNA native loads successfully + */ public static boolean isEnabled() { return OpenSslNativeJna.INIT_OK; } + /** + * @return the error of JNA + */ public static Throwable initialisationError() { return OpenSslNativeJna.INIT_ERROR; } http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/ed1b6097/src/main/java/org/apache/commons/crypto/jna/OpenSslJnaCipher.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/crypto/jna/OpenSslJnaCipher.java b/src/main/java/org/apache/commons/crypto/jna/OpenSslJnaCipher.java index e9a0588..c83f150 100644 --- a/src/main/java/org/apache/commons/crypto/jna/OpenSslJnaCipher.java +++ b/src/main/java/org/apache/commons/crypto/jna/OpenSslJnaCipher.java @@ -245,8 +245,11 @@ class OpenSslJnaCipher implements CryptoCipher { // OpenSslNativeJna.EVP_CIPHER_CTX_free(context); } } - - private void throwOnError(int retVal) { + + /** + * @param retVal the result value of error. + */ + private void throwOnError(int retVal) { if (retVal != 1) { NativeLong err = OpenSslNativeJna.ERR_peek_error(); String errdesc = OpenSslNativeJna.ERR_error_string(err, null); @@ -265,6 +268,12 @@ class OpenSslJnaCipher implements CryptoCipher { final String mode; final String padding; + /** + * Constructor of Transform. + * @param algorithm the algorithm name + * @param mode the mode name + * @param padding the padding name + */ public Transform(String algorithm, String mode, String padding) { this.algorithm = algorithm; this.mode = mode; @@ -272,6 +281,12 @@ class OpenSslJnaCipher implements CryptoCipher { } } + /** + * Tokenize the transformation. + * @param transformation current transformation + * @return the Transform + * @throws NoSuchAlgorithmException if the algorithm is not supported + */ private static Transform tokenizeTransformation(String transformation) throws NoSuchAlgorithmException { if (transformation == null) { @@ -296,10 +311,19 @@ class OpenSslJnaCipher implements CryptoCipher { return new Transform(parts[0], parts[1], parts[2]); } - /** Currently only support AES/CTR/NoPadding. */ + /** + * AlgorithmMode of JNA. Currently only support AES/CTR/NoPadding. + */ private static enum AlgorithmMode { AES_CTR, AES_CBC; + /** + * Gets the AlgorithmMode instance. + * @param algorithm the algorithm name + * @param mode the mode name + * @return the AlgorithmMode instance + * @throws NoSuchAlgorithmException if the algorithm is not support + */ static AlgorithmMode get(String algorithm, String mode) throws NoSuchAlgorithmException { try { return AlgorithmMode.valueOf(algorithm + "_" + mode); @@ -309,9 +333,19 @@ class OpenSslJnaCipher implements CryptoCipher { } } + /** + * Padding of JNA. + */ private static enum Padding { NoPadding, PKCS5Padding; + /** + * Gets the Padding instance. + * + * @param padding the padding name + * @return the AlgorithmMode instance + * @throws NoSuchPaddingException if the algorithm is not support + */ static int get(String padding) throws NoSuchPaddingException { try { return Padding.valueOf(padding).ordinal(); http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/ed1b6097/src/main/java/org/apache/commons/crypto/jna/OpenSslJnaCryptoRandom.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/crypto/jna/OpenSslJnaCryptoRandom.java b/src/main/java/org/apache/commons/crypto/jna/OpenSslJnaCryptoRandom.java index c9c0419..b3f4c4f 100644 --- a/src/main/java/org/apache/commons/crypto/jna/OpenSslJnaCryptoRandom.java +++ b/src/main/java/org/apache/commons/crypto/jna/OpenSslJnaCryptoRandom.java @@ -164,7 +164,10 @@ class OpenSslJnaCryptoRandom extends Random implements CryptoRandom { //OpenSslNativeJna.CRYPTO_set_locking_callback(null); //LOCK.unlock(); } - + + /** + * Closes the rdrand engine. + */ private void closeRdrandEngine() { if(rdrandEngine != null) { @@ -181,7 +184,10 @@ class OpenSslJnaCryptoRandom extends Random implements CryptoRandom { public boolean isRdrandEnabled() { return rdrandEnabled; } - + + /** + * @param retVal the result value of error. + */ private void throwOnError(int retVal) { if (retVal != 1) { NativeLong err = OpenSslNativeJna.ERR_peek_error(); http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/ed1b6097/src/main/java/org/apache/commons/crypto/jna/OpenSslNativeJna.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/crypto/jna/OpenSslNativeJna.java b/src/main/java/org/apache/commons/crypto/jna/OpenSslNativeJna.java index f75566e..099c049 100644 --- a/src/main/java/org/apache/commons/crypto/jna/OpenSslNativeJna.java +++ b/src/main/java/org/apache/commons/crypto/jna/OpenSslNativeJna.java @@ -32,7 +32,7 @@ class OpenSslNativeJna { static final int OOSL_JNA_DECRYPT_MODE = 0; static final boolean INIT_OK; - + static final Throwable INIT_ERROR; static { @@ -53,54 +53,229 @@ class OpenSslNativeJna { } //misc + /** + * @return OPENSSL_VERSION_NUMBER which is a numeric release version + * * identifier + */ public static native NativeLong SSLeay(); + + /** + * Retrieves version/build information about OpenSSL library. + * + * @param type type can be SSLEAY_VERSION, SSLEAY_CFLAGS, SSLEAY_BUILT_ON... + * @return A pointer to a constant string describing the version of the + * OpenSSL library or giving information about the library build. + */ public static native String SSLeay_version(int type); + + /** + * Registers the error strings for all libcrypto functions. + */ public static native void ERR_load_crypto_strings(); + + /** + * @return the earliest error code from the thread's error queue without + * modifying it. + */ public static native NativeLong ERR_peek_error(); + + + + /** + * Generates a human-readable string representing the error code e. + * @see <a>https://www.openssl.org/docs/manmaster/crypto/ERR_error_string.html</a> + * + * @param err the error code + * @param null_ buf is NULL, the error string is placed in a static buffer + * @return the human-readable error messages. + */ public static native String ERR_error_string(NativeLong err, char[] null_); //String ERR_lib_error_string(NativeLong err); //String ERR_func_error_string(NativeLong err); - //String ERR_reason_error_string(NativeLong err); - + //en-/decryption + /** + * Creates a cipher context. + * + * @return a pointer to a newly created EVP_CIPHER_CTX for success and + * NULL for failure. + */ public static native PointerByReference EVP_CIPHER_CTX_new(); + + + /** + * EVP_CIPHER_CTX_init() remains as an alias for EVP_CIPHER_CTX_reset + * @param p cipher context + */ public static native void EVP_CIPHER_CTX_init(PointerByReference p); + + /** + * Enables or disables padding + * @param c cipher context + * @param pad If the pad parameter is zero then no padding is performed + * @return always returns 1 + */ public static native int EVP_CIPHER_CTX_set_padding(PointerByReference c, int pad); + + /** + * @return an openssl AES evp cipher instance with a 128-bit key CBC mode + */ public static native PointerByReference EVP_aes_128_cbc(); + + /** + * @return an openssl AES evp cipher instance with a 128-bit key CTR mode + */ public static native PointerByReference EVP_aes_128_ctr(); + + /** + * @return an openssl AES evp cipher instance with a 192-bit key CBC mode + */ public static native PointerByReference EVP_aes_192_cbc(); + + /** + * @return an openssl AES evp cipher instance with a 192-bit key CTR mode + */ public static native PointerByReference EVP_aes_192_ctr(); + + /** + * @return an openssl AES evp cipher instance with a 256-bit key CBC mode + */ public static native PointerByReference EVP_aes_256_cbc(); + + /** + * @return an openssl AES evp cipher instance with a 256-bit key CTR mode + */ public static native PointerByReference EVP_aes_256_ctr(); + + /** + * Init a cipher. + * @param ctx cipher context + * @param cipher evp cipher instance + * @param impl engine + * @param key key + * @param iv iv + * @param enc 1 for encryption, 0 for decryption + * @return 1 for success and 0 for failure. + */ public static native int EVP_CipherInit_ex(PointerByReference ctx, PointerByReference cipher, PointerByReference impl, byte key[], byte iv[], int enc); + + + /** + * Continues a multiple-part encryption/decryption operation. + * + * @param ctx cipher context + * @param bout output byte buffer + * @param outl output length + * @param in input byte buffer + * @param inl input length + * @return 1 for success and 0 for failure. + */ public static native int EVP_CipherUpdate(PointerByReference ctx, ByteBuffer bout, int[] outl, ByteBuffer in, int inl); - public static native int EVP_CipherFinal_ex(PointerByReference ctx, ByteBuffer bout, int[] outl); + + /** + * Finishes a multiple-part operation. + * + * @param ctx cipher context + * @param bout output byte buffer + * @param outl output length + * @return 1 for success and 0 for failure. + */ + public static native int EVP_CipherFinal_ex(PointerByReference ctx, ByteBuffer bout, int[] outl); + + /** + * Clears all information from a cipher context and free up any allocated + * memory associate with it, including ctx itself. + * @param c openssl evp cipher + */ public static native void EVP_CIPHER_CTX_free(PointerByReference c); + + /** + * Clears all information from a cipher context and free up any allocated + * * memory associate with it. + * @param c openssl evp cipher + */ public static native void EVP_CIPHER_CTX_cleanup(PointerByReference c); - + //Random generator + /** + * OpenSSL uses for random number generation + * @return pointers to the respective methods + */ public static native PointerByReference RAND_get_rand_method(); + + /** + * OpenSSL uses for random number generation. + * @return pointers to the respective methods + */ public static native PointerByReference RAND_SSLeay(); + + /** + * Generates random data + * @param buf the bytes for generated random. + * @param num buffer length + * @return 1 on success, 0 otherwise. + */ public static native int RAND_bytes(ByteBuffer buf, int num); + + /** + * Releases all functional references. + * + * @param e engine reference. + * @return 0 on success, 1 otherwise. + */ public static native int ENGINE_finish(PointerByReference e); + + /** + * Frees the structural reference + * @param e engine reference. + * @return 0 on success, 1 otherwise. + */ public static native int ENGINE_free(PointerByReference e); + + /** + * Cleanups before program exit, it will avoid memory leaks. + * @return 0 on success, 1 otherwise. + */ public static native int ENGINE_cleanup(); + + /** + * Obtains a functional reference from an existing structural reference. + * @param e engine reference + * @return zero if the ENGINE was not already operational and couldn't be successfully initialised + */ public static native int ENGINE_init(PointerByReference e); + + /** + * Sets the engine as the default for random number generation. + * @param e engine reference + * @param flags ENGINE_METHOD_RAND + * @return zero if failed. + */ public static native int ENGINE_set_default(PointerByReference e, int flags); + + /** + * Gets engine by id + * @param id engine id + * @return engine instance + */ public static native PointerByReference ENGINE_by_id(String id); + + /** + * Initializes the engine. + */ public static native void ENGINE_load_rdrand(); - + //TODO callback multithreading /*public interface Id_function_cb extends Callback { long invoke (); } - + public interface Locking_function_cb extends Callback { void invoke(int mode, int n, String file, int line); } - + public static final Id_function_cb default_id_function = new Id_function_cb() { - + @Override public long invoke() { //id always positive @@ -108,7 +283,7 @@ class OpenSslNativeJna { return id; } }; - + int CRYPTO_num_locks(); void CRYPTO_set_id_callback(Id_function_cb id_function); void CRYPTO_set_locking_callback(Locking_function_cb locking_function);*/ http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/ed1b6097/src/main/java/org/apache/commons/crypto/random/CryptoRandomFactory.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/crypto/random/CryptoRandomFactory.java b/src/main/java/org/apache/commons/crypto/random/CryptoRandomFactory.java index 5416cc7..2ef50ba 100644 --- a/src/main/java/org/apache/commons/crypto/random/CryptoRandomFactory.java +++ b/src/main/java/org/apache/commons/crypto/random/CryptoRandomFactory.java @@ -116,6 +116,10 @@ public class CryptoRandomFactory { private final String className; + /** + * The private constructor. + * @param klass the Class of CryptoRandom + */ private RandomProvider(Class<? extends CryptoRandom> klass) { this.klass = klass; this.className = klass.getName();