Giter Site home page Giter Site logo

paragonie / constant_time_encoding Goto Github PK

View Code? Open in Web Editor NEW
783.0 18.0 37.0 168 KB

Constant-Time Character Encoding in PHP Projects

Home Page: https://paragonie.com/blog/2016/06/constant-time-encoding-boring-cryptography-rfc-4648-and-you

License: Other

PHP 100.00%
character-encoding encoding php base64 base64url base32 base32hex base16 hexadecimal rfc-4648

constant_time_encoding's People

Contributors

chris8934 avatar hansott avatar inkeliz avatar jdreesen avatar krsriq avatar michaeldyrynda avatar nikanderson avatar paragonie-scott avatar paragonie-security avatar sharkmachine avatar systemsolutionweb avatar szepeviktor avatar timwolla avatar tvlooy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

constant_time_encoding's Issues

PHP8 Compatibility - TypeError: mb_substr(): Argument #3 ($length) must be of type ?int, null given

Edit: So for hours now I have been trying to get to the bottom of this to give you more details.

All I have managed to find out is that it only effects PHP 8 RC3 when JIT Enabled.

It doesn't always happen either... I cannot get a reliable reproducer - except that when I deploy to production it reproduces within moments and kills my live service.

I cannot replicate ever - even in production - when PHP preloading & JIT is disabled.

return \mb_substr($str, $start, $length, '8bit');

TypeError: mb_substr(): Argument #3 ($length) must be of type ?int, null given
#34 /vendor/paragonie/constant_time_encoding/src/Binary.php(76): mb_substr
#33 /vendor/paragonie/constant_time_encoding/src/Binary.php(76): ParagonIE\ConstantTime\Binary::safeSubstr
#32 /vendor/paragonie/hidden-string/src/HiddenString.php(173): ParagonIE\HiddenString\HiddenString::safeStrcpy
#31 /vendor/paragonie/hidden-string/src/HiddenString.php(56): ParagonIE\HiddenString\HiddenString::__construct
#30 /vendor/paragonie/halite/src/Symmetric/Crypto.php(208): ParagonIE\Halite\Symmetric\Crypto::decryptWithAd
#29 /vendor/paragonie/halite/src/Symmetric/Crypto.php(105): ParagonIE\Halite\Symmetric\Crypto::decrypt
#28 /vendor/philetaylor/doctrine-encrypt/Encryptors/HaliteEncryptor.php(82): Philetaylor\DoctrineEncryptBundle\Encryptors\HaliteEncryptor::decrypt

Base58 Encoding/Decoding?

I don't think there's any reason to add these, since they're not covered by RFC 4648 and the only people who would be interested are BitCoin enthusiasts (which have very little overlap with the PHP community).

Happy to be proven wrong, though.

PHP Version support

Hi,
which version of this package can I use to support both PHP 5.6 and 8.x?

I am not happy about having to do that, however, I am creating a WordPress plugin so I have to support what WP supports :/

Thanks

Fatal error: Uncaught RangeException: Base64::decode() only expects characters in the correct base64 alphabet

Using n from https://www.rfc-editor.org/rfc/rfc7517#appendix-A.1 throws an exception in v2.6.3:

<?php
require __DIR__ . '/vendor/autoload.php';

use ParagonIE\ConstantTime\Base64;

$str = '0vx7agoebGcQSuuPiLJXZptN9nndrQmbXEps2aiAFbWhM78LhWx4cbbfAAtVT86zwu1RK7aPFFxuhDR1L6tSoc_BJECPebWKRXjBZCiFV4n3oknjhMstn64tZ_2W-5JsGY4Hc5n9yBXArwl93lqt7_RN5w6Cf0h4QyQ5v-65YGjQR0_FDW2QvzqY368QQMicAtaSqzs8KJZgnYb9c7d0zgdAZHzu6qMQvRL5hajrn1n91CbOpbISD08qNLyrdkt-bFTWhAI4vMQFh6WeZu0fM4lFd2NcRwr3XPksINHaQ-G_xBniIqbw0Ls1jF44-csFCur-kEgU8awapJzKnqDKgw';

$str = Base64::decode($str);

echo strlen($str);

base64_decode has no problem with $str, altho neither sodium_base642bin($str, SODIUM_BASE64_VARIANT_ORIGINAL) nor sodium_base642bin($str, SODIUM_BASE64_VARIANT_URLSAFE) like the string.

unit tests fail with 2.6.1

They still work with 2.6.0.

Possibly related to #44.

$ vendor/bin/phpunit --verbose
PHPUnit 9.5.20 #StandWithUkraine

Runtime:       PHP 8.1.6
Configuration: /home/X/Projects/constant_time_encoding/phpunit.xml.dist

.E.....E..........................................                50 / 50 (100%)

Time: 00:01.520, Memory: 12.00 MB

There were 2 errors:

1) ParagonIE\ConstantTime\Tests\Base32HexTest::testDecodeNoPadding
Uninitialized string offset 8

