X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Farctwo.c;h=27b7c1bd9d027f3858d316a22181badfa1e11819;hb=12619428c2ef7601f014af01048a28274de7a36c;hp=ccaa4b6c2885949f1e444c9f48d04de3a38fda49;hpb=a42e0dae2fe2bc4d1166d572e8b5b447de9b9873;p=gnulib.git diff --git a/lib/arctwo.c b/lib/arctwo.c index ccaa4b6c2..27b7c1bd9 100644 --- a/lib/arctwo.c +++ b/lib/arctwo.c @@ -1,5 +1,6 @@ /* arctwo.c --- The RC2 cipher as described in RFC 2268. - * Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation, Inc. + * Copyright (C) 2003, 2004, 2005, 2006, 2008, 2009, 2010 Free Software + * Foundation, Inc. * * This file is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published @@ -31,6 +32,8 @@ #include "arctwo.h" +#include "bitrotate.h" + static const uint8_t arctwo_sbox[] = { 217, 120, 249, 196, 25, 221, 181, 237, 40, 233, 253, 121, 74, 160, 216, 157, @@ -66,9 +69,6 @@ static const uint8_t arctwo_sbox[] = { 10, 166, 32, 104, 254, 127, 193, 173 }; -#define rotl16(x,n) (((x) << ((uint16_t)(n))) | ((x) >> (16 - (uint16_t)(n)))) -#define rotr16(x,n) (((x) >> ((uint16_t)(n))) | ((x) << (16 - (uint16_t)(n)))) - /* C89 compliant way to cast 'char' to 'unsigned char'. */ static inline unsigned char to_uchar (char ch) @@ -78,10 +78,10 @@ to_uchar (char ch) void arctwo_encrypt (arctwo_context *context, const char *inbuf, - char *outbuf, size_t length) + char *outbuf, size_t length) { for (; length >= ARCTWO_BLOCK_SIZE; length -= ARCTWO_BLOCK_SIZE, - inbuf += ARCTWO_BLOCK_SIZE, outbuf += ARCTWO_BLOCK_SIZE) + inbuf += ARCTWO_BLOCK_SIZE, outbuf += ARCTWO_BLOCK_SIZE) { size_t i, j; uint16_t word0 = 0, word1 = 0, word2 = 0, word3 = 0; @@ -96,29 +96,29 @@ arctwo_encrypt (arctwo_context *context, const char *inbuf, word3 = (word3 << 8) | to_uchar (inbuf[6]); for (i = 0; i < 16; i++) - { - j = i * 4; - /* For some reason I cannot combine those steps. */ - word0 += (word1 & ~word3) + (word2 & word3) + context->S[j]; - word0 = rotl16 (word0, 1); - - word1 += (word2 & ~word0) + (word3 & word0) + context->S[j + 1]; - word1 = rotl16 (word1, 2); - - word2 += (word3 & ~word1) + (word0 & word1) + context->S[j + 2]; - word2 = rotl16 (word2, 3); - - word3 += (word0 & ~word2) + (word1 & word2) + context->S[j + 3]; - word3 = rotl16 (word3, 5); - - if (i == 4 || i == 10) - { - word0 += context->S[word3 & 63]; - word1 += context->S[word0 & 63]; - word2 += context->S[word1 & 63]; - word3 += context->S[word2 & 63]; - } - } + { + j = i * 4; + /* For some reason I cannot combine those steps. */ + word0 += (word1 & ~word3) + (word2 & word3) + context->S[j]; + word0 = rotl16 (word0, 1); + + word1 += (word2 & ~word0) + (word3 & word0) + context->S[j + 1]; + word1 = rotl16 (word1, 2); + + word2 += (word3 & ~word1) + (word0 & word1) + context->S[j + 2]; + word2 = rotl16 (word2, 3); + + word3 += (word0 & ~word2) + (word1 & word2) + context->S[j + 3]; + word3 = rotl16 (word3, 5); + + if (i == 4 || i == 10) + { + word0 += context->S[word3 & 63]; + word1 += context->S[word0 & 63]; + word2 += context->S[word1 & 63]; + word3 += context->S[word2 & 63]; + } + } outbuf[0] = word0 & 255; outbuf[1] = word0 >> 8; @@ -133,10 +133,10 @@ arctwo_encrypt (arctwo_context *context, const char *inbuf, void arctwo_decrypt (arctwo_context *context, const char *inbuf, - char *outbuf, size_t length) + char *outbuf, size_t length) { for (; length >= ARCTWO_BLOCK_SIZE; length -= ARCTWO_BLOCK_SIZE, - inbuf += ARCTWO_BLOCK_SIZE, outbuf += ARCTWO_BLOCK_SIZE) + inbuf += ARCTWO_BLOCK_SIZE, outbuf += ARCTWO_BLOCK_SIZE) { size_t i, j; uint16_t word0 = 0, word1 = 0, word2 = 0, word3 = 0; @@ -151,29 +151,29 @@ arctwo_decrypt (arctwo_context *context, const char *inbuf, word3 = (word3 << 8) | to_uchar (inbuf[6]); for (i = 16; i > 0; i--) - { - j = (i - 1) * 4; + { + j = (i - 1) * 4; - word3 = rotr16 (word3, 5); - word3 -= (word0 & ~word2) + (word1 & word2) + context->S[j + 3]; + word3 = rotr16 (word3, 5); + word3 -= (word0 & ~word2) + (word1 & word2) + context->S[j + 3]; - word2 = rotr16 (word2, 3); - word2 -= (word3 & ~word1) + (word0 & word1) + context->S[j + 2]; + word2 = rotr16 (word2, 3); + word2 -= (word3 & ~word1) + (word0 & word1) + context->S[j + 2]; - word1 = rotr16 (word1, 2); - word1 -= (word2 & ~word0) + (word3 & word0) + context->S[j + 1]; + word1 = rotr16 (word1, 2); + word1 -= (word2 & ~word0) + (word3 & word0) + context->S[j + 1]; - word0 = rotr16 (word0, 1); - word0 -= (word1 & ~word3) + (word2 & word3) + context->S[j]; + word0 = rotr16 (word0, 1); + word0 -= (word1 & ~word3) + (word2 & word3) + context->S[j]; - if (i == 6 || i == 12) - { - word3 = word3 - context->S[word2 & 63]; - word2 = word2 - context->S[word1 & 63]; - word1 = word1 - context->S[word0 & 63]; - word0 = word0 - context->S[word3 & 63]; - } - } + if (i == 6 || i == 12) + { + word3 = word3 - context->S[word2 & 63]; + word2 = word2 - context->S[word1 & 63]; + word1 = word1 - context->S[word0 & 63]; + word0 = word0 - context->S[word3 & 63]; + } + } outbuf[0] = word0 & 255; outbuf[1] = word0 >> 8; @@ -188,7 +188,7 @@ arctwo_decrypt (arctwo_context *context, const char *inbuf, void arctwo_setkey_ekb (arctwo_context *context, - size_t keylen, const char *key, size_t effective_keylen) + size_t keylen, const char *key, size_t effective_keylen) { size_t i; uint8_t *S, x; @@ -217,10 +217,10 @@ arctwo_setkey_ekb (arctwo_context *context, S[i] = x; while (i--) - { - x = arctwo_sbox[x ^ S[i + len]]; - S[i] = x; - } + { + x = arctwo_sbox[x ^ S[i + len]]; + S[i] = x; + } } /* Make the expanded key, endian independent. */