static void init_rand_state(struct frandom_state *state, int seedflag)
{
	unsigned int i, j, k;
	u8 *S;
	u8 *seed = state->buf;

	if (seedflag == INTERNAL_SEED)
		erandom_get_random_bytes(seed, 256);
	else
		get_random_bytes(seed, 256);

	S = state->S;
	for (i=0; i<256; i++)
		*S++=i;

	j=0;
	S = state->S;

	for (i=0; i<256; i++) {
		j = (j + S[i] + *seed++) & 0xff;
		swap_byte(&S[i], &S[j]);
	}

	i=0; j=0;
	for (k=0; k<256; k++) {
		i = (i + 1) & 0xff;
		j = (j + S[i]) & 0xff;
		swap_byte(&S[i], &S[j]);
	}

	state->i = i;
	state->j = j;
}
Example #2
0
static void init_rand_state(struct frandom_state *state, int seedflag)
{
	unsigned int i, j, k;
	u8 *S;
	u8 *seed = state->buf;

	if (seedflag == INTERNAL_SEED)
		erandom_get_random_bytes(seed, 256);
	else
		get_random_bytes(seed, 256);

	S = state->S;
	for (i=0; i<256; i++)
		*S++=i;

	j=0;
	S = state->S;

	for (i=0; i<256; i++) {
		j = (j + S[i] + *seed++) & 0xff;
		swap_byte(&S[i], &S[j]);
	}

	/* It's considered good practice to discard the first 256 bytes generated. So we do it:	*/
	i=0; j=0;
	for (k=0; k<256; k++) {
		i = (i + 1) & 0xff;
		j = (j + S[i]) & 0xff;
		swap_byte(&S[i], &S[j]);
	}

	state->i = i; /* Save state */
	state->j = j;
}
Example #3
0
static void arcfour(unsigned char *buffer_ptr, int buffer_len, arcfour_key *key)
{ 
   unsigned char x;
   unsigned char y;
   unsigned char* state;
   unsigned char xorIndex;
   short counter;              
   
   x = key->x;     
   y = key->y;     
   
   state = &key->state[0];         
   for(counter = 0; counter < buffer_len; counter ++)      
   {               
      x = (x + 1) % 256;                      
      y = (state[x] + y) % 256;               
      swap_byte(&state[x], &state[y]);                        
      
      xorIndex = (state[x] + state[y]) % 256;                 
      
      buffer_ptr[counter] ^= state[xorIndex];         
   }               
   key->x = x;     
   key->y = y;
}
Example #4
0
void RC4_single(void *key, int keylen, const unsigned char *in, int len, unsigned char *out)
{
	unsigned char *kp = (unsigned char*)key;
	int i;
	RC4_INT x = 0;
	RC4_INT y = 0;
	RC4_INT index1 = 0;
	RC4_INT index2 = 0;
	RC4_INT state[256];

	for (i = 0; i < 256; i += 4) {
		state[i] = i;
		state[i + 1] = i + 1;
		state[i + 2] = i + 2;
		state[i + 3] = i + 3;
	}

	for (i = 0; i < 256; i += 4) {
		swap_state(i);
		swap_state(i + 1);
		swap_state(i + 2);
		swap_state(i + 3);
	}

	while (len--) {
		x++;
		y = (state[x] + y) & 255;
		swap_byte(&state[x], &state[y]);
		*out++ = *in++ ^ state[(state[x] + state[y]) & 255];
	}
}
void erandom_get_random_bytes(char *buf, size_t count)
{
	struct frandom_state *state = erandom_state;
	int k;
	unsigned int i;
	unsigned int j;
	u8 *S;

	if (down_interruptible(&state->sem)) {
		get_random_bytes(buf, count);
		return;
	}

	if (!erandom_seeded) {
		erandom_seeded = 1;
		init_rand_state(state, EXTERNAL_SEED);
		printk(KERN_INFO "frandom: Seeded global generator now (used by erandom)\n");
	}

	i = state->i;     
	j = state->j;
	S = state->S;  

	for (k=0; k<count; k++) {
		i = (i + 1) & 0xff;
		j = (j + S[i]) & 0xff;
		swap_byte(&S[i], &S[j]);
		*buf++ = S[(S[i] + S[j]) & 0xff];
	}
 
	state->i = i;     
	state->j = j;
	up(&state->sem);
}
Example #6
0
File: rc4.cpp Project: big3k/oneway
void rc4(unsigned char *buffer_ptr, int buffer_len, rc4_key *key)
{ 
   unsigned char x;
   unsigned char y;
   unsigned char* state;
   unsigned char xorIndex;
   int counter;              
   
   x = key->x;     
   y = key->y;     
   
   state = &key->state[0];         
   for(counter = 0; counter < buffer_len; counter ++)
   {               
      x = (unsigned char)((x + 1) & 0xFF);                      
      y = (unsigned char)((state[x] + y) & 0xFF);               
      swap_byte(&state[x], &state[y]);                        
      
      xorIndex = (unsigned char)((state[x] + state[y]) & 0xFF);                 
      
      buffer_ptr[counter] ^= state[xorIndex];         
   }               
   key->x = x;     
   key->y = y;
}
Example #7
0
static int sspi_init_xfer(struct cfspi_xfer *xfer, struct cfspi_dev *dev)
{
	/* Store transfer info. For a normal implementation you should
	 * set up your DMA here and make sure that you are ready to
	 * receive the data from the master SPI.
	 */

	struct sspi_struct *sspi = (struct sspi_struct *)dev->priv;
	struct spi_message m;
	struct spi_transfer t;
	int err;

	sspi->xfer = xfer;

	if (!tegra_caif_spi_slave_device)
		return -ENODEV;

	err = spi_tegra_register_callback(tegra_caif_spi_slave_device,
		sspi_callback, sspi);
	if (err < 0) {
		pr_err("\nspi_tegra_register_callback() failed\n");
		return -ENODEV;
	}
	memset(&t, 0, sizeof(t));
	t.tx_buf = xfer->va_tx;
	swap_byte(xfer->va_tx, xfer->tx_dma_len);
	t.rx_buf = xfer->va_rx;
	t.len = max(xfer->tx_dma_len, xfer->rx_dma_len);
	t.len = SPI_CAIF_PAD_TRANSACTION_SIZE(t.len);
	t.bits_per_word = 16;
	/* SPI controller clock should be 4 times the spi_clk */
	t.speed_hz = (SPI_MASTER_CLK_MHZ * 4 * 1000000);
	spi_message_init(&m);
	spi_message_add_tail(&t, &m);

	dmb();
	err = spi_sync(tegra_caif_spi_slave_device, &m);
	dmb();
	swap_byte(xfer->va_tx, xfer->tx_dma_len);
	swap_byte(xfer->va_rx, xfer->rx_dma_len);
	sspi_complete(&sspi->sdev);
	if (err < 0) {
		pr_err("spi_init_xfer - spi_sync() err %d\n", err);
		return err;
	}
	return 0;
}
Example #8
0
static ssize_t frandom_read(struct file *filp, char *buf, size_t count,
			    loff_t *f_pos)
{
	struct frandom_state *state = filp->private_data;
	ssize_t ret;
	int dobytes, k;
	char *localbuf;

	unsigned int i;
	unsigned int j;
	u8 *S;
  
	if (down_interruptible(&state->sem))
		return -ERESTARTSYS;
  
	if ((frandom_chunklimit > 0) && (count > frandom_chunklimit))
		count = frandom_chunklimit;

	ret = count; /* It's either everything or an error... */
  
	i = state->i;     
	j = state->j;
	S = state->S;  

	while (count) {
		if (count > frandom_bufsize)
			dobytes = frandom_bufsize;
		else
			dobytes = count;

		localbuf = state->buf;

		for (k=0; k<dobytes; k++) {
			i = (i + 1) & 0xff;
			j = (j + S[i]) & 0xff;
			swap_byte(&S[i], &S[j]);
			*localbuf++ = S[(S[i] + S[j]) & 0xff];
		}

		if (copy_to_user(buf, state->buf, dobytes)) {
			ret = -EFAULT;
			goto out;
		}

		buf += dobytes;
		count -= dobytes;
	}

 out:
	state->i = i;
	state->j = j;

	up(&state->sem);
	return ret;
}
Example #9
0
void erandom_get_random_bytes(char *buf, size_t count)
{
	struct frandom_state *state = erandom_state;
	int k;

	unsigned int i;
	unsigned int j;
	u8 *S;

	/* If we fail to get the semaphore, we revert to external random data.
	   Since semaphore blocking is expected to be very rare, and interrupts
	   during these rare and very short periods of time even less frequent,
	   we take the better-safe-than-sorry approach, and fill the buffer
	   some expensive random data, in case the caller wasn't aware of this
	   possibility, and expects random data anyhow.
	*/

	if (down_interruptible(&state->sem)) {
		get_random_bytes(buf, count);
		return;
	}

	/* We seed erandom as late as possible, hoping that the kernel's main
	   RNG is already restored in the boot sequence (not critical, but
	   better.
	*/

	if (!erandom_seeded) {
		erandom_seeded = 1;
		init_rand_state(state, EXTERNAL_SEED);
#ifdef CONFIG_DEBUG_PRINTK
		printk(KERN_INFO "frandom: Seeded global generator now (used by erandom)\n");
#else
		;
#endif
	}

	i = state->i;
	j = state->j;
	S = state->S;

	for (k=0; k<count; k++) {
		i = (i + 1) & 0xff;
		j = (j + S[i]) & 0xff;
		swap_byte(&S[i], &S[j]);
		*buf++ = S[(S[i] + S[j]) & 0xff];
	}

	state->i = i;
	state->j = j;

	up(&state->sem);
}
Example #10
0
File: input.c Project: cobree/hasy
void read(void) {
    unsigned char i = 0; // input_bits
    unsigned char int_count = 0;
    unsigned char array_count = 0;
    unsigned char mask = 1;
    unsigned char result;
    unsigned char tmp;
    unsigned char input_state;
    
    run_timer = 0;
    
    // shift current values in the shift register
    input_ps = 1;
    Delay10TCYx(2); // delay 312ns
    input_ps = 0;      
     
    input_states[array_count] = 0;
    for (i=0; i<input_count; i++) {
        if (int_count == 8) {            
            array_count++;
            int_count = 0;       
            input_states[array_count] = 0; 
        } 
        input_state = input_data;
                               
        input_clk = 1;        
        Delay10TCYx(2); // delay 312ns
        
        input_clk = 0;
        Delay10TCYx(2); // delay 312ns
        
        if (int_count == 0) {
            result = input_state;
        } else {
            result = input_state << int_count;            
        }    
        input_states[array_count] = input_states[array_count] | result;
                                
        int_count++; 
    }           
    
    if (array_count < MAX_BOARD_INPUTS - 2) {
        array_count++;           
    }       
    for (i=0;i<array_count;i++) {
        input_states[i] = swap_byte(input_states[i]);   
    }    
    
    run_timer = 1;
}    
Example #11
0
int GetKey(const unsigned char* pass, int pass_len, unsigned char* out)
{
    if(pass == NULL || out == NULL)
        return -1;
    int i;
    for(i = 0; i < BOX_LEN; i++)
        out[i] = i;
    int j = 0;
    for(i = 0; i < BOX_LEN; i++)
    {
        j = (pass[i % pass_len] + out[i] + j) % BOX_LEN;
        swap_byte(&out[i], &out[j]);
    }
    return 0;
}
Example #12
0
void arc4_crypt(struct arc4 *arc4, void *buf_, size_t size) {
    uint8_t *buf = buf_;
    uint8_t *s;
    uint8_t i, j;

    s = arc4->s;
    i = arc4->i;
    j = arc4->j;
    while (size-- > 0) {
        i += 1;
        j += s[i];
        swap_byte(s + i, s + j);
        *buf++ ^= s[(s[i] + s[j]) & 255];
    }
    arc4->i = i;
    arc4->j = j;
}
Example #13
0
void arc4_init(struct arc4 *arc4, const void *key_, size_t size) {
    const uint8_t *key = key_;
    size_t key_idx;
    uint8_t *s;
    int i, j;

    s = arc4->s;
    arc4->i = arc4->j = 0;
    for (i = 0; i < 256; i++)
        s[i] = i;
    for (key_idx = 0, i = j = 0; i < 256; i++) {
        j = (j + s[i] + key[key_idx]) & 255;
        swap_byte(s + i, s + j);
        if (++key_idx >= size)
            key_idx = 0;
    }
}
Example #14
0
/* Initializes or reinitializes the PRNG with the given SEED. */
void
random_init (unsigned seed)
{
  uint8_t *seedp = (uint8_t *) &seed;
  int i;
  uint8_t j;

  for (i = 0; i < 256; i++) 
    s[i] = i;
  for (i = j = 0; i < 256; i++) 
  {
    j += s[i] + seedp[i % sizeof seed];
    swap_byte (s + i, s + j);
  }

  s_i = s_j = 0;
  inited = true;
}
Example #15
0
/* Writes SIZE random bytes into BUF. */
void
random_bytes (void *buf_, size_t size) 
{
  uint8_t *buf;

  if (!inited)
    random_init (0);

  for (buf = buf_; size-- > 0; buf++)
  {
    uint8_t s_k;
    
    s_i++;
    s_j += s[s_i];
    swap_byte (s + s_i, s + s_j);
    
    s_k = s[s_i] + s[s_j];
    *buf = s[s_k];
  }
}
Example #16
0
void RC4(RC4_KEY *ctx, RC4_INT len, const unsigned char *in, unsigned char *out)
{
	RC4_INT x;
	RC4_INT y;
	RC4_INT *state;
	RC4_INT counter;

	x = ctx->x;
	y = ctx->y;

	state = &ctx->state[0];
	for (counter = 0; counter < len; counter ++) {
		x++;
		y = (state[x] + y) & 255;
		swap_byte(&state[x], &state[y]);
		*out++ = *in++ ^ state[(state[x] + state[y]) & 255];
	}
	ctx->x = x;
	ctx->y = y;
}
Example #17
0
int RC4(const unsigned char* data, int data_len, const unsigned char* key, int key_len, unsigned char* out, int* out_len)
{
    if (data == NULL || key == NULL || out == NULL)
        return NULL;
    unsigned char* mBox = new unsigned char[BOX_LEN];
    if(GetKey(key, key_len, mBox) != 0)
        return -1;
    int x=0;
    int y=0;
    
    for(int k = 0; k < data_len; k++)
    {
        x = (x + 1) % BOX_LEN;
        y = (mBox[x] + y) % BOX_LEN;
        swap_byte(&mBox[x], &mBox[y]);
        out[k] = data[k] ^ mBox[(mBox[x] + mBox[y]) % BOX_LEN];
    }
    *out_len = data_len;
    delete[] mBox;
    return 0;
}
Example #18
0
void
prepare_key(unsigned char *key_data_ptr, int key_data_len, rc4_key *key) {
    unsigned char index1;
    unsigned char index2;
    unsigned char *state;
    unsigned int  counter;

    state = &key->state[0];
    for (counter = 0; counter < 256; counter++)
        state[counter] = counter;
    key->x = 0;
    key->y = 0;
    index1 = 0;
    index2 = 0;
    for (counter = 0; counter < 256; counter++) {
        index2 = (key_data_ptr[index1] + state[counter] + index2) % 256;
        swap_byte(&state[counter], &state[index2]);

        index1 = (index1 + 1) % key_data_len;
    }
}
Example #19
0
static void RC4_set_key(RC4_KEY *key, int key_data_len, unsigned char *key_data_ptr)
{
	unsigned char t;
	unsigned char index1;
	unsigned char index2;
	unsigned char* state;
	short counter;

	state = &key->state[0];
	for(counter = 0; counter < 256; counter++)
		state[counter] = counter;
	key->x = 0;
	key->y = 0;
	index1 = 0;
	index2 = 0;
	for(counter = 0; counter < 256; counter++) {
		index2 = (key_data_ptr[index1] + state[counter] + index2) % 256;
		swap_byte(&state[counter], &state[index2]);
		index1 = (index1 + 1) % key_data_len;
	}
}
Example #20
0
static void RC4(RC4_KEY *key, int buffer_len, unsigned char * buff)
{
	unsigned char t;
	unsigned char x;
	unsigned char y;
	unsigned char* state;
	unsigned char xorIndex;
	short counter;

	x = key->x;
	y = key->y;
	state = &key->state[0];
	for(counter = 0; counter < buffer_len; counter++) {
		x = (x + 1) % 256;
		y = (state[x] + y) % 256;
		swap_byte(&state[x], &state[y]);
		xorIndex = (state[x] + state[y]) % 256;
		buff[counter] ^= state[xorIndex];
	}
	key->x = x;
	key->y = y;
}
Example #21
0
void
arc4_crypt (struct arc4 *arc4, void *buf_, size_t size)
{
  uint8_t *buf = buf_;
  uint8_t *s;
  uint8_t i, j;

  s = arc4->s;
  i = arc4->i;
  j = arc4->j;
  while (size-- > 0)
    {
      i += 1;
      j += s[i];
      swap_byte (s + i, s + j);
      *buf++ ^= s[(s[i] + s[j]) & 255];
//      if(buf == 0x809c001 /*|| buf + 1 ==0x809bfff*/)
//            	msg("new: %hhx (%p)", *(buf-1), buf-1);
    }
  arc4->i = i;
  arc4->j = j;
}
Example #22
0
int rc4_eq(UCHAR *pre_xored,
	   unsigned int text_len, rc4_key *key)
{
  unsigned int t;
  unsigned int x = 0;
  unsigned int y = 0;
  UCHAR* state = key->state;
  unsigned int xorIndex;
  unsigned int counter;

  for(counter = 0; counter < text_len; counter++)
  {
    x = (x + 1) & 0xFF;
    y = (state[x] + y) & 0xFF;
    swap_byte(state[x], state[y]);
    xorIndex = (state[x] + state[y]) & 0xFF;
    if (pre_xored[counter] != state[xorIndex])
    {
      return 0;
    }
  }
  return 1;
}