padding zeros in char *

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Keith Thompson

    #16
    Re: padding zeros in char *

    "Old Wolf" <oldwolf@inspir e.net.nz> writes:[color=blue]
    > Keith Thompson wrote:[color=green]
    >> I strongly suspect that AES_decrypt() requires 16-byte character
    >> arrays, not strings (at least not the way C defines the term
    >> "string"). Padding a character array with '\0' does make sense; in
    >> fact, this may be one of the few good uses for strncpy().[/color]
    >
    > Unlikely, as strncpy() will stop reading the source when it
    > encounters the first '\0' character. I gather from the thread
    > that the source may contain multiple '\0' characters at
    > various points.[/color]

    Good point.

    --
    Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.

    Comment

    • Michael Wojcik

      #17
      Re: padding zeros in char *


      In article <lnu0fo26ei.fsf @nuthaus.mib.or g>, Keith Thompson <kst-u@mib.org> writes:[color=blue]
      > Joe Wright <jwright@comcas t.net> writes:[color=green]
      > > Keith Thompson wrote:[color=darkred]
      > >> Martin Ambuhl <mambuhl@earthl ink.net> writes:
      > >>
      > >>>Joe Wright wrote:
      > >>>
      > >>>>s.seitz@net z-haut.de wrote:
      > >>>
      > >>>>>Again, my problem is NOT the AES_decrypt(). My problem is padding the
      > >>>>>input with trailing \0 bytes.
      > >>>>
      > >>>>How about '0' bytes instead of '\0' bytes?
      > >>[/color]
      > > The problem seems to be that OP needs 16-byte strings.[/color][/color]

      Unfortunately, the problem is that OP needs to be operating on
      contiguous sequences of 128 bits, and not strings at all (as defined
      by C).
      [color=blue][color=green]
      > > Clearly an
      > > arbitrarily placed '\0' may shorten the string. Padding with '0'
      > > characters will solve length problem.
      > >
      > > The OP may be mis-reading the requirement. Demanding 16-byte strings
      > > and padding with '\0' doesn't make sense.[/color][/color]

      Yes, but not in the way that your suggestion corrects, unfortunately.
      [color=blue]
      > I strongly suspect that AES_decrypt() requires 16-byte character
      > arrays, not strings (at least not the way C defines the term
      > "string").[/color]

      More precisely, it requires 128 bits of ciphertext, and not strings
      at all.
      [color=blue]
      > Padding a character array with '\0' does make sense; in
      > fact, this may be one of the few good uses for strncpy().[/color]

      I think strncpy's the wrong approach here. Even where CHAR_BIT is 8,
      using strncpy implies that the function operates on strings, which is
      not true.

      The OP should be passing AES_decrypt a buffer of 128 bits of
      ciphertext. If fewer than 128 bits of ciphertext remain, the buffer
      should be padded at the end with zero bits. memset and memcpy are
      the appropriate functions to use here; if it's necessary to support
      platforms where CHAR_BIT is not 8, the code will need to use bit
      operations as well, since AES is an octet-granular encoding.

      --
      Michael Wojcik michael.wojcik@ microfocus.com

      Recently, they appeared at the reopening of the Brookdale Library,
      luring passersby with the opportunity to be anonymously silly.

      Comment

      Working...