Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions ext/sodium/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ public function init(Runtime $runtime): void
'SODIUM_CRYPTO_BOX_KEYPAIRBYTES' => VmSodium::CRYPTO_BOX_KEYPAIRBYTES,
'SODIUM_CRYPTO_BOX_MACBYTES' => VmSodium::CRYPTO_BOX_MACBYTES,
'SODIUM_CRYPTO_BOX_SEALBYTES' => VmSodium::CRYPTO_BOX_SEALBYTES,
'SODIUM_CRYPTO_AEAD_AES256GCM_KEYBYTES' => VmSodium::CRYPTO_AEAD_AES256GCM_KEYBYTES,
'SODIUM_CRYPTO_AEAD_AES256GCM_NPUBBYTES' => VmSodium::CRYPTO_AEAD_AES256GCM_NPUBBYTES,
'SODIUM_CRYPTO_AEAD_AES256GCM_NSECBYTES' => VmSodium::CRYPTO_AEAD_AES256GCM_NSECRETBYTES,
'SODIUM_CRYPTO_AEAD_AES256GCM_ABYTES' => VmSodium::CRYPTO_AEAD_AES256GCM_ABYTES,
'SODIUM_CRYPTO_SIGN_BYTES' => VmSodium::CRYPTO_SIGN_BYTES,
'SODIUM_CRYPTO_SIGN_SEEDBYTES' => VmSodium::CRYPTO_SIGN_SEEDBYTES,
'SODIUM_CRYPTO_SIGN_PUBLICKEYBYTES' => VmSodium::CRYPTO_SIGN_PUBLICKEYBYTES,
'SODIUM_CRYPTO_SIGN_SECRETKEYBYTES' => VmSodium::CRYPTO_SIGN_SECRETKEYBYTES,
'SODIUM_CRYPTO_SIGN_KEYPAIRBYTES' => VmSodium::CRYPTO_SIGN_KEYPAIRBYTES,
'SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_ABYTES' => VmSodium::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_ABYTES,
'SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_HEADERBYTES' => VmSodium::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_HEADERBYTES,
'SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_KEYBYTES' => VmSodium::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_KEYBYTES,
'SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_MESSAGE' => VmSodium::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_MESSAGE,
'SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_PUSH' => VmSodium::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_PUSH,
'SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY' => VmSodium::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY,
'SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_FINAL' => VmSodium::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_FINAL,
] as $name => $value) {
$var = new VM\Variable();
$var->int($value);
Expand Down Expand Up @@ -87,6 +103,23 @@ public function getFunctions(): array
new sodium_crypto_box_secretkey(),
new sodium_crypto_box_seal(),
new sodium_crypto_box_seal_open(),
new sodium_crypto_aead_aes256gcm_is_available(),
new sodium_crypto_aead_aes256gcm_encrypt(),
new sodium_crypto_aead_aes256gcm_decrypt(),
new sodium_crypto_sign_keypair(),
new sodium_crypto_sign_publickey(),
new sodium_crypto_sign_secretkey(),
new sodium_crypto_sign_publickey_from_secretkey(),
new sodium_crypto_sign(),
new sodium_crypto_sign_open(),
new sodium_crypto_sign_detached(),
new sodium_crypto_sign_verify_detached(),
new sodium_crypto_secretstream_xchacha20poly1305_keygen(),
new sodium_crypto_secretstream_xchacha20poly1305_init_push(),
new sodium_crypto_secretstream_xchacha20poly1305_init_pull(),
new sodium_crypto_secretstream_xchacha20poly1305_push(),
new sodium_crypto_secretstream_xchacha20poly1305_pull(),
new sodium_crypto_secretstream_xchacha20poly1305_rekey(),
];
}
}
36 changes: 36 additions & 0 deletions ext/sodium/SodiumSignFunction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace PHPCompiler\ext\sodium;

use PHPCompiler\ext\standard\VmString;
use PHPCompiler\Frame;
use PHPCompiler\Func\Internal;
use PHPCompiler\JIT\Context;
use PHPCompiler\JIT\Variable as JITVariable;
use PHPCompiler\VM\BuiltinExecute;
use PHPCompiler\VM\Variable;
use PHPLLVM\Value;

/** Shared VM wiring for sodium_crypto_sign() / sign_detached() (php-src ext/sodium/libsodium.c; #15541). */
abstract class SodiumSignFunction extends Internal
{
abstract protected function invoke(string $message, string $secretkey): string;

public function execute(Frame $frame): void
{
$this->requireExactArgCount($frame, $this->getName(), 2);
$message = VmString::coerceStringBuiltinArg($frame->calledArgs[0], $this->getName(), 0, 'message');
$secretkey = VmString::coerceStringBuiltinArg($frame->calledArgs[1], $this->getName(), 1, 'secret_key');
$result = $this->invoke($message, $secretkey);
BuiltinExecute::writeReturn($frame, static function (Variable $ret) use ($result): void {
$ret->string($result);
});
}

public function call(Context $context, JITVariable ...$args): Value
{
throw new \LogicException($this->getName().'() JIT is not supported in this compiler build');
}
}
37 changes: 37 additions & 0 deletions ext/sodium/SodiumSignKeypairExtractFunction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace PHPCompiler\ext\sodium;

