2016-09-28 2 views
1

Crypto ++ライブラリを使用して、CBCモードのAESアルゴリズムでファイルを復号化しています。私は鍵が敏感な材料がゼロ化されていることを確認するためにSecByteBlockを使用してスタック上に宣言されているCrypto ++のSecByteBlockクラスのメリット

言うAESのsample codeSecByteBlockクラスに出くわしました。

SecByteBlockの内容がクリアされ、どのような利点char*に対するこのクラスのオファーを取得しますとき、誰かが私に説明していただけます。アドバンス

答えて

2

感謝[タイトル] 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を書き込みます。完全および基準について


SecByteBlockSecBlock<byte>ためだけのtypedefです。 SecBlock<T>は、関心のあるクラスで、ライブラリが頻繁にここでSecBlock<byte>SecBlock<word32>SecBlock<word64>など

を使用したDoxygenはSecBlockのマニュアルページを生成します:SecBlock< T, A > Class Template Reference。そして、ここにはsecblock.hのヘッダーファイル(ヘッダーのみの実装)があります。 SecByteBlock


SecByteBlockの内容がクリアされますときに誰かが私に説明していただけます...

内容は、オブジェクトの破壊の明白なケースの下にクリアされます。つまり、SecBlockに割り当てられたメモリは、デストラクタが実行されるときに0のパターンでクリアされます。

明白ではないケースがあります。それは、resizeが縮小されたときです。この場合、OSに戻される余分なスペースも消去されます。

ソースコードでワイピングが確認できます。例えば、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  } 

文字の上に何の利点このクラスの提供*。

まあ、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のwikiページEVP Symmetric Encryption and Decryption | C++ Progamsは、同様のアロケータを使用して、例ではsecure_stringクラスを提供しています。 OpenSS:zallocatorOpenSSL_cleanseとなります。 Crypto++ 5.6.4 secblock.hから

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 }; 
関連する問題