Java


JAVA.CRYPTO.CAWH : Cryptographic Algorithm with Weak Hash (Java)

Summary

A cryptographic algorithm that contains a possibly reversible hashing algorithm.

Some cryptography algorithms are nowadays considered as outdated and unsafe or actually broken, since they are too easy to decode. This checker identifies uses of unsafe algorithms for cryptography.

The OWASP documentation provides more information on Using the Java Cryptographic Extensions.

Properties

Class Name Cryptographic Algorithm with Weak Hash (Java)
Significance security
Mnemonic JAVA.CRYPTO.CAWH
Categories
CWE CWE:328 Use of Weak Hash
CERT-Java CERT-Java:DRD18 Do not use the default behavior in a cryptographic library if it does not use recommended practices
OWASP-2017 OWASP-2017:A2 Broken authentication
OWASP-2021 OWASP-2021:A7 Identification and authorization failures
Availability Available for Java only.
Enabling Checks for this warning class are enabled by default. To disable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += discard class="Cryptographic Algorithm with Weak Hash (Java)"

Example

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.Signature;
import java.security.SignatureException;
import java.util.Arrays;

public class Signature {
    
  public static String getSignature(String text) { 

      String ret = null;

      try { 
        KeyPair keyPair = getKeyPair(); 
        byte[] data = text.getBytes("UTF8"); 
        Signature signature = Signature.getInstance("SHA1withRSA"); // "Cryptographic Algorithm with Weak Hash (Java)" warning issued here
        signature.initSign(keyPair.getPrivate()); 
        signature.update(data); 
        byte[] bytes = signature.sign(); 
        ret = Arrays.toString(bytes); 
      } catch (UnsupportedEncodingException | NoSuchAlgorithmException
               | InvalidKeyException | SignatureException e) { 
        System.out.println("Unable to compute the signature: " + e); 
      }
      return ret;
  } 

  private static KeyPair getKeyPair() throws NoSuchAlgorithmException 
      { 
      KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA"); 
      generator.initialize(1024); 
      return generator.genKeyPair(); 
  } 
}

In this example, the analysis triggers a Cryptographic Algorithm with Weak Hash (Java) warning because RSA (a safe cryptographic algorithm) is used in combination with SHA-1 (an unsafe hashing algorithm) to generate a signature. The OWASP documentation provides more information on Testing for Weak Encryption.

To resolve this issue, change the hashing algorithm to a more secure one. For example, replace string "SHA1withRSA" with "SHA256withRSA" in the call to Signature.getInstance().

Resolution

Use a safer cryptography algorithm.

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.