You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2022/12/12 16:32:41 UTC

[commons-crypto] branch master updated (05969be -> f02901d)

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-crypto.git


    from 05969be  Refactor duplicate code
     new 0040f46  Refactor duplicate code
     new f02901d  Javadoc

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../org/apache/commons/crypto/cipher/OpenSsl.java  |  3 +-
 .../commons/crypto/jna/OpenSsl20XNativeJna.java    | 38 +++++++++++-----------
 .../commons/crypto/jna/OpenSslJnaCipher.java       |  5 ++-
 .../commons/crypto/jna/OpenSslNativeJna.java       | 16 +++------
 .../org/apache/commons/crypto/utils/Padding.java   | 13 +++++---
 .../commons/crypto/utils/Transformation.java       | 34 ++++++++++++++-----
 6 files changed, 62 insertions(+), 47 deletions(-)


[commons-crypto] 01/02: Refactor duplicate code

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-crypto.git

commit 0040f46cdfd15831965a24c4347d22fbbc0dcd8e
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Dec 12 11:31:12 2022 -0500

    Refactor duplicate code
---
 .../org/apache/commons/crypto/cipher/OpenSsl.java  |  3 +-
 .../commons/crypto/jna/OpenSslJnaCipher.java       |  5 ++--
 .../commons/crypto/jna/OpenSslNativeJna.java       | 16 ++++------
 .../org/apache/commons/crypto/utils/Padding.java   | 13 ++++++---
 .../commons/crypto/utils/Transformation.java       | 34 +++++++++++++++++-----
 5 files changed, 43 insertions(+), 28 deletions(-)

diff --git a/src/main/java/org/apache/commons/crypto/cipher/OpenSsl.java b/src/main/java/org/apache/commons/crypto/cipher/OpenSsl.java
index 5c58c2d..c97f392 100644
--- a/src/main/java/org/apache/commons/crypto/cipher/OpenSsl.java
+++ b/src/main/java/org/apache/commons/crypto/cipher/OpenSsl.java
@@ -28,7 +28,6 @@ import javax.crypto.NoSuchPaddingException;
 import javax.crypto.ShortBufferException;
 
 import org.apache.commons.crypto.Crypto;
-import org.apache.commons.crypto.utils.Padding;
 import org.apache.commons.crypto.utils.Transformation;
 import org.apache.commons.crypto.utils.Utils;
 
@@ -127,7 +126,7 @@ final class OpenSsl {
         }
         final Transformation transform = Transformation.parse(transformation);
         final int algorithmMode = AlgorithmMode.get(transform.getAlgorithm(), transform.getMode());
-        final int padding = Padding.get(transform.getPadding());
+        final int padding = transform.getPadding().ordinal();
         final long context = OpenSslNative.initContext(algorithmMode, padding);
         return new OpenSsl(context, algorithmMode, padding);
     }
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 e1894ec..94b14b5 100644
--- a/src/main/java/org/apache/commons/crypto/jna/OpenSslJnaCipher.java
+++ b/src/main/java/org/apache/commons/crypto/jna/OpenSslJnaCipher.java
@@ -35,7 +35,6 @@ import javax.crypto.spec.IvParameterSpec;
 
 import org.apache.commons.crypto.cipher.CryptoCipher;
 import org.apache.commons.crypto.cipher.CryptoCipherFactory;
-import org.apache.commons.crypto.utils.Padding;
 import org.apache.commons.crypto.utils.Transformation;
 
 import com.sun.jna.NativeLong;
@@ -70,10 +69,10 @@ class OpenSslJnaCipher implements CryptoCipher {
         algorithmMode = AlgorithmMode.get(transform.getAlgorithm(), transform.getMode());
 
         if (algorithmMode != AlgorithmMode.AES_CBC && algorithmMode != AlgorithmMode.AES_CTR) {
-            throw new GeneralSecurityException("unknown algorithm " + transform.getAlgorithm() + "_" + transform.getMode());
+            throw new GeneralSecurityException("Unknown algorithm " + transform.getAlgorithm() + "_" + transform.getMode());
         }
 
-        padding = Padding.get(transform.getPadding());
+        padding = transform.getPadding().ordinal();
         context = OpenSslNativeJna.EVP_CIPHER_CTX_new();
 
     }
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 f5ab128..ea7522d 100644
--- a/src/main/java/org/apache/commons/crypto/jna/OpenSslNativeJna.java
+++ b/src/main/java/org/apache/commons/crypto/jna/OpenSslNativeJna.java
@@ -73,11 +73,7 @@ class OpenSslNativeJna {
 
         INIT_OK = JnaImplementation._INIT_OK();
 
-        if (INIT_OK) {
-            INIT_ERROR = null;
-        } else {
-            INIT_ERROR = JnaImplementation._INIT_ERROR();
-        }
+        INIT_ERROR = INIT_OK ? null : JnaImplementation._INIT_ERROR();
     }
 
     private OpenSslNativeJna() {
@@ -143,9 +139,8 @@ class OpenSslNativeJna {
         return JnaImplementation._EVP_CIPHER_CTX_new();
     }
 
-    // TODO: native method returns int
-    public static void EVP_CIPHER_CTX_set_padding(final PointerByReference context, final int padding) {
-        JnaImplementation._EVP_CIPHER_CTX_set_padding(context, padding);
+    public static int EVP_CIPHER_CTX_set_padding(final PointerByReference context, final int padding) {
+        return JnaImplementation._EVP_CIPHER_CTX_set_padding(context, padding);
     }
 
     public static int EVP_CipherFinal_ex(final PointerByReference context, final ByteBuffer outBuffer,
@@ -184,9 +179,8 @@ class OpenSslNativeJna {
         JnaImplementation._ENGINE_load_rdrand();
     }
 
-    // TODO: native method returns int
-    public static void ENGINE_cleanup() {
-        JnaImplementation._ENGINE_cleanup();
+    public static int ENGINE_cleanup() {
+        return JnaImplementation._ENGINE_cleanup();
     }
 
     public static void EVP_CIPHER_CTX_cleanup(final PointerByReference context) {
diff --git a/src/main/java/org/apache/commons/crypto/utils/Padding.java b/src/main/java/org/apache/commons/crypto/utils/Padding.java
index 445f19a..59d82e4 100644
--- a/src/main/java/org/apache/commons/crypto/utils/Padding.java
+++ b/src/main/java/org/apache/commons/crypto/utils/Padding.java
@@ -25,20 +25,25 @@ import javax.crypto.NoSuchPaddingException;
  */
 public enum Padding {
 
-    NoPadding, PKCS5Padding;
+    /** Don't change the order of this enum value. */
+    NoPadding,
+
+    /** Don't change the order of this enum value. */
+    PKCS5Padding;
 
     /**
      * Gets a Padding.
      *
      * @param padding the padding name.
      * @return a Padding instance.
-     * @throws NoSuchPaddingException if the algorithm is not support
+     * @throws NoSuchPaddingException if the algorithm is not supported.
      */
-    public static int get(final String padding) throws NoSuchPaddingException {
+    public static Padding get(final String padding) throws NoSuchPaddingException {
         try {
-            return Padding.valueOf(padding).ordinal();
+            return Padding.valueOf(padding);
         } catch (final Exception e) {
             throw new NoSuchPaddingException("Algorithm not supported: " + padding);
         }
     }
+
 }
\ No newline at end of file
diff --git a/src/main/java/org/apache/commons/crypto/utils/Transformation.java b/src/main/java/org/apache/commons/crypto/utils/Transformation.java
index acef502..53a61b9 100644
--- a/src/main/java/org/apache/commons/crypto/utils/Transformation.java
+++ b/src/main/java/org/apache/commons/crypto/utils/Transformation.java
@@ -20,6 +20,8 @@ package org.apache.commons.crypto.utils;
 
 import java.security.NoSuchAlgorithmException;
 
+import javax.crypto.NoSuchPaddingException;
+
 /**
  * Transformation algorithm, mode and padding, in the format "Algorithm/Mode/Padding", for example "AES/CBC/NoPadding".
  *
@@ -27,14 +29,18 @@ import java.security.NoSuchAlgorithmException;
  */
 public class Transformation {
 
+    private static final int T_DELIM_PARTS = 3;
+    private static final String T_DELIM_REGEX = "/";
+
     /**
      * Parses a transformation.
      *
      * @param transformation current transformation
      * @return the Transformation
      * @throws NoSuchAlgorithmException if the algorithm is not supported
+     * @throws NoSuchPaddingException Thrown when the padding is unsupported.
      */
-    public static Transformation parse(final String transformation) throws NoSuchAlgorithmException {
+    public static Transformation parse(final String transformation) throws NoSuchAlgorithmException, NoSuchPaddingException {
         if (transformation == null) {
             throw new NoSuchAlgorithmException("No transformation given.");
         }
@@ -44,16 +50,16 @@ public class Transformation {
         // algorithm (e.g., AES) index 1: mode (e.g., CTR) index 2: padding (e.g.,
         // NoPadding)
         //
-        final String[] parts = transformation.split("/", 4);
-        if (parts.length != 3) {
+        final String[] parts = transformation.split(T_DELIM_REGEX, T_DELIM_PARTS + 1);
+        if (parts.length != T_DELIM_PARTS) {
             throw new NoSuchAlgorithmException("Invalid transformation format: " + transformation);
         }
         return new Transformation(parts[0], parts[1], parts[2]);
     }
 
-    final String algorithm;
-    final String mode;
-    final String padding;
+    private final String algorithm;
+    private final String mode;
+    private final Padding padding;
 
     /**
      * Constructs a new instance.
@@ -62,12 +68,24 @@ public class Transformation {
      * @param mode the mode name
      * @param padding the padding name
      */
-    private Transformation(final String algorithm, final String mode, final String padding) {
+    private Transformation(final String algorithm, final String mode, final Padding padding) {
         this.algorithm = algorithm;
         this.mode = mode;
         this.padding = padding;
     }
 
+    /**
+     * Constructs a new instance.
+     *
+     * @param algorithm the algorithm name
+     * @param mode the mode name
+     * @param padding the padding name
+     * @throws NoSuchPaddingException Thrown when the padding is unsupported.
+     */
+    private Transformation(final String algorithm, final String mode, final String padding) throws NoSuchPaddingException {
+        this(algorithm, mode, Padding.get(padding));
+    }
+
     /**
      * Gets the algorithm.
      * 
@@ -91,7 +109,7 @@ public class Transformation {
      * 
      * @return the padding.
      */
-    public String getPadding() {
+    public Padding getPadding() {
         return padding;
     }
 }
\ No newline at end of file


[commons-crypto] 02/02: Javadoc

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-crypto.git

commit f02901da596261dc4d078dd2f6cfff183f4da25a
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Dec 12 11:32:37 2022 -0500

    Javadoc
---
 .../commons/crypto/jna/OpenSsl20XNativeJna.java    | 38 +++++++++++-----------
 1 file changed, 19 insertions(+), 19 deletions(-)

diff --git a/src/main/java/org/apache/commons/crypto/jna/OpenSsl20XNativeJna.java b/src/main/java/org/apache/commons/crypto/jna/OpenSsl20XNativeJna.java
index ae1cb49..ecc1a2a 100644
--- a/src/main/java/org/apache/commons/crypto/jna/OpenSsl20XNativeJna.java
+++ b/src/main/java/org/apache/commons/crypto/jna/OpenSsl20XNativeJna.java
@@ -101,7 +101,7 @@ class OpenSsl20XNativeJna implements OpenSslInterfaceNativeJna {
 
     // TODO: NOT USED?
     /**
-     * EVP_CIPHER_CTX_init() remains as an alias for EVP_CIPHER_CTX_reset
+     * EVP_CIPHER_CTX_init() remains as an alias for EVP_CIPHER_CTX_reset.
      *
      * @param p
      *            cipher context
@@ -109,43 +109,43 @@ class OpenSsl20XNativeJna implements OpenSslInterfaceNativeJna {
     public static native void EVP_CIPHER_CTX_init(PointerByReference p);
 
     /**
-     * Enables or disables padding
+     * Enables or disables padding.
      *
      * @param c
-     *            cipher context
+     *            cipher context.
      * @param pad
-     *            If the pad parameter is zero then no padding is performed
+     *            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
+     * @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
+     * @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
+     * @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
+     * @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
+     * @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
+     * @return an OpenSSL AES EVP cipher instance with a 256-bit key CTR mode.
      */
     public static native PointerByReference EVP_aes_256_ctr();
 
@@ -221,26 +221,26 @@ class OpenSsl20XNativeJna implements OpenSslInterfaceNativeJna {
 
     // Random generator
     /**
-     * OpenSSL uses for random number generation
+     * OpenSSL uses for random number generation.
      *
-     * @return pointers to the respective methods
+     * @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
+     * @return pointers to the respective methods.
      */
     public static native PointerByReference RAND_SSLeay();
 
     /**
-     * Generates random data
+     * Generates random data.
      *
      * @param buf
      *            the bytes for generated random.
      * @param num
-     *            buffer length
+     *            buffer length.
      * @return 1 on success, 0 otherwise.
      */
     public static native int RAND_bytes(ByteBuffer buf, int num);
@@ -286,18 +286,18 @@ class OpenSsl20XNativeJna implements OpenSslInterfaceNativeJna {
      * Sets the engine as the default for random number generation.
      *
      * @param e
-     *            engine reference
+     *            engine reference.
      * @param flags
-     *            ENGINE_METHOD_RAND
+     *            ENGINE_METHOD_RAND.
      * @return zero if failed.
      */
     public static native int ENGINE_set_default(PointerByReference e, int flags);
 
     /**
-     * Gets engine by id
+     * Gets engine by id.
      *
      * @param id
-     *            engine id
+     *            engine id.
      * @return engine instance
      */
     public static native PointerByReference ENGINE_by_id(String id);