-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test compatibility with FIPS-enabled OpenSSL 1.0.2
Some of the standard library crypto/cipher AES-GCM tests fail when using OpenSSL 1.0.2 in FIPS mode, but pass with the same library when FIPS mode is disabled. Extend the AES-GCM tests to cover all the failure cases caught by the standard library tests and build the OpenSSL 1.0.2 library used in CI to be FIPS enabled. Link the OpenSSL 1.0.2 CI build against FIPS Object Module 2.0.1 rather than the latest version (2.0.16) as at least one commercially-supported FIPS validated build of OpenSSL 1.0.2 is known to use that version of the FIPS Object Module and some of the failures seen with 2.0.1 do not reproduce with 2.0.16. Signed-off-by: Cory Snider <csnider@mirantis.com>
- Loading branch information
Showing
4 changed files
with
875 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// gentestvectors emits cryptographic test vectors using the Go standard library | ||
// cryptographic routines to test the OpenSSL bindings. | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"crypto/aes" | ||
"crypto/cipher" | ||
"flag" | ||
"fmt" | ||
"go/format" | ||
"io" | ||
"log" | ||
"math/rand" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
var outputPath = flag.String("out", "", "output path (default stdout)") | ||
|
||
func init() { | ||
log.SetFlags(log.Llongfile) | ||
log.SetOutput(os.Stderr) | ||
} | ||
|
||
func main() { | ||
flag.Parse() | ||
|
||
var b bytes.Buffer | ||
fmt.Fprint(&b, "// Code generated by cmd/gentestvectors. DO NOT EDIT.\n\n") | ||
if *outputPath != "" { | ||
fmt.Fprintf(&b, "//go"+":generate go run github.com/golang-fips/openssl/v2/cmd/gentestvectors -out %s\n\n", filepath.Base(*outputPath)) | ||
} | ||
|
||
pkg := "openssl_test" | ||
if gopackage := os.Getenv("GOPACKAGE"); gopackage != "" { | ||
pkg = gopackage + "_test" | ||
} | ||
fmt.Fprintf(&b, "package %s\n\n", pkg) | ||
|
||
aesGCM(&b) | ||
|
||
generated, err := format.Source(b.Bytes()) | ||
if err != nil { | ||
log.Fatalf("failed to format generated code: %v", err) | ||
} | ||
|
||
if *outputPath != "" { | ||
err := os.WriteFile(*outputPath, generated, 0o644) | ||
if err != nil { | ||
log.Fatalf("failed to write output file: %v\n", err) | ||
} | ||
} else { | ||
_, _ = os.Stdout.Write(generated) | ||
} | ||
} | ||
|
||
func aesGCM(w io.Writer) { | ||
r := rand.New(rand.NewSource(0)) | ||
|
||
fmt.Fprintln(w, `var aesGCMTests = []struct { | ||
description string | ||
key, nonce, plaintext, aad, ciphertext []byte | ||
}{`) | ||
|
||
for _, keyLen := range []int{16, 24, 32} { | ||
for _, aadLen := range []int{0, 1, 3, 13, 30} { | ||
for _, plaintextLen := range []int{0, 1, 3, 13, 16, 51} { | ||
if aadLen == 0 && plaintextLen == 0 { | ||
continue | ||
} | ||
|
||
key := randbytes(r, keyLen) | ||
nonce := randbytes(r, 12) | ||
plaintext := randbytes(r, plaintextLen) | ||
aad := randbytes(r, aadLen) | ||
|
||
c, err := aes.NewCipher(key) | ||
if err != nil { | ||
panic(err) | ||
} | ||
aead, err := cipher.NewGCM(c) | ||
if err != nil { | ||
panic(err) | ||
} | ||
ciphertext := aead.Seal(nil, nonce, plaintext, aad) | ||
|
||
fmt.Fprint(w, "\t{\n") | ||
fmt.Fprintf(w, "\t\tdescription: \"AES-%d/AAD=%d/Plaintext=%d\",\n", keyLen*8, aadLen, plaintextLen) | ||
printBytesField(w, "key", key) | ||
printBytesField(w, "nonce", nonce) | ||
printBytesField(w, "plaintext", plaintext) | ||
printBytesField(w, "aad", aad) | ||
printBytesField(w, "ciphertext", ciphertext) | ||
fmt.Fprint(w, "\t},\n") | ||
} | ||
} | ||
} | ||
fmt.Fprintln(w, "}") | ||
} | ||
|
||
func randbytes(r *rand.Rand, n int) []byte { | ||
if n == 0 { | ||
return nil | ||
} | ||
b := make([]byte, n) | ||
r.Read(b) | ||
return b | ||
} | ||
|
||
func printBytesField(w io.Writer, name string, b []byte) { | ||
if len(b) == 0 { | ||
return | ||
} | ||
fmt.Fprintf(w, "\t\t%s: %#v,\n", name, b) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.