Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Package wycheproof tests for the PHP ecosystem #109

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
22 changes: 22 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "c2sp/wycheproof",
"description": "Wycheproof test vectors for cryptographic software",
"authors": [
{
"name": "Scott Arciszewski",
"email": "[email protected]",
"homepage": "https://github.com/tob-scott-a"
}
],
"type": "library",
"keywords": ["wycheproof", "testing", "cryptography", "crypto"],
"license": "Apache-2.0",
"autoload": {
"psr-4": {
"C2sp\\Wycheproof\\": "php"
}
},
"require": {
"ext-json": "*"
}
}
9 changes: 9 additions & 0 deletions php/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Wycheproof for PHP

This does not implement a complete testing harness for PHP software.
This code exists to expose Wycheproof test vectors to the PHP ecosystem and
provide a simple way to get the file paths for all test vectors.

Users are responsible for selecting the test vector file they are interested in,
decoding the JSON file, iterating over the objects, and interacting with their
chosen cryptography implementation.
48 changes: 48 additions & 0 deletions php/Wycheproof.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);
namespace C2sp\Wycheproof;

class Wycheproof
{
const DEFAULT_TEST_VECTOR_DIR_NAME = 'testvectors_v1';

private string $testVectorRoot;

public function __construct(?string $testVectorRoot = null)
{
if (is_null($testVectorRoot)) {
// Default to where ever the test vectors are installed locally
$testVectorRoot = dirname(__DIR__) .
DIRECTORY_SEPARATOR .
self::DEFAULT_TEST_VECTOR_DIR_NAME;
}
$testVectorRoot = realpath($testVectorRoot);
if (!is_dir($testVectorRoot)) {
throw new WycheproofException('Cannot read directory');
}
$this->testVectorRoot = $testVectorRoot;
}

/**
* Returns a list of file paths that can be used with fopen() or
* file_get_contents()
*
* @return array<string, string>
*/
public function listTestVectorFiles(): array
{
$files = [];
foreach (glob($this->testVectorRoot . DIRECTORY_SEPARATOR . '*.json') as $file) {
$realpath = realpath($file);
if (!is_string($realpath)) {
continue;
}
if (!str_starts_with($realpath, $this->testVectorRoot)) {
continue;
}
$test_name = preg_replace('#/(.+?)\.json$#', '$1', $file);
$files[$test_name] = $file;
}
return $files;
}
}
7 changes: 7 additions & 0 deletions php/WycheproofException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace C2sp\Wycheproof;

class WycheproofException extends \Exception
{

}