Padding.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #
  2. # Util/Padding.py : Functions to manage padding
  3. #
  4. # ===================================================================
  5. #
  6. # Copyright (c) 2014, Legrandin <helderijs@gmail.com>
  7. # All rights reserved.
  8. #
  9. # Redistribution and use in source and binary forms, with or without
  10. # modification, are permitted provided that the following conditions
  11. # are met:
  12. #
  13. # 1. Redistributions of source code must retain the above copyright
  14. # notice, this list of conditions and the following disclaimer.
  15. # 2. Redistributions in binary form must reproduce the above copyright
  16. # notice, this list of conditions and the following disclaimer in
  17. # the documentation and/or other materials provided with the
  18. # distribution.
  19. #
  20. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  30. # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. # POSSIBILITY OF SUCH DAMAGE.
  32. # ===================================================================
  33. __all__ = [ 'pad', 'unpad' ]
  34. from Crypto.Util.py3compat import *
  35. def pad(data_to_pad, block_size, style='pkcs7'):
  36. """Apply standard padding.
  37. Args:
  38. data_to_pad (byte string):
  39. The data that needs to be padded.
  40. block_size (integer):
  41. The block boundary to use for padding. The output length is guaranteed
  42. to be a multiple of :data:`block_size`.
  43. style (string):
  44. Padding algorithm. It can be *'pkcs7'* (default), *'iso7816'* or *'x923'*.
  45. Return:
  46. byte string : the original data with the appropriate padding added at the end.
  47. """
  48. padding_len = block_size-len(data_to_pad)%block_size
  49. if style == 'pkcs7':
  50. padding = bchr(padding_len)*padding_len
  51. elif style == 'x923':
  52. padding = bchr(0)*(padding_len-1) + bchr(padding_len)
  53. elif style == 'iso7816':
  54. padding = bchr(128) + bchr(0)*(padding_len-1)
  55. else:
  56. raise ValueError("Unknown padding style")
  57. return data_to_pad + padding
  58. def unpad(padded_data, block_size, style='pkcs7'):
  59. """Remove standard padding.
  60. Args:
  61. padded_data (byte string):
  62. A piece of data with padding that needs to be stripped.
  63. block_size (integer):
  64. The block boundary to use for padding. The input length
  65. must be a multiple of :data:`block_size`.
  66. style (string):
  67. Padding algorithm. It can be *'pkcs7'* (default), *'iso7816'* or *'x923'*.
  68. Return:
  69. byte string : data without padding.
  70. Raises:
  71. ValueError: if the padding is incorrect.
  72. """
  73. pdata_len = len(padded_data)
  74. if pdata_len == 0:
  75. raise ValueError("Zero-length input cannot be unpadded")
  76. if pdata_len % block_size:
  77. raise ValueError("Input data is not padded")
  78. if style in ('pkcs7', 'x923'):
  79. padding_len = bord(padded_data[-1])
  80. if padding_len<1 or padding_len>min(block_size, pdata_len):
  81. raise ValueError("Padding is incorrect.")
  82. if style == 'pkcs7':
  83. if padded_data[-padding_len:]!=bchr(padding_len)*padding_len:
  84. raise ValueError("PKCS#7 padding is incorrect.")
  85. else:
  86. if padded_data[-padding_len:-1]!=bchr(0)*(padding_len-1):
  87. raise ValueError("ANSI X.923 padding is incorrect.")
  88. elif style == 'iso7816':
  89. padding_len = pdata_len - padded_data.rfind(bchr(128))
  90. if padding_len<1 or padding_len>min(block_size, pdata_len):
  91. raise ValueError("Padding is incorrect.")
  92. if padding_len>1 and padded_data[1-padding_len:]!=bchr(0)*(padding_len-1):
  93. raise ValueError("ISO 7816-4 padding is incorrect.")
  94. else:
  95. raise ValueError("Unknown padding style")
  96. return padded_data[:-padding_len]