« 暗号化についても考えてみる (TrueCrypt開発終了?) | トップページ | キー入力とマウス »

2014年5月30日 (金)

暗号化についても考えてみる その3

Crypto++ Library 5.6.2をダウンロードしました。

http://www.cryptopp.com/cryptopp562.zip

zipファイルなので、なんでもいいんですが、unzipコマンドで展開します。引数を与えずにunzipするとカレントディレクトリにわらわらと沢山ファイルが出現するので、びっくりします。-dオプションで展開するディレクトリを指定します。

$ unzip cryptopp562.zip -d cryptopp562

cryptopp562の下にファイルが出来上がります。テキストらしきファイルは

$ ls *.txt
License.txt Readme.txt

2つですね。Readme.txtを読んでみます。

まず、いろいろな特徴について書かれていてます。暗号化アルゴリズムとか。

なにやら、「どんな目的にもただで使ってもいいよ。でも、License.txtは読んでね」らしいです。

You are welcome to use it for any purpose without paying me, but see License.txt for the fine print.

サポートするコンパイラ及び環境、それぞれに関する注意みたいなこと、最後にバージョンアップ内容の履歴です。

Lincense.txtには、開発に貢献した人とそのソースのリスト、最後にこのライブラリのライセンスについて記述されています。

まず、ライブラリを同梱もしくは静的にリンクすることに問題はなさそうです。では、MinGW-4.8.2用に作りましょう。

VC用のプロジェクトファイルとかTurboC++用のプロジェクトファイルがありますが、今回使うのは、GNUmakefileだけです。このファイルを自分の環境に合わせて、チョコチョコと直してmakeすればいいのではないでしょうかね。

イチかバチかでmakeって実行してみましょうかね( ̄ー ̄)ニヤリ

cygwinのmakeを…とここまで書いて気づきました。cygwinのmakeもoMinGWのmakeも同じver 4でした。つまり、mingw32-make.exeをalias しなくてもcygwinのmakeを使えるのでは?

と思いましたが、ダメでした。cygwinのmake(4.0)とmingw32-make.exe(4.0.90)は違うようです。4.0で変更したけど、結局戻したものが4.0.90なのかもしれません…ダメか。

気を取り直して、crypto++をmakeしてみました。

5分後、ライブラリが出来てしまいました。スタティックリンク用のライブラリです。静的にくっついちゃうライブラリなので、別途ダイナミックリンクライブラリを入れる必要がないのでこれでいいです。

大丈夫なのだろうか、これで…

2種類の警告が出ていました。

1つめ、"usingキーワードを使って、簡単にアクセスしたらいいんじゃない?"という提案みたいです。
「ほっとけ」、と思うのですが気を利かせてご指摘くださっています。
GNUmakefileで-Wdeprecatedが指定されているためですね。これを削除すれば、警告が出なくなるかもしれませんが、ま、理由がわかっていれば、指摘してくれる分には問題ないので、そのままにします。

winpipes.h:115:2: warning: access declarations are deprecated in favour of using-declarations; suggestion: add the 'using' keyword [-Wdeprecated]
  NetworkSource::GetMaxWaitObjectCount;

2つめは、defineマクロの2重定義でした。Windowsのバージョンを示すマクロみたいですね。crypto++側は0x0400、MinGW側は0x502です。「redefined [enabled by default]」とあるので、再定義はO.K.のようです。
ということで0x400が今は有効になっているようです。

fipstest.cpp:11:0: warning: "_WIN32_WINNT" redefined [enabled by default]
 #define _WIN32_WINNT 0x0400
 ^
In file included from C:/Tools/develop/Qt/Qt5.3.0/Tools/mingw482_32/i686-w64-mingw32/include/crtdefs.h:10:0,
                 from C:/Tools/develop/Qt/Qt5.3.0/Tools/mingw482_32/i686-w64-mingw32/include/stddef.h:7,
                 from C:/Tools/develop/Qt/Qt5.3.0/Tools/mingw482_32/lib/gcc/i686-w64-mingw32/4.8.2/include/stddef.h:1,
                 from stdcpp.h:9,
                 from cryptlib.h:83,
                 from seckey.h:8,
                 from rijndael.h:7,
                 from aes.h:4,
                 from dll.h:11,
                 from fipstest.cpp:8:
C:/Tools/develop/Qt/Qt5.3.0/Tools/mingw482_32/i686-w64-mingw32/include/_mingw.h:229:0: note: this is the location of the previous definition
 #define _WIN32_WINNT 0x502
 ^

http://www.westbrook.jp/Tips/Win/OSVersion.htmlによると、0x400はWindows95らしいです。20年近く前ですが…

0x502は5.2でしょうから、Windows XP (64bit) Professionalみたいです。どちらにすべきでしょうか?

とりあえず、システム側であるMinGWの方を優先しましょう。なにかあったら、再ビルドすればいいですし。

fipstest.cppの11行目を"//"でコメントアウトしました。

で、再度makeを実行します。

というわけで、静的にリンクする用のライブラリ libcryptopp.a が出来上がりました。

cryptest.exeという実行ファイルが出来上がっていますので、ほんとにちゃんと出来てるのか気になる人はテストできます。

しかし、ライブラリは出来ましたが、使い方が分かりませんねぇ(^-^;
カンなんですが、cryptlib.hをインクルードすればいいんじゃないですかね、たぶん。

$ ./cryptest.exe
Test Driver for Crypto++(R) Library, a C++ Class Library of Cryptographic Schemes
- To generate an RSA key
        cryptest g
- To encrypt and decrypt a string using RSA
        cryptest r
- To sign a file using RSA
        cryptest rs privatekeyfile messagefile signaturefile
- To verify a signature of a file using RSA
        cryptest rv publickeyfile messagefile signaturefile
- To digest a file using several hash functions in parallel
        cryptest m file
- To encrypt and decrypt a string using DES-EDE in CBC mode
        cryptest t
- To encrypt or decrypt a file
        cryptest e|d input output
- To secret share a file (shares will be named file.000, file.001, etc)
        cryptest ss threshold number-of-shares file
- To reconstruct a secret-shared file
        cryptest sr file share1 share2 [....]
        (number of shares given must be equal to threshold)
- To information disperse a file (shares will be named file.000, file.001, etc)
        cryptest id threshold number-of-shares file
- To reconstruct an information-dispersed file
        cryptest ir file share1 share2 [....]
        (number of shares given must be equal to threshold)
- To gzip a file
        cryptest z compression-level input output
- To gunzip a file
        cryptest u input output
- To encrypt a file with AES in CTR mode
        cryptest ae input output
- To base64 encode a file
        cryptest e64 input output
- To base64 decode a file
        cryptest d64 input output
- To hex encode a file
        cryptest e16 input output
- To hex decode a file
        cryptest d16 input output
- To forward a TCP connection
        cryptest ft source-port destination-host destination-port
- To run the FIPS 140-2 sample application
        cryptest fips
- To generate 100000 random files using FIPS Approved X.917 RNG
        cryptest fips-rand
- To run Maurer's randomness test on a file
        cryptest mt input
- To run a test script (available in TestVectors subdirectory)
        cryptest tv filename
- To run validation tests
        cryptest v
- To display version number
       cryptest V
- To run benchmarks
        cryptest b [time allocated for each benchmark in seconds] [frequency of CPU in gigahertz]

(追記)

Readme.txtの他の特徴(Other features)の記述が興味深いです。

暗号化/復号化アルゴリズム以外にいろいろな処理が提供されています。
乱数発生器、素数生成とvelification、gzip/zipの圧縮と展開、なんかは興味がありますね。
PCMデータを圧縮して保存とかできそうですね。

  * pseudo random number generators (PRNG): ANSI X9.17 appendix C, RandomPool
  * password based key derivation functions: PBKDF1 and PBKDF2 from PKCS #5,
    PBKDF from PKCS #12 appendix B
  * Shamir's secret sharing scheme and Rabin's information dispersal algorithm
    (IDA)
  * fast multi-precision integer (bignum) and polynomial operations
  * finite field arithmetics, including GF(p) and GF(2^n)
  * prime number generation and verification
  * useful non-cryptographic algorithms
      + DEFLATE (RFC 1951) compression/decompression with gzip (RFC 1952) and
        zlib (RFC 1950) format support
      + hex, base-32, and base-64 coding/decoding
      + 32-bit CRC and Adler32 checksum
  * class wrappers for these operating system features (optional):
      + high resolution timers on Windows, Unix, and Mac OS
      + Berkeley and Windows style sockets
      + Windows named pipes
      + /dev/random, /dev/urandom, /dev/srandom
      + Microsoft's CryptGenRandom on Windows
  * A high level interface for most of the above, using a filter/pipeline
    metaphor
  * benchmarks and validation testing
  * x86, x86-64 (x64), MMX, and SSE2 assembly code for the most commonly used
    algorithms, with run-time CPU feature detection and code selection
  * some versions are available in FIPS 140-2 validated form

« 暗号化についても考えてみる (TrueCrypt開発終了?) | トップページ | キー入力とマウス »

コメント

コメントを書く

コメントは記事投稿者が公開するまで表示されません。

(ウェブ上には掲載しません)

トラックバック


この記事へのトラックバック一覧です: 暗号化についても考えてみる その3:

« 暗号化についても考えてみる (TrueCrypt開発終了?) | トップページ | キー入力とマウス »