Back to index

lightning-sunbird  0.9+nobinonly
Classes | Defines | Typedefs | Functions | Variables
arcfour.c File Reference
#include "prerr.h"
#include "secerr.h"
#include "prtypes.h"
#include "blapi.h"

Go to the source code of this file.

Classes

struct  RC4ContextStr

Defines

#define WORDSIZE   sizeof(WORD)
#define ARCFOUR_STATE_SIZE   256
#define MASK1BYTE   (WORD)(0xff)
#define SWAP(a, b)
#define ARCFOUR_STATE_STIR(ii)
#define ARCFOUR_NEXT_BYTE()
#define ARCFOUR_NEXT4BYTES_B(n)
#define ARCFOUR_NEXT_WORD()   { streamWord = 0; ARCFOUR_NEXT4BYTES_B(0); }
#define RSH   >>
#define LSH   <<

Typedefs

typedef unsigned long WORD
typedef PRUint8 Stype

Functions

RC4Context * RC4_AllocateContext (void)
SECStatus RC4_InitContext (RC4Context *cx, const unsigned char *key, unsigned int len, const unsigned char *unused1, int unused2, unsigned int unused3, unsigned int unused4)
RC4Context * RC4_CreateContext (const unsigned char *key, int len)
void RC4_DestroyContext (RC4Context *cx, PRBool freeit)
static SECStatus rc4_unrolled (RC4Context *cx, unsigned char *output, unsigned int *outputLen, unsigned int maxOutputLen, const unsigned char *input, unsigned int inputLen)
SECStatus RC4_Encrypt (RC4Context *cx, unsigned char *output, unsigned int *outputLen, unsigned int maxOutputLen, const unsigned char *input, unsigned int inputLen)
SECStatus RC4_Decrypt (RC4Context *cx, unsigned char *output, unsigned int *outputLen, unsigned int maxOutputLen, const unsigned char *input, unsigned int inputLen)

Variables

static const Stype Kinit [256]

Class Documentation

struct RC4ContextStr

Definition at line 92 of file arcfour.c.

Class Members
Stype i
Stype j
Stype S

Define Documentation

Value:
ARCFOUR_NEXT_BYTE(); streamWord |= (WORD)cx->S[t] << (n + 24); \
       ARCFOUR_NEXT_BYTE(); streamWord |= (WORD)cx->S[t] << (n + 16); \
       ARCFOUR_NEXT_BYTE(); streamWord |= (WORD)cx->S[t] << (n +  8); \
       ARCFOUR_NEXT_BYTE(); streamWord |= (WORD)cx->S[t] << (n     );

Definition at line 347 of file arcfour.c.

Value:
tmpSi = cx->S[++tmpi]; \
       tmpj += tmpSi; \
       tmpSj = cx->S[tmpj]; \
       cx->S[tmpi] = tmpSj; \
       cx->S[tmpj] = tmpSi; \
       t = tmpSi + tmpSj;

Definition at line 225 of file arcfour.c.

#define ARCFOUR_NEXT_WORD ( )    { streamWord = 0; ARCFOUR_NEXT4BYTES_B(0); }

Definition at line 369 of file arcfour.c.

Definition at line 80 of file arcfour.c.

Value:
j = j + cx->S[ii] + K[ii]; \
       SWAP(cx->S[ii], cx->S[j]);
#define LSH   <<

Definition at line 379 of file arcfour.c.

#define MASK1BYTE   (WORD)(0xff)

Definition at line 82 of file arcfour.c.

#define RSH   >>

Definition at line 378 of file arcfour.c.

#define SWAP (   a,
  b 
)
Value:
tmp = a; \
       a = b; \
       b = tmp;

Definition at line 84 of file arcfour.c.

#define WORDSIZE   sizeof(WORD)

Definition at line 72 of file arcfour.c.


Typedef Documentation

typedef PRUint8 Stype

Definition at line 77 of file arcfour.c.

typedef unsigned long WORD

Definition at line 70 of file arcfour.c.


