| 1 |
// |
| 2 |
// Copyright 2022 The Sigstore Authors. |
| 3 |
// |
| 4 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
// you may not use this file except in compliance with the License. |
| 6 |
// You may obtain a copy of the License at |
| 7 |
// |
| 8 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
// |
| 10 |
// Unless required by applicable law or agreed to in writing, software |
| 11 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
// See the License for the specific language governing permissions and |
| 14 |
// limitations under the License. |
| 15 |
|
| 16 |
package signature |
| 17 |
|
| 18 |
import ( |
| 19 |
"bytes" |
| 20 |
"crypto" |
| 21 |
"crypto/ecdsa" |
| 22 |
"crypto/ed25519" |
| 23 |
"crypto/elliptic" |
| 24 |
"crypto/rsa" |
| 25 |
"math/big" |
| 26 |
"testing" |
| 27 |
|
| 28 |
"github.com/sigstore/sigstore/pkg/cryptoutils" |
| 29 |
|
| 30 |
fuzz "github.com/AdaLogics/go-fuzz-headers" |
| 31 |
"github.com/sigstore/sigstore/pkg/signature" |
| 32 |
) |
| 33 |
|
| 34 |
func FuzzECDSASigner(f *testing.F) { |
| 35 |
f.Fuzz(func(t *testing.T, data []byte) { |
| 36 |
x := ecdsa.PrivateKey{} |
| 37 |
z := new(big.Int) |
| 38 |
z.SetBytes(data) |
| 39 |
x.X = z |
| 40 |
x.Y = z |
| 41 |
x.D = z |
| 42 |
x.Curve = elliptic.P384() |
| 43 |
|
| 44 |
signer, err := signature.LoadECDSASignerVerifier(&x, crypto.SHA512) |
| 45 |
if err != nil { |
| 46 |
if signer != nil { |
| 47 |
t.Errorf("key %v is not nil when there is an error %v ", signer, err) |
| 48 |
} |
| 49 |
t.Skip("not valid key") |
| 50 |
} |
| 51 |
|
| 52 |
sig, err := signer.SignMessage(bytes.NewReader(data)) |
| 53 |
if err != nil { |
| 54 |
if sig != nil { |
| 55 |
t.Errorf("key %v is not nil when there is an error %v ", sig, err) |
| 56 |
} |
| 57 |
t.Skip("not valid key") |
| 58 |
} |
| 59 |
|
| 60 |
if err = signer.VerifySignature(bytes.NewReader(sig), bytes.NewReader(data)); err != nil { |
| 61 |
t.Skip("not valid key") |
| 62 |
} |
| 63 |
}) |
| 64 |
} |
| 65 |
|
| 66 |
func FuzzComputeDigest(f *testing.F) { |
| 67 |
f.Fuzz(func(t *testing.T, data []byte) { |
| 68 |
hashFuncs := []crypto.Hash{ |
| 69 |
crypto.SHA256, |
| 70 |
crypto.SHA512, |
| 71 |
crypto.SHA384, |
| 72 |
crypto.SHA224, |
| 73 |
crypto.SHA1, |
| 74 |
} |
| 75 |
data, _, err := signature.ComputeDigestForSigning(bytes.NewReader(data), crypto.SHA512, hashFuncs) |
| 76 |
if err != nil { |
| 77 |
if data != nil { |
| 78 |
t.Errorf("key %v is not nil when there is an error %v ", data, err) |
| 79 |
} |
| 80 |
t.Skip("not valid key") |
| 81 |
} |
| 82 |
}) |
| 83 |
} |
| 84 |
|
| 85 |
func FuzzComputeVerifying(f *testing.F) { |
| 86 |
f.Fuzz(func(t *testing.T, data []byte) { |
| 87 |
hashFuncs := []crypto.Hash{ |
| 88 |
crypto.SHA256, |
| 89 |
crypto.SHA512, |
| 90 |
crypto.SHA384, |
| 91 |
crypto.SHA224, |
| 92 |
crypto.SHA1, |
| 93 |
} |
| 94 |
data, _, err := signature.ComputeDigestForVerifying(bytes.NewReader(data), crypto.SHA512, hashFuncs) |
| 95 |
if err != nil { |
| 96 |
if data != nil { |
| 97 |
t.Errorf("key %v is not nil when there is an error %v ", data, err) |
| 98 |
} |
| 99 |
t.Skip("not valid key") |
| 100 |
} |
| 101 |
}) |
| 102 |
} |
| 103 |
|
| 104 |
func FuzzED25529SignerVerfier(f *testing.F) { |
| 105 |
f.Fuzz(func(t *testing.T, data []byte) { |
| 106 |
x := ed25519.PrivateKey(data) |
| 107 |
|
| 108 |
signer, err := signature.LoadED25519SignerVerifier(x) |
| 109 |
if err != nil { |
| 110 |
if signer != nil { |
| 111 |
t.Errorf("key %v is not nil when there is an error %v ", signer, err) |
| 112 |
} |
| 113 |
t.Skip("not valid key") |
| 114 |
} |
| 115 |
|
| 116 |
sig, err := signer.SignMessage(bytes.NewReader(data)) |
| 117 |
if err != nil { |
| 118 |
if sig != nil { |
| 119 |
t.Errorf("key %v is not nil when there is an error %v ", sig, err) |
| 120 |
} |
| 121 |
t.Skip("not valid key") |
| 122 |
} |
| 123 |
|
| 124 |
signer.VerifySignature(bytes.NewReader(sig), bytes.NewReader(data)) |
| 125 |
}) |
| 126 |
} |
| 127 |
|
| 128 |
func FuzzRSAPKCS1v15SignerVerfier(f *testing.F) { |
| 129 |
f.Fuzz(func(t *testing.T, data []byte) { |
| 130 |
f := fuzz.NewConsumer(data) |
| 131 |
x := rsa.PrivateKey{} |
| 132 |
f.GenerateStruct(&x) |
| 133 |
|
| 134 |
signer, err := signature.LoadRSAPKCS1v15Signer(&x, crypto.SHA512) |
| 135 |
if err != nil { |
| 136 |
if signer != nil { |
| 137 |
t.Errorf("key %v is not nil when there is an error %v ", signer, err) |
| 138 |
} |
| 139 |
t.Skip("not valid key") |
| 140 |
} |
| 141 |
|
| 142 |
sig, err := signer.SignMessage(bytes.NewReader(data)) |
| 143 |
if err != nil { |
| 144 |
if sig != nil { |
| 145 |
t.Errorf("key %v is not nil when there is an error %v ", sig, err) |
| 146 |
} |
| 147 |
t.Skip("not valid key") |
| 148 |
} |
| 149 |
if _, err := signer.Sign(bytes.NewReader(data), data, nil); err != nil { |
| 150 |
t.Skip("not valid key") |
| 151 |
} |
| 152 |
}) |
| 153 |
} |
| 154 |
|
| 155 |
func FuzzRSAPSSSignerVerfier(f *testing.F) { |
| 156 |
f.Fuzz(func(t *testing.T, data []byte) { |
| 157 |
privateKey, err := cryptoutils.UnmarshalPEMToPrivateKey(data, cryptoutils.SkipPassword) |
| 158 |
if err != nil { |
| 159 |
t.Skip() |
| 160 |
} |
| 161 |
signer, err := signature.LoadRSAPSSSignerVerifier(privateKey.(*rsa.PrivateKey), crypto.SHA512, nil) |
| 162 |
if err != nil { |
| 163 |
if signer != nil { |
| 164 |
t.Errorf("key %v is not nil when there is an error %v ", signer, err) |
| 165 |
} |
| 166 |
t.Skip("not valid key") |
| 167 |
} |
| 168 |
|
| 169 |
sig, err := signer.SignMessage(bytes.NewReader(data)) |
| 170 |
if err != nil { |
| 171 |
if sig != nil { |
| 172 |
t.Errorf("key %v is not nil when there is an error %v ", sig, err) |
| 173 |
} |
| 174 |
t.Skip("not valid key") |
| 175 |
} |
| 176 |
if _, err := signer.Sign(bytes.NewReader(data), data, nil); err != nil { |
| 177 |
t.Skip("not valid key") |
| 178 |
} |
| 179 |
}) |
| 180 |
} |