Use of a broken or weak cryptographic algorithm¶
ID: rb/weak-cryptographic-algorithmKind: problemSecurity severity: 7.5Severity: warningPrecision: highTags: - security - external/cwe/cwe-327Query suites: - ruby-code-scanning.qls - ruby-security-extended.qls - ruby-security-and-quality.qls
Click to see the query in the CodeQL repository
Using broken or weak cryptographic algorithms can leave data vulnerable to being decrypted or forged by an attacker.
Many cryptographic algorithms provided by cryptography libraries are known to be weak, or flawed. Using such an algorithm means that encrypted or hashed data is less secure than it appears to be.
Recommendation¶
Ensure that you use a strong, modern cryptographic algorithm, such as AES-128 or RSA-2048.
Example¶
The following code uses theOpenSSL library to encrypt some secret data. When you create a cipher usingOpenSSL you must specify the encryption algorithm to use. The first example uses DES, which is an older algorithm that is now considered weak. The second example uses AES, which is a stronger modern algorithm.
require'openssl'classEncryptorattr_accessor:secret_keydefencrypt_message_weak(message)cipher=OpenSSL::Cipher.new('des')# BAD: weak encryptioncipher.encryptcipher.key=secret_keycipher.update(message)cipher.finalenddefencrypt_message_strong(message)cipher=OpenSSL::Cipher::AES128.new# GOOD: strong encryptioncipher.encryptcipher.key=secret_keycipher.update(message)cipher.finalendend
References¶
NIST, FIPS 140 Annex a: Approved Security Functions.
NIST, SP 800-131A: Transitions: Recommendation for Transitioning the Use of Cryptographic Algorithms and Key Lengths.
Common Weakness Enumeration:CWE-327.