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

Fix #22140 - Add bech32 encoding/decoding #23466

Merged
merged 20 commits into from
Nov 1, 2024
Merged

Conversation

W0nda
Copy link
Contributor

@W0nda W0nda commented Oct 11, 2024

  • Mark this if you consider it ready to merge
  • I've added tests (optional)
  • I wrote some lines in the book (optional)

Description

Add bech32 encoding/decoding but I don't know if I should integrate that to rax2 or not...

@W0nda W0nda closed this Oct 11, 2024
@W0nda W0nda deleted the mybranch branch October 11, 2024 19:24
@W0nda W0nda restored the mybranch branch October 11, 2024 19:30
@W0nda W0nda reopened this Oct 11, 2024
@trufae
Copy link
Collaborator

trufae commented Oct 11, 2024

Maybe makes more sense to be in rahash2

@trufae
Copy link
Collaborator

trufae commented Oct 11, 2024

Fix indentation rules

  • dont preincrement
  • Dont else after return

@trufae
Copy link
Collaborator

trufae commented Oct 11, 2024

Its probably better to have it as a crypto plugin for encoding. Also chk the linting issues to follow the indentation rules (space before tab) and having some tests would be great too.

good work!

@trufae
Copy link
Collaborator

trufae commented Oct 12, 2024

Will be good to have the plugin , take a look at the crypto_base64.c . if you dont expose this api and use it directly in the plugin it wont need to be new-abi specific

@W0nda
Copy link
Contributor Author

W0nda commented Oct 12, 2024

I'm implementing tests, next pushed commits will be:

  1. tests
  2. implementation of bech32 as plugin

@W0nda
Copy link
Contributor Author

W0nda commented Oct 12, 2024

The crypto plugins implementation isn't valid at all. Sorry for having pushed too soon.
I'll correct that and push tomorrow or next week.

@trufae
Copy link
Collaborator

trufae commented Oct 13, 2024

sounds good. there's no hurry. ill cut the release before merging this

@trufae
Copy link
Collaborator

trufae commented Oct 17, 2024

github decided to broke all ubuntu-latest images, i fixed that on master, can you rebase your patches on top of master?

@W0nda
Copy link
Contributor Author

W0nda commented Oct 21, 2024

The encoding code as been tested and works but I don't know how to integrate it into r2. Each time I try to build it fails.

(The implem in crypto_bech32.c isn't totally finished but I tried to understand how to compile it first)

@trufae
Copy link
Collaborator

trufae commented Oct 21, 2024

The header file is not there. Maybe you forgot to commit it?

@@ -9,6 +9,7 @@
#include <r_hash.h>
#include <r_lib.h>
#include <r_crypto/r_sm4.h>
#include <r_crypto/r_bech32.h>
Copy link
Collaborator

Choose a reason for hiding this comment

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

this file doesnt exist

BECH32_ENCODING_BECH32M
} bech32_encoding;

R_API int bech32_encode(char *output, const char *hrp, const uint8_t *data, size_t data_len, bech32_encoding enc);
Copy link
Collaborator

Choose a reason for hiding this comment

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

we dont want to expose new APIs that will break the abi contract. Can you make this static for now? actually its only purpose is to be used by the plugin so it shouldnt be an issue

Copy link
Collaborator

Choose a reason for hiding this comment

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

also public apis must start with r_ and if you really want to expose them you can wrap them under R2_USE_NEW_ABI

return 0;
}

void test_crypto_bech32_encode (void) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

static

size_t data_len;
int i;
for (i = 0; i < sizeof (valid_checksum_bech32) / sizeof (valid_checksum_bech32[0]); i++) {
bech32_decode (hrp, data, &data_len, valid_checksum_bech32[i]);
Copy link
Collaborator

Choose a reason for hiding this comment

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

these tests use an api that shouldnt be made public to protect the abi contract. so you can:

  • use RCrypto api to test bech32 with its plugin instead
  • expose this API via R2_USE_NEW_ABI, and prefix it with r_crypto_bech32_...

char hrp[84];
size_t data_len;
int i;
for (i = 0; i < sizeof (valid_checksum_bech32) / sizeof (valid_checksum_bech32[0]); i++) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

better move this sizeof() to a const int size = .. one line above

Copy link
Collaborator

@trufae trufae left a comment

Choose a reason for hiding this comment

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

. if you need more help better use the chat

@trufae
Copy link
Collaborator

trufae commented Oct 21, 2024

please commit the missing file, i can take over the pr and do the needed fixes if you want, but the error in the build log complains about a file which i guess you didnt' commited

@trufae trufae added this to the 5.9.8 - gigglebytes milestone Oct 21, 2024
@W0nda
Copy link
Contributor Author

W0nda commented Oct 24, 2024

Missing file coming.
Sorry for that

@trufae
Copy link
Collaborator

trufae commented Oct 25, 2024

Another missing file
IMG_7349

@trufae
Copy link
Collaborator

trufae commented Oct 25, 2024

just fixing it on a branch, but this code have quite many errors and warnings. ill fix them up in my pr.

p/crypto_bech32.c: In function ‘update’:
p/crypto_bech32.c:188:56: error: ‘enc’ undeclared (first use in this function)
  188 |                 bech32_encode (in_out, hrp, data, len, enc);
      |                                                        ^~~
p/crypto_bech32.c:188:56: note: each undeclared identifier is reported only once for each function it appears in
p/crypto_bech32.c:188:40: warning: passing argument 2 of ‘bech32_encode’ makes pointer from integer without a cast [-Wint-conversion]
  188 |                 bech32_encode (in_out, hrp, data, len, enc);
      |                                        ^~~
      |                                        |
      |                                        char
p/crypto_bech32.c:64:46: note: expected ‘const char *’ but argument is of type ‘char’
   64 | int bech32_encode (char *output, const char *hrp, const uint8_t *data, size_t data_len, bech32_encoding enc) {
      |                                  ~~~~~~~~~~~~^~~
p/crypto_bech32.c:188:45: warning: passing argument 3 of ‘bech32_encode’ makes pointer from integer without a cast [-Wint-conversion]
  188 |                 bech32_encode (in_out, hrp, data, len, enc);
      |                                             ^~~~
      |                                             |
      |                                             char
p/crypto_bech32.c:64:66: note: expected ‘const uint8_t *’ {aka ‘const unsigned char *’} but argument is of type ‘char’
   64 | int bech32_encode (char *output, const char *hrp, const uint8_t *data, size_t data_len, bech32_encoding enc) {
      |                                                   ~~~~~~~~~~~~~~~^~~~
p/crypto_bech32.c:188:51: warning: passing argument 4 of ‘bech32_encode’ makes integer from pointer without a cast [-Wint-conversion]
  188 |                 bech32_encode (in_out, hrp, data, len, enc);
      |                                                   ^~~
      |                                                   |
      |                                                   size_t * {aka long unsigned int *}
p/crypto_bech32.c:64:79: note: expected ‘size_t’ {aka ‘long unsigned int’} but argument is of type ‘size_t *’ {aka ‘long unsigned int *’}
   64 | int bech32_encode (char *output, const char *hrp, const uint8_t *data, size_t data_len, bech32_encoding enc) {
      |                                                                        ~~~~~~~^~~~~~~~
p/crypto_bech32.c:190:32: warning: passing argument 1 of ‘bech32_decode’ makes pointer from integer without a cast [-Wint-conversion]
  190 |                 bech32_decode (hrp, data, len, in_out);
      |                                ^~~
      |                                |
      |                                char
p/crypto_bech32.c:106:38: note: expected ‘char *’ but argument is of type ‘char’
  106 | bech32_encoding bech32_decode (char *hrp, uint8_t *data, size_t *data_len, const char *input) {
      |                                ~~~~~~^~~
p/crypto_bech32.c:190:37: warning: passing argument 2 of ‘bech32_decode’ makes pointer from integer without a cast [-Wint-conversion]
  190 |                 bech32_decode (hrp, data, len, in_out);
      |                                     ^~~~
      |                                     |
      |                                     char
p/crypto_bech32.c:106:52: note: expected ‘uint8_t *’ {aka ‘unsigned char *’} but argument is of type ‘char’
  106 | bech32_encoding bech32_decode (char *hrp, uint8_t *data, size_t *data_len, const char *input) {
      |                                           ~~~~~~~~~^~~~
p/crypto_bech32.c: In function ‘end’:
p/crypto_bech32.c:198:28: error: ‘hrp’ undeclared (first use in this function)
  198 |         return update (cj, hrp, data, data_len, in_out, enc);
      |                            ^~~
p/crypto_bech32.c:198:33: error: ‘data’ undeclared (first use in this function)
  198 |         return update (cj, hrp, data, data_len, in_out, enc);
      |                                 ^~~~
p/crypto_bech32.c:198:39: error: ‘data_len’ undeclared (first use in this function)
  198 |         return update (cj, hrp, data, data_len, in_out, enc);
      |                                       ^~~~~~~~
p/crypto_bech32.c:198:49: error: ‘in_out’ undeclared (first use in this function); did you mean ‘ino_t’?
  198 |         return update (cj, hrp, data, data_len, in_out, enc);
      |                                                 ^~~~~~
      |                                                 ino_t
p/crypto_bech32.c:198:57: error: ‘enc’ undeclared (first use in this function); did you mean ‘end’?
  198 |         return update (cj, hrp, data, data_len, in_out, enc);
      |                                                         ^~~
      |                                                         end
p/crypto_bech32.c:198:16: error: too many arguments to function ‘update’
  198 |         return update (cj, hrp, data, data_len, in_out, enc);
      |                ^~~~~~
p/crypto_bech32.c:184:13: note: declared here
  184 | static bool update(RCryptoJob *cj, const ut8 *buf, size_t *len) {
      |             ^~~~~~
p/crypto_bech32.c: At top level:
p/crypto_bech32.c:210:19: warning: initialization of ‘_Bool (*)(RCryptoJob *, const uint8_t *, int)’ {aka ‘_Bool (*)(struct r_crypto_job_t *, const unsigned char *, int)’} from incompatible pointer type ‘_Bool (*)(RCryptoJob *, const uint8_t *, size_t *)’ {aka ‘_Bool (*)(struct r_crypto_job_t *, const unsigned char *, long unsigned int *)’} [-Wincompatible-pointer-types]
  210 |         .update = update,
      |                   ^~~~~~
p/crypto_bech32.c:210:19: note: (near initialization for ‘r_crypto_plugin_bech32.update’)
p/crypto_bech32.c: In function ‘bech32_decode’:
p/crypto_bech32.c:169:1: warning: control reaches end of non-void function [-Wreturn-type]
  169 | }
      | ^
p/crypto_bech32.c: In function ‘end’:
p/crypto_bech32.c:199:1: warning: control reaches end of non-void function [-Wreturn-type]
  199 | }
      | ^

@trufae
Copy link
Collaborator

trufae commented Oct 25, 2024

here you have: #23528

cherry pick the last commit, as its the only one that is different from your branch, but even if it compiles this code wont work because it have lots of inconsistent issues and im not sure how to use the bech32 algorithm, what's hrp, adn so on. please update your pr with my commit and fix the code

Copy link
Collaborator

@trufae trufae left a comment

Choose a reason for hiding this comment

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

^

@W0nda
Copy link
Contributor Author

W0nda commented Oct 30, 2024

I can't build without r_bech32.h (that's why I left it last commit).

@trufae
Copy link
Collaborator

trufae commented Oct 30, 2024

if you cant build withhout the r_bech32.h is because its included or referenced from a .d somewhere.

@W0nda
Copy link
Contributor Author

W0nda commented Oct 31, 2024

Apparently I can build without r_bech32.h ...

@trufae
Copy link
Collaborator

trufae commented Oct 31, 2024

There are conflicts with master now, also this function can be removed because its unused p/crypto_bech32.c:45:12: error: ‘hrplength’ defined but not used [-Werror=unused-function] 45 | static int hrplength(const unsigned char *bech32_checksum, const unsigned char size) { | ^~~~~~~~~

@W0nda
Copy link
Contributor Author

W0nda commented Oct 31, 2024

I tried to resolve the conflicts i hope it worked.

@trufae
Copy link
Collaborator

trufae commented Nov 1, 2024

Loooks like building now! Good job!

@trufae trufae merged commit 58bacd2 into radareorg:master Nov 1, 2024
42 checks passed
@W0nda W0nda deleted the mybranch branch November 1, 2024 14:03
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