github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/fuzz/nacl_enc/fuzz.go (about)

     1  // Copyright 2015 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  //go:build gofuzz
     5  // +build gofuzz
     6  
     7  package libkb
     8  
     9  import (
    10  	"bytes"
    11  
    12  	"github.com/keybase/client/go/libkb"
    13  )
    14  
    15  var kp1, kp2 libkb.NaclDHKeyPair
    16  
    17  func init() {
    18  	var err error
    19  	kp1, err = libkb.GenerateNaclDHKeyPair()
    20  	if err != nil {
    21  		panic(err)
    22  	}
    23  	kp2, err = libkb.GenerateNaclDHKeyPair()
    24  	if err != nil {
    25  		panic(err)
    26  	}
    27  }
    28  
    29  func Fuzz(data []byte) int {
    30  	ctext, err := kp1.EncryptToString(data, nil)
    31  	if err != nil {
    32  		panic(err)
    33  	}
    34  	out, kid, err := kp1.DecryptFromString(ctext)
    35  	if err != nil {
    36  		panic(err)
    37  	}
    38  	if !bytes.Equal(out, data) {
    39  		panic("message mismatch")
    40  	}
    41  	if kid.Equal(kp1.GetKID()) {
    42  		panic("kid should be ephemeral")
    43  	}
    44  
    45  	ctext, err = kp2.EncryptToString(data, &kp1)
    46  	if err != nil {
    47  		panic(err)
    48  	}
    49  	out, kid, err = kp2.DecryptFromString(ctext)
    50  	if err != nil {
    51  		panic(err)
    52  	}
    53  	if !bytes.Equal(out, data) {
    54  		panic("message mismatch")
    55  	}
    56  	if kid.NotEqual(kp1.GetKID()) {
    57  		panic("KID mismatch for sender")
    58  	}
    59  
    60  	return 0
    61  }