use PHPCompiler\ext\standard\VmString;
use PHPCompiler\Frame;
use PHPCompiler\Func\Internal;
use PHPCompiler\JIT\Context;
use PHPCompiler\JIT\Variable as JITVariable;
use PHPCompiler\VM\BuiltinExecute;
use PHPCompiler\VM\Variable;
use PHPLLVM\Value;

/**
* Shared VM wiring for sodium_crypto_sign_publickey()/secretkey() (php-src ext/sodium/libsodium.c; #15541).
*/
abstract class SodiumSignKeypairExtractFunction extends Internal
{
abstract protected function invoke(string $keypair): string;

public function execute(Frame $frame): void
{
$this->requireExactArgCount($frame, $this->getName(), 1);
$keypair = VmString::coerceStringBuiltinArg($frame->calledArgs[0], $this->getName(), 0, 'keypair');
$result = $this->invoke($keypair);
BuiltinExecute::writeReturn($frame, static function (Variable $ret) use ($result): void {
$ret->string($result);
});
}

public function call(Context $context, JITVariable ...$args): Value
{
throw new \LogicException($this->getName().'() JIT is not supported in this compiler build');
}
}
44 changes: 44 additions & 0 deletions ext/sodium/SodiumSignOpenFunction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace PHPCompiler\ext\sodium;

use PHPCompiler\ext\standard\VmString;
use PHPCompiler\Frame;
use PHPCompiler\Func\Internal;
use PHPCompiler\JIT\Context;
use PHPCompiler\JIT\Variable as JITVariable;
use PHPCompiler\VM\BuiltinExecute;
use PHPCompiler\VM\Variable;
use PHPLLVM\Value;

/** Shared VM wiring for sodium_crypto_sign_open() (php-src ext/sodium/libsodium.c; #15541). */
abstract class SodiumSignOpenFunction extends Internal
{
/**
* @return string|false
*/
abstract protected function invoke(string $signedMessage, string $publickey): string|false;

public function execute(Frame $frame): void
{
$this->requireExactArgCount($frame, $this->getName(), 2);
$signedMessage = VmString::coerceStringBuiltinArg($frame->calledArgs[0], $this->getName(), 0, 'signed_message');
$publickey = VmString::coerceStringBuiltinArg($frame->calledArgs[1], $this->getName(), 1, 'public_key');
$result = $this->invoke($signedMessage, $publickey);
BuiltinExecute::writeReturn($frame, static function (Variable $ret) use ($result): void {
if (false === $result) {
$ret->bool(false);

return;
}
$ret->string($result);
});
}

public function call(Context $context, JITVariable ...$args): Value
{
throw new \LogicException($this->getName().'() JIT is not supported in this compiler build');
}
}
37 changes: 37 additions & 0 deletions ext/sodium/SodiumSignVerifyDetachedFunction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace PHPCompiler\ext\sodium;

use PHPCompiler\ext\standard\VmString;
use PHPCompiler\Frame;
use PHPCompiler\Func\Internal;
use PHPCompiler\JIT\Context;
use PHPCompiler\JIT\Variable as JITVariable;
use PHPCompiler\VM\BuiltinExecute;
use PHPCompiler\VM\Variable;
use PHPLLVM\Value;

/** Shared VM wiring for sodium_crypto_sign_verify_detached() (php-src ext/sodium/libsodium.c; #15541). */
abstract class SodiumSignVerifyDetachedFunction extends Internal
{
abstract protected function invoke(string $signature, string $message, string $publickey): bool;

public function execute(Frame $frame): void
{
$this->requireExactArgCount($frame, $this->getName(), 3);
$signature = VmString::coerceStringBuiltinArg($frame->calledArgs[0], $this->getName(), 0, 'signature');
$message = VmString::coerceStringBuiltinArg($frame->calledArgs[1], $this->getName(), 1, 'message');
$publickey = VmString::coerceStringBuiltinArg($frame->calledArgs[2], $this->getName(), 2, 'public_key');
$result = $this->invoke($signature, $message, $publickey);
BuiltinExecute::writeReturn($frame, static function (Variable $ret) use ($result): void {
$ret->bool($result);
});
}

public function call(Context $context, JITVariable ...$args): Value
{
throw new \LogicException($this->getName().'() JIT is not supported in this compiler build');
}
}
Loading