stupidgcm: normalize constructor naming

New() -> NewAES256GCM()

Also add missing NewChacha20poly1305
constructor in without_openssl.go.
This commit is contained in:
Jakob Unterwurzacher 2021-09-07 18:11:11 +02:00
parent f47e287c20
commit 85c2beccaf
8 changed files with 16 additions and 13 deletions

View File

@ -120,7 +120,7 @@ func New(key []byte, aeadType AEADTypeEnum, IVBitLen int, useHKDF bool, forceDec
if IVBitLen != 128 { if IVBitLen != 128 {
log.Panicf("stupidgcm only supports 128-bit IVs, you wanted %d", IVBitLen) log.Panicf("stupidgcm only supports 128-bit IVs, you wanted %d", IVBitLen)
} }
aeadCipher = stupidgcm.New(gcmKey, forceDecode) aeadCipher = stupidgcm.NewAES256GCM(gcmKey, forceDecode)
case BackendGoGCM: case BackendGoGCM:
goGcmBlockCipher, err := aes.NewCipher(gcmKey) goGcmBlockCipher, err := aes.NewCipher(gcmKey)
if err != nil { if err != nil {

View File

@ -116,7 +116,7 @@ func bStupidGCM(b *testing.B) {
if stupidgcm.BuiltWithoutOpenssl { if stupidgcm.BuiltWithoutOpenssl {
b.Skip("openssl has been disabled at compile-time") b.Skip("openssl has been disabled at compile-time")
} }
bEncrypt(b, stupidgcm.New(randBytes(32), false)) bEncrypt(b, stupidgcm.NewAES256GCM(randBytes(32), false))
} }
// bGoGCM benchmarks Go stdlib GCM // bGoGCM benchmarks Go stdlib GCM

View File

@ -31,7 +31,7 @@ func BenchmarkStupidGCMDecrypt(b *testing.B) {
if stupidgcm.BuiltWithoutOpenssl { if stupidgcm.BuiltWithoutOpenssl {
b.Skip("openssl has been disabled at compile-time") b.Skip("openssl has been disabled at compile-time")
} }
bDecrypt(b, stupidgcm.New(randBytes(32), false)) bDecrypt(b, stupidgcm.NewAES256GCM(randBytes(32), false))
} }
func BenchmarkGoGCM(b *testing.B) { func BenchmarkGoGCM(b *testing.B) {

View File

@ -37,7 +37,7 @@ func init() {
// block by XChaCha20-Poly1305. // block by XChaCha20-Poly1305.
// //
// Only 32-bytes keys and 12-byte IVs are supported. // Only 32-bytes keys and 12-byte IVs are supported.
func NewChacha20poly1305(key []byte) *stupidChacha20poly1305 { func NewChacha20poly1305(key []byte) cipher.AEAD {
if len(key) != chacha20poly1305.KeySize { if len(key) != chacha20poly1305.KeySize {
log.Panicf("Only %d-byte keys are supported, you passed %d bytes", chacha20poly1305.KeySize, len(key)) log.Panicf("Only %d-byte keys are supported, you passed %d bytes", chacha20poly1305.KeySize, len(key))
} }

View File

@ -23,10 +23,10 @@ type stupidGCM struct {
stupidAEADCommon stupidAEADCommon
} }
// New returns a new AES-GCM-256 cipher that satisfies the cipher.AEAD interface. // NewAES256GCM returns a new AES-256-GCM cipher that satisfies the cipher.AEAD interface.
// //
// Only 32-bytes keys and 16-byte IVs are supported. // Only 32-bytes keys and 16-byte IVs are supported.
func New(keyIn []byte, forceDecode bool) cipher.AEAD { func NewAES256GCM(keyIn []byte, forceDecode bool) cipher.AEAD {
if len(keyIn) != keyLen { if len(keyIn) != keyLen {
log.Panicf("Only %d-byte keys are supported", keyLen) log.Panicf("Only %d-byte keys are supported", keyLen)
} }

View File

@ -13,7 +13,7 @@ import (
func TestStupidGCM(t *testing.T) { func TestStupidGCM(t *testing.T) {
key := randBytes(32) key := randBytes(32)
sGCM := New(key, false) sGCM := NewAES256GCM(key, false)
gAES, err := aes.NewCipher(key) gAES, err := aes.NewCipher(key)
if err != nil { if err != nil {

View File

@ -11,19 +11,22 @@ import (
"github.com/rfjakob/gocryptfs/v2/internal/exitcodes" "github.com/rfjakob/gocryptfs/v2/internal/exitcodes"
) )
type StupidGCM struct{}
const ( const (
// BuiltWithoutOpenssl indicates if openssl been disabled at compile-time // BuiltWithoutOpenssl indicates if openssl been disabled at compile-time
BuiltWithoutOpenssl = true BuiltWithoutOpenssl = true
) )
func errExit() { func errExit() {
fmt.Fprintln(os.Stderr, "gocryptfs has been compiled without openssl support but you are still trying to use openssl") fmt.Fprintln(os.Stderr, "I have been compiled without openssl support but you are still trying to use openssl")
os.Exit(exitcodes.OpenSSL) os.Exit(exitcodes.OpenSSL)
} }
func New(_ []byte, _ bool) cipher.AEAD { func NewAES256GCM(_ []byte, _ bool) cipher.AEAD {
errExit()
return nil
}
func NewChacha20poly1305(_ []byte) cipher.AEAD {
errExit() errExit()
return nil return nil
} }

View File

@ -70,7 +70,7 @@ func (x *stupidXchacha20poly1305) Seal(dst, nonce, plaintext, additionalData []b
} }
hKey, _ := chacha20.HChaCha20(x.key[:], nonce[0:16]) hKey, _ := chacha20.HChaCha20(x.key[:], nonce[0:16])
c := NewChacha20poly1305(hKey) c := NewChacha20poly1305(hKey).(*stupidChacha20poly1305)
defer c.Wipe() defer c.Wipe()
// The first 4 bytes of the final nonce are unused counter space. // The first 4 bytes of the final nonce are unused counter space.
@ -95,7 +95,7 @@ func (x *stupidXchacha20poly1305) Open(dst, nonce, ciphertext, additionalData []
} }
hKey, _ := chacha20.HChaCha20(x.key[:], nonce[0:16]) hKey, _ := chacha20.HChaCha20(x.key[:], nonce[0:16])
c := NewChacha20poly1305(hKey) c := NewChacha20poly1305(hKey).(*stupidChacha20poly1305)
defer c.Wipe() defer c.Wipe()
// The first 4 bytes of the final nonce are unused counter space. // The first 4 bytes of the final nonce are unused counter space.