64 Commits

Author SHA1 Message Date
Arthur Gautier
7803ce534e
pkcs1v15: expose RsaSignatureAssociatedOid (#392)
This allows reuse of the `RsaSignatureAssociatedOid` trait to pull
implementation of `SignatureAlgorithmIdentifier` in other crates (like
`yubihsm.rs`).
2023-11-29 14:55:44 -07:00
Tony Arcieri
94856ec765
pkcs1v15: note RSASSA-PKCS1-v1_5 in docs (#324)
This is the name for PKCS#1v1.5 signatures originally specified in
RFC3447 but is also used by RFC8017 (which we cite)
2023-05-03 17:13:52 -06:00
Tony Arcieri
f5918ad3bf
Refactor padding modes into submodules (#312)
The padding mode modules have gotten quite large.

This commit refactors types into respective submodules, with the
toplevel module defining the same-named padding schemes.
2023-04-27 07:58:19 -06:00
Tony Arcieri
d9968bc0c9
Refactor traits (#315)
There were several modules that defined traits, including one called
`traits`.

This consolidates all of them under `traits`, retaining the previous
module structure as internal submodules:

- `keytraits` => `traits::keys`
- `padding` => `traits::padding`
- `traits` => `traits::encryption`

Additionally this removes the traits that were re-exported at the
toplevel, instead re-exporting them all under `traits`.
2023-04-27 07:39:37 -06:00
Tony Arcieri
78ea9cb7da
Impl ZeroizeOnDrop for RsaPrivateKey+newtypes (#311)
`RsaPrivateKey` self-zeroizes on drop, so add the `ZeroizeOnDrop` marker
trait to `RsaPrivateKey` and all newtypes thereof, i.e. `DecryptingKey`
and `SigningKey` for the various padding modes.

This also removes the `Zeroize` impl on `RsaPrivateKey`, since it
self-zeroizes on `Drop`, and allowing `Zeroize` might accidentally
permit use-after-zeroize vulnerabilities.
2023-04-26 10:39:18 -06:00
Tony Arcieri
b55c75ec39
Ensure signatures have right length and don't overflow (#306)
In both the PKCS#1v1.5 and PSS implementations, checks the signature
value to ensure it does not overflow the modulus.

In the PKCS#1v1.5 implementation, checks the signature length to ensure
it matches the public key size. The PSS implementation was already doing
this.

Closes #272
2023-04-25 08:36:26 -06:00
Dmitry Baryshkov
574664808d
Internals refactoring (#304)
* feat: decouple key generation and random generation

Make generate_multi_prime_key_with_exp() generic enough to generate
abstract key structure. Rewrite RsaPrivateKey constructors to use
RsaPrivateKey::from_components().

* feat: move key-related traits to separate module

Move PublicKeyParts to the separate module.

* feat: stop using RsaPrivateKey in internals.rs

Make internals.rs generic enough to be moved to the algorithms module.

* feat: move soft RSA implementation to crate::algorithms::rsa.rs

Separate software RSA implementation to separate module under
crate::algorithms.

* key: drop raw_int_*_primitive wrappers

Now as raw_int_encryption_primitive() and raw_int_decryption_primitive()
became simple wrappers around properly defined functions we can inline
them and always use software RSA algorithm from src::algorithms::rsa.rs.

* feat: move internals.rs to src/algortihms/pad.rs

internals.rs now contains only small functions related to BigUint to
Vec<u8> conversion. Move them to src/algorithms/pad.rs and get rid of
internals.rs

* algorithms: protect all functions with pub(crate)

While it is expected that the functions inside algorithms crates might
be useful (and used) by other parties, they are low level functions and
as such impose a high risk of being misused. Protect all of them with
pub(crate) to prevent them from being exposed by mistake.

Also add big fat warnings to raw RSA functions, which should never be
used unless authors knows exactly what they are using.

Signed-off-by: Dmitry Baryshkov <dmitry.baryshkov@linaro.org>
2023-04-23 16:53:21 -06:00
Dmitry Baryshkov
284fd79c1f
Remove primitive traits (#300)
The crate contains several exported traits targeting
hardware-accelerated implementations (PublicKey, PrivateKey,
EncryptionPrimitive, DecriptionPrimitive). However these traits
overcomplicate internal structure of the crate. It is not clear, which
level of API can be implemented by the hardware accelerators.
The crate is already quite complicated, implementing both
PaddingScheme-based API and Signer/Verifier/Encryptor/Decryptor API.

Remove the complication for now. The proper level of indirection can be
introduced once support for actual hardware accelerators is implemented.

Inline and drop the RsaPrivateKey::raw_decryption_primitive() function.
There is no need to zeroize argument, it is ciphertext, so it can be
assumed to be safe.

Change raw_int_decryption_primitive() and raw_int_decryption_primitive()
to output Result<BigUint> instead of Result<Vec<u8>>, because they also
take BigUint rather than Vec<u8> or &[u8].

In order to simplify adding support for RSA hardware accelerators, move
all formatting and padding functions to a separate modules, making it
theoretically possible to use that for implementing support for
low-level RSA hardware accelerators.

Also follows the pkcs1v15 change and use BigUint as a Signature's
internal implementation.

Signed-off-by: Dmitry Baryshkov <dmitry.baryshkov@linaro.org>
2023-04-19 08:51:06 -06:00
Tony Arcieri
2ec8708541
pkcs1v15: use BigUint as Signature's inner type (#298)
This one half of #220.

Doing anything with a signature involves converting it from bytes into a
`BigUint`, so this changes the inner type the latter which is more
useful.

It should also help address #272, since it will enable doing those sort
of checks more eagerly.
2023-04-18 12:17:07 -06:00
Tony Arcieri
891a4caba9
Rename Pkcs1v15Sign::new_raw to Pkcs1v15Sign::new_unprefixed (#293)
Following #290, which amended `pkcs1v15::SigningKey`, this commit makes
a corresponding change to `Pkcs1v15Sign` so the method name is
consistent with `SigningKey::new_unprefixed`
2023-04-17 06:40:24 -06:00
Tony Arcieri
bf1defd014
pkcs1v15: make *_with_prefix methods the default (#290)
Renames the following:

- `SigningKey::new` => `SigningKey::new_unprefixed`
- `SigningKey::new_with_prefix` => `SigningKey::new`
- `VerifyingKey::new` => `VerifyingKey::new_unprefixed`
- `VerifyingKey::new_with_prefix` => `VerifyingKey::new`

The `*_with_prefix` methods are preserved with a deprecation warning,
which should help people migrate to the new versions.

Closes #238
2023-04-11 06:37:34 -06:00
Dmitry Baryshkov
cf90255057
AssociatedAlgorithmIdentifier implementation (#278)
Implement associated AlgorithmIdentifier traits for PKCS1v15 and PSS keys

Signed-off-by: Dmitry Baryshkov <dmitry.baryshkov@linaro.org>
2023-04-04 20:12:08 -06:00
Matt Keeter
39d3eb95f2
Fix incorrect doc link (#267) 2023-03-02 10:05:53 -07:00
Dmitry Baryshkov
dacabfc5ff
Add Encryption-related traits (#259)
* feat: relax Sized requirement for random source parameters

* oaep: move OAEP test cases to src/oaep.rs

There is little point in having only OAEP test cases in src/key.rs. Move
them to proper module, oaep.rs.

* oaep: mark two functions as private

Currently the crate doesn't mark the oaep module as public. Thus it
makes little sense to mark top-level functions as public. Drop the
modifier.

* feat: traits: add traits for encryption and decryption

Add traits following the signature design for encryption and decryption.

* oaep: add support for new encryption API

Add new EncryptingKey and DecryptingKey structs implementing Encryptor /
Decryptor traits.

Signed-off-by: Dmitry Baryshkov <dmitry.baryshkov@linaro.org>
2023-02-13 08:35:20 -07:00
Tony Arcieri
134a061237
Add sha2 feature with oid subfeature enabled (#255)
We seem to be running into a lot of people who are having trouble with
PKCS#1 v1.5 signatures because the failure mode for the `oid` feature of
the `sha2` crate being disabled is fairly unscrutable.

See #234, #253, and the semi-related tracking issue for #238.

If `rsa` has a `sha2` feature, we can always ensure `oid` is enabled,
and this can be used in code examples. It also means users don't need
two crates to create/verify PKCS#1 v1.5 signatures.

RSA is used commonly enough with the SHA2 family that this integration
probably makes sense.
2023-01-20 16:46:27 -07:00
Tony Arcieri
35372d9516
Refactor PaddingScheme into a trait (#244)
Splits up the `PaddingScheme` enum into four structs, named after the
previous variants of the struct (adopting capitalization from the Rust
API guidelines):

- `oaep::Oaep`
- `pkcs1v15::{Pkcs1v15Encrypt, Pkcs1v15Sign}`
- `pss::Pss`

All of these are re-exported from the toplevel.

Each of these structs impls one or more of the following traits:

- `PaddingScheme`: used for encryption
- `SignatureScheme`: used for signing

The `PaddingScheme` constructors have been remapped as follows:

- `new_oaep` => `Oaep::new`
- `new_oaep_with_label` => `Oaep::new_with_label`
- `new_oaep_with_mgf_hash` => `Oaep::new_with_mgf_hash`
- `new_oaep_with_mgf_hash_with_label` => `Oaep::new_with_mgf_hash_and_label`
- `new_pkcs1v15_encrypt` => `Pkcs1v15Encrypt`
- `new_pkcs1v15_sign` => `Pkcs1v15Sign::new`
- `new_pkcs1v15_sign_raw` => `Pkcs1v15Sign::new_raw`
- `new_pss` => `Pss::{new, new_blinded}`
- `new_pss_with_salt` => `Pss::{new_with_salt new_blinded_with_salt}`
2023-01-10 13:59:31 -07:00
Tony Arcieri
35a32093f0
Bump signature to v2.0.0-rc.1 (#247) 2023-01-07 21:26:52 -07:00
Dmitry Baryshkov
8c96243f9f
v0.8.0-pre.0 (#237)
Also uses the new `CryptoRngCore` where possible instead of separate
`CryptoRng + RngCore`, and switches to `signature` v2.0.0-pre.3

Signed-off-by: Dmitry Baryshkov <dmitry.baryshkov@linaro.org>
2022-12-17 08:26:59 -07:00
Dmitry Baryshkov
b39752d4d8
feat: switch to version 2.0 (pre) of the signature crate (#217)
Rework the crate to implement traits from the preview of the signature
crate. Use `Vec<u8>` as `Self::Repr` type.

Drop the hand-crafted `From` traits, replacing them with the
implementation of the `Keypair` trait.

Signed-off-by: Dmitry Baryshkov <dmitry.baryshkov@linaro.org>
2022-12-04 19:03:38 -07:00
Tony Arcieri
6800df5b37
Linkify code examples in rustdoc (#222)
Makes each code example a linkable section of the rustdoc.

Uses these links to link from individual modules to code examples in the
toplevel rustdoc.
2022-11-12 18:43:30 -07:00
Tony Arcieri
eeb18ee88d
Add missing rustdoc comments; enable missing_docs lint (#216)
Several types and methods were missing documentation.

This commit adds document and enables warnings for `missing_docs`.

Additionally it updates all references to PKCS#1 RFCs to use RFC8017,
which documents the latest version of PKCS#1.
2022-10-31 14:15:52 -06:00
Dmitry Baryshkov
ece83cbdad
feat: add support for EncodePrivateKey (#208)
Implement encoding Signing keys to PKCS#8 DER format.

Signed-off-by: Dmitry Baryshkov <dmitry.baryshkov@linaro.org>
2022-10-08 14:38:43 -06:00
Dmitry Baryshkov
9066931701
RSA: implement EncodePublicKey for VerifyingKeys (#207)
Implement key -> der conversion for public keys.

Signed-off-by: Dmitry Baryshkov <dmitry.baryshkov@linaro.org>
2022-10-08 13:01:19 -06:00
Dirk Stolle
7caba62e9b
chore: fix some typos (#196) 2022-10-05 17:52:57 -06:00
Nathaniel McCallum
c880e5fed8
feat: enable extracting the inner key type (#201)
Signed-off-by: Nathaniel McCallum <nathaniel@profian.com>

Signed-off-by: Nathaniel McCallum <nathaniel@profian.com>
2022-09-27 15:42:36 -06:00
Dmitry Baryshkov
165f06f9cd
feat: iplement hazmat signature traits for PKCS1v15 keys (#195)
Implement PrehashSigner and PrehashVerifier traits for PKCS1v15
structures.

Signed-off-by: Dmitry Baryshkov <dmitry.baryshkov@linaro.org>
2022-09-22 10:03:46 -06:00
Nathaniel McCallum
a760b51b1e
feat: impl From for Signing/Verifying keys (#193)
Signed-off-by: Nathaniel McCallum <nathaniel@profian.com>
2022-09-21 16:43:04 -06:00
Nathaniel McCallum
0332c659df
feat: impl AsRef for Signing/Verifying keys (#194)
Signed-off-by: Nathaniel McCallum <nathaniel@profian.com>
2022-09-21 16:42:29 -06:00
Nathaniel McCallum
c2a57c640d
chore: resolve numerous clippy lints
Signed-off-by: Nathaniel McCallum <nathaniel@profian.com>
2022-09-21 23:06:18 +02:00
Xynnn_
29ff287752
feat: derive Clone and Debug for SigningKey/VerifyingKey (#191)
- added Clone and Debug for SigningKey & VerifyingKey
of pss and pkcs1v15

Signed-off-by: Xynnn007 <mading.ma@alibaba-inc.com>
2022-09-19 09:17:56 -06:00
Dmitry Baryshkov
92ef4c823b
pkcs1v15: use AssociatedOid for getting the RSA prefix (#183)
Drop internal implementation of `AssociatedHash` and use `AssociatedOid`
trait from `const_oid` to get the OID corresponding to the `Digest` and to
format the ASN.1 prefix.

Also removes the previous `Hash` enum as it was used for looking up OIDs.

Signed-off-by: Dmitry Baryshkov <dmitry.baryshkov@linaro.org>
2022-09-16 11:25:05 -06:00
Dmitry Baryshkov
36df97aded
Allow dereferencing Signature types (#182)
Implement Deref<Target = [u8]> for the Signature types to allow
automatically dereferencing Signature as byte slices.

Signed-off-by: Dmitry Baryshkov <dmitry.baryshkov@linaro.org>
2022-09-12 10:43:13 -06:00
Dmitry Baryshkov
d68e2731dc
Update RSA signature traits implementations (#179)
- Change the `SigningKey` and `VerifiyingKey` implementations accept raw
message rather than pre-hashed message.

- Implement the experimental (preview) `DigestSigner` and `DigestVerifier`
traits for the PKCS1v15 structs.

- Implement the experimental (preview) `RandomizedDigestSigner` and
`DigestVerifier` traits for the PSS structs.

Signed-off-by: Dmitry Baryshkov <dmitry.baryshkov@linaro.org>
2022-09-07 11:47:03 -06:00
Dmitry Baryshkov
40242fbbb0
Implement Signer/Verifier/Signature interfaces for the RSA signatures (#174)
Refactor the `rsa` crate to use the API defined by the signature crate.

This adds `pss` and `pkcs1v15` modules, each of them providing
`Signature`, `Verifier` and `Signer`/`RandomizedSigner` implementations.

Add tests for pkcs1v15 and pss signature verification functions to check
that verifying invalid signatures returns an error.

Signed-off-by: Dmitry Baryshkov <dmitry.baryshkov@linaro.org>
2022-08-19 11:46:40 -06:00
Tony Arcieri
4ccdcf92a9
Make RsaPrivateKey::from_components fallible (#167)
Adds an error case in the event the number of `primes` provides is fewer
than 2, which prevents panics when invoking methods which expect primes
to always be present at indices 0 and 1 (i.e. `p` and `q`)

Fixes #163
2022-07-25 06:35:47 -06:00
Addison Crump
fb6016575a
zeroize plaintexts which are about to be encrypted (#154) 2022-03-15 15:20:20 -06:00
Artyom Pavlov
bbfd911207
Remove nondeterministic tests (#152) 2022-03-14 14:22:48 +00:00
Artyom Pavlov
cf27569687
Replace rand dependency with rand_core (#148) 2022-03-13 19:50:05 +00:00
dignifiedquire
7a1eaa0329 refactor: rename RSA* to Rsa
This matches the rust naming convention better.

Closes #24
2021-07-26 23:25:13 +02:00
Tony Arcieri
89a527c05f
rustfmt (#105)
Applies `rustfmt` with the following version:

    rustfmt 1.4.37-stable (2a3635d5 2021-05-04)
2021-07-25 14:14:18 -07:00
Robin Lambertz
e8152949f9
feat: nostd, core+alloc support
* No-std support

* Fix tests

* Cleanly error out when building without the alloc feature

* Run no-std tests on arm-linux-gnu target

* Fix nostd tests

* Attempt 2 at fixing nostd tests

* Fix warnings when running tests in nostd mode

* fixup! No-std support
2020-08-07 22:39:36 +02:00
dignifiedquire
668e9ddfe4 apply CR 2020-04-10 14:34:25 +02:00
dignifiedquire
11500ed5e9 make oaep and pss generic over keys 2020-03-06 23:10:55 +01:00
dignifiedquire
167080e2a4 Merge remote-tracking branch 'origin/master' into oaep-dig 2020-03-06 22:49:57 +01:00
Jack Grigg
ff584d1bb8 pkcs1v15: Make decrypt() and sign() generic over PrivateKey 2020-03-07 09:46:48 +13:00
dignifiedquire
476f642075 Merge branch 'pss' into oaep-dig 2020-03-06 21:10:13 +01:00
Jack Grigg
ea2a3bfeb8 Introduce key::{PrivateKey, PublicKeyParts}
RSAPrivateKey no longer implements PublicKey. Instead,
RSAPublicKey::from(RSAPrivateKey) should be used to obtain a public key.
2020-03-06 18:05:57 +01:00
Jack Grigg
826ea30004 Introduce raw::{DecryptionPrimitive, EncryptionPrimitive} 2020-03-06 18:05:57 +01:00
roblabla
610c4cc95f Move copy_with_left_pad to algorithms 2019-09-26 14:49:59 +00:00
roblabla
30220cfd44 Fix the tests 2019-09-26 14:49:59 +00:00