github.com/mtsmfm/go/src@v0.0.0-20221020090648-44bdcb9f8fde/crypto/rsa/boring_test.go (about) 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 "runtime" 17 "runtime/debug" 18 "sync" 19 "testing" 20 ) 21 22 func TestBoringASN1Marshal(t *testing.T) { 23 k, err := GenerateKey(rand.Reader, 128) 24 if err != nil { 25 t.Fatal(err) 26 } 27 _, err = asn1.Marshal(k.PublicKey) 28 if err != nil { 29 t.Fatal(err) 30 } 31 } 32 33 func TestBoringVerify(t *testing.T) { 34 // Check that signatures that lack leading zeroes don't verify. 35 key := &PublicKey{ 36 N: bigFromHex("c4fdf7b40a5477f206e6ee278eaef888ca73bf9128a9eef9f2f1ddb8b7b71a4c07cfa241f028a04edb405e4d916c61d6beabc333813dc7b484d2b3c52ee233c6a79b1eea4e9cc51596ba9cd5ac5aeb9df62d86ea051055b79d03f8a4fa9f38386f5bd17529138f3325d46801514ea9047977e0829ed728e68636802796801be1"), 37 E: 65537, 38 } 39 40 hash := fromHex("019c5571724fb5d0e47a4260c940e9803ba05a44") 41 paddedHash := fromHex("3021300906052b0e03021a05000414019c5571724fb5d0e47a4260c940e9803ba05a44") 42 43 // signature is one byte shorter than key.N. 44 sig := fromHex("5edfbeb6a73e7225ad3cc52724e2872e04260d7daf0d693c170d8c4b243b8767bc7785763533febc62ec2600c30603c433c095453ede59ff2fcabeb84ce32e0ed9d5cf15ffcbc816202b64370d4d77c1e9077d74e94a16fb4fa2e5bec23a56d7a73cf275f91691ae1801a976fcde09e981a2f6327ac27ea1fecf3185df0d56") 45 46 err := VerifyPKCS1v15(key, 0, paddedHash, sig) 47 if err == nil { 48 t.Errorf("raw: expected verification error") 49 } 50 51 err = VerifyPKCS1v15(key, crypto.SHA1, hash, sig) 52 if err == nil { 53 t.Errorf("sha1: expected verification error") 54 } 55 } 56 57 func BenchmarkBoringVerify(b *testing.B) { 58 // Check that signatures that lack leading zeroes don't verify. 59 key := &PublicKey{ 60 N: bigFromHex("c4fdf7b40a5477f206e6ee278eaef888ca73bf9128a9eef9f2f1ddb8b7b71a4c07cfa241f028a04edb405e4d916c61d6beabc333813dc7b484d2b3c52ee233c6a79b1eea4e9cc51596ba9cd5ac5aeb9df62d86ea051055b79d03f8a4fa9f38386f5bd17529138f3325d46801514ea9047977e0829ed728e68636802796801be1"), 61 E: 65537, 62 } 63 64 hash := fromHex("019c5571724fb5d0e47a4260c940e9803ba05a44") 65 66 // signature is one byte shorter than key.N. 67 sig := fromHex("5edfbeb6a73e7225ad3cc52724e2872e04260d7daf0d693c170d8c4b243b8767bc7785763533febc62ec2600c30603c433c095453ede59ff2fcabeb84ce32e0ed9d5cf15ffcbc816202b64370d4d77c1e9077d74e94a16fb4fa2e5bec23a56d7a73cf275f91691ae1801a976fcde09e981a2f6327ac27ea1fecf3185df0d56") 68 69 b.ReportAllocs() 70 71 for i := 0; i < b.N; i++ { 72 err := VerifyPKCS1v15(key, crypto.SHA1, hash, sig) 73 if err == nil { 74 b.Fatalf("sha1: expected verification error") 75 } 76 } 77 } 78 79 func TestBoringGenerateKey(t *testing.T) { 80 k, err := GenerateKey(rand.Reader, 2048) // 2048 is smallest size BoringCrypto might kick in for 81 if err != nil { 82 t.Fatal(err) 83 } 84 85 // Non-Boring GenerateKey always sets CRTValues to a non-nil (possibly empty) slice. 86 if k.Precomputed.CRTValues == nil { 87 t.Fatalf("GenerateKey: Precomputed.CRTValues = nil") 88 } 89 } 90 91 func TestBoringFinalizers(t *testing.T) { 92 if runtime.GOOS == "nacl" || runtime.GOOS == "js" { 93 // Times out on nacl and js/wasm (without BoringCrypto) 94 // but not clear why - probably consuming rand.Reader too quickly 95 // and being throttled. Also doesn't really matter. 96 t.Skipf("skipping on %s/%s", runtime.GOOS, runtime.GOARCH) 97 } 98 99 k, err := GenerateKey(rand.Reader, 2048) 100 if err != nil { 101 t.Fatal(err) 102 } 103 104 // Run test with GOGC=10, to make bug more likely. 105 // Without the KeepAlives, the loop usually dies after 106 // about 30 iterations. 107 defer debug.SetGCPercent(debug.SetGCPercent(10)) 108 for n := 0; n < 200; n++ { 109 // Clear the underlying BoringCrypto object cache. 110 privCache.Clear() 111 112 // Race to create the underlying BoringCrypto object. 113 // The ones that lose the race are prime candidates for 114 // being GC'ed too early if the finalizers are not being 115 // used correctly. 116 var wg sync.WaitGroup 117 for i := 0; i < 10; i++ { 118 wg.Add(1) 119 go func() { 120 defer wg.Done() 121 sum := make([]byte, 32) 122 _, err := SignPKCS1v15(rand.Reader, k, crypto.SHA256, sum) 123 if err != nil { 124 panic(err) // usually caused by memory corruption, so hard stop 125 } 126 }() 127 } 128 wg.Wait() 129 } 130 }