Debian Codegraph @

lib/Crypto/Util/Padding.py

raw symbols SCIP
1 #
2 # Util/Padding.py : Functions to manage padding
3 #
4 # ===================================================================
5 #
6 # Copyright (c) 2014, Legrandin <[email protected]>
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
34 __all__ = [ 'pad', 'unpad' ]
35
36 from Crypto.Util.py3compat import *
37
38
39 def pad(data_to_pad, block_size, style='pkcs7'):
40 """Apply standard padding.
41
42 Args:
43 data_to_pad (byte string):
44 The data that needs to be padded.
45 block_size (integer):
46 The block boundary to use for padding. The output length is guaranteed
47 to be a multiple of :data:`block_size`.
48 style (string):
49 Padding algorithm. It can be *'pkcs7'* (default), *'iso7816'* or *'x923'*.
50
51 Return:
52 byte string : the original data with the appropriate padding added at the end.
53 """
54
55 padding_len = block_size-len(data_to_pad)%block_size
56 if style == 'pkcs7':
57 padding = bchr(padding_len)*padding_len
58 elif style == 'x923':
59 padding = bchr(0)*(padding_len-1) + bchr(padding_len)
60 elif style == 'iso7816':
61 padding = bchr(128) + bchr(0)*(padding_len-1)
62 else:
63 raise ValueError("Unknown padding style")
64 return data_to_pad + padding
65
66
67 def unpad(padded_data, block_size, style='pkcs7'):
68 """Remove standard padding.
69
70 Args:
71 padded_data (byte string):
72 A piece of data with padding that needs to be stripped.
73 block_size (integer):
74 The block boundary to use for padding. The input length
75 must be a multiple of :data:`block_size`.
76 style (string):
77 Padding algorithm. It can be *'pkcs7'* (default), *'iso7816'* or *'x923'*.
78 Return:
79 byte string : data without padding.
80 Raises:
81 ValueError: if the padding is incorrect.
82 """
83
84 pdata_len = len(padded_data)
85 if pdata_len == 0:
86 raise ValueError("Zero-length input cannot be unpadded")
87 if pdata_len % block_size:
88 raise ValueError("Input data is not padded")
89 if style in ('pkcs7', 'x923'):
90 padding_len = bord(padded_data[-1])
91 if padding_len<1 or padding_len>min(block_size, pdata_len):
92 raise ValueError("Padding is incorrect.")
93 if style == 'pkcs7':
94 if padded_data[-padding_len:]!=bchr(padding_len)*padding_len:
95 raise ValueError("PKCS#7 padding is incorrect.")
96 else:
97 if padded_data[-padding_len:-1]!=bchr(0)*(padding_len-1):
98 raise ValueError("ANSI X.923 padding is incorrect.")
99 elif style == 'iso7816':
100 padding_len = pdata_len - padded_data.rfind(bchr(128))
101 if padding_len<1 or padding_len>min(block_size, pdata_len):
102 raise ValueError("Padding is incorrect.")
103 if padding_len>1 and padded_data[1-padding_len:]!=bchr(0)*(padding_len-1):
104 raise ValueError("ISO 7816-4 padding is incorrect.")
105 else:
106 raise ValueError("Unknown padding style")
107 return padded_data[:-padding_len]
108