comparison dcrypt/crypto/padding/PKCS7.d @ 27:8b5eaf3c2979

Fixed error in hash message padding reported by Glenn Haecker.
author Thomas Dixon <reikon@reikon.us>
date Sat, 09 May 2009 23:29:20 -0400
parents cd376996cdb3
children ad687db713a4
comparison
equal deleted inserted replaced
26:176c933827a8 27:8b5eaf3c2979
12 12
13 /** 13 /**
14 * This class implements the padding scheme described in PKCS7 14 * This class implements the padding scheme described in PKCS7
15 * from RSA Security. Ex. [... 0x03, 0x03, 0x03] 15 * from RSA Security. Ex. [... 0x03, 0x03, 0x03]
16 */ 16 */
17 class PKCS7 : BlockCipherPadding { 17 class PKCS7 : BlockCipherPadding
18 char[] name() { 18 {
19 char[] name()
20 {
19 return "PKCS7"; 21 return "PKCS7";
20 } 22 }
21 23
22 ubyte[] pad(uint len) { 24 ubyte[] pad(uint len)
25 {
23 ubyte[] output = new ubyte[len]; 26 ubyte[] output = new ubyte[len];
24 27
25 output[0..output.length] = cast(byte)len; 28 output[0..output.length] = cast(byte)len;
26 29
27 return output; 30 return output;
28 } 31 }
29 32
30 uint unpad(void[] input_) { 33 uint unpad(void[] input_)
34 {
31 ubyte[] input = cast(ubyte[]) input_; 35 ubyte[] input = cast(ubyte[]) input_;
32 36
33 ubyte len = input[input.length-1]; 37 ubyte len = input[input.length-1];
34 38
35 if (len > input.length || len == 0) 39 if (len > input.length || len == 0)
36 throw new InvalidPaddingError(name()~": Incorrect padding."); 40 throw new InvalidPaddingError(name()~": Incorrect padding.");
37 41
38 uint limit = input.length; 42 uint limit = input.length;
39 for (int i = 0; i < len; i++) 43 for (int i = 0; i < len; i++)
40 if (input[--limit] != len) 44 if (input[--limit] != len)
41 throw new InvalidPaddingError( 45 throw new InvalidPaddingError(name()~": Pad value does not match pad length.");
42 name()~": Pad value does not match pad length."); 46
43 return len; 47 return len;
44 } 48 }
45 } 49 }