0
|
1 /**
|
|
2 * This file is part of the dcrypt project.
|
|
3 *
|
|
4 * Copyright: Copyright (C) dcrypt contributors 2008. All rights reserved.
|
|
5 * License: MIT
|
|
6 * Authors: Thomas Dixon
|
|
7 */
|
|
8
|
|
9 module dcrypt.crypto.padding.PKCS7;
|
|
10
|
|
11 import dcrypt.crypto.BlockCipherPadding;
|
|
12
|
|
13 /**
|
|
14 * This class implements the padding scheme described in PKCS7
|
|
15 * from RSA Security. Ex. [... 0x03, 0x03, 0x03]
|
|
16 */
|
|
17 class PKCS7 : BlockCipherPadding {
|
|
18 char[] name() {
|
|
19 return "PKCS7";
|
|
20 }
|
|
21
|
|
22 /* Assumes input_ is a multiple of the underlying
|
|
23 * block cipher's block size.
|
|
24 */
|
|
25 uint padBlock(void[] input_, uint inOff) {
|
|
26 ubyte[] input = cast(ubyte[]) input_;
|
|
27
|
|
28 ubyte len = (input.length - inOff);
|
|
29
|
|
30 input[inOff..input.length] = len;
|
|
31
|
|
32 return len;
|
|
33 }
|
|
34
|
|
35 uint padLength(void[] input_) {
|
|
36 ubyte[] input = cast(ubyte[]) input_;
|
|
37
|
|
38 ubyte len = input[input.length-1];
|
|
39
|
|
40 if (len > input.length || len == 0)
|
|
41 throw new InvalidPaddingError(name()~": Incorrect padding.");
|
|
42
|
|
43 uint limit = input.length;
|
|
44 for (int i = 0; i < len; i++)
|
|
45 if (input[--limit] != len)
|
|
46 throw new InvalidPaddingError(
|
|
47 name()~": Pad value does not match pad length.");
|
|
48 return len;
|
|
49 }
|
|
50 }
|