/* * The QUAFFLER library * Copyright (c) 2006 Clifford Wolf * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * */ #include "quaffler.h" void qfl_ofb_init(unsigned char *sbox) { int i, j, k; for (i=0; i<256; i++) sbox[i] = i; for (i=0; i<256; i++) { j = qfl_os_unix_rand() % 256; k = sbox[i]; sbox[i] = sbox[j]; sbox[j] = k; } sbox[256] = 0; sbox[257] = 0; } void qfl_ofb_recover(unsigned char *sbox) { unsigned char map[256]; int i; for (i=0; i<256; i++) map[i] = 0; for (i=0; i<256; i++) map[sbox[i]] = 1; for (i=0; i<256; i++) if (!map[i]) { sbox[255] = i; break; } sbox[256] = 0; sbox[257] = 0; } void qfl_ofb_xor(unsigned char *sbox, unsigned char *data, int len) { int i = sbox[256]; int j = sbox[257]; unsigned char t; while (len-- > 0) { i = (i+1) & 255; j = (j+sbox[i]) & 255; t = sbox[i]; sbox[i] = sbox[j]; sbox[j] = t; *(data++) ^= sbox[(sbox[i]+sbox[j]) & 255]; } sbox[256] = i; sbox[257] = j; }