Skip to content

Commit 2037d27

Browse files
committed
Fixed a case when public key contained null bytes
1 parent b85f192 commit 2037d27

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

other/fun/strkey.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ void print_key(unsigned char *key)
5757

5858
int main(int argc, char *argv[])
5959
{
60-
unsigned char public_key[crypto_box_PUBLICKEYBYTES + 1]; // null terminator
60+
unsigned char public_key[crypto_box_PUBLICKEYBYTES]; // null terminator
6161
unsigned char secret_key[crypto_box_SECRETKEYBYTES];
6262
int offset = 0;
6363
size_t len;
64-
unsigned char desired_bin[crypto_box_PUBLICKEYBYTES + 1]; // null terminator
64+
unsigned char desired_bin[crypto_box_PUBLICKEYBYTES]; // null terminator
6565

6666
if (argc == 3) {
6767
offset = atoi(argv[1]);
@@ -89,23 +89,30 @@ int main(int argc, char *argv[])
8989
exit(1);
9090
}
9191

92-
desired_bin[len/2] = '\0';
93-
public_key[crypto_box_PUBLICKEYBYTES + 1] = '\0';
92+
len /= 2;
9493

9594
#ifdef PRINT_TRIES_COUNT
9695
long long unsigned int tries = 0;
9796
#endif
9897

9998
if (offset < 0) {
99+
int found = 0;
100100
do {
101101
#ifdef PRINT_TRIES_COUNT
102-
tries ++;
102+
tries ++;
103103
#endif
104104
crypto_box_keypair(public_key, secret_key);
105-
} while (strstr(public_key, desired_bin) == 0);
105+
int i;
106+
for (i = 0; i <= crypto_box_PUBLICKEYBYTES - len; i ++) {
107+
if (memcmp(public_key + i, desired_bin, len) == 0) {
108+
found = 1;
109+
break;
110+
}
111+
}
112+
} while (!found);
106113
} else {
107-
unsigned char *p = public_key + offset;
108-
len /= 2;
114+
unsigned char *p = public_key + offset;
115+
109116
do {
110117
#ifdef PRINT_TRIES_COUNT
111118
tries ++;
@@ -127,4 +134,4 @@ int main(int argc, char *argv[])
127134
#endif
128135

129136
return 0;
130-
}
137+
}

0 commit comments

Comments
 (0)