pub struct Pka<'d, T: Instance> { /* private fields */ }Expand description
PKA driver
Implementations§
Source§impl<'d, T: Instance> Pka<'d, T>
impl<'d, T: Instance> Pka<'d, T>
Sourcepub fn new_blocking(
peripheral: Peri<'d, T>,
_irq: impl Binding<T::Interrupt, InterruptHandler<T>> + 'd,
) -> Self
pub fn new_blocking( peripheral: Peri<'d, T>, _irq: impl Binding<T::Interrupt, InterruptHandler<T>> + 'd, ) -> Self
Create a new PKA driver
Sourcepub fn ecdsa_verify(
&mut self,
curve: &EcdsaCurveParams,
public_key: &EcdsaPublicKey<'_>,
signature: &EcdsaSignature<'_>,
message_hash: &[u8],
) -> Result<bool, Error>
pub fn ecdsa_verify( &mut self, curve: &EcdsaCurveParams, public_key: &EcdsaPublicKey<'_>, signature: &EcdsaSignature<'_>, message_hash: &[u8], ) -> Result<bool, Error>
Verify an ECDSA signature
Returns Ok(true) if signature is valid, Ok(false) if invalid.
Sourcepub fn ecdsa_sign(
&mut self,
curve: &EcdsaCurveParams,
private_key: &[u8],
k: &[u8],
message_hash: &[u8],
signature_r: &mut [u8],
signature_s: &mut [u8],
) -> Result<(), Error>
pub fn ecdsa_sign( &mut self, curve: &EcdsaCurveParams, private_key: &[u8], k: &[u8], message_hash: &[u8], signature_r: &mut [u8], signature_s: &mut [u8], ) -> Result<(), Error>
Generate an ECDSA signature
§Arguments
curve- Curve parametersprivate_key- Private key dk- Random nonce (MUST be cryptographically random and unique per signature!)message_hash- Hash of the message to sign
§Returns
Signature (r, s) as byte arrays
§Security Warning
The k value MUST be:
- Cryptographically random
- Unique for every signature
- Never reused or predictable Failure to ensure this will compromise the private key!
Sourcepub fn ecc_mul(
&mut self,
curve: &EcdsaCurveParams,
k: &[u8],
point_x: &[u8],
point_y: &[u8],
result: &mut EccPoint,
) -> Result<(), Error>
pub fn ecc_mul( &mut self, curve: &EcdsaCurveParams, k: &[u8], point_x: &[u8], point_y: &[u8], result: &mut EccPoint, ) -> Result<(), Error>
Perform ECC scalar multiplication: result = k * P
This is the core operation for ECDH key agreement:
- To generate public key: public = private_key * G (generator point)
- To compute shared secret: shared = my_private * peer_public
§Arguments
curve- Curve parametersk- Scalar multiplierpoint_x- Input point X coordinatepoint_y- Input point Y coordinateresult- Output point (must be initialized with correct size)
Sourcepub fn point_check(
&mut self,
curve: &EcdsaCurveParams,
point_x: &[u8],
point_y: &[u8],
) -> Result<bool, Error>
pub fn point_check( &mut self, curve: &EcdsaCurveParams, point_x: &[u8], point_y: &[u8], ) -> Result<bool, Error>
Check if a point is on the curve
This should be called to validate any externally-provided public key before using it in cryptographic operations.
Sourcepub fn modular_exp(
&mut self,
base: &[u8],
exponent: &[u8],
modulus: &[u8],
result: &mut [u8],
) -> Result<(), Error>
pub fn modular_exp( &mut self, base: &[u8], exponent: &[u8], modulus: &[u8], result: &mut [u8], ) -> Result<(), Error>
Perform modular exponentiation: result = base^exp mod n
This is the core RSA operation:
- Encryption: ciphertext = plaintext^e mod n
- Decryption: plaintext = ciphertext^d mod n
- Signing: signature = hash^d mod n
- Verification: hash = signature^e mod n
§Arguments
base- Base value (plaintext/ciphertext)exponent- Exponent (e for encrypt/verify, d for decrypt/sign)modulus- RSA modulus nresult- Output buffer (must be same size as modulus)
Sourcepub fn rsa_crt_exp(
&mut self,
ciphertext: &[u8],
params: &RsaCrtParams<'_>,
result: &mut [u8],
) -> Result<(), Error>
pub fn rsa_crt_exp( &mut self, ciphertext: &[u8], params: &RsaCrtParams<'_>, result: &mut [u8], ) -> Result<(), Error>
Perform RSA CRT exponentiation for fast decryption
Uses Chinese Remainder Theorem for ~4x faster RSA private key operations.
§Arguments
ciphertext- Encrypted dataparams- CRT parameters (p, q, dp, dq, qinv)result- Output buffer
Sourcepub fn modular_inv(
&mut self,
a: &[u8],
modulus: &[u8],
result: &mut [u8],
) -> Result<(), Error>
pub fn modular_inv( &mut self, a: &[u8], modulus: &[u8], result: &mut [u8], ) -> Result<(), Error>
Compute modular inverse: result = a^(-1) mod n
Sourcepub fn modular_add(
&mut self,
a: &[u8],
b: &[u8],
modulus: &[u8],
result: &mut [u8],
) -> Result<(), Error>
pub fn modular_add( &mut self, a: &[u8], b: &[u8], modulus: &[u8], result: &mut [u8], ) -> Result<(), Error>
Compute modular addition: result = (a + b) mod n
Sourcepub fn modular_sub(
&mut self,
a: &[u8],
b: &[u8],
modulus: &[u8],
result: &mut [u8],
) -> Result<(), Error>
pub fn modular_sub( &mut self, a: &[u8], b: &[u8], modulus: &[u8], result: &mut [u8], ) -> Result<(), Error>
Compute modular subtraction: result = (a - b) mod n
Sourcepub fn arithmetic_mul(
&mut self,
a: &[u8],
b: &[u8],
result: &mut [u8],
) -> Result<(), Error>
pub fn arithmetic_mul( &mut self, a: &[u8], b: &[u8], result: &mut [u8], ) -> Result<(), Error>
Compute arithmetic multiplication: result = a * b
Sourcepub fn montgomery_param(
&mut self,
modulus: &[u8],
result: &mut [u32],
) -> Result<(), Error>
pub fn montgomery_param( &mut self, modulus: &[u8], result: &mut [u32], ) -> Result<(), Error>
Compute Montgomery parameter R² mod n
This parameter is required for fast modular exponentiation and other Montgomery-form operations. The result should be stored and reused for multiple operations with the same modulus.
§Arguments
modulus- The modulus nresult- Output buffer for R² mod n (must be same size as modulus)
Sourcepub fn modular_exp_fast(
&mut self,
base: &[u8],
exponent: &[u8],
modulus: &[u8],
montgomery_param: &[u32],
result: &mut [u8],
) -> Result<(), Error>
pub fn modular_exp_fast( &mut self, base: &[u8], exponent: &[u8], modulus: &[u8], montgomery_param: &[u32], result: &mut [u8], ) -> Result<(), Error>
Perform modular exponentiation with pre-computed Montgomery parameter (fast mode)
This is faster than modular_exp when you have the Montgomery parameter
pre-computed (via montgomery_param).
§Arguments
base- Base valueexponent- Exponentmodulus- Modulus nmontgomery_param- Pre-computed Montgomery parameter R² mod nresult- Output buffer (must be same size as modulus)
Sourcepub fn modular_exp_protect(
&mut self,
params: &ModExpProtectParams<'_>,
result: &mut [u8],
) -> Result<(), Error>
pub fn modular_exp_protect( &mut self, params: &ModExpProtectParams<'_>, result: &mut [u8], ) -> Result<(), Error>
Perform modular exponentiation with side-channel protection
This mode provides constant-time execution to protect against timing and power analysis attacks. It requires phi(n) as input.
§Arguments
params- Protected mode parameters including phi(n)result- Output buffer (must be same size as modulus)
Sourcepub fn montgomery_mul(
&mut self,
a: &[u8],
b: &[u8],
modulus: &[u8],
result: &mut [u8],
) -> Result<(), Error>
pub fn montgomery_mul( &mut self, a: &[u8], b: &[u8], modulus: &[u8], result: &mut [u8], ) -> Result<(), Error>
Perform Montgomery multiplication: result = (a * b * R^-1) mod n
This is useful for operations in Montgomery form.
Sourcepub fn arithmetic_add(
&mut self,
a: &[u8],
b: &[u8],
result: &mut [u8],
) -> Result<(), Error>
pub fn arithmetic_add( &mut self, a: &[u8], b: &[u8], result: &mut [u8], ) -> Result<(), Error>
Compute arithmetic addition: result = a + b
Note: Result may be one word larger than inputs if there’s overflow.
Sourcepub fn arithmetic_sub(
&mut self,
a: &[u8],
b: &[u8],
result: &mut [u8],
) -> Result<(), Error>
pub fn arithmetic_sub( &mut self, a: &[u8], b: &[u8], result: &mut [u8], ) -> Result<(), Error>
Compute arithmetic subtraction: result = a - b
Note: If a < b, the result will be the two’s complement.
Sourcepub fn comparison(
&mut self,
a: &[u8],
b: &[u8],
) -> Result<ComparisonResult, Error>
pub fn comparison( &mut self, a: &[u8], b: &[u8], ) -> Result<ComparisonResult, Error>
Compare two big integers
Returns the comparison result indicating whether a < b, a == b, or a > b.
Sourcepub fn modular_red(
&mut self,
a: &[u8],
modulus: &[u8],
result: &mut [u8],
) -> Result<(), Error>
pub fn modular_red( &mut self, a: &[u8], modulus: &[u8], result: &mut [u8], ) -> Result<(), Error>
Compute modular reduction: result = a mod n
Sourcepub fn ecc_complete_add(
&mut self,
curve: &EcdsaCurveParams,
p: &EccProjectivePoint,
q: &EccProjectivePoint,
result: &mut EccProjectivePoint,
) -> Result<(), Error>
pub fn ecc_complete_add( &mut self, curve: &EcdsaCurveParams, p: &EccProjectivePoint, q: &EccProjectivePoint, result: &mut EccProjectivePoint, ) -> Result<(), Error>
ECC complete point addition in projective coordinates: R = P + Q
This operation adds two points in projective coordinates and handles all edge cases (point at infinity, point doubling, etc.).
§Arguments
curve- Curve parametersp- First point in projective coordinatesq- Second point in projective coordinatesresult- Output point in projective coordinates
Sourcepub fn double_base_ladder(
&mut self,
curve: &EcdsaCurveParams,
k: &[u8],
p: &EccProjectivePoint,
m: &[u8],
q: &EccProjectivePoint,
result: &mut EccPoint,
) -> Result<(), Error>
pub fn double_base_ladder( &mut self, curve: &EcdsaCurveParams, k: &[u8], p: &EccProjectivePoint, m: &[u8], q: &EccProjectivePoint, result: &mut EccPoint, ) -> Result<(), Error>
ECC double base ladder: result = kP + mQ (side-channel resistant)
This operation computes the linear combination of two points using the double base ladder algorithm, which provides side-channel resistance.
§Arguments
curve- Curve parametersk- Scalar for point Pp- First point in projective coordinatesm- Scalar for point Qq- Second point in projective coordinatesresult- Output point in affine coordinates
Sourcepub fn projective_to_affine(
&mut self,
modulus: &[u8],
montgomery_param: &[u32],
point: &EccProjectivePoint,
result: &mut EccPoint,
) -> Result<(), Error>
pub fn projective_to_affine( &mut self, modulus: &[u8], montgomery_param: &[u32], point: &EccProjectivePoint, result: &mut EccPoint, ) -> Result<(), Error>
Convert a point from projective to affine coordinates
§Arguments
modulus- The curve modulus pmontgomery_param- Pre-computed Montgomery parameter R² mod ppoint- Point in projective coordinatesresult- Output point in affine coordinates