|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 #include "blapit.h" |
|
5 #include "secport.h" |
|
6 #include "secerr.h" |
|
7 |
|
8 /* |
|
9 * Prepare a buffer for any padded CBC encryption algorithm, growing to the |
|
10 * appropriate boundary and filling with the appropriate padding. |
|
11 * blockSize must be a power of 2. |
|
12 * |
|
13 * NOTE: If arena is non-NULL, we re-allocate from there, otherwise |
|
14 * we assume (and use) XP memory (re)allocation. |
|
15 */ |
|
16 unsigned char * |
|
17 CBC_PadBuffer(PLArenaPool *arena, unsigned char *inbuf, unsigned int inlen, |
|
18 unsigned int *outlen, int blockSize) |
|
19 { |
|
20 unsigned char *outbuf; |
|
21 unsigned int des_len; |
|
22 unsigned int i; |
|
23 unsigned char des_pad_len; |
|
24 |
|
25 /* |
|
26 * We need from 1 to blockSize bytes -- we *always* grow. |
|
27 * The extra bytes contain the value of the length of the padding: |
|
28 * if we have 2 bytes of padding, then the padding is "0x02, 0x02". |
|
29 */ |
|
30 des_len = (inlen + blockSize) & ~(blockSize - 1); |
|
31 |
|
32 if (arena != NULL) { |
|
33 outbuf = (unsigned char*)PORT_ArenaGrow (arena, inbuf, inlen, des_len); |
|
34 } else { |
|
35 outbuf = (unsigned char*)PORT_Realloc (inbuf, des_len); |
|
36 } |
|
37 |
|
38 if (outbuf == NULL) { |
|
39 PORT_SetError (SEC_ERROR_NO_MEMORY); |
|
40 return NULL; |
|
41 } |
|
42 |
|
43 des_pad_len = des_len - inlen; |
|
44 for (i = inlen; i < des_len; i++) |
|
45 outbuf[i] = des_pad_len; |
|
46 |
|
47 *outlen = des_len; |
|
48 return outbuf; |
|
49 } |