root/lib/crypto/arcfour.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. arcfour_init
  2. arcfour_crypt_sbox
  3. arcfour_crypt_blob
  4. arcfour_crypt

   1 /* 
   2    Unix SMB/CIFS implementation.
   3 
   4    An implementation of the arcfour algorithm
   5 
   6    Copyright (C) Andrew Tridgell 1998
   7    
   8    This program is free software; you can redistribute it and/or modify
   9    it under the terms of the GNU General Public License as published by
  10    the Free Software Foundation; either version 3 of the License, or
  11    (at your option) any later version.
  12    
  13    This program is distributed in the hope that it will be useful,
  14    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16    GNU General Public License for more details.
  17    
  18    You should have received a copy of the GNU General Public License
  19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  20 */
  21 
  22 #include "includes.h"
  23 #include "../lib/crypto/arcfour.h"
  24 
  25 /* initialise the arcfour sbox with key */
  26 _PUBLIC_ void arcfour_init(struct arcfour_state *state, const DATA_BLOB *key) 
     /* [<][>][^][v][top][bottom][index][help] */
  27 {
  28         int ind;
  29         uint8_t j = 0;
  30         for (ind = 0; ind < sizeof(state->sbox); ind++) {
  31                 state->sbox[ind] = (uint8_t)ind;
  32         }
  33         
  34         for (ind = 0; ind < sizeof(state->sbox); ind++) {
  35                 uint8_t tc;
  36                 
  37                 j += (state->sbox[ind] + key->data[ind%key->length]);
  38                 
  39                 tc = state->sbox[ind];
  40                 state->sbox[ind] = state->sbox[j];
  41                 state->sbox[j] = tc;
  42         }
  43         state->index_i = 0;
  44         state->index_j = 0;
  45 }
  46 
  47 /* crypt the data with arcfour */
  48 _PUBLIC_ void arcfour_crypt_sbox(struct arcfour_state *state, uint8_t *data, int len) 
     /* [<][>][^][v][top][bottom][index][help] */
  49 {
  50         int ind;
  51         
  52         for (ind = 0; ind < len; ind++) {
  53                 uint8_t tc;
  54                 uint8_t t;
  55 
  56                 state->index_i++;
  57                 state->index_j += state->sbox[state->index_i];
  58 
  59                 tc = state->sbox[state->index_i];
  60                 state->sbox[state->index_i] = state->sbox[state->index_j];
  61                 state->sbox[state->index_j] = tc;
  62                 
  63                 t = state->sbox[state->index_i] + state->sbox[state->index_j];
  64                 data[ind] = data[ind] ^ state->sbox[t];
  65         }
  66 }
  67 
  68 /*
  69   arcfour encryption with a blob key
  70 */
  71 _PUBLIC_ void arcfour_crypt_blob(uint8_t *data, int len, const DATA_BLOB *key) 
     /* [<][>][^][v][top][bottom][index][help] */
  72 {
  73         struct arcfour_state state;
  74         arcfour_init(&state, key);
  75         arcfour_crypt_sbox(&state, data, len);
  76 }
  77 
  78 /*
  79   a variant that assumes a 16 byte key. This should be removed
  80   when the last user is gone
  81 */
  82 _PUBLIC_ void arcfour_crypt(uint8_t *data, const uint8_t keystr[16], int len)
     /* [<][>][^][v][top][bottom][index][help] */
  83 {
  84         DATA_BLOB key = data_blob(keystr, 16);
  85         
  86         arcfour_crypt_blob(data, len, &key);
  87 
  88         data_blob_free(&key);
  89 }
  90 
  91 

/* [<][>][^][v][top][bottom][index][help] */