/src/nss-nspr/nss/lib/softoken/padbuf.c
Line | Count | Source (jump to first uncovered line) |
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 | 0 | { |
20 | 0 | unsigned char *outbuf; |
21 | 0 | unsigned int des_len; |
22 | 0 | unsigned int i; |
23 | 0 | 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 | 0 | des_len = (inlen + blockSize) & ~(blockSize - 1); |
31 | |
|
32 | 0 | if (arena != NULL) { |
33 | 0 | outbuf = (unsigned char *)PORT_ArenaGrow(arena, inbuf, inlen, des_len); |
34 | 0 | } else { |
35 | 0 | outbuf = (unsigned char *)PORT_Realloc(inbuf, des_len); |
36 | 0 | } |
37 | |
|
38 | 0 | if (outbuf == NULL) { |
39 | 0 | PORT_SetError(SEC_ERROR_NO_MEMORY); |
40 | 0 | return NULL; |
41 | 0 | } |
42 | | |
43 | 0 | des_pad_len = des_len - inlen; |
44 | 0 | for (i = inlen; i < des_len; i++) |
45 | 0 | outbuf[i] = des_pad_len; |
46 | |
|
47 | 0 | *outlen = des_len; |
48 | 0 | return outbuf; |
49 | 0 | } |