Skip to content

Commit 0fcb944

Browse files
committed
adding a project API description
1 parent 230d6f3 commit 0fcb944

1 file changed

Lines changed: 109 additions & 0 deletions

File tree

README.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,112 @@ Implementation of PKCS7 padding in C. It can be used to pre-prepare data before
2424
including input values that are already a multiple of the block size,
2525
and no padding string is a suffix of another. This padding method is
2626
well defined if and only if k is less than 256.
27+
28+
# Quick start
29+
30+
Download this repository and go to directory with project.
31+
32+
```
33+
git clone https://github.com/GRISHNOV/PKCS7-Padding.git
34+
cd PKCS7-Padding
35+
```
36+
37+
Run the project build using the make utility.
38+
39+
```
40+
make
41+
```
42+
43+
Now the `build` folder contains the `PKCS7.o` object file. Use it together with the `PKCS7.h` header file from `include` folder to work with PKCS7 padding in your project.
44+
45+
For PKCS7 padding, use this function with appropriate structure:
46+
47+
```C
48+
/*
49+
A pointer to this structure is returned from the function addPadding().
50+
The structure contains result of adding PKCS7 padding.
51+
*/
52+
typedef struct {
53+
void* dataWithPadding; /* result of adding padding to the data */
54+
uint64_t dataLengthWithPadding; /* length of the result */
55+
uint8_t valueOfByteForPadding; /* used for padding byte value */
56+
} PKCS7_Padding;
57+
58+
/*
59+
Applies PKCS7 padding to data.
60+
Your data at the provided address does not change. A copy is created, to which the adding padding is applied.
61+
WARNING: use only 0 < BLOCK_SIZE < 256
62+
*/
63+
PKCS7_Padding* addPadding(const void* const data, const uint64_t dataLength, const uint8_t BLOCK_SIZE);
64+
```
65+
66+
For PKCS7 unpadding, use this function with appropriate structure:
67+
68+
```C
69+
/*
70+
A pointer to this structure is returned from the function removePadding().
71+
The structure contains result of removing PKCS7 padding.
72+
*/
73+
typedef struct {
74+
void* dataWithoutPadding; /* result of remove padding from data */
75+
uint64_t dataLengthWithoutPadding; /* length of the result */
76+
uint8_t valueOfRemovedByteFromData; /* value of byte that was used for padding */
77+
} PKCS7_unPadding;
78+
79+
/*
80+
Remove PKCS7 padding from data.
81+
Your data at the provided address does not change. A copy is created, to which the removing padding is applied.
82+
*/
83+
PKCS7_unPadding* removePadding(const void* const data, const uint64_t dataLength);
84+
```
85+
86+
Also note the `paddingBlockSize` constants from the `PKCS7.h` file, which can be used to pass to addPadding() as the third parameter. For convenience, you can set your own `BLOCK_SIZE_CUSTOM_VALUE` value to this enum:
87+
88+
```C
89+
/*
90+
Examples of commonly used block sizes for data padding.
91+
WARNING: block size for PKCS7 padding can be 0 < BLOCK_SIZE < 256 bytes.
92+
*/
93+
typedef enum {
94+
BLOCK_SIZE_128_BIT = 128 / 8, /* 16 bytes block */
95+
BLOCK_SIZE_256_BIT = 256 / 8, /* 32 bytes block */
96+
BLOCK_SIZE_CUSTOM_VALUE = 0 /* you can set your own constant to use */
97+
} paddingBlockSize; /* can be used as third argument to the function addPadding() */
98+
```
99+
100+
# Demonstration
101+
102+
You can also find `PKCS7_test.out` in the `build` folder. Use it to see how algorithm PKCS7 works.
103+
104+
For example (3 bytes data and 128 bits block size):
105+
106+
```
107+
./build/PKCS7_test.out 24 128
108+
```
109+
110+
Output:
111+
112+
```
113+
************************************
114+
115+
ORIGINAL DATA (size is 3 bytes):
116+
117+
ff fe fd
118+
119+
************************************
120+
121+
WITH PADDING (now size is 16 bytes):
122+
123+
ff fe fd d
124+
d d d d
125+
d d d d
126+
d d d d
127+
128+
************************************
129+
130+
REMOVE PADDING (size is 3 bytes):
131+
132+
ff fe fd
133+
134+
************************************
135+
```

0 commit comments

Comments
 (0)