xref: /aosp_15_r20/external/tink/go/signature/subtle/ed25519_signer.go (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1// Copyright 2020 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 subtle
18
19import (
20	"crypto/ed25519"
21)
22
23// ED25519Signer is an implementation of Signer for ED25519.
24type ED25519Signer struct {
25	privateKey *ed25519.PrivateKey
26}
27
28// NewED25519Signer creates a new instance of ED25519Signer.
29func NewED25519Signer(keyValue []byte) (*ED25519Signer, error) {
30	p := ed25519.NewKeyFromSeed(keyValue)
31	return NewED25519SignerFromPrivateKey(&p)
32}
33
34// NewED25519SignerFromPrivateKey creates a new instance of ED25519Signer
35func NewED25519SignerFromPrivateKey(privateKey *ed25519.PrivateKey) (*ED25519Signer, error) {
36	return &ED25519Signer{
37		privateKey: privateKey,
38	}, nil
39}
40
41// Sign computes a signature for the given data.
42func (e *ED25519Signer) Sign(data []byte) ([]byte, error) {
43	r := ed25519.Sign(*e.privateKey, data)
44	if len(r) != ed25519.SignatureSize {
45		return nil, errInvalidED25519Signature
46	}
47	return r, nil
48}
49