Function Documentation

RC4Context* RC4_AllocateContext ( void  )

Definition at line 144 of file arcfour.c.

{
    return PORT_ZNew(RC4Context);
}

Here is the caller graph for this function:

RC4Context* RC4_CreateContext ( const unsigned char *  key,
int  len 
)

Definition at line 198 of file arcfour.c.

{
    RC4Context *cx = RC4_AllocateContext();
    if (cx) {
       SECStatus rv = RC4_InitContext(cx, key, len, NULL, 0, 0, 0);
       if (rv != SECSuccess) {
           PORT_ZFree(cx, sizeof(*cx));
           cx = NULL;
       }
    }
    return cx;
}

Here is the caller graph for this function:

SECStatus RC4_Decrypt ( RC4Context *  cx,
unsigned char *  output,
unsigned int outputLen,
unsigned int  maxOutputLen,
const unsigned char *  input,
unsigned int  inputLen 
)

Definition at line 615 of file arcfour.c.

{
       PORT_Assert(maxOutputLen >= inputLen);
       if (maxOutputLen < inputLen) {
              PORT_SetError(SEC_ERROR_INVALID_ARGS);
              return SECFailure;
       }
       /* decrypt and encrypt are same operation. */
#if defined(NSS_BEVAND_ARCFOUR)
       ARCFOUR(cx, inputLen, input, output);
        *outputLen = inputLen;
       return SECSuccess;
#elif defined( CONVERT_TO_WORDS )
       /* Convert the byte-stream to a word-stream */
       return rc4_wordconv(cx, output, outputLen, maxOutputLen, input, inputLen);
#else
       /* Operate on bytes, but unroll the main loop */
       return rc4_unrolled(cx, output, outputLen, maxOutputLen, input, inputLen);
#endif
}

Here is the caller graph for this function:

void RC4_DestroyContext ( RC4Context *  cx,
PRBool  freeit 
)

Definition at line 212 of file arcfour.c.

{
       if (freeit)
              PORT_ZFree(cx, sizeof(*cx));
}

Here is the caller graph for this function:

SECStatus RC4_Encrypt ( RC4Context *  cx,
unsigned char *  output,
unsigned int outputLen,
unsigned int  maxOutputLen,
const unsigned char *  input,
unsigned int  inputLen 
)

Definition at line 593 of file arcfour.c.

{
       PORT_Assert(maxOutputLen >= inputLen);
       if (maxOutputLen < inputLen) {
              PORT_SetError(SEC_ERROR_INVALID_ARGS);
              return SECFailure;
       }
#if defined(NSS_BEVAND_ARCFOUR)
       ARCFOUR(cx, inputLen, input, output);
        *outputLen = inputLen;
       return SECSuccess;
#elif defined( CONVERT_TO_WORDS )
       /* Convert the byte-stream to a word-stream */
       return rc4_wordconv(cx, output, outputLen, maxOutputLen, input, inputLen);
#else
       /* Operate on bytes, but unroll the main loop */
       return rc4_unrolled(cx, output, outputLen, maxOutputLen, input, inputLen);
#endif
}

Here is the caller graph for this function:

SECStatus RC4_InitContext ( RC4Context *  cx,
const unsigned char *  key,
unsigned int  len,
const unsigned char *  unused1,
int  unused2,
unsigned int  unused3,
unsigned int  unused4 
)

Definition at line 150 of file arcfour.c.

{
       int i;
       PRUint8 j, tmp;
       PRUint8 K[256];
       PRUint8 *L;
       /* verify the key length. */
       PORT_Assert(len > 0 && len < ARCFOUR_STATE_SIZE);
       if (len < 0 || len >= ARCFOUR_STATE_SIZE) {
              PORT_SetError(SEC_ERROR_INVALID_ARGS);
              return SECFailure;
       }
       if (cx == NULL) {
           PORT_SetError(SEC_ERROR_INVALID_ARGS);
           return SECFailure;
       }
       /* Initialize the state using array indices. */
       memcpy(cx->S, Kinit, sizeof cx->S);
       /* Fill in K repeatedly with values from key. */
       L = K;
       for (i = sizeof K; i > len; i-= len) {
              memcpy(L, key, len);
              L += len;
       }
       memcpy(L, key, i);
       /* Stir the state of the generator.  At this point it is assumed
        * that the key is the size of the state buffer.  If this is not
        * the case, the key bytes are repeated to fill the buffer.
        */
       j = 0;
#define ARCFOUR_STATE_STIR(ii) \
       j = j + cx->S[ii] + K[ii]; \
       SWAP(cx->S[ii], cx->S[j]);
       for (i=0; i<ARCFOUR_STATE_SIZE; i++) {
              ARCFOUR_STATE_STIR(i);
       }
       cx->i = 0;
       cx->j = 0;
       return SECSuccess;
}

Here is the caller graph for this function:

static SECStatus rc4_unrolled ( RC4Context *  cx,
unsigned char *  output,
unsigned int outputLen,
unsigned int  maxOutputLen,
const unsigned char *  input,
unsigned int  inputLen 
) [static]

Definition at line 270 of file arcfour.c.

{
       PRUint8 t;
       Stype tmpSi, tmpSj;
       register PRUint8 tmpi = cx->i;
       register PRUint8 tmpj = cx->j;
       int index;
       PORT_Assert(maxOutputLen >= inputLen);
       if (maxOutputLen < inputLen) {
              PORT_SetError(SEC_ERROR_INVALID_ARGS);
              return SECFailure;
       }
       for (index = inputLen / 8; index-- > 0; input += 8, output += 8) {
              ARCFOUR_NEXT_BYTE();
              output[0] = cx->S[t] ^ input[0];
              ARCFOUR_NEXT_BYTE();
              output[1] = cx->S[t] ^ input[1];
              ARCFOUR_NEXT_BYTE();
              output[2] = cx->S[t] ^ input[2];
              ARCFOUR_NEXT_BYTE();
              output[3] = cx->S[t] ^ input[3];
              ARCFOUR_NEXT_BYTE();
              output[4] = cx->S[t] ^ input[4];
              ARCFOUR_NEXT_BYTE();
              output[5] = cx->S[t] ^ input[5];
              ARCFOUR_NEXT_BYTE();
              output[6] = cx->S[t] ^ input[6];
              ARCFOUR_NEXT_BYTE();
              output[7] = cx->S[t] ^ input[7];
       }
       index = inputLen % 8;
       if (index) {
              input += index;
              output += index;
              switch (index) {
              case 7:
                     ARCFOUR_NEXT_BYTE();
                     output[-7] = cx->S[t] ^ input[-7]; /* FALLTHRU */
              case 6:
                     ARCFOUR_NEXT_BYTE();
                     output[-6] = cx->S[t] ^ input[-6]; /* FALLTHRU */
              case 5:
                     ARCFOUR_NEXT_BYTE();
                     output[-5] = cx->S[t] ^ input[-5]; /* FALLTHRU */
              case 4:
                     ARCFOUR_NEXT_BYTE();
                     output[-4] = cx->S[t] ^ input[-4]; /* FALLTHRU */
              case 3:
                     ARCFOUR_NEXT_BYTE();
                     output[-3] = cx->S[t] ^ input[-3]; /* FALLTHRU */
              case 2:
                     ARCFOUR_NEXT_BYTE();
                     output[-2] = cx->S[t] ^ input[-2]; /* FALLTHRU */
              case 1:
                     ARCFOUR_NEXT_BYTE();
                     output[-1] = cx->S[t] ^ input[-1]; /* FALLTHRU */
              default:
                     /* FALLTHRU */
                     ; /* hp-ux build breaks without this */
              }
       }
       cx->i = tmpi;
       cx->j = tmpj;
       *outputLen = inputLen;
       return SECSuccess;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

const Stype Kinit[256] [static]

Definition at line 108 of file arcfour.c.