1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/nss/lib/softoken/padbuf.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,49 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 +#include "blapit.h" 1.8 +#include "secport.h" 1.9 +#include "secerr.h" 1.10 + 1.11 +/* 1.12 + * Prepare a buffer for any padded CBC encryption algorithm, growing to the 1.13 + * appropriate boundary and filling with the appropriate padding. 1.14 + * blockSize must be a power of 2. 1.15 + * 1.16 + * NOTE: If arena is non-NULL, we re-allocate from there, otherwise 1.17 + * we assume (and use) XP memory (re)allocation. 1.18 + */ 1.19 +unsigned char * 1.20 +CBC_PadBuffer(PLArenaPool *arena, unsigned char *inbuf, unsigned int inlen, 1.21 + unsigned int *outlen, int blockSize) 1.22 +{ 1.23 + unsigned char *outbuf; 1.24 + unsigned int des_len; 1.25 + unsigned int i; 1.26 + unsigned char des_pad_len; 1.27 + 1.28 + /* 1.29 + * We need from 1 to blockSize bytes -- we *always* grow. 1.30 + * The extra bytes contain the value of the length of the padding: 1.31 + * if we have 2 bytes of padding, then the padding is "0x02, 0x02". 1.32 + */ 1.33 + des_len = (inlen + blockSize) & ~(blockSize - 1); 1.34 + 1.35 + if (arena != NULL) { 1.36 + outbuf = (unsigned char*)PORT_ArenaGrow (arena, inbuf, inlen, des_len); 1.37 + } else { 1.38 + outbuf = (unsigned char*)PORT_Realloc (inbuf, des_len); 1.39 + } 1.40 + 1.41 + if (outbuf == NULL) { 1.42 + PORT_SetError (SEC_ERROR_NO_MEMORY); 1.43 + return NULL; 1.44 + } 1.45 + 1.46 + des_pad_len = des_len - inlen; 1.47 + for (i = inlen; i < des_len; i++) 1.48 + outbuf[i] = des_pad_len; 1.49 + 1.50 + *outlen = des_len; 1.51 + return outbuf; 1.52 +}