michael@0: /* arcfour.c - the arc four algorithm. michael@0: * 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: michael@0: #ifdef FREEBL_NO_DEPEND michael@0: #include "stubs.h" michael@0: #endif michael@0: michael@0: #include "prerr.h" michael@0: #include "secerr.h" michael@0: michael@0: #include "prtypes.h" michael@0: #include "blapi.h" michael@0: michael@0: /* Architecture-dependent defines */ michael@0: michael@0: #if defined(SOLARIS) || defined(HPUX) || defined(NSS_X86) || \ michael@0: defined(_WIN64) michael@0: /* Convert the byte-stream to a word-stream */ michael@0: #define CONVERT_TO_WORDS michael@0: #endif michael@0: michael@0: #if defined(AIX) || defined(OSF1) || defined(NSS_BEVAND_ARCFOUR) michael@0: /* Treat array variables as words, not bytes, on CPUs that take michael@0: * much longer to write bytes than to write words, or when using michael@0: * assembler code that required it. michael@0: */ michael@0: #define USE_WORD michael@0: #endif michael@0: michael@0: #if defined(IS_64) || defined(NSS_BEVAND_ARCFOUR) michael@0: typedef PRUint64 WORD; michael@0: #else michael@0: typedef PRUint32 WORD; michael@0: #endif michael@0: #define WORDSIZE sizeof(WORD) michael@0: michael@0: #if defined(USE_WORD) michael@0: typedef WORD Stype; michael@0: #else michael@0: typedef PRUint8 Stype; michael@0: #endif michael@0: michael@0: #define ARCFOUR_STATE_SIZE 256 michael@0: michael@0: #define MASK1BYTE (WORD)(0xff) michael@0: michael@0: #define SWAP(a, b) \ michael@0: tmp = a; \ michael@0: a = b; \ michael@0: b = tmp; michael@0: michael@0: /* michael@0: * State information for stream cipher. michael@0: */ michael@0: struct RC4ContextStr michael@0: { michael@0: #if defined(NSS_ARCFOUR_IJ_B4_S) || defined(NSS_BEVAND_ARCFOUR) michael@0: Stype i; michael@0: Stype j; michael@0: Stype S[ARCFOUR_STATE_SIZE]; michael@0: #else michael@0: Stype S[ARCFOUR_STATE_SIZE]; michael@0: Stype i; michael@0: Stype j; michael@0: #endif michael@0: }; michael@0: michael@0: /* michael@0: * array indices [0..255] to initialize cx->S array (faster than loop). michael@0: */ michael@0: static const Stype Kinit[256] = { michael@0: 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, michael@0: 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, michael@0: 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, michael@0: 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, michael@0: 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, michael@0: 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, michael@0: 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, michael@0: 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, michael@0: 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, michael@0: 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, michael@0: 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, michael@0: 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, michael@0: 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, michael@0: 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, michael@0: 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, michael@0: 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, michael@0: 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, michael@0: 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, michael@0: 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, michael@0: 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, michael@0: 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, michael@0: 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, michael@0: 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, michael@0: 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, michael@0: 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, michael@0: 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, michael@0: 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, michael@0: 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, michael@0: 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, michael@0: 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, michael@0: 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, michael@0: 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff michael@0: }; michael@0: michael@0: RC4Context * michael@0: RC4_AllocateContext(void) michael@0: { michael@0: return PORT_ZNew(RC4Context); michael@0: } michael@0: michael@0: SECStatus michael@0: RC4_InitContext(RC4Context *cx, const unsigned char *key, unsigned int len, michael@0: const unsigned char * unused1, int unused2, michael@0: unsigned int unused3, unsigned int unused4) michael@0: { michael@0: unsigned int i; michael@0: PRUint8 j, tmp; michael@0: PRUint8 K[256]; michael@0: PRUint8 *L; michael@0: michael@0: /* verify the key length. */ michael@0: PORT_Assert(len > 0 && len < ARCFOUR_STATE_SIZE); michael@0: if (len == 0 || len >= ARCFOUR_STATE_SIZE) { michael@0: PORT_SetError(SEC_ERROR_BAD_KEY); michael@0: return SECFailure; michael@0: } michael@0: if (cx == NULL) { michael@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); michael@0: return SECFailure; michael@0: } michael@0: /* Initialize the state using array indices. */ michael@0: memcpy(cx->S, Kinit, sizeof cx->S); michael@0: /* Fill in K repeatedly with values from key. */ michael@0: L = K; michael@0: for (i = sizeof K; i > len; i-= len) { michael@0: memcpy(L, key, len); michael@0: L += len; michael@0: } michael@0: memcpy(L, key, i); michael@0: /* Stir the state of the generator. At this point it is assumed michael@0: * that the key is the size of the state buffer. If this is not michael@0: * the case, the key bytes are repeated to fill the buffer. michael@0: */ michael@0: j = 0; michael@0: #define ARCFOUR_STATE_STIR(ii) \ michael@0: j = j + cx->S[ii] + K[ii]; \ michael@0: SWAP(cx->S[ii], cx->S[j]); michael@0: for (i=0; ii = 0; michael@0: cx->j = 0; michael@0: return SECSuccess; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Initialize a new generator. michael@0: */ michael@0: RC4Context * michael@0: RC4_CreateContext(const unsigned char *key, int len) michael@0: { michael@0: RC4Context *cx = RC4_AllocateContext(); michael@0: if (cx) { michael@0: SECStatus rv = RC4_InitContext(cx, key, len, NULL, 0, 0, 0); michael@0: if (rv != SECSuccess) { michael@0: PORT_ZFree(cx, sizeof(*cx)); michael@0: cx = NULL; michael@0: } michael@0: } michael@0: return cx; michael@0: } michael@0: michael@0: void michael@0: RC4_DestroyContext(RC4Context *cx, PRBool freeit) michael@0: { michael@0: if (freeit) michael@0: PORT_ZFree(cx, sizeof(*cx)); michael@0: } michael@0: michael@0: #if defined(NSS_BEVAND_ARCFOUR) michael@0: extern void ARCFOUR(RC4Context *cx, WORD inputLen, michael@0: const unsigned char *input, unsigned char *output); michael@0: #else michael@0: /* michael@0: * Generate the next byte in the stream. michael@0: */ michael@0: #define ARCFOUR_NEXT_BYTE() \ michael@0: tmpSi = cx->S[++tmpi]; \ michael@0: tmpj += tmpSi; \ michael@0: tmpSj = cx->S[tmpj]; \ michael@0: cx->S[tmpi] = tmpSj; \ michael@0: cx->S[tmpj] = tmpSi; \ michael@0: t = tmpSi + tmpSj; michael@0: michael@0: #ifdef CONVERT_TO_WORDS michael@0: /* michael@0: * Straight ARCFOUR op. No optimization. michael@0: */ michael@0: static SECStatus michael@0: rc4_no_opt(RC4Context *cx, unsigned char *output, michael@0: unsigned int *outputLen, unsigned int maxOutputLen, michael@0: const unsigned char *input, unsigned int inputLen) michael@0: { michael@0: PRUint8 t; michael@0: Stype tmpSi, tmpSj; michael@0: register PRUint8 tmpi = cx->i; michael@0: register PRUint8 tmpj = cx->j; michael@0: unsigned int index; michael@0: PORT_Assert(maxOutputLen >= inputLen); michael@0: if (maxOutputLen < inputLen) { michael@0: PORT_SetError(SEC_ERROR_OUTPUT_LEN); michael@0: return SECFailure; michael@0: } michael@0: for (index=0; index < inputLen; index++) { michael@0: /* Generate next byte from stream. */ michael@0: ARCFOUR_NEXT_BYTE(); michael@0: /* output = next stream byte XOR next input byte */ michael@0: output[index] = cx->S[t] ^ input[index]; michael@0: } michael@0: *outputLen = inputLen; michael@0: cx->i = tmpi; michael@0: cx->j = tmpj; michael@0: return SECSuccess; michael@0: } michael@0: michael@0: #else michael@0: /* !CONVERT_TO_WORDS */ michael@0: michael@0: /* michael@0: * Byte-at-a-time ARCFOUR, unrolling the loop into 8 pieces. michael@0: */ michael@0: static SECStatus michael@0: rc4_unrolled(RC4Context *cx, unsigned char *output, michael@0: unsigned int *outputLen, unsigned int maxOutputLen, michael@0: const unsigned char *input, unsigned int inputLen) michael@0: { michael@0: PRUint8 t; michael@0: Stype tmpSi, tmpSj; michael@0: register PRUint8 tmpi = cx->i; michael@0: register PRUint8 tmpj = cx->j; michael@0: int index; michael@0: PORT_Assert(maxOutputLen >= inputLen); michael@0: if (maxOutputLen < inputLen) { michael@0: PORT_SetError(SEC_ERROR_OUTPUT_LEN); michael@0: return SECFailure; michael@0: } michael@0: for (index = inputLen / 8; index-- > 0; input += 8, output += 8) { michael@0: ARCFOUR_NEXT_BYTE(); michael@0: output[0] = cx->S[t] ^ input[0]; michael@0: ARCFOUR_NEXT_BYTE(); michael@0: output[1] = cx->S[t] ^ input[1]; michael@0: ARCFOUR_NEXT_BYTE(); michael@0: output[2] = cx->S[t] ^ input[2]; michael@0: ARCFOUR_NEXT_BYTE(); michael@0: output[3] = cx->S[t] ^ input[3]; michael@0: ARCFOUR_NEXT_BYTE(); michael@0: output[4] = cx->S[t] ^ input[4]; michael@0: ARCFOUR_NEXT_BYTE(); michael@0: output[5] = cx->S[t] ^ input[5]; michael@0: ARCFOUR_NEXT_BYTE(); michael@0: output[6] = cx->S[t] ^ input[6]; michael@0: ARCFOUR_NEXT_BYTE(); michael@0: output[7] = cx->S[t] ^ input[7]; michael@0: } michael@0: index = inputLen % 8; michael@0: if (index) { michael@0: input += index; michael@0: output += index; michael@0: switch (index) { michael@0: case 7: michael@0: ARCFOUR_NEXT_BYTE(); michael@0: output[-7] = cx->S[t] ^ input[-7]; /* FALLTHRU */ michael@0: case 6: michael@0: ARCFOUR_NEXT_BYTE(); michael@0: output[-6] = cx->S[t] ^ input[-6]; /* FALLTHRU */ michael@0: case 5: michael@0: ARCFOUR_NEXT_BYTE(); michael@0: output[-5] = cx->S[t] ^ input[-5]; /* FALLTHRU */ michael@0: case 4: michael@0: ARCFOUR_NEXT_BYTE(); michael@0: output[-4] = cx->S[t] ^ input[-4]; /* FALLTHRU */ michael@0: case 3: michael@0: ARCFOUR_NEXT_BYTE(); michael@0: output[-3] = cx->S[t] ^ input[-3]; /* FALLTHRU */ michael@0: case 2: michael@0: ARCFOUR_NEXT_BYTE(); michael@0: output[-2] = cx->S[t] ^ input[-2]; /* FALLTHRU */ michael@0: case 1: michael@0: ARCFOUR_NEXT_BYTE(); michael@0: output[-1] = cx->S[t] ^ input[-1]; /* FALLTHRU */ michael@0: default: michael@0: /* FALLTHRU */ michael@0: ; /* hp-ux build breaks without this */ michael@0: } michael@0: } michael@0: cx->i = tmpi; michael@0: cx->j = tmpj; michael@0: *outputLen = inputLen; michael@0: return SECSuccess; michael@0: } michael@0: #endif michael@0: michael@0: #ifdef IS_LITTLE_ENDIAN michael@0: #define ARCFOUR_NEXT4BYTES_L(n) \ michael@0: ARCFOUR_NEXT_BYTE(); streamWord |= (WORD)cx->S[t] << (n ); \ michael@0: ARCFOUR_NEXT_BYTE(); streamWord |= (WORD)cx->S[t] << (n + 8); \ michael@0: ARCFOUR_NEXT_BYTE(); streamWord |= (WORD)cx->S[t] << (n + 16); \ michael@0: ARCFOUR_NEXT_BYTE(); streamWord |= (WORD)cx->S[t] << (n + 24); michael@0: #else michael@0: #define ARCFOUR_NEXT4BYTES_B(n) \ michael@0: ARCFOUR_NEXT_BYTE(); streamWord |= (WORD)cx->S[t] << (n + 24); \ michael@0: ARCFOUR_NEXT_BYTE(); streamWord |= (WORD)cx->S[t] << (n + 16); \ michael@0: ARCFOUR_NEXT_BYTE(); streamWord |= (WORD)cx->S[t] << (n + 8); \ michael@0: ARCFOUR_NEXT_BYTE(); streamWord |= (WORD)cx->S[t] << (n ); michael@0: #endif michael@0: michael@0: #if (defined(IS_64) && !defined(__sparc)) || defined(NSS_USE_64) michael@0: /* 64-bit wordsize */ michael@0: #ifdef IS_LITTLE_ENDIAN michael@0: #define ARCFOUR_NEXT_WORD() \ michael@0: { streamWord = 0; ARCFOUR_NEXT4BYTES_L(0); ARCFOUR_NEXT4BYTES_L(32); } michael@0: #else michael@0: #define ARCFOUR_NEXT_WORD() \ michael@0: { streamWord = 0; ARCFOUR_NEXT4BYTES_B(32); ARCFOUR_NEXT4BYTES_B(0); } michael@0: #endif michael@0: #else michael@0: /* 32-bit wordsize */ michael@0: #ifdef IS_LITTLE_ENDIAN michael@0: #define ARCFOUR_NEXT_WORD() \ michael@0: { streamWord = 0; ARCFOUR_NEXT4BYTES_L(0); } michael@0: #else michael@0: #define ARCFOUR_NEXT_WORD() \ michael@0: { streamWord = 0; ARCFOUR_NEXT4BYTES_B(0); } michael@0: #endif michael@0: #endif michael@0: michael@0: #ifdef IS_LITTLE_ENDIAN michael@0: #define RSH << michael@0: #define LSH >> michael@0: #else michael@0: #define RSH >> michael@0: #define LSH << michael@0: #endif michael@0: michael@0: #ifdef IS_LITTLE_ENDIAN michael@0: #define LEFTMOST_BYTE_SHIFT 0 michael@0: #define NEXT_BYTE_SHIFT(shift) shift + 8 michael@0: #else michael@0: #define LEFTMOST_BYTE_SHIFT 8*(WORDSIZE - 1) michael@0: #define NEXT_BYTE_SHIFT(shift) shift - 8 michael@0: #endif michael@0: michael@0: #ifdef CONVERT_TO_WORDS michael@0: static SECStatus michael@0: rc4_wordconv(RC4Context *cx, unsigned char *output, michael@0: unsigned int *outputLen, unsigned int maxOutputLen, michael@0: const unsigned char *input, unsigned int inputLen) michael@0: { michael@0: PR_STATIC_ASSERT(sizeof(PRUword) == sizeof(ptrdiff_t)); michael@0: unsigned int inOffset = (PRUword)input % WORDSIZE; michael@0: unsigned int outOffset = (PRUword)output % WORDSIZE; michael@0: register WORD streamWord; michael@0: register const WORD *pInWord; michael@0: register WORD *pOutWord; michael@0: register WORD inWord, nextInWord; michael@0: PRUint8 t; michael@0: register Stype tmpSi, tmpSj; michael@0: register PRUint8 tmpi = cx->i; michael@0: register PRUint8 tmpj = cx->j; michael@0: unsigned int bufShift, invBufShift; michael@0: unsigned int i; michael@0: const unsigned char *finalIn; michael@0: unsigned char *finalOut; michael@0: michael@0: PORT_Assert(maxOutputLen >= inputLen); michael@0: if (maxOutputLen < inputLen) { michael@0: PORT_SetError(SEC_ERROR_OUTPUT_LEN); michael@0: return SECFailure; michael@0: } michael@0: if (inputLen < 2*WORDSIZE) { michael@0: /* Ignore word conversion, do byte-at-a-time */ michael@0: return rc4_no_opt(cx, output, outputLen, maxOutputLen, input, inputLen); michael@0: } michael@0: *outputLen = inputLen; michael@0: pInWord = (const WORD *)(input - inOffset); michael@0: pOutWord = (WORD *)(output - outOffset); michael@0: if (inOffset <= outOffset) { michael@0: bufShift = 8*(outOffset - inOffset); michael@0: invBufShift = 8*WORDSIZE - bufShift; michael@0: } else { michael@0: invBufShift = 8*(inOffset - outOffset); michael@0: bufShift = 8*WORDSIZE - invBufShift; michael@0: } michael@0: /*****************************************************************/ michael@0: /* Step 1: */ michael@0: /* If the first output word is partial, consume the bytes in the */ michael@0: /* first partial output word by loading one or two words of */ michael@0: /* input and shifting them accordingly. Otherwise, just load */ michael@0: /* in the first word of input. At the end of this block, at */ michael@0: /* least one partial word of input should ALWAYS be loaded. */ michael@0: /*****************************************************************/ michael@0: if (outOffset) { michael@0: unsigned int byteCount = WORDSIZE - outOffset; michael@0: for (i = 0; i < byteCount; i++) { michael@0: ARCFOUR_NEXT_BYTE(); michael@0: output[i] = cx->S[t] ^ input[i]; michael@0: } michael@0: /* Consumed byteCount bytes of input */ michael@0: inputLen -= byteCount; michael@0: pInWord++; michael@0: michael@0: /* move to next word of output */ michael@0: pOutWord++; michael@0: michael@0: /* If buffers are relatively misaligned, shift the bytes in inWord michael@0: * to be aligned to the output buffer. michael@0: */ michael@0: if (inOffset < outOffset) { michael@0: /* The first input word (which may be partial) has more bytes michael@0: * than needed. Copy the remainder to inWord. michael@0: */ michael@0: unsigned int shift = LEFTMOST_BYTE_SHIFT; michael@0: inWord = 0; michael@0: for (i = 0; i < outOffset - inOffset; i++) { michael@0: inWord |= (WORD)input[byteCount + i] << shift; michael@0: shift = NEXT_BYTE_SHIFT(shift); michael@0: } michael@0: } else if (inOffset > outOffset) { michael@0: /* Consumed some bytes in the second input word. Copy the michael@0: * remainder to inWord. michael@0: */ michael@0: inWord = *pInWord++; michael@0: inWord = inWord LSH invBufShift; michael@0: } else { michael@0: inWord = 0; michael@0: } michael@0: } else { michael@0: /* output is word-aligned */ michael@0: if (inOffset) { michael@0: /* Input is not word-aligned. The first word load of input michael@0: * will not produce a full word of input bytes, so one word michael@0: * must be pre-loaded. The main loop below will load in the michael@0: * next input word and shift some of its bytes into inWord michael@0: * in order to create a full input word. Note that the main michael@0: * loop must execute at least once because the input must michael@0: * be at least two words. michael@0: */ michael@0: unsigned int shift = LEFTMOST_BYTE_SHIFT; michael@0: inWord = 0; michael@0: for (i = 0; i < WORDSIZE - inOffset; i++) { michael@0: inWord |= (WORD)input[i] << shift; michael@0: shift = NEXT_BYTE_SHIFT(shift); michael@0: } michael@0: pInWord++; michael@0: } else { michael@0: /* Input is word-aligned. The first word load of input michael@0: * will produce a full word of input bytes, so nothing michael@0: * needs to be loaded here. michael@0: */ michael@0: inWord = 0; michael@0: } michael@0: } michael@0: /*****************************************************************/ michael@0: /* Step 2: main loop */ michael@0: /* At this point the output buffer is word-aligned. Any unused */ michael@0: /* bytes from above will be in inWord (shifted correctly). If */ michael@0: /* the input buffer is unaligned relative to the output buffer, */ michael@0: /* shifting has to be done. */ michael@0: /*****************************************************************/ michael@0: if (bufShift) { michael@0: /* preloadedByteCount is the number of input bytes pre-loaded michael@0: * in inWord. michael@0: */ michael@0: unsigned int preloadedByteCount = bufShift/8; michael@0: for (; inputLen >= preloadedByteCount + WORDSIZE; michael@0: inputLen -= WORDSIZE) { michael@0: nextInWord = *pInWord++; michael@0: inWord |= nextInWord RSH bufShift; michael@0: nextInWord = nextInWord LSH invBufShift; michael@0: ARCFOUR_NEXT_WORD(); michael@0: *pOutWord++ = inWord ^ streamWord; michael@0: inWord = nextInWord; michael@0: } michael@0: if (inputLen == 0) { michael@0: /* Nothing left to do. */ michael@0: cx->i = tmpi; michael@0: cx->j = tmpj; michael@0: return SECSuccess; michael@0: } michael@0: finalIn = (const unsigned char *)pInWord - preloadedByteCount; michael@0: } else { michael@0: for (; inputLen >= WORDSIZE; inputLen -= WORDSIZE) { michael@0: inWord = *pInWord++; michael@0: ARCFOUR_NEXT_WORD(); michael@0: *pOutWord++ = inWord ^ streamWord; michael@0: } michael@0: if (inputLen == 0) { michael@0: /* Nothing left to do. */ michael@0: cx->i = tmpi; michael@0: cx->j = tmpj; michael@0: return SECSuccess; michael@0: } michael@0: finalIn = (const unsigned char *)pInWord; michael@0: } michael@0: /*****************************************************************/ michael@0: /* Step 3: */ michael@0: /* Do the remaining partial word of input one byte at a time. */ michael@0: /*****************************************************************/ michael@0: finalOut = (unsigned char *)pOutWord; michael@0: for (i = 0; i < inputLen; i++) { michael@0: ARCFOUR_NEXT_BYTE(); michael@0: finalOut[i] = cx->S[t] ^ finalIn[i]; michael@0: } michael@0: cx->i = tmpi; michael@0: cx->j = tmpj; michael@0: return SECSuccess; michael@0: } michael@0: #endif michael@0: #endif /* NSS_BEVAND_ARCFOUR */ michael@0: michael@0: SECStatus michael@0: RC4_Encrypt(RC4Context *cx, unsigned char *output, michael@0: unsigned int *outputLen, unsigned int maxOutputLen, michael@0: const unsigned char *input, unsigned int inputLen) michael@0: { michael@0: PORT_Assert(maxOutputLen >= inputLen); michael@0: if (maxOutputLen < inputLen) { michael@0: PORT_SetError(SEC_ERROR_OUTPUT_LEN); michael@0: return SECFailure; michael@0: } michael@0: #if defined(NSS_BEVAND_ARCFOUR) michael@0: ARCFOUR(cx, inputLen, input, output); michael@0: *outputLen = inputLen; michael@0: return SECSuccess; michael@0: #elif defined( CONVERT_TO_WORDS ) michael@0: /* Convert the byte-stream to a word-stream */ michael@0: return rc4_wordconv(cx, output, outputLen, maxOutputLen, input, inputLen); michael@0: #else michael@0: /* Operate on bytes, but unroll the main loop */ michael@0: return rc4_unrolled(cx, output, outputLen, maxOutputLen, input, inputLen); michael@0: #endif michael@0: } michael@0: michael@0: SECStatus RC4_Decrypt(RC4Context *cx, unsigned char *output, michael@0: unsigned int *outputLen, unsigned int maxOutputLen, michael@0: const unsigned char *input, unsigned int inputLen) michael@0: { michael@0: PORT_Assert(maxOutputLen >= inputLen); michael@0: if (maxOutputLen < inputLen) { michael@0: PORT_SetError(SEC_ERROR_OUTPUT_LEN); michael@0: return SECFailure; michael@0: } michael@0: /* decrypt and encrypt are same operation. */ michael@0: #if defined(NSS_BEVAND_ARCFOUR) michael@0: ARCFOUR(cx, inputLen, input, output); michael@0: *outputLen = inputLen; michael@0: return SECSuccess; michael@0: #elif defined( CONVERT_TO_WORDS ) michael@0: /* Convert the byte-stream to a word-stream */ michael@0: return rc4_wordconv(cx, output, outputLen, maxOutputLen, input, inputLen); michael@0: #else michael@0: /* Operate on bytes, but unroll the main loop */ michael@0: return rc4_unrolled(cx, output, outputLen, maxOutputLen, input, inputLen); michael@0: #endif michael@0: } michael@0: michael@0: #undef CONVERT_TO_WORDS michael@0: #undef USE_WORD