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

Use random_bytes instead of self-rolled random function #38018

Merged
merged 1 commit into from
Oct 29, 2020

Conversation

IljaN
Copy link
Member

@IljaN IljaN commented Oct 19, 2020

Description

Replace Secure::generate with random_bytes to strengthen the IV.
This creates a new format for the ecrypted-string with a v2| prefix. The modified encryption
code encrypts the session on disk

Related Issue

Motivation and Context

How Has This Been Tested?

  • test environment:
  • test case 1:
  • test case 2:
  • ...

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Database schema changes (next release will require increase of minor version instead of patch)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Technical debt
  • Tests only (no source changes)

Checklist:

  • Code changes
  • Unit tests added
  • Acceptance tests added
  • Documentation ticket raised:
  • Changelog item, see TEMPLATE

@update-docs
Copy link

update-docs bot commented Oct 19, 2020

Thanks for opening this pull request! The maintainers of this repository would appreciate it if you would create a changelog item based on your changes.

@IljaN IljaN self-assigned this Oct 19, 2020
@IljaN
Copy link
Member Author

IljaN commented Oct 19, 2020

CI Fail. The issue is that the random values are outside of the UTF8 range and thus can't be written to the database. This is probably the reason why the weak "random-text" method was used to generate the IV. Or maybe the code predates random_bytes.

The correct implementation would be to use base64Encode before writing the cipher-material to disk or in to the database.

@C0rby
Copy link
Contributor

C0rby commented Oct 20, 2020

How about this?

$iv = \random_bytes($this->ivLength);
$this->cipher->setIV($iv);
$iv = \bin2hex($iv);
...
return $ciphertext.'|'.$iv.'|'.$hmac;

And when one needs the iv again it's just:

$iv = \hex2bin($iv);
$this->cipher-setIV($iv);

@lynn-stephenson
Copy link

@C0rby That's the correct way of doing it. You can encode the output to ASCII hexadecimal before storing the data, or base64ing it. Your choice.

Copy link
Contributor

@C0rby C0rby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! 👍

Copy link
Member

@DeepDiver1975 DeepDiver1975 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add unit tests

@IljaN IljaN requested review from C0rby and DeepDiver1975 October 29, 2020 15:31
@IljaN IljaN force-pushed the use-better-random-source branch from 2d1912b to 816bc5b Compare October 29, 2020 16:21
Copy link
Member

@DeepDiver1975 DeepDiver1975 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

V1 test ?

@IljaN
Copy link
Member Author

IljaN commented Oct 29, 2020

@DeepDiver1975 V1 is already tested here:

public function testLaterDecryption() {

@sonarcloud
Copy link

sonarcloud bot commented Oct 29, 2020

Kudos, SonarCloud Quality Gate passed!

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities (and Security Hotspot 0 Security Hotspots to review)
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@IljaN IljaN merged commit 5cc07a8 into master Oct 29, 2020
@delete-merged-branch delete-merged-branch bot deleted the use-better-random-source branch October 29, 2020 18:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SecureRandom::generate should use bytes instead of ASCII characters
4 participants