Ejemplo n.º 1
0
int main(){
	unsigned int s = (unsigned int)time(NULL);
	s = 1337544566;
	printf("Session = %ui\n",s);
	srand(s);
	CBNetworkParameters * net = CBNewNetworkParameters();
	net->networkCode = 0;
	CBEvents events;
	events.onErrorReceived = err;
	CBDependencies dep;
	dep.sha256 = sha256;
	CBString * addstr = CBNewStringByCopyingCString("1D5A1q5d192j5gYuWiP3CSE5fcaaZxe6E9");
	CBAddress * add = CBNewAddressFromString(addstr, false, &events, &dep);
	CBGetObjectVT(addstr)->release(&addstr);
	u_int8_t v = CBGetVersionChecksumBytesVT(add)->getVersion(add);
	if (v != 0) {
		printf("PRODUCTION NET VERSION DOES NOT MATCH %i != 0\n",v);
		return 1;
	}
	CBString * str = CBGetVersionChecksumBytesVT(add)->getString(add);
	if (strcmp(str->string, "1D5A1q5d192j5gYuWiP3CSE5fcaaZxe6E9")){
		printf("NOT CACHED STRING WRONG %s != 1D5A1q5d192j5gYuWiP3CSE5fcaaZxe6E9\n",str->string);
		return 1;
	}
	CBGetObjectVT(str)->release(&str);
	CBGetObjectVT(add)->release(&add);
	addstr = CBNewStringByCopyingCString("mhFwRrjRNt8hYeWtm9LwqCpCgXjF38RJqn");
	add = CBNewAddressFromString(addstr, false, &events, &dep);
	CBGetObjectVT(addstr)->release(&addstr);
	v = CBGetVersionChecksumBytesVT(add)->getVersion(add);
	if (v != 0x6f) {
		printf("TEST NET VERSION DOES NOT MATCH %i != 111\n",v);
		return 1;
	}
	CBGetObjectVT(add)->release(&add);
	addstr = CBNewStringByCopyingCString("19tknf38VozS4GfoxHe6vUP3NRghbGGT6H");
	add = CBNewAddressFromString(addstr, true, &events, &dep);
	CBGetObjectVT(addstr)->release(&addstr);
	str = CBGetVersionChecksumBytesVT(add)->getString(add);
	if (strcmp(str->string, "19tknf38VozS4GfoxHe6vUP3NRghbGGT6H")){
		printf("CACHED STRING WRONG %s != 19tknf38VozS4GfoxHe6vUP3NRghbGGT6H\n",str->string);
		return 1;
	}
	CBGetObjectVT(str)->release(&str);
	CBGetObjectVT(add)->release(&add);
	// Test building from fake RIPEMD160 hash
	u_int8_t * hash = malloc(20);
	for (int x = 0; x < 20; x++)
		hash[x] = rand();
	add = CBNewAddressFromRIPEMD160Hash(net, hash, false, &events, &dep);
	free(hash);
	v = CBGetVersionChecksumBytesVT(add)->getVersion(add);
	if (v != 0) {
		printf("PRODUCTION NET VERSION FOR RIPEMD160 TEST DOES NOT MATCH %i != 0\n",v);
		return 1;
	}
	CBGetObjectVT(add)->release(&add);
	hash = malloc(20);
	for (int x = 0; x < 20; x++)
		hash[x] = rand();
	net->networkCode = 0x6f;
	add = CBNewAddressFromRIPEMD160Hash(net, hash, false, &events, &dep);
	free(hash);
	v = CBGetVersionChecksumBytesVT(add)->getVersion(add);
	if (v != 0x6f) {
		printf("TEST NET VERSION FOR RIPEMD160 TEST DOES NOT MATCH %i != 111\n",v);
		return 1;
	}
	CBGetObjectVT(add)->release(&add);
	// Test CBNewAddressFromRIPEMD160Hash for pre-known address
	hash = malloc(20);
	for (int x = 0; x < 20; x++)
		hash[x] = x;
	net->networkCode = 0;
	add = CBNewAddressFromRIPEMD160Hash(net, hash, false, &events, &dep);
	free(hash);
	str = CBGetVersionChecksumBytesVT(add)->getString(add);
	if (strcmp(str->string, "112D2adLM3UKy4Z4giRbReR6gjWuvHUqB")){
		printf("RIPEMD160 TEST STRING WRONG %s != 112D2adLM3UKy4Z4giRbReR6gjWuvHUqB\n",str->string);
		return 1;
	}
	CBGetObjectVT(str)->release(&str);
	CBGetObjectVT(add)->release(&add);
	// Find "1got", speed test
	hash = malloc(20);
	while (1) {
		for (int x = 0; x < 20; x++)
			hash[x] = rand();
		add = CBNewAddressFromRIPEMD160Hash(net, hash, true, &events, &dep);
		str = CBGetVersionChecksumBytesVT(add)->getString(add);
		if (!strncmp(str->string, "1got", 4)) {
			printf("GOT STRING = %s\n",str->string);
			break;
		}
		CBGetObjectVT(str)->release(&str);
		CBGetObjectVT(add)->release(&add);
	}
	free(hash);
	return 0;
}
Ejemplo n.º 2
0
void checkKey(CBHDKey * key, uint8_t x, uint8_t y){
	// Check version bytes
	if (key->versionBytes != CB_HD_KEY_VERSION_PROD_PRIVATE) {
		printf("VERSION BYTES FAIL AT %u - %u\n", x, y);
		exit(EXIT_FAILURE);
	}
	// Check address
	CBAddress * address = CBNewAddressFromRIPEMD160Hash(CBHDKeyGetHash(key), CB_NETWORK_PRODUCTION, false);
	CBByteArray * str = CBChecksumBytesGetString(CBGetChecksumBytes(address));
	CBReleaseObject(address);
	if (memcmp(CBByteArrayGetData(str), testVectors[x][y].addr, 34) != 0) {
		printf("ADDR FAIL AT %u - %u\n", x, y);
		exit(EXIT_FAILURE);
	}
	CBReleaseObject(str);
	// Check WIF
	CBWIF * wif = CBHDKeyGetWIF(key);
	str = CBChecksumBytesGetString(CBGetChecksumBytes(wif));
	CBReleaseObject(wif);
	if (memcmp(CBByteArrayGetData(str), testVectors[x][y].WIF, 52) != 0) {
		printf("WIF FAIL AT %u - %u\n", x, y);
		exit(EXIT_FAILURE);
	}
	CBReleaseObject(str);
	// Check chain code
	if (memcmp(key->chainCode, testVectors[x][y].chainCode, 32) != 0) {
		printf("CHAIN CODE FAIL AT %u - %u\n", x, y);
		exit(EXIT_FAILURE);
	}
	// Check child ID
	if (memcmp(&key->childID, &testVectors[x][y].childID, sizeof(key->childID)) != 0) {
		printf("CHILD ID FAIL AT %u - %u\n", x, y);
		exit(EXIT_FAILURE);
	}
	// Check depth
	if (key->depth != y) {
		printf("DEPTH FAIL AT %u - %u\n", x, y);
		exit(EXIT_FAILURE);
	}
	// Check serialisation of private key
	uint8_t * keyData = malloc(82);
	CBHDKeySerialise(key, keyData);
	CBChecksumBytes * checksumBytes = CBNewChecksumBytesFromBytes(keyData, 82, false);
	str = CBChecksumBytesGetString(checksumBytes);
	CBReleaseObject(checksumBytes);
	if (memcmp(CBByteArrayGetData(str), testVectors[x][y].privString, 111) != 0) {
		printf("PRIVATE KEY STRING FAIL AT %u - %u\n", x, y);
		exit(EXIT_FAILURE);
	}
	CBReleaseObject(str);
	// Check serialisation of public key
	key->versionBytes = CB_HD_KEY_VERSION_PROD_PUBLIC;
	keyData = malloc(82);
	CBHDKeySerialise(key, keyData);
	checksumBytes = CBNewChecksumBytesFromBytes(keyData, 82, false);
	str = CBChecksumBytesGetString(checksumBytes);
	CBReleaseObject(checksumBytes);
	if (memcmp(CBByteArrayGetData(str), testVectors[x][y].pubString, 111) != 0) {
		printf("PUBLIC KEY STRING FAIL AT %u - %u\n", x, y);
		exit(EXIT_FAILURE);
	}
	CBReleaseObject(str);
	// Make private again
	key->versionBytes = CB_HD_KEY_VERSION_PROD_PRIVATE;
}
Ejemplo n.º 3
0
int main(){
	CBByteArray * walletKeyString = CBNewByteArrayFromString("xpub6DRhpXssnj7X6CwJgseK9oyFxSC8jk6nJz2SWkf5pjsQs12xv89Dfr627TtaZKkFbG6Aq23fmaNaf5KRo9iGfEXTTXvtd6gsXJTB8Sdah3B", false);
    CBChecksumBytes * walletKeyData = CBNewChecksumBytesFromString(walletKeyString, false);
    CBHDKey * cbkey = CBNewHDKeyFromData(CBByteArrayGetData(CBGetByteArray(walletKeyData)));
	CBAddress * address = CBNewAddressFromRIPEMD160Hash(CBHDKeyGetHash(cbkey), CB_NETWORK_PRODUCTION, false);
	CBByteArray * str = CBChecksumBytesGetString(CBGetChecksumBytes(address));
	printf("%s\n", CBByteArrayGetData(str));
	CBReleaseObject(address);
	// Test type
	if (CBHDKeyGetType(CB_HD_KEY_VERSION_PROD_PRIVATE) != CB_HD_KEY_TYPE_PRIVATE) {
		printf("CB_HD_KEY_VERSION_PROD_PRIVATE TYPE FAIL\n");
		return EXIT_FAILURE;
	}
	if (CBHDKeyGetType(CB_HD_KEY_VERSION_PROD_PUBLIC) != CB_HD_KEY_TYPE_PUBLIC) {
		printf("CB_HD_KEY_VERSION_PROD_PUBLIC TYPE FAIL\n");
		return EXIT_FAILURE;
	}
	if (CBHDKeyGetType(CB_HD_KEY_VERSION_TEST_PRIVATE) != CB_HD_KEY_TYPE_PRIVATE) {
		printf("CB_HD_KEY_VERSION_TEST_PRIVATE TYPE FAIL\n");
		return EXIT_FAILURE;
	}
	if (CBHDKeyGetType(CB_HD_KEY_VERSION_TEST_PUBLIC) != CB_HD_KEY_TYPE_PUBLIC) {
		printf("CB_HD_KEY_VERSION_TEST_PUBLIC TYPE FAIL\n");
		return EXIT_FAILURE;
	}
	// Test HMAC-SHA512
	uint8_t hash[64];
	CBHDKeyHmacSha512((uint8_t [37]){0x2f, 0xf7, 0xd6, 0x9f, 0x7a, 0x59, 0x0b, 0xb0, 0x5e, 0x68, 0xd1, 0xdc, 0x0f, 0xcf, 0x8d, 0xc2, 0x17, 0x59, 0xc9, 0x39, 0xbb, 0x6b, 0x9b, 0x02, 0x0f, 0x65, 0x5d, 0x53, 0x85, 0x3c, 0xb5, 0xc2, 0x14, 0x61, 0x4b, 0x24, 0x42}, (uint8_t [32]){0xa2, 0x55, 0x21, 0xe3, 0xc5, 0x5b, 0x65, 0xd1, 0xcf, 0x25, 0x4b, 0x6c, 0x85, 0x23, 0xdc, 0xbf, 0x89, 0x46, 0x8d, 0x1f, 0x09, 0x1f, 0x15, 0x87, 0x6b, 0xbb, 0xc7, 0xfd, 0xd5, 0x44, 0x28, 0x43}, hash);
	if (memcmp(hash, (uint8_t [64]){0xfa, 0xa7, 0x9d, 0x85, 0xe0, 0xe4, 0x3d, 0xae, 0x8c, 0x3f, 0x99, 0xf0, 0x70, 0xdf, 0x97, 0x56, 0x2b, 0x3f, 0xbb, 0x17, 0x35, 0x20, 0xe0, 0x87, 0x32, 0xa6, 0x64, 0xca, 0xd4, 0x55, 0x0b, 0xbe, 0xc1, 0x11, 0xe5, 0xf8, 0x80, 0xdb, 0xb7, 0x3d, 0x67, 0x74, 0xbb, 0xc2, 0x9f, 0x67, 0xd9, 0x67, 0xaa, 0x10, 0xac, 0x60, 0x18, 0x90, 0x7f, 0x35, 0x53, 0xe3, 0x21, 0x38, 0xf6, 0x5b, 0xbe, 0x69}, 64) != 0) {
		printf("HMAC FAIL\n");
		return EXIT_FAILURE;
	}
	for (uint8_t x = 0; x < NUM_TEST_VECTORS; x++) {
		// Deserialise private key
		CBByteArray * masterString = CBNewByteArrayFromString(testVectors[x][0].privString, true);
		CBChecksumBytes * masterData = CBNewChecksumBytesFromString(masterString, false);
		CBReleaseObject(masterString);
		CBHDKey * key = CBNewHDKeyFromData(CBByteArrayGetData(CBGetByteArray(masterData)));
		CBReleaseObject(masterData);
		checkKey(key, x, 0);
		for (uint8_t y = 0; y < NUM_CHILDREN; y++) {
			if (testVectors[x][y+1].childID.priv == false) {
				// Derive public child and check public key is correct by address
				CBHDKey * newKey = CBNewHDKey(false);
				key->versionBytes = CB_HD_KEY_VERSION_PROD_PUBLIC;
				CBHDKeyDeriveChild(key, testVectors[x][y+1].childID, newKey);
				key->versionBytes = CB_HD_KEY_VERSION_PROD_PRIVATE;
				CBAddress * address = CBNewAddressFromRIPEMD160Hash(CBHDKeyGetHash(newKey), CB_NETWORK_PRODUCTION, false);
				CBByteArray * str = CBChecksumBytesGetString(CBGetChecksumBytes(address));
				CBReleaseObject(address);
				if (memcmp(CBByteArrayGetData(str), testVectors[x][y + 1].addr, 34) != 0) {
					printf("ADDR FROM PUB FAIL AT %u - %u\n", x, y + 1);
					exit(EXIT_FAILURE);
				}
				CBReleaseObject(str);
				// Check serialisation of public key
				uint8_t * keyData = malloc(82);
				CBHDKeySerialise(newKey, keyData);
				CBChecksumBytes * checksumBytes = CBNewChecksumBytesFromBytes(keyData, 82, false);
				str = CBChecksumBytesGetString(checksumBytes);
				CBReleaseObject(checksumBytes);
				if (memcmp(CBByteArrayGetData(str), testVectors[x][y+1].pubString, 111) != 0) {
					printf("PUBLIC KEY STRING FROM PUB FAIL AT %u - %u\n", x, y);
					exit(EXIT_FAILURE);
				}
				CBReleaseObject(str);
				free(newKey);
			}
			// Derive private child
			CBHDKey * newKey = CBNewHDKey(true);
			CBHDKeyDeriveChild(key, testVectors[x][y+1].childID, newKey);
			free(key);
			key = newKey;
			checkKey(key, x, y+1);
		}
		free(key);
	}
	return EXIT_SUCCESS;
}
Ejemplo n.º 4
0
int main(){
	printf("OpenSSL version: %s\n", OPENSSL_VERSION_TEXT);
	printf("Enter the number of keys: ");
	fflush(stdout);
	char stringMatch[31];
	getLine(stringMatch);
	unsigned long int i = strtol(stringMatch, NULL, 0);
	printf("Enter a string of text for the key (30 max): ");
	fflush(stdout);
	getLine(stringMatch);
	printf("Waiting for entropy... Move the cursor around...\n");
	fflush(stdout);
	char entropy[32];
	FILE * f = fopen("/dev/random", "r");
	if (fread(entropy, 32, 1, f) != 1){
		printf("FAILURING GETTING ENTROPY!");
		return 1;
	}
	RAND_add(entropy, 32, 32);
	fclose(f);
	printf("Making %lu addresses for \"%s\"\n\n", i, stringMatch);
	EC_KEY * key = EC_KEY_new_by_curve_name(NID_secp256k1);
	uint8_t * pubKey = NULL;
	int pubSize = 0;
	uint8_t * privKey = NULL;
	int privSize = 0;
	uint8_t * shaHash = malloc(32);
	uint8_t * ripemdHash = malloc(20);
	for (unsigned int x = 0; x < i;) {
		if(! EC_KEY_generate_key(key)){
			printf("GENERATE KEY FAIL\n"); 
			return 1;
		}
		int pubSizeNew = i2o_ECPublicKey(key, NULL);
		if(! pubSizeNew){
			printf("PUB KEY TO DATA ZERO\n"); 
			return 1;
		}
		if (pubSizeNew != pubSize) {
			pubSize = pubSizeNew;
			pubKey = realloc(pubKey, pubSize);
		}
		uint8_t * pubKey2 = pubKey;
		if(i2o_ECPublicKey(key, &pubKey2) != pubSize){
			printf("PUB KEY TO DATA FAIL\n");
			return 1;
		}
		SHA256(pubKey, pubSize, shaHash);
		RIPEMD160(shaHash, 32, ripemdHash);
		CBAddress * address = CBNewAddressFromRIPEMD160Hash(ripemdHash, CB_PRODUCTION_NETWORK_BYTE, false, err);
		CBByteArray * string = CBChecksumBytesGetString(CBGetChecksumBytes(address));
		CBReleaseObject(address);
		bool match = true;
		uint8_t offset = 1;
		size_t matchSize = strlen(stringMatch);
		for (uint8_t y = 0; y < matchSize;) {
			char other = islower(stringMatch[y]) ? toupper(stringMatch[y]) : (isupper(stringMatch[y])? tolower(stringMatch[y]) : '\0');
			if (CBByteArrayGetByte(string, y+offset) != stringMatch[y] && CBByteArrayGetByte(string, y+offset) != other) {
				offset++;
				y = 0;
				if (string->length < matchSize + offset) {
					match = false;
					break;
				}
			}else y++;
		}
		if (match) {
			// Get private key
			const BIGNUM * privKeyNum = EC_KEY_get0_private_key(key);
			if (! privKeyNum) {
				printf("PRIV KEY TO BN FAIL\n");
			}
			int privSizeNew = BN_num_bytes(privKeyNum);
			if (privSizeNew != privSize) {
				privSize = privSizeNew;
				privKey = realloc(privKey, privSize);
			}
			int res = BN_bn2bin(privKeyNum, privKey);
			if (res != privSize) {
				printf("PRIV KEY TO DATA FAIL\n");
			}
			// Print data to stdout
			printf("Private key (hex): ");
			for (int x = 0; x < privSize; x++) {
				printf(" %.2X", privKey[x]);
			}
			printf("\nPublic key (hex): ");
			for (int x = 0; x < pubSize; x++) {
				printf(" %.2X", pubKey[x]);
			}
			printf("\nAddress (base-58): %s\n\n", CBByteArrayGetData(string));
			x++; // Move to next
		}
		CBReleaseObject(string);
	}
	free(shaHash);
	free(ripemdHash);
	EC_KEY_free(key);
	return 0;
}