Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.

Commit d0623e8

Browse files
committed
SM4 block cipher minor fixes. Cipher value updated. Encryption algorithm
updated #3
1 parent e25f950 commit d0623e8

File tree

9 files changed

+278
-142
lines changed

9 files changed

+278
-142
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ list(APPEND ${CURRENT_PROJECT_NAME}_PUBLIC_HEADERS
2727

2828
include/nil/crypto3/block/block_cipher.hpp
2929
include/nil/crypto3/block/cipher_state.hpp
30+
include/nil/crypto3/block/cipher_value.hpp
3031

31-
include/nil/crypto3/block/detail/cipher_value.hpp
3232
include/nil/crypto3/block/detail/stream_endian.hpp
3333
include/nil/crypto3/block/detail/pack.hpp
3434
include/nil/crypto3/block/detail/digest.hpp

include/nil/crypto3/block/algorithm/encrypt.hpp

+48-17
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,45 @@ namespace nil {
4242
OutputIterator out) {
4343

4444
typedef typename BlockCipher::stream_encrypter_type EncryptionMode;
45-
typedef typename detail::itr_stream_encrypter_traits<EncryptionMode, InputIterator>::type CiperState;
45+
typedef typename block::block_accumulator<EncryptionMode> CipherAccumulator;
4646

47-
typedef detail::value_encrypter_impl<CiperState> StreamEncrypterImpl;
48-
typedef detail::itr_encrypter_impl<StreamEncrypterImpl, OutputIterator> EncrypterImpl;
47+
typedef block::detail::value_encrypter_impl<CipherAccumulator> StreamEncrypterImpl;
48+
typedef block::detail::itr_encrypter_impl<StreamEncrypterImpl, OutputIterator> EncrypterImpl;
4949

5050
return EncrypterImpl(first, last, std::move(out), CiperState(BlockCipher(key_first, key_last)));
5151
}
5252

53+
/*!
54+
* @brief
55+
*
56+
* @ingroup block_algorithms
57+
*
58+
* @tparam BlockCipher
59+
* @tparam InputIterator
60+
* @tparam OutputIterator
61+
* @tparam StreamEncrypter
62+
*
63+
* @param first
64+
* @param last
65+
* @param out
66+
*
67+
* @return
68+
*/
69+
template<typename BlockCipher,
70+
typename InputIterator,
71+
typename OutputAccumulator = typename block::block_accumulator<
72+
typename BlockCipher::stream_encoder_type>>
73+
OutputAccumulator &encrypt(InputIterator first, InputIterator last, typename OutputAccumulator::type &acc) {
74+
75+
typedef typename BlockCipher::stream_encrypter_type EncryptionMode;
76+
typedef typename block::block_accumulator<EncryptionMode> CipherAccumulator;
77+
78+
typedef block::detail::ref_encrypter_impl<CipherAccumulator> StreamEncrypterImpl;
79+
typedef block::detail::range_encrypter_impl<StreamEncrypterImpl> EncrypterImpl;
80+
81+
return EncrypterImpl(first, last, acc);
82+
}
83+
5384
/*!
5485
* @brief
5586
*
@@ -65,16 +96,16 @@ namespace nil {
6596
template<typename BlockCipher,
6697
typename InputIterator,
6798
typename KeyIterator,
68-
typename CipherState = typename detail::itr_stream_encrypter_traits<
69-
typename BlockCipher::stream_encrypter_type, InputIterator>::type>
70-
detail::range_encrypter_impl<detail::value_encrypter_impl<CipherState>> encrypt(InputIterator first,
71-
InputIterator last,
72-
KeyIterator key_first,
73-
KeyIterator key_last) {
74-
typedef detail::value_encrypter_impl<CipherState> StreamEncrypterImpl;
75-
typedef detail::range_encrypter_impl<StreamEncrypterImpl> EncrypterImpl;
76-
77-
return EncrypterImpl(first, last, CipherState(BlockCipher(key_first, key_last)));
99+
typename CipherAccumulator = typename block::block_accumulator<
100+
typename BlockCipher::stream_encoder_type>>
101+
detail::range_encrypter_impl<detail::value_encrypter_impl<CipherAccumulator>> encrypt(InputIterator first,
102+
InputIterator last,
103+
KeyIterator key_first,
104+
KeyIterator key_last) {
105+
typedef block::detail::value_encrypter_impl<CipherAccumulator> StreamEncrypterImpl;
106+
typedef block::detail::range_encrypter_impl<StreamEncrypterImpl> EncrypterImpl;
107+
108+
return EncrypterImpl(first, last, CipherAccumulator(BlockCipher(key_first, key_last)));
78109
}
79110

80111
/*!
@@ -96,8 +127,8 @@ namespace nil {
96127
typedef typename detail::range_stream_encrypter_traits<typename BlockCipher::stream_encrypter_type,
97128
SinglePassRange>::type CipherState;
98129

99-
typedef detail::value_encrypter_impl<CipherState> StreamEncrypterImpl;
100-
typedef detail::itr_encrypter_impl<StreamEncrypterImpl, OutputIterator> EncrypterImpl;
130+
typedef block::detail::value_encrypter_impl<CipherState> StreamEncrypterImpl;
131+
typedef block::detail::itr_encrypter_impl<StreamEncrypterImpl, OutputIterator> EncrypterImpl;
101132

102133
return EncrypterImpl(rng, std::move(out), CipherState(BlockCipher(key)));
103134
}
@@ -121,8 +152,8 @@ namespace nil {
121152
detail::range_encrypter_impl<detail::value_encrypter_impl<CipherState>> encrypt(const SinglePassRange &r,
122153
const KeyRange &key) {
123154

124-
typedef detail::value_encrypter_impl<CipherState> StreamEncrypterImpl;
125-
typedef detail::range_encrypter_impl<StreamEncrypterImpl> EncrypterImpl;
155+
typedef block::detail::value_encrypter_impl<CipherState> StreamEncrypterImpl;
156+
typedef block::detail::range_encrypter_impl<StreamEncrypterImpl> EncrypterImpl;
126157

127158
return EncrypterImpl(r, CipherState(BlockCipher(key)));
128159
}

include/nil/crypto3/block/aria.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#include <nil/crypto3/block/detail/aria/aria_policy.hpp>
1414

15-
#include <nil/crypto3/block/block_state_preprocessor.hpp>
15+
#include <nil/crypto3/block/detail/block_state_preprocessor.hpp>
1616
#include <nil/crypto3/block/detail/stream_endian.hpp>
1717

1818
#include <nil/crypto3/block/detail/utilities/cpuid/cpuid.hpp>

0 commit comments

Comments
 (0)