예제 #1
0
  public BigInteger calculateAgreement(CipherParameters pubKey) {
    ECPublicKeyParameters pub = (ECPublicKeyParameters) pubKey;
    ECPoint P = pub.getQ().multiply(key.getD());

    // if (p.isInfinity()) throw new RuntimeException("d*Q == infinity");

    return P.getX().toBigInteger();
  }
  private void storeKeyPair(ECKeyPair keyPair) {
    String privateKey = CMAccountUtils.encodeHex(keyPair.getPrivateKey().getD());
    ECPoint publicKey = keyPair.getPublicKey().getQ();

    ContentValues values = new ContentValues();
    values.put(CMAccountProvider.ECDHKeyStoreColumns.PRIVATE, privateKey.toString());
    values.put(
        CMAccountProvider.ECDHKeyStoreColumns.PUBLIC,
        CMAccountUtils.encodeHex(publicKey.getEncoded()));
    values.put(CMAccountProvider.ECDHKeyStoreColumns.KEY_ID, keyPair.getKeyId());
    mContext.getContentResolver().insert(CMAccountProvider.ECDH_CONTENT_URI, values);
  }
예제 #3
0
 private static RawKeyBytes deriveChildKeyBytesFromPublic(
     DeterministicKey parent, ChildNumber childNumber) throws HDDerivationException {
   checkArgument(!childNumber.isHardened(), "Can't use private derivation with public keys only.");
   byte[] parentPublicKey = ECKey.compressPoint(parent.getPubKeyPoint()).getEncoded();
   assert parentPublicKey.length == 33 : parentPublicKey.length;
   ByteBuffer data = ByteBuffer.allocate(37);
   data.put(parentPublicKey);
   data.putInt(childNumber.i());
   byte[] i = HDUtils.hmacSha512(parent.getChainCode(), data.array());
   assert i.length == 64 : i.length;
   byte[] il = Arrays.copyOfRange(i, 0, 32);
   byte[] chainCode = Arrays.copyOfRange(i, 32, 64);
   BigInteger ilInt = new BigInteger(1, il);
   assertLessThanN(ilInt, "Illegal derived key: I_L >= n");
   ECPoint Ki = ECKey.CURVE.getG().multiply(ilInt).add(parent.getPubKeyPoint());
   assertNonInfinity(Ki, "Illegal derived key: derived public key equals infinity.");
   return new RawKeyBytes(Ki.getEncoded(true), chainCode);
 }
  public byte[] encode() {

    byte[] publicKey = new byte[64];
    System.arraycopy(ephemeralPublicKey.getEncoded(false), 1, publicKey, 0, publicKey.length);

    byte[] publicBytes = RLP.encode(publicKey);
    byte[] nonceBytes = RLP.encode(nonce);
    byte[] versionBytes = RLP.encodeInt(version);

    return RLP.encodeList(publicBytes, nonceBytes, versionBytes);
  }
예제 #5
0
  private static RawKeyBytes deriveChildKeyBytes(DeterministicKey parent, ChildNumber childNumber)
      throws HDDerivationException {

    byte[] parentPublicKey = HDUtils.getBytes(parent.getPubPoint());
    assert parentPublicKey.length == 33 : parentPublicKey.length;
    ByteBuffer data = ByteBuffer.allocate(37);
    if (childNumber.isPrivateDerivation()) {
      data.put(parent.getPrivKeyBytes33());
    } else {
      data.put(parentPublicKey);
    }
    data.putInt(childNumber.getI());
    byte[] i = HDUtils.hmacSha512(parent.getChainCode(), data.array());
    assert i.length == 64 : i.length;
    byte[] il = Arrays.copyOfRange(i, 0, 32);
    byte[] chainCode = Arrays.copyOfRange(i, 32, 64);
    BigInteger ilInt = HDUtils.toBigInteger(il);
    assertLessThanN(ilInt, "Illegal derived key: I_L >= n");
    byte[] keyBytes;
    final BigInteger privAsFieldElement = parent.getPrivAsFieldElement();
    if (privAsFieldElement != null) {
      BigInteger ki = privAsFieldElement.add(ilInt).mod(HDUtils.getEcParams().getN());
      assertNonZero(ki, "Illegal derived key: derived private key equals 0.");
      keyBytes = ki.toByteArray();
    } else {
      checkArgument(
          !childNumber.isPrivateDerivation(),
          "Can't use private derivation with public keys only.");
      ECPoint Ki = HDUtils.getEcParams().getG().multiply(ilInt).add(parent.getPubPoint());
      checkArgument(
          !Ki.equals(HDUtils.getCurve().getInfinity()),
          "Illegal derived key: derived public key equals infinity.");
      keyBytes = HDUtils.toCompressed(Ki.getEncoded());
    }
    return new RawKeyBytes(keyBytes, chainCode);
  }
예제 #6
0
 private static void assertNonInfinity(ECPoint point, String errorMessage) {
   if (point.equals(ECKey.CURVE.getCurve().getInfinity()))
     throw new HDDerivationException(errorMessage);
 }