Botan 1.10.17

mode_pad.cpp
Go to the documentation of this file.
1 /*
2 * CBC Padding Methods
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #include <botan/mode_pad.h>
9 #include <botan/exceptn.h>
10 #include <botan/internal/assert.h>
11
12 namespace Botan {
13
14 /*
15 * Default amount of padding
16 */
17 size_t BlockCipherModePaddingMethod::pad_bytes(size_t bs, size_t pos) const
18 {
19 return (bs - pos);
20 }
21
22 /*
23 * Pad with PKCS #7 Method
24 */
25 void PKCS7_Padding::pad(byte block[], size_t size, size_t position) const
26 {
27 const size_t bytes_remaining = size - position;
28 const byte pad_value = static_cast<byte>(bytes_remaining);
29
30 BOTAN_ASSERT_EQUAL(pad_value, bytes_remaining,
31 "Overflow in PKCS7_Padding");
32
33 for(size_t j = 0; j != size; ++j)
34 block[j] = pad_value;
35 }
36
37 /*
38 * Unpad with PKCS #7 Method
39 */
40 size_t PKCS7_Padding::unpad(const byte block[], size_t size) const
41 {
42 size_t position = block[size-1];
43 if(position > size)
44 throw Decoding_Error(name());
45 for(size_t j = size-position; j != size-1; ++j)
46 if(block[j] != position)
47 throw Decoding_Error(name());
48 return (size-position);
49 }
50
51 /*
52 * Query if the size is valid for this method
53 */
54 bool PKCS7_Padding::valid_blocksize(size_t size) const
55 {
56 if(size > 0 && size < 256)
57 return true;
58 else
59 return false;
60 }
61
62 /*
63 * Pad with ANSI X9.23 Method
64 */
65 void ANSI_X923_Padding::pad(byte block[], size_t size, size_t position) const
66 {
67 for(size_t j = 0; j != size-position; ++j)
68 block[j] = 0;
69 block[size-position-1] = static_cast<byte>(size-position);
70 }
71
72 /*
73 * Unpad with ANSI X9.23 Method
74 */
75 size_t ANSI_X923_Padding::unpad(const byte block[], size_t size) const
76 {
77 size_t position = block[size-1];
78 if(position > size)
79 throw Decoding_Error(name());
80 for(size_t j = size-position; j != size-1; ++j)
81 if(block[j] != 0)
82 throw Decoding_Error(name());
83 return (size-position);
84 }
85
86 /*
87 * Query if the size is valid for this method
88 */
89 bool ANSI_X923_Padding::valid_blocksize(size_t size) const
90 {
91 if(size > 0 && size < 256)
92 return true;
93 else
94 return false;
95 }
96
97 /*
98 * Pad with One and Zeros Method
99 */
100 void OneAndZeros_Padding::pad(byte block[], size_t size, size_t) const
101 {
102 block[0] = 0x80;
103 for(size_t j = 1; j != size; ++j)
104 block[j] = 0x00;
105 }
106
107 /*
108 * Unpad with One and Zeros Method
109 */
110 size_t OneAndZeros_Padding::unpad(const byte block[], size_t size) const
111 {
112 while(size)
113 {
114 if(block[size-1] == 0x80)
115 break;
116 if(block[size-1] != 0x00)
117 throw Decoding_Error(name());
118 size--;
119 }
120 if(!size)
121 throw Decoding_Error(name());
122 return (size-1);
123 }
124
125 /*
126 * Query if the size is valid for this method
127 */
128 bool OneAndZeros_Padding::valid_blocksize(size_t size) const
129 {
130 return (size > 0);
131 }
132
133 }
Botan::PKCS7_Padding::valid_blocksize
bool valid_blocksize(size_t) const
Definition: mode_pad.cpp:54
Botan::ANSI_X923_Padding::pad
void pad(byte[], size_t, size_t) const
Definition: mode_pad.cpp:65
Botan::PKCS7_Padding::name
std::string name() const
Definition: mode_pad.h:78
Botan::BlockCipherModePaddingMethod::pad_bytes
virtual size_t pad_bytes(size_t block_size, size_t position) const
Definition: mode_pad.cpp:17
Botan::PKCS7_Padding::pad
void pad(byte[], size_t, size_t) const
Definition: mode_pad.cpp:25
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::ANSI_X923_Padding::valid_blocksize
bool valid_blocksize(size_t) const
Definition: mode_pad.cpp:89
Botan::Decoding_Error
Definition: exceptn.h:138
BOTAN_ASSERT_EQUAL
#define BOTAN_ASSERT_EQUAL(value1, value2, msg)
Definition: assert.h:29
Botan
Definition: algo_base.h:14
Botan::OneAndZeros_Padding::valid_blocksize
bool valid_blocksize(size_t) const
Definition: mode_pad.cpp:128
Botan::OneAndZeros_Padding::unpad
size_t unpad(const byte[], size_t) const
Definition: mode_pad.cpp:110
Botan::ANSI_X923_Padding::name
std::string name() const
Definition: mode_pad.h:90
Botan::OneAndZeros_Padding::pad
void pad(byte[], size_t, size_t) const
Definition: mode_pad.cpp:100
Botan::OneAndZeros_Padding::name
std::string name() const
Definition: mode_pad.h:102
Botan::PKCS7_Padding::unpad
size_t unpad(const byte[], size_t) const
Definition: mode_pad.cpp:40
Botan::ANSI_X923_Padding::unpad
size_t unpad(const byte[], size_t) const
Definition: mode_pad.cpp:75