diff options
author | lloyd <[email protected]> | 2009-03-31 03:41:11 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2009-03-31 03:41:11 +0000 |
commit | 56110fc1db3e24e76e3530ad80b55a27b6eef67c (patch) | |
tree | 34a8629099f66ece34db43e862e0480c04fc1e18 /src | |
parent | 58bcc7e7c08badf1a11735129704b1d38fa8abad (diff) |
Add support for multiple Sbox parameter sets in the GOST 28147-89 implementation.
In addition to the GOST 34.11 test parameters (used in Crypto++ among other
things), the GOST 34.11 CryptoPro parameters (used in implementations of the
GOST hash function) are now supported.
Diffstat (limited to 'src')
-rw-r--r-- | src/block/gost_28147/gost_28147.cpp | 55 | ||||
-rw-r--r-- | src/block/gost_28147/gost_28147.h | 33 |
2 files changed, 71 insertions, 17 deletions
diff --git a/src/block/gost_28147/gost_28147.cpp b/src/block/gost_28147/gost_28147.cpp index d433924fc..3f975f4dc 100644 --- a/src/block/gost_28147/gost_28147.cpp +++ b/src/block/gost_28147/gost_28147.cpp @@ -10,28 +10,53 @@ namespace Botan { +byte GOST_28147_89_Params::sbox_entry(u32bit row, u32bit col) const + { + byte x = sboxes[4 * col + (row / 2)]; + + return (row % 2 == 0) ? (x >> 4) : (x & 0x0F); + } + +GOST_28147_89_Params::GOST_28147_89_Params(const std::string& name) + { + if(name == "R3411_94_TestParam") + sboxes = GOST_R_3411_TEST_PARAMS; + else if(name == "R3411_CryptoPro") + sboxes = GOST_R_3411_CRYPTOPRO_PARAMS; + else + throw Invalid_Argument("GOST_28147_89_Params: Unknown " + name); + } + +// Encoded in the packed fromat from RFC 4357 + +// GostR3411_94_TestParamSet (OID 1.2.643.2.2.31.0) +const byte GOST_28147_89_Params::GOST_R_3411_TEST_PARAMS[64] = { + 0x4E, 0x57, 0x64, 0xD1, 0xAB, 0x8D, 0xCB, 0xBF, 0x94, 0x1A, 0x7A, + 0x4D, 0x2C, 0xD1, 0x10, 0x10, 0xD6, 0xA0, 0x57, 0x35, 0x8D, 0x38, + 0xF2, 0xF7, 0x0F, 0x49, 0xD1, 0x5A, 0xEA, 0x2F, 0x8D, 0x94, 0x62, + 0xEE, 0x43, 0x09, 0xB3, 0xF4, 0xA6, 0xA2, 0x18, 0xC6, 0x98, 0xE3, + 0xC1, 0x7C, 0xE5, 0x7E, 0x70, 0x6B, 0x09, 0x66, 0xF7, 0x02, 0x3C, + 0x8B, 0x55, 0x95, 0xBF, 0x28, 0x39, 0xB3, 0x2E, 0xCC }; + +// GostR3411-94-CryptoProParamSet (OID 1.2.643.2.2.31.1) +const byte GOST_28147_89_Params::GOST_R_3411_CRYPTOPRO_PARAMS[64] = { + 0xA5, 0x74, 0x77, 0xD1, 0x4F, 0xFA, 0x66, 0xE3, 0x54, 0xC7, 0x42, + 0x4A, 0x60, 0xEC, 0xB4, 0x19, 0x82, 0x90, 0x9D, 0x75, 0x1D, 0x4F, + 0xC9, 0x0B, 0x3B, 0x12, 0x2F, 0x54, 0x79, 0x08, 0xA0, 0xAF, 0xD1, + 0x3E, 0x1A, 0x38, 0xC7, 0xB1, 0x81, 0xC6, 0xE6, 0x56, 0x05, 0x87, + 0x03, 0x25, 0xEB, 0xFE, 0x9C, 0x6D, 0xF8, 0x6D, 0x2E, 0xAB, 0xDE, + 0x20, 0xBA, 0x89, 0x3C, 0x92, 0xF8, 0xD3, 0x53, 0xBC }; + /* * GOST Constructor */ -GOST_28147_89::GOST_28147_89() : BlockCipher(8, 32) +GOST_28147_89::GOST_28147_89(const GOST_28147_89_Params& param) : BlockCipher(8, 32) { - - // GostR3411_94_TestParamSet (OID 1.2.643.2.2.31.0) - const byte sbox[8][16] = { - {0x4,0xA,0x9,0x2,0xD,0x8,0x0,0xE,0x6,0xB,0x1,0xC,0x7,0xF,0x5,0x3}, - {0xE,0xB,0x4,0xC,0x6,0xD,0xF,0xA,0x2,0x3,0x8,0x1,0x0,0x7,0x5,0x9}, - {0x5,0x8,0x1,0xD,0xA,0x3,0x4,0x2,0xE,0xF,0xC,0x7,0x6,0x0,0x9,0xB}, - {0x7,0xD,0xA,0x1,0x0,0x8,0x9,0xF,0xE,0x4,0x6,0xC,0xB,0x2,0x5,0x3}, - {0x6,0xC,0x7,0x1,0x5,0xF,0xD,0x8,0x4,0xA,0x9,0xE,0x0,0x3,0xB,0x2}, - {0x4,0xB,0xA,0x0,0x7,0x2,0x1,0xD,0x3,0x6,0x8,0x5,0x9,0xC,0xF,0xE}, - {0xD,0xB,0x4,0x1,0x3,0xF,0x5,0x9,0x0,0xA,0xE,0x7,0x6,0x8,0x2,0xC}, - {0x1,0xF,0xD,0x0,0x5,0x7,0xA,0x4,0x9,0x2,0x3,0xE,0x6,0xB,0x8,0xC}, - }; - for(size_t i = 0; i != 4; ++i) for(size_t j = 0; j != 256; ++j) { - u32bit T = sbox[2*i][j%16] | (sbox[2*i+1][j/16] << 4); + u32bit T = (param.sbox_entry(2*i , j % 16)) | + (param.sbox_entry(2*i+1, j / 16) << 4); SBOX[256*i+j] = rotate_left(T, (11+8*i) % 32); } } diff --git a/src/block/gost_28147/gost_28147.h b/src/block/gost_28147/gost_28147.h index 1ff04b890..98eaba870 100644 --- a/src/block/gost_28147/gost_28147.h +++ b/src/block/gost_28147/gost_28147.h @@ -12,6 +12,32 @@ namespace Botan { +class GOST_28147_89_Params; + +/** +* The GOST 28147-89 block cipher uses a set of 4 bit Sboxes, however +* the standard does not actually define these Sboxes; they are +* considered a local configuration issue. Several different sets are +* used. +*/ +class GOST_28147_89_Params + { + public: + byte sbox_entry(u32bit row, u32bit col) const; + + /** + * Default GOST parameters are the ones given in GOST R 34.11 for + * testing purposes; these sboxes are also used by Crypto++, and, + * at least according to Wikipedia, the Central Bank of Russian Federation + */ + GOST_28147_89_Params(const std::string& name = "R3411_94_TestParam"); + private: + static const byte GOST_R_3411_TEST_PARAMS[64]; + static const byte GOST_R_3411_CRYPTOPRO_PARAMS[64]; + + const byte* sboxes; + }; + /** * GOST 28147-89 */ @@ -21,10 +47,13 @@ class BOTAN_DLL GOST_28147_89 : public BlockCipher void clear() throw() { EK.clear(); } std::string name() const { return "GOST-28147-89"; } - BlockCipher* clone() const { return new GOST_28147_89; } + BlockCipher* clone() const { return new GOST_28147_89(SBOX); } - GOST_28147_89(); + GOST_28147_89(const GOST_28147_89_Params& params); private: + GOST_28147_89(const SecureBuffer<u32bit, 1024>& other_SBOX) : + BlockCipher(8, 32), SBOX(other_SBOX) {} + void enc(const byte[], byte[]) const; void dec(const byte[], byte[]) const; void key_schedule(const byte[], u32bit); |