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

add the capability to add padding for the hash traqnsformations (md5, ...) #11

Open
terefang opened this issue Sep 26, 2021 · 8 comments
Labels
enhancement New feature or request

Comments

@terefang
Copy link

an overview of standardized paddings is here https://en.wikipedia.org/wiki/Padding_%28cryptography%29

i saw the for 0.2 zero-padding has been merged

this enhancement request would rely upon that capability

@abhimanyu003 abhimanyu003 added the enhancement New feature or request label Sep 26, 2021
@abhimanyu003
Copy link
Owner

Hello, @terefang thanks for the suggestion.
Can you please provide/share few good examples for MD5, SHA padding?

@terefang
Copy link
Author

there are many types of padding but all rely on the block-size of the hash-function as a parameter
(MD4/MD5 = 16 bytes, SHA1 = 20 bytes)

Zero Byte Padding (ISO/IEC 10118-1 and ISO/IEC 9797-1)

from 0 to BS-1 zero bytes are added as padding

so for the byte sequence "s3cr3tPa55w0rd" (14 bytes)

  • MD4/5 adds 2 zero bytes
  • SHA1 adds 6 zero bytes

as padding.

  • MD5 on a 16/32/48/64/... bytes sequence will not add any padding
  • SHA1 on a 20/40/60/80/... bytes sequence will not add any padding

@terefang
Copy link
Author

terefang commented Oct 11, 2021

ANSI X9.23 Padding

from 1 to BS bytes are added as padding, all added bytes are zero except the last byte which is set to the total number of bytes padded.

so for the byte sequence "s3cr3tPa55w0rd" (14 bytes)

  • MD4/5 adds the bytes 0x00 0x02
  • SHA1 adds the bytes 0x00 0x00 0x00 0x00 0x00 0x06

padding bytes will always be added

  • the least padding is the byte 0x01
  • the longest padding is (BS-1) x 0x00 followed by a byte set to BS

@terefang
Copy link
Author

PKCS#5, PKCS#7, RFC 5652 Padding

from 1 to BS bytes are added as padding, all added bytes are set to the total number of bytes padded.

so for the byte sequence "s3cr3tPa55w0rd" (14 bytes)

  • MD4/5 adds the bytes 0x02 0x02
  • SHA1 adds the bytes 0x06 0x06 0x06 0x06 0x06 0x06

padding bytes will always be added

  • the least padding is the byte 0x01
  • the longest padding is BS bytes all set to BS

@terefang
Copy link
Author

ISO/IEC 7816-4:2005 Padding

from 1 to BS bytes are added as padding, the first byte is set to 0x80 all other bytes are zero

so for the byte sequence "s3cr3tPa55w0rd" (14 bytes)

  • MD5 adds the bytes 0x80 0x00
  • SHA1 adds the bytes 0x80 0x00 0x00 0x00 0x00 0x00

padding bytes will always be added

  • the least padding is the byte 0x80
  • the longest padding is BS bytes all zero except for the first byte set to 0x80

@itslychee
Copy link

@terefang Apologies for my asking, as I'm quite new to all of this stuff and I would like to help to the best of my ability, but is s3cr3tPa55w0rd the message digest in the examples you provided?

@itslychee
Copy link

Unless I'm gravely mistaken, it seems if Go's crypto module already does this for the user.
image

@rverst
Copy link
Collaborator

rverst commented Oct 12, 2021

Unless I'm gravely mistaken, it seems if Go's crypto module already does this for the user.

@itslychee yes, of course, the crypto packages of go are implemented as required by the individual algorithms. Otherwise, IMHO, they would be pretty useless.

@terefang, I honestly don't understand exactly what you're getting at. The noted zero-padding was an example implementation of mine to give an example of the new processor design. It simply prepends a fixed number of zeros to a number. Actually, the implementation is not even good, it would be better to specify the desired "width" of the number and then prepend a corresponding number of zeros. However, I haven't had time to tackle this yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants