Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/vendor/ed25519-dalek/src/verifying.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / vendor / ed25519-dalek / src / verifying.rs` 741 lines · 26310 bytes Raw Blame History 1 // -*- mode: rust; -*- 2 // 3 // This file is part of `ed25519`-dalek. 4 // Copyright (c) 2017-2019 isis lovecruft 5 // See LICENSE for licensing information. 6 // 7 // Authors: 8 // - isis agora lovecruft 9 10 //! `ed25519` public keys. 11 12 use core::fmt::Debug; 13 use core::hash::{Hash, Hasher}; 14 15 use curve25519_dalek::{ 16 digest::{generic_array::typenum::U64, Digest}, 17 edwards::{CompressedEdwardsY, EdwardsPoint}, 18 montgomery::MontgomeryPoint, 19 scalar::Scalar, 20 }; 21 22 use `ed25519`::signature::Verifier; 23 24 use sha2::Sha512; 25 26 #[cfg(feature = "pkcs8")] 27 use `ed25519`::pkcs8; 28 29 #[cfg(feature = "serde")] 30 use serde::{Deserialize, Deserializer, Serialize, Serializer}; 31 32 #[cfg(feature = "digest")] 33 use crate::context::Context; 34 #[cfg(feature = "digest")] 35 use signature::DigestVerifier; 36 37 use crate::{ 38 constants::PUBLIC_KEY_LENGTH, 39 errors::{InternalError, SignatureError}, 40 hazmat::ExpandedSecretKey, 41 signature::InternalSignature, 42 signing::SigningKey, 43 }; 44 45 #[cfg(feature = "hazmat")] 46 mod stream; 47 #[cfg(feature = "hazmat")] 48 pub use self::stream::StreamVerifier; 49 50 /// An `ed25519` public key. 51 /// 52 /// # Note 53 /// 54 /// The `Eq` and `Hash` impls here use the compressed Edwards y encoding, _not_ the algebraic 55 /// representation. This means if this `VerifyingKey` is non-canonically encoded, it will be 56 /// considered unequal to the other equivalent encoding, despite the two representing the same 57 /// point. More encoding details can be found 58 /// [here](https://hdevalence.ca/blog/2020-10-04-its-25519am). 59 /// If you want to make sure that signatures produced with respect to those sorts of public keys 60 /// are rejected, use [`VerifyingKey::verify_strict`]. 61 // Invariant: VerifyingKey.1 is always the decompression of VerifyingKey.0 62 #[derive(Copy, Clone, Default, Eq)] 63 pub struct VerifyingKey { 64 /// Serialized compressed Edwards-y point. 65 pub(crate) compressed: CompressedEdwardsY, 66 67 /// Decompressed Edwards point used for curve arithmetic operations. 68 pub(crate) point: EdwardsPoint, 69 } 70 71 impl Debug for VerifyingKey { 72 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 73 write!(f, "VerifyingKey({:?}), {:?})", self.compressed, self.point) 74 } 75 } 76 77 impl AsRef<[u8]> for VerifyingKey { 78 fn as_ref(&self) -> &[u8] { 79 self.as_bytes() 80 } 81 } 82 83 impl Hash for VerifyingKey { 84 fn hash(&self, state: &mut H) { 85 self.as_bytes().hash(state); 86 } 87 } 88 89 impl PartialEq for VerifyingKey { 90 fn eq(&self, other: &VerifyingKey) -> bool { 91 self.as_bytes() == other.as_bytes() 92 } 93 } 94 95 impl From<&ExpandedSecretKey> for VerifyingKey { 96 /// Derive this public key from its corresponding `ExpandedSecretKey`. 97 fn from(expanded_secret_key: &ExpandedSecretKey) -> VerifyingKey { 98 VerifyingKey::from(EdwardsPoint::mul_base(&expanded_secret_key.scalar)) 99 } 100 } 101 102 impl From<&SigningKey> for VerifyingKey { 103 fn from(signing_key: &SigningKey) -> VerifyingKey { 104 signing_key.verifying_key() 105 } 106 } 107 108 impl From for VerifyingKey { 109 fn from(point: EdwardsPoint) -> VerifyingKey { 110 VerifyingKey { 111 point, 112 compressed: point.compress(), 113 } 114 } 115 } 116 117 impl VerifyingKey { 118 /// Convert this public key to a byte array. 119 #[inline] 120 pub fn to_bytes(&self) -> [u8; PUBLIC_KEY_LENGTH] { 121 self.compressed.to_bytes() 122 } 123 124 /// View this public key as a byte array. 125 #[inline] 126 pub fn as_bytes(&self) -> &[u8; PUBLIC_KEY_LENGTH] { 127 &(self.compressed).0 128 } 129 130 /// Construct a `VerifyingKey` from a slice of bytes. 131 /// 132 /// Verifies the point is valid under [ZIP-215] rules. RFC 8032 / NIST point validation criteria 133 /// are currently unsupported (see [dalek-cryptography/curve25519-dalek#626]). 134 /// 135 /// # Example 136 /// 137 /// ``` 138 /// use `ed25519`_dalek::VerifyingKey; 139 /// use `ed25519`_dalek::PUBLIC_KEY_LENGTH; 140 /// use `ed25519`_dalek::SignatureError; 141 /// 142 /// # fn doctest() -> Result { 143 /// let public_key_bytes: [u8; PUBLIC_KEY_LENGTH] = [ 144 /// 215, 90, 152, 1, 130, 177, 10, 183, 213, 75, 254, 211, 201, 100, 7, 58, 145 /// 14, 225, 114, 243, 218, 166, 35, 37, 175, 2, 26, 104, 247, 7, 81, 26]; 146 /// 147 /// let public_key = VerifyingKey::from_bytes(&public_key_bytes)?; 148 /// # 149 /// # Ok(public_key) 150 /// # } 151 /// # 152 /// # fn main() { 153 /// # doctest(); 154 /// # } 155 /// ``` 156 /// 157 /// # Returns 158 /// 159 /// A `Result` whose okay value is an EdDSA `VerifyingKey` or whose error value 160 /// is a `SignatureError` describing the error that occurred. 161 /// 162 /// [ZIP-215]: https://zips.z.cash/zip-0215 163 /// [dalek-cryptography/curve25519-dalek#626]: https://github.com/dalek-cryptography/curve25519-dalek/issues/626 164 #[inline] 165 pub fn from_bytes(bytes: &[u8; PUBLIC_KEY_LENGTH]) -> Result { 166 let compressed = CompressedEdwardsY(*bytes); 167 let point = compressed 168 .decompress() 169 .ok_or(InternalError::PointDecompression)?; 170 171 // Invariant: VerifyingKey.1 is always the decompression of VerifyingKey.0 172 Ok(VerifyingKey { compressed, point }) 173 } 174 175 /// Create a verifying context that can be used for Ed25519ph with 176 /// [`DigestVerifier`]. 177 #[cfg(feature = "digest")] 178 pub fn with_context<'k, 'v>( 179 &'k self, 180 context_value: &'v [u8], 181 ) -> Result, SignatureError> { 182 Context::new(self, context_value) 183 } 184 185 /// Returns whether this is a _weak_ public key, i.e., if this public key has low order. 186 /// 187 /// A weak public key can be used to generate a signature that's valid for almost every 188 /// message. [`Self::verify_strict`] denies weak keys, but if you want to check for this 189 /// property before verification, then use this method. 190 pub fn is_weak(&self) -> bool { 191 self.point.is_small_order() 192 } 193 194 /// The ordinary non-batched Ed25519 verification check, rejecting non-canonical R values. (see 195 /// [`Self::RCompute`]). `CtxDigest` is the digest used to calculate the pseudorandomness 196 /// needed for signing. According to the spec, `CtxDigest = Sha512`. 197 /// 198 /// This definition is loose in its parameters so that end-users of the `hazmat` module can 199 /// change how the `ExpandedSecretKey` is calculated and which hash function to use. 200 #[allow(non_snake_case)] 201 pub(crate) fn raw_verify( 202 &self, 203 message: &[u8], 204 signature: &`ed25519`::Signature, 205 ) -> Result<(), SignatureError> 206 where 207 CtxDigest: Digest, 208 { 209 let signature = InternalSignature::try_from(signature)?; 210 211 let expected_R = RCompute::::compute(self, signature, None, message); 212 if expected_R == signature.R { 213 Ok(()) 214 } else { 215 Err(InternalError::Verify.into()) 216 } 217 } 218 219 /// The prehashed non-batched Ed25519 verification check, rejecting non-canonical R values. 220 /// (see [`Self::recompute_R`]). `CtxDigest` is the digest used to calculate the 221 /// pseudorandomness needed for signing. `MsgDigest` is the digest used to hash the signed 222 /// message. According to the spec, `MsgDigest = CtxDigest = Sha512`. 223 /// 224 /// This definition is loose in its parameters so that end-users of the `hazmat` module can 225 /// change how the `ExpandedSecretKey` is calculated and which hash function to use. 226 #[cfg(feature = "digest")] 227 #[allow(non_snake_case)] 228 pub(crate) fn raw_verify_prehashed( 229 &self, 230 prehashed_message: MsgDigest, 231 context: Option<&[u8]>, 232 signature: &`ed25519`::Signature, 233 ) -> Result<(), SignatureError> 234 where 235 CtxDigest: Digest, 236 MsgDigest: Digest, 237 { 238 let signature = InternalSignature::try_from(signature)?; 239 240 let ctx: &[u8] = context.unwrap_or(b""); 241 debug_assert!( 242 ctx.len() <= 255, 243 "The context must not be longer than 255 octets." 244 ); 245 246 let message = prehashed_message.finalize(); 247 248 let expected_R = RCompute::::compute(self, signature, Some(ctx), &message); 249 250 if expected_R == signature.R { 251 Ok(()) 252 } else { 253 Err(InternalError::Verify.into()) 254 } 255 } 256 257 /// Verify a `signature` on a `prehashed_message` using the Ed25519ph algorithm. 258 /// 259 /// # Inputs 260 /// 261 /// * `prehashed_message` is an instantiated hash digest with 512-bits of 262 /// output which has had the message to be signed previously fed into its 263 /// state. 264 /// * `context` is an optional context string, up to 255 bytes inclusive, 265 /// which may be used to provide additional domain separation. If not 266 /// set, this will default to an empty string. 267 /// * `signature` is a purported Ed25519ph signature on the `prehashed_message`. 268 /// 269 /// # Returns 270 /// 271 /// Returns `true` if the `signature` was a valid signature created by this 272 /// [`SigningKey`] on the `prehashed_message`. 273 /// 274 /// # Note 275 /// 276 /// The RFC only permits SHA-512 to be used for prehashing, i.e., `MsgDigest = Sha512`. This 277 /// function technically works, and is probably safe to use, with any secure hash function with 278 /// 512-bit digests, but anything outside of SHA-512 is NOT specification-compliant. We expose 279 /// [`crate::Sha512`] for user convenience. 280 #[cfg(feature = "digest")] 281 #[allow(non_snake_case)] 282 pub fn verify_prehashed( 283 &self, 284 prehashed_message: MsgDigest, 285 context: Option<&[u8]>, 286 signature: &`ed25519`::Signature, 287 ) -> Result<(), SignatureError> 288 where 289 MsgDigest: Digest, 290 { 291 self.raw_verify_prehashed::(prehashed_message, context, signature) 292 } 293 294 /// Strictly verify a signature on a message with this keypair's public key. 295 /// 296 /// # On The (Multiple) Sources of Malleability in Ed25519 Signatures 297 /// 298 /// This version of verification is technically non-RFC8032 compliant. The 299 /// following explains why. 300 /// 301 /// 1. Scalar Malleability 302 /// 303 /// The authors of the RFC explicitly stated that verification of an `ed25519` 304 /// signature must fail if the scalar `s` is not properly reduced mod $\ell$: 305 /// 306 /// > To verify a signature on a message M using public key A, with F 307 /// > being 0 for Ed25519ctx, 1 for Ed25519ph, and if Ed25519ctx or 308 /// > Ed25519ph is being used, C being the context, first split the 309 /// > signature into two 32-octet halves. Decode the first half as a 310 /// > point R, and the second half as an integer S, in the range 311 /// > 0 <= s < L. Decode the public key A as point A'. If any of the 312 /// > decodings fail (including S being out of range), the signature is 313 /// > invalid.) 314 /// 315 /// All `verify_*()` functions within `ed25519`-dalek perform this check. 316 /// 317 /// 2. Point malleability 318 /// 319 /// The authors of the RFC added in a malleability check to step #3 in 320 /// §5.1.7, for small torsion components in the `R` value of the signature, 321 /// *which is not strictly required*, as they state: 322 /// 323 /// > Check the group equation \[8\]\[S\]B = \[8\]R + \[8\]\[k\]A'. It's 324 /// > sufficient, but not required, to instead check \[S\]B = R + \[k\]A'. 325 /// 326 /// # History of Malleability Checks 327 /// 328 /// As originally defined (cf. the "Malleability" section in the README of 329 /// this repo), `ed25519` signatures didn't consider *any* form of 330 /// malleability to be an issue. Later the scalar malleability was 331 /// considered important. Still later, particularly with interests in 332 /// cryptocurrency design and in unique identities (e.g. for Signal users, 333 /// Tor onion services, etc.), the group element malleability became a 334 /// concern. 335 /// 336 /// However, libraries had already been created to conform to the original 337 /// definition. One well-used library in particular even implemented the 338 /// group element malleability check, *but only for batch verification*! 339 /// Which meant that even using the same library, a single signature could 340 /// verify fine individually, but suddenly, when verifying it with a bunch 341 /// of other signatures, the whole batch would fail! 342 /// 343 /// # "Strict" Verification 344 /// 345 /// This method performs *both* of the above signature malleability checks. 346 /// 347 /// It must be done as a separate method because one doesn't simply get to 348 /// change the definition of a cryptographic primitive ten years 349 /// after-the-fact with zero consideration for backwards compatibility in 350 /// hardware and protocols which have it already have the older definition 351 /// baked in. 352 /// 353 /// # Return 354 /// 355 /// Returns `Ok(())` if the signature is valid, and `Err` otherwise. 356 #[allow(non_snake_case)] 357 pub fn verify_strict( 358 &self, 359 message: &[u8], 360 signature: &`ed25519`::Signature, 361 ) -> Result<(), SignatureError> { 362 let signature = InternalSignature::try_from(signature)?; 363 364 let signature_R = signature 365 .R 366 .decompress() 367 .ok_or_else(|| SignatureError::from(InternalError::Verify))?; 368 369 // Logical OR is fine here as we're not trying to be constant time. 370 if signature_R.is_small_order() || self.point.is_small_order() { 371 return Err(InternalError::Verify.into()); 372 } 373 374 let expected_R = RCompute::::compute(self, signature, None, message); 375 if expected_R == signature.R { 376 Ok(()) 377 } else { 378 Err(InternalError::Verify.into()) 379 } 380 } 381 382 /// Constructs stream verifier with candidate `signature`. 383 /// 384 /// Useful for cases where the whole message is not available all at once, allowing the 385 /// internal signature state to be updated incrementally and verified at the end. In some cases, 386 /// this will reduce the need for additional allocations. 387 #[cfg(feature = "hazmat")] 388 pub fn verify_stream( 389 &self, 390 signature: &`ed25519`::Signature, 391 ) -> Result { 392 let signature = InternalSignature::try_from(signature)?; 393 Ok(StreamVerifier::new(*self, signature)) 394 } 395 396 /// Verify a `signature` on a `prehashed_message` using the Ed25519ph algorithm, 397 /// using strict signature checking as defined by [`Self::verify_strict`]. 398 /// 399 /// # Inputs 400 /// 401 /// * `prehashed_message` is an instantiated hash digest with 512-bits of 402 /// output which has had the message to be signed previously fed into its 403 /// state. 404 /// * `context` is an optional context string, up to 255 bytes inclusive, 405 /// which may be used to provide additional domain separation. If not 406 /// set, this will default to an empty string. 407 /// * `signature` is a purported Ed25519ph signature on the `prehashed_message`. 408 /// 409 /// # Returns 410 /// 411 /// Returns `true` if the `signature` was a valid signature created by this 412 /// [`SigningKey`] on the `prehashed_message`. 413 /// 414 /// # Note 415 /// 416 /// The RFC only permits SHA-512 to be used for prehashing, i.e., `MsgDigest = Sha512`. This 417 /// function technically works, and is probably safe to use, with any secure hash function with 418 /// 512-bit digests, but anything outside of SHA-512 is NOT specification-compliant. We expose 419 /// [`crate::Sha512`] for user convenience. 420 #[cfg(feature = "digest")] 421 #[allow(non_snake_case)] 422 pub fn verify_prehashed_strict( 423 &self, 424 prehashed_message: MsgDigest, 425 context: Option<&[u8]>, 426 signature: &`ed25519`::Signature, 427 ) -> Result<(), SignatureError> 428 where 429 MsgDigest: Digest, 430 { 431 let signature = InternalSignature::try_from(signature)?; 432 433 let ctx: &[u8] = context.unwrap_or(b""); 434 debug_assert!( 435 ctx.len() <= 255, 436 "The context must not be longer than 255 octets." 437 ); 438 439 let signature_R = signature 440 .R 441 .decompress() 442 .ok_or_else(|| SignatureError::from(InternalError::Verify))?; 443 444 // Logical OR is fine here as we're not trying to be constant time. 445 if signature_R.is_small_order() || self.point.is_small_order() { 446 return Err(InternalError::Verify.into()); 447 } 448 449 let message = prehashed_message.finalize(); 450 let expected_R = RCompute::::compute(self, signature, Some(ctx), &message); 451 452 if expected_R == signature.R { 453 Ok(()) 454 } else { 455 Err(InternalError::Verify.into()) 456 } 457 } 458 459 /// Convert this verifying key into Montgomery form. 460 /// 461 /// This can be used for performing X25519 Diffie-Hellman using Ed25519 keys. The output of 462 /// this function is a valid X25519 public key whose secret key is `sk.to_scalar_bytes()`, 463 /// where `sk` is a valid signing key for this `VerifyingKey`. 464 /// 465 /// # Note 466 /// 467 /// We do NOT recommend this usage of a signing/verifying key. Signing keys are usually 468 /// long-term keys, while keys used for key exchange should rather be ephemeral. If you can 469 /// help it, use a separate key for encryption. 470 /// 471 /// For more information on the security of systems which use the same keys for both signing 472 /// and Diffie-Hellman, see the paper 473 /// [On using the same key pair for Ed25519 and an X25519 based KEM](https://eprint.iacr.org/2021/509). 474 pub fn to_montgomery(&self) -> MontgomeryPoint { 475 self.point.to_montgomery() 476 } 477 478 /// Return this verifying key in Edwards form. 479 pub fn to_edwards(&self) -> EdwardsPoint { 480 self.point 481 } 482 } 483 484 /// Helper for verification. Computes the _expected_ R component of the signature. The 485 /// caller compares this to the real R component. 486 /// This computes `H(R || A || M)` where `H` is the 512-bit hash function 487 /// given by `CtxDigest` (this is SHA-512 in spec-compliant Ed25519). 488 /// 489 /// For pre-hashed variants a `h` with the context already included can be provided. 490 /// Note that this returns the compressed form of R and the caller does a byte comparison. This 491 /// means that all our verification functions do not accept non-canonically encoded R values. 492 /// See the validation criteria blog post for more details: 493 /// https://hdevalence.ca/blog/2020-10-04-its-25519am 494 pub(crate) struct RCompute { 495 key: VerifyingKey, 496 signature: InternalSignature, 497 h: CtxDigest, 498 } 499 500 #[allow(non_snake_case)] 501 impl RCompute 502 where 503 CtxDigest: Digest, 504 { 505 /// If `prehash_ctx.is_some()`, this does the prehashed variant of the computation using its 506 /// contents. 507 pub(crate) fn compute( 508 key: &VerifyingKey, 509 signature: InternalSignature, 510 prehash_ctx: Option<&[u8]>, 511 message: &[u8], 512 ) -> CompressedEdwardsY { 513 let mut c = Self::new(key, signature, prehash_ctx); 514 c.update(message); 515 c.finish() 516 } 517 518 pub(crate) fn new( 519 key: &VerifyingKey, 520 signature: InternalSignature, 521 prehash_ctx: Option<&[u8]>, 522 ) -> Self { 523 let R = &signature.R; 524 let A = &key.compressed; 525 526 let mut h = CtxDigest::new(); 527 if let Some(c) = prehash_ctx { 528 h.update(b"SigEd25519 no Ed25519 collisions"); 529 h.update([1]); // Ed25519ph 530 h.update([c.len() as u8]); 531 h.update(c); 532 } 533 534 h.update(R.as_bytes()); 535 h.update(A.as_bytes()); 536 Self { 537 key: *key, 538 signature, 539 h, 540 } 541 } 542 543 pub(crate) fn update(&mut self, m: &[u8]) { 544 self.h.update(m) 545 } 546 547 pub(crate) fn finish(self) -> CompressedEdwardsY { 548 let k = Scalar::from_hash(self.h); 549 550 let minus_A: EdwardsPoint = -self.key.point; 551 // Recall the (non-batched) verification equation: -[k]A + [s]B = R 552 EdwardsPoint::vartime_double_scalar_mul_basepoint(&k, &(minus_A), &self.signature.s) 553 .compress() 554 } 555 } 556 557 impl Verifier<`ed25519`::Signature> for VerifyingKey { 558 /// Verify a signature on a message with this keypair's public key. 559 /// 560 /// # Return 561 /// 562 /// Returns `Ok(())` if the signature is valid, and `Err` otherwise. 563 fn verify(&self, message: &[u8], signature: &`ed25519`::Signature) -> Result<(), SignatureError> { 564 self.raw_verify::(message, signature) 565 } 566 } 567 568 /// Equivalent to [`VerifyingKey::verify_prehashed`] with `context` set to [`None`]. 569 #[cfg(feature = "digest")] 570 impl DigestVerifier for VerifyingKey 571 where 572 MsgDigest: Digest, 573 { 574 fn verify_digest( 575 &self, 576 msg_digest: MsgDigest, 577 signature: &`ed25519`::Signature, 578 ) -> Result<(), SignatureError> { 579 self.verify_prehashed(msg_digest, None, signature) 580 } 581 } 582 583 /// Equivalent to [`VerifyingKey::verify_prehashed`] with `context` set to [`Some`] 584 /// containing `self.value()`. 585 #[cfg(feature = "digest")] 586 impl DigestVerifier for Context<'_, '_, VerifyingKey> 587 where 588 MsgDigest: Digest, 589 { 590 fn verify_digest( 591 &self, 592 msg_digest: MsgDigest, 593 signature: &`ed25519`::Signature, 594 ) -> Result<(), SignatureError> { 595 self.key() 596 .verify_prehashed(msg_digest, Some(self.value()), signature) 597 } 598 } 599 600 impl TryFrom<&[u8]> for VerifyingKey { 601 type Error = SignatureError; 602 603 #[inline] 604 fn try_from(bytes: &[u8]) -> Result { 605 let bytes = bytes.try_into().map_err(|_| InternalError::BytesLength { 606 name: "VerifyingKey", 607 length: PUBLIC_KEY_LENGTH, 608 })?; 609 Self::from_bytes(bytes) 610 } 611 } 612 613 impl From for EdwardsPoint { 614 fn from(vk: VerifyingKey) -> EdwardsPoint { 615 vk.point 616 } 617 } 618 619 #[cfg(all(feature = "alloc", feature = "pkcs8"))] 620 impl pkcs8::EncodePublicKey for VerifyingKey { 621 fn to_public_key_der(&self) -> pkcs8::spki::Result { 622 pkcs8::PublicKeyBytes::from(self).to_public_key_der() 623 } 624 } 625 626 #[cfg(all(feature = "alloc", feature = "pkcs8"))] 627 impl pkcs8::spki::DynSignatureAlgorithmIdentifier for VerifyingKey { 628 fn signature_algorithm_identifier( 629 &self, 630 ) -> pkcs8::spki::Result { 631 // From https://datatracker.ietf.org/doc/html/rfc8410 632 // `id-Ed25519 OBJECT IDENTIFIER ::= { 1 3 101 112 }` 633 Ok(`ed25519`::pkcs8::spki::AlgorithmIdentifierOwned { 634 oid: `ed25519`::pkcs8::ALGORITHM_OID, 635 parameters: None, 636 }) 637 } 638 } 639 640 #[cfg(feature = "pkcs8")] 641 impl TryFrom for VerifyingKey { 642 type Error = pkcs8::spki::Error; 643 644 fn try_from(pkcs8_key: pkcs8::PublicKeyBytes) -> pkcs8::spki::Result { 645 VerifyingKey::try_from(&pkcs8_key) 646 } 647 } 648 649 #[cfg(feature = "pkcs8")] 650 impl TryFrom<&pkcs8::PublicKeyBytes> for VerifyingKey { 651 type Error = pkcs8::spki::Error; 652 653 fn try_from(pkcs8_key: &pkcs8::PublicKeyBytes) -> pkcs8::spki::Result { 654 VerifyingKey::from_bytes(pkcs8_key.as_ref()).map_err(|_| pkcs8::spki::Error::KeyMalformed) 655 } 656 } 657 658 #[cfg(feature = "pkcs8")] 659 impl From for pkcs8::PublicKeyBytes { 660 fn from(verifying_key: VerifyingKey) -> pkcs8::PublicKeyBytes { 661 pkcs8::PublicKeyBytes::from(&verifying_key) 662 } 663 } 664 665 #[cfg(feature = "pkcs8")] 666 impl From<&VerifyingKey> for pkcs8::PublicKeyBytes { 667 fn from(verifying_key: &VerifyingKey) -> pkcs8::PublicKeyBytes { 668 pkcs8::PublicKeyBytes(verifying_key.to_bytes()) 669 } 670 } 671 672 #[cfg(feature = "pkcs8")] 673 impl TryFrom> for VerifyingKey { 674 type Error = pkcs8::spki::Error; 675 676 fn try_from(public_key: pkcs8::spki::SubjectPublicKeyInfoRef<'_>) -> pkcs8::spki::Result { 677 pkcs8::PublicKeyBytes::try_from(public_key)?.try_into() 678 } 679 } 680 681 #[cfg(feature = "serde")] 682 impl Serialize for VerifyingKey { 683 fn serialize(&self, serializer: S) -> Result 684 where 685 S: Serializer, 686 { 687 serializer.serialize_bytes(&self.as_bytes()[..]) 688 } 689 } 690 691 #[cfg(feature = "serde")] 692 impl<'d> Deserialize<'d> for VerifyingKey { 693 fn deserialize(deserializer: D) -> Result 694 where 695 D: Deserializer<'d>, 696 { 697 struct VerifyingKeyVisitor; 698 699 impl<'de> serde::de::Visitor<'de> for VerifyingKeyVisitor { 700 type Value = VerifyingKey; 701 702 fn expecting(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 703 write!(formatter, concat!("An `ed25519` verifying (public) key")) 704 } 705 706 fn visit_bytes(self, bytes: &[u8]) -> Result { 707 VerifyingKey::try_from(bytes).map_err(E::custom) 708 } 709 710 fn visit_seq(self, mut seq: A) -> Result 711 where 712 A: serde::de::SeqAccess<'de>, 713 { 714 let mut bytes = [0u8; 32]; 715 716 #[allow(clippy::needless_range_loop)] 717 for i in 0..32 { 718 bytes[i] = seq 719 .next_element()? 720 .ok_or_else(|| serde::de::Error::invalid_length(i, &"expected 32 bytes"))?; 721 } 722 723 let remaining = (0..) 724 .map(|_| seq.next_element::()) 725 .take_while(|el| matches!(el, Ok(Some(_)))) 726 .count(); 727 728 if remaining > 0 { 729 return Err(serde::de::Error::invalid_length( 730 32 + remaining, 731 &"expected 32 bytes", 732 )); 733 } 734 735 VerifyingKey::try_from(&bytes[..]).map_err(serde::de::Error::custom) 736 } 737 } 738 739 deserializer.deserialize_bytes(VerifyingKeyVisitor) 740 } 741 }