Source file src/crypto/rsa/boring_test.go

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build boringcrypto
     6  
     7  // Note: Can run these tests against the non-BoringCrypto
     8  // version of the code by using "CGO_ENABLED=0 go test".
     9  
    10  package rsa
    11  
    12  import (
    13  	"crypto"
    14  	"crypto/rand"
    15  	"encoding/asn1"
    16  	"encoding/hex"
    17  	"math/big"
    18  	"runtime"
    19  	"runtime/debug"
    20  	"sync"
    21  	"testing"
    22  )
    23  
    24  func TestBoringASN1Marshal(t *testing.T) {
    25  	t.Setenv("GODEBUG", "rsa1024min=0")
    26  
    27  	k, err := GenerateKey(rand.Reader, 128)
    28  	if err != nil {
    29  		t.Fatal(err)
    30  	}
    31  	_, err = asn1.Marshal(k.PublicKey)
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  }
    36  
    37  func TestBoringVerify(t *testing.T) {
    38  	// Check that signatures that lack leading zeroes don't verify.
    39  	key := &PublicKey{
    40  		N: bigFromHex("c4fdf7b40a5477f206e6ee278eaef888ca73bf9128a9eef9f2f1ddb8b7b71a4c07cfa241f028a04edb405e4d916c61d6beabc333813dc7b484d2b3c52ee233c6a79b1eea4e9cc51596ba9cd5ac5aeb9df62d86ea051055b79d03f8a4fa9f38386f5bd17529138f3325d46801514ea9047977e0829ed728e68636802796801be1"),
    41  		E: 65537,
    42  	}
    43  
    44  	hash := fromHex("019c5571724fb5d0e47a4260c940e9803ba05a44")
    45  	paddedHash := fromHex("3021300906052b0e03021a05000414019c5571724fb5d0e47a4260c940e9803ba05a44")
    46  
    47  	// signature is one byte shorter than key.N.
    48  	sig := fromHex("5edfbeb6a73e7225ad3cc52724e2872e04260d7daf0d693c170d8c4b243b8767bc7785763533febc62ec2600c30603c433c095453ede59ff2fcabeb84ce32e0ed9d5cf15ffcbc816202b64370d4d77c1e9077d74e94a16fb4fa2e5bec23a56d7a73cf275f91691ae1801a976fcde09e981a2f6327ac27ea1fecf3185df0d56")
    49  
    50  	err := VerifyPKCS1v15(key, 0, paddedHash, sig)
    51  	if err == nil {
    52  		t.Errorf("raw: expected verification error")
    53  	}
    54  
    55  	err = VerifyPKCS1v15(key, crypto.SHA1, hash, sig)
    56  	if err == nil {
    57  		t.Errorf("sha1: expected verification error")
    58  	}
    59  }
    60  
    61  func BenchmarkBoringVerify(b *testing.B) {
    62  	// Check that signatures that lack leading zeroes don't verify.
    63  	key := &PublicKey{
    64  		N: bigFromHex("c4fdf7b40a5477f206e6ee278eaef888ca73bf9128a9eef9f2f1ddb8b7b71a4c07cfa241f028a04edb405e4d916c61d6beabc333813dc7b484d2b3c52ee233c6a79b1eea4e9cc51596ba9cd5ac5aeb9df62d86ea051055b79d03f8a4fa9f38386f5bd17529138f3325d46801514ea9047977e0829ed728e68636802796801be1"),
    65  		E: 65537,
    66  	}
    67  
    68  	hash := fromHex("019c5571724fb5d0e47a4260c940e9803ba05a44")
    69  
    70  	// signature is one byte shorter than key.N.
    71  	sig := fromHex("5edfbeb6a73e7225ad3cc52724e2872e04260d7daf0d693c170d8c4b243b8767bc7785763533febc62ec2600c30603c433c095453ede59ff2fcabeb84ce32e0ed9d5cf15ffcbc816202b64370d4d77c1e9077d74e94a16fb4fa2e5bec23a56d7a73cf275f91691ae1801a976fcde09e981a2f6327ac27ea1fecf3185df0d56")
    72  
    73  	b.ReportAllocs()
    74  
    75  	for i := 0; i < b.N; i++ {
    76  		err := VerifyPKCS1v15(key, crypto.SHA1, hash, sig)
    77  		if err == nil {
    78  			b.Fatalf("sha1: expected verification error")
    79  		}
    80  	}
    81  }
    82  
    83  func TestBoringGenerateKey(t *testing.T) {
    84  	k, err := GenerateKey(rand.Reader, 2048) // 2048 is smallest size BoringCrypto might kick in for
    85  	if err != nil {
    86  		t.Fatal(err)
    87  	}
    88  
    89  	// Non-Boring GenerateKey always sets CRTValues to a non-nil (possibly empty) slice.
    90  	if k.Precomputed.CRTValues == nil {
    91  		t.Fatalf("GenerateKey: Precomputed.CRTValues = nil")
    92  	}
    93  }
    94  
    95  func TestBoringFinalizers(t *testing.T) {
    96  	if runtime.GOOS == "nacl" || runtime.GOOS == "js" {
    97  		// Times out on nacl and js/wasm (without BoringCrypto)
    98  		// but not clear why - probably consuming rand.Reader too quickly
    99  		// and being throttled. Also doesn't really matter.
   100  		t.Skipf("skipping on %s/%s", runtime.GOOS, runtime.GOARCH)
   101  	}
   102  
   103  	k, err := GenerateKey(rand.Reader, 2048)
   104  	if err != nil {
   105  		t.Fatal(err)
   106  	}
   107  
   108  	// Run test with GOGC=10, to make bug more likely.
   109  	// Without the KeepAlives, the loop usually dies after
   110  	// about 30 iterations.
   111  	defer debug.SetGCPercent(debug.SetGCPercent(10))
   112  	for n := 0; n < 200; n++ {
   113  		// Clear the underlying BoringCrypto object cache.
   114  		privCache.Clear()
   115  
   116  		// Race to create the underlying BoringCrypto object.
   117  		// The ones that lose the race are prime candidates for
   118  		// being GC'ed too early if the finalizers are not being
   119  		// used correctly.
   120  		var wg sync.WaitGroup
   121  		for i := 0; i < 10; i++ {
   122  			wg.Add(1)
   123  			go func() {
   124  				defer wg.Done()
   125  				sum := make([]byte, 32)
   126  				_, err := SignPKCS1v15(rand.Reader, k, crypto.SHA256, sum)
   127  				if err != nil {
   128  					panic(err) // usually caused by memory corruption, so hard stop
   129  				}
   130  			}()
   131  		}
   132  		wg.Wait()
   133  	}
   134  }
   135  
   136  func bigFromHex(hex string) *big.Int {
   137  	n, ok := new(big.Int).SetString(hex, 16)
   138  	if !ok {
   139  		panic("bad hex: " + hex)
   140  	}
   141  	return n
   142  }
   143  
   144  func fromHex(hexStr string) []byte {
   145  	s, err := hex.DecodeString(hexStr)
   146  	if err != nil {
   147  		panic(err)
   148  	}
   149  	return s
   150  }
   151  

View as plain text