/home/X/Projects/constant_time_encoding/src/Base32.php:200
/home/X/Projects/constant_time_encoding/tests/Base32HexTest.php:59

2) ParagonIE\ConstantTime\Tests\Base32Test::testDecodeNoPadding
Uninitialized string offset 8

/home/X/Projects/constant_time_encoding/src/Base32.php:200
/home/X/Projects/constant_time_encoding/tests/Base32Test.php:80

ERRORS!
Tests: 50, Assertions: 49865, Errors: 2.

Hex::decode() doesn't accept hexadecimal string

The docblock reads "Convert a hexadecimal string into a binary string without cache-timing leaks". This doesn't happen to be the case.

<?php

use \ParagonIE\ConstantTime\Hex;

require __DIR__.'/vendor/autoload.php';

function random_hexadecimal_string()
{
    $string = '';
    for ($i=0; $i<32; $i++) {
        $string .= chr( rand( 65, 90 ) );
    }

    return $string;
}

for ($i = 0; $i < 10; $i++) {
    try {
        $string = random_hexadecimal_string();
        echo Hex::decode($string);
        echo "\n";
    } catch (\Exception $e) {
        echo "fail: {$string}\n";
    }
}

for ($i = 0; $i < 10; $i++) {
    try {
        $string = random_int(0, 999999999999999999);
        echo Hex::decode($string);
        echo "\n";
    } catch (\Exception $e) {
        echo "fail: {$string}\n";
    }
}

Fails for all the hex decimal strings I've randomly generated.

new v1.0 release

Would it be possible to release v1.0.2 with the fixes queued in the v1.x branch? I'm looking forward to be able to consistently catch \RangeException on decode failures.

Base64::decode() gives Incorrect Padding error - base64_decode() works just fine

In the following code I have one string that I test against Base64::decode() and against base64_decode() and each one behaves differently.

First, my code:

<?php
require __DIR__ . '/vendor/autoload.php';

use ParagonIE\ConstantTime\Base64;

$str = 'MIIFzzCCBLegAwIBAgIDAfdlMA0GCSqGSIb3DQEBBQUAMHMxCzAJBgNVBAYTAlBM
MSgwJgYDVQQKDB9LcmFqb3dhIEl6YmEgUm96bGljemVuaW93YSBTLkEuMSQwIgYD
VQQDDBtDT1BFIFNaQUZJUiAtIEt3YWxpZmlrb3dhbnkxFDASBgNVBAUTC05yIHdw
aXN1OiA2MB4XDTExMTEwOTA2MDAwMFoXDTEzMTEwOTA2MDAwMFowgdkxCzAJBgNV
BAYTAlBMMRwwGgYDVQQKDBNVcnrEhWQgTWlhc3RhIEdkeW5pMRswGQYDVQQFExJQ
RVNFTDogNjEwNjA2MDMxMTgxGTAXBgNVBAMMEEplcnp5IFByemV3b3Jza2kxTzBN
BgNVBBAwRgwiQWwuIE1hcnN6YcWCa2EgUGnFgnN1ZHNraWVnbyA1Mi81NAwNODEt
MzgyIEdkeW5pYQwGUG9sc2thDAlwb21vcnNraWUxDjAMBgNVBCoMBUplcnp5MRMw
EQYDVQQEDApQcnpld29yc2tpMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCM
m5vjGqHPthJCMqKpqssSISRos0PYDTcEQzyyurfX67EJWKtZj6HNwuDMEGJ02iBN
ZfjUl7r8dIi28bSKhNlsfycXZKYRcIjp0+r5RqtR2auo9GQ6veKb61DEAGIqaR+u
LLcJVTHCu0w9oXLGbRlGth5eNoj03CxXVAH2IfhbNwIDAQABo4IChzCCAoMwDAYD
VR0TAQH/BAIwADCCAUgGA1UdIAEB/wSCATwwggE4MIIBNAYJKoRoAYb3IwEBMIIB
JTCB3QYIKwYBBQUHAgIwgdAMgc1EZWtsYXJhY2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y2XuLNhBNHMAyTqpYPX8Yvav1c+Z50OMaSXHAnTa20zv8UtiHbaAhwlifCelU
Mj93S';

$str = preg_replace('#[\r\n]#', '', $str);

echo strlen(base64_decode($str)) . "\n";
echo strlen(Base64::decode($str)) . "\n";

When I run that code I get the following output:

1491

Fatal error: Uncaught RangeException: Incorrect padding in /path/to/vendor/paragonie/constant_time_encoding/src/Base64.php:105
Stack trace:
#0 /path/to/test.php(42): ParagonIE\ConstantTime\Base64::decode('MIIFzzCCBLegAwI...')
#1 {main} thrown in /path/to/vendor/paragonie/constant_time_encoding/src/Base64.php on line 105

Any ideas? I was under the impression Base64::decode() was supposed to be a drop in replacement for base64_decode.

New Release?

It's been a while since the last release and the changes from #48 which are somewhat security-sensitive are still pending. Could you release a new version?

v2.6.3...master

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.