michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: #include "blapit.h" michael@0: #include "secport.h" michael@0: #include "secerr.h" michael@0: michael@0: /* michael@0: * Prepare a buffer for any padded CBC encryption algorithm, growing to the michael@0: * appropriate boundary and filling with the appropriate padding. michael@0: * blockSize must be a power of 2. michael@0: * michael@0: * NOTE: If arena is non-NULL, we re-allocate from there, otherwise michael@0: * we assume (and use) XP memory (re)allocation. michael@0: */ michael@0: unsigned char * michael@0: CBC_PadBuffer(PLArenaPool *arena, unsigned char *inbuf, unsigned int inlen, michael@0: unsigned int *outlen, int blockSize) michael@0: { michael@0: unsigned char *outbuf; michael@0: unsigned int des_len; michael@0: unsigned int i; michael@0: unsigned char des_pad_len; michael@0: michael@0: /* michael@0: * We need from 1 to blockSize bytes -- we *always* grow. michael@0: * The extra bytes contain the value of the length of the padding: michael@0: * if we have 2 bytes of padding, then the padding is "0x02, 0x02". michael@0: */ michael@0: des_len = (inlen + blockSize) & ~(blockSize - 1); michael@0: michael@0: if (arena != NULL) { michael@0: outbuf = (unsigned char*)PORT_ArenaGrow (arena, inbuf, inlen, des_len); michael@0: } else { michael@0: outbuf = (unsigned char*)PORT_Realloc (inbuf, des_len); michael@0: } michael@0: michael@0: if (outbuf == NULL) { michael@0: PORT_SetError (SEC_ERROR_NO_MEMORY); michael@0: return NULL; michael@0: } michael@0: michael@0: des_pad_len = des_len - inlen; michael@0: for (i = inlen; i < des_len; i++) michael@0: outbuf[i] = des_pad_len; michael@0: michael@0: *outlen = des_len; michael@0: return outbuf; michael@0: }