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
12namespace Botan {
13
14/*
15* Default amount of padding
16*/
17size_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*/
25void 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*/
40size_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*/
54bool 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*/
65void 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*/
75size_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*/
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*/
100void 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*/
110size_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*/
129 {
130 return (size > 0);
131 }
132
133}
#define BOTAN_ASSERT_EQUAL(value1, value2, msg)
Definition assert.h:29
std::string name() const
Definition mode_pad.h:90
void pad(byte[], size_t, size_t) const
Definition mode_pad.cpp:65
size_t unpad(const byte[], size_t) const
Definition mode_pad.cpp:75
bool valid_blocksize(size_t) const
Definition mode_pad.cpp:89
virtual size_t pad_bytes(size_t block_size, size_t position) const
Definition mode_pad.cpp:17
size_t unpad(const byte[], size_t) const
Definition mode_pad.cpp:110
bool valid_blocksize(size_t) const
Definition mode_pad.cpp:128
void pad(byte[], size_t, size_t) const
Definition mode_pad.cpp:100
std::string name() const
Definition mode_pad.h:102
void pad(byte[], size_t, size_t) const
Definition mode_pad.cpp:25
size_t unpad(const byte[], size_t) const
Definition mode_pad.cpp:40
bool valid_blocksize(size_t) const
Definition mode_pad.cpp:54
std::string name() const
Definition mode_pad.h:78
Decoding_Error(const std::string &name)
Definition exceptn.h:140