1// Copyright 2022 Google LLC 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14// 15//////////////////////////////////////////////////////////////////////////////// 16 17package signature 18 19import ( 20 "crypto" 21 "crypto/rsa" 22 "fmt" 23 "hash" 24 25 "github.com/google/tink/go/subtle" 26 "github.com/google/tink/go/tink" 27) 28 29// RSA_SSA_PSS_Verifier is an implementation of Verifier for RSA-SSA-PSS. 30type RSA_SSA_PSS_Verifier struct { 31 publicKey *rsa.PublicKey 32 hashFunc func() hash.Hash 33 hashID crypto.Hash 34 saltLength int 35} 36 37var _ tink.Verifier = (*RSA_SSA_PSS_Verifier)(nil) 38 39// New_RSA_SSA_PSS_Verifier creates a new instance of RSA_SSA_PSS_Verifier. 40func New_RSA_SSA_PSS_Verifier(hashAlg string, saltLength int, pubKey *rsa.PublicKey) (*RSA_SSA_PSS_Verifier, error) { 41 if err := validRSAPublicKey(pubKey); err != nil { 42 return nil, err 43 } 44 hashFunc, hashID, err := rsaHashFunc(hashAlg) 45 if err != nil { 46 return nil, err 47 } 48 if saltLength < 0 { 49 return nil, fmt.Errorf("invalid salt length") 50 } 51 return &RSA_SSA_PSS_Verifier{ 52 publicKey: pubKey, 53 hashFunc: hashFunc, 54 hashID: hashID, 55 saltLength: saltLength, 56 }, nil 57} 58 59// Verify verifies whether the given signature is valid for the given data. 60// It returns an error if the signature is not valid; nil otherwise. 61func (v *RSA_SSA_PSS_Verifier) Verify(signature, data []byte) error { 62 digest, err := subtle.ComputeHash(v.hashFunc, data) 63 if err != nil { 64 return err 65 } 66 return rsa.VerifyPSS(v.publicKey, v.hashID, digest, signature, &rsa.PSSOptions{SaltLength: v.saltLength}) 67} 68