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