irma_signature.go 1.57 KB
Newer Older
1
2
3
4
5
6
7
package irma

import (
	"crypto/sha256"
	"encoding/asn1"
	"log"
	"math/big"
8
9
10

	"github.com/bwesterb/go-atum"
	"github.com/mhe/gabi"
11
12
)

13
// SignedMessage is a message signed with an attribute-based signature
14
// The 'realnonce' will be calculated as: SigRequest.GetNonce() = ASN1(nonce, SHA256(message), timestampSignature)
15
type SignedMessage struct {
16
	Signature gabi.ProofList  `json:"signature"`
17
18
19
	Nonce     *big.Int        `json:"nonce"`
	Context   *big.Int        `json:"context"`
	Message   string          `json:"message"`
20
	Timestamp *atum.Timestamp `json:"timestamp"`
21
22
}

23
24
func (sm *SignedMessage) GetNonce() *big.Int {
	return ASN1ConvertSignatureNonce(sm.Message, sm.Nonce, sm.Timestamp)
25
26
}

27
28
29
30
func (sm *SignedMessage) MatchesNonceAndContext(request *SignatureRequest) bool {
	return sm.Nonce.Cmp(request.Nonce) == 0 &&
		sm.Context.Cmp(request.Context) == 0 &&
		sm.GetNonce().Cmp(request.GetNonce()) == 0
31
32
}

33
34
35
// ASN1ConvertSignatureNonce computes the nonce that is used in the creation of the attribute-based signature:
//    nonce = SHA256(serverNonce, SHA256(message), timestampSignature)
// where serverNonce is the nonce sent by the signature requestor.
36
37
38
39
40
41
42
func ASN1ConvertSignatureNonce(message string, nonce *big.Int, timestamp *atum.Timestamp) *big.Int {
	msgHash := sha256.Sum256([]byte(message))
	tohash := []interface{}{nonce, new(big.Int).SetBytes(msgHash[:])}
	if timestamp != nil {
		tohash = append(tohash, timestamp.Sig.Data)
	}
	asn1bytes, err := asn1.Marshal(tohash)
43
	if err != nil {
44
		log.Print(err) // TODO
45
46
47
48
	}
	asn1hash := sha256.Sum256(asn1bytes)
	return new(big.Int).SetBytes(asn1hash[:])
}