-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Compile secp256 in U-boot #603
Conversation
Adds about ~36k or so (big for a bootloader) diff --git a/lib/Kconfig b/lib/Kconfig index 622f3c26c33..38c2f6493a1 100644 --- a/lib/Kconfig +++ b/lib/Kconfig @@ -175,2 +175,3 @@ config AES source lib/rsa/Kconfig +source lib/secp256k1/Kconfig diff --git a/lib/Makefile b/lib/Makefile index 5f583aed37d..02ec35e8201 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -51,2 +51,3 @@ endif obj-$(CONFIG_RSA) += rsa/ +obj-$(CONFIG_DSA) += secp256k1/ obj-$(CONFIG_SHA1) += sha1.o
Sorry, I didn't see this PR before. This is very interesting! For reducing the code size, it would make sense to include only the verification parts, not signing, which would not be necessary. I don't think there is currently a define for this. Also you're using the basic configuration,
For RV64 you can use the 64-bit code for the field and scalar operations, which have fewer steps thus shorter (and faster):
Note that secp256k1 precomputes a large table in the context at initialization (in
For bootloader use, you'd want to a) reduce the size of the table and b) embed the table statically (which unfortunately increases the code size). It can be set arbitrarily small. I don't know what is the smallest table size that still leads to acceptable verification performance for your use case. There's a PR that makes the window configurable and has some benchmarks: #596. There hasn't been work yet on embedding the table statically AFAIK. |
Signing uses a static table, its more or less trivial to do. (usually people who have wanted to use the library in embedded contexts have wanted signing, not verification... :) ). There are also a whole bunch of easy size optimizations where we have several different versions of a function optimized for different cases which could all be substituted by the most general. Re leaving out signing, the linker should strip the signing parts if they're not used. |
I liked the idea (also by @gmaxwell) to hardcode the table the for a few small
Are you sure? Unless using full program optimization, or |
I was curious so I created a list of function/data objects and their sizes—on RV64—that are used from
This can be considered a lower bound on the size added to the bootloader for verification only. Of course, any hardcoded precomputed |
we should be able to get a 3.8% size reduction with negligible impact in performance by having something to use the strongest normalize function for all normalize uses.... but I dunno if it's worth having a define to shrink the code if that's all that it does. Some of the larger functions like scalar inverse and fe_inverse could probably be shrank a whole lot with a small performance hit by un-unrolling them by adding a small state machine. I notice the sqrt isn't in your list, this is because you don't have a pubkey load. The sqrt is only needed if a compressed pubkey is used. For a bootloader, you'd be best off using an uncompressed pubkey, then the code doesn't need the big and somewhat slow sqrt function. |
What are the next steps here? The patch as-is isn't acceptable as it introduces Linux-specific includes in the source code, as well as adding a static Makefile that would be confusing to other builders. Those things should, if necessary, at least move to some contrib directory or perhaps even a separate repository. However, there are perhaps things that can be done to the codebase itself to help its use in this setting? Maybe a way to configure while dropping things that aren't relevant to verification? Since #596 the WINDOW_G size is configurable to reduce the size of the static table. |
This pull request is more for discussion, and get some commentary on the value of having device firmware (such as u-boot) that can verify DSA signatures of kernel and userspace payloads.
Things that I need are some help with the simplest example code to check DSA signatures, and any pointers to any other smaller embedded DSA signature codes, or some discussion on whether adapting secp256k to this application is even a good idea.
In particular, maybe @laanwj might find this interesting for a riscv-laptop
Adds about ~36k or so (big for a bootloader)
Also needs the following in u-boot:
diff --git a/lib/Kconfig b/lib/Kconfig
index 622f3c26c33..38c2f6493a1 100644
--- a/lib/Kconfig
+++ b/lib/Kconfig
@@ -175,2 +175,3 @@ config AES
source lib/rsa/Kconfig
+source lib/secp256k1/Kconfig
diff --git a/lib/Makefile b/lib/Makefile
index 5f583aed37d..02ec35e8201 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -51,2 +51,3 @@ endif
obj-$(CONFIG_RSA) += rsa/
+obj-$(CONFIG_DSA) += secp256k1/
obj-$(CONFIG_SHA1) += sha1.o