8000 Initial commit of RSA Key support by alistair23 · Pull Request #2839 · tock/tock · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Initial commit of RSA Key support #2839

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 16, 2022
Merged

Conversation

alistair23
Copy link
Contributor
@alistair23 alistair23 commented Sep 24, 2021

Pull Request Overview

This is a split from #2693 adding RSA Key support.

Why add a trait and capsule?

Public/Private keys are too complex and critical to be handled in plain buffers. Using plain buffers opens us up to easy mistakes of mismatching keys type or extracting useful information.

For this reason this PR adds a generic HIL for public/private key pairs. Using this generic HIL we also add a RSA specific HIL and two implementations of RSA keys. We have two implementations to support the case where we have both public/private keys and the case where we only have a public key. This way we can avoid overhead of private key storage if we aren't going to use it.

Why add a new enum type?

The goal is to support 3 main use cases:

  1. Keys stored on flash. The keys are stored at some address in read only flash and we want to "import" them and use them in the kernel
  2. The app specifies a key. A userspace application obtains a key and passes it to the kernel to use for crypto operations
  3. We generate a key pair while running

In order to support all three we need to accept both mutable buffers (for userspace driver capsules and generating keys) and immutable buffers (for data stored in flash).

In order to support both I have added a new enum called MutImutBuffer. This allows us to pass around either a mutable static buffer or an immutable static buffer. This can be passed all the way down to the chip driver which can then unpack the buffers and write the data to hardware.

Testing Strategy

This has been tested with the OpenTitan RSA implementation: #2899

TODO or Help Wanted

Documentation Updated

  • Updated the relevant files in /docs, or no updates are required.

Formatting

  • Ran make prepush.

@github-actions github-actions bot added the HIL This affects a Tock HIL interface. label Sep 24, 2021
8000
@alistair23 alistair23 force-pushed the alistair/rsa-keys branch 3 times, most recently from 70e4fc1 to 0d32777 Compare September 27, 2021 06:57
@alistair23
Copy link
Contributor Author

Ok, I'm a little stuck.

There are three main use cases I think we need to support:

  1. Keys stored on flash. The keys are stored at some address in read only flash and we want to "import" them and use them in the kernel
  2. The app specifies a key. A userspace application obtains a key and passes it to the kernel to use for crypto operations
  3. We generate a key pair while running

Ideally we want to support all 3 without too much overhead.

My original plan was that a board would create a single RSA2048Keys struct. It would then copy over a key from flash, perform operations and then move on to the next key. This has the large downside that we have to store the key in memory. My hope though was that we could create a very small number of structs and re-use them. So for example:

let key = RSA2048Keys::new()
key.import(&flash_key_one);
key.do_stuff();
key.import(&flash_key_two);
key.do_stuff();

This means we need to keep a single key in memory, but can support any number of keys. We can also support all 3 use cases above. The problem is I think this is too much over head as we have to store large keys in memory.

The other option is to have an interface that takes a static key, like this:

    /// Import an existing public key. This will override any existing keys.
    ///
    /// The `public_key` is stored internally and can be retrieved with
    /// `pub_key()`.
    ///
    /// The possible ErrorCodes are:
    ///     - `ALREADY`: A key is already imported or in the process of being
    ///                  generated.
    ///     - `INVAL`: An invalid key was supplied.
    ///     - `SIZE`: An invalid key size was supplied.
    ///     - `NOMEM`: The key can not be stored.
    fn import_public_key(
        &'a self,
        public_key: &'static [u8],
    ) -> Result<(), (&'static [u8], ErrorCode)>;

    /// Return the public key supplied by `import_public_key()` or
    /// `generate()`.
    ///
    /// On success the return value is `Ok(())`.
    /// On failure the possible ErrorCodes are:
    ///     - `NODEVICE`: The key does not exist
    ///     - `SIZE`: An invalid buffer size was supplied.
    ///     - `NOMEM`: The key can not be stored.
    fn pub_key(&'a self) -> Result<&'static [u8], ErrorCode>;

The problem here is mutability. We need a mutable buffer to allow generating keys and working with apps, but that won't work with read only flash. It's a similar issue to tock/libtock-rs#334

We could have read and read/write traits based on where the key is coming from. Am I missing something, does anyone have a better idea?

@alistair23 alistair23 marked this pull request as draft September 27, 2021 07:10
@alistair23 alistair23 changed the title Initial commit of RSA Key support Initial commit of a mutable/immutable buffer enum and RSA Key support Sep 28, 2021
@alistair23 alistair23 force-pushed the alistair/rsa-keys branch 2 times, most recently from 6890a67 to 7f9e95e Compare September 28, 2021 03:18
@github-actions github-actions bot added the nrf Change pertains to the nRF5x family of MCUs. label Sep 28, 2021
@github-actions github-actions bot removed the nrf Change pertains to the nRF5x family of MCUs. label Sep 28, 2021
@alistair23 alistair23 closed this Sep 28, 2021
@alistair23 alistair23 deleted the alistair/rsa-keys branch September 28, 2021 03:27
@alistair23 alistair23 reopened this Sep 28, 2021
@alistair23 alistair23 force-pushed the alistair/rsa-keys branch 3 times, most recently from aaf8d36 to 40920ab Compare September 28, 2021 06:35
@github-actions github-actions bot added the tock-libraries This affects libraries supported by the Tock project label Sep 28, 2021
@github-actions github-actions bot removed the tock-libraries This affects libraries supported by the Tock project label Sep 28, 2021
@alistair23 alistair23 marked this pull request as ready for review November 12, 2021 01:38
@alistair23
Copy link
Contributor Author

Marking this as ready for review as there is now a full implementation

Copy link
Contributor
@phil-levis phil-levis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be updated to not use MutImutBuffer.

@alistair23 alistair23 force-pushed the alistair/rsa-keys branch from 44a5fd1 to c7f86e3 Compare 8000 December 2, 2021 03:23
@alistair23
Copy link
Contributor Author

Updated to not use MutImutBuffer in any HIL

bors bot added a commit that referenced this pull request Dec 10, 2021
2852: kernel: utilities: Initial commit of mut_imut_buffer r=hudson-ayers a=alistair23

### Pull Request Overview

There are a few times inside the Tock kernel where we want to support either a `'static mut` buffer or a `'static` buffer. Examples of this include the [RSA key support](#2839) but also the [OTBN](https://github.com/tock/tock/blob/master/chips/lowrisc/src/otbn.rs#L153).

In order to support both I have added a new enum called MutImutBuffer. This allows us to pass around either a mutable static buffer or an immutable static buffer. This can be passed all the way down to the chip driver which can then unpack the buffers and write the data to hardware.

### Testing Strategy

CI and RSA key implementation

### TODO or Help Wanted

### Documentation Updated

- [X] Updated the relevant files in `/docs`, or no updates are required.

### Formatting

- [X] Ran `make prepush`.


Co-authored-by: Alistair Francis <alistair.francis@wdc.com>
@github-actions github-actions bot added the WG-OpenTitan In the purview of the OpenTitan working group. label Dec 10, 2021
@alistair23
Copy link
Contributor Author

Rebased on master and all comments should be addresses

@phil-levis
Copy link
Contributor

Hey, getting back to this now. I think we resolved all of the issues with keys, but I suspect that rsa_math is going to require some more work due to the mutable/immutable problem. Can you cut that file so I can approve this?

Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
@alistair23
Copy link
Contributor Author

Done!

Copy link
Contributor
@phil-levis phil-levis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the TRD needs a lot of work, but that can be improved later.

@alistair23
Copy link
Contributor Author

Can this be merged?

@hudson-ayers
Copy link
Contributor

bors r+

@bors
Copy link
Contributor
bors bot commented Feb 16, 2022

@bors bors bot merged commit ef962ec into tock:master Feb 16, 2022
@phil-levis phil-levis mentioned this pull request May 20, 2022
2 tasks
bors bot added a commit that referenced this pull request Jun 15, 2022
2899: OpenTitan: Add support for hardware RSA operations r=phil-levis a=alistair23

### Pull Request Overview

This PR builds on top of:
 * #2898
 * #2892
 * #2839

and adds support for RSA hardware operations using the OTBN.

### Testing Strategy

Running the RSA test in this PR on the CW310 FPGA.

### TODO or Help Wanted

### Documentation Updated

- [X] Updated the relevant files in `/docs`, or no updates are required.

### Formatting

- [X] Ran `make prepush`.


Co-authored-by: Alistair Francis <alistair.francis@wdc.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
HIL This affects a Tock HIL interface. kernel WG-OpenTitan In the purview of the OpenTitan working group.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0