Skip to content

Commit 2f06dba

Browse files
authored
Merge pull request #54 from EdgeApp/william/fix-length-math
Fix length math
2 parents 233c2e6 + 8ebeb8a commit 2f06dba

File tree

6 files changed

+13
-69
lines changed

6 files changed

+13
-69
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# react-native-fast-crypto
22

3+
## Unreleased
4+
5+
- fixed: Malloc proper size for secp256k1 keys.
6+
37
## 2.2.0 (2022-03-18)
48

59
- changed: Compile secp256k1 as an XCFramework, making it compatible with the iOS simulator on M1 Macs.

android/src/main/cpp/crypto_bridge.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//fastCrypto.cpp
22
#include <android/log.h>
33
#include <jni.h>
4-
#include "fast_crypto.h"
4+
#include "native-crypto.h"
55

66
#define LOG_TAG "crypto_bridge-JNI"
77

@@ -292,9 +292,8 @@ Java_co_airbitz_fastcrypto_RNFastCryptoModule_secp256k1EcPubkeyCreateJNI(JNIEnv
292292
}
293293
int privateKeyLen = strlen(szPrivateKeyHex);
294294

295-
// Buffer size: privateKeyLen * 2 + 3 bytes
296-
// For 64-char private key: uncompressed public key = 130 hex chars (string) + 1 null terminator = 131 bytes
297-
char szPublicKeyHex[privateKeyLen * 2 + 3];
295+
// Hex with a null terminator is 2 * bytes + 1:
296+
char szPublicKeyHex[2 * DECOMPRESSED_PUBKEY_LENGTH + 1];
298297

299298
fast_crypto_secp256k1_ec_pubkey_create(szPrivateKeyHex, szPublicKeyHex, jiCompressed);
300299
jstring out = env->NewStringUTF(szPublicKeyHex);

android/src/main/cpp/fast_crypto.h

Lines changed: 0 additions & 57 deletions
This file was deleted.

android/src/main/cpp/native-crypto.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ extern "C" {
1616
#include <stdlib.h>
1717
#include <string.h>
1818

19-
#define COMPRESSED_PUBKEY_LENGTH 33
20-
#define DECOMPRESSED_PUBKEY_LENGTH 65
21-
#define PRIVKEY_LENGTH 64
22-
2319
void fast_crypto_scrypt (const uint8_t *passwd, size_t passwdlen, const uint8_t *salt, size_t saltlen, uint64_t N,
2420
uint32_t r, uint32_t p, uint8_t *buf, size_t buflen)
2521
{

android/src/main/cpp/native-crypto.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@
4444
extern "C" {
4545
#endif
4646

47+
#define COMPRESSED_PUBKEY_LENGTH 33
48+
#define DECOMPRESSED_PUBKEY_LENGTH 65
49+
#define PRIVKEY_LENGTH 64
50+
4751
void fast_crypto_scrypt (const uint8_t *passwd, size_t passwdlen, const uint8_t *salt, size_t saltlen, uint64_t N,
4852
uint32_t r, uint32_t p, uint8_t *buf, size_t buflen);
4953
void fast_crypto_secp256k1_ec_privkey_tweak_add(char *szPrivateKeyHex, const char *szTweak);

ios/RNFastCrypto.m

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,10 @@ - (dispatch_queue_t)methodQueue
7979
resolver:(RCTPromiseResolveBlock)resolve
8080
rejecter:(RCTPromiseRejectBlock)reject)
8181
{
82-
// Buffer size: privateKeyLen * 2 + 3 bytes
83-
// For 64-char private key: uncompressed public key = 130 hex chars (string) + 1 null terminator = 131 bytes
84-
char *szPublicKeyHex = malloc(sizeof(char) * ([privateKeyHex length] * 2 + 3));
82+
// Hex with a null terminator is 2 * bytes + 1:
83+
char szPublicKeyHex[2 * DECOMPRESSED_PUBKEY_LENGTH + 1];
8584
fast_crypto_secp256k1_ec_pubkey_create([privateKeyHex UTF8String], szPublicKeyHex, compressed);
8685
NSString *publicKeyHex = [NSString stringWithUTF8String:szPublicKeyHex];
87-
free(szPublicKeyHex);
8886
resolve(publicKeyHex);
8987
}
9088

0 commit comments

Comments
 (0)