Line | Count | Source |
1 | | // safer.h - originally written and placed in the public domain by Wei Dai |
2 | | |
3 | | /// \file safer.h |
4 | | /// \brief Classes for the SAFER and SAFER-K block ciphers |
5 | | |
6 | | #ifndef CRYPTOPP_SAFER_H |
7 | | #define CRYPTOPP_SAFER_H |
8 | | |
9 | | #include "seckey.h" |
10 | | #include "secblock.h" |
11 | | |
12 | | NAMESPACE_BEGIN(CryptoPP) |
13 | | |
14 | | /// \brief SAFER block cipher |
15 | | class SAFER |
16 | | { |
17 | | public: |
18 | | /// \brief SAFER block cipher default operation |
19 | | class CRYPTOPP_NO_VTABLE Base : public BlockCipher |
20 | | { |
21 | | public: |
22 | 4 | unsigned int OptimalDataAlignment() const {return 1;} |
23 | | void UncheckedSetKey(const byte *userkey, unsigned int length, const NameValuePairs ¶ms); |
24 | | |
25 | | protected: |
26 | | virtual bool Strengthened() const =0; |
27 | | |
28 | | SecByteBlock keySchedule; |
29 | | static const byte exp_tab[256]; |
30 | | static const byte log_tab[256]; |
31 | | }; |
32 | | |
33 | | /// \brief SAFER block cipher encryption operation |
34 | | class CRYPTOPP_NO_VTABLE Enc : public Base |
35 | | { |
36 | | public: |
37 | | void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const; |
38 | | }; |
39 | | |
40 | | /// \brief SAFER block cipher decryption operation |
41 | | class CRYPTOPP_NO_VTABLE Dec : public Base |
42 | | { |
43 | | public: |
44 | | void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const; |
45 | | }; |
46 | | }; |
47 | | |
48 | | /// \brief SAFER block cipher default implementation |
49 | | /// \tparam BASE SAFER::Enc or SAFER::Dec derived base class |
50 | | /// \tparam INFO SAFER_Info derived class |
51 | | /// \tparam STR flag indicating a strengthened implementation |
52 | | /// \details SAFER-K is not strengthened; while SAFER-SK is strengthened. |
53 | | template <class BASE, class INFO, bool STR> |
54 | | class CRYPTOPP_NO_VTABLE SAFER_Impl : public BlockCipherImpl<INFO, BASE> |
55 | | { |
56 | | protected: |
57 | 12 | bool Strengthened() const {return STR;} CryptoPP::SAFER_Impl<CryptoPP::SAFER::Enc, CryptoPP::SAFER_K_Info, false>::Strengthened() const Line | Count | Source | 57 | 6 | bool Strengthened() const {return STR;} |
CryptoPP::SAFER_Impl<CryptoPP::SAFER::Enc, CryptoPP::SAFER_SK_Info, true>::Strengthened() const Line | Count | Source | 57 | 3 | bool Strengthened() const {return STR;} |
Unexecuted instantiation: CryptoPP::SAFER_Impl<CryptoPP::SAFER::Dec, CryptoPP::SAFER_K_Info, false>::Strengthened() const CryptoPP::SAFER_Impl<CryptoPP::SAFER::Dec, CryptoPP::SAFER_SK_Info, true>::Strengthened() const Line | Count | Source | 57 | 3 | bool Strengthened() const {return STR;} |
|
58 | | }; |
59 | | |
60 | | /// \brief SAFER-K block cipher information |
61 | | struct SAFER_K_Info : public FixedBlockSize<8>, public VariableKeyLength<16, 8, 16, 8>, public VariableRounds<10, 1, 13> |
62 | | { |
63 | 18 | CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "SAFER-K";} |
64 | | }; |
65 | | |
66 | | /// \brief SAFER-K block cipher |
67 | | /// \sa <a href="http://www.cryptopp.com/wiki/SAFER-K">SAFER-K</a> |
68 | | class SAFER_K : public SAFER_K_Info, public SAFER, public BlockCipherDocumentation |
69 | | { |
70 | | public: |
71 | | typedef BlockCipherFinal<ENCRYPTION, SAFER_Impl<Enc, SAFER_K_Info, false> > Encryption; |
72 | | typedef BlockCipherFinal<DECRYPTION, SAFER_Impl<Dec, SAFER_K_Info, false> > Decryption; |
73 | | }; |
74 | | |
75 | | /// \brief SAFER-SK block cipher information |
76 | | struct SAFER_SK_Info : public FixedBlockSize<8>, public VariableKeyLength<16, 8, 16, 8>, public VariableRounds<10, 1, 13> |
77 | | { |
78 | 13 | CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "SAFER-SK";} |
79 | | }; |
80 | | |
81 | | /// \brief SAFER-SK block cipher |
82 | | /// \sa <a href="http://www.cryptopp.com/wiki/SAFER-SK">SAFER-SK</a> |
83 | | class SAFER_SK : public SAFER_SK_Info, public SAFER, public BlockCipherDocumentation |
84 | | { |
85 | | public: |
86 | | typedef BlockCipherFinal<ENCRYPTION, SAFER_Impl<Enc, SAFER_SK_Info, true> > Encryption; |
87 | | typedef BlockCipherFinal<DECRYPTION, SAFER_Impl<Dec, SAFER_SK_Info, true> > Decryption; |
88 | | }; |
89 | | |
90 | | typedef SAFER_K::Encryption SAFER_K_Encryption; |
91 | | typedef SAFER_K::Decryption SAFER_K_Decryption; |
92 | | |
93 | | typedef SAFER_SK::Encryption SAFER_SK_Encryption; |
94 | | typedef SAFER_SK::Decryption SAFER_SK_Decryption; |
95 | | |
96 | | NAMESPACE_END |
97 | | |
98 | | #endif |