github / github/codeql

Java Code Scanning and Semmle LGTM Query Suites Allows for Weak Crypto with BlowFish Key size < 128

Đang mở
#4,851 1 bình luận 0 reaction 0 người được giao Xem trên GitHub
question
Ngôn ngữ chính
CodeQL
Star
10.1k
Fork
2.1k
Merge trung bình
2 ngày 15 giờ
Pull request đã merge (30 ngày)
141

Mô tả

The Blowfish cryptographic algorithm is considered secure by CodeQL’s `java-code-scanning` or `java-lgtm-full` query suite (reference: https://github.com/github/codeql/blob/main/java/ql/src/semmle/code/java/security/Encryption.qll). However, Blowfish is only considered secure when a key size of greater than 128 is used (reference: https://rules.sonarsource.com/java/RSPEC-4787?search=blowfish ).
Since `java-code-scanning` or `java-lgtm-full` query suite do not check for key size they will not flag insecure cryptographic use that uses BlowFish with a key size less than 128.

Here is an example:

```java
import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.security.SecureRandom;
import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class BlowFish {

public static void main(String[] args) {
int keygen_size = 64;
try {

KeyGenerator keyGenerator = KeyGenerator.getInstance("Blowfish");
keyGenerator.init(keygen_size);

Key key = keyGenerator.generateKey();

Cipher cipher = Cipher.getInstance("Blowfish/CFB8/NoPadding");

SecureRandom random = new SecureRandom();
byte[] iv = new byte[8];
random.nextBytes(iv);

IvParameterSpec spec = new IvParameterSpec(iv);
cipher.init(Cipher.ENCRYPT_MODE, key, spec);
System.out.println(cipher.getBlockSize());
String plaintext = "Hello World";
byte[] encrypted = cipher.doFinal(plaintext.getBytes());

String encrypted_str = new String(encrypted, StandardCharsets.UTF_8);

System.out.println(encrypted_str);

Cipher decrypter = Cipher.getInstance("Blowfish/CFB8/NoPadding");
decrypter.init(Cipher.DECRYPT_MODE, key, spec);

byte [] retrieved = decrypter.doFinal(encrypted);

String retrieved_text = new String(retrieved, StandardCharsets.UTF_8);

System.out.println(retrieved_text);
} catch (Exception e) {
e.printStackTrace();
}

}
}
```

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.