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

refactor md5 tool with dgst and fix stdin behavior #1766

Merged
merged 3 commits into from
Aug 14, 2024

Conversation

samuel40791765
Copy link
Contributor

@samuel40791765 samuel40791765 commented Aug 14, 2024

Issues:

Addresses CryptoAlg-2602

Description of changes:

Our original md5 stdin behavior was acting weird. The issue seems to be that we weren't actually parsing all of the input from stdin.

ubuntu in 🌐 ip-172-31-63-232 in aws-lc on  fix-md5-tool [=$+?] via △ v3.16.3 via 🐹 v1.20.4 
❯ echo "test" | openssl md5 
(stdin)= d8e8fca2dc0f896fd7cb4cb0031ba249

ubuntu in 🌐 ip-172-31-63-232 in aws-lc on  fix-md5-tool [=$+?] via △ v3.16.3 via 🐹 v1.20.4 
❯ echo "test" | ./test_build_dir/tool-openssl/openssl md5 
(stdin)= 098f6bcd4621d373cade4e832627b4f6

The original implementation used getline which only reads one line. This worked with the original test since the file contents were sent into stdin with <, but this doesn't work correctly with other stdin behaviors such as echo test | openssl md5.
The correct and more useful way to handle stdin is to read it from the 0 file descriptor. This is how OpenSSL/BoringSSL reads from files.

  1. This change fixes the behavior by rebuilding md5 onto our dgst implementation. This is identical to how OpenSSL factors the specific hashing tool names. md5 now uses dgst under the hood and inherits the file hashing behavior we implemented for dgst hmac.
  2. stdin was missing from dgst, so that was implemented as well. stdin now works for both openssl dgst -hmac ... and openssl md5. Corresponding tests have also been added to verify.
  3. Code was structured so that we can easily extend new digest support for the openssl tool with just a few lines. I considered adding it along with this PR, but didn't want to muddle it with the new logic.

FIxed version:

ubuntu in 🌐 ip-172-31-63-232 in aws-lc on  fix-md5-tool [$?] via △ v3.16.3 via 🐹 v1.20.4 
❯ echo "test_fix" | openssl md5
(stdin)= 1312e69e9dc6ec31b51af24fc42ebdea

ubuntu in 🌐 ip-172-31-63-232 in aws-lc on  fix-md5-tool [$?] via △ v3.16.3 via 🐹 v1.20.4 
❯ echo "test_fix" | ./test_build_dir/tool-openssl/openssl md5
(stdin)= 1312e69e9dc6ec31b51af24fc42ebdea

Call-outs:

Deleted the md5 files since most logic was moved under dgst.

Testing:

  1. New tests for hmac stdin
  2. New tests for md5 stdin and md5 files.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license and the ISC license.

@samuel40791765 samuel40791765 requested a review from a team as a code owner August 14, 2024 00:18
tool-openssl/dgst.cc Outdated Show resolved Hide resolved
tool-openssl/dgst.cc Show resolved Hide resolved
@codecov-commenter
Copy link

codecov-commenter commented Aug 14, 2024

Codecov Report

Attention: Patch coverage is 2.96296% with 131 lines in your changes missing coverage. Please review.

Project coverage is 78.31%. Comparing base (0c52b12) to head (93ad99f).

Files Patch % Lines
tool-openssl/dgst.cc 0.00% 73 Missing ⚠️
tool-openssl/dgst_test.cc 6.45% 58 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff            @@
##           main    #1766       +/-   ##
=========================================
+ Coverage      0   78.31%   +78.31%     
=========================================
  Files         0      580      +580     
  Lines         0    96981    +96981     
  Branches      0    13901    +13901     
=========================================
+ Hits          0    75946    +75946     
- Misses        0    20413    +20413     
- Partials      0      622      +622     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

justsmth
justsmth previously approved these changes Aug 14, 2024
andrewhop
andrewhop previously approved these changes Aug 14, 2024
@samuel40791765 samuel40791765 dismissed stale reviews from andrewhop and justsmth via 93ad99f August 14, 2024 20:51
@samuel40791765 samuel40791765 enabled auto-merge (squash) August 14, 2024 21:04
@samuel40791765 samuel40791765 merged commit 52d1651 into aws:main Aug 14, 2024
105 of 106 checks passed
samuel40791765 added a commit to samuel40791765/aws-lc that referenced this pull request Aug 14, 2024
Our original md5 stdin behavior was acting weird. The issue seems to be
that we weren't actually parsing all of the input from `stdin`.
```
❯ echo "test" | openssl md5
(stdin)= d8e8fca2dc0f896fd7cb4cb0031ba249

❯ echo "test" | ./test_build_dir/tool-openssl/openssl md5
(stdin)= 098f6bcd4621d373cade4e832627b4f6
```
The original implementation used `getline` which only reads one line.
This worked with the original test since the file contents were sent
into `stdin` with `<`, but this doesn't work correctly with other
`stdin` behaviors such as `echo test | openssl md5`.
The correct and more useful way to handle `stdin` is to read it from
[the `0` file
descriptor.](https://en.wikipedia.org/wiki/File_descriptor) This is how
OpenSSL/BoringSSL reads from files.

1. This change fixes the behavior by rebuilding `md5` onto our `dgst`
implementation. This is identical to how OpenSSL factors the specific
hashing tool names. `md5` now uses `dgst` under the hood and inherits
the file hashing behavior we implemented for `dgst hmac`.
2. `stdin` was missing from `dgst`, so that was implemented as well.
`stdin` now works for both `openssl dgst -hmac ...` and `openssl md5`.
Corresponding tests have also been added to verify.
3. Code was structured so that we can easily extend new digest support
for the openssl tool with just a few lines. I considered adding it along
with this PR, but didn't want to muddle it with the new logic.

FIxed version:
```
❯ echo "test_fix" | openssl md5
(stdin)= 1312e69e9dc6ec31b51af24fc42ebdea

❯ echo "test_fix" | ./test_build_dir/tool-openssl/openssl md5
(stdin)= 1312e69e9dc6ec31b51af24fc42ebdea
```

By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 license and the ISC license.

(cherry picked from commit 52d1651)
samuel40791765 added a commit that referenced this pull request Aug 15, 2024
Our original md5 stdin behavior was acting weird. The issue seems to be
that we weren't actually parsing all of the input from `stdin`.
```
❯ echo "test" | openssl md5
(stdin)= d8e8fca2dc0f896fd7cb4cb0031ba249

❯ echo "test" | ./test_build_dir/tool-openssl/openssl md5
(stdin)= 098f6bcd4621d373cade4e832627b4f6
```
The original implementation used `getline` which only reads one line.
This worked with the original test since the file contents were sent
into `stdin` with `<`, but this doesn't work correctly with other
`stdin` behaviors such as `echo test | openssl md5`.
The correct and more useful way to handle `stdin` is to read it from
[the `0` file
descriptor.](https://en.wikipedia.org/wiki/File_descriptor) This is how
OpenSSL/BoringSSL reads from files.

1. This change fixes the behavior by rebuilding `md5` onto our `dgst`
implementation. This is identical to how OpenSSL factors the specific
hashing tool names. `md5` now uses `dgst` under the hood and inherits
the file hashing behavior we implemented for `dgst hmac`.
2. `stdin` was missing from `dgst`, so that was implemented as well.
`stdin` now works for both `openssl dgst -hmac ...` and `openssl md5`.
Corresponding tests have also been added to verify.
3. Code was structured so that we can easily extend new digest support
for the openssl tool with just a few lines. I considered adding it along
with this PR, but didn't want to muddle it with the new logic.

FIxed version:
```
❯ echo "test_fix" | openssl md5
(stdin)= 1312e69e9dc6ec31b51af24fc42ebdea

❯ echo "test_fix" | ./test_build_dir/tool-openssl/openssl md5
(stdin)= 1312e69e9dc6ec31b51af24fc42ebdea
```

By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 license and the ISC license.

(cherry picked from commit 52d1651)
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.

4 participants