github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/kbfs/kbfscrypto/signature_test.go (about)

     1  // Copyright 2016 Keybase Inc. All rights reserved.
     2  // Use of this source code is governed by a BSD
     3  // license that can be found in the LICENSE file.
     4  
     5  package kbfscrypto
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/keybase/client/go/kbcrypto"
    11  	"github.com/keybase/client/go/libkb"
    12  	"github.com/pkg/errors"
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  // Test that Verify() rejects various types of bad signatures.
    17  func TestVerifyFailures(t *testing.T) {
    18  	signingKey := MakeFakeSigningKeyOrBust("client sign")
    19  
    20  	msg := []byte("message")
    21  	sigInfo := signingKey.Sign(msg)
    22  
    23  	// Wrong version.
    24  
    25  	sigInfoWrongVersion := sigInfo.DeepCopy()
    26  	sigInfoWrongVersion.Version = 0
    27  	err := Verify(msg, sigInfoWrongVersion)
    28  	assert.Equal(t, UnknownSigVer{Ver: sigInfoWrongVersion.Version},
    29  		errors.Cause(err))
    30  
    31  	// Corrupt key.
    32  
    33  	sigInfoCorruptKey := sigInfo.DeepCopy()
    34  	sigInfoCorruptKey.VerifyingKey = MakeVerifyingKey("")
    35  	err = Verify(msg, sigInfoCorruptKey)
    36  	assert.Equal(t, libkb.KeyCannotVerifyError{}, errors.Cause(err))
    37  
    38  	// Wrong sizes.
    39  
    40  	shortSigInfo := sigInfo.DeepCopy()
    41  	shortSigInfo.Signature = shortSigInfo.Signature[:len(shortSigInfo.Signature)-1]
    42  	err = Verify(msg, shortSigInfo)
    43  	assert.Equal(t, kbcrypto.VerificationError{}, errors.Cause(err))
    44  
    45  	longSigInfo := sigInfo.DeepCopy()
    46  	longSigInfo.Signature = append(longSigInfo.Signature, byte(0))
    47  	err = Verify(msg, longSigInfo)
    48  	assert.Equal(t, kbcrypto.VerificationError{}, errors.Cause(err))
    49  
    50  	// Corrupt signature.
    51  
    52  	corruptSigInfo := sigInfo.DeepCopy()
    53  	corruptSigInfo.Signature[0] = ^sigInfo.Signature[0]
    54  	err = Verify(msg, corruptSigInfo)
    55  	assert.Equal(t, kbcrypto.VerificationError{}, errors.Cause(err))
    56  
    57  	// Wrong key.
    58  
    59  	sigInfoWrongKey := sigInfo.DeepCopy()
    60  	sigInfoWrongKey.VerifyingKey = MakeFakeVerifyingKeyOrBust("wrong key")
    61  	err = Verify(msg, sigInfoWrongKey)
    62  	assert.Equal(t, kbcrypto.VerificationError{}, errors.Cause(err))
    63  
    64  	// Corrupt message.
    65  
    66  	corruptMsg := msg
    67  	corruptMsg = append(corruptMsg, []byte("corruption")...)
    68  	err = Verify(corruptMsg, sigInfo)
    69  	assert.Equal(t, kbcrypto.VerificationError{}, errors.Cause(err))
    70  }