1 // Copyright 2020, The Android Open Source Project 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 //! This module implements Error for the keystore2_crypto library. 16 use crate::zvec; 17 18 /// Crypto specific error codes. 19 #[derive(Debug, thiserror::Error, Eq, PartialEq)] 20 pub enum Error { 21 /// This is returned if the C/C++ implementation of AES_gcm_decrypt returned false. 22 #[error("Failed to decrypt.")] 23 DecryptionFailed, 24 25 /// This is returned if the C/C++ implementation of AES_gcm_encrypt returned false. 26 #[error("Failed to encrypt.")] 27 EncryptionFailed, 28 29 /// The initialization vector has the wrong length. 30 #[error("Invalid IV length.")] 31 InvalidIvLength, 32 33 /// The aead tag has the wrong length. 34 #[error("Invalid AEAD tag length.")] 35 InvalidAeadTagLength, 36 37 /// The key has the wrong length. 38 #[error("Invalid key length.")] 39 InvalidKeyLength, 40 41 /// Invalid data length. 42 #[error("Invalid data length.")] 43 InvalidDataLength, 44 45 /// Invalid salt length. 46 #[error("Invalid salt length.")] 47 InvalidSaltLength, 48 49 /// Random number generation failed. 50 #[error("Random number generation failed.")] 51 RandomNumberGenerationFailed, 52 53 /// ZVec construction failed. 54 #[error(transparent)] 55 LayoutError(#[from] std::alloc::LayoutErr), 56 57 /// Nix error. 58 #[error(transparent)] 59 NixError(#[from] nix::Error), 60 61 /// This is returned if the C implementation of HKDFExtract returned false 62 /// or otherwise failed. 63 #[error("Failed to extract.")] 64 HKDFExtractFailed, 65 66 /// This is returned if the C implementation of HKDFExpand returned false. 67 #[error("Failed to expand.")] 68 HKDFExpandFailed, 69 70 /// This is returned if the C implementation of ECDHComputeKey returned -1. 71 #[error("Failed to compute ecdh key.")] 72 ECDHComputeKeyFailed, 73 74 /// This is returned if the C implementation of ECKEYGenerateKey returned null. 75 #[error("Failed to generate key.")] 76 ECKEYGenerateKeyFailed, 77 78 /// This is returned if the C implementation of ECKEYMarshalPrivateKey returned 0. 79 #[error("Failed to marshal private key.")] 80 ECKEYMarshalPrivateKeyFailed, 81 82 /// This is returned if the C implementation of ECKEYParsePrivateKey returned null. 83 #[error("Failed to parse private key.")] 84 ECKEYParsePrivateKeyFailed, 85 86 /// This is returned if the C implementation of ECPOINTPoint2Oct returned 0. 87 #[error("Failed to convert point to oct.")] 88 ECPoint2OctFailed, 89 90 /// This is returned if the C implementation of ECPOINTOct2Point returned null. 91 #[error("Failed to convert oct to point.")] 92 ECOct2PointFailed, 93 94 /// This is returned if the C implementation of extractSubjectFromCertificate failed. 95 #[error("Failed to extract certificate subject.")] 96 ExtractSubjectFailed, 97 98 /// This is returned if the C implementation of hmacSha256 failed. 99 #[error("Failed to calculate HMAC-SHA256.")] 100 HmacSha256Failed, 101 102 /// Zvec error. 103 #[error(transparent)] 104 ZVec(#[from] zvec::Error), 105 } 106