monotone

monotone Mtn Source Tree

Root/botan/mode_pad.cpp

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

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status