X-Git-Url: http://git.silcnet.org/gitweb/?a=blobdiff_plain;f=lib%2Fsilccrypt%2Fciphers_def.h;h=ad00c9aa90246e39b518c491b81916c8a820ccf6;hb=9f20f0382b6229eca740925a73f96294f6dcedc6;hp=19716f4805f6802ec99e0b7579a9b07b088af17d;hpb=c145f2f7712489b6530438b18bd71e5d1e6ec642;p=crypto.git diff --git a/lib/silccrypt/ciphers_def.h b/lib/silccrypt/ciphers_def.h index 19716f48..ad00c9aa 100644 --- a/lib/silccrypt/ciphers_def.h +++ b/lib/silccrypt/ciphers_def.h @@ -4,7 +4,7 @@ Author: Pekka Riikonen - Copyright (C) 1999 - 2007 Pekka Riikonen + Copyright (C) 1999 - 2008 Pekka Riikonen This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -20,6 +20,9 @@ #ifndef CIPHERS_DEF_H #define CIPHERS_DEF_H + +#include "silclog.h" + /* General definitions for algorithms */ typedef unsigned char u1byte; typedef SilcUInt32 u4byte; @@ -27,8 +30,8 @@ typedef SilcUInt32 u32; typedef SilcUInt32 uint_32t; typedef SilcUInt8 uint_8t; -#define rotr(x, nr) (((x) >> ((int)(nr))) | ((x) << (32 - (int)(nr)))) -#define rotl(x, nr) (((x) << ((int)(nr))) | ((x) >> (32 - (int)(nr)))) +#define rotr(x, nr) silc_ror(x, nr) +#define rotl(x, nr) silc_rol(x, nr) #define byte(x, nr) ((x) >> (nr * 8) & 255) /* Byte key to words */ @@ -80,7 +83,7 @@ do { \ #define SILC_CBC_DEC_LSB_128_32(len, iv, block_prev, block, \ block_dec, src, dst, i, dec) \ do { \ - if (len & (16 - 1)) \ + if (!len || len & (16 - 1)) \ return FALSE; \ \ SILC_GET32_LSB(block_prev[0], &iv[0]); \ @@ -162,7 +165,7 @@ do { \ #define SILC_CBC_DEC_MSB_128_32(len, iv, block_prev, block, \ block_dec, src, dst, i, dec) \ do { \ - if (len & (16 - 1)) \ + if (!len || len & (16 - 1)) \ return FALSE; \ \ SILC_GET32_MSB(block_prev[0], &iv[0]); \ @@ -236,7 +239,7 @@ do { \ #define SILC_CBC_DEC_MSB_64_32(len, iv, block_prev, block, \ block_dec, src, dst, i, dec) \ do { \ - if (len & (8 - 1)) \ + if (!len || len & (8 - 1)) \ return FALSE; \ \ SILC_GET32_MSB(block_prev[0], &iv[0]); \ @@ -265,147 +268,103 @@ do { \ SILC_PUT32_MSB(block[1], &iv[4]); \ } while(0) +#ifndef WORDS_BIGENDIAN -/* CTR mode 128-bit block, MSB, MSB counter, the 8-bit enc_ctr argument must - be encrypted */ +/* CTR mode 128-bit block, MSB, MSB counter, the 8-bit ctr argument must + be encrypted to enc_ctr */ -#define SILC_CTR_MSB_128_8(iv, ctr, enc_ctr, pad, src, dst, enc) \ +#define SILC_CTR_MSB_128_8(ctr, enc_ctr, pad, src, dst, enc) \ do { \ - SILC_GET32_MSB(ctr[0], iv); \ - SILC_GET32_MSB(ctr[1], iv + 4); \ - SILC_GET32_MSB(ctr[2], iv + 8); \ - SILC_GET32_MSB(ctr[3], iv + 12); \ - \ - if (pad == 0) \ - pad = 16; \ - \ - while (len-- > 0) { \ + while (len > 0) { \ if (pad == 16) { \ - if (++ctr[3] == 0) \ - if (++ctr[2] == 0) \ - if (++ctr[1] == 0) \ - ++ctr[0]; \ - \ - SILC_PUT32_MSB(ctr[0], enc_ctr); \ - SILC_PUT32_MSB(ctr[1], enc_ctr + 4); \ - SILC_PUT32_MSB(ctr[2], enc_ctr + 8); \ - SILC_PUT32_MSB(ctr[3], enc_ctr + 12); \ + for (i = 15; i >= 0; i--) \ + if (++ctr[i]) \ + break; \ \ enc; \ + \ + if (len >= 16) { \ + *(SilcUInt64 *)dst = *(SilcUInt64 *)src ^ *(SilcUInt64 *)enc_ctr; \ + *(SilcUInt64 *)(dst + 8) = *(SilcUInt64 *)(src + 8) ^ \ + *(SilcUInt64 *)(enc_ctr + 8); \ + src += 16; \ + dst += 16; \ + len -= 16; \ + silc_prefetch((void *)src, 0, 0); \ + continue; \ + } \ pad = 0; \ } \ *dst++ = *src++ ^ enc_ctr[pad++]; \ + len--; \ } \ - \ - SILC_PUT32_MSB(ctr[0], iv); \ - SILC_PUT32_MSB(ctr[1], iv + 4); \ - SILC_PUT32_MSB(ctr[2], iv + 8); \ - SILC_PUT32_MSB(ctr[3], iv + 12); \ } while(0) -/* CTR mode 128-bit block, MSB, MSB counter, the 32-bit ctr argument must - be encrypted to enc_ctr */ +#else /* WORDS_BIGENDIAN */ -#define SILC_CTR_MSB_128_32(iv, ctr, enc_ctr, pad, src, dst, enc) \ +#define SILC_CTR_MSB_128_8(ctr, enc_ctr, pad, src, dst, enc) \ do { \ - SILC_GET32_MSB(ctr[0], iv); \ - SILC_GET32_MSB(ctr[1], iv + 4); \ - SILC_GET32_MSB(ctr[2], iv + 8); \ - SILC_GET32_MSB(ctr[3], iv + 12); \ - \ - if (pad == 0) \ - pad = 16; \ - \ while (len-- > 0) { \ if (pad == 16) { \ - if (++ctr[3] == 0) \ - if (++ctr[2] == 0) \ - if (++ctr[1] == 0) \ - ++ctr[0]; \ + for (i = 15; i >= 0; i--) \ + if (++ctr[i]) \ + break; \ \ enc; \ - SILC_PUT32_MSB(enc_ctr[0], iv); \ - SILC_PUT32_MSB(enc_ctr[1], iv + 4); \ - SILC_PUT32_MSB(enc_ctr[2], iv + 8); \ - SILC_PUT32_MSB(enc_ctr[3], iv + 12); \ pad = 0; \ } \ *dst++ = *src++ ^ enc_ctr[pad++]; \ } \ - \ - SILC_PUT32_MSB(ctr[0], iv); \ - SILC_PUT32_MSB(ctr[1], iv + 4); \ - SILC_PUT32_MSB(ctr[2], iv + 8); \ - SILC_PUT32_MSB(ctr[3], iv + 12); \ } while(0) -/* CTR mode 128-bit block, LSB, MSB counter, the 32-bit enc_ctr argument - must be encrypted */ +#endif /* !WORDS_BIGENDIAN */ -#define SILC_CTR_LSB_128_32(iv, ctr, enc_ctr, pad, src, dst, enc) \ +/* CTR mode 128-bit block, LSB, MSB counter, the 32-bit tmp argument + must be encrypted, enc_ctr must have the encrypted data too. */ + +#define SILC_CTR_LSB_128_32(ctr, tmp, enc_ctr, pad, src, dst, enc) \ do { \ - SILC_GET32_MSB(ctr[0], iv); \ - SILC_GET32_MSB(ctr[1], iv + 4); \ - SILC_GET32_MSB(ctr[2], iv + 8); \ - SILC_GET32_MSB(ctr[3], iv + 12); \ - \ - if (pad == 0) \ - pad = 16; \ - \ while (len-- > 0) { \ if (pad == 16) { \ - if (++ctr[3] == 0) \ - if (++ctr[2] == 0) \ - if (++ctr[1] == 0) \ - ++ctr[0]; \ - \ - enc_ctr[0] = SILC_SWAB_32(ctr[0]); \ - enc_ctr[1] = SILC_SWAB_32(ctr[1]); \ - enc_ctr[2] = SILC_SWAB_32(ctr[2]); \ - enc_ctr[3] = SILC_SWAB_32(ctr[3]); \ + for (i = 15; i >= 0; i--) \ + if (++ctr[i]) \ + break; \ \ + SILC_GET32_LSB(tmp[0], ctr); \ + SILC_GET32_LSB(tmp[1], ctr + 4); \ + SILC_GET32_LSB(tmp[2], ctr + 8); \ + SILC_GET32_LSB(tmp[3], ctr + 12); \ enc; \ - SILC_PUT32_LSB(enc_ctr[0], iv); \ - SILC_PUT32_LSB(enc_ctr[1], iv + 4); \ - SILC_PUT32_LSB(enc_ctr[2], iv + 8); \ - SILC_PUT32_LSB(enc_ctr[3], iv + 12); \ + SILC_PUT32_LSB(tmp[0], enc_ctr); \ + SILC_PUT32_LSB(tmp[1], enc_ctr + 4); \ + SILC_PUT32_LSB(tmp[2], enc_ctr + 8); \ + SILC_PUT32_LSB(tmp[3], enc_ctr + 12); \ pad = 0; \ } \ - *dst++ = *src++ ^ iv[pad++]; \ + *dst++ = *src++ ^ enc_ctr[pad++]; \ } \ - \ - SILC_PUT32_MSB(ctr[0], iv); \ - SILC_PUT32_MSB(ctr[1], iv + 4); \ - SILC_PUT32_MSB(ctr[2], iv + 8); \ - SILC_PUT32_MSB(ctr[3], iv + 12); \ } while(0) -/* CTR mode 64-bit block, MSB, MSB counter, the 32-bit ctr argument must - be encrypted to enc_ctr */ +/* CTR mode 128-bit block, LSB, MSB counter, the 32-bit tmp argument + must be encrypted, enc_ctr must have the encrypted data too. */ -#define SILC_CTR_MSB_64_32(iv, ctr, enc_ctr, pad, src, dst, enc) \ +#define SILC_CTR_MSB_64_32(ctr, tmp, enc_ctr, pad, src, dst, enc) \ do { \ - SILC_GET32_MSB(ctr[0], iv); \ - SILC_GET32_MSB(ctr[1], iv + 4); \ - \ - if (pad == 0) \ - pad = 8; \ - \ while (len-- > 0) { \ if (pad == 8) { \ - if (++ctr[1] == 0) \ - ++ctr[0]; \ + for (i = 7; i >= 0; i--) \ + if (++ctr[i]) \ + break; \ \ + SILC_GET32_MSB(tmp[0], ctr); \ + SILC_GET32_MSB(tmp[1], ctr + 4); \ enc; \ - SILC_PUT32_MSB(enc_ctr[0], iv); \ - SILC_PUT32_MSB(enc_ctr[1], iv + 4); \ + SILC_PUT32_MSB(tmp[0], enc_ctr); \ + SILC_PUT32_MSB(tmp[1], enc_ctr + 4); \ pad = 0; \ } \ - *dst++ = *src++ ^ iv[pad++]; \ + *dst++ = *src++ ^ enc_ctr[pad++]; \ } \ - \ - SILC_PUT32_MSB(ctr[0], iv); \ - SILC_PUT32_MSB(ctr[1], iv + 4); \ } while(0) /* CFB 128-bit block, LSB, the 32-bit cfb argument must be encrypted. */ @@ -438,6 +397,7 @@ do { \ #define SILC_CFB_DEC_LSB_128_32(iv, cfb, pad, src, dst, dec) \ do { \ + unsigned char temp; \ while (len-- > 0) { \ if (pad == 16) { \ SILC_GET32_LSB(cfb[0], iv); \ @@ -453,9 +413,9 @@ do { \ SILC_PUT32_LSB(cfb[3], iv + 12); \ pad = 0; \ } \ - iv[pad] = *src ^ iv[pad]; \ - *dst = iv[pad]; \ - iv[pad++] = *src; \ + temp = *src; \ + *dst = temp ^ iv[pad]; \ + iv[pad++] = temp; \ dst++; \ src++; \ } \ @@ -491,6 +451,7 @@ do { \ #define SILC_CFB_DEC_MSB_128_32(iv, cfb, pad, src, dst, dec) \ do { \ + unsigned char temp; \ while (len-- > 0) { \ if (pad == 16) { \ SILC_GET32_MSB(cfb[0], iv); \ @@ -506,9 +467,9 @@ do { \ SILC_PUT32_MSB(cfb[3], iv + 12); \ pad = 0; \ } \ - iv[pad] = *src ^ iv[pad]; \ - *dst = iv[pad]; \ - iv[pad++] = *src; \ + temp = *src; \ + *dst = temp ^ iv[pad]; \ + iv[pad++] = temp; \ dst++; \ src++; \ } \ @@ -540,6 +501,7 @@ do { \ #define SILC_CFB_DEC_MSB_64_32(iv, cfb, pad, src, dst, dec) \ do { \ + unsigned char temp; \ while (len-- > 0) { \ if (pad == 8) { \ SILC_GET32_MSB(cfb[0], iv); \ @@ -551,9 +513,9 @@ do { \ SILC_PUT32_MSB(cfb[1], iv + 4); \ pad = 0; \ } \ - iv[pad] = *src ^ iv[pad]; \ - *dst = iv[pad]; \ - iv[pad++] = *src; \ + temp = *src; \ + *dst = temp ^ iv[pad]; \ + iv[pad++] = temp; \ dst++; \ src++; \ } \ @@ -578,15 +540,16 @@ do { \ /* CFB 128-bit block, MSB, the 8-bit iv argument must be decrypted. */ #define SILC_CFB_DEC_MSB_128_8(iv, pad, src, dst, dec) \ - do { \ +do { \ + unsigned char temp; \ while (len-- > 0) { \ if (pad == 16) { \ dec; \ pad = 0; \ } \ - iv[pad] = *src ^ iv[pad]; \ - *dst = iv[pad]; \ - iv[pad++] = *src; \ + temp = *src; \ + *dst = temp ^ iv[pad]; \ + iv[pad++] = temp; \ dst++; \ src++; \ } \