Beispiel #1
0
bool CBHDKeyDeriveChild(CBHDKey * parentKey, CBHDKeyChildID childID, CBHDKey * childKey){
	uint8_t hash[64], inputData[37];
	CBHDKeyType type = CBHDKeyGetType(parentKey->versionBytes);
	// Add childID to inputData
	CBInt32ToArrayBigEndian(inputData, 33, CBHDKeyGetChildNumber(childID));
	if (childID.priv) {
		// Private key derivation
		// Parent must be private
		if (type != CB_HD_KEY_TYPE_PRIVATE) {
			CBLogError("Attempting to derive a public-key without public-key derivation, or the key was of an unknown type.");
			return false;
		}
		// Add private key
		inputData[0] = 0;
		memcpy(inputData + 1, CBHDKeyGetPrivateKey(parentKey), 32);
	}else{
		// Public key derivation
		// Can be public or private
		if (type == CB_HD_KEY_TYPE_UNKNOWN) {
			CBLogError("Attempting to derive a key from an unknown key.");
			return false;
		}
		// Add the public key
		memcpy(inputData, CBHDKeyGetPublicKey(parentKey), 33);
	}
	// Get HMAC-SHA512 hash
	CBHDKeyHmacSha512(inputData, parentKey->chainCode, hash);
	// Copy over chain code, childID and version bytes
	memcpy(childKey->chainCode, hash + 32, 32);
	childKey->childID = childID;
	childKey->versionBytes = parentKey->versionBytes;
	// Set fingerprint of parent
	memcpy(childKey->parentFingerprint, CBHDKeyGetHash(parentKey), 4);
	// Set depth
	childKey->depth = parentKey->depth + 1;
	// Calculate key
	if (type == CB_HD_KEY_TYPE_PRIVATE) {
		// Calculating the private key
		// Add private key to first 32 bytes and modulo the order the curve
		// Split into four 64bit integers and add each one
		bool overflow = 0;
		for (uint8_t x = 4; x--;) {
			uint64_t a = CBArrayToInt64BigEndian(hash, 8*x);
			uint64_t b = CBArrayToInt64BigEndian(CBHDKeyGetPrivateKey(parentKey), 8*x) + overflow;
			uint64_t c = a + b;
			overflow = (c < b)? 1 : 0;
			CBInt64ToArrayBigEndian(CBHDKeyGetPrivateKey(childKey), 8*x, c);
		}
		if (overflow || memcmp(CBHDKeyGetPrivateKey(childKey), CB_CURVE_ORDER, 32) > 0) {
			// Take away CB_CURVE_ORDER
			bool carry = 0;
			for (uint8_t x = 4; x--;) {
				uint64_t a = CBArrayToInt64BigEndian(CBHDKeyGetPrivateKey(childKey), 8*x);
				uint64_t b = CBArrayToInt64BigEndian(CB_CURVE_ORDER, 8*x) + carry;
				carry = a < b;
				a -= b;
				CBInt64ToArrayBigEndian(CBHDKeyGetPrivateKey(childKey), 8*x, a);
			}
		}
		// Derive public key from private key
		CBKeyGetPublicKey(childKey->keyPair->privkey, CBHDKeyGetPublicKey(childKey));
		// Do not bother checking validity???
	}else{
		// Calculating the public key
		// Multiply the first 32 bytes by the base point (derive public key) and add to the public key point.
		CBKeyGetPublicKey(hash, CBHDKeyGetPublicKey(childKey));
		CBAddPoints(CBHDKeyGetPublicKey(childKey), CBHDKeyGetPublicKey(parentKey));
	}
	return true;
}
Beispiel #2
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;
}