This rule addresses the use of Initialization Vectors (IVs) in counter mode encryption. An Initialization Vector is a random number that is used as the starting point for encryption algorithms. It’s essential to generate fresh IVs for every encryption operation to ensure the output is not predictable, enhancing the overall security of the encryption process.
Using static or hard-coded IVs introduces a significant security risk. If an attacker knows the IV, they can predict the output of the encryption, making it much easier to break. The fundamental principle of cryptography is to keep all aspects of the encryption process as unpredictable as possible.
To adhere to this rule, always generate a fresh, random IV for each encryption operation. This ensures that even if an attacker manages to compromise one encryption operation, subsequent operations remain secure due to the unpredictability of the IVs.
Non-Compliant Code Examples
// Noncompliant - Using a static IV
classInsecureEncryption{funencryptData(secretKey:String,data:ByteArray):ByteArray{// BAD: Hardcoded IV that will be reused for every encryption
valstaticIV="staticIVvalue123".toByteArray()valcipher=Cipher.getInstance("AES/CCM/NoPadding")valkey=SecretKeySpec(secretKey.toByteArray(),"AES")valparamSpec=CCMParameterSpec(128,staticIV)cipher.init(Cipher.ENCRYPT_MODE,key,paramSpec)returncipher.doFinal(data)}}
Compliant Code Examples
// Compliant - Using a secure random IV
classSecureEncryption{funencryptData(secretKey:String,data:ByteArray):ByteArray{// GOOD: Generate fresh random IV for each encryption
valsecureRandom=SecureRandom()valrandomIV=ByteArray(12).apply{secureRandom.nextBytes(this)}valcipher=Cipher.getInstance("AES/CCM/NoPadding")valkey=SecretKeySpec(secretKey.toByteArray(),"AES")valparamSpec=CCMParameterSpec(128,randomIV)cipher.init(Cipher.ENCRYPT_MODE,key,paramSpec)returncipher.doFinal(data)}}
원활한 통합. Datadog Code Security를 경험해 보세요
Datadog Code Security
이 규칙을 사용해 Datadog Code Security로 코드를 분석하세요
규칙 사용 방법
1
2
rulesets:- kotlin-security # Rules to enforce Kotlin security.
리포지토리 루트에 위의 내용을 포함하는 static-analysis.datadog.yml을 만듭니다
무료 IDE 플러그인을 사용하거나 CI 파이프라인에 Code Security 검사를 추가합니다