-
Notifications
You must be signed in to change notification settings - Fork 0
integrity: signer and verifier implementation #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
patapenka-alexey
wants to merge
1
commit into
master
Choose a base branch
from
patapenka-alexey/tntp-4171-signer-verifier
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| // Package crypto implements crypto interfaces. | ||
| package crypto | ||
|
|
||
| // Signer implements high-level API for package signing. | ||
| type Signer interface { | ||
| // Name returns name of the crypto algorithm, used by signer. | ||
| Name() string | ||
| // Sign returns signature for passed data. | ||
| Sign(data []byte) ([]byte, error) | ||
| } | ||
|
|
||
| // Verifier is an interface implementing a generic signature | ||
| // verification algorithm. | ||
| type Verifier interface { | ||
| // Name returns name of the crypto algorithm, used by verifier. | ||
| Name() string | ||
| // Verify checks data and signature mapping. | ||
| Verify(data []byte, signature []byte) error | ||
| } | ||
|
|
||
| // SignerVerifier common interface. | ||
| type SignerVerifier interface { | ||
| Signer | ||
| Verifier | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package crypto | ||
|
|
||
| import ( | ||
| "crypto" | ||
| "crypto/rand" | ||
| "crypto/rsa" | ||
| "fmt" | ||
|
|
||
| "github.com/tarantool/go-storage/hasher" | ||
| ) | ||
|
|
||
| // RSAPSS represents RSA PSS algo for signing/verification | ||
| // (with SHA256 as digest calculation function). | ||
| type RSAPSS struct { | ||
| publicKey rsa.PublicKey | ||
| privateKey rsa.PrivateKey | ||
| hash crypto.Hash | ||
| hasher hasher.Hasher | ||
| } | ||
|
|
||
| // NewRSAPSS creates new RSAPSS object. | ||
| func NewRSAPSS(privKey rsa.PrivateKey, pubKey rsa.PublicKey) RSAPSS { | ||
| return RSAPSS{ | ||
| publicKey: pubKey, | ||
| privateKey: privKey, | ||
| hash: crypto.SHA256, | ||
| hasher: hasher.NewSHA256Hasher(), | ||
| } | ||
| } | ||
|
|
||
| // Name implements SignerVerifier interface. | ||
| func (r *RSAPSS) Name() string { | ||
| return "RSASSA-PSS" | ||
| } | ||
|
|
||
| // Sign generates SHA-256 digest and signs it using RSASSA-PSS. | ||
| func (r *RSAPSS) Sign(data []byte) ([]byte, error) { | ||
| digest, err := r.hasher.Hash(data) | ||
| if err != nil { | ||
| return []byte{}, fmt.Errorf("failed to get hash: %w", err) | ||
| } | ||
|
|
||
| signature, err := rsa.SignPSS(rand.Reader, &r.privateKey, r.hash, digest, &rsa.PSSOptions{ | ||
| SaltLength: rsa.PSSSaltLengthEqualsHash, | ||
| Hash: r.hash, | ||
| }) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to sign: %w", err) | ||
| } | ||
|
|
||
| return signature, nil | ||
| } | ||
|
|
||
| // Verify compares data with signature. | ||
| func (r *RSAPSS) Verify(data []byte, signature []byte) error { | ||
| digest, err := r.hasher.Hash(data) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get hash: %w", err) | ||
| } | ||
|
|
||
| err = rsa.VerifyPSS(&r.publicKey, r.hash, digest, signature, &rsa.PSSOptions{ | ||
| SaltLength: rsa.PSSSaltLengthEqualsHash, | ||
| Hash: r.hash, | ||
| }) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to verify: %w", err) | ||
| } | ||
|
|
||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| package crypto_test | ||
|
|
||
| import ( | ||
| "crypto/rand" | ||
| "crypto/rsa" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| "github.com/tarantool/go-storage/crypto" | ||
| ) | ||
|
|
||
| func TestRsaWithoutKeys(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| rsapss := crypto.NewRSAPSS(rsa.PrivateKey{}, rsa.PublicKey{}) //nolint:exhaustruct | ||
| require.NotNil(t, rsapss, "rsapss must be returned") | ||
|
|
||
| data := []byte("abc") | ||
|
|
||
| sig, err := rsapss.Sign(data) | ||
| require.ErrorContains(t, err, "failed to sign: crypto/rsa: missing public modulus") | ||
| require.Nil(t, sig, "signature must be nil") | ||
|
|
||
| err = rsapss.Verify(data, sig) | ||
| require.ErrorContains(t, err, "failed to verify: crypto/rsa: missing public modulus") | ||
| } | ||
|
|
||
| func TestRsaOnlyPrivateKey(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| privateKey, err := rsa.GenerateKey(rand.Reader, 2048) | ||
| require.NoError(t, err) | ||
|
|
||
| rsapss := crypto.NewRSAPSS(*privateKey, rsa.PublicKey{}) //nolint:exhaustruct | ||
| require.NotNil(t, rsapss, "rsapss must be returned") | ||
|
|
||
| data := []byte("abc") | ||
|
|
||
| sig, err := rsapss.Sign(data) | ||
| require.NoError(t, err, "Sign must be successful") | ||
| require.NotNil(t, sig, "signature must be returned") | ||
|
|
||
| err = rsapss.Verify(data, sig) | ||
| require.ErrorContains(t, err, "failed to verify: crypto/rsa: missing public modulus") | ||
| } | ||
|
|
||
| func TestRsaOnlyPublicKey(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| privateKey, err := rsa.GenerateKey(rand.Reader, 2048) | ||
| require.NoError(t, err) | ||
|
|
||
| rsapss := crypto.NewRSAPSS(rsa.PrivateKey{}, privateKey.PublicKey) //nolint:exhaustruct | ||
| require.NotNil(t, rsapss, "rsapss must be returned") | ||
|
|
||
| data := []byte("abc") | ||
|
|
||
| sig, err := rsapss.Sign(data) | ||
| require.ErrorContains(t, err, "failed to sign: crypto/rsa: missing public modulus") | ||
| require.Nil(t, sig, "signature must be nil") | ||
|
|
||
| // Re-create to have a valid sign. | ||
| rsapss = crypto.NewRSAPSS(*privateKey, privateKey.PublicKey) | ||
| require.NotNil(t, rsapss, "rsapss must be returned") | ||
|
|
||
| sign, err := rsapss.Sign(data) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, sign) | ||
|
|
||
| err = rsapss.Verify(data, sign) | ||
| require.NoError(t, err, "Verify must be successful") | ||
| } | ||
|
|
||
| func TestRsaSignVerify(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| privateKey, err := rsa.GenerateKey(rand.Reader, 2048) | ||
| require.NoError(t, err) | ||
|
|
||
| rsapss := crypto.NewRSAPSS(*privateKey, privateKey.PublicKey) | ||
| require.NotNil(t, rsapss, "rsapss must be returned") | ||
|
|
||
| data := []byte("abc") | ||
|
|
||
| sig, err := rsapss.Sign(data) | ||
| require.NoError(t, err, "Sign must be successful") | ||
| require.NotNil(t, sig, "signature must be returned") | ||
|
|
||
| err = rsapss.Verify(data, sig) | ||
| require.NoError(t, err, "Verify must be successful") | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or it could be:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This, ty