Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create go rule S5542: Encryption algorithms should be used with secure mode and padding scheme #4631

Merged
merged 4 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions rules/S5542/go/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
133 changes: 133 additions & 0 deletions rules/S5542/go/rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@

include::../summary.adoc[]

== Why is this an issue?

include::../rationale.adoc[]

include::../impact.adoc[]

// How to fix it section

== How to fix it

=== Code examples

==== Noncompliant code example

include::../common/fix/aes-noncompliant-example.adoc[]

[source,go,diff-id=1,diff-type=noncompliant]
----
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
)
func encrypt() {
plaintext := []byte("Exampleplaintext")

key := make([]byte, 32)
rand.Read(key)
block, _ := aes.NewCipher(key)
iv := make([]byte, block.BlockSize())
rand.Read(iv)

encrypter := cipher.NewCBCEncrypter(block, iv) // Noncompliant

ciphertext := make([]byte, len(plaintext))
encrypter.CryptBlocks(ciphertext, plaintext)
}
----

include::../common/fix/rsa-noncompliant-example.adoc[]

[source,go,diff-id=2,diff-type=noncompliant]
----
import (
"crypto/rand"
"crypto/rsa"
)
func encrypt() {
random := rand.Reader
plaintext := []byte("Exampleplaintext")
privateKey, _ := rsa.GenerateKey(random, 4096)
ciphertext, _ := rsa.EncryptPKCS1v15(random, &privateKey.PublicKey, plaintext) // Noncompliant
}
----

==== Compliant solution

include::../common/fix/aes-compliant-example.adoc[]

[source,go,diff-id=1,diff-type=compliant]
----
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
)
func encrypt() {
plaintext := []byte("Exampleplaintext")

key := make([]byte, 32)
rand.Read(key)
block, _ := aes.NewCipher(key)
nonce := make([]byte, 12)
rand.Read(nonce)

aesgcm, _ := cipher.NewGCM(block) // Compliant

ciphertext := aesgcm.Seal(nil, nonce, plaintext, nil)
}
----

include::../common/fix/rsa-compliant-example.adoc[]

[source,go,diff-id=2,diff-type=compliant]
----
import (
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
)
func encrypt() {
random := rand.Reader
plaintext := []byte("Exampleplaintext")
privateKey, _ := rsa.GenerateKey(random, 4096)
ciphertext, _ := rsa.EncryptOAEP(sha256.New(), random, &privateKey.PublicKey, plaintext, nil) // Compliant
}
----

=== How does this work?

include::../common/fix/fix.adoc[]



== Resources

include::../common/resources/docs.adoc[]

include::../common/resources/articles.adoc[]

include::../common/resources/presentations.adoc[]

include::../common/resources/standards.adoc[]


ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

include::../message.adoc[]

'''
== Comments And Links
(visible only on this page)

include::../comments-and-links.adoc[]

endif::env-github,rspecator-view[]