# HG changeset patch # User Simon Josefsson # Date 1220014297 -7200 # Node ID 4c522dd8fa801f5bc426ac40b5119f45587039ef # Parent 3ee08d494058f61edd220a960805937cd84ecd94 Add bitrotate module. diff --git a/ChangeLog b/ChangeLog --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,20 @@ +2008-08-29 Simon Josefsson + + * MODULES.html.sh (Misc): Add bitrotate. + + * modules/bitrotate: New file. + + * lib/bitrotate.h: New file. + + * modules/bitrotate-tests: New file. + + * tests/test-bitrotate.c: New file. + + * modules/crypto/gc-arctwo, modules/crypto/arctwo: Add dependency + on the bitrotate module. + + * lib/arctwo.c: Use new bitrotate module. + 2008-08-29 Jim Meyering bootstrap: merge changes from coreutils diff --git a/MODULES.html.sh b/MODULES.html.sh --- a/MODULES.html.sh +++ b/MODULES.html.sh @@ -2887,6 +2887,7 @@ func_begin_table func_module argp func_module argz + func_module bitrotate func_module byteswap func_module exitfail func_module error diff --git a/lib/arctwo.c b/lib/arctwo.c --- a/lib/arctwo.c +++ b/lib/arctwo.c @@ -1,5 +1,5 @@ /* 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 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 +31,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 +68,6 @@ 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) diff --git a/lib/bitrotate.h b/lib/bitrotate.h new file mode 100644 --- /dev/null +++ b/lib/bitrotate.h @@ -0,0 +1,60 @@ +/* bitrotate.h - Rotate bits in integers + Copyright (C) 2008 Free Software Foundation, Inc. + + 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 + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +/* Written by Simon Josefsson , 2008. */ + +#ifndef _GL_BITROTATE_H +#define _GL_BITROTATE_H + +#include + +/* Given an unsigned 32-bit argument X, return the value corresponding + to rotating the bits N steps to the left. N must be between 1 and + 31 inclusive. */ +static inline uint32_t +rotl32 (uint32_t x, int n) +{ + return ((x << n) | (x >> (32 - n))) & 0xFFFFFFFF; +} + +/* Given an unsigned 32-bit argument X, return the value corresponding + to rotating the bits N steps to the right. N must be between 1 to + 31 inclusive.*/ +static inline uint32_t +rotr32 (uint32_t x, int n) +{ + return ((x >> n) | (x << (32 - n))) & 0xFFFFFFFF; +} + +/* Given an unsigned 16-bit argument X, return the value corresponding + to rotating the bits N steps to the left. N must be between 1 to + 15 inclusive. */ +static inline uint16_t +rotl16 (uint16_t x, int n) +{ + return ((x << n) | (x >> (16 - n))) & 0xFFFFFFFF; +} + +/* Given an unsigned 16-bit argument X, return the value corresponding + to rotating the bits N steps to the right. N must be in 1 to 15 + inclusive. */ +static inline uint16_t +rotr16 (uint16_t x, int n) +{ + return ((x >> n) | (x << (16 - n))) & 0xFFFFFFFF; +} + +#endif /* _GL_BITROTATE_H */ diff --git a/modules/bitrotate b/modules/bitrotate new file mode 100644 --- /dev/null +++ b/modules/bitrotate @@ -0,0 +1,21 @@ +Description: +Rotate bits in 16 and 32 bit integers. + +Files: +lib/bitrotate.h + +Depends-on: + +configure.ac: + +Makefile.am: +lib_SOURCES += bitrotate.h + +Include: +"bitrotate.h" + +License: +LGPLv2+ + +Maintainer: +Simon Josefsson diff --git a/modules/bitrotate-tests b/modules/bitrotate-tests new file mode 100644 --- /dev/null +++ b/modules/bitrotate-tests @@ -0,0 +1,10 @@ +Files: +tests/test-bitrotate.c + +Depends-on: + +configure.ac: + +Makefile.am: +TESTS += test-bitrotate +check_PROGRAMS += test-bitrotate diff --git a/modules/crypto/arctwo b/modules/crypto/arctwo --- a/modules/crypto/arctwo +++ b/modules/crypto/arctwo @@ -8,6 +8,7 @@ Depends-on: stdint +bitrotate configure.ac: gl_ARCTWO diff --git a/modules/crypto/gc-arctwo b/modules/crypto/gc-arctwo --- a/modules/crypto/gc-arctwo +++ b/modules/crypto/gc-arctwo @@ -10,6 +10,7 @@ Depends-on: stdint crypto/gc +bitrotate configure.ac: gl_GC_ARCTWO diff --git a/tests/test-bitrotate.c b/tests/test-bitrotate.c new file mode 100644 --- /dev/null +++ b/tests/test-bitrotate.c @@ -0,0 +1,91 @@ +/* Test of substitute. + Copyright (C) 2007-2008 Free Software Foundation, Inc. + + 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 + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +/* Written by Simon Josefsson , 2008. */ + +#include + +#include "bitrotate.h" + +#include +#include + +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion failed\n", \ + __FILE__, __LINE__); \ + fflush (stderr); \ + abort (); \ + } \ + } \ + while (0) + +int +main (void) +{ + ASSERT (rotl16 (43981, 1) == 22427); + ASSERT (rotl16 (43981, 2) == 44854); + ASSERT (rotl16 (43981, 3) == 24173); + ASSERT (rotl16 (43981, 4) == 48346); + ASSERT (rotl16 (43981, 5) == 31157); + ASSERT (rotl16 (43981, 6) == 62314); + ASSERT (rotl16 (43981, 7) == 59093); + ASSERT (rotl16 (43981, 8) == 52651); + ASSERT (rotl16 (43981, 9) == 39767); + ASSERT (rotl16 (43981, 10) == 13999); + ASSERT (rotl16 (43981, 11) == 27998); + ASSERT (rotl16 (43981, 12) == 55996); + ASSERT (rotl16 (43981, 13) == 46457); + ASSERT (rotl16 (43981, 14) == 27379); + ASSERT (rotl16 (43981, 15) == 54758); + + ASSERT (rotl32 (2309737967U, 1) == 324508639U); + ASSERT (rotl32 (2309737967U, 2) == 649017278U); + ASSERT (rotl32 (2309737967U, 3) == 1298034556U); + ASSERT (rotl32 (2309737967U, 4) == 2596069112U); + ASSERT (rotl32 (2309737967U, 5) == 897170929U); + ASSERT (rotl32 (2309737967U, 6) == 1794341858U); + ASSERT (rotl32 (2309737967U, 7) == 3588683716U); + ASSERT (rotl32 (2309737967U, 8) == 2882400137U); + ASSERT (rotl32 (2309737967U, 9) == 1469832979U); + ASSERT (rotl32 (2309737967U, 10) == 2939665958U); + ASSERT (rotl32 (2309737967U, 11) == 1584364621U); + ASSERT (rotl32 (2309737967U, 12) == 3168729242U); + ASSERT (rotl32 (2309737967U, 13) == 2042491189U); + ASSERT (rotl32 (2309737967U, 14) == 4084982378U); + ASSERT (rotl32 (2309737967U, 15) == 3874997461U); + ASSERT (rotl32 (2309737967U, 16) == 3455027627U); + ASSERT (rotl32 (2309737967U, 17) == 2615087959U); + ASSERT (rotl32 (2309737967U, 18) == 935208623U); + ASSERT (rotl32 (2309737967U, 19) == 1870417246U); + ASSERT (rotl32 (2309737967U, 20) == 3740834492U); + ASSERT (rotl32 (2309737967U, 21) == 3186701689U); + ASSERT (rotl32 (2309737967U, 22) == 2078436083U); + ASSERT (rotl32 (2309737967U, 23) == 4156872166U); + ASSERT (rotl32 (2309737967U, 24) == 4018777037U); + ASSERT (rotl32 (2309737967U, 25) == 3742586779U); + ASSERT (rotl32 (2309737967U, 26) == 3190206263U); + ASSERT (rotl32 (2309737967U, 27) == 2085445231U); + ASSERT (rotl32 (2309737967U, 28) == 4170890462U); + ASSERT (rotl32 (2309737967U, 29) == 4046813629U); + ASSERT (rotl32 (2309737967U, 30) == 3798659963U); + ASSERT (rotl32 (2309737967U, 31) == 3302352631U); + + return 0; +}