win_crypto_ng/
lib.rs

1use winapi::shared::ntdef::NTSTATUS;
2use winapi::shared::ntstatus;
3
4use std::fmt;
5use std::num::NonZeroU32;
6
7pub mod asymmetric;
8pub mod buffer;
9pub mod hash;
10pub mod key_blob;
11pub mod property;
12pub mod random;
13pub mod symmetric;
14
15pub mod helpers;
16
17// Compile and test the README
18#[cfg(doctest)]
19doc_comment::doctest!("../README.md");
20
21/// Error type
22///
23/// These errors are a subset of [`NTSTATUS`](https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-erref/596a1078-e883-4972-9bbc-49e60bebca55).
24/// Only the values used by CNG are part of this enum.
25#[derive(Clone, Copy, Debug, PartialOrd, PartialEq)]
26pub enum Error {
27    NotFound,
28    InvalidParameter,
29    InvalidSignature,
30    NoMemory,
31    BufferTooSmall,
32    InvalidHandle,
33    NotSupported,
34    AuthTagMismatch,
35    InvalidBufferSize,
36    Unsuccessful,
37    BadData,
38
39    Unknown(NTSTATUS),
40}
41
42impl fmt::Display for Error {
43    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44        write!(f, "{:?}", self)
45    }
46}
47
48impl std::error::Error for Error {}
49
50impl Error {
51    fn check(status: NTSTATUS) -> crate::Result<()> {
52        match status {
53            ntstatus::STATUS_SUCCESS => Ok(()),
54            ntstatus::STATUS_NOT_FOUND => Err(Error::NotFound),
55            ntstatus::STATUS_INVALID_PARAMETER => Err(Error::InvalidParameter),
56            ntstatus::STATUS_NO_MEMORY | winapi::shared::winerror::NTE_NO_MEMORY => {
57                Err(Error::NoMemory)
58            }
59            ntstatus::STATUS_BUFFER_TOO_SMALL => Err(Error::BufferTooSmall),
60            ntstatus::STATUS_INVALID_HANDLE => Err(Error::InvalidHandle),
61            ntstatus::STATUS_INVALID_SIGNATURE => Err(Error::InvalidSignature),
62            ntstatus::STATUS_NOT_SUPPORTED => Err(Error::NotSupported),
63            ntstatus::STATUS_AUTH_TAG_MISMATCH => Err(Error::AuthTagMismatch),
64            ntstatus::STATUS_INVALID_BUFFER_SIZE => Err(Error::InvalidBufferSize),
65            ntstatus::STATUS_DATA_ERROR | winapi::shared::winerror::NTE_BAD_DATA => {
66                Err(Error::BadData)
67            }
68            ntstatus::STATUS_UNSUCCESSFUL => Err(Error::Unsuccessful),
69            value => Err(Error::Unknown(value)),
70        }
71    }
72}
73
74impl From<Error> for NonZeroU32 {
75    fn from(val: Error) -> Self {
76        let code: i32 = match val {
77            Error::NotFound => ntstatus::STATUS_NOT_FOUND,
78            Error::InvalidParameter => ntstatus::STATUS_INVALID_PARAMETER,
79            Error::BufferTooSmall => ntstatus::STATUS_BUFFER_TOO_SMALL,
80            Error::InvalidHandle => ntstatus::STATUS_INVALID_HANDLE,
81            Error::InvalidSignature => ntstatus::STATUS_INVALID_SIGNATURE,
82            Error::NotSupported => ntstatus::STATUS_NOT_SUPPORTED,
83            Error::AuthTagMismatch => ntstatus::STATUS_AUTH_TAG_MISMATCH,
84            Error::InvalidBufferSize => ntstatus::STATUS_INVALID_BUFFER_SIZE,
85            Error::BadData => ntstatus::STATUS_DATA_ERROR,
86            Error::Unsuccessful => ntstatus::STATUS_UNSUCCESSFUL,
87            Error::NoMemory => ntstatus::STATUS_NO_MEMORY,
88            Error::Unknown(value) => value,
89        };
90
91        NonZeroU32::new(code.unsigned_abs()).expect("Error to not be STATUS_SUCCESS")
92    }
93}
94
95pub type Result<T, E = crate::Error> = std::result::Result<T, E>;