security/nss/lib/softoken/padbuf.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial