• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2023 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 use crypto_provider::ed25519::{
16     InvalidPublicKeyBytes, RawPrivateKey, RawPrivateKeyPermit, RawPublicKey, RawSignature,
17     SignatureError, SignatureImpl,
18 };
19 
20 pub struct Ed25519;
21 
22 impl crypto_provider::ed25519::Ed25519Provider for Ed25519 {
23     type KeyPair = KeyPair;
24     type PublicKey = PublicKey;
25     type Signature = Signature;
26 }
27 
28 pub struct KeyPair(bssl_crypto::ed25519::PrivateKey);
29 
30 impl crypto_provider::ed25519::KeyPairImpl for KeyPair {
31     type PublicKey = PublicKey;
32     type Signature = Signature;
33 
raw_private_key(&self, _permit: &RawPrivateKeyPermit) -> RawPrivateKey34     fn raw_private_key(&self, _permit: &RawPrivateKeyPermit) -> RawPrivateKey {
35         self.0.to_seed()
36     }
37 
from_raw_private_key(bytes: &RawPrivateKey, _permit: &RawPrivateKeyPermit) -> Self where Self: Sized,38     fn from_raw_private_key(bytes: &RawPrivateKey, _permit: &RawPrivateKeyPermit) -> Self
39     where
40         Self: Sized,
41     {
42         let private_key = bssl_crypto::ed25519::PrivateKey::from_seed(bytes);
43         Self(private_key)
44     }
45 
sign(&self, msg: &[u8]) -> Self::Signature46     fn sign(&self, msg: &[u8]) -> Self::Signature {
47         Signature(self.0.sign(msg))
48     }
49 
generate() -> Self50     fn generate() -> Self {
51         Self(bssl_crypto::ed25519::PrivateKey::generate())
52     }
53 
public_key(&self) -> Self::PublicKey54     fn public_key(&self) -> Self::PublicKey {
55         PublicKey(self.0.to_public())
56     }
57 }
58 
59 pub struct Signature(bssl_crypto::ed25519::Signature);
60 
61 impl crypto_provider::ed25519::SignatureImpl for Signature {
from_bytes(bytes: &RawSignature) -> Self62     fn from_bytes(bytes: &RawSignature) -> Self {
63         Self(bssl_crypto::ed25519::Signature::from(*bytes))
64     }
65 
to_bytes(&self) -> RawSignature66     fn to_bytes(&self) -> RawSignature {
67         self.0
68     }
69 }
70 
71 pub struct PublicKey(bssl_crypto::ed25519::PublicKey);
72 
73 impl crypto_provider::ed25519::PublicKeyImpl for PublicKey {
74     type Signature = Signature;
75 
from_bytes(bytes: &RawPublicKey) -> Result<Self, InvalidPublicKeyBytes> where Self: Sized,76     fn from_bytes(bytes: &RawPublicKey) -> Result<Self, InvalidPublicKeyBytes>
77     where
78         Self: Sized,
79     {
80         Ok(Self(bssl_crypto::ed25519::PublicKey::from_bytes(bytes)))
81     }
82 
to_bytes(&self) -> RawPublicKey83     fn to_bytes(&self) -> RawPublicKey {
84         *self.0.as_bytes()
85     }
86 
verify_strict( &self, message: &[u8], signature: &Self::Signature, ) -> Result<(), SignatureError>87     fn verify_strict(
88         &self,
89         message: &[u8],
90         signature: &Self::Signature,
91     ) -> Result<(), SignatureError> {
92         self.0
93             .verify(message, &bssl_crypto::ed25519::Signature::from(signature.to_bytes()))
94             .map_err(|_| SignatureError)
95     }
96 }
97 
98 #[cfg(test)]
99 mod tests {
100     use super::*;
101     use crypto_provider_test::ed25519::{run_rfc_test_vectors, run_wycheproof_test_vectors};
102 
103     #[test]
wycheproof_test_ed25519_boringssl()104     fn wycheproof_test_ed25519_boringssl() {
105         run_wycheproof_test_vectors::<Ed25519>()
106     }
107 
108     #[test]
rfc_test_ed25519_boringssl()109     fn rfc_test_ed25519_boringssl() {
110         run_rfc_test_vectors::<Ed25519>()
111     }
112 }
113