Skip to content

Commit

Permalink
Added publictopi6.c for calculating cjdns IP6 addresses from public k…
Browse files Browse the repository at this point in the history
…ey, fixed bug in DevUrandomRandomSeed which was only taking sizeof(uintptr_t) bytes of random, made LinuxRandomUuidSysctlRandomSeed work on linux since KERN_RANDOM is an enum.
  • Loading branch information
Caleb James DeLisle committed Feb 7, 2013
1 parent 892477d commit 1e3d50c
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -255,6 +255,13 @@ target_link_libraries(privatetopublic
util util
crypto crypto
) )
add_executable(publictoip6
publictoip6.c
)
target_link_libraries(publictoip6
util
cjdns-crypto-key
)




#INSTALL(TARGETS cjdroute RUNTIME DESTINATION bin) #INSTALL(TARGETS cjdroute RUNTIME DESTINATION bin)
Expand Down
6 changes: 3 additions & 3 deletions crypto/random/seed/DevUrandomRandomSeed.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@


static int get(struct RandomSeed* randomSeed, uint64_t output[8]) static int get(struct RandomSeed* randomSeed, uint64_t output[8])
{ {
Bits_memset(output, 0, sizeof(output)); Bits_memset(output, 0, 64);
int fd = -1; int fd = -1;
int tries = 0; int tries = 0;
while ((fd = open("/dev/urandom", O_RDONLY, 0)) < 0) { while ((fd = open("/dev/urandom", O_RDONLY, 0)) < 0) {
Expand All @@ -38,7 +38,7 @@ static int get(struct RandomSeed* randomSeed, uint64_t output[8])
} }
tries = 0; tries = 0;
uint8_t* buff = (uint8_t*) output; uint8_t* buff = (uint8_t*) output;
int count = sizeof(output); int count = 64;
while (count > 0) { while (count > 0) {
int r = read(fd, buff, count); int r = read(fd, buff, count);
if (r < 1) { if (r < 1) {
Expand All @@ -52,7 +52,7 @@ static int get(struct RandomSeed* randomSeed, uint64_t output[8])
count -= r; count -= r;
} }
close(fd); close(fd);
if (count == 0 && !Bits_isZero(output, sizeof(output))) { if (count == 0 && !Bits_isZero(output, 64)) {
return 0; return 0;
} }
return -1; return -1;
Expand Down
2 changes: 1 addition & 1 deletion crypto/random/seed/LinuxRandomUuidSysctlRandomSeed.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include <unistd.h> #include <unistd.h>
#include <sys/sysctl.h> #include <sys/sysctl.h>


#if defined(KERN_RANDOM) && defined(RANDOM_UUID) #ifdef Linux
static int getUUID(uint64_t output[2]) static int getUUID(uint64_t output[2])
{ {
int mib[] = { CTL_KERN, KERN_RANDOM, RANDOM_UUID }; int mib[] = { CTL_KERN, KERN_RANDOM, RANDOM_UUID };
Expand Down
54 changes: 54 additions & 0 deletions publictoip6.c
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,54 @@
/* vim: set expandtab ts=4 sw=4: */
/*
* You may redistribute this program and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "crypto/Key.h"
#include "util/AddrTools.h"

static int usage(char* appName)
{
printf("Usage: %s <public key>\n"
"\n"
"Get a cjdns IPv6 address from a public key.\n"
"The key should be in Base32 and end in '.k'.\n",
appName);
return 0;
}

int main(int argc, char** argv)
{
if (argc < 2) {
return usage(argv[0]);
}

uint8_t keyBytes[32];
uint8_t ip6Bytes[16];
String key = { .bytes = argv[1], .len = strlen(argv[1]) };

int ret = Key_parse(&key, keyBytes, ip6Bytes);
switch (ret) {
case Key_parse_TOO_SHORT: fprintf(stderr, "ERROR: Key_parse_TOO_SHORT\n"); return 1;
case Key_parse_MALFORMED: fprintf(stderr, "ERROR: Key_parse_MALFORMED\n"); return 1;
case Key_parse_DECODE_FAILED: fprintf(stderr, "ERROR: Key_parse_DECODE_FAILED\n"); return 1;
case Key_parse_INVALID: fprintf(stderr, "ERROR: Key_parse_INVALID\n"); return 1;
case 0: break;
default: fprintf(stderr, "ERROR: unknown error [%d]\n", ret); return 1;
}

uint8_t output[40] = {0};
AddrTools_printIp(output, ip6Bytes);
printf("%s\n", output);
return 0;
}

0 comments on commit 1e3d50c

Please sign in to comment.