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 //! Placeholder crate for an unimplemented CP. Can be used to satisfy the trait bounds of 16 //! the uber CryptoProvider trait, when only a subset of the associated types have real implementations 17 //! Can be removed once no one else is depending on it. 18 19 #![allow(unused_variables)] 20 21 use std::fmt::Debug; 22 23 use crypto_provider::aead::AeadInit; 24 use crypto_provider::{ 25 aead::{Aead, AeadError, AesGcm, AesGcmSiv}, 26 aes::{ 27 cbc::{AesCbcIv, AesCbcPkcs7Padded, DecryptionError, EncryptionError}, 28 ctr::{AesCtr, NonceAndCounter}, 29 Aes, Aes128Key, Aes256Key, AesBlock, AesCipher, AesDecryptCipher, AesEncryptCipher, 30 }, 31 ed25519::{ 32 self, Ed25519Provider, InvalidPublicKeyBytes, KeyPairImpl, RawPrivateKey, 33 RawPrivateKeyPermit, RawPublicKey, RawSignature, SignatureError, SignatureImpl, 34 }, 35 elliptic_curve::{EcdhProvider, EphemeralSecret, PublicKey}, 36 hkdf::{Hkdf, InvalidLength}, 37 hmac::{Hmac, MacError}, 38 p256::{P256PublicKey, PointCompression, P256}, 39 tinyvec::{ArrayVec, SliceVec}, 40 x25519::X25519, 41 }; 42 43 #[derive(Default, Clone, Debug, PartialEq, Eq)] 44 pub struct CryptoProviderStubs; 45 46 impl crypto_provider::CryptoProvider for CryptoProviderStubs { 47 type HkdfSha256 = HkdfStubs; 48 type HmacSha256 = HmacStubs; 49 type HkdfSha512 = HkdfStubs; 50 type HmacSha512 = HmacStubs; 51 type AesCbcPkcs7Padded = AesCbcPkcs7PaddedStubs; 52 type X25519 = X25519Stubs; 53 type P256 = P256Stubs; 54 type Sha256 = Sha2Stubs; 55 type Sha512 = Sha2Stubs; 56 type Aes128 = Aes128Impl; 57 type Aes256 = Aes256Impl; 58 type AesCtr128 = Aes128Stubs; 59 type AesCtr256 = Aes256Stubs; 60 type Ed25519 = Ed25519Stubs; 61 type Aes128GcmSiv = Aes128Stubs; 62 type Aes256GcmSiv = Aes256Stubs; 63 type Aes128Gcm = Aes128Stubs; 64 type Aes256Gcm = Aes256Stubs; 65 type CryptoRng = (); 66 constant_time_eq(_a: &[u8], _b: &[u8]) -> bool67 fn constant_time_eq(_a: &[u8], _b: &[u8]) -> bool { 68 unimplemented!() 69 } 70 } 71 72 pub struct Aes128Impl; 73 74 impl Aes for Aes128Impl { 75 type Key = Aes128Key; 76 type EncryptCipher = Aes128Stubs; 77 type DecryptCipher = Aes128Stubs; 78 } 79 80 pub struct Aes256Impl; 81 82 impl Aes for Aes256Impl { 83 type Key = Aes256Key; 84 type EncryptCipher = Aes256Stubs; 85 type DecryptCipher = Aes256Stubs; 86 } 87 88 #[derive(Clone)] 89 pub struct HkdfStubs; 90 91 impl Hkdf for HkdfStubs { new(_salt: Option<&[u8]>, _ikm: &[u8]) -> Self92 fn new(_salt: Option<&[u8]>, _ikm: &[u8]) -> Self { 93 unimplemented!() 94 } 95 expand_multi_info( &self, _info_components: &[&[u8]], _okm: &mut [u8], ) -> Result<(), InvalidLength>96 fn expand_multi_info( 97 &self, 98 _info_components: &[&[u8]], 99 _okm: &mut [u8], 100 ) -> Result<(), InvalidLength> { 101 unimplemented!() 102 } 103 expand(&self, _info: &[u8], _okm: &mut [u8]) -> Result<(), InvalidLength>104 fn expand(&self, _info: &[u8], _okm: &mut [u8]) -> Result<(), InvalidLength> { 105 unimplemented!() 106 } 107 } 108 109 pub struct HmacStubs; 110 111 impl Hmac<32> for HmacStubs { new_from_key(_key: [u8; 32]) -> Self112 fn new_from_key(_key: [u8; 32]) -> Self { 113 unimplemented!() 114 } 115 new_from_slice(_key: &[u8]) -> Result<Self, crypto_provider::hmac::InvalidLength>116 fn new_from_slice(_key: &[u8]) -> Result<Self, crypto_provider::hmac::InvalidLength> { 117 unimplemented!() 118 } 119 update(&mut self, _data: &[u8])120 fn update(&mut self, _data: &[u8]) { 121 unimplemented!() 122 } 123 finalize(self) -> [u8; 32]124 fn finalize(self) -> [u8; 32] { 125 unimplemented!() 126 } 127 verify_slice(self, _tag: &[u8]) -> Result<(), MacError>128 fn verify_slice(self, _tag: &[u8]) -> Result<(), MacError> { 129 unimplemented!() 130 } 131 verify(self, _tag: [u8; 32]) -> Result<(), MacError>132 fn verify(self, _tag: [u8; 32]) -> Result<(), MacError> { 133 unimplemented!() 134 } 135 verify_truncated_left(self, _tag: &[u8]) -> Result<(), MacError>136 fn verify_truncated_left(self, _tag: &[u8]) -> Result<(), MacError> { 137 unimplemented!() 138 } 139 } 140 141 impl Hmac<64> for HmacStubs { new_from_key(_key: [u8; 64]) -> Self142 fn new_from_key(_key: [u8; 64]) -> Self { 143 unimplemented!() 144 } 145 new_from_slice(_key: &[u8]) -> Result<Self, crypto_provider::hmac::InvalidLength>146 fn new_from_slice(_key: &[u8]) -> Result<Self, crypto_provider::hmac::InvalidLength> { 147 unimplemented!() 148 } 149 update(&mut self, _data: &[u8])150 fn update(&mut self, _data: &[u8]) { 151 unimplemented!() 152 } 153 finalize(self) -> [u8; 64]154 fn finalize(self) -> [u8; 64] { 155 unimplemented!() 156 } 157 verify_slice(self, _tag: &[u8]) -> Result<(), MacError>158 fn verify_slice(self, _tag: &[u8]) -> Result<(), MacError> { 159 unimplemented!() 160 } 161 verify(self, _tag: [u8; 64]) -> Result<(), MacError>162 fn verify(self, _tag: [u8; 64]) -> Result<(), MacError> { 163 unimplemented!() 164 } 165 verify_truncated_left(self, _tag: &[u8]) -> Result<(), MacError>166 fn verify_truncated_left(self, _tag: &[u8]) -> Result<(), MacError> { 167 unimplemented!() 168 } 169 } 170 171 pub struct AesCbcPkcs7PaddedStubs; 172 173 impl AesCbcPkcs7Padded for AesCbcPkcs7PaddedStubs { encrypt(_key: &Aes256Key, _iv: &AesCbcIv, _message: &[u8]) -> Vec<u8>174 fn encrypt(_key: &Aes256Key, _iv: &AesCbcIv, _message: &[u8]) -> Vec<u8> { 175 unimplemented!() 176 } 177 encrypt_in_place( key: &Aes256Key, iv: &AesCbcIv, message: &mut SliceVec<u8>, ) -> Result<(), EncryptionError>178 fn encrypt_in_place( 179 key: &Aes256Key, 180 iv: &AesCbcIv, 181 message: &mut SliceVec<u8>, 182 ) -> Result<(), EncryptionError> { 183 unimplemented!() 184 } 185 decrypt( _key: &Aes256Key, _iv: &AesCbcIv, _ciphertext: &[u8], ) -> Result<Vec<u8>, DecryptionError>186 fn decrypt( 187 _key: &Aes256Key, 188 _iv: &AesCbcIv, 189 _ciphertext: &[u8], 190 ) -> Result<Vec<u8>, DecryptionError> { 191 unimplemented!() 192 } 193 decrypt_in_place( key: &Aes256Key, iv: &AesCbcIv, ciphertext: &mut SliceVec<u8>, ) -> Result<(), DecryptionError>194 fn decrypt_in_place( 195 key: &Aes256Key, 196 iv: &AesCbcIv, 197 ciphertext: &mut SliceVec<u8>, 198 ) -> Result<(), DecryptionError> { 199 unimplemented!() 200 } 201 } 202 203 pub struct X25519Stubs; 204 205 impl EcdhProvider<X25519> for X25519Stubs { 206 type PublicKey = EcdhPubKey; 207 type EphemeralSecret = EphSecretStubs; 208 type SharedSecret = [u8; 32]; 209 } 210 211 pub struct EphSecretStubs; 212 213 impl EphemeralSecret<X25519> for EphSecretStubs { 214 type Impl = X25519Stubs; 215 type Error = (); 216 type Rng = (); 217 type EncodedPublicKey = [u8; 32]; 218 generate_random(_rng: &mut Self::Rng) -> Self219 fn generate_random(_rng: &mut Self::Rng) -> Self { 220 unimplemented!() 221 } 222 public_key_bytes(&self) -> Self::EncodedPublicKey223 fn public_key_bytes(&self) -> Self::EncodedPublicKey { 224 unimplemented!() 225 } 226 diffie_hellman( self, _other_pub: &<Self::Impl as EcdhProvider<X25519>>::PublicKey, ) -> Result<<Self::Impl as EcdhProvider<X25519>>::SharedSecret, Self::Error>227 fn diffie_hellman( 228 self, 229 _other_pub: &<Self::Impl as EcdhProvider<X25519>>::PublicKey, 230 ) -> Result<<Self::Impl as EcdhProvider<X25519>>::SharedSecret, Self::Error> { 231 unimplemented!() 232 } 233 } 234 235 impl EphemeralSecret<P256> for EphSecretStubs { 236 type Impl = P256Stubs; 237 type Error = (); 238 type Rng = (); 239 type EncodedPublicKey = ArrayVec<[u8; 65]>; 240 generate_random(_rng: &mut Self::Rng) -> Self241 fn generate_random(_rng: &mut Self::Rng) -> Self { 242 unimplemented!() 243 } 244 public_key_bytes(&self) -> Self::EncodedPublicKey245 fn public_key_bytes(&self) -> Self::EncodedPublicKey { 246 unimplemented!() 247 } 248 diffie_hellman( self, _other_pub: &<Self::Impl as EcdhProvider<P256>>::PublicKey, ) -> Result<<Self::Impl as EcdhProvider<P256>>::SharedSecret, Self::Error>249 fn diffie_hellman( 250 self, 251 _other_pub: &<Self::Impl as EcdhProvider<P256>>::PublicKey, 252 ) -> Result<<Self::Impl as EcdhProvider<P256>>::SharedSecret, Self::Error> { 253 unimplemented!() 254 } 255 } 256 257 #[derive(Debug, PartialEq)] 258 pub struct EcdhPubKey; 259 260 impl PublicKey<X25519> for EcdhPubKey { 261 type Error = (); 262 type EncodedPublicKey = [u8; 32]; 263 from_bytes(_bytes: &[u8]) -> Result<Self, Self::Error>264 fn from_bytes(_bytes: &[u8]) -> Result<Self, Self::Error> { 265 unimplemented!() 266 } 267 to_bytes(&self) -> Self::EncodedPublicKey268 fn to_bytes(&self) -> Self::EncodedPublicKey { 269 unimplemented!() 270 } 271 } 272 273 #[derive(Debug, PartialEq, Eq)] 274 pub struct PublicKeyStubs; 275 276 impl P256PublicKey for PublicKeyStubs { 277 type Error = (); 278 from_sec1_bytes(_bytes: &[u8]) -> Result<Self, Self::Error>279 fn from_sec1_bytes(_bytes: &[u8]) -> Result<Self, Self::Error> { 280 unimplemented!() 281 } 282 to_sec1_bytes(&self, _point_compression: PointCompression) -> ArrayVec<[u8; 65]>283 fn to_sec1_bytes(&self, _point_compression: PointCompression) -> ArrayVec<[u8; 65]> { 284 unimplemented!() 285 } 286 to_affine_coordinates(&self) -> Result<([u8; 32], [u8; 32]), Self::Error>287 fn to_affine_coordinates(&self) -> Result<([u8; 32], [u8; 32]), Self::Error> { 288 unimplemented!() 289 } 290 from_affine_coordinates(_x: &[u8; 32], _y: &[u8; 32]) -> Result<Self, Self::Error>291 fn from_affine_coordinates(_x: &[u8; 32], _y: &[u8; 32]) -> Result<Self, Self::Error> { 292 unimplemented!() 293 } 294 } 295 296 pub struct P256Stubs; 297 298 impl EcdhProvider<P256> for P256Stubs { 299 type PublicKey = PublicKeyStubs; 300 type EphemeralSecret = EphSecretStubs; 301 type SharedSecret = [u8; 32]; 302 } 303 304 pub struct Sha2Stubs; 305 306 impl crypto_provider::sha2::Sha256 for Sha2Stubs { sha256(_input: &[u8]) -> [u8; 32]307 fn sha256(_input: &[u8]) -> [u8; 32] { 308 unimplemented!() 309 } 310 } 311 312 impl crypto_provider::sha2::Sha512 for Sha2Stubs { sha512(_input: &[u8]) -> [u8; 64]313 fn sha512(_input: &[u8]) -> [u8; 64] { 314 unimplemented!() 315 } 316 } 317 318 pub struct Aes128Stubs; 319 320 impl AeadInit<Aes128Key> for Aes128Stubs { new(key: &Aes128Key) -> Self321 fn new(key: &Aes128Key) -> Self { 322 unimplemented!() 323 } 324 } 325 326 impl AesCipher for Aes128Stubs { 327 type Key = Aes128Key; 328 new(key: &Self::Key) -> Self329 fn new(key: &Self::Key) -> Self { 330 unimplemented!() 331 } 332 } 333 334 impl AesDecryptCipher for Aes128Stubs { decrypt(&self, block: &mut AesBlock)335 fn decrypt(&self, block: &mut AesBlock) { 336 unimplemented!() 337 } 338 } 339 340 impl AesEncryptCipher for Aes128Stubs { encrypt(&self, block: &mut AesBlock)341 fn encrypt(&self, block: &mut AesBlock) { 342 unimplemented!() 343 } 344 } 345 346 impl AesCtr for Aes128Stubs { 347 type Key = Aes128Key; 348 new(_key: &Self::Key, _nonce_and_counter: NonceAndCounter) -> Self349 fn new(_key: &Self::Key, _nonce_and_counter: NonceAndCounter) -> Self { 350 unimplemented!() 351 } 352 apply_keystream(&mut self, data: &mut [u8])353 fn apply_keystream(&mut self, data: &mut [u8]) { 354 unimplemented!() 355 } 356 } 357 358 impl Aead for Aes128Stubs { 359 const TAG_SIZE: usize = 16; 360 type Nonce = [u8; 12]; 361 type Tag = [u8; 16]; 362 encrypt(&self, msg: &[u8], aad: &[u8], nonce: &[u8; 12]) -> Result<Vec<u8>, AeadError>363 fn encrypt(&self, msg: &[u8], aad: &[u8], nonce: &[u8; 12]) -> Result<Vec<u8>, AeadError> { 364 unimplemented!() 365 } 366 encrypt_detached( &self, msg: &mut [u8], aad: &[u8], nonce: &Self::Nonce, ) -> Result<Self::Tag, AeadError>367 fn encrypt_detached( 368 &self, 369 msg: &mut [u8], 370 aad: &[u8], 371 nonce: &Self::Nonce, 372 ) -> Result<Self::Tag, AeadError> { 373 unimplemented!() 374 } 375 decrypt(&self, msg: &[u8], aad: &[u8], nonce: &[u8; 12]) -> Result<Vec<u8>, AeadError>376 fn decrypt(&self, msg: &[u8], aad: &[u8], nonce: &[u8; 12]) -> Result<Vec<u8>, AeadError> { 377 unimplemented!() 378 } 379 decrypt_detached( &self, msg: &mut [u8], aad: &[u8], nonce: &Self::Nonce, tag: &Self::Tag, ) -> Result<(), AeadError>380 fn decrypt_detached( 381 &self, 382 msg: &mut [u8], 383 aad: &[u8], 384 nonce: &Self::Nonce, 385 tag: &Self::Tag, 386 ) -> Result<(), AeadError> { 387 unimplemented!() 388 } 389 } 390 391 impl AesGcmSiv for Aes128Stubs {} 392 393 impl AesGcm for Aes128Stubs {} 394 395 pub struct Aes256Stubs; 396 397 impl AeadInit<Aes256Key> for Aes256Stubs { new(key: &Aes256Key) -> Self398 fn new(key: &Aes256Key) -> Self { 399 unimplemented!() 400 } 401 } 402 403 impl AesCipher for Aes256Stubs { 404 type Key = Aes256Key; 405 new(key: &Self::Key) -> Self406 fn new(key: &Self::Key) -> Self { 407 unimplemented!() 408 } 409 } 410 411 impl AesEncryptCipher for Aes256Stubs { encrypt(&self, block: &mut AesBlock)412 fn encrypt(&self, block: &mut AesBlock) { 413 unimplemented!() 414 } 415 } 416 417 impl AesDecryptCipher for Aes256Stubs { decrypt(&self, block: &mut AesBlock)418 fn decrypt(&self, block: &mut AesBlock) { 419 unimplemented!() 420 } 421 } 422 423 impl AesCtr for Aes256Stubs { 424 type Key = Aes256Key; 425 new(_key: &Self::Key, _nonce_and_counter: NonceAndCounter) -> Self426 fn new(_key: &Self::Key, _nonce_and_counter: NonceAndCounter) -> Self { 427 unimplemented!() 428 } 429 apply_keystream(&mut self, data: &mut [u8])430 fn apply_keystream(&mut self, data: &mut [u8]) { 431 unimplemented!() 432 } 433 } 434 435 impl Aead for Aes256Stubs { 436 const TAG_SIZE: usize = 16; 437 type Nonce = [u8; 12]; 438 type Tag = [u8; 16]; 439 encrypt(&self, msg: &[u8], aad: &[u8], nonce: &[u8; 12]) -> Result<Vec<u8>, AeadError>440 fn encrypt(&self, msg: &[u8], aad: &[u8], nonce: &[u8; 12]) -> Result<Vec<u8>, AeadError> { 441 unimplemented!() 442 } 443 encrypt_detached( &self, msg: &mut [u8], aad: &[u8], nonce: &Self::Nonce, ) -> Result<Self::Tag, AeadError>444 fn encrypt_detached( 445 &self, 446 msg: &mut [u8], 447 aad: &[u8], 448 nonce: &Self::Nonce, 449 ) -> Result<Self::Tag, AeadError> { 450 unimplemented!() 451 } 452 decrypt(&self, msg: &[u8], aad: &[u8], nonce: &[u8; 12]) -> Result<Vec<u8>, AeadError>453 fn decrypt(&self, msg: &[u8], aad: &[u8], nonce: &[u8; 12]) -> Result<Vec<u8>, AeadError> { 454 unimplemented!() 455 } 456 decrypt_detached( &self, msg: &mut [u8], aad: &[u8], nonce: &Self::Nonce, tag: &Self::Tag, ) -> Result<(), AeadError>457 fn decrypt_detached( 458 &self, 459 msg: &mut [u8], 460 aad: &[u8], 461 nonce: &Self::Nonce, 462 tag: &Self::Tag, 463 ) -> Result<(), AeadError> { 464 unimplemented!() 465 } 466 } 467 468 impl AesGcmSiv for Aes256Stubs {} 469 470 impl AesGcm for Aes256Stubs {} 471 472 pub struct Ed25519Stubs; 473 474 impl Ed25519Provider for Ed25519Stubs { 475 type KeyPair = KeyPairStubs; 476 type PublicKey = PublicKeyStubs; 477 type Signature = SignatureStubs; 478 } 479 480 impl ed25519::PublicKeyImpl for PublicKeyStubs { 481 type Signature = SignatureStubs; 482 from_bytes(bytes: &RawPublicKey) -> Result<Self, InvalidPublicKeyBytes> where Self: Sized,483 fn from_bytes(bytes: &RawPublicKey) -> Result<Self, InvalidPublicKeyBytes> 484 where 485 Self: Sized, 486 { 487 unimplemented!() 488 } 489 to_bytes(&self) -> RawPublicKey490 fn to_bytes(&self) -> RawPublicKey { 491 unimplemented!() 492 } 493 verify_strict( &self, _message: &[u8], _signature: &Self::Signature, ) -> Result<(), SignatureError>494 fn verify_strict( 495 &self, 496 _message: &[u8], 497 _signature: &Self::Signature, 498 ) -> Result<(), SignatureError> { 499 unimplemented!() 500 } 501 } 502 503 pub struct SignatureStubs; 504 505 impl SignatureImpl for SignatureStubs { from_bytes(_bytes: &RawSignature) -> Self506 fn from_bytes(_bytes: &RawSignature) -> Self { 507 unimplemented!() 508 } 509 to_bytes(&self) -> RawSignature510 fn to_bytes(&self) -> RawSignature { 511 unimplemented!() 512 } 513 } 514 515 pub struct KeyPairStubs; 516 517 impl KeyPairImpl for KeyPairStubs { 518 type PublicKey = PublicKeyStubs; 519 type Signature = SignatureStubs; 520 raw_private_key(&self, _permit: &RawPrivateKeyPermit) -> RawPrivateKey521 fn raw_private_key(&self, _permit: &RawPrivateKeyPermit) -> RawPrivateKey { 522 unimplemented!() 523 } 524 from_raw_private_key(_bytes: &RawPrivateKey, _permit: &RawPrivateKeyPermit) -> Self where Self: Sized,525 fn from_raw_private_key(_bytes: &RawPrivateKey, _permit: &RawPrivateKeyPermit) -> Self 526 where 527 Self: Sized, 528 { 529 unimplemented!() 530 } 531 sign(&self, _msg: &[u8]) -> Self::Signature532 fn sign(&self, _msg: &[u8]) -> Self::Signature { 533 unimplemented!() 534 } 535 generate() -> Self536 fn generate() -> Self { 537 unimplemented!() 538 } 539 public_key(&self) -> Self::PublicKey540 fn public_key(&self) -> Self::PublicKey { 541 unimplemented!() 542 } 543 } 544