我正在使用Crypto ++库在CBC模式下使用AES算法解密文件。我在AES的sample code中遇到了SecByteBlock
课,其中说
使用SecByteBlock在堆栈上声明密钥,以确保敏感材料归零。
有人可以在char*
的内容被清除以及此课程优于BrightScript Debugger> Syntax Error. (compile error &h02) in $LIVECOMPILE(185)
BrightScript Debugger> Syntax Error. (compile error &h02) in $LIVECOMPILE(186)
BrightScript Debugger> Syntax Error. (compile error &h02) in $LIVECOMPILE(187)
BrightScript Debugger> Syntax Error. (compile error &h02) in $LIVECOMPILE(188)
BrightScript Debugger> Syntax Error. (compile error &h02) in $LIVECOMPILE(189)
BrightScript Debugger> Syntax Error. (compile error &h02) in $LIVECOMPILE(190)
BrightScript Debugger> Syntax Error. (compile error &h02) in $LIVECOMPILE(191)
BrightScript Debugger> Syntax Error. (compile error &h02) in $LIVECOMPILE(192)
BrightScript Debugger> Syntax Error. (compile error &h02) in $LIVECOMPILE(193)
BrightScript Debugger> Syntax Error. (compile error &h02) in $LIVECOMPILE(194)
BrightScript Debugger> Syntax Error. (compile error &h02) in $LIVECOMPILE(195)
BrightScript Debugger> Syntax Error. (compile error &h02) in $LIVECOMPILE(196)
BrightScript Debugger> Syntax Error. (compile error &h02) in $LIVECOMPILE(197)
BrightScript Debugger> Syntax Error. (compile error &h02) in $LIVECOMPILE(198)
BrightScript Debugger> Syntax Error. (compile error &h02) in $LIVECOMPILE(199)
BrightScript Debugger> Syntax Error. (compile error &h02) in $LIVECOMPILE(200)
BrightScript Debugger> Syntax Error. (compile error &h02) in $LIVECOMPILE(201)
BrightScript Debugger> Syntax Error. (compile error &h02) in $LIVECOMPILE(202)
BrightScript Debugger> Syntax Error. (compile error &h02) in $LIVECOMPILE(203)
BrightScript Debugger> Syntax Error. (compile error &h02) in $LIVECOMPILE(204)
BrightScript Debugger> Syntax Error. (compile error &h02) in $LIVECOMPILE(205)
BrightScript Debugger> Syntax Error. (compile error &h02) in $LIVECOMPILE(206)
BrightScript Debugger> Syntax Error. (compile error &h02) in $LIVECOMPILE(207)
BrightScript Debugger> Syntax Error. (compile error &h02) in $LIVECOMPILE(208)
BrightScript Debugger> Syntax Error. (compile error &h02) in $LIVECOMPILE(209)
BrightScript Debugger> Syntax Error. (compile error &h02) in $LIVECOMPILE(210)
BrightScript Debugger> Syntax Error. (compile error &h02) in $LIVECOMPILE(211)
时提供哪些优势时向我解释。
提前致谢
答案 0 :(得分:3)
[title]来自Crypto ++的SecByteBlock类的优点
使用SecByteBlock
的好处是可以使托管缓冲区归零。归零通常是合规项目。例如,即使在1级验证时,FIPS 140-2也需要它。
第二个非显而易见的好处是分配器 不 初始化POD内存 - 它返回一个原始块。这很有意义,因为你经常初始化0值,然后用数据覆盖内容。初始化没有任何意义,它节省了大量时间。
您可以SecBlock
提供0值初始化块。以下是两种方法:
SecByteBlock block1(32);
SecByteBlock block2(NULL, 32);
block1
大小为32字节,未初始化且内容为垃圾。
block2
的大小也是32字节,但它使用(ptr, size)
重载。重载将复制到ptr
指向的块中,或者如果其NULL
将写入0。
为了完整性和参考,SecByteBlock
只是SecBlock<byte>
的typedef。 SecBlock<T>
是您感兴趣的类,图书馆经常使用SecBlock<byte>
,SecBlock<word32>
,SecBlock<word64>
等。
以下是SecBlock
:SecBlock< T, A > Class Template Reference的Doxygen生成手册页。这是secblock.h的头文件(它只是一个标题实现)。
当
SecByteBlock
的内容被清除时,有人可以解释一下......
SecByteBlock
的内容在明显的对象破坏情况下被清除。也就是说,当析构函数运行时,分配给SecBlock
的内存将以0的模式清除。
有一个非常明显的情况,就是在调用resize
缩小一个时。在这种情况下,也会擦除返回操作系统的额外空间。
您可以在源代码中看到擦除。例如,来自Crypto++ 5.6.4 secblock.h:
187 //! \brief Deallocates a block of memory
188 //! \param ptr the pointer for the allocation
189 //! \param size the size of the allocation, in elements
190 //! \details Internally, SecureWipeArray() is called before deallocating the memory.
191 //! Once the memory block is wiped or zeroized, AlignedDeallocate() or
192 //! UnalignedDeallocate() is called.
193 //! \details AlignedDeallocate() is used if T_Align16 is true.
194 //! UnalignedDeallocate() used if T_Align16 is false.
195 void deallocate(void *ptr, size_type size)
196 {
197 CRYPTOPP_ASSERT((ptr && size) || !(ptr || size));
198 SecureWipeArray((pointer)ptr, size);
199
200 #if CRYPTOPP_BOOL_ALIGN16
201 if (T_Align16 && size*sizeof(T) >= 16)
202 return AlignedDeallocate(ptr);
203 #endif
204
205 UnalignedDeallocate(ptr);
206 }
这个课程提供的优势超过了char *。
嗯,SecBlock<byte>
是一个管理缓冲区的类。 char*
只是一种类型,并没有多少。您必须管理char*
指向的缓冲区。
您可以使用类似std::string
的内容,但不会检测溢出(只需要std::vector
来检查)并且您不会进行归零。
话虽如此,你可以做到这两点:
typdef SecBlock<char> SecCharBlock
typedef std::basic_string<char, std::char_traits<char>, AllocatorWithCleanup<char> > secure_string
第二个很酷。您可以这样做,因为secblock.h提供了与标准库兼容的安全分配器。 SecBlock<T>
在内部使用安全分配器,其名为AllocatiorWithCleanup<T>
。
OpenSSL在EVP Symmetric Encryption and Decryption | C++ Progams上的wiki页面使用类似的分配器在其示例中提供secure_string
类。 OpenSS:zallocator
来电OpenSSL_cleanse
。
141 //! \class AllocatorWithCleanup
142 //! \brief Allocates a block of memory with cleanup
143 //! \tparam T class or type
144 //! \tparam T_Align16 boolean that determines whether allocations should be aligned on 16-byte boundaries
145 //! \details If T_Align16 is true, then AllocatorWithCleanup calls AlignedAllocate()
146 //! for memory allocations. If T_Align16 is false, then AllocatorWithCleanup() calls
147 //! UnalignedAllocate() for memory allocations.
148 //! \details Template parameter T_Align16 is effectively controlled by cryptlib.h and mirrors
149 //! CRYPTOPP_BOOL_ALIGN16. CRYPTOPP_BOOL_ALIGN16 is often used as the template parameter.
150 template <class T, bool T_Align16 = false>
151 class AllocatorWithCleanup : public AllocatorBase<T>
152 {
153 public:
154 CRYPTOPP_INHERIT_ALLOCATOR_TYPES
155
156 //! \brief Allocates a block of memory
157 //! \param ptr the size of the allocation
158 //! \param size the size of the allocation, in elements
159 //! \returns a memory block
160 //! \throws InvalidArgument
161 //! \details allocate() first checks the size of the request. If it is non-0
162 //! and less than max_size(), then an attempt is made to fulfill the request
163 //! using either AlignedAllocate() or UnalignedAllocate().
164 //! \details AlignedAllocate() is used if T_Align16 is true.
165 //! UnalignedAllocate() used if T_Align16 is false.
166 //! \details This is the C++ *Placement New* operator. ptr is not used, and the function
167 //! CRYPTOPP_ASSERTs in Debug builds if ptr is non-NULL.
168 //! \sa CallNewHandler() for the methods used to recover from a failed
169 //! allocation attempt.
170 //! \note size is the count of elements, and not the number of bytes
171 pointer allocate(size_type size, const void *ptr = NULL)
172 {
173 CRYPTOPP_UNUSED(ptr); CRYPTOPP_ASSERT(ptr == NULL);
174 this->CheckSize(size);
175 if (size == 0)
176 return NULL;
177
178 #if CRYPTOPP_BOOL_ALIGN16
179 // TODO: should this need the test 'size*sizeof(T) >= 16'?
180 if (T_Align16 && size*sizeof(T) >= 16)
181 return (pointer)AlignedAllocate(size*sizeof(T));
182 #endif
183
184 return (pointer)UnalignedAllocate(size*sizeof(T));
185 }
186 ...
241 };