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