Skip to content

Commit

Permalink
add brainwallet.io support
Browse files Browse the repository at this point in the history
  • Loading branch information
ryancdotorg committed Aug 29, 2015
1 parent 71d365d commit 642d217
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 29 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ brainflayer.o: brainflayer.c secp256k1/include/secp256k1.h

warpwallet.o: warpwallet.c scrypt-jane/scrypt-jane.h

brainwalletio.o: brainwalletio.c scrypt-jane/scrypt-jane.h

%.o: %.c
$(COMPILE) -c $< -o $@

Expand All @@ -37,7 +39,7 @@ blfchk: blfchk.o hex.o bloom.o
hex2blf: hex2blf.o hex.o bloom.o
$(COMPILE) -static $^ $(LIBS) -o $@

brainflayer: brainflayer.o hex.o bloom.o warpwallet.o secp256k1/.libs/libsecp256k1.a scrypt-jane/scrypt-jane.o
brainflayer: brainflayer.o hex.o bloom.o warpwallet.o brainwalletio.o secp256k1/.libs/libsecp256k1.a scrypt-jane/scrypt-jane.o
$(COMPILE) -static $^ $(LIBS) -o $@

brainflayer-alt: brainflayer.o hex.o bloom.o warpwallet.o brainwalletio.o secp256k1/.libs/libsecp256k1.a scrypt-jane/scrypt-jane.o
Expand Down
77 changes: 49 additions & 28 deletions brainflayer.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "hash160.h"

#include "warpwallet.h"
#include "brainwalletio.h"

static int brainflayer_is_init = 0;

Expand Down Expand Up @@ -150,20 +151,36 @@ static int hexpriv2hash160(unsigned char *hpriv, size_t hpriv_sz) {
return priv2hash160(unhex(hpriv, hpriv_sz, unhexed, sizeof(unhexed)));
}

static unsigned char *warpsalt;
static size_t warpsalt_sz;
static unsigned char *kdfsalt;
static size_t kdfsalt_sz;

static int warppass2hash160(unsigned char *pass, size_t pass_sz) {
int ret;
if ((ret = warpwallet(pass, pass_sz, warpsalt, warpsalt_sz, hash256)) != 0) return ret;
if ((ret = warpwallet(pass, pass_sz, kdfsalt, kdfsalt_sz, hash256)) != 0) return ret;
pass[pass_sz] = 0;
return priv2hash160(hash256);
}

static int bwiopass2hash160(unsigned char *pass, size_t pass_sz) {
int ret;
if ((ret = brainwalletio(pass, pass_sz, kdfsalt, kdfsalt_sz, hash256)) != 0) return ret;
pass[pass_sz] = 0;
return priv2hash160(hash256);
}

static unsigned char *warppass;
static size_t warppass_sz;
static unsigned char *kdfpass;
static size_t kdfpass_sz;

static int warpsalt2hash160(unsigned char *salt, size_t salt_sz) {
int ret;
if ((ret = warpwallet(warppass, warppass_sz, salt, salt_sz, hash256)) != 0) return ret;
if ((ret = warpwallet(kdfpass, kdfpass_sz, salt, salt_sz, hash256)) != 0) return ret;
salt[salt_sz] = 0;
return priv2hash160(hash256);
}

static int bwiosalt2hash160(unsigned char *salt, size_t salt_sz) {
int ret;
if ((ret = brainwalletio(kdfpass, kdfpass_sz, salt, salt_sz, hash256)) != 0) return ret;
salt[salt_sz] = 0;
return priv2hash160(hash256);
}
Expand Down Expand Up @@ -277,20 +294,10 @@ int main(int argc, char **argv) {
input2hash160 = &hexpriv2hash160;
} else if (strcmp(topt, "warp") == 0) {
spok = 1;
if (popt) {
warppass = popt;
warppass_sz = strlen(popt);
input2hash160 = &warpsalt2hash160;
} else {
if (sopt) {
warpsalt = sopt;
warpsalt_sz = strlen(warpsalt);
} else {
warpsalt = malloc(0);
warpsalt_sz = 0;
}
input2hash160 = &warppass2hash160;
}
input2hash160 = popt ? &warpsalt2hash160 : &warppass2hash160;
} else if (strcmp(topt, "bwio") == 0) {
spok = 1;
input2hash160 = popt ? &bwiosalt2hash160 : &bwiopass2hash160;
} else {
bail(1, "Unknown input type '%s'.\n", topt);
}
Expand All @@ -299,14 +306,28 @@ int main(int argc, char **argv) {
input2hash160 = &pass2hash160;
}

if (popt && !spok) {
bail(1, "Specifying a passphrase not supported with input type '%s'\n", topt);
}
if (sopt && !spok) {
bail(1, "Specifying a salt not supported with this input type '%s'\n", topt);
}
if (sopt && popt) {
bail(1, "Cannot specify both a salt and a passphrase\n");
if (spok) {
if (sopt && popt) {
bail(1, "Cannot specify both a salt and a passphrase\n");
}
if (popt) {
kdfpass = popt;
kdfpass_sz = strlen(popt);
} else {
if (sopt) {
kdfsalt = sopt;
kdfsalt_sz = strlen(kdfsalt);
} else {
kdfsalt = malloc(0);
kdfsalt_sz = 0;
}
}
} else {
if (popt) {
bail(1, "Specifying a passphrase not supported with input type '%s'\n", topt);
} else if (sopt) {
bail(1, "Specifying a salt not supported with this input type '%s'\n", topt);
}
}

if (bopt) {
Expand Down
45 changes: 45 additions & 0 deletions brainwalletio.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* Copyright (c) 2015 Ryan Castellucci, All Rights Reserved */
#include <time.h>
#include <unistd.h>
#include <assert.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <signal.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>

#include <openssl/evp.h>
#include <openssl/sha.h>
// crypto.h used for the version
#include <openssl/crypto.h>

#include "scrypt-jane/scrypt-jane.h"

#include "hex.h"

#define _SCRYPT_N (1<<18)
#define _SCRYPT_r 8
#define _SCRYPT_p 1

#define jane_scrypt(p, pl, s, ss, k, ks) \
scrypt(p, pl, s, ss, 17, 3, 0, k, ks)

static SHA256_CTX sha256_ctx;

int brainwalletio(unsigned char *pass, size_t pass_sz,
unsigned char *salt, size_t salt_sz,
unsigned char *out) {
unsigned char seed1[32], seed2[65];

int seed1_sz = sizeof(seed1), seed2_sz = (sizeof(seed2) - 1);

jane_scrypt(pass, pass_sz, salt, salt_sz, seed1, seed1_sz);
hex(seed1, seed1_sz, seed2, seed2_sz);
SHA256_Init(&sha256_ctx);
SHA256_Update(&sha256_ctx, seed2, seed2_sz);
SHA256_Final(out, &sha256_ctx);

return 0;
}
8 changes: 8 additions & 0 deletions brainwalletio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* Copyright (c) 2015 Ryan Castellucci, All Rights Reserved */
#ifndef __BRAINFLAYER_BRAINWALLETIO_H_
#define __BRAINFLAYER_BRAINWALLETIO_H_

int brainwalletio(unsigned char *, size_t, unsigned char *, size_t, unsigned char *);

/* vim: set ts=2 sw=2 et ai si: */
#endif /* __BRAINFLAYER_BRAINWALLETIO_H_ */

0 comments on commit 642d217

Please sign in to comment.