psa_crypto/
lib.rs

1// Copyright 2020 Contributors to the Parsec project.
2// SPDX-License-Identifier: Apache-2.0
3
4//! # PSA Cryptography API Wrapper
5//!
6//! This crate provides abstraction over an implementation of the PSA Cryptography API.
7//! Please check the API
8//! [here](https://developer.arm.com/architectures/security-architectures/platform-security-architecture/documentation)
9//! for a more complete description of operations and types.
10//! This abstraction is built on top of the `psa-crypto-sys` crate.
11
12#![cfg_attr(not(feature = "std"), no_std)]
13#![allow(renamed_and_removed_lints, unknown_lints)]
14#![deny(
15    nonstandard_style,
16    dead_code,
17    improper_ctypes,
18    non_shorthand_field_patterns,
19    no_mangle_generic_items,
20    overflowing_literals,
21    path_statements,
22    patterns_in_fns_without_body,
23    private_bounds,
24    private_in_public,
25    private_interfaces,
26    renamed_and_removed_lints,
27    unconditional_recursion,
28    unnameable_types,
29    unused,
30    unused_allocation,
31    unused_comparisons,
32    unused_parens,
33    while_true,
34    missing_debug_implementations,
35    missing_docs,
36    // The following ling is triggered when casting a reference to a raw pointer.
37    //trivial_casts,
38    trivial_numeric_casts,
39    unused_extern_crates,
40    unused_import_braces,
41    unused_qualifications,
42    unused_results,
43    missing_copy_implementations
44)]
45// This one is hard to avoid.
46#![allow(clippy::multiple_crate_versions)]
47
48#[cfg(feature = "operations")]
49pub mod operations;
50pub mod types;
51
52pub use psa_crypto_sys as ffi;
53
54#[cfg(feature = "operations")]
55use core::sync::atomic::{AtomicBool, Ordering};
56#[cfg(feature = "operations")]
57use types::status::{Error, Result, Status};
58
59#[cfg(feature = "operations")]
60static INITIALISED: AtomicBool = AtomicBool::new(false);
61
62/// Initialize the PSA Crypto library
63///
64/// Applications must call this function before calling any other function in crate.
65/// Applications are permitted to call this function more than once. Once a call succeeds,
66/// subsequent calls are guaranteed to succeed.
67///
68///
69/// # Example
70///
71/// ```rust
72/// use psa_crypto::init;
73/// init().unwrap();
74/// // Can be called twice
75/// init().unwrap();
76/// ```
77#[cfg(feature = "operations")]
78pub fn init() -> Result<()> {
79    // It is not a problem to call psa_crypto_init more than once.
80    Status::from(unsafe { psa_crypto_sys::psa_crypto_init() }).to_result()?;
81    INITIALISED.store(true, Ordering::Relaxed);
82
83    Ok(())
84}
85
86/// Check if the PSA Crypto library has been initialized
87///
88/// Example
89///
90/// ```
91/// use psa_crypto::{initialized, init};
92/// init().unwrap();
93/// initialized().unwrap();
94/// ```
95#[cfg(feature = "operations")]
96pub fn initialized() -> Result<()> {
97    if INITIALISED.load(Ordering::Relaxed) {
98        Ok(())
99    } else {
100        Err(Error::BadState)
101    }
102}