public function InMemoryIdentityKeyStore() { $this->trustedKeys = []; $identityKeyPairKeys = Curve::generateKeyPair(); $this->identityKeyPair = new IdentityKeyPair(new IdentityKey($identityKeyPairKeys->getPublicKey()), $identityKeyPairKeys->getPrivateKey()); $this->localRegistrationId = KeyHelper::generateRegistrationId(); }
public function KeyExchangeMessage($messageVersion = null, $sequence = null, $flags = null, $baseKey = null, $baseKeySignature = null, $ratchetKey = null, $identityKey = null, $serialized = null) { /* :type messageVersion: int :type sequence: int :type flags:int :type baseKey: ECPublicKey :type baseKeySignature: bytearray :type ratchetKey: ECPublicKey :type identityKey: IdentityKey :type serialized: bytearray */ if ($serialized == null) { $this->supportedVersion = CiphertextMessage::CURRENT_VERSION; $this->version = $messageVersion; $this->sequence = $sequence; $this->flags = $flags; $this->baseKey = $baseKey; $this->baseKeySignature = $baseKeySignature; $this->ratchetKey = $ratchetKey; $this->identityKey = $identityKey; $version = ByteUtil::intsToByteHighAndLow($this->version, $this->supportedVersion); $keyExchangeMessage = new Textsecure_KeyExchangeMessage(); $keyExchangeMessage->setId($this->sequence << 5 | $this->flags); $keyExchangeMessage->setBaseKey($baseKey->serialize()); $keyExchangeMessage->setRatchetKey($ratchetKey->serialize()); $keyExchangeMessage->setIdentityKey($identityKey->serialize()); if ($messageVersion >= 3) { $keyExchangeMessage->setBaseKeySignature($baseKeySignature); } $this->serialized = ByteUtil::combine([chr((int) $version), $keyExchangeMessage->serializeToString()]); } else { try { $parts = ByteUtil::split($serialized, 1, strlen($serialized) - 1); $this->version = ByteUtil::highBitsToInt(ord($parts[0][0])); $this->supportedVersion = ByteUtil::lowBitsToInt(ord($parts[0][0])); if ($this->version <= CiphertextMessage::UNSUPPORTED_VERSION) { throw new LegacyMessageException('Unsupported legacy version: ' . $this->version); } if ($this->version > CiphertextMessage::CURRENT_VERSION) { throw new InvalidVersionException('Unkown version: ' . $this->version); } $message = new Textsecure_KeyExchangeMessage(); $message->parseFromString($parts[1]); if ($message->getId() == null || $message->getBaseKey() == null || $message->getRatchetKey() == null || $message->getIdentityKey() == null || $this->version >= 3 && $message->getBaseKeySignature() == null) { throw new InvalidMessageException('Some required fields are missing!'); } $this->sequence = $message->getId() >> 5; $this->flags = $message->getId() & 0x1f; $this->serialized = $serialized; $this->baseKey = Curve::decodePoint($message->getBaseKey(), 0); $this->baseKeySignature = $message->getBaseKeySignature(); $this->ratchetKey = Curve::decodePoint($message->getRatchetKey(), 0); $this->identityKey = new IdentityKey($message->getIdentityKey(), 0); } catch (InvalidKeyException $ex) { throw new InvalidMessageException($ex->getMessage()); } } }
public function IdentityKey($publicKeyOrBytes, $offset = null) { if ($offset === null) { $this->publicKey = $publicKeyOrBytes; } else { $this->publicKey = Curve::decodePoint($publicKeyOrBytes, $offset); } }
public function createChain($ECPublicKey_theirRatchetKey, $ECKeyPair_ourRatchetKey) { $sharedSecret = Curve::calculateAgreement($ECPublicKey_theirRatchetKey, $ECKeyPair_ourRatchetKey->getPrivateKey()); $derivedSecretBytes = $this->kdf->deriveSecrets($sharedSecret, 'WhisperRatchet', DerivedRootSecrets::SIZE, $this->key); $derivedSecrets = new DerivedRootSecrets($derivedSecretBytes); $newRootKey = new self($this->kdf, $derivedSecrets->getRootKey()); $newChainKey = new ChainKey($this->kdf, $derivedSecrets->getChainKey(), 0); return [$newRootKey, $newChainKey]; }
public function getKeyPair() { try { $publicKey = Curve::decodePoint($this->structure->getPublicKey(), 0); $privateKey = Curve::decodePrivatePoint($this->structure->getPrivateKey()); return new ECKeyPair($publicKey, $privateKey); } catch (InvalidKeyException $e) { throw new Exception($e->getMessage()); } }
public function IdentityKeyPair($publicKey = null, $privateKey = null, $serialized = null) { if ($serialized == null) { $this->publicKey = $publicKey; $this->privateKey = $privateKey; } else { $structure = new Textsecure_IdentityKeyPairStructure(); $structure->parseFromString($serialized); $this->publicKey = new IdentityKey($structure->getPublicKey(), 0); $this->privateKey = Curve::decodePrivatePoint($structure->getPrivateKey()); } }
public function WhisperMessage($messageVersion = null, $macKey = null, $senderRatchetKey = null, $counter = null, $previousCounter = null, $cipherText = null, $senderIdentityKey = null, $receiverIdentityKey = null, $serialized = null) { if ($serialized == null) { $version = ByteUtil::intsToByteHighAndLow($messageVersion, self::CURRENT_VERSION); $proto_message = new Textsecure_WhisperMessage(); $proto_message->setRatchetKey((string) $senderRatchetKey->serialize()); $proto_message->setCounter($counter); $proto_message->setPreviousCounter($previousCounter); $proto_message->setCiphertext($cipherText); $message = $proto_message->serializeToString(); $mac = $this->getMac($messageVersion, $senderIdentityKey, $receiverIdentityKey, $macKey, ByteUtil::combine([chr((int) $version), $message])); $this->serialized = ByteUtil::combine([chr((int) $version), $message, $mac]); $this->senderRatchetKey = $senderRatchetKey; $this->counter = $counter; $this->previousCounter = $previousCounter; $this->cipherText = $cipherText; $this->messageVersion = $messageVersion; } else { try { $messageParts = ByteUtil::split($serialized, 1, strlen($serialized) - 1 - self::MAC_LENGTH, self::MAC_LENGTH); $version = ord($messageParts[0][0]); $message = $messageParts[1]; $mac = $messageParts[2]; if (ByteUtil::highBitsToInt($version) <= self::UNSUPPORTED_VERSION) { throw new LegacyMessageException("Legacy message " . ByteUtil::highBitsToInt($version)); } if (ByteUtil::highBitsToInt($version) > self::CURRENT_VERSION) { throw new InvalidMessageException("Unknown version: " . ByteUtil::highBitsToInt($version)); } $proto_message = new Textsecure_WhisperMessage(); try { $proto_message->parseFromString($message); } catch (Exception $ex) { throw new InvalidMessageException("Incomplete message."); } if ($proto_message->getCiphertext() === null || $proto_message->getCounter() === null || $proto_message->getRatchetKey() == null) { throw new InvalidMessageException("Incomplete message."); } $this->serialized = $serialized; $this->senderRatchetKey = Curve::decodePoint($proto_message->getRatchetKey(), 0); $this->messageVersion = ByteUtil::highBitsToInt($version); $this->counter = $proto_message->getCounter(); $this->previousCounter = $proto_message->getPreviousCounter(); $this->cipherText = $proto_message->getCiphertext(); } catch (Exception $ex) { throw new InvalidMessageException($ex->getMessage()); } } }
public function PreKeyWhisperMessage($messageVersion = null, $registrationId = null, $preKeyId = null, $signedPreKeyId = null, $ecPublicBaseKey = null, $identityKey = null, $whisperMessage = null, $serialized = null) { if ($serialized != null) { $this->version = ByteUtil::highBitsToInt($serialized[0]); if ($this->version > self::CURRENT_VERSION) { throw new InvalidVersionException("Unknown version " . $this->version); } $preKeyWhisperMessage = new Textsecure_PreKeyWhisperMessage(); $preKeyWhisperMessage->parseFromString(substr($serialized, 1)); if ($this->version == 2 && $preKeyWhisperMessage->getPreKeyId() == null || $this->version == 3 && $preKeyWhisperMessage->getSignedPreKeyId() == null || $preKeyWhisperMessage->getBaseKey() == null || $preKeyWhisperMessage->getIdentityKey() == null || $preKeyWhisperMessage->getMessage() == null) { throw new InvalidMessageException("Incomplete message"); } $this->serialized = $serialized; $this->registrationId = $preKeyWhisperMessage->getRegistrationId(); $this->preKeyId = $preKeyWhisperMessage->getPreKeyId(); $this->signedPreKeyId = $preKeyWhisperMessage->getSignedPreKeyId(); $this->baseKey = Curve::decodePoint((string) $preKeyWhisperMessage->getBaseKey(), 0); $this->identityKey = new IdentityKey(Curve::decodePoint((string) $preKeyWhisperMessage->getIdentityKey(), 0)); $this->message = new WhisperMessage(null, null, null, null, null, null, null, null, $preKeyWhisperMessage->getMessage()); } else { try { $this->version = $messageVersion; $this->registrationId = $registrationId; $this->preKeyId = $preKeyId; $this->signedPreKeyId = $signedPreKeyId; $this->baseKey = $ecPublicBaseKey; $this->identityKey = $identityKey; $this->message = $whisperMessage; $builder = new Textsecure_PreKeyWhisperMessage(); $builder->setSignedPreKeyId($this->signedPreKeyId); $builder->setBaseKey($this->baseKey->serialize()); $builder->setIdentityKey($this->identityKey->serialize()); $builder->setMessage($whisperMessage->serialize()); $builder->setRegistrationId($this->registrationId); if ($preKeyId != null) { $builder->setPreKeyId($preKeyId); } $versionBytes = ByteUtil::intsToByteHighAndLow($this->version, self::CURRENT_VERSION); $messageBytes = $builder->serializeToString(); $this->serialized = ByteUtil::combine([chr($versionBytes), $messageBytes]); } catch (Exception $ex) { throw new InvalidMessageException($ex->getMessage() . " - " . $ex->getLine() . " - " . $ex->getFile()); } } }
public function SenderKeyDistributionMessage($id = null, $iteration = null, $chainKey = null, $signatureKey = null, $serialized = null) { if ($serialized == null) { $version = ByteUtil::intsToByteHighAndLow(self::CURRENT_VERSION, self::CURRENT_VERSION); $this->id = $id; $this->iteration = $iteration; $this->chainKey = $chainKey; $this->signatureKey = $signatureKey; $proto_skdm = new Textsecure_SenderKeyDistributionMessage(); $proto_skdm->setId($id); $proto_skdm->setIteration($iteration); $proto_skdm->setChainKey((string) $chainKey); $proto_skdm->setSigningKey((string) $signatureKey->serialize()); $this->serialized = chr($version) . $proto_skdm->serializeToString(); } else { $parts = ByteUtil::split($serialized, 1, strlen($serialized) - 1); $version = ord($parts[0][0]); $message = $parts[1]; if (ByteUtil::highBitsToInt($version) < self::CURRENT_VERSION) { throw new LegacyMessageException("Legacy message: " + ByteUtil::highBitsToInt($version)); } if (ByteUtil::highBitsToInt($version) > self::CURRENT_VERSION) { throw new InvalidMessageException("Unknown version: " + ByteUtil::highBitsToInt($version)); } $proto_skdm = new Textsecure_SenderKeyDistributionMessage(); try { $proto_skdm->parseFromString($message); } catch (Exception $ex) { throw new InvalidMessageException("Incomplete message."); } if ($proto_skdm->getId() === null || $proto_skdm->getIteration() === null || $proto_skdm->getChainKey() === null || $proto_skdm->getSigningKey() === null) { throw new InvalidMessageException("Incomplete message."); } $this->serialized = $serialized; $this->id = $proto_skdm->getId(); $this->iteration = $proto_skdm->getIteration(); $this->chainKey = $proto_skdm->getChainKey(); $this->signatureKey = Curve::decodePoint($proto_skdm->getSigningKey(), 0); } }
$ge->addColor("black", 0, 0, 0); $ge->addColor("red", 255, 0, 0); $ge->addColor("green", 0, 255, 0); $ge->addColor("blue", 0, 0, 255); $gobjs = array(); $gobjs[] = new Line("black", 10, 5, 100, 200); $gobjs[] = new Line("blue", 200, 150, 390, 380); $gobjs[] = new Line("red", 60, 40, 10, 300); $gobjs[] = new Line("green", 5, 390, 390, 10); foreach ($gobjs as $gobj) { $gobj->render($ge); } $img = "/tmp/test.png"; //$ge->saveAsPng( $img ); //$ge->showPng(); $curv = new Curve(array(100, 100, 100)); $curv->SetYarr($sfr->rfrqs); $curv->render($ge); $curv->SetYarr($sfr6->rfrqs); $curv->render($ge); //$curv->SetYarr( $sfr3->rfrqs ); //$curv->render( $ge ); //$curv->SetYarr( $sfr4->rfrqs ); $curv->render($ge); //$curv->SetYarr( $sfr5->rfrqs ); $curv->render($ge); $stam = new StatisticMatch(); $stam->match2($sfr->words, $sfr6->words); $mat = new MatchLines(array(0, 0, 255)); $mat->SetPairs($stam->pairs); $mat->render($ge);
protected function initializeSessionsV3($aliceSessionState, $bobSessionState) { $aliceIdentityKeyPair = Curve::generateKeyPair(); $aliceIdentityKey = new IdentityKeyPair(new IdentityKey($aliceIdentityKeyPair->getPublicKey()), $aliceIdentityKeyPair->getPrivateKey()); $aliceBaseKey = Curve::generateKeyPair(); $aliceEphemeralKey = Curve::generateKeyPair(); $alicePreKey = $aliceBaseKey; $bobIdentityKeyPair = Curve::generateKeyPair(); $bobIdentityKey = new IdentityKeyPair(new IdentityKey($bobIdentityKeyPair->getPublicKey()), $bobIdentityKeyPair->getPrivateKey()); $bobBaseKey = Curve::generateKeyPair(); $bobEphemeralKey = $bobBaseKey; $bobPreKey = Curve::generateKeyPair(); $aliceParameters = AliceAxolotlParameters::newBuilder()->setOurBaseKey($aliceBaseKey)->setOurIdentityKey($aliceIdentityKey)->setTheirOneTimePreKey(null)->setTheirRatchetKey($bobEphemeralKey->getPublicKey())->setTheirSignedPreKey($bobBaseKey->getPublicKey())->setTheirIdentityKey($bobIdentityKey->getPublicKey())->create(); $bobParameters = BobAxolotlParameters::newBuilder()->setOurRatchetKey($bobEphemeralKey)->setOurSignedPreKey($bobBaseKey)->setOurOneTimePreKey(null)->setOurIdentityKey($bobIdentityKey)->setTheirIdentityKey($aliceIdentityKey->getPublicKey())->setTheirBaseKey($aliceBaseKey->getPublicKey())->create(); RatchetingSession::initializeSessionAsAlice($aliceSessionState, 3, $aliceParameters); RatchetingSession::initializeSessionAsBob($bobSessionState, 3, $bobParameters); }
public function processInitKeyExchangeMessage() { try { $sequence = KeyHelper::getRandomSequence(65534) + 1; $flags = KeyExchangeMessage::INITIATE_FLAG; $baseKey = Curve::generateKeyPair(); $ratchetKey = Curve::generateKeyPair(); $identityKey = $this->identityKeyStore->getIdentityKeyPair(); $baseKeySignature = Curve::calculateSignature($identityKey->getPrivateKey(), $baseKey->getPublicKey()->serialize()); $sessionRecord = $this->sessionStore->loadSession($this->recipientId, $this->deviceId); $sessionRecord->getSessionState()->setPendingKeyExchange($sequence, $baseKey, $ratchetKey, $identityKey); $this->sessionStore->storeSession($this->recipientId, $this->deviceId, $sessionRecord); return new KeyExchangeMessage(2, $sequence, $flags, $baseKey->getPublicKey(), $baseKeySignature, $ratchetKey->getPublicKey(), $identityKey->getPublicKey()); } catch (InvalidKeyException $ex) { throw new Exception($ex->getMessage()); } }
public static function generateSenderSigningKey() { return Curve::generateKeyPair(); }
public function getUnacknowledgedPreKeyMessageItems() { $preKeyId = null; if ($this->sessionStructure->getPendingPreKey()->getPreKeyId() != null) { $preKeyId = $this->sessionStructure->getPendingPreKey()->getPreKeyId(); } return new UnacknowledgedPreKeyMessageItems($preKeyId, $this->sessionStructure->getPendingPreKey()->getSignedPreKeyId(), Curve::decodePoint($this->sessionStructure->getPendingPreKey()->getBaseKey(), 0)); }
$sfr1->load("/tmp/{$file1}"); $stam = new StatisticMatch(); $stam->match2($sfr0->words, $sfr1->words); $iMax = count($sfr0->statistic); if ($iMax > count($sfr1->statistic)) { $iMax = count($sfr1->statistic); } for ($ii = 0; $ii < $iMax; $ii++) { if ($stam->IsMatch($ii)) { print "*****"; } else { print "- - - -"; } print $sfr0->statistic[$ii] . "===" . $sfr1->statistic[$ii] . "<br>"; } exit(0); ///////////////////////////////////// $ge = new GraphicsEnvironment(1000, 1000); $mat = new MatchLines(array(0, 0, 255)); $mat->SetPairs($stam->pairs); $mat->render($ge); $curv = new Curve(); $curv->bUp = 1; $curv->color = array(0, 0, 255); $curv->SetYarr($sfr0->rfrqs); $curv->render($ge); $curv->bUp = 1; $curv->color = array(255, 0, 0); $curv->SetYarr($sfr1->rfrqs); $curv->render($ge); //$ge->showPng();
public function getKeyPair() { $publicKey = Curve::decodePoint($this->structure->getPublicKey(), 0); $privateKey = Curve::decodePrivatePoint($this->structure->getPrivateKey()); return new ECKeyPair($publicKey, $privateKey); }
public function getSigningKeyPrivate() { return Curve::decodePrivatePoint($this->senderKeyStateStructure->getSenderSigningKey()->getPrivate()); }
public function getOrCreateChainKey($sessionState, $ECPublicKey_theirEphemeral) { $theirEphemeral = $ECPublicKey_theirEphemeral; if ($sessionState->hasReceiverChain($theirEphemeral)) { return $sessionState->getReceiverChainKey($theirEphemeral); } else { $rootKey = $sessionState->getRootKey(); $ourEphemeral = $sessionState->getSenderRatchetKeyPair(); $receiverChain = $rootKey->createChain($theirEphemeral, $ourEphemeral); $ourNewEphemeral = Curve::generateKeyPair(); $senderChain = $receiverChain[0]->createChain($theirEphemeral, $ourNewEphemeral); $sessionState->setRootKey($senderChain[0]); $sessionState->addReceiverChain($theirEphemeral, $receiverChain[1]); $sessionState->setPreviousCounter(max($sessionState->getSenderChainKey()->getIndex() - 1, 0)); $sessionState->setSenderChain($ourNewEphemeral, $senderChain[1]); return $receiverChain[1]; } }
public static function add($p1, $p2) { if (self::cmp($p2, self::INFINITY) == 0 && $p1 instanceof Point) { return $p1; } if (self::cmp($p1, self::INFINITY) == 0 && $p2 instanceof Point) { return $p2; } if (self::cmp($p1, self::INFINITY) == 0 && self::cmp($p2, self::INFINITY) == 0) { return self::INFINITY; } if (Curve::cmp($p1->curve, $p2->curve) == 0) { if (gmp_cmp($p1->x, $p2->x) == 0) { if (gmp_mod(gmp_add($p1->y, $p2->y), $p1->curve->prime) == 0) { return self::INFINITY; } else { return self::double($p1); } } $p = $p1->curve->prime; $l = gmp_mul(gmp_sub($p2->y, $p1->y), gmp_invert(gmp_sub($p2->x, $p1->x), $p)); $x3 = gmp_mod(gmp_sub(gmp_sub(gmp_pow($l, 2), $p1->x), $p2->x), $p); $y3 = gmp_mod(gmp_sub(gmp_mul($l, gmp_sub($p1->x, $x3)), $p1->y), $p); return new Point($p1->curve, $x3, $y3); } else { throw new ErrorException('Elliptic curves do not match'); } }
private function getSignature($signatureKey, $serialized) { try { return Curve::calculateSignature($signatureKey, $serialized); } catch (InvalidKeyException $ex) { throw new Exception($ex->getMessage()); } }