Skip to content
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

Amalgamation #596

Merged
merged 7 commits into from
Feb 21, 2025
Merged

Amalgamation #596

merged 7 commits into from
Feb 21, 2025

Conversation

sjaeckel
Copy link
Member

@sjaeckel sjaeckel commented Aug 5, 2022

Checklist

  • documentation is added or updated
  • tests are added or updated

This adds support to create and build an amalgamated version of the library, which is maybe useful for some.

@sjaeckel sjaeckel added this to the next milestone Aug 5, 2022
@sjaeckel sjaeckel requested review from karel-m and eduardsui August 5, 2022 15:27
@sjaeckel sjaeckel force-pushed the add-pem-support branch 2 times, most recently from f486b8c to abbeeaa Compare June 20, 2023 17:13
@sjaeckel sjaeckel force-pushed the add-pem-support branch 7 times, most recently from 30daf31 to 473ae9f Compare October 14, 2023 09:55
@sjaeckel sjaeckel force-pushed the add-pem-support branch 12 times, most recently from 37b9b8d to 33c35d0 Compare October 20, 2023 12:24
@sjaeckel sjaeckel force-pushed the add-pem-support branch 4 times, most recently from 4d9823f to ad1499d Compare February 27, 2024 15:55
@sjaeckel sjaeckel force-pushed the add-pem-support branch 2 times, most recently from 6f506cb to 2594f3a Compare August 20, 2024 11:29
Base automatically changed from add-pem-support to develop August 20, 2024 12:34
@sjaeckel sjaeckel force-pushed the amalgamation branch 3 times, most recently from b975c98 to c8391c4 Compare February 18, 2025 10:28
@sjaeckel sjaeckel requested a review from levitte February 18, 2025 10:33
@sjaeckel sjaeckel marked this pull request as ready for review February 18, 2025 10:33
@eduardsui
Copy link
Collaborator

Cool! Just tested it, it works great! Thank you! It still includes aes_tab.c, safe_tab.c, sober128tab.c and whirltab.c, but very easy to manage. I will run some tests on TLSe using the new amalgamated version, but from the first runs it looks absolutely great!

Thanks!

@sjaeckel
Copy link
Member Author

It still includes aes_tab.c, safe_tab.c, sober128tab.c and whirltab.c, but very easy to manage.

better?

@levitte
Copy link
Collaborator

levitte commented Feb 18, 2025

Can't quite see the point with this sort of amalgamation, but if someone finds it useful, why not?

@sjaeckel
Copy link
Member Author

Can't quite see the point with this sort of amalgamation, but if someone finds it useful, why not?

What do you mean by "can't see the point"? What would you prefer/do instead?

@levitte
Copy link
Collaborator

levitte commented Feb 20, 2025

Can't quite see the point with this sort of amalgamation, but if someone finds it useful, why not?

What do you mean by "can't see the point"? What would you prefer/do instead?

My remark wasn't specific to libtomcrypt. Amalgamation showed up on my radar just a few months ago, and I've yet to understand what's the underlying need. I can understand wanting to amalgamate a gazillion dependencies (I've looked at some rust projects and OMG the number of them!), but otherwise?...

But like I said, if someone does find this useful, yeah ok sure, why not?

Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
* de-duplicate `struct oid_to_pbes`
* add makefile target
* add amalgamation to release
* fix `small` demo
* add header guards for `tomcrypt_private.h`
* update docs
* add CI job with amalgamated library

Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
```
pre_gen/tomcrypt_amalgam.c: In function ‘pkcs_1_oaep_encode’:
pre_gen/tomcrypt_amalgam.c:64485:18: warning: ‘DB’ may be used uninitialized [-Wmaybe-uninitialized]
64485 |       if ((err = hash_memory(lparam_hash_used, DB, 0, DB, &x)) != CRYPT_OK) {
      |                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
pre_gen/tomcrypt_amalgam.c:22613:5: note: by argument 2 of type ‘const unsigned char *’ to ‘hash_memory’ declared here
22613 | int hash_memory(int hash, const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen)
      |     ^~~~~~~~~~~
```

Use `out` instead of `DB`, since `out` is `LTC_ARGCHK`'ed.

Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
Instead of wrongfully always returning OK, return the real error code.

This was uncovered by the amalgamation via the warning:

```
pre_gen/tomcrypt_amalgam.c: In function ‘der_decode_custom_type_ex’:
pre_gen/tomcrypt_amalgam.c:49820:10: warning: ‘*(unsigned int *)((char *)&ident + offsetof(ltc_asn1_list, type))’ may be used uninitialized [-Wmaybe-uninitialized]
49820 |       if ((ident.type != root->type) ||
      |          ^
pre_gen/tomcrypt_amalgam.c:49780:18: note: ‘*(unsigned int *)((char *)&ident + offsetof(ltc_asn1_list, type))’ was declared here
49780 |    ltc_asn1_list ident;
      |                  ^~~~~
```

Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
@sjaeckel sjaeckel merged commit d032686 into develop Feb 21, 2025
78 checks passed
@sjaeckel sjaeckel deleted the amalgamation branch February 21, 2025 11:08
@sjaeckel
Copy link
Member Author

[...] I've yet to understand what's the underlying need.

I guess the most important is "lazyness". You don't want to manage all those libraries, so you simply take the single C files and add them directly to your project.

@eduardsui
Copy link
Collaborator

I confirm the "lazyness" :). Also, it is simpler to manage when writing wrappers for various interpreters or microcontrollers. A few years ago I've seen this idea on sqlite (which offers an amalgamated version) - I just add it to my project and it just worked. Very easy when checking out libraries, for example: I come across a new XML library and I want to try it by using it in the simplest mode possible. I just write a few lines of C code and just compile the whole thing without any need of makefiles or cmake. This doesn't mean that this is the best way, but it is "quick to try".

@levitte
Copy link
Collaborator

levitte commented Feb 23, 2025

Ok, I can understand that part.

I immediately find it a pity, then, that C has some unfortunate limitations that makes such things difficult, 'cause for example, static things that are meant for just one translation unit (source file) suddenly become "public" throughout the whole library. I saw a blog on this, by someone who tried to amalgamate OpenSSL.... oh boy.

Side note: I have just recently gotten interested in Zig, BTW, and think it's much more conducive to amalgamation. Nothing I really thought of before, but you got me thinking, so thank you for showing me your points

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants