| /** |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, |
| * software distributed under the License is distributed on an |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| * KIND, either express or implied. See the License for the |
| * specific language governing permissions and limitations |
| * under the License. |
| */ |
| |
| package sec |
| |
| import ( |
| "bytes" |
| "crypto" |
| "crypto/rsa" |
| |
| "github.com/apache/mynewt-artifact/errors" |
| ) |
| |
| type Sig struct { |
| KeyHash []byte |
| Data []byte |
| } |
| |
| func checkOneKeyOneSig(k PubSignKey, sig Sig, hash []byte) (bool, error) { |
| pubBytes, err := k.Bytes() |
| if err != nil { |
| return false, errors.WithStack(err) |
| } |
| keyHash := RawKeyHash(pubBytes) |
| |
| if !bytes.Equal(keyHash, sig.KeyHash) { |
| return false, nil |
| } |
| |
| if k.Rsa != nil { |
| opts := rsa.PSSOptions{ |
| SaltLength: rsa.PSSSaltLengthEqualsHash, |
| } |
| err := rsa.VerifyPSS(k.Rsa, crypto.SHA256, hash, sig.Data, &opts) |
| return err == nil, nil |
| } |
| |
| if k.Ec != nil { |
| return false, errors.Errorf( |
| "ecdsa signature verification not supported") |
| } |
| |
| return false, nil |
| } |
| |
| func VerifySigs(key PubSignKey, sigs []Sig, hash []byte) (int, error) { |
| for i, s := range sigs { |
| match, err := checkOneKeyOneSig(key, s, hash) |
| if err != nil { |
| return -1, err |
| } |
| if match { |
| return i, nil |
| } |
| } |
| |
| return -1, nil |
| } |