diff options
Diffstat (limited to 'botan/src')
1097 files changed, 79292 insertions, 0 deletions
diff --git a/botan/src/.obj/debug-shared/adler32.o b/botan/src/.obj/debug-shared/adler32.o Binary files differnew file mode 100644 index 0000000..c141341 --- /dev/null +++ b/botan/src/.obj/debug-shared/adler32.o diff --git a/botan/src/.obj/debug-shared/aes.o b/botan/src/.obj/debug-shared/aes.o Binary files differnew file mode 100644 index 0000000..35fad19 --- /dev/null +++ b/botan/src/.obj/debug-shared/aes.o diff --git a/botan/src/.obj/debug-shared/aes_tab.o b/botan/src/.obj/debug-shared/aes_tab.o Binary files differnew file mode 100644 index 0000000..5c60fbe --- /dev/null +++ b/botan/src/.obj/debug-shared/aes_tab.o diff --git a/botan/src/.obj/debug-shared/alg_id.o b/botan/src/.obj/debug-shared/alg_id.o Binary files differnew file mode 100644 index 0000000..ce54157 --- /dev/null +++ b/botan/src/.obj/debug-shared/alg_id.o diff --git a/botan/src/.obj/debug-shared/algo_factory.o b/botan/src/.obj/debug-shared/algo_factory.o Binary files differnew file mode 100644 index 0000000..7b43a50 --- /dev/null +++ b/botan/src/.obj/debug-shared/algo_factory.o diff --git a/botan/src/.obj/debug-shared/algo_filt.o b/botan/src/.obj/debug-shared/algo_filt.o Binary files differnew file mode 100644 index 0000000..2458ba7 --- /dev/null +++ b/botan/src/.obj/debug-shared/algo_filt.o diff --git a/botan/src/.obj/debug-shared/arc4.o b/botan/src/.obj/debug-shared/arc4.o Binary files differnew file mode 100644 index 0000000..6436153 --- /dev/null +++ b/botan/src/.obj/debug-shared/arc4.o diff --git a/botan/src/.obj/debug-shared/asn1_alt.o b/botan/src/.obj/debug-shared/asn1_alt.o Binary files differnew file mode 100644 index 0000000..a6466b3 --- /dev/null +++ b/botan/src/.obj/debug-shared/asn1_alt.o diff --git a/botan/src/.obj/debug-shared/asn1_att.o b/botan/src/.obj/debug-shared/asn1_att.o Binary files differnew file mode 100644 index 0000000..efd5c4e --- /dev/null +++ b/botan/src/.obj/debug-shared/asn1_att.o diff --git a/botan/src/.obj/debug-shared/asn1_dn.o b/botan/src/.obj/debug-shared/asn1_dn.o Binary files differnew file mode 100644 index 0000000..ec63bba --- /dev/null +++ b/botan/src/.obj/debug-shared/asn1_dn.o diff --git a/botan/src/.obj/debug-shared/asn1_eac_str.o b/botan/src/.obj/debug-shared/asn1_eac_str.o Binary files differnew file mode 100644 index 0000000..d96b4ee --- /dev/null +++ b/botan/src/.obj/debug-shared/asn1_eac_str.o diff --git a/botan/src/.obj/debug-shared/asn1_eac_tm.o b/botan/src/.obj/debug-shared/asn1_eac_tm.o Binary files differnew file mode 100644 index 0000000..10a3025 --- /dev/null +++ b/botan/src/.obj/debug-shared/asn1_eac_tm.o diff --git a/botan/src/.obj/debug-shared/asn1_int.o b/botan/src/.obj/debug-shared/asn1_int.o Binary files differnew file mode 100644 index 0000000..272c761 --- /dev/null +++ b/botan/src/.obj/debug-shared/asn1_int.o diff --git a/botan/src/.obj/debug-shared/asn1_oid.o b/botan/src/.obj/debug-shared/asn1_oid.o Binary files differnew file mode 100644 index 0000000..cadab8a --- /dev/null +++ b/botan/src/.obj/debug-shared/asn1_oid.o diff --git a/botan/src/.obj/debug-shared/asn1_str.o b/botan/src/.obj/debug-shared/asn1_str.o Binary files differnew file mode 100644 index 0000000..ae89911 --- /dev/null +++ b/botan/src/.obj/debug-shared/asn1_str.o diff --git a/botan/src/.obj/debug-shared/asn1_tm.o b/botan/src/.obj/debug-shared/asn1_tm.o Binary files differnew file mode 100644 index 0000000..a80e73c --- /dev/null +++ b/botan/src/.obj/debug-shared/asn1_tm.o diff --git a/botan/src/.obj/debug-shared/auto_rng.o b/botan/src/.obj/debug-shared/auto_rng.o Binary files differnew file mode 100644 index 0000000..92cfb37 --- /dev/null +++ b/botan/src/.obj/debug-shared/auto_rng.o diff --git a/botan/src/.obj/debug-shared/b64_char.o b/botan/src/.obj/debug-shared/b64_char.o Binary files differnew file mode 100644 index 0000000..dfdc73e --- /dev/null +++ b/botan/src/.obj/debug-shared/b64_char.o diff --git a/botan/src/.obj/debug-shared/base64.o b/botan/src/.obj/debug-shared/base64.o Binary files differnew file mode 100644 index 0000000..5785000 --- /dev/null +++ b/botan/src/.obj/debug-shared/base64.o diff --git a/botan/src/.obj/debug-shared/basefilt.o b/botan/src/.obj/debug-shared/basefilt.o Binary files differnew file mode 100644 index 0000000..706ec96 --- /dev/null +++ b/botan/src/.obj/debug-shared/basefilt.o diff --git a/botan/src/.obj/debug-shared/benchmark.o b/botan/src/.obj/debug-shared/benchmark.o Binary files differnew file mode 100644 index 0000000..95ab486 --- /dev/null +++ b/botan/src/.obj/debug-shared/benchmark.o diff --git a/botan/src/.obj/debug-shared/ber_dec.o b/botan/src/.obj/debug-shared/ber_dec.o Binary files differnew file mode 100644 index 0000000..7abcd76 --- /dev/null +++ b/botan/src/.obj/debug-shared/ber_dec.o diff --git a/botan/src/.obj/debug-shared/big_code.o b/botan/src/.obj/debug-shared/big_code.o Binary files differnew file mode 100644 index 0000000..8c36907 --- /dev/null +++ b/botan/src/.obj/debug-shared/big_code.o diff --git a/botan/src/.obj/debug-shared/big_io.o b/botan/src/.obj/debug-shared/big_io.o Binary files differnew file mode 100644 index 0000000..cb85aa3 --- /dev/null +++ b/botan/src/.obj/debug-shared/big_io.o diff --git a/botan/src/.obj/debug-shared/big_ops2.o b/botan/src/.obj/debug-shared/big_ops2.o Binary files differnew file mode 100644 index 0000000..c5a6f2f --- /dev/null +++ b/botan/src/.obj/debug-shared/big_ops2.o diff --git a/botan/src/.obj/debug-shared/big_ops3.o b/botan/src/.obj/debug-shared/big_ops3.o Binary files differnew file mode 100644 index 0000000..bad3fa3 --- /dev/null +++ b/botan/src/.obj/debug-shared/big_ops3.o diff --git a/botan/src/.obj/debug-shared/big_rand.o b/botan/src/.obj/debug-shared/big_rand.o Binary files differnew file mode 100644 index 0000000..2c6a6bc --- /dev/null +++ b/botan/src/.obj/debug-shared/big_rand.o diff --git a/botan/src/.obj/debug-shared/bigint.o b/botan/src/.obj/debug-shared/bigint.o Binary files differnew file mode 100644 index 0000000..ca03c56 --- /dev/null +++ b/botan/src/.obj/debug-shared/bigint.o diff --git a/botan/src/.obj/debug-shared/blfs_tab.o b/botan/src/.obj/debug-shared/blfs_tab.o Binary files differnew file mode 100644 index 0000000..5b38825 --- /dev/null +++ b/botan/src/.obj/debug-shared/blfs_tab.o diff --git a/botan/src/.obj/debug-shared/blinding.o b/botan/src/.obj/debug-shared/blinding.o Binary files differnew file mode 100644 index 0000000..a987906 --- /dev/null +++ b/botan/src/.obj/debug-shared/blinding.o diff --git a/botan/src/.obj/debug-shared/blowfish.o b/botan/src/.obj/debug-shared/blowfish.o Binary files differnew file mode 100644 index 0000000..a735489 --- /dev/null +++ b/botan/src/.obj/debug-shared/blowfish.o diff --git a/botan/src/.obj/debug-shared/buf_filt.o b/botan/src/.obj/debug-shared/buf_filt.o Binary files differnew file mode 100644 index 0000000..e032df7 --- /dev/null +++ b/botan/src/.obj/debug-shared/buf_filt.o diff --git a/botan/src/.obj/debug-shared/cast128.o b/botan/src/.obj/debug-shared/cast128.o Binary files differnew file mode 100644 index 0000000..52b9657 --- /dev/null +++ b/botan/src/.obj/debug-shared/cast128.o diff --git a/botan/src/.obj/debug-shared/cast256.o b/botan/src/.obj/debug-shared/cast256.o Binary files differnew file mode 100644 index 0000000..d56d51e --- /dev/null +++ b/botan/src/.obj/debug-shared/cast256.o diff --git a/botan/src/.obj/debug-shared/cast_tab.o b/botan/src/.obj/debug-shared/cast_tab.o Binary files differnew file mode 100644 index 0000000..823269d --- /dev/null +++ b/botan/src/.obj/debug-shared/cast_tab.o diff --git a/botan/src/.obj/debug-shared/cbc.o b/botan/src/.obj/debug-shared/cbc.o Binary files differnew file mode 100644 index 0000000..b53293f --- /dev/null +++ b/botan/src/.obj/debug-shared/cbc.o diff --git a/botan/src/.obj/debug-shared/cbc_mac.o b/botan/src/.obj/debug-shared/cbc_mac.o Binary files differnew file mode 100644 index 0000000..13ad287 --- /dev/null +++ b/botan/src/.obj/debug-shared/cbc_mac.o diff --git a/botan/src/.obj/debug-shared/certstor.o b/botan/src/.obj/debug-shared/certstor.o Binary files differnew file mode 100644 index 0000000..5b43a5e --- /dev/null +++ b/botan/src/.obj/debug-shared/certstor.o diff --git a/botan/src/.obj/debug-shared/cfb.o b/botan/src/.obj/debug-shared/cfb.o Binary files differnew file mode 100644 index 0000000..39b76a9 --- /dev/null +++ b/botan/src/.obj/debug-shared/cfb.o diff --git a/botan/src/.obj/debug-shared/charset.o b/botan/src/.obj/debug-shared/charset.o Binary files differnew file mode 100644 index 0000000..ae27196 --- /dev/null +++ b/botan/src/.obj/debug-shared/charset.o diff --git a/botan/src/.obj/debug-shared/cmac.o b/botan/src/.obj/debug-shared/cmac.o Binary files differnew file mode 100644 index 0000000..42c6daa --- /dev/null +++ b/botan/src/.obj/debug-shared/cmac.o diff --git a/botan/src/.obj/debug-shared/cms_algo.o b/botan/src/.obj/debug-shared/cms_algo.o Binary files differnew file mode 100644 index 0000000..c355f50 --- /dev/null +++ b/botan/src/.obj/debug-shared/cms_algo.o diff --git a/botan/src/.obj/debug-shared/cms_comp.o b/botan/src/.obj/debug-shared/cms_comp.o Binary files differnew file mode 100644 index 0000000..6c99ce4 --- /dev/null +++ b/botan/src/.obj/debug-shared/cms_comp.o diff --git a/botan/src/.obj/debug-shared/cms_dalg.o b/botan/src/.obj/debug-shared/cms_dalg.o Binary files differnew file mode 100644 index 0000000..ad18cc0 --- /dev/null +++ b/botan/src/.obj/debug-shared/cms_dalg.o diff --git a/botan/src/.obj/debug-shared/cms_dec.o b/botan/src/.obj/debug-shared/cms_dec.o Binary files differnew file mode 100644 index 0000000..ed25af9 --- /dev/null +++ b/botan/src/.obj/debug-shared/cms_dec.o diff --git a/botan/src/.obj/debug-shared/cms_ealg.o b/botan/src/.obj/debug-shared/cms_ealg.o Binary files differnew file mode 100644 index 0000000..b06566c --- /dev/null +++ b/botan/src/.obj/debug-shared/cms_ealg.o diff --git a/botan/src/.obj/debug-shared/cms_enc.o b/botan/src/.obj/debug-shared/cms_enc.o Binary files differnew file mode 100644 index 0000000..df74976 --- /dev/null +++ b/botan/src/.obj/debug-shared/cms_enc.o diff --git a/botan/src/.obj/debug-shared/crc24.o b/botan/src/.obj/debug-shared/crc24.o Binary files differnew file mode 100644 index 0000000..15b9d20 --- /dev/null +++ b/botan/src/.obj/debug-shared/crc24.o diff --git a/botan/src/.obj/debug-shared/crc32.o b/botan/src/.obj/debug-shared/crc32.o Binary files differnew file mode 100644 index 0000000..42c4ed4 --- /dev/null +++ b/botan/src/.obj/debug-shared/crc32.o diff --git a/botan/src/.obj/debug-shared/crl_ent.o b/botan/src/.obj/debug-shared/crl_ent.o Binary files differnew file mode 100644 index 0000000..276119f --- /dev/null +++ b/botan/src/.obj/debug-shared/crl_ent.o diff --git a/botan/src/.obj/debug-shared/cryptobox.o b/botan/src/.obj/debug-shared/cryptobox.o Binary files differnew file mode 100644 index 0000000..e395ef5 --- /dev/null +++ b/botan/src/.obj/debug-shared/cryptobox.o diff --git a/botan/src/.obj/debug-shared/ctr.o b/botan/src/.obj/debug-shared/ctr.o Binary files differnew file mode 100644 index 0000000..c40ce6c --- /dev/null +++ b/botan/src/.obj/debug-shared/ctr.o diff --git a/botan/src/.obj/debug-shared/cts.o b/botan/src/.obj/debug-shared/cts.o Binary files differnew file mode 100644 index 0000000..6024736 --- /dev/null +++ b/botan/src/.obj/debug-shared/cts.o diff --git a/botan/src/.obj/debug-shared/curve_gfp.o b/botan/src/.obj/debug-shared/curve_gfp.o Binary files differnew file mode 100644 index 0000000..9691704 --- /dev/null +++ b/botan/src/.obj/debug-shared/curve_gfp.o diff --git a/botan/src/.obj/debug-shared/cvc_ado.o b/botan/src/.obj/debug-shared/cvc_ado.o Binary files differnew file mode 100644 index 0000000..a80cff1 --- /dev/null +++ b/botan/src/.obj/debug-shared/cvc_ado.o diff --git a/botan/src/.obj/debug-shared/cvc_ca.o b/botan/src/.obj/debug-shared/cvc_ca.o Binary files differnew file mode 100644 index 0000000..1af1db1 --- /dev/null +++ b/botan/src/.obj/debug-shared/cvc_ca.o diff --git a/botan/src/.obj/debug-shared/cvc_cert.o b/botan/src/.obj/debug-shared/cvc_cert.o Binary files differnew file mode 100644 index 0000000..263c61d --- /dev/null +++ b/botan/src/.obj/debug-shared/cvc_cert.o diff --git a/botan/src/.obj/debug-shared/cvc_req.o b/botan/src/.obj/debug-shared/cvc_req.o Binary files differnew file mode 100644 index 0000000..12a206a --- /dev/null +++ b/botan/src/.obj/debug-shared/cvc_req.o diff --git a/botan/src/.obj/debug-shared/cvc_self.o b/botan/src/.obj/debug-shared/cvc_self.o Binary files differnew file mode 100644 index 0000000..2800f3c --- /dev/null +++ b/botan/src/.obj/debug-shared/cvc_self.o diff --git a/botan/src/.obj/debug-shared/data_snk.o b/botan/src/.obj/debug-shared/data_snk.o Binary files differnew file mode 100644 index 0000000..d098c94 --- /dev/null +++ b/botan/src/.obj/debug-shared/data_snk.o diff --git a/botan/src/.obj/debug-shared/data_src.o b/botan/src/.obj/debug-shared/data_src.o Binary files differnew file mode 100644 index 0000000..1f4ce2c --- /dev/null +++ b/botan/src/.obj/debug-shared/data_src.o diff --git a/botan/src/.obj/debug-shared/datastor.o b/botan/src/.obj/debug-shared/datastor.o Binary files differnew file mode 100644 index 0000000..a857d51 --- /dev/null +++ b/botan/src/.obj/debug-shared/datastor.o diff --git a/botan/src/.obj/debug-shared/def_mode.o b/botan/src/.obj/debug-shared/def_mode.o Binary files differnew file mode 100644 index 0000000..551abc4 --- /dev/null +++ b/botan/src/.obj/debug-shared/def_mode.o diff --git a/botan/src/.obj/debug-shared/def_pk_ops.o b/botan/src/.obj/debug-shared/def_pk_ops.o Binary files differnew file mode 100644 index 0000000..5afc53b --- /dev/null +++ b/botan/src/.obj/debug-shared/def_pk_ops.o diff --git a/botan/src/.obj/debug-shared/def_powm.o b/botan/src/.obj/debug-shared/def_powm.o Binary files differnew file mode 100644 index 0000000..0386e25 --- /dev/null +++ b/botan/src/.obj/debug-shared/def_powm.o diff --git a/botan/src/.obj/debug-shared/defalloc.o b/botan/src/.obj/debug-shared/defalloc.o Binary files differnew file mode 100644 index 0000000..5221988 --- /dev/null +++ b/botan/src/.obj/debug-shared/defalloc.o diff --git a/botan/src/.obj/debug-shared/der_enc.o b/botan/src/.obj/debug-shared/der_enc.o Binary files differnew file mode 100644 index 0000000..e584f0f --- /dev/null +++ b/botan/src/.obj/debug-shared/der_enc.o diff --git a/botan/src/.obj/debug-shared/des.o b/botan/src/.obj/debug-shared/des.o Binary files differnew file mode 100644 index 0000000..29fdd94 --- /dev/null +++ b/botan/src/.obj/debug-shared/des.o diff --git a/botan/src/.obj/debug-shared/des_tab.o b/botan/src/.obj/debug-shared/des_tab.o Binary files differnew file mode 100644 index 0000000..aee7c49 --- /dev/null +++ b/botan/src/.obj/debug-shared/des_tab.o diff --git a/botan/src/.obj/debug-shared/desx.o b/botan/src/.obj/debug-shared/desx.o Binary files differnew file mode 100644 index 0000000..f2351e8 --- /dev/null +++ b/botan/src/.obj/debug-shared/desx.o diff --git a/botan/src/.obj/debug-shared/dh.o b/botan/src/.obj/debug-shared/dh.o Binary files differnew file mode 100644 index 0000000..544c61f --- /dev/null +++ b/botan/src/.obj/debug-shared/dh.o diff --git a/botan/src/.obj/debug-shared/dh_core.o b/botan/src/.obj/debug-shared/dh_core.o Binary files differnew file mode 100644 index 0000000..7f55f3e --- /dev/null +++ b/botan/src/.obj/debug-shared/dh_core.o diff --git a/botan/src/.obj/debug-shared/divide.o b/botan/src/.obj/debug-shared/divide.o Binary files differnew file mode 100644 index 0000000..ffc64a3 --- /dev/null +++ b/botan/src/.obj/debug-shared/divide.o diff --git a/botan/src/.obj/debug-shared/dl_algo.o b/botan/src/.obj/debug-shared/dl_algo.o Binary files differnew file mode 100644 index 0000000..f769572 --- /dev/null +++ b/botan/src/.obj/debug-shared/dl_algo.o diff --git a/botan/src/.obj/debug-shared/dl_group.o b/botan/src/.obj/debug-shared/dl_group.o Binary files differnew file mode 100644 index 0000000..bb3ae2e --- /dev/null +++ b/botan/src/.obj/debug-shared/dl_group.o diff --git a/botan/src/.obj/debug-shared/dlies.o b/botan/src/.obj/debug-shared/dlies.o Binary files differnew file mode 100644 index 0000000..c75df7a --- /dev/null +++ b/botan/src/.obj/debug-shared/dlies.o diff --git a/botan/src/.obj/debug-shared/dsa.o b/botan/src/.obj/debug-shared/dsa.o Binary files differnew file mode 100644 index 0000000..878c333 --- /dev/null +++ b/botan/src/.obj/debug-shared/dsa.o diff --git a/botan/src/.obj/debug-shared/dsa_core.o b/botan/src/.obj/debug-shared/dsa_core.o Binary files differnew file mode 100644 index 0000000..542fb3b --- /dev/null +++ b/botan/src/.obj/debug-shared/dsa_core.o diff --git a/botan/src/.obj/debug-shared/dsa_gen.o b/botan/src/.obj/debug-shared/dsa_gen.o Binary files differnew file mode 100644 index 0000000..203229a --- /dev/null +++ b/botan/src/.obj/debug-shared/dsa_gen.o diff --git a/botan/src/.obj/debug-shared/dsa_op.o b/botan/src/.obj/debug-shared/dsa_op.o Binary files differnew file mode 100644 index 0000000..10bc1dc --- /dev/null +++ b/botan/src/.obj/debug-shared/dsa_op.o diff --git a/botan/src/.obj/debug-shared/eax.o b/botan/src/.obj/debug-shared/eax.o Binary files differnew file mode 100644 index 0000000..28d039b --- /dev/null +++ b/botan/src/.obj/debug-shared/eax.o diff --git a/botan/src/.obj/debug-shared/eax_dec.o b/botan/src/.obj/debug-shared/eax_dec.o Binary files differnew file mode 100644 index 0000000..bb8c770 --- /dev/null +++ b/botan/src/.obj/debug-shared/eax_dec.o diff --git a/botan/src/.obj/debug-shared/ec_dompar.o b/botan/src/.obj/debug-shared/ec_dompar.o Binary files differnew file mode 100644 index 0000000..f718902 --- /dev/null +++ b/botan/src/.obj/debug-shared/ec_dompar.o diff --git a/botan/src/.obj/debug-shared/ecb.o b/botan/src/.obj/debug-shared/ecb.o Binary files differnew file mode 100644 index 0000000..e3357c4 --- /dev/null +++ b/botan/src/.obj/debug-shared/ecb.o diff --git a/botan/src/.obj/debug-shared/ecc_key.o b/botan/src/.obj/debug-shared/ecc_key.o Binary files differnew file mode 100644 index 0000000..4f626f2 --- /dev/null +++ b/botan/src/.obj/debug-shared/ecc_key.o diff --git a/botan/src/.obj/debug-shared/ecdsa.o b/botan/src/.obj/debug-shared/ecdsa.o Binary files differnew file mode 100644 index 0000000..a1d29de --- /dev/null +++ b/botan/src/.obj/debug-shared/ecdsa.o diff --git a/botan/src/.obj/debug-shared/ecdsa_core.o b/botan/src/.obj/debug-shared/ecdsa_core.o Binary files differnew file mode 100644 index 0000000..4916a1f --- /dev/null +++ b/botan/src/.obj/debug-shared/ecdsa_core.o diff --git a/botan/src/.obj/debug-shared/ecdsa_op.o b/botan/src/.obj/debug-shared/ecdsa_op.o Binary files differnew file mode 100644 index 0000000..19514ee --- /dev/null +++ b/botan/src/.obj/debug-shared/ecdsa_op.o diff --git a/botan/src/.obj/debug-shared/ecdsa_sig.o b/botan/src/.obj/debug-shared/ecdsa_sig.o Binary files differnew file mode 100644 index 0000000..69c18ff --- /dev/null +++ b/botan/src/.obj/debug-shared/ecdsa_sig.o diff --git a/botan/src/.obj/debug-shared/eckaeg.o b/botan/src/.obj/debug-shared/eckaeg.o Binary files differnew file mode 100644 index 0000000..217aa56 --- /dev/null +++ b/botan/src/.obj/debug-shared/eckaeg.o diff --git a/botan/src/.obj/debug-shared/eckaeg_core.o b/botan/src/.obj/debug-shared/eckaeg_core.o Binary files differnew file mode 100644 index 0000000..7b8ac17 --- /dev/null +++ b/botan/src/.obj/debug-shared/eckaeg_core.o diff --git a/botan/src/.obj/debug-shared/eckaeg_op.o b/botan/src/.obj/debug-shared/eckaeg_op.o Binary files differnew file mode 100644 index 0000000..29e186b --- /dev/null +++ b/botan/src/.obj/debug-shared/eckaeg_op.o diff --git a/botan/src/.obj/debug-shared/elg_core.o b/botan/src/.obj/debug-shared/elg_core.o Binary files differnew file mode 100644 index 0000000..a54e8ad --- /dev/null +++ b/botan/src/.obj/debug-shared/elg_core.o diff --git a/botan/src/.obj/debug-shared/elg_op.o b/botan/src/.obj/debug-shared/elg_op.o Binary files differnew file mode 100644 index 0000000..8185fdc --- /dev/null +++ b/botan/src/.obj/debug-shared/elg_op.o diff --git a/botan/src/.obj/debug-shared/elgamal.o b/botan/src/.obj/debug-shared/elgamal.o Binary files differnew file mode 100644 index 0000000..34a0e96 --- /dev/null +++ b/botan/src/.obj/debug-shared/elgamal.o diff --git a/botan/src/.obj/debug-shared/eme.o b/botan/src/.obj/debug-shared/eme.o Binary files differnew file mode 100644 index 0000000..e9995ff --- /dev/null +++ b/botan/src/.obj/debug-shared/eme.o diff --git a/botan/src/.obj/debug-shared/eme1.o b/botan/src/.obj/debug-shared/eme1.o Binary files differnew file mode 100644 index 0000000..dfd1fc6 --- /dev/null +++ b/botan/src/.obj/debug-shared/eme1.o diff --git a/botan/src/.obj/debug-shared/eme_pkcs.o b/botan/src/.obj/debug-shared/eme_pkcs.o Binary files differnew file mode 100644 index 0000000..1981db3 --- /dev/null +++ b/botan/src/.obj/debug-shared/eme_pkcs.o diff --git a/botan/src/.obj/debug-shared/emsa1.o b/botan/src/.obj/debug-shared/emsa1.o Binary files differnew file mode 100644 index 0000000..b912205 --- /dev/null +++ b/botan/src/.obj/debug-shared/emsa1.o diff --git a/botan/src/.obj/debug-shared/emsa1_bsi.o b/botan/src/.obj/debug-shared/emsa1_bsi.o Binary files differnew file mode 100644 index 0000000..f28a7b3 --- /dev/null +++ b/botan/src/.obj/debug-shared/emsa1_bsi.o diff --git a/botan/src/.obj/debug-shared/emsa2.o b/botan/src/.obj/debug-shared/emsa2.o Binary files differnew file mode 100644 index 0000000..2ea2f72 --- /dev/null +++ b/botan/src/.obj/debug-shared/emsa2.o diff --git a/botan/src/.obj/debug-shared/emsa3.o b/botan/src/.obj/debug-shared/emsa3.o Binary files differnew file mode 100644 index 0000000..dd7ebb3 --- /dev/null +++ b/botan/src/.obj/debug-shared/emsa3.o diff --git a/botan/src/.obj/debug-shared/emsa4.o b/botan/src/.obj/debug-shared/emsa4.o Binary files differnew file mode 100644 index 0000000..b020392 --- /dev/null +++ b/botan/src/.obj/debug-shared/emsa4.o diff --git a/botan/src/.obj/debug-shared/emsa_raw.o b/botan/src/.obj/debug-shared/emsa_raw.o Binary files differnew file mode 100644 index 0000000..1e5a631 --- /dev/null +++ b/botan/src/.obj/debug-shared/emsa_raw.o diff --git a/botan/src/.obj/debug-shared/es_dev.o b/botan/src/.obj/debug-shared/es_dev.o Binary files differnew file mode 100644 index 0000000..13e3a4a --- /dev/null +++ b/botan/src/.obj/debug-shared/es_dev.o diff --git a/botan/src/.obj/debug-shared/es_egd.o b/botan/src/.obj/debug-shared/es_egd.o Binary files differnew file mode 100644 index 0000000..ffc7f78 --- /dev/null +++ b/botan/src/.obj/debug-shared/es_egd.o diff --git a/botan/src/.obj/debug-shared/es_ftw.o b/botan/src/.obj/debug-shared/es_ftw.o Binary files differnew file mode 100644 index 0000000..2dfeba8 --- /dev/null +++ b/botan/src/.obj/debug-shared/es_ftw.o diff --git a/botan/src/.obj/debug-shared/es_unix.o b/botan/src/.obj/debug-shared/es_unix.o Binary files differnew file mode 100644 index 0000000..00d79a6 --- /dev/null +++ b/botan/src/.obj/debug-shared/es_unix.o diff --git a/botan/src/.obj/debug-shared/exceptn.o b/botan/src/.obj/debug-shared/exceptn.o Binary files differnew file mode 100644 index 0000000..1a26041 --- /dev/null +++ b/botan/src/.obj/debug-shared/exceptn.o diff --git a/botan/src/.obj/debug-shared/fd_unix.o b/botan/src/.obj/debug-shared/fd_unix.o Binary files differnew file mode 100644 index 0000000..b4d8fcf --- /dev/null +++ b/botan/src/.obj/debug-shared/fd_unix.o diff --git a/botan/src/.obj/debug-shared/filter.o b/botan/src/.obj/debug-shared/filter.o Binary files differnew file mode 100644 index 0000000..4f0f9ae --- /dev/null +++ b/botan/src/.obj/debug-shared/filter.o diff --git a/botan/src/.obj/debug-shared/fork256.o b/botan/src/.obj/debug-shared/fork256.o Binary files differnew file mode 100644 index 0000000..c776f3d --- /dev/null +++ b/botan/src/.obj/debug-shared/fork256.o diff --git a/botan/src/.obj/debug-shared/get_enc.o b/botan/src/.obj/debug-shared/get_enc.o Binary files differnew file mode 100644 index 0000000..88859af --- /dev/null +++ b/botan/src/.obj/debug-shared/get_enc.o diff --git a/botan/src/.obj/debug-shared/get_pbe.o b/botan/src/.obj/debug-shared/get_pbe.o Binary files differnew file mode 100644 index 0000000..011a79f --- /dev/null +++ b/botan/src/.obj/debug-shared/get_pbe.o diff --git a/botan/src/.obj/debug-shared/gfp_element.o b/botan/src/.obj/debug-shared/gfp_element.o Binary files differnew file mode 100644 index 0000000..279035f --- /dev/null +++ b/botan/src/.obj/debug-shared/gfp_element.o diff --git a/botan/src/.obj/debug-shared/gost_28147.o b/botan/src/.obj/debug-shared/gost_28147.o Binary files differnew file mode 100644 index 0000000..e1bf41f --- /dev/null +++ b/botan/src/.obj/debug-shared/gost_28147.o diff --git a/botan/src/.obj/debug-shared/gost_3411.o b/botan/src/.obj/debug-shared/gost_3411.o Binary files differnew file mode 100644 index 0000000..c024125 --- /dev/null +++ b/botan/src/.obj/debug-shared/gost_3411.o diff --git a/botan/src/.obj/debug-shared/has160.o b/botan/src/.obj/debug-shared/has160.o Binary files differnew file mode 100644 index 0000000..f312abd --- /dev/null +++ b/botan/src/.obj/debug-shared/has160.o diff --git a/botan/src/.obj/debug-shared/hash_id.o b/botan/src/.obj/debug-shared/hash_id.o Binary files differnew file mode 100644 index 0000000..afc0cbb --- /dev/null +++ b/botan/src/.obj/debug-shared/hash_id.o diff --git a/botan/src/.obj/debug-shared/hex.o b/botan/src/.obj/debug-shared/hex.o Binary files differnew file mode 100644 index 0000000..1d19e9f --- /dev/null +++ b/botan/src/.obj/debug-shared/hex.o diff --git a/botan/src/.obj/debug-shared/hex_char.o b/botan/src/.obj/debug-shared/hex_char.o Binary files differnew file mode 100644 index 0000000..1211682 --- /dev/null +++ b/botan/src/.obj/debug-shared/hex_char.o diff --git a/botan/src/.obj/debug-shared/hmac.o b/botan/src/.obj/debug-shared/hmac.o Binary files differnew file mode 100644 index 0000000..d29ecfe --- /dev/null +++ b/botan/src/.obj/debug-shared/hmac.o diff --git a/botan/src/.obj/debug-shared/hmac_rng.o b/botan/src/.obj/debug-shared/hmac_rng.o Binary files differnew file mode 100644 index 0000000..abd74a8 --- /dev/null +++ b/botan/src/.obj/debug-shared/hmac_rng.o diff --git a/botan/src/.obj/debug-shared/idea.o b/botan/src/.obj/debug-shared/idea.o Binary files differnew file mode 100644 index 0000000..69f7e0b --- /dev/null +++ b/botan/src/.obj/debug-shared/idea.o diff --git a/botan/src/.obj/debug-shared/if_algo.o b/botan/src/.obj/debug-shared/if_algo.o Binary files differnew file mode 100644 index 0000000..0be681e --- /dev/null +++ b/botan/src/.obj/debug-shared/if_algo.o diff --git a/botan/src/.obj/debug-shared/if_core.o b/botan/src/.obj/debug-shared/if_core.o Binary files differnew file mode 100644 index 0000000..e2d86ea --- /dev/null +++ b/botan/src/.obj/debug-shared/if_core.o diff --git a/botan/src/.obj/debug-shared/if_op.o b/botan/src/.obj/debug-shared/if_op.o Binary files differnew file mode 100644 index 0000000..9ac607c --- /dev/null +++ b/botan/src/.obj/debug-shared/if_op.o diff --git a/botan/src/.obj/debug-shared/init.o b/botan/src/.obj/debug-shared/init.o Binary files differnew file mode 100644 index 0000000..2ef69ac --- /dev/null +++ b/botan/src/.obj/debug-shared/init.o diff --git a/botan/src/.obj/debug-shared/jacobi.o b/botan/src/.obj/debug-shared/jacobi.o Binary files differnew file mode 100644 index 0000000..a58b0c9 --- /dev/null +++ b/botan/src/.obj/debug-shared/jacobi.o diff --git a/botan/src/.obj/debug-shared/kasumi.o b/botan/src/.obj/debug-shared/kasumi.o Binary files differnew file mode 100644 index 0000000..04fb935 --- /dev/null +++ b/botan/src/.obj/debug-shared/kasumi.o diff --git a/botan/src/.obj/debug-shared/kdf.o b/botan/src/.obj/debug-shared/kdf.o Binary files differnew file mode 100644 index 0000000..6ba5f51 --- /dev/null +++ b/botan/src/.obj/debug-shared/kdf.o diff --git a/botan/src/.obj/debug-shared/kdf1.o b/botan/src/.obj/debug-shared/kdf1.o Binary files differnew file mode 100644 index 0000000..4b94931 --- /dev/null +++ b/botan/src/.obj/debug-shared/kdf1.o diff --git a/botan/src/.obj/debug-shared/kdf2.o b/botan/src/.obj/debug-shared/kdf2.o Binary files differnew file mode 100644 index 0000000..55c224b --- /dev/null +++ b/botan/src/.obj/debug-shared/kdf2.o diff --git a/botan/src/.obj/debug-shared/keypair.o b/botan/src/.obj/debug-shared/keypair.o Binary files differnew file mode 100644 index 0000000..5a841dc --- /dev/null +++ b/botan/src/.obj/debug-shared/keypair.o diff --git a/botan/src/.obj/debug-shared/libstate.o b/botan/src/.obj/debug-shared/libstate.o Binary files differnew file mode 100644 index 0000000..bd8a7b8 --- /dev/null +++ b/botan/src/.obj/debug-shared/libstate.o diff --git a/botan/src/.obj/debug-shared/lion.o b/botan/src/.obj/debug-shared/lion.o Binary files differnew file mode 100644 index 0000000..c8635d7 --- /dev/null +++ b/botan/src/.obj/debug-shared/lion.o diff --git a/botan/src/.obj/debug-shared/look_pk.o b/botan/src/.obj/debug-shared/look_pk.o Binary files differnew file mode 100644 index 0000000..fe16b55 --- /dev/null +++ b/botan/src/.obj/debug-shared/look_pk.o diff --git a/botan/src/.obj/debug-shared/lookup.o b/botan/src/.obj/debug-shared/lookup.o Binary files differnew file mode 100644 index 0000000..4c5a6d1 --- /dev/null +++ b/botan/src/.obj/debug-shared/lookup.o diff --git a/botan/src/.obj/debug-shared/lookup_block.o b/botan/src/.obj/debug-shared/lookup_block.o Binary files differnew file mode 100644 index 0000000..181a990 --- /dev/null +++ b/botan/src/.obj/debug-shared/lookup_block.o diff --git a/botan/src/.obj/debug-shared/lookup_hash.o b/botan/src/.obj/debug-shared/lookup_hash.o Binary files differnew file mode 100644 index 0000000..f8c2c2e --- /dev/null +++ b/botan/src/.obj/debug-shared/lookup_hash.o diff --git a/botan/src/.obj/debug-shared/lookup_mac.o b/botan/src/.obj/debug-shared/lookup_mac.o Binary files differnew file mode 100644 index 0000000..b3b839e --- /dev/null +++ b/botan/src/.obj/debug-shared/lookup_mac.o diff --git a/botan/src/.obj/debug-shared/lookup_stream.o b/botan/src/.obj/debug-shared/lookup_stream.o Binary files differnew file mode 100644 index 0000000..66a46df --- /dev/null +++ b/botan/src/.obj/debug-shared/lookup_stream.o diff --git a/botan/src/.obj/debug-shared/lubyrack.o b/botan/src/.obj/debug-shared/lubyrack.o Binary files differnew file mode 100644 index 0000000..231b3cc --- /dev/null +++ b/botan/src/.obj/debug-shared/lubyrack.o diff --git a/botan/src/.obj/debug-shared/mac.o b/botan/src/.obj/debug-shared/mac.o Binary files differnew file mode 100644 index 0000000..6dfe76b --- /dev/null +++ b/botan/src/.obj/debug-shared/mac.o diff --git a/botan/src/.obj/debug-shared/make_prm.o b/botan/src/.obj/debug-shared/make_prm.o Binary files differnew file mode 100644 index 0000000..3950130 --- /dev/null +++ b/botan/src/.obj/debug-shared/make_prm.o diff --git a/botan/src/.obj/debug-shared/mars.o b/botan/src/.obj/debug-shared/mars.o Binary files differnew file mode 100644 index 0000000..a4476ad --- /dev/null +++ b/botan/src/.obj/debug-shared/mars.o diff --git a/botan/src/.obj/debug-shared/mars_tab.o b/botan/src/.obj/debug-shared/mars_tab.o Binary files differnew file mode 100644 index 0000000..5f99ffd --- /dev/null +++ b/botan/src/.obj/debug-shared/mars_tab.o diff --git a/botan/src/.obj/debug-shared/md2.o b/botan/src/.obj/debug-shared/md2.o Binary files differnew file mode 100644 index 0000000..bd07ed8 --- /dev/null +++ b/botan/src/.obj/debug-shared/md2.o diff --git a/botan/src/.obj/debug-shared/md4.o b/botan/src/.obj/debug-shared/md4.o Binary files differnew file mode 100644 index 0000000..4aabb2f --- /dev/null +++ b/botan/src/.obj/debug-shared/md4.o diff --git a/botan/src/.obj/debug-shared/md5.o b/botan/src/.obj/debug-shared/md5.o Binary files differnew file mode 100644 index 0000000..7ebc711 --- /dev/null +++ b/botan/src/.obj/debug-shared/md5.o diff --git a/botan/src/.obj/debug-shared/mdx_hash.o b/botan/src/.obj/debug-shared/mdx_hash.o Binary files differnew file mode 100644 index 0000000..02de4d6 --- /dev/null +++ b/botan/src/.obj/debug-shared/mdx_hash.o diff --git a/botan/src/.obj/debug-shared/mem_pool.o b/botan/src/.obj/debug-shared/mem_pool.o Binary files differnew file mode 100644 index 0000000..18fa1fc --- /dev/null +++ b/botan/src/.obj/debug-shared/mem_pool.o diff --git a/botan/src/.obj/debug-shared/mgf1.o b/botan/src/.obj/debug-shared/mgf1.o Binary files differnew file mode 100644 index 0000000..c579ec4 --- /dev/null +++ b/botan/src/.obj/debug-shared/mgf1.o diff --git a/botan/src/.obj/debug-shared/misty1.o b/botan/src/.obj/debug-shared/misty1.o Binary files differnew file mode 100644 index 0000000..a71176d --- /dev/null +++ b/botan/src/.obj/debug-shared/misty1.o diff --git a/botan/src/.obj/debug-shared/mlock.o b/botan/src/.obj/debug-shared/mlock.o Binary files differnew file mode 100644 index 0000000..27b6da6 --- /dev/null +++ b/botan/src/.obj/debug-shared/mlock.o diff --git a/botan/src/.obj/debug-shared/mmap_mem.o b/botan/src/.obj/debug-shared/mmap_mem.o Binary files differnew file mode 100644 index 0000000..a744071 --- /dev/null +++ b/botan/src/.obj/debug-shared/mmap_mem.o diff --git a/botan/src/.obj/debug-shared/mode_pad.o b/botan/src/.obj/debug-shared/mode_pad.o Binary files differnew file mode 100644 index 0000000..38091d8 --- /dev/null +++ b/botan/src/.obj/debug-shared/mode_pad.o diff --git a/botan/src/.obj/debug-shared/modebase.o b/botan/src/.obj/debug-shared/modebase.o Binary files differnew file mode 100644 index 0000000..973dd4d --- /dev/null +++ b/botan/src/.obj/debug-shared/modebase.o diff --git a/botan/src/.obj/debug-shared/mp_asm.o b/botan/src/.obj/debug-shared/mp_asm.o Binary files differnew file mode 100644 index 0000000..9c6ce85 --- /dev/null +++ b/botan/src/.obj/debug-shared/mp_asm.o diff --git a/botan/src/.obj/debug-shared/mp_comba.o b/botan/src/.obj/debug-shared/mp_comba.o Binary files differnew file mode 100644 index 0000000..76ae756 --- /dev/null +++ b/botan/src/.obj/debug-shared/mp_comba.o diff --git a/botan/src/.obj/debug-shared/mp_karat.o b/botan/src/.obj/debug-shared/mp_karat.o Binary files differnew file mode 100644 index 0000000..fc71d64 --- /dev/null +++ b/botan/src/.obj/debug-shared/mp_karat.o diff --git a/botan/src/.obj/debug-shared/mp_misc.o b/botan/src/.obj/debug-shared/mp_misc.o Binary files differnew file mode 100644 index 0000000..788e48c --- /dev/null +++ b/botan/src/.obj/debug-shared/mp_misc.o diff --git a/botan/src/.obj/debug-shared/mp_monty.o b/botan/src/.obj/debug-shared/mp_monty.o Binary files differnew file mode 100644 index 0000000..7296273 --- /dev/null +++ b/botan/src/.obj/debug-shared/mp_monty.o diff --git a/botan/src/.obj/debug-shared/mp_mulop.o b/botan/src/.obj/debug-shared/mp_mulop.o Binary files differnew file mode 100644 index 0000000..4388496 --- /dev/null +++ b/botan/src/.obj/debug-shared/mp_mulop.o diff --git a/botan/src/.obj/debug-shared/mp_numth.o b/botan/src/.obj/debug-shared/mp_numth.o Binary files differnew file mode 100644 index 0000000..5c68bbe --- /dev/null +++ b/botan/src/.obj/debug-shared/mp_numth.o diff --git a/botan/src/.obj/debug-shared/mp_shift.o b/botan/src/.obj/debug-shared/mp_shift.o Binary files differnew file mode 100644 index 0000000..21fac65 --- /dev/null +++ b/botan/src/.obj/debug-shared/mp_shift.o diff --git a/botan/src/.obj/debug-shared/mux_noop.o b/botan/src/.obj/debug-shared/mux_noop.o Binary files differnew file mode 100644 index 0000000..fee1e58 --- /dev/null +++ b/botan/src/.obj/debug-shared/mux_noop.o diff --git a/botan/src/.obj/debug-shared/mux_pthr.o b/botan/src/.obj/debug-shared/mux_pthr.o Binary files differnew file mode 100644 index 0000000..162ea21 --- /dev/null +++ b/botan/src/.obj/debug-shared/mux_pthr.o diff --git a/botan/src/.obj/debug-shared/noekeon.o b/botan/src/.obj/debug-shared/noekeon.o Binary files differnew file mode 100644 index 0000000..c1dda71 --- /dev/null +++ b/botan/src/.obj/debug-shared/noekeon.o diff --git a/botan/src/.obj/debug-shared/nr.o b/botan/src/.obj/debug-shared/nr.o Binary files differnew file mode 100644 index 0000000..491e670 --- /dev/null +++ b/botan/src/.obj/debug-shared/nr.o diff --git a/botan/src/.obj/debug-shared/nr_core.o b/botan/src/.obj/debug-shared/nr_core.o Binary files differnew file mode 100644 index 0000000..9dca32d --- /dev/null +++ b/botan/src/.obj/debug-shared/nr_core.o diff --git a/botan/src/.obj/debug-shared/nr_op.o b/botan/src/.obj/debug-shared/nr_op.o Binary files differnew file mode 100644 index 0000000..55f362f --- /dev/null +++ b/botan/src/.obj/debug-shared/nr_op.o diff --git a/botan/src/.obj/debug-shared/numthry.o b/botan/src/.obj/debug-shared/numthry.o Binary files differnew file mode 100644 index 0000000..becfa23 --- /dev/null +++ b/botan/src/.obj/debug-shared/numthry.o diff --git a/botan/src/.obj/debug-shared/ofb.o b/botan/src/.obj/debug-shared/ofb.o Binary files differnew file mode 100644 index 0000000..aa886b1 --- /dev/null +++ b/botan/src/.obj/debug-shared/ofb.o diff --git a/botan/src/.obj/debug-shared/oids.o b/botan/src/.obj/debug-shared/oids.o Binary files differnew file mode 100644 index 0000000..1019b4e --- /dev/null +++ b/botan/src/.obj/debug-shared/oids.o diff --git a/botan/src/.obj/debug-shared/openpgp.o b/botan/src/.obj/debug-shared/openpgp.o Binary files differnew file mode 100644 index 0000000..1a9a382 --- /dev/null +++ b/botan/src/.obj/debug-shared/openpgp.o diff --git a/botan/src/.obj/debug-shared/out_buf.o b/botan/src/.obj/debug-shared/out_buf.o Binary files differnew file mode 100644 index 0000000..dbd7295 --- /dev/null +++ b/botan/src/.obj/debug-shared/out_buf.o diff --git a/botan/src/.obj/debug-shared/par_hash.o b/botan/src/.obj/debug-shared/par_hash.o Binary files differnew file mode 100644 index 0000000..93d393e --- /dev/null +++ b/botan/src/.obj/debug-shared/par_hash.o diff --git a/botan/src/.obj/debug-shared/parsing.o b/botan/src/.obj/debug-shared/parsing.o Binary files differnew file mode 100644 index 0000000..9e57586 --- /dev/null +++ b/botan/src/.obj/debug-shared/parsing.o diff --git a/botan/src/.obj/debug-shared/pbes1.o b/botan/src/.obj/debug-shared/pbes1.o Binary files differnew file mode 100644 index 0000000..66bf321 --- /dev/null +++ b/botan/src/.obj/debug-shared/pbes1.o diff --git a/botan/src/.obj/debug-shared/pbes2.o b/botan/src/.obj/debug-shared/pbes2.o Binary files differnew file mode 100644 index 0000000..a0ea9d4 --- /dev/null +++ b/botan/src/.obj/debug-shared/pbes2.o diff --git a/botan/src/.obj/debug-shared/pbkdf1.o b/botan/src/.obj/debug-shared/pbkdf1.o Binary files differnew file mode 100644 index 0000000..d934491 --- /dev/null +++ b/botan/src/.obj/debug-shared/pbkdf1.o diff --git a/botan/src/.obj/debug-shared/pbkdf2.o b/botan/src/.obj/debug-shared/pbkdf2.o Binary files differnew file mode 100644 index 0000000..dad5d1c --- /dev/null +++ b/botan/src/.obj/debug-shared/pbkdf2.o diff --git a/botan/src/.obj/debug-shared/pem.o b/botan/src/.obj/debug-shared/pem.o Binary files differnew file mode 100644 index 0000000..e9a71bb --- /dev/null +++ b/botan/src/.obj/debug-shared/pem.o diff --git a/botan/src/.obj/debug-shared/pgp_s2k.o b/botan/src/.obj/debug-shared/pgp_s2k.o Binary files differnew file mode 100644 index 0000000..1d6fd25 --- /dev/null +++ b/botan/src/.obj/debug-shared/pgp_s2k.o diff --git a/botan/src/.obj/debug-shared/pipe.o b/botan/src/.obj/debug-shared/pipe.o Binary files differnew file mode 100644 index 0000000..dbe3773 --- /dev/null +++ b/botan/src/.obj/debug-shared/pipe.o diff --git a/botan/src/.obj/debug-shared/pipe_io.o b/botan/src/.obj/debug-shared/pipe_io.o Binary files differnew file mode 100644 index 0000000..66ce0ed --- /dev/null +++ b/botan/src/.obj/debug-shared/pipe_io.o diff --git a/botan/src/.obj/debug-shared/pipe_rw.o b/botan/src/.obj/debug-shared/pipe_rw.o Binary files differnew file mode 100644 index 0000000..c6054bd --- /dev/null +++ b/botan/src/.obj/debug-shared/pipe_rw.o diff --git a/botan/src/.obj/debug-shared/pk_algs.o b/botan/src/.obj/debug-shared/pk_algs.o Binary files differnew file mode 100644 index 0000000..d23f19a --- /dev/null +++ b/botan/src/.obj/debug-shared/pk_algs.o diff --git a/botan/src/.obj/debug-shared/pk_engine.o b/botan/src/.obj/debug-shared/pk_engine.o Binary files differnew file mode 100644 index 0000000..04c9c7f --- /dev/null +++ b/botan/src/.obj/debug-shared/pk_engine.o diff --git a/botan/src/.obj/debug-shared/pk_filts.o b/botan/src/.obj/debug-shared/pk_filts.o Binary files differnew file mode 100644 index 0000000..4dffa99 --- /dev/null +++ b/botan/src/.obj/debug-shared/pk_filts.o diff --git a/botan/src/.obj/debug-shared/pk_keys.o b/botan/src/.obj/debug-shared/pk_keys.o Binary files differnew file mode 100644 index 0000000..f233bea --- /dev/null +++ b/botan/src/.obj/debug-shared/pk_keys.o diff --git a/botan/src/.obj/debug-shared/pkcs10.o b/botan/src/.obj/debug-shared/pkcs10.o Binary files differnew file mode 100644 index 0000000..9931ded --- /dev/null +++ b/botan/src/.obj/debug-shared/pkcs10.o diff --git a/botan/src/.obj/debug-shared/pkcs8.o b/botan/src/.obj/debug-shared/pkcs8.o Binary files differnew file mode 100644 index 0000000..ad809ba --- /dev/null +++ b/botan/src/.obj/debug-shared/pkcs8.o diff --git a/botan/src/.obj/debug-shared/point_gfp.o b/botan/src/.obj/debug-shared/point_gfp.o Binary files differnew file mode 100644 index 0000000..1a00e63 --- /dev/null +++ b/botan/src/.obj/debug-shared/point_gfp.o diff --git a/botan/src/.obj/debug-shared/policy.o b/botan/src/.obj/debug-shared/policy.o Binary files differnew file mode 100644 index 0000000..a51aa51 --- /dev/null +++ b/botan/src/.obj/debug-shared/policy.o diff --git a/botan/src/.obj/debug-shared/pow_mod.o b/botan/src/.obj/debug-shared/pow_mod.o Binary files differnew file mode 100644 index 0000000..6d25d79 --- /dev/null +++ b/botan/src/.obj/debug-shared/pow_mod.o diff --git a/botan/src/.obj/debug-shared/powm_fw.o b/botan/src/.obj/debug-shared/powm_fw.o Binary files differnew file mode 100644 index 0000000..991cf15 --- /dev/null +++ b/botan/src/.obj/debug-shared/powm_fw.o diff --git a/botan/src/.obj/debug-shared/powm_mnt.o b/botan/src/.obj/debug-shared/powm_mnt.o Binary files differnew file mode 100644 index 0000000..489c275 --- /dev/null +++ b/botan/src/.obj/debug-shared/powm_mnt.o diff --git a/botan/src/.obj/debug-shared/prf_ssl3.o b/botan/src/.obj/debug-shared/prf_ssl3.o Binary files differnew file mode 100644 index 0000000..9d2c135 --- /dev/null +++ b/botan/src/.obj/debug-shared/prf_ssl3.o diff --git a/botan/src/.obj/debug-shared/prf_tls.o b/botan/src/.obj/debug-shared/prf_tls.o Binary files differnew file mode 100644 index 0000000..4dcc60c --- /dev/null +++ b/botan/src/.obj/debug-shared/prf_tls.o diff --git a/botan/src/.obj/debug-shared/prf_x942.o b/botan/src/.obj/debug-shared/prf_x942.o Binary files differnew file mode 100644 index 0000000..bd59e3a --- /dev/null +++ b/botan/src/.obj/debug-shared/prf_x942.o diff --git a/botan/src/.obj/debug-shared/primes.o b/botan/src/.obj/debug-shared/primes.o Binary files differnew file mode 100644 index 0000000..3f310dc --- /dev/null +++ b/botan/src/.obj/debug-shared/primes.o diff --git a/botan/src/.obj/debug-shared/prov_weight.o b/botan/src/.obj/debug-shared/prov_weight.o Binary files differnew file mode 100644 index 0000000..e135fb4 --- /dev/null +++ b/botan/src/.obj/debug-shared/prov_weight.o diff --git a/botan/src/.obj/debug-shared/pubkey.o b/botan/src/.obj/debug-shared/pubkey.o Binary files differnew file mode 100644 index 0000000..51df14e --- /dev/null +++ b/botan/src/.obj/debug-shared/pubkey.o diff --git a/botan/src/.obj/debug-shared/pubkey_enums.o b/botan/src/.obj/debug-shared/pubkey_enums.o Binary files differnew file mode 100644 index 0000000..d336137 --- /dev/null +++ b/botan/src/.obj/debug-shared/pubkey_enums.o diff --git a/botan/src/.obj/debug-shared/randpool.o b/botan/src/.obj/debug-shared/randpool.o Binary files differnew file mode 100644 index 0000000..5c69d2b --- /dev/null +++ b/botan/src/.obj/debug-shared/randpool.o diff --git a/botan/src/.obj/debug-shared/rc2.o b/botan/src/.obj/debug-shared/rc2.o Binary files differnew file mode 100644 index 0000000..b75d11b --- /dev/null +++ b/botan/src/.obj/debug-shared/rc2.o diff --git a/botan/src/.obj/debug-shared/rc5.o b/botan/src/.obj/debug-shared/rc5.o Binary files differnew file mode 100644 index 0000000..0d93650 --- /dev/null +++ b/botan/src/.obj/debug-shared/rc5.o diff --git a/botan/src/.obj/debug-shared/rc6.o b/botan/src/.obj/debug-shared/rc6.o Binary files differnew file mode 100644 index 0000000..1dd3ec3 --- /dev/null +++ b/botan/src/.obj/debug-shared/rc6.o diff --git a/botan/src/.obj/debug-shared/reducer.o b/botan/src/.obj/debug-shared/reducer.o Binary files differnew file mode 100644 index 0000000..a9df142 --- /dev/null +++ b/botan/src/.obj/debug-shared/reducer.o diff --git a/botan/src/.obj/debug-shared/ressol.o b/botan/src/.obj/debug-shared/ressol.o Binary files differnew file mode 100644 index 0000000..cf24625 --- /dev/null +++ b/botan/src/.obj/debug-shared/ressol.o diff --git a/botan/src/.obj/debug-shared/rmd128.o b/botan/src/.obj/debug-shared/rmd128.o Binary files differnew file mode 100644 index 0000000..04202c3 --- /dev/null +++ b/botan/src/.obj/debug-shared/rmd128.o diff --git a/botan/src/.obj/debug-shared/rmd160.o b/botan/src/.obj/debug-shared/rmd160.o Binary files differnew file mode 100644 index 0000000..08177d6 --- /dev/null +++ b/botan/src/.obj/debug-shared/rmd160.o diff --git a/botan/src/.obj/debug-shared/rng.o b/botan/src/.obj/debug-shared/rng.o Binary files differnew file mode 100644 index 0000000..9a25c34 --- /dev/null +++ b/botan/src/.obj/debug-shared/rng.o diff --git a/botan/src/.obj/debug-shared/rsa.o b/botan/src/.obj/debug-shared/rsa.o Binary files differnew file mode 100644 index 0000000..06338b0 --- /dev/null +++ b/botan/src/.obj/debug-shared/rsa.o diff --git a/botan/src/.obj/debug-shared/rw.o b/botan/src/.obj/debug-shared/rw.o Binary files differnew file mode 100644 index 0000000..d5efc95 --- /dev/null +++ b/botan/src/.obj/debug-shared/rw.o diff --git a/botan/src/.obj/debug-shared/s2k.o b/botan/src/.obj/debug-shared/s2k.o Binary files differnew file mode 100644 index 0000000..02210e4 --- /dev/null +++ b/botan/src/.obj/debug-shared/s2k.o diff --git a/botan/src/.obj/debug-shared/safe_tab.o b/botan/src/.obj/debug-shared/safe_tab.o Binary files differnew file mode 100644 index 0000000..f122bb1 --- /dev/null +++ b/botan/src/.obj/debug-shared/safe_tab.o diff --git a/botan/src/.obj/debug-shared/safer_sk.o b/botan/src/.obj/debug-shared/safer_sk.o Binary files differnew file mode 100644 index 0000000..b4b88ed --- /dev/null +++ b/botan/src/.obj/debug-shared/safer_sk.o diff --git a/botan/src/.obj/debug-shared/salsa20.o b/botan/src/.obj/debug-shared/salsa20.o Binary files differnew file mode 100644 index 0000000..40a27e9 --- /dev/null +++ b/botan/src/.obj/debug-shared/salsa20.o diff --git a/botan/src/.obj/debug-shared/scan_name.o b/botan/src/.obj/debug-shared/scan_name.o Binary files differnew file mode 100644 index 0000000..699665e --- /dev/null +++ b/botan/src/.obj/debug-shared/scan_name.o diff --git a/botan/src/.obj/debug-shared/secqueue.o b/botan/src/.obj/debug-shared/secqueue.o Binary files differnew file mode 100644 index 0000000..c518dd7 --- /dev/null +++ b/botan/src/.obj/debug-shared/secqueue.o diff --git a/botan/src/.obj/debug-shared/seed.o b/botan/src/.obj/debug-shared/seed.o Binary files differnew file mode 100644 index 0000000..5266fbf --- /dev/null +++ b/botan/src/.obj/debug-shared/seed.o diff --git a/botan/src/.obj/debug-shared/seed_tab.o b/botan/src/.obj/debug-shared/seed_tab.o Binary files differnew file mode 100644 index 0000000..764ca30 --- /dev/null +++ b/botan/src/.obj/debug-shared/seed_tab.o diff --git a/botan/src/.obj/debug-shared/selftest.o b/botan/src/.obj/debug-shared/selftest.o Binary files differnew file mode 100644 index 0000000..c252ac0 --- /dev/null +++ b/botan/src/.obj/debug-shared/selftest.o diff --git a/botan/src/.obj/debug-shared/serpent.o b/botan/src/.obj/debug-shared/serpent.o Binary files differnew file mode 100644 index 0000000..250e964 --- /dev/null +++ b/botan/src/.obj/debug-shared/serpent.o diff --git a/botan/src/.obj/debug-shared/sha160.o b/botan/src/.obj/debug-shared/sha160.o Binary files differnew file mode 100644 index 0000000..870628a --- /dev/null +++ b/botan/src/.obj/debug-shared/sha160.o diff --git a/botan/src/.obj/debug-shared/sha2_32.o b/botan/src/.obj/debug-shared/sha2_32.o Binary files differnew file mode 100644 index 0000000..4f9e464 --- /dev/null +++ b/botan/src/.obj/debug-shared/sha2_32.o diff --git a/botan/src/.obj/debug-shared/sha2_64.o b/botan/src/.obj/debug-shared/sha2_64.o Binary files differnew file mode 100644 index 0000000..32f5c04 --- /dev/null +++ b/botan/src/.obj/debug-shared/sha2_64.o diff --git a/botan/src/.obj/debug-shared/signed_obj.o b/botan/src/.obj/debug-shared/signed_obj.o Binary files differnew file mode 100644 index 0000000..bcfb319 --- /dev/null +++ b/botan/src/.obj/debug-shared/signed_obj.o diff --git a/botan/src/.obj/debug-shared/skein_512.o b/botan/src/.obj/debug-shared/skein_512.o Binary files differnew file mode 100644 index 0000000..90b88b0 --- /dev/null +++ b/botan/src/.obj/debug-shared/skein_512.o diff --git a/botan/src/.obj/debug-shared/skipjack.o b/botan/src/.obj/debug-shared/skipjack.o Binary files differnew file mode 100644 index 0000000..4d16ebe --- /dev/null +++ b/botan/src/.obj/debug-shared/skipjack.o diff --git a/botan/src/.obj/debug-shared/sqr_tab.o b/botan/src/.obj/debug-shared/sqr_tab.o Binary files differnew file mode 100644 index 0000000..1e9b6eb --- /dev/null +++ b/botan/src/.obj/debug-shared/sqr_tab.o diff --git a/botan/src/.obj/debug-shared/square.o b/botan/src/.obj/debug-shared/square.o Binary files differnew file mode 100644 index 0000000..3bab299 --- /dev/null +++ b/botan/src/.obj/debug-shared/square.o diff --git a/botan/src/.obj/debug-shared/ssl3_mac.o b/botan/src/.obj/debug-shared/ssl3_mac.o Binary files differnew file mode 100644 index 0000000..7ae2589 --- /dev/null +++ b/botan/src/.obj/debug-shared/ssl3_mac.o diff --git a/botan/src/.obj/debug-shared/stream_cipher.o b/botan/src/.obj/debug-shared/stream_cipher.o Binary files differnew file mode 100644 index 0000000..4f8afa2 --- /dev/null +++ b/botan/src/.obj/debug-shared/stream_cipher.o diff --git a/botan/src/.obj/debug-shared/symkey.o b/botan/src/.obj/debug-shared/symkey.o Binary files differnew file mode 100644 index 0000000..3a6acd9 --- /dev/null +++ b/botan/src/.obj/debug-shared/symkey.o diff --git a/botan/src/.obj/debug-shared/tea.o b/botan/src/.obj/debug-shared/tea.o Binary files differnew file mode 100644 index 0000000..752d211 --- /dev/null +++ b/botan/src/.obj/debug-shared/tea.o diff --git a/botan/src/.obj/debug-shared/tig_tab.o b/botan/src/.obj/debug-shared/tig_tab.o Binary files differnew file mode 100644 index 0000000..748071d --- /dev/null +++ b/botan/src/.obj/debug-shared/tig_tab.o diff --git a/botan/src/.obj/debug-shared/tiger.o b/botan/src/.obj/debug-shared/tiger.o Binary files differnew file mode 100644 index 0000000..a4a19f2 --- /dev/null +++ b/botan/src/.obj/debug-shared/tiger.o diff --git a/botan/src/.obj/debug-shared/timer.o b/botan/src/.obj/debug-shared/timer.o Binary files differnew file mode 100644 index 0000000..b4aa829 --- /dev/null +++ b/botan/src/.obj/debug-shared/timer.o diff --git a/botan/src/.obj/debug-shared/tm_unix.o b/botan/src/.obj/debug-shared/tm_unix.o Binary files differnew file mode 100644 index 0000000..dfd239d --- /dev/null +++ b/botan/src/.obj/debug-shared/tm_unix.o diff --git a/botan/src/.obj/debug-shared/tur_tab.o b/botan/src/.obj/debug-shared/tur_tab.o Binary files differnew file mode 100644 index 0000000..033f32b --- /dev/null +++ b/botan/src/.obj/debug-shared/tur_tab.o diff --git a/botan/src/.obj/debug-shared/turing.o b/botan/src/.obj/debug-shared/turing.o Binary files differnew file mode 100644 index 0000000..4468608 --- /dev/null +++ b/botan/src/.obj/debug-shared/turing.o diff --git a/botan/src/.obj/debug-shared/two_tab.o b/botan/src/.obj/debug-shared/two_tab.o Binary files differnew file mode 100644 index 0000000..1cf9cd2 --- /dev/null +++ b/botan/src/.obj/debug-shared/two_tab.o diff --git a/botan/src/.obj/debug-shared/twofish.o b/botan/src/.obj/debug-shared/twofish.o Binary files differnew file mode 100644 index 0000000..ff643aa --- /dev/null +++ b/botan/src/.obj/debug-shared/twofish.o diff --git a/botan/src/.obj/debug-shared/ui.o b/botan/src/.obj/debug-shared/ui.o Binary files differnew file mode 100644 index 0000000..cb54205 --- /dev/null +++ b/botan/src/.obj/debug-shared/ui.o diff --git a/botan/src/.obj/debug-shared/unix_cmd.o b/botan/src/.obj/debug-shared/unix_cmd.o Binary files differnew file mode 100644 index 0000000..ae0927b --- /dev/null +++ b/botan/src/.obj/debug-shared/unix_cmd.o diff --git a/botan/src/.obj/debug-shared/unix_src.o b/botan/src/.obj/debug-shared/unix_src.o Binary files differnew file mode 100644 index 0000000..75bd018 --- /dev/null +++ b/botan/src/.obj/debug-shared/unix_src.o diff --git a/botan/src/.obj/debug-shared/util.o b/botan/src/.obj/debug-shared/util.o Binary files differnew file mode 100644 index 0000000..7a0955c --- /dev/null +++ b/botan/src/.obj/debug-shared/util.o diff --git a/botan/src/.obj/debug-shared/version.o b/botan/src/.obj/debug-shared/version.o Binary files differnew file mode 100644 index 0000000..c6d8ded --- /dev/null +++ b/botan/src/.obj/debug-shared/version.o diff --git a/botan/src/.obj/debug-shared/whrl_tab.o b/botan/src/.obj/debug-shared/whrl_tab.o Binary files differnew file mode 100644 index 0000000..5cf5b7a --- /dev/null +++ b/botan/src/.obj/debug-shared/whrl_tab.o diff --git a/botan/src/.obj/debug-shared/whrlpool.o b/botan/src/.obj/debug-shared/whrlpool.o Binary files differnew file mode 100644 index 0000000..5b316d4 --- /dev/null +++ b/botan/src/.obj/debug-shared/whrlpool.o diff --git a/botan/src/.obj/debug-shared/wid_wake.o b/botan/src/.obj/debug-shared/wid_wake.o Binary files differnew file mode 100644 index 0000000..d9a423d --- /dev/null +++ b/botan/src/.obj/debug-shared/wid_wake.o diff --git a/botan/src/.obj/debug-shared/x509_ca.o b/botan/src/.obj/debug-shared/x509_ca.o Binary files differnew file mode 100644 index 0000000..b7268f8 --- /dev/null +++ b/botan/src/.obj/debug-shared/x509_ca.o diff --git a/botan/src/.obj/debug-shared/x509_crl.o b/botan/src/.obj/debug-shared/x509_crl.o Binary files differnew file mode 100644 index 0000000..222eb60 --- /dev/null +++ b/botan/src/.obj/debug-shared/x509_crl.o diff --git a/botan/src/.obj/debug-shared/x509_ext.o b/botan/src/.obj/debug-shared/x509_ext.o Binary files differnew file mode 100644 index 0000000..921e4bf --- /dev/null +++ b/botan/src/.obj/debug-shared/x509_ext.o diff --git a/botan/src/.obj/debug-shared/x509_key.o b/botan/src/.obj/debug-shared/x509_key.o Binary files differnew file mode 100644 index 0000000..03e6326 --- /dev/null +++ b/botan/src/.obj/debug-shared/x509_key.o diff --git a/botan/src/.obj/debug-shared/x509_obj.o b/botan/src/.obj/debug-shared/x509_obj.o Binary files differnew file mode 100644 index 0000000..1a0964a --- /dev/null +++ b/botan/src/.obj/debug-shared/x509_obj.o diff --git a/botan/src/.obj/debug-shared/x509cert.o b/botan/src/.obj/debug-shared/x509cert.o Binary files differnew file mode 100644 index 0000000..923ea9f --- /dev/null +++ b/botan/src/.obj/debug-shared/x509cert.o diff --git a/botan/src/.obj/debug-shared/x509find.o b/botan/src/.obj/debug-shared/x509find.o Binary files differnew file mode 100644 index 0000000..6b4341f --- /dev/null +++ b/botan/src/.obj/debug-shared/x509find.o diff --git a/botan/src/.obj/debug-shared/x509opt.o b/botan/src/.obj/debug-shared/x509opt.o Binary files differnew file mode 100644 index 0000000..257ce1e --- /dev/null +++ b/botan/src/.obj/debug-shared/x509opt.o diff --git a/botan/src/.obj/debug-shared/x509self.o b/botan/src/.obj/debug-shared/x509self.o Binary files differnew file mode 100644 index 0000000..fadd580 --- /dev/null +++ b/botan/src/.obj/debug-shared/x509self.o diff --git a/botan/src/.obj/debug-shared/x509stor.o b/botan/src/.obj/debug-shared/x509stor.o Binary files differnew file mode 100644 index 0000000..742f590 --- /dev/null +++ b/botan/src/.obj/debug-shared/x509stor.o diff --git a/botan/src/.obj/debug-shared/x919_mac.o b/botan/src/.obj/debug-shared/x919_mac.o Binary files differnew file mode 100644 index 0000000..6b73d45 --- /dev/null +++ b/botan/src/.obj/debug-shared/x919_mac.o diff --git a/botan/src/.obj/debug-shared/x931_rng.o b/botan/src/.obj/debug-shared/x931_rng.o Binary files differnew file mode 100644 index 0000000..8d22e04 --- /dev/null +++ b/botan/src/.obj/debug-shared/x931_rng.o diff --git a/botan/src/.obj/debug-shared/xtea.o b/botan/src/.obj/debug-shared/xtea.o Binary files differnew file mode 100644 index 0000000..487a3aa --- /dev/null +++ b/botan/src/.obj/debug-shared/xtea.o diff --git a/botan/src/.obj/debug-shared/xts.o b/botan/src/.obj/debug-shared/xts.o Binary files differnew file mode 100644 index 0000000..703aaaf --- /dev/null +++ b/botan/src/.obj/debug-shared/xts.o diff --git a/botan/src/Makefile b/botan/src/Makefile new file mode 100644 index 0000000..648b30a --- /dev/null +++ b/botan/src/Makefile @@ -0,0 +1,1628 @@ +############################################################################# +# Makefile for building: libBotan_debug.1.0.0.dylib +# Generated by qmake (2.01a) (Qt 4.7.0) on: Sat Jun 26 13:13:55 2010 +# Project: src.pro +# Template: lib +# Command: /opt/Qt/4.7/bin/qmake -spec /opt/Qt/4.7/mkspecs/macx-g++ -o Makefile src.pro +############################################################################# + +####### Compiler, tools and options + +CC = gcc +CXX = g++ +DEFINES = -DIDE_LIBRARY_BASENAME=\"lib\" -DWITH_TESTS -DQT_NO_CAST_TO_ASCII -DQT_TESTLIB_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED +CFLAGS = -pipe -fPIC -ansi -fpermissive -finline-functions -Wno-long-long -g -gdwarf-2 -arch x86_64 -Xarch_x86_64 -mmacosx-version-min=10.5 -fvisibility=hidden -Wall -W -fPIC $(DEFINES) +CXXFLAGS = -pipe -fPIC -ansi -fpermissive -finline-functions -Wno-long-long -g -gdwarf-2 -arch x86_64 -Xarch_x86_64 -mmacosx-version-min=10.5 -fvisibility=hidden -Wall -W -fPIC $(DEFINES) +INCPATH = -I/opt/Qt/4.7/mkspecs/macx-g++ -I. -I/opt/Qt/4.7/lib/QtCore.framework/Versions/4/Headers -I/opt/Qt/4.7/include/QtCore -I/opt/Qt/4.7/lib/QtGui.framework/Versions/4/Headers -I/opt/Qt/4.7/include/QtGui -I/opt/Qt/4.7/lib/QtTest.framework/Versions/4/Headers -I/opt/Qt/4.7/include/QtTest -I/opt/Qt/4.7/include -I../../../../libs -I/Users/ebaak/depot/creator-qtest/tools -I. -I../build -I../build/botan -I.moc/debug-shared -F/opt/Qt/4.7/lib +LINK = g++ +LFLAGS = -headerpad_max_install_names -arch x86_64 -Xarch_x86_64 -mmacosx-version-min=10.5 -single_module -dynamiclib -compatibility_version 1.0 -current_version 1.0.0 -Wl,-install_name,@executable_path/../PlugIns/libBotan_debug.1.dylib +LIBS = $(SUBLIBS) -F/opt/Qt/4.7/lib -L/opt/Qt/4.7/lib -L/Users/ebaak/depot/creator-qtest/bin/Qt\ Creator.app/Contents/PlugIns -framework QtTest -L/opt/Qt/4.7/lib -F/opt/Qt/4.7/lib -framework IOKit -framework ApplicationServices -framework Security -framework QtGui -framework QtCore +AR = ar cq +RANLIB = ranlib -s +QMAKE = /opt/Qt/4.7/bin/qmake +TAR = tar -cf +COMPRESS = gzip -9f +COPY = cp -f +SED = sed +COPY_FILE = cp -f +COPY_DIR = cp -f -R +STRIP = +INSTALL_FILE = $(COPY_FILE) +INSTALL_DIR = $(COPY_DIR) +INSTALL_PROGRAM = $(COPY_FILE) +DEL_FILE = rm -f +SYMLINK = ln -f -s +DEL_DIR = rmdir +MOVE = mv -f +CHK_DIR_EXISTS= test -d +MKDIR = mkdir -p +export MACOSX_DEPLOYMENT_TARGET = 10.4 + +####### Output directory + +OBJECTS_DIR = .obj/debug-shared/ + +####### Files + +SOURCES = algo_factory/algo_factory.cpp \ + algo_factory/prov_weight.cpp \ + alloc/mem_pool/mem_pool.cpp \ + alloc/system_alloc/defalloc.cpp \ + asn1/alg_id.cpp \ + asn1/asn1_alt.cpp \ + asn1/asn1_att.cpp \ + asn1/asn1_dn.cpp \ + asn1/asn1_int.cpp \ + asn1/asn1_oid.cpp \ + asn1/asn1_str.cpp \ + asn1/asn1_tm.cpp \ + asn1/ber_dec.cpp \ + asn1/der_enc.cpp \ + benchmark/benchmark.cpp \ + block/aes/aes.cpp \ + block/aes/aes_tab.cpp \ + block/blowfish/blfs_tab.cpp \ + block/blowfish/blowfish.cpp \ + block/cast/cast128.cpp \ + block/cast/cast256.cpp \ + block/cast/cast_tab.cpp \ + block/des/des.cpp \ + block/des/des_tab.cpp \ + block/des/desx.cpp \ + block/gost_28147/gost_28147.cpp \ + block/idea/idea.cpp \ + block/kasumi/kasumi.cpp \ + block/lion/lion.cpp \ + block/lubyrack/lubyrack.cpp \ + block/mars/mars.cpp \ + block/mars/mars_tab.cpp \ + block/misty1/misty1.cpp \ + block/noekeon/noekeon.cpp \ + block/rc2/rc2.cpp \ + block/rc5/rc5.cpp \ + block/rc6/rc6.cpp \ + block/safer/safe_tab.cpp \ + block/safer/safer_sk.cpp \ + block/seed/seed.cpp \ + block/seed/seed_tab.cpp \ + block/serpent/serpent.cpp \ + block/skipjack/skipjack.cpp \ + block/square/sqr_tab.cpp \ + block/square/square.cpp \ + block/tea/tea.cpp \ + block/twofish/two_tab.cpp \ + block/twofish/twofish.cpp \ + block/xtea/xtea.cpp \ + cert/x509/certstor.cpp \ + cert/x509/crl_ent.cpp \ + cert/x509/pkcs10.cpp \ + cert/x509/x509_ca.cpp \ + cert/x509/x509_crl.cpp \ + cert/x509/x509_ext.cpp \ + cert/x509/x509_obj.cpp \ + cert/x509/x509cert.cpp \ + cert/x509/x509find.cpp \ + cert/x509/x509opt.cpp \ + cert/x509/x509self.cpp \ + cert/x509/x509stor.cpp \ + checksum/adler32/adler32.cpp \ + checksum/crc24/crc24.cpp \ + checksum/crc32/crc32.cpp \ + cms/cms_algo.cpp \ + cms/cms_comp.cpp \ + cms/cms_dalg.cpp \ + cms/cms_dec.cpp \ + cms/cms_ealg.cpp \ + cms/cms_enc.cpp \ + codec/base64/b64_char.cpp \ + codec/base64/base64.cpp \ + codec/hex/hex.cpp \ + codec/hex/hex_char.cpp \ + codec/openpgp/openpgp.cpp \ + codec/pem/pem.cpp \ + cryptobox/cryptobox.cpp \ + engine/def_engine/def_mode.cpp \ + engine/def_engine/def_pk_ops.cpp \ + engine/def_engine/def_powm.cpp \ + engine/def_engine/lookup_block.cpp \ + engine/def_engine/lookup_hash.cpp \ + engine/def_engine/lookup_mac.cpp \ + engine/def_engine/lookup_stream.cpp \ + filters/algo_filt.cpp \ + filters/basefilt.cpp \ + filters/buf_filt.cpp \ + filters/data_snk.cpp \ + filters/data_src.cpp \ + filters/filter.cpp \ + filters/out_buf.cpp \ + filters/pipe.cpp \ + filters/pipe_io.cpp \ + filters/pipe_rw.cpp \ + filters/secqueue.cpp \ + hash/fork256/fork256.cpp \ + hash/gost_3411/gost_3411.cpp \ + hash/has160/has160.cpp \ + hash/md2/md2.cpp \ + hash/md4/md4.cpp \ + hash/md5/md5.cpp \ + hash/mdx_hash/mdx_hash.cpp \ + hash/par_hash/par_hash.cpp \ + hash/rmd128/rmd128.cpp \ + hash/rmd160/rmd160.cpp \ + hash/sha1/sha160.cpp \ + hash/sha2/sha2_32.cpp \ + hash/sha2/sha2_64.cpp \ + hash/skein/skein_512.cpp \ + hash/tiger/tig_tab.cpp \ + hash/tiger/tiger.cpp \ + hash/whirlpool/whrl_tab.cpp \ + hash/whirlpool/whrlpool.cpp \ + kdf/kdf.cpp \ + kdf/kdf1/kdf1.cpp \ + kdf/kdf2/kdf2.cpp \ + kdf/mgf1/mgf1.cpp \ + kdf/ssl_prf/prf_ssl3.cpp \ + kdf/tls_prf/prf_tls.cpp \ + kdf/x942_prf/prf_x942.cpp \ + libstate/get_enc.cpp \ + libstate/init.cpp \ + libstate/libstate.cpp \ + libstate/look_pk.cpp \ + libstate/lookup.cpp \ + libstate/oid_lookup/oids.cpp \ + libstate/pk_engine.cpp \ + libstate/policy.cpp \ + libstate/scan_name.cpp \ + mac/cbc_mac/cbc_mac.cpp \ + mac/cmac/cmac.cpp \ + mac/hmac/hmac.cpp \ + mac/mac.cpp \ + mac/ssl3mac/ssl3_mac.cpp \ + mac/x919_mac/x919_mac.cpp \ + math/bigint/big_code.cpp \ + math/bigint/big_io.cpp \ + math/bigint/big_ops2.cpp \ + math/bigint/big_ops3.cpp \ + math/bigint/big_rand.cpp \ + math/bigint/bigint.cpp \ + math/bigint/divide.cpp \ + math/bigint/monty_generic/mp_monty.cpp \ + math/bigint/mp_asm.cpp \ + math/bigint/mp_comba.cpp \ + math/bigint/mp_karat.cpp \ + math/bigint/mp_misc.cpp \ + math/bigint/mp_shift.cpp \ + math/bigint/mulop_generic/mp_mulop.cpp \ + math/numbertheory/blinding.cpp \ + math/numbertheory/dsa_gen.cpp \ + math/numbertheory/jacobi.cpp \ + math/numbertheory/make_prm.cpp \ + math/numbertheory/mp_numth.cpp \ + math/numbertheory/numthry.cpp \ + math/numbertheory/pow_mod.cpp \ + math/numbertheory/powm_fw.cpp \ + math/numbertheory/powm_mnt.cpp \ + math/numbertheory/primes.cpp \ + math/numbertheory/reducer.cpp \ + math/numbertheory/ressol.cpp \ + modes/cbc/cbc.cpp \ + modes/cfb/cfb.cpp \ + modes/ctr/ctr.cpp \ + modes/cts/cts.cpp \ + modes/eax/eax.cpp \ + modes/eax/eax_dec.cpp \ + modes/ecb/ecb.cpp \ + modes/mode_pad/mode_pad.cpp \ + modes/modebase.cpp \ + modes/ofb/ofb.cpp \ + modes/xts/xts.cpp \ + mutex/noop_mutex/mux_noop.cpp \ + pbe/get_pbe.cpp \ + pbe/pbes1/pbes1.cpp \ + pbe/pbes2/pbes2.cpp \ + pk_pad/eme.cpp \ + pk_pad/eme1/eme1.cpp \ + pk_pad/eme_pkcs/eme_pkcs.cpp \ + pk_pad/emsa1/emsa1.cpp \ + pk_pad/emsa1_bsi/emsa1_bsi.cpp \ + pk_pad/emsa2/emsa2.cpp \ + pk_pad/emsa3/emsa3.cpp \ + pk_pad/emsa4/emsa4.cpp \ + pk_pad/emsa_raw/emsa_raw.cpp \ + pk_pad/hash_id/hash_id.cpp \ + pubkey/dh/dh.cpp \ + pubkey/dh/dh_core.cpp \ + pubkey/dl_algo/dl_algo.cpp \ + pubkey/dl_group/dl_group.cpp \ + pubkey/dlies/dlies.cpp \ + pubkey/dsa/dsa.cpp \ + pubkey/dsa/dsa_core.cpp \ + pubkey/dsa/dsa_op.cpp \ + pubkey/elgamal/elg_core.cpp \ + pubkey/elgamal/elg_op.cpp \ + pubkey/elgamal/elgamal.cpp \ + pubkey/if_algo/if_algo.cpp \ + pubkey/if_algo/if_core.cpp \ + pubkey/if_algo/if_op.cpp \ + pubkey/keypair/keypair.cpp \ + pubkey/nr/nr.cpp \ + pubkey/nr/nr_core.cpp \ + pubkey/nr/nr_op.cpp \ + pubkey/pk_algs.cpp \ + pubkey/pk_codecs/pkcs8.cpp \ + pubkey/pk_codecs/x509_key.cpp \ + pubkey/pk_filts.cpp \ + pubkey/pk_keys.cpp \ + pubkey/pubkey.cpp \ + pubkey/pubkey_enums.cpp \ + pubkey/rsa/rsa.cpp \ + pubkey/rw/rw.cpp \ + rng/auto_rng/auto_rng.cpp \ + rng/hmac_rng/hmac_rng.cpp \ + rng/randpool/randpool.cpp \ + rng/rng.cpp \ + rng/x931_rng/x931_rng.cpp \ + s2k/pbkdf1/pbkdf1.cpp \ + s2k/pbkdf2/pbkdf2.cpp \ + s2k/pgps2k/pgp_s2k.cpp \ + s2k/s2k.cpp \ + selftest/selftest.cpp \ + stream/arc4/arc4.cpp \ + stream/salsa20/salsa20.cpp \ + stream/stream_cipher.cpp \ + stream/turing/tur_tab.cpp \ + stream/turing/turing.cpp \ + stream/wid_wake/wid_wake.cpp \ + sym_algo/symkey.cpp \ + timer/timer.cpp \ + utils/charset.cpp \ + utils/datastor/datastor.cpp \ + utils/exceptn.cpp \ + utils/mlock.cpp \ + utils/parsing.cpp \ + utils/ui.cpp \ + utils/util.cpp \ + utils/version.cpp \ + alloc/alloc_mmap/mmap_mem.cpp \ + cert/cvc/asn1_eac_str.cpp \ + cert/cvc/asn1_eac_tm.cpp \ + cert/cvc/cvc_ado.cpp \ + cert/cvc/cvc_ca.cpp \ + cert/cvc/cvc_cert.cpp \ + cert/cvc/cvc_req.cpp \ + cert/cvc/cvc_self.cpp \ + cert/cvc/ecdsa_sig.cpp \ + cert/cvc/signed_obj.cpp \ + entropy/dev_random/es_dev.cpp \ + entropy/egd/es_egd.cpp \ + entropy/proc_walk/es_ftw.cpp \ + entropy/unix_procs/es_unix.cpp \ + entropy/unix_procs/unix_cmd.cpp \ + entropy/unix_procs/unix_src.cpp \ + filters/fd_unix/fd_unix.cpp \ + math/gfpmath/curve_gfp.cpp \ + math/gfpmath/gfp_element.cpp \ + math/gfpmath/point_gfp.cpp \ + mutex/pthreads/mux_pthr.cpp \ + pubkey/ec_dompar/ec_dompar.cpp \ + pubkey/ecc_key/ecc_key.cpp \ + pubkey/ecdsa/ecdsa.cpp \ + pubkey/ecdsa/ecdsa_core.cpp \ + pubkey/ecdsa/ecdsa_op.cpp \ + pubkey/eckaeg/eckaeg.cpp \ + pubkey/eckaeg/eckaeg_core.cpp \ + pubkey/eckaeg/eckaeg_op.cpp \ + timer/gettimeofday/tm_unix.cpp +OBJECTS = .obj/debug-shared/algo_factory.o \ + .obj/debug-shared/prov_weight.o \ + .obj/debug-shared/mem_pool.o \ + .obj/debug-shared/defalloc.o \ + .obj/debug-shared/alg_id.o \ + .obj/debug-shared/asn1_alt.o \ + .obj/debug-shared/asn1_att.o \ + .obj/debug-shared/asn1_dn.o \ + .obj/debug-shared/asn1_int.o \ + .obj/debug-shared/asn1_oid.o \ + .obj/debug-shared/asn1_str.o \ + .obj/debug-shared/asn1_tm.o \ + .obj/debug-shared/ber_dec.o \ + .obj/debug-shared/der_enc.o \ + .obj/debug-shared/benchmark.o \ + .obj/debug-shared/aes.o \ + .obj/debug-shared/aes_tab.o \ + .obj/debug-shared/blfs_tab.o \ + .obj/debug-shared/blowfish.o \ + .obj/debug-shared/cast128.o \ + .obj/debug-shared/cast256.o \ + .obj/debug-shared/cast_tab.o \ + .obj/debug-shared/des.o \ + .obj/debug-shared/des_tab.o \ + .obj/debug-shared/desx.o \ + .obj/debug-shared/gost_28147.o \ + .obj/debug-shared/idea.o \ + .obj/debug-shared/kasumi.o \ + .obj/debug-shared/lion.o \ + .obj/debug-shared/lubyrack.o \ + .obj/debug-shared/mars.o \ + .obj/debug-shared/mars_tab.o \ + .obj/debug-shared/misty1.o \ + .obj/debug-shared/noekeon.o \ + .obj/debug-shared/rc2.o \ + .obj/debug-shared/rc5.o \ + .obj/debug-shared/rc6.o \ + .obj/debug-shared/safe_tab.o \ + .obj/debug-shared/safer_sk.o \ + .obj/debug-shared/seed.o \ + .obj/debug-shared/seed_tab.o \ + .obj/debug-shared/serpent.o \ + .obj/debug-shared/skipjack.o \ + .obj/debug-shared/sqr_tab.o \ + .obj/debug-shared/square.o \ + .obj/debug-shared/tea.o \ + .obj/debug-shared/two_tab.o \ + .obj/debug-shared/twofish.o \ + .obj/debug-shared/xtea.o \ + .obj/debug-shared/certstor.o \ + .obj/debug-shared/crl_ent.o \ + .obj/debug-shared/pkcs10.o \ + .obj/debug-shared/x509_ca.o \ + .obj/debug-shared/x509_crl.o \ + .obj/debug-shared/x509_ext.o \ + .obj/debug-shared/x509_obj.o \ + .obj/debug-shared/x509cert.o \ + .obj/debug-shared/x509find.o \ + .obj/debug-shared/x509opt.o \ + .obj/debug-shared/x509self.o \ + .obj/debug-shared/x509stor.o \ + .obj/debug-shared/adler32.o \ + .obj/debug-shared/crc24.o \ + .obj/debug-shared/crc32.o \ + .obj/debug-shared/cms_algo.o \ + .obj/debug-shared/cms_comp.o \ + .obj/debug-shared/cms_dalg.o \ + .obj/debug-shared/cms_dec.o \ + .obj/debug-shared/cms_ealg.o \ + .obj/debug-shared/cms_enc.o \ + .obj/debug-shared/b64_char.o \ + .obj/debug-shared/base64.o \ + .obj/debug-shared/hex.o \ + .obj/debug-shared/hex_char.o \ + .obj/debug-shared/openpgp.o \ + .obj/debug-shared/pem.o \ + .obj/debug-shared/cryptobox.o \ + .obj/debug-shared/def_mode.o \ + .obj/debug-shared/def_pk_ops.o \ + .obj/debug-shared/def_powm.o \ + .obj/debug-shared/lookup_block.o \ + .obj/debug-shared/lookup_hash.o \ + .obj/debug-shared/lookup_mac.o \ + .obj/debug-shared/lookup_stream.o \ + .obj/debug-shared/algo_filt.o \ + .obj/debug-shared/basefilt.o \ + .obj/debug-shared/buf_filt.o \ + .obj/debug-shared/data_snk.o \ + .obj/debug-shared/data_src.o \ + .obj/debug-shared/filter.o \ + .obj/debug-shared/out_buf.o \ + .obj/debug-shared/pipe.o \ + .obj/debug-shared/pipe_io.o \ + .obj/debug-shared/pipe_rw.o \ + .obj/debug-shared/secqueue.o \ + .obj/debug-shared/fork256.o \ + .obj/debug-shared/gost_3411.o \ + .obj/debug-shared/has160.o \ + .obj/debug-shared/md2.o \ + .obj/debug-shared/md4.o \ + .obj/debug-shared/md5.o \ + .obj/debug-shared/mdx_hash.o \ + .obj/debug-shared/par_hash.o \ + .obj/debug-shared/rmd128.o \ + .obj/debug-shared/rmd160.o \ + .obj/debug-shared/sha160.o \ + .obj/debug-shared/sha2_32.o \ + .obj/debug-shared/sha2_64.o \ + .obj/debug-shared/skein_512.o \ + .obj/debug-shared/tig_tab.o \ + .obj/debug-shared/tiger.o \ + .obj/debug-shared/whrl_tab.o \ + .obj/debug-shared/whrlpool.o \ + .obj/debug-shared/kdf.o \ + .obj/debug-shared/kdf1.o \ + .obj/debug-shared/kdf2.o \ + .obj/debug-shared/mgf1.o \ + .obj/debug-shared/prf_ssl3.o \ + .obj/debug-shared/prf_tls.o \ + .obj/debug-shared/prf_x942.o \ + .obj/debug-shared/get_enc.o \ + .obj/debug-shared/init.o \ + .obj/debug-shared/libstate.o \ + .obj/debug-shared/look_pk.o \ + .obj/debug-shared/lookup.o \ + .obj/debug-shared/oids.o \ + .obj/debug-shared/pk_engine.o \ + .obj/debug-shared/policy.o \ + .obj/debug-shared/scan_name.o \ + .obj/debug-shared/cbc_mac.o \ + .obj/debug-shared/cmac.o \ + .obj/debug-shared/hmac.o \ + .obj/debug-shared/mac.o \ + .obj/debug-shared/ssl3_mac.o \ + .obj/debug-shared/x919_mac.o \ + .obj/debug-shared/big_code.o \ + .obj/debug-shared/big_io.o \ + .obj/debug-shared/big_ops2.o \ + .obj/debug-shared/big_ops3.o \ + .obj/debug-shared/big_rand.o \ + .obj/debug-shared/bigint.o \ + .obj/debug-shared/divide.o \ + .obj/debug-shared/mp_monty.o \ + .obj/debug-shared/mp_asm.o \ + .obj/debug-shared/mp_comba.o \ + .obj/debug-shared/mp_karat.o \ + .obj/debug-shared/mp_misc.o \ + .obj/debug-shared/mp_shift.o \ + .obj/debug-shared/mp_mulop.o \ + .obj/debug-shared/blinding.o \ + .obj/debug-shared/dsa_gen.o \ + .obj/debug-shared/jacobi.o \ + .obj/debug-shared/make_prm.o \ + .obj/debug-shared/mp_numth.o \ + .obj/debug-shared/numthry.o \ + .obj/debug-shared/pow_mod.o \ + .obj/debug-shared/powm_fw.o \ + .obj/debug-shared/powm_mnt.o \ + .obj/debug-shared/primes.o \ + .obj/debug-shared/reducer.o \ + .obj/debug-shared/ressol.o \ + .obj/debug-shared/cbc.o \ + .obj/debug-shared/cfb.o \ + .obj/debug-shared/ctr.o \ + .obj/debug-shared/cts.o \ + .obj/debug-shared/eax.o \ + .obj/debug-shared/eax_dec.o \ + .obj/debug-shared/ecb.o \ + .obj/debug-shared/mode_pad.o \ + .obj/debug-shared/modebase.o \ + .obj/debug-shared/ofb.o \ + .obj/debug-shared/xts.o \ + .obj/debug-shared/mux_noop.o \ + .obj/debug-shared/get_pbe.o \ + .obj/debug-shared/pbes1.o \ + .obj/debug-shared/pbes2.o \ + .obj/debug-shared/eme.o \ + .obj/debug-shared/eme1.o \ + .obj/debug-shared/eme_pkcs.o \ + .obj/debug-shared/emsa1.o \ + .obj/debug-shared/emsa1_bsi.o \ + .obj/debug-shared/emsa2.o \ + .obj/debug-shared/emsa3.o \ + .obj/debug-shared/emsa4.o \ + .obj/debug-shared/emsa_raw.o \ + .obj/debug-shared/hash_id.o \ + .obj/debug-shared/dh.o \ + .obj/debug-shared/dh_core.o \ + .obj/debug-shared/dl_algo.o \ + .obj/debug-shared/dl_group.o \ + .obj/debug-shared/dlies.o \ + .obj/debug-shared/dsa.o \ + .obj/debug-shared/dsa_core.o \ + .obj/debug-shared/dsa_op.o \ + .obj/debug-shared/elg_core.o \ + .obj/debug-shared/elg_op.o \ + .obj/debug-shared/elgamal.o \ + .obj/debug-shared/if_algo.o \ + .obj/debug-shared/if_core.o \ + .obj/debug-shared/if_op.o \ + .obj/debug-shared/keypair.o \ + .obj/debug-shared/nr.o \ + .obj/debug-shared/nr_core.o \ + .obj/debug-shared/nr_op.o \ + .obj/debug-shared/pk_algs.o \ + .obj/debug-shared/pkcs8.o \ + .obj/debug-shared/x509_key.o \ + .obj/debug-shared/pk_filts.o \ + .obj/debug-shared/pk_keys.o \ + .obj/debug-shared/pubkey.o \ + .obj/debug-shared/pubkey_enums.o \ + .obj/debug-shared/rsa.o \ + .obj/debug-shared/rw.o \ + .obj/debug-shared/auto_rng.o \ + .obj/debug-shared/hmac_rng.o \ + .obj/debug-shared/randpool.o \ + .obj/debug-shared/rng.o \ + .obj/debug-shared/x931_rng.o \ + .obj/debug-shared/pbkdf1.o \ + .obj/debug-shared/pbkdf2.o \ + .obj/debug-shared/pgp_s2k.o \ + .obj/debug-shared/s2k.o \ + .obj/debug-shared/selftest.o \ + .obj/debug-shared/arc4.o \ + .obj/debug-shared/salsa20.o \ + .obj/debug-shared/stream_cipher.o \ + .obj/debug-shared/tur_tab.o \ + .obj/debug-shared/turing.o \ + .obj/debug-shared/wid_wake.o \ + .obj/debug-shared/symkey.o \ + .obj/debug-shared/timer.o \ + .obj/debug-shared/charset.o \ + .obj/debug-shared/datastor.o \ + .obj/debug-shared/exceptn.o \ + .obj/debug-shared/mlock.o \ + .obj/debug-shared/parsing.o \ + .obj/debug-shared/ui.o \ + .obj/debug-shared/util.o \ + .obj/debug-shared/version.o \ + .obj/debug-shared/mmap_mem.o \ + .obj/debug-shared/asn1_eac_str.o \ + .obj/debug-shared/asn1_eac_tm.o \ + .obj/debug-shared/cvc_ado.o \ + .obj/debug-shared/cvc_ca.o \ + .obj/debug-shared/cvc_cert.o \ + .obj/debug-shared/cvc_req.o \ + .obj/debug-shared/cvc_self.o \ + .obj/debug-shared/ecdsa_sig.o \ + .obj/debug-shared/signed_obj.o \ + .obj/debug-shared/es_dev.o \ + .obj/debug-shared/es_egd.o \ + .obj/debug-shared/es_ftw.o \ + .obj/debug-shared/es_unix.o \ + .obj/debug-shared/unix_cmd.o \ + .obj/debug-shared/unix_src.o \ + .obj/debug-shared/fd_unix.o \ + .obj/debug-shared/curve_gfp.o \ + .obj/debug-shared/gfp_element.o \ + .obj/debug-shared/point_gfp.o \ + .obj/debug-shared/mux_pthr.o \ + .obj/debug-shared/ec_dompar.o \ + .obj/debug-shared/ecc_key.o \ + .obj/debug-shared/ecdsa.o \ + .obj/debug-shared/ecdsa_core.o \ + .obj/debug-shared/ecdsa_op.o \ + .obj/debug-shared/eckaeg.o \ + .obj/debug-shared/eckaeg_core.o \ + .obj/debug-shared/eckaeg_op.o \ + .obj/debug-shared/tm_unix.o +DIST = /opt/Qt/4.7/mkspecs/common/unix.conf \ + /opt/Qt/4.7/mkspecs/common/mac.conf \ + /opt/Qt/4.7/mkspecs/common/mac-g++.conf \ + /opt/Qt/4.7/mkspecs/qconfig.pri \ + /opt/Qt/4.7/mkspecs/features/qt_functions.prf \ + /opt/Qt/4.7/mkspecs/features/qt_config.prf \ + /opt/Qt/4.7/mkspecs/features/exclusive_builds.prf \ + /opt/Qt/4.7/mkspecs/features/default_pre.prf \ + /opt/Qt/4.7/mkspecs/features/mac/default_pre.prf \ + /Users/ebaak/depot/creator-qtest/qtcreator.pri \ + ../../../../rpath.pri \ + ../../../../qtcreatorlibrary.pri \ + /opt/Qt/4.7/mkspecs/features/mac/dwarf2.prf \ + /opt/Qt/4.7/mkspecs/features/debug.prf \ + /opt/Qt/4.7/mkspecs/features/default_post.prf \ + /opt/Qt/4.7/mkspecs/features/mac/default_post.prf \ + /opt/Qt/4.7/mkspecs/features/mac/x86_64.prf \ + /opt/Qt/4.7/mkspecs/features/mac/objective_c.prf \ + /opt/Qt/4.7/mkspecs/features/unix/hide_symbols.prf \ + /opt/Qt/4.7/mkspecs/features/dll.prf \ + /opt/Qt/4.7/mkspecs/features/shared.prf \ + /opt/Qt/4.7/mkspecs/features/warn_on.prf \ + /opt/Qt/4.7/mkspecs/modules/qt_webkit_version.pri \ + /opt/Qt/4.7/mkspecs/features/qt.prf \ + /opt/Qt/4.7/mkspecs/features/unix/thread.prf \ + /opt/Qt/4.7/mkspecs/features/moc.prf \ + /opt/Qt/4.7/mkspecs/features/mac/rez.prf \ + /opt/Qt/4.7/mkspecs/features/mac/sdk.prf \ + /opt/Qt/4.7/mkspecs/features/resources.prf \ + /opt/Qt/4.7/mkspecs/features/uic.prf \ + /opt/Qt/4.7/mkspecs/features/yacc.prf \ + /opt/Qt/4.7/mkspecs/features/lex.prf \ + src.pro +QMAKE_TARGET = Botan_debug +DESTDIR = /Users/ebaak/depot/creator-qtest/bin/Qt Creator.app/Contents/PlugIns/ +TARGET = libBotan_debug.1.0.0.dylib +TARGETA = /Users/ebaak/depot/creator-qtest/bin/Qt\ Creator.app/Contents/PlugIns/libBotan_debug.a +TARGETD = libBotan_debug.1.0.0.dylib +TARGET0 = libBotan_debug.dylib +TARGET1 = libBotan_debug.1.dylib +TARGET2 = libBotan_debug.1.0.dylib + +####### Custom Compiler Variables +QMAKE_COMP_QMAKE_OBJECTIVE_CFLAGS = -pipe \ + -fPIC \ + -ansi \ + -fpermissive \ + -finline-functions \ + -Wno-long-long \ + -g \ + -gdwarf-2 \ + -arch \ + x86_64 \ + -Xarch_x86_64 \ + -mmacosx-version-min=10.5 \ + -fvisibility=hidden \ + -Wall \ + -W + + +first: all +####### Implicit rules + +.SUFFIXES: .o .c .cpp .cc .cxx .C + +.cpp.o: + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<" + +.cc.o: + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<" + +.cxx.o: + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<" + +.C.o: + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<" + +.c.o: + $(CC) -c $(CFLAGS) $(INCPATH) -o "$@" "$<" + +####### Build rules + +all: Makefile /Users/ebaak/depot/creator-qtest/bin/Qt\ Creator.app/Contents/PlugIns/$(TARGET) + +/Users/ebaak/depot/creator-qtest/bin/Qt\ Creator.app/Contents/PlugIns/$(TARGET): $(OBJECTS) $(SUBLIBS) $(OBJCOMP) + @$(CHK_DIR_EXISTS) /Users/ebaak/depot/creator-qtest/bin/Qt\ Creator.app/Contents/PlugIns/ || $(MKDIR) /Users/ebaak/depot/creator-qtest/bin/Qt\ Creator.app/Contents/PlugIns/ + -$(DEL_FILE) $(TARGET) $(TARGET0) $(TARGET1) $(TARGET2) + $(LINK) $(LFLAGS) -o $(TARGET) $(OBJECTS) $(LIBS) $(OBJCOMP) + -ln -s $(TARGET) $(TARGET0) + -ln -s $(TARGET) $(TARGET1) + -ln -s $(TARGET) $(TARGET2) + -$(DEL_FILE) /Users/ebaak/depot/creator-qtest/bin/Qt\ Creator.app/Contents/PlugIns/$(TARGET) + -$(DEL_FILE) /Users/ebaak/depot/creator-qtest/bin/Qt\ Creator.app/Contents/PlugIns/$(TARGET0) + -$(DEL_FILE) /Users/ebaak/depot/creator-qtest/bin/Qt\ Creator.app/Contents/PlugIns/$(TARGET1) + -$(DEL_FILE) /Users/ebaak/depot/creator-qtest/bin/Qt\ Creator.app/Contents/PlugIns/$(TARGET2) + -$(MOVE) $(TARGET) $(TARGET0) $(TARGET1) $(TARGET2) /Users/ebaak/depot/creator-qtest/bin/Qt\ Creator.app/Contents/PlugIns/ + + + +staticlib: $(TARGETA) + +$(TARGETA): $(OBJECTS) $(OBJCOMP) + -$(DEL_FILE) $(TARGETA) + $(AR) $(TARGETA) $(OBJECTS) + $(RANLIB) $(TARGETA) + +Makefile: src.pro /opt/Qt/4.7/mkspecs/macx-g++/qmake.conf /opt/Qt/4.7/mkspecs/common/unix.conf \ + /opt/Qt/4.7/mkspecs/common/mac.conf \ + /opt/Qt/4.7/mkspecs/common/mac-g++.conf \ + /opt/Qt/4.7/mkspecs/qconfig.pri \ + /opt/Qt/4.7/mkspecs/features/qt_functions.prf \ + /opt/Qt/4.7/mkspecs/features/qt_config.prf \ + /opt/Qt/4.7/mkspecs/features/exclusive_builds.prf \ + /opt/Qt/4.7/mkspecs/features/default_pre.prf \ + /opt/Qt/4.7/mkspecs/features/mac/default_pre.prf \ + /Users/ebaak/depot/creator-qtest/qtcreator.pri \ + ../../../../rpath.pri \ + ../../../../qtcreatorlibrary.pri \ + /opt/Qt/4.7/mkspecs/features/mac/dwarf2.prf \ + /opt/Qt/4.7/mkspecs/features/debug.prf \ + /opt/Qt/4.7/mkspecs/features/default_post.prf \ + /opt/Qt/4.7/mkspecs/features/mac/default_post.prf \ + /opt/Qt/4.7/mkspecs/features/mac/x86_64.prf \ + /opt/Qt/4.7/mkspecs/features/mac/objective_c.prf \ + /opt/Qt/4.7/mkspecs/features/unix/hide_symbols.prf \ + /opt/Qt/4.7/mkspecs/features/dll.prf \ + /opt/Qt/4.7/mkspecs/features/shared.prf \ + /opt/Qt/4.7/mkspecs/features/warn_on.prf \ + /opt/Qt/4.7/mkspecs/modules/qt_webkit_version.pri \ + /opt/Qt/4.7/mkspecs/features/qt.prf \ + /opt/Qt/4.7/mkspecs/features/unix/thread.prf \ + /opt/Qt/4.7/mkspecs/features/moc.prf \ + /opt/Qt/4.7/mkspecs/features/mac/rez.prf \ + /opt/Qt/4.7/mkspecs/features/mac/sdk.prf \ + /opt/Qt/4.7/mkspecs/features/resources.prf \ + /opt/Qt/4.7/mkspecs/features/uic.prf \ + /opt/Qt/4.7/mkspecs/features/yacc.prf \ + /opt/Qt/4.7/mkspecs/features/lex.prf \ + /opt/Qt/4.7/lib/QtTest.framework/QtTest.prl \ + /opt/Qt/4.7/lib/QtCore.framework/QtCore.prl \ + /opt/Qt/4.7/lib/QtGui.framework/QtGui.prl + $(QMAKE) -spec /opt/Qt/4.7/mkspecs/macx-g++ -o Makefile src.pro +/opt/Qt/4.7/mkspecs/common/unix.conf: +/opt/Qt/4.7/mkspecs/common/mac.conf: +/opt/Qt/4.7/mkspecs/common/mac-g++.conf: +/opt/Qt/4.7/mkspecs/qconfig.pri: +/opt/Qt/4.7/mkspecs/features/qt_functions.prf: +/opt/Qt/4.7/mkspecs/features/qt_config.prf: +/opt/Qt/4.7/mkspecs/features/exclusive_builds.prf: +/opt/Qt/4.7/mkspecs/features/default_pre.prf: +/opt/Qt/4.7/mkspecs/features/mac/default_pre.prf: +/Users/ebaak/depot/creator-qtest/qtcreator.pri: +../../../../rpath.pri: +../../../../qtcreatorlibrary.pri: +/opt/Qt/4.7/mkspecs/features/mac/dwarf2.prf: +/opt/Qt/4.7/mkspecs/features/debug.prf: +/opt/Qt/4.7/mkspecs/features/default_post.prf: +/opt/Qt/4.7/mkspecs/features/mac/default_post.prf: +/opt/Qt/4.7/mkspecs/features/mac/x86_64.prf: +/opt/Qt/4.7/mkspecs/features/mac/objective_c.prf: +/opt/Qt/4.7/mkspecs/features/unix/hide_symbols.prf: +/opt/Qt/4.7/mkspecs/features/dll.prf: +/opt/Qt/4.7/mkspecs/features/shared.prf: +/opt/Qt/4.7/mkspecs/features/warn_on.prf: +/opt/Qt/4.7/mkspecs/modules/qt_webkit_version.pri: +/opt/Qt/4.7/mkspecs/features/qt.prf: +/opt/Qt/4.7/mkspecs/features/unix/thread.prf: +/opt/Qt/4.7/mkspecs/features/moc.prf: +/opt/Qt/4.7/mkspecs/features/mac/rez.prf: +/opt/Qt/4.7/mkspecs/features/mac/sdk.prf: +/opt/Qt/4.7/mkspecs/features/resources.prf: +/opt/Qt/4.7/mkspecs/features/uic.prf: +/opt/Qt/4.7/mkspecs/features/yacc.prf: +/opt/Qt/4.7/mkspecs/features/lex.prf: +/opt/Qt/4.7/lib/QtTest.framework/QtTest.prl: +/opt/Qt/4.7/lib/QtCore.framework/QtCore.prl: +/opt/Qt/4.7/lib/QtGui.framework/QtGui.prl: +qmake: FORCE + @$(QMAKE) -spec /opt/Qt/4.7/mkspecs/macx-g++ -o Makefile src.pro + +dist: + @$(CHK_DIR_EXISTS) .obj/debug-shared/Botan_debug1.0.0 || $(MKDIR) .obj/debug-shared/Botan_debug1.0.0 + $(COPY_FILE) --parents $(SOURCES) $(DIST) .obj/debug-shared/Botan_debug1.0.0/ && $(COPY_FILE) --parents algo_factory/algo_cache.h algo_factory/algo_factory.h alloc/allocate.h alloc/mem_pool/mem_pool.h alloc/secmem.h alloc/system_alloc/defalloc.h asn1/alg_id.h asn1/asn1_int.h asn1/asn1_obj.h asn1/asn1_oid.h asn1/ber_dec.h asn1/der_enc.h benchmark/benchmark.h block/aes/aes.h block/block_cipher.h block/blowfish/blowfish.h block/cast/cast128.h block/cast/cast256.h block/des/des.h block/des/desx.h block/gost_28147/gost_28147.h block/idea/idea.h block/kasumi/kasumi.h block/lion/lion.h block/lubyrack/lubyrack.h block/mars/mars.h block/misty1/misty1.h block/noekeon/noekeon.h block/rc2/rc2.h block/rc5/rc5.h block/rc6/rc6.h block/safer/safer_sk.h block/seed/seed.h block/serpent/serpent.h block/skipjack/skipjack.h block/square/square.h block/tea/tea.h block/twofish/twofish.h block/xtea/xtea.h cert/x509/certstor.h cert/x509/crl_ent.h cert/x509/pkcs10.h cert/x509/x509_ca.h cert/x509/x509_crl.h cert/x509/x509_ext.h cert/x509/x509_obj.h cert/x509/x509cert.h cert/x509/x509find.h cert/x509/x509self.h cert/x509/x509stor.h checksum/adler32/adler32.h checksum/crc24/crc24.h checksum/crc32/crc32.h cms/cms_dec.h cms/cms_enc.h codec/base64/base64.h codec/hex/hex.h codec/openpgp/openpgp.h codec/pem/pem.h cryptobox/cryptobox.h engine/def_engine/def_eng.h engine/engine.h entropy/entropy_src.h filters/basefilt.h filters/buf_filt.h filters/data_snk.h filters/data_src.h filters/filter.h filters/filters.h filters/out_buf.h filters/pbe.h filters/pipe.h filters/secqueue.h hash/fork256/fork256.h hash/gost_3411/gost_3411.h hash/has160/has160.h hash/hash.h hash/md2/md2.h hash/md4/md4.h hash/md5/md5.h hash/mdx_hash/mdx_hash.h hash/par_hash/par_hash.h hash/rmd128/rmd128.h hash/rmd160/rmd160.h hash/sha1/sha160.h hash/sha2/sha2_32.h hash/sha2/sha2_64.h hash/skein/skein_512.h hash/tiger/tiger.h hash/whirlpool/whrlpool.h kdf/kdf.h kdf/kdf1/kdf1.h kdf/kdf2/kdf2.h kdf/mgf1/mgf1.h kdf/ssl_prf/prf_ssl3.h kdf/tls_prf/prf_tls.h kdf/x942_prf/prf_x942.h libstate/botan.h libstate/init.h libstate/libstate.h libstate/look_pk.h libstate/lookup.h libstate/oid_lookup/oids.h libstate/pk_engine.h libstate/scan_name.h mac/cbc_mac/cbc_mac.h mac/cmac/cmac.h mac/hmac/hmac.h mac/mac.h mac/ssl3mac/ssl3_mac.h mac/x919_mac/x919_mac.h math/bigint/bigint.h math/bigint/divide.h math/bigint/mp_core.h math/bigint/mp_generic/mp_asm.h math/bigint/mp_generic/mp_asmi.h math/bigint/mp_types.h math/numbertheory/blinding.h math/numbertheory/def_powm.h math/numbertheory/numthry.h math/numbertheory/pow_mod.h math/numbertheory/reducer.h modes/cbc/cbc.h modes/cfb/cfb.h modes/ctr/ctr.h modes/cts/cts.h modes/eax/eax.h modes/ecb/ecb.h modes/mode_pad/mode_pad.h modes/modebase.h modes/ofb/ofb.h modes/xts/xts.h mutex/mutex.h mutex/noop_mutex/mux_noop.h pbe/get_pbe.h pbe/pbes1/pbes1.h pbe/pbes2/pbes2.h pk_pad/eme.h pk_pad/eme1/eme1.h pk_pad/eme_pkcs/eme_pkcs.h pk_pad/emsa.h pk_pad/emsa1/emsa1.h pk_pad/emsa1_bsi/emsa1_bsi.h pk_pad/emsa2/emsa2.h pk_pad/emsa3/emsa3.h pk_pad/emsa4/emsa4.h pk_pad/emsa_raw/emsa_raw.h pk_pad/hash_id/hash_id.h pubkey/dh/dh.h pubkey/dh/dh_core.h pubkey/dh/dh_op.h pubkey/dl_algo/dl_algo.h pubkey/dl_group/dl_group.h pubkey/dlies/dlies.h pubkey/dsa/dsa.h pubkey/dsa/dsa_core.h pubkey/dsa/dsa_op.h pubkey/elgamal/elg_core.h pubkey/elgamal/elg_op.h pubkey/elgamal/elgamal.h pubkey/if_algo/if_algo.h pubkey/if_algo/if_core.h pubkey/if_algo/if_op.h pubkey/keypair/keypair.h pubkey/nr/nr.h pubkey/nr/nr_core.h pubkey/nr/nr_op.h pubkey/pk_algs.h pubkey/pk_codecs/pkcs8.h pubkey/pk_codecs/x509_key.h pubkey/pk_filts.h pubkey/pk_keys.h pubkey/pubkey.h pubkey/pubkey_enums.h pubkey/rsa/rsa.h pubkey/rw/rw.h rng/auto_rng/auto_rng.h rng/hmac_rng/hmac_rng.h rng/randpool/randpool.h rng/rng.h rng/x931_rng/x931_rng.h s2k/pbkdf1/pbkdf1.h s2k/pbkdf2/pbkdf2.h s2k/pgps2k/pgp_s2k.h s2k/s2k.h selftest/selftest.h stream/arc4/arc4.h stream/salsa20/salsa20.h stream/stream_cipher.h stream/turing/turing.h stream/wid_wake/wid_wake.h sym_algo/sym_algo.h sym_algo/symkey.h timer/timer.h utils/bit_ops.h utils/bswap.h utils/buf_comp/buf_comp.h utils/charset.h utils/datastor/datastor.h utils/exceptn.h utils/loadstor.h utils/mem_ops.h utils/parsing.h utils/rotate.h utils/stl_util.h utils/types.h utils/ui.h utils/util.h utils/version.h utils/xor_buf.h alloc/alloc_mmap/mmap_mem.h cert/cvc/cvc_ado.h cert/cvc/cvc_ca.h cert/cvc/cvc_cert.h cert/cvc/cvc_gen_cert.h cert/cvc/cvc_key.h cert/cvc/cvc_req.h cert/cvc/cvc_self.h cert/cvc/eac_asn_obj.h cert/cvc/eac_obj.h cert/cvc/ecdsa_sig.h cert/cvc/freestore.h cert/cvc/signed_obj.h entropy/dev_random/es_dev.h entropy/egd/es_egd.h entropy/proc_walk/es_ftw.h entropy/unix_procs/es_unix.h entropy/unix_procs/unix_cmd.h filters/fd_unix/fd_unix.h math/gfpmath/curve_gfp.h math/gfpmath/gfp_element.h math/gfpmath/gfp_modulus.h math/gfpmath/point_gfp.h mutex/pthreads/mux_pthr.h pubkey/ec_dompar/ec_dompar.h pubkey/ecc_key/ecc_key.h pubkey/ecdsa/ecdsa.h pubkey/ecdsa/ecdsa_core.h pubkey/ecdsa/ecdsa_op.h pubkey/eckaeg/eckaeg.h pubkey/eckaeg/eckaeg_core.h pubkey/eckaeg/eckaeg_op.h timer/gettimeofday/tm_unix.h .obj/debug-shared/Botan_debug1.0.0/ && $(COPY_FILE) --parents algo_factory/algo_factory.cpp algo_factory/prov_weight.cpp alloc/mem_pool/mem_pool.cpp alloc/system_alloc/defalloc.cpp asn1/alg_id.cpp asn1/asn1_alt.cpp asn1/asn1_att.cpp asn1/asn1_dn.cpp asn1/asn1_int.cpp asn1/asn1_oid.cpp asn1/asn1_str.cpp asn1/asn1_tm.cpp asn1/ber_dec.cpp asn1/der_enc.cpp benchmark/benchmark.cpp block/aes/aes.cpp block/aes/aes_tab.cpp block/blowfish/blfs_tab.cpp block/blowfish/blowfish.cpp block/cast/cast128.cpp block/cast/cast256.cpp block/cast/cast_tab.cpp block/des/des.cpp block/des/des_tab.cpp block/des/desx.cpp block/gost_28147/gost_28147.cpp block/idea/idea.cpp block/kasumi/kasumi.cpp block/lion/lion.cpp block/lubyrack/lubyrack.cpp block/mars/mars.cpp block/mars/mars_tab.cpp block/misty1/misty1.cpp block/noekeon/noekeon.cpp block/rc2/rc2.cpp block/rc5/rc5.cpp block/rc6/rc6.cpp block/safer/safe_tab.cpp block/safer/safer_sk.cpp block/seed/seed.cpp block/seed/seed_tab.cpp block/serpent/serpent.cpp block/skipjack/skipjack.cpp block/square/sqr_tab.cpp block/square/square.cpp block/tea/tea.cpp block/twofish/two_tab.cpp block/twofish/twofish.cpp block/xtea/xtea.cpp cert/x509/certstor.cpp cert/x509/crl_ent.cpp cert/x509/pkcs10.cpp cert/x509/x509_ca.cpp cert/x509/x509_crl.cpp cert/x509/x509_ext.cpp cert/x509/x509_obj.cpp cert/x509/x509cert.cpp cert/x509/x509find.cpp cert/x509/x509opt.cpp cert/x509/x509self.cpp cert/x509/x509stor.cpp checksum/adler32/adler32.cpp checksum/crc24/crc24.cpp checksum/crc32/crc32.cpp cms/cms_algo.cpp cms/cms_comp.cpp cms/cms_dalg.cpp cms/cms_dec.cpp cms/cms_ealg.cpp cms/cms_enc.cpp codec/base64/b64_char.cpp codec/base64/base64.cpp codec/hex/hex.cpp codec/hex/hex_char.cpp codec/openpgp/openpgp.cpp codec/pem/pem.cpp cryptobox/cryptobox.cpp engine/def_engine/def_mode.cpp engine/def_engine/def_pk_ops.cpp engine/def_engine/def_powm.cpp engine/def_engine/lookup_block.cpp engine/def_engine/lookup_hash.cpp engine/def_engine/lookup_mac.cpp engine/def_engine/lookup_stream.cpp filters/algo_filt.cpp filters/basefilt.cpp filters/buf_filt.cpp filters/data_snk.cpp filters/data_src.cpp filters/filter.cpp filters/out_buf.cpp filters/pipe.cpp filters/pipe_io.cpp filters/pipe_rw.cpp filters/secqueue.cpp hash/fork256/fork256.cpp hash/gost_3411/gost_3411.cpp hash/has160/has160.cpp hash/md2/md2.cpp hash/md4/md4.cpp hash/md5/md5.cpp hash/mdx_hash/mdx_hash.cpp hash/par_hash/par_hash.cpp hash/rmd128/rmd128.cpp hash/rmd160/rmd160.cpp hash/sha1/sha160.cpp hash/sha2/sha2_32.cpp hash/sha2/sha2_64.cpp hash/skein/skein_512.cpp hash/tiger/tig_tab.cpp hash/tiger/tiger.cpp hash/whirlpool/whrl_tab.cpp hash/whirlpool/whrlpool.cpp kdf/kdf.cpp kdf/kdf1/kdf1.cpp kdf/kdf2/kdf2.cpp kdf/mgf1/mgf1.cpp kdf/ssl_prf/prf_ssl3.cpp kdf/tls_prf/prf_tls.cpp kdf/x942_prf/prf_x942.cpp libstate/get_enc.cpp libstate/init.cpp libstate/libstate.cpp libstate/look_pk.cpp libstate/lookup.cpp libstate/oid_lookup/oids.cpp libstate/pk_engine.cpp libstate/policy.cpp libstate/scan_name.cpp mac/cbc_mac/cbc_mac.cpp mac/cmac/cmac.cpp mac/hmac/hmac.cpp mac/mac.cpp mac/ssl3mac/ssl3_mac.cpp mac/x919_mac/x919_mac.cpp math/bigint/big_code.cpp math/bigint/big_io.cpp math/bigint/big_ops2.cpp math/bigint/big_ops3.cpp math/bigint/big_rand.cpp math/bigint/bigint.cpp math/bigint/divide.cpp math/bigint/monty_generic/mp_monty.cpp math/bigint/mp_asm.cpp math/bigint/mp_comba.cpp math/bigint/mp_karat.cpp math/bigint/mp_misc.cpp math/bigint/mp_shift.cpp math/bigint/mulop_generic/mp_mulop.cpp math/numbertheory/blinding.cpp math/numbertheory/dsa_gen.cpp math/numbertheory/jacobi.cpp math/numbertheory/make_prm.cpp math/numbertheory/mp_numth.cpp math/numbertheory/numthry.cpp math/numbertheory/pow_mod.cpp math/numbertheory/powm_fw.cpp math/numbertheory/powm_mnt.cpp math/numbertheory/primes.cpp math/numbertheory/reducer.cpp math/numbertheory/ressol.cpp modes/cbc/cbc.cpp modes/cfb/cfb.cpp modes/ctr/ctr.cpp modes/cts/cts.cpp modes/eax/eax.cpp modes/eax/eax_dec.cpp modes/ecb/ecb.cpp modes/mode_pad/mode_pad.cpp modes/modebase.cpp modes/ofb/ofb.cpp modes/xts/xts.cpp mutex/noop_mutex/mux_noop.cpp pbe/get_pbe.cpp pbe/pbes1/pbes1.cpp pbe/pbes2/pbes2.cpp pk_pad/eme.cpp pk_pad/eme1/eme1.cpp pk_pad/eme_pkcs/eme_pkcs.cpp pk_pad/emsa1/emsa1.cpp pk_pad/emsa1_bsi/emsa1_bsi.cpp pk_pad/emsa2/emsa2.cpp pk_pad/emsa3/emsa3.cpp pk_pad/emsa4/emsa4.cpp pk_pad/emsa_raw/emsa_raw.cpp pk_pad/hash_id/hash_id.cpp pubkey/dh/dh.cpp pubkey/dh/dh_core.cpp pubkey/dl_algo/dl_algo.cpp pubkey/dl_group/dl_group.cpp pubkey/dlies/dlies.cpp pubkey/dsa/dsa.cpp pubkey/dsa/dsa_core.cpp pubkey/dsa/dsa_op.cpp pubkey/elgamal/elg_core.cpp pubkey/elgamal/elg_op.cpp pubkey/elgamal/elgamal.cpp pubkey/if_algo/if_algo.cpp pubkey/if_algo/if_core.cpp pubkey/if_algo/if_op.cpp pubkey/keypair/keypair.cpp pubkey/nr/nr.cpp pubkey/nr/nr_core.cpp pubkey/nr/nr_op.cpp pubkey/pk_algs.cpp pubkey/pk_codecs/pkcs8.cpp pubkey/pk_codecs/x509_key.cpp pubkey/pk_filts.cpp pubkey/pk_keys.cpp pubkey/pubkey.cpp pubkey/pubkey_enums.cpp pubkey/rsa/rsa.cpp pubkey/rw/rw.cpp rng/auto_rng/auto_rng.cpp rng/hmac_rng/hmac_rng.cpp rng/randpool/randpool.cpp rng/rng.cpp rng/x931_rng/x931_rng.cpp s2k/pbkdf1/pbkdf1.cpp s2k/pbkdf2/pbkdf2.cpp s2k/pgps2k/pgp_s2k.cpp s2k/s2k.cpp selftest/selftest.cpp stream/arc4/arc4.cpp stream/salsa20/salsa20.cpp stream/stream_cipher.cpp stream/turing/tur_tab.cpp stream/turing/turing.cpp stream/wid_wake/wid_wake.cpp sym_algo/symkey.cpp timer/timer.cpp utils/charset.cpp utils/datastor/datastor.cpp utils/exceptn.cpp utils/mlock.cpp utils/parsing.cpp utils/ui.cpp utils/util.cpp utils/version.cpp alloc/alloc_mmap/mmap_mem.cpp cert/cvc/asn1_eac_str.cpp cert/cvc/asn1_eac_tm.cpp cert/cvc/cvc_ado.cpp cert/cvc/cvc_ca.cpp cert/cvc/cvc_cert.cpp cert/cvc/cvc_req.cpp cert/cvc/cvc_self.cpp cert/cvc/ecdsa_sig.cpp cert/cvc/signed_obj.cpp entropy/dev_random/es_dev.cpp entropy/egd/es_egd.cpp entropy/proc_walk/es_ftw.cpp entropy/unix_procs/es_unix.cpp entropy/unix_procs/unix_cmd.cpp entropy/unix_procs/unix_src.cpp filters/fd_unix/fd_unix.cpp math/gfpmath/curve_gfp.cpp math/gfpmath/gfp_element.cpp math/gfpmath/point_gfp.cpp mutex/pthreads/mux_pthr.cpp pubkey/ec_dompar/ec_dompar.cpp pubkey/ecc_key/ecc_key.cpp pubkey/ecdsa/ecdsa.cpp pubkey/ecdsa/ecdsa_core.cpp pubkey/ecdsa/ecdsa_op.cpp pubkey/eckaeg/eckaeg.cpp pubkey/eckaeg/eckaeg_core.cpp pubkey/eckaeg/eckaeg_op.cpp timer/gettimeofday/tm_unix.cpp .obj/debug-shared/Botan_debug1.0.0/ && (cd `dirname .obj/debug-shared/Botan_debug1.0.0` && $(TAR) Botan_debug1.0.0.tar Botan_debug1.0.0 && $(COMPRESS) Botan_debug1.0.0.tar) && $(MOVE) `dirname .obj/debug-shared/Botan_debug1.0.0`/Botan_debug1.0.0.tar.gz . && $(DEL_FILE) -r .obj/debug-shared/Botan_debug1.0.0 + + +clean:compiler_clean + -$(DEL_FILE) $(OBJECTS) + -$(DEL_FILE) *~ core *.core + + +####### Sub-libraries + +distclean: clean + -$(DEL_FILE) /Users/ebaak/depot/creator-qtest/bin/Qt Creator.app/Contents/PlugIns/$(TARGET) + -$(DEL_FILE) /Users/ebaak/depot/creator-qtest/bin/Qt Creator.app/Contents/PlugIns/$(TARGET0) /Users/ebaak/depot/creator-qtest/bin/Qt Creator.app/Contents/PlugIns/$(TARGET1) /Users/ebaak/depot/creator-qtest/bin/Qt Creator.app/Contents/PlugIns/$(TARGET2) $(TARGETA) + -$(DEL_FILE) Makefile + + +check: first + +mocclean: compiler_moc_header_clean compiler_moc_source_clean + +mocables: compiler_moc_header_make_all compiler_moc_source_make_all + +compiler_objective_c_make_all: +compiler_objective_c_clean: +compiler_moc_header_make_all: +compiler_moc_header_clean: +compiler_rcc_make_all: +compiler_rcc_clean: +compiler_image_collection_make_all: qmake_image_collection.cpp +compiler_image_collection_clean: + -$(DEL_FILE) qmake_image_collection.cpp +compiler_moc_source_make_all: +compiler_moc_source_clean: +compiler_rez_source_make_all: +compiler_rez_source_clean: +compiler_uic_make_all: +compiler_uic_clean: +compiler_yacc_decl_make_all: +compiler_yacc_decl_clean: +compiler_yacc_impl_make_all: +compiler_yacc_impl_clean: +compiler_lex_make_all: +compiler_lex_clean: +compiler_clean: + +####### Compile + +.obj/debug-shared/algo_factory.o: algo_factory/algo_factory.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/algo_factory.o algo_factory/algo_factory.cpp + +.obj/debug-shared/prov_weight.o: algo_factory/prov_weight.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/prov_weight.o algo_factory/prov_weight.cpp + +.obj/debug-shared/mem_pool.o: alloc/mem_pool/mem_pool.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/mem_pool.o alloc/mem_pool/mem_pool.cpp + +.obj/debug-shared/defalloc.o: alloc/system_alloc/defalloc.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/defalloc.o alloc/system_alloc/defalloc.cpp + +.obj/debug-shared/alg_id.o: asn1/alg_id.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/alg_id.o asn1/alg_id.cpp + +.obj/debug-shared/asn1_alt.o: asn1/asn1_alt.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/asn1_alt.o asn1/asn1_alt.cpp + +.obj/debug-shared/asn1_att.o: asn1/asn1_att.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/asn1_att.o asn1/asn1_att.cpp + +.obj/debug-shared/asn1_dn.o: asn1/asn1_dn.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/asn1_dn.o asn1/asn1_dn.cpp + +.obj/debug-shared/asn1_int.o: asn1/asn1_int.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/asn1_int.o asn1/asn1_int.cpp + +.obj/debug-shared/asn1_oid.o: asn1/asn1_oid.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/asn1_oid.o asn1/asn1_oid.cpp + +.obj/debug-shared/asn1_str.o: asn1/asn1_str.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/asn1_str.o asn1/asn1_str.cpp + +.obj/debug-shared/asn1_tm.o: asn1/asn1_tm.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/asn1_tm.o asn1/asn1_tm.cpp + +.obj/debug-shared/ber_dec.o: asn1/ber_dec.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/ber_dec.o asn1/ber_dec.cpp + +.obj/debug-shared/der_enc.o: asn1/der_enc.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/der_enc.o asn1/der_enc.cpp + +.obj/debug-shared/benchmark.o: benchmark/benchmark.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/benchmark.o benchmark/benchmark.cpp + +.obj/debug-shared/aes.o: block/aes/aes.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/aes.o block/aes/aes.cpp + +.obj/debug-shared/aes_tab.o: block/aes/aes_tab.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/aes_tab.o block/aes/aes_tab.cpp + +.obj/debug-shared/blfs_tab.o: block/blowfish/blfs_tab.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/blfs_tab.o block/blowfish/blfs_tab.cpp + +.obj/debug-shared/blowfish.o: block/blowfish/blowfish.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/blowfish.o block/blowfish/blowfish.cpp + +.obj/debug-shared/cast128.o: block/cast/cast128.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/cast128.o block/cast/cast128.cpp + +.obj/debug-shared/cast256.o: block/cast/cast256.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/cast256.o block/cast/cast256.cpp + +.obj/debug-shared/cast_tab.o: block/cast/cast_tab.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/cast_tab.o block/cast/cast_tab.cpp + +.obj/debug-shared/des.o: block/des/des.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/des.o block/des/des.cpp + +.obj/debug-shared/des_tab.o: block/des/des_tab.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/des_tab.o block/des/des_tab.cpp + +.obj/debug-shared/desx.o: block/des/desx.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/desx.o block/des/desx.cpp + +.obj/debug-shared/gost_28147.o: block/gost_28147/gost_28147.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/gost_28147.o block/gost_28147/gost_28147.cpp + +.obj/debug-shared/idea.o: block/idea/idea.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/idea.o block/idea/idea.cpp + +.obj/debug-shared/kasumi.o: block/kasumi/kasumi.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/kasumi.o block/kasumi/kasumi.cpp + +.obj/debug-shared/lion.o: block/lion/lion.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/lion.o block/lion/lion.cpp + +.obj/debug-shared/lubyrack.o: block/lubyrack/lubyrack.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/lubyrack.o block/lubyrack/lubyrack.cpp + +.obj/debug-shared/mars.o: block/mars/mars.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/mars.o block/mars/mars.cpp + +.obj/debug-shared/mars_tab.o: block/mars/mars_tab.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/mars_tab.o block/mars/mars_tab.cpp + +.obj/debug-shared/misty1.o: block/misty1/misty1.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/misty1.o block/misty1/misty1.cpp + +.obj/debug-shared/noekeon.o: block/noekeon/noekeon.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/noekeon.o block/noekeon/noekeon.cpp + +.obj/debug-shared/rc2.o: block/rc2/rc2.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/rc2.o block/rc2/rc2.cpp + +.obj/debug-shared/rc5.o: block/rc5/rc5.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/rc5.o block/rc5/rc5.cpp + +.obj/debug-shared/rc6.o: block/rc6/rc6.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/rc6.o block/rc6/rc6.cpp + +.obj/debug-shared/safe_tab.o: block/safer/safe_tab.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/safe_tab.o block/safer/safe_tab.cpp + +.obj/debug-shared/safer_sk.o: block/safer/safer_sk.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/safer_sk.o block/safer/safer_sk.cpp + +.obj/debug-shared/seed.o: block/seed/seed.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/seed.o block/seed/seed.cpp + +.obj/debug-shared/seed_tab.o: block/seed/seed_tab.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/seed_tab.o block/seed/seed_tab.cpp + +.obj/debug-shared/serpent.o: block/serpent/serpent.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/serpent.o block/serpent/serpent.cpp + +.obj/debug-shared/skipjack.o: block/skipjack/skipjack.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/skipjack.o block/skipjack/skipjack.cpp + +.obj/debug-shared/sqr_tab.o: block/square/sqr_tab.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/sqr_tab.o block/square/sqr_tab.cpp + +.obj/debug-shared/square.o: block/square/square.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/square.o block/square/square.cpp + +.obj/debug-shared/tea.o: block/tea/tea.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/tea.o block/tea/tea.cpp + +.obj/debug-shared/two_tab.o: block/twofish/two_tab.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/two_tab.o block/twofish/two_tab.cpp + +.obj/debug-shared/twofish.o: block/twofish/twofish.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/twofish.o block/twofish/twofish.cpp + +.obj/debug-shared/xtea.o: block/xtea/xtea.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/xtea.o block/xtea/xtea.cpp + +.obj/debug-shared/certstor.o: cert/x509/certstor.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/certstor.o cert/x509/certstor.cpp + +.obj/debug-shared/crl_ent.o: cert/x509/crl_ent.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/crl_ent.o cert/x509/crl_ent.cpp + +.obj/debug-shared/pkcs10.o: cert/x509/pkcs10.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/pkcs10.o cert/x509/pkcs10.cpp + +.obj/debug-shared/x509_ca.o: cert/x509/x509_ca.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/x509_ca.o cert/x509/x509_ca.cpp + +.obj/debug-shared/x509_crl.o: cert/x509/x509_crl.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/x509_crl.o cert/x509/x509_crl.cpp + +.obj/debug-shared/x509_ext.o: cert/x509/x509_ext.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/x509_ext.o cert/x509/x509_ext.cpp + +.obj/debug-shared/x509_obj.o: cert/x509/x509_obj.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/x509_obj.o cert/x509/x509_obj.cpp + +.obj/debug-shared/x509cert.o: cert/x509/x509cert.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/x509cert.o cert/x509/x509cert.cpp + +.obj/debug-shared/x509find.o: cert/x509/x509find.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/x509find.o cert/x509/x509find.cpp + +.obj/debug-shared/x509opt.o: cert/x509/x509opt.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/x509opt.o cert/x509/x509opt.cpp + +.obj/debug-shared/x509self.o: cert/x509/x509self.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/x509self.o cert/x509/x509self.cpp + +.obj/debug-shared/x509stor.o: cert/x509/x509stor.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/x509stor.o cert/x509/x509stor.cpp + +.obj/debug-shared/adler32.o: checksum/adler32/adler32.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/adler32.o checksum/adler32/adler32.cpp + +.obj/debug-shared/crc24.o: checksum/crc24/crc24.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/crc24.o checksum/crc24/crc24.cpp + +.obj/debug-shared/crc32.o: checksum/crc32/crc32.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/crc32.o checksum/crc32/crc32.cpp + +.obj/debug-shared/cms_algo.o: cms/cms_algo.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/cms_algo.o cms/cms_algo.cpp + +.obj/debug-shared/cms_comp.o: cms/cms_comp.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/cms_comp.o cms/cms_comp.cpp + +.obj/debug-shared/cms_dalg.o: cms/cms_dalg.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/cms_dalg.o cms/cms_dalg.cpp + +.obj/debug-shared/cms_dec.o: cms/cms_dec.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/cms_dec.o cms/cms_dec.cpp + +.obj/debug-shared/cms_ealg.o: cms/cms_ealg.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/cms_ealg.o cms/cms_ealg.cpp + +.obj/debug-shared/cms_enc.o: cms/cms_enc.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/cms_enc.o cms/cms_enc.cpp + +.obj/debug-shared/b64_char.o: codec/base64/b64_char.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/b64_char.o codec/base64/b64_char.cpp + +.obj/debug-shared/base64.o: codec/base64/base64.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/base64.o codec/base64/base64.cpp + +.obj/debug-shared/hex.o: codec/hex/hex.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/hex.o codec/hex/hex.cpp + +.obj/debug-shared/hex_char.o: codec/hex/hex_char.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/hex_char.o codec/hex/hex_char.cpp + +.obj/debug-shared/openpgp.o: codec/openpgp/openpgp.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/openpgp.o codec/openpgp/openpgp.cpp + +.obj/debug-shared/pem.o: codec/pem/pem.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/pem.o codec/pem/pem.cpp + +.obj/debug-shared/cryptobox.o: cryptobox/cryptobox.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/cryptobox.o cryptobox/cryptobox.cpp + +.obj/debug-shared/def_mode.o: engine/def_engine/def_mode.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/def_mode.o engine/def_engine/def_mode.cpp + +.obj/debug-shared/def_pk_ops.o: engine/def_engine/def_pk_ops.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/def_pk_ops.o engine/def_engine/def_pk_ops.cpp + +.obj/debug-shared/def_powm.o: engine/def_engine/def_powm.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/def_powm.o engine/def_engine/def_powm.cpp + +.obj/debug-shared/lookup_block.o: engine/def_engine/lookup_block.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/lookup_block.o engine/def_engine/lookup_block.cpp + +.obj/debug-shared/lookup_hash.o: engine/def_engine/lookup_hash.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/lookup_hash.o engine/def_engine/lookup_hash.cpp + +.obj/debug-shared/lookup_mac.o: engine/def_engine/lookup_mac.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/lookup_mac.o engine/def_engine/lookup_mac.cpp + +.obj/debug-shared/lookup_stream.o: engine/def_engine/lookup_stream.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/lookup_stream.o engine/def_engine/lookup_stream.cpp + +.obj/debug-shared/algo_filt.o: filters/algo_filt.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/algo_filt.o filters/algo_filt.cpp + +.obj/debug-shared/basefilt.o: filters/basefilt.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/basefilt.o filters/basefilt.cpp + +.obj/debug-shared/buf_filt.o: filters/buf_filt.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/buf_filt.o filters/buf_filt.cpp + +.obj/debug-shared/data_snk.o: filters/data_snk.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/data_snk.o filters/data_snk.cpp + +.obj/debug-shared/data_src.o: filters/data_src.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/data_src.o filters/data_src.cpp + +.obj/debug-shared/filter.o: filters/filter.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/filter.o filters/filter.cpp + +.obj/debug-shared/out_buf.o: filters/out_buf.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/out_buf.o filters/out_buf.cpp + +.obj/debug-shared/pipe.o: filters/pipe.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/pipe.o filters/pipe.cpp + +.obj/debug-shared/pipe_io.o: filters/pipe_io.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/pipe_io.o filters/pipe_io.cpp + +.obj/debug-shared/pipe_rw.o: filters/pipe_rw.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/pipe_rw.o filters/pipe_rw.cpp + +.obj/debug-shared/secqueue.o: filters/secqueue.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/secqueue.o filters/secqueue.cpp + +.obj/debug-shared/fork256.o: hash/fork256/fork256.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/fork256.o hash/fork256/fork256.cpp + +.obj/debug-shared/gost_3411.o: hash/gost_3411/gost_3411.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/gost_3411.o hash/gost_3411/gost_3411.cpp + +.obj/debug-shared/has160.o: hash/has160/has160.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/has160.o hash/has160/has160.cpp + +.obj/debug-shared/md2.o: hash/md2/md2.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/md2.o hash/md2/md2.cpp + +.obj/debug-shared/md4.o: hash/md4/md4.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/md4.o hash/md4/md4.cpp + +.obj/debug-shared/md5.o: hash/md5/md5.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/md5.o hash/md5/md5.cpp + +.obj/debug-shared/mdx_hash.o: hash/mdx_hash/mdx_hash.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/mdx_hash.o hash/mdx_hash/mdx_hash.cpp + +.obj/debug-shared/par_hash.o: hash/par_hash/par_hash.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/par_hash.o hash/par_hash/par_hash.cpp + +.obj/debug-shared/rmd128.o: hash/rmd128/rmd128.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/rmd128.o hash/rmd128/rmd128.cpp + +.obj/debug-shared/rmd160.o: hash/rmd160/rmd160.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/rmd160.o hash/rmd160/rmd160.cpp + +.obj/debug-shared/sha160.o: hash/sha1/sha160.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/sha160.o hash/sha1/sha160.cpp + +.obj/debug-shared/sha2_32.o: hash/sha2/sha2_32.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/sha2_32.o hash/sha2/sha2_32.cpp + +.obj/debug-shared/sha2_64.o: hash/sha2/sha2_64.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/sha2_64.o hash/sha2/sha2_64.cpp + +.obj/debug-shared/skein_512.o: hash/skein/skein_512.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/skein_512.o hash/skein/skein_512.cpp + +.obj/debug-shared/tig_tab.o: hash/tiger/tig_tab.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/tig_tab.o hash/tiger/tig_tab.cpp + +.obj/debug-shared/tiger.o: hash/tiger/tiger.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/tiger.o hash/tiger/tiger.cpp + +.obj/debug-shared/whrl_tab.o: hash/whirlpool/whrl_tab.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/whrl_tab.o hash/whirlpool/whrl_tab.cpp + +.obj/debug-shared/whrlpool.o: hash/whirlpool/whrlpool.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/whrlpool.o hash/whirlpool/whrlpool.cpp + +.obj/debug-shared/kdf.o: kdf/kdf.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/kdf.o kdf/kdf.cpp + +.obj/debug-shared/kdf1.o: kdf/kdf1/kdf1.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/kdf1.o kdf/kdf1/kdf1.cpp + +.obj/debug-shared/kdf2.o: kdf/kdf2/kdf2.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/kdf2.o kdf/kdf2/kdf2.cpp + +.obj/debug-shared/mgf1.o: kdf/mgf1/mgf1.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/mgf1.o kdf/mgf1/mgf1.cpp + +.obj/debug-shared/prf_ssl3.o: kdf/ssl_prf/prf_ssl3.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/prf_ssl3.o kdf/ssl_prf/prf_ssl3.cpp + +.obj/debug-shared/prf_tls.o: kdf/tls_prf/prf_tls.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/prf_tls.o kdf/tls_prf/prf_tls.cpp + +.obj/debug-shared/prf_x942.o: kdf/x942_prf/prf_x942.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/prf_x942.o kdf/x942_prf/prf_x942.cpp + +.obj/debug-shared/get_enc.o: libstate/get_enc.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/get_enc.o libstate/get_enc.cpp + +.obj/debug-shared/init.o: libstate/init.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/init.o libstate/init.cpp + +.obj/debug-shared/libstate.o: libstate/libstate.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/libstate.o libstate/libstate.cpp + +.obj/debug-shared/look_pk.o: libstate/look_pk.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/look_pk.o libstate/look_pk.cpp + +.obj/debug-shared/lookup.o: libstate/lookup.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/lookup.o libstate/lookup.cpp + +.obj/debug-shared/oids.o: libstate/oid_lookup/oids.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/oids.o libstate/oid_lookup/oids.cpp + +.obj/debug-shared/pk_engine.o: libstate/pk_engine.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/pk_engine.o libstate/pk_engine.cpp + +.obj/debug-shared/policy.o: libstate/policy.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/policy.o libstate/policy.cpp + +.obj/debug-shared/scan_name.o: libstate/scan_name.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/scan_name.o libstate/scan_name.cpp + +.obj/debug-shared/cbc_mac.o: mac/cbc_mac/cbc_mac.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/cbc_mac.o mac/cbc_mac/cbc_mac.cpp + +.obj/debug-shared/cmac.o: mac/cmac/cmac.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/cmac.o mac/cmac/cmac.cpp + +.obj/debug-shared/hmac.o: mac/hmac/hmac.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/hmac.o mac/hmac/hmac.cpp + +.obj/debug-shared/mac.o: mac/mac.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/mac.o mac/mac.cpp + +.obj/debug-shared/ssl3_mac.o: mac/ssl3mac/ssl3_mac.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/ssl3_mac.o mac/ssl3mac/ssl3_mac.cpp + +.obj/debug-shared/x919_mac.o: mac/x919_mac/x919_mac.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/x919_mac.o mac/x919_mac/x919_mac.cpp + +.obj/debug-shared/big_code.o: math/bigint/big_code.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/big_code.o math/bigint/big_code.cpp + +.obj/debug-shared/big_io.o: math/bigint/big_io.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/big_io.o math/bigint/big_io.cpp + +.obj/debug-shared/big_ops2.o: math/bigint/big_ops2.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/big_ops2.o math/bigint/big_ops2.cpp + +.obj/debug-shared/big_ops3.o: math/bigint/big_ops3.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/big_ops3.o math/bigint/big_ops3.cpp + +.obj/debug-shared/big_rand.o: math/bigint/big_rand.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/big_rand.o math/bigint/big_rand.cpp + +.obj/debug-shared/bigint.o: math/bigint/bigint.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/bigint.o math/bigint/bigint.cpp + +.obj/debug-shared/divide.o: math/bigint/divide.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/divide.o math/bigint/divide.cpp + +.obj/debug-shared/mp_monty.o: math/bigint/monty_generic/mp_monty.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/mp_monty.o math/bigint/monty_generic/mp_monty.cpp + +.obj/debug-shared/mp_asm.o: math/bigint/mp_asm.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/mp_asm.o math/bigint/mp_asm.cpp + +.obj/debug-shared/mp_comba.o: math/bigint/mp_comba.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/mp_comba.o math/bigint/mp_comba.cpp + +.obj/debug-shared/mp_karat.o: math/bigint/mp_karat.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/mp_karat.o math/bigint/mp_karat.cpp + +.obj/debug-shared/mp_misc.o: math/bigint/mp_misc.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/mp_misc.o math/bigint/mp_misc.cpp + +.obj/debug-shared/mp_shift.o: math/bigint/mp_shift.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/mp_shift.o math/bigint/mp_shift.cpp + +.obj/debug-shared/mp_mulop.o: math/bigint/mulop_generic/mp_mulop.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/mp_mulop.o math/bigint/mulop_generic/mp_mulop.cpp + +.obj/debug-shared/blinding.o: math/numbertheory/blinding.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/blinding.o math/numbertheory/blinding.cpp + +.obj/debug-shared/dsa_gen.o: math/numbertheory/dsa_gen.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/dsa_gen.o math/numbertheory/dsa_gen.cpp + +.obj/debug-shared/jacobi.o: math/numbertheory/jacobi.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/jacobi.o math/numbertheory/jacobi.cpp + +.obj/debug-shared/make_prm.o: math/numbertheory/make_prm.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/make_prm.o math/numbertheory/make_prm.cpp + +.obj/debug-shared/mp_numth.o: math/numbertheory/mp_numth.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/mp_numth.o math/numbertheory/mp_numth.cpp + +.obj/debug-shared/numthry.o: math/numbertheory/numthry.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/numthry.o math/numbertheory/numthry.cpp + +.obj/debug-shared/pow_mod.o: math/numbertheory/pow_mod.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/pow_mod.o math/numbertheory/pow_mod.cpp + +.obj/debug-shared/powm_fw.o: math/numbertheory/powm_fw.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/powm_fw.o math/numbertheory/powm_fw.cpp + +.obj/debug-shared/powm_mnt.o: math/numbertheory/powm_mnt.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/powm_mnt.o math/numbertheory/powm_mnt.cpp + +.obj/debug-shared/primes.o: math/numbertheory/primes.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/primes.o math/numbertheory/primes.cpp + +.obj/debug-shared/reducer.o: math/numbertheory/reducer.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/reducer.o math/numbertheory/reducer.cpp + +.obj/debug-shared/ressol.o: math/numbertheory/ressol.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/ressol.o math/numbertheory/ressol.cpp + +.obj/debug-shared/cbc.o: modes/cbc/cbc.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/cbc.o modes/cbc/cbc.cpp + +.obj/debug-shared/cfb.o: modes/cfb/cfb.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/cfb.o modes/cfb/cfb.cpp + +.obj/debug-shared/ctr.o: modes/ctr/ctr.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/ctr.o modes/ctr/ctr.cpp + +.obj/debug-shared/cts.o: modes/cts/cts.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/cts.o modes/cts/cts.cpp + +.obj/debug-shared/eax.o: modes/eax/eax.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/eax.o modes/eax/eax.cpp + +.obj/debug-shared/eax_dec.o: modes/eax/eax_dec.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/eax_dec.o modes/eax/eax_dec.cpp + +.obj/debug-shared/ecb.o: modes/ecb/ecb.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/ecb.o modes/ecb/ecb.cpp + +.obj/debug-shared/mode_pad.o: modes/mode_pad/mode_pad.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/mode_pad.o modes/mode_pad/mode_pad.cpp + +.obj/debug-shared/modebase.o: modes/modebase.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/modebase.o modes/modebase.cpp + +.obj/debug-shared/ofb.o: modes/ofb/ofb.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/ofb.o modes/ofb/ofb.cpp + +.obj/debug-shared/xts.o: modes/xts/xts.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/xts.o modes/xts/xts.cpp + +.obj/debug-shared/mux_noop.o: mutex/noop_mutex/mux_noop.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/mux_noop.o mutex/noop_mutex/mux_noop.cpp + +.obj/debug-shared/get_pbe.o: pbe/get_pbe.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/get_pbe.o pbe/get_pbe.cpp + +.obj/debug-shared/pbes1.o: pbe/pbes1/pbes1.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/pbes1.o pbe/pbes1/pbes1.cpp + +.obj/debug-shared/pbes2.o: pbe/pbes2/pbes2.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/pbes2.o pbe/pbes2/pbes2.cpp + +.obj/debug-shared/eme.o: pk_pad/eme.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/eme.o pk_pad/eme.cpp + +.obj/debug-shared/eme1.o: pk_pad/eme1/eme1.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/eme1.o pk_pad/eme1/eme1.cpp + +.obj/debug-shared/eme_pkcs.o: pk_pad/eme_pkcs/eme_pkcs.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/eme_pkcs.o pk_pad/eme_pkcs/eme_pkcs.cpp + +.obj/debug-shared/emsa1.o: pk_pad/emsa1/emsa1.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/emsa1.o pk_pad/emsa1/emsa1.cpp + +.obj/debug-shared/emsa1_bsi.o: pk_pad/emsa1_bsi/emsa1_bsi.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/emsa1_bsi.o pk_pad/emsa1_bsi/emsa1_bsi.cpp + +.obj/debug-shared/emsa2.o: pk_pad/emsa2/emsa2.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/emsa2.o pk_pad/emsa2/emsa2.cpp + +.obj/debug-shared/emsa3.o: pk_pad/emsa3/emsa3.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/emsa3.o pk_pad/emsa3/emsa3.cpp + +.obj/debug-shared/emsa4.o: pk_pad/emsa4/emsa4.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/emsa4.o pk_pad/emsa4/emsa4.cpp + +.obj/debug-shared/emsa_raw.o: pk_pad/emsa_raw/emsa_raw.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/emsa_raw.o pk_pad/emsa_raw/emsa_raw.cpp + +.obj/debug-shared/hash_id.o: pk_pad/hash_id/hash_id.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/hash_id.o pk_pad/hash_id/hash_id.cpp + +.obj/debug-shared/dh.o: pubkey/dh/dh.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/dh.o pubkey/dh/dh.cpp + +.obj/debug-shared/dh_core.o: pubkey/dh/dh_core.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/dh_core.o pubkey/dh/dh_core.cpp + +.obj/debug-shared/dl_algo.o: pubkey/dl_algo/dl_algo.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/dl_algo.o pubkey/dl_algo/dl_algo.cpp + +.obj/debug-shared/dl_group.o: pubkey/dl_group/dl_group.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/dl_group.o pubkey/dl_group/dl_group.cpp + +.obj/debug-shared/dlies.o: pubkey/dlies/dlies.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/dlies.o pubkey/dlies/dlies.cpp + +.obj/debug-shared/dsa.o: pubkey/dsa/dsa.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/dsa.o pubkey/dsa/dsa.cpp + +.obj/debug-shared/dsa_core.o: pubkey/dsa/dsa_core.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/dsa_core.o pubkey/dsa/dsa_core.cpp + +.obj/debug-shared/dsa_op.o: pubkey/dsa/dsa_op.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/dsa_op.o pubkey/dsa/dsa_op.cpp + +.obj/debug-shared/elg_core.o: pubkey/elgamal/elg_core.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/elg_core.o pubkey/elgamal/elg_core.cpp + +.obj/debug-shared/elg_op.o: pubkey/elgamal/elg_op.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/elg_op.o pubkey/elgamal/elg_op.cpp + +.obj/debug-shared/elgamal.o: pubkey/elgamal/elgamal.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/elgamal.o pubkey/elgamal/elgamal.cpp + +.obj/debug-shared/if_algo.o: pubkey/if_algo/if_algo.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/if_algo.o pubkey/if_algo/if_algo.cpp + +.obj/debug-shared/if_core.o: pubkey/if_algo/if_core.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/if_core.o pubkey/if_algo/if_core.cpp + +.obj/debug-shared/if_op.o: pubkey/if_algo/if_op.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/if_op.o pubkey/if_algo/if_op.cpp + +.obj/debug-shared/keypair.o: pubkey/keypair/keypair.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/keypair.o pubkey/keypair/keypair.cpp + +.obj/debug-shared/nr.o: pubkey/nr/nr.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/nr.o pubkey/nr/nr.cpp + +.obj/debug-shared/nr_core.o: pubkey/nr/nr_core.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/nr_core.o pubkey/nr/nr_core.cpp + +.obj/debug-shared/nr_op.o: pubkey/nr/nr_op.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/nr_op.o pubkey/nr/nr_op.cpp + +.obj/debug-shared/pk_algs.o: pubkey/pk_algs.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/pk_algs.o pubkey/pk_algs.cpp + +.obj/debug-shared/pkcs8.o: pubkey/pk_codecs/pkcs8.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/pkcs8.o pubkey/pk_codecs/pkcs8.cpp + +.obj/debug-shared/x509_key.o: pubkey/pk_codecs/x509_key.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/x509_key.o pubkey/pk_codecs/x509_key.cpp + +.obj/debug-shared/pk_filts.o: pubkey/pk_filts.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/pk_filts.o pubkey/pk_filts.cpp + +.obj/debug-shared/pk_keys.o: pubkey/pk_keys.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/pk_keys.o pubkey/pk_keys.cpp + +.obj/debug-shared/pubkey.o: pubkey/pubkey.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/pubkey.o pubkey/pubkey.cpp + +.obj/debug-shared/pubkey_enums.o: pubkey/pubkey_enums.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/pubkey_enums.o pubkey/pubkey_enums.cpp + +.obj/debug-shared/rsa.o: pubkey/rsa/rsa.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/rsa.o pubkey/rsa/rsa.cpp + +.obj/debug-shared/rw.o: pubkey/rw/rw.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/rw.o pubkey/rw/rw.cpp + +.obj/debug-shared/auto_rng.o: rng/auto_rng/auto_rng.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/auto_rng.o rng/auto_rng/auto_rng.cpp + +.obj/debug-shared/hmac_rng.o: rng/hmac_rng/hmac_rng.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/hmac_rng.o rng/hmac_rng/hmac_rng.cpp + +.obj/debug-shared/randpool.o: rng/randpool/randpool.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/randpool.o rng/randpool/randpool.cpp + +.obj/debug-shared/rng.o: rng/rng.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/rng.o rng/rng.cpp + +.obj/debug-shared/x931_rng.o: rng/x931_rng/x931_rng.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/x931_rng.o rng/x931_rng/x931_rng.cpp + +.obj/debug-shared/pbkdf1.o: s2k/pbkdf1/pbkdf1.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/pbkdf1.o s2k/pbkdf1/pbkdf1.cpp + +.obj/debug-shared/pbkdf2.o: s2k/pbkdf2/pbkdf2.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/pbkdf2.o s2k/pbkdf2/pbkdf2.cpp + +.obj/debug-shared/pgp_s2k.o: s2k/pgps2k/pgp_s2k.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/pgp_s2k.o s2k/pgps2k/pgp_s2k.cpp + +.obj/debug-shared/s2k.o: s2k/s2k.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/s2k.o s2k/s2k.cpp + +.obj/debug-shared/selftest.o: selftest/selftest.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/selftest.o selftest/selftest.cpp + +.obj/debug-shared/arc4.o: stream/arc4/arc4.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/arc4.o stream/arc4/arc4.cpp + +.obj/debug-shared/salsa20.o: stream/salsa20/salsa20.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/salsa20.o stream/salsa20/salsa20.cpp + +.obj/debug-shared/stream_cipher.o: stream/stream_cipher.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/stream_cipher.o stream/stream_cipher.cpp + +.obj/debug-shared/tur_tab.o: stream/turing/tur_tab.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/tur_tab.o stream/turing/tur_tab.cpp + +.obj/debug-shared/turing.o: stream/turing/turing.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/turing.o stream/turing/turing.cpp + +.obj/debug-shared/wid_wake.o: stream/wid_wake/wid_wake.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/wid_wake.o stream/wid_wake/wid_wake.cpp + +.obj/debug-shared/symkey.o: sym_algo/symkey.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/symkey.o sym_algo/symkey.cpp + +.obj/debug-shared/timer.o: timer/timer.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/timer.o timer/timer.cpp + +.obj/debug-shared/charset.o: utils/charset.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/charset.o utils/charset.cpp + +.obj/debug-shared/datastor.o: utils/datastor/datastor.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/datastor.o utils/datastor/datastor.cpp + +.obj/debug-shared/exceptn.o: utils/exceptn.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/exceptn.o utils/exceptn.cpp + +.obj/debug-shared/mlock.o: utils/mlock.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/mlock.o utils/mlock.cpp + +.obj/debug-shared/parsing.o: utils/parsing.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/parsing.o utils/parsing.cpp + +.obj/debug-shared/ui.o: utils/ui.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/ui.o utils/ui.cpp + +.obj/debug-shared/util.o: utils/util.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/util.o utils/util.cpp + +.obj/debug-shared/version.o: utils/version.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/version.o utils/version.cpp + +.obj/debug-shared/mmap_mem.o: alloc/alloc_mmap/mmap_mem.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/mmap_mem.o alloc/alloc_mmap/mmap_mem.cpp + +.obj/debug-shared/asn1_eac_str.o: cert/cvc/asn1_eac_str.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/asn1_eac_str.o cert/cvc/asn1_eac_str.cpp + +.obj/debug-shared/asn1_eac_tm.o: cert/cvc/asn1_eac_tm.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/asn1_eac_tm.o cert/cvc/asn1_eac_tm.cpp + +.obj/debug-shared/cvc_ado.o: cert/cvc/cvc_ado.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/cvc_ado.o cert/cvc/cvc_ado.cpp + +.obj/debug-shared/cvc_ca.o: cert/cvc/cvc_ca.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/cvc_ca.o cert/cvc/cvc_ca.cpp + +.obj/debug-shared/cvc_cert.o: cert/cvc/cvc_cert.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/cvc_cert.o cert/cvc/cvc_cert.cpp + +.obj/debug-shared/cvc_req.o: cert/cvc/cvc_req.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/cvc_req.o cert/cvc/cvc_req.cpp + +.obj/debug-shared/cvc_self.o: cert/cvc/cvc_self.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/cvc_self.o cert/cvc/cvc_self.cpp + +.obj/debug-shared/ecdsa_sig.o: cert/cvc/ecdsa_sig.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/ecdsa_sig.o cert/cvc/ecdsa_sig.cpp + +.obj/debug-shared/signed_obj.o: cert/cvc/signed_obj.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/signed_obj.o cert/cvc/signed_obj.cpp + +.obj/debug-shared/es_dev.o: entropy/dev_random/es_dev.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/es_dev.o entropy/dev_random/es_dev.cpp + +.obj/debug-shared/es_egd.o: entropy/egd/es_egd.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/es_egd.o entropy/egd/es_egd.cpp + +.obj/debug-shared/es_ftw.o: entropy/proc_walk/es_ftw.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/es_ftw.o entropy/proc_walk/es_ftw.cpp + +.obj/debug-shared/es_unix.o: entropy/unix_procs/es_unix.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/es_unix.o entropy/unix_procs/es_unix.cpp + +.obj/debug-shared/unix_cmd.o: entropy/unix_procs/unix_cmd.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/unix_cmd.o entropy/unix_procs/unix_cmd.cpp + +.obj/debug-shared/unix_src.o: entropy/unix_procs/unix_src.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/unix_src.o entropy/unix_procs/unix_src.cpp + +.obj/debug-shared/fd_unix.o: filters/fd_unix/fd_unix.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/fd_unix.o filters/fd_unix/fd_unix.cpp + +.obj/debug-shared/curve_gfp.o: math/gfpmath/curve_gfp.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/curve_gfp.o math/gfpmath/curve_gfp.cpp + +.obj/debug-shared/gfp_element.o: math/gfpmath/gfp_element.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/gfp_element.o math/gfpmath/gfp_element.cpp + +.obj/debug-shared/point_gfp.o: math/gfpmath/point_gfp.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/point_gfp.o math/gfpmath/point_gfp.cpp + +.obj/debug-shared/mux_pthr.o: mutex/pthreads/mux_pthr.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/mux_pthr.o mutex/pthreads/mux_pthr.cpp + +.obj/debug-shared/ec_dompar.o: pubkey/ec_dompar/ec_dompar.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/ec_dompar.o pubkey/ec_dompar/ec_dompar.cpp + +.obj/debug-shared/ecc_key.o: pubkey/ecc_key/ecc_key.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/ecc_key.o pubkey/ecc_key/ecc_key.cpp + +.obj/debug-shared/ecdsa.o: pubkey/ecdsa/ecdsa.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/ecdsa.o pubkey/ecdsa/ecdsa.cpp + +.obj/debug-shared/ecdsa_core.o: pubkey/ecdsa/ecdsa_core.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/ecdsa_core.o pubkey/ecdsa/ecdsa_core.cpp + +.obj/debug-shared/ecdsa_op.o: pubkey/ecdsa/ecdsa_op.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/ecdsa_op.o pubkey/ecdsa/ecdsa_op.cpp + +.obj/debug-shared/eckaeg.o: pubkey/eckaeg/eckaeg.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/eckaeg.o pubkey/eckaeg/eckaeg.cpp + +.obj/debug-shared/eckaeg_core.o: pubkey/eckaeg/eckaeg_core.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/eckaeg_core.o pubkey/eckaeg/eckaeg_core.cpp + +.obj/debug-shared/eckaeg_op.o: pubkey/eckaeg/eckaeg_op.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/eckaeg_op.o pubkey/eckaeg/eckaeg_op.cpp + +.obj/debug-shared/tm_unix.o: timer/gettimeofday/tm_unix.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o .obj/debug-shared/tm_unix.o timer/gettimeofday/tm_unix.cpp + +####### Install + +install: FORCE + +uninstall: FORCE + +FORCE: + diff --git a/botan/src/algo_factory/algo_cache.h b/botan/src/algo_factory/algo_cache.h new file mode 100644 index 0000000..17ea996 --- /dev/null +++ b/botan/src/algo_factory/algo_cache.h @@ -0,0 +1,224 @@ +/** +* An algorithm cache (used by Algorithm_Factory) +*/ + +#ifndef BOTAN_ALGORITHM_CACHE_TEMPLATE_H__ +#define BOTAN_ALGORITHM_CACHE_TEMPLATE_H__ + +#include <botan/mutex.h> +#include <botan/stl_util.h> +#include <string> +#include <vector> +#include <map> + +namespace Botan { + +/** +* @param prov_name a provider name +* @return weight for this provider +*/ +u32bit static_provider_weight(const std::string& prov_name); + +/** +* Algorithm_Cache (used by Algorithm_Factory) +*/ +template<typename T> +class Algorithm_Cache + { + public: + const T* get(const std::string& algo_spec, + const std::string& pref_provider); + + /** + * Add a new algorithm implementation to the cache + */ + void add(T* algo, + const std::string& requested_name, + const std::string& provider_name); + + /** + * Set the preferred provider + */ + void set_preferred_provider(const std::string& algo_spec, + const std::string& provider); + + /** + * Return the list of providers of this algorithm + */ + std::vector<std::string> providers_of(const std::string& algo_name); + + Algorithm_Cache(Mutex* m) : mutex(m) {} + ~Algorithm_Cache(); + private: + typedef typename std::map<std::string, std::map<std::string, T*> >::iterator + algorithms_iterator; + + typedef typename std::map<std::string, T*>::iterator provider_iterator; + + algorithms_iterator find_algorithm(const std::string& algo_spec); + + Mutex* mutex; + std::map<std::string, std::string> aliases; + std::map<std::string, std::string> pref_providers; + std::map<std::string, std::map<std::string, T*> > algorithms; + }; + +/** +* Look for an algorithm implementation in the cache, also checking aliases +* Assumes object lock is held +*/ +template<typename T> +typename Algorithm_Cache<T>::algorithms_iterator +Algorithm_Cache<T>::find_algorithm(const std::string& algo_spec) + { + algorithms_iterator algo = algorithms.find(algo_spec); + + // Not found? Check if a known alias + if(algo == algorithms.end()) + { + std::map<std::string, std::string>::const_iterator alias = + aliases.find(algo_spec); + + if(alias != aliases.end()) + algo = algorithms.find(alias->second); + } + + return algo; + } + +/** +* Look for an algorithm implementation by a particular provider +*/ +template<typename T> +const T* Algorithm_Cache<T>::get(const std::string& algo_spec, + const std::string& requested_provider) + { + Mutex_Holder lock(mutex); + + algorithms_iterator algo = find_algorithm(algo_spec); + if(algo == algorithms.end()) // algo not found at all (no providers) + return 0; + + // If a provider is requested specifically, return it or fail entirely + if(requested_provider != "") + { + provider_iterator prov = algo->second.find(requested_provider); + if(prov != algo->second.end()) + return prov->second; + return 0; + } + + const T* prototype = 0; + std::string prototype_provider; + u32bit prototype_prov_weight = 0; + + const std::string pref_provider = search_map(pref_providers, algo_spec); + + for(provider_iterator i = algo->second.begin(); i != algo->second.end(); ++i) + { + const std::string prov_name = i->first; + const u32bit prov_weight = static_provider_weight(prov_name); + + // preferred prov exists, return immediately + if(prov_name == pref_provider) + return i->second; + + if(prototype == 0 || prov_weight > prototype_prov_weight) + { + prototype = i->second; + prototype_provider = i->first; + prototype_prov_weight = prov_weight; + } + } + + return prototype; + } + +/** +* Add an implementation to the cache +*/ +template<typename T> +void Algorithm_Cache<T>::add(T* algo, + const std::string& requested_name, + const std::string& provider) + { + if(!algo) + return; + + Mutex_Holder lock(mutex); + + delete algorithms[algo->name()][provider]; + algorithms[algo->name()][provider] = algo; + + if(algo->name() != requested_name && + aliases.find(requested_name) == aliases.end()) + { + aliases[requested_name] = algo->name(); + } + } + +/** +* Find the providers of this algo (if any) +*/ +template<typename T> std::vector<std::string> +Algorithm_Cache<T>::providers_of(const std::string& algo_name) + { + Mutex_Holder lock(mutex); + + std::vector<std::string> providers; + + algorithms_iterator algo = find_algorithm(algo_name); + + if(algo != algorithms.end()) + { + provider_iterator provider = algo->second.begin(); + + while(provider != algo->second.end()) + { + providers.push_back(provider->first); + ++provider; + } + } + + return providers; + } + +/** +* Set the preferred provider for an algorithm +*/ +template<typename T> +void Algorithm_Cache<T>::set_preferred_provider(const std::string& algo_spec, + const std::string& provider) + { + Mutex_Holder lock(mutex); + + pref_providers[algo_spec] = provider; + } + +/** +* Algorithm_Cache<T> Destructor +*/ +template<typename T> +Algorithm_Cache<T>::~Algorithm_Cache() + { + algorithms_iterator algo = algorithms.begin(); + + while(algo != algorithms.end()) + { + provider_iterator provider = algo->second.begin(); + + while(provider != algo->second.end()) + { + delete provider->second; + ++provider; + } + + ++algo; + } + + delete mutex; + } + +} + +#endif diff --git a/botan/src/algo_factory/algo_factory.cpp b/botan/src/algo_factory/algo_factory.cpp new file mode 100644 index 0000000..269c58c --- /dev/null +++ b/botan/src/algo_factory/algo_factory.cpp @@ -0,0 +1,286 @@ +/* +Algorithm Factory +(C) 2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/algo_factory.h> +#include <botan/algo_cache.h> +#include <botan/stl_util.h> +#include <botan/engine.h> +#include <botan/exceptn.h> + +#include <botan/block_cipher.h> +#include <botan/stream_cipher.h> +#include <botan/hash.h> +#include <botan/mac.h> + +#include <algorithm> + +namespace Botan { + +namespace { + +/** +* Template functions for the factory prototype/search algorithm +*/ +template<typename T> +T* engine_get_algo(Engine* engine, const SCAN_Name& request, + Algorithm_Factory& af) + { return 0; } + +template<> +BlockCipher* engine_get_algo(Engine* engine, const SCAN_Name& request, + Algorithm_Factory& af) + { return engine->find_block_cipher(request, af); } + +template<> +StreamCipher* engine_get_algo(Engine* engine, const SCAN_Name& request, + Algorithm_Factory& af) + { return engine->find_stream_cipher(request, af); } + +template<> +HashFunction* engine_get_algo(Engine* engine, const SCAN_Name& request, + Algorithm_Factory& af) + { return engine->find_hash(request, af); } + +template<> +MessageAuthenticationCode* engine_get_algo(Engine* engine, + const SCAN_Name& request, + Algorithm_Factory& af) + { return engine->find_mac(request, af); } + +template<typename T> +const T* factory_prototype(const std::string& algo_spec, + const std::string& provider, + const std::vector<Engine*>& engines, + Algorithm_Factory& af, + Algorithm_Cache<T>* cache) + { + if(const T* cache_hit = cache->get(algo_spec, provider)) + return cache_hit; + + SCAN_Name scan_name(algo_spec); + for(u32bit i = 0; i != engines.size(); ++i) + { + if(provider == "" || engines[i]->provider_name() == provider) + { + T* impl = engine_get_algo<T>(engines[i], scan_name, af); + if(impl) + cache->add(impl, algo_spec, engines[i]->provider_name()); + } + } + + return cache->get(algo_spec, provider); + } + +} + +/** +* Setup caches +*/ +Algorithm_Factory::Algorithm_Factory(const std::vector<Engine*>& engines_in, + Mutex_Factory& mf) + { + engines = engines_in; + + block_cipher_cache = new Algorithm_Cache<BlockCipher>(mf.make()); + stream_cipher_cache = new Algorithm_Cache<StreamCipher>(mf.make()); + hash_cache = new Algorithm_Cache<HashFunction>(mf.make()); + mac_cache = new Algorithm_Cache<MessageAuthenticationCode>(mf.make()); + } + +/** +* Delete all engines +*/ +Algorithm_Factory::~Algorithm_Factory() + { + std::for_each(engines.begin(), engines.end(), del_fun<Engine>()); + + delete block_cipher_cache; + delete stream_cipher_cache; + delete hash_cache; + delete mac_cache; + } + +/** +* Set the preferred provider for an algorithm +*/ +void Algorithm_Factory::set_preferred_provider(const std::string& algo_spec, + const std::string& provider) + { + if(prototype_block_cipher(algo_spec)) + block_cipher_cache->set_preferred_provider(algo_spec, provider); + else if(prototype_stream_cipher(algo_spec)) + stream_cipher_cache->set_preferred_provider(algo_spec, provider); + else if(prototype_hash_function(algo_spec)) + hash_cache->set_preferred_provider(algo_spec, provider); + else if(prototype_mac(algo_spec)) + mac_cache->set_preferred_provider(algo_spec, provider); + } + +/** +* Get an engine out of the list +*/ +Engine* Algorithm_Factory::get_engine_n(u32bit n) const + { + if(n >= engines.size()) + return 0; + return engines[n]; + } + +/** +* Return the possible providers of a request +* Note: assumes you don't have different types by the same name +*/ +std::vector<std::string> +Algorithm_Factory::providers_of(const std::string& algo_spec) + { + /* The checks with if(prototype_X(algo_spec)) have the effect of + forcing a full search, since otherwise there might not be any + providers at all in the cache. + */ + + if(prototype_block_cipher(algo_spec)) + return block_cipher_cache->providers_of(algo_spec); + else if(prototype_stream_cipher(algo_spec)) + return stream_cipher_cache->providers_of(algo_spec); + else if(prototype_hash_function(algo_spec)) + return hash_cache->providers_of(algo_spec); + else if(prototype_mac(algo_spec)) + return mac_cache->providers_of(algo_spec); + else + return std::vector<std::string>(); + } + +/** +* Return the prototypical block cipher corresponding to this request +*/ +const BlockCipher* +Algorithm_Factory::prototype_block_cipher(const std::string& algo_spec, + const std::string& provider) + { + return factory_prototype<BlockCipher>(algo_spec, provider, engines, + *this, block_cipher_cache); + } + +/** +* Return the prototypical stream cipher corresponding to this request +*/ +const StreamCipher* +Algorithm_Factory::prototype_stream_cipher(const std::string& algo_spec, + const std::string& provider) + { + return factory_prototype<StreamCipher>(algo_spec, provider, engines, + *this, stream_cipher_cache); + } + +/** +* Return the prototypical object corresponding to this request (if found) +*/ +const HashFunction* +Algorithm_Factory::prototype_hash_function(const std::string& algo_spec, + const std::string& provider) + { + return factory_prototype<HashFunction>(algo_spec, provider, engines, + *this, hash_cache); + } + +/** +* Return the prototypical object corresponding to this request +*/ +const MessageAuthenticationCode* +Algorithm_Factory::prototype_mac(const std::string& algo_spec, + const std::string& provider) + { + return factory_prototype<MessageAuthenticationCode>(algo_spec, provider, + engines, + *this, mac_cache); + } + +/** +* Return a new block cipher corresponding to this request +*/ +BlockCipher* +Algorithm_Factory::make_block_cipher(const std::string& algo_spec, + const std::string& provider) + { + if(const BlockCipher* proto = prototype_block_cipher(algo_spec, provider)) + return proto->clone(); + throw Algorithm_Not_Found(algo_spec); + } + +/** +* Return a new stream cipher corresponding to this request +*/ +StreamCipher* +Algorithm_Factory::make_stream_cipher(const std::string& algo_spec, + const std::string& provider) + { + if(const StreamCipher* proto = prototype_stream_cipher(algo_spec, provider)) + return proto->clone(); + throw Algorithm_Not_Found(algo_spec); + } + +/** +* Return a new object corresponding to this request +*/ +HashFunction* +Algorithm_Factory::make_hash_function(const std::string& algo_spec, + const std::string& provider) + { + if(const HashFunction* proto = prototype_hash_function(algo_spec, provider)) + return proto->clone(); + throw Algorithm_Not_Found(algo_spec); + } + +/** +* Return a new object corresponding to this request +*/ +MessageAuthenticationCode* +Algorithm_Factory::make_mac(const std::string& algo_spec, + const std::string& provider) + { + if(const MessageAuthenticationCode* proto = prototype_mac(algo_spec, provider)) + return proto->clone(); + throw Algorithm_Not_Found(algo_spec); + } + +/** +* Add a new block cipher +*/ +void Algorithm_Factory::add_block_cipher(BlockCipher* block_cipher, + const std::string& provider) + { + block_cipher_cache->add(block_cipher, block_cipher->name(), provider); + } + +/** +* Add a new stream cipher +*/ +void Algorithm_Factory::add_stream_cipher(StreamCipher* stream_cipher, + const std::string& provider) + { + stream_cipher_cache->add(stream_cipher, stream_cipher->name(), provider); + } + +/** +* Add a new hash +*/ +void Algorithm_Factory::add_hash_function(HashFunction* hash, + const std::string& provider) + { + hash_cache->add(hash, hash->name(), provider); + } + +/** +* Add a new mac +*/ +void Algorithm_Factory::add_mac(MessageAuthenticationCode* mac, + const std::string& provider) + { + mac_cache->add(mac, mac->name(), provider); + } + +} diff --git a/botan/src/algo_factory/algo_factory.h b/botan/src/algo_factory/algo_factory.h new file mode 100644 index 0000000..73e5920 --- /dev/null +++ b/botan/src/algo_factory/algo_factory.h @@ -0,0 +1,132 @@ +/** +* Algorithm Factory +* (C) 2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ALGORITHM_FACTORY_H__ +#define BOTAN_ALGORITHM_FACTORY_H__ + +#include <botan/mutex.h> +#include <string> +#include <vector> + +namespace Botan { + +/** +* Forward declarations (don't need full definitions here) +*/ +class BlockCipher; +class StreamCipher; +class HashFunction; +class MessageAuthenticationCode; + +template<typename T> class Algorithm_Cache; + +class Engine; + +/** +* Algorithm Factory +*/ +class BOTAN_DLL Algorithm_Factory + { + public: + /** + * Constructor + * @param engines_in the list of engines to use + * @param mf a mutex factory + */ + Algorithm_Factory(const std::vector<Engine*>& engines_in, + Mutex_Factory& mf); + + /** + * Destructor + */ + ~Algorithm_Factory(); + + /* + * Provider management + */ + std::vector<std::string> providers_of(const std::string& algo_spec); + + void set_preferred_provider(const std::string& algo_spec, + const std::string& provider); + + /* + * Block cipher operations + */ + const BlockCipher* + prototype_block_cipher(const std::string& algo_spec, + const std::string& provider = ""); + + BlockCipher* make_block_cipher(const std::string& algo_spec, + const std::string& provider = ""); + + void add_block_cipher(BlockCipher* hash, const std::string& provider); + + /* + * Stream cipher operations + */ + const StreamCipher* + prototype_stream_cipher(const std::string& algo_spec, + const std::string& provider = ""); + + StreamCipher* make_stream_cipher(const std::string& algo_spec, + const std::string& provider = ""); + + void add_stream_cipher(StreamCipher* hash, const std::string& provider); + + /* + * Hash function operations + */ + const HashFunction* + prototype_hash_function(const std::string& algo_spec, + const std::string& provider = ""); + + HashFunction* make_hash_function(const std::string& algo_spec, + const std::string& provider = ""); + + void add_hash_function(HashFunction* hash, const std::string& provider); + + /* + * MAC operations + */ + const MessageAuthenticationCode* + prototype_mac(const std::string& algo_spec, + const std::string& provider = ""); + + MessageAuthenticationCode* make_mac(const std::string& algo_spec, + const std::string& provider = ""); + + void add_mac(MessageAuthenticationCode* mac, + const std::string& provider); + + /* + * Deprecated + */ + class BOTAN_DLL Engine_Iterator + { + public: + class Engine* next() { return af.get_engine_n(n++); } + Engine_Iterator(const Algorithm_Factory& a) : af(a) { n = 0; } + private: + const Algorithm_Factory& af; + u32bit n; + }; + friend class Engine_Iterator; + + private: + class Engine* get_engine_n(u32bit) const; + + std::vector<class Engine*> engines; + + Algorithm_Cache<BlockCipher>* block_cipher_cache; + Algorithm_Cache<StreamCipher>* stream_cipher_cache; + Algorithm_Cache<HashFunction>* hash_cache; + Algorithm_Cache<MessageAuthenticationCode>* mac_cache; + }; + +} + +#endif diff --git a/botan/src/algo_factory/info.txt b/botan/src/algo_factory/info.txt new file mode 100644 index 0000000..dfc4223 --- /dev/null +++ b/botan/src/algo_factory/info.txt @@ -0,0 +1,21 @@ +realname "Algorithm Factory" + +load_on auto + +define ALGORITHM_FACTORY + +<add> +algo_factory.cpp +algo_factory.h +algo_cache.h +prov_weight.cpp +</add> + +<requires> +block +engine +hash +mac +mutex +stream +</requires> diff --git a/botan/src/algo_factory/prov_weight.cpp b/botan/src/algo_factory/prov_weight.cpp new file mode 100644 index 0000000..a55a8b1 --- /dev/null +++ b/botan/src/algo_factory/prov_weight.cpp @@ -0,0 +1,33 @@ +/** +* Default provider weights for Algorithm_Cache +* (C) 2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/algo_cache.h> + +namespace Botan { + +/** +* Return a static provider weighing +*/ +u32bit static_provider_weight(const std::string& prov_name) + { + /* + * Prefer asm over C++, but prefer anything over OpenSSL or GNU MP; to use + * them, set the provider explicitly for the algorithms you want + */ + + if(prov_name == "core") return 5; + if(prov_name == "ia32") return 6; + if(prov_name == "amd64") return 7; + if(prov_name == "sse2") return 8; + + if(prov_name == "openssl") return 2; + if(prov_name == "gmp") return 1; + + return 0; // other + } + +} diff --git a/botan/src/alloc/alloc_mmap/info.txt b/botan/src/alloc/alloc_mmap/info.txt new file mode 100644 index 0000000..65d9b29 --- /dev/null +++ b/botan/src/alloc/alloc_mmap/info.txt @@ -0,0 +1,30 @@ +realname "Disk Based Allocation System" + +define ALLOC_MMAP +modset unix + +load_on auto + +<add> +mmap_mem.cpp +mmap_mem.h +</add> + +<os> +linux +freebsd +dragonfly +openbsd +netbsd +solaris +qnx +darwin +tru64 + +# Only without -ansi, otherwise can't get mkstemp +#cygwin +</os> + +<requires> +mem_pool +</requires> diff --git a/botan/src/alloc/alloc_mmap/mmap_mem.cpp b/botan/src/alloc/alloc_mmap/mmap_mem.cpp new file mode 100644 index 0000000..546da7a --- /dev/null +++ b/botan/src/alloc/alloc_mmap/mmap_mem.cpp @@ -0,0 +1,124 @@ +/* +* Memory Mapping Allocator +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/mmap_mem.h> +#include <cstring> + +#include <sys/types.h> +#include <sys/mman.h> +#include <sys/stat.h> +#include <unistd.h> +#include <stdlib.h> +#include <fcntl.h> + +#ifndef MAP_FAILED + #define MAP_FAILED -1 +#endif + +namespace Botan { + +namespace { + +/* +* MemoryMapping_Allocator Exception +*/ +class MemoryMapping_Failed : public Exception + { + public: + MemoryMapping_Failed(const std::string& msg) : + Exception("MemoryMapping_Allocator: " + msg) {} + }; + +} + +/* +* Memory Map a File into Memory +*/ +void* MemoryMapping_Allocator::alloc_block(u32bit n) + { + class TemporaryFile + { + public: + int get_fd() const { return fd; } + const std::string path() const { return filepath; } + + TemporaryFile(const std::string& base) + { + const std::string path = base + "XXXXXX"; + + filepath = new char[path.length() + 1]; + std::strcpy(filepath, path.c_str()); + + mode_t old_umask = ::umask(077); + fd = ::mkstemp(filepath); + ::umask(old_umask); + } + + ~TemporaryFile() + { + delete[] filepath; + if(fd != -1 && ::close(fd) == -1) + throw MemoryMapping_Failed("Could not close file"); + } + private: + int fd; + char* filepath; + }; + + TemporaryFile file("/tmp/botan_"); + + if(file.get_fd() == -1) + throw MemoryMapping_Failed("Could not create file"); + + if(::unlink(file.path().c_str())) + throw MemoryMapping_Failed("Could not unlink file '" + file.path() + "'"); + + if(::lseek(file.get_fd(), n-1, SEEK_SET) < 0) + throw MemoryMapping_Failed("Could not seek file"); + + if(::write(file.get_fd(), "\0", 1) != 1) + throw MemoryMapping_Failed("Could not write to file"); + +#ifndef MAP_NOSYNC + #define MAP_NOSYNC 0 +#endif + + void* ptr = ::mmap(0, n, + PROT_READ | PROT_WRITE, + MAP_SHARED | MAP_NOSYNC, + file.get_fd(), 0); + + if(ptr == static_cast<void*>(MAP_FAILED)) + throw MemoryMapping_Failed("Could not map file"); + + return ptr; + } + +/* +* Remove a Memory Mapping +*/ +void MemoryMapping_Allocator::dealloc_block(void* ptr, u32bit n) + { + if(ptr == 0) + return; + + const byte PATTERNS[] = { 0x00, 0xFF, 0xAA, 0x55, 0x73, 0x8C, 0x5F, 0xA0, + 0x6E, 0x91, 0x30, 0xCF, 0xD3, 0x2C, 0xAC, 0x00 }; + + for(u32bit j = 0; j != sizeof(PATTERNS); j++) + { + std::memset(ptr, PATTERNS[j], n); + + if(::msync(ptr, n, MS_SYNC)) + throw MemoryMapping_Failed("Sync operation failed"); + } + + if(::munmap(ptr, n)) + throw MemoryMapping_Failed("Could not unmap file"); + } + +} diff --git a/botan/src/alloc/alloc_mmap/mmap_mem.h b/botan/src/alloc/alloc_mmap/mmap_mem.h new file mode 100644 index 0000000..bef166a --- /dev/null +++ b/botan/src/alloc/alloc_mmap/mmap_mem.h @@ -0,0 +1,30 @@ +/* +* Memory Mapping Allocator +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_MMAP_ALLOCATOR_H__ +#define BOTAN_MMAP_ALLOCATOR_H__ + +#include <botan/mem_pool.h> + +namespace Botan { + +/* +* Memory Mapping Allocator +*/ +class BOTAN_DLL MemoryMapping_Allocator : public Pooling_Allocator + { + public: + MemoryMapping_Allocator(Mutex* m) : Pooling_Allocator(m) {} + std::string type() const { return "mmap"; } + private: + void* alloc_block(u32bit); + void dealloc_block(void*, u32bit); + }; + +} + +#endif diff --git a/botan/src/alloc/allocate.h b/botan/src/alloc/allocate.h new file mode 100644 index 0000000..180f2c0 --- /dev/null +++ b/botan/src/alloc/allocate.h @@ -0,0 +1,37 @@ +/* +* Allocator +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ALLOCATOR_H__ +#define BOTAN_ALLOCATOR_H__ + +#include <botan/types.h> +#include <string> + +namespace Botan { + +/* +* Allocator Interface +*/ +class BOTAN_DLL Allocator + { + public: + static Allocator* get(bool); + + virtual void* allocate(u32bit) = 0; + virtual void deallocate(void*, u32bit) = 0; + + virtual std::string type() const = 0; + + virtual void init() {} + virtual void destroy() {} + + virtual ~Allocator() {} + }; + +} + +#endif diff --git a/botan/src/alloc/info.txt b/botan/src/alloc/info.txt new file mode 100644 index 0000000..fa50aa0 --- /dev/null +++ b/botan/src/alloc/info.txt @@ -0,0 +1,8 @@ +realname "Allocator" + +load_on auto + +<add> +allocate.h +secmem.h +</add> diff --git a/botan/src/alloc/mem_pool/info.txt b/botan/src/alloc/mem_pool/info.txt new file mode 100644 index 0000000..0a762cc --- /dev/null +++ b/botan/src/alloc/mem_pool/info.txt @@ -0,0 +1,12 @@ +realname "Memory Pool Allocator" + +load_on auto + +<add> +mem_pool.cpp +mem_pool.h +</add> + +<requires> +mutex +</requires> diff --git a/botan/src/alloc/mem_pool/mem_pool.cpp b/botan/src/alloc/mem_pool/mem_pool.cpp new file mode 100644 index 0000000..a6ebef3 --- /dev/null +++ b/botan/src/alloc/mem_pool/mem_pool.cpp @@ -0,0 +1,265 @@ +/* +* Pooling Allocator +* (C) 1999-2008 Jack Lloyd +* 2005 Matthew Gregan +* 2005-2006 Matt Johnston +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/mem_pool.h> +#include <botan/util.h> +#include <botan/mem_ops.h> +#include <algorithm> +#include <exception> + +namespace Botan { + +namespace { + +/* +* Memory Allocation Exception +*/ +struct Memory_Exhaustion : public std::bad_alloc + { + const char* what() const throw() + { return "Ran out of memory, allocation failed"; } + }; + +} + +/* +* Memory_Block Constructor +*/ +Pooling_Allocator::Memory_Block::Memory_Block(void* buf) + { + buffer = static_cast<byte*>(buf); + bitmap = 0; + buffer_end = buffer + (BLOCK_SIZE * BITMAP_SIZE); + } + +/* +* See if ptr is contained by this block +*/ +bool Pooling_Allocator::Memory_Block::contains(void* ptr, + u32bit length) const throw() + { + return ((buffer <= ptr) && + (buffer_end >= static_cast<byte*>(ptr) + length * BLOCK_SIZE)); + } + +/* +* Allocate some memory, if possible +*/ +byte* Pooling_Allocator::Memory_Block::alloc(u32bit n) throw() + { + if(n == 0 || n > BITMAP_SIZE) + return 0; + + if(n == BITMAP_SIZE) + { + if(bitmap) + return 0; + else + { + bitmap = ~bitmap; + return buffer; + } + } + + bitmap_type mask = (static_cast<bitmap_type>(1) << n) - 1; + u32bit offset = 0; + + while(bitmap & mask) + { + mask <<= 1; + ++offset; + + if((bitmap & mask) == 0) + break; + if(mask >> 63) + break; + } + + if(bitmap & mask) + return 0; + + bitmap |= mask; + return buffer + offset * BLOCK_SIZE; + } + +/* +* Mark this memory as free, if we own it +*/ +void Pooling_Allocator::Memory_Block::free(void* ptr, u32bit blocks) throw() + { + clear_mem(static_cast<byte*>(ptr), blocks * BLOCK_SIZE); + + const u32bit offset = (static_cast<byte*>(ptr) - buffer) / BLOCK_SIZE; + + if(offset == 0 && blocks == BITMAP_SIZE) + bitmap = ~bitmap; + else + { + for(u32bit j = 0; j != blocks; ++j) + bitmap &= ~(static_cast<bitmap_type>(1) << (j+offset)); + } + } + +/* +* Pooling_Allocator Constructor +*/ +Pooling_Allocator::Pooling_Allocator(Mutex* m) : mutex(m) + { + last_used = blocks.begin(); + } + +/* +* Pooling_Allocator Destructor +*/ +Pooling_Allocator::~Pooling_Allocator() + { + delete mutex; + if(blocks.size()) + throw Invalid_State("Pooling_Allocator: Never released memory"); + } + +/* +* Free all remaining memory +*/ +void Pooling_Allocator::destroy() + { + Mutex_Holder lock(mutex); + + blocks.clear(); + + for(u32bit j = 0; j != allocated.size(); ++j) + dealloc_block(allocated[j].first, allocated[j].second); + allocated.clear(); + } + +/* +* Allocation +*/ +void* Pooling_Allocator::allocate(u32bit n) + { + const u32bit BITMAP_SIZE = Memory_Block::bitmap_size(); + const u32bit BLOCK_SIZE = Memory_Block::block_size(); + + Mutex_Holder lock(mutex); + + if(n <= BITMAP_SIZE * BLOCK_SIZE) + { + const u32bit block_no = round_up(n, BLOCK_SIZE) / BLOCK_SIZE; + + byte* mem = allocate_blocks(block_no); + if(mem) + return mem; + + get_more_core(BOTAN_MEM_POOL_CHUNK_SIZE); + + mem = allocate_blocks(block_no); + if(mem) + return mem; + + throw Memory_Exhaustion(); + } + + void* new_buf = alloc_block(n); + if(new_buf) + return new_buf; + + throw Memory_Exhaustion(); + } + +/* +* Deallocation +*/ +void Pooling_Allocator::deallocate(void* ptr, u32bit n) + { + const u32bit BITMAP_SIZE = Memory_Block::bitmap_size(); + const u32bit BLOCK_SIZE = Memory_Block::block_size(); + + if(ptr == 0 || n == 0) + return; + + Mutex_Holder lock(mutex); + + if(n > BITMAP_SIZE * BLOCK_SIZE) + dealloc_block(ptr, n); + else + { + const u32bit block_no = round_up(n, BLOCK_SIZE) / BLOCK_SIZE; + + std::vector<Memory_Block>::iterator i = + std::lower_bound(blocks.begin(), blocks.end(), Memory_Block(ptr)); + + if(i == blocks.end() || !i->contains(ptr, block_no)) + throw Invalid_State("Pointer released to the wrong allocator"); + + i->free(ptr, block_no); + } + } + +/* +* Try to get some memory from an existing block +*/ +byte* Pooling_Allocator::allocate_blocks(u32bit n) + { + if(blocks.empty()) + return 0; + + std::vector<Memory_Block>::iterator i = last_used; + + do + { + byte* mem = i->alloc(n); + if(mem) + { + last_used = i; + return mem; + } + + ++i; + if(i == blocks.end()) + i = blocks.begin(); + } + while(i != last_used); + + return 0; + } + +/* +* Allocate more memory for the pool +*/ +void Pooling_Allocator::get_more_core(u32bit in_bytes) + { + const u32bit BITMAP_SIZE = Memory_Block::bitmap_size(); + const u32bit BLOCK_SIZE = Memory_Block::block_size(); + + const u32bit TOTAL_BLOCK_SIZE = BLOCK_SIZE * BITMAP_SIZE; + + // upper bound on allocation is 1 MiB + in_bytes = std::min<u32bit>(in_bytes, 1024 * 1024); + + const u32bit in_blocks = round_up(in_bytes, BLOCK_SIZE) / TOTAL_BLOCK_SIZE; + const u32bit to_allocate = in_blocks * TOTAL_BLOCK_SIZE; + + void* ptr = alloc_block(to_allocate); + if(ptr == 0) + throw Memory_Exhaustion(); + + allocated.push_back(std::make_pair(ptr, to_allocate)); + + for(u32bit j = 0; j != in_blocks; ++j) + { + byte* byte_ptr = static_cast<byte*>(ptr); + blocks.push_back(Memory_Block(byte_ptr + j * TOTAL_BLOCK_SIZE)); + } + + std::sort(blocks.begin(), blocks.end()); + last_used = std::lower_bound(blocks.begin(), blocks.end(), + Memory_Block(ptr)); + } + +} diff --git a/botan/src/alloc/mem_pool/mem_pool.h b/botan/src/alloc/mem_pool/mem_pool.h new file mode 100644 index 0000000..a578009 --- /dev/null +++ b/botan/src/alloc/mem_pool/mem_pool.h @@ -0,0 +1,74 @@ +/* +* Pooling Allocator +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_POOLING_ALLOCATOR_H__ +#define BOTAN_POOLING_ALLOCATOR_H__ + +#include <botan/allocate.h> +#include <botan/exceptn.h> +#include <botan/mutex.h> +#include <utility> +#include <vector> + +namespace Botan { + +/* +* Pooling Allocator +*/ +class BOTAN_DLL Pooling_Allocator : public Allocator + { + public: + void* allocate(u32bit); + void deallocate(void*, u32bit); + + void destroy(); + + Pooling_Allocator(Mutex*); + ~Pooling_Allocator(); + private: + void get_more_core(u32bit); + byte* allocate_blocks(u32bit); + + virtual void* alloc_block(u32bit) = 0; + virtual void dealloc_block(void*, u32bit) = 0; + + class BOTAN_DLL Memory_Block + { + public: + Memory_Block(void*); + + static u32bit bitmap_size() { return BITMAP_SIZE; } + static u32bit block_size() { return BLOCK_SIZE; } + + bool contains(void*, u32bit) const throw(); + byte* alloc(u32bit) throw(); + void free(void*, u32bit) throw(); + + bool operator<(const Memory_Block& other) const + { + if(buffer < other.buffer && other.buffer < buffer_end) + return false; + return (buffer < other.buffer); + } + private: + typedef u64bit bitmap_type; + static const u32bit BITMAP_SIZE = 8 * sizeof(bitmap_type); + static const u32bit BLOCK_SIZE = 64; + + bitmap_type bitmap; + byte* buffer, *buffer_end; + }; + + std::vector<Memory_Block> blocks; + std::vector<Memory_Block>::iterator last_used; + std::vector<std::pair<void*, u32bit> > allocated; + Mutex* mutex; + }; + +} + +#endif diff --git a/botan/src/alloc/secmem.h b/botan/src/alloc/secmem.h new file mode 100644 index 0000000..d64a376 --- /dev/null +++ b/botan/src/alloc/secmem.h @@ -0,0 +1,438 @@ +/* +* Secure Memory Buffers +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_SECURE_MEMORY_BUFFERS_H__ +#define BOTAN_SECURE_MEMORY_BUFFERS_H__ + +#include <botan/allocate.h> +#include <botan/mem_ops.h> +#include <algorithm> + +namespace Botan { + +/** +* This class represents variable length memory buffers. +*/ +template<typename T> +class MemoryRegion + { + public: + /** + * Find out the size of the buffer, i.e. how many objects of type T it + * contains. + * @return the size of the buffer + */ + u32bit size() const { return used; } + + /** + * Find out whether this buffer is empty. + * @return true if the buffer is empty, false otherwise + */ + bool is_empty() const { return (used == 0); } + + /** + * Find out whether this buffer is non-empty + * @return true if the buffer is non-empty, false otherwise + */ + bool has_items() const { return (used != 0); } + + /** + * Get a pointer to the first element in the buffer. + * @return a pointer to the first element in the buffer + */ + operator T* () { return buf; } + + /** + * Get a constant pointer to the first element in the buffer. + * @return a constant pointer to the first element in the buffer + */ + operator const T* () const { return buf; } + + /** + * Get a pointer to the first element in the buffer. + * @return a pointer to the first element in the buffer + */ + T* begin() { return buf; } + + /** + * Get a constant pointer to the first element in the buffer. + * @return a constant pointer to the first element in the buffer + */ + const T* begin() const { return buf; } + + /** + * Get a pointer to the last element in the buffer. + * @return a pointer to the last element in the buffer + */ + T* end() { return (buf + size()); } + + /** + * Get a constant pointer to the last element in the buffer. + * @return a constant pointer to the last element in the buffer + */ + const T* end() const { return (buf + size()); } + + /** + * Check two buffers for equality. + * @return true iff the content of both buffers is byte-wise equal + */ + bool operator==(const MemoryRegion<T>& other) const + { + return (size() == other.size() && + same_mem(buf, other.buf, size())); + } + + /** + * Compare two buffers lexicographically. + * @return true if this buffer is lexicographically smaller than other. + */ + bool operator<(const MemoryRegion<T>& other) const; + + /** + * Check two buffers for inequality. + * @return false if the content of both buffers is byte-wise equal, true + * otherwise. + */ + bool operator!=(const MemoryRegion<T>& in) const + { return (!(*this == in)); } + + /** + * Copy the contents of another buffer into this buffer. + * The former contents of *this are discarded. + * @param in the buffer to copy the contents from. + * @return a reference to *this + */ + MemoryRegion<T>& operator=(const MemoryRegion<T>& in) + { if(this != &in) set(in); return (*this); } + + /** + * The use of this function is discouraged because of the risk of memory + * errors. Use MemoryRegion<T>::set() + * instead. + * Copy the contents of an array of objects of type T into this buffer. + * The former contents of *this are discarded. + * The length of *this must be at least n, otherwise memory errors occur. + * @param in the array to copy the contents from + * @param n the length of in + */ + void copy(const T in[], u32bit n) + { copy(0, in, n); } + + /** + * The use of this function is discouraged because of the risk of memory + * errors. Use MemoryRegion<T>::set() + * instead. + * Copy the contents of an array of objects of type T into this buffer. + * The former contents of *this are discarded. + * The length of *this must be at least n, otherwise memory errors occur. + * @param off the offset position inside this buffer to start inserting + * the copied bytes + * @param in the array to copy the contents from + * @param n the length of in + */ + void copy(u32bit off, const T in[], u32bit n) + { copy_mem(buf + off, in, (n > size() - off) ? (size() - off) : n); } + + /** + * Set the contents of this according to the argument. The size of + * *this is increased if necessary. + * @param in the array of objects of type T to copy the contents from + * @param n the size of array in + */ + void set(const T in[], u32bit n) { create(n); copy(in, n); } + + /** + * Set the contents of this according to the argument. The size of + * *this is increased if necessary. + * @param in the buffer to copy the contents from + */ + void set(const MemoryRegion<T>& in) { set(in.begin(), in.size()); } + + /** + * Append data to the end of this buffer. + * @param data the array containing the data to append + * @param n the size of the array data + */ + void append(const T data[], u32bit n) + { grow_to(size()+n); copy(size() - n, data, n); } + + /** + * Append a single element. + * @param x the element to append + */ + void append(T x) { append(&x, 1); } + + /** + * Append data to the end of this buffer. + * @param data the buffer containing the data to append + */ + void append(const MemoryRegion<T>& x) { append(x.begin(), x.size()); } + + /** + * Zeroise the bytes of this buffer. The length remains unchanged. + */ + void clear() { clear_mem(buf, allocated); } + + /** + * Reset this buffer to an empty buffer with size zero. + */ + void destroy() { create(0); } + + /** + * Reset this buffer to a buffer of specified length. The content will be + * initialized to zero bytes. + * @param n the new length of the buffer + */ + void create(u32bit n); + + /** + * Preallocate memory, so that this buffer can grow up to size n without + * having to perform any actual memory allocations. (This is + * the same principle as for std::vector::reserve().) + */ + void grow_to(u32bit N); + + /** + * Swap this buffer with another object. + */ + void swap(MemoryRegion<T>& other); + + ~MemoryRegion() { deallocate(buf, allocated); } + protected: + MemoryRegion() { buf = 0; alloc = 0; used = allocated = 0; } + MemoryRegion(const MemoryRegion<T>& other) + { + buf = 0; + used = allocated = 0; + alloc = other.alloc; + set(other.buf, other.used); + } + + void init(bool locking, u32bit length = 0) + { alloc = Allocator::get(locking); create(length); } + private: + T* allocate(u32bit n) + { + return static_cast<T*>(alloc->allocate(sizeof(T)*n)); + } + + void deallocate(T* p, u32bit n) + { alloc->deallocate(p, sizeof(T)*n); } + + T* buf; + u32bit used; + u32bit allocated; + Allocator* alloc; + }; + +/* +* Create a new buffer +*/ +template<typename T> +void MemoryRegion<T>::create(u32bit n) + { + if(n <= allocated) { clear(); used = n; return; } + deallocate(buf, allocated); + buf = allocate(n); + allocated = used = n; + } + +/* +* Increase the size of the buffer +*/ +template<typename T> +void MemoryRegion<T>::grow_to(u32bit n) + { + if(n > used && n <= allocated) + { + clear_mem(buf + used, n - used); + used = n; + return; + } + else if(n > allocated) + { + T* new_buf = allocate(n); + copy_mem(new_buf, buf, used); + deallocate(buf, allocated); + buf = new_buf; + allocated = used = n; + } + } + +/* +* Compare this buffer with another one +*/ +template<typename T> +bool MemoryRegion<T>::operator<(const MemoryRegion<T>& in) const + { + if(size() < in.size()) return true; + if(size() > in.size()) return false; + + for(u32bit j = 0; j != size(); j++) + { + if(buf[j] < in[j]) return true; + if(buf[j] > in[j]) return false; + } + + return false; + } + +/* +* Swap this buffer with another one +*/ +template<typename T> +void MemoryRegion<T>::swap(MemoryRegion<T>& x) + { + std::swap(buf, x.buf); + std::swap(used, x.used); + std::swap(allocated, x.allocated); + std::swap(alloc, x.alloc); + } + +/** +* This class represents variable length buffers that do not +* make use of memory locking. +*/ +template<typename T> +class MemoryVector : public MemoryRegion<T> + { + public: + /** + * Copy the contents of another buffer into this buffer. + * @param in the buffer to copy the contents from + * @return a reference to *this + */ + MemoryVector<T>& operator=(const MemoryRegion<T>& in) + { if(this != &in) set(in); return (*this); } + + /** + * Create a buffer of the specified length. + * @param n the length of the buffer to create. + + */ + MemoryVector(u32bit n = 0) { MemoryRegion<T>::init(false, n); } + + /** + * Create a buffer with the specified contents. + * @param in the array containing the data to be initially copied + * into the newly created buffer + * @param n the size of the arry in + */ + MemoryVector(const T in[], u32bit n) + { MemoryRegion<T>::init(false); set(in, n); } + + /** + * Copy constructor. + */ + MemoryVector(const MemoryRegion<T>& in) + { MemoryRegion<T>::init(false); set(in); } + + /** + * Create a buffer whose content is the concatenation of two other + * buffers. + * @param in1 the first part of the new contents + * @param in2 the contents to be appended to in1 + */ + MemoryVector(const MemoryRegion<T>& in1, const MemoryRegion<T>& in2) + { MemoryRegion<T>::init(false); set(in1); append(in2); } + }; + +/** +* This class represents variable length buffers using the operating +* systems capability to lock memory, i.e. keeping it from being +* swapped out to disk. In this way, a security hole allowing attackers +* to find swapped out secret keys is closed. Please refer to +* Botan::InitializerOptions::secure_memory() for restrictions and +* further details. +*/ +template<typename T> +class SecureVector : public MemoryRegion<T> + { + public: + /** + * Copy the contents of another buffer into this buffer. + * @param in the buffer to copy the contents from + * @return a reference to *this + */ + SecureVector<T>& operator=(const MemoryRegion<T>& in) + { if(this != &in) set(in); return (*this); } + + /** + * Create a buffer of the specified length. + * @param n the length of the buffer to create. + + */ + SecureVector(u32bit n = 0) { MemoryRegion<T>::init(true, n); } + + /** + * Create a buffer with the specified contents. + * @param in the array containing the data to be initially copied + * into the newly created buffer + * @param n the size of the array in + */ + SecureVector(const T in[], u32bit n) + { MemoryRegion<T>::init(true); set(in, n); } + + /** + * Create a buffer with contents specified contents. + * @param in the buffer holding the contents that will be + * copied into the newly created buffer. + */ + SecureVector(const MemoryRegion<T>& in) + { MemoryRegion<T>::init(true); set(in); } + + /** + * Create a buffer whose content is the concatenation of two other + * buffers. + * @param in1 the first part of the new contents + * @param in2 the contents to be appended to in1 + */ + SecureVector(const MemoryRegion<T>& in1, const MemoryRegion<T>& in2) + { MemoryRegion<T>::init(true); set(in1); append(in2); } + }; + +/** +* This class represents fixed length buffers using the operating +* systems capability to lock memory, i.e. keeping it from being +* swapped out to disk. In this way, a security hole allowing attackers +* to find swapped out secret keys is closed. Please refer to +* Botan::InitializerOptions::secure_memory() for restrictions and +* further details. +*/ +template<typename T, u32bit L> +class SecureBuffer : public MemoryRegion<T> + { + public: + /** + * Copy the contents of another buffer into this buffer. + * @param in the buffer to copy the contents from + * @return a reference to *this + */ + SecureBuffer<T,L>& operator=(const SecureBuffer<T,L>& in) + { if(this != &in) set(in); return (*this); } + + /** + * Create a buffer of the length L. + */ + SecureBuffer() { MemoryRegion<T>::init(true, L); } + + /** + * Create a buffer of size L with the specified contents. + * @param in the array containing the data to be initially copied + * into the newly created buffer + * @param n the size of the array in + */ + SecureBuffer(const T in[], u32bit n) + { MemoryRegion<T>::init(true, L); copy(in, n); } + private: + SecureBuffer<T, L>& operator=(const MemoryRegion<T>& in) + { if(this != &in) set(in); return (*this); } + }; + +} + +#endif diff --git a/botan/src/alloc/system_alloc/defalloc.cpp b/botan/src/alloc/system_alloc/defalloc.cpp new file mode 100644 index 0000000..8791c74 --- /dev/null +++ b/botan/src/alloc/system_alloc/defalloc.cpp @@ -0,0 +1,100 @@ +/* +* Basic Allocators +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/defalloc.h> +#include <botan/libstate.h> +#include <botan/util.h> +#include <cstdlib> +#include <cstring> + +namespace Botan { + +namespace { + +/* +* Perform Memory Allocation +*/ +void* do_malloc(u32bit n, bool do_lock) + { + void* ptr = std::malloc(n); + + if(!ptr) + return 0; + + if(do_lock) + lock_mem(ptr, n); + + std::memset(ptr, 0, n); + return ptr; + } + +/* +* Perform Memory Deallocation +*/ +void do_free(void* ptr, u32bit n, bool do_lock) + { + if(!ptr) + return; + + std::memset(ptr, 0, n); + if(do_lock) + unlock_mem(ptr, n); + + std::free(ptr); + } + +} + +/* +* Malloc_Allocator's Allocation +*/ +void* Malloc_Allocator::allocate(u32bit n) + { + return do_malloc(n, false); + } + +/* +* Malloc_Allocator's Deallocation +*/ +void Malloc_Allocator::deallocate(void* ptr, u32bit n) + { + do_free(ptr, n, false); + } + +/* +* Locking_Allocator's Allocation +*/ +void* Locking_Allocator::alloc_block(u32bit n) + { + return do_malloc(n, true); + } + +/* +* Locking_Allocator's Deallocation +*/ +void Locking_Allocator::dealloc_block(void* ptr, u32bit n) + { + do_free(ptr, n, true); + } + +/* +* Get an allocator +*/ +Allocator* Allocator::get(bool locking) + { + std::string type = ""; + if(!locking) + type = "malloc"; + + Allocator* alloc = global_state().get_allocator(type); + if(alloc) + return alloc; + + throw Exception("Couldn't find an allocator to use in get_allocator"); + } + +} diff --git a/botan/src/alloc/system_alloc/defalloc.h b/botan/src/alloc/system_alloc/defalloc.h new file mode 100644 index 0000000..627e8df --- /dev/null +++ b/botan/src/alloc/system_alloc/defalloc.h @@ -0,0 +1,43 @@ +/* +* Basic Allocators +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_BASIC_ALLOC_H__ +#define BOTAN_BASIC_ALLOC_H__ + +#include <botan/mem_pool.h> + +namespace Botan { + +/* +* Malloc Allocator +*/ +class BOTAN_DLL Malloc_Allocator : public Allocator + { + public: + void* allocate(u32bit); + void deallocate(void*, u32bit); + + std::string type() const { return "malloc"; } + }; + +/* +* Locking Allocator +*/ +class BOTAN_DLL Locking_Allocator : public Pooling_Allocator + { + public: + Locking_Allocator(Mutex* m) : Pooling_Allocator(m) {} + + std::string type() const { return "locking"; } + private: + void* alloc_block(u32bit); + void dealloc_block(void*, u32bit); + }; + +} + +#endif diff --git a/botan/src/alloc/system_alloc/info.txt b/botan/src/alloc/system_alloc/info.txt new file mode 100644 index 0000000..5fade38 --- /dev/null +++ b/botan/src/alloc/system_alloc/info.txt @@ -0,0 +1,13 @@ +realname "Default (Malloc) Allocators" + +load_on auto + +<add> +defalloc.cpp +defalloc.h +</add> + +<requires> +libstate +mem_pool +</requires> diff --git a/botan/src/asn1/alg_id.cpp b/botan/src/asn1/alg_id.cpp new file mode 100644 index 0000000..94709ba --- /dev/null +++ b/botan/src/asn1/alg_id.cpp @@ -0,0 +1,103 @@ +/* +* Algorithm Identifier +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/alg_id.h> +#include <botan/der_enc.h> +#include <botan/ber_dec.h> +#include <botan/oids.h> + +namespace Botan { + +/* +* Create an AlgorithmIdentifier +*/ +AlgorithmIdentifier::AlgorithmIdentifier(const OID& alg_id, + const MemoryRegion<byte>& param) + { + oid = alg_id; + parameters = param; + } + +/* +* Create an AlgorithmIdentifier +*/ +AlgorithmIdentifier::AlgorithmIdentifier(const std::string& alg_id, + const MemoryRegion<byte>& param) + { + oid = OIDS::lookup(alg_id); + parameters = param; + } + +/* +* Create an AlgorithmIdentifier +*/ +AlgorithmIdentifier::AlgorithmIdentifier(const OID& alg_id, + Encoding_Option option) + { + const byte DER_NULL[] = { 0x05, 0x00 }; + + oid = alg_id; + if(option == USE_NULL_PARAM) + parameters.append(DER_NULL, sizeof(DER_NULL)); + } + +/* +* Create an AlgorithmIdentifier +*/ +AlgorithmIdentifier::AlgorithmIdentifier(const std::string& alg_id, + Encoding_Option option) + { + const byte DER_NULL[] = { 0x05, 0x00 }; + + oid = OIDS::lookup(alg_id); + if(option == USE_NULL_PARAM) + parameters.append(DER_NULL, sizeof(DER_NULL)); + } + +/* +* Compare two AlgorithmIdentifiers +*/ +bool operator==(const AlgorithmIdentifier& a1, const AlgorithmIdentifier& a2) + { + if(a1.oid != a2.oid) + return false; + if(a1.parameters != a2.parameters) + return false; + return true; + } + +/* +* Compare two AlgorithmIdentifiers +*/ +bool operator!=(const AlgorithmIdentifier& a1, const AlgorithmIdentifier& a2) + { + return !(a1 == a2); + } + +/* +* DER encode an AlgorithmIdentifier +*/ +void AlgorithmIdentifier::encode_into(DER_Encoder& codec) const + { + codec.start_cons(SEQUENCE) + .encode(oid) + .raw_bytes(parameters) + .end_cons(); + } + +/* +* Decode a BER encoded AlgorithmIdentifier +*/ +void AlgorithmIdentifier::decode_from(BER_Decoder& codec) + { + codec.start_cons(SEQUENCE) + .decode(oid) + .raw_bytes(parameters) + .end_cons(); + } + +} diff --git a/botan/src/asn1/alg_id.h b/botan/src/asn1/alg_id.h new file mode 100644 index 0000000..4a1ad2f --- /dev/null +++ b/botan/src/asn1/alg_id.h @@ -0,0 +1,49 @@ +/* +* Algorithm Identifier +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ALGORITHM_IDENTIFIER_H__ +#define BOTAN_ALGORITHM_IDENTIFIER_H__ + +#include <botan/asn1_int.h> +#include <botan/asn1_oid.h> +#include <string> + +namespace Botan { + +/* +* Algorithm Identifier +*/ +class BOTAN_DLL AlgorithmIdentifier : public ASN1_Object + { + public: + enum Encoding_Option { USE_NULL_PARAM }; + + void encode_into(class DER_Encoder&) const; + void decode_from(class BER_Decoder&); + + AlgorithmIdentifier() {} + AlgorithmIdentifier(const OID&, Encoding_Option); + AlgorithmIdentifier(const std::string&, Encoding_Option); + + AlgorithmIdentifier(const OID&, const MemoryRegion<byte>&); + AlgorithmIdentifier(const std::string&, const MemoryRegion<byte>&); + + OID oid; + SecureVector<byte> parameters; + }; + +/* +* Comparison Operations +*/ +bool BOTAN_DLL operator==(const AlgorithmIdentifier&, + const AlgorithmIdentifier&); +bool BOTAN_DLL operator!=(const AlgorithmIdentifier&, + const AlgorithmIdentifier&); + +} + +#endif diff --git a/botan/src/asn1/asn1_alt.cpp b/botan/src/asn1/asn1_alt.cpp new file mode 100644 index 0000000..41974ee --- /dev/null +++ b/botan/src/asn1/asn1_alt.cpp @@ -0,0 +1,228 @@ +/* +* AlternativeName +* (C) 1999-2007 Jack Lloyd +* 2007 Yves Jerschow +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/asn1_obj.h> +#include <botan/der_enc.h> +#include <botan/ber_dec.h> +#include <botan/oids.h> +#include <botan/stl_util.h> +#include <botan/charset.h> +#include <botan/parsing.h> +#include <botan/loadstor.h> + +namespace Botan { + +/* +* Create an AlternativeName +*/ +AlternativeName::AlternativeName(const std::string& email_addr, + const std::string& uri, + const std::string& dns, + const std::string& ip) + { + add_attribute("RFC822", email_addr); + add_attribute("DNS", dns); + add_attribute("URI", uri); + add_attribute("IP", ip); + } + +/* +* Add an attribute to an alternative name +*/ +void AlternativeName::add_attribute(const std::string& type, + const std::string& str) + { + if(type == "" || str == "") + return; + + typedef std::multimap<std::string, std::string>::iterator iter; + std::pair<iter, iter> range = alt_info.equal_range(type); + for(iter j = range.first; j != range.second; ++j) + if(j->second == str) + return; + + multimap_insert(alt_info, type, str); + } + +/* +* Add an OtherName field +*/ +void AlternativeName::add_othername(const OID& oid, const std::string& value, + ASN1_Tag type) + { + if(value == "") + return; + multimap_insert(othernames, oid, ASN1_String(value, type)); + } + +/* +* Get the attributes of this alternative name +*/ +std::multimap<std::string, std::string> AlternativeName::get_attributes() const + { + return alt_info; + } + +/* +* Get the otherNames +*/ +std::multimap<OID, ASN1_String> AlternativeName::get_othernames() const + { + return othernames; + } + +/* +* Return all of the alternative names +*/ +std::multimap<std::string, std::string> AlternativeName::contents() const + { + std::multimap<std::string, std::string> names; + + typedef std::multimap<std::string, std::string>::const_iterator rdn_iter; + for(rdn_iter j = alt_info.begin(); j != alt_info.end(); ++j) + multimap_insert(names, j->first, j->second); + + typedef std::multimap<OID, ASN1_String>::const_iterator on_iter; + for(on_iter j = othernames.begin(); j != othernames.end(); ++j) + multimap_insert(names, OIDS::lookup(j->first), j->second.value()); + + return names; + } + +/* +* Return if this object has anything useful +*/ +bool AlternativeName::has_items() const + { + return (alt_info.size() > 0 || othernames.size() > 0); + } + +namespace { + +/* +* DER encode an AlternativeName entry +*/ +void encode_entries(DER_Encoder& encoder, + const std::multimap<std::string, std::string>& attr, + const std::string& type, ASN1_Tag tagging) + { + typedef std::multimap<std::string, std::string>::const_iterator iter; + + std::pair<iter, iter> range = attr.equal_range(type); + for(iter j = range.first; j != range.second; ++j) + { + if(type == "RFC822" || type == "DNS" || type == "URI") + { + ASN1_String asn1_string(j->second, IA5_STRING); + encoder.add_object(tagging, CONTEXT_SPECIFIC, asn1_string.iso_8859()); + } + else if(type == "IP") + { + u32bit ip = string_to_ipv4(j->second); + byte ip_buf[4] = { 0 }; + store_be(ip, ip_buf); + encoder.add_object(tagging, CONTEXT_SPECIFIC, ip_buf, 4); + } + } + } + +} + +/* +* DER encode an AlternativeName extension +*/ +void AlternativeName::encode_into(DER_Encoder& der) const + { + der.start_cons(SEQUENCE); + + encode_entries(der, alt_info, "RFC822", ASN1_Tag(1)); + encode_entries(der, alt_info, "DNS", ASN1_Tag(2)); + encode_entries(der, alt_info, "URI", ASN1_Tag(6)); + encode_entries(der, alt_info, "IP", ASN1_Tag(7)); + + std::multimap<OID, ASN1_String>::const_iterator i; + for(i = othernames.begin(); i != othernames.end(); ++i) + { + der.start_explicit(0) + .encode(i->first) + .start_explicit(0) + .encode(i->second) + .end_explicit() + .end_explicit(); + } + + der.end_cons(); + } + +/* +* Decode a BER encoded AlternativeName +*/ +void AlternativeName::decode_from(BER_Decoder& source) + { + BER_Decoder names = source.start_cons(SEQUENCE); + + while(names.more_items()) + { + BER_Object obj = names.get_next_object(); + if((obj.class_tag != CONTEXT_SPECIFIC) && + (obj.class_tag != (CONTEXT_SPECIFIC | CONSTRUCTED))) + continue; + + ASN1_Tag tag = obj.type_tag; + + if(tag == 0) + { + BER_Decoder othername(obj.value); + + OID oid; + othername.decode(oid); + if(othername.more_items()) + { + BER_Object othername_value_outer = othername.get_next_object(); + othername.verify_end(); + + if(othername_value_outer.type_tag != ASN1_Tag(0) || + othername_value_outer.class_tag != + (CONTEXT_SPECIFIC | CONSTRUCTED) + ) + throw Decoding_Error("Invalid tags on otherName value"); + + BER_Decoder othername_value_inner(othername_value_outer.value); + + BER_Object value = othername_value_inner.get_next_object(); + othername_value_inner.verify_end(); + + ASN1_Tag value_type = value.type_tag; + + if(is_string_type(value_type) && value.class_tag == UNIVERSAL) + add_othername(oid, ASN1::to_string(value), value_type); + } + } + else if(tag == 1 || tag == 2 || tag == 6) + { + const std::string value = Charset::transcode(ASN1::to_string(obj), + LATIN1_CHARSET, + LOCAL_CHARSET); + + if(tag == 1) add_attribute("RFC822", value); + if(tag == 2) add_attribute("DNS", value); + if(tag == 6) add_attribute("URI", value); + } + else if(tag == 7) + { + if(obj.value.size() == 4) + { + u32bit ip = load_be<u32bit>(obj.value.begin(), 0); + add_attribute("IP", ipv4_to_string(ip)); + } + } + + } + } + +} diff --git a/botan/src/asn1/asn1_att.cpp b/botan/src/asn1/asn1_att.cpp new file mode 100644 index 0000000..c8d771e --- /dev/null +++ b/botan/src/asn1/asn1_att.cpp @@ -0,0 +1,60 @@ +/* +* Attribute +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/asn1_obj.h> +#include <botan/der_enc.h> +#include <botan/ber_dec.h> +#include <botan/oids.h> + +namespace Botan { + +/* +* Create an Attribute +*/ +Attribute::Attribute(const OID& attr_oid, const MemoryRegion<byte>& attr_value) + { + oid = attr_oid; + parameters = attr_value; + } + +/* +* Create an Attribute +*/ +Attribute::Attribute(const std::string& attr_oid, + const MemoryRegion<byte>& attr_value) + { + oid = OIDS::lookup(attr_oid); + parameters = attr_value; + } + +/* +* DER encode a Attribute +*/ +void Attribute::encode_into(DER_Encoder& codec) const + { + codec.start_cons(SEQUENCE) + .encode(oid) + .start_cons(SET) + .raw_bytes(parameters) + .end_cons() + .end_cons(); + } + +/* +* Decode a BER encoded Attribute +*/ +void Attribute::decode_from(BER_Decoder& codec) + { + codec.start_cons(SEQUENCE) + .decode(oid) + .start_cons(SET) + .raw_bytes(parameters) + .end_cons() + .end_cons(); + } + +} diff --git a/botan/src/asn1/asn1_dn.cpp b/botan/src/asn1/asn1_dn.cpp new file mode 100644 index 0000000..c5a132d --- /dev/null +++ b/botan/src/asn1/asn1_dn.cpp @@ -0,0 +1,304 @@ +/* +* X509_DN +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/asn1_obj.h> +#include <botan/der_enc.h> +#include <botan/ber_dec.h> +#include <botan/parsing.h> +#include <botan/stl_util.h> +#include <botan/oids.h> + +namespace Botan { + +/* +* Create an empty X509_DN +*/ +X509_DN::X509_DN() + { + } + +/* +* Create an X509_DN +*/ +X509_DN::X509_DN(const std::multimap<OID, std::string>& args) + { + std::multimap<OID, std::string>::const_iterator j; + for(j = args.begin(); j != args.end(); ++j) + add_attribute(j->first, j->second); + } + +/* +* Create an X509_DN +*/ +X509_DN::X509_DN(const std::multimap<std::string, std::string>& args) + { + std::multimap<std::string, std::string>::const_iterator j; + for(j = args.begin(); j != args.end(); ++j) + add_attribute(OIDS::lookup(j->first), j->second); + } + +/* +* Add an attribute to a X509_DN +*/ +void X509_DN::add_attribute(const std::string& type, + const std::string& str) + { + OID oid = OIDS::lookup(type); + add_attribute(oid, str); + } + +/* +* Add an attribute to a X509_DN +*/ +void X509_DN::add_attribute(const OID& oid, const std::string& str) + { + if(str == "") + return; + + typedef std::multimap<OID, ASN1_String>::iterator rdn_iter; + + std::pair<rdn_iter, rdn_iter> range = dn_info.equal_range(oid); + for(rdn_iter j = range.first; j != range.second; ++j) + if(j->second.value() == str) + return; + + multimap_insert(dn_info, oid, ASN1_String(str)); + dn_bits.destroy(); + } + +/* +* Get the attributes of this X509_DN +*/ +std::multimap<OID, std::string> X509_DN::get_attributes() const + { + typedef std::multimap<OID, ASN1_String>::const_iterator rdn_iter; + + std::multimap<OID, std::string> retval; + for(rdn_iter j = dn_info.begin(); j != dn_info.end(); ++j) + multimap_insert(retval, j->first, j->second.value()); + return retval; + } + +/* +* Get the contents of this X.500 Name +*/ +std::multimap<std::string, std::string> X509_DN::contents() const + { + typedef std::multimap<OID, ASN1_String>::const_iterator rdn_iter; + + std::multimap<std::string, std::string> retval; + for(rdn_iter j = dn_info.begin(); j != dn_info.end(); ++j) + multimap_insert(retval, OIDS::lookup(j->first), j->second.value()); + return retval; + } + +/* +* Get a single attribute type +*/ +std::vector<std::string> X509_DN::get_attribute(const std::string& attr) const + { + typedef std::multimap<OID, ASN1_String>::const_iterator rdn_iter; + + const OID oid = OIDS::lookup(deref_info_field(attr)); + std::pair<rdn_iter, rdn_iter> range = dn_info.equal_range(oid); + + std::vector<std::string> values; + for(rdn_iter j = range.first; j != range.second; ++j) + values.push_back(j->second.value()); + return values; + } + +/* +* Handle the decoding operation of a DN +*/ +void X509_DN::do_decode(const MemoryRegion<byte>& bits) + { + BER_Decoder sequence(bits); + + while(sequence.more_items()) + { + BER_Decoder rdn = sequence.start_cons(SET); + + while(rdn.more_items()) + { + OID oid; + ASN1_String str; + + rdn.start_cons(SEQUENCE) + .decode(oid) + .decode(str) + .verify_end() + .end_cons(); + + add_attribute(oid, str.value()); + } + } + + dn_bits = bits; + } + +/* +* Return the BER encoded data, if any +*/ +MemoryVector<byte> X509_DN::get_bits() const + { + return dn_bits; + } + +/* +* Deref aliases in a subject/issuer info request +*/ +std::string X509_DN::deref_info_field(const std::string& info) + { + if(info == "Name" || info == "CommonName") return "X520.CommonName"; + if(info == "SerialNumber") return "X520.SerialNumber"; + if(info == "Country") return "X520.Country"; + if(info == "Organization") return "X520.Organization"; + if(info == "Organizational Unit" || info == "OrgUnit") + return "X520.OrganizationalUnit"; + if(info == "Locality") return "X520.Locality"; + if(info == "State" || info == "Province") return "X520.State"; + if(info == "Email") return "RFC822"; + return info; + } + +/* +* Compare two X509_DNs for equality +*/ +bool operator==(const X509_DN& dn1, const X509_DN& dn2) + { + typedef std::multimap<OID, std::string>::const_iterator rdn_iter; + + std::multimap<OID, std::string> attr1 = dn1.get_attributes(); + std::multimap<OID, std::string> attr2 = dn2.get_attributes(); + + if(attr1.size() != attr2.size()) return false; + + rdn_iter p1 = attr1.begin(); + rdn_iter p2 = attr2.begin(); + + while(true) + { + if(p1 == attr1.end() && p2 == attr2.end()) + break; + if(p1 == attr1.end()) return false; + if(p2 == attr2.end()) return false; + if(p1->first != p2->first) return false; + if(!x500_name_cmp(p1->second, p2->second)) + return false; + ++p1; + ++p2; + } + return true; + } + +/* +* Compare two X509_DNs for inequality +*/ +bool operator!=(const X509_DN& dn1, const X509_DN& dn2) + { + return !(dn1 == dn2); + } + +/* +* Compare two X509_DNs +*/ +bool operator<(const X509_DN& dn1, const X509_DN& dn2) + { + typedef std::multimap<OID, std::string>::const_iterator rdn_iter; + + std::multimap<OID, std::string> attr1 = dn1.get_attributes(); + std::multimap<OID, std::string> attr2 = dn2.get_attributes(); + + if(attr1.size() < attr2.size()) return true; + if(attr1.size() > attr2.size()) return false; + + for(rdn_iter p1 = attr1.begin(); p1 != attr1.end(); ++p1) + { + std::multimap<OID, std::string>::const_iterator p2; + p2 = attr2.find(p1->first); + if(p2 == attr2.end()) return false; + if(p1->second > p2->second) return false; + if(p1->second < p2->second) return true; + } + return false; + } + +namespace { + +/* +* DER encode a RelativeDistinguishedName +*/ +void do_ava(DER_Encoder& encoder, + const std::multimap<OID, std::string>& dn_info, + ASN1_Tag string_type, const std::string& oid_str, + bool must_exist = false) + { + typedef std::multimap<OID, std::string>::const_iterator rdn_iter; + + const OID oid = OIDS::lookup(oid_str); + const bool exists = (dn_info.find(oid) != dn_info.end()); + + if(!exists && must_exist) + throw Encoding_Error("X509_DN: No entry for " + oid_str); + if(!exists) return; + + std::pair<rdn_iter, rdn_iter> range = dn_info.equal_range(oid); + + for(rdn_iter j = range.first; j != range.second; ++j) + { + encoder.start_cons(SET) + .start_cons(SEQUENCE) + .encode(oid) + .encode(ASN1_String(j->second, string_type)) + .end_cons() + .end_cons(); + } + } + +} + +/* +* DER encode a DistinguishedName +*/ +void X509_DN::encode_into(DER_Encoder& der) const + { + std::multimap<OID, std::string> dn_info = get_attributes(); + + der.start_cons(SEQUENCE); + + if(dn_bits.has_items()) + der.raw_bytes(dn_bits); + else + { + do_ava(der, dn_info, PRINTABLE_STRING, "X520.Country", true); + do_ava(der, dn_info, DIRECTORY_STRING, "X520.State"); + do_ava(der, dn_info, DIRECTORY_STRING, "X520.Locality"); + do_ava(der, dn_info, DIRECTORY_STRING, "X520.Organization"); + do_ava(der, dn_info, DIRECTORY_STRING, "X520.OrganizationalUnit"); + do_ava(der, dn_info, DIRECTORY_STRING, "X520.CommonName", true); + do_ava(der, dn_info, PRINTABLE_STRING, "X520.SerialNumber"); + } + + der.end_cons(); + } + +/* +* Decode a BER encoded DistinguishedName +*/ +void X509_DN::decode_from(BER_Decoder& source) + { + dn_info.clear(); + + source.start_cons(SEQUENCE) + .raw_bytes(dn_bits) + .end_cons(); + + do_decode(dn_bits); + } + +} diff --git a/botan/src/asn1/asn1_int.cpp b/botan/src/asn1/asn1_int.cpp new file mode 100644 index 0000000..5e18f39 --- /dev/null +++ b/botan/src/asn1/asn1_int.cpp @@ -0,0 +1,68 @@ +/* +* ASN.1 Internals +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/asn1_int.h> +#include <botan/der_enc.h> +#include <botan/ber_dec.h> +#include <botan/data_src.h> +#include <botan/parsing.h> + +namespace Botan { + +/* +* BER Decoding Exceptions +*/ +BER_Decoding_Error::BER_Decoding_Error(const std::string& str) : + Decoding_Error("BER: " + str) {} + +BER_Bad_Tag::BER_Bad_Tag(const std::string& str, ASN1_Tag tag) : + BER_Decoding_Error(str + ": " + to_string(tag)) {} + +BER_Bad_Tag::BER_Bad_Tag(const std::string& str, + ASN1_Tag tag1, ASN1_Tag tag2) : + BER_Decoding_Error(str + ": " + to_string(tag1) + "/" + to_string(tag2)) {} + +namespace ASN1 { + +/* +* Put some arbitrary bytes into a SEQUENCE +*/ +SecureVector<byte> put_in_sequence(const MemoryRegion<byte>& contents) + { + return DER_Encoder() + .start_cons(SEQUENCE) + .raw_bytes(contents) + .end_cons() + .get_contents(); + } + +/* +* Convert a BER object into a string object +*/ +std::string to_string(const BER_Object& obj) + { + return std::string(reinterpret_cast<const char*>(obj.value.begin()), + obj.value.size()); + } + +/* +* Do heuristic tests for BER data +*/ +bool maybe_BER(DataSource& source) + { + byte first_byte; + if(!source.peek_byte(first_byte)) + throw Stream_IO_Error("ASN1::maybe_BER: Source was empty"); + + if(first_byte == (SEQUENCE | CONSTRUCTED)) + return true; + return false; + } + +} + +} diff --git a/botan/src/asn1/asn1_int.h b/botan/src/asn1/asn1_int.h new file mode 100644 index 0000000..619f45b --- /dev/null +++ b/botan/src/asn1/asn1_int.h @@ -0,0 +1,108 @@ +/* +* ASN.1 Internals +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ASN1_H__ +#define BOTAN_ASN1_H__ + +#include <botan/secmem.h> +#include <botan/exceptn.h> + +namespace Botan { + +/* +* ASN.1 Type and Class Tags +*/ +enum ASN1_Tag { + UNIVERSAL = 0x00, + APPLICATION = 0x40, + CONTEXT_SPECIFIC = 0x80, + PRIVATE = 0xC0, + + CONSTRUCTED = 0x20, + + EOC = 0x00, + BOOLEAN = 0x01, + INTEGER = 0x02, + BIT_STRING = 0x03, + OCTET_STRING = 0x04, + NULL_TAG = 0x05, + OBJECT_ID = 0x06, + ENUMERATED = 0x0A, + SEQUENCE = 0x10, + SET = 0x11, + + UTF8_STRING = 0x0C, + NUMERIC_STRING = 0x12, + PRINTABLE_STRING = 0x13, + T61_STRING = 0x14, + IA5_STRING = 0x16, + VISIBLE_STRING = 0x1A, + BMP_STRING = 0x1E, + + UTC_TIME = 0x17, + GENERALIZED_TIME = 0x18, + + NO_OBJECT = 0xFF00, + DIRECTORY_STRING = 0xFF01 +}; + +/* +* Basic ASN.1 Object Interface +*/ +class BOTAN_DLL ASN1_Object + { + public: + virtual void encode_into(class DER_Encoder&) const = 0; + virtual void decode_from(class BER_Decoder&) = 0; + virtual ~ASN1_Object() {} + }; + +/* +* BER Encoded Object +*/ +class BOTAN_DLL BER_Object + { + public: + void assert_is_a(ASN1_Tag, ASN1_Tag); + + ASN1_Tag type_tag, class_tag; + SecureVector<byte> value; + }; + +/* +* ASN.1 Utility Functions +*/ +class DataSource; + +namespace ASN1 { + +SecureVector<byte> put_in_sequence(const MemoryRegion<byte>&); +std::string to_string(const BER_Object&); +bool maybe_BER(DataSource&); + +} + +/* +* General BER Decoding Error Exception +*/ +struct BER_Decoding_Error : public Decoding_Error + { + BER_Decoding_Error(const std::string&); + }; + +/* +* Exception For Incorrect BER Taggings +*/ +struct BER_Bad_Tag : public BER_Decoding_Error + { + BER_Bad_Tag(const std::string&, ASN1_Tag); + BER_Bad_Tag(const std::string&, ASN1_Tag, ASN1_Tag); + }; + +} + +#endif diff --git a/botan/src/asn1/asn1_obj.h b/botan/src/asn1/asn1_obj.h new file mode 100644 index 0000000..ea21c47 --- /dev/null +++ b/botan/src/asn1/asn1_obj.h @@ -0,0 +1,160 @@ +/* +* Common ASN.1 Objects +* (C) 1999-2007 Jack Lloyd +* 2007 Yves Jerschow +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ASN1_OBJ_H__ +#define BOTAN_ASN1_OBJ_H__ + +#include <botan/asn1_int.h> +#include <botan/asn1_oid.h> +#include <botan/alg_id.h> +#include <vector> +#include <map> + +namespace Botan { + +/* +* Attribute +*/ +class BOTAN_DLL Attribute : public ASN1_Object + { + public: + void encode_into(class DER_Encoder&) const; + void decode_from(class BER_Decoder&); + + OID oid; + MemoryVector<byte> parameters; + + Attribute() {} + Attribute(const OID&, const MemoryRegion<byte>&); + Attribute(const std::string&, const MemoryRegion<byte>&); + }; + +/* +* X.509 Time +*/ +class BOTAN_DLL X509_Time : public ASN1_Object + { + public: + void encode_into(class DER_Encoder&) const; + void decode_from(class BER_Decoder&); + + std::string as_string() const; + std::string readable_string() const; + bool time_is_set() const; + + s32bit cmp(const X509_Time&) const; + + void set_to(const std::string&); + void set_to(const std::string&, ASN1_Tag); + + X509_Time(u64bit); + X509_Time(const std::string& = ""); + X509_Time(const std::string&, ASN1_Tag); + private: + bool passes_sanity_check() const; + u32bit year, month, day, hour, minute, second; + ASN1_Tag tag; + }; + +/* +* Simple String +*/ +class BOTAN_DLL ASN1_String : public ASN1_Object + { + public: + void encode_into(class DER_Encoder&) const; + void decode_from(class BER_Decoder&); + + std::string value() const; + std::string iso_8859() const; + + ASN1_Tag tagging() const; + + ASN1_String(const std::string& = ""); + ASN1_String(const std::string&, ASN1_Tag); + private: + std::string iso_8859_str; + ASN1_Tag tag; + }; + +/* +* Distinguished Name +*/ +class BOTAN_DLL X509_DN : public ASN1_Object + { + public: + void encode_into(class DER_Encoder&) const; + void decode_from(class BER_Decoder&); + + std::multimap<OID, std::string> get_attributes() const; + std::vector<std::string> get_attribute(const std::string&) const; + + std::multimap<std::string, std::string> contents() const; + + void add_attribute(const std::string&, const std::string&); + void add_attribute(const OID&, const std::string&); + + static std::string deref_info_field(const std::string&); + + void do_decode(const MemoryRegion<byte>&); + MemoryVector<byte> get_bits() const; + + X509_DN(); + X509_DN(const std::multimap<OID, std::string>&); + X509_DN(const std::multimap<std::string, std::string>&); + private: + std::multimap<OID, ASN1_String> dn_info; + MemoryVector<byte> dn_bits; + }; + +/* +* Alternative Name +*/ +class BOTAN_DLL AlternativeName : public ASN1_Object + { + public: + void encode_into(class DER_Encoder&) const; + void decode_from(class BER_Decoder&); + + std::multimap<std::string, std::string> contents() const; + + void add_attribute(const std::string&, const std::string&); + std::multimap<std::string, std::string> get_attributes() const; + + void add_othername(const OID&, const std::string&, ASN1_Tag); + std::multimap<OID, ASN1_String> get_othernames() const; + + bool has_items() const; + + AlternativeName(const std::string& = "", const std::string& = "", + const std::string& = "", const std::string& = ""); + private: + std::multimap<std::string, std::string> alt_info; + std::multimap<OID, ASN1_String> othernames; + }; + +/* +* Comparison Operations +*/ +bool BOTAN_DLL operator==(const X509_Time&, const X509_Time&); +bool BOTAN_DLL operator!=(const X509_Time&, const X509_Time&); +bool BOTAN_DLL operator<=(const X509_Time&, const X509_Time&); +bool BOTAN_DLL operator>=(const X509_Time&, const X509_Time&); + +bool BOTAN_DLL operator==(const X509_DN&, const X509_DN&); +bool BOTAN_DLL operator!=(const X509_DN&, const X509_DN&); +bool BOTAN_DLL operator<(const X509_DN&, const X509_DN&); + +/* +* Helper Functions +*/ +bool BOTAN_DLL is_string_type(ASN1_Tag); + +} + +#endif diff --git a/botan/src/asn1/asn1_oid.cpp b/botan/src/asn1/asn1_oid.cpp new file mode 100644 index 0000000..531ceb9 --- /dev/null +++ b/botan/src/asn1/asn1_oid.cpp @@ -0,0 +1,176 @@ +/* +* ASN.1 OID +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/asn1_oid.h> +#include <botan/der_enc.h> +#include <botan/ber_dec.h> +#include <botan/bit_ops.h> +#include <botan/parsing.h> + +namespace Botan { + +/* +* ASN.1 OID Constructor +*/ +OID::OID(const std::string& oid_str) + { + if(oid_str != "") + { + id = parse_asn1_oid(oid_str); + if(id.size() < 2 || id[0] > 2) + throw Invalid_OID(oid_str); + if((id[0] == 0 || id[0] == 1) && id[1] > 39) + throw Invalid_OID(oid_str); + } + } + +/* +* Clear the current OID +*/ +void OID::clear() + { + id.clear(); + } + +/* +* Return this OID as a string +*/ +std::string OID::as_string() const + { + std::string oid_str; + for(u32bit j = 0; j != id.size(); ++j) + { + oid_str += to_string(id[j]); + if(j != id.size() - 1) + oid_str += '.'; + } + return oid_str; + } + +/* +* OID equality comparison +*/ +bool OID::operator==(const OID& oid) const + { + if(id.size() != oid.id.size()) + return false; + for(u32bit j = 0; j != id.size(); ++j) + if(id[j] != oid.id[j]) + return false; + return true; + } + +/* +* Append another component to the OID +*/ +OID& OID::operator+=(u32bit component) + { + id.push_back(component); + return (*this); + } + +/* +* Append another component to the OID +*/ +OID operator+(const OID& oid, u32bit component) + { + OID new_oid(oid); + new_oid += component; + return new_oid; + } + +/* +* OID inequality comparison +*/ +bool operator!=(const OID& a, const OID& b) + { + return !(a == b); + } + +/* +* Compare two OIDs +*/ +bool operator<(const OID& a, const OID& b) + { + std::vector<u32bit> oid1 = a.get_id(); + std::vector<u32bit> oid2 = b.get_id(); + + if(oid1.size() < oid2.size()) + return true; + if(oid1.size() > oid2.size()) + return false; + for(u32bit j = 0; j != oid1.size(); ++j) + { + if(oid1[j] < oid2[j]) + return true; + if(oid1[j] > oid2[j]) + return false; + } + return false; + } + +/* +* DER encode an OBJECT IDENTIFIER +*/ +void OID::encode_into(DER_Encoder& der) const + { + if(id.size() < 2) + throw Invalid_Argument("OID::encode_into: OID is invalid"); + + MemoryVector<byte> encoding; + encoding.append(40 * id[0] + id[1]); + + for(u32bit j = 2; j != id.size(); ++j) + { + if(id[j] == 0) + encoding.append(0); + else + { + u32bit blocks = high_bit(id[j]) + 6; + blocks = (blocks - (blocks % 7)) / 7; + + for(u32bit k = 0; k != blocks - 1; ++k) + encoding.append(0x80 | ((id[j] >> 7*(blocks-k-1)) & 0x7F)); + encoding.append(id[j] & 0x7F); + } + } + der.add_object(OBJECT_ID, UNIVERSAL, encoding); + } + +/* +* Decode a BER encoded OBJECT IDENTIFIER +*/ +void OID::decode_from(BER_Decoder& decoder) + { + BER_Object obj = decoder.get_next_object(); + if(obj.type_tag != OBJECT_ID || obj.class_tag != UNIVERSAL) + throw BER_Bad_Tag("Error decoding OID, unknown tag", + obj.type_tag, obj.class_tag); + if(obj.value.size() < 2) + throw BER_Decoding_Error("OID encoding is too short"); + + + clear(); + id.push_back(obj.value[0] / 40); + id.push_back(obj.value[0] % 40); + + u32bit j = 0; + while(j != obj.value.size() - 1) + { + u32bit component = 0; + while(j != obj.value.size() - 1) + { + ++j; + component = (component << 7) + (obj.value[j] & 0x7F); + if(!(obj.value[j] & 0x80)) + break; + } + id.push_back(component); + } + } + +} diff --git a/botan/src/asn1/asn1_oid.h b/botan/src/asn1/asn1_oid.h new file mode 100644 index 0000000..e6d077b --- /dev/null +++ b/botan/src/asn1/asn1_oid.h @@ -0,0 +1,96 @@ +/* +* ASN.1 OID +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ASN1_OID_H__ +#define BOTAN_ASN1_OID_H__ + +#include <botan/asn1_int.h> +#include <string> +#include <vector> + +namespace Botan { + +/** +* This class represents ASN.1 object identifiers. +*/ +class BOTAN_DLL OID : public ASN1_Object + { + public: + void encode_into(class DER_Encoder&) const; + void decode_from(class BER_Decoder&); + + /** + * Find out whether this OID is empty + * @return true is no OID value is set + */ + bool is_empty() const { return id.size() == 0; } + + /** + * Get this OID as list (vector) of its components. + * @return a vector representing this OID + */ + std::vector<u32bit> get_id() const { return id; } + + /** + * Get this OID as a string + * @return a string representing this OID + */ + std::string as_string() const; + + /** + * Compare two OIDs. + * @return true if they are equal, false otherwise + */ + bool operator==(const OID&) const; + + /** + * Reset this instance to an empty OID. + */ + void clear(); + + /** + * Add a component to this OID. + * @param new_comp the new component to add to the end of this OID + * @return a reference to *this + */ + OID& operator+=(u32bit new_comp); + + /** + * Construct an OID from a string. + * @param str a string in the form "a.b.c" etc., where a,b,c are numbers + */ + OID(const std::string& str = ""); + private: + std::vector<u32bit> id; + }; + +/** +* Append another component onto the OID. +* @param oid the OID to add the new component to +* @param new_comp the new component to add +*/ +OID operator+(const OID& oid, u32bit new_comp); + +/** +* Compare two OIDs. +* @param a the first OID +* @param b the second OID +* @return true if a is not equal to b +*/ +bool operator!=(const OID& a, const OID& b); + +/** +* Compare two OIDs. +* @param a the first OID +* @param b the second OID +* @return true if a is lexicographically smaller than b +*/ +bool operator<(const OID& a, const OID& b); + +} + +#endif diff --git a/botan/src/asn1/asn1_str.cpp b/botan/src/asn1/asn1_str.cpp new file mode 100644 index 0000000..25782e2 --- /dev/null +++ b/botan/src/asn1/asn1_str.cpp @@ -0,0 +1,160 @@ +/* +* Simple ASN.1 String Types +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/asn1_obj.h> +#include <botan/der_enc.h> +#include <botan/ber_dec.h> +#include <botan/charset.h> +#include <botan/parsing.h> + +namespace Botan { + +namespace { + +/* +* Choose an encoding for the string +*/ +ASN1_Tag choose_encoding(const std::string& str, + const std::string& type) + { + static const byte IS_PRINTABLE[256] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, + 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00 }; + + for(u32bit j = 0; j != str.size(); ++j) + { + if(!IS_PRINTABLE[static_cast<byte>(str[j])]) + { + if(type == "utf8") return UTF8_STRING; + if(type == "latin1") return T61_STRING; + throw Invalid_Argument("choose_encoding: Bad string type " + type); + } + } + return PRINTABLE_STRING; + } + +} + +/* +* Check if type is a known ASN.1 string type +*/ +bool is_string_type(ASN1_Tag tag) + { + if(tag == NUMERIC_STRING || tag == PRINTABLE_STRING || + tag == VISIBLE_STRING || tag == T61_STRING || tag == IA5_STRING || + tag == UTF8_STRING || tag == BMP_STRING) + return true; + return false; + } + +/* +* Create an ASN1_String +*/ +ASN1_String::ASN1_String(const std::string& str, ASN1_Tag t) : tag(t) + { + iso_8859_str = Charset::transcode(str, LOCAL_CHARSET, LATIN1_CHARSET); + + if(tag == DIRECTORY_STRING) + tag = choose_encoding(iso_8859_str, "latin1"); + + if(tag != NUMERIC_STRING && + tag != PRINTABLE_STRING && + tag != VISIBLE_STRING && + tag != T61_STRING && + tag != IA5_STRING && + tag != UTF8_STRING && + tag != BMP_STRING) + throw Invalid_Argument("ASN1_String: Unknown string type " + + to_string(tag)); + } + +/* +* Create an ASN1_String +*/ +ASN1_String::ASN1_String(const std::string& str) + { + iso_8859_str = Charset::transcode(str, LOCAL_CHARSET, LATIN1_CHARSET); + tag = choose_encoding(iso_8859_str, "latin1"); + } + +/* +* Return this string in ISO 8859-1 encoding +*/ +std::string ASN1_String::iso_8859() const + { + return iso_8859_str; + } + +/* +* Return this string in local encoding +*/ +std::string ASN1_String::value() const + { + return Charset::transcode(iso_8859_str, LATIN1_CHARSET, LOCAL_CHARSET); + } + +/* +* Return the type of this string object +*/ +ASN1_Tag ASN1_String::tagging() const + { + return tag; + } + +/* +* DER encode an ASN1_String +*/ +void ASN1_String::encode_into(DER_Encoder& encoder) const + { + std::string value = iso_8859(); + if(tagging() == UTF8_STRING) + value = Charset::transcode(value, LATIN1_CHARSET, UTF8_CHARSET); + encoder.add_object(tagging(), UNIVERSAL, value); + } + +/* +* Decode a BER encoded ASN1_String +*/ +void ASN1_String::decode_from(BER_Decoder& source) + { + BER_Object obj = source.get_next_object(); + + Character_Set charset_is; + + if(obj.type_tag == BMP_STRING) + charset_is = UCS2_CHARSET; + else if(obj.type_tag == UTF8_STRING) + charset_is = UTF8_CHARSET; + else + charset_is = LATIN1_CHARSET; + + *this = ASN1_String( + Charset::transcode(ASN1::to_string(obj), charset_is, LOCAL_CHARSET), + obj.type_tag); + } + +} diff --git a/botan/src/asn1/asn1_tm.cpp b/botan/src/asn1/asn1_tm.cpp new file mode 100644 index 0000000..f85ea12 --- /dev/null +++ b/botan/src/asn1/asn1_tm.cpp @@ -0,0 +1,297 @@ +/* +* X.509 Time Types +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/asn1_obj.h> +#include <botan/der_enc.h> +#include <botan/ber_dec.h> +#include <botan/charset.h> +#include <botan/parsing.h> +#include <ctime> + +namespace Botan { + +namespace { + +/* +* Convert a time_t to a struct tm +*/ +std::tm get_tm(u64bit timer) + { + std::time_t time_val = static_cast<std::time_t>(timer); + + std::tm* tm_p = std::gmtime(&time_val); + if(tm_p == 0) + throw Encoding_Error("X509_Time: gmtime could not encode " + + to_string(timer)); + return (*tm_p); + } + +} + +/* +* Create an X509_Time +*/ +X509_Time::X509_Time(const std::string& time_str) + { + set_to(time_str); + } + +/* +* Create an X509_Time +*/ +X509_Time::X509_Time(u64bit timer) + { + std::tm time_info = get_tm(timer); + + year = time_info.tm_year + 1900; + month = time_info.tm_mon + 1; + day = time_info.tm_mday; + hour = time_info.tm_hour; + minute = time_info.tm_min; + second = time_info.tm_sec; + + if(year >= 2050) + tag = GENERALIZED_TIME; + else + tag = UTC_TIME; + } + +/* +* Create an X509_Time +*/ +X509_Time::X509_Time(const std::string& t_spec, ASN1_Tag t) : tag(t) + { + set_to(t_spec, tag); + } + +/* +* Set the time with a human readable string +*/ +void X509_Time::set_to(const std::string& time_str) + { + if(time_str == "") + { + year = month = day = hour = minute = second = 0; + return; + } + + std::vector<std::string> params; + std::string current; + + for(u32bit j = 0; j != time_str.size(); ++j) + { + if(Charset::is_digit(time_str[j])) + current += time_str[j]; + else + { + if(current != "") + params.push_back(current); + current.clear(); + } + } + if(current != "") + params.push_back(current); + + if(params.size() < 3 || params.size() > 6) + throw Invalid_Argument("Invalid time specification " + time_str); + + year = to_u32bit(params[0]); + month = to_u32bit(params[1]); + day = to_u32bit(params[2]); + hour = (params.size() >= 4) ? to_u32bit(params[3]) : 0; + minute = (params.size() >= 5) ? to_u32bit(params[4]) : 0; + second = (params.size() == 6) ? to_u32bit(params[5]) : 0; + + if(year >= 2050) + tag = GENERALIZED_TIME; + else + tag = UTC_TIME; + + if(!passes_sanity_check()) + throw Invalid_Argument("Invalid time specification " + time_str); + } + +/* +* Set the time with an ISO time format string +*/ +void X509_Time::set_to(const std::string& t_spec, ASN1_Tag tag) + { + if(tag != GENERALIZED_TIME && tag != UTC_TIME) + throw Invalid_Argument("X509_Time: Invalid tag " + to_string(tag)); + if(tag == GENERALIZED_TIME && t_spec.size() != 13 && t_spec.size() != 15) + throw Invalid_Argument("Invalid GeneralizedTime: " + t_spec); + if(tag == UTC_TIME && t_spec.size() != 11 && t_spec.size() != 13) + throw Invalid_Argument("Invalid UTCTime: " + t_spec); + if(t_spec[t_spec.size()-1] != 'Z') + throw Invalid_Argument("Invalid time encoding: " + t_spec); + + const u32bit YEAR_SIZE = (tag == UTC_TIME) ? 2 : 4; + + std::vector<std::string> params; + std::string current; + + for(u32bit j = 0; j != YEAR_SIZE; ++j) + current += t_spec[j]; + params.push_back(current); + current.clear(); + + for(u32bit j = YEAR_SIZE; j != t_spec.size() - 1; ++j) + { + current += t_spec[j]; + if(current.size() == 2) + { + params.push_back(current); + current.clear(); + } + } + + year = to_u32bit(params[0]); + month = to_u32bit(params[1]); + day = to_u32bit(params[2]); + hour = to_u32bit(params[3]); + minute = to_u32bit(params[4]); + second = (params.size() == 6) ? to_u32bit(params[5]) : 0; + + if(tag == UTC_TIME) + { + if(year >= 50) year += 1900; + else year += 2000; + } + + if(!passes_sanity_check()) + throw Invalid_Argument("Invalid time specification " + t_spec); + } + +/* +* DER encode a X509_Time +*/ +void X509_Time::encode_into(DER_Encoder& der) const + { + if(tag != GENERALIZED_TIME && tag != UTC_TIME) + throw Invalid_Argument("X509_Time: Bad encoding tag"); + der.add_object(tag, UNIVERSAL, + Charset::transcode(as_string(), + LOCAL_CHARSET, LATIN1_CHARSET)); + } + +/* +* Decode a BER encoded X509_Time +*/ +void X509_Time::decode_from(BER_Decoder& source) + { + BER_Object ber_time = source.get_next_object(); + set_to(Charset::transcode(ASN1::to_string(ber_time), + LATIN1_CHARSET, LOCAL_CHARSET), + ber_time.type_tag); + } + +/* +* Return a string representation of the time +*/ +std::string X509_Time::as_string() const + { + if(time_is_set() == false) + throw Invalid_State("X509_Time::as_string: No time set"); + + std::string asn1rep; + if(tag == GENERALIZED_TIME) + asn1rep = to_string(year, 4); + else + { + if(year < 1950 || year >= 2050) + throw Encoding_Error("X509_Time: The time " + readable_string() + + " cannot be encoded as a UTCTime"); + u32bit asn1year = (year >= 2000) ? (year - 2000) : (year - 1900); + asn1rep = to_string(asn1year, 2); + } + asn1rep += to_string(month, 2) + to_string(day, 2); + asn1rep += to_string(hour, 2) + to_string(minute, 2) + to_string(second, 2); + asn1rep += "Z"; + return asn1rep; + } + +/* +* Return if the time has been set somehow +*/ +bool X509_Time::time_is_set() const + { + return (year != 0); + } + +/* +* Return a human readable string representation +*/ +std::string X509_Time::readable_string() const + { + if(time_is_set() == false) + throw Invalid_State("X509_Time::readable_string: No time set"); + + std::string readable; + readable += to_string(year, 4) + "/"; + readable += to_string(month ) + "/"; + readable += to_string(day ) + " "; + readable += to_string(hour ) + ":"; + readable += to_string(minute, 2) + ":"; + readable += to_string(second, 2) + " UTC"; + return readable; + } + +/* +* Do a general sanity check on the time +*/ +bool X509_Time::passes_sanity_check() const + { + if(year < 1950 || year > 2100) + return false; + if(month == 0 || month > 12) + return false; + if(day == 0 || day > 31) + return false; + if(hour >= 24 || minute > 60 || second > 60) + return false; + return true; + } + +/* +* Compare this time against another +*/ +s32bit X509_Time::cmp(const X509_Time& other) const + { + if(time_is_set() == false) + throw Invalid_State("X509_Time::cmp: No time set"); + + const s32bit EARLIER = -1, LATER = 1, SAME_TIME = 0; + + if(year < other.year) return EARLIER; + if(year > other.year) return LATER; + if(month < other.month) return EARLIER; + if(month > other.month) return LATER; + if(day < other.day) return EARLIER; + if(day > other.day) return LATER; + if(hour < other.hour) return EARLIER; + if(hour > other.hour) return LATER; + if(minute < other.minute) return EARLIER; + if(minute > other.minute) return LATER; + if(second < other.second) return EARLIER; + if(second > other.second) return LATER; + + return SAME_TIME; + } + +/* +* Compare two X509_Times for in various ways +*/ +bool operator==(const X509_Time& t1, const X509_Time& t2) + { return (t1.cmp(t2) == 0); } +bool operator!=(const X509_Time& t1, const X509_Time& t2) + { return (t1.cmp(t2) != 0); } +bool operator<=(const X509_Time& t1, const X509_Time& t2) + { return (t1.cmp(t2) <= 0); } +bool operator>=(const X509_Time& t1, const X509_Time& t2) + { return (t1.cmp(t2) >= 0); } + +} diff --git a/botan/src/asn1/ber_dec.cpp b/botan/src/asn1/ber_dec.cpp new file mode 100644 index 0000000..ce60466 --- /dev/null +++ b/botan/src/asn1/ber_dec.cpp @@ -0,0 +1,471 @@ +/* +* BER Decoder +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/ber_dec.h> +#include <botan/bigint.h> +#include <botan/loadstor.h> + +namespace Botan { + +namespace { + +/* +* BER decode an ASN.1 type tag +*/ +u32bit decode_tag(DataSource* ber, ASN1_Tag& type_tag, ASN1_Tag& class_tag) + { + byte b; + if(!ber->read_byte(b)) + { + class_tag = type_tag = NO_OBJECT; + return 0; + } + + if((b & 0x1F) != 0x1F) + { + type_tag = ASN1_Tag(b & 0x1F); + class_tag = ASN1_Tag(b & 0xE0); + return 1; + } + + u32bit tag_bytes = 1; + class_tag = ASN1_Tag(b & 0xE0); + + u32bit tag_buf = 0; + while(true) + { + if(!ber->read_byte(b)) + throw BER_Decoding_Error("Long-form tag truncated"); + if(tag_buf & 0xFF000000) + throw BER_Decoding_Error("Long-form tag overflowed 32 bits"); + ++tag_bytes; + tag_buf = (tag_buf << 7) | (b & 0x7F); + if((b & 0x80) == 0) break; + } + type_tag = ASN1_Tag(tag_buf); + return tag_bytes; + } + +/* +* Find the EOC marker +*/ +u32bit find_eoc(DataSource*); + +/* +* BER decode an ASN.1 length field +*/ +u32bit decode_length(DataSource* ber, u32bit& field_size) + { + byte b; + if(!ber->read_byte(b)) + throw BER_Decoding_Error("Length field not found"); + field_size = 1; + if((b & 0x80) == 0) + return b; + + field_size += (b & 0x7F); + if(field_size == 1) return find_eoc(ber); + if(field_size > 5) + throw BER_Decoding_Error("Length field is too large"); + + u32bit length = 0; + + for(u32bit j = 0; j != field_size - 1; ++j) + { + if(get_byte(0, length) != 0) + throw BER_Decoding_Error("Field length overflow"); + if(!ber->read_byte(b)) + throw BER_Decoding_Error("Corrupted length field"); + length = (length << 8) | b; + } + return length; + } + +/* +* BER decode an ASN.1 length field +*/ +u32bit decode_length(DataSource* ber) + { + u32bit dummy; + return decode_length(ber, dummy); + } + +/* +* Find the EOC marker +*/ +u32bit find_eoc(DataSource* ber) + { + SecureVector<byte> buffer(DEFAULT_BUFFERSIZE), data; + + while(true) + { + const u32bit got = ber->peek(buffer, buffer.size(), data.size()); + if(got == 0) + break; + data.append(buffer, got); + } + + DataSource_Memory source(data); + data.destroy(); + + u32bit length = 0; + while(true) + { + ASN1_Tag type_tag, class_tag; + u32bit tag_size = decode_tag(&source, type_tag, class_tag); + if(type_tag == NO_OBJECT) + break; + + u32bit length_size = 0; + u32bit item_size = decode_length(&source, length_size); + source.discard_next(item_size); + + length += item_size + length_size + tag_size; + + if(type_tag == EOC) + break; + } + return length; + } + +} + +/* +* Check a type invariant on BER data +*/ +void BER_Object::assert_is_a(ASN1_Tag type_tag, ASN1_Tag class_tag) + { + if(this->type_tag != type_tag || this->class_tag != class_tag) + throw BER_Decoding_Error("Tag mismatch when decoding"); + } + +/* +* Check if more objects are there +*/ +bool BER_Decoder::more_items() const + { + if(source->end_of_data() && (pushed.type_tag == NO_OBJECT)) + return false; + return true; + } + +/* +* Verify that no bytes remain in the source +*/ +BER_Decoder& BER_Decoder::verify_end() + { + if(!source->end_of_data() || (pushed.type_tag != NO_OBJECT)) + throw Invalid_State("BER_Decoder::verify_end called, but data remains"); + return (*this); + } + +/* +* Save all the bytes remaining in the source +*/ +BER_Decoder& BER_Decoder::raw_bytes(MemoryRegion<byte>& out) + { + out.destroy(); + byte buf; + while(source->read_byte(buf)) + out.append(buf); + return (*this); + } + +/* +* Discard all the bytes remaining in the source +*/ +BER_Decoder& BER_Decoder::discard_remaining() + { + byte buf; + while(source->read_byte(buf)) + ; + return (*this); + } + +/* +* Return the BER encoding of the next object +*/ +BER_Object BER_Decoder::get_next_object() + { + BER_Object next; + + if(pushed.type_tag != NO_OBJECT) + { + next = pushed; + pushed.class_tag = pushed.type_tag = NO_OBJECT; + return next; + } + + decode_tag(source, next.type_tag, next.class_tag); + if(next.type_tag == NO_OBJECT) + return next; + + u32bit length = decode_length(source); + next.value.create(length); + if(source->read(next.value, length) != length) + throw BER_Decoding_Error("Value truncated"); + + if(next.type_tag == EOC && next.class_tag == UNIVERSAL) + return get_next_object(); + + return next; + } + +/* +* Push a object back into the stream +*/ +void BER_Decoder::push_back(const BER_Object& obj) + { + if(pushed.type_tag != NO_OBJECT) + throw Invalid_State("BER_Decoder: Only one push back is allowed"); + pushed = obj; + } + +/* +* Begin decoding a CONSTRUCTED type +*/ +BER_Decoder BER_Decoder::start_cons(ASN1_Tag type_tag, + ASN1_Tag class_tag) + { + BER_Object obj = get_next_object(); + obj.assert_is_a(type_tag, ASN1_Tag(class_tag | CONSTRUCTED)); + + BER_Decoder result(obj.value, obj.value.size()); + result.parent = this; + return result; + } + +/* +* Finish decoding a CONSTRUCTED type +*/ +BER_Decoder& BER_Decoder::end_cons() + { + if(!parent) + throw Invalid_State("BER_Decoder::end_cons called with NULL parent"); + if(!source->end_of_data()) + throw Decoding_Error("BER_Decoder::end_cons called with data left"); + return (*parent); + } + +/* +* BER_Decoder Constructor +*/ +BER_Decoder::BER_Decoder(DataSource& src) + { + source = &src; + owns = false; + pushed.type_tag = pushed.class_tag = NO_OBJECT; + parent = 0; + } + +/* +* BER_Decoder Constructor + */ +BER_Decoder::BER_Decoder(const byte data[], u32bit length) + { + source = new DataSource_Memory(data, length); + owns = true; + pushed.type_tag = pushed.class_tag = NO_OBJECT; + parent = 0; + } + +/* +* BER_Decoder Constructor +*/ +BER_Decoder::BER_Decoder(const MemoryRegion<byte>& data) + { + source = new DataSource_Memory(data); + owns = true; + pushed.type_tag = pushed.class_tag = NO_OBJECT; + parent = 0; + } + +/* +* BER_Decoder Copy Constructor +*/ +BER_Decoder::BER_Decoder(const BER_Decoder& other) + { + source = other.source; + owns = false; + if(other.owns) + { + other.owns = false; + owns = true; + } + pushed.type_tag = pushed.class_tag = NO_OBJECT; + parent = other.parent; + } + +/* +* BER_Decoder Destructor +*/ +BER_Decoder::~BER_Decoder() + { + if(owns) + delete source; + source = 0; + } + +/* +* Request for an object to decode itself +*/ +BER_Decoder& BER_Decoder::decode(ASN1_Object& obj) + { + obj.decode_from(*this); + return (*this); + } + +/* +* Decode a BER encoded NULL +*/ +BER_Decoder& BER_Decoder::decode_null() + { + BER_Object obj = get_next_object(); + obj.assert_is_a(NULL_TAG, UNIVERSAL); + if(obj.value.size()) + throw BER_Decoding_Error("NULL object had nonzero size"); + return (*this); + } + +/* +* Decode a BER encoded BOOLEAN +*/ +BER_Decoder& BER_Decoder::decode(bool& out) + { + return decode(out, BOOLEAN, UNIVERSAL); + } + +/* +* Decode a small BER encoded INTEGER +*/ +BER_Decoder& BER_Decoder::decode(u32bit& out) + { + return decode(out, INTEGER, UNIVERSAL); + } + +/* +* Decode a BER encoded INTEGER +*/ +BER_Decoder& BER_Decoder::decode(BigInt& out) + { + return decode(out, INTEGER, UNIVERSAL); + } + +/* +* Decode a BER encoded BOOLEAN +*/ +BER_Decoder& BER_Decoder::decode(bool& out, + ASN1_Tag type_tag, ASN1_Tag class_tag) + { + BER_Object obj = get_next_object(); + obj.assert_is_a(type_tag, class_tag); + + if(obj.value.size() != 1) + throw BER_Decoding_Error("BER boolean value had invalid size"); + + out = (obj.value[0]) ? true : false; + return (*this); + } + +/* +* Decode a small BER encoded INTEGER +*/ +BER_Decoder& BER_Decoder::decode(u32bit& out, + ASN1_Tag type_tag, ASN1_Tag class_tag) + { + BigInt integer; + decode(integer, type_tag, class_tag); + out = integer.to_u32bit(); + return (*this); + } + +/* +* Decode a BER encoded INTEGER +*/ +BER_Decoder& BER_Decoder::decode(BigInt& out, + ASN1_Tag type_tag, ASN1_Tag class_tag) + { + BER_Object obj = get_next_object(); + obj.assert_is_a(type_tag, class_tag); + + if(obj.value.is_empty()) + out = 0; + else + { + const bool negative = (obj.value[0] & 0x80) ? true : false; + + if(negative) + { + for(u32bit j = obj.value.size(); j > 0; --j) + if(obj.value[j-1]--) + break; + for(u32bit j = 0; j != obj.value.size(); ++j) + obj.value[j] = ~obj.value[j]; + } + + out = BigInt(obj.value, obj.value.size()); + + if(negative) + out.flip_sign(); + } + + return (*this); + } + +/* +* BER decode a BIT STRING or OCTET STRING +*/ +BER_Decoder& BER_Decoder::decode(MemoryRegion<byte>& out, ASN1_Tag real_type) + { + return decode(out, real_type, real_type, UNIVERSAL); + } + +/* +* BER decode a BIT STRING or OCTET STRING +*/ +BER_Decoder& BER_Decoder::decode(MemoryRegion<byte>& buffer, + ASN1_Tag real_type, + ASN1_Tag type_tag, ASN1_Tag class_tag) + { + if(real_type != OCTET_STRING && real_type != BIT_STRING) + throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", real_type); + + BER_Object obj = get_next_object(); + obj.assert_is_a(type_tag, class_tag); + + if(real_type == OCTET_STRING) + buffer = obj.value; + else + { + if(obj.value[0] >= 8) + throw BER_Decoding_Error("Bad number of unused bits in BIT STRING"); + buffer.set(obj.value + 1, obj.value.size() - 1); + } + return (*this); + } + +/* +* Decode an OPTIONAL string type +*/ +BER_Decoder& BER_Decoder::decode_optional_string(MemoryRegion<byte>& out, + ASN1_Tag real_type, + u16bit type_no) + { + BER_Object obj = get_next_object(); + + ASN1_Tag type_tag = static_cast<ASN1_Tag>(type_no); + + out.clear(); + push_back(obj); + + if(obj.type_tag == type_tag && obj.class_tag == CONTEXT_SPECIFIC) + decode(out, real_type, type_tag, CONTEXT_SPECIFIC); + + return (*this); + } + +} diff --git a/botan/src/asn1/ber_dec.h b/botan/src/asn1/ber_dec.h new file mode 100644 index 0000000..2e38af3 --- /dev/null +++ b/botan/src/asn1/ber_dec.h @@ -0,0 +1,122 @@ +/* +* BER Decoder +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_BER_DECODER_H__ +#define BOTAN_BER_DECODER_H__ + +#include <botan/asn1_oid.h> +#include <botan/data_src.h> + +namespace Botan { + +/* +* BER Decoding Object +*/ +class BOTAN_DLL BER_Decoder + { + public: + BER_Object get_next_object(); + void push_back(const BER_Object&); + + bool more_items() const; + BER_Decoder& verify_end(); + BER_Decoder& discard_remaining(); + + BER_Decoder start_cons(ASN1_Tag, ASN1_Tag = UNIVERSAL); + BER_Decoder& end_cons(); + + BER_Decoder& raw_bytes(MemoryRegion<byte>&); + + BER_Decoder& decode_null(); + BER_Decoder& decode(bool&); + BER_Decoder& decode(u32bit&); + BER_Decoder& decode(class BigInt&); + BER_Decoder& decode(MemoryRegion<byte>&, ASN1_Tag); + + BER_Decoder& decode(bool&, ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC); + BER_Decoder& decode(u32bit&, ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC); + BER_Decoder& decode(class BigInt&, + ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC); + BER_Decoder& decode(MemoryRegion<byte>&, ASN1_Tag, + ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC); + + BER_Decoder& decode(class ASN1_Object&); + + template<typename T> + BER_Decoder& decode_optional(T&, ASN1_Tag, ASN1_Tag, const T& = T()); + + template<typename T> + BER_Decoder& decode_list(std::vector<T>&, bool = true); + + BER_Decoder& decode_optional_string(MemoryRegion<byte>&, + ASN1_Tag, u16bit); + + BER_Decoder(DataSource&); + BER_Decoder(const byte[], u32bit); + BER_Decoder(const MemoryRegion<byte>&); + BER_Decoder(const BER_Decoder&); + ~BER_Decoder(); + private: + BER_Decoder& operator=(const BER_Decoder&) { return (*this); } + + BER_Decoder* parent; + DataSource* source; + BER_Object pushed; + mutable bool owns; + }; + +/* +* Decode an OPTIONAL or DEFAULT element +*/ +template<typename T> +BER_Decoder& BER_Decoder::decode_optional(T& out, + ASN1_Tag type_tag, + ASN1_Tag class_tag, + const T& default_value) + { + BER_Object obj = get_next_object(); + + if(obj.type_tag == type_tag && obj.class_tag == class_tag) + { + if(class_tag & CONSTRUCTED) + BER_Decoder(obj.value).decode(out).verify_end(); + else + { + push_back(obj); + decode(out, type_tag, class_tag); + } + } + else + { + out = default_value; + push_back(obj); + } + + return (*this); + } + +/* +* Decode a list of homogenously typed values +*/ +template<typename T> +BER_Decoder& BER_Decoder::decode_list(std::vector<T>& vec, bool clear_it) + { + if(clear_it) + vec.clear(); + + while(more_items()) + { + T value; + decode(value); + vec.push_back(value); + } + return (*this); + } + +} + +#endif diff --git a/botan/src/asn1/der_enc.cpp b/botan/src/asn1/der_enc.cpp new file mode 100644 index 0000000..bee2694 --- /dev/null +++ b/botan/src/asn1/der_enc.cpp @@ -0,0 +1,393 @@ +/* +* DER Encoder +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/der_enc.h> +#include <botan/asn1_int.h> +#include <botan/bigint.h> +#include <botan/loadstor.h> +#include <botan/bit_ops.h> +#include <botan/parsing.h> +#include <algorithm> + +namespace Botan { + +namespace { + +/* +* DER encode an ASN.1 type tag +*/ +SecureVector<byte> encode_tag(ASN1_Tag type_tag, ASN1_Tag class_tag) + { + if((class_tag | 0xE0) != 0xE0) + throw Encoding_Error("DER_Encoder: Invalid class tag " + + to_string(class_tag)); + + SecureVector<byte> encoded_tag; + if(type_tag <= 30) + encoded_tag.append(static_cast<byte>(type_tag | class_tag)); + else + { + u32bit blocks = high_bit(type_tag) + 6; + blocks = (blocks - (blocks % 7)) / 7; + + encoded_tag.append(class_tag | 0x1F); + for(u32bit k = 0; k != blocks - 1; ++k) + encoded_tag.append(0x80 | ((type_tag >> 7*(blocks-k-1)) & 0x7F)); + encoded_tag.append(type_tag & 0x7F); + } + + return encoded_tag; + } + +/* +* DER encode an ASN.1 length field +*/ +SecureVector<byte> encode_length(u32bit length) + { + SecureVector<byte> encoded_length; + if(length <= 127) + encoded_length.append(static_cast<byte>(length)); + else + { + const u32bit top_byte = significant_bytes(length); + encoded_length.append(static_cast<byte>(0x80 | top_byte)); + for(u32bit j = 4-top_byte; j != 4; ++j) + encoded_length.append(get_byte(j, length)); + } + return encoded_length; + } + +} + +/* +* Return the encoded SEQUENCE/SET +*/ +SecureVector<byte> DER_Encoder::DER_Sequence::get_contents() + { + const ASN1_Tag real_class_tag = ASN1_Tag(class_tag | CONSTRUCTED); + + SecureVector<byte> encoded_tag = encode_tag(type_tag, real_class_tag); + + if(type_tag == SET) + { + std::sort(set_contents.begin(), set_contents.end()); + for(u32bit j = 0; j != set_contents.size(); ++j) + contents.append(set_contents[j]); + set_contents.clear(); + } + + SecureVector<byte> encoded_length = encode_length(contents.size()); + + SecureVector<byte> retval; + retval.append(encoded_tag); + retval.append(encoded_length); + retval.append(contents); + contents.destroy(); + return retval; + } + +/* +* Add an encoded value to the SEQUENCE/SET +*/ +void DER_Encoder::DER_Sequence::add_bytes(const byte data[], u32bit length) + { + if(type_tag == SET) + set_contents.push_back(SecureVector<byte>(data, length)); + else + contents.append(data, length); + } + +/* +* Return the type and class taggings +*/ +ASN1_Tag DER_Encoder::DER_Sequence::tag_of() const + { + return ASN1_Tag(type_tag | class_tag); + } + +/* +* DER_Sequence Constructor +*/ +DER_Encoder::DER_Sequence::DER_Sequence(ASN1_Tag t1, ASN1_Tag t2) : + type_tag(t1), class_tag(t2) + { + } + +/* +* Return the encoded contents +*/ +SecureVector<byte> DER_Encoder::get_contents() + { + if(subsequences.size() != 0) + throw Invalid_State("DER_Encoder: Sequence hasn't been marked done"); + + SecureVector<byte> retval; + retval = contents; + contents.destroy(); + return retval; + } + +/* +* Start a new ASN.1 SEQUENCE/SET/EXPLICIT +*/ +DER_Encoder& DER_Encoder::start_cons(ASN1_Tag type_tag, + ASN1_Tag class_tag) + { + subsequences.push_back(DER_Sequence(type_tag, class_tag)); + return (*this); + } + +/* +* Finish the current ASN.1 SEQUENCE/SET/EXPLICIT +*/ +DER_Encoder& DER_Encoder::end_cons() + { + if(subsequences.empty()) + throw Invalid_State("DER_Encoder::end_cons: No such sequence"); + + SecureVector<byte> seq = subsequences[subsequences.size()-1].get_contents(); + subsequences.pop_back(); + raw_bytes(seq); + return (*this); + } + +/* +* Start a new ASN.1 EXPLICIT encoding +*/ +DER_Encoder& DER_Encoder::start_explicit(u16bit type_no) + { + ASN1_Tag type_tag = static_cast<ASN1_Tag>(type_no); + + if(type_tag == SET) + throw Internal_Error("DER_Encoder.start_explicit(SET); cannot perform"); + + return start_cons(type_tag, CONTEXT_SPECIFIC); + } + +/* +* Finish the current ASN.1 EXPLICIT encoding +*/ +DER_Encoder& DER_Encoder::end_explicit() + { + return end_cons(); + } + +/* +* Write raw bytes into the stream +*/ +DER_Encoder& DER_Encoder::raw_bytes(const MemoryRegion<byte>& val) + { + return raw_bytes(val.begin(), val.size()); + } + +/* +* Write raw bytes into the stream +*/ +DER_Encoder& DER_Encoder::raw_bytes(const byte bytes[], u32bit length) + { + if(subsequences.size()) + subsequences[subsequences.size()-1].add_bytes(bytes, length); + else + contents.append(bytes, length); + + return (*this); + } + +/* +* Encode a NULL object +*/ +DER_Encoder& DER_Encoder::encode_null() + { + return add_object(NULL_TAG, UNIVERSAL, 0, 0); + } + +/* +* DER encode a BOOLEAN +*/ +DER_Encoder& DER_Encoder::encode(bool is_true) + { + return encode(is_true, BOOLEAN, UNIVERSAL); + } + +/* +* DER encode a small INTEGER +*/ +DER_Encoder& DER_Encoder::encode(u32bit n) + { + return encode(BigInt(n), INTEGER, UNIVERSAL); + } + +/* +* DER encode a small INTEGER +*/ +DER_Encoder& DER_Encoder::encode(const BigInt& n) + { + return encode(n, INTEGER, UNIVERSAL); + } + +/* +* DER encode an OCTET STRING or BIT STRING +*/ +DER_Encoder& DER_Encoder::encode(const MemoryRegion<byte>& bytes, + ASN1_Tag real_type) + { + return encode(bytes.begin(), bytes.size(), + real_type, real_type, UNIVERSAL); + } + +/* +* Encode this object +*/ +DER_Encoder& DER_Encoder::encode(const byte bytes[], u32bit length, + ASN1_Tag real_type) + { + return encode(bytes, length, real_type, real_type, UNIVERSAL); + } + +/* +* DER encode a BOOLEAN +*/ +DER_Encoder& DER_Encoder::encode(bool is_true, + ASN1_Tag type_tag, ASN1_Tag class_tag) + { + byte val = is_true ? 0xFF : 0x00; + return add_object(type_tag, class_tag, &val, 1); + } + +/* +* DER encode a small INTEGER +*/ +DER_Encoder& DER_Encoder::encode(u32bit n, + ASN1_Tag type_tag, ASN1_Tag class_tag) + { + return encode(BigInt(n), type_tag, class_tag); + } + +/* +* DER encode an INTEGER +*/ +DER_Encoder& DER_Encoder::encode(const BigInt& n, + ASN1_Tag type_tag, ASN1_Tag class_tag) + { + if(n == 0) + return add_object(type_tag, class_tag, 0); + + bool extra_zero = (n.bits() % 8 == 0); + SecureVector<byte> contents(extra_zero + n.bytes()); + BigInt::encode(contents.begin() + extra_zero, n); + if(n < 0) + { + for(u32bit j = 0; j != contents.size(); ++j) + contents[j] = ~contents[j]; + for(u32bit j = contents.size(); j > 0; --j) + if(++contents[j-1]) + break; + } + + return add_object(type_tag, class_tag, contents); + } + +/* +* DER encode an OCTET STRING or BIT STRING +*/ +DER_Encoder& DER_Encoder::encode(const MemoryRegion<byte>& bytes, + ASN1_Tag real_type, + ASN1_Tag type_tag, ASN1_Tag class_tag) + { + return encode(bytes.begin(), bytes.size(), + real_type, type_tag, class_tag); + } + +/* +* DER encode an OCTET STRING or BIT STRING +*/ +DER_Encoder& DER_Encoder::encode(const byte bytes[], u32bit length, + ASN1_Tag real_type, + ASN1_Tag type_tag, ASN1_Tag class_tag) + { + if(real_type != OCTET_STRING && real_type != BIT_STRING) + throw Invalid_Argument("DER_Encoder: Invalid tag for byte/bit string"); + + if(real_type == BIT_STRING) + { + SecureVector<byte> encoded; + encoded.append(0); + encoded.append(bytes, length); + return add_object(type_tag, class_tag, encoded); + } + else + return add_object(type_tag, class_tag, bytes, length); + } + +/* +* Conditionally write some values to the stream +*/ +DER_Encoder& DER_Encoder::encode_if(bool cond, DER_Encoder& codec) + { + if(cond) + return raw_bytes(codec.get_contents()); + return (*this); + } + +/* +* Request for an object to encode itself +*/ +DER_Encoder& DER_Encoder::encode(const ASN1_Object& obj) + { + obj.encode_into(*this); + return (*this); + } + +/* +* Write the encoding of the byte(s) +*/ +DER_Encoder& DER_Encoder::add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, + const byte rep[], u32bit length) + { + SecureVector<byte> encoded_tag = encode_tag(type_tag, class_tag); + SecureVector<byte> encoded_length = encode_length(length); + + SecureVector<byte> buffer; + buffer.append(encoded_tag); + buffer.append(encoded_length); + buffer.append(rep, length); + + return raw_bytes(buffer); + } + +/* +* Write the encoding of the byte(s) +*/ +DER_Encoder& DER_Encoder::add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, + const MemoryRegion<byte>& rep_buf) + { + const byte* rep = rep_buf.begin(); + const u32bit rep_len = rep_buf.size(); + return add_object(type_tag, class_tag, rep, rep_len); + } + +/* +* Write the encoding of the byte(s) +*/ +DER_Encoder& DER_Encoder::add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, + const std::string& rep_str) + { + const byte* rep = reinterpret_cast<const byte*>(rep_str.data()); + const u32bit rep_len = rep_str.size(); + return add_object(type_tag, class_tag, rep, rep_len); + } + +/* +* Write the encoding of the byte +*/ +DER_Encoder& DER_Encoder::add_object(ASN1_Tag type_tag, + ASN1_Tag class_tag, byte rep) + { + return add_object(type_tag, class_tag, &rep, 1); + } + +} diff --git a/botan/src/asn1/der_enc.h b/botan/src/asn1/der_enc.h new file mode 100644 index 0000000..23b5297 --- /dev/null +++ b/botan/src/asn1/der_enc.h @@ -0,0 +1,91 @@ +/* +* DER Encoder +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_DER_ENCODER_H__ +#define BOTAN_DER_ENCODER_H__ + +#include <botan/asn1_int.h> +#include <vector> + +namespace Botan { + +/* +* General DER Encoding Object +*/ +class BOTAN_DLL DER_Encoder + { + public: + SecureVector<byte> get_contents(); + + DER_Encoder& start_cons(ASN1_Tag, ASN1_Tag = UNIVERSAL); + DER_Encoder& end_cons(); + + DER_Encoder& start_explicit(u16bit); + DER_Encoder& end_explicit(); + + DER_Encoder& raw_bytes(const byte[], u32bit); + DER_Encoder& raw_bytes(const MemoryRegion<byte>&); + + DER_Encoder& encode_null(); + DER_Encoder& encode(bool); + DER_Encoder& encode(u32bit); + DER_Encoder& encode(const class BigInt&); + DER_Encoder& encode(const MemoryRegion<byte>&, ASN1_Tag); + DER_Encoder& encode(const byte[], u32bit, ASN1_Tag); + + DER_Encoder& encode(bool, ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC); + DER_Encoder& encode(u32bit, ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC); + DER_Encoder& encode(const class BigInt&, ASN1_Tag, + ASN1_Tag = CONTEXT_SPECIFIC); + DER_Encoder& encode(const MemoryRegion<byte>&, ASN1_Tag, + ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC); + DER_Encoder& encode(const byte[], u32bit, ASN1_Tag, + ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC); + + template<typename T> + DER_Encoder& encode_optional(const T& value, const T& default_value) + { + if(value != default_value) + encode(value); + return (*this); + } + + template<typename T> + DER_Encoder& encode_list(const std::vector<T>& values) + { + for(u32bit j = 0; j != values.size(); ++j) + encode(values[j]); + return (*this); + } + + DER_Encoder& encode(const class ASN1_Object&); + DER_Encoder& encode_if(bool, DER_Encoder&); + + DER_Encoder& add_object(ASN1_Tag, ASN1_Tag, const byte[], u32bit); + DER_Encoder& add_object(ASN1_Tag, ASN1_Tag, const MemoryRegion<byte>&); + DER_Encoder& add_object(ASN1_Tag, ASN1_Tag, const std::string&); + DER_Encoder& add_object(ASN1_Tag, ASN1_Tag, byte); + private: + class DER_Sequence + { + public: + ASN1_Tag tag_of() const; + SecureVector<byte> get_contents(); + void add_bytes(const byte[], u32bit); + DER_Sequence(ASN1_Tag, ASN1_Tag); + private: + ASN1_Tag type_tag, class_tag; + SecureVector<byte> contents; + std::vector< SecureVector<byte> > set_contents; + }; + SecureVector<byte> contents; + std::vector<DER_Sequence> subsequences; + }; + +} + +#endif diff --git a/botan/src/asn1/info.txt b/botan/src/asn1/info.txt new file mode 100644 index 0000000..7b8110c --- /dev/null +++ b/botan/src/asn1/info.txt @@ -0,0 +1,31 @@ +realname "ASN.1/BER/DER module" + +define ASN1 + +load_on auto + +<add> +alg_id.cpp +asn1_alt.cpp +asn1_att.cpp +asn1_dn.cpp +asn1_int.cpp +asn1_oid.cpp +asn1_str.cpp +asn1_tm.cpp +ber_dec.cpp +der_enc.cpp +alg_id.h +asn1_int.h +asn1_obj.h +asn1_oid.h +ber_dec.h +der_enc.h +</add> + +<requires> +alloc +bigint +filters +oid_lookup +</requires> diff --git a/botan/src/benchmark/benchmark.cpp b/botan/src/benchmark/benchmark.cpp new file mode 100644 index 0000000..01e3b94 --- /dev/null +++ b/botan/src/benchmark/benchmark.cpp @@ -0,0 +1,189 @@ +/** +* Runtime benchmarking +* (C) 2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/benchmark.h> +#include <botan/buf_comp.h> +#include <botan/block_cipher.h> +#include <botan/stream_cipher.h> +#include <botan/hash.h> +#include <botan/mac.h> +#include <botan/util.h> +#include <memory> + +namespace Botan { + +namespace { + +/** +* Benchmark BufferedComputation (hash or MAC) +*/ +std::pair<u64bit, u64bit> bench_buf_comp(BufferedComputation* buf_comp, + Timer& timer, + u64bit nanoseconds_max, + const byte buf[], u32bit buf_len) + { + const u64bit start = timer.clock(); + u64bit nanoseconds_used = 0; + u64bit reps = 0; + + while(nanoseconds_used < nanoseconds_max) + { + buf_comp->update(buf, buf_len); + ++reps; + nanoseconds_used = timer.clock() - start; + } + + return std::make_pair(reps * buf_len, nanoseconds_used); + } + +/** +* Benchmark block cipher +*/ +std::pair<u64bit, u64bit> +bench_block_cipher(BlockCipher* block_cipher, + Timer& timer, + u64bit nanoseconds_max, + byte buf[], u32bit buf_len) + { + const u64bit start = timer.clock(); + u64bit nanoseconds_used = 0; + u64bit reps = 0; + + const u32bit in_blocks = buf_len / block_cipher->BLOCK_SIZE; + + while(nanoseconds_used < nanoseconds_max) + { + for(u32bit i = 0; i != in_blocks; ++i) + block_cipher->encrypt(buf + block_cipher->BLOCK_SIZE * i); + + ++reps; + nanoseconds_used = timer.clock() - start; + } + + return std::make_pair(reps * in_blocks * block_cipher->BLOCK_SIZE, + nanoseconds_used); + } + +/** +* Benchmark stream +*/ +std::pair<u64bit, u64bit> +bench_stream_cipher(StreamCipher* stream_cipher, + Timer& timer, + u64bit nanoseconds_max, + byte buf[], u32bit buf_len) + { + const u64bit start = timer.clock(); + u64bit nanoseconds_used = 0; + u64bit reps = 0; + + while(nanoseconds_used < nanoseconds_max) + { + stream_cipher->encrypt(buf, buf_len); + ++reps; + nanoseconds_used = timer.clock() - start; + } + + return std::make_pair(reps * buf_len, nanoseconds_used); + } + +/** +* Benchmark hash +*/ +std::pair<u64bit, u64bit> +bench_hash(HashFunction* hash, Timer& timer, + u64bit nanoseconds_max, + const byte buf[], u32bit buf_len) + { + return bench_buf_comp(hash, timer, nanoseconds_max, buf, buf_len); + } + +/** +* Benchmark MAC +*/ +std::pair<u64bit, u64bit> +bench_mac(MessageAuthenticationCode* mac, + Timer& timer, + u64bit nanoseconds_max, + const byte buf[], u32bit buf_len) + { + mac->set_key(buf, mac->MAXIMUM_KEYLENGTH); + return bench_buf_comp(mac, timer, nanoseconds_max, buf, buf_len); + } + +} + +std::map<std::string, double> +algorithm_benchmark(const std::string& name, + u32bit milliseconds, + Timer& timer, + RandomNumberGenerator& rng, + Algorithm_Factory& af) + { + std::vector<std::string> providers = af.providers_of(name); + std::map<std::string, double> all_results; + + if(providers.empty()) // no providers, nothing to do + return all_results; + + const u64bit ns_per_provider = + ((u64bit)milliseconds * 1000 * 1000) / providers.size(); + + std::vector<byte> buf(16 * 1024); + rng.randomize(&buf[0], buf.size()); + + for(u32bit i = 0; i != providers.size(); ++i) + { + const std::string provider = providers[i]; + + std::pair<u64bit, u64bit> results(0, 0); + + if(const BlockCipher* proto = + af.prototype_block_cipher(name, provider)) + { + std::auto_ptr<BlockCipher> block_cipher(proto->clone()); + results = bench_block_cipher(block_cipher.get(), timer, + ns_per_provider, + &buf[0], buf.size()); + } + else if(const StreamCipher* proto = + af.prototype_stream_cipher(name, provider)) + { + std::auto_ptr<StreamCipher> stream_cipher(proto->clone()); + results = bench_stream_cipher(stream_cipher.get(), timer, + ns_per_provider, + &buf[0], buf.size()); + } + else if(const HashFunction* proto = + af.prototype_hash_function(name, provider)) + { + std::auto_ptr<HashFunction> hash(proto->clone()); + results = bench_hash(hash.get(), timer, ns_per_provider, + &buf[0], buf.size()); + } + else if(const MessageAuthenticationCode* proto = + af.prototype_mac(name, provider)) + { + std::auto_ptr<MessageAuthenticationCode> mac(proto->clone()); + results = bench_mac(mac.get(), timer, ns_per_provider, + &buf[0], buf.size()); + } + + if(results.first && results.second) + { + /* 953.67 == 1000 * 1000 * 1000 / 1024 / 1024 - the conversion + factor from bytes per nanosecond to mebibytes per second. + */ + double speed = (953.67 * results.first) / results.second; + all_results[provider] = speed; + } + } + + return all_results; + } + +} diff --git a/botan/src/benchmark/benchmark.h b/botan/src/benchmark/benchmark.h new file mode 100644 index 0000000..272cfdf --- /dev/null +++ b/botan/src/benchmark/benchmark.h @@ -0,0 +1,59 @@ +/** +* Runtime benchmarking +* (C) 2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_RUNTIME_BENCHMARK_H__ +#define BOTAN_RUNTIME_BENCHMARK_H__ + +#include <botan/algo_factory.h> +#include <botan/timer.h> +#include <botan/rng.h> +#include <map> +#include <string> + +/** +* Choose some sort of default timer implementation to use, since some +* (like hardware tick counters and current Win32 timer) are not +* reliable for benchmarking. +*/ +#if defined(BOTAN_HAS_TIMER_POSIX) + #include <botan/tm_posix.h> +#elif defined(BOTAN_HAS_TIMER_UNIX) + #include <botan/tm_unix.h> +#endif + +namespace Botan { + +#if defined(BOTAN_HAS_TIMER_POSIX) + typedef POSIX_Timer Default_Benchmark_Timer; +#elif defined(BOTAN_HAS_TIMER_UNIX) + typedef Unix_Timer Default_Benchmark_Timer; +#else + /* I have not had good success using clock(), the results seem + * pretty bogus, but as a last resort it works. + */ + typedef ANSI_Clock_Timer Default_Benchmark_Timer; +#endif + +/** +* Algorithm benchmark +* @param name the name of the algorithm to test (cipher, hash, or MAC) +* @param milliseconds total time for the benchmark to run +* @param timer the timer to use +* @param rng the rng to use to generate random inputs +* @param af the algorithm factory used to create objects +* @return results a map from provider to speed in mebibytes per second +*/ +std::map<std::string, double> +algorithm_benchmark(const std::string& name, + u32bit milliseconds, + Timer& timer, + RandomNumberGenerator& rng, + Algorithm_Factory& af); + +} + +#endif diff --git a/botan/src/benchmark/info.txt b/botan/src/benchmark/info.txt new file mode 100644 index 0000000..0fbcdb2 --- /dev/null +++ b/botan/src/benchmark/info.txt @@ -0,0 +1,21 @@ +realname "Benchmarking" + +define RUNTIME_BENCHMARKING + +load_on auto + +<add> +benchmark.cpp +benchmark.h +</add> + +<requires> +algo_factory +block +buf_comp +hash +mac +rng +stream +timer +</requires> diff --git a/botan/src/block/aes/aes.cpp b/botan/src/block/aes/aes.cpp new file mode 100644 index 0000000..9072b50 --- /dev/null +++ b/botan/src/block/aes/aes.cpp @@ -0,0 +1,257 @@ +/** +* AES +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/aes.h> +#include <botan/loadstor.h> + +namespace Botan { + +/** +* AES Encryption +*/ +void AES::enc(const byte in[], byte out[]) const + { + const u32bit* TE0 = TE; + const u32bit* TE1 = TE + 256; + const u32bit* TE2 = TE + 512; + const u32bit* TE3 = TE + 768; + + u32bit T0 = load_be<u32bit>(in, 0) ^ EK[0]; + u32bit T1 = load_be<u32bit>(in, 1) ^ EK[1]; + u32bit T2 = load_be<u32bit>(in, 2) ^ EK[2]; + u32bit T3 = load_be<u32bit>(in, 3) ^ EK[3]; + + u32bit B0, B1, B2, B3; + B0 = TE0[get_byte(0, T0)] ^ TE1[get_byte(1, T1)] ^ + TE2[get_byte(2, T2)] ^ TE3[get_byte(3, T3)] ^ EK[4]; + B1 = TE0[get_byte(0, T1)] ^ TE1[get_byte(1, T2)] ^ + TE2[get_byte(2, T3)] ^ TE3[get_byte(3, T0)] ^ EK[5]; + B2 = TE0[get_byte(0, T2)] ^ TE1[get_byte(1, T3)] ^ + TE2[get_byte(2, T0)] ^ TE3[get_byte(3, T1)] ^ EK[6]; + B3 = TE0[get_byte(0, T3)] ^ TE1[get_byte(1, T0)] ^ + TE2[get_byte(2, T1)] ^ TE3[get_byte(3, T2)] ^ EK[7]; + + for(u32bit j = 2; j != ROUNDS; j += 2) + { + const u32bit K0 = EK[4*j]; + const u32bit K1 = EK[4*j+1]; + const u32bit K2 = EK[4*j+2]; + const u32bit K3 = EK[4*j+3]; + + T0 = TE0[get_byte(0, B0)] ^ TE1[get_byte(1, B1)] ^ + TE2[get_byte(2, B2)] ^ TE3[get_byte(3, B3)] ^ K0; + T1 = TE0[get_byte(0, B1)] ^ TE1[get_byte(1, B2)] ^ + TE2[get_byte(2, B3)] ^ TE3[get_byte(3, B0)] ^ K1; + T2 = TE0[get_byte(0, B2)] ^ TE1[get_byte(1, B3)] ^ + TE2[get_byte(2, B0)] ^ TE3[get_byte(3, B1)] ^ K2; + T3 = TE0[get_byte(0, B3)] ^ TE1[get_byte(1, B0)] ^ + TE2[get_byte(2, B1)] ^ TE3[get_byte(3, B2)] ^ K3; + + const u32bit K4 = EK[4*(j+1)+0]; + const u32bit K5 = EK[4*(j+1)+1]; + const u32bit K6 = EK[4*(j+1)+2]; + const u32bit K7 = EK[4*(j+1)+3]; + + B0 = TE0[get_byte(0, T0)] ^ TE1[get_byte(1, T1)] ^ + TE2[get_byte(2, T2)] ^ TE3[get_byte(3, T3)] ^ K4; + B1 = TE0[get_byte(0, T1)] ^ TE1[get_byte(1, T2)] ^ + TE2[get_byte(2, T3)] ^ TE3[get_byte(3, T0)] ^ K5; + B2 = TE0[get_byte(0, T2)] ^ TE1[get_byte(1, T3)] ^ + TE2[get_byte(2, T0)] ^ TE3[get_byte(3, T1)] ^ K6; + B3 = TE0[get_byte(0, T3)] ^ TE1[get_byte(1, T0)] ^ + TE2[get_byte(2, T1)] ^ TE3[get_byte(3, T2)] ^ K7; + } + + /* + Joseph Bonneau and Ilya Mironov's paper + <a href = "http://icme2007.org/users/mironov/papers/aes-timing.pdf"> + Cache-Collision Timing Attacks Against AES</a> describes an attack + that can recover AES keys with as few as 2<sup>13</sup> samples. + + """In addition to OpenSSL v. 0.9.8.(a), which was used in our + experiments, the AES implementations of Crypto++ 5.2.1 and + LibTomCrypt 1.09 use the original Rijndael C implementation with + very few changes and are highly vulnerable. The AES implementations + in libgcrypt v. 1.2.2 and Botan v. 1.4.2 are also vulnerable, but + use a smaller byte-wide final table which lessens the effectiveness + of the attacks.""" + */ + out[ 0] = SE[get_byte(0, B0)] ^ ME[0]; + out[ 1] = SE[get_byte(1, B1)] ^ ME[1]; + out[ 2] = SE[get_byte(2, B2)] ^ ME[2]; + out[ 3] = SE[get_byte(3, B3)] ^ ME[3]; + out[ 4] = SE[get_byte(0, B1)] ^ ME[4]; + out[ 5] = SE[get_byte(1, B2)] ^ ME[5]; + out[ 6] = SE[get_byte(2, B3)] ^ ME[6]; + out[ 7] = SE[get_byte(3, B0)] ^ ME[7]; + out[ 8] = SE[get_byte(0, B2)] ^ ME[8]; + out[ 9] = SE[get_byte(1, B3)] ^ ME[9]; + out[10] = SE[get_byte(2, B0)] ^ ME[10]; + out[11] = SE[get_byte(3, B1)] ^ ME[11]; + out[12] = SE[get_byte(0, B3)] ^ ME[12]; + out[13] = SE[get_byte(1, B0)] ^ ME[13]; + out[14] = SE[get_byte(2, B1)] ^ ME[14]; + out[15] = SE[get_byte(3, B2)] ^ ME[15]; + } + +/** +* AES Decryption +*/ +void AES::dec(const byte in[], byte out[]) const + { + const u32bit* TD0 = TD; + const u32bit* TD1 = TD + 256; + const u32bit* TD2 = TD + 512; + const u32bit* TD3 = TD + 768; + + u32bit T0 = load_be<u32bit>(in, 0) ^ DK[0]; + u32bit T1 = load_be<u32bit>(in, 1) ^ DK[1]; + u32bit T2 = load_be<u32bit>(in, 2) ^ DK[2]; + u32bit T3 = load_be<u32bit>(in, 3) ^ DK[3]; + + u32bit B0, B1, B2, B3; + B0 = TD0[get_byte(0, T0)] ^ TD1[get_byte(1, T3)] ^ + TD2[get_byte(2, T2)] ^ TD3[get_byte(3, T1)] ^ DK[4]; + B1 = TD0[get_byte(0, T1)] ^ TD1[get_byte(1, T0)] ^ + TD2[get_byte(2, T3)] ^ TD3[get_byte(3, T2)] ^ DK[5]; + B2 = TD0[get_byte(0, T2)] ^ TD1[get_byte(1, T1)] ^ + TD2[get_byte(2, T0)] ^ TD3[get_byte(3, T3)] ^ DK[6]; + B3 = TD0[get_byte(0, T3)] ^ TD1[get_byte(1, T2)] ^ + TD2[get_byte(2, T1)] ^ TD3[get_byte(3, T0)] ^ DK[7]; + + for(u32bit j = 2; j != ROUNDS; j += 2) + { + const u32bit K0 = DK[4*j+0]; + const u32bit K1 = DK[4*j+1]; + const u32bit K2 = DK[4*j+2]; + const u32bit K3 = DK[4*j+3]; + + T0 = TD0[get_byte(0, B0)] ^ TD1[get_byte(1, B3)] ^ + TD2[get_byte(2, B2)] ^ TD3[get_byte(3, B1)] ^ K0; + T1 = TD0[get_byte(0, B1)] ^ TD1[get_byte(1, B0)] ^ + TD2[get_byte(2, B3)] ^ TD3[get_byte(3, B2)] ^ K1; + T2 = TD0[get_byte(0, B2)] ^ TD1[get_byte(1, B1)] ^ + TD2[get_byte(2, B0)] ^ TD3[get_byte(3, B3)] ^ K2; + T3 = TD0[get_byte(0, B3)] ^ TD1[get_byte(1, B2)] ^ + TD2[get_byte(2, B1)] ^ TD3[get_byte(3, B0)] ^ K3; + + const u32bit K4 = DK[4*(j+1)+0]; + const u32bit K5 = DK[4*(j+1)+1]; + const u32bit K6 = DK[4*(j+1)+2]; + const u32bit K7 = DK[4*(j+1)+3]; + + B0 = TD0[get_byte(0, T0)] ^ TD1[get_byte(1, T3)] ^ + TD2[get_byte(2, T2)] ^ TD3[get_byte(3, T1)] ^ K4; + B1 = TD0[get_byte(0, T1)] ^ TD1[get_byte(1, T0)] ^ + TD2[get_byte(2, T3)] ^ TD3[get_byte(3, T2)] ^ K5; + B2 = TD0[get_byte(0, T2)] ^ TD1[get_byte(1, T1)] ^ + TD2[get_byte(2, T0)] ^ TD3[get_byte(3, T3)] ^ K6; + B3 = TD0[get_byte(0, T3)] ^ TD1[get_byte(1, T2)] ^ + TD2[get_byte(2, T1)] ^ TD3[get_byte(3, T0)] ^ K7; + } + + out[ 0] = SD[get_byte(0, B0)] ^ MD[0]; + out[ 1] = SD[get_byte(1, B3)] ^ MD[1]; + out[ 2] = SD[get_byte(2, B2)] ^ MD[2]; + out[ 3] = SD[get_byte(3, B1)] ^ MD[3]; + out[ 4] = SD[get_byte(0, B1)] ^ MD[4]; + out[ 5] = SD[get_byte(1, B0)] ^ MD[5]; + out[ 6] = SD[get_byte(2, B3)] ^ MD[6]; + out[ 7] = SD[get_byte(3, B2)] ^ MD[7]; + out[ 8] = SD[get_byte(0, B2)] ^ MD[8]; + out[ 9] = SD[get_byte(1, B1)] ^ MD[9]; + out[10] = SD[get_byte(2, B0)] ^ MD[10]; + out[11] = SD[get_byte(3, B3)] ^ MD[11]; + out[12] = SD[get_byte(0, B3)] ^ MD[12]; + out[13] = SD[get_byte(1, B2)] ^ MD[13]; + out[14] = SD[get_byte(2, B1)] ^ MD[14]; + out[15] = SD[get_byte(3, B0)] ^ MD[15]; + } + +/** +* AES Key Schedule +*/ +void AES::key_schedule(const byte key[], u32bit length) + { + static const u32bit RC[10] = { + 0x01000000, 0x02000000, 0x04000000, 0x08000000, 0x10000000, 0x20000000, + 0x40000000, 0x80000000, 0x1B000000, 0x36000000 }; + ROUNDS = (length / 4) + 6; + + SecureBuffer<u32bit, 64> XEK, XDK; + + const u32bit X = length / 4; + for(u32bit j = 0; j != X; ++j) + XEK[j] = load_be<u32bit>(key, j); + + for(u32bit j = X; j < 4*(ROUNDS+1); j += X) + { + XEK[j] = XEK[j-X] ^ S(rotate_left(XEK[j-1], 8)) ^ RC[(j-X)/X]; + for(u32bit k = 1; k != X; ++k) + { + if(X == 8 && k == 4) + XEK[j+k] = XEK[j+k-X] ^ S(XEK[j+k-1]); + else + XEK[j+k] = XEK[j+k-X] ^ XEK[j+k-1]; + } + } + + for(u32bit j = 0; j != 4*(ROUNDS+1); j += 4) + { + XDK[j ] = XEK[4*ROUNDS-j ]; + XDK[j+1] = XEK[4*ROUNDS-j+1]; + XDK[j+2] = XEK[4*ROUNDS-j+2]; + XDK[j+3] = XEK[4*ROUNDS-j+3]; + } + + for(u32bit j = 4; j != length + 24; ++j) + XDK[j] = TD[SE[get_byte(0, XDK[j])] + 0] ^ + TD[SE[get_byte(1, XDK[j])] + 256] ^ + TD[SE[get_byte(2, XDK[j])] + 512] ^ + TD[SE[get_byte(3, XDK[j])] + 768]; + + for(u32bit j = 0; j != 4; ++j) + { + store_be(XEK[j+4*ROUNDS], ME + 4*j); + store_be(XEK[j], MD + 4*j); + } + + EK.copy(XEK, length + 24); + DK.copy(XDK, length + 24); + } + +/** +* AES Byte Substitution +*/ +u32bit AES::S(u32bit input) + { + return make_u32bit(SE[get_byte(0, input)], SE[get_byte(1, input)], + SE[get_byte(2, input)], SE[get_byte(3, input)]); + } + +/** +* AES Constructor +*/ +AES::AES(u32bit key_size) : BlockCipher(16, key_size) + { + if(key_size != 16 && key_size != 24 && key_size != 32) + throw Invalid_Key_Length(name(), key_size); + ROUNDS = (key_size / 4) + 6; + } + +/** +* Clear memory of sensitive data +*/ +void AES::clear() throw() + { + EK.clear(); + DK.clear(); + ME.clear(); + MD.clear(); + } + +} diff --git a/botan/src/block/aes/aes.h b/botan/src/block/aes/aes.h new file mode 100644 index 0000000..05e2e31 --- /dev/null +++ b/botan/src/block/aes/aes.h @@ -0,0 +1,81 @@ +/** +* AES +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_AES_H__ +#define BOTAN_AES_H__ + +#include <botan/block_cipher.h> + +namespace Botan { + +/** +* Rijndael aka AES +*/ +class BOTAN_DLL AES : public BlockCipher + { + public: + void clear() throw(); + std::string name() const { return "AES"; } + BlockCipher* clone() const { return new AES; } + AES() : BlockCipher(16, 16, 32, 8) { ROUNDS = 14; } + AES(u32bit); + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key_schedule(const byte[], u32bit); + static u32bit S(u32bit); + + static const byte SE[256]; + static const byte SD[256]; + static const u32bit TE[1024]; + static const u32bit TD[1024]; + + u32bit ROUNDS; + + SecureBuffer<u32bit, 56> EK; + SecureBuffer<byte, 16> ME; + + SecureBuffer<u32bit, 56> DK; + SecureBuffer<byte, 16> MD; + }; + +/** +* AES-128 +*/ +class BOTAN_DLL AES_128 : public AES + { + public: + std::string name() const { return "AES-128"; } + BlockCipher* clone() const { return new AES_128; } + AES_128() : AES(16) {} + }; + +/** +* AES-192 +*/ +class BOTAN_DLL AES_192 : public AES + { + public: + std::string name() const { return "AES-192"; } + BlockCipher* clone() const { return new AES_192; } + AES_192() : AES(24) {} + }; + +/** +* AES-256 +*/ +class BOTAN_DLL AES_256 : public AES + { + public: + std::string name() const { return "AES-256"; } + BlockCipher* clone() const { return new AES_256; } + AES_256() : AES(32) {} + }; + +} + +#endif diff --git a/botan/src/block/aes/aes_tab.cpp b/botan/src/block/aes/aes_tab.cpp new file mode 100644 index 0000000..d42a2cd --- /dev/null +++ b/botan/src/block/aes/aes_tab.cpp @@ -0,0 +1,406 @@ +/* +* S-Box and Diffusion Tables for AES +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/aes.h> + +namespace Botan { + +const byte AES::SE[256] = { + 0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B, + 0xFE, 0xD7, 0xAB, 0x76, 0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0, + 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0, 0xB7, 0xFD, 0x93, 0x26, + 0x36, 0x3F, 0xF7, 0xCC, 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15, + 0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A, 0x07, 0x12, 0x80, 0xE2, + 0xEB, 0x27, 0xB2, 0x75, 0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0, + 0x52, 0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, 0x84, 0x53, 0xD1, 0x00, 0xED, + 0x20, 0xFC, 0xB1, 0x5B, 0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58, 0xCF, + 0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85, 0x45, 0xF9, 0x02, 0x7F, + 0x50, 0x3C, 0x9F, 0xA8, 0x51, 0xA3, 0x40, 0x8F, 0x92, 0x9D, 0x38, 0xF5, + 0xBC, 0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, 0xD2, 0xCD, 0x0C, 0x13, 0xEC, + 0x5F, 0x97, 0x44, 0x17, 0xC4, 0xA7, 0x7E, 0x3D, 0x64, 0x5D, 0x19, 0x73, + 0x60, 0x81, 0x4F, 0xDC, 0x22, 0x2A, 0x90, 0x88, 0x46, 0xEE, 0xB8, 0x14, + 0xDE, 0x5E, 0x0B, 0xDB, 0xE0, 0x32, 0x3A, 0x0A, 0x49, 0x06, 0x24, 0x5C, + 0xC2, 0xD3, 0xAC, 0x62, 0x91, 0x95, 0xE4, 0x79, 0xE7, 0xC8, 0x37, 0x6D, + 0x8D, 0xD5, 0x4E, 0xA9, 0x6C, 0x56, 0xF4, 0xEA, 0x65, 0x7A, 0xAE, 0x08, + 0xBA, 0x78, 0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6, 0xE8, 0xDD, 0x74, 0x1F, + 0x4B, 0xBD, 0x8B, 0x8A, 0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E, + 0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E, 0xE1, 0xF8, 0x98, 0x11, + 0x69, 0xD9, 0x8E, 0x94, 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF, + 0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, 0x41, 0x99, 0x2D, 0x0F, + 0xB0, 0x54, 0xBB, 0x16 }; + +const byte AES::SD[256] = { + 0x52, 0x09, 0x6A, 0xD5, 0x30, 0x36, 0xA5, 0x38, 0xBF, 0x40, 0xA3, 0x9E, + 0x81, 0xF3, 0xD7, 0xFB, 0x7C, 0xE3, 0x39, 0x82, 0x9B, 0x2F, 0xFF, 0x87, + 0x34, 0x8E, 0x43, 0x44, 0xC4, 0xDE, 0xE9, 0xCB, 0x54, 0x7B, 0x94, 0x32, + 0xA6, 0xC2, 0x23, 0x3D, 0xEE, 0x4C, 0x95, 0x0B, 0x42, 0xFA, 0xC3, 0x4E, + 0x08, 0x2E, 0xA1, 0x66, 0x28, 0xD9, 0x24, 0xB2, 0x76, 0x5B, 0xA2, 0x49, + 0x6D, 0x8B, 0xD1, 0x25, 0x72, 0xF8, 0xF6, 0x64, 0x86, 0x68, 0x98, 0x16, + 0xD4, 0xA4, 0x5C, 0xCC, 0x5D, 0x65, 0xB6, 0x92, 0x6C, 0x70, 0x48, 0x50, + 0xFD, 0xED, 0xB9, 0xDA, 0x5E, 0x15, 0x46, 0x57, 0xA7, 0x8D, 0x9D, 0x84, + 0x90, 0xD8, 0xAB, 0x00, 0x8C, 0xBC, 0xD3, 0x0A, 0xF7, 0xE4, 0x58, 0x05, + 0xB8, 0xB3, 0x45, 0x06, 0xD0, 0x2C, 0x1E, 0x8F, 0xCA, 0x3F, 0x0F, 0x02, + 0xC1, 0xAF, 0xBD, 0x03, 0x01, 0x13, 0x8A, 0x6B, 0x3A, 0x91, 0x11, 0x41, + 0x4F, 0x67, 0xDC, 0xEA, 0x97, 0xF2, 0xCF, 0xCE, 0xF0, 0xB4, 0xE6, 0x73, + 0x96, 0xAC, 0x74, 0x22, 0xE7, 0xAD, 0x35, 0x85, 0xE2, 0xF9, 0x37, 0xE8, + 0x1C, 0x75, 0xDF, 0x6E, 0x47, 0xF1, 0x1A, 0x71, 0x1D, 0x29, 0xC5, 0x89, + 0x6F, 0xB7, 0x62, 0x0E, 0xAA, 0x18, 0xBE, 0x1B, 0xFC, 0x56, 0x3E, 0x4B, + 0xC6, 0xD2, 0x79, 0x20, 0x9A, 0xDB, 0xC0, 0xFE, 0x78, 0xCD, 0x5A, 0xF4, + 0x1F, 0xDD, 0xA8, 0x33, 0x88, 0x07, 0xC7, 0x31, 0xB1, 0x12, 0x10, 0x59, + 0x27, 0x80, 0xEC, 0x5F, 0x60, 0x51, 0x7F, 0xA9, 0x19, 0xB5, 0x4A, 0x0D, + 0x2D, 0xE5, 0x7A, 0x9F, 0x93, 0xC9, 0x9C, 0xEF, 0xA0, 0xE0, 0x3B, 0x4D, + 0xAE, 0x2A, 0xF5, 0xB0, 0xC8, 0xEB, 0xBB, 0x3C, 0x83, 0x53, 0x99, 0x61, + 0x17, 0x2B, 0x04, 0x7E, 0xBA, 0x77, 0xD6, 0x26, 0xE1, 0x69, 0x14, 0x63, + 0x55, 0x21, 0x0C, 0x7D }; + +const u32bit AES::TE[1024] = { + 0xC66363A5, 0xF87C7C84, 0xEE777799, 0xF67B7B8D, 0xFFF2F20D, 0xD66B6BBD, + 0xDE6F6FB1, 0x91C5C554, 0x60303050, 0x02010103, 0xCE6767A9, 0x562B2B7D, + 0xE7FEFE19, 0xB5D7D762, 0x4DABABE6, 0xEC76769A, 0x8FCACA45, 0x1F82829D, + 0x89C9C940, 0xFA7D7D87, 0xEFFAFA15, 0xB25959EB, 0x8E4747C9, 0xFBF0F00B, + 0x41ADADEC, 0xB3D4D467, 0x5FA2A2FD, 0x45AFAFEA, 0x239C9CBF, 0x53A4A4F7, + 0xE4727296, 0x9BC0C05B, 0x75B7B7C2, 0xE1FDFD1C, 0x3D9393AE, 0x4C26266A, + 0x6C36365A, 0x7E3F3F41, 0xF5F7F702, 0x83CCCC4F, 0x6834345C, 0x51A5A5F4, + 0xD1E5E534, 0xF9F1F108, 0xE2717193, 0xABD8D873, 0x62313153, 0x2A15153F, + 0x0804040C, 0x95C7C752, 0x46232365, 0x9DC3C35E, 0x30181828, 0x379696A1, + 0x0A05050F, 0x2F9A9AB5, 0x0E070709, 0x24121236, 0x1B80809B, 0xDFE2E23D, + 0xCDEBEB26, 0x4E272769, 0x7FB2B2CD, 0xEA75759F, 0x1209091B, 0x1D83839E, + 0x582C2C74, 0x341A1A2E, 0x361B1B2D, 0xDC6E6EB2, 0xB45A5AEE, 0x5BA0A0FB, + 0xA45252F6, 0x763B3B4D, 0xB7D6D661, 0x7DB3B3CE, 0x5229297B, 0xDDE3E33E, + 0x5E2F2F71, 0x13848497, 0xA65353F5, 0xB9D1D168, 0x00000000, 0xC1EDED2C, + 0x40202060, 0xE3FCFC1F, 0x79B1B1C8, 0xB65B5BED, 0xD46A6ABE, 0x8DCBCB46, + 0x67BEBED9, 0x7239394B, 0x944A4ADE, 0x984C4CD4, 0xB05858E8, 0x85CFCF4A, + 0xBBD0D06B, 0xC5EFEF2A, 0x4FAAAAE5, 0xEDFBFB16, 0x864343C5, 0x9A4D4DD7, + 0x66333355, 0x11858594, 0x8A4545CF, 0xE9F9F910, 0x04020206, 0xFE7F7F81, + 0xA05050F0, 0x783C3C44, 0x259F9FBA, 0x4BA8A8E3, 0xA25151F3, 0x5DA3A3FE, + 0x804040C0, 0x058F8F8A, 0x3F9292AD, 0x219D9DBC, 0x70383848, 0xF1F5F504, + 0x63BCBCDF, 0x77B6B6C1, 0xAFDADA75, 0x42212163, 0x20101030, 0xE5FFFF1A, + 0xFDF3F30E, 0xBFD2D26D, 0x81CDCD4C, 0x180C0C14, 0x26131335, 0xC3ECEC2F, + 0xBE5F5FE1, 0x359797A2, 0x884444CC, 0x2E171739, 0x93C4C457, 0x55A7A7F2, + 0xFC7E7E82, 0x7A3D3D47, 0xC86464AC, 0xBA5D5DE7, 0x3219192B, 0xE6737395, + 0xC06060A0, 0x19818198, 0x9E4F4FD1, 0xA3DCDC7F, 0x44222266, 0x542A2A7E, + 0x3B9090AB, 0x0B888883, 0x8C4646CA, 0xC7EEEE29, 0x6BB8B8D3, 0x2814143C, + 0xA7DEDE79, 0xBC5E5EE2, 0x160B0B1D, 0xADDBDB76, 0xDBE0E03B, 0x64323256, + 0x743A3A4E, 0x140A0A1E, 0x924949DB, 0x0C06060A, 0x4824246C, 0xB85C5CE4, + 0x9FC2C25D, 0xBDD3D36E, 0x43ACACEF, 0xC46262A6, 0x399191A8, 0x319595A4, + 0xD3E4E437, 0xF279798B, 0xD5E7E732, 0x8BC8C843, 0x6E373759, 0xDA6D6DB7, + 0x018D8D8C, 0xB1D5D564, 0x9C4E4ED2, 0x49A9A9E0, 0xD86C6CB4, 0xAC5656FA, + 0xF3F4F407, 0xCFEAEA25, 0xCA6565AF, 0xF47A7A8E, 0x47AEAEE9, 0x10080818, + 0x6FBABAD5, 0xF0787888, 0x4A25256F, 0x5C2E2E72, 0x381C1C24, 0x57A6A6F1, + 0x73B4B4C7, 0x97C6C651, 0xCBE8E823, 0xA1DDDD7C, 0xE874749C, 0x3E1F1F21, + 0x964B4BDD, 0x61BDBDDC, 0x0D8B8B86, 0x0F8A8A85, 0xE0707090, 0x7C3E3E42, + 0x71B5B5C4, 0xCC6666AA, 0x904848D8, 0x06030305, 0xF7F6F601, 0x1C0E0E12, + 0xC26161A3, 0x6A35355F, 0xAE5757F9, 0x69B9B9D0, 0x17868691, 0x99C1C158, + 0x3A1D1D27, 0x279E9EB9, 0xD9E1E138, 0xEBF8F813, 0x2B9898B3, 0x22111133, + 0xD26969BB, 0xA9D9D970, 0x078E8E89, 0x339494A7, 0x2D9B9BB6, 0x3C1E1E22, + 0x15878792, 0xC9E9E920, 0x87CECE49, 0xAA5555FF, 0x50282878, 0xA5DFDF7A, + 0x038C8C8F, 0x59A1A1F8, 0x09898980, 0x1A0D0D17, 0x65BFBFDA, 0xD7E6E631, + 0x844242C6, 0xD06868B8, 0x824141C3, 0x299999B0, 0x5A2D2D77, 0x1E0F0F11, + 0x7BB0B0CB, 0xA85454FC, 0x6DBBBBD6, 0x2C16163A, 0xA5C66363, 0x84F87C7C, + 0x99EE7777, 0x8DF67B7B, 0x0DFFF2F2, 0xBDD66B6B, 0xB1DE6F6F, 0x5491C5C5, + 0x50603030, 0x03020101, 0xA9CE6767, 0x7D562B2B, 0x19E7FEFE, 0x62B5D7D7, + 0xE64DABAB, 0x9AEC7676, 0x458FCACA, 0x9D1F8282, 0x4089C9C9, 0x87FA7D7D, + 0x15EFFAFA, 0xEBB25959, 0xC98E4747, 0x0BFBF0F0, 0xEC41ADAD, 0x67B3D4D4, + 0xFD5FA2A2, 0xEA45AFAF, 0xBF239C9C, 0xF753A4A4, 0x96E47272, 0x5B9BC0C0, + 0xC275B7B7, 0x1CE1FDFD, 0xAE3D9393, 0x6A4C2626, 0x5A6C3636, 0x417E3F3F, + 0x02F5F7F7, 0x4F83CCCC, 0x5C683434, 0xF451A5A5, 0x34D1E5E5, 0x08F9F1F1, + 0x93E27171, 0x73ABD8D8, 0x53623131, 0x3F2A1515, 0x0C080404, 0x5295C7C7, + 0x65462323, 0x5E9DC3C3, 0x28301818, 0xA1379696, 0x0F0A0505, 0xB52F9A9A, + 0x090E0707, 0x36241212, 0x9B1B8080, 0x3DDFE2E2, 0x26CDEBEB, 0x694E2727, + 0xCD7FB2B2, 0x9FEA7575, 0x1B120909, 0x9E1D8383, 0x74582C2C, 0x2E341A1A, + 0x2D361B1B, 0xB2DC6E6E, 0xEEB45A5A, 0xFB5BA0A0, 0xF6A45252, 0x4D763B3B, + 0x61B7D6D6, 0xCE7DB3B3, 0x7B522929, 0x3EDDE3E3, 0x715E2F2F, 0x97138484, + 0xF5A65353, 0x68B9D1D1, 0x00000000, 0x2CC1EDED, 0x60402020, 0x1FE3FCFC, + 0xC879B1B1, 0xEDB65B5B, 0xBED46A6A, 0x468DCBCB, 0xD967BEBE, 0x4B723939, + 0xDE944A4A, 0xD4984C4C, 0xE8B05858, 0x4A85CFCF, 0x6BBBD0D0, 0x2AC5EFEF, + 0xE54FAAAA, 0x16EDFBFB, 0xC5864343, 0xD79A4D4D, 0x55663333, 0x94118585, + 0xCF8A4545, 0x10E9F9F9, 0x06040202, 0x81FE7F7F, 0xF0A05050, 0x44783C3C, + 0xBA259F9F, 0xE34BA8A8, 0xF3A25151, 0xFE5DA3A3, 0xC0804040, 0x8A058F8F, + 0xAD3F9292, 0xBC219D9D, 0x48703838, 0x04F1F5F5, 0xDF63BCBC, 0xC177B6B6, + 0x75AFDADA, 0x63422121, 0x30201010, 0x1AE5FFFF, 0x0EFDF3F3, 0x6DBFD2D2, + 0x4C81CDCD, 0x14180C0C, 0x35261313, 0x2FC3ECEC, 0xE1BE5F5F, 0xA2359797, + 0xCC884444, 0x392E1717, 0x5793C4C4, 0xF255A7A7, 0x82FC7E7E, 0x477A3D3D, + 0xACC86464, 0xE7BA5D5D, 0x2B321919, 0x95E67373, 0xA0C06060, 0x98198181, + 0xD19E4F4F, 0x7FA3DCDC, 0x66442222, 0x7E542A2A, 0xAB3B9090, 0x830B8888, + 0xCA8C4646, 0x29C7EEEE, 0xD36BB8B8, 0x3C281414, 0x79A7DEDE, 0xE2BC5E5E, + 0x1D160B0B, 0x76ADDBDB, 0x3BDBE0E0, 0x56643232, 0x4E743A3A, 0x1E140A0A, + 0xDB924949, 0x0A0C0606, 0x6C482424, 0xE4B85C5C, 0x5D9FC2C2, 0x6EBDD3D3, + 0xEF43ACAC, 0xA6C46262, 0xA8399191, 0xA4319595, 0x37D3E4E4, 0x8BF27979, + 0x32D5E7E7, 0x438BC8C8, 0x596E3737, 0xB7DA6D6D, 0x8C018D8D, 0x64B1D5D5, + 0xD29C4E4E, 0xE049A9A9, 0xB4D86C6C, 0xFAAC5656, 0x07F3F4F4, 0x25CFEAEA, + 0xAFCA6565, 0x8EF47A7A, 0xE947AEAE, 0x18100808, 0xD56FBABA, 0x88F07878, + 0x6F4A2525, 0x725C2E2E, 0x24381C1C, 0xF157A6A6, 0xC773B4B4, 0x5197C6C6, + 0x23CBE8E8, 0x7CA1DDDD, 0x9CE87474, 0x213E1F1F, 0xDD964B4B, 0xDC61BDBD, + 0x860D8B8B, 0x850F8A8A, 0x90E07070, 0x427C3E3E, 0xC471B5B5, 0xAACC6666, + 0xD8904848, 0x05060303, 0x01F7F6F6, 0x121C0E0E, 0xA3C26161, 0x5F6A3535, + 0xF9AE5757, 0xD069B9B9, 0x91178686, 0x5899C1C1, 0x273A1D1D, 0xB9279E9E, + 0x38D9E1E1, 0x13EBF8F8, 0xB32B9898, 0x33221111, 0xBBD26969, 0x70A9D9D9, + 0x89078E8E, 0xA7339494, 0xB62D9B9B, 0x223C1E1E, 0x92158787, 0x20C9E9E9, + 0x4987CECE, 0xFFAA5555, 0x78502828, 0x7AA5DFDF, 0x8F038C8C, 0xF859A1A1, + 0x80098989, 0x171A0D0D, 0xDA65BFBF, 0x31D7E6E6, 0xC6844242, 0xB8D06868, + 0xC3824141, 0xB0299999, 0x775A2D2D, 0x111E0F0F, 0xCB7BB0B0, 0xFCA85454, + 0xD66DBBBB, 0x3A2C1616, 0x63A5C663, 0x7C84F87C, 0x7799EE77, 0x7B8DF67B, + 0xF20DFFF2, 0x6BBDD66B, 0x6FB1DE6F, 0xC55491C5, 0x30506030, 0x01030201, + 0x67A9CE67, 0x2B7D562B, 0xFE19E7FE, 0xD762B5D7, 0xABE64DAB, 0x769AEC76, + 0xCA458FCA, 0x829D1F82, 0xC94089C9, 0x7D87FA7D, 0xFA15EFFA, 0x59EBB259, + 0x47C98E47, 0xF00BFBF0, 0xADEC41AD, 0xD467B3D4, 0xA2FD5FA2, 0xAFEA45AF, + 0x9CBF239C, 0xA4F753A4, 0x7296E472, 0xC05B9BC0, 0xB7C275B7, 0xFD1CE1FD, + 0x93AE3D93, 0x266A4C26, 0x365A6C36, 0x3F417E3F, 0xF702F5F7, 0xCC4F83CC, + 0x345C6834, 0xA5F451A5, 0xE534D1E5, 0xF108F9F1, 0x7193E271, 0xD873ABD8, + 0x31536231, 0x153F2A15, 0x040C0804, 0xC75295C7, 0x23654623, 0xC35E9DC3, + 0x18283018, 0x96A13796, 0x050F0A05, 0x9AB52F9A, 0x07090E07, 0x12362412, + 0x809B1B80, 0xE23DDFE2, 0xEB26CDEB, 0x27694E27, 0xB2CD7FB2, 0x759FEA75, + 0x091B1209, 0x839E1D83, 0x2C74582C, 0x1A2E341A, 0x1B2D361B, 0x6EB2DC6E, + 0x5AEEB45A, 0xA0FB5BA0, 0x52F6A452, 0x3B4D763B, 0xD661B7D6, 0xB3CE7DB3, + 0x297B5229, 0xE33EDDE3, 0x2F715E2F, 0x84971384, 0x53F5A653, 0xD168B9D1, + 0x00000000, 0xED2CC1ED, 0x20604020, 0xFC1FE3FC, 0xB1C879B1, 0x5BEDB65B, + 0x6ABED46A, 0xCB468DCB, 0xBED967BE, 0x394B7239, 0x4ADE944A, 0x4CD4984C, + 0x58E8B058, 0xCF4A85CF, 0xD06BBBD0, 0xEF2AC5EF, 0xAAE54FAA, 0xFB16EDFB, + 0x43C58643, 0x4DD79A4D, 0x33556633, 0x85941185, 0x45CF8A45, 0xF910E9F9, + 0x02060402, 0x7F81FE7F, 0x50F0A050, 0x3C44783C, 0x9FBA259F, 0xA8E34BA8, + 0x51F3A251, 0xA3FE5DA3, 0x40C08040, 0x8F8A058F, 0x92AD3F92, 0x9DBC219D, + 0x38487038, 0xF504F1F5, 0xBCDF63BC, 0xB6C177B6, 0xDA75AFDA, 0x21634221, + 0x10302010, 0xFF1AE5FF, 0xF30EFDF3, 0xD26DBFD2, 0xCD4C81CD, 0x0C14180C, + 0x13352613, 0xEC2FC3EC, 0x5FE1BE5F, 0x97A23597, 0x44CC8844, 0x17392E17, + 0xC45793C4, 0xA7F255A7, 0x7E82FC7E, 0x3D477A3D, 0x64ACC864, 0x5DE7BA5D, + 0x192B3219, 0x7395E673, 0x60A0C060, 0x81981981, 0x4FD19E4F, 0xDC7FA3DC, + 0x22664422, 0x2A7E542A, 0x90AB3B90, 0x88830B88, 0x46CA8C46, 0xEE29C7EE, + 0xB8D36BB8, 0x143C2814, 0xDE79A7DE, 0x5EE2BC5E, 0x0B1D160B, 0xDB76ADDB, + 0xE03BDBE0, 0x32566432, 0x3A4E743A, 0x0A1E140A, 0x49DB9249, 0x060A0C06, + 0x246C4824, 0x5CE4B85C, 0xC25D9FC2, 0xD36EBDD3, 0xACEF43AC, 0x62A6C462, + 0x91A83991, 0x95A43195, 0xE437D3E4, 0x798BF279, 0xE732D5E7, 0xC8438BC8, + 0x37596E37, 0x6DB7DA6D, 0x8D8C018D, 0xD564B1D5, 0x4ED29C4E, 0xA9E049A9, + 0x6CB4D86C, 0x56FAAC56, 0xF407F3F4, 0xEA25CFEA, 0x65AFCA65, 0x7A8EF47A, + 0xAEE947AE, 0x08181008, 0xBAD56FBA, 0x7888F078, 0x256F4A25, 0x2E725C2E, + 0x1C24381C, 0xA6F157A6, 0xB4C773B4, 0xC65197C6, 0xE823CBE8, 0xDD7CA1DD, + 0x749CE874, 0x1F213E1F, 0x4BDD964B, 0xBDDC61BD, 0x8B860D8B, 0x8A850F8A, + 0x7090E070, 0x3E427C3E, 0xB5C471B5, 0x66AACC66, 0x48D89048, 0x03050603, + 0xF601F7F6, 0x0E121C0E, 0x61A3C261, 0x355F6A35, 0x57F9AE57, 0xB9D069B9, + 0x86911786, 0xC15899C1, 0x1D273A1D, 0x9EB9279E, 0xE138D9E1, 0xF813EBF8, + 0x98B32B98, 0x11332211, 0x69BBD269, 0xD970A9D9, 0x8E89078E, 0x94A73394, + 0x9BB62D9B, 0x1E223C1E, 0x87921587, 0xE920C9E9, 0xCE4987CE, 0x55FFAA55, + 0x28785028, 0xDF7AA5DF, 0x8C8F038C, 0xA1F859A1, 0x89800989, 0x0D171A0D, + 0xBFDA65BF, 0xE631D7E6, 0x42C68442, 0x68B8D068, 0x41C38241, 0x99B02999, + 0x2D775A2D, 0x0F111E0F, 0xB0CB7BB0, 0x54FCA854, 0xBBD66DBB, 0x163A2C16, + 0x6363A5C6, 0x7C7C84F8, 0x777799EE, 0x7B7B8DF6, 0xF2F20DFF, 0x6B6BBDD6, + 0x6F6FB1DE, 0xC5C55491, 0x30305060, 0x01010302, 0x6767A9CE, 0x2B2B7D56, + 0xFEFE19E7, 0xD7D762B5, 0xABABE64D, 0x76769AEC, 0xCACA458F, 0x82829D1F, + 0xC9C94089, 0x7D7D87FA, 0xFAFA15EF, 0x5959EBB2, 0x4747C98E, 0xF0F00BFB, + 0xADADEC41, 0xD4D467B3, 0xA2A2FD5F, 0xAFAFEA45, 0x9C9CBF23, 0xA4A4F753, + 0x727296E4, 0xC0C05B9B, 0xB7B7C275, 0xFDFD1CE1, 0x9393AE3D, 0x26266A4C, + 0x36365A6C, 0x3F3F417E, 0xF7F702F5, 0xCCCC4F83, 0x34345C68, 0xA5A5F451, + 0xE5E534D1, 0xF1F108F9, 0x717193E2, 0xD8D873AB, 0x31315362, 0x15153F2A, + 0x04040C08, 0xC7C75295, 0x23236546, 0xC3C35E9D, 0x18182830, 0x9696A137, + 0x05050F0A, 0x9A9AB52F, 0x0707090E, 0x12123624, 0x80809B1B, 0xE2E23DDF, + 0xEBEB26CD, 0x2727694E, 0xB2B2CD7F, 0x75759FEA, 0x09091B12, 0x83839E1D, + 0x2C2C7458, 0x1A1A2E34, 0x1B1B2D36, 0x6E6EB2DC, 0x5A5AEEB4, 0xA0A0FB5B, + 0x5252F6A4, 0x3B3B4D76, 0xD6D661B7, 0xB3B3CE7D, 0x29297B52, 0xE3E33EDD, + 0x2F2F715E, 0x84849713, 0x5353F5A6, 0xD1D168B9, 0x00000000, 0xEDED2CC1, + 0x20206040, 0xFCFC1FE3, 0xB1B1C879, 0x5B5BEDB6, 0x6A6ABED4, 0xCBCB468D, + 0xBEBED967, 0x39394B72, 0x4A4ADE94, 0x4C4CD498, 0x5858E8B0, 0xCFCF4A85, + 0xD0D06BBB, 0xEFEF2AC5, 0xAAAAE54F, 0xFBFB16ED, 0x4343C586, 0x4D4DD79A, + 0x33335566, 0x85859411, 0x4545CF8A, 0xF9F910E9, 0x02020604, 0x7F7F81FE, + 0x5050F0A0, 0x3C3C4478, 0x9F9FBA25, 0xA8A8E34B, 0x5151F3A2, 0xA3A3FE5D, + 0x4040C080, 0x8F8F8A05, 0x9292AD3F, 0x9D9DBC21, 0x38384870, 0xF5F504F1, + 0xBCBCDF63, 0xB6B6C177, 0xDADA75AF, 0x21216342, 0x10103020, 0xFFFF1AE5, + 0xF3F30EFD, 0xD2D26DBF, 0xCDCD4C81, 0x0C0C1418, 0x13133526, 0xECEC2FC3, + 0x5F5FE1BE, 0x9797A235, 0x4444CC88, 0x1717392E, 0xC4C45793, 0xA7A7F255, + 0x7E7E82FC, 0x3D3D477A, 0x6464ACC8, 0x5D5DE7BA, 0x19192B32, 0x737395E6, + 0x6060A0C0, 0x81819819, 0x4F4FD19E, 0xDCDC7FA3, 0x22226644, 0x2A2A7E54, + 0x9090AB3B, 0x8888830B, 0x4646CA8C, 0xEEEE29C7, 0xB8B8D36B, 0x14143C28, + 0xDEDE79A7, 0x5E5EE2BC, 0x0B0B1D16, 0xDBDB76AD, 0xE0E03BDB, 0x32325664, + 0x3A3A4E74, 0x0A0A1E14, 0x4949DB92, 0x06060A0C, 0x24246C48, 0x5C5CE4B8, + 0xC2C25D9F, 0xD3D36EBD, 0xACACEF43, 0x6262A6C4, 0x9191A839, 0x9595A431, + 0xE4E437D3, 0x79798BF2, 0xE7E732D5, 0xC8C8438B, 0x3737596E, 0x6D6DB7DA, + 0x8D8D8C01, 0xD5D564B1, 0x4E4ED29C, 0xA9A9E049, 0x6C6CB4D8, 0x5656FAAC, + 0xF4F407F3, 0xEAEA25CF, 0x6565AFCA, 0x7A7A8EF4, 0xAEAEE947, 0x08081810, + 0xBABAD56F, 0x787888F0, 0x25256F4A, 0x2E2E725C, 0x1C1C2438, 0xA6A6F157, + 0xB4B4C773, 0xC6C65197, 0xE8E823CB, 0xDDDD7CA1, 0x74749CE8, 0x1F1F213E, + 0x4B4BDD96, 0xBDBDDC61, 0x8B8B860D, 0x8A8A850F, 0x707090E0, 0x3E3E427C, + 0xB5B5C471, 0x6666AACC, 0x4848D890, 0x03030506, 0xF6F601F7, 0x0E0E121C, + 0x6161A3C2, 0x35355F6A, 0x5757F9AE, 0xB9B9D069, 0x86869117, 0xC1C15899, + 0x1D1D273A, 0x9E9EB927, 0xE1E138D9, 0xF8F813EB, 0x9898B32B, 0x11113322, + 0x6969BBD2, 0xD9D970A9, 0x8E8E8907, 0x9494A733, 0x9B9BB62D, 0x1E1E223C, + 0x87879215, 0xE9E920C9, 0xCECE4987, 0x5555FFAA, 0x28287850, 0xDFDF7AA5, + 0x8C8C8F03, 0xA1A1F859, 0x89898009, 0x0D0D171A, 0xBFBFDA65, 0xE6E631D7, + 0x4242C684, 0x6868B8D0, 0x4141C382, 0x9999B029, 0x2D2D775A, 0x0F0F111E, + 0xB0B0CB7B, 0x5454FCA8, 0xBBBBD66D, 0x16163A2C }; + +const u32bit AES::TD[1024] = { + 0x51F4A750, 0x7E416553, 0x1A17A4C3, 0x3A275E96, 0x3BAB6BCB, 0x1F9D45F1, + 0xACFA58AB, 0x4BE30393, 0x2030FA55, 0xAD766DF6, 0x88CC7691, 0xF5024C25, + 0x4FE5D7FC, 0xC52ACBD7, 0x26354480, 0xB562A38F, 0xDEB15A49, 0x25BA1B67, + 0x45EA0E98, 0x5DFEC0E1, 0xC32F7502, 0x814CF012, 0x8D4697A3, 0x6BD3F9C6, + 0x038F5FE7, 0x15929C95, 0xBF6D7AEB, 0x955259DA, 0xD4BE832D, 0x587421D3, + 0x49E06929, 0x8EC9C844, 0x75C2896A, 0xF48E7978, 0x99583E6B, 0x27B971DD, + 0xBEE14FB6, 0xF088AD17, 0xC920AC66, 0x7DCE3AB4, 0x63DF4A18, 0xE51A3182, + 0x97513360, 0x62537F45, 0xB16477E0, 0xBB6BAE84, 0xFE81A01C, 0xF9082B94, + 0x70486858, 0x8F45FD19, 0x94DE6C87, 0x527BF8B7, 0xAB73D323, 0x724B02E2, + 0xE31F8F57, 0x6655AB2A, 0xB2EB2807, 0x2FB5C203, 0x86C57B9A, 0xD33708A5, + 0x302887F2, 0x23BFA5B2, 0x02036ABA, 0xED16825C, 0x8ACF1C2B, 0xA779B492, + 0xF307F2F0, 0x4E69E2A1, 0x65DAF4CD, 0x0605BED5, 0xD134621F, 0xC4A6FE8A, + 0x342E539D, 0xA2F355A0, 0x058AE132, 0xA4F6EB75, 0x0B83EC39, 0x4060EFAA, + 0x5E719F06, 0xBD6E1051, 0x3E218AF9, 0x96DD063D, 0xDD3E05AE, 0x4DE6BD46, + 0x91548DB5, 0x71C45D05, 0x0406D46F, 0x605015FF, 0x1998FB24, 0xD6BDE997, + 0x894043CC, 0x67D99E77, 0xB0E842BD, 0x07898B88, 0xE7195B38, 0x79C8EEDB, + 0xA17C0A47, 0x7C420FE9, 0xF8841EC9, 0x00000000, 0x09808683, 0x322BED48, + 0x1E1170AC, 0x6C5A724E, 0xFD0EFFFB, 0x0F853856, 0x3DAED51E, 0x362D3927, + 0x0A0FD964, 0x685CA621, 0x9B5B54D1, 0x24362E3A, 0x0C0A67B1, 0x9357E70F, + 0xB4EE96D2, 0x1B9B919E, 0x80C0C54F, 0x61DC20A2, 0x5A774B69, 0x1C121A16, + 0xE293BA0A, 0xC0A02AE5, 0x3C22E043, 0x121B171D, 0x0E090D0B, 0xF28BC7AD, + 0x2DB6A8B9, 0x141EA9C8, 0x57F11985, 0xAF75074C, 0xEE99DDBB, 0xA37F60FD, + 0xF701269F, 0x5C72F5BC, 0x44663BC5, 0x5BFB7E34, 0x8B432976, 0xCB23C6DC, + 0xB6EDFC68, 0xB8E4F163, 0xD731DCCA, 0x42638510, 0x13972240, 0x84C61120, + 0x854A247D, 0xD2BB3DF8, 0xAEF93211, 0xC729A16D, 0x1D9E2F4B, 0xDCB230F3, + 0x0D8652EC, 0x77C1E3D0, 0x2BB3166C, 0xA970B999, 0x119448FA, 0x47E96422, + 0xA8FC8CC4, 0xA0F03F1A, 0x567D2CD8, 0x223390EF, 0x87494EC7, 0xD938D1C1, + 0x8CCAA2FE, 0x98D40B36, 0xA6F581CF, 0xA57ADE28, 0xDAB78E26, 0x3FADBFA4, + 0x2C3A9DE4, 0x5078920D, 0x6A5FCC9B, 0x547E4662, 0xF68D13C2, 0x90D8B8E8, + 0x2E39F75E, 0x82C3AFF5, 0x9F5D80BE, 0x69D0937C, 0x6FD52DA9, 0xCF2512B3, + 0xC8AC993B, 0x10187DA7, 0xE89C636E, 0xDB3BBB7B, 0xCD267809, 0x6E5918F4, + 0xEC9AB701, 0x834F9AA8, 0xE6956E65, 0xAAFFE67E, 0x21BCCF08, 0xEF15E8E6, + 0xBAE79BD9, 0x4A6F36CE, 0xEA9F09D4, 0x29B07CD6, 0x31A4B2AF, 0x2A3F2331, + 0xC6A59430, 0x35A266C0, 0x744EBC37, 0xFC82CAA6, 0xE090D0B0, 0x33A7D815, + 0xF104984A, 0x41ECDAF7, 0x7FCD500E, 0x1791F62F, 0x764DD68D, 0x43EFB04D, + 0xCCAA4D54, 0xE49604DF, 0x9ED1B5E3, 0x4C6A881B, 0xC12C1FB8, 0x4665517F, + 0x9D5EEA04, 0x018C355D, 0xFA877473, 0xFB0B412E, 0xB3671D5A, 0x92DBD252, + 0xE9105633, 0x6DD64713, 0x9AD7618C, 0x37A10C7A, 0x59F8148E, 0xEB133C89, + 0xCEA927EE, 0xB761C935, 0xE11CE5ED, 0x7A47B13C, 0x9CD2DF59, 0x55F2733F, + 0x1814CE79, 0x73C737BF, 0x53F7CDEA, 0x5FFDAA5B, 0xDF3D6F14, 0x7844DB86, + 0xCAAFF381, 0xB968C43E, 0x3824342C, 0xC2A3405F, 0x161DC372, 0xBCE2250C, + 0x283C498B, 0xFF0D9541, 0x39A80171, 0x080CB3DE, 0xD8B4E49C, 0x6456C190, + 0x7BCB8461, 0xD532B670, 0x486C5C74, 0xD0B85742, 0x5051F4A7, 0x537E4165, + 0xC31A17A4, 0x963A275E, 0xCB3BAB6B, 0xF11F9D45, 0xABACFA58, 0x934BE303, + 0x552030FA, 0xF6AD766D, 0x9188CC76, 0x25F5024C, 0xFC4FE5D7, 0xD7C52ACB, + 0x80263544, 0x8FB562A3, 0x49DEB15A, 0x6725BA1B, 0x9845EA0E, 0xE15DFEC0, + 0x02C32F75, 0x12814CF0, 0xA38D4697, 0xC66BD3F9, 0xE7038F5F, 0x9515929C, + 0xEBBF6D7A, 0xDA955259, 0x2DD4BE83, 0xD3587421, 0x2949E069, 0x448EC9C8, + 0x6A75C289, 0x78F48E79, 0x6B99583E, 0xDD27B971, 0xB6BEE14F, 0x17F088AD, + 0x66C920AC, 0xB47DCE3A, 0x1863DF4A, 0x82E51A31, 0x60975133, 0x4562537F, + 0xE0B16477, 0x84BB6BAE, 0x1CFE81A0, 0x94F9082B, 0x58704868, 0x198F45FD, + 0x8794DE6C, 0xB7527BF8, 0x23AB73D3, 0xE2724B02, 0x57E31F8F, 0x2A6655AB, + 0x07B2EB28, 0x032FB5C2, 0x9A86C57B, 0xA5D33708, 0xF2302887, 0xB223BFA5, + 0xBA02036A, 0x5CED1682, 0x2B8ACF1C, 0x92A779B4, 0xF0F307F2, 0xA14E69E2, + 0xCD65DAF4, 0xD50605BE, 0x1FD13462, 0x8AC4A6FE, 0x9D342E53, 0xA0A2F355, + 0x32058AE1, 0x75A4F6EB, 0x390B83EC, 0xAA4060EF, 0x065E719F, 0x51BD6E10, + 0xF93E218A, 0x3D96DD06, 0xAEDD3E05, 0x464DE6BD, 0xB591548D, 0x0571C45D, + 0x6F0406D4, 0xFF605015, 0x241998FB, 0x97D6BDE9, 0xCC894043, 0x7767D99E, + 0xBDB0E842, 0x8807898B, 0x38E7195B, 0xDB79C8EE, 0x47A17C0A, 0xE97C420F, + 0xC9F8841E, 0x00000000, 0x83098086, 0x48322BED, 0xAC1E1170, 0x4E6C5A72, + 0xFBFD0EFF, 0x560F8538, 0x1E3DAED5, 0x27362D39, 0x640A0FD9, 0x21685CA6, + 0xD19B5B54, 0x3A24362E, 0xB10C0A67, 0x0F9357E7, 0xD2B4EE96, 0x9E1B9B91, + 0x4F80C0C5, 0xA261DC20, 0x695A774B, 0x161C121A, 0x0AE293BA, 0xE5C0A02A, + 0x433C22E0, 0x1D121B17, 0x0B0E090D, 0xADF28BC7, 0xB92DB6A8, 0xC8141EA9, + 0x8557F119, 0x4CAF7507, 0xBBEE99DD, 0xFDA37F60, 0x9FF70126, 0xBC5C72F5, + 0xC544663B, 0x345BFB7E, 0x768B4329, 0xDCCB23C6, 0x68B6EDFC, 0x63B8E4F1, + 0xCAD731DC, 0x10426385, 0x40139722, 0x2084C611, 0x7D854A24, 0xF8D2BB3D, + 0x11AEF932, 0x6DC729A1, 0x4B1D9E2F, 0xF3DCB230, 0xEC0D8652, 0xD077C1E3, + 0x6C2BB316, 0x99A970B9, 0xFA119448, 0x2247E964, 0xC4A8FC8C, 0x1AA0F03F, + 0xD8567D2C, 0xEF223390, 0xC787494E, 0xC1D938D1, 0xFE8CCAA2, 0x3698D40B, + 0xCFA6F581, 0x28A57ADE, 0x26DAB78E, 0xA43FADBF, 0xE42C3A9D, 0x0D507892, + 0x9B6A5FCC, 0x62547E46, 0xC2F68D13, 0xE890D8B8, 0x5E2E39F7, 0xF582C3AF, + 0xBE9F5D80, 0x7C69D093, 0xA96FD52D, 0xB3CF2512, 0x3BC8AC99, 0xA710187D, + 0x6EE89C63, 0x7BDB3BBB, 0x09CD2678, 0xF46E5918, 0x01EC9AB7, 0xA8834F9A, + 0x65E6956E, 0x7EAAFFE6, 0x0821BCCF, 0xE6EF15E8, 0xD9BAE79B, 0xCE4A6F36, + 0xD4EA9F09, 0xD629B07C, 0xAF31A4B2, 0x312A3F23, 0x30C6A594, 0xC035A266, + 0x37744EBC, 0xA6FC82CA, 0xB0E090D0, 0x1533A7D8, 0x4AF10498, 0xF741ECDA, + 0x0E7FCD50, 0x2F1791F6, 0x8D764DD6, 0x4D43EFB0, 0x54CCAA4D, 0xDFE49604, + 0xE39ED1B5, 0x1B4C6A88, 0xB8C12C1F, 0x7F466551, 0x049D5EEA, 0x5D018C35, + 0x73FA8774, 0x2EFB0B41, 0x5AB3671D, 0x5292DBD2, 0x33E91056, 0x136DD647, + 0x8C9AD761, 0x7A37A10C, 0x8E59F814, 0x89EB133C, 0xEECEA927, 0x35B761C9, + 0xEDE11CE5, 0x3C7A47B1, 0x599CD2DF, 0x3F55F273, 0x791814CE, 0xBF73C737, + 0xEA53F7CD, 0x5B5FFDAA, 0x14DF3D6F, 0x867844DB, 0x81CAAFF3, 0x3EB968C4, + 0x2C382434, 0x5FC2A340, 0x72161DC3, 0x0CBCE225, 0x8B283C49, 0x41FF0D95, + 0x7139A801, 0xDE080CB3, 0x9CD8B4E4, 0x906456C1, 0x617BCB84, 0x70D532B6, + 0x74486C5C, 0x42D0B857, 0xA75051F4, 0x65537E41, 0xA4C31A17, 0x5E963A27, + 0x6BCB3BAB, 0x45F11F9D, 0x58ABACFA, 0x03934BE3, 0xFA552030, 0x6DF6AD76, + 0x769188CC, 0x4C25F502, 0xD7FC4FE5, 0xCBD7C52A, 0x44802635, 0xA38FB562, + 0x5A49DEB1, 0x1B6725BA, 0x0E9845EA, 0xC0E15DFE, 0x7502C32F, 0xF012814C, + 0x97A38D46, 0xF9C66BD3, 0x5FE7038F, 0x9C951592, 0x7AEBBF6D, 0x59DA9552, + 0x832DD4BE, 0x21D35874, 0x692949E0, 0xC8448EC9, 0x896A75C2, 0x7978F48E, + 0x3E6B9958, 0x71DD27B9, 0x4FB6BEE1, 0xAD17F088, 0xAC66C920, 0x3AB47DCE, + 0x4A1863DF, 0x3182E51A, 0x33609751, 0x7F456253, 0x77E0B164, 0xAE84BB6B, + 0xA01CFE81, 0x2B94F908, 0x68587048, 0xFD198F45, 0x6C8794DE, 0xF8B7527B, + 0xD323AB73, 0x02E2724B, 0x8F57E31F, 0xAB2A6655, 0x2807B2EB, 0xC2032FB5, + 0x7B9A86C5, 0x08A5D337, 0x87F23028, 0xA5B223BF, 0x6ABA0203, 0x825CED16, + 0x1C2B8ACF, 0xB492A779, 0xF2F0F307, 0xE2A14E69, 0xF4CD65DA, 0xBED50605, + 0x621FD134, 0xFE8AC4A6, 0x539D342E, 0x55A0A2F3, 0xE132058A, 0xEB75A4F6, + 0xEC390B83, 0xEFAA4060, 0x9F065E71, 0x1051BD6E, 0x8AF93E21, 0x063D96DD, + 0x05AEDD3E, 0xBD464DE6, 0x8DB59154, 0x5D0571C4, 0xD46F0406, 0x15FF6050, + 0xFB241998, 0xE997D6BD, 0x43CC8940, 0x9E7767D9, 0x42BDB0E8, 0x8B880789, + 0x5B38E719, 0xEEDB79C8, 0x0A47A17C, 0x0FE97C42, 0x1EC9F884, 0x00000000, + 0x86830980, 0xED48322B, 0x70AC1E11, 0x724E6C5A, 0xFFFBFD0E, 0x38560F85, + 0xD51E3DAE, 0x3927362D, 0xD9640A0F, 0xA621685C, 0x54D19B5B, 0x2E3A2436, + 0x67B10C0A, 0xE70F9357, 0x96D2B4EE, 0x919E1B9B, 0xC54F80C0, 0x20A261DC, + 0x4B695A77, 0x1A161C12, 0xBA0AE293, 0x2AE5C0A0, 0xE0433C22, 0x171D121B, + 0x0D0B0E09, 0xC7ADF28B, 0xA8B92DB6, 0xA9C8141E, 0x198557F1, 0x074CAF75, + 0xDDBBEE99, 0x60FDA37F, 0x269FF701, 0xF5BC5C72, 0x3BC54466, 0x7E345BFB, + 0x29768B43, 0xC6DCCB23, 0xFC68B6ED, 0xF163B8E4, 0xDCCAD731, 0x85104263, + 0x22401397, 0x112084C6, 0x247D854A, 0x3DF8D2BB, 0x3211AEF9, 0xA16DC729, + 0x2F4B1D9E, 0x30F3DCB2, 0x52EC0D86, 0xE3D077C1, 0x166C2BB3, 0xB999A970, + 0x48FA1194, 0x642247E9, 0x8CC4A8FC, 0x3F1AA0F0, 0x2CD8567D, 0x90EF2233, + 0x4EC78749, 0xD1C1D938, 0xA2FE8CCA, 0x0B3698D4, 0x81CFA6F5, 0xDE28A57A, + 0x8E26DAB7, 0xBFA43FAD, 0x9DE42C3A, 0x920D5078, 0xCC9B6A5F, 0x4662547E, + 0x13C2F68D, 0xB8E890D8, 0xF75E2E39, 0xAFF582C3, 0x80BE9F5D, 0x937C69D0, + 0x2DA96FD5, 0x12B3CF25, 0x993BC8AC, 0x7DA71018, 0x636EE89C, 0xBB7BDB3B, + 0x7809CD26, 0x18F46E59, 0xB701EC9A, 0x9AA8834F, 0x6E65E695, 0xE67EAAFF, + 0xCF0821BC, 0xE8E6EF15, 0x9BD9BAE7, 0x36CE4A6F, 0x09D4EA9F, 0x7CD629B0, + 0xB2AF31A4, 0x23312A3F, 0x9430C6A5, 0x66C035A2, 0xBC37744E, 0xCAA6FC82, + 0xD0B0E090, 0xD81533A7, 0x984AF104, 0xDAF741EC, 0x500E7FCD, 0xF62F1791, + 0xD68D764D, 0xB04D43EF, 0x4D54CCAA, 0x04DFE496, 0xB5E39ED1, 0x881B4C6A, + 0x1FB8C12C, 0x517F4665, 0xEA049D5E, 0x355D018C, 0x7473FA87, 0x412EFB0B, + 0x1D5AB367, 0xD25292DB, 0x5633E910, 0x47136DD6, 0x618C9AD7, 0x0C7A37A1, + 0x148E59F8, 0x3C89EB13, 0x27EECEA9, 0xC935B761, 0xE5EDE11C, 0xB13C7A47, + 0xDF599CD2, 0x733F55F2, 0xCE791814, 0x37BF73C7, 0xCDEA53F7, 0xAA5B5FFD, + 0x6F14DF3D, 0xDB867844, 0xF381CAAF, 0xC43EB968, 0x342C3824, 0x405FC2A3, + 0xC372161D, 0x250CBCE2, 0x498B283C, 0x9541FF0D, 0x017139A8, 0xB3DE080C, + 0xE49CD8B4, 0xC1906456, 0x84617BCB, 0xB670D532, 0x5C74486C, 0x5742D0B8, + 0xF4A75051, 0x4165537E, 0x17A4C31A, 0x275E963A, 0xAB6BCB3B, 0x9D45F11F, + 0xFA58ABAC, 0xE303934B, 0x30FA5520, 0x766DF6AD, 0xCC769188, 0x024C25F5, + 0xE5D7FC4F, 0x2ACBD7C5, 0x35448026, 0x62A38FB5, 0xB15A49DE, 0xBA1B6725, + 0xEA0E9845, 0xFEC0E15D, 0x2F7502C3, 0x4CF01281, 0x4697A38D, 0xD3F9C66B, + 0x8F5FE703, 0x929C9515, 0x6D7AEBBF, 0x5259DA95, 0xBE832DD4, 0x7421D358, + 0xE0692949, 0xC9C8448E, 0xC2896A75, 0x8E7978F4, 0x583E6B99, 0xB971DD27, + 0xE14FB6BE, 0x88AD17F0, 0x20AC66C9, 0xCE3AB47D, 0xDF4A1863, 0x1A3182E5, + 0x51336097, 0x537F4562, 0x6477E0B1, 0x6BAE84BB, 0x81A01CFE, 0x082B94F9, + 0x48685870, 0x45FD198F, 0xDE6C8794, 0x7BF8B752, 0x73D323AB, 0x4B02E272, + 0x1F8F57E3, 0x55AB2A66, 0xEB2807B2, 0xB5C2032F, 0xC57B9A86, 0x3708A5D3, + 0x2887F230, 0xBFA5B223, 0x036ABA02, 0x16825CED, 0xCF1C2B8A, 0x79B492A7, + 0x07F2F0F3, 0x69E2A14E, 0xDAF4CD65, 0x05BED506, 0x34621FD1, 0xA6FE8AC4, + 0x2E539D34, 0xF355A0A2, 0x8AE13205, 0xF6EB75A4, 0x83EC390B, 0x60EFAA40, + 0x719F065E, 0x6E1051BD, 0x218AF93E, 0xDD063D96, 0x3E05AEDD, 0xE6BD464D, + 0x548DB591, 0xC45D0571, 0x06D46F04, 0x5015FF60, 0x98FB2419, 0xBDE997D6, + 0x4043CC89, 0xD99E7767, 0xE842BDB0, 0x898B8807, 0x195B38E7, 0xC8EEDB79, + 0x7C0A47A1, 0x420FE97C, 0x841EC9F8, 0x00000000, 0x80868309, 0x2BED4832, + 0x1170AC1E, 0x5A724E6C, 0x0EFFFBFD, 0x8538560F, 0xAED51E3D, 0x2D392736, + 0x0FD9640A, 0x5CA62168, 0x5B54D19B, 0x362E3A24, 0x0A67B10C, 0x57E70F93, + 0xEE96D2B4, 0x9B919E1B, 0xC0C54F80, 0xDC20A261, 0x774B695A, 0x121A161C, + 0x93BA0AE2, 0xA02AE5C0, 0x22E0433C, 0x1B171D12, 0x090D0B0E, 0x8BC7ADF2, + 0xB6A8B92D, 0x1EA9C814, 0xF1198557, 0x75074CAF, 0x99DDBBEE, 0x7F60FDA3, + 0x01269FF7, 0x72F5BC5C, 0x663BC544, 0xFB7E345B, 0x4329768B, 0x23C6DCCB, + 0xEDFC68B6, 0xE4F163B8, 0x31DCCAD7, 0x63851042, 0x97224013, 0xC6112084, + 0x4A247D85, 0xBB3DF8D2, 0xF93211AE, 0x29A16DC7, 0x9E2F4B1D, 0xB230F3DC, + 0x8652EC0D, 0xC1E3D077, 0xB3166C2B, 0x70B999A9, 0x9448FA11, 0xE9642247, + 0xFC8CC4A8, 0xF03F1AA0, 0x7D2CD856, 0x3390EF22, 0x494EC787, 0x38D1C1D9, + 0xCAA2FE8C, 0xD40B3698, 0xF581CFA6, 0x7ADE28A5, 0xB78E26DA, 0xADBFA43F, + 0x3A9DE42C, 0x78920D50, 0x5FCC9B6A, 0x7E466254, 0x8D13C2F6, 0xD8B8E890, + 0x39F75E2E, 0xC3AFF582, 0x5D80BE9F, 0xD0937C69, 0xD52DA96F, 0x2512B3CF, + 0xAC993BC8, 0x187DA710, 0x9C636EE8, 0x3BBB7BDB, 0x267809CD, 0x5918F46E, + 0x9AB701EC, 0x4F9AA883, 0x956E65E6, 0xFFE67EAA, 0xBCCF0821, 0x15E8E6EF, + 0xE79BD9BA, 0x6F36CE4A, 0x9F09D4EA, 0xB07CD629, 0xA4B2AF31, 0x3F23312A, + 0xA59430C6, 0xA266C035, 0x4EBC3774, 0x82CAA6FC, 0x90D0B0E0, 0xA7D81533, + 0x04984AF1, 0xECDAF741, 0xCD500E7F, 0x91F62F17, 0x4DD68D76, 0xEFB04D43, + 0xAA4D54CC, 0x9604DFE4, 0xD1B5E39E, 0x6A881B4C, 0x2C1FB8C1, 0x65517F46, + 0x5EEA049D, 0x8C355D01, 0x877473FA, 0x0B412EFB, 0x671D5AB3, 0xDBD25292, + 0x105633E9, 0xD647136D, 0xD7618C9A, 0xA10C7A37, 0xF8148E59, 0x133C89EB, + 0xA927EECE, 0x61C935B7, 0x1CE5EDE1, 0x47B13C7A, 0xD2DF599C, 0xF2733F55, + 0x14CE7918, 0xC737BF73, 0xF7CDEA53, 0xFDAA5B5F, 0x3D6F14DF, 0x44DB8678, + 0xAFF381CA, 0x68C43EB9, 0x24342C38, 0xA3405FC2, 0x1DC37216, 0xE2250CBC, + 0x3C498B28, 0x0D9541FF, 0xA8017139, 0x0CB3DE08, 0xB4E49CD8, 0x56C19064, + 0xCB84617B, 0x32B670D5, 0x6C5C7448, 0xB85742D0 }; + +} diff --git a/botan/src/block/aes/info.txt b/botan/src/block/aes/info.txt new file mode 100644 index 0000000..2803ccc --- /dev/null +++ b/botan/src/block/aes/info.txt @@ -0,0 +1,11 @@ +realname "AES" + +define AES + +load_on auto + +<add> +aes.cpp +aes.h +aes_tab.cpp +</add> diff --git a/botan/src/block/block_cipher.h b/botan/src/block/block_cipher.h new file mode 100644 index 0000000..01c45af --- /dev/null +++ b/botan/src/block/block_cipher.h @@ -0,0 +1,100 @@ +/** +* Block Cipher Base Class +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_BLOCK_CIPHER_H__ +#define BOTAN_BLOCK_CIPHER_H__ + +#include <botan/sym_algo.h> + +namespace Botan { + +/** +* This class represents a block cipher object. +* +* It would be very useful to extend this interface to support the +* encryption of multiple blocks at a time. This could help +* performance, wrt cache effects in the software implementations, and +* could be a big deal when supporting block ciphers implemented as +* hardware devices. It could be used by implementations of ECB, and +* more importantly counter mode (which most designs are moving to, due +* to the parallelism possible in counter mode which is not the case +* with feedback-based modes like CBC). +* +* Probable future API here: +* virtual void encrypt_n(const byte in[], byte out[], +* u32bit blocks) const = 0; +* virtual void decrypt_n(const byte in[], byte out[], +* u32bit blocks) const = 0; +*/ +class BOTAN_DLL BlockCipher : public SymmetricAlgorithm + { + public: + /** + * The block size of this algorithm. + */ + const u32bit BLOCK_SIZE; + + /** + * Encrypt a block. + * @param in The plaintext block to be encrypted as a byte array. + * Must be of length BLOCK_SIZE. + * @param out The byte array designated to hold the encrypted block. + * Must be of length BLOCK_SIZE. + */ + void encrypt(const byte in[], byte out[]) const { enc(in, out); } + + /** + * Decrypt a block. + * @param in The ciphertext block to be decypted as a byte array. + * Must be of length BLOCK_SIZE. + * @param out The byte array designated to hold the decrypted block. + * Must be of length BLOCK_SIZE. + */ + void decrypt(const byte in[], byte out[]) const { dec(in, out); } + + /** + * Encrypt a block. + * @param in The plaintext block to be encrypted as a byte array. + * Must be of length BLOCK_SIZE. Will hold the result when the function + * has finished. + */ + void encrypt(byte block[]) const { enc(block, block); } + + /** + * Decrypt a block. + * @param in The ciphertext block to be decrypted as a byte array. + * Must be of length BLOCK_SIZE. Will hold the result when the function + * has finished. + */ + void decrypt(byte block[]) const { dec(block, block); } + + /** + * Get a new object representing the same algorithm as *this + */ + virtual BlockCipher* clone() const = 0; + + /** + * Zeroize internal state + */ + virtual void clear() throw() = 0; + + BlockCipher(u32bit block_size, + u32bit key_min, + u32bit key_max = 0, + u32bit key_mod = 1) : + SymmetricAlgorithm(key_min, key_max, key_mod), + BLOCK_SIZE(block_size) {} + + virtual ~BlockCipher() {} + private: + virtual void enc(const byte[], byte[]) const = 0; + virtual void dec(const byte[], byte[]) const = 0; + }; + +} + +#endif diff --git a/botan/src/block/blowfish/blfs_tab.cpp b/botan/src/block/blowfish/blfs_tab.cpp new file mode 100644 index 0000000..070fa6c --- /dev/null +++ b/botan/src/block/blowfish/blfs_tab.cpp @@ -0,0 +1,190 @@ +/* +* S-Box and P-Box Tables for Blowfish +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/blowfish.h> + +namespace Botan { + +const u32bit Blowfish::P_INIT[18] = { + 0x243F6A88, 0x85A308D3, 0x13198A2E, 0x03707344, 0xA4093822, 0x299F31D0, + 0x082EFA98, 0xEC4E6C89, 0x452821E6, 0x38D01377, 0xBE5466CF, 0x34E90C6C, + 0xC0AC29B7, 0xC97C50DD, 0x3F84D5B5, 0xB5470917, 0x9216D5D9, 0x8979FB1B }; + +const u32bit Blowfish::S_INIT[1024] = { + 0xD1310BA6, 0x98DFB5AC, 0x2FFD72DB, 0xD01ADFB7, 0xB8E1AFED, 0x6A267E96, + 0xBA7C9045, 0xF12C7F99, 0x24A19947, 0xB3916CF7, 0x0801F2E2, 0x858EFC16, + 0x636920D8, 0x71574E69, 0xA458FEA3, 0xF4933D7E, 0x0D95748F, 0x728EB658, + 0x718BCD58, 0x82154AEE, 0x7B54A41D, 0xC25A59B5, 0x9C30D539, 0x2AF26013, + 0xC5D1B023, 0x286085F0, 0xCA417918, 0xB8DB38EF, 0x8E79DCB0, 0x603A180E, + 0x6C9E0E8B, 0xB01E8A3E, 0xD71577C1, 0xBD314B27, 0x78AF2FDA, 0x55605C60, + 0xE65525F3, 0xAA55AB94, 0x57489862, 0x63E81440, 0x55CA396A, 0x2AAB10B6, + 0xB4CC5C34, 0x1141E8CE, 0xA15486AF, 0x7C72E993, 0xB3EE1411, 0x636FBC2A, + 0x2BA9C55D, 0x741831F6, 0xCE5C3E16, 0x9B87931E, 0xAFD6BA33, 0x6C24CF5C, + 0x7A325381, 0x28958677, 0x3B8F4898, 0x6B4BB9AF, 0xC4BFE81B, 0x66282193, + 0x61D809CC, 0xFB21A991, 0x487CAC60, 0x5DEC8032, 0xEF845D5D, 0xE98575B1, + 0xDC262302, 0xEB651B88, 0x23893E81, 0xD396ACC5, 0x0F6D6FF3, 0x83F44239, + 0x2E0B4482, 0xA4842004, 0x69C8F04A, 0x9E1F9B5E, 0x21C66842, 0xF6E96C9A, + 0x670C9C61, 0xABD388F0, 0x6A51A0D2, 0xD8542F68, 0x960FA728, 0xAB5133A3, + 0x6EEF0B6C, 0x137A3BE4, 0xBA3BF050, 0x7EFB2A98, 0xA1F1651D, 0x39AF0176, + 0x66CA593E, 0x82430E88, 0x8CEE8619, 0x456F9FB4, 0x7D84A5C3, 0x3B8B5EBE, + 0xE06F75D8, 0x85C12073, 0x401A449F, 0x56C16AA6, 0x4ED3AA62, 0x363F7706, + 0x1BFEDF72, 0x429B023D, 0x37D0D724, 0xD00A1248, 0xDB0FEAD3, 0x49F1C09B, + 0x075372C9, 0x80991B7B, 0x25D479D8, 0xF6E8DEF7, 0xE3FE501A, 0xB6794C3B, + 0x976CE0BD, 0x04C006BA, 0xC1A94FB6, 0x409F60C4, 0x5E5C9EC2, 0x196A2463, + 0x68FB6FAF, 0x3E6C53B5, 0x1339B2EB, 0x3B52EC6F, 0x6DFC511F, 0x9B30952C, + 0xCC814544, 0xAF5EBD09, 0xBEE3D004, 0xDE334AFD, 0x660F2807, 0x192E4BB3, + 0xC0CBA857, 0x45C8740F, 0xD20B5F39, 0xB9D3FBDB, 0x5579C0BD, 0x1A60320A, + 0xD6A100C6, 0x402C7279, 0x679F25FE, 0xFB1FA3CC, 0x8EA5E9F8, 0xDB3222F8, + 0x3C7516DF, 0xFD616B15, 0x2F501EC8, 0xAD0552AB, 0x323DB5FA, 0xFD238760, + 0x53317B48, 0x3E00DF82, 0x9E5C57BB, 0xCA6F8CA0, 0x1A87562E, 0xDF1769DB, + 0xD542A8F6, 0x287EFFC3, 0xAC6732C6, 0x8C4F5573, 0x695B27B0, 0xBBCA58C8, + 0xE1FFA35D, 0xB8F011A0, 0x10FA3D98, 0xFD2183B8, 0x4AFCB56C, 0x2DD1D35B, + 0x9A53E479, 0xB6F84565, 0xD28E49BC, 0x4BFB9790, 0xE1DDF2DA, 0xA4CB7E33, + 0x62FB1341, 0xCEE4C6E8, 0xEF20CADA, 0x36774C01, 0xD07E9EFE, 0x2BF11FB4, + 0x95DBDA4D, 0xAE909198, 0xEAAD8E71, 0x6B93D5A0, 0xD08ED1D0, 0xAFC725E0, + 0x8E3C5B2F, 0x8E7594B7, 0x8FF6E2FB, 0xF2122B64, 0x8888B812, 0x900DF01C, + 0x4FAD5EA0, 0x688FC31C, 0xD1CFF191, 0xB3A8C1AD, 0x2F2F2218, 0xBE0E1777, + 0xEA752DFE, 0x8B021FA1, 0xE5A0CC0F, 0xB56F74E8, 0x18ACF3D6, 0xCE89E299, + 0xB4A84FE0, 0xFD13E0B7, 0x7CC43B81, 0xD2ADA8D9, 0x165FA266, 0x80957705, + 0x93CC7314, 0x211A1477, 0xE6AD2065, 0x77B5FA86, 0xC75442F5, 0xFB9D35CF, + 0xEBCDAF0C, 0x7B3E89A0, 0xD6411BD3, 0xAE1E7E49, 0x00250E2D, 0x2071B35E, + 0x226800BB, 0x57B8E0AF, 0x2464369B, 0xF009B91E, 0x5563911D, 0x59DFA6AA, + 0x78C14389, 0xD95A537F, 0x207D5BA2, 0x02E5B9C5, 0x83260376, 0x6295CFA9, + 0x11C81968, 0x4E734A41, 0xB3472DCA, 0x7B14A94A, 0x1B510052, 0x9A532915, + 0xD60F573F, 0xBC9BC6E4, 0x2B60A476, 0x81E67400, 0x08BA6FB5, 0x571BE91F, + 0xF296EC6B, 0x2A0DD915, 0xB6636521, 0xE7B9F9B6, 0xFF34052E, 0xC5855664, + 0x53B02D5D, 0xA99F8FA1, 0x08BA4799, 0x6E85076A, 0x4B7A70E9, 0xB5B32944, + 0xDB75092E, 0xC4192623, 0xAD6EA6B0, 0x49A7DF7D, 0x9CEE60B8, 0x8FEDB266, + 0xECAA8C71, 0x699A17FF, 0x5664526C, 0xC2B19EE1, 0x193602A5, 0x75094C29, + 0xA0591340, 0xE4183A3E, 0x3F54989A, 0x5B429D65, 0x6B8FE4D6, 0x99F73FD6, + 0xA1D29C07, 0xEFE830F5, 0x4D2D38E6, 0xF0255DC1, 0x4CDD2086, 0x8470EB26, + 0x6382E9C6, 0x021ECC5E, 0x09686B3F, 0x3EBAEFC9, 0x3C971814, 0x6B6A70A1, + 0x687F3584, 0x52A0E286, 0xB79C5305, 0xAA500737, 0x3E07841C, 0x7FDEAE5C, + 0x8E7D44EC, 0x5716F2B8, 0xB03ADA37, 0xF0500C0D, 0xF01C1F04, 0x0200B3FF, + 0xAE0CF51A, 0x3CB574B2, 0x25837A58, 0xDC0921BD, 0xD19113F9, 0x7CA92FF6, + 0x94324773, 0x22F54701, 0x3AE5E581, 0x37C2DADC, 0xC8B57634, 0x9AF3DDA7, + 0xA9446146, 0x0FD0030E, 0xECC8C73E, 0xA4751E41, 0xE238CD99, 0x3BEA0E2F, + 0x3280BBA1, 0x183EB331, 0x4E548B38, 0x4F6DB908, 0x6F420D03, 0xF60A04BF, + 0x2CB81290, 0x24977C79, 0x5679B072, 0xBCAF89AF, 0xDE9A771F, 0xD9930810, + 0xB38BAE12, 0xDCCF3F2E, 0x5512721F, 0x2E6B7124, 0x501ADDE6, 0x9F84CD87, + 0x7A584718, 0x7408DA17, 0xBC9F9ABC, 0xE94B7D8C, 0xEC7AEC3A, 0xDB851DFA, + 0x63094366, 0xC464C3D2, 0xEF1C1847, 0x3215D908, 0xDD433B37, 0x24C2BA16, + 0x12A14D43, 0x2A65C451, 0x50940002, 0x133AE4DD, 0x71DFF89E, 0x10314E55, + 0x81AC77D6, 0x5F11199B, 0x043556F1, 0xD7A3C76B, 0x3C11183B, 0x5924A509, + 0xF28FE6ED, 0x97F1FBFA, 0x9EBABF2C, 0x1E153C6E, 0x86E34570, 0xEAE96FB1, + 0x860E5E0A, 0x5A3E2AB3, 0x771FE71C, 0x4E3D06FA, 0x2965DCB9, 0x99E71D0F, + 0x803E89D6, 0x5266C825, 0x2E4CC978, 0x9C10B36A, 0xC6150EBA, 0x94E2EA78, + 0xA5FC3C53, 0x1E0A2DF4, 0xF2F74EA7, 0x361D2B3D, 0x1939260F, 0x19C27960, + 0x5223A708, 0xF71312B6, 0xEBADFE6E, 0xEAC31F66, 0xE3BC4595, 0xA67BC883, + 0xB17F37D1, 0x018CFF28, 0xC332DDEF, 0xBE6C5AA5, 0x65582185, 0x68AB9802, + 0xEECEA50F, 0xDB2F953B, 0x2AEF7DAD, 0x5B6E2F84, 0x1521B628, 0x29076170, + 0xECDD4775, 0x619F1510, 0x13CCA830, 0xEB61BD96, 0x0334FE1E, 0xAA0363CF, + 0xB5735C90, 0x4C70A239, 0xD59E9E0B, 0xCBAADE14, 0xEECC86BC, 0x60622CA7, + 0x9CAB5CAB, 0xB2F3846E, 0x648B1EAF, 0x19BDF0CA, 0xA02369B9, 0x655ABB50, + 0x40685A32, 0x3C2AB4B3, 0x319EE9D5, 0xC021B8F7, 0x9B540B19, 0x875FA099, + 0x95F7997E, 0x623D7DA8, 0xF837889A, 0x97E32D77, 0x11ED935F, 0x16681281, + 0x0E358829, 0xC7E61FD6, 0x96DEDFA1, 0x7858BA99, 0x57F584A5, 0x1B227263, + 0x9B83C3FF, 0x1AC24696, 0xCDB30AEB, 0x532E3054, 0x8FD948E4, 0x6DBC3128, + 0x58EBF2EF, 0x34C6FFEA, 0xFE28ED61, 0xEE7C3C73, 0x5D4A14D9, 0xE864B7E3, + 0x42105D14, 0x203E13E0, 0x45EEE2B6, 0xA3AAABEA, 0xDB6C4F15, 0xFACB4FD0, + 0xC742F442, 0xEF6ABBB5, 0x654F3B1D, 0x41CD2105, 0xD81E799E, 0x86854DC7, + 0xE44B476A, 0x3D816250, 0xCF62A1F2, 0x5B8D2646, 0xFC8883A0, 0xC1C7B6A3, + 0x7F1524C3, 0x69CB7492, 0x47848A0B, 0x5692B285, 0x095BBF00, 0xAD19489D, + 0x1462B174, 0x23820E00, 0x58428D2A, 0x0C55F5EA, 0x1DADF43E, 0x233F7061, + 0x3372F092, 0x8D937E41, 0xD65FECF1, 0x6C223BDB, 0x7CDE3759, 0xCBEE7460, + 0x4085F2A7, 0xCE77326E, 0xA6078084, 0x19F8509E, 0xE8EFD855, 0x61D99735, + 0xA969A7AA, 0xC50C06C2, 0x5A04ABFC, 0x800BCADC, 0x9E447A2E, 0xC3453484, + 0xFDD56705, 0x0E1E9EC9, 0xDB73DBD3, 0x105588CD, 0x675FDA79, 0xE3674340, + 0xC5C43465, 0x713E38D8, 0x3D28F89E, 0xF16DFF20, 0x153E21E7, 0x8FB03D4A, + 0xE6E39F2B, 0xDB83ADF7, 0xE93D5A68, 0x948140F7, 0xF64C261C, 0x94692934, + 0x411520F7, 0x7602D4F7, 0xBCF46B2E, 0xD4A20068, 0xD4082471, 0x3320F46A, + 0x43B7D4B7, 0x500061AF, 0x1E39F62E, 0x97244546, 0x14214F74, 0xBF8B8840, + 0x4D95FC1D, 0x96B591AF, 0x70F4DDD3, 0x66A02F45, 0xBFBC09EC, 0x03BD9785, + 0x7FAC6DD0, 0x31CB8504, 0x96EB27B3, 0x55FD3941, 0xDA2547E6, 0xABCA0A9A, + 0x28507825, 0x530429F4, 0x0A2C86DA, 0xE9B66DFB, 0x68DC1462, 0xD7486900, + 0x680EC0A4, 0x27A18DEE, 0x4F3FFEA2, 0xE887AD8C, 0xB58CE006, 0x7AF4D6B6, + 0xAACE1E7C, 0xD3375FEC, 0xCE78A399, 0x406B2A42, 0x20FE9E35, 0xD9F385B9, + 0xEE39D7AB, 0x3B124E8B, 0x1DC9FAF7, 0x4B6D1856, 0x26A36631, 0xEAE397B2, + 0x3A6EFA74, 0xDD5B4332, 0x6841E7F7, 0xCA7820FB, 0xFB0AF54E, 0xD8FEB397, + 0x454056AC, 0xBA489527, 0x55533A3A, 0x20838D87, 0xFE6BA9B7, 0xD096954B, + 0x55A867BC, 0xA1159A58, 0xCCA92963, 0x99E1DB33, 0xA62A4A56, 0x3F3125F9, + 0x5EF47E1C, 0x9029317C, 0xFDF8E802, 0x04272F70, 0x80BB155C, 0x05282CE3, + 0x95C11548, 0xE4C66D22, 0x48C1133F, 0xC70F86DC, 0x07F9C9EE, 0x41041F0F, + 0x404779A4, 0x5D886E17, 0x325F51EB, 0xD59BC0D1, 0xF2BCC18F, 0x41113564, + 0x257B7834, 0x602A9C60, 0xDFF8E8A3, 0x1F636C1B, 0x0E12B4C2, 0x02E1329E, + 0xAF664FD1, 0xCAD18115, 0x6B2395E0, 0x333E92E1, 0x3B240B62, 0xEEBEB922, + 0x85B2A20E, 0xE6BA0D99, 0xDE720C8C, 0x2DA2F728, 0xD0127845, 0x95B794FD, + 0x647D0862, 0xE7CCF5F0, 0x5449A36F, 0x877D48FA, 0xC39DFD27, 0xF33E8D1E, + 0x0A476341, 0x992EFF74, 0x3A6F6EAB, 0xF4F8FD37, 0xA812DC60, 0xA1EBDDF8, + 0x991BE14C, 0xDB6E6B0D, 0xC67B5510, 0x6D672C37, 0x2765D43B, 0xDCD0E804, + 0xF1290DC7, 0xCC00FFA3, 0xB5390F92, 0x690FED0B, 0x667B9FFB, 0xCEDB7D9C, + 0xA091CF0B, 0xD9155EA3, 0xBB132F88, 0x515BAD24, 0x7B9479BF, 0x763BD6EB, + 0x37392EB3, 0xCC115979, 0x8026E297, 0xF42E312D, 0x6842ADA7, 0xC66A2B3B, + 0x12754CCC, 0x782EF11C, 0x6A124237, 0xB79251E7, 0x06A1BBE6, 0x4BFB6350, + 0x1A6B1018, 0x11CAEDFA, 0x3D25BDD8, 0xE2E1C3C9, 0x44421659, 0x0A121386, + 0xD90CEC6E, 0xD5ABEA2A, 0x64AF674E, 0xDA86A85F, 0xBEBFE988, 0x64E4C3FE, + 0x9DBC8057, 0xF0F7C086, 0x60787BF8, 0x6003604D, 0xD1FD8346, 0xF6381FB0, + 0x7745AE04, 0xD736FCCC, 0x83426B33, 0xF01EAB71, 0xB0804187, 0x3C005E5F, + 0x77A057BE, 0xBDE8AE24, 0x55464299, 0xBF582E61, 0x4E58F48F, 0xF2DDFDA2, + 0xF474EF38, 0x8789BDC2, 0x5366F9C3, 0xC8B38E74, 0xB475F255, 0x46FCD9B9, + 0x7AEB2661, 0x8B1DDF84, 0x846A0E79, 0x915F95E2, 0x466E598E, 0x20B45770, + 0x8CD55591, 0xC902DE4C, 0xB90BACE1, 0xBB8205D0, 0x11A86248, 0x7574A99E, + 0xB77F19B6, 0xE0A9DC09, 0x662D09A1, 0xC4324633, 0xE85A1F02, 0x09F0BE8C, + 0x4A99A025, 0x1D6EFE10, 0x1AB93D1D, 0x0BA5A4DF, 0xA186F20F, 0x2868F169, + 0xDCB7DA83, 0x573906FE, 0xA1E2CE9B, 0x4FCD7F52, 0x50115E01, 0xA70683FA, + 0xA002B5C4, 0x0DE6D027, 0x9AF88C27, 0x773F8641, 0xC3604C06, 0x61A806B5, + 0xF0177A28, 0xC0F586E0, 0x006058AA, 0x30DC7D62, 0x11E69ED7, 0x2338EA63, + 0x53C2DD94, 0xC2C21634, 0xBBCBEE56, 0x90BCB6DE, 0xEBFC7DA1, 0xCE591D76, + 0x6F05E409, 0x4B7C0188, 0x39720A3D, 0x7C927C24, 0x86E3725F, 0x724D9DB9, + 0x1AC15BB4, 0xD39EB8FC, 0xED545578, 0x08FCA5B5, 0xD83D7CD3, 0x4DAD0FC4, + 0x1E50EF5E, 0xB161E6F8, 0xA28514D9, 0x6C51133C, 0x6FD5C7E7, 0x56E14EC4, + 0x362ABFCE, 0xDDC6C837, 0xD79A3234, 0x92638212, 0x670EFA8E, 0x406000E0, + 0x3A39CE37, 0xD3FAF5CF, 0xABC27737, 0x5AC52D1B, 0x5CB0679E, 0x4FA33742, + 0xD3822740, 0x99BC9BBE, 0xD5118E9D, 0xBF0F7315, 0xD62D1C7E, 0xC700C47B, + 0xB78C1B6B, 0x21A19045, 0xB26EB1BE, 0x6A366EB4, 0x5748AB2F, 0xBC946E79, + 0xC6A376D2, 0x6549C2C8, 0x530FF8EE, 0x468DDE7D, 0xD5730A1D, 0x4CD04DC6, + 0x2939BBDB, 0xA9BA4650, 0xAC9526E8, 0xBE5EE304, 0xA1FAD5F0, 0x6A2D519A, + 0x63EF8CE2, 0x9A86EE22, 0xC089C2B8, 0x43242EF6, 0xA51E03AA, 0x9CF2D0A4, + 0x83C061BA, 0x9BE96A4D, 0x8FE51550, 0xBA645BD6, 0x2826A2F9, 0xA73A3AE1, + 0x4BA99586, 0xEF5562E9, 0xC72FEFD3, 0xF752F7DA, 0x3F046F69, 0x77FA0A59, + 0x80E4A915, 0x87B08601, 0x9B09E6AD, 0x3B3EE593, 0xE990FD5A, 0x9E34D797, + 0x2CF0B7D9, 0x022B8B51, 0x96D5AC3A, 0x017DA67D, 0xD1CF3ED6, 0x7C7D2D28, + 0x1F9F25CF, 0xADF2B89B, 0x5AD6B472, 0x5A88F54C, 0xE029AC71, 0xE019A5E6, + 0x47B0ACFD, 0xED93FA9B, 0xE8D3C48D, 0x283B57CC, 0xF8D56629, 0x79132E28, + 0x785F0191, 0xED756055, 0xF7960E44, 0xE3D35E8C, 0x15056DD4, 0x88F46DBA, + 0x03A16125, 0x0564F0BD, 0xC3EB9E15, 0x3C9057A2, 0x97271AEC, 0xA93A072A, + 0x1B3F6D9B, 0x1E6321F5, 0xF59C66FB, 0x26DCF319, 0x7533D928, 0xB155FDF5, + 0x03563482, 0x8ABA3CBB, 0x28517711, 0xC20AD9F8, 0xABCC5167, 0xCCAD925F, + 0x4DE81751, 0x3830DC8E, 0x379D5862, 0x9320F991, 0xEA7A90C2, 0xFB3E7BCE, + 0x5121CE64, 0x774FBE32, 0xA8B6E37E, 0xC3293D46, 0x48DE5369, 0x6413E680, + 0xA2AE0810, 0xDD6DB224, 0x69852DFD, 0x09072166, 0xB39A460A, 0x6445C0DD, + 0x586CDECF, 0x1C20C8AE, 0x5BBEF7DD, 0x1B588D40, 0xCCD2017F, 0x6BB4E3BB, + 0xDDA26A7E, 0x3A59FF45, 0x3E350A44, 0xBCB4CDD5, 0x72EACEA8, 0xFA6484BB, + 0x8D6612AE, 0xBF3C6F47, 0xD29BE463, 0x542F5D9E, 0xAEC2771B, 0xF64E6370, + 0x740E0D8D, 0xE75B1357, 0xF8721671, 0xAF537D5D, 0x4040CB08, 0x4EB4E2CC, + 0x34D2466A, 0x0115AF84, 0xE1B00428, 0x95983A1D, 0x06B89FB4, 0xCE6EA048, + 0x6F3F3B82, 0x3520AB82, 0x011A1D4B, 0x277227F8, 0x611560B1, 0xE7933FDC, + 0xBB3A792B, 0x344525BD, 0xA08839E1, 0x51CE794B, 0x2F32C9B7, 0xA01FBAC9, + 0xE01CC87E, 0xBCC7D1F6, 0xCF0111C3, 0xA1E8AAC7, 0x1A908749, 0xD44FBD9A, + 0xD0DADECB, 0xD50ADA38, 0x0339C32A, 0xC6913667, 0x8DF9317C, 0xE0B12B4F, + 0xF79E59B7, 0x43F5BB3A, 0xF2D519FF, 0x27D9459C, 0xBF97222C, 0x15E6FC2A, + 0x0F91FC71, 0x9B941525, 0xFAE59361, 0xCEB69CEB, 0xC2A86459, 0x12BAA8D1, + 0xB6C1075E, 0xE3056A0C, 0x10D25065, 0xCB03A442, 0xE0EC6E0E, 0x1698DB3B, + 0x4C98A0BE, 0x3278E964, 0x9F1F9532, 0xE0D392DF, 0xD3A0342B, 0x8971F21E, + 0x1B0A7441, 0x4BA3348C, 0xC5BE7120, 0xC37632D8, 0xDF359F8D, 0x9B992F2E, + 0xE60B6F47, 0x0FE3F11D, 0xE54CDA54, 0x1EDAD891, 0xCE6279CF, 0xCD3E7E6F, + 0x1618B166, 0xFD2C1D05, 0x848FD2C5, 0xF6FB2299, 0xF523F357, 0xA6327623, + 0x93A83531, 0x56CCCD02, 0xACF08162, 0x5A75EBB5, 0x6E163697, 0x88D273CC, + 0xDE966292, 0x81B949D0, 0x4C50901B, 0x71C65614, 0xE6C6C7BD, 0x327A140A, + 0x45E1D006, 0xC3F27B9A, 0xC9AA53FD, 0x62A80F00, 0xBB25BFE2, 0x35BDD2F6, + 0x71126905, 0xB2040222, 0xB6CBCF7C, 0xCD769C2B, 0x53113EC0, 0x1640E3D3, + 0x38ABBD60, 0x2547ADF0, 0xBA38209C, 0xF746CE76, 0x77AFA1C5, 0x20756060, + 0x85CBFE4E, 0x8AE88DD8, 0x7AAAF9B0, 0x4CF9AA7E, 0x1948C25C, 0x02FB8A8C, + 0x01C36AE4, 0xD6EBE1F9, 0x90D4F869, 0xA65CDEA0, 0x3F09252D, 0xC208E69F, + 0xB74E6132, 0xCE77E25B, 0x578FDFE3, 0x3AC372E6 }; + +} diff --git a/botan/src/block/blowfish/blowfish.cpp b/botan/src/block/blowfish/blowfish.cpp new file mode 100644 index 0000000..b0599d6 --- /dev/null +++ b/botan/src/block/blowfish/blowfish.cpp @@ -0,0 +1,125 @@ +/* +* Blowfish +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/blowfish.h> +#include <botan/loadstor.h> + +namespace Botan { + +/* +* Blowfish Encryption +*/ +void Blowfish::enc(const byte in[], byte out[]) const + { + const u32bit* S1 = S + 0; + const u32bit* S2 = S + 256; + const u32bit* S3 = S + 512; + const u32bit* S4 = S + 768; + + u32bit L = load_be<u32bit>(in, 0); + u32bit R = load_be<u32bit>(in, 1); + + for(u32bit j = 0; j != 16; j += 2) + { + L ^= P[j]; + R ^= ((S1[get_byte(0, L)] + S2[get_byte(1, L)]) ^ + S3[get_byte(2, L)]) + S4[get_byte(3, L)]; + + R ^= P[j+1]; + L ^= ((S1[get_byte(0, R)] + S2[get_byte(1, R)]) ^ + S3[get_byte(2, R)]) + S4[get_byte(3, R)]; + } + + L ^= P[16]; R ^= P[17]; + + store_be(out, R, L); + } + +/* +* Blowfish Decryption +*/ +void Blowfish::dec(const byte in[], byte out[]) const + { + const u32bit* S1 = S + 0; + const u32bit* S2 = S + 256; + const u32bit* S3 = S + 512; + const u32bit* S4 = S + 768; + + u32bit L = load_be<u32bit>(in, 0); + u32bit R = load_be<u32bit>(in, 1); + + for(u32bit j = 17; j != 1; j -= 2) + { + L ^= P[j]; + R ^= ((S1[get_byte(0, L)] + S2[get_byte(1, L)]) ^ + S3[get_byte(2, L)]) + S4[get_byte(3, L)]; + + R ^= P[j-1]; + L ^= ((S1[get_byte(0, R)] + S2[get_byte(1, R)]) ^ + S3[get_byte(2, R)]) + S4[get_byte(3, R)]; + } + + L ^= P[1]; R ^= P[0]; + + store_be(out, R, L); + } + +/* +* Blowfish Key Schedule +*/ +void Blowfish::key_schedule(const byte key[], u32bit length) + { + clear(); + + for(u32bit j = 0, k = 0; j != 18; ++j, k += 4) + P[j] ^= make_u32bit(key[(k ) % length], key[(k+1) % length], + key[(k+2) % length], key[(k+3) % length]); + + u32bit L = 0, R = 0; + generate_sbox(P, 18, L, R); + generate_sbox(S, 1024, L, R); + } + +/* +* Generate one of the Sboxes +*/ +void Blowfish::generate_sbox(u32bit Box[], u32bit size, + u32bit& L, u32bit& R) const + { + const u32bit* S1 = S + 0; + const u32bit* S2 = S + 256; + const u32bit* S3 = S + 512; + const u32bit* S4 = S + 768; + + for(u32bit j = 0; j != size; j += 2) + { + for(u32bit k = 0; k != 16; k += 2) + { + L ^= P[k]; + R ^= ((S1[get_byte(0, L)] + S2[get_byte(1, L)]) ^ + S3[get_byte(2, L)]) + S4[get_byte(3, L)]; + + R ^= P[k+1]; + L ^= ((S1[get_byte(0, R)] + S2[get_byte(1, R)]) ^ + S3[get_byte(2, R)]) + S4[get_byte(3, R)]; + } + + u32bit T = R; R = L ^ P[16]; L = T ^ P[17]; + Box[j] = L; Box[j+1] = R; + } + } + +/* +* Clear memory of sensitive data +*/ +void Blowfish::clear() throw() + { + P.copy(P_INIT, 18); + S.copy(S_INIT, 1024); + } + +} diff --git a/botan/src/block/blowfish/blowfish.h b/botan/src/block/blowfish/blowfish.h new file mode 100644 index 0000000..f0f2641 --- /dev/null +++ b/botan/src/block/blowfish/blowfish.h @@ -0,0 +1,40 @@ +/* +* Blowfish +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_BLOWFISH_H__ +#define BOTAN_BLOWFISH_H__ + +#include <botan/block_cipher.h> + +namespace Botan { + +/* +* Blowfish +*/ +class BOTAN_DLL Blowfish : public BlockCipher + { + public: + void clear() throw(); + std::string name() const { return "Blowfish"; } + BlockCipher* clone() const { return new Blowfish; } + Blowfish() : BlockCipher(8, 1, 56) {} + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key_schedule(const byte[], u32bit); + void generate_sbox(u32bit[], u32bit, u32bit&, u32bit&) const; + + static const u32bit P_INIT[18]; + static const u32bit S_INIT[1024]; + + SecureBuffer<u32bit, 1024> S; + SecureBuffer<u32bit, 18> P; + }; + +} + +#endif diff --git a/botan/src/block/blowfish/info.txt b/botan/src/block/blowfish/info.txt new file mode 100644 index 0000000..0a9d2ad --- /dev/null +++ b/botan/src/block/blowfish/info.txt @@ -0,0 +1,11 @@ +realname "Blowfish" + +define BLOWFISH + +load_on auto + +<add> +blfs_tab.cpp +blowfish.cpp +blowfish.h +</add> diff --git a/botan/src/block/cast/cast128.cpp b/botan/src/block/cast/cast128.cpp new file mode 100644 index 0000000..046638a --- /dev/null +++ b/botan/src/block/cast/cast128.cpp @@ -0,0 +1,172 @@ +/* +* CAST-128 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/cast128.h> +#include <botan/loadstor.h> +#include <botan/rotate.h> + +namespace Botan { + +namespace { + +/* +* CAST-128 Round Type 1 +*/ +inline void R1(u32bit& L, u32bit R, u32bit MK, u32bit RK) + { + u32bit T = rotate_left(MK + R, RK); + L ^= (CAST_SBOX1[get_byte(0, T)] ^ CAST_SBOX2[get_byte(1, T)]) - + CAST_SBOX3[get_byte(2, T)] + CAST_SBOX4[get_byte(3, T)]; + } + +/* +* CAST-128 Round Type 2 +*/ +inline void R2(u32bit& L, u32bit R, u32bit MK, u32bit RK) + { + u32bit T = rotate_left(MK ^ R, RK); + L ^= (CAST_SBOX1[get_byte(0, T)] - CAST_SBOX2[get_byte(1, T)] + + CAST_SBOX3[get_byte(2, T)]) ^ CAST_SBOX4[get_byte(3, T)]; + } + +/* +* CAST-128 Round Type 3 +*/ +inline void R3(u32bit& L, u32bit R, u32bit MK, u32bit RK) + { + u32bit T = rotate_left(MK - R, RK); + L ^= ((CAST_SBOX1[get_byte(0, T)] + CAST_SBOX2[get_byte(1, T)]) ^ + CAST_SBOX3[get_byte(2, T)]) - CAST_SBOX4[get_byte(3, T)]; + } + +} + +/* +* CAST-128 Encryption +*/ +void CAST_128::enc(const byte in[], byte out[]) const + { + u32bit L = load_be<u32bit>(in, 0); + u32bit R = load_be<u32bit>(in, 1); + + R1(L, R, MK[ 0], RK[ 0]); + R2(R, L, MK[ 1], RK[ 1]); + R3(L, R, MK[ 2], RK[ 2]); + R1(R, L, MK[ 3], RK[ 3]); + R2(L, R, MK[ 4], RK[ 4]); + R3(R, L, MK[ 5], RK[ 5]); + R1(L, R, MK[ 6], RK[ 6]); + R2(R, L, MK[ 7], RK[ 7]); + R3(L, R, MK[ 8], RK[ 8]); + R1(R, L, MK[ 9], RK[ 9]); + R2(L, R, MK[10], RK[10]); + R3(R, L, MK[11], RK[11]); + R1(L, R, MK[12], RK[12]); + R2(R, L, MK[13], RK[13]); + R3(L, R, MK[14], RK[14]); + R1(R, L, MK[15], RK[15]); + + store_be(out, R, L); + } + +/* +* CAST-128 Decryption +*/ +void CAST_128::dec(const byte in[], byte out[]) const + { + u32bit L = load_be<u32bit>(in, 0); + u32bit R = load_be<u32bit>(in, 1); + + R1(L, R, MK[15], RK[15]); + R3(R, L, MK[14], RK[14]); + R2(L, R, MK[13], RK[13]); + R1(R, L, MK[12], RK[12]); + R3(L, R, MK[11], RK[11]); + R2(R, L, MK[10], RK[10]); + R1(L, R, MK[ 9], RK[ 9]); + R3(R, L, MK[ 8], RK[ 8]); + R2(L, R, MK[ 7], RK[ 7]); + R1(R, L, MK[ 6], RK[ 6]); + R3(L, R, MK[ 5], RK[ 5]); + R2(R, L, MK[ 4], RK[ 4]); + R1(L, R, MK[ 3], RK[ 3]); + R3(R, L, MK[ 2], RK[ 2]); + R2(L, R, MK[ 1], RK[ 1]); + R1(R, L, MK[ 0], RK[ 0]); + + store_be(out, R, L); + } + +/* +* CAST-128 Key Schedule +*/ +void CAST_128::key_schedule(const byte key[], u32bit length) + { + clear(); + SecureBuffer<u32bit, 4> X; + for(u32bit j = 0; j != length; ++j) + X[j/4] = (X[j/4] << 8) + key[j]; + + key_schedule(MK, X); + key_schedule(RK, X); + + for(u32bit j = 0; j != 16; ++j) + RK[j] %= 32; + } + +/* +* S-Box Based Key Expansion +*/ +void CAST_128::key_schedule(u32bit K[16], u32bit X[4]) + { + class ByteReader + { + public: + byte operator()(u32bit i) { return (X[i/4] >> (8*(3 - (i%4)))); } + ByteReader(const u32bit* x) : X(x) {} + private: + const u32bit* X; + }; + + SecureBuffer<u32bit, 4> Z; + ByteReader x(X), z(Z); + + Z[0] = X[0] ^ S5[x(13)] ^ S6[x(15)] ^ S7[x(12)] ^ S8[x(14)] ^ S7[x( 8)]; + Z[1] = X[2] ^ S5[z( 0)] ^ S6[z( 2)] ^ S7[z( 1)] ^ S8[z( 3)] ^ S8[x(10)]; + Z[2] = X[3] ^ S5[z( 7)] ^ S6[z( 6)] ^ S7[z( 5)] ^ S8[z( 4)] ^ S5[x( 9)]; + Z[3] = X[1] ^ S5[z(10)] ^ S6[z( 9)] ^ S7[z(11)] ^ S8[z( 8)] ^ S6[x(11)]; + K[ 0] = S5[z( 8)] ^ S6[z( 9)] ^ S7[z( 7)] ^ S8[z( 6)] ^ S5[z( 2)]; + K[ 1] = S5[z(10)] ^ S6[z(11)] ^ S7[z( 5)] ^ S8[z( 4)] ^ S6[z( 6)]; + K[ 2] = S5[z(12)] ^ S6[z(13)] ^ S7[z( 3)] ^ S8[z( 2)] ^ S7[z( 9)]; + K[ 3] = S5[z(14)] ^ S6[z(15)] ^ S7[z( 1)] ^ S8[z( 0)] ^ S8[z(12)]; + X[0] = Z[2] ^ S5[z( 5)] ^ S6[z( 7)] ^ S7[z( 4)] ^ S8[z( 6)] ^ S7[z( 0)]; + X[1] = Z[0] ^ S5[x( 0)] ^ S6[x( 2)] ^ S7[x( 1)] ^ S8[x( 3)] ^ S8[z( 2)]; + X[2] = Z[1] ^ S5[x( 7)] ^ S6[x( 6)] ^ S7[x( 5)] ^ S8[x( 4)] ^ S5[z( 1)]; + X[3] = Z[3] ^ S5[x(10)] ^ S6[x( 9)] ^ S7[x(11)] ^ S8[x( 8)] ^ S6[z( 3)]; + K[ 4] = S5[x( 3)] ^ S6[x( 2)] ^ S7[x(12)] ^ S8[x(13)] ^ S5[x( 8)]; + K[ 5] = S5[x( 1)] ^ S6[x( 0)] ^ S7[x(14)] ^ S8[x(15)] ^ S6[x(13)]; + K[ 6] = S5[x( 7)] ^ S6[x( 6)] ^ S7[x( 8)] ^ S8[x( 9)] ^ S7[x( 3)]; + K[ 7] = S5[x( 5)] ^ S6[x( 4)] ^ S7[x(10)] ^ S8[x(11)] ^ S8[x( 7)]; + Z[0] = X[0] ^ S5[x(13)] ^ S6[x(15)] ^ S7[x(12)] ^ S8[x(14)] ^ S7[x( 8)]; + Z[1] = X[2] ^ S5[z( 0)] ^ S6[z( 2)] ^ S7[z( 1)] ^ S8[z( 3)] ^ S8[x(10)]; + Z[2] = X[3] ^ S5[z( 7)] ^ S6[z( 6)] ^ S7[z( 5)] ^ S8[z( 4)] ^ S5[x( 9)]; + Z[3] = X[1] ^ S5[z(10)] ^ S6[z( 9)] ^ S7[z(11)] ^ S8[z( 8)] ^ S6[x(11)]; + K[ 8] = S5[z( 3)] ^ S6[z( 2)] ^ S7[z(12)] ^ S8[z(13)] ^ S5[z( 9)]; + K[ 9] = S5[z( 1)] ^ S6[z( 0)] ^ S7[z(14)] ^ S8[z(15)] ^ S6[z(12)]; + K[10] = S5[z( 7)] ^ S6[z( 6)] ^ S7[z( 8)] ^ S8[z( 9)] ^ S7[z( 2)]; + K[11] = S5[z( 5)] ^ S6[z( 4)] ^ S7[z(10)] ^ S8[z(11)] ^ S8[z( 6)]; + X[0] = Z[2] ^ S5[z( 5)] ^ S6[z( 7)] ^ S7[z( 4)] ^ S8[z( 6)] ^ S7[z( 0)]; + X[1] = Z[0] ^ S5[x( 0)] ^ S6[x( 2)] ^ S7[x( 1)] ^ S8[x( 3)] ^ S8[z( 2)]; + X[2] = Z[1] ^ S5[x( 7)] ^ S6[x( 6)] ^ S7[x( 5)] ^ S8[x( 4)] ^ S5[z( 1)]; + X[3] = Z[3] ^ S5[x(10)] ^ S6[x( 9)] ^ S7[x(11)] ^ S8[x( 8)] ^ S6[z( 3)]; + K[12] = S5[x( 8)] ^ S6[x( 9)] ^ S7[x( 7)] ^ S8[x( 6)] ^ S5[x( 3)]; + K[13] = S5[x(10)] ^ S6[x(11)] ^ S7[x( 5)] ^ S8[x( 4)] ^ S6[x( 7)]; + K[14] = S5[x(12)] ^ S6[x(13)] ^ S7[x( 3)] ^ S8[x( 2)] ^ S7[x( 8)]; + K[15] = S5[x(14)] ^ S6[x(15)] ^ S7[x( 1)] ^ S8[x( 0)] ^ S8[x(13)]; + } + +} diff --git a/botan/src/block/cast/cast128.h b/botan/src/block/cast/cast128.h new file mode 100644 index 0000000..6804814 --- /dev/null +++ b/botan/src/block/cast/cast128.h @@ -0,0 +1,47 @@ +/* +* CAST-128 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_CAST128_H__ +#define BOTAN_CAST128_H__ + +#include <botan/block_cipher.h> + +namespace Botan { + +/* +* CAST-128 +*/ +class BOTAN_DLL CAST_128 : public BlockCipher + { + public: + void clear() throw() { MK.clear(); RK.clear(); } + std::string name() const { return "CAST-128"; } + BlockCipher* clone() const { return new CAST_128; } + CAST_128() : BlockCipher(8, 11, 16) {} + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key_schedule(const byte[], u32bit); + + static void key_schedule(u32bit[16], u32bit[4]); + + static const u32bit S5[256]; + static const u32bit S6[256]; + static const u32bit S7[256]; + static const u32bit S8[256]; + + SecureBuffer<u32bit, 16> MK, RK; + }; + +extern const u32bit CAST_SBOX1[256]; +extern const u32bit CAST_SBOX2[256]; +extern const u32bit CAST_SBOX3[256]; +extern const u32bit CAST_SBOX4[256]; + +} + +#endif diff --git a/botan/src/block/cast/cast256.cpp b/botan/src/block/cast/cast256.cpp new file mode 100644 index 0000000..22ff876 --- /dev/null +++ b/botan/src/block/cast/cast256.cpp @@ -0,0 +1,165 @@ +/* +* CAST-256 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/cast256.h> +#include <botan/loadstor.h> +#include <botan/rotate.h> + +namespace Botan { + +namespace { + +/* +* CAST-256 Round Type 1 +*/ +void round1(u32bit& out, u32bit in, u32bit mask, u32bit rot) + { + u32bit temp = rotate_left(mask + in, rot); + out ^= (CAST_SBOX1[get_byte(0, temp)] ^ CAST_SBOX2[get_byte(1, temp)]) - + CAST_SBOX3[get_byte(2, temp)] + CAST_SBOX4[get_byte(3, temp)]; + } + +/* +* CAST-256 Round Type 2 +*/ +void round2(u32bit& out, u32bit in, u32bit mask, u32bit rot) + { + u32bit temp = rotate_left(mask ^ in, rot); + out ^= (CAST_SBOX1[get_byte(0, temp)] - CAST_SBOX2[get_byte(1, temp)] + + CAST_SBOX3[get_byte(2, temp)]) ^ CAST_SBOX4[get_byte(3, temp)]; + } + +/* +* CAST-256 Round Type 3 +*/ +void round3(u32bit& out, u32bit in, u32bit mask, u32bit rot) + { + u32bit temp = rotate_left(mask - in, rot); + out ^= ((CAST_SBOX1[get_byte(0, temp)] + CAST_SBOX2[get_byte(1, temp)]) ^ + CAST_SBOX3[get_byte(2, temp)]) - CAST_SBOX4[get_byte(3, temp)]; + } + +} + +/* +* CAST-256 Encryption +*/ +void CAST_256::enc(const byte in[], byte out[]) const + { + u32bit A = load_be<u32bit>(in, 0); + u32bit B = load_be<u32bit>(in, 1); + u32bit C = load_be<u32bit>(in, 2); + u32bit D = load_be<u32bit>(in, 3); + + round1(C, D, MK[ 0], RK[ 0]); round2(B, C, MK[ 1], RK[ 1]); + round3(A, B, MK[ 2], RK[ 2]); round1(D, A, MK[ 3], RK[ 3]); + round1(C, D, MK[ 4], RK[ 4]); round2(B, C, MK[ 5], RK[ 5]); + round3(A, B, MK[ 6], RK[ 6]); round1(D, A, MK[ 7], RK[ 7]); + round1(C, D, MK[ 8], RK[ 8]); round2(B, C, MK[ 9], RK[ 9]); + round3(A, B, MK[10], RK[10]); round1(D, A, MK[11], RK[11]); + round1(C, D, MK[12], RK[12]); round2(B, C, MK[13], RK[13]); + round3(A, B, MK[14], RK[14]); round1(D, A, MK[15], RK[15]); + round1(C, D, MK[16], RK[16]); round2(B, C, MK[17], RK[17]); + round3(A, B, MK[18], RK[18]); round1(D, A, MK[19], RK[19]); + round1(C, D, MK[20], RK[20]); round2(B, C, MK[21], RK[21]); + round3(A, B, MK[22], RK[22]); round1(D, A, MK[23], RK[23]); + round1(D, A, MK[27], RK[27]); round3(A, B, MK[26], RK[26]); + round2(B, C, MK[25], RK[25]); round1(C, D, MK[24], RK[24]); + round1(D, A, MK[31], RK[31]); round3(A, B, MK[30], RK[30]); + round2(B, C, MK[29], RK[29]); round1(C, D, MK[28], RK[28]); + round1(D, A, MK[35], RK[35]); round3(A, B, MK[34], RK[34]); + round2(B, C, MK[33], RK[33]); round1(C, D, MK[32], RK[32]); + round1(D, A, MK[39], RK[39]); round3(A, B, MK[38], RK[38]); + round2(B, C, MK[37], RK[37]); round1(C, D, MK[36], RK[36]); + round1(D, A, MK[43], RK[43]); round3(A, B, MK[42], RK[42]); + round2(B, C, MK[41], RK[41]); round1(C, D, MK[40], RK[40]); + round1(D, A, MK[47], RK[47]); round3(A, B, MK[46], RK[46]); + round2(B, C, MK[45], RK[45]); round1(C, D, MK[44], RK[44]); + + store_be(out, A, B, C, D); + } + +/* +* CAST-256 Decryption +*/ +void CAST_256::dec(const byte in[], byte out[]) const + { + u32bit A = load_be<u32bit>(in, 0); + u32bit B = load_be<u32bit>(in, 1); + u32bit C = load_be<u32bit>(in, 2); + u32bit D = load_be<u32bit>(in, 3); + + round1(C, D, MK[44], RK[44]); round2(B, C, MK[45], RK[45]); + round3(A, B, MK[46], RK[46]); round1(D, A, MK[47], RK[47]); + round1(C, D, MK[40], RK[40]); round2(B, C, MK[41], RK[41]); + round3(A, B, MK[42], RK[42]); round1(D, A, MK[43], RK[43]); + round1(C, D, MK[36], RK[36]); round2(B, C, MK[37], RK[37]); + round3(A, B, MK[38], RK[38]); round1(D, A, MK[39], RK[39]); + round1(C, D, MK[32], RK[32]); round2(B, C, MK[33], RK[33]); + round3(A, B, MK[34], RK[34]); round1(D, A, MK[35], RK[35]); + round1(C, D, MK[28], RK[28]); round2(B, C, MK[29], RK[29]); + round3(A, B, MK[30], RK[30]); round1(D, A, MK[31], RK[31]); + round1(C, D, MK[24], RK[24]); round2(B, C, MK[25], RK[25]); + round3(A, B, MK[26], RK[26]); round1(D, A, MK[27], RK[27]); + round1(D, A, MK[23], RK[23]); round3(A, B, MK[22], RK[22]); + round2(B, C, MK[21], RK[21]); round1(C, D, MK[20], RK[20]); + round1(D, A, MK[19], RK[19]); round3(A, B, MK[18], RK[18]); + round2(B, C, MK[17], RK[17]); round1(C, D, MK[16], RK[16]); + round1(D, A, MK[15], RK[15]); round3(A, B, MK[14], RK[14]); + round2(B, C, MK[13], RK[13]); round1(C, D, MK[12], RK[12]); + round1(D, A, MK[11], RK[11]); round3(A, B, MK[10], RK[10]); + round2(B, C, MK[ 9], RK[ 9]); round1(C, D, MK[ 8], RK[ 8]); + round1(D, A, MK[ 7], RK[ 7]); round3(A, B, MK[ 6], RK[ 6]); + round2(B, C, MK[ 5], RK[ 5]); round1(C, D, MK[ 4], RK[ 4]); + round1(D, A, MK[ 3], RK[ 3]); round3(A, B, MK[ 2], RK[ 2]); + round2(B, C, MK[ 1], RK[ 1]); round1(C, D, MK[ 0], RK[ 0]); + + store_be(out, A, B, C, D); + } + +/* +* CAST-256 Key Schedule +*/ +void CAST_256::key_schedule(const byte key[], u32bit length) + { + SecureBuffer<u32bit, 8> TMP; + for(u32bit j = 0; j != length; ++j) + TMP[j/4] = (TMP[j/4] << 8) + key[j]; + + u32bit A = TMP[0], B = TMP[1], C = TMP[2], D = TMP[3], + E = TMP[4], F = TMP[5], G = TMP[6], H = TMP[7]; + for(u32bit j = 0; j != 48; j += 4) + { + round1(G, H, KEY_MASK[4*j+ 0], KEY_ROT[(4*j+ 0) % 32]); + round2(F, G, KEY_MASK[4*j+ 1], KEY_ROT[(4*j+ 1) % 32]); + round3(E, F, KEY_MASK[4*j+ 2], KEY_ROT[(4*j+ 2) % 32]); + round1(D, E, KEY_MASK[4*j+ 3], KEY_ROT[(4*j+ 3) % 32]); + round2(C, D, KEY_MASK[4*j+ 4], KEY_ROT[(4*j+ 4) % 32]); + round3(B, C, KEY_MASK[4*j+ 5], KEY_ROT[(4*j+ 5) % 32]); + round1(A, B, KEY_MASK[4*j+ 6], KEY_ROT[(4*j+ 6) % 32]); + round2(H, A, KEY_MASK[4*j+ 7], KEY_ROT[(4*j+ 7) % 32]); + round1(G, H, KEY_MASK[4*j+ 8], KEY_ROT[(4*j+ 8) % 32]); + round2(F, G, KEY_MASK[4*j+ 9], KEY_ROT[(4*j+ 9) % 32]); + round3(E, F, KEY_MASK[4*j+10], KEY_ROT[(4*j+10) % 32]); + round1(D, E, KEY_MASK[4*j+11], KEY_ROT[(4*j+11) % 32]); + round2(C, D, KEY_MASK[4*j+12], KEY_ROT[(4*j+12) % 32]); + round3(B, C, KEY_MASK[4*j+13], KEY_ROT[(4*j+13) % 32]); + round1(A, B, KEY_MASK[4*j+14], KEY_ROT[(4*j+14) % 32]); + round2(H, A, KEY_MASK[4*j+15], KEY_ROT[(4*j+15) % 32]); + + RK[j ] = (A % 32); + RK[j+1] = (C % 32); + RK[j+2] = (E % 32); + RK[j+3] = (G % 32); + MK[j ] = H; + MK[j+1] = F; + MK[j+2] = D; + MK[j+3] = B; + } + } + +} diff --git a/botan/src/block/cast/cast256.h b/botan/src/block/cast/cast256.h new file mode 100644 index 0000000..cd48edd --- /dev/null +++ b/botan/src/block/cast/cast256.h @@ -0,0 +1,44 @@ +/* +* CAST-256 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_CAST256_H__ +#define BOTAN_CAST256_H__ + +#include <botan/block_cipher.h> + +namespace Botan { + +/* +* CAST-256 +*/ +class BOTAN_DLL CAST_256 : public BlockCipher + { + public: + void clear() throw() { MK.clear(); RK.clear(); } + std::string name() const { return "CAST-256"; } + BlockCipher* clone() const { return new CAST_256; } + CAST_256() : BlockCipher(16, 4, 32, 4) {} + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key_schedule(const byte[], u32bit); + + static const u32bit KEY_MASK[192]; + static const byte KEY_ROT[32]; + + SecureBuffer<u32bit, 48> MK; + SecureBuffer<byte, 48> RK; + }; + +extern const u32bit CAST_SBOX1[256]; +extern const u32bit CAST_SBOX2[256]; +extern const u32bit CAST_SBOX3[256]; +extern const u32bit CAST_SBOX4[256]; + +} + +#endif diff --git a/botan/src/block/cast/cast_tab.cpp b/botan/src/block/cast/cast_tab.cpp new file mode 100644 index 0000000..61c8437 --- /dev/null +++ b/botan/src/block/cast/cast_tab.cpp @@ -0,0 +1,412 @@ +/* +* S-Box Tables for CAST-128 and CAST-256 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/cast128.h> +#include <botan/cast256.h> + +namespace Botan { + +const u32bit CAST_SBOX1[256] = { + 0x30FB40D4, 0x9FA0FF0B, 0x6BECCD2F, 0x3F258C7A, 0x1E213F2F, 0x9C004DD3, + 0x6003E540, 0xCF9FC949, 0xBFD4AF27, 0x88BBBDB5, 0xE2034090, 0x98D09675, + 0x6E63A0E0, 0x15C361D2, 0xC2E7661D, 0x22D4FF8E, 0x28683B6F, 0xC07FD059, + 0xFF2379C8, 0x775F50E2, 0x43C340D3, 0xDF2F8656, 0x887CA41A, 0xA2D2BD2D, + 0xA1C9E0D6, 0x346C4819, 0x61B76D87, 0x22540F2F, 0x2ABE32E1, 0xAA54166B, + 0x22568E3A, 0xA2D341D0, 0x66DB40C8, 0xA784392F, 0x004DFF2F, 0x2DB9D2DE, + 0x97943FAC, 0x4A97C1D8, 0x527644B7, 0xB5F437A7, 0xB82CBAEF, 0xD751D159, + 0x6FF7F0ED, 0x5A097A1F, 0x827B68D0, 0x90ECF52E, 0x22B0C054, 0xBC8E5935, + 0x4B6D2F7F, 0x50BB64A2, 0xD2664910, 0xBEE5812D, 0xB7332290, 0xE93B159F, + 0xB48EE411, 0x4BFF345D, 0xFD45C240, 0xAD31973F, 0xC4F6D02E, 0x55FC8165, + 0xD5B1CAAD, 0xA1AC2DAE, 0xA2D4B76D, 0xC19B0C50, 0x882240F2, 0x0C6E4F38, + 0xA4E4BFD7, 0x4F5BA272, 0x564C1D2F, 0xC59C5319, 0xB949E354, 0xB04669FE, + 0xB1B6AB8A, 0xC71358DD, 0x6385C545, 0x110F935D, 0x57538AD5, 0x6A390493, + 0xE63D37E0, 0x2A54F6B3, 0x3A787D5F, 0x6276A0B5, 0x19A6FCDF, 0x7A42206A, + 0x29F9D4D5, 0xF61B1891, 0xBB72275E, 0xAA508167, 0x38901091, 0xC6B505EB, + 0x84C7CB8C, 0x2AD75A0F, 0x874A1427, 0xA2D1936B, 0x2AD286AF, 0xAA56D291, + 0xD7894360, 0x425C750D, 0x93B39E26, 0x187184C9, 0x6C00B32D, 0x73E2BB14, + 0xA0BEBC3C, 0x54623779, 0x64459EAB, 0x3F328B82, 0x7718CF82, 0x59A2CEA6, + 0x04EE002E, 0x89FE78E6, 0x3FAB0950, 0x325FF6C2, 0x81383F05, 0x6963C5C8, + 0x76CB5AD6, 0xD49974C9, 0xCA180DCF, 0x380782D5, 0xC7FA5CF6, 0x8AC31511, + 0x35E79E13, 0x47DA91D0, 0xF40F9086, 0xA7E2419E, 0x31366241, 0x051EF495, + 0xAA573B04, 0x4A805D8D, 0x548300D0, 0x00322A3C, 0xBF64CDDF, 0xBA57A68E, + 0x75C6372B, 0x50AFD341, 0xA7C13275, 0x915A0BF5, 0x6B54BFAB, 0x2B0B1426, + 0xAB4CC9D7, 0x449CCD82, 0xF7FBF265, 0xAB85C5F3, 0x1B55DB94, 0xAAD4E324, + 0xCFA4BD3F, 0x2DEAA3E2, 0x9E204D02, 0xC8BD25AC, 0xEADF55B3, 0xD5BD9E98, + 0xE31231B2, 0x2AD5AD6C, 0x954329DE, 0xADBE4528, 0xD8710F69, 0xAA51C90F, + 0xAA786BF6, 0x22513F1E, 0xAA51A79B, 0x2AD344CC, 0x7B5A41F0, 0xD37CFBAD, + 0x1B069505, 0x41ECE491, 0xB4C332E6, 0x032268D4, 0xC9600ACC, 0xCE387E6D, + 0xBF6BB16C, 0x6A70FB78, 0x0D03D9C9, 0xD4DF39DE, 0xE01063DA, 0x4736F464, + 0x5AD328D8, 0xB347CC96, 0x75BB0FC3, 0x98511BFB, 0x4FFBCC35, 0xB58BCF6A, + 0xE11F0ABC, 0xBFC5FE4A, 0xA70AEC10, 0xAC39570A, 0x3F04442F, 0x6188B153, + 0xE0397A2E, 0x5727CB79, 0x9CEB418F, 0x1CACD68D, 0x2AD37C96, 0x0175CB9D, + 0xC69DFF09, 0xC75B65F0, 0xD9DB40D8, 0xEC0E7779, 0x4744EAD4, 0xB11C3274, + 0xDD24CB9E, 0x7E1C54BD, 0xF01144F9, 0xD2240EB1, 0x9675B3FD, 0xA3AC3755, + 0xD47C27AF, 0x51C85F4D, 0x56907596, 0xA5BB15E6, 0x580304F0, 0xCA042CF1, + 0x011A37EA, 0x8DBFAADB, 0x35BA3E4A, 0x3526FFA0, 0xC37B4D09, 0xBC306ED9, + 0x98A52666, 0x5648F725, 0xFF5E569D, 0x0CED63D0, 0x7C63B2CF, 0x700B45E1, + 0xD5EA50F1, 0x85A92872, 0xAF1FBDA7, 0xD4234870, 0xA7870BF3, 0x2D3B4D79, + 0x42E04198, 0x0CD0EDE7, 0x26470DB8, 0xF881814C, 0x474D6AD7, 0x7C0C5E5C, + 0xD1231959, 0x381B7298, 0xF5D2F4DB, 0xAB838653, 0x6E2F1E23, 0x83719C9E, + 0xBD91E046, 0x9A56456E, 0xDC39200C, 0x20C8C571, 0x962BDA1C, 0xE1E696FF, + 0xB141AB08, 0x7CCA89B9, 0x1A69E783, 0x02CC4843, 0xA2F7C579, 0x429EF47D, + 0x427B169C, 0x5AC9F049, 0xDD8F0F00, 0x5C8165BF }; + +const u32bit CAST_SBOX2[256] = { + 0x1F201094, 0xEF0BA75B, 0x69E3CF7E, 0x393F4380, 0xFE61CF7A, 0xEEC5207A, + 0x55889C94, 0x72FC0651, 0xADA7EF79, 0x4E1D7235, 0xD55A63CE, 0xDE0436BA, + 0x99C430EF, 0x5F0C0794, 0x18DCDB7D, 0xA1D6EFF3, 0xA0B52F7B, 0x59E83605, + 0xEE15B094, 0xE9FFD909, 0xDC440086, 0xEF944459, 0xBA83CCB3, 0xE0C3CDFB, + 0xD1DA4181, 0x3B092AB1, 0xF997F1C1, 0xA5E6CF7B, 0x01420DDB, 0xE4E7EF5B, + 0x25A1FF41, 0xE180F806, 0x1FC41080, 0x179BEE7A, 0xD37AC6A9, 0xFE5830A4, + 0x98DE8B7F, 0x77E83F4E, 0x79929269, 0x24FA9F7B, 0xE113C85B, 0xACC40083, + 0xD7503525, 0xF7EA615F, 0x62143154, 0x0D554B63, 0x5D681121, 0xC866C359, + 0x3D63CF73, 0xCEE234C0, 0xD4D87E87, 0x5C672B21, 0x071F6181, 0x39F7627F, + 0x361E3084, 0xE4EB573B, 0x602F64A4, 0xD63ACD9C, 0x1BBC4635, 0x9E81032D, + 0x2701F50C, 0x99847AB4, 0xA0E3DF79, 0xBA6CF38C, 0x10843094, 0x2537A95E, + 0xF46F6FFE, 0xA1FF3B1F, 0x208CFB6A, 0x8F458C74, 0xD9E0A227, 0x4EC73A34, + 0xFC884F69, 0x3E4DE8DF, 0xEF0E0088, 0x3559648D, 0x8A45388C, 0x1D804366, + 0x721D9BFD, 0xA58684BB, 0xE8256333, 0x844E8212, 0x128D8098, 0xFED33FB4, + 0xCE280AE1, 0x27E19BA5, 0xD5A6C252, 0xE49754BD, 0xC5D655DD, 0xEB667064, + 0x77840B4D, 0xA1B6A801, 0x84DB26A9, 0xE0B56714, 0x21F043B7, 0xE5D05860, + 0x54F03084, 0x066FF472, 0xA31AA153, 0xDADC4755, 0xB5625DBF, 0x68561BE6, + 0x83CA6B94, 0x2D6ED23B, 0xECCF01DB, 0xA6D3D0BA, 0xB6803D5C, 0xAF77A709, + 0x33B4A34C, 0x397BC8D6, 0x5EE22B95, 0x5F0E5304, 0x81ED6F61, 0x20E74364, + 0xB45E1378, 0xDE18639B, 0x881CA122, 0xB96726D1, 0x8049A7E8, 0x22B7DA7B, + 0x5E552D25, 0x5272D237, 0x79D2951C, 0xC60D894C, 0x488CB402, 0x1BA4FE5B, + 0xA4B09F6B, 0x1CA815CF, 0xA20C3005, 0x8871DF63, 0xB9DE2FCB, 0x0CC6C9E9, + 0x0BEEFF53, 0xE3214517, 0xB4542835, 0x9F63293C, 0xEE41E729, 0x6E1D2D7C, + 0x50045286, 0x1E6685F3, 0xF33401C6, 0x30A22C95, 0x31A70850, 0x60930F13, + 0x73F98417, 0xA1269859, 0xEC645C44, 0x52C877A9, 0xCDFF33A6, 0xA02B1741, + 0x7CBAD9A2, 0x2180036F, 0x50D99C08, 0xCB3F4861, 0xC26BD765, 0x64A3F6AB, + 0x80342676, 0x25A75E7B, 0xE4E6D1FC, 0x20C710E6, 0xCDF0B680, 0x17844D3B, + 0x31EEF84D, 0x7E0824E4, 0x2CCB49EB, 0x846A3BAE, 0x8FF77888, 0xEE5D60F6, + 0x7AF75673, 0x2FDD5CDB, 0xA11631C1, 0x30F66F43, 0xB3FAEC54, 0x157FD7FA, + 0xEF8579CC, 0xD152DE58, 0xDB2FFD5E, 0x8F32CE19, 0x306AF97A, 0x02F03EF8, + 0x99319AD5, 0xC242FA0F, 0xA7E3EBB0, 0xC68E4906, 0xB8DA230C, 0x80823028, + 0xDCDEF3C8, 0xD35FB171, 0x088A1BC8, 0xBEC0C560, 0x61A3C9E8, 0xBCA8F54D, + 0xC72FEFFA, 0x22822E99, 0x82C570B4, 0xD8D94E89, 0x8B1C34BC, 0x301E16E6, + 0x273BE979, 0xB0FFEAA6, 0x61D9B8C6, 0x00B24869, 0xB7FFCE3F, 0x08DC283B, + 0x43DAF65A, 0xF7E19798, 0x7619B72F, 0x8F1C9BA4, 0xDC8637A0, 0x16A7D3B1, + 0x9FC393B7, 0xA7136EEB, 0xC6BCC63E, 0x1A513742, 0xEF6828BC, 0x520365D6, + 0x2D6A77AB, 0x3527ED4B, 0x821FD216, 0x095C6E2E, 0xDB92F2FB, 0x5EEA29CB, + 0x145892F5, 0x91584F7F, 0x5483697B, 0x2667A8CC, 0x85196048, 0x8C4BACEA, + 0x833860D4, 0x0D23E0F9, 0x6C387E8A, 0x0AE6D249, 0xB284600C, 0xD835731D, + 0xDCB1C647, 0xAC4C56EA, 0x3EBD81B3, 0x230EABB0, 0x6438BC87, 0xF0B5B1FA, + 0x8F5EA2B3, 0xFC184642, 0x0A036B7A, 0x4FB089BD, 0x649DA589, 0xA345415E, + 0x5C038323, 0x3E5D3BB9, 0x43D79572, 0x7E6DD07C, 0x06DFDF1E, 0x6C6CC4EF, + 0x7160A539, 0x73BFBE70, 0x83877605, 0x4523ECF1 }; + +const u32bit CAST_SBOX3[256] = { + 0x8DEFC240, 0x25FA5D9F, 0xEB903DBF, 0xE810C907, 0x47607FFF, 0x369FE44B, + 0x8C1FC644, 0xAECECA90, 0xBEB1F9BF, 0xEEFBCAEA, 0xE8CF1950, 0x51DF07AE, + 0x920E8806, 0xF0AD0548, 0xE13C8D83, 0x927010D5, 0x11107D9F, 0x07647DB9, + 0xB2E3E4D4, 0x3D4F285E, 0xB9AFA820, 0xFADE82E0, 0xA067268B, 0x8272792E, + 0x553FB2C0, 0x489AE22B, 0xD4EF9794, 0x125E3FBC, 0x21FFFCEE, 0x825B1BFD, + 0x9255C5ED, 0x1257A240, 0x4E1A8302, 0xBAE07FFF, 0x528246E7, 0x8E57140E, + 0x3373F7BF, 0x8C9F8188, 0xA6FC4EE8, 0xC982B5A5, 0xA8C01DB7, 0x579FC264, + 0x67094F31, 0xF2BD3F5F, 0x40FFF7C1, 0x1FB78DFC, 0x8E6BD2C1, 0x437BE59B, + 0x99B03DBF, 0xB5DBC64B, 0x638DC0E6, 0x55819D99, 0xA197C81C, 0x4A012D6E, + 0xC5884A28, 0xCCC36F71, 0xB843C213, 0x6C0743F1, 0x8309893C, 0x0FEDDD5F, + 0x2F7FE850, 0xD7C07F7E, 0x02507FBF, 0x5AFB9A04, 0xA747D2D0, 0x1651192E, + 0xAF70BF3E, 0x58C31380, 0x5F98302E, 0x727CC3C4, 0x0A0FB402, 0x0F7FEF82, + 0x8C96FDAD, 0x5D2C2AAE, 0x8EE99A49, 0x50DA88B8, 0x8427F4A0, 0x1EAC5790, + 0x796FB449, 0x8252DC15, 0xEFBD7D9B, 0xA672597D, 0xADA840D8, 0x45F54504, + 0xFA5D7403, 0xE83EC305, 0x4F91751A, 0x925669C2, 0x23EFE941, 0xA903F12E, + 0x60270DF2, 0x0276E4B6, 0x94FD6574, 0x927985B2, 0x8276DBCB, 0x02778176, + 0xF8AF918D, 0x4E48F79E, 0x8F616DDF, 0xE29D840E, 0x842F7D83, 0x340CE5C8, + 0x96BBB682, 0x93B4B148, 0xEF303CAB, 0x984FAF28, 0x779FAF9B, 0x92DC560D, + 0x224D1E20, 0x8437AA88, 0x7D29DC96, 0x2756D3DC, 0x8B907CEE, 0xB51FD240, + 0xE7C07CE3, 0xE566B4A1, 0xC3E9615E, 0x3CF8209D, 0x6094D1E3, 0xCD9CA341, + 0x5C76460E, 0x00EA983B, 0xD4D67881, 0xFD47572C, 0xF76CEDD9, 0xBDA8229C, + 0x127DADAA, 0x438A074E, 0x1F97C090, 0x081BDB8A, 0x93A07EBE, 0xB938CA15, + 0x97B03CFF, 0x3DC2C0F8, 0x8D1AB2EC, 0x64380E51, 0x68CC7BFB, 0xD90F2788, + 0x12490181, 0x5DE5FFD4, 0xDD7EF86A, 0x76A2E214, 0xB9A40368, 0x925D958F, + 0x4B39FFFA, 0xBA39AEE9, 0xA4FFD30B, 0xFAF7933B, 0x6D498623, 0x193CBCFA, + 0x27627545, 0x825CF47A, 0x61BD8BA0, 0xD11E42D1, 0xCEAD04F4, 0x127EA392, + 0x10428DB7, 0x8272A972, 0x9270C4A8, 0x127DE50B, 0x285BA1C8, 0x3C62F44F, + 0x35C0EAA5, 0xE805D231, 0x428929FB, 0xB4FCDF82, 0x4FB66A53, 0x0E7DC15B, + 0x1F081FAB, 0x108618AE, 0xFCFD086D, 0xF9FF2889, 0x694BCC11, 0x236A5CAE, + 0x12DECA4D, 0x2C3F8CC5, 0xD2D02DFE, 0xF8EF5896, 0xE4CF52DA, 0x95155B67, + 0x494A488C, 0xB9B6A80C, 0x5C8F82BC, 0x89D36B45, 0x3A609437, 0xEC00C9A9, + 0x44715253, 0x0A874B49, 0xD773BC40, 0x7C34671C, 0x02717EF6, 0x4FEB5536, + 0xA2D02FFF, 0xD2BF60C4, 0xD43F03C0, 0x50B4EF6D, 0x07478CD1, 0x006E1888, + 0xA2E53F55, 0xB9E6D4BC, 0xA2048016, 0x97573833, 0xD7207D67, 0xDE0F8F3D, + 0x72F87B33, 0xABCC4F33, 0x7688C55D, 0x7B00A6B0, 0x947B0001, 0x570075D2, + 0xF9BB88F8, 0x8942019E, 0x4264A5FF, 0x856302E0, 0x72DBD92B, 0xEE971B69, + 0x6EA22FDE, 0x5F08AE2B, 0xAF7A616D, 0xE5C98767, 0xCF1FEBD2, 0x61EFC8C2, + 0xF1AC2571, 0xCC8239C2, 0x67214CB8, 0xB1E583D1, 0xB7DC3E62, 0x7F10BDCE, + 0xF90A5C38, 0x0FF0443D, 0x606E6DC6, 0x60543A49, 0x5727C148, 0x2BE98A1D, + 0x8AB41738, 0x20E1BE24, 0xAF96DA0F, 0x68458425, 0x99833BE5, 0x600D457D, + 0x282F9350, 0x8334B362, 0xD91D1120, 0x2B6D8DA0, 0x642B1E31, 0x9C305A00, + 0x52BCE688, 0x1B03588A, 0xF7BAEFD5, 0x4142ED9C, 0xA4315C11, 0x83323EC5, + 0xDFEF4636, 0xA133C501, 0xE9D3531C, 0xEE353783 }; + +const u32bit CAST_SBOX4[256] = { + 0x9DB30420, 0x1FB6E9DE, 0xA7BE7BEF, 0xD273A298, 0x4A4F7BDB, 0x64AD8C57, + 0x85510443, 0xFA020ED1, 0x7E287AFF, 0xE60FB663, 0x095F35A1, 0x79EBF120, + 0xFD059D43, 0x6497B7B1, 0xF3641F63, 0x241E4ADF, 0x28147F5F, 0x4FA2B8CD, + 0xC9430040, 0x0CC32220, 0xFDD30B30, 0xC0A5374F, 0x1D2D00D9, 0x24147B15, + 0xEE4D111A, 0x0FCA5167, 0x71FF904C, 0x2D195FFE, 0x1A05645F, 0x0C13FEFE, + 0x081B08CA, 0x05170121, 0x80530100, 0xE83E5EFE, 0xAC9AF4F8, 0x7FE72701, + 0xD2B8EE5F, 0x06DF4261, 0xBB9E9B8A, 0x7293EA25, 0xCE84FFDF, 0xF5718801, + 0x3DD64B04, 0xA26F263B, 0x7ED48400, 0x547EEBE6, 0x446D4CA0, 0x6CF3D6F5, + 0x2649ABDF, 0xAEA0C7F5, 0x36338CC1, 0x503F7E93, 0xD3772061, 0x11B638E1, + 0x72500E03, 0xF80EB2BB, 0xABE0502E, 0xEC8D77DE, 0x57971E81, 0xE14F6746, + 0xC9335400, 0x6920318F, 0x081DBB99, 0xFFC304A5, 0x4D351805, 0x7F3D5CE3, + 0xA6C866C6, 0x5D5BCCA9, 0xDAEC6FEA, 0x9F926F91, 0x9F46222F, 0x3991467D, + 0xA5BF6D8E, 0x1143C44F, 0x43958302, 0xD0214EEB, 0x022083B8, 0x3FB6180C, + 0x18F8931E, 0x281658E6, 0x26486E3E, 0x8BD78A70, 0x7477E4C1, 0xB506E07C, + 0xF32D0A25, 0x79098B02, 0xE4EABB81, 0x28123B23, 0x69DEAD38, 0x1574CA16, + 0xDF871B62, 0x211C40B7, 0xA51A9EF9, 0x0014377B, 0x041E8AC8, 0x09114003, + 0xBD59E4D2, 0xE3D156D5, 0x4FE876D5, 0x2F91A340, 0x557BE8DE, 0x00EAE4A7, + 0x0CE5C2EC, 0x4DB4BBA6, 0xE756BDFF, 0xDD3369AC, 0xEC17B035, 0x06572327, + 0x99AFC8B0, 0x56C8C391, 0x6B65811C, 0x5E146119, 0x6E85CB75, 0xBE07C002, + 0xC2325577, 0x893FF4EC, 0x5BBFC92D, 0xD0EC3B25, 0xB7801AB7, 0x8D6D3B24, + 0x20C763EF, 0xC366A5FC, 0x9C382880, 0x0ACE3205, 0xAAC9548A, 0xECA1D7C7, + 0x041AFA32, 0x1D16625A, 0x6701902C, 0x9B757A54, 0x31D477F7, 0x9126B031, + 0x36CC6FDB, 0xC70B8B46, 0xD9E66A48, 0x56E55A79, 0x026A4CEB, 0x52437EFF, + 0x2F8F76B4, 0x0DF980A5, 0x8674CDE3, 0xEDDA04EB, 0x17A9BE04, 0x2C18F4DF, + 0xB7747F9D, 0xAB2AF7B4, 0xEFC34D20, 0x2E096B7C, 0x1741A254, 0xE5B6A035, + 0x213D42F6, 0x2C1C7C26, 0x61C2F50F, 0x6552DAF9, 0xD2C231F8, 0x25130F69, + 0xD8167FA2, 0x0418F2C8, 0x001A96A6, 0x0D1526AB, 0x63315C21, 0x5E0A72EC, + 0x49BAFEFD, 0x187908D9, 0x8D0DBD86, 0x311170A7, 0x3E9B640C, 0xCC3E10D7, + 0xD5CAD3B6, 0x0CAEC388, 0xF73001E1, 0x6C728AFF, 0x71EAE2A1, 0x1F9AF36E, + 0xCFCBD12F, 0xC1DE8417, 0xAC07BE6B, 0xCB44A1D8, 0x8B9B0F56, 0x013988C3, + 0xB1C52FCA, 0xB4BE31CD, 0xD8782806, 0x12A3A4E2, 0x6F7DE532, 0x58FD7EB6, + 0xD01EE900, 0x24ADFFC2, 0xF4990FC5, 0x9711AAC5, 0x001D7B95, 0x82E5E7D2, + 0x109873F6, 0x00613096, 0xC32D9521, 0xADA121FF, 0x29908415, 0x7FBB977F, + 0xAF9EB3DB, 0x29C9ED2A, 0x5CE2A465, 0xA730F32C, 0xD0AA3FE8, 0x8A5CC091, + 0xD49E2CE7, 0x0CE454A9, 0xD60ACD86, 0x015F1919, 0x77079103, 0xDEA03AF6, + 0x78A8565E, 0xDEE356DF, 0x21F05CBE, 0x8B75E387, 0xB3C50651, 0xB8A5C3EF, + 0xD8EEB6D2, 0xE523BE77, 0xC2154529, 0x2F69EFDF, 0xAFE67AFB, 0xF470C4B2, + 0xF3E0EB5B, 0xD6CC9876, 0x39E4460C, 0x1FDA8538, 0x1987832F, 0xCA007367, + 0xA99144F8, 0x296B299E, 0x492FC295, 0x9266BEAB, 0xB5676E69, 0x9BD3DDDA, + 0xDF7E052F, 0xDB25701C, 0x1B5E51EE, 0xF65324E6, 0x6AFCE36C, 0x0316CC04, + 0x8644213E, 0xB7DC59D0, 0x7965291F, 0xCCD6FD43, 0x41823979, 0x932BCDF6, + 0xB657C34D, 0x4EDFD282, 0x7AE5290C, 0x3CB9536B, 0x851E20FE, 0x9833557E, + 0x13ECF0B0, 0xD3FFB372, 0x3F85C5C1, 0x0AEF7ED2 }; + +const u32bit CAST_128::S5[256] = { + 0x7EC90C04, 0x2C6E74B9, 0x9B0E66DF, 0xA6337911, 0xB86A7FFF, 0x1DD358F5, + 0x44DD9D44, 0x1731167F, 0x08FBF1FA, 0xE7F511CC, 0xD2051B00, 0x735ABA00, + 0x2AB722D8, 0x386381CB, 0xACF6243A, 0x69BEFD7A, 0xE6A2E77F, 0xF0C720CD, + 0xC4494816, 0xCCF5C180, 0x38851640, 0x15B0A848, 0xE68B18CB, 0x4CAADEFF, + 0x5F480A01, 0x0412B2AA, 0x259814FC, 0x41D0EFE2, 0x4E40B48D, 0x248EB6FB, + 0x8DBA1CFE, 0x41A99B02, 0x1A550A04, 0xBA8F65CB, 0x7251F4E7, 0x95A51725, + 0xC106ECD7, 0x97A5980A, 0xC539B9AA, 0x4D79FE6A, 0xF2F3F763, 0x68AF8040, + 0xED0C9E56, 0x11B4958B, 0xE1EB5A88, 0x8709E6B0, 0xD7E07156, 0x4E29FEA7, + 0x6366E52D, 0x02D1C000, 0xC4AC8E05, 0x9377F571, 0x0C05372A, 0x578535F2, + 0x2261BE02, 0xD642A0C9, 0xDF13A280, 0x74B55BD2, 0x682199C0, 0xD421E5EC, + 0x53FB3CE8, 0xC8ADEDB3, 0x28A87FC9, 0x3D959981, 0x5C1FF900, 0xFE38D399, + 0x0C4EFF0B, 0x062407EA, 0xAA2F4FB1, 0x4FB96976, 0x90C79505, 0xB0A8A774, + 0xEF55A1FF, 0xE59CA2C2, 0xA6B62D27, 0xE66A4263, 0xDF65001F, 0x0EC50966, + 0xDFDD55BC, 0x29DE0655, 0x911E739A, 0x17AF8975, 0x32C7911C, 0x89F89468, + 0x0D01E980, 0x524755F4, 0x03B63CC9, 0x0CC844B2, 0xBCF3F0AA, 0x87AC36E9, + 0xE53A7426, 0x01B3D82B, 0x1A9E7449, 0x64EE2D7E, 0xCDDBB1DA, 0x01C94910, + 0xB868BF80, 0x0D26F3FD, 0x9342EDE7, 0x04A5C284, 0x636737B6, 0x50F5B616, + 0xF24766E3, 0x8ECA36C1, 0x136E05DB, 0xFEF18391, 0xFB887A37, 0xD6E7F7D4, + 0xC7FB7DC9, 0x3063FCDF, 0xB6F589DE, 0xEC2941DA, 0x26E46695, 0xB7566419, + 0xF654EFC5, 0xD08D58B7, 0x48925401, 0xC1BACB7F, 0xE5FF550F, 0xB6083049, + 0x5BB5D0E8, 0x87D72E5A, 0xAB6A6EE1, 0x223A66CE, 0xC62BF3CD, 0x9E0885F9, + 0x68CB3E47, 0x086C010F, 0xA21DE820, 0xD18B69DE, 0xF3F65777, 0xFA02C3F6, + 0x407EDAC3, 0xCBB3D550, 0x1793084D, 0xB0D70EBA, 0x0AB378D5, 0xD951FB0C, + 0xDED7DA56, 0x4124BBE4, 0x94CA0B56, 0x0F5755D1, 0xE0E1E56E, 0x6184B5BE, + 0x580A249F, 0x94F74BC0, 0xE327888E, 0x9F7B5561, 0xC3DC0280, 0x05687715, + 0x646C6BD7, 0x44904DB3, 0x66B4F0A3, 0xC0F1648A, 0x697ED5AF, 0x49E92FF6, + 0x309E374F, 0x2CB6356A, 0x85808573, 0x4991F840, 0x76F0AE02, 0x083BE84D, + 0x28421C9A, 0x44489406, 0x736E4CB8, 0xC1092910, 0x8BC95FC6, 0x7D869CF4, + 0x134F616F, 0x2E77118D, 0xB31B2BE1, 0xAA90B472, 0x3CA5D717, 0x7D161BBA, + 0x9CAD9010, 0xAF462BA2, 0x9FE459D2, 0x45D34559, 0xD9F2DA13, 0xDBC65487, + 0xF3E4F94E, 0x176D486F, 0x097C13EA, 0x631DA5C7, 0x445F7382, 0x175683F4, + 0xCDC66A97, 0x70BE0288, 0xB3CDCF72, 0x6E5DD2F3, 0x20936079, 0x459B80A5, + 0xBE60E2DB, 0xA9C23101, 0xEBA5315C, 0x224E42F2, 0x1C5C1572, 0xF6721B2C, + 0x1AD2FFF3, 0x8C25404E, 0x324ED72F, 0x4067B7FD, 0x0523138E, 0x5CA3BC78, + 0xDC0FD66E, 0x75922283, 0x784D6B17, 0x58EBB16E, 0x44094F85, 0x3F481D87, + 0xFCFEAE7B, 0x77B5FF76, 0x8C2302BF, 0xAAF47556, 0x5F46B02A, 0x2B092801, + 0x3D38F5F7, 0x0CA81F36, 0x52AF4A8A, 0x66D5E7C0, 0xDF3B0874, 0x95055110, + 0x1B5AD7A8, 0xF61ED5AD, 0x6CF6E479, 0x20758184, 0xD0CEFA65, 0x88F7BE58, + 0x4A046826, 0x0FF6F8F3, 0xA09C7F70, 0x5346ABA0, 0x5CE96C28, 0xE176EDA3, + 0x6BAC307F, 0x376829D2, 0x85360FA9, 0x17E3FE2A, 0x24B79767, 0xF5A96B20, + 0xD6CD2595, 0x68FF1EBF, 0x7555442C, 0xF19F06BE, 0xF9E0659A, 0xEEB9491D, + 0x34010718, 0xBB30CAB8, 0xE822FE15, 0x88570983, 0x750E6249, 0xDA627E55, + 0x5E76FFA8, 0xB1534546, 0x6D47DE08, 0xEFE9E7D4 }; + +const u32bit CAST_128::S6[256] = { + 0xF6FA8F9D, 0x2CAC6CE1, 0x4CA34867, 0xE2337F7C, 0x95DB08E7, 0x016843B4, + 0xECED5CBC, 0x325553AC, 0xBF9F0960, 0xDFA1E2ED, 0x83F0579D, 0x63ED86B9, + 0x1AB6A6B8, 0xDE5EBE39, 0xF38FF732, 0x8989B138, 0x33F14961, 0xC01937BD, + 0xF506C6DA, 0xE4625E7E, 0xA308EA99, 0x4E23E33C, 0x79CBD7CC, 0x48A14367, + 0xA3149619, 0xFEC94BD5, 0xA114174A, 0xEAA01866, 0xA084DB2D, 0x09A8486F, + 0xA888614A, 0x2900AF98, 0x01665991, 0xE1992863, 0xC8F30C60, 0x2E78EF3C, + 0xD0D51932, 0xCF0FEC14, 0xF7CA07D2, 0xD0A82072, 0xFD41197E, 0x9305A6B0, + 0xE86BE3DA, 0x74BED3CD, 0x372DA53C, 0x4C7F4448, 0xDAB5D440, 0x6DBA0EC3, + 0x083919A7, 0x9FBAEED9, 0x49DBCFB0, 0x4E670C53, 0x5C3D9C01, 0x64BDB941, + 0x2C0E636A, 0xBA7DD9CD, 0xEA6F7388, 0xE70BC762, 0x35F29ADB, 0x5C4CDD8D, + 0xF0D48D8C, 0xB88153E2, 0x08A19866, 0x1AE2EAC8, 0x284CAF89, 0xAA928223, + 0x9334BE53, 0x3B3A21BF, 0x16434BE3, 0x9AEA3906, 0xEFE8C36E, 0xF890CDD9, + 0x80226DAE, 0xC340A4A3, 0xDF7E9C09, 0xA694A807, 0x5B7C5ECC, 0x221DB3A6, + 0x9A69A02F, 0x68818A54, 0xCEB2296F, 0x53C0843A, 0xFE893655, 0x25BFE68A, + 0xB4628ABC, 0xCF222EBF, 0x25AC6F48, 0xA9A99387, 0x53BDDB65, 0xE76FFBE7, + 0xE967FD78, 0x0BA93563, 0x8E342BC1, 0xE8A11BE9, 0x4980740D, 0xC8087DFC, + 0x8DE4BF99, 0xA11101A0, 0x7FD37975, 0xDA5A26C0, 0xE81F994F, 0x9528CD89, + 0xFD339FED, 0xB87834BF, 0x5F04456D, 0x22258698, 0xC9C4C83B, 0x2DC156BE, + 0x4F628DAA, 0x57F55EC5, 0xE2220ABE, 0xD2916EBF, 0x4EC75B95, 0x24F2C3C0, + 0x42D15D99, 0xCD0D7FA0, 0x7B6E27FF, 0xA8DC8AF0, 0x7345C106, 0xF41E232F, + 0x35162386, 0xE6EA8926, 0x3333B094, 0x157EC6F2, 0x372B74AF, 0x692573E4, + 0xE9A9D848, 0xF3160289, 0x3A62EF1D, 0xA787E238, 0xF3A5F676, 0x74364853, + 0x20951063, 0x4576698D, 0xB6FAD407, 0x592AF950, 0x36F73523, 0x4CFB6E87, + 0x7DA4CEC0, 0x6C152DAA, 0xCB0396A8, 0xC50DFE5D, 0xFCD707AB, 0x0921C42F, + 0x89DFF0BB, 0x5FE2BE78, 0x448F4F33, 0x754613C9, 0x2B05D08D, 0x48B9D585, + 0xDC049441, 0xC8098F9B, 0x7DEDE786, 0xC39A3373, 0x42410005, 0x6A091751, + 0x0EF3C8A6, 0x890072D6, 0x28207682, 0xA9A9F7BE, 0xBF32679D, 0xD45B5B75, + 0xB353FD00, 0xCBB0E358, 0x830F220A, 0x1F8FB214, 0xD372CF08, 0xCC3C4A13, + 0x8CF63166, 0x061C87BE, 0x88C98F88, 0x6062E397, 0x47CF8E7A, 0xB6C85283, + 0x3CC2ACFB, 0x3FC06976, 0x4E8F0252, 0x64D8314D, 0xDA3870E3, 0x1E665459, + 0xC10908F0, 0x513021A5, 0x6C5B68B7, 0x822F8AA0, 0x3007CD3E, 0x74719EEF, + 0xDC872681, 0x073340D4, 0x7E432FD9, 0x0C5EC241, 0x8809286C, 0xF592D891, + 0x08A930F6, 0x957EF305, 0xB7FBFFBD, 0xC266E96F, 0x6FE4AC98, 0xB173ECC0, + 0xBC60B42A, 0x953498DA, 0xFBA1AE12, 0x2D4BD736, 0x0F25FAAB, 0xA4F3FCEB, + 0xE2969123, 0x257F0C3D, 0x9348AF49, 0x361400BC, 0xE8816F4A, 0x3814F200, + 0xA3F94043, 0x9C7A54C2, 0xBC704F57, 0xDA41E7F9, 0xC25AD33A, 0x54F4A084, + 0xB17F5505, 0x59357CBE, 0xEDBD15C8, 0x7F97C5AB, 0xBA5AC7B5, 0xB6F6DEAF, + 0x3A479C3A, 0x5302DA25, 0x653D7E6A, 0x54268D49, 0x51A477EA, 0x5017D55B, + 0xD7D25D88, 0x44136C76, 0x0404A8C8, 0xB8E5A121, 0xB81A928A, 0x60ED5869, + 0x97C55B96, 0xEAEC991B, 0x29935913, 0x01FDB7F1, 0x088E8DFA, 0x9AB6F6F5, + 0x3B4CBF9F, 0x4A5DE3AB, 0xE6051D35, 0xA0E1D855, 0xD36B4CF1, 0xF544EDEB, + 0xB0E93524, 0xBEBB8FBD, 0xA2D762CF, 0x49C92F54, 0x38B5F331, 0x7128A454, + 0x48392905, 0xA65B1DB8, 0x851C97BD, 0xD675CF2F }; + +const u32bit CAST_128::S7[256] = { + 0x85E04019, 0x332BF567, 0x662DBFFF, 0xCFC65693, 0x2A8D7F6F, 0xAB9BC912, + 0xDE6008A1, 0x2028DA1F, 0x0227BCE7, 0x4D642916, 0x18FAC300, 0x50F18B82, + 0x2CB2CB11, 0xB232E75C, 0x4B3695F2, 0xB28707DE, 0xA05FBCF6, 0xCD4181E9, + 0xE150210C, 0xE24EF1BD, 0xB168C381, 0xFDE4E789, 0x5C79B0D8, 0x1E8BFD43, + 0x4D495001, 0x38BE4341, 0x913CEE1D, 0x92A79C3F, 0x089766BE, 0xBAEEADF4, + 0x1286BECF, 0xB6EACB19, 0x2660C200, 0x7565BDE4, 0x64241F7A, 0x8248DCA9, + 0xC3B3AD66, 0x28136086, 0x0BD8DFA8, 0x356D1CF2, 0x107789BE, 0xB3B2E9CE, + 0x0502AA8F, 0x0BC0351E, 0x166BF52A, 0xEB12FF82, 0xE3486911, 0xD34D7516, + 0x4E7B3AFF, 0x5F43671B, 0x9CF6E037, 0x4981AC83, 0x334266CE, 0x8C9341B7, + 0xD0D854C0, 0xCB3A6C88, 0x47BC2829, 0x4725BA37, 0xA66AD22B, 0x7AD61F1E, + 0x0C5CBAFA, 0x4437F107, 0xB6E79962, 0x42D2D816, 0x0A961288, 0xE1A5C06E, + 0x13749E67, 0x72FC081A, 0xB1D139F7, 0xF9583745, 0xCF19DF58, 0xBEC3F756, + 0xC06EBA30, 0x07211B24, 0x45C28829, 0xC95E317F, 0xBC8EC511, 0x38BC46E9, + 0xC6E6FA14, 0xBAE8584A, 0xAD4EBC46, 0x468F508B, 0x7829435F, 0xF124183B, + 0x821DBA9F, 0xAFF60FF4, 0xEA2C4E6D, 0x16E39264, 0x92544A8B, 0x009B4FC3, + 0xABA68CED, 0x9AC96F78, 0x06A5B79A, 0xB2856E6E, 0x1AEC3CA9, 0xBE838688, + 0x0E0804E9, 0x55F1BE56, 0xE7E5363B, 0xB3A1F25D, 0xF7DEBB85, 0x61FE033C, + 0x16746233, 0x3C034C28, 0xDA6D0C74, 0x79AAC56C, 0x3CE4E1AD, 0x51F0C802, + 0x98F8F35A, 0x1626A49F, 0xEED82B29, 0x1D382FE3, 0x0C4FB99A, 0xBB325778, + 0x3EC6D97B, 0x6E77A6A9, 0xCB658B5C, 0xD45230C7, 0x2BD1408B, 0x60C03EB7, + 0xB9068D78, 0xA33754F4, 0xF430C87D, 0xC8A71302, 0xB96D8C32, 0xEBD4E7BE, + 0xBE8B9D2D, 0x7979FB06, 0xE7225308, 0x8B75CF77, 0x11EF8DA4, 0xE083C858, + 0x8D6B786F, 0x5A6317A6, 0xFA5CF7A0, 0x5DDA0033, 0xF28EBFB0, 0xF5B9C310, + 0xA0EAC280, 0x08B9767A, 0xA3D9D2B0, 0x79D34217, 0x021A718D, 0x9AC6336A, + 0x2711FD60, 0x438050E3, 0x069908A8, 0x3D7FEDC4, 0x826D2BEF, 0x4EEB8476, + 0x488DCF25, 0x36C9D566, 0x28E74E41, 0xC2610ACA, 0x3D49A9CF, 0xBAE3B9DF, + 0xB65F8DE6, 0x92AEAF64, 0x3AC7D5E6, 0x9EA80509, 0xF22B017D, 0xA4173F70, + 0xDD1E16C3, 0x15E0D7F9, 0x50B1B887, 0x2B9F4FD5, 0x625ABA82, 0x6A017962, + 0x2EC01B9C, 0x15488AA9, 0xD716E740, 0x40055A2C, 0x93D29A22, 0xE32DBF9A, + 0x058745B9, 0x3453DC1E, 0xD699296E, 0x496CFF6F, 0x1C9F4986, 0xDFE2ED07, + 0xB87242D1, 0x19DE7EAE, 0x053E561A, 0x15AD6F8C, 0x66626C1C, 0x7154C24C, + 0xEA082B2A, 0x93EB2939, 0x17DCB0F0, 0x58D4F2AE, 0x9EA294FB, 0x52CF564C, + 0x9883FE66, 0x2EC40581, 0x763953C3, 0x01D6692E, 0xD3A0C108, 0xA1E7160E, + 0xE4F2DFA6, 0x693ED285, 0x74904698, 0x4C2B0EDD, 0x4F757656, 0x5D393378, + 0xA132234F, 0x3D321C5D, 0xC3F5E194, 0x4B269301, 0xC79F022F, 0x3C997E7E, + 0x5E4F9504, 0x3FFAFBBD, 0x76F7AD0E, 0x296693F4, 0x3D1FCE6F, 0xC61E45BE, + 0xD3B5AB34, 0xF72BF9B7, 0x1B0434C0, 0x4E72B567, 0x5592A33D, 0xB5229301, + 0xCFD2A87F, 0x60AEB767, 0x1814386B, 0x30BCC33D, 0x38A0C07D, 0xFD1606F2, + 0xC363519B, 0x589DD390, 0x5479F8E6, 0x1CB8D647, 0x97FD61A9, 0xEA7759F4, + 0x2D57539D, 0x569A58CF, 0xE84E63AD, 0x462E1B78, 0x6580F87E, 0xF3817914, + 0x91DA55F4, 0x40A230F3, 0xD1988F35, 0xB6E318D2, 0x3FFA50BC, 0x3D40F021, + 0xC3C0BDAE, 0x4958C24C, 0x518F36B2, 0x84B1D370, 0x0FEDCE83, 0x878DDADA, + 0xF2A279C7, 0x94E01BE8, 0x90716F4B, 0x954B8AA3 }; + +const u32bit CAST_128::S8[256] = { + 0xE216300D, 0xBBDDFFFC, 0xA7EBDABD, 0x35648095, 0x7789F8B7, 0xE6C1121B, + 0x0E241600, 0x052CE8B5, 0x11A9CFB0, 0xE5952F11, 0xECE7990A, 0x9386D174, + 0x2A42931C, 0x76E38111, 0xB12DEF3A, 0x37DDDDFC, 0xDE9ADEB1, 0x0A0CC32C, + 0xBE197029, 0x84A00940, 0xBB243A0F, 0xB4D137CF, 0xB44E79F0, 0x049EEDFD, + 0x0B15A15D, 0x480D3168, 0x8BBBDE5A, 0x669DED42, 0xC7ECE831, 0x3F8F95E7, + 0x72DF191B, 0x7580330D, 0x94074251, 0x5C7DCDFA, 0xABBE6D63, 0xAA402164, + 0xB301D40A, 0x02E7D1CA, 0x53571DAE, 0x7A3182A2, 0x12A8DDEC, 0xFDAA335D, + 0x176F43E8, 0x71FB46D4, 0x38129022, 0xCE949AD4, 0xB84769AD, 0x965BD862, + 0x82F3D055, 0x66FB9767, 0x15B80B4E, 0x1D5B47A0, 0x4CFDE06F, 0xC28EC4B8, + 0x57E8726E, 0x647A78FC, 0x99865D44, 0x608BD593, 0x6C200E03, 0x39DC5FF6, + 0x5D0B00A3, 0xAE63AFF2, 0x7E8BD632, 0x70108C0C, 0xBBD35049, 0x2998DF04, + 0x980CF42A, 0x9B6DF491, 0x9E7EDD53, 0x06918548, 0x58CB7E07, 0x3B74EF2E, + 0x522FFFB1, 0xD24708CC, 0x1C7E27CD, 0xA4EB215B, 0x3CF1D2E2, 0x19B47A38, + 0x424F7618, 0x35856039, 0x9D17DEE7, 0x27EB35E6, 0xC9AFF67B, 0x36BAF5B8, + 0x09C467CD, 0xC18910B1, 0xE11DBF7B, 0x06CD1AF8, 0x7170C608, 0x2D5E3354, + 0xD4DE495A, 0x64C6D006, 0xBCC0C62C, 0x3DD00DB3, 0x708F8F34, 0x77D51B42, + 0x264F620F, 0x24B8D2BF, 0x15C1B79E, 0x46A52564, 0xF8D7E54E, 0x3E378160, + 0x7895CDA5, 0x859C15A5, 0xE6459788, 0xC37BC75F, 0xDB07BA0C, 0x0676A3AB, + 0x7F229B1E, 0x31842E7B, 0x24259FD7, 0xF8BEF472, 0x835FFCB8, 0x6DF4C1F2, + 0x96F5B195, 0xFD0AF0FC, 0xB0FE134C, 0xE2506D3D, 0x4F9B12EA, 0xF215F225, + 0xA223736F, 0x9FB4C428, 0x25D04979, 0x34C713F8, 0xC4618187, 0xEA7A6E98, + 0x7CD16EFC, 0x1436876C, 0xF1544107, 0xBEDEEE14, 0x56E9AF27, 0xA04AA441, + 0x3CF7C899, 0x92ECBAE6, 0xDD67016D, 0x151682EB, 0xA842EEDF, 0xFDBA60B4, + 0xF1907B75, 0x20E3030F, 0x24D8C29E, 0xE139673B, 0xEFA63FB8, 0x71873054, + 0xB6F2CF3B, 0x9F326442, 0xCB15A4CC, 0xB01A4504, 0xF1E47D8D, 0x844A1BE5, + 0xBAE7DFDC, 0x42CBDA70, 0xCD7DAE0A, 0x57E85B7A, 0xD53F5AF6, 0x20CF4D8C, + 0xCEA4D428, 0x79D130A4, 0x3486EBFB, 0x33D3CDDC, 0x77853B53, 0x37EFFCB5, + 0xC5068778, 0xE580B3E6, 0x4E68B8F4, 0xC5C8B37E, 0x0D809EA2, 0x398FEB7C, + 0x132A4F94, 0x43B7950E, 0x2FEE7D1C, 0x223613BD, 0xDD06CAA2, 0x37DF932B, + 0xC4248289, 0xACF3EBC3, 0x5715F6B7, 0xEF3478DD, 0xF267616F, 0xC148CBE4, + 0x9052815E, 0x5E410FAB, 0xB48A2465, 0x2EDA7FA4, 0xE87B40E4, 0xE98EA084, + 0x5889E9E1, 0xEFD390FC, 0xDD07D35B, 0xDB485694, 0x38D7E5B2, 0x57720101, + 0x730EDEBC, 0x5B643113, 0x94917E4F, 0x503C2FBA, 0x646F1282, 0x7523D24A, + 0xE0779695, 0xF9C17A8F, 0x7A5B2121, 0xD187B896, 0x29263A4D, 0xBA510CDF, + 0x81F47C9F, 0xAD1163ED, 0xEA7B5965, 0x1A00726E, 0x11403092, 0x00DA6D77, + 0x4A0CDD61, 0xAD1F4603, 0x605BDFB0, 0x9EEDC364, 0x22EBE6A8, 0xCEE7D28A, + 0xA0E736A0, 0x5564A6B9, 0x10853209, 0xC7EB8F37, 0x2DE705CA, 0x8951570F, + 0xDF09822B, 0xBD691A6C, 0xAA12E4F2, 0x87451C0F, 0xE0F6A27A, 0x3ADA4819, + 0x4CF1764F, 0x0D771C2B, 0x67CDB156, 0x350D8384, 0x5938FA0F, 0x42399EF3, + 0x36997B07, 0x0E84093D, 0x4AA93E61, 0x8360D87B, 0x1FA98B0C, 0x1149382C, + 0xE97625A5, 0x0614D1B7, 0x0E25244B, 0x0C768347, 0x589E8D82, 0x0D2059D1, + 0xA466BB1E, 0xF8DA0A82, 0x04F19130, 0xBA6E4EC0, 0x99265164, 0x1EE7230D, + 0x50B2AD80, 0xEAEE6801, 0x8DB2A283, 0xEA8BF59E }; + +const u32bit CAST_256::KEY_MASK[192] = { + 0x5A827999, 0xC95C653A, 0x383650DB, 0xA7103C7C, 0x15EA281D, 0x84C413BE, + 0xF39DFF5F, 0x6277EB00, 0xD151D6A1, 0x402BC242, 0xAF05ADE3, 0x1DDF9984, + 0x8CB98525, 0xFB9370C6, 0x6A6D5C67, 0xD9474808, 0x482133A9, 0xB6FB1F4A, + 0x25D50AEB, 0x94AEF68C, 0x0388E22D, 0x7262CDCE, 0xE13CB96F, 0x5016A510, + 0xBEF090B1, 0x2DCA7C52, 0x9CA467F3, 0x0B7E5394, 0x7A583F35, 0xE9322AD6, + 0x580C1677, 0xC6E60218, 0x35BFEDB9, 0xA499D95A, 0x1373C4FB, 0x824DB09C, + 0xF1279C3D, 0x600187DE, 0xCEDB737F, 0x3DB55F20, 0xAC8F4AC1, 0x1B693662, + 0x8A432203, 0xF91D0DA4, 0x67F6F945, 0xD6D0E4E6, 0x45AAD087, 0xB484BC28, + 0x235EA7C9, 0x9238936A, 0x01127F0B, 0x6FEC6AAC, 0xDEC6564D, 0x4DA041EE, + 0xBC7A2D8F, 0x2B541930, 0x9A2E04D1, 0x0907F072, 0x77E1DC13, 0xE6BBC7B4, + 0x5595B355, 0xC46F9EF6, 0x33498A97, 0xA2237638, 0x10FD61D9, 0x7FD74D7A, + 0xEEB1391B, 0x5D8B24BC, 0xCC65105D, 0x3B3EFBFE, 0xAA18E79F, 0x18F2D340, + 0x87CCBEE1, 0xF6A6AA82, 0x65809623, 0xD45A81C4, 0x43346D65, 0xB20E5906, + 0x20E844A7, 0x8FC23048, 0xFE9C1BE9, 0x6D76078A, 0xDC4FF32B, 0x4B29DECC, + 0xBA03CA6D, 0x28DDB60E, 0x97B7A1AF, 0x06918D50, 0x756B78F1, 0xE4456492, + 0x531F5033, 0xC1F93BD4, 0x30D32775, 0x9FAD1316, 0x0E86FEB7, 0x7D60EA58, + 0xEC3AD5F9, 0x5B14C19A, 0xC9EEAD3B, 0x38C898DC, 0xA7A2847D, 0x167C701E, + 0x85565BBF, 0xF4304760, 0x630A3301, 0xD1E41EA2, 0x40BE0A43, 0xAF97F5E4, + 0x1E71E185, 0x8D4BCD26, 0xFC25B8C7, 0x6AFFA468, 0xD9D99009, 0x48B37BAA, + 0xB78D674B, 0x266752EC, 0x95413E8D, 0x041B2A2E, 0x72F515CF, 0xE1CF0170, + 0x50A8ED11, 0xBF82D8B2, 0x2E5CC453, 0x9D36AFF4, 0x0C109B95, 0x7AEA8736, + 0xE9C472D7, 0x589E5E78, 0xC7784A19, 0x365235BA, 0xA52C215B, 0x14060CFC, + 0x82DFF89D, 0xF1B9E43E, 0x6093CFDF, 0xCF6DBB80, 0x3E47A721, 0xAD2192C2, + 0x1BFB7E63, 0x8AD56A04, 0xF9AF55A5, 0x68894146, 0xD7632CE7, 0x463D1888, + 0xB5170429, 0x23F0EFCA, 0x92CADB6B, 0x01A4C70C, 0x707EB2AD, 0xDF589E4E, + 0x4E3289EF, 0xBD0C7590, 0x2BE66131, 0x9AC04CD2, 0x099A3873, 0x78742414, + 0xE74E0FB5, 0x5627FB56, 0xC501E6F7, 0x33DBD298, 0xA2B5BE39, 0x118FA9DA, + 0x8069957B, 0xEF43811C, 0x5E1D6CBD, 0xCCF7585E, 0x3BD143FF, 0xAAAB2FA0, + 0x19851B41, 0x885F06E2, 0xF738F283, 0x6612DE24, 0xD4ECC9C5, 0x43C6B566, + 0xB2A0A107, 0x217A8CA8, 0x90547849, 0xFF2E63EA, 0x6E084F8B, 0xDCE23B2C, + 0x4BBC26CD, 0xBA96126E, 0x296FFE0F, 0x9849E9B0, 0x0723D551, 0x75FDC0F2, + 0xE4D7AC93, 0x53B19834, 0xC28B83D5, 0x31656F76, 0xA03F5B17, 0x0F1946B8 }; + +const byte CAST_256::KEY_ROT[32] = { + 0x13, 0x04, 0x15, 0x06, 0x17, 0x08, 0x19, 0x0A, 0x1B, 0x0C, 0x1D, 0x0E, + 0x1F, 0x10, 0x01, 0x12, 0x03, 0x14, 0x05, 0x16, 0x07, 0x18, 0x09, 0x1A, + 0x0B, 0x1C, 0x0D, 0x1E, 0x0F, 0x00, 0x11, 0x02 }; + +} diff --git a/botan/src/block/cast/info.txt b/botan/src/block/cast/info.txt new file mode 100644 index 0000000..73d3f90 --- /dev/null +++ b/botan/src/block/cast/info.txt @@ -0,0 +1,13 @@ +realname "CAST" + +define CAST + +load_on auto + +<add> +cast128.cpp +cast128.h +cast256.cpp +cast256.h +cast_tab.cpp +</add> diff --git a/botan/src/block/des/des.cpp b/botan/src/block/des/des.cpp new file mode 100644 index 0000000..37520e0 --- /dev/null +++ b/botan/src/block/des/des.cpp @@ -0,0 +1,265 @@ +/* +* DES +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/des.h> +#include <botan/loadstor.h> + +namespace Botan { + +namespace { + +/* +* DES Key Schedule +*/ +void des_key_schedule(u32bit round_key[32], const byte key[8]) + { + static const byte ROT[16] = { 1, 1, 2, 2, 2, 2, 2, 2, + 1, 2, 2, 2, 2, 2, 2, 1 }; + + u32bit C = ((key[7] & 0x80) << 20) | ((key[6] & 0x80) << 19) | + ((key[5] & 0x80) << 18) | ((key[4] & 0x80) << 17) | + ((key[3] & 0x80) << 16) | ((key[2] & 0x80) << 15) | + ((key[1] & 0x80) << 14) | ((key[0] & 0x80) << 13) | + ((key[7] & 0x40) << 13) | ((key[6] & 0x40) << 12) | + ((key[5] & 0x40) << 11) | ((key[4] & 0x40) << 10) | + ((key[3] & 0x40) << 9) | ((key[2] & 0x40) << 8) | + ((key[1] & 0x40) << 7) | ((key[0] & 0x40) << 6) | + ((key[7] & 0x20) << 6) | ((key[6] & 0x20) << 5) | + ((key[5] & 0x20) << 4) | ((key[4] & 0x20) << 3) | + ((key[3] & 0x20) << 2) | ((key[2] & 0x20) << 1) | + ((key[1] & 0x20) ) | ((key[0] & 0x20) >> 1) | + ((key[7] & 0x10) >> 1) | ((key[6] & 0x10) >> 2) | + ((key[5] & 0x10) >> 3) | ((key[4] & 0x10) >> 4); + u32bit D = ((key[7] & 0x02) << 26) | ((key[6] & 0x02) << 25) | + ((key[5] & 0x02) << 24) | ((key[4] & 0x02) << 23) | + ((key[3] & 0x02) << 22) | ((key[2] & 0x02) << 21) | + ((key[1] & 0x02) << 20) | ((key[0] & 0x02) << 19) | + ((key[7] & 0x04) << 17) | ((key[6] & 0x04) << 16) | + ((key[5] & 0x04) << 15) | ((key[4] & 0x04) << 14) | + ((key[3] & 0x04) << 13) | ((key[2] & 0x04) << 12) | + ((key[1] & 0x04) << 11) | ((key[0] & 0x04) << 10) | + ((key[7] & 0x08) << 8) | ((key[6] & 0x08) << 7) | + ((key[5] & 0x08) << 6) | ((key[4] & 0x08) << 5) | + ((key[3] & 0x08) << 4) | ((key[2] & 0x08) << 3) | + ((key[1] & 0x08) << 2) | ((key[0] & 0x08) << 1) | + ((key[3] & 0x10) >> 1) | ((key[2] & 0x10) >> 2) | + ((key[1] & 0x10) >> 3) | ((key[0] & 0x10) >> 4); + + for(u32bit j = 0; j != 16; ++j) + { + C = ((C << ROT[j]) | (C >> (28-ROT[j]))) & 0x0FFFFFFF; + D = ((D << ROT[j]) | (D >> (28-ROT[j]))) & 0x0FFFFFFF; + round_key[2*j ] = ((C & 0x00000010) << 22) | ((C & 0x00000800) << 17) | + ((C & 0x00000020) << 16) | ((C & 0x00004004) << 15) | + ((C & 0x00000200) << 11) | ((C & 0x00020000) << 10) | + ((C & 0x01000000) >> 6) | ((C & 0x00100000) >> 4) | + ((C & 0x00010000) << 3) | ((C & 0x08000000) >> 2) | + ((C & 0x00800000) << 1) | ((D & 0x00000010) << 8) | + ((D & 0x00000002) << 7) | ((D & 0x00000001) << 2) | + ((D & 0x00000200) ) | ((D & 0x00008000) >> 2) | + ((D & 0x00000088) >> 3) | ((D & 0x00001000) >> 7) | + ((D & 0x00080000) >> 9) | ((D & 0x02020000) >> 14) | + ((D & 0x00400000) >> 21); + round_key[2*j+1] = ((C & 0x00000001) << 28) | ((C & 0x00000082) << 18) | + ((C & 0x00002000) << 14) | ((C & 0x00000100) << 10) | + ((C & 0x00001000) << 9) | ((C & 0x00040000) << 6) | + ((C & 0x02400000) << 4) | ((C & 0x00008000) << 2) | + ((C & 0x00200000) >> 1) | ((C & 0x04000000) >> 10) | + ((D & 0x00000020) << 6) | ((D & 0x00000100) ) | + ((D & 0x00000800) >> 1) | ((D & 0x00000040) >> 3) | + ((D & 0x00010000) >> 4) | ((D & 0x00000400) >> 5) | + ((D & 0x00004000) >> 10) | ((D & 0x04000000) >> 13) | + ((D & 0x00800000) >> 14) | ((D & 0x00100000) >> 18) | + ((D & 0x01000000) >> 24) | ((D & 0x08000000) >> 26); + } + } + +/* +* DES Encryption +*/ +void des_encrypt(u32bit& L, u32bit& R, + const u32bit round_key[32]) + { + for(u32bit j = 0; j != 16; j += 2) + { + u32bit T0, T1; + + T0 = rotate_right(R, 4) ^ round_key[2*j]; + T1 = R ^ round_key[2*j + 1]; + + L ^= DES_SPBOX1[get_byte(0, T0)] ^ DES_SPBOX2[get_byte(0, T1)] ^ + DES_SPBOX3[get_byte(1, T0)] ^ DES_SPBOX4[get_byte(1, T1)] ^ + DES_SPBOX5[get_byte(2, T0)] ^ DES_SPBOX6[get_byte(2, T1)] ^ + DES_SPBOX7[get_byte(3, T0)] ^ DES_SPBOX8[get_byte(3, T1)]; + + T0 = rotate_right(L, 4) ^ round_key[2*j + 2]; + T1 = L ^ round_key[2*j + 3]; + + R ^= DES_SPBOX1[get_byte(0, T0)] ^ DES_SPBOX2[get_byte(0, T1)] ^ + DES_SPBOX3[get_byte(1, T0)] ^ DES_SPBOX4[get_byte(1, T1)] ^ + DES_SPBOX5[get_byte(2, T0)] ^ DES_SPBOX6[get_byte(2, T1)] ^ + DES_SPBOX7[get_byte(3, T0)] ^ DES_SPBOX8[get_byte(3, T1)]; + } + } + +/* +* DES Decryption +*/ +void des_decrypt(u32bit& L, u32bit& R, + const u32bit round_key[32]) + { + for(u32bit j = 16; j != 0; j -= 2) + { + u32bit T0, T1; + + T0 = rotate_right(R, 4) ^ round_key[2*j - 2]; + T1 = R ^ round_key[2*j - 1]; + + L ^= DES_SPBOX1[get_byte(0, T0)] ^ DES_SPBOX2[get_byte(0, T1)] ^ + DES_SPBOX3[get_byte(1, T0)] ^ DES_SPBOX4[get_byte(1, T1)] ^ + DES_SPBOX5[get_byte(2, T0)] ^ DES_SPBOX6[get_byte(2, T1)] ^ + DES_SPBOX7[get_byte(3, T0)] ^ DES_SPBOX8[get_byte(3, T1)]; + + T0 = rotate_right(L, 4) ^ round_key[2*j - 4]; + T1 = L ^ round_key[2*j - 3]; + + R ^= DES_SPBOX1[get_byte(0, T0)] ^ DES_SPBOX2[get_byte(0, T1)] ^ + DES_SPBOX3[get_byte(1, T0)] ^ DES_SPBOX4[get_byte(1, T1)] ^ + DES_SPBOX5[get_byte(2, T0)] ^ DES_SPBOX6[get_byte(2, T1)] ^ + DES_SPBOX7[get_byte(3, T0)] ^ DES_SPBOX8[get_byte(3, T1)]; + } + } + +} + +/* +* DES Encryption +*/ +void DES::enc(const byte in[], byte out[]) const + { + u64bit T = (DES_IPTAB1[in[0]] ) | (DES_IPTAB1[in[1]] << 1) | + (DES_IPTAB1[in[2]] << 2) | (DES_IPTAB1[in[3]] << 3) | + (DES_IPTAB1[in[4]] << 4) | (DES_IPTAB1[in[5]] << 5) | + (DES_IPTAB1[in[6]] << 6) | (DES_IPTAB2[in[7]] ); + + u32bit L = static_cast<u32bit>(T >> 32); + u32bit R = static_cast<u32bit>(T); + + des_encrypt(L, R, round_key); + + T = (DES_FPTAB1[get_byte(0, L)] << 5) | (DES_FPTAB1[get_byte(1, L)] << 3) | + (DES_FPTAB1[get_byte(2, L)] << 1) | (DES_FPTAB2[get_byte(3, L)] << 1) | + (DES_FPTAB1[get_byte(0, R)] << 4) | (DES_FPTAB1[get_byte(1, R)] << 2) | + (DES_FPTAB1[get_byte(2, R)] ) | (DES_FPTAB2[get_byte(3, R)] ); + + T = rotate_left(T, 32); + + store_be(T, out); + } + +/* +* DES Decryption +*/ +void DES::dec(const byte in[], byte out[]) const + { + u64bit T = (DES_IPTAB1[in[0]] ) | (DES_IPTAB1[in[1]] << 1) | + (DES_IPTAB1[in[2]] << 2) | (DES_IPTAB1[in[3]] << 3) | + (DES_IPTAB1[in[4]] << 4) | (DES_IPTAB1[in[5]] << 5) | + (DES_IPTAB1[in[6]] << 6) | (DES_IPTAB2[in[7]] ); + + u32bit L = static_cast<u32bit>(T >> 32); + u32bit R = static_cast<u32bit>(T); + + des_decrypt(L, R, round_key); + + T = (DES_FPTAB1[get_byte(0, L)] << 5) | (DES_FPTAB1[get_byte(1, L)] << 3) | + (DES_FPTAB1[get_byte(2, L)] << 1) | (DES_FPTAB2[get_byte(3, L)] << 1) | + (DES_FPTAB1[get_byte(0, R)] << 4) | (DES_FPTAB1[get_byte(1, R)] << 2) | + (DES_FPTAB1[get_byte(2, R)] ) | (DES_FPTAB2[get_byte(3, R)] ); + + T = rotate_left(T, 32); + + store_be(T, out); + } + +/* +* DES Key Schedule +*/ +void DES::key_schedule(const byte key[], u32bit) + { + des_key_schedule(round_key.begin(), key); + } + +/* +* TripleDES Encryption +*/ +void TripleDES::enc(const byte in[], byte out[]) const + { + u64bit T = (DES_IPTAB1[in[0]] ) | (DES_IPTAB1[in[1]] << 1) | + (DES_IPTAB1[in[2]] << 2) | (DES_IPTAB1[in[3]] << 3) | + (DES_IPTAB1[in[4]] << 4) | (DES_IPTAB1[in[5]] << 5) | + (DES_IPTAB1[in[6]] << 6) | (DES_IPTAB2[in[7]] ); + + u32bit L = static_cast<u32bit>(T >> 32); + u32bit R = static_cast<u32bit>(T); + + des_encrypt(L, R, round_key); + des_decrypt(R, L, round_key + 32); + des_encrypt(L, R, round_key + 64); + + T = (DES_FPTAB1[get_byte(0, L)] << 5) | (DES_FPTAB1[get_byte(1, L)] << 3) | + (DES_FPTAB1[get_byte(2, L)] << 1) | (DES_FPTAB2[get_byte(3, L)] << 1) | + (DES_FPTAB1[get_byte(0, R)] << 4) | (DES_FPTAB1[get_byte(1, R)] << 2) | + (DES_FPTAB1[get_byte(2, R)] ) | (DES_FPTAB2[get_byte(3, R)] ); + + T = rotate_left(T, 32); + + store_be(T, out); + } + +/* +* TripleDES Decryption +*/ +void TripleDES::dec(const byte in[], byte out[]) const + { + u64bit T = (DES_IPTAB1[in[0]] ) | (DES_IPTAB1[in[1]] << 1) | + (DES_IPTAB1[in[2]] << 2) | (DES_IPTAB1[in[3]] << 3) | + (DES_IPTAB1[in[4]] << 4) | (DES_IPTAB1[in[5]] << 5) | + (DES_IPTAB1[in[6]] << 6) | (DES_IPTAB2[in[7]] ); + + u32bit L = static_cast<u32bit>(T >> 32); + u32bit R = static_cast<u32bit>(T); + + des_decrypt(L, R, round_key + 64); + des_encrypt(R, L, round_key + 32); + des_decrypt(L, R, round_key); + + T = (DES_FPTAB1[get_byte(0, L)] << 5) | (DES_FPTAB1[get_byte(1, L)] << 3) | + (DES_FPTAB1[get_byte(2, L)] << 1) | (DES_FPTAB2[get_byte(3, L)] << 1) | + (DES_FPTAB1[get_byte(0, R)] << 4) | (DES_FPTAB1[get_byte(1, R)] << 2) | + (DES_FPTAB1[get_byte(2, R)] ) | (DES_FPTAB2[get_byte(3, R)] ); + + T = rotate_left(T, 32); + + store_be(T, out); + } + +/* +* TripleDES Key Schedule +*/ +void TripleDES::key_schedule(const byte key[], u32bit length) + { + des_key_schedule(&round_key[0], key); + des_key_schedule(&round_key[32], key + 8); + + if(length == 24) + des_key_schedule(&round_key[64], key + 16); + else + copy_mem(&round_key[64], round_key.begin(), 32); + } + +} diff --git a/botan/src/block/des/des.h b/botan/src/block/des/des.h new file mode 100644 index 0000000..6fa59de --- /dev/null +++ b/botan/src/block/des/des.h @@ -0,0 +1,70 @@ +/* +* DES +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_DES_H__ +#define BOTAN_DES_H__ + +#include <botan/block_cipher.h> + +namespace Botan { + +/* +* DES +*/ +class BOTAN_DLL DES : public BlockCipher + { + public: + void clear() throw() { round_key.clear(); } + std::string name() const { return "DES"; } + BlockCipher* clone() const { return new DES; } + DES() : BlockCipher(8, 8) {} + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key_schedule(const byte[], u32bit); + + SecureBuffer<u32bit, 32> round_key; + }; + +/* +* Triple DES +*/ +class BOTAN_DLL TripleDES : public BlockCipher + { + public: + void clear() throw() { round_key.clear(); } + std::string name() const { return "TripleDES"; } + BlockCipher* clone() const { return new TripleDES; } + TripleDES() : BlockCipher(8, 16, 24, 8) {} + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key_schedule(const byte[], u32bit); + + SecureBuffer<u32bit, 96> round_key; + }; + +/* +* DES Tables +*/ +extern const u32bit DES_SPBOX1[256]; +extern const u32bit DES_SPBOX2[256]; +extern const u32bit DES_SPBOX3[256]; +extern const u32bit DES_SPBOX4[256]; +extern const u32bit DES_SPBOX5[256]; +extern const u32bit DES_SPBOX6[256]; +extern const u32bit DES_SPBOX7[256]; +extern const u32bit DES_SPBOX8[256]; + +extern const u64bit DES_IPTAB1[256]; +extern const u64bit DES_IPTAB2[256]; +extern const u64bit DES_FPTAB1[256]; +extern const u64bit DES_FPTAB2[256]; + +} + +#endif diff --git a/botan/src/block/des/des_tab.cpp b/botan/src/block/des/des_tab.cpp new file mode 100644 index 0000000..288c7f3 --- /dev/null +++ b/botan/src/block/des/des_tab.cpp @@ -0,0 +1,636 @@ +/* +* Substitution/Permutation Tables for DES +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/des.h> + +namespace Botan { + +const u32bit DES_SPBOX1[256] = { + 0x01010400, 0x00000000, 0x00010000, 0x01010404, 0x01010004, 0x00010404, + 0x00000004, 0x00010000, 0x00000400, 0x01010400, 0x01010404, 0x00000400, + 0x01000404, 0x01010004, 0x01000000, 0x00000004, 0x00000404, 0x01000400, + 0x01000400, 0x00010400, 0x00010400, 0x01010000, 0x01010000, 0x01000404, + 0x00010004, 0x01000004, 0x01000004, 0x00010004, 0x00000000, 0x00000404, + 0x00010404, 0x01000000, 0x00010000, 0x01010404, 0x00000004, 0x01010000, + 0x01010400, 0x01000000, 0x01000000, 0x00000400, 0x01010004, 0x00010000, + 0x00010400, 0x01000004, 0x00000400, 0x00000004, 0x01000404, 0x00010404, + 0x01010404, 0x00010004, 0x01010000, 0x01000404, 0x01000004, 0x00000404, + 0x00010404, 0x01010400, 0x00000404, 0x01000400, 0x01000400, 0x00000000, + 0x00010004, 0x00010400, 0x00000000, 0x01010004, 0x01010400, 0x00000000, + 0x00010000, 0x01010404, 0x01010004, 0x00010404, 0x00000004, 0x00010000, + 0x00000400, 0x01010400, 0x01010404, 0x00000400, 0x01000404, 0x01010004, + 0x01000000, 0x00000004, 0x00000404, 0x01000400, 0x01000400, 0x00010400, + 0x00010400, 0x01010000, 0x01010000, 0x01000404, 0x00010004, 0x01000004, + 0x01000004, 0x00010004, 0x00000000, 0x00000404, 0x00010404, 0x01000000, + 0x00010000, 0x01010404, 0x00000004, 0x01010000, 0x01010400, 0x01000000, + 0x01000000, 0x00000400, 0x01010004, 0x00010000, 0x00010400, 0x01000004, + 0x00000400, 0x00000004, 0x01000404, 0x00010404, 0x01010404, 0x00010004, + 0x01010000, 0x01000404, 0x01000004, 0x00000404, 0x00010404, 0x01010400, + 0x00000404, 0x01000400, 0x01000400, 0x00000000, 0x00010004, 0x00010400, + 0x00000000, 0x01010004, 0x01010400, 0x00000000, 0x00010000, 0x01010404, + 0x01010004, 0x00010404, 0x00000004, 0x00010000, 0x00000400, 0x01010400, + 0x01010404, 0x00000400, 0x01000404, 0x01010004, 0x01000000, 0x00000004, + 0x00000404, 0x01000400, 0x01000400, 0x00010400, 0x00010400, 0x01010000, + 0x01010000, 0x01000404, 0x00010004, 0x01000004, 0x01000004, 0x00010004, + 0x00000000, 0x00000404, 0x00010404, 0x01000000, 0x00010000, 0x01010404, + 0x00000004, 0x01010000, 0x01010400, 0x01000000, 0x01000000, 0x00000400, + 0x01010004, 0x00010000, 0x00010400, 0x01000004, 0x00000400, 0x00000004, + 0x01000404, 0x00010404, 0x01010404, 0x00010004, 0x01010000, 0x01000404, + 0x01000004, 0x00000404, 0x00010404, 0x01010400, 0x00000404, 0x01000400, + 0x01000400, 0x00000000, 0x00010004, 0x00010400, 0x00000000, 0x01010004, + 0x01010400, 0x00000000, 0x00010000, 0x01010404, 0x01010004, 0x00010404, + 0x00000004, 0x00010000, 0x00000400, 0x01010400, 0x01010404, 0x00000400, + 0x01000404, 0x01010004, 0x01000000, 0x00000004, 0x00000404, 0x01000400, + 0x01000400, 0x00010400, 0x00010400, 0x01010000, 0x01010000, 0x01000404, + 0x00010004, 0x01000004, 0x01000004, 0x00010004, 0x00000000, 0x00000404, + 0x00010404, 0x01000000, 0x00010000, 0x01010404, 0x00000004, 0x01010000, + 0x01010400, 0x01000000, 0x01000000, 0x00000400, 0x01010004, 0x00010000, + 0x00010400, 0x01000004, 0x00000400, 0x00000004, 0x01000404, 0x00010404, + 0x01010404, 0x00010004, 0x01010000, 0x01000404, 0x01000004, 0x00000404, + 0x00010404, 0x01010400, 0x00000404, 0x01000400, 0x01000400, 0x00000000, + 0x00010004, 0x00010400, 0x00000000, 0x01010004 }; + +const u32bit DES_SPBOX2[256] = { + 0x80108020, 0x80008000, 0x00008000, 0x00108020, 0x00100000, 0x00000020, + 0x80100020, 0x80008020, 0x80000020, 0x80108020, 0x80108000, 0x80000000, + 0x80008000, 0x00100000, 0x00000020, 0x80100020, 0x00108000, 0x00100020, + 0x80008020, 0x00000000, 0x80000000, 0x00008000, 0x00108020, 0x80100000, + 0x00100020, 0x80000020, 0x00000000, 0x00108000, 0x00008020, 0x80108000, + 0x80100000, 0x00008020, 0x00000000, 0x00108020, 0x80100020, 0x00100000, + 0x80008020, 0x80100000, 0x80108000, 0x00008000, 0x80100000, 0x80008000, + 0x00000020, 0x80108020, 0x00108020, 0x00000020, 0x00008000, 0x80000000, + 0x00008020, 0x80108000, 0x00100000, 0x80000020, 0x00100020, 0x80008020, + 0x80000020, 0x00100020, 0x00108000, 0x00000000, 0x80008000, 0x00008020, + 0x80000000, 0x80100020, 0x80108020, 0x00108000, 0x80108020, 0x80008000, + 0x00008000, 0x00108020, 0x00100000, 0x00000020, 0x80100020, 0x80008020, + 0x80000020, 0x80108020, 0x80108000, 0x80000000, 0x80008000, 0x00100000, + 0x00000020, 0x80100020, 0x00108000, 0x00100020, 0x80008020, 0x00000000, + 0x80000000, 0x00008000, 0x00108020, 0x80100000, 0x00100020, 0x80000020, + 0x00000000, 0x00108000, 0x00008020, 0x80108000, 0x80100000, 0x00008020, + 0x00000000, 0x00108020, 0x80100020, 0x00100000, 0x80008020, 0x80100000, + 0x80108000, 0x00008000, 0x80100000, 0x80008000, 0x00000020, 0x80108020, + 0x00108020, 0x00000020, 0x00008000, 0x80000000, 0x00008020, 0x80108000, + 0x00100000, 0x80000020, 0x00100020, 0x80008020, 0x80000020, 0x00100020, + 0x00108000, 0x00000000, 0x80008000, 0x00008020, 0x80000000, 0x80100020, + 0x80108020, 0x00108000, 0x80108020, 0x80008000, 0x00008000, 0x00108020, + 0x00100000, 0x00000020, 0x80100020, 0x80008020, 0x80000020, 0x80108020, + 0x80108000, 0x80000000, 0x80008000, 0x00100000, 0x00000020, 0x80100020, + 0x00108000, 0x00100020, 0x80008020, 0x00000000, 0x80000000, 0x00008000, + 0x00108020, 0x80100000, 0x00100020, 0x80000020, 0x00000000, 0x00108000, + 0x00008020, 0x80108000, 0x80100000, 0x00008020, 0x00000000, 0x00108020, + 0x80100020, 0x00100000, 0x80008020, 0x80100000, 0x80108000, 0x00008000, + 0x80100000, 0x80008000, 0x00000020, 0x80108020, 0x00108020, 0x00000020, + 0x00008000, 0x80000000, 0x00008020, 0x80108000, 0x00100000, 0x80000020, + 0x00100020, 0x80008020, 0x80000020, 0x00100020, 0x00108000, 0x00000000, + 0x80008000, 0x00008020, 0x80000000, 0x80100020, 0x80108020, 0x00108000, + 0x80108020, 0x80008000, 0x00008000, 0x00108020, 0x00100000, 0x00000020, + 0x80100020, 0x80008020, 0x80000020, 0x80108020, 0x80108000, 0x80000000, + 0x80008000, 0x00100000, 0x00000020, 0x80100020, 0x00108000, 0x00100020, + 0x80008020, 0x00000000, 0x80000000, 0x00008000, 0x00108020, 0x80100000, + 0x00100020, 0x80000020, 0x00000000, 0x00108000, 0x00008020, 0x80108000, + 0x80100000, 0x00008020, 0x00000000, 0x00108020, 0x80100020, 0x00100000, + 0x80008020, 0x80100000, 0x80108000, 0x00008000, 0x80100000, 0x80008000, + 0x00000020, 0x80108020, 0x00108020, 0x00000020, 0x00008000, 0x80000000, + 0x00008020, 0x80108000, 0x00100000, 0x80000020, 0x00100020, 0x80008020, + 0x80000020, 0x00100020, 0x00108000, 0x00000000, 0x80008000, 0x00008020, + 0x80000000, 0x80100020, 0x80108020, 0x00108000 }; + +const u32bit DES_SPBOX3[256] = { + 0x00000208, 0x08020200, 0x00000000, 0x08020008, 0x08000200, 0x00000000, + 0x00020208, 0x08000200, 0x00020008, 0x08000008, 0x08000008, 0x00020000, + 0x08020208, 0x00020008, 0x08020000, 0x00000208, 0x08000000, 0x00000008, + 0x08020200, 0x00000200, 0x00020200, 0x08020000, 0x08020008, 0x00020208, + 0x08000208, 0x00020200, 0x00020000, 0x08000208, 0x00000008, 0x08020208, + 0x00000200, 0x08000000, 0x08020200, 0x08000000, 0x00020008, 0x00000208, + 0x00020000, 0x08020200, 0x08000200, 0x00000000, 0x00000200, 0x00020008, + 0x08020208, 0x08000200, 0x08000008, 0x00000200, 0x00000000, 0x08020008, + 0x08000208, 0x00020000, 0x08000000, 0x08020208, 0x00000008, 0x00020208, + 0x00020200, 0x08000008, 0x08020000, 0x08000208, 0x00000208, 0x08020000, + 0x00020208, 0x00000008, 0x08020008, 0x00020200, 0x00000208, 0x08020200, + 0x00000000, 0x08020008, 0x08000200, 0x00000000, 0x00020208, 0x08000200, + 0x00020008, 0x08000008, 0x08000008, 0x00020000, 0x08020208, 0x00020008, + 0x08020000, 0x00000208, 0x08000000, 0x00000008, 0x08020200, 0x00000200, + 0x00020200, 0x08020000, 0x08020008, 0x00020208, 0x08000208, 0x00020200, + 0x00020000, 0x08000208, 0x00000008, 0x08020208, 0x00000200, 0x08000000, + 0x08020200, 0x08000000, 0x00020008, 0x00000208, 0x00020000, 0x08020200, + 0x08000200, 0x00000000, 0x00000200, 0x00020008, 0x08020208, 0x08000200, + 0x08000008, 0x00000200, 0x00000000, 0x08020008, 0x08000208, 0x00020000, + 0x08000000, 0x08020208, 0x00000008, 0x00020208, 0x00020200, 0x08000008, + 0x08020000, 0x08000208, 0x00000208, 0x08020000, 0x00020208, 0x00000008, + 0x08020008, 0x00020200, 0x00000208, 0x08020200, 0x00000000, 0x08020008, + 0x08000200, 0x00000000, 0x00020208, 0x08000200, 0x00020008, 0x08000008, + 0x08000008, 0x00020000, 0x08020208, 0x00020008, 0x08020000, 0x00000208, + 0x08000000, 0x00000008, 0x08020200, 0x00000200, 0x00020200, 0x08020000, + 0x08020008, 0x00020208, 0x08000208, 0x00020200, 0x00020000, 0x08000208, + 0x00000008, 0x08020208, 0x00000200, 0x08000000, 0x08020200, 0x08000000, + 0x00020008, 0x00000208, 0x00020000, 0x08020200, 0x08000200, 0x00000000, + 0x00000200, 0x00020008, 0x08020208, 0x08000200, 0x08000008, 0x00000200, + 0x00000000, 0x08020008, 0x08000208, 0x00020000, 0x08000000, 0x08020208, + 0x00000008, 0x00020208, 0x00020200, 0x08000008, 0x08020000, 0x08000208, + 0x00000208, 0x08020000, 0x00020208, 0x00000008, 0x08020008, 0x00020200, + 0x00000208, 0x08020200, 0x00000000, 0x08020008, 0x08000200, 0x00000000, + 0x00020208, 0x08000200, 0x00020008, 0x08000008, 0x08000008, 0x00020000, + 0x08020208, 0x00020008, 0x08020000, 0x00000208, 0x08000000, 0x00000008, + 0x08020200, 0x00000200, 0x00020200, 0x08020000, 0x08020008, 0x00020208, + 0x08000208, 0x00020200, 0x00020000, 0x08000208, 0x00000008, 0x08020208, + 0x00000200, 0x08000000, 0x08020200, 0x08000000, 0x00020008, 0x00000208, + 0x00020000, 0x08020200, 0x08000200, 0x00000000, 0x00000200, 0x00020008, + 0x08020208, 0x08000200, 0x08000008, 0x00000200, 0x00000000, 0x08020008, + 0x08000208, 0x00020000, 0x08000000, 0x08020208, 0x00000008, 0x00020208, + 0x00020200, 0x08000008, 0x08020000, 0x08000208, 0x00000208, 0x08020000, + 0x00020208, 0x00000008, 0x08020008, 0x00020200 }; + +const u32bit DES_SPBOX4[256] = { + 0x00802001, 0x00002081, 0x00002081, 0x00000080, 0x00802080, 0x00800081, + 0x00800001, 0x00002001, 0x00000000, 0x00802000, 0x00802000, 0x00802081, + 0x00000081, 0x00000000, 0x00800080, 0x00800001, 0x00000001, 0x00002000, + 0x00800000, 0x00802001, 0x00000080, 0x00800000, 0x00002001, 0x00002080, + 0x00800081, 0x00000001, 0x00002080, 0x00800080, 0x00002000, 0x00802080, + 0x00802081, 0x00000081, 0x00800080, 0x00800001, 0x00802000, 0x00802081, + 0x00000081, 0x00000000, 0x00000000, 0x00802000, 0x00002080, 0x00800080, + 0x00800081, 0x00000001, 0x00802001, 0x00002081, 0x00002081, 0x00000080, + 0x00802081, 0x00000081, 0x00000001, 0x00002000, 0x00800001, 0x00002001, + 0x00802080, 0x00800081, 0x00002001, 0x00002080, 0x00800000, 0x00802001, + 0x00000080, 0x00800000, 0x00002000, 0x00802080, 0x00802001, 0x00002081, + 0x00002081, 0x00000080, 0x00802080, 0x00800081, 0x00800001, 0x00002001, + 0x00000000, 0x00802000, 0x00802000, 0x00802081, 0x00000081, 0x00000000, + 0x00800080, 0x00800001, 0x00000001, 0x00002000, 0x00800000, 0x00802001, + 0x00000080, 0x00800000, 0x00002001, 0x00002080, 0x00800081, 0x00000001, + 0x00002080, 0x00800080, 0x00002000, 0x00802080, 0x00802081, 0x00000081, + 0x00800080, 0x00800001, 0x00802000, 0x00802081, 0x00000081, 0x00000000, + 0x00000000, 0x00802000, 0x00002080, 0x00800080, 0x00800081, 0x00000001, + 0x00802001, 0x00002081, 0x00002081, 0x00000080, 0x00802081, 0x00000081, + 0x00000001, 0x00002000, 0x00800001, 0x00002001, 0x00802080, 0x00800081, + 0x00002001, 0x00002080, 0x00800000, 0x00802001, 0x00000080, 0x00800000, + 0x00002000, 0x00802080, 0x00802001, 0x00002081, 0x00002081, 0x00000080, + 0x00802080, 0x00800081, 0x00800001, 0x00002001, 0x00000000, 0x00802000, + 0x00802000, 0x00802081, 0x00000081, 0x00000000, 0x00800080, 0x00800001, + 0x00000001, 0x00002000, 0x00800000, 0x00802001, 0x00000080, 0x00800000, + 0x00002001, 0x00002080, 0x00800081, 0x00000001, 0x00002080, 0x00800080, + 0x00002000, 0x00802080, 0x00802081, 0x00000081, 0x00800080, 0x00800001, + 0x00802000, 0x00802081, 0x00000081, 0x00000000, 0x00000000, 0x00802000, + 0x00002080, 0x00800080, 0x00800081, 0x00000001, 0x00802001, 0x00002081, + 0x00002081, 0x00000080, 0x00802081, 0x00000081, 0x00000001, 0x00002000, + 0x00800001, 0x00002001, 0x00802080, 0x00800081, 0x00002001, 0x00002080, + 0x00800000, 0x00802001, 0x00000080, 0x00800000, 0x00002000, 0x00802080, + 0x00802001, 0x00002081, 0x00002081, 0x00000080, 0x00802080, 0x00800081, + 0x00800001, 0x00002001, 0x00000000, 0x00802000, 0x00802000, 0x00802081, + 0x00000081, 0x00000000, 0x00800080, 0x00800001, 0x00000001, 0x00002000, + 0x00800000, 0x00802001, 0x00000080, 0x00800000, 0x00002001, 0x00002080, + 0x00800081, 0x00000001, 0x00002080, 0x00800080, 0x00002000, 0x00802080, + 0x00802081, 0x00000081, 0x00800080, 0x00800001, 0x00802000, 0x00802081, + 0x00000081, 0x00000000, 0x00000000, 0x00802000, 0x00002080, 0x00800080, + 0x00800081, 0x00000001, 0x00802001, 0x00002081, 0x00002081, 0x00000080, + 0x00802081, 0x00000081, 0x00000001, 0x00002000, 0x00800001, 0x00002001, + 0x00802080, 0x00800081, 0x00002001, 0x00002080, 0x00800000, 0x00802001, + 0x00000080, 0x00800000, 0x00002000, 0x00802080 }; + +const u32bit DES_SPBOX5[256] = { + 0x00000100, 0x02080100, 0x02080000, 0x42000100, 0x00080000, 0x00000100, + 0x40000000, 0x02080000, 0x40080100, 0x00080000, 0x02000100, 0x40080100, + 0x42000100, 0x42080000, 0x00080100, 0x40000000, 0x02000000, 0x40080000, + 0x40080000, 0x00000000, 0x40000100, 0x42080100, 0x42080100, 0x02000100, + 0x42080000, 0x40000100, 0x00000000, 0x42000000, 0x02080100, 0x02000000, + 0x42000000, 0x00080100, 0x00080000, 0x42000100, 0x00000100, 0x02000000, + 0x40000000, 0x02080000, 0x42000100, 0x40080100, 0x02000100, 0x40000000, + 0x42080000, 0x02080100, 0x40080100, 0x00000100, 0x02000000, 0x42080000, + 0x42080100, 0x00080100, 0x42000000, 0x42080100, 0x02080000, 0x00000000, + 0x40080000, 0x42000000, 0x00080100, 0x02000100, 0x40000100, 0x00080000, + 0x00000000, 0x40080000, 0x02080100, 0x40000100, 0x00000100, 0x02080100, + 0x02080000, 0x42000100, 0x00080000, 0x00000100, 0x40000000, 0x02080000, + 0x40080100, 0x00080000, 0x02000100, 0x40080100, 0x42000100, 0x42080000, + 0x00080100, 0x40000000, 0x02000000, 0x40080000, 0x40080000, 0x00000000, + 0x40000100, 0x42080100, 0x42080100, 0x02000100, 0x42080000, 0x40000100, + 0x00000000, 0x42000000, 0x02080100, 0x02000000, 0x42000000, 0x00080100, + 0x00080000, 0x42000100, 0x00000100, 0x02000000, 0x40000000, 0x02080000, + 0x42000100, 0x40080100, 0x02000100, 0x40000000, 0x42080000, 0x02080100, + 0x40080100, 0x00000100, 0x02000000, 0x42080000, 0x42080100, 0x00080100, + 0x42000000, 0x42080100, 0x02080000, 0x00000000, 0x40080000, 0x42000000, + 0x00080100, 0x02000100, 0x40000100, 0x00080000, 0x00000000, 0x40080000, + 0x02080100, 0x40000100, 0x00000100, 0x02080100, 0x02080000, 0x42000100, + 0x00080000, 0x00000100, 0x40000000, 0x02080000, 0x40080100, 0x00080000, + 0x02000100, 0x40080100, 0x42000100, 0x42080000, 0x00080100, 0x40000000, + 0x02000000, 0x40080000, 0x40080000, 0x00000000, 0x40000100, 0x42080100, + 0x42080100, 0x02000100, 0x42080000, 0x40000100, 0x00000000, 0x42000000, + 0x02080100, 0x02000000, 0x42000000, 0x00080100, 0x00080000, 0x42000100, + 0x00000100, 0x02000000, 0x40000000, 0x02080000, 0x42000100, 0x40080100, + 0x02000100, 0x40000000, 0x42080000, 0x02080100, 0x40080100, 0x00000100, + 0x02000000, 0x42080000, 0x42080100, 0x00080100, 0x42000000, 0x42080100, + 0x02080000, 0x00000000, 0x40080000, 0x42000000, 0x00080100, 0x02000100, + 0x40000100, 0x00080000, 0x00000000, 0x40080000, 0x02080100, 0x40000100, + 0x00000100, 0x02080100, 0x02080000, 0x42000100, 0x00080000, 0x00000100, + 0x40000000, 0x02080000, 0x40080100, 0x00080000, 0x02000100, 0x40080100, + 0x42000100, 0x42080000, 0x00080100, 0x40000000, 0x02000000, 0x40080000, + 0x40080000, 0x00000000, 0x40000100, 0x42080100, 0x42080100, 0x02000100, + 0x42080000, 0x40000100, 0x00000000, 0x42000000, 0x02080100, 0x02000000, + 0x42000000, 0x00080100, 0x00080000, 0x42000100, 0x00000100, 0x02000000, + 0x40000000, 0x02080000, 0x42000100, 0x40080100, 0x02000100, 0x40000000, + 0x42080000, 0x02080100, 0x40080100, 0x00000100, 0x02000000, 0x42080000, + 0x42080100, 0x00080100, 0x42000000, 0x42080100, 0x02080000, 0x00000000, + 0x40080000, 0x42000000, 0x00080100, 0x02000100, 0x40000100, 0x00080000, + 0x00000000, 0x40080000, 0x02080100, 0x40000100 }; + +const u32bit DES_SPBOX6[256] = { + 0x20000010, 0x20400000, 0x00004000, 0x20404010, 0x20400000, 0x00000010, + 0x20404010, 0x00400000, 0x20004000, 0x00404010, 0x00400000, 0x20000010, + 0x00400010, 0x20004000, 0x20000000, 0x00004010, 0x00000000, 0x00400010, + 0x20004010, 0x00004000, 0x00404000, 0x20004010, 0x00000010, 0x20400010, + 0x20400010, 0x00000000, 0x00404010, 0x20404000, 0x00004010, 0x00404000, + 0x20404000, 0x20000000, 0x20004000, 0x00000010, 0x20400010, 0x00404000, + 0x20404010, 0x00400000, 0x00004010, 0x20000010, 0x00400000, 0x20004000, + 0x20000000, 0x00004010, 0x20000010, 0x20404010, 0x00404000, 0x20400000, + 0x00404010, 0x20404000, 0x00000000, 0x20400010, 0x00000010, 0x00004000, + 0x20400000, 0x00404010, 0x00004000, 0x00400010, 0x20004010, 0x00000000, + 0x20404000, 0x20000000, 0x00400010, 0x20004010, 0x20000010, 0x20400000, + 0x00004000, 0x20404010, 0x20400000, 0x00000010, 0x20404010, 0x00400000, + 0x20004000, 0x00404010, 0x00400000, 0x20000010, 0x00400010, 0x20004000, + 0x20000000, 0x00004010, 0x00000000, 0x00400010, 0x20004010, 0x00004000, + 0x00404000, 0x20004010, 0x00000010, 0x20400010, 0x20400010, 0x00000000, + 0x00404010, 0x20404000, 0x00004010, 0x00404000, 0x20404000, 0x20000000, + 0x20004000, 0x00000010, 0x20400010, 0x00404000, 0x20404010, 0x00400000, + 0x00004010, 0x20000010, 0x00400000, 0x20004000, 0x20000000, 0x00004010, + 0x20000010, 0x20404010, 0x00404000, 0x20400000, 0x00404010, 0x20404000, + 0x00000000, 0x20400010, 0x00000010, 0x00004000, 0x20400000, 0x00404010, + 0x00004000, 0x00400010, 0x20004010, 0x00000000, 0x20404000, 0x20000000, + 0x00400010, 0x20004010, 0x20000010, 0x20400000, 0x00004000, 0x20404010, + 0x20400000, 0x00000010, 0x20404010, 0x00400000, 0x20004000, 0x00404010, + 0x00400000, 0x20000010, 0x00400010, 0x20004000, 0x20000000, 0x00004010, + 0x00000000, 0x00400010, 0x20004010, 0x00004000, 0x00404000, 0x20004010, + 0x00000010, 0x20400010, 0x20400010, 0x00000000, 0x00404010, 0x20404000, + 0x00004010, 0x00404000, 0x20404000, 0x20000000, 0x20004000, 0x00000010, + 0x20400010, 0x00404000, 0x20404010, 0x00400000, 0x00004010, 0x20000010, + 0x00400000, 0x20004000, 0x20000000, 0x00004010, 0x20000010, 0x20404010, + 0x00404000, 0x20400000, 0x00404010, 0x20404000, 0x00000000, 0x20400010, + 0x00000010, 0x00004000, 0x20400000, 0x00404010, 0x00004000, 0x00400010, + 0x20004010, 0x00000000, 0x20404000, 0x20000000, 0x00400010, 0x20004010, + 0x20000010, 0x20400000, 0x00004000, 0x20404010, 0x20400000, 0x00000010, + 0x20404010, 0x00400000, 0x20004000, 0x00404010, 0x00400000, 0x20000010, + 0x00400010, 0x20004000, 0x20000000, 0x00004010, 0x00000000, 0x00400010, + 0x20004010, 0x00004000, 0x00404000, 0x20004010, 0x00000010, 0x20400010, + 0x20400010, 0x00000000, 0x00404010, 0x20404000, 0x00004010, 0x00404000, + 0x20404000, 0x20000000, 0x20004000, 0x00000010, 0x20400010, 0x00404000, + 0x20404010, 0x00400000, 0x00004010, 0x20000010, 0x00400000, 0x20004000, + 0x20000000, 0x00004010, 0x20000010, 0x20404010, 0x00404000, 0x20400000, + 0x00404010, 0x20404000, 0x00000000, 0x20400010, 0x00000010, 0x00004000, + 0x20400000, 0x00404010, 0x00004000, 0x00400010, 0x20004010, 0x00000000, + 0x20404000, 0x20000000, 0x00400010, 0x20004010 }; + +const u32bit DES_SPBOX7[256] = { + 0x00200000, 0x04200002, 0x04000802, 0x00000000, 0x00000800, 0x04000802, + 0x00200802, 0x04200800, 0x04200802, 0x00200000, 0x00000000, 0x04000002, + 0x00000002, 0x04000000, 0x04200002, 0x00000802, 0x04000800, 0x00200802, + 0x00200002, 0x04000800, 0x04000002, 0x04200000, 0x04200800, 0x00200002, + 0x04200000, 0x00000800, 0x00000802, 0x04200802, 0x00200800, 0x00000002, + 0x04000000, 0x00200800, 0x04000000, 0x00200800, 0x00200000, 0x04000802, + 0x04000802, 0x04200002, 0x04200002, 0x00000002, 0x00200002, 0x04000000, + 0x04000800, 0x00200000, 0x04200800, 0x00000802, 0x00200802, 0x04200800, + 0x00000802, 0x04000002, 0x04200802, 0x04200000, 0x00200800, 0x00000000, + 0x00000002, 0x04200802, 0x00000000, 0x00200802, 0x04200000, 0x00000800, + 0x04000002, 0x04000800, 0x00000800, 0x00200002, 0x00200000, 0x04200002, + 0x04000802, 0x00000000, 0x00000800, 0x04000802, 0x00200802, 0x04200800, + 0x04200802, 0x00200000, 0x00000000, 0x04000002, 0x00000002, 0x04000000, + 0x04200002, 0x00000802, 0x04000800, 0x00200802, 0x00200002, 0x04000800, + 0x04000002, 0x04200000, 0x04200800, 0x00200002, 0x04200000, 0x00000800, + 0x00000802, 0x04200802, 0x00200800, 0x00000002, 0x04000000, 0x00200800, + 0x04000000, 0x00200800, 0x00200000, 0x04000802, 0x04000802, 0x04200002, + 0x04200002, 0x00000002, 0x00200002, 0x04000000, 0x04000800, 0x00200000, + 0x04200800, 0x00000802, 0x00200802, 0x04200800, 0x00000802, 0x04000002, + 0x04200802, 0x04200000, 0x00200800, 0x00000000, 0x00000002, 0x04200802, + 0x00000000, 0x00200802, 0x04200000, 0x00000800, 0x04000002, 0x04000800, + 0x00000800, 0x00200002, 0x00200000, 0x04200002, 0x04000802, 0x00000000, + 0x00000800, 0x04000802, 0x00200802, 0x04200800, 0x04200802, 0x00200000, + 0x00000000, 0x04000002, 0x00000002, 0x04000000, 0x04200002, 0x00000802, + 0x04000800, 0x00200802, 0x00200002, 0x04000800, 0x04000002, 0x04200000, + 0x04200800, 0x00200002, 0x04200000, 0x00000800, 0x00000802, 0x04200802, + 0x00200800, 0x00000002, 0x04000000, 0x00200800, 0x04000000, 0x00200800, + 0x00200000, 0x04000802, 0x04000802, 0x04200002, 0x04200002, 0x00000002, + 0x00200002, 0x04000000, 0x04000800, 0x00200000, 0x04200800, 0x00000802, + 0x00200802, 0x04200800, 0x00000802, 0x04000002, 0x04200802, 0x04200000, + 0x00200800, 0x00000000, 0x00000002, 0x04200802, 0x00000000, 0x00200802, + 0x04200000, 0x00000800, 0x04000002, 0x04000800, 0x00000800, 0x00200002, + 0x00200000, 0x04200002, 0x04000802, 0x00000000, 0x00000800, 0x04000802, + 0x00200802, 0x04200800, 0x04200802, 0x00200000, 0x00000000, 0x04000002, + 0x00000002, 0x04000000, 0x04200002, 0x00000802, 0x04000800, 0x00200802, + 0x00200002, 0x04000800, 0x04000002, 0x04200000, 0x04200800, 0x00200002, + 0x04200000, 0x00000800, 0x00000802, 0x04200802, 0x00200800, 0x00000002, + 0x04000000, 0x00200800, 0x04000000, 0x00200800, 0x00200000, 0x04000802, + 0x04000802, 0x04200002, 0x04200002, 0x00000002, 0x00200002, 0x04000000, + 0x04000800, 0x00200000, 0x04200800, 0x00000802, 0x00200802, 0x04200800, + 0x00000802, 0x04000002, 0x04200802, 0x04200000, 0x00200800, 0x00000000, + 0x00000002, 0x04200802, 0x00000000, 0x00200802, 0x04200000, 0x00000800, + 0x04000002, 0x04000800, 0x00000800, 0x00200002 }; + +const u32bit DES_SPBOX8[256] = { + 0x10001040, 0x00001000, 0x00040000, 0x10041040, 0x10000000, 0x10001040, + 0x00000040, 0x10000000, 0x00040040, 0x10040000, 0x10041040, 0x00041000, + 0x10041000, 0x00041040, 0x00001000, 0x00000040, 0x10040000, 0x10000040, + 0x10001000, 0x00001040, 0x00041000, 0x00040040, 0x10040040, 0x10041000, + 0x00001040, 0x00000000, 0x00000000, 0x10040040, 0x10000040, 0x10001000, + 0x00041040, 0x00040000, 0x00041040, 0x00040000, 0x10041000, 0x00001000, + 0x00000040, 0x10040040, 0x00001000, 0x00041040, 0x10001000, 0x00000040, + 0x10000040, 0x10040000, 0x10040040, 0x10000000, 0x00040000, 0x10001040, + 0x00000000, 0x10041040, 0x00040040, 0x10000040, 0x10040000, 0x10001000, + 0x10001040, 0x00000000, 0x10041040, 0x00041000, 0x00041000, 0x00001040, + 0x00001040, 0x00040040, 0x10000000, 0x10041000, 0x10001040, 0x00001000, + 0x00040000, 0x10041040, 0x10000000, 0x10001040, 0x00000040, 0x10000000, + 0x00040040, 0x10040000, 0x10041040, 0x00041000, 0x10041000, 0x00041040, + 0x00001000, 0x00000040, 0x10040000, 0x10000040, 0x10001000, 0x00001040, + 0x00041000, 0x00040040, 0x10040040, 0x10041000, 0x00001040, 0x00000000, + 0x00000000, 0x10040040, 0x10000040, 0x10001000, 0x00041040, 0x00040000, + 0x00041040, 0x00040000, 0x10041000, 0x00001000, 0x00000040, 0x10040040, + 0x00001000, 0x00041040, 0x10001000, 0x00000040, 0x10000040, 0x10040000, + 0x10040040, 0x10000000, 0x00040000, 0x10001040, 0x00000000, 0x10041040, + 0x00040040, 0x10000040, 0x10040000, 0x10001000, 0x10001040, 0x00000000, + 0x10041040, 0x00041000, 0x00041000, 0x00001040, 0x00001040, 0x00040040, + 0x10000000, 0x10041000, 0x10001040, 0x00001000, 0x00040000, 0x10041040, + 0x10000000, 0x10001040, 0x00000040, 0x10000000, 0x00040040, 0x10040000, + 0x10041040, 0x00041000, 0x10041000, 0x00041040, 0x00001000, 0x00000040, + 0x10040000, 0x10000040, 0x10001000, 0x00001040, 0x00041000, 0x00040040, + 0x10040040, 0x10041000, 0x00001040, 0x00000000, 0x00000000, 0x10040040, + 0x10000040, 0x10001000, 0x00041040, 0x00040000, 0x00041040, 0x00040000, + 0x10041000, 0x00001000, 0x00000040, 0x10040040, 0x00001000, 0x00041040, + 0x10001000, 0x00000040, 0x10000040, 0x10040000, 0x10040040, 0x10000000, + 0x00040000, 0x10001040, 0x00000000, 0x10041040, 0x00040040, 0x10000040, + 0x10040000, 0x10001000, 0x10001040, 0x00000000, 0x10041040, 0x00041000, + 0x00041000, 0x00001040, 0x00001040, 0x00040040, 0x10000000, 0x10041000, + 0x10001040, 0x00001000, 0x00040000, 0x10041040, 0x10000000, 0x10001040, + 0x00000040, 0x10000000, 0x00040040, 0x10040000, 0x10041040, 0x00041000, + 0x10041000, 0x00041040, 0x00001000, 0x00000040, 0x10040000, 0x10000040, + 0x10001000, 0x00001040, 0x00041000, 0x00040040, 0x10040040, 0x10041000, + 0x00001040, 0x00000000, 0x00000000, 0x10040040, 0x10000040, 0x10001000, + 0x00041040, 0x00040000, 0x00041040, 0x00040000, 0x10041000, 0x00001000, + 0x00000040, 0x10040040, 0x00001000, 0x00041040, 0x10001000, 0x00000040, + 0x10000040, 0x10040000, 0x10040040, 0x10000000, 0x00040000, 0x10001040, + 0x00000000, 0x10041040, 0x00040040, 0x10000040, 0x10040000, 0x10001000, + 0x10001040, 0x00000000, 0x10041040, 0x00041000, 0x00041000, 0x00001040, + 0x00001040, 0x00040040, 0x10000000, 0x10041000 }; + +const u64bit DES_IPTAB1[256] = { +(u64bit) 0x0000000000000000ULL, (u64bit) 0x0000000200000000ULL, (u64bit) (u64bit) 0x0000000000000002ULL, (u64bit) 0x0000000200000002ULL, +(u64bit) 0x0000020000000000ULL, (u64bit) 0x0000020200000000ULL, (u64bit) 0x0000020000000002ULL, (u64bit) 0x0000020200000002ULL, +(u64bit) 0x0000000000000200ULL, (u64bit) 0x0000000200000200ULL, (u64bit) 0x0000000000000202ULL, (u64bit) 0x0000000200000202ULL, +(u64bit) 0x0000020000000200ULL, (u64bit) 0x0000020200000200ULL, (u64bit) 0x0000020000000202ULL, (u64bit) 0x0000020200000202ULL, +(u64bit) 0x0002000000000000ULL, (u64bit) 0x0002000200000000ULL, (u64bit) 0x0002000000000002ULL, (u64bit) 0x0002000200000002ULL, +(u64bit) 0x0002020000000000ULL, (u64bit) 0x0002020200000000ULL, (u64bit) 0x0002020000000002ULL, (u64bit) 0x0002020200000002ULL, +(u64bit) 0x0002000000000200ULL, (u64bit) 0x0002000200000200ULL, (u64bit) 0x0002000000000202ULL, (u64bit) 0x0002000200000202ULL, +(u64bit) 0x0002020000000200ULL, (u64bit) 0x0002020200000200ULL, (u64bit) 0x0002020000000202ULL, (u64bit) 0x0002020200000202ULL, +(u64bit) 0x0000000000020000ULL, (u64bit) 0x0000000200020000ULL, (u64bit) 0x0000000000020002ULL, (u64bit) 0x0000000200020002ULL, +(u64bit) 0x0000020000020000ULL, (u64bit) 0x0000020200020000ULL, (u64bit) 0x0000020000020002ULL, (u64bit) 0x0000020200020002ULL, +(u64bit) 0x0000000000020200ULL, (u64bit) 0x0000000200020200ULL, (u64bit) 0x0000000000020202ULL, (u64bit) 0x0000000200020202ULL, +(u64bit) 0x0000020000020200ULL, (u64bit) 0x0000020200020200ULL, (u64bit) 0x0000020000020202ULL, (u64bit) 0x0000020200020202ULL, +(u64bit) 0x0002000000020000ULL, (u64bit) 0x0002000200020000ULL, (u64bit) 0x0002000000020002ULL, (u64bit) 0x0002000200020002ULL, +(u64bit) 0x0002020000020000ULL, (u64bit) 0x0002020200020000ULL, (u64bit) 0x0002020000020002ULL, (u64bit) 0x0002020200020002ULL, +(u64bit) 0x0002000000020200ULL, (u64bit) 0x0002000200020200ULL, (u64bit) 0x0002000000020202ULL, (u64bit) 0x0002000200020202ULL, +(u64bit) 0x0002020000020200ULL, (u64bit) 0x0002020200020200ULL, (u64bit) 0x0002020000020202ULL, (u64bit) 0x0002020200020202ULL, +(u64bit) 0x0200000000000000ULL, (u64bit) 0x0200000200000000ULL, (u64bit) 0x0200000000000002ULL, (u64bit) 0x0200000200000002ULL, +(u64bit) 0x0200020000000000ULL, (u64bit) 0x0200020200000000ULL, (u64bit) 0x0200020000000002ULL, (u64bit) 0x0200020200000002ULL, +(u64bit) 0x0200000000000200ULL, (u64bit) 0x0200000200000200ULL, (u64bit) 0x0200000000000202ULL, (u64bit) 0x0200000200000202ULL, +(u64bit) 0x0200020000000200ULL, (u64bit) 0x0200020200000200ULL, (u64bit) 0x0200020000000202ULL, (u64bit) 0x0200020200000202ULL, +(u64bit) 0x0202000000000000ULL, (u64bit) 0x0202000200000000ULL, (u64bit) 0x0202000000000002ULL, (u64bit) 0x0202000200000002ULL, +(u64bit) 0x0202020000000000ULL, (u64bit) 0x0202020200000000ULL, (u64bit) 0x0202020000000002ULL, (u64bit) 0x0202020200000002ULL, +(u64bit) 0x0202000000000200ULL, (u64bit) 0x0202000200000200ULL, (u64bit) 0x0202000000000202ULL, (u64bit) 0x0202000200000202ULL, +(u64bit) 0x0202020000000200ULL, (u64bit) 0x0202020200000200ULL, (u64bit) 0x0202020000000202ULL, (u64bit) 0x0202020200000202ULL, +(u64bit) 0x0200000000020000ULL, (u64bit) 0x0200000200020000ULL, (u64bit) 0x0200000000020002ULL, (u64bit) 0x0200000200020002ULL, +(u64bit) 0x0200020000020000ULL, (u64bit) 0x0200020200020000ULL, (u64bit) 0x0200020000020002ULL, (u64bit) 0x0200020200020002ULL, +(u64bit) 0x0200000000020200ULL, (u64bit) 0x0200000200020200ULL, (u64bit) 0x0200000000020202ULL, (u64bit) 0x0200000200020202ULL, +(u64bit) 0x0200020000020200ULL, (u64bit) 0x0200020200020200ULL, (u64bit) 0x0200020000020202ULL, (u64bit) 0x0200020200020202ULL, +(u64bit) 0x0202000000020000ULL, (u64bit) 0x0202000200020000ULL, (u64bit) 0x0202000000020002ULL, (u64bit) 0x0202000200020002ULL, +(u64bit) 0x0202020000020000ULL, (u64bit) 0x0202020200020000ULL, (u64bit) 0x0202020000020002ULL, (u64bit) 0x0202020200020002ULL, +(u64bit) 0x0202000000020200ULL, (u64bit) 0x0202000200020200ULL, (u64bit) 0x0202000000020202ULL, (u64bit) 0x0202000200020202ULL, +(u64bit) 0x0202020000020200ULL, (u64bit) 0x0202020200020200ULL, (u64bit) 0x0202020000020202ULL, (u64bit) 0x0202020200020202ULL, +(u64bit) 0x0000000002000000ULL, (u64bit) 0x0000000202000000ULL, (u64bit) 0x0000000002000002ULL, (u64bit) 0x0000000202000002ULL, +(u64bit) 0x0000020002000000ULL, (u64bit) 0x0000020202000000ULL, (u64bit) 0x0000020002000002ULL, (u64bit) 0x0000020202000002ULL, +(u64bit) 0x0000000002000200ULL, (u64bit) 0x0000000202000200ULL, (u64bit) 0x0000000002000202ULL, (u64bit) 0x0000000202000202ULL, +(u64bit) 0x0000020002000200ULL, (u64bit) 0x0000020202000200ULL, (u64bit) 0x0000020002000202ULL, (u64bit) 0x0000020202000202ULL, +(u64bit) 0x0002000002000000ULL, (u64bit) 0x0002000202000000ULL, (u64bit) 0x0002000002000002ULL, (u64bit) 0x0002000202000002ULL, +(u64bit) 0x0002020002000000ULL, (u64bit) 0x0002020202000000ULL, (u64bit) 0x0002020002000002ULL, (u64bit) 0x0002020202000002ULL, +(u64bit) 0x0002000002000200ULL, (u64bit) 0x0002000202000200ULL, (u64bit) 0x0002000002000202ULL, (u64bit) 0x0002000202000202ULL, +(u64bit) 0x0002020002000200ULL, (u64bit) 0x0002020202000200ULL, (u64bit) 0x0002020002000202ULL, (u64bit) 0x0002020202000202ULL, +(u64bit) 0x0000000002020000ULL, (u64bit) 0x0000000202020000ULL, (u64bit) 0x0000000002020002ULL, (u64bit) 0x0000000202020002ULL, +(u64bit) 0x0000020002020000ULL, (u64bit) 0x0000020202020000ULL, (u64bit) 0x0000020002020002ULL, (u64bit) 0x0000020202020002ULL, +(u64bit) 0x0000000002020200ULL, (u64bit) 0x0000000202020200ULL, (u64bit) 0x0000000002020202ULL, (u64bit) 0x0000000202020202ULL, +(u64bit) 0x0000020002020200ULL, (u64bit) 0x0000020202020200ULL, (u64bit) 0x0000020002020202ULL, (u64bit) 0x0000020202020202ULL, +(u64bit) 0x0002000002020000ULL, (u64bit) 0x0002000202020000ULL, (u64bit) 0x0002000002020002ULL, (u64bit) 0x0002000202020002ULL, +(u64bit) 0x0002020002020000ULL, (u64bit) 0x0002020202020000ULL, (u64bit) 0x0002020002020002ULL, (u64bit) 0x0002020202020002ULL, +(u64bit) 0x0002000002020200ULL, (u64bit) 0x0002000202020200ULL, (u64bit) 0x0002000002020202ULL, (u64bit) 0x0002000202020202ULL, +(u64bit) 0x0002020002020200ULL, (u64bit) 0x0002020202020200ULL, (u64bit) 0x0002020002020202ULL, (u64bit) 0x0002020202020202ULL, +(u64bit) 0x0200000002000000ULL, (u64bit) 0x0200000202000000ULL, (u64bit) 0x0200000002000002ULL, (u64bit) 0x0200000202000002ULL, +(u64bit) 0x0200020002000000ULL, (u64bit) 0x0200020202000000ULL, (u64bit) 0x0200020002000002ULL, (u64bit) 0x0200020202000002ULL, +(u64bit) 0x0200000002000200ULL, (u64bit) 0x0200000202000200ULL, (u64bit) 0x0200000002000202ULL, (u64bit) 0x0200000202000202ULL, +(u64bit) 0x0200020002000200ULL, (u64bit) 0x0200020202000200ULL, (u64bit) 0x0200020002000202ULL, (u64bit) 0x0200020202000202ULL, +(u64bit) 0x0202000002000000ULL, (u64bit) 0x0202000202000000ULL, (u64bit) 0x0202000002000002ULL, (u64bit) 0x0202000202000002ULL, +(u64bit) 0x0202020002000000ULL, (u64bit) 0x0202020202000000ULL, (u64bit) 0x0202020002000002ULL, (u64bit) 0x0202020202000002ULL, +(u64bit) 0x0202000002000200ULL, (u64bit) 0x0202000202000200ULL, (u64bit) 0x0202000002000202ULL, (u64bit) 0x0202000202000202ULL, +(u64bit) 0x0202020002000200ULL, (u64bit) 0x0202020202000200ULL, (u64bit) 0x0202020002000202ULL, (u64bit) 0x0202020202000202ULL, +(u64bit) 0x0200000002020000ULL, (u64bit) 0x0200000202020000ULL, (u64bit) 0x0200000002020002ULL, (u64bit) 0x0200000202020002ULL, +(u64bit) 0x0200020002020000ULL, (u64bit) 0x0200020202020000ULL, (u64bit) 0x0200020002020002ULL, (u64bit) 0x0200020202020002ULL, +(u64bit) 0x0200000002020200ULL, (u64bit) 0x0200000202020200ULL, (u64bit) 0x0200000002020202ULL, (u64bit) 0x0200000202020202ULL, +(u64bit) 0x0200020002020200ULL, (u64bit) 0x0200020202020200ULL, (u64bit) 0x0200020002020202ULL, (u64bit) 0x0200020202020202ULL, +(u64bit) 0x0202000002020000ULL, (u64bit) 0x0202000202020000ULL, (u64bit) 0x0202000002020002ULL, (u64bit) 0x0202000202020002ULL, +(u64bit) 0x0202020002020000ULL, (u64bit) 0x0202020202020000ULL, (u64bit) 0x0202020002020002ULL, (u64bit) 0x0202020202020002ULL, +(u64bit) 0x0202000002020200ULL, (u64bit) 0x0202000202020200ULL, (u64bit) 0x0202000002020202ULL, (u64bit) 0x0202000202020202ULL, +(u64bit) 0x0202020002020200ULL, (u64bit) 0x0202020202020200ULL, (u64bit) 0x0202020002020202ULL, (u64bit) 0x0202020202020202ULL }; + +const u64bit DES_IPTAB2[256] = { +(u64bit) 0x0000000000000000ULL, (u64bit) 0x0000010000000000ULL, (u64bit) 0x0000000000000100ULL, (u64bit) 0x0000010000000100ULL, +(u64bit) 0x0001000000000000ULL, (u64bit) 0x0001010000000000ULL, (u64bit) 0x0001000000000100ULL, (u64bit) 0x0001010000000100ULL, +(u64bit) 0x0000000000010000ULL, (u64bit) 0x0000010000010000ULL, (u64bit) 0x0000000000010100ULL, (u64bit) 0x0000010000010100ULL, +(u64bit) 0x0001000000010000ULL, (u64bit) 0x0001010000010000ULL, (u64bit) 0x0001000000010100ULL, (u64bit) 0x0001010000010100ULL, +(u64bit) 0x0100000000000000ULL, (u64bit) 0x0100010000000000ULL, (u64bit) 0x0100000000000100ULL, (u64bit) 0x0100010000000100ULL, +(u64bit) 0x0101000000000000ULL, (u64bit) 0x0101010000000000ULL, (u64bit) 0x0101000000000100ULL, (u64bit) 0x0101010000000100ULL, +(u64bit) 0x0100000000010000ULL, (u64bit) 0x0100010000010000ULL, (u64bit) 0x0100000000010100ULL, (u64bit) 0x0100010000010100ULL, +(u64bit) 0x0101000000010000ULL, (u64bit) 0x0101010000010000ULL, (u64bit) 0x0101000000010100ULL, (u64bit) 0x0101010000010100ULL, +(u64bit) 0x0000000001000000ULL, (u64bit) 0x0000010001000000ULL, (u64bit) 0x0000000001000100ULL, (u64bit) 0x0000010001000100ULL, +(u64bit) 0x0001000001000000ULL, (u64bit) 0x0001010001000000ULL, (u64bit) 0x0001000001000100ULL, (u64bit) 0x0001010001000100ULL, +(u64bit) 0x0000000001010000ULL, (u64bit) 0x0000010001010000ULL, (u64bit) 0x0000000001010100ULL, (u64bit) 0x0000010001010100ULL, +(u64bit) 0x0001000001010000ULL, (u64bit) 0x0001010001010000ULL, (u64bit) 0x0001000001010100ULL, (u64bit) 0x0001010001010100ULL, +(u64bit) 0x0100000001000000ULL, (u64bit) 0x0100010001000000ULL, (u64bit) 0x0100000001000100ULL, (u64bit) 0x0100010001000100ULL, +(u64bit) 0x0101000001000000ULL, (u64bit) 0x0101010001000000ULL, (u64bit) 0x0101000001000100ULL, (u64bit) 0x0101010001000100ULL, +(u64bit) 0x0100000001010000ULL, (u64bit) 0x0100010001010000ULL, (u64bit) 0x0100000001010100ULL, (u64bit) 0x0100010001010100ULL, +(u64bit) 0x0101000001010000ULL, (u64bit) 0x0101010001010000ULL, (u64bit) 0x0101000001010100ULL, (u64bit) 0x0101010001010100ULL, +(u64bit) 0x0000000100000000ULL, (u64bit) 0x0000010100000000ULL, (u64bit) 0x0000000100000100ULL, (u64bit) 0x0000010100000100ULL, +(u64bit) 0x0001000100000000ULL, (u64bit) 0x0001010100000000ULL, (u64bit) 0x0001000100000100ULL, (u64bit) 0x0001010100000100ULL, +(u64bit) 0x0000000100010000ULL, (u64bit) 0x0000010100010000ULL, (u64bit) 0x0000000100010100ULL, (u64bit) 0x0000010100010100ULL, +(u64bit) 0x0001000100010000ULL, (u64bit) 0x0001010100010000ULL, (u64bit) 0x0001000100010100ULL, (u64bit) 0x0001010100010100ULL, +(u64bit) 0x0100000100000000ULL, (u64bit) 0x0100010100000000ULL, (u64bit) 0x0100000100000100ULL, (u64bit) 0x0100010100000100ULL, +(u64bit) 0x0101000100000000ULL, (u64bit) 0x0101010100000000ULL, (u64bit) 0x0101000100000100ULL, (u64bit) 0x0101010100000100ULL, +(u64bit) 0x0100000100010000ULL, (u64bit) 0x0100010100010000ULL, (u64bit) 0x0100000100010100ULL, (u64bit) 0x0100010100010100ULL, +(u64bit) 0x0101000100010000ULL, (u64bit) 0x0101010100010000ULL, (u64bit) 0x0101000100010100ULL, (u64bit) 0x0101010100010100ULL, +(u64bit) 0x0000000101000000ULL, (u64bit) 0x0000010101000000ULL, (u64bit) 0x0000000101000100ULL, (u64bit) 0x0000010101000100ULL, +(u64bit) 0x0001000101000000ULL, (u64bit) 0x0001010101000000ULL, (u64bit) 0x0001000101000100ULL, (u64bit) 0x0001010101000100ULL, +(u64bit) 0x0000000101010000ULL, (u64bit) 0x0000010101010000ULL, (u64bit) 0x0000000101010100ULL, (u64bit) 0x0000010101010100ULL, +(u64bit) 0x0001000101010000ULL, (u64bit) 0x0001010101010000ULL, (u64bit) 0x0001000101010100ULL, (u64bit) 0x0001010101010100ULL, +(u64bit) 0x0100000101000000ULL, (u64bit) 0x0100010101000000ULL, (u64bit) 0x0100000101000100ULL, (u64bit) 0x0100010101000100ULL, +(u64bit) 0x0101000101000000ULL, (u64bit) 0x0101010101000000ULL, (u64bit) 0x0101000101000100ULL, (u64bit) 0x0101010101000100ULL, +(u64bit) 0x0100000101010000ULL, (u64bit) 0x0100010101010000ULL, (u64bit) 0x0100000101010100ULL, (u64bit) 0x0100010101010100ULL, +(u64bit) 0x0101000101010000ULL, (u64bit) 0x0101010101010000ULL, (u64bit) 0x0101000101010100ULL, (u64bit) 0x0101010101010100ULL, +(u64bit) 0x0000000000000001ULL, (u64bit) 0x0000010000000001ULL, (u64bit) 0x0000000000000101ULL, (u64bit) 0x0000010000000101ULL, +(u64bit) 0x0001000000000001ULL, (u64bit) 0x0001010000000001ULL, (u64bit) 0x0001000000000101ULL, (u64bit) 0x0001010000000101ULL, +(u64bit) 0x0000000000010001ULL, (u64bit) 0x0000010000010001ULL, (u64bit) 0x0000000000010101ULL, (u64bit) 0x0000010000010101ULL, +(u64bit) 0x0001000000010001ULL, (u64bit) 0x0001010000010001ULL, (u64bit) 0x0001000000010101ULL, (u64bit) 0x0001010000010101ULL, +(u64bit) 0x0100000000000001ULL, (u64bit) 0x0100010000000001ULL, (u64bit) 0x0100000000000101ULL, (u64bit) 0x0100010000000101ULL, +(u64bit) 0x0101000000000001ULL, (u64bit) 0x0101010000000001ULL, (u64bit) 0x0101000000000101ULL, (u64bit) 0x0101010000000101ULL, +(u64bit) 0x0100000000010001ULL, (u64bit) 0x0100010000010001ULL, (u64bit) 0x0100000000010101ULL, (u64bit) 0x0100010000010101ULL, +(u64bit) 0x0101000000010001ULL, (u64bit) 0x0101010000010001ULL, (u64bit) 0x0101000000010101ULL, (u64bit) 0x0101010000010101ULL, +(u64bit) 0x0000000001000001ULL, (u64bit) 0x0000010001000001ULL, (u64bit) 0x0000000001000101ULL, (u64bit) 0x0000010001000101ULL, +(u64bit) 0x0001000001000001ULL, (u64bit) 0x0001010001000001ULL, (u64bit) 0x0001000001000101ULL, (u64bit) 0x0001010001000101ULL, +(u64bit) 0x0000000001010001ULL, (u64bit) 0x0000010001010001ULL, (u64bit) 0x0000000001010101ULL, (u64bit) 0x0000010001010101ULL, +(u64bit) 0x0001000001010001ULL, (u64bit) 0x0001010001010001ULL, (u64bit) 0x0001000001010101ULL, (u64bit) 0x0001010001010101ULL, +(u64bit) 0x0100000001000001ULL, (u64bit) 0x0100010001000001ULL, (u64bit) 0x0100000001000101ULL, (u64bit) 0x0100010001000101ULL, +(u64bit) 0x0101000001000001ULL, (u64bit) 0x0101010001000001ULL, (u64bit) 0x0101000001000101ULL, (u64bit) 0x0101010001000101ULL, +(u64bit) 0x0100000001010001ULL, (u64bit) 0x0100010001010001ULL, (u64bit) 0x0100000001010101ULL, (u64bit) 0x0100010001010101ULL, +(u64bit) 0x0101000001010001ULL, (u64bit) 0x0101010001010001ULL, (u64bit) 0x0101000001010101ULL, (u64bit) 0x0101010001010101ULL, +(u64bit) 0x0000000100000001ULL, (u64bit) 0x0000010100000001ULL, (u64bit) 0x0000000100000101ULL, (u64bit) 0x0000010100000101ULL, +(u64bit) 0x0001000100000001ULL, (u64bit) 0x0001010100000001ULL, (u64bit) 0x0001000100000101ULL, (u64bit) 0x0001010100000101ULL, +(u64bit) 0x0000000100010001ULL, (u64bit) 0x0000010100010001ULL, (u64bit) 0x0000000100010101ULL, (u64bit) 0x0000010100010101ULL, +(u64bit) 0x0001000100010001ULL, (u64bit) 0x0001010100010001ULL, (u64bit) 0x0001000100010101ULL, (u64bit) 0x0001010100010101ULL, +(u64bit) 0x0100000100000001ULL, (u64bit) 0x0100010100000001ULL, (u64bit) 0x0100000100000101ULL, (u64bit) 0x0100010100000101ULL, +(u64bit) 0x0101000100000001ULL, (u64bit) 0x0101010100000001ULL, (u64bit) 0x0101000100000101ULL, (u64bit) 0x0101010100000101ULL, +(u64bit) 0x0100000100010001ULL, (u64bit) 0x0100010100010001ULL, (u64bit) 0x0100000100010101ULL, (u64bit) 0x0100010100010101ULL, +(u64bit) 0x0101000100010001ULL, (u64bit) 0x0101010100010001ULL, (u64bit) 0x0101000100010101ULL, (u64bit) 0x0101010100010101ULL, +(u64bit) 0x0000000101000001ULL, (u64bit) 0x0000010101000001ULL, (u64bit) 0x0000000101000101ULL, (u64bit) 0x0000010101000101ULL, +(u64bit) 0x0001000101000001ULL, (u64bit) 0x0001010101000001ULL, (u64bit) 0x0001000101000101ULL, (u64bit) 0x0001010101000101ULL, +(u64bit) 0x0000000101010001ULL, (u64bit) 0x0000010101010001ULL, (u64bit) 0x0000000101010101ULL, (u64bit) 0x0000010101010101ULL, +(u64bit) 0x0001000101010001ULL, (u64bit) 0x0001010101010001ULL, (u64bit) 0x0001000101010101ULL, (u64bit) 0x0001010101010101ULL, +(u64bit) 0x0100000101000001ULL, (u64bit) 0x0100010101000001ULL, (u64bit) 0x0100000101000101ULL, (u64bit) 0x0100010101000101ULL, +(u64bit) 0x0101000101000001ULL, (u64bit) 0x0101010101000001ULL, (u64bit) 0x0101000101000101ULL, (u64bit) 0x0101010101000101ULL, +(u64bit) 0x0100000101010001ULL, (u64bit) 0x0100010101010001ULL, (u64bit) 0x0100000101010101ULL, (u64bit) 0x0100010101010101ULL, +(u64bit) 0x0101000101010001ULL, (u64bit) 0x0101010101010001ULL, (u64bit) 0x0101000101010101ULL, (u64bit) 0x0101010101010101ULL }; + +const u64bit DES_FPTAB1[256] = { +(u64bit) 0x0000000000000000ULL, (u64bit) 0x0000000100000000ULL, (u64bit) 0x0000000004000000ULL, (u64bit) 0x0000000104000000ULL, +(u64bit) 0x0000000000040000ULL, (u64bit) 0x0000000100040000ULL, (u64bit) 0x0000000004040000ULL, (u64bit) 0x0000000104040000ULL, +(u64bit) 0x0000000000000400ULL, (u64bit) 0x0000000100000400ULL, (u64bit) 0x0000000004000400ULL, (u64bit) 0x0000000104000400ULL, +(u64bit) 0x0000000000040400ULL, (u64bit) 0x0000000100040400ULL, (u64bit) 0x0000000004040400ULL, (u64bit) 0x0000000104040400ULL, +(u64bit) 0x0000000000000004ULL, (u64bit) 0x0000000100000004ULL, (u64bit) 0x0000000004000004ULL, (u64bit) 0x0000000104000004ULL, +(u64bit) 0x0000000000040004ULL, (u64bit) 0x0000000100040004ULL, (u64bit) 0x0000000004040004ULL, (u64bit) 0x0000000104040004ULL, +(u64bit) 0x0000000000000404ULL, (u64bit) 0x0000000100000404ULL, (u64bit) 0x0000000004000404ULL, (u64bit) 0x0000000104000404ULL, +(u64bit) 0x0000000000040404ULL, (u64bit) 0x0000000100040404ULL, (u64bit) 0x0000000004040404ULL, (u64bit) 0x0000000104040404ULL, +(u64bit) 0x0400000000000000ULL, (u64bit) 0x0400000100000000ULL, (u64bit) 0x0400000004000000ULL, (u64bit) 0x0400000104000000ULL, +(u64bit) 0x0400000000040000ULL, (u64bit) 0x0400000100040000ULL, (u64bit) 0x0400000004040000ULL, (u64bit) 0x0400000104040000ULL, +(u64bit) 0x0400000000000400ULL, (u64bit) 0x0400000100000400ULL, (u64bit) 0x0400000004000400ULL, (u64bit) 0x0400000104000400ULL, +(u64bit) 0x0400000000040400ULL, (u64bit) 0x0400000100040400ULL, (u64bit) 0x0400000004040400ULL, (u64bit) 0x0400000104040400ULL, +(u64bit) 0x0400000000000004ULL, (u64bit) 0x0400000100000004ULL, (u64bit) 0x0400000004000004ULL, (u64bit) 0x0400000104000004ULL, +(u64bit) 0x0400000000040004ULL, (u64bit) 0x0400000100040004ULL, (u64bit) 0x0400000004040004ULL, (u64bit) 0x0400000104040004ULL, +(u64bit) 0x0400000000000404ULL, (u64bit) 0x0400000100000404ULL, (u64bit) 0x0400000004000404ULL, (u64bit) 0x0400000104000404ULL, +(u64bit) 0x0400000000040404ULL, (u64bit) 0x0400000100040404ULL, (u64bit) 0x0400000004040404ULL, (u64bit) 0x0400000104040404ULL, +(u64bit) 0x0004000000000000ULL, (u64bit) 0x0004000100000000ULL, (u64bit) 0x0004000004000000ULL, (u64bit) 0x0004000104000000ULL, +(u64bit) 0x0004000000040000ULL, (u64bit) 0x0004000100040000ULL, (u64bit) 0x0004000004040000ULL, (u64bit) 0x0004000104040000ULL, +(u64bit) 0x0004000000000400ULL, (u64bit) 0x0004000100000400ULL, (u64bit) 0x0004000004000400ULL, (u64bit) 0x0004000104000400ULL, +(u64bit) 0x0004000000040400ULL, (u64bit) 0x0004000100040400ULL, (u64bit) 0x0004000004040400ULL, (u64bit) 0x0004000104040400ULL, +(u64bit) 0x0004000000000004ULL, (u64bit) 0x0004000100000004ULL, (u64bit) 0x0004000004000004ULL, (u64bit) 0x0004000104000004ULL, +(u64bit) 0x0004000000040004ULL, (u64bit) 0x0004000100040004ULL, (u64bit) 0x0004000004040004ULL, (u64bit) 0x0004000104040004ULL, +(u64bit) 0x0004000000000404ULL, (u64bit) 0x0004000100000404ULL, (u64bit) 0x0004000004000404ULL, (u64bit) 0x0004000104000404ULL, +(u64bit) 0x0004000000040404ULL, (u64bit) 0x0004000100040404ULL, (u64bit) 0x0004000004040404ULL, (u64bit) 0x0004000104040404ULL, +(u64bit) 0x0404000000000000ULL, (u64bit) 0x0404000100000000ULL, (u64bit) 0x0404000004000000ULL, (u64bit) 0x0404000104000000ULL, +(u64bit) 0x0404000000040000ULL, (u64bit) 0x0404000100040000ULL, (u64bit) 0x0404000004040000ULL, (u64bit) 0x0404000104040000ULL, +(u64bit) 0x0404000000000400ULL, (u64bit) 0x0404000100000400ULL, (u64bit) 0x0404000004000400ULL, (u64bit) 0x0404000104000400ULL, +(u64bit) 0x0404000000040400ULL, (u64bit) 0x0404000100040400ULL, (u64bit) 0x0404000004040400ULL, (u64bit) 0x0404000104040400ULL, +(u64bit) 0x0404000000000004ULL, (u64bit) 0x0404000100000004ULL, (u64bit) 0x0404000004000004ULL, (u64bit) 0x0404000104000004ULL, +(u64bit) 0x0404000000040004ULL, (u64bit) 0x0404000100040004ULL, (u64bit) 0x0404000004040004ULL, (u64bit) 0x0404000104040004ULL, +(u64bit) 0x0404000000000404ULL, (u64bit) 0x0404000100000404ULL, (u64bit) 0x0404000004000404ULL, (u64bit) 0x0404000104000404ULL, +(u64bit) 0x0404000000040404ULL, (u64bit) 0x0404000100040404ULL, (u64bit) 0x0404000004040404ULL, (u64bit) 0x0404000104040404ULL, +(u64bit) 0x0000040000000000ULL, (u64bit) 0x0000040100000000ULL, (u64bit) 0x0000040004000000ULL, (u64bit) 0x0000040104000000ULL, +(u64bit) 0x0000040000040000ULL, (u64bit) 0x0000040100040000ULL, (u64bit) 0x0000040004040000ULL, (u64bit) 0x0000040104040000ULL, +(u64bit) 0x0000040000000400ULL, (u64bit) 0x0000040100000400ULL, (u64bit) 0x0000040004000400ULL, (u64bit) 0x0000040104000400ULL, +(u64bit) 0x0000040000040400ULL, (u64bit) 0x0000040100040400ULL, (u64bit) 0x0000040004040400ULL, (u64bit) 0x0000040104040400ULL, +(u64bit) 0x0000040000000004ULL, (u64bit) 0x0000040100000004ULL, (u64bit) 0x0000040004000004ULL, (u64bit) 0x0000040104000004ULL, +(u64bit) 0x0000040000040004ULL, (u64bit) 0x0000040100040004ULL, (u64bit) 0x0000040004040004ULL, (u64bit) 0x0000040104040004ULL, +(u64bit) 0x0000040000000404ULL, (u64bit) 0x0000040100000404ULL, (u64bit) 0x0000040004000404ULL, (u64bit) 0x0000040104000404ULL, +(u64bit) 0x0000040000040404ULL, (u64bit) 0x0000040100040404ULL, (u64bit) 0x0000040004040404ULL, (u64bit) 0x0000040104040404ULL, +(u64bit) 0x0400040000000000ULL, (u64bit) 0x0400040100000000ULL, (u64bit) 0x0400040004000000ULL, (u64bit) 0x0400040104000000ULL, +(u64bit) 0x0400040000040000ULL, (u64bit) 0x0400040100040000ULL, (u64bit) 0x0400040004040000ULL, (u64bit) 0x0400040104040000ULL, +(u64bit) 0x0400040000000400ULL, (u64bit) 0x0400040100000400ULL, (u64bit) 0x0400040004000400ULL, (u64bit) 0x0400040104000400ULL, +(u64bit) 0x0400040000040400ULL, (u64bit) 0x0400040100040400ULL, (u64bit) 0x0400040004040400ULL, (u64bit) 0x0400040104040400ULL, +(u64bit) 0x0400040000000004ULL, (u64bit) 0x0400040100000004ULL, (u64bit) 0x0400040004000004ULL, (u64bit) 0x0400040104000004ULL, +(u64bit) 0x0400040000040004ULL, (u64bit) 0x0400040100040004ULL, (u64bit) 0x0400040004040004ULL, (u64bit) 0x0400040104040004ULL, +(u64bit) 0x0400040000000404ULL, (u64bit) 0x0400040100000404ULL, (u64bit) 0x0400040004000404ULL, (u64bit) 0x0400040104000404ULL, +(u64bit) 0x0400040000040404ULL, (u64bit) 0x0400040100040404ULL, (u64bit) 0x0400040004040404ULL, (u64bit) 0x0400040104040404ULL, +(u64bit) 0x0004040000000000ULL, (u64bit) 0x0004040100000000ULL, (u64bit) 0x0004040004000000ULL, (u64bit) 0x0004040104000000ULL, +(u64bit) 0x0004040000040000ULL, (u64bit) 0x0004040100040000ULL, (u64bit) 0x0004040004040000ULL, (u64bit) 0x0004040104040000ULL, +(u64bit) 0x0004040000000400ULL, (u64bit) 0x0004040100000400ULL, (u64bit) 0x0004040004000400ULL, (u64bit) 0x0004040104000400ULL, +(u64bit) 0x0004040000040400ULL, (u64bit) 0x0004040100040400ULL, (u64bit) 0x0004040004040400ULL, (u64bit) 0x0004040104040400ULL, +(u64bit) 0x0004040000000004ULL, (u64bit) 0x0004040100000004ULL, (u64bit) 0x0004040004000004ULL, (u64bit) 0x0004040104000004ULL, +(u64bit) 0x0004040000040004ULL, (u64bit) 0x0004040100040004ULL, (u64bit) 0x0004040004040004ULL, (u64bit) 0x0004040104040004ULL, +(u64bit) 0x0004040000000404ULL, (u64bit) 0x0004040100000404ULL, (u64bit) 0x0004040004000404ULL, (u64bit) 0x0004040104000404ULL, +(u64bit) 0x0004040000040404ULL, (u64bit) 0x0004040100040404ULL, (u64bit) 0x0004040004040404ULL, (u64bit) 0x0004040104040404ULL, +(u64bit) 0x0404040000000000ULL, (u64bit) 0x0404040100000000ULL, (u64bit) 0x0404040004000000ULL, (u64bit) 0x0404040104000000ULL, +(u64bit) 0x0404040000040000ULL, (u64bit) 0x0404040100040000ULL, (u64bit) 0x0404040004040000ULL, (u64bit) 0x0404040104040000ULL, +(u64bit) 0x0404040000000400ULL, (u64bit) 0x0404040100000400ULL, (u64bit) 0x0404040004000400ULL, (u64bit) 0x0404040104000400ULL, +(u64bit) 0x0404040000040400ULL, (u64bit) 0x0404040100040400ULL, (u64bit) 0x0404040004040400ULL, (u64bit) 0x0404040104040400ULL, +(u64bit) 0x0404040000000004ULL, (u64bit) 0x0404040100000004ULL, (u64bit) 0x0404040004000004ULL, (u64bit) 0x0404040104000004ULL, +(u64bit) 0x0404040000040004ULL, (u64bit) 0x0404040100040004ULL, (u64bit) 0x0404040004040004ULL, (u64bit) 0x0404040104040004ULL, +(u64bit) 0x0404040000000404ULL, (u64bit) 0x0404040100000404ULL, (u64bit) 0x0404040004000404ULL, (u64bit) 0x0404040104000404ULL, +(u64bit) 0x0404040000040404ULL, (u64bit) 0x0404040100040404ULL, (u64bit) 0x0404040004040404ULL, (u64bit) 0x0404040104040404ULL }; + +const u64bit DES_FPTAB2[256] = { +(u64bit) 0x0000000000000000ULL, (u64bit) 0x0000004000000000ULL, (u64bit) 0x0000000001000000ULL, (u64bit) 0x0000004001000000ULL, +(u64bit) 0x0000000000010000ULL, (u64bit) 0x0000004000010000ULL, (u64bit) 0x0000000001010000ULL, (u64bit) 0x0000004001010000ULL, +(u64bit) 0x0000000000000100ULL, (u64bit) 0x0000004000000100ULL, (u64bit) 0x0000000001000100ULL, (u64bit) 0x0000004001000100ULL, +(u64bit) 0x0000000000010100ULL, (u64bit) 0x0000004000010100ULL, (u64bit) 0x0000000001010100ULL, (u64bit) 0x0000004001010100ULL, +(u64bit) 0x0000000000000001ULL, (u64bit) 0x0000004000000001ULL, (u64bit) 0x0000000001000001ULL, (u64bit) 0x0000004001000001ULL, +(u64bit) 0x0000000000010001ULL, (u64bit) 0x0000004000010001ULL, (u64bit) 0x0000000001010001ULL, (u64bit) 0x0000004001010001ULL, +(u64bit) 0x0000000000000101ULL, (u64bit) 0x0000004000000101ULL, (u64bit) 0x0000000001000101ULL, (u64bit) 0x0000004001000101ULL, +(u64bit) 0x0000000000010101ULL, (u64bit) 0x0000004000010101ULL, (u64bit) 0x0000000001010101ULL, (u64bit) 0x0000004001010101ULL, +(u64bit) 0x0100000000000000ULL, (u64bit) 0x0100004000000000ULL, (u64bit) 0x0100000001000000ULL, (u64bit) 0x0100004001000000ULL, +(u64bit) 0x0100000000010000ULL, (u64bit) 0x0100004000010000ULL, (u64bit) 0x0100000001010000ULL, (u64bit) 0x0100004001010000ULL, +(u64bit) 0x0100000000000100ULL, (u64bit) 0x0100004000000100ULL, (u64bit) 0x0100000001000100ULL, (u64bit) 0x0100004001000100ULL, +(u64bit) 0x0100000000010100ULL, (u64bit) 0x0100004000010100ULL, (u64bit) 0x0100000001010100ULL, (u64bit) 0x0100004001010100ULL, +(u64bit) 0x0100000000000001ULL, (u64bit) 0x0100004000000001ULL, (u64bit) 0x0100000001000001ULL, (u64bit) 0x0100004001000001ULL, +(u64bit) 0x0100000000010001ULL, (u64bit) 0x0100004000010001ULL, (u64bit) 0x0100000001010001ULL, (u64bit) 0x0100004001010001ULL, +(u64bit) 0x0100000000000101ULL, (u64bit) 0x0100004000000101ULL, (u64bit) 0x0100000001000101ULL, (u64bit) 0x0100004001000101ULL, +(u64bit) 0x0100000000010101ULL, (u64bit) 0x0100004000010101ULL, (u64bit) 0x0100000001010101ULL, (u64bit) 0x0100004001010101ULL, +(u64bit) 0x0001000000000000ULL, (u64bit) 0x0001004000000000ULL, (u64bit) 0x0001000001000000ULL, (u64bit) 0x0001004001000000ULL, +(u64bit) 0x0001000000010000ULL, (u64bit) 0x0001004000010000ULL, (u64bit) 0x0001000001010000ULL, (u64bit) 0x0001004001010000ULL, +(u64bit) 0x0001000000000100ULL, (u64bit) 0x0001004000000100ULL, (u64bit) 0x0001000001000100ULL, (u64bit) 0x0001004001000100ULL, +(u64bit) 0x0001000000010100ULL, (u64bit) 0x0001004000010100ULL, (u64bit) 0x0001000001010100ULL, (u64bit) 0x0001004001010100ULL, +(u64bit) 0x0001000000000001ULL, (u64bit) 0x0001004000000001ULL, (u64bit) 0x0001000001000001ULL, (u64bit) 0x0001004001000001ULL, +(u64bit) 0x0001000000010001ULL, (u64bit) 0x0001004000010001ULL, (u64bit) 0x0001000001010001ULL, (u64bit) 0x0001004001010001ULL, +(u64bit) 0x0001000000000101ULL, (u64bit) 0x0001004000000101ULL, (u64bit) 0x0001000001000101ULL, (u64bit) 0x0001004001000101ULL, +(u64bit) 0x0001000000010101ULL, (u64bit) 0x0001004000010101ULL, (u64bit) 0x0001000001010101ULL, (u64bit) 0x0001004001010101ULL, +(u64bit) 0x0101000000000000ULL, (u64bit) 0x0101004000000000ULL, (u64bit) 0x0101000001000000ULL, (u64bit) 0x0101004001000000ULL, +(u64bit) 0x0101000000010000ULL, (u64bit) 0x0101004000010000ULL, (u64bit) 0x0101000001010000ULL, (u64bit) 0x0101004001010000ULL, +(u64bit) 0x0101000000000100ULL, (u64bit) 0x0101004000000100ULL, (u64bit) 0x0101000001000100ULL, (u64bit) 0x0101004001000100ULL, +(u64bit) 0x0101000000010100ULL, (u64bit) 0x0101004000010100ULL, (u64bit) 0x0101000001010100ULL, (u64bit) 0x0101004001010100ULL, +(u64bit) 0x0101000000000001ULL, (u64bit) 0x0101004000000001ULL, (u64bit) 0x0101000001000001ULL, (u64bit) 0x0101004001000001ULL, +(u64bit) 0x0101000000010001ULL, (u64bit) 0x0101004000010001ULL, (u64bit) 0x0101000001010001ULL, (u64bit) 0x0101004001010001ULL, +(u64bit) 0x0101000000000101ULL, (u64bit) 0x0101004000000101ULL, (u64bit) 0x0101000001000101ULL, (u64bit) 0x0101004001000101ULL, +(u64bit) 0x0101000000010101ULL, (u64bit) 0x0101004000010101ULL, (u64bit) 0x0101000001010101ULL, (u64bit) 0x0101004001010101ULL, +(u64bit) 0x0000010000000000ULL, (u64bit) 0x0000014000000000ULL, (u64bit) 0x0000010001000000ULL, (u64bit) 0x0000014001000000ULL, +(u64bit) 0x0000010000010000ULL, (u64bit) 0x0000014000010000ULL, (u64bit) 0x0000010001010000ULL, (u64bit) 0x0000014001010000ULL, +(u64bit) 0x0000010000000100ULL, (u64bit) 0x0000014000000100ULL, (u64bit) 0x0000010001000100ULL, (u64bit) 0x0000014001000100ULL, +(u64bit) 0x0000010000010100ULL, (u64bit) 0x0000014000010100ULL, (u64bit) 0x0000010001010100ULL, (u64bit) 0x0000014001010100ULL, +(u64bit) 0x0000010000000001ULL, (u64bit) 0x0000014000000001ULL, (u64bit) 0x0000010001000001ULL, (u64bit) 0x0000014001000001ULL, +(u64bit) 0x0000010000010001ULL, (u64bit) 0x0000014000010001ULL, (u64bit) 0x0000010001010001ULL, (u64bit) 0x0000014001010001ULL, +(u64bit) 0x0000010000000101ULL, (u64bit) 0x0000014000000101ULL, (u64bit) 0x0000010001000101ULL, (u64bit) 0x0000014001000101ULL, +(u64bit) 0x0000010000010101ULL, (u64bit) 0x0000014000010101ULL, (u64bit) 0x0000010001010101ULL, (u64bit) 0x0000014001010101ULL, +(u64bit) 0x0100010000000000ULL, (u64bit) 0x0100014000000000ULL, (u64bit) 0x0100010001000000ULL, (u64bit) 0x0100014001000000ULL, +(u64bit) 0x0100010000010000ULL, (u64bit) 0x0100014000010000ULL, (u64bit) 0x0100010001010000ULL, (u64bit) 0x0100014001010000ULL, +(u64bit) 0x0100010000000100ULL, (u64bit) 0x0100014000000100ULL, (u64bit) 0x0100010001000100ULL, (u64bit) 0x0100014001000100ULL, +(u64bit) 0x0100010000010100ULL, (u64bit) 0x0100014000010100ULL, (u64bit) 0x0100010001010100ULL, (u64bit) 0x0100014001010100ULL, +(u64bit) 0x0100010000000001ULL, (u64bit) 0x0100014000000001ULL, (u64bit) 0x0100010001000001ULL, (u64bit) 0x0100014001000001ULL, +(u64bit) 0x0100010000010001ULL, (u64bit) 0x0100014000010001ULL, (u64bit) 0x0100010001010001ULL, (u64bit) 0x0100014001010001ULL, +(u64bit) 0x0100010000000101ULL, (u64bit) 0x0100014000000101ULL, (u64bit) 0x0100010001000101ULL, (u64bit) 0x0100014001000101ULL, +(u64bit) 0x0100010000010101ULL, (u64bit) 0x0100014000010101ULL, (u64bit) 0x0100010001010101ULL, (u64bit) 0x0100014001010101ULL, +(u64bit) 0x0001010000000000ULL, (u64bit) 0x0001014000000000ULL, (u64bit) 0x0001010001000000ULL, (u64bit) 0x0001014001000000ULL, +(u64bit) 0x0001010000010000ULL, (u64bit) 0x0001014000010000ULL, (u64bit) 0x0001010001010000ULL, (u64bit) 0x0001014001010000ULL, +(u64bit) 0x0001010000000100ULL, (u64bit) 0x0001014000000100ULL, (u64bit) 0x0001010001000100ULL, (u64bit) 0x0001014001000100ULL, +(u64bit) 0x0001010000010100ULL, (u64bit) 0x0001014000010100ULL, (u64bit) 0x0001010001010100ULL, (u64bit) 0x0001014001010100ULL, +(u64bit) 0x0001010000000001ULL, (u64bit) 0x0001014000000001ULL, (u64bit) 0x0001010001000001ULL, (u64bit) 0x0001014001000001ULL, +(u64bit) 0x0001010000010001ULL, (u64bit) 0x0001014000010001ULL, (u64bit) 0x0001010001010001ULL, (u64bit) 0x0001014001010001ULL, +(u64bit) 0x0001010000000101ULL, (u64bit) 0x0001014000000101ULL, (u64bit) 0x0001010001000101ULL, (u64bit) 0x0001014001000101ULL, +(u64bit) 0x0001010000010101ULL, (u64bit) 0x0001014000010101ULL, (u64bit) 0x0001010001010101ULL, (u64bit) 0x0001014001010101ULL, +(u64bit) 0x0101010000000000ULL, (u64bit) 0x0101014000000000ULL, (u64bit) 0x0101010001000000ULL, (u64bit) 0x0101014001000000ULL, +(u64bit) 0x0101010000010000ULL, (u64bit) 0x0101014000010000ULL, (u64bit) 0x0101010001010000ULL, (u64bit) 0x0101014001010000ULL, +(u64bit) 0x0101010000000100ULL, (u64bit) 0x0101014000000100ULL, (u64bit) 0x0101010001000100ULL, (u64bit) 0x0101014001000100ULL, +(u64bit) 0x0101010000010100ULL, (u64bit) 0x0101014000010100ULL, (u64bit) 0x0101010001010100ULL, (u64bit) 0x0101014001010100ULL, +(u64bit) 0x0101010000000001ULL, (u64bit) 0x0101014000000001ULL, (u64bit) 0x0101010001000001ULL, (u64bit) 0x0101014001000001ULL, +(u64bit) 0x0101010000010001ULL, (u64bit) 0x0101014000010001ULL, (u64bit) 0x0101010001010001ULL, (u64bit) 0x0101014001010001ULL, +(u64bit) 0x0101010000000101ULL, (u64bit) 0x0101014000000101ULL, (u64bit) 0x0101010001000101ULL, (u64bit) 0x0101014001000101ULL, +(u64bit) 0x0101010000010101ULL, (u64bit) 0x0101014000010101ULL, (u64bit) 0x0101010001010101ULL, (u64bit) 0x0101014001010101ULL }; + +} diff --git a/botan/src/block/des/desx.cpp b/botan/src/block/des/desx.cpp new file mode 100644 index 0000000..e557901 --- /dev/null +++ b/botan/src/block/des/desx.cpp @@ -0,0 +1,43 @@ +/* +* DES +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/desx.h> +#include <botan/xor_buf.h> + +namespace Botan { + +/* +* DESX Encryption +*/ +void DESX::enc(const byte in[], byte out[]) const + { + xor_buf(out, in, K1.begin(), BLOCK_SIZE); + des.encrypt(out); + xor_buf(out, K2.begin(), BLOCK_SIZE); + } + +/* +* DESX Decryption +*/ +void DESX::dec(const byte in[], byte out[]) const + { + xor_buf(out, in, K2.begin(), BLOCK_SIZE); + des.decrypt(out); + xor_buf(out, K1.begin(), BLOCK_SIZE); + } + +/* +* DESX Key Schedule +*/ +void DESX::key_schedule(const byte key[], u32bit) + { + K1.copy(key, 8); + des.set_key(key + 8, 8); + K2.copy(key + 16, 8); + } + +} diff --git a/botan/src/block/des/desx.h b/botan/src/block/des/desx.h new file mode 100644 index 0000000..49ecc24 --- /dev/null +++ b/botan/src/block/des/desx.h @@ -0,0 +1,35 @@ +/* +* DESX +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_DESX_H__ +#define BOTAN_DESX_H__ + +#include <botan/des.h> + +namespace Botan { + +/* +* DESX +*/ +class BOTAN_DLL DESX : public BlockCipher + { + public: + void clear() throw() { des.clear(); K1.clear(); K2.clear(); } + std::string name() const { return "DESX"; } + BlockCipher* clone() const { return new DESX; } + DESX() : BlockCipher(8, 24) {} + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key_schedule(const byte[], u32bit); + SecureBuffer<byte, 8> K1, K2; + DES des; + }; + +} + +#endif diff --git a/botan/src/block/des/info.txt b/botan/src/block/des/info.txt new file mode 100644 index 0000000..ed05979 --- /dev/null +++ b/botan/src/block/des/info.txt @@ -0,0 +1,13 @@ +realname "DES" + +define DES + +load_on auto + +<add> +des.cpp +des.h +des_tab.cpp +desx.h +desx.cpp +</add> diff --git a/botan/src/block/gost_28147/gost_28147.cpp b/botan/src/block/gost_28147/gost_28147.cpp new file mode 100644 index 0000000..bfd092c --- /dev/null +++ b/botan/src/block/gost_28147/gost_28147.cpp @@ -0,0 +1,139 @@ +/* +* GOST 28147-89 +* (C) 1999-2009 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/gost_28147.h> +#include <botan/loadstor.h> + +namespace Botan { + +byte GOST_28147_89_Params::sbox_entry(u32bit row, u32bit col) const + { + byte x = sboxes[4 * col + (row / 2)]; + + return (row % 2 == 0) ? (x >> 4) : (x & 0x0F); + } + +GOST_28147_89_Params::GOST_28147_89_Params(const std::string& n) : name(n) + { + // Encoded in the packed fromat from RFC 4357 + + // GostR3411_94_TestParamSet (OID 1.2.643.2.2.31.0) + static const byte GOST_R_3411_TEST_PARAMS[64] = { + 0x4E, 0x57, 0x64, 0xD1, 0xAB, 0x8D, 0xCB, 0xBF, 0x94, 0x1A, 0x7A, + 0x4D, 0x2C, 0xD1, 0x10, 0x10, 0xD6, 0xA0, 0x57, 0x35, 0x8D, 0x38, + 0xF2, 0xF7, 0x0F, 0x49, 0xD1, 0x5A, 0xEA, 0x2F, 0x8D, 0x94, 0x62, + 0xEE, 0x43, 0x09, 0xB3, 0xF4, 0xA6, 0xA2, 0x18, 0xC6, 0x98, 0xE3, + 0xC1, 0x7C, 0xE5, 0x7E, 0x70, 0x6B, 0x09, 0x66, 0xF7, 0x02, 0x3C, + 0x8B, 0x55, 0x95, 0xBF, 0x28, 0x39, 0xB3, 0x2E, 0xCC }; + + // GostR3411-94-CryptoProParamSet (OID 1.2.643.2.2.31.1) + static const byte GOST_R_3411_CRYPTOPRO_PARAMS[64] = { + 0xA5, 0x74, 0x77, 0xD1, 0x4F, 0xFA, 0x66, 0xE3, 0x54, 0xC7, 0x42, + 0x4A, 0x60, 0xEC, 0xB4, 0x19, 0x82, 0x90, 0x9D, 0x75, 0x1D, 0x4F, + 0xC9, 0x0B, 0x3B, 0x12, 0x2F, 0x54, 0x79, 0x08, 0xA0, 0xAF, 0xD1, + 0x3E, 0x1A, 0x38, 0xC7, 0xB1, 0x81, 0xC6, 0xE6, 0x56, 0x05, 0x87, + 0x03, 0x25, 0xEB, 0xFE, 0x9C, 0x6D, 0xF8, 0x6D, 0x2E, 0xAB, 0xDE, + 0x20, 0xBA, 0x89, 0x3C, 0x92, 0xF8, 0xD3, 0x53, 0xBC }; + + if(name == "R3411_94_TestParam") + sboxes = GOST_R_3411_TEST_PARAMS; + else if(name == "R3411_CryptoPro") + sboxes = GOST_R_3411_CRYPTOPRO_PARAMS; + else + throw Invalid_Argument("GOST_28147_89_Params: Unknown " + name); + } + +/* +* GOST Constructor +*/ +GOST_28147_89::GOST_28147_89(const GOST_28147_89_Params& param) : + BlockCipher(8, 32) + { + // Convert the parallel 4x4 sboxes into larger word-based sboxes + for(size_t i = 0; i != 4; ++i) + for(size_t j = 0; j != 256; ++j) + { + u32bit T = (param.sbox_entry(2*i , j % 16)) | + (param.sbox_entry(2*i+1, j / 16) << 4); + SBOX[256*i+j] = rotate_left(T, (11+8*i) % 32); + } + } + +/* +* Two rounds of GOST +*/ +#define GOST_2ROUND(N1, N2, R1, R2) \ + do { \ + u32bit T0 = N1 + EK[R1]; \ + N2 ^= SBOX[get_byte(3, T0)] | \ + SBOX[get_byte(2, T0)+256] | \ + SBOX[get_byte(1, T0)+512] | \ + SBOX[get_byte(0, T0)+768]; \ + \ + u32bit T1 = N2 + EK[R2]; \ + N1 ^= SBOX[get_byte(3, T1)] | \ + SBOX[get_byte(2, T1)+256] | \ + SBOX[get_byte(1, T1)+512] | \ + SBOX[get_byte(0, T1)+768]; \ + } while(0) + +/* +* GOST Encryption +*/ +void GOST_28147_89::enc(const byte in[], byte out[]) const + { + u32bit N1 = load_le<u32bit>(in, 0), N2 = load_le<u32bit>(in, 1); + + for(size_t i = 0; i != 3; ++i) + { + GOST_2ROUND(N1, N2, 0, 1); + GOST_2ROUND(N1, N2, 2, 3); + GOST_2ROUND(N1, N2, 4, 5); + GOST_2ROUND(N1, N2, 6, 7); + } + + GOST_2ROUND(N1, N2, 7, 6); + GOST_2ROUND(N1, N2, 5, 4); + GOST_2ROUND(N1, N2, 3, 2); + GOST_2ROUND(N1, N2, 1, 0); + + store_le(out, N2, N1); + } + +/* +* GOST Decryption +*/ +void GOST_28147_89::dec(const byte in[], byte out[]) const + { + u32bit N1 = load_le<u32bit>(in, 0), N2 = load_le<u32bit>(in, 1); + + GOST_2ROUND(N1, N2, 0, 1); + GOST_2ROUND(N1, N2, 2, 3); + GOST_2ROUND(N1, N2, 4, 5); + GOST_2ROUND(N1, N2, 6, 7); + + for(size_t i = 0; i != 3; ++i) + { + GOST_2ROUND(N1, N2, 7, 6); + GOST_2ROUND(N1, N2, 5, 4); + GOST_2ROUND(N1, N2, 3, 2); + GOST_2ROUND(N1, N2, 1, 0); + } + + store_le(out, N2, N1); + } + +/* +* GOST Key Schedule +*/ +void GOST_28147_89::key_schedule(const byte key[], u32bit) + { + for(u32bit j = 0; j != 8; ++j) + EK[j] = load_le<u32bit>(key, j); + } + +} diff --git a/botan/src/block/gost_28147/gost_28147.h b/botan/src/block/gost_28147/gost_28147.h new file mode 100644 index 0000000..96d24c6 --- /dev/null +++ b/botan/src/block/gost_28147/gost_28147.h @@ -0,0 +1,67 @@ +/* +* GOST 28147-89 +* (C) 1999-2009 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_GOST_28147_89_H__ +#define BOTAN_GOST_28147_89_H__ + +#include <botan/block_cipher.h> + +namespace Botan { + +class GOST_28147_89_Params; + +/** +* The GOST 28147-89 block cipher uses a set of 4 bit Sboxes, however +* the standard does not actually define these Sboxes; they are +* considered a local configuration issue. Several different sets are +* used. +*/ +class GOST_28147_89_Params + { + public: + byte sbox_entry(u32bit row, u32bit col) const; + + std::string param_name() const { return name; } + + /** + * Default GOST parameters are the ones given in GOST R 34.11 for + * testing purposes; these sboxes are also used by Crypto++, and, + * at least according to Wikipedia, the Central Bank of Russian Federation + */ + GOST_28147_89_Params(const std::string& name = "R3411_94_TestParam"); + private: + const byte* sboxes; + std::string name; + }; + +/** +* GOST 28147-89 +*/ +class BOTAN_DLL GOST_28147_89 : public BlockCipher + { + public: + void clear() throw() { EK.clear(); } + + std::string name() const { return "GOST-28147-89"; } + BlockCipher* clone() const { return new GOST_28147_89(SBOX); } + + GOST_28147_89(const GOST_28147_89_Params& params); + private: + GOST_28147_89(const SecureBuffer<u32bit, 1024>& other_SBOX) : + BlockCipher(8, 32), SBOX(other_SBOX) {} + + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key_schedule(const byte[], u32bit); + + SecureBuffer<u32bit, 1024> SBOX; + SecureBuffer<u32bit, 8> EK; + }; + +} + +#endif diff --git a/botan/src/block/gost_28147/info.txt b/botan/src/block/gost_28147/info.txt new file mode 100644 index 0000000..6e187fd --- /dev/null +++ b/botan/src/block/gost_28147/info.txt @@ -0,0 +1,10 @@ +realname "GOST 28147-89" + +define GOST_28147_89 + +load_on auto + +<add> +gost_28147.cpp +gost_28147.h +</add> diff --git a/botan/src/block/idea/idea.cpp b/botan/src/block/idea/idea.cpp new file mode 100644 index 0000000..5bbe470 --- /dev/null +++ b/botan/src/block/idea/idea.cpp @@ -0,0 +1,174 @@ +/* +* IDEA +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/idea.h> +#include <botan/loadstor.h> + +namespace Botan { + +namespace { + +/* +* Multiplication modulo 65537 +*/ +inline u16bit mul(u16bit x, u16bit y) + { + if(x && y) + { + u32bit T = static_cast<u32bit>(x) * y; + x = static_cast<u16bit>(T >> 16); + y = static_cast<u16bit>(T & 0xFFFF); + return static_cast<u16bit>(y - x + ((y < x) ? 1 : 0)); + } + else + return static_cast<u16bit>(1 - x - y); + } + +/* +* Find multiplicative inverses modulo 65537 +*/ +u16bit mul_inv(u16bit x) + { + if(x <= 1) + return x; + + u16bit t0 = static_cast<u16bit>(65537 / x), t1 = 1; + u16bit y = static_cast<u16bit>(65537 % x); + + while(y != 1) + { + u16bit q = x / y; + x %= y; + t1 += q * t0; + + if(x == 1) + return t1; + + q = y / x; + y %= x; + t0 += q * t1; + } + return (1 - t0); + } + +} + +/* +* IDEA Encryption +*/ +void IDEA::enc(const byte in[], byte out[]) const + { + u16bit X1 = load_be<u16bit>(in, 0); + u16bit X2 = load_be<u16bit>(in, 1); + u16bit X3 = load_be<u16bit>(in, 2); + u16bit X4 = load_be<u16bit>(in, 3); + + for(u32bit j = 0; j != 8; ++j) + { + X1 = mul(X1, EK[6*j+0]); + X2 += EK[6*j+1]; + X3 += EK[6*j+2]; + X4 = mul(X4, EK[6*j+3]); + + u16bit T0 = X3; + X3 = mul(X3 ^ X1, EK[6*j+4]); + + u16bit T1 = X2; + X2 = mul((X2 ^ X4) + X3, EK[6*j+5]); + X3 += X2; + + X1 ^= X2; + X4 ^= X3; + X2 ^= T0; + X3 ^= T1; + } + + X1 = mul(X1, EK[48]); + X2 += EK[50]; + X3 += EK[49]; + X4 = mul(X4, EK[51]); + + store_be(out, X1, X3, X2, X4); + } + +/* +* IDEA Decryption +*/ +void IDEA::dec(const byte in[], byte out[]) const + { + u16bit X1 = load_be<u16bit>(in, 0); + u16bit X2 = load_be<u16bit>(in, 1); + u16bit X3 = load_be<u16bit>(in, 2); + u16bit X4 = load_be<u16bit>(in, 3); + + for(u32bit j = 0; j != 8; ++j) + { + X1 = mul(X1, DK[6*j+0]); + X2 += DK[6*j+1]; + X3 += DK[6*j+2]; + X4 = mul(X4, DK[6*j+3]); + + u16bit T0 = X3; + X3 = mul(X3 ^ X1, DK[6*j+4]); + + u16bit T1 = X2; + X2 = mul((X2 ^ X4) + X3, DK[6*j+5]); + X3 += X2; + + X1 ^= X2; + X4 ^= X3; + X2 ^= T0; + X3 ^= T1; + } + + X1 = mul(X1, DK[48]); + X2 += DK[50]; + X3 += DK[49]; + X4 = mul(X4, DK[51]); + + store_be(out, X1, X3, X2, X4); + } + +/* +* IDEA Key Schedule +*/ +void IDEA::key_schedule(const byte key[], u32bit) + { + for(u32bit j = 0; j != 8; ++j) + EK[j] = load_be<u16bit>(key, j); + + for(u32bit j = 1, k = 8, offset = 0; k != 52; j %= 8, ++j, ++k) + { + EK[j+7+offset] = static_cast<u16bit>((EK[(j % 8) + offset] << 9) | + (EK[((j+1) % 8) + offset] >> 7)); + offset += (j == 8) ? 8 : 0; + } + + DK[51] = mul_inv(EK[3]); + DK[50] = -EK[2]; + DK[49] = -EK[1]; + DK[48] = mul_inv(EK[0]); + + for(u32bit j = 1, k = 4, counter = 47; j != 8; ++j, k += 6) + { + DK[counter--] = EK[k+1]; + DK[counter--] = EK[k]; + DK[counter--] = mul_inv(EK[k+5]); + DK[counter--] = -EK[k+3]; + DK[counter--] = -EK[k+4]; + DK[counter--] = mul_inv(EK[k+2]); + } + + DK[5] = EK[47]; + DK[4] = EK[46]; + DK[3] = mul_inv(EK[51]); + DK[2] = -EK[50]; + DK[1] = -EK[49]; + DK[0] = mul_inv(EK[48]); + } + +} diff --git a/botan/src/block/idea/idea.h b/botan/src/block/idea/idea.h new file mode 100644 index 0000000..2c53cd0 --- /dev/null +++ b/botan/src/block/idea/idea.h @@ -0,0 +1,34 @@ +/* +* IDEA +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_IDEA_H__ +#define BOTAN_IDEA_H__ + +#include <botan/block_cipher.h> + +namespace Botan { + +/* +* IDEA +*/ +class BOTAN_DLL IDEA : public BlockCipher + { + public: + void clear() throw() { EK.clear(); DK.clear(); } + std::string name() const { return "IDEA"; } + BlockCipher* clone() const { return new IDEA; } + IDEA() : BlockCipher(8, 16) {} + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key_schedule(const byte[], u32bit); + SecureBuffer<u16bit, 52> EK, DK; + }; + +} + +#endif diff --git a/botan/src/block/idea/info.txt b/botan/src/block/idea/info.txt new file mode 100644 index 0000000..2da3ffb --- /dev/null +++ b/botan/src/block/idea/info.txt @@ -0,0 +1,10 @@ +realname "IDEA" + +define IDEA + +load_on auto + +<add> +idea.cpp +idea.h +</add> diff --git a/botan/src/block/info.txt b/botan/src/block/info.txt new file mode 100644 index 0000000..ff48fb0 --- /dev/null +++ b/botan/src/block/info.txt @@ -0,0 +1,13 @@ +realname "Block Ciphers" + +load_on auto + +define BLOCK_CIPHER + +<add> +block_cipher.h +</add> + +<requires> +sym_algo +</requires> diff --git a/botan/src/block/kasumi/info.txt b/botan/src/block/kasumi/info.txt new file mode 100644 index 0000000..8ea879b --- /dev/null +++ b/botan/src/block/kasumi/info.txt @@ -0,0 +1,10 @@ +realname "Kasumi" + +define KASUMI + +load_on auto + +<add> +kasumi.cpp +kasumi.h +</add> diff --git a/botan/src/block/kasumi/kasumi.cpp b/botan/src/block/kasumi/kasumi.cpp new file mode 100644 index 0000000..e051dde --- /dev/null +++ b/botan/src/block/kasumi/kasumi.cpp @@ -0,0 +1,215 @@ +/* +* KASUMI +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/kasumi.h> +#include <botan/loadstor.h> +#include <botan/rotate.h> + +namespace Botan { + +namespace { + +/* +* KASUMI S-Boxes +*/ +const byte KASUMI_SBOX_S7[128] = { + 0x36, 0x32, 0x3E, 0x38, 0x16, 0x22, 0x5E, 0x60, 0x26, 0x06, 0x3F, 0x5D, + 0x02, 0x12, 0x7B, 0x21, 0x37, 0x71, 0x27, 0x72, 0x15, 0x43, 0x41, 0x0C, + 0x2F, 0x49, 0x2E, 0x1B, 0x19, 0x6F, 0x7C, 0x51, 0x35, 0x09, 0x79, 0x4F, + 0x34, 0x3C, 0x3A, 0x30, 0x65, 0x7F, 0x28, 0x78, 0x68, 0x46, 0x47, 0x2B, + 0x14, 0x7A, 0x48, 0x3D, 0x17, 0x6D, 0x0D, 0x64, 0x4D, 0x01, 0x10, 0x07, + 0x52, 0x0A, 0x69, 0x62, 0x75, 0x74, 0x4C, 0x0B, 0x59, 0x6A, 0x00, 0x7D, + 0x76, 0x63, 0x56, 0x45, 0x1E, 0x39, 0x7E, 0x57, 0x70, 0x33, 0x11, 0x05, + 0x5F, 0x0E, 0x5A, 0x54, 0x5B, 0x08, 0x23, 0x67, 0x20, 0x61, 0x1C, 0x42, + 0x66, 0x1F, 0x1A, 0x2D, 0x4B, 0x04, 0x55, 0x5C, 0x25, 0x4A, 0x50, 0x31, + 0x44, 0x1D, 0x73, 0x2C, 0x40, 0x6B, 0x6C, 0x18, 0x6E, 0x53, 0x24, 0x4E, + 0x2A, 0x13, 0x0F, 0x29, 0x58, 0x77, 0x3B, 0x03 }; + +const u16bit KASUMI_SBOX_S9[512] = { + 0x00A7, 0x00EF, 0x00A1, 0x017B, 0x0187, 0x014E, 0x0009, 0x0152, 0x0026, + 0x00E2, 0x0030, 0x0166, 0x01C4, 0x0181, 0x005A, 0x018D, 0x00B7, 0x00FD, + 0x0093, 0x014B, 0x019F, 0x0154, 0x0033, 0x016A, 0x0132, 0x01F4, 0x0106, + 0x0052, 0x00D8, 0x009F, 0x0164, 0x00B1, 0x00AF, 0x00F1, 0x01E9, 0x0025, + 0x00CE, 0x0011, 0x0000, 0x014D, 0x002C, 0x00FE, 0x017A, 0x003A, 0x008F, + 0x00DC, 0x0051, 0x0190, 0x005F, 0x0003, 0x013B, 0x00F5, 0x0036, 0x00EB, + 0x00DA, 0x0195, 0x01D8, 0x0108, 0x00AC, 0x01EE, 0x0173, 0x0122, 0x018F, + 0x004C, 0x00A5, 0x00C5, 0x018B, 0x0079, 0x0101, 0x01E0, 0x01A7, 0x00D4, + 0x00F0, 0x001C, 0x01CE, 0x00B0, 0x0196, 0x01FB, 0x0120, 0x00DF, 0x01F5, + 0x0197, 0x00F9, 0x0109, 0x0059, 0x00BA, 0x00DD, 0x01AC, 0x00A4, 0x004A, + 0x01B8, 0x00C4, 0x01CA, 0x01A5, 0x015E, 0x00A3, 0x00E8, 0x009E, 0x0086, + 0x0162, 0x000D, 0x00FA, 0x01EB, 0x008E, 0x00BF, 0x0045, 0x00C1, 0x01A9, + 0x0098, 0x00E3, 0x016E, 0x0087, 0x0158, 0x012C, 0x0114, 0x00F2, 0x01B5, + 0x0140, 0x0071, 0x0116, 0x000B, 0x00F3, 0x0057, 0x013D, 0x0024, 0x005D, + 0x01F0, 0x001B, 0x01E7, 0x01BE, 0x01E2, 0x0029, 0x0044, 0x009C, 0x01C9, + 0x0083, 0x0146, 0x0193, 0x0153, 0x0014, 0x0027, 0x0073, 0x01BA, 0x007C, + 0x01DB, 0x0180, 0x01FC, 0x0035, 0x0070, 0x00AA, 0x01DF, 0x0097, 0x007E, + 0x00A9, 0x0049, 0x010C, 0x0117, 0x0141, 0x00A8, 0x016C, 0x016B, 0x0124, + 0x002E, 0x01F3, 0x0189, 0x0147, 0x0144, 0x0018, 0x01C8, 0x010B, 0x009D, + 0x01CC, 0x01E8, 0x01AA, 0x0135, 0x00E5, 0x01B7, 0x01FA, 0x00D0, 0x010F, + 0x015D, 0x0191, 0x01B2, 0x00EC, 0x0010, 0x00D1, 0x0167, 0x0034, 0x0038, + 0x0078, 0x00C7, 0x0115, 0x01D1, 0x01A0, 0x00FC, 0x011F, 0x00F6, 0x0006, + 0x0053, 0x0131, 0x01A4, 0x0159, 0x0099, 0x01F6, 0x0041, 0x003D, 0x00F4, + 0x011A, 0x00AD, 0x00DE, 0x01A2, 0x0043, 0x0182, 0x0170, 0x0105, 0x0065, + 0x01DC, 0x0123, 0x00C3, 0x01AE, 0x0031, 0x004F, 0x00A6, 0x014A, 0x0118, + 0x017F, 0x0175, 0x0080, 0x017E, 0x0198, 0x009B, 0x01EF, 0x016F, 0x0184, + 0x0112, 0x006B, 0x01CB, 0x01A1, 0x003E, 0x01C6, 0x0084, 0x00E1, 0x00CB, + 0x013C, 0x00EA, 0x000E, 0x012D, 0x005B, 0x01F7, 0x011E, 0x01A8, 0x00D3, + 0x015B, 0x0133, 0x008C, 0x0176, 0x0023, 0x0067, 0x007D, 0x01AB, 0x0013, + 0x00D6, 0x01C5, 0x0092, 0x01F2, 0x013A, 0x01BC, 0x00E6, 0x0100, 0x0149, + 0x00C6, 0x011D, 0x0032, 0x0074, 0x004E, 0x019A, 0x000A, 0x00CD, 0x01FE, + 0x00AB, 0x00E7, 0x002D, 0x008B, 0x01D3, 0x001D, 0x0056, 0x01F9, 0x0020, + 0x0048, 0x001A, 0x0156, 0x0096, 0x0139, 0x01EA, 0x01AF, 0x00EE, 0x019B, + 0x0145, 0x0095, 0x01D9, 0x0028, 0x0077, 0x00AE, 0x0163, 0x00B9, 0x00E9, + 0x0185, 0x0047, 0x01C0, 0x0111, 0x0174, 0x0037, 0x006E, 0x00B2, 0x0142, + 0x000C, 0x01D5, 0x0188, 0x0171, 0x00BE, 0x0001, 0x006D, 0x0177, 0x0089, + 0x00B5, 0x0058, 0x004B, 0x0134, 0x0104, 0x01E4, 0x0062, 0x0110, 0x0172, + 0x0113, 0x019C, 0x006F, 0x0150, 0x013E, 0x0004, 0x01F8, 0x01EC, 0x0103, + 0x0130, 0x004D, 0x0151, 0x01B3, 0x0015, 0x0165, 0x012F, 0x014C, 0x01E3, + 0x0012, 0x002F, 0x0055, 0x0019, 0x01F1, 0x01DA, 0x0121, 0x0064, 0x010D, + 0x0128, 0x01DE, 0x010E, 0x006A, 0x001F, 0x0068, 0x01B1, 0x0054, 0x019E, + 0x01E6, 0x018A, 0x0060, 0x0063, 0x009A, 0x01FF, 0x0094, 0x019D, 0x0169, + 0x0199, 0x00FF, 0x00A2, 0x00D7, 0x012E, 0x00C9, 0x010A, 0x015F, 0x0157, + 0x0090, 0x01B9, 0x016D, 0x006C, 0x012A, 0x00FB, 0x0022, 0x00B6, 0x01FD, + 0x008A, 0x00D2, 0x014F, 0x0085, 0x0137, 0x0160, 0x0148, 0x008D, 0x018C, + 0x015A, 0x007B, 0x013F, 0x01C2, 0x0119, 0x01AD, 0x00E4, 0x01BB, 0x01E1, + 0x005C, 0x0194, 0x01E5, 0x01A6, 0x00F8, 0x0129, 0x0017, 0x00D5, 0x0082, + 0x01D2, 0x0016, 0x00D9, 0x011B, 0x0046, 0x0126, 0x0168, 0x01A3, 0x007F, + 0x0138, 0x0179, 0x0007, 0x01D4, 0x00C2, 0x0002, 0x0075, 0x0127, 0x01CF, + 0x0102, 0x00E0, 0x01BF, 0x00F7, 0x00BB, 0x0050, 0x018E, 0x011C, 0x0161, + 0x0069, 0x0186, 0x012B, 0x01D7, 0x01D6, 0x00B8, 0x0039, 0x00C8, 0x015C, + 0x003F, 0x00CC, 0x00BC, 0x0021, 0x01C3, 0x0061, 0x001E, 0x0136, 0x00DB, + 0x005E, 0x00A0, 0x0081, 0x01ED, 0x0040, 0x00B3, 0x0107, 0x0066, 0x00BD, + 0x00CF, 0x0072, 0x0192, 0x01B6, 0x01DD, 0x0183, 0x007A, 0x00C0, 0x002A, + 0x017D, 0x0005, 0x0091, 0x0076, 0x00B4, 0x01C1, 0x0125, 0x0143, 0x0088, + 0x017C, 0x002B, 0x0042, 0x003C, 0x01C7, 0x0155, 0x01BD, 0x00CA, 0x01B0, + 0x0008, 0x00ED, 0x000F, 0x0178, 0x01B4, 0x01D0, 0x003B, 0x01CD }; + +/* +* KASUMI FI Function +*/ +u16bit FI(u16bit I, u16bit K) + { + u16bit D9 = (I >> 7); + byte D7 = (I & 0x7F); + D9 = KASUMI_SBOX_S9[D9] ^ D7; + D7 = KASUMI_SBOX_S7[D7] ^ (D9 & 0x7F); + + D7 ^= (K >> 9); + D9 = KASUMI_SBOX_S9[D9 ^ (K & 0x1FF)] ^ D7; + D7 = KASUMI_SBOX_S7[D7] ^ (D9 & 0x7F); + return (D7 << 9) | D9; + } + +} + +/* +* KASUMI Encryption +*/ +void KASUMI::enc(const byte in[], byte out[]) const + { + u16bit B0 = load_be<u16bit>(in, 0); + u16bit B1 = load_be<u16bit>(in, 1); + u16bit B2 = load_be<u16bit>(in, 2); + u16bit B3 = load_be<u16bit>(in, 3); + + for(u32bit j = 0; j != 8; j += 2) + { + const u16bit* K = EK + 8*j; + + u16bit R = B1 ^ (rotate_left(B0, 1) & K[0]); + u16bit L = B0 ^ (rotate_left(R, 1) | K[1]); + + L = FI(L ^ K[ 2], K[ 3]) ^ R; + R = FI(R ^ K[ 4], K[ 5]) ^ L; + L = FI(L ^ K[ 6], K[ 7]) ^ R; + + R = B2 ^= R; + L = B3 ^= L; + + R = FI(R ^ K[10], K[11]) ^ L; + L = FI(L ^ K[12], K[13]) ^ R; + R = FI(R ^ K[14], K[15]) ^ L; + + R ^= (rotate_left(L, 1) & K[8]); + L ^= (rotate_left(R, 1) | K[9]); + + B0 ^= L; + B1 ^= R; + } + + store_be(out, B0, B1, B2, B3); + } + +/* +* KASUMI Decryption +*/ +void KASUMI::dec(const byte in[], byte out[]) const + { + u16bit B0 = load_be<u16bit>(in, 0); + u16bit B1 = load_be<u16bit>(in, 1); + u16bit B2 = load_be<u16bit>(in, 2); + u16bit B3 = load_be<u16bit>(in, 3); + + for(u32bit j = 0; j != 8; j += 2) + { + const u16bit* K = EK + 8*(6-j); + + u16bit L = B2, R = B3; + + L = FI(L ^ K[10], K[11]) ^ R; + R = FI(R ^ K[12], K[13]) ^ L; + L = FI(L ^ K[14], K[15]) ^ R; + + L ^= (rotate_left(R, 1) & K[8]); + R ^= (rotate_left(L, 1) | K[9]); + + R = B0 ^= R; + L = B1 ^= L; + + L ^= (rotate_left(R, 1) & K[0]); + R ^= (rotate_left(L, 1) | K[1]); + + R = FI(R ^ K[2], K[3]) ^ L; + L = FI(L ^ K[4], K[5]) ^ R; + R = FI(R ^ K[6], K[7]) ^ L; + + B2 ^= L; + B3 ^= R; + } + + store_be(out, B0, B1, B2, B3); + } + +/* +* KASUMI Key Schedule +*/ +void KASUMI::key_schedule(const byte key[], u32bit) + { + static const u16bit RC[] = { 0x0123, 0x4567, 0x89AB, 0xCDEF, + 0xFEDC, 0xBA98, 0x7654, 0x3210 }; + + SecureBuffer<u16bit, 16> K; + for(u32bit j = 0; j != 8; ++j) + { + K[j] = load_be<u16bit>(key, j); + K[j+8] = K[j] ^ RC[j]; + } + + for(u32bit j = 0; j != 8; ++j) + { + EK[8*j ] = rotate_left(K[(j+0) % 8 ], 2); + EK[8*j+1] = rotate_left(K[(j+2) % 8 + 8], 1); + EK[8*j+2] = rotate_left(K[(j+1) % 8 ], 5); + EK[8*j+3] = K[(j+4) % 8 + 8]; + EK[8*j+4] = rotate_left(K[(j+5) % 8 ], 8); + EK[8*j+5] = K[(j+3) % 8 + 8]; + EK[8*j+6] = rotate_left(K[(j+6) % 8 ], 13); + EK[8*j+7] = K[(j+7) % 8 + 8]; + } + } + +} diff --git a/botan/src/block/kasumi/kasumi.h b/botan/src/block/kasumi/kasumi.h new file mode 100644 index 0000000..df49fa9 --- /dev/null +++ b/botan/src/block/kasumi/kasumi.h @@ -0,0 +1,36 @@ +/* +* KASUMI +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_KASUMI_H__ +#define BOTAN_KASUMI_H__ + +#include <botan/block_cipher.h> + +namespace Botan { + +/* +* KASUMI +*/ +class BOTAN_DLL KASUMI : public BlockCipher + { + public: + void clear() throw() { EK.clear(); } + std::string name() const { return "KASUMI"; } + BlockCipher* clone() const { return new KASUMI; } + + KASUMI() : BlockCipher(8, 16) {} + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key_schedule(const byte[], u32bit); + + SecureBuffer<u16bit, 64> EK; + }; + +} + +#endif diff --git a/botan/src/block/lion/info.txt b/botan/src/block/lion/info.txt new file mode 100644 index 0000000..81ef585 --- /dev/null +++ b/botan/src/block/lion/info.txt @@ -0,0 +1,15 @@ +realname "Lion" + +define LION + +load_on auto + +<add> +lion.cpp +lion.h +</add> + +<requires> +hash +stream +</requires> diff --git a/botan/src/block/lion/lion.cpp b/botan/src/block/lion/lion.cpp new file mode 100644 index 0000000..c7cdf6d --- /dev/null +++ b/botan/src/block/lion/lion.cpp @@ -0,0 +1,114 @@ +/* +* Lion +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/lion.h> +#include <botan/xor_buf.h> +#include <botan/parsing.h> + +namespace Botan { + +/* +* Lion Encryption +*/ +void Lion::enc(const byte in[], byte out[]) const + { + SecureVector<byte> buffer(LEFT_SIZE); + + xor_buf(buffer, in, key1, LEFT_SIZE); + cipher->set_key(buffer, LEFT_SIZE); + cipher->encrypt(in + LEFT_SIZE, out + LEFT_SIZE, RIGHT_SIZE); + + hash->update(out + LEFT_SIZE, RIGHT_SIZE); + hash->final(buffer); + xor_buf(out, in, buffer, LEFT_SIZE); + + xor_buf(buffer, out, key2, LEFT_SIZE); + cipher->set_key(buffer, LEFT_SIZE); + cipher->encrypt(out + LEFT_SIZE, RIGHT_SIZE); + } + +/* +* Lion Decryption +*/ +void Lion::dec(const byte in[], byte out[]) const + { + SecureVector<byte> buffer(LEFT_SIZE); + + xor_buf(buffer, in, key2, LEFT_SIZE); + cipher->set_key(buffer, LEFT_SIZE); + cipher->encrypt(in + LEFT_SIZE, out + LEFT_SIZE, RIGHT_SIZE); + + hash->update(out + LEFT_SIZE, RIGHT_SIZE); + hash->final(buffer); + xor_buf(out, in, buffer, LEFT_SIZE); + + xor_buf(buffer, out, key1, LEFT_SIZE); + cipher->set_key(buffer, LEFT_SIZE); + cipher->encrypt(out + LEFT_SIZE, RIGHT_SIZE); + } + +/* +* Lion Key Schedule +*/ +void Lion::key_schedule(const byte key[], u32bit length) + { + clear(); + + key1.copy(key, length / 2); + key2.copy(key + length / 2, length / 2); + } + +/* +* Return the name of this type +*/ +std::string Lion::name() const + { + return "Lion(" + hash->name() + "," + + cipher->name() + "," + + to_string(BLOCK_SIZE) + ")"; + } + +/* +* Return a clone of this object +*/ +BlockCipher* Lion::clone() const + { + return new Lion(hash->clone(), cipher->clone(), BLOCK_SIZE); + } + +/* +* Clear memory of sensitive data +*/ +void Lion::clear() throw() + { + hash->clear(); + cipher->clear(); + key1.clear(); + key2.clear(); + } + +/* +* Lion Constructor +*/ +Lion::Lion(HashFunction* hash_in, StreamCipher* sc_in, u32bit block_len) : + BlockCipher(std::max<u32bit>(2*hash_in->OUTPUT_LENGTH + 1, block_len), + 2, 2*hash_in->OUTPUT_LENGTH, 2), + LEFT_SIZE(hash_in->OUTPUT_LENGTH), + RIGHT_SIZE(BLOCK_SIZE - LEFT_SIZE), + hash(hash_in), + cipher(sc_in) + { + if(2*LEFT_SIZE + 1 > BLOCK_SIZE) + throw Invalid_Argument(name() + ": Chosen block size is too small"); + if(!cipher->valid_keylength(LEFT_SIZE)) + throw Exception(name() + ": This stream/hash combination is invalid"); + + key1.create(LEFT_SIZE); + key2.create(LEFT_SIZE); + } + +} diff --git a/botan/src/block/lion/lion.h b/botan/src/block/lion/lion.h new file mode 100644 index 0000000..5bc4e72 --- /dev/null +++ b/botan/src/block/lion/lion.h @@ -0,0 +1,43 @@ +/* +* Lion +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_LION_H__ +#define BOTAN_LION_H__ + +#include <botan/block_cipher.h> +#include <botan/stream_cipher.h> +#include <botan/hash.h> + +namespace Botan { + +/* +* Lion +*/ +class BOTAN_DLL Lion : public BlockCipher + { + public: + void clear() throw(); + std::string name() const; + BlockCipher* clone() const; + + Lion(HashFunction*, StreamCipher*, u32bit); + ~Lion() { delete hash; delete cipher; } + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key_schedule(const byte[], u32bit); + + const u32bit LEFT_SIZE, RIGHT_SIZE; + + HashFunction* hash; + StreamCipher* cipher; + SecureVector<byte> key1, key2; + }; + +} + +#endif diff --git a/botan/src/block/lubyrack/info.txt b/botan/src/block/lubyrack/info.txt new file mode 100644 index 0000000..a478526 --- /dev/null +++ b/botan/src/block/lubyrack/info.txt @@ -0,0 +1,14 @@ +realname "Luby-Rackoff" + +define LUBY_RACKOFF + +load_on auto + +<add> +lubyrack.cpp +lubyrack.h +</add> + +<requires> +hash +</requires> diff --git a/botan/src/block/lubyrack/lubyrack.cpp b/botan/src/block/lubyrack/lubyrack.cpp new file mode 100644 index 0000000..a9d2b1d --- /dev/null +++ b/botan/src/block/lubyrack/lubyrack.cpp @@ -0,0 +1,116 @@ +/* +* Luby-Rackoff +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/lubyrack.h> +#include <botan/xor_buf.h> + +namespace Botan { + +/* +* Luby-Rackoff Encryption +*/ +void LubyRackoff::enc(const byte in[], byte out[]) const + { + const u32bit len = hash->OUTPUT_LENGTH; + + SecureVector<byte> buffer(len); + hash->update(K1); + hash->update(in, len); + hash->final(buffer); + xor_buf(out + len, in + len, buffer, len); + + hash->update(K2); + hash->update(out + len, len); + hash->final(buffer); + xor_buf(out, in, buffer, len); + + hash->update(K1); + hash->update(out, len); + hash->final(buffer); + xor_buf(out + len, buffer, len); + + hash->update(K2); + hash->update(out + len, len); + hash->final(buffer); + xor_buf(out, buffer, len); + } + +/* +* Luby-Rackoff Decryption +*/ +void LubyRackoff::dec(const byte in[], byte out[]) const + { + const u32bit len = hash->OUTPUT_LENGTH; + + SecureVector<byte> buffer(len); + hash->update(K2); + hash->update(in + len, len); + hash->final(buffer); + xor_buf(out, in, buffer, len); + + hash->update(K1); + hash->update(out, len); + hash->final(buffer); + xor_buf(out + len, in + len, buffer, len); + + hash->update(K2); + hash->update(out + len, len); + hash->final(buffer); + xor_buf(out, buffer, len); + + hash->update(K1); + hash->update(out, len); + hash->final(buffer); + xor_buf(out + len, buffer, len); + } + +/* +* Luby-Rackoff Key Schedule +*/ +void LubyRackoff::key_schedule(const byte key[], u32bit length) + { + K1.set(key, length / 2); + K2.set(key + length / 2, length / 2); + } + +/* +* Clear memory of sensitive data +*/ +void LubyRackoff::clear() throw() + { + K1.clear(); + K2.clear(); + hash->clear(); + } + +/* +* Return a clone of this object +*/ +BlockCipher* LubyRackoff::clone() const + { + return new LubyRackoff(hash->clone()); + } + +/* +* Return the name of this type +*/ +std::string LubyRackoff::name() const + { + return "Luby-Rackoff(" + hash->name() + ")"; + } + +/* +* Luby-Rackoff Constructor +*/ +LubyRackoff::LubyRackoff(HashFunction* h) : + BlockCipher(2 * (h ? h->OUTPUT_LENGTH: 0), + 2, 32, 2), + hash(h) + { + } + +} diff --git a/botan/src/block/lubyrack/lubyrack.h b/botan/src/block/lubyrack/lubyrack.h new file mode 100644 index 0000000..ebde313 --- /dev/null +++ b/botan/src/block/lubyrack/lubyrack.h @@ -0,0 +1,38 @@ +/* +* Luby-Rackoff +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_LUBY_RACKOFF_H__ +#define BOTAN_LUBY_RACKOFF_H__ + +#include <botan/block_cipher.h> +#include <botan/hash.h> + +namespace Botan { + +/* +* Luby-Rackoff +*/ +class BOTAN_DLL LubyRackoff : public BlockCipher + { + public: + void clear() throw(); + std::string name() const; + BlockCipher* clone() const; + + LubyRackoff(HashFunction* hash); + ~LubyRackoff() { delete hash; } + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key_schedule(const byte[], u32bit); + HashFunction* hash; + SecureVector<byte> K1, K2; + }; + +} + +#endif diff --git a/botan/src/block/mars/info.txt b/botan/src/block/mars/info.txt new file mode 100644 index 0000000..b0ad8af --- /dev/null +++ b/botan/src/block/mars/info.txt @@ -0,0 +1,11 @@ +realname "MARS" + +define MARS + +load_on auto + +<add> +mars.cpp +mars.h +mars_tab.cpp +</add> diff --git a/botan/src/block/mars/mars.cpp b/botan/src/block/mars/mars.cpp new file mode 100644 index 0000000..08c8409 --- /dev/null +++ b/botan/src/block/mars/mars.cpp @@ -0,0 +1,276 @@ +/* +* MARS +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/mars.h> +#include <botan/loadstor.h> +#include <botan/rotate.h> + +namespace Botan { + +namespace { + +/* +* Generate a mask for runs of bits +*/ +u32bit gen_mask(u32bit input) + { + u32bit mask = 0; + + for(u32bit j = 2; j != 31; ++j) + { + u32bit region = (input >> (j-1)) & 0x07; + + if(region == 0x00 || region == 0x07) + { + u32bit low = (j < 9) ? 0 : (j - 9); + u32bit high = (j < 23) ? j : 23; + + for(u32bit k = low; k != high; ++k) + { + u32bit value = (input >> k) & 0x3FF; + + if(value == 0 || value == 0x3FF) + { + mask |= 1 << j; + break; + } + } + } + } + + return mask; + } + +} + +/* +* MARS Encryption +*/ +void MARS::enc(const byte in[], byte out[]) const + { + u32bit A = load_le<u32bit>(in, 0) + EK[0]; + u32bit B = load_le<u32bit>(in, 1) + EK[1]; + u32bit C = load_le<u32bit>(in, 2) + EK[2]; + u32bit D = load_le<u32bit>(in, 3) + EK[3]; + + forward_mix(A, B, C, D); + + encrypt_round(A, B, C, D, 0); + encrypt_round(B, C, D, A, 1); + encrypt_round(C, D, A, B, 2); + encrypt_round(D, A, B, C, 3); + encrypt_round(A, B, C, D, 4); + encrypt_round(B, C, D, A, 5); + encrypt_round(C, D, A, B, 6); + encrypt_round(D, A, B, C, 7); + + encrypt_round(A, D, C, B, 8); + encrypt_round(B, A, D, C, 9); + encrypt_round(C, B, A, D, 10); + encrypt_round(D, C, B, A, 11); + encrypt_round(A, D, C, B, 12); + encrypt_round(B, A, D, C, 13); + encrypt_round(C, B, A, D, 14); + encrypt_round(D, C, B, A, 15); + + reverse_mix(A, B, C, D); + + A -= EK[36]; B -= EK[37]; C -= EK[38]; D -= EK[39]; + + store_le(out, A, B, C, D); + } + +/* +* MARS Decryption +*/ +void MARS::dec(const byte in[], byte out[]) const + { + u32bit A = load_le<u32bit>(in, 3) + EK[39]; + u32bit B = load_le<u32bit>(in, 2) + EK[38]; + u32bit C = load_le<u32bit>(in, 1) + EK[37]; + u32bit D = load_le<u32bit>(in, 0) + EK[36]; + + forward_mix(A, B, C, D); + + decrypt_round(A, B, C, D, 15); + decrypt_round(B, C, D, A, 14); + decrypt_round(C, D, A, B, 13); + decrypt_round(D, A, B, C, 12); + decrypt_round(A, B, C, D, 11); + decrypt_round(B, C, D, A, 10); + decrypt_round(C, D, A, B, 9); + decrypt_round(D, A, B, C, 8); + + decrypt_round(A, D, C, B, 7); + decrypt_round(B, A, D, C, 6); + decrypt_round(C, B, A, D, 5); + decrypt_round(D, C, B, A, 4); + decrypt_round(A, D, C, B, 3); + decrypt_round(B, A, D, C, 2); + decrypt_round(C, B, A, D, 1); + decrypt_round(D, C, B, A, 0); + + reverse_mix(A, B, C, D); + + A -= EK[3]; B -= EK[2]; C -= EK[1]; D -= EK[0]; + + store_le(out, D, C, B, A); + } + +/* +* MARS Forward Mixing Operation +*/ +void MARS::forward_mix(u32bit& A, u32bit& B, u32bit& C, u32bit& D) + { + for(u32bit j = 0; j != 2; ++j) + { + B ^= SBOX[get_byte(3, A)]; B += SBOX[get_byte(2, A) + 256]; + C += SBOX[get_byte(1, A)]; D ^= SBOX[get_byte(0, A) + 256]; + A = rotate_right(A, 24) + D; + + C ^= SBOX[get_byte(3, B)]; C += SBOX[get_byte(2, B) + 256]; + D += SBOX[get_byte(1, B)]; A ^= SBOX[get_byte(0, B) + 256]; + B = rotate_right(B, 24) + C; + + D ^= SBOX[get_byte(3, C)]; D += SBOX[get_byte(2, C) + 256]; + A += SBOX[get_byte(1, C)]; B ^= SBOX[get_byte(0, C) + 256]; + C = rotate_right(C, 24); + + A ^= SBOX[get_byte(3, D)]; A += SBOX[get_byte(2, D) + 256]; + B += SBOX[get_byte(1, D)]; C ^= SBOX[get_byte(0, D) + 256]; + D = rotate_right(D, 24); + } + } + +/* +* MARS Reverse Mixing Operation +*/ +void MARS::reverse_mix(u32bit& A, u32bit& B, u32bit& C, u32bit& D) + { + for(u32bit j = 0; j != 2; ++j) + { + B ^= SBOX[get_byte(3, A) + 256]; C -= SBOX[get_byte(0, A)]; + D -= SBOX[get_byte(1, A) + 256]; D ^= SBOX[get_byte(2, A)]; + A = rotate_left(A, 24); + + C ^= SBOX[get_byte(3, B) + 256]; D -= SBOX[get_byte(0, B)]; + A -= SBOX[get_byte(1, B) + 256]; A ^= SBOX[get_byte(2, B)]; + C -= (B = rotate_left(B, 24)); + + D ^= SBOX[get_byte(3, C) + 256]; A -= SBOX[get_byte(0, C)]; + B -= SBOX[get_byte(1, C) + 256]; B ^= SBOX[get_byte(2, C)]; + C = rotate_left(C, 24); + D -= A; + + A ^= SBOX[get_byte(3, D) + 256]; B -= SBOX[get_byte(0, D)]; + C -= SBOX[get_byte(1, D) + 256]; C ^= SBOX[get_byte(2, D)]; + D = rotate_left(D, 24); + } + } + +/* +* MARS Encryption Round +*/ +void MARS::encrypt_round(u32bit& A, u32bit& B, u32bit& C, u32bit& D, + u32bit round) const + { + u32bit X, Y, Z; + X = A + EK[2*round + 4]; + A = rotate_left(A, 13); + Y = A * EK[2*round + 5]; + Z = SBOX[X % 512]; + Y = rotate_left(Y, 5); + Z ^= Y; + C += rotate_left(X, Y % 32); + Y = rotate_left(Y, 5); + Z ^= Y; + D ^= Y; + B += rotate_left(Z, Y % 32); + } + +/* +* MARS Decryption Round +*/ +void MARS::decrypt_round(u32bit& A, u32bit& B, u32bit& C, u32bit& D, + u32bit round) const + { + u32bit X, Y, Z; + Y = A * EK[2*round + 5]; + A = rotate_right(A, 13); + X = A + EK[2*round + 4]; + Z = SBOX[X % 512]; + Y = rotate_left(Y, 5); + Z ^= Y; + C -= rotate_left(X, Y % 32); + Y = rotate_left(Y, 5); + Z ^= Y; + D ^= Y; + B -= rotate_left(Z, Y % 32); + } + +/* +* MARS Key Schedule +*/ +void MARS::key_schedule(const byte key[], u32bit length) + { + SecureBuffer<u32bit, 15> T; + for(u32bit j = 0; j != length / 4; ++j) + T[j] = load_le<u32bit>(key, j); + T[length / 4] = length / 4; + + for(u32bit j = 0; j != 4; ++j) + { + T[ 0] ^= rotate_left(T[ 8] ^ T[13], 3) ^ (j ); + T[ 1] ^= rotate_left(T[ 9] ^ T[14], 3) ^ (j + 4); + T[ 2] ^= rotate_left(T[10] ^ T[ 0], 3) ^ (j + 8); + T[ 3] ^= rotate_left(T[11] ^ T[ 1], 3) ^ (j + 12); + T[ 4] ^= rotate_left(T[12] ^ T[ 2], 3) ^ (j + 16); + T[ 5] ^= rotate_left(T[13] ^ T[ 3], 3) ^ (j + 20); + T[ 6] ^= rotate_left(T[14] ^ T[ 4], 3) ^ (j + 24); + T[ 7] ^= rotate_left(T[ 0] ^ T[ 5], 3) ^ (j + 28); + T[ 8] ^= rotate_left(T[ 1] ^ T[ 6], 3) ^ (j + 32); + T[ 9] ^= rotate_left(T[ 2] ^ T[ 7], 3) ^ (j + 36); + T[10] ^= rotate_left(T[ 3] ^ T[ 8], 3) ^ (j + 40); + T[11] ^= rotate_left(T[ 4] ^ T[ 9], 3) ^ (j + 44); + T[12] ^= rotate_left(T[ 5] ^ T[10], 3) ^ (j + 48); + T[13] ^= rotate_left(T[ 6] ^ T[11], 3) ^ (j + 52); + T[14] ^= rotate_left(T[ 7] ^ T[12], 3) ^ (j + 56); + + for(u32bit k = 0; k != 4; ++k) + { + T[ 0] = rotate_left(T[ 0] + SBOX[T[14] % 512], 9); + T[ 1] = rotate_left(T[ 1] + SBOX[T[ 0] % 512], 9); + T[ 2] = rotate_left(T[ 2] + SBOX[T[ 1] % 512], 9); + T[ 3] = rotate_left(T[ 3] + SBOX[T[ 2] % 512], 9); + T[ 4] = rotate_left(T[ 4] + SBOX[T[ 3] % 512], 9); + T[ 5] = rotate_left(T[ 5] + SBOX[T[ 4] % 512], 9); + T[ 6] = rotate_left(T[ 6] + SBOX[T[ 5] % 512], 9); + T[ 7] = rotate_left(T[ 7] + SBOX[T[ 6] % 512], 9); + T[ 8] = rotate_left(T[ 8] + SBOX[T[ 7] % 512], 9); + T[ 9] = rotate_left(T[ 9] + SBOX[T[ 8] % 512], 9); + T[10] = rotate_left(T[10] + SBOX[T[ 9] % 512], 9); + T[11] = rotate_left(T[11] + SBOX[T[10] % 512], 9); + T[12] = rotate_left(T[12] + SBOX[T[11] % 512], 9); + T[13] = rotate_left(T[13] + SBOX[T[12] % 512], 9); + T[14] = rotate_left(T[14] + SBOX[T[13] % 512], 9); + } + + EK[10*j + 0] = T[ 0]; EK[10*j + 1] = T[ 4]; EK[10*j + 2] = T[ 8]; + EK[10*j + 3] = T[12]; EK[10*j + 4] = T[ 1]; EK[10*j + 5] = T[ 5]; + EK[10*j + 6] = T[ 9]; EK[10*j + 7] = T[13]; EK[10*j + 8] = T[ 2]; + EK[10*j + 9] = T[ 6]; + } + + for(u32bit j = 5; j != 37; j += 2) + { + u32bit key3 = EK[j] & 3; + EK[j] |= 3; + EK[j] ^= rotate_left(SBOX[265 + key3], EK[j-1] % 32) & gen_mask(EK[j]); + } + } + +} diff --git a/botan/src/block/mars/mars.h b/botan/src/block/mars/mars.h new file mode 100644 index 0000000..ca49695 --- /dev/null +++ b/botan/src/block/mars/mars.h @@ -0,0 +1,38 @@ +/* +* MARS +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_MARS_H__ +#define BOTAN_MARS_H__ + +#include <botan/block_cipher.h> + +namespace Botan { + +class BOTAN_DLL MARS : public BlockCipher + { + public: + void clear() throw() { EK.clear(); } + std::string name() const { return "MARS"; } + BlockCipher* clone() const { return new MARS; } + MARS() : BlockCipher(16, 16, 32, 4) {} + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key_schedule(const byte[], u32bit); + + void encrypt_round(u32bit&, u32bit&, u32bit&, u32bit&, u32bit) const; + void decrypt_round(u32bit&, u32bit&, u32bit&, u32bit&, u32bit) const; + static void forward_mix(u32bit&, u32bit&, u32bit&, u32bit&); + static void reverse_mix(u32bit&, u32bit&, u32bit&, u32bit&); + + static const u32bit SBOX[512]; + SecureBuffer<u32bit, 40> EK; + }; + +} + +#endif diff --git a/botan/src/block/mars/mars_tab.cpp b/botan/src/block/mars/mars_tab.cpp new file mode 100644 index 0000000..fb8f345 --- /dev/null +++ b/botan/src/block/mars/mars_tab.cpp @@ -0,0 +1,100 @@ +/* +* S-Box Table for MARS +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/mars.h> + +namespace Botan { + +const u32bit MARS::SBOX[512] = { + 0x09D0C479, 0x28C8FFE0, 0x84AA6C39, 0x9DAD7287, 0x7DFF9BE3, 0xD4268361, + 0xC96DA1D4, 0x7974CC93, 0x85D0582E, 0x2A4B5705, 0x1CA16A62, 0xC3BD279D, + 0x0F1F25E5, 0x5160372F, 0xC695C1FB, 0x4D7FF1E4, 0xAE5F6BF4, 0x0D72EE46, + 0xFF23DE8A, 0xB1CF8E83, 0xF14902E2, 0x3E981E42, 0x8BF53EB6, 0x7F4BF8AC, + 0x83631F83, 0x25970205, 0x76AFE784, 0x3A7931D4, 0x4F846450, 0x5C64C3F6, + 0x210A5F18, 0xC6986A26, 0x28F4E826, 0x3A60A81C, 0xD340A664, 0x7EA820C4, + 0x526687C5, 0x7EDDD12B, 0x32A11D1D, 0x9C9EF086, 0x80F6E831, 0xAB6F04AD, + 0x56FB9B53, 0x8B2E095C, 0xB68556AE, 0xD2250B0D, 0x294A7721, 0xE21FB253, + 0xAE136749, 0xE82AAE86, 0x93365104, 0x99404A66, 0x78A784DC, 0xB69BA84B, + 0x04046793, 0x23DB5C1E, 0x46CAE1D6, 0x2FE28134, 0x5A223942, 0x1863CD5B, + 0xC190C6E3, 0x07DFB846, 0x6EB88816, 0x2D0DCC4A, 0xA4CCAE59, 0x3798670D, + 0xCBFA9493, 0x4F481D45, 0xEAFC8CA8, 0xDB1129D6, 0xB0449E20, 0x0F5407FB, + 0x6167D9A8, 0xD1F45763, 0x4DAA96C3, 0x3BEC5958, 0xABABA014, 0xB6CCD201, + 0x38D6279F, 0x02682215, 0x8F376CD5, 0x092C237E, 0xBFC56593, 0x32889D2C, + 0x854B3E95, 0x05BB9B43, 0x7DCD5DCD, 0xA02E926C, 0xFAE527E5, 0x36A1C330, + 0x3412E1AE, 0xF257F462, 0x3C4F1D71, 0x30A2E809, 0x68E5F551, 0x9C61BA44, + 0x5DED0AB8, 0x75CE09C8, 0x9654F93E, 0x698C0CCA, 0x243CB3E4, 0x2B062B97, + 0x0F3B8D9E, 0x00E050DF, 0xFC5D6166, 0xE35F9288, 0xC079550D, 0x0591AEE8, + 0x8E531E74, 0x75FE3578, 0x2F6D829A, 0xF60B21AE, 0x95E8EB8D, 0x6699486B, + 0x901D7D9B, 0xFD6D6E31, 0x1090ACEF, 0xE0670DD8, 0xDAB2E692, 0xCD6D4365, + 0xE5393514, 0x3AF345F0, 0x6241FC4D, 0x460DA3A3, 0x7BCF3729, 0x8BF1D1E0, + 0x14AAC070, 0x1587ED55, 0x3AFD7D3E, 0xD2F29E01, 0x29A9D1F6, 0xEFB10C53, + 0xCF3B870F, 0xB414935C, 0x664465ED, 0x024ACAC7, 0x59A744C1, 0x1D2936A7, + 0xDC580AA6, 0xCF574CA8, 0x040A7A10, 0x6CD81807, 0x8A98BE4C, 0xACCEA063, + 0xC33E92B5, 0xD1E0E03D, 0xB322517E, 0x2092BD13, 0x386B2C4A, 0x52E8DD58, + 0x58656DFB, 0x50820371, 0x41811896, 0xE337EF7E, 0xD39FB119, 0xC97F0DF6, + 0x68FEA01B, 0xA150A6E5, 0x55258962, 0xEB6FF41B, 0xD7C9CD7A, 0xA619CD9E, + 0xBCF09576, 0x2672C073, 0xF003FB3C, 0x4AB7A50B, 0x1484126A, 0x487BA9B1, + 0xA64FC9C6, 0xF6957D49, 0x38B06A75, 0xDD805FCD, 0x63D094CF, 0xF51C999E, + 0x1AA4D343, 0xB8495294, 0xCE9F8E99, 0xBFFCD770, 0xC7C275CC, 0x378453A7, + 0x7B21BE33, 0x397F41BD, 0x4E94D131, 0x92CC1F98, 0x5915EA51, 0x99F861B7, + 0xC9980A88, 0x1D74FD5F, 0xB0A495F8, 0x614DEED0, 0xB5778EEA, 0x5941792D, + 0xFA90C1F8, 0x33F824B4, 0xC4965372, 0x3FF6D550, 0x4CA5FEC0, 0x8630E964, + 0x5B3FBBD6, 0x7DA26A48, 0xB203231A, 0x04297514, 0x2D639306, 0x2EB13149, + 0x16A45272, 0x532459A0, 0x8E5F4872, 0xF966C7D9, 0x07128DC0, 0x0D44DB62, + 0xAFC8D52D, 0x06316131, 0xD838E7CE, 0x1BC41D00, 0x3A2E8C0F, 0xEA83837E, + 0xB984737D, 0x13BA4891, 0xC4F8B949, 0xA6D6ACB3, 0xA215CDCE, 0x8359838B, + 0x6BD1AA31, 0xF579DD52, 0x21B93F93, 0xF5176781, 0x187DFDDE, 0xE94AEB76, + 0x2B38FD54, 0x431DE1DA, 0xAB394825, 0x9AD3048F, 0xDFEA32AA, 0x659473E3, + 0x623F7863, 0xF3346C59, 0xAB3AB685, 0x3346A90B, 0x6B56443E, 0xC6DE01F8, + 0x8D421FC0, 0x9B0ED10C, 0x88F1A1E9, 0x54C1F029, 0x7DEAD57B, 0x8D7BA426, + 0x4CF5178A, 0x551A7CCA, 0x1A9A5F08, 0xFCD651B9, 0x25605182, 0xE11FC6C3, + 0xB6FD9676, 0x337B3027, 0xB7C8EB14, 0x9E5FD030, 0x6B57E354, 0xAD913CF7, + 0x7E16688D, 0x58872A69, 0x2C2FC7DF, 0xE389CCC6, 0x30738DF1, 0x0824A734, + 0xE1797A8B, 0xA4A8D57B, 0x5B5D193B, 0xC8A8309B, 0x73F9A978, 0x73398D32, + 0x0F59573E, 0xE9DF2B03, 0xE8A5B6C8, 0x848D0704, 0x98DF93C2, 0x720A1DC3, + 0x684F259A, 0x943BA848, 0xA6370152, 0x863B5EA3, 0xD17B978B, 0x6D9B58EF, + 0x0A700DD4, 0xA73D36BF, 0x8E6A0829, 0x8695BC14, 0xE35B3447, 0x933AC568, + 0x8894B022, 0x2F511C27, 0xDDFBCC3C, 0x006662B6, 0x117C83FE, 0x4E12B414, + 0xC2BCA766, 0x3A2FEC10, 0xF4562420, 0x55792E2A, 0x46F5D857, 0xCEDA25CE, + 0xC3601D3B, 0x6C00AB46, 0xEFAC9C28, 0xB3C35047, 0x611DFEE3, 0x257C3207, + 0xFDD58482, 0x3B14D84F, 0x23BECB64, 0xA075F3A3, 0x088F8EAD, 0x07ADF158, + 0x7796943C, 0xFACABF3D, 0xC09730CD, 0xF7679969, 0xDA44E9ED, 0x2C854C12, + 0x35935FA3, 0x2F057D9F, 0x690624F8, 0x1CB0BAFD, 0x7B0DBDC6, 0x810F23BB, + 0xFA929A1A, 0x6D969A17, 0x6742979B, 0x74AC7D05, 0x010E65C4, 0x86A3D963, + 0xF907B5A0, 0xD0042BD3, 0x158D7D03, 0x287A8255, 0xBBA8366F, 0x096EDC33, + 0x21916A7B, 0x77B56B86, 0x951622F9, 0xA6C5E650, 0x8CEA17D1, 0xCD8C62BC, + 0xA3D63433, 0x358A68FD, 0x0F9B9D3C, 0xD6AA295B, 0xFE33384A, 0xC000738E, + 0xCD67EB2F, 0xE2EB6DC2, 0x97338B02, 0x06C9F246, 0x419CF1AD, 0x2B83C045, + 0x3723F18A, 0xCB5B3089, 0x160BEAD7, 0x5D494656, 0x35F8A74B, 0x1E4E6C9E, + 0x000399BD, 0x67466880, 0xB4174831, 0xACF423B2, 0xCA815AB3, 0x5A6395E7, + 0x302A67C5, 0x8BDB446B, 0x108F8FA4, 0x10223EDA, 0x92B8B48B, 0x7F38D0EE, + 0xAB2701D4, 0x0262D415, 0xAF224A30, 0xB3D88ABA, 0xF8B2C3AF, 0xDAF7EF70, + 0xCC97D3B7, 0xE9614B6C, 0x2BAEBFF4, 0x70F687CF, 0x386C9156, 0xCE092EE5, + 0x01E87DA6, 0x6CE91E6A, 0xBB7BCC84, 0xC7922C20, 0x9D3B71FD, 0x060E41C6, + 0xD7590F15, 0x4E03BB47, 0x183C198E, 0x63EEB240, 0x2DDBF49A, 0x6D5CBA54, + 0x923750AF, 0xF9E14236, 0x7838162B, 0x59726C72, 0x81B66760, 0xBB2926C1, + 0x48A0CE0D, 0xA6C0496D, 0xAD43507B, 0x718D496A, 0x9DF057AF, 0x44B1BDE6, + 0x054356DC, 0xDE7CED35, 0xD51A138B, 0x62088CC9, 0x35830311, 0xC96EFCA2, + 0x686F86EC, 0x8E77CB68, 0x63E1D6B8, 0xC80F9778, 0x79C491FD, 0x1B4C67F2, + 0x72698D7D, 0x5E368C31, 0xF7D95E2E, 0xA1D3493F, 0xDCD9433E, 0x896F1552, + 0x4BC4CA7A, 0xA6D1BAF4, 0xA5A96DCC, 0x0BEF8B46, 0xA169FDA7, 0x74DF40B7, + 0x4E208804, 0x9A756607, 0x038E87C8, 0x20211E44, 0x8B7AD4BF, 0xC6403F35, + 0x1848E36D, 0x80BDB038, 0x1E62891C, 0x643D2107, 0xBF04D6F8, 0x21092C8C, + 0xF644F389, 0x0778404E, 0x7B78ADB8, 0xA2C52D53, 0x42157ABE, 0xA2253E2E, + 0x7BF3F4AE, 0x80F594F9, 0x953194E7, 0x77EB92ED, 0xB3816930, 0xDA8D9336, + 0xBF447469, 0xF26D9483, 0xEE6FAED5, 0x71371235, 0xDE425F73, 0xB4E59F43, + 0x7DBE2D4E, 0x2D37B185, 0x49DC9A63, 0x98C39D98, 0x1301C9A2, 0x389B1BBF, + 0x0C18588D, 0xA421C1BA, 0x7AA3865C, 0x71E08558, 0x3C5CFCAA, 0x7D239CA4, + 0x0297D9DD, 0xD7DC2830, 0x4B37802B, 0x7428AB54, 0xAEEE0347, 0x4B3FBB85, + 0x692F2F08, 0x134E578E, 0x36D9E0BF, 0xAE8B5FCF, 0xEDB93ECF, 0x2B27248E, + 0x170EB1EF, 0x7DC57FD6, 0x1E760F16, 0xB1136601, 0x864E1B9B, 0xD7EA7319, + 0x3AB871BD, 0xCFA4D76F, 0xE31BD782, 0x0DBEB469, 0xABB96061, 0x5370F85D, + 0xFFB07E37, 0xDA30D0FB, 0xEBC977B6, 0x0B98B40F, 0x3A4D0FE6, 0xDF4FC26B, + 0x159CF22A, 0xC298D6E2, 0x2B78EF6A, 0x61A94AC0, 0xAB561187, 0x14EEA0F0, + 0xDF0D4164, 0x19AF70EE }; + +} diff --git a/botan/src/block/misty1/info.txt b/botan/src/block/misty1/info.txt new file mode 100644 index 0000000..d45058b --- /dev/null +++ b/botan/src/block/misty1/info.txt @@ -0,0 +1,10 @@ +realname "MISTY-1" + +define MISTY1 + +load_on auto + +<add> +misty1.cpp +misty1.h +</add> diff --git a/botan/src/block/misty1/misty1.cpp b/botan/src/block/misty1/misty1.cpp new file mode 100644 index 0000000..a35ff58 --- /dev/null +++ b/botan/src/block/misty1/misty1.cpp @@ -0,0 +1,249 @@ +/* +* MISTY1 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/misty1.h> +#include <botan/loadstor.h> +#include <botan/parsing.h> + +namespace Botan { + +namespace { + +static const byte MISTY1_SBOX_S7[128] = { + 0x1B, 0x32, 0x33, 0x5A, 0x3B, 0x10, 0x17, 0x54, 0x5B, 0x1A, 0x72, 0x73, + 0x6B, 0x2C, 0x66, 0x49, 0x1F, 0x24, 0x13, 0x6C, 0x37, 0x2E, 0x3F, 0x4A, + 0x5D, 0x0F, 0x40, 0x56, 0x25, 0x51, 0x1C, 0x04, 0x0B, 0x46, 0x20, 0x0D, + 0x7B, 0x35, 0x44, 0x42, 0x2B, 0x1E, 0x41, 0x14, 0x4B, 0x79, 0x15, 0x6F, + 0x0E, 0x55, 0x09, 0x36, 0x74, 0x0C, 0x67, 0x53, 0x28, 0x0A, 0x7E, 0x38, + 0x02, 0x07, 0x60, 0x29, 0x19, 0x12, 0x65, 0x2F, 0x30, 0x39, 0x08, 0x68, + 0x5F, 0x78, 0x2A, 0x4C, 0x64, 0x45, 0x75, 0x3D, 0x59, 0x48, 0x03, 0x57, + 0x7C, 0x4F, 0x62, 0x3C, 0x1D, 0x21, 0x5E, 0x27, 0x6A, 0x70, 0x4D, 0x3A, + 0x01, 0x6D, 0x6E, 0x63, 0x18, 0x77, 0x23, 0x05, 0x26, 0x76, 0x00, 0x31, + 0x2D, 0x7A, 0x7F, 0x61, 0x50, 0x22, 0x11, 0x06, 0x47, 0x16, 0x52, 0x4E, + 0x71, 0x3E, 0x69, 0x43, 0x34, 0x5C, 0x58, 0x7D }; + +static const u16bit MISTY1_SBOX_S9[512] = { + 0x01C3, 0x00CB, 0x0153, 0x019F, 0x01E3, 0x00E9, 0x00FB, 0x0035, 0x0181, + 0x00B9, 0x0117, 0x01EB, 0x0133, 0x0009, 0x002D, 0x00D3, 0x00C7, 0x014A, + 0x0037, 0x007E, 0x00EB, 0x0164, 0x0193, 0x01D8, 0x00A3, 0x011E, 0x0055, + 0x002C, 0x001D, 0x01A2, 0x0163, 0x0118, 0x014B, 0x0152, 0x01D2, 0x000F, + 0x002B, 0x0030, 0x013A, 0x00E5, 0x0111, 0x0138, 0x018E, 0x0063, 0x00E3, + 0x00C8, 0x01F4, 0x001B, 0x0001, 0x009D, 0x00F8, 0x01A0, 0x016D, 0x01F3, + 0x001C, 0x0146, 0x007D, 0x00D1, 0x0082, 0x01EA, 0x0183, 0x012D, 0x00F4, + 0x019E, 0x01D3, 0x00DD, 0x01E2, 0x0128, 0x01E0, 0x00EC, 0x0059, 0x0091, + 0x0011, 0x012F, 0x0026, 0x00DC, 0x00B0, 0x018C, 0x010F, 0x01F7, 0x00E7, + 0x016C, 0x00B6, 0x00F9, 0x00D8, 0x0151, 0x0101, 0x014C, 0x0103, 0x00B8, + 0x0154, 0x012B, 0x01AE, 0x0017, 0x0071, 0x000C, 0x0047, 0x0058, 0x007F, + 0x01A4, 0x0134, 0x0129, 0x0084, 0x015D, 0x019D, 0x01B2, 0x01A3, 0x0048, + 0x007C, 0x0051, 0x01CA, 0x0023, 0x013D, 0x01A7, 0x0165, 0x003B, 0x0042, + 0x00DA, 0x0192, 0x00CE, 0x00C1, 0x006B, 0x009F, 0x01F1, 0x012C, 0x0184, + 0x00FA, 0x0196, 0x01E1, 0x0169, 0x017D, 0x0031, 0x0180, 0x010A, 0x0094, + 0x01DA, 0x0186, 0x013E, 0x011C, 0x0060, 0x0175, 0x01CF, 0x0067, 0x0119, + 0x0065, 0x0068, 0x0099, 0x0150, 0x0008, 0x0007, 0x017C, 0x00B7, 0x0024, + 0x0019, 0x00DE, 0x0127, 0x00DB, 0x00E4, 0x01A9, 0x0052, 0x0109, 0x0090, + 0x019C, 0x01C1, 0x0028, 0x01B3, 0x0135, 0x016A, 0x0176, 0x00DF, 0x01E5, + 0x0188, 0x00C5, 0x016E, 0x01DE, 0x01B1, 0x00C3, 0x01DF, 0x0036, 0x00EE, + 0x01EE, 0x00F0, 0x0093, 0x0049, 0x009A, 0x01B6, 0x0069, 0x0081, 0x0125, + 0x000B, 0x005E, 0x00B4, 0x0149, 0x01C7, 0x0174, 0x003E, 0x013B, 0x01B7, + 0x008E, 0x01C6, 0x00AE, 0x0010, 0x0095, 0x01EF, 0x004E, 0x00F2, 0x01FD, + 0x0085, 0x00FD, 0x00F6, 0x00A0, 0x016F, 0x0083, 0x008A, 0x0156, 0x009B, + 0x013C, 0x0107, 0x0167, 0x0098, 0x01D0, 0x01E9, 0x0003, 0x01FE, 0x00BD, + 0x0122, 0x0089, 0x00D2, 0x018F, 0x0012, 0x0033, 0x006A, 0x0142, 0x00ED, + 0x0170, 0x011B, 0x00E2, 0x014F, 0x0158, 0x0131, 0x0147, 0x005D, 0x0113, + 0x01CD, 0x0079, 0x0161, 0x01A5, 0x0179, 0x009E, 0x01B4, 0x00CC, 0x0022, + 0x0132, 0x001A, 0x00E8, 0x0004, 0x0187, 0x01ED, 0x0197, 0x0039, 0x01BF, + 0x01D7, 0x0027, 0x018B, 0x00C6, 0x009C, 0x00D0, 0x014E, 0x006C, 0x0034, + 0x01F2, 0x006E, 0x00CA, 0x0025, 0x00BA, 0x0191, 0x00FE, 0x0013, 0x0106, + 0x002F, 0x01AD, 0x0172, 0x01DB, 0x00C0, 0x010B, 0x01D6, 0x00F5, 0x01EC, + 0x010D, 0x0076, 0x0114, 0x01AB, 0x0075, 0x010C, 0x01E4, 0x0159, 0x0054, + 0x011F, 0x004B, 0x00C4, 0x01BE, 0x00F7, 0x0029, 0x00A4, 0x000E, 0x01F0, + 0x0077, 0x004D, 0x017A, 0x0086, 0x008B, 0x00B3, 0x0171, 0x00BF, 0x010E, + 0x0104, 0x0097, 0x015B, 0x0160, 0x0168, 0x00D7, 0x00BB, 0x0066, 0x01CE, + 0x00FC, 0x0092, 0x01C5, 0x006F, 0x0016, 0x004A, 0x00A1, 0x0139, 0x00AF, + 0x00F1, 0x0190, 0x000A, 0x01AA, 0x0143, 0x017B, 0x0056, 0x018D, 0x0166, + 0x00D4, 0x01FB, 0x014D, 0x0194, 0x019A, 0x0087, 0x01F8, 0x0123, 0x00A7, + 0x01B8, 0x0141, 0x003C, 0x01F9, 0x0140, 0x002A, 0x0155, 0x011A, 0x01A1, + 0x0198, 0x00D5, 0x0126, 0x01AF, 0x0061, 0x012E, 0x0157, 0x01DC, 0x0072, + 0x018A, 0x00AA, 0x0096, 0x0115, 0x00EF, 0x0045, 0x007B, 0x008D, 0x0145, + 0x0053, 0x005F, 0x0178, 0x00B2, 0x002E, 0x0020, 0x01D5, 0x003F, 0x01C9, + 0x01E7, 0x01AC, 0x0044, 0x0038, 0x0014, 0x00B1, 0x016B, 0x00AB, 0x00B5, + 0x005A, 0x0182, 0x01C8, 0x01D4, 0x0018, 0x0177, 0x0064, 0x00CF, 0x006D, + 0x0100, 0x0199, 0x0130, 0x015A, 0x0005, 0x0120, 0x01BB, 0x01BD, 0x00E0, + 0x004F, 0x00D6, 0x013F, 0x01C4, 0x012A, 0x0015, 0x0006, 0x00FF, 0x019B, + 0x00A6, 0x0043, 0x0088, 0x0050, 0x015F, 0x01E8, 0x0121, 0x0073, 0x017E, + 0x00BC, 0x00C2, 0x00C9, 0x0173, 0x0189, 0x01F5, 0x0074, 0x01CC, 0x01E6, + 0x01A8, 0x0195, 0x001F, 0x0041, 0x000D, 0x01BA, 0x0032, 0x003D, 0x01D1, + 0x0080, 0x00A8, 0x0057, 0x01B9, 0x0162, 0x0148, 0x00D9, 0x0105, 0x0062, + 0x007A, 0x0021, 0x01FF, 0x0112, 0x0108, 0x01C0, 0x00A9, 0x011D, 0x01B0, + 0x01A6, 0x00CD, 0x00F3, 0x005C, 0x0102, 0x005B, 0x01D9, 0x0144, 0x01F6, + 0x00AD, 0x00A5, 0x003A, 0x01CB, 0x0136, 0x017F, 0x0046, 0x00E1, 0x001E, + 0x01DD, 0x00E6, 0x0137, 0x01FA, 0x0185, 0x008C, 0x008F, 0x0040, 0x01B5, + 0x00BE, 0x0078, 0x0000, 0x00AC, 0x0110, 0x015E, 0x0124, 0x0002, 0x01BC, + 0x00A2, 0x00EA, 0x0070, 0x01FC, 0x0116, 0x015C, 0x004C, 0x01C2 }; + +/* +* MISTY1 FI Function +*/ +u16bit FI(u16bit input, u16bit key7, u16bit key9) + { + u16bit D9 = input >> 7, D7 = input & 0x7F; + D9 = MISTY1_SBOX_S9[D9] ^ D7; + D7 = (MISTY1_SBOX_S7[D7] ^ key7 ^ D9) & 0x7F; + D9 = MISTY1_SBOX_S9[D9 ^ key9] ^ D7; + return static_cast<u16bit>((D7 << 9) | D9); + } + +} + +/* +* MISTY1 Encryption +*/ +void MISTY1::enc(const byte in[], byte out[]) const + { + u16bit B0 = load_be<u16bit>(in, 0); + u16bit B1 = load_be<u16bit>(in, 1); + u16bit B2 = load_be<u16bit>(in, 2); + u16bit B3 = load_be<u16bit>(in, 3); + + for(u32bit j = 0; j != 12; j += 3) + { + const u16bit* RK = EK + 8 * j; + + B1 ^= B0 & RK[0]; + B0 ^= B1 | RK[1]; + B3 ^= B2 & RK[2]; + B2 ^= B3 | RK[3]; + + u32bit T0, T1; + + T0 = FI(B0 ^ RK[ 4], RK[ 5], RK[ 6]) ^ B1; + T1 = FI(B1 ^ RK[ 7], RK[ 8], RK[ 9]) ^ T0; + T0 = FI(T0 ^ RK[10], RK[11], RK[12]) ^ T1; + + B2 ^= T1 ^ RK[13]; + B3 ^= T0; + + T0 = FI(B2 ^ RK[14], RK[15], RK[16]) ^ B3; + T1 = FI(B3 ^ RK[17], RK[18], RK[19]) ^ T0; + T0 = FI(T0 ^ RK[20], RK[21], RK[22]) ^ T1; + + B0 ^= T1 ^ RK[23]; + B1 ^= T0; + } + + B1 ^= B0 & EK[96]; + B0 ^= B1 | EK[97]; + B3 ^= B2 & EK[98]; + B2 ^= B3 | EK[99]; + + store_be(out, B2, B3, B0, B1); + } + +/* +* MISTY1 Decryption +*/ +void MISTY1::dec(const byte in[], byte out[]) const + { + u16bit B0 = load_be<u16bit>(in, 2); + u16bit B1 = load_be<u16bit>(in, 3); + u16bit B2 = load_be<u16bit>(in, 0); + u16bit B3 = load_be<u16bit>(in, 1); + + for(u32bit j = 0; j != 12; j += 3) + { + const u16bit* RK = DK + 8 * j; + + B2 ^= B3 | RK[0]; + B3 ^= B2 & RK[1]; + B0 ^= B1 | RK[2]; + B1 ^= B0 & RK[3]; + + u32bit T0, T1; + + T0 = FI(B2 ^ RK[ 4], RK[ 5], RK[ 6]) ^ B3; + T1 = FI(B3 ^ RK[ 7], RK[ 8], RK[ 9]) ^ T0; + T0 = FI(T0 ^ RK[10], RK[11], RK[12]) ^ T1; + + B0 ^= T1 ^ RK[13]; + B1 ^= T0; + + T0 = FI(B0 ^ RK[14], RK[15], RK[16]) ^ B1; + T1 = FI(B1 ^ RK[17], RK[18], RK[19]) ^ T0; + T0 = FI(T0 ^ RK[20], RK[21], RK[22]) ^ T1; + + B2 ^= T1 ^ RK[23]; + B3 ^= T0; + } + + B2 ^= B3 | DK[96]; + B3 ^= B2 & DK[97]; + B0 ^= B1 | DK[98]; + B1 ^= B0 & DK[99]; + + store_be(out, B0, B1, B2, B3); + } + +/* +* MISTY1 Key Schedule +*/ +void MISTY1::key_schedule(const byte key[], u32bit length) + { + SecureBuffer<u16bit, 32> KS; + for(u32bit j = 0; j != length / 2; ++j) + KS[j] = load_be<u16bit>(key, j); + + for(u32bit j = 0; j != 8; ++j) + { + KS[j+ 8] = FI(KS[j], KS[(j+1) % 8] >> 9, KS[(j+1) % 8] & 0x1FF); + KS[j+16] = KS[j+8] >> 9; + KS[j+24] = KS[j+8] & 0x1FF; + } + + /* + * Precomputed indexes for the orderings of the subkeys (MISTY1 reuses + * values) + */ + static const byte EK_ORDER[100] = { + 0x00, 0x0E, 0x0A, 0x04, 0x00, 0x15, 0x1D, 0x02, 0x11, 0x19, 0x07, 0x13, + 0x1B, 0x04, 0x01, 0x16, 0x1E, 0x03, 0x12, 0x1A, 0x00, 0x14, 0x1C, 0x05, + 0x01, 0x0F, 0x0B, 0x05, 0x02, 0x17, 0x1F, 0x04, 0x13, 0x1B, 0x01, 0x15, + 0x1D, 0x06, 0x03, 0x10, 0x18, 0x05, 0x14, 0x1C, 0x02, 0x16, 0x1E, 0x07, + 0x02, 0x08, 0x0C, 0x06, 0x04, 0x11, 0x19, 0x06, 0x15, 0x1D, 0x03, 0x17, + 0x1F, 0x00, 0x05, 0x12, 0x1A, 0x07, 0x16, 0x1E, 0x04, 0x10, 0x18, 0x01, + 0x03, 0x09, 0x0D, 0x07, 0x06, 0x13, 0x1B, 0x00, 0x17, 0x1F, 0x05, 0x11, + 0x19, 0x02, 0x07, 0x14, 0x1C, 0x01, 0x10, 0x18, 0x06, 0x12, 0x1A, 0x03, + 0x04, 0x0A, 0x0E, 0x00 }; + + static const byte DK_ORDER[100] = { + 0x00, 0x0E, 0x0A, 0x04, 0x07, 0x14, 0x1C, 0x01, 0x10, 0x18, 0x06, 0x12, + 0x1A, 0x03, 0x06, 0x13, 0x1B, 0x00, 0x17, 0x1F, 0x05, 0x11, 0x19, 0x02, + 0x07, 0x0D, 0x09, 0x03, 0x05, 0x12, 0x1A, 0x07, 0x16, 0x1E, 0x04, 0x10, + 0x18, 0x01, 0x04, 0x11, 0x19, 0x06, 0x15, 0x1D, 0x03, 0x17, 0x1F, 0x00, + 0x06, 0x0C, 0x08, 0x02, 0x03, 0x10, 0x18, 0x05, 0x14, 0x1C, 0x02, 0x16, + 0x1E, 0x07, 0x02, 0x17, 0x1F, 0x04, 0x13, 0x1B, 0x01, 0x15, 0x1D, 0x06, + 0x05, 0x0B, 0x0F, 0x01, 0x01, 0x16, 0x1E, 0x03, 0x12, 0x1A, 0x00, 0x14, + 0x1C, 0x05, 0x00, 0x15, 0x1D, 0x02, 0x11, 0x19, 0x07, 0x13, 0x1B, 0x04, + 0x04, 0x0A, 0x0E, 0x00 }; + + for(u32bit j = 0; j != 100; ++j) + { + EK[j] = KS[EK_ORDER[j]]; + DK[j] = KS[DK_ORDER[j]]; + } + } + +/* +* MISTY1 Constructor +*/ +MISTY1::MISTY1(u32bit rounds) : BlockCipher(8, 16) + { + if(rounds != 8) + throw Invalid_Argument("MISTY1: Invalid number of rounds: " + + to_string(rounds)); + } + +} diff --git a/botan/src/block/misty1/misty1.h b/botan/src/block/misty1/misty1.h new file mode 100644 index 0000000..62d4f85 --- /dev/null +++ b/botan/src/block/misty1/misty1.h @@ -0,0 +1,35 @@ +/** +* MISTY1 +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_MISTY1_H__ +#define BOTAN_MISTY1_H__ + +#include <botan/block_cipher.h> + +namespace Botan { + +/** +* MISTY1 +*/ +class BOTAN_DLL MISTY1 : public BlockCipher + { + public: + void clear() throw() { EK.clear(); DK.clear(); } + std::string name() const { return "MISTY1"; } + BlockCipher* clone() const { return new MISTY1; } + MISTY1(u32bit = 8); + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key_schedule(const byte[], u32bit); + + SecureBuffer<u16bit, 100> EK, DK; + }; + +} + +#endif diff --git a/botan/src/block/noekeon/info.txt b/botan/src/block/noekeon/info.txt new file mode 100644 index 0000000..8025c20 --- /dev/null +++ b/botan/src/block/noekeon/info.txt @@ -0,0 +1,10 @@ +realname "Noekeon" + +define NOEKEON + +load_on auto + +<add> +noekeon.cpp +noekeon.h +</add> diff --git a/botan/src/block/noekeon/noekeon.cpp b/botan/src/block/noekeon/noekeon.cpp new file mode 100644 index 0000000..90eb9ad --- /dev/null +++ b/botan/src/block/noekeon/noekeon.cpp @@ -0,0 +1,198 @@ +/* +* Noekeon +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/noekeon.h> +#include <botan/loadstor.h> +#include <botan/rotate.h> + +namespace Botan { + +namespace { + +/* +* Noekeon's Theta Operation +*/ +inline void theta(u32bit& A0, u32bit& A1, + u32bit& A2, u32bit& A3, + const u32bit EK[4]) + { + u32bit T = A0 ^ A2; + T ^= rotate_left(T, 8) ^ rotate_right(T, 8); + A1 ^= T; + A3 ^= T; + + A0 ^= EK[0]; + A1 ^= EK[1]; + A2 ^= EK[2]; + A3 ^= EK[3]; + + T = A1 ^ A3; + T ^= rotate_left(T, 8) ^ rotate_right(T, 8); + A0 ^= T; + A2 ^= T; + } + +/* +* Theta With Null Key +*/ +inline void theta(u32bit& A0, u32bit& A1, + u32bit& A2, u32bit& A3) + { + u32bit T = A0 ^ A2; + T ^= rotate_left(T, 8) ^ rotate_right(T, 8); + A1 ^= T; + A3 ^= T; + + T = A1 ^ A3; + T ^= rotate_left(T, 8) ^ rotate_right(T, 8); + A0 ^= T; + A2 ^= T; + } + +/* +* Noekeon's Gamma S-Box Layer +*/ +inline void gamma(u32bit& A0, u32bit& A1, u32bit& A2, u32bit& A3) + { + A1 ^= ~A3 & ~A2; + A0 ^= A2 & A1; + + u32bit T = A3; + A3 = A0; + A0 = T; + + A2 ^= A0 ^ A1 ^ A3; + + A1 ^= ~A3 & ~A2; + A0 ^= A2 & A1; + } + +} + +/* +* Noekeon Round Constants +*/ +const byte Noekeon::RC[] = { + 0x80, 0x1B, 0x36, 0x6C, 0xD8, 0xAB, 0x4D, 0x9A, + 0x2F, 0x5E, 0xBC, 0x63, 0xC6, 0x97, 0x35, 0x6A, + 0xD4 }; + +/* +* Noekeon Encryption +*/ +void Noekeon::enc(const byte in[], byte out[]) const + { + u32bit A0 = load_be<u32bit>(in, 0); + u32bit A1 = load_be<u32bit>(in, 1); + u32bit A2 = load_be<u32bit>(in, 2); + u32bit A3 = load_be<u32bit>(in, 3); + + for(u32bit j = 0; j != 16; ++j) + { + A0 ^= RC[j]; + theta(A0, A1, A2, A3, EK); + + A1 = rotate_left(A1, 1); + A2 = rotate_left(A2, 5); + A3 = rotate_left(A3, 2); + + gamma(A0, A1, A2, A3); + + A1 = rotate_right(A1, 1); + A2 = rotate_right(A2, 5); + A3 = rotate_right(A3, 2); + } + + A0 ^= RC[16]; + theta(A0, A1, A2, A3, EK); + + store_be(out, A0, A1, A2, A3); + } + +/* +* Noekeon Encryption +*/ +void Noekeon::dec(const byte in[], byte out[]) const + { + u32bit A0 = load_be<u32bit>(in, 0); + u32bit A1 = load_be<u32bit>(in, 1); + u32bit A2 = load_be<u32bit>(in, 2); + u32bit A3 = load_be<u32bit>(in, 3); + + for(u32bit j = 16; j != 0; --j) + { + theta(A0, A1, A2, A3, DK); + A0 ^= RC[j]; + + A1 = rotate_left(A1, 1); + A2 = rotate_left(A2, 5); + A3 = rotate_left(A3, 2); + + gamma(A0, A1, A2, A3); + + A1 = rotate_right(A1, 1); + A2 = rotate_right(A2, 5); + A3 = rotate_right(A3, 2); + } + + theta(A0, A1, A2, A3, DK); + A0 ^= RC[0]; + + store_be(out, A0, A1, A2, A3); + } + +/* +* Noekeon Key Schedule +*/ +void Noekeon::key_schedule(const byte key[], u32bit) + { + u32bit A0 = load_be<u32bit>(key, 0); + u32bit A1 = load_be<u32bit>(key, 1); + u32bit A2 = load_be<u32bit>(key, 2); + u32bit A3 = load_be<u32bit>(key, 3); + + for(u32bit j = 0; j != 16; ++j) + { + A0 ^= RC[j]; + theta(A0, A1, A2, A3); + + A1 = rotate_left(A1, 1); + A2 = rotate_left(A2, 5); + A3 = rotate_left(A3, 2); + + gamma(A0, A1, A2, A3); + + A1 = rotate_right(A1, 1); + A2 = rotate_right(A2, 5); + A3 = rotate_right(A3, 2); + } + + A0 ^= RC[16]; + + DK[0] = A0; + DK[1] = A1; + DK[2] = A2; + DK[3] = A3; + + theta(A0, A1, A2, A3); + + EK[0] = A0; + EK[1] = A1; + EK[2] = A2; + EK[3] = A3; + } + +/* +* Clear memory of sensitive data +*/ +void Noekeon::clear() throw() + { + EK.clear(); + DK.clear(); + } + +} diff --git a/botan/src/block/noekeon/noekeon.h b/botan/src/block/noekeon/noekeon.h new file mode 100644 index 0000000..8938924 --- /dev/null +++ b/botan/src/block/noekeon/noekeon.h @@ -0,0 +1,37 @@ +/* +* Noekeon +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_NOEKEON_H__ +#define BOTAN_NOEKEON_H__ + +#include <botan/block_cipher.h> + +namespace Botan { + +/* +* Noekeon +*/ +class BOTAN_DLL Noekeon : public BlockCipher + { + public: + void clear() throw(); + std::string name() const { return "Noekeon"; } + BlockCipher* clone() const { return new Noekeon; } + Noekeon() : BlockCipher(16, 16) {} + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key_schedule(const byte[], u32bit); + + static const byte RC[17]; + + SecureBuffer<u32bit, 4> EK, DK; + }; + +} + +#endif diff --git a/botan/src/block/rc2/info.txt b/botan/src/block/rc2/info.txt new file mode 100644 index 0000000..099141d --- /dev/null +++ b/botan/src/block/rc2/info.txt @@ -0,0 +1,10 @@ +realname "RC2" + +define RC2 + +load_on auto + +<add> +rc2.cpp +rc2.h +</add> diff --git a/botan/src/block/rc2/rc2.cpp b/botan/src/block/rc2/rc2.cpp new file mode 100644 index 0000000..5827bdb --- /dev/null +++ b/botan/src/block/rc2/rc2.cpp @@ -0,0 +1,163 @@ +/* +* RC2 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/rc2.h> +#include <botan/loadstor.h> +#include <botan/rotate.h> + +namespace Botan { + +/* +* RC2 Encryption +*/ +void RC2::enc(const byte in[], byte out[]) const + { + u16bit R0 = load_le<u16bit>(in, 0); + u16bit R1 = load_le<u16bit>(in, 1); + u16bit R2 = load_le<u16bit>(in, 2); + u16bit R3 = load_le<u16bit>(in, 3); + + for(u32bit j = 0; j != 16; ++j) + { + R0 += (R1 & ~R3) + (R2 & R3) + K[4*j]; + R0 = rotate_left(R0, 1); + + R1 += (R2 & ~R0) + (R3 & R0) + K[4*j + 1]; + R1 = rotate_left(R1, 2); + + R2 += (R3 & ~R1) + (R0 & R1) + K[4*j + 2]; + R2 = rotate_left(R2, 3); + + R3 += (R0 & ~R2) + (R1 & R2) + K[4*j + 3]; + R3 = rotate_left(R3, 5); + + if(j == 4 || j == 10) + { + R0 += K[R3 % 64]; + R1 += K[R0 % 64]; + R2 += K[R1 % 64]; + R3 += K[R2 % 64]; + } + } + + store_le(out, R0, R1, R2, R3); + } + +/* +* RC2 Decryption +*/ +void RC2::dec(const byte in[], byte out[]) const + { + u16bit R0 = load_le<u16bit>(in, 0); + u16bit R1 = load_le<u16bit>(in, 1); + u16bit R2 = load_le<u16bit>(in, 2); + u16bit R3 = load_le<u16bit>(in, 3); + + for(u32bit j = 0; j != 16; ++j) + { + R3 = rotate_right(R3, 5); + R3 -= (R0 & ~R2) + (R1 & R2) + K[63 - (4*j + 0)]; + + R2 = rotate_right(R2, 3); + R2 -= (R3 & ~R1) + (R0 & R1) + K[63 - (4*j + 1)]; + + R1 = rotate_right(R1, 2); + R1 -= (R2 & ~R0) + (R3 & R0) + K[63 - (4*j + 2)]; + + R0 = rotate_right(R0, 1); + R0 -= (R1 & ~R3) + (R2 & R3) + K[63 - (4*j + 3)]; + + if(j == 4 || j == 10) + { + R3 -= K[R2 % 64]; + R2 -= K[R1 % 64]; + R1 -= K[R0 % 64]; + R0 -= K[R3 % 64]; + } + } + + store_le(out, R0, R1, R2, R3); + } + +/* +* RC2 Key Schedule +*/ +void RC2::key_schedule(const byte key[], u32bit length) + { + static const byte TABLE[256] = { + 0xD9, 0x78, 0xF9, 0xC4, 0x19, 0xDD, 0xB5, 0xED, 0x28, 0xE9, 0xFD, 0x79, + 0x4A, 0xA0, 0xD8, 0x9D, 0xC6, 0x7E, 0x37, 0x83, 0x2B, 0x76, 0x53, 0x8E, + 0x62, 0x4C, 0x64, 0x88, 0x44, 0x8B, 0xFB, 0xA2, 0x17, 0x9A, 0x59, 0xF5, + 0x87, 0xB3, 0x4F, 0x13, 0x61, 0x45, 0x6D, 0x8D, 0x09, 0x81, 0x7D, 0x32, + 0xBD, 0x8F, 0x40, 0xEB, 0x86, 0xB7, 0x7B, 0x0B, 0xF0, 0x95, 0x21, 0x22, + 0x5C, 0x6B, 0x4E, 0x82, 0x54, 0xD6, 0x65, 0x93, 0xCE, 0x60, 0xB2, 0x1C, + 0x73, 0x56, 0xC0, 0x14, 0xA7, 0x8C, 0xF1, 0xDC, 0x12, 0x75, 0xCA, 0x1F, + 0x3B, 0xBE, 0xE4, 0xD1, 0x42, 0x3D, 0xD4, 0x30, 0xA3, 0x3C, 0xB6, 0x26, + 0x6F, 0xBF, 0x0E, 0xDA, 0x46, 0x69, 0x07, 0x57, 0x27, 0xF2, 0x1D, 0x9B, + 0xBC, 0x94, 0x43, 0x03, 0xF8, 0x11, 0xC7, 0xF6, 0x90, 0xEF, 0x3E, 0xE7, + 0x06, 0xC3, 0xD5, 0x2F, 0xC8, 0x66, 0x1E, 0xD7, 0x08, 0xE8, 0xEA, 0xDE, + 0x80, 0x52, 0xEE, 0xF7, 0x84, 0xAA, 0x72, 0xAC, 0x35, 0x4D, 0x6A, 0x2A, + 0x96, 0x1A, 0xD2, 0x71, 0x5A, 0x15, 0x49, 0x74, 0x4B, 0x9F, 0xD0, 0x5E, + 0x04, 0x18, 0xA4, 0xEC, 0xC2, 0xE0, 0x41, 0x6E, 0x0F, 0x51, 0xCB, 0xCC, + 0x24, 0x91, 0xAF, 0x50, 0xA1, 0xF4, 0x70, 0x39, 0x99, 0x7C, 0x3A, 0x85, + 0x23, 0xB8, 0xB4, 0x7A, 0xFC, 0x02, 0x36, 0x5B, 0x25, 0x55, 0x97, 0x31, + 0x2D, 0x5D, 0xFA, 0x98, 0xE3, 0x8A, 0x92, 0xAE, 0x05, 0xDF, 0x29, 0x10, + 0x67, 0x6C, 0xBA, 0xC9, 0xD3, 0x00, 0xE6, 0xCF, 0xE1, 0x9E, 0xA8, 0x2C, + 0x63, 0x16, 0x01, 0x3F, 0x58, 0xE2, 0x89, 0xA9, 0x0D, 0x38, 0x34, 0x1B, + 0xAB, 0x33, 0xFF, 0xB0, 0xBB, 0x48, 0x0C, 0x5F, 0xB9, 0xB1, 0xCD, 0x2E, + 0xC5, 0xF3, 0xDB, 0x47, 0xE5, 0xA5, 0x9C, 0x77, 0x0A, 0xA6, 0x20, 0x68, + 0xFE, 0x7F, 0xC1, 0xAD }; + + SecureBuffer<byte, 128> L; + L.copy(key, length); + + for(u32bit j = length; j != 128; ++j) + L[j] = TABLE[(L[j-1] + L[j-length]) % 256]; + L[128-length] = TABLE[L[128-length]]; + for(s32bit j = 127-length; j >= 0; --j) + L[j] = TABLE[L[j+1] ^ L[j+length]]; + + for(u32bit j = 0; j != 64; ++j) + K[j] = load_le<u16bit>(L, j); + } + +/* +* Return the code of the effective key bits +*/ +byte RC2::EKB_code(u32bit ekb) + { + const byte EKB[256] = { + 0xBD, 0x56, 0xEA, 0xF2, 0xA2, 0xF1, 0xAC, 0x2A, 0xB0, 0x93, 0xD1, 0x9C, + 0x1B, 0x33, 0xFD, 0xD0, 0x30, 0x04, 0xB6, 0xDC, 0x7D, 0xDF, 0x32, 0x4B, + 0xF7, 0xCB, 0x45, 0x9B, 0x31, 0xBB, 0x21, 0x5A, 0x41, 0x9F, 0xE1, 0xD9, + 0x4A, 0x4D, 0x9E, 0xDA, 0xA0, 0x68, 0x2C, 0xC3, 0x27, 0x5F, 0x80, 0x36, + 0x3E, 0xEE, 0xFB, 0x95, 0x1A, 0xFE, 0xCE, 0xA8, 0x34, 0xA9, 0x13, 0xF0, + 0xA6, 0x3F, 0xD8, 0x0C, 0x78, 0x24, 0xAF, 0x23, 0x52, 0xC1, 0x67, 0x17, + 0xF5, 0x66, 0x90, 0xE7, 0xE8, 0x07, 0xB8, 0x60, 0x48, 0xE6, 0x1E, 0x53, + 0xF3, 0x92, 0xA4, 0x72, 0x8C, 0x08, 0x15, 0x6E, 0x86, 0x00, 0x84, 0xFA, + 0xF4, 0x7F, 0x8A, 0x42, 0x19, 0xF6, 0xDB, 0xCD, 0x14, 0x8D, 0x50, 0x12, + 0xBA, 0x3C, 0x06, 0x4E, 0xEC, 0xB3, 0x35, 0x11, 0xA1, 0x88, 0x8E, 0x2B, + 0x94, 0x99, 0xB7, 0x71, 0x74, 0xD3, 0xE4, 0xBF, 0x3A, 0xDE, 0x96, 0x0E, + 0xBC, 0x0A, 0xED, 0x77, 0xFC, 0x37, 0x6B, 0x03, 0x79, 0x89, 0x62, 0xC6, + 0xD7, 0xC0, 0xD2, 0x7C, 0x6A, 0x8B, 0x22, 0xA3, 0x5B, 0x05, 0x5D, 0x02, + 0x75, 0xD5, 0x61, 0xE3, 0x18, 0x8F, 0x55, 0x51, 0xAD, 0x1F, 0x0B, 0x5E, + 0x85, 0xE5, 0xC2, 0x57, 0x63, 0xCA, 0x3D, 0x6C, 0xB4, 0xC5, 0xCC, 0x70, + 0xB2, 0x91, 0x59, 0x0D, 0x47, 0x20, 0xC8, 0x4F, 0x58, 0xE0, 0x01, 0xE2, + 0x16, 0x38, 0xC4, 0x6F, 0x3B, 0x0F, 0x65, 0x46, 0xBE, 0x7E, 0x2D, 0x7B, + 0x82, 0xF9, 0x40, 0xB5, 0x1D, 0x73, 0xF8, 0xEB, 0x26, 0xC7, 0x87, 0x97, + 0x25, 0x54, 0xB1, 0x28, 0xAA, 0x98, 0x9D, 0xA5, 0x64, 0x6D, 0x7A, 0xD4, + 0x10, 0x81, 0x44, 0xEF, 0x49, 0xD6, 0xAE, 0x2E, 0xDD, 0x76, 0x5C, 0x2F, + 0xA7, 0x1C, 0xC9, 0x09, 0x69, 0x9A, 0x83, 0xCF, 0x29, 0x39, 0xB9, 0xE9, + 0x4C, 0xFF, 0x43, 0xAB }; + + if(ekb < 256) + return EKB[ekb]; + else + throw Encoding_Error("RC2::EKB_code: EKB is too large"); + } + +} diff --git a/botan/src/block/rc2/rc2.h b/botan/src/block/rc2/rc2.h new file mode 100644 index 0000000..cb6f58f --- /dev/null +++ b/botan/src/block/rc2/rc2.h @@ -0,0 +1,37 @@ +/* +* RC2 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_RC2_H__ +#define BOTAN_RC2_H__ + +#include <botan/block_cipher.h> + +namespace Botan { + +/* +* RC2 +*/ +class BOTAN_DLL RC2 : public BlockCipher + { + public: + static byte EKB_code(u32bit); + + void clear() throw() { K.clear(); } + std::string name() const { return "RC2"; } + BlockCipher* clone() const { return new RC2; } + RC2() : BlockCipher(8, 1, 32) {} + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key_schedule(const byte[], u32bit); + + SecureBuffer<u16bit, 64> K; + }; + +} + +#endif diff --git a/botan/src/block/rc5/info.txt b/botan/src/block/rc5/info.txt new file mode 100644 index 0000000..4a150c3 --- /dev/null +++ b/botan/src/block/rc5/info.txt @@ -0,0 +1,10 @@ +realname "RC5" + +define RC5 + +load_on auto + +<add> +rc5.cpp +rc5.h +</add> diff --git a/botan/src/block/rc5/rc5.cpp b/botan/src/block/rc5/rc5.cpp new file mode 100644 index 0000000..5d83d5a --- /dev/null +++ b/botan/src/block/rc5/rc5.cpp @@ -0,0 +1,103 @@ +/* +* RC5 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/rc5.h> +#include <botan/loadstor.h> +#include <botan/rotate.h> +#include <botan/parsing.h> +#include <algorithm> + +namespace Botan { + +/* +* RC5 Encryption +*/ +void RC5::enc(const byte in[], byte out[]) const + { + u32bit A = load_le<u32bit>(in, 0), B = load_le<u32bit>(in, 1); + + A += S[0]; B += S[1]; + for(u32bit j = 0; j != ROUNDS; j += 4) + { + A = rotate_left(A ^ B, B % 32) + S[2*j+2]; + B = rotate_left(B ^ A, A % 32) + S[2*j+3]; + A = rotate_left(A ^ B, B % 32) + S[2*j+4]; + B = rotate_left(B ^ A, A % 32) + S[2*j+5]; + A = rotate_left(A ^ B, B % 32) + S[2*j+6]; + B = rotate_left(B ^ A, A % 32) + S[2*j+7]; + A = rotate_left(A ^ B, B % 32) + S[2*j+8]; + B = rotate_left(B ^ A, A % 32) + S[2*j+9]; + } + + store_le(out, A, B); + } + +/* +* RC5 Decryption +*/ +void RC5::dec(const byte in[], byte out[]) const + { + u32bit A = load_le<u32bit>(in, 0), B = load_le<u32bit>(in, 1); + + for(u32bit j = ROUNDS; j != 0; j -= 4) + { + B = rotate_right(B - S[2*j+1], A % 32) ^ A; + A = rotate_right(A - S[2*j ], B % 32) ^ B; + B = rotate_right(B - S[2*j-1], A % 32) ^ A; + A = rotate_right(A - S[2*j-2], B % 32) ^ B; + B = rotate_right(B - S[2*j-3], A % 32) ^ A; + A = rotate_right(A - S[2*j-4], B % 32) ^ B; + B = rotate_right(B - S[2*j-5], A % 32) ^ A; + A = rotate_right(A - S[2*j-6], B % 32) ^ B; + } + B -= S[1]; A -= S[0]; + + store_le(out, A, B); + } + +/* +* RC5 Key Schedule +*/ +void RC5::key_schedule(const byte key[], u32bit length) + { + const u32bit WORD_KEYLENGTH = (((length - 1) / 4) + 1), + MIX_ROUNDS = 3*std::max(WORD_KEYLENGTH, S.size()); + S[0] = 0xB7E15163; + for(u32bit j = 1; j != S.size(); ++j) + S[j] = S[j-1] + 0x9E3779B9; + + SecureBuffer<u32bit, 8> K; + for(s32bit j = length-1; j >= 0; --j) + K[j/4] = (K[j/4] << 8) + key[j]; + for(u32bit j = 0, A = 0, B = 0; j != MIX_ROUNDS; ++j) + { + A = rotate_left(S[j % S.size()] + A + B, 3); + B = rotate_left(K[j % WORD_KEYLENGTH] + A + B, (A + B) % 32); + S[j % S.size()] = A; + K[j % WORD_KEYLENGTH] = B; + } + } + +/* +* Return the name of this type +*/ +std::string RC5::name() const + { + return "RC5(" + to_string(ROUNDS) + ")"; + } + +/* +* RC5 Constructor +*/ +RC5::RC5(u32bit r) : BlockCipher(8, 1, 32), ROUNDS(r) + { + if(ROUNDS < 8 || ROUNDS > 32 || (ROUNDS % 4 != 0)) + throw Invalid_Argument(name() + ": Invalid number of rounds"); + S.create(2*ROUNDS + 2); + } + +} diff --git a/botan/src/block/rc5/rc5.h b/botan/src/block/rc5/rc5.h new file mode 100644 index 0000000..0832247 --- /dev/null +++ b/botan/src/block/rc5/rc5.h @@ -0,0 +1,35 @@ +/* +* RC5 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_RC5_H__ +#define BOTAN_RC5_H__ + +#include <botan/block_cipher.h> + +namespace Botan { + +/* +* RC5 +*/ +class BOTAN_DLL RC5 : public BlockCipher + { + public: + void clear() throw() { S.clear(); } + std::string name() const; + BlockCipher* clone() const { return new RC5(ROUNDS); } + RC5(u32bit); + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key_schedule(const byte[], u32bit); + SecureVector<u32bit> S; + const u32bit ROUNDS; + }; + +} + +#endif diff --git a/botan/src/block/rc6/info.txt b/botan/src/block/rc6/info.txt new file mode 100644 index 0000000..1457e78 --- /dev/null +++ b/botan/src/block/rc6/info.txt @@ -0,0 +1,10 @@ +realname "RC6" + +define RC6 + +load_on auto + +<add> +rc6.cpp +rc6.h +</add> diff --git a/botan/src/block/rc6/rc6.cpp b/botan/src/block/rc6/rc6.cpp new file mode 100644 index 0000000..3b30ea9 --- /dev/null +++ b/botan/src/block/rc6/rc6.cpp @@ -0,0 +1,122 @@ +/* +* RC6 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/rc6.h> +#include <botan/loadstor.h> +#include <botan/rotate.h> +#include <algorithm> + +namespace Botan { + +/* +* RC6 Encryption +*/ +void RC6::enc(const byte in[], byte out[]) const + { + u32bit A = load_le<u32bit>(in, 0); + u32bit B = load_le<u32bit>(in, 1); + u32bit C = load_le<u32bit>(in, 2); + u32bit D = load_le<u32bit>(in, 3); + + B += S[0]; D += S[1]; + + for(u32bit j = 0; j != 20; j += 4) + { + u32bit T1, T2; + + T1 = rotate_left(B*(2*B+1), 5); + T2 = rotate_left(D*(2*D+1), 5); + A = rotate_left(A ^ T1, T2 % 32) + S[2*j+2]; + C = rotate_left(C ^ T2, T1 % 32) + S[2*j+3]; + + T1 = rotate_left(C*(2*C+1), 5); + T2 = rotate_left(A*(2*A+1), 5); + B = rotate_left(B ^ T1, T2 % 32) + S[2*j+4]; + D = rotate_left(D ^ T2, T1 % 32) + S[2*j+5]; + + T1 = rotate_left(D*(2*D+1), 5); + T2 = rotate_left(B*(2*B+1), 5); + C = rotate_left(C ^ T1, T2 % 32) + S[2*j+6]; + A = rotate_left(A ^ T2, T1 % 32) + S[2*j+7]; + + T1 = rotate_left(A*(2*A+1), 5); + T2 = rotate_left(C*(2*C+1), 5); + D = rotate_left(D ^ T1, T2 % 32) + S[2*j+8]; + B = rotate_left(B ^ T2, T1 % 32) + S[2*j+9]; + } + + A += S[42]; C += S[43]; + + store_le(out, A, B, C, D); + } + +/* +* RC6 Decryption +*/ +void RC6::dec(const byte in[], byte out[]) const + { + u32bit A = load_le<u32bit>(in, 0); + u32bit B = load_le<u32bit>(in, 1); + u32bit C = load_le<u32bit>(in, 2); + u32bit D = load_le<u32bit>(in, 3); + + C -= S[43]; A -= S[42]; + + for(u32bit j = 0; j != 20; j += 4) + { + u32bit T1, T2; + + T1 = rotate_left(A*(2*A+1), 5); + T2 = rotate_left(C*(2*C+1), 5); + B = rotate_right(B - S[41 - 2*j], T1 % 32) ^ T2; + D = rotate_right(D - S[40 - 2*j], T2 % 32) ^ T1; + + T1 = rotate_left(D*(2*D+1), 5); + T2 = rotate_left(B*(2*B+1), 5); + A = rotate_right(A - S[39 - 2*j], T1 % 32) ^ T2; + C = rotate_right(C - S[38 - 2*j], T2 % 32) ^ T1; + + T1 = rotate_left(C*(2*C+1), 5); + T2 = rotate_left(A*(2*A+1), 5); + D = rotate_right(D - S[37 - 2*j], T1 % 32) ^ T2; + B = rotate_right(B - S[36 - 2*j], T2 % 32) ^ T1; + + T1 = rotate_left(B*(2*B+1), 5); + T2 = rotate_left(D*(2*D+1), 5); + C = rotate_right(C - S[35 - 2*j], T1 % 32) ^ T2; + A = rotate_right(A - S[34 - 2*j], T2 % 32) ^ T1; + } + + D -= S[1]; B -= S[0]; + + store_le(out, A, B, C, D); + } + +/* +* RC6 Key Schedule +*/ +void RC6::key_schedule(const byte key[], u32bit length) + { + const u32bit WORD_KEYLENGTH = (((length - 1) / 4) + 1), + MIX_ROUNDS = 3*std::max(WORD_KEYLENGTH, S.size()); + S[0] = 0xB7E15163; + for(u32bit j = 1; j != S.size(); ++j) + S[j] = S[j-1] + 0x9E3779B9; + + SecureBuffer<u32bit, 8> K; + for(s32bit j = length-1; j >= 0; --j) + K[j/4] = (K[j/4] << 8) + key[j]; + for(u32bit j = 0, A = 0, B = 0; j != MIX_ROUNDS; ++j) + { + A = rotate_left(S[j % S.size()] + A + B, 3); + B = rotate_left(K[j % WORD_KEYLENGTH] + A + B, (A + B) % 32); + S[j % S.size()] = A; + K[j % WORD_KEYLENGTH] = B; + } + } + +} diff --git a/botan/src/block/rc6/rc6.h b/botan/src/block/rc6/rc6.h new file mode 100644 index 0000000..cb2800b --- /dev/null +++ b/botan/src/block/rc6/rc6.h @@ -0,0 +1,35 @@ +/* +* RC6 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_RC6_H__ +#define BOTAN_RC6_H__ + +#include <botan/block_cipher.h> + +namespace Botan { + +/* +* RC6 +*/ +class BOTAN_DLL RC6 : public BlockCipher + { + public: + void clear() throw() { S.clear(); } + std::string name() const { return "RC6"; } + BlockCipher* clone() const { return new RC6; } + RC6() : BlockCipher(16, 1, 32) {} + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key_schedule(const byte[], u32bit); + + SecureBuffer<u32bit, 44> S; + }; + +} + +#endif diff --git a/botan/src/block/safer/info.txt b/botan/src/block/safer/info.txt new file mode 100644 index 0000000..973cbff --- /dev/null +++ b/botan/src/block/safer/info.txt @@ -0,0 +1,11 @@ +realname "SAFER" + +define SAFER + +load_on auto + +<add> +safe_tab.cpp +safer_sk.cpp +safer_sk.h +</add> diff --git a/botan/src/block/safer/safe_tab.cpp b/botan/src/block/safer/safe_tab.cpp new file mode 100644 index 0000000..e265a40 --- /dev/null +++ b/botan/src/block/safer/safe_tab.cpp @@ -0,0 +1,121 @@ +/* +* S-Box Tables for SAFER-SK +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/safer_sk.h> + +namespace Botan { + +const byte SAFER_SK::EXP[256] = { + 0x01, 0x2D, 0xE2, 0x93, 0xBE, 0x45, 0x15, 0xAE, 0x78, 0x03, 0x87, 0xA4, + 0xB8, 0x38, 0xCF, 0x3F, 0x08, 0x67, 0x09, 0x94, 0xEB, 0x26, 0xA8, 0x6B, + 0xBD, 0x18, 0x34, 0x1B, 0xBB, 0xBF, 0x72, 0xF7, 0x40, 0x35, 0x48, 0x9C, + 0x51, 0x2F, 0x3B, 0x55, 0xE3, 0xC0, 0x9F, 0xD8, 0xD3, 0xF3, 0x8D, 0xB1, + 0xFF, 0xA7, 0x3E, 0xDC, 0x86, 0x77, 0xD7, 0xA6, 0x11, 0xFB, 0xF4, 0xBA, + 0x92, 0x91, 0x64, 0x83, 0xF1, 0x33, 0xEF, 0xDA, 0x2C, 0xB5, 0xB2, 0x2B, + 0x88, 0xD1, 0x99, 0xCB, 0x8C, 0x84, 0x1D, 0x14, 0x81, 0x97, 0x71, 0xCA, + 0x5F, 0xA3, 0x8B, 0x57, 0x3C, 0x82, 0xC4, 0x52, 0x5C, 0x1C, 0xE8, 0xA0, + 0x04, 0xB4, 0x85, 0x4A, 0xF6, 0x13, 0x54, 0xB6, 0xDF, 0x0C, 0x1A, 0x8E, + 0xDE, 0xE0, 0x39, 0xFC, 0x20, 0x9B, 0x24, 0x4E, 0xA9, 0x98, 0x9E, 0xAB, + 0xF2, 0x60, 0xD0, 0x6C, 0xEA, 0xFA, 0xC7, 0xD9, 0x00, 0xD4, 0x1F, 0x6E, + 0x43, 0xBC, 0xEC, 0x53, 0x89, 0xFE, 0x7A, 0x5D, 0x49, 0xC9, 0x32, 0xC2, + 0xF9, 0x9A, 0xF8, 0x6D, 0x16, 0xDB, 0x59, 0x96, 0x44, 0xE9, 0xCD, 0xE6, + 0x46, 0x42, 0x8F, 0x0A, 0xC1, 0xCC, 0xB9, 0x65, 0xB0, 0xD2, 0xC6, 0xAC, + 0x1E, 0x41, 0x62, 0x29, 0x2E, 0x0E, 0x74, 0x50, 0x02, 0x5A, 0xC3, 0x25, + 0x7B, 0x8A, 0x2A, 0x5B, 0xF0, 0x06, 0x0D, 0x47, 0x6F, 0x70, 0x9D, 0x7E, + 0x10, 0xCE, 0x12, 0x27, 0xD5, 0x4C, 0x4F, 0xD6, 0x79, 0x30, 0x68, 0x36, + 0x75, 0x7D, 0xE4, 0xED, 0x80, 0x6A, 0x90, 0x37, 0xA2, 0x5E, 0x76, 0xAA, + 0xC5, 0x7F, 0x3D, 0xAF, 0xA5, 0xE5, 0x19, 0x61, 0xFD, 0x4D, 0x7C, 0xB7, + 0x0B, 0xEE, 0xAD, 0x4B, 0x22, 0xF5, 0xE7, 0x73, 0x23, 0x21, 0xC8, 0x05, + 0xE1, 0x66, 0xDD, 0xB3, 0x58, 0x69, 0x63, 0x56, 0x0F, 0xA1, 0x31, 0x95, + 0x17, 0x07, 0x3A, 0x28 }; + +const byte SAFER_SK::LOG[512] = { + 0x80, 0x00, 0xB0, 0x09, 0x60, 0xEF, 0xB9, 0xFD, 0x10, 0x12, 0x9F, 0xE4, + 0x69, 0xBA, 0xAD, 0xF8, 0xC0, 0x38, 0xC2, 0x65, 0x4F, 0x06, 0x94, 0xFC, + 0x19, 0xDE, 0x6A, 0x1B, 0x5D, 0x4E, 0xA8, 0x82, 0x70, 0xED, 0xE8, 0xEC, + 0x72, 0xB3, 0x15, 0xC3, 0xFF, 0xAB, 0xB6, 0x47, 0x44, 0x01, 0xAC, 0x25, + 0xC9, 0xFA, 0x8E, 0x41, 0x1A, 0x21, 0xCB, 0xD3, 0x0D, 0x6E, 0xFE, 0x26, + 0x58, 0xDA, 0x32, 0x0F, 0x20, 0xA9, 0x9D, 0x84, 0x98, 0x05, 0x9C, 0xBB, + 0x22, 0x8C, 0x63, 0xE7, 0xC5, 0xE1, 0x73, 0xC6, 0xAF, 0x24, 0x5B, 0x87, + 0x66, 0x27, 0xF7, 0x57, 0xF4, 0x96, 0xB1, 0xB7, 0x5C, 0x8B, 0xD5, 0x54, + 0x79, 0xDF, 0xAA, 0xF6, 0x3E, 0xA3, 0xF1, 0x11, 0xCA, 0xF5, 0xD1, 0x17, + 0x7B, 0x93, 0x83, 0xBC, 0xBD, 0x52, 0x1E, 0xEB, 0xAE, 0xCC, 0xD6, 0x35, + 0x08, 0xC8, 0x8A, 0xB4, 0xE2, 0xCD, 0xBF, 0xD9, 0xD0, 0x50, 0x59, 0x3F, + 0x4D, 0x62, 0x34, 0x0A, 0x48, 0x88, 0xB5, 0x56, 0x4C, 0x2E, 0x6B, 0x9E, + 0xD2, 0x3D, 0x3C, 0x03, 0x13, 0xFB, 0x97, 0x51, 0x75, 0x4A, 0x91, 0x71, + 0x23, 0xBE, 0x76, 0x2A, 0x5F, 0xF9, 0xD4, 0x55, 0x0B, 0xDC, 0x37, 0x31, + 0x16, 0x74, 0xD7, 0x77, 0xA7, 0xE6, 0x07, 0xDB, 0xA4, 0x2F, 0x46, 0xF3, + 0x61, 0x45, 0x67, 0xE3, 0x0C, 0xA2, 0x3B, 0x1C, 0x85, 0x18, 0x04, 0x1D, + 0x29, 0xA0, 0x8F, 0xB2, 0x5A, 0xD8, 0xA6, 0x7E, 0xEE, 0x8D, 0x53, 0x4B, + 0xA1, 0x9A, 0xC1, 0x0E, 0x7A, 0x49, 0xA5, 0x2C, 0x81, 0xC4, 0xC7, 0x36, + 0x2B, 0x7F, 0x43, 0x95, 0x33, 0xF2, 0x6C, 0x68, 0x6D, 0xF0, 0x02, 0x28, + 0xCE, 0xDD, 0x9B, 0xEA, 0x5E, 0x99, 0x7C, 0x14, 0x86, 0xCF, 0xE5, 0x42, + 0xB8, 0x40, 0x78, 0x2D, 0x3A, 0xE9, 0x64, 0x1F, 0x92, 0x90, 0x7D, 0x39, + 0x6F, 0xE0, 0x89, 0x30, 0x80, 0x00, 0xB0, 0x09, 0x60, 0xEF, 0xB9, 0xFD, + 0x10, 0x12, 0x9F, 0xE4, 0x69, 0xBA, 0xAD, 0xF8, 0xC0, 0x38, 0xC2, 0x65, + 0x4F, 0x06, 0x94, 0xFC, 0x19, 0xDE, 0x6A, 0x1B, 0x5D, 0x4E, 0xA8, 0x82, + 0x70, 0xED, 0xE8, 0xEC, 0x72, 0xB3, 0x15, 0xC3, 0xFF, 0xAB, 0xB6, 0x47, + 0x44, 0x01, 0xAC, 0x25, 0xC9, 0xFA, 0x8E, 0x41, 0x1A, 0x21, 0xCB, 0xD3, + 0x0D, 0x6E, 0xFE, 0x26, 0x58, 0xDA, 0x32, 0x0F, 0x20, 0xA9, 0x9D, 0x84, + 0x98, 0x05, 0x9C, 0xBB, 0x22, 0x8C, 0x63, 0xE7, 0xC5, 0xE1, 0x73, 0xC6, + 0xAF, 0x24, 0x5B, 0x87, 0x66, 0x27, 0xF7, 0x57, 0xF4, 0x96, 0xB1, 0xB7, + 0x5C, 0x8B, 0xD5, 0x54, 0x79, 0xDF, 0xAA, 0xF6, 0x3E, 0xA3, 0xF1, 0x11, + 0xCA, 0xF5, 0xD1, 0x17, 0x7B, 0x93, 0x83, 0xBC, 0xBD, 0x52, 0x1E, 0xEB, + 0xAE, 0xCC, 0xD6, 0x35, 0x08, 0xC8, 0x8A, 0xB4, 0xE2, 0xCD, 0xBF, 0xD9, + 0xD0, 0x50, 0x59, 0x3F, 0x4D, 0x62, 0x34, 0x0A, 0x48, 0x88, 0xB5, 0x56, + 0x4C, 0x2E, 0x6B, 0x9E, 0xD2, 0x3D, 0x3C, 0x03, 0x13, 0xFB, 0x97, 0x51, + 0x75, 0x4A, 0x91, 0x71, 0x23, 0xBE, 0x76, 0x2A, 0x5F, 0xF9, 0xD4, 0x55, + 0x0B, 0xDC, 0x37, 0x31, 0x16, 0x74, 0xD7, 0x77, 0xA7, 0xE6, 0x07, 0xDB, + 0xA4, 0x2F, 0x46, 0xF3, 0x61, 0x45, 0x67, 0xE3, 0x0C, 0xA2, 0x3B, 0x1C, + 0x85, 0x18, 0x04, 0x1D, 0x29, 0xA0, 0x8F, 0xB2, 0x5A, 0xD8, 0xA6, 0x7E, + 0xEE, 0x8D, 0x53, 0x4B, 0xA1, 0x9A, 0xC1, 0x0E, 0x7A, 0x49, 0xA5, 0x2C, + 0x81, 0xC4, 0xC7, 0x36, 0x2B, 0x7F, 0x43, 0x95, 0x33, 0xF2, 0x6C, 0x68, + 0x6D, 0xF0, 0x02, 0x28, 0xCE, 0xDD, 0x9B, 0xEA, 0x5E, 0x99, 0x7C, 0x14, + 0x86, 0xCF, 0xE5, 0x42, 0xB8, 0x40, 0x78, 0x2D, 0x3A, 0xE9, 0x64, 0x1F, + 0x92, 0x90, 0x7D, 0x39, 0x6F, 0xE0, 0x89, 0x30 }; + +const byte SAFER_SK::BIAS[208] = { + 0x16, 0x73, 0x3B, 0x1E, 0x8E, 0x70, 0xBD, 0x86, 0x47, 0x7E, 0x24, 0x56, + 0xF1, 0x77, 0x88, 0x46, 0xB1, 0xBA, 0xA3, 0xB7, 0x10, 0x0A, 0xC5, 0x37, + 0xC9, 0x5A, 0x28, 0xAC, 0x64, 0xA5, 0xEC, 0xAB, 0xC6, 0x67, 0x95, 0x58, + 0x0D, 0xF8, 0x9A, 0xF6, 0x66, 0xDC, 0x05, 0x3D, 0xD3, 0x8A, 0xC3, 0xD8, + 0x6A, 0xE9, 0x36, 0x49, 0x43, 0xBF, 0xEB, 0xD4, 0x9B, 0x68, 0xA0, 0x65, + 0x5D, 0x57, 0x92, 0x1F, 0x71, 0x5C, 0xBB, 0x22, 0xC1, 0xBE, 0x7B, 0xBC, + 0x63, 0x94, 0x5F, 0x2A, 0x61, 0xB8, 0x34, 0x32, 0xFD, 0xFB, 0x17, 0x40, + 0xE6, 0x51, 0x1D, 0x41, 0x8F, 0x29, 0xDD, 0x04, 0x80, 0xDE, 0xE7, 0x31, + 0x7F, 0x01, 0xA2, 0xF7, 0x39, 0xDA, 0x6F, 0x23, 0xFE, 0x3A, 0xD0, 0x1C, + 0xD1, 0x30, 0x3E, 0x12, 0xCD, 0x0F, 0xE0, 0xA8, 0xAF, 0x82, 0x59, 0x2C, + 0x7D, 0xAD, 0xB2, 0xEF, 0xC2, 0x87, 0xCE, 0x75, 0x13, 0x02, 0x90, 0x4F, + 0x2E, 0x72, 0x33, 0x85, 0x8D, 0xCF, 0xA9, 0x81, 0xE2, 0xC4, 0x27, 0x2F, + 0x7A, 0x9F, 0x52, 0xE1, 0x15, 0x38, 0x2B, 0xFC, 0x42, 0xC7, 0x08, 0xE4, + 0x09, 0x55, 0x5E, 0x8C, 0x76, 0x60, 0xFF, 0xDF, 0xD7, 0x98, 0xFA, 0x0B, + 0x00, 0x1A, 0xF9, 0xA6, 0xB9, 0xE8, 0x9E, 0x62, 0xD9, 0x91, 0x50, 0xD2, + 0xEE, 0x18, 0xB4, 0x07, 0xEA, 0x5B, 0xA4, 0xC8, 0x0E, 0xCB, 0x48, 0x69, + 0x4E, 0x9C, 0x35, 0x79, 0x45, 0x4D, 0x54, 0xE5, 0x3C, 0x0C, 0x4A, 0x8B, + 0x3F, 0xCC, 0xA7, 0xDB }; + +const byte SAFER_SK::KEY_INDEX[208] = { + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x0B, 0x0C, 0x0D, 0x0E, + 0x0F, 0x10, 0x11, 0x09, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x01, + 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x09, 0x0A, 0x0B, 0x05, 0x06, 0x07, 0x08, + 0x00, 0x01, 0x02, 0x03, 0x0F, 0x10, 0x11, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, + 0x07, 0x08, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x11, 0x09, 0x0A, 0x0B, + 0x0C, 0x0D, 0x0E, 0x0F, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x02, 0x03, 0x04, 0x05, + 0x06, 0x07, 0x08, 0x00, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x09, 0x0A, + 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x01, 0x02, 0x0E, 0x0F, 0x10, 0x11, + 0x09, 0x0A, 0x0B, 0x0C, 0x06, 0x07, 0x08, 0x00, 0x01, 0x02, 0x03, 0x04, + 0x10, 0x11, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x08, 0x00, 0x01, 0x02, + 0x03, 0x04, 0x05, 0x06, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x0B, 0x0C, 0x0D, 0x0E, + 0x0F, 0x10, 0x11, 0x09, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x01, + 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x09, 0x0A, 0x0B, 0x05, 0x06, 0x07, 0x08, + 0x00, 0x01, 0x02, 0x03, 0x0F, 0x10, 0x11, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, + 0x07, 0x08, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x11, 0x09, 0x0A, 0x0B, + 0x0C, 0x0D, 0x0E, 0x0F }; + +} diff --git a/botan/src/block/safer/safer_sk.cpp b/botan/src/block/safer/safer_sk.cpp new file mode 100644 index 0000000..f72c477 --- /dev/null +++ b/botan/src/block/safer/safer_sk.cpp @@ -0,0 +1,115 @@ +/* +* SAFER-SK +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/safer_sk.h> +#include <botan/rotate.h> +#include <botan/parsing.h> +#include <botan/rotate.h> + +namespace Botan { + +/* +* SAFER-SK Encryption +*/ +void SAFER_SK::enc(const byte in[], byte out[]) const + { + byte A = in[0], B = in[1], C = in[2], D = in[3], + E = in[4], F = in[5], G = in[6], H = in[7], X, Y; + for(u32bit j = 0; j != 16*ROUNDS; j += 16) + { + A = EXP[A ^ EK[j ]]; B = LOG[B + EK[j+1]]; + C = LOG[C + EK[j+2]]; D = EXP[D ^ EK[j+3]]; + E = EXP[E ^ EK[j+4]]; F = LOG[F + EK[j+5]]; + G = LOG[G + EK[j+6]]; H = EXP[H ^ EK[j+7]]; + A += EK[j+ 8]; B ^= EK[j+ 9]; C ^= EK[j+10]; D += EK[j+11]; + E += EK[j+12]; F ^= EK[j+13]; G ^= EK[j+14]; H += EK[j+15]; + B += A; D += C; F += E; H += G; A += B; C += D; E += F; G += H; + C += A; G += E; D += B; H += F; A += C; E += G; B += D; F += H; + H += D; Y = D + H; D = B + F; X = B + D; B = A + E; + A += B; F = C + G; E = C + F; C = X; G = Y; + } + out[0] = A ^ EK[16*ROUNDS+0]; out[1] = B + EK[16*ROUNDS+1]; + out[2] = C + EK[16*ROUNDS+2]; out[3] = D ^ EK[16*ROUNDS+3]; + out[4] = E ^ EK[16*ROUNDS+4]; out[5] = F + EK[16*ROUNDS+5]; + out[6] = G + EK[16*ROUNDS+6]; out[7] = H ^ EK[16*ROUNDS+7]; + } + +/* +* SAFER-SK Decryption +*/ +void SAFER_SK::dec(const byte in[], byte out[]) const + { + byte A = in[0], B = in[1], C = in[2], D = in[3], + E = in[4], F = in[5], G = in[6], H = in[7]; + A ^= EK[16*ROUNDS+0]; B -= EK[16*ROUNDS+1]; C -= EK[16*ROUNDS+2]; + D ^= EK[16*ROUNDS+3]; E ^= EK[16*ROUNDS+4]; F -= EK[16*ROUNDS+5]; + G -= EK[16*ROUNDS+6]; H ^= EK[16*ROUNDS+7]; + for(s32bit j = 16*(ROUNDS-1); j >= 0; j -= 16) + { + byte T = E; E = B; B = C; C = T; T = F; F = D; D = G; G = T; + A -= E; B -= F; C -= G; D -= H; E -= A; F -= B; G -= C; H -= D; + A -= C; E -= G; B -= D; F -= H; C -= A; G -= E; D -= B; H -= F; + A -= B; C -= D; E -= F; G -= H; B -= A; D -= C; F -= E; H -= G; + A = LOG[A - EK[j+8 ] + 256]; B = EXP[B ^ EK[j+9 ]]; + C = EXP[C ^ EK[j+10]]; D = LOG[D - EK[j+11] + 256]; + E = LOG[E - EK[j+12] + 256]; F = EXP[F ^ EK[j+13]]; + G = EXP[G ^ EK[j+14]]; H = LOG[H - EK[j+15] + 256]; + A ^= EK[j+0]; B -= EK[j+1]; C -= EK[j+2]; D ^= EK[j+3]; + E ^= EK[j+4]; F -= EK[j+5]; G -= EK[j+6]; H ^= EK[j+7]; + } + out[0] = A; out[1] = B; out[2] = C; out[3] = D; + out[4] = E; out[5] = F; out[6] = G; out[7] = H; + } + +/* +* SAFER-SK Key Schedule +*/ +void SAFER_SK::key_schedule(const byte key[], u32bit) + { + SecureBuffer<byte, 18> KB; + + for(u32bit j = 0; j != 8; ++j) + { + KB[ 8] ^= KB[j] = rotate_left(key[j], 5); + KB[17] ^= KB[j+9] = EK[j] = key[j+8]; + } + for(u32bit j = 0; j != ROUNDS; ++j) + { + for(u32bit k = 0; k != 18; ++k) + KB[k] = rotate_left(KB[k], 6); + for(u32bit k = 0; k != 16; ++k) + EK[16*j+k+8] = KB[KEY_INDEX[16*j+k]] + BIAS[16*j+k]; + } + } + +/* +* Return the name of this type +*/ +std::string SAFER_SK::name() const + { + return "SAFER-SK(" + to_string(ROUNDS) + ")"; + } + +/* +* Return a clone of this object +*/ +BlockCipher* SAFER_SK::clone() const + { + return new SAFER_SK(ROUNDS); + } + +/* +* SAFER-SK Constructor +*/ +SAFER_SK::SAFER_SK(u32bit rounds) : BlockCipher(8, 16), + EK(16 * rounds + 8), ROUNDS(rounds) + { + if(ROUNDS > 13 || ROUNDS == 0) + throw Invalid_Argument(name() + ": Invalid number of rounds"); + } + +} diff --git a/botan/src/block/safer/safer_sk.h b/botan/src/block/safer/safer_sk.h new file mode 100644 index 0000000..e52c583 --- /dev/null +++ b/botan/src/block/safer/safer_sk.h @@ -0,0 +1,40 @@ +/* +* SAFER-SK +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_SAFER_SK_H__ +#define BOTAN_SAFER_SK_H__ + +#include <botan/block_cipher.h> + +namespace Botan { + +/* +* SAFER-SK +*/ +class BOTAN_DLL SAFER_SK : public BlockCipher + { + public: + void clear() throw() { EK.clear(); } + std::string name() const; + BlockCipher* clone() const; + SAFER_SK(u32bit); + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key_schedule(const byte[], u32bit); + + static const byte EXP[256]; + static const byte LOG[512]; + static const byte BIAS[208]; + static const byte KEY_INDEX[208]; + SecureVector<byte> EK; + const u32bit ROUNDS; + }; + +} + +#endif diff --git a/botan/src/block/seed/info.txt b/botan/src/block/seed/info.txt new file mode 100644 index 0000000..d049793 --- /dev/null +++ b/botan/src/block/seed/info.txt @@ -0,0 +1,11 @@ +realname "SEED" + +define SEED + +load_on auto + +<add> +seed.cpp +seed.h +seed_tab.cpp +</add> diff --git a/botan/src/block/seed/seed.cpp b/botan/src/block/seed/seed.cpp new file mode 100644 index 0000000..b06a7cd --- /dev/null +++ b/botan/src/block/seed/seed.cpp @@ -0,0 +1,127 @@ +/* +* SEED +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/seed.h> +#include <botan/loadstor.h> + +namespace Botan { + +/* +* SEED G Function +*/ +u32bit SEED::G_FUNC::operator()(u32bit X) const + { + return (S0[get_byte(3, X)] ^ S1[get_byte(2, X)] ^ + S2[get_byte(1, X)] ^ S3[get_byte(0, X)]); + } + +/* +* SEED Encryption +*/ +void SEED::enc(const byte in[], byte out[]) const + { + u32bit B0 = load_be<u32bit>(in, 0); + u32bit B1 = load_be<u32bit>(in, 1); + u32bit B2 = load_be<u32bit>(in, 2); + u32bit B3 = load_be<u32bit>(in, 3); + + G_FUNC G; + + for(u32bit j = 0; j != 16; j += 2) + { + u32bit T0, T1; + + T0 = B2 ^ K[2*j]; + T1 = G(B2 ^ B3 ^ K[2*j+1]); + T0 = G(T1 + T0); + T1 = G(T1 + T0); + B1 ^= T1; + B0 ^= T0 + T1; + + T0 = B0 ^ K[2*j+2]; + T1 = G(B0 ^ B1 ^ K[2*j+3]); + T0 = G(T1 + T0); + T1 = G(T1 + T0); + B3 ^= T1; + B2 ^= T0 + T1; + } + + store_be(out, B2, B3, B0, B1); + } + +/* +* SEED Decryption +*/ +void SEED::dec(const byte in[], byte out[]) const + { + u32bit B0 = load_be<u32bit>(in, 0); + u32bit B1 = load_be<u32bit>(in, 1); + u32bit B2 = load_be<u32bit>(in, 2); + u32bit B3 = load_be<u32bit>(in, 3); + + G_FUNC G; + + for(u32bit j = 0; j != 16; j += 2) + { + u32bit T0, T1; + + T0 = B2 ^ K[30-2*j]; + T1 = G(B2 ^ B3 ^ K[31-2*j]); + T0 = G(T1 + T0); + T1 = G(T1 + T0); + B1 ^= T1; + B0 ^= T0 + T1; + + T0 = B0 ^ K[28-2*j]; + T1 = G(B0 ^ B1 ^ K[29-2*j]); + T0 = G(T1 + T0); + T1 = G(T1 + T0); + B3 ^= T1; + B2 ^= T0 + T1; + } + + store_be(out, B2, B3, B0, B1); + } + +/* +* SEED Key Schedule +*/ +void SEED::key_schedule(const byte key[], u32bit) + { + const u32bit RC[16] = { + 0x9E3779B9, 0x3C6EF373, 0x78DDE6E6, 0xF1BBCDCC, + 0xE3779B99, 0xC6EF3733, 0x8DDE6E67, 0x1BBCDCCF, + 0x3779B99E, 0x6EF3733C, 0xDDE6E678, 0xBBCDCCF1, + 0x779B99E3, 0xEF3733C6, 0xDE6E678D, 0xBCDCCF1B + }; + + SecureBuffer<u32bit, 4> WK; + + for(u32bit j = 0; j != 4; ++j) + WK[j] = load_be<u32bit>(key, j); + + G_FUNC G; + + for(u32bit j = 0; j != 16; j += 2) + { + K[2*j ] = G(WK[0] + WK[2] - RC[j]); + K[2*j+1] = G(WK[1] - WK[3] + RC[j]) ^ K[2*j]; + + byte T = get_byte(3, WK[0]); + WK[0] = (WK[0] >> 8) | (get_byte(3, WK[1]) << 24); + WK[1] = (WK[1] >> 8) | (T << 24); + + K[2*j+2] = G(WK[0] + WK[2] - RC[j+1]); + K[2*j+3] = G(WK[1] - WK[3] + RC[j+1]) ^ K[2*j+2]; + + T = get_byte(0, WK[3]); + WK[3] = (WK[3] << 8) | get_byte(0, WK[2]); + WK[2] = (WK[2] << 8) | T; + } + } + +} diff --git a/botan/src/block/seed/seed.h b/botan/src/block/seed/seed.h new file mode 100644 index 0000000..54c25d5 --- /dev/null +++ b/botan/src/block/seed/seed.h @@ -0,0 +1,43 @@ +/* +* SEED +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_SEED_H__ +#define BOTAN_SEED_H__ + +#include <botan/block_cipher.h> + +namespace Botan { + +/* +* SEED +*/ +class BOTAN_DLL SEED : public BlockCipher + { + public: + void clear() throw() { K.clear(); } + std::string name() const { return "SEED"; } + BlockCipher* clone() const { return new SEED; } + SEED() : BlockCipher(16, 16) {} + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key_schedule(const byte[], u32bit); + + class G_FUNC + { + public: + u32bit operator()(u32bit) const; + private: + static const u32bit S0[256], S1[256], S2[256], S3[256]; + }; + + SecureBuffer<u32bit, 32> K; + }; + +} + +#endif diff --git a/botan/src/block/seed/seed_tab.cpp b/botan/src/block/seed/seed_tab.cpp new file mode 100644 index 0000000..6ada36e --- /dev/null +++ b/botan/src/block/seed/seed_tab.cpp @@ -0,0 +1,192 @@ +/* +* S-Box Tables for SEED +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/seed.h> + +namespace Botan { + +const u32bit SEED::G_FUNC::S0[256] = { + 0x2989A1A8, 0x05858184, 0x16C6D2D4, 0x13C3D3D0, 0x14445054, 0x1D0D111C, + 0x2C8CA0AC, 0x25052124, 0x1D4D515C, 0x03434340, 0x18081018, 0x1E0E121C, + 0x11415150, 0x3CCCF0FC, 0x0ACAC2C8, 0x23436360, 0x28082028, 0x04444044, + 0x20002020, 0x1D8D919C, 0x20C0E0E0, 0x22C2E2E0, 0x08C8C0C8, 0x17071314, + 0x2585A1A4, 0x0F8F838C, 0x03030300, 0x3B4B7378, 0x3B8BB3B8, 0x13031310, + 0x12C2D2D0, 0x2ECEE2EC, 0x30407070, 0x0C8C808C, 0x3F0F333C, 0x2888A0A8, + 0x32023230, 0x1DCDD1DC, 0x36C6F2F4, 0x34447074, 0x2CCCE0EC, 0x15859194, + 0x0B0B0308, 0x17475354, 0x1C4C505C, 0x1B4B5358, 0x3D8DB1BC, 0x01010100, + 0x24042024, 0x1C0C101C, 0x33437370, 0x18889098, 0x10001010, 0x0CCCC0CC, + 0x32C2F2F0, 0x19C9D1D8, 0x2C0C202C, 0x27C7E3E4, 0x32427270, 0x03838380, + 0x1B8B9398, 0x11C1D1D0, 0x06868284, 0x09C9C1C8, 0x20406060, 0x10405050, + 0x2383A3A0, 0x2BCBE3E8, 0x0D0D010C, 0x3686B2B4, 0x1E8E929C, 0x0F4F434C, + 0x3787B3B4, 0x1A4A5258, 0x06C6C2C4, 0x38487078, 0x2686A2A4, 0x12021210, + 0x2F8FA3AC, 0x15C5D1D4, 0x21416160, 0x03C3C3C0, 0x3484B0B4, 0x01414140, + 0x12425250, 0x3D4D717C, 0x0D8D818C, 0x08080008, 0x1F0F131C, 0x19899198, + 0x00000000, 0x19091118, 0x04040004, 0x13435350, 0x37C7F3F4, 0x21C1E1E0, + 0x3DCDF1FC, 0x36467274, 0x2F0F232C, 0x27072324, 0x3080B0B0, 0x0B8B8388, + 0x0E0E020C, 0x2B8BA3A8, 0x2282A2A0, 0x2E4E626C, 0x13839390, 0x0D4D414C, + 0x29496168, 0x3C4C707C, 0x09090108, 0x0A0A0208, 0x3F8FB3BC, 0x2FCFE3EC, + 0x33C3F3F0, 0x05C5C1C4, 0x07878384, 0x14041014, 0x3ECEF2FC, 0x24446064, + 0x1ECED2DC, 0x2E0E222C, 0x0B4B4348, 0x1A0A1218, 0x06060204, 0x21012120, + 0x2B4B6368, 0x26466264, 0x02020200, 0x35C5F1F4, 0x12829290, 0x0A8A8288, + 0x0C0C000C, 0x3383B3B0, 0x3E4E727C, 0x10C0D0D0, 0x3A4A7278, 0x07474344, + 0x16869294, 0x25C5E1E4, 0x26062224, 0x00808080, 0x2D8DA1AC, 0x1FCFD3DC, + 0x2181A1A0, 0x30003030, 0x37073334, 0x2E8EA2AC, 0x36063234, 0x15051114, + 0x22022220, 0x38083038, 0x34C4F0F4, 0x2787A3A4, 0x05454144, 0x0C4C404C, + 0x01818180, 0x29C9E1E8, 0x04848084, 0x17879394, 0x35053134, 0x0BCBC3C8, + 0x0ECEC2CC, 0x3C0C303C, 0x31417170, 0x11011110, 0x07C7C3C4, 0x09898188, + 0x35457174, 0x3BCBF3F8, 0x1ACAD2D8, 0x38C8F0F8, 0x14849094, 0x19495158, + 0x02828280, 0x04C4C0C4, 0x3FCFF3FC, 0x09494148, 0x39093138, 0x27476364, + 0x00C0C0C0, 0x0FCFC3CC, 0x17C7D3D4, 0x3888B0B8, 0x0F0F030C, 0x0E8E828C, + 0x02424240, 0x23032320, 0x11819190, 0x2C4C606C, 0x1BCBD3D8, 0x2484A0A4, + 0x34043034, 0x31C1F1F0, 0x08484048, 0x02C2C2C0, 0x2F4F636C, 0x3D0D313C, + 0x2D0D212C, 0x00404040, 0x3E8EB2BC, 0x3E0E323C, 0x3C8CB0BC, 0x01C1C1C0, + 0x2A8AA2A8, 0x3A8AB2B8, 0x0E4E424C, 0x15455154, 0x3B0B3338, 0x1CCCD0DC, + 0x28486068, 0x3F4F737C, 0x1C8C909C, 0x18C8D0D8, 0x0A4A4248, 0x16465254, + 0x37477374, 0x2080A0A0, 0x2DCDE1EC, 0x06464244, 0x3585B1B4, 0x2B0B2328, + 0x25456164, 0x3ACAF2F8, 0x23C3E3E0, 0x3989B1B8, 0x3181B1B0, 0x1F8F939C, + 0x1E4E525C, 0x39C9F1F8, 0x26C6E2E4, 0x3282B2B0, 0x31013130, 0x2ACAE2E8, + 0x2D4D616C, 0x1F4F535C, 0x24C4E0E4, 0x30C0F0F0, 0x0DCDC1CC, 0x08888088, + 0x16061214, 0x3A0A3238, 0x18485058, 0x14C4D0D4, 0x22426260, 0x29092128, + 0x07070304, 0x33033330, 0x28C8E0E8, 0x1B0B1318, 0x05050104, 0x39497178, + 0x10809090, 0x2A4A6268, 0x2A0A2228, 0x1A8A9298 }; + +const u32bit SEED::G_FUNC::S1[256] = { + 0x38380830, 0xE828C8E0, 0x2C2D0D21, 0xA42686A2, 0xCC0FCFC3, 0xDC1ECED2, + 0xB03383B3, 0xB83888B0, 0xAC2F8FA3, 0x60204060, 0x54154551, 0xC407C7C3, + 0x44044440, 0x6C2F4F63, 0x682B4B63, 0x581B4B53, 0xC003C3C3, 0x60224262, + 0x30330333, 0xB43585B1, 0x28290921, 0xA02080A0, 0xE022C2E2, 0xA42787A3, + 0xD013C3D3, 0x90118191, 0x10110111, 0x04060602, 0x1C1C0C10, 0xBC3C8CB0, + 0x34360632, 0x480B4B43, 0xEC2FCFE3, 0x88088880, 0x6C2C4C60, 0xA82888A0, + 0x14170713, 0xC404C4C0, 0x14160612, 0xF434C4F0, 0xC002C2C2, 0x44054541, + 0xE021C1E1, 0xD416C6D2, 0x3C3F0F33, 0x3C3D0D31, 0x8C0E8E82, 0x98188890, + 0x28280820, 0x4C0E4E42, 0xF436C6F2, 0x3C3E0E32, 0xA42585A1, 0xF839C9F1, + 0x0C0D0D01, 0xDC1FCFD3, 0xD818C8D0, 0x282B0B23, 0x64264662, 0x783A4A72, + 0x24270723, 0x2C2F0F23, 0xF031C1F1, 0x70324272, 0x40024242, 0xD414C4D0, + 0x40014141, 0xC000C0C0, 0x70334373, 0x64274763, 0xAC2C8CA0, 0x880B8B83, + 0xF437C7F3, 0xAC2D8DA1, 0x80008080, 0x1C1F0F13, 0xC80ACAC2, 0x2C2C0C20, + 0xA82A8AA2, 0x34340430, 0xD012C2D2, 0x080B0B03, 0xEC2ECEE2, 0xE829C9E1, + 0x5C1D4D51, 0x94148490, 0x18180810, 0xF838C8F0, 0x54174753, 0xAC2E8EA2, + 0x08080800, 0xC405C5C1, 0x10130313, 0xCC0DCDC1, 0x84068682, 0xB83989B1, + 0xFC3FCFF3, 0x7C3D4D71, 0xC001C1C1, 0x30310131, 0xF435C5F1, 0x880A8A82, + 0x682A4A62, 0xB03181B1, 0xD011C1D1, 0x20200020, 0xD417C7D3, 0x00020202, + 0x20220222, 0x04040400, 0x68284860, 0x70314171, 0x04070703, 0xD81BCBD3, + 0x9C1D8D91, 0x98198991, 0x60214161, 0xBC3E8EB2, 0xE426C6E2, 0x58194951, + 0xDC1DCDD1, 0x50114151, 0x90108090, 0xDC1CCCD0, 0x981A8A92, 0xA02383A3, + 0xA82B8BA3, 0xD010C0D0, 0x80018181, 0x0C0F0F03, 0x44074743, 0x181A0A12, + 0xE023C3E3, 0xEC2CCCE0, 0x8C0D8D81, 0xBC3F8FB3, 0x94168692, 0x783B4B73, + 0x5C1C4C50, 0xA02282A2, 0xA02181A1, 0x60234363, 0x20230323, 0x4C0D4D41, + 0xC808C8C0, 0x9C1E8E92, 0x9C1C8C90, 0x383A0A32, 0x0C0C0C00, 0x2C2E0E22, + 0xB83A8AB2, 0x6C2E4E62, 0x9C1F8F93, 0x581A4A52, 0xF032C2F2, 0x90128292, + 0xF033C3F3, 0x48094941, 0x78384870, 0xCC0CCCC0, 0x14150511, 0xF83BCBF3, + 0x70304070, 0x74354571, 0x7C3F4F73, 0x34350531, 0x10100010, 0x00030303, + 0x64244460, 0x6C2D4D61, 0xC406C6C2, 0x74344470, 0xD415C5D1, 0xB43484B0, + 0xE82ACAE2, 0x08090901, 0x74364672, 0x18190911, 0xFC3ECEF2, 0x40004040, + 0x10120212, 0xE020C0E0, 0xBC3D8DB1, 0x04050501, 0xF83ACAF2, 0x00010101, + 0xF030C0F0, 0x282A0A22, 0x5C1E4E52, 0xA82989A1, 0x54164652, 0x40034343, + 0x84058581, 0x14140410, 0x88098981, 0x981B8B93, 0xB03080B0, 0xE425C5E1, + 0x48084840, 0x78394971, 0x94178793, 0xFC3CCCF0, 0x1C1E0E12, 0x80028282, + 0x20210121, 0x8C0C8C80, 0x181B0B13, 0x5C1F4F53, 0x74374773, 0x54144450, + 0xB03282B2, 0x1C1D0D11, 0x24250521, 0x4C0F4F43, 0x00000000, 0x44064642, + 0xEC2DCDE1, 0x58184850, 0x50124252, 0xE82BCBE3, 0x7C3E4E72, 0xD81ACAD2, + 0xC809C9C1, 0xFC3DCDF1, 0x30300030, 0x94158591, 0x64254561, 0x3C3C0C30, + 0xB43686B2, 0xE424C4E0, 0xB83B8BB3, 0x7C3C4C70, 0x0C0E0E02, 0x50104050, + 0x38390931, 0x24260622, 0x30320232, 0x84048480, 0x68294961, 0x90138393, + 0x34370733, 0xE427C7E3, 0x24240420, 0xA42484A0, 0xC80BCBC3, 0x50134353, + 0x080A0A02, 0x84078783, 0xD819C9D1, 0x4C0C4C40, 0x80038383, 0x8C0F8F83, + 0xCC0ECEC2, 0x383B0B33, 0x480A4A42, 0xB43787B3 }; + +const u32bit SEED::G_FUNC::S2[256] = { + 0xA1A82989, 0x81840585, 0xD2D416C6, 0xD3D013C3, 0x50541444, 0x111C1D0D, + 0xA0AC2C8C, 0x21242505, 0x515C1D4D, 0x43400343, 0x10181808, 0x121C1E0E, + 0x51501141, 0xF0FC3CCC, 0xC2C80ACA, 0x63602343, 0x20282808, 0x40440444, + 0x20202000, 0x919C1D8D, 0xE0E020C0, 0xE2E022C2, 0xC0C808C8, 0x13141707, + 0xA1A42585, 0x838C0F8F, 0x03000303, 0x73783B4B, 0xB3B83B8B, 0x13101303, + 0xD2D012C2, 0xE2EC2ECE, 0x70703040, 0x808C0C8C, 0x333C3F0F, 0xA0A82888, + 0x32303202, 0xD1DC1DCD, 0xF2F436C6, 0x70743444, 0xE0EC2CCC, 0x91941585, + 0x03080B0B, 0x53541747, 0x505C1C4C, 0x53581B4B, 0xB1BC3D8D, 0x01000101, + 0x20242404, 0x101C1C0C, 0x73703343, 0x90981888, 0x10101000, 0xC0CC0CCC, + 0xF2F032C2, 0xD1D819C9, 0x202C2C0C, 0xE3E427C7, 0x72703242, 0x83800383, + 0x93981B8B, 0xD1D011C1, 0x82840686, 0xC1C809C9, 0x60602040, 0x50501040, + 0xA3A02383, 0xE3E82BCB, 0x010C0D0D, 0xB2B43686, 0x929C1E8E, 0x434C0F4F, + 0xB3B43787, 0x52581A4A, 0xC2C406C6, 0x70783848, 0xA2A42686, 0x12101202, + 0xA3AC2F8F, 0xD1D415C5, 0x61602141, 0xC3C003C3, 0xB0B43484, 0x41400141, + 0x52501242, 0x717C3D4D, 0x818C0D8D, 0x00080808, 0x131C1F0F, 0x91981989, + 0x00000000, 0x11181909, 0x00040404, 0x53501343, 0xF3F437C7, 0xE1E021C1, + 0xF1FC3DCD, 0x72743646, 0x232C2F0F, 0x23242707, 0xB0B03080, 0x83880B8B, + 0x020C0E0E, 0xA3A82B8B, 0xA2A02282, 0x626C2E4E, 0x93901383, 0x414C0D4D, + 0x61682949, 0x707C3C4C, 0x01080909, 0x02080A0A, 0xB3BC3F8F, 0xE3EC2FCF, + 0xF3F033C3, 0xC1C405C5, 0x83840787, 0x10141404, 0xF2FC3ECE, 0x60642444, + 0xD2DC1ECE, 0x222C2E0E, 0x43480B4B, 0x12181A0A, 0x02040606, 0x21202101, + 0x63682B4B, 0x62642646, 0x02000202, 0xF1F435C5, 0x92901282, 0x82880A8A, + 0x000C0C0C, 0xB3B03383, 0x727C3E4E, 0xD0D010C0, 0x72783A4A, 0x43440747, + 0x92941686, 0xE1E425C5, 0x22242606, 0x80800080, 0xA1AC2D8D, 0xD3DC1FCF, + 0xA1A02181, 0x30303000, 0x33343707, 0xA2AC2E8E, 0x32343606, 0x11141505, + 0x22202202, 0x30383808, 0xF0F434C4, 0xA3A42787, 0x41440545, 0x404C0C4C, + 0x81800181, 0xE1E829C9, 0x80840484, 0x93941787, 0x31343505, 0xC3C80BCB, + 0xC2CC0ECE, 0x303C3C0C, 0x71703141, 0x11101101, 0xC3C407C7, 0x81880989, + 0x71743545, 0xF3F83BCB, 0xD2D81ACA, 0xF0F838C8, 0x90941484, 0x51581949, + 0x82800282, 0xC0C404C4, 0xF3FC3FCF, 0x41480949, 0x31383909, 0x63642747, + 0xC0C000C0, 0xC3CC0FCF, 0xD3D417C7, 0xB0B83888, 0x030C0F0F, 0x828C0E8E, + 0x42400242, 0x23202303, 0x91901181, 0x606C2C4C, 0xD3D81BCB, 0xA0A42484, + 0x30343404, 0xF1F031C1, 0x40480848, 0xC2C002C2, 0x636C2F4F, 0x313C3D0D, + 0x212C2D0D, 0x40400040, 0xB2BC3E8E, 0x323C3E0E, 0xB0BC3C8C, 0xC1C001C1, + 0xA2A82A8A, 0xB2B83A8A, 0x424C0E4E, 0x51541545, 0x33383B0B, 0xD0DC1CCC, + 0x60682848, 0x737C3F4F, 0x909C1C8C, 0xD0D818C8, 0x42480A4A, 0x52541646, + 0x73743747, 0xA0A02080, 0xE1EC2DCD, 0x42440646, 0xB1B43585, 0x23282B0B, + 0x61642545, 0xF2F83ACA, 0xE3E023C3, 0xB1B83989, 0xB1B03181, 0x939C1F8F, + 0x525C1E4E, 0xF1F839C9, 0xE2E426C6, 0xB2B03282, 0x31303101, 0xE2E82ACA, + 0x616C2D4D, 0x535C1F4F, 0xE0E424C4, 0xF0F030C0, 0xC1CC0DCD, 0x80880888, + 0x12141606, 0x32383A0A, 0x50581848, 0xD0D414C4, 0x62602242, 0x21282909, + 0x03040707, 0x33303303, 0xE0E828C8, 0x13181B0B, 0x01040505, 0x71783949, + 0x90901080, 0x62682A4A, 0x22282A0A, 0x92981A8A }; + +const u32bit SEED::G_FUNC::S3[256] = { + 0x08303838, 0xC8E0E828, 0x0D212C2D, 0x86A2A426, 0xCFC3CC0F, 0xCED2DC1E, + 0x83B3B033, 0x88B0B838, 0x8FA3AC2F, 0x40606020, 0x45515415, 0xC7C3C407, + 0x44404404, 0x4F636C2F, 0x4B63682B, 0x4B53581B, 0xC3C3C003, 0x42626022, + 0x03333033, 0x85B1B435, 0x09212829, 0x80A0A020, 0xC2E2E022, 0x87A3A427, + 0xC3D3D013, 0x81919011, 0x01111011, 0x06020406, 0x0C101C1C, 0x8CB0BC3C, + 0x06323436, 0x4B43480B, 0xCFE3EC2F, 0x88808808, 0x4C606C2C, 0x88A0A828, + 0x07131417, 0xC4C0C404, 0x06121416, 0xC4F0F434, 0xC2C2C002, 0x45414405, + 0xC1E1E021, 0xC6D2D416, 0x0F333C3F, 0x0D313C3D, 0x8E828C0E, 0x88909818, + 0x08202828, 0x4E424C0E, 0xC6F2F436, 0x0E323C3E, 0x85A1A425, 0xC9F1F839, + 0x0D010C0D, 0xCFD3DC1F, 0xC8D0D818, 0x0B23282B, 0x46626426, 0x4A72783A, + 0x07232427, 0x0F232C2F, 0xC1F1F031, 0x42727032, 0x42424002, 0xC4D0D414, + 0x41414001, 0xC0C0C000, 0x43737033, 0x47636427, 0x8CA0AC2C, 0x8B83880B, + 0xC7F3F437, 0x8DA1AC2D, 0x80808000, 0x0F131C1F, 0xCAC2C80A, 0x0C202C2C, + 0x8AA2A82A, 0x04303434, 0xC2D2D012, 0x0B03080B, 0xCEE2EC2E, 0xC9E1E829, + 0x4D515C1D, 0x84909414, 0x08101818, 0xC8F0F838, 0x47535417, 0x8EA2AC2E, + 0x08000808, 0xC5C1C405, 0x03131013, 0xCDC1CC0D, 0x86828406, 0x89B1B839, + 0xCFF3FC3F, 0x4D717C3D, 0xC1C1C001, 0x01313031, 0xC5F1F435, 0x8A82880A, + 0x4A62682A, 0x81B1B031, 0xC1D1D011, 0x00202020, 0xC7D3D417, 0x02020002, + 0x02222022, 0x04000404, 0x48606828, 0x41717031, 0x07030407, 0xCBD3D81B, + 0x8D919C1D, 0x89919819, 0x41616021, 0x8EB2BC3E, 0xC6E2E426, 0x49515819, + 0xCDD1DC1D, 0x41515011, 0x80909010, 0xCCD0DC1C, 0x8A92981A, 0x83A3A023, + 0x8BA3A82B, 0xC0D0D010, 0x81818001, 0x0F030C0F, 0x47434407, 0x0A12181A, + 0xC3E3E023, 0xCCE0EC2C, 0x8D818C0D, 0x8FB3BC3F, 0x86929416, 0x4B73783B, + 0x4C505C1C, 0x82A2A022, 0x81A1A021, 0x43636023, 0x03232023, 0x4D414C0D, + 0xC8C0C808, 0x8E929C1E, 0x8C909C1C, 0x0A32383A, 0x0C000C0C, 0x0E222C2E, + 0x8AB2B83A, 0x4E626C2E, 0x8F939C1F, 0x4A52581A, 0xC2F2F032, 0x82929012, + 0xC3F3F033, 0x49414809, 0x48707838, 0xCCC0CC0C, 0x05111415, 0xCBF3F83B, + 0x40707030, 0x45717435, 0x4F737C3F, 0x05313435, 0x00101010, 0x03030003, + 0x44606424, 0x4D616C2D, 0xC6C2C406, 0x44707434, 0xC5D1D415, 0x84B0B434, + 0xCAE2E82A, 0x09010809, 0x46727436, 0x09111819, 0xCEF2FC3E, 0x40404000, + 0x02121012, 0xC0E0E020, 0x8DB1BC3D, 0x05010405, 0xCAF2F83A, 0x01010001, + 0xC0F0F030, 0x0A22282A, 0x4E525C1E, 0x89A1A829, 0x46525416, 0x43434003, + 0x85818405, 0x04101414, 0x89818809, 0x8B93981B, 0x80B0B030, 0xC5E1E425, + 0x48404808, 0x49717839, 0x87939417, 0xCCF0FC3C, 0x0E121C1E, 0x82828002, + 0x01212021, 0x8C808C0C, 0x0B13181B, 0x4F535C1F, 0x47737437, 0x44505414, + 0x82B2B032, 0x0D111C1D, 0x05212425, 0x4F434C0F, 0x00000000, 0x46424406, + 0xCDE1EC2D, 0x48505818, 0x42525012, 0xCBE3E82B, 0x4E727C3E, 0xCAD2D81A, + 0xC9C1C809, 0xCDF1FC3D, 0x00303030, 0x85919415, 0x45616425, 0x0C303C3C, + 0x86B2B436, 0xC4E0E424, 0x8BB3B83B, 0x4C707C3C, 0x0E020C0E, 0x40505010, + 0x09313839, 0x06222426, 0x02323032, 0x84808404, 0x49616829, 0x83939013, + 0x07333437, 0xC7E3E427, 0x04202424, 0x84A0A424, 0xCBC3C80B, 0x43535013, + 0x0A02080A, 0x87838407, 0xC9D1D819, 0x4C404C0C, 0x83838003, 0x8F838C0F, + 0xCEC2CC0E, 0x0B33383B, 0x4A42480A, 0x87B3B437 }; + +} diff --git a/botan/src/block/serpent/info.txt b/botan/src/block/serpent/info.txt new file mode 100644 index 0000000..baaccbf --- /dev/null +++ b/botan/src/block/serpent/info.txt @@ -0,0 +1,10 @@ +realname "Serpent" + +define SERPENT + +load_on auto + +<add> +serpent.cpp +serpent.h +</add> diff --git a/botan/src/block/serpent/serpent.cpp b/botan/src/block/serpent/serpent.cpp new file mode 100644 index 0000000..df7592f --- /dev/null +++ b/botan/src/block/serpent/serpent.cpp @@ -0,0 +1,369 @@ +/* +* Serpent +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/serpent.h> +#include <botan/loadstor.h> +#include <botan/rotate.h> + +namespace Botan { + +namespace { + +/* +* Serpent Encryption S-Box 1 +*/ +inline void SBoxE1(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3) + { + u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4; + T3 ^= T0; T4 = T1; T1 &= T3; T4 ^= T2; T1 ^= T0; T0 |= T3; T0 ^= T4; + T4 ^= T3; T3 ^= T2; T2 |= T1; T2 ^= T4; T4 = ~T4; T4 |= T1; T1 ^= T3; + T1 ^= T4; T3 |= T0; T1 ^= T3; T4 ^= T3; + B0 = T1; B1 = T4; B2 = T2; B3 = T0; + } + +/* +* Serpent Encryption S-Box 2 +*/ +inline void SBoxE2(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3) + { + u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4; + T0 = ~T0; T2 = ~T2; T4 = T0; T0 &= T1; T2 ^= T0; T0 |= T3; T3 ^= T2; + T1 ^= T0; T0 ^= T4; T4 |= T1; T1 ^= T3; T2 |= T0; T2 &= T4; T0 ^= T1; + T1 &= T2; T1 ^= T0; T0 &= T2; T0 ^= T4; + B0 = T2; B1 = T0; B2 = T3; B3 = T1; + } + +/* +* Serpent Encryption S-Box 3 +*/ +inline void SBoxE3(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3) + { + u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4; + T4 = T0; T0 &= T2; T0 ^= T3; T2 ^= T1; T2 ^= T0; T3 |= T4; T3 ^= T1; + T4 ^= T2; T1 = T3; T3 |= T4; T3 ^= T0; T0 &= T1; T4 ^= T0; T1 ^= T3; + T1 ^= T4; T4 = ~T4; + B0 = T2; B1 = T3; B2 = T1; B3 = T4; + } + +/* +* Serpent Encryption S-Box 4 +*/ +inline void SBoxE4(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3) + { + u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4; + T4 = T0; T0 |= T3; T3 ^= T1; T1 &= T4; T4 ^= T2; T2 ^= T3; T3 &= T0; + T4 |= T1; T3 ^= T4; T0 ^= T1; T4 &= T0; T1 ^= T3; T4 ^= T2; T1 |= T0; + T1 ^= T2; T0 ^= T3; T2 = T1; T1 |= T3; T1 ^= T0; + B0 = T1; B1 = T2; B2 = T3; B3 = T4; + } + +/* +* Serpent Encryption S-Box 5 +*/ +inline void SBoxE5(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3) + { + u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4; + T1 ^= T3; T3 = ~T3; T2 ^= T3; T3 ^= T0; T4 = T1; T1 &= T3; T1 ^= T2; + T4 ^= T3; T0 ^= T4; T2 &= T4; T2 ^= T0; T0 &= T1; T3 ^= T0; T4 |= T1; + T4 ^= T0; T0 |= T3; T0 ^= T2; T2 &= T3; T0 = ~T0; T4 ^= T2; + B0 = T1; B1 = T4; B2 = T0; B3 = T3; + } + +/* +* Serpent Encryption S-Box 6 +*/ +inline void SBoxE6(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3) + { + u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4; + T0 ^= T1; T1 ^= T3; T3 = ~T3; T4 = T1; T1 &= T0; T2 ^= T3; T1 ^= T2; + T2 |= T4; T4 ^= T3; T3 &= T1; T3 ^= T0; T4 ^= T1; T4 ^= T2; T2 ^= T0; + T0 &= T3; T2 = ~T2; T0 ^= T4; T4 |= T3; T2 ^= T4; + B0 = T1; B1 = T3; B2 = T0; B3 = T2; + } + +/* +* Serpent Encryption S-Box 7 +*/ +inline void SBoxE7(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3) + { + u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4; + T2 = ~T2; T4 = T3; T3 &= T0; T0 ^= T4; T3 ^= T2; T2 |= T4; T1 ^= T3; + T2 ^= T0; T0 |= T1; T2 ^= T1; T4 ^= T0; T0 |= T3; T0 ^= T2; T4 ^= T3; + T4 ^= T0; T3 = ~T3; T2 &= T4; T2 ^= T3; + B0 = T0; B1 = T1; B2 = T4; B3 = T2; + } + +/* +* Serpent Encryption S-Box 8 +*/ +inline void SBoxE8(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3) + { + u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4; + T4 = T1; T1 |= T2; T1 ^= T3; T4 ^= T2; T2 ^= T1; T3 |= T4; T3 &= T0; + T4 ^= T2; T3 ^= T1; T1 |= T4; T1 ^= T0; T0 |= T4; T0 ^= T2; T1 ^= T4; + T2 ^= T1; T1 &= T0; T1 ^= T4; T2 = ~T2; T2 |= T0; T4 ^= T2; + B0 = T4; B1 = T3; B2 = T1; B3 = T0; + } + +/* +* Serpent Decryption S-Box 1 +*/ +inline void SBoxD1(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3) + { + u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4; + T2 = ~T2; T4 = T1; T1 |= T0; T4 = ~T4; T1 ^= T2; T2 |= T4; T1 ^= T3; + T0 ^= T4; T2 ^= T0; T0 &= T3; T4 ^= T0; T0 |= T1; T0 ^= T2; T3 ^= T4; + T2 ^= T1; T3 ^= T0; T3 ^= T1; T2 &= T3; T4 ^= T2; + B0 = T0; B1 = T4; B2 = T1; B3 = T3; + } + +/* +* Serpent Decryption S-Box 2 +*/ +inline void SBoxD2(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3) + { + u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4; + T4 = T1; T1 ^= T3; T3 &= T1; T4 ^= T2; T3 ^= T0; T0 |= T1; T2 ^= T3; + T0 ^= T4; T0 |= T2; T1 ^= T3; T0 ^= T1; T1 |= T3; T1 ^= T0; T4 = ~T4; + T4 ^= T1; T1 |= T0; T1 ^= T0; T1 |= T4; T3 ^= T1; + B0 = T4; B1 = T0; B2 = T3; B3 = T2; + } + +/* +* Serpent Decryption S-Box 3 +*/ +inline void SBoxD3(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3) + { + u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4; + T2 ^= T3; T3 ^= T0; T4 = T3; T3 &= T2; T3 ^= T1; T1 |= T2; T1 ^= T4; + T4 &= T3; T2 ^= T3; T4 &= T0; T4 ^= T2; T2 &= T1; T2 |= T0; T3 = ~T3; + T2 ^= T3; T0 ^= T3; T0 &= T1; T3 ^= T4; T3 ^= T0; + B0 = T1; B1 = T4; B2 = T2; B3 = T3; + } + +/* +* Serpent Decryption S-Box 4 +*/ +inline void SBoxD4(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3) + { + u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4; + T4 = T2; T2 ^= T1; T0 ^= T2; T4 &= T2; T4 ^= T0; T0 &= T1; T1 ^= T3; + T3 |= T4; T2 ^= T3; T0 ^= T3; T1 ^= T4; T3 &= T2; T3 ^= T1; T1 ^= T0; + T1 |= T2; T0 ^= T3; T1 ^= T4; T0 ^= T1; + B0 = T2; B1 = T1; B2 = T3; B3 = T0; + } + +/* +* Serpent Decryption S-Box 5 +*/ +inline void SBoxD5(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3) + { + u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4; + T4 = T2; T2 &= T3; T2 ^= T1; T1 |= T3; T1 &= T0; T4 ^= T2; T4 ^= T1; + T1 &= T2; T0 = ~T0; T3 ^= T4; T1 ^= T3; T3 &= T0; T3 ^= T2; T0 ^= T1; + T2 &= T0; T3 ^= T0; T2 ^= T4; T2 |= T3; T3 ^= T0; T2 ^= T1; + B0 = T0; B1 = T3; B2 = T2; B3 = T4; + } + +/* +* Serpent Decryption S-Box 6 +*/ +inline void SBoxD6(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3) + { + u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4; + T1 = ~T1; T4 = T3; T2 ^= T1; T3 |= T0; T3 ^= T2; T2 |= T1; T2 &= T0; + T4 ^= T3; T2 ^= T4; T4 |= T0; T4 ^= T1; T1 &= T2; T1 ^= T3; T4 ^= T2; + T3 &= T4; T4 ^= T1; T3 ^= T4; T4 = ~T4; T3 ^= T0; + B0 = T1; B1 = T4; B2 = T3; B3 = T2; + } + +/* +* Serpent Decryption S-Box 7 +*/ +inline void SBoxD7(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3) + { + u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4; + T0 ^= T2; T4 = T2; T2 &= T0; T4 ^= T3; T2 = ~T2; T3 ^= T1; T2 ^= T3; + T4 |= T0; T0 ^= T2; T3 ^= T4; T4 ^= T1; T1 &= T3; T1 ^= T0; T0 ^= T3; + T0 |= T2; T3 ^= T1; T4 ^= T0; + B0 = T1; B1 = T2; B2 = T4; B3 = T3; + } + +/* +* Serpent Decryption S-Box 8 +*/ +inline void SBoxD8(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3) + { + u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4; + T4 = T2; T2 ^= T0; T0 &= T3; T4 |= T3; T2 = ~T2; T3 ^= T1; T1 |= T0; + T0 ^= T2; T2 &= T4; T3 &= T4; T1 ^= T2; T2 ^= T0; T0 |= T2; T4 ^= T1; + T0 ^= T3; T3 ^= T4; T4 |= T0; T3 ^= T2; T4 ^= T2; + B0 = T3; B1 = T0; B2 = T1; B3 = T4; + } + +/* +* Serpent's Linear Transformation +*/ +inline void transform(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3) + { + B0 = rotate_left(B0, 13); B2 = rotate_left(B2, 3); + B1 ^= B0 ^ B2; B3 ^= B2 ^ (B0 << 3); + B1 = rotate_left(B1, 1); B3 = rotate_left(B3, 7); + B0 ^= B1 ^ B3; B2 ^= B3 ^ (B1 << 7); + B0 = rotate_left(B0, 5); B2 = rotate_left(B2, 22); + } + +/* +* Serpent's Inverse Linear Transformation +*/ +inline void i_transform(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3) + { + B2 = rotate_right(B2, 22); B0 = rotate_right(B0, 5); + B2 ^= B3 ^ (B1 << 7); B0 ^= B1 ^ B3; + B3 = rotate_right(B3, 7); B1 = rotate_right(B1, 1); + B3 ^= B2 ^ (B0 << 3); B1 ^= B0 ^ B2; + B2 = rotate_right(B2, 3); B0 = rotate_right(B0, 13); + } + +} + +/* +* XOR a key block with a data block +*/ +#define key_xor(round, B0, B1, B2, B3) \ + B0 ^= round_key[4*round ]; \ + B1 ^= round_key[4*round+1]; \ + B2 ^= round_key[4*round+2]; \ + B3 ^= round_key[4*round+3]; + +/* +* Serpent Encryption +*/ +void Serpent::enc(const byte in[], byte out[]) const + { + u32bit B0 = load_le<u32bit>(in, 0); + u32bit B1 = load_le<u32bit>(in, 1); + u32bit B2 = load_le<u32bit>(in, 2); + u32bit B3 = load_le<u32bit>(in, 3); + + key_xor( 0,B0,B1,B2,B3); SBoxE1(B0,B1,B2,B3); transform(B0,B1,B2,B3); + key_xor( 1,B0,B1,B2,B3); SBoxE2(B0,B1,B2,B3); transform(B0,B1,B2,B3); + key_xor( 2,B0,B1,B2,B3); SBoxE3(B0,B1,B2,B3); transform(B0,B1,B2,B3); + key_xor( 3,B0,B1,B2,B3); SBoxE4(B0,B1,B2,B3); transform(B0,B1,B2,B3); + key_xor( 4,B0,B1,B2,B3); SBoxE5(B0,B1,B2,B3); transform(B0,B1,B2,B3); + key_xor( 5,B0,B1,B2,B3); SBoxE6(B0,B1,B2,B3); transform(B0,B1,B2,B3); + key_xor( 6,B0,B1,B2,B3); SBoxE7(B0,B1,B2,B3); transform(B0,B1,B2,B3); + key_xor( 7,B0,B1,B2,B3); SBoxE8(B0,B1,B2,B3); transform(B0,B1,B2,B3); + key_xor( 8,B0,B1,B2,B3); SBoxE1(B0,B1,B2,B3); transform(B0,B1,B2,B3); + key_xor( 9,B0,B1,B2,B3); SBoxE2(B0,B1,B2,B3); transform(B0,B1,B2,B3); + key_xor(10,B0,B1,B2,B3); SBoxE3(B0,B1,B2,B3); transform(B0,B1,B2,B3); + key_xor(11,B0,B1,B2,B3); SBoxE4(B0,B1,B2,B3); transform(B0,B1,B2,B3); + key_xor(12,B0,B1,B2,B3); SBoxE5(B0,B1,B2,B3); transform(B0,B1,B2,B3); + key_xor(13,B0,B1,B2,B3); SBoxE6(B0,B1,B2,B3); transform(B0,B1,B2,B3); + key_xor(14,B0,B1,B2,B3); SBoxE7(B0,B1,B2,B3); transform(B0,B1,B2,B3); + key_xor(15,B0,B1,B2,B3); SBoxE8(B0,B1,B2,B3); transform(B0,B1,B2,B3); + key_xor(16,B0,B1,B2,B3); SBoxE1(B0,B1,B2,B3); transform(B0,B1,B2,B3); + key_xor(17,B0,B1,B2,B3); SBoxE2(B0,B1,B2,B3); transform(B0,B1,B2,B3); + key_xor(18,B0,B1,B2,B3); SBoxE3(B0,B1,B2,B3); transform(B0,B1,B2,B3); + key_xor(19,B0,B1,B2,B3); SBoxE4(B0,B1,B2,B3); transform(B0,B1,B2,B3); + key_xor(20,B0,B1,B2,B3); SBoxE5(B0,B1,B2,B3); transform(B0,B1,B2,B3); + key_xor(21,B0,B1,B2,B3); SBoxE6(B0,B1,B2,B3); transform(B0,B1,B2,B3); + key_xor(22,B0,B1,B2,B3); SBoxE7(B0,B1,B2,B3); transform(B0,B1,B2,B3); + key_xor(23,B0,B1,B2,B3); SBoxE8(B0,B1,B2,B3); transform(B0,B1,B2,B3); + key_xor(24,B0,B1,B2,B3); SBoxE1(B0,B1,B2,B3); transform(B0,B1,B2,B3); + key_xor(25,B0,B1,B2,B3); SBoxE2(B0,B1,B2,B3); transform(B0,B1,B2,B3); + key_xor(26,B0,B1,B2,B3); SBoxE3(B0,B1,B2,B3); transform(B0,B1,B2,B3); + key_xor(27,B0,B1,B2,B3); SBoxE4(B0,B1,B2,B3); transform(B0,B1,B2,B3); + key_xor(28,B0,B1,B2,B3); SBoxE5(B0,B1,B2,B3); transform(B0,B1,B2,B3); + key_xor(29,B0,B1,B2,B3); SBoxE6(B0,B1,B2,B3); transform(B0,B1,B2,B3); + key_xor(30,B0,B1,B2,B3); SBoxE7(B0,B1,B2,B3); transform(B0,B1,B2,B3); + key_xor(31,B0,B1,B2,B3); SBoxE8(B0,B1,B2,B3); key_xor(32,B0,B1,B2,B3); + + store_le(out, B0, B1, B2, B3); + } + +/* +* Serpent Decryption +*/ +void Serpent::dec(const byte in[], byte out[]) const + { + u32bit B0 = load_le<u32bit>(in, 0); + u32bit B1 = load_le<u32bit>(in, 1); + u32bit B2 = load_le<u32bit>(in, 2); + u32bit B3 = load_le<u32bit>(in, 3); + + key_xor(32,B0,B1,B2,B3); SBoxD8(B0,B1,B2,B3); key_xor(31,B0,B1,B2,B3); + i_transform(B0,B1,B2,B3); SBoxD7(B0,B1,B2,B3); key_xor(30,B0,B1,B2,B3); + i_transform(B0,B1,B2,B3); SBoxD6(B0,B1,B2,B3); key_xor(29,B0,B1,B2,B3); + i_transform(B0,B1,B2,B3); SBoxD5(B0,B1,B2,B3); key_xor(28,B0,B1,B2,B3); + i_transform(B0,B1,B2,B3); SBoxD4(B0,B1,B2,B3); key_xor(27,B0,B1,B2,B3); + i_transform(B0,B1,B2,B3); SBoxD3(B0,B1,B2,B3); key_xor(26,B0,B1,B2,B3); + i_transform(B0,B1,B2,B3); SBoxD2(B0,B1,B2,B3); key_xor(25,B0,B1,B2,B3); + i_transform(B0,B1,B2,B3); SBoxD1(B0,B1,B2,B3); key_xor(24,B0,B1,B2,B3); + i_transform(B0,B1,B2,B3); SBoxD8(B0,B1,B2,B3); key_xor(23,B0,B1,B2,B3); + i_transform(B0,B1,B2,B3); SBoxD7(B0,B1,B2,B3); key_xor(22,B0,B1,B2,B3); + i_transform(B0,B1,B2,B3); SBoxD6(B0,B1,B2,B3); key_xor(21,B0,B1,B2,B3); + i_transform(B0,B1,B2,B3); SBoxD5(B0,B1,B2,B3); key_xor(20,B0,B1,B2,B3); + i_transform(B0,B1,B2,B3); SBoxD4(B0,B1,B2,B3); key_xor(19,B0,B1,B2,B3); + i_transform(B0,B1,B2,B3); SBoxD3(B0,B1,B2,B3); key_xor(18,B0,B1,B2,B3); + i_transform(B0,B1,B2,B3); SBoxD2(B0,B1,B2,B3); key_xor(17,B0,B1,B2,B3); + i_transform(B0,B1,B2,B3); SBoxD1(B0,B1,B2,B3); key_xor(16,B0,B1,B2,B3); + i_transform(B0,B1,B2,B3); SBoxD8(B0,B1,B2,B3); key_xor(15,B0,B1,B2,B3); + i_transform(B0,B1,B2,B3); SBoxD7(B0,B1,B2,B3); key_xor(14,B0,B1,B2,B3); + i_transform(B0,B1,B2,B3); SBoxD6(B0,B1,B2,B3); key_xor(13,B0,B1,B2,B3); + i_transform(B0,B1,B2,B3); SBoxD5(B0,B1,B2,B3); key_xor(12,B0,B1,B2,B3); + i_transform(B0,B1,B2,B3); SBoxD4(B0,B1,B2,B3); key_xor(11,B0,B1,B2,B3); + i_transform(B0,B1,B2,B3); SBoxD3(B0,B1,B2,B3); key_xor(10,B0,B1,B2,B3); + i_transform(B0,B1,B2,B3); SBoxD2(B0,B1,B2,B3); key_xor( 9,B0,B1,B2,B3); + i_transform(B0,B1,B2,B3); SBoxD1(B0,B1,B2,B3); key_xor( 8,B0,B1,B2,B3); + i_transform(B0,B1,B2,B3); SBoxD8(B0,B1,B2,B3); key_xor( 7,B0,B1,B2,B3); + i_transform(B0,B1,B2,B3); SBoxD7(B0,B1,B2,B3); key_xor( 6,B0,B1,B2,B3); + i_transform(B0,B1,B2,B3); SBoxD6(B0,B1,B2,B3); key_xor( 5,B0,B1,B2,B3); + i_transform(B0,B1,B2,B3); SBoxD5(B0,B1,B2,B3); key_xor( 4,B0,B1,B2,B3); + i_transform(B0,B1,B2,B3); SBoxD4(B0,B1,B2,B3); key_xor( 3,B0,B1,B2,B3); + i_transform(B0,B1,B2,B3); SBoxD3(B0,B1,B2,B3); key_xor( 2,B0,B1,B2,B3); + i_transform(B0,B1,B2,B3); SBoxD2(B0,B1,B2,B3); key_xor( 1,B0,B1,B2,B3); + i_transform(B0,B1,B2,B3); SBoxD1(B0,B1,B2,B3); key_xor( 0,B0,B1,B2,B3); + + store_le(out, B0, B1, B2, B3); + } + +/* +* Serpent Key Schedule +*/ +void Serpent::key_schedule(const byte key[], u32bit length) + { + const u32bit PHI = 0x9E3779B9; + + SecureBuffer<u32bit, 140> W; + for(u32bit j = 0; j != length / 4; ++j) + W[j] = load_le<u32bit>(key, j); + + W[length / 4] |= u32bit(1) << ((length%4)*8); + for(u32bit j = 8; j != 140; ++j) + W[j] = rotate_left(W[j-8] ^ W[j-5] ^ W[j-3] ^ W[j-1] ^ PHI ^ (j-8), 11); + SBoxE4(W[ 8],W[ 9],W[ 10],W[ 11]); SBoxE3(W[ 12],W[ 13],W[ 14],W[ 15]); + SBoxE2(W[ 16],W[ 17],W[ 18],W[ 19]); SBoxE1(W[ 20],W[ 21],W[ 22],W[ 23]); + SBoxE8(W[ 24],W[ 25],W[ 26],W[ 27]); SBoxE7(W[ 28],W[ 29],W[ 30],W[ 31]); + SBoxE6(W[ 32],W[ 33],W[ 34],W[ 35]); SBoxE5(W[ 36],W[ 37],W[ 38],W[ 39]); + SBoxE4(W[ 40],W[ 41],W[ 42],W[ 43]); SBoxE3(W[ 44],W[ 45],W[ 46],W[ 47]); + SBoxE2(W[ 48],W[ 49],W[ 50],W[ 51]); SBoxE1(W[ 52],W[ 53],W[ 54],W[ 55]); + SBoxE8(W[ 56],W[ 57],W[ 58],W[ 59]); SBoxE7(W[ 60],W[ 61],W[ 62],W[ 63]); + SBoxE6(W[ 64],W[ 65],W[ 66],W[ 67]); SBoxE5(W[ 68],W[ 69],W[ 70],W[ 71]); + SBoxE4(W[ 72],W[ 73],W[ 74],W[ 75]); SBoxE3(W[ 76],W[ 77],W[ 78],W[ 79]); + SBoxE2(W[ 80],W[ 81],W[ 82],W[ 83]); SBoxE1(W[ 84],W[ 85],W[ 86],W[ 87]); + SBoxE8(W[ 88],W[ 89],W[ 90],W[ 91]); SBoxE7(W[ 92],W[ 93],W[ 94],W[ 95]); + SBoxE6(W[ 96],W[ 97],W[ 98],W[ 99]); SBoxE5(W[100],W[101],W[102],W[103]); + SBoxE4(W[104],W[105],W[106],W[107]); SBoxE3(W[108],W[109],W[110],W[111]); + SBoxE2(W[112],W[113],W[114],W[115]); SBoxE1(W[116],W[117],W[118],W[119]); + SBoxE8(W[120],W[121],W[122],W[123]); SBoxE7(W[124],W[125],W[126],W[127]); + SBoxE6(W[128],W[129],W[130],W[131]); SBoxE5(W[132],W[133],W[134],W[135]); + SBoxE4(W[136],W[137],W[138],W[139]); + round_key.copy(W + 8, 132); + } + +} diff --git a/botan/src/block/serpent/serpent.h b/botan/src/block/serpent/serpent.h new file mode 100644 index 0000000..5b9be25 --- /dev/null +++ b/botan/src/block/serpent/serpent.h @@ -0,0 +1,35 @@ +/* +* Serpent +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_SERPENT_H__ +#define BOTAN_SERPENT_H__ + +#include <botan/block_cipher.h> + +namespace Botan { + +/* +* Serpent +*/ +class BOTAN_DLL Serpent : public BlockCipher + { + public: + void clear() throw() { round_key.clear(); } + std::string name() const { return "Serpent"; } + BlockCipher* clone() const { return new Serpent; } + Serpent() : BlockCipher(16, 16, 32, 8) {} + protected: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key_schedule(const byte[], u32bit); + + SecureBuffer<u32bit, 132> round_key; + }; + +} + +#endif diff --git a/botan/src/block/serpent_ia32/info.txt b/botan/src/block/serpent_ia32/info.txt new file mode 100644 index 0000000..ea05062 --- /dev/null +++ b/botan/src/block/serpent_ia32/info.txt @@ -0,0 +1,35 @@ +realname "Serpent (IA-32)" + +define SERPENT_IA32 + +load_on asm_ok + +<add> +serp_ia32_imp.S +serp_ia32.cpp +serp_ia32.h +</add> + +<arch> +ia32 +</arch> + +<cc> +gcc +icc +</cc> + +# ELF systems +<os> +linux +freebsd +dragonfly +netbsd +openbsd +solaris +</os> + +<requires> +asm_ia32 +serpent +</requires> diff --git a/botan/src/block/serpent_ia32/serp_ia32.cpp b/botan/src/block/serpent_ia32/serp_ia32.cpp new file mode 100644 index 0000000..37dd4e6 --- /dev/null +++ b/botan/src/block/serpent_ia32/serp_ia32.cpp @@ -0,0 +1,51 @@ +/* +* IA-32 Serpent +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/serp_ia32.h> +#include <botan/loadstor.h> + +namespace Botan { + +extern "C" { + +void botan_serpent_ia32_encrypt(const byte[16], byte[16], const u32bit[132]); +void botan_serpent_ia32_decrypt(const byte[16], byte[16], const u32bit[132]); +void botan_serpent_ia32_key_schedule(u32bit[140]); + +} + +/* +* Serpent Encryption +*/ +void Serpent_IA32::enc(const byte in[], byte out[]) const + { + botan_serpent_ia32_encrypt(in, out, round_key); + } + +/* +* Serpent Decryption +*/ +void Serpent_IA32::dec(const byte in[], byte out[]) const + { + botan_serpent_ia32_decrypt(in, out, round_key); + } + +/* +* Serpent Key Schedule +*/ +void Serpent_IA32::key_schedule(const byte key[], u32bit length) + { + SecureBuffer<u32bit, 140> W; + for(u32bit j = 0; j != length / 4; ++j) + W[j] = make_u32bit(key[4*j+3], key[4*j+2], key[4*j+1], key[4*j]); + W[length / 4] |= u32bit(1) << ((length%4)*8); + + botan_serpent_ia32_key_schedule(W); + round_key.copy(W + 8, 132); + } + +} diff --git a/botan/src/block/serpent_ia32/serp_ia32.h b/botan/src/block/serpent_ia32/serp_ia32.h new file mode 100644 index 0000000..565e988 --- /dev/null +++ b/botan/src/block/serpent_ia32/serp_ia32.h @@ -0,0 +1,30 @@ +/* +* Serpent (IA-32) +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_SERPENT_IA32_H__ +#define BOTAN_SERPENT_IA32_H__ + +#include <botan/serpent.h> + +namespace Botan { + +/* +* Serpent +*/ +class BOTAN_DLL Serpent_IA32 : public Serpent + { + public: + BlockCipher* clone() const { return new Serpent_IA32; } + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key_schedule(const byte[], u32bit); + }; + +} + +#endif diff --git a/botan/src/block/serpent_ia32/serp_ia32_imp.S b/botan/src/block/serpent_ia32/serp_ia32_imp.S new file mode 100644 index 0000000..9e50f8c --- /dev/null +++ b/botan/src/block/serpent_ia32/serp_ia32_imp.S @@ -0,0 +1,669 @@ +/* +* Serpent Source File +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/asm_macr.h> + +START_LISTING(serp_ia32.S) + +#define SBOX_E1(A, B, C, D, T) \ + XOR(D, A) ; \ + ASSIGN(T, B) ; \ + AND(B, D) ; \ + XOR(T, C) ; \ + XOR(B, A) ; \ + OR(A, D) ; \ + XOR(A, T) ; \ + XOR(T, D) ; \ + XOR(D, C) ; \ + OR(C, B) ; \ + XOR(C, T) ; \ + NOT(T) ; \ + OR(T, B) ; \ + XOR(B, D) ; \ + XOR(B, T) ; \ + OR(D, A) ; \ + XOR(B, D) ; \ + XOR(T, D) ; \ + ASSIGN(D, A) ; \ + ASSIGN(A, B) ; \ + ASSIGN(B, T) ; + +#define SBOX_E2(A, B, C, D, T) \ + NOT(A) ; \ + NOT(C) ; \ + ASSIGN(T, A) ; \ + AND(A, B) ; \ + XOR(C, A) ; \ + OR(A, D) ; \ + XOR(D, C) ; \ + XOR(B, A) ; \ + XOR(A, T) ; \ + OR(T, B) ; \ + XOR(B, D) ; \ + OR(C, A) ; \ + AND(C, T) ; \ + XOR(A, B) ; \ + AND(B, C) ; \ + XOR(B, A) ; \ + AND(A, C) ; \ + XOR(T, A) ; \ + ASSIGN(A, C) ; \ + ASSIGN(C, D) ; \ + ASSIGN(D, B) ; \ + ASSIGN(B, T) ; + +#define SBOX_E3(A, B, C, D, T) \ + ASSIGN(T, A) ; \ + AND(A, C) ; \ + XOR(A, D) ; \ + XOR(C, B) ; \ + XOR(C, A) ; \ + OR(D, T) ; \ + XOR(D, B) ; \ + XOR(T, C) ; \ + ASSIGN(B, D) ; \ + OR(D, T) ; \ + XOR(D, A) ; \ + AND(A, B) ; \ + XOR(T, A) ; \ + XOR(B, D) ; \ + XOR(B, T) ; \ + NOT(T) ; \ + ASSIGN(A, C) ; \ + ASSIGN(C, B) ; \ + ASSIGN(B, D) ; \ + ASSIGN(D, T) ; + +#define SBOX_E4(A, B, C, D, T) \ + ASSIGN(T, A) ; \ + OR(A, D) ; \ + XOR(D, B) ; \ + AND(B, T) ; \ + XOR(T, C) ; \ + XOR(C, D) ; \ + AND(D, A) ; \ + OR(T, B) ; \ + XOR(D, T) ; \ + XOR(A, B) ; \ + AND(T, A) ; \ + XOR(B, D) ; \ + XOR(T, C) ; \ + OR(B, A) ; \ + XOR(B, C) ; \ + XOR(A, D) ; \ + ASSIGN(C, B) ; \ + OR(B, D) ; \ + XOR(B, A) ; \ + ASSIGN(A, B) ; \ + ASSIGN(B, C) ; \ + ASSIGN(C, D) ; \ + ASSIGN(D, T) ; + +#define SBOX_E5(A, B, C, D, T) \ + XOR(B, D) ; \ + NOT(D) ; \ + XOR(C, D) ; \ + XOR(D, A) ; \ + ASSIGN(T, B) ; \ + AND(B, D) ; \ + XOR(B, C) ; \ + XOR(T, D) ; \ + XOR(A, T) ; \ + AND(C, T) ; \ + XOR(C, A) ; \ + AND(A, B) ; \ + XOR(D, A) ; \ + OR(T, B) ; \ + XOR(T, A) ; \ + OR(A, D) ; \ + XOR(A, C) ; \ + AND(C, D) ; \ + NOT(A) ; \ + XOR(T, C) ; \ + ASSIGN(C, A) ; \ + ASSIGN(A, B) ; \ + ASSIGN(B, T) ; + +#define SBOX_E6(A, B, C, D, T) \ + XOR(A, B) ; \ + XOR(B, D) ; \ + NOT(D) ; \ + ASSIGN(T, B) ; \ + AND(B, A) ; \ + XOR(C, D) ; \ + XOR(B, C) ; \ + OR(C, T) ; \ + XOR(T, D) ; \ + AND(D, B) ; \ + XOR(D, A) ; \ + XOR(T, B) ; \ + XOR(T, C) ; \ + XOR(C, A) ; \ + AND(A, D) ; \ + NOT(C) ; \ + XOR(A, T) ; \ + OR(T, D) ; \ + XOR(T, C) ; \ + ASSIGN(C, A) ; \ + ASSIGN(A, B) ; \ + ASSIGN(B, D) ; \ + ASSIGN(D, T) ; + +#define SBOX_E7(A, B, C, D, T) \ + NOT(C) ; \ + ASSIGN(T, D) ; \ + AND(D, A) ; \ + XOR(A, T) ; \ + XOR(D, C) ; \ + OR(C, T) ; \ + XOR(B, D) ; \ + XOR(C, A) ; \ + OR(A, B) ; \ + XOR(C, B) ; \ + XOR(T, A) ; \ + OR(A, D) ; \ + XOR(A, C) ; \ + XOR(T, D) ; \ + XOR(T, A) ; \ + NOT(D) ; \ + AND(C, T) ; \ + XOR(C, D) ; \ + ASSIGN(D, C) ; \ + ASSIGN(C, T) ; + +#define SBOX_E8(A, B, C, D, T) \ + ASSIGN(T, B) ; \ + OR(B, C) ; \ + XOR(B, D) ; \ + XOR(T, C) ; \ + XOR(C, B) ; \ + OR(D, T) ; \ + AND(D, A) ; \ + XOR(T, C) ; \ + XOR(D, B) ; \ + OR(B, T) ; \ + XOR(B, A) ; \ + OR(A, T) ; \ + XOR(A, C) ; \ + XOR(B, T) ; \ + XOR(C, B) ; \ + AND(B, A) ; \ + XOR(B, T) ; \ + NOT(C) ; \ + OR(C, A) ; \ + XOR(T, C) ; \ + ASSIGN(C, B) ; \ + ASSIGN(B, D) ; \ + ASSIGN(D, A) ; \ + ASSIGN(A, T) ; + +#define SBOX_D1(A, B, C, D, T) \ + NOT(C) ; \ + ASSIGN(T, B) ; \ + OR(B, A) ; \ + NOT(T) ; \ + XOR(B, C) ; \ + OR(C, T) ; \ + XOR(B, D) ; \ + XOR(A, T) ; \ + XOR(C, A) ; \ + AND(A, D) ; \ + XOR(T, A) ; \ + OR(A, B) ; \ + XOR(A, C) ; \ + XOR(D, T) ; \ + XOR(C, B) ; \ + XOR(D, A) ; \ + XOR(D, B) ; \ + AND(C, D) ; \ + XOR(T, C) ; \ + ASSIGN(C, B) ; \ + ASSIGN(B, T) ; + +#define SBOX_D2(A, B, C, D, T) \ + ASSIGN(T, B) ; \ + XOR(B, D) ; \ + AND(D, B) ; \ + XOR(T, C) ; \ + XOR(D, A) ; \ + OR(A, B) ; \ + XOR(C, D) ; \ + XOR(A, T) ; \ + OR(A, C) ; \ + XOR(B, D) ; \ + XOR(A, B) ; \ + OR(B, D) ; \ + XOR(B, A) ; \ + NOT(T) ; \ + XOR(T, B) ; \ + OR(B, A) ; \ + XOR(B, A) ; \ + OR(B, T) ; \ + XOR(D, B) ; \ + ASSIGN(B, A) ; \ + ASSIGN(A, T) ; \ + ASSIGN(T, D) ; \ + ASSIGN(D, C) ; \ + ASSIGN(C, T) ; + +#define SBOX_D3(A, B, C, D, T) \ + XOR(C, D) ; \ + XOR(D, A) ; \ + ASSIGN(T, D) ; \ + AND(D, C) ; \ + XOR(D, B) ; \ + OR(B, C) ; \ + XOR(B, T) ; \ + AND(T, D) ; \ + XOR(C, D) ; \ + AND(T, A) ; \ + XOR(T, C) ; \ + AND(C, B) ; \ + OR(C, A) ; \ + NOT(D) ; \ + XOR(C, D) ; \ + XOR(A, D) ; \ + AND(A, B) ; \ + XOR(D, T) ; \ + XOR(D, A) ; \ + ASSIGN(A, B) ; \ + ASSIGN(B, T) ; + +#define SBOX_D4(A, B, C, D, T) \ + ASSIGN(T, C) ; \ + XOR(C, B) ; \ + XOR(A, C) ; \ + AND(T, C) ; \ + XOR(T, A) ; \ + AND(A, B) ; \ + XOR(B, D) ; \ + OR(D, T) ; \ + XOR(C, D) ; \ + XOR(A, D) ; \ + XOR(B, T) ; \ + AND(D, C) ; \ + XOR(D, B) ; \ + XOR(B, A) ; \ + OR(B, C) ; \ + XOR(A, D) ; \ + XOR(B, T) ; \ + XOR(A, B) ; \ + ASSIGN(T, A) ; \ + ASSIGN(A, C) ; \ + ASSIGN(C, D) ; \ + ASSIGN(D, T) ; + +#define SBOX_D5(A, B, C, D, T) \ + ASSIGN(T, C) ; \ + AND(C, D) ; \ + XOR(C, B) ; \ + OR(B, D) ; \ + AND(B, A) ; \ + XOR(T, C) ; \ + XOR(T, B) ; \ + AND(B, C) ; \ + NOT(A) ; \ + XOR(D, T) ; \ + XOR(B, D) ; \ + AND(D, A) ; \ + XOR(D, C) ; \ + XOR(A, B) ; \ + AND(C, A) ; \ + XOR(D, A) ; \ + XOR(C, T) ; \ + OR(C, D) ; \ + XOR(D, A) ; \ + XOR(C, B) ; \ + ASSIGN(B, D) ; \ + ASSIGN(D, T) ; + +#define SBOX_D6(A, B, C, D, T) \ + NOT(B) ; \ + ASSIGN(T, D) ; \ + XOR(C, B) ; \ + OR(D, A) ; \ + XOR(D, C) ; \ + OR(C, B) ; \ + AND(C, A) ; \ + XOR(T, D) ; \ + XOR(C, T) ; \ + OR(T, A) ; \ + XOR(T, B) ; \ + AND(B, C) ; \ + XOR(B, D) ; \ + XOR(T, C) ; \ + AND(D, T) ; \ + XOR(T, B) ; \ + XOR(D, T) ; \ + NOT(T) ; \ + XOR(D, A) ; \ + ASSIGN(A, B) ; \ + ASSIGN(B, T) ; \ + ASSIGN(T, D) ; \ + ASSIGN(D, C) ; \ + ASSIGN(C, T) ; + +#define SBOX_D7(A, B, C, D, T) \ + XOR(A, C) ; \ + ASSIGN(T, C) ; \ + AND(C, A) ; \ + XOR(T, D) ; \ + NOT(C) ; \ + XOR(D, B) ; \ + XOR(C, D) ; \ + OR(T, A) ; \ + XOR(A, C) ; \ + XOR(D, T) ; \ + XOR(T, B) ; \ + AND(B, D) ; \ + XOR(B, A) ; \ + XOR(A, D) ; \ + OR(A, C) ; \ + XOR(D, B) ; \ + XOR(T, A) ; \ + ASSIGN(A, B) ; \ + ASSIGN(B, C) ; \ + ASSIGN(C, T) ; + +#define SBOX_D8(A, B, C, D, T) \ + ASSIGN(T, C) ; \ + XOR(C, A) ; \ + AND(A, D) ; \ + OR(T, D) ; \ + NOT(C) ; \ + XOR(D, B) ; \ + OR(B, A) ; \ + XOR(A, C) ; \ + AND(C, T) ; \ + AND(D, T) ; \ + XOR(B, C) ; \ + XOR(C, A) ; \ + OR(A, C) ; \ + XOR(T, B) ; \ + XOR(A, D) ; \ + XOR(D, T) ; \ + OR(T, A) ; \ + XOR(D, C) ; \ + XOR(T, C) ; \ + ASSIGN(C, B) ; \ + ASSIGN(B, A) ; \ + ASSIGN(A, D) ; \ + ASSIGN(D, T) ; + +#define TRANSFORM(A, B, C, D, T) \ + ROTL_IMM(A, 13) ; \ + ROTL_IMM(C, 3) ; \ + SHL2_3(T, A) ; \ + XOR(B, A) ; \ + XOR(D, C) ; \ + XOR(B, C) ; \ + XOR(D, T) ; \ + ROTL_IMM(B, 1) ; \ + ROTL_IMM(D, 7) ; \ + ASSIGN(T, B) ; \ + SHL_IMM(T, 7) ; \ + XOR(A, B) ; \ + XOR(C, D) ; \ + XOR(A, D) ; \ + XOR(C, T) ; \ + ROTL_IMM(A, 5) ; \ + ROTL_IMM(C, 22) ; + +#define I_TRANSFORM(A, B, C, D, T) \ + ROTR_IMM(C, 22) ; \ + ROTR_IMM(A, 5) ; \ + ASSIGN(T, B) ; \ + SHL_IMM(T, 7) ; \ + XOR(A, B) ; \ + XOR(C, D) ; \ + XOR(A, D) ; \ + XOR(C, T) ; \ + ROTR_IMM(D, 7) ; \ + ROTR_IMM(B, 1) ; \ + SHL2_3(T, A) ; \ + XOR(B, C) ; \ + XOR(D, C) ; \ + XOR(B, A) ; \ + XOR(D, T) ; \ + ROTR_IMM(C, 3) ; \ + ROTR_IMM(A, 13) ; + +#define KEY_XOR(A, B, C, D, N) \ + XOR(A, ARRAY4(EDI, (4*N ))) ; \ + XOR(B, ARRAY4(EDI, (4*N+1))) ; \ + XOR(C, ARRAY4(EDI, (4*N+2))) ; \ + XOR(D, ARRAY4(EDI, (4*N+3))) ; + +/* +* Serpent Encryption +*/ +START_FUNCTION(botan_serpent_ia32_encrypt) + SPILL_REGS() +#define PUSHED 4 + + ASSIGN(EBP, ARG(1)) /* input block */ + ASSIGN(EAX, ARRAY4(EBP, 0)) + ASSIGN(EBX, ARRAY4(EBP, 1)) + ASSIGN(ECX, ARRAY4(EBP, 2)) + ASSIGN(EDX, ARRAY4(EBP, 3)) + + ASSIGN(EDI, ARG(3)) /* round keys */ + ZEROIZE(EBP) + +#define E_ROUND(A, B, C, D, T, N, SBOX) \ + KEY_XOR(A, B, C, D, N) \ + SBOX(A, B, C, D, T) \ + TRANSFORM(A, B, C, D, T) + + + E_ROUND(EAX, EBX, ECX, EDX, EBP, 0, SBOX_E1) + E_ROUND(EAX, EBX, ECX, EDX, EBP, 1, SBOX_E2) + E_ROUND(EAX, EBX, ECX, EDX, EBP, 2, SBOX_E3) + E_ROUND(EAX, EBX, ECX, EDX, EBP, 3, SBOX_E4) + E_ROUND(EAX, EBX, ECX, EDX, EBP, 4, SBOX_E5) + E_ROUND(EAX, EBX, ECX, EDX, EBP, 5, SBOX_E6) + E_ROUND(EAX, EBX, ECX, EDX, EBP, 6, SBOX_E7) + E_ROUND(EAX, EBX, ECX, EDX, EBP, 7, SBOX_E8) + + E_ROUND(EAX, EBX, ECX, EDX, EBP, 8, SBOX_E1) + E_ROUND(EAX, EBX, ECX, EDX, EBP, 9, SBOX_E2) + E_ROUND(EAX, EBX, ECX, EDX, EBP, 10, SBOX_E3) + E_ROUND(EAX, EBX, ECX, EDX, EBP, 11, SBOX_E4) + E_ROUND(EAX, EBX, ECX, EDX, EBP, 12, SBOX_E5) + E_ROUND(EAX, EBX, ECX, EDX, EBP, 13, SBOX_E6) + E_ROUND(EAX, EBX, ECX, EDX, EBP, 14, SBOX_E7) + E_ROUND(EAX, EBX, ECX, EDX, EBP, 15, SBOX_E8) + + E_ROUND(EAX, EBX, ECX, EDX, EBP, 16, SBOX_E1) + E_ROUND(EAX, EBX, ECX, EDX, EBP, 17, SBOX_E2) + E_ROUND(EAX, EBX, ECX, EDX, EBP, 18, SBOX_E3) + E_ROUND(EAX, EBX, ECX, EDX, EBP, 19, SBOX_E4) + E_ROUND(EAX, EBX, ECX, EDX, EBP, 20, SBOX_E5) + E_ROUND(EAX, EBX, ECX, EDX, EBP, 21, SBOX_E6) + E_ROUND(EAX, EBX, ECX, EDX, EBP, 22, SBOX_E7) + E_ROUND(EAX, EBX, ECX, EDX, EBP, 23, SBOX_E8) + + E_ROUND(EAX, EBX, ECX, EDX, EBP, 24, SBOX_E1) + E_ROUND(EAX, EBX, ECX, EDX, EBP, 25, SBOX_E2) + E_ROUND(EAX, EBX, ECX, EDX, EBP, 26, SBOX_E3) + E_ROUND(EAX, EBX, ECX, EDX, EBP, 27, SBOX_E4) + E_ROUND(EAX, EBX, ECX, EDX, EBP, 28, SBOX_E5) + E_ROUND(EAX, EBX, ECX, EDX, EBP, 29, SBOX_E6) + E_ROUND(EAX, EBX, ECX, EDX, EBP, 30, SBOX_E7) + + KEY_XOR(EAX, EBX, ECX, EDX, 31) + SBOX_E8(EAX, EBX, ECX, EDX, EBP) + KEY_XOR(EAX, EBX, ECX, EDX, 32) + + ASSIGN(EBP, ARG(2)) /* output block */ + ASSIGN(ARRAY4(EBP, 0), EAX) + ASSIGN(ARRAY4(EBP, 1), EBX) + ASSIGN(ARRAY4(EBP, 2), ECX) + ASSIGN(ARRAY4(EBP, 3), EDX) + + RESTORE_REGS() +#undef PUSHED +END_FUNCTION(botan_serpent_ia32_encrypt) + +/* +* Serpent Decryption +*/ +START_FUNCTION(botan_serpent_ia32_decrypt) + SPILL_REGS() +#define PUSHED 4 + + ASSIGN(EBP, ARG(1)) /* input block */ + ASSIGN(EAX, ARRAY4(EBP, 0)) + ASSIGN(EBX, ARRAY4(EBP, 1)) + ASSIGN(ECX, ARRAY4(EBP, 2)) + ASSIGN(EDX, ARRAY4(EBP, 3)) + + ASSIGN(EDI, ARG(3)) /* round keys */ + + ZEROIZE(EBP) + +#define D_ROUND(A, B, C, D, T, N, SBOX) \ + I_TRANSFORM(A, B, C, D, T) \ + SBOX(A, B, C, D, T) \ + KEY_XOR(A, B, C, D, N) \ + + KEY_XOR(EAX, EBX, ECX, EDX, 32) + SBOX_D8(EAX, EBX, ECX, EDX, EBP) + KEY_XOR(EAX, EBX, ECX, EDX, 31) + + D_ROUND(EAX, EBX, ECX, EDX, EBP, 30, SBOX_D7) + D_ROUND(EAX, EBX, ECX, EDX, EBP, 29, SBOX_D6) + D_ROUND(EAX, EBX, ECX, EDX, EBP, 28, SBOX_D5) + D_ROUND(EAX, EBX, ECX, EDX, EBP, 27, SBOX_D4) + D_ROUND(EAX, EBX, ECX, EDX, EBP, 26, SBOX_D3) + D_ROUND(EAX, EBX, ECX, EDX, EBP, 25, SBOX_D2) + D_ROUND(EAX, EBX, ECX, EDX, EBP, 24, SBOX_D1) + + D_ROUND(EAX, EBX, ECX, EDX, EBP, 23, SBOX_D8) + D_ROUND(EAX, EBX, ECX, EDX, EBP, 22, SBOX_D7) + D_ROUND(EAX, EBX, ECX, EDX, EBP, 21, SBOX_D6) + D_ROUND(EAX, EBX, ECX, EDX, EBP, 20, SBOX_D5) + D_ROUND(EAX, EBX, ECX, EDX, EBP, 19, SBOX_D4) + D_ROUND(EAX, EBX, ECX, EDX, EBP, 18, SBOX_D3) + D_ROUND(EAX, EBX, ECX, EDX, EBP, 17, SBOX_D2) + D_ROUND(EAX, EBX, ECX, EDX, EBP, 16, SBOX_D1) + + D_ROUND(EAX, EBX, ECX, EDX, EBP, 15, SBOX_D8) + D_ROUND(EAX, EBX, ECX, EDX, EBP, 14, SBOX_D7) + D_ROUND(EAX, EBX, ECX, EDX, EBP, 13, SBOX_D6) + D_ROUND(EAX, EBX, ECX, EDX, EBP, 12, SBOX_D5) + D_ROUND(EAX, EBX, ECX, EDX, EBP, 11, SBOX_D4) + D_ROUND(EAX, EBX, ECX, EDX, EBP, 10, SBOX_D3) + D_ROUND(EAX, EBX, ECX, EDX, EBP, 9, SBOX_D2) + D_ROUND(EAX, EBX, ECX, EDX, EBP, 8, SBOX_D1) + + D_ROUND(EAX, EBX, ECX, EDX, EBP, 7, SBOX_D8) + D_ROUND(EAX, EBX, ECX, EDX, EBP, 6, SBOX_D7) + D_ROUND(EAX, EBX, ECX, EDX, EBP, 5, SBOX_D6) + D_ROUND(EAX, EBX, ECX, EDX, EBP, 4, SBOX_D5) + D_ROUND(EAX, EBX, ECX, EDX, EBP, 3, SBOX_D4) + D_ROUND(EAX, EBX, ECX, EDX, EBP, 2, SBOX_D3) + D_ROUND(EAX, EBX, ECX, EDX, EBP, 1, SBOX_D2) + D_ROUND(EAX, EBX, ECX, EDX, EBP, 0, SBOX_D1) + + ASSIGN(EBP, ARG(2)) /* output block */ + ASSIGN(ARRAY4(EBP, 0), EAX) + ASSIGN(ARRAY4(EBP, 1), EBX) + ASSIGN(ARRAY4(EBP, 2), ECX) + ASSIGN(ARRAY4(EBP, 3), EDX) + + RESTORE_REGS() +#undef PUSHED +END_FUNCTION(botan_serpent_ia32_decrypt) + +/* +* Serpent Key Schedule +*/ +START_FUNCTION(botan_serpent_ia32_key_schedule) + SPILL_REGS() +#define PUSHED 4 + + ASSIGN(EDI, ARG(1)) /* round keys */ + ASSIGN(ESI, IMM(8)) + ADD_IMM(EDI, 32) + +START_LOOP(.EXPANSION) + ASSIGN(EAX, ARRAY4(EDI, -1)) + ASSIGN(EBX, ARRAY4(EDI, -3)) + ASSIGN(ECX, ARRAY4(EDI, -5)) + ASSIGN(EDX, ARRAY4(EDI, -8)) + + ASSIGN(EBP, ESI) + SUB_IMM(EBP, 8) + XOR(EBP, IMM(0x9E3779B9)) + XOR(EAX, EBX) + XOR(ECX, EDX) + XOR(EAX, EBP) + XOR(EAX, ECX) + + ROTL_IMM(EAX, 11) + + ASSIGN(ARRAY4(EDI, 0), EAX) + + ADD_IMM(ESI, 1) + ADD_IMM(EDI, 4) +LOOP_UNTIL_EQ(ESI, 140, .EXPANSION) + + ASSIGN(EDI, ARG(1)) /* round keys */ + +#define LOAD_AND_SBOX(MSG, SBOX) \ + ASSIGN(EAX, ARRAY4(EDI, (4*MSG+ 8))) ; \ + ASSIGN(EBX, ARRAY4(EDI, (4*MSG+ 9))) ; \ + ASSIGN(ECX, ARRAY4(EDI, (4*MSG+10))) ; \ + ASSIGN(EDX, ARRAY4(EDI, (4*MSG+11))) ; \ + SBOX(EAX, EBX, ECX, EDX, EBP) ; \ + ASSIGN(ARRAY4(EDI, (4*MSG+ 8)), EAX) ; \ + ASSIGN(ARRAY4(EDI, (4*MSG+ 9)), EBX) ; \ + ASSIGN(ARRAY4(EDI, (4*MSG+10)), ECX) ; \ + ASSIGN(ARRAY4(EDI, (4*MSG+11)), EDX) + + LOAD_AND_SBOX( 0, SBOX_E4) + LOAD_AND_SBOX( 1, SBOX_E3) + LOAD_AND_SBOX( 2, SBOX_E2) + LOAD_AND_SBOX( 3, SBOX_E1) + + LOAD_AND_SBOX( 4, SBOX_E8) + LOAD_AND_SBOX( 5, SBOX_E7) + LOAD_AND_SBOX( 6, SBOX_E6) + LOAD_AND_SBOX( 7, SBOX_E5) + LOAD_AND_SBOX( 8, SBOX_E4) + LOAD_AND_SBOX( 9, SBOX_E3) + LOAD_AND_SBOX(10, SBOX_E2) + LOAD_AND_SBOX(11, SBOX_E1) + + LOAD_AND_SBOX(12, SBOX_E8) + LOAD_AND_SBOX(13, SBOX_E7) + LOAD_AND_SBOX(14, SBOX_E6) + LOAD_AND_SBOX(15, SBOX_E5) + LOAD_AND_SBOX(16, SBOX_E4) + LOAD_AND_SBOX(17, SBOX_E3) + LOAD_AND_SBOX(18, SBOX_E2) + LOAD_AND_SBOX(19, SBOX_E1) + + LOAD_AND_SBOX(20, SBOX_E8) + LOAD_AND_SBOX(21, SBOX_E7) + LOAD_AND_SBOX(22, SBOX_E6) + LOAD_AND_SBOX(23, SBOX_E5) + LOAD_AND_SBOX(24, SBOX_E4) + LOAD_AND_SBOX(25, SBOX_E3) + LOAD_AND_SBOX(26, SBOX_E2) + LOAD_AND_SBOX(27, SBOX_E1) + + LOAD_AND_SBOX(28, SBOX_E8) + LOAD_AND_SBOX(29, SBOX_E7) + LOAD_AND_SBOX(30, SBOX_E6) + LOAD_AND_SBOX(31, SBOX_E5) + LOAD_AND_SBOX(32, SBOX_E4) + + RESTORE_REGS() +#undef PUSHED +END_FUNCTION(botan_serpent_ia32_key_schedule) diff --git a/botan/src/block/skipjack/info.txt b/botan/src/block/skipjack/info.txt new file mode 100644 index 0000000..4b38d6d --- /dev/null +++ b/botan/src/block/skipjack/info.txt @@ -0,0 +1,10 @@ +realname "Skipjack" + +define SKIPJACK + +load_on auto + +<add> +skipjack.cpp +skipjack.h +</add> diff --git a/botan/src/block/skipjack/skipjack.cpp b/botan/src/block/skipjack/skipjack.cpp new file mode 100644 index 0000000..f5ffc86 --- /dev/null +++ b/botan/src/block/skipjack/skipjack.cpp @@ -0,0 +1,162 @@ +/* +* Skipjack +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/skipjack.h> +#include <botan/loadstor.h> + +namespace Botan { + +/* +* Skipjack Encryption +*/ +void Skipjack::enc(const byte in[], byte out[]) const + { + u16bit W1 = load_le<u16bit>(in, 3); + u16bit W2 = load_le<u16bit>(in, 2); + u16bit W3 = load_le<u16bit>(in, 1); + u16bit W4 = load_le<u16bit>(in, 0); + + step_A(W1,W4, 1); step_A(W4,W3, 2); step_A(W3,W2, 3); step_A(W2,W1, 4); + step_A(W1,W4, 5); step_A(W4,W3, 6); step_A(W3,W2, 7); step_A(W2,W1, 8); + + step_B(W1,W2, 9); step_B(W4,W1,10); step_B(W3,W4,11); step_B(W2,W3,12); + step_B(W1,W2,13); step_B(W4,W1,14); step_B(W3,W4,15); step_B(W2,W3,16); + + step_A(W1,W4,17); step_A(W4,W3,18); step_A(W3,W2,19); step_A(W2,W1,20); + step_A(W1,W4,21); step_A(W4,W3,22); step_A(W3,W2,23); step_A(W2,W1,24); + + step_B(W1,W2,25); step_B(W4,W1,26); step_B(W3,W4,27); step_B(W2,W3,28); + step_B(W1,W2,29); step_B(W4,W1,30); step_B(W3,W4,31); step_B(W2,W3,32); + + store_le(out, W4, W3, W2, W1); + } + +/* +* Skipjack Decryption +*/ +void Skipjack::dec(const byte in[], byte out[]) const + { + u16bit W1 = load_le<u16bit>(in, 3); + u16bit W2 = load_le<u16bit>(in, 2); + u16bit W3 = load_le<u16bit>(in, 1); + u16bit W4 = load_le<u16bit>(in, 0); + + step_Bi(W2,W3,32); step_Bi(W3,W4,31); step_Bi(W4,W1,30); step_Bi(W1,W2,29); + step_Bi(W2,W3,28); step_Bi(W3,W4,27); step_Bi(W4,W1,26); step_Bi(W1,W2,25); + + step_Ai(W1,W2,24); step_Ai(W2,W3,23); step_Ai(W3,W4,22); step_Ai(W4,W1,21); + step_Ai(W1,W2,20); step_Ai(W2,W3,19); step_Ai(W3,W4,18); step_Ai(W4,W1,17); + + step_Bi(W2,W3,16); step_Bi(W3,W4,15); step_Bi(W4,W1,14); step_Bi(W1,W2,13); + step_Bi(W2,W3,12); step_Bi(W3,W4,11); step_Bi(W4,W1,10); step_Bi(W1,W2, 9); + + step_Ai(W1,W2, 8); step_Ai(W2,W3, 7); step_Ai(W3,W4, 6); step_Ai(W4,W1, 5); + step_Ai(W1,W2, 4); step_Ai(W2,W3, 3); step_Ai(W3,W4, 2); step_Ai(W4,W1, 1); + + store_le(out, W4, W3, W2, W1); + } + +/* +* Skipjack Stepping Rule 'A' +*/ +void Skipjack::step_A(u16bit& W1, u16bit& W4, u32bit round) const + { + byte G1 = get_byte(0, W1), G2 = get_byte(1, W1), G3; + G3 = FTABLE[(4 * round - 4) % 10][G2] ^ G1; + G1 = FTABLE[(4 * round - 3) % 10][G3] ^ G2; + G2 = FTABLE[(4 * round - 2) % 10][G1] ^ G3; + G3 = FTABLE[(4 * round - 1) % 10][G2] ^ G1; + W1 = make_u16bit(G2, G3); + W4 ^= W1 ^ round; + } + +/* +* Skipjack Stepping Rule 'B' +*/ +void Skipjack::step_B(u16bit& W1, u16bit& W2, u32bit round) const + { + W2 ^= W1 ^ round; + byte G1 = get_byte(0, W1), G2 = get_byte(1, W1), G3; + G3 = FTABLE[(4 * round - 4) % 10][G2] ^ G1; + G1 = FTABLE[(4 * round - 3) % 10][G3] ^ G2; + G2 = FTABLE[(4 * round - 2) % 10][G1] ^ G3; + G3 = FTABLE[(4 * round - 1) % 10][G2] ^ G1; + W1 = make_u16bit(G2, G3); + } + +/* +* Skipjack Invserse Stepping Rule 'A' +*/ +void Skipjack::step_Ai(u16bit& W1, u16bit& W2, u32bit round) const + { + W1 ^= W2 ^ round; + byte G1 = get_byte(1, W2), G2 = get_byte(0, W2), G3; + G3 = FTABLE[(4 * round - 1) % 10][G2] ^ G1; + G1 = FTABLE[(4 * round - 2) % 10][G3] ^ G2; + G2 = FTABLE[(4 * round - 3) % 10][G1] ^ G3; + G3 = FTABLE[(4 * round - 4) % 10][G2] ^ G1; + W2 = make_u16bit(G3, G2); + } + +/* +* Skipjack Invserse Stepping Rule 'B' +*/ +void Skipjack::step_Bi(u16bit& W2, u16bit& W3, u32bit round) const + { + byte G1 = get_byte(1, W2), G2 = get_byte(0, W2), G3; + G3 = FTABLE[(4 * round - 1) % 10][G2] ^ G1; + G1 = FTABLE[(4 * round - 2) % 10][G3] ^ G2; + G2 = FTABLE[(4 * round - 3) % 10][G1] ^ G3; + G3 = FTABLE[(4 * round - 4) % 10][G2] ^ G1; + W2 = make_u16bit(G3, G2); + W3 ^= W2 ^ round; + } + +/* +* Skipjack Key Schedule +*/ +void Skipjack::key_schedule(const byte key[], u32bit) + { + static const byte F[256] = { + 0xA3, 0xD7, 0x09, 0x83, 0xF8, 0x48, 0xF6, 0xF4, 0xB3, 0x21, 0x15, 0x78, + 0x99, 0xB1, 0xAF, 0xF9, 0xE7, 0x2D, 0x4D, 0x8A, 0xCE, 0x4C, 0xCA, 0x2E, + 0x52, 0x95, 0xD9, 0x1E, 0x4E, 0x38, 0x44, 0x28, 0x0A, 0xDF, 0x02, 0xA0, + 0x17, 0xF1, 0x60, 0x68, 0x12, 0xB7, 0x7A, 0xC3, 0xE9, 0xFA, 0x3D, 0x53, + 0x96, 0x84, 0x6B, 0xBA, 0xF2, 0x63, 0x9A, 0x19, 0x7C, 0xAE, 0xE5, 0xF5, + 0xF7, 0x16, 0x6A, 0xA2, 0x39, 0xB6, 0x7B, 0x0F, 0xC1, 0x93, 0x81, 0x1B, + 0xEE, 0xB4, 0x1A, 0xEA, 0xD0, 0x91, 0x2F, 0xB8, 0x55, 0xB9, 0xDA, 0x85, + 0x3F, 0x41, 0xBF, 0xE0, 0x5A, 0x58, 0x80, 0x5F, 0x66, 0x0B, 0xD8, 0x90, + 0x35, 0xD5, 0xC0, 0xA7, 0x33, 0x06, 0x65, 0x69, 0x45, 0x00, 0x94, 0x56, + 0x6D, 0x98, 0x9B, 0x76, 0x97, 0xFC, 0xB2, 0xC2, 0xB0, 0xFE, 0xDB, 0x20, + 0xE1, 0xEB, 0xD6, 0xE4, 0xDD, 0x47, 0x4A, 0x1D, 0x42, 0xED, 0x9E, 0x6E, + 0x49, 0x3C, 0xCD, 0x43, 0x27, 0xD2, 0x07, 0xD4, 0xDE, 0xC7, 0x67, 0x18, + 0x89, 0xCB, 0x30, 0x1F, 0x8D, 0xC6, 0x8F, 0xAA, 0xC8, 0x74, 0xDC, 0xC9, + 0x5D, 0x5C, 0x31, 0xA4, 0x70, 0x88, 0x61, 0x2C, 0x9F, 0x0D, 0x2B, 0x87, + 0x50, 0x82, 0x54, 0x64, 0x26, 0x7D, 0x03, 0x40, 0x34, 0x4B, 0x1C, 0x73, + 0xD1, 0xC4, 0xFD, 0x3B, 0xCC, 0xFB, 0x7F, 0xAB, 0xE6, 0x3E, 0x5B, 0xA5, + 0xAD, 0x04, 0x23, 0x9C, 0x14, 0x51, 0x22, 0xF0, 0x29, 0x79, 0x71, 0x7E, + 0xFF, 0x8C, 0x0E, 0xE2, 0x0C, 0xEF, 0xBC, 0x72, 0x75, 0x6F, 0x37, 0xA1, + 0xEC, 0xD3, 0x8E, 0x62, 0x8B, 0x86, 0x10, 0xE8, 0x08, 0x77, 0x11, 0xBE, + 0x92, 0x4F, 0x24, 0xC5, 0x32, 0x36, 0x9D, 0xCF, 0xF3, 0xA6, 0xBB, 0xAC, + 0x5E, 0x6C, 0xA9, 0x13, 0x57, 0x25, 0xB5, 0xE3, 0xBD, 0xA8, 0x3A, 0x01, + 0x05, 0x59, 0x2A, 0x46 }; + + for(u32bit j = 0; j != 10; ++j) + for(u32bit k = 0; k != 256; ++k) + FTABLE[j][k] = F[k ^ key[9-j]]; + } + +/* +* Clear memory of sensitive data +*/ +void Skipjack::clear() throw() + { + for(u32bit j = 0; j != 10; ++j) + FTABLE[j].clear(); + } + +} diff --git a/botan/src/block/skipjack/skipjack.h b/botan/src/block/skipjack/skipjack.h new file mode 100644 index 0000000..231cd9c --- /dev/null +++ b/botan/src/block/skipjack/skipjack.h @@ -0,0 +1,38 @@ +/* +* Skipjack +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_SKIPJACK_H__ +#define BOTAN_SKIPJACK_H__ + +#include <botan/block_cipher.h> + +namespace Botan { + +/* +* Skipjack +*/ +class BOTAN_DLL Skipjack : public BlockCipher + { + public: + void clear() throw(); + std::string name() const { return "Skipjack"; } + BlockCipher* clone() const { return new Skipjack; } + Skipjack() : BlockCipher(8, 10) {} + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key_schedule(const byte[], u32bit); + void step_A(u16bit&, u16bit&, u32bit) const; + void step_B(u16bit&, u16bit&, u32bit) const; + void step_Ai(u16bit&, u16bit&, u32bit) const; + void step_Bi(u16bit&, u16bit&, u32bit) const; + SecureBuffer<byte, 256> FTABLE[10]; + }; + +} + +#endif diff --git a/botan/src/block/square/info.txt b/botan/src/block/square/info.txt new file mode 100644 index 0000000..a206979 --- /dev/null +++ b/botan/src/block/square/info.txt @@ -0,0 +1,11 @@ +realname "Square" + +define SQUARE + +load_on auto + +<add> +sqr_tab.cpp +square.cpp +square.h +</add> diff --git a/botan/src/block/square/sqr_tab.cpp b/botan/src/block/square/sqr_tab.cpp new file mode 100644 index 0000000..331bf3e --- /dev/null +++ b/botan/src/block/square/sqr_tab.cpp @@ -0,0 +1,460 @@ +/* +* S-Box and Diffusion Tables for Square +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/square.h> + +namespace Botan { + +const byte Square::SE[256] = { +0xB1, 0xCE, 0xC3, 0x95, 0x5A, 0xAD, 0xE7, 0x02, 0x4D, 0x44, 0xFB, 0x91, 0x0C, +0x87, 0xA1, 0x50, 0xCB, 0x67, 0x54, 0xDD, 0x46, 0x8F, 0xE1, 0x4E, 0xF0, 0xFD, +0xFC, 0xEB, 0xF9, 0xC4, 0x1A, 0x6E, 0x5E, 0xF5, 0xCC, 0x8D, 0x1C, 0x56, 0x43, +0xFE, 0x07, 0x61, 0xF8, 0x75, 0x59, 0xFF, 0x03, 0x22, 0x8A, 0xD1, 0x13, 0xEE, +0x88, 0x00, 0x0E, 0x34, 0x15, 0x80, 0x94, 0xE3, 0xED, 0xB5, 0x53, 0x23, 0x4B, +0x47, 0x17, 0xA7, 0x90, 0x35, 0xAB, 0xD8, 0xB8, 0xDF, 0x4F, 0x57, 0x9A, 0x92, +0xDB, 0x1B, 0x3C, 0xC8, 0x99, 0x04, 0x8E, 0xE0, 0xD7, 0x7D, 0x85, 0xBB, 0x40, +0x2C, 0x3A, 0x45, 0xF1, 0x42, 0x65, 0x20, 0x41, 0x18, 0x72, 0x25, 0x93, 0x70, +0x36, 0x05, 0xF2, 0x0B, 0xA3, 0x79, 0xEC, 0x08, 0x27, 0x31, 0x32, 0xB6, 0x7C, +0xB0, 0x0A, 0x73, 0x5B, 0x7B, 0xB7, 0x81, 0xD2, 0x0D, 0x6A, 0x26, 0x9E, 0x58, +0x9C, 0x83, 0x74, 0xB3, 0xAC, 0x30, 0x7A, 0x69, 0x77, 0x0F, 0xAE, 0x21, 0xDE, +0xD0, 0x2E, 0x97, 0x10, 0xA4, 0x98, 0xA8, 0xD4, 0x68, 0x2D, 0x62, 0x29, 0x6D, +0x16, 0x49, 0x76, 0xC7, 0xE8, 0xC1, 0x96, 0x37, 0xE5, 0xCA, 0xF4, 0xE9, 0x63, +0x12, 0xC2, 0xA6, 0x14, 0xBC, 0xD3, 0x28, 0xAF, 0x2F, 0xE6, 0x24, 0x52, 0xC6, +0xA0, 0x09, 0xBD, 0x8C, 0xCF, 0x5D, 0x11, 0x5F, 0x01, 0xC5, 0x9F, 0x3D, 0xA2, +0x9B, 0xC9, 0x3B, 0xBE, 0x51, 0x19, 0x1F, 0x3F, 0x5C, 0xB2, 0xEF, 0x4A, 0xCD, +0xBF, 0xBA, 0x6F, 0x64, 0xD9, 0xF3, 0x3E, 0xB4, 0xAA, 0xDC, 0xD5, 0x06, 0xC0, +0x7E, 0xF6, 0x66, 0x6C, 0x84, 0x71, 0x38, 0xB9, 0x1D, 0x7F, 0x9D, 0x48, 0x8B, +0x2A, 0xDA, 0xA5, 0x33, 0x82, 0x39, 0xD6, 0x78, 0x86, 0xFA, 0xE4, 0x2B, 0xA9, +0x1E, 0x89, 0x60, 0x6B, 0xEA, 0x55, 0x4C, 0xF7, 0xE2 }; + +const byte Square::SD[256] = { +0x35, 0xBE, 0x07, 0x2E, 0x53, 0x69, 0xDB, 0x28, 0x6F, 0xB7, 0x76, 0x6B, 0x0C, +0x7D, 0x36, 0x8B, 0x92, 0xBC, 0xA9, 0x32, 0xAC, 0x38, 0x9C, 0x42, 0x63, 0xC8, +0x1E, 0x4F, 0x24, 0xE5, 0xF7, 0xC9, 0x61, 0x8D, 0x2F, 0x3F, 0xB3, 0x65, 0x7F, +0x70, 0xAF, 0x9A, 0xEA, 0xF5, 0x5B, 0x98, 0x90, 0xB1, 0x87, 0x71, 0x72, 0xED, +0x37, 0x45, 0x68, 0xA3, 0xE3, 0xEF, 0x5C, 0xC5, 0x50, 0xC1, 0xD6, 0xCA, 0x5A, +0x62, 0x5F, 0x26, 0x09, 0x5D, 0x14, 0x41, 0xE8, 0x9D, 0xCE, 0x40, 0xFD, 0x08, +0x17, 0x4A, 0x0F, 0xC7, 0xB4, 0x3E, 0x12, 0xFC, 0x25, 0x4B, 0x81, 0x2C, 0x04, +0x78, 0xCB, 0xBB, 0x20, 0xBD, 0xF9, 0x29, 0x99, 0xA8, 0xD3, 0x60, 0xDF, 0x11, +0x97, 0x89, 0x7E, 0xFA, 0xE0, 0x9B, 0x1F, 0xD2, 0x67, 0xE2, 0x64, 0x77, 0x84, +0x2B, 0x9E, 0x8A, 0xF1, 0x6D, 0x88, 0x79, 0x74, 0x57, 0xDD, 0xE6, 0x39, 0x7B, +0xEE, 0x83, 0xE1, 0x58, 0xF2, 0x0D, 0x34, 0xF8, 0x30, 0xE9, 0xB9, 0x23, 0x54, +0x15, 0x44, 0x0B, 0x4D, 0x66, 0x3A, 0x03, 0xA2, 0x91, 0x94, 0x52, 0x4C, 0xC3, +0x82, 0xE7, 0x80, 0xC0, 0xB6, 0x0E, 0xC2, 0x6C, 0x93, 0xEC, 0xAB, 0x43, 0x95, +0xF6, 0xD8, 0x46, 0x86, 0x05, 0x8C, 0xB0, 0x75, 0x00, 0xCC, 0x85, 0xD7, 0x3D, +0x73, 0x7A, 0x48, 0xE4, 0xD1, 0x59, 0xAD, 0xB8, 0xC6, 0xD0, 0xDC, 0xA1, 0xAA, +0x02, 0x1D, 0xBF, 0xB5, 0x9F, 0x51, 0xC4, 0xA5, 0x10, 0x22, 0xCF, 0x01, 0xBA, +0x8F, 0x31, 0x7C, 0xAE, 0x96, 0xDA, 0xF0, 0x56, 0x47, 0xD4, 0xEB, 0x4E, 0xD9, +0x13, 0x8E, 0x49, 0x55, 0x16, 0xFF, 0x3B, 0xF4, 0xA4, 0xB2, 0x06, 0xA0, 0xA7, +0xFB, 0x1B, 0x6E, 0x3C, 0x33, 0xCD, 0x18, 0x5E, 0x6A, 0xD5, 0xA6, 0x21, 0xDE, +0xFE, 0x2A, 0x1C, 0xF3, 0x0A, 0x1A, 0x19, 0x27, 0x2D }; + +const byte Square::Log[256] = { +0x00, 0x00, 0x01, 0x86, 0x02, 0x0D, 0x87, 0x4C, 0x03, 0xD2, 0x0E, 0xAE, 0x88, +0x22, 0x4D, 0x93, 0x04, 0x1A, 0xD3, 0xCB, 0x0F, 0x98, 0xAF, 0xA8, 0x89, 0xF0, +0x23, 0x59, 0x4E, 0x35, 0x94, 0x09, 0x05, 0x8F, 0x1B, 0x6E, 0xD4, 0x39, 0xCC, +0xBB, 0x10, 0x68, 0x99, 0x77, 0xB0, 0xDF, 0xA9, 0x72, 0x8A, 0xFA, 0xF1, 0xA0, +0x24, 0x52, 0x5A, 0x60, 0x4F, 0x2F, 0x36, 0xDC, 0x95, 0x32, 0x0A, 0x1F, 0x06, +0xA5, 0x90, 0x49, 0x1C, 0x5D, 0x6F, 0xB8, 0xD5, 0xC1, 0x3A, 0xB5, 0xCD, 0x63, +0xBC, 0x3D, 0x11, 0x44, 0x69, 0x81, 0x9A, 0x27, 0x78, 0xC4, 0xB1, 0xE6, 0xE0, +0xEA, 0xAA, 0x55, 0x73, 0xD8, 0x8B, 0xF6, 0xFB, 0x16, 0xF2, 0xF4, 0xA1, 0x40, +0x25, 0x42, 0x53, 0xE4, 0x5B, 0xA3, 0x61, 0xBF, 0x50, 0xF8, 0x30, 0x2D, 0x37, +0x8D, 0xDD, 0x66, 0x96, 0x18, 0x33, 0xEE, 0x0B, 0xFD, 0x20, 0xD0, 0x07, 0x57, +0xA6, 0xC9, 0x91, 0xAC, 0x4A, 0x84, 0x1D, 0xDA, 0x5E, 0x9E, 0x70, 0x75, 0xB9, +0x6C, 0xD6, 0xE8, 0xC2, 0x7F, 0x3B, 0xB3, 0xB6, 0x47, 0xCE, 0xEC, 0x64, 0x2B, +0xBD, 0xE2, 0x3E, 0x14, 0x12, 0x29, 0x45, 0x7D, 0x6A, 0x9C, 0x82, 0xC7, 0x9B, +0xC6, 0x28, 0x7C, 0x79, 0x7A, 0xC5, 0x7B, 0xB2, 0x46, 0xE7, 0x7E, 0xE1, 0x13, +0xEB, 0x2A, 0xAB, 0x83, 0x56, 0xC8, 0x74, 0x6B, 0xD9, 0x9D, 0x8C, 0x65, 0xF7, +0x2C, 0xFC, 0xCF, 0x17, 0xED, 0xF3, 0x3F, 0xF5, 0x15, 0xA2, 0xBE, 0x41, 0xE3, +0x26, 0xC3, 0x43, 0x80, 0x54, 0xD7, 0xE5, 0xE9, 0x5C, 0xB7, 0xA4, 0x48, 0x62, +0x3C, 0xC0, 0xB4, 0x51, 0x5F, 0xF9, 0x9F, 0x31, 0x1E, 0x2E, 0xDB, 0x38, 0xBA, +0x8E, 0x6D, 0xDE, 0x71, 0x67, 0x76, 0x97, 0xA7, 0x19, 0xCA, 0x34, 0x08, 0xEF, +0x58, 0x0C, 0x4B, 0xFE, 0x85, 0x21, 0x92, 0xD1, 0xAD }; + +const byte Square::ALog[255] = { +0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0xF5, 0x1F, 0x3E, 0x7C, 0xF8, +0x05, 0x0A, 0x14, 0x28, 0x50, 0xA0, 0xB5, 0x9F, 0xCB, 0x63, 0xC6, 0x79, 0xF2, +0x11, 0x22, 0x44, 0x88, 0xE5, 0x3F, 0x7E, 0xFC, 0x0D, 0x1A, 0x34, 0x68, 0xD0, +0x55, 0xAA, 0xA1, 0xB7, 0x9B, 0xC3, 0x73, 0xE6, 0x39, 0x72, 0xE4, 0x3D, 0x7A, +0xF4, 0x1D, 0x3A, 0x74, 0xE8, 0x25, 0x4A, 0x94, 0xDD, 0x4F, 0x9E, 0xC9, 0x67, +0xCE, 0x69, 0xD2, 0x51, 0xA2, 0xB1, 0x97, 0xDB, 0x43, 0x86, 0xF9, 0x07, 0x0E, +0x1C, 0x38, 0x70, 0xE0, 0x35, 0x6A, 0xD4, 0x5D, 0xBA, 0x81, 0xF7, 0x1B, 0x36, +0x6C, 0xD8, 0x45, 0x8A, 0xE1, 0x37, 0x6E, 0xDC, 0x4D, 0x9A, 0xC1, 0x77, 0xEE, +0x29, 0x52, 0xA4, 0xBD, 0x8F, 0xEB, 0x23, 0x46, 0x8C, 0xED, 0x2F, 0x5E, 0xBC, +0x8D, 0xEF, 0x2B, 0x56, 0xAC, 0xAD, 0xAF, 0xAB, 0xA3, 0xB3, 0x93, 0xD3, 0x53, +0xA6, 0xB9, 0x87, 0xFB, 0x03, 0x06, 0x0C, 0x18, 0x30, 0x60, 0xC0, 0x75, 0xEA, +0x21, 0x42, 0x84, 0xFD, 0x0F, 0x1E, 0x3C, 0x78, 0xF0, 0x15, 0x2A, 0x54, 0xA8, +0xA5, 0xBF, 0x8B, 0xE3, 0x33, 0x66, 0xCC, 0x6D, 0xDA, 0x41, 0x82, 0xF1, 0x17, +0x2E, 0x5C, 0xB8, 0x85, 0xFF, 0x0B, 0x16, 0x2C, 0x58, 0xB0, 0x95, 0xDF, 0x4B, +0x96, 0xD9, 0x47, 0x8E, 0xE9, 0x27, 0x4E, 0x9C, 0xCD, 0x6F, 0xDE, 0x49, 0x92, +0xD1, 0x57, 0xAE, 0xA9, 0xA7, 0xBB, 0x83, 0xF3, 0x13, 0x26, 0x4C, 0x98, 0xC5, +0x7F, 0xFE, 0x09, 0x12, 0x24, 0x48, 0x90, 0xD5, 0x5F, 0xBE, 0x89, 0xE7, 0x3B, +0x76, 0xEC, 0x2D, 0x5A, 0xB4, 0x9D, 0xCF, 0x6B, 0xD6, 0x59, 0xB2, 0x91, 0xD7, +0x5B, 0xB6, 0x99, 0xC7, 0x7B, 0xF6, 0x19, 0x32, 0x64, 0xC8, 0x65, 0xCA, 0x61, +0xC2, 0x71, 0xE2, 0x31, 0x62, 0xC4, 0x7D, 0xFA }; + +const u32bit Square::TE0[256] = { +0x97B1B126, 0x69CECEA7, 0x73C3C3B0, 0xDF95954A, 0xB45A5AEE, 0xAFADAD02, +0x3BE7E7DC, 0x04020206, 0x9A4D4DD7, 0x884444CC, 0x03FBFBF8, 0xD7919146, +0x180C0C14, 0xFB87877C, 0xB7A1A116, 0xA05050F0, 0x63CBCBA8, 0xCE6767A9, +0xA85454FC, 0x4FDDDD92, 0x8C4646CA, 0xEB8F8F64, 0x37E1E1D6, 0x9C4E4ED2, +0x15F0F0E5, 0x0FFDFDF2, 0x0DFCFCF1, 0x23EBEBC8, 0x07F9F9FE, 0x7DC4C4B9, +0x341A1A2E, 0xDC6E6EB2, 0xBC5E5EE2, 0x1FF5F5EA, 0x6DCCCCA1, 0xEF8D8D62, +0x381C1C24, 0xAC5656FA, 0x864343C5, 0x09FEFEF7, 0x0E070709, 0xC26161A3, +0x05F8F8FD, 0xEA75759F, 0xB25959EB, 0x0BFFFFF4, 0x06030305, 0x44222266, +0xE18A8A6B, 0x57D1D186, 0x26131335, 0x29EEEEC7, 0xE588886D, 0x00000000, +0x1C0E0E12, 0x6834345C, 0x2A15153F, 0xF5808075, 0xDD949449, 0x33E3E3D0, +0x2FEDEDC2, 0x9FB5B52A, 0xA65353F5, 0x46232365, 0x964B4BDD, 0x8E4747C9, +0x2E171739, 0xBBA7A71C, 0xD5909045, 0x6A35355F, 0xA3ABAB08, 0x45D8D89D, +0x85B8B83D, 0x4BDFDF94, 0x9E4F4FD1, 0xAE5757F9, 0xC19A9A5B, 0xD1929243, +0x43DBDB98, 0x361B1B2D, 0x783C3C44, 0x65C8C8AD, 0xC799995E, 0x0804040C, +0xE98E8E67, 0x35E0E0D5, 0x5BD7D78C, 0xFA7D7D87, 0xFF85857A, 0x83BBBB38, +0x804040C0, 0x582C2C74, 0x743A3A4E, 0x8A4545CF, 0x17F1F1E6, 0x844242C6, +0xCA6565AF, 0x40202060, 0x824141C3, 0x30181828, 0xE4727296, 0x4A25256F, +0xD3939340, 0xE0707090, 0x6C36365A, 0x0A05050F, 0x11F2F2E3, 0x160B0B1D, +0xB3A3A310, 0xF279798B, 0x2DECECC1, 0x10080818, 0x4E272769, 0x62313153, +0x64323256, 0x99B6B62F, 0xF87C7C84, 0x95B0B025, 0x140A0A1E, 0xE6737395, +0xB65B5BED, 0xF67B7B8D, 0x9BB7B72C, 0xF7818176, 0x51D2D283, 0x1A0D0D17, +0xD46A6ABE, 0x4C26266A, 0xC99E9E57, 0xB05858E8, 0xCD9C9C51, 0xF3838370, +0xE874749C, 0x93B3B320, 0xADACAC01, 0x60303050, 0xF47A7A8E, 0xD26969BB, +0xEE777799, 0x1E0F0F11, 0xA9AEAE07, 0x42212163, 0x49DEDE97, 0x55D0D085, +0x5C2E2E72, 0xDB97974C, 0x20101030, 0xBDA4A419, 0xC598985D, 0xA5A8A80D, +0x5DD4D489, 0xD06868B8, 0x5A2D2D77, 0xC46262A6, 0x5229297B, 0xDA6D6DB7, +0x2C16163A, 0x924949DB, 0xEC76769A, 0x7BC7C7BC, 0x25E8E8CD, 0x77C1C1B6, +0xD996964F, 0x6E373759, 0x3FE5E5DA, 0x61CACAAB, 0x1DF4F4E9, 0x27E9E9CE, +0xC66363A5, 0x24121236, 0x71C2C2B3, 0xB9A6A61F, 0x2814143C, 0x8DBCBC31, +0x53D3D380, 0x50282878, 0xABAFAF04, 0x5E2F2F71, 0x39E6E6DF, 0x4824246C, +0xA45252F6, 0x79C6C6BF, 0xB5A0A015, 0x1209091B, 0x8FBDBD32, 0xED8C8C61, +0x6BCFCFA4, 0xBA5D5DE7, 0x22111133, 0xBE5F5FE1, 0x02010103, 0x7FC5C5BA, +0xCB9F9F54, 0x7A3D3D47, 0xB1A2A213, 0xC39B9B58, 0x67C9C9AE, 0x763B3B4D, +0x89BEBE37, 0xA25151F3, 0x3219192B, 0x3E1F1F21, 0x7E3F3F41, 0xB85C5CE4, +0x91B2B223, 0x2BEFEFC4, 0x944A4ADE, 0x6FCDCDA2, 0x8BBFBF34, 0x81BABA3B, +0xDE6F6FB1, 0xC86464AC, 0x47D9D99E, 0x13F3F3E0, 0x7C3E3E42, 0x9DB4B429, +0xA1AAAA0B, 0x4DDCDC91, 0x5FD5D58A, 0x0C06060A, 0x75C0C0B5, 0xFC7E7E82, +0x19F6F6EF, 0xCC6666AA, 0xD86C6CB4, 0xFD848479, 0xE2717193, 0x70383848, +0x87B9B93E, 0x3A1D1D27, 0xFE7F7F81, 0xCF9D9D52, 0x904848D8, 0xE38B8B68, +0x542A2A7E, 0x41DADA9B, 0xBFA5A51A, 0x66333355, 0xF1828273, 0x7239394B, +0x59D6D68F, 0xF0787888, 0xF986867F, 0x01FAFAFB, 0x3DE4E4D9, 0x562B2B7D, +0xA7A9A90E, 0x3C1E1E22, 0xE789896E, 0xC06060A0, 0xD66B6BBD, 0x21EAEACB, +0xAA5555FF, 0x984C4CD4, 0x1BF7F7EC, 0x31E2E2D3 }; + +const u32bit Square::TE1[256] = { +0x2697B1B1, 0xA769CECE, 0xB073C3C3, 0x4ADF9595, 0xEEB45A5A, 0x02AFADAD, +0xDC3BE7E7, 0x06040202, 0xD79A4D4D, 0xCC884444, 0xF803FBFB, 0x46D79191, +0x14180C0C, 0x7CFB8787, 0x16B7A1A1, 0xF0A05050, 0xA863CBCB, 0xA9CE6767, +0xFCA85454, 0x924FDDDD, 0xCA8C4646, 0x64EB8F8F, 0xD637E1E1, 0xD29C4E4E, +0xE515F0F0, 0xF20FFDFD, 0xF10DFCFC, 0xC823EBEB, 0xFE07F9F9, 0xB97DC4C4, +0x2E341A1A, 0xB2DC6E6E, 0xE2BC5E5E, 0xEA1FF5F5, 0xA16DCCCC, 0x62EF8D8D, +0x24381C1C, 0xFAAC5656, 0xC5864343, 0xF709FEFE, 0x090E0707, 0xA3C26161, +0xFD05F8F8, 0x9FEA7575, 0xEBB25959, 0xF40BFFFF, 0x05060303, 0x66442222, +0x6BE18A8A, 0x8657D1D1, 0x35261313, 0xC729EEEE, 0x6DE58888, 0x00000000, +0x121C0E0E, 0x5C683434, 0x3F2A1515, 0x75F58080, 0x49DD9494, 0xD033E3E3, +0xC22FEDED, 0x2A9FB5B5, 0xF5A65353, 0x65462323, 0xDD964B4B, 0xC98E4747, +0x392E1717, 0x1CBBA7A7, 0x45D59090, 0x5F6A3535, 0x08A3ABAB, 0x9D45D8D8, +0x3D85B8B8, 0x944BDFDF, 0xD19E4F4F, 0xF9AE5757, 0x5BC19A9A, 0x43D19292, +0x9843DBDB, 0x2D361B1B, 0x44783C3C, 0xAD65C8C8, 0x5EC79999, 0x0C080404, +0x67E98E8E, 0xD535E0E0, 0x8C5BD7D7, 0x87FA7D7D, 0x7AFF8585, 0x3883BBBB, +0xC0804040, 0x74582C2C, 0x4E743A3A, 0xCF8A4545, 0xE617F1F1, 0xC6844242, +0xAFCA6565, 0x60402020, 0xC3824141, 0x28301818, 0x96E47272, 0x6F4A2525, +0x40D39393, 0x90E07070, 0x5A6C3636, 0x0F0A0505, 0xE311F2F2, 0x1D160B0B, +0x10B3A3A3, 0x8BF27979, 0xC12DECEC, 0x18100808, 0x694E2727, 0x53623131, +0x56643232, 0x2F99B6B6, 0x84F87C7C, 0x2595B0B0, 0x1E140A0A, 0x95E67373, +0xEDB65B5B, 0x8DF67B7B, 0x2C9BB7B7, 0x76F78181, 0x8351D2D2, 0x171A0D0D, +0xBED46A6A, 0x6A4C2626, 0x57C99E9E, 0xE8B05858, 0x51CD9C9C, 0x70F38383, +0x9CE87474, 0x2093B3B3, 0x01ADACAC, 0x50603030, 0x8EF47A7A, 0xBBD26969, +0x99EE7777, 0x111E0F0F, 0x07A9AEAE, 0x63422121, 0x9749DEDE, 0x8555D0D0, +0x725C2E2E, 0x4CDB9797, 0x30201010, 0x19BDA4A4, 0x5DC59898, 0x0DA5A8A8, +0x895DD4D4, 0xB8D06868, 0x775A2D2D, 0xA6C46262, 0x7B522929, 0xB7DA6D6D, +0x3A2C1616, 0xDB924949, 0x9AEC7676, 0xBC7BC7C7, 0xCD25E8E8, 0xB677C1C1, +0x4FD99696, 0x596E3737, 0xDA3FE5E5, 0xAB61CACA, 0xE91DF4F4, 0xCE27E9E9, +0xA5C66363, 0x36241212, 0xB371C2C2, 0x1FB9A6A6, 0x3C281414, 0x318DBCBC, +0x8053D3D3, 0x78502828, 0x04ABAFAF, 0x715E2F2F, 0xDF39E6E6, 0x6C482424, +0xF6A45252, 0xBF79C6C6, 0x15B5A0A0, 0x1B120909, 0x328FBDBD, 0x61ED8C8C, +0xA46BCFCF, 0xE7BA5D5D, 0x33221111, 0xE1BE5F5F, 0x03020101, 0xBA7FC5C5, +0x54CB9F9F, 0x477A3D3D, 0x13B1A2A2, 0x58C39B9B, 0xAE67C9C9, 0x4D763B3B, +0x3789BEBE, 0xF3A25151, 0x2B321919, 0x213E1F1F, 0x417E3F3F, 0xE4B85C5C, +0x2391B2B2, 0xC42BEFEF, 0xDE944A4A, 0xA26FCDCD, 0x348BBFBF, 0x3B81BABA, +0xB1DE6F6F, 0xACC86464, 0x9E47D9D9, 0xE013F3F3, 0x427C3E3E, 0x299DB4B4, +0x0BA1AAAA, 0x914DDCDC, 0x8A5FD5D5, 0x0A0C0606, 0xB575C0C0, 0x82FC7E7E, +0xEF19F6F6, 0xAACC6666, 0xB4D86C6C, 0x79FD8484, 0x93E27171, 0x48703838, +0x3E87B9B9, 0x273A1D1D, 0x81FE7F7F, 0x52CF9D9D, 0xD8904848, 0x68E38B8B, +0x7E542A2A, 0x9B41DADA, 0x1ABFA5A5, 0x55663333, 0x73F18282, 0x4B723939, +0x8F59D6D6, 0x88F07878, 0x7FF98686, 0xFB01FAFA, 0xD93DE4E4, 0x7D562B2B, +0x0EA7A9A9, 0x223C1E1E, 0x6EE78989, 0xA0C06060, 0xBDD66B6B, 0xCB21EAEA, +0xFFAA5555, 0xD4984C4C, 0xEC1BF7F7, 0xD331E2E2 }; + +const u32bit Square::TE2[256] = { +0xB12697B1, 0xCEA769CE, 0xC3B073C3, 0x954ADF95, 0x5AEEB45A, 0xAD02AFAD, +0xE7DC3BE7, 0x02060402, 0x4DD79A4D, 0x44CC8844, 0xFBF803FB, 0x9146D791, +0x0C14180C, 0x877CFB87, 0xA116B7A1, 0x50F0A050, 0xCBA863CB, 0x67A9CE67, +0x54FCA854, 0xDD924FDD, 0x46CA8C46, 0x8F64EB8F, 0xE1D637E1, 0x4ED29C4E, +0xF0E515F0, 0xFDF20FFD, 0xFCF10DFC, 0xEBC823EB, 0xF9FE07F9, 0xC4B97DC4, +0x1A2E341A, 0x6EB2DC6E, 0x5EE2BC5E, 0xF5EA1FF5, 0xCCA16DCC, 0x8D62EF8D, +0x1C24381C, 0x56FAAC56, 0x43C58643, 0xFEF709FE, 0x07090E07, 0x61A3C261, +0xF8FD05F8, 0x759FEA75, 0x59EBB259, 0xFFF40BFF, 0x03050603, 0x22664422, +0x8A6BE18A, 0xD18657D1, 0x13352613, 0xEEC729EE, 0x886DE588, 0x00000000, +0x0E121C0E, 0x345C6834, 0x153F2A15, 0x8075F580, 0x9449DD94, 0xE3D033E3, +0xEDC22FED, 0xB52A9FB5, 0x53F5A653, 0x23654623, 0x4BDD964B, 0x47C98E47, +0x17392E17, 0xA71CBBA7, 0x9045D590, 0x355F6A35, 0xAB08A3AB, 0xD89D45D8, +0xB83D85B8, 0xDF944BDF, 0x4FD19E4F, 0x57F9AE57, 0x9A5BC19A, 0x9243D192, +0xDB9843DB, 0x1B2D361B, 0x3C44783C, 0xC8AD65C8, 0x995EC799, 0x040C0804, +0x8E67E98E, 0xE0D535E0, 0xD78C5BD7, 0x7D87FA7D, 0x857AFF85, 0xBB3883BB, +0x40C08040, 0x2C74582C, 0x3A4E743A, 0x45CF8A45, 0xF1E617F1, 0x42C68442, +0x65AFCA65, 0x20604020, 0x41C38241, 0x18283018, 0x7296E472, 0x256F4A25, +0x9340D393, 0x7090E070, 0x365A6C36, 0x050F0A05, 0xF2E311F2, 0x0B1D160B, +0xA310B3A3, 0x798BF279, 0xECC12DEC, 0x08181008, 0x27694E27, 0x31536231, +0x32566432, 0xB62F99B6, 0x7C84F87C, 0xB02595B0, 0x0A1E140A, 0x7395E673, +0x5BEDB65B, 0x7B8DF67B, 0xB72C9BB7, 0x8176F781, 0xD28351D2, 0x0D171A0D, +0x6ABED46A, 0x266A4C26, 0x9E57C99E, 0x58E8B058, 0x9C51CD9C, 0x8370F383, +0x749CE874, 0xB32093B3, 0xAC01ADAC, 0x30506030, 0x7A8EF47A, 0x69BBD269, +0x7799EE77, 0x0F111E0F, 0xAE07A9AE, 0x21634221, 0xDE9749DE, 0xD08555D0, +0x2E725C2E, 0x974CDB97, 0x10302010, 0xA419BDA4, 0x985DC598, 0xA80DA5A8, +0xD4895DD4, 0x68B8D068, 0x2D775A2D, 0x62A6C462, 0x297B5229, 0x6DB7DA6D, +0x163A2C16, 0x49DB9249, 0x769AEC76, 0xC7BC7BC7, 0xE8CD25E8, 0xC1B677C1, +0x964FD996, 0x37596E37, 0xE5DA3FE5, 0xCAAB61CA, 0xF4E91DF4, 0xE9CE27E9, +0x63A5C663, 0x12362412, 0xC2B371C2, 0xA61FB9A6, 0x143C2814, 0xBC318DBC, +0xD38053D3, 0x28785028, 0xAF04ABAF, 0x2F715E2F, 0xE6DF39E6, 0x246C4824, +0x52F6A452, 0xC6BF79C6, 0xA015B5A0, 0x091B1209, 0xBD328FBD, 0x8C61ED8C, +0xCFA46BCF, 0x5DE7BA5D, 0x11332211, 0x5FE1BE5F, 0x01030201, 0xC5BA7FC5, +0x9F54CB9F, 0x3D477A3D, 0xA213B1A2, 0x9B58C39B, 0xC9AE67C9, 0x3B4D763B, +0xBE3789BE, 0x51F3A251, 0x192B3219, 0x1F213E1F, 0x3F417E3F, 0x5CE4B85C, +0xB22391B2, 0xEFC42BEF, 0x4ADE944A, 0xCDA26FCD, 0xBF348BBF, 0xBA3B81BA, +0x6FB1DE6F, 0x64ACC864, 0xD99E47D9, 0xF3E013F3, 0x3E427C3E, 0xB4299DB4, +0xAA0BA1AA, 0xDC914DDC, 0xD58A5FD5, 0x060A0C06, 0xC0B575C0, 0x7E82FC7E, +0xF6EF19F6, 0x66AACC66, 0x6CB4D86C, 0x8479FD84, 0x7193E271, 0x38487038, +0xB93E87B9, 0x1D273A1D, 0x7F81FE7F, 0x9D52CF9D, 0x48D89048, 0x8B68E38B, +0x2A7E542A, 0xDA9B41DA, 0xA51ABFA5, 0x33556633, 0x8273F182, 0x394B7239, +0xD68F59D6, 0x7888F078, 0x867FF986, 0xFAFB01FA, 0xE4D93DE4, 0x2B7D562B, +0xA90EA7A9, 0x1E223C1E, 0x896EE789, 0x60A0C060, 0x6BBDD66B, 0xEACB21EA, +0x55FFAA55, 0x4CD4984C, 0xF7EC1BF7, 0xE2D331E2 }; + +const u32bit Square::TE3[256] = { +0xB1B12697, 0xCECEA769, 0xC3C3B073, 0x95954ADF, 0x5A5AEEB4, 0xADAD02AF, +0xE7E7DC3B, 0x02020604, 0x4D4DD79A, 0x4444CC88, 0xFBFBF803, 0x919146D7, +0x0C0C1418, 0x87877CFB, 0xA1A116B7, 0x5050F0A0, 0xCBCBA863, 0x6767A9CE, +0x5454FCA8, 0xDDDD924F, 0x4646CA8C, 0x8F8F64EB, 0xE1E1D637, 0x4E4ED29C, +0xF0F0E515, 0xFDFDF20F, 0xFCFCF10D, 0xEBEBC823, 0xF9F9FE07, 0xC4C4B97D, +0x1A1A2E34, 0x6E6EB2DC, 0x5E5EE2BC, 0xF5F5EA1F, 0xCCCCA16D, 0x8D8D62EF, +0x1C1C2438, 0x5656FAAC, 0x4343C586, 0xFEFEF709, 0x0707090E, 0x6161A3C2, +0xF8F8FD05, 0x75759FEA, 0x5959EBB2, 0xFFFFF40B, 0x03030506, 0x22226644, +0x8A8A6BE1, 0xD1D18657, 0x13133526, 0xEEEEC729, 0x88886DE5, 0x00000000, +0x0E0E121C, 0x34345C68, 0x15153F2A, 0x808075F5, 0x949449DD, 0xE3E3D033, +0xEDEDC22F, 0xB5B52A9F, 0x5353F5A6, 0x23236546, 0x4B4BDD96, 0x4747C98E, +0x1717392E, 0xA7A71CBB, 0x909045D5, 0x35355F6A, 0xABAB08A3, 0xD8D89D45, +0xB8B83D85, 0xDFDF944B, 0x4F4FD19E, 0x5757F9AE, 0x9A9A5BC1, 0x929243D1, +0xDBDB9843, 0x1B1B2D36, 0x3C3C4478, 0xC8C8AD65, 0x99995EC7, 0x04040C08, +0x8E8E67E9, 0xE0E0D535, 0xD7D78C5B, 0x7D7D87FA, 0x85857AFF, 0xBBBB3883, +0x4040C080, 0x2C2C7458, 0x3A3A4E74, 0x4545CF8A, 0xF1F1E617, 0x4242C684, +0x6565AFCA, 0x20206040, 0x4141C382, 0x18182830, 0x727296E4, 0x25256F4A, +0x939340D3, 0x707090E0, 0x36365A6C, 0x05050F0A, 0xF2F2E311, 0x0B0B1D16, +0xA3A310B3, 0x79798BF2, 0xECECC12D, 0x08081810, 0x2727694E, 0x31315362, +0x32325664, 0xB6B62F99, 0x7C7C84F8, 0xB0B02595, 0x0A0A1E14, 0x737395E6, +0x5B5BEDB6, 0x7B7B8DF6, 0xB7B72C9B, 0x818176F7, 0xD2D28351, 0x0D0D171A, +0x6A6ABED4, 0x26266A4C, 0x9E9E57C9, 0x5858E8B0, 0x9C9C51CD, 0x838370F3, +0x74749CE8, 0xB3B32093, 0xACAC01AD, 0x30305060, 0x7A7A8EF4, 0x6969BBD2, +0x777799EE, 0x0F0F111E, 0xAEAE07A9, 0x21216342, 0xDEDE9749, 0xD0D08555, +0x2E2E725C, 0x97974CDB, 0x10103020, 0xA4A419BD, 0x98985DC5, 0xA8A80DA5, +0xD4D4895D, 0x6868B8D0, 0x2D2D775A, 0x6262A6C4, 0x29297B52, 0x6D6DB7DA, +0x16163A2C, 0x4949DB92, 0x76769AEC, 0xC7C7BC7B, 0xE8E8CD25, 0xC1C1B677, +0x96964FD9, 0x3737596E, 0xE5E5DA3F, 0xCACAAB61, 0xF4F4E91D, 0xE9E9CE27, +0x6363A5C6, 0x12123624, 0xC2C2B371, 0xA6A61FB9, 0x14143C28, 0xBCBC318D, +0xD3D38053, 0x28287850, 0xAFAF04AB, 0x2F2F715E, 0xE6E6DF39, 0x24246C48, +0x5252F6A4, 0xC6C6BF79, 0xA0A015B5, 0x09091B12, 0xBDBD328F, 0x8C8C61ED, +0xCFCFA46B, 0x5D5DE7BA, 0x11113322, 0x5F5FE1BE, 0x01010302, 0xC5C5BA7F, +0x9F9F54CB, 0x3D3D477A, 0xA2A213B1, 0x9B9B58C3, 0xC9C9AE67, 0x3B3B4D76, +0xBEBE3789, 0x5151F3A2, 0x19192B32, 0x1F1F213E, 0x3F3F417E, 0x5C5CE4B8, +0xB2B22391, 0xEFEFC42B, 0x4A4ADE94, 0xCDCDA26F, 0xBFBF348B, 0xBABA3B81, +0x6F6FB1DE, 0x6464ACC8, 0xD9D99E47, 0xF3F3E013, 0x3E3E427C, 0xB4B4299D, +0xAAAA0BA1, 0xDCDC914D, 0xD5D58A5F, 0x06060A0C, 0xC0C0B575, 0x7E7E82FC, +0xF6F6EF19, 0x6666AACC, 0x6C6CB4D8, 0x848479FD, 0x717193E2, 0x38384870, +0xB9B93E87, 0x1D1D273A, 0x7F7F81FE, 0x9D9D52CF, 0x4848D890, 0x8B8B68E3, +0x2A2A7E54, 0xDADA9B41, 0xA5A51ABF, 0x33335566, 0x828273F1, 0x39394B72, +0xD6D68F59, 0x787888F0, 0x86867FF9, 0xFAFAFB01, 0xE4E4D93D, 0x2B2B7D56, +0xA9A90EA7, 0x1E1E223C, 0x89896EE7, 0x6060A0C0, 0x6B6BBDD6, 0xEAEACB21, +0x5555FFAA, 0x4C4CD498, 0xF7F7EC1B, 0xE2E2D331 }; + +const u32bit Square::TD0[256] = { +0xE368BC02, 0x5585620C, 0x2A3F2331, 0x61AB13F7, 0x98D46D72, 0x21CB9A19, +0x3C22A461, 0x459D3DCD, 0x05FDB423, 0x2BC4075F, 0x9B2C01C0, 0x3DD9800F, +0x486C5C74, 0xF97F7E85, 0xF173AB1F, 0xB6EDDE0E, 0x283C6BED, 0x4997781A, +0x9F2A918D, 0xC9579F33, 0xA907A8AA, 0xA50DED7D, 0x7C422D8F, 0x764DB0C9, +0x4D91E857, 0xCEA963CC, 0xB4EE96D2, 0x3028E1B6, 0x0DF161B9, 0xBD196726, +0x419BAD80, 0xC0A06EC7, 0x5183F241, 0x92DBF034, 0x6FA21EFC, 0x8F32CE4C, +0x13E03373, 0x69A7C66D, 0xE56D6493, 0xBF1A2FFA, 0xBB1CBFB7, 0x587403B5, +0xE76E2C4F, 0x5D89B796, 0xE89C052A, 0x446619A3, 0x342E71FB, 0x0FF22965, +0xFE81827A, 0xB11322F1, 0xA30835EC, 0xCD510F7E, 0xFF7AA614, 0x5C7293F8, +0x2FC29712, 0xF370E3C3, 0x992F491C, 0xD1431568, 0xC2A3261B, 0x88CC32B3, +0x8ACF7A6F, 0xB0E8069F, 0x7A47F51E, 0xD2BB79DA, 0xE6950821, 0x4398E55C, +0xD0B83106, 0x11E37BAF, 0x7E416553, 0xCCAA2B10, 0xD8B4E49C, 0x6456A7D4, +0xFB7C3659, 0x724B2084, 0xEA9F4DF6, 0x6A5FAADF, 0x2DC1DFCE, 0x70486858, +0xCAAFF381, 0x0605D891, 0x5A774B69, 0x94DE28A5, 0x39DF1042, 0x813BC347, +0xFC82CAA6, 0x23C8D2C5, 0x03F86CB2, 0x080CD59A, 0xDAB7AC40, 0x7DB909E1, +0x3824342C, 0xCF5247A2, 0xDCB274D1, 0x63A85B2B, 0x35D55595, 0x479E7511, +0x15E5EBE2, 0x4B9430C6, 0x4A6F14A8, 0x91239C86, 0x4C6ACC39, 0x5F8AFF4A, +0x0406904D, 0xEE99DDBB, 0x1E1152CA, 0xAAFFC418, 0xEB646998, 0x07FEFCFF, +0x8B345E01, 0x567D0EBE, 0xBAE79BD9, 0x4263C132, 0x75B5DC7B, 0x97264417, +0x67AECB66, 0x95250CCB, 0xEC9A9567, 0x57862AD0, 0x60503799, 0xB8E4D305, +0x65AD83BA, 0x19EFAE35, 0xA4F6C913, 0xC15B4AA9, 0x873E1BD6, 0xA0F0595E, +0x18148A5B, 0xAF02703B, 0xAB04E076, 0xDD4950BF, 0xDF4A1863, 0xC6A5B656, +0x853D530A, 0xFA871237, 0x77B694A7, 0x4665517F, 0xED61B109, 0x1BECE6E9, +0xD5458525, 0xF5753B52, 0x7FBA413D, 0x27CE4288, 0xB2EB4E43, 0xD6BDE997, +0x527B9EF3, 0x62537F45, 0x2C3AFBA0, 0x7BBCD170, 0xB91FF76B, 0x121B171D, +0xFD79EEC8, 0x3A277CF0, 0x0C0A45D7, 0x96DD6079, 0x2233F6AB, 0xACFA1C89, +0xC8ACBB5D, 0xA10B7D30, 0xD4BEA14B, 0xBEE10B94, 0x25CD0A54, 0x547E4662, +0xA2F31182, 0x17E6A33E, 0x263566E6, 0xC3580275, 0x83388B9B, 0x7844BDC2, +0x020348DC, 0x4F92A08B, 0x2E39B37C, 0x4E6984E5, 0xF0888F71, 0x362D3927, +0x9CD2FD3F, 0x01FB246E, 0x893716DD, 0x00000000, 0xF68D57E0, 0xE293986C, +0x744EF815, 0x9320D45A, 0xAD0138E7, 0xD3405DB4, 0x1A17C287, 0xB3106A2D, +0x5078D62F, 0xF48E1F3C, 0xA70EA5A1, 0x71B34C36, 0x9AD725AE, 0x5E71DB24, +0x161D8750, 0xEF62F9D5, 0x8D318690, 0x1C121A16, 0xA6F581CF, 0x5B8C6F07, +0x37D61D49, 0x6E593A92, 0x84C67764, 0x86C53FB8, 0xD746CDF9, 0xE090D0B0, +0x29C74F83, 0xE49640FD, 0x0E090D0B, 0x6DA15620, 0x8EC9EA22, 0xDB4C882E, +0xF776738E, 0xB515B2BC, 0x10185FC1, 0x322BA96A, 0x6BA48EB1, 0xAEF95455, +0x406089EE, 0x6655EF08, 0xE9672144, 0x3E21ECBD, 0x2030BE77, 0xF28BC7AD, +0x80C0E729, 0x141ECF8C, 0xBCE24348, 0xC4A6FE8A, 0x31D3C5D8, 0xB716FA60, +0x5380BA9D, 0xD94FC0F2, 0x1DE93E78, 0x24362E3A, 0xE16BF4DE, 0xCB54D7EF, +0x09F7F1F4, 0x82C3AFF5, 0x0BF4B928, 0x9D29D951, 0xC75E9238, 0xF8845AEB, +0x90D8B8E8, 0xDEB13C0D, 0x33D08D04, 0x685CE203, 0xC55DDAE4, 0x3BDC589E, +0x0A0F9D46, 0x3FDAC8D3, 0x598F27DB, 0xA8FC8CC4, 0x79BF99AC, 0x6C5A724E, +0x8CCAA2FE, 0x9ED1B5E3, 0x1FEA76A4, 0x73B004EA }; + +const u32bit Square::TD1[256] = { +0x02E368BC, 0x0C558562, 0x312A3F23, 0xF761AB13, 0x7298D46D, 0x1921CB9A, +0x613C22A4, 0xCD459D3D, 0x2305FDB4, 0x5F2BC407, 0xC09B2C01, 0x0F3DD980, +0x74486C5C, 0x85F97F7E, 0x1FF173AB, 0x0EB6EDDE, 0xED283C6B, 0x1A499778, +0x8D9F2A91, 0x33C9579F, 0xAAA907A8, 0x7DA50DED, 0x8F7C422D, 0xC9764DB0, +0x574D91E8, 0xCCCEA963, 0xD2B4EE96, 0xB63028E1, 0xB90DF161, 0x26BD1967, +0x80419BAD, 0xC7C0A06E, 0x415183F2, 0x3492DBF0, 0xFC6FA21E, 0x4C8F32CE, +0x7313E033, 0x6D69A7C6, 0x93E56D64, 0xFABF1A2F, 0xB7BB1CBF, 0xB5587403, +0x4FE76E2C, 0x965D89B7, 0x2AE89C05, 0xA3446619, 0xFB342E71, 0x650FF229, +0x7AFE8182, 0xF1B11322, 0xECA30835, 0x7ECD510F, 0x14FF7AA6, 0xF85C7293, +0x122FC297, 0xC3F370E3, 0x1C992F49, 0x68D14315, 0x1BC2A326, 0xB388CC32, +0x6F8ACF7A, 0x9FB0E806, 0x1E7A47F5, 0xDAD2BB79, 0x21E69508, 0x5C4398E5, +0x06D0B831, 0xAF11E37B, 0x537E4165, 0x10CCAA2B, 0x9CD8B4E4, 0xD46456A7, +0x59FB7C36, 0x84724B20, 0xF6EA9F4D, 0xDF6A5FAA, 0xCE2DC1DF, 0x58704868, +0x81CAAFF3, 0x910605D8, 0x695A774B, 0xA594DE28, 0x4239DF10, 0x47813BC3, +0xA6FC82CA, 0xC523C8D2, 0xB203F86C, 0x9A080CD5, 0x40DAB7AC, 0xE17DB909, +0x2C382434, 0xA2CF5247, 0xD1DCB274, 0x2B63A85B, 0x9535D555, 0x11479E75, +0xE215E5EB, 0xC64B9430, 0xA84A6F14, 0x8691239C, 0x394C6ACC, 0x4A5F8AFF, +0x4D040690, 0xBBEE99DD, 0xCA1E1152, 0x18AAFFC4, 0x98EB6469, 0xFF07FEFC, +0x018B345E, 0xBE567D0E, 0xD9BAE79B, 0x324263C1, 0x7B75B5DC, 0x17972644, +0x6667AECB, 0xCB95250C, 0x67EC9A95, 0xD057862A, 0x99605037, 0x05B8E4D3, +0xBA65AD83, 0x3519EFAE, 0x13A4F6C9, 0xA9C15B4A, 0xD6873E1B, 0x5EA0F059, +0x5B18148A, 0x3BAF0270, 0x76AB04E0, 0xBFDD4950, 0x63DF4A18, 0x56C6A5B6, +0x0A853D53, 0x37FA8712, 0xA777B694, 0x7F466551, 0x09ED61B1, 0xE91BECE6, +0x25D54585, 0x52F5753B, 0x3D7FBA41, 0x8827CE42, 0x43B2EB4E, 0x97D6BDE9, +0xF3527B9E, 0x4562537F, 0xA02C3AFB, 0x707BBCD1, 0x6BB91FF7, 0x1D121B17, +0xC8FD79EE, 0xF03A277C, 0xD70C0A45, 0x7996DD60, 0xAB2233F6, 0x89ACFA1C, +0x5DC8ACBB, 0x30A10B7D, 0x4BD4BEA1, 0x94BEE10B, 0x5425CD0A, 0x62547E46, +0x82A2F311, 0x3E17E6A3, 0xE6263566, 0x75C35802, 0x9B83388B, 0xC27844BD, +0xDC020348, 0x8B4F92A0, 0x7C2E39B3, 0xE54E6984, 0x71F0888F, 0x27362D39, +0x3F9CD2FD, 0x6E01FB24, 0xDD893716, 0x00000000, 0xE0F68D57, 0x6CE29398, +0x15744EF8, 0x5A9320D4, 0xE7AD0138, 0xB4D3405D, 0x871A17C2, 0x2DB3106A, +0x2F5078D6, 0x3CF48E1F, 0xA1A70EA5, 0x3671B34C, 0xAE9AD725, 0x245E71DB, +0x50161D87, 0xD5EF62F9, 0x908D3186, 0x161C121A, 0xCFA6F581, 0x075B8C6F, +0x4937D61D, 0x926E593A, 0x6484C677, 0xB886C53F, 0xF9D746CD, 0xB0E090D0, +0x8329C74F, 0xFDE49640, 0x0B0E090D, 0x206DA156, 0x228EC9EA, 0x2EDB4C88, +0x8EF77673, 0xBCB515B2, 0xC110185F, 0x6A322BA9, 0xB16BA48E, 0x55AEF954, +0xEE406089, 0x086655EF, 0x44E96721, 0xBD3E21EC, 0x772030BE, 0xADF28BC7, +0x2980C0E7, 0x8C141ECF, 0x48BCE243, 0x8AC4A6FE, 0xD831D3C5, 0x60B716FA, +0x9D5380BA, 0xF2D94FC0, 0x781DE93E, 0x3A24362E, 0xDEE16BF4, 0xEFCB54D7, +0xF409F7F1, 0xF582C3AF, 0x280BF4B9, 0x519D29D9, 0x38C75E92, 0xEBF8845A, +0xE890D8B8, 0x0DDEB13C, 0x0433D08D, 0x03685CE2, 0xE4C55DDA, 0x9E3BDC58, +0x460A0F9D, 0xD33FDAC8, 0xDB598F27, 0xC4A8FC8C, 0xAC79BF99, 0x4E6C5A72, +0xFE8CCAA2, 0xE39ED1B5, 0xA41FEA76, 0xEA73B004 }; + +const u32bit Square::TD2[256] = { +0xBC02E368, 0x620C5585, 0x23312A3F, 0x13F761AB, 0x6D7298D4, 0x9A1921CB, +0xA4613C22, 0x3DCD459D, 0xB42305FD, 0x075F2BC4, 0x01C09B2C, 0x800F3DD9, +0x5C74486C, 0x7E85F97F, 0xAB1FF173, 0xDE0EB6ED, 0x6BED283C, 0x781A4997, +0x918D9F2A, 0x9F33C957, 0xA8AAA907, 0xED7DA50D, 0x2D8F7C42, 0xB0C9764D, +0xE8574D91, 0x63CCCEA9, 0x96D2B4EE, 0xE1B63028, 0x61B90DF1, 0x6726BD19, +0xAD80419B, 0x6EC7C0A0, 0xF2415183, 0xF03492DB, 0x1EFC6FA2, 0xCE4C8F32, +0x337313E0, 0xC66D69A7, 0x6493E56D, 0x2FFABF1A, 0xBFB7BB1C, 0x03B55874, +0x2C4FE76E, 0xB7965D89, 0x052AE89C, 0x19A34466, 0x71FB342E, 0x29650FF2, +0x827AFE81, 0x22F1B113, 0x35ECA308, 0x0F7ECD51, 0xA614FF7A, 0x93F85C72, +0x97122FC2, 0xE3C3F370, 0x491C992F, 0x1568D143, 0x261BC2A3, 0x32B388CC, +0x7A6F8ACF, 0x069FB0E8, 0xF51E7A47, 0x79DAD2BB, 0x0821E695, 0xE55C4398, +0x3106D0B8, 0x7BAF11E3, 0x65537E41, 0x2B10CCAA, 0xE49CD8B4, 0xA7D46456, +0x3659FB7C, 0x2084724B, 0x4DF6EA9F, 0xAADF6A5F, 0xDFCE2DC1, 0x68587048, +0xF381CAAF, 0xD8910605, 0x4B695A77, 0x28A594DE, 0x104239DF, 0xC347813B, +0xCAA6FC82, 0xD2C523C8, 0x6CB203F8, 0xD59A080C, 0xAC40DAB7, 0x09E17DB9, +0x342C3824, 0x47A2CF52, 0x74D1DCB2, 0x5B2B63A8, 0x559535D5, 0x7511479E, +0xEBE215E5, 0x30C64B94, 0x14A84A6F, 0x9C869123, 0xCC394C6A, 0xFF4A5F8A, +0x904D0406, 0xDDBBEE99, 0x52CA1E11, 0xC418AAFF, 0x6998EB64, 0xFCFF07FE, +0x5E018B34, 0x0EBE567D, 0x9BD9BAE7, 0xC1324263, 0xDC7B75B5, 0x44179726, +0xCB6667AE, 0x0CCB9525, 0x9567EC9A, 0x2AD05786, 0x37996050, 0xD305B8E4, +0x83BA65AD, 0xAE3519EF, 0xC913A4F6, 0x4AA9C15B, 0x1BD6873E, 0x595EA0F0, +0x8A5B1814, 0x703BAF02, 0xE076AB04, 0x50BFDD49, 0x1863DF4A, 0xB656C6A5, +0x530A853D, 0x1237FA87, 0x94A777B6, 0x517F4665, 0xB109ED61, 0xE6E91BEC, +0x8525D545, 0x3B52F575, 0x413D7FBA, 0x428827CE, 0x4E43B2EB, 0xE997D6BD, +0x9EF3527B, 0x7F456253, 0xFBA02C3A, 0xD1707BBC, 0xF76BB91F, 0x171D121B, +0xEEC8FD79, 0x7CF03A27, 0x45D70C0A, 0x607996DD, 0xF6AB2233, 0x1C89ACFA, +0xBB5DC8AC, 0x7D30A10B, 0xA14BD4BE, 0x0B94BEE1, 0x0A5425CD, 0x4662547E, +0x1182A2F3, 0xA33E17E6, 0x66E62635, 0x0275C358, 0x8B9B8338, 0xBDC27844, +0x48DC0203, 0xA08B4F92, 0xB37C2E39, 0x84E54E69, 0x8F71F088, 0x3927362D, +0xFD3F9CD2, 0x246E01FB, 0x16DD8937, 0x00000000, 0x57E0F68D, 0x986CE293, +0xF815744E, 0xD45A9320, 0x38E7AD01, 0x5DB4D340, 0xC2871A17, 0x6A2DB310, +0xD62F5078, 0x1F3CF48E, 0xA5A1A70E, 0x4C3671B3, 0x25AE9AD7, 0xDB245E71, +0x8750161D, 0xF9D5EF62, 0x86908D31, 0x1A161C12, 0x81CFA6F5, 0x6F075B8C, +0x1D4937D6, 0x3A926E59, 0x776484C6, 0x3FB886C5, 0xCDF9D746, 0xD0B0E090, +0x4F8329C7, 0x40FDE496, 0x0D0B0E09, 0x56206DA1, 0xEA228EC9, 0x882EDB4C, +0x738EF776, 0xB2BCB515, 0x5FC11018, 0xA96A322B, 0x8EB16BA4, 0x5455AEF9, +0x89EE4060, 0xEF086655, 0x2144E967, 0xECBD3E21, 0xBE772030, 0xC7ADF28B, +0xE72980C0, 0xCF8C141E, 0x4348BCE2, 0xFE8AC4A6, 0xC5D831D3, 0xFA60B716, +0xBA9D5380, 0xC0F2D94F, 0x3E781DE9, 0x2E3A2436, 0xF4DEE16B, 0xD7EFCB54, +0xF1F409F7, 0xAFF582C3, 0xB9280BF4, 0xD9519D29, 0x9238C75E, 0x5AEBF884, +0xB8E890D8, 0x3C0DDEB1, 0x8D0433D0, 0xE203685C, 0xDAE4C55D, 0x589E3BDC, +0x9D460A0F, 0xC8D33FDA, 0x27DB598F, 0x8CC4A8FC, 0x99AC79BF, 0x724E6C5A, +0xA2FE8CCA, 0xB5E39ED1, 0x76A41FEA, 0x04EA73B0 }; + +const u32bit Square::TD3[256] = { +0x68BC02E3, 0x85620C55, 0x3F23312A, 0xAB13F761, 0xD46D7298, 0xCB9A1921, +0x22A4613C, 0x9D3DCD45, 0xFDB42305, 0xC4075F2B, 0x2C01C09B, 0xD9800F3D, +0x6C5C7448, 0x7F7E85F9, 0x73AB1FF1, 0xEDDE0EB6, 0x3C6BED28, 0x97781A49, +0x2A918D9F, 0x579F33C9, 0x07A8AAA9, 0x0DED7DA5, 0x422D8F7C, 0x4DB0C976, +0x91E8574D, 0xA963CCCE, 0xEE96D2B4, 0x28E1B630, 0xF161B90D, 0x196726BD, +0x9BAD8041, 0xA06EC7C0, 0x83F24151, 0xDBF03492, 0xA21EFC6F, 0x32CE4C8F, +0xE0337313, 0xA7C66D69, 0x6D6493E5, 0x1A2FFABF, 0x1CBFB7BB, 0x7403B558, +0x6E2C4FE7, 0x89B7965D, 0x9C052AE8, 0x6619A344, 0x2E71FB34, 0xF229650F, +0x81827AFE, 0x1322F1B1, 0x0835ECA3, 0x510F7ECD, 0x7AA614FF, 0x7293F85C, +0xC297122F, 0x70E3C3F3, 0x2F491C99, 0x431568D1, 0xA3261BC2, 0xCC32B388, +0xCF7A6F8A, 0xE8069FB0, 0x47F51E7A, 0xBB79DAD2, 0x950821E6, 0x98E55C43, +0xB83106D0, 0xE37BAF11, 0x4165537E, 0xAA2B10CC, 0xB4E49CD8, 0x56A7D464, +0x7C3659FB, 0x4B208472, 0x9F4DF6EA, 0x5FAADF6A, 0xC1DFCE2D, 0x48685870, +0xAFF381CA, 0x05D89106, 0x774B695A, 0xDE28A594, 0xDF104239, 0x3BC34781, +0x82CAA6FC, 0xC8D2C523, 0xF86CB203, 0x0CD59A08, 0xB7AC40DA, 0xB909E17D, +0x24342C38, 0x5247A2CF, 0xB274D1DC, 0xA85B2B63, 0xD5559535, 0x9E751147, +0xE5EBE215, 0x9430C64B, 0x6F14A84A, 0x239C8691, 0x6ACC394C, 0x8AFF4A5F, +0x06904D04, 0x99DDBBEE, 0x1152CA1E, 0xFFC418AA, 0x646998EB, 0xFEFCFF07, +0x345E018B, 0x7D0EBE56, 0xE79BD9BA, 0x63C13242, 0xB5DC7B75, 0x26441797, +0xAECB6667, 0x250CCB95, 0x9A9567EC, 0x862AD057, 0x50379960, 0xE4D305B8, +0xAD83BA65, 0xEFAE3519, 0xF6C913A4, 0x5B4AA9C1, 0x3E1BD687, 0xF0595EA0, +0x148A5B18, 0x02703BAF, 0x04E076AB, 0x4950BFDD, 0x4A1863DF, 0xA5B656C6, +0x3D530A85, 0x871237FA, 0xB694A777, 0x65517F46, 0x61B109ED, 0xECE6E91B, +0x458525D5, 0x753B52F5, 0xBA413D7F, 0xCE428827, 0xEB4E43B2, 0xBDE997D6, +0x7B9EF352, 0x537F4562, 0x3AFBA02C, 0xBCD1707B, 0x1FF76BB9, 0x1B171D12, +0x79EEC8FD, 0x277CF03A, 0x0A45D70C, 0xDD607996, 0x33F6AB22, 0xFA1C89AC, +0xACBB5DC8, 0x0B7D30A1, 0xBEA14BD4, 0xE10B94BE, 0xCD0A5425, 0x7E466254, +0xF31182A2, 0xE6A33E17, 0x3566E626, 0x580275C3, 0x388B9B83, 0x44BDC278, +0x0348DC02, 0x92A08B4F, 0x39B37C2E, 0x6984E54E, 0x888F71F0, 0x2D392736, +0xD2FD3F9C, 0xFB246E01, 0x3716DD89, 0x00000000, 0x8D57E0F6, 0x93986CE2, +0x4EF81574, 0x20D45A93, 0x0138E7AD, 0x405DB4D3, 0x17C2871A, 0x106A2DB3, +0x78D62F50, 0x8E1F3CF4, 0x0EA5A1A7, 0xB34C3671, 0xD725AE9A, 0x71DB245E, +0x1D875016, 0x62F9D5EF, 0x3186908D, 0x121A161C, 0xF581CFA6, 0x8C6F075B, +0xD61D4937, 0x593A926E, 0xC6776484, 0xC53FB886, 0x46CDF9D7, 0x90D0B0E0, +0xC74F8329, 0x9640FDE4, 0x090D0B0E, 0xA156206D, 0xC9EA228E, 0x4C882EDB, +0x76738EF7, 0x15B2BCB5, 0x185FC110, 0x2BA96A32, 0xA48EB16B, 0xF95455AE, +0x6089EE40, 0x55EF0866, 0x672144E9, 0x21ECBD3E, 0x30BE7720, 0x8BC7ADF2, +0xC0E72980, 0x1ECF8C14, 0xE24348BC, 0xA6FE8AC4, 0xD3C5D831, 0x16FA60B7, +0x80BA9D53, 0x4FC0F2D9, 0xE93E781D, 0x362E3A24, 0x6BF4DEE1, 0x54D7EFCB, +0xF7F1F409, 0xC3AFF582, 0xF4B9280B, 0x29D9519D, 0x5E9238C7, 0x845AEBF8, +0xD8B8E890, 0xB13C0DDE, 0xD08D0433, 0x5CE20368, 0x5DDAE4C5, 0xDC589E3B, +0x0F9D460A, 0xDAC8D33F, 0x8F27DB59, 0xFC8CC4A8, 0xBF99AC79, 0x5A724E6C, +0xCAA2FE8C, 0xD1B5E39E, 0xEA76A41F, 0xB004EA73 }; + +} diff --git a/botan/src/block/square/square.cpp b/botan/src/block/square/square.cpp new file mode 100644 index 0000000..cb22654 --- /dev/null +++ b/botan/src/block/square/square.cpp @@ -0,0 +1,187 @@ +/* +* Square +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/square.h> +#include <botan/loadstor.h> +#include <botan/rotate.h> + +namespace Botan { + +/* +* Square Encryption +*/ +void Square::enc(const byte in[], byte out[]) const + { + u32bit T0, T1, T2, T3, B0, B1, B2, B3; + B0 = TE0[in[ 0] ^ ME[ 0]] ^ TE1[in[ 4] ^ ME[ 4]] ^ + TE2[in[ 8] ^ ME[ 8]] ^ TE3[in[12] ^ ME[12]] ^ EK[0]; + B1 = TE0[in[ 1] ^ ME[ 1]] ^ TE1[in[ 5] ^ ME[ 5]] ^ + TE2[in[ 9] ^ ME[ 9]] ^ TE3[in[13] ^ ME[13]] ^ EK[1]; + B2 = TE0[in[ 2] ^ ME[ 2]] ^ TE1[in[ 6] ^ ME[ 6]] ^ + TE2[in[10] ^ ME[10]] ^ TE3[in[14] ^ ME[14]] ^ EK[2]; + B3 = TE0[in[ 3] ^ ME[ 3]] ^ TE1[in[ 7] ^ ME[ 7]] ^ + TE2[in[11] ^ ME[11]] ^ TE3[in[15] ^ ME[15]] ^ EK[3]; + for(u32bit j = 1; j != 7; j += 2) + { + T0 = TE0[get_byte(0, B0)] ^ TE1[get_byte(0, B1)] ^ + TE2[get_byte(0, B2)] ^ TE3[get_byte(0, B3)] ^ EK[4*j+0]; + T1 = TE0[get_byte(1, B0)] ^ TE1[get_byte(1, B1)] ^ + TE2[get_byte(1, B2)] ^ TE3[get_byte(1, B3)] ^ EK[4*j+1]; + T2 = TE0[get_byte(2, B0)] ^ TE1[get_byte(2, B1)] ^ + TE2[get_byte(2, B2)] ^ TE3[get_byte(2, B3)] ^ EK[4*j+2]; + T3 = TE0[get_byte(3, B0)] ^ TE1[get_byte(3, B1)] ^ + TE2[get_byte(3, B2)] ^ TE3[get_byte(3, B3)] ^ EK[4*j+3]; + B0 = TE0[get_byte(0, T0)] ^ TE1[get_byte(0, T1)] ^ + TE2[get_byte(0, T2)] ^ TE3[get_byte(0, T3)] ^ EK[4*j+4]; + B1 = TE0[get_byte(1, T0)] ^ TE1[get_byte(1, T1)] ^ + TE2[get_byte(1, T2)] ^ TE3[get_byte(1, T3)] ^ EK[4*j+5]; + B2 = TE0[get_byte(2, T0)] ^ TE1[get_byte(2, T1)] ^ + TE2[get_byte(2, T2)] ^ TE3[get_byte(2, T3)] ^ EK[4*j+6]; + B3 = TE0[get_byte(3, T0)] ^ TE1[get_byte(3, T1)] ^ + TE2[get_byte(3, T2)] ^ TE3[get_byte(3, T3)] ^ EK[4*j+7]; + } + out[ 0] = SE[get_byte(0, B0)] ^ ME[16]; + out[ 1] = SE[get_byte(0, B1)] ^ ME[17]; + out[ 2] = SE[get_byte(0, B2)] ^ ME[18]; + out[ 3] = SE[get_byte(0, B3)] ^ ME[19]; + out[ 4] = SE[get_byte(1, B0)] ^ ME[20]; + out[ 5] = SE[get_byte(1, B1)] ^ ME[21]; + out[ 6] = SE[get_byte(1, B2)] ^ ME[22]; + out[ 7] = SE[get_byte(1, B3)] ^ ME[23]; + out[ 8] = SE[get_byte(2, B0)] ^ ME[24]; + out[ 9] = SE[get_byte(2, B1)] ^ ME[25]; + out[10] = SE[get_byte(2, B2)] ^ ME[26]; + out[11] = SE[get_byte(2, B3)] ^ ME[27]; + out[12] = SE[get_byte(3, B0)] ^ ME[28]; + out[13] = SE[get_byte(3, B1)] ^ ME[29]; + out[14] = SE[get_byte(3, B2)] ^ ME[30]; + out[15] = SE[get_byte(3, B3)] ^ ME[31]; + } + +/* +* Square Decryption +*/ +void Square::dec(const byte in[], byte out[]) const + { + u32bit T0, T1, T2, T3, B0, B1, B2, B3; + B0 = TD0[in[ 0] ^ MD[ 0]] ^ TD1[in[ 4] ^ MD[ 4]] ^ + TD2[in[ 8] ^ MD[ 8]] ^ TD3[in[12] ^ MD[12]] ^ DK[0]; + B1 = TD0[in[ 1] ^ MD[ 1]] ^ TD1[in[ 5] ^ MD[ 5]] ^ + TD2[in[ 9] ^ MD[ 9]] ^ TD3[in[13] ^ MD[13]] ^ DK[1]; + B2 = TD0[in[ 2] ^ MD[ 2]] ^ TD1[in[ 6] ^ MD[ 6]] ^ + TD2[in[10] ^ MD[10]] ^ TD3[in[14] ^ MD[14]] ^ DK[2]; + B3 = TD0[in[ 3] ^ MD[ 3]] ^ TD1[in[ 7] ^ MD[ 7]] ^ + TD2[in[11] ^ MD[11]] ^ TD3[in[15] ^ MD[15]] ^ DK[3]; + for(u32bit j = 1; j != 7; j += 2) + { + T0 = TD0[get_byte(0, B0)] ^ TD1[get_byte(0, B1)] ^ + TD2[get_byte(0, B2)] ^ TD3[get_byte(0, B3)] ^ DK[4*j+0]; + T1 = TD0[get_byte(1, B0)] ^ TD1[get_byte(1, B1)] ^ + TD2[get_byte(1, B2)] ^ TD3[get_byte(1, B3)] ^ DK[4*j+1]; + T2 = TD0[get_byte(2, B0)] ^ TD1[get_byte(2, B1)] ^ + TD2[get_byte(2, B2)] ^ TD3[get_byte(2, B3)] ^ DK[4*j+2]; + T3 = TD0[get_byte(3, B0)] ^ TD1[get_byte(3, B1)] ^ + TD2[get_byte(3, B2)] ^ TD3[get_byte(3, B3)] ^ DK[4*j+3]; + B0 = TD0[get_byte(0, T0)] ^ TD1[get_byte(0, T1)] ^ + TD2[get_byte(0, T2)] ^ TD3[get_byte(0, T3)] ^ DK[4*j+4]; + B1 = TD0[get_byte(1, T0)] ^ TD1[get_byte(1, T1)] ^ + TD2[get_byte(1, T2)] ^ TD3[get_byte(1, T3)] ^ DK[4*j+5]; + B2 = TD0[get_byte(2, T0)] ^ TD1[get_byte(2, T1)] ^ + TD2[get_byte(2, T2)] ^ TD3[get_byte(2, T3)] ^ DK[4*j+6]; + B3 = TD0[get_byte(3, T0)] ^ TD1[get_byte(3, T1)] ^ + TD2[get_byte(3, T2)] ^ TD3[get_byte(3, T3)] ^ DK[4*j+7]; + } + out[ 0] = SD[get_byte(0, B0)] ^ MD[16]; + out[ 1] = SD[get_byte(0, B1)] ^ MD[17]; + out[ 2] = SD[get_byte(0, B2)] ^ MD[18]; + out[ 3] = SD[get_byte(0, B3)] ^ MD[19]; + out[ 4] = SD[get_byte(1, B0)] ^ MD[20]; + out[ 5] = SD[get_byte(1, B1)] ^ MD[21]; + out[ 6] = SD[get_byte(1, B2)] ^ MD[22]; + out[ 7] = SD[get_byte(1, B3)] ^ MD[23]; + out[ 8] = SD[get_byte(2, B0)] ^ MD[24]; + out[ 9] = SD[get_byte(2, B1)] ^ MD[25]; + out[10] = SD[get_byte(2, B2)] ^ MD[26]; + out[11] = SD[get_byte(2, B3)] ^ MD[27]; + out[12] = SD[get_byte(3, B0)] ^ MD[28]; + out[13] = SD[get_byte(3, B1)] ^ MD[29]; + out[14] = SD[get_byte(3, B2)] ^ MD[30]; + out[15] = SD[get_byte(3, B3)] ^ MD[31]; + } + +/* +* Square Key Schedule +*/ +void Square::key_schedule(const byte key[], u32bit) + { + SecureBuffer<u32bit, 36> XEK, XDK; + for(u32bit j = 0; j != 4; ++j) + XEK[j] = load_be<u32bit>(key, j); + for(u32bit j = 0; j != 8; ++j) + { + XEK[4*j+4] = XEK[4*j ] ^ rotate_left(XEK[4*j+3], 8) ^ (0x01000000 << j); + XEK[4*j+5] = XEK[4*j+1] ^ XEK[4*j+4]; + XEK[4*j+6] = XEK[4*j+2] ^ XEK[4*j+5]; + XEK[4*j+7] = XEK[4*j+3] ^ XEK[4*j+6]; + XDK.copy(28 - 4*j, XEK + 4*(j+1), 4); + transform(XEK + 4*j); + } + for(u32bit j = 0; j != 4; ++j) + for(u32bit k = 0; k != 4; ++k) + { + ME[4*j+k ] = get_byte(k, XEK[j ]); + ME[4*j+k+16] = get_byte(k, XEK[j+32]); + MD[4*j+k ] = get_byte(k, XDK[j ]); + MD[4*j+k+16] = get_byte(k, XEK[j ]); + } + EK.copy(XEK + 4, 28); + DK.copy(XDK + 4, 28); + } + +/* +* Square's Inverse Linear Transformation +*/ +void Square::transform(u32bit round_key[4]) + { + static const byte G[4][4] = { + { 0x02, 0x01, 0x01, 0x03 }, + { 0x03, 0x02, 0x01, 0x01 }, + { 0x01, 0x03, 0x02, 0x01 }, + { 0x01, 0x01, 0x03, 0x02 } }; + + for(u32bit j = 0; j != 4; ++j) + { + SecureBuffer<byte, 4> A, B; + + store_be(round_key[j], A); + + for(u32bit k = 0; k != 4; ++k) + for(u32bit l = 0; l != 4; ++l) + { + const byte a = A[l]; + const byte b = G[l][k]; + + if(a && b) + B[k] ^= ALog[(Log[a] + Log[b]) % 255]; + } + + round_key[j] = load_be<u32bit>(B.begin(), 0); + } + } + +/* +* Clear memory of sensitive data +*/ +void Square::clear() throw() + { + EK.clear(); + DK.clear(); + ME.clear(); + MD.clear(); + } + +} diff --git a/botan/src/block/square/square.h b/botan/src/block/square/square.h new file mode 100644 index 0000000..94a1fc3 --- /dev/null +++ b/botan/src/block/square/square.h @@ -0,0 +1,52 @@ +/* +* Square +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_SQUARE_H__ +#define BOTAN_SQUARE_H__ + +#include <botan/block_cipher.h> + +namespace Botan { + +/* +* Square +*/ +class BOTAN_DLL Square : public BlockCipher + { + public: + void clear() throw(); + std::string name() const { return "Square"; } + BlockCipher* clone() const { return new Square; } + Square() : BlockCipher(16, 16) {} + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key_schedule(const byte[], u32bit); + + static void transform(u32bit[4]); + + static const byte SE[256]; + static const byte SD[256]; + static const byte Log[256]; + static const byte ALog[255]; + + static const u32bit TE0[256]; + static const u32bit TE1[256]; + static const u32bit TE2[256]; + static const u32bit TE3[256]; + static const u32bit TD0[256]; + static const u32bit TD1[256]; + static const u32bit TD2[256]; + static const u32bit TD3[256]; + + SecureBuffer<u32bit, 28> EK, DK; + SecureBuffer<byte, 32> ME, MD; + }; + +} + +#endif diff --git a/botan/src/block/tea/info.txt b/botan/src/block/tea/info.txt new file mode 100644 index 0000000..6a0e76b --- /dev/null +++ b/botan/src/block/tea/info.txt @@ -0,0 +1,10 @@ +realname "TEA" + +define TEA + +load_on auto + +<add> +tea.cpp +tea.h +</add> diff --git a/botan/src/block/tea/tea.cpp b/botan/src/block/tea/tea.cpp new file mode 100644 index 0000000..2b4212d --- /dev/null +++ b/botan/src/block/tea/tea.cpp @@ -0,0 +1,58 @@ +/* +* TEA +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/tea.h> +#include <botan/loadstor.h> + +namespace Botan { + +/* +* TEA Encryption +*/ +void TEA::enc(const byte in[], byte out[]) const + { + u32bit L = load_be<u32bit>(in, 0), R = load_be<u32bit>(in, 1); + + u32bit S = 0; + for(u32bit j = 0; j != 32; ++j) + { + S += 0x9E3779B9; + L += ((R << 4) + K[0]) ^ (R + S) ^ ((R >> 5) + K[1]); + R += ((L << 4) + K[2]) ^ (L + S) ^ ((L >> 5) + K[3]); + } + + store_be(out, L, R); + } + +/* +* TEA Decryption +*/ +void TEA::dec(const byte in[], byte out[]) const + { + u32bit L = load_be<u32bit>(in, 0), R = load_be<u32bit>(in, 1); + + u32bit S = 0xC6EF3720; + for(u32bit j = 0; j != 32; ++j) + { + R -= ((L << 4) + K[2]) ^ (L + S) ^ ((L >> 5) + K[3]); + L -= ((R << 4) + K[0]) ^ (R + S) ^ ((R >> 5) + K[1]); + S -= 0x9E3779B9; + } + + store_be(out, L, R); + } + +/* +* TEA Key Schedule +*/ +void TEA::key_schedule(const byte key[], u32bit) + { + for(u32bit j = 0; j != 4; ++j) + K[j] = load_be<u32bit>(key, j); + } + +} diff --git a/botan/src/block/tea/tea.h b/botan/src/block/tea/tea.h new file mode 100644 index 0000000..8ddf3e3 --- /dev/null +++ b/botan/src/block/tea/tea.h @@ -0,0 +1,34 @@ +/* +* TEA +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_TEA_H__ +#define BOTAN_TEA_H__ + +#include <botan/block_cipher.h> + +namespace Botan { + +/* +* TEA +*/ +class BOTAN_DLL TEA : public BlockCipher + { + public: + void clear() throw() { K.clear(); } + std::string name() const { return "TEA"; } + BlockCipher* clone() const { return new TEA; } + TEA() : BlockCipher(8, 16) {} + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key_schedule(const byte[], u32bit); + SecureBuffer<u32bit, 4> K; + }; + +} + +#endif diff --git a/botan/src/block/twofish/info.txt b/botan/src/block/twofish/info.txt new file mode 100644 index 0000000..35639d8 --- /dev/null +++ b/botan/src/block/twofish/info.txt @@ -0,0 +1,11 @@ +realname "Twofish" + +define TWOFISH + +load_on auto + +<add> +two_tab.cpp +twofish.cpp +twofish.h +</add> diff --git a/botan/src/block/twofish/two_tab.cpp b/botan/src/block/twofish/two_tab.cpp new file mode 100644 index 0000000..19ba58d --- /dev/null +++ b/botan/src/block/twofish/two_tab.cpp @@ -0,0 +1,293 @@ +/* +* S-Box and MDS Tables for Twofish +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/twofish.h> + +namespace Botan { + +const byte Twofish::Q0[256] = { + 0xA9, 0x67, 0xB3, 0xE8, 0x04, 0xFD, 0xA3, 0x76, 0x9A, 0x92, 0x80, 0x78, + 0xE4, 0xDD, 0xD1, 0x38, 0x0D, 0xC6, 0x35, 0x98, 0x18, 0xF7, 0xEC, 0x6C, + 0x43, 0x75, 0x37, 0x26, 0xFA, 0x13, 0x94, 0x48, 0xF2, 0xD0, 0x8B, 0x30, + 0x84, 0x54, 0xDF, 0x23, 0x19, 0x5B, 0x3D, 0x59, 0xF3, 0xAE, 0xA2, 0x82, + 0x63, 0x01, 0x83, 0x2E, 0xD9, 0x51, 0x9B, 0x7C, 0xA6, 0xEB, 0xA5, 0xBE, + 0x16, 0x0C, 0xE3, 0x61, 0xC0, 0x8C, 0x3A, 0xF5, 0x73, 0x2C, 0x25, 0x0B, + 0xBB, 0x4E, 0x89, 0x6B, 0x53, 0x6A, 0xB4, 0xF1, 0xE1, 0xE6, 0xBD, 0x45, + 0xE2, 0xF4, 0xB6, 0x66, 0xCC, 0x95, 0x03, 0x56, 0xD4, 0x1C, 0x1E, 0xD7, + 0xFB, 0xC3, 0x8E, 0xB5, 0xE9, 0xCF, 0xBF, 0xBA, 0xEA, 0x77, 0x39, 0xAF, + 0x33, 0xC9, 0x62, 0x71, 0x81, 0x79, 0x09, 0xAD, 0x24, 0xCD, 0xF9, 0xD8, + 0xE5, 0xC5, 0xB9, 0x4D, 0x44, 0x08, 0x86, 0xE7, 0xA1, 0x1D, 0xAA, 0xED, + 0x06, 0x70, 0xB2, 0xD2, 0x41, 0x7B, 0xA0, 0x11, 0x31, 0xC2, 0x27, 0x90, + 0x20, 0xF6, 0x60, 0xFF, 0x96, 0x5C, 0xB1, 0xAB, 0x9E, 0x9C, 0x52, 0x1B, + 0x5F, 0x93, 0x0A, 0xEF, 0x91, 0x85, 0x49, 0xEE, 0x2D, 0x4F, 0x8F, 0x3B, + 0x47, 0x87, 0x6D, 0x46, 0xD6, 0x3E, 0x69, 0x64, 0x2A, 0xCE, 0xCB, 0x2F, + 0xFC, 0x97, 0x05, 0x7A, 0xAC, 0x7F, 0xD5, 0x1A, 0x4B, 0x0E, 0xA7, 0x5A, + 0x28, 0x14, 0x3F, 0x29, 0x88, 0x3C, 0x4C, 0x02, 0xB8, 0xDA, 0xB0, 0x17, + 0x55, 0x1F, 0x8A, 0x7D, 0x57, 0xC7, 0x8D, 0x74, 0xB7, 0xC4, 0x9F, 0x72, + 0x7E, 0x15, 0x22, 0x12, 0x58, 0x07, 0x99, 0x34, 0x6E, 0x50, 0xDE, 0x68, + 0x65, 0xBC, 0xDB, 0xF8, 0xC8, 0xA8, 0x2B, 0x40, 0xDC, 0xFE, 0x32, 0xA4, + 0xCA, 0x10, 0x21, 0xF0, 0xD3, 0x5D, 0x0F, 0x00, 0x6F, 0x9D, 0x36, 0x42, + 0x4A, 0x5E, 0xC1, 0xE0 }; + +const byte Twofish::Q1[256] = { + 0x75, 0xF3, 0xC6, 0xF4, 0xDB, 0x7B, 0xFB, 0xC8, 0x4A, 0xD3, 0xE6, 0x6B, + 0x45, 0x7D, 0xE8, 0x4B, 0xD6, 0x32, 0xD8, 0xFD, 0x37, 0x71, 0xF1, 0xE1, + 0x30, 0x0F, 0xF8, 0x1B, 0x87, 0xFA, 0x06, 0x3F, 0x5E, 0xBA, 0xAE, 0x5B, + 0x8A, 0x00, 0xBC, 0x9D, 0x6D, 0xC1, 0xB1, 0x0E, 0x80, 0x5D, 0xD2, 0xD5, + 0xA0, 0x84, 0x07, 0x14, 0xB5, 0x90, 0x2C, 0xA3, 0xB2, 0x73, 0x4C, 0x54, + 0x92, 0x74, 0x36, 0x51, 0x38, 0xB0, 0xBD, 0x5A, 0xFC, 0x60, 0x62, 0x96, + 0x6C, 0x42, 0xF7, 0x10, 0x7C, 0x28, 0x27, 0x8C, 0x13, 0x95, 0x9C, 0xC7, + 0x24, 0x46, 0x3B, 0x70, 0xCA, 0xE3, 0x85, 0xCB, 0x11, 0xD0, 0x93, 0xB8, + 0xA6, 0x83, 0x20, 0xFF, 0x9F, 0x77, 0xC3, 0xCC, 0x03, 0x6F, 0x08, 0xBF, + 0x40, 0xE7, 0x2B, 0xE2, 0x79, 0x0C, 0xAA, 0x82, 0x41, 0x3A, 0xEA, 0xB9, + 0xE4, 0x9A, 0xA4, 0x97, 0x7E, 0xDA, 0x7A, 0x17, 0x66, 0x94, 0xA1, 0x1D, + 0x3D, 0xF0, 0xDE, 0xB3, 0x0B, 0x72, 0xA7, 0x1C, 0xEF, 0xD1, 0x53, 0x3E, + 0x8F, 0x33, 0x26, 0x5F, 0xEC, 0x76, 0x2A, 0x49, 0x81, 0x88, 0xEE, 0x21, + 0xC4, 0x1A, 0xEB, 0xD9, 0xC5, 0x39, 0x99, 0xCD, 0xAD, 0x31, 0x8B, 0x01, + 0x18, 0x23, 0xDD, 0x1F, 0x4E, 0x2D, 0xF9, 0x48, 0x4F, 0xF2, 0x65, 0x8E, + 0x78, 0x5C, 0x58, 0x19, 0x8D, 0xE5, 0x98, 0x57, 0x67, 0x7F, 0x05, 0x64, + 0xAF, 0x63, 0xB6, 0xFE, 0xF5, 0xB7, 0x3C, 0xA5, 0xCE, 0xE9, 0x68, 0x44, + 0xE0, 0x4D, 0x43, 0x69, 0x29, 0x2E, 0xAC, 0x15, 0x59, 0xA8, 0x0A, 0x9E, + 0x6E, 0x47, 0xDF, 0x34, 0x35, 0x6A, 0xCF, 0xDC, 0x22, 0xC9, 0xC0, 0x9B, + 0x89, 0xD4, 0xED, 0xAB, 0x12, 0xA2, 0x0D, 0x52, 0xBB, 0x02, 0x2F, 0xA9, + 0xD7, 0x61, 0x1E, 0xB4, 0x50, 0x04, 0xF6, 0xC2, 0x16, 0x25, 0x86, 0x56, + 0x55, 0x09, 0xBE, 0x91 }; + +const byte Twofish::RS[32] = { + 0x01, 0xA4, 0x02, 0xA4, 0xA4, 0x56, 0xA1, 0x55, 0x55, 0x82, 0xFC, 0x87, + 0x87, 0xF3, 0xC1, 0x5A, 0x5A, 0x1E, 0x47, 0x58, 0x58, 0xC6, 0xAE, 0xDB, + 0xDB, 0x68, 0x3D, 0x9E, 0x9E, 0xE5, 0x19, 0x03 }; + +const byte Twofish::EXP_TO_POLY[255] = { + 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x4D, 0x9A, 0x79, 0xF2, + 0xA9, 0x1F, 0x3E, 0x7C, 0xF8, 0xBD, 0x37, 0x6E, 0xDC, 0xF5, 0xA7, 0x03, + 0x06, 0x0C, 0x18, 0x30, 0x60, 0xC0, 0xCD, 0xD7, 0xE3, 0x8B, 0x5B, 0xB6, + 0x21, 0x42, 0x84, 0x45, 0x8A, 0x59, 0xB2, 0x29, 0x52, 0xA4, 0x05, 0x0A, + 0x14, 0x28, 0x50, 0xA0, 0x0D, 0x1A, 0x34, 0x68, 0xD0, 0xED, 0x97, 0x63, + 0xC6, 0xC1, 0xCF, 0xD3, 0xEB, 0x9B, 0x7B, 0xF6, 0xA1, 0x0F, 0x1E, 0x3C, + 0x78, 0xF0, 0xAD, 0x17, 0x2E, 0x5C, 0xB8, 0x3D, 0x7A, 0xF4, 0xA5, 0x07, + 0x0E, 0x1C, 0x38, 0x70, 0xE0, 0x8D, 0x57, 0xAE, 0x11, 0x22, 0x44, 0x88, + 0x5D, 0xBA, 0x39, 0x72, 0xE4, 0x85, 0x47, 0x8E, 0x51, 0xA2, 0x09, 0x12, + 0x24, 0x48, 0x90, 0x6D, 0xDA, 0xF9, 0xBF, 0x33, 0x66, 0xCC, 0xD5, 0xE7, + 0x83, 0x4B, 0x96, 0x61, 0xC2, 0xC9, 0xDF, 0xF3, 0xAB, 0x1B, 0x36, 0x6C, + 0xD8, 0xFD, 0xB7, 0x23, 0x46, 0x8C, 0x55, 0xAA, 0x19, 0x32, 0x64, 0xC8, + 0xDD, 0xF7, 0xA3, 0x0B, 0x16, 0x2C, 0x58, 0xB0, 0x2D, 0x5A, 0xB4, 0x25, + 0x4A, 0x94, 0x65, 0xCA, 0xD9, 0xFF, 0xB3, 0x2B, 0x56, 0xAC, 0x15, 0x2A, + 0x54, 0xA8, 0x1D, 0x3A, 0x74, 0xE8, 0x9D, 0x77, 0xEE, 0x91, 0x6F, 0xDE, + 0xF1, 0xAF, 0x13, 0x26, 0x4C, 0x98, 0x7D, 0xFA, 0xB9, 0x3F, 0x7E, 0xFC, + 0xB5, 0x27, 0x4E, 0x9C, 0x75, 0xEA, 0x99, 0x7F, 0xFE, 0xB1, 0x2F, 0x5E, + 0xBC, 0x35, 0x6A, 0xD4, 0xE5, 0x87, 0x43, 0x86, 0x41, 0x82, 0x49, 0x92, + 0x69, 0xD2, 0xE9, 0x9F, 0x73, 0xE6, 0x81, 0x4F, 0x9E, 0x71, 0xE2, 0x89, + 0x5F, 0xBE, 0x31, 0x62, 0xC4, 0xC5, 0xC7, 0xC3, 0xCB, 0xDB, 0xFB, 0xBB, + 0x3B, 0x76, 0xEC, 0x95, 0x67, 0xCE, 0xD1, 0xEF, 0x93, 0x6B, 0xD6, 0xE1, + 0x8F, 0x53, 0xA6 }; + +const byte Twofish::POLY_TO_EXP[255] = { + 0x00, 0x01, 0x17, 0x02, 0x2E, 0x18, 0x53, 0x03, 0x6A, 0x2F, 0x93, 0x19, + 0x34, 0x54, 0x45, 0x04, 0x5C, 0x6B, 0xB6, 0x30, 0xA6, 0x94, 0x4B, 0x1A, + 0x8C, 0x35, 0x81, 0x55, 0xAA, 0x46, 0x0D, 0x05, 0x24, 0x5D, 0x87, 0x6C, + 0x9B, 0xB7, 0xC1, 0x31, 0x2B, 0xA7, 0xA3, 0x95, 0x98, 0x4C, 0xCA, 0x1B, + 0xE6, 0x8D, 0x73, 0x36, 0xCD, 0x82, 0x12, 0x56, 0x62, 0xAB, 0xF0, 0x47, + 0x4F, 0x0E, 0xBD, 0x06, 0xD4, 0x25, 0xD2, 0x5E, 0x27, 0x88, 0x66, 0x6D, + 0xD6, 0x9C, 0x79, 0xB8, 0x08, 0xC2, 0xDF, 0x32, 0x68, 0x2C, 0xFD, 0xA8, + 0x8A, 0xA4, 0x5A, 0x96, 0x29, 0x99, 0x22, 0x4D, 0x60, 0xCB, 0xE4, 0x1C, + 0x7B, 0xE7, 0x3B, 0x8E, 0x9E, 0x74, 0xF4, 0x37, 0xD8, 0xCE, 0xF9, 0x83, + 0x6F, 0x13, 0xB2, 0x57, 0xE1, 0x63, 0xDC, 0xAC, 0xC4, 0xF1, 0xAF, 0x48, + 0x0A, 0x50, 0x42, 0x0F, 0xBA, 0xBE, 0xC7, 0x07, 0xDE, 0xD5, 0x78, 0x26, + 0x65, 0xD3, 0xD1, 0x5F, 0xE3, 0x28, 0x21, 0x89, 0x59, 0x67, 0xFC, 0x6E, + 0xB1, 0xD7, 0xF8, 0x9D, 0xF3, 0x7A, 0x3A, 0xB9, 0xC6, 0x09, 0x41, 0xC3, + 0xAE, 0xE0, 0xDB, 0x33, 0x44, 0x69, 0x92, 0x2D, 0x52, 0xFE, 0x16, 0xA9, + 0x0C, 0x8B, 0x80, 0xA5, 0x4A, 0x5B, 0xB5, 0x97, 0xC9, 0x2A, 0xA2, 0x9A, + 0xC0, 0x23, 0x86, 0x4E, 0xBC, 0x61, 0xEF, 0xCC, 0x11, 0xE5, 0x72, 0x1D, + 0x3D, 0x7C, 0xEB, 0xE8, 0xE9, 0x3C, 0xEA, 0x8F, 0x7D, 0x9F, 0xEC, 0x75, + 0x1E, 0xF5, 0x3E, 0x38, 0xF6, 0xD9, 0x3F, 0xCF, 0x76, 0xFA, 0x1F, 0x84, + 0xA0, 0x70, 0xED, 0x14, 0x90, 0xB3, 0x7E, 0x58, 0xFB, 0xE2, 0x20, 0x64, + 0xD0, 0xDD, 0x77, 0xAD, 0xDA, 0xC5, 0x40, 0xF2, 0x39, 0xB0, 0xF7, 0x49, + 0xB4, 0x0B, 0x7F, 0x51, 0x15, 0x43, 0x91, 0x10, 0x71, 0xBB, 0xEE, 0xBF, + 0x85, 0xC8, 0xA1 }; + +const u32bit Twofish::MDS0[256] = { + 0xBCBC3275, 0xECEC21F3, 0x202043C6, 0xB3B3C9F4, 0xDADA03DB, 0x02028B7B, + 0xE2E22BFB, 0x9E9EFAC8, 0xC9C9EC4A, 0xD4D409D3, 0x18186BE6, 0x1E1E9F6B, + 0x98980E45, 0xB2B2387D, 0xA6A6D2E8, 0x2626B74B, 0x3C3C57D6, 0x93938A32, + 0x8282EED8, 0x525298FD, 0x7B7BD437, 0xBBBB3771, 0x5B5B97F1, 0x474783E1, + 0x24243C30, 0x5151E20F, 0xBABAC6F8, 0x4A4AF31B, 0xBFBF4887, 0x0D0D70FA, + 0xB0B0B306, 0x7575DE3F, 0xD2D2FD5E, 0x7D7D20BA, 0x666631AE, 0x3A3AA35B, + 0x59591C8A, 0x00000000, 0xCDCD93BC, 0x1A1AE09D, 0xAEAE2C6D, 0x7F7FABC1, + 0x2B2BC7B1, 0xBEBEB90E, 0xE0E0A080, 0x8A8A105D, 0x3B3B52D2, 0x6464BAD5, + 0xD8D888A0, 0xE7E7A584, 0x5F5FE807, 0x1B1B1114, 0x2C2CC2B5, 0xFCFCB490, + 0x3131272C, 0x808065A3, 0x73732AB2, 0x0C0C8173, 0x79795F4C, 0x6B6B4154, + 0x4B4B0292, 0x53536974, 0x94948F36, 0x83831F51, 0x2A2A3638, 0xC4C49CB0, + 0x2222C8BD, 0xD5D5F85A, 0xBDBDC3FC, 0x48487860, 0xFFFFCE62, 0x4C4C0796, + 0x4141776C, 0xC7C7E642, 0xEBEB24F7, 0x1C1C1410, 0x5D5D637C, 0x36362228, + 0x6767C027, 0xE9E9AF8C, 0x4444F913, 0x1414EA95, 0xF5F5BB9C, 0xCFCF18C7, + 0x3F3F2D24, 0xC0C0E346, 0x7272DB3B, 0x54546C70, 0x29294CCA, 0xF0F035E3, + 0x0808FE85, 0xC6C617CB, 0xF3F34F11, 0x8C8CE4D0, 0xA4A45993, 0xCACA96B8, + 0x68683BA6, 0xB8B84D83, 0x38382820, 0xE5E52EFF, 0xADAD569F, 0x0B0B8477, + 0xC8C81DC3, 0x9999FFCC, 0x5858ED03, 0x19199A6F, 0x0E0E0A08, 0x95957EBF, + 0x70705040, 0xF7F730E7, 0x6E6ECF2B, 0x1F1F6EE2, 0xB5B53D79, 0x09090F0C, + 0x616134AA, 0x57571682, 0x9F9F0B41, 0x9D9D803A, 0x111164EA, 0x2525CDB9, + 0xAFAFDDE4, 0x4545089A, 0xDFDF8DA4, 0xA3A35C97, 0xEAEAD57E, 0x353558DA, + 0xEDEDD07A, 0x4343FC17, 0xF8F8CB66, 0xFBFBB194, 0x3737D3A1, 0xFAFA401D, + 0xC2C2683D, 0xB4B4CCF0, 0x32325DDE, 0x9C9C71B3, 0x5656E70B, 0xE3E3DA72, + 0x878760A7, 0x15151B1C, 0xF9F93AEF, 0x6363BFD1, 0x3434A953, 0x9A9A853E, + 0xB1B1428F, 0x7C7CD133, 0x88889B26, 0x3D3DA65F, 0xA1A1D7EC, 0xE4E4DF76, + 0x8181942A, 0x91910149, 0x0F0FFB81, 0xEEEEAA88, 0x161661EE, 0xD7D77321, + 0x9797F5C4, 0xA5A5A81A, 0xFEFE3FEB, 0x6D6DB5D9, 0x7878AEC5, 0xC5C56D39, + 0x1D1DE599, 0x7676A4CD, 0x3E3EDCAD, 0xCBCB6731, 0xB6B6478B, 0xEFEF5B01, + 0x12121E18, 0x6060C523, 0x6A6AB0DD, 0x4D4DF61F, 0xCECEE94E, 0xDEDE7C2D, + 0x55559DF9, 0x7E7E5A48, 0x2121B24F, 0x03037AF2, 0xA0A02665, 0x5E5E198E, + 0x5A5A6678, 0x65654B5C, 0x62624E58, 0xFDFD4519, 0x0606F48D, 0x404086E5, + 0xF2F2BE98, 0x3333AC57, 0x17179067, 0x05058E7F, 0xE8E85E05, 0x4F4F7D64, + 0x89896AAF, 0x10109563, 0x74742FB6, 0x0A0A75FE, 0x5C5C92F5, 0x9B9B74B7, + 0x2D2D333C, 0x3030D6A5, 0x2E2E49CE, 0x494989E9, 0x46467268, 0x77775544, + 0xA8A8D8E0, 0x9696044D, 0x2828BD43, 0xA9A92969, 0xD9D97929, 0x8686912E, + 0xD1D187AC, 0xF4F44A15, 0x8D8D1559, 0xD6D682A8, 0xB9B9BC0A, 0x42420D9E, + 0xF6F6C16E, 0x2F2FB847, 0xDDDD06DF, 0x23233934, 0xCCCC6235, 0xF1F1C46A, + 0xC1C112CF, 0x8585EBDC, 0x8F8F9E22, 0x7171A1C9, 0x9090F0C0, 0xAAAA539B, + 0x0101F189, 0x8B8BE1D4, 0x4E4E8CED, 0x8E8E6FAB, 0xABABA212, 0x6F6F3EA2, + 0xE6E6540D, 0xDBDBF252, 0x92927BBB, 0xB7B7B602, 0x6969CA2F, 0x3939D9A9, + 0xD3D30CD7, 0xA7A72361, 0xA2A2AD1E, 0xC3C399B4, 0x6C6C4450, 0x07070504, + 0x04047FF6, 0x272746C2, 0xACACA716, 0xD0D07625, 0x50501386, 0xDCDCF756, + 0x84841A55, 0xE1E15109, 0x7A7A25BE, 0x1313EF91 }; + +const u32bit Twofish::MDS1[256] = { + 0xA9D93939, 0x67901717, 0xB3719C9C, 0xE8D2A6A6, 0x04050707, 0xFD985252, + 0xA3658080, 0x76DFE4E4, 0x9A084545, 0x92024B4B, 0x80A0E0E0, 0x78665A5A, + 0xE4DDAFAF, 0xDDB06A6A, 0xD1BF6363, 0x38362A2A, 0x0D54E6E6, 0xC6432020, + 0x3562CCCC, 0x98BEF2F2, 0x181E1212, 0xF724EBEB, 0xECD7A1A1, 0x6C774141, + 0x43BD2828, 0x7532BCBC, 0x37D47B7B, 0x269B8888, 0xFA700D0D, 0x13F94444, + 0x94B1FBFB, 0x485A7E7E, 0xF27A0303, 0xD0E48C8C, 0x8B47B6B6, 0x303C2424, + 0x84A5E7E7, 0x54416B6B, 0xDF06DDDD, 0x23C56060, 0x1945FDFD, 0x5BA33A3A, + 0x3D68C2C2, 0x59158D8D, 0xF321ECEC, 0xAE316666, 0xA23E6F6F, 0x82165757, + 0x63951010, 0x015BEFEF, 0x834DB8B8, 0x2E918686, 0xD9B56D6D, 0x511F8383, + 0x9B53AAAA, 0x7C635D5D, 0xA63B6868, 0xEB3FFEFE, 0xA5D63030, 0xBE257A7A, + 0x16A7ACAC, 0x0C0F0909, 0xE335F0F0, 0x6123A7A7, 0xC0F09090, 0x8CAFE9E9, + 0x3A809D9D, 0xF5925C5C, 0x73810C0C, 0x2C273131, 0x2576D0D0, 0x0BE75656, + 0xBB7B9292, 0x4EE9CECE, 0x89F10101, 0x6B9F1E1E, 0x53A93434, 0x6AC4F1F1, + 0xB499C3C3, 0xF1975B5B, 0xE1834747, 0xE66B1818, 0xBDC82222, 0x450E9898, + 0xE26E1F1F, 0xF4C9B3B3, 0xB62F7474, 0x66CBF8F8, 0xCCFF9999, 0x95EA1414, + 0x03ED5858, 0x56F7DCDC, 0xD4E18B8B, 0x1C1B1515, 0x1EADA2A2, 0xD70CD3D3, + 0xFB2BE2E2, 0xC31DC8C8, 0x8E195E5E, 0xB5C22C2C, 0xE9894949, 0xCF12C1C1, + 0xBF7E9595, 0xBA207D7D, 0xEA641111, 0x77840B0B, 0x396DC5C5, 0xAF6A8989, + 0x33D17C7C, 0xC9A17171, 0x62CEFFFF, 0x7137BBBB, 0x81FB0F0F, 0x793DB5B5, + 0x0951E1E1, 0xADDC3E3E, 0x242D3F3F, 0xCDA47676, 0xF99D5555, 0xD8EE8282, + 0xE5864040, 0xC5AE7878, 0xB9CD2525, 0x4D049696, 0x44557777, 0x080A0E0E, + 0x86135050, 0xE730F7F7, 0xA1D33737, 0x1D40FAFA, 0xAA346161, 0xED8C4E4E, + 0x06B3B0B0, 0x706C5454, 0xB22A7373, 0xD2523B3B, 0x410B9F9F, 0x7B8B0202, + 0xA088D8D8, 0x114FF3F3, 0x3167CBCB, 0xC2462727, 0x27C06767, 0x90B4FCFC, + 0x20283838, 0xF67F0404, 0x60784848, 0xFF2EE5E5, 0x96074C4C, 0x5C4B6565, + 0xB1C72B2B, 0xAB6F8E8E, 0x9E0D4242, 0x9CBBF5F5, 0x52F2DBDB, 0x1BF34A4A, + 0x5FA63D3D, 0x9359A4A4, 0x0ABCB9B9, 0xEF3AF9F9, 0x91EF1313, 0x85FE0808, + 0x49019191, 0xEE611616, 0x2D7CDEDE, 0x4FB22121, 0x8F42B1B1, 0x3BDB7272, + 0x47B82F2F, 0x8748BFBF, 0x6D2CAEAE, 0x46E3C0C0, 0xD6573C3C, 0x3E859A9A, + 0x6929A9A9, 0x647D4F4F, 0x2A948181, 0xCE492E2E, 0xCB17C6C6, 0x2FCA6969, + 0xFCC3BDBD, 0x975CA3A3, 0x055EE8E8, 0x7AD0EDED, 0xAC87D1D1, 0x7F8E0505, + 0xD5BA6464, 0x1AA8A5A5, 0x4BB72626, 0x0EB9BEBE, 0xA7608787, 0x5AF8D5D5, + 0x28223636, 0x14111B1B, 0x3FDE7575, 0x2979D9D9, 0x88AAEEEE, 0x3C332D2D, + 0x4C5F7979, 0x02B6B7B7, 0xB896CACA, 0xDA583535, 0xB09CC4C4, 0x17FC4343, + 0x551A8484, 0x1FF64D4D, 0x8A1C5959, 0x7D38B2B2, 0x57AC3333, 0xC718CFCF, + 0x8DF40606, 0x74695353, 0xB7749B9B, 0xC4F59797, 0x9F56ADAD, 0x72DAE3E3, + 0x7ED5EAEA, 0x154AF4F4, 0x229E8F8F, 0x12A2ABAB, 0x584E6262, 0x07E85F5F, + 0x99E51D1D, 0x34392323, 0x6EC1F6F6, 0x50446C6C, 0xDE5D3232, 0x68724646, + 0x6526A0A0, 0xBC93CDCD, 0xDB03DADA, 0xF8C6BABA, 0xC8FA9E9E, 0xA882D6D6, + 0x2BCF6E6E, 0x40507070, 0xDCEB8585, 0xFE750A0A, 0x328A9393, 0xA48DDFDF, + 0xCA4C2929, 0x10141C1C, 0x2173D7D7, 0xF0CCB4B4, 0xD309D4D4, 0x5D108A8A, + 0x0FE25151, 0x00000000, 0x6F9A1919, 0x9DE01A1A, 0x368F9494, 0x42E6C7C7, + 0x4AECC9C9, 0x5EFDD2D2, 0xC1AB7F7F, 0xE0D8A8A8 }; + +const u32bit Twofish::MDS2[256] = { + 0xBC75BC32, 0xECF3EC21, 0x20C62043, 0xB3F4B3C9, 0xDADBDA03, 0x027B028B, + 0xE2FBE22B, 0x9EC89EFA, 0xC94AC9EC, 0xD4D3D409, 0x18E6186B, 0x1E6B1E9F, + 0x9845980E, 0xB27DB238, 0xA6E8A6D2, 0x264B26B7, 0x3CD63C57, 0x9332938A, + 0x82D882EE, 0x52FD5298, 0x7B377BD4, 0xBB71BB37, 0x5BF15B97, 0x47E14783, + 0x2430243C, 0x510F51E2, 0xBAF8BAC6, 0x4A1B4AF3, 0xBF87BF48, 0x0DFA0D70, + 0xB006B0B3, 0x753F75DE, 0xD25ED2FD, 0x7DBA7D20, 0x66AE6631, 0x3A5B3AA3, + 0x598A591C, 0x00000000, 0xCDBCCD93, 0x1A9D1AE0, 0xAE6DAE2C, 0x7FC17FAB, + 0x2BB12BC7, 0xBE0EBEB9, 0xE080E0A0, 0x8A5D8A10, 0x3BD23B52, 0x64D564BA, + 0xD8A0D888, 0xE784E7A5, 0x5F075FE8, 0x1B141B11, 0x2CB52CC2, 0xFC90FCB4, + 0x312C3127, 0x80A38065, 0x73B2732A, 0x0C730C81, 0x794C795F, 0x6B546B41, + 0x4B924B02, 0x53745369, 0x9436948F, 0x8351831F, 0x2A382A36, 0xC4B0C49C, + 0x22BD22C8, 0xD55AD5F8, 0xBDFCBDC3, 0x48604878, 0xFF62FFCE, 0x4C964C07, + 0x416C4177, 0xC742C7E6, 0xEBF7EB24, 0x1C101C14, 0x5D7C5D63, 0x36283622, + 0x672767C0, 0xE98CE9AF, 0x441344F9, 0x149514EA, 0xF59CF5BB, 0xCFC7CF18, + 0x3F243F2D, 0xC046C0E3, 0x723B72DB, 0x5470546C, 0x29CA294C, 0xF0E3F035, + 0x088508FE, 0xC6CBC617, 0xF311F34F, 0x8CD08CE4, 0xA493A459, 0xCAB8CA96, + 0x68A6683B, 0xB883B84D, 0x38203828, 0xE5FFE52E, 0xAD9FAD56, 0x0B770B84, + 0xC8C3C81D, 0x99CC99FF, 0x580358ED, 0x196F199A, 0x0E080E0A, 0x95BF957E, + 0x70407050, 0xF7E7F730, 0x6E2B6ECF, 0x1FE21F6E, 0xB579B53D, 0x090C090F, + 0x61AA6134, 0x57825716, 0x9F419F0B, 0x9D3A9D80, 0x11EA1164, 0x25B925CD, + 0xAFE4AFDD, 0x459A4508, 0xDFA4DF8D, 0xA397A35C, 0xEA7EEAD5, 0x35DA3558, + 0xED7AEDD0, 0x431743FC, 0xF866F8CB, 0xFB94FBB1, 0x37A137D3, 0xFA1DFA40, + 0xC23DC268, 0xB4F0B4CC, 0x32DE325D, 0x9CB39C71, 0x560B56E7, 0xE372E3DA, + 0x87A78760, 0x151C151B, 0xF9EFF93A, 0x63D163BF, 0x345334A9, 0x9A3E9A85, + 0xB18FB142, 0x7C337CD1, 0x8826889B, 0x3D5F3DA6, 0xA1ECA1D7, 0xE476E4DF, + 0x812A8194, 0x91499101, 0x0F810FFB, 0xEE88EEAA, 0x16EE1661, 0xD721D773, + 0x97C497F5, 0xA51AA5A8, 0xFEEBFE3F, 0x6DD96DB5, 0x78C578AE, 0xC539C56D, + 0x1D991DE5, 0x76CD76A4, 0x3EAD3EDC, 0xCB31CB67, 0xB68BB647, 0xEF01EF5B, + 0x1218121E, 0x602360C5, 0x6ADD6AB0, 0x4D1F4DF6, 0xCE4ECEE9, 0xDE2DDE7C, + 0x55F9559D, 0x7E487E5A, 0x214F21B2, 0x03F2037A, 0xA065A026, 0x5E8E5E19, + 0x5A785A66, 0x655C654B, 0x6258624E, 0xFD19FD45, 0x068D06F4, 0x40E54086, + 0xF298F2BE, 0x335733AC, 0x17671790, 0x057F058E, 0xE805E85E, 0x4F644F7D, + 0x89AF896A, 0x10631095, 0x74B6742F, 0x0AFE0A75, 0x5CF55C92, 0x9BB79B74, + 0x2D3C2D33, 0x30A530D6, 0x2ECE2E49, 0x49E94989, 0x46684672, 0x77447755, + 0xA8E0A8D8, 0x964D9604, 0x284328BD, 0xA969A929, 0xD929D979, 0x862E8691, + 0xD1ACD187, 0xF415F44A, 0x8D598D15, 0xD6A8D682, 0xB90AB9BC, 0x429E420D, + 0xF66EF6C1, 0x2F472FB8, 0xDDDFDD06, 0x23342339, 0xCC35CC62, 0xF16AF1C4, + 0xC1CFC112, 0x85DC85EB, 0x8F228F9E, 0x71C971A1, 0x90C090F0, 0xAA9BAA53, + 0x018901F1, 0x8BD48BE1, 0x4EED4E8C, 0x8EAB8E6F, 0xAB12ABA2, 0x6FA26F3E, + 0xE60DE654, 0xDB52DBF2, 0x92BB927B, 0xB702B7B6, 0x692F69CA, 0x39A939D9, + 0xD3D7D30C, 0xA761A723, 0xA21EA2AD, 0xC3B4C399, 0x6C506C44, 0x07040705, + 0x04F6047F, 0x27C22746, 0xAC16ACA7, 0xD025D076, 0x50865013, 0xDC56DCF7, + 0x8455841A, 0xE109E151, 0x7ABE7A25, 0x139113EF }; + +const u32bit Twofish::MDS3[256] = { + 0xD939A9D9, 0x90176790, 0x719CB371, 0xD2A6E8D2, 0x05070405, 0x9852FD98, + 0x6580A365, 0xDFE476DF, 0x08459A08, 0x024B9202, 0xA0E080A0, 0x665A7866, + 0xDDAFE4DD, 0xB06ADDB0, 0xBF63D1BF, 0x362A3836, 0x54E60D54, 0x4320C643, + 0x62CC3562, 0xBEF298BE, 0x1E12181E, 0x24EBF724, 0xD7A1ECD7, 0x77416C77, + 0xBD2843BD, 0x32BC7532, 0xD47B37D4, 0x9B88269B, 0x700DFA70, 0xF94413F9, + 0xB1FB94B1, 0x5A7E485A, 0x7A03F27A, 0xE48CD0E4, 0x47B68B47, 0x3C24303C, + 0xA5E784A5, 0x416B5441, 0x06DDDF06, 0xC56023C5, 0x45FD1945, 0xA33A5BA3, + 0x68C23D68, 0x158D5915, 0x21ECF321, 0x3166AE31, 0x3E6FA23E, 0x16578216, + 0x95106395, 0x5BEF015B, 0x4DB8834D, 0x91862E91, 0xB56DD9B5, 0x1F83511F, + 0x53AA9B53, 0x635D7C63, 0x3B68A63B, 0x3FFEEB3F, 0xD630A5D6, 0x257ABE25, + 0xA7AC16A7, 0x0F090C0F, 0x35F0E335, 0x23A76123, 0xF090C0F0, 0xAFE98CAF, + 0x809D3A80, 0x925CF592, 0x810C7381, 0x27312C27, 0x76D02576, 0xE7560BE7, + 0x7B92BB7B, 0xE9CE4EE9, 0xF10189F1, 0x9F1E6B9F, 0xA93453A9, 0xC4F16AC4, + 0x99C3B499, 0x975BF197, 0x8347E183, 0x6B18E66B, 0xC822BDC8, 0x0E98450E, + 0x6E1FE26E, 0xC9B3F4C9, 0x2F74B62F, 0xCBF866CB, 0xFF99CCFF, 0xEA1495EA, + 0xED5803ED, 0xF7DC56F7, 0xE18BD4E1, 0x1B151C1B, 0xADA21EAD, 0x0CD3D70C, + 0x2BE2FB2B, 0x1DC8C31D, 0x195E8E19, 0xC22CB5C2, 0x8949E989, 0x12C1CF12, + 0x7E95BF7E, 0x207DBA20, 0x6411EA64, 0x840B7784, 0x6DC5396D, 0x6A89AF6A, + 0xD17C33D1, 0xA171C9A1, 0xCEFF62CE, 0x37BB7137, 0xFB0F81FB, 0x3DB5793D, + 0x51E10951, 0xDC3EADDC, 0x2D3F242D, 0xA476CDA4, 0x9D55F99D, 0xEE82D8EE, + 0x8640E586, 0xAE78C5AE, 0xCD25B9CD, 0x04964D04, 0x55774455, 0x0A0E080A, + 0x13508613, 0x30F7E730, 0xD337A1D3, 0x40FA1D40, 0x3461AA34, 0x8C4EED8C, + 0xB3B006B3, 0x6C54706C, 0x2A73B22A, 0x523BD252, 0x0B9F410B, 0x8B027B8B, + 0x88D8A088, 0x4FF3114F, 0x67CB3167, 0x4627C246, 0xC06727C0, 0xB4FC90B4, + 0x28382028, 0x7F04F67F, 0x78486078, 0x2EE5FF2E, 0x074C9607, 0x4B655C4B, + 0xC72BB1C7, 0x6F8EAB6F, 0x0D429E0D, 0xBBF59CBB, 0xF2DB52F2, 0xF34A1BF3, + 0xA63D5FA6, 0x59A49359, 0xBCB90ABC, 0x3AF9EF3A, 0xEF1391EF, 0xFE0885FE, + 0x01914901, 0x6116EE61, 0x7CDE2D7C, 0xB2214FB2, 0x42B18F42, 0xDB723BDB, + 0xB82F47B8, 0x48BF8748, 0x2CAE6D2C, 0xE3C046E3, 0x573CD657, 0x859A3E85, + 0x29A96929, 0x7D4F647D, 0x94812A94, 0x492ECE49, 0x17C6CB17, 0xCA692FCA, + 0xC3BDFCC3, 0x5CA3975C, 0x5EE8055E, 0xD0ED7AD0, 0x87D1AC87, 0x8E057F8E, + 0xBA64D5BA, 0xA8A51AA8, 0xB7264BB7, 0xB9BE0EB9, 0x6087A760, 0xF8D55AF8, + 0x22362822, 0x111B1411, 0xDE753FDE, 0x79D92979, 0xAAEE88AA, 0x332D3C33, + 0x5F794C5F, 0xB6B702B6, 0x96CAB896, 0x5835DA58, 0x9CC4B09C, 0xFC4317FC, + 0x1A84551A, 0xF64D1FF6, 0x1C598A1C, 0x38B27D38, 0xAC3357AC, 0x18CFC718, + 0xF4068DF4, 0x69537469, 0x749BB774, 0xF597C4F5, 0x56AD9F56, 0xDAE372DA, + 0xD5EA7ED5, 0x4AF4154A, 0x9E8F229E, 0xA2AB12A2, 0x4E62584E, 0xE85F07E8, + 0xE51D99E5, 0x39233439, 0xC1F66EC1, 0x446C5044, 0x5D32DE5D, 0x72466872, + 0x26A06526, 0x93CDBC93, 0x03DADB03, 0xC6BAF8C6, 0xFA9EC8FA, 0x82D6A882, + 0xCF6E2BCF, 0x50704050, 0xEB85DCEB, 0x750AFE75, 0x8A93328A, 0x8DDFA48D, + 0x4C29CA4C, 0x141C1014, 0x73D72173, 0xCCB4F0CC, 0x09D4D309, 0x108A5D10, + 0xE2510FE2, 0x00000000, 0x9A196F9A, 0xE01A9DE0, 0x8F94368F, 0xE6C742E6, + 0xECC94AEC, 0xFDD25EFD, 0xAB7FC1AB, 0xD8A8E0D8 }; + +} diff --git a/botan/src/block/twofish/twofish.cpp b/botan/src/block/twofish/twofish.cpp new file mode 100644 index 0000000..9784b00 --- /dev/null +++ b/botan/src/block/twofish/twofish.cpp @@ -0,0 +1,218 @@ +/* +* Twofish +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/twofish.h> +#include <botan/loadstor.h> +#include <botan/rotate.h> + +namespace Botan { + +/* +* Twofish Encryption +*/ +void Twofish::enc(const byte in[], byte out[]) const + { + u32bit A = load_le<u32bit>(in, 0) ^ round_key[0]; + u32bit B = load_le<u32bit>(in, 1) ^ round_key[1]; + u32bit C = load_le<u32bit>(in, 2) ^ round_key[2]; + u32bit D = load_le<u32bit>(in, 3) ^ round_key[3]; + + for(u32bit j = 0; j != 16; j += 2) + { + u32bit X, Y; + + X = SBox0[get_byte(3, A)] ^ SBox1[get_byte(2, A)] ^ + SBox2[get_byte(1, A)] ^ SBox3[get_byte(0, A)]; + Y = SBox0[get_byte(0, B)] ^ SBox1[get_byte(3, B)] ^ + SBox2[get_byte(2, B)] ^ SBox3[get_byte(1, B)]; + X += Y; + Y += X + round_key[2*j + 9]; + X += round_key[2*j + 8]; + + C = rotate_right(C ^ X, 1); + D = rotate_left(D, 1) ^ Y; + + X = SBox0[get_byte(3, C)] ^ SBox1[get_byte(2, C)] ^ + SBox2[get_byte(1, C)] ^ SBox3[get_byte(0, C)]; + Y = SBox0[get_byte(0, D)] ^ SBox1[get_byte(3, D)] ^ + SBox2[get_byte(2, D)] ^ SBox3[get_byte(1, D)]; + X += Y; + Y += X + round_key[2*j + 11]; + X += round_key[2*j + 10]; + + A = rotate_right(A ^ X, 1); + B = rotate_left(B, 1) ^ Y; + } + + C ^= round_key[4]; + D ^= round_key[5]; + A ^= round_key[6]; + B ^= round_key[7]; + + store_le(out, C, D, A, B); + } + +/* +* Twofish Decryption +*/ +void Twofish::dec(const byte in[], byte out[]) const + { + u32bit A = load_le<u32bit>(in, 0) ^ round_key[4]; + u32bit B = load_le<u32bit>(in, 1) ^ round_key[5]; + u32bit C = load_le<u32bit>(in, 2) ^ round_key[6]; + u32bit D = load_le<u32bit>(in, 3) ^ round_key[7]; + + for(u32bit j = 0; j != 16; j += 2) + { + u32bit X, Y; + + X = SBox0[get_byte(3, A)] ^ SBox1[get_byte(2, A)] ^ + SBox2[get_byte(1, A)] ^ SBox3[get_byte(0, A)]; + Y = SBox0[get_byte(0, B)] ^ SBox1[get_byte(3, B)] ^ + SBox2[get_byte(2, B)] ^ SBox3[get_byte(1, B)]; + X += Y; + Y += X + round_key[39 - 2*j]; + X += round_key[38 - 2*j]; + + C = rotate_left(C, 1) ^ X; + D = rotate_right(D ^ Y, 1); + + X = SBox0[get_byte(3, C)] ^ SBox1[get_byte(2, C)] ^ + SBox2[get_byte(1, C)] ^ SBox3[get_byte(0, C)]; + Y = SBox0[get_byte(0, D)] ^ SBox1[get_byte(3, D)] ^ + SBox2[get_byte(2, D)] ^ SBox3[get_byte(1, D)]; + X += Y; + Y += X + round_key[37 - 2*j]; + X += round_key[36 - 2*j]; + + A = rotate_left(A, 1) ^ X; + B = rotate_right(B ^ Y, 1); + } + + C ^= round_key[0]; + D ^= round_key[1]; + A ^= round_key[2]; + B ^= round_key[3]; + + store_le(out, C, D, A, B); + } + +/* +* Twofish Key Schedule +*/ +void Twofish::key_schedule(const byte key[], u32bit length) + { + SecureBuffer<byte, 16> S; + + for(u32bit j = 0; j != length; ++j) + rs_mul(S + 4*(j/8), key[j], j); + + if(length == 16) + { + for(u32bit j = 0; j != 256; ++j) + { + SBox0[j] = MDS0[Q0[Q0[j]^S[ 0]]^S[ 4]]; + SBox1[j] = MDS1[Q0[Q1[j]^S[ 1]]^S[ 5]]; + SBox2[j] = MDS2[Q1[Q0[j]^S[ 2]]^S[ 6]]; + SBox3[j] = MDS3[Q1[Q1[j]^S[ 3]]^S[ 7]]; + } + for(u32bit j = 0; j != 40; j += 2) + { + u32bit X = MDS0[Q0[Q0[j ]^key[ 8]]^key[ 0]] ^ + MDS1[Q0[Q1[j ]^key[ 9]]^key[ 1]] ^ + MDS2[Q1[Q0[j ]^key[10]]^key[ 2]] ^ + MDS3[Q1[Q1[j ]^key[11]]^key[ 3]]; + u32bit Y = MDS0[Q0[Q0[j+1]^key[12]]^key[ 4]] ^ + MDS1[Q0[Q1[j+1]^key[13]]^key[ 5]] ^ + MDS2[Q1[Q0[j+1]^key[14]]^key[ 6]] ^ + MDS3[Q1[Q1[j+1]^key[15]]^key[ 7]]; + Y = rotate_left(Y, 8); X += Y; Y += X; + round_key[j] = X; round_key[j+1] = rotate_left(Y, 9); + } + } + else if(length == 24) + { + for(u32bit j = 0; j != 256; ++j) + { + SBox0[j] = MDS0[Q0[Q0[Q1[j]^S[ 0]]^S[ 4]]^S[ 8]]; + SBox1[j] = MDS1[Q0[Q1[Q1[j]^S[ 1]]^S[ 5]]^S[ 9]]; + SBox2[j] = MDS2[Q1[Q0[Q0[j]^S[ 2]]^S[ 6]]^S[10]]; + SBox3[j] = MDS3[Q1[Q1[Q0[j]^S[ 3]]^S[ 7]]^S[11]]; + } + for(u32bit j = 0; j != 40; j += 2) + { + u32bit X = MDS0[Q0[Q0[Q1[j ]^key[16]]^key[ 8]]^key[ 0]] ^ + MDS1[Q0[Q1[Q1[j ]^key[17]]^key[ 9]]^key[ 1]] ^ + MDS2[Q1[Q0[Q0[j ]^key[18]]^key[10]]^key[ 2]] ^ + MDS3[Q1[Q1[Q0[j ]^key[19]]^key[11]]^key[ 3]]; + u32bit Y = MDS0[Q0[Q0[Q1[j+1]^key[20]]^key[12]]^key[ 4]] ^ + MDS1[Q0[Q1[Q1[j+1]^key[21]]^key[13]]^key[ 5]] ^ + MDS2[Q1[Q0[Q0[j+1]^key[22]]^key[14]]^key[ 6]] ^ + MDS3[Q1[Q1[Q0[j+1]^key[23]]^key[15]]^key[ 7]]; + Y = rotate_left(Y, 8); X += Y; Y += X; + round_key[j] = X; round_key[j+1] = rotate_left(Y, 9); + } + } + else if(length == 32) + { + for(u32bit j = 0; j != 256; ++j) + { + SBox0[j] = MDS0[Q0[Q0[Q1[Q1[j]^S[ 0]]^S[ 4]]^S[ 8]]^S[12]]; + SBox1[j] = MDS1[Q0[Q1[Q1[Q0[j]^S[ 1]]^S[ 5]]^S[ 9]]^S[13]]; + SBox2[j] = MDS2[Q1[Q0[Q0[Q0[j]^S[ 2]]^S[ 6]]^S[10]]^S[14]]; + SBox3[j] = MDS3[Q1[Q1[Q0[Q1[j]^S[ 3]]^S[ 7]]^S[11]]^S[15]]; + } + for(u32bit j = 0; j != 40; j += 2) + { + u32bit X = MDS0[Q0[Q0[Q1[Q1[j ]^key[24]]^key[16]]^key[ 8]]^key[ 0]] ^ + MDS1[Q0[Q1[Q1[Q0[j ]^key[25]]^key[17]]^key[ 9]]^key[ 1]] ^ + MDS2[Q1[Q0[Q0[Q0[j ]^key[26]]^key[18]]^key[10]]^key[ 2]] ^ + MDS3[Q1[Q1[Q0[Q1[j ]^key[27]]^key[19]]^key[11]]^key[ 3]]; + u32bit Y = MDS0[Q0[Q0[Q1[Q1[j+1]^key[28]]^key[20]]^key[12]]^key[ 4]] ^ + MDS1[Q0[Q1[Q1[Q0[j+1]^key[29]]^key[21]]^key[13]]^key[ 5]] ^ + MDS2[Q1[Q0[Q0[Q0[j+1]^key[30]]^key[22]]^key[14]]^key[ 6]] ^ + MDS3[Q1[Q1[Q0[Q1[j+1]^key[31]]^key[23]]^key[15]]^key[ 7]]; + Y = rotate_left(Y, 8); X += Y; Y += X; + round_key[j] = X; round_key[j+1] = rotate_left(Y, 9); + } + } + } + +/* +* Do one column of the RS matrix multiplcation +*/ +void Twofish::rs_mul(byte S[4], byte key, u32bit offset) + { + if(key) + { + byte X = POLY_TO_EXP[key - 1]; + + byte RS1 = RS[(4*offset ) % 32]; + byte RS2 = RS[(4*offset+1) % 32]; + byte RS3 = RS[(4*offset+2) % 32]; + byte RS4 = RS[(4*offset+3) % 32]; + + S[0] ^= EXP_TO_POLY[(X + POLY_TO_EXP[RS1 - 1]) % 255]; + S[1] ^= EXP_TO_POLY[(X + POLY_TO_EXP[RS2 - 1]) % 255]; + S[2] ^= EXP_TO_POLY[(X + POLY_TO_EXP[RS3 - 1]) % 255]; + S[3] ^= EXP_TO_POLY[(X + POLY_TO_EXP[RS4 - 1]) % 255]; + } + } + +/* +* Clear memory of sensitive data +*/ +void Twofish::clear() throw() + { + SBox0.clear(); + SBox1.clear(); + SBox2.clear(); + SBox3.clear(); + round_key.clear(); + } + +} diff --git a/botan/src/block/twofish/twofish.h b/botan/src/block/twofish/twofish.h new file mode 100644 index 0000000..0640e32 --- /dev/null +++ b/botan/src/block/twofish/twofish.h @@ -0,0 +1,48 @@ +/* +* Twofish +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_TWOFISH_H__ +#define BOTAN_TWOFISH_H__ + +#include <botan/block_cipher.h> + +namespace Botan { + +/* +* Twofish +*/ +class BOTAN_DLL Twofish : public BlockCipher + { + public: + void clear() throw(); + std::string name() const { return "Twofish"; } + BlockCipher* clone() const { return new Twofish; } + Twofish() : BlockCipher(16, 16, 32, 8) {} + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key_schedule(const byte[], u32bit); + + static void rs_mul(byte[4], byte, u32bit); + + static const u32bit MDS0[256]; + static const u32bit MDS1[256]; + static const u32bit MDS2[256]; + static const u32bit MDS3[256]; + static const byte Q0[256]; + static const byte Q1[256]; + static const byte RS[32]; + static const byte EXP_TO_POLY[255]; + static const byte POLY_TO_EXP[255]; + + SecureBuffer<u32bit, 256> SBox0, SBox1, SBox2, SBox3; + SecureBuffer<u32bit, 40> round_key; + }; + +} + +#endif diff --git a/botan/src/block/xtea/info.txt b/botan/src/block/xtea/info.txt new file mode 100644 index 0000000..0120a3b --- /dev/null +++ b/botan/src/block/xtea/info.txt @@ -0,0 +1,10 @@ +realname "XTEA" + +define XTEA + +load_on auto + +<add> +xtea.cpp +xtea.h +</add> diff --git a/botan/src/block/xtea/xtea.cpp b/botan/src/block/xtea/xtea.cpp new file mode 100644 index 0000000..5047f65 --- /dev/null +++ b/botan/src/block/xtea/xtea.cpp @@ -0,0 +1,64 @@ +/* +* XTEA +* (C) 1999-2009 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/xtea.h> +#include <botan/loadstor.h> +#include <botan/parsing.h> + +namespace Botan { + +/* +* XTEA Encryption +*/ +void XTEA::enc(const byte in[], byte out[]) const + { + u32bit L = load_be<u32bit>(in, 0), R = load_be<u32bit>(in, 1); + + for(u32bit j = 0; j != 32; ++j) + { + L += (((R << 4) ^ (R >> 5)) + R) ^ EK[2*j]; + R += (((L << 4) ^ (L >> 5)) + L) ^ EK[2*j+1]; + } + + store_be(out, L, R); + } + +/* +* XTEA Decryption +*/ +void XTEA::dec(const byte in[], byte out[]) const + { + u32bit L = load_be<u32bit>(in, 0), R = load_be<u32bit>(in, 1); + + for(u32bit j = 0; j != 32; ++j) + { + R -= (((L << 4) ^ (L >> 5)) + L) ^ EK[63 - 2*j]; + L -= (((R << 4) ^ (R >> 5)) + R) ^ EK[62 - 2*j]; + } + + store_be(out, L, R); + } + +/* +* XTEA Key Schedule +*/ +void XTEA::key_schedule(const byte key[], u32bit) + { + SecureBuffer<u32bit, 4> UK; + for(u32bit i = 0; i != 4; ++i) + UK[i] = load_be<u32bit>(key, i); + + u32bit D = 0; + for(u32bit i = 0; i != 64; i += 2) + { + EK[i ] = D + UK[D % 4]; + D += 0x9E3779B9; + EK[i+1] = D + UK[(D >> 11) % 4]; + } + } + +} diff --git a/botan/src/block/xtea/xtea.h b/botan/src/block/xtea/xtea.h new file mode 100644 index 0000000..d9c6066 --- /dev/null +++ b/botan/src/block/xtea/xtea.h @@ -0,0 +1,34 @@ +/* +* XTEA +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_XTEA_H__ +#define BOTAN_XTEA_H__ + +#include <botan/block_cipher.h> + +namespace Botan { + +/* +* XTEA +*/ +class BOTAN_DLL XTEA : public BlockCipher + { + public: + void clear() throw() { EK.clear(); } + std::string name() const { return "XTEA"; } + BlockCipher* clone() const { return new XTEA; } + XTEA() : BlockCipher(8, 16) {} + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key_schedule(const byte[], u32bit); + SecureBuffer<u32bit, 64> EK; + }; + +} + +#endif diff --git a/botan/src/build-data/arch/alpha.txt b/botan/src/build-data/arch/alpha.txt new file mode 100644 index 0000000..60b2643 --- /dev/null +++ b/botan/src/build-data/arch/alpha.txt @@ -0,0 +1,31 @@ +realname "DEC Alpha" + +default_submodel alpha-ev4 +endian little + +<aliases> +axp +alphaaxp +</aliases> + +<submodels> +alpha-ev4 +alpha-ev5 +alpha-ev56 +alpha-pca56 +alpha-ev6 +alpha-ev67 +alpha-ev68 +alpha-ev7 +</submodels> + +<submodel_aliases> +alphaev4 -> alpha-ev4 +alphaev5 -> alpha-ev5 +alphaev56 -> alpha-ev56 +alphapca56 -> alpha-pca56 +alphaev6 -> alpha-ev6 +alphaev67 -> alpha-ev67 +alphaev68 -> alpha-ev68 +alphaev7 -> alpha-ev7 +</submodel_aliases> diff --git a/botan/src/build-data/arch/amd64.txt b/botan/src/build-data/arch/amd64.txt new file mode 100644 index 0000000..216588e --- /dev/null +++ b/botan/src/build-data/arch/amd64.txt @@ -0,0 +1,27 @@ +realname "x86-64" + +default_submodel opteron + +endian little +unaligned ok + +<aliases> +x86-64 +x86_64 # for RPM +</aliases> + +<submodels> +opteron +em64t +core2 +</submodels> + +<submodel_aliases> +core2duo -> core2 +intelcore2 -> core2 +intelcore2duo -> core2 + +amdopteron -> opteron +athlon64 -> opteron +k8 -> opteron +</submodel_aliases> diff --git a/botan/src/build-data/arch/arm.txt b/botan/src/build-data/arch/arm.txt new file mode 100644 index 0000000..c6be4ad --- /dev/null +++ b/botan/src/build-data/arch/arm.txt @@ -0,0 +1,23 @@ +realname "ARM" + +default_submodel arm2 + +<submodels> +arm2 +arm3 +arm6 +arm7 +arm8 +arm9 +strongarm +strongarm110 +strongarm1100 +xscale +</submodels> + +<submodel_aliases> +sa110 -> strongarm110 +sa1100 -> strongarm1100 +strongarm1110 -> strongarm1100 +armv5tel -> xscale +</submodel_aliases> diff --git a/botan/src/build-data/arch/hitachi-sh.txt b/botan/src/build-data/arch/hitachi-sh.txt new file mode 100644 index 0000000..8e9f7ee --- /dev/null +++ b/botan/src/build-data/arch/hitachi-sh.txt @@ -0,0 +1,11 @@ +realname "Hitachi SH" + +default_submodel hitachi-sh1 + +<submodels> +hitachi-sh1 +hitachi-sh2 +hitachi-sh3 +hitachi-sh3e +hitachi-sh4 +</submodels> diff --git a/botan/src/build-data/arch/hppa.txt b/botan/src/build-data/arch/hppa.txt new file mode 100644 index 0000000..4cdd408 --- /dev/null +++ b/botan/src/build-data/arch/hppa.txt @@ -0,0 +1,17 @@ +realname "HP-PA" + +default_submodel hppa1.0 + +<aliases> +hp-pa +parisc +pa-risc +hp-parisc +hp-pa-risc +</aliases> + +<submodels> +hppa1.0 +hppa1.1 +hppa2.0 +</submodels> diff --git a/botan/src/build-data/arch/ia32.txt b/botan/src/build-data/arch/ia32.txt new file mode 100644 index 0000000..aafcf9a --- /dev/null +++ b/botan/src/build-data/arch/ia32.txt @@ -0,0 +1,60 @@ +realname "IA-32" + +default_submodel i386 + +endian little +unaligned ok + +<aliases> +x86 +ix86 +80x86 +i86pc # for Solaris +</aliases> + +<submodels> +i386 +i486 +i586 +i686 +pentium2 +pentium3 +pentium4 +pentium-m +prescott +k6 +athlon +</submodels> + +<submodel_aliases> +pentium -> i586 +pentiumpro -> i686 +pentium_pro -> i686 +pentium2 -> pentium2 +pentiumii -> pentium2 +pentium3 -> pentium3 +pentiumiii -> pentium3 + +x86family5 -> i586 +x86family6 -> i686 + +pentiumm -> pentium-m +intelpentiumm -> pentium-m + +intelpentium4 -> pentium4 + +duron -> athlon +athlon-xp -> athlon +k7 -> athlon + +p2 -> i686 +p3 -> i686 +p4 -> pentium4 + +intelcput2250 -> prescott +intelcput2300 -> prescott +intelcput2400 -> prescott +intelcput2500 -> prescott +intelcput2600 -> prescott +intelcput2700 -> prescott +</submodel_aliases> diff --git a/botan/src/build-data/arch/ia64.txt b/botan/src/build-data/arch/ia64.txt new file mode 100644 index 0000000..7ca84c0 --- /dev/null +++ b/botan/src/build-data/arch/ia64.txt @@ -0,0 +1,15 @@ +realname "IA-64" + +# This is safe: only affects tuning, not ISA +default_submodel itanium2 + +<aliases> +merced +itanium +</aliases> + +<submodels> +itanium1 +itanium2 +mckinley +</submodels> diff --git a/botan/src/build-data/arch/m68k.txt b/botan/src/build-data/arch/m68k.txt new file mode 100644 index 0000000..27f246a --- /dev/null +++ b/botan/src/build-data/arch/m68k.txt @@ -0,0 +1,15 @@ +realname "Motorola 680x0" + +default_submodel 68020 + +<aliases> +680x0 +68k +</aliases> + +<submodels> +68020 +68030 +68040 +68060 +</submodels> diff --git a/botan/src/build-data/arch/mips32.txt b/botan/src/build-data/arch/mips32.txt new file mode 100644 index 0000000..9846c8f --- /dev/null +++ b/botan/src/build-data/arch/mips32.txt @@ -0,0 +1,22 @@ +realname "MIPS" + +default_submodel r3000 + +<aliases> +mips +mipsel # For Debian +</aliases> + +<submodels> +r3000 +r6000 +</submodels> + +<submodel_aliases> +r3k -> r3000 +r6k -> r6000 + +# These are for RPM +mipsbe -> r3000 +mipsle -> r3000 +</submodel_aliases> diff --git a/botan/src/build-data/arch/mips64.txt b/botan/src/build-data/arch/mips64.txt new file mode 100644 index 0000000..dbb49d0 --- /dev/null +++ b/botan/src/build-data/arch/mips64.txt @@ -0,0 +1,22 @@ +realname "MIPS64" + +default_submodel r4400 + +<submodels> +r4000 +r4100 +r4300 +r4400 +r4600 +r4560 +r5000 +r8000 +r10000 +</submodels> + +<submodel_aliases> +r4k -> r4000 +r5k -> r5000 +r8k -> r8000 +r10k -> r10000 +</submodel_aliases> diff --git a/botan/src/build-data/arch/ppc.txt b/botan/src/build-data/arch/ppc.txt new file mode 100644 index 0000000..16112f3 --- /dev/null +++ b/botan/src/build-data/arch/ppc.txt @@ -0,0 +1,24 @@ +realname "PowerPC" + +endian big + +default_submodel ppc604 + +<aliases> +powerpc +</aliases> + +<submodel_aliases> +g3 -> ppc740 +g4 -> ppc7450 +</submodel_aliases> + +<submodels> +ppc601 +ppc603 +ppc604 +ppc740 +ppc750 +ppc7400 +ppc7450 +</submodels> diff --git a/botan/src/build-data/arch/ppc64.txt b/botan/src/build-data/arch/ppc64.txt new file mode 100644 index 0000000..7c8944f --- /dev/null +++ b/botan/src/build-data/arch/ppc64.txt @@ -0,0 +1,26 @@ +realname "PowerPC 64" + +endian big + +default_submodel power4 + +<aliases> +powerpc64 +</aliases> + +<submodel_aliases> +g5 -> ppc970 +</submodel_aliases> + +<submodels> +rs64a +ppc970 +power3 +power4 +power5 +cellppu +</submodels> + +<submodel_aliases> +cellbroadbandengine -> cellppu +</submodel_aliases> diff --git a/botan/src/build-data/arch/s390.txt b/botan/src/build-data/arch/s390.txt new file mode 100644 index 0000000..392f513 --- /dev/null +++ b/botan/src/build-data/arch/s390.txt @@ -0,0 +1,7 @@ +realname "S/390 31-bit" + +default_submodel s390 + +<submodels> +s390 +</submodels> diff --git a/botan/src/build-data/arch/s390x.txt b/botan/src/build-data/arch/s390x.txt new file mode 100644 index 0000000..49fb0bd --- /dev/null +++ b/botan/src/build-data/arch/s390x.txt @@ -0,0 +1,7 @@ +realname "S/390 64-bit" + +default_submodel s390x + +<submodels> +s390x +</submodels> diff --git a/botan/src/build-data/arch/sparc32.txt b/botan/src/build-data/arch/sparc32.txt new file mode 100644 index 0000000..6b752df --- /dev/null +++ b/botan/src/build-data/arch/sparc32.txt @@ -0,0 +1,34 @@ +realname "SPARC" + +# V7 doesn't have integer multiply, so it will be bitterly slow for some things +# (especially BigInt). Also, it's fairly rare nowadays, so we default to V8. +default_submodel sparc32-v8 + +endian big + +<aliases> +sparc +</aliases> + +<submodels> +sparc32-v7 +sparc32-v8 +sparc32-v9 +</submodels> + +<submodel_aliases> +cypress -> sparc32-v7 +supersparc -> sparc32-v8 +hypersparc -> sparc32-v8 +microsparc -> sparc32-v8 +sparclite -> sparc32-v8 + +sparcv7 -> sparc32-v7 +sparcv8 -> sparc32-v8 +sparcv9 -> sparc32-v9 + +sparc-v7 -> sparc32-v7 +sparc-v8 -> sparc32-v8 +sparc-v9 -> sparc32-v9 +</submodel_aliases> + diff --git a/botan/src/build-data/arch/sparc64.txt b/botan/src/build-data/arch/sparc64.txt new file mode 100644 index 0000000..c0575ef --- /dev/null +++ b/botan/src/build-data/arch/sparc64.txt @@ -0,0 +1,15 @@ +realname "SPARC64" + +default_submodel sparc64-ultra + +<submodels> +sparc64-ultra +sparc64-ultra2 +sparc64-ultra3 +</submodels> + +<submodel_aliases> +ultrasparc -> sparc64-ultra +ultrasparc2 -> sparc64-ultra2 +ultrasparc3 -> sparc64-ultra3 +</submodel_aliases> diff --git a/botan/src/build-data/botan-config.in b/botan/src/build-data/botan-config.in new file mode 100644 index 0000000..f3fa3db --- /dev/null +++ b/botan/src/build-data/botan-config.in @@ -0,0 +1,69 @@ +#!/bin/sh + +# For normal builds: +guess_prefix=`dirname \`dirname $0\`` +includedir=%{includedir} +libdir=%{libdir} + +# For workspace builds: +#guess_prefix=`dirname $0` +#includedir=build/include +#libdir= + +install_prefix=%{prefix} +prefix= + +usage() +{ + echo "$0 [--prefix[=DIR]] [--version] [--libs] [--cflags]" + exit 1 +} + +if test $# -eq 0; then + usage +fi + +if test `echo $guess_prefix | cut -c 1` = "/"; then + prefix=$guess_prefix +else + prefix=$install_prefix +fi + +while test $# -gt 0; do + case "$1" in + -*=*) optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'` ;; + *) optarg= ;; + esac + case "$1" in + --prefix=*) + prefix=$optarg + ;; + --prefix) + echo $prefix + ;; + --version) + echo %{version} + exit 0 + ;; + --cflags) + if [ $prefix != "/usr" -a $prefix != "/usr/local" ] + then + echo -I$prefix/$includedir + fi + ;; + --libs) + if [ $prefix != "/usr" -a $prefix != "/usr/local" ] + then + echo -L$prefix/$libdir -lbotan %{link_to} + else + echo -lbotan %{link_to} + fi + ;; + *) + usage + ;; + esac + shift +done + +exit 0 diff --git a/botan/src/build-data/botan.doxy.in b/botan/src/build-data/botan.doxy.in new file mode 100644 index 0000000..87d6e58 --- /dev/null +++ b/botan/src/build-data/botan.doxy.in @@ -0,0 +1,233 @@ +# Doxyfile 1.5.4 + +#--------------------------------------------------------------------------- +# Project related configuration options +#--------------------------------------------------------------------------- +DOXYFILE_ENCODING = UTF-8 +PROJECT_NAME = Botan +PROJECT_NUMBER = %{version} +OUTPUT_DIRECTORY = doc/doxygen +CREATE_SUBDIRS = NO +OUTPUT_LANGUAGE = English +BRIEF_MEMBER_DESC = YES +REPEAT_BRIEF = YES +ABBREVIATE_BRIEF = +ALWAYS_DETAILED_SEC = NO +INLINE_INHERITED_MEMB = NO +FULL_PATH_NAMES = YES +STRIP_FROM_PATH = +STRIP_FROM_INC_PATH = +SHORT_NAMES = NO +JAVADOC_AUTOBRIEF = NO +QT_AUTOBRIEF = NO +MULTILINE_CPP_IS_BRIEF = NO +DETAILS_AT_TOP = NO +INHERIT_DOCS = YES +SEPARATE_MEMBER_PAGES = NO +TAB_SIZE = 8 +ALIASES = +OPTIMIZE_OUTPUT_FOR_C = NO +OPTIMIZE_OUTPUT_JAVA = NO +BUILTIN_STL_SUPPORT = NO +CPP_CLI_SUPPORT = NO +SIP_SUPPORT = NO +DISTRIBUTE_GROUP_DOC = NO +SUBGROUPING = YES +TYPEDEF_HIDES_STRUCT = NO +#--------------------------------------------------------------------------- +# Build related configuration options +#--------------------------------------------------------------------------- +EXTRACT_ALL = YES +EXTRACT_PRIVATE = NO +EXTRACT_STATIC = NO +EXTRACT_LOCAL_CLASSES = YES +EXTRACT_LOCAL_METHODS = NO +EXTRACT_ANON_NSPACES = NO +HIDE_UNDOC_MEMBERS = NO +HIDE_UNDOC_CLASSES = NO +HIDE_FRIEND_COMPOUNDS = NO +HIDE_IN_BODY_DOCS = NO +INTERNAL_DOCS = NO +CASE_SENSE_NAMES = YES +HIDE_SCOPE_NAMES = NO +SHOW_INCLUDE_FILES = YES +INLINE_INFO = YES +SORT_MEMBER_DOCS = YES +SORT_BRIEF_DOCS = YES +SORT_BY_SCOPE_NAME = NO +GENERATE_TODOLIST = YES +GENERATE_TESTLIST = YES +GENERATE_BUGLIST = YES +GENERATE_DEPRECATEDLIST= YES +ENABLED_SECTIONS = +MAX_INITIALIZER_LINES = 30 +SHOW_USED_FILES = YES +SHOW_DIRECTORIES = NO +FILE_VERSION_FILTER = +#--------------------------------------------------------------------------- +# configuration options related to warning and progress messages +#--------------------------------------------------------------------------- +QUIET = YES +WARNINGS = YES +WARN_IF_UNDOCUMENTED = NO +WARN_IF_DOC_ERROR = YES +WARN_NO_PARAMDOC = NO +WARN_FORMAT = "$file:$line: $text" +WARN_LOGFILE = +#--------------------------------------------------------------------------- +# configuration options related to the input files +#--------------------------------------------------------------------------- +INPUT = src +INPUT_ENCODING = UTF-8 +FILE_PATTERNS = +RECURSIVE = YES +EXCLUDE = +EXCLUDE_SYMLINKS = NO +EXCLUDE_PATTERNS = +EXCLUDE_SYMBOLS = +EXAMPLE_PATH = +EXAMPLE_PATTERNS = +EXAMPLE_RECURSIVE = NO +IMAGE_PATH = +INPUT_FILTER = +FILTER_PATTERNS = +FILTER_SOURCE_FILES = NO +#--------------------------------------------------------------------------- +# configuration options related to source browsing +#--------------------------------------------------------------------------- +SOURCE_BROWSER = YES +INLINE_SOURCES = YES +STRIP_CODE_COMMENTS = NO +REFERENCED_BY_RELATION = YES +REFERENCES_RELATION = YES +REFERENCES_LINK_SOURCE = YES +USE_HTAGS = NO +VERBATIM_HEADERS = YES +#--------------------------------------------------------------------------- +# configuration options related to the alphabetical class index +#--------------------------------------------------------------------------- +ALPHABETICAL_INDEX = YES +COLS_IN_ALPHA_INDEX = 5 +IGNORE_PREFIX = +#--------------------------------------------------------------------------- +# configuration options related to the HTML output +#--------------------------------------------------------------------------- +GENERATE_HTML = YES +HTML_OUTPUT = html +HTML_FILE_EXTENSION = .html +HTML_HEADER = +HTML_FOOTER = +HTML_STYLESHEET = +HTML_ALIGN_MEMBERS = YES +GENERATE_HTMLHELP = NO +HTML_DYNAMIC_SECTIONS = NO +CHM_FILE = +HHC_LOCATION = +GENERATE_CHI = NO +BINARY_TOC = NO +TOC_EXPAND = NO +DISABLE_INDEX = NO +ENUM_VALUES_PER_LINE = 4 +GENERATE_TREEVIEW = NO +TREEVIEW_WIDTH = 250 +#--------------------------------------------------------------------------- +# configuration options related to the LaTeX output +#--------------------------------------------------------------------------- +GENERATE_LATEX = NO +LATEX_OUTPUT = latex +LATEX_CMD_NAME = latex +MAKEINDEX_CMD_NAME = makeindex +COMPACT_LATEX = NO +PAPER_TYPE = a4wide +EXTRA_PACKAGES = +LATEX_HEADER = +PDF_HYPERLINKS = NO +USE_PDFLATEX = NO +LATEX_BATCHMODE = NO +LATEX_HIDE_INDICES = NO +#--------------------------------------------------------------------------- +# configuration options related to the RTF output +#--------------------------------------------------------------------------- +GENERATE_RTF = NO +RTF_OUTPUT = rtf +COMPACT_RTF = NO +RTF_HYPERLINKS = NO +RTF_STYLESHEET_FILE = +RTF_EXTENSIONS_FILE = +#--------------------------------------------------------------------------- +# configuration options related to the man page output +#--------------------------------------------------------------------------- +GENERATE_MAN = NO +MAN_OUTPUT = man +MAN_EXTENSION = .3 +MAN_LINKS = NO +#--------------------------------------------------------------------------- +# configuration options related to the XML output +#--------------------------------------------------------------------------- +GENERATE_XML = NO +XML_OUTPUT = xml +XML_SCHEMA = +XML_DTD = +XML_PROGRAMLISTING = YES +#--------------------------------------------------------------------------- +# configuration options for the AutoGen Definitions output +#--------------------------------------------------------------------------- +GENERATE_AUTOGEN_DEF = NO +#--------------------------------------------------------------------------- +# configuration options related to the Perl module output +#--------------------------------------------------------------------------- +GENERATE_PERLMOD = NO +PERLMOD_LATEX = NO +PERLMOD_PRETTY = YES +PERLMOD_MAKEVAR_PREFIX = +#--------------------------------------------------------------------------- +# Configuration options related to the preprocessor +#--------------------------------------------------------------------------- +ENABLE_PREPROCESSING = YES +MACRO_EXPANSION = NO +EXPAND_ONLY_PREDEF = NO +SEARCH_INCLUDES = YES +INCLUDE_PATH = +INCLUDE_FILE_PATTERNS = +PREDEFINED = +EXPAND_AS_DEFINED = +SKIP_FUNCTION_MACROS = YES +#--------------------------------------------------------------------------- +# Configuration::additions related to external references +#--------------------------------------------------------------------------- +TAGFILES = +GENERATE_TAGFILE = +ALLEXTERNALS = NO +EXTERNAL_GROUPS = YES +PERL_PATH = /usr/bin/perl +#--------------------------------------------------------------------------- +# Configuration options related to the dot tool +#--------------------------------------------------------------------------- +CLASS_DIAGRAMS = YES +MSCGEN_PATH = +HIDE_UNDOC_RELATIONS = YES +HAVE_DOT = NO +CLASS_GRAPH = YES +COLLABORATION_GRAPH = YES +GROUP_GRAPHS = YES +UML_LOOK = NO +TEMPLATE_RELATIONS = NO +INCLUDE_GRAPH = YES +INCLUDED_BY_GRAPH = YES +CALL_GRAPH = NO +CALLER_GRAPH = NO +GRAPHICAL_HIERARCHY = YES +DIRECTORY_GRAPH = YES +DOT_IMAGE_FORMAT = png +DOT_PATH = +DOTFILE_DIRS = +DOT_GRAPH_MAX_NODES = 50 +MAX_DOT_GRAPH_DEPTH = 0 +DOT_TRANSPARENT = YES +DOT_MULTI_TARGETS = NO +GENERATE_LEGEND = YES +DOT_CLEANUP = YES +#--------------------------------------------------------------------------- +# Configuration::additions related to the search engine +#--------------------------------------------------------------------------- +SEARCHENGINE = NO diff --git a/botan/src/build-data/botan.pc.in b/botan/src/build-data/botan.pc.in new file mode 100644 index 0000000..70ed65d --- /dev/null +++ b/botan/src/build-data/botan.pc.in @@ -0,0 +1,12 @@ +prefix=%{prefix} +exec_prefix=${prefix} +libdir=${prefix}/%{libdir} +includedir=${prefix}/include + +Name: Botan +Description: Multi-platform C++ crypto library +Version: %{version} + +Libs: -L${libdir} -lbotan +Libs.private: %{link_to} +Cflags: -I${includedir} diff --git a/botan/src/build-data/buildh.in b/botan/src/build-data/buildh.in new file mode 100644 index 0000000..e4ab0f4 --- /dev/null +++ b/botan/src/build-data/buildh.in @@ -0,0 +1,66 @@ + +#ifndef BOTAN_BUILD_CONFIG_H__ +#define BOTAN_BUILD_CONFIG_H__ + +/* This file was automatically generated %{timestamp} UTC */ + +#define BOTAN_VERSION_MAJOR %{version_major} +#define BOTAN_VERSION_MINOR %{version_minor} +#define BOTAN_VERSION_PATCH %{version_patch} + +#ifndef BOTAN_DLL + #define BOTAN_DLL %{dll_export_flags} +#endif + +/* Chunk sizes */ +#define BOTAN_DEFAULT_BUFFER_SIZE 4096 +#define BOTAN_MEM_POOL_CHUNK_SIZE 64*1024 + +/* BigInt toggles */ +#define BOTAN_MP_WORD_BITS %{mp_bits} +#define BOTAN_KARAT_MUL_THRESHOLD 32 +#define BOTAN_KARAT_SQR_THRESHOLD 32 +#define BOTAN_PRIVATE_KEY_OP_BLINDING_BITS 64 + +/* PK key consistency checking toggles */ +#define BOTAN_PUBLIC_KEY_STRONG_CHECKS_ON_LOAD 1 +#define BOTAN_PRIVATE_KEY_STRONG_CHECKS_ON_LOAD 1 +#define BOTAN_PRIVATE_KEY_STRONG_CHECKS_ON_GENERATE 1 + +/* Should we use GCC-style inline assembler? */ +#if !defined(BOTAN_USE_GCC_INLINE_ASM) && defined(__GNUG__) + #define BOTAN_USE_GCC_INLINE_ASM 1 +#endif + +#ifndef BOTAN_USE_GCC_INLINE_ASM + #define BOTAN_USE_GCC_INLINE_ASM 0 +#endif + +/* Target identification and feature test macros */ +%{target_os_defines} + +%{target_cpu_defines} + +%{target_compiler_defines} + +/* Module definitions */ +%{module_defines} + +/* Local configuration options */ +%{local_config} + +/* +%{user}@%{hostname} ran '%{command_line}' + +Target +------- +Compiler: %{cc} %{lib_opt} %{mach_opt} +Arch: %{submodel}/%{arch} +OS: %{os} + +Modules +------- +%{mod_list} +*/ + +#endif diff --git a/botan/src/build-data/cc/bcc.txt b/botan/src/build-data/cc/bcc.txt new file mode 100644 index 0000000..df09daf --- /dev/null +++ b/botan/src/build-data/cc/bcc.txt @@ -0,0 +1,31 @@ +realname "Borland C++" + +binary_name "bcc32" + +compile_option "-c " +output_to_option "-o " +add_include_dir_option "-I" +add_lib_dir_option "-L" +add_lib_option "-l" + +lib_opt_flags "-O2" +check_opt_flags "-O2" +debug_flags "" +no_debug_flags "" +lang_flags "-tWR -tWM -tWC" +warning_flags "-w" + +dll_import_flags "" +dll_export_flags "" + +ar_command "tlib /C /P256" + +makefile_style nmake + +<mach_opt> +i486 -> "/G4" +i586 -> "/G5" +i686 -> "/G6" +athlon -> "/G6" +pentium4 -> "/G6" +</mach_opt> diff --git a/botan/src/build-data/cc/compaq.txt b/botan/src/build-data/cc/compaq.txt new file mode 100644 index 0000000..66d3a52 --- /dev/null +++ b/botan/src/build-data/cc/compaq.txt @@ -0,0 +1,29 @@ +realname "Compaq C++" + +binary_name "cxx" + +compile_option "-c " +output_to_option "-o " +add_include_dir_option "-I" +add_lib_dir_option "-L" +add_lib_option "-l" + +# -O3 and up seem to be unhappy with Botan +lib_opt_flags "-O2" +check_opt_flags "-O2" +debug_flags "-g" +no_debug_flags "" +lang_flags "-std ansi -D__USE_STD_IOSTREAM" + +dll_import_flags "" +dll_export_flags "" + +makefile_style unix + +<so_link_flags> +default -> "$(CXX) -shared -soname $(SONAME)" +</so_link_flags> + +<mach_opt> +alpha -> "-arch=SUBMODEL" alpha- +</mach_opt> diff --git a/botan/src/build-data/cc/ekopath.txt b/botan/src/build-data/cc/ekopath.txt new file mode 100644 index 0000000..ca24710 --- /dev/null +++ b/botan/src/build-data/cc/ekopath.txt @@ -0,0 +1,40 @@ +realname "PathScale EKOPath C++" + +binary_name "pathCC" + +compile_option "-c " +output_to_option "-o " +add_include_dir_option "-I" +add_lib_dir_option "-L" +add_lib_option "-l" + +lib_opt_flags "-O3 -OPT:Ofast:alias=disjoint" +check_opt_flags "-O2" +lang_flags "-D_REENTRANT -ansi -Wno-long-long" +warning_flags "-W -Wall" + +ar_command "pathCC -ar -o" + +shared_flags "-fPIC" +debug_flags "-g" + +dll_import_flags "" +dll_export_flags "" + +makefile_style unix + +<so_link_flags> +default -> "$(CXX) -shared -fPIC -Wl,-soname,$(SONAME)" +</so_link_flags> + +<mach_opt> +athlon -> "-mcpu=athlon" +pentium4 -> "-mcpu=pentium4" + +opteron -> "-mcpu=opteron" +em64t -> "-mcpu=em64t" +core2 -> "-mcpu=core" + +ia32 -> "-mcpu=anyx86" +amd64 -> "-mcpu=athlon64" +</mach_opt> diff --git a/botan/src/build-data/cc/gcc.txt b/botan/src/build-data/cc/gcc.txt new file mode 100644 index 0000000..978ed6d --- /dev/null +++ b/botan/src/build-data/cc/gcc.txt @@ -0,0 +1,89 @@ +realname "GNU C++" + +binary_name "g++" + +compiler_has_tr1 yes + +compile_option "-c " +output_to_option "-o " +add_include_dir_option "-I" +add_lib_dir_option "-L" +add_lib_option "-l" + +lang_flags "-D_REENTRANT -ansi -Wno-long-long" +warning_flags "-W -Wall" +#warning_flags "-Wextra -Wall -Wstrict-aliasing -Wstrict-overflow=5 -Wcast-align -Wmissing-declarations -Wno-unused-parameter" + +lib_opt_flags "-O2 -finline-functions" +check_opt_flags "-O2" +shared_flags "-fPIC" +debug_flags "-g" + +dll_import_flags "" +dll_export_flags "" + +makefile_style unix + +<so_link_flags> +# The default works for GNU ld and several other Unix linkers +default -> "$(CXX) -shared -fPIC -Wl,-soname,$(SONAME)" + +# AIX doesn't seem to have soname support (weird...) +aix -> "$(CXX) -shared -fPIC" + +darwin -> "$(CXX) -dynamiclib -fPIC -install_name $(SONAME)" +hpux -> "$(CXX) -shared -fPIC -Wl,+h,$(SONAME)" +solaris -> "$(CXX) -shared -fPIC -Wl,-h,$(SONAME)" +# Gotta use ld directly on BeOS, their GCC is busted +beos -> "ld -shared -h $(SONAME)" +</so_link_flags> + +<mach_opt> +# Specializations first (they don't need to be, just clearer) +i386 -> "-mtune=i686 -momit-leaf-frame-pointer" +r10000 -> "-mips4" +alpha-ev67 -> "-mcpu=ev6" # FIXME: GCC 3.1 and on has -march=ev67 +alpha-ev68 -> "-mcpu=ev6" +alpha-ev7 -> "-mcpu=ev6" +ppc601 -> "-mpowerpc -mcpu=601" +sparc64-ultra3 -> "-mcpu=v9 -mtune=ultrasparc3" + +em64t -> "-march=nocona -momit-leaf-frame-pointer" +cellppu -> "" + +# Default family options (SUBMODEL is substitued with the real submodel) +# Anything after the quotes is what should be *removed* from the submodel name +# before it's put into SUBMODEL. +alpha -> "-mcpu=SUBMODEL" alpha- +amd64 -> "-march=SUBMODEL -momit-leaf-frame-pointer" +arm -> "-mcpu=SUBMODEL" +ia32 -> "-march=SUBMODEL -momit-leaf-frame-pointer" +ia64 -> "-mtune=SUBMODEL" +hppa -> "-march=SUBMODEL" hppa +m68k -> "-mSUBMODEL" +hitachi-sh -> "-mSUBMODEL" hitachi-sh +sparc32 -> "-mcpu=SUBMODEL -Wa,-xarch=v8plus" sparc32- +sparc64 -> "-mcpu=v9 -mtune=ultrasparc" +mips32 -> "-mips1 -mcpu=SUBMODEL" mips32- +mips64 -> "-mips3 -mcpu=SUBMODEL" mips64- +ppc -> "-mcpu=SUBMODEL" ppc +ppc64 -> "-mcpu=SUBMODEL" ppc +</mach_opt> + +# Note that the 'linking' bit means "use this for both compiling *and* linking" +<mach_abi_linking> +amd64 -> "-m64" +mips64 -> "-mabi=64" +s390 -> "-m31" +s390x -> "-m64" +sparc32 -> "-m32 -mno-app-regs" +sparc64 -> "-m64 -mno-app-regs" +ppc64 -> "-m64" + +# This should probably be used on most/all targets, but the docs are incomplete +openbsd -> "-pthread" +freebsd -> "-pthread" +dragonfly -> "-pthread" +netbsd -> "-pthread -D_NETBSD_SOURCE" +qnx -> "-fexceptions -D_QNX_SOURCE" +</mach_abi_linking> diff --git a/botan/src/build-data/cc/hpcc.txt b/botan/src/build-data/cc/hpcc.txt new file mode 100644 index 0000000..284e92c --- /dev/null +++ b/botan/src/build-data/cc/hpcc.txt @@ -0,0 +1,32 @@ +realname "HP-UX C++" + +binary_name "aCC" + +compile_option "-c " +output_to_option "-o " +add_include_dir_option "-I" +add_lib_dir_option "-L" +add_lib_option "-l" + +lib_opt_flags "+O2" +check_opt_flags "+O2" +debug_flags "-g" +no_debug_flags "" +lang_flags "-AA -ext +eh -z" +warning_flags "" # +w +shared_flags "+Z" + +dll_import_flags "" +dll_export_flags "" + +makefile_style unix + +<mach_abi_linking> +hppa1.0 -> "+DAportable" +hppa1.1 -> "+DA1.1" +hppa2.0 -> "+DA2.0W" +</mach_abi_linking> + +<so_link_flags> +default -> "$(CXX) +Z -b -Wl,+h,$(SONAME)" # Documented in cc(1), but not CC(1) (?) +</so_link_flags> diff --git a/botan/src/build-data/cc/icc.txt b/botan/src/build-data/cc/icc.txt new file mode 100644 index 0000000..7d8e968 --- /dev/null +++ b/botan/src/build-data/cc/icc.txt @@ -0,0 +1,35 @@ +realname "Intel C++" + +binary_name "icpc" + +compiler_has_tr1 yes + +compile_option "-c " +output_to_option "-o " +add_include_dir_option "-I" +add_lib_dir_option "-L" +add_lib_option "-l" + +lib_opt_flags "-O2 -ip -unroll" +check_opt_flags "-O2" +debug_flags "-g" +no_debug_flags "-fomit-frame-pointer" +lang_flags "" +warning_flags "-w1" +shared_flags "-fPIC" + +dll_import_flags "" +dll_export_flags "" + +makefile_style unix + +<mach_opt> +pentium3 -> "-march=pentium3" +pentium4 -> "-march=pentium4" +pentium-m -> "-march=pentium3" +core2 -> "-march=core2" +</mach_opt> + +<so_link_flags> +default -> "$(CXX) -fPIC -shared" +</so_link_flags> diff --git a/botan/src/build-data/cc/kai.txt b/botan/src/build-data/cc/kai.txt new file mode 100644 index 0000000..8585e54 --- /dev/null +++ b/botan/src/build-data/cc/kai.txt @@ -0,0 +1,30 @@ +realname "KAI C++" + +binary_name "KCC" + +compile_option "-c " +output_to_option "-o " +add_include_dir_option "-I" +add_lib_dir_option "-L" +add_lib_option "-l" + +ar_command "KCC -o" + +lib_opt_flags "+K3 --inline_auto_space_time=65 --abstract_pointer" +check_opt_flags "+K3" +lang_flags "-D__KAI_STRICT" +debug_flags "-g" +no_debug_flags "" + +dll_import_flags "" +dll_export_flags "" + +makefile_style unix + +<so_link_flags> +default -> "$(CXX) --soname $(SONAME)" +</so_link_flags> + +<mach_abi_linking> +all -> "--one_per" +</mach_abi_linking> diff --git a/botan/src/build-data/cc/mipspro.txt b/botan/src/build-data/cc/mipspro.txt new file mode 100644 index 0000000..b75fc4f --- /dev/null +++ b/botan/src/build-data/cc/mipspro.txt @@ -0,0 +1,42 @@ +realname "SGI MIPSPro C++" + +binary_name "CC" + +compile_option "-c " +output_to_option "-o " +add_include_dir_option "-I" +add_lib_dir_option "-L" +add_lib_option "-l" + +lib_opt_flags "-O3 -OPT:alias=TYPED" +check_opt_flags "-O3 -OPT:alias=TYPED" +debug_flags "-g3" +no_debug_flags "" +shared_flags "-KPIC" +lang_flags "-ansi -LANG:ansi-for-init-scope=ON" +# Disabled, because, while my code is fine, it warns about a lot of it's own +# headers <g> +#warning_flags "-fullwarn" + +dll_import_flags "" +dll_export_flags "" + +makefile_style unix + +<so_link_flags> +default -> "$(CXX) -shared -Wl,-soname,$(SONAME)" +</so_link_flags> + +<mach_opt> +mips32 -> "-mips1" +mips64 -> "-mips3" + +mips64-r5000 -> "-mips4 -r5000" +mips64-r8000 -> "-mips4 -r8000" +mips64-r10000 -> "-mips4 -r10000" +</mach_opt> + +<mach_abi_linking> +mips32 -> "-n32" +mips64 -> "-64" +</mach_abi_linking> diff --git a/botan/src/build-data/cc/msvc.txt b/botan/src/build-data/cc/msvc.txt new file mode 100644 index 0000000..68e4517 --- /dev/null +++ b/botan/src/build-data/cc/msvc.txt @@ -0,0 +1,27 @@ +realname "Visual C++" + +binary_name "cl.exe" + +compile_option "/nologo /c " +output_to_option "/Fo" +add_include_dir_option "/I" +add_lib_dir_option "-L" +add_lib_option "" + +lib_opt_flags "/O2" +check_opt_flags "/O2" +debug_flags "/Zi" +no_debug_flags "" +lang_flags "/EHsc /GR /D_CONSOLE" +warning_flags "" + +dll_import_flags "__declspec(dllimport)" +dll_export_flags "__declspec(dllexport)" + +ar_command "lib" + +makefile_style nmake + +<so_link_flags> +default -> "link /DLL" +</so_link_flags> diff --git a/botan/src/build-data/cc/open64.txt b/botan/src/build-data/cc/open64.txt new file mode 100644 index 0000000..b7c1e9e --- /dev/null +++ b/botan/src/build-data/cc/open64.txt @@ -0,0 +1,30 @@ +realname "Open64" + +binary_name "openCC" + +compile_option "-c " +output_to_option "-o " +add_include_dir_option "-I" +add_lib_dir_option "-L" +add_lib_option "-l" + +lib_opt_flags "-O3 -OPT:alias=TYPED" +check_opt_flags "-O3 -OPT:alias=TYPED" +debug_flags "-g3" +no_debug_flags "-fomit-frame-pointer" +shared_flags "-fPIC" +lang_flags "-ansi -LANG:ansi-for-init-scope=ON" +warning_flags "-Wall -W" + +dll_import_flags "" +dll_export_flags "" + +makefile_style unix + +<so_link_flags> +default -> "$(CXX) -shared -Wl,-soname,$(SONAME)" +</so_link_flags> + +<mach_abi_linking> +amd64 -> "-m64" +</mach_abi_linking> diff --git a/botan/src/build-data/cc/pgi.txt b/botan/src/build-data/cc/pgi.txt new file mode 100644 index 0000000..35f4664 --- /dev/null +++ b/botan/src/build-data/cc/pgi.txt @@ -0,0 +1,31 @@ +realname "Portland Group C++" + +binary_name "pgCC" + +compile_option "-c " +output_to_option "-o " +add_include_dir_option "-I" +add_lib_dir_option "-L" +add_lib_option "-l" + +lib_opt_flags "-fast -Minline" +check_opt_flags "-fast" +shared_flags "-fPIC" + +dll_import_flags "" +dll_export_flags "" + +makefile_style unix + +<so_link_flags> +linux -> "$(CXX) -shared -fPIC -Wl,-soname,$(SONAME)" +solaris -> "$(CXX) -G -fPIC -Wl,-h,$(SONAME)" +</so_link_flags> + +<mach_opt> +i586 -> "-tp p5" +i686 -> "-tp p6" +athlon -> "-tp k7" +pentium4 -> "-tp p6" +ia32 -> "-tp px" +</mach_opt> diff --git a/botan/src/build-data/cc/sgipro64.txt b/botan/src/build-data/cc/sgipro64.txt new file mode 100644 index 0000000..28132ff --- /dev/null +++ b/botan/src/build-data/cc/sgipro64.txt @@ -0,0 +1,30 @@ +realname "SGI Pro64" + +binary_name "sgiCC" + +compile_option "-c " +output_to_option "-o " +add_include_dir_option "-I" +add_lib_dir_option "-L" +add_lib_option "-l" + +lib_opt_flags "-O3 -OPT:alias=TYPED" +check_opt_flags "-O3 -OPT:alias=TYPED" +debug_flags "-g3" +no_debug_flags "" +shared_flags "-KPIC" +lang_flags "-ansi -LANG:ansi-for-init-scope=ON" +warning_flags "-Wall -W" + +dll_import_flags "" +dll_export_flags "" + +makefile_style unix + +<so_link_flags> +default -> "$(CXX) -shared -Wl,-soname,$(SONAME)" +</so_link_flags> + +<mach_opt> + +</mach_opt> diff --git a/botan/src/build-data/cc/sunwspro.txt b/botan/src/build-data/cc/sunwspro.txt new file mode 100644 index 0000000..e1bc0b2 --- /dev/null +++ b/botan/src/build-data/cc/sunwspro.txt @@ -0,0 +1,47 @@ +realname "Sun Workshop Pro C++" + +binary_name "CC" + +compile_option "-c " +output_to_option "-o " +add_include_dir_option "-I" +add_lib_dir_option "-L" +add_lib_option "-l" + +# The compiler supports up to -xO5, but anything higher than -xO2 causes +# incorrect results, infinite loops, other badness. :( +lib_opt_flags "-xO2" +check_opt_flags "-xO2" +debug_flags "-g" +no_debug_flags "" +shared_flags "-KPIC" +warning_flags "+w" +lang_flags "+p -D__EXTENSIONS__" + +dll_import_flags "" +dll_export_flags "" + +makefile_style unix + +<so_link_flags> +default -> "$(CXX) -G -h$(SONAME)" +</so_link_flags> + +<mach_opt> +i386 -> "-xtarget=486" +i486 -> "-xtarget=486" +i586 -> "-xtarget=pentium" +i686 -> "-xtarget=pentium_pro" +pentium4 -> "-xtarget=pentium_pro" +k6 -> "-xtarget=pentium" + +sparc32-v9 -> "-xchip=ultra -xarch=v8" + +sparc32 -> "-xchip=ultra -xarch=SUBMODEL" sparc32- +sparc64 -> "-xchip=SUBMODEL" sparc64- +</mach_opt> + +<mach_abi_linking> +sparc64 -> "-xarch=v9" +amd64 -> "-m64" +</mach_abi_linking> diff --git a/botan/src/build-data/cc/xlc.txt b/botan/src/build-data/cc/xlc.txt new file mode 100644 index 0000000..64b8884 --- /dev/null +++ b/botan/src/build-data/cc/xlc.txt @@ -0,0 +1,29 @@ +realname "IBM XL C/C++" + +binary_name "xlC" + +compile_option "-c " +output_to_option "-o " +add_include_dir_option "-I" +add_lib_dir_option "-L" +add_lib_option "-l" + +lib_opt_flags "-O2" +check_opt_flags "-O2" +lang_flags "" +debug_flags "-g" +no_debug_flags "" + +dll_import_flags "" +dll_export_flags "" + +makefile_style unix + +<mach_opt> +cellppu -> "-qarch=cell" +ppc970 -> "-qarch=ppc970" +power4 -> "-qarch=pwr4" +power5 -> "-qarch=pwr5" + +cellppu -> "-qarch=cell" +</mach_opt> diff --git a/botan/src/build-data/makefile/nmake.in b/botan/src/build-data/makefile/nmake.in new file mode 100644 index 0000000..34c864e --- /dev/null +++ b/botan/src/build-data/makefile/nmake.in @@ -0,0 +1,107 @@ +################################################## +# Compiler Options # +################################################## +CXX = %{cc} +LIB_OPT = %{lib_opt} +CHECK_OPT = %{check_opt} +MACH_OPT = %{mach_opt} +LANG_FLAGS = %{lang_flags} +WARN_FLAGS = %{warn_flags} +LINK_TO = %{link_to} + +################################################## +# Version Numbers # +################################################## +VERSION = %{version} + +################################################## +# Installation Settings # +################################################## +DESTDIR = %{prefix} + +LIBDIR = $(DESTDIR)\%{libdir} +HEADERDIR = $(DESTDIR)\%{includedir}\botan +DOCDIR = $(DESTDIR)\%{docdir}\Botan-$(VERSION) + +################################################## +# Aliases for Common Programs # +################################################## +AR = %{ar_command} +CD = @cd +ECHO = @echo +INSTALL = %{install_cmd_exec} +INSTALL_CMD = %{install_cmd_exec} +MKDIR = @md +MKDIR_INSTALL = @md +RM = @del /Q +RMDIR = @rmdir + +################################################## +# File Lists # +################################################## +CHECK = check + +DOCS = %{doc_files} + +HEADERS = %{include_files} + +LIBOBJS = %{lib_objs} + +CHECKOBJS = %{check_objs} + +LIB_FLAGS = $(LIB_OPT) $(MACH_OPT) $(LANG_FLAGS) $(WARN_FLAGS) +CHECK_FLAGS = $(CHECK_OPT) $(LANG_FLAGS) $(WARN_FLAGS) + +LIBRARIES = $(STATIC_LIB) + +LIBNAME = libbotan +STATIC_LIB = $(LIBNAME).%{static_suffix} + +all: $(LIBRARIES) + +################################################## +# Build Commands # +################################################## +%{lib_build_cmds} + +%{check_build_cmds} + +################################################## +# Link Commands # +################################################## + +# Link for Borland? +#ilink32 -L$(BCB)\lib -w -c -x -Gn -ap -Tpe c0x32.obj \ +# $(CHECKOBJS),check.exe,,$(LINK_TO) $(STATIC_LIB) + +$(CHECK): $(LIBRARIES) $(CHECKOBJS) + link /OUT:$@.exe $(CHECKOBJS) $(STATIC_LIB) $(LINK_TO) + +$(STATIC_LIB): $(LIBOBJS) + $(AR) /OUT:$@ /NAME:BOTAN-$(VERSION) $(LIBOBJS) + +################################################## +# Misc Targets # +################################################## +static: $(STATIC_LIB) + +################################################## +# Fake Targets # +################################################## +clean: + $(RM) %{build_dir}\lib\* %{build_dir}\checks\* + $(RM) $(LIBRARIES) $(CHECK) + +distclean: clean + $(RM) %{build_dir}\build.h + $(RM) %{build_dir}\include\botan\* + $(RMDIR) %{build_dir}\include\botan %{build_dir}\include + $(RMDIR) %{build_dir}\lib %{build_dir}\checks + $(RMDIR) %{build_dir} + $(RM) Makefile + +################################################## +# Install Commands # +################################################## +install: $(LIBRARIES) + $(ECHO) "Install command not implemented" diff --git a/botan/src/build-data/makefile/unix.in b/botan/src/build-data/makefile/unix.in new file mode 100644 index 0000000..a48a5a1 --- /dev/null +++ b/botan/src/build-data/makefile/unix.in @@ -0,0 +1,121 @@ +################################################## +# Compiler Options # +################################################## +CXX = %{cc} +LIB_OPT = %{lib_opt} +CHECK_OPT = %{check_opt} +MACH_OPT = %{mach_opt} +LANG_FLAGS = %{lang_flags} +WARN_FLAGS = %{warn_flags} +LINK_TO = %{link_to} + +################################################## +# Version Numbers # +################################################## +VERSION = %{version} + +################################################## +# Installation Settings # +################################################## +DESTDIR = %{prefix} + +BINDIR = $(DESTDIR)/bin +LIBDIR = $(DESTDIR)/%{libdir} +HEADERDIR = $(DESTDIR)/%{includedir}/botan +DOCDIR = $(DESTDIR)/%{docdir}/Botan-$(VERSION) +PKGCONF_DIR = $(LIBDIR)/pkgconfig + +CONFIG_SCRIPT = %{botan_config} +PKGCONFIG = %{botan_pkgconfig} + +################################################## +# Aliases for Common Programs # +################################################## +AR = %{ar_command} +CD = @cd +ECHO = @echo +INSTALL_CMD_EXEC = %{install_cmd_exec} +INSTALL_CMD_DATA = %{install_cmd_data} +LN = ln -fs +MKDIR = @mkdir +MKDIR_INSTALL = @umask 022; mkdir -p -m 755 +RANLIB = %{ranlib_command} +RM = @rm -f +RM_R = @rm -rf + +################################################## +# File Lists # +################################################## +CHECK = %{check_prefix}check + +DOCS = %{doc_files} + +HEADERS = %{include_files} + +LIBOBJS = %{lib_objs} + +CHECKOBJS = %{check_objs} + +LIB_FLAGS = $(LIB_OPT) $(MACH_OPT) $(LANG_FLAGS) $(WARN_FLAGS) +CHECK_FLAGS = $(CHECK_OPT) $(LANG_FLAGS) $(WARN_FLAGS) + +LIBRARIES = $(STATIC_LIB) + +LIBNAME = %{lib_prefix}libbotan +STATIC_LIB = $(LIBNAME).a + +all: $(LIBRARIES) + +################################################## +# Build Commands # +################################################## +%{lib_build_cmds} + +%{check_build_cmds} + +################################################## +# Link Commands # +################################################## +$(CHECK): $(LIBRARIES) $(CHECKOBJS) + $(CXX) $(CHECKOBJS) -L. libbotan.a $(LINK_TO) -o $(CHECK) + +$(STATIC_LIB): $(LIBOBJS) + $(RM) $(STATIC_LIB) + $(AR) $(STATIC_LIB) $(LIBOBJS) + $(RANLIB) $(STATIC_LIB) + +################################################## +# Fake Targets # +################################################## +.PHONY = doxygen clean distclean install static + +static: $(STATIC_LIB) + +doxygen: + doxygen %{doc_src_dir}/botan.doxy + +clean: + $(RM_R) %{build_dir}/lib/* %{build_dir}/checks/* + $(RM) $(LIBRARIES) $(SYMLINK) $(CHECK) + +distclean: clean + $(RM_R) %{build_dir} + $(RM_R) %{doc_src_dir}/doxygen %{doc_src_dir}/botan.doxy + $(RM) Makefile $(CONFIG_SCRIPT) $(PKGCONFIG) + +install: $(LIBRARIES) + $(ECHO) "Installing Botan into $(DESTDIR)... " + $(MKDIR_INSTALL) $(DOCDIR) + $(MKDIR_INSTALL) $(HEADERDIR) + $(MKDIR_INSTALL) $(LIBDIR) + $(MKDIR_INSTALL) $(BINDIR) + $(MKDIR_INSTALL) $(PKGCONF_DIR) + for i in $(DOCS); do \ + $(INSTALL_CMD_DATA) $$i $(DOCDIR); \ + done + for i in $(HEADERS); do \ + $(INSTALL_CMD_DATA) $$i $(HEADERDIR); \ + done + $(INSTALL_CMD_DATA) $(STATIC_LIB) $(LIBDIR) + $(INSTALL_CMD_EXEC) $(CONFIG_SCRIPT) $(BINDIR) + $(INSTALL_CMD_DATA) $(PKGCONFIG) $(PKGCONF_DIR) diff --git a/botan/src/build-data/makefile/unix_shr.in b/botan/src/build-data/makefile/unix_shr.in new file mode 100644 index 0000000..f718d11 --- /dev/null +++ b/botan/src/build-data/makefile/unix_shr.in @@ -0,0 +1,137 @@ +################################################## +# Compiler Options # +################################################## +CXX = %{cc} +LIB_OPT = %{lib_opt} +CHECK_OPT = %{check_opt} +MACH_OPT = %{mach_opt} +LANG_FLAGS = %{lang_flags} +WARN_FLAGS = %{warn_flags} +SO_OBJ_FLAGS = %{shared_flags} +SO_LINK_CMD = %{so_link} +LINK_TO = %{link_to} + +################################################## +# Version Numbers # +################################################## +VERSION = %{version} +SO_VERSION = %{so_version} + +################################################## +# Installation Settings # +################################################## +DESTDIR = %{prefix} + +BINDIR = $(DESTDIR)/bin +LIBDIR = $(DESTDIR)/%{libdir} +HEADERDIR = $(DESTDIR)/%{includedir}/botan +DOCDIR = $(DESTDIR)/%{docdir}/Botan-$(VERSION) +PKGCONF_DIR = $(LIBDIR)/pkgconfig + +CONFIG_SCRIPT = %{botan_config} +PKGCONFIG = %{botan_pkgconfig} + +################################################## +# Aliases for Common Programs # +################################################## +AR = %{ar_command} +CD = @cd +ECHO = @echo +INSTALL_CMD_EXEC = %{install_cmd_exec} +INSTALL_CMD_DATA = %{install_cmd_data} +LN = ln -fs +MKDIR = @mkdir +MKDIR_INSTALL = @umask 022; mkdir -p -m 755 +RANLIB = %{ranlib_command} +RM = @rm -f +RM_R = @rm -rf + +################################################## +# File Lists # +################################################## +CHECK = %{check_prefix}check + +DOCS = %{doc_files} + +HEADERS = %{include_files} + +LIBOBJS = %{lib_objs} + +CHECKOBJS = %{check_objs} + +LIB_FLAGS = $(LIB_OPT) $(MACH_OPT) $(LANG_FLAGS) $(WARN_FLAGS) $(SO_OBJ_FLAGS) +CHECK_FLAGS = $(CHECK_OPT) $(LANG_FLAGS) $(WARN_FLAGS) + +LIBRARIES = $(STATIC_LIB) $(SHARED_LIB) + +LIBNAME = %{lib_prefix}libbotan +STATIC_LIB = $(LIBNAME).a + +SHARED_LIB = $(LIBNAME)-$(SO_VERSION).%{so_suffix} +SONAME = $(LIBNAME)-$(SO_VERSION).%{so_suffix} + +SYMLINK = libbotan.%{so_suffix} + +all: $(LIBRARIES) + +################################################## +# Build Commands # +################################################## +%{lib_build_cmds} + +%{check_build_cmds} + +################################################## +# Link Commands # +################################################## +$(CHECK): $(LIBRARIES) $(CHECKOBJS) + $(CXX) $(LDFLAGS) $(CHECKOBJS) -o $(CHECK) -L. -lbotan-%{so_version} $(LINK_TO) + +$(STATIC_LIB): $(LIBOBJS) + $(RM) $(STATIC_LIB) + $(AR) $(STATIC_LIB) $(LIBOBJS) + $(RANLIB) $(STATIC_LIB) + +$(SHARED_LIB): $(LIBOBJS) + $(SO_LINK_CMD) $(LDFLAGS) $(LIBOBJS) -o $(SHARED_LIB) $(LINK_TO) + $(LN) $(SHARED_LIB) $(SYMLINK) + +################################################## +# Fake Targets # +################################################## +.PHONY = doxygen clean distclean install static shared + +static: $(STATIC_LIB) + +shared: $(SHARED_LIB) + +doxygen: + doxygen %{doc_src_dir}/botan.doxy + +clean: + $(RM_R) %{build_dir}/lib/* %{build_dir}/checks/* + $(RM) $(LIBRARIES) $(SYMLINK) $(CHECK) + +distclean: clean + $(RM_R) %{build_dir} + $(RM_R) %{doc_src_dir}/doxygen %{doc_src_dir}/botan.doxy + $(RM) Makefile $(CONFIG_SCRIPT) $(PKGCONFIG) + +install: $(LIBRARIES) + $(ECHO) "Installing Botan into $(DESTDIR)... " + $(MKDIR_INSTALL) $(DOCDIR) + $(MKDIR_INSTALL) $(HEADERDIR) + $(MKDIR_INSTALL) $(LIBDIR) + $(MKDIR_INSTALL) $(BINDIR) + $(MKDIR_INSTALL) $(PKGCONF_DIR) + for i in $(DOCS); do \ + $(INSTALL_CMD_DATA) $$i $(DOCDIR); \ + done + for i in $(HEADERS); do \ + $(INSTALL_CMD_DATA) $$i $(HEADERDIR); \ + done + $(INSTALL_CMD_DATA) $(STATIC_LIB) $(LIBDIR) + $(INSTALL_CMD_EXEC) $(CONFIG_SCRIPT) $(BINDIR) + $(INSTALL_CMD_EXEC) $(SHARED_LIB) $(LIBDIR) + $(INSTALL_CMD_DATA) $(PKGCONFIG) $(PKGCONF_DIR) + $(CD) $(LIBDIR); $(LN) $(SHARED_LIB) $(SYMLINK) diff --git a/botan/src/build-data/os/aix.txt b/botan/src/build-data/os/aix.txt new file mode 100644 index 0000000..cec8185 --- /dev/null +++ b/botan/src/build-data/os/aix.txt @@ -0,0 +1,7 @@ +realname "AIX" + +os_type unix + +<supports_shared> +all +</supports_shared> diff --git a/botan/src/build-data/os/beos.txt b/botan/src/build-data/os/beos.txt new file mode 100644 index 0000000..2b12792 --- /dev/null +++ b/botan/src/build-data/os/beos.txt @@ -0,0 +1,16 @@ +realname "BeOS" + +os_type beos + +install_root /boot/beos +header_dir ../develop/headers +lib_dir system/lib +doc_dir documentation + +<supports_shared> +all +</supports_shared> + +<aliases> +haiku +</aliases> diff --git a/botan/src/build-data/os/cygwin.txt b/botan/src/build-data/os/cygwin.txt new file mode 100644 index 0000000..c2aadea --- /dev/null +++ b/botan/src/build-data/os/cygwin.txt @@ -0,0 +1,13 @@ +realname "Cygwin" + +os_type unix + +install_root c:\Botan +doc_dir docs + +# Cygwin supports shared libs fine, but there are problems with making a Botan +# shared library when libraries it depends on are static-only (such as libz). +# So until I can figure out a work-around, it's disabled. +<supports_shared> +#all +</supports_shared> diff --git a/botan/src/build-data/os/darwin.txt b/botan/src/build-data/os/darwin.txt new file mode 100644 index 0000000..2986212 --- /dev/null +++ b/botan/src/build-data/os/darwin.txt @@ -0,0 +1,19 @@ +realname "Darwin / MacOS X" + +os_type unix + +so_suffix dylib + +# It doesn't have the 's' option; you need to use needs ranlib +ar_command "ar cr" +ar_needs_ranlib yes + +doc_dir doc + +<supports_shared> +all +</supports_shared> + +<aliases> +macosx +</aliases> diff --git a/botan/src/build-data/os/defaults.txt b/botan/src/build-data/os/defaults.txt new file mode 100644 index 0000000..5648643 --- /dev/null +++ b/botan/src/build-data/os/defaults.txt @@ -0,0 +1,19 @@ +# Defaults are for a Unix box, since that's what most of OS variants are. It +# would be nice to have generic 'windows', 'beos', 'unix', etc targets to +# handle defaults for those classes of OSes, though Unix is by far the most +# varied/fragmented + +obj_suffix o +so_suffix so +static_suffix a + +ar_command "ar crs" +ar_needs_ranlib no + +install_root /usr/local +header_dir include +lib_dir lib +doc_dir share/doc + +install_cmd_data "install -m 644" +install_cmd_exec "install -m 755" diff --git a/botan/src/build-data/os/dragonfly.txt b/botan/src/build-data/os/dragonfly.txt new file mode 100644 index 0000000..7e36634 --- /dev/null +++ b/botan/src/build-data/os/dragonfly.txt @@ -0,0 +1,11 @@ +realname "DragonFly" + +os_type unix + +<target_features> +posix_mlock +</target_features> + +<supports_shared> +all +</supports_shared> diff --git a/botan/src/build-data/os/freebsd.txt b/botan/src/build-data/os/freebsd.txt new file mode 100644 index 0000000..ea96b0c --- /dev/null +++ b/botan/src/build-data/os/freebsd.txt @@ -0,0 +1,11 @@ +realname "FreeBSD" + +os_type unix + +<target_features> +posix_mlock +</target_features> + +<supports_shared> +all +</supports_shared> diff --git a/botan/src/build-data/os/hpux.txt b/botan/src/build-data/os/hpux.txt new file mode 100644 index 0000000..6e17d3b --- /dev/null +++ b/botan/src/build-data/os/hpux.txt @@ -0,0 +1,13 @@ +realname "HP-UX" + +os_type unix + +so_suffix sl + +<supports_shared> +all +</supports_shared> + +<aliases> +hp-ux +</aliases> diff --git a/botan/src/build-data/os/irix.txt b/botan/src/build-data/os/irix.txt new file mode 100644 index 0000000..fd8b432 --- /dev/null +++ b/botan/src/build-data/os/irix.txt @@ -0,0 +1,7 @@ +realname "IRIX" + +os_type unix + +<supports_shared> +all +</supports_shared> diff --git a/botan/src/build-data/os/linux.txt b/botan/src/build-data/os/linux.txt new file mode 100644 index 0000000..5352851 --- /dev/null +++ b/botan/src/build-data/os/linux.txt @@ -0,0 +1,12 @@ +realname "Linux" + +os_type unix + +<target_features> +posix_mlock +</target_features> + +# Is this correct? +<supports_shared> +all +</supports_shared> diff --git a/botan/src/build-data/os/mingw.txt b/botan/src/build-data/os/mingw.txt new file mode 100644 index 0000000..eb25017 --- /dev/null +++ b/botan/src/build-data/os/mingw.txt @@ -0,0 +1,22 @@ +realname "MS Windows (MinGW)" +os_type windows + +obj_suffix o +so_suffix dll +static_suffix a + +ar_command "ar crs" +ar_needs_ranlib yes + +install_root /mingw +header_dir include +lib_dir lib +doc_dir share/doc + +install_cmd_data "install -m 644" +install_cmd_exec "install -m 755" + +<aliases> +msys +mingw32 +</aliases> diff --git a/botan/src/build-data/os/netbsd.txt b/botan/src/build-data/os/netbsd.txt new file mode 100644 index 0000000..435d8f5 --- /dev/null +++ b/botan/src/build-data/os/netbsd.txt @@ -0,0 +1,7 @@ +realname "NetBSD" + +os_type unix + +<supports_shared> +all +</supports_shared> diff --git a/botan/src/build-data/os/openbsd.txt b/botan/src/build-data/os/openbsd.txt new file mode 100644 index 0000000..cb44bd1 --- /dev/null +++ b/botan/src/build-data/os/openbsd.txt @@ -0,0 +1,7 @@ +realname "OpenBSD" + +os_type unix + +<supports_shared> +all +</supports_shared> diff --git a/botan/src/build-data/os/qnx.txt b/botan/src/build-data/os/qnx.txt new file mode 100644 index 0000000..28bc8de --- /dev/null +++ b/botan/src/build-data/os/qnx.txt @@ -0,0 +1,7 @@ +realname "QNX" + +os_type unix + +<supports_shared> +all +</supports_shared> diff --git a/botan/src/build-data/os/solaris.txt b/botan/src/build-data/os/solaris.txt new file mode 100644 index 0000000..8610b48 --- /dev/null +++ b/botan/src/build-data/os/solaris.txt @@ -0,0 +1,15 @@ +realname "Solaris" + +os_type unix + +<target_features> +posix_mlock +</target_features> + +<supports_shared> +all +</supports_shared> + +<aliases> +sunos +</aliases> diff --git a/botan/src/build-data/os/tru64.txt b/botan/src/build-data/os/tru64.txt new file mode 100644 index 0000000..e320c1d --- /dev/null +++ b/botan/src/build-data/os/tru64.txt @@ -0,0 +1,11 @@ +realname "Tru64" + +os_type unix + +<supports_shared> +all +</supports_shared> + +<aliases> +osf1 +</aliases> diff --git a/botan/src/build-data/os/windows.txt b/botan/src/build-data/os/windows.txt new file mode 100644 index 0000000..a04d609 --- /dev/null +++ b/botan/src/build-data/os/windows.txt @@ -0,0 +1,26 @@ +realname "MS Windows" + +os_type windows + +obj_suffix obj +so_suffix dll +static_suffix lib + +install_root c:\Botan +doc_dir docs + +install_cmd_data "copy" +install_cmd_exec "copy" + +<target_features> +win32_virtual_lock +</target_features> + +<supports_shared> +msvc +</supports_shared> + +<aliases> +win32 +MSWin32 +</aliases> diff --git a/botan/src/cert/cvc/asn1_eac_str.cpp b/botan/src/cert/cvc/asn1_eac_str.cpp new file mode 100644 index 0000000..a306ffb --- /dev/null +++ b/botan/src/cert/cvc/asn1_eac_str.cpp @@ -0,0 +1,130 @@ +/* +* Simple ASN.1 String Types +* (C) 2007 FlexSecure GmbH +* 2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/eac_asn_obj.h> +#include <botan/der_enc.h> +#include <botan/ber_dec.h> +#include <botan/charset.h> +#include <botan/parsing.h> +#include <sstream> + +namespace Botan { + +/* +* Create an ASN1_EAC_String +*/ +ASN1_EAC_String::ASN1_EAC_String(const std::string& str, ASN1_Tag t) : tag(t) + { + iso_8859_str = Charset::transcode(str, LOCAL_CHARSET, LATIN1_CHARSET); + if (!sanity_check()) + { + throw Invalid_Argument("attempted to construct ASN1_EAC_String with illegal characters"); + } + } + +/* +* Return this string in ISO 8859-1 encoding +*/ +std::string ASN1_EAC_String::iso_8859() const + { + return iso_8859_str; + } + +/* +* Return this string in local encoding +*/ +std::string ASN1_EAC_String::value() const + { + return Charset::transcode(iso_8859_str, LATIN1_CHARSET, LOCAL_CHARSET); + } + +/* +* Return the type of this string object +*/ +ASN1_Tag ASN1_EAC_String::tagging() const + { + return tag; + } + +/* +* DER encode an ASN1_EAC_String +*/ +void ASN1_EAC_String::encode_into(DER_Encoder& encoder) const + { + std::string value = iso_8859(); + encoder.add_object(tagging(), APPLICATION, value); + } + +/* +* Decode a BER encoded ASN1_EAC_String +*/ +void ASN1_EAC_String::decode_from(BER_Decoder& source) + { + BER_Object obj = source.get_next_object(); + if (obj.type_tag != this->tag) + { + + std::string message("decoding type mismatch for ASN1_EAC_String, tag is "); + std::stringstream ss; + std::string str_is; + ss << std::hex << obj.type_tag; + ss >> str_is; + message.append(str_is); + message.append(", while it should be "); + std::stringstream ss2; + std::string str_should; + ss2 << std::hex << this->tag; + ss2 >> str_should; + message.append(str_should); + throw Decoding_Error(message); + } + Character_Set charset_is; + charset_is = LATIN1_CHARSET; + + try + { + *this = ASN1_EAC_String( + Charset::transcode(ASN1::to_string(obj), charset_is, LOCAL_CHARSET), + obj.type_tag); + } + catch (Invalid_Argument inv_arg) + { + throw Decoding_Error(std::string("error while decoding ASN1_EAC_String: ") + std::string(inv_arg.what())); + } + } + +// checks for compliance to the alphabet defined in TR-03110 v1.10, 2007-08-20 +// p. 43 +bool ASN1_EAC_String::sanity_check() const + { + const byte* rep = reinterpret_cast<const byte*>(iso_8859_str.data()); + const u32bit rep_len = iso_8859_str.size(); + for (u32bit i=0; i<rep_len; i++) + { + if ((rep[i] < 0x20) || ((rep[i] >= 0x7F) && (rep[i] < 0xA0))) + { + return false; + } + } + return true; + } + +bool operator==(const ASN1_EAC_String& lhs, const ASN1_EAC_String& rhs) + { + return (lhs.iso_8859() == rhs.iso_8859()); + } + +ASN1_Car::ASN1_Car(std::string const& str) + : ASN1_EAC_String(str, ASN1_Tag(2)) + {} + +ASN1_Chr::ASN1_Chr(std::string const& str) + : ASN1_EAC_String(str, ASN1_Tag(32)) + {} + +} diff --git a/botan/src/cert/cvc/asn1_eac_tm.cpp b/botan/src/cert/cvc/asn1_eac_tm.cpp new file mode 100644 index 0000000..05533b5 --- /dev/null +++ b/botan/src/cert/cvc/asn1_eac_tm.cpp @@ -0,0 +1,361 @@ +/* +* EAC Time Types +* (C) 2007 FlexSecure GmbH +* 2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/eac_asn_obj.h> +#include <botan/der_enc.h> +#include <botan/ber_dec.h> +#include <botan/charset.h> +#include <botan/parsing.h> +#include <ctime> +#include <sstream> + +namespace Botan { + +namespace { + +/* +* Convert a time_t to a struct tm +*/ +std::tm get_tm(u64bit timer) + { + std::time_t time_val = static_cast<std::time_t>(timer); + + std::tm* tm_p = std::gmtime(&time_val); + if (tm_p == 0) + throw Encoding_Error("EAC_Time: gmtime could not encode " + + to_string(timer)); + return (*tm_p); + } +SecureVector<byte> enc_two_digit(u32bit in) + { + SecureVector<byte> result; + in %= 100; + if (in < 10) + { + result.append(0x00); + } + else + { + u32bit y_first_pos = (in - (in%10))/10; + result.append(static_cast<byte>(y_first_pos)); + } + u32bit y_sec_pos = in%10; + result.append(static_cast<byte>(y_sec_pos)); + return result; + } +u32bit dec_two_digit(byte b1, byte b2) + { + u32bit upper = (u32bit)b1; + u32bit lower = (u32bit)b2; + if (upper > 9 || lower > 9) + { + throw Invalid_Argument("u32bit dec_two_digit(byte b1, byte b2): value too large"); + } + return upper*10 + lower; + + } +} + +/* +* Create an EAC_Time +*/ +EAC_Time::EAC_Time(u64bit timer, ASN1_Tag t) + :tag(t) + { + std::tm time_info = get_tm(timer); + + year = time_info.tm_year + 1900; + month = time_info.tm_mon + 1; + day = time_info.tm_mday; + + } + +/* +* Create an EAC_Time +*/ +EAC_Time::EAC_Time(const std::string& t_spec, ASN1_Tag t) + :tag(t) + { + set_to(t_spec); + } +/* +* Create an EAC_Time +*/ +EAC_Time::EAC_Time(u32bit y, u32bit m, u32bit d, ASN1_Tag t) + : year(y), + month(m), + day(d), + tag(t) + { + } + +/* +* Set the time with a human readable string +*/ +void EAC_Time::set_to(const std::string& time_str) + { + if (time_str == "") + { + year = month = day = 0; + return; + } + + std::vector<std::string> params; + std::string current; + + for (u32bit j = 0; j != time_str.size(); ++j) + { + if (Charset::is_digit(time_str[j])) + current += time_str[j]; + else + { + if (current != "") + params.push_back(current); + current.clear(); + } + } + if (current != "") + params.push_back(current); + + if (params.size() != 3) + throw Invalid_Argument("Invalid time specification " + time_str); + + year = to_u32bit(params[0]); + month = to_u32bit(params[1]); + day = to_u32bit(params[2]); + + if (!passes_sanity_check()) + throw Invalid_Argument("Invalid time specification " + time_str); + } + + +/* +* DER encode a EAC_Time +*/ +void EAC_Time::encode_into(DER_Encoder& der) const + { + der.add_object(tag, APPLICATION, + encoded_eac_time()); + } + +/* +* Return a string representation of the time +*/ +std::string EAC_Time::as_string() const + { + if (time_is_set() == false) + throw Invalid_State("EAC_Time::as_string: No time set"); + + std::string asn1rep; + asn1rep = to_string(year, 2); + + asn1rep += to_string(month, 2) + to_string(day, 2); + + return asn1rep; + } + +/* +* Return if the time has been set somehow +*/ +bool EAC_Time::time_is_set() const + { + return (year != 0); + } + +/* +* Return a human readable string representation +*/ +std::string EAC_Time::readable_string() const + { + if (time_is_set() == false) + throw Invalid_State("EAC_Time::readable_string: No time set"); + + std::string readable; + readable += to_string(year, 2) + "/"; + readable += to_string(month, 2) + "/"; + readable += to_string(day, 2) + " "; + + return readable; + } + +/* +* Do a general sanity check on the time +*/ +bool EAC_Time::passes_sanity_check() const + { + if (year < 2000 || year > 2099) + return false; + if (month == 0 || month > 12) + return false; + if (day == 0 || day > 31) + return false; + + return true; + } + +/****************************************** +* modification functions +******************************************/ + +void EAC_Time::add_years(u32bit years) + { + year += years; + } +void EAC_Time::add_months(u32bit months) + { + year += months/12; + month += months % 12; + if(month > 12) + { + year += 1; + month -= 12; + } + } + + +/* +* Compare this time against another +*/ +s32bit EAC_Time::cmp(const EAC_Time& other) const + { + if (time_is_set() == false) + throw Invalid_State("EAC_Time::cmp: No time set"); + + const s32bit EARLIER = -1, LATER = 1, SAME_TIME = 0; + + if (year < other.year) return EARLIER; + if (year > other.year) return LATER; + if (month < other.month) return EARLIER; + if (month > other.month) return LATER; + if (day < other.day) return EARLIER; + if (day > other.day) return LATER; + + return SAME_TIME; + } + +/* +* Compare two EAC_Times for in various ways +*/ +bool operator==(const EAC_Time& t1, const EAC_Time& t2) + { + return (t1.cmp(t2) == 0); + } +bool operator!=(const EAC_Time& t1, const EAC_Time& t2) + { + return (t1.cmp(t2) != 0); + } +bool operator<=(const EAC_Time& t1, const EAC_Time& t2) + { + return (t1.cmp(t2) <= 0); + } +bool operator>=(const EAC_Time& t1, const EAC_Time& t2) + { + return (t1.cmp(t2) >= 0); + } +bool operator>(const EAC_Time& t1, const EAC_Time& t2) + { + return (t1.cmp(t2) > 0); + } +bool operator<(const EAC_Time& t1, const EAC_Time& t2) + { + return (t1.cmp(t2) < 0); + } + +/* +* Decode a BER encoded EAC_Time +*/ +void EAC_Time::decode_from(BER_Decoder& source) + { + BER_Object obj = source.get_next_object(); + if (obj.type_tag != this->tag) + { + std::string message("decoding type mismatch for EAC_Time, tag is "); + std::stringstream ss; + std::string str_is; + ss << std::hex << obj.type_tag; + ss >> str_is; + message.append(str_is); + message.append(", while it should be "); + std::stringstream ss2; + std::string str_should; + ss2 << std::hex << this->tag; + ss2 >> str_should; + message.append(str_should); + throw Decoding_Error(message); + + } + if (obj.value.size() != 6) + { + throw Decoding_Error("EAC_Time decoding failed"); + } + try + { + u32bit tmp_year = dec_two_digit(obj.value[0], obj.value[1]); + u32bit tmp_mon = dec_two_digit(obj.value[2], obj.value[3]); + u32bit tmp_day = dec_two_digit(obj.value[4], obj.value[5]); + year = tmp_year + 2000; + month = tmp_mon; + day = tmp_day; + } + catch (Invalid_Argument) + { + throw Decoding_Error("EAC_Time decoding failed"); + } + + } + +u32bit EAC_Time::get_year() const + { + return year; + } +u32bit EAC_Time::get_month() const + { + return month; + } +u32bit EAC_Time::get_day() const + { + return day; + } + +/* +* make the value an octet string for encoding +*/ +SecureVector<byte> EAC_Time::encoded_eac_time() const + { + SecureVector<byte> result; + result.append(enc_two_digit(year)); + result.append(enc_two_digit(month)); + result.append(enc_two_digit(day)); + return result; + } + +ASN1_Ced::ASN1_Ced(std::string const& str) + : EAC_Time(str, ASN1_Tag(37)) + {} + +ASN1_Ced::ASN1_Ced(u64bit val) + : EAC_Time(val, ASN1_Tag(37)) + {} + +ASN1_Ced::ASN1_Ced(EAC_Time const& other) + : EAC_Time(other.get_year(), other.get_month(), other.get_day(), ASN1_Tag(37)) + {} + +ASN1_Cex::ASN1_Cex(std::string const& str) + : EAC_Time(str, ASN1_Tag(36)) + {} + +ASN1_Cex::ASN1_Cex(u64bit val) + : EAC_Time(val, ASN1_Tag(36)) + {} + +ASN1_Cex::ASN1_Cex(EAC_Time const& other) + : EAC_Time(other.get_year(), other.get_month(), other.get_day(), ASN1_Tag(36)) + {} + +} diff --git a/botan/src/cert/cvc/cvc_ado.cpp b/botan/src/cert/cvc/cvc_ado.cpp new file mode 100644 index 0000000..6e1484e --- /dev/null +++ b/botan/src/cert/cvc/cvc_ado.cpp @@ -0,0 +1,135 @@ +/* +* CVC Certificate Constructor +* (C) 2007 FlexSecure GmbH +* 2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/cvc_ado.h> +#include <fstream> +#include <assert.h> + +namespace Botan { + +EAC1_1_ADO::EAC1_1_ADO(std::tr1::shared_ptr<DataSource> in) + { + init(in); + do_decode(); + } + +EAC1_1_ADO::EAC1_1_ADO(const std::string& in) + { + std::tr1::shared_ptr<DataSource> stream(new DataSource_Stream(in, true)); + init(stream); + do_decode(); + } + +void EAC1_1_ADO::force_decode() + { + SecureVector<byte> inner_cert; + BER_Decoder(tbs_bits) + .start_cons(ASN1_Tag(33)) + .raw_bytes(inner_cert) + .end_cons() + .decode(m_car) + .verify_end(); + + SecureVector<byte> req_bits = DER_Encoder() + .start_cons(ASN1_Tag(33), APPLICATION) + .raw_bytes(inner_cert) + .end_cons() + .get_contents(); + + std::tr1::shared_ptr<DataSource> req_source(new DataSource_Memory(req_bits)); + m_req = EAC1_1_Req(req_source); + sig_algo = m_req.sig_algo; + } + +MemoryVector<byte> EAC1_1_ADO::make_signed( + std::auto_ptr<PK_Signer> signer, + const MemoryRegion<byte>& tbs_bits, + RandomNumberGenerator& rng) + { + SecureVector<byte> concat_sig = + EAC1_1_obj<EAC1_1_ADO>::make_signature(signer.get(), tbs_bits, rng); + assert(concat_sig.size() % 2 == 0); + MemoryVector<byte> result = DER_Encoder() + .start_cons(ASN1_Tag(7), APPLICATION) + .raw_bytes(tbs_bits) + .encode(concat_sig, OCTET_STRING, ASN1_Tag(55), APPLICATION) + .end_cons() + .get_contents(); + return result; + } + +ASN1_Car EAC1_1_ADO::get_car() const + { + return m_car; + } + +void EAC1_1_ADO::decode_info(SharedPtrConverter<DataSource> source, + SecureVector<byte> & res_tbs_bits, + ECDSA_Signature & res_sig) + { + SecureVector<byte> concat_sig; + SecureVector<byte> cert_inner_bits; + ASN1_Car car; + BER_Decoder(*source.get_ptr().get()) + .start_cons(ASN1_Tag(7)) + .start_cons(ASN1_Tag(33)) + .raw_bytes(cert_inner_bits) + .end_cons() + .decode(car) + .decode(concat_sig, OCTET_STRING, ASN1_Tag(55), APPLICATION) + .end_cons(); + + SecureVector<byte> enc_cert = DER_Encoder() + .start_cons(ASN1_Tag(33), APPLICATION) + .raw_bytes(cert_inner_bits) + .end_cons() + .get_contents(); + SecureVector<byte> enc_car = DER_Encoder() + .encode(car) + .get_contents(); + res_tbs_bits = enc_cert; + res_tbs_bits.append(enc_car); + res_sig = decode_concatenation(concat_sig); + + + } +void EAC1_1_ADO::encode(Pipe& out, X509_Encoding encoding) const + { + SecureVector<byte> concat_sig(EAC1_1_obj<EAC1_1_ADO>::m_sig.get_concatenation()); + SecureVector<byte> der = DER_Encoder() + .start_cons(ASN1_Tag(7), APPLICATION) + .raw_bytes(tbs_bits) + .encode(concat_sig, OCTET_STRING, ASN1_Tag(55), APPLICATION) + .end_cons() + .get_contents(); + if(encoding == PEM) + throw Invalid_Argument("EAC1_1_ADO::encode() cannot PEM encode an EAC object"); + else + out.write(der); + } + +SecureVector<byte> EAC1_1_ADO::tbs_data() const + { + return tbs_bits; + } + +bool EAC1_1_ADO::operator==(EAC1_1_ADO const& rhs) const + { + assert(((this->m_req == rhs.m_req) && (this->tbs_data() == rhs.tbs_data())) || + ((this->m_req != rhs.m_req) && (this->tbs_data() != rhs.tbs_data()))); + return (this->get_concat_sig() == rhs.get_concat_sig() + && this->tbs_data() == rhs.tbs_data() + && this->get_car() == rhs.get_car()); + } + +EAC1_1_Req EAC1_1_ADO::get_request() const + { + return m_req; + } + +} diff --git a/botan/src/cert/cvc/cvc_ado.h b/botan/src/cert/cvc/cvc_ado.h new file mode 100644 index 0000000..2c4f3ce --- /dev/null +++ b/botan/src/cert/cvc/cvc_ado.h @@ -0,0 +1,101 @@ +/* +* EAC1_1 CVC ADO +* (C) 2008 Falko Strenzke +* strenzke@flexsecure.de +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_EAC_CVC_ADO_H__ +#define BOTAN_EAC_CVC_ADO_H__ + +#include <botan/x509_key.h> +#include <botan/pubkey_enums.h> +#include <botan/pubkey.h> +#include <botan/ecdsa.h> +#include <botan/eac_obj.h> +#include <botan/cvc_req.h> +#include <string> + +namespace Botan { + +/** +* This class represents a TR03110 (EAC) v1.1 CVC ADO request +*/ + + // CRTP continuation from EAC1_1_obj +class BOTAN_DLL EAC1_1_ADO : public EAC1_1_obj<EAC1_1_ADO> + { + public: + friend class EAC1_1_obj<EAC1_1_ADO>; + + /** + * Construct a CVC ADO request from a DER encoded CVC ADO request file. + * @param str the path to the DER encoded file + */ + EAC1_1_ADO(const std::string& str); + + /** + * Construct a CVC ADO request from a data source + * @param source the data source + */ + EAC1_1_ADO(std::tr1::shared_ptr<DataSource> source); + + /** + * Create a signed CVC ADO request from to be signed (TBS) data + * @param signer the signer used to sign the CVC ADO request + * @param tbs_bits the TBS data to sign + */ + static MemoryVector<byte> make_signed( + std::auto_ptr<PK_Signer> signer, + const MemoryRegion<byte>& tbs_bits, + RandomNumberGenerator& rng); + + /** + * Get the CAR of this CVC ADO request + * @result the CAR of this CVC ADO request + */ + ASN1_Car get_car() const; + + /** + * Get the CVC request contained in this object. + * @result the CVC request inside this CVC ADO request + */ + EAC1_1_Req get_request() const; + + /** + * Encode this object into a pipe. Only DER is supported. + * @param out the pipe to encode this object into + * @param encoding the encoding type to use, must be DER + */ + void encode(Pipe& out, X509_Encoding encoding) const; + + bool operator==(EAC1_1_ADO const& rhs) const; + + /** + * Get the TBS data of this CVC ADO request. + * @result the TBS data + */ + SecureVector<byte> tbs_data() const; + + virtual ~EAC1_1_ADO() {} + private: + ASN1_Car m_car; + EAC1_1_Req m_req; + + void force_decode(); + static void decode_info(SharedPtrConverter<DataSource> source, + SecureVector<byte> & res_tbs_bits, + ECDSA_Signature & res_sig); + }; + +inline bool operator!=(EAC1_1_ADO const& lhs, EAC1_1_ADO const& rhs) + { + return (!(lhs == rhs)); + } + +} + +#endif + + diff --git a/botan/src/cert/cvc/cvc_ca.cpp b/botan/src/cert/cvc/cvc_ca.cpp new file mode 100644 index 0000000..638d3f9 --- /dev/null +++ b/botan/src/cert/cvc/cvc_ca.cpp @@ -0,0 +1,46 @@ +#include <botan/cvc_ca.h> +#include <botan/cvc_cert.h> +#include <botan/der_enc.h> +#include <botan/util.h> +#include <botan/oids.h> +namespace Botan { + +EAC1_1_CVC EAC1_1_CVC_CA::make_cert(std::auto_ptr<PK_Signer> signer, + MemoryRegion<byte> const& public_key, + ASN1_Car const& car, + ASN1_Chr const& chr, + byte holder_auth_templ, + ASN1_Ced ced, + ASN1_Cex cex, + RandomNumberGenerator& rng) + { + OID chat_oid(OIDS::lookup("CertificateHolderAuthorizationTemplate")); + MemoryVector<byte> enc_chat_val; + enc_chat_val.append(holder_auth_templ); + + MemoryVector<byte> enc_cpi; + enc_cpi.append(0x00); + MemoryVector<byte> tbs = DER_Encoder() + .encode(enc_cpi, OCTET_STRING, ASN1_Tag(41), APPLICATION) // cpi + .encode(car) + .raw_bytes(public_key) + .encode(chr) + .start_cons(ASN1_Tag(76), APPLICATION) + .encode(chat_oid) + .encode(enc_chat_val, OCTET_STRING, ASN1_Tag(19), APPLICATION) + .end_cons() + .encode(ced) + .encode(cex) + .get_contents(); + + MemoryVector<byte> signed_cert = + EAC1_1_CVC::make_signed(signer, + EAC1_1_CVC::build_cert_body(tbs), + rng); + + std::tr1::shared_ptr<DataSource> source(new DataSource_Memory(signed_cert)); + + return EAC1_1_CVC(source); + } + +} diff --git a/botan/src/cert/cvc/cvc_ca.h b/botan/src/cert/cvc/cvc_ca.h new file mode 100644 index 0000000..3ec307b --- /dev/null +++ b/botan/src/cert/cvc/cvc_ca.h @@ -0,0 +1,51 @@ +/* +* EAC1.1 CVC Certificate Authority +* (C) 2007 FlexSecure GmbH +* 2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_CVC_CA_H__ +#define BOTAN_CVC_CA_H__ + +#include <botan/pkcs8.h> +#include <botan/pkcs10.h> +#include <botan/pubkey.h> +#include <botan/cvc_cert.h> + +namespace Botan { + +/** +* This class represents a CVC CA. +*/ +class BOTAN_DLL EAC1_1_CVC_CA + { + public: + + /** + * Create an arbitrary EAC 1.1 CVC. + * The desired key encoding must be set within the key (if applicable). + * @param signer the signer used to sign the certificate + * @param public_key the DER encoded public key to appear in + * the certificate + * @param car the CAR of the certificate + * @param chr the CHR of the certificate + * @param holder_auth_templ the holder authorization value byte to + * appear in the CHAT of the certificate + * @param ced the CED to appear in the certificate + * @param ced the CEX to appear in the certificate + */ + static EAC1_1_CVC make_cert(std::auto_ptr<PK_Signer> signer, + MemoryRegion<byte> const& public_key, + ASN1_Car const& car, + ASN1_Chr const& chr, + byte holder_auth_templ, + ASN1_Ced ced, + ASN1_Cex cex, + RandomNumberGenerator& rng); + }; + +} + +#endif diff --git a/botan/src/cert/cvc/cvc_cert.cpp b/botan/src/cert/cvc/cvc_cert.cpp new file mode 100644 index 0000000..d2be12d --- /dev/null +++ b/botan/src/cert/cvc/cvc_cert.cpp @@ -0,0 +1,102 @@ +/* + (C) 2007 FlexSecure GmbH + 2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/cvc_cert.h> +#include <botan/cvc_key.h> +#include <botan/ecdsa.h> + +namespace Botan { + +ASN1_Car EAC1_1_CVC::get_car() const + { + return m_car; + } + +ASN1_Ced EAC1_1_CVC::get_ced() const + { + return m_ced; + } +ASN1_Cex EAC1_1_CVC::get_cex() const + { + return m_cex; + } +u32bit EAC1_1_CVC::get_chat_value() const + { + return m_chat_val; + } + +/* +* Decode the TBSCertificate data +*/ +void EAC1_1_CVC::force_decode() + { + SecureVector<byte> enc_pk; + SecureVector<byte> enc_chat_val; + u32bit cpi; + BER_Decoder tbs_cert(tbs_bits); + tbs_cert.decode(cpi, ASN1_Tag(41), APPLICATION) + .decode(m_car) + .start_cons(ASN1_Tag(73)) + .raw_bytes(enc_pk) + .end_cons() + .decode(m_chr) + .start_cons(ASN1_Tag(76)) + .decode(m_chat_oid) + .decode(enc_chat_val, OCTET_STRING, ASN1_Tag(19), APPLICATION) + .end_cons() + .decode(m_ced) + .decode(m_cex) + .verify_end(); + + if(enc_chat_val.size() != 1) + throw Decoding_Error("CertificateHolderAuthorizationValue was not of length 1"); + + if(cpi != 0) + throw Decoding_Error("EAC1_1 certificate´s cpi was not 0"); + + // FIXME: PK algos have no notion of EAC encoder/decoder currently +#if 0 + ECDSA_PublicKey tmp_pk; + std::auto_ptr<EAC1_1_CVC_Decoder> dec = tmp_pk.cvc_eac1_1_decoder(); + sig_algo = dec->public_key(enc_pk); + + + m_pk = tmp_pk; + m_chat_val = enc_chat_val[0]; + self_signed = false; + if(m_car.iso_8859() == m_chr.iso_8859()) + { + self_signed= true; + } +#endif + } + +/* +* CVC Certificate Constructor +*/ +EAC1_1_CVC::EAC1_1_CVC(std::tr1::shared_ptr<DataSource>& in) + { + init(in); + self_signed = false; + do_decode(); + } + +EAC1_1_CVC::EAC1_1_CVC(const std::string& in) + { + std::tr1::shared_ptr<DataSource> stream(new DataSource_Stream(in, true)); + init(stream); + self_signed = false; + do_decode(); + } + +bool EAC1_1_CVC::operator==(EAC1_1_CVC const& rhs) const + { + return (tbs_data() == rhs.tbs_data() + && get_concat_sig() == rhs.get_concat_sig()); + } + +} diff --git a/botan/src/cert/cvc/cvc_cert.h b/botan/src/cert/cvc/cvc_cert.h new file mode 100644 index 0000000..17671d3 --- /dev/null +++ b/botan/src/cert/cvc/cvc_cert.h @@ -0,0 +1,94 @@ +/* +* EAC1_1 CVC +* (C) 2008 Falko Strenzke +* 2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_CVC_EAC_H__ +#define BOTAN_CVC_EAC_H__ + +#include <botan/x509_key.h> +#include <botan/pubkey_enums.h> +#include <botan/signed_obj.h> +#include <botan/pubkey.h> +#include <botan/ecdsa.h> +#include <botan/ecdsa_sig.h> +#include <botan/eac_obj.h> +#include <botan/cvc_gen_cert.h> +#include <string> + +namespace Botan { + +/** +* This class represents TR03110 (EAC) v1.1 CV Certificates +*/ +class BOTAN_DLL EAC1_1_CVC : public EAC1_1_gen_CVC<EAC1_1_CVC>//Signed_Object + { + public: + friend class EAC1_1_obj<EAC1_1_CVC>; + + /** + * Get the CAR of the certificate. + * @result the CAR of the certificate + */ + ASN1_Car get_car() const; + + /** + * Get the CED of this certificate. + * @result the CED this certificate + */ + ASN1_Ced get_ced() const; + + /** + * Get the CEX of this certificate. + * @result the CEX this certificate + */ + ASN1_Cex get_cex() const; + + /** + * Get the CHAT value. + * @result the CHAT value + */ + u32bit get_chat_value() const; + + bool operator==(const EAC1_1_CVC&) const; + + /** + * Construct a CVC from a data source + * @param source the data source + */ + EAC1_1_CVC(std::tr1::shared_ptr<DataSource>& source); + + /** + * Construct a CVC from a file + * @param str the path to the certificate file + */ + EAC1_1_CVC(const std::string& str); + + virtual ~EAC1_1_CVC() {} + private: + void force_decode(); + friend class EAC1_1_CVC_CA; + EAC1_1_CVC() {} + + ASN1_Car m_car; + ASN1_Ced m_ced; + ASN1_Cex m_cex; + byte m_chat_val; + OID m_chat_oid; + }; + +/* +* Comparison +*/ +inline bool operator!=(EAC1_1_CVC const& lhs, EAC1_1_CVC const& rhs) + { + return !(lhs == rhs); + } + +} + +#endif + diff --git a/botan/src/cert/cvc/cvc_gen_cert.h b/botan/src/cert/cvc/cvc_gen_cert.h new file mode 100644 index 0000000..4a78802 --- /dev/null +++ b/botan/src/cert/cvc/cvc_gen_cert.h @@ -0,0 +1,177 @@ +/* +* EAC1_1 general CVC +* (C) 2008 Falko Strenzke +* 2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_EAC_CVC_GEN_CERT_H__ +#define BOTAN_EAC_CVC_GEN_CERT_H__ + +#include <botan/x509_key.h> +#include <botan/eac_asn_obj.h> +#include <botan/pubkey_enums.h> +#include <botan/pubkey.h> +#include <botan/ecdsa_sig.h> +#include <string> +#include <assert.h> + +namespace Botan { + +/** +* This class represents TR03110 (EAC) v1.1 generalized CV Certificates +*/ +template<typename Derived> +class BOTAN_DLL EAC1_1_gen_CVC : public EAC1_1_obj<Derived> // CRTP continuation from EAC1_1_obj + { + friend class EAC1_1_obj<EAC1_1_gen_CVC>; + + public: + + /** + * Get this certificates public key. + * @result this certificates public key + */ + std::auto_ptr<Public_Key> subject_public_key() const; + + /** + * Find out whether this object is self signed. + * @result true if this object is self signed + */ + bool is_self_signed() const; + + /** + * Get the CHR of the certificate. + * @result the CHR of the certificate + */ + ASN1_Chr get_chr() const; + + /** + * Put the DER encoded version of this object into a pipe. PEM + * is not supported. + * @param out the pipe to push the DER encoded version into + * @param encoding the encoding to use. Must be DER. + */ + void encode(Pipe& out, X509_Encoding encoding) const; + + /** + * Get the to-be-signed (TBS) data of this object. + * @result the TBS data of this object + */ + SecureVector<byte> tbs_data() const; + + /** + * Build the DER encoded certifcate body of an object + * @param tbs the data to be signed + * @result the correctly encoded body of the object + */ + static SecureVector<byte> build_cert_body(MemoryRegion<byte> const& tbs); + + /** + * Create a signed generalized CVC object. + * @param signer the signer used to sign this object + * @param tbs_bits the body the generalized CVC object to be signed + * @result the DER encoded signed generalized CVC object + */ + static MemoryVector<byte> make_signed( + std::auto_ptr<PK_Signer> signer, + const MemoryRegion<byte>& tbs_bits, + RandomNumberGenerator& rng); + virtual ~EAC1_1_gen_CVC<Derived>() + {} + + protected: + ECDSA_PublicKey m_pk; // public key + ASN1_Chr m_chr; + bool self_signed; + + static void decode_info(SharedPtrConverter<DataSource> source, + SecureVector<byte> & res_tbs_bits, + ECDSA_Signature & res_sig); + + }; + +template<typename Derived> ASN1_Chr EAC1_1_gen_CVC<Derived>::get_chr() const + { + return m_chr; + } + +template<typename Derived> bool EAC1_1_gen_CVC<Derived>::is_self_signed() const + { + return self_signed; + } + +template<typename Derived> MemoryVector<byte> EAC1_1_gen_CVC<Derived>::make_signed( + std::auto_ptr<PK_Signer> signer, + const MemoryRegion<byte>& tbs_bits, + RandomNumberGenerator& rng) // static + { + SecureVector<byte> concat_sig = EAC1_1_obj<Derived>::make_signature(signer.get(), tbs_bits, rng); + assert(concat_sig.size() % 2 == 0); + return DER_Encoder() + .start_cons(ASN1_Tag(33), APPLICATION) + .raw_bytes(tbs_bits) + .encode(concat_sig, OCTET_STRING, ASN1_Tag(55), APPLICATION) + .end_cons() + .get_contents(); + } + +template<typename Derived> std::auto_ptr<Public_Key> EAC1_1_gen_CVC<Derived>::subject_public_key() const + { + return std::auto_ptr<Public_Key>(new ECDSA_PublicKey(m_pk)); + } + +template<typename Derived> SecureVector<byte> EAC1_1_gen_CVC<Derived>::build_cert_body(MemoryRegion<byte> const& tbs) + { + return DER_Encoder() + .start_cons(ASN1_Tag(78), APPLICATION) + .raw_bytes(tbs) + .end_cons().get_contents(); + } + +template<typename Derived> SecureVector<byte> EAC1_1_gen_CVC<Derived>::tbs_data() const + { + return build_cert_body(EAC1_1_obj<Derived>::tbs_bits); + } + +template<typename Derived> void EAC1_1_gen_CVC<Derived>::encode(Pipe& out, X509_Encoding encoding) const + { + SecureVector<byte> concat_sig(EAC1_1_obj<Derived>::m_sig.get_concatenation()); + SecureVector<byte> der = DER_Encoder() + .start_cons(ASN1_Tag(33), APPLICATION) + .start_cons(ASN1_Tag(78), APPLICATION) + .raw_bytes(EAC1_1_obj<Derived>::tbs_bits) + .end_cons() + .encode(concat_sig, OCTET_STRING, ASN1_Tag(55), APPLICATION) + .end_cons() + .get_contents(); + + if (encoding == PEM) + throw Invalid_Argument("EAC1_1_gen_CVC::encode() cannot PEM encode an EAC object"); + else + out.write(der); + } + +template<typename Derived> +void EAC1_1_gen_CVC<Derived>::decode_info( + SharedPtrConverter<DataSource> source, + SecureVector<byte> & res_tbs_bits, + ECDSA_Signature & res_sig) + { + SecureVector<byte> concat_sig; + BER_Decoder(*source.get_shared().get()) + .start_cons(ASN1_Tag(33)) + .start_cons(ASN1_Tag(78)) + .raw_bytes(res_tbs_bits) + .end_cons() + .decode(concat_sig, OCTET_STRING, ASN1_Tag(55), APPLICATION) + .end_cons(); + res_sig = decode_concatenation(concat_sig); + } + +} + +#endif + + diff --git a/botan/src/cert/cvc/cvc_key.h b/botan/src/cert/cvc/cvc_key.h new file mode 100644 index 0000000..67b6cef --- /dev/null +++ b/botan/src/cert/cvc/cvc_key.h @@ -0,0 +1,54 @@ +/* +* EAC CVC Public Key +* (C) 2008 FlexSecure Gmbh +* Falko Strenzke +* strenzke@flexsecure.de +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_EAC1_1_CVC_PUBLIC_KEY_H__ +#define BOTAN_EAC1_1_CVC_PUBLIC_KEY_H__ + +#include <botan/pipe.h> +#include <botan/pk_keys.h> +#include <botan/alg_id.h> + +namespace Botan { + +/** +* This class represents EAC 1.1 CVC public key encoders. +*/ +class BOTAN_DLL EAC1_1_CVC_Encoder + { + public: + /** + * Get the DER encoded CVC public key. + * @param alg_id the algorithm identifier to use in the encoding + * @return the DER encoded public key + */ + virtual MemoryVector<byte> + public_key(const AlgorithmIdentifier& enc) const = 0; + + virtual ~EAC1_1_CVC_Encoder() {} + }; + +/** +* This class represents EAC 1.1 CVC public key decoders. +*/ +class BOTAN_DLL EAC1_1_CVC_Decoder + { + public: + /** + * Decode a CVC public key. + * @param enc the DER encoded public key to decode + * @return the algorithm identifier found in the encoded public key + */ + virtual AlgorithmIdentifier const + public_key(const MemoryRegion<byte>& enc) = 0; + + virtual ~EAC1_1_CVC_Decoder() {} + }; +} + +#endif diff --git a/botan/src/cert/cvc/cvc_req.cpp b/botan/src/cert/cvc/cvc_req.cpp new file mode 100644 index 0000000..70a44ba --- /dev/null +++ b/botan/src/cert/cvc/cvc_req.cpp @@ -0,0 +1,68 @@ +/* + (C) 2007 FlexSecure GmbH + 2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/cvc_cert.h> +#include <botan/der_enc.h> +#include <botan/ber_dec.h> +#include <botan/pem.h> +#include <botan/parsing.h> +#include <assert.h> +#include <botan/cvc_key.h> +#include <botan/oids.h> +#include <botan/look_pk.h> +#include <botan/cvc_req.h> +#include <botan/freestore.h> + +namespace Botan { + +bool EAC1_1_Req::operator==(EAC1_1_Req const& rhs) const + { + return (this->tbs_data() == rhs.tbs_data() + && this->get_concat_sig() == rhs.get_concat_sig()); + } + +void EAC1_1_Req::force_decode() + { + SecureVector<byte> enc_pk; + BER_Decoder tbs_cert(tbs_bits); + u32bit cpi; + tbs_cert.decode(cpi, ASN1_Tag(41), APPLICATION) + .start_cons(ASN1_Tag(73)) + .raw_bytes(enc_pk) + .end_cons() + .decode(m_chr) + .verify_end(); + if(cpi != 0) + { + throw Decoding_Error("EAC1_1 request´s cpi was not 0"); + } + + // FIXME: No EAC support in ECDSA +#if 0 + ECDSA_PublicKey tmp_pk; + std::auto_ptr<EAC1_1_CVC_Decoder> dec = tmp_pk.cvc_eac1_1_decoder(); + sig_algo = dec->public_key(enc_pk); + m_pk = tmp_pk; +#endif + } + +EAC1_1_Req::EAC1_1_Req(std::tr1::shared_ptr<DataSource> in) + { + init(in); + self_signed = true; + do_decode(); + } + +EAC1_1_Req::EAC1_1_Req(const std::string& in) + { + std::tr1::shared_ptr<DataSource> stream(new DataSource_Stream(in, true)); + init(stream); + self_signed = true; + do_decode(); + } + +} diff --git a/botan/src/cert/cvc/cvc_req.h b/botan/src/cert/cvc/cvc_req.h new file mode 100644 index 0000000..96b6a44 --- /dev/null +++ b/botan/src/cert/cvc/cvc_req.h @@ -0,0 +1,62 @@ +/* +* EAC1_1 CVC Request +* (C) 2008 Falko Strenzke +* strenzke@flexsecure.de +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_EAC_CVC_REQ_H__ +#define BOTAN_EAC_CVC_REQ_H__ + +#include <botan/x509_key.h> +#include <botan/pubkey_enums.h> +#include <botan/cvc_gen_cert.h> + +namespace Botan { + +/** +* This class represents TR03110 v1.1 EAC CV Certificate Requests. +*/ +class BOTAN_DLL EAC1_1_Req : public EAC1_1_gen_CVC<EAC1_1_Req> + { + public: + friend class EAC1_1_Req_CA; + friend class EAC1_1_ADO; + friend class EAC1_1_obj<EAC1_1_Req>; + + /** + * Compare for equality with other + * @param other compare for equality with this object + */ + bool operator==(const EAC1_1_Req& other) const; + + /** + * Construct a CVC request from a data source. + * @param source the data source + */ + EAC1_1_Req(std::tr1::shared_ptr<DataSource> source); + + /** + * Construct a CVC request from a DER encoded CVC reqeust file. + * @param str the path to the DER encoded file + */ + EAC1_1_Req(const std::string& str); + + virtual ~EAC1_1_Req(){} + private: + void force_decode(); + EAC1_1_Req() {} + }; + +/* +* Comparison Operator +*/ +inline bool operator!=(EAC1_1_Req const& lhs, EAC1_1_Req const& rhs) + { + return !(lhs == rhs); + } + +} + +#endif diff --git a/botan/src/cert/cvc/cvc_self.cpp b/botan/src/cert/cvc/cvc_self.cpp new file mode 100644 index 0000000..91ea387 --- /dev/null +++ b/botan/src/cert/cvc/cvc_self.cpp @@ -0,0 +1,327 @@ +/* + (C) 2007 FlexSecure GmbH + 2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/cvc_self.h> +#include <botan/cvc_cert.h> +#include <botan/cvc_ca.h> +#include <botan/alg_id.h> +#include <botan/cvc_key.h> +#include <botan/oids.h> +#include <botan/look_pk.h> +#include <botan/cvc_req.h> +#include <botan/cvc_ado.h> +#include <botan/util.h> +#include <sstream> + +namespace Botan { + +namespace { + +/******************************* +* cvc CHAT values +*******************************/ +enum CHAT_values{ + CVCA = 0xC0, + DVCA_domestic = 0x80, + DVCA_foreign = 0x40, + IS = 0x00, + + IRIS = 0x02, + FINGERPRINT = 0x01 +}; + +std::string padding_and_hash_from_oid(OID const& oid) + { + std::string padding_and_hash = OIDS::lookup(oid); // use the hash + assert(padding_and_hash.substr(0,6) == "ECDSA/"); // can only be ECDSA for now + assert(padding_and_hash.find("/",0) == 5); + padding_and_hash.erase(0, padding_and_hash.find("/",0) + 1); + return padding_and_hash; + } +std::string fixed_len_seqnr(u32bit seqnr, u32bit len) + { + std::stringstream ss; + std::string result; + ss << seqnr; + ss >> result; + if (result.size() > len) + { + throw Invalid_Argument("fixed_len_seqnr(): number too high to be encoded in provided length"); + } + while (result.size() < len) + { + result.insert(0,"0"); + } + return result; + } + +} +namespace CVC_EAC +{ + +EAC1_1_CVC create_self_signed_cert(Private_Key const& key, + EAC1_1_CVC_Options const& opt, + RandomNumberGenerator& rng) + { + // NOTE: we ignore + // the value + // of opt.chr + ECDSA_PrivateKey const* priv_key = dynamic_cast<ECDSA_PrivateKey const*>(&key); + + if (priv_key == 0) + { + throw Invalid_Argument("CVC_EAC::create_self_signed_cert(): unsupported key type"); + } + + ASN1_Chr chr(opt.car.value()); + + AlgorithmIdentifier sig_algo; + std::string padding_and_hash(eac_cvc_emsa + "(" + opt.hash_alg + ")"); + sig_algo.oid = OIDS::lookup(priv_key->algo_name() + "/" + padding_and_hash); + sig_algo = AlgorithmIdentifier(sig_algo.oid, AlgorithmIdentifier::USE_NULL_PARAM); + + std::auto_ptr<Botan::PK_Signer> signer(get_pk_signer(*priv_key, padding_and_hash)); + +#if 0 // FIXME + std::auto_ptr<EAC1_1_CVC_Encoder> enc(priv_key->cvc_eac1_1_encoder()); + MemoryVector<byte> enc_public_key = enc->public_key(sig_algo); +#else + MemoryVector<byte> enc_public_key; +#endif + + return EAC1_1_CVC_CA::make_cert(signer, enc_public_key, opt.car, chr, opt.holder_auth_templ, opt.ced, opt.cex, rng); + + } + +EAC1_1_Req create_cvc_req(Private_Key const& key, + ASN1_Chr const& chr, + std::string const& hash_alg, + RandomNumberGenerator& rng) + { + + ECDSA_PrivateKey const* priv_key = dynamic_cast<ECDSA_PrivateKey const*>(&key); + if (priv_key == 0) + { + throw Invalid_Argument("CVC_EAC::create_self_signed_cert(): unsupported key type"); + } + AlgorithmIdentifier sig_algo; + std::string padding_and_hash(eac_cvc_emsa + "(" + hash_alg + ")"); + sig_algo.oid = OIDS::lookup(priv_key->algo_name() + "/" + padding_and_hash); + sig_algo = AlgorithmIdentifier(sig_algo.oid, AlgorithmIdentifier::USE_NULL_PARAM); + + std::auto_ptr<Botan::PK_Signer> signer(get_pk_signer(*priv_key, padding_and_hash)); + +#if 0 // FIXME + std::auto_ptr<EAC1_1_CVC_Encoder> enc(priv_key->cvc_eac1_1_encoder()); + MemoryVector<byte> enc_public_key = enc->public_key(sig_algo); +#else + MemoryVector<byte> enc_public_key; +#endif + + MemoryVector<byte> enc_cpi; + enc_cpi.append(0x00); + MemoryVector<byte> tbs = DER_Encoder() + .encode(enc_cpi, OCTET_STRING, ASN1_Tag(41), APPLICATION) + .raw_bytes(enc_public_key) + .encode(chr) + .get_contents(); + + MemoryVector<byte> signed_cert = EAC1_1_gen_CVC<EAC1_1_Req>::make_signed(signer, EAC1_1_gen_CVC<EAC1_1_Req>::build_cert_body(tbs), rng); + std::tr1::shared_ptr<DataSource> source(new DataSource_Memory(signed_cert)); + return EAC1_1_Req(source); + } + +EAC1_1_ADO create_ado_req(Private_Key const& key, + EAC1_1_Req const& req, + ASN1_Car const& car, + RandomNumberGenerator& rng) + { + + ECDSA_PrivateKey const* priv_key = dynamic_cast<ECDSA_PrivateKey const*>(&key); + if (priv_key == 0) + { + throw Invalid_Argument("CVC_EAC::create_self_signed_cert(): unsupported key type"); + } + std::string padding_and_hash = padding_and_hash_from_oid(req.signature_algorithm().oid); + std::auto_ptr<Botan::PK_Signer> signer(get_pk_signer(*priv_key, padding_and_hash)); + SecureVector<byte> tbs_bits = req.BER_encode(); + tbs_bits.append(DER_Encoder().encode(car).get_contents()); + MemoryVector<byte> signed_cert = EAC1_1_ADO::make_signed(signer, tbs_bits, rng); + std::tr1::shared_ptr<DataSource> source(new DataSource_Memory(signed_cert)); + return EAC1_1_ADO(source); + } + +} // namespace CVC_EAC +namespace DE_EAC +{ + +EAC1_1_CVC create_cvca(Private_Key const& key, + std::string const& hash, + ASN1_Car const& car, bool iris, bool fingerpr, + u32bit cvca_validity_months, + RandomNumberGenerator& rng) + { + ECDSA_PrivateKey const* priv_key = dynamic_cast<ECDSA_PrivateKey const*>(&key); + if (priv_key == 0) + { + throw Invalid_Argument("CVC_EAC::create_self_signed_cert(): unsupported key type"); + } + EAC1_1_CVC_Options opts; + opts.car = car; + const u64bit current_time = system_time(); + + opts.ced = ASN1_Ced(current_time); + opts.cex = ASN1_Cex(opts.ced); + opts.cex.add_months(cvca_validity_months); + opts.holder_auth_templ = (CVCA | (iris * IRIS) | (fingerpr * FINGERPRINT)); + opts.hash_alg = hash; + return Botan::CVC_EAC::create_self_signed_cert(*priv_key, opts, rng); + } + + + +EAC1_1_CVC link_cvca(EAC1_1_CVC const& signer, + Private_Key const& key, + EAC1_1_CVC const& signee, + RandomNumberGenerator& rng) + { + ECDSA_PrivateKey const* priv_key = dynamic_cast<ECDSA_PrivateKey const*>(&key); + if (priv_key == 0) + { + throw Invalid_Argument("CVC_EAC::create_self_signed_cert(): unsupported key type"); + } + ASN1_Ced ced(system_time()); + ASN1_Cex cex(signee.get_cex()); + if (*static_cast<EAC_Time*>(&ced) > *static_cast<EAC_Time*>(&cex)) + { + std::string detail("link_cvca(): validity periods of provided certificates don't overlap: currend time = ced = "); + detail += ced.as_string(); + detail += ", signee.cex = "; + detail += cex.as_string(); + throw Invalid_Argument(detail); + } + if (signer.signature_algorithm() != signee.signature_algorithm()) + { + throw Invalid_Argument("link_cvca(): signature algorithms of signer and signee don´t match"); + } + AlgorithmIdentifier sig_algo = signer.signature_algorithm(); + std::string padding_and_hash = padding_and_hash_from_oid(sig_algo.oid); + std::auto_ptr<Botan::PK_Signer> pk_signer(get_pk_signer(*priv_key, padding_and_hash)); + std::auto_ptr<Public_Key> pk = signee.subject_public_key(); + ECDSA_PublicKey* subj_pk = dynamic_cast<ECDSA_PublicKey*>(pk.get()); + subj_pk->set_parameter_encoding(ENC_EXPLICIT); + +#if 0 // FIXME + std::auto_ptr<EAC1_1_CVC_Encoder> enc(subj_pk->cvc_eac1_1_encoder()); + MemoryVector<byte> enc_public_key = enc->public_key(sig_algo); +#else + MemoryVector<byte> enc_public_key; +#endif + + return EAC1_1_CVC_CA::make_cert(pk_signer, enc_public_key, + signer.get_car(), + signee.get_chr(), + signer.get_chat_value(), + ced, + cex, + rng); + } + +EAC1_1_CVC sign_request(EAC1_1_CVC const& signer_cert, + Private_Key const& key, + EAC1_1_Req const& signee, + u32bit seqnr, + u32bit seqnr_len, + bool domestic, + u32bit dvca_validity_months, + u32bit ca_is_validity_months, + RandomNumberGenerator& rng) + { + ECDSA_PrivateKey const* priv_key = dynamic_cast<ECDSA_PrivateKey const*>(&key); + if (priv_key == 0) + { + throw Invalid_Argument("CVC_EAC::create_self_signed_cert(): unsupported key type"); + } + std::string chr_str = signee.get_chr().value(); + chr_str.append(fixed_len_seqnr(seqnr, seqnr_len)); + ASN1_Chr chr(chr_str); + std::string padding_and_hash = padding_and_hash_from_oid(signee.signature_algorithm().oid); + std::auto_ptr<Botan::PK_Signer> pk_signer(get_pk_signer(*priv_key, padding_and_hash)); + std::auto_ptr<Public_Key> pk = signee.subject_public_key(); + ECDSA_PublicKey* subj_pk = dynamic_cast<ECDSA_PublicKey*>(pk.get()); + std::auto_ptr<Public_Key> signer_pk = signer_cert.subject_public_key(); + + // for the case that the domain parameters are not set... + // (we use those from the signer because they must fit) + subj_pk->set_domain_parameters(priv_key->domain_parameters()); + + subj_pk->set_parameter_encoding(ENC_IMPLICITCA); + +#if 0 // FIXME + std::auto_ptr<EAC1_1_CVC_Encoder> enc(subj_pk->cvc_eac1_1_encoder()); + MemoryVector<byte> enc_public_key = enc->public_key(sig_algo); +#else + MemoryVector<byte> enc_public_key; +#endif + + AlgorithmIdentifier sig_algo(signer_cert.signature_algorithm()); + const u64bit current_time = system_time(); + ASN1_Ced ced(current_time); + u32bit chat_val; + u32bit chat_low = signer_cert.get_chat_value() & 0x3; // take the chat rights from signer + ASN1_Cex cex(ced); + if ((signer_cert.get_chat_value() & CVCA) == CVCA) + { + // we sign a dvca + cex.add_months(dvca_validity_months); + if (domestic) + { + chat_val = DVCA_domestic | chat_low; + } + else + { + chat_val = DVCA_foreign | chat_low; + } + } + else if ((signer_cert.get_chat_value() & DVCA_domestic) == DVCA_domestic || + (signer_cert.get_chat_value() & DVCA_foreign) == DVCA_foreign) + { + cex.add_months(ca_is_validity_months); + chat_val = IS | chat_low; + } + else + { + throw Invalid_Argument("sign_request(): encountered illegal value for CHAT"); + // (IS cannot sign certificates) + } + return EAC1_1_CVC_CA::make_cert(pk_signer, enc_public_key, + ASN1_Car(signer_cert.get_chr().iso_8859()), + chr, + chat_val, + ced, + cex, + rng); + } + +EAC1_1_Req create_cvc_req(Private_Key const& prkey, + ASN1_Chr const& chr, + std::string const& hash_alg, + RandomNumberGenerator& rng) + { + ECDSA_PrivateKey const* priv_key = dynamic_cast<ECDSA_PrivateKey const*>(&prkey); + if (priv_key == 0) + { + throw Invalid_Argument("CVC_EAC::create_self_signed_cert(): unsupported key type"); + } + ECDSA_PrivateKey key(*priv_key); + key.set_parameter_encoding(ENC_IMPLICITCA); + return Botan::CVC_EAC::create_cvc_req(key, chr, hash_alg, rng); + } + +} // namespace DE_EAC + +} diff --git a/botan/src/cert/cvc/cvc_self.h b/botan/src/cert/cvc/cvc_self.h new file mode 100644 index 0000000..db23547 --- /dev/null +++ b/botan/src/cert/cvc/cvc_self.h @@ -0,0 +1,167 @@ +/* +* CVC Self-Signed Certificate +* (C) 2007 FlexSecure GmbH +* 2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_CVC_EAC_SELF_H__ +#define BOTAN_CVC_EAC_SELF_H__ + +#include <botan/pkcs8.h> +#include <botan/pkcs10.h> +#include <botan/cvc_cert.h> +#include <botan/ecdsa.h> +#include <botan/asn1_obj.h> +#include <botan/cvc_req.h> +#include <botan/cvc_ado.h> + +namespace Botan { + +/** +* This class represents a set of options used for the creation of CVC certificates +*/ +class BOTAN_DLL EAC1_1_CVC_Options + { + public: + + ASN1_Car car; + ASN1_Chr chr; + byte holder_auth_templ; + ASN1_Ced ced; + ASN1_Cex cex; + std::string hash_alg; + }; + +/** +* This namespace represents general EAC 1.1 convenience functions. +*/ +namespace CVC_EAC { + +/** +* Create a selfsigned CVCA +* @param rng the rng to use +* @param key the ECDSA private key to be used to sign the certificate +* @param opts used to set several parameters. Necessary are: +* car, holder_auth_templ, hash_alg, ced, cex and hash_alg +* @result the self signed certificate +*/ + +EAC1_1_CVC create_self_signed_cert(Private_Key const& key, + EAC1_1_CVC_Options const& opts, + RandomNumberGenerator& rng); +/** +* Create a CVC request. The key encoding will be according to the provided private key. +* @param priv_key the private key associated with the requesting entity +* @param chr the chr to appear in the certificate (to be provided without +* sequence number) +* @param hash_alg the string defining the hash algorithm to be used for the creation +* of the signature +* @param rng the rng to use +* @result the new request +*/ +EAC1_1_Req create_cvc_req(Private_Key const& priv_key, + ASN1_Chr const& chr, + std::string const& hash_alg, + RandomNumberGenerator& rng); + +/** +* Create an ADO from a request object. +* @param priv_key the private key used to sign the ADO +* @param req the request forming the body of the ADO +* @param car the CAR forming the body of the ADO, i.e. the +* CHR of the entity associated with the provided private key +* @param rng the rng to use +*/ +EAC1_1_ADO create_ado_req(Private_Key const& priv_key, + EAC1_1_Req const& req, + ASN1_Car const& car, + RandomNumberGenerator& rng); +} +/** +* This namespace represents EAC 1.1 CVC convenience functions following the specific german +* requirements. +*/ +namespace DE_EAC { + +/** +* Create a CVCA certificate. +* @param priv_key the private key associated with the CVCA certificate +* to be created +* @param hash the string identifying the hash algorithm to be used +* for signing the certificate to be created +* @param car the CAR of the certificate to be created +* @param iris indicates whether the entity associated with the certificate +* shall be entitled to read the biometrical iris image +* @param fingerpr indicates whether the entity associated with the certificate +* shall be entitled to read the biometrical fingerprint image +* @param rng the rng to use +* @result the CVCA certificate created +*/ +EAC1_1_CVC create_cvca(Private_Key const& priv_key, + std::string const& hash, + ASN1_Car const& car, + bool iris, + bool fingerpr, + u32bit cvca_validity_months, + RandomNumberGenerator& rng); + +/** +* Create a link certificate between two CVCA certificates. The key +* encoding will be implicitCA. +* @param signer the cvca certificate associated with the signing +* entity +* @param priv_key the private key associated with the signer +* @param to_be_signed the certificate which whose CAR/CHR will be +* the holder of the link certificate +* @param rng a random number generator +*/ +EAC1_1_CVC link_cvca(EAC1_1_CVC const& signer, + Private_Key const& priv_key, + EAC1_1_CVC const& to_be_signed, + RandomNumberGenerator& rng); + +/** +* Create a CVC request. The key encoding will be implicitCA. +* @param priv_key the private key associated with the requesting entity +* @param chr the chr to appear in the certificate (to be provided without +* sequence number) +* @param hash_alg the string defining the hash algorithm to be used for the creation +* of the signature +* @param rng a random number generator +* @result the new request +*/ +EAC1_1_Req create_cvc_req(Private_Key const& priv_key, + ASN1_Chr const& chr, + std::string const& hash_alg, + RandomNumberGenerator& rng); + +/** +* Sign a CVC request. +* @param signer_cert the certificate of the signing entity +* @param priv_key the private key of the signing entity +* @param req the request to be signed +* @param seqnr the sequence number of the certificate to be created +* @param seqnr_len the number of digits the sequence number will be +* encoded in +* @param domestic indicates whether to sign a domestic or a foreign certificate: +* set to true for domestic +* @param rng a random number generator +* @result the new certificate +* +**/ +EAC1_1_CVC sign_request(EAC1_1_CVC const& signer_cert, + Private_Key const& priv_key, + EAC1_1_Req const& req, + u32bit seqnr, + u32bit seqnr_len, + bool domestic, + u32bit dvca_validity_months, + u32bit ca_is_validity_months, + RandomNumberGenerator& rng); +} + +} + +#endif diff --git a/botan/src/cert/cvc/eac_asn_obj.h b/botan/src/cert/cvc/eac_asn_obj.h new file mode 100644 index 0000000..3e70f6b --- /dev/null +++ b/botan/src/cert/cvc/eac_asn_obj.h @@ -0,0 +1,244 @@ +/* +* EAC ASN.1 Objects +* (C) 2007-2008 FlexSecure GmbH +* 2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_EAC_ASN1_OBJ_H__ +#define BOTAN_EAC_ASN1_OBJ_H__ + +#include <botan/asn1_obj.h> +#include <vector> +#include <map> + +namespace Botan { + +/** +* This class represents CVC EAC Time objects. +* It only models year, month and day. Only limited sanity checks of +* the inputted date value are performed. +*/ +class BOTAN_DLL EAC_Time : public ASN1_Object + { + public: + void encode_into(class DER_Encoder&) const; + void decode_from(class BER_Decoder&); + + /** + * Get a this objects value as a string. + * @return the date string + */ + std::string as_string() const; + + /** + * Get a this objects value as a readable formatted string. + * @return the date string + */ + std::string readable_string() const; + + /** + * Find out whether this object's values have been set. + * @return true if this object's internal values are set + */ + bool time_is_set() const; + + /** + * Compare this to another EAC_Time object. + * @return -1 if this object's date is earlier than + * other, +1 in the opposite case, and 0 if both dates are + * equal. + */ + s32bit cmp(const EAC_Time& other) const; + + /** + * Set this' value by a string value. + * @param str a string in the format "yyyy mm dd", + * e.g. "2007 08 01" + */ + void set_to(const std::string& str); + //void set_to(const std::string&, ASN1_Tag); + + /** + * Add the specified number of years to this. + * @param years the number of years to add + */ + void add_years(u32bit years); + + /** + * Add the specified number of months to this. + * @param months the number of months to add + */ + void add_months(u32bit months); + + /** + * Get the year value of this objects. + * @return the year value + */ + u32bit get_year() const; + + /** + * Get the month value of this objects. + * @return the month value + */ + u32bit get_month() const; + + /** + * Get the day value of this objects. + * @return the day value + */ + u32bit get_day() const; + + EAC_Time(u64bit, ASN1_Tag t = ASN1_Tag(0)); + //EAC_Time(const std::string& = ""); + EAC_Time(const std::string&, ASN1_Tag = ASN1_Tag(0)); + EAC_Time(u32bit year, u32bit month, u32bit day, ASN1_Tag = ASN1_Tag(0)); + + virtual ~EAC_Time() {} + private: + SecureVector<byte> encoded_eac_time() const; + bool passes_sanity_check() const; + u32bit year, month, day; + ASN1_Tag tag; + }; + +/** +* This class represents CVC CEDs. Only limited sanity checks of +* the inputted date value are performed. +*/ +class BOTAN_DLL ASN1_Ced : public EAC_Time + { + public: + /** + * Construct a CED from a string value. + * @param str a string in the format "yyyy mm dd", + * e.g. "2007 08 01" + */ + ASN1_Ced(std::string const& str = ""); + + /** + * Construct a CED from a timer value. + * @param time the number of seconds elapsed midnight, 1st + * January 1970 GMT (or 7pm, 31st December 1969 EST) up to the + * desired date + */ + ASN1_Ced(u64bit time); + + /** + * Copy constructor (for general EAC_Time objects). + * @param other the object to copy from + */ + ASN1_Ced(EAC_Time const& other); + //ASN1_Ced(ASN1_Cex const& cex); + }; + + +/** +* This class represents CVC CEXs. Only limited sanity checks of +* the inputted date value are performed. +*/ +class BOTAN_DLL ASN1_Cex : public EAC_Time + { + public: + /** + * Construct a CED from a string value. + * @param str a string in the format "yyyy mm dd", + * e.g. "2007 08 01" + */ + ASN1_Cex(std::string const& str=""); + + /** + * Construct a CED from a timer value. + * @param time the number of seconds elapsed + * midnight, 1st + * January 1970 GMT (or 7pm, 31st December 1969 EST) + * up to the desired date + */ + ASN1_Cex(u64bit time); + + /** + * Copy constructor (for general EAC_Time objects). + * @param other the object to copy from + */ + ASN1_Cex(EAC_Time const& other); + //ASN1_Cex(ASN1_Ced const& ced); + }; + +/** +* Base class for car/chr of cv certificates. +*/ +class BOTAN_DLL ASN1_EAC_String: public ASN1_Object + { + public: + void encode_into(class DER_Encoder&) const; + void decode_from(class BER_Decoder&); + + /** + * Get this objects string value. + * @return the string value + */ + std::string value() const; + + /** + * Get this objects string value. + * @return the string value in iso8859 encoding + */ + std::string iso_8859() const; + + ASN1_Tag tagging() const; + ASN1_EAC_String(const std::string& str, ASN1_Tag the_tag); + + virtual ~ASN1_EAC_String() {} + protected: + bool sanity_check() const; + private: + std::string iso_8859_str; + ASN1_Tag tag; + }; + +/** +* This class represents CARs of CVCs. (String tagged with 2) +*/ +class BOTAN_DLL ASN1_Car : public ASN1_EAC_String + { + public: + /** + * Create a CAR with the specified content. + * @param str the CAR value + */ + ASN1_Car(std::string const& str = ""); + }; + +/** +* This class represents CHRs of CVCs (tag 32) +*/ +class BOTAN_DLL ASN1_Chr : public ASN1_EAC_String + { + public: + /** + * Create a CHR with the specified content. + * @param str the CHR value + */ + ASN1_Chr(std::string const& str = ""); + }; + +/* +* Comparison Operations +*/ +bool operator==(const EAC_Time&, const EAC_Time&); +bool operator!=(const EAC_Time&, const EAC_Time&); +bool operator<=(const EAC_Time&, const EAC_Time&); +bool operator>=(const EAC_Time&, const EAC_Time&); +bool operator>(const EAC_Time&, const EAC_Time&); +bool operator<(const EAC_Time&, const EAC_Time&); + +bool operator==(const ASN1_EAC_String&, const ASN1_EAC_String&); +inline bool operator!=(const ASN1_EAC_String& lhs, const ASN1_EAC_String& rhs) + { + return !(lhs == rhs); + } + +} + +#endif diff --git a/botan/src/cert/cvc/eac_obj.h b/botan/src/cert/cvc/eac_obj.h new file mode 100644 index 0000000..04afd7e --- /dev/null +++ b/botan/src/cert/cvc/eac_obj.h @@ -0,0 +1,131 @@ +/* +* EAC1_1 objects +* (C) 2008 Falko Strenzke +* strenzke@flexsecure.de +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_EAC_OBJ_H__ +#define BOTAN_EAC_OBJ_H__ + +#include <botan/pubkey.h> +#include <botan/x509_key.h> +#include <botan/signed_obj.h> +#include <botan/pubkey_enums.h> +#include <botan/pubkey.h> +#include <botan/parsing.h> +#include <botan/pem.h> +#include <botan/oids.h> +#include <botan/look_pk.h> +#include <botan/ecdsa_sig.h> +#include <string> + +namespace Botan { + +const std::string eac_cvc_emsa("EMSA1_BSI"); + +/* +* TR03110 v1.1 EAC CV Certificate +*/ +template<typename Derived> // CRTP is used enable the call sequence: +class BOTAN_DLL EAC1_1_obj : public EAC_Signed_Object + { + // data members first: + protected: + + ECDSA_Signature m_sig; + + // member functions here: + public: + /** + * Return the signature as a concatenation of the encoded parts. + * @result the concatenated signature + */ + SecureVector<byte> get_concat_sig() const; + + /** + * Verify the signature of this objects. + * @param pub_key the public key to verify the signature with + * @result true if the verification succeeded + */ + virtual bool check_signature(Public_Key& pub_key) const; + + protected: + void init(SharedPtrConverter<DataSource> in); + + static SecureVector<byte> make_signature(PK_Signer* signer, + const MemoryRegion<byte>& tbs_bits, + RandomNumberGenerator& rng); + + virtual ~EAC1_1_obj<Derived>(){} + + }; + +template<typename Derived> SecureVector<byte> EAC1_1_obj<Derived>::get_concat_sig() const + { + return m_sig.get_concatenation(); + } + +template<typename Derived> SecureVector<byte> +EAC1_1_obj<Derived>::make_signature(PK_Signer* signer, + const MemoryRegion<byte>& tbs_bits, + RandomNumberGenerator& rng) + { + // this is the signature as a der sequence + SecureVector<byte> seq_sig = signer->sign_message(tbs_bits, rng); + + ECDSA_Signature sig(decode_seq(seq_sig)); + SecureVector<byte> concat_sig(sig.get_concatenation()); + return concat_sig; + } + +template<typename Derived> void EAC1_1_obj<Derived>::init(SharedPtrConverter<DataSource> in) + { + try + { + Derived::decode_info(in.get_shared(), tbs_bits, m_sig); + } + catch(Decoding_Error) + { + throw Decoding_Error(PEM_label_pref + " decoding failed"); + } + } + +template<typename Derived> +bool EAC1_1_obj<Derived>::check_signature(Public_Key& pub_key) const + { + try + { + std::vector<std::string> sig_info = + split_on(OIDS::lookup(sig_algo.oid), '/'); + + if(sig_info.size() != 2 || sig_info[0] != pub_key.algo_name()) + { + return false; + } + + std::string padding = sig_info[1]; + Signature_Format format = + (pub_key.message_parts() >= 2) ? DER_SEQUENCE : IEEE_1363; + + if(!dynamic_cast<PK_Verifying_wo_MR_Key*>(&pub_key)) + return false; + + std::auto_ptr<ECDSA_Signature_Encoder> enc(new ECDSA_Signature_Encoder(&m_sig)); + SecureVector<byte> seq_sig = enc->signature_bits(); + SecureVector<byte> to_sign = tbs_data(); + + PK_Verifying_wo_MR_Key& sig_key = dynamic_cast<PK_Verifying_wo_MR_Key&>(pub_key); + std::auto_ptr<PK_Verifier> verifier(get_pk_verifier(sig_key, padding, format)); + return verifier->verify_message(to_sign, seq_sig); + } + catch(...) + { + return false; + } + } + +} + +#endif diff --git a/botan/src/cert/cvc/ecdsa_sig.cpp b/botan/src/cert/cvc/ecdsa_sig.cpp new file mode 100644 index 0000000..c33a455 --- /dev/null +++ b/botan/src/cert/cvc/ecdsa_sig.cpp @@ -0,0 +1,64 @@ + +#include <botan/ecdsa_sig.h> +#include <memory> + +namespace Botan { + +ECDSA_Signature::ECDSA_Signature(const BigInt& r, const BigInt& s) + : m_r(r), + m_s(s) + {} + +ECDSA_Signature::ECDSA_Signature(const ECDSA_Signature& other) + : m_r(other.m_r), m_s(other.m_s) + {} + +ECDSA_Signature const& ECDSA_Signature::operator=(const ECDSA_Signature& other) + { + m_r = other.m_r; + m_s = other.m_s; + return *this; + } + +bool operator==(const ECDSA_Signature& lhs, const ECDSA_Signature& rhs) + { + return (lhs.get_r() == rhs.get_r() && lhs.get_s() == rhs.get_s()); + } + +SecureVector<byte> const ECDSA_Signature::get_concatenation() const + { + u32bit enc_len = m_r > m_s ? m_r.bytes() : m_s.bytes(); // use the larger + + SecureVector<byte> sv_r = BigInt::encode_1363(m_r, enc_len); + SecureVector<byte> sv_s = BigInt::encode_1363(m_s, enc_len); + + SecureVector<byte> result(sv_r); + result.append(sv_s); + return result; + } + +ECDSA_Signature const decode_seq(MemoryRegion<byte> const& seq) + { + ECDSA_Signature sig; + + std::auto_ptr<ECDSA_Signature_Decoder> dec(new ECDSA_Signature_Decoder(&sig)); + dec->signature_bits(seq); + return sig; + } + +ECDSA_Signature const decode_concatenation(MemoryRegion<byte> const& concat) + { + if(concat.size() % 2 != 0) + throw Invalid_Argument("Erroneous length of signature"); + + u32bit rs_len = concat.size()/2; + SecureVector<byte> sv_r; + SecureVector<byte> sv_s; + sv_r.set(concat.begin(), rs_len); + sv_s.set(&concat[rs_len], rs_len); + BigInt r = BigInt::decode(sv_r, sv_r.size()); + BigInt s = BigInt::decode(sv_s, sv_s.size()); + return ECDSA_Signature(r, s); + } + +} diff --git a/botan/src/cert/cvc/ecdsa_sig.h b/botan/src/cert/cvc/ecdsa_sig.h new file mode 100644 index 0000000..15015c7 --- /dev/null +++ b/botan/src/cert/cvc/ecdsa_sig.h @@ -0,0 +1,88 @@ +/* +* ECDSA +* (C) 2007 Falko Strenzke, FlexSecure GmbH +* (C) 2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ECDSA_SIGNATURE_H__ +#define BOTAN_ECDSA_SIGNATURE_H__ + +#include <botan/bigint.h> +#include <botan/der_enc.h> +#include <botan/ber_dec.h> + +namespace Botan { + +class BOTAN_DLL ECDSA_Signature + { + public: + friend class ECDSA_Signature_Decoder; + + ECDSA_Signature() {} + ECDSA_Signature(const BigInt& r, const BigInt& s); + ECDSA_Signature(ECDSA_Signature const& other); + ECDSA_Signature const& operator=(ECDSA_Signature const& other); + + const BigInt& get_r() const { return m_r; } + const BigInt& get_s() const { return m_s; } + + /** + * return the r||s + */ + SecureVector<byte> const get_concatenation() const; + private: + BigInt m_r; + BigInt m_s; + }; + +/* Equality of ECDSA_Signature */ +bool operator==(const ECDSA_Signature& lhs, const ECDSA_Signature& rhs); +inline bool operator!=(const ECDSA_Signature& lhs, const ECDSA_Signature& rhs) + { + return !(lhs == rhs); + } + +class BOTAN_DLL ECDSA_Signature_Decoder + { + public: + void signature_bits(const MemoryRegion<byte>& bits) + { + BER_Decoder(bits) + .start_cons(SEQUENCE) + .decode(m_signature->m_r) + .decode(m_signature->m_s) + .verify_end() + .end_cons(); + } + ECDSA_Signature_Decoder(ECDSA_Signature* signature) : m_signature(signature) + {} + private: + ECDSA_Signature* m_signature; + }; + +class BOTAN_DLL ECDSA_Signature_Encoder + { + public: + MemoryVector<byte> signature_bits() const + { + return DER_Encoder() + .start_cons(SEQUENCE) + .encode(m_signature->get_r()) + .encode(m_signature->get_s()) + .end_cons() + .get_contents(); + } + ECDSA_Signature_Encoder(const ECDSA_Signature* signature) : m_signature(signature) + {} + private: + const ECDSA_Signature* m_signature; + }; + +ECDSA_Signature const decode_seq(MemoryRegion<byte> const& seq); +ECDSA_Signature const decode_concatenation(MemoryRegion<byte> const& concatenation); + +} + +#endif diff --git a/botan/src/cert/cvc/freestore.h b/botan/src/cert/cvc/freestore.h new file mode 100644 index 0000000..f95afa8 --- /dev/null +++ b/botan/src/cert/cvc/freestore.h @@ -0,0 +1,85 @@ +/** +* (C) 2007 Christoph Ludwig +* ludwig@fh-worms.de +**/ + +#ifndef BOTAN_FREESTORE_H__ +#define BOTAN_FREESTORE_H__ + +#include <botan/build.h> + +#if defined(BOTAN_USE_STD_TR1) + #include <tr1/memory> +#elif defined(BOTAN_USE_BOOST_TR1) + #include <boost/tr1/memory.hpp> +#else + #error "Please choose a TR1 implementation in build.h" +#endif + +namespace Botan { + +/** +* This class is intended as an function call parameter type and +* enables convenient automatic conversions between plain and smart +* pointer types. It internally stores a SharedPointer which can be +* accessed. +* +* Distributed under the terms of the Botan license +*/ +template<typename T> +class BOTAN_DLL SharedPtrConverter + { + public: + typedef std::tr1::shared_ptr<T> SharedPtr; + + /** + * Construct a null pointer equivalent object. + */ + SharedPtrConverter() : ptr() {} + + /** + * Copy constructor. + */ + SharedPtrConverter(SharedPtrConverter const& other) : + ptr(other.ptr) {} + + /** + * Construct a converter object from another pointer type. + * @param p the pointer which shall be set as the internally stored + * pointer value of this converter. + */ + template<typename Ptr> + SharedPtrConverter(Ptr p) + : ptr(p) {} + + /** + * Get the internally stored shared pointer. + * @return the internally stored shared pointer + */ + SharedPtr const& get_ptr() const { return this->ptr; } + + /** + * Get the internally stored shared pointer. + * @return the internally stored shared pointer + */ + SharedPtr get_ptr() { return this->ptr; } + + /** + * Get the internally stored shared pointer. + * @return the internally stored shared pointer + */ + SharedPtr const& get_shared() const { return this->ptr; } + + /** + * Get the internally stored shared pointer. + * @return the internally stored shared pointer + */ + SharedPtr get_shared() { return this->ptr; } + + private: + SharedPtr ptr; + }; + +} + +#endif diff --git a/botan/src/cert/cvc/info.txt b/botan/src/cert/cvc/info.txt new file mode 100644 index 0000000..e3e11f5 --- /dev/null +++ b/botan/src/cert/cvc/info.txt @@ -0,0 +1,44 @@ +realname "Card Verifiable Certificates" + +define CARD_VERIFIABLE_CERTIFICATES + +uses_tr1 yes + +load_on auto + +<add> +asn1_eac_str.cpp +asn1_eac_tm.cpp +ecdsa_sig.cpp +ecdsa_sig.h +cvc_ado.cpp +cvc_ado.h +cvc_ca.cpp +cvc_ca.h +cvc_cert.cpp +cvc_cert.h +cvc_gen_cert.h +cvc_key.h +cvc_req.cpp +cvc_req.h +cvc_self.cpp +cvc_self.h +eac_asn_obj.h +eac_obj.h +signed_obj.cpp +signed_obj.h +freestore.h +</add> + +<requires> +asn1 +bigint +ecdsa +filters +libstate +oid_lookup +pem +pk_codecs +pubkey +x509 +</requires> diff --git a/botan/src/cert/cvc/signed_obj.cpp b/botan/src/cert/cvc/signed_obj.cpp new file mode 100644 index 0000000..4a08ed0 --- /dev/null +++ b/botan/src/cert/cvc/signed_obj.cpp @@ -0,0 +1,67 @@ +/* +* X.509 SIGNED Object +* (C) 1999-2007 Jack Lloyd +* 2007 FlexSecure GmbH +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/signed_obj.h> + +namespace Botan { + +/* +* Return a BER encoded X.509 object +*/ +SecureVector<byte> EAC_Signed_Object::BER_encode() const + { + Pipe ber; + ber.start_msg(); + encode(ber, RAW_BER); + ber.end_msg(); + return ber.read_all(); + } + +/* +* Return a PEM encoded X.509 object +*/ +std::string EAC_Signed_Object::PEM_encode() const + { + Pipe pem; + pem.start_msg(); + encode(pem, PEM); + pem.end_msg(); + return pem.read_all_as_string(); + } + +/* +* Return the algorithm used to sign this object +*/ +AlgorithmIdentifier EAC_Signed_Object::signature_algorithm() const + { + return sig_algo; + } + +/* +* Try to decode the actual information +*/ +void EAC_Signed_Object::do_decode() + { + try { + force_decode(); + } + catch(Decoding_Error& e) + { + const std::string what = e.what(); + throw Decoding_Error(PEM_label_pref + " decoding failed (" + + what.substr(23, std::string::npos) + ")"); + } + catch(Invalid_Argument& e) + { + const std::string what = e.what(); + throw Decoding_Error(PEM_label_pref + " decoding failed (" + + what.substr(7, std::string::npos) + ")"); + } + } + +} diff --git a/botan/src/cert/cvc/signed_obj.h b/botan/src/cert/cvc/signed_obj.h new file mode 100644 index 0000000..3c233d2 --- /dev/null +++ b/botan/src/cert/cvc/signed_obj.h @@ -0,0 +1,93 @@ +/* +* EAC SIGNED Object +* (C) 2007 FlexSecure GmbH +* 2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_EAC_SIGNED_OBJECT_H__ +#define BOTAN_EAC_SIGNED_OBJECT_H__ + +#include <botan/asn1_obj.h> +#include <botan/pubkey_enums.h> +#include <botan/freestore.h> +#include <botan/pipe.h> +#include <vector> + +namespace Botan { + +/** +* This class represents abstract signed EAC object +*/ +class BOTAN_DLL EAC_Signed_Object + { + public: + /** + * Get the TBS (to-be-signed) data in this object. + * @return the DER encoded TBS data of this object + */ + virtual SecureVector<byte> tbs_data() const = 0; + + /** + * Get the signature of this object as a concatenation, i.e. if the + * signature consists of multiple parts (like in the case of ECDSA) + * these will be concatenated. + * @return the signature as a concatenation of its parts + */ + + /* + NOTE: this is here only because abstract signature objects have + not yet been introduced + */ + virtual SecureVector<byte> get_concat_sig() const = 0; + + /** + * Get the signature algorithm identifier used to sign this object. + * @result the signature algorithm identifier + */ + AlgorithmIdentifier signature_algorithm() const; + + /** + * Check the signature of this object. + * @param key the public key associated with this signed object + * @return true if the signature was created by the private key + * associated with this public key + */ + virtual bool check_signature(class Public_Key&) const = 0; + + /** + * Write this object DER encoded into a specified pipe. + * @param pipe the pipe to write the encoded object to + * @param enc the encoding type to use + */ + virtual void encode(Pipe&, X509_Encoding = PEM) const = 0; + + /** + * BER encode this object. + * @return the result containing the BER representation of this object. + */ + SecureVector<byte> BER_encode() const; + + /** + * PEM encode this object. + * @return the result containing the PEM representation of this object. + */ + std::string PEM_encode() const; + + virtual ~EAC_Signed_Object() {} + protected: + void do_decode(); + EAC_Signed_Object() {} + + AlgorithmIdentifier sig_algo; + SecureVector<byte> tbs_bits; + std::string PEM_label_pref; + std::vector<std::string> PEM_labels_allowed; + private: + virtual void force_decode() = 0; + }; + +} + +#endif diff --git a/botan/src/cert/x509/certstor.cpp b/botan/src/cert/x509/certstor.cpp new file mode 100644 index 0000000..96f2699 --- /dev/null +++ b/botan/src/cert/x509/certstor.cpp @@ -0,0 +1,48 @@ +/* +* Certificate Store +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/certstor.h> + +namespace Botan { + +/* +* Search by name +*/ +std::vector<X509_Certificate> +Certificate_Store::by_name(const std::string&) const + { + return std::vector<X509_Certificate>(); + } + +/* +* Search by email +*/ +std::vector<X509_Certificate> +Certificate_Store::by_email(const std::string&) const + { + return std::vector<X509_Certificate>(); + } + +/* +* Search by X.500 distinguished name +*/ +std::vector<X509_Certificate> +Certificate_Store::by_dn(const X509_DN&) const + { + return std::vector<X509_Certificate>(); + } + +/* +* Find any CRLs that might be useful +*/ +std::vector<X509_CRL> +Certificate_Store::get_crls_for(const X509_Certificate&) const + { + return std::vector<X509_CRL>(); + } + +} diff --git a/botan/src/cert/x509/certstor.h b/botan/src/cert/x509/certstor.h new file mode 100644 index 0000000..d5004e3 --- /dev/null +++ b/botan/src/cert/x509/certstor.h @@ -0,0 +1,39 @@ +/* +* Certificate Store +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_CERT_STORE_H__ +#define BOTAN_CERT_STORE_H__ + +#include <botan/x509cert.h> +#include <botan/x509_crl.h> + +namespace Botan { + +/* +* Certificate Store Interface +*/ +class BOTAN_DLL Certificate_Store + { + public: + virtual std::vector<X509_Certificate> + by_SKID(const MemoryRegion<byte>&) const = 0; + + virtual std::vector<X509_Certificate> by_name(const std::string&) const; + virtual std::vector<X509_Certificate> by_email(const std::string&) const; + virtual std::vector<X509_Certificate> by_dn(const X509_DN&) const; + + virtual std::vector<X509_CRL> + get_crls_for(const X509_Certificate&) const; + + virtual Certificate_Store* clone() const = 0; + + virtual ~Certificate_Store() {} + }; + +} + +#endif diff --git a/botan/src/cert/x509/crl_ent.cpp b/botan/src/cert/x509/crl_ent.cpp new file mode 100644 index 0000000..afea8cf --- /dev/null +++ b/botan/src/cert/x509/crl_ent.cpp @@ -0,0 +1,107 @@ +/* +* CRL Entry +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/crl_ent.h> +#include <botan/x509_ext.h> +#include <botan/der_enc.h> +#include <botan/ber_dec.h> +#include <botan/bigint.h> +#include <botan/oids.h> +#include <botan/util.h> + +namespace Botan { + +/* +* Create a CRL_Entry +*/ +CRL_Entry::CRL_Entry(bool t_on_unknown_crit) : + throw_on_unknown_critical(t_on_unknown_crit) + { + reason = UNSPECIFIED; + } + +/* +* Create a CRL_Entry +*/ +CRL_Entry::CRL_Entry(const X509_Certificate& cert, CRL_Code why) : + throw_on_unknown_critical(false) + { + serial = cert.serial_number(); + time = X509_Time(system_time()); + reason = why; + } + +/* +* Compare two CRL_Entrys for equality +*/ +bool operator==(const CRL_Entry& a1, const CRL_Entry& a2) + { + if(a1.serial_number() != a2.serial_number()) + return false; + if(a1.expire_time() != a2.expire_time()) + return false; + if(a1.reason_code() != a2.reason_code()) + return false; + return true; + } + +/* +* Compare two CRL_Entrys for inequality +*/ +bool operator!=(const CRL_Entry& a1, const CRL_Entry& a2) + { + return !(a1 == a2); + } + +/* +* Compare two CRL_Entrys +*/ +bool operator<(const CRL_Entry& a1, const CRL_Entry& a2) + { + return (a1.expire_time().cmp(a2.expire_time()) < 0); + } + +/* +* DER encode a CRL_Entry +*/ +void CRL_Entry::encode_into(DER_Encoder& der) const + { + Extensions extensions; + + extensions.add(new Cert_Extension::CRL_ReasonCode(reason)); + + der.start_cons(SEQUENCE) + .encode(BigInt::decode(serial, serial.size())) + .encode(time) + .encode(extensions) + .end_cons(); + } + +/* +* Decode a BER encoded CRL_Entry +*/ +void CRL_Entry::decode_from(BER_Decoder& source) + { + BigInt serial_number_bn; + + source.start_cons(SEQUENCE) + .decode(serial_number_bn) + .decode(time); + + if(source.more_items()) + { + Extensions extensions(throw_on_unknown_critical); + source.decode(extensions); + Data_Store info; + extensions.contents_to(info, info); + reason = CRL_Code(info.get1_u32bit("X509v3.CRLReasonCode")); + } + + serial = BigInt::encode(serial_number_bn); + } + +} diff --git a/botan/src/cert/x509/crl_ent.h b/botan/src/cert/x509/crl_ent.h new file mode 100644 index 0000000..050356c --- /dev/null +++ b/botan/src/cert/x509/crl_ent.h @@ -0,0 +1,78 @@ +/* +* CRL Entry +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_CRL_ENTRY_H__ +#define BOTAN_CRL_ENTRY_H__ + +#include <botan/x509cert.h> + +namespace Botan { + +/** +* This class represents CRL entries +*/ +class BOTAN_DLL CRL_Entry : public ASN1_Object + { + public: + void encode_into(class DER_Encoder&) const; + void decode_from(class BER_Decoder&); + + /** + * Get the serial number of the certificate associated with this entry. + * @return the certificate's serial number + */ + MemoryVector<byte> serial_number() const { return serial; } + + /** + * Get the revocation date of the certificate associated with this entry + * @return the certificate's revocation date + */ + X509_Time expire_time() const { return time; } + + /** + * Get the entries reason code + * @return the reason code + */ + CRL_Code reason_code() const { return reason; } + + /** + * Construct an empty CRL entry. + */ + CRL_Entry(bool throw_on_unknown_critical_extension = false); + + /** + * Construct an CRL entry. + * @param cert the certificate to revoke + * @param reason the reason code to set in the entry + */ + CRL_Entry(const X509_Certificate&, CRL_Code = UNSPECIFIED); + + private: + bool throw_on_unknown_critical; + MemoryVector<byte> serial; + X509_Time time; + CRL_Code reason; + }; + +/** +* Test two CRL entries for equality in all fields. +*/ +BOTAN_DLL bool operator==(const CRL_Entry&, const CRL_Entry&); + +/** +* Test two CRL entries for inequality in at least one field. +*/ +BOTAN_DLL bool operator!=(const CRL_Entry&, const CRL_Entry&); + +/** +* Order two entries based on the revocation date. +*/ +BOTAN_DLL bool operator<(const CRL_Entry&, const CRL_Entry&); + +} + +#endif diff --git a/botan/src/cert/x509/info.txt b/botan/src/cert/x509/info.txt new file mode 100644 index 0000000..552e2aa --- /dev/null +++ b/botan/src/cert/x509/info.txt @@ -0,0 +1,45 @@ +realname "X.509" + +define X509 + +load_on auto + +<add> +certstor.h +certstor.cpp +crl_ent.cpp +crl_ent.h +pkcs10.h +pkcs10.cpp +x509_ca.cpp +x509_ca.h +x509_crl.cpp +x509_crl.h +x509_ext.cpp +x509_ext.h +x509_obj.cpp +x509_obj.h +x509cert.cpp +x509cert.h +x509find.cpp +x509find.h +x509opt.cpp +x509self.cpp +x509self.h +x509stor.cpp +x509stor.h +</add> + +<requires> +asn1 +bigint +datastor +filters +libstate +oid_lookup +pem +pk_codecs +pubkey +rng +sha1 +</requires> diff --git a/botan/src/cert/x509/pkcs10.cpp b/botan/src/cert/x509/pkcs10.cpp new file mode 100644 index 0000000..5617cec --- /dev/null +++ b/botan/src/cert/x509/pkcs10.cpp @@ -0,0 +1,200 @@ +/* +* PKCS #10 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/pkcs10.h> +#include <botan/der_enc.h> +#include <botan/ber_dec.h> +#include <botan/parsing.h> +#include <botan/x509stor.h> +#include <botan/x509_ext.h> +#include <botan/oids.h> +#include <botan/pem.h> + +namespace Botan { + +/* +* PKCS10_Request Constructor +*/ +PKCS10_Request::PKCS10_Request(DataSource& in) : + X509_Object(in, "CERTIFICATE REQUEST/NEW CERTIFICATE REQUEST") + { + do_decode(); + } + +/* +* PKCS10_Request Constructor +*/ +PKCS10_Request::PKCS10_Request(const std::string& in) : + X509_Object(in, "CERTIFICATE REQUEST/NEW CERTIFICATE REQUEST") + { + do_decode(); + } + +/* +* Deocde the CertificateRequestInfo +*/ +void PKCS10_Request::force_decode() + { + BER_Decoder cert_req_info(tbs_bits); + + u32bit version; + cert_req_info.decode(version); + if(version != 0) + throw Decoding_Error("Unknown version code in PKCS #10 request: " + + to_string(version)); + + X509_DN dn_subject; + cert_req_info.decode(dn_subject); + + info.add(dn_subject.contents()); + + BER_Object public_key = cert_req_info.get_next_object(); + if(public_key.type_tag != SEQUENCE || public_key.class_tag != CONSTRUCTED) + throw BER_Bad_Tag("PKCS10_Request: Unexpected tag for public key", + public_key.type_tag, public_key.class_tag); + + info.add("X509.Certificate.public_key", + PEM_Code::encode( + ASN1::put_in_sequence(public_key.value), + "PUBLIC KEY" + ) + ); + + BER_Object attr_bits = cert_req_info.get_next_object(); + + if(attr_bits.type_tag == 0 && + attr_bits.class_tag == ASN1_Tag(CONSTRUCTED | CONTEXT_SPECIFIC)) + { + BER_Decoder attributes(attr_bits.value); + while(attributes.more_items()) + { + Attribute attr; + attributes.decode(attr); + handle_attribute(attr); + } + attributes.verify_end(); + } + else if(attr_bits.type_tag != NO_OBJECT) + throw BER_Bad_Tag("PKCS10_Request: Unexpected tag for attributes", + attr_bits.type_tag, attr_bits.class_tag); + + cert_req_info.verify_end(); + + X509_Code sig_check = X509_Store::check_sig(*this, subject_public_key()); + if(sig_check != VERIFIED) + throw Decoding_Error("PKCS #10 request: Bad signature detected"); + } + +/* +* Handle attributes in a PKCS #10 request +*/ +void PKCS10_Request::handle_attribute(const Attribute& attr) + { + BER_Decoder value(attr.parameters); + + if(attr.oid == OIDS::lookup("PKCS9.EmailAddress")) + { + ASN1_String email; + value.decode(email); + info.add("RFC822", email.value()); + } + else if(attr.oid == OIDS::lookup("PKCS9.ChallengePassword")) + { + ASN1_String challenge_password; + value.decode(challenge_password); + info.add("PKCS9.ChallengePassword", challenge_password.value()); + } + else if(attr.oid == OIDS::lookup("PKCS9.ExtensionRequest")) + { + Extensions extensions; + value.decode(extensions).verify_end(); + + Data_Store issuer_info; + extensions.contents_to(info, issuer_info); + } + } + +/* +* Return the challenge password (if any) +*/ +std::string PKCS10_Request::challenge_password() const + { + return info.get1("PKCS9.ChallengePassword"); + } + +/* +* Return the name of the requestor +*/ +X509_DN PKCS10_Request::subject_dn() const + { + return create_dn(info); + } + +/* +* Return the public key of the requestor +*/ +MemoryVector<byte> PKCS10_Request::raw_public_key() const + { + DataSource_Memory source(info.get1("X509.Certificate.public_key")); + return PEM_Code::decode_check_label(source, "PUBLIC KEY"); + } + +/* +* Return the public key of the requestor +*/ +Public_Key* PKCS10_Request::subject_public_key() const + { + DataSource_Memory source(info.get1("X509.Certificate.public_key")); + return X509::load_key(source); + } + +/* +* Return the alternative names of the requestor +*/ +AlternativeName PKCS10_Request::subject_alt_name() const + { + return create_alt_name(info); + } + +/* +* Return the key constraints (if any) +*/ +Key_Constraints PKCS10_Request::constraints() const + { + return Key_Constraints(info.get1_u32bit("X509v3.KeyUsage", NO_CONSTRAINTS)); + } + +/* +* Return the extendend key constraints (if any) +*/ +std::vector<OID> PKCS10_Request::ex_constraints() const + { + std::vector<std::string> oids = info.get("X509v3.ExtendedKeyUsage"); + + std::vector<OID> result; + for(u32bit j = 0; j != oids.size(); ++j) + result.push_back(OID(oids[j])); + return result; + } + +/* +* Return is a CA certificate is requested +*/ +bool PKCS10_Request::is_CA() const + { + return info.get1_u32bit("X509v3.BasicConstraints.is_ca"); + } + +/* +* Return the desired path limit (if any) +*/ +u32bit PKCS10_Request::path_limit() const + { + return info.get1_u32bit("X509v3.BasicConstraints.path_constraint", 0); + } + +} diff --git a/botan/src/cert/x509/pkcs10.h b/botan/src/cert/x509/pkcs10.h new file mode 100644 index 0000000..9b435de --- /dev/null +++ b/botan/src/cert/x509/pkcs10.h @@ -0,0 +1,101 @@ +/* +* PKCS #10 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_PKCS10_H__ +#define BOTAN_PKCS10_H__ + +#include <botan/x509_obj.h> +#include <botan/pkcs8.h> +#include <botan/datastor.h> +#include <vector> + +namespace Botan { + +/** +* PKCS #10 Certificate Request. +*/ +class BOTAN_DLL PKCS10_Request : public X509_Object + { + public: + /** + * Get the subject public key. + * @return the subject public key + */ + Public_Key* subject_public_key() const; + + /** + * Get the raw DER encoded public key. + * @return the raw DER encoded public key + */ + MemoryVector<byte> raw_public_key() const; + + /** + * Get the subject DN. + * @return the subject DN + */ + X509_DN subject_dn() const; + + /** + * Get the subject alternative name. + * @return the subject alternative name. + */ + AlternativeName subject_alt_name() const; + + /** + * Get the key constraints for the key associated with this + * PKCS#10 object. + * @return the key constraints + */ + Key_Constraints constraints() const; + + /** + * Get the extendend key constraints (if any). + * @return the extended key constraints + */ + std::vector<OID> ex_constraints() const; + + /** + * Find out whether this is a CA request. + * @result true if it is a CA request, false otherwise. + */ + bool is_CA() const; + + /** + * Return the constraint on the path length defined + * in the BasicConstraints extension. + * @return the path limit + */ + u32bit path_limit() const; + + /** + * Get the challenge password for this request + * @return the challenge password for this request + */ + std::string challenge_password() const; + + /** + * Create a PKCS#10 Request from a data source. + * @param source the data source providing the DER encoded request + */ + PKCS10_Request(DataSource& source); + + /** + * Create a PKCS#10 Request from a file. + * @param filename the name of the file containing the DER or PEM + * encoded request file + */ + PKCS10_Request(const std::string& filename); + private: + void force_decode(); + void handle_attribute(const Attribute&); + + Data_Store info; + }; + +} + +#endif diff --git a/botan/src/cert/x509/x509_ca.cpp b/botan/src/cert/x509/x509_ca.cpp new file mode 100644 index 0000000..41e3147 --- /dev/null +++ b/botan/src/cert/x509/x509_ca.cpp @@ -0,0 +1,284 @@ +/* +* X.509 Certificate Authority +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/x509_ca.h> +#include <botan/x509stor.h> +#include <botan/der_enc.h> +#include <botan/ber_dec.h> +#include <botan/look_pk.h> +#include <botan/bigint.h> +#include <botan/parsing.h> +#include <botan/oids.h> +#include <botan/util.h> +#include <algorithm> +#include <typeinfo> +#include <iterator> +#include <memory> +#include <set> + +namespace Botan { + +/* +* Load the certificate and private key +*/ +X509_CA::X509_CA(const X509_Certificate& c, + const Private_Key& key) : cert(c) + { + const Private_Key* key_pointer = &key; + if(!dynamic_cast<const PK_Signing_Key*>(key_pointer)) + throw Invalid_Argument("X509_CA: " + key.algo_name() + " cannot sign"); + + if(!cert.is_CA_cert()) + throw Invalid_Argument("X509_CA: This certificate is not for a CA"); + + signer = choose_sig_format(key, ca_sig_algo); + } + +/* +* Sign a PKCS #10 certificate request +*/ +X509_Certificate X509_CA::sign_request(const PKCS10_Request& req, + RandomNumberGenerator& rng, + const X509_Time& not_before, + const X509_Time& not_after) + { + Key_Constraints constraints; + if(req.is_CA()) + constraints = Key_Constraints(KEY_CERT_SIGN | CRL_SIGN); + else + { + std::auto_ptr<Public_Key> key(req.subject_public_key()); + constraints = X509::find_constraints(*key, req.constraints()); + } + + Extensions extensions; + + extensions.add(new Cert_Extension::Authority_Key_ID(cert.subject_key_id())); + extensions.add(new Cert_Extension::Subject_Key_ID(req.raw_public_key())); + + extensions.add( + new Cert_Extension::Basic_Constraints(req.is_CA(), req.path_limit())); + + extensions.add(new Cert_Extension::Key_Usage(constraints)); + extensions.add( + new Cert_Extension::Extended_Key_Usage(req.ex_constraints())); + + extensions.add( + new Cert_Extension::Subject_Alternative_Name(req.subject_alt_name())); + + return make_cert(signer, rng, ca_sig_algo, req.raw_public_key(), + not_before, not_after, + cert.subject_dn(), req.subject_dn(), + extensions); + } + +/* +* Create a new certificate +*/ +X509_Certificate X509_CA::make_cert(PK_Signer* signer, + RandomNumberGenerator& rng, + const AlgorithmIdentifier& sig_algo, + const MemoryRegion<byte>& pub_key, + const X509_Time& not_before, + const X509_Time& not_after, + const X509_DN& issuer_dn, + const X509_DN& subject_dn, + const Extensions& extensions) + { + const u32bit X509_CERT_VERSION = 3; + const u32bit SERIAL_BITS = 128; + + BigInt serial_no(rng, SERIAL_BITS); + + DataSource_Memory source(X509_Object::make_signed(signer, rng, sig_algo, + DER_Encoder().start_cons(SEQUENCE) + .start_explicit(0) + .encode(X509_CERT_VERSION-1) + .end_explicit() + + .encode(serial_no) + + .encode(sig_algo) + .encode(issuer_dn) + + .start_cons(SEQUENCE) + .encode(not_before) + .encode(not_after) + .end_cons() + + .encode(subject_dn) + .raw_bytes(pub_key) + + .start_explicit(3) + .start_cons(SEQUENCE) + .encode(extensions) + .end_cons() + .end_explicit() + .end_cons() + .get_contents() + )); + + return X509_Certificate(source); + } + +/* +* Create a new, empty CRL +*/ +X509_CRL X509_CA::new_crl(RandomNumberGenerator& rng, + u32bit next_update) const + { + std::vector<CRL_Entry> empty; + return make_crl(empty, 1, next_update, rng); + } + +/* +* Update a CRL with new entries +*/ +X509_CRL X509_CA::update_crl(const X509_CRL& crl, + const std::vector<CRL_Entry>& new_revoked, + RandomNumberGenerator& rng, + u32bit next_update) const + { + std::vector<CRL_Entry> already_revoked = crl.get_revoked(); + std::vector<CRL_Entry> all_revoked; + + X509_Store store; + store.add_cert(cert, true); + if(store.add_crl(crl) != VERIFIED) + throw Invalid_Argument("X509_CA::update_crl: Invalid CRL provided"); + + std::set<SecureVector<byte> > removed_from_crl; + for(u32bit j = 0; j != new_revoked.size(); ++j) + { + if(new_revoked[j].reason_code() == DELETE_CRL_ENTRY) + removed_from_crl.insert(new_revoked[j].serial_number()); + else + all_revoked.push_back(new_revoked[j]); + } + + for(u32bit j = 0; j != already_revoked.size(); ++j) + { + std::set<SecureVector<byte> >::const_iterator i; + i = removed_from_crl.find(already_revoked[j].serial_number()); + + if(i == removed_from_crl.end()) + all_revoked.push_back(already_revoked[j]); + } + std::sort(all_revoked.begin(), all_revoked.end()); + + std::vector<CRL_Entry> cert_list; + std::unique_copy(all_revoked.begin(), all_revoked.end(), + std::back_inserter(cert_list)); + + return make_crl(cert_list, crl.crl_number() + 1, next_update, rng); + } + +/* +* Create a CRL +*/ +X509_CRL X509_CA::make_crl(const std::vector<CRL_Entry>& revoked, + u32bit crl_number, u32bit next_update, + RandomNumberGenerator& rng) const + { + const u32bit X509_CRL_VERSION = 2; + + if(next_update == 0) + next_update = timespec_to_u32bit("7d"); + + // Totally stupid: ties encoding logic to the return of std::time!! + const u64bit current_time = system_time(); + + Extensions extensions; + extensions.add( + new Cert_Extension::Authority_Key_ID(cert.subject_key_id())); + extensions.add(new Cert_Extension::CRL_Number(crl_number)); + + DataSource_Memory source(X509_Object::make_signed(signer, rng, ca_sig_algo, + DER_Encoder().start_cons(SEQUENCE) + .encode(X509_CRL_VERSION-1) + .encode(ca_sig_algo) + .encode(cert.issuer_dn()) + .encode(X509_Time(current_time)) + .encode(X509_Time(current_time + next_update)) + .encode_if(revoked.size() > 0, + DER_Encoder() + .start_cons(SEQUENCE) + .encode_list(revoked) + .end_cons() + ) + .start_explicit(0) + .start_cons(SEQUENCE) + .encode(extensions) + .end_cons() + .end_explicit() + .end_cons() + .get_contents() + )); + + return X509_CRL(source); + } + +/* +* Return the CA's certificate +*/ +X509_Certificate X509_CA::ca_certificate() const + { + return cert; + } + +/* +* X509_CA Destructor +*/ +X509_CA::~X509_CA() + { + delete signer; + } + +/* +* Choose a signing format for the key +*/ +PK_Signer* choose_sig_format(const Private_Key& key, + AlgorithmIdentifier& sig_algo) + { + std::string padding; + Signature_Format format; + + const std::string algo_name = key.algo_name(); + + if(algo_name == "RSA") + { + padding = "EMSA3(SHA-160)"; + format = IEEE_1363; + } + else if(algo_name == "DSA") + { + padding = "EMSA1(SHA-160)"; + format = DER_SEQUENCE; + } + else if(algo_name == "ECDSA") + { + padding = "EMSA1_BSI(SHA-160)"; + format = IEEE_1363; + } + else + throw Invalid_Argument("Unknown X.509 signing key type: " + algo_name); + + sig_algo.oid = OIDS::lookup(algo_name + "/" + padding); + + std::auto_ptr<X509_Encoder> encoding(key.x509_encoder()); + if(!encoding.get()) + throw Encoding_Error("Key " + algo_name + " does not support " + "X.509 encoding"); + + sig_algo.parameters = encoding->alg_id().parameters; + + const PK_Signing_Key& sig_key = dynamic_cast<const PK_Signing_Key&>(key); + + return get_pk_signer(sig_key, padding, format); + } + +} diff --git a/botan/src/cert/x509/x509_ca.h b/botan/src/cert/x509/x509_ca.h new file mode 100644 index 0000000..ef2a8d1 --- /dev/null +++ b/botan/src/cert/x509/x509_ca.h @@ -0,0 +1,108 @@ +/* +* X.509 Certificate Authority +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_X509_CA_H__ +#define BOTAN_X509_CA_H__ + +#include <botan/x509cert.h> +#include <botan/x509_crl.h> +#include <botan/x509_ext.h> +#include <botan/pkcs8.h> +#include <botan/pkcs10.h> +#include <botan/pubkey.h> + +namespace Botan { + +/** +* This class represents X.509 Certificate Authorities (CAs). +*/ +class BOTAN_DLL X509_CA + { + public: + + /** + * Sign a PKCS#10 Request. + * @param req the request to sign + * @param rng the rng to use + * @param not_before the starting time for the certificate + * @param not_after the expiration time for the certificate + * @return the resulting certificate + */ + X509_Certificate sign_request(const PKCS10_Request& req, + RandomNumberGenerator& rng, + const X509_Time& not_before, + const X509_Time& not_after); + + /** + * Get the certificate of this CA. + * @return the CA certificate + */ + X509_Certificate ca_certificate() const; + + /** + * Create a new and empty CRL for this CA. + * @param rng the random number generator to use + * @param next_update the time to set in next update in seconds + * as the offset from the current time + * @return the new CRL + */ + X509_CRL new_crl(RandomNumberGenerator& rng, u32bit = 0) const; + + /** + * Create a new CRL by with additional entries. + * @param last_crl the last CRL of this CA to add the new entries to + * @param new_entries contains the new CRL entries to be added to the CRL + * @param rng the random number generator to use + * @param next_update the time to set in next update in seconds + * as the offset from the current time + */ + X509_CRL update_crl(const X509_CRL& last_crl, + const std::vector<CRL_Entry>& new_entries, + RandomNumberGenerator& rng, + u32bit next_update = 0) const; + + static X509_Certificate make_cert(PK_Signer*, + RandomNumberGenerator&, + const AlgorithmIdentifier&, + const MemoryRegion<byte>&, + const X509_Time&, const X509_Time&, + const X509_DN&, const X509_DN&, + const Extensions&); + + /** + * Create a new CA object. + * @param ca_certificate the certificate of the CA + * @param key the private key of the CA + */ + X509_CA(const X509_Certificate& ca_certificate, const Private_Key& key); + ~X509_CA(); + private: + X509_CA(const X509_CA&) {} + X509_CA& operator=(const X509_CA&) { return (*this); } + + X509_CRL make_crl(const std::vector<CRL_Entry>&, + u32bit, u32bit, RandomNumberGenerator&) const; + + AlgorithmIdentifier ca_sig_algo; + X509_Certificate cert; + PK_Signer* signer; + }; + +/** +* Choose the default signature format for a certain public key signature +* scheme. +* @param key will be the key to choose a padding scheme for +* @param alg_id will be set to the chosen scheme +* @return A PK_Signer object for generating signatures +*/ +BOTAN_DLL PK_Signer* choose_sig_format(const Private_Key& key, + AlgorithmIdentifier& alg_id); + + +} + +#endif diff --git a/botan/src/cert/x509/x509_crl.cpp b/botan/src/cert/x509/x509_crl.cpp new file mode 100644 index 0000000..f6a344d --- /dev/null +++ b/botan/src/cert/x509/x509_crl.cpp @@ -0,0 +1,147 @@ +/* +* X.509 CRL +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/x509_crl.h> +#include <botan/x509_ext.h> +#include <botan/ber_dec.h> +#include <botan/parsing.h> +#include <botan/bigint.h> +#include <botan/oids.h> + +namespace Botan { + +/* +* Load a X.509 CRL +*/ +X509_CRL::X509_CRL(DataSource& in, bool touc) : + X509_Object(in, "X509 CRL/CRL"), throw_on_unknown_critical(touc) + { + do_decode(); + } + +/* +* Load a X.509 CRL +*/ +X509_CRL::X509_CRL(const std::string& in, bool touc) : + X509_Object(in, "CRL/X509 CRL"), throw_on_unknown_critical(touc) + { + do_decode(); + } + +/* +* Decode the TBSCertList data +*/ +void X509_CRL::force_decode() + { + BER_Decoder tbs_crl(tbs_bits); + + u32bit version; + tbs_crl.decode_optional(version, INTEGER, UNIVERSAL); + + if(version != 0 && version != 1) + throw X509_CRL_Error("Unknown X.509 CRL version " + + to_string(version+1)); + + AlgorithmIdentifier sig_algo_inner; + tbs_crl.decode(sig_algo_inner); + + if(sig_algo != sig_algo_inner) + throw X509_CRL_Error("Algorithm identifier mismatch"); + + X509_DN dn_issuer; + tbs_crl.decode(dn_issuer); + info.add(dn_issuer.contents()); + + X509_Time start, end; + tbs_crl.decode(start).decode(end); + info.add("X509.CRL.start", start.readable_string()); + info.add("X509.CRL.end", end.readable_string()); + + BER_Object next = tbs_crl.get_next_object(); + + if(next.type_tag == SEQUENCE && next.class_tag == CONSTRUCTED) + { + BER_Decoder cert_list(next.value); + + while(cert_list.more_items()) + { + CRL_Entry entry(throw_on_unknown_critical); + cert_list.decode(entry); + revoked.push_back(entry); + } + next = tbs_crl.get_next_object(); + } + + if(next.type_tag == 0 && + next.class_tag == ASN1_Tag(CONSTRUCTED | CONTEXT_SPECIFIC)) + { + BER_Decoder crl_options(next.value); + + Extensions extensions(throw_on_unknown_critical); + + crl_options.decode(extensions).verify_end(); + + extensions.contents_to(info, info); + + next = tbs_crl.get_next_object(); + } + + if(next.type_tag != NO_OBJECT) + throw X509_CRL_Error("Unknown tag in CRL"); + + tbs_crl.verify_end(); + } + +/* +* Return the list of revoked certificates +*/ +std::vector<CRL_Entry> X509_CRL::get_revoked() const + { + return revoked; + } + +/* +* Return the distinguished name of the issuer +*/ +X509_DN X509_CRL::issuer_dn() const + { + return create_dn(info); + } + +/* +* Return the key identifier of the issuer +*/ +MemoryVector<byte> X509_CRL::authority_key_id() const + { + return info.get1_memvec("X509v3.AuthorityKeyIdentifier"); + } + +/* +* Return the CRL number of this CRL +*/ +u32bit X509_CRL::crl_number() const + { + return info.get1_u32bit("X509v3.CRLNumber"); + } + +/* +* Return the issue data of the CRL +*/ +X509_Time X509_CRL::this_update() const + { + return info.get1("X509.CRL.start"); + } + +/* +* Return the date when a new CRL will be issued +*/ +X509_Time X509_CRL::next_update() const + { + return info.get1("X509.CRL.end"); + } + +} diff --git a/botan/src/cert/x509/x509_crl.h b/botan/src/cert/x509/x509_crl.h new file mode 100644 index 0000000..6caef42 --- /dev/null +++ b/botan/src/cert/x509/x509_crl.h @@ -0,0 +1,90 @@ +/* +* X.509 CRL +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_X509_CRL_H__ +#define BOTAN_X509_CRL_H__ + +#include <botan/x509_obj.h> +#include <botan/crl_ent.h> +#include <vector> + +namespace Botan { + +/** +* This class represents X.509 Certificate Revocation Lists (CRLs). +*/ +class BOTAN_DLL X509_CRL : public X509_Object + { + public: + /** + * This class represents CRL related errors. + */ + struct X509_CRL_Error : public Exception + { + X509_CRL_Error(const std::string& error) : + Exception("X509_CRL: " + error) {} + }; + + /** + * Get the entries of this CRL in the form of a vector. + * @return a vector containing the entries of this CRL. + */ + std::vector<CRL_Entry> get_revoked() const; + + /** + * Get the issuer DN of this CRL. + * @return the CRLs issuer DN + */ + X509_DN issuer_dn() const; + + /** + * Get the AuthorityKeyIdentifier of this CRL. + * @return this CRLs AuthorityKeyIdentifier + */ + MemoryVector<byte> authority_key_id() const; + + /** + * Get the serial number of this CRL. + * @return the CRLs serial number + */ + u32bit crl_number() const; + + /** + * Get the CRL's thisUpdate value. + * @return the CRLs thisUpdate + */ + X509_Time this_update() const; + + /** + * Get the CRL's nextUpdate value. + * @return the CRLs nextdUpdate + */ + X509_Time next_update() const; + + /** + * Construct a CRL from a data source. + * @param source the data source providing the DER or PEM encoded CRL. + */ + X509_CRL(DataSource&, bool throw_on_unknown_critical = false); + + /** + * Construct a CRL from a file containing the DER or PEM encoded CRL. + * @param filename the name of the CRL file + */ + X509_CRL(const std::string& filename, + bool throw_on_unknown_critical = false); + private: + void force_decode(); + + bool throw_on_unknown_critical; + std::vector<CRL_Entry> revoked; + Data_Store info; + }; + +} + +#endif diff --git a/botan/src/cert/x509/x509_ext.cpp b/botan/src/cert/x509/x509_ext.cpp new file mode 100644 index 0000000..5e07cbd --- /dev/null +++ b/botan/src/cert/x509/x509_ext.cpp @@ -0,0 +1,581 @@ +/* +* X.509 Certificate Extensions +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/x509_ext.h> +#include <botan/sha160.h> +#include <botan/der_enc.h> +#include <botan/ber_dec.h> +#include <botan/oids.h> +#include <botan/libstate.h> +#include <botan/bit_ops.h> +#include <algorithm> +#include <memory> + +namespace Botan { + +/* +* List of X.509 Certificate Extensions +*/ +Certificate_Extension* Extensions::get_extension(const OID& oid) + { +#define X509_EXTENSION(NAME, TYPE) \ + if(OIDS::name_of(oid, NAME)) \ + return new Cert_Extension::TYPE(); + + X509_EXTENSION("X509v3.KeyUsage", Key_Usage); + X509_EXTENSION("X509v3.BasicConstraints", Basic_Constraints); + X509_EXTENSION("X509v3.SubjectKeyIdentifier", Subject_Key_ID); + X509_EXTENSION("X509v3.AuthorityKeyIdentifier", Authority_Key_ID); + X509_EXTENSION("X509v3.ExtendedKeyUsage", Extended_Key_Usage); + X509_EXTENSION("X509v3.IssuerAlternativeName", Issuer_Alternative_Name); + X509_EXTENSION("X509v3.SubjectAlternativeName", Subject_Alternative_Name); + X509_EXTENSION("X509v3.CRLNumber", CRL_Number); + X509_EXTENSION("X509v3.CertificatePolicies", Certificate_Policies); + + return 0; + } + +/* +* Extensions Copy Constructor +*/ +Extensions::Extensions(const Extensions& extensions) : ASN1_Object() + { + *this = extensions; + } + +/* +* Extensions Assignment Operator +*/ +Extensions& Extensions::operator=(const Extensions& other) + { + for(u32bit j = 0; j != extensions.size(); ++j) + delete extensions[j]; + extensions.clear(); + + for(u32bit j = 0; j != other.extensions.size(); ++j) + extensions.push_back(other.extensions[j]->copy()); + + return (*this); + } + +/* +* Return the OID of this extension +*/ +OID Certificate_Extension::oid_of() const + { + return OIDS::lookup(oid_name()); + } + +/* +* Encode an Extensions list +*/ +void Extensions::encode_into(DER_Encoder& to_object) const + { + for(u32bit j = 0; j != extensions.size(); ++j) + { + const Certificate_Extension* ext = extensions[j]; + + std::string setting; + + if(ext->config_id() != "") + setting = global_state().option("x509/exts/" + ext->config_id()); + + if(setting == "") + setting = "yes"; + + if(setting != "yes" && setting != "no" && setting != "critical") + throw Invalid_Argument("X509_CA:: Invalid value for option " + "x509/exts/" + ext->config_id() + " of " + + setting); + + bool is_critical = (setting == "critical"); + bool should_encode = ext->should_encode() && (setting != "no"); + + if(should_encode) + { + to_object.start_cons(SEQUENCE) + .encode(ext->oid_of()) + .encode_optional(is_critical, false) + .encode(ext->encode_inner(), OCTET_STRING) + .end_cons(); + } + } + } + +/* +* Decode a list of Extensions +*/ +void Extensions::decode_from(BER_Decoder& from_source) + { + for(u32bit j = 0; j != extensions.size(); ++j) + delete extensions[j]; + extensions.clear(); + + BER_Decoder sequence = from_source.start_cons(SEQUENCE); + while(sequence.more_items()) + { + OID oid; + MemoryVector<byte> value; + bool critical; + + sequence.start_cons(SEQUENCE) + .decode(oid) + .decode_optional(critical, BOOLEAN, UNIVERSAL, false) + .decode(value, OCTET_STRING) + .verify_end() + .end_cons(); + + Certificate_Extension* ext = get_extension(oid); + + if(!ext) + { + if(!critical || !should_throw) + continue; + + throw Decoding_Error("Encountered unknown X.509 extension marked " + "as critical; OID = " + oid.as_string()); + } + + ext->decode_inner(value); + + extensions.push_back(ext); + } + sequence.verify_end(); + } + +/* +* Write the extensions to an info store +*/ +void Extensions::contents_to(Data_Store& subject_info, + Data_Store& issuer_info) const + { + for(u32bit j = 0; j != extensions.size(); ++j) + extensions[j]->contents_to(subject_info, issuer_info); + } + +/* +* Delete an Extensions list +*/ +Extensions::~Extensions() + { + for(u32bit j = 0; j != extensions.size(); ++j) + delete extensions[j]; + } + +namespace Cert_Extension { + +/* +* Checked accessor for the path_limit member +*/ +u32bit Basic_Constraints::get_path_limit() const + { + if(!is_ca) + throw Invalid_State("Basic_Constraints::get_path_limit: Not a CA"); + return path_limit; + } + +/* +* Encode the extension +*/ +MemoryVector<byte> Basic_Constraints::encode_inner() const + { + return DER_Encoder() + .start_cons(SEQUENCE) + .encode_if(is_ca, + DER_Encoder() + .encode(is_ca) + .encode_optional(path_limit, NO_CERT_PATH_LIMIT) + ) + .end_cons() + .get_contents(); + } + +/* +* Decode the extension +*/ +void Basic_Constraints::decode_inner(const MemoryRegion<byte>& in) + { + BER_Decoder(in) + .start_cons(SEQUENCE) + .decode_optional(is_ca, BOOLEAN, UNIVERSAL, false) + .decode_optional(path_limit, INTEGER, UNIVERSAL, NO_CERT_PATH_LIMIT) + .verify_end() + .end_cons(); + + if(is_ca == false) + path_limit = 0; + } + +/* +* Return a textual representation +*/ +void Basic_Constraints::contents_to(Data_Store& subject, Data_Store&) const + { + subject.add("X509v3.BasicConstraints.is_ca", (is_ca ? 1 : 0)); + subject.add("X509v3.BasicConstraints.path_constraint", path_limit); + } + +/* +* Encode the extension +*/ +MemoryVector<byte> Key_Usage::encode_inner() const + { + if(constraints == NO_CONSTRAINTS) + throw Encoding_Error("Cannot encode zero usage constraints"); + + const u32bit unused_bits = low_bit(constraints) - 1; + + SecureVector<byte> der; + der.append(BIT_STRING); + der.append(2 + ((unused_bits < 8) ? 1 : 0)); + der.append(unused_bits % 8); + der.append((constraints >> 8) & 0xFF); + if(constraints & 0xFF) + der.append(constraints & 0xFF); + + return der; + } + +/* +* Decode the extension +*/ +void Key_Usage::decode_inner(const MemoryRegion<byte>& in) + { + BER_Decoder ber(in); + + BER_Object obj = ber.get_next_object(); + + if(obj.type_tag != BIT_STRING || obj.class_tag != UNIVERSAL) + throw BER_Bad_Tag("Bad tag for usage constraint", + obj.type_tag, obj.class_tag); + + if(obj.value.size() != 2 && obj.value.size() != 3) + throw BER_Decoding_Error("Bad size for BITSTRING in usage constraint"); + + if(obj.value[0] >= 8) + throw BER_Decoding_Error("Invalid unused bits in usage constraint"); + + obj.value[obj.value.size()-1] &= (0xFF << obj.value[0]); + + u16bit usage = 0; + for(u32bit j = 1; j != obj.value.size(); ++j) + usage = (obj.value[j] << 8) | usage; + + constraints = Key_Constraints(usage); + } + +/* +* Return a textual representation +*/ +void Key_Usage::contents_to(Data_Store& subject, Data_Store&) const + { + subject.add("X509v3.KeyUsage", constraints); + } + +/* +* Encode the extension +*/ +MemoryVector<byte> Subject_Key_ID::encode_inner() const + { + return DER_Encoder().encode(key_id, OCTET_STRING).get_contents(); + } + +/* +* Decode the extension +*/ +void Subject_Key_ID::decode_inner(const MemoryRegion<byte>& in) + { + BER_Decoder(in).decode(key_id, OCTET_STRING).verify_end(); + } + +/* +* Return a textual representation +*/ +void Subject_Key_ID::contents_to(Data_Store& subject, Data_Store&) const + { + subject.add("X509v3.SubjectKeyIdentifier", key_id); + } + +/* +* Subject_Key_ID Constructor +*/ +Subject_Key_ID::Subject_Key_ID(const MemoryRegion<byte>& pub_key) + { + SHA_160 hash; + key_id = hash.process(pub_key); + } + +/* +* Encode the extension +*/ +MemoryVector<byte> Authority_Key_ID::encode_inner() const + { + return DER_Encoder() + .start_cons(SEQUENCE) + .encode(key_id, OCTET_STRING, ASN1_Tag(0), CONTEXT_SPECIFIC) + .end_cons() + .get_contents(); + } + +/* +* Decode the extension +*/ +void Authority_Key_ID::decode_inner(const MemoryRegion<byte>& in) + { + BER_Decoder(in) + .start_cons(SEQUENCE) + .decode_optional_string(key_id, OCTET_STRING, 0); + } + +/* +* Return a textual representation +*/ +void Authority_Key_ID::contents_to(Data_Store&, Data_Store& issuer) const + { + if(key_id.size()) + issuer.add("X509v3.AuthorityKeyIdentifier", key_id); + } + +/* +* Encode the extension +*/ +MemoryVector<byte> Alternative_Name::encode_inner() const + { + return DER_Encoder().encode(alt_name).get_contents(); + } + +/* +* Decode the extension +*/ +void Alternative_Name::decode_inner(const MemoryRegion<byte>& in) + { + BER_Decoder(in).decode(alt_name); + } + +/* +* Return a textual representation +*/ +void Alternative_Name::contents_to(Data_Store& subject_info, + Data_Store& issuer_info) const + { + std::multimap<std::string, std::string> contents = + get_alt_name().contents(); + + if(oid_name_str == "X509v3.SubjectAlternativeName") + subject_info.add(contents); + else if(oid_name_str == "X509v3.IssuerAlternativeName") + issuer_info.add(contents); + else + throw Internal_Error("In Alternative_Name, unknown type " + + oid_name_str); + } + +/* +* Alternative_Name Constructor +*/ +Alternative_Name::Alternative_Name(const AlternativeName& alt_name, + const std::string& oid_name_str, + const std::string& config_name_str) + { + this->alt_name = alt_name; + this->oid_name_str = oid_name_str; + this->config_name_str = config_name_str; + } + +/* +* Subject_Alternative_Name Constructor +*/ +Subject_Alternative_Name::Subject_Alternative_Name( + const AlternativeName& name) : + + Alternative_Name(name, "X509v3.SubjectAlternativeName", + "subject_alternative_name") + { + } + +/* +* Issuer_Alternative_Name Constructor +*/ +Issuer_Alternative_Name::Issuer_Alternative_Name(const AlternativeName& name) : + Alternative_Name(name, "X509v3.IssuerAlternativeName", + "issuer_alternative_name") + { + } + +/* +* Encode the extension +*/ +MemoryVector<byte> Extended_Key_Usage::encode_inner() const + { + return DER_Encoder() + .start_cons(SEQUENCE) + .encode_list(oids) + .end_cons() + .get_contents(); + } + +/* +* Decode the extension +*/ +void Extended_Key_Usage::decode_inner(const MemoryRegion<byte>& in) + { + BER_Decoder(in) + .start_cons(SEQUENCE) + .decode_list(oids) + .end_cons(); + } + +/* +* Return a textual representation +*/ +void Extended_Key_Usage::contents_to(Data_Store& subject, Data_Store&) const + { + for(u32bit j = 0; j != oids.size(); ++j) + subject.add("X509v3.ExtendedKeyUsage", oids[j].as_string()); + } + +namespace { + +/* +* A policy specifier +*/ +class Policy_Information : public ASN1_Object + { + public: + OID oid; + + void encode_into(DER_Encoder& codec) const + { + codec.start_cons(SEQUENCE) + .encode(oid) + .end_cons(); + } + + void decode_from(BER_Decoder& codec) + { + codec.start_cons(SEQUENCE) + .decode(oid) + .discard_remaining() + .end_cons(); + } + }; + +} + +/* +* Encode the extension +*/ +MemoryVector<byte> Certificate_Policies::encode_inner() const + { + throw Exception("Certificate_Policies::encode_inner: Bugged"); + + std::vector<Policy_Information> policies; + + return DER_Encoder() + .start_cons(SEQUENCE) + .encode_list(policies) + .end_cons() + .get_contents(); + } + +/* +* Decode the extension +*/ +void Certificate_Policies::decode_inner(const MemoryRegion<byte>& in) + { + std::vector<Policy_Information> policies; + + BER_Decoder(in) + .start_cons(SEQUENCE) + .decode_list(policies) + .end_cons(); + } + +/* +* Return a textual representation +*/ +void Certificate_Policies::contents_to(Data_Store& info, Data_Store&) const + { + for(u32bit j = 0; j != oids.size(); ++j) + info.add("X509v3.ExtendedKeyUsage", oids[j].as_string()); + } + +/* +* Checked accessor for the crl_number member +*/ +u32bit CRL_Number::get_crl_number() const + { + if(!has_value) + throw Invalid_State("CRL_Number::get_crl_number: Not set"); + return crl_number; + } + +/* +* Copy a CRL_Number extension +*/ +CRL_Number* CRL_Number::copy() const + { + if(!has_value) + throw Invalid_State("CRL_Number::copy: Not set"); + return new CRL_Number(crl_number); + } + +/* +* Encode the extension +*/ +MemoryVector<byte> CRL_Number::encode_inner() const + { + return DER_Encoder().encode(crl_number).get_contents(); + } + +/* +* Decode the extension +*/ +void CRL_Number::decode_inner(const MemoryRegion<byte>& in) + { + BER_Decoder(in).decode(crl_number); + } + +/* +* Return a textual representation +*/ +void CRL_Number::contents_to(Data_Store& info, Data_Store&) const + { + info.add("X509v3.CRLNumber", crl_number); + } + +/* +* Encode the extension +*/ +MemoryVector<byte> CRL_ReasonCode::encode_inner() const + { + return DER_Encoder() + .encode(static_cast<u32bit>(reason), ENUMERATED, UNIVERSAL) + .get_contents(); + } + +/* +* Decode the extension +*/ +void CRL_ReasonCode::decode_inner(const MemoryRegion<byte>& in) + { + u32bit reason_code = 0; + BER_Decoder(in).decode(reason_code, ENUMERATED, UNIVERSAL); + reason = static_cast<CRL_Code>(reason_code); + } + +/* +* Return a textual representation +*/ +void CRL_ReasonCode::contents_to(Data_Store& info, Data_Store&) const + { + info.add("X509v3.CRLReasonCode", reason); + } + +} + +} diff --git a/botan/src/cert/x509/x509_ext.h b/botan/src/cert/x509/x509_ext.h new file mode 100644 index 0000000..108215e --- /dev/null +++ b/botan/src/cert/x509/x509_ext.h @@ -0,0 +1,317 @@ +/* +* X.509 Certificate Extensions +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_X509_EXTENSIONS_H__ +#define BOTAN_X509_EXTENSIONS_H__ + +#include <botan/asn1_int.h> +#include <botan/asn1_oid.h> +#include <botan/asn1_obj.h> +#include <botan/datastor.h> +#include <botan/pubkey_enums.h> + +namespace Botan { + +/* +* X.509 Certificate Extension +*/ +class BOTAN_DLL Certificate_Extension + { + public: + OID oid_of() const; + + virtual Certificate_Extension* copy() const = 0; + + virtual void contents_to(Data_Store&, Data_Store&) const = 0; + virtual std::string config_id() const = 0; + virtual std::string oid_name() const = 0; + + virtual ~Certificate_Extension() {} + protected: + friend class Extensions; + virtual bool should_encode() const { return true; } + virtual MemoryVector<byte> encode_inner() const = 0; + virtual void decode_inner(const MemoryRegion<byte>&) = 0; + }; + +/* +* X.509 Certificate Extension List +*/ +class BOTAN_DLL Extensions : public ASN1_Object + { + public: + void encode_into(class DER_Encoder&) const; + void decode_from(class BER_Decoder&); + + void contents_to(Data_Store&, Data_Store&) const; + + void add(Certificate_Extension* extn) + { extensions.push_back(extn); } + + Extensions& operator=(const Extensions&); + + Extensions(const Extensions&); + Extensions(bool st = true) : should_throw(st) {} + ~Extensions(); + private: + static Certificate_Extension* get_extension(const OID&); + + std::vector<Certificate_Extension*> extensions; + bool should_throw; + }; + +namespace Cert_Extension { + +/* +* Basic Constraints Extension +*/ +class BOTAN_DLL Basic_Constraints : public Certificate_Extension + { + public: + Basic_Constraints* copy() const + { return new Basic_Constraints(is_ca, path_limit); } + + Basic_Constraints(bool ca = false, u32bit limit = 0) : + is_ca(ca), path_limit(limit) {} + + bool get_is_ca() const { return is_ca; } + u32bit get_path_limit() const; + private: + std::string config_id() const { return "basic_constraints"; } + std::string oid_name() const { return "X509v3.BasicConstraints"; } + + MemoryVector<byte> encode_inner() const; + void decode_inner(const MemoryRegion<byte>&); + void contents_to(Data_Store&, Data_Store&) const; + + bool is_ca; + u32bit path_limit; + }; + +/* +* Key Usage Constraints Extension +*/ +class BOTAN_DLL Key_Usage : public Certificate_Extension + { + public: + Key_Usage* copy() const { return new Key_Usage(constraints); } + + Key_Usage(Key_Constraints c = NO_CONSTRAINTS) : constraints(c) {} + + Key_Constraints get_constraints() const { return constraints; } + private: + std::string config_id() const { return "key_usage"; } + std::string oid_name() const { return "X509v3.KeyUsage"; } + + bool should_encode() const { return (constraints != NO_CONSTRAINTS); } + MemoryVector<byte> encode_inner() const; + void decode_inner(const MemoryRegion<byte>&); + void contents_to(Data_Store&, Data_Store&) const; + + Key_Constraints constraints; + }; + +/* +* Subject Key Identifier Extension +*/ +class BOTAN_DLL Subject_Key_ID : public Certificate_Extension + { + public: + Subject_Key_ID* copy() const { return new Subject_Key_ID(key_id); } + + Subject_Key_ID() {} + Subject_Key_ID(const MemoryRegion<byte>&); + + MemoryVector<byte> get_key_id() const { return key_id; } + private: + std::string config_id() const { return "subject_key_id"; } + std::string oid_name() const { return "X509v3.SubjectKeyIdentifier"; } + + bool should_encode() const { return (key_id.size() > 0); } + MemoryVector<byte> encode_inner() const; + void decode_inner(const MemoryRegion<byte>&); + void contents_to(Data_Store&, Data_Store&) const; + + MemoryVector<byte> key_id; + }; + +/* +* Authority Key Identifier Extension +*/ +class BOTAN_DLL Authority_Key_ID : public Certificate_Extension + { + public: + Authority_Key_ID* copy() const { return new Authority_Key_ID(key_id); } + + Authority_Key_ID() {} + Authority_Key_ID(const MemoryRegion<byte>& k) : key_id(k) {} + + MemoryVector<byte> get_key_id() const { return key_id; } + private: + std::string config_id() const { return "authority_key_id"; } + std::string oid_name() const { return "X509v3.AuthorityKeyIdentifier"; } + + bool should_encode() const { return (key_id.size() > 0); } + MemoryVector<byte> encode_inner() const; + void decode_inner(const MemoryRegion<byte>&); + void contents_to(Data_Store&, Data_Store&) const; + + MemoryVector<byte> key_id; + }; + +/* +* Alternative Name Extension Base Class +*/ +class BOTAN_DLL Alternative_Name : public Certificate_Extension + { + public: + AlternativeName get_alt_name() const { return alt_name; } + + protected: + Alternative_Name(const AlternativeName&, + const std::string&, const std::string&); + + Alternative_Name(const std::string&, const std::string&); + private: + std::string config_id() const { return config_name_str; } + std::string oid_name() const { return oid_name_str; } + + bool should_encode() const { return alt_name.has_items(); } + MemoryVector<byte> encode_inner() const; + void decode_inner(const MemoryRegion<byte>&); + void contents_to(Data_Store&, Data_Store&) const; + + std::string config_name_str, oid_name_str; + AlternativeName alt_name; + }; + +/* +* Subject Alternative Name Extension +*/ +class BOTAN_DLL Subject_Alternative_Name : public Alternative_Name + { + public: + Subject_Alternative_Name* copy() const + { return new Subject_Alternative_Name(get_alt_name()); } + + Subject_Alternative_Name(const AlternativeName& = AlternativeName()); + }; + +/* +* Issuer Alternative Name Extension +*/ +class BOTAN_DLL Issuer_Alternative_Name : public Alternative_Name + { + public: + Issuer_Alternative_Name* copy() const + { return new Issuer_Alternative_Name(get_alt_name()); } + + Issuer_Alternative_Name(const AlternativeName& = AlternativeName()); + }; + +/* +* Extended Key Usage Extension +*/ +class BOTAN_DLL Extended_Key_Usage : public Certificate_Extension + { + public: + Extended_Key_Usage* copy() const { return new Extended_Key_Usage(oids); } + + Extended_Key_Usage() {} + Extended_Key_Usage(const std::vector<OID>& o) : oids(o) {} + + std::vector<OID> get_oids() const { return oids; } + private: + std::string config_id() const { return "extended_key_usage"; } + std::string oid_name() const { return "X509v3.ExtendedKeyUsage"; } + + bool should_encode() const { return (oids.size() > 0); } + MemoryVector<byte> encode_inner() const; + void decode_inner(const MemoryRegion<byte>&); + void contents_to(Data_Store&, Data_Store&) const; + + std::vector<OID> oids; + }; + +/* +* Certificate Policies Extension +*/ +class BOTAN_DLL Certificate_Policies : public Certificate_Extension + { + public: + Certificate_Policies* copy() const + { return new Certificate_Policies(oids); } + + Certificate_Policies() {} + Certificate_Policies(const std::vector<OID>& o) : oids(o) {} + + std::vector<OID> get_oids() const { return oids; } + private: + std::string config_id() const { return "policy_info"; } + std::string oid_name() const { return "X509v3.CertificatePolicies"; } + + bool should_encode() const { return (oids.size() > 0); } + MemoryVector<byte> encode_inner() const; + void decode_inner(const MemoryRegion<byte>&); + void contents_to(Data_Store&, Data_Store&) const; + + std::vector<OID> oids; + }; + +/* +* CRL Number Extension +*/ +class BOTAN_DLL CRL_Number : public Certificate_Extension + { + public: + CRL_Number* copy() const; + + CRL_Number() : has_value(false), crl_number(0) {} + CRL_Number(u32bit n) : has_value(true), crl_number(n) {} + + u32bit get_crl_number() const; + private: + std::string config_id() const { return "crl_number"; } + std::string oid_name() const { return "X509v3.CRLNumber"; } + + bool should_encode() const { return has_value; } + MemoryVector<byte> encode_inner() const; + void decode_inner(const MemoryRegion<byte>&); + void contents_to(Data_Store&, Data_Store&) const; + + bool has_value; + u32bit crl_number; + }; + +/* +* CRL Entry Reason Code Extension +*/ +class BOTAN_DLL CRL_ReasonCode : public Certificate_Extension + { + public: + CRL_ReasonCode* copy() const { return new CRL_ReasonCode(reason); } + + CRL_ReasonCode(CRL_Code r = UNSPECIFIED) : reason(r) {} + + CRL_Code get_reason() const { return reason; } + private: + std::string config_id() const { return "crl_reason"; } + std::string oid_name() const { return "X509v3.ReasonCode"; } + + bool should_encode() const { return (reason != UNSPECIFIED); } + MemoryVector<byte> encode_inner() const; + void decode_inner(const MemoryRegion<byte>&); + void contents_to(Data_Store&, Data_Store&) const; + + CRL_Code reason; + }; + +} + +} + +#endif diff --git a/botan/src/cert/x509/x509_obj.cpp b/botan/src/cert/x509/x509_obj.cpp new file mode 100644 index 0000000..31b4a30 --- /dev/null +++ b/botan/src/cert/x509/x509_obj.cpp @@ -0,0 +1,235 @@ +/* +* X.509 SIGNED Object +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/x509_obj.h> +#include <botan/x509_key.h> +#include <botan/look_pk.h> +#include <botan/oids.h> +#include <botan/der_enc.h> +#include <botan/ber_dec.h> +#include <botan/parsing.h> +#include <botan/pem.h> +#include <algorithm> +#include <memory> + +namespace Botan { + +/* +* Create a generic X.509 object +*/ +X509_Object::X509_Object(DataSource& stream, const std::string& labels) + { + init(stream, labels); + } + +/* +* Createa a generic X.509 object +*/ +X509_Object::X509_Object(const std::string& file, const std::string& labels) + { + DataSource_Stream stream(file, true); + init(stream, labels); + } + +/* +* Read a PEM or BER X.509 object +*/ +void X509_Object::init(DataSource& in, const std::string& labels) + { + PEM_labels_allowed = split_on(labels, '/'); + if(PEM_labels_allowed.size() < 1) + throw Invalid_Argument("Bad labels argument to X509_Object"); + + PEM_label_pref = PEM_labels_allowed[0]; + std::sort(PEM_labels_allowed.begin(), PEM_labels_allowed.end()); + + try { + if(ASN1::maybe_BER(in) && !PEM_Code::matches(in)) + decode_info(in); + else + { + std::string got_label; + DataSource_Memory ber(PEM_Code::decode(in, got_label)); + + if(!std::binary_search(PEM_labels_allowed.begin(), + PEM_labels_allowed.end(), got_label)) + throw Decoding_Error("Invalid PEM label: " + got_label); + decode_info(ber); + } + } + catch(Decoding_Error) + { + throw Decoding_Error(PEM_label_pref + " decoding failed"); + } + } + +/* +* Read a BER encoded X.509 object +*/ +void X509_Object::decode_info(DataSource& source) + { + BER_Decoder(source) + .start_cons(SEQUENCE) + .start_cons(SEQUENCE) + .raw_bytes(tbs_bits) + .end_cons() + .decode(sig_algo) + .decode(sig, BIT_STRING) + .verify_end() + .end_cons(); + } + +/* +* Return a BER or PEM encoded X.509 object +*/ +void X509_Object::encode(Pipe& out, X509_Encoding encoding) const + { + SecureVector<byte> der = DER_Encoder() + .start_cons(SEQUENCE) + .start_cons(SEQUENCE) + .raw_bytes(tbs_bits) + .end_cons() + .encode(sig_algo) + .encode(sig, BIT_STRING) + .end_cons() + .get_contents(); + + if(encoding == PEM) + out.write(PEM_Code::encode(der, PEM_label_pref)); + else + out.write(der); + } + +/* +* Return a BER encoded X.509 object +*/ +SecureVector<byte> X509_Object::BER_encode() const + { + Pipe ber; + ber.start_msg(); + encode(ber, RAW_BER); + ber.end_msg(); + return ber.read_all(); + } + +/* +* Return a PEM encoded X.509 object +*/ +std::string X509_Object::PEM_encode() const + { + Pipe pem; + pem.start_msg(); + encode(pem, PEM); + pem.end_msg(); + return pem.read_all_as_string(); + } + +/* +* Return the TBS data +*/ +SecureVector<byte> X509_Object::tbs_data() const + { + return ASN1::put_in_sequence(tbs_bits); + } + +/* +* Return the signature of this object +*/ +SecureVector<byte> X509_Object::signature() const + { + return sig; + } + +/* +* Return the algorithm used to sign this object +*/ +AlgorithmIdentifier X509_Object::signature_algorithm() const + { + return sig_algo; + } + +/* +* Check the signature on an object +*/ +bool X509_Object::check_signature(Public_Key& pub_key) const + { + try { + std::vector<std::string> sig_info = + split_on(OIDS::lookup(sig_algo.oid), '/'); + + if(sig_info.size() != 2 || sig_info[0] != pub_key.algo_name()) + return false; + + std::string padding = sig_info[1]; + Signature_Format format = + (pub_key.message_parts() >= 2) ? DER_SEQUENCE : IEEE_1363; + + std::auto_ptr<PK_Verifier> verifier; + + if(dynamic_cast<PK_Verifying_with_MR_Key*>(&pub_key)) + { + PK_Verifying_with_MR_Key& sig_key = + dynamic_cast<PK_Verifying_with_MR_Key&>(pub_key); + verifier.reset(get_pk_verifier(sig_key, padding, format)); + } + else if(dynamic_cast<PK_Verifying_wo_MR_Key*>(&pub_key)) + { + PK_Verifying_wo_MR_Key& sig_key = + dynamic_cast<PK_Verifying_wo_MR_Key&>(pub_key); + verifier.reset(get_pk_verifier(sig_key, padding, format)); + } + else + return false; + + return verifier->verify_message(tbs_data(), signature()); + } + catch(...) + { + return false; + } + } + +/* +* Apply the X.509 SIGNED macro +*/ +MemoryVector<byte> X509_Object::make_signed(PK_Signer* signer, + RandomNumberGenerator& rng, + const AlgorithmIdentifier& algo, + const MemoryRegion<byte>& tbs_bits) + { + return DER_Encoder() + .start_cons(SEQUENCE) + .raw_bytes(tbs_bits) + .encode(algo) + .encode(signer->sign_message(tbs_bits, rng), BIT_STRING) + .end_cons() + .get_contents(); + } + +/* +* Try to decode the actual information +*/ +void X509_Object::do_decode() + { + try { + force_decode(); + } + catch(Decoding_Error& e) + { + const std::string what = e.what(); + throw Decoding_Error(PEM_label_pref + " decoding failed (" + + what.substr(23, std::string::npos) + ")"); + } + catch(Invalid_Argument& e) + { + const std::string what = e.what(); + throw Decoding_Error(PEM_label_pref + " decoding failed (" + + what.substr(7, std::string::npos) + ")"); + } + } + +} diff --git a/botan/src/cert/x509/x509_obj.h b/botan/src/cert/x509/x509_obj.h new file mode 100644 index 0000000..c7f92fa --- /dev/null +++ b/botan/src/cert/x509/x509_obj.h @@ -0,0 +1,67 @@ +/* +* X.509 SIGNED Object +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_X509_OBJECT_H__ +#define BOTAN_X509_OBJECT_H__ + +#include <botan/asn1_obj.h> +#include <botan/pipe.h> +#include <botan/pubkey_enums.h> +#include <botan/rng.h> +#include <vector> + +namespace Botan { + +/** +* This class represents abstract X.509 signed objects as +* in the X.500 SIGNED macro +*/ +class BOTAN_DLL X509_Object + { + public: + SecureVector<byte> tbs_data() const; + SecureVector<byte> signature() const; + AlgorithmIdentifier signature_algorithm() const; + + /** + * Create a signed X509 object. + * @param signer the signer used to sign the object + * @param rng the random number generator to use + * @param alg_id the algorithm identifier of the signature scheme + * @param tbs the tbs bits to be signed + * @return the signed X509 object + */ + static MemoryVector<byte> make_signed(class PK_Signer* signer, + RandomNumberGenerator& rng, + const AlgorithmIdentifier& alg_id, + const MemoryRegion<byte>& tbs); + + bool check_signature(class Public_Key&) const; + + void encode(Pipe&, X509_Encoding = PEM) const; + SecureVector<byte> BER_encode() const; + std::string PEM_encode() const; + + X509_Object(DataSource&, const std::string&); + X509_Object(const std::string&, const std::string&); + virtual ~X509_Object() {} + protected: + void do_decode(); + X509_Object() {} + AlgorithmIdentifier sig_algo; + SecureVector<byte> tbs_bits, sig; + private: + virtual void force_decode() = 0; + void init(DataSource&, const std::string&); + void decode_info(DataSource&); + std::vector<std::string> PEM_labels_allowed; + std::string PEM_label_pref; + }; + +} + +#endif diff --git a/botan/src/cert/x509/x509cert.cpp b/botan/src/cert/x509/x509cert.cpp new file mode 100644 index 0000000..ac5839f --- /dev/null +++ b/botan/src/cert/x509/x509cert.cpp @@ -0,0 +1,366 @@ +/* +* X.509 Certificates +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/x509cert.h> +#include <botan/x509_ext.h> +#include <botan/der_enc.h> +#include <botan/ber_dec.h> +#include <botan/stl_util.h> +#include <botan/parsing.h> +#include <botan/bigint.h> +#include <botan/oids.h> +#include <botan/pem.h> +#include <algorithm> + +namespace Botan { + +namespace { + +/* +* Lookup each OID in the vector +*/ +std::vector<std::string> lookup_oids(const std::vector<std::string>& in) + { + std::vector<std::string> out; + + std::vector<std::string>::const_iterator i = in.begin(); + while(i != in.end()) + { + out.push_back(OIDS::lookup(OID(*i))); + ++i; + } + return out; + } + +} + +/* +* X509_Certificate Constructor +*/ +X509_Certificate::X509_Certificate(DataSource& in) : + X509_Object(in, "CERTIFICATE/X509 CERTIFICATE") + { + self_signed = false; + do_decode(); + } + +/* +* X509_Certificate Constructor +*/ +X509_Certificate::X509_Certificate(const std::string& in) : + X509_Object(in, "CERTIFICATE/X509 CERTIFICATE") + { + self_signed = false; + do_decode(); + } + +/* +* Decode the TBSCertificate data +*/ +void X509_Certificate::force_decode() + { + u32bit version; + BigInt serial_bn; + AlgorithmIdentifier sig_algo_inner; + X509_DN dn_issuer, dn_subject; + X509_Time start, end; + + BER_Decoder tbs_cert(tbs_bits); + + tbs_cert.decode_optional(version, ASN1_Tag(0), + ASN1_Tag(CONSTRUCTED | CONTEXT_SPECIFIC)) + .decode(serial_bn) + .decode(sig_algo_inner) + .decode(dn_issuer) + .start_cons(SEQUENCE) + .decode(start) + .decode(end) + .verify_end() + .end_cons() + .decode(dn_subject); + + if(version > 2) + throw Decoding_Error("Unknown X.509 cert version " + to_string(version)); + if(sig_algo != sig_algo_inner) + throw Decoding_Error("Algorithm identifier mismatch"); + + self_signed = (dn_subject == dn_issuer); + + subject.add(dn_subject.contents()); + issuer.add(dn_issuer.contents()); + + BER_Object public_key = tbs_cert.get_next_object(); + if(public_key.type_tag != SEQUENCE || public_key.class_tag != CONSTRUCTED) + throw BER_Bad_Tag("X509_Certificate: Unexpected tag for public key", + public_key.type_tag, public_key.class_tag); + + MemoryVector<byte> v2_issuer_key_id, v2_subject_key_id; + + tbs_cert.decode_optional_string(v2_issuer_key_id, BIT_STRING, 1); + tbs_cert.decode_optional_string(v2_subject_key_id, BIT_STRING, 2); + + BER_Object v3_exts_data = tbs_cert.get_next_object(); + if(v3_exts_data.type_tag == 3 && + v3_exts_data.class_tag == ASN1_Tag(CONSTRUCTED | CONTEXT_SPECIFIC)) + { + Extensions extensions; + + BER_Decoder(v3_exts_data.value).decode(extensions).verify_end(); + + extensions.contents_to(subject, issuer); + } + else if(v3_exts_data.type_tag != NO_OBJECT) + throw BER_Bad_Tag("Unknown tag in X.509 cert", + v3_exts_data.type_tag, v3_exts_data.class_tag); + + if(tbs_cert.more_items()) + throw Decoding_Error("TBSCertificate has more items that expected"); + + subject.add("X509.Certificate.version", version); + subject.add("X509.Certificate.serial", BigInt::encode(serial_bn)); + subject.add("X509.Certificate.start", start.readable_string()); + subject.add("X509.Certificate.end", end.readable_string()); + + issuer.add("X509.Certificate.v2.key_id", v2_issuer_key_id); + subject.add("X509.Certificate.v2.key_id", v2_subject_key_id); + + subject.add("X509.Certificate.public_key", + PEM_Code::encode( + ASN1::put_in_sequence(public_key.value), + "PUBLIC KEY" + ) + ); + + if(is_CA_cert() && + !subject.has_value("X509v3.BasicConstraints.path_constraint")) + { + u32bit limit = (x509_version() < 3) ? NO_CERT_PATH_LIMIT : 0; + subject.add("X509v3.BasicConstraints.path_constraint", limit); + } + } + +/* +* Return the X.509 version in use +*/ +u32bit X509_Certificate::x509_version() const + { + return (subject.get1_u32bit("X509.Certificate.version") + 1); + } + +/* +* Return the time this cert becomes valid +*/ +std::string X509_Certificate::start_time() const + { + return subject.get1("X509.Certificate.start"); + } + +/* +* Return the time this cert becomes invalid +*/ +std::string X509_Certificate::end_time() const + { + return subject.get1("X509.Certificate.end"); + } + +/* +* Return information about the subject +*/ +std::vector<std::string> +X509_Certificate::subject_info(const std::string& what) const + { + return subject.get(X509_DN::deref_info_field(what)); + } + +/* +* Return information about the issuer +*/ +std::vector<std::string> +X509_Certificate::issuer_info(const std::string& what) const + { + return issuer.get(X509_DN::deref_info_field(what)); + } + +/* +* Return the public key in this certificate +*/ +Public_Key* X509_Certificate::subject_public_key() const + { + DataSource_Memory source(subject.get1("X509.Certificate.public_key")); + return X509::load_key(source); + } + +/* +* Check if the certificate is for a CA +*/ +bool X509_Certificate::is_CA_cert() const + { + if(!subject.get1_u32bit("X509v3.BasicConstraints.is_ca")) + return false; + if((constraints() & KEY_CERT_SIGN) || (constraints() == NO_CONSTRAINTS)) + return true; + return false; + } + +/* +* Return the path length constraint +*/ +u32bit X509_Certificate::path_limit() const + { + return subject.get1_u32bit("X509v3.BasicConstraints.path_constraint", 0); + } + +/* +* Return the key usage constraints +*/ +Key_Constraints X509_Certificate::constraints() const + { + return Key_Constraints(subject.get1_u32bit("X509v3.KeyUsage", + NO_CONSTRAINTS)); + } + +/* +* Return the list of extended key usage OIDs +*/ +std::vector<std::string> X509_Certificate::ex_constraints() const + { + return lookup_oids(subject.get("X509v3.ExtendedKeyUsage")); + } + +/* +* Return the list of certificate policies +*/ +std::vector<std::string> X509_Certificate::policies() const + { + return lookup_oids(subject.get("X509v3.CertificatePolicies")); + } + +/* +* Return the authority key id +*/ +MemoryVector<byte> X509_Certificate::authority_key_id() const + { + return issuer.get1_memvec("X509v3.AuthorityKeyIdentifier"); + } + +/* +* Return the subject key id +*/ +MemoryVector<byte> X509_Certificate::subject_key_id() const + { + return subject.get1_memvec("X509v3.SubjectKeyIdentifier"); + } + +/* +* Return the certificate serial number +*/ +MemoryVector<byte> X509_Certificate::serial_number() const + { + return subject.get1_memvec("X509.Certificate.serial"); + } + +/* +* Return the distinguished name of the issuer +*/ +X509_DN X509_Certificate::issuer_dn() const + { + return create_dn(issuer); + } + +/* +* Return the distinguished name of the subject +*/ +X509_DN X509_Certificate::subject_dn() const + { + return create_dn(subject); + } + +/* +* Compare two certificates for equality +*/ +bool X509_Certificate::operator==(const X509_Certificate& other) const + { + return (sig == other.sig && + sig_algo == other.sig_algo && + self_signed == other.self_signed && + issuer == other.issuer && + subject == other.subject); + } + +/* +* X.509 Certificate Comparison +*/ +bool operator!=(const X509_Certificate& cert1, const X509_Certificate& cert2) + { + return !(cert1 == cert2); + } + +/* +* Create and populate a X509_DN +*/ +X509_DN create_dn(const Data_Store& info) + { + class DN_Matcher : public Data_Store::Matcher + { + public: + bool operator()(const std::string& key, const std::string&) const + { + if(key.find("X520.") != std::string::npos) + return true; + return false; + } + }; + + std::multimap<std::string, std::string> names = + info.search_with(DN_Matcher()); + + X509_DN dn; + + std::multimap<std::string, std::string>::iterator j; + for(j = names.begin(); j != names.end(); ++j) + dn.add_attribute(j->first, j->second); + + return dn; + } + +/* +* Create and populate an AlternativeName +*/ +AlternativeName create_alt_name(const Data_Store& info) + { + class AltName_Matcher : public Data_Store::Matcher + { + public: + bool operator()(const std::string& key, const std::string&) const + { + for(u32bit j = 0; j != matches.size(); ++j) + if(key.compare(matches[j]) == 0) + return true; + return false; + } + + AltName_Matcher(const std::string& match_any_of) + { + matches = split_on(match_any_of, '/'); + } + private: + std::vector<std::string> matches; + }; + + std::multimap<std::string, std::string> names = + info.search_with(AltName_Matcher("RFC822/DNS/URI/IP")); + + AlternativeName alt_name; + + std::multimap<std::string, std::string>::iterator j; + for(j = names.begin(); j != names.end(); ++j) + alt_name.add_attribute(j->first, j->second); + + return alt_name; + } + +} diff --git a/botan/src/cert/x509/x509cert.h b/botan/src/cert/x509/x509cert.h new file mode 100644 index 0000000..4a9d11f --- /dev/null +++ b/botan/src/cert/x509/x509cert.h @@ -0,0 +1,185 @@ +/* +* X.509 Certificates +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_X509_CERTS_H__ +#define BOTAN_X509_CERTS_H__ + +#include <botan/x509_obj.h> +#include <botan/x509_key.h> +#include <botan/datastor.h> +#include <botan/pubkey_enums.h> +#include <map> + +namespace Botan { + +/** +* This class represents X.509 Certificate +*/ +class BOTAN_DLL X509_Certificate : public X509_Object + { + public: + /** + * Get the public key associated with this certificate. + * @return the subject public key of this certificate + */ + Public_Key* subject_public_key() const; + + /** + * Get the issuer certificate DN. + * @return the issuer DN of this certificate + */ + X509_DN issuer_dn() const; + + /** + * Get the subject certificate DN. + * @return the subject DN of this certificate + */ + X509_DN subject_dn() const; + + /** + * Get a value for a specific subject_info parameter name. + * @param name the name of the paramter to look up. Possible names are + * "X509.Certificate.version", "X509.Certificate.serial", + * "X509.Certificate.start", "X509.Certificate.end", + * "X509.Certificate.v2.key_id", "X509.Certificate.public_key", + * "X509v3.BasicConstraints.path_constraint", + * "X509v3.BasicConstraints.is_ca", "X509v3.ExtendedKeyUsage", + * "X509v3.CertificatePolicies", "X509v3.SubjectKeyIdentifier" or + * "X509.Certificate.serial". + * @return the value(s) of the specified parameter + */ + std::vector<std::string> subject_info(const std::string& name) const; + + /** + * Get a value for a specific subject_info parameter name. + * @param name the name of the paramter to look up. Possible names are + * "X509.Certificate.v2.key_id" or "X509v3.AuthorityKeyIdentifier". + * @return the value(s) of the specified parameter + */ + std::vector<std::string> issuer_info(const std::string& name) const; + + /** + * Get the notBefore of the certificate. + * @return the notBefore of the certificate + */ + std::string start_time() const; + + /** + * Get the notAfter of the certificate. + * @return the notAfter of the certificate + */ + std::string end_time() const; + + /** + * Get the X509 version of this certificate object. + * @return the X509 version + */ + u32bit x509_version() const; + + /** + * Get the serial number of this certificate. + * @return the certificates serial number + */ + MemoryVector<byte> serial_number() const; + + /** + * Get the DER encoded AuthorityKeyIdentifier of this certificate. + * @return the DER encoded AuthorityKeyIdentifier + */ + MemoryVector<byte> authority_key_id() const; + + /** + * Get the DER encoded SubjectKeyIdentifier of this certificate. + * @return the DER encoded SubjectKeyIdentifier + */ + MemoryVector<byte> subject_key_id() const; + + /** + * Check whether this certificate is self signed. + * @return true if this certificate is self signed + */ + bool is_self_signed() const { return self_signed; } + + /** + * Check whether this certificate is a CA certificate. + * @return true if this certificate is a CA certificate + */ + bool is_CA_cert() const; + + /** + * Get the path limit as defined in the BasicConstraints extension of + * this certificate. + * @return the path limit + */ + u32bit path_limit() const; + + /** + * Get the key constraints as defined in the KeyUsage extension of this + * certificate. + * @return the key constraints + */ + Key_Constraints constraints() const; + + /** + * Get the key constraints as defined in the ExtendedKeyUsage + * extension of this + * certificate. + * @return the key constraints + */ + std::vector<std::string> ex_constraints() const; + + /** + * Get the policies as defined in the CertificatePolicies extension + * of this certificate. + * @return the certificate policies + */ + std::vector<std::string> policies() const; + + /** + * Check to certificates for equality. + * @return true both certificates are (binary) equal + */ + bool operator==(const X509_Certificate& other) const; + + /** + * Create a certificate from a data source providing the DER or + * PEM encoded certificate. + * @param source the data source + */ + X509_Certificate(DataSource& source); + + /** + * Create a certificate from a file containing the DER or PEM + * encoded certificate. + * @param filename the name of the certificate file + */ + X509_Certificate(const std::string& filename); + private: + void force_decode(); + friend class X509_CA; + X509_Certificate() {} + + Data_Store subject, issuer; + bool self_signed; + }; + +/** +* Check two certificates for inequality +* @return true if the arguments represent different certificates, +* false if they are binary identical +*/ +BOTAN_DLL bool operator!=(const X509_Certificate&, const X509_Certificate&); + +/* +* Data Store Extraction Operations +*/ +BOTAN_DLL X509_DN create_dn(const Data_Store&); +BOTAN_DLL AlternativeName create_alt_name(const Data_Store&); + +} + +#endif diff --git a/botan/src/cert/x509/x509find.cpp b/botan/src/cert/x509/x509find.cpp new file mode 100644 index 0000000..257367d --- /dev/null +++ b/botan/src/cert/x509/x509find.cpp @@ -0,0 +1,111 @@ +/* +* X.509 Certificate Store Searching +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/x509find.h> +#include <botan/charset.h> +#include <algorithm> + +namespace Botan { + +namespace { + +/* +* Compare based on case-insensive substrings +*/ +bool substring_match(const std::string& searching_for, + const std::string& found) + { + if(std::search(found.begin(), found.end(), searching_for.begin(), + searching_for.end(), Charset::caseless_cmp) != found.end()) + return true; + return false; + } + +/* +* Compare based on case-insensive match +*/ +bool ignore_case(const std::string& searching_for, const std::string& found) + { + if(searching_for.size() != found.size()) + return false; + + return std::equal(found.begin(), found.end(), + searching_for.begin(), Charset::caseless_cmp); + } + +} + +/* +* Search based on the contents of a DN entry +*/ +bool DN_Check::match(const X509_Certificate& cert) const + { + std::vector<std::string> info = cert.subject_info(dn_entry); + + for(u32bit j = 0; j != info.size(); ++j) + if(compare(info[j], looking_for)) + return true; + return false; + } + +/* +* DN_Check Constructor +*/ +DN_Check::DN_Check(const std::string& dn_entry, const std::string& looking_for, + compare_fn func) + { + this->dn_entry = dn_entry; + this->looking_for = looking_for; + compare = func; + } + +/* +* DN_Check Constructor +*/ +DN_Check::DN_Check(const std::string& dn_entry, const std::string& looking_for, + Search_Type method) + { + this->dn_entry = dn_entry; + this->looking_for = looking_for; + + if(method == SUBSTRING_MATCHING) + compare = &substring_match; + else if(method == IGNORE_CASE) + compare = &ignore_case; + else + throw Invalid_Argument("Unknown method argument to DN_Check()"); + } + +/* +* Match by issuer and serial number +*/ +bool IandS_Match::match(const X509_Certificate& cert) const + { + if(cert.serial_number() != serial) + return false; + return (cert.issuer_dn() == issuer); + } + +/* +* IandS_Match Constructor +*/ +IandS_Match::IandS_Match(const X509_DN& issuer, + const MemoryRegion<byte>& serial) + { + this->issuer = issuer; + this->serial = serial; + } + +/* +* Match by subject key identifier +*/ +bool SKID_Match::match(const X509_Certificate& cert) const + { + return (cert.subject_key_id() == skid); + } + +} diff --git a/botan/src/cert/x509/x509find.h b/botan/src/cert/x509/x509find.h new file mode 100644 index 0000000..a7a84c7 --- /dev/null +++ b/botan/src/cert/x509/x509find.h @@ -0,0 +1,60 @@ +/* +* X.509 Certificate Store Searching +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_X509_CERT_STORE_SEARCH_H__ +#define BOTAN_X509_CERT_STORE_SEARCH_H__ + +#include <botan/x509stor.h> + +namespace Botan { + +/* +* Search based on the contents of a DN entry +*/ +class BOTAN_DLL DN_Check : public X509_Store::Search_Func + { + public: + typedef bool (*compare_fn)(const std::string&, const std::string&); + enum Search_Type { SUBSTRING_MATCHING, IGNORE_CASE }; + + bool match(const X509_Certificate& cert) const; + + DN_Check(const std::string&, const std::string&, compare_fn); + DN_Check(const std::string&, const std::string&, Search_Type); + private: + std::string dn_entry, looking_for; + compare_fn compare; + }; + +/* +* Search for a certificate by issuer/serial +*/ +class BOTAN_DLL IandS_Match : public X509_Store::Search_Func + { + public: + bool match(const X509_Certificate& cert) const; + IandS_Match(const X509_DN&, const MemoryRegion<byte>&); + private: + X509_DN issuer; + MemoryVector<byte> serial; + }; + +/* +* Search for a certificate by subject keyid +*/ +class BOTAN_DLL SKID_Match : public X509_Store::Search_Func + { + public: + bool match(const X509_Certificate& cert) const; + SKID_Match(const MemoryRegion<byte>& s) : skid(s) {} + private: + MemoryVector<byte> skid; + }; + +} + +#endif diff --git a/botan/src/cert/x509/x509opt.cpp b/botan/src/cert/x509/x509opt.cpp new file mode 100644 index 0000000..de9d589 --- /dev/null +++ b/botan/src/cert/x509/x509opt.cpp @@ -0,0 +1,108 @@ +/* +* X.509 Certificate Options +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/x509self.h> +#include <botan/util.h> +#include <botan/parsing.h> +#include <botan/oids.h> +#include <ctime> + +namespace Botan { + +/* +* Set when the certificate should become valid +*/ +void X509_Cert_Options::not_before(const std::string& time_string) + { + start = X509_Time(time_string); + } + +/* +* Set when the certificate should expire +*/ +void X509_Cert_Options::not_after(const std::string& time_string) + { + end = X509_Time(time_string); + } + +/* +* Set key constraint information +*/ +void X509_Cert_Options::add_constraints(Key_Constraints usage) + { + constraints = usage; + } + +/* +* Set key constraint information +*/ +void X509_Cert_Options::add_ex_constraint(const OID& oid) + { + ex_constraints.push_back(oid); + } + +/* +* Set key constraint information +*/ +void X509_Cert_Options::add_ex_constraint(const std::string& oid_str) + { + ex_constraints.push_back(OIDS::lookup(oid_str)); + } + +/* +* Mark this certificate for CA usage +*/ +void X509_Cert_Options::CA_key(u32bit limit) + { + is_CA = true; + path_limit = limit; + } + +/* +* Do basic sanity checks +*/ +void X509_Cert_Options::sanity_check() const + { + if(common_name == "" || country == "") + throw Encoding_Error("X.509 certificate: name and country MUST be set"); + if(country.size() != 2) + throw Encoding_Error("Invalid ISO country code: " + country); + if(start >= end) + throw Encoding_Error("X509_Cert_Options: invalid time constraints"); + } + +/* +* Initialize the certificate options +*/ +X509_Cert_Options::X509_Cert_Options(const std::string& initial_opts, + u32bit expiration_time_in_seconds) + { + is_CA = false; + path_limit = 0; + constraints = NO_CONSTRAINTS; + + const u32bit now = system_time(); + + start = X509_Time(now); + end = X509_Time(now + expiration_time_in_seconds); + + if(initial_opts == "") + return; + + std::vector<std::string> parsed = split_on(initial_opts, '/'); + + if(parsed.size() > 4) + throw Invalid_Argument("X.509 cert options: Too many names: " + + initial_opts); + + if(parsed.size() >= 1) common_name = parsed[0]; + if(parsed.size() >= 2) country = parsed[1]; + if(parsed.size() >= 3) organization = parsed[2]; + if(parsed.size() == 4) org_unit = parsed[3]; + } + +} diff --git a/botan/src/cert/x509/x509self.cpp b/botan/src/cert/x509/x509self.cpp new file mode 100644 index 0000000..8afb22a --- /dev/null +++ b/botan/src/cert/x509/x509self.cpp @@ -0,0 +1,176 @@ +/* +* PKCS #10/Self Signed Cert Creation +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/x509self.h> +#include <botan/x509_ext.h> +#include <botan/x509_ca.h> +#include <botan/der_enc.h> +#include <botan/look_pk.h> +#include <botan/oids.h> +#include <botan/pipe.h> +#include <memory> + +namespace Botan { + +namespace { + +/* +* Shared setup for self-signed items +*/ +MemoryVector<byte> shared_setup(const X509_Cert_Options& opts, + const Private_Key& key) + { + const Private_Key* key_pointer = &key; + if(!dynamic_cast<const PK_Signing_Key*>(key_pointer)) + throw Invalid_Argument("Key type " + key.algo_name() + " cannot sign"); + + opts.sanity_check(); + + Pipe key_encoder; + key_encoder.start_msg(); + X509::encode(key, key_encoder, RAW_BER); + key_encoder.end_msg(); + + return key_encoder.read_all(); + } + +/* +* Load information from the X509_Cert_Options +*/ +void load_info(const X509_Cert_Options& opts, X509_DN& subject_dn, + AlternativeName& subject_alt) + { + subject_dn.add_attribute("X520.CommonName", opts.common_name); + subject_dn.add_attribute("X520.Country", opts.country); + subject_dn.add_attribute("X520.State", opts.state); + subject_dn.add_attribute("X520.Locality", opts.locality); + subject_dn.add_attribute("X520.Organization", opts.organization); + subject_dn.add_attribute("X520.OrganizationalUnit", opts.org_unit); + subject_dn.add_attribute("X520.SerialNumber", opts.serial_number); + subject_alt = AlternativeName(opts.email, opts.uri, opts.dns, opts.ip); + subject_alt.add_othername(OIDS::lookup("PKIX.XMPPAddr"), + opts.xmpp, UTF8_STRING); + } + +} + +namespace X509 { + +/* +* Create a new self-signed X.509 certificate +*/ +X509_Certificate create_self_signed_cert(const X509_Cert_Options& opts, + const Private_Key& key, + RandomNumberGenerator& rng) + { + AlgorithmIdentifier sig_algo; + X509_DN subject_dn; + AlternativeName subject_alt; + + MemoryVector<byte> pub_key = shared_setup(opts, key); + std::auto_ptr<PK_Signer> signer(choose_sig_format(key, sig_algo)); + load_info(opts, subject_dn, subject_alt); + + Key_Constraints constraints; + if(opts.is_CA) + constraints = Key_Constraints(KEY_CERT_SIGN | CRL_SIGN); + else + constraints = find_constraints(key, opts.constraints); + + Extensions extensions; + + extensions.add(new Cert_Extension::Subject_Key_ID(pub_key)); + extensions.add(new Cert_Extension::Key_Usage(constraints)); + extensions.add( + new Cert_Extension::Extended_Key_Usage(opts.ex_constraints)); + extensions.add( + new Cert_Extension::Subject_Alternative_Name(subject_alt)); + extensions.add( + new Cert_Extension::Basic_Constraints(opts.is_CA, opts.path_limit)); + + return X509_CA::make_cert(signer.get(), rng, sig_algo, pub_key, + opts.start, opts.end, + subject_dn, subject_dn, + extensions); + } + +/* +* Create a PKCS #10 certificate request +*/ +PKCS10_Request create_cert_req(const X509_Cert_Options& opts, + const Private_Key& key, + RandomNumberGenerator& rng) + { + AlgorithmIdentifier sig_algo; + X509_DN subject_dn; + AlternativeName subject_alt; + + MemoryVector<byte> pub_key = shared_setup(opts, key); + std::auto_ptr<PK_Signer> signer(choose_sig_format(key, sig_algo)); + load_info(opts, subject_dn, subject_alt); + + const u32bit PKCS10_VERSION = 0; + + Extensions extensions; + + extensions.add( + new Cert_Extension::Basic_Constraints(opts.is_CA, opts.path_limit)); + extensions.add( + new Cert_Extension::Key_Usage( + opts.is_CA ? Key_Constraints(KEY_CERT_SIGN | CRL_SIGN) : + find_constraints(key, opts.constraints) + ) + ); + extensions.add( + new Cert_Extension::Extended_Key_Usage(opts.ex_constraints)); + extensions.add( + new Cert_Extension::Subject_Alternative_Name(subject_alt)); + + DER_Encoder tbs_req; + + tbs_req.start_cons(SEQUENCE) + .encode(PKCS10_VERSION) + .encode(subject_dn) + .raw_bytes(pub_key) + .start_explicit(0); + + if(opts.challenge != "") + { + ASN1_String challenge(opts.challenge, DIRECTORY_STRING); + + tbs_req.encode( + Attribute("PKCS9.ChallengePassword", + DER_Encoder().encode(challenge).get_contents() + ) + ); + } + + tbs_req.encode( + Attribute("PKCS9.ExtensionRequest", + DER_Encoder() + .start_cons(SEQUENCE) + .encode(extensions) + .end_cons() + .get_contents() + ) + ) + .end_explicit() + .end_cons(); + + DataSource_Memory source( + X509_Object::make_signed(signer.get(), + rng, + sig_algo, + tbs_req.get_contents()) + ); + + return PKCS10_Request(source); + } + +} + +} diff --git a/botan/src/cert/x509/x509self.h b/botan/src/cert/x509/x509self.h new file mode 100644 index 0000000..bd3e291 --- /dev/null +++ b/botan/src/cert/x509/x509self.h @@ -0,0 +1,198 @@ +/* +* X.509 Self-Signed Certificate +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_X509_SELF_H__ +#define BOTAN_X509_SELF_H__ + +#include <botan/x509cert.h> +#include <botan/pkcs8.h> +#include <botan/pkcs10.h> + +namespace Botan { + +/** +* Options for X.509 certificates. +*/ +class BOTAN_DLL X509_Cert_Options + { + public: + /** + * the subject common name + */ + std::string common_name; + + /** + * the subject counry + */ + std::string country; + + /** + * the subject organization + */ + std::string organization; + + /** + * the subject organizational unit + */ + std::string org_unit; + + /** + * the subject locality + */ + std::string locality; + + /** + * the subject state + */ + std::string state; + + /** + * the subject serial number + */ + std::string serial_number; + + /** + * the subject email adress + */ + std::string email; + + /** + * the subject URI + */ + std::string uri; + + /** + * the subject IPv4 address + */ + std::string ip; + + /** + * the subject DNS + */ + std::string dns; + + /** + * the subject XMPP + */ + std::string xmpp; + + /** + * the subject challenge password + */ + std::string challenge; + + /** + * the subject notBefore + */ + X509_Time start; + /** + * the subject notAfter + */ + X509_Time end; + + /** + * Indicates whether the certificate request + */ + bool is_CA; + + /** + * Indicates the BasicConstraints path limit + */ + u32bit path_limit; + + /** + * The key constraints for the subject public key + */ + Key_Constraints constraints; + + /** + * The key extended constraints for the subject public key + */ + std::vector<OID> ex_constraints; + + /** + * Check the options set in this object for validity. + */ + void sanity_check() const; + + /** + * Mark the certificate as a CA certificate and set the path limit. + * @param limit the path limit to be set in the BasicConstraints extension. + */ + void CA_key(u32bit limit = 1); + + /** + * Set the notBefore of the certificate. + * @param time the notBefore value of the certificate + */ + void not_before(const std::string& time); + + /** + * Set the notAfter of the certificate. + * @param time the notAfter value of the certificate + */ + void not_after(const std::string& time); + + /** + * Add the key constraints of the KeyUsage extension. + * @param constr the constraints to set + */ + void add_constraints(Key_Constraints constr); + + /** + * Add constraints to the ExtendedKeyUsage extension. + * @param oid the oid to add + */ + void add_ex_constraint(const OID& oid); + + /** + * Add constraints to the ExtendedKeyUsage extension. + * @param name the name to look up the oid to add + */ + void add_ex_constraint(const std::string& name); + + /** + * Construct a new options object + * @param opts define the common name of this object. An example for this + * parameter would be "common_name/country/organization/organizational_unit". + * @param expire_time the expiration time (from the current clock in seconds) + */ + X509_Cert_Options(const std::string& opts = "", + u32bit expire_time = 365 * 24 * 60 * 60); + }; + +namespace X509 { + +/** +* Create a self-signed X.509 certificate. +* @param opts the options defining the certificate to create +* @param key the private key used for signing, i.e. the key +* associated with this self-signed certificate +* @param rng the rng to use +* @return the newly created self-signed certificate +*/ +BOTAN_DLL X509_Certificate +create_self_signed_cert(const X509_Cert_Options& opts, + const Private_Key& key, + RandomNumberGenerator& rng); + +/** +* Create a PKCS#10 certificate request. +* @param opts the options defining the request to create +* @param key the key used to sign this request +* @param rng the rng to use +* @return the newly created PKCS#10 request +*/ +BOTAN_DLL PKCS10_Request create_cert_req(const X509_Cert_Options& opts, + const Private_Key& key, + RandomNumberGenerator& rng); + +} + +} + +#endif diff --git a/botan/src/cert/x509/x509stor.cpp b/botan/src/cert/x509/x509stor.cpp new file mode 100644 index 0000000..cb61bc2 --- /dev/null +++ b/botan/src/cert/x509/x509stor.cpp @@ -0,0 +1,695 @@ +/* +* X.509 Certificate Store +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/x509stor.h> +#include <botan/parsing.h> +#include <botan/pubkey.h> +#include <botan/look_pk.h> +#include <botan/oids.h> +#include <botan/util.h> +#include <algorithm> +#include <memory> + +namespace Botan { + +namespace { + +/* +* Do a validity check +*/ +s32bit validity_check(const X509_Time& start, const X509_Time& end, + u64bit current_time, u32bit slack) + { + const s32bit NOT_YET_VALID = -1, VALID_TIME = 0, EXPIRED = 1; + + if(start.cmp(current_time + slack) > 0) + return NOT_YET_VALID; + if(end.cmp(current_time - slack) < 0) + return EXPIRED; + return VALID_TIME; + } + +/* +* Compare the value of unique ID fields +*/ +bool compare_ids(const MemoryVector<byte>& id1, + const MemoryVector<byte>& id2) + { + if(!id1.size() || !id2.size()) + return true; + return (id1 == id2); + } + +/* +* Check a particular usage restriction +*/ +bool check_usage(const X509_Certificate& cert, X509_Store::Cert_Usage usage, + X509_Store::Cert_Usage check_for, Key_Constraints constraints) + { + if((usage & check_for) == 0) + return true; + if(cert.constraints() == NO_CONSTRAINTS) + return true; + if(cert.constraints() & constraints) + return true; + return false; + } + +/* +* Check a particular usage restriction +*/ +bool check_usage(const X509_Certificate& cert, X509_Store::Cert_Usage usage, + X509_Store::Cert_Usage check_for, + const std::string& usage_oid) + { + if((usage & check_for) == 0) + return true; + + const std::vector<std::string> constraints = cert.ex_constraints(); + + if(constraints.empty()) + return true; + + return std::binary_search(constraints.begin(), constraints.end(), + usage_oid); + } + +/* +* Check the usage restrictions +*/ +X509_Code usage_check(const X509_Certificate& cert, + X509_Store::Cert_Usage usage) + { + if(usage == X509_Store::ANY) + return VERIFIED; + + if(!check_usage(cert, usage, X509_Store::CRL_SIGNING, CRL_SIGN)) + return CA_CERT_NOT_FOR_CRL_ISSUER; + + if(!check_usage(cert, usage, X509_Store::TLS_SERVER, "PKIX.ServerAuth")) + return INVALID_USAGE; + if(!check_usage(cert, usage, X509_Store::TLS_CLIENT, "PKIX.ClientAuth")) + return INVALID_USAGE; + if(!check_usage(cert, usage, X509_Store::CODE_SIGNING, "PKIX.CodeSigning")) + return INVALID_USAGE; + if(!check_usage(cert, usage, X509_Store::EMAIL_PROTECTION, + "PKIX.EmailProtection")) + return INVALID_USAGE; + if(!check_usage(cert, usage, X509_Store::TIME_STAMPING, + "PKIX.TimeStamping")) + return INVALID_USAGE; + + return VERIFIED; + } + +} + +/* +* Define equality for revocation data +*/ +bool X509_Store::CRL_Data::operator==(const CRL_Data& other) const + { + if(issuer != other.issuer) + return false; + if(serial != other.serial) + return false; + return compare_ids(auth_key_id, other.auth_key_id); + } + +/* +* Define inequality for revocation data +*/ +bool X509_Store::CRL_Data::operator!=(const CRL_Data& other) const + { + return !((*this) == other); + } + +/* +* Define an ordering for revocation data +*/ +bool X509_Store::CRL_Data::operator<(const X509_Store::CRL_Data& other) const + { + if(*this == other) + return false; + + const MemoryVector<byte>& serial1 = serial; + const MemoryVector<byte>& key_id1 = auth_key_id; + const MemoryVector<byte>& serial2 = other.serial; + const MemoryVector<byte>& key_id2 = other.auth_key_id; + + if(compare_ids(key_id1, key_id2) == false) + { + if(std::lexicographical_compare(key_id1.begin(), key_id1.end(), + key_id2.begin(), key_id2.end())) + return true; + + if(std::lexicographical_compare(key_id2.begin(), key_id2.end(), + key_id1.begin(), key_id1.end())) + return false; + } + + if(compare_ids(serial1, serial2) == false) + { + if(std::lexicographical_compare(serial1.begin(), serial1.end(), + serial2.begin(), serial2.end())) + return true; + + if(std::lexicographical_compare(serial2.begin(), serial2.end(), + serial1.begin(), serial1.end())) + return false; + } + + return (issuer < other.issuer); + } + +/* +* X509_Store Constructor +*/ +X509_Store::X509_Store(u32bit slack, u32bit cache_timeout) + { + revoked_info_valid = true; + + validation_cache_timeout = cache_timeout; + time_slack = slack; + } + +/* +* X509_Store Copy Constructor +*/ +X509_Store::X509_Store(const X509_Store& other) + { + certs = other.certs; + revoked = other.revoked; + revoked_info_valid = other.revoked_info_valid; + for(u32bit j = 0; j != other.stores.size(); ++j) + stores[j] = other.stores[j]->clone(); + time_slack = other.time_slack; + } + +/* +* X509_Store Destructor +*/ +X509_Store::~X509_Store() + { + for(u32bit j = 0; j != stores.size(); ++j) + delete stores[j]; + } + +/* +* Verify a certificate's authenticity +*/ +X509_Code X509_Store::validate_cert(const X509_Certificate& cert, + Cert_Usage cert_usage) + { + recompute_revoked_info(); + + std::vector<u32bit> indexes; + X509_Code chaining_result = construct_cert_chain(cert, indexes); + if(chaining_result != VERIFIED) + return chaining_result; + + const u64bit current_time = system_time(); + + s32bit time_check = validity_check(cert.start_time(), cert.end_time(), + current_time, time_slack); + if(time_check < 0) return CERT_NOT_YET_VALID; + else if(time_check > 0) return CERT_HAS_EXPIRED; + + X509_Code sig_check_result = check_sig(cert, certs[indexes[0]]); + if(sig_check_result != VERIFIED) + return sig_check_result; + + if(is_revoked(cert)) + return CERT_IS_REVOKED; + + for(u32bit j = 0; j != indexes.size() - 1; ++j) + { + const X509_Certificate& current_cert = certs[indexes[j]].cert; + + time_check = validity_check(current_cert.start_time(), + current_cert.end_time(), + current_time, + time_slack); + + if(time_check < 0) return CERT_NOT_YET_VALID; + else if(time_check > 0) return CERT_HAS_EXPIRED; + + sig_check_result = check_sig(certs[indexes[j]], certs[indexes[j+1]]); + if(sig_check_result != VERIFIED) + return sig_check_result; + } + + return usage_check(cert, cert_usage); + } + +/* +* Find this certificate +*/ +u32bit X509_Store::find_cert(const X509_DN& subject_dn, + const MemoryRegion<byte>& subject_key_id) const + { + for(u32bit j = 0; j != certs.size(); ++j) + { + const X509_Certificate& this_cert = certs[j].cert; + if(compare_ids(this_cert.subject_key_id(), subject_key_id) && + this_cert.subject_dn() == subject_dn) + return j; + } + return NO_CERT_FOUND; + } + +/* +* Find the parent of this certificate +*/ +u32bit X509_Store::find_parent_of(const X509_Certificate& cert) + { + const X509_DN issuer_dn = cert.issuer_dn(); + const MemoryVector<byte> auth_key_id = cert.authority_key_id(); + + u32bit index = find_cert(issuer_dn, auth_key_id); + + if(index != NO_CERT_FOUND) + return index; + + if(auth_key_id.size()) + { + for(u32bit j = 0; j != stores.size(); ++j) + { + std::vector<X509_Certificate> got = stores[j]->by_SKID(auth_key_id); + + if(got.empty()) + continue; + + for(u32bit k = 0; k != got.size(); ++k) + add_cert(got[k]); + return find_cert(issuer_dn, auth_key_id); + } + } + + return NO_CERT_FOUND; + } + +/* +* Construct a chain of certificate relationships +*/ +X509_Code X509_Store::construct_cert_chain(const X509_Certificate& end_cert, + std::vector<u32bit>& indexes, + bool need_full_chain) + { + u32bit parent = find_parent_of(end_cert); + + while(true) + { + if(parent == NO_CERT_FOUND) + return CERT_ISSUER_NOT_FOUND; + indexes.push_back(parent); + + if(certs[parent].is_verified(validation_cache_timeout)) + if(certs[parent].verify_result() != VERIFIED) + return certs[parent].verify_result(); + + const X509_Certificate& parent_cert = certs[parent].cert; + if(!parent_cert.is_CA_cert()) + return CA_CERT_NOT_FOR_CERT_ISSUER; + + if(certs[parent].is_trusted()) + break; + if(parent_cert.is_self_signed()) + return CANNOT_ESTABLISH_TRUST; + + if(parent_cert.path_limit() < indexes.size() - 1) + return CERT_CHAIN_TOO_LONG; + + parent = find_parent_of(parent_cert); + } + + if(need_full_chain) + return VERIFIED; + + while(true) + { + if(indexes.size() < 2) + break; + + const u32bit cert = indexes.back(); + + if(certs[cert].is_verified(validation_cache_timeout)) + { + if(certs[cert].verify_result() != VERIFIED) + throw Internal_Error("X509_Store::construct_cert_chain"); + indexes.pop_back(); + } + else + break; + } + + const u32bit last_cert = indexes.back(); + const u32bit parent_of_last_cert = find_parent_of(certs[last_cert].cert); + if(parent_of_last_cert == NO_CERT_FOUND) + return CERT_ISSUER_NOT_FOUND; + indexes.push_back(parent_of_last_cert); + + return VERIFIED; + } + +/* +* Check the CAs signature on a certificate +*/ +X509_Code X509_Store::check_sig(const Cert_Info& cert_info, + const Cert_Info& ca_cert_info) const + { + if(cert_info.is_verified(validation_cache_timeout)) + return cert_info.verify_result(); + + const X509_Certificate& cert = cert_info.cert; + const X509_Certificate& ca_cert = ca_cert_info.cert; + + X509_Code verify_code = check_sig(cert, ca_cert.subject_public_key()); + + cert_info.set_result(verify_code); + + return verify_code; + } + +/* +* Check a CA's signature +*/ +X509_Code X509_Store::check_sig(const X509_Object& object, Public_Key* key) + { + std::auto_ptr<Public_Key> pub_key(key); + std::auto_ptr<PK_Verifier> verifier; + + try { + std::vector<std::string> sig_info = + split_on(OIDS::lookup(object.signature_algorithm().oid), '/'); + + if(sig_info.size() != 2 || sig_info[0] != pub_key->algo_name()) + return SIGNATURE_ERROR; + + std::string padding = sig_info[1]; + Signature_Format format; + if(key->message_parts() >= 2) format = DER_SEQUENCE; + else format = IEEE_1363; + + if(dynamic_cast<PK_Verifying_with_MR_Key*>(pub_key.get())) + { + PK_Verifying_with_MR_Key* sig_key = + dynamic_cast<PK_Verifying_with_MR_Key*>(pub_key.get()); + verifier.reset(get_pk_verifier(*sig_key, padding, format)); + } + else if(dynamic_cast<PK_Verifying_wo_MR_Key*>(pub_key.get())) + { + PK_Verifying_wo_MR_Key* sig_key = + dynamic_cast<PK_Verifying_wo_MR_Key*>(pub_key.get()); + verifier.reset(get_pk_verifier(*sig_key, padding, format)); + } + else + return CA_CERT_CANNOT_SIGN; + + bool valid = verifier->verify_message(object.tbs_data(), + object.signature()); + + if(valid) + return VERIFIED; + else + return SIGNATURE_ERROR; + } + catch(Decoding_Error) { return CERT_FORMAT_ERROR; } + catch(Exception) {} + + return UNKNOWN_X509_ERROR; + } + +/* +* Recompute the revocation status of the certs +*/ +void X509_Store::recompute_revoked_info() const + { + if(revoked_info_valid) + return; + + for(u32bit j = 0; j != certs.size(); ++j) + { + if((certs[j].is_verified(validation_cache_timeout)) && + (certs[j].verify_result() != VERIFIED)) + continue; + + if(is_revoked(certs[j].cert)) + certs[j].set_result(CERT_IS_REVOKED); + } + + revoked_info_valid = true; + } + +/* +* Check if a certificate is revoked +*/ +bool X509_Store::is_revoked(const X509_Certificate& cert) const + { + CRL_Data revoked_info; + revoked_info.issuer = cert.issuer_dn(); + revoked_info.serial = cert.serial_number(); + revoked_info.auth_key_id = cert.authority_key_id(); + + if(std::binary_search(revoked.begin(), revoked.end(), revoked_info)) + return true; + return false; + } + +/* +* Retrieve all the certificates in the store +*/ +std::vector<X509_Certificate> +X509_Store::get_certs(const Search_Func& search) const + { + std::vector<X509_Certificate> found_certs; + for(u32bit j = 0; j != certs.size(); ++j) + { + if(search.match(certs[j].cert)) + found_certs.push_back(certs[j].cert); + } + return found_certs; + } + +/* +* Construct a path back to a root for this cert +*/ +std::vector<X509_Certificate> +X509_Store::get_cert_chain(const X509_Certificate& cert) + { + std::vector<X509_Certificate> result; + std::vector<u32bit> indexes; + X509_Code chaining_result = construct_cert_chain(cert, indexes, true); + + if(chaining_result != VERIFIED) + throw Invalid_State("X509_Store::get_cert_chain: Can't construct chain"); + + for(u32bit j = 0; j != indexes.size(); ++j) + result.push_back(certs[indexes[j]].cert); + return result; + } + +/* +* Add a certificate store to the list of stores +*/ +void X509_Store::add_new_certstore(Certificate_Store* certstore) + { + stores.push_back(certstore); + } + +/* +* Add a certificate to the store +*/ +void X509_Store::add_cert(const X509_Certificate& cert, bool trusted) + { + if(trusted && !cert.is_self_signed()) + throw Invalid_Argument("X509_Store: Trusted certs must be self-signed"); + + if(find_cert(cert.subject_dn(), cert.subject_key_id()) == NO_CERT_FOUND) + { + revoked_info_valid = false; + Cert_Info info(cert, trusted); + certs.push_back(info); + } + else if(trusted) + { + for(u32bit j = 0; j != certs.size(); ++j) + { + const X509_Certificate& this_cert = certs[j].cert; + if(this_cert == cert) + certs[j].trusted = trusted; + } + } + } + +/* +* Add one or more certificates to the store +*/ +void X509_Store::do_add_certs(DataSource& source, bool trusted) + { + while(!source.end_of_data()) + { + try { + X509_Certificate cert(source); + add_cert(cert, trusted); + } + catch(Decoding_Error) {} + catch(Invalid_Argument) {} + } + } + +/* +* Add one or more certificates to the store +*/ +void X509_Store::add_certs(DataSource& source) + { + do_add_certs(source, false); + } + +/* +* Add one or more certificates to the store +*/ +void X509_Store::add_trusted_certs(DataSource& source) + { + do_add_certs(source, true); + } + +/* +* Add one or more certificates to the store +*/ +X509_Code X509_Store::add_crl(const X509_CRL& crl) + { + s32bit time_check = validity_check(crl.this_update(), crl.next_update(), + system_time(), time_slack); + + if(time_check < 0) return CRL_NOT_YET_VALID; + else if(time_check > 0) return CRL_HAS_EXPIRED; + + u32bit cert_index = NO_CERT_FOUND; + + for(u32bit j = 0; j != certs.size(); ++j) + { + const X509_Certificate& this_cert = certs[j].cert; + if(compare_ids(this_cert.subject_key_id(), crl.authority_key_id())) + { + if(this_cert.subject_dn() == crl.issuer_dn()) + cert_index = j; + } + } + + if(cert_index == NO_CERT_FOUND) + return CRL_ISSUER_NOT_FOUND; + + const X509_Certificate& ca_cert = certs[cert_index].cert; + + X509_Code verify_result = validate_cert(ca_cert, CRL_SIGNING); + if(verify_result != VERIFIED) + return verify_result; + + verify_result = check_sig(crl, ca_cert.subject_public_key()); + if(verify_result != VERIFIED) + return verify_result; + + std::vector<CRL_Entry> revoked_certs = crl.get_revoked(); + + for(u32bit j = 0; j != revoked_certs.size(); ++j) + { + CRL_Data revoked_info; + revoked_info.issuer = crl.issuer_dn(); + revoked_info.serial = revoked_certs[j].serial_number(); + revoked_info.auth_key_id = crl.authority_key_id(); + + std::vector<CRL_Data>::iterator p = + std::find(revoked.begin(), revoked.end(), revoked_info); + + if(revoked_certs[j].reason_code() == REMOVE_FROM_CRL) + { + if(p == revoked.end()) continue; + revoked.erase(p); + } + else + { + if(p != revoked.end()) continue; + revoked.push_back(revoked_info); + } + } + + std::sort(revoked.begin(), revoked.end()); + revoked_info_valid = false; + + return VERIFIED; + } + +/* +* PEM encode the set of certificates +*/ +std::string X509_Store::PEM_encode() const + { + std::string cert_store; + for(u32bit j = 0; j != certs.size(); ++j) + cert_store += certs[j].cert.PEM_encode(); + return cert_store; + } + +/* +* Create a Cert_Info structure +*/ +X509_Store::Cert_Info::Cert_Info(const X509_Certificate& c, + bool t) : cert(c), trusted(t) + { + checked = false; + result = UNKNOWN_X509_ERROR; + last_checked = 0; + } + +/* +* Return the verification results +*/ +X509_Code X509_Store::Cert_Info::verify_result() const + { + if(!checked) + throw Invalid_State("Cert_Info::verify_result() called; not checked"); + return result; + } + +/* +* Set the verification results +*/ +void X509_Store::Cert_Info::set_result(X509_Code code) const + { + result = code; + last_checked = system_time(); + checked = true; + } + +/* +* Check if this certificate can be trusted +*/ +bool X509_Store::Cert_Info::is_trusted() const + { + return trusted; + } + +/* +* Check if this certificate has been verified +*/ +bool X509_Store::Cert_Info::is_verified(u32bit timeout) const + { + if(!checked) + return false; + if(result != VERIFIED && result != CERT_NOT_YET_VALID) + return true; + + const u64bit current_time = system_time(); + + if(current_time > last_checked + timeout) + checked = false; + + return checked; + } + +} diff --git a/botan/src/cert/x509/x509stor.h b/botan/src/cert/x509/x509stor.h new file mode 100644 index 0000000..4e60378 --- /dev/null +++ b/botan/src/cert/x509/x509stor.h @@ -0,0 +1,143 @@ +/* +* X.509 Certificate Store +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_X509_CERT_STORE_H__ +#define BOTAN_X509_CERT_STORE_H__ + +#include <botan/x509cert.h> +#include <botan/x509_crl.h> +#include <botan/certstor.h> + +namespace Botan { + +/* +* X.509 Certificate Validation Result +*/ +enum X509_Code { + VERIFIED, + UNKNOWN_X509_ERROR, + CANNOT_ESTABLISH_TRUST, + CERT_CHAIN_TOO_LONG, + SIGNATURE_ERROR, + POLICY_ERROR, + INVALID_USAGE, + + CERT_FORMAT_ERROR, + CERT_ISSUER_NOT_FOUND, + CERT_NOT_YET_VALID, + CERT_HAS_EXPIRED, + CERT_IS_REVOKED, + + CRL_FORMAT_ERROR, + CRL_ISSUER_NOT_FOUND, + CRL_NOT_YET_VALID, + CRL_HAS_EXPIRED, + + CA_CERT_CANNOT_SIGN, + CA_CERT_NOT_FOR_CERT_ISSUER, + CA_CERT_NOT_FOR_CRL_ISSUER +}; + +/* +* X.509 Certificate Store +*/ +class BOTAN_DLL X509_Store + { + public: + class BOTAN_DLL Search_Func + { + public: + virtual bool match(const X509_Certificate&) const = 0; + virtual ~Search_Func() {} + }; + + enum Cert_Usage { + ANY = 0x00, + TLS_SERVER = 0x01, + TLS_CLIENT = 0x02, + CODE_SIGNING = 0x04, + EMAIL_PROTECTION = 0x08, + TIME_STAMPING = 0x10, + CRL_SIGNING = 0x20 + }; + + X509_Code validate_cert(const X509_Certificate&, Cert_Usage = ANY); + + std::vector<X509_Certificate> get_certs(const Search_Func&) const; + std::vector<X509_Certificate> get_cert_chain(const X509_Certificate&); + std::string PEM_encode() const; + + /* + * Made CRL_Data public for XLC for Cell 0.9, otherwise cannot + * instantiate member variable std::vector<CRL_Data> revoked + */ + class BOTAN_DLL CRL_Data + { + public: + X509_DN issuer; + MemoryVector<byte> serial, auth_key_id; + bool operator==(const CRL_Data&) const; + bool operator!=(const CRL_Data&) const; + bool operator<(const CRL_Data&) const; + }; + + X509_Code add_crl(const X509_CRL&); + void add_cert(const X509_Certificate&, bool = false); + void add_certs(DataSource&); + void add_trusted_certs(DataSource&); + + void add_new_certstore(Certificate_Store*); + + static X509_Code check_sig(const X509_Object&, Public_Key*); + + X509_Store(u32bit time_slack = 24*60*60, + u32bit cache_results = 30*60); + + X509_Store(const X509_Store&); + ~X509_Store(); + private: + X509_Store& operator=(const X509_Store&) { return (*this); } + + class BOTAN_DLL Cert_Info + { + public: + bool is_verified(u32bit timeout) const; + bool is_trusted() const; + X509_Code verify_result() const; + void set_result(X509_Code) const; + Cert_Info(const X509_Certificate&, bool = false); + + X509_Certificate cert; + bool trusted; + private: + mutable bool checked; + mutable X509_Code result; + mutable u64bit last_checked; + }; + + u32bit find_cert(const X509_DN&, const MemoryRegion<byte>&) const; + X509_Code check_sig(const Cert_Info&, const Cert_Info&) const; + void recompute_revoked_info() const; + + void do_add_certs(DataSource&, bool); + X509_Code construct_cert_chain(const X509_Certificate&, + std::vector<u32bit>&, bool = false); + + u32bit find_parent_of(const X509_Certificate&); + bool is_revoked(const X509_Certificate&) const; + + static const u32bit NO_CERT_FOUND = 0xFFFFFFFF; + std::vector<Cert_Info> certs; + std::vector<CRL_Data> revoked; + std::vector<Certificate_Store*> stores; + u32bit time_slack, validation_cache_timeout; + mutable bool revoked_info_valid; + }; + +} + +#endif diff --git a/botan/src/checksum/adler32/adler32.cpp b/botan/src/checksum/adler32/adler32.cpp new file mode 100644 index 0000000..c66943b --- /dev/null +++ b/botan/src/checksum/adler32/adler32.cpp @@ -0,0 +1,74 @@ +/* +* Adler32 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/adler32.h> +#include <botan/loadstor.h> + +namespace Botan { + +/* +* Adler32 Checksum +*/ +void Adler32::hash(const byte input[], u32bit length) + { + u32bit S1x = S1, S2x = S2; + while(length >= 16) + { + S1x += input[ 0]; S2x += S1x; + S1x += input[ 1]; S2x += S1x; + S1x += input[ 2]; S2x += S1x; + S1x += input[ 3]; S2x += S1x; + S1x += input[ 4]; S2x += S1x; + S1x += input[ 5]; S2x += S1x; + S1x += input[ 6]; S2x += S1x; + S1x += input[ 7]; S2x += S1x; + S1x += input[ 8]; S2x += S1x; + S1x += input[ 9]; S2x += S1x; + S1x += input[10]; S2x += S1x; + S1x += input[11]; S2x += S1x; + S1x += input[12]; S2x += S1x; + S1x += input[13]; S2x += S1x; + S1x += input[14]; S2x += S1x; + S1x += input[15]; S2x += S1x; + input += 16; + length -= 16; + } + for(u32bit j = 0; j != length; ++j) + { + S1x += input[j]; S2x += S1x; + } + S1x %= 65521; + S2x %= 65521; + S1 = S1x; + S2 = S2x; + } + +/* +* Update an Adler32 Checksum +*/ +void Adler32::add_data(const byte input[], u32bit length) + { + const u32bit PROCESS_AMOUNT = 5552; + while(length >= PROCESS_AMOUNT) + { + hash(input, PROCESS_AMOUNT); + input += PROCESS_AMOUNT; + length -= PROCESS_AMOUNT; + } + hash(input, length); + } + +/* +* Finalize an Adler32 Checksum +*/ +void Adler32::final_result(byte output[]) + { + store_be(output, S2, S1); + clear(); + } + +} diff --git a/botan/src/checksum/adler32/adler32.h b/botan/src/checksum/adler32/adler32.h new file mode 100644 index 0000000..98a28bc --- /dev/null +++ b/botan/src/checksum/adler32/adler32.h @@ -0,0 +1,35 @@ +/* +* Adler32 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ADLER32_H__ +#define BOTAN_ADLER32_H__ + +#include <botan/hash.h> + +namespace Botan { + +/* +* Adler32 +*/ +class BOTAN_DLL Adler32 : public HashFunction + { + public: + void clear() throw() { S1 = 1; S2 = 0; } + std::string name() const { return "Adler32"; } + HashFunction* clone() const { return new Adler32; } + Adler32() : HashFunction(4) { clear(); } + ~Adler32() { clear(); } + private: + void add_data(const byte[], u32bit); + void final_result(byte[]); + void hash(const byte[], u32bit); + u16bit S1, S2; + }; + +} + +#endif diff --git a/botan/src/checksum/adler32/info.txt b/botan/src/checksum/adler32/info.txt new file mode 100644 index 0000000..76662cd --- /dev/null +++ b/botan/src/checksum/adler32/info.txt @@ -0,0 +1,14 @@ +realname "Adler32" + +define ADLER32 + +load_on auto + +<add> +adler32.cpp +adler32.h +</add> + +<requires> +hash +</requires> diff --git a/botan/src/checksum/crc24/crc24.cpp b/botan/src/checksum/crc24/crc24.cpp new file mode 100644 index 0000000..e50b4d3 --- /dev/null +++ b/botan/src/checksum/crc24/crc24.cpp @@ -0,0 +1,100 @@ +/* +* CRC24 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/crc24.h> +#include <botan/loadstor.h> + +namespace Botan { + +/* +* Update a CRC24 Checksum +*/ +void CRC24::add_data(const byte input[], u32bit length) + { + const u32bit TABLE[256] = { + 0x00000000, 0x00864CFB, 0x008AD50D, 0x000C99F6, 0x0093E6E1, 0x0015AA1A, + 0x001933EC, 0x009F7F17, 0x00A18139, 0x0027CDC2, 0x002B5434, 0x00AD18CF, + 0x003267D8, 0x00B42B23, 0x00B8B2D5, 0x003EFE2E, 0x00C54E89, 0x00430272, + 0x004F9B84, 0x00C9D77F, 0x0056A868, 0x00D0E493, 0x00DC7D65, 0x005A319E, + 0x0064CFB0, 0x00E2834B, 0x00EE1ABD, 0x00685646, 0x00F72951, 0x007165AA, + 0x007DFC5C, 0x00FBB0A7, 0x000CD1E9, 0x008A9D12, 0x008604E4, 0x0000481F, + 0x009F3708, 0x00197BF3, 0x0015E205, 0x0093AEFE, 0x00AD50D0, 0x002B1C2B, + 0x002785DD, 0x00A1C926, 0x003EB631, 0x00B8FACA, 0x00B4633C, 0x00322FC7, + 0x00C99F60, 0x004FD39B, 0x00434A6D, 0x00C50696, 0x005A7981, 0x00DC357A, + 0x00D0AC8C, 0x0056E077, 0x00681E59, 0x00EE52A2, 0x00E2CB54, 0x006487AF, + 0x00FBF8B8, 0x007DB443, 0x00712DB5, 0x00F7614E, 0x0019A3D2, 0x009FEF29, + 0x009376DF, 0x00153A24, 0x008A4533, 0x000C09C8, 0x0000903E, 0x0086DCC5, + 0x00B822EB, 0x003E6E10, 0x0032F7E6, 0x00B4BB1D, 0x002BC40A, 0x00AD88F1, + 0x00A11107, 0x00275DFC, 0x00DCED5B, 0x005AA1A0, 0x00563856, 0x00D074AD, + 0x004F0BBA, 0x00C94741, 0x00C5DEB7, 0x0043924C, 0x007D6C62, 0x00FB2099, + 0x00F7B96F, 0x0071F594, 0x00EE8A83, 0x0068C678, 0x00645F8E, 0x00E21375, + 0x0015723B, 0x00933EC0, 0x009FA736, 0x0019EBCD, 0x008694DA, 0x0000D821, + 0x000C41D7, 0x008A0D2C, 0x00B4F302, 0x0032BFF9, 0x003E260F, 0x00B86AF4, + 0x002715E3, 0x00A15918, 0x00ADC0EE, 0x002B8C15, 0x00D03CB2, 0x00567049, + 0x005AE9BF, 0x00DCA544, 0x0043DA53, 0x00C596A8, 0x00C90F5E, 0x004F43A5, + 0x0071BD8B, 0x00F7F170, 0x00FB6886, 0x007D247D, 0x00E25B6A, 0x00641791, + 0x00688E67, 0x00EEC29C, 0x003347A4, 0x00B50B5F, 0x00B992A9, 0x003FDE52, + 0x00A0A145, 0x0026EDBE, 0x002A7448, 0x00AC38B3, 0x0092C69D, 0x00148A66, + 0x00181390, 0x009E5F6B, 0x0001207C, 0x00876C87, 0x008BF571, 0x000DB98A, + 0x00F6092D, 0x007045D6, 0x007CDC20, 0x00FA90DB, 0x0065EFCC, 0x00E3A337, + 0x00EF3AC1, 0x0069763A, 0x00578814, 0x00D1C4EF, 0x00DD5D19, 0x005B11E2, + 0x00C46EF5, 0x0042220E, 0x004EBBF8, 0x00C8F703, 0x003F964D, 0x00B9DAB6, + 0x00B54340, 0x00330FBB, 0x00AC70AC, 0x002A3C57, 0x0026A5A1, 0x00A0E95A, + 0x009E1774, 0x00185B8F, 0x0014C279, 0x00928E82, 0x000DF195, 0x008BBD6E, + 0x00872498, 0x00016863, 0x00FAD8C4, 0x007C943F, 0x00700DC9, 0x00F64132, + 0x00693E25, 0x00EF72DE, 0x00E3EB28, 0x0065A7D3, 0x005B59FD, 0x00DD1506, + 0x00D18CF0, 0x0057C00B, 0x00C8BF1C, 0x004EF3E7, 0x00426A11, 0x00C426EA, + 0x002AE476, 0x00ACA88D, 0x00A0317B, 0x00267D80, 0x00B90297, 0x003F4E6C, + 0x0033D79A, 0x00B59B61, 0x008B654F, 0x000D29B4, 0x0001B042, 0x0087FCB9, + 0x001883AE, 0x009ECF55, 0x009256A3, 0x00141A58, 0x00EFAAFF, 0x0069E604, + 0x00657FF2, 0x00E33309, 0x007C4C1E, 0x00FA00E5, 0x00F69913, 0x0070D5E8, + 0x004E2BC6, 0x00C8673D, 0x00C4FECB, 0x0042B230, 0x00DDCD27, 0x005B81DC, + 0x0057182A, 0x00D154D1, 0x0026359F, 0x00A07964, 0x00ACE092, 0x002AAC69, + 0x00B5D37E, 0x00339F85, 0x003F0673, 0x00B94A88, 0x0087B4A6, 0x0001F85D, + 0x000D61AB, 0x008B2D50, 0x00145247, 0x00921EBC, 0x009E874A, 0x0018CBB1, + 0x00E37B16, 0x006537ED, 0x0069AE1B, 0x00EFE2E0, 0x00709DF7, 0x00F6D10C, + 0x00FA48FA, 0x007C0401, 0x0042FA2F, 0x00C4B6D4, 0x00C82F22, 0x004E63D9, + 0x00D11CCE, 0x00575035, 0x005BC9C3, 0x00DD8538 }; + + u32bit tmp = crc; + while(length >= 16) + { + tmp = TABLE[((tmp >> 16) ^ input[ 0]) & 0xFF] ^ (tmp << 8); + tmp = TABLE[((tmp >> 16) ^ input[ 1]) & 0xFF] ^ (tmp << 8); + tmp = TABLE[((tmp >> 16) ^ input[ 2]) & 0xFF] ^ (tmp << 8); + tmp = TABLE[((tmp >> 16) ^ input[ 3]) & 0xFF] ^ (tmp << 8); + tmp = TABLE[((tmp >> 16) ^ input[ 4]) & 0xFF] ^ (tmp << 8); + tmp = TABLE[((tmp >> 16) ^ input[ 5]) & 0xFF] ^ (tmp << 8); + tmp = TABLE[((tmp >> 16) ^ input[ 6]) & 0xFF] ^ (tmp << 8); + tmp = TABLE[((tmp >> 16) ^ input[ 7]) & 0xFF] ^ (tmp << 8); + tmp = TABLE[((tmp >> 16) ^ input[ 8]) & 0xFF] ^ (tmp << 8); + tmp = TABLE[((tmp >> 16) ^ input[ 9]) & 0xFF] ^ (tmp << 8); + tmp = TABLE[((tmp >> 16) ^ input[10]) & 0xFF] ^ (tmp << 8); + tmp = TABLE[((tmp >> 16) ^ input[11]) & 0xFF] ^ (tmp << 8); + tmp = TABLE[((tmp >> 16) ^ input[12]) & 0xFF] ^ (tmp << 8); + tmp = TABLE[((tmp >> 16) ^ input[13]) & 0xFF] ^ (tmp << 8); + tmp = TABLE[((tmp >> 16) ^ input[14]) & 0xFF] ^ (tmp << 8); + tmp = TABLE[((tmp >> 16) ^ input[15]) & 0xFF] ^ (tmp << 8); + input += 16; + length -= 16; + } + for(u32bit j = 0; j != length; ++j) + tmp = TABLE[((tmp >> 16) ^ input[j]) & 0xFF] ^ (tmp << 8); + crc = tmp; + } + +/* +* Finalize a CRC24 Checksum +*/ +void CRC24::final_result(byte output[]) + { + for(u32bit j = 0; j != 3; ++j) + output[j] = get_byte(j+1, crc); + clear(); + } + +} diff --git a/botan/src/checksum/crc24/crc24.h b/botan/src/checksum/crc24/crc24.h new file mode 100644 index 0000000..bca4d0e --- /dev/null +++ b/botan/src/checksum/crc24/crc24.h @@ -0,0 +1,34 @@ +/* +* CRC24 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_CRC24_H__ +#define BOTAN_CRC24_H__ + +#include <botan/hash.h> + +namespace Botan { + +/* +* CRC24 +*/ +class BOTAN_DLL CRC24 : public HashFunction + { + public: + void clear() throw() { crc = 0xB704CE; } + std::string name() const { return "CRC24"; } + HashFunction* clone() const { return new CRC24; } + CRC24() : HashFunction(3) { clear(); } + ~CRC24() { clear(); } + private: + void add_data(const byte[], u32bit); + void final_result(byte[]); + u32bit crc; + }; + +} + +#endif diff --git a/botan/src/checksum/crc24/info.txt b/botan/src/checksum/crc24/info.txt new file mode 100644 index 0000000..33b86a9 --- /dev/null +++ b/botan/src/checksum/crc24/info.txt @@ -0,0 +1,14 @@ +realname "CRC-24" + +define CRC24 + +load_on auto + +<add> +crc24.cpp +crc24.h +</add> + +<requires> +hash +</requires> diff --git a/botan/src/checksum/crc32/crc32.cpp b/botan/src/checksum/crc32/crc32.cpp new file mode 100644 index 0000000..4246209 --- /dev/null +++ b/botan/src/checksum/crc32/crc32.cpp @@ -0,0 +1,102 @@ +/* +* CRC32 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/crc32.h> +#include <botan/loadstor.h> + +namespace Botan { + +/* +* Update a CRC32 Checksum +*/ +void CRC32::add_data(const byte input[], u32bit length) + { + const u32bit TABLE[256] = { + 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, + 0xE963A535, 0x9E6495A3, 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, + 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, 0x1DB71064, 0x6AB020F2, + 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, + 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9, + 0xFA0F3D63, 0x8D080DF5, 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, + 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, 0x35B5A8FA, 0x42B2986C, + 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59, + 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, + 0xCFBA9599, 0xB8BDA50F, 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, + 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, 0x76DC4190, 0x01DB7106, + 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433, + 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D, + 0x91646C97, 0xE6635C01, 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, + 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 0x65B0D9C6, 0x12B7E950, + 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65, + 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7, + 0xA4D1C46D, 0xD3D6F4FB, 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, + 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, 0x5005713C, 0x270241AA, + 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F, + 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81, + 0xB7BD5C3B, 0xC0BA6CAD, 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, + 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683, 0xE3630B12, 0x94643B84, + 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, + 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB, + 0x196C3671, 0x6E6B06E7, 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, + 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, 0xD6D6A3E8, 0xA1D1937E, + 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B, + 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55, + 0x316E8EEF, 0x4669BE79, 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, + 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, 0xC5BA3BBE, 0xB2BD0B28, + 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D, + 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F, + 0x72076785, 0x05005713, 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, + 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 0x86D3D2D4, 0xF1D4E242, + 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777, + 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69, + 0x616BFFD3, 0x166CCF45, 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, + 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, 0xAED16A4A, 0xD9D65ADC, + 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9, + 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693, + 0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, + 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D }; + + u32bit tmp = crc; + while(length >= 16) + { + tmp = TABLE[(tmp ^ input[ 0]) & 0xFF] ^ (tmp >> 8); + tmp = TABLE[(tmp ^ input[ 1]) & 0xFF] ^ (tmp >> 8); + tmp = TABLE[(tmp ^ input[ 2]) & 0xFF] ^ (tmp >> 8); + tmp = TABLE[(tmp ^ input[ 3]) & 0xFF] ^ (tmp >> 8); + tmp = TABLE[(tmp ^ input[ 4]) & 0xFF] ^ (tmp >> 8); + tmp = TABLE[(tmp ^ input[ 5]) & 0xFF] ^ (tmp >> 8); + tmp = TABLE[(tmp ^ input[ 6]) & 0xFF] ^ (tmp >> 8); + tmp = TABLE[(tmp ^ input[ 7]) & 0xFF] ^ (tmp >> 8); + tmp = TABLE[(tmp ^ input[ 8]) & 0xFF] ^ (tmp >> 8); + tmp = TABLE[(tmp ^ input[ 9]) & 0xFF] ^ (tmp >> 8); + tmp = TABLE[(tmp ^ input[10]) & 0xFF] ^ (tmp >> 8); + tmp = TABLE[(tmp ^ input[11]) & 0xFF] ^ (tmp >> 8); + tmp = TABLE[(tmp ^ input[12]) & 0xFF] ^ (tmp >> 8); + tmp = TABLE[(tmp ^ input[13]) & 0xFF] ^ (tmp >> 8); + tmp = TABLE[(tmp ^ input[14]) & 0xFF] ^ (tmp >> 8); + tmp = TABLE[(tmp ^ input[15]) & 0xFF] ^ (tmp >> 8); + input += 16; + length -= 16; + } + + for(u32bit j = 0; j != length; ++j) + tmp = TABLE[(tmp ^ input[j]) & 0xFF] ^ (tmp >> 8); + + crc = tmp; + } + +/* +* Finalize a CRC32 Checksum +*/ +void CRC32::final_result(byte output[]) + { + crc ^= 0xFFFFFFFF; + store_be(crc, output); + clear(); + } + +} diff --git a/botan/src/checksum/crc32/crc32.h b/botan/src/checksum/crc32/crc32.h new file mode 100644 index 0000000..390fb10 --- /dev/null +++ b/botan/src/checksum/crc32/crc32.h @@ -0,0 +1,34 @@ +/* +* CRC32 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_CRC32_H__ +#define BOTAN_CRC32_H__ + +#include <botan/hash.h> + +namespace Botan { + +/* +* CRC32 +*/ +class BOTAN_DLL CRC32 : public HashFunction + { + public: + void clear() throw() { crc = 0xFFFFFFFF; } + std::string name() const { return "CRC32"; } + HashFunction* clone() const { return new CRC32; } + CRC32() : HashFunction(4) { clear(); } + ~CRC32() { clear(); } + private: + void add_data(const byte[], u32bit); + void final_result(byte[]); + u32bit crc; + }; + +} + +#endif diff --git a/botan/src/checksum/crc32/info.txt b/botan/src/checksum/crc32/info.txt new file mode 100644 index 0000000..15933b3 --- /dev/null +++ b/botan/src/checksum/crc32/info.txt @@ -0,0 +1,14 @@ +realname "CRC-32" + +define CRC32 + +load_on auto + +<add> +crc32.cpp +crc32.h +</add> + +<requires> +hash +</requires> diff --git a/botan/src/cms/cms_algo.cpp b/botan/src/cms/cms_algo.cpp new file mode 100644 index 0000000..748aa73 --- /dev/null +++ b/botan/src/cms/cms_algo.cpp @@ -0,0 +1,164 @@ +/* +* CMS Algorithm Specific Code +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/cms_enc.h> +#include <botan/der_enc.h> +#include <botan/sha160.h> +#include <botan/cbc.h> +#include <botan/filters.h> +#include <botan/libstate.h> + +#if defined(BOTAN_HAS_RC2) + #include <botan/rc2.h> +#endif + +namespace Botan { + +namespace { + +/* +* Wrap a key as specified in RFC 3217 +*/ +SecureVector<byte> do_rfc3217_wrap(RandomNumberGenerator& rng, + const std::string& cipher_name, + const SymmetricKey& kek, + const SecureVector<byte>& input) + { + class Flip_Bytes : public Filter + { + public: + void write(const byte data[], u32bit length) + { + buf.append(data, length); + } + void end_msg() + { + for(u32bit j = 0; j != buf.size(); j++) + send(buf[buf.size()-j-1]); + buf.destroy(); + } + Flip_Bytes(const SecureVector<byte>& prefix) { buf.append(prefix); } + private: + SecureVector<byte> buf; + }; + + Algorithm_Factory& af = global_state().algorithm_factory(); + + const BlockCipher* cipher = af.prototype_block_cipher(cipher_name); + + if(!cipher || cipher->BLOCK_SIZE != 8) + throw Encoding_Error("do_rfc3217_wrap: Bad cipher: " + cipher_name); + + Pipe icv(new Hash_Filter(new SHA_160, 8)); + icv.process_msg(input); + + InitializationVector iv(rng, 8); + InitializationVector fixed("4ADDA22C79E82105"); + + Pipe pipe(new CBC_Encryption(cipher->clone(), new Null_Padding, kek, iv), + new Flip_Bytes(iv.bits_of()), + new CBC_Encryption(cipher->clone(), new Null_Padding, kek, iv)); + + pipe.start_msg(); + pipe.write(input); + pipe.write(icv.read_all()); + pipe.end_msg(); + return pipe.read_all(); + } + +} + +/* +* Wrap a CEK with a KEK +*/ +SecureVector<byte> CMS_Encoder::wrap_key(RandomNumberGenerator& rng, + const std::string& cipher, + const SymmetricKey& cek, + const SymmetricKey& kek) + { +#if defined(BOTAN_HAS_DES) + if(cipher == "TripleDES") + { + SymmetricKey cek_parity = cek; + cek_parity.set_odd_parity(); + return do_rfc3217_wrap(rng, cipher, kek, cek_parity.bits_of()); + } +#endif + +#if defined(BOTAN_HAS_RC2) || defined(BOTAN_HAS_CAST) + if(cipher == "RC2" || cipher == "CAST-128") + { + if(kek.length() != 16) + throw Encoding_Error("CMS: 128-bit KEKs must be used with " + cipher); + + SecureVector<byte> lcekpad; + lcekpad.append((byte)cek.length()); + lcekpad.append(cek.bits_of()); + while(lcekpad.size() % 8) + lcekpad.append(rng.next_byte()); + return do_rfc3217_wrap(rng, cipher, kek, lcekpad); + } +#endif + + throw Invalid_Argument("CMS_Encoder::wrap: Unknown cipher " + cipher); + } + +/* +* Encode the parameters for an encryption algo +*/ +SecureVector<byte> CMS_Encoder::encode_params(const std::string& cipher, + const SymmetricKey& key, + const InitializationVector& iv) + { + DER_Encoder encoder; + +#if defined(BOTAN_HAS_RC2) + if(cipher == "RC2") + { + encoder.start_cons(SEQUENCE). + encode((u32bit)RC2::EKB_code(8*key.length())). + encode(iv.bits_of(), OCTET_STRING). + end_cons(); + return encoder.get_contents(); + } +#endif + + if(cipher == "CAST-128") + { + encoder.start_cons(SEQUENCE). + encode(iv.bits_of(), OCTET_STRING). + encode(8*key.length()). + end_cons(); + } + else + encoder.encode(iv.bits_of(), OCTET_STRING); + + return encoder.get_contents(); + } + +/* +* Generate a CEK or KEK for the cipher +*/ +SymmetricKey CMS_Encoder::setup_key(RandomNumberGenerator& rng, + const std::string& cipher) + { + u32bit keysize = 0; + + if(cipher == "TripleDES") keysize = 24; + if(cipher == "RC2") keysize = 16; + if(cipher == "CAST-128") keysize = 16; + + if(keysize == 0) + throw Invalid_Argument("CMS: Cannot encrypt with cipher " + cipher); + + SymmetricKey key(rng, keysize); + if(cipher == "DES" || cipher == "TripleDES") + key.set_odd_parity(); + return key; + } + +} diff --git a/botan/src/cms/cms_comp.cpp b/botan/src/cms/cms_comp.cpp new file mode 100644 index 0000000..b11cf90 --- /dev/null +++ b/botan/src/cms/cms_comp.cpp @@ -0,0 +1,107 @@ +/* +* CMS Compression +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/cms_enc.h> +#include <botan/cms_dec.h> +#include <botan/der_enc.h> +#include <botan/ber_dec.h> +#include <botan/oids.h> +#include <botan/pipe.h> + +#if defined(BOTAN_HAS_COMPRESSOR_ZLIB) + #include <botan/zlib.h> +#endif + +namespace Botan { + +/* +* Compress a message +*/ +void CMS_Encoder::compress(const std::string& algo) + { + if(!CMS_Encoder::can_compress_with(algo)) + throw Invalid_Argument("CMS_Encoder: Cannot compress with " + algo); + + Filter* compressor = 0; + +#if defined(BOTAN_HAS_COMPRESSOR_ZLIB) + if(algo == "Zlib") compressor = new Zlib_Compression; +#endif + + if(compressor == 0) + throw Internal_Error("CMS: Couldn't get ahold of a compressor"); + + Pipe pipe(compressor); + pipe.process_msg(data); + SecureVector<byte> compressed = pipe.read_all(); + + DER_Encoder encoder; + encoder.start_cons(SEQUENCE). + encode((u32bit)0). + encode(AlgorithmIdentifier("Compression." + algo, + MemoryVector<byte>())). + raw_bytes(make_econtent(compressed, type)). + end_cons(); + + add_layer("CMS.CompressedData", encoder); + } + +/* +* See if the named compression algo is available +*/ +bool CMS_Encoder::can_compress_with(const std::string& algo) + { + if(algo == "") + throw Invalid_Algorithm_Name("Empty string to can_compress_with"); + +#if defined(BOTAN_HAS_COMPRESSOR_ZLIB) + if(algo == "Zlib") + return true; +#endif + + return false; + } + +/* +* Decompress a message +*/ +void CMS_Decoder::decompress(BER_Decoder& decoder) + { + u32bit version; + AlgorithmIdentifier comp_algo; + + BER_Decoder comp_info = decoder.start_cons(SEQUENCE); + + comp_info.decode(version); + if(version != 0) + throw Decoding_Error("CMS: Unknown version for CompressedData"); + + comp_info.decode(comp_algo); + read_econtent(comp_info); + comp_info.end_cons(); + + Filter* decompressor = 0; + + info = comp_algo.oid.as_string(); + +#if defined(BOTAN_HAS_COMPRESSOR_ZLIB) + if(comp_algo.oid == OIDS::lookup("Compression.Zlib")) + { + decompressor = new Zlib_Decompression; + info = "Zlib"; + } +#endif + + if(!decompressor) + status = FAILURE; + + Pipe pipe(decompressor); + pipe.process_msg(data); + data = pipe.read_all(); + } + +} diff --git a/botan/src/cms/cms_dalg.cpp b/botan/src/cms/cms_dalg.cpp new file mode 100644 index 0000000..7ed793f --- /dev/null +++ b/botan/src/cms/cms_dalg.cpp @@ -0,0 +1,276 @@ +/* +* CMS Decoding Operations +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/cms_dec.h> +#include <botan/x509find.h> +#include <botan/ber_dec.h> +#include <botan/oids.h> +#include <botan/hash.h> +#include <botan/look_pk.h> +#include <botan/bigint.h> +#include <botan/libstate.h> +#include <memory> + +namespace Botan { + +namespace { + +/* +* Compute the hash of some content +*/ +SecureVector<byte> hash_of(const SecureVector<byte>& content, + const AlgorithmIdentifier& hash_algo, + std::string& hash_name) + { + hash_name = OIDS::lookup(hash_algo.oid); + + Algorithm_Factory& af = global_state().algorithm_factory(); + + std::auto_ptr<HashFunction> hash_fn(af.make_hash_function(hash_name)); + return hash_fn->process(content); + } + +/* +* Find a cert based on SignerIdentifier +*/ +std::vector<X509_Certificate> get_cert(BER_Decoder& signer_info, + X509_Store& store) + { + BER_Object id = signer_info.get_next_object(); + + std::vector<X509_Certificate> found; + + if(id.type_tag == SEQUENCE && id.class_tag == CONSTRUCTED) + { + X509_DN issuer; + BigInt serial; + BER_Decoder iands(id.value); + iands.decode(issuer); + iands.decode(serial); + + found = store.get_certs(IandS_Match(issuer, BigInt::encode(serial))); + } + else if(id.type_tag == 0 && id.class_tag == CONSTRUCTED) + found = store.get_certs(SKID_Match(id.value)); + else + throw Decoding_Error("CMS: Unknown tag for cert identifier"); + + // verify cert if found + + if(found.size() > 1) + throw Internal_Error("CMS: Found more than one match in get_cert"); + return found; + } + +/* +* Read OriginatorInfo +*/ +void read_orig_info(BER_Decoder& info, X509_Store& store) + { + BER_Object next = info.get_next_object(); + + if(next.type_tag == 0 && + next.class_tag == ASN1_Tag(CONSTRUCTED | CONTEXT_SPECIFIC)) + { + DataSource_Memory certs(next.value); + while(!certs.end_of_data()) + { + // FIXME: can be attribute certs too + // FIXME: DoS? + X509_Certificate cert(certs); + store.add_cert(cert); + } + next = info.get_next_object(); + } + if(next.type_tag == 1 && + next.class_tag == ASN1_Tag(CONSTRUCTED | CONTEXT_SPECIFIC)) + { + DataSource_Memory crls(next.value); + while(!crls.end_of_data()) + { + // FIXME: DoS? + X509_CRL crl(crls); + store.add_crl(crl); + } + next = info.get_next_object(); + } + info.push_back(next); + } + +/* +* Decode any Attributes, and check type +*/ +SecureVector<byte> decode_attributes(BER_Decoder& ber, const OID& type, + bool& bad_attributes) + { + BER_Object obj = ber.get_next_object(); + SecureVector<byte> digest; + + bool got_digest = false; + bool got_content_type = false; + + if(obj.type_tag == 0 && + obj.class_tag == ASN1_Tag(CONSTRUCTED | CONTEXT_SPECIFIC)) + ber.push_back(obj); + else + { + BER_Decoder attributes(obj.value); + while(attributes.more_items()) + { + Attribute attr; + attributes.decode(attr); + BER_Decoder attr_value(attr.parameters); + + if(attr.oid == OIDS::lookup("PKCS9.MessageDigest")) + { + got_digest = true; + attr_value.decode(digest, OCTET_STRING); + } + else if(attr.oid == OIDS::lookup("PKCS9.ContentType")) + { + got_content_type = true; + OID inner_type; + attr_value.decode(inner_type); + if(inner_type != type) + bad_attributes = true; + } + else + throw Decoding_Error("Unknown/unhandled CMS attribute found: " + + OIDS::lookup(attr.oid)); + } + + if(!got_digest || !got_content_type) + bad_attributes = true; + } + + return digest; + } + +} + +/* +* Decode this layer of CMS encoding +*/ +void CMS_Decoder::decode_layer() + { + try { + if(status == FAILURE) + throw Invalid_State("CMS: Decoder is in FAILURE state"); + + status = GOOD; + info = ""; + + type = next_type; + + if(type == OIDS::lookup("CMS.DataContent")) + return; + + BER_Decoder decoder(data); + if(type == OIDS::lookup("CMS.CompressedData")) + decompress(decoder); + else if(type == OIDS::lookup("CMS.DigestedData")) + { + u32bit version; + AlgorithmIdentifier hash_algo; + SecureVector<byte> digest; + + BER_Decoder hash_info = decoder.start_cons(SEQUENCE); + + hash_info.decode(version); + if(version != 0 && version != 2) + throw Decoding_Error("CMS: Unknown version for DigestedData"); + + hash_info.decode(hash_algo); + read_econtent(hash_info); + hash_info.decode(digest, OCTET_STRING); + hash_info.end_cons(); + + if(digest != hash_of(data, hash_algo, info)) + status = BAD; + } + else if(type == OIDS::lookup("CMS.SignedData")) + { +#if 1 + throw Exception("FIXME: not implemented"); +#else + u32bit version; + + BER_Decoder sig_info = BER::get_subsequence(decoder); + BER::decode(sig_info, version); + if(version != 1 && version != 3) + throw Decoding_Error("CMS: Unknown version for SignedData"); + BER::get_subset(sig_info); // hash algos (do something with these?) + read_econtent(sig_info); + read_orig_info(sig_info, store); + + BER_Decoder signer_infos = BER::get_subset(sig_info); + while(signer_infos.more_items()) + { + AlgorithmIdentifier sig_algo, hash_algo; + SecureVector<byte> signature, digest; + u32bit version; + + BER_Decoder signer_info = BER::get_subsequence(signer_infos); + BER::decode(signer_info, version); + if(version != 1 && version != 3) + throw Decoding_Error("CMS: Unknown version for SignerInfo"); + + std::vector<X509_Certificate> certs = get_cert(signer_info, store); + if(certs.size() == 0) { status = NO_KEY; continue; } + + BER::decode(signer_info, hash_algo); + bool bad_attr = false; + digest = decode_attributes(signer_info, next_type, bad_attr); + if(bad_attr) { status = BAD; continue; } + BER::decode(signer_info, sig_algo); + BER::decode(signer_info, signature, OCTET_STRING); + // unsigned attributes + signer_info.verify_end(); + + if(digest.has_items()) + { + std::string hash; + if(digest != hash_of(data, hash_algo, hash)) + { + status = BAD; + continue; + } + status = check_sig(signed_attr, sig_algo, signature, certs[0]); + } + else + status = check_sig(data, sig_algo, signature, certs[0]); + + if(status == BAD) + continue; + + // fix this (gets only last signer, for one thing) + // maybe some way for the user to get all certs that signed the + // message? that would be useful + info = "CN=" + cert.subject_info("CommonName") + + ",O=" + cert.subject_info("Organization") + + ",OU=" + cert.subject_info("Organizational Unit"); + } +#endif + } + else if(type == OIDS::lookup("CMS.EnvelopedData")) + { + throw Exception("FIXME: not implemented"); + } + else if(type == OIDS::lookup("CMS.AuthenticatedData")) + { + throw Exception("FIXME: not implemented"); + } + else + throw Decoding_Error("CMS: Unknown content ID " + type.as_string()); + } + catch(std::exception) + { + status = FAILURE; + } + } + +} diff --git a/botan/src/cms/cms_dec.cpp b/botan/src/cms/cms_dec.cpp new file mode 100644 index 0000000..222399f --- /dev/null +++ b/botan/src/cms/cms_dec.cpp @@ -0,0 +1,127 @@ +/* +* CMS Decoding +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/cms_dec.h> +#include <botan/ber_dec.h> +#include <botan/asn1_int.h> +#include <botan/oids.h> +#include <botan/pem.h> + +namespace Botan { + +/* +* CMS_Decoder Constructor +*/ +CMS_Decoder::CMS_Decoder(DataSource& in, const X509_Store& x509store, + User_Interface& ui_ref, PKCS8_PrivateKey* key) : + ui(ui_ref), store(x509store) + { + status = GOOD; + + add_key(key); + + if(ASN1::maybe_BER(in) && !PEM_Code::matches(in)) + initial_read(in); + else + { + DataSource_Memory ber(PEM_Code::decode_check_label(in, "PKCS7")); + initial_read(ber); + } + } + +/* +* Read the outermost ContentInfo +*/ +void CMS_Decoder::initial_read(DataSource&) + { + // FIXME... + + /* + BER_Decoder decoder(in); + BER_Decoder content_info = decoder.start_cons(SEQUENCE); + + content_info.decode(next_type); + + + BER_Decoder content_type = BER::get_subsequence(content_info, ASN1_Tag(0)); + data = content_type.get_remaining(); + */ + + decode_layer(); + } + +/* +* Add another private key to use +*/ +void CMS_Decoder::add_key(PKCS8_PrivateKey* key) + { + if(!key) + return; + +#if 0 + for(u32bit j = 0; j != keys.size(); j++) + if(keys[j]->key_id() == key->key_id()) + return; +#endif + + keys.push_back(key); + } + +/* +* Return the status information +*/ +CMS_Decoder::Status CMS_Decoder::layer_status() const + { + return status; + } + +/* +* Return the final data content +*/ +std::string CMS_Decoder::get_data() const + { + if(layer_type() != DATA) + throw Invalid_State("CMS: Cannot retrieve data from non-DATA layer"); + return std::string((const char*)data.begin(), data.size()); + } + +/* +* Return the content type of this layer +*/ +CMS_Decoder::Content_Type CMS_Decoder::layer_type() const + { + if(type == OIDS::lookup("CMS.DataContent")) return DATA; + if(type == OIDS::lookup("CMS.EnvelopedData")) return ENVELOPED; + if(type == OIDS::lookup("CMS.CompressedData")) return COMPRESSED; + if(type == OIDS::lookup("CMS.SignedData")) return SIGNED; + if(type == OIDS::lookup("CMS.AuthenticatedData")) return AUTHENTICATED; + if(type == OIDS::lookup("CMS.DigestedData")) return DIGESTED; + return UNKNOWN; + } + +/* +* Return some information about this layer +*/ +std::string CMS_Decoder::layer_info() const + { + return info; + } + +/* +* Return some information about this layer +*/ +void CMS_Decoder::read_econtent(BER_Decoder& decoder) + { + BER_Decoder econtent_info = decoder.start_cons(SEQUENCE); + econtent_info.decode(next_type); + + // FIXME + //BER_Decoder econtent = BER::get_subsequence(econtent_info, ASN1_Tag(0)); + //econtent.decode(data, OCTET_STRING); + } + +} diff --git a/botan/src/cms/cms_dec.h b/botan/src/cms/cms_dec.h new file mode 100644 index 0000000..75b61c9 --- /dev/null +++ b/botan/src/cms/cms_dec.h @@ -0,0 +1,65 @@ +/* +* CMS Decoding +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_CMS_DECODER_H__ +#define BOTAN_CMS_DECODER_H__ + +#include <botan/x509cert.h> +#include <botan/x509stor.h> +#include <botan/pkcs8.h> +#include <botan/ber_dec.h> +#include <botan/ui.h> + +namespace Botan { + +/* +* CMS Decoding Operation +*/ +class BOTAN_DLL CMS_Decoder + { + public: + enum Status { GOOD, BAD, NO_KEY, FAILURE }; + + enum Content_Type { DATA, UNKNOWN, COMPRESSED, ENVELOPED, SIGNED, + AUTHENTICATED, DIGESTED }; + + Status layer_status() const; + Content_Type layer_type() const; + std::string layer_info() const; + std::string layer_algo() const; + std::string get_data() const; + std::vector<X509_Certificate> get_certs() const; + std::vector<X509_CRL> get_crls() const; + + void next_layer() { decode_layer(); } + + void add_key(PKCS8_PrivateKey*); + + CMS_Decoder(DataSource&, const X509_Store&, User_Interface&, + PKCS8_PrivateKey* = 0); + private: + std::string get_passphrase(const std::string&); + void read_econtent(BER_Decoder&); + void initial_read(DataSource&); + void decode_layer(); + void decompress(BER_Decoder&); + + User_Interface& ui; + + X509_Store store; + std::vector<std::string> passphrases; + std::vector<PKCS8_PrivateKey*> keys; + + OID type, next_type; + SecureVector<byte> data; + Status status; + std::string info; + }; + +} + +#endif diff --git a/botan/src/cms/cms_ealg.cpp b/botan/src/cms/cms_ealg.cpp new file mode 100644 index 0000000..2970e8e --- /dev/null +++ b/botan/src/cms/cms_ealg.cpp @@ -0,0 +1,401 @@ +/* +* CMS Encoding Operations +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/cms_enc.h> +#include <botan/der_enc.h> +#include <botan/x509find.h> +#include <botan/bigint.h> +#include <botan/oids.h> +#include <botan/cbc.h> +#include <botan/hash.h> +#include <botan/look_pk.h> +#include <botan/libstate.h> +#include <botan/pipe.h> +#include <memory> + +namespace Botan { + +namespace { + +/* +* Choose an algorithm +*/ +std::string choose_algo(const std::string& user_algo, + const std::string& default_algo) + { + if(user_algo == "") + return global_state().deref_alias(default_algo); + return global_state().deref_alias(user_algo); + } + +/* +* Encode a SignerIdentifier/RecipientIdentifier +*/ +DER_Encoder& encode_si(DER_Encoder& der, const X509_Certificate& cert, + bool use_skid_encoding = false) + { + if(cert.subject_key_id().size() && use_skid_encoding) + der.encode(cert.subject_key_id(), OCTET_STRING, ASN1_Tag(0)); + else + { + der.start_cons(SEQUENCE). + encode(cert.issuer_dn()). + encode(BigInt::decode(cert.serial_number())). + end_cons(); + } + + return der; + } + +/* +* Compute the hash of some content +*/ +SecureVector<byte> hash_of(const SecureVector<byte>& content, + const std::string& hash_name) + { + Algorithm_Factory& af = global_state().algorithm_factory(); + std::auto_ptr<HashFunction> hash_fn(af.make_hash_function(hash_name)); + return hash_fn->process(content); + } + +/* +* Encode Attributes containing info on content +*/ +SecureVector<byte> encode_attr(const SecureVector<byte>& data, + const std::string& type, + const std::string& hash) + { + SecureVector<byte> digest = hash_of(data, hash); + + DER_Encoder encoder; + encoder.encode(OIDS::lookup(type)); + Attribute content_type("PKCS9.ContentType", encoder.get_contents()); + + encoder.encode(digest, OCTET_STRING); + Attribute message_digest("PKCS9.MessageDigest", encoder.get_contents()); + + encoder.start_cons(SET) + .encode(content_type) + .encode(message_digest) + .end_cons(); + + return encoder.get_contents(); + } + +} + +/* +* Encrypt a message +*/ +void CMS_Encoder::encrypt(RandomNumberGenerator& rng, + const X509_Certificate& to, + const std::string user_cipher) + { + const std::string cipher = choose_algo(user_cipher, "TripleDES"); + + std::auto_ptr<X509_PublicKey> key(to.subject_public_key()); + const std::string algo = key->algo_name(); + + Key_Constraints constraints = to.constraints(); + + if(algo == "RSA") + { + if(constraints != NO_CONSTRAINTS && !(constraints & KEY_ENCIPHERMENT)) + throw Invalid_Argument("CMS: Constraints not set for encryption"); + + PK_Encrypting_Key* enc_key = dynamic_cast<PK_Encrypting_Key*>(key.get()); + if(enc_key == 0) + throw Internal_Error("CMS_Encoder::encrypt: " + algo + + " can't encrypt"); + + encrypt_ktri(rng, to, enc_key, cipher); + } + else if(algo == "DH") + { + if(constraints != NO_CONSTRAINTS && !(constraints & KEY_AGREEMENT)) + throw Invalid_Argument("CMS: Constraints not set for key agreement"); + + encrypt_kari(rng, to, key.get(), cipher); + } + else + throw Invalid_Argument("Unknown CMS PK encryption algorithm " + algo); + } + +/* +* Encrypt a message with a key transport algo +*/ +void CMS_Encoder::encrypt_ktri(RandomNumberGenerator& rng, + const X509_Certificate& to, + PK_Encrypting_Key* pub_key, + const std::string& cipher) + { + const std::string padding = "EME-PKCS1-v1_5"; + const std::string pk_algo = pub_key->algo_name(); + std::auto_ptr<PK_Encryptor> enc(get_pk_encryptor(*pub_key, padding)); + + SymmetricKey cek = setup_key(rng, cipher); + + AlgorithmIdentifier alg_id(OIDS::lookup(pk_algo + '/' + padding), + AlgorithmIdentifier::USE_NULL_PARAM); + + DER_Encoder encoder; + + encoder.start_cons(SEQUENCE) + .encode((u32bit)0) + .start_cons(SET) + .start_cons(SEQUENCE) + .encode((u32bit)0); + encode_si(encoder, to) + .encode(alg_id) + .encode(enc->encrypt(cek.bits_of(), rng), OCTET_STRING) + .end_cons() + .end_cons() + .raw_bytes(do_encrypt(rng, cek, cipher)) + .end_cons(); + + add_layer("CMS.EnvelopedData", encoder); + } + +/* +* Encrypt a message with a key agreement algo +*/ +void CMS_Encoder::encrypt_kari(RandomNumberGenerator&, + const X509_Certificate&, + X509_PublicKey*, + const std::string&) + { + throw Exception("FIXME: unimplemented"); + +#if 0 + SymmetricKey cek = setup_key(rng, cipher); + + DER_Encoder encoder; + encoder.start_cons(SEQUENCE); + encoder.encode(2); + encoder.start_cons(SET); + encoder.start_sequence(ASN1_Tag(1)); + encoder.encode(3); + encode_si(encoder, to); + encoder.encode(AlgorithmIdentifier(pk_algo + "/" + padding)); + encoder.encode(encrypted_cek, OCTET_STRING); + encoder.end_cons(); + encoder.end_cons(); + encoder.raw_bytes(do_encrypt(rng, cek, cipher)); + encoder.end_cons(); + + add_layer("CMS.EnvelopedData", encoder); +#endif + } + +/* +* Encrypt a message with a shared key +*/ +void CMS_Encoder::encrypt(RandomNumberGenerator& rng, + const SymmetricKey& kek, + const std::string& user_cipher) + { + throw Exception("FIXME: untested"); + + const std::string cipher = choose_algo(user_cipher, "TripleDES"); + SymmetricKey cek = setup_key(rng, cipher); + + SecureVector<byte> kek_id; // FIXME: ? + + DER_Encoder encoder; + + encoder.start_cons(SEQUENCE) + .encode((u32bit)2) + .start_explicit(ASN1_Tag(2)) + .encode((u32bit)4) + .start_cons(SEQUENCE) + .encode(kek_id, OCTET_STRING) + .end_cons() + .encode(AlgorithmIdentifier(OIDS::lookup("KeyWrap." + cipher), + AlgorithmIdentifier::USE_NULL_PARAM)) + .encode(wrap_key(rng, cipher, cek, kek), OCTET_STRING) + .end_cons() + .raw_bytes(do_encrypt(rng, cek, cipher)) + .end_cons(); + + add_layer("CMS.EnvelopedData", encoder); + } + +/* +* Encrypt a message with a passphrase +*/ +void CMS_Encoder::encrypt(RandomNumberGenerator&, + const std::string&, + const std::string& user_cipher) + { + const std::string cipher = choose_algo(user_cipher, "TripleDES"); + throw Exception("FIXME: unimplemented"); + /* + SymmetricKey cek = setup_key(key); + + DER_Encoder encoder; + encoder.start_cons(SEQUENCE); + encoder.encode(0); + encoder.raw_bytes(do_encrypt(rng, cek, cipher)); + encoder.end_cons(); + + add_layer("CMS.EnvelopedData", encoder); + */ + } + +/* +* Encrypt the content with the chosen key/cipher +*/ +SecureVector<byte> CMS_Encoder::do_encrypt(RandomNumberGenerator& rng, + const SymmetricKey& key, + const std::string& cipher_name) + { + Algorithm_Factory& af = global_state().algorithm_factory(); + + const BlockCipher* cipher = af.prototype_block_cipher(cipher_name); + + if(!cipher) + throw Invalid_Argument("CMS: Can't encrypt with non-existent cipher " + cipher_name); + + if(!OIDS::have_oid(cipher->name() + "/CBC")) + throw Encoding_Error("CMS: No OID assigned for " + cipher_name + "/CBC"); + + InitializationVector iv(rng, cipher->BLOCK_SIZE); + + AlgorithmIdentifier content_cipher; + content_cipher.oid = OIDS::lookup(cipher->name() + "/CBC"); + content_cipher.parameters = encode_params(cipher->name(), key, iv); + + Pipe pipe(new CBC_Encryption(cipher->clone(), new PKCS7_Padding, key, iv)); + + pipe.process_msg(data); + + DER_Encoder encoder; + encoder.start_cons(SEQUENCE); + encoder.encode(OIDS::lookup(type)); + encoder.encode(content_cipher); + encoder.encode(pipe.read_all(), OCTET_STRING, ASN1_Tag(0)); + encoder.end_cons(); + + return encoder.get_contents(); + } + +/* +* Sign a message +*/ +void CMS_Encoder::sign(const X509_Certificate& cert, + const PKCS8_PrivateKey& key, + RandomNumberGenerator& rng, + const std::vector<X509_Certificate>& chain, + const std::string& hash, + const std::string& pad_algo) + { + std::string padding = pad_algo + "(" + hash + ")"; + + // FIXME: Add new get_format() func to PK_Signing_Key, PK_Verifying_*_Key + Signature_Format format = IEEE_1363; + + const PK_Signing_Key& sig_key = dynamic_cast<const PK_Signing_Key&>(key); + std::auto_ptr<PK_Signer> signer(get_pk_signer(sig_key, padding, format)); + + AlgorithmIdentifier sig_algo(OIDS::lookup(key.algo_name() + "/" + padding), + AlgorithmIdentifier::USE_NULL_PARAM); + + SecureVector<byte> signed_attr = encode_attr(data, type, hash); + signer->update(signed_attr); + SecureVector<byte> signature = signer->signature(rng); + signed_attr[0] = 0xA0; + + const u32bit SI_VERSION = cert.subject_key_id().size() ? 3 : 1; + const u32bit CMS_VERSION = (type != "CMS.DataContent") ? 3 : SI_VERSION; + + DER_Encoder encoder; + + encoder.start_cons(SEQUENCE) + .encode(CMS_VERSION) + .start_cons(SET) + .encode(AlgorithmIdentifier(OIDS::lookup(hash), + AlgorithmIdentifier::USE_NULL_PARAM)) + .end_cons() + .raw_bytes(make_econtent(data, type)); + + encoder.start_cons(ASN1_Tag(0), CONTEXT_SPECIFIC); + for(u32bit j = 0; j != chain.size(); j++) + encoder.raw_bytes(chain[j].BER_encode()); + encoder.raw_bytes(cert.BER_encode()).end_cons(); + + encoder.start_cons(SET) + .start_cons(SEQUENCE) + .encode(SI_VERSION); + encode_si(encoder, cert, ((SI_VERSION == 3) ? true : false)) + .encode( + AlgorithmIdentifier(OIDS::lookup(hash), + AlgorithmIdentifier::USE_NULL_PARAM) + ) + .raw_bytes(signed_attr) + .encode(sig_algo) + .encode(signature, OCTET_STRING) + .end_cons() + .end_cons() + .end_cons(); + + add_layer("CMS.SignedData", encoder); + } + +/* +* Digest a message +*/ +void CMS_Encoder::digest(const std::string& user_hash) + { + const std::string hash = choose_algo(user_hash, "SHA-1"); + if(!OIDS::have_oid(hash)) + throw Encoding_Error("CMS: No OID assigned for " + hash); + + const u32bit VERSION = (type != "CMS.DataContent") ? 2 : 0; + + DER_Encoder encoder; + encoder.start_cons(SEQUENCE) + .encode(VERSION) + .encode(AlgorithmIdentifier(OIDS::lookup(hash), + AlgorithmIdentifier::USE_NULL_PARAM)) + .raw_bytes(make_econtent(data, type)) + .encode(hash_of(data, hash), OCTET_STRING) + .end_cons(); + + add_layer("CMS.DigestedData", encoder); + } + +/* +* MAC a message with an encrypted key +*/ +void CMS_Encoder::authenticate(const X509_Certificate&, + const std::string& mac_algo) + { + const std::string mac = choose_algo(mac_algo, "HMAC(SHA-1)"); + throw Exception("FIXME: unimplemented"); + } + +/* +* MAC a message with a shared key +*/ +void CMS_Encoder::authenticate(const SymmetricKey&, + const std::string& mac_algo) + { + const std::string mac = choose_algo(mac_algo, "HMAC(SHA-1)"); + throw Exception("FIXME: unimplemented"); + } + +/* +* MAC a message with a passphrase +*/ +void CMS_Encoder::authenticate(const std::string&, + const std::string& mac_algo) + { + const std::string mac = choose_algo(mac_algo, "HMAC(SHA-1)"); + throw Exception("FIXME: unimplemented"); + } + +} diff --git a/botan/src/cms/cms_enc.cpp b/botan/src/cms/cms_enc.cpp new file mode 100644 index 0000000..2413676 --- /dev/null +++ b/botan/src/cms/cms_enc.cpp @@ -0,0 +1,86 @@ +/* +* CMS Encoding Base +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/cms_enc.h> +#include <botan/der_enc.h> +#include <botan/oids.h> +#include <botan/pem.h> + +namespace Botan { + +/* +* Setup the intitial layer of CMS data +*/ +void CMS_Encoder::set_data(const byte buf[], u32bit length) + { + if(data.has_items()) + throw Invalid_State("Cannot call CMS_Encoder::set_data here"); + + data.set(buf, length); + type = "CMS.DataContent"; + } + +/* +* Setup the intitial layer of CMS data +*/ +void CMS_Encoder::set_data(const std::string& str) + { + set_data((const byte*)str.c_str(), str.length()); + } + +/* +* Finalize and return the CMS encoded data +*/ +SecureVector<byte> CMS_Encoder::get_contents() + { + DER_Encoder encoder; + + encoder.start_cons(SEQUENCE). + encode(OIDS::lookup(type)). + start_explicit(0). + raw_bytes(data). + end_explicit(). + end_cons(); + + data.clear(); + + return encoder.get_contents(); + } + +/* +* Add a new layer of encapsulation +*/ +void CMS_Encoder::add_layer(const std::string& oid, DER_Encoder& new_layer) + { + data = new_layer.get_contents(); + type = oid; + } + +/* +* Return the PEM-encoded data +*/ +std::string CMS_Encoder::PEM_contents() + { + return PEM_Code::encode(get_contents(), "PKCS7"); + } + +/* +* Make an EncapsulatedContentInfo +*/ +SecureVector<byte> CMS_Encoder::make_econtent(const SecureVector<byte>& data, + const std::string& type) + { + return DER_Encoder().start_cons(SEQUENCE). + encode(OIDS::lookup(type)). + start_explicit(0). + encode(data, OCTET_STRING). + end_explicit(). + end_cons(). + get_contents(); + } + +} diff --git a/botan/src/cms/cms_enc.h b/botan/src/cms/cms_enc.h new file mode 100644 index 0000000..6fdd2b7 --- /dev/null +++ b/botan/src/cms/cms_enc.h @@ -0,0 +1,92 @@ +/* +* CMS Encoding +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_CMS_ENCODER_H__ +#define BOTAN_CMS_ENCODER_H__ + +#include <botan/x509cert.h> +#include <botan/x509stor.h> +#include <botan/pkcs8.h> +#include <botan/symkey.h> + +namespace Botan { + +/* +* CMS Encoding Operation +*/ +class BOTAN_DLL CMS_Encoder + { + public: + + void encrypt(RandomNumberGenerator&, + const X509_Certificate&, const std::string = ""); + + void encrypt(RandomNumberGenerator& rng, + const std::string&, const std::string& = ""); + + void encrypt(RandomNumberGenerator& rng, + const SymmetricKey&, const std::string& = ""); + + void authenticate(const X509_Certificate&, const std::string& = ""); + void authenticate(const std::string&, const std::string& = ""); + void authenticate(const SymmetricKey&, const std::string& = ""); + + void sign(const X509_Certificate& cert, + const PKCS8_PrivateKey& key, + RandomNumberGenerator& rng, + const std::vector<X509_Certificate>& cert_chain, + const std::string& hash, + const std::string& padding); + + void digest(const std::string& = ""); + + void compress(const std::string&); + static bool can_compress_with(const std::string&); + + SecureVector<byte> get_contents(); + std::string PEM_contents(); + + void set_data(const std::string&); + void set_data(const byte[], u32bit); + + CMS_Encoder(const std::string& str) { set_data(str); } + CMS_Encoder(const byte buf[], u32bit length) { set_data(buf, length); } + private: + void add_layer(const std::string&, DER_Encoder&); + + void encrypt_ktri(RandomNumberGenerator&, + const X509_Certificate&, PK_Encrypting_Key*, + const std::string&); + void encrypt_kari(RandomNumberGenerator&, + const X509_Certificate&, X509_PublicKey*, + const std::string&); + + SecureVector<byte> do_encrypt(RandomNumberGenerator& rng, + const SymmetricKey&, const std::string&); + + static SecureVector<byte> make_econtent(const SecureVector<byte>&, + const std::string&); + + static SymmetricKey setup_key(RandomNumberGenerator& rng, + const std::string&); + + static SecureVector<byte> wrap_key(RandomNumberGenerator& rng, + const std::string&, + const SymmetricKey&, + const SymmetricKey&); + + static SecureVector<byte> encode_params(const std::string&, + const SymmetricKey&, + const InitializationVector&); + + SecureVector<byte> data; + std::string type; + }; + +} + +#endif diff --git a/botan/src/cms/info.txt b/botan/src/cms/info.txt new file mode 100644 index 0000000..82c31b5 --- /dev/null +++ b/botan/src/cms/info.txt @@ -0,0 +1,31 @@ +realname "CMS" + +define CMS + +load_on auto + +<add> +cms_algo.cpp +cms_comp.cpp +cms_dalg.cpp +cms_dec.cpp +cms_dec.h +cms_ealg.cpp +cms_enc.cpp +cms_enc.h +</add> + +<requires> +asn1 +bigint +cbc +filters +hash +libstate +oid_lookup +pem +pk_codecs +sha1 +sym_algo +x509 +</requires> diff --git a/botan/src/codec/base64/b64_char.cpp b/botan/src/codec/base64/b64_char.cpp new file mode 100644 index 0000000..e5722a0 --- /dev/null +++ b/botan/src/codec/base64/b64_char.cpp @@ -0,0 +1,47 @@ +/* +* Base64 Codec Character Tables +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/base64.h> + +namespace Botan { + +/* +* Base64 Encoder Lookup Table +*/ +const byte Base64_Encoder::BIN_TO_BASE64[64] = { +0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, +0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, +0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, +0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, +0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2B, 0x2F }; + +/* +* Base64 Decoder Lookup Table +*/ +const byte Base64_Decoder::BASE64_TO_BIN[256] = { +0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, +0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, +0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, +0x80, 0x80, 0x80, 0x80, 0x3E, 0x80, 0x80, 0x80, 0x3F, 0x34, 0x35, 0x36, 0x37, +0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, +0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, +0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, +0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, +0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, +0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, +0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, +0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, +0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, +0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, +0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, +0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, +0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, +0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, +0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, +0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 }; + +} diff --git a/botan/src/codec/base64/base64.cpp b/botan/src/codec/base64/base64.cpp new file mode 100644 index 0000000..dfcc1ca --- /dev/null +++ b/botan/src/codec/base64/base64.cpp @@ -0,0 +1,228 @@ +/* +* Base64 Encoder/Decoder +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/base64.h> +#include <botan/charset.h> +#include <botan/exceptn.h> +#include <algorithm> + +namespace Botan { + +/* +* Base64_Encoder Constructor +*/ +Base64_Encoder::Base64_Encoder(bool breaks, u32bit length, bool t_n) : + line_length(breaks ? length : 0), trailing_newline(t_n) + { + in.create(48); + out.create(4); + + counter = position = 0; + } + +/* +* Base64 Encoding Operation +*/ +void Base64_Encoder::encode(const byte in[3], byte out[4]) + { + out[0] = BIN_TO_BASE64[((in[0] & 0xFC) >> 2)]; + out[1] = BIN_TO_BASE64[((in[0] & 0x03) << 4) | (in[1] >> 4)]; + out[2] = BIN_TO_BASE64[((in[1] & 0x0F) << 2) | (in[2] >> 6)]; + out[3] = BIN_TO_BASE64[((in[2] & 0x3F) )]; + } + +/* +* Encode and send a block +*/ +void Base64_Encoder::encode_and_send(const byte block[], u32bit length) + { + for(u32bit j = 0; j != length; j += 3) + { + encode(block + j, out); + do_output(out, 4); + } + } + +/* +* Handle the output +*/ +void Base64_Encoder::do_output(const byte input[], u32bit length) + { + if(line_length == 0) + send(input, length); + else + { + u32bit remaining = length, offset = 0; + while(remaining) + { + u32bit sent = std::min(line_length - counter, remaining); + send(input + offset, sent); + counter += sent; + remaining -= sent; + offset += sent; + if(counter == line_length) + { + send('\n'); + counter = 0; + } + } + } + } + +/* +* Convert some data into Base64 +*/ +void Base64_Encoder::write(const byte input[], u32bit length) + { + in.copy(position, input, length); + if(position + length >= in.size()) + { + encode_and_send(in, in.size()); + input += (in.size() - position); + length -= (in.size() - position); + while(length >= in.size()) + { + encode_and_send(input, in.size()); + input += in.size(); + length -= in.size(); + } + in.copy(input, length); + position = 0; + } + position += length; + } + +/* +* Flush buffers +*/ +void Base64_Encoder::end_msg() + { + u32bit start_of_last_block = 3 * (position / 3), + left_over = position % 3; + encode_and_send(in, start_of_last_block); + + if(left_over) + { + SecureBuffer<byte, 3> remainder(in + start_of_last_block, left_over); + + encode(remainder, out); + + u32bit empty_bits = 8 * (3 - left_over), index = 4 - 1; + while(empty_bits >= 8) + { + out[index--] = '='; + empty_bits -= 6; + } + + do_output(out, 4); + } + + if(trailing_newline || (counter && line_length)) + send('\n'); + + counter = position = 0; + } + +/* +* Base64_Decoder Constructor +*/ +Base64_Decoder::Base64_Decoder(Decoder_Checking c) : checking(c) + { + in.create(48); + out.create(3); + position = 0; + } + +/* +* Check if a character is a valid Base64 char +*/ +bool Base64_Decoder::is_valid(byte in) + { + return (BASE64_TO_BIN[in] != 0x80); + } + +/* +* Base64 Decoding Operation +*/ +void Base64_Decoder::decode(const byte in[4], byte out[3]) + { + out[0] = ((BASE64_TO_BIN[in[0]] << 2) | (BASE64_TO_BIN[in[1]] >> 4)); + out[1] = ((BASE64_TO_BIN[in[1]] << 4) | (BASE64_TO_BIN[in[2]] >> 2)); + out[2] = ((BASE64_TO_BIN[in[2]] << 6) | (BASE64_TO_BIN[in[3]])); + } + +/* +* Decode and send a block +*/ +void Base64_Decoder::decode_and_send(const byte block[], u32bit length) + { + for(u32bit j = 0; j != length; j += 4) + { + decode(block + j, out); + send(out, 3); + } + } + +/* +* Handle processing an invalid character +*/ +void Base64_Decoder::handle_bad_char(byte c) + { + if(c == '=' || checking == NONE) + return; + + if((checking == IGNORE_WS) && Charset::is_space(c)) + return; + + throw Decoding_Error( + std::string("Base64_Decoder: Invalid base64 character '") + + static_cast<char>(c) + "'" + ); + } + +/* +* Convert some data from Base64 +*/ +void Base64_Decoder::write(const byte input[], u32bit length) + { + for(u32bit j = 0; j != length; ++j) + { + if(is_valid(input[j])) + in[position++] = input[j]; + else + handle_bad_char(input[j]); + + if(position == in.size()) + { + decode_and_send(in, in.size()); + position = 0; + } + } + } + +/* +* Flush buffers +*/ +void Base64_Decoder::end_msg() + { + if(position != 0) + { + u32bit start_of_last_block = 4 * (position / 4), + left_over = position % 4; + decode_and_send(in, start_of_last_block); + + if(left_over) + { + SecureBuffer<byte, 4> remainder(in + start_of_last_block, left_over); + decode(remainder, out); + send(out, ((left_over == 1) ? (1) : (left_over - 1))); + } + } + position = 0; + } + +} diff --git a/botan/src/codec/base64/base64.h b/botan/src/codec/base64/base64.h new file mode 100644 index 0000000..aca02da --- /dev/null +++ b/botan/src/codec/base64/base64.h @@ -0,0 +1,94 @@ +/* +* Base64 Encoder/Decoder +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_BASE64_H__ +#define BOTAN_BASE64_H__ + +#include <botan/filter.h> + +namespace Botan { + +/** +* This class represents a Base64 encoder. +*/ +class BOTAN_DLL Base64_Encoder : public Filter + { + public: + static void encode(const byte in[3], byte out[4]); + + /** + * Input a part of a message to the encoder. + * @param input the message to input as a byte array + * @param length the length of the byte array input + */ + void write(const byte input[], u32bit length); + + /** + * Inform the Encoder that the current message shall be closed. + */ + void end_msg(); + + /** + * Create a base64 encoder. + * @param breaks whether to use line breaks in the Streamcipheroutput + * @param length the length of the lines of the output + * @param t_n whether to use a trailing newline + */ + Base64_Encoder(bool breaks = false, u32bit length = 72, + bool t_n = false); + private: + void encode_and_send(const byte[], u32bit); + void do_output(const byte[], u32bit); + static const byte BIN_TO_BASE64[64]; + + const u32bit line_length; + const bool trailing_newline; + SecureVector<byte> in, out; + u32bit position, counter; + }; + +/** +* This object represents a Base64 decoder. +*/ +class BOTAN_DLL Base64_Decoder : public Filter + { + public: + static void decode(const byte input[4], byte output[3]); + + static bool is_valid(byte); + + /** + * Input a part of a message to the decoder. + * @param input the message to input as a byte array + * @param length the length of the byte array input + */ + void write(const byte input[], u32bit length); + + /** + * Inform the Encoder that the current message shall be closed. + */ + void end_msg(); + + /** + * Create a base64 encoder. + * @param checking the type of checking that shall be performed by + * the decoder + */ + Base64_Decoder(Decoder_Checking checking = NONE); + private: + void decode_and_send(const byte[], u32bit); + void handle_bad_char(byte); + static const byte BASE64_TO_BIN[256]; + + const Decoder_Checking checking; + SecureVector<byte> in, out; + u32bit position; + }; + +} + +#endif diff --git a/botan/src/codec/base64/info.txt b/botan/src/codec/base64/info.txt new file mode 100644 index 0000000..d4ed809 --- /dev/null +++ b/botan/src/codec/base64/info.txt @@ -0,0 +1,15 @@ +realname "Base64 Codec" + +define BASE64_CODEC + +load_on auto + +<add> +base64.cpp +b64_char.cpp +base64.h +</add> + +<requires> +filters +</requires> diff --git a/botan/src/codec/bzip2/bzip2.cpp b/botan/src/codec/bzip2/bzip2.cpp new file mode 100644 index 0000000..4cdca53 --- /dev/null +++ b/botan/src/codec/bzip2/bzip2.cpp @@ -0,0 +1,275 @@ +/* +* Bzip Compressor +* (C) 2001 Peter J Jones +* 2001-2007 Jack Lloyd +* 2006 Matt Johnston +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/bzip2.h> +#include <botan/exceptn.h> + +#include <map> +#include <cstring> +#define BZ_NO_STDIO +#include <bzlib.h> + +namespace Botan { + +namespace { + +/* +* Allocation Information for Bzip +*/ +class Bzip_Alloc_Info + { + public: + std::map<void*, u32bit> current_allocs; + Allocator* alloc; + + Bzip_Alloc_Info() { alloc = Allocator::get(false); } + }; + +/* +* Allocation Function for Bzip +*/ +void* bzip_malloc(void* info_ptr, int n, int size) + { + Bzip_Alloc_Info* info = static_cast<Bzip_Alloc_Info*>(info_ptr); + void* ptr = info->alloc->allocate(n * size); + info->current_allocs[ptr] = n * size; + return ptr; + } + +/* +* Allocation Function for Bzip +*/ +void bzip_free(void* info_ptr, void* ptr) + { + Bzip_Alloc_Info* info = static_cast<Bzip_Alloc_Info*>(info_ptr); + std::map<void*, u32bit>::const_iterator i = info->current_allocs.find(ptr); + if(i == info->current_allocs.end()) + throw Invalid_Argument("bzip_free: Got pointer not allocated by us"); + info->alloc->deallocate(ptr, i->second); + } + +} + +/* +* Wrapper Type for Bzip2 Stream +*/ +class Bzip_Stream + { + public: + bz_stream stream; + + Bzip_Stream() + { + std::memset(&stream, 0, sizeof(bz_stream)); + stream.bzalloc = bzip_malloc; + stream.bzfree = bzip_free; + stream.opaque = new Bzip_Alloc_Info; + } + ~Bzip_Stream() + { + Bzip_Alloc_Info* info = static_cast<Bzip_Alloc_Info*>(stream.opaque); + delete info; + std::memset(&stream, 0, sizeof(bz_stream)); + } + }; + +/* +* Bzip_Compression Constructor +*/ +Bzip_Compression::Bzip_Compression(u32bit l) : + level((l >= 9) ? 9 : l), buffer(DEFAULT_BUFFERSIZE) + { + bz = 0; + } + +/* +* Start Compressing with Bzip +*/ +void Bzip_Compression::start_msg() + { + clear(); + bz = new Bzip_Stream; + if(BZ2_bzCompressInit(&(bz->stream), level, 0, 0) != BZ_OK) + throw Exception("Bzip_Compression: Memory allocation error"); + } + +/* +* Compress Input with Bzip +*/ +void Bzip_Compression::write(const byte input[], u32bit length) + { + bz->stream.next_in = reinterpret_cast<char*>(const_cast<byte*>(input)); + bz->stream.avail_in = length; + + while(bz->stream.avail_in != 0) + { + bz->stream.next_out = reinterpret_cast<char*>(buffer.begin()); + bz->stream.avail_out = buffer.size(); + BZ2_bzCompress(&(bz->stream), BZ_RUN); + send(buffer, buffer.size() - bz->stream.avail_out); + } + } + +/* +* Finish Compressing with Bzip +*/ +void Bzip_Compression::end_msg() + { + bz->stream.next_in = 0; + bz->stream.avail_in = 0; + + int rc = BZ_OK; + while(rc != BZ_STREAM_END) + { + bz->stream.next_out = reinterpret_cast<char*>(buffer.begin()); + bz->stream.avail_out = buffer.size(); + rc = BZ2_bzCompress(&(bz->stream), BZ_FINISH); + send(buffer, buffer.size() - bz->stream.avail_out); + } + clear(); + } + +/* +* Flush the Bzip Compressor +*/ +void Bzip_Compression::flush() + { + bz->stream.next_in = 0; + bz->stream.avail_in = 0; + + int rc = BZ_OK; + while(rc != BZ_RUN_OK) + { + bz->stream.next_out = reinterpret_cast<char*>(buffer.begin()); + bz->stream.avail_out = buffer.size(); + rc = BZ2_bzCompress(&(bz->stream), BZ_FLUSH); + send(buffer, buffer.size() - bz->stream.avail_out); + } + } + +/* +* Clean up Compression Context +*/ +void Bzip_Compression::clear() + { + if(!bz) return; + BZ2_bzCompressEnd(&(bz->stream)); + delete bz; + bz = 0; + } + +/* +* Bzip_Decompression Constructor +*/ +Bzip_Decompression::Bzip_Decompression(bool s) : + small_mem(s), buffer(DEFAULT_BUFFERSIZE) + { + no_writes = true; + bz = 0; + } + +/* +* Decompress Input with Bzip +*/ +void Bzip_Decompression::write(const byte input_arr[], u32bit length) + { + if(length) no_writes = false; + + char* input = reinterpret_cast<char*>(const_cast<byte*>(input_arr)); + + bz->stream.next_in = input; + bz->stream.avail_in = length; + + while(bz->stream.avail_in != 0) + { + bz->stream.next_out = reinterpret_cast<char*>(buffer.begin()); + bz->stream.avail_out = buffer.size(); + + int rc = BZ2_bzDecompress(&(bz->stream)); + + if(rc != BZ_OK && rc != BZ_STREAM_END) + { + clear(); + if(rc == BZ_DATA_ERROR) + throw Decoding_Error("Bzip_Decompression: Data integrity error"); + if(rc == BZ_DATA_ERROR_MAGIC) + throw Decoding_Error("Bzip_Decompression: Invalid input"); + if(rc == BZ_MEM_ERROR) + throw Exception("Bzip_Decompression: Memory allocation error"); + throw Exception("Bzip_Decompression: Unknown decompress error"); + } + + send(buffer, buffer.size() - bz->stream.avail_out); + + if(rc == BZ_STREAM_END) + { + u32bit read_from_block = length - bz->stream.avail_in; + start_msg(); + bz->stream.next_in = input + read_from_block; + bz->stream.avail_in = length - read_from_block; + input += read_from_block; + length -= read_from_block; + } + } + } + +/* +* Start Decompressing with Bzip +*/ +void Bzip_Decompression::start_msg() + { + clear(); + bz = new Bzip_Stream; + + if(BZ2_bzDecompressInit(&(bz->stream), 0, small_mem) != BZ_OK) + throw Exception("Bzip_Decompression: Memory allocation error"); + + no_writes = true; + } + +/* +* Finish Decompressing with Bzip +*/ +void Bzip_Decompression::end_msg() + { + if(no_writes) return; + bz->stream.next_in = 0; + bz->stream.avail_in = 0; + + int rc = BZ_OK; + while(rc != BZ_STREAM_END) + { + bz->stream.next_out = reinterpret_cast<char*>(buffer.begin()); + bz->stream.avail_out = buffer.size(); + rc = BZ2_bzDecompress(&(bz->stream)); + + if(rc != BZ_OK && rc != BZ_STREAM_END) + { + clear(); + throw Exception("Bzip_Decompression: Error finalizing decompression"); + } + + send(buffer, buffer.size() - bz->stream.avail_out); + } + + clear(); + } + +/* +* Clean up Decompression Context +*/ +void Bzip_Decompression::clear() + { + if(!bz) return; + BZ2_bzDecompressEnd(&(bz->stream)); + delete bz; + bz = 0; + } + +} diff --git a/botan/src/codec/bzip2/bzip2.h b/botan/src/codec/bzip2/bzip2.h new file mode 100644 index 0000000..f422635 --- /dev/null +++ b/botan/src/codec/bzip2/bzip2.h @@ -0,0 +1,61 @@ +/* +* Bzip Compressor +* (C) 2001 Peter J Jones +* 2001-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_BZIP2_H__ +#define BOTAN_BZIP2_H__ + +#include <botan/filter.h> + +namespace Botan { + +/* +* Bzip Compression Filter +*/ +class BOTAN_DLL Bzip_Compression : public Filter + { + public: + void write(const byte input[], u32bit length); + void start_msg(); + void end_msg(); + + void flush(); + + Bzip_Compression(u32bit = 9); + ~Bzip_Compression() { clear(); } + private: + void clear(); + + const u32bit level; + SecureVector<byte> buffer; + class Bzip_Stream* bz; + }; + +/* +* Bzip Decompression Filter +*/ +class BOTAN_DLL Bzip_Decompression : public Filter + { + public: + void write(const byte input[], u32bit length); + void start_msg(); + void end_msg(); + + Bzip_Decompression(bool = false); + ~Bzip_Decompression() { clear(); } + private: + void clear(); + + const bool small_mem; + SecureVector<byte> buffer; + class Bzip_Stream* bz; + bool no_writes; + }; + +} + +#endif diff --git a/botan/src/codec/bzip2/info.txt b/botan/src/codec/bzip2/info.txt new file mode 100644 index 0000000..1be84e4 --- /dev/null +++ b/botan/src/codec/bzip2/info.txt @@ -0,0 +1,21 @@ +# This module was written by Peter J. Jones + +realname "Bzip2 Compressor" + +define COMPRESSOR_BZIP2 +modset compression + +load_on request + +<add> +bzip2.h +bzip2.cpp +</add> + +<libs> +all -> bz2 +</libs> + +<requires> +filters +</requires> diff --git a/botan/src/codec/hex/hex.cpp b/botan/src/codec/hex/hex.cpp new file mode 100644 index 0000000..fbacc27 --- /dev/null +++ b/botan/src/codec/hex/hex.cpp @@ -0,0 +1,191 @@ +/* +* Hex Encoder/Decoder +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/hex.h> +#include <botan/parsing.h> +#include <botan/charset.h> +#include <botan/exceptn.h> +#include <algorithm> + +namespace Botan { + +/* +* Hex_Encoder Constructor +*/ +Hex_Encoder::Hex_Encoder(bool breaks, u32bit length, Case c) : + casing(c), line_length(breaks ? length : 0) + { + in.create(64); + out.create(2*in.size()); + counter = position = 0; + } + +/* +* Hex_Encoder Constructor +*/ +Hex_Encoder::Hex_Encoder(Case c) : casing(c), line_length(0) + { + in.create(64); + out.create(2*in.size()); + counter = position = 0; + } + +/* +* Hex Encoding Operation +*/ +void Hex_Encoder::encode(byte in, byte out[2], Hex_Encoder::Case casing) + { + const byte* BIN_TO_HEX = ((casing == Uppercase) ? BIN_TO_HEX_UPPER : + BIN_TO_HEX_LOWER); + + out[0] = BIN_TO_HEX[((in >> 4) & 0x0F)]; + out[1] = BIN_TO_HEX[((in ) & 0x0F)]; + } + +/* +* Encode and send a block +*/ +void Hex_Encoder::encode_and_send(const byte block[], u32bit length) + { + for(u32bit j = 0; j != length; ++j) + encode(block[j], out + 2*j, casing); + + if(line_length == 0) + send(out, 2*length); + else + { + u32bit remaining = 2*length, offset = 0; + while(remaining) + { + u32bit sent = std::min(line_length - counter, remaining); + send(out + offset, sent); + counter += sent; + remaining -= sent; + offset += sent; + if(counter == line_length) + { + send('\n'); + counter = 0; + } + } + } + } + +/* +* Convert some data into hex format +*/ +void Hex_Encoder::write(const byte input[], u32bit length) + { + in.copy(position, input, length); + if(position + length >= in.size()) + { + encode_and_send(in, in.size()); + input += (in.size() - position); + length -= (in.size() - position); + while(length >= in.size()) + { + encode_and_send(input, in.size()); + input += in.size(); + length -= in.size(); + } + in.copy(input, length); + position = 0; + } + position += length; + } + +/* +* Flush buffers +*/ +void Hex_Encoder::end_msg() + { + encode_and_send(in, position); + if(counter && line_length) + send('\n'); + counter = position = 0; + } + +/* +* Hex_Decoder Constructor +*/ +Hex_Decoder::Hex_Decoder(Decoder_Checking c) : checking(c) + { + in.create(64); + out.create(in.size() / 2); + position = 0; + } + +/* +* Check if a character is a valid hex char +*/ +bool Hex_Decoder::is_valid(byte in) + { + return (HEX_TO_BIN[in] != 0x80); + } + +/* +* Handle processing an invalid character +*/ +void Hex_Decoder::handle_bad_char(byte c) + { + if(checking == NONE) + return; + + if((checking == IGNORE_WS) && Charset::is_space(c)) + return; + + throw Decoding_Error("Hex_Decoder: Invalid hex character: " + + to_string(c)); + } + +/* +* Hex Decoding Operation +*/ +byte Hex_Decoder::decode(const byte hex[2]) + { + return ((HEX_TO_BIN[hex[0]] << 4) | HEX_TO_BIN[hex[1]]); + } + +/* +* Decode and send a block +*/ +void Hex_Decoder::decode_and_send(const byte block[], u32bit length) + { + for(u32bit j = 0; j != length / 2; ++j) + out[j] = decode(block + 2*j); + send(out, length / 2); + } + +/* +* Convert some data from hex format +*/ +void Hex_Decoder::write(const byte input[], u32bit length) + { + for(u32bit j = 0; j != length; ++j) + { + if(is_valid(input[j])) + in[position++] = input[j]; + else + handle_bad_char(input[j]); + if(position == in.size()) + { + decode_and_send(in, in.size()); + position = 0; + } + } + } + +/* +* Flush buffers +*/ +void Hex_Decoder::end_msg() + { + decode_and_send(in, position); + position = 0; + } + +} diff --git a/botan/src/codec/hex/hex.h b/botan/src/codec/hex/hex.h new file mode 100644 index 0000000..035bf4e --- /dev/null +++ b/botan/src/codec/hex/hex.h @@ -0,0 +1,90 @@ +/* +* Hex Encoder/Decoder +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_HEX_H__ +#define BOTAN_HEX_H__ + +#include <botan/filter.h> + +namespace Botan { + +/** +* This class represents a hex encoder. It encodes byte arrays to hex strings. +*/ +class BOTAN_DLL Hex_Encoder : public Filter + { + public: + /** + * Whether to use uppercase or lowercase letters for the encoded string. + */ + enum Case { Uppercase, Lowercase }; + + /** + Encode a single byte into two hex characters + */ + static void encode(byte in, byte out[2], Case the_case = Uppercase); + + void write(const byte in[], u32bit length); + void end_msg(); + + /** + * Create a hex encoder. + * @param the_case the case to use in the encoded strings. + */ + Hex_Encoder(Case the_case); + + /** + * Create a hex encoder. + * @param newlines should newlines be used + * @param line_length if newlines are used, how long are lines + * @param the_case the case to use in the encoded strings + */ + Hex_Encoder(bool newlines = false, + u32bit line_length = 72, + Case the_case = Uppercase); + private: + void encode_and_send(const byte[], u32bit); + static const byte BIN_TO_HEX_UPPER[16]; + static const byte BIN_TO_HEX_LOWER[16]; + + const Case casing; + const u32bit line_length; + SecureVector<byte> in, out; + u32bit position, counter; + }; + +/** +* This class represents a hex decoder. It converts hex strings to byte arrays. +*/ +class BOTAN_DLL Hex_Decoder : public Filter + { + public: + static byte decode(const byte[2]); + static bool is_valid(byte); + + void write(const byte[], u32bit); + void end_msg(); + + /** + * Construct a Hex Decoder using the specified + * character checking. + * @param checking the checking to use during decoding. + */ + Hex_Decoder(Decoder_Checking checking = NONE); + private: + void decode_and_send(const byte[], u32bit); + void handle_bad_char(byte); + static const byte HEX_TO_BIN[256]; + + const Decoder_Checking checking; + SecureVector<byte> in, out; + u32bit position; + }; + +} + +#endif diff --git a/botan/src/codec/hex/hex_char.cpp b/botan/src/codec/hex/hex_char.cpp new file mode 100644 index 0000000..c28efc5 --- /dev/null +++ b/botan/src/codec/hex/hex_char.cpp @@ -0,0 +1,48 @@ +/* +* Hex Character Table +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/hex.h> + +namespace Botan { + +/* +* Hex Encoder Lookup Tables +*/ +const byte Hex_Encoder::BIN_TO_HEX_UPPER[16] = { +0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, +0x44, 0x45, 0x46 }; + +const byte Hex_Encoder::BIN_TO_HEX_LOWER[16] = { +0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x61, 0x62, 0x63, +0x64, 0x65, 0x66 }; + +/* +* Hex Decoder Lookup Table +*/ +const byte Hex_Decoder::HEX_TO_BIN[256] = { +0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, +0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, +0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, +0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x01, 0x02, 0x03, +0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, +0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, +0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, +0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x80, +0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, +0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, +0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, +0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, +0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, +0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, +0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, +0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, +0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, +0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, +0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, +0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 }; + +} diff --git a/botan/src/codec/hex/info.txt b/botan/src/codec/hex/info.txt new file mode 100644 index 0000000..512a5de --- /dev/null +++ b/botan/src/codec/hex/info.txt @@ -0,0 +1,15 @@ +realname "Hex Codec" + +define HEX_CODEC + +load_on auto + +<add> +hex.cpp +hex_char.cpp +hex.h +</add> + +<requires> +filters +</requires> diff --git a/botan/src/codec/openpgp/info.txt b/botan/src/codec/openpgp/info.txt new file mode 100644 index 0000000..6b30850 --- /dev/null +++ b/botan/src/codec/openpgp/info.txt @@ -0,0 +1,15 @@ +realname "OpenPGP Codec" + +define OPENPGP_CODEC + +load_on auto + +<add> +openpgp.cpp +openpgp.h +</add> + +<requires> +crc24 +filters +</requires> diff --git a/botan/src/codec/openpgp/openpgp.cpp b/botan/src/codec/openpgp/openpgp.cpp new file mode 100644 index 0000000..7f9cf5f --- /dev/null +++ b/botan/src/codec/openpgp/openpgp.cpp @@ -0,0 +1,197 @@ +/* +* OpenPGP +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/openpgp.h> +#include <botan/filters.h> +#include <botan/charset.h> +#include <botan/crc24.h> + +namespace Botan { + +namespace OpenPGP { + +/* +* OpenPGP Base64 encoding +*/ +std::string encode(const byte input[], u32bit length, + const std::string& label, + const std::map<std::string, std::string>& headers) + { + const std::string PGP_HEADER = "-----BEGIN PGP " + label + "-----\n"; + const std::string PGP_TRAILER = "-----END PGP " + label + "-----\n"; + const u32bit PGP_WIDTH = 64; + + std::string pgp_encoded = PGP_HEADER; + + if(headers.find("Version") != headers.end()) + pgp_encoded += "Version: " + headers.find("Version")->second + '\n'; + + std::map<std::string, std::string>::const_iterator i = headers.begin(); + while(i != headers.end()) + { + if(i->first != "Version") + pgp_encoded += i->first + ": " + i->second + '\n'; + ++i; + } + pgp_encoded += '\n'; + + Pipe pipe(new Fork( + new Base64_Encoder(true, PGP_WIDTH), + new Chain(new Hash_Filter(new CRC24), new Base64_Encoder) + ) + ); + + pipe.process_msg(input, length); + + pgp_encoded += pipe.read_all_as_string(0); + pgp_encoded += '=' + pipe.read_all_as_string(1) + '\n'; + pgp_encoded += PGP_TRAILER; + + return pgp_encoded; + } + +/* +* OpenPGP Base64 encoding +*/ +std::string encode(const byte input[], u32bit length, + const std::string& type) + { + std::map<std::string, std::string> empty; + return encode(input, length, type, empty); + } + +/* +* OpenPGP Base64 decoding +*/ +SecureVector<byte> decode(DataSource& source, std::string& label, + std::map<std::string, std::string>& headers) + { + const u32bit RANDOM_CHAR_LIMIT = 5; + + const std::string PGP_HEADER1 = "-----BEGIN PGP "; + const std::string PGP_HEADER2 = "-----"; + u32bit position = 0; + + while(position != PGP_HEADER1.length()) + { + byte b; + if(!source.read_byte(b)) + throw Decoding_Error("PGP: No PGP header found"); + if(b == PGP_HEADER1[position]) + ++position; + else if(position >= RANDOM_CHAR_LIMIT) + throw Decoding_Error("PGP: Malformed PGP header"); + else + position = 0; + } + position = 0; + while(position != PGP_HEADER2.length()) + { + byte b; + if(!source.read_byte(b)) + throw Decoding_Error("PGP: No PGP header found"); + if(b == PGP_HEADER2[position]) + ++position; + else if(position) + throw Decoding_Error("PGP: Malformed PGP header"); + + if(position == 0) + label += static_cast<char>(b); + } + + headers.clear(); + bool end_of_headers = false; + while(!end_of_headers) + { + std::string this_header; + byte b = 0; + while(b != '\n') + { + if(!source.read_byte(b)) + throw Decoding_Error("PGP: Bad armor header"); + if(b != '\n') + this_header += static_cast<char>(b); + } + + end_of_headers = true; + for(u32bit j = 0; j != this_header.length(); ++j) + if(!Charset::is_space(this_header[j])) + end_of_headers = false; + + if(!end_of_headers) + { + std::string::size_type pos = this_header.find(": "); + if(pos == std::string::npos) + throw Decoding_Error("OpenPGP: Bad headers"); + + std::string key = this_header.substr(0, pos); + std::string value = this_header.substr(pos + 2, std::string::npos); + headers[key] = value; + } + } + + Pipe base64(new Base64_Decoder, + new Fork(0, + new Chain(new Hash_Filter(new CRC24), + new Base64_Encoder) + ) + ); + base64.start_msg(); + + const std::string PGP_TRAILER = "-----END PGP " + label + "-----"; + position = 0; + bool newline_seen = 0; + std::string crc; + while(position != PGP_TRAILER.length()) + { + byte b; + if(!source.read_byte(b)) + throw Decoding_Error("PGP: No PGP trailer found"); + if(b == PGP_TRAILER[position]) + ++position; + else if(position) + throw Decoding_Error("PGP: Malformed PGP trailer"); + + if(b == '=' && newline_seen) + { + while(b != '\n') + { + if(!source.read_byte(b)) + throw Decoding_Error("PGP: Bad CRC tail"); + if(b != '\n') + crc += static_cast<char>(b); + } + } + else if(b == '\n') + newline_seen = true; + else if(position == 0) + { + base64.write(b); + newline_seen = false; + } + } + base64.end_msg(); + + if(crc != "" && crc != base64.read_all_as_string(1)) + throw Decoding_Error("PGP: Corrupt CRC"); + + return base64.read_all(); + } + +/* +* OpenPGP Base64 decoding +*/ +SecureVector<byte> decode(DataSource& source, std::string& label) + { + std::map<std::string, std::string> ignored; + return decode(source, label, ignored); + } + +} + +} + diff --git a/botan/src/codec/openpgp/openpgp.h b/botan/src/codec/openpgp/openpgp.h new file mode 100644 index 0000000..890fcf0 --- /dev/null +++ b/botan/src/codec/openpgp/openpgp.h @@ -0,0 +1,34 @@ +/* +* OpenPGP +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_OPENPGP_H__ +#define BOTAN_OPENPGP_H__ + +#include <botan/data_src.h> +#include <string> +#include <map> + +namespace Botan { + +namespace OpenPGP { + +/* +* OpenPGP Base64 encoding/decoding +*/ +BOTAN_DLL std::string encode(const byte[], u32bit, const std::string&, + const std::map<std::string, std::string>&); +BOTAN_DLL SecureVector<byte> decode(DataSource&, std::string&, + std::map<std::string, std::string>&); + +BOTAN_DLL std::string encode(const byte[], u32bit, const std::string&); +BOTAN_DLL SecureVector<byte> decode(DataSource&, std::string&); + +} + +} + +#endif diff --git a/botan/src/codec/pem/info.txt b/botan/src/codec/pem/info.txt new file mode 100644 index 0000000..bbe8d4c --- /dev/null +++ b/botan/src/codec/pem/info.txt @@ -0,0 +1,15 @@ +realname "PEM Codec" + +define PEM_CODEC + +load_on auto + +<add> +pem.cpp +pem.h +</add> + +<requires> +base64 +filters +</requires> diff --git a/botan/src/codec/pem/pem.cpp b/botan/src/codec/pem/pem.cpp new file mode 100644 index 0000000..5141bee --- /dev/null +++ b/botan/src/codec/pem/pem.cpp @@ -0,0 +1,143 @@ +/* +* PEM Encoding/Decoding +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/pem.h> +#include <botan/filters.h> +#include <botan/parsing.h> + +namespace Botan { + +namespace PEM_Code { + +/* +* PEM encode BER/DER-encoded objects +*/ +std::string encode(const byte der[], u32bit length, const std::string& label, + u32bit width) + { + const std::string PEM_HEADER = "-----BEGIN " + label + "-----\n"; + const std::string PEM_TRAILER = "-----END " + label + "-----\n"; + + Pipe pipe(new Base64_Encoder(true, width)); + pipe.process_msg(der, length); + return (PEM_HEADER + pipe.read_all_as_string() + PEM_TRAILER); + } + +/* +* PEM encode BER/DER-encoded objects +*/ +std::string encode(const MemoryRegion<byte>& data, const std::string& label, + u32bit width) + { + return encode(data, data.size(), label, width); + } + +/* +* Decode PEM down to raw BER/DER +*/ +SecureVector<byte> decode_check_label(DataSource& source, + const std::string& label_want) + { + std::string label_got; + SecureVector<byte> ber = decode(source, label_got); + if(label_got != label_want) + throw Decoding_Error("PEM: Label mismatch, wanted " + label_want + + ", got " + label_got); + return ber; + } + +/* +* Decode PEM down to raw BER/DER +*/ +SecureVector<byte> decode(DataSource& source, std::string& label) + { + const u32bit RANDOM_CHAR_LIMIT = 8; + + const std::string PEM_HEADER1 = "-----BEGIN "; + const std::string PEM_HEADER2 = "-----"; + u32bit position = 0; + + while(position != PEM_HEADER1.length()) + { + byte b; + if(!source.read_byte(b)) + throw Decoding_Error("PEM: No PEM header found"); + if(b == PEM_HEADER1[position]) + ++position; + else if(position >= RANDOM_CHAR_LIMIT) + throw Decoding_Error("PEM: Malformed PEM header"); + else + position = 0; + } + position = 0; + while(position != PEM_HEADER2.length()) + { + byte b; + if(!source.read_byte(b)) + throw Decoding_Error("PEM: No PEM header found"); + if(b == PEM_HEADER2[position]) + ++position; + else if(position) + throw Decoding_Error("PEM: Malformed PEM header"); + + if(position == 0) + label += static_cast<char>(b); + } + + Pipe base64(new Base64_Decoder); + base64.start_msg(); + + const std::string PEM_TRAILER = "-----END " + label + "-----"; + position = 0; + while(position != PEM_TRAILER.length()) + { + byte b; + if(!source.read_byte(b)) + throw Decoding_Error("PEM: No PEM trailer found"); + if(b == PEM_TRAILER[position]) + ++position; + else if(position) + throw Decoding_Error("PEM: Malformed PEM trailer"); + + if(position == 0) + base64.write(b); + } + base64.end_msg(); + return base64.read_all(); + } + +/* +* Search for a PEM signature +*/ +bool matches(DataSource& source, const std::string& extra, + u32bit search_range) + { + const std::string PEM_HEADER = "-----BEGIN " + extra; + + SecureVector<byte> search_buf(search_range); + u32bit got = source.peek(search_buf, search_buf.size(), 0); + + if(got < PEM_HEADER.length()) + return false; + + u32bit index = 0; + + for(u32bit j = 0; j != got; ++j) + { + if(search_buf[j] == PEM_HEADER[index]) + ++index; + else + index = 0; + if(index == PEM_HEADER.size()) + return true; + } + return false; + } + +} + +} diff --git a/botan/src/codec/pem/pem.h b/botan/src/codec/pem/pem.h new file mode 100644 index 0000000..9fe8acb --- /dev/null +++ b/botan/src/codec/pem/pem.h @@ -0,0 +1,35 @@ +/* +* PEM Encoding/Decoding +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_PEM_H__ +#define BOTAN_PEM_H__ + +#include <botan/data_src.h> + +namespace Botan { + +namespace PEM_Code { + +/* +* PEM Encoding/Decoding +*/ +BOTAN_DLL std::string encode(const byte[], u32bit, + const std::string&, u32bit = 64); +BOTAN_DLL std::string encode(const MemoryRegion<byte>&, + const std::string&, u32bit = 64); + +BOTAN_DLL SecureVector<byte> decode(DataSource&, std::string&); +BOTAN_DLL SecureVector<byte> decode_check_label(DataSource&, + const std::string&); +BOTAN_DLL bool matches(DataSource&, const std::string& = "", + u32bit search_range = 4096); + +} + +} + +#endif diff --git a/botan/src/codec/zlib/info.txt b/botan/src/codec/zlib/info.txt new file mode 100644 index 0000000..9b1c35d --- /dev/null +++ b/botan/src/codec/zlib/info.txt @@ -0,0 +1,23 @@ +realname "Zlib Compressor" +#realname "Zlib/Gzip Compressor" + +define COMPRESSOR_ZLIB +#define COMPRESSOR_ZLIB,COMPRESSOR_GZIP + +load_on request +modset compression + +<add> +zlib.h +zlib.cpp +#gzip.h +#gzip.cpp +</add> + +<libs> +all -> z +</libs> + +<requires> +filters +</requires> diff --git a/botan/src/codec/zlib/zlib.cpp b/botan/src/codec/zlib/zlib.cpp new file mode 100644 index 0000000..246e329 --- /dev/null +++ b/botan/src/codec/zlib/zlib.cpp @@ -0,0 +1,287 @@ +/* +* Zlib Compressor +* (C) 2001 Peter J Jones +* 2001-2007 Jack Lloyd +* 2006 Matt Johnston +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/zlib.h> +#include <botan/exceptn.h> + +#include <cstring> +#include <map> +#include <zlib.h> + +namespace Botan { + +namespace { + +/* +* Allocation Information for Zlib +*/ +class Zlib_Alloc_Info + { + public: + std::map<void*, u32bit> current_allocs; + Allocator* alloc; + + Zlib_Alloc_Info() { alloc = Allocator::get(false); } + }; + +/* +* Allocation Function for Zlib +*/ +void* zlib_malloc(void* info_ptr, unsigned int n, unsigned int size) + { + Zlib_Alloc_Info* info = static_cast<Zlib_Alloc_Info*>(info_ptr); + void* ptr = info->alloc->allocate(n * size); + info->current_allocs[ptr] = n * size; + return ptr; + } + +/* +* Allocation Function for Zlib +*/ +void zlib_free(void* info_ptr, void* ptr) + { + Zlib_Alloc_Info* info = static_cast<Zlib_Alloc_Info*>(info_ptr); + std::map<void*, u32bit>::const_iterator i = info->current_allocs.find(ptr); + if(i == info->current_allocs.end()) + throw Invalid_Argument("zlib_free: Got pointer not allocated by us"); + info->alloc->deallocate(ptr, i->second); + } + +} + +/* +* Wrapper Type for Zlib z_stream +*/ +class Zlib_Stream + { + public: + z_stream stream; + + Zlib_Stream() + { + std::memset(&stream, 0, sizeof(z_stream)); + stream.zalloc = zlib_malloc; + stream.zfree = zlib_free; + stream.opaque = new Zlib_Alloc_Info; + } + ~Zlib_Stream() + { + Zlib_Alloc_Info* info = static_cast<Zlib_Alloc_Info*>(stream.opaque); + delete info; + std::memset(&stream, 0, sizeof(z_stream)); + } + }; + +/* +* Zlib_Compression Constructor +*/ +Zlib_Compression::Zlib_Compression(u32bit l) : + level((l >= 9) ? 9 : l), buffer(DEFAULT_BUFFERSIZE) + { + zlib = 0; + } + +/* +* Start Compressing with Zlib +*/ +void Zlib_Compression::start_msg() + { + clear(); + zlib = new Zlib_Stream; + if(deflateInit(&(zlib->stream), level) != Z_OK) + throw Exception("Zlib_Compression: Memory allocation error"); + } + +/* +* Compress Input with Zlib +*/ +void Zlib_Compression::write(const byte input[], u32bit length) + { + zlib->stream.next_in = static_cast<Bytef*>(const_cast<byte*>(input)); + zlib->stream.avail_in = length; + + while(zlib->stream.avail_in != 0) + { + zlib->stream.next_out = static_cast<Bytef*>(buffer.begin()); + zlib->stream.avail_out = buffer.size(); + deflate(&(zlib->stream), Z_NO_FLUSH); + send(buffer.begin(), buffer.size() - zlib->stream.avail_out); + } + } + +/* +* Finish Compressing with Zlib +*/ +void Zlib_Compression::end_msg() + { + zlib->stream.next_in = 0; + zlib->stream.avail_in = 0; + + int rc = Z_OK; + while(rc != Z_STREAM_END) + { + zlib->stream.next_out = reinterpret_cast<Bytef*>(buffer.begin()); + zlib->stream.avail_out = buffer.size(); + rc = deflate(&(zlib->stream), Z_FINISH); + send(buffer.begin(), buffer.size() - zlib->stream.avail_out); + } + clear(); + } + +/* +* Flush the Zlib Compressor +*/ +void Zlib_Compression::flush() + { + zlib->stream.next_in = 0; + zlib->stream.avail_in = 0; + + while(true) + { + zlib->stream.avail_out = buffer.size(); + + zlib->stream.next_out = reinterpret_cast<Bytef*>(buffer.begin()); + + + deflate(&(zlib->stream), Z_FULL_FLUSH); + send(buffer.begin(), buffer.size() - zlib->stream.avail_out); + if(zlib->stream.avail_out == buffer.size()) break; + } + } + +/* +* Clean up Compression Context +*/ +void Zlib_Compression::clear() + { + if(zlib) + { + deflateEnd(&(zlib->stream)); + delete zlib; + zlib = 0; + } + + buffer.clear(); + } + +/* +* Zlib_Decompression Constructor +*/ +Zlib_Decompression::Zlib_Decompression() : buffer(DEFAULT_BUFFERSIZE) + { + zlib = 0; + no_writes = true; + } + +/* +* Start Decompressing with Zlib +*/ +void Zlib_Decompression::start_msg() + { + clear(); + zlib = new Zlib_Stream; + if(inflateInit(&(zlib->stream)) != Z_OK) + throw Exception("Zlib_Decompression: Memory allocation error"); + } + +/* +* Decompress Input with Zlib +*/ +void Zlib_Decompression::write(const byte input_arr[], u32bit length) + { + if(length) no_writes = false; + + // non-const needed by zlib api :( + Bytef* input = reinterpret_cast<Bytef*>(const_cast<byte*>(input_arr)); + + zlib->stream.next_in = input; + zlib->stream.avail_in = length; + + while(zlib->stream.avail_in != 0) + { + zlib->stream.next_out = reinterpret_cast<Bytef*>(buffer.begin()); + zlib->stream.avail_out = buffer.size(); + + int rc = inflate(&(zlib->stream), Z_SYNC_FLUSH); + + if(rc != Z_OK && rc != Z_STREAM_END) + { + clear(); + if(rc == Z_DATA_ERROR) + throw Decoding_Error("Zlib_Decompression: Data integrity error"); + if(rc == Z_NEED_DICT) + throw Decoding_Error("Zlib_Decompression: Need preset dictionary"); + if(rc == Z_MEM_ERROR) + throw Exception("Zlib_Decompression: Memory allocation error"); + throw Exception("Zlib_Decompression: Unknown decompress error"); + } + + send(buffer.begin(), buffer.size() - zlib->stream.avail_out); + + if(rc == Z_STREAM_END) + { + u32bit read_from_block = length - zlib->stream.avail_in; + start_msg(); + + zlib->stream.next_in = input + read_from_block; + zlib->stream.avail_in = length - read_from_block; + + input += read_from_block; + length -= read_from_block; + } + } + } + +/* +* Finish Decompressing with Zlib +*/ +void Zlib_Decompression::end_msg() + { + if(no_writes) return; + zlib->stream.next_in = 0; + zlib->stream.avail_in = 0; + + int rc = Z_OK; + + while(rc != Z_STREAM_END) + { + zlib->stream.next_out = reinterpret_cast<Bytef*>(buffer.begin()); + zlib->stream.avail_out = buffer.size(); + rc = inflate(&(zlib->stream), Z_SYNC_FLUSH); + + if(rc != Z_OK && rc != Z_STREAM_END) + { + clear(); + throw Exception("Zlib_Decompression: Error finalizing decompression"); + } + + send(buffer.begin(), buffer.size() - zlib->stream.avail_out); + } + + clear(); + } + +/* +* Clean up Decompression Context +*/ +void Zlib_Decompression::clear() + { + no_writes = true; + + if(zlib) + { + inflateEnd(&(zlib->stream)); + delete zlib; + zlib = 0; + } + + buffer.clear(); + } + +} diff --git a/botan/src/codec/zlib/zlib.h b/botan/src/codec/zlib/zlib.h new file mode 100644 index 0000000..4a7f3bc --- /dev/null +++ b/botan/src/codec/zlib/zlib.h @@ -0,0 +1,58 @@ +/* +* Zlib Compressor +* (C) 2001 Peter J Jones +* 2001-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ZLIB_H__ +#define BOTAN_ZLIB_H__ + +#include <botan/filter.h> + +namespace Botan { + +/* +* Zlib Compression Filter +*/ +class BOTAN_DLL Zlib_Compression : public Filter + { + public: + void write(const byte input[], u32bit length); + void start_msg(); + void end_msg(); + + void flush(); + + Zlib_Compression(u32bit = 6); + ~Zlib_Compression() { clear(); } + private: + void clear(); + const u32bit level; + SecureVector<byte> buffer; + class Zlib_Stream* zlib; + }; + +/* +* Zlib Decompression Filter +*/ +class BOTAN_DLL Zlib_Decompression : public Filter + { + public: + void write(const byte input[], u32bit length); + void start_msg(); + void end_msg(); + + Zlib_Decompression(); + ~Zlib_Decompression() { clear(); } + private: + void clear(); + SecureVector<byte> buffer; + class Zlib_Stream* zlib; + bool no_writes; + }; + +} + +#endif diff --git a/botan/src/cryptobox/cryptobox.cpp b/botan/src/cryptobox/cryptobox.cpp new file mode 100644 index 0000000..c27bbaf --- /dev/null +++ b/botan/src/cryptobox/cryptobox.cpp @@ -0,0 +1,146 @@ +/* +* Cryptobox Message Routines +* (C) 2009 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/cryptobox.h> +#include <botan/filters.h> +#include <botan/pipe.h> +#include <botan/serpent.h> +#include <botan/sha2_64.h> +#include <botan/ctr.h> +#include <botan/hmac.h> +#include <botan/pbkdf2.h> +#include <botan/pem.h> +#include <botan/loadstor.h> +#include <botan/mem_ops.h> + +namespace Botan { + +namespace CryptoBox { + +namespace { + +/* +First 24 bits of SHA-256("Botan Cryptobox"), followed by 8 0 bits +for later use as flags, etc if needed +*/ +const u32bit CRYPTOBOX_VERSION_CODE = 0xEFC22400; + +const u32bit VERSION_CODE_LEN = 4; +const u32bit CIPHER_KEY_LEN = 32; +const u32bit CIPHER_IV_LEN = 16; +const u32bit MAC_KEY_LEN = 32; +const u32bit MAC_OUTPUT_LEN = 20; +const u32bit PBKDF_SALT_LEN = 10; +const u32bit PBKDF_ITERATIONS = 8 * 1024; + +const u32bit PBKDF_OUTPUT_LEN = CIPHER_KEY_LEN + CIPHER_IV_LEN + MAC_KEY_LEN; + +} + +std::string encrypt(const byte input[], u32bit input_len, + const std::string& passphrase, + RandomNumberGenerator& rng) + { + SecureVector<byte> pbkdf_salt(PBKDF_SALT_LEN); + rng.randomize(pbkdf_salt.begin(), pbkdf_salt.size()); + + PKCS5_PBKDF2 pbkdf(new HMAC(new SHA_512)); + pbkdf.change_salt(pbkdf_salt.begin(), pbkdf_salt.size()); + pbkdf.set_iterations(PBKDF_ITERATIONS); + + OctetString mk = pbkdf.derive_key(PBKDF_OUTPUT_LEN, passphrase); + + SymmetricKey cipher_key(mk.begin(), CIPHER_KEY_LEN); + SymmetricKey mac_key(mk.begin() + CIPHER_KEY_LEN, MAC_KEY_LEN); + InitializationVector iv(mk.begin() + CIPHER_KEY_LEN + MAC_KEY_LEN, + CIPHER_IV_LEN); + + Pipe pipe(new CTR_BE(new Serpent, cipher_key, iv), + new Fork( + 0, + new MAC_Filter(new HMAC(new SHA_512), + mac_key, MAC_OUTPUT_LEN))); + + pipe.process_msg(input, input_len); + + /* + Output format is: + version # (4 bytes) + salt (10 bytes) + mac (20 bytes) + ciphertext + */ + u32bit ciphertext_len = pipe.remaining(0); + + SecureVector<byte> out_buf; + + for(u32bit i = 0; i != VERSION_CODE_LEN; ++i) + out_buf.append(get_byte(i, CRYPTOBOX_VERSION_CODE)); + + out_buf.append(pbkdf_salt.begin(), pbkdf_salt.size()); + + out_buf.grow_to(out_buf.size() + MAC_OUTPUT_LEN + ciphertext_len); + pipe.read(out_buf + VERSION_CODE_LEN + PBKDF_SALT_LEN, MAC_OUTPUT_LEN, 1); + pipe.read(out_buf + VERSION_CODE_LEN + PBKDF_SALT_LEN + MAC_OUTPUT_LEN, + ciphertext_len, 0); + + return PEM_Code::encode(out_buf.begin(), out_buf.size(), + "BOTAN CRYPTOBOX MESSAGE"); + } + +std::string decrypt(const byte input[], u32bit input_len, + const std::string& passphrase) + { + DataSource_Memory input_src(input, input_len); + SecureVector<byte> ciphertext = + PEM_Code::decode_check_label(input_src, + "BOTAN CRYPTOBOX MESSAGE"); + + if(ciphertext.size() < (VERSION_CODE_LEN + PBKDF_SALT_LEN + MAC_OUTPUT_LEN)) + throw Decoding_Error("Invalid CryptoBox input"); + + for(u32bit i = 0; i != VERSION_CODE_LEN; ++i) + if(ciphertext[i] != get_byte(i, CRYPTOBOX_VERSION_CODE)) + throw Decoding_Error("Bad CryptoBox version"); + + SecureVector<byte> pbkdf_salt(ciphertext + VERSION_CODE_LEN, PBKDF_SALT_LEN); + + PKCS5_PBKDF2 pbkdf(new HMAC(new SHA_512)); + pbkdf.change_salt(pbkdf_salt.begin(), pbkdf_salt.size()); + pbkdf.set_iterations(PBKDF_ITERATIONS); + + OctetString mk = pbkdf.derive_key(PBKDF_OUTPUT_LEN, passphrase); + + SymmetricKey cipher_key(mk.begin(), CIPHER_KEY_LEN); + SymmetricKey mac_key(mk.begin() + CIPHER_KEY_LEN, MAC_KEY_LEN); + InitializationVector iv(mk.begin() + CIPHER_KEY_LEN + MAC_KEY_LEN, + CIPHER_IV_LEN); + + Pipe pipe(new Fork( + new CTR_BE(new Serpent, cipher_key, iv), + new MAC_Filter(new HMAC(new SHA_512), + mac_key, MAC_OUTPUT_LEN))); + + const u32bit ciphertext_offset = + VERSION_CODE_LEN + PBKDF_SALT_LEN + MAC_OUTPUT_LEN; + + pipe.process_msg(ciphertext + ciphertext_offset, + ciphertext.size() - ciphertext_offset); + + byte computed_mac[MAC_OUTPUT_LEN]; + pipe.read(computed_mac, MAC_OUTPUT_LEN, 1); + + if(!same_mem(computed_mac, ciphertext + VERSION_CODE_LEN + PBKDF_SALT_LEN, + MAC_OUTPUT_LEN)) + throw Integrity_Failure("CryptoBox integrity failure"); + + return pipe.read_all_as_string(0); + } + +} + +} diff --git a/botan/src/cryptobox/cryptobox.h b/botan/src/cryptobox/cryptobox.h new file mode 100644 index 0000000..a30cb24 --- /dev/null +++ b/botan/src/cryptobox/cryptobox.h @@ -0,0 +1,42 @@ +/* +* Cryptobox Message Routines +* (C) 2009 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_CRYPTOBOX_H__ +#define BOTAN_CRYPTOBOX_H__ + +#include <string> +#include <botan/rng.h> + +namespace Botan { + +namespace CryptoBox { + +/** +* Encrypt a message +* @param input the input data +* @param input_len the length of input in bytes +* @param passphrase the passphrase used to encrypt the message +* @param rng a ref to a random number generator, such as AutoSeeded_RNG +*/ +BOTAN_DLL std::string encrypt(const byte input[], u32bit input_len, + const std::string& passphrase, + RandomNumberGenerator& rng); + +/** +* Decrypt a message encrypted with CryptoBox::encrypt +* @param input the input data +* @param input_len the length of input in bytes +* @param passphrase the passphrase used to encrypt the message +*/ +BOTAN_DLL std::string decrypt(const byte input[], u32bit input_len, + const std::string& passphrase); + +} + +} + +#endif diff --git a/botan/src/cryptobox/info.txt b/botan/src/cryptobox/info.txt new file mode 100644 index 0000000..b9b9806 --- /dev/null +++ b/botan/src/cryptobox/info.txt @@ -0,0 +1,22 @@ +realname "Crypto Box" + +load_on auto + +define CRYPTO_BOX + +<add> +cryptobox.h +cryptobox.cpp +</add> + +<requires> +filters +ctr +hmac +rng +serpent +sha2 +base64 +pbkdf2 +pem +</requires> diff --git a/botan/src/engine/amd64_eng/eng_amd64.cpp b/botan/src/engine/amd64_eng/eng_amd64.cpp new file mode 100644 index 0000000..eed2cf3 --- /dev/null +++ b/botan/src/engine/amd64_eng/eng_amd64.cpp @@ -0,0 +1,27 @@ +/** +* AMD64 Assembly Implementation Engine +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/eng_amd64.h> + +#if defined(BOTAN_HAS_SHA1_AMD64) + #include <botan/sha1_amd64.h> +#endif + +namespace Botan { + +HashFunction* AMD64_Assembler_Engine::find_hash(const SCAN_Name& request, + Algorithm_Factory&) const + { +#if defined(BOTAN_HAS_SHA1_AMD64) + if(request.algo_name() == "SHA-160") + return new SHA_160_AMD64; +#endif + + return 0; + } + +} diff --git a/botan/src/engine/amd64_eng/eng_amd64.h b/botan/src/engine/amd64_eng/eng_amd64.h new file mode 100644 index 0000000..528291f --- /dev/null +++ b/botan/src/engine/amd64_eng/eng_amd64.h @@ -0,0 +1,26 @@ +/** +* x86-64 Assembly Implementation Engines +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_AMD64_ASM_ENGINE_H__ +#define BOTAN_AMD64_ASM_ENGINE_H__ + +#include <botan/engine.h> + +namespace Botan { + +class BOTAN_DLL AMD64_Assembler_Engine : public Engine + { + public: + std::string provider_name() const { return "amd64"; } + private: + HashFunction* find_hash(const SCAN_Name& reqeust, + Algorithm_Factory&) const; + }; + +} + +#endif diff --git a/botan/src/engine/amd64_eng/info.txt b/botan/src/engine/amd64_eng/info.txt new file mode 100644 index 0000000..47f8914 --- /dev/null +++ b/botan/src/engine/amd64_eng/info.txt @@ -0,0 +1,10 @@ +realname "AMD64 Assembler Engine" + +define ENGINE_AMD64_ASSEMBLER + +load_on dep + +<add> +eng_amd64.cpp +eng_amd64.h +</add> diff --git a/botan/src/engine/def_engine/def_eng.h b/botan/src/engine/def_engine/def_eng.h new file mode 100644 index 0000000..2d71454 --- /dev/null +++ b/botan/src/engine/def_engine/def_eng.h @@ -0,0 +1,83 @@ +/* +* Default Engine +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_DEFAULT_ENGINE_H__ +#define BOTAN_DEFAULT_ENGINE_H__ + +#include <botan/engine.h> + +namespace Botan { + +/* +* Default Engine +*/ +class BOTAN_DLL Default_Engine : public Engine + { + public: + std::string provider_name() const { return "core"; } + +#if defined(BOTAN_HAS_IF_PUBLIC_KEY_FAMILY) + IF_Operation* if_op(const BigInt&, const BigInt&, const BigInt&, + const BigInt&, const BigInt&, const BigInt&, + const BigInt&, const BigInt&) const; +#endif + +#if defined(BOTAN_HAS_DSA) + DSA_Operation* dsa_op(const DL_Group&, const BigInt&, + const BigInt&) const; +#endif + +#if defined(BOTAN_HAS_NYBERG_RUEPPEL) + NR_Operation* nr_op(const DL_Group&, const BigInt&, const BigInt&) const; +#endif + +#if defined(BOTAN_HAS_ELGAMAL) + ELG_Operation* elg_op(const DL_Group&, const BigInt&, + const BigInt&) const; +#endif + +#if defined(BOTAN_HAS_DIFFIE_HELLMAN) + DH_Operation* dh_op(const DL_Group&, const BigInt&) const; +#endif + +#if defined(BOTAN_HAS_ECDSA) + virtual ECDSA_Operation* ecdsa_op(const EC_Domain_Params&, + const BigInt&, + const PointGFp&) const; +#endif + +#if defined(BOTAN_HAS_ECKAEG) + virtual ECKAEG_Operation* eckaeg_op(const EC_Domain_Params&, + const BigInt&, + const PointGFp&) const; +#endif + + Modular_Exponentiator* mod_exp(const BigInt&, + Power_Mod::Usage_Hints) const; + + virtual bool can_add_algorithms() { return true; } + + Keyed_Filter* get_cipher(const std::string&, Cipher_Dir, + Algorithm_Factory&); + + private: + BlockCipher* find_block_cipher(const SCAN_Name&, + Algorithm_Factory&) const; + + StreamCipher* find_stream_cipher(const SCAN_Name&, + Algorithm_Factory&) const; + + HashFunction* find_hash(const SCAN_Name& reqeust, + Algorithm_Factory&) const; + + MessageAuthenticationCode* find_mac(const SCAN_Name& reqeust, + Algorithm_Factory&) const; + }; + +} + +#endif diff --git a/botan/src/engine/def_engine/def_mode.cpp b/botan/src/engine/def_engine/def_mode.cpp new file mode 100644 index 0000000..2b093a0 --- /dev/null +++ b/botan/src/engine/def_engine/def_mode.cpp @@ -0,0 +1,209 @@ +/* +* Default Engine +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/def_eng.h> +#include <botan/parsing.h> +#include <botan/filters.h> +#include <botan/algo_factory.h> +#include <botan/mode_pad.h> +#include <memory> + +#if defined(BOTAN_HAS_ECB) + #include <botan/ecb.h> +#endif + +#if defined(BOTAN_HAS_CBC) + #include <botan/cbc.h> +#endif + +#if defined(BOTAN_HAS_CTS) + #include <botan/cts.h> +#endif + +#if defined(BOTAN_HAS_CFB) + #include <botan/cfb.h> +#endif + +#if defined(BOTAN_HAS_OFB) + #include <botan/ofb.h> +#endif + +#if defined(BOTAN_HAS_CTR) + #include <botan/ctr.h> +#endif + +#if defined(BOTAN_HAS_EAX) + #include <botan/eax.h> +#endif + +#if defined(BOTAN_HAS_XTS) + #include <botan/xts.h> +#endif + +namespace Botan { + +namespace { + +/** +* Get a block cipher padding method by name +*/ +BlockCipherModePaddingMethod* get_bc_pad(const std::string& algo_spec) + { + SCAN_Name request(algo_spec); + +#if defined(BOTAN_HAS_CIPHER_MODE_PADDING) + if(request.algo_name() == "PKCS7") + return new PKCS7_Padding; + + if(request.algo_name() == "OneAndZeros") + return new OneAndZeros_Padding; + + if(request.algo_name() == "X9.23") + return new ANSI_X923_Padding; + + if(request.algo_name() == "NoPadding") + return new Null_Padding; +#endif + + throw Algorithm_Not_Found(algo_spec); + } + +} + +/* +* Get a cipher object +*/ +Keyed_Filter* Default_Engine::get_cipher(const std::string& algo_spec, + Cipher_Dir direction, + Algorithm_Factory& af) + { + std::vector<std::string> algo_parts = split_on(algo_spec, '/'); + if(algo_parts.empty()) + throw Invalid_Algorithm_Name(algo_spec); + + const std::string cipher_name = algo_parts[0]; + + // check if it is a stream cipher first (easy case) + const StreamCipher* stream_cipher = af.prototype_stream_cipher(cipher_name); + if(stream_cipher) + return new StreamCipher_Filter(stream_cipher->clone()); + + const BlockCipher* block_cipher = af.prototype_block_cipher(cipher_name); + if(!block_cipher) + return 0; + + if(algo_parts.size() != 2 && algo_parts.size() != 3) + return 0; + + std::string mode = algo_parts[1]; + u32bit bits = 0; + + if(mode.find("CFB") != std::string::npos || + mode.find("EAX") != std::string::npos) + { + std::vector<std::string> algo_info = parse_algorithm_name(mode); + mode = algo_info[0]; + if(algo_info.size() == 1) + bits = 8*block_cipher->BLOCK_SIZE; + else if(algo_info.size() == 2) + bits = to_u32bit(algo_info[1]); + else + throw Invalid_Algorithm_Name(algo_spec); + } + + std::string padding; + if(algo_parts.size() == 3) + padding = algo_parts[2]; + else + padding = (mode == "CBC") ? "PKCS7" : "NoPadding"; + + if(mode == "ECB" && padding == "CTS") + return 0; + else if((mode != "CBC" && mode != "ECB") && padding != "NoPadding") + throw Invalid_Algorithm_Name(algo_spec); + +#if defined(BOTAN_HAS_OFB) + if(mode == "OFB") + return new OFB(block_cipher->clone()); +#endif + +#if defined(BOTAN_HAS_CTR) + if(mode == "CTR-BE") + return new CTR_BE(block_cipher->clone()); +#endif + +#if defined(BOTAN_HAS_ECB) + if(mode == "ECB") + { + if(direction == ENCRYPTION) + return new ECB_Encryption(block_cipher->clone(), get_bc_pad(padding)); + else + return new ECB_Decryption(block_cipher->clone(), get_bc_pad(padding)); + } +#endif + +#if defined(BOTAN_HAS_CFB) + if(mode == "CFB") + { + if(direction == ENCRYPTION) + return new CFB_Encryption(block_cipher->clone(), bits); + else + return new CFB_Decryption(block_cipher->clone(), bits); + } +#endif + + if(mode == "CBC") + { + if(padding == "CTS") + { +#if defined(BOTAN_HAS_CTS) + if(direction == ENCRYPTION) + return new CTS_Encryption(block_cipher->clone()); + else + return new CTS_Decryption(block_cipher->clone()); +#else + return 0; +#endif + } + +#if defined(BOTAN_HAS_CBC) + if(direction == ENCRYPTION) + return new CBC_Encryption(block_cipher->clone(), + get_bc_pad(padding)); + else + return new CBC_Decryption(block_cipher->clone(), + get_bc_pad(padding)); +#else + return 0; +#endif + } + +#if defined(BOTAN_HAS_EAX) + if(mode == "EAX") + { + if(direction == ENCRYPTION) + return new EAX_Encryption(block_cipher->clone(), bits); + else + return new EAX_Decryption(block_cipher->clone(), bits); + } +#endif + +#if defined(BOTAN_HAS_XTS) + if(mode == "XTS") + { + if(direction == ENCRYPTION) + return new XTS_Encryption(block_cipher->clone()); + else + return new XTS_Decryption(block_cipher->clone()); + } +#endif + + throw Algorithm_Not_Found("get_mode: " + cipher_name + "/" + + mode + "/" + padding); + } + +} diff --git a/botan/src/engine/def_engine/def_pk_ops.cpp b/botan/src/engine/def_engine/def_pk_ops.cpp new file mode 100644 index 0000000..31dce7a --- /dev/null +++ b/botan/src/engine/def_engine/def_pk_ops.cpp @@ -0,0 +1,121 @@ +/* +* PK Operations +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/def_eng.h> + +#if defined(BOTAN_HAS_IF_PUBLIC_KEY_FAMILY) + #include <botan/if_op.h> +#endif + +#if defined(BOTAN_HAS_DSA) + #include <botan/dsa_op.h> +#endif + +#if defined(BOTAN_HAS_NYBERG_RUEPPEL) + #include <botan/nr_op.h> +#endif + +#if defined(BOTAN_HAS_ELGAMAL) + #include <botan/elg_op.h> +#endif + +#if defined(BOTAN_HAS_DIFFIE_HELLMAN) + #include <botan/dh_op.h> +#endif + +#if defined(BOTAN_HAS_ECDSA) + #include <botan/ecdsa_op.h> +#endif + +#if defined(BOTAN_HAS_ECKAEG) + #include <botan/eckaeg_op.h> +#endif + +namespace Botan { + +#if defined(BOTAN_HAS_IF_PUBLIC_KEY_FAMILY) +/* +* Acquire an IF op +*/ +IF_Operation* Default_Engine::if_op(const BigInt& e, const BigInt& n, + const BigInt& d, const BigInt& p, + const BigInt& q, const BigInt& d1, + const BigInt& d2, const BigInt& c) const + { + return new Default_IF_Op(e, n, d, p, q, d1, d2, c); + } +#endif + +#if defined(BOTAN_HAS_DSA) +/* +* Acquire a DSA op +*/ +DSA_Operation* Default_Engine::dsa_op(const DL_Group& group, const BigInt& y, + const BigInt& x) const + { + return new Default_DSA_Op(group, y, x); + } +#endif + +#if defined(BOTAN_HAS_NYBERG_RUEPPEL) +/* +* Acquire a NR op +*/ +NR_Operation* Default_Engine::nr_op(const DL_Group& group, const BigInt& y, + const BigInt& x) const + { + return new Default_NR_Op(group, y, x); + } +#endif + +#if defined(BOTAN_HAS_ELGAMAL) +/* +* Acquire an ElGamal op +*/ +ELG_Operation* Default_Engine::elg_op(const DL_Group& group, const BigInt& y, + const BigInt& x) const + { + return new Default_ELG_Op(group, y, x); + } +#endif + +#if defined(BOTAN_HAS_DIFFIE_HELLMAN) +/* +* Acquire a DH op +*/ +DH_Operation* Default_Engine::dh_op(const DL_Group& group, + const BigInt& x) const + { + return new Default_DH_Op(group, x); + } +#endif + +#if defined(BOTAN_HAS_ECDSA) +/* +* Acquire a ECDSA op +*/ +ECDSA_Operation* Default_Engine::ecdsa_op(const EC_Domain_Params& dom_pars, + const BigInt& priv_key, + const PointGFp& pub_key) const + { + return new Default_ECDSA_Op(dom_pars, priv_key, pub_key); + } +#endif + +#if defined(BOTAN_HAS_ECKAEG) +/* +* Acquire a ECKAEG op +*/ +ECKAEG_Operation* Default_Engine::eckaeg_op(const EC_Domain_Params& dom_pars, + const BigInt& priv_key, + const PointGFp& pub_key) const + { + return new Default_ECKAEG_Op(dom_pars, priv_key, pub_key); + } +#endif + +} diff --git a/botan/src/engine/def_engine/def_powm.cpp b/botan/src/engine/def_engine/def_powm.cpp new file mode 100644 index 0000000..9e7a88a --- /dev/null +++ b/botan/src/engine/def_engine/def_powm.cpp @@ -0,0 +1,24 @@ +/* +* Modular Exponentiation +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/def_eng.h> +#include <botan/def_powm.h> + +namespace Botan { + +/* +* Choose a modular exponentation algorithm +*/ +Modular_Exponentiator* +Default_Engine::mod_exp(const BigInt& n, Power_Mod::Usage_Hints hints) const + { + if(n.is_odd()) + return new Montgomery_Exponentiator(n, hints); + return new Fixed_Window_Exponentiator(n, hints); + } + +} diff --git a/botan/src/engine/def_engine/info.txt b/botan/src/engine/def_engine/info.txt new file mode 100644 index 0000000..fd31ee2 --- /dev/null +++ b/botan/src/engine/def_engine/info.txt @@ -0,0 +1,24 @@ +realname "Default Engine" + +define DEFAULT_ENGINE + +load_on auto + +<add> +def_eng.h +def_mode.cpp +def_pk_ops.cpp +def_powm.cpp +lookup_block.cpp +lookup_hash.cpp +lookup_mac.cpp +lookup_stream.cpp +</add> + +<requires> +algo_factory +filters +libstate +mode_pad +numbertheory +</requires> diff --git a/botan/src/engine/def_engine/lookup_block.cpp b/botan/src/engine/def_engine/lookup_block.cpp new file mode 100644 index 0000000..7ee5f58 --- /dev/null +++ b/botan/src/engine/def_engine/lookup_block.cpp @@ -0,0 +1,264 @@ +/* +* Block Cipher Lookup +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/def_eng.h> +#include <botan/scan_name.h> +#include <botan/algo_factory.h> + +#if defined(BOTAN_HAS_AES) + #include <botan/aes.h> +#endif + +#if defined(BOTAN_HAS_BLOWFISH) + #include <botan/blowfish.h> +#endif + +#if defined(BOTAN_HAS_CAST) + #include <botan/cast128.h> + #include <botan/cast256.h> +#endif + +#if defined(BOTAN_HAS_DES) + #include <botan/des.h> + #include <botan/desx.h> +#endif + +#if defined(BOTAN_HAS_GOST_28147_89) + #include <botan/gost_28147.h> +#endif + +#if defined(BOTAN_HAS_IDEA) + #include <botan/idea.h> +#endif + +#if defined(BOTAN_HAS_KASUMI) + #include <botan/kasumi.h> +#endif + +#if defined(BOTAN_HAS_LION) + #include <botan/lion.h> +#endif + +#if defined(BOTAN_HAS_LUBY_RACKOFF) + #include <botan/lubyrack.h> +#endif + +#if defined(BOTAN_HAS_MARS) + #include <botan/mars.h> +#endif + +#if defined(BOTAN_HAS_MISTY1) + #include <botan/misty1.h> +#endif + +#if defined(BOTAN_HAS_NOEKEON) + #include <botan/noekeon.h> +#endif + +#if defined(BOTAN_HAS_RC2) + #include <botan/rc2.h> +#endif + +#if defined(BOTAN_HAS_RC5) + #include <botan/rc5.h> +#endif + +#if defined(BOTAN_HAS_RC6) + #include <botan/rc6.h> +#endif + +#if defined(BOTAN_HAS_SAFER) + #include <botan/safer_sk.h> +#endif + +#if defined(BOTAN_HAS_SEED) + #include <botan/seed.h> +#endif + +#if defined(BOTAN_HAS_SERPENT) + #include <botan/serpent.h> +#endif + +#if defined(BOTAN_HAS_SKIPJACK) + #include <botan/skipjack.h> +#endif + +#if defined(BOTAN_HAS_SQUARE) + #include <botan/square.h> +#endif + +#if defined(BOTAN_HAS_TEA) + #include <botan/tea.h> +#endif + +#if defined(BOTAN_HAS_TWOFISH) + #include <botan/twofish.h> +#endif + +#if defined(BOTAN_HAS_XTEA) + #include <botan/xtea.h> +#endif + +namespace Botan { + +/* +* Look for an algorithm with this name +*/ +BlockCipher* +Default_Engine::find_block_cipher(const SCAN_Name& request, + Algorithm_Factory& af) const + { + +#if defined(BOTAN_HAS_AES) + if(request.algo_name() == "AES") + return new AES; + if(request.algo_name() == "AES-128") + return new AES_128; + if(request.algo_name() == "AES-192") + return new AES_192; + if(request.algo_name() == "AES-256") + return new AES_256; +#endif + +#if defined(BOTAN_HAS_BLOWFISH) + if(request.algo_name() == "Blowfish") + return new Blowfish; +#endif + +#if defined(BOTAN_HAS_CAST) + if(request.algo_name() == "CAST-128") + return new CAST_128; + if(request.algo_name() == "CAST-256") + return new CAST_256; +#endif + +#if defined(BOTAN_HAS_DES) + if(request.algo_name() == "DES") + return new DES; + if(request.algo_name() == "DESX") + return new DESX; + if(request.algo_name() == "TripleDES") + return new TripleDES; +#endif + +#if defined(BOTAN_HAS_GOST_28147_89) + if(request.algo_name() == "GOST-28147-89") + return new GOST_28147_89(request.arg(0, "R3411_94_TestParam")); +#endif + +#if defined(BOTAN_HAS_IDEA) + if(request.algo_name() == "IDEA") + return new IDEA; +#endif + +#if defined(BOTAN_HAS_KASUMI) + if(request.algo_name() == "KASUMI") + return new KASUMI; +#endif + +#if defined(BOTAN_HAS_MARS) + if(request.algo_name() == "MARS") + return new MARS; +#endif + +#if defined(BOTAN_HAS_MISTY1) + if(request.algo_name() == "MISTY1") + return new MISTY1(request.arg_as_u32bit(0, 8)); +#endif + +#if defined(BOTAN_HAS_NOEKEON) + if(request.algo_name() == "Noekeon") + return new Noekeon; +#endif + +#if defined(BOTAN_HAS_RC2) + if(request.algo_name() == "RC2") + return new RC2; +#endif + +#if defined(BOTAN_HAS_RC5) + if(request.algo_name() == "RC5") + return new RC5(request.arg_as_u32bit(0, 12)); +#endif + +#if defined(BOTAN_HAS_RC6) + if(request.algo_name() == "RC6") + return new RC6; +#endif + +#if defined(BOTAN_HAS_SAFER) + if(request.algo_name() == "SAFER-SK") + return new SAFER_SK(request.arg_as_u32bit(0, 10)); +#endif + +#if defined(BOTAN_HAS_SEED) + if(request.algo_name() == "SEED") + return new SEED; +#endif + +#if defined(BOTAN_HAS_SERPENT) + if(request.algo_name() == "Serpent") + return new Serpent; +#endif + +#if defined(BOTAN_HAS_SKIPJACK) + if(request.algo_name() == "Skipjack") + return new Skipjack; +#endif + +#if defined(BOTAN_HAS_SQUARE) + if(request.algo_name() == "Square") + return new Square; +#endif + +#if defined(BOTAN_HAS_TEA) + if(request.algo_name() == "TEA") + return new TEA; +#endif + +#if defined(BOTAN_HAS_TWOFISH) + if(request.algo_name() == "Twofish") + return new Twofish; +#endif + +#if defined(BOTAN_HAS_XTEA) + if(request.algo_name() == "XTEA") + return new XTEA; +#endif + +#if defined(BOTAN_HAS_LUBY_RACKOFF) + if(request.algo_name() == "Luby-Rackoff" && request.arg_count() == 1) + { + const HashFunction* hash = af.prototype_hash_function(request.arg(0)); + + if(hash) + return new LubyRackoff(hash->clone()); + } +#endif + +#if defined(BOTAN_HAS_LION) + if(request.algo_name() == "Lion" && request.arg_count_between(2, 3)) + { + const u32bit block_size = request.arg_as_u32bit(2, 1024); + + const HashFunction* hash = + af.prototype_hash_function(request.arg(0)); + + const StreamCipher* stream_cipher = + af.prototype_stream_cipher(request.arg(1)); + + if(!hash || !stream_cipher) + return 0; + + return new Lion(hash->clone(), stream_cipher->clone(), block_size); + } +#endif + + return 0; + } + +} diff --git a/botan/src/engine/def_engine/lookup_hash.cpp b/botan/src/engine/def_engine/lookup_hash.cpp new file mode 100644 index 0000000..58136fc --- /dev/null +++ b/botan/src/engine/def_engine/lookup_hash.cpp @@ -0,0 +1,209 @@ +/* +* Hash Algorithms Lookup +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/def_eng.h> +#include <botan/scan_name.h> +#include <botan/algo_factory.h> +#include <memory> + +#if defined(BOTAN_HAS_ADLER32) + #include <botan/adler32.h> +#endif + +#if defined(BOTAN_HAS_CRC24) + #include <botan/crc24.h> +#endif + +#if defined(BOTAN_HAS_CRC32) + #include <botan/crc32.h> +#endif + +#if defined(BOTAN_HAS_FORK_256) + #include <botan/fork256.h> +#endif + +#if defined(BOTAN_HAS_GOST_34_11) + #include <botan/gost_3411.h> +#endif + +#if defined(BOTAN_HAS_HAS_160) + #include <botan/has160.h> +#endif + +#if defined(BOTAN_HAS_MD2) + #include <botan/md2.h> +#endif + +#if defined(BOTAN_HAS_MD4) + #include <botan/md4.h> +#endif + +#if defined(BOTAN_HAS_MD5) + #include <botan/md5.h> +#endif + +#if defined(BOTAN_HAS_RIPEMD_128) + #include <botan/rmd128.h> +#endif + +#if defined(BOTAN_HAS_RIPEMD_160) + #include <botan/rmd160.h> +#endif + +#if defined(BOTAN_HAS_SHA1) + #include <botan/sha160.h> +#endif + +#if defined(BOTAN_HAS_SHA2) + #include <botan/sha2_32.h> + #include <botan/sha2_64.h> +#endif + +#if defined(BOTAN_HAS_SKEIN_512) + #include <botan/skein_512.h> +#endif + +#if defined(BOTAN_HAS_TIGER) + #include <botan/tiger.h> +#endif + +#if defined(BOTAN_HAS_WHIRLPOOL) + #include <botan/whrlpool.h> +#endif + +#if defined(BOTAN_HAS_PARALLEL_HASH) + #include <botan/par_hash.h> +#endif + +namespace Botan { + +/* +* Look for an algorithm with this name +*/ +HashFunction* +Default_Engine::find_hash(const SCAN_Name& request, + Algorithm_Factory& af) const + { +#if defined(BOTAN_HAS_ADLER32) + if(request.algo_name() == "Adler32") + return new Adler32; +#endif + +#if defined(BOTAN_HAS_CRC24) + if(request.algo_name() == "CRC24") + return new CRC24; +#endif + +#if defined(BOTAN_HAS_CRC32) + if(request.algo_name() == "CRC32") + return new CRC32; +#endif + +#if defined(BOTAN_HAS_FORK_256) + if(request.algo_name() == "FORK-256") + return new FORK_256; +#endif + +#if defined(BOTAN_HAS_GOST_34_11) + if(request.algo_name() == "GOST-34.11") + return new GOST_34_11; +#endif + +#if defined(BOTAN_HAS_HAS_160) + if(request.algo_name() == "HAS-160") + return new HAS_160; +#endif + +#if defined(BOTAN_HAS_MD2) + if(request.algo_name() == "MD2") + return new MD2; +#endif + +#if defined(BOTAN_HAS_MD4) + if(request.algo_name() == "MD4") + return new MD4; +#endif + +#if defined(BOTAN_HAS_MD5) + if(request.algo_name() == "MD5") + return new MD5; +#endif + +#if defined(BOTAN_HAS_RIPEMD_128) + if(request.algo_name() == "RIPEMD-128") + return new RIPEMD_128; +#endif + +#if defined(BOTAN_HAS_RIPEMD_160) + if(request.algo_name() == "RIPEMD-160") + return new RIPEMD_160; +#endif + +#if defined(BOTAN_HAS_SHA1) + if(request.algo_name() == "SHA-160") + return new SHA_160; +#endif + +#if defined(BOTAN_HAS_SHA2) + if(request.algo_name() == "SHA-224") + return new SHA_224; + if(request.algo_name() == "SHA-256") + return new SHA_256; + if(request.algo_name() == "SHA-384") + return new SHA_384; + if(request.algo_name() == "SHA-512") + return new SHA_512; +#endif + +#if defined(BOTAN_HAS_TIGER) + if(request.algo_name() == "Tiger") + return new Tiger(request.arg_as_u32bit(0, 24), // hash output + request.arg_as_u32bit(1, 3)); // # passes +#endif + +#if defined(BOTAN_HAS_SKEIN_512) + if(request.algo_name() == "Skein-512") + return new Skein_512(request.arg_as_u32bit(0, 512), + request.arg(1, "")); +#endif + +#if defined(BOTAN_HAS_WHIRLPOOL) + if(request.algo_name() == "Whirlpool") + return new Whirlpool; +#endif + +#if defined(BOTAN_HAS_PARALLEL_HASH) + + if(request.algo_name() == "Parallel") + { + std::vector<const HashFunction*> hash_prototypes; + + /* First pass, just get the prototypes (no memory allocation). Then + if all were found, replace each prototype with a newly created clone + */ + for(size_t i = 0; i != request.arg_count(); ++i) + { + const HashFunction* hash = af.prototype_hash_function(request.arg(i)); + if(!hash) + return 0; + + hash_prototypes.push_back(hash); + } + + std::vector<HashFunction*> hashes; + for(size_t i = 0; i != hash_prototypes.size(); ++i) + hashes.push_back(hash_prototypes[i]->clone()); + + return new Parallel(hashes); + } + +#endif + + return 0; + } + +} diff --git a/botan/src/engine/def_engine/lookup_mac.cpp b/botan/src/engine/def_engine/lookup_mac.cpp new file mode 100644 index 0000000..3fef12b --- /dev/null +++ b/botan/src/engine/def_engine/lookup_mac.cpp @@ -0,0 +1,70 @@ +/* +* MAC Lookup +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/def_eng.h> +#include <botan/scan_name.h> +#include <botan/algo_factory.h> + +#if defined(BOTAN_HAS_CBC_MAC) + #include <botan/cbc_mac.h> +#endif + +#if defined(BOTAN_HAS_CMAC) + #include <botan/cmac.h> +#endif + +#if defined(BOTAN_HAS_HMAC) + #include <botan/hmac.h> +#endif + +#if defined(BOTAN_HAS_SSL3_MAC) + #include <botan/ssl3_mac.h> +#endif + +#if defined(BOTAN_HAS_ANSI_X919_MAC) + #include <botan/x919_mac.h> +#endif + +namespace Botan { + +/* +* Look for an algorithm with this name +*/ +MessageAuthenticationCode* +Default_Engine::find_mac(const SCAN_Name& request, + Algorithm_Factory& af) const + { + +#if defined(BOTAN_HAS_CBC_MAC) + if(request.algo_name() == "CBC-MAC" && request.arg_count() == 1) + return new CBC_MAC(af.make_block_cipher(request.arg(0))); +#endif + +#if defined(BOTAN_HAS_CMAC) + if(request.algo_name() == "CMAC" && request.arg_count() == 1) + return new CMAC(af.make_block_cipher(request.arg(0))); +#endif + +#if defined(BOTAN_HAS_HMAC) + if(request.algo_name() == "HMAC" && request.arg_count() == 1) + return new HMAC(af.make_hash_function(request.arg(0))); +#endif + +#if defined(BOTAN_HAS_SSL3_MAC) + if(request.algo_name() == "SSL3-MAC" && request.arg_count() == 1) + return new SSL3_MAC(af.make_hash_function(request.arg(0))); +#endif + +#if defined(BOTAN_HAS_ANSI_X919_MAC) + if(request.algo_name() == "X9.19-MAC" && request.arg_count() == 0) + return new ANSI_X919_MAC(af.make_block_cipher("DES")); +#endif + + return 0; + } + +} diff --git a/botan/src/engine/def_engine/lookup_stream.cpp b/botan/src/engine/def_engine/lookup_stream.cpp new file mode 100644 index 0000000..e2f1b32 --- /dev/null +++ b/botan/src/engine/def_engine/lookup_stream.cpp @@ -0,0 +1,61 @@ +/* +* Stream Cipher Lookup +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/def_eng.h> +#include <botan/scan_name.h> + +#if defined(BOTAN_HAS_ARC4) + #include <botan/arc4.h> +#endif + +#if defined(BOTAN_HAS_SALSA20) + #include <botan/salsa20.h> +#endif + +#if defined(BOTAN_HAS_TURING) + #include <botan/turing.h> +#endif + +#if defined(BOTAN_HAS_WID_WAKE) + #include <botan/wid_wake.h> +#endif + +namespace Botan { + +/* +* Look for an algorithm with this name +*/ +StreamCipher* +Default_Engine::find_stream_cipher(const SCAN_Name& request, + Algorithm_Factory&) const + { +#if defined(BOTAN_HAS_ARC4) + if(request.algo_name() == "ARC4") + return new ARC4(request.arg_as_u32bit(0, 0)); + if(request.algo_name() == "RC4_drop") + return new ARC4(768); +#endif + +#if defined(BOTAN_HAS_SALSA20) + if(request.algo_name() == "Salsa20") + return new Salsa20; +#endif + +#if defined(BOTAN_HAS_TURING) + if(request.algo_name() == "Turing") + return new Turing; +#endif + +#if defined(BOTAN_HAS_WID_WAKE) + if(request.algo_name() == "WiderWake4+1-BE") + return new WiderWake_41_BE; +#endif + + return 0; + } + +} diff --git a/botan/src/engine/engine.h b/botan/src/engine/engine.h new file mode 100644 index 0000000..66a159e --- /dev/null +++ b/botan/src/engine/engine.h @@ -0,0 +1,140 @@ +/* +* Engine +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ENGINE_H__ +#define BOTAN_ENGINE_H__ + +#include <botan/scan_name.h> + +#include <botan/block_cipher.h> +#include <botan/stream_cipher.h> +#include <botan/hash.h> +#include <botan/mac.h> +#include <botan/pow_mod.h> + +#include <utility> +#include <map> + +#if defined(BOTAN_HAS_IF_PUBLIC_KEY_FAMILY) + #include <botan/if_op.h> +#endif + +#if defined(BOTAN_HAS_DSA) + #include <botan/dsa_op.h> +#endif + +#if defined(BOTAN_HAS_DIFFIE_HELLMAN) + #include <botan/dh_op.h> +#endif + +#if defined(BOTAN_HAS_NYBERG_RUEPPEL) + #include <botan/nr_op.h> +#endif + +#if defined(BOTAN_HAS_ELGAMAL) + #include <botan/elg_op.h> +#endif + +#if defined(BOTAN_HAS_ECDSA) + #include <botan/ecdsa_op.h> + #include <botan/ec_dompar.h> +#endif + +#if defined(BOTAN_HAS_ECKAEG) + #include <botan/eckaeg_op.h> + #include <botan/ec_dompar.h> +#endif + +namespace Botan { + +class Algorithm_Factory; +class Keyed_Filter; + +/* +* Engine Base Class +*/ +class BOTAN_DLL Engine + { + public: + virtual ~Engine() {} + + virtual std::string provider_name() const = 0; + + // Lookup functions + virtual BlockCipher* + find_block_cipher(const SCAN_Name&, Algorithm_Factory&) const + { return 0; } + + virtual StreamCipher* + find_stream_cipher(const SCAN_Name&, Algorithm_Factory&) const + { return 0; } + + virtual HashFunction* + find_hash(const SCAN_Name&, Algorithm_Factory&) const + { return 0; } + + virtual MessageAuthenticationCode* + find_mac(const SCAN_Name&, Algorithm_Factory&) const + { return 0; } + + virtual Modular_Exponentiator* + mod_exp(const BigInt&, Power_Mod::Usage_Hints) const + { return 0; } + + virtual Keyed_Filter* get_cipher(const std::string&, + Cipher_Dir, + Algorithm_Factory&) + { return 0; } + +#if defined(BOTAN_HAS_IF_PUBLIC_KEY_FAMILY) + virtual IF_Operation* if_op(const BigInt&, const BigInt&, const BigInt&, + const BigInt&, const BigInt&, const BigInt&, + const BigInt&, const BigInt&) const + { return 0; } +#endif + +#if defined(BOTAN_HAS_DSA) + virtual DSA_Operation* dsa_op(const DL_Group&, const BigInt&, + const BigInt&) const + { return 0; } +#endif + +#if defined(BOTAN_HAS_NYBERG_RUEPPEL) + virtual NR_Operation* nr_op(const DL_Group&, const BigInt&, + const BigInt&) const + { return 0; } +#endif + +#if defined(BOTAN_HAS_ELGAMAL) + virtual ELG_Operation* elg_op(const DL_Group&, const BigInt&, + const BigInt&) const + { return 0; } +#endif + +#if defined(BOTAN_HAS_DIFFIE_HELLMAN) + virtual DH_Operation* dh_op(const DL_Group&, const BigInt&) const + { return 0; } +#endif + +#if defined(BOTAN_HAS_ECDSA) + virtual ECDSA_Operation* ecdsa_op(const EC_Domain_Params&, + const BigInt&, + const PointGFp&) const + { return 0; } +#endif + +#if defined(BOTAN_HAS_ECKAEG) + virtual ECKAEG_Operation* eckaeg_op(const EC_Domain_Params&, + const BigInt&, + const PointGFp&) const + { return 0; } +#endif + }; + +} + +#endif diff --git a/botan/src/engine/gnump/eng_gmp.h b/botan/src/engine/gnump/eng_gmp.h new file mode 100644 index 0000000..6a52b7e --- /dev/null +++ b/botan/src/engine/gnump/eng_gmp.h @@ -0,0 +1,57 @@ +/* +* GMP Engine +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ENGINE_GMP_H__ +#define BOTAN_ENGINE_GMP_H__ + +#include <botan/engine.h> + +namespace Botan { + +/* +* GMP Engine +*/ +class BOTAN_DLL GMP_Engine : public Engine + { + public: + std::string provider_name() const { return "gmp"; } + +#if defined(BOTAN_HAS_IF_PUBLIC_KEY_FAMILY) + IF_Operation* if_op(const BigInt&, const BigInt&, const BigInt&, + const BigInt&, const BigInt&, const BigInt&, + const BigInt&, const BigInt&) const; +#endif + +#if defined(BOTAN_HAS_DSA) + DSA_Operation* dsa_op(const DL_Group&, const BigInt&, + const BigInt&) const; +#endif + +#if defined(BOTAN_HAS_NYBERG_RUEPPEL) + NR_Operation* nr_op(const DL_Group&, const BigInt&, const BigInt&) const; +#endif + +#if defined(BOTAN_HAS_ELGAMAL) + ELG_Operation* elg_op(const DL_Group&, const BigInt&, + const BigInt&) const; +#endif + +#if defined(BOTAN_HAS_DIFFIE_HELLMAN) + DH_Operation* dh_op(const DL_Group&, const BigInt&) const; +#endif + + Modular_Exponentiator* mod_exp(const BigInt&, + Power_Mod::Usage_Hints) const; + + GMP_Engine(); + private: + static void set_memory_hooks(); + }; + +} + +#endif diff --git a/botan/src/engine/gnump/gmp_dh.cpp b/botan/src/engine/gnump/gmp_dh.cpp new file mode 100644 index 0000000..b332402 --- /dev/null +++ b/botan/src/engine/gnump/gmp_dh.cpp @@ -0,0 +1,53 @@ +/* +* GMP Engine +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/eng_gmp.h> +#include <botan/gmp_wrap.h> +#include <gmp.h> + +namespace Botan { + +#if defined(BOTAN_HAS_DIFFIE_HELLMAN) +namespace { + +/* +* GMP DH Operation +*/ +class GMP_DH_Op : public DH_Operation + { + public: + BigInt agree(const BigInt& i) const; + DH_Operation* clone() const { return new GMP_DH_Op(*this); } + + GMP_DH_Op(const DL_Group& group, const BigInt& x_bn) : + x(x_bn), p(group.get_p()) {} + private: + GMP_MPZ x, p; + }; + +/* +* GMP DH Key Agreement Operation +*/ +BigInt GMP_DH_Op::agree(const BigInt& i_bn) const + { + GMP_MPZ i(i_bn); + mpz_powm(i.value, i.value, x.value, p.value); + return i.to_bigint(); + } + +} + +/* +* Acquire a DH op +*/ +DH_Operation* GMP_Engine::dh_op(const DL_Group& group, const BigInt& x) const + { + return new GMP_DH_Op(group, x); + } +#endif + +} diff --git a/botan/src/engine/gnump/gmp_dsa.cpp b/botan/src/engine/gnump/gmp_dsa.cpp new file mode 100644 index 0000000..69a9c3e --- /dev/null +++ b/botan/src/engine/gnump/gmp_dsa.cpp @@ -0,0 +1,124 @@ +/* +* GMP DSA Engine +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/eng_gmp.h> +#include <botan/gmp_wrap.h> +#include <gmp.h> + +namespace Botan { + +#if defined(BOTAN_HAS_DSA) + +namespace { + +/* +* GMP DSA Operation +*/ +class GMP_DSA_Op : public DSA_Operation + { + public: + bool verify(const byte[], u32bit, const byte[], u32bit) const; + SecureVector<byte> sign(const byte[], u32bit, const BigInt&) const; + + DSA_Operation* clone() const { return new GMP_DSA_Op(*this); } + + GMP_DSA_Op(const DL_Group& group, const BigInt& y1, const BigInt& x1) : + x(x1), y(y1), p(group.get_p()), q(group.get_q()), g(group.get_g()) {} + private: + const GMP_MPZ x, y, p, q, g; + }; + +/* +* GMP DSA Verify Operation +*/ +bool GMP_DSA_Op::verify(const byte msg[], u32bit msg_len, + const byte sig[], u32bit sig_len) const + { + const u32bit q_bytes = q.bytes(); + + if(sig_len != 2*q_bytes || msg_len > q_bytes) + return false; + + GMP_MPZ r(sig, q_bytes); + GMP_MPZ s(sig + q_bytes, q_bytes); + GMP_MPZ i(msg, msg_len); + + if(mpz_cmp_ui(r.value, 0) <= 0 || mpz_cmp(r.value, q.value) >= 0) + return false; + if(mpz_cmp_ui(s.value, 0) <= 0 || mpz_cmp(s.value, q.value) >= 0) + return false; + + if(mpz_invert(s.value, s.value, q.value) == 0) + return false; + + GMP_MPZ si; + mpz_mul(si.value, s.value, i.value); + mpz_mod(si.value, si.value, q.value); + mpz_powm(si.value, g.value, si.value, p.value); + + GMP_MPZ sr; + mpz_mul(sr.value, s.value, r.value); + mpz_mod(sr.value, sr.value, q.value); + mpz_powm(sr.value, y.value, sr.value, p.value); + + mpz_mul(si.value, si.value, sr.value); + mpz_mod(si.value, si.value, p.value); + mpz_mod(si.value, si.value, q.value); + + if(mpz_cmp(si.value, r.value) == 0) + return true; + return false; + } + +/* +* GMP DSA Sign Operation +*/ +SecureVector<byte> GMP_DSA_Op::sign(const byte in[], u32bit length, + const BigInt& k_bn) const + { + if(mpz_cmp_ui(x.value, 0) == 0) + throw Internal_Error("GMP_DSA_Op::sign: No private key"); + + GMP_MPZ i(in, length); + GMP_MPZ k(k_bn); + + GMP_MPZ r; + mpz_powm(r.value, g.value, k.value, p.value); + mpz_mod(r.value, r.value, q.value); + + mpz_invert(k.value, k.value, q.value); + + GMP_MPZ s; + mpz_mul(s.value, x.value, r.value); + mpz_add(s.value, s.value, i.value); + mpz_mul(s.value, s.value, k.value); + mpz_mod(s.value, s.value, q.value); + + if(mpz_cmp_ui(r.value, 0) == 0 || mpz_cmp_ui(s.value, 0) == 0) + throw Internal_Error("GMP_DSA_Op::sign: r or s was zero"); + + const u32bit q_bytes = q.bytes(); + + SecureVector<byte> output(2*q_bytes); + r.encode(output, q_bytes); + s.encode(output + q_bytes, q_bytes); + return output; + } + +} + +/* +* Acquire a DSA op +*/ +DSA_Operation* GMP_Engine::dsa_op(const DL_Group& group, const BigInt& y, + const BigInt& x) const + { + return new GMP_DSA_Op(group, y, x); + } +#endif + +} diff --git a/botan/src/engine/gnump/gmp_elg.cpp b/botan/src/engine/gnump/gmp_elg.cpp new file mode 100644 index 0000000..ee109f1 --- /dev/null +++ b/botan/src/engine/gnump/gmp_elg.cpp @@ -0,0 +1,92 @@ +/* +* GMP ElGamal Engine +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/eng_gmp.h> +#include <botan/gmp_wrap.h> +#include <gmp.h> + +namespace Botan { + +#if defined(BOTAN_HAS_ELGAMAL) + +namespace { + +/* +* GMP ElGamal Operation +*/ +class GMP_ELG_Op : public ELG_Operation + { + public: + SecureVector<byte> encrypt(const byte[], u32bit, const BigInt&) const; + BigInt decrypt(const BigInt&, const BigInt&) const; + + ELG_Operation* clone() const { return new GMP_ELG_Op(*this); } + + GMP_ELG_Op(const DL_Group& group, const BigInt& y1, const BigInt& x1) : + x(x1), y(y1), g(group.get_g()), p(group.get_p()) {} + private: + GMP_MPZ x, y, g, p; + }; + +/* +* GMP ElGamal Encrypt Operation +*/ +SecureVector<byte> GMP_ELG_Op::encrypt(const byte in[], u32bit length, + const BigInt& k_bn) const + { + GMP_MPZ i(in, length); + + if(mpz_cmp(i.value, p.value) >= 0) + throw Invalid_Argument("GMP_ELG_Op: Input is too large"); + + GMP_MPZ a, b, k(k_bn); + + mpz_powm(a.value, g.value, k.value, p.value); + mpz_powm(b.value, y.value, k.value, p.value); + mpz_mul(b.value, b.value, i.value); + mpz_mod(b.value, b.value, p.value); + + const u32bit p_bytes = p.bytes(); + SecureVector<byte> output(2*p_bytes); + a.encode(output, p_bytes); + b.encode(output + p_bytes, p_bytes); + return output; + } + +/* +* GMP ElGamal Decrypt Operation +*/ +BigInt GMP_ELG_Op::decrypt(const BigInt& a_bn, const BigInt& b_bn) const + { + if(mpz_cmp_ui(x.value, 0) == 0) + throw Internal_Error("GMP_ELG_Op::decrypt: No private key"); + + GMP_MPZ a(a_bn), b(b_bn); + + if(mpz_cmp(a.value, p.value) >= 0 || mpz_cmp(b.value, p.value) >= 0) + throw Invalid_Argument("GMP_ELG_Op: Invalid message"); + + mpz_powm(a.value, a.value, x.value, p.value); + mpz_invert(a.value, a.value, p.value); + mpz_mul(a.value, a.value, b.value); + mpz_mod(a.value, a.value, p.value); + return a.to_bigint(); + } + +} + +/* +* Acquire an ElGamal op +*/ +ELG_Operation* GMP_Engine::elg_op(const DL_Group& group, const BigInt& y, + const BigInt& x) const + { + return new GMP_ELG_Op(group, y, x); + } +#endif + +} diff --git a/botan/src/engine/gnump/gmp_if.cpp b/botan/src/engine/gnump/gmp_if.cpp new file mode 100644 index 0000000..b96f2dd --- /dev/null +++ b/botan/src/engine/gnump/gmp_if.cpp @@ -0,0 +1,81 @@ +/* +* GMP IF Engine +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/eng_gmp.h> +#include <botan/gmp_wrap.h> +#include <gmp.h> + +namespace Botan { + +#if defined(BOTAN_HAS_IF_PUBLIC_KEY_FAMILY) + +namespace { + +/* +* GMP IF Operation +*/ +class GMP_IF_Op : public IF_Operation + { + public: + BigInt public_op(const BigInt&) const; + BigInt private_op(const BigInt&) const; + + IF_Operation* clone() const { return new GMP_IF_Op(*this); } + + GMP_IF_Op(const BigInt& e_bn, const BigInt& n_bn, const BigInt&, + const BigInt& p_bn, const BigInt& q_bn, const BigInt& d1_bn, + const BigInt& d2_bn, const BigInt& c_bn) : + e(e_bn), n(n_bn), p(p_bn), q(q_bn), d1(d1_bn), d2(d2_bn), c(c_bn) {} + private: + const GMP_MPZ e, n, p, q, d1, d2, c; + }; + +/* +* GMP IF Public Operation +*/ +BigInt GMP_IF_Op::public_op(const BigInt& i_bn) const + { + GMP_MPZ i(i_bn); + mpz_powm(i.value, i.value, e.value, n.value); + return i.to_bigint(); + } + +/* +* GMP IF Private Operation +*/ +BigInt GMP_IF_Op::private_op(const BigInt& i_bn) const + { + if(mpz_cmp_ui(p.value, 0) == 0) + throw Internal_Error("GMP_IF_Op::private_op: No private key"); + + GMP_MPZ j1, j2, h(i_bn); + + mpz_powm(j1.value, h.value, d1.value, p.value); + mpz_powm(j2.value, h.value, d2.value, q.value); + mpz_sub(h.value, j1.value, j2.value); + mpz_mul(h.value, h.value, c.value); + mpz_mod(h.value, h.value, p.value); + mpz_mul(h.value, h.value, q.value); + mpz_add(h.value, h.value, j2.value); + return h.to_bigint(); + } + +} + +/* +* Acquire an IF op +*/ +IF_Operation* GMP_Engine::if_op(const BigInt& e, const BigInt& n, + const BigInt& d, const BigInt& p, + const BigInt& q, const BigInt& d1, + const BigInt& d2, const BigInt& c) const + { + return new GMP_IF_Op(e, n, d, p, q, d1, d2, c); + } +#endif + +} diff --git a/botan/src/engine/gnump/gmp_mem.cpp b/botan/src/engine/gnump/gmp_mem.cpp new file mode 100644 index 0000000..89a1ed2 --- /dev/null +++ b/botan/src/engine/gnump/gmp_mem.cpp @@ -0,0 +1,70 @@ +/* +* GNU MP Memory Handlers +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/eng_gmp.h> +#include <cstring> +#include <gmp.h> + +namespace Botan { + +namespace { + +/* +* Allocator used by GNU MP +*/ +Allocator* gmp_alloc = 0; + +/* +* Allocation Function for GNU MP +*/ +void* gmp_malloc(size_t n) + { + return gmp_alloc->allocate(n); + } + +/* +* Reallocation Function for GNU MP +*/ +void* gmp_realloc(void* ptr, size_t old_n, size_t new_n) + { + void* new_buf = gmp_alloc->allocate(new_n); + std::memcpy(new_buf, ptr, std::min(old_n, new_n)); + gmp_alloc->deallocate(ptr, old_n); + return new_buf; + } + +/* +* Deallocation Function for GNU MP +*/ +void gmp_free(void* ptr, size_t n) + { + gmp_alloc->deallocate(ptr, n); + } + +} + +/* +* Set the GNU MP memory functions +*/ +void GMP_Engine::set_memory_hooks() + { + if(gmp_alloc == 0) + { + gmp_alloc = Allocator::get(true); + mp_set_memory_functions(gmp_malloc, gmp_realloc, gmp_free); + } + } + +/* +* GMP_Engine Constructor +*/ +GMP_Engine::GMP_Engine() + { + set_memory_hooks(); + } + +} diff --git a/botan/src/engine/gnump/gmp_nr.cpp b/botan/src/engine/gnump/gmp_nr.cpp new file mode 100644 index 0000000..4aeb09f --- /dev/null +++ b/botan/src/engine/gnump/gmp_nr.cpp @@ -0,0 +1,107 @@ +/* +* GMP NR Engine +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/eng_gmp.h> +#include <botan/gmp_wrap.h> +#include <gmp.h> + +namespace Botan { + +#if defined(BOTAN_HAS_NYBERG_RUEPPEL) + +namespace { + +/* +* GMP NR Operation +*/ +class GMP_NR_Op : public NR_Operation + { + public: + SecureVector<byte> verify(const byte[], u32bit) const; + SecureVector<byte> sign(const byte[], u32bit, const BigInt&) const; + + NR_Operation* clone() const { return new GMP_NR_Op(*this); } + + GMP_NR_Op(const DL_Group& group, const BigInt& y1, const BigInt& x1) : + x(x1), y(y1), p(group.get_p()), q(group.get_q()), g(group.get_g()) {} + private: + const GMP_MPZ x, y, p, q, g; + }; + +/* +* GMP NR Verify Operation +*/ +SecureVector<byte> GMP_NR_Op::verify(const byte sig[], u32bit sig_len) const + { + const u32bit q_bytes = q.bytes(); + + if(sig_len != 2*q_bytes) + return false; + + GMP_MPZ c(sig, q_bytes); + GMP_MPZ d(sig + q_bytes, q_bytes); + + if(mpz_cmp_ui(c.value, 0) <= 0 || mpz_cmp(c.value, q.value) >= 0 || + mpz_cmp(d.value, q.value) >= 0) + throw Invalid_Argument("GMP_NR_Op::verify: Invalid signature"); + + GMP_MPZ i1, i2; + mpz_powm(i1.value, g.value, d.value, p.value); + mpz_powm(i2.value, y.value, c.value, p.value); + mpz_mul(i1.value, i1.value, i2.value); + mpz_mod(i1.value, i1.value, p.value); + mpz_sub(i1.value, c.value, i1.value); + mpz_mod(i1.value, i1.value, q.value); + return BigInt::encode(i1.to_bigint()); + } + +/* +* GMP NR Sign Operation +*/ +SecureVector<byte> GMP_NR_Op::sign(const byte in[], u32bit length, + const BigInt& k_bn) const + { + if(mpz_cmp_ui(x.value, 0) == 0) + throw Internal_Error("GMP_NR_Op::sign: No private key"); + + GMP_MPZ f(in, length); + GMP_MPZ k(k_bn); + + if(mpz_cmp(f.value, q.value) >= 0) + throw Invalid_Argument("GMP_NR_Op::sign: Input is out of range"); + + GMP_MPZ c, d; + mpz_powm(c.value, g.value, k.value, p.value); + mpz_add(c.value, c.value, f.value); + mpz_mod(c.value, c.value, q.value); + mpz_mul(d.value, x.value, c.value); + mpz_sub(d.value, k.value, d.value); + mpz_mod(d.value, d.value, q.value); + + if(mpz_cmp_ui(c.value, 0) == 0) + throw Internal_Error("Default_NR_Op::sign: c was zero"); + + const u32bit q_bytes = q.bytes(); + SecureVector<byte> output(2*q_bytes); + c.encode(output, q_bytes); + d.encode(output + q_bytes, q_bytes); + return output; + } + +} + +/* +* Acquire a NR op +*/ +NR_Operation* GMP_Engine::nr_op(const DL_Group& group, const BigInt& y, + const BigInt& x) const + { + return new GMP_NR_Op(group, y, x); + } +#endif + +} diff --git a/botan/src/engine/gnump/gmp_powm.cpp b/botan/src/engine/gnump/gmp_powm.cpp new file mode 100644 index 0000000..687aed8 --- /dev/null +++ b/botan/src/engine/gnump/gmp_powm.cpp @@ -0,0 +1,53 @@ +/* +* GMP Modular Exponentiation +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/eng_gmp.h> +#include <botan/gmp_wrap.h> + +namespace Botan { + +namespace { + +/* +* GMP Modular Exponentiator +*/ +class GMP_Modular_Exponentiator : public Modular_Exponentiator + { + public: + void set_base(const BigInt& b) { base = b; } + void set_exponent(const BigInt& e) { exp = e; } + BigInt execute() const; + Modular_Exponentiator* copy() const + { return new GMP_Modular_Exponentiator(*this); } + + GMP_Modular_Exponentiator(const BigInt& n) : mod(n) {} + private: + GMP_MPZ base, exp, mod; + }; + +/* +* Compute the result +*/ +BigInt GMP_Modular_Exponentiator::execute() const + { + GMP_MPZ r; + mpz_powm(r.value, base.value, exp.value, mod.value); + return r.to_bigint(); + } + +} + +/* +* Return the GMP-based modular exponentiator +*/ +Modular_Exponentiator* GMP_Engine::mod_exp(const BigInt& n, + Power_Mod::Usage_Hints) const + { + return new GMP_Modular_Exponentiator(n); + } + +} diff --git a/botan/src/engine/gnump/gmp_wrap.cpp b/botan/src/engine/gnump/gmp_wrap.cpp new file mode 100644 index 0000000..735fc70 --- /dev/null +++ b/botan/src/engine/gnump/gmp_wrap.cpp @@ -0,0 +1,98 @@ +/* +* GMP Wrapper +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/gmp_wrap.h> + +#define GNU_MP_VERSION_CODE_FOR(a,b,c) ((a << 16) | (b << 8) | (c)) + +#define GNU_MP_VERSION_CODE \ + GNU_MP_VERSION_CODE_FOR(__GNU_MP_VERSION, __GNU_MP_VERSION_MINOR, \ + __GNU_MP_VERSION_PATCHLEVEL) + +#if GNU_MP_VERSION_CODE < GNU_MP_VERSION_CODE_FOR(4,1,0) + #error Your GNU MP install is too old, upgrade to 4.1 or later +#endif + +namespace Botan { + +/* +* GMP_MPZ Constructor +*/ +GMP_MPZ::GMP_MPZ(const BigInt& in) + { + mpz_init(value); + if(in != 0) + mpz_import(value, in.sig_words(), -1, sizeof(word), 0, 0, in.data()); + } + +/* +* GMP_MPZ Constructor +*/ +GMP_MPZ::GMP_MPZ(const byte in[], u32bit length) + { + mpz_init(value); + mpz_import(value, length, 1, 1, 0, 0, in); + } + +/* +* GMP_MPZ Copy Constructor +*/ +GMP_MPZ::GMP_MPZ(const GMP_MPZ& other) + { + mpz_init_set(value, other.value); + } + +/* +* GMP_MPZ Destructor +*/ +GMP_MPZ::~GMP_MPZ() + { + mpz_clear(value); + } + +/* +* GMP_MPZ Assignment Operator +*/ +GMP_MPZ& GMP_MPZ::operator=(const GMP_MPZ& other) + { + mpz_set(value, other.value); + return (*this); + } + +/* +* Export the mpz_t as a bytestring +*/ +void GMP_MPZ::encode(byte out[], u32bit length) const + { + size_t dummy = 0; + mpz_export(out + (length - bytes()), &dummy, 1, 1, 0, 0, value); + } + +/* +* Return the number of significant bytes +*/ +u32bit GMP_MPZ::bytes() const + { + return ((mpz_sizeinbase(value, 2) + 7) / 8); + } + +/* +* GMP to BigInt Conversions +*/ +BigInt GMP_MPZ::to_bigint() const + { + BigInt out(BigInt::Positive, (bytes() + sizeof(word) - 1) / sizeof(word)); + size_t dummy = 0; + mpz_export(out.get_reg(), &dummy, -1, sizeof(word), 0, 0, value); + + if(mpz_sgn(value) < 0) + out.flip_sign(); + + return out; + } + +} diff --git a/botan/src/engine/gnump/gmp_wrap.h b/botan/src/engine/gnump/gmp_wrap.h new file mode 100644 index 0000000..11a51c8 --- /dev/null +++ b/botan/src/engine/gnump/gmp_wrap.h @@ -0,0 +1,38 @@ +/* +* GMP MPZ Wrapper +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_GMP_MPZ_WRAP_H__ +#define BOTAN_GMP_MPZ_WRAP_H__ + +#include <botan/bigint.h> +#include <gmp.h> + +namespace Botan { + +/* +* Lightweight GMP mpz_t Wrapper +*/ +class BOTAN_DLL GMP_MPZ + { + public: + mpz_t value; + + BigInt to_bigint() const; + void encode(byte[], u32bit) const; + u32bit bytes() const; + + GMP_MPZ& operator=(const GMP_MPZ&); + + GMP_MPZ(const GMP_MPZ&); + GMP_MPZ(const BigInt& = 0); + GMP_MPZ(const byte[], u32bit); + ~GMP_MPZ(); + }; + +} + +#endif diff --git a/botan/src/engine/gnump/info.txt b/botan/src/engine/gnump/info.txt new file mode 100644 index 0000000..67a9bcd --- /dev/null +++ b/botan/src/engine/gnump/info.txt @@ -0,0 +1,26 @@ +realname "GMP Engine" + +define ENGINE_GNU_MP + +load_on request + +<libs> +all -> gmp +</libs> + +<add> +eng_gmp.h +gmp_dh.cpp +gmp_dsa.cpp +gmp_elg.cpp +gmp_if.cpp +gmp_mem.cpp +gmp_nr.cpp +gmp_powm.cpp +gmp_wrap.cpp +gmp_wrap.h +</add> + +<requires> +bigint +</requires> diff --git a/botan/src/engine/ia32_eng/eng_ia32.cpp b/botan/src/engine/ia32_eng/eng_ia32.cpp new file mode 100644 index 0000000..6ff2a4b --- /dev/null +++ b/botan/src/engine/ia32_eng/eng_ia32.cpp @@ -0,0 +1,62 @@ +/* +* Assembly Implementation Engine +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/eng_ia32.h> + +#if defined(BOTAN_HAS_SERPENT_IA32) + #include <botan/serp_ia32.h> +#endif + +#if defined(BOTAN_HAS_MD4_IA32) + #include <botan/md4_ia32.h> +#endif + +#if defined(BOTAN_HAS_MD5_IA32) + #include <botan/md5_ia32.h> +#endif + +#if defined(BOTAN_HAS_SHA1_IA32) + #include <botan/sha1_ia32.h> +#endif + +namespace Botan { + +BlockCipher* +IA32_Assembler_Engine::find_block_cipher(const SCAN_Name& request, + Algorithm_Factory&) const + { +#if defined(BOTAN_HAS_SERPENT_IA32) + if(request.algo_name() == "Serpent") + return new Serpent_IA32; +#endif + + return 0; + } + +HashFunction* +IA32_Assembler_Engine::find_hash(const SCAN_Name& request, + Algorithm_Factory&) const + { +#if defined(BOTAN_HAS_MD4_IA32) + if(request.algo_name() == "MD4") + return new MD4_IA32; +#endif + +#if defined(BOTAN_HAS_MD5_IA32) + if(request.algo_name() == "MD5") + return new MD5_IA32; +#endif + +#if defined(BOTAN_HAS_SHA1_IA32) + if(request.algo_name() == "SHA-160") + return new SHA_160_IA32; +#endif + + return 0; + } + +} diff --git a/botan/src/engine/ia32_eng/eng_ia32.h b/botan/src/engine/ia32_eng/eng_ia32.h new file mode 100644 index 0000000..b7cb482 --- /dev/null +++ b/botan/src/engine/ia32_eng/eng_ia32.h @@ -0,0 +1,29 @@ +/** +* IA-32 Assembly Implementation Engines +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_IA32_ASM_ENGINE_H__ +#define BOTAN_IA32_ASM_ENGINE_H__ + +#include <botan/engine.h> + +namespace Botan { + +class BOTAN_DLL IA32_Assembler_Engine : public Engine + { + public: + std::string provider_name() const { return "ia32"; } + private: + BlockCipher* find_block_cipher(const SCAN_Name&, + Algorithm_Factory&) const; + + HashFunction* find_hash(const SCAN_Name& reqeust, + Algorithm_Factory&) const; + }; + +} + +#endif diff --git a/botan/src/engine/ia32_eng/info.txt b/botan/src/engine/ia32_eng/info.txt new file mode 100644 index 0000000..3bf2a7f --- /dev/null +++ b/botan/src/engine/ia32_eng/info.txt @@ -0,0 +1,10 @@ +realname "IA32 Assembler Engine" + +define ENGINE_IA32_ASSEMBLER + +load_on dep + +<add> +eng_ia32.cpp +eng_ia32.h +</add> diff --git a/botan/src/engine/info.txt b/botan/src/engine/info.txt new file mode 100644 index 0000000..eef3c03 --- /dev/null +++ b/botan/src/engine/info.txt @@ -0,0 +1,18 @@ +realname "Engines" + +define ENGINES + +load_on auto + +<add> +engine.h +</add> + +<requires> +block +hash +libstate +mac +numbertheory +stream +</requires> diff --git a/botan/src/engine/openssl/arc4_openssl.cpp b/botan/src/engine/openssl/arc4_openssl.cpp new file mode 100644 index 0000000..08ed3eb --- /dev/null +++ b/botan/src/engine/openssl/arc4_openssl.cpp @@ -0,0 +1,82 @@ +/* +* OpenSSL ARC4 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/eng_ossl.h> +#include <botan/parsing.h> +#include <openssl/rc4.h> + +namespace Botan { + +namespace { + +/** +* ARC4 as implemented by OpenSSL +*/ +class ARC4_OpenSSL : public StreamCipher + { + public: + void clear() throw() { std::memset(&state, 0, sizeof(state)); } + std::string name() const; + StreamCipher* clone() const { return new ARC4_OpenSSL(SKIP); } + + ARC4_OpenSSL(u32bit s = 0) : StreamCipher(1, 32), SKIP(s) { clear(); } + ~ARC4_OpenSSL() { clear(); } + private: + void cipher(const byte[], byte[], u32bit); + void key_schedule(const byte[], u32bit); + + const u32bit SKIP; + RC4_KEY state; + }; + +/* +* Return the name of this type +*/ +std::string ARC4_OpenSSL::name() const + { + if(SKIP == 0) return "ARC4"; + if(SKIP == 256) return "MARK-4"; + else return "RC4_skip(" + to_string(SKIP) + ")"; + } + +/* +* ARC4 Key Schedule +*/ +void ARC4_OpenSSL::key_schedule(const byte key[], u32bit length) + { + RC4_set_key(&state, length, key); + byte dummy = 0; + for(u32bit j = 0; j != SKIP; j++) + RC4(&state, 1, &dummy, &dummy); + } + +/* +* ARC4 Encryption +*/ +void ARC4_OpenSSL::cipher(const byte in[], byte out[], u32bit length) + { + RC4(&state, length, in, out); + } + +} + +/** +* Look for an OpenSSL-suported stream cipher (ARC4) +*/ +StreamCipher* +OpenSSL_Engine::find_stream_cipher(const SCAN_Name& request, + Algorithm_Factory&) const + { + if(request.algo_name() == "ARC4") + return new ARC4_OpenSSL(request.arg_as_u32bit(0, 0)); + if(request.algo_name() == "RC4_drop") + return new ARC4_OpenSSL(768); + + return 0; + } + +} diff --git a/botan/src/engine/openssl/bn_powm.cpp b/botan/src/engine/openssl/bn_powm.cpp new file mode 100644 index 0000000..7b836d1 --- /dev/null +++ b/botan/src/engine/openssl/bn_powm.cpp @@ -0,0 +1,54 @@ +/* +* OpenSSL Modular Exponentiation +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/eng_ossl.h> +#include <botan/bn_wrap.h> + +namespace Botan { + +namespace { + +/* +* OpenSSL Modular Exponentiator +*/ +class OpenSSL_Modular_Exponentiator : public Modular_Exponentiator + { + public: + void set_base(const BigInt& b) { base = b; } + void set_exponent(const BigInt& e) { exp = e; } + BigInt execute() const; + Modular_Exponentiator* copy() const + { return new OpenSSL_Modular_Exponentiator(*this); } + + OpenSSL_Modular_Exponentiator(const BigInt& n) : mod(n) {} + private: + OSSL_BN base, exp, mod; + OSSL_BN_CTX ctx; + }; + +/* +* Compute the result +*/ +BigInt OpenSSL_Modular_Exponentiator::execute() const + { + OSSL_BN r; + BN_mod_exp(r.value, base.value, exp.value, mod.value, ctx.value); + return r.to_bigint(); + } + +} + +/* +* Return the OpenSSL-based modular exponentiator +*/ +Modular_Exponentiator* OpenSSL_Engine::mod_exp(const BigInt& n, + Power_Mod::Usage_Hints) const + { + return new OpenSSL_Modular_Exponentiator(n); + } + +} diff --git a/botan/src/engine/openssl/bn_wrap.cpp b/botan/src/engine/openssl/bn_wrap.cpp new file mode 100644 index 0000000..e1cfe3f --- /dev/null +++ b/botan/src/engine/openssl/bn_wrap.cpp @@ -0,0 +1,116 @@ +/* +* OpenSSL BN Wrapper +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/bn_wrap.h> + +namespace Botan { + +/* +* OSSL_BN Constructor +*/ +OSSL_BN::OSSL_BN(const BigInt& in) + { + value = BN_new(); + SecureVector<byte> encoding = BigInt::encode(in); + if(in != 0) + BN_bin2bn(encoding, encoding.size(), value); + } + +/* +* OSSL_BN Constructor +*/ +OSSL_BN::OSSL_BN(const byte in[], u32bit length) + { + value = BN_new(); + BN_bin2bn(in, length, value); + } + +/* +* OSSL_BN Copy Constructor +*/ +OSSL_BN::OSSL_BN(const OSSL_BN& other) + { + value = BN_dup(other.value); + } + +/* +* OSSL_BN Destructor +*/ +OSSL_BN::~OSSL_BN() + { + BN_clear_free(value); + } + +/* +* OSSL_BN Assignment Operator +*/ +OSSL_BN& OSSL_BN::operator=(const OSSL_BN& other) + { + BN_copy(value, other.value); + return (*this); + } + +/* +* Export the BIGNUM as a bytestring +*/ +void OSSL_BN::encode(byte out[], u32bit length) const + { + BN_bn2bin(value, out + (length - bytes())); + } + +/* +* Return the number of significant bytes +*/ +u32bit OSSL_BN::bytes() const + { + return BN_num_bytes(value); + } + +/* +* OpenSSL to BigInt Conversions +*/ +BigInt OSSL_BN::to_bigint() const + { + SecureVector<byte> out(bytes()); + BN_bn2bin(value, out); + return BigInt::decode(out); + } + +/* +* OSSL_BN_CTX Constructor +*/ +OSSL_BN_CTX::OSSL_BN_CTX() + { + value = BN_CTX_new(); + } + +/* +* OSSL_BN_CTX Copy Constructor +*/ +OSSL_BN_CTX::OSSL_BN_CTX(const OSSL_BN_CTX&) + { + value = BN_CTX_new(); + } + +/* +* OSSL_BN_CTX Destructor +*/ +OSSL_BN_CTX::~OSSL_BN_CTX() + { + BN_CTX_free(value); + } + +/* +* OSSL_BN_CTX Assignment Operator +*/ +OSSL_BN_CTX& OSSL_BN_CTX::operator=(const OSSL_BN_CTX&) + { + value = BN_CTX_new(); + return (*this); + } + +} diff --git a/botan/src/engine/openssl/bn_wrap.h b/botan/src/engine/openssl/bn_wrap.h new file mode 100644 index 0000000..4d18be1 --- /dev/null +++ b/botan/src/engine/openssl/bn_wrap.h @@ -0,0 +1,53 @@ +/* +* OpenSSL BN Wrapper +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_OPENSSL_BN_WRAP_H__ +#define BOTAN_OPENSSL_BN_WRAP_H__ + +#include <botan/bigint.h> +#include <openssl/bn.h> + +namespace Botan { + +/* +* Lightweight OpenSSL BN Wrapper +*/ +class BOTAN_DLL OSSL_BN + { + public: + BIGNUM* value; + + BigInt to_bigint() const; + void encode(byte[], u32bit) const; + u32bit bytes() const; + + OSSL_BN& operator=(const OSSL_BN&); + + OSSL_BN(const OSSL_BN&); + OSSL_BN(const BigInt& = 0); + OSSL_BN(const byte[], u32bit); + ~OSSL_BN(); + }; + +/* +* Lightweight OpenSSL BN_CTX Wrapper +*/ +class BOTAN_DLL OSSL_BN_CTX + { + public: + BN_CTX* value; + + OSSL_BN_CTX& operator=(const OSSL_BN_CTX&); + + OSSL_BN_CTX(); + OSSL_BN_CTX(const OSSL_BN_CTX&); + ~OSSL_BN_CTX(); + }; + +} + +#endif diff --git a/botan/src/engine/openssl/eng_ossl.h b/botan/src/engine/openssl/eng_ossl.h new file mode 100644 index 0000000..7105546 --- /dev/null +++ b/botan/src/engine/openssl/eng_ossl.h @@ -0,0 +1,64 @@ +/* +* OpenSSL Engine +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ENGINE_OPENSSL_H__ +#define BOTAN_ENGINE_OPENSSL_H__ + +#include <botan/engine.h> + +namespace Botan { + +/* +* OpenSSL Engine +*/ +class BOTAN_DLL OpenSSL_Engine : public Engine + { + public: + /** + * Return the provider name ("openssl") + */ + std::string provider_name() const { return "openssl"; } + +#if defined(BOTAN_HAS_IF_PUBLIC_KEY_FAMILY) + IF_Operation* if_op(const BigInt&, const BigInt&, const BigInt&, + const BigInt&, const BigInt&, const BigInt&, + const BigInt&, const BigInt&) const; +#endif + +#if defined(BOTAN_HAS_DSA) + DSA_Operation* dsa_op(const DL_Group&, const BigInt&, + const BigInt&) const; +#endif + +#if defined(BOTAN_HAS_NYBERG_RUEPPEL) + NR_Operation* nr_op(const DL_Group&, const BigInt&, const BigInt&) const; +#endif + +#if defined(BOTAN_HAS_ELGAMAL) + ELG_Operation* elg_op(const DL_Group&, const BigInt&, + const BigInt&) const; +#endif + +#if defined(BOTAN_HAS_DIFFIE_HELLMAN) + DH_Operation* dh_op(const DL_Group&, const BigInt&) const; +#endif + + Modular_Exponentiator* mod_exp(const BigInt&, + Power_Mod::Usage_Hints) const; + private: + BlockCipher* find_block_cipher(const SCAN_Name&, + Algorithm_Factory&) const; + + StreamCipher* find_stream_cipher(const SCAN_Name&, + Algorithm_Factory&) const; + + HashFunction* find_hash(const SCAN_Name&, Algorithm_Factory&) const; + }; + +} + +#endif diff --git a/botan/src/engine/openssl/info.txt b/botan/src/engine/openssl/info.txt new file mode 100644 index 0000000..3f2f1ab --- /dev/null +++ b/botan/src/engine/openssl/info.txt @@ -0,0 +1,28 @@ +realname "OpenSSL Engine" + +define ENGINE_OPENSSL + +load_on request + +<libs> +all -> crypto +</libs> + +<add> +arc4_openssl.cpp +bn_powm.cpp +bn_wrap.cpp +bn_wrap.h +eng_ossl.h +ossl_bc.cpp +ossl_dh.cpp +ossl_dsa.cpp +ossl_elg.cpp +ossl_if.cpp +ossl_md.cpp +ossl_nr.cpp +</add> + +<requires> +bigint +</requires> diff --git a/botan/src/engine/openssl/ossl_bc.cpp b/botan/src/engine/openssl/ossl_bc.cpp new file mode 100644 index 0000000..4d3761a --- /dev/null +++ b/botan/src/engine/openssl/ossl_bc.cpp @@ -0,0 +1,200 @@ +/* +* OpenSSL Block Cipher +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/eng_ossl.h> +#include <openssl/evp.h> + +namespace Botan { + +namespace { + +/* +* EVP Block Cipher +*/ +class EVP_BlockCipher : public BlockCipher + { + public: + void clear() throw(); + std::string name() const { return cipher_name; } + BlockCipher* clone() const; + EVP_BlockCipher(const EVP_CIPHER*, const std::string&); + EVP_BlockCipher(const EVP_CIPHER*, const std::string&, + u32bit, u32bit, u32bit); + + ~EVP_BlockCipher(); + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key_schedule(const byte[], u32bit); + std::string cipher_name; + mutable EVP_CIPHER_CTX encrypt, decrypt; + }; + +/* +* EVP Block Cipher Constructor +*/ +EVP_BlockCipher::EVP_BlockCipher(const EVP_CIPHER* algo, + const std::string& algo_name) : + BlockCipher(EVP_CIPHER_block_size(algo), EVP_CIPHER_key_length(algo)), + cipher_name(algo_name) + { + if(EVP_CIPHER_mode(algo) != EVP_CIPH_ECB_MODE) + throw Invalid_Argument("EVP_BlockCipher: Non-ECB EVP was passed in"); + + EVP_CIPHER_CTX_init(&encrypt); + EVP_CIPHER_CTX_init(&decrypt); + + EVP_EncryptInit_ex(&encrypt, algo, 0, 0, 0); + EVP_DecryptInit_ex(&decrypt, algo, 0, 0, 0); + + EVP_CIPHER_CTX_set_padding(&encrypt, 0); + EVP_CIPHER_CTX_set_padding(&decrypt, 0); + } + +/* +* EVP Block Cipher Constructor +*/ +EVP_BlockCipher::EVP_BlockCipher(const EVP_CIPHER* algo, + const std::string& algo_name, + u32bit key_min, u32bit key_max, + u32bit key_mod) : + BlockCipher(EVP_CIPHER_block_size(algo), key_min, key_max, key_mod), + cipher_name(algo_name) + { + if(EVP_CIPHER_mode(algo) != EVP_CIPH_ECB_MODE) + throw Invalid_Argument("EVP_BlockCipher: Non-ECB EVP was passed in"); + + EVP_CIPHER_CTX_init(&encrypt); + EVP_CIPHER_CTX_init(&decrypt); + + EVP_EncryptInit_ex(&encrypt, algo, 0, 0, 0); + EVP_DecryptInit_ex(&decrypt, algo, 0, 0, 0); + + EVP_CIPHER_CTX_set_padding(&encrypt, 0); + EVP_CIPHER_CTX_set_padding(&decrypt, 0); + } + +/* +* EVP Block Cipher Destructor +*/ +EVP_BlockCipher::~EVP_BlockCipher() + { + EVP_CIPHER_CTX_cleanup(&encrypt); + EVP_CIPHER_CTX_cleanup(&decrypt); + } + +/* +* Encrypt a block +*/ +void EVP_BlockCipher::enc(const byte in[], byte out[]) const + { + int out_len = 0; + EVP_EncryptUpdate(&encrypt, out, &out_len, in, BLOCK_SIZE); + } + +/* +* Decrypt a block +*/ +void EVP_BlockCipher::dec(const byte in[], byte out[]) const + { + int out_len = 0; + EVP_DecryptUpdate(&decrypt, out, &out_len, in, BLOCK_SIZE); + } + +/* +* Set the key +*/ +void EVP_BlockCipher::key_schedule(const byte key[], u32bit length) + { + SecureVector<byte> full_key(key, length); + + if(cipher_name == "TripleDES" && length == 16) + full_key.append(key, 8); + else + if(EVP_CIPHER_CTX_set_key_length(&encrypt, length) == 0 || + EVP_CIPHER_CTX_set_key_length(&decrypt, length) == 0) + throw Invalid_Argument("EVP_BlockCipher: Bad key length for " + + cipher_name); + + if(cipher_name == "RC2") + { + EVP_CIPHER_CTX_ctrl(&encrypt, EVP_CTRL_SET_RC2_KEY_BITS, length*8, 0); + EVP_CIPHER_CTX_ctrl(&decrypt, EVP_CTRL_SET_RC2_KEY_BITS, length*8, 0); + } + + EVP_EncryptInit_ex(&encrypt, 0, 0, full_key.begin(), 0); + EVP_DecryptInit_ex(&decrypt, 0, 0, full_key.begin(), 0); + } + +/* +* Return a clone of this object +*/ +BlockCipher* EVP_BlockCipher::clone() const + { + return new EVP_BlockCipher(EVP_CIPHER_CTX_cipher(&encrypt), + cipher_name, MINIMUM_KEYLENGTH, + MAXIMUM_KEYLENGTH, KEYLENGTH_MULTIPLE); + } + +/* +* Clear memory of sensitive data +*/ +void EVP_BlockCipher::clear() throw() + { + const EVP_CIPHER* algo = EVP_CIPHER_CTX_cipher(&encrypt); + + EVP_CIPHER_CTX_cleanup(&encrypt); + EVP_CIPHER_CTX_cleanup(&decrypt); + EVP_CIPHER_CTX_init(&encrypt); + EVP_CIPHER_CTX_init(&decrypt); + EVP_EncryptInit_ex(&encrypt, algo, 0, 0, 0); + EVP_DecryptInit_ex(&decrypt, algo, 0, 0, 0); + EVP_CIPHER_CTX_set_padding(&encrypt, 0); + EVP_CIPHER_CTX_set_padding(&decrypt, 0); + } + +} + +/* +* Look for an algorithm with this name +*/ +BlockCipher* +OpenSSL_Engine::find_block_cipher(const SCAN_Name& request, + Algorithm_Factory&) const + { +#define HANDLE_EVP_CIPHER(NAME, EVP) \ + if(request.algo_name() == NAME && request.arg_count() == 0) \ + return new EVP_BlockCipher(EVP, NAME); + +#define HANDLE_EVP_CIPHER_KEYLEN(NAME, EVP, MIN, MAX, MOD) \ + if(request.algo_name() == NAME && request.arg_count() == 0) \ + return new EVP_BlockCipher(EVP, NAME, MIN, MAX, MOD); + +#if 0 + /* + Using OpenSSL's AES causes crashes inside EVP on x86-64 with OpenSSL 0.9.8g + cause is unknown + */ + HANDLE_EVP_CIPHER("AES-128", EVP_aes_128_ecb()); + HANDLE_EVP_CIPHER("AES-192", EVP_aes_192_ecb()); + HANDLE_EVP_CIPHER("AES-256", EVP_aes_256_ecb()); +#endif + + HANDLE_EVP_CIPHER("DES", EVP_des_ecb()); + HANDLE_EVP_CIPHER_KEYLEN("TripleDES", EVP_des_ede3_ecb(), 16, 24, 8); + + HANDLE_EVP_CIPHER_KEYLEN("Blowfish", EVP_bf_ecb(), 1, 56, 1); + HANDLE_EVP_CIPHER_KEYLEN("CAST-128", EVP_cast5_ecb(), 1, 16, 1); + HANDLE_EVP_CIPHER_KEYLEN("RC2", EVP_rc2_ecb(), 1, 32, 1); + +#undef HANDLE_EVP_CIPHER +#undef HANDLE_EVP_CIPHER_KEYLEN + + return 0; + } + +} diff --git a/botan/src/engine/openssl/ossl_dh.cpp b/botan/src/engine/openssl/ossl_dh.cpp new file mode 100644 index 0000000..72eab8a --- /dev/null +++ b/botan/src/engine/openssl/ossl_dh.cpp @@ -0,0 +1,60 @@ +/* +* OpenSSL Engine +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/eng_ossl.h> +#include <botan/bn_wrap.h> +#include <openssl/opensslv.h> + +#if OPENSSL_VERSION_NUMBER < 0x0090700F + #error Your OpenSSL install is too old, upgrade to 0.9.7 or later +#endif + +namespace Botan { + +#if defined(BOTAN_HAS_DIFFIE_HELLMAN) + +namespace { + +/* +* OpenSSL DH Operation +*/ +class OpenSSL_DH_Op : public DH_Operation + { + public: + BigInt agree(const BigInt& i) const; + DH_Operation* clone() const { return new OpenSSL_DH_Op(*this); } + + OpenSSL_DH_Op(const DL_Group& group, const BigInt& x_bn) : + x(x_bn), p(group.get_p()) {} + private: + OSSL_BN x, p; + OSSL_BN_CTX ctx; + }; + +/* +* OpenSSL DH Key Agreement Operation +*/ +BigInt OpenSSL_DH_Op::agree(const BigInt& i_bn) const + { + OSSL_BN i(i_bn), r; + BN_mod_exp(r.value, i.value, x.value, p.value, ctx.value); + return r.to_bigint(); + } + +} + +/* +* Acquire a DH op +*/ +DH_Operation* OpenSSL_Engine::dh_op(const DL_Group& group, + const BigInt& x) const + { + return new OpenSSL_DH_Op(group, x); + } +#endif + +} diff --git a/botan/src/engine/openssl/ossl_dsa.cpp b/botan/src/engine/openssl/ossl_dsa.cpp new file mode 100644 index 0000000..bfffb87 --- /dev/null +++ b/botan/src/engine/openssl/ossl_dsa.cpp @@ -0,0 +1,126 @@ +/* +* OpenSSL DSA Engine +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/eng_ossl.h> +#include <botan/bn_wrap.h> +#include <openssl/opensslv.h> + +#if OPENSSL_VERSION_NUMBER < 0x0090700F + #error Your OpenSSL install is too old, upgrade to 0.9.7 or later +#endif + +namespace Botan { + +#if defined(BOTAN_HAS_DSA) + +namespace { + +/* +* OpenSSL DSA Operation +*/ +class OpenSSL_DSA_Op : public DSA_Operation + { + public: + bool verify(const byte[], u32bit, const byte[], u32bit) const; + SecureVector<byte> sign(const byte[], u32bit, const BigInt&) const; + + DSA_Operation* clone() const { return new OpenSSL_DSA_Op(*this); } + + OpenSSL_DSA_Op(const DL_Group& group, const BigInt& y1, + const BigInt& x1) : + x(x1), y(y1), p(group.get_p()), q(group.get_q()), g(group.get_g()) {} + private: + const OSSL_BN x, y, p, q, g; + OSSL_BN_CTX ctx; + }; + +/* +* OpenSSL DSA Verify Operation +*/ +bool OpenSSL_DSA_Op::verify(const byte msg[], u32bit msg_len, + const byte sig[], u32bit sig_len) const + { + const u32bit q_bytes = q.bytes(); + + if(sig_len != 2*q_bytes || msg_len > q_bytes) + return false; + + OSSL_BN r(sig, q_bytes); + OSSL_BN s(sig + q_bytes, q_bytes); + OSSL_BN i(msg, msg_len); + + if(BN_is_zero(r.value) || BN_cmp(r.value, q.value) >= 0) + return false; + if(BN_is_zero(s.value) || BN_cmp(s.value, q.value) >= 0) + return false; + + if(BN_mod_inverse(s.value, s.value, q.value, ctx.value) == 0) + return false; + + OSSL_BN si; + BN_mod_mul(si.value, s.value, i.value, q.value, ctx.value); + BN_mod_exp(si.value, g.value, si.value, p.value, ctx.value); + + OSSL_BN sr; + BN_mod_mul(sr.value, s.value, r.value, q.value, ctx.value); + BN_mod_exp(sr.value, y.value, sr.value, p.value, ctx.value); + + BN_mod_mul(si.value, si.value, sr.value, p.value, ctx.value); + BN_nnmod(si.value, si.value, q.value, ctx.value); + + if(BN_cmp(si.value, r.value) == 0) + return true; + return false; + } + +/* +* OpenSSL DSA Sign Operation +*/ +SecureVector<byte> OpenSSL_DSA_Op::sign(const byte in[], u32bit length, + const BigInt& k_bn) const + { + if(BN_is_zero(x.value)) + throw Internal_Error("OpenSSL_DSA_Op::sign: No private key"); + + OSSL_BN i(in, length); + OSSL_BN k(k_bn); + + OSSL_BN r; + BN_mod_exp(r.value, g.value, k.value, p.value, ctx.value); + BN_nnmod(r.value, r.value, q.value, ctx.value); + + BN_mod_inverse(k.value, k.value, q.value, ctx.value); + + OSSL_BN s; + BN_mul(s.value, x.value, r.value, ctx.value); + BN_add(s.value, s.value, i.value); + BN_mod_mul(s.value, s.value, k.value, q.value, ctx.value); + + if(BN_is_zero(r.value) || BN_is_zero(s.value)) + throw Internal_Error("OpenSSL_DSA_Op::sign: r or s was zero"); + + const u32bit q_bytes = q.bytes(); + + SecureVector<byte> output(2*q_bytes); + r.encode(output, q_bytes); + s.encode(output + q_bytes, q_bytes); + return output; + } + +} + +/* +* Acquire a DSA op +*/ +DSA_Operation* OpenSSL_Engine::dsa_op(const DL_Group& group, const BigInt& y, + const BigInt& x) const + { + return new OpenSSL_DSA_Op(group, y, x); + } +#endif + +} diff --git a/botan/src/engine/openssl/ossl_elg.cpp b/botan/src/engine/openssl/ossl_elg.cpp new file mode 100644 index 0000000..aefda9a --- /dev/null +++ b/botan/src/engine/openssl/ossl_elg.cpp @@ -0,0 +1,95 @@ +/* +* OpenSSL Engine +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/eng_ossl.h> +#include <botan/bn_wrap.h> +#include <openssl/opensslv.h> + +#if OPENSSL_VERSION_NUMBER < 0x0090700F + #error Your OpenSSL install is too old, upgrade to 0.9.7 or later +#endif + +namespace Botan { + +#if defined(BOTAN_HAS_ELGAMAL) + +namespace { + +/* +* OpenSSL ElGamal Operation +*/ +class OpenSSL_ELG_Op : public ELG_Operation + { + public: + SecureVector<byte> encrypt(const byte[], u32bit, const BigInt&) const; + BigInt decrypt(const BigInt&, const BigInt&) const; + + ELG_Operation* clone() const { return new OpenSSL_ELG_Op(*this); } + OpenSSL_ELG_Op(const DL_Group& group, const BigInt& y1, + const BigInt& x1) : + x(x1), y(y1), g(group.get_g()), p(group.get_p()) {} + private: + OSSL_BN x, y, g, p; + OSSL_BN_CTX ctx; + }; + +/* +* OpenSSL ElGamal Encrypt Operation +*/ +SecureVector<byte> OpenSSL_ELG_Op::encrypt(const byte in[], u32bit length, + const BigInt& k_bn) const + { + OSSL_BN i(in, length); + + if(BN_cmp(i.value, p.value) >= 0) + throw Invalid_Argument("OpenSSL_ELG_Op: Input is too large"); + + OSSL_BN a, b, k(k_bn); + + BN_mod_exp(a.value, g.value, k.value, p.value, ctx.value); + BN_mod_exp(b.value, y.value, k.value, p.value, ctx.value); + BN_mod_mul(b.value, b.value, i.value, p.value, ctx.value); + + const u32bit p_bytes = p.bytes(); + SecureVector<byte> output(2*p_bytes); + a.encode(output, p_bytes); + b.encode(output + p_bytes, p_bytes); + return output; + } + +/* +* OpenSSL ElGamal Decrypt Operation +*/ +BigInt OpenSSL_ELG_Op::decrypt(const BigInt& a_bn, const BigInt& b_bn) const + { + if(BN_is_zero(x.value)) + throw Internal_Error("OpenSSL_ELG_Op::decrypt: No private key"); + + OSSL_BN a(a_bn), b(b_bn), t; + + if(BN_cmp(a.value, p.value) >= 0 || BN_cmp(b.value, p.value) >= 0) + throw Invalid_Argument("OpenSSL_ELG_Op: Invalid message"); + + BN_mod_exp(t.value, a.value, x.value, p.value, ctx.value); + BN_mod_inverse(a.value, t.value, p.value, ctx.value); + BN_mod_mul(a.value, a.value, b.value, p.value, ctx.value); + return a.to_bigint(); + } + +} + +/* +* Acquire an ElGamal op +*/ +ELG_Operation* OpenSSL_Engine::elg_op(const DL_Group& group, const BigInt& y, + const BigInt& x) const + { + return new OpenSSL_ELG_Op(group, y, x); + } +#endif + +} diff --git a/botan/src/engine/openssl/ossl_if.cpp b/botan/src/engine/openssl/ossl_if.cpp new file mode 100644 index 0000000..bbc10d5 --- /dev/null +++ b/botan/src/engine/openssl/ossl_if.cpp @@ -0,0 +1,85 @@ +/* +* OpenSSL IF Engine +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/eng_ossl.h> +#include <botan/bn_wrap.h> +#include <openssl/opensslv.h> + +#if OPENSSL_VERSION_NUMBER < 0x0090700F + #error Your OpenSSL install is too old, upgrade to 0.9.7 or later +#endif + +namespace Botan { + +#if defined(BOTAN_HAS_IF_PUBLIC_KEY_FAMILY) + +namespace { + +/* +* OpenSSL IF Operation +*/ +class OpenSSL_IF_Op : public IF_Operation + { + public: + BigInt public_op(const BigInt&) const; + BigInt private_op(const BigInt&) const; + + IF_Operation* clone() const { return new OpenSSL_IF_Op(*this); } + + OpenSSL_IF_Op(const BigInt& e_bn, const BigInt& n_bn, const BigInt&, + const BigInt& p_bn, const BigInt& q_bn, const BigInt& d1_bn, + const BigInt& d2_bn, const BigInt& c_bn) : + e(e_bn), n(n_bn), p(p_bn), q(q_bn), d1(d1_bn), d2(d2_bn), c(c_bn) {} + private: + const OSSL_BN e, n, p, q, d1, d2, c; + OSSL_BN_CTX ctx; + }; + +/* +* OpenSSL IF Public Operation +*/ +BigInt OpenSSL_IF_Op::public_op(const BigInt& i_bn) const + { + OSSL_BN i(i_bn), r; + BN_mod_exp(r.value, i.value, e.value, n.value, ctx.value); + return r.to_bigint(); + } + +/* +* OpenSSL IF Private Operation +*/ +BigInt OpenSSL_IF_Op::private_op(const BigInt& i_bn) const + { + if(BN_is_zero(p.value)) + throw Internal_Error("OpenSSL_IF_Op::private_op: No private key"); + + OSSL_BN j1, j2, h(i_bn); + + BN_mod_exp(j1.value, h.value, d1.value, p.value, ctx.value); + BN_mod_exp(j2.value, h.value, d2.value, q.value, ctx.value); + BN_sub(h.value, j1.value, j2.value); + BN_mod_mul(h.value, h.value, c.value, p.value, ctx.value); + BN_mul(h.value, h.value, q.value, ctx.value); + BN_add(h.value, h.value, j2.value); + return h.to_bigint(); + } + +} + +/* +* Acquire an IF op +*/ +IF_Operation* OpenSSL_Engine::if_op(const BigInt& e, const BigInt& n, + const BigInt& d, const BigInt& p, + const BigInt& q, const BigInt& d1, + const BigInt& d2, const BigInt& c) const + { + return new OpenSSL_IF_Op(e, n, d, p, q, d1, d2, c); + } +#endif + +} diff --git a/botan/src/engine/openssl/ossl_md.cpp b/botan/src/engine/openssl/ossl_md.cpp new file mode 100644 index 0000000..7c8fb67 --- /dev/null +++ b/botan/src/engine/openssl/ossl_md.cpp @@ -0,0 +1,126 @@ +/* +* OpenSSL Hash Functions +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/eng_ossl.h> +#include <openssl/evp.h> + +namespace Botan { + +namespace { + +/* +* EVP Hash Function +*/ +class EVP_HashFunction : public HashFunction + { + public: + void clear() throw(); + std::string name() const { return algo_name; } + HashFunction* clone() const; + EVP_HashFunction(const EVP_MD*, const std::string&); + ~EVP_HashFunction(); + private: + void add_data(const byte[], u32bit); + void final_result(byte[]); + + std::string algo_name; + EVP_MD_CTX md; + }; + +/* +* Update an EVP Hash Calculation +*/ +void EVP_HashFunction::add_data(const byte input[], u32bit length) + { + EVP_DigestUpdate(&md, input, length); + } + +/* +* Finalize an EVP Hash Calculation +*/ +void EVP_HashFunction::final_result(byte output[]) + { + EVP_DigestFinal_ex(&md, output, 0); + const EVP_MD* algo = EVP_MD_CTX_md(&md); + EVP_DigestInit_ex(&md, algo, 0); + } + +/* +* Clear memory of sensitive data +*/ +void EVP_HashFunction::clear() throw() + { + const EVP_MD* algo = EVP_MD_CTX_md(&md); + EVP_DigestInit_ex(&md, algo, 0); + } + +/* +* Return a clone of this object +*/ +HashFunction* EVP_HashFunction::clone() const + { + const EVP_MD* algo = EVP_MD_CTX_md(&md); + return new EVP_HashFunction(algo, name()); + } + +/* +* Create an EVP hash function +*/ +EVP_HashFunction::EVP_HashFunction(const EVP_MD* algo, + const std::string& name) : + HashFunction(EVP_MD_size(algo), EVP_MD_block_size(algo)), + algo_name(name) + { + EVP_MD_CTX_init(&md); + EVP_DigestInit_ex(&md, algo, 0); + } + +/* +* Destroy an EVP hash function +*/ +EVP_HashFunction::~EVP_HashFunction() + { + EVP_MD_CTX_cleanup(&md); + } + +} + +/* +* Look for an algorithm with this name +*/ +HashFunction* OpenSSL_Engine::find_hash(const SCAN_Name& request, + Algorithm_Factory&) const + { +#ifndef OPENSSL_NO_SHA + if(request.algo_name() == "SHA-160") + return new EVP_HashFunction(EVP_sha1(), "SHA-160"); +#endif + +#ifndef OPENSSL_NO_MD2 + if(request.algo_name() == "MD2") + return new EVP_HashFunction(EVP_md2(), "MD2"); +#endif + +#ifndef OPENSSL_NO_MD4 + if(request.algo_name() == "MD4") + return new EVP_HashFunction(EVP_md4(), "MD4"); +#endif + +#ifndef OPENSSL_NO_MD5 + if(request.algo_name() == "MD5") + return new EVP_HashFunction(EVP_md5(), "MD5"); +#endif + +#ifndef OPENSSL_NO_RIPEMD + if(request.algo_name() == "RIPEMD-160") + return new EVP_HashFunction(EVP_ripemd160(), "RIPEMD-160"); +#endif + + return 0; + } + +} diff --git a/botan/src/engine/openssl/ossl_nr.cpp b/botan/src/engine/openssl/ossl_nr.cpp new file mode 100644 index 0000000..532e4b8 --- /dev/null +++ b/botan/src/engine/openssl/ossl_nr.cpp @@ -0,0 +1,113 @@ +/* +* OpenSSL NR Engine +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/eng_ossl.h> +#include <botan/bn_wrap.h> +#include <openssl/opensslv.h> + +#if OPENSSL_VERSION_NUMBER < 0x0090700F + #error Your OpenSSL install is too old, upgrade to 0.9.7 or later +#endif + +namespace Botan { + +#if defined(BOTAN_HAS_NYBERG_RUEPPEL) + +namespace { + +/* +* OpenSSL NR Operation +*/ +class OpenSSL_NR_Op : public NR_Operation + { + public: + SecureVector<byte> verify(const byte[], u32bit) const; + SecureVector<byte> sign(const byte[], u32bit, const BigInt&) const; + + NR_Operation* clone() const { return new OpenSSL_NR_Op(*this); } + + OpenSSL_NR_Op(const DL_Group& group, const BigInt& y1, + const BigInt& x1) : + x(x1), y(y1), p(group.get_p()), q(group.get_q()), g(group.get_g()) {} + private: + const OSSL_BN x, y, p, q, g; + OSSL_BN_CTX ctx; + }; + +/* +* OpenSSL NR Verify Operation +*/ +SecureVector<byte> OpenSSL_NR_Op::verify(const byte sig[], + u32bit sig_len) const + { + const u32bit q_bytes = q.bytes(); + + if(sig_len != 2*q_bytes) + return false; + + OSSL_BN c(sig, q_bytes); + OSSL_BN d(sig + q_bytes, q_bytes); + + if(BN_is_zero(c.value) || BN_cmp(c.value, q.value) >= 0 || + BN_cmp(d.value, q.value) >= 0) + throw Invalid_Argument("OpenSSL_NR_Op::verify: Invalid signature"); + + OSSL_BN i1, i2; + BN_mod_exp(i1.value, g.value, d.value, p.value, ctx.value); + BN_mod_exp(i2.value, y.value, c.value, p.value, ctx.value); + BN_mod_mul(i1.value, i1.value, i2.value, p.value, ctx.value); + BN_sub(i1.value, c.value, i1.value); + BN_nnmod(i1.value, i1.value, q.value, ctx.value); + return BigInt::encode(i1.to_bigint()); + } + +/* +* OpenSSL NR Sign Operation +*/ +SecureVector<byte> OpenSSL_NR_Op::sign(const byte in[], u32bit length, + const BigInt& k_bn) const + { + if(BN_is_zero(x.value)) + throw Internal_Error("OpenSSL_NR_Op::sign: No private key"); + + OSSL_BN f(in, length); + OSSL_BN k(k_bn); + + if(BN_cmp(f.value, q.value) >= 0) + throw Invalid_Argument("OpenSSL_NR_Op::sign: Input is out of range"); + + OSSL_BN c, d; + BN_mod_exp(c.value, g.value, k.value, p.value, ctx.value); + BN_add(c.value, c.value, f.value); + BN_nnmod(c.value, c.value, q.value, ctx.value); + BN_mul(d.value, x.value, c.value, ctx.value); + BN_sub(d.value, k.value, d.value); + BN_nnmod(d.value, d.value, q.value, ctx.value); + + if(BN_is_zero(c.value)) + throw Internal_Error("Default_NR_Op::sign: c was zero"); + + const u32bit q_bytes = q.bytes(); + SecureVector<byte> output(2*q_bytes); + c.encode(output, q_bytes); + d.encode(output + q_bytes, q_bytes); + return output; + } + +} + +/* +* Acquire a NR op +*/ +NR_Operation* OpenSSL_Engine::nr_op(const DL_Group& group, const BigInt& y, + const BigInt& x) const + { + return new OpenSSL_NR_Op(group, y, x); + } +#endif + +} diff --git a/botan/src/engine/sse2_eng/eng_sse2.cpp b/botan/src/engine/sse2_eng/eng_sse2.cpp new file mode 100644 index 0000000..c738b3d --- /dev/null +++ b/botan/src/engine/sse2_eng/eng_sse2.cpp @@ -0,0 +1,27 @@ +/** +* SSE2 Assembly Engine +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/eng_sse2.h> + +#if defined(BOTAN_HAS_SHA1_SSE2) + #include <botan/sha1_sse2.h> +#endif + +namespace Botan { + +HashFunction* SSE2_Assembler_Engine::find_hash(const SCAN_Name& request, + Algorithm_Factory&) const + { +#if defined(BOTAN_HAS_SHA1_SSE2) + if(request.algo_name() == "SHA-160") + return new SHA_160_SSE2; +#endif + + return 0; + } + +} diff --git a/botan/src/engine/sse2_eng/eng_sse2.h b/botan/src/engine/sse2_eng/eng_sse2.h new file mode 100644 index 0000000..129697e --- /dev/null +++ b/botan/src/engine/sse2_eng/eng_sse2.h @@ -0,0 +1,26 @@ +/** +* SSE2 Assembly Engine +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_SSE2_ASM_ENGINE_H__ +#define BOTAN_SSE2_ASM_ENGINE_H__ + +#include <botan/engine.h> + +namespace Botan { + +class BOTAN_DLL SSE2_Assembler_Engine : public Engine + { + public: + std::string provider_name() const { return "sse2"; } + private: + HashFunction* find_hash(const SCAN_Name& reqeust, + Algorithm_Factory&) const; + }; + +} + +#endif diff --git a/botan/src/engine/sse2_eng/info.txt b/botan/src/engine/sse2_eng/info.txt new file mode 100644 index 0000000..6242c7f --- /dev/null +++ b/botan/src/engine/sse2_eng/info.txt @@ -0,0 +1,15 @@ +realname "SSE2 Assembler Engine" + +define ENGINE_SSE2_ASSEMBLER + +load_on dep + +<add> +eng_sse2.cpp +eng_sse2.h +</add> + +<arch> +ia32 +amd64 +</arch> diff --git a/botan/src/entropy/beos_stats/es_beos.cpp b/botan/src/entropy/beos_stats/es_beos.cpp new file mode 100644 index 0000000..18eca55 --- /dev/null +++ b/botan/src/entropy/beos_stats/es_beos.cpp @@ -0,0 +1,65 @@ +/** +* BeOS EntropySource +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/es_beos.h> + +#include <kernel/OS.h> +#include <kernel/image.h> +#include <interface/InterfaceDefs.h> + +namespace Botan { + +/** +* BeOS entropy poll +*/ +void BeOS_EntropySource::poll(Entropy_Accumulator& accum) + { + system_info info_sys; + get_system_info(&info_sys); + accum.add(info_sys, 2); + + key_info info_key; // current state of the keyboard + get_key_info(&info_key); + accum.add(info_key, 0); + + accum.add(idle_time(), 0); + + team_info info_team; + int32 cookie_team = 0; + + while(get_next_team_info(&cookie_team, &info_team) == B_OK) + { + accum.add(info_team, 2); + + team_id id = info_team.team; + int32 cookie = 0; + + thread_info info_thr; + while(get_next_thread_info(id, &cookie, &info_thr) == B_OK) + accum.add(info_thr, 1); + + cookie = 0; + image_info info_img; + while(get_next_image_info(id, &cookie, &info_img) == B_OK) + accum.add(info_img, 1); + + cookie = 0; + sem_info info_sem; + while(get_next_sem_info(id, &cookie, &info_sem) == B_OK) + accum.add(info_sem, 1); + + cookie = 0; + area_info info_area; + while(get_next_area_info(id, &cookie, &info_area) == B_OK) + accum.add(info_area, 2); + + if(accum.polling_goal_achieved()) + break; + } + } + +} diff --git a/botan/src/entropy/beos_stats/es_beos.h b/botan/src/entropy/beos_stats/es_beos.h new file mode 100644 index 0000000..be80ad3 --- /dev/null +++ b/botan/src/entropy/beos_stats/es_beos.h @@ -0,0 +1,28 @@ +/** +* BeOS EntropySource +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ENTROPY_SRC_BEOS_H__ +#define BOTAN_ENTROPY_SRC_BEOS_H__ + +#include <botan/entropy_src.h> + +namespace Botan { + +/** +* BeOS Entropy Source +*/ +class BOTAN_DLL BeOS_EntropySource : public EntropySource + { + private: + std::string name() const { return "BeOS Statistics"; } + + void poll(Entropy_Accumulator& accum); + }; + +} + +#endif diff --git a/botan/src/entropy/beos_stats/info.txt b/botan/src/entropy/beos_stats/info.txt new file mode 100644 index 0000000..a7e62cf --- /dev/null +++ b/botan/src/entropy/beos_stats/info.txt @@ -0,0 +1,19 @@ +realname "BeOS Entropy Source" + +define ENTROPY_SRC_BEOS +modset beos + +load_on auto + +<add> +es_beos.h +es_beos.cpp +</add> + +<os> +beos +</os> + +<libs> +beos -> root,be +</libs> diff --git a/botan/src/entropy/cryptoapi_rng/es_capi.cpp b/botan/src/entropy/cryptoapi_rng/es_capi.cpp new file mode 100644 index 0000000..a70b520 --- /dev/null +++ b/botan/src/entropy/cryptoapi_rng/es_capi.cpp @@ -0,0 +1,93 @@ +/* +* Win32 CryptoAPI EntropySource +* (C) 1999-2009 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/es_capi.h> +#include <botan/parsing.h> +#include <windows.h> +#include <wincrypt.h> + +namespace Botan { + +namespace { + +class CSP_Handle + { + public: + CSP_Handle(u64bit capi_provider) + { + valid = false; + DWORD prov_type = (DWORD)capi_provider; + + if(CryptAcquireContext(&handle, 0, 0, + prov_type, CRYPT_VERIFYCONTEXT)) + valid = true; + } + + ~CSP_Handle() + { + if(is_valid()) + CryptReleaseContext(handle, 0); + } + + u32bit gen_random(byte out[], u32bit n) const + { + if(is_valid() && CryptGenRandom(handle, n, out)) + return n; + return 0; + } + + bool is_valid() const { return valid; } + + HCRYPTPROV get_handle() const { return handle; } + private: + HCRYPTPROV handle; + bool valid; + }; + +} + +/** +* Gather Entropy from Win32 CAPI +*/ +void Win32_CAPI_EntropySource::poll(Entropy_Accumulator& accum) + { + MemoryRegion<byte>& io_buffer = accum.get_io_buffer(32); + + for(u32bit j = 0; j != prov_types.size(); ++j) + { + CSP_Handle csp(prov_types[j]); + + u32bit got = csp.gen_random(io_buffer.begin(), io_buffer.size()); + + if(got) + { + accum.add(io_buffer.begin(), io_buffer.size(), 8); + break; + } + } + } + +/** +* Win32_Capi_Entropysource Constructor +*/ +Win32_CAPI_EntropySource::Win32_CAPI_EntropySource(const std::string& provs) + { + std::vector<std::string> capi_provs = split_on(provs, ':'); + + for(u32bit j = 0; j != capi_provs.size(); ++j) + { + if(capi_provs[j] == "RSA_FULL") prov_types.push_back(PROV_RSA_FULL); + if(capi_provs[j] == "INTEL_SEC") prov_types.push_back(PROV_INTEL_SEC); + if(capi_provs[j] == "FORTEZZA") prov_types.push_back(PROV_FORTEZZA); + if(capi_provs[j] == "RNG") prov_types.push_back(PROV_RNG); + } + + if(prov_types.size() == 0) + prov_types.push_back(PROV_RSA_FULL); + } + +} diff --git a/botan/src/entropy/cryptoapi_rng/es_capi.h b/botan/src/entropy/cryptoapi_rng/es_capi.h new file mode 100644 index 0000000..55966d7 --- /dev/null +++ b/botan/src/entropy/cryptoapi_rng/es_capi.h @@ -0,0 +1,33 @@ +/* +* Win32 CAPI EntropySource +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ENTROPY_SRC_WIN32_CAPI_H__ +#define BOTAN_ENTROPY_SRC_WIN32_CAPI_H__ + +#include <botan/entropy_src.h> +#include <vector> + +namespace Botan { + +/** +* Win32 CAPI Entropy Source +*/ +class BOTAN_DLL Win32_CAPI_EntropySource : public EntropySource + { + public: + std::string name() const { return "Win32 CryptoGenRandom"; } + + void poll(Entropy_Accumulator& accum); + + Win32_CAPI_EntropySource(const std::string& = ""); + private: + std::vector<u64bit> prov_types; + }; + +} + +#endif diff --git a/botan/src/entropy/cryptoapi_rng/info.txt b/botan/src/entropy/cryptoapi_rng/info.txt new file mode 100644 index 0000000..643c67d --- /dev/null +++ b/botan/src/entropy/cryptoapi_rng/info.txt @@ -0,0 +1,21 @@ +realname "Win32 CryptoAPI Entropy Source" + +define ENTROPY_SRC_CAPI +load_on auto +modset win32 + +<add> +es_capi.h +es_capi.cpp +</add> + +# We'll just assume CAPI is there; this is OK except for 3.x, early versions +# of 95, and maybe NT 3.5 +<os> +windows +cygwin +</os> + +<libs> +windows -> advapi32.lib +</libs> diff --git a/botan/src/entropy/dev_random/es_dev.cpp b/botan/src/entropy/dev_random/es_dev.cpp new file mode 100644 index 0000000..ef30741 --- /dev/null +++ b/botan/src/entropy/dev_random/es_dev.cpp @@ -0,0 +1,125 @@ +/* +* /dev/random EntropySource +* (C) 1999-2009 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/es_dev.h> + +#include <sys/types.h> +#include <sys/select.h> +#include <sys/stat.h> +#include <unistd.h> +#include <fcntl.h> + +namespace Botan { + +/** +Close the device, if open +*/ +void Device_EntropySource::Device_Reader::close() + { + if(fd > 0) { ::close(fd); fd = -1; } + } + +/** +Read bytes from a device file +*/ +u32bit Device_EntropySource::Device_Reader::get(byte out[], u32bit length, + u32bit ms_wait_time) + { + if(fd < 0) + return 0; + + if(fd >= FD_SETSIZE) + return 0; + + fd_set read_set; + FD_ZERO(&read_set); + FD_SET(fd, &read_set); + + struct ::timeval timeout; + + timeout.tv_sec = (ms_wait_time / 1000); + timeout.tv_usec = (ms_wait_time % 1000) * 1000; + + if(::select(fd + 1, &read_set, 0, 0, &timeout) < 0) + return 0; + + if(!(FD_ISSET(fd, &read_set))) + return 0; + + const ssize_t got = ::read(fd, out, length); + if(got <= 0) + return 0; + + return static_cast<u32bit>(got); + } + +/** +Attempt to open a device +*/ +Device_EntropySource::Device_Reader::fd_type +Device_EntropySource::Device_Reader::open(const std::string& pathname) + { +#ifndef O_NONBLOCK + #define O_NONBLOCK 0 +#endif + +#ifndef O_NOCTTY + #define O_NOCTTY 0 +#endif + + const int flags = O_RDONLY | O_NONBLOCK | O_NOCTTY; + return ::open(pathname.c_str(), flags); + } + +/** +Device_EntropySource constructor +Open a file descriptor to each (available) device in fsnames +*/ +Device_EntropySource::Device_EntropySource( + const std::vector<std::string>& fsnames) + { + for(u32bit i = 0; i != fsnames.size(); ++i) + { + Device_Reader::fd_type fd = Device_Reader::open(fsnames[i]); + if(fd > 0) + devices.push_back(Device_Reader(fd)); + } + } + +/** +Device_EntropySource destructor: close all open devices +*/ +Device_EntropySource::~Device_EntropySource() + { + for(size_t i = 0; i != devices.size(); ++i) + devices[i].close(); + } + +/** +* Gather entropy from a RNG device +*/ +void Device_EntropySource::poll(Entropy_Accumulator& accum) + { + u32bit go_get = std::min<u32bit>(accum.desired_remaining_bits() / 8, 48); + + u32bit read_wait_ms = std::max<u32bit>(go_get, 1000); + MemoryRegion<byte>& io_buffer = accum.get_io_buffer(go_get); + + for(size_t i = 0; i != devices.size(); ++i) + { + u32bit got = devices[i].get(io_buffer.begin(), io_buffer.size(), + read_wait_ms); + + if(got) + { + accum.add(io_buffer.begin(), got, 8); + break; + } + } + } + +} diff --git a/botan/src/entropy/dev_random/es_dev.h b/botan/src/entropy/dev_random/es_dev.h new file mode 100644 index 0000000..df9dbe1 --- /dev/null +++ b/botan/src/entropy/dev_random/es_dev.h @@ -0,0 +1,53 @@ +/* +* /dev/random EntropySource +* (C) 1999-2009 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ENTROPY_SRC_DEVICE_H__ +#define BOTAN_ENTROPY_SRC_DEVICE_H__ + +#include <botan/entropy_src.h> +#include <vector> +#include <string> + +namespace Botan { + +class BOTAN_DLL Device_EntropySource : public EntropySource + { + public: + std::string name() const { return "RNG Device Reader"; } + + void poll(Entropy_Accumulator& accum); + + Device_EntropySource(const std::vector<std::string>& fsnames); + ~Device_EntropySource(); + private: + + /** + A class handling reading from a Unix character device + */ + class Device_Reader + { + public: + typedef int fd_type; + + // Does not own fd, a transient class + Device_Reader(fd_type device_fd) : fd(device_fd) {} + + void close(); + + u32bit get(byte out[], u32bit length, u32bit ms_wait_time); + + static fd_type open(const std::string& pathname); + private: + fd_type fd; + }; + + std::vector<Device_Reader> devices; + }; + +} + +#endif diff --git a/botan/src/entropy/dev_random/info.txt b/botan/src/entropy/dev_random/info.txt new file mode 100644 index 0000000..fddb7ac --- /dev/null +++ b/botan/src/entropy/dev_random/info.txt @@ -0,0 +1,28 @@ +realname "RNG Device Reader" + +define ENTROPY_SRC_DEVICE + +load_on auto +modset unix + +<add> +es_dev.h +es_dev.cpp +</add> + +<os> +aix +beos +cygwin +darwin +freebsd +dragonfly +hpux +irix +linux +netbsd +openbsd +qnx +solaris +tru64 +</os> diff --git a/botan/src/entropy/egd/es_egd.cpp b/botan/src/entropy/egd/es_egd.cpp new file mode 100644 index 0000000..9e37f8f --- /dev/null +++ b/botan/src/entropy/egd/es_egd.cpp @@ -0,0 +1,156 @@ +/* +* EGD EntropySource +* (C) 1999-2009 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/es_egd.h> +#include <botan/parsing.h> +#include <botan/exceptn.h> +#include <cstring> +#include <stdexcept> + +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <unistd.h> + +#include <sys/socket.h> +#include <sys/un.h> + +#ifndef PF_LOCAL + #define PF_LOCAL PF_UNIX +#endif + +namespace Botan { + +EGD_EntropySource::EGD_Socket::EGD_Socket(const std::string& path) : + socket_path(path), m_fd(-1) + { + } + +/** +* Attempt a connection to an EGD/PRNGD socket +*/ +int EGD_EntropySource::EGD_Socket::open_socket(const std::string& path) + { + int fd = ::socket(PF_LOCAL, SOCK_STREAM, 0); + + if(fd >= 0) + { + sockaddr_un addr; + std::memset(&addr, 0, sizeof(addr)); + addr.sun_family = PF_LOCAL; + + if(sizeof(addr.sun_path) < path.length() + 1) + throw std::invalid_argument("EGD socket path is too long"); + + std::strcpy(addr.sun_path, path.c_str()); + + int len = sizeof(addr.sun_family) + std::strlen(addr.sun_path) + 1; + + if(::connect(fd, reinterpret_cast<struct ::sockaddr*>(&addr), len) < 0) + { + ::close(fd); + fd = -1; + } + } + + return fd; + } + +/** +* Attempt to read entropy from EGD +*/ +u32bit EGD_EntropySource::EGD_Socket::read(byte outbuf[], u32bit length) + { + if(length == 0) + return 0; + + if(m_fd < 0) + { + m_fd = open_socket(socket_path); + if(m_fd < 0) + return 0; + } + + try + { + // 1 == EGD command for non-blocking read + byte egd_read_command[2] = { + 1, static_cast<byte>(std::min<u32bit>(length, 255)) }; + + if(::write(m_fd, egd_read_command, 2) != 2) + throw std::runtime_error("Writing entropy read command to EGD failed"); + + byte out_len = 0; + if(::read(m_fd, &out_len, 1) != 1) + throw std::runtime_error("Reading response length from EGD failed"); + + if(out_len > egd_read_command[1]) + throw std::runtime_error("Bogus length field recieved from EGD"); + + ssize_t count = ::read(m_fd, outbuf, out_len); + + if(count != out_len) + throw std::runtime_error("Reading entropy result from EGD failed"); + + return static_cast<u32bit>(count); + } + catch(std::exception) + { + this->close(); + // Will attempt to reopen next poll + } + + return 0; + } + +void EGD_EntropySource::EGD_Socket::close() + { + if(m_fd > 0) + { + ::close(m_fd); + m_fd = -1; + } + } + +/** +* EGD_EntropySource constructor +*/ +EGD_EntropySource::EGD_EntropySource(const std::vector<std::string>& paths) + { + for(size_t i = 0; i != paths.size(); ++i) + sockets.push_back(EGD_Socket(paths[i])); + } + +EGD_EntropySource::~EGD_EntropySource() + { + for(size_t i = 0; i != sockets.size(); ++i) + sockets[i].close(); + sockets.clear(); + } + +/** +* Gather Entropy from EGD +*/ +void EGD_EntropySource::poll(Entropy_Accumulator& accum) + { + u32bit go_get = std::min<u32bit>(accum.desired_remaining_bits() / 8, 32); + + MemoryRegion<byte>& io_buffer = accum.get_io_buffer(go_get); + + for(size_t i = 0; i != sockets.size(); ++i) + { + u32bit got = sockets[i].read(io_buffer.begin(), io_buffer.size()); + + if(got) + { + accum.add(io_buffer.begin(), got, 8); + break; + } + } + } + +} diff --git a/botan/src/entropy/egd/es_egd.h b/botan/src/entropy/egd/es_egd.h new file mode 100644 index 0000000..5db6565 --- /dev/null +++ b/botan/src/entropy/egd/es_egd.h @@ -0,0 +1,49 @@ +/** +* EGD EntropySource +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ENTROPY_SRC_EGD_H__ +#define BOTAN_ENTROPY_SRC_EGD_H__ + +#include <botan/entropy_src.h> +#include <string> +#include <vector> + +namespace Botan { + +/** +* EGD Entropy Source +*/ +class BOTAN_DLL EGD_EntropySource : public EntropySource + { + public: + std::string name() const { return "EGD/PRNGD"; } + + void poll(Entropy_Accumulator& accum); + + EGD_EntropySource(const std::vector<std::string>&); + ~EGD_EntropySource(); + private: + class EGD_Socket + { + public: + EGD_Socket(const std::string& path); + + void close(); + u32bit read(byte outbuf[], u32bit length); + private: + static int open_socket(const std::string& path); + + std::string socket_path; + int m_fd; // cached fd + }; + + std::vector<EGD_Socket> sockets; + }; + +} + +#endif diff --git a/botan/src/entropy/egd/info.txt b/botan/src/entropy/egd/info.txt new file mode 100644 index 0000000..85ba86c --- /dev/null +++ b/botan/src/entropy/egd/info.txt @@ -0,0 +1,32 @@ +realname "EGD Entropy Source" + +define ENTROPY_SRC_EGD + +load_on auto +modset unix + +<add> +es_egd.h +es_egd.cpp +</add> + +<libs> +solaris -> socket +qnx -> socket +</libs> + +<os> +aix +cygwin +darwin +freebsd +dragonfly +hpux +irix +linux +netbsd +openbsd +qnx +solaris +tru64 +</os> diff --git a/botan/src/entropy/entropy_src.h b/botan/src/entropy/entropy_src.h new file mode 100644 index 0000000..a1a53fa --- /dev/null +++ b/botan/src/entropy/entropy_src.h @@ -0,0 +1,95 @@ +/** +* EntropySource +* (C) 2008-2009 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ENTROPY_SOURCE_BASE_H__ +#define BOTAN_ENTROPY_SOURCE_BASE_H__ + +#include <botan/buf_comp.h> +#include <string> +#include <utility> + +namespace Botan { + +/** +* Class used to accumulate the poll results of EntropySources +*/ +class Entropy_Accumulator + { + public: + Entropy_Accumulator(u32bit goal) : + entropy_goal(goal), collected_bits(0) {} + + virtual ~Entropy_Accumulator() {} + + /** + @return cached I/O buffer for repeated polls + */ + MemoryRegion<byte>& get_io_buffer(u32bit size) + { io_buffer.create(size); return io_buffer; } + + u32bit bits_collected() const + { return static_cast<u32bit>(collected_bits); } + + bool polling_goal_achieved() const + { return (collected_bits >= entropy_goal); } + + u32bit desired_remaining_bits() const + { + if(collected_bits >= entropy_goal) + return 0; + return static_cast<u32bit>(entropy_goal - collected_bits); + } + + void add(const void* bytes, u32bit length, double entropy_bits_per_byte) + { + add_bytes(reinterpret_cast<const byte*>(bytes), length); + collected_bits += entropy_bits_per_byte * length; + } + + template<typename T> + void add(const T& v, double entropy_bits_per_byte) + { + add(&v, sizeof(T), entropy_bits_per_byte); + } + private: + virtual void add_bytes(const byte bytes[], u32bit length) = 0; + + SecureVector<byte> io_buffer; + u32bit entropy_goal; + double collected_bits; + }; + +class Entropy_Accumulator_BufferedComputation : public Entropy_Accumulator + { + public: + Entropy_Accumulator_BufferedComputation(BufferedComputation& sink, + u32bit goal) : + Entropy_Accumulator(goal), entropy_sink(sink) {} + + private: + virtual void add_bytes(const byte bytes[], u32bit length) + { + entropy_sink.update(bytes, length); + } + + BufferedComputation& entropy_sink; + }; + +/** +* Abstract interface to a source of (hopefully unpredictable) system entropy +*/ +class BOTAN_DLL EntropySource + { + public: + virtual std::string name() const = 0; + virtual void poll(Entropy_Accumulator& accum) = 0; + virtual ~EntropySource() {} + }; + +} + +#endif diff --git a/botan/src/entropy/info.txt b/botan/src/entropy/info.txt new file mode 100644 index 0000000..ec3be5f --- /dev/null +++ b/botan/src/entropy/info.txt @@ -0,0 +1,11 @@ +realname "Entropy Sources" + +load_on auto + +<add> +entropy_src.h +</add> + +<requires> +buf_comp +</requires> diff --git a/botan/src/entropy/proc_walk/es_ftw.cpp b/botan/src/entropy/proc_walk/es_ftw.cpp new file mode 100644 index 0000000..2016f09 --- /dev/null +++ b/botan/src/entropy/proc_walk/es_ftw.cpp @@ -0,0 +1,146 @@ +/* +* FTW EntropySource +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/es_ftw.h> +#include <botan/secmem.h> +#include <cstring> +#include <deque> + +#ifndef _POSIX_C_SOURCE + #define _POSIX_C_SOURCE 199309 +#endif + +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> +#include <dirent.h> +#include <fcntl.h> + +namespace Botan { + +namespace { + +class Directory_Walker : public FTW_EntropySource::File_Descriptor_Source + { + public: + Directory_Walker(const std::string& root) { add_directory(root); } + ~Directory_Walker(); + + int next_fd(); + private: + void add_directory(const std::string&); + + std::deque<std::pair<DIR*, std::string> > dirs; + }; + +void Directory_Walker::add_directory(const std::string& dirname) + { + DIR* dir = ::opendir(dirname.c_str()); + if(dir) + dirs.push_back(std::make_pair(dir, dirname)); + } + +Directory_Walker::~Directory_Walker() + { + while(dirs.size()) + { + ::closedir(dirs[0].first); + dirs.pop_front(); + } + } + +int Directory_Walker::next_fd() + { + while(dirs.size()) + { + std::pair<DIR*, std::string> dirinfo = dirs[0]; + + struct dirent* entry = ::readdir(dirinfo.first); + + if(!entry) + { + ::closedir(dirinfo.first); + dirs.pop_front(); + continue; + } + + const std::string filename = entry->d_name; + + if(filename == "." || filename == "..") + continue; + + const std::string full_path = dirinfo.second + '/' + filename; + + struct stat stat_buf; + if(::lstat(full_path.c_str(), &stat_buf) == -1) + continue; + + if(S_ISDIR(stat_buf.st_mode)) + add_directory(full_path); + else if(S_ISREG(stat_buf.st_mode) && (stat_buf.st_mode & S_IROTH)) + { + int fd = ::open(full_path.c_str(), O_RDONLY | O_NOCTTY); + + if(fd > 0) + return fd; + } + } + + return -1; + } + +} + +/** +* FTW_EntropySource Constructor +*/ +FTW_EntropySource::FTW_EntropySource(const std::string& p) : path(p) + { + dir = 0; + } + +/** +* FTW_EntropySource Destructor +*/ +FTW_EntropySource::~FTW_EntropySource() + { + delete dir; + } + +void FTW_EntropySource::poll(Entropy_Accumulator& accum) + { + const u32bit MAX_FILES_READ_PER_POLL = 1024; + + if(!dir) + dir = new Directory_Walker(path); + + MemoryRegion<byte>& io_buffer = accum.get_io_buffer(128); + + for(u32bit i = 0; i != MAX_FILES_READ_PER_POLL; ++i) + { + int fd = dir->next_fd(); + + // If we've exhaused this walk of the directory, halt the poll + if(fd == -1) + { + delete dir; + dir = 0; + break; + } + + ssize_t got = ::read(fd, io_buffer.begin(), io_buffer.size()); + ::close(fd); + + if(got > 0) + accum.add(io_buffer.begin(), got, .01); + + if(accum.polling_goal_achieved()) + break; + } + } + +} diff --git a/botan/src/entropy/proc_walk/es_ftw.h b/botan/src/entropy/proc_walk/es_ftw.h new file mode 100644 index 0000000..928a7b1 --- /dev/null +++ b/botan/src/entropy/proc_walk/es_ftw.h @@ -0,0 +1,42 @@ +/* +* File Tree Walking EntropySource +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ENTROPY_SRC_FTW_H__ +#define BOTAN_ENTROPY_SRC_FTW_H__ + +#include <botan/entropy_src.h> + +namespace Botan { + +/** +* File Tree Walking Entropy Source +*/ +class BOTAN_DLL FTW_EntropySource : public EntropySource + { + public: + std::string name() const { return "Proc Walker"; } + + void poll(Entropy_Accumulator& accum); + + FTW_EntropySource(const std::string& root_dir); + ~FTW_EntropySource(); + + class File_Descriptor_Source + { + public: + virtual int next_fd() = 0; + virtual ~File_Descriptor_Source() {} + }; + private: + + std::string path; + File_Descriptor_Source* dir; + }; + +} + +#endif diff --git a/botan/src/entropy/proc_walk/info.txt b/botan/src/entropy/proc_walk/info.txt new file mode 100644 index 0000000..db96ccd --- /dev/null +++ b/botan/src/entropy/proc_walk/info.txt @@ -0,0 +1,34 @@ +realname "File Tree Walking Entropy Source" + +define ENTROPY_SRC_FTW + +load_on auto +modset unix + +<add> +es_ftw.h +es_ftw.cpp +</add> + +<os> +aix +cygwin +darwin +freebsd +dragonfly +hpux +irix +linux +openbsd +qnx +solaris +tru64 + +# Doesn't build on 2.0.2/x86 due to libc/libstdc++ header issues; no +# big deal since it has /dev/*random +#netbsd +</os> + +<requires> +alloc +</requires> diff --git a/botan/src/entropy/unix_procs/es_unix.cpp b/botan/src/entropy/unix_procs/es_unix.cpp new file mode 100644 index 0000000..fc5b026 --- /dev/null +++ b/botan/src/entropy/unix_procs/es_unix.cpp @@ -0,0 +1,114 @@ +/* +* Unix EntropySource +* (C) 1999-2009 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/es_unix.h> +#include <botan/unix_cmd.h> +#include <botan/parsing.h> +#include <algorithm> +#include <sys/time.h> +#include <sys/stat.h> +#include <sys/resource.h> +#include <unistd.h> + +namespace Botan { + +namespace { + +/** +* Sort ordering by priority +*/ +bool Unix_Program_Cmp(const Unix_Program& a, const Unix_Program& b) + { return (a.priority < b.priority); } + +} + +/** +* Unix_EntropySource Constructor +*/ +Unix_EntropySource::Unix_EntropySource(const std::vector<std::string>& path) : + PATH(path) + { + add_default_sources(sources); + } + +/** +* Add sources to the list +*/ +void Unix_EntropySource::add_sources(const Unix_Program srcs[], u32bit count) + { + sources.insert(sources.end(), srcs, srcs + count); + std::sort(sources.begin(), sources.end(), Unix_Program_Cmp); + } + +/** +* Poll for entropy on a generic Unix system, first by grabbing various +* statistics (stat on common files, getrusage, etc), and then, if more +* is required, by exec'ing various programs like uname and rpcinfo and +* reading the output. +*/ +void Unix_EntropySource::poll(Entropy_Accumulator& accum) + { + const char* stat_targets[] = { + "/", + "/tmp", + "/var/tmp", + "/usr", + "/home", + "/etc/passwd", + ".", + "..", + 0 }; + + for(u32bit j = 0; stat_targets[j]; j++) + { + struct stat statbuf; + clear_mem(&statbuf, 1); + ::stat(stat_targets[j], &statbuf); + accum.add(&statbuf, sizeof(statbuf), .005); + } + + accum.add(::getpid(), 0); + accum.add(::getppid(), 0); + accum.add(::getuid(), 0); + accum.add(::geteuid(), 0); + accum.add(::getegid(), 0); + accum.add(::getpgrp(), 0); + accum.add(::getsid(0), 0); + + struct ::rusage usage; + ::getrusage(RUSAGE_SELF, &usage); + accum.add(usage, .005); + + ::getrusage(RUSAGE_CHILDREN, &usage); + accum.add(usage, .005); + + const u32bit MINIMAL_WORKING = 16; + + MemoryRegion<byte>& io_buffer = accum.get_io_buffer(DEFAULT_BUFFERSIZE); + + for(u32bit j = 0; j != sources.size(); j++) + { + DataSource_Command pipe(sources[j].name_and_args, PATH); + + u32bit got_from_src = 0; + + while(!pipe.end_of_data()) + { + u32bit got_this_loop = pipe.read(io_buffer, io_buffer.size()); + got_from_src += got_this_loop; + + accum.add(io_buffer.begin(), got_this_loop, .005); + } + + sources[j].working = (got_from_src >= MINIMAL_WORKING) ? true : false; + + if(accum.polling_goal_achieved()) + break; + } + } + +} diff --git a/botan/src/entropy/unix_procs/es_unix.h b/botan/src/entropy/unix_procs/es_unix.h new file mode 100644 index 0000000..1f8abb7 --- /dev/null +++ b/botan/src/entropy/unix_procs/es_unix.h @@ -0,0 +1,39 @@ +/* +* Unix EntropySource +* (C) 1999-2009 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ENTROPY_SRC_UNIX_H__ +#define BOTAN_ENTROPY_SRC_UNIX_H__ + +#include <botan/entropy_src.h> +#include <botan/unix_cmd.h> +#include <vector> + +namespace Botan { + +/** +* Unix Entropy Source +*/ +class BOTAN_DLL Unix_EntropySource : public EntropySource + { + public: + std::string name() const { return "Unix Entropy Source"; } + + void poll(Entropy_Accumulator& accum); + + void add_sources(const Unix_Program[], u32bit); + Unix_EntropySource(const std::vector<std::string>& path); + private: + static void add_default_sources(std::vector<Unix_Program>&); + void fast_poll(Entropy_Accumulator& accum); + + const std::vector<std::string> PATH; + std::vector<Unix_Program> sources; + }; + +} + +#endif diff --git a/botan/src/entropy/unix_procs/info.txt b/botan/src/entropy/unix_procs/info.txt new file mode 100644 index 0000000..928ec13 --- /dev/null +++ b/botan/src/entropy/unix_procs/info.txt @@ -0,0 +1,33 @@ +realname "Generic Unix Entropy Source" + +define ENTROPY_SRC_UNIX +modset unix,beos + +load_on auto + +<add> +es_unix.cpp +unix_src.cpp +unix_cmd.cpp +es_unix.h +unix_cmd.h +</add> + +<os> +aix +beos +cygwin +darwin +#freebsd +hpux +irix +linux +netbsd +qnx +solaris +tru64 +</os> + +<requires> +filters +</requires> diff --git a/botan/src/entropy/unix_procs/unix_cmd.cpp b/botan/src/entropy/unix_procs/unix_cmd.cpp new file mode 100644 index 0000000..1cae07e --- /dev/null +++ b/botan/src/entropy/unix_procs/unix_cmd.cpp @@ -0,0 +1,236 @@ +/* +* Unix Command Execution +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/unix_cmd.h> +#include <botan/parsing.h> +#include <botan/exceptn.h> + +#include <sys/time.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <stdlib.h> +#include <unistd.h> +#include <signal.h> + +namespace Botan { + +namespace { + +/** +* Attempt to execute the command +*/ +void do_exec(const std::vector<std::string>& arg_list, + const std::vector<std::string>& paths) + { + const u32bit args = arg_list.size() - 1; + + const char* arg1 = (args >= 1) ? arg_list[1].c_str() : 0; + const char* arg2 = (args >= 2) ? arg_list[2].c_str() : 0; + const char* arg3 = (args >= 3) ? arg_list[3].c_str() : 0; + const char* arg4 = (args >= 4) ? arg_list[4].c_str() : 0; + + for(u32bit j = 0; j != paths.size(); j++) + { + const std::string full_path = paths[j] + "/" + arg_list[0]; + const char* fsname = full_path.c_str(); + ::execl(fsname, fsname, arg1, arg2, arg3, arg4, NULL); + } + } + +} + +/** +* Local information about the pipe +*/ +struct pipe_wrapper + { + int fd; + pid_t pid; + pipe_wrapper() { fd = -1; pid = 0; } + }; + +/** +* Read from the pipe +*/ +u32bit DataSource_Command::read(byte buf[], u32bit length) + { + if(end_of_data()) + return 0; + + fd_set set; + FD_ZERO(&set); + FD_SET(pipe->fd, &set); + + struct ::timeval tv; + tv.tv_sec = 0; + tv.tv_usec = MAX_BLOCK_USECS; + + ssize_t got = 0; + if(::select(pipe->fd + 1, &set, 0, 0, &tv) == 1) + { + if(FD_ISSET(pipe->fd, &set)) + got = ::read(pipe->fd, buf, length); + } + + if(got <= 0) + { + shutdown_pipe(); + return 0; + } + + return static_cast<u32bit>(got); + } + +/** +* Peek at the pipe contents +*/ +u32bit DataSource_Command::peek(byte[], u32bit, u32bit) const + { + if(end_of_data()) + throw Invalid_State("DataSource_Command: Cannot peek when out of data"); + throw Stream_IO_Error("Cannot peek/seek on a command pipe"); + } + +/** +* Check if we reached EOF +*/ +bool DataSource_Command::end_of_data() const + { + return (pipe) ? false : true; + } + +/** +* Return the Unix file descriptor of the pipe +*/ +int DataSource_Command::fd() const + { + if(!pipe) + return -1; + return pipe->fd; + } + +/** +* Return a human-readable ID for this stream +*/ +std::string DataSource_Command::id() const + { + return "Unix command: " + arg_list[0]; + } + +/** +* Create the pipe +*/ +void DataSource_Command::create_pipe(const std::vector<std::string>& paths) + { + bool found_something = false; + for(u32bit j = 0; j != paths.size(); j++) + { + const std::string full_path = paths[j] + "/" + arg_list[0]; + if(::access(full_path.c_str(), X_OK) == 0) + { + found_something = true; + break; + } + } + if(!found_something) + return; + + int pipe_fd[2]; + if(::pipe(pipe_fd) != 0) + return; + + pid_t pid = ::fork(); + + if(pid == -1) + { + ::close(pipe_fd[0]); + ::close(pipe_fd[1]); + } + else if(pid > 0) + { + pipe = new pipe_wrapper; + pipe->fd = pipe_fd[0]; + pipe->pid = pid; + ::close(pipe_fd[1]); + } + else + { + if(dup2(pipe_fd[1], STDOUT_FILENO) == -1) + ::exit(127); + if(close(pipe_fd[0]) != 0 || close(pipe_fd[1]) != 0) + ::exit(127); + if(close(STDERR_FILENO) != 0) + ::exit(127); + + do_exec(arg_list, paths); + ::exit(127); + } + } + +/** +* Shutdown the pipe +*/ +void DataSource_Command::shutdown_pipe() + { + if(pipe) + { + pid_t reaped = waitpid(pipe->pid, 0, WNOHANG); + + if(reaped == 0) + { + kill(pipe->pid, SIGTERM); + + struct ::timeval tv; + tv.tv_sec = 0; + tv.tv_usec = KILL_WAIT; + select(0, 0, 0, 0, &tv); + + reaped = ::waitpid(pipe->pid, 0, WNOHANG); + + if(reaped == 0) + { + ::kill(pipe->pid, SIGKILL); + do + reaped = ::waitpid(pipe->pid, 0, 0); + while(reaped == -1); + } + } + + ::close(pipe->fd); + delete pipe; + pipe = 0; + } + } + +/** +* DataSource_Command Constructor +*/ +DataSource_Command::DataSource_Command(const std::string& prog_and_args, + const std::vector<std::string>& paths) : + MAX_BLOCK_USECS(100000), KILL_WAIT(10000) + { + arg_list = split_on(prog_and_args, ' '); + + if(arg_list.size() == 0) + throw Invalid_Argument("DataSource_Command: No command given"); + if(arg_list.size() > 5) + throw Invalid_Argument("DataSource_Command: Too many args"); + + pipe = 0; + create_pipe(paths); + } + +/** +* DataSource_Command Destructor +*/ +DataSource_Command::~DataSource_Command() + { + if(!end_of_data()) + shutdown_pipe(); + } + +} diff --git a/botan/src/entropy/unix_procs/unix_cmd.h b/botan/src/entropy/unix_procs/unix_cmd.h new file mode 100644 index 0000000..aec1891 --- /dev/null +++ b/botan/src/entropy/unix_procs/unix_cmd.h @@ -0,0 +1,59 @@ +/** +* Unix Command Execution +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_UNIX_CMD_H__ +#define BOTAN_UNIX_CMD_H__ + +#include <botan/types.h> +#include <botan/data_src.h> +#include <string> +#include <vector> + +namespace Botan { + +/** +* Unix Program Info +*/ +struct Unix_Program + { + Unix_Program(const char* n, u32bit p) + { name_and_args = n; priority = p; working = true; } + + std::string name_and_args; + u32bit priority; + bool working; + }; + +/** +* Command Output DataSource +*/ +class BOTAN_DLL DataSource_Command : public DataSource + { + public: + u32bit read(byte[], u32bit); + u32bit peek(byte[], u32bit, u32bit) const; + bool end_of_data() const; + std::string id() const; + + int fd() const; + + DataSource_Command(const std::string&, + const std::vector<std::string>& paths); + ~DataSource_Command(); + private: + void create_pipe(const std::vector<std::string>&); + void shutdown_pipe(); + + const u32bit MAX_BLOCK_USECS, KILL_WAIT; + + std::vector<std::string> arg_list; + struct pipe_wrapper* pipe; + }; + +} + +#endif diff --git a/botan/src/entropy/unix_procs/unix_src.cpp b/botan/src/entropy/unix_procs/unix_src.cpp new file mode 100644 index 0000000..c843af6 --- /dev/null +++ b/botan/src/entropy/unix_procs/unix_src.cpp @@ -0,0 +1,61 @@ +/* +* Program List for Unix_EntropySource +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/es_unix.h> + +namespace Botan { + +/** +* Default Commands for Entropy Gathering +*/ +void Unix_EntropySource::add_default_sources(std::vector<Unix_Program>& srcs) + { + srcs.push_back(Unix_Program("vmstat", 1)); + srcs.push_back(Unix_Program("vmstat -s", 1)); + srcs.push_back(Unix_Program("pfstat", 1)); + srcs.push_back(Unix_Program("netstat -in", 1)); + + srcs.push_back(Unix_Program("iostat", 2)); + srcs.push_back(Unix_Program("mpstat", 2)); + srcs.push_back(Unix_Program("nfsstat", 2)); + srcs.push_back(Unix_Program("portstat", 2)); + srcs.push_back(Unix_Program("arp -a -n", 2)); + srcs.push_back(Unix_Program("ifconfig -a", 2)); + srcs.push_back(Unix_Program("pstat -T", 2)); + srcs.push_back(Unix_Program("pstat -s", 2)); + srcs.push_back(Unix_Program("uname -a", 2)); + srcs.push_back(Unix_Program("uptime", 2)); + srcs.push_back(Unix_Program("ipcs -a", 2)); + srcs.push_back(Unix_Program("procinfo -a", 2)); + + srcs.push_back(Unix_Program("sysinfo", 3)); + srcs.push_back(Unix_Program("listarea", 3)); + srcs.push_back(Unix_Program("listdev", 3)); + + srcs.push_back(Unix_Program("who", 3)); + srcs.push_back(Unix_Program("finger", 3)); + srcs.push_back(Unix_Program("netstat -s", 3)); + srcs.push_back(Unix_Program("netstat -an", 3)); + srcs.push_back(Unix_Program("ps -A", 3)); + srcs.push_back(Unix_Program("mailstats", 3)); + srcs.push_back(Unix_Program("rpcinfo -p localhost", 3)); + + srcs.push_back(Unix_Program("dmesg", 4)); + srcs.push_back(Unix_Program("ls -alni /tmp", 4)); + srcs.push_back(Unix_Program("ls -alni /proc", 4)); + srcs.push_back(Unix_Program("df -l", 4)); + srcs.push_back(Unix_Program("last -5", 4)); + srcs.push_back(Unix_Program("pstat -f", 4)); + + srcs.push_back(Unix_Program("ps aux", 5)); + srcs.push_back(Unix_Program("ps -elf", 5)); + + srcs.push_back(Unix_Program("sar -A", 6)); + srcs.push_back(Unix_Program("lsof", 6)); + } + +} diff --git a/botan/src/entropy/win32_stats/es_win32.cpp b/botan/src/entropy/win32_stats/es_win32.cpp new file mode 100644 index 0000000..a8e9e40 --- /dev/null +++ b/botan/src/entropy/win32_stats/es_win32.cpp @@ -0,0 +1,118 @@ +/** +* Win32 EntropySource +* (C) 1999-2009 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/es_win32.h> +#include <windows.h> +#include <tlhelp32.h> + +namespace Botan { + +/** +* Win32 poll using stats functions including Tooltip32 +*/ +void Win32_EntropySource::poll(Entropy_Accumulator& accum) + { + /* + First query a bunch of basic statistical stuff, though + don't count it for much in terms of contributed entropy. + */ + accum.add(GetTickCount(), 0); + accum.add(GetMessagePos(), 0); + accum.add(GetMessageTime(), 0); + accum.add(GetInputState(), 0); + accum.add(GetCurrentProcessId(), 0); + accum.add(GetCurrentThreadId(), 0); + + SYSTEM_INFO sys_info; + GetSystemInfo(&sys_info); + accum.add(sys_info, 1); + + MEMORYSTATUS mem_info; + GlobalMemoryStatus(&mem_info); + accum.add(mem_info, 1); + + POINT point; + GetCursorPos(&point); + accum.add(point, 1); + + GetCaretPos(&point); + accum.add(point, 1); + + LARGE_INTEGER perf_counter; + QueryPerformanceCounter(&perf_counter); + accum.add(perf_counter, 0); + + /* + Now use the Tooltip library to iterate throug various objects on + the system, including processes, threads, and heap objects. + */ + + HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0); + +#define TOOLHELP32_ITER(DATA_TYPE, FUNC_FIRST, FUNC_NEXT) \ + if(!accum.polling_goal_achieved()) \ + { \ + DATA_TYPE info; \ + info.dwSize = sizeof(DATA_TYPE); \ + if(FUNC_FIRST(snapshot, &info)) \ + { \ + do \ + { \ + accum.add(info, 1); \ + } while(FUNC_NEXT(snapshot, &info)); \ + } \ + } + + TOOLHELP32_ITER(MODULEENTRY32, Module32First, Module32Next); + TOOLHELP32_ITER(PROCESSENTRY32, Process32First, Process32Next); + TOOLHELP32_ITER(THREADENTRY32, Thread32First, Thread32Next); + +#undef TOOLHELP32_ITER + + if(!accum.polling_goal_achieved()) + { + u32bit heap_lists_found = 0; + HEAPLIST32 heap_list; + heap_list.dwSize = sizeof(HEAPLIST32); + + const u32bit HEAP_LISTS_MAX = 32; + const u32bit HEAP_OBJS_PER_LIST = 128; + + if(Heap32ListFirst(snapshot, &heap_list)) + { + do + { + accum.add(heap_list, 1); + + if(++heap_lists_found > HEAP_LISTS_MAX) + break; + + u32bit heap_objs_found = 0; + HEAPENTRY32 heap_entry; + heap_entry.dwSize = sizeof(HEAPENTRY32); + if(Heap32First(&heap_entry, heap_list.th32ProcessID, + heap_list.th32HeapID)) + { + do + { + if(heap_objs_found++ > HEAP_OBJS_PER_LIST) + break; + accum.add(heap_entry, 1); + } while(Heap32Next(&heap_entry)); + } + + if(accum.polling_goal_achieved()) + break; + + } while(Heap32ListNext(snapshot, &heap_list)); + } + } + + CloseHandle(snapshot); + } + +} diff --git a/botan/src/entropy/win32_stats/es_win32.h b/botan/src/entropy/win32_stats/es_win32.h new file mode 100644 index 0000000..0aa9054 --- /dev/null +++ b/botan/src/entropy/win32_stats/es_win32.h @@ -0,0 +1,27 @@ +/** +* Win32 EntropySource +* (C) 1999-2009 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ENTROPY_SRC_WIN32_H__ +#define BOTAN_ENTROPY_SRC_WIN32_H__ + +#include <botan/entropy_src.h> + +namespace Botan { + +/** +* Win32 Entropy Source +*/ +class BOTAN_DLL Win32_EntropySource : public EntropySource + { + public: + std::string name() const { return "Win32 Statistics"; } + void poll(Entropy_Accumulator& accum); + }; + +} + +#endif diff --git a/botan/src/entropy/win32_stats/info.txt b/botan/src/entropy/win32_stats/info.txt new file mode 100644 index 0000000..ca71009 --- /dev/null +++ b/botan/src/entropy/win32_stats/info.txt @@ -0,0 +1,24 @@ +realname "Win32 Entropy Source" + +# Probably not much of an issue anymore +#note "This module will not run under NT4" + +define ENTROPY_SRC_WIN32 +modset win32 + +load_on auto + +<add> +es_win32.h +es_win32.cpp +</add> + +<os> +windows +cygwin +mingw +</os> + +<libs> +windows -> user32.lib +</libs> diff --git a/botan/src/filters/algo_filt.cpp b/botan/src/filters/algo_filt.cpp new file mode 100644 index 0000000..23f7a20 --- /dev/null +++ b/botan/src/filters/algo_filt.cpp @@ -0,0 +1,124 @@ +/* +* Filters +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/filters.h> +#include <botan/libstate.h> +#include <algorithm> + +namespace Botan { + +/* +* StreamCipher_Filter Constructor +*/ +StreamCipher_Filter::StreamCipher_Filter(const std::string& sc_name) : + buffer(DEFAULT_BUFFERSIZE) + { + Algorithm_Factory& af = global_state().algorithm_factory(); + base_ptr = cipher = af.make_stream_cipher(sc_name); + } + +/* +* StreamCipher_Filter Constructor +*/ +StreamCipher_Filter::StreamCipher_Filter(StreamCipher* stream_cipher) : + buffer(DEFAULT_BUFFERSIZE) + { + base_ptr = cipher = stream_cipher; + } + +/* +* StreamCipher_Filter Constructor +*/ +StreamCipher_Filter::StreamCipher_Filter(const std::string& sc_name, + const SymmetricKey& key) : + buffer(DEFAULT_BUFFERSIZE) + { + Algorithm_Factory& af = global_state().algorithm_factory(); + base_ptr = cipher = af.make_stream_cipher(sc_name); + cipher->set_key(key); + } + +/* +* Set the IV of a stream cipher +*/ +void StreamCipher_Filter::set_iv(const InitializationVector& iv) + { + cipher->resync(iv.begin(), iv.length()); + } + +/* +* Write data into a StreamCipher_Filter +*/ +void StreamCipher_Filter::write(const byte input[], u32bit length) + { + while(length) + { + u32bit copied = std::min(length, buffer.size()); + cipher->encrypt(input, buffer, copied); + send(buffer, copied); + input += copied; + length -= copied; + } + } + +/* +* Hash_Filter Constructor +*/ +Hash_Filter::Hash_Filter(const std::string& algo_spec, + u32bit len) : + OUTPUT_LENGTH(len) + { + Algorithm_Factory& af = global_state().algorithm_factory(); + hash = af.make_hash_function(algo_spec); + } + +/* +* Complete a calculation by a Hash_Filter +*/ +void Hash_Filter::end_msg() + { + SecureVector<byte> output = hash->final(); + if(OUTPUT_LENGTH) + send(output, std::min(OUTPUT_LENGTH, output.size())); + else + send(output); + } + +/* +* MAC_Filter Constructor +*/ +MAC_Filter::MAC_Filter(const std::string& mac_name, u32bit len) : + OUTPUT_LENGTH(len) + { + Algorithm_Factory& af = global_state().algorithm_factory(); + base_ptr = mac = af.make_mac(mac_name); + } + +/* +* MAC_Filter Constructor +*/ +MAC_Filter::MAC_Filter(const std::string& mac_name, const SymmetricKey& key, + u32bit len) : OUTPUT_LENGTH(len) + { + Algorithm_Factory& af = global_state().algorithm_factory(); + base_ptr = mac = af.make_mac(mac_name); + mac->set_key(key); + } + +/* +* Complete a calculation by a MAC_Filter +*/ +void MAC_Filter::end_msg() + { + SecureVector<byte> output = mac->final(); + if(OUTPUT_LENGTH) + send(output, std::min(OUTPUT_LENGTH, output.size())); + else + send(output); + } + +} diff --git a/botan/src/filters/basefilt.cpp b/botan/src/filters/basefilt.cpp new file mode 100644 index 0000000..02dbd8a --- /dev/null +++ b/botan/src/filters/basefilt.cpp @@ -0,0 +1,74 @@ +/* +* Basic Filters +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/basefilt.h> + +namespace Botan { + +/* +* Chain Constructor +*/ +Chain::Chain(Filter* f1, Filter* f2, Filter* f3, Filter* f4) + { + if(f1) { attach(f1); incr_owns(); } + if(f2) { attach(f2); incr_owns(); } + if(f3) { attach(f3); incr_owns(); } + if(f4) { attach(f4); incr_owns(); } + } + +/* +* Chain Constructor +*/ +Chain::Chain(Filter* filters[], u32bit count) + { + for(u32bit j = 0; j != count; ++j) + if(filters[j]) + { + attach(filters[j]); + incr_owns(); + } + } + +/* +* Fork Constructor +*/ +Fork::Fork(Filter* f1, Filter* f2, Filter* f3, Filter* f4) + { + Filter* filters[4] = { f1, f2, f3, f4 }; + set_next(filters, 4); + } + +/* +* Fork Constructor +*/ +Fork::Fork(Filter* filters[], u32bit count) + { + set_next(filters, count); + } + +/* +* Set the algorithm key +*/ +void Keyed_Filter::set_key(const SymmetricKey& key) + { + if(base_ptr) + base_ptr->set_key(key); + else + throw Invalid_State("Keyed_Filter::set_key: No base algorithm set"); + } + +/* +* Check if a keylength is valid +*/ +bool Keyed_Filter::valid_keylength(u32bit n) const + { + if(base_ptr) + return base_ptr->valid_keylength(n); + throw Invalid_State("Keyed_Filter::valid_keylength: No base algorithm set"); + } + +} diff --git a/botan/src/filters/basefilt.h b/botan/src/filters/basefilt.h new file mode 100644 index 0000000..75625ab --- /dev/null +++ b/botan/src/filters/basefilt.h @@ -0,0 +1,99 @@ +/* +* Basic Filters +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_BASEFILT_H__ +#define BOTAN_BASEFILT_H__ + +#include <botan/filter.h> +#include <botan/sym_algo.h> + +namespace Botan { + +/** +* This class represents Filter chains. A Filter chain is an ordered +* concatenation of Filters, the input to a Chain sequentially passes +* through all the Filters contained in the Chain. +*/ + +class BOTAN_DLL Chain : public Fanout_Filter + { + public: + void write(const byte input[], u32bit length) { send(input, length); } + + /** + * Construct a chain of up to four filters. The filters are set + * up in the same order as the arguments. + */ + Chain(Filter* = 0, Filter* = 0, Filter* = 0, Filter* = 0); + + /** + * Construct a chain from range of filters + * @param filter_arr the list of filters + * @param length how many filters + */ + Chain(Filter* filter_arr[], u32bit length); + }; + +/** +* This class represents a fork filter, whose purpose is to fork the +* flow of data. It causes an input message to result in n messages at +* the end of the filter, where n is the number of forks. +*/ +class BOTAN_DLL Fork : public Fanout_Filter + { + public: + void write(const byte input[], u32bit length) { send(input, length); } + void set_port(u32bit n) { Fanout_Filter::set_port(n); } + + /** + * Construct a Fork filter with up to four forks. + */ + Fork(Filter*, Filter*, Filter* = 0, Filter* = 0); + + /** + * Construct a Fork from range of filters + * @param filter_arr the list of filters + * @param length how many filters + */ + Fork(Filter* filter_arr[], u32bit length); + }; + +/** +* This class represents keyed filters, i.e. filters that have to be +* fed with a key in order to function. +*/ +class BOTAN_DLL Keyed_Filter : public Filter + { + public: + + /** + * Set the key of this filter. + * @param key the key to set + */ + virtual void set_key(const SymmetricKey& key); + + /** + * Set the initialization vector of this filter. + * @param iv the initialization vector to set + */ + virtual void set_iv(const InitializationVector&) {} + + /** + * Check whether a key length is valid for this filter. + * @param length the key length to be checked for validity + * @return true if the key length is valid, false otherwise + */ + virtual bool valid_keylength(u32bit length) const; + + Keyed_Filter() { base_ptr = 0; } + protected: + SymmetricAlgorithm* base_ptr; + }; + +} + +#endif diff --git a/botan/src/filters/buf_filt.cpp b/botan/src/filters/buf_filt.cpp new file mode 100644 index 0000000..53352b5 --- /dev/null +++ b/botan/src/filters/buf_filt.cpp @@ -0,0 +1,71 @@ +/* +* Buffering Filter +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/buf_filt.h> +#include <botan/exceptn.h> +#include <algorithm> + +namespace Botan { + +/* +* Buffering_Filter Constructor +*/ +Buffering_Filter::Buffering_Filter(u32bit b, u32bit i) : INITIAL_BLOCK_SIZE(i), + BLOCK_SIZE(b) + { + initial_block_pos = block_pos = 0; + initial.create(INITIAL_BLOCK_SIZE); + block.create(BLOCK_SIZE); + } + +/* +* Reset the Buffering Filter +*/ +void Buffering_Filter::end_msg() + { + if(initial_block_pos != INITIAL_BLOCK_SIZE) + throw Exception("Buffering_Filter: Not enough data for first block"); + final_block(block, block_pos); + initial_block_pos = block_pos = 0; + initial.clear(); + block.clear(); + } + +/* +* Buffer input into blocks +*/ +void Buffering_Filter::write(const byte input[], u32bit length) + { + if(initial_block_pos != INITIAL_BLOCK_SIZE) + { + u32bit copied = std::min(INITIAL_BLOCK_SIZE - initial_block_pos, length); + initial.copy(initial_block_pos, input, copied); + input += copied; + length -= copied; + initial_block_pos += copied; + if(initial_block_pos == INITIAL_BLOCK_SIZE) + initial_block(initial); + } + block.copy(block_pos, input, length); + if(block_pos + length >= BLOCK_SIZE) + { + main_block(block); + input += (BLOCK_SIZE - block_pos); + length -= (BLOCK_SIZE - block_pos); + while(length >= BLOCK_SIZE) + { + main_block(input); + input += BLOCK_SIZE; + length -= BLOCK_SIZE; + } + block.copy(input, length); + block_pos = 0; + } + block_pos += length; + } + +} diff --git a/botan/src/filters/buf_filt.h b/botan/src/filters/buf_filt.h new file mode 100644 index 0000000..ce3dbc9 --- /dev/null +++ b/botan/src/filters/buf_filt.h @@ -0,0 +1,39 @@ +/* +* Buffering Filter +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_BUFFERING_FILTER_H__ +#define BOTAN_BUFFERING_FILTER_H__ + +#include <botan/filter.h> + +namespace Botan { + +/** +* Buffering_Filter: This class represents filters for operations that +* maintain an internal state. +*/ + +class BOTAN_DLL Buffering_Filter : public Filter + { + public: + void write(const byte[], u32bit); + virtual void end_msg(); + Buffering_Filter(u32bit, u32bit = 0); + virtual ~Buffering_Filter() {} + protected: + virtual void initial_block(const byte[]) {} + virtual void main_block(const byte[]) = 0; + virtual void final_block(const byte[], u32bit) = 0; + private: + const u32bit INITIAL_BLOCK_SIZE, BLOCK_SIZE; + SecureVector<byte> initial, block; + u32bit initial_block_pos, block_pos; + }; + +} + +#endif diff --git a/botan/src/filters/data_snk.cpp b/botan/src/filters/data_snk.cpp new file mode 100644 index 0000000..f8ee9f8 --- /dev/null +++ b/botan/src/filters/data_snk.cpp @@ -0,0 +1,62 @@ +/* +* DataSink +* (C) 1999-2007 Jack Lloyd +* 2005 Matthew Gregan +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/data_snk.h> +#include <botan/exceptn.h> +#include <fstream> + +namespace Botan { + +/* +* Write to a stream +*/ +void DataSink_Stream::write(const byte out[], u32bit length) + { + sink->write(reinterpret_cast<const char*>(out), length); + if(!sink->good()) + throw Stream_IO_Error("DataSink_Stream: Failure writing to " + + identifier); + } + +/* +* DataSink_Stream Constructor +*/ +DataSink_Stream::DataSink_Stream(std::ostream& out, + const std::string& name) : + identifier(name != "" ? name : "<std::ostream>"), owner(false) + { + sink = &out; + } + +/* +* DataSink_Stream Constructor +*/ +DataSink_Stream::DataSink_Stream(const std::string& path, + bool use_binary) : + identifier(path), owner(true) + { + if(use_binary) + sink = new std::ofstream(path.c_str(), std::ios::binary); + else + sink = new std::ofstream(path.c_str()); + + if(!sink->good()) + throw Stream_IO_Error("DataSink_Stream: Failure opening " + path); + } + +/* +* DataSink_Stream Destructor +*/ +DataSink_Stream::~DataSink_Stream() + { + if(owner) + delete sink; + sink = 0; + } + +} diff --git a/botan/src/filters/data_snk.h b/botan/src/filters/data_snk.h new file mode 100644 index 0000000..61ddf6e --- /dev/null +++ b/botan/src/filters/data_snk.h @@ -0,0 +1,65 @@ +/* +* DataSink +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_DATA_SINK_H__ +#define BOTAN_DATA_SINK_H__ + +#include <botan/filter.h> +#include <iosfwd> + +namespace Botan { + +/** +* This class represents abstract data sink objects. +*/ +class BOTAN_DLL DataSink : public Filter + { + public: + bool attachable() { return false; } + DataSink() {} + virtual ~DataSink() {} + private: + DataSink& operator=(const DataSink&) { return (*this); } + DataSink(const DataSink&); + }; + +/** +* This class represents a data sink which writes its output to a stream. +*/ +class BOTAN_DLL DataSink_Stream : public DataSink + { + public: + void write(const byte[], u32bit); + + /** + * Construct a DataSink_Stream from a stream. + * @param stream the stream to write to + * @param name identifier + */ + DataSink_Stream(std::ostream& stream, + const std::string& name = ""); + + /** + * Construct a DataSink_Stream from a stream. + * @param file the name of the file to open a stream to + * @param use_binary indicates whether to treat the file + * as a binary file or not + */ + DataSink_Stream(const std::string& filename, + bool use_binary = false); + + ~DataSink_Stream(); + private: + const std::string identifier; + const bool owner; + + std::ostream* sink; + }; + +} + +#endif diff --git a/botan/src/filters/data_src.cpp b/botan/src/filters/data_src.cpp new file mode 100644 index 0000000..e6387c4 --- /dev/null +++ b/botan/src/filters/data_src.cpp @@ -0,0 +1,207 @@ +/* +* DataSource +* (C) 1999-2007 Jack Lloyd +* 2005 Matthew Gregan +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/data_src.h> +#include <botan/exceptn.h> + +#include <fstream> +#include <algorithm> + +namespace Botan { + +/* +* Read a single byte from the DataSource +*/ +u32bit DataSource::read_byte(byte& out) + { + return read(&out, 1); + } + +/* +* Peek a single byte from the DataSource +*/ +u32bit DataSource::peek_byte(byte& out) const + { + return peek(&out, 1, 0); + } + +/* +* Discard the next N bytes of the data +*/ +u32bit DataSource::discard_next(u32bit n) + { + u32bit discarded = 0; + byte dummy; + for(u32bit j = 0; j != n; ++j) + discarded += read_byte(dummy); + return discarded; + } + +/* +* Read from a memory buffer +*/ +u32bit DataSource_Memory::read(byte out[], u32bit length) + { + u32bit got = std::min(source.size() - offset, length); + copy_mem(out, source + offset, got); + offset += got; + return got; + } + +/* +* Peek into a memory buffer +*/ +u32bit DataSource_Memory::peek(byte out[], u32bit length, + u32bit peek_offset) const + { + const u32bit bytes_left = source.size() - offset; + if(peek_offset >= bytes_left) return 0; + + u32bit got = std::min(bytes_left - peek_offset, length); + copy_mem(out, source + offset + peek_offset, got); + return got; + } + +/* +* Check if the memory buffer is empty +*/ +bool DataSource_Memory::end_of_data() const + { + return (offset == source.size()); + } + +/* +* DataSource_Memory Constructor +*/ +DataSource_Memory::DataSource_Memory(const byte in[], u32bit length) + { + source.set(in, length); + offset = 0; + } + +/* +* DataSource_Memory Constructor +*/ +DataSource_Memory::DataSource_Memory(const MemoryRegion<byte>& in) + { + source = in; + offset = 0; + } + +/* +* DataSource_Memory Constructor +*/ +DataSource_Memory::DataSource_Memory(const std::string& in) + { + source.set(reinterpret_cast<const byte*>(in.data()), in.length()); + offset = 0; + } + +/* +* Read from a stream +*/ +u32bit DataSource_Stream::read(byte out[], u32bit length) + { + source->read(reinterpret_cast<char*>(out), length); + if(source->bad()) + throw Stream_IO_Error("DataSource_Stream::read: Source failure"); + + u32bit got = source->gcount(); + total_read += got; + return got; + } + +/* +* Peek into a stream +*/ +u32bit DataSource_Stream::peek(byte out[], u32bit length, u32bit offset) const + { + if(end_of_data()) + throw Invalid_State("DataSource_Stream: Cannot peek when out of data"); + + u32bit got = 0; + + if(offset) + { + SecureVector<byte> buf(offset); + source->read(reinterpret_cast<char*>(buf.begin()), buf.size()); + if(source->bad()) + throw Stream_IO_Error("DataSource_Stream::peek: Source failure"); + got = source->gcount(); + } + + if(got == offset) + { + source->read(reinterpret_cast<char*>(out), length); + if(source->bad()) + throw Stream_IO_Error("DataSource_Stream::peek: Source failure"); + got = source->gcount(); + } + + if(source->eof()) + source->clear(); + source->seekg(total_read, std::ios::beg); + + return got; + } + +/* +* Check if the stream is empty or in error +*/ +bool DataSource_Stream::end_of_data() const + { + return (!source->good()); + } + +/* +* Return a human-readable ID for this stream +*/ +std::string DataSource_Stream::id() const + { + return identifier; + } + +/* +* DataSource_Stream Constructor +*/ +DataSource_Stream::DataSource_Stream(const std::string& path, + bool use_binary) : + identifier(path), owner(true) + { + if(use_binary) + source = new std::ifstream(path.c_str(), std::ios::binary); + else + source = new std::ifstream(path.c_str()); + + if(!source->good()) + throw Stream_IO_Error("DataSource: Failure opening file " + path); + + total_read = 0; + } + +/* +* DataSource_Stream Constructor +*/ +DataSource_Stream::DataSource_Stream(std::istream& in, + const std::string& name) : + identifier(name), owner(false) + { + source = ∈ + total_read = 0; + } + +/* +* DataSource_Stream Destructor +*/ +DataSource_Stream::~DataSource_Stream() + { + if(owner) + delete source; + } + +} diff --git a/botan/src/filters/data_src.h b/botan/src/filters/data_src.h new file mode 100644 index 0000000..e16217e --- /dev/null +++ b/botan/src/filters/data_src.h @@ -0,0 +1,150 @@ +/* +* DataSource +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_DATA_SRC_H__ +#define BOTAN_DATA_SRC_H__ + +#include <botan/secmem.h> +#include <string> +#include <iosfwd> + +namespace Botan { + +/** +* This class represents an abstract data source object. +*/ +class BOTAN_DLL DataSource + { + public: + /** + * Read from the source. Moves the internal offset so that + * every call to read will return a new portion of the source. + * @param out the byte array to write the result to + * @param length the length of the byte array out + * @return the length in bytes that was actually read and put + * into out + */ + virtual u32bit read(byte out[], u32bit length) = 0; + + /** + * Read from the source but do not modify the internal offset. Consecutive + * calls to peek() will return portions of the source starting at the same + * position. + * @param out the byte array to write the output to + * @param length the length of the byte array out + * @return the length in bytes that was actually read and put + * into out + */ + virtual u32bit peek(byte out[], u32bit length, + u32bit peek_offset) const = 0; + + /** + * Test whether the source still has data that can be read. + * @return true if there is still data to read, false otherwise + */ + virtual bool end_of_data() const = 0; + /** + * return the id of this data source + * @return the std::string representing the id of this data source + */ + virtual std::string id() const { return ""; } + + /** + * Read one byte. + * @param the byte to read to + * @return the length in bytes that was actually read and put + * into out + */ + u32bit read_byte(byte& out); + + /** + * Peek at one byte. + * @param the byte to read to + * @return the length in bytes that was actually read and put + * into out + */ + u32bit peek_byte(byte& out) const; + + /** + * Discard the next N bytes of the data + * @param N the number of bytes to discard + * @return the number of bytes actually discarded + */ + u32bit discard_next(u32bit N); + + DataSource() {} + virtual ~DataSource() {} + private: + DataSource& operator=(const DataSource&) { return (*this); } + DataSource(const DataSource&); + }; + +/** +* This class represents a Memory-Based DataSource +*/ +class BOTAN_DLL DataSource_Memory : public DataSource + { + public: + u32bit read(byte[], u32bit); + u32bit peek(byte[], u32bit, u32bit) const; + bool end_of_data() const; + + /** + * Construct a memory source that reads from a string + * @param in the string to read from + */ + DataSource_Memory(const std::string& in); + + /** + * Construct a memory source that reads from a byte array + * @param in the byte array to read from + * @param length the length of the byte array + */ + DataSource_Memory(const byte in[], u32bit length); + + /** + * Construct a memory source that reads from a MemoryRegion + * @param in the MemoryRegion to read from + */ + DataSource_Memory(const MemoryRegion<byte>& in); + private: + SecureVector<byte> source; + u32bit offset; + }; + +/** +* This class represents a Stream-Based DataSource. +*/ +class BOTAN_DLL DataSource_Stream : public DataSource + { + public: + u32bit read(byte[], u32bit); + u32bit peek(byte[], u32bit, u32bit) const; + bool end_of_data() const; + std::string id() const; + + DataSource_Stream(std::istream&, const std::string& id = ""); + + /** + * Construct a Stream-Based DataSource from file + * @param file the name of the file + * @param use_binary whether to treat the file as binary or not + */ + DataSource_Stream(const std::string& file, bool use_binary = false); + + ~DataSource_Stream(); + private: + const std::string identifier; + const bool owner; + + std::istream* source; + u32bit total_read; + }; + +} + +#endif diff --git a/botan/src/filters/fd_unix/fd_unix.cpp b/botan/src/filters/fd_unix/fd_unix.cpp new file mode 100644 index 0000000..7f19b0a --- /dev/null +++ b/botan/src/filters/fd_unix/fd_unix.cpp @@ -0,0 +1,53 @@ +/* +* Pipe I/O for Unix +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/pipe.h> +#include <botan/exceptn.h> +#include <unistd.h> + +namespace Botan { + +/* +* Write data from a pipe into a Unix fd +*/ +int operator<<(int fd, Pipe& pipe) + { + SecureVector<byte> buffer(DEFAULT_BUFFERSIZE); + while(pipe.remaining()) + { + u32bit got = pipe.read(buffer, buffer.size()); + u32bit position = 0; + while(got) + { + ssize_t ret = write(fd, buffer + position, got); + if(ret == -1) + throw Stream_IO_Error("Pipe output operator (unixfd) has failed"); + position += ret; + got -= ret; + } + } + return fd; + } + +/* +* Read data from a Unix fd into a pipe +*/ +int operator>>(int fd, Pipe& pipe) + { + SecureVector<byte> buffer(DEFAULT_BUFFERSIZE); + while(true) + { + ssize_t ret = read(fd, buffer, buffer.size()); + if(ret == 0) break; + if(ret == -1) + throw Stream_IO_Error("Pipe input operator (unixfd) has failed"); + pipe.write(buffer, ret); + } + return fd; + } + +} diff --git a/botan/src/filters/fd_unix/fd_unix.h b/botan/src/filters/fd_unix/fd_unix.h new file mode 100644 index 0000000..0aed009 --- /dev/null +++ b/botan/src/filters/fd_unix/fd_unix.h @@ -0,0 +1,23 @@ +/* +* Pipe I/O for Unix +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_PIPE_UNIXFD_H__ +#define BOTAN_PIPE_UNIXFD_H__ + +#include <botan/pipe.h> + +namespace Botan { + +/* +* Unix I/O Operators for Pipe +*/ +int operator<<(int, Pipe&); +int operator>>(int, Pipe&); + +} + +#endif diff --git a/botan/src/filters/fd_unix/info.txt b/botan/src/filters/fd_unix/info.txt new file mode 100644 index 0000000..d87978c --- /dev/null +++ b/botan/src/filters/fd_unix/info.txt @@ -0,0 +1,28 @@ +realname "Unix I/O support for Pipe" + +define PIPE_UNIXFD_IO +modset unix,beos + +load_on auto + +<add> +fd_unix.h +fd_unix.cpp +</add> + +<os> +aix +beos +cygwin +darwin +freebsd +dragonfly +hpux +irix +linux +netbsd +openbsd +qnx +solaris +tru64 +</os> diff --git a/botan/src/filters/filter.cpp b/botan/src/filters/filter.cpp new file mode 100644 index 0000000..4bf0ef9 --- /dev/null +++ b/botan/src/filters/filter.cpp @@ -0,0 +1,127 @@ +/* +* Filter +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/filter.h> +#include <botan/secqueue.h> +#include <botan/exceptn.h> + +namespace Botan { + +/* +* Filter Constructor +*/ +Filter::Filter() + { + next.resize(1); + port_num = 0; + filter_owns = 0; + owned = false; + } + +/* +* Send data to all ports +*/ +void Filter::send(const byte input[], u32bit length) + { + bool nothing_attached = true; + for(u32bit j = 0; j != total_ports(); ++j) + if(next[j]) + { + if(write_queue.has_items()) + next[j]->write(write_queue, write_queue.size()); + next[j]->write(input, length); + nothing_attached = false; + } + if(nothing_attached) + write_queue.append(input, length); + else if(write_queue.has_items()) + write_queue.destroy(); + } + +/* +* Start a new message +*/ +void Filter::new_msg() + { + start_msg(); + for(u32bit j = 0; j != total_ports(); ++j) + if(next[j]) + next[j]->new_msg(); + } + +/* +* End the current message +*/ +void Filter::finish_msg() + { + end_msg(); + for(u32bit j = 0; j != total_ports(); ++j) + if(next[j]) + next[j]->finish_msg(); + } + +/* +* Attach a filter to the current port +*/ +void Filter::attach(Filter* new_filter) + { + if(new_filter) + { + Filter* last = this; + while(last->get_next()) + last = last->get_next(); + last->next[last->current_port()] = new_filter; + } + } + +/* +* Set the active port on a filter +*/ +void Filter::set_port(u32bit new_port) + { + if(new_port >= total_ports()) + throw Invalid_Argument("Filter: Invalid port number"); + port_num = new_port; + } + +/* +* Return the next Filter in the logical chain +*/ +Filter* Filter::get_next() const + { + if(port_num < next.size()) + return next[port_num]; + return 0; + } + +/* +* Set the next Filters +*/ +void Filter::set_next(Filter* filters[], u32bit size) + { + while(size && filters && filters[size-1] == 0) + --size; + + next.clear(); + next.resize(size); + + port_num = 0; + filter_owns = 0; + + for(u32bit j = 0; j != size; ++j) + next[j] = filters[j]; + } + +/* +* Return the total number of ports +*/ +u32bit Filter::total_ports() const + { + return next.size(); + } + +} diff --git a/botan/src/filters/filter.h b/botan/src/filters/filter.h new file mode 100644 index 0000000..b13a366 --- /dev/null +++ b/botan/src/filters/filter.h @@ -0,0 +1,113 @@ +/* +* Filter +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_FILTER_H__ +#define BOTAN_FILTER_H__ + +#include <botan/secmem.h> +#include <vector> + +namespace Botan { + +/** +* This class represents general abstract filter objects. +*/ +class BOTAN_DLL Filter + { + public: + + /** + * Write a portion of a message to this filter. + * @param input the input as a byte array + * @param length the length of the byte array input + */ + virtual void write(const byte input[], u32bit length) = 0; + + /** + * Start a new message. Must be closed by end_msg() before another + * message can be startet. + */ + virtual void start_msg() {} + + /** + * Tell the Filter that the current message shall be ended. + */ + virtual void end_msg() {} + + /** + * Check whether this filter is an attachable filter. + * @return true if this filter is attachable, false otherwise + */ + virtual bool attachable() { return true; } + + /** + * Start a new message in *this and all following filters. Only for + * internal use, not intended for use in client applications. + */ + void new_msg(); + + /** + * End a new message in *this and all following filters. Only for + * internal use, not intended for use in client applications. + */ + void finish_msg(); + + virtual ~Filter() {} + protected: + void send(const byte[], u32bit); + void send(byte input) { send(&input, 1); } + void send(const MemoryRegion<byte>& in) { send(in.begin(), in.size()); } + Filter(); + private: + Filter(const Filter&) {} + Filter& operator=(const Filter&) { return (*this); } + + friend class Pipe; + friend class Fanout_Filter; + + u32bit total_ports() const; + u32bit current_port() const { return port_num; } + void set_port(u32bit); + + u32bit owns() const { return filter_owns; } + + void attach(Filter*); + void set_next(Filter*[], u32bit); + Filter* get_next() const; + + SecureVector<byte> write_queue; + std::vector<Filter*> next; + u32bit port_num, filter_owns; + + // true if filter belongs to a pipe --> prohibit filter sharing! + bool owned; + }; + +/** +* This is the abstract Fanout_Filter base class. +**/ +class BOTAN_DLL Fanout_Filter : public Filter + { + protected: + void incr_owns() { ++filter_owns; } + + void set_port(u32bit n) { Filter::set_port(n); } + void set_next(Filter* f[], u32bit n) { Filter::set_next(f, n); } + void attach(Filter* f) { Filter::attach(f); } + }; + +/** +* The type of checking to be performed by decoders: +* NONE - no checks, IGNORE_WS - perform checks, but ignore +* whitespaces, FULL_CHECK - perform checks, also complain +* about white spaces. +*/ +enum Decoder_Checking { NONE, IGNORE_WS, FULL_CHECK }; + +} + +#endif diff --git a/botan/src/filters/filters.h b/botan/src/filters/filters.h new file mode 100644 index 0000000..725651f --- /dev/null +++ b/botan/src/filters/filters.h @@ -0,0 +1,189 @@ +/* +* Filters +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_FILTERS_H__ +#define BOTAN_FILTERS_H__ + +#include <botan/block_cipher.h> +#include <botan/stream_cipher.h> +#include <botan/hash.h> +#include <botan/mac.h> + +#include <botan/pipe.h> +#include <botan/basefilt.h> +#include <botan/data_snk.h> +#include <botan/scan_name.h> + +#if defined(BOTAN_HAS_BASE64_CODEC) + #include <botan/base64.h> +#endif + +#if defined(BOTAN_HAS_HEX_CODEC) + #include <botan/hex.h> +#endif + +namespace Botan { + +/** +* Stream Cipher Filter. +*/ +class BOTAN_DLL StreamCipher_Filter : public Keyed_Filter + { + public: + + /** + * Seek in the stream. + * @param position the position to seek ahead + */ + void seek(u32bit position) { cipher->seek(position); } + + /** + * Find out whether the cipher underlying this filter supports + * resyncing. + * @return true if the cipher supports resyncing + */ + bool supports_resync() const { return (cipher->IV_LENGTH != 0); } + + /** + * Set the initialization vector for this filter. + * @param iv the initialization vector to set + */ + void set_iv(const InitializationVector& iv); + void write(const byte[], u32bit); + + /** + * Construct a stream cipher filter. + * @param cipher_obj a cipher object to use + */ + StreamCipher_Filter(StreamCipher* cipher_obj); + + /** + * Construct a stream cipher filter. + * @param cipher the name of the desired cipher + */ + StreamCipher_Filter(const std::string& cipher); + + /** + * Construct a stream cipher filter. + * @param cipher the name of the desired cipher + * @param key the key to use inside this filter + */ + StreamCipher_Filter(const std::string& cipher, const SymmetricKey& key); + + ~StreamCipher_Filter() { delete cipher; } + private: + SecureVector<byte> buffer; + StreamCipher* cipher; + }; + +/** +* Hash Filter. +*/ +class BOTAN_DLL Hash_Filter : public Filter + { + public: + void write(const byte input[], u32bit len) { hash->update(input, len); } + void end_msg(); + + /** + * Construct a hash filter. + * @param hash_fun the hash function to use + * @param len the output length of this filter. Leave the default + * value 0 if you want to use the full output of the hashfunction + * hash. Otherwise, specify a smaller value here so that the + * output of the hash algorithm will be cut off. + */ + Hash_Filter(HashFunction* hash_fun, u32bit len = 0) : + OUTPUT_LENGTH(len), hash(hash_fun) {} + + /** + * Construct a hash filter. + * @param request the name of the hash algorithm to use + * @param len the output length of this filter. Leave the default + * value 0 if you want to use the full output of the hashfunction + * hash. Otherwise, specify a smaller value here so that the + * output of the hash algorithm will be cut off. + */ + Hash_Filter(const std::string& request, u32bit len = 0); + + ~Hash_Filter() { delete hash; } + private: + const u32bit OUTPUT_LENGTH; + HashFunction* hash; + }; + +/** +* MessageAuthenticationCode Filter. +*/ +class BOTAN_DLL MAC_Filter : public Keyed_Filter + { + public: + void write(const byte input[], u32bit len) { mac->update(input, len); } + void end_msg(); + + /** + * Construct a MAC filter. The MAC key will be left empty. + * @param mac the MAC to use + * @param len the output length of this filter. Leave the default + * value 0 if you want to use the full output of the + * MAC. Otherwise, specify a smaller value here so that the + * output of the MAC will be cut off. + */ + MAC_Filter(MessageAuthenticationCode* mac_obj, + u32bit out_len = 0) : OUTPUT_LENGTH(out_len) + { + base_ptr = mac = mac_obj; + } + + /** + * Construct a MAC filter. + * @param mac the MAC to use + * @param key the MAC key to use + * @param len the output length of this filter. Leave the default + * value 0 if you want to use the full output of the + * MAC. Otherwise, specify a smaller value here so that the + * output of the MAC will be cut off. + */ + MAC_Filter(MessageAuthenticationCode* mac_obj, + const SymmetricKey& key, + u32bit out_len = 0) : OUTPUT_LENGTH(out_len) + { + base_ptr = mac = mac_obj; + mac->set_key(key); + } + + /** + * Construct a MAC filter. The MAC key will be left empty. + * @param mac the name of the MAC to use + * @param len the output length of this filter. Leave the default + * value 0 if you want to use the full output of the + * MAC. Otherwise, specify a smaller value here so that the + * output of the MAC will be cut off. + */ + MAC_Filter(const std::string& mac, u32bit len = 0); + + /** + * Construct a MAC filter. + * @param mac the name of the MAC to use + * @param key the MAC key to use + * @param len the output length of this filter. Leave the default + * value 0 if you want to use the full output of the + * MAC. Otherwise, specify a smaller value here so that the + * output of the MAC will be cut off. + */ + MAC_Filter(const std::string& mac, const SymmetricKey& key, + u32bit len = 0); + + ~MAC_Filter() { delete mac; } + private: + const u32bit OUTPUT_LENGTH; + MessageAuthenticationCode* mac; + }; + +} + +#endif diff --git a/botan/src/filters/info.txt b/botan/src/filters/info.txt new file mode 100644 index 0000000..79a92a9 --- /dev/null +++ b/botan/src/filters/info.txt @@ -0,0 +1,41 @@ +realname "Pipe/Filter" + +load_on auto + +define FILTERS + +<add> +algo_filt.cpp +basefilt.cpp +basefilt.h +buf_filt.cpp +buf_filt.h +data_snk.cpp +data_snk.h +data_src.cpp +data_src.h +filter.cpp +filter.h +filters.h +out_buf.cpp +out_buf.h +pbe.h +pipe.cpp +pipe.h +pipe_io.cpp +pipe_rw.cpp +secqueue.cpp +secqueue.h +</add> + +<requires> +alloc +asn1 +block +hash +libstate +mac +rng +stream +sym_algo +</requires> diff --git a/botan/src/filters/out_buf.cpp b/botan/src/filters/out_buf.cpp new file mode 100644 index 0000000..6002f4f --- /dev/null +++ b/botan/src/filters/out_buf.cpp @@ -0,0 +1,119 @@ +/* +* Pipe Output Buffer Source file +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/out_buf.h> +#include <botan/secqueue.h> + +namespace Botan { + +/* +* Read data from a message +*/ +u32bit Output_Buffers::read(byte output[], u32bit length, + Pipe::message_id msg) + { + SecureQueue* q = get(msg); + if(q) + return q->read(output, length); + return 0; + } + +/* +* Peek at data in a message +*/ +u32bit Output_Buffers::peek(byte output[], u32bit length, + u32bit stream_offset, + Pipe::message_id msg) const + { + SecureQueue* q = get(msg); + if(q) + return q->peek(output, length, stream_offset); + return 0; + } + +/* +* Check available bytes in a message +*/ +u32bit Output_Buffers::remaining(Pipe::message_id msg) const + { + SecureQueue* q = get(msg); + if(q) + return q->size(); + return 0; + } + +/* +* Add a new output queue +*/ +void Output_Buffers::add(SecureQueue* queue) + { + if(!queue) + throw Internal_Error("Output_Buffers::add: Argument was NULL"); + + if(buffers.size() == buffers.max_size()) + throw Internal_Error("Output_Buffers::add: No more room in container"); + + buffers.push_back(queue); + } + +/* +* Retire old output queues +*/ +void Output_Buffers::retire() + { + while(buffers.size()) + { + if(buffers[0] == 0 || buffers[0]->size() == 0) + { + delete buffers[0]; + buffers.pop_front(); + offset = offset + Pipe::message_id(1); + } + else + break; + } + } + +/* +* Get a particular output queue +*/ +SecureQueue* Output_Buffers::get(Pipe::message_id msg) const + { + if(msg < offset) + return 0; + if(msg > message_count()) + throw Internal_Error("Output_Buffers::get: msg > size"); + + return buffers[msg-offset]; + } + +/* +* Return the total number of messages +*/ +Pipe::message_id Output_Buffers::message_count() const + { + return (offset + buffers.size()); + } + +/* +* Output_Buffers Constructor +*/ +Output_Buffers::Output_Buffers() + { + offset = 0; + } + +/* +* Output_Buffers Destructor +*/ +Output_Buffers::~Output_Buffers() + { + for(u32bit j = 0; j != buffers.size(); ++j) + delete buffers[j]; + } + +} diff --git a/botan/src/filters/out_buf.h b/botan/src/filters/out_buf.h new file mode 100644 index 0000000..0baacda --- /dev/null +++ b/botan/src/filters/out_buf.h @@ -0,0 +1,43 @@ +/* +* Output Buffer +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_OUTPUT_BUFFER_H__ +#define BOTAN_OUTPUT_BUFFER_H__ + +#include <botan/types.h> +#include <botan/pipe.h> +#include <deque> + +namespace Botan { + +/* +* Container of output buffers for Pipe +*/ +class BOTAN_DLL Output_Buffers + { + public: + u32bit read(byte[], u32bit, Pipe::message_id); + u32bit peek(byte[], u32bit, u32bit, Pipe::message_id) const; + u32bit remaining(Pipe::message_id) const; + + void add(class SecureQueue*); + void retire(); + + Pipe::message_id message_count() const; + + Output_Buffers(); + ~Output_Buffers(); + private: + class SecureQueue* get(Pipe::message_id) const; + + std::deque<SecureQueue*> buffers; + Pipe::message_id offset; + }; + +} + +#endif diff --git a/botan/src/filters/pbe.h b/botan/src/filters/pbe.h new file mode 100644 index 0000000..f06d593 --- /dev/null +++ b/botan/src/filters/pbe.h @@ -0,0 +1,56 @@ +/* +* PBE +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_PBE_BASE_H__ +#define BOTAN_PBE_BASE_H__ + +#include <botan/asn1_oid.h> +#include <botan/data_src.h> +#include <botan/filter.h> +#include <botan/rng.h> + +namespace Botan { + +/** +* Password Based Encryption (PBE) Filter. +*/ +class BOTAN_DLL PBE : public Filter + { + public: + /** + * Set this filter's key. + * @param pw the password to be used for the encryption + */ + virtual void set_key(const std::string&) = 0; + + /** + * Create a new random salt value and set the default iterations value. + */ + virtual void new_params(RandomNumberGenerator& rng) = 0; + + /** + * DER encode the params (the number of iterations and the salt value) + * @return the encoded params + */ + virtual MemoryVector<byte> encode_params() const = 0; + + /** + * Decode params and use them inside this Filter. + * @param src a data source to read the encoded params from + */ + virtual void decode_params(DataSource&) = 0; + + /** + * Get this PBE's OID. + * @return the OID + */ + virtual OID get_oid() const = 0; + }; + +} + +#endif diff --git a/botan/src/filters/pipe.cpp b/botan/src/filters/pipe.cpp new file mode 100644 index 0000000..33824be --- /dev/null +++ b/botan/src/filters/pipe.cpp @@ -0,0 +1,306 @@ +/* +* Pipe +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/pipe.h> +#include <botan/out_buf.h> +#include <botan/secqueue.h> +#include <botan/parsing.h> + +namespace Botan { + +/* +* Constructor for Invalid_Message_Number +*/ +Pipe::Invalid_Message_Number::Invalid_Message_Number(const std::string& where, + message_id msg) + { + set_msg("Pipe::" + where + ": Invalid message number " + + to_string(msg)); + } + +namespace { + +/* +* A Filter that does nothing +*/ +class Null_Filter : public Filter + { + public: + void write(const byte input[], u32bit length) + { send(input, length); } + }; + +} + +/* +* Pipe Constructor +*/ +Pipe::Pipe(Filter* f1, Filter* f2, Filter* f3, Filter* f4) + { + init(); + append(f1); + append(f2); + append(f3); + append(f4); + } + +/* +* Pipe Constructor +*/ +Pipe::Pipe(Filter* filter_array[], u32bit count) + { + init(); + for(u32bit j = 0; j != count; ++j) + append(filter_array[j]); + } + +/* +* Pipe Destructor +*/ +Pipe::~Pipe() + { + destruct(pipe); + delete outputs; + } + +/* +* Initialize the Pipe +*/ +void Pipe::init() + { + outputs = new Output_Buffers; + pipe = 0; + default_read = 0; + inside_msg = false; + } + +/* +* Reset the Pipe +*/ +void Pipe::reset() + { + if(inside_msg) + throw Invalid_State("Pipe cannot be reset while it is processing"); + destruct(pipe); + pipe = 0; + inside_msg = false; + } + +/* +* Destroy the Pipe +*/ +void Pipe::destruct(Filter* to_kill) + { + if(!to_kill || dynamic_cast<SecureQueue*>(to_kill)) + return; + for(u32bit j = 0; j != to_kill->total_ports(); ++j) + destruct(to_kill->next[j]); + delete to_kill; + } + +/* +* Test if the Pipe has any data in it +*/ +bool Pipe::end_of_data() const + { + return (remaining() == 0); + } + +/* +* Set the default read message +*/ +void Pipe::set_default_msg(message_id msg) + { + if(msg >= message_count()) + throw Invalid_Argument("Pipe::set_default_msg: msg number is too high"); + default_read = msg; + } + +/* +* Process a full message at once +*/ +void Pipe::process_msg(const byte input[], u32bit length) + { + start_msg(); + write(input, length); + end_msg(); + } + +/* +* Process a full message at once +*/ +void Pipe::process_msg(const MemoryRegion<byte>& input) + { + process_msg(input.begin(), input.size()); + } + +/* +* Process a full message at once +*/ +void Pipe::process_msg(const std::string& input) + { + process_msg(reinterpret_cast<const byte*>(input.data()), input.length()); + } + +/* +* Process a full message at once +*/ +void Pipe::process_msg(DataSource& input) + { + start_msg(); + write(input); + end_msg(); + } + +/* +* Start a new message +*/ +void Pipe::start_msg() + { + if(inside_msg) + throw Invalid_State("Pipe::start_msg: Message was already started"); + if(pipe == 0) + pipe = new Null_Filter; + find_endpoints(pipe); + pipe->new_msg(); + inside_msg = true; + } + +/* +* End the current message +*/ +void Pipe::end_msg() + { + if(!inside_msg) + throw Invalid_State("Pipe::end_msg: Message was already ended"); + pipe->finish_msg(); + clear_endpoints(pipe); + if(dynamic_cast<Null_Filter*>(pipe)) + { + delete pipe; + pipe = 0; + } + inside_msg = false; + + outputs->retire(); + } + +/* +* Find the endpoints of the Pipe +*/ +void Pipe::find_endpoints(Filter* f) + { + for(u32bit j = 0; j != f->total_ports(); ++j) + if(f->next[j] && !dynamic_cast<SecureQueue*>(f->next[j])) + find_endpoints(f->next[j]); + else + { + SecureQueue* q = new SecureQueue; + f->next[j] = q; + outputs->add(q); + } + } + +/* +* Remove the SecureQueues attached to the Filter +*/ +void Pipe::clear_endpoints(Filter* f) + { + if(!f) return; + for(u32bit j = 0; j != f->total_ports(); ++j) + { + if(f->next[j] && dynamic_cast<SecureQueue*>(f->next[j])) + f->next[j] = 0; + clear_endpoints(f->next[j]); + } + } + +/* +* Append a Filter to the Pipe +*/ +void Pipe::append(Filter* filter) + { + if(inside_msg) + throw Invalid_State("Cannot append to a Pipe while it is processing"); + if(!filter) + return; + if(dynamic_cast<SecureQueue*>(filter)) + throw Invalid_Argument("Pipe::append: SecureQueue cannot be used"); + if(filter->owned) + throw Invalid_Argument("Filters cannot be shared among multiple Pipes"); + + filter->owned = true; + + if(!pipe) pipe = filter; + else pipe->attach(filter); + } + +/* +* Prepend a Filter to the Pipe +*/ +void Pipe::prepend(Filter* filter) + { + if(inside_msg) + throw Invalid_State("Cannot prepend to a Pipe while it is processing"); + if(!filter) + return; + if(dynamic_cast<SecureQueue*>(filter)) + throw Invalid_Argument("Pipe::prepend: SecureQueue cannot be used"); + if(filter->owned) + throw Invalid_Argument("Filters cannot be shared among multiple Pipes"); + + filter->owned = true; + + if(pipe) filter->attach(pipe); + pipe = filter; + } + +/* +* Pop a Filter off the Pipe +*/ +void Pipe::pop() + { + if(inside_msg) + throw Invalid_State("Cannot pop off a Pipe while it is processing"); + + if(!pipe) + return; + + if(pipe->total_ports() > 1) + throw Invalid_State("Cannot pop off a Filter with multiple ports"); + + Filter* f = pipe; + u32bit owns = f->owns(); + pipe = pipe->next[0]; + delete f; + + while(owns--) + { + f = pipe; + pipe = pipe->next[0]; + delete f; + } + } + +/* +* Return the number of messages in this Pipe +*/ +Pipe::message_id Pipe::message_count() const + { + return outputs->message_count(); + } + +/* +* Static Member Variables +*/ +const Pipe::message_id Pipe::LAST_MESSAGE = + static_cast<Pipe::message_id>(-2); + +const Pipe::message_id Pipe::DEFAULT_MESSAGE = + static_cast<Pipe::message_id>(-1); + +} diff --git a/botan/src/filters/pipe.h b/botan/src/filters/pipe.h new file mode 100644 index 0000000..120f2fb --- /dev/null +++ b/botan/src/filters/pipe.h @@ -0,0 +1,275 @@ +/* +* Pipe +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_PIPE_H__ +#define BOTAN_PIPE_H__ + +#include <botan/data_src.h> +#include <botan/filter.h> +#include <botan/exceptn.h> +#include <iosfwd> + +namespace Botan { + +/** +* This class represents pipe objects. +* A set of filters can be placed into a pipe, and information flows +* through the pipe until it reaches the end, where the output is +* collected for retrieval. If you're familiar with the Unix shell +* environment, this design will sound quite familiar. +*/ + +class BOTAN_DLL Pipe : public DataSource + { + public: + typedef u32bit message_id; + + class Invalid_Message_Number : public Invalid_Argument + { + public: + Invalid_Message_Number(const std::string&, message_id); + }; + + static const message_id LAST_MESSAGE; + static const message_id DEFAULT_MESSAGE; + + /** + * Write input to the pipe, i.e. to its first filter. + * @param in the byte array to write + * @param length the length of the byte array in + */ + void write(const byte in[], u32bit length); + + /** + * Write input to the pipe, i.e. to its first filter. + * @param in the MemoryRegion containing the data to write + */ + void write(const MemoryRegion<byte>& in); + + /** + * Write input to the pipe, i.e. to its first filter. + * @param in the string containing the data to write + */ + void write(const std::string& in); + + /** + * Write input to the pipe, i.e. to its first filter. + * @param in the DataSource to read the data from + */ + void write(DataSource& in); + + /** + * Write input to the pipe, i.e. to its first filter. + * @param in a single byte to be written + */ + void write(byte in); + + /** + * Perform start_msg(), write() and end_msg() sequentially. + * @param in the byte array containing the data to write + * @param length the length of the byte array to write + */ + void process_msg(const byte in[], u32bit length); + + /** + * Perform start_msg(), write() and end_msg() sequentially. + * @param in the MemoryRegion containing the data to write + */ + void process_msg(const MemoryRegion<byte>& in); + + /** + * Perform start_msg(), write() and end_msg() sequentially. + * @param in the string containing the data to write + */ + void process_msg(const std::string& in); + + /** + * Perform start_msg(), write() and end_msg() sequentially. + * @param in the DataSource providing the data to write + */ + void process_msg(DataSource& in); + + /** + * Find out how many bytes are ready to read. + * @param msg the number identifying the message + * for which the information is desired + * @return the number of bytes that can still be read + */ + u32bit remaining(message_id msg = DEFAULT_MESSAGE) const; + + /** + * Read the default message from the pipe. Moves the internal + * offset so that every call to read will return a new portion of + * the message. + * @param output the byte array to write the read bytes to + * @param length the length of the byte array output + * @return the number of bytes actually read into output + */ + u32bit read(byte output[], u32bit length); + + /** + * Read a specified message from the pipe. Moves the internal + * offset so that every call to read will return a new portion of + * the message. + * @param output the byte array to write the read bytes to + * @param length the length of the byte array output + * @param msg the number identifying the message to read from + * @return the number of bytes actually read into output + */ + u32bit read(byte output[], u32bit length, message_id msg); + + /** + * Read a single byte from the pipe. Moves the internal offset so that + * every call to read will return a new portion of the message. + * @param output the byte to write the result to + * @return the number of bytes actually read into output + */ + u32bit read(byte& output, message_id msg = DEFAULT_MESSAGE); + + /** + * Read the full contents of the pipe. + * @param msg the number identifying the message to read from + * @return a SecureVector holding the contents of the pipe + */ + SecureVector<byte> read_all(message_id msg = DEFAULT_MESSAGE); + + /** + * Read the full contents of the pipe. + * @param msg the number identifying the message to read from + * @return a string holding the contents of the pipe + */ + std::string read_all_as_string(message_id = DEFAULT_MESSAGE); + + /** Read from the default message but do not modify the internal + * offset. Consecutive calls to peek() will return portions of + * the message starting at the same position. + * @param output the byte array to write the peeked message part to + * @param length the length of the byte array output + * @param offset the offset from the current position in message + * @return the number of bytes actually peeked and written into output + */ + u32bit peek(byte output[], u32bit length, u32bit offset) const; + + /** Read from the specified message but do not modify the + * internal offset. Consecutive calls to peek() will return + * portions of the message starting at the same position. + * @param output the byte array to write the peeked message part to + * @param length the length of the byte array output + * @param offset the offset from the current position in message + * @param msg the number identifying the message to peek from + * @return the number of bytes actually peeked and written into output + */ + u32bit peek(byte output[], u32bit length, + u32bit offset, message_id msg) const; + + /** Read a single byte from the specified message but do not + * modify the internal offset. Consecutive calls to peek() will + * return portions of the message starting at the same position. + * @param output the byte to write the peeked message byte to + * @param offset the offset from the current position in message + * @param msg the number identifying the message to peek from + * @return the number of bytes actually peeked and written into output + */ + u32bit peek(byte& output, u32bit offset, + message_id msg = DEFAULT_MESSAGE) const; + + u32bit default_msg() const { return default_read; } + + /** + * Set the default message + * @param msg the number identifying the message which is going to + * be the new default message + */ + void set_default_msg(message_id msg); + + /** + * Get the number of messages the are in this pipe. + * @return the number of messages the are in this pipe + */ + message_id message_count() const; + + /** + * Test whether this pipe has any data that can be read from. + * @return true if there is more data to read, false otherwise + */ + bool end_of_data() const; + + /** + * Start a new message in the pipe. A potential other message in this pipe + * must be closed with end_msg() before this function may be called. + */ + void start_msg(); + + /** + * End the current message. + */ + void end_msg(); + + /** + * Insert a new filter at the front of the pipe + * @param filt the new filter to insert + */ + void prepend(Filter* filt); + + /** + * Insert a new filter at the back of the pipe + * @param filt the new filter to insert + */ + void append(Filter* filt); + + /** + * Remove the first filter at the front of the pipe. + */ + void pop(); + + /** + * Reset this pipe to an empty pipe. + */ + void reset(); + + /** + * Construct a Pipe of up to four filters. The filters are set up + * in the same order as the arguments. + */ + Pipe(Filter* = 0, Filter* = 0, Filter* = 0, Filter* = 0); + + /** + * Construct a Pipe from range of filters passed as an array + * @param filters the set of filters to use + * @param count the number of elements in filters + */ + Pipe(Filter* filters[], u32bit count); + ~Pipe(); + private: + Pipe(const Pipe&) : DataSource() {} + Pipe& operator=(const Pipe&) { return (*this); } + void init(); + void destruct(Filter*); + void find_endpoints(Filter*); + void clear_endpoints(Filter*); + + message_id get_message_no(const std::string&, message_id) const; + + Filter* pipe; + class Output_Buffers* outputs; + message_id default_read; + bool inside_msg; + }; + +/* +* I/O Operators for Pipe +*/ +BOTAN_DLL std::ostream& operator<<(std::ostream&, Pipe&); +BOTAN_DLL std::istream& operator>>(std::istream&, Pipe&); + +} + +#endif + +#if defined(BOTAN_HAS_PIPE_UNIXFD_IO) + #include <botan/fd_unix.h> +#endif diff --git a/botan/src/filters/pipe_io.cpp b/botan/src/filters/pipe_io.cpp new file mode 100644 index 0000000..c57be6d --- /dev/null +++ b/botan/src/filters/pipe_io.cpp @@ -0,0 +1,45 @@ +/* +* Pipe I/O +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/pipe.h> +#include <iostream> + +namespace Botan { + +/* +* Write data from a pipe into an ostream +*/ +std::ostream& operator<<(std::ostream& stream, Pipe& pipe) + { + SecureVector<byte> buffer(DEFAULT_BUFFERSIZE); + while(stream.good() && pipe.remaining()) + { + u32bit got = pipe.read(buffer, buffer.size()); + stream.write(reinterpret_cast<const char*>(buffer.begin()), got); + } + if(!stream.good()) + throw Stream_IO_Error("Pipe output operator (iostream) has failed"); + return stream; + } + +/* +* Read data from an istream into a pipe +*/ +std::istream& operator>>(std::istream& stream, Pipe& pipe) + { + SecureVector<byte> buffer(DEFAULT_BUFFERSIZE); + while(stream.good()) + { + stream.read(reinterpret_cast<char*>(buffer.begin()), buffer.size()); + pipe.write(buffer, stream.gcount()); + } + if(stream.bad() || (stream.fail() && !stream.eof())) + throw Stream_IO_Error("Pipe input operator (iostream) has failed"); + return stream; + } + +} diff --git a/botan/src/filters/pipe_rw.cpp b/botan/src/filters/pipe_rw.cpp new file mode 100644 index 0000000..41b57a7 --- /dev/null +++ b/botan/src/filters/pipe_rw.cpp @@ -0,0 +1,167 @@ +/* +* Pipe Reading/Writing +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/pipe.h> +#include <botan/out_buf.h> +#include <botan/secqueue.h> + +namespace Botan { + +/* +* Look up the canonical ID for a queue +*/ +Pipe::message_id Pipe::get_message_no(const std::string& func_name, + message_id msg) const + { + if(msg == DEFAULT_MESSAGE) + msg = default_msg(); + else if(msg == LAST_MESSAGE) + msg = message_count() - 1; + + if(msg >= message_count()) + throw Invalid_Message_Number(func_name, msg); + + return msg; + } + +/* +* Write into a Pipe +*/ +void Pipe::write(const byte input[], u32bit length) + { + if(!inside_msg) + throw Exception("Cannot write to a Pipe while it is not processing"); + pipe->write(input, length); + } + +/* +* Write into a Pipe +*/ +void Pipe::write(const MemoryRegion<byte>& input) + { + write(input.begin(), input.size()); + } + +/* +* Write a string into a Pipe +*/ +void Pipe::write(const std::string& str) + { + write(reinterpret_cast<const byte*>(str.data()), str.size()); + } + +/* +* Write a single byte into a Pipe +*/ +void Pipe::write(byte input) + { + write(&input, 1); + } + +/* +* Write the contents of a DataSource into a Pipe +*/ +void Pipe::write(DataSource& source) + { + SecureVector<byte> buffer(DEFAULT_BUFFERSIZE); + while(!source.end_of_data()) + { + u32bit got = source.read(buffer, buffer.size()); + write(buffer, got); + } + } + +/* +* Read some data from the pipe +*/ +u32bit Pipe::read(byte output[], u32bit length, message_id msg) + { + return outputs->read(output, length, get_message_no("read", msg)); + } + +/* +* Read some data from the pipe +*/ +u32bit Pipe::read(byte output[], u32bit length) + { + return read(output, length, DEFAULT_MESSAGE); + } + +/* +* Read a single byte from the pipe +*/ +u32bit Pipe::read(byte& out, message_id msg) + { + return read(&out, 1, msg); + } + +/* +* Return all data in the pipe +*/ +SecureVector<byte> Pipe::read_all(message_id msg) + { + msg = ((msg != DEFAULT_MESSAGE) ? msg : default_msg()); + SecureVector<byte> buffer(remaining(msg)); + read(buffer, buffer.size(), msg); + return buffer; + } + +/* +* Return all data in the pipe as a string +*/ +std::string Pipe::read_all_as_string(message_id msg) + { + msg = ((msg != DEFAULT_MESSAGE) ? msg : default_msg()); + SecureVector<byte> buffer(DEFAULT_BUFFERSIZE); + std::string str; + str.reserve(remaining(msg)); + + while(true) + { + u32bit got = read(buffer, buffer.size(), msg); + if(got == 0) + break; + str.append(reinterpret_cast<const char*>(buffer.begin()), got); + } + + return str; + } + +/* +* Find out how many bytes are ready to read +*/ +u32bit Pipe::remaining(message_id msg) const + { + return outputs->remaining(get_message_no("remaining", msg)); + } + +/* +* Peek at some data in the pipe +*/ +u32bit Pipe::peek(byte output[], u32bit length, + u32bit offset, message_id msg) const + { + return outputs->peek(output, length, offset, get_message_no("peek", msg)); + } + +/* +* Peek at some data in the pipe +*/ +u32bit Pipe::peek(byte output[], u32bit length, u32bit offset) const + { + return peek(output, length, offset, DEFAULT_MESSAGE); + } + +/* +* Peek at a byte in the pipe +*/ +u32bit Pipe::peek(byte& out, u32bit offset, message_id msg) const + { + return peek(&out, 1, offset, msg); + } + +} diff --git a/botan/src/filters/secqueue.cpp b/botan/src/filters/secqueue.cpp new file mode 100644 index 0000000..f63ef89 --- /dev/null +++ b/botan/src/filters/secqueue.cpp @@ -0,0 +1,205 @@ +/* +* SecureQueue +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/secqueue.h> +#include <algorithm> + +namespace Botan { + +/* +* SecureQueueNode +*/ +class SecureQueueNode + { + public: + u32bit write(const byte input[], u32bit length) + { + u32bit copied = std::min(length, buffer.size() - end); + copy_mem(buffer + end, input, copied); + end += copied; + return copied; + } + u32bit read(byte output[], u32bit length) + { + u32bit copied = std::min(length, end - start); + copy_mem(output, buffer + start, copied); + start += copied; + return copied; + } + u32bit peek(byte output[], u32bit length, u32bit offset = 0) + { + const u32bit left = end - start; + if(offset >= left) return 0; + u32bit copied = std::min(length, left - offset); + copy_mem(output, buffer + start + offset, copied); + return copied; + } + u32bit size() const { return (end - start); } + SecureQueueNode() { next = 0; start = end = 0; } + ~SecureQueueNode() { next = 0; start = end = 0; } + private: + friend class SecureQueue; + SecureQueueNode* next; + SecureBuffer<byte, DEFAULT_BUFFERSIZE> buffer; + u32bit start, end; + }; + +/* +* Create a SecureQueue +*/ +SecureQueue::SecureQueue() + { + set_next(0, 0); + head = tail = new SecureQueueNode; + } + +/* +* Copy a SecureQueue +*/ +SecureQueue::SecureQueue(const SecureQueue& input) : + Fanout_Filter(), DataSource() + { + set_next(0, 0); + + head = tail = new SecureQueueNode; + SecureQueueNode* temp = input.head; + while(temp) + { + write(temp->buffer + temp->start, temp->end - temp->start); + temp = temp->next; + } + } + +/* +* Destroy this SecureQueue +*/ +void SecureQueue::destroy() + { + SecureQueueNode* temp = head; + while(temp) + { + SecureQueueNode* holder = temp->next; + delete temp; + temp = holder; + } + head = tail = 0; + } + +/* +* Copy a SecureQueue +*/ +SecureQueue& SecureQueue::operator=(const SecureQueue& input) + { + destroy(); + head = tail = new SecureQueueNode; + SecureQueueNode* temp = input.head; + while(temp) + { + write(temp->buffer + temp->start, temp->end - temp->start); + temp = temp->next; + } + return (*this); + } + +/* +* Add some bytes to the queue +*/ +void SecureQueue::write(const byte input[], u32bit length) + { + if(!head) + head = tail = new SecureQueueNode; + while(length) + { + const u32bit n = tail->write(input, length); + input += n; + length -= n; + if(length) + { + tail->next = new SecureQueueNode; + tail = tail->next; + } + } + } + +/* +* Read some bytes from the queue +*/ +u32bit SecureQueue::read(byte output[], u32bit length) + { + u32bit got = 0; + while(length && head) + { + const u32bit n = head->read(output, length); + output += n; + got += n; + length -= n; + if(head->size() == 0) + { + SecureQueueNode* holder = head->next; + delete head; + head = holder; + } + } + return got; + } + +/* +* Read data, but do not remove it from queue +*/ +u32bit SecureQueue::peek(byte output[], u32bit length, u32bit offset) const + { + SecureQueueNode* current = head; + + while(offset && current) + { + if(offset >= current->size()) + { + offset -= current->size(); + current = current->next; + } + else + break; + } + + u32bit got = 0; + while(length && current) + { + const u32bit n = current->peek(output, length, offset); + offset = 0; + output += n; + got += n; + length -= n; + current = current->next; + } + return got; + } + +/* +* Return how many bytes the queue holds +*/ +u32bit SecureQueue::size() const + { + SecureQueueNode* current = head; + u32bit count = 0; + + while(current) + { + count += current->size(); + current = current->next; + } + return count; + } + +/* +* Test if the queue has any data in it +*/ +bool SecureQueue::end_of_data() const + { + return (size() == 0); + } + +} diff --git a/botan/src/filters/secqueue.h b/botan/src/filters/secqueue.h new file mode 100644 index 0000000..fc1fc21 --- /dev/null +++ b/botan/src/filters/secqueue.h @@ -0,0 +1,43 @@ +/* +* SecureQueue +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_SECURE_QUEUE_H__ +#define BOTAN_SECURE_QUEUE_H__ + +#include <botan/data_src.h> +#include <botan/filter.h> + +namespace Botan { + +/* +* SecureQueue +*/ +class BOTAN_DLL SecureQueue : public Fanout_Filter, public DataSource + { + public: + void write(const byte[], u32bit); + + u32bit read(byte[], u32bit); + u32bit peek(byte[], u32bit, u32bit = 0) const; + + bool end_of_data() const; + u32bit size() const; + bool attachable() { return false; } + + SecureQueue& operator=(const SecureQueue&); + SecureQueue(); + SecureQueue(const SecureQueue&); + ~SecureQueue() { destroy(); } + private: + void destroy(); + class SecureQueueNode* head; + class SecureQueueNode* tail; + }; + +} + +#endif diff --git a/botan/src/hash/fork256/fork256.cpp b/botan/src/hash/fork256/fork256.cpp new file mode 100644 index 0000000..f80bff4 --- /dev/null +++ b/botan/src/hash/fork256/fork256.cpp @@ -0,0 +1,149 @@ +/* +* FORK-256 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/fork256.h> +#include <botan/loadstor.h> +#include <botan/rotate.h> + +namespace Botan { + +namespace { + +/* +* FORK-256 Step Function +*/ +inline void step(u32bit& A, u32bit& B, u32bit& C, u32bit& D, + u32bit& E, u32bit& F, u32bit& G, u32bit& H, + u32bit M1, u32bit M2, u32bit D1, u32bit D2) + { + u32bit T0, T1; + + A += M1; T0 = A + (rotate_left(A, 7) ^ rotate_left(A, 22)); + A += D1; T1 = A ^ (rotate_left(A, 13) + rotate_left(A, 27)); + + B = (B + T0) ^ T1; + C = (C + rotate_left(T0, 5)) ^ rotate_left(T1, 9); + D = (D + rotate_left(T0, 17)) ^ rotate_left(T1, 21); + + E += M2; T0 = E ^ (rotate_left(E, 13) + rotate_left(E, 27)); + E += D2; T1 = E + (rotate_left(E, 7) ^ rotate_left(E, 22)); + + F = (F + T0) ^ T1; + G = (G + rotate_left(T0, 9)) ^ rotate_left(T1, 5); + H = (H + rotate_left(T0, 21)) ^ rotate_left(T1, 17); + } + +} + +/* +* FORK-256 Compression Function +*/ +void FORK_256::compress_n(const byte input[], u32bit blocks) + { + const u32bit DELTA[16] = { + 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5, 0x3956C25B, 0x59F111F1, + 0x923F82A4, 0xAB1C5ED5, 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3, + 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174 + }; + + for(u32bit i = 0; i != blocks; ++i) + { + u32bit A1, B1, C1, D1, E1, F1, G1, H1; + u32bit A2, B2, C2, D2, E2, F2, G2, H2; + u32bit A3, B3, C3, D3, E3, F3, G3, H3; + u32bit A4, B4, C4, D4, E4, F4, G4, H4; + + A1 = A2 = A3 = A4 = digest[0]; + B1 = B2 = B3 = B4 = digest[1]; + C1 = C2 = C3 = C4 = digest[2]; + D1 = D2 = D3 = D4 = digest[3]; + E1 = E2 = E3 = E4 = digest[4]; + F1 = F2 = F3 = F4 = digest[5]; + G1 = G2 = G3 = G4 = digest[6]; + H1 = H2 = H3 = H4 = digest[7]; + + for(u32bit j = 0; j != 16; ++j) + M[j] = load_be<u32bit>(input, j); + input += HASH_BLOCK_SIZE; + + step(A1, B1, C1, D1, E1, F1, G1, H1, M[ 0], M[ 1], DELTA[ 0], DELTA[ 1]); + step(A2, B2, C2, D2, E2, F2, G2, H2, M[14], M[15], DELTA[15], DELTA[14]); + step(A3, B3, C3, D3, E3, F3, G3, H3, M[ 7], M[ 6], DELTA[ 1], DELTA[ 0]); + step(A4, B4, C4, D4, E4, F4, G4, H4, M[ 5], M[12], DELTA[14], DELTA[15]); + + step(H1, A1, B1, C1, D1, E1, F1, G1, M[ 2], M[ 3], DELTA[ 2], DELTA[ 3]); + step(H2, A2, B2, C2, D2, E2, F2, G2, M[11], M[ 9], DELTA[13], DELTA[12]); + step(H3, A3, B3, C3, D3, E3, F3, G3, M[10], M[14], DELTA[ 3], DELTA[ 2]); + step(H4, A4, B4, C4, D4, E4, F4, G4, M[ 1], M[ 8], DELTA[12], DELTA[13]); + + step(G1, H1, A1, B1, C1, D1, E1, F1, M[ 4], M[ 5], DELTA[ 4], DELTA[ 5]); + step(G2, H2, A2, B2, C2, D2, E2, F2, M[ 8], M[10], DELTA[11], DELTA[10]); + step(G3, H3, A3, B3, C3, D3, E3, F3, M[13], M[ 2], DELTA[ 5], DELTA[ 4]); + step(G4, H4, A4, B4, C4, D4, E4, F4, M[15], M[ 0], DELTA[10], DELTA[11]); + + step(F1, G1, H1, A1, B1, C1, D1, E1, M[ 6], M[ 7], DELTA[ 6], DELTA[ 7]); + step(F2, G2, H2, A2, B2, C2, D2, E2, M[ 3], M[ 4], DELTA[ 9], DELTA[ 8]); + step(F3, G3, H3, A3, B3, C3, D3, E3, M[ 9], M[12], DELTA[ 7], DELTA[ 6]); + step(F4, G4, H4, A4, B4, C4, D4, E4, M[13], M[11], DELTA[ 8], DELTA[ 9]); + + step(E1, F1, G1, H1, A1, B1, C1, D1, M[ 8], M[ 9], DELTA[ 8], DELTA[ 9]); + step(E2, F2, G2, H2, A2, B2, C2, D2, M[ 2], M[13], DELTA[ 7], DELTA[ 6]); + step(E3, F3, G3, H3, A3, B3, C3, D3, M[11], M[ 4], DELTA[ 9], DELTA[ 8]); + step(E4, F4, G4, H4, A4, B4, C4, D4, M[ 3], M[10], DELTA[ 6], DELTA[ 7]); + + step(D1, E1, F1, G1, H1, A1, B1, C1, M[10], M[11], DELTA[10], DELTA[11]); + step(D2, E2, F2, G2, H2, A2, B2, C2, M[ 0], M[ 5], DELTA[ 5], DELTA[ 4]); + step(D3, E3, F3, G3, H3, A3, B3, C3, M[15], M[ 8], DELTA[11], DELTA[10]); + step(D4, E4, F4, G4, H4, A4, B4, C4, M[ 9], M[ 2], DELTA[ 4], DELTA[ 5]); + + step(C1, D1, E1, F1, G1, H1, A1, B1, M[12], M[13], DELTA[12], DELTA[13]); + step(C2, D2, E2, F2, G2, H2, A2, B2, M[ 6], M[ 7], DELTA[ 3], DELTA[ 2]); + step(C3, D3, E3, F3, G3, H3, A3, B3, M[ 5], M[ 0], DELTA[13], DELTA[12]); + step(C4, D4, E4, F4, G4, H4, A4, B4, M[ 7], M[14], DELTA[ 2], DELTA[ 3]); + + step(B1, C1, D1, E1, F1, G1, H1, A1, M[14], M[15], DELTA[14], DELTA[15]); + step(B2, C2, D2, E2, F2, G2, H2, A2, M[12], M[ 1], DELTA[ 1], DELTA[ 0]); + step(B3, C3, D3, E3, F3, G3, H3, A3, M[ 1], M[ 3], DELTA[15], DELTA[14]); + step(B4, C4, D4, E4, F4, G4, H4, A4, M[ 4], M[ 6], DELTA[ 0], DELTA[ 1]); + + digest[0] += (A1 + A2) ^ (A3 + A4); + digest[1] += (B1 + B2) ^ (B3 + B4); + digest[2] += (C1 + C2) ^ (C3 + C4); + digest[3] += (D1 + D2) ^ (D3 + D4); + digest[4] += (E1 + E2) ^ (E3 + E4); + digest[5] += (F1 + F2) ^ (F3 + F4); + digest[6] += (G1 + G2) ^ (G3 + G4); + digest[7] += (H1 + H2) ^ (H3 + H4); + } + } + +/* +* Copy out the digest +*/ +void FORK_256::copy_out(byte output[]) + { + for(u32bit j = 0; j != OUTPUT_LENGTH; j += 4) + store_be(digest[j/4], output + j); + } + +/* +* Clear memory of sensitive data +*/ +void FORK_256::clear() throw() + { + MDx_HashFunction::clear(); + digest[0] = 0x6A09E667; + digest[1] = 0xBB67AE85; + digest[2] = 0x3C6EF372; + digest[3] = 0xA54FF53A; + digest[4] = 0x510E527F; + digest[5] = 0x9B05688C; + digest[6] = 0x1F83D9AB; + digest[7] = 0x5BE0CD19; + } + +} diff --git a/botan/src/hash/fork256/fork256.h b/botan/src/hash/fork256/fork256.h new file mode 100644 index 0000000..70d336c --- /dev/null +++ b/botan/src/hash/fork256/fork256.h @@ -0,0 +1,35 @@ +/* +* FORK-256 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_FORK_256_H__ +#define BOTAN_FORK_256_H__ + +#include <botan/mdx_hash.h> + +namespace Botan { + +/* +* FORK-256 +*/ +class BOTAN_DLL FORK_256 : public MDx_HashFunction + { + public: + void clear() throw(); + std::string name() const { return "FORK-256"; } + HashFunction* clone() const { return new FORK_256; } + FORK_256() : MDx_HashFunction(32, 64, true, true) { clear(); } + private: + void compress_n(const byte[], u32bit blocks); + void copy_out(byte[]); + + SecureBuffer<u32bit, 8> digest; + SecureBuffer<u32bit, 16> M; + }; + +} + +#endif diff --git a/botan/src/hash/fork256/info.txt b/botan/src/hash/fork256/info.txt new file mode 100644 index 0000000..ae0c9f1 --- /dev/null +++ b/botan/src/hash/fork256/info.txt @@ -0,0 +1,14 @@ +realname "FORK-256" + +define FORK_256 + +load_on auto + +<add> +fork256.cpp +fork256.h +</add> + +<requires> +mdx_hash +</requires> diff --git a/botan/src/hash/gost_3411/gost_3411.cpp b/botan/src/hash/gost_3411/gost_3411.cpp new file mode 100644 index 0000000..5cbb8a7 --- /dev/null +++ b/botan/src/hash/gost_3411/gost_3411.cpp @@ -0,0 +1,236 @@ +/* +* GOST 34.11 +* (C) 2009 Jack Lloyd +*/ + +#include <botan/gost_3411.h> +#include <botan/loadstor.h> +#include <botan/rotate.h> +#include <botan/xor_buf.h> + +namespace Botan { + +/** +* GOST 34.11 Constructor +*/ +GOST_34_11::GOST_34_11() : + HashFunction(32, 32), + cipher(GOST_28147_89_Params("R3411_CryptoPro")) + { + count = 0; + position = 0; + } + +void GOST_34_11::clear() throw() + { + cipher.clear(); + sum.clear(); + hash.clear(); + count = 0; + position = 0; + } + +/** +* Hash additional inputs +*/ +void GOST_34_11::add_data(const byte input[], u32bit length) + { + count += length; + + if(position) + { + buffer.copy(position, input, length); + + if(position + length >= HASH_BLOCK_SIZE) + { + compress_n(buffer.begin(), 1); + input += (HASH_BLOCK_SIZE - position); + length -= (HASH_BLOCK_SIZE - position); + position = 0; + } + } + + const u32bit full_blocks = length / HASH_BLOCK_SIZE; + const u32bit remaining = length % HASH_BLOCK_SIZE; + + if(full_blocks) + compress_n(input, full_blocks); + + buffer.copy(position, input + full_blocks * HASH_BLOCK_SIZE, remaining); + position += remaining; + } + +/** +* The GOST 34.11 compression function +*/ +void GOST_34_11::compress_n(const byte input[], u32bit blocks) + { + for(u32bit i = 0; i != blocks; ++i) + { + for(u32bit j = 0, carry = 0; j != 32; ++j) + { + u16bit s = sum[j] + input[32*i+j] + carry; + carry = get_byte(0, s); + sum[j] = get_byte(1, s); + } + + byte S[32] = { 0 }; + + u64bit U[4], V[4]; + + for(u32bit j = 0; j != 4; ++j) + { + U[j] = load_be<u64bit>(hash, j); + V[j] = load_be<u64bit>(input + 32*i, j); + } + + for(u32bit j = 0; j != 4; ++j) + { + byte key[32] = { 0 }; + + // P transformation + for(size_t k = 0; k != 4; ++k) + for(size_t l = 0; l != 8; ++l) + key[4*l+k] = get_byte(l, U[k]) ^ get_byte(l, V[k]); + + cipher.set_key(key, 32); + cipher.encrypt(hash + 8*j, S + 8*j); + + if(j == 3) + break; + + // A(x) + u64bit A_U = U[0]; + U[0] = U[1]; + U[1] = U[2]; + U[2] = U[3]; + U[3] = U[0] ^ A_U; + + if(j == 1) // C_3 + { + U[0] ^= (u64bit) 0x00FF00FF00FF00FFULL; + U[1] ^= (u64bit) 0xFF00FF00FF00FF00ULL; + U[2] ^= (u64bit) 0x00FFFF00FF0000FFULL; + U[3] ^= (u64bit) 0xFF000000FFFF00FFULL; + } + + // A(A(x)) + u64bit AA_V_1 = V[0] ^ V[1]; + u64bit AA_V_2 = V[1] ^ V[2]; + V[0] = V[2]; + V[1] = V[3]; + V[2] = AA_V_1; + V[3] = AA_V_2; + } + + byte S2[32] = { 0 }; + + // 12 rounds of psi + S2[ 0] = S[24]; + S2[ 1] = S[25]; + S2[ 2] = S[26]; + S2[ 3] = S[27]; + S2[ 4] = S[28]; + S2[ 5] = S[29]; + S2[ 6] = S[30]; + S2[ 7] = S[31]; + S2[ 8] = S[ 0] ^ S[ 2] ^ S[ 4] ^ S[ 6] ^ S[24] ^ S[30]; + S2[ 9] = S[ 1] ^ S[ 3] ^ S[ 5] ^ S[ 7] ^ S[25] ^ S[31]; + S2[10] = S[ 0] ^ S[ 8] ^ S[24] ^ S[26] ^ S[30]; + S2[11] = S[ 1] ^ S[ 9] ^ S[25] ^ S[27] ^ S[31]; + S2[12] = S[ 0] ^ S[ 4] ^ S[ 6] ^ S[10] ^ S[24] ^ S[26] ^ S[28] ^ S[30]; + S2[13] = S[ 1] ^ S[ 5] ^ S[ 7] ^ S[11] ^ S[25] ^ S[27] ^ S[29] ^ S[31]; + S2[14] = S[ 0] ^ S[ 4] ^ S[ 8] ^ S[12] ^ S[24] ^ S[26] ^ S[28]; + S2[15] = S[ 1] ^ S[ 5] ^ S[ 9] ^ S[13] ^ S[25] ^ S[27] ^ S[29]; + S2[16] = S[ 2] ^ S[ 6] ^ S[10] ^ S[14] ^ S[26] ^ S[28] ^ S[30]; + S2[17] = S[ 3] ^ S[ 7] ^ S[11] ^ S[15] ^ S[27] ^ S[29] ^ S[31]; + S2[18] = S[ 0] ^ S[ 2] ^ S[ 6] ^ S[ 8] ^ S[12] ^ S[16] ^ S[24] ^ S[28]; + S2[19] = S[ 1] ^ S[ 3] ^ S[ 7] ^ S[ 9] ^ S[13] ^ S[17] ^ S[25] ^ S[29]; + S2[20] = S[ 2] ^ S[ 4] ^ S[ 8] ^ S[10] ^ S[14] ^ S[18] ^ S[26] ^ S[30]; + S2[21] = S[ 3] ^ S[ 5] ^ S[ 9] ^ S[11] ^ S[15] ^ S[19] ^ S[27] ^ S[31]; + S2[22] = S[ 0] ^ S[ 2] ^ S[10] ^ S[12] ^ S[16] ^ S[20] ^ S[24] ^ S[28] ^ S[30]; + S2[23] = S[ 1] ^ S[ 3] ^ S[11] ^ S[13] ^ S[17] ^ S[21] ^ S[25] ^ S[29] ^ S[31]; + S2[24] = S[ 0] ^ S[ 6] ^ S[12] ^ S[14] ^ S[18] ^ S[22] ^ S[24] ^ S[26]; + S2[25] = S[ 1] ^ S[ 7] ^ S[13] ^ S[15] ^ S[19] ^ S[23] ^ S[25] ^ S[27]; + S2[26] = S[ 2] ^ S[ 8] ^ S[14] ^ S[16] ^ S[20] ^ S[24] ^ S[26] ^ S[28]; + S2[27] = S[ 3] ^ S[ 9] ^ S[15] ^ S[17] ^ S[21] ^ S[25] ^ S[27] ^ S[29]; + S2[28] = S[ 4] ^ S[10] ^ S[16] ^ S[18] ^ S[22] ^ S[26] ^ S[28] ^ S[30]; + S2[29] = S[ 5] ^ S[11] ^ S[17] ^ S[19] ^ S[23] ^ S[27] ^ S[29] ^ S[31]; + S2[30] = S[ 0] ^ S[ 2] ^ S[ 4] ^ S[12] ^ S[18] ^ S[20] ^ S[28]; + S2[31] = S[ 1] ^ S[ 3] ^ S[ 5] ^ S[13] ^ S[19] ^ S[21] ^ S[29]; + + xor_buf(S, S2, input + 32*i, 32); + + S2[0] = S[0] ^ S[2] ^ S[4] ^ S[6] ^ S[24] ^ S[30]; + S2[1] = S[1] ^ S[3] ^ S[5] ^ S[7] ^ S[25] ^ S[31]; + + copy_mem(S, S+2, 30); + S[30] = S2[0]; + S[31] = S2[1]; + + xor_buf(S, hash, 32); + + // 61 rounds of psi + S2[ 0] = S[ 2] ^ S[ 6] ^ S[14] ^ S[20] ^ S[22] ^ S[26] ^ S[28] ^ S[30]; + S2[ 1] = S[ 3] ^ S[ 7] ^ S[15] ^ S[21] ^ S[23] ^ S[27] ^ S[29] ^ S[31]; + S2[ 2] = S[ 0] ^ S[ 2] ^ S[ 6] ^ S[ 8] ^ S[16] ^ S[22] ^ S[28]; + S2[ 3] = S[ 1] ^ S[ 3] ^ S[ 7] ^ S[ 9] ^ S[17] ^ S[23] ^ S[29]; + S2[ 4] = S[ 2] ^ S[ 4] ^ S[ 8] ^ S[10] ^ S[18] ^ S[24] ^ S[30]; + S2[ 5] = S[ 3] ^ S[ 5] ^ S[ 9] ^ S[11] ^ S[19] ^ S[25] ^ S[31]; + S2[ 6] = S[ 0] ^ S[ 2] ^ S[10] ^ S[12] ^ S[20] ^ S[24] ^ S[26] ^ S[30]; + S2[ 7] = S[ 1] ^ S[ 3] ^ S[11] ^ S[13] ^ S[21] ^ S[25] ^ S[27] ^ S[31]; + S2[ 8] = S[ 0] ^ S[ 6] ^ S[12] ^ S[14] ^ S[22] ^ S[24] ^ S[26] ^ S[28] ^ S[30]; + S2[ 9] = S[ 1] ^ S[ 7] ^ S[13] ^ S[15] ^ S[23] ^ S[25] ^ S[27] ^ S[29] ^ S[31]; + S2[10] = S[ 0] ^ S[ 4] ^ S[ 6] ^ S[ 8] ^ S[14] ^ S[16] ^ S[26] ^ S[28]; + S2[11] = S[ 1] ^ S[ 5] ^ S[ 7] ^ S[ 9] ^ S[15] ^ S[17] ^ S[27] ^ S[29]; + S2[12] = S[ 2] ^ S[ 6] ^ S[ 8] ^ S[10] ^ S[16] ^ S[18] ^ S[28] ^ S[30]; + S2[13] = S[ 3] ^ S[ 7] ^ S[ 9] ^ S[11] ^ S[17] ^ S[19] ^ S[29] ^ S[31]; + S2[14] = S[ 0] ^ S[ 2] ^ S[ 6] ^ S[ 8] ^ S[10] ^ S[12] ^ S[18] ^ S[20] ^ S[24]; + S2[15] = S[ 1] ^ S[ 3] ^ S[ 7] ^ S[ 9] ^ S[11] ^ S[13] ^ S[19] ^ S[21] ^ S[25]; + S2[16] = S[ 2] ^ S[ 4] ^ S[ 8] ^ S[10] ^ S[12] ^ S[14] ^ S[20] ^ S[22] ^ S[26]; + S2[17] = S[ 3] ^ S[ 5] ^ S[ 9] ^ S[11] ^ S[13] ^ S[15] ^ S[21] ^ S[23] ^ S[27]; + S2[18] = S[ 4] ^ S[ 6] ^ S[10] ^ S[12] ^ S[14] ^ S[16] ^ S[22] ^ S[24] ^ S[28]; + S2[19] = S[ 5] ^ S[ 7] ^ S[11] ^ S[13] ^ S[15] ^ S[17] ^ S[23] ^ S[25] ^ S[29]; + S2[20] = S[ 6] ^ S[ 8] ^ S[12] ^ S[14] ^ S[16] ^ S[18] ^ S[24] ^ S[26] ^ S[30]; + S2[21] = S[ 7] ^ S[ 9] ^ S[13] ^ S[15] ^ S[17] ^ S[19] ^ S[25] ^ S[27] ^ S[31]; + S2[22] = S[ 0] ^ S[ 2] ^ S[ 4] ^ S[ 6] ^ S[ 8] ^ S[10] ^ S[14] ^ S[16] ^ S[18] ^ S[20] ^ S[24] ^ S[26] ^ S[28] ^ S[30]; + S2[23] = S[ 1] ^ S[ 3] ^ S[ 5] ^ S[ 7] ^ S[ 9] ^ S[11] ^ S[15] ^ S[17] ^ S[19] ^ S[21] ^ S[25] ^ S[27] ^ S[29] ^ S[31]; + S2[24] = S[ 0] ^ S[ 8] ^ S[10] ^ S[12] ^ S[16] ^ S[18] ^ S[20] ^ S[22] ^ S[24] ^ S[26] ^ S[28]; + S2[25] = S[ 1] ^ S[ 9] ^ S[11] ^ S[13] ^ S[17] ^ S[19] ^ S[21] ^ S[23] ^ S[25] ^ S[27] ^ S[29]; + S2[26] = S[ 2] ^ S[10] ^ S[12] ^ S[14] ^ S[18] ^ S[20] ^ S[22] ^ S[24] ^ S[26] ^ S[28] ^ S[30]; + S2[27] = S[ 3] ^ S[11] ^ S[13] ^ S[15] ^ S[19] ^ S[21] ^ S[23] ^ S[25] ^ S[27] ^ S[29] ^ S[31]; + S2[28] = S[ 0] ^ S[ 2] ^ S[ 6] ^ S[12] ^ S[14] ^ S[16] ^ S[20] ^ S[22] ^ S[26] ^ S[28]; + S2[29] = S[ 1] ^ S[ 3] ^ S[ 7] ^ S[13] ^ S[15] ^ S[17] ^ S[21] ^ S[23] ^ S[27] ^ S[29]; + S2[30] = S[ 2] ^ S[ 4] ^ S[ 8] ^ S[14] ^ S[16] ^ S[18] ^ S[22] ^ S[24] ^ S[28] ^ S[30]; + S2[31] = S[ 3] ^ S[ 5] ^ S[ 9] ^ S[15] ^ S[17] ^ S[19] ^ S[23] ^ S[25] ^ S[29] ^ S[31]; + + hash.copy(S2, 32); + } + } + +/** +* Produce the final GOST 34.11 output +*/ +void GOST_34_11::final_result(byte out[]) + { + if(position) + { + clear_mem(buffer.begin() + position, buffer.size() - position); + compress_n(buffer, 1); + } + + SecureBuffer<byte, 32> length_buf; + const u64bit bit_count = count * 8; + store_le(bit_count, length_buf); + + SecureBuffer<byte, 32> sum_buf(sum); + + compress_n(length_buf, 1); + compress_n(sum_buf, 1); + + copy_mem(out, hash.begin(), 32); + + clear(); + } + +} diff --git a/botan/src/hash/gost_3411/gost_3411.h b/botan/src/hash/gost_3411/gost_3411.h new file mode 100644 index 0000000..c695550 --- /dev/null +++ b/botan/src/hash/gost_3411/gost_3411.h @@ -0,0 +1,41 @@ +/** +* GOST 34.11 +* (C) 2009 Jack Lloyd +*/ + +#ifndef BOTAN_GOST_3411_H__ +#define BOTAN_GOST_3411_H__ + +#include <botan/hash.h> +#include <botan/gost_28147.h> + +namespace Botan { + +/** +* GOST 34.11 +*/ +class BOTAN_DLL GOST_34_11 : public HashFunction + { + public: + void clear() throw(); + std::string name() const { return "GOST-R-34.11-94" ; } + HashFunction* clone() const { return new GOST_34_11; } + + GOST_34_11(); + protected: + void compress_n(const byte input[], u32bit blocks); + + void add_data(const byte[], u32bit); + void final_result(byte[]); + + GOST_28147_89 cipher; + SecureBuffer<byte, 32> buffer; + SecureBuffer<byte, 32> sum; + SecureBuffer<byte, 32> hash; + u64bit count; + u32bit position; + }; + +} + +#endif diff --git a/botan/src/hash/gost_3411/info.txt b/botan/src/hash/gost_3411/info.txt new file mode 100644 index 0000000..65b9475 --- /dev/null +++ b/botan/src/hash/gost_3411/info.txt @@ -0,0 +1,14 @@ +realname "GOST 34.11" + +define GOST_34_11 + +load_on auto + +<add> +gost_3411.cpp +gost_3411.h +</add> + +<requires> +gost_28147 +</requires> diff --git a/botan/src/hash/has160/has160.cpp b/botan/src/hash/has160/has160.cpp new file mode 100644 index 0000000..9a505d3 --- /dev/null +++ b/botan/src/hash/has160/has160.cpp @@ -0,0 +1,159 @@ +/* +* HAS-160 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/has160.h> +#include <botan/loadstor.h> +#include <botan/rotate.h> + +namespace Botan { + +namespace { + +/* +* HAS-160 F1 Function +*/ +inline void F1(u32bit A, u32bit& B, u32bit C, u32bit D, u32bit& E, + u32bit msg, u32bit rot) + { + E += rotate_left(A, rot) + (D ^ (B & (C ^ D))) + msg; + B = rotate_left(B, 10); + } + +/* +* HAS-160 F2 Function +*/ +inline void F2(u32bit A, u32bit& B, u32bit C, u32bit D, u32bit& E, + u32bit msg, u32bit rot) + { + E += rotate_left(A, rot) + (B ^ C ^ D) + msg + 0x5A827999; + B = rotate_left(B, 17); + } + +/* +* HAS-160 F3 Function +*/ +inline void F3(u32bit A, u32bit& B, u32bit C, u32bit D, u32bit& E, + u32bit msg, u32bit rot) + { + E += rotate_left(A, rot) + (C ^ (B | ~D)) + msg + 0x6ED9EBA1; + B = rotate_left(B, 25); + } + +/* +* HAS-160 F4 Function +*/ +inline void F4(u32bit A, u32bit& B, u32bit C, u32bit D, u32bit& E, + u32bit msg, u32bit rot) + { + E += rotate_left(A, rot) + (B ^ C ^ D) + msg + 0x8F1BBCDC; + B = rotate_left(B, 30); + } + +} + +/* +* HAS-160 Compression Function +*/ +void HAS_160::compress_n(const byte input[], u32bit blocks) + { + for(u32bit i = 0; i != blocks; ++i) + { + for(u32bit j = 0; j != 16; ++j) + X[j] = load_le<u32bit>(input, j); + + u32bit A = digest[0], B = digest[1], C = digest[2], + D = digest[3], E = digest[4]; + + X[16] = X[ 0] ^ X[ 1] ^ X[ 2] ^ X[ 3]; + X[17] = X[ 4] ^ X[ 5] ^ X[ 6] ^ X[ 7]; + X[18] = X[ 8] ^ X[ 9] ^ X[10] ^ X[11]; + X[19] = X[12] ^ X[13] ^ X[14] ^ X[15]; + F1(A,B,C,D,E,X[18], 5); F1(E,A,B,C,D,X[ 0],11); + F1(D,E,A,B,C,X[ 1], 7); F1(C,D,E,A,B,X[ 2],15); + F1(B,C,D,E,A,X[ 3], 6); F1(A,B,C,D,E,X[19],13); + F1(E,A,B,C,D,X[ 4], 8); F1(D,E,A,B,C,X[ 5],14); + F1(C,D,E,A,B,X[ 6], 7); F1(B,C,D,E,A,X[ 7],12); + F1(A,B,C,D,E,X[16], 9); F1(E,A,B,C,D,X[ 8],11); + F1(D,E,A,B,C,X[ 9], 8); F1(C,D,E,A,B,X[10],15); + F1(B,C,D,E,A,X[11], 6); F1(A,B,C,D,E,X[17],12); + F1(E,A,B,C,D,X[12], 9); F1(D,E,A,B,C,X[13],14); + F1(C,D,E,A,B,X[14], 5); F1(B,C,D,E,A,X[15],13); + + X[16] = X[ 3] ^ X[ 6] ^ X[ 9] ^ X[12]; + X[17] = X[ 2] ^ X[ 5] ^ X[ 8] ^ X[15]; + X[18] = X[ 1] ^ X[ 4] ^ X[11] ^ X[14]; + X[19] = X[ 0] ^ X[ 7] ^ X[10] ^ X[13]; + F2(A,B,C,D,E,X[18], 5); F2(E,A,B,C,D,X[ 3],11); + F2(D,E,A,B,C,X[ 6], 7); F2(C,D,E,A,B,X[ 9],15); + F2(B,C,D,E,A,X[12], 6); F2(A,B,C,D,E,X[19],13); + F2(E,A,B,C,D,X[15], 8); F2(D,E,A,B,C,X[ 2],14); + F2(C,D,E,A,B,X[ 5], 7); F2(B,C,D,E,A,X[ 8],12); + F2(A,B,C,D,E,X[16], 9); F2(E,A,B,C,D,X[11],11); + F2(D,E,A,B,C,X[14], 8); F2(C,D,E,A,B,X[ 1],15); + F2(B,C,D,E,A,X[ 4], 6); F2(A,B,C,D,E,X[17],12); + F2(E,A,B,C,D,X[ 7], 9); F2(D,E,A,B,C,X[10],14); + F2(C,D,E,A,B,X[13], 5); F2(B,C,D,E,A,X[ 0],13); + + X[16] = X[ 5] ^ X[ 7] ^ X[12] ^ X[14]; + X[17] = X[ 0] ^ X[ 2] ^ X[ 9] ^ X[11]; + X[18] = X[ 4] ^ X[ 6] ^ X[13] ^ X[15]; + X[19] = X[ 1] ^ X[ 3] ^ X[ 8] ^ X[10]; + F3(A,B,C,D,E,X[18], 5); F3(E,A,B,C,D,X[12],11); + F3(D,E,A,B,C,X[ 5], 7); F3(C,D,E,A,B,X[14],15); + F3(B,C,D,E,A,X[ 7], 6); F3(A,B,C,D,E,X[19],13); + F3(E,A,B,C,D,X[ 0], 8); F3(D,E,A,B,C,X[ 9],14); + F3(C,D,E,A,B,X[ 2], 7); F3(B,C,D,E,A,X[11],12); + F3(A,B,C,D,E,X[16], 9); F3(E,A,B,C,D,X[ 4],11); + F3(D,E,A,B,C,X[13], 8); F3(C,D,E,A,B,X[ 6],15); + F3(B,C,D,E,A,X[15], 6); F3(A,B,C,D,E,X[17],12); + F3(E,A,B,C,D,X[ 8], 9); F3(D,E,A,B,C,X[ 1],14); + F3(C,D,E,A,B,X[10], 5); F3(B,C,D,E,A,X[ 3],13); + + X[16] = X[ 2] ^ X[ 7] ^ X[ 8] ^ X[13]; + X[17] = X[ 3] ^ X[ 4] ^ X[ 9] ^ X[14]; + X[18] = X[ 0] ^ X[ 5] ^ X[10] ^ X[15]; + X[19] = X[ 1] ^ X[ 6] ^ X[11] ^ X[12]; + F4(A,B,C,D,E,X[18], 5); F4(E,A,B,C,D,X[ 7],11); + F4(D,E,A,B,C,X[ 2], 7); F4(C,D,E,A,B,X[13],15); + F4(B,C,D,E,A,X[ 8], 6); F4(A,B,C,D,E,X[19],13); + F4(E,A,B,C,D,X[ 3], 8); F4(D,E,A,B,C,X[14],14); + F4(C,D,E,A,B,X[ 9], 7); F4(B,C,D,E,A,X[ 4],12); + F4(A,B,C,D,E,X[16], 9); F4(E,A,B,C,D,X[15],11); + F4(D,E,A,B,C,X[10], 8); F4(C,D,E,A,B,X[ 5],15); + F4(B,C,D,E,A,X[ 0], 6); F4(A,B,C,D,E,X[17],12); + F4(E,A,B,C,D,X[11], 9); F4(D,E,A,B,C,X[ 6],14); + F4(C,D,E,A,B,X[ 1], 5); F4(B,C,D,E,A,X[12],13); + + digest[0] += A; digest[1] += B; digest[2] += C; + digest[3] += D; digest[4] += E; + } + } + +/* +* Copy out the digest +*/ +void HAS_160::copy_out(byte output[]) + { + for(u32bit j = 0; j != OUTPUT_LENGTH; j += 4) + store_le(digest[j/4], output + j); + } + +/* +* Clear memory of sensitive data +*/ +void HAS_160::clear() throw() + { + MDx_HashFunction::clear(); + X.clear(); + digest[0] = 0x67452301; + digest[1] = 0xEFCDAB89; + digest[2] = 0x98BADCFE; + digest[3] = 0x10325476; + digest[4] = 0xC3D2E1F0; + } + +} diff --git a/botan/src/hash/has160/has160.h b/botan/src/hash/has160/has160.h new file mode 100644 index 0000000..44bb63b --- /dev/null +++ b/botan/src/hash/has160/has160.h @@ -0,0 +1,35 @@ +/* +* HAS-160 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_HAS_160_H__ +#define BOTAN_HAS_160_H__ + +#include <botan/mdx_hash.h> + +namespace Botan { + +/* +* HAS-160 +*/ +class BOTAN_DLL HAS_160 : public MDx_HashFunction + { + public: + void clear() throw(); + std::string name() const { return "HAS-160"; } + HashFunction* clone() const { return new HAS_160; } + HAS_160() : MDx_HashFunction(20, 64, false, true) { clear(); } + private: + void compress_n(const byte[], u32bit blocks); + void copy_out(byte[]); + + SecureBuffer<u32bit, 20> X; + SecureBuffer<u32bit, 5> digest; + }; + +} + +#endif diff --git a/botan/src/hash/has160/info.txt b/botan/src/hash/has160/info.txt new file mode 100644 index 0000000..a945f32 --- /dev/null +++ b/botan/src/hash/has160/info.txt @@ -0,0 +1,14 @@ +realname "HAS-160" + +define HAS_160 + +load_on auto + +<add> +has160.cpp +has160.h +</add> + +<requires> +mdx_hash +</requires> diff --git a/botan/src/hash/hash.h b/botan/src/hash/hash.h new file mode 100644 index 0000000..a30234b --- /dev/null +++ b/botan/src/hash/hash.h @@ -0,0 +1,52 @@ +/** +* Hash Function Base Class +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_HASH_FUNCTION_BASE_CLASS_H__ +#define BOTAN_HASH_FUNCTION_BASE_CLASS_H__ + +#include <botan/buf_comp.h> +#include <string> + +namespace Botan { + +/** +* This class represents hash function (message digest) objects. +*/ +class BOTAN_DLL HashFunction : public BufferedComputation + { + public: + /** + * The hash block size as defined for this algorithm. + */ + const u32bit HASH_BLOCK_SIZE; + + /** + * Get a new object representing the same algorithm as *this + */ + virtual HashFunction* clone() const = 0; + + /** + * Get the name of this algorithm. + * @return the name of this algorithm + */ + virtual std::string name() const = 0; + + /** + * Reset the internal state of this object. + */ + virtual void clear() throw() = 0; + + HashFunction(u32bit hash_len, u32bit block_len = 0) : + BufferedComputation(hash_len), HASH_BLOCK_SIZE(block_len) {} + virtual ~HashFunction() {} + private: + HashFunction& operator=(const HashFunction&); + }; + +} + +#endif diff --git a/botan/src/hash/info.txt b/botan/src/hash/info.txt new file mode 100644 index 0000000..ce55f7d --- /dev/null +++ b/botan/src/hash/info.txt @@ -0,0 +1,11 @@ +realname "Hash Functions" + +load_on auto + +<add> +hash.h +</add> + +<requires> +buf_comp +</requires> diff --git a/botan/src/hash/md2/info.txt b/botan/src/hash/md2/info.txt new file mode 100644 index 0000000..ff33e1e --- /dev/null +++ b/botan/src/hash/md2/info.txt @@ -0,0 +1,10 @@ +realname "MD2" + +define MD2 + +load_on auto + +<add> +md2.cpp +md2.h +</add> diff --git a/botan/src/hash/md2/md2.cpp b/botan/src/hash/md2/md2.cpp new file mode 100644 index 0000000..c67e72b --- /dev/null +++ b/botan/src/hash/md2/md2.cpp @@ -0,0 +1,108 @@ +/* +* MD2 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/md2.h> +#include <botan/xor_buf.h> + +namespace Botan { + +/** +* MD2 Compression Function +*/ +void MD2::hash(const byte input[]) + { + static const byte SBOX[256] = { + 0x29, 0x2E, 0x43, 0xC9, 0xA2, 0xD8, 0x7C, 0x01, 0x3D, 0x36, 0x54, 0xA1, + 0xEC, 0xF0, 0x06, 0x13, 0x62, 0xA7, 0x05, 0xF3, 0xC0, 0xC7, 0x73, 0x8C, + 0x98, 0x93, 0x2B, 0xD9, 0xBC, 0x4C, 0x82, 0xCA, 0x1E, 0x9B, 0x57, 0x3C, + 0xFD, 0xD4, 0xE0, 0x16, 0x67, 0x42, 0x6F, 0x18, 0x8A, 0x17, 0xE5, 0x12, + 0xBE, 0x4E, 0xC4, 0xD6, 0xDA, 0x9E, 0xDE, 0x49, 0xA0, 0xFB, 0xF5, 0x8E, + 0xBB, 0x2F, 0xEE, 0x7A, 0xA9, 0x68, 0x79, 0x91, 0x15, 0xB2, 0x07, 0x3F, + 0x94, 0xC2, 0x10, 0x89, 0x0B, 0x22, 0x5F, 0x21, 0x80, 0x7F, 0x5D, 0x9A, + 0x5A, 0x90, 0x32, 0x27, 0x35, 0x3E, 0xCC, 0xE7, 0xBF, 0xF7, 0x97, 0x03, + 0xFF, 0x19, 0x30, 0xB3, 0x48, 0xA5, 0xB5, 0xD1, 0xD7, 0x5E, 0x92, 0x2A, + 0xAC, 0x56, 0xAA, 0xC6, 0x4F, 0xB8, 0x38, 0xD2, 0x96, 0xA4, 0x7D, 0xB6, + 0x76, 0xFC, 0x6B, 0xE2, 0x9C, 0x74, 0x04, 0xF1, 0x45, 0x9D, 0x70, 0x59, + 0x64, 0x71, 0x87, 0x20, 0x86, 0x5B, 0xCF, 0x65, 0xE6, 0x2D, 0xA8, 0x02, + 0x1B, 0x60, 0x25, 0xAD, 0xAE, 0xB0, 0xB9, 0xF6, 0x1C, 0x46, 0x61, 0x69, + 0x34, 0x40, 0x7E, 0x0F, 0x55, 0x47, 0xA3, 0x23, 0xDD, 0x51, 0xAF, 0x3A, + 0xC3, 0x5C, 0xF9, 0xCE, 0xBA, 0xC5, 0xEA, 0x26, 0x2C, 0x53, 0x0D, 0x6E, + 0x85, 0x28, 0x84, 0x09, 0xD3, 0xDF, 0xCD, 0xF4, 0x41, 0x81, 0x4D, 0x52, + 0x6A, 0xDC, 0x37, 0xC8, 0x6C, 0xC1, 0xAB, 0xFA, 0x24, 0xE1, 0x7B, 0x08, + 0x0C, 0xBD, 0xB1, 0x4A, 0x78, 0x88, 0x95, 0x8B, 0xE3, 0x63, 0xE8, 0x6D, + 0xE9, 0xCB, 0xD5, 0xFE, 0x3B, 0x00, 0x1D, 0x39, 0xF2, 0xEF, 0xB7, 0x0E, + 0x66, 0x58, 0xD0, 0xE4, 0xA6, 0x77, 0x72, 0xF8, 0xEB, 0x75, 0x4B, 0x0A, + 0x31, 0x44, 0x50, 0xB4, 0x8F, 0xED, 0x1F, 0x1A, 0xDB, 0x99, 0x8D, 0x33, + 0x9F, 0x11, 0x83, 0x14 }; + + X.copy(16, input, HASH_BLOCK_SIZE); + xor_buf(X + 32, X, X + 16, HASH_BLOCK_SIZE); + byte T = 0; + for(u32bit j = 0; j != 18; ++j) + { + for(u32bit k = 0; k != 48; k += 8) + { + T = X[k ] ^= SBOX[T]; T = X[k+1] ^= SBOX[T]; + T = X[k+2] ^= SBOX[T]; T = X[k+3] ^= SBOX[T]; + T = X[k+4] ^= SBOX[T]; T = X[k+5] ^= SBOX[T]; + T = X[k+6] ^= SBOX[T]; T = X[k+7] ^= SBOX[T]; + } + T += j; + } + T = checksum[15]; + for(u32bit j = 0; j != HASH_BLOCK_SIZE; ++j) + T = checksum[j] ^= SBOX[input[j] ^ T]; + } + +/** +* Update the hash +*/ +void MD2::add_data(const byte input[], u32bit length) + { + buffer.copy(position, input, length); + if(position + length >= HASH_BLOCK_SIZE) + { + hash(buffer.begin()); + input += (HASH_BLOCK_SIZE - position); + length -= (HASH_BLOCK_SIZE - position); + while(length >= HASH_BLOCK_SIZE) + { + hash(input); + input += HASH_BLOCK_SIZE; + length -= HASH_BLOCK_SIZE; + } + buffer.copy(input, length); + position = 0; + } + position += length; + } + +/** +* Finalize a MD2 Hash +*/ +void MD2::final_result(byte output[]) + { + for(u32bit j = position; j != HASH_BLOCK_SIZE; ++j) + buffer[j] = static_cast<byte>(HASH_BLOCK_SIZE - position); + hash(buffer); + hash(checksum); + copy_mem(output, X.begin(), OUTPUT_LENGTH); + clear(); + } + +/** +* Clear memory of sensitive data +*/ +void MD2::clear() throw() + { + X.clear(); + checksum.clear(); + buffer.clear(); + position = 0; + } + +} diff --git a/botan/src/hash/md2/md2.h b/botan/src/hash/md2/md2.h new file mode 100644 index 0000000..9337c43 --- /dev/null +++ b/botan/src/hash/md2/md2.h @@ -0,0 +1,37 @@ +/* +* MD2 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_MD2_H__ +#define BOTAN_MD2_H__ + +#include <botan/hash.h> + +namespace Botan { + +/* +* MD2 +*/ +class BOTAN_DLL MD2 : public HashFunction + { + public: + void clear() throw(); + std::string name() const { return "MD2"; } + HashFunction* clone() const { return new MD2; } + MD2() : HashFunction(16, 16) { clear(); } + private: + void add_data(const byte[], u32bit); + void hash(const byte[]); + void final_result(byte[]); + + SecureBuffer<byte, 48> X; + SecureBuffer<byte, 16> checksum, buffer; + u32bit position; + }; + +} + +#endif diff --git a/botan/src/hash/md4/info.txt b/botan/src/hash/md4/info.txt new file mode 100644 index 0000000..fc9cbe1 --- /dev/null +++ b/botan/src/hash/md4/info.txt @@ -0,0 +1,14 @@ +realname "MD4" + +define MD4 + +load_on auto + +<add> +md4.cpp +md4.h +</add> + +<requires> +mdx_hash +</requires> diff --git a/botan/src/hash/md4/md4.cpp b/botan/src/hash/md4/md4.cpp new file mode 100644 index 0000000..39e3c8c --- /dev/null +++ b/botan/src/hash/md4/md4.cpp @@ -0,0 +1,108 @@ +/* +* MD4 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/md4.h> +#include <botan/loadstor.h> +#include <botan/rotate.h> + +namespace Botan { + +namespace { + +/* +* MD4 FF Function +*/ +inline void FF(u32bit& A, u32bit B, u32bit C, u32bit D, u32bit M, byte S) + { + A += (D ^ (B & (C ^ D))) + M; + A = rotate_left(A, S); + } + +/* +* MD4 GG Function +*/ +inline void GG(u32bit& A, u32bit B, u32bit C, u32bit D, u32bit M, byte S) + { + A += ((B & C) | (D & (B | C))) + M + 0x5A827999; + A = rotate_left(A, S); + } + +/* +* MD4 HH Function +*/ +inline void HH(u32bit& A, u32bit B, u32bit C, u32bit D, u32bit M, byte S) + { + A += (B ^ C ^ D) + M + 0x6ED9EBA1; + A = rotate_left(A, S); + } + +} + +/* +* MD4 Compression Function +*/ +void MD4::compress_n(const byte input[], u32bit blocks) + { + u32bit A = digest[0], B = digest[1], C = digest[2], D = digest[3]; + + for(u32bit i = 0; i != blocks; ++i) + { + for(u32bit j = 0; j != 16; ++j) + M[j] = load_le<u32bit>(input, j); + input += HASH_BLOCK_SIZE; + + FF(A,B,C,D,M[ 0], 3); FF(D,A,B,C,M[ 1], 7); FF(C,D,A,B,M[ 2],11); + FF(B,C,D,A,M[ 3],19); FF(A,B,C,D,M[ 4], 3); FF(D,A,B,C,M[ 5], 7); + FF(C,D,A,B,M[ 6],11); FF(B,C,D,A,M[ 7],19); FF(A,B,C,D,M[ 8], 3); + FF(D,A,B,C,M[ 9], 7); FF(C,D,A,B,M[10],11); FF(B,C,D,A,M[11],19); + FF(A,B,C,D,M[12], 3); FF(D,A,B,C,M[13], 7); FF(C,D,A,B,M[14],11); + FF(B,C,D,A,M[15],19); + + GG(A,B,C,D,M[ 0], 3); GG(D,A,B,C,M[ 4], 5); GG(C,D,A,B,M[ 8], 9); + GG(B,C,D,A,M[12],13); GG(A,B,C,D,M[ 1], 3); GG(D,A,B,C,M[ 5], 5); + GG(C,D,A,B,M[ 9], 9); GG(B,C,D,A,M[13],13); GG(A,B,C,D,M[ 2], 3); + GG(D,A,B,C,M[ 6], 5); GG(C,D,A,B,M[10], 9); GG(B,C,D,A,M[14],13); + GG(A,B,C,D,M[ 3], 3); GG(D,A,B,C,M[ 7], 5); GG(C,D,A,B,M[11], 9); + GG(B,C,D,A,M[15],13); + + HH(A,B,C,D,M[ 0], 3); HH(D,A,B,C,M[ 8], 9); HH(C,D,A,B,M[ 4],11); + HH(B,C,D,A,M[12],15); HH(A,B,C,D,M[ 2], 3); HH(D,A,B,C,M[10], 9); + HH(C,D,A,B,M[ 6],11); HH(B,C,D,A,M[14],15); HH(A,B,C,D,M[ 1], 3); + HH(D,A,B,C,M[ 9], 9); HH(C,D,A,B,M[ 5],11); HH(B,C,D,A,M[13],15); + HH(A,B,C,D,M[ 3], 3); HH(D,A,B,C,M[11], 9); HH(C,D,A,B,M[ 7],11); + HH(B,C,D,A,M[15],15); + + A = (digest[0] += A); + B = (digest[1] += B); + C = (digest[2] += C); + D = (digest[3] += D); + } + } + +/* +* Copy out the digest +*/ +void MD4::copy_out(byte output[]) + { + for(u32bit j = 0; j != OUTPUT_LENGTH; j += 4) + store_le(digest[j/4], output + j); + } + +/* +* Clear memory of sensitive data +*/ +void MD4::clear() throw() + { + MDx_HashFunction::clear(); + M.clear(); + digest[0] = 0x67452301; + digest[1] = 0xEFCDAB89; + digest[2] = 0x98BADCFE; + digest[3] = 0x10325476; + } + +} diff --git a/botan/src/hash/md4/md4.h b/botan/src/hash/md4/md4.h new file mode 100644 index 0000000..df6f229 --- /dev/null +++ b/botan/src/hash/md4/md4.h @@ -0,0 +1,36 @@ +/* +* MD4 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_MD4_H__ +#define BOTAN_MD4_H__ + +#include <botan/mdx_hash.h> + +namespace Botan { + +/* +* MD4 +*/ +class BOTAN_DLL MD4 : public MDx_HashFunction + { + public: + void clear() throw(); + std::string name() const { return "MD4"; } + HashFunction* clone() const { return new MD4; } + MD4() : MDx_HashFunction(16, 64, false, true) { clear(); } + protected: + void compress_n(const byte input[], u32bit blocks); + void hash_old(const byte[]); + void copy_out(byte[]); + + SecureBuffer<u32bit, 48> M; + SecureBuffer<u32bit, 4> digest; + }; + +} + +#endif diff --git a/botan/src/hash/md4_ia32/info.txt b/botan/src/hash/md4_ia32/info.txt new file mode 100644 index 0000000..fee7dd1 --- /dev/null +++ b/botan/src/hash/md4_ia32/info.txt @@ -0,0 +1,35 @@ +realname "MD4 (IA-32)" + +define MD4_IA32 + +load_on asm_ok + +<add> +md4_ia32_imp.S +md4_ia32.cpp +md4_ia32.h +</add> + +<arch> +ia32 +</arch> + +<cc> +gcc +icc +</cc> + +# ELF systems +<os> +linux +freebsd +dragonfly +netbsd +openbsd +solaris +</os> + +<requires> +asm_ia32 +md4 +</requires> diff --git a/botan/src/hash/md4_ia32/md4_ia32.cpp b/botan/src/hash/md4_ia32/md4_ia32.cpp new file mode 100644 index 0000000..12fe71d --- /dev/null +++ b/botan/src/hash/md4_ia32/md4_ia32.cpp @@ -0,0 +1,27 @@ +/* +* MD4 (IA-32) +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/md4_ia32.h> +#include <botan/loadstor.h> + +namespace Botan { + +extern "C" void botan_md4_ia32_compress(u32bit[4], const byte[64], u32bit[16]); + +/* +* MD4 Compression Function +*/ +void MD4_IA32::compress_n(const byte input[], u32bit blocks) + { + for(u32bit i = 0; i != blocks; ++i) + { + botan_md4_ia32_compress(digest, input, M); + input += HASH_BLOCK_SIZE; + } + } + +} diff --git a/botan/src/hash/md4_ia32/md4_ia32.h b/botan/src/hash/md4_ia32/md4_ia32.h new file mode 100644 index 0000000..f01d148 --- /dev/null +++ b/botan/src/hash/md4_ia32/md4_ia32.h @@ -0,0 +1,28 @@ +/* +* MD4 (IA-32) +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_MD4_IA32_H__ +#define BOTAN_MD4_IA32_H__ + +#include <botan/md4.h> + +namespace Botan { + +/* +* MD4 +*/ +class BOTAN_DLL MD4_IA32 : public MD4 + { + public: + HashFunction* clone() const { return new MD4_IA32; } + private: + void compress_n(const byte[], u32bit blocks); + }; + +} + +#endif diff --git a/botan/src/hash/md4_ia32/md4_ia32_imp.S b/botan/src/hash/md4_ia32/md4_ia32_imp.S new file mode 100644 index 0000000..ca04cbe --- /dev/null +++ b/botan/src/hash/md4_ia32/md4_ia32_imp.S @@ -0,0 +1,137 @@ +/* +* MD4 Source File +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/asm_macr.h> + +START_LISTING(md4_ia32.S) + +START_FUNCTION(botan_md4_ia32_compress) + SPILL_REGS() + +#define PUSHED 4 + + ASSIGN(EBP, ARG(2)) /* input block */ + ASSIGN(EDI, ARG(3)) /* expanded words */ + + ZEROIZE(ESI) + +START_LOOP(.LOAD_INPUT) + ADD_IMM(ESI, 4) + + ASSIGN(EAX, ARRAY4(EBP, 0)) + ASSIGN(EBX, ARRAY4(EBP, 1)) + ASSIGN(ECX, ARRAY4(EBP, 2)) + ASSIGN(EDX, ARRAY4(EBP, 3)) + + ADD_IMM(EBP, 16) + + ASSIGN(ARRAY4_INDIRECT(EDI,ESI,-4), EAX) + ASSIGN(ARRAY4_INDIRECT(EDI,ESI,-3), EBX) + ASSIGN(ARRAY4_INDIRECT(EDI,ESI,-2), ECX) + ASSIGN(ARRAY4_INDIRECT(EDI,ESI,-1), EDX) +LOOP_UNTIL_EQ(ESI, 16, .LOAD_INPUT) + + ASSIGN(EBP, ARG(1)) + ASSIGN(EAX, ARRAY4(EBP, 0)) + ASSIGN(EBX, ARRAY4(EBP, 1)) + ASSIGN(ECX, ARRAY4(EBP, 2)) + ASSIGN(EDX, ARRAY4(EBP, 3)) + +#define MSG EDI +#define T1 ESI +#define T2 EBP + +#define FF(A, B, C, D, N, S) \ + ASSIGN(T1, ARRAY4(MSG, N)) ; \ + ASSIGN(T2, C) ; \ + XOR(T2, D) ; \ + AND(T2, B) ; \ + XOR(T2, D) ; \ + ADD(A, T1) ; \ + ADD(A, T2) ; \ + ROTL_IMM(A, S) ; + +#define GG(A, B, C, D, N, S) \ + ASSIGN(T1, ARRAY4(MSG, N)) ; \ + ASSIGN(T2, B) ; \ + OR(T2, C) ; \ + AND(T2, D) ; \ + ADD3_IMM(A, T1, 0x5A827999) ; \ + ASSIGN(T1, B) ; \ + AND(T1, C) ; \ + OR(T2, T1) ; \ + ADD(A, T2) ; \ + ROTL_IMM(A, S) ; + +#define HH(A, B, C, D, N, S) \ + ASSIGN(T1, ARRAY4(MSG, N)) ; \ + ASSIGN(T2, B) ; \ + XOR(T2, C) ; \ + XOR(T2, D) ; \ + ADD3_IMM(A, T1, 0x6ED9EBA1) ; \ + ADD(A, T2) ; \ + ROTL_IMM(A, S) ; + + FF(EAX,EBX,ECX,EDX, 0, 3); + FF(EDX,EAX,EBX,ECX, 1, 7); + FF(ECX,EDX,EAX,EBX, 2,11); + FF(EBX,ECX,EDX,EAX, 3,19); + FF(EAX,EBX,ECX,EDX, 4, 3); + FF(EDX,EAX,EBX,ECX, 5, 7); + FF(ECX,EDX,EAX,EBX, 6,11); + FF(EBX,ECX,EDX,EAX, 7,19); + FF(EAX,EBX,ECX,EDX, 8, 3); + FF(EDX,EAX,EBX,ECX, 9, 7); + FF(ECX,EDX,EAX,EBX,10,11); + FF(EBX,ECX,EDX,EAX,11,19); + FF(EAX,EBX,ECX,EDX,12, 3); + FF(EDX,EAX,EBX,ECX,13, 7); + FF(ECX,EDX,EAX,EBX,14,11); + FF(EBX,ECX,EDX,EAX,15,19); + + GG(EAX,EBX,ECX,EDX, 0, 3); + GG(EDX,EAX,EBX,ECX, 4, 5); + GG(ECX,EDX,EAX,EBX, 8, 9); + GG(EBX,ECX,EDX,EAX,12,13); + GG(EAX,EBX,ECX,EDX, 1, 3); + GG(EDX,EAX,EBX,ECX, 5, 5); + GG(ECX,EDX,EAX,EBX, 9, 9); + GG(EBX,ECX,EDX,EAX,13,13); + GG(EAX,EBX,ECX,EDX, 2, 3); + GG(EDX,EAX,EBX,ECX, 6, 5); + GG(ECX,EDX,EAX,EBX,10, 9); + GG(EBX,ECX,EDX,EAX,14,13); + GG(EAX,EBX,ECX,EDX, 3, 3); + GG(EDX,EAX,EBX,ECX, 7, 5); + GG(ECX,EDX,EAX,EBX,11, 9); + GG(EBX,ECX,EDX,EAX,15,13); + + HH(EAX,EBX,ECX,EDX, 0, 3); + HH(EDX,EAX,EBX,ECX, 8, 9); + HH(ECX,EDX,EAX,EBX, 4,11); + HH(EBX,ECX,EDX,EAX,12,15); + HH(EAX,EBX,ECX,EDX, 2, 3); + HH(EDX,EAX,EBX,ECX,10, 9); + HH(ECX,EDX,EAX,EBX, 6,11); + HH(EBX,ECX,EDX,EAX,14,15); + HH(EAX,EBX,ECX,EDX, 1, 3); + HH(EDX,EAX,EBX,ECX, 9, 9); + HH(ECX,EDX,EAX,EBX, 5,11); + HH(EBX,ECX,EDX,EAX,13,15); + HH(EAX,EBX,ECX,EDX, 3, 3); + HH(EDX,EAX,EBX,ECX,11, 9); + HH(ECX,EDX,EAX,EBX, 7,11); + HH(EBX,ECX,EDX,EAX,15,15); + + ASSIGN(EBP, ARG(1)) + ADD(ARRAY4(EBP, 0), EAX) + ADD(ARRAY4(EBP, 1), EBX) + ADD(ARRAY4(EBP, 2), ECX) + ADD(ARRAY4(EBP, 3), EDX) + + RESTORE_REGS() +END_FUNCTION(botan_md4_ia32_compress) diff --git a/botan/src/hash/md5/info.txt b/botan/src/hash/md5/info.txt new file mode 100644 index 0000000..525a45a --- /dev/null +++ b/botan/src/hash/md5/info.txt @@ -0,0 +1,14 @@ +realname "MD5" + +define MD5 + +load_on auto + +<add> +md5.cpp +md5.h +</add> + +<requires> +mdx_hash +</requires> diff --git a/botan/src/hash/md5/md5.cpp b/botan/src/hash/md5/md5.cpp new file mode 100644 index 0000000..7c280aa --- /dev/null +++ b/botan/src/hash/md5/md5.cpp @@ -0,0 +1,136 @@ +/* +* MD5 +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/md5.h> +#include <botan/loadstor.h> +#include <botan/rotate.h> + +namespace Botan { + +namespace { + +/* +* MD5 FF Function +*/ +inline void FF(u32bit& A, u32bit B, u32bit C, u32bit D, u32bit msg, + byte S, u32bit magic) + { + A += (D ^ (B & (C ^ D))) + msg + magic; + A = rotate_left(A, S) + B; + } + +/* +* MD5 GG Function +*/ +inline void GG(u32bit& A, u32bit B, u32bit C, u32bit D, u32bit msg, + byte S, u32bit magic) + { + A += (C ^ (D & (B ^ C))) + msg + magic; + A = rotate_left(A, S) + B; + } + +/* +* MD5 HH Function +*/ +inline void HH(u32bit& A, u32bit B, u32bit C, u32bit D, u32bit msg, + byte S, u32bit magic) + { + A += (B ^ C ^ D) + msg + magic; + A = rotate_left(A, S) + B; + } + +/* +* MD5 II Function +*/ +inline void II(u32bit& A, u32bit B, u32bit C, u32bit D, u32bit msg, + byte S, u32bit magic) + { + A += (C ^ (B | ~D)) + msg + magic; + A = rotate_left(A, S) + B; + } + +} + +/* +* MD5 Compression Function +*/ +void MD5::compress_n(const byte input[], u32bit blocks) + { + u32bit A = digest[0], B = digest[1], C = digest[2], D = digest[3]; + + for(u32bit i = 0; i != blocks; ++i) + { + for(u32bit j = 0; j != 16; ++j) + M[j] = load_le<u32bit>(input, j); + input += HASH_BLOCK_SIZE; + + FF(A,B,C,D,M[ 0], 7,0xD76AA478); FF(D,A,B,C,M[ 1],12,0xE8C7B756); + FF(C,D,A,B,M[ 2],17,0x242070DB); FF(B,C,D,A,M[ 3],22,0xC1BDCEEE); + FF(A,B,C,D,M[ 4], 7,0xF57C0FAF); FF(D,A,B,C,M[ 5],12,0x4787C62A); + FF(C,D,A,B,M[ 6],17,0xA8304613); FF(B,C,D,A,M[ 7],22,0xFD469501); + FF(A,B,C,D,M[ 8], 7,0x698098D8); FF(D,A,B,C,M[ 9],12,0x8B44F7AF); + FF(C,D,A,B,M[10],17,0xFFFF5BB1); FF(B,C,D,A,M[11],22,0x895CD7BE); + FF(A,B,C,D,M[12], 7,0x6B901122); FF(D,A,B,C,M[13],12,0xFD987193); + FF(C,D,A,B,M[14],17,0xA679438E); FF(B,C,D,A,M[15],22,0x49B40821); + + GG(A,B,C,D,M[ 1], 5,0xF61E2562); GG(D,A,B,C,M[ 6], 9,0xC040B340); + GG(C,D,A,B,M[11],14,0x265E5A51); GG(B,C,D,A,M[ 0],20,0xE9B6C7AA); + GG(A,B,C,D,M[ 5], 5,0xD62F105D); GG(D,A,B,C,M[10], 9,0x02441453); + GG(C,D,A,B,M[15],14,0xD8A1E681); GG(B,C,D,A,M[ 4],20,0xE7D3FBC8); + GG(A,B,C,D,M[ 9], 5,0x21E1CDE6); GG(D,A,B,C,M[14], 9,0xC33707D6); + GG(C,D,A,B,M[ 3],14,0xF4D50D87); GG(B,C,D,A,M[ 8],20,0x455A14ED); + GG(A,B,C,D,M[13], 5,0xA9E3E905); GG(D,A,B,C,M[ 2], 9,0xFCEFA3F8); + GG(C,D,A,B,M[ 7],14,0x676F02D9); GG(B,C,D,A,M[12],20,0x8D2A4C8A); + + HH(A,B,C,D,M[ 5], 4,0xFFFA3942); HH(D,A,B,C,M[ 8],11,0x8771F681); + HH(C,D,A,B,M[11],16,0x6D9D6122); HH(B,C,D,A,M[14],23,0xFDE5380C); + HH(A,B,C,D,M[ 1], 4,0xA4BEEA44); HH(D,A,B,C,M[ 4],11,0x4BDECFA9); + HH(C,D,A,B,M[ 7],16,0xF6BB4B60); HH(B,C,D,A,M[10],23,0xBEBFBC70); + HH(A,B,C,D,M[13], 4,0x289B7EC6); HH(D,A,B,C,M[ 0],11,0xEAA127FA); + HH(C,D,A,B,M[ 3],16,0xD4EF3085); HH(B,C,D,A,M[ 6],23,0x04881D05); + HH(A,B,C,D,M[ 9], 4,0xD9D4D039); HH(D,A,B,C,M[12],11,0xE6DB99E5); + HH(C,D,A,B,M[15],16,0x1FA27CF8); HH(B,C,D,A,M[ 2],23,0xC4AC5665); + + II(A,B,C,D,M[ 0], 6,0xF4292244); II(D,A,B,C,M[ 7],10,0x432AFF97); + II(C,D,A,B,M[14],15,0xAB9423A7); II(B,C,D,A,M[ 5],21,0xFC93A039); + II(A,B,C,D,M[12], 6,0x655B59C3); II(D,A,B,C,M[ 3],10,0x8F0CCC92); + II(C,D,A,B,M[10],15,0xFFEFF47D); II(B,C,D,A,M[ 1],21,0x85845DD1); + II(A,B,C,D,M[ 8], 6,0x6FA87E4F); II(D,A,B,C,M[15],10,0xFE2CE6E0); + II(C,D,A,B,M[ 6],15,0xA3014314); II(B,C,D,A,M[13],21,0x4E0811A1); + II(A,B,C,D,M[ 4], 6,0xF7537E82); II(D,A,B,C,M[11],10,0xBD3AF235); + II(C,D,A,B,M[ 2],15,0x2AD7D2BB); II(B,C,D,A,M[ 9],21,0xEB86D391); + + A = (digest[0] += A); + B = (digest[1] += B); + C = (digest[2] += C); + D = (digest[3] += D); + } + } + +/* +* Copy out the digest +*/ +void MD5::copy_out(byte output[]) + { + for(u32bit j = 0; j != OUTPUT_LENGTH; j += 4) + store_le(digest[j/4], output + j); + } + +/* +* Clear memory of sensitive data +*/ +void MD5::clear() throw() + { + MDx_HashFunction::clear(); + M.clear(); + digest[0] = 0x67452301; + digest[1] = 0xEFCDAB89; + digest[2] = 0x98BADCFE; + digest[3] = 0x10325476; + } + +} diff --git a/botan/src/hash/md5/md5.h b/botan/src/hash/md5/md5.h new file mode 100644 index 0000000..85f684d --- /dev/null +++ b/botan/src/hash/md5/md5.h @@ -0,0 +1,35 @@ +/** +* MD5 +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_MD5_H__ +#define BOTAN_MD5_H__ + +#include <botan/mdx_hash.h> + +namespace Botan { + +/** +* MD5 +*/ +class BOTAN_DLL MD5 : public MDx_HashFunction + { + public: + void clear() throw(); + std::string name() const { return "MD5"; } + HashFunction* clone() const { return new MD5; } + MD5() : MDx_HashFunction(16, 64, false, true) { clear(); } + protected: + void compress_n(const byte[], u32bit blocks); + void copy_out(byte[]); + + SecureBuffer<u32bit, 16> M; + SecureBuffer<u32bit, 4> digest; + }; + +} + +#endif diff --git a/botan/src/hash/md5_ia32/info.txt b/botan/src/hash/md5_ia32/info.txt new file mode 100644 index 0000000..ad9923b --- /dev/null +++ b/botan/src/hash/md5_ia32/info.txt @@ -0,0 +1,35 @@ +realname "MD5 (IA-32)" + +define MD5_IA32 + +load_on asm_ok + +<add> +md5_ia32_imp.S +md5_ia32.cpp +md5_ia32.h +</add> + +<arch> +ia32 +</arch> + +<cc> +gcc +icc +</cc> + +# ELF systems +<os> +linux +freebsd +dragonfly +netbsd +openbsd +solaris +</os> + +<requires> +asm_ia32 +md5 +</requires> diff --git a/botan/src/hash/md5_ia32/md5_ia32.cpp b/botan/src/hash/md5_ia32/md5_ia32.cpp new file mode 100644 index 0000000..443569b --- /dev/null +++ b/botan/src/hash/md5_ia32/md5_ia32.cpp @@ -0,0 +1,32 @@ +/* +* MD5 (IA-32) +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/md5_ia32.h> +#include <botan/loadstor.h> + +namespace Botan { + +namespace { + +extern "C" +void botan_md5_ia32_compress(u32bit[4], const byte[64], u32bit[16]); + +} + +/* +* MD5 Compression Function +*/ +void MD5_IA32::compress_n(const byte input[], u32bit blocks) + { + for(u32bit i = 0; i != blocks; ++i) + { + botan_md5_ia32_compress(digest, input, M); + input += HASH_BLOCK_SIZE; + } + } + +} diff --git a/botan/src/hash/md5_ia32/md5_ia32.h b/botan/src/hash/md5_ia32/md5_ia32.h new file mode 100644 index 0000000..723d724 --- /dev/null +++ b/botan/src/hash/md5_ia32/md5_ia32.h @@ -0,0 +1,28 @@ +/* +* MD5 (IA-32) +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_MD5_IA32_H__ +#define BOTAN_MD5_IA32_H__ + +#include <botan/md5.h> + +namespace Botan { + +/* +* MD5 +*/ +class BOTAN_DLL MD5_IA32 : public MD5 + { + public: + HashFunction* clone() const { return new MD5_IA32; } + private: + void compress_n(const byte[], u32bit blocks); + }; + +} + +#endif diff --git a/botan/src/hash/md5_ia32/md5_ia32_imp.S b/botan/src/hash/md5_ia32/md5_ia32_imp.S new file mode 100644 index 0000000..8087bbd --- /dev/null +++ b/botan/src/hash/md5_ia32/md5_ia32_imp.S @@ -0,0 +1,166 @@ +/* +* MD5 Source File +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/asm_macr.h> + +START_LISTING(md5_ia32.S) + +START_FUNCTION(botan_md5_ia32_compress) + SPILL_REGS() + +#define PUSHED 4 + + ASSIGN(EBP, ARG(2)) /* input block */ + ASSIGN(EDI, ARG(3)) /* expanded words */ + + ZEROIZE(ESI) + +START_LOOP(.LOAD_INPUT) + ADD_IMM(ESI, 4) + + ASSIGN(EAX, ARRAY4(EBP, 0)) + ASSIGN(EBX, ARRAY4(EBP, 1)) + ASSIGN(ECX, ARRAY4(EBP, 2)) + ASSIGN(EDX, ARRAY4(EBP, 3)) + + ADD_IMM(EBP, 16) + + ASSIGN(ARRAY4_INDIRECT(EDI,ESI,-4), EAX) + ASSIGN(ARRAY4_INDIRECT(EDI,ESI,-3), EBX) + ASSIGN(ARRAY4_INDIRECT(EDI,ESI,-2), ECX) + ASSIGN(ARRAY4_INDIRECT(EDI,ESI,-1), EDX) +LOOP_UNTIL_EQ(ESI, 16, .LOAD_INPUT) + + ASSIGN(EBP, ARG(1)) + ASSIGN(EAX, ARRAY4(EBP, 0)) + ASSIGN(EBX, ARRAY4(EBP, 1)) + ASSIGN(ECX, ARRAY4(EBP, 2)) + ASSIGN(EDX, ARRAY4(EBP, 3)) + +#define MSG EDI +#define T1 ESI +#define T2 EBP + +#define FF(A, B, C, D, N, S, MAGIC) \ + ASSIGN(T1, ARRAY4(MSG, N)) ; \ + ASSIGN(T2, C) ; \ + XOR(T2, D) ; \ + AND(T2, B) ; \ + XOR(T2, D) ; \ + ADD3_IMM(A, T1, MAGIC) ; \ + ADD(A, T2) ; \ + ROTL_IMM(A, S) ; \ + ADD(A, B) ; + +#define GG(A, B, C, D, N, S, MAGIC) \ + ASSIGN(T1, ARRAY4(MSG, N)) ; \ + ASSIGN(T2, B) ; \ + XOR(T2, C) ; \ + AND(T2, D) ; \ + XOR(T2, C) ; \ + ADD3_IMM(A, T1, MAGIC) ; \ + ADD(A, T2) ; \ + ROTL_IMM(A, S) ; \ + ADD(A, B) ; + +#define HH(A, B, C, D, N, S, MAGIC) \ + ASSIGN(T1, ARRAY4(MSG, N)) ; \ + ASSIGN(T2, B) ; \ + XOR(T2, C) ; \ + XOR(T2, D) ; \ + ADD3_IMM(A, T1, MAGIC) ; \ + ADD(A, T2) ; \ + ROTL_IMM(A, S) ; \ + ADD(A, B) ; + +#define II(A, B, C, D, N, S, MAGIC) \ + ASSIGN(T1, ARRAY4(MSG, N)) ; \ + ASSIGN(T2, D) ; \ + NOT(T2) ; \ + OR(T2, B) ; \ + XOR(T2, C) ; \ + ADD3_IMM(A, T1, MAGIC) ; \ + ADD(A, T2) ; \ + ROTL_IMM(A, S) ; \ + ADD(A, B) ; + + FF(EAX,EBX,ECX,EDX, 0, 7,0xD76AA478); + FF(EDX,EAX,EBX,ECX, 1,12,0xE8C7B756); + FF(ECX,EDX,EAX,EBX, 2,17,0x242070DB); + FF(EBX,ECX,EDX,EAX, 3,22,0xC1BDCEEE); + FF(EAX,EBX,ECX,EDX, 4, 7,0xF57C0FAF); + FF(EDX,EAX,EBX,ECX, 5,12,0x4787C62A); + FF(ECX,EDX,EAX,EBX, 6,17,0xA8304613); + FF(EBX,ECX,EDX,EAX, 7,22,0xFD469501); + FF(EAX,EBX,ECX,EDX, 8, 7,0x698098D8); + FF(EDX,EAX,EBX,ECX, 9,12,0x8B44F7AF); + FF(ECX,EDX,EAX,EBX,10,17,0xFFFF5BB1); + FF(EBX,ECX,EDX,EAX,11,22,0x895CD7BE); + FF(EAX,EBX,ECX,EDX,12, 7,0x6B901122); + FF(EDX,EAX,EBX,ECX,13,12,0xFD987193); + FF(ECX,EDX,EAX,EBX,14,17,0xA679438E); + FF(EBX,ECX,EDX,EAX,15,22,0x49B40821); + + GG(EAX,EBX,ECX,EDX, 1, 5,0xF61E2562); + GG(EDX,EAX,EBX,ECX, 6, 9,0xC040B340); + GG(ECX,EDX,EAX,EBX,11,14,0x265E5A51); + GG(EBX,ECX,EDX,EAX, 0,20,0xE9B6C7AA); + GG(EAX,EBX,ECX,EDX, 5, 5,0xD62F105D); + GG(EDX,EAX,EBX,ECX,10, 9,0x02441453); + GG(ECX,EDX,EAX,EBX,15,14,0xD8A1E681); + GG(EBX,ECX,EDX,EAX, 4,20,0xE7D3FBC8); + GG(EAX,EBX,ECX,EDX, 9, 5,0x21E1CDE6); + GG(EDX,EAX,EBX,ECX,14, 9,0xC33707D6); + GG(ECX,EDX,EAX,EBX, 3,14,0xF4D50D87); + GG(EBX,ECX,EDX,EAX, 8,20,0x455A14ED); + GG(EAX,EBX,ECX,EDX,13, 5,0xA9E3E905); + GG(EDX,EAX,EBX,ECX, 2, 9,0xFCEFA3F8); + GG(ECX,EDX,EAX,EBX, 7,14,0x676F02D9); + GG(EBX,ECX,EDX,EAX,12,20,0x8D2A4C8A); + + HH(EAX,EBX,ECX,EDX, 5, 4,0xFFFA3942); + HH(EDX,EAX,EBX,ECX, 8,11,0x8771F681); + HH(ECX,EDX,EAX,EBX,11,16,0x6D9D6122); + HH(EBX,ECX,EDX,EAX,14,23,0xFDE5380C); + HH(EAX,EBX,ECX,EDX, 1, 4,0xA4BEEA44); + HH(EDX,EAX,EBX,ECX, 4,11,0x4BDECFA9); + HH(ECX,EDX,EAX,EBX, 7,16,0xF6BB4B60); + HH(EBX,ECX,EDX,EAX,10,23,0xBEBFBC70); + HH(EAX,EBX,ECX,EDX,13, 4,0x289B7EC6); + HH(EDX,EAX,EBX,ECX, 0,11,0xEAA127FA); + HH(ECX,EDX,EAX,EBX, 3,16,0xD4EF3085); + HH(EBX,ECX,EDX,EAX, 6,23,0x04881D05); + HH(EAX,EBX,ECX,EDX, 9, 4,0xD9D4D039); + HH(EDX,EAX,EBX,ECX,12,11,0xE6DB99E5); + HH(ECX,EDX,EAX,EBX,15,16,0x1FA27CF8); + HH(EBX,ECX,EDX,EAX, 2,23,0xC4AC5665); + + II(EAX,EBX,ECX,EDX, 0, 6,0xF4292244); + II(EDX,EAX,EBX,ECX, 7,10,0x432AFF97); + II(ECX,EDX,EAX,EBX,14,15,0xAB9423A7); + II(EBX,ECX,EDX,EAX, 5,21,0xFC93A039); + II(EAX,EBX,ECX,EDX,12, 6,0x655B59C3); + II(EDX,EAX,EBX,ECX, 3,10,0x8F0CCC92); + II(ECX,EDX,EAX,EBX,10,15,0xFFEFF47D); + II(EBX,ECX,EDX,EAX, 1,21,0x85845DD1); + II(EAX,EBX,ECX,EDX, 8, 6,0x6FA87E4F); + II(EDX,EAX,EBX,ECX,15,10,0xFE2CE6E0); + II(ECX,EDX,EAX,EBX, 6,15,0xA3014314); + II(EBX,ECX,EDX,EAX,13,21,0x4E0811A1); + II(EAX,EBX,ECX,EDX, 4, 6,0xF7537E82); + II(EDX,EAX,EBX,ECX,11,10,0xBD3AF235); + II(ECX,EDX,EAX,EBX, 2,15,0x2AD7D2BB); + II(EBX,ECX,EDX,EAX, 9,21,0xEB86D391); + + ASSIGN(EBP, ARG(1)) + ADD(ARRAY4(EBP, 0), EAX) + ADD(ARRAY4(EBP, 1), EBX) + ADD(ARRAY4(EBP, 2), ECX) + ADD(ARRAY4(EBP, 3), EDX) + + RESTORE_REGS() +END_FUNCTION(botan_md5_ia32_compress) diff --git a/botan/src/hash/mdx_hash/info.txt b/botan/src/hash/mdx_hash/info.txt new file mode 100644 index 0000000..412c933 --- /dev/null +++ b/botan/src/hash/mdx_hash/info.txt @@ -0,0 +1,10 @@ +realname "MDx Hash Base" + +define MDX_HASH_FUNCTION + +load_on dep + +<add> +mdx_hash.cpp +mdx_hash.h +</add> diff --git a/botan/src/hash/mdx_hash/mdx_hash.cpp b/botan/src/hash/mdx_hash/mdx_hash.cpp new file mode 100644 index 0000000..b630ec2 --- /dev/null +++ b/botan/src/hash/mdx_hash/mdx_hash.cpp @@ -0,0 +1,105 @@ +/** +* Merkle-Damgard Hash Function +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/mdx_hash.h> +#include <botan/exceptn.h> +#include <botan/loadstor.h> + +namespace Botan { + +/** +* MDx_HashFunction Constructor +*/ +MDx_HashFunction::MDx_HashFunction(u32bit hash_len, u32bit block_len, + bool byte_end, bool bit_end, + u32bit cnt_size) : + HashFunction(hash_len, block_len), buffer(block_len), + BIG_BYTE_ENDIAN(byte_end), BIG_BIT_ENDIAN(bit_end), COUNT_SIZE(cnt_size) + { + if(COUNT_SIZE >= OUTPUT_LENGTH || COUNT_SIZE >= HASH_BLOCK_SIZE) + throw Invalid_Argument("MDx_HashFunction: COUNT_SIZE is too big"); + count = position = 0; + } + +/** +* Clear memory of sensitive data +*/ +void MDx_HashFunction::clear() throw() + { + buffer.clear(); + count = position = 0; + } + +/** +* Update the hash +*/ +void MDx_HashFunction::add_data(const byte input[], u32bit length) + { + count += length; + + if(position) + { + buffer.copy(position, input, length); + + if(position + length >= HASH_BLOCK_SIZE) + { + compress_n(buffer.begin(), 1); + input += (HASH_BLOCK_SIZE - position); + length -= (HASH_BLOCK_SIZE - position); + position = 0; + } + } + + const u32bit full_blocks = length / HASH_BLOCK_SIZE; + const u32bit remaining = length % HASH_BLOCK_SIZE; + + if(full_blocks) + compress_n(input, full_blocks); + + buffer.copy(position, input + full_blocks * HASH_BLOCK_SIZE, remaining); + position += remaining; + } + +/** +* Finalize a hash +*/ +void MDx_HashFunction::final_result(byte output[]) + { + buffer[position] = (BIG_BIT_ENDIAN ? 0x80 : 0x01); + for(u32bit j = position+1; j != HASH_BLOCK_SIZE; ++j) + buffer[j] = 0; + + if(position >= HASH_BLOCK_SIZE - COUNT_SIZE) + { + compress_n(buffer, 1); + buffer.clear(); + } + + write_count(buffer + HASH_BLOCK_SIZE - COUNT_SIZE); + + compress_n(buffer, 1); + copy_out(output); + clear(); + } + +/** +* Write the count bits to the buffer +*/ +void MDx_HashFunction::write_count(byte out[]) + { + if(COUNT_SIZE < 8) + throw Invalid_State("MDx_HashFunction::write_count: COUNT_SIZE < 8"); + + const u64bit bit_count = count * 8; + + if(BIG_BYTE_ENDIAN) + store_be(bit_count, out + COUNT_SIZE - 8); + else + store_le(bit_count, out + COUNT_SIZE - 8); + } + +} diff --git a/botan/src/hash/mdx_hash/mdx_hash.h b/botan/src/hash/mdx_hash/mdx_hash.h new file mode 100644 index 0000000..0c3aa78 --- /dev/null +++ b/botan/src/hash/mdx_hash/mdx_hash.h @@ -0,0 +1,42 @@ +/** +* MDx Hash Function +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_MDX_BASE_H__ +#define BOTAN_MDX_BASE_H__ + +#include <botan/hash.h> + +namespace Botan { + +/** +* MDx Hash Function Base Class +*/ +class BOTAN_DLL MDx_HashFunction : public HashFunction + { + public: + MDx_HashFunction(u32bit, u32bit, bool, bool, u32bit = 8); + virtual ~MDx_HashFunction() {} + protected: + void add_data(const byte[], u32bit); + void final_result(byte output[]); + virtual void compress_n(const byte block[], u32bit block_n) = 0; + + void clear() throw(); + virtual void copy_out(byte[]) = 0; + virtual void write_count(byte[]); + private: + SecureVector<byte> buffer; + u64bit count; + u32bit position; + + const bool BIG_BYTE_ENDIAN, BIG_BIT_ENDIAN; + const u32bit COUNT_SIZE; + }; + +} + +#endif diff --git a/botan/src/hash/par_hash/info.txt b/botan/src/hash/par_hash/info.txt new file mode 100644 index 0000000..45716aa --- /dev/null +++ b/botan/src/hash/par_hash/info.txt @@ -0,0 +1,10 @@ +realname "Parallel Hash" + +define PARALLEL_HASH + +load_on auto + +<add> +par_hash.cpp +par_hash.h +</add> diff --git a/botan/src/hash/par_hash/par_hash.cpp b/botan/src/hash/par_hash/par_hash.cpp new file mode 100644 index 0000000..4b0c7c4 --- /dev/null +++ b/botan/src/hash/par_hash/par_hash.cpp @@ -0,0 +1,103 @@ +/* +* Parallel +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/par_hash.h> + +namespace Botan { + +namespace { + +/* +* Return the sum of the hash sizes +*/ +u32bit sum_of_hash_lengths(const std::vector<HashFunction*>& hashes) + { + u32bit sum = 0; + + for(u32bit j = 0; j != hashes.size(); ++j) + sum += hashes[j]->OUTPUT_LENGTH; + + return sum; + } + +} + +/* +* Update the hash +*/ +void Parallel::add_data(const byte input[], u32bit length) + { + for(u32bit j = 0; j != hashes.size(); ++j) + hashes[j]->update(input, length); + } + +/* +* Finalize the hash +*/ +void Parallel::final_result(byte hash[]) + { + u32bit offset = 0; + for(u32bit j = 0; j != hashes.size(); ++j) + { + hashes[j]->final(hash + offset); + offset += hashes[j]->OUTPUT_LENGTH; + } + } + +/* +* Return the name of this type +*/ +std::string Parallel::name() const + { + std::string hash_names; + for(u32bit j = 0; j != hashes.size(); ++j) + { + if(j) + hash_names += ','; + hash_names += hashes[j]->name(); + } + return "Parallel(" + hash_names + ")"; + } + +/* +* Return a clone of this object +*/ +HashFunction* Parallel::clone() const + { + std::vector<HashFunction*> hash_copies; + for(u32bit j = 0; j != hashes.size(); ++j) + hash_copies.push_back(hashes[j]->clone()); + return new Parallel(hash_copies); + } + +/* +* Clear memory of sensitive data +*/ +void Parallel::clear() throw() + { + for(u32bit j = 0; j != hashes.size(); ++j) + hashes[j]->clear(); + } + +/* +* Parallel Constructor +*/ +Parallel::Parallel(const std::vector<HashFunction*>& hash_in) : + HashFunction(sum_of_hash_lengths(hash_in)), hashes(hash_in) + { + } + +/* +* Parallel Destructor +*/ +Parallel::~Parallel() + { + for(u32bit j = 0; j != hashes.size(); ++j) + delete hashes[j]; + } + +} diff --git a/botan/src/hash/par_hash/par_hash.h b/botan/src/hash/par_hash/par_hash.h new file mode 100644 index 0000000..7e75c27 --- /dev/null +++ b/botan/src/hash/par_hash/par_hash.h @@ -0,0 +1,36 @@ +/* +* Parallel Hash +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_PARALLEL_HASH_H__ +#define BOTAN_PARALLEL_HASH_H__ + +#include <botan/hash.h> +#include <vector> + +namespace Botan { + +/* +* Parallel +*/ +class BOTAN_DLL Parallel : public HashFunction + { + public: + void clear() throw(); + std::string name() const; + HashFunction* clone() const; + + Parallel(const std::vector<HashFunction*>&); + ~Parallel(); + private: + void add_data(const byte[], u32bit); + void final_result(byte[]); + std::vector<HashFunction*> hashes; + }; + +} + +#endif diff --git a/botan/src/hash/rmd128/info.txt b/botan/src/hash/rmd128/info.txt new file mode 100644 index 0000000..402271d --- /dev/null +++ b/botan/src/hash/rmd128/info.txt @@ -0,0 +1,14 @@ +realname "RIPEMD-128" + +define RIPEMD_128 + +load_on auto + +<add> +rmd128.cpp +rmd128.h +</add> + +<requires> +mdx_hash +</requires> diff --git a/botan/src/hash/rmd128/rmd128.cpp b/botan/src/hash/rmd128/rmd128.cpp new file mode 100644 index 0000000..8b2c0cc --- /dev/null +++ b/botan/src/hash/rmd128/rmd128.cpp @@ -0,0 +1,172 @@ +/* +* RIPEMD-128 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/rmd128.h> +#include <botan/loadstor.h> +#include <botan/rotate.h> + +namespace Botan { + +namespace { + +/* +* RIPEMD-128 F1 Function +*/ +inline void F1(u32bit& A, u32bit B, u32bit C, u32bit D, + u32bit msg, u32bit shift) + { + A += (B ^ C ^ D) + msg; + A = rotate_left(A, shift); + } + +/* +* RIPEMD-128 F2 Function +*/ +inline void F2(u32bit& A, u32bit B, u32bit C, u32bit D, + u32bit msg, u32bit shift, u32bit magic) + { + A += (D ^ (B & (C ^ D))) + msg + magic; + A = rotate_left(A, shift); + } + +/* +* RIPEMD-128 F3 Function +*/ +inline void F3(u32bit& A, u32bit B, u32bit C, u32bit D, + u32bit msg, u32bit shift, u32bit magic) + { + A += (D ^ (B | ~C)) + msg + magic; + A = rotate_left(A, shift); + } + +/* +* RIPEMD-128 F4 Function +*/ +inline void F4(u32bit& A, u32bit B, u32bit C, u32bit D, + u32bit msg, u32bit shift, u32bit magic) + { + A += (C ^ (D & (B ^ C))) + msg + magic; + A = rotate_left(A, shift); + } + +} + +/* +* RIPEMD-128 Compression Function +*/ +void RIPEMD_128::compress_n(const byte input[], u32bit blocks) + { + for(u32bit i = 0; i != blocks; ++i) + { + for(u32bit j = 0; j != 16; ++j) + M[j] = load_le<u32bit>(input, j); + input += HASH_BLOCK_SIZE; + + u32bit A1 = digest[0], A2 = A1, B1 = digest[1], B2 = B1, + C1 = digest[2], C2 = C1, D1 = digest[3], D2 = D1; + + const u32bit MAGIC2 = 0x5A827999, MAGIC3 = 0x6ED9EBA1, + MAGIC4 = 0x8F1BBCDC, MAGIC5 = 0x50A28BE6, + MAGIC6 = 0x5C4DD124, MAGIC7 = 0x6D703EF3; + + F1(A1,B1,C1,D1,M[ 0],11 ); F4(A2,B2,C2,D2,M[ 5], 8,MAGIC5); + F1(D1,A1,B1,C1,M[ 1],14 ); F4(D2,A2,B2,C2,M[14], 9,MAGIC5); + F1(C1,D1,A1,B1,M[ 2],15 ); F4(C2,D2,A2,B2,M[ 7], 9,MAGIC5); + F1(B1,C1,D1,A1,M[ 3],12 ); F4(B2,C2,D2,A2,M[ 0],11,MAGIC5); + F1(A1,B1,C1,D1,M[ 4], 5 ); F4(A2,B2,C2,D2,M[ 9],13,MAGIC5); + F1(D1,A1,B1,C1,M[ 5], 8 ); F4(D2,A2,B2,C2,M[ 2],15,MAGIC5); + F1(C1,D1,A1,B1,M[ 6], 7 ); F4(C2,D2,A2,B2,M[11],15,MAGIC5); + F1(B1,C1,D1,A1,M[ 7], 9 ); F4(B2,C2,D2,A2,M[ 4], 5,MAGIC5); + F1(A1,B1,C1,D1,M[ 8],11 ); F4(A2,B2,C2,D2,M[13], 7,MAGIC5); + F1(D1,A1,B1,C1,M[ 9],13 ); F4(D2,A2,B2,C2,M[ 6], 7,MAGIC5); + F1(C1,D1,A1,B1,M[10],14 ); F4(C2,D2,A2,B2,M[15], 8,MAGIC5); + F1(B1,C1,D1,A1,M[11],15 ); F4(B2,C2,D2,A2,M[ 8],11,MAGIC5); + F1(A1,B1,C1,D1,M[12], 6 ); F4(A2,B2,C2,D2,M[ 1],14,MAGIC5); + F1(D1,A1,B1,C1,M[13], 7 ); F4(D2,A2,B2,C2,M[10],14,MAGIC5); + F1(C1,D1,A1,B1,M[14], 9 ); F4(C2,D2,A2,B2,M[ 3],12,MAGIC5); + F1(B1,C1,D1,A1,M[15], 8 ); F4(B2,C2,D2,A2,M[12], 6,MAGIC5); + + F2(A1,B1,C1,D1,M[ 7], 7,MAGIC2); F3(A2,B2,C2,D2,M[ 6], 9,MAGIC6); + F2(D1,A1,B1,C1,M[ 4], 6,MAGIC2); F3(D2,A2,B2,C2,M[11],13,MAGIC6); + F2(C1,D1,A1,B1,M[13], 8,MAGIC2); F3(C2,D2,A2,B2,M[ 3],15,MAGIC6); + F2(B1,C1,D1,A1,M[ 1],13,MAGIC2); F3(B2,C2,D2,A2,M[ 7], 7,MAGIC6); + F2(A1,B1,C1,D1,M[10],11,MAGIC2); F3(A2,B2,C2,D2,M[ 0],12,MAGIC6); + F2(D1,A1,B1,C1,M[ 6], 9,MAGIC2); F3(D2,A2,B2,C2,M[13], 8,MAGIC6); + F2(C1,D1,A1,B1,M[15], 7,MAGIC2); F3(C2,D2,A2,B2,M[ 5], 9,MAGIC6); + F2(B1,C1,D1,A1,M[ 3],15,MAGIC2); F3(B2,C2,D2,A2,M[10],11,MAGIC6); + F2(A1,B1,C1,D1,M[12], 7,MAGIC2); F3(A2,B2,C2,D2,M[14], 7,MAGIC6); + F2(D1,A1,B1,C1,M[ 0],12,MAGIC2); F3(D2,A2,B2,C2,M[15], 7,MAGIC6); + F2(C1,D1,A1,B1,M[ 9],15,MAGIC2); F3(C2,D2,A2,B2,M[ 8],12,MAGIC6); + F2(B1,C1,D1,A1,M[ 5], 9,MAGIC2); F3(B2,C2,D2,A2,M[12], 7,MAGIC6); + F2(A1,B1,C1,D1,M[ 2],11,MAGIC2); F3(A2,B2,C2,D2,M[ 4], 6,MAGIC6); + F2(D1,A1,B1,C1,M[14], 7,MAGIC2); F3(D2,A2,B2,C2,M[ 9],15,MAGIC6); + F2(C1,D1,A1,B1,M[11],13,MAGIC2); F3(C2,D2,A2,B2,M[ 1],13,MAGIC6); + F2(B1,C1,D1,A1,M[ 8],12,MAGIC2); F3(B2,C2,D2,A2,M[ 2],11,MAGIC6); + + F3(A1,B1,C1,D1,M[ 3],11,MAGIC3); F2(A2,B2,C2,D2,M[15], 9,MAGIC7); + F3(D1,A1,B1,C1,M[10],13,MAGIC3); F2(D2,A2,B2,C2,M[ 5], 7,MAGIC7); + F3(C1,D1,A1,B1,M[14], 6,MAGIC3); F2(C2,D2,A2,B2,M[ 1],15,MAGIC7); + F3(B1,C1,D1,A1,M[ 4], 7,MAGIC3); F2(B2,C2,D2,A2,M[ 3],11,MAGIC7); + F3(A1,B1,C1,D1,M[ 9],14,MAGIC3); F2(A2,B2,C2,D2,M[ 7], 8,MAGIC7); + F3(D1,A1,B1,C1,M[15], 9,MAGIC3); F2(D2,A2,B2,C2,M[14], 6,MAGIC7); + F3(C1,D1,A1,B1,M[ 8],13,MAGIC3); F2(C2,D2,A2,B2,M[ 6], 6,MAGIC7); + F3(B1,C1,D1,A1,M[ 1],15,MAGIC3); F2(B2,C2,D2,A2,M[ 9],14,MAGIC7); + F3(A1,B1,C1,D1,M[ 2],14,MAGIC3); F2(A2,B2,C2,D2,M[11],12,MAGIC7); + F3(D1,A1,B1,C1,M[ 7], 8,MAGIC3); F2(D2,A2,B2,C2,M[ 8],13,MAGIC7); + F3(C1,D1,A1,B1,M[ 0],13,MAGIC3); F2(C2,D2,A2,B2,M[12], 5,MAGIC7); + F3(B1,C1,D1,A1,M[ 6], 6,MAGIC3); F2(B2,C2,D2,A2,M[ 2],14,MAGIC7); + F3(A1,B1,C1,D1,M[13], 5,MAGIC3); F2(A2,B2,C2,D2,M[10],13,MAGIC7); + F3(D1,A1,B1,C1,M[11],12,MAGIC3); F2(D2,A2,B2,C2,M[ 0],13,MAGIC7); + F3(C1,D1,A1,B1,M[ 5], 7,MAGIC3); F2(C2,D2,A2,B2,M[ 4], 7,MAGIC7); + F3(B1,C1,D1,A1,M[12], 5,MAGIC3); F2(B2,C2,D2,A2,M[13], 5,MAGIC7); + + F4(A1,B1,C1,D1,M[ 1],11,MAGIC4); F1(A2,B2,C2,D2,M[ 8],15 ); + F4(D1,A1,B1,C1,M[ 9],12,MAGIC4); F1(D2,A2,B2,C2,M[ 6], 5 ); + F4(C1,D1,A1,B1,M[11],14,MAGIC4); F1(C2,D2,A2,B2,M[ 4], 8 ); + F4(B1,C1,D1,A1,M[10],15,MAGIC4); F1(B2,C2,D2,A2,M[ 1],11 ); + F4(A1,B1,C1,D1,M[ 0],14,MAGIC4); F1(A2,B2,C2,D2,M[ 3],14 ); + F4(D1,A1,B1,C1,M[ 8],15,MAGIC4); F1(D2,A2,B2,C2,M[11],14 ); + F4(C1,D1,A1,B1,M[12], 9,MAGIC4); F1(C2,D2,A2,B2,M[15], 6 ); + F4(B1,C1,D1,A1,M[ 4], 8,MAGIC4); F1(B2,C2,D2,A2,M[ 0],14 ); + F4(A1,B1,C1,D1,M[13], 9,MAGIC4); F1(A2,B2,C2,D2,M[ 5], 6 ); + F4(D1,A1,B1,C1,M[ 3],14,MAGIC4); F1(D2,A2,B2,C2,M[12], 9 ); + F4(C1,D1,A1,B1,M[ 7], 5,MAGIC4); F1(C2,D2,A2,B2,M[ 2],12 ); + F4(B1,C1,D1,A1,M[15], 6,MAGIC4); F1(B2,C2,D2,A2,M[13], 9 ); + F4(A1,B1,C1,D1,M[14], 8,MAGIC4); F1(A2,B2,C2,D2,M[ 9],12 ); + F4(D1,A1,B1,C1,M[ 5], 6,MAGIC4); F1(D2,A2,B2,C2,M[ 7], 5 ); + F4(C1,D1,A1,B1,M[ 6], 5,MAGIC4); F1(C2,D2,A2,B2,M[10],15 ); + F4(B1,C1,D1,A1,M[ 2],12,MAGIC4); F1(B2,C2,D2,A2,M[14], 8 ); + + D2 = digest[1] + C1 + D2; digest[1] = digest[2] + D1 + A2; + digest[2] = digest[3] + A1 + B2; digest[3] = digest[0] + B1 + C2; + digest[0] = D2; + } + } + +/* +* Copy out the digest +*/ +void RIPEMD_128::copy_out(byte output[]) + { + for(u32bit j = 0; j != OUTPUT_LENGTH; j += 4) + store_le(digest[j/4], output + j); + } + +/* +* Clear memory of sensitive data +*/ +void RIPEMD_128::clear() throw() + { + MDx_HashFunction::clear(); + M.clear(); + digest[0] = 0x67452301; + digest[1] = 0xEFCDAB89; + digest[2] = 0x98BADCFE; + digest[3] = 0x10325476; + } + +} diff --git a/botan/src/hash/rmd128/rmd128.h b/botan/src/hash/rmd128/rmd128.h new file mode 100644 index 0000000..031ae57 --- /dev/null +++ b/botan/src/hash/rmd128/rmd128.h @@ -0,0 +1,35 @@ +/* +* RIPEMD-128 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_RIPEMD_128_H__ +#define BOTAN_RIPEMD_128_H__ + +#include <botan/mdx_hash.h> + +namespace Botan { + +/* +* RIPEMD-128 +*/ +class BOTAN_DLL RIPEMD_128 : public MDx_HashFunction + { + public: + void clear() throw(); + std::string name() const { return "RIPEMD-128"; } + HashFunction* clone() const { return new RIPEMD_128; } + RIPEMD_128() : MDx_HashFunction(16, 64, false, true) { clear(); } + private: + void compress_n(const byte[], u32bit blocks); + void copy_out(byte[]); + + SecureBuffer<u32bit, 16> M; + SecureBuffer<u32bit, 4> digest; + }; + +} + +#endif diff --git a/botan/src/hash/rmd160/info.txt b/botan/src/hash/rmd160/info.txt new file mode 100644 index 0000000..af4b5c2 --- /dev/null +++ b/botan/src/hash/rmd160/info.txt @@ -0,0 +1,14 @@ +realname "RIPEMD-160" + +define RIPEMD_160 + +load_on auto + +<add> +rmd160.cpp +rmd160.h +</add> + +<requires> +mdx_hash +</requires> diff --git a/botan/src/hash/rmd160/rmd160.cpp b/botan/src/hash/rmd160/rmd160.cpp new file mode 100644 index 0000000..863de84 --- /dev/null +++ b/botan/src/hash/rmd160/rmd160.cpp @@ -0,0 +1,210 @@ +/* +* RIPEMD-160 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/rmd160.h> +#include <botan/loadstor.h> +#include <botan/rotate.h> + +namespace Botan { + +namespace { + +/* +* RIPEMD-160 F1 Function +*/ +inline void F1(u32bit& A, u32bit B, u32bit& C, u32bit D, u32bit E, + u32bit msg, u32bit shift) + { + A += (B ^ C ^ D) + msg; + A = rotate_left(A, shift) + E; + C = rotate_left(C, 10); + } + +/* +* RIPEMD-160 F2 Function +*/ +inline void F2(u32bit& A, u32bit B, u32bit& C, u32bit D, u32bit E, + u32bit msg, u32bit shift, u32bit magic) + { + A += (D ^ (B & (C ^ D))) + msg + magic; + A = rotate_left(A, shift) + E; + C = rotate_left(C, 10); + } + +/* +* RIPEMD-160 F3 Function +*/ +inline void F3(u32bit& A, u32bit B, u32bit& C, u32bit D, u32bit E, + u32bit msg, u32bit shift, u32bit magic) + { + A += (D ^ (B | ~C)) + msg + magic; + A = rotate_left(A, shift) + E; + C = rotate_left(C, 10); + } + +/* +* RIPEMD-160 F4 Function +*/ +inline void F4(u32bit& A, u32bit B, u32bit& C, u32bit D, u32bit E, + u32bit msg, u32bit shift, u32bit magic) + { + A += (C ^ (D & (B ^ C))) + msg + magic; + A = rotate_left(A, shift) + E; + C = rotate_left(C, 10); + } + +/* +* RIPEMD-160 F5 Function +*/ +inline void F5(u32bit& A, u32bit B, u32bit& C, u32bit D, u32bit E, + u32bit msg, u32bit shift, u32bit magic) + { + A += (B ^ (C | ~D)) + msg + magic; + A = rotate_left(A, shift) + E; + C = rotate_left(C, 10); + } + +} + +/* +* RIPEMD-160 Compression Function +*/ +void RIPEMD_160::compress_n(const byte input[], u32bit blocks) + { + const u32bit MAGIC2 = 0x5A827999, MAGIC3 = 0x6ED9EBA1, + MAGIC4 = 0x8F1BBCDC, MAGIC5 = 0xA953FD4E, + MAGIC6 = 0x50A28BE6, MAGIC7 = 0x5C4DD124, + MAGIC8 = 0x6D703EF3, MAGIC9 = 0x7A6D76E9; + + for(u32bit i = 0; i != blocks; ++i) + { + for(u32bit j = 0; j != 16; ++j) + M[j] = load_le<u32bit>(input, j); + input += HASH_BLOCK_SIZE; + + u32bit A1 = digest[0], A2 = A1, B1 = digest[1], B2 = B1, + C1 = digest[2], C2 = C1, D1 = digest[3], D2 = D1, + E1 = digest[4], E2 = E1; + + F1(A1,B1,C1,D1,E1,M[ 0],11 ); F5(A2,B2,C2,D2,E2,M[ 5], 8,MAGIC6); + F1(E1,A1,B1,C1,D1,M[ 1],14 ); F5(E2,A2,B2,C2,D2,M[14], 9,MAGIC6); + F1(D1,E1,A1,B1,C1,M[ 2],15 ); F5(D2,E2,A2,B2,C2,M[ 7], 9,MAGIC6); + F1(C1,D1,E1,A1,B1,M[ 3],12 ); F5(C2,D2,E2,A2,B2,M[ 0],11,MAGIC6); + F1(B1,C1,D1,E1,A1,M[ 4], 5 ); F5(B2,C2,D2,E2,A2,M[ 9],13,MAGIC6); + F1(A1,B1,C1,D1,E1,M[ 5], 8 ); F5(A2,B2,C2,D2,E2,M[ 2],15,MAGIC6); + F1(E1,A1,B1,C1,D1,M[ 6], 7 ); F5(E2,A2,B2,C2,D2,M[11],15,MAGIC6); + F1(D1,E1,A1,B1,C1,M[ 7], 9 ); F5(D2,E2,A2,B2,C2,M[ 4], 5,MAGIC6); + F1(C1,D1,E1,A1,B1,M[ 8],11 ); F5(C2,D2,E2,A2,B2,M[13], 7,MAGIC6); + F1(B1,C1,D1,E1,A1,M[ 9],13 ); F5(B2,C2,D2,E2,A2,M[ 6], 7,MAGIC6); + F1(A1,B1,C1,D1,E1,M[10],14 ); F5(A2,B2,C2,D2,E2,M[15], 8,MAGIC6); + F1(E1,A1,B1,C1,D1,M[11],15 ); F5(E2,A2,B2,C2,D2,M[ 8],11,MAGIC6); + F1(D1,E1,A1,B1,C1,M[12], 6 ); F5(D2,E2,A2,B2,C2,M[ 1],14,MAGIC6); + F1(C1,D1,E1,A1,B1,M[13], 7 ); F5(C2,D2,E2,A2,B2,M[10],14,MAGIC6); + F1(B1,C1,D1,E1,A1,M[14], 9 ); F5(B2,C2,D2,E2,A2,M[ 3],12,MAGIC6); + F1(A1,B1,C1,D1,E1,M[15], 8 ); F5(A2,B2,C2,D2,E2,M[12], 6,MAGIC6); + + F2(E1,A1,B1,C1,D1,M[ 7], 7,MAGIC2); F4(E2,A2,B2,C2,D2,M[ 6], 9,MAGIC7); + F2(D1,E1,A1,B1,C1,M[ 4], 6,MAGIC2); F4(D2,E2,A2,B2,C2,M[11],13,MAGIC7); + F2(C1,D1,E1,A1,B1,M[13], 8,MAGIC2); F4(C2,D2,E2,A2,B2,M[ 3],15,MAGIC7); + F2(B1,C1,D1,E1,A1,M[ 1],13,MAGIC2); F4(B2,C2,D2,E2,A2,M[ 7], 7,MAGIC7); + F2(A1,B1,C1,D1,E1,M[10],11,MAGIC2); F4(A2,B2,C2,D2,E2,M[ 0],12,MAGIC7); + F2(E1,A1,B1,C1,D1,M[ 6], 9,MAGIC2); F4(E2,A2,B2,C2,D2,M[13], 8,MAGIC7); + F2(D1,E1,A1,B1,C1,M[15], 7,MAGIC2); F4(D2,E2,A2,B2,C2,M[ 5], 9,MAGIC7); + F2(C1,D1,E1,A1,B1,M[ 3],15,MAGIC2); F4(C2,D2,E2,A2,B2,M[10],11,MAGIC7); + F2(B1,C1,D1,E1,A1,M[12], 7,MAGIC2); F4(B2,C2,D2,E2,A2,M[14], 7,MAGIC7); + F2(A1,B1,C1,D1,E1,M[ 0],12,MAGIC2); F4(A2,B2,C2,D2,E2,M[15], 7,MAGIC7); + F2(E1,A1,B1,C1,D1,M[ 9],15,MAGIC2); F4(E2,A2,B2,C2,D2,M[ 8],12,MAGIC7); + F2(D1,E1,A1,B1,C1,M[ 5], 9,MAGIC2); F4(D2,E2,A2,B2,C2,M[12], 7,MAGIC7); + F2(C1,D1,E1,A1,B1,M[ 2],11,MAGIC2); F4(C2,D2,E2,A2,B2,M[ 4], 6,MAGIC7); + F2(B1,C1,D1,E1,A1,M[14], 7,MAGIC2); F4(B2,C2,D2,E2,A2,M[ 9],15,MAGIC7); + F2(A1,B1,C1,D1,E1,M[11],13,MAGIC2); F4(A2,B2,C2,D2,E2,M[ 1],13,MAGIC7); + F2(E1,A1,B1,C1,D1,M[ 8],12,MAGIC2); F4(E2,A2,B2,C2,D2,M[ 2],11,MAGIC7); + + F3(D1,E1,A1,B1,C1,M[ 3],11,MAGIC3); F3(D2,E2,A2,B2,C2,M[15], 9,MAGIC8); + F3(C1,D1,E1,A1,B1,M[10],13,MAGIC3); F3(C2,D2,E2,A2,B2,M[ 5], 7,MAGIC8); + F3(B1,C1,D1,E1,A1,M[14], 6,MAGIC3); F3(B2,C2,D2,E2,A2,M[ 1],15,MAGIC8); + F3(A1,B1,C1,D1,E1,M[ 4], 7,MAGIC3); F3(A2,B2,C2,D2,E2,M[ 3],11,MAGIC8); + F3(E1,A1,B1,C1,D1,M[ 9],14,MAGIC3); F3(E2,A2,B2,C2,D2,M[ 7], 8,MAGIC8); + F3(D1,E1,A1,B1,C1,M[15], 9,MAGIC3); F3(D2,E2,A2,B2,C2,M[14], 6,MAGIC8); + F3(C1,D1,E1,A1,B1,M[ 8],13,MAGIC3); F3(C2,D2,E2,A2,B2,M[ 6], 6,MAGIC8); + F3(B1,C1,D1,E1,A1,M[ 1],15,MAGIC3); F3(B2,C2,D2,E2,A2,M[ 9],14,MAGIC8); + F3(A1,B1,C1,D1,E1,M[ 2],14,MAGIC3); F3(A2,B2,C2,D2,E2,M[11],12,MAGIC8); + F3(E1,A1,B1,C1,D1,M[ 7], 8,MAGIC3); F3(E2,A2,B2,C2,D2,M[ 8],13,MAGIC8); + F3(D1,E1,A1,B1,C1,M[ 0],13,MAGIC3); F3(D2,E2,A2,B2,C2,M[12], 5,MAGIC8); + F3(C1,D1,E1,A1,B1,M[ 6], 6,MAGIC3); F3(C2,D2,E2,A2,B2,M[ 2],14,MAGIC8); + F3(B1,C1,D1,E1,A1,M[13], 5,MAGIC3); F3(B2,C2,D2,E2,A2,M[10],13,MAGIC8); + F3(A1,B1,C1,D1,E1,M[11],12,MAGIC3); F3(A2,B2,C2,D2,E2,M[ 0],13,MAGIC8); + F3(E1,A1,B1,C1,D1,M[ 5], 7,MAGIC3); F3(E2,A2,B2,C2,D2,M[ 4], 7,MAGIC8); + F3(D1,E1,A1,B1,C1,M[12], 5,MAGIC3); F3(D2,E2,A2,B2,C2,M[13], 5,MAGIC8); + + F4(C1,D1,E1,A1,B1,M[ 1],11,MAGIC4); F2(C2,D2,E2,A2,B2,M[ 8],15,MAGIC9); + F4(B1,C1,D1,E1,A1,M[ 9],12,MAGIC4); F2(B2,C2,D2,E2,A2,M[ 6], 5,MAGIC9); + F4(A1,B1,C1,D1,E1,M[11],14,MAGIC4); F2(A2,B2,C2,D2,E2,M[ 4], 8,MAGIC9); + F4(E1,A1,B1,C1,D1,M[10],15,MAGIC4); F2(E2,A2,B2,C2,D2,M[ 1],11,MAGIC9); + F4(D1,E1,A1,B1,C1,M[ 0],14,MAGIC4); F2(D2,E2,A2,B2,C2,M[ 3],14,MAGIC9); + F4(C1,D1,E1,A1,B1,M[ 8],15,MAGIC4); F2(C2,D2,E2,A2,B2,M[11],14,MAGIC9); + F4(B1,C1,D1,E1,A1,M[12], 9,MAGIC4); F2(B2,C2,D2,E2,A2,M[15], 6,MAGIC9); + F4(A1,B1,C1,D1,E1,M[ 4], 8,MAGIC4); F2(A2,B2,C2,D2,E2,M[ 0],14,MAGIC9); + F4(E1,A1,B1,C1,D1,M[13], 9,MAGIC4); F2(E2,A2,B2,C2,D2,M[ 5], 6,MAGIC9); + F4(D1,E1,A1,B1,C1,M[ 3],14,MAGIC4); F2(D2,E2,A2,B2,C2,M[12], 9,MAGIC9); + F4(C1,D1,E1,A1,B1,M[ 7], 5,MAGIC4); F2(C2,D2,E2,A2,B2,M[ 2],12,MAGIC9); + F4(B1,C1,D1,E1,A1,M[15], 6,MAGIC4); F2(B2,C2,D2,E2,A2,M[13], 9,MAGIC9); + F4(A1,B1,C1,D1,E1,M[14], 8,MAGIC4); F2(A2,B2,C2,D2,E2,M[ 9],12,MAGIC9); + F4(E1,A1,B1,C1,D1,M[ 5], 6,MAGIC4); F2(E2,A2,B2,C2,D2,M[ 7], 5,MAGIC9); + F4(D1,E1,A1,B1,C1,M[ 6], 5,MAGIC4); F2(D2,E2,A2,B2,C2,M[10],15,MAGIC9); + F4(C1,D1,E1,A1,B1,M[ 2],12,MAGIC4); F2(C2,D2,E2,A2,B2,M[14], 8,MAGIC9); + + F5(B1,C1,D1,E1,A1,M[ 4], 9,MAGIC5); F1(B2,C2,D2,E2,A2,M[12], 8 ); + F5(A1,B1,C1,D1,E1,M[ 0],15,MAGIC5); F1(A2,B2,C2,D2,E2,M[15], 5 ); + F5(E1,A1,B1,C1,D1,M[ 5], 5,MAGIC5); F1(E2,A2,B2,C2,D2,M[10],12 ); + F5(D1,E1,A1,B1,C1,M[ 9],11,MAGIC5); F1(D2,E2,A2,B2,C2,M[ 4], 9 ); + F5(C1,D1,E1,A1,B1,M[ 7], 6,MAGIC5); F1(C2,D2,E2,A2,B2,M[ 1],12 ); + F5(B1,C1,D1,E1,A1,M[12], 8,MAGIC5); F1(B2,C2,D2,E2,A2,M[ 5], 5 ); + F5(A1,B1,C1,D1,E1,M[ 2],13,MAGIC5); F1(A2,B2,C2,D2,E2,M[ 8],14 ); + F5(E1,A1,B1,C1,D1,M[10],12,MAGIC5); F1(E2,A2,B2,C2,D2,M[ 7], 6 ); + F5(D1,E1,A1,B1,C1,M[14], 5,MAGIC5); F1(D2,E2,A2,B2,C2,M[ 6], 8 ); + F5(C1,D1,E1,A1,B1,M[ 1],12,MAGIC5); F1(C2,D2,E2,A2,B2,M[ 2],13 ); + F5(B1,C1,D1,E1,A1,M[ 3],13,MAGIC5); F1(B2,C2,D2,E2,A2,M[13], 6 ); + F5(A1,B1,C1,D1,E1,M[ 8],14,MAGIC5); F1(A2,B2,C2,D2,E2,M[14], 5 ); + F5(E1,A1,B1,C1,D1,M[11],11,MAGIC5); F1(E2,A2,B2,C2,D2,M[ 0],15 ); + F5(D1,E1,A1,B1,C1,M[ 6], 8,MAGIC5); F1(D2,E2,A2,B2,C2,M[ 3],13 ); + F5(C1,D1,E1,A1,B1,M[15], 5,MAGIC5); F1(C2,D2,E2,A2,B2,M[ 9],11 ); + F5(B1,C1,D1,E1,A1,M[13], 6,MAGIC5); F1(B2,C2,D2,E2,A2,M[11],11 ); + + C1 = digest[1] + C1 + D2; + digest[1] = digest[2] + D1 + E2; + digest[2] = digest[3] + E1 + A2; + digest[3] = digest[4] + A1 + B2; + digest[4] = digest[0] + B1 + C2; + digest[0] = C1; + } + } + +/* +* Copy out the digest +*/ +void RIPEMD_160::copy_out(byte output[]) + { + for(u32bit j = 0; j != OUTPUT_LENGTH; j += 4) + store_le(digest[j/4], output + j); + } + +/* +* Clear memory of sensitive data +*/ +void RIPEMD_160::clear() throw() + { + MDx_HashFunction::clear(); + M.clear(); + digest[0] = 0x67452301; + digest[1] = 0xEFCDAB89; + digest[2] = 0x98BADCFE; + digest[3] = 0x10325476; + digest[4] = 0xC3D2E1F0; + } + +} diff --git a/botan/src/hash/rmd160/rmd160.h b/botan/src/hash/rmd160/rmd160.h new file mode 100644 index 0000000..f2babc5 --- /dev/null +++ b/botan/src/hash/rmd160/rmd160.h @@ -0,0 +1,35 @@ +/* +* RIPEMD-160 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_RIPEMD_160_H__ +#define BOTAN_RIPEMD_160_H__ + +#include <botan/mdx_hash.h> + +namespace Botan { + +/* +* RIPEMD-160 +*/ +class BOTAN_DLL RIPEMD_160 : public MDx_HashFunction + { + public: + void clear() throw(); + std::string name() const { return "RIPEMD-160"; } + HashFunction* clone() const { return new RIPEMD_160; } + RIPEMD_160() : MDx_HashFunction(20, 64, false, true) { clear(); } + private: + void compress_n(const byte[], u32bit blocks); + void copy_out(byte[]); + + SecureBuffer<u32bit, 16> M; + SecureBuffer<u32bit, 5> digest; + }; + +} + +#endif diff --git a/botan/src/hash/sha1/info.txt b/botan/src/hash/sha1/info.txt new file mode 100644 index 0000000..a0ae30b --- /dev/null +++ b/botan/src/hash/sha1/info.txt @@ -0,0 +1,14 @@ +realname "SHA-1" + +define SHA1 + +load_on auto + +<add> +sha160.cpp +sha160.h +</add> + +<requires> +mdx_hash +</requires> diff --git a/botan/src/hash/sha1/sha160.cpp b/botan/src/hash/sha1/sha160.cpp new file mode 100644 index 0000000..45323a1 --- /dev/null +++ b/botan/src/hash/sha1/sha160.cpp @@ -0,0 +1,162 @@ +/* +* SHA-160 +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/sha160.h> +#include <botan/loadstor.h> +#include <botan/rotate.h> + +namespace Botan { + +namespace { + +/* +* SHA-160 F1 Function +*/ +inline void F1(u32bit A, u32bit& B, u32bit C, u32bit D, u32bit& E, u32bit msg) + { + E += (D ^ (B & (C ^ D))) + msg + 0x5A827999 + rotate_left(A, 5); + B = rotate_left(B, 30); + } + +/* +* SHA-160 F2 Function +*/ +inline void F2(u32bit A, u32bit& B, u32bit C, u32bit D, u32bit& E, u32bit msg) + { + E += (B ^ C ^ D) + msg + 0x6ED9EBA1 + rotate_left(A, 5); + B = rotate_left(B, 30); + } + +/* +* SHA-160 F3 Function +*/ +inline void F3(u32bit A, u32bit& B, u32bit C, u32bit D, u32bit& E, u32bit msg) + { + E += ((B & C) | ((B | C) & D)) + msg + 0x8F1BBCDC + rotate_left(A, 5); + B = rotate_left(B, 30); + } + +/* +* SHA-160 F4 Function +*/ +inline void F4(u32bit A, u32bit& B, u32bit C, u32bit D, u32bit& E, u32bit msg) + { + E += (B ^ C ^ D) + msg + 0xCA62C1D6 + rotate_left(A, 5); + B = rotate_left(B, 30); + } + +} + +/* +* SHA-160 Compression Function +*/ +void SHA_160::compress_n(const byte input[], u32bit blocks) + { + u32bit A = digest[0], B = digest[1], C = digest[2], + D = digest[3], E = digest[4]; + + for(u32bit i = 0; i != blocks; ++i) + { + for(u32bit j = 0; j != 16; j += 4) + { + W[j ] = load_be<u32bit>(input, j); + W[j+1] = load_be<u32bit>(input, j+1); + W[j+2] = load_be<u32bit>(input, j+2); + W[j+3] = load_be<u32bit>(input, j+3); + } + input += HASH_BLOCK_SIZE; + + for(u32bit j = 16; j != 80; j += 4) + { + W[j ] = rotate_left((W[j-3] ^ W[j-8] ^ W[j-14] ^ W[j-16]), 1); + W[j+1] = rotate_left((W[j-2] ^ W[j-7] ^ W[j-13] ^ W[j-15]), 1); + W[j+2] = rotate_left((W[j-1] ^ W[j-6] ^ W[j-12] ^ W[j-14]), 1); + W[j+3] = rotate_left((W[j ] ^ W[j-5] ^ W[j-11] ^ W[j-13]), 1); + } + + F1(A,B,C,D,E,W[ 0]); F1(E,A,B,C,D,W[ 1]); F1(D,E,A,B,C,W[ 2]); + F1(C,D,E,A,B,W[ 3]); F1(B,C,D,E,A,W[ 4]); F1(A,B,C,D,E,W[ 5]); + F1(E,A,B,C,D,W[ 6]); F1(D,E,A,B,C,W[ 7]); F1(C,D,E,A,B,W[ 8]); + F1(B,C,D,E,A,W[ 9]); F1(A,B,C,D,E,W[10]); F1(E,A,B,C,D,W[11]); + F1(D,E,A,B,C,W[12]); F1(C,D,E,A,B,W[13]); F1(B,C,D,E,A,W[14]); + F1(A,B,C,D,E,W[15]); F1(E,A,B,C,D,W[16]); F1(D,E,A,B,C,W[17]); + F1(C,D,E,A,B,W[18]); F1(B,C,D,E,A,W[19]); + + F2(A,B,C,D,E,W[20]); F2(E,A,B,C,D,W[21]); F2(D,E,A,B,C,W[22]); + F2(C,D,E,A,B,W[23]); F2(B,C,D,E,A,W[24]); F2(A,B,C,D,E,W[25]); + F2(E,A,B,C,D,W[26]); F2(D,E,A,B,C,W[27]); F2(C,D,E,A,B,W[28]); + F2(B,C,D,E,A,W[29]); F2(A,B,C,D,E,W[30]); F2(E,A,B,C,D,W[31]); + F2(D,E,A,B,C,W[32]); F2(C,D,E,A,B,W[33]); F2(B,C,D,E,A,W[34]); + F2(A,B,C,D,E,W[35]); F2(E,A,B,C,D,W[36]); F2(D,E,A,B,C,W[37]); + F2(C,D,E,A,B,W[38]); F2(B,C,D,E,A,W[39]); + + F3(A,B,C,D,E,W[40]); F3(E,A,B,C,D,W[41]); F3(D,E,A,B,C,W[42]); + F3(C,D,E,A,B,W[43]); F3(B,C,D,E,A,W[44]); F3(A,B,C,D,E,W[45]); + F3(E,A,B,C,D,W[46]); F3(D,E,A,B,C,W[47]); F3(C,D,E,A,B,W[48]); + F3(B,C,D,E,A,W[49]); F3(A,B,C,D,E,W[50]); F3(E,A,B,C,D,W[51]); + F3(D,E,A,B,C,W[52]); F3(C,D,E,A,B,W[53]); F3(B,C,D,E,A,W[54]); + F3(A,B,C,D,E,W[55]); F3(E,A,B,C,D,W[56]); F3(D,E,A,B,C,W[57]); + F3(C,D,E,A,B,W[58]); F3(B,C,D,E,A,W[59]); + + F4(A,B,C,D,E,W[60]); F4(E,A,B,C,D,W[61]); F4(D,E,A,B,C,W[62]); + F4(C,D,E,A,B,W[63]); F4(B,C,D,E,A,W[64]); F4(A,B,C,D,E,W[65]); + F4(E,A,B,C,D,W[66]); F4(D,E,A,B,C,W[67]); F4(C,D,E,A,B,W[68]); + F4(B,C,D,E,A,W[69]); F4(A,B,C,D,E,W[70]); F4(E,A,B,C,D,W[71]); + F4(D,E,A,B,C,W[72]); F4(C,D,E,A,B,W[73]); F4(B,C,D,E,A,W[74]); + F4(A,B,C,D,E,W[75]); F4(E,A,B,C,D,W[76]); F4(D,E,A,B,C,W[77]); + F4(C,D,E,A,B,W[78]); F4(B,C,D,E,A,W[79]); + + A = (digest[0] += A); + B = (digest[1] += B); + C = (digest[2] += C); + D = (digest[3] += D); + E = (digest[4] += E); + } + } + +/* +* Copy out the digest +*/ +void SHA_160::copy_out(byte output[]) + { + for(u32bit j = 0; j != OUTPUT_LENGTH; j += 4) + store_be(digest[j/4], output + j); + } + +/* +* Clear memory of sensitive data +*/ +void SHA_160::clear() throw() + { + MDx_HashFunction::clear(); + W.clear(); + digest[0] = 0x67452301; + digest[1] = 0xEFCDAB89; + digest[2] = 0x98BADCFE; + digest[3] = 0x10325476; + digest[4] = 0xC3D2E1F0; + } + +/* +* SHA_160 Constructor +*/ +SHA_160::SHA_160() : + MDx_HashFunction(20, 64, true, true), W(80) + { + clear(); + } + +/* +* SHA_160 Constructor +*/ +SHA_160::SHA_160(u32bit W_size) : + MDx_HashFunction(20, 64, true, true), W(W_size) + { + clear(); + } + +} diff --git a/botan/src/hash/sha1/sha160.h b/botan/src/hash/sha1/sha160.h new file mode 100644 index 0000000..232cf03 --- /dev/null +++ b/botan/src/hash/sha1/sha160.h @@ -0,0 +1,38 @@ +/* +* SHA-160 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_SHA_160_H__ +#define BOTAN_SHA_160_H__ + +#include <botan/mdx_hash.h> + +namespace Botan { + +/* +* SHA-160 +*/ +class BOTAN_DLL SHA_160 : public MDx_HashFunction + { + public: + void clear() throw(); + std::string name() const { return "SHA-160"; } + HashFunction* clone() const { return new SHA_160; } + SHA_160(); + + protected: + SHA_160(u32bit W_size); + + void compress_n(const byte[], u32bit blocks); + void copy_out(byte[]); + + SecureBuffer<u32bit, 5> digest; + SecureVector<u32bit> W; + }; + +} + +#endif diff --git a/botan/src/hash/sha1_amd64/info.txt b/botan/src/hash/sha1_amd64/info.txt new file mode 100644 index 0000000..ddbc7d3 --- /dev/null +++ b/botan/src/hash/sha1_amd64/info.txt @@ -0,0 +1,34 @@ +realname "SHA-1 (x86-64 assembler)" + +define SHA1_AMD64 + +load_on asm_ok + +<add> +sha1_amd64_imp.S +sha1_amd64.cpp +sha1_amd64.h +</add> + +<arch> +amd64 +</arch> + +<cc> +gcc +icc +</cc> + +# ELF systems +<os> +linux +netbsd +openbsd +solaris +</os> + +<requires> +amd64_eng +asm_amd64 +sha1 +</requires> diff --git a/botan/src/hash/sha1_amd64/sha1_amd64.cpp b/botan/src/hash/sha1_amd64/sha1_amd64.cpp new file mode 100644 index 0000000..0efbd85 --- /dev/null +++ b/botan/src/hash/sha1_amd64/sha1_amd64.cpp @@ -0,0 +1,31 @@ +/* +* SHA-160 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/sha1_amd64.h> + +namespace Botan { + +namespace { + +extern "C" +void botan_sha160_amd64_compress(u32bit[5], const byte[64], u32bit[80]); + +} + +/* +* SHA-160 Compression Function +*/ +void SHA_160_AMD64::compress_n(const byte input[], u32bit blocks) + { + for(u32bit i = 0; i != blocks; ++i) + { + botan_sha160_amd64_compress(digest, input, W); + input += HASH_BLOCK_SIZE; + } + } + +} diff --git a/botan/src/hash/sha1_amd64/sha1_amd64.h b/botan/src/hash/sha1_amd64/sha1_amd64.h new file mode 100644 index 0000000..f182627 --- /dev/null +++ b/botan/src/hash/sha1_amd64/sha1_amd64.h @@ -0,0 +1,28 @@ +/* +* SHA-160 (x86-64) +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_SHA_160_AMD64_H__ +#define BOTAN_SHA_160_AMD64_H__ + +#include <botan/sha160.h> + +namespace Botan { + +/* +* SHA-160 +*/ +class BOTAN_DLL SHA_160_AMD64 : public SHA_160 + { + public: + HashFunction* clone() const { return new SHA_160_AMD64; } + private: + void compress_n(const byte[], u32bit blocks); + }; + +} + +#endif diff --git a/botan/src/hash/sha1_amd64/sha1_amd64_imp.S b/botan/src/hash/sha1_amd64/sha1_amd64_imp.S new file mode 100644 index 0000000..34a8318 --- /dev/null +++ b/botan/src/hash/sha1_amd64/sha1_amd64_imp.S @@ -0,0 +1,260 @@ +/* +* SHA-160 Source File +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/asm_macr.h> + +START_LISTING(sha1_amd64.S) + +START_FUNCTION(botan_sha160_amd64_compress) + +#define DIGEST_ARR %rdi +#define INPUT %rsi +#define W %rdx +#define LOOP_CTR %eax + +#define A %r8d +#define B %r9d +#define C %r10d +#define D %r11d +#define E %ecx + + ZEROIZE(LOOP_CTR) + +ALIGN; +.LOOP_LOAD_INPUT: + addl $8, %eax + + movq ARRAY8(INPUT, 0), %r8 + movq ARRAY8(INPUT, 1), %r9 + movq ARRAY8(INPUT, 2), %r10 + movq ARRAY8(INPUT, 3), %r11 + + bswap %r8 + bswap %r9 + bswap %r10 + bswap %r11 + + rolq $32, %r8 + rolq $32, %r9 + rolq $32, %r10 + rolq $32, %r11 + + movq %r8, ARRAY8(W, 0) + movq %r9, ARRAY8(W, 1) + movq %r10, ARRAY8(W, 2) + movq %r11, ARRAY8(W, 3) + + addq $32, W + addq $32, INPUT + + cmp IMM(16), LOOP_CTR + jne .LOOP_LOAD_INPUT + +/* +#define A %r8d +#define B %r9d +#define C %r10d +#define D %r11d +#define E %ecx +*/ + +ALIGN; +.LOOP_EXPANSION: + addl $4, LOOP_CTR + + ZEROIZE(A) + ASSIGN(B, ARRAY4(W, -1)) + ASSIGN(C, ARRAY4(W, -2)) + ASSIGN(D, ARRAY4(W, -3)) + + XOR(A, ARRAY4(W, -5)) + XOR(B, ARRAY4(W, -6)) + XOR(C, ARRAY4(W, -7)) + XOR(D, ARRAY4(W, -8)) + + XOR(A, ARRAY4(W, -11)) + XOR(B, ARRAY4(W, -12)) + XOR(C, ARRAY4(W, -13)) + XOR(D, ARRAY4(W, -14)) + + XOR(A, ARRAY4(W, -13)) + XOR(B, ARRAY4(W, -14)) + XOR(C, ARRAY4(W, -15)) + XOR(D, ARRAY4(W, -16)) + + ROTL_IMM(D, 1) + ROTL_IMM(C, 1) + ROTL_IMM(B, 1) + XOR(A, D) + ROTL_IMM(A, 1) + + ASSIGN(ARRAY4(W, 0), D) + ASSIGN(ARRAY4(W, 1), C) + ASSIGN(ARRAY4(W, 2), B) + ASSIGN(ARRAY4(W, 3), A) + + addq $16, W + cmp IMM(80), LOOP_CTR + jne .LOOP_EXPANSION + + subq $320, W + +#define MAGIC1 0x5A827999 +#define MAGIC2 0x6ED9EBA1 +#define MAGIC3 0x8F1BBCDC +#define MAGIC4 0xCA62C1D6 + +#define T %esi +#define T2 %eax + +#define F1(A, B, C, D, E, F, N) \ + ASSIGN(T2, ARRAY4(W, N)) ; \ + ASSIGN(A, F) ; \ + ROTL_IMM(F, 5) ; \ + ADD(F, E) ; \ + ASSIGN(E, C) ; \ + XOR(E, D) ; \ + ADD3_IMM(F, T2, MAGIC1) ; \ + AND(E, B) ; \ + XOR(E, D) ; \ + ROTR_IMM(B, 2) ; \ + ADD(E, F) ; + +#define F2_4(A, B, C, D, E, F, N, MAGIC) \ + ASSIGN(T2, ARRAY4(W, N)) ; \ + ASSIGN(A, F) ; \ + ROTL_IMM(F, 5) ; \ + ADD(F, E) ; \ + ASSIGN(E, B) ; \ + XOR(E, C) ; \ + ADD3_IMM(F, T2, MAGIC) ; \ + XOR(E, D) ; \ + ROTR_IMM(B, 2) ; \ + ADD(E, F) ; + +#define F3(A, B, C, D, E, F, N) \ + ASSIGN(T2, ARRAY4(W, N)) ; \ + ASSIGN(A, F) ; \ + ROTL_IMM(F, 5) ; \ + ADD(F, E) ; \ + ASSIGN(E, B) ; \ + OR(E, C) ; \ + AND(E, D) ; \ + ADD3_IMM(F, T2, MAGIC3) ; \ + ASSIGN(T2, B) ; \ + AND(T2, C) ; \ + OR(E, T2) ; \ + ROTR_IMM(B, 2) ; \ + ADD(E, F) ; + +#define F2(A, B, C, D, E, F, W) \ + F2_4(A, B, C, D, E, F, W, MAGIC2) + +#define F4(A, B, C, D, E, F, W) \ + F2_4(A, B, C, D, E, F, W, MAGIC4) + + ASSIGN(T, ARRAY4(DIGEST_ARR, 0)) + ASSIGN(B, ARRAY4(DIGEST_ARR, 1)) + ASSIGN(C, ARRAY4(DIGEST_ARR, 2)) + ASSIGN(D, ARRAY4(DIGEST_ARR, 3)) + ASSIGN(E, ARRAY4(DIGEST_ARR, 4)) + + /* First Round */ + F1(A, B, C, D, E, T, 0) + F1(T, A, B, C, D, E, 1) + F1(E, T, A, B, C, D, 2) + F1(D, E, T, A, B, C, 3) + F1(C, D, E, T, A, B, 4) + F1(B, C, D, E, T, A, 5) + F1(A, B, C, D, E, T, 6) + F1(T, A, B, C, D, E, 7) + F1(E, T, A, B, C, D, 8) + F1(D, E, T, A, B, C, 9) + F1(C, D, E, T, A, B, 10) + F1(B, C, D, E, T, A, 11) + F1(A, B, C, D, E, T, 12) + F1(T, A, B, C, D, E, 13) + F1(E, T, A, B, C, D, 14) + F1(D, E, T, A, B, C, 15) + F1(C, D, E, T, A, B, 16) + F1(B, C, D, E, T, A, 17) + F1(A, B, C, D, E, T, 18) + F1(T, A, B, C, D, E, 19) + + /* Second Round */ + F2(E, T, A, B, C, D, 20) + F2(D, E, T, A, B, C, 21) + F2(C, D, E, T, A, B, 22) + F2(B, C, D, E, T, A, 23) + F2(A, B, C, D, E, T, 24) + F2(T, A, B, C, D, E, 25) + F2(E, T, A, B, C, D, 26) + F2(D, E, T, A, B, C, 27) + F2(C, D, E, T, A, B, 28) + F2(B, C, D, E, T, A, 29) + F2(A, B, C, D, E, T, 30) + F2(T, A, B, C, D, E, 31) + F2(E, T, A, B, C, D, 32) + F2(D, E, T, A, B, C, 33) + F2(C, D, E, T, A, B, 34) + F2(B, C, D, E, T, A, 35) + F2(A, B, C, D, E, T, 36) + F2(T, A, B, C, D, E, 37) + F2(E, T, A, B, C, D, 38) + F2(D, E, T, A, B, C, 39) + + /* Third Round */ + F3(C, D, E, T, A, B, 40) + F3(B, C, D, E, T, A, 41) + F3(A, B, C, D, E, T, 42) + F3(T, A, B, C, D, E, 43) + F3(E, T, A, B, C, D, 44) + F3(D, E, T, A, B, C, 45) + F3(C, D, E, T, A, B, 46) + F3(B, C, D, E, T, A, 47) + F3(A, B, C, D, E, T, 48) + F3(T, A, B, C, D, E, 49) + F3(E, T, A, B, C, D, 50) + F3(D, E, T, A, B, C, 51) + F3(C, D, E, T, A, B, 52) + F3(B, C, D, E, T, A, 53) + F3(A, B, C, D, E, T, 54) + F3(T, A, B, C, D, E, 55) + F3(E, T, A, B, C, D, 56) + F3(D, E, T, A, B, C, 57) + F3(C, D, E, T, A, B, 58) + F3(B, C, D, E, T, A, 59) + + /* Fourth Round */ + F4(A, B, C, D, E, T, 60) + F4(T, A, B, C, D, E, 61) + F4(E, T, A, B, C, D, 62) + F4(D, E, T, A, B, C, 63) + F4(C, D, E, T, A, B, 64) + F4(B, C, D, E, T, A, 65) + F4(A, B, C, D, E, T, 66) + F4(T, A, B, C, D, E, 67) + F4(E, T, A, B, C, D, 68) + F4(D, E, T, A, B, C, 69) + F4(C, D, E, T, A, B, 70) + F4(B, C, D, E, T, A, 71) + F4(A, B, C, D, E, T, 72) + F4(T, A, B, C, D, E, 73) + F4(E, T, A, B, C, D, 74) + F4(D, E, T, A, B, C, 75) + F4(C, D, E, T, A, B, 76) + F4(B, C, D, E, T, A, 77) + F4(A, B, C, D, E, T, 78) + F4(T, A, B, C, D, E, 79) + + ADD(ARRAY4(DIGEST_ARR, 0), D) + ADD(ARRAY4(DIGEST_ARR, 1), T) + ADD(ARRAY4(DIGEST_ARR, 2), A) + ADD(ARRAY4(DIGEST_ARR, 3), B) + ADD(ARRAY4(DIGEST_ARR, 4), C) + +END_FUNCTION(botan_sha160_amd64_compress) diff --git a/botan/src/hash/sha1_ia32/info.txt b/botan/src/hash/sha1_ia32/info.txt new file mode 100644 index 0000000..bfb3211 --- /dev/null +++ b/botan/src/hash/sha1_ia32/info.txt @@ -0,0 +1,35 @@ +realname "SHA-1 (IA-32)" + +define SHA1_IA32 + +load_on asm_ok + +<add> +sha1_ia32_imp.S +sha1_ia32.cpp +sha1_ia32.h +</add> + +<arch> +ia32 +</arch> + +<cc> +gcc +icc +</cc> + +# ELF systems +<os> +linux +freebsd +dragonfly +netbsd +openbsd +solaris +</os> + +<requires> +asm_ia32 +sha1 +</requires> diff --git a/botan/src/hash/sha1_ia32/sha1_ia32.cpp b/botan/src/hash/sha1_ia32/sha1_ia32.cpp new file mode 100644 index 0000000..0fa0b6b --- /dev/null +++ b/botan/src/hash/sha1_ia32/sha1_ia32.cpp @@ -0,0 +1,32 @@ +/* +* SHA-160 (IA-32) +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/sha1_ia32.h> +#include <botan/loadstor.h> + +namespace Botan { + +namespace { + +extern "C" +void botan_sha160_ia32_compress(u32bit[5], const byte[64], u32bit[81]); + +} + +/* +* SHA-160 Compression Function +*/ +void SHA_160_IA32::compress_n(const byte input[], u32bit blocks) + { + for(u32bit i = 0; i != blocks; ++i) + { + botan_sha160_ia32_compress(digest, input, W); + input += HASH_BLOCK_SIZE; + } + } + +} diff --git a/botan/src/hash/sha1_ia32/sha1_ia32.h b/botan/src/hash/sha1_ia32/sha1_ia32.h new file mode 100644 index 0000000..fd34971 --- /dev/null +++ b/botan/src/hash/sha1_ia32/sha1_ia32.h @@ -0,0 +1,31 @@ +/* +* SHA-160 (IA-32) +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_SHA_160_IA32_H__ +#define BOTAN_SHA_160_IA32_H__ + +#include <botan/sha160.h> + +namespace Botan { + +/* +* SHA-160 +*/ +class BOTAN_DLL SHA_160_IA32 : public SHA_160 + { + public: + HashFunction* clone() const { return new SHA_160_IA32; } + + // Note 81 instead of normal 80: IA-32 asm needs an extra temp + SHA_160_IA32() : SHA_160(81) {} + private: + void compress_n(const byte[], u32bit blocks); + }; + +} + +#endif diff --git a/botan/src/hash/sha1_ia32/sha1_ia32_imp.S b/botan/src/hash/sha1_ia32/sha1_ia32_imp.S new file mode 100644 index 0000000..e76b9fb --- /dev/null +++ b/botan/src/hash/sha1_ia32/sha1_ia32_imp.S @@ -0,0 +1,244 @@ +/* +* SHA-160 Source File +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/asm_macr.h> + +START_LISTING(sha1_ia32.S) + +START_FUNCTION(botan_sha160_ia32_compress) + SPILL_REGS() + +#define PUSHED 4 + + ASSIGN(EDI, ARG(2)) + ASSIGN(EBP, ARG(3)) + + ZEROIZE(ESI) + +START_LOOP(.LOAD_INPUT) + ADD_IMM(ESI, 4) + + ASSIGN(EAX, ARRAY4(EDI, 0)) + ASSIGN(EBX, ARRAY4(EDI, 1)) + ASSIGN(ECX, ARRAY4(EDI, 2)) + ASSIGN(EDX, ARRAY4(EDI, 3)) + + ADD_IMM(EDI, 16) + + BSWAP(EAX) + BSWAP(EBX) + BSWAP(ECX) + BSWAP(EDX) + + ASSIGN(ARRAY4_INDIRECT(EBP,ESI,-4), EAX) + ASSIGN(ARRAY4_INDIRECT(EBP,ESI,-3), EBX) + ASSIGN(ARRAY4_INDIRECT(EBP,ESI,-2), ECX) + ASSIGN(ARRAY4_INDIRECT(EBP,ESI,-1), EDX) +LOOP_UNTIL_EQ(ESI, 16, .LOAD_INPUT) + + ADD2_IMM(EDI, EBP, 64) + +START_LOOP(.EXPANSION) + ADD_IMM(ESI, 4) + + ZEROIZE(EAX) + ASSIGN(EBX, ARRAY4(EDI, -1)) + ASSIGN(ECX, ARRAY4(EDI, -2)) + ASSIGN(EDX, ARRAY4(EDI, -3)) + + XOR(EAX, ARRAY4(EDI, -5)) + XOR(EBX, ARRAY4(EDI, -6)) + XOR(ECX, ARRAY4(EDI, -7)) + XOR(EDX, ARRAY4(EDI, -8)) + + XOR(EAX, ARRAY4(EDI, -11)) + XOR(EBX, ARRAY4(EDI, -12)) + XOR(ECX, ARRAY4(EDI, -13)) + XOR(EDX, ARRAY4(EDI, -14)) + + XOR(EAX, ARRAY4(EDI, -13)) + XOR(EBX, ARRAY4(EDI, -14)) + XOR(ECX, ARRAY4(EDI, -15)) + XOR(EDX, ARRAY4(EDI, -16)) + + ROTL_IMM(EDX, 1) + ROTL_IMM(ECX, 1) + ROTL_IMM(EBX, 1) + XOR(EAX, EDX) + ROTL_IMM(EAX, 1) + + ASSIGN(ARRAY4(EDI, 0), EDX) + ASSIGN(ARRAY4(EDI, 1), ECX) + ASSIGN(ARRAY4(EDI, 2), EBX) + ASSIGN(ARRAY4(EDI, 3), EAX) + + ADD_IMM(EDI, 16) +LOOP_UNTIL_EQ(ESI, 80, .EXPANSION) + +#define MAGIC1 0x5A827999 +#define MAGIC2 0x6ED9EBA1 +#define MAGIC3 0x8F1BBCDC +#define MAGIC4 0xCA62C1D6 + +#define MSG ESP +#define T2 EBP + +#define F1(A, B, C, D, E, F, N) \ + ASSIGN(T2, ARRAY4(MSG, N)) ; \ + ASSIGN(A, F) ; \ + ROTL_IMM(F, 5) ; \ + ADD(F, E) ; \ + ASSIGN(E, C) ; \ + XOR(E, D) ; \ + ADD3_IMM(F, T2, MAGIC1) ; \ + AND(E, B) ; \ + XOR(E, D) ; \ + ROTR_IMM(B, 2) ; \ + ADD(E, F) ; + +#define F2_4(A, B, C, D, E, F, N, MAGIC) \ + ASSIGN(T2, ARRAY4(MSG, N)) ; \ + ASSIGN(A, F) ; \ + ROTL_IMM(F, 5) ; \ + ADD(F, E) ; \ + ASSIGN(E, B) ; \ + XOR(E, C) ; \ + ADD3_IMM(F, T2, MAGIC) ; \ + XOR(E, D) ; \ + ROTR_IMM(B, 2) ; \ + ADD(E, F) ; + +#define F3(A, B, C, D, E, F, N) \ + ASSIGN(T2, ARRAY4(MSG, N)) ; \ + ASSIGN(A, F) ; \ + ROTL_IMM(F, 5) ; \ + ADD(F, E) ; \ + ASSIGN(E, B) ; \ + OR(E, C) ; \ + AND(E, D) ; \ + ADD3_IMM(F, T2, MAGIC3) ; \ + ASSIGN(T2, B) ; \ + AND(T2, C) ; \ + OR(E, T2) ; \ + ROTR_IMM(B, 2) ; \ + ADD(E, F) ; + +#define F2(A, B, C, D, E, F, MSG) \ + F2_4(A, B, C, D, E, F, MSG, MAGIC2) + +#define F4(A, B, C, D, E, F, MSG) \ + F2_4(A, B, C, D, E, F, MSG, MAGIC4) + + ASSIGN(EAX, ARG(1)) + ASSIGN(EDI, ARRAY4(EAX, 0)) + ASSIGN(EBX, ARRAY4(EAX, 1)) + ASSIGN(ECX, ARRAY4(EAX, 2)) + ASSIGN(EDX, ARRAY4(EAX, 3)) + ASSIGN(ESI, ARRAY4(EAX, 4)) + + ASSIGN(ARRAY4(EBP, 80), ESP) + ASSIGN(ESP, EBP) + + /* First Round */ + F1(EAX, EBX, ECX, EDX, ESI, EDI, 0) + F1(EDI, EAX, EBX, ECX, EDX, ESI, 1) + F1(ESI, EDI, EAX, EBX, ECX, EDX, 2) + F1(EDX, ESI, EDI, EAX, EBX, ECX, 3) + F1(ECX, EDX, ESI, EDI, EAX, EBX, 4) + F1(EBX, ECX, EDX, ESI, EDI, EAX, 5) + F1(EAX, EBX, ECX, EDX, ESI, EDI, 6) + F1(EDI, EAX, EBX, ECX, EDX, ESI, 7) + F1(ESI, EDI, EAX, EBX, ECX, EDX, 8) + F1(EDX, ESI, EDI, EAX, EBX, ECX, 9) + F1(ECX, EDX, ESI, EDI, EAX, EBX, 10) + F1(EBX, ECX, EDX, ESI, EDI, EAX, 11) + F1(EAX, EBX, ECX, EDX, ESI, EDI, 12) + F1(EDI, EAX, EBX, ECX, EDX, ESI, 13) + F1(ESI, EDI, EAX, EBX, ECX, EDX, 14) + F1(EDX, ESI, EDI, EAX, EBX, ECX, 15) + F1(ECX, EDX, ESI, EDI, EAX, EBX, 16) + F1(EBX, ECX, EDX, ESI, EDI, EAX, 17) + F1(EAX, EBX, ECX, EDX, ESI, EDI, 18) + F1(EDI, EAX, EBX, ECX, EDX, ESI, 19) + + /* Second Round */ + F2(ESI, EDI, EAX, EBX, ECX, EDX, 20) + F2(EDX, ESI, EDI, EAX, EBX, ECX, 21) + F2(ECX, EDX, ESI, EDI, EAX, EBX, 22) + F2(EBX, ECX, EDX, ESI, EDI, EAX, 23) + F2(EAX, EBX, ECX, EDX, ESI, EDI, 24) + F2(EDI, EAX, EBX, ECX, EDX, ESI, 25) + F2(ESI, EDI, EAX, EBX, ECX, EDX, 26) + F2(EDX, ESI, EDI, EAX, EBX, ECX, 27) + F2(ECX, EDX, ESI, EDI, EAX, EBX, 28) + F2(EBX, ECX, EDX, ESI, EDI, EAX, 29) + F2(EAX, EBX, ECX, EDX, ESI, EDI, 30) + F2(EDI, EAX, EBX, ECX, EDX, ESI, 31) + F2(ESI, EDI, EAX, EBX, ECX, EDX, 32) + F2(EDX, ESI, EDI, EAX, EBX, ECX, 33) + F2(ECX, EDX, ESI, EDI, EAX, EBX, 34) + F2(EBX, ECX, EDX, ESI, EDI, EAX, 35) + F2(EAX, EBX, ECX, EDX, ESI, EDI, 36) + F2(EDI, EAX, EBX, ECX, EDX, ESI, 37) + F2(ESI, EDI, EAX, EBX, ECX, EDX, 38) + F2(EDX, ESI, EDI, EAX, EBX, ECX, 39) + + /* Third Round */ + F3(ECX, EDX, ESI, EDI, EAX, EBX, 40) + F3(EBX, ECX, EDX, ESI, EDI, EAX, 41) + F3(EAX, EBX, ECX, EDX, ESI, EDI, 42) + F3(EDI, EAX, EBX, ECX, EDX, ESI, 43) + F3(ESI, EDI, EAX, EBX, ECX, EDX, 44) + F3(EDX, ESI, EDI, EAX, EBX, ECX, 45) + F3(ECX, EDX, ESI, EDI, EAX, EBX, 46) + F3(EBX, ECX, EDX, ESI, EDI, EAX, 47) + F3(EAX, EBX, ECX, EDX, ESI, EDI, 48) + F3(EDI, EAX, EBX, ECX, EDX, ESI, 49) + F3(ESI, EDI, EAX, EBX, ECX, EDX, 50) + F3(EDX, ESI, EDI, EAX, EBX, ECX, 51) + F3(ECX, EDX, ESI, EDI, EAX, EBX, 52) + F3(EBX, ECX, EDX, ESI, EDI, EAX, 53) + F3(EAX, EBX, ECX, EDX, ESI, EDI, 54) + F3(EDI, EAX, EBX, ECX, EDX, ESI, 55) + F3(ESI, EDI, EAX, EBX, ECX, EDX, 56) + F3(EDX, ESI, EDI, EAX, EBX, ECX, 57) + F3(ECX, EDX, ESI, EDI, EAX, EBX, 58) + F3(EBX, ECX, EDX, ESI, EDI, EAX, 59) + + /* Fourth Round */ + F4(EAX, EBX, ECX, EDX, ESI, EDI, 60) + F4(EDI, EAX, EBX, ECX, EDX, ESI, 61) + F4(ESI, EDI, EAX, EBX, ECX, EDX, 62) + F4(EDX, ESI, EDI, EAX, EBX, ECX, 63) + F4(ECX, EDX, ESI, EDI, EAX, EBX, 64) + F4(EBX, ECX, EDX, ESI, EDI, EAX, 65) + F4(EAX, EBX, ECX, EDX, ESI, EDI, 66) + F4(EDI, EAX, EBX, ECX, EDX, ESI, 67) + F4(ESI, EDI, EAX, EBX, ECX, EDX, 68) + F4(EDX, ESI, EDI, EAX, EBX, ECX, 69) + F4(ECX, EDX, ESI, EDI, EAX, EBX, 70) + F4(EBX, ECX, EDX, ESI, EDI, EAX, 71) + F4(EAX, EBX, ECX, EDX, ESI, EDI, 72) + F4(EDI, EAX, EBX, ECX, EDX, ESI, 73) + F4(ESI, EDI, EAX, EBX, ECX, EDX, 74) + F4(EDX, ESI, EDI, EAX, EBX, ECX, 75) + F4(ECX, EDX, ESI, EDI, EAX, EBX, 76) + F4(EBX, ECX, EDX, ESI, EDI, EAX, 77) + F4(EAX, EBX, ECX, EDX, ESI, EDI, 78) + F4(EDI, EAX, EBX, ECX, EDX, ESI, 79) + + ASSIGN(ESP, ARRAY4(ESP, 80)) + + ASSIGN(EBP, ARG(1)) + ADD(ARRAY4(EBP, 0), EDX) + ADD(ARRAY4(EBP, 1), EDI) + ADD(ARRAY4(EBP, 2), EAX) + ADD(ARRAY4(EBP, 3), EBX) + ADD(ARRAY4(EBP, 4), ECX) + + RESTORE_REGS() +END_FUNCTION(botan_sha160_ia32_compress) diff --git a/botan/src/hash/sha1_sse2/info.txt b/botan/src/hash/sha1_sse2/info.txt new file mode 100644 index 0000000..b8d693b --- /dev/null +++ b/botan/src/hash/sha1_sse2/info.txt @@ -0,0 +1,28 @@ +realname "SHA-1 (SSE2)" + +define SHA1_SSE2 + +load_on auto + +<add> +sha1_sse2_imp.cpp +sha1_sse2.cpp +sha1_sse2.h +</add> + +<arch> +pentium-m +pentium4 +prescott +amd64 +</arch> + +<cc> +gcc +icc +</cc> + +<requires> +sha1 +sse2_eng +</requires> diff --git a/botan/src/hash/sha1_sse2/sha1_sse2.cpp b/botan/src/hash/sha1_sse2/sha1_sse2.cpp new file mode 100644 index 0000000..dddc06b --- /dev/null +++ b/botan/src/hash/sha1_sse2/sha1_sse2.cpp @@ -0,0 +1,24 @@ +/* +* SHA-160 (SSE2) +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/sha1_sse2.h> + +namespace Botan { + +/* +* SHA-160 Compression Function +*/ +void SHA_160_SSE2::compress_n(const byte input[], u32bit blocks) + { + for(u32bit i = 0; i != blocks; ++i) + { + botan_sha1_sse2_compress(digest, reinterpret_cast<const u32bit*>(input)); + input += HASH_BLOCK_SIZE; + } + } + +} diff --git a/botan/src/hash/sha1_sse2/sha1_sse2.h b/botan/src/hash/sha1_sse2/sha1_sse2.h new file mode 100644 index 0000000..0f8eebe --- /dev/null +++ b/botan/src/hash/sha1_sse2/sha1_sse2.h @@ -0,0 +1,31 @@ +/* +* SHA-160 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_SHA_160_SSE2_H__ +#define BOTAN_SHA_160_SSE2_H__ + +#include <botan/sha160.h> + +namespace Botan { + +/* +* SHA-160 +*/ +class BOTAN_DLL SHA_160_SSE2 : public SHA_160 + { + public: + HashFunction* clone() const { return new SHA_160_SSE2; } + SHA_160_SSE2() : SHA_160(0) {} // no W needed + private: + void compress_n(const byte[], u32bit blocks); + }; + +extern "C" void botan_sha1_sse2_compress(u32bit[5], const u32bit*); + +} + +#endif diff --git a/botan/src/hash/sha1_sse2/sha1_sse2_imp.cpp b/botan/src/hash/sha1_sse2/sha1_sse2_imp.cpp new file mode 100644 index 0000000..90a8dcc --- /dev/null +++ b/botan/src/hash/sha1_sse2/sha1_sse2_imp.cpp @@ -0,0 +1,331 @@ +/* this code is public domain. + + * dean gaudet <dean@arctic.org> + + * this code was inspired by this paper: + + * SHA: A Design for Parallel Architectures? + * Antoon Bosselaers, Ren´e Govaerts and Joos Vandewalle + * <http://www.esat.kuleuven.ac.be/~cosicart/pdf/AB-9700.pdf> + + * more information available on this implementation here: + + * http://arctic.org/~dean/crypto/sha1.html + + * version: 2 + */ + +/* + * Lightly modified for Botan, tested under GCC 4.1.1 and ICC 9.1 + * on a Linux/Core2 system. + + */ +#include <botan/sha1_sse2.h> +#include <xmmintrin.h> + +namespace Botan { + +namespace { + +typedef union { + u32bit u32[4]; + __m128i u128; + } v4si __attribute__((aligned(16))); + +static const v4si K00_19 = { { 0x5a827999, 0x5a827999, 0x5a827999, 0x5a827999 } }; +static const v4si K20_39 = { { 0x6ed9eba1, 0x6ed9eba1, 0x6ed9eba1, 0x6ed9eba1 } }; +static const v4si K40_59 = { { 0x8f1bbcdc, 0x8f1bbcdc, 0x8f1bbcdc, 0x8f1bbcdc } }; +static const v4si K60_79 = { { 0xca62c1d6, 0xca62c1d6, 0xca62c1d6, 0xca62c1d6 } }; + +#define UNALIGNED 1 +#if UNALIGNED +#define load(p) _mm_loadu_si128(p) +#else +#define load(p) (*p) +#endif + + +/* +the first 16 bytes only need byte swapping + +prepared points to 4x u32bit, 16-byte aligned + +W points to the 4 dwords which need preparing -- +and is overwritten with the swapped bytes +*/ +#define prep00_15(prep, W) do { \ + __m128i r1, r2; \ + \ + r1 = (W); \ + if (1) { \ + r1 = _mm_shufflehi_epi16(r1, _MM_SHUFFLE(2, 3, 0, 1)); \ + r1 = _mm_shufflelo_epi16(r1, _MM_SHUFFLE(2, 3, 0, 1)); \ + r2 = _mm_slli_epi16(r1, 8); \ + r1 = _mm_srli_epi16(r1, 8); \ + r1 = _mm_or_si128(r1, r2); \ + (W) = r1; \ + } \ + (prep).u128 = _mm_add_epi32(K00_19.u128, r1); \ + } while(0) + + + +/* +for each multiple of 4, t, we want to calculate this: + +W[t+0] = rol(W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16], 1); +W[t+1] = rol(W[t-2] ^ W[t-7] ^ W[t-13] ^ W[t-15], 1); +W[t+2] = rol(W[t-1] ^ W[t-6] ^ W[t-12] ^ W[t-14], 1); +W[t+3] = rol(W[t] ^ W[t-5] ^ W[t-11] ^ W[t-13], 1); + +we'll actually calculate this: + +W[t+0] = rol(W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16], 1); +W[t+1] = rol(W[t-2] ^ W[t-7] ^ W[t-13] ^ W[t-15], 1); +W[t+2] = rol(W[t-1] ^ W[t-6] ^ W[t-12] ^ W[t-14], 1); +W[t+3] = rol( 0 ^ W[t-5] ^ W[t-11] ^ W[t-13], 1); +W[t+3] ^= rol(W[t+0], 1); + +the parameters are: + +W0 = &W[t-16]; +W1 = &W[t-12]; +W2 = &W[t- 8]; +W3 = &W[t- 4]; + +and on output: +prepared = W0 + K +W0 = W[t]..W[t+3] +*/ + +/* note that there is a step here where i want to do a rol by 1, which +* normally would look like this: +* +* r1 = psrld r0,$31 +* r0 = pslld r0,$1 +* r0 = por r0,r1 +* +* but instead i do this: +* +* r1 = pcmpltd r0,zero +* r0 = paddd r0,r0 +* r0 = psub r0,r1 +* +* because pcmpltd and paddd are availabe in both MMX units on +* efficeon, pentium-m, and opteron but shifts are available in +* only one unit. +*/ +#define prep(prep, XW0, XW1, XW2, XW3, K) do { \ + __m128i r0, r1, r2, r3; \ + \ + /* load W[t-4] 16-byte aligned, and shift */ \ + r3 = _mm_srli_si128((XW3), 4); \ + r0 = (XW0); \ + /* get high 64-bits of XW0 into low 64-bits */ \ + r1 = _mm_shuffle_epi32((XW0), _MM_SHUFFLE(1,0,3,2)); \ + /* load high 64-bits of r1 */ \ + r1 = _mm_unpacklo_epi64(r1, (XW1)); \ + r2 = (XW2); \ + \ + r0 = _mm_xor_si128(r1, r0); \ + r2 = _mm_xor_si128(r3, r2); \ + r0 = _mm_xor_si128(r2, r0); \ + /* unrotated W[t]..W[t+2] in r0 ... still need W[t+3] */ \ + \ + r2 = _mm_slli_si128(r0, 12); \ + r1 = _mm_cmplt_epi32(r0, _mm_setzero_si128()); \ + r0 = _mm_add_epi32(r0, r0); /* shift left by 1 */ \ + r0 = _mm_sub_epi32(r0, r1); /* r0 has W[t]..W[t+2] */ \ + \ + r3 = _mm_srli_epi32(r2, 30); \ + r2 = _mm_slli_epi32(r2, 2); \ + \ + r0 = _mm_xor_si128(r0, r3); \ + r0 = _mm_xor_si128(r0, r2); /* r0 now has W[t+3] */ \ + \ + (XW0) = r0; \ + (prep).u128 = _mm_add_epi32(r0, (K).u128); \ + } while(0) + + +static inline u32bit rol(u32bit src, u32bit amt) + { + /* gcc and icc appear to turn this into a rotate */ + return (src << amt) | (src >> (32 - amt)); + } + + +static inline u32bit f00_19(u32bit x, u32bit y, u32bit z) + { + /* FIPS 180-2 says this: (x & y) ^ (~x & z) + * but we can calculate it in fewer steps. + */ + return ((y ^ z) & x) ^ z; + } + + +static inline u32bit f20_39(u32bit x, u32bit y, u32bit z) + { + return (x ^ z) ^ y; + } + + +static inline u32bit f40_59(u32bit x, u32bit y, u32bit z) + { + /* FIPS 180-2 says this: (x & y) ^ (x & z) ^ (y & z) + * but we can calculate it in fewer steps. + */ + return (x & z) | ((x | z) & y); + } + + +static inline u32bit f60_79(u32bit x, u32bit y, u32bit z) + { + return f20_39(x, y, z); + } + +#define step(nn_mm, xa, xb, xc, xd, xe, xt, input) do { \ + (xt) = (input) + f##nn_mm((xb), (xc), (xd)); \ + (xb) = rol((xb), 30); \ + (xt) += ((xe) + rol((xa), 5)); \ + } while(0) + +} + +extern "C" void botan_sha1_sse2_compress(u32bit H[5], + const u32bit* inputu) + { + const __m128i * input = (const __m128i *)inputu; + __m128i W0, W1, W2, W3; + v4si prep0, prep1, prep2; + u32bit a, b, c, d, e, t; + + a = H[0]; + b = H[1]; + c = H[2]; + d = H[3]; + e = H[4]; + + /* i've tried arranging the SSE2 code to be 4, 8, 12, and 16 + * steps ahead of the integer code. 12 steps ahead seems + * to produce the best performance. -dean + */ + W0 = load(&input[0]); + prep00_15(prep0, W0); /* prepare for 00 through 03 */ + W1 = load(&input[1]); + prep00_15(prep1, W1); /* prepare for 04 through 07 */ + W2 = load(&input[2]); + prep00_15(prep2, W2); /* prepare for 08 through 11 */ + + W3 = load(&input[3]); + step(00_19, a, b, c, d, e, t, prep0.u32[0]); /* 00 */ + step(00_19, t, a, b, c, d, e, prep0.u32[1]); /* 01 */ + step(00_19, e, t, a, b, c, d, prep0.u32[2]); /* 02 */ + step(00_19, d, e, t, a, b, c, prep0.u32[3]); /* 03 */ + prep00_15(prep0, W3); + step(00_19, c, d, e, t, a, b, prep1.u32[0]); /* 04 */ + step(00_19, b, c, d, e, t, a, prep1.u32[1]); /* 05 */ + step(00_19, a, b, c, d, e, t, prep1.u32[2]); /* 06 */ + step(00_19, t, a, b, c, d, e, prep1.u32[3]); /* 07 */ + prep(prep1, W0, W1, W2, W3, K00_19); /* prepare for 16 through 19 */ + step(00_19, e, t, a, b, c, d, prep2.u32[0]); /* 08 */ + step(00_19, d, e, t, a, b, c, prep2.u32[1]); /* 09 */ + step(00_19, c, d, e, t, a, b, prep2.u32[2]); /* 10 */ + step(00_19, b, c, d, e, t, a, prep2.u32[3]); /* 11 */ + prep(prep2, W1, W2, W3, W0, K20_39); /* prepare for 20 through 23 */ + step(00_19, a, b, c, d, e, t, prep0.u32[0]); /* 12 */ + step(00_19, t, a, b, c, d, e, prep0.u32[1]); /* 13 */ + step(00_19, e, t, a, b, c, d, prep0.u32[2]); /* 14 */ + step(00_19, d, e, t, a, b, c, prep0.u32[3]); /* 15 */ + prep(prep0, W2, W3, W0, W1, K20_39); + step(00_19, c, d, e, t, a, b, prep1.u32[0]); /* 16 */ + step(00_19, b, c, d, e, t, a, prep1.u32[1]); /* 17 */ + step(00_19, a, b, c, d, e, t, prep1.u32[2]); /* 18 */ + step(00_19, t, a, b, c, d, e, prep1.u32[3]); /* 19 */ + + prep(prep1, W3, W0, W1, W2, K20_39); + step(20_39, e, t, a, b, c, d, prep2.u32[0]); /* 20 */ + step(20_39, d, e, t, a, b, c, prep2.u32[1]); /* 21 */ + step(20_39, c, d, e, t, a, b, prep2.u32[2]); /* 22 */ + step(20_39, b, c, d, e, t, a, prep2.u32[3]); /* 23 */ + prep(prep2, W0, W1, W2, W3, K20_39); + step(20_39, a, b, c, d, e, t, prep0.u32[0]); /* 24 */ + step(20_39, t, a, b, c, d, e, prep0.u32[1]); /* 25 */ + step(20_39, e, t, a, b, c, d, prep0.u32[2]); /* 26 */ + step(20_39, d, e, t, a, b, c, prep0.u32[3]); /* 27 */ + prep(prep0, W1, W2, W3, W0, K20_39); + step(20_39, c, d, e, t, a, b, prep1.u32[0]); /* 28 */ + step(20_39, b, c, d, e, t, a, prep1.u32[1]); /* 29 */ + step(20_39, a, b, c, d, e, t, prep1.u32[2]); /* 30 */ + step(20_39, t, a, b, c, d, e, prep1.u32[3]); /* 31 */ + prep(prep1, W2, W3, W0, W1, K40_59); + step(20_39, e, t, a, b, c, d, prep2.u32[0]); /* 32 */ + step(20_39, d, e, t, a, b, c, prep2.u32[1]); /* 33 */ + step(20_39, c, d, e, t, a, b, prep2.u32[2]); /* 34 */ + step(20_39, b, c, d, e, t, a, prep2.u32[3]); /* 35 */ + prep(prep2, W3, W0, W1, W2, K40_59); + step(20_39, a, b, c, d, e, t, prep0.u32[0]); /* 36 */ + step(20_39, t, a, b, c, d, e, prep0.u32[1]); /* 37 */ + step(20_39, e, t, a, b, c, d, prep0.u32[2]); /* 38 */ + step(20_39, d, e, t, a, b, c, prep0.u32[3]); /* 39 */ + + prep(prep0, W0, W1, W2, W3, K40_59); + step(40_59, c, d, e, t, a, b, prep1.u32[0]); /* 40 */ + step(40_59, b, c, d, e, t, a, prep1.u32[1]); /* 41 */ + step(40_59, a, b, c, d, e, t, prep1.u32[2]); /* 42 */ + step(40_59, t, a, b, c, d, e, prep1.u32[3]); /* 43 */ + prep(prep1, W1, W2, W3, W0, K40_59); + step(40_59, e, t, a, b, c, d, prep2.u32[0]); /* 44 */ + step(40_59, d, e, t, a, b, c, prep2.u32[1]); /* 45 */ + step(40_59, c, d, e, t, a, b, prep2.u32[2]); /* 46 */ + step(40_59, b, c, d, e, t, a, prep2.u32[3]); /* 47 */ + prep(prep2, W2, W3, W0, W1, K40_59); + step(40_59, a, b, c, d, e, t, prep0.u32[0]); /* 48 */ + step(40_59, t, a, b, c, d, e, prep0.u32[1]); /* 49 */ + step(40_59, e, t, a, b, c, d, prep0.u32[2]); /* 50 */ + step(40_59, d, e, t, a, b, c, prep0.u32[3]); /* 51 */ + prep(prep0, W3, W0, W1, W2, K60_79); + step(40_59, c, d, e, t, a, b, prep1.u32[0]); /* 52 */ + step(40_59, b, c, d, e, t, a, prep1.u32[1]); /* 53 */ + step(40_59, a, b, c, d, e, t, prep1.u32[2]); /* 54 */ + step(40_59, t, a, b, c, d, e, prep1.u32[3]); /* 55 */ + prep(prep1, W0, W1, W2, W3, K60_79); + step(40_59, e, t, a, b, c, d, prep2.u32[0]); /* 56 */ + step(40_59, d, e, t, a, b, c, prep2.u32[1]); /* 57 */ + step(40_59, c, d, e, t, a, b, prep2.u32[2]); /* 58 */ + step(40_59, b, c, d, e, t, a, prep2.u32[3]); /* 59 */ + + prep(prep2, W1, W2, W3, W0, K60_79); + step(60_79, a, b, c, d, e, t, prep0.u32[0]); /* 60 */ + step(60_79, t, a, b, c, d, e, prep0.u32[1]); /* 61 */ + step(60_79, e, t, a, b, c, d, prep0.u32[2]); /* 62 */ + step(60_79, d, e, t, a, b, c, prep0.u32[3]); /* 63 */ + prep(prep0, W2, W3, W0, W1, K60_79); + step(60_79, c, d, e, t, a, b, prep1.u32[0]); /* 64 */ + step(60_79, b, c, d, e, t, a, prep1.u32[1]); /* 65 */ + step(60_79, a, b, c, d, e, t, prep1.u32[2]); /* 66 */ + step(60_79, t, a, b, c, d, e, prep1.u32[3]); /* 67 */ + prep(prep1, W3, W0, W1, W2, K60_79); + step(60_79, e, t, a, b, c, d, prep2.u32[0]); /* 68 */ + step(60_79, d, e, t, a, b, c, prep2.u32[1]); /* 69 */ + step(60_79, c, d, e, t, a, b, prep2.u32[2]); /* 70 */ + step(60_79, b, c, d, e, t, a, prep2.u32[3]); /* 71 */ + + step(60_79, a, b, c, d, e, t, prep0.u32[0]); /* 72 */ + step(60_79, t, a, b, c, d, e, prep0.u32[1]); /* 73 */ + step(60_79, e, t, a, b, c, d, prep0.u32[2]); /* 74 */ + step(60_79, d, e, t, a, b, c, prep0.u32[3]); /* 75 */ + /* no more input to prepare */ + step(60_79, c, d, e, t, a, b, prep1.u32[0]); /* 76 */ + step(60_79, b, c, d, e, t, a, prep1.u32[1]); /* 77 */ + step(60_79, a, b, c, d, e, t, prep1.u32[2]); /* 78 */ + step(60_79, t, a, b, c, d, e, prep1.u32[3]); /* 79 */ + /* e, t, a, b, c, d */ + H[0] += e; + H[1] += t; + H[2] += a; + H[3] += b; + H[4] += c; + } + +} diff --git a/botan/src/hash/sha2/info.txt b/botan/src/hash/sha2/info.txt new file mode 100644 index 0000000..9b92ff3 --- /dev/null +++ b/botan/src/hash/sha2/info.txt @@ -0,0 +1,16 @@ +realname "SHA-2 (224, 256, 384, 512)" + +define SHA2 + +load_on auto + +<add> +sha2_32.cpp +sha2_32.h +sha2_64.cpp +sha2_64.h +</add> + +<requires> +mdx_hash +</requires> diff --git a/botan/src/hash/sha2/sha2_32.cpp b/botan/src/hash/sha2/sha2_32.cpp new file mode 100644 index 0000000..9da2ec2 --- /dev/null +++ b/botan/src/hash/sha2/sha2_32.cpp @@ -0,0 +1,193 @@ +/* +* SHA-{224,256} +* (C) 1999-2008 Jack Lloyd +* 2007 FlexSecure GmbH +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/sha2_32.h> +#include <botan/loadstor.h> +#include <botan/rotate.h> + +namespace Botan { + +namespace { + +/* +* SHA-256 Rho Function +*/ +inline u32bit rho(u32bit X, u32bit rot1, u32bit rot2, u32bit rot3) + { + return (rotate_right(X, rot1) ^ rotate_right(X, rot2) ^ + rotate_right(X, rot3)); + } + +/* +* SHA-256 Sigma Function +*/ +inline u32bit sigma(u32bit X, u32bit rot1, u32bit rot2, u32bit shift) + { + return (rotate_right(X, rot1) ^ rotate_right(X, rot2) ^ (X >> shift)); + } + +/* +* SHA-256 F1 Function +*/ +inline void F1(u32bit A, u32bit B, u32bit C, u32bit& D, + u32bit E, u32bit F, u32bit G, u32bit& H, + u32bit msg, u32bit magic) + { + magic += rho(E, 6, 11, 25) + ((E & F) ^ (~E & G)) + msg; + D += magic + H; + H += magic + rho(A, 2, 13, 22) + ((A & B) ^ (A & C) ^ (B & C)); + } + +} + +/* +* SHA-256 Compression Function +*/ +void SHA_224_256_BASE::compress_n(const byte input[], u32bit blocks) + { + u32bit A = digest[0], B = digest[1], C = digest[2], + D = digest[3], E = digest[4], F = digest[5], + G = digest[6], H = digest[7]; + + for(u32bit i = 0; i != blocks; ++i) + { + for(u32bit j = 0; j != 16; ++j) + W[j] = load_be<u32bit>(input, j); + input += HASH_BLOCK_SIZE; + + for(u32bit j = 16; j != 64; ++j) + W[j] = sigma(W[j- 2], 17, 19, 10) + W[j- 7] + + sigma(W[j-15], 7, 18, 3) + W[j-16]; + + F1(A, B, C, D, E, F, G, H, W[ 0], 0x428A2F98); + F1(H, A, B, C, D, E, F, G, W[ 1], 0x71374491); + F1(G, H, A, B, C, D, E, F, W[ 2], 0xB5C0FBCF); + F1(F, G, H, A, B, C, D, E, W[ 3], 0xE9B5DBA5); + F1(E, F, G, H, A, B, C, D, W[ 4], 0x3956C25B); + F1(D, E, F, G, H, A, B, C, W[ 5], 0x59F111F1); + F1(C, D, E, F, G, H, A, B, W[ 6], 0x923F82A4); + F1(B, C, D, E, F, G, H, A, W[ 7], 0xAB1C5ED5); + F1(A, B, C, D, E, F, G, H, W[ 8], 0xD807AA98); + F1(H, A, B, C, D, E, F, G, W[ 9], 0x12835B01); + F1(G, H, A, B, C, D, E, F, W[10], 0x243185BE); + F1(F, G, H, A, B, C, D, E, W[11], 0x550C7DC3); + F1(E, F, G, H, A, B, C, D, W[12], 0x72BE5D74); + F1(D, E, F, G, H, A, B, C, W[13], 0x80DEB1FE); + F1(C, D, E, F, G, H, A, B, W[14], 0x9BDC06A7); + F1(B, C, D, E, F, G, H, A, W[15], 0xC19BF174); + F1(A, B, C, D, E, F, G, H, W[16], 0xE49B69C1); + F1(H, A, B, C, D, E, F, G, W[17], 0xEFBE4786); + F1(G, H, A, B, C, D, E, F, W[18], 0x0FC19DC6); + F1(F, G, H, A, B, C, D, E, W[19], 0x240CA1CC); + F1(E, F, G, H, A, B, C, D, W[20], 0x2DE92C6F); + F1(D, E, F, G, H, A, B, C, W[21], 0x4A7484AA); + F1(C, D, E, F, G, H, A, B, W[22], 0x5CB0A9DC); + F1(B, C, D, E, F, G, H, A, W[23], 0x76F988DA); + F1(A, B, C, D, E, F, G, H, W[24], 0x983E5152); + F1(H, A, B, C, D, E, F, G, W[25], 0xA831C66D); + F1(G, H, A, B, C, D, E, F, W[26], 0xB00327C8); + F1(F, G, H, A, B, C, D, E, W[27], 0xBF597FC7); + F1(E, F, G, H, A, B, C, D, W[28], 0xC6E00BF3); + F1(D, E, F, G, H, A, B, C, W[29], 0xD5A79147); + F1(C, D, E, F, G, H, A, B, W[30], 0x06CA6351); + F1(B, C, D, E, F, G, H, A, W[31], 0x14292967); + F1(A, B, C, D, E, F, G, H, W[32], 0x27B70A85); + F1(H, A, B, C, D, E, F, G, W[33], 0x2E1B2138); + F1(G, H, A, B, C, D, E, F, W[34], 0x4D2C6DFC); + F1(F, G, H, A, B, C, D, E, W[35], 0x53380D13); + F1(E, F, G, H, A, B, C, D, W[36], 0x650A7354); + F1(D, E, F, G, H, A, B, C, W[37], 0x766A0ABB); + F1(C, D, E, F, G, H, A, B, W[38], 0x81C2C92E); + F1(B, C, D, E, F, G, H, A, W[39], 0x92722C85); + F1(A, B, C, D, E, F, G, H, W[40], 0xA2BFE8A1); + F1(H, A, B, C, D, E, F, G, W[41], 0xA81A664B); + F1(G, H, A, B, C, D, E, F, W[42], 0xC24B8B70); + F1(F, G, H, A, B, C, D, E, W[43], 0xC76C51A3); + F1(E, F, G, H, A, B, C, D, W[44], 0xD192E819); + F1(D, E, F, G, H, A, B, C, W[45], 0xD6990624); + F1(C, D, E, F, G, H, A, B, W[46], 0xF40E3585); + F1(B, C, D, E, F, G, H, A, W[47], 0x106AA070); + F1(A, B, C, D, E, F, G, H, W[48], 0x19A4C116); + F1(H, A, B, C, D, E, F, G, W[49], 0x1E376C08); + F1(G, H, A, B, C, D, E, F, W[50], 0x2748774C); + F1(F, G, H, A, B, C, D, E, W[51], 0x34B0BCB5); + F1(E, F, G, H, A, B, C, D, W[52], 0x391C0CB3); + F1(D, E, F, G, H, A, B, C, W[53], 0x4ED8AA4A); + F1(C, D, E, F, G, H, A, B, W[54], 0x5B9CCA4F); + F1(B, C, D, E, F, G, H, A, W[55], 0x682E6FF3); + F1(A, B, C, D, E, F, G, H, W[56], 0x748F82EE); + F1(H, A, B, C, D, E, F, G, W[57], 0x78A5636F); + F1(G, H, A, B, C, D, E, F, W[58], 0x84C87814); + F1(F, G, H, A, B, C, D, E, W[59], 0x8CC70208); + F1(E, F, G, H, A, B, C, D, W[60], 0x90BEFFFA); + F1(D, E, F, G, H, A, B, C, W[61], 0xA4506CEB); + F1(C, D, E, F, G, H, A, B, W[62], 0xBEF9A3F7); + F1(B, C, D, E, F, G, H, A, W[63], 0xC67178F2); + + A = (digest[0] += A); + B = (digest[1] += B); + C = (digest[2] += C); + D = (digest[3] += D); + E = (digest[4] += E); + F = (digest[5] += F); + G = (digest[6] += G); + H = (digest[7] += H); + } + } + +/* +* Copy out the digest +*/ +void SHA_224_256_BASE::copy_out(byte output[]) + { + for(u32bit j = 0; j != OUTPUT_LENGTH; j += 4) + store_be(digest[j/4], output + j); + } + +/* +* Clear memory of sensitive data +*/ +void SHA_224_256_BASE::clear() throw() + { + MDx_HashFunction::clear(); + W.clear(); + } + +/* +* Clear memory of sensitive data +*/ +void SHA_224::clear() throw() + { + SHA_224_256_BASE::clear(); + digest[0] = 0xc1059ed8; + digest[1] = 0x367cd507; + digest[2] = 0x3070dd17; + digest[3] = 0xf70e5939; + digest[4] = 0xffc00b31; + digest[5] = 0x68581511; + digest[6] = 0x64f98fa7; + digest[7] = 0xbefa4fa4; + } + +/* +* Clear memory of sensitive data +*/ +void SHA_256::clear() throw() + { + SHA_224_256_BASE::clear(); + digest[0] = 0x6A09E667; + digest[1] = 0xBB67AE85; + digest[2] = 0x3C6EF372; + digest[3] = 0xA54FF53A; + digest[4] = 0x510E527F; + digest[5] = 0x9B05688C; + digest[6] = 0x1F83D9AB; + digest[7] = 0x5BE0CD19; + } + +} diff --git a/botan/src/hash/sha2/sha2_32.h b/botan/src/hash/sha2/sha2_32.h new file mode 100644 index 0000000..05083d1 --- /dev/null +++ b/botan/src/hash/sha2/sha2_32.h @@ -0,0 +1,59 @@ +/* +* SHA-{224,256} +* (C) 1999-2008 Jack Lloyd +* 2007 FlexSecure GmbH +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_SHA_256_H__ +#define BOTAN_SHA_256_H__ + +#include <botan/mdx_hash.h> + +namespace Botan { + +/* +* SHA-{224,256} Base +*/ +class BOTAN_DLL SHA_224_256_BASE : public MDx_HashFunction + { + protected: + void clear() throw(); + SHA_224_256_BASE(u32bit out) : + MDx_HashFunction(out, 64, true, true) { clear(); } + + SecureBuffer<u32bit, 64> W; + SecureBuffer<u32bit, 8> digest; + private: + void compress_n(const byte[], u32bit blocks); + void copy_out(byte[]); + }; + +/* +* SHA-224 +*/ +class BOTAN_DLL SHA_224 : public SHA_224_256_BASE + { + public: + void clear() throw(); + std::string name() const { return "SHA-224"; } + HashFunction* clone() const { return new SHA_224; } + SHA_224() : SHA_224_256_BASE(28) { clear(); } + }; + +/* +* SHA-256 +*/ +class BOTAN_DLL SHA_256 : public SHA_224_256_BASE + { + public: + void clear() throw(); + std::string name() const { return "SHA-256"; } + HashFunction* clone() const { return new SHA_256; } + SHA_256() : SHA_224_256_BASE(32) { clear (); } + }; + +} + +#endif diff --git a/botan/src/hash/sha2/sha2_64.cpp b/botan/src/hash/sha2/sha2_64.cpp new file mode 100644 index 0000000..c2a8749 --- /dev/null +++ b/botan/src/hash/sha2/sha2_64.cpp @@ -0,0 +1,208 @@ +/* +* SHA-{384,512} +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/sha2_64.h> +#include <botan/loadstor.h> +#include <botan/rotate.h> + +namespace Botan { + +namespace { + +/* +* SHA-{384,512} Rho Function +*/ +inline u64bit rho(u64bit X, u32bit rot1, u32bit rot2, u32bit rot3) + { + return (rotate_right(X, rot1) ^ rotate_right(X, rot2) ^ + rotate_right(X, rot3)); + } + +/* +* SHA-{384,512} F1 Function +*/ +inline void F1(u64bit A, u64bit B, u64bit C, u64bit& D, + u64bit E, u64bit F, u64bit G, u64bit& H, + u64bit msg, u64bit magic) + { + magic += rho(E, 14, 18, 41) + ((E & F) ^ (~E & G)) + msg; + D += magic + H; + H += magic + rho(A, 28, 34, 39) + ((A & B) ^ (A & C) ^ (B & C)); + } + +/* +* SHA-{384,512} Sigma Function +*/ +inline u64bit sigma(u64bit X, u32bit rot1, u32bit rot2, u32bit shift) + { + return (rotate_right(X, rot1) ^ rotate_right(X, rot2) ^ (X >> shift)); + } + +} + +/* +* SHA-{384,512} Compression Function +*/ +void SHA_384_512_BASE::compress_n(const byte input[], u32bit blocks) + { + u64bit A = digest[0], B = digest[1], C = digest[2], + D = digest[3], E = digest[4], F = digest[5], + G = digest[6], H = digest[7]; + + for(u32bit i = 0; i != blocks; ++i) + { + for(u32bit j = 0; j != 16; ++j) + W[j] = load_be<u64bit>(input, j); + input += HASH_BLOCK_SIZE; + + for(u32bit j = 16; j != 80; ++j) + W[j] = sigma(W[j- 2], 19, 61, 6) + W[j- 7] + + sigma(W[j-15], 1, 8, 7) + W[j-16]; + + F1(A, B, C, D, E, F, G, H, W[ 0], (u64bit) 0x428A2F98D728AE22ULL); + F1(H, A, B, C, D, E, F, G, W[ 1], (u64bit) 0x7137449123EF65CDULL); + F1(G, H, A, B, C, D, E, F, W[ 2], (u64bit) 0xB5C0FBCFEC4D3B2FULL); + F1(F, G, H, A, B, C, D, E, W[ 3], (u64bit) 0xE9B5DBA58189DBBCULL); + F1(E, F, G, H, A, B, C, D, W[ 4], (u64bit) 0x3956C25BF348B538ULL); + F1(D, E, F, G, H, A, B, C, W[ 5], (u64bit) 0x59F111F1B605D019ULL); + F1(C, D, E, F, G, H, A, B, W[ 6], (u64bit) 0x923F82A4AF194F9BULL); + F1(B, C, D, E, F, G, H, A, W[ 7], (u64bit) 0xAB1C5ED5DA6D8118ULL); + F1(A, B, C, D, E, F, G, H, W[ 8], (u64bit) 0xD807AA98A3030242ULL); + F1(H, A, B, C, D, E, F, G, W[ 9], (u64bit) 0x12835B0145706FBEULL); + F1(G, H, A, B, C, D, E, F, W[10], (u64bit) 0x243185BE4EE4B28CULL); + F1(F, G, H, A, B, C, D, E, W[11], (u64bit) 0x550C7DC3D5FFB4E2ULL); + F1(E, F, G, H, A, B, C, D, W[12], (u64bit) 0x72BE5D74F27B896FULL); + F1(D, E, F, G, H, A, B, C, W[13], (u64bit) 0x80DEB1FE3B1696B1ULL); + F1(C, D, E, F, G, H, A, B, W[14], (u64bit) 0x9BDC06A725C71235ULL); + F1(B, C, D, E, F, G, H, A, W[15], (u64bit) 0xC19BF174CF692694ULL); + F1(A, B, C, D, E, F, G, H, W[16], (u64bit) 0xE49B69C19EF14AD2ULL); + F1(H, A, B, C, D, E, F, G, W[17], (u64bit) 0xEFBE4786384F25E3ULL); + F1(G, H, A, B, C, D, E, F, W[18], (u64bit) 0x0FC19DC68B8CD5B5ULL); + F1(F, G, H, A, B, C, D, E, W[19], (u64bit) 0x240CA1CC77AC9C65ULL); + F1(E, F, G, H, A, B, C, D, W[20], (u64bit) 0x2DE92C6F592B0275ULL); + F1(D, E, F, G, H, A, B, C, W[21], (u64bit) 0x4A7484AA6EA6E483ULL); + F1(C, D, E, F, G, H, A, B, W[22], (u64bit) 0x5CB0A9DCBD41FBD4ULL); + F1(B, C, D, E, F, G, H, A, W[23], (u64bit) 0x76F988DA831153B5ULL); + F1(A, B, C, D, E, F, G, H, W[24], (u64bit) 0x983E5152EE66DFABULL); + F1(H, A, B, C, D, E, F, G, W[25], (u64bit) 0xA831C66D2DB43210ULL); + F1(G, H, A, B, C, D, E, F, W[26], (u64bit) 0xB00327C898FB213FULL); + F1(F, G, H, A, B, C, D, E, W[27], (u64bit) 0xBF597FC7BEEF0EE4ULL); + F1(E, F, G, H, A, B, C, D, W[28], (u64bit) 0xC6E00BF33DA88FC2ULL); + F1(D, E, F, G, H, A, B, C, W[29], (u64bit) 0xD5A79147930AA725ULL); + F1(C, D, E, F, G, H, A, B, W[30], (u64bit) 0x06CA6351E003826FULL); + F1(B, C, D, E, F, G, H, A, W[31], (u64bit) 0x142929670A0E6E70ULL); + F1(A, B, C, D, E, F, G, H, W[32], (u64bit) 0x27B70A8546D22FFCULL); + F1(H, A, B, C, D, E, F, G, W[33], (u64bit) 0x2E1B21385C26C926ULL); + F1(G, H, A, B, C, D, E, F, W[34], (u64bit) 0x4D2C6DFC5AC42AEDULL); + F1(F, G, H, A, B, C, D, E, W[35], (u64bit) 0x53380D139D95B3DFULL); + F1(E, F, G, H, A, B, C, D, W[36], (u64bit) 0x650A73548BAF63DEULL); + F1(D, E, F, G, H, A, B, C, W[37], (u64bit) 0x766A0ABB3C77B2A8ULL); + F1(C, D, E, F, G, H, A, B, W[38], (u64bit) 0x81C2C92E47EDAEE6ULL); + F1(B, C, D, E, F, G, H, A, W[39], (u64bit) 0x92722C851482353BULL); + F1(A, B, C, D, E, F, G, H, W[40], (u64bit) 0xA2BFE8A14CF10364ULL); + F1(H, A, B, C, D, E, F, G, W[41], (u64bit) 0xA81A664BBC423001ULL); + F1(G, H, A, B, C, D, E, F, W[42], (u64bit) 0xC24B8B70D0F89791ULL); + F1(F, G, H, A, B, C, D, E, W[43], (u64bit) 0xC76C51A30654BE30ULL); + F1(E, F, G, H, A, B, C, D, W[44], (u64bit) 0xD192E819D6EF5218ULL); + F1(D, E, F, G, H, A, B, C, W[45], (u64bit) 0xD69906245565A910ULL); + F1(C, D, E, F, G, H, A, B, W[46], (u64bit) 0xF40E35855771202AULL); + F1(B, C, D, E, F, G, H, A, W[47], (u64bit) 0x106AA07032BBD1B8ULL); + F1(A, B, C, D, E, F, G, H, W[48], (u64bit) 0x19A4C116B8D2D0C8ULL); + F1(H, A, B, C, D, E, F, G, W[49], (u64bit) 0x1E376C085141AB53ULL); + F1(G, H, A, B, C, D, E, F, W[50], (u64bit) 0x2748774CDF8EEB99ULL); + F1(F, G, H, A, B, C, D, E, W[51], (u64bit) 0x34B0BCB5E19B48A8ULL); + F1(E, F, G, H, A, B, C, D, W[52], (u64bit) 0x391C0CB3C5C95A63ULL); + F1(D, E, F, G, H, A, B, C, W[53], (u64bit) 0x4ED8AA4AE3418ACBULL); + F1(C, D, E, F, G, H, A, B, W[54], (u64bit) 0x5B9CCA4F7763E373ULL); + F1(B, C, D, E, F, G, H, A, W[55], (u64bit) 0x682E6FF3D6B2B8A3ULL); + F1(A, B, C, D, E, F, G, H, W[56], (u64bit) 0x748F82EE5DEFB2FCULL); + F1(H, A, B, C, D, E, F, G, W[57], (u64bit) 0x78A5636F43172F60ULL); + F1(G, H, A, B, C, D, E, F, W[58], (u64bit) 0x84C87814A1F0AB72ULL); + F1(F, G, H, A, B, C, D, E, W[59], (u64bit) 0x8CC702081A6439ECULL); + F1(E, F, G, H, A, B, C, D, W[60], (u64bit) 0x90BEFFFA23631E28ULL); + F1(D, E, F, G, H, A, B, C, W[61], (u64bit) 0xA4506CEBDE82BDE9ULL); + F1(C, D, E, F, G, H, A, B, W[62], (u64bit) 0xBEF9A3F7B2C67915ULL); + F1(B, C, D, E, F, G, H, A, W[63], (u64bit) 0xC67178F2E372532BULL); + F1(A, B, C, D, E, F, G, H, W[64], (u64bit) 0xCA273ECEEA26619CULL); + F1(H, A, B, C, D, E, F, G, W[65], (u64bit) 0xD186B8C721C0C207ULL); + F1(G, H, A, B, C, D, E, F, W[66], (u64bit) 0xEADA7DD6CDE0EB1EULL); + F1(F, G, H, A, B, C, D, E, W[67], (u64bit) 0xF57D4F7FEE6ED178ULL); + F1(E, F, G, H, A, B, C, D, W[68], (u64bit) 0x06F067AA72176FBAULL); + F1(D, E, F, G, H, A, B, C, W[69], (u64bit) 0x0A637DC5A2C898A6ULL); + F1(C, D, E, F, G, H, A, B, W[70], (u64bit) 0x113F9804BEF90DAEULL); + F1(B, C, D, E, F, G, H, A, W[71], (u64bit) 0x1B710B35131C471BULL); + F1(A, B, C, D, E, F, G, H, W[72], (u64bit) 0x28DB77F523047D84ULL); + F1(H, A, B, C, D, E, F, G, W[73], (u64bit) 0x32CAAB7B40C72493ULL); + F1(G, H, A, B, C, D, E, F, W[74], (u64bit) 0x3C9EBE0A15C9BEBCULL); + F1(F, G, H, A, B, C, D, E, W[75], (u64bit) 0x431D67C49C100D4CULL); + F1(E, F, G, H, A, B, C, D, W[76], (u64bit) 0x4CC5D4BECB3E42B6ULL); + F1(D, E, F, G, H, A, B, C, W[77], (u64bit) 0x597F299CFC657E2AULL); + F1(C, D, E, F, G, H, A, B, W[78], (u64bit) 0x5FCB6FAB3AD6FAECULL); + F1(B, C, D, E, F, G, H, A, W[79], (u64bit) 0x6C44198C4A475817ULL); + + A = (digest[0] += A); + B = (digest[1] += B); + C = (digest[2] += C); + D = (digest[3] += D); + E = (digest[4] += E); + F = (digest[5] += F); + G = (digest[6] += G); + H = (digest[7] += H); + } + } + +/* +* Copy out the digest +*/ +void SHA_384_512_BASE::copy_out(byte output[]) + { + for(u32bit j = 0; j != OUTPUT_LENGTH; j += 8) + store_be(digest[j/8], output + j); + } + +/* +* Clear memory of sensitive data +*/ +void SHA_384_512_BASE::clear() throw() + { + MDx_HashFunction::clear(); + W.clear(); + } + +/* +* Clear memory of sensitive data +*/ +void SHA_384::clear() throw() + { + SHA_384_512_BASE::clear(); + digest[0] = (u64bit) 0xCBBB9D5DC1059ED8ULL; + digest[1] = (u64bit) 0x629A292A367CD507ULL; + digest[2] = (u64bit) 0x9159015A3070DD17ULL; + digest[3] = (u64bit) 0x152FECD8F70E5939ULL; + digest[4] = (u64bit) 0x67332667FFC00B31ULL; + digest[5] = (u64bit) 0x8EB44A8768581511ULL; + digest[6] = (u64bit) 0xDB0C2E0D64F98FA7ULL; + digest[7] = (u64bit) 0x47B5481DBEFA4FA4ULL; + } + +/* +* Clear memory of sensitive data +*/ +void SHA_512::clear() throw() + { + SHA_384_512_BASE::clear(); + digest[0] = (u64bit) 0x6A09E667F3BCC908ULL; + digest[1] = (u64bit) 0xBB67AE8584CAA73BULL; + digest[2] = (u64bit) 0x3C6EF372FE94F82BULL; + digest[3] = (u64bit) 0xA54FF53A5F1D36F1ULL; + digest[4] = (u64bit) 0x510E527FADE682D1ULL; + digest[5] = (u64bit) 0x9B05688C2B3E6C1FULL; + digest[6] = (u64bit) 0x1F83D9ABFB41BD6BULL; + digest[7] = (u64bit) 0x5BE0CD19137E2179ULL; + } + +} diff --git a/botan/src/hash/sha2/sha2_64.h b/botan/src/hash/sha2/sha2_64.h new file mode 100644 index 0000000..dcc6dc8 --- /dev/null +++ b/botan/src/hash/sha2/sha2_64.h @@ -0,0 +1,60 @@ +/* +* SHA-{384,512} +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_SHA_64BIT_H__ +#define BOTAN_SHA_64BIT_H__ + +#include <botan/mdx_hash.h> + +namespace Botan { + +/* +* SHA-{384,512} Base +*/ +class BOTAN_DLL SHA_384_512_BASE : public MDx_HashFunction + { + protected: + void clear() throw(); + + SHA_384_512_BASE(u32bit out) : + MDx_HashFunction(out, 128, true, true, 16) {} + + SecureBuffer<u64bit, 8> digest; + private: + void compress_n(const byte[], u32bit blocks); + void copy_out(byte[]); + + SecureBuffer<u64bit, 80> W; + }; + +/* +* SHA-384 +*/ +class BOTAN_DLL SHA_384 : public SHA_384_512_BASE + { + public: + void clear() throw(); + std::string name() const { return "SHA-384"; } + HashFunction* clone() const { return new SHA_384; } + SHA_384() : SHA_384_512_BASE(48) { clear(); } + }; + +/* +* SHA-512 +*/ +class BOTAN_DLL SHA_512 : public SHA_384_512_BASE + { + public: + void clear() throw(); + std::string name() const { return "SHA-512"; } + HashFunction* clone() const { return new SHA_512; } + SHA_512() : SHA_384_512_BASE(64) { clear(); } + }; + +} + +#endif diff --git a/botan/src/hash/skein/info.txt b/botan/src/hash/skein/info.txt new file mode 100644 index 0000000..bab8497 --- /dev/null +++ b/botan/src/hash/skein/info.txt @@ -0,0 +1,14 @@ +realname "Skein" + +define SKEIN_512 + +load_on auto + +<add> +skein_512.cpp +skein_512.h +</add> + +<requires> +alloc +</requires> diff --git a/botan/src/hash/skein/skein_512.cpp b/botan/src/hash/skein/skein_512.cpp new file mode 100644 index 0000000..da4715d --- /dev/null +++ b/botan/src/hash/skein/skein_512.cpp @@ -0,0 +1,262 @@ +/** +* The Skein-512 hash function +* (C) 2009 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/skein_512.h> +#include <botan/loadstor.h> +#include <botan/parsing.h> +#include <botan/exceptn.h> +#include <algorithm> + +namespace Botan { + +namespace { + +enum type_code { + SKEIN_KEY = 0, + SKEIN_CONFIG = 4, + SKEIN_PERSONALIZATION = 8, + SKEIN_PUBLIC_KEY = 12, + SKEIN_KEY_IDENTIFIER = 16, + SKEIN_NONCE = 20, + SKEIN_MSG = 48, + SKEIN_OUTPUT = 63 +}; + +void ubi_512(u64bit H[9], u64bit T[], const byte msg[], u64bit msg_len) + { + do + { + const u64bit to_proc = std::min<u64bit>(msg_len, 64); + T[0] += to_proc; + + u64bit M[8] = { 0 }; + for(u32bit j = 0; j != to_proc / 8; ++j) + M[j] = load_le<u64bit>(msg, j); + + if(to_proc % 8) + { + for(u32bit j = 0; j != to_proc % 8; ++j) + M[to_proc/8] |= ((u64bit)msg[8*(to_proc/8)+j] << (8*j)); + } + + H[8] = H[0] ^ H[1] ^ H[2] ^ H[3] ^ + H[4] ^ H[5] ^ H[6] ^ H[7] ^ (u64bit) 0x5555555555555555ULL; + + T[2] = T[0] ^ T[1]; + + u64bit X0 = M[0] + H[0]; + u64bit X1 = M[1] + H[1]; + u64bit X2 = M[2] + H[2]; + u64bit X3 = M[3] + H[3]; + u64bit X4 = M[4] + H[4]; + u64bit X5 = M[5] + H[5] + T[0]; + u64bit X6 = M[6] + H[6] + T[1]; + u64bit X7 = M[7] + H[7]; + +#define THREEFISH_ROUND(I1,I2,I3,I4,I5,I6,I7,I8,ROT1,ROT2,ROT3,ROT4) \ + do { \ + X##I1 += X##I2; X##I2 = rotate_left(X##I2, ROT1) ^ X##I1; \ + X##I3 += X##I4; X##I4 = rotate_left(X##I4, ROT2) ^ X##I3; \ + X##I5 += X##I6; X##I6 = rotate_left(X##I6, ROT3) ^ X##I5; \ + X##I7 += X##I8; X##I8 = rotate_left(X##I8, ROT4) ^ X##I7; \ + } while(0); + +#define THREEFISH_INJECT_KEY(r) \ + do { \ + X0 += H[(r ) % 9]; \ + X1 += H[(r+1) % 9]; \ + X2 += H[(r+2) % 9]; \ + X3 += H[(r+3) % 9]; \ + X4 += H[(r+4) % 9]; \ + X5 += H[(r+5) % 9] + T[(r ) % 3]; \ + X6 += H[(r+6) % 9] + T[(r+1) % 3]; \ + X7 += H[(r+7) % 9] + (r); \ + } while(0); + +#define THREEFISH_8_ROUNDS(R1,R2) \ + do { \ + THREEFISH_ROUND(0,1,2,3,4,5,6,7, 46,36,19,37); \ + THREEFISH_ROUND(2,1,4,7,6,5,0,3, 33,27,14,42); \ + THREEFISH_ROUND(4,1,6,3,0,5,2,7, 17,49,36,39); \ + THREEFISH_ROUND(6,1,0,7,2,5,4,3, 44, 9,54,56); \ + \ + THREEFISH_INJECT_KEY(R1); \ + \ + THREEFISH_ROUND(0,1,2,3,4,5,6,7, 39,30,34,24); \ + THREEFISH_ROUND(2,1,4,7,6,5,0,3, 13,50,10,17); \ + THREEFISH_ROUND(4,1,6,3,0,5,2,7, 25,29,39,43); \ + THREEFISH_ROUND(6,1,0,7,2,5,4,3, 8,35,56,22); \ + \ + THREEFISH_INJECT_KEY(R2); \ + } while(0); + + THREEFISH_8_ROUNDS(1,2); + THREEFISH_8_ROUNDS(3,4); + THREEFISH_8_ROUNDS(5,6); + THREEFISH_8_ROUNDS(7,8); + THREEFISH_8_ROUNDS(9,10); + THREEFISH_8_ROUNDS(11,12); + THREEFISH_8_ROUNDS(13,14); + THREEFISH_8_ROUNDS(15,16); + THREEFISH_8_ROUNDS(17,18); + + // message feed forward + H[0] = X0 ^ M[0]; + H[1] = X1 ^ M[1]; + H[2] = X2 ^ M[2]; + H[3] = X3 ^ M[3]; + H[4] = X4 ^ M[4]; + H[5] = X5 ^ M[5]; + H[6] = X6 ^ M[6]; + H[7] = X7 ^ M[7]; + + T[1] &= ~((u64bit)1 << 62); // clear first flag if set + + msg_len -= to_proc; + msg += to_proc; + } while(msg_len); + } + +void reset_tweak(u64bit T[3], type_code type, bool final) + { + T[0] = 0; + T[1] = ((u64bit)type << 56) | ((u64bit)1 << 62) | ((u64bit)final << 63); + } + +void initial_block(u64bit H[9], u64bit T[3], u32bit output_bits, + const std::string& personalization) + { + clear_mem(H, 9); + + // ASCII("SHA3") followed by version (0x0001) code + byte config_str[32] = { 0x53, 0x48, 0x41, 0x33, 0x01, 0x00, 0 }; + store_le(output_bits, config_str + 8); + + reset_tweak(T, SKEIN_CONFIG, true); + ubi_512(H, T, config_str, sizeof(config_str)); + + if(personalization != "") + { + /* + This is a limitation of this implementation, and not of the + algorithm specification. Could be fixed relatively easily, but + doesn't seem worth the trouble. + */ + if(personalization.length() > 64) + throw Invalid_Argument("Skein personalization must be <= 64 bytes"); + + const byte* bits = reinterpret_cast<const byte*>(personalization.data()); + + reset_tweak(T, SKEIN_PERSONALIZATION, true); + ubi_512(H, T, bits, personalization.length()); + } + + reset_tweak(T, SKEIN_MSG, false); + } + +} + +Skein_512::Skein_512(u32bit arg_output_bits, + const std::string& arg_personalization) : + HashFunction(arg_output_bits / 8, 64), + personalization(arg_personalization), + output_bits(arg_output_bits) + { + if(output_bits == 0 || output_bits % 8 != 0) + throw Invalid_Argument("Bad output bits size for Skein-512"); + + buf_pos = 0; + initial_block(H, T, output_bits, personalization); + } + +std::string Skein_512::name() const + { + return "Skein-512(" + to_string(output_bits) + ")"; + } + +HashFunction* Skein_512::clone() const + { + return new Skein_512(output_bits, personalization); + } + +void Skein_512::clear() throw() + { + H.clear(); + T.clear(); + buffer.clear(); + buf_pos = 0; + } + +void Skein_512::add_data(const byte input[], u32bit length) + { + if(length == 0) + return; + + if(buf_pos) + { + buffer.copy(buf_pos, input, length); + if(buf_pos + length > 64) + { + ubi_512(H, T, &buffer[0], buffer.size()); + + input += (64 - buf_pos); + length -= (64 - buf_pos); + buf_pos = 0; + } + } + + const u32bit full_blocks = (length - 1) / 64; + + if(full_blocks) + ubi_512(H, T, input, 64*full_blocks); + + length -= full_blocks * 64; + + buffer.copy(buf_pos, input + full_blocks * 64, length); + buf_pos += length; + } + +void Skein_512::final_result(byte out[]) + { + T[1] |= ((u64bit)1 << 63); // final block flag + + for(u32bit i = buf_pos; i != buffer.size(); ++i) + buffer[i] = 0; + + ubi_512(H, T, &buffer[0], buf_pos); + + byte counter[8] = { 0 }; + + u32bit out_bytes = output_bits / 8; + + SecureBuffer<u64bit, 9> H_out; + + while(out_bytes) + { + const u32bit to_proc = std::min<u32bit>(out_bytes, 64); + + H_out.copy(H.begin(), 8); + + reset_tweak(T, SKEIN_OUTPUT, true); + ubi_512(H_out, T, counter, sizeof(counter)); + + for(u32bit i = 0; i != to_proc; ++i) + out[i] = get_byte(7-i%8, H_out[i/8]); + + out_bytes -= to_proc; + out += to_proc; + + for(u32bit i = 0; i != sizeof(counter); ++i) + if(++counter[i]) + break; + } + + buf_pos = 0; + initial_block(H, T, output_bits, personalization); + } + +} diff --git a/botan/src/hash/skein/skein_512.h b/botan/src/hash/skein/skein_512.h new file mode 100644 index 0000000..fa558fc --- /dev/null +++ b/botan/src/hash/skein/skein_512.h @@ -0,0 +1,41 @@ +/** +* The Skein-512 hash function +* (C) 2009 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_SKEIN_512_H__ +#define BOTAN_SKEIN_512_H__ + +#include <botan/secmem.h> +#include <botan/hash.h> +#include <string> + +namespace Botan { + +class BOTAN_DLL Skein_512 : public HashFunction + { + public: + Skein_512(u32bit output_bits = 512, + const std::string& personalization = ""); + + HashFunction* clone() const; + std::string name() const; + void clear() throw(); + private: + void add_data(const byte input[], u32bit length); + void final_result(byte out[]); + + std::string personalization; + u32bit output_bits; + SecureBuffer<u64bit, 9> H; + SecureBuffer<u64bit, 3> T; + + SecureBuffer<byte, 64> buffer; + u32bit buf_pos; + }; + +} + +#endif diff --git a/botan/src/hash/tiger/info.txt b/botan/src/hash/tiger/info.txt new file mode 100644 index 0000000..7d4cd71 --- /dev/null +++ b/botan/src/hash/tiger/info.txt @@ -0,0 +1,15 @@ +realname "Tiger" + +define TIGER + +load_on auto + +<add> +tig_tab.cpp +tiger.cpp +tiger.h +</add> + +<requires> +mdx_hash +</requires> diff --git a/botan/src/hash/tiger/tig_tab.cpp b/botan/src/hash/tiger/tig_tab.cpp new file mode 100644 index 0000000..6fc8686 --- /dev/null +++ b/botan/src/hash/tiger/tig_tab.cpp @@ -0,0 +1,364 @@ +/* +* S-Box Tables for Tiger +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/tiger.h> + +namespace Botan { + +const u64bit Tiger::SBOX1[256] = { + (u64bit) 0x02AAB17CF7E90C5EULL, (u64bit) 0xAC424B03E243A8ECULL, (u64bit) 0x72CD5BE30DD5FCD3ULL, + (u64bit) 0x6D019B93F6F97F3AULL, (u64bit) 0xCD9978FFD21F9193ULL, (u64bit) 0x7573A1C9708029E2ULL, + (u64bit) 0xB164326B922A83C3ULL, (u64bit) 0x46883EEE04915870ULL, (u64bit) 0xEAACE3057103ECE6ULL, + (u64bit) 0xC54169B808A3535CULL, (u64bit) 0x4CE754918DDEC47CULL, (u64bit) 0x0AA2F4DFDC0DF40CULL, + (u64bit) 0x10B76F18A74DBEFAULL, (u64bit) 0xC6CCB6235AD1AB6AULL, (u64bit) 0x13726121572FE2FFULL, + (u64bit) 0x1A488C6F199D921EULL, (u64bit) 0x4BC9F9F4DA0007CAULL, (u64bit) 0x26F5E6F6E85241C7ULL, + (u64bit) 0x859079DBEA5947B6ULL, (u64bit) 0x4F1885C5C99E8C92ULL, (u64bit) 0xD78E761EA96F864BULL, + (u64bit) 0x8E36428C52B5C17DULL, (u64bit) 0x69CF6827373063C1ULL, (u64bit) 0xB607C93D9BB4C56EULL, + (u64bit) 0x7D820E760E76B5EAULL, (u64bit) 0x645C9CC6F07FDC42ULL, (u64bit) 0xBF38A078243342E0ULL, + (u64bit) 0x5F6B343C9D2E7D04ULL, (u64bit) 0xF2C28AEB600B0EC6ULL, (u64bit) 0x6C0ED85F7254BCACULL, + (u64bit) 0x71592281A4DB4FE5ULL, (u64bit) 0x1967FA69CE0FED9FULL, (u64bit) 0xFD5293F8B96545DBULL, + (u64bit) 0xC879E9D7F2A7600BULL, (u64bit) 0x860248920193194EULL, (u64bit) 0xA4F9533B2D9CC0B3ULL, + (u64bit) 0x9053836C15957613ULL, (u64bit) 0xDB6DCF8AFC357BF1ULL, (u64bit) 0x18BEEA7A7A370F57ULL, + (u64bit) 0x037117CA50B99066ULL, (u64bit) 0x6AB30A9774424A35ULL, (u64bit) 0xF4E92F02E325249BULL, + (u64bit) 0x7739DB07061CCAE1ULL, (u64bit) 0xD8F3B49CECA42A05ULL, (u64bit) 0xBD56BE3F51382F73ULL, + (u64bit) 0x45FAED5843B0BB28ULL, (u64bit) 0x1C813D5C11BF1F83ULL, (u64bit) 0x8AF0E4B6D75FA169ULL, + (u64bit) 0x33EE18A487AD9999ULL, (u64bit) 0x3C26E8EAB1C94410ULL, (u64bit) 0xB510102BC0A822F9ULL, + (u64bit) 0x141EEF310CE6123BULL, (u64bit) 0xFC65B90059DDB154ULL, (u64bit) 0xE0158640C5E0E607ULL, + (u64bit) 0x884E079826C3A3CFULL, (u64bit) 0x930D0D9523C535FDULL, (u64bit) 0x35638D754E9A2B00ULL, + (u64bit) 0x4085FCCF40469DD5ULL, (u64bit) 0xC4B17AD28BE23A4CULL, (u64bit) 0xCAB2F0FC6A3E6A2EULL, + (u64bit) 0x2860971A6B943FCDULL, (u64bit) 0x3DDE6EE212E30446ULL, (u64bit) 0x6222F32AE01765AEULL, + (u64bit) 0x5D550BB5478308FEULL, (u64bit) 0xA9EFA98DA0EDA22AULL, (u64bit) 0xC351A71686C40DA7ULL, + (u64bit) 0x1105586D9C867C84ULL, (u64bit) 0xDCFFEE85FDA22853ULL, (u64bit) 0xCCFBD0262C5EEF76ULL, + (u64bit) 0xBAF294CB8990D201ULL, (u64bit) 0xE69464F52AFAD975ULL, (u64bit) 0x94B013AFDF133E14ULL, + (u64bit) 0x06A7D1A32823C958ULL, (u64bit) 0x6F95FE5130F61119ULL, (u64bit) 0xD92AB34E462C06C0ULL, + (u64bit) 0xED7BDE33887C71D2ULL, (u64bit) 0x79746D6E6518393EULL, (u64bit) 0x5BA419385D713329ULL, + (u64bit) 0x7C1BA6B948A97564ULL, (u64bit) 0x31987C197BFDAC67ULL, (u64bit) 0xDE6C23C44B053D02ULL, + (u64bit) 0x581C49FED002D64DULL, (u64bit) 0xDD474D6338261571ULL, (u64bit) 0xAA4546C3E473D062ULL, + (u64bit) 0x928FCE349455F860ULL, (u64bit) 0x48161BBACAAB94D9ULL, (u64bit) 0x63912430770E6F68ULL, + (u64bit) 0x6EC8A5E602C6641CULL, (u64bit) 0x87282515337DDD2BULL, (u64bit) 0x2CDA6B42034B701BULL, + (u64bit) 0xB03D37C181CB096DULL, (u64bit) 0xE108438266C71C6FULL, (u64bit) 0x2B3180C7EB51B255ULL, + (u64bit) 0xDF92B82F96C08BBCULL, (u64bit) 0x5C68C8C0A632F3BAULL, (u64bit) 0x5504CC861C3D0556ULL, + (u64bit) 0xABBFA4E55FB26B8FULL, (u64bit) 0x41848B0AB3BACEB4ULL, (u64bit) 0xB334A273AA445D32ULL, + (u64bit) 0xBCA696F0A85AD881ULL, (u64bit) 0x24F6EC65B528D56CULL, (u64bit) 0x0CE1512E90F4524AULL, + (u64bit) 0x4E9DD79D5506D35AULL, (u64bit) 0x258905FAC6CE9779ULL, (u64bit) 0x2019295B3E109B33ULL, + (u64bit) 0xF8A9478B73A054CCULL, (u64bit) 0x2924F2F934417EB0ULL, (u64bit) 0x3993357D536D1BC4ULL, + (u64bit) 0x38A81AC21DB6FF8BULL, (u64bit) 0x47C4FBF17D6016BFULL, (u64bit) 0x1E0FAADD7667E3F5ULL, + (u64bit) 0x7ABCFF62938BEB96ULL, (u64bit) 0xA78DAD948FC179C9ULL, (u64bit) 0x8F1F98B72911E50DULL, + (u64bit) 0x61E48EAE27121A91ULL, (u64bit) 0x4D62F7AD31859808ULL, (u64bit) 0xECEBA345EF5CEAEBULL, + (u64bit) 0xF5CEB25EBC9684CEULL, (u64bit) 0xF633E20CB7F76221ULL, (u64bit) 0xA32CDF06AB8293E4ULL, + (u64bit) 0x985A202CA5EE2CA4ULL, (u64bit) 0xCF0B8447CC8A8FB1ULL, (u64bit) 0x9F765244979859A3ULL, + (u64bit) 0xA8D516B1A1240017ULL, (u64bit) 0x0BD7BA3EBB5DC726ULL, (u64bit) 0xE54BCA55B86ADB39ULL, + (u64bit) 0x1D7A3AFD6C478063ULL, (u64bit) 0x519EC608E7669EDDULL, (u64bit) 0x0E5715A2D149AA23ULL, + (u64bit) 0x177D4571848FF194ULL, (u64bit) 0xEEB55F3241014C22ULL, (u64bit) 0x0F5E5CA13A6E2EC2ULL, + (u64bit) 0x8029927B75F5C361ULL, (u64bit) 0xAD139FABC3D6E436ULL, (u64bit) 0x0D5DF1A94CCF402FULL, + (u64bit) 0x3E8BD948BEA5DFC8ULL, (u64bit) 0xA5A0D357BD3FF77EULL, (u64bit) 0xA2D12E251F74F645ULL, + (u64bit) 0x66FD9E525E81A082ULL, (u64bit) 0x2E0C90CE7F687A49ULL, (u64bit) 0xC2E8BCBEBA973BC5ULL, + (u64bit) 0x000001BCE509745FULL, (u64bit) 0x423777BBE6DAB3D6ULL, (u64bit) 0xD1661C7EAEF06EB5ULL, + (u64bit) 0xA1781F354DAACFD8ULL, (u64bit) 0x2D11284A2B16AFFCULL, (u64bit) 0xF1FC4F67FA891D1FULL, + (u64bit) 0x73ECC25DCB920ADAULL, (u64bit) 0xAE610C22C2A12651ULL, (u64bit) 0x96E0A810D356B78AULL, + (u64bit) 0x5A9A381F2FE7870FULL, (u64bit) 0xD5AD62EDE94E5530ULL, (u64bit) 0xD225E5E8368D1427ULL, + (u64bit) 0x65977B70C7AF4631ULL, (u64bit) 0x99F889B2DE39D74FULL, (u64bit) 0x233F30BF54E1D143ULL, + (u64bit) 0x9A9675D3D9A63C97ULL, (u64bit) 0x5470554FF334F9A8ULL, (u64bit) 0x166ACB744A4F5688ULL, + (u64bit) 0x70C74CAAB2E4AEADULL, (u64bit) 0xF0D091646F294D12ULL, (u64bit) 0x57B82A89684031D1ULL, + (u64bit) 0xEFD95A5A61BE0B6BULL, (u64bit) 0x2FBD12E969F2F29AULL, (u64bit) 0x9BD37013FEFF9FE8ULL, + (u64bit) 0x3F9B0404D6085A06ULL, (u64bit) 0x4940C1F3166CFE15ULL, (u64bit) 0x09542C4DCDF3DEFBULL, + (u64bit) 0xB4C5218385CD5CE3ULL, (u64bit) 0xC935B7DC4462A641ULL, (u64bit) 0x3417F8A68ED3B63FULL, + (u64bit) 0xB80959295B215B40ULL, (u64bit) 0xF99CDAEF3B8C8572ULL, (u64bit) 0x018C0614F8FCB95DULL, + (u64bit) 0x1B14ACCD1A3ACDF3ULL, (u64bit) 0x84D471F200BB732DULL, (u64bit) 0xC1A3110E95E8DA16ULL, + (u64bit) 0x430A7220BF1A82B8ULL, (u64bit) 0xB77E090D39DF210EULL, (u64bit) 0x5EF4BD9F3CD05E9DULL, + (u64bit) 0x9D4FF6DA7E57A444ULL, (u64bit) 0xDA1D60E183D4A5F8ULL, (u64bit) 0xB287C38417998E47ULL, + (u64bit) 0xFE3EDC121BB31886ULL, (u64bit) 0xC7FE3CCC980CCBEFULL, (u64bit) 0xE46FB590189BFD03ULL, + (u64bit) 0x3732FD469A4C57DCULL, (u64bit) 0x7EF700A07CF1AD65ULL, (u64bit) 0x59C64468A31D8859ULL, + (u64bit) 0x762FB0B4D45B61F6ULL, (u64bit) 0x155BAED099047718ULL, (u64bit) 0x68755E4C3D50BAA6ULL, + (u64bit) 0xE9214E7F22D8B4DFULL, (u64bit) 0x2ADDBF532EAC95F4ULL, (u64bit) 0x32AE3909B4BD0109ULL, + (u64bit) 0x834DF537B08E3450ULL, (u64bit) 0xFA209DA84220728DULL, (u64bit) 0x9E691D9B9EFE23F7ULL, + (u64bit) 0x0446D288C4AE8D7FULL, (u64bit) 0x7B4CC524E169785BULL, (u64bit) 0x21D87F0135CA1385ULL, + (u64bit) 0xCEBB400F137B8AA5ULL, (u64bit) 0x272E2B66580796BEULL, (u64bit) 0x3612264125C2B0DEULL, + (u64bit) 0x057702BDAD1EFBB2ULL, (u64bit) 0xD4BABB8EACF84BE9ULL, (u64bit) 0x91583139641BC67BULL, + (u64bit) 0x8BDC2DE08036E024ULL, (u64bit) 0x603C8156F49F68EDULL, (u64bit) 0xF7D236F7DBEF5111ULL, + (u64bit) 0x9727C4598AD21E80ULL, (u64bit) 0xA08A0896670A5FD7ULL, (u64bit) 0xCB4A8F4309EBA9CBULL, + (u64bit) 0x81AF564B0F7036A1ULL, (u64bit) 0xC0B99AA778199ABDULL, (u64bit) 0x959F1EC83FC8E952ULL, + (u64bit) 0x8C505077794A81B9ULL, (u64bit) 0x3ACAAF8F056338F0ULL, (u64bit) 0x07B43F50627A6778ULL, + (u64bit) 0x4A44AB49F5ECCC77ULL, (u64bit) 0x3BC3D6E4B679EE98ULL, (u64bit) 0x9CC0D4D1CF14108CULL, + (u64bit) 0x4406C00B206BC8A0ULL, (u64bit) 0x82A18854C8D72D89ULL, (u64bit) 0x67E366B35C3C432CULL, + (u64bit) 0xB923DD61102B37F2ULL, (u64bit) 0x56AB2779D884271DULL, (u64bit) 0xBE83E1B0FF1525AFULL, + (u64bit) 0xFB7C65D4217E49A9ULL, (u64bit) 0x6BDBE0E76D48E7D4ULL, (u64bit) 0x08DF828745D9179EULL, + (u64bit) 0x22EA6A9ADD53BD34ULL, (u64bit) 0xE36E141C5622200AULL, (u64bit) 0x7F805D1B8CB750EEULL, + (u64bit) 0xAFE5C7A59F58E837ULL, (u64bit) 0xE27F996A4FB1C23CULL, (u64bit) 0xD3867DFB0775F0D0ULL, + (u64bit) 0xD0E673DE6E88891AULL, (u64bit) 0x123AEB9EAFB86C25ULL, (u64bit) 0x30F1D5D5C145B895ULL, + (u64bit) 0xBB434A2DEE7269E7ULL, (u64bit) 0x78CB67ECF931FA38ULL, (u64bit) 0xF33B0372323BBF9CULL, + (u64bit) 0x52D66336FB279C74ULL, (u64bit) 0x505F33AC0AFB4EAAULL, (u64bit) 0xE8A5CD99A2CCE187ULL, + (u64bit) 0x534974801E2D30BBULL, (u64bit) 0x8D2D5711D5876D90ULL, (u64bit) 0x1F1A412891BC038EULL, + (u64bit) 0xD6E2E71D82E56648ULL, (u64bit) 0x74036C3A497732B7ULL, (u64bit) 0x89B67ED96361F5ABULL, + (u64bit) 0xFFED95D8F1EA02A2ULL, (u64bit) 0xE72B3BD61464D43DULL, (u64bit) 0xA6300F170BDC4820ULL, + (u64bit) 0xEBC18760ED78A77AULL }; + +const u64bit Tiger::SBOX2[256] = { + (u64bit) 0xE6A6BE5A05A12138ULL, (u64bit) 0xB5A122A5B4F87C98ULL, (u64bit) 0x563C6089140B6990ULL, + (u64bit) 0x4C46CB2E391F5DD5ULL, (u64bit) 0xD932ADDBC9B79434ULL, (u64bit) 0x08EA70E42015AFF5ULL, + (u64bit) 0xD765A6673E478CF1ULL, (u64bit) 0xC4FB757EAB278D99ULL, (u64bit) 0xDF11C6862D6E0692ULL, + (u64bit) 0xDDEB84F10D7F3B16ULL, (u64bit) 0x6F2EF604A665EA04ULL, (u64bit) 0x4A8E0F0FF0E0DFB3ULL, + (u64bit) 0xA5EDEEF83DBCBA51ULL, (u64bit) 0xFC4F0A2A0EA4371EULL, (u64bit) 0xE83E1DA85CB38429ULL, + (u64bit) 0xDC8FF882BA1B1CE2ULL, (u64bit) 0xCD45505E8353E80DULL, (u64bit) 0x18D19A00D4DB0717ULL, + (u64bit) 0x34A0CFEDA5F38101ULL, (u64bit) 0x0BE77E518887CAF2ULL, (u64bit) 0x1E341438B3C45136ULL, + (u64bit) 0xE05797F49089CCF9ULL, (u64bit) 0xFFD23F9DF2591D14ULL, (u64bit) 0x543DDA228595C5CDULL, + (u64bit) 0x661F81FD99052A33ULL, (u64bit) 0x8736E641DB0F7B76ULL, (u64bit) 0x15227725418E5307ULL, + (u64bit) 0xE25F7F46162EB2FAULL, (u64bit) 0x48A8B2126C13D9FEULL, (u64bit) 0xAFDC541792E76EEAULL, + (u64bit) 0x03D912BFC6D1898FULL, (u64bit) 0x31B1AAFA1B83F51BULL, (u64bit) 0xF1AC2796E42AB7D9ULL, + (u64bit) 0x40A3A7D7FCD2EBACULL, (u64bit) 0x1056136D0AFBBCC5ULL, (u64bit) 0x7889E1DD9A6D0C85ULL, + (u64bit) 0xD33525782A7974AAULL, (u64bit) 0xA7E25D09078AC09BULL, (u64bit) 0xBD4138B3EAC6EDD0ULL, + (u64bit) 0x920ABFBE71EB9E70ULL, (u64bit) 0xA2A5D0F54FC2625CULL, (u64bit) 0xC054E36B0B1290A3ULL, + (u64bit) 0xF6DD59FF62FE932BULL, (u64bit) 0x3537354511A8AC7DULL, (u64bit) 0xCA845E9172FADCD4ULL, + (u64bit) 0x84F82B60329D20DCULL, (u64bit) 0x79C62CE1CD672F18ULL, (u64bit) 0x8B09A2ADD124642CULL, + (u64bit) 0xD0C1E96A19D9E726ULL, (u64bit) 0x5A786A9B4BA9500CULL, (u64bit) 0x0E020336634C43F3ULL, + (u64bit) 0xC17B474AEB66D822ULL, (u64bit) 0x6A731AE3EC9BAAC2ULL, (u64bit) 0x8226667AE0840258ULL, + (u64bit) 0x67D4567691CAECA5ULL, (u64bit) 0x1D94155C4875ADB5ULL, (u64bit) 0x6D00FD985B813FDFULL, + (u64bit) 0x51286EFCB774CD06ULL, (u64bit) 0x5E8834471FA744AFULL, (u64bit) 0xF72CA0AEE761AE2EULL, + (u64bit) 0xBE40E4CDAEE8E09AULL, (u64bit) 0xE9970BBB5118F665ULL, (u64bit) 0x726E4BEB33DF1964ULL, + (u64bit) 0x703B000729199762ULL, (u64bit) 0x4631D816F5EF30A7ULL, (u64bit) 0xB880B5B51504A6BEULL, + (u64bit) 0x641793C37ED84B6CULL, (u64bit) 0x7B21ED77F6E97D96ULL, (u64bit) 0x776306312EF96B73ULL, + (u64bit) 0xAE528948E86FF3F4ULL, (u64bit) 0x53DBD7F286A3F8F8ULL, (u64bit) 0x16CADCE74CFC1063ULL, + (u64bit) 0x005C19BDFA52C6DDULL, (u64bit) 0x68868F5D64D46AD3ULL, (u64bit) 0x3A9D512CCF1E186AULL, + (u64bit) 0x367E62C2385660AEULL, (u64bit) 0xE359E7EA77DCB1D7ULL, (u64bit) 0x526C0773749ABE6EULL, + (u64bit) 0x735AE5F9D09F734BULL, (u64bit) 0x493FC7CC8A558BA8ULL, (u64bit) 0xB0B9C1533041AB45ULL, + (u64bit) 0x321958BA470A59BDULL, (u64bit) 0x852DB00B5F46C393ULL, (u64bit) 0x91209B2BD336B0E5ULL, + (u64bit) 0x6E604F7D659EF19FULL, (u64bit) 0xB99A8AE2782CCB24ULL, (u64bit) 0xCCF52AB6C814C4C7ULL, + (u64bit) 0x4727D9AFBE11727BULL, (u64bit) 0x7E950D0C0121B34DULL, (u64bit) 0x756F435670AD471FULL, + (u64bit) 0xF5ADD442615A6849ULL, (u64bit) 0x4E87E09980B9957AULL, (u64bit) 0x2ACFA1DF50AEE355ULL, + (u64bit) 0xD898263AFD2FD556ULL, (u64bit) 0xC8F4924DD80C8FD6ULL, (u64bit) 0xCF99CA3D754A173AULL, + (u64bit) 0xFE477BACAF91BF3CULL, (u64bit) 0xED5371F6D690C12DULL, (u64bit) 0x831A5C285E687094ULL, + (u64bit) 0xC5D3C90A3708A0A4ULL, (u64bit) 0x0F7F903717D06580ULL, (u64bit) 0x19F9BB13B8FDF27FULL, + (u64bit) 0xB1BD6F1B4D502843ULL, (u64bit) 0x1C761BA38FFF4012ULL, (u64bit) 0x0D1530C4E2E21F3BULL, + (u64bit) 0x8943CE69A7372C8AULL, (u64bit) 0xE5184E11FEB5CE66ULL, (u64bit) 0x618BDB80BD736621ULL, + (u64bit) 0x7D29BAD68B574D0BULL, (u64bit) 0x81BB613E25E6FE5BULL, (u64bit) 0x071C9C10BC07913FULL, + (u64bit) 0xC7BEEB7909AC2D97ULL, (u64bit) 0xC3E58D353BC5D757ULL, (u64bit) 0xEB017892F38F61E8ULL, + (u64bit) 0xD4EFFB9C9B1CC21AULL, (u64bit) 0x99727D26F494F7ABULL, (u64bit) 0xA3E063A2956B3E03ULL, + (u64bit) 0x9D4A8B9A4AA09C30ULL, (u64bit) 0x3F6AB7D500090FB4ULL, (u64bit) 0x9CC0F2A057268AC0ULL, + (u64bit) 0x3DEE9D2DEDBF42D1ULL, (u64bit) 0x330F49C87960A972ULL, (u64bit) 0xC6B2720287421B41ULL, + (u64bit) 0x0AC59EC07C00369CULL, (u64bit) 0xEF4EAC49CB353425ULL, (u64bit) 0xF450244EEF0129D8ULL, + (u64bit) 0x8ACC46E5CAF4DEB6ULL, (u64bit) 0x2FFEAB63989263F7ULL, (u64bit) 0x8F7CB9FE5D7A4578ULL, + (u64bit) 0x5BD8F7644E634635ULL, (u64bit) 0x427A7315BF2DC900ULL, (u64bit) 0x17D0C4AA2125261CULL, + (u64bit) 0x3992486C93518E50ULL, (u64bit) 0xB4CBFEE0A2D7D4C3ULL, (u64bit) 0x7C75D6202C5DDD8DULL, + (u64bit) 0xDBC295D8E35B6C61ULL, (u64bit) 0x60B369D302032B19ULL, (u64bit) 0xCE42685FDCE44132ULL, + (u64bit) 0x06F3DDB9DDF65610ULL, (u64bit) 0x8EA4D21DB5E148F0ULL, (u64bit) 0x20B0FCE62FCD496FULL, + (u64bit) 0x2C1B912358B0EE31ULL, (u64bit) 0xB28317B818F5A308ULL, (u64bit) 0xA89C1E189CA6D2CFULL, + (u64bit) 0x0C6B18576AAADBC8ULL, (u64bit) 0xB65DEAA91299FAE3ULL, (u64bit) 0xFB2B794B7F1027E7ULL, + (u64bit) 0x04E4317F443B5BEBULL, (u64bit) 0x4B852D325939D0A6ULL, (u64bit) 0xD5AE6BEEFB207FFCULL, + (u64bit) 0x309682B281C7D374ULL, (u64bit) 0xBAE309A194C3B475ULL, (u64bit) 0x8CC3F97B13B49F05ULL, + (u64bit) 0x98A9422FF8293967ULL, (u64bit) 0x244B16B01076FF7CULL, (u64bit) 0xF8BF571C663D67EEULL, + (u64bit) 0x1F0D6758EEE30DA1ULL, (u64bit) 0xC9B611D97ADEB9B7ULL, (u64bit) 0xB7AFD5887B6C57A2ULL, + (u64bit) 0x6290AE846B984FE1ULL, (u64bit) 0x94DF4CDEACC1A5FDULL, (u64bit) 0x058A5BD1C5483AFFULL, + (u64bit) 0x63166CC142BA3C37ULL, (u64bit) 0x8DB8526EB2F76F40ULL, (u64bit) 0xE10880036F0D6D4EULL, + (u64bit) 0x9E0523C9971D311DULL, (u64bit) 0x45EC2824CC7CD691ULL, (u64bit) 0x575B8359E62382C9ULL, + (u64bit) 0xFA9E400DC4889995ULL, (u64bit) 0xD1823ECB45721568ULL, (u64bit) 0xDAFD983B8206082FULL, + (u64bit) 0xAA7D29082386A8CBULL, (u64bit) 0x269FCD4403B87588ULL, (u64bit) 0x1B91F5F728BDD1E0ULL, + (u64bit) 0xE4669F39040201F6ULL, (u64bit) 0x7A1D7C218CF04ADEULL, (u64bit) 0x65623C29D79CE5CEULL, + (u64bit) 0x2368449096C00BB1ULL, (u64bit) 0xAB9BF1879DA503BAULL, (u64bit) 0xBC23ECB1A458058EULL, + (u64bit) 0x9A58DF01BB401ECCULL, (u64bit) 0xA070E868A85F143DULL, (u64bit) 0x4FF188307DF2239EULL, + (u64bit) 0x14D565B41A641183ULL, (u64bit) 0xEE13337452701602ULL, (u64bit) 0x950E3DCF3F285E09ULL, + (u64bit) 0x59930254B9C80953ULL, (u64bit) 0x3BF299408930DA6DULL, (u64bit) 0xA955943F53691387ULL, + (u64bit) 0xA15EDECAA9CB8784ULL, (u64bit) 0x29142127352BE9A0ULL, (u64bit) 0x76F0371FFF4E7AFBULL, + (u64bit) 0x0239F450274F2228ULL, (u64bit) 0xBB073AF01D5E868BULL, (u64bit) 0xBFC80571C10E96C1ULL, + (u64bit) 0xD267088568222E23ULL, (u64bit) 0x9671A3D48E80B5B0ULL, (u64bit) 0x55B5D38AE193BB81ULL, + (u64bit) 0x693AE2D0A18B04B8ULL, (u64bit) 0x5C48B4ECADD5335FULL, (u64bit) 0xFD743B194916A1CAULL, + (u64bit) 0x2577018134BE98C4ULL, (u64bit) 0xE77987E83C54A4ADULL, (u64bit) 0x28E11014DA33E1B9ULL, + (u64bit) 0x270CC59E226AA213ULL, (u64bit) 0x71495F756D1A5F60ULL, (u64bit) 0x9BE853FB60AFEF77ULL, + (u64bit) 0xADC786A7F7443DBFULL, (u64bit) 0x0904456173B29A82ULL, (u64bit) 0x58BC7A66C232BD5EULL, + (u64bit) 0xF306558C673AC8B2ULL, (u64bit) 0x41F639C6B6C9772AULL, (u64bit) 0x216DEFE99FDA35DAULL, + (u64bit) 0x11640CC71C7BE615ULL, (u64bit) 0x93C43694565C5527ULL, (u64bit) 0xEA038E6246777839ULL, + (u64bit) 0xF9ABF3CE5A3E2469ULL, (u64bit) 0x741E768D0FD312D2ULL, (u64bit) 0x0144B883CED652C6ULL, + (u64bit) 0xC20B5A5BA33F8552ULL, (u64bit) 0x1AE69633C3435A9DULL, (u64bit) 0x97A28CA4088CFDECULL, + (u64bit) 0x8824A43C1E96F420ULL, (u64bit) 0x37612FA66EEEA746ULL, (u64bit) 0x6B4CB165F9CF0E5AULL, + (u64bit) 0x43AA1C06A0ABFB4AULL, (u64bit) 0x7F4DC26FF162796BULL, (u64bit) 0x6CBACC8E54ED9B0FULL, + (u64bit) 0xA6B7FFEFD2BB253EULL, (u64bit) 0x2E25BC95B0A29D4FULL, (u64bit) 0x86D6A58BDEF1388CULL, + (u64bit) 0xDED74AC576B6F054ULL, (u64bit) 0x8030BDBC2B45805DULL, (u64bit) 0x3C81AF70E94D9289ULL, + (u64bit) 0x3EFF6DDA9E3100DBULL, (u64bit) 0xB38DC39FDFCC8847ULL, (u64bit) 0x123885528D17B87EULL, + (u64bit) 0xF2DA0ED240B1B642ULL, (u64bit) 0x44CEFADCD54BF9A9ULL, (u64bit) 0x1312200E433C7EE6ULL, + (u64bit) 0x9FFCC84F3A78C748ULL, (u64bit) 0xF0CD1F72248576BBULL, (u64bit) 0xEC6974053638CFE4ULL, + (u64bit) 0x2BA7B67C0CEC4E4CULL, (u64bit) 0xAC2F4DF3E5CE32EDULL, (u64bit) 0xCB33D14326EA4C11ULL, + (u64bit) 0xA4E9044CC77E58BCULL, (u64bit) 0x5F513293D934FCEFULL, (u64bit) 0x5DC9645506E55444ULL, + (u64bit) 0x50DE418F317DE40AULL, (u64bit) 0x388CB31A69DDE259ULL, (u64bit) 0x2DB4A83455820A86ULL, + (u64bit) 0x9010A91E84711AE9ULL, (u64bit) 0x4DF7F0B7B1498371ULL, (u64bit) 0xD62A2EABC0977179ULL, + (u64bit) 0x22FAC097AA8D5C0EULL }; + +const u64bit Tiger::SBOX3[256] = { + (u64bit) 0xF49FCC2FF1DAF39BULL, (u64bit) 0x487FD5C66FF29281ULL, (u64bit) 0xE8A30667FCDCA83FULL, + (u64bit) 0x2C9B4BE3D2FCCE63ULL, (u64bit) 0xDA3FF74B93FBBBC2ULL, (u64bit) 0x2FA165D2FE70BA66ULL, + (u64bit) 0xA103E279970E93D4ULL, (u64bit) 0xBECDEC77B0E45E71ULL, (u64bit) 0xCFB41E723985E497ULL, + (u64bit) 0xB70AAA025EF75017ULL, (u64bit) 0xD42309F03840B8E0ULL, (u64bit) 0x8EFC1AD035898579ULL, + (u64bit) 0x96C6920BE2B2ABC5ULL, (u64bit) 0x66AF4163375A9172ULL, (u64bit) 0x2174ABDCCA7127FBULL, + (u64bit) 0xB33CCEA64A72FF41ULL, (u64bit) 0xF04A4933083066A5ULL, (u64bit) 0x8D970ACDD7289AF5ULL, + (u64bit) 0x8F96E8E031C8C25EULL, (u64bit) 0xF3FEC02276875D47ULL, (u64bit) 0xEC7BF310056190DDULL, + (u64bit) 0xF5ADB0AEBB0F1491ULL, (u64bit) 0x9B50F8850FD58892ULL, (u64bit) 0x4975488358B74DE8ULL, + (u64bit) 0xA3354FF691531C61ULL, (u64bit) 0x0702BBE481D2C6EEULL, (u64bit) 0x89FB24057DEDED98ULL, + (u64bit) 0xAC3075138596E902ULL, (u64bit) 0x1D2D3580172772EDULL, (u64bit) 0xEB738FC28E6BC30DULL, + (u64bit) 0x5854EF8F63044326ULL, (u64bit) 0x9E5C52325ADD3BBEULL, (u64bit) 0x90AA53CF325C4623ULL, + (u64bit) 0xC1D24D51349DD067ULL, (u64bit) 0x2051CFEEA69EA624ULL, (u64bit) 0x13220F0A862E7E4FULL, + (u64bit) 0xCE39399404E04864ULL, (u64bit) 0xD9C42CA47086FCB7ULL, (u64bit) 0x685AD2238A03E7CCULL, + (u64bit) 0x066484B2AB2FF1DBULL, (u64bit) 0xFE9D5D70EFBF79ECULL, (u64bit) 0x5B13B9DD9C481854ULL, + (u64bit) 0x15F0D475ED1509ADULL, (u64bit) 0x0BEBCD060EC79851ULL, (u64bit) 0xD58C6791183AB7F8ULL, + (u64bit) 0xD1187C5052F3EEE4ULL, (u64bit) 0xC95D1192E54E82FFULL, (u64bit) 0x86EEA14CB9AC6CA2ULL, + (u64bit) 0x3485BEB153677D5DULL, (u64bit) 0xDD191D781F8C492AULL, (u64bit) 0xF60866BAA784EBF9ULL, + (u64bit) 0x518F643BA2D08C74ULL, (u64bit) 0x8852E956E1087C22ULL, (u64bit) 0xA768CB8DC410AE8DULL, + (u64bit) 0x38047726BFEC8E1AULL, (u64bit) 0xA67738B4CD3B45AAULL, (u64bit) 0xAD16691CEC0DDE19ULL, + (u64bit) 0xC6D4319380462E07ULL, (u64bit) 0xC5A5876D0BA61938ULL, (u64bit) 0x16B9FA1FA58FD840ULL, + (u64bit) 0x188AB1173CA74F18ULL, (u64bit) 0xABDA2F98C99C021FULL, (u64bit) 0x3E0580AB134AE816ULL, + (u64bit) 0x5F3B05B773645ABBULL, (u64bit) 0x2501A2BE5575F2F6ULL, (u64bit) 0x1B2F74004E7E8BA9ULL, + (u64bit) 0x1CD7580371E8D953ULL, (u64bit) 0x7F6ED89562764E30ULL, (u64bit) 0xB15926FF596F003DULL, + (u64bit) 0x9F65293DA8C5D6B9ULL, (u64bit) 0x6ECEF04DD690F84CULL, (u64bit) 0x4782275FFF33AF88ULL, + (u64bit) 0xE41433083F820801ULL, (u64bit) 0xFD0DFE409A1AF9B5ULL, (u64bit) 0x4325A3342CDB396BULL, + (u64bit) 0x8AE77E62B301B252ULL, (u64bit) 0xC36F9E9F6655615AULL, (u64bit) 0x85455A2D92D32C09ULL, + (u64bit) 0xF2C7DEA949477485ULL, (u64bit) 0x63CFB4C133A39EBAULL, (u64bit) 0x83B040CC6EBC5462ULL, + (u64bit) 0x3B9454C8FDB326B0ULL, (u64bit) 0x56F56A9E87FFD78CULL, (u64bit) 0x2DC2940D99F42BC6ULL, + (u64bit) 0x98F7DF096B096E2DULL, (u64bit) 0x19A6E01E3AD852BFULL, (u64bit) 0x42A99CCBDBD4B40BULL, + (u64bit) 0xA59998AF45E9C559ULL, (u64bit) 0x366295E807D93186ULL, (u64bit) 0x6B48181BFAA1F773ULL, + (u64bit) 0x1FEC57E2157A0A1DULL, (u64bit) 0x4667446AF6201AD5ULL, (u64bit) 0xE615EBCACFB0F075ULL, + (u64bit) 0xB8F31F4F68290778ULL, (u64bit) 0x22713ED6CE22D11EULL, (u64bit) 0x3057C1A72EC3C93BULL, + (u64bit) 0xCB46ACC37C3F1F2FULL, (u64bit) 0xDBB893FD02AAF50EULL, (u64bit) 0x331FD92E600B9FCFULL, + (u64bit) 0xA498F96148EA3AD6ULL, (u64bit) 0xA8D8426E8B6A83EAULL, (u64bit) 0xA089B274B7735CDCULL, + (u64bit) 0x87F6B3731E524A11ULL, (u64bit) 0x118808E5CBC96749ULL, (u64bit) 0x9906E4C7B19BD394ULL, + (u64bit) 0xAFED7F7E9B24A20CULL, (u64bit) 0x6509EADEEB3644A7ULL, (u64bit) 0x6C1EF1D3E8EF0EDEULL, + (u64bit) 0xB9C97D43E9798FB4ULL, (u64bit) 0xA2F2D784740C28A3ULL, (u64bit) 0x7B8496476197566FULL, + (u64bit) 0x7A5BE3E6B65F069DULL, (u64bit) 0xF96330ED78BE6F10ULL, (u64bit) 0xEEE60DE77A076A15ULL, + (u64bit) 0x2B4BEE4AA08B9BD0ULL, (u64bit) 0x6A56A63EC7B8894EULL, (u64bit) 0x02121359BA34FEF4ULL, + (u64bit) 0x4CBF99F8283703FCULL, (u64bit) 0x398071350CAF30C8ULL, (u64bit) 0xD0A77A89F017687AULL, + (u64bit) 0xF1C1A9EB9E423569ULL, (u64bit) 0x8C7976282DEE8199ULL, (u64bit) 0x5D1737A5DD1F7ABDULL, + (u64bit) 0x4F53433C09A9FA80ULL, (u64bit) 0xFA8B0C53DF7CA1D9ULL, (u64bit) 0x3FD9DCBC886CCB77ULL, + (u64bit) 0xC040917CA91B4720ULL, (u64bit) 0x7DD00142F9D1DCDFULL, (u64bit) 0x8476FC1D4F387B58ULL, + (u64bit) 0x23F8E7C5F3316503ULL, (u64bit) 0x032A2244E7E37339ULL, (u64bit) 0x5C87A5D750F5A74BULL, + (u64bit) 0x082B4CC43698992EULL, (u64bit) 0xDF917BECB858F63CULL, (u64bit) 0x3270B8FC5BF86DDAULL, + (u64bit) 0x10AE72BB29B5DD76ULL, (u64bit) 0x576AC94E7700362BULL, (u64bit) 0x1AD112DAC61EFB8FULL, + (u64bit) 0x691BC30EC5FAA427ULL, (u64bit) 0xFF246311CC327143ULL, (u64bit) 0x3142368E30E53206ULL, + (u64bit) 0x71380E31E02CA396ULL, (u64bit) 0x958D5C960AAD76F1ULL, (u64bit) 0xF8D6F430C16DA536ULL, + (u64bit) 0xC8FFD13F1BE7E1D2ULL, (u64bit) 0x7578AE66004DDBE1ULL, (u64bit) 0x05833F01067BE646ULL, + (u64bit) 0xBB34B5AD3BFE586DULL, (u64bit) 0x095F34C9A12B97F0ULL, (u64bit) 0x247AB64525D60CA8ULL, + (u64bit) 0xDCDBC6F3017477D1ULL, (u64bit) 0x4A2E14D4DECAD24DULL, (u64bit) 0xBDB5E6D9BE0A1EEBULL, + (u64bit) 0x2A7E70F7794301ABULL, (u64bit) 0xDEF42D8A270540FDULL, (u64bit) 0x01078EC0A34C22C1ULL, + (u64bit) 0xE5DE511AF4C16387ULL, (u64bit) 0x7EBB3A52BD9A330AULL, (u64bit) 0x77697857AA7D6435ULL, + (u64bit) 0x004E831603AE4C32ULL, (u64bit) 0xE7A21020AD78E312ULL, (u64bit) 0x9D41A70C6AB420F2ULL, + (u64bit) 0x28E06C18EA1141E6ULL, (u64bit) 0xD2B28CBD984F6B28ULL, (u64bit) 0x26B75F6C446E9D83ULL, + (u64bit) 0xBA47568C4D418D7FULL, (u64bit) 0xD80BADBFE6183D8EULL, (u64bit) 0x0E206D7F5F166044ULL, + (u64bit) 0xE258A43911CBCA3EULL, (u64bit) 0x723A1746B21DC0BCULL, (u64bit) 0xC7CAA854F5D7CDD3ULL, + (u64bit) 0x7CAC32883D261D9CULL, (u64bit) 0x7690C26423BA942CULL, (u64bit) 0x17E55524478042B8ULL, + (u64bit) 0xE0BE477656A2389FULL, (u64bit) 0x4D289B5E67AB2DA0ULL, (u64bit) 0x44862B9C8FBBFD31ULL, + (u64bit) 0xB47CC8049D141365ULL, (u64bit) 0x822C1B362B91C793ULL, (u64bit) 0x4EB14655FB13DFD8ULL, + (u64bit) 0x1ECBBA0714E2A97BULL, (u64bit) 0x6143459D5CDE5F14ULL, (u64bit) 0x53A8FBF1D5F0AC89ULL, + (u64bit) 0x97EA04D81C5E5B00ULL, (u64bit) 0x622181A8D4FDB3F3ULL, (u64bit) 0xE9BCD341572A1208ULL, + (u64bit) 0x1411258643CCE58AULL, (u64bit) 0x9144C5FEA4C6E0A4ULL, (u64bit) 0x0D33D06565CF620FULL, + (u64bit) 0x54A48D489F219CA1ULL, (u64bit) 0xC43E5EAC6D63C821ULL, (u64bit) 0xA9728B3A72770DAFULL, + (u64bit) 0xD7934E7B20DF87EFULL, (u64bit) 0xE35503B61A3E86E5ULL, (u64bit) 0xCAE321FBC819D504ULL, + (u64bit) 0x129A50B3AC60BFA6ULL, (u64bit) 0xCD5E68EA7E9FB6C3ULL, (u64bit) 0xB01C90199483B1C7ULL, + (u64bit) 0x3DE93CD5C295376CULL, (u64bit) 0xAED52EDF2AB9AD13ULL, (u64bit) 0x2E60F512C0A07884ULL, + (u64bit) 0xBC3D86A3E36210C9ULL, (u64bit) 0x35269D9B163951CEULL, (u64bit) 0x0C7D6E2AD0CDB5FAULL, + (u64bit) 0x59E86297D87F5733ULL, (u64bit) 0x298EF221898DB0E7ULL, (u64bit) 0x55000029D1A5AA7EULL, + (u64bit) 0x8BC08AE1B5061B45ULL, (u64bit) 0xC2C31C2B6C92703AULL, (u64bit) 0x94CC596BAF25EF42ULL, + (u64bit) 0x0A1D73DB22540456ULL, (u64bit) 0x04B6A0F9D9C4179AULL, (u64bit) 0xEFFDAFA2AE3D3C60ULL, + (u64bit) 0xF7C8075BB49496C4ULL, (u64bit) 0x9CC5C7141D1CD4E3ULL, (u64bit) 0x78BD1638218E5534ULL, + (u64bit) 0xB2F11568F850246AULL, (u64bit) 0xEDFABCFA9502BC29ULL, (u64bit) 0x796CE5F2DA23051BULL, + (u64bit) 0xAAE128B0DC93537CULL, (u64bit) 0x3A493DA0EE4B29AEULL, (u64bit) 0xB5DF6B2C416895D7ULL, + (u64bit) 0xFCABBD25122D7F37ULL, (u64bit) 0x70810B58105DC4B1ULL, (u64bit) 0xE10FDD37F7882A90ULL, + (u64bit) 0x524DCAB5518A3F5CULL, (u64bit) 0x3C9E85878451255BULL, (u64bit) 0x4029828119BD34E2ULL, + (u64bit) 0x74A05B6F5D3CECCBULL, (u64bit) 0xB610021542E13ECAULL, (u64bit) 0x0FF979D12F59E2ACULL, + (u64bit) 0x6037DA27E4F9CC50ULL, (u64bit) 0x5E92975A0DF1847DULL, (u64bit) 0xD66DE190D3E623FEULL, + (u64bit) 0x5032D6B87B568048ULL, (u64bit) 0x9A36B7CE8235216EULL, (u64bit) 0x80272A7A24F64B4AULL, + (u64bit) 0x93EFED8B8C6916F7ULL, (u64bit) 0x37DDBFF44CCE1555ULL, (u64bit) 0x4B95DB5D4B99BD25ULL, + (u64bit) 0x92D3FDA169812FC0ULL, (u64bit) 0xFB1A4A9A90660BB6ULL, (u64bit) 0x730C196946A4B9B2ULL, + (u64bit) 0x81E289AA7F49DA68ULL, (u64bit) 0x64669A0F83B1A05FULL, (u64bit) 0x27B3FF7D9644F48BULL, + (u64bit) 0xCC6B615C8DB675B3ULL, (u64bit) 0x674F20B9BCEBBE95ULL, (u64bit) 0x6F31238275655982ULL, + (u64bit) 0x5AE488713E45CF05ULL, (u64bit) 0xBF619F9954C21157ULL, (u64bit) 0xEABAC46040A8EAE9ULL, + (u64bit) 0x454C6FE9F2C0C1CDULL, (u64bit) 0x419CF6496412691CULL, (u64bit) 0xD3DC3BEF265B0F70ULL, + (u64bit) 0x6D0E60F5C3578A9EULL }; + +const u64bit Tiger::SBOX4[256] = { + (u64bit) 0x5B0E608526323C55ULL, (u64bit) 0x1A46C1A9FA1B59F5ULL, (u64bit) 0xA9E245A17C4C8FFAULL, + (u64bit) 0x65CA5159DB2955D7ULL, (u64bit) 0x05DB0A76CE35AFC2ULL, (u64bit) 0x81EAC77EA9113D45ULL, + (u64bit) 0x528EF88AB6AC0A0DULL, (u64bit) 0xA09EA253597BE3FFULL, (u64bit) 0x430DDFB3AC48CD56ULL, + (u64bit) 0xC4B3A67AF45CE46FULL, (u64bit) 0x4ECECFD8FBE2D05EULL, (u64bit) 0x3EF56F10B39935F0ULL, + (u64bit) 0x0B22D6829CD619C6ULL, (u64bit) 0x17FD460A74DF2069ULL, (u64bit) 0x6CF8CC8E8510ED40ULL, + (u64bit) 0xD6C824BF3A6ECAA7ULL, (u64bit) 0x61243D581A817049ULL, (u64bit) 0x048BACB6BBC163A2ULL, + (u64bit) 0xD9A38AC27D44CC32ULL, (u64bit) 0x7FDDFF5BAAF410ABULL, (u64bit) 0xAD6D495AA804824BULL, + (u64bit) 0xE1A6A74F2D8C9F94ULL, (u64bit) 0xD4F7851235DEE8E3ULL, (u64bit) 0xFD4B7F886540D893ULL, + (u64bit) 0x247C20042AA4BFDAULL, (u64bit) 0x096EA1C517D1327CULL, (u64bit) 0xD56966B4361A6685ULL, + (u64bit) 0x277DA5C31221057DULL, (u64bit) 0x94D59893A43ACFF7ULL, (u64bit) 0x64F0C51CCDC02281ULL, + (u64bit) 0x3D33BCC4FF6189DBULL, (u64bit) 0xE005CB184CE66AF1ULL, (u64bit) 0xFF5CCD1D1DB99BEAULL, + (u64bit) 0xB0B854A7FE42980FULL, (u64bit) 0x7BD46A6A718D4B9FULL, (u64bit) 0xD10FA8CC22A5FD8CULL, + (u64bit) 0xD31484952BE4BD31ULL, (u64bit) 0xC7FA975FCB243847ULL, (u64bit) 0x4886ED1E5846C407ULL, + (u64bit) 0x28CDDB791EB70B04ULL, (u64bit) 0xC2B00BE2F573417FULL, (u64bit) 0x5C9590452180F877ULL, + (u64bit) 0x7A6BDDFFF370EB00ULL, (u64bit) 0xCE509E38D6D9D6A4ULL, (u64bit) 0xEBEB0F00647FA702ULL, + (u64bit) 0x1DCC06CF76606F06ULL, (u64bit) 0xE4D9F28BA286FF0AULL, (u64bit) 0xD85A305DC918C262ULL, + (u64bit) 0x475B1D8732225F54ULL, (u64bit) 0x2D4FB51668CCB5FEULL, (u64bit) 0xA679B9D9D72BBA20ULL, + (u64bit) 0x53841C0D912D43A5ULL, (u64bit) 0x3B7EAA48BF12A4E8ULL, (u64bit) 0x781E0E47F22F1DDFULL, + (u64bit) 0xEFF20CE60AB50973ULL, (u64bit) 0x20D261D19DFFB742ULL, (u64bit) 0x16A12B03062A2E39ULL, + (u64bit) 0x1960EB2239650495ULL, (u64bit) 0x251C16FED50EB8B8ULL, (u64bit) 0x9AC0C330F826016EULL, + (u64bit) 0xED152665953E7671ULL, (u64bit) 0x02D63194A6369570ULL, (u64bit) 0x5074F08394B1C987ULL, + (u64bit) 0x70BA598C90B25CE1ULL, (u64bit) 0x794A15810B9742F6ULL, (u64bit) 0x0D5925E9FCAF8C6CULL, + (u64bit) 0x3067716CD868744EULL, (u64bit) 0x910AB077E8D7731BULL, (u64bit) 0x6A61BBDB5AC42F61ULL, + (u64bit) 0x93513EFBF0851567ULL, (u64bit) 0xF494724B9E83E9D5ULL, (u64bit) 0xE887E1985C09648DULL, + (u64bit) 0x34B1D3C675370CFDULL, (u64bit) 0xDC35E433BC0D255DULL, (u64bit) 0xD0AAB84234131BE0ULL, + (u64bit) 0x08042A50B48B7EAFULL, (u64bit) 0x9997C4EE44A3AB35ULL, (u64bit) 0x829A7B49201799D0ULL, + (u64bit) 0x263B8307B7C54441ULL, (u64bit) 0x752F95F4FD6A6CA6ULL, (u64bit) 0x927217402C08C6E5ULL, + (u64bit) 0x2A8AB754A795D9EEULL, (u64bit) 0xA442F7552F72943DULL, (u64bit) 0x2C31334E19781208ULL, + (u64bit) 0x4FA98D7CEAEE6291ULL, (u64bit) 0x55C3862F665DB309ULL, (u64bit) 0xBD0610175D53B1F3ULL, + (u64bit) 0x46FE6CB840413F27ULL, (u64bit) 0x3FE03792DF0CFA59ULL, (u64bit) 0xCFE700372EB85E8FULL, + (u64bit) 0xA7BE29E7ADBCE118ULL, (u64bit) 0xE544EE5CDE8431DDULL, (u64bit) 0x8A781B1B41F1873EULL, + (u64bit) 0xA5C94C78A0D2F0E7ULL, (u64bit) 0x39412E2877B60728ULL, (u64bit) 0xA1265EF3AFC9A62CULL, + (u64bit) 0xBCC2770C6A2506C5ULL, (u64bit) 0x3AB66DD5DCE1CE12ULL, (u64bit) 0xE65499D04A675B37ULL, + (u64bit) 0x7D8F523481BFD216ULL, (u64bit) 0x0F6F64FCEC15F389ULL, (u64bit) 0x74EFBE618B5B13C8ULL, + (u64bit) 0xACDC82B714273E1DULL, (u64bit) 0xDD40BFE003199D17ULL, (u64bit) 0x37E99257E7E061F8ULL, + (u64bit) 0xFA52626904775AAAULL, (u64bit) 0x8BBBF63A463D56F9ULL, (u64bit) 0xF0013F1543A26E64ULL, + (u64bit) 0xA8307E9F879EC898ULL, (u64bit) 0xCC4C27A4150177CCULL, (u64bit) 0x1B432F2CCA1D3348ULL, + (u64bit) 0xDE1D1F8F9F6FA013ULL, (u64bit) 0x606602A047A7DDD6ULL, (u64bit) 0xD237AB64CC1CB2C7ULL, + (u64bit) 0x9B938E7225FCD1D3ULL, (u64bit) 0xEC4E03708E0FF476ULL, (u64bit) 0xFEB2FBDA3D03C12DULL, + (u64bit) 0xAE0BCED2EE43889AULL, (u64bit) 0x22CB8923EBFB4F43ULL, (u64bit) 0x69360D013CF7396DULL, + (u64bit) 0x855E3602D2D4E022ULL, (u64bit) 0x073805BAD01F784CULL, (u64bit) 0x33E17A133852F546ULL, + (u64bit) 0xDF4874058AC7B638ULL, (u64bit) 0xBA92B29C678AA14AULL, (u64bit) 0x0CE89FC76CFAADCDULL, + (u64bit) 0x5F9D4E0908339E34ULL, (u64bit) 0xF1AFE9291F5923B9ULL, (u64bit) 0x6E3480F60F4A265FULL, + (u64bit) 0xEEBF3A2AB29B841CULL, (u64bit) 0xE21938A88F91B4ADULL, (u64bit) 0x57DFEFF845C6D3C3ULL, + (u64bit) 0x2F006B0BF62CAAF2ULL, (u64bit) 0x62F479EF6F75EE78ULL, (u64bit) 0x11A55AD41C8916A9ULL, + (u64bit) 0xF229D29084FED453ULL, (u64bit) 0x42F1C27B16B000E6ULL, (u64bit) 0x2B1F76749823C074ULL, + (u64bit) 0x4B76ECA3C2745360ULL, (u64bit) 0x8C98F463B91691BDULL, (u64bit) 0x14BCC93CF1ADE66AULL, + (u64bit) 0x8885213E6D458397ULL, (u64bit) 0x8E177DF0274D4711ULL, (u64bit) 0xB49B73B5503F2951ULL, + (u64bit) 0x10168168C3F96B6BULL, (u64bit) 0x0E3D963B63CAB0AEULL, (u64bit) 0x8DFC4B5655A1DB14ULL, + (u64bit) 0xF789F1356E14DE5CULL, (u64bit) 0x683E68AF4E51DAC1ULL, (u64bit) 0xC9A84F9D8D4B0FD9ULL, + (u64bit) 0x3691E03F52A0F9D1ULL, (u64bit) 0x5ED86E46E1878E80ULL, (u64bit) 0x3C711A0E99D07150ULL, + (u64bit) 0x5A0865B20C4E9310ULL, (u64bit) 0x56FBFC1FE4F0682EULL, (u64bit) 0xEA8D5DE3105EDF9BULL, + (u64bit) 0x71ABFDB12379187AULL, (u64bit) 0x2EB99DE1BEE77B9CULL, (u64bit) 0x21ECC0EA33CF4523ULL, + (u64bit) 0x59A4D7521805C7A1ULL, (u64bit) 0x3896F5EB56AE7C72ULL, (u64bit) 0xAA638F3DB18F75DCULL, + (u64bit) 0x9F39358DABE9808EULL, (u64bit) 0xB7DEFA91C00B72ACULL, (u64bit) 0x6B5541FD62492D92ULL, + (u64bit) 0x6DC6DEE8F92E4D5BULL, (u64bit) 0x353F57ABC4BEEA7EULL, (u64bit) 0x735769D6DA5690CEULL, + (u64bit) 0x0A234AA642391484ULL, (u64bit) 0xF6F9508028F80D9DULL, (u64bit) 0xB8E319A27AB3F215ULL, + (u64bit) 0x31AD9C1151341A4DULL, (u64bit) 0x773C22A57BEF5805ULL, (u64bit) 0x45C7561A07968633ULL, + (u64bit) 0xF913DA9E249DBE36ULL, (u64bit) 0xDA652D9B78A64C68ULL, (u64bit) 0x4C27A97F3BC334EFULL, + (u64bit) 0x76621220E66B17F4ULL, (u64bit) 0x967743899ACD7D0BULL, (u64bit) 0xF3EE5BCAE0ED6782ULL, + (u64bit) 0x409F753600C879FCULL, (u64bit) 0x06D09A39B5926DB6ULL, (u64bit) 0x6F83AEB0317AC588ULL, + (u64bit) 0x01E6CA4A86381F21ULL, (u64bit) 0x66FF3462D19F3025ULL, (u64bit) 0x72207C24DDFD3BFBULL, + (u64bit) 0x4AF6B6D3E2ECE2EBULL, (u64bit) 0x9C994DBEC7EA08DEULL, (u64bit) 0x49ACE597B09A8BC4ULL, + (u64bit) 0xB38C4766CF0797BAULL, (u64bit) 0x131B9373C57C2A75ULL, (u64bit) 0xB1822CCE61931E58ULL, + (u64bit) 0x9D7555B909BA1C0CULL, (u64bit) 0x127FAFDD937D11D2ULL, (u64bit) 0x29DA3BADC66D92E4ULL, + (u64bit) 0xA2C1D57154C2ECBCULL, (u64bit) 0x58C5134D82F6FE24ULL, (u64bit) 0x1C3AE3515B62274FULL, + (u64bit) 0xE907C82E01CB8126ULL, (u64bit) 0xF8ED091913E37FCBULL, (u64bit) 0x3249D8F9C80046C9ULL, + (u64bit) 0x80CF9BEDE388FB63ULL, (u64bit) 0x1881539A116CF19EULL, (u64bit) 0x5103F3F76BD52457ULL, + (u64bit) 0x15B7E6F5AE47F7A8ULL, (u64bit) 0xDBD7C6DED47E9CCFULL, (u64bit) 0x44E55C410228BB1AULL, + (u64bit) 0xB647D4255EDB4E99ULL, (u64bit) 0x5D11882BB8AAFC30ULL, (u64bit) 0xF5098BBB29D3212AULL, + (u64bit) 0x8FB5EA14E90296B3ULL, (u64bit) 0x677B942157DD025AULL, (u64bit) 0xFB58E7C0A390ACB5ULL, + (u64bit) 0x89D3674C83BD4A01ULL, (u64bit) 0x9E2DA4DF4BF3B93BULL, (u64bit) 0xFCC41E328CAB4829ULL, + (u64bit) 0x03F38C96BA582C52ULL, (u64bit) 0xCAD1BDBD7FD85DB2ULL, (u64bit) 0xBBB442C16082AE83ULL, + (u64bit) 0xB95FE86BA5DA9AB0ULL, (u64bit) 0xB22E04673771A93FULL, (u64bit) 0x845358C9493152D8ULL, + (u64bit) 0xBE2A488697B4541EULL, (u64bit) 0x95A2DC2DD38E6966ULL, (u64bit) 0xC02C11AC923C852BULL, + (u64bit) 0x2388B1990DF2A87BULL, (u64bit) 0x7C8008FA1B4F37BEULL, (u64bit) 0x1F70D0C84D54E503ULL, + (u64bit) 0x5490ADEC7ECE57D4ULL, (u64bit) 0x002B3C27D9063A3AULL, (u64bit) 0x7EAEA3848030A2BFULL, + (u64bit) 0xC602326DED2003C0ULL, (u64bit) 0x83A7287D69A94086ULL, (u64bit) 0xC57A5FCB30F57A8AULL, + (u64bit) 0xB56844E479EBE779ULL, (u64bit) 0xA373B40F05DCBCE9ULL, (u64bit) 0xD71A786E88570EE2ULL, + (u64bit) 0x879CBACDBDE8F6A0ULL, (u64bit) 0x976AD1BCC164A32FULL, (u64bit) 0xAB21E25E9666D78BULL, + (u64bit) 0x901063AAE5E5C33CULL, (u64bit) 0x9818B34448698D90ULL, (u64bit) 0xE36487AE3E1E8ABBULL, + (u64bit) 0xAFBDF931893BDCB4ULL, (u64bit) 0x6345A0DC5FBBD519ULL, (u64bit) 0x8628FE269B9465CAULL, + (u64bit) 0x1E5D01603F9C51ECULL, (u64bit) 0x4DE44006A15049B7ULL, (u64bit) 0xBF6C70E5F776CBB1ULL, + (u64bit) 0x411218F2EF552BEDULL, (u64bit) 0xCB0C0708705A36A3ULL, (u64bit) 0xE74D14754F986044ULL, + (u64bit) 0xCD56D9430EA8280EULL, (u64bit) 0xC12591D7535F5065ULL, (u64bit) 0xC83223F1720AEF96ULL, + (u64bit) 0xC3A0396F7363A51FULL }; + +} diff --git a/botan/src/hash/tiger/tiger.cpp b/botan/src/hash/tiger/tiger.cpp new file mode 100644 index 0000000..08af9c7 --- /dev/null +++ b/botan/src/hash/tiger/tiger.cpp @@ -0,0 +1,164 @@ +/* +* Tiger +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/tiger.h> +#include <botan/exceptn.h> +#include <botan/loadstor.h> +#include <botan/parsing.h> + +namespace Botan { + +/* +* Tiger Compression Function +*/ +void Tiger::compress_n(const byte input[], u32bit blocks) + { + u64bit A = digest[0], B = digest[1], C = digest[2]; + + for(u32bit i = 0; i != blocks; ++i) + { + for(u32bit j = 0; j != 8; ++j) + X[j] = load_le<u64bit>(input, j); + input += HASH_BLOCK_SIZE; + + pass(A, B, C, X, 5); mix(X); + pass(C, A, B, X, 7); mix(X); + pass(B, C, A, X, 9); + + for(u32bit j = 3; j != PASS; ++j) + { + mix(X); + pass(A, B, C, X, 9); + u64bit T = A; A = C; C = B; B = T; + } + + A = (digest[0] ^= A); + B = digest[1] = B - digest[1]; + C = (digest[2] += C); + } + } + +/* +* Copy out the digest +*/ +void Tiger::copy_out(byte output[]) + { + for(u32bit j = 0; j != OUTPUT_LENGTH; ++j) + output[j] = get_byte(7 - (j % 8), digest[j/8]); + } + +/* +* Tiger Pass +*/ +void Tiger::pass(u64bit& A, u64bit& B, u64bit& C, u64bit X[8], byte mul) + { + C ^= X[0]; + A -= SBOX1[get_byte(7, C)] ^ SBOX2[get_byte(5, C)] ^ + SBOX3[get_byte(3, C)] ^ SBOX4[get_byte(1, C)]; + B += SBOX1[get_byte(0, C)] ^ SBOX2[get_byte(2, C)] ^ + SBOX3[get_byte(4, C)] ^ SBOX4[get_byte(6, C)]; + B *= mul; + + A ^= X[1]; + B -= SBOX1[get_byte(7, A)] ^ SBOX2[get_byte(5, A)] ^ + SBOX3[get_byte(3, A)] ^ SBOX4[get_byte(1, A)]; + C += SBOX1[get_byte(0, A)] ^ SBOX2[get_byte(2, A)] ^ + SBOX3[get_byte(4, A)] ^ SBOX4[get_byte(6, A)]; + C *= mul; + + B ^= X[2]; + C -= SBOX1[get_byte(7, B)] ^ SBOX2[get_byte(5, B)] ^ + SBOX3[get_byte(3, B)] ^ SBOX4[get_byte(1, B)]; + A += SBOX1[get_byte(0, B)] ^ SBOX2[get_byte(2, B)] ^ + SBOX3[get_byte(4, B)] ^ SBOX4[get_byte(6, B)]; + A *= mul; + + C ^= X[3]; + A -= SBOX1[get_byte(7, C)] ^ SBOX2[get_byte(5, C)] ^ + SBOX3[get_byte(3, C)] ^ SBOX4[get_byte(1, C)]; + B += SBOX1[get_byte(0, C)] ^ SBOX2[get_byte(2, C)] ^ + SBOX3[get_byte(4, C)] ^ SBOX4[get_byte(6, C)]; + B *= mul; + + A ^= X[4]; + B -= SBOX1[get_byte(7, A)] ^ SBOX2[get_byte(5, A)] ^ + SBOX3[get_byte(3, A)] ^ SBOX4[get_byte(1, A)]; + C += SBOX1[get_byte(0, A)] ^ SBOX2[get_byte(2, A)] ^ + SBOX3[get_byte(4, A)] ^ SBOX4[get_byte(6, A)]; + C *= mul; + + B ^= X[5]; + C -= SBOX1[get_byte(7, B)] ^ SBOX2[get_byte(5, B)] ^ + SBOX3[get_byte(3, B)] ^ SBOX4[get_byte(1, B)]; + A += SBOX1[get_byte(0, B)] ^ SBOX2[get_byte(2, B)] ^ + SBOX3[get_byte(4, B)] ^ SBOX4[get_byte(6, B)]; + A *= mul; + + C ^= X[6]; + A -= SBOX1[get_byte(7, C)] ^ SBOX2[get_byte(5, C)] ^ + SBOX3[get_byte(3, C)] ^ SBOX4[get_byte(1, C)]; + B += SBOX1[get_byte(0, C)] ^ SBOX2[get_byte(2, C)] ^ + SBOX3[get_byte(4, C)] ^ SBOX4[get_byte(6, C)]; + B *= mul; + + A ^= X[7]; + B -= SBOX1[get_byte(7, A)] ^ SBOX2[get_byte(5, A)] ^ + SBOX3[get_byte(3, A)] ^ SBOX4[get_byte(1, A)]; + C += SBOX1[get_byte(0, A)] ^ SBOX2[get_byte(2, A)] ^ + SBOX3[get_byte(4, A)] ^ SBOX4[get_byte(6, A)]; + C *= mul; + } + +/* +* Tiger Mixing Function +*/ +void Tiger::mix(u64bit X[8]) + { + X[0] -= X[7] ^ (u64bit) 0xA5A5A5A5A5A5A5A5ULL; X[1] ^= X[0]; + X[2] += X[1]; X[3] -= X[2] ^ ((~X[1]) << 19); X[4] ^= X[3]; + X[5] += X[4]; X[6] -= X[5] ^ ((~X[4]) >> 23); X[7] ^= X[6]; + X[0] += X[7]; X[1] -= X[0] ^ ((~X[7]) << 19); X[2] ^= X[1]; + X[3] += X[2]; X[4] -= X[3] ^ ((~X[2]) >> 23); X[5] ^= X[4]; + X[6] += X[5]; X[7] -= X[6] ^ (u64bit) 0x0123456789ABCDEFULL; + } + +/* +* Clear memory of sensitive data +*/ +void Tiger::clear() throw() + { + MDx_HashFunction::clear(); + X.clear(); + digest[0] = (u64bit) 0x0123456789ABCDEFULL; + digest[1] = (u64bit) 0xFEDCBA9876543210ULL; + digest[2] = (u64bit) 0xF096A5B4C3B2E187ULL; + } + +/* +* Return the name of this type +*/ +std::string Tiger::name() const + { + return "Tiger(" + to_string(OUTPUT_LENGTH) + "," + to_string(PASS) + ")"; + } + +/* +* Tiger Constructor +*/ +Tiger::Tiger(u32bit hashlen, u32bit pass) : + MDx_HashFunction(hashlen, 64, false, false), PASS(pass) + { + if(OUTPUT_LENGTH != 16 && OUTPUT_LENGTH != 20 && OUTPUT_LENGTH != 24) + throw Invalid_Argument("Tiger: Illegal hash output size: " + + to_string(OUTPUT_LENGTH)); + if(PASS < 3) + throw Invalid_Argument("Tiger: Invalid number of passes: " + + to_string(PASS)); + clear(); + } + +} diff --git a/botan/src/hash/tiger/tiger.h b/botan/src/hash/tiger/tiger.h new file mode 100644 index 0000000..63184a9 --- /dev/null +++ b/botan/src/hash/tiger/tiger.h @@ -0,0 +1,44 @@ +/* +* Tiger +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_TIGER_H__ +#define BOTAN_TIGER_H__ + +#include <botan/mdx_hash.h> + +namespace Botan { + +/* +* Tiger +*/ +class BOTAN_DLL Tiger : public MDx_HashFunction + { + public: + void clear() throw(); + std::string name() const; + HashFunction* clone() const { return new Tiger(OUTPUT_LENGTH); } + Tiger(u32bit = 24, u32bit = 3); + private: + void compress_n(const byte[], u32bit block); + void copy_out(byte[]); + + static void pass(u64bit&, u64bit&, u64bit&, u64bit[8], byte); + static void mix(u64bit[8]); + + static const u64bit SBOX1[256]; + static const u64bit SBOX2[256]; + static const u64bit SBOX3[256]; + static const u64bit SBOX4[256]; + + SecureBuffer<u64bit, 8> X; + SecureBuffer<u64bit, 3> digest; + const u32bit PASS; + }; + +} + +#endif diff --git a/botan/src/hash/whirlpool/info.txt b/botan/src/hash/whirlpool/info.txt new file mode 100644 index 0000000..be55b5c --- /dev/null +++ b/botan/src/hash/whirlpool/info.txt @@ -0,0 +1,15 @@ +realname "Whirlpool" + +define WHIRLPOOL + +load_on auto + +<add> +whrl_tab.cpp +whrlpool.cpp +whrlpool.h +</add> + +<requires> +mdx_hash +</requires> diff --git a/botan/src/hash/whirlpool/whrl_tab.cpp b/botan/src/hash/whirlpool/whrl_tab.cpp new file mode 100644 index 0000000..7ece927 --- /dev/null +++ b/botan/src/hash/whirlpool/whrl_tab.cpp @@ -0,0 +1,540 @@ +/* +* Diffusion Tables for Whirlpool +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/whrlpool.h> + +namespace Botan { + +const u64bit Whirlpool::C0[256] = { +(u64bit) 0x18186018C07830D8ULL, (u64bit) 0x23238C2305AF4626ULL, (u64bit) 0xC6C63FC67EF991B8ULL, (u64bit) 0xE8E887E8136FCDFBULL, +(u64bit) 0x878726874CA113CBULL, (u64bit) 0xB8B8DAB8A9626D11ULL, (u64bit) 0x0101040108050209ULL, (u64bit) 0x4F4F214F426E9E0DULL, +(u64bit) 0x3636D836ADEE6C9BULL, (u64bit) 0xA6A6A2A6590451FFULL, (u64bit) 0xD2D26FD2DEBDB90CULL, (u64bit) 0xF5F5F3F5FB06F70EULL, +(u64bit) 0x7979F979EF80F296ULL, (u64bit) 0x6F6FA16F5FCEDE30ULL, (u64bit) 0x91917E91FCEF3F6DULL, (u64bit) 0x52525552AA07A4F8ULL, +(u64bit) 0x60609D6027FDC047ULL, (u64bit) 0xBCBCCABC89766535ULL, (u64bit) 0x9B9B569BACCD2B37ULL, (u64bit) 0x8E8E028E048C018AULL, +(u64bit) 0xA3A3B6A371155BD2ULL, (u64bit) 0x0C0C300C603C186CULL, (u64bit) 0x7B7BF17BFF8AF684ULL, (u64bit) 0x3535D435B5E16A80ULL, +(u64bit) 0x1D1D741DE8693AF5ULL, (u64bit) 0xE0E0A7E05347DDB3ULL, (u64bit) 0xD7D77BD7F6ACB321ULL, (u64bit) 0xC2C22FC25EED999CULL, +(u64bit) 0x2E2EB82E6D965C43ULL, (u64bit) 0x4B4B314B627A9629ULL, (u64bit) 0xFEFEDFFEA321E15DULL, (u64bit) 0x575741578216AED5ULL, +(u64bit) 0x15155415A8412ABDULL, (u64bit) 0x7777C1779FB6EEE8ULL, (u64bit) 0x3737DC37A5EB6E92ULL, (u64bit) 0xE5E5B3E57B56D79EULL, +(u64bit) 0x9F9F469F8CD92313ULL, (u64bit) 0xF0F0E7F0D317FD23ULL, (u64bit) 0x4A4A354A6A7F9420ULL, (u64bit) 0xDADA4FDA9E95A944ULL, +(u64bit) 0x58587D58FA25B0A2ULL, (u64bit) 0xC9C903C906CA8FCFULL, (u64bit) 0x2929A429558D527CULL, (u64bit) 0x0A0A280A5022145AULL, +(u64bit) 0xB1B1FEB1E14F7F50ULL, (u64bit) 0xA0A0BAA0691A5DC9ULL, (u64bit) 0x6B6BB16B7FDAD614ULL, (u64bit) 0x85852E855CAB17D9ULL, +(u64bit) 0xBDBDCEBD8173673CULL, (u64bit) 0x5D5D695DD234BA8FULL, (u64bit) 0x1010401080502090ULL, (u64bit) 0xF4F4F7F4F303F507ULL, +(u64bit) 0xCBCB0BCB16C08BDDULL, (u64bit) 0x3E3EF83EEDC67CD3ULL, (u64bit) 0x0505140528110A2DULL, (u64bit) 0x676781671FE6CE78ULL, +(u64bit) 0xE4E4B7E47353D597ULL, (u64bit) 0x27279C2725BB4E02ULL, (u64bit) 0x4141194132588273ULL, (u64bit) 0x8B8B168B2C9D0BA7ULL, +(u64bit) 0xA7A7A6A7510153F6ULL, (u64bit) 0x7D7DE97DCF94FAB2ULL, (u64bit) 0x95956E95DCFB3749ULL, (u64bit) 0xD8D847D88E9FAD56ULL, +(u64bit) 0xFBFBCBFB8B30EB70ULL, (u64bit) 0xEEEE9FEE2371C1CDULL, (u64bit) 0x7C7CED7CC791F8BBULL, (u64bit) 0x6666856617E3CC71ULL, +(u64bit) 0xDDDD53DDA68EA77BULL, (u64bit) 0x17175C17B84B2EAFULL, (u64bit) 0x4747014702468E45ULL, (u64bit) 0x9E9E429E84DC211AULL, +(u64bit) 0xCACA0FCA1EC589D4ULL, (u64bit) 0x2D2DB42D75995A58ULL, (u64bit) 0xBFBFC6BF9179632EULL, (u64bit) 0x07071C07381B0E3FULL, +(u64bit) 0xADAD8EAD012347ACULL, (u64bit) 0x5A5A755AEA2FB4B0ULL, (u64bit) 0x838336836CB51BEFULL, (u64bit) 0x3333CC3385FF66B6ULL, +(u64bit) 0x636391633FF2C65CULL, (u64bit) 0x02020802100A0412ULL, (u64bit) 0xAAAA92AA39384993ULL, (u64bit) 0x7171D971AFA8E2DEULL, +(u64bit) 0xC8C807C80ECF8DC6ULL, (u64bit) 0x19196419C87D32D1ULL, (u64bit) 0x494939497270923BULL, (u64bit) 0xD9D943D9869AAF5FULL, +(u64bit) 0xF2F2EFF2C31DF931ULL, (u64bit) 0xE3E3ABE34B48DBA8ULL, (u64bit) 0x5B5B715BE22AB6B9ULL, (u64bit) 0x88881A8834920DBCULL, +(u64bit) 0x9A9A529AA4C8293EULL, (u64bit) 0x262698262DBE4C0BULL, (u64bit) 0x3232C8328DFA64BFULL, (u64bit) 0xB0B0FAB0E94A7D59ULL, +(u64bit) 0xE9E983E91B6ACFF2ULL, (u64bit) 0x0F0F3C0F78331E77ULL, (u64bit) 0xD5D573D5E6A6B733ULL, (u64bit) 0x80803A8074BA1DF4ULL, +(u64bit) 0xBEBEC2BE997C6127ULL, (u64bit) 0xCDCD13CD26DE87EBULL, (u64bit) 0x3434D034BDE46889ULL, (u64bit) 0x48483D487A759032ULL, +(u64bit) 0xFFFFDBFFAB24E354ULL, (u64bit) 0x7A7AF57AF78FF48DULL, (u64bit) 0x90907A90F4EA3D64ULL, (u64bit) 0x5F5F615FC23EBE9DULL, +(u64bit) 0x202080201DA0403DULL, (u64bit) 0x6868BD6867D5D00FULL, (u64bit) 0x1A1A681AD07234CAULL, (u64bit) 0xAEAE82AE192C41B7ULL, +(u64bit) 0xB4B4EAB4C95E757DULL, (u64bit) 0x54544D549A19A8CEULL, (u64bit) 0x93937693ECE53B7FULL, (u64bit) 0x222288220DAA442FULL, +(u64bit) 0x64648D6407E9C863ULL, (u64bit) 0xF1F1E3F1DB12FF2AULL, (u64bit) 0x7373D173BFA2E6CCULL, (u64bit) 0x12124812905A2482ULL, +(u64bit) 0x40401D403A5D807AULL, (u64bit) 0x0808200840281048ULL, (u64bit) 0xC3C32BC356E89B95ULL, (u64bit) 0xECEC97EC337BC5DFULL, +(u64bit) 0xDBDB4BDB9690AB4DULL, (u64bit) 0xA1A1BEA1611F5FC0ULL, (u64bit) 0x8D8D0E8D1C830791ULL, (u64bit) 0x3D3DF43DF5C97AC8ULL, +(u64bit) 0x97976697CCF1335BULL, (u64bit) 0x0000000000000000ULL, (u64bit) 0xCFCF1BCF36D483F9ULL, (u64bit) 0x2B2BAC2B4587566EULL, +(u64bit) 0x7676C57697B3ECE1ULL, (u64bit) 0x8282328264B019E6ULL, (u64bit) 0xD6D67FD6FEA9B128ULL, (u64bit) 0x1B1B6C1BD87736C3ULL, +(u64bit) 0xB5B5EEB5C15B7774ULL, (u64bit) 0xAFAF86AF112943BEULL, (u64bit) 0x6A6AB56A77DFD41DULL, (u64bit) 0x50505D50BA0DA0EAULL, +(u64bit) 0x45450945124C8A57ULL, (u64bit) 0xF3F3EBF3CB18FB38ULL, (u64bit) 0x3030C0309DF060ADULL, (u64bit) 0xEFEF9BEF2B74C3C4ULL, +(u64bit) 0x3F3FFC3FE5C37EDAULL, (u64bit) 0x55554955921CAAC7ULL, (u64bit) 0xA2A2B2A2791059DBULL, (u64bit) 0xEAEA8FEA0365C9E9ULL, +(u64bit) 0x656589650FECCA6AULL, (u64bit) 0xBABAD2BAB9686903ULL, (u64bit) 0x2F2FBC2F65935E4AULL, (u64bit) 0xC0C027C04EE79D8EULL, +(u64bit) 0xDEDE5FDEBE81A160ULL, (u64bit) 0x1C1C701CE06C38FCULL, (u64bit) 0xFDFDD3FDBB2EE746ULL, (u64bit) 0x4D4D294D52649A1FULL, +(u64bit) 0x92927292E4E03976ULL, (u64bit) 0x7575C9758FBCEAFAULL, (u64bit) 0x06061806301E0C36ULL, (u64bit) 0x8A8A128A249809AEULL, +(u64bit) 0xB2B2F2B2F940794BULL, (u64bit) 0xE6E6BFE66359D185ULL, (u64bit) 0x0E0E380E70361C7EULL, (u64bit) 0x1F1F7C1FF8633EE7ULL, +(u64bit) 0x6262956237F7C455ULL, (u64bit) 0xD4D477D4EEA3B53AULL, (u64bit) 0xA8A89AA829324D81ULL, (u64bit) 0x96966296C4F43152ULL, +(u64bit) 0xF9F9C3F99B3AEF62ULL, (u64bit) 0xC5C533C566F697A3ULL, (u64bit) 0x2525942535B14A10ULL, (u64bit) 0x59597959F220B2ABULL, +(u64bit) 0x84842A8454AE15D0ULL, (u64bit) 0x7272D572B7A7E4C5ULL, (u64bit) 0x3939E439D5DD72ECULL, (u64bit) 0x4C4C2D4C5A619816ULL, +(u64bit) 0x5E5E655ECA3BBC94ULL, (u64bit) 0x7878FD78E785F09FULL, (u64bit) 0x3838E038DDD870E5ULL, (u64bit) 0x8C8C0A8C14860598ULL, +(u64bit) 0xD1D163D1C6B2BF17ULL, (u64bit) 0xA5A5AEA5410B57E4ULL, (u64bit) 0xE2E2AFE2434DD9A1ULL, (u64bit) 0x616199612FF8C24EULL, +(u64bit) 0xB3B3F6B3F1457B42ULL, (u64bit) 0x2121842115A54234ULL, (u64bit) 0x9C9C4A9C94D62508ULL, (u64bit) 0x1E1E781EF0663CEEULL, +(u64bit) 0x4343114322528661ULL, (u64bit) 0xC7C73BC776FC93B1ULL, (u64bit) 0xFCFCD7FCB32BE54FULL, (u64bit) 0x0404100420140824ULL, +(u64bit) 0x51515951B208A2E3ULL, (u64bit) 0x99995E99BCC72F25ULL, (u64bit) 0x6D6DA96D4FC4DA22ULL, (u64bit) 0x0D0D340D68391A65ULL, +(u64bit) 0xFAFACFFA8335E979ULL, (u64bit) 0xDFDF5BDFB684A369ULL, (u64bit) 0x7E7EE57ED79BFCA9ULL, (u64bit) 0x242490243DB44819ULL, +(u64bit) 0x3B3BEC3BC5D776FEULL, (u64bit) 0xABAB96AB313D4B9AULL, (u64bit) 0xCECE1FCE3ED181F0ULL, (u64bit) 0x1111441188552299ULL, +(u64bit) 0x8F8F068F0C890383ULL, (u64bit) 0x4E4E254E4A6B9C04ULL, (u64bit) 0xB7B7E6B7D1517366ULL, (u64bit) 0xEBEB8BEB0B60CBE0ULL, +(u64bit) 0x3C3CF03CFDCC78C1ULL, (u64bit) 0x81813E817CBF1FFDULL, (u64bit) 0x94946A94D4FE3540ULL, (u64bit) 0xF7F7FBF7EB0CF31CULL, +(u64bit) 0xB9B9DEB9A1676F18ULL, (u64bit) 0x13134C13985F268BULL, (u64bit) 0x2C2CB02C7D9C5851ULL, (u64bit) 0xD3D36BD3D6B8BB05ULL, +(u64bit) 0xE7E7BBE76B5CD38CULL, (u64bit) 0x6E6EA56E57CBDC39ULL, (u64bit) 0xC4C437C46EF395AAULL, (u64bit) 0x03030C03180F061BULL, +(u64bit) 0x565645568A13ACDCULL, (u64bit) 0x44440D441A49885EULL, (u64bit) 0x7F7FE17FDF9EFEA0ULL, (u64bit) 0xA9A99EA921374F88ULL, +(u64bit) 0x2A2AA82A4D825467ULL, (u64bit) 0xBBBBD6BBB16D6B0AULL, (u64bit) 0xC1C123C146E29F87ULL, (u64bit) 0x53535153A202A6F1ULL, +(u64bit) 0xDCDC57DCAE8BA572ULL, (u64bit) 0x0B0B2C0B58271653ULL, (u64bit) 0x9D9D4E9D9CD32701ULL, (u64bit) 0x6C6CAD6C47C1D82BULL, +(u64bit) 0x3131C43195F562A4ULL, (u64bit) 0x7474CD7487B9E8F3ULL, (u64bit) 0xF6F6FFF6E309F115ULL, (u64bit) 0x464605460A438C4CULL, +(u64bit) 0xACAC8AAC092645A5ULL, (u64bit) 0x89891E893C970FB5ULL, (u64bit) 0x14145014A04428B4ULL, (u64bit) 0xE1E1A3E15B42DFBAULL, +(u64bit) 0x16165816B04E2CA6ULL, (u64bit) 0x3A3AE83ACDD274F7ULL, (u64bit) 0x6969B9696FD0D206ULL, (u64bit) 0x09092409482D1241ULL, +(u64bit) 0x7070DD70A7ADE0D7ULL, (u64bit) 0xB6B6E2B6D954716FULL, (u64bit) 0xD0D067D0CEB7BD1EULL, (u64bit) 0xEDED93ED3B7EC7D6ULL, +(u64bit) 0xCCCC17CC2EDB85E2ULL, (u64bit) 0x424215422A578468ULL, (u64bit) 0x98985A98B4C22D2CULL, (u64bit) 0xA4A4AAA4490E55EDULL, +(u64bit) 0x2828A0285D885075ULL, (u64bit) 0x5C5C6D5CDA31B886ULL, (u64bit) 0xF8F8C7F8933FED6BULL, (u64bit) 0x8686228644A411C2ULL }; + +const u64bit Whirlpool::C1[256] = { +(u64bit) 0xD818186018C07830ULL, (u64bit) 0x2623238C2305AF46ULL, (u64bit) 0xB8C6C63FC67EF991ULL, (u64bit) 0xFBE8E887E8136FCDULL, +(u64bit) 0xCB878726874CA113ULL, (u64bit) 0x11B8B8DAB8A9626DULL, (u64bit) 0x0901010401080502ULL, (u64bit) 0x0D4F4F214F426E9EULL, +(u64bit) 0x9B3636D836ADEE6CULL, (u64bit) 0xFFA6A6A2A6590451ULL, (u64bit) 0x0CD2D26FD2DEBDB9ULL, (u64bit) 0x0EF5F5F3F5FB06F7ULL, +(u64bit) 0x967979F979EF80F2ULL, (u64bit) 0x306F6FA16F5FCEDEULL, (u64bit) 0x6D91917E91FCEF3FULL, (u64bit) 0xF852525552AA07A4ULL, +(u64bit) 0x4760609D6027FDC0ULL, (u64bit) 0x35BCBCCABC897665ULL, (u64bit) 0x379B9B569BACCD2BULL, (u64bit) 0x8A8E8E028E048C01ULL, +(u64bit) 0xD2A3A3B6A371155BULL, (u64bit) 0x6C0C0C300C603C18ULL, (u64bit) 0x847B7BF17BFF8AF6ULL, (u64bit) 0x803535D435B5E16AULL, +(u64bit) 0xF51D1D741DE8693AULL, (u64bit) 0xB3E0E0A7E05347DDULL, (u64bit) 0x21D7D77BD7F6ACB3ULL, (u64bit) 0x9CC2C22FC25EED99ULL, +(u64bit) 0x432E2EB82E6D965CULL, (u64bit) 0x294B4B314B627A96ULL, (u64bit) 0x5DFEFEDFFEA321E1ULL, (u64bit) 0xD5575741578216AEULL, +(u64bit) 0xBD15155415A8412AULL, (u64bit) 0xE87777C1779FB6EEULL, (u64bit) 0x923737DC37A5EB6EULL, (u64bit) 0x9EE5E5B3E57B56D7ULL, +(u64bit) 0x139F9F469F8CD923ULL, (u64bit) 0x23F0F0E7F0D317FDULL, (u64bit) 0x204A4A354A6A7F94ULL, (u64bit) 0x44DADA4FDA9E95A9ULL, +(u64bit) 0xA258587D58FA25B0ULL, (u64bit) 0xCFC9C903C906CA8FULL, (u64bit) 0x7C2929A429558D52ULL, (u64bit) 0x5A0A0A280A502214ULL, +(u64bit) 0x50B1B1FEB1E14F7FULL, (u64bit) 0xC9A0A0BAA0691A5DULL, (u64bit) 0x146B6BB16B7FDAD6ULL, (u64bit) 0xD985852E855CAB17ULL, +(u64bit) 0x3CBDBDCEBD817367ULL, (u64bit) 0x8F5D5D695DD234BAULL, (u64bit) 0x9010104010805020ULL, (u64bit) 0x07F4F4F7F4F303F5ULL, +(u64bit) 0xDDCBCB0BCB16C08BULL, (u64bit) 0xD33E3EF83EEDC67CULL, (u64bit) 0x2D0505140528110AULL, (u64bit) 0x78676781671FE6CEULL, +(u64bit) 0x97E4E4B7E47353D5ULL, (u64bit) 0x0227279C2725BB4EULL, (u64bit) 0x7341411941325882ULL, (u64bit) 0xA78B8B168B2C9D0BULL, +(u64bit) 0xF6A7A7A6A7510153ULL, (u64bit) 0xB27D7DE97DCF94FAULL, (u64bit) 0x4995956E95DCFB37ULL, (u64bit) 0x56D8D847D88E9FADULL, +(u64bit) 0x70FBFBCBFB8B30EBULL, (u64bit) 0xCDEEEE9FEE2371C1ULL, (u64bit) 0xBB7C7CED7CC791F8ULL, (u64bit) 0x716666856617E3CCULL, +(u64bit) 0x7BDDDD53DDA68EA7ULL, (u64bit) 0xAF17175C17B84B2EULL, (u64bit) 0x454747014702468EULL, (u64bit) 0x1A9E9E429E84DC21ULL, +(u64bit) 0xD4CACA0FCA1EC589ULL, (u64bit) 0x582D2DB42D75995AULL, (u64bit) 0x2EBFBFC6BF917963ULL, (u64bit) 0x3F07071C07381B0EULL, +(u64bit) 0xACADAD8EAD012347ULL, (u64bit) 0xB05A5A755AEA2FB4ULL, (u64bit) 0xEF838336836CB51BULL, (u64bit) 0xB63333CC3385FF66ULL, +(u64bit) 0x5C636391633FF2C6ULL, (u64bit) 0x1202020802100A04ULL, (u64bit) 0x93AAAA92AA393849ULL, (u64bit) 0xDE7171D971AFA8E2ULL, +(u64bit) 0xC6C8C807C80ECF8DULL, (u64bit) 0xD119196419C87D32ULL, (u64bit) 0x3B49493949727092ULL, (u64bit) 0x5FD9D943D9869AAFULL, +(u64bit) 0x31F2F2EFF2C31DF9ULL, (u64bit) 0xA8E3E3ABE34B48DBULL, (u64bit) 0xB95B5B715BE22AB6ULL, (u64bit) 0xBC88881A8834920DULL, +(u64bit) 0x3E9A9A529AA4C829ULL, (u64bit) 0x0B262698262DBE4CULL, (u64bit) 0xBF3232C8328DFA64ULL, (u64bit) 0x59B0B0FAB0E94A7DULL, +(u64bit) 0xF2E9E983E91B6ACFULL, (u64bit) 0x770F0F3C0F78331EULL, (u64bit) 0x33D5D573D5E6A6B7ULL, (u64bit) 0xF480803A8074BA1DULL, +(u64bit) 0x27BEBEC2BE997C61ULL, (u64bit) 0xEBCDCD13CD26DE87ULL, (u64bit) 0x893434D034BDE468ULL, (u64bit) 0x3248483D487A7590ULL, +(u64bit) 0x54FFFFDBFFAB24E3ULL, (u64bit) 0x8D7A7AF57AF78FF4ULL, (u64bit) 0x6490907A90F4EA3DULL, (u64bit) 0x9D5F5F615FC23EBEULL, +(u64bit) 0x3D202080201DA040ULL, (u64bit) 0x0F6868BD6867D5D0ULL, (u64bit) 0xCA1A1A681AD07234ULL, (u64bit) 0xB7AEAE82AE192C41ULL, +(u64bit) 0x7DB4B4EAB4C95E75ULL, (u64bit) 0xCE54544D549A19A8ULL, (u64bit) 0x7F93937693ECE53BULL, (u64bit) 0x2F222288220DAA44ULL, +(u64bit) 0x6364648D6407E9C8ULL, (u64bit) 0x2AF1F1E3F1DB12FFULL, (u64bit) 0xCC7373D173BFA2E6ULL, (u64bit) 0x8212124812905A24ULL, +(u64bit) 0x7A40401D403A5D80ULL, (u64bit) 0x4808082008402810ULL, (u64bit) 0x95C3C32BC356E89BULL, (u64bit) 0xDFECEC97EC337BC5ULL, +(u64bit) 0x4DDBDB4BDB9690ABULL, (u64bit) 0xC0A1A1BEA1611F5FULL, (u64bit) 0x918D8D0E8D1C8307ULL, (u64bit) 0xC83D3DF43DF5C97AULL, +(u64bit) 0x5B97976697CCF133ULL, (u64bit) 0x0000000000000000ULL, (u64bit) 0xF9CFCF1BCF36D483ULL, (u64bit) 0x6E2B2BAC2B458756ULL, +(u64bit) 0xE17676C57697B3ECULL, (u64bit) 0xE68282328264B019ULL, (u64bit) 0x28D6D67FD6FEA9B1ULL, (u64bit) 0xC31B1B6C1BD87736ULL, +(u64bit) 0x74B5B5EEB5C15B77ULL, (u64bit) 0xBEAFAF86AF112943ULL, (u64bit) 0x1D6A6AB56A77DFD4ULL, (u64bit) 0xEA50505D50BA0DA0ULL, +(u64bit) 0x5745450945124C8AULL, (u64bit) 0x38F3F3EBF3CB18FBULL, (u64bit) 0xAD3030C0309DF060ULL, (u64bit) 0xC4EFEF9BEF2B74C3ULL, +(u64bit) 0xDA3F3FFC3FE5C37EULL, (u64bit) 0xC755554955921CAAULL, (u64bit) 0xDBA2A2B2A2791059ULL, (u64bit) 0xE9EAEA8FEA0365C9ULL, +(u64bit) 0x6A656589650FECCAULL, (u64bit) 0x03BABAD2BAB96869ULL, (u64bit) 0x4A2F2FBC2F65935EULL, (u64bit) 0x8EC0C027C04EE79DULL, +(u64bit) 0x60DEDE5FDEBE81A1ULL, (u64bit) 0xFC1C1C701CE06C38ULL, (u64bit) 0x46FDFDD3FDBB2EE7ULL, (u64bit) 0x1F4D4D294D52649AULL, +(u64bit) 0x7692927292E4E039ULL, (u64bit) 0xFA7575C9758FBCEAULL, (u64bit) 0x3606061806301E0CULL, (u64bit) 0xAE8A8A128A249809ULL, +(u64bit) 0x4BB2B2F2B2F94079ULL, (u64bit) 0x85E6E6BFE66359D1ULL, (u64bit) 0x7E0E0E380E70361CULL, (u64bit) 0xE71F1F7C1FF8633EULL, +(u64bit) 0x556262956237F7C4ULL, (u64bit) 0x3AD4D477D4EEA3B5ULL, (u64bit) 0x81A8A89AA829324DULL, (u64bit) 0x5296966296C4F431ULL, +(u64bit) 0x62F9F9C3F99B3AEFULL, (u64bit) 0xA3C5C533C566F697ULL, (u64bit) 0x102525942535B14AULL, (u64bit) 0xAB59597959F220B2ULL, +(u64bit) 0xD084842A8454AE15ULL, (u64bit) 0xC57272D572B7A7E4ULL, (u64bit) 0xEC3939E439D5DD72ULL, (u64bit) 0x164C4C2D4C5A6198ULL, +(u64bit) 0x945E5E655ECA3BBCULL, (u64bit) 0x9F7878FD78E785F0ULL, (u64bit) 0xE53838E038DDD870ULL, (u64bit) 0x988C8C0A8C148605ULL, +(u64bit) 0x17D1D163D1C6B2BFULL, (u64bit) 0xE4A5A5AEA5410B57ULL, (u64bit) 0xA1E2E2AFE2434DD9ULL, (u64bit) 0x4E616199612FF8C2ULL, +(u64bit) 0x42B3B3F6B3F1457BULL, (u64bit) 0x342121842115A542ULL, (u64bit) 0x089C9C4A9C94D625ULL, (u64bit) 0xEE1E1E781EF0663CULL, +(u64bit) 0x6143431143225286ULL, (u64bit) 0xB1C7C73BC776FC93ULL, (u64bit) 0x4FFCFCD7FCB32BE5ULL, (u64bit) 0x2404041004201408ULL, +(u64bit) 0xE351515951B208A2ULL, (u64bit) 0x2599995E99BCC72FULL, (u64bit) 0x226D6DA96D4FC4DAULL, (u64bit) 0x650D0D340D68391AULL, +(u64bit) 0x79FAFACFFA8335E9ULL, (u64bit) 0x69DFDF5BDFB684A3ULL, (u64bit) 0xA97E7EE57ED79BFCULL, (u64bit) 0x19242490243DB448ULL, +(u64bit) 0xFE3B3BEC3BC5D776ULL, (u64bit) 0x9AABAB96AB313D4BULL, (u64bit) 0xF0CECE1FCE3ED181ULL, (u64bit) 0x9911114411885522ULL, +(u64bit) 0x838F8F068F0C8903ULL, (u64bit) 0x044E4E254E4A6B9CULL, (u64bit) 0x66B7B7E6B7D15173ULL, (u64bit) 0xE0EBEB8BEB0B60CBULL, +(u64bit) 0xC13C3CF03CFDCC78ULL, (u64bit) 0xFD81813E817CBF1FULL, (u64bit) 0x4094946A94D4FE35ULL, (u64bit) 0x1CF7F7FBF7EB0CF3ULL, +(u64bit) 0x18B9B9DEB9A1676FULL, (u64bit) 0x8B13134C13985F26ULL, (u64bit) 0x512C2CB02C7D9C58ULL, (u64bit) 0x05D3D36BD3D6B8BBULL, +(u64bit) 0x8CE7E7BBE76B5CD3ULL, (u64bit) 0x396E6EA56E57CBDCULL, (u64bit) 0xAAC4C437C46EF395ULL, (u64bit) 0x1B03030C03180F06ULL, +(u64bit) 0xDC565645568A13ACULL, (u64bit) 0x5E44440D441A4988ULL, (u64bit) 0xA07F7FE17FDF9EFEULL, (u64bit) 0x88A9A99EA921374FULL, +(u64bit) 0x672A2AA82A4D8254ULL, (u64bit) 0x0ABBBBD6BBB16D6BULL, (u64bit) 0x87C1C123C146E29FULL, (u64bit) 0xF153535153A202A6ULL, +(u64bit) 0x72DCDC57DCAE8BA5ULL, (u64bit) 0x530B0B2C0B582716ULL, (u64bit) 0x019D9D4E9D9CD327ULL, (u64bit) 0x2B6C6CAD6C47C1D8ULL, +(u64bit) 0xA43131C43195F562ULL, (u64bit) 0xF37474CD7487B9E8ULL, (u64bit) 0x15F6F6FFF6E309F1ULL, (u64bit) 0x4C464605460A438CULL, +(u64bit) 0xA5ACAC8AAC092645ULL, (u64bit) 0xB589891E893C970FULL, (u64bit) 0xB414145014A04428ULL, (u64bit) 0xBAE1E1A3E15B42DFULL, +(u64bit) 0xA616165816B04E2CULL, (u64bit) 0xF73A3AE83ACDD274ULL, (u64bit) 0x066969B9696FD0D2ULL, (u64bit) 0x4109092409482D12ULL, +(u64bit) 0xD77070DD70A7ADE0ULL, (u64bit) 0x6FB6B6E2B6D95471ULL, (u64bit) 0x1ED0D067D0CEB7BDULL, (u64bit) 0xD6EDED93ED3B7EC7ULL, +(u64bit) 0xE2CCCC17CC2EDB85ULL, (u64bit) 0x68424215422A5784ULL, (u64bit) 0x2C98985A98B4C22DULL, (u64bit) 0xEDA4A4AAA4490E55ULL, +(u64bit) 0x752828A0285D8850ULL, (u64bit) 0x865C5C6D5CDA31B8ULL, (u64bit) 0x6BF8F8C7F8933FEDULL, (u64bit) 0xC28686228644A411ULL }; + +const u64bit Whirlpool::C2[256] = { +(u64bit) 0x30D818186018C078ULL, (u64bit) 0x462623238C2305AFULL, (u64bit) 0x91B8C6C63FC67EF9ULL, (u64bit) 0xCDFBE8E887E8136FULL, +(u64bit) 0x13CB878726874CA1ULL, (u64bit) 0x6D11B8B8DAB8A962ULL, (u64bit) 0x0209010104010805ULL, (u64bit) 0x9E0D4F4F214F426EULL, +(u64bit) 0x6C9B3636D836ADEEULL, (u64bit) 0x51FFA6A6A2A65904ULL, (u64bit) 0xB90CD2D26FD2DEBDULL, (u64bit) 0xF70EF5F5F3F5FB06ULL, +(u64bit) 0xF2967979F979EF80ULL, (u64bit) 0xDE306F6FA16F5FCEULL, (u64bit) 0x3F6D91917E91FCEFULL, (u64bit) 0xA4F852525552AA07ULL, +(u64bit) 0xC04760609D6027FDULL, (u64bit) 0x6535BCBCCABC8976ULL, (u64bit) 0x2B379B9B569BACCDULL, (u64bit) 0x018A8E8E028E048CULL, +(u64bit) 0x5BD2A3A3B6A37115ULL, (u64bit) 0x186C0C0C300C603CULL, (u64bit) 0xF6847B7BF17BFF8AULL, (u64bit) 0x6A803535D435B5E1ULL, +(u64bit) 0x3AF51D1D741DE869ULL, (u64bit) 0xDDB3E0E0A7E05347ULL, (u64bit) 0xB321D7D77BD7F6ACULL, (u64bit) 0x999CC2C22FC25EEDULL, +(u64bit) 0x5C432E2EB82E6D96ULL, (u64bit) 0x96294B4B314B627AULL, (u64bit) 0xE15DFEFEDFFEA321ULL, (u64bit) 0xAED5575741578216ULL, +(u64bit) 0x2ABD15155415A841ULL, (u64bit) 0xEEE87777C1779FB6ULL, (u64bit) 0x6E923737DC37A5EBULL, (u64bit) 0xD79EE5E5B3E57B56ULL, +(u64bit) 0x23139F9F469F8CD9ULL, (u64bit) 0xFD23F0F0E7F0D317ULL, (u64bit) 0x94204A4A354A6A7FULL, (u64bit) 0xA944DADA4FDA9E95ULL, +(u64bit) 0xB0A258587D58FA25ULL, (u64bit) 0x8FCFC9C903C906CAULL, (u64bit) 0x527C2929A429558DULL, (u64bit) 0x145A0A0A280A5022ULL, +(u64bit) 0x7F50B1B1FEB1E14FULL, (u64bit) 0x5DC9A0A0BAA0691AULL, (u64bit) 0xD6146B6BB16B7FDAULL, (u64bit) 0x17D985852E855CABULL, +(u64bit) 0x673CBDBDCEBD8173ULL, (u64bit) 0xBA8F5D5D695DD234ULL, (u64bit) 0x2090101040108050ULL, (u64bit) 0xF507F4F4F7F4F303ULL, +(u64bit) 0x8BDDCBCB0BCB16C0ULL, (u64bit) 0x7CD33E3EF83EEDC6ULL, (u64bit) 0x0A2D050514052811ULL, (u64bit) 0xCE78676781671FE6ULL, +(u64bit) 0xD597E4E4B7E47353ULL, (u64bit) 0x4E0227279C2725BBULL, (u64bit) 0x8273414119413258ULL, (u64bit) 0x0BA78B8B168B2C9DULL, +(u64bit) 0x53F6A7A7A6A75101ULL, (u64bit) 0xFAB27D7DE97DCF94ULL, (u64bit) 0x374995956E95DCFBULL, (u64bit) 0xAD56D8D847D88E9FULL, +(u64bit) 0xEB70FBFBCBFB8B30ULL, (u64bit) 0xC1CDEEEE9FEE2371ULL, (u64bit) 0xF8BB7C7CED7CC791ULL, (u64bit) 0xCC716666856617E3ULL, +(u64bit) 0xA77BDDDD53DDA68EULL, (u64bit) 0x2EAF17175C17B84BULL, (u64bit) 0x8E45474701470246ULL, (u64bit) 0x211A9E9E429E84DCULL, +(u64bit) 0x89D4CACA0FCA1EC5ULL, (u64bit) 0x5A582D2DB42D7599ULL, (u64bit) 0x632EBFBFC6BF9179ULL, (u64bit) 0x0E3F07071C07381BULL, +(u64bit) 0x47ACADAD8EAD0123ULL, (u64bit) 0xB4B05A5A755AEA2FULL, (u64bit) 0x1BEF838336836CB5ULL, (u64bit) 0x66B63333CC3385FFULL, +(u64bit) 0xC65C636391633FF2ULL, (u64bit) 0x041202020802100AULL, (u64bit) 0x4993AAAA92AA3938ULL, (u64bit) 0xE2DE7171D971AFA8ULL, +(u64bit) 0x8DC6C8C807C80ECFULL, (u64bit) 0x32D119196419C87DULL, (u64bit) 0x923B494939497270ULL, (u64bit) 0xAF5FD9D943D9869AULL, +(u64bit) 0xF931F2F2EFF2C31DULL, (u64bit) 0xDBA8E3E3ABE34B48ULL, (u64bit) 0xB6B95B5B715BE22AULL, (u64bit) 0x0DBC88881A883492ULL, +(u64bit) 0x293E9A9A529AA4C8ULL, (u64bit) 0x4C0B262698262DBEULL, (u64bit) 0x64BF3232C8328DFAULL, (u64bit) 0x7D59B0B0FAB0E94AULL, +(u64bit) 0xCFF2E9E983E91B6AULL, (u64bit) 0x1E770F0F3C0F7833ULL, (u64bit) 0xB733D5D573D5E6A6ULL, (u64bit) 0x1DF480803A8074BAULL, +(u64bit) 0x6127BEBEC2BE997CULL, (u64bit) 0x87EBCDCD13CD26DEULL, (u64bit) 0x68893434D034BDE4ULL, (u64bit) 0x903248483D487A75ULL, +(u64bit) 0xE354FFFFDBFFAB24ULL, (u64bit) 0xF48D7A7AF57AF78FULL, (u64bit) 0x3D6490907A90F4EAULL, (u64bit) 0xBE9D5F5F615FC23EULL, +(u64bit) 0x403D202080201DA0ULL, (u64bit) 0xD00F6868BD6867D5ULL, (u64bit) 0x34CA1A1A681AD072ULL, (u64bit) 0x41B7AEAE82AE192CULL, +(u64bit) 0x757DB4B4EAB4C95EULL, (u64bit) 0xA8CE54544D549A19ULL, (u64bit) 0x3B7F93937693ECE5ULL, (u64bit) 0x442F222288220DAAULL, +(u64bit) 0xC86364648D6407E9ULL, (u64bit) 0xFF2AF1F1E3F1DB12ULL, (u64bit) 0xE6CC7373D173BFA2ULL, (u64bit) 0x248212124812905AULL, +(u64bit) 0x807A40401D403A5DULL, (u64bit) 0x1048080820084028ULL, (u64bit) 0x9B95C3C32BC356E8ULL, (u64bit) 0xC5DFECEC97EC337BULL, +(u64bit) 0xAB4DDBDB4BDB9690ULL, (u64bit) 0x5FC0A1A1BEA1611FULL, (u64bit) 0x07918D8D0E8D1C83ULL, (u64bit) 0x7AC83D3DF43DF5C9ULL, +(u64bit) 0x335B97976697CCF1ULL, (u64bit) 0x0000000000000000ULL, (u64bit) 0x83F9CFCF1BCF36D4ULL, (u64bit) 0x566E2B2BAC2B4587ULL, +(u64bit) 0xECE17676C57697B3ULL, (u64bit) 0x19E68282328264B0ULL, (u64bit) 0xB128D6D67FD6FEA9ULL, (u64bit) 0x36C31B1B6C1BD877ULL, +(u64bit) 0x7774B5B5EEB5C15BULL, (u64bit) 0x43BEAFAF86AF1129ULL, (u64bit) 0xD41D6A6AB56A77DFULL, (u64bit) 0xA0EA50505D50BA0DULL, +(u64bit) 0x8A5745450945124CULL, (u64bit) 0xFB38F3F3EBF3CB18ULL, (u64bit) 0x60AD3030C0309DF0ULL, (u64bit) 0xC3C4EFEF9BEF2B74ULL, +(u64bit) 0x7EDA3F3FFC3FE5C3ULL, (u64bit) 0xAAC755554955921CULL, (u64bit) 0x59DBA2A2B2A27910ULL, (u64bit) 0xC9E9EAEA8FEA0365ULL, +(u64bit) 0xCA6A656589650FECULL, (u64bit) 0x6903BABAD2BAB968ULL, (u64bit) 0x5E4A2F2FBC2F6593ULL, (u64bit) 0x9D8EC0C027C04EE7ULL, +(u64bit) 0xA160DEDE5FDEBE81ULL, (u64bit) 0x38FC1C1C701CE06CULL, (u64bit) 0xE746FDFDD3FDBB2EULL, (u64bit) 0x9A1F4D4D294D5264ULL, +(u64bit) 0x397692927292E4E0ULL, (u64bit) 0xEAFA7575C9758FBCULL, (u64bit) 0x0C3606061806301EULL, (u64bit) 0x09AE8A8A128A2498ULL, +(u64bit) 0x794BB2B2F2B2F940ULL, (u64bit) 0xD185E6E6BFE66359ULL, (u64bit) 0x1C7E0E0E380E7036ULL, (u64bit) 0x3EE71F1F7C1FF863ULL, +(u64bit) 0xC4556262956237F7ULL, (u64bit) 0xB53AD4D477D4EEA3ULL, (u64bit) 0x4D81A8A89AA82932ULL, (u64bit) 0x315296966296C4F4ULL, +(u64bit) 0xEF62F9F9C3F99B3AULL, (u64bit) 0x97A3C5C533C566F6ULL, (u64bit) 0x4A102525942535B1ULL, (u64bit) 0xB2AB59597959F220ULL, +(u64bit) 0x15D084842A8454AEULL, (u64bit) 0xE4C57272D572B7A7ULL, (u64bit) 0x72EC3939E439D5DDULL, (u64bit) 0x98164C4C2D4C5A61ULL, +(u64bit) 0xBC945E5E655ECA3BULL, (u64bit) 0xF09F7878FD78E785ULL, (u64bit) 0x70E53838E038DDD8ULL, (u64bit) 0x05988C8C0A8C1486ULL, +(u64bit) 0xBF17D1D163D1C6B2ULL, (u64bit) 0x57E4A5A5AEA5410BULL, (u64bit) 0xD9A1E2E2AFE2434DULL, (u64bit) 0xC24E616199612FF8ULL, +(u64bit) 0x7B42B3B3F6B3F145ULL, (u64bit) 0x42342121842115A5ULL, (u64bit) 0x25089C9C4A9C94D6ULL, (u64bit) 0x3CEE1E1E781EF066ULL, +(u64bit) 0x8661434311432252ULL, (u64bit) 0x93B1C7C73BC776FCULL, (u64bit) 0xE54FFCFCD7FCB32BULL, (u64bit) 0x0824040410042014ULL, +(u64bit) 0xA2E351515951B208ULL, (u64bit) 0x2F2599995E99BCC7ULL, (u64bit) 0xDA226D6DA96D4FC4ULL, (u64bit) 0x1A650D0D340D6839ULL, +(u64bit) 0xE979FAFACFFA8335ULL, (u64bit) 0xA369DFDF5BDFB684ULL, (u64bit) 0xFCA97E7EE57ED79BULL, (u64bit) 0x4819242490243DB4ULL, +(u64bit) 0x76FE3B3BEC3BC5D7ULL, (u64bit) 0x4B9AABAB96AB313DULL, (u64bit) 0x81F0CECE1FCE3ED1ULL, (u64bit) 0x2299111144118855ULL, +(u64bit) 0x03838F8F068F0C89ULL, (u64bit) 0x9C044E4E254E4A6BULL, (u64bit) 0x7366B7B7E6B7D151ULL, (u64bit) 0xCBE0EBEB8BEB0B60ULL, +(u64bit) 0x78C13C3CF03CFDCCULL, (u64bit) 0x1FFD81813E817CBFULL, (u64bit) 0x354094946A94D4FEULL, (u64bit) 0xF31CF7F7FBF7EB0CULL, +(u64bit) 0x6F18B9B9DEB9A167ULL, (u64bit) 0x268B13134C13985FULL, (u64bit) 0x58512C2CB02C7D9CULL, (u64bit) 0xBB05D3D36BD3D6B8ULL, +(u64bit) 0xD38CE7E7BBE76B5CULL, (u64bit) 0xDC396E6EA56E57CBULL, (u64bit) 0x95AAC4C437C46EF3ULL, (u64bit) 0x061B03030C03180FULL, +(u64bit) 0xACDC565645568A13ULL, (u64bit) 0x885E44440D441A49ULL, (u64bit) 0xFEA07F7FE17FDF9EULL, (u64bit) 0x4F88A9A99EA92137ULL, +(u64bit) 0x54672A2AA82A4D82ULL, (u64bit) 0x6B0ABBBBD6BBB16DULL, (u64bit) 0x9F87C1C123C146E2ULL, (u64bit) 0xA6F153535153A202ULL, +(u64bit) 0xA572DCDC57DCAE8BULL, (u64bit) 0x16530B0B2C0B5827ULL, (u64bit) 0x27019D9D4E9D9CD3ULL, (u64bit) 0xD82B6C6CAD6C47C1ULL, +(u64bit) 0x62A43131C43195F5ULL, (u64bit) 0xE8F37474CD7487B9ULL, (u64bit) 0xF115F6F6FFF6E309ULL, (u64bit) 0x8C4C464605460A43ULL, +(u64bit) 0x45A5ACAC8AAC0926ULL, (u64bit) 0x0FB589891E893C97ULL, (u64bit) 0x28B414145014A044ULL, (u64bit) 0xDFBAE1E1A3E15B42ULL, +(u64bit) 0x2CA616165816B04EULL, (u64bit) 0x74F73A3AE83ACDD2ULL, (u64bit) 0xD2066969B9696FD0ULL, (u64bit) 0x124109092409482DULL, +(u64bit) 0xE0D77070DD70A7ADULL, (u64bit) 0x716FB6B6E2B6D954ULL, (u64bit) 0xBD1ED0D067D0CEB7ULL, (u64bit) 0xC7D6EDED93ED3B7EULL, +(u64bit) 0x85E2CCCC17CC2EDBULL, (u64bit) 0x8468424215422A57ULL, (u64bit) 0x2D2C98985A98B4C2ULL, (u64bit) 0x55EDA4A4AAA4490EULL, +(u64bit) 0x50752828A0285D88ULL, (u64bit) 0xB8865C5C6D5CDA31ULL, (u64bit) 0xED6BF8F8C7F8933FULL, (u64bit) 0x11C28686228644A4ULL }; + +const u64bit Whirlpool::C3[256] = { +(u64bit) 0x7830D818186018C0ULL, (u64bit) 0xAF462623238C2305ULL, (u64bit) 0xF991B8C6C63FC67EULL, (u64bit) 0x6FCDFBE8E887E813ULL, +(u64bit) 0xA113CB878726874CULL, (u64bit) 0x626D11B8B8DAB8A9ULL, (u64bit) 0x0502090101040108ULL, (u64bit) 0x6E9E0D4F4F214F42ULL, +(u64bit) 0xEE6C9B3636D836ADULL, (u64bit) 0x0451FFA6A6A2A659ULL, (u64bit) 0xBDB90CD2D26FD2DEULL, (u64bit) 0x06F70EF5F5F3F5FBULL, +(u64bit) 0x80F2967979F979EFULL, (u64bit) 0xCEDE306F6FA16F5FULL, (u64bit) 0xEF3F6D91917E91FCULL, (u64bit) 0x07A4F852525552AAULL, +(u64bit) 0xFDC04760609D6027ULL, (u64bit) 0x766535BCBCCABC89ULL, (u64bit) 0xCD2B379B9B569BACULL, (u64bit) 0x8C018A8E8E028E04ULL, +(u64bit) 0x155BD2A3A3B6A371ULL, (u64bit) 0x3C186C0C0C300C60ULL, (u64bit) 0x8AF6847B7BF17BFFULL, (u64bit) 0xE16A803535D435B5ULL, +(u64bit) 0x693AF51D1D741DE8ULL, (u64bit) 0x47DDB3E0E0A7E053ULL, (u64bit) 0xACB321D7D77BD7F6ULL, (u64bit) 0xED999CC2C22FC25EULL, +(u64bit) 0x965C432E2EB82E6DULL, (u64bit) 0x7A96294B4B314B62ULL, (u64bit) 0x21E15DFEFEDFFEA3ULL, (u64bit) 0x16AED55757415782ULL, +(u64bit) 0x412ABD15155415A8ULL, (u64bit) 0xB6EEE87777C1779FULL, (u64bit) 0xEB6E923737DC37A5ULL, (u64bit) 0x56D79EE5E5B3E57BULL, +(u64bit) 0xD923139F9F469F8CULL, (u64bit) 0x17FD23F0F0E7F0D3ULL, (u64bit) 0x7F94204A4A354A6AULL, (u64bit) 0x95A944DADA4FDA9EULL, +(u64bit) 0x25B0A258587D58FAULL, (u64bit) 0xCA8FCFC9C903C906ULL, (u64bit) 0x8D527C2929A42955ULL, (u64bit) 0x22145A0A0A280A50ULL, +(u64bit) 0x4F7F50B1B1FEB1E1ULL, (u64bit) 0x1A5DC9A0A0BAA069ULL, (u64bit) 0xDAD6146B6BB16B7FULL, (u64bit) 0xAB17D985852E855CULL, +(u64bit) 0x73673CBDBDCEBD81ULL, (u64bit) 0x34BA8F5D5D695DD2ULL, (u64bit) 0x5020901010401080ULL, (u64bit) 0x03F507F4F4F7F4F3ULL, +(u64bit) 0xC08BDDCBCB0BCB16ULL, (u64bit) 0xC67CD33E3EF83EEDULL, (u64bit) 0x110A2D0505140528ULL, (u64bit) 0xE6CE78676781671FULL, +(u64bit) 0x53D597E4E4B7E473ULL, (u64bit) 0xBB4E0227279C2725ULL, (u64bit) 0x5882734141194132ULL, (u64bit) 0x9D0BA78B8B168B2CULL, +(u64bit) 0x0153F6A7A7A6A751ULL, (u64bit) 0x94FAB27D7DE97DCFULL, (u64bit) 0xFB374995956E95DCULL, (u64bit) 0x9FAD56D8D847D88EULL, +(u64bit) 0x30EB70FBFBCBFB8BULL, (u64bit) 0x71C1CDEEEE9FEE23ULL, (u64bit) 0x91F8BB7C7CED7CC7ULL, (u64bit) 0xE3CC716666856617ULL, +(u64bit) 0x8EA77BDDDD53DDA6ULL, (u64bit) 0x4B2EAF17175C17B8ULL, (u64bit) 0x468E454747014702ULL, (u64bit) 0xDC211A9E9E429E84ULL, +(u64bit) 0xC589D4CACA0FCA1EULL, (u64bit) 0x995A582D2DB42D75ULL, (u64bit) 0x79632EBFBFC6BF91ULL, (u64bit) 0x1B0E3F07071C0738ULL, +(u64bit) 0x2347ACADAD8EAD01ULL, (u64bit) 0x2FB4B05A5A755AEAULL, (u64bit) 0xB51BEF838336836CULL, (u64bit) 0xFF66B63333CC3385ULL, +(u64bit) 0xF2C65C636391633FULL, (u64bit) 0x0A04120202080210ULL, (u64bit) 0x384993AAAA92AA39ULL, (u64bit) 0xA8E2DE7171D971AFULL, +(u64bit) 0xCF8DC6C8C807C80EULL, (u64bit) 0x7D32D119196419C8ULL, (u64bit) 0x70923B4949394972ULL, (u64bit) 0x9AAF5FD9D943D986ULL, +(u64bit) 0x1DF931F2F2EFF2C3ULL, (u64bit) 0x48DBA8E3E3ABE34BULL, (u64bit) 0x2AB6B95B5B715BE2ULL, (u64bit) 0x920DBC88881A8834ULL, +(u64bit) 0xC8293E9A9A529AA4ULL, (u64bit) 0xBE4C0B262698262DULL, (u64bit) 0xFA64BF3232C8328DULL, (u64bit) 0x4A7D59B0B0FAB0E9ULL, +(u64bit) 0x6ACFF2E9E983E91BULL, (u64bit) 0x331E770F0F3C0F78ULL, (u64bit) 0xA6B733D5D573D5E6ULL, (u64bit) 0xBA1DF480803A8074ULL, +(u64bit) 0x7C6127BEBEC2BE99ULL, (u64bit) 0xDE87EBCDCD13CD26ULL, (u64bit) 0xE468893434D034BDULL, (u64bit) 0x75903248483D487AULL, +(u64bit) 0x24E354FFFFDBFFABULL, (u64bit) 0x8FF48D7A7AF57AF7ULL, (u64bit) 0xEA3D6490907A90F4ULL, (u64bit) 0x3EBE9D5F5F615FC2ULL, +(u64bit) 0xA0403D202080201DULL, (u64bit) 0xD5D00F6868BD6867ULL, (u64bit) 0x7234CA1A1A681AD0ULL, (u64bit) 0x2C41B7AEAE82AE19ULL, +(u64bit) 0x5E757DB4B4EAB4C9ULL, (u64bit) 0x19A8CE54544D549AULL, (u64bit) 0xE53B7F93937693ECULL, (u64bit) 0xAA442F222288220DULL, +(u64bit) 0xE9C86364648D6407ULL, (u64bit) 0x12FF2AF1F1E3F1DBULL, (u64bit) 0xA2E6CC7373D173BFULL, (u64bit) 0x5A24821212481290ULL, +(u64bit) 0x5D807A40401D403AULL, (u64bit) 0x2810480808200840ULL, (u64bit) 0xE89B95C3C32BC356ULL, (u64bit) 0x7BC5DFECEC97EC33ULL, +(u64bit) 0x90AB4DDBDB4BDB96ULL, (u64bit) 0x1F5FC0A1A1BEA161ULL, (u64bit) 0x8307918D8D0E8D1CULL, (u64bit) 0xC97AC83D3DF43DF5ULL, +(u64bit) 0xF1335B97976697CCULL, (u64bit) 0x0000000000000000ULL, (u64bit) 0xD483F9CFCF1BCF36ULL, (u64bit) 0x87566E2B2BAC2B45ULL, +(u64bit) 0xB3ECE17676C57697ULL, (u64bit) 0xB019E68282328264ULL, (u64bit) 0xA9B128D6D67FD6FEULL, (u64bit) 0x7736C31B1B6C1BD8ULL, +(u64bit) 0x5B7774B5B5EEB5C1ULL, (u64bit) 0x2943BEAFAF86AF11ULL, (u64bit) 0xDFD41D6A6AB56A77ULL, (u64bit) 0x0DA0EA50505D50BAULL, +(u64bit) 0x4C8A574545094512ULL, (u64bit) 0x18FB38F3F3EBF3CBULL, (u64bit) 0xF060AD3030C0309DULL, (u64bit) 0x74C3C4EFEF9BEF2BULL, +(u64bit) 0xC37EDA3F3FFC3FE5ULL, (u64bit) 0x1CAAC75555495592ULL, (u64bit) 0x1059DBA2A2B2A279ULL, (u64bit) 0x65C9E9EAEA8FEA03ULL, +(u64bit) 0xECCA6A656589650FULL, (u64bit) 0x686903BABAD2BAB9ULL, (u64bit) 0x935E4A2F2FBC2F65ULL, (u64bit) 0xE79D8EC0C027C04EULL, +(u64bit) 0x81A160DEDE5FDEBEULL, (u64bit) 0x6C38FC1C1C701CE0ULL, (u64bit) 0x2EE746FDFDD3FDBBULL, (u64bit) 0x649A1F4D4D294D52ULL, +(u64bit) 0xE0397692927292E4ULL, (u64bit) 0xBCEAFA7575C9758FULL, (u64bit) 0x1E0C360606180630ULL, (u64bit) 0x9809AE8A8A128A24ULL, +(u64bit) 0x40794BB2B2F2B2F9ULL, (u64bit) 0x59D185E6E6BFE663ULL, (u64bit) 0x361C7E0E0E380E70ULL, (u64bit) 0x633EE71F1F7C1FF8ULL, +(u64bit) 0xF7C4556262956237ULL, (u64bit) 0xA3B53AD4D477D4EEULL, (u64bit) 0x324D81A8A89AA829ULL, (u64bit) 0xF4315296966296C4ULL, +(u64bit) 0x3AEF62F9F9C3F99BULL, (u64bit) 0xF697A3C5C533C566ULL, (u64bit) 0xB14A102525942535ULL, (u64bit) 0x20B2AB59597959F2ULL, +(u64bit) 0xAE15D084842A8454ULL, (u64bit) 0xA7E4C57272D572B7ULL, (u64bit) 0xDD72EC3939E439D5ULL, (u64bit) 0x6198164C4C2D4C5AULL, +(u64bit) 0x3BBC945E5E655ECAULL, (u64bit) 0x85F09F7878FD78E7ULL, (u64bit) 0xD870E53838E038DDULL, (u64bit) 0x8605988C8C0A8C14ULL, +(u64bit) 0xB2BF17D1D163D1C6ULL, (u64bit) 0x0B57E4A5A5AEA541ULL, (u64bit) 0x4DD9A1E2E2AFE243ULL, (u64bit) 0xF8C24E616199612FULL, +(u64bit) 0x457B42B3B3F6B3F1ULL, (u64bit) 0xA542342121842115ULL, (u64bit) 0xD625089C9C4A9C94ULL, (u64bit) 0x663CEE1E1E781EF0ULL, +(u64bit) 0x5286614343114322ULL, (u64bit) 0xFC93B1C7C73BC776ULL, (u64bit) 0x2BE54FFCFCD7FCB3ULL, (u64bit) 0x1408240404100420ULL, +(u64bit) 0x08A2E351515951B2ULL, (u64bit) 0xC72F2599995E99BCULL, (u64bit) 0xC4DA226D6DA96D4FULL, (u64bit) 0x391A650D0D340D68ULL, +(u64bit) 0x35E979FAFACFFA83ULL, (u64bit) 0x84A369DFDF5BDFB6ULL, (u64bit) 0x9BFCA97E7EE57ED7ULL, (u64bit) 0xB44819242490243DULL, +(u64bit) 0xD776FE3B3BEC3BC5ULL, (u64bit) 0x3D4B9AABAB96AB31ULL, (u64bit) 0xD181F0CECE1FCE3EULL, (u64bit) 0x5522991111441188ULL, +(u64bit) 0x8903838F8F068F0CULL, (u64bit) 0x6B9C044E4E254E4AULL, (u64bit) 0x517366B7B7E6B7D1ULL, (u64bit) 0x60CBE0EBEB8BEB0BULL, +(u64bit) 0xCC78C13C3CF03CFDULL, (u64bit) 0xBF1FFD81813E817CULL, (u64bit) 0xFE354094946A94D4ULL, (u64bit) 0x0CF31CF7F7FBF7EBULL, +(u64bit) 0x676F18B9B9DEB9A1ULL, (u64bit) 0x5F268B13134C1398ULL, (u64bit) 0x9C58512C2CB02C7DULL, (u64bit) 0xB8BB05D3D36BD3D6ULL, +(u64bit) 0x5CD38CE7E7BBE76BULL, (u64bit) 0xCBDC396E6EA56E57ULL, (u64bit) 0xF395AAC4C437C46EULL, (u64bit) 0x0F061B03030C0318ULL, +(u64bit) 0x13ACDC565645568AULL, (u64bit) 0x49885E44440D441AULL, (u64bit) 0x9EFEA07F7FE17FDFULL, (u64bit) 0x374F88A9A99EA921ULL, +(u64bit) 0x8254672A2AA82A4DULL, (u64bit) 0x6D6B0ABBBBD6BBB1ULL, (u64bit) 0xE29F87C1C123C146ULL, (u64bit) 0x02A6F153535153A2ULL, +(u64bit) 0x8BA572DCDC57DCAEULL, (u64bit) 0x2716530B0B2C0B58ULL, (u64bit) 0xD327019D9D4E9D9CULL, (u64bit) 0xC1D82B6C6CAD6C47ULL, +(u64bit) 0xF562A43131C43195ULL, (u64bit) 0xB9E8F37474CD7487ULL, (u64bit) 0x09F115F6F6FFF6E3ULL, (u64bit) 0x438C4C464605460AULL, +(u64bit) 0x2645A5ACAC8AAC09ULL, (u64bit) 0x970FB589891E893CULL, (u64bit) 0x4428B414145014A0ULL, (u64bit) 0x42DFBAE1E1A3E15BULL, +(u64bit) 0x4E2CA616165816B0ULL, (u64bit) 0xD274F73A3AE83ACDULL, (u64bit) 0xD0D2066969B9696FULL, (u64bit) 0x2D12410909240948ULL, +(u64bit) 0xADE0D77070DD70A7ULL, (u64bit) 0x54716FB6B6E2B6D9ULL, (u64bit) 0xB7BD1ED0D067D0CEULL, (u64bit) 0x7EC7D6EDED93ED3BULL, +(u64bit) 0xDB85E2CCCC17CC2EULL, (u64bit) 0x578468424215422AULL, (u64bit) 0xC22D2C98985A98B4ULL, (u64bit) 0x0E55EDA4A4AAA449ULL, +(u64bit) 0x8850752828A0285DULL, (u64bit) 0x31B8865C5C6D5CDAULL, (u64bit) 0x3FED6BF8F8C7F893ULL, (u64bit) 0xA411C28686228644ULL }; + +const u64bit Whirlpool::C4[256] = { +(u64bit) 0xC07830D818186018ULL, (u64bit) 0x05AF462623238C23ULL, (u64bit) 0x7EF991B8C6C63FC6ULL, (u64bit) 0x136FCDFBE8E887E8ULL, +(u64bit) 0x4CA113CB87872687ULL, (u64bit) 0xA9626D11B8B8DAB8ULL, (u64bit) 0x0805020901010401ULL, (u64bit) 0x426E9E0D4F4F214FULL, +(u64bit) 0xADEE6C9B3636D836ULL, (u64bit) 0x590451FFA6A6A2A6ULL, (u64bit) 0xDEBDB90CD2D26FD2ULL, (u64bit) 0xFB06F70EF5F5F3F5ULL, +(u64bit) 0xEF80F2967979F979ULL, (u64bit) 0x5FCEDE306F6FA16FULL, (u64bit) 0xFCEF3F6D91917E91ULL, (u64bit) 0xAA07A4F852525552ULL, +(u64bit) 0x27FDC04760609D60ULL, (u64bit) 0x89766535BCBCCABCULL, (u64bit) 0xACCD2B379B9B569BULL, (u64bit) 0x048C018A8E8E028EULL, +(u64bit) 0x71155BD2A3A3B6A3ULL, (u64bit) 0x603C186C0C0C300CULL, (u64bit) 0xFF8AF6847B7BF17BULL, (u64bit) 0xB5E16A803535D435ULL, +(u64bit) 0xE8693AF51D1D741DULL, (u64bit) 0x5347DDB3E0E0A7E0ULL, (u64bit) 0xF6ACB321D7D77BD7ULL, (u64bit) 0x5EED999CC2C22FC2ULL, +(u64bit) 0x6D965C432E2EB82EULL, (u64bit) 0x627A96294B4B314BULL, (u64bit) 0xA321E15DFEFEDFFEULL, (u64bit) 0x8216AED557574157ULL, +(u64bit) 0xA8412ABD15155415ULL, (u64bit) 0x9FB6EEE87777C177ULL, (u64bit) 0xA5EB6E923737DC37ULL, (u64bit) 0x7B56D79EE5E5B3E5ULL, +(u64bit) 0x8CD923139F9F469FULL, (u64bit) 0xD317FD23F0F0E7F0ULL, (u64bit) 0x6A7F94204A4A354AULL, (u64bit) 0x9E95A944DADA4FDAULL, +(u64bit) 0xFA25B0A258587D58ULL, (u64bit) 0x06CA8FCFC9C903C9ULL, (u64bit) 0x558D527C2929A429ULL, (u64bit) 0x5022145A0A0A280AULL, +(u64bit) 0xE14F7F50B1B1FEB1ULL, (u64bit) 0x691A5DC9A0A0BAA0ULL, (u64bit) 0x7FDAD6146B6BB16BULL, (u64bit) 0x5CAB17D985852E85ULL, +(u64bit) 0x8173673CBDBDCEBDULL, (u64bit) 0xD234BA8F5D5D695DULL, (u64bit) 0x8050209010104010ULL, (u64bit) 0xF303F507F4F4F7F4ULL, +(u64bit) 0x16C08BDDCBCB0BCBULL, (u64bit) 0xEDC67CD33E3EF83EULL, (u64bit) 0x28110A2D05051405ULL, (u64bit) 0x1FE6CE7867678167ULL, +(u64bit) 0x7353D597E4E4B7E4ULL, (u64bit) 0x25BB4E0227279C27ULL, (u64bit) 0x3258827341411941ULL, (u64bit) 0x2C9D0BA78B8B168BULL, +(u64bit) 0x510153F6A7A7A6A7ULL, (u64bit) 0xCF94FAB27D7DE97DULL, (u64bit) 0xDCFB374995956E95ULL, (u64bit) 0x8E9FAD56D8D847D8ULL, +(u64bit) 0x8B30EB70FBFBCBFBULL, (u64bit) 0x2371C1CDEEEE9FEEULL, (u64bit) 0xC791F8BB7C7CED7CULL, (u64bit) 0x17E3CC7166668566ULL, +(u64bit) 0xA68EA77BDDDD53DDULL, (u64bit) 0xB84B2EAF17175C17ULL, (u64bit) 0x02468E4547470147ULL, (u64bit) 0x84DC211A9E9E429EULL, +(u64bit) 0x1EC589D4CACA0FCAULL, (u64bit) 0x75995A582D2DB42DULL, (u64bit) 0x9179632EBFBFC6BFULL, (u64bit) 0x381B0E3F07071C07ULL, +(u64bit) 0x012347ACADAD8EADULL, (u64bit) 0xEA2FB4B05A5A755AULL, (u64bit) 0x6CB51BEF83833683ULL, (u64bit) 0x85FF66B63333CC33ULL, +(u64bit) 0x3FF2C65C63639163ULL, (u64bit) 0x100A041202020802ULL, (u64bit) 0x39384993AAAA92AAULL, (u64bit) 0xAFA8E2DE7171D971ULL, +(u64bit) 0x0ECF8DC6C8C807C8ULL, (u64bit) 0xC87D32D119196419ULL, (u64bit) 0x7270923B49493949ULL, (u64bit) 0x869AAF5FD9D943D9ULL, +(u64bit) 0xC31DF931F2F2EFF2ULL, (u64bit) 0x4B48DBA8E3E3ABE3ULL, (u64bit) 0xE22AB6B95B5B715BULL, (u64bit) 0x34920DBC88881A88ULL, +(u64bit) 0xA4C8293E9A9A529AULL, (u64bit) 0x2DBE4C0B26269826ULL, (u64bit) 0x8DFA64BF3232C832ULL, (u64bit) 0xE94A7D59B0B0FAB0ULL, +(u64bit) 0x1B6ACFF2E9E983E9ULL, (u64bit) 0x78331E770F0F3C0FULL, (u64bit) 0xE6A6B733D5D573D5ULL, (u64bit) 0x74BA1DF480803A80ULL, +(u64bit) 0x997C6127BEBEC2BEULL, (u64bit) 0x26DE87EBCDCD13CDULL, (u64bit) 0xBDE468893434D034ULL, (u64bit) 0x7A75903248483D48ULL, +(u64bit) 0xAB24E354FFFFDBFFULL, (u64bit) 0xF78FF48D7A7AF57AULL, (u64bit) 0xF4EA3D6490907A90ULL, (u64bit) 0xC23EBE9D5F5F615FULL, +(u64bit) 0x1DA0403D20208020ULL, (u64bit) 0x67D5D00F6868BD68ULL, (u64bit) 0xD07234CA1A1A681AULL, (u64bit) 0x192C41B7AEAE82AEULL, +(u64bit) 0xC95E757DB4B4EAB4ULL, (u64bit) 0x9A19A8CE54544D54ULL, (u64bit) 0xECE53B7F93937693ULL, (u64bit) 0x0DAA442F22228822ULL, +(u64bit) 0x07E9C86364648D64ULL, (u64bit) 0xDB12FF2AF1F1E3F1ULL, (u64bit) 0xBFA2E6CC7373D173ULL, (u64bit) 0x905A248212124812ULL, +(u64bit) 0x3A5D807A40401D40ULL, (u64bit) 0x4028104808082008ULL, (u64bit) 0x56E89B95C3C32BC3ULL, (u64bit) 0x337BC5DFECEC97ECULL, +(u64bit) 0x9690AB4DDBDB4BDBULL, (u64bit) 0x611F5FC0A1A1BEA1ULL, (u64bit) 0x1C8307918D8D0E8DULL, (u64bit) 0xF5C97AC83D3DF43DULL, +(u64bit) 0xCCF1335B97976697ULL, (u64bit) 0x0000000000000000ULL, (u64bit) 0x36D483F9CFCF1BCFULL, (u64bit) 0x4587566E2B2BAC2BULL, +(u64bit) 0x97B3ECE17676C576ULL, (u64bit) 0x64B019E682823282ULL, (u64bit) 0xFEA9B128D6D67FD6ULL, (u64bit) 0xD87736C31B1B6C1BULL, +(u64bit) 0xC15B7774B5B5EEB5ULL, (u64bit) 0x112943BEAFAF86AFULL, (u64bit) 0x77DFD41D6A6AB56AULL, (u64bit) 0xBA0DA0EA50505D50ULL, +(u64bit) 0x124C8A5745450945ULL, (u64bit) 0xCB18FB38F3F3EBF3ULL, (u64bit) 0x9DF060AD3030C030ULL, (u64bit) 0x2B74C3C4EFEF9BEFULL, +(u64bit) 0xE5C37EDA3F3FFC3FULL, (u64bit) 0x921CAAC755554955ULL, (u64bit) 0x791059DBA2A2B2A2ULL, (u64bit) 0x0365C9E9EAEA8FEAULL, +(u64bit) 0x0FECCA6A65658965ULL, (u64bit) 0xB9686903BABAD2BAULL, (u64bit) 0x65935E4A2F2FBC2FULL, (u64bit) 0x4EE79D8EC0C027C0ULL, +(u64bit) 0xBE81A160DEDE5FDEULL, (u64bit) 0xE06C38FC1C1C701CULL, (u64bit) 0xBB2EE746FDFDD3FDULL, (u64bit) 0x52649A1F4D4D294DULL, +(u64bit) 0xE4E0397692927292ULL, (u64bit) 0x8FBCEAFA7575C975ULL, (u64bit) 0x301E0C3606061806ULL, (u64bit) 0x249809AE8A8A128AULL, +(u64bit) 0xF940794BB2B2F2B2ULL, (u64bit) 0x6359D185E6E6BFE6ULL, (u64bit) 0x70361C7E0E0E380EULL, (u64bit) 0xF8633EE71F1F7C1FULL, +(u64bit) 0x37F7C45562629562ULL, (u64bit) 0xEEA3B53AD4D477D4ULL, (u64bit) 0x29324D81A8A89AA8ULL, (u64bit) 0xC4F4315296966296ULL, +(u64bit) 0x9B3AEF62F9F9C3F9ULL, (u64bit) 0x66F697A3C5C533C5ULL, (u64bit) 0x35B14A1025259425ULL, (u64bit) 0xF220B2AB59597959ULL, +(u64bit) 0x54AE15D084842A84ULL, (u64bit) 0xB7A7E4C57272D572ULL, (u64bit) 0xD5DD72EC3939E439ULL, (u64bit) 0x5A6198164C4C2D4CULL, +(u64bit) 0xCA3BBC945E5E655EULL, (u64bit) 0xE785F09F7878FD78ULL, (u64bit) 0xDDD870E53838E038ULL, (u64bit) 0x148605988C8C0A8CULL, +(u64bit) 0xC6B2BF17D1D163D1ULL, (u64bit) 0x410B57E4A5A5AEA5ULL, (u64bit) 0x434DD9A1E2E2AFE2ULL, (u64bit) 0x2FF8C24E61619961ULL, +(u64bit) 0xF1457B42B3B3F6B3ULL, (u64bit) 0x15A5423421218421ULL, (u64bit) 0x94D625089C9C4A9CULL, (u64bit) 0xF0663CEE1E1E781EULL, +(u64bit) 0x2252866143431143ULL, (u64bit) 0x76FC93B1C7C73BC7ULL, (u64bit) 0xB32BE54FFCFCD7FCULL, (u64bit) 0x2014082404041004ULL, +(u64bit) 0xB208A2E351515951ULL, (u64bit) 0xBCC72F2599995E99ULL, (u64bit) 0x4FC4DA226D6DA96DULL, (u64bit) 0x68391A650D0D340DULL, +(u64bit) 0x8335E979FAFACFFAULL, (u64bit) 0xB684A369DFDF5BDFULL, (u64bit) 0xD79BFCA97E7EE57EULL, (u64bit) 0x3DB4481924249024ULL, +(u64bit) 0xC5D776FE3B3BEC3BULL, (u64bit) 0x313D4B9AABAB96ABULL, (u64bit) 0x3ED181F0CECE1FCEULL, (u64bit) 0x8855229911114411ULL, +(u64bit) 0x0C8903838F8F068FULL, (u64bit) 0x4A6B9C044E4E254EULL, (u64bit) 0xD1517366B7B7E6B7ULL, (u64bit) 0x0B60CBE0EBEB8BEBULL, +(u64bit) 0xFDCC78C13C3CF03CULL, (u64bit) 0x7CBF1FFD81813E81ULL, (u64bit) 0xD4FE354094946A94ULL, (u64bit) 0xEB0CF31CF7F7FBF7ULL, +(u64bit) 0xA1676F18B9B9DEB9ULL, (u64bit) 0x985F268B13134C13ULL, (u64bit) 0x7D9C58512C2CB02CULL, (u64bit) 0xD6B8BB05D3D36BD3ULL, +(u64bit) 0x6B5CD38CE7E7BBE7ULL, (u64bit) 0x57CBDC396E6EA56EULL, (u64bit) 0x6EF395AAC4C437C4ULL, (u64bit) 0x180F061B03030C03ULL, +(u64bit) 0x8A13ACDC56564556ULL, (u64bit) 0x1A49885E44440D44ULL, (u64bit) 0xDF9EFEA07F7FE17FULL, (u64bit) 0x21374F88A9A99EA9ULL, +(u64bit) 0x4D8254672A2AA82AULL, (u64bit) 0xB16D6B0ABBBBD6BBULL, (u64bit) 0x46E29F87C1C123C1ULL, (u64bit) 0xA202A6F153535153ULL, +(u64bit) 0xAE8BA572DCDC57DCULL, (u64bit) 0x582716530B0B2C0BULL, (u64bit) 0x9CD327019D9D4E9DULL, (u64bit) 0x47C1D82B6C6CAD6CULL, +(u64bit) 0x95F562A43131C431ULL, (u64bit) 0x87B9E8F37474CD74ULL, (u64bit) 0xE309F115F6F6FFF6ULL, (u64bit) 0x0A438C4C46460546ULL, +(u64bit) 0x092645A5ACAC8AACULL, (u64bit) 0x3C970FB589891E89ULL, (u64bit) 0xA04428B414145014ULL, (u64bit) 0x5B42DFBAE1E1A3E1ULL, +(u64bit) 0xB04E2CA616165816ULL, (u64bit) 0xCDD274F73A3AE83AULL, (u64bit) 0x6FD0D2066969B969ULL, (u64bit) 0x482D124109092409ULL, +(u64bit) 0xA7ADE0D77070DD70ULL, (u64bit) 0xD954716FB6B6E2B6ULL, (u64bit) 0xCEB7BD1ED0D067D0ULL, (u64bit) 0x3B7EC7D6EDED93EDULL, +(u64bit) 0x2EDB85E2CCCC17CCULL, (u64bit) 0x2A57846842421542ULL, (u64bit) 0xB4C22D2C98985A98ULL, (u64bit) 0x490E55EDA4A4AAA4ULL, +(u64bit) 0x5D8850752828A028ULL, (u64bit) 0xDA31B8865C5C6D5CULL, (u64bit) 0x933FED6BF8F8C7F8ULL, (u64bit) 0x44A411C286862286ULL }; + +const u64bit Whirlpool::C5[256] = { +(u64bit) 0x18C07830D8181860ULL, (u64bit) 0x2305AF462623238CULL, (u64bit) 0xC67EF991B8C6C63FULL, (u64bit) 0xE8136FCDFBE8E887ULL, +(u64bit) 0x874CA113CB878726ULL, (u64bit) 0xB8A9626D11B8B8DAULL, (u64bit) 0x0108050209010104ULL, (u64bit) 0x4F426E9E0D4F4F21ULL, +(u64bit) 0x36ADEE6C9B3636D8ULL, (u64bit) 0xA6590451FFA6A6A2ULL, (u64bit) 0xD2DEBDB90CD2D26FULL, (u64bit) 0xF5FB06F70EF5F5F3ULL, +(u64bit) 0x79EF80F2967979F9ULL, (u64bit) 0x6F5FCEDE306F6FA1ULL, (u64bit) 0x91FCEF3F6D91917EULL, (u64bit) 0x52AA07A4F8525255ULL, +(u64bit) 0x6027FDC04760609DULL, (u64bit) 0xBC89766535BCBCCAULL, (u64bit) 0x9BACCD2B379B9B56ULL, (u64bit) 0x8E048C018A8E8E02ULL, +(u64bit) 0xA371155BD2A3A3B6ULL, (u64bit) 0x0C603C186C0C0C30ULL, (u64bit) 0x7BFF8AF6847B7BF1ULL, (u64bit) 0x35B5E16A803535D4ULL, +(u64bit) 0x1DE8693AF51D1D74ULL, (u64bit) 0xE05347DDB3E0E0A7ULL, (u64bit) 0xD7F6ACB321D7D77BULL, (u64bit) 0xC25EED999CC2C22FULL, +(u64bit) 0x2E6D965C432E2EB8ULL, (u64bit) 0x4B627A96294B4B31ULL, (u64bit) 0xFEA321E15DFEFEDFULL, (u64bit) 0x578216AED5575741ULL, +(u64bit) 0x15A8412ABD151554ULL, (u64bit) 0x779FB6EEE87777C1ULL, (u64bit) 0x37A5EB6E923737DCULL, (u64bit) 0xE57B56D79EE5E5B3ULL, +(u64bit) 0x9F8CD923139F9F46ULL, (u64bit) 0xF0D317FD23F0F0E7ULL, (u64bit) 0x4A6A7F94204A4A35ULL, (u64bit) 0xDA9E95A944DADA4FULL, +(u64bit) 0x58FA25B0A258587DULL, (u64bit) 0xC906CA8FCFC9C903ULL, (u64bit) 0x29558D527C2929A4ULL, (u64bit) 0x0A5022145A0A0A28ULL, +(u64bit) 0xB1E14F7F50B1B1FEULL, (u64bit) 0xA0691A5DC9A0A0BAULL, (u64bit) 0x6B7FDAD6146B6BB1ULL, (u64bit) 0x855CAB17D985852EULL, +(u64bit) 0xBD8173673CBDBDCEULL, (u64bit) 0x5DD234BA8F5D5D69ULL, (u64bit) 0x1080502090101040ULL, (u64bit) 0xF4F303F507F4F4F7ULL, +(u64bit) 0xCB16C08BDDCBCB0BULL, (u64bit) 0x3EEDC67CD33E3EF8ULL, (u64bit) 0x0528110A2D050514ULL, (u64bit) 0x671FE6CE78676781ULL, +(u64bit) 0xE47353D597E4E4B7ULL, (u64bit) 0x2725BB4E0227279CULL, (u64bit) 0x4132588273414119ULL, (u64bit) 0x8B2C9D0BA78B8B16ULL, +(u64bit) 0xA7510153F6A7A7A6ULL, (u64bit) 0x7DCF94FAB27D7DE9ULL, (u64bit) 0x95DCFB374995956EULL, (u64bit) 0xD88E9FAD56D8D847ULL, +(u64bit) 0xFB8B30EB70FBFBCBULL, (u64bit) 0xEE2371C1CDEEEE9FULL, (u64bit) 0x7CC791F8BB7C7CEDULL, (u64bit) 0x6617E3CC71666685ULL, +(u64bit) 0xDDA68EA77BDDDD53ULL, (u64bit) 0x17B84B2EAF17175CULL, (u64bit) 0x4702468E45474701ULL, (u64bit) 0x9E84DC211A9E9E42ULL, +(u64bit) 0xCA1EC589D4CACA0FULL, (u64bit) 0x2D75995A582D2DB4ULL, (u64bit) 0xBF9179632EBFBFC6ULL, (u64bit) 0x07381B0E3F07071CULL, +(u64bit) 0xAD012347ACADAD8EULL, (u64bit) 0x5AEA2FB4B05A5A75ULL, (u64bit) 0x836CB51BEF838336ULL, (u64bit) 0x3385FF66B63333CCULL, +(u64bit) 0x633FF2C65C636391ULL, (u64bit) 0x02100A0412020208ULL, (u64bit) 0xAA39384993AAAA92ULL, (u64bit) 0x71AFA8E2DE7171D9ULL, +(u64bit) 0xC80ECF8DC6C8C807ULL, (u64bit) 0x19C87D32D1191964ULL, (u64bit) 0x497270923B494939ULL, (u64bit) 0xD9869AAF5FD9D943ULL, +(u64bit) 0xF2C31DF931F2F2EFULL, (u64bit) 0xE34B48DBA8E3E3ABULL, (u64bit) 0x5BE22AB6B95B5B71ULL, (u64bit) 0x8834920DBC88881AULL, +(u64bit) 0x9AA4C8293E9A9A52ULL, (u64bit) 0x262DBE4C0B262698ULL, (u64bit) 0x328DFA64BF3232C8ULL, (u64bit) 0xB0E94A7D59B0B0FAULL, +(u64bit) 0xE91B6ACFF2E9E983ULL, (u64bit) 0x0F78331E770F0F3CULL, (u64bit) 0xD5E6A6B733D5D573ULL, (u64bit) 0x8074BA1DF480803AULL, +(u64bit) 0xBE997C6127BEBEC2ULL, (u64bit) 0xCD26DE87EBCDCD13ULL, (u64bit) 0x34BDE468893434D0ULL, (u64bit) 0x487A75903248483DULL, +(u64bit) 0xFFAB24E354FFFFDBULL, (u64bit) 0x7AF78FF48D7A7AF5ULL, (u64bit) 0x90F4EA3D6490907AULL, (u64bit) 0x5FC23EBE9D5F5F61ULL, +(u64bit) 0x201DA0403D202080ULL, (u64bit) 0x6867D5D00F6868BDULL, (u64bit) 0x1AD07234CA1A1A68ULL, (u64bit) 0xAE192C41B7AEAE82ULL, +(u64bit) 0xB4C95E757DB4B4EAULL, (u64bit) 0x549A19A8CE54544DULL, (u64bit) 0x93ECE53B7F939376ULL, (u64bit) 0x220DAA442F222288ULL, +(u64bit) 0x6407E9C86364648DULL, (u64bit) 0xF1DB12FF2AF1F1E3ULL, (u64bit) 0x73BFA2E6CC7373D1ULL, (u64bit) 0x12905A2482121248ULL, +(u64bit) 0x403A5D807A40401DULL, (u64bit) 0x0840281048080820ULL, (u64bit) 0xC356E89B95C3C32BULL, (u64bit) 0xEC337BC5DFECEC97ULL, +(u64bit) 0xDB9690AB4DDBDB4BULL, (u64bit) 0xA1611F5FC0A1A1BEULL, (u64bit) 0x8D1C8307918D8D0EULL, (u64bit) 0x3DF5C97AC83D3DF4ULL, +(u64bit) 0x97CCF1335B979766ULL, (u64bit) 0x0000000000000000ULL, (u64bit) 0xCF36D483F9CFCF1BULL, (u64bit) 0x2B4587566E2B2BACULL, +(u64bit) 0x7697B3ECE17676C5ULL, (u64bit) 0x8264B019E6828232ULL, (u64bit) 0xD6FEA9B128D6D67FULL, (u64bit) 0x1BD87736C31B1B6CULL, +(u64bit) 0xB5C15B7774B5B5EEULL, (u64bit) 0xAF112943BEAFAF86ULL, (u64bit) 0x6A77DFD41D6A6AB5ULL, (u64bit) 0x50BA0DA0EA50505DULL, +(u64bit) 0x45124C8A57454509ULL, (u64bit) 0xF3CB18FB38F3F3EBULL, (u64bit) 0x309DF060AD3030C0ULL, (u64bit) 0xEF2B74C3C4EFEF9BULL, +(u64bit) 0x3FE5C37EDA3F3FFCULL, (u64bit) 0x55921CAAC7555549ULL, (u64bit) 0xA2791059DBA2A2B2ULL, (u64bit) 0xEA0365C9E9EAEA8FULL, +(u64bit) 0x650FECCA6A656589ULL, (u64bit) 0xBAB9686903BABAD2ULL, (u64bit) 0x2F65935E4A2F2FBCULL, (u64bit) 0xC04EE79D8EC0C027ULL, +(u64bit) 0xDEBE81A160DEDE5FULL, (u64bit) 0x1CE06C38FC1C1C70ULL, (u64bit) 0xFDBB2EE746FDFDD3ULL, (u64bit) 0x4D52649A1F4D4D29ULL, +(u64bit) 0x92E4E03976929272ULL, (u64bit) 0x758FBCEAFA7575C9ULL, (u64bit) 0x06301E0C36060618ULL, (u64bit) 0x8A249809AE8A8A12ULL, +(u64bit) 0xB2F940794BB2B2F2ULL, (u64bit) 0xE66359D185E6E6BFULL, (u64bit) 0x0E70361C7E0E0E38ULL, (u64bit) 0x1FF8633EE71F1F7CULL, +(u64bit) 0x6237F7C455626295ULL, (u64bit) 0xD4EEA3B53AD4D477ULL, (u64bit) 0xA829324D81A8A89AULL, (u64bit) 0x96C4F43152969662ULL, +(u64bit) 0xF99B3AEF62F9F9C3ULL, (u64bit) 0xC566F697A3C5C533ULL, (u64bit) 0x2535B14A10252594ULL, (u64bit) 0x59F220B2AB595979ULL, +(u64bit) 0x8454AE15D084842AULL, (u64bit) 0x72B7A7E4C57272D5ULL, (u64bit) 0x39D5DD72EC3939E4ULL, (u64bit) 0x4C5A6198164C4C2DULL, +(u64bit) 0x5ECA3BBC945E5E65ULL, (u64bit) 0x78E785F09F7878FDULL, (u64bit) 0x38DDD870E53838E0ULL, (u64bit) 0x8C148605988C8C0AULL, +(u64bit) 0xD1C6B2BF17D1D163ULL, (u64bit) 0xA5410B57E4A5A5AEULL, (u64bit) 0xE2434DD9A1E2E2AFULL, (u64bit) 0x612FF8C24E616199ULL, +(u64bit) 0xB3F1457B42B3B3F6ULL, (u64bit) 0x2115A54234212184ULL, (u64bit) 0x9C94D625089C9C4AULL, (u64bit) 0x1EF0663CEE1E1E78ULL, +(u64bit) 0x4322528661434311ULL, (u64bit) 0xC776FC93B1C7C73BULL, (u64bit) 0xFCB32BE54FFCFCD7ULL, (u64bit) 0x0420140824040410ULL, +(u64bit) 0x51B208A2E3515159ULL, (u64bit) 0x99BCC72F2599995EULL, (u64bit) 0x6D4FC4DA226D6DA9ULL, (u64bit) 0x0D68391A650D0D34ULL, +(u64bit) 0xFA8335E979FAFACFULL, (u64bit) 0xDFB684A369DFDF5BULL, (u64bit) 0x7ED79BFCA97E7EE5ULL, (u64bit) 0x243DB44819242490ULL, +(u64bit) 0x3BC5D776FE3B3BECULL, (u64bit) 0xAB313D4B9AABAB96ULL, (u64bit) 0xCE3ED181F0CECE1FULL, (u64bit) 0x1188552299111144ULL, +(u64bit) 0x8F0C8903838F8F06ULL, (u64bit) 0x4E4A6B9C044E4E25ULL, (u64bit) 0xB7D1517366B7B7E6ULL, (u64bit) 0xEB0B60CBE0EBEB8BULL, +(u64bit) 0x3CFDCC78C13C3CF0ULL, (u64bit) 0x817CBF1FFD81813EULL, (u64bit) 0x94D4FE354094946AULL, (u64bit) 0xF7EB0CF31CF7F7FBULL, +(u64bit) 0xB9A1676F18B9B9DEULL, (u64bit) 0x13985F268B13134CULL, (u64bit) 0x2C7D9C58512C2CB0ULL, (u64bit) 0xD3D6B8BB05D3D36BULL, +(u64bit) 0xE76B5CD38CE7E7BBULL, (u64bit) 0x6E57CBDC396E6EA5ULL, (u64bit) 0xC46EF395AAC4C437ULL, (u64bit) 0x03180F061B03030CULL, +(u64bit) 0x568A13ACDC565645ULL, (u64bit) 0x441A49885E44440DULL, (u64bit) 0x7FDF9EFEA07F7FE1ULL, (u64bit) 0xA921374F88A9A99EULL, +(u64bit) 0x2A4D8254672A2AA8ULL, (u64bit) 0xBBB16D6B0ABBBBD6ULL, (u64bit) 0xC146E29F87C1C123ULL, (u64bit) 0x53A202A6F1535351ULL, +(u64bit) 0xDCAE8BA572DCDC57ULL, (u64bit) 0x0B582716530B0B2CULL, (u64bit) 0x9D9CD327019D9D4EULL, (u64bit) 0x6C47C1D82B6C6CADULL, +(u64bit) 0x3195F562A43131C4ULL, (u64bit) 0x7487B9E8F37474CDULL, (u64bit) 0xF6E309F115F6F6FFULL, (u64bit) 0x460A438C4C464605ULL, +(u64bit) 0xAC092645A5ACAC8AULL, (u64bit) 0x893C970FB589891EULL, (u64bit) 0x14A04428B4141450ULL, (u64bit) 0xE15B42DFBAE1E1A3ULL, +(u64bit) 0x16B04E2CA6161658ULL, (u64bit) 0x3ACDD274F73A3AE8ULL, (u64bit) 0x696FD0D2066969B9ULL, (u64bit) 0x09482D1241090924ULL, +(u64bit) 0x70A7ADE0D77070DDULL, (u64bit) 0xB6D954716FB6B6E2ULL, (u64bit) 0xD0CEB7BD1ED0D067ULL, (u64bit) 0xED3B7EC7D6EDED93ULL, +(u64bit) 0xCC2EDB85E2CCCC17ULL, (u64bit) 0x422A578468424215ULL, (u64bit) 0x98B4C22D2C98985AULL, (u64bit) 0xA4490E55EDA4A4AAULL, +(u64bit) 0x285D8850752828A0ULL, (u64bit) 0x5CDA31B8865C5C6DULL, (u64bit) 0xF8933FED6BF8F8C7ULL, (u64bit) 0x8644A411C2868622ULL }; + +const u64bit Whirlpool::C6[256] = { +(u64bit) 0x6018C07830D81818ULL, (u64bit) 0x8C2305AF46262323ULL, (u64bit) 0x3FC67EF991B8C6C6ULL, (u64bit) 0x87E8136FCDFBE8E8ULL, +(u64bit) 0x26874CA113CB8787ULL, (u64bit) 0xDAB8A9626D11B8B8ULL, (u64bit) 0x0401080502090101ULL, (u64bit) 0x214F426E9E0D4F4FULL, +(u64bit) 0xD836ADEE6C9B3636ULL, (u64bit) 0xA2A6590451FFA6A6ULL, (u64bit) 0x6FD2DEBDB90CD2D2ULL, (u64bit) 0xF3F5FB06F70EF5F5ULL, +(u64bit) 0xF979EF80F2967979ULL, (u64bit) 0xA16F5FCEDE306F6FULL, (u64bit) 0x7E91FCEF3F6D9191ULL, (u64bit) 0x5552AA07A4F85252ULL, +(u64bit) 0x9D6027FDC0476060ULL, (u64bit) 0xCABC89766535BCBCULL, (u64bit) 0x569BACCD2B379B9BULL, (u64bit) 0x028E048C018A8E8EULL, +(u64bit) 0xB6A371155BD2A3A3ULL, (u64bit) 0x300C603C186C0C0CULL, (u64bit) 0xF17BFF8AF6847B7BULL, (u64bit) 0xD435B5E16A803535ULL, +(u64bit) 0x741DE8693AF51D1DULL, (u64bit) 0xA7E05347DDB3E0E0ULL, (u64bit) 0x7BD7F6ACB321D7D7ULL, (u64bit) 0x2FC25EED999CC2C2ULL, +(u64bit) 0xB82E6D965C432E2EULL, (u64bit) 0x314B627A96294B4BULL, (u64bit) 0xDFFEA321E15DFEFEULL, (u64bit) 0x41578216AED55757ULL, +(u64bit) 0x5415A8412ABD1515ULL, (u64bit) 0xC1779FB6EEE87777ULL, (u64bit) 0xDC37A5EB6E923737ULL, (u64bit) 0xB3E57B56D79EE5E5ULL, +(u64bit) 0x469F8CD923139F9FULL, (u64bit) 0xE7F0D317FD23F0F0ULL, (u64bit) 0x354A6A7F94204A4AULL, (u64bit) 0x4FDA9E95A944DADAULL, +(u64bit) 0x7D58FA25B0A25858ULL, (u64bit) 0x03C906CA8FCFC9C9ULL, (u64bit) 0xA429558D527C2929ULL, (u64bit) 0x280A5022145A0A0AULL, +(u64bit) 0xFEB1E14F7F50B1B1ULL, (u64bit) 0xBAA0691A5DC9A0A0ULL, (u64bit) 0xB16B7FDAD6146B6BULL, (u64bit) 0x2E855CAB17D98585ULL, +(u64bit) 0xCEBD8173673CBDBDULL, (u64bit) 0x695DD234BA8F5D5DULL, (u64bit) 0x4010805020901010ULL, (u64bit) 0xF7F4F303F507F4F4ULL, +(u64bit) 0x0BCB16C08BDDCBCBULL, (u64bit) 0xF83EEDC67CD33E3EULL, (u64bit) 0x140528110A2D0505ULL, (u64bit) 0x81671FE6CE786767ULL, +(u64bit) 0xB7E47353D597E4E4ULL, (u64bit) 0x9C2725BB4E022727ULL, (u64bit) 0x1941325882734141ULL, (u64bit) 0x168B2C9D0BA78B8BULL, +(u64bit) 0xA6A7510153F6A7A7ULL, (u64bit) 0xE97DCF94FAB27D7DULL, (u64bit) 0x6E95DCFB37499595ULL, (u64bit) 0x47D88E9FAD56D8D8ULL, +(u64bit) 0xCBFB8B30EB70FBFBULL, (u64bit) 0x9FEE2371C1CDEEEEULL, (u64bit) 0xED7CC791F8BB7C7CULL, (u64bit) 0x856617E3CC716666ULL, +(u64bit) 0x53DDA68EA77BDDDDULL, (u64bit) 0x5C17B84B2EAF1717ULL, (u64bit) 0x014702468E454747ULL, (u64bit) 0x429E84DC211A9E9EULL, +(u64bit) 0x0FCA1EC589D4CACAULL, (u64bit) 0xB42D75995A582D2DULL, (u64bit) 0xC6BF9179632EBFBFULL, (u64bit) 0x1C07381B0E3F0707ULL, +(u64bit) 0x8EAD012347ACADADULL, (u64bit) 0x755AEA2FB4B05A5AULL, (u64bit) 0x36836CB51BEF8383ULL, (u64bit) 0xCC3385FF66B63333ULL, +(u64bit) 0x91633FF2C65C6363ULL, (u64bit) 0x0802100A04120202ULL, (u64bit) 0x92AA39384993AAAAULL, (u64bit) 0xD971AFA8E2DE7171ULL, +(u64bit) 0x07C80ECF8DC6C8C8ULL, (u64bit) 0x6419C87D32D11919ULL, (u64bit) 0x39497270923B4949ULL, (u64bit) 0x43D9869AAF5FD9D9ULL, +(u64bit) 0xEFF2C31DF931F2F2ULL, (u64bit) 0xABE34B48DBA8E3E3ULL, (u64bit) 0x715BE22AB6B95B5BULL, (u64bit) 0x1A8834920DBC8888ULL, +(u64bit) 0x529AA4C8293E9A9AULL, (u64bit) 0x98262DBE4C0B2626ULL, (u64bit) 0xC8328DFA64BF3232ULL, (u64bit) 0xFAB0E94A7D59B0B0ULL, +(u64bit) 0x83E91B6ACFF2E9E9ULL, (u64bit) 0x3C0F78331E770F0FULL, (u64bit) 0x73D5E6A6B733D5D5ULL, (u64bit) 0x3A8074BA1DF48080ULL, +(u64bit) 0xC2BE997C6127BEBEULL, (u64bit) 0x13CD26DE87EBCDCDULL, (u64bit) 0xD034BDE468893434ULL, (u64bit) 0x3D487A7590324848ULL, +(u64bit) 0xDBFFAB24E354FFFFULL, (u64bit) 0xF57AF78FF48D7A7AULL, (u64bit) 0x7A90F4EA3D649090ULL, (u64bit) 0x615FC23EBE9D5F5FULL, +(u64bit) 0x80201DA0403D2020ULL, (u64bit) 0xBD6867D5D00F6868ULL, (u64bit) 0x681AD07234CA1A1AULL, (u64bit) 0x82AE192C41B7AEAEULL, +(u64bit) 0xEAB4C95E757DB4B4ULL, (u64bit) 0x4D549A19A8CE5454ULL, (u64bit) 0x7693ECE53B7F9393ULL, (u64bit) 0x88220DAA442F2222ULL, +(u64bit) 0x8D6407E9C8636464ULL, (u64bit) 0xE3F1DB12FF2AF1F1ULL, (u64bit) 0xD173BFA2E6CC7373ULL, (u64bit) 0x4812905A24821212ULL, +(u64bit) 0x1D403A5D807A4040ULL, (u64bit) 0x2008402810480808ULL, (u64bit) 0x2BC356E89B95C3C3ULL, (u64bit) 0x97EC337BC5DFECECULL, +(u64bit) 0x4BDB9690AB4DDBDBULL, (u64bit) 0xBEA1611F5FC0A1A1ULL, (u64bit) 0x0E8D1C8307918D8DULL, (u64bit) 0xF43DF5C97AC83D3DULL, +(u64bit) 0x6697CCF1335B9797ULL, (u64bit) 0x0000000000000000ULL, (u64bit) 0x1BCF36D483F9CFCFULL, (u64bit) 0xAC2B4587566E2B2BULL, +(u64bit) 0xC57697B3ECE17676ULL, (u64bit) 0x328264B019E68282ULL, (u64bit) 0x7FD6FEA9B128D6D6ULL, (u64bit) 0x6C1BD87736C31B1BULL, +(u64bit) 0xEEB5C15B7774B5B5ULL, (u64bit) 0x86AF112943BEAFAFULL, (u64bit) 0xB56A77DFD41D6A6AULL, (u64bit) 0x5D50BA0DA0EA5050ULL, +(u64bit) 0x0945124C8A574545ULL, (u64bit) 0xEBF3CB18FB38F3F3ULL, (u64bit) 0xC0309DF060AD3030ULL, (u64bit) 0x9BEF2B74C3C4EFEFULL, +(u64bit) 0xFC3FE5C37EDA3F3FULL, (u64bit) 0x4955921CAAC75555ULL, (u64bit) 0xB2A2791059DBA2A2ULL, (u64bit) 0x8FEA0365C9E9EAEAULL, +(u64bit) 0x89650FECCA6A6565ULL, (u64bit) 0xD2BAB9686903BABAULL, (u64bit) 0xBC2F65935E4A2F2FULL, (u64bit) 0x27C04EE79D8EC0C0ULL, +(u64bit) 0x5FDEBE81A160DEDEULL, (u64bit) 0x701CE06C38FC1C1CULL, (u64bit) 0xD3FDBB2EE746FDFDULL, (u64bit) 0x294D52649A1F4D4DULL, +(u64bit) 0x7292E4E039769292ULL, (u64bit) 0xC9758FBCEAFA7575ULL, (u64bit) 0x1806301E0C360606ULL, (u64bit) 0x128A249809AE8A8AULL, +(u64bit) 0xF2B2F940794BB2B2ULL, (u64bit) 0xBFE66359D185E6E6ULL, (u64bit) 0x380E70361C7E0E0EULL, (u64bit) 0x7C1FF8633EE71F1FULL, +(u64bit) 0x956237F7C4556262ULL, (u64bit) 0x77D4EEA3B53AD4D4ULL, (u64bit) 0x9AA829324D81A8A8ULL, (u64bit) 0x6296C4F431529696ULL, +(u64bit) 0xC3F99B3AEF62F9F9ULL, (u64bit) 0x33C566F697A3C5C5ULL, (u64bit) 0x942535B14A102525ULL, (u64bit) 0x7959F220B2AB5959ULL, +(u64bit) 0x2A8454AE15D08484ULL, (u64bit) 0xD572B7A7E4C57272ULL, (u64bit) 0xE439D5DD72EC3939ULL, (u64bit) 0x2D4C5A6198164C4CULL, +(u64bit) 0x655ECA3BBC945E5EULL, (u64bit) 0xFD78E785F09F7878ULL, (u64bit) 0xE038DDD870E53838ULL, (u64bit) 0x0A8C148605988C8CULL, +(u64bit) 0x63D1C6B2BF17D1D1ULL, (u64bit) 0xAEA5410B57E4A5A5ULL, (u64bit) 0xAFE2434DD9A1E2E2ULL, (u64bit) 0x99612FF8C24E6161ULL, +(u64bit) 0xF6B3F1457B42B3B3ULL, (u64bit) 0x842115A542342121ULL, (u64bit) 0x4A9C94D625089C9CULL, (u64bit) 0x781EF0663CEE1E1EULL, +(u64bit) 0x1143225286614343ULL, (u64bit) 0x3BC776FC93B1C7C7ULL, (u64bit) 0xD7FCB32BE54FFCFCULL, (u64bit) 0x1004201408240404ULL, +(u64bit) 0x5951B208A2E35151ULL, (u64bit) 0x5E99BCC72F259999ULL, (u64bit) 0xA96D4FC4DA226D6DULL, (u64bit) 0x340D68391A650D0DULL, +(u64bit) 0xCFFA8335E979FAFAULL, (u64bit) 0x5BDFB684A369DFDFULL, (u64bit) 0xE57ED79BFCA97E7EULL, (u64bit) 0x90243DB448192424ULL, +(u64bit) 0xEC3BC5D776FE3B3BULL, (u64bit) 0x96AB313D4B9AABABULL, (u64bit) 0x1FCE3ED181F0CECEULL, (u64bit) 0x4411885522991111ULL, +(u64bit) 0x068F0C8903838F8FULL, (u64bit) 0x254E4A6B9C044E4EULL, (u64bit) 0xE6B7D1517366B7B7ULL, (u64bit) 0x8BEB0B60CBE0EBEBULL, +(u64bit) 0xF03CFDCC78C13C3CULL, (u64bit) 0x3E817CBF1FFD8181ULL, (u64bit) 0x6A94D4FE35409494ULL, (u64bit) 0xFBF7EB0CF31CF7F7ULL, +(u64bit) 0xDEB9A1676F18B9B9ULL, (u64bit) 0x4C13985F268B1313ULL, (u64bit) 0xB02C7D9C58512C2CULL, (u64bit) 0x6BD3D6B8BB05D3D3ULL, +(u64bit) 0xBBE76B5CD38CE7E7ULL, (u64bit) 0xA56E57CBDC396E6EULL, (u64bit) 0x37C46EF395AAC4C4ULL, (u64bit) 0x0C03180F061B0303ULL, +(u64bit) 0x45568A13ACDC5656ULL, (u64bit) 0x0D441A49885E4444ULL, (u64bit) 0xE17FDF9EFEA07F7FULL, (u64bit) 0x9EA921374F88A9A9ULL, +(u64bit) 0xA82A4D8254672A2AULL, (u64bit) 0xD6BBB16D6B0ABBBBULL, (u64bit) 0x23C146E29F87C1C1ULL, (u64bit) 0x5153A202A6F15353ULL, +(u64bit) 0x57DCAE8BA572DCDCULL, (u64bit) 0x2C0B582716530B0BULL, (u64bit) 0x4E9D9CD327019D9DULL, (u64bit) 0xAD6C47C1D82B6C6CULL, +(u64bit) 0xC43195F562A43131ULL, (u64bit) 0xCD7487B9E8F37474ULL, (u64bit) 0xFFF6E309F115F6F6ULL, (u64bit) 0x05460A438C4C4646ULL, +(u64bit) 0x8AAC092645A5ACACULL, (u64bit) 0x1E893C970FB58989ULL, (u64bit) 0x5014A04428B41414ULL, (u64bit) 0xA3E15B42DFBAE1E1ULL, +(u64bit) 0x5816B04E2CA61616ULL, (u64bit) 0xE83ACDD274F73A3AULL, (u64bit) 0xB9696FD0D2066969ULL, (u64bit) 0x2409482D12410909ULL, +(u64bit) 0xDD70A7ADE0D77070ULL, (u64bit) 0xE2B6D954716FB6B6ULL, (u64bit) 0x67D0CEB7BD1ED0D0ULL, (u64bit) 0x93ED3B7EC7D6EDEDULL, +(u64bit) 0x17CC2EDB85E2CCCCULL, (u64bit) 0x15422A5784684242ULL, (u64bit) 0x5A98B4C22D2C9898ULL, (u64bit) 0xAAA4490E55EDA4A4ULL, +(u64bit) 0xA0285D8850752828ULL, (u64bit) 0x6D5CDA31B8865C5CULL, (u64bit) 0xC7F8933FED6BF8F8ULL, (u64bit) 0x228644A411C28686ULL }; + +const u64bit Whirlpool::C7[256] = { +(u64bit) 0x186018C07830D818ULL, (u64bit) 0x238C2305AF462623ULL, (u64bit) 0xC63FC67EF991B8C6ULL, (u64bit) 0xE887E8136FCDFBE8ULL, +(u64bit) 0x8726874CA113CB87ULL, (u64bit) 0xB8DAB8A9626D11B8ULL, (u64bit) 0x0104010805020901ULL, (u64bit) 0x4F214F426E9E0D4FULL, +(u64bit) 0x36D836ADEE6C9B36ULL, (u64bit) 0xA6A2A6590451FFA6ULL, (u64bit) 0xD26FD2DEBDB90CD2ULL, (u64bit) 0xF5F3F5FB06F70EF5ULL, +(u64bit) 0x79F979EF80F29679ULL, (u64bit) 0x6FA16F5FCEDE306FULL, (u64bit) 0x917E91FCEF3F6D91ULL, (u64bit) 0x525552AA07A4F852ULL, +(u64bit) 0x609D6027FDC04760ULL, (u64bit) 0xBCCABC89766535BCULL, (u64bit) 0x9B569BACCD2B379BULL, (u64bit) 0x8E028E048C018A8EULL, +(u64bit) 0xA3B6A371155BD2A3ULL, (u64bit) 0x0C300C603C186C0CULL, (u64bit) 0x7BF17BFF8AF6847BULL, (u64bit) 0x35D435B5E16A8035ULL, +(u64bit) 0x1D741DE8693AF51DULL, (u64bit) 0xE0A7E05347DDB3E0ULL, (u64bit) 0xD77BD7F6ACB321D7ULL, (u64bit) 0xC22FC25EED999CC2ULL, +(u64bit) 0x2EB82E6D965C432EULL, (u64bit) 0x4B314B627A96294BULL, (u64bit) 0xFEDFFEA321E15DFEULL, (u64bit) 0x5741578216AED557ULL, +(u64bit) 0x155415A8412ABD15ULL, (u64bit) 0x77C1779FB6EEE877ULL, (u64bit) 0x37DC37A5EB6E9237ULL, (u64bit) 0xE5B3E57B56D79EE5ULL, +(u64bit) 0x9F469F8CD923139FULL, (u64bit) 0xF0E7F0D317FD23F0ULL, (u64bit) 0x4A354A6A7F94204AULL, (u64bit) 0xDA4FDA9E95A944DAULL, +(u64bit) 0x587D58FA25B0A258ULL, (u64bit) 0xC903C906CA8FCFC9ULL, (u64bit) 0x29A429558D527C29ULL, (u64bit) 0x0A280A5022145A0AULL, +(u64bit) 0xB1FEB1E14F7F50B1ULL, (u64bit) 0xA0BAA0691A5DC9A0ULL, (u64bit) 0x6BB16B7FDAD6146BULL, (u64bit) 0x852E855CAB17D985ULL, +(u64bit) 0xBDCEBD8173673CBDULL, (u64bit) 0x5D695DD234BA8F5DULL, (u64bit) 0x1040108050209010ULL, (u64bit) 0xF4F7F4F303F507F4ULL, +(u64bit) 0xCB0BCB16C08BDDCBULL, (u64bit) 0x3EF83EEDC67CD33EULL, (u64bit) 0x05140528110A2D05ULL, (u64bit) 0x6781671FE6CE7867ULL, +(u64bit) 0xE4B7E47353D597E4ULL, (u64bit) 0x279C2725BB4E0227ULL, (u64bit) 0x4119413258827341ULL, (u64bit) 0x8B168B2C9D0BA78BULL, +(u64bit) 0xA7A6A7510153F6A7ULL, (u64bit) 0x7DE97DCF94FAB27DULL, (u64bit) 0x956E95DCFB374995ULL, (u64bit) 0xD847D88E9FAD56D8ULL, +(u64bit) 0xFBCBFB8B30EB70FBULL, (u64bit) 0xEE9FEE2371C1CDEEULL, (u64bit) 0x7CED7CC791F8BB7CULL, (u64bit) 0x66856617E3CC7166ULL, +(u64bit) 0xDD53DDA68EA77BDDULL, (u64bit) 0x175C17B84B2EAF17ULL, (u64bit) 0x47014702468E4547ULL, (u64bit) 0x9E429E84DC211A9EULL, +(u64bit) 0xCA0FCA1EC589D4CAULL, (u64bit) 0x2DB42D75995A582DULL, (u64bit) 0xBFC6BF9179632EBFULL, (u64bit) 0x071C07381B0E3F07ULL, +(u64bit) 0xAD8EAD012347ACADULL, (u64bit) 0x5A755AEA2FB4B05AULL, (u64bit) 0x8336836CB51BEF83ULL, (u64bit) 0x33CC3385FF66B633ULL, +(u64bit) 0x6391633FF2C65C63ULL, (u64bit) 0x020802100A041202ULL, (u64bit) 0xAA92AA39384993AAULL, (u64bit) 0x71D971AFA8E2DE71ULL, +(u64bit) 0xC807C80ECF8DC6C8ULL, (u64bit) 0x196419C87D32D119ULL, (u64bit) 0x4939497270923B49ULL, (u64bit) 0xD943D9869AAF5FD9ULL, +(u64bit) 0xF2EFF2C31DF931F2ULL, (u64bit) 0xE3ABE34B48DBA8E3ULL, (u64bit) 0x5B715BE22AB6B95BULL, (u64bit) 0x881A8834920DBC88ULL, +(u64bit) 0x9A529AA4C8293E9AULL, (u64bit) 0x2698262DBE4C0B26ULL, (u64bit) 0x32C8328DFA64BF32ULL, (u64bit) 0xB0FAB0E94A7D59B0ULL, +(u64bit) 0xE983E91B6ACFF2E9ULL, (u64bit) 0x0F3C0F78331E770FULL, (u64bit) 0xD573D5E6A6B733D5ULL, (u64bit) 0x803A8074BA1DF480ULL, +(u64bit) 0xBEC2BE997C6127BEULL, (u64bit) 0xCD13CD26DE87EBCDULL, (u64bit) 0x34D034BDE4688934ULL, (u64bit) 0x483D487A75903248ULL, +(u64bit) 0xFFDBFFAB24E354FFULL, (u64bit) 0x7AF57AF78FF48D7AULL, (u64bit) 0x907A90F4EA3D6490ULL, (u64bit) 0x5F615FC23EBE9D5FULL, +(u64bit) 0x2080201DA0403D20ULL, (u64bit) 0x68BD6867D5D00F68ULL, (u64bit) 0x1A681AD07234CA1AULL, (u64bit) 0xAE82AE192C41B7AEULL, +(u64bit) 0xB4EAB4C95E757DB4ULL, (u64bit) 0x544D549A19A8CE54ULL, (u64bit) 0x937693ECE53B7F93ULL, (u64bit) 0x2288220DAA442F22ULL, +(u64bit) 0x648D6407E9C86364ULL, (u64bit) 0xF1E3F1DB12FF2AF1ULL, (u64bit) 0x73D173BFA2E6CC73ULL, (u64bit) 0x124812905A248212ULL, +(u64bit) 0x401D403A5D807A40ULL, (u64bit) 0x0820084028104808ULL, (u64bit) 0xC32BC356E89B95C3ULL, (u64bit) 0xEC97EC337BC5DFECULL, +(u64bit) 0xDB4BDB9690AB4DDBULL, (u64bit) 0xA1BEA1611F5FC0A1ULL, (u64bit) 0x8D0E8D1C8307918DULL, (u64bit) 0x3DF43DF5C97AC83DULL, +(u64bit) 0x976697CCF1335B97ULL, (u64bit) 0x0000000000000000ULL, (u64bit) 0xCF1BCF36D483F9CFULL, (u64bit) 0x2BAC2B4587566E2BULL, +(u64bit) 0x76C57697B3ECE176ULL, (u64bit) 0x82328264B019E682ULL, (u64bit) 0xD67FD6FEA9B128D6ULL, (u64bit) 0x1B6C1BD87736C31BULL, +(u64bit) 0xB5EEB5C15B7774B5ULL, (u64bit) 0xAF86AF112943BEAFULL, (u64bit) 0x6AB56A77DFD41D6AULL, (u64bit) 0x505D50BA0DA0EA50ULL, +(u64bit) 0x450945124C8A5745ULL, (u64bit) 0xF3EBF3CB18FB38F3ULL, (u64bit) 0x30C0309DF060AD30ULL, (u64bit) 0xEF9BEF2B74C3C4EFULL, +(u64bit) 0x3FFC3FE5C37EDA3FULL, (u64bit) 0x554955921CAAC755ULL, (u64bit) 0xA2B2A2791059DBA2ULL, (u64bit) 0xEA8FEA0365C9E9EAULL, +(u64bit) 0x6589650FECCA6A65ULL, (u64bit) 0xBAD2BAB9686903BAULL, (u64bit) 0x2FBC2F65935E4A2FULL, (u64bit) 0xC027C04EE79D8EC0ULL, +(u64bit) 0xDE5FDEBE81A160DEULL, (u64bit) 0x1C701CE06C38FC1CULL, (u64bit) 0xFDD3FDBB2EE746FDULL, (u64bit) 0x4D294D52649A1F4DULL, +(u64bit) 0x927292E4E0397692ULL, (u64bit) 0x75C9758FBCEAFA75ULL, (u64bit) 0x061806301E0C3606ULL, (u64bit) 0x8A128A249809AE8AULL, +(u64bit) 0xB2F2B2F940794BB2ULL, (u64bit) 0xE6BFE66359D185E6ULL, (u64bit) 0x0E380E70361C7E0EULL, (u64bit) 0x1F7C1FF8633EE71FULL, +(u64bit) 0x62956237F7C45562ULL, (u64bit) 0xD477D4EEA3B53AD4ULL, (u64bit) 0xA89AA829324D81A8ULL, (u64bit) 0x966296C4F4315296ULL, +(u64bit) 0xF9C3F99B3AEF62F9ULL, (u64bit) 0xC533C566F697A3C5ULL, (u64bit) 0x25942535B14A1025ULL, (u64bit) 0x597959F220B2AB59ULL, +(u64bit) 0x842A8454AE15D084ULL, (u64bit) 0x72D572B7A7E4C572ULL, (u64bit) 0x39E439D5DD72EC39ULL, (u64bit) 0x4C2D4C5A6198164CULL, +(u64bit) 0x5E655ECA3BBC945EULL, (u64bit) 0x78FD78E785F09F78ULL, (u64bit) 0x38E038DDD870E538ULL, (u64bit) 0x8C0A8C148605988CULL, +(u64bit) 0xD163D1C6B2BF17D1ULL, (u64bit) 0xA5AEA5410B57E4A5ULL, (u64bit) 0xE2AFE2434DD9A1E2ULL, (u64bit) 0x6199612FF8C24E61ULL, +(u64bit) 0xB3F6B3F1457B42B3ULL, (u64bit) 0x21842115A5423421ULL, (u64bit) 0x9C4A9C94D625089CULL, (u64bit) 0x1E781EF0663CEE1EULL, +(u64bit) 0x4311432252866143ULL, (u64bit) 0xC73BC776FC93B1C7ULL, (u64bit) 0xFCD7FCB32BE54FFCULL, (u64bit) 0x0410042014082404ULL, +(u64bit) 0x515951B208A2E351ULL, (u64bit) 0x995E99BCC72F2599ULL, (u64bit) 0x6DA96D4FC4DA226DULL, (u64bit) 0x0D340D68391A650DULL, +(u64bit) 0xFACFFA8335E979FAULL, (u64bit) 0xDF5BDFB684A369DFULL, (u64bit) 0x7EE57ED79BFCA97EULL, (u64bit) 0x2490243DB4481924ULL, +(u64bit) 0x3BEC3BC5D776FE3BULL, (u64bit) 0xAB96AB313D4B9AABULL, (u64bit) 0xCE1FCE3ED181F0CEULL, (u64bit) 0x1144118855229911ULL, +(u64bit) 0x8F068F0C8903838FULL, (u64bit) 0x4E254E4A6B9C044EULL, (u64bit) 0xB7E6B7D1517366B7ULL, (u64bit) 0xEB8BEB0B60CBE0EBULL, +(u64bit) 0x3CF03CFDCC78C13CULL, (u64bit) 0x813E817CBF1FFD81ULL, (u64bit) 0x946A94D4FE354094ULL, (u64bit) 0xF7FBF7EB0CF31CF7ULL, +(u64bit) 0xB9DEB9A1676F18B9ULL, (u64bit) 0x134C13985F268B13ULL, (u64bit) 0x2CB02C7D9C58512CULL, (u64bit) 0xD36BD3D6B8BB05D3ULL, +(u64bit) 0xE7BBE76B5CD38CE7ULL, (u64bit) 0x6EA56E57CBDC396EULL, (u64bit) 0xC437C46EF395AAC4ULL, (u64bit) 0x030C03180F061B03ULL, +(u64bit) 0x5645568A13ACDC56ULL, (u64bit) 0x440D441A49885E44ULL, (u64bit) 0x7FE17FDF9EFEA07FULL, (u64bit) 0xA99EA921374F88A9ULL, +(u64bit) 0x2AA82A4D8254672AULL, (u64bit) 0xBBD6BBB16D6B0ABBULL, (u64bit) 0xC123C146E29F87C1ULL, (u64bit) 0x535153A202A6F153ULL, +(u64bit) 0xDC57DCAE8BA572DCULL, (u64bit) 0x0B2C0B582716530BULL, (u64bit) 0x9D4E9D9CD327019DULL, (u64bit) 0x6CAD6C47C1D82B6CULL, +(u64bit) 0x31C43195F562A431ULL, (u64bit) 0x74CD7487B9E8F374ULL, (u64bit) 0xF6FFF6E309F115F6ULL, (u64bit) 0x4605460A438C4C46ULL, +(u64bit) 0xAC8AAC092645A5ACULL, (u64bit) 0x891E893C970FB589ULL, (u64bit) 0x145014A04428B414ULL, (u64bit) 0xE1A3E15B42DFBAE1ULL, +(u64bit) 0x165816B04E2CA616ULL, (u64bit) 0x3AE83ACDD274F73AULL, (u64bit) 0x69B9696FD0D20669ULL, (u64bit) 0x092409482D124109ULL, +(u64bit) 0x70DD70A7ADE0D770ULL, (u64bit) 0xB6E2B6D954716FB6ULL, (u64bit) 0xD067D0CEB7BD1ED0ULL, (u64bit) 0xED93ED3B7EC7D6EDULL, +(u64bit) 0xCC17CC2EDB85E2CCULL, (u64bit) 0x4215422A57846842ULL, (u64bit) 0x985A98B4C22D2C98ULL, (u64bit) 0xA4AAA4490E55EDA4ULL, +(u64bit) 0x28A0285D88507528ULL, (u64bit) 0x5C6D5CDA31B8865CULL, (u64bit) 0xF8C7F8933FED6BF8ULL, (u64bit) 0x86228644A411C286ULL }; + +} diff --git a/botan/src/hash/whirlpool/whrlpool.cpp b/botan/src/hash/whirlpool/whrlpool.cpp new file mode 100644 index 0000000..53c057b --- /dev/null +++ b/botan/src/hash/whirlpool/whrlpool.cpp @@ -0,0 +1,146 @@ +/* +* Whirlpool +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/whrlpool.h> +#include <botan/loadstor.h> + +namespace Botan { + +/* +* Whirlpool Compression Function +*/ +void Whirlpool::compress_n(const byte in[], u32bit blocks) + { + static const u64bit RC[10] = { + (u64bit) 0x1823C6E887B8014FULL, (u64bit) 0x36A6D2F5796F9152ULL, + (u64bit) 0x60BC9B8EA30C7B35ULL, (u64bit) 0x1DE0D7C22E4BFE57ULL, + (u64bit) 0x157737E59FF04ADAULL, (u64bit) 0x58C9290AB1A06B85ULL, + (u64bit) 0xBD5D10F4CB3E0567ULL, (u64bit) 0xE427418BA77D95D8ULL, + (u64bit) 0xFBEE7C66DD17479EULL, (u64bit) 0xCA2DBF07AD5A8333ULL + }; + + for(u32bit i = 0; i != blocks; ++i) + { + for(u32bit j = 0; j != 8; ++j) + M[j] = load_be<u64bit>(in, j); + in += HASH_BLOCK_SIZE; + + u64bit K0, K1, K2, K3, K4, K5, K6, K7; + K0 = digest[0]; K1 = digest[1]; K2 = digest[2]; K3 = digest[3]; + K4 = digest[4]; K5 = digest[5]; K6 = digest[6]; K7 = digest[7]; + + u64bit B0, B1, B2, B3, B4, B5, B6, B7; + B0 = K0 ^ M[0]; B1 = K1 ^ M[1]; B2 = K2 ^ M[2]; B3 = K3 ^ M[3]; + B4 = K4 ^ M[4]; B5 = K5 ^ M[5]; B6 = K6 ^ M[6]; B7 = K7 ^ M[7]; + + for(u32bit j = 0; j != 10; ++j) + { + u64bit T0, T1, T2, T3, T4, T5, T6, T7; + T0 = C0[get_byte(0, K0)] ^ C1[get_byte(1, K7)] ^ + C2[get_byte(2, K6)] ^ C3[get_byte(3, K5)] ^ + C4[get_byte(4, K4)] ^ C5[get_byte(5, K3)] ^ + C6[get_byte(6, K2)] ^ C7[get_byte(7, K1)] ^ RC[j]; + T1 = C0[get_byte(0, K1)] ^ C1[get_byte(1, K0)] ^ + C2[get_byte(2, K7)] ^ C3[get_byte(3, K6)] ^ + C4[get_byte(4, K5)] ^ C5[get_byte(5, K4)] ^ + C6[get_byte(6, K3)] ^ C7[get_byte(7, K2)]; + T2 = C0[get_byte(0, K2)] ^ C1[get_byte(1, K1)] ^ + C2[get_byte(2, K0)] ^ C3[get_byte(3, K7)] ^ + C4[get_byte(4, K6)] ^ C5[get_byte(5, K5)] ^ + C6[get_byte(6, K4)] ^ C7[get_byte(7, K3)]; + T3 = C0[get_byte(0, K3)] ^ C1[get_byte(1, K2)] ^ + C2[get_byte(2, K1)] ^ C3[get_byte(3, K0)] ^ + C4[get_byte(4, K7)] ^ C5[get_byte(5, K6)] ^ + C6[get_byte(6, K5)] ^ C7[get_byte(7, K4)]; + T4 = C0[get_byte(0, K4)] ^ C1[get_byte(1, K3)] ^ + C2[get_byte(2, K2)] ^ C3[get_byte(3, K1)] ^ + C4[get_byte(4, K0)] ^ C5[get_byte(5, K7)] ^ + C6[get_byte(6, K6)] ^ C7[get_byte(7, K5)]; + T5 = C0[get_byte(0, K5)] ^ C1[get_byte(1, K4)] ^ + C2[get_byte(2, K3)] ^ C3[get_byte(3, K2)] ^ + C4[get_byte(4, K1)] ^ C5[get_byte(5, K0)] ^ + C6[get_byte(6, K7)] ^ C7[get_byte(7, K6)]; + T6 = C0[get_byte(0, K6)] ^ C1[get_byte(1, K5)] ^ + C2[get_byte(2, K4)] ^ C3[get_byte(3, K3)] ^ + C4[get_byte(4, K2)] ^ C5[get_byte(5, K1)] ^ + C6[get_byte(6, K0)] ^ C7[get_byte(7, K7)]; + T7 = C0[get_byte(0, K7)] ^ C1[get_byte(1, K6)] ^ + C2[get_byte(2, K5)] ^ C3[get_byte(3, K4)] ^ + C4[get_byte(4, K3)] ^ C5[get_byte(5, K2)] ^ + C6[get_byte(6, K1)] ^ C7[get_byte(7, K0)]; + + K0 = T0; K1 = T1; K2 = T2; K3 = T3; + K4 = T4; K5 = T5; K6 = T6; K7 = T7; + + T0 = C0[get_byte(0, B0)] ^ C1[get_byte(1, B7)] ^ + C2[get_byte(2, B6)] ^ C3[get_byte(3, B5)] ^ + C4[get_byte(4, B4)] ^ C5[get_byte(5, B3)] ^ + C6[get_byte(6, B2)] ^ C7[get_byte(7, B1)] ^ K0; + T1 = C0[get_byte(0, B1)] ^ C1[get_byte(1, B0)] ^ + C2[get_byte(2, B7)] ^ C3[get_byte(3, B6)] ^ + C4[get_byte(4, B5)] ^ C5[get_byte(5, B4)] ^ + C6[get_byte(6, B3)] ^ C7[get_byte(7, B2)] ^ K1; + T2 = C0[get_byte(0, B2)] ^ C1[get_byte(1, B1)] ^ + C2[get_byte(2, B0)] ^ C3[get_byte(3, B7)] ^ + C4[get_byte(4, B6)] ^ C5[get_byte(5, B5)] ^ + C6[get_byte(6, B4)] ^ C7[get_byte(7, B3)] ^ K2; + T3 = C0[get_byte(0, B3)] ^ C1[get_byte(1, B2)] ^ + C2[get_byte(2, B1)] ^ C3[get_byte(3, B0)] ^ + C4[get_byte(4, B7)] ^ C5[get_byte(5, B6)] ^ + C6[get_byte(6, B5)] ^ C7[get_byte(7, B4)] ^ K3; + T4 = C0[get_byte(0, B4)] ^ C1[get_byte(1, B3)] ^ + C2[get_byte(2, B2)] ^ C3[get_byte(3, B1)] ^ + C4[get_byte(4, B0)] ^ C5[get_byte(5, B7)] ^ + C6[get_byte(6, B6)] ^ C7[get_byte(7, B5)] ^ K4; + T5 = C0[get_byte(0, B5)] ^ C1[get_byte(1, B4)] ^ + C2[get_byte(2, B3)] ^ C3[get_byte(3, B2)] ^ + C4[get_byte(4, B1)] ^ C5[get_byte(5, B0)] ^ + C6[get_byte(6, B7)] ^ C7[get_byte(7, B6)] ^ K5; + T6 = C0[get_byte(0, B6)] ^ C1[get_byte(1, B5)] ^ + C2[get_byte(2, B4)] ^ C3[get_byte(3, B3)] ^ + C4[get_byte(4, B2)] ^ C5[get_byte(5, B1)] ^ + C6[get_byte(6, B0)] ^ C7[get_byte(7, B7)] ^ K6; + T7 = C0[get_byte(0, B7)] ^ C1[get_byte(1, B6)] ^ + C2[get_byte(2, B5)] ^ C3[get_byte(3, B4)] ^ + C4[get_byte(4, B3)] ^ C5[get_byte(5, B2)] ^ + C6[get_byte(6, B1)] ^ C7[get_byte(7, B0)] ^ K7; + + B0 = T0; B1 = T1; B2 = T2; B3 = T3; + B4 = T4; B5 = T5; B6 = T6; B7 = T7; + } + + digest[0] ^= B0 ^ M[0]; + digest[1] ^= B1 ^ M[1]; + digest[2] ^= B2 ^ M[2]; + digest[3] ^= B3 ^ M[3]; + digest[4] ^= B4 ^ M[4]; + digest[5] ^= B5 ^ M[5]; + digest[6] ^= B6 ^ M[6]; + digest[7] ^= B7 ^ M[7]; + } + } + +/* +* Copy out the digest +*/ +void Whirlpool::copy_out(byte output[]) + { + for(u32bit j = 0; j != OUTPUT_LENGTH; j += 8) + store_be(digest[j/8], output + j); + } + +/* +* Clear memory of sensitive data +*/ +void Whirlpool::clear() throw() + { + MDx_HashFunction::clear(); + M.clear(); + digest.clear(); + } + +} diff --git a/botan/src/hash/whirlpool/whrlpool.h b/botan/src/hash/whirlpool/whrlpool.h new file mode 100644 index 0000000..b72ff60 --- /dev/null +++ b/botan/src/hash/whirlpool/whrlpool.h @@ -0,0 +1,42 @@ +/* +* Whirlpool +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_WHIRLPOOL_H__ +#define BOTAN_WHIRLPOOL_H__ + +#include <botan/mdx_hash.h> + +namespace Botan { + +/* +* Whirlpool +*/ +class BOTAN_DLL Whirlpool : public MDx_HashFunction + { + public: + void clear() throw(); + std::string name() const { return "Whirlpool"; } + HashFunction* clone() const { return new Whirlpool; } + Whirlpool() : MDx_HashFunction(64, 64, true, true, 32) { clear(); } + private: + void compress_n(const byte[], u32bit blocks); + void copy_out(byte[]); + + static const u64bit C0[256]; + static const u64bit C1[256]; + static const u64bit C2[256]; + static const u64bit C3[256]; + static const u64bit C4[256]; + static const u64bit C5[256]; + static const u64bit C6[256]; + static const u64bit C7[256]; + SecureBuffer<u64bit, 8> M, digest; + }; + +} + +#endif diff --git a/botan/src/kdf/info.txt b/botan/src/kdf/info.txt new file mode 100644 index 0000000..1965a20 --- /dev/null +++ b/botan/src/kdf/info.txt @@ -0,0 +1,14 @@ +realname "KDF Base Class" + +define KDF_BASE + +load_on auto + +<add> +kdf.cpp +kdf.h +</add> + +<requires> +alloc +</requires> diff --git a/botan/src/kdf/kdf.cpp b/botan/src/kdf/kdf.cpp new file mode 100644 index 0000000..4be8475 --- /dev/null +++ b/botan/src/kdf/kdf.cpp @@ -0,0 +1,68 @@ +/* +* KDF Base Class +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/kdf.h> + +namespace Botan { + +/* +* Derive a key +*/ +SecureVector<byte> KDF::derive_key(u32bit key_len, + const MemoryRegion<byte>& secret, + const std::string& salt) const + { + return derive_key(key_len, secret, secret.size(), + reinterpret_cast<const byte*>(salt.data()), + salt.length()); + } + +/* +* Derive a key +*/ +SecureVector<byte> KDF::derive_key(u32bit key_len, + const MemoryRegion<byte>& secret, + const byte salt[], u32bit salt_len) const + { + return derive_key(key_len, secret.begin(), secret.size(), + salt, salt_len); + } + +/* +* Derive a key +*/ +SecureVector<byte> KDF::derive_key(u32bit key_len, + const MemoryRegion<byte>& secret, + const MemoryRegion<byte>& salt) const + { + return derive_key(key_len, secret.begin(), secret.size(), + salt.begin(), salt.size()); + } + +/* +* Derive a key +*/ +SecureVector<byte> KDF::derive_key(u32bit key_len, + const byte secret[], u32bit secret_len, + const std::string& salt) const + { + return derive_key(key_len, secret, secret_len, + reinterpret_cast<const byte*>(salt.data()), + salt.length()); + } + +/* +* Derive a key +*/ +SecureVector<byte> KDF::derive_key(u32bit key_len, + const byte secret[], u32bit secret_len, + const byte salt[], u32bit salt_len) const + { + return derive(key_len, secret, secret_len, salt, salt_len); + } + +} diff --git a/botan/src/kdf/kdf.h b/botan/src/kdf/kdf.h new file mode 100644 index 0000000..70f636b --- /dev/null +++ b/botan/src/kdf/kdf.h @@ -0,0 +1,60 @@ +/* +* KDF/MGF +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_KDF_BASE_H__ +#define BOTAN_KDF_BASE_H__ + +#include <botan/secmem.h> +#include <botan/types.h> + +namespace Botan { + +/* +* Key Derivation Function +*/ +class BOTAN_DLL KDF + { + public: + SecureVector<byte> derive_key(u32bit key_len, + const MemoryRegion<byte>& secret, + const std::string& salt = "") const; + SecureVector<byte> derive_key(u32bit key_len, + const MemoryRegion<byte>& secret, + const MemoryRegion<byte>& salt) const; + + SecureVector<byte> derive_key(u32bit key_len, + const MemoryRegion<byte>& secret, + const byte salt[], u32bit salt_len) const; + + SecureVector<byte> derive_key(u32bit key_len, + const byte secret[], u32bit secret_len, + const std::string& salt = "") const; + SecureVector<byte> derive_key(u32bit key_len, + const byte secret[], u32bit secret_len, + const byte salt[], u32bit salt_len) const; + + virtual ~KDF() {} + private: + virtual SecureVector<byte> derive(u32bit, const byte[], u32bit, + const byte[], u32bit) const = 0; + }; + +/* +* Mask Generation Function +*/ +class BOTAN_DLL MGF + { + public: + virtual void mask(const byte in[], u32bit in_len, + byte out[], u32bit out_len) const = 0; + + virtual ~MGF() {} + }; + +} + +#endif diff --git a/botan/src/kdf/kdf1/info.txt b/botan/src/kdf/kdf1/info.txt new file mode 100644 index 0000000..ede1001 --- /dev/null +++ b/botan/src/kdf/kdf1/info.txt @@ -0,0 +1,14 @@ +realname "KDF1" + +define KDF1 + +load_on auto + +<add> +kdf1.h +kdf1.cpp +</add> + +<requires> +hash +</requires> diff --git a/botan/src/kdf/kdf1/kdf1.cpp b/botan/src/kdf/kdf1/kdf1.cpp new file mode 100644 index 0000000..539d9ed --- /dev/null +++ b/botan/src/kdf/kdf1/kdf1.cpp @@ -0,0 +1,24 @@ +/* +* KDF1 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/kdf1.h> + +namespace Botan { + +/* +* KDF1 Key Derivation Mechanism +*/ +SecureVector<byte> KDF1::derive(u32bit, + const byte secret[], u32bit secret_len, + const byte P[], u32bit P_len) const + { + hash->update(secret, secret_len); + hash->update(P, P_len); + return hash->final(); + } + +} diff --git a/botan/src/kdf/kdf1/kdf1.h b/botan/src/kdf/kdf1/kdf1.h new file mode 100644 index 0000000..d657ccc --- /dev/null +++ b/botan/src/kdf/kdf1/kdf1.h @@ -0,0 +1,36 @@ +/* +* KDF1 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_KDF1_H__ +#define BOTAN_KDF1_H__ + +#include <botan/kdf.h> +#include <botan/hash.h> + +namespace Botan { + +/* +* KDF1 +*/ +class BOTAN_DLL KDF1 : public KDF + { + public: + SecureVector<byte> derive(u32bit, + const byte secret[], u32bit secret_len, + const byte P[], u32bit P_len) const; + + KDF1(HashFunction* h) : hash(h) {} + KDF1(const KDF1& other) : KDF(), hash(other.hash->clone()) {} + + ~KDF1() { delete hash; } + private: + HashFunction* hash; + }; + +} + +#endif diff --git a/botan/src/kdf/kdf2/info.txt b/botan/src/kdf/kdf2/info.txt new file mode 100644 index 0000000..1858f89 --- /dev/null +++ b/botan/src/kdf/kdf2/info.txt @@ -0,0 +1,14 @@ +realname "KDF2" + +define KDF2 + +load_on auto + +<add> +kdf2.cpp +kdf2.h +</add> + +<requires> +hash +</requires> diff --git a/botan/src/kdf/kdf2/kdf2.cpp b/botan/src/kdf/kdf2/kdf2.cpp new file mode 100644 index 0000000..167f644 --- /dev/null +++ b/botan/src/kdf/kdf2/kdf2.cpp @@ -0,0 +1,41 @@ +/* +* KDF2 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/kdf2.h> +#include <botan/loadstor.h> + +namespace Botan { + +/* +* KDF2 Key Derivation Mechanism +*/ +SecureVector<byte> KDF2::derive(u32bit out_len, + const byte secret[], u32bit secret_len, + const byte P[], u32bit P_len) const + { + SecureVector<byte> output; + u32bit counter = 1; + + while(out_len && counter) + { + hash->update(secret, secret_len); + for(u32bit j = 0; j != 4; ++j) + hash->update(get_byte(j, counter)); + hash->update(P, P_len); + SecureVector<byte> hash_result = hash->final(); + + u32bit added = std::min(hash_result.size(), out_len); + output.append(hash_result, added); + out_len -= added; + + ++counter; + } + + return output; + } + +} diff --git a/botan/src/kdf/kdf2/kdf2.h b/botan/src/kdf/kdf2/kdf2.h new file mode 100644 index 0000000..f748bed --- /dev/null +++ b/botan/src/kdf/kdf2/kdf2.h @@ -0,0 +1,34 @@ +/* +* KDF2 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_KDF2_H__ +#define BOTAN_KDF2_H__ + +#include <botan/kdf.h> +#include <botan/hash.h> + +namespace Botan { + +/* +* KDF2 +*/ +class BOTAN_DLL KDF2 : public KDF + { + public: + SecureVector<byte> derive(u32bit, const byte[], u32bit, + const byte[], u32bit) const; + + KDF2(HashFunction* h) : hash(h) {} + KDF2(const KDF2& other) : KDF(), hash(other.hash->clone()) {} + ~KDF2() { delete hash; } + private: + HashFunction* hash; + }; + +} + +#endif diff --git a/botan/src/kdf/mgf1/info.txt b/botan/src/kdf/mgf1/info.txt new file mode 100644 index 0000000..f9e952f --- /dev/null +++ b/botan/src/kdf/mgf1/info.txt @@ -0,0 +1,14 @@ +realname "MGF1" + +define MGF1 + +load_on dep + +<add> +mgf1.h +mgf1.cpp +</add> + +<requires> +hash +</requires> diff --git a/botan/src/kdf/mgf1/mgf1.cpp b/botan/src/kdf/mgf1/mgf1.cpp new file mode 100644 index 0000000..a26e33a --- /dev/null +++ b/botan/src/kdf/mgf1/mgf1.cpp @@ -0,0 +1,58 @@ +/* +* MGF1 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/mgf1.h> +#include <botan/loadstor.h> +#include <botan/exceptn.h> +#include <botan/xor_buf.h> +#include <algorithm> +#include <memory> + +namespace Botan { + +/* +* MGF1 Mask Generation Function +*/ +void MGF1::mask(const byte in[], u32bit in_len, byte out[], + u32bit out_len) const + { + u32bit counter = 0; + + while(out_len) + { + hash->update(in, in_len); + for(u32bit j = 0; j != 4; ++j) + hash->update(get_byte(j, counter)); + SecureVector<byte> buffer = hash->final(); + + u32bit xored = std::min(buffer.size(), out_len); + xor_buf(out, buffer.begin(), xored); + out += xored; + out_len -= xored; + + ++counter; + } + } + +/* +* MGF1 Constructor +*/ +MGF1::MGF1(HashFunction* h) : hash(h) + { + if(!hash) + throw Invalid_Argument("MGF1 given null hash object"); + } + +/* +* MGF1 Destructor +*/ +MGF1::~MGF1() + { + delete hash; + } + +} diff --git a/botan/src/kdf/mgf1/mgf1.h b/botan/src/kdf/mgf1/mgf1.h new file mode 100644 index 0000000..799ba7e --- /dev/null +++ b/botan/src/kdf/mgf1/mgf1.h @@ -0,0 +1,36 @@ +/* +* MGF1 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_MGF1_H__ +#define BOTAN_MGF1_H__ + +#include <botan/kdf.h> +#include <botan/hash.h> + +namespace Botan { + +/* +* MGF1 (Mask Generation Function) +*/ +class BOTAN_DLL MGF1 : public MGF + { + public: + void mask(const byte[], u32bit, byte[], u32bit) const; + + /** + MGF1 constructor: takes ownership of hash + */ + MGF1(HashFunction* hash); + + ~MGF1(); + private: + HashFunction* hash; + }; + +} + +#endif diff --git a/botan/src/kdf/ssl_prf/info.txt b/botan/src/kdf/ssl_prf/info.txt new file mode 100644 index 0000000..f862905 --- /dev/null +++ b/botan/src/kdf/ssl_prf/info.txt @@ -0,0 +1,16 @@ +realname "SSLv3 PRF" + +define SSL_V3_PRF + +load_on auto + +<add> +prf_ssl3.h +prf_ssl3.cpp +</add> + +<requires> +md5 +sha1 +sym_algo +</requires> diff --git a/botan/src/kdf/ssl_prf/prf_ssl3.cpp b/botan/src/kdf/ssl_prf/prf_ssl3.cpp new file mode 100644 index 0000000..2b67644 --- /dev/null +++ b/botan/src/kdf/ssl_prf/prf_ssl3.cpp @@ -0,0 +1,76 @@ +/* +* SSLv3 PRF +* (C) 2004-2006 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/prf_ssl3.h> +#include <botan/symkey.h> +#include <botan/exceptn.h> +#include <botan/sha160.h> +#include <botan/md5.h> +#include <memory> + +namespace Botan { + +namespace { + +/* +* Return the next inner hash +*/ +OctetString next_hash(u32bit where, u32bit want, + HashFunction& md5, HashFunction& sha1, + const byte secret[], u32bit secret_len, + const byte seed[], u32bit seed_len) + { + if(want > md5.OUTPUT_LENGTH) + throw Internal_Error("SSL3_PRF:next_hash: want is too big"); + + const byte ASCII_A_CHAR = 0x41; + + for(u32bit j = 0; j != where + 1; j++) + sha1.update(ASCII_A_CHAR + where); + sha1.update(secret, secret_len); + sha1.update(seed, seed_len); + SecureVector<byte> sha1_hash = sha1.final(); + + md5.update(secret, secret_len); + md5.update(sha1_hash); + SecureVector<byte> md5_hash = md5.final(); + + return OctetString(md5_hash, want); + } + +} + +/* +* SSL3 PRF +*/ +SecureVector<byte> SSL3_PRF::derive(u32bit key_len, + const byte secret[], u32bit secret_len, + const byte seed[], u32bit seed_len) const + { + if(key_len > 416) + throw Internal_Error("SSL3_PRF: Requested key length is too large"); + + MD5 md5; + SHA_160 sha1; + + OctetString output; + + int counter = 0; + while(key_len) + { + const u32bit produce = std::min(key_len, md5.OUTPUT_LENGTH); + + output = output + next_hash(counter++, produce, md5, sha1, + secret, secret_len, seed, seed_len); + + key_len -= produce; + } + + return output.bits_of(); + } + +} diff --git a/botan/src/kdf/ssl_prf/prf_ssl3.h b/botan/src/kdf/ssl_prf/prf_ssl3.h new file mode 100644 index 0000000..165fc7c --- /dev/null +++ b/botan/src/kdf/ssl_prf/prf_ssl3.h @@ -0,0 +1,27 @@ +/* +* SSLv3 PRF +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_SSLV3_PRF_H__ +#define BOTAN_SSLV3_PRF_H__ + +#include <botan/kdf.h> + +namespace Botan { + +/* +* SSL3 PRF +*/ +class BOTAN_DLL SSL3_PRF : public KDF + { + public: + SecureVector<byte> derive(u32bit, const byte[], u32bit, + const byte[], u32bit) const; + }; + +} + +#endif diff --git a/botan/src/kdf/tls_prf/info.txt b/botan/src/kdf/tls_prf/info.txt new file mode 100644 index 0000000..f95ef9c --- /dev/null +++ b/botan/src/kdf/tls_prf/info.txt @@ -0,0 +1,17 @@ +realname "TLS v1.0 PRF" + +define TLS_V10_PRF + +load_on auto + +<add> +prf_tls.h +prf_tls.cpp +</add> + +<requires> +hmac +mac +md5 +sha1 +</requires> diff --git a/botan/src/kdf/tls_prf/prf_tls.cpp b/botan/src/kdf/tls_prf/prf_tls.cpp new file mode 100644 index 0000000..7c638b9 --- /dev/null +++ b/botan/src/kdf/tls_prf/prf_tls.cpp @@ -0,0 +1,85 @@ +/* +* TLS PRF +* (C) 2004-2006 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/prf_tls.h> +#include <botan/xor_buf.h> +#include <botan/hmac.h> +#include <botan/md5.h> +#include <botan/sha160.h> + +namespace Botan { + +namespace { + +/* +* TLS PRF P_hash function +*/ +SecureVector<byte> P_hash(MessageAuthenticationCode* mac, + u32bit len, + const byte secret[], u32bit secret_len, + const byte seed[], u32bit seed_len) + { + SecureVector<byte> out; + + mac->set_key(secret, secret_len); + + SecureVector<byte> A(seed, seed_len); + while(len) + { + const u32bit this_block_len = std::min(mac->OUTPUT_LENGTH, len); + + A = mac->process(A); + + mac->update(A); + mac->update(seed, seed_len); + SecureVector<byte> block = mac->final(); + + out.append(block, this_block_len); + len -= this_block_len; + } + return out; + } + +} + +/* +* TLS PRF Constructor and Destructor +*/ +TLS_PRF::TLS_PRF() + { + hmac_md5 = new HMAC(new MD5); + hmac_sha1 = new HMAC(new SHA_160); + } + +TLS_PRF::~TLS_PRF() + { + delete hmac_md5; + delete hmac_sha1; + } + +/* +* TLS PRF +*/ +SecureVector<byte> TLS_PRF::derive(u32bit key_len, + const byte secret[], u32bit secret_len, + const byte seed[], u32bit seed_len) const + { + u32bit S1_len = (secret_len + 1) / 2, + S2_len = (secret_len + 1) / 2; + const byte* S1 = secret; + const byte* S2 = secret + (secret_len - S2_len); + + SecureVector<byte> key1, key2; + key1 = P_hash(hmac_md5, key_len, S1, S1_len, seed, seed_len); + key2 = P_hash(hmac_sha1, key_len, S2, S2_len, seed, seed_len); + + xor_buf(key1.begin(), key2.begin(), key2.size()); + + return key1; + } + +} diff --git a/botan/src/kdf/tls_prf/prf_tls.h b/botan/src/kdf/tls_prf/prf_tls.h new file mode 100644 index 0000000..d212795 --- /dev/null +++ b/botan/src/kdf/tls_prf/prf_tls.h @@ -0,0 +1,34 @@ +/* +* TLS v1.0 PRF +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_TLS_PRF_H__ +#define BOTAN_TLS_PRF_H__ + +#include <botan/kdf.h> +#include <botan/mac.h> + +namespace Botan { + +/* +* TLS PRF +*/ +class BOTAN_DLL TLS_PRF : public KDF + { + public: + SecureVector<byte> derive(u32bit, const byte[], u32bit, + const byte[], u32bit) const; + + TLS_PRF(); + ~TLS_PRF(); + private: + MessageAuthenticationCode* hmac_md5; + MessageAuthenticationCode* hmac_sha1; + }; + +} + +#endif diff --git a/botan/src/kdf/x942_prf/info.txt b/botan/src/kdf/x942_prf/info.txt new file mode 100644 index 0000000..295c2cd --- /dev/null +++ b/botan/src/kdf/x942_prf/info.txt @@ -0,0 +1,16 @@ +realname "X942 PRF" + +define X942_PRF + +load_on auto + +<add> +prf_x942.cpp +prf_x942.h +</add> + +<requires> +asn1 +oid_lookup +sha1 +</requires> diff --git a/botan/src/kdf/x942_prf/prf_x942.cpp b/botan/src/kdf/x942_prf/prf_x942.cpp new file mode 100644 index 0000000..d9ee09d --- /dev/null +++ b/botan/src/kdf/x942_prf/prf_x942.cpp @@ -0,0 +1,91 @@ +/* +* X9.42 PRF +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/prf_x942.h> +#include <botan/der_enc.h> +#include <botan/oids.h> +#include <botan/sha160.h> +#include <botan/loadstor.h> +#include <algorithm> +#include <memory> + +namespace Botan { + +namespace { + +/* +* Encode an integer as an OCTET STRING +*/ +MemoryVector<byte> encode_x942_int(u32bit n) + { + byte n_buf[4] = { 0 }; + store_be(n, n_buf); + return DER_Encoder().encode(n_buf, 4, OCTET_STRING).get_contents(); + } + +} + +/* +* X9.42 PRF +*/ +SecureVector<byte> X942_PRF::derive(u32bit key_len, + const byte secret[], u32bit secret_len, + const byte salt[], u32bit salt_len) const + { + SHA_160 hash; + const OID kek_algo(key_wrap_oid); + + SecureVector<byte> key; + u32bit counter = 1; + + while(key.size() != key_len && counter) + { + hash.update(secret, secret_len); + + hash.update( + DER_Encoder().start_cons(SEQUENCE) + + .start_cons(SEQUENCE) + .encode(kek_algo) + .raw_bytes(encode_x942_int(counter)) + .end_cons() + + .encode_if(salt_len != 0, + DER_Encoder() + .start_explicit(0) + .encode(salt, salt_len, OCTET_STRING) + .end_explicit() + ) + + .start_explicit(2) + .raw_bytes(encode_x942_int(8 * key_len)) + .end_explicit() + + .end_cons().get_contents() + ); + + SecureVector<byte> digest = hash.final(); + key.append(digest, std::min(digest.size(), key_len - key.size())); + + ++counter; + } + + return key; + } + +/* +* X9.42 Constructor +*/ +X942_PRF::X942_PRF(const std::string& oid) + { + if(OIDS::have_oid(oid)) + key_wrap_oid = OIDS::lookup(oid).as_string(); + else + key_wrap_oid = oid; + } + +} diff --git a/botan/src/kdf/x942_prf/prf_x942.h b/botan/src/kdf/x942_prf/prf_x942.h new file mode 100644 index 0000000..f957566 --- /dev/null +++ b/botan/src/kdf/x942_prf/prf_x942.h @@ -0,0 +1,31 @@ +/* +* X9.42 PRF +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ANSI_X942_PRF_H__ +#define BOTAN_ANSI_X942_PRF_H__ + +#include <botan/kdf.h> + +namespace Botan { + +/* +* X9.42 PRF +*/ +class BOTAN_DLL X942_PRF : public KDF + { + public: + SecureVector<byte> derive(u32bit, const byte[], u32bit, + const byte[], u32bit) const; + + X942_PRF(const std::string&); + private: + std::string key_wrap_oid; + }; + +} + +#endif diff --git a/botan/src/libstate/botan.h b/botan/src/libstate/botan.h new file mode 100644 index 0000000..3fa1312 --- /dev/null +++ b/botan/src/libstate/botan.h @@ -0,0 +1,18 @@ +/** +* A vague catch all include file for Botan +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/init.h> +#include <botan/lookup.h> +#include <botan/libstate.h> +#include <botan/version.h> +#include <botan/parsing.h> + +#include <botan/rng.h> + +#if defined(BOTAN_HAS_AUTO_SEEDING_RNG) + #include <botan/auto_rng.h> +#endif diff --git a/botan/src/libstate/get_enc.cpp b/botan/src/libstate/get_enc.cpp new file mode 100644 index 0000000..ab4d158 --- /dev/null +++ b/botan/src/libstate/get_enc.cpp @@ -0,0 +1,238 @@ +/* +* PBKDF/EMSA/EME/KDF/MGF Retrieval +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/lookup.h> +#include <botan/libstate.h> +#include <botan/scan_name.h> + +#if defined(BOTAN_HAS_PBKDF1) + #include <botan/pbkdf1.h> +#endif + +#if defined(BOTAN_HAS_PBKDF2) + #include <botan/pbkdf2.h> + #include <botan/hmac.h> +#endif + +#if defined(BOTAN_HAS_PGPS2K) + #include <botan/pgp_s2k.h> +#endif + +#if defined(BOTAN_HAS_MGF1) + #include <botan/mgf1.h> +#endif + +#if defined(BOTAN_HAS_EMSA1) + #include <botan/emsa1.h> +#endif + +#if defined(BOTAN_HAS_EMSA1_BSI) + #include <botan/emsa1_bsi.h> +#endif + +#if defined(BOTAN_HAS_EMSA2) + #include <botan/emsa2.h> +#endif + +#if defined(BOTAN_HAS_EMSA3) + #include <botan/emsa3.h> +#endif + +#if defined(BOTAN_HAS_EMSA4) + #include <botan/emsa4.h> +#endif + +#if defined(BOTAN_HAS_EMSA_RAW) + #include <botan/emsa_raw.h> +#endif + +#if defined(BOTAN_HAS_EME1) + #include <botan/eme1.h> +#endif + +#if defined(BOTAN_HAS_EME_PKCS1v15) + #include <botan/eme_pkcs.h> +#endif + +#if defined(BOTAN_HAS_KDF1) + #include <botan/kdf1.h> +#endif + +#if defined(BOTAN_HAS_KDF2) + #include <botan/kdf2.h> +#endif + +#if defined(BOTAN_HAS_X942_PRF) + #include <botan/prf_x942.h> +#endif + +#if defined(BOTAN_HAS_SSL_V3_PRF) + #include <botan/prf_ssl3.h> +#endif + +#if defined(BOTAN_HAS_TLS_V10_PRF) + #include <botan/prf_tls.h> +#endif + +namespace Botan { + +/* +* Get a S2K algorithm by name +*/ +S2K* get_s2k(const std::string& algo_spec) + { + SCAN_Name request(algo_spec); + + Algorithm_Factory& af = global_state().algorithm_factory(); + +#if defined(BOTAN_HAS_PBKDF1) + if(request.algo_name() == "PBKDF1" && request.arg_count() == 1) + return new PKCS5_PBKDF1(af.make_hash_function(request.arg(0))); +#endif + +#if defined(BOTAN_HAS_PBKDF2) + if(request.algo_name() == "PBKDF2" && request.arg_count() == 1) + return new PKCS5_PBKDF2(new HMAC(af.make_hash_function(request.arg(0)))); +#endif + +#if defined(BOTAN_HAS_PGPS2K) + if(request.algo_name() == "OpenPGP-S2K" && request.arg_count() == 1) + return new OpenPGP_S2K(af.make_hash_function(request.arg(0))); +#endif + + throw Algorithm_Not_Found(algo_spec); + } + +/* +* Get an EMSA by name +*/ +EMSA* get_emsa(const std::string& algo_spec) + { + SCAN_Name request(algo_spec); + + Algorithm_Factory& af = global_state().algorithm_factory(); + +#if defined(BOTAN_HAS_EMSA_RAW) + if(request.algo_name() == "Raw" && request.arg_count() == 0) + return new EMSA_Raw; +#endif + +#if defined(BOTAN_HAS_EMSA1) + if(request.algo_name() == "EMSA1" && request.arg_count() == 1) + return new EMSA1(af.make_hash_function(request.arg(0))); +#endif + +#if defined(BOTAN_HAS_EMSA1_BSI) + if(request.algo_name() == "EMSA1_BSI" && request.arg_count() == 1) + return new EMSA1_BSI(af.make_hash_function(request.arg(0))); +#endif + +#if defined(BOTAN_HAS_EMSA2) + if(request.algo_name() == "EMSA2" && request.arg_count() == 1) + return new EMSA2(af.make_hash_function(request.arg(0))); +#endif + +#if defined(BOTAN_HAS_EMSA3) + if(request.algo_name() == "EMSA3" && request.arg_count() == 1) + { + if(request.arg(0) == "Raw") + return new EMSA3_Raw; + return new EMSA3(af.make_hash_function(request.arg(0))); + } +#endif + +#if defined(BOTAN_HAS_EMSA4) + if(request.algo_name() == "EMSA4" && request.arg_count_between(1, 3)) + { + // 3 args: Hash, MGF, salt size (MGF is hardcoded MGF1 in Botan) + if(request.arg_count() == 1) + return new EMSA4(af.make_hash_function(request.arg(0))); + + if(request.arg_count() == 2 && request.arg(1) != "MGF1") + return new EMSA4(af.make_hash_function(request.arg(0))); + + if(request.arg_count() == 3) + return new EMSA4(af.make_hash_function(request.arg(0)), + request.arg_as_u32bit(2, 0)); + } +#endif + + throw Algorithm_Not_Found(algo_spec); + } + +/* +* Get an EME by name +*/ +EME* get_eme(const std::string& algo_spec) + { + SCAN_Name request(algo_spec); + + Algorithm_Factory& af = global_state().algorithm_factory(); + + if(request.algo_name() == "Raw") + return 0; // No padding + +#if defined(BOTAN_HAS_EME_PKCS1v15) + if(request.algo_name() == "PKCS1v15" && request.arg_count() == 0) + return new EME_PKCS1v15; +#endif + +#if defined(BOTAN_HAS_EME1) + if(request.algo_name() == "EME1" && request.arg_count_between(1, 2)) + { + if(request.arg_count() == 1 || + (request.arg_count() == 2 && request.arg(1) == "MGF1")) + { + return new EME1(af.make_hash_function(request.arg(0))); + } + } +#endif + + throw Algorithm_Not_Found(algo_spec); + } + +/* +* Get an KDF by name +*/ +KDF* get_kdf(const std::string& algo_spec) + { + SCAN_Name request(algo_spec); + + Algorithm_Factory& af = global_state().algorithm_factory(); + + if(request.algo_name() == "Raw") + return 0; // No KDF + +#if defined(BOTAN_HAS_KDF1) + if(request.algo_name() == "KDF1" && request.arg_count() == 1) + return new KDF1(af.make_hash_function(request.arg(0))); +#endif + +#if defined(BOTAN_HAS_KDF2) + if(request.algo_name() == "KDF2" && request.arg_count() == 1) + return new KDF2(af.make_hash_function(request.arg(0))); +#endif + +#if defined(BOTAN_HAS_X942_PRF) + if(request.algo_name() == "X9.42-PRF" && request.arg_count() == 1) + return new X942_PRF(request.arg(0)); // OID +#endif + +#if defined(BOTAN_HAS_TLS_V10_PRF) + if(request.algo_name() == "TLS-PRF" && request.arg_count() == 0) + return new TLS_PRF; +#endif + +#if defined(BOTAN_HAS_SSL_V3_PRF) + if(request.algo_name() == "SSL3-PRF" && request.arg_count() == 0) + return new SSL3_PRF; +#endif + + throw Algorithm_Not_Found(algo_spec); + } + +} diff --git a/botan/src/libstate/info.txt b/botan/src/libstate/info.txt new file mode 100644 index 0000000..6eaa2f7 --- /dev/null +++ b/botan/src/libstate/info.txt @@ -0,0 +1,45 @@ +realname "Botan Libstate Module" + +load_on always + +define LIBSTATE_MODULE + +<add> +botan.h +get_enc.cpp +init.cpp +init.h +libstate.cpp +libstate.h +look_pk.cpp +look_pk.h +lookup.cpp +lookup.h +pk_engine.cpp +pk_engine.h +policy.cpp +scan_name.cpp +scan_name.h +</add> + +<requires> +algo_factory +alloc +bigint +block +def_engine +engine +filters +hash +kdf +mac +mode_pad +mutex +noop_mutex +pk_pad +pubkey +rng +s2k +stream +system_alloc +</requires> diff --git a/botan/src/libstate/init.cpp b/botan/src/libstate/init.cpp new file mode 100644 index 0000000..b908de6 --- /dev/null +++ b/botan/src/libstate/init.cpp @@ -0,0 +1,75 @@ +/** +* Default Initialization Function +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/init.h> +#include <botan/parsing.h> +#include <botan/libstate.h> + +namespace Botan { + +/* +* Library Initialization +*/ +void LibraryInitializer::initialize(const std::string& arg_string) + { + bool thread_safe = false; + + const std::vector<std::string> arg_list = split_on(arg_string, ' '); + for(u32bit j = 0; j != arg_list.size(); ++j) + { + if(arg_list[j].size() == 0) + continue; + + std::string name, value; + + if(arg_list[j].find('=') == std::string::npos) + { + name = arg_list[j]; + value = "true"; + } + else + { + std::vector<std::string> name_and_value = split_on(arg_list[j], '='); + name = name_and_value[0]; + value = name_and_value[1]; + } + + bool is_on = + (value == "1" || value == "true" || value == "yes" || value == "on"); + + if(name == "thread_safe") + thread_safe = is_on; + } + + try + { + /* + This two stage initialization process is because Library_State's + constructor will implicitly refer to global state through the + allocators and so for, so global_state() has to be a valid + reference before initialize() can be called. Yeah, gross. + */ + set_global_state(new Library_State); + + global_state().initialize(thread_safe); + } + catch(...) + { + deinitialize(); + throw; + } + } + +/* +* Library Shutdown +*/ +void LibraryInitializer::deinitialize() + { + set_global_state(0); + } + +} diff --git a/botan/src/libstate/init.h b/botan/src/libstate/init.h new file mode 100644 index 0000000..254f945 --- /dev/null +++ b/botan/src/libstate/init.h @@ -0,0 +1,41 @@ +/** +* Library Initialization +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_LIBRARY_INITIALIZER_H__ +#define BOTAN_LIBRARY_INITIALIZER_H__ + +#include <botan/build.h> +#include <string> + +namespace Botan { + +/** +* This class represents the Library Initialization/Shutdown Object. It +* has to exceed the lifetime of any Botan object used in an +* application. You can call initialize/deinitialize or use +* LibraryInitializer in the RAII style. +*/ +class BOTAN_DLL LibraryInitializer + { + public: + static void initialize(const std::string& options = ""); + + static void deinitialize(); + + /** + * Initialize the library + * @param thread_safe if the library should use a thread-safe mutex + */ + LibraryInitializer(const std::string& options = "") + { LibraryInitializer::initialize(options); } + + ~LibraryInitializer() { LibraryInitializer::deinitialize(); } + }; + +} + +#endif diff --git a/botan/src/libstate/libstate.cpp b/botan/src/libstate/libstate.cpp new file mode 100644 index 0000000..3275c64 --- /dev/null +++ b/botan/src/libstate/libstate.cpp @@ -0,0 +1,338 @@ +/* +* Library Internal/Global State +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/libstate.h> +#include <botan/init.h> +#include <botan/engine.h> +#include <botan/stl_util.h> +#include <botan/mutex.h> +#include <botan/mux_noop.h> +#include <botan/charset.h> +#include <botan/defalloc.h> +#include <botan/def_eng.h> +#include <algorithm> + +#if defined(BOTAN_HAS_MUTEX_PTHREAD) + #include <botan/mux_pthr.h> +#elif defined(BOTAN_HAS_MUTEX_WIN32) + #include <botan/mux_win32.h> +#elif defined(BOTAN_HAS_MUTEX_QT) + #include <botan/mux_qt.h> +#endif + +#if defined(BOTAN_HAS_ALLOC_MMAP) + #include <botan/mmap_mem.h> +#endif + +#if defined(BOTAN_HAS_ENGINE_IA32_ASSEMBLER) + #include <botan/eng_ia32.h> +#endif + +#if defined(BOTAN_HAS_ENGINE_AMD64_ASSEMBLER) + #include <botan/eng_amd64.h> +#endif + +#if defined(BOTAN_HAS_ENGINE_SSE2_ASSEMBLER) + #include <botan/eng_sse2.h> +#endif + +#if defined(BOTAN_HAS_ENGINE_GNU_MP) + #include <botan/eng_gmp.h> +#endif + +#if defined(BOTAN_HAS_ENGINE_OPENSSL) + #include <botan/eng_ossl.h> +#endif + +namespace Botan { + +/* +* Botan's global state +*/ +namespace { + +Library_State* global_lib_state = 0; + +} + +/* +* Access the global state object +*/ +Library_State& global_state() + { + /* Lazy initialization. Botan still needs to be deinitialized later + on or memory might leak. + */ + if(!global_lib_state) + LibraryInitializer::initialize("thread_safe=true"); + + return (*global_lib_state); + } + +/* +* Set a new global state object +*/ +void set_global_state(Library_State* new_state) + { + delete swap_global_state(new_state); + } + +/* +* Swap two global state objects +*/ +Library_State* swap_global_state(Library_State* new_state) + { + Library_State* old_state = global_lib_state; + global_lib_state = new_state; + return old_state; + } + +/* +* Get a new mutex object +*/ +Mutex* Library_State::get_mutex() const + { + return mutex_factory->make(); + } + +/* +* Get an allocator by its name +*/ +Allocator* Library_State::get_allocator(const std::string& type) const + { + Mutex_Holder lock(allocator_lock); + + if(type != "") + return search_map<std::string, Allocator*>(alloc_factory, type, 0); + + if(!cached_default_allocator) + { + std::string chosen = this->option("base/default_allocator"); + + if(chosen == "") + chosen = "malloc"; + + cached_default_allocator = + search_map<std::string, Allocator*>(alloc_factory, chosen, 0); + } + + return cached_default_allocator; + } + +/* +* Create a new name to object mapping +*/ +void Library_State::add_allocator(Allocator* allocator) + { + Mutex_Holder lock(allocator_lock); + + allocator->init(); + + allocators.push_back(allocator); + alloc_factory[allocator->type()] = allocator; + } + +/* +* Set the default allocator type +*/ +void Library_State::set_default_allocator(const std::string& type) + { + Mutex_Holder lock(allocator_lock); + + if(type == "") + return; + + this->set("conf", "base/default_allocator", type); + cached_default_allocator = 0; + } + +/* +* Get a configuration value +*/ +std::string Library_State::get(const std::string& section, + const std::string& key) const + { + Mutex_Holder lock(config_lock); + + return search_map<std::string, std::string>(config, + section + "/" + key, ""); + } + +/* +* See if a particular option has been set +*/ +bool Library_State::is_set(const std::string& section, + const std::string& key) const + { + Mutex_Holder lock(config_lock); + + return search_map(config, section + "/" + key, false, true); + } + +/* +* Set a configuration value +*/ +void Library_State::set(const std::string& section, const std::string& key, + const std::string& value, bool overwrite) + { + Mutex_Holder lock(config_lock); + + std::string full_key = section + "/" + key; + + std::map<std::string, std::string>::const_iterator i = + config.find(full_key); + + if(overwrite || i == config.end() || i->second == "") + config[full_key] = value; + } + +/* +* Add an alias +*/ +void Library_State::add_alias(const std::string& key, const std::string& value) + { + set("alias", key, value); + } + +/* +* Dereference an alias to a fixed name +*/ +std::string Library_State::deref_alias(const std::string& key) const + { + std::string result = key; + while(is_set("alias", result)) + result = get("alias", result); + return result; + } + +/* +* Set/Add an option +*/ +void Library_State::set_option(const std::string key, + const std::string& value) + { + set("conf", key, value); + } + +/* +* Get an option value +*/ +std::string Library_State::option(const std::string& key) const + { + return get("conf", key); + } + +/** +Return a reference to the Algorithm_Factory +*/ +Algorithm_Factory& Library_State::algorithm_factory() + { + if(!m_algorithm_factory) + throw Invalid_State("Uninitialized in Library_State::algorithm_factory"); + return *m_algorithm_factory; + } + +/* +* Load a set of modules +*/ +void Library_State::initialize(bool thread_safe) + { + if(mutex_factory) + throw Invalid_State("Library_State has already been initialized"); + + if(!thread_safe) + { + mutex_factory = new Noop_Mutex_Factory; + } + else + { +#if defined(BOTAN_HAS_MUTEX_PTHREAD) + mutex_factory = new Pthread_Mutex_Factory; +#elif defined(BOTAN_HAS_MUTEX_WIN32) + mutex_factory = new Win32_Mutex_Factory; +#elif defined(BOTAN_HAS_MUTEX_QT) + mutex_factory Qt_Mutex_Factory; +#else + throw Invalid_State("Could not find a thread-safe mutex object to use"); +#endif + } + + allocator_lock = mutex_factory->make(); + config_lock = mutex_factory->make(); + + cached_default_allocator = 0; + + add_allocator(new Malloc_Allocator); + add_allocator(new Locking_Allocator(mutex_factory->make())); + +#if defined(BOTAN_HAS_ALLOC_MMAP) + add_allocator(new MemoryMapping_Allocator(mutex_factory->make())); +#endif + + set_default_allocator("locking"); + + load_default_config(); + + std::vector<Engine*> engines; + +#if defined(BOTAN_HAS_ENGINE_GNU_MP) + engines.push_back(new GMP_Engine); +#endif + +#if defined(BOTAN_HAS_ENGINE_OPENSSL) + engines.push_back(new OpenSSL_Engine); +#endif + +#if defined(BOTAN_HAS_ENGINE_SSE2_ASSEMBLER) + engines.push_back(new SSE2_Assembler_Engine); +#endif + +#if defined(BOTAN_HAS_ENGINE_AMD64_ASSEMBLER) + engines.push_back(new AMD64_Assembler_Engine); +#endif + +#if defined(BOTAN_HAS_ENGINE_IA32_ASSEMBLER) + engines.push_back(new IA32_Assembler_Engine); +#endif + + engines.push_back(new Default_Engine); + + m_algorithm_factory = new Algorithm_Factory(engines, *mutex_factory); + } + +/* +* Library_State Constructor +*/ +Library_State::Library_State() + { + mutex_factory = 0; + allocator_lock = config_lock = 0; + cached_default_allocator = 0; + m_algorithm_factory = 0; + } + +/* +* Library_State Destructor +*/ +Library_State::~Library_State() + { + delete m_algorithm_factory; + + cached_default_allocator = 0; + + for(u32bit j = 0; j != allocators.size(); ++j) + { + allocators[j]->destroy(); + delete allocators[j]; + } + + delete allocator_lock; + delete mutex_factory; + delete config_lock; + } + +} diff --git a/botan/src/libstate/libstate.h b/botan/src/libstate/libstate.h new file mode 100644 index 0000000..2493863 --- /dev/null +++ b/botan/src/libstate/libstate.h @@ -0,0 +1,125 @@ +/* +* Library Internal/Global State +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_LIB_STATE_H__ +#define BOTAN_LIB_STATE_H__ + +#include <botan/types.h> +#include <botan/allocate.h> +#include <botan/algo_factory.h> + +#include <string> +#include <vector> +#include <map> + +namespace Botan { + +/* +* Global State Container Base +*/ +class BOTAN_DLL Library_State + { + public: + Library_State(); + ~Library_State(); + + void initialize(bool thread_safe); + + Algorithm_Factory& algorithm_factory(); + + Allocator* get_allocator(const std::string& = "") const; + void add_allocator(Allocator*); + void set_default_allocator(const std::string&); + + /** + * Get a parameter value as std::string. + * @param section the section of the desired key + * @param key the desired keys name + * @result the value of the parameter + */ + std::string get(const std::string& section, + const std::string& key) const; + + /** + * Check whether a certain parameter is set + * or not. + * @param section the section of the desired key + * @param key the desired keys name + * @result true if the parameters value is set, + * false otherwise + */ + bool is_set(const std::string& section, const std::string& key) const; + + /** + * Set a configuration parameter. + * @param section the section of the desired key + * @param key the desired keys name + * @param overwrite if set to true, the parameters value + * will be overwritten even if it is already set, otherwise + * no existing values will be overwritten. + */ + void set(const std::string& section, const std::string& key, + const std::string& value, bool overwrite = true); + + /** + * Get a parameters value out of the "conf" section ( + * referred to as option). + * @param key the desired keys name + */ + std::string option(const std::string& key) const; + + /** + * Set an option. + * @param key the key of the option to set + * @param value the value to set + */ + void set_option(const std::string key, const std::string& value); + + /** + * Add a parameter value to the "alias" section. + * @param key the name of the parameter which shall have a new alias + * @param value the new alias + */ + void add_alias(const std::string&, const std::string&); + + /** + * Resolve an alias. + * @param alias the alias to resolve. + * @return what the alias stands for + */ + std::string deref_alias(const std::string&) const; + + class Mutex* get_mutex() const; + private: + void load_default_config(); + + Library_State(const Library_State&) {} + Library_State& operator=(const Library_State&) { return (*this); } + + class Mutex_Factory* mutex_factory; + + std::map<std::string, std::string> config; + class Mutex* config_lock; + + class Mutex* allocator_lock; + std::map<std::string, Allocator*> alloc_factory; + mutable Allocator* cached_default_allocator; + std::vector<Allocator*> allocators; + + Algorithm_Factory* m_algorithm_factory; + }; + +/* +* Global State +*/ +BOTAN_DLL Library_State& global_state(); +BOTAN_DLL void set_global_state(Library_State*); +BOTAN_DLL Library_State* swap_global_state(Library_State*); + +} + +#endif diff --git a/botan/src/libstate/look_pk.cpp b/botan/src/libstate/look_pk.cpp new file mode 100644 index 0000000..8eb4738 --- /dev/null +++ b/botan/src/libstate/look_pk.cpp @@ -0,0 +1,76 @@ +/* +* PK Algorithm Lookup +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/look_pk.h> +#include <botan/lookup.h> + +namespace Botan { + +/* +* Get a PK_Encryptor object +*/ +PK_Encryptor* get_pk_encryptor(const PK_Encrypting_Key& key, + const std::string& eme) + { + return new PK_Encryptor_MR_with_EME(key, get_eme(eme)); + } + +/* +* Get a PK_Decryptor object +*/ +PK_Decryptor* get_pk_decryptor(const PK_Decrypting_Key& key, + const std::string& eme) + { + return new PK_Decryptor_MR_with_EME(key, get_eme(eme)); + } + +/* +* Get a PK_Signer object +*/ +PK_Signer* get_pk_signer(const PK_Signing_Key& key, + const std::string& emsa, + Signature_Format sig_format) + { + PK_Signer* signer = new PK_Signer(key, get_emsa(emsa)); + signer->set_output_format(sig_format); + return signer; + } + +/* +* Get a PK_Verifier object +*/ +PK_Verifier* get_pk_verifier(const PK_Verifying_with_MR_Key& key, + const std::string& emsa, + Signature_Format sig_format) + { + PK_Verifier* verifier = new PK_Verifier_with_MR(key, get_emsa(emsa)); + verifier->set_input_format(sig_format); + return verifier; + } + +/* +* Get a PK_Verifier object +*/ +PK_Verifier* get_pk_verifier(const PK_Verifying_wo_MR_Key& key, + const std::string& emsa, + Signature_Format sig_format) + { + PK_Verifier* verifier = new PK_Verifier_wo_MR(key, get_emsa(emsa)); + verifier->set_input_format(sig_format); + return verifier; + } + +/* +* Get a PK_Key_Agreement object +*/ +PK_Key_Agreement* get_pk_kas(const PK_Key_Agreement_Key& key, + const std::string& kdf) + { + return new PK_Key_Agreement(key, get_kdf(kdf)); + } + +} diff --git a/botan/src/libstate/look_pk.h b/botan/src/libstate/look_pk.h new file mode 100644 index 0000000..27b67dc --- /dev/null +++ b/botan/src/libstate/look_pk.h @@ -0,0 +1,78 @@ +/* +* PK Algorithm Lookup +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_PK_LOOKUP_H__ +#define BOTAN_PK_LOOKUP_H__ + +#include <botan/build.h> +#include <botan/pubkey.h> + +namespace Botan { + +/** +* Public key encryptor factory method. +* @param key the key that will work inside the encryptor +* @param pad determines the algorithm and encoding +* @return the public key encryptor object +*/ +BOTAN_DLL PK_Encryptor* get_pk_encryptor(const PK_Encrypting_Key& key, + const std::string& pad); + +/** +* Public key decryptor factory method. +* @param key the key that will work inside the decryptor +* @param pad determines the algorithm and encoding +* @return the public key decryptor object +*/ +BOTAN_DLL PK_Decryptor* get_pk_decryptor(const PK_Decrypting_Key& key, + const std::string& pad); + +/** +* Public key signer factory method. +* @param key the key that will work inside the signer +* @param pad determines the algorithm, encoding and hash algorithm +* @param sig_format the signature format to be used +* @return the public key signer object +*/ +BOTAN_DLL PK_Signer* get_pk_signer(const PK_Signing_Key& key, + const std::string& pad, + Signature_Format = IEEE_1363); + +/** +* Public key verifier factory method. +* @param key the key that will work inside the verifier +* @param pad determines the algorithm, encoding and hash algorithm +* @param sig_format the signature format to be used +* @return the public key verifier object +*/ +BOTAN_DLL PK_Verifier* get_pk_verifier(const PK_Verifying_with_MR_Key& key, + const std::string& pad, + Signature_Format = IEEE_1363); + +/** +* Public key verifier factory method. +* @param key the key that will work inside the verifier +* @param pad determines the algorithm, encoding and hash algorithm +* @param sig_form the signature format to be used +* @return the public key verifier object +*/ +BOTAN_DLL PK_Verifier* get_pk_verifier(const PK_Verifying_wo_MR_Key& key, + const std::string& pad, + Signature_Format sig_form = IEEE_1363); + +/** +* Public key key agreement factory method. +* @param key the key that will work inside the key agreement +* @param pad determines the algorithm, encoding and hash algorithm +* @return the public key verifier object +*/ +BOTAN_DLL PK_Key_Agreement* get_pk_kas(const PK_Key_Agreement_Key& key, + const std::string& pad); + +} + +#endif diff --git a/botan/src/libstate/lookup.cpp b/botan/src/libstate/lookup.cpp new file mode 100644 index 0000000..3b49116 --- /dev/null +++ b/botan/src/libstate/lookup.cpp @@ -0,0 +1,296 @@ +/* +* Algorithm Retrieval +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/lookup.h> +#include <botan/libstate.h> +#include <botan/engine.h> + +namespace Botan { + +/** +* Acquire a block cipher +*/ +const BlockCipher* retrieve_block_cipher(const std::string& algo_spec) + { + Algorithm_Factory& af = global_state().algorithm_factory(); + return af.prototype_block_cipher(algo_spec); + } + +/** +* Get a block cipher by name +*/ +BlockCipher* get_block_cipher(const std::string& algo_spec) + { + Algorithm_Factory& af = global_state().algorithm_factory(); + return af.make_block_cipher(algo_spec); + } + +/** +* Acquire a stream cipher +*/ +const StreamCipher* retrieve_stream_cipher(const std::string& algo_spec) + { + Algorithm_Factory& af = global_state().algorithm_factory(); + return af.prototype_stream_cipher(algo_spec); + } + +/** +* Get a stream cipher by name +*/ +StreamCipher* get_stream_cipher(const std::string& algo_spec) + { + Algorithm_Factory& af = global_state().algorithm_factory(); + return af.make_stream_cipher(algo_spec); + } + +/** +* Acquire a hash function +*/ +const HashFunction* retrieve_hash(const std::string& algo_spec) + { + Algorithm_Factory& af = global_state().algorithm_factory(); + return af.prototype_hash_function(algo_spec); + } + +/** +* Get a hash function by name +*/ +HashFunction* get_hash(const std::string& algo_spec) + { + Algorithm_Factory& af = global_state().algorithm_factory(); + return af.make_hash_function(algo_spec); + } + +/** +* Query if Botan has the named hash function +*/ +bool have_hash(const std::string& algo_spec) + { + Algorithm_Factory& af = global_state().algorithm_factory(); + return af.prototype_hash_function(algo_spec); + } + +/** +* Acquire an authentication code +*/ +const MessageAuthenticationCode* retrieve_mac(const std::string& algo_spec) + { + Algorithm_Factory& af = global_state().algorithm_factory(); + return af.prototype_mac(algo_spec); + } + +/** +* Get a MAC by name +*/ +MessageAuthenticationCode* get_mac(const std::string& algo_spec) + { + Algorithm_Factory& af = global_state().algorithm_factory(); + return af.make_mac(algo_spec); + } + +/** +* Query if Botan has the named MAC +*/ +bool have_mac(const std::string& algo_spec) + { + Algorithm_Factory& af = global_state().algorithm_factory(); + return af.prototype_mac(algo_spec); + } + +/** +* Query if an algorithm exists +*/ +bool have_algorithm(const std::string& name) + { + if(retrieve_block_cipher(name)) + return true; + if(retrieve_stream_cipher(name)) + return true; + if(retrieve_hash(name)) + return true; + if(retrieve_mac(name)) + return true; + return false; + } + +/** +* Query if Botan has the named block cipher +*/ +bool have_block_cipher(const std::string& name) + { + return (retrieve_block_cipher(name) != 0); + } + +/** +* Query if Botan has the named stream cipher +*/ +bool have_stream_cipher(const std::string& name) + { + return (retrieve_stream_cipher(name) != 0); + } + +/** +* Query the block size of a cipher or hash +*/ +u32bit block_size_of(const std::string& name) + { + const BlockCipher* cipher = retrieve_block_cipher(name); + if(cipher) + return cipher->BLOCK_SIZE; + + const HashFunction* hash = retrieve_hash(name); + if(hash) + return hash->HASH_BLOCK_SIZE; + + throw Algorithm_Not_Found(name); + } + +/** +* Query the OUTPUT_LENGTH of a hash or MAC +*/ +u32bit output_length_of(const std::string& name) + { + const HashFunction* hash = retrieve_hash(name); + if(hash) + return hash->OUTPUT_LENGTH; + + const MessageAuthenticationCode* mac = retrieve_mac(name); + if(mac) + return mac->OUTPUT_LENGTH; + + throw Algorithm_Not_Found(name); + } + +/** +* Check if a keylength is valid for this algo +*/ +bool valid_keylength_for(u32bit key_len, const std::string& name) + { + const BlockCipher* bc = retrieve_block_cipher(name); + if(bc) + return bc->valid_keylength(key_len); + + const StreamCipher* sc = retrieve_stream_cipher(name); + if(sc) + return sc->valid_keylength(key_len); + + const MessageAuthenticationCode* mac = retrieve_mac(name); + if(mac) + return mac->valid_keylength(key_len); + + throw Algorithm_Not_Found(name); + } + +/** +* Query the MINIMUM_KEYLENGTH of an algorithm +*/ +u32bit min_keylength_of(const std::string& name) + { + const BlockCipher* bc = retrieve_block_cipher(name); + if(bc) + return bc->MINIMUM_KEYLENGTH; + + const StreamCipher* sc = retrieve_stream_cipher(name); + if(sc) + return sc->MINIMUM_KEYLENGTH; + + const MessageAuthenticationCode* mac = retrieve_mac(name); + if(mac) + return mac->MINIMUM_KEYLENGTH; + + throw Algorithm_Not_Found(name); + } + +/** +* Query the MAXIMUM_KEYLENGTH of an algorithm +*/ +u32bit max_keylength_of(const std::string& name) + { + const BlockCipher* bc = retrieve_block_cipher(name); + if(bc) + return bc->MAXIMUM_KEYLENGTH; + + const StreamCipher* sc = retrieve_stream_cipher(name); + if(sc) + return sc->MAXIMUM_KEYLENGTH; + + const MessageAuthenticationCode* mac = retrieve_mac(name); + if(mac) + return mac->MAXIMUM_KEYLENGTH; + + throw Algorithm_Not_Found(name); + } + +/** +* Query the KEYLENGTH_MULTIPLE of an algorithm +*/ +u32bit keylength_multiple_of(const std::string& name) + { + const BlockCipher* bc = retrieve_block_cipher(name); + if(bc) + return bc->KEYLENGTH_MULTIPLE; + + const StreamCipher* sc = retrieve_stream_cipher(name); + if(sc) + return sc->KEYLENGTH_MULTIPLE; + + const MessageAuthenticationCode* mac = retrieve_mac(name); + if(mac) + return mac->KEYLENGTH_MULTIPLE; + + throw Algorithm_Not_Found(name); + } + +/** +* Get a cipher object +*/ +Keyed_Filter* get_cipher(const std::string& algo_spec, + Cipher_Dir direction) + { + Algorithm_Factory& af = global_state().algorithm_factory(); + + Algorithm_Factory::Engine_Iterator i(af); + + while(Engine* engine = i.next()) + { + Keyed_Filter* algo = engine->get_cipher(algo_spec, direction, af); + if(algo) + return algo; + } + + throw Algorithm_Not_Found(algo_spec); + } + +/** +* Get a cipher object +*/ +Keyed_Filter* get_cipher(const std::string& algo_spec, + const SymmetricKey& key, + const InitializationVector& iv, + Cipher_Dir direction) + { + Keyed_Filter* cipher = get_cipher(algo_spec, direction); + cipher->set_key(key); + + if(iv.length()) + cipher->set_iv(iv); + + return cipher; + } + +/** +* Get a cipher object +*/ +Keyed_Filter* get_cipher(const std::string& algo_spec, + const SymmetricKey& key, + Cipher_Dir direction) + { + return get_cipher(algo_spec, + key, InitializationVector(), direction); + } + +} diff --git a/botan/src/libstate/lookup.h b/botan/src/libstate/lookup.h new file mode 100644 index 0000000..0f48ddd --- /dev/null +++ b/botan/src/libstate/lookup.h @@ -0,0 +1,239 @@ +/* +* Algorithm Lookup +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_LOOKUP_H__ +#define BOTAN_LOOKUP_H__ + +#include <botan/block_cipher.h> +#include <botan/stream_cipher.h> +#include <botan/hash.h> +#include <botan/mac.h> +#include <botan/s2k.h> + +#include <botan/filters.h> +#include <botan/mode_pad.h> +#include <botan/kdf.h> +#include <botan/eme.h> +#include <botan/emsa.h> + +namespace Botan { + +/* +* Retrieve an object from the lookup table +*/ +// NOTE: these functions return internally stored objects, library +// retains ownership + +BOTAN_DLL const BlockCipher* +retrieve_block_cipher(const std::string&); + +BOTAN_DLL const StreamCipher* +retrieve_stream_cipher(const std::string&); + +BOTAN_DLL const HashFunction* +retrieve_hash(const std::string&); + +BOTAN_DLL const MessageAuthenticationCode* +retrieve_mac(const std::string&); + +/* +* Get an algorithm object +*/ +// NOTE: these functions create and return new objects, letting the +// caller assume ownership of them + +/** +* Block cipher factory method. +* @param algo_spec the name of the desired block cipher +* @return a pointer to the block cipher object +*/ +BOTAN_DLL BlockCipher* get_block_cipher(const std::string& name); + + +/** +* Stream cipher factory method. +* @param algo_spec the name of the desired stream cipher +* @return a pointer to the stream cipher object +*/ +BOTAN_DLL StreamCipher* get_stream_cipher(const std::string& name); + +/** +* Hash function factory method. +* @param algo_spec the name of the desired hash function +* @return a pointer to the hash function object +*/ +BOTAN_DLL HashFunction* get_hash(const std::string& name); + +/** +* MAC factory method. +* @param algo_spec the name of the desired MAC +* @return a pointer to the MAC object +*/ +BOTAN_DLL MessageAuthenticationCode* get_mac(const std::string& name); + +/** +* String to key algorithm factory method. +* @param name the name of the desired string to key (S2K) algorithm +* @return a pointer to the string to key algorithm object +*/ +BOTAN_DLL S2K* get_s2k(const std::string& name); + +/* +* Get an EMSA/EME/KDF/MGF function +*/ +// NOTE: these functions create and return new objects, letting the +// caller assume ownership of them + +/** +* Factory method for EME (message-encoding methods for encryption) objects +* @param name the name of the EME to create +* @return a pointer to the desired EME object +*/ +BOTAN_DLL EME* get_eme(const std::string& name); + +/** +* Factory method for EMSA (message-encoding methods for signatures +* with appendix) objects +* @param name the name of the EME to create +* @return a pointer to the desired EME object +*/ +BOTAN_DLL EMSA* get_emsa(const std::string& name); + +/** +* Factory method for KDF (key derivation function) +* @param name the name of the KDF to create +* @return a pointer to the desired KDF object +*/ +BOTAN_DLL KDF* get_kdf(const std::string& name); + +/* +* Get a cipher object +*/ + +/** +* Factory method for general symmetric cipher filters. +* @param algo_spec the name of the desired cipher +* @param key the key to be used for encryption/decryption performed by +* the filter +* @param iv the initialization vector to be used +* @param direction determines whether the filter will be an encrypting or decrypting +* filter +* @return a pointer to the encryption or decryption filter +*/ +BOTAN_DLL Keyed_Filter* get_cipher(const std::string& name, + const SymmetricKey& key, + const InitializationVector& iv, + Cipher_Dir dir); +/** +* Factory method for general symmetric cipher filters. +* @param algo_spec the name of the desired cipher +* @param key the key to be used for encryption/decryption performed by +* the filter +* @param direction determines whether the filter will be an encrypting or decrypting +* filter +* @return a pointer to the encryption or decryption filter +*/ +BOTAN_DLL Keyed_Filter* get_cipher(const std::string& name, + const SymmetricKey& key, + Cipher_Dir dir); + +/** Factory method for general symmetric cipher filters. No key will +* be set in the filter. +* @param algo_spec the name of the desired cipher + +* @param direction determines whether the filter will be an encrypting or +* decrypting filter +* @return a pointer to the encryption or decryption filter +*/ +BOTAN_DLL Keyed_Filter* get_cipher(const std::string& name, Cipher_Dir dir); + +/** +* Check if an algorithm exists. +* @param name the name of the algorithm to check for +* @return true if the algorithm exists, false otherwise +*/ +BOTAN_DLL bool have_algorithm(const std::string& name); + +/** +* Check if a block cipher algorithm exists. +* @param name the name of the algorithm to check for +* @return true if the algorithm exists, false otherwise +*/ +BOTAN_DLL bool have_block_cipher(const std::string& name); + +/** +* Check if a stream cipher algorithm exists. +* @param name the name of the algorithm to check for +* @return true if the algorithm exists, false otherwise +*/ +BOTAN_DLL bool have_stream_cipher(const std::string& name); + +/** +* Check if a hash algorithm exists. +* @param algo_spec the name of the algorithm to check for +* @return true if the algorithm exists, false otherwise +*/ +BOTAN_DLL bool have_hash(const std::string& name); + +/** +* Check if a MAC algorithm exists. +* @param algo_spec the name of the algorithm to check for +* @return true if the algorithm exists, false otherwise +*/ +BOTAN_DLL bool have_mac(const std::string& name); + +/* +* Query information about an algorithm +*/ + +/** +* Find out the block size of a certain symmetric algorithm. +* @param name the name of the algorithm +* @return the block size of the specified algorithm +*/ +BOTAN_DLL u32bit block_size_of(const std::string& name); + +/** +* Find out the output length of a certain symmetric algorithm. +* @param name the name of the algorithm +* @return the output length of the specified algorithm +*/ +BOTAN_DLL u32bit output_length_of(const std::string& name); + +/** +* Find out the whether a certain key length is allowd for a given +* symmetric algorithm. +* @param key_len the key length in question +* @param name the name of the algorithm +* @return true if the key length is valid for that algorithm, false otherwise +*/ +BOTAN_DLL bool valid_keylength_for(u32bit keylen, const std::string& name); + +/** +* Find out the minimum key size of a certain symmetric algorithm. +* @param name the name of the algorithm +* @return the minimum key length of the specified algorithm +*/ +BOTAN_DLL u32bit min_keylength_of(const std::string& name); + +/** +* Find out the maximum key size of a certain symmetric algorithm. +* @param name the name of the algorithm +* @return the maximum key length of the specified algorithm +*/ +BOTAN_DLL u32bit max_keylength_of(const std::string& name); + +/** +* Find out the size any valid key is a multiple of for a certain algorithm. +* @param name the name of the algorithm +* @return the size any valid key is a multiple of +*/ +BOTAN_DLL u32bit keylength_multiple_of(const std::string& name); + +} + +#endif diff --git a/botan/src/libstate/oid_lookup/info.txt b/botan/src/libstate/oid_lookup/info.txt new file mode 100644 index 0000000..609eb91 --- /dev/null +++ b/botan/src/libstate/oid_lookup/info.txt @@ -0,0 +1,14 @@ +realname "OID Lookup" + +load_on dep + +define OID_LOOKUP + +<add> +oids.cpp +oids.h +</add> + +<requires> +asn1 +</requires> diff --git a/botan/src/libstate/oid_lookup/oids.cpp b/botan/src/libstate/oid_lookup/oids.cpp new file mode 100644 index 0000000..232c633 --- /dev/null +++ b/botan/src/libstate/oid_lookup/oids.cpp @@ -0,0 +1,76 @@ +/* +* OID Registry +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/oids.h> +#include <botan/libstate.h> + +namespace Botan { + +namespace OIDS { + +/* +* Register an OID to string mapping +*/ +void add_oid(const OID& oid, const std::string& name) + { + const std::string oid_str = oid.as_string(); + + if(!global_state().is_set("oid2str", oid_str)) + global_state().set("oid2str", oid_str, name); + if(!global_state().is_set("str2oid", name)) + global_state().set("str2oid", name, oid_str); + } + +/* +* Do an OID to string lookup +*/ +std::string lookup(const OID& oid) + { + std::string name = global_state().get("oid2str", oid.as_string()); + if(name == "") + return oid.as_string(); + return name; + } + +/* +* Do a string to OID lookup +*/ +OID lookup(const std::string& name) + { + std::string value = global_state().get("str2oid", name); + if(value != "") + return OID(value); + + try + { + return OID(name); + } + catch(Exception) + { + throw Lookup_Error("No object identifier found for " + name); + } + } + +/* +* Check to see if an OID exists in the table +*/ +bool have_oid(const std::string& name) + { + return global_state().is_set("str2oid", name); + } + +/* +* Check to see if an OID exists in the table +*/ +bool name_of(const OID& oid, const std::string& name) + { + return (oid == lookup(name)); + } + +} + +} diff --git a/botan/src/libstate/oid_lookup/oids.h b/botan/src/libstate/oid_lookup/oids.h new file mode 100644 index 0000000..fdfe61f --- /dev/null +++ b/botan/src/libstate/oid_lookup/oids.h @@ -0,0 +1,58 @@ +/* +* OID Registry +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_OIDS_H__ +#define BOTAN_OIDS_H__ + +#include <botan/asn1_oid.h> + +namespace Botan { + +namespace OIDS { + +/** +* Register an OID to string mapping. +* @param oid the oid to register +* @param name the name to be associated with the oid +*/ +BOTAN_DLL void add_oid(const OID& oid, const std::string& name); + +/** +* See if an OID exists in the internal table. +* @param oid the oid to check for +* @return true if the oid is registered +*/ +BOTAN_DLL bool have_oid(const std::string& oid); + +/** +* Resolve an OID +* @param oid the OID to look up +* @return the name associated with this OID +*/ +BOTAN_DLL std::string lookup(const OID& oid); + +/** +* Find the OID to a name. The lookup will be performed in the +* general OID section of the configuration. +* @param name the name to resolve +* @return the OID associated with the specified name +*/ +BOTAN_DLL OID lookup(const std::string& name); + +/** +* Tests whether the specified OID stands for the specified name. +* @param oid the OID to check +* @param name the name to check +* @return true if the specified OID stands for the specified name +*/ +BOTAN_DLL bool name_of(const OID& oid, const std::string& name); + +} + +} + +#endif diff --git a/botan/src/libstate/pk_engine.cpp b/botan/src/libstate/pk_engine.cpp new file mode 100644 index 0000000..790ddcd --- /dev/null +++ b/botan/src/libstate/pk_engine.cpp @@ -0,0 +1,175 @@ +/* +* PK Engine Lookup +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/pk_engine.h> +#include <botan/libstate.h> +#include <botan/engine.h> + +namespace Botan { + +namespace Engine_Core { + +#if defined(BOTAN_HAS_IF_PUBLIC_KEY_FAMILY) +/* +* Acquire an IF op +*/ +IF_Operation* if_op(const BigInt& e, const BigInt& n, const BigInt& d, + const BigInt& p, const BigInt& q, const BigInt& d1, + const BigInt& d2, const BigInt& c) + { + Algorithm_Factory::Engine_Iterator i(global_state().algorithm_factory()); + + while(const Engine* engine = i.next()) + { + IF_Operation* op = engine->if_op(e, n, d, p, q, d1, d2, c); + if(op) + return op; + } + + throw Lookup_Error("Engine_Core::if_op: Unable to find a working engine"); + } +#endif + +#if defined(BOTAN_HAS_DSA) +/* +* Acquire a DSA op +*/ +DSA_Operation* dsa_op(const DL_Group& group, const BigInt& y, const BigInt& x) + { + Algorithm_Factory::Engine_Iterator i(global_state().algorithm_factory()); + + while(const Engine* engine = i.next()) + { + DSA_Operation* op = engine->dsa_op(group, y, x); + if(op) + return op; + } + + throw Lookup_Error("Engine_Core::dsa_op: Unable to find a working engine"); + } +#endif + +#if defined(BOTAN_HAS_NYBERG_RUEPPEL) +/* +* Acquire a NR op +*/ +NR_Operation* nr_op(const DL_Group& group, const BigInt& y, const BigInt& x) + { + Algorithm_Factory::Engine_Iterator i(global_state().algorithm_factory()); + + while(const Engine* engine = i.next()) + { + NR_Operation* op = engine->nr_op(group, y, x); + if(op) + return op; + } + + throw Lookup_Error("Engine_Core::nr_op: Unable to find a working engine"); + } +#endif + +#if defined(BOTAN_HAS_ELGAMAL) +/* +* Acquire an ElGamal op +*/ +ELG_Operation* elg_op(const DL_Group& group, const BigInt& y, const BigInt& x) + { + Algorithm_Factory::Engine_Iterator i(global_state().algorithm_factory()); + + while(const Engine* engine = i.next()) + { + ELG_Operation* op = engine->elg_op(group, y, x); + if(op) + return op; + } + + throw Lookup_Error("Engine_Core::elg_op: Unable to find a working engine"); + } +#endif + +#if defined(BOTAN_HAS_DIFFIE_HELLMAN) +/* +* Acquire a DH op +*/ +DH_Operation* dh_op(const DL_Group& group, const BigInt& x) + { + Algorithm_Factory::Engine_Iterator i(global_state().algorithm_factory()); + + while(const Engine* engine = i.next()) + { + DH_Operation* op = engine->dh_op(group, x); + if(op) + return op; + } + + throw Lookup_Error("Engine_Core::dh_op: Unable to find a working engine"); + } +#endif + +#if defined(BOTAN_HAS_ECDSA) +/* +* Acquire an ECDSA op +*/ +ECDSA_Operation* ecdsa_op(const EC_Domain_Params& dom_pars, + const BigInt& priv_key, + const PointGFp& pub_key) + { + Algorithm_Factory::Engine_Iterator i(global_state().algorithm_factory()); + + while(const Engine* engine = i.next()) + { + ECDSA_Operation* op = engine->ecdsa_op(dom_pars, priv_key, pub_key); + if(op) + return op; + } + + throw Lookup_Error("Engine_Core::ecdsa_op: Unable to find a working engine"); + } +#endif + +#if defined(BOTAN_HAS_ECKAEG) +/* +* Acquire a ECKAEG op +*/ +ECKAEG_Operation* eckaeg_op(const EC_Domain_Params& dom_pars, + const BigInt& priv_key, + const PointGFp& pub_key) + { + Algorithm_Factory::Engine_Iterator i(global_state().algorithm_factory()); + + while(const Engine* engine = i.next()) + { + ECKAEG_Operation* op = engine->eckaeg_op(dom_pars, priv_key, pub_key); + if(op) + return op; + } + + throw Lookup_Error("Engine_Core::eckaeg_op: Unable to find a working engine"); + } +#endif + +/* +* Acquire a modular exponentiator +*/ +Modular_Exponentiator* mod_exp(const BigInt& n, Power_Mod::Usage_Hints hints) + { + Algorithm_Factory::Engine_Iterator i(global_state().algorithm_factory()); + + while(const Engine* engine = i.next()) + { + Modular_Exponentiator* op = engine->mod_exp(n, hints); + + if(op) + return op; + } + + throw Lookup_Error("Engine_Core::mod_exp: Unable to find a working engine"); + } + +} + +} diff --git a/botan/src/libstate/pk_engine.h b/botan/src/libstate/pk_engine.h new file mode 100644 index 0000000..256a47c --- /dev/null +++ b/botan/src/libstate/pk_engine.h @@ -0,0 +1,95 @@ +/** +* Engine for PK +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ENGINE_PK_LOOKUP_H__ +#define BOTAN_ENGINE_PK_LOOKUP_H__ + +#include <botan/bigint.h> +#include <botan/pow_mod.h> + +#if defined(BOTAN_HAS_IF_PUBLIC_KEY_FAMILY) + #include <botan/if_op.h> +#endif + +#if defined(BOTAN_HAS_DSA) + #include <botan/dsa_op.h> +#endif + +#if defined(BOTAN_HAS_DIFFIE_HELLMAN) + #include <botan/dh_op.h> +#endif + +#if defined(BOTAN_HAS_NYBERG_RUEPPEL) + #include <botan/nr_op.h> +#endif + +#if defined(BOTAN_HAS_ELGAMAL) + #include <botan/elg_op.h> +#endif + +#if defined(BOTAN_HAS_ECDSA) + #include <botan/ecdsa_op.h> + #include <botan/ec_dompar.h> +#endif + +#if defined(BOTAN_HAS_ECKAEG) + #include <botan/eckaeg_op.h> + #include <botan/ec_dompar.h> +#endif + +namespace Botan { + +class Algorithm_Factory; +class Keyed_Filter; +class Modular_Exponentiator; + +namespace Engine_Core { + +/* +* Get an operation from an Engine +*/ +Modular_Exponentiator* mod_exp(const BigInt&, Power_Mod::Usage_Hints); + +#if defined(BOTAN_HAS_IF_PUBLIC_KEY_FAMILY) +IF_Operation* if_op(const BigInt&, const BigInt&, const BigInt&, + const BigInt&, const BigInt&, const BigInt&, + const BigInt&, const BigInt&); +#endif + +#if defined(BOTAN_HAS_DSA) +DSA_Operation* dsa_op(const DL_Group&, const BigInt&, const BigInt&); +#endif + +#if defined(BOTAN_HAS_NYBERG_RUEPPEL) +NR_Operation* nr_op(const DL_Group&, const BigInt&, const BigInt&); +#endif + +#if defined(BOTAN_HAS_ELGAMAL) +ELG_Operation* elg_op(const DL_Group&, const BigInt&, const BigInt&); +#endif + +#if defined(BOTAN_HAS_DIFFIE_HELLMAN) +DH_Operation* dh_op(const DL_Group&, const BigInt&); +#endif + +#if defined(BOTAN_HAS_ECDSA) +ECDSA_Operation* ecdsa_op(const EC_Domain_Params& dom_pars, + const BigInt& priv_key, + const PointGFp& pub_key); +#endif + +#if defined(BOTAN_HAS_ECKAEG) +ECKAEG_Operation* eckaeg_op(const EC_Domain_Params& dom_pars, + const BigInt& priv_key, + const PointGFp& pub_key); +#endif + +} + +} + +#endif diff --git a/botan/src/libstate/policy.cpp b/botan/src/libstate/policy.cpp new file mode 100644 index 0000000..dfc1dfc --- /dev/null +++ b/botan/src/libstate/policy.cpp @@ -0,0 +1,511 @@ +/* +* Default Policy +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/libstate.h> + +namespace Botan { + +namespace { + +/* +* OID loading helper function +*/ +void add_oid(Library_State& config, + const std::string& oid_str, + const std::string& name) + { + if(!config.is_set("oid2str", oid_str)) + config.set("oid2str", oid_str, name); + if(!config.is_set("str2oid", name)) + config.set("str2oid", name, oid_str); + } + +/* +* Load all of the default OIDs +*/ +void set_default_oids(Library_State& config) + { + /* Public key types */ + add_oid(config, "1.2.840.113549.1.1.1", "RSA"); + add_oid(config, "2.5.8.1.1", "RSA"); // RSA alternate + add_oid(config, "1.2.840.10040.4.1", "DSA"); + add_oid(config, "1.2.840.10046.2.1", "DH"); + add_oid(config, "1.3.6.1.4.1.3029.1.2.1", "ELG"); + add_oid(config, "1.3.6.1.4.1.25258.1.1", "RW"); + add_oid(config, "1.3.6.1.4.1.25258.1.2", "NR"); + add_oid(config, "1.2.840.10045.2.1", "ECDSA"); // X9.62 + + /* Ciphers */ + add_oid(config, "1.3.14.3.2.7", "DES/CBC"); + add_oid(config, "1.2.840.113549.3.7", "TripleDES/CBC"); + add_oid(config, "1.2.840.113549.3.2", "RC2/CBC"); + add_oid(config, "1.2.840.113533.7.66.10", "CAST-128/CBC"); + add_oid(config, "2.16.840.1.101.3.4.1.2", "AES-128/CBC"); + add_oid(config, "2.16.840.1.101.3.4.1.22", "AES-192/CBC"); + add_oid(config, "2.16.840.1.101.3.4.1.42", "AES-256/CBC"); + + /* Hash Functions */ + add_oid(config, "1.2.840.113549.2.5", "MD5"); + add_oid(config, "1.3.6.1.4.1.11591.12.2", "Tiger(24,3)"); + + add_oid(config, "1.3.14.3.2.26", "SHA-160"); + add_oid(config, "2.16.840.1.101.3.4.2.4", "SHA-224"); + add_oid(config, "2.16.840.1.101.3.4.2.1", "SHA-256"); + add_oid(config, "2.16.840.1.101.3.4.2.2", "SHA-384"); + add_oid(config, "2.16.840.1.101.3.4.2.3", "SHA-512"); + + /* Key Wrap */ + add_oid(config, "1.2.840.113549.1.9.16.3.6", "KeyWrap.TripleDES"); + add_oid(config, "1.2.840.113549.1.9.16.3.7", "KeyWrap.RC2"); + add_oid(config, "1.2.840.113533.7.66.15", "KeyWrap.CAST-128"); + add_oid(config, "2.16.840.1.101.3.4.1.5", "KeyWrap.AES-128"); + add_oid(config, "2.16.840.1.101.3.4.1.25", "KeyWrap.AES-192"); + add_oid(config, "2.16.840.1.101.3.4.1.45", "KeyWrap.AES-256"); + + /* Compression */ + add_oid(config, "1.2.840.113549.1.9.16.3.8", "Compression.Zlib"); + + /* Public key signature schemes */ + add_oid(config, "1.2.840.113549.1.1.1", "RSA/EME-PKCS1-v1_5"); + add_oid(config, "1.2.840.113549.1.1.2", "RSA/EMSA3(MD2)"); + add_oid(config, "1.2.840.113549.1.1.4", "RSA/EMSA3(MD5)"); + add_oid(config, "1.2.840.113549.1.1.5", "RSA/EMSA3(SHA-160)"); + add_oid(config, "1.2.840.113549.1.1.11", "RSA/EMSA3(SHA-256)"); + add_oid(config, "1.2.840.113549.1.1.12", "RSA/EMSA3(SHA-384)"); + add_oid(config, "1.2.840.113549.1.1.13", "RSA/EMSA3(SHA-512)"); + add_oid(config, "1.3.36.3.3.1.2", "RSA/EMSA3(RIPEMD-160)"); + + add_oid(config, "1.2.840.10040.4.3", "DSA/EMSA1(SHA-160)"); + add_oid(config, "2.16.840.1.101.3.4.3.1", "DSA/EMSA1(SHA-224)"); + add_oid(config, "2.16.840.1.101.3.4.3.2", "DSA/EMSA1(SHA-256)"); + + add_oid(config, "1.2.840.10045.4.1", "ECDSA/EMSA1_BSI(SHA-160)"); + add_oid(config, "1.2.840.10045.4.3.1", "ECDSA/EMSA1_BSI(SHA-224)"); + add_oid(config, "1.2.840.10045.4.3.2", "ECDSA/EMSA1_BSI(SHA-256)"); + add_oid(config, "1.2.840.10045.4.3.3", "ECDSA/EMSA1_BSI(SHA-384)"); + add_oid(config, "1.2.840.10045.4.3.4", "ECDSA/EMSA1_BSI(SHA-512)"); + + add_oid(config, "1.2.840.10045.4.3.1", "ECDSA/EMSA1(SHA-224)"); + add_oid(config, "1.2.840.10045.4.3.2", "ECDSA/EMSA1(SHA-256)"); + add_oid(config, "1.2.840.10045.4.3.3", "ECDSA/EMSA1(SHA-384)"); + add_oid(config, "1.2.840.10045.4.3.4", "ECDSA/EMSA1(SHA-512)"); + + add_oid(config, "1.3.6.1.4.1.25258.2.1.1.1", "RW/EMSA2(RIPEMD-160)"); + add_oid(config, "1.3.6.1.4.1.25258.2.1.1.2", "RW/EMSA2(SHA-160)"); + add_oid(config, "1.3.6.1.4.1.25258.2.1.1.3", "RW/EMSA2(SHA-224)"); + add_oid(config, "1.3.6.1.4.1.25258.2.1.1.4", "RW/EMSA2(SHA-256)"); + add_oid(config, "1.3.6.1.4.1.25258.2.1.1.5", "RW/EMSA2(SHA-384)"); + add_oid(config, "1.3.6.1.4.1.25258.2.1.1.6", "RW/EMSA2(SHA-512)"); + + add_oid(config, "1.3.6.1.4.1.25258.2.1.2.1", "RW/EMSA4(RIPEMD-160)"); + add_oid(config, "1.3.6.1.4.1.25258.2.1.2.2", "RW/EMSA4(SHA-160)"); + add_oid(config, "1.3.6.1.4.1.25258.2.1.2.3", "RW/EMSA4(SHA-224)"); + add_oid(config, "1.3.6.1.4.1.25258.2.1.2.4", "RW/EMSA4(SHA-256)"); + add_oid(config, "1.3.6.1.4.1.25258.2.1.2.5", "RW/EMSA4(SHA-384)"); + add_oid(config, "1.3.6.1.4.1.25258.2.1.2.6", "RW/EMSA4(SHA-512)"); + + add_oid(config, "1.3.6.1.4.1.25258.2.2.1.1", "NR/EMSA2(RIPEMD-160)"); + add_oid(config, "1.3.6.1.4.1.25258.2.2.1.2", "NR/EMSA2(SHA-160)"); + add_oid(config, "1.3.6.1.4.1.25258.2.2.1.3", "NR/EMSA2(SHA-224)"); + add_oid(config, "1.3.6.1.4.1.25258.2.2.1.4", "NR/EMSA2(SHA-256)"); + add_oid(config, "1.3.6.1.4.1.25258.2.2.1.5", "NR/EMSA2(SHA-384)"); + add_oid(config, "1.3.6.1.4.1.25258.2.2.1.6", "NR/EMSA2(SHA-512)"); + + add_oid(config, "2.5.4.3", "X520.CommonName"); + add_oid(config, "2.5.4.4", "X520.Surname"); + add_oid(config, "2.5.4.5", "X520.SerialNumber"); + add_oid(config, "2.5.4.6", "X520.Country"); + add_oid(config, "2.5.4.7", "X520.Locality"); + add_oid(config, "2.5.4.8", "X520.State"); + add_oid(config, "2.5.4.10", "X520.Organization"); + add_oid(config, "2.5.4.11", "X520.OrganizationalUnit"); + add_oid(config, "2.5.4.12", "X520.Title"); + add_oid(config, "2.5.4.42", "X520.GivenName"); + add_oid(config, "2.5.4.43", "X520.Initials"); + add_oid(config, "2.5.4.44", "X520.GenerationalQualifier"); + add_oid(config, "2.5.4.46", "X520.DNQualifier"); + add_oid(config, "2.5.4.65", "X520.Pseudonym"); + + add_oid(config, "1.2.840.113549.1.5.12", "PKCS5.PBKDF2"); + add_oid(config, "1.2.840.113549.1.5.1", "PBE-PKCS5v15(MD2,DES/CBC)"); + add_oid(config, "1.2.840.113549.1.5.4", "PBE-PKCS5v15(MD2,RC2/CBC)"); + add_oid(config, "1.2.840.113549.1.5.3", "PBE-PKCS5v15(MD5,DES/CBC)"); + add_oid(config, "1.2.840.113549.1.5.6", "PBE-PKCS5v15(MD5,RC2/CBC)"); + add_oid(config, "1.2.840.113549.1.5.10", "PBE-PKCS5v15(SHA-160,DES/CBC)"); + add_oid(config, "1.2.840.113549.1.5.11", "PBE-PKCS5v15(SHA-160,RC2/CBC)"); + add_oid(config, "1.2.840.113549.1.5.13", "PBE-PKCS5v20"); + + add_oid(config, "1.2.840.113549.1.9.1", "PKCS9.EmailAddress"); + add_oid(config, "1.2.840.113549.1.9.2", "PKCS9.UnstructuredName"); + add_oid(config, "1.2.840.113549.1.9.3", "PKCS9.ContentType"); + add_oid(config, "1.2.840.113549.1.9.4", "PKCS9.MessageDigest"); + add_oid(config, "1.2.840.113549.1.9.7", "PKCS9.ChallengePassword"); + add_oid(config, "1.2.840.113549.1.9.14", "PKCS9.ExtensionRequest"); + + add_oid(config, "1.2.840.113549.1.7.1", "CMS.DataContent"); + add_oid(config, "1.2.840.113549.1.7.2", "CMS.SignedData"); + add_oid(config, "1.2.840.113549.1.7.3", "CMS.EnvelopedData"); + add_oid(config, "1.2.840.113549.1.7.5", "CMS.DigestedData"); + add_oid(config, "1.2.840.113549.1.7.6", "CMS.EncryptedData"); + add_oid(config, "1.2.840.113549.1.9.16.1.2", "CMS.AuthenticatedData"); + add_oid(config, "1.2.840.113549.1.9.16.1.9", "CMS.CompressedData"); + + add_oid(config, "2.5.29.14", "X509v3.SubjectKeyIdentifier"); + add_oid(config, "2.5.29.15", "X509v3.KeyUsage"); + add_oid(config, "2.5.29.17", "X509v3.SubjectAlternativeName"); + add_oid(config, "2.5.29.18", "X509v3.IssuerAlternativeName"); + add_oid(config, "2.5.29.19", "X509v3.BasicConstraints"); + add_oid(config, "2.5.29.20", "X509v3.CRLNumber"); + add_oid(config, "2.5.29.21", "X509v3.ReasonCode"); + add_oid(config, "2.5.29.23", "X509v3.HoldInstructionCode"); + add_oid(config, "2.5.29.24", "X509v3.InvalidityDate"); + add_oid(config, "2.5.29.32", "X509v3.CertificatePolicies"); + add_oid(config, "2.5.29.35", "X509v3.AuthorityKeyIdentifier"); + add_oid(config, "2.5.29.36", "X509v3.PolicyConstraints"); + add_oid(config, "2.5.29.37", "X509v3.ExtendedKeyUsage"); + + add_oid(config, "2.5.29.32.0", "X509v3.AnyPolicy"); + + add_oid(config, "1.3.6.1.5.5.7.3.1", "PKIX.ServerAuth"); + add_oid(config, "1.3.6.1.5.5.7.3.2", "PKIX.ClientAuth"); + add_oid(config, "1.3.6.1.5.5.7.3.3", "PKIX.CodeSigning"); + add_oid(config, "1.3.6.1.5.5.7.3.4", "PKIX.EmailProtection"); + add_oid(config, "1.3.6.1.5.5.7.3.5", "PKIX.IPsecEndSystem"); + add_oid(config, "1.3.6.1.5.5.7.3.6", "PKIX.IPsecTunnel"); + add_oid(config, "1.3.6.1.5.5.7.3.7", "PKIX.IPsecUser"); + add_oid(config, "1.3.6.1.5.5.7.3.8", "PKIX.TimeStamping"); + add_oid(config, "1.3.6.1.5.5.7.3.9", "PKIX.OCSPSigning"); + + add_oid(config, "1.3.6.1.5.5.7.8.5", "PKIX.XMPPAddr"); + + /* CVC */ + add_oid(config, "0.4.0.127.0.7.3.1.2.1", + "CertificateHolderAuthorizationTemplate"); + } + +/* +* Set the default algorithm aliases +*/ +void set_default_aliases(Library_State& config) + { + config.add_alias("OpenPGP.Cipher.1", "IDEA"); + config.add_alias("OpenPGP.Cipher.2", "TripleDES"); + config.add_alias("OpenPGP.Cipher.3", "CAST-128"); + config.add_alias("OpenPGP.Cipher.4", "Blowfish"); + config.add_alias("OpenPGP.Cipher.5", "SAFER-SK(13)"); + config.add_alias("OpenPGP.Cipher.7", "AES-128"); + config.add_alias("OpenPGP.Cipher.8", "AES-192"); + config.add_alias("OpenPGP.Cipher.9", "AES-256"); + config.add_alias("OpenPGP.Cipher.10", "Twofish"); + + config.add_alias("OpenPGP.Digest.1", "MD5"); + config.add_alias("OpenPGP.Digest.2", "SHA-1"); + config.add_alias("OpenPGP.Digest.3", "RIPEMD-160"); + config.add_alias("OpenPGP.Digest.5", "MD2"); + config.add_alias("OpenPGP.Digest.6", "Tiger(24,3)"); + config.add_alias("OpenPGP.Digest.8", "SHA-256"); + + config.add_alias("TLS.Digest.0", "Parallel(MD5,SHA-160)"); + + config.add_alias("EME-PKCS1-v1_5", "PKCS1v15"); + config.add_alias("OAEP-MGF1", "EME1"); + config.add_alias("EME-OAEP", "EME1"); + config.add_alias("X9.31", "EMSA2"); + config.add_alias("EMSA-PKCS1-v1_5", "EMSA3"); + config.add_alias("PSS-MGF1", "EMSA4"); + config.add_alias("EMSA-PSS", "EMSA4"); + + config.add_alias("Rijndael", "AES"); + config.add_alias("3DES", "TripleDES"); + config.add_alias("DES-EDE", "TripleDES"); + config.add_alias("CAST5", "CAST-128"); + config.add_alias("SHA1", "SHA-160"); + config.add_alias("SHA-1", "SHA-160"); + config.add_alias("MARK-4", "ARC4(256)"); + config.add_alias("OMAC", "CMAC"); + config.add_alias("GOST", "GOST-28147-89"); + } + +/* +* Set the default configuration toggles +*/ +void set_default_config(Library_State& config) + { + config.set_option("base/default_allocator", "malloc"); + + config.set_option("x509/exts/basic_constraints", "critical"); + config.set_option("x509/exts/subject_key_id", "yes"); + config.set_option("x509/exts/authority_key_id", "yes"); + config.set_option("x509/exts/subject_alternative_name", "yes"); + config.set_option("x509/exts/issuer_alternative_name", "no"); + config.set_option("x509/exts/key_usage", "critical"); + config.set_option("x509/exts/extended_key_usage", "yes"); + config.set_option("x509/exts/crl_number", "yes"); + } + +/* +* Set the built-in discrete log groups +*/ +void set_default_dl_groups(Library_State& config) + { + config.set("dl", "modp/ietf/768", + "-----BEGIN X942 DH PARAMETERS-----" + "MIHIAmEA///////////JD9qiIWjCNMTGYouA3BzRKQJOCIpnzHQCC76mOxObIlFK" + "CHmONATd75UZs806QxswKwpt8l8UN0/hNW1tUcJF5IW1dmJefsb0TELppjo2IP//" + "////////AgECAmB//////////+SH7VEQtGEaYmMxRcBuDmiUgScERTPmOgEF31Md" + "ic2RKKUEPMcaAm73yozZ5p0hjZgVhTb5L4obp/Catrao4SLyQtq7MS8/Y3omIXTT" + "HRsQf/////////8=" + "-----END X942 DH PARAMETERS-----"); + + config.set("dl", "modp/ietf/1024", + "-----BEGIN X942 DH PARAMETERS-----" + "MIIBCgKBgQD//////////8kP2qIhaMI0xMZii4DcHNEpAk4IimfMdAILvqY7E5si" + "UUoIeY40BN3vlRmzzTpDGzArCm3yXxQ3T+E1bW1RwkXkhbV2Yl5+xvRMQummN+1r" + "C/9ctvQGt+3uOGv7Womfpa6fJBF8Sx/mSShmUezmU4H//////////wIBAgKBgH//" + "////////5IftURC0YRpiYzFFwG4OaJSBJwRFM+Y6AQXfUx2JzZEopQQ8xxoCbvfK" + "jNnmnSGNmBWFNvkvihun8Jq2tqjhIvJC2rsxLz9jeiYhdNMb9rWF/65begNb9vcc" + "Nf2tRM/S10+SCL4lj/MklDMo9nMpwP//////////" + "-----END X942 DH PARAMETERS-----"); + + config.set("dl", "modp/ietf/1536", + "-----BEGIN X942 DH PARAMETERS-----" + "MIIBigKBwQD//////////8kP2qIhaMI0xMZii4DcHNEpAk4IimfMdAILvqY7E5si" + "UUoIeY40BN3vlRmzzTpDGzArCm3yXxQ3T+E1bW1RwkXkhbV2Yl5+xvRMQummN+1r" + "C/9ctvQGt+3uOGv7Womfpa6fJBF8Sx/mSShmUezkWz3CAHy4oWO/BZjaSDYcVdOa" + "aRY/qP0kz1+DZV0j3KOtlhxi81YghVK7ntUpB3CWlm1nDDVOSryYBPF0bAjKI3Mn" + "//////////8CAQICgcB//////////+SH7VEQtGEaYmMxRcBuDmiUgScERTPmOgEF" + "31Mdic2RKKUEPMcaAm73yozZ5p0hjZgVhTb5L4obp/Catrao4SLyQtq7MS8/Y3om" + "IXTTG/a1hf+uW3oDW/b3HDX9rUTP0tdPkgi+JY/zJJQzKPZyLZ7hAD5cULHfgsxt" + "JBsOKunNNIsf1H6SZ6/Bsq6R7lHWyw4xeasQQqldz2qUg7hLSzazhhqnJV5MAni6" + "NgRlEbmT//////////8=" + "-----END X942 DH PARAMETERS-----"); + + config.set("dl", "modp/ietf/2048", + "-----BEGIN X942 DH PARAMETERS-----" + "MIICDAKCAQEA///////////JD9qiIWjCNMTGYouA3BzRKQJOCIpnzHQCC76mOxOb" + "IlFKCHmONATd75UZs806QxswKwpt8l8UN0/hNW1tUcJF5IW1dmJefsb0TELppjft" + "awv/XLb0Brft7jhr+1qJn6WunyQRfEsf5kkoZlHs5Fs9wgB8uKFjvwWY2kg2HFXT" + "mmkWP6j9JM9fg2VdI9yjrZYcYvNWIIVSu57VKQdwlpZtZww1Tkq8mATxdGwIyhgh" + "fDKQXkYuNs474553LBgOhgObJ4Oi7Aeij7XFXfBvTFLJ3ivL9pVYFxg5lUl86pVq" + "5RXSJhiY+gUQFXKOWoqsqmj//////////wIBAgKCAQB//////////+SH7VEQtGEa" + "YmMxRcBuDmiUgScERTPmOgEF31Mdic2RKKUEPMcaAm73yozZ5p0hjZgVhTb5L4ob" + "p/Catrao4SLyQtq7MS8/Y3omIXTTG/a1hf+uW3oDW/b3HDX9rUTP0tdPkgi+JY/z" + "JJQzKPZyLZ7hAD5cULHfgsxtJBsOKunNNIsf1H6SZ6/Bsq6R7lHWyw4xeasQQqld" + "z2qUg7hLSzazhhqnJV5MAni6NgRlDBC+GUgvIxcbZx3xzzuWDAdDAc2TwdF2A9FH" + "2uKu+DemKWTvFeX7SqwLjBzKpL51SrVyiukTDEx9AogKuUctRVZVNH//////////" + "-----END X942 DH PARAMETERS-----"); + + config.set("dl", "modp/ietf/3072", + "-----BEGIN X942 DH PARAMETERS-----" + "MIIDDAKCAYEA///////////JD9qiIWjCNMTGYouA3BzRKQJOCIpnzHQCC76mOxOb" + "IlFKCHmONATd75UZs806QxswKwpt8l8UN0/hNW1tUcJF5IW1dmJefsb0TELppjft" + "awv/XLb0Brft7jhr+1qJn6WunyQRfEsf5kkoZlHs5Fs9wgB8uKFjvwWY2kg2HFXT" + "mmkWP6j9JM9fg2VdI9yjrZYcYvNWIIVSu57VKQdwlpZtZww1Tkq8mATxdGwIyhgh" + "fDKQXkYuNs474553LBgOhgObJ4Oi7Aeij7XFXfBvTFLJ3ivL9pVYFxg5lUl86pVq" + "5RXSJhiY+gUQFXKOWoqqxC2tMxcNBFB6M6hVIavfHLpk7PuFBFjb7wqK6nFXXQYM" + "fbOXD4Wm4eTHq/WujNsJM9cejJTgSiVhnc7j0iYa0u5r8S/6BtmKCGTYdgJzPshq" + "ZFIfKxgXeyAMu+EXV3phXWx3CYjAutlG4gjiT6B05asxQ9tb/OD9EI5LgtEgqTrS" + "yv//////////AgECAoIBgH//////////5IftURC0YRpiYzFFwG4OaJSBJwRFM+Y6" + "AQXfUx2JzZEopQQ8xxoCbvfKjNnmnSGNmBWFNvkvihun8Jq2tqjhIvJC2rsxLz9j" + "eiYhdNMb9rWF/65begNb9vccNf2tRM/S10+SCL4lj/MklDMo9nItnuEAPlxQsd+C" + "zG0kGw4q6c00ix/UfpJnr8GyrpHuUdbLDjF5qxBCqV3PapSDuEtLNrOGGqclXkwC" + "eLo2BGUMEL4ZSC8jFxtnHfHPO5YMB0MBzZPB0XYD0Ufa4q74N6YpZO8V5ftKrAuM" + "HMqkvnVKtXKK6RMMTH0CiAq5Ry1FVWIW1pmLhoIoPRnUKpDV745dMnZ9woIsbfeF" + "RXU4q66DBj7Zy4fC03DyY9X610ZthJnrj0ZKcCUSsM7ncekTDWl3NfiX/QNsxQQy" + "bDsBOZ9kNTIpD5WMC72QBl3wi6u9MK62O4TEYF1so3EEcSfQOnLVmKHtrf5wfohH" + "JcFokFSdaWV//////////w==" + "-----END X942 DH PARAMETERS-----"); + + config.set("dl", "modp/ietf/4096", + "-----BEGIN X942 DH PARAMETERS-----" + "MIIEDAKCAgEA///////////JD9qiIWjCNMTGYouA3BzRKQJOCIpnzHQCC76mOxOb" + "IlFKCHmONATd75UZs806QxswKwpt8l8UN0/hNW1tUcJF5IW1dmJefsb0TELppjft" + "awv/XLb0Brft7jhr+1qJn6WunyQRfEsf5kkoZlHs5Fs9wgB8uKFjvwWY2kg2HFXT" + "mmkWP6j9JM9fg2VdI9yjrZYcYvNWIIVSu57VKQdwlpZtZww1Tkq8mATxdGwIyhgh" + "fDKQXkYuNs474553LBgOhgObJ4Oi7Aeij7XFXfBvTFLJ3ivL9pVYFxg5lUl86pVq" + "5RXSJhiY+gUQFXKOWoqqxC2tMxcNBFB6M6hVIavfHLpk7PuFBFjb7wqK6nFXXQYM" + "fbOXD4Wm4eTHq/WujNsJM9cejJTgSiVhnc7j0iYa0u5r8S/6BtmKCGTYdgJzPshq" + "ZFIfKxgXeyAMu+EXV3phXWx3CYjAutlG4gjiT6B05asxQ9tb/OD9EI5LgtEgqSEI" + "ARpyPBKnh+bXiHGaEL26WyaZwycYavTiPBqUaDS2FQvaJYPpyirUTOjbu8LbBN6O" + "+S6O/BQfvsqmKHxZR05rwF2ZspZPoJDDoiM7oYZRW+ftH2EpcM7i16+4G912IXBI" + "HNAGkSfVsFqpk7TqmI2P3cGG/7fckKbAj030Nck0BjGZ//////////8CAQICggIA" + "f//////////kh+1RELRhGmJjMUXAbg5olIEnBEUz5joBBd9THYnNkSilBDzHGgJu" + "98qM2eadIY2YFYU2+S+KG6fwmra2qOEi8kLauzEvP2N6JiF00xv2tYX/rlt6A1v2" + "9xw1/a1Ez9LXT5IIviWP8ySUMyj2ci2e4QA+XFCx34LMbSQbDirpzTSLH9R+kmev" + "wbKuke5R1ssOMXmrEEKpXc9qlIO4S0s2s4YapyVeTAJ4ujYEZQwQvhlILyMXG2cd" + "8c87lgwHQwHNk8HRdgPRR9rirvg3pilk7xXl+0qsC4wcyqS+dUq1corpEwxMfQKI" + "CrlHLUVVYhbWmYuGgig9GdQqkNXvjl0ydn3Cgixt94VFdTirroMGPtnLh8LTcPJj" + "1frXRm2EmeuPRkpwJRKwzudx6RMNaXc1+Jf9A2zFBDJsOwE5n2Q1MikPlYwLvZAG" + "XfCLq70wrrY7hMRgXWyjcQRxJ9A6ctWYoe2t/nB+iEclwWiQVJCEAI05HglTw/Nr" + "xDjNCF7dLZNM4ZOMNXpxHg1KNBpbCoXtEsH05RVqJnRt3eFtgm9HfJdHfgoP32VT" + "FD4so6c14C7M2Usn0Ehh0RGd0MMorfP2j7CUuGdxa9fcDe67ELgkDmgDSJPq2C1U" + "ydp1TEbH7uDDf9vuSFNgR6b6GuSaAxjM//////////8=" + "-----END X942 DH PARAMETERS-----"); + + config.set("dl", "modp/ietf/6144", + "-----BEGIN X942 DH PARAMETERS-----" + "MIIGDAKCAwEA///////////JD9qiIWjCNMTGYouA3BzRKQJOCIpnzHQCC76mOxOb" + "IlFKCHmONATd75UZs806QxswKwpt8l8UN0/hNW1tUcJF5IW1dmJefsb0TELppjft" + "awv/XLb0Brft7jhr+1qJn6WunyQRfEsf5kkoZlHs5Fs9wgB8uKFjvwWY2kg2HFXT" + "mmkWP6j9JM9fg2VdI9yjrZYcYvNWIIVSu57VKQdwlpZtZww1Tkq8mATxdGwIyhgh" + "fDKQXkYuNs474553LBgOhgObJ4Oi7Aeij7XFXfBvTFLJ3ivL9pVYFxg5lUl86pVq" + "5RXSJhiY+gUQFXKOWoqqxC2tMxcNBFB6M6hVIavfHLpk7PuFBFjb7wqK6nFXXQYM" + "fbOXD4Wm4eTHq/WujNsJM9cejJTgSiVhnc7j0iYa0u5r8S/6BtmKCGTYdgJzPshq" + "ZFIfKxgXeyAMu+EXV3phXWx3CYjAutlG4gjiT6B05asxQ9tb/OD9EI5LgtEgqSEI" + "ARpyPBKnh+bXiHGaEL26WyaZwycYavTiPBqUaDS2FQvaJYPpyirUTOjbu8LbBN6O" + "+S6O/BQfvsqmKHxZR05rwF2ZspZPoJDDoiM7oYZRW+ftH2EpcM7i16+4G912IXBI" + "HNAGkSfVsFqpk7TqmI2P3cGG/7fckKbAj030Nck0AoSSNsP6tNJ8cCbB1NyyYCZG" + "3sl1HnY9uje9+P+UBq2eUw7l2zgvQTABrrBqU+2QJ9gxF5cnsIZaiRjaPtvrz5sU" + "7UTObLrO1Lsb238UR+bMJUszIFFRK9evQm+49AE3jNK/WYPKAcZLkuzwMuoV0XId" + "A/SC185udP721V5wL0aYDIK1qEAxkAscnlnnyX++x+jzI6l6fjbMiL4PHUW3/1ha" + "xUvUB7IrQVSqzI9tfr9I4dgUzF7SD4A34KeXFe7ym+MoBqHVi7fF2nb1UKo9ih+/" + "8OsZzLGjE9Vc2lbJ7C7yljI4f+jXbjwEaAQ+j2Y/SGDuEr8tWwt0dNbmlPkebcxA" + "JP//////////AoIDAH//////////5IftURC0YRpiYzFFwG4OaJSBJwRFM+Y6AQXf" + "Ux2JzZEopQQ8xxoCbvfKjNnmnSGNmBWFNvkvihun8Jq2tqjhIvJC2rsxLz9jeiYh" + "dNMb9rWF/65begNb9vccNf2tRM/S10+SCL4lj/MklDMo9nItnuEAPlxQsd+CzG0k" + "Gw4q6c00ix/UfpJnr8GyrpHuUdbLDjF5qxBCqV3PapSDuEtLNrOGGqclXkwCeLo2" + "BGUMEL4ZSC8jFxtnHfHPO5YMB0MBzZPB0XYD0Ufa4q74N6YpZO8V5ftKrAuMHMqk" + "vnVKtXKK6RMMTH0CiAq5Ry1FVWIW1pmLhoIoPRnUKpDV745dMnZ9woIsbfeFRXU4" + "q66DBj7Zy4fC03DyY9X610ZthJnrj0ZKcCUSsM7ncekTDWl3NfiX/QNsxQQybDsB" + "OZ9kNTIpD5WMC72QBl3wi6u9MK62O4TEYF1so3EEcSfQOnLVmKHtrf5wfohHJcFo" + "kFSQhACNOR4JU8Pza8Q4zQhe3S2TTOGTjDV6cR4NSjQaWwqF7RLB9OUVaiZ0bd3h" + "bYJvR3yXR34KD99lUxQ+LKOnNeAuzNlLJ9BIYdERndDDKK3z9o+wlLhncWvX3A3u" + "uxC4JA5oA0iT6tgtVMnadUxGx+7gw3/b7khTYEem+hrkmgFCSRth/VppPjgTYOpu" + "WTATI29kuo87Ht0b3vx/ygNWzymHcu2cF6CYANdYNSn2yBPsGIvLk9hDLUSMbR9t" + "9efNinaiZzZdZ2pdje2/iiPzZhKlmZAoqJXr16E33HoAm8ZpX6zB5QDjJcl2eBl1" + "Cui5DoH6QWvnNzp/e2qvOBejTAZBWtQgGMgFjk8s8+S/32P0eZHUvT8bZkRfB46i" + "2/+sLWKl6gPZFaCqVWZHtr9fpHDsCmYvaQfAG/BTy4r3eU3xlANQ6sXb4u07eqhV" + "HsUP3/h1jOZY0Ynqrm0rZPYXeUsZHD/0a7ceAjQCH0ezH6Qwdwlflq2Fujprc0p8" + "jzbmIBJ//////////wIBAg==" + "-----END X942 DH PARAMETERS-----"); + + config.set("dl", "modp/ietf/8192", + "-----BEGIN X942 DH PARAMETERS-----" + "MIIIDAKCBAEA///////////JD9qiIWjCNMTGYouA3BzRKQJOCIpnzHQCC76mOxOb" + "IlFKCHmONATd75UZs806QxswKwpt8l8UN0/hNW1tUcJF5IW1dmJefsb0TELppjft" + "awv/XLb0Brft7jhr+1qJn6WunyQRfEsf5kkoZlHs5Fs9wgB8uKFjvwWY2kg2HFXT" + "mmkWP6j9JM9fg2VdI9yjrZYcYvNWIIVSu57VKQdwlpZtZww1Tkq8mATxdGwIyhgh" + "fDKQXkYuNs474553LBgOhgObJ4Oi7Aeij7XFXfBvTFLJ3ivL9pVYFxg5lUl86pVq" + "5RXSJhiY+gUQFXKOWoqqxC2tMxcNBFB6M6hVIavfHLpk7PuFBFjb7wqK6nFXXQYM" + "fbOXD4Wm4eTHq/WujNsJM9cejJTgSiVhnc7j0iYa0u5r8S/6BtmKCGTYdgJzPshq" + "ZFIfKxgXeyAMu+EXV3phXWx3CYjAutlG4gjiT6B05asxQ9tb/OD9EI5LgtEgqSEI" + "ARpyPBKnh+bXiHGaEL26WyaZwycYavTiPBqUaDS2FQvaJYPpyirUTOjbu8LbBN6O" + "+S6O/BQfvsqmKHxZR05rwF2ZspZPoJDDoiM7oYZRW+ftH2EpcM7i16+4G912IXBI" + "HNAGkSfVsFqpk7TqmI2P3cGG/7fckKbAj030Nck0AoSSNsP6tNJ8cCbB1NyyYCZG" + "3sl1HnY9uje9+P+UBq2eUw7l2zgvQTABrrBqU+2QJ9gxF5cnsIZaiRjaPtvrz5sU" + "7UTObLrO1Lsb238UR+bMJUszIFFRK9evQm+49AE3jNK/WYPKAcZLkuzwMuoV0XId" + "A/SC185udP721V5wL0aYDIK1qEAxkAscnlnnyX++x+jzI6l6fjbMiL4PHUW3/1ha" + "xUvUB7IrQVSqzI9tfr9I4dgUzF7SD4A34KeXFe7ym+MoBqHVi7fF2nb1UKo9ih+/" + "8OsZzLGjE9Vc2lbJ7C7yljI4f+jXbjwEaAQ+j2Y/SGDuEr8tWwt0dNbmlPkebb4R" + "WXSjkm8S/uXkOHd8tqky34zYvsTQc7kxujvIMraNndMAdB+nv4r8R+0ldvaTa6Qk" + "ZjqrY5xa5PVoNCO0dCvxyXgjjxbL451lLeP9uL78hIrZIiIuBKQDfAcT61eoGiPw" + "xzRz/GRs6jBrS8vIhi+Dhd36nUt/osCH6HloMwPtW906Bis89bOieKZtKhP4P0T4" + "Ld8xDuB0q2o2RZfomaAlXcFk8xzFCEaFHfmrSBld7X6hsdUQvX7nTXP682vDHs+i" + "aDWQRvTrh5+SQAlDi0gcbNeImgAu1e44K8kZDab8Am5HlVjkR1Z36aqeMFDidlaU" + "38gfVuiAuW5xYMmA3Zjt09///////////wKCBAB//////////+SH7VEQtGEaYmMx" + "RcBuDmiUgScERTPmOgEF31Mdic2RKKUEPMcaAm73yozZ5p0hjZgVhTb5L4obp/Ca" + "trao4SLyQtq7MS8/Y3omIXTTG/a1hf+uW3oDW/b3HDX9rUTP0tdPkgi+JY/zJJQz" + "KPZyLZ7hAD5cULHfgsxtJBsOKunNNIsf1H6SZ6/Bsq6R7lHWyw4xeasQQqldz2qU" + "g7hLSzazhhqnJV5MAni6NgRlDBC+GUgvIxcbZx3xzzuWDAdDAc2TwdF2A9FH2uKu" + "+DemKWTvFeX7SqwLjBzKpL51SrVyiukTDEx9AogKuUctRVViFtaZi4aCKD0Z1CqQ" + "1e+OXTJ2fcKCLG33hUV1OKuugwY+2cuHwtNw8mPV+tdGbYSZ649GSnAlErDO53Hp" + "Ew1pdzX4l/0DbMUEMmw7ATmfZDUyKQ+VjAu9kAZd8IurvTCutjuExGBdbKNxBHEn" + "0Dpy1Zih7a3+cH6IRyXBaJBUkIQAjTkeCVPD82vEOM0IXt0tk0zhk4w1enEeDUo0" + "GlsKhe0SwfTlFWomdG3d4W2Cb0d8l0d+Cg/fZVMUPiyjpzXgLszZSyfQSGHREZ3Q" + "wyit8/aPsJS4Z3Fr19wN7rsQuCQOaANIk+rYLVTJ2nVMRsfu4MN/2+5IU2BHpvoa" + "5JoBQkkbYf1aaT44E2DqblkwEyNvZLqPOx7dG978f8oDVs8ph3LtnBegmADXWDUp" + "9sgT7BiLy5PYQy1EjG0fbfXnzYp2omc2XWdqXY3tv4oj82YSpZmQKKiV69ehN9x6" + "AJvGaV+sweUA4yXJdngZdQrouQ6B+kFr5zc6f3tqrzgXo0wGQVrUIBjIBY5PLPPk" + "v99j9HmR1L0/G2ZEXweOotv/rC1ipeoD2RWgqlVmR7a/X6Rw7ApmL2kHwBvwU8uK" + "93lN8ZQDUOrF2+LtO3qoVR7FD9/4dYzmWNGJ6q5tK2T2F3lLGRw/9Gu3HgI0Ah9H" + "sx+kMHcJX5athbo6a3NKfI823wisulHJN4l/cvIcO75bVJlvxmxfYmg53JjdHeQZ" + "W0bO6YA6D9PfxX4j9pK7e0m10hIzHVWxzi1yerQaEdo6FfjkvBHHi2XxzrKW8f7c" + "X35CRWyRERcCUgG+A4n1q9QNEfhjmjn+MjZ1GDWl5eRDF8HC7v1Opb/RYEP0PLQZ" + "gfat7p0DFZ562dE8UzaVCfwfonwW75iHcDpVtRsiy/RM0BKu4LJ5jmKEI0KO/NWk" + "DK72v1DY6ohev3Omuf15teGPZ9E0GsgjenXDz8kgBKHFpA42a8RNABdq9xwV5IyG" + "034BNyPKrHIjqzv01U8YKHE7K0pv5A+rdEBctziwZMBuzHbp7///////////AgEC" + "-----END X942 DH PARAMETERS-----"); + + config.set("dl", "dsa/jce/512", + "-----BEGIN DSA PARAMETERS-----" + "MIGdAkEA/KaCzo4Syrom78z3EQ5SbbB4sF7ey80etKII864WF64B81uRpH5t9jQT" + "xeEu0ImbzRMqzVDZkVG9xD7nN1kuFwIVAJYu3cw2nLqOuyYO5rahJtk0bjjFAkEA" + "3gtU76vylwh+5iPVylWIxkgo70/eT/uuHs0gBndrBbEbgeo83pvDlkwWh8UyW/Q9" + "fM76DQqGvl3/3dDRFD3NdQ==" + "-----END DSA PARAMETERS-----"); + + config.set("dl", "dsa/jce/768", + "-----BEGIN DSA PARAMETERS-----" + "MIHdAmEA6eZCWZ01XzfJf/01ZxILjiXJzUPpJ7OpZw++xdiQFBki0sOzrSSACTeZ" + "hp0ehGqrSfqwrSbSzmoiIZ1HC859d31KIfvpwnC1f2BwAvPO+Dk2lM9F7jaIwRqM" + "VqsSej2vAhUAnNvYTJ8awvOND4D0KrlS5zOL9RECYQDe7p717RUWzn5pXmcrjO5F" + "5s17NuDmOF+JS6hhY/bz5sbU6KgRRtQBfe/dccvZD6Akdlm4i3zByJT0gmn9Txqs" + "CjBTjf9rP8ds+xMcnnlltYhYqwpDtVczWRKoqlR/lWg=" + "-----END DSA PARAMETERS-----"); + + config.set("dl", "dsa/jce/1024", + "-----BEGIN DSA PARAMETERS-----" + "MIIBHgKBgQD9f1OBHXUSKVLfSpwu7OTn9hG3UjzvRADDHj+AtlEmaUVdQCJR+1k9" + "jVj6v8X1ujD2y5tVbNeBO4AdNG/yZmC3a5lQpaSfn+gEexAiwk+7qdf+t8Yb+DtX" + "58aophUPBPuD9tPFHsMCNVQTWhaRMvZ1864rYdcq7/IiAxmd0UgBxwIVAJdgUI8V" + "IwvMspK5gqLrhAvwWBz1AoGARpYDUS4wJ4zTlHWV2yLuyYJqYyKtyXNE9B10DDJX" + "JMj577qn1NgD/4xgnc0QDrxb38+tfGpCX66nhuogUOvpg1HqH9of3yTWlHqmuaoj" + "dmlTgC9NfUqOy6BtGXaKJJH/sW0O+cQ6mbX3FnL/bwoktETQc20E04oaEyLa9s3Y" + "jJ0=" + "-----END DSA PARAMETERS-----"); + + config.set("dl", "dsa/botan/2048", + "-----BEGIN DSA PARAMETERS-----" + "MIICLAKCAQEAkcSKT9+898Aq6V59oSYSK13Shk9Vm4fo50oobVL1m9HeaN/WRdDg" + "DGDAgAMYkZgDdO61lKUyv9Z7mgnqxLhmOgeRDmjzlGX7cEDSXfE5MuusQ0elMOy6" + "YchU+biA08DDZgCAWHxFVm2t4mvVo5S+CTtMDyS1r/747GxbPlf7iQJam8FnaZMh" + "MeFtPJTvyrGNDfBhIDzFPmEDvHLVWUv9QMplOA9EqahR3LB1SV/AM6ilgHGhvXj+" + "BS9mVVZI60txnSr+i0iA+NrW8VgYuhePiSdMhwvpuW6wjEbEAEDMLv4d+xsYaN0x" + "nePDSjKmOrbrEiQgmkGWgMx5AtFyjU354QIhAIzX1FD4bwrZTu5M5GmodW0evRBY" + "JBlD6v+ws1RYXpJNAoIBAA2fXgdhtNvRgz1qsalhoJlsXyIwP3LYTBQPZ8Qx2Uq1" + "cVvqgaDJjTnOS8941rnryJXTT+idlAkdWEhhXvFfXobxHZb2yWniA936WDVkIKSc" + "tES1lbkBqTPP4HZ7WU8YoHt/kd7NukRriJkPePL/kfL+fNQ/0uRtGOraH3u2YCxh" + "f27zpLKE8v2boQo2BC3o+oeiyjZZf+yBFXoUheRAQd8CgwERy4gLvm7UlIFIhvll" + "zcMTX1zPE4Nyi/ZbgG+WksCxDWxMCcdabKO0ATyxarLBBfa+I66pAA6rIXiYX5cs" + "mAV+HIbkTnIYaI6krg82NtzKdFydzU5q/7Z8y8E9YTE=" + "-----END DSA PARAMETERS-----"); + + config.set("dl", "dsa/botan/3072", + "-----BEGIN DSA PARAMETERS-----" + "MIIDLAKCAYEA5LUIgHWWY1heFCRgyi2d/xMviuTIQN2jomZoiRJP5WOLhOiim3rz" + "+hIJvmv8S1By7Tsrc4e68/hX9HioAijvNgC3az3Pth0g00RlslBtLK+H3259wM6R" + "vS0Wekb2rcwxxTHk+cervbkq3fNbCoBsZikqX14X6WTdCZkDczrEKKs12A6m9oW/" + "uovkBo5UGK5eytno/wc94rY+Tn6tNciptwtb1Hz7iNNztm83kxk5sKtxvVWVgJCG" + "2gFVM30YWg5Ps2pRmxtiArhZHmACRJzxzTpmOE9tIHOxzXO+ypO68eGmEX0COPIi" + "rh7X/tGFqJDn9n+rj+uXU8wTSlGD3+h64llfe1wtn7tCJJ/dWVE+HTOWs+sv2GaE" + "8oWoRI/nV6ApiBxAdguU75Gb35dAw4OJWZ7FGm6btRmo4GhJHpzgovz+PLYNZs8N" + "+tIKjsaEBIaEphREV1vRck1zUrRKdgB3s71r04XOWwpyUMwL92jagpI4Buuc+7E4" + "hDcxthggjHWbAiEAs+vTZOxp74zzuvZDt1c0sWM5suSeXN4bWcHp+0DuDFsCggGA" + "K+0h7vg5ZKIwrom7px2ffDnFL8gim047x+WUTTKdoQ8BDqyee69sAJ/E6ylgcj4r" + "Vt9GY+TDrIAOkljeL3ZJ0gZ4KJP4Ze/KSY0u7zAHTqXop6smJxKk2UovOwuaku5A" + "D7OKPMWaXcfkNtXABLIuNQKDgbUck0B+sy1K4P1Cy0XhLQ7O6KJiOO3iCCp7FSIR" + "PGbO+NdFxs88uUX4TS9N4W1Epx3hmCcOE/A1U8iLjTI60LlIob8hA6lJl5tu0W+1" + "88lT2Vt8jojKZ9z1pjb7nKOdkkIV96iE7Wx+48ltjZcVQnl0t8Q1EoLhPTdz99KL" + "RS8QiSoTx1hzKN6kgntrNpsqjcFyrcWD9R8qZZjFSD5bxGewL5HQWcQC0Y4sJoD3" + "dqoG9JKAoscsF8xC1bbnQMXEsas8UcLtCSviotiwU65Xc9FCXtKwjwbi3VBZLfGk" + "eMFVkc39EVZP+I/zi3IdQjkv2kcyEtz9jS2IqXagCv/m//tDCjWeZMorNRyiQSOU" + "-----END DSA PARAMETERS-----"); + } +} + +/* +* Set the default policy +*/ +void Library_State::load_default_config() + { + set_default_config(*this); + set_default_aliases(*this); + set_default_oids(*this); + set_default_dl_groups(*this); + } + +} diff --git a/botan/src/libstate/scan_name.cpp b/botan/src/libstate/scan_name.cpp new file mode 100644 index 0000000..ef77187 --- /dev/null +++ b/botan/src/libstate/scan_name.cpp @@ -0,0 +1,74 @@ +/** +SCAN Name Abstraction +(C) 2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/scan_name.h> +#include <botan/parsing.h> +#include <botan/libstate.h> +#include <stdexcept> + +namespace Botan { + +namespace { + +std::vector<std::string> +parse_and_deref_aliases(const std::string& algo_spec) + { + std::vector<std::string> parts = parse_algorithm_name(algo_spec); + std::vector<std::string> out; + + for(size_t i = 0; i != parts.size(); ++i) + { + std::string part_i = global_state().deref_alias(parts[i]); + + if(i == 0 && part_i.find_first_of(",()") != std::string::npos) + { + std::vector<std::string> parts_i = parse_and_deref_aliases(part_i); + + for(size_t j = 0; j != parts_i.size(); ++j) + out.push_back(parts_i[j]); + } + else + out.push_back(part_i); + } + + return out; + } + +} + +SCAN_Name::SCAN_Name(const std::string& algo_spec) + { + orig_algo_spec = algo_spec; + + name = parse_and_deref_aliases(algo_spec); + + if(name.size() == 0) + throw Decoding_Error("Bad SCAN name " + algo_spec); + } + +std::string SCAN_Name::arg(u32bit i) const + { + if(i >= arg_count()) + throw std::range_error("SCAN_Name::argument"); + return name[i+1]; + } + +std::string SCAN_Name::arg(u32bit i, const std::string& def_value) const + { + if(i >= arg_count()) + return def_value; + return name[i+1]; + } + +u32bit SCAN_Name::arg_as_u32bit(u32bit i, u32bit def_value) const + { + if(i >= arg_count()) + return def_value; + return to_u32bit(name[i+1]); + } + +} diff --git a/botan/src/libstate/scan_name.h b/botan/src/libstate/scan_name.h new file mode 100644 index 0000000..9e7af40 --- /dev/null +++ b/botan/src/libstate/scan_name.h @@ -0,0 +1,77 @@ +/** +SCAN Name Abstraction +(C) 2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_SCAN_NAME_H__ +#define BOTAN_SCAN_NAME_H__ + +#include <botan/types.h> +#include <string> +#include <vector> +#include <set> + +namespace Botan { + +/** +A class encapsulating a SCAN name (similar to JCE conventions) +http://www.users.zetnet.co.uk/hopwood/crypto/scan/ +*/ +class SCAN_Name + { + public: + /** + @param algo_spec A SCAN name + */ + SCAN_Name(const std::string& algo_spec); + + /** + @return the original input string + */ + std::string as_string() const { return orig_algo_spec; } + + /** + @return the algorithm name + */ + std::string algo_name() const { return name[0]; } + + /** + @return the number of arguments + */ + u32bit arg_count() const { return name.size() - 1; } + + /** + @return if the number of arguments is between lower and upper + */ + bool arg_count_between(u32bit lower, u32bit upper) const + { return ((arg_count() >= lower) && (arg_count() <= upper)); } + + /** + @param i which argument + @return the ith argument + */ + std::string arg(u32bit i) const; + + /** + @param i which argument + @param def_value the default value + @return the ith argument or the default value + */ + std::string arg(u32bit i, const std::string& def_value) const; + + /** + @param i which argument + @param def_value the default value + @return the ith argument as a u32bit, or the default value + */ + u32bit arg_as_u32bit(u32bit i, u32bit def_value) const; + private: + std::string orig_algo_spec; + std::vector<std::string> name; + }; + +} + +#endif diff --git a/botan/src/mac/cbc_mac/cbc_mac.cpp b/botan/src/mac/cbc_mac/cbc_mac.cpp new file mode 100644 index 0000000..f5d9e15 --- /dev/null +++ b/botan/src/mac/cbc_mac/cbc_mac.cpp @@ -0,0 +1,109 @@ +/* +* CBC-MAC +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/cbc_mac.h> +#include <botan/xor_buf.h> +#include <algorithm> + +namespace Botan { + +/* +* Update an CBC-MAC Calculation +*/ +void CBC_MAC::add_data(const byte input[], u32bit length) + { + u32bit xored = std::min(OUTPUT_LENGTH - position, length); + xor_buf(state + position, input, xored); + position += xored; + + if(position < OUTPUT_LENGTH) + return; + + e->encrypt(state); + input += xored; + length -= xored; + while(length >= OUTPUT_LENGTH) + { + xor_buf(state, input, OUTPUT_LENGTH); + e->encrypt(state); + input += OUTPUT_LENGTH; + length -= OUTPUT_LENGTH; + } + + xor_buf(state, input, length); + position = length; + } + +/* +* Finalize an CBC-MAC Calculation +*/ +void CBC_MAC::final_result(byte mac[]) + { + if(position) + e->encrypt(state); + + copy_mem(mac, state.begin(), state.size()); + state.clear(); + position = 0; + } + +/* +* CBC-MAC Key Schedule +*/ +void CBC_MAC::key_schedule(const byte key[], u32bit length) + { + e->set_key(key, length); + } + +/* +* Clear memory of sensitive data +*/ +void CBC_MAC::clear() throw() + { + e->clear(); + state.clear(); + position = 0; + } + +/* +* Return the name of this type +*/ +std::string CBC_MAC::name() const + { + return "CBC-MAC(" + e->name() + ")"; + } + +/* +* Return a clone of this object +*/ +MessageAuthenticationCode* CBC_MAC::clone() const + { + return new CBC_MAC(e->clone()); + } + +/* +* CBC-MAC Constructor +*/ +CBC_MAC::CBC_MAC(BlockCipher* e_in) : + MessageAuthenticationCode(e_in->BLOCK_SIZE, + e_in->MINIMUM_KEYLENGTH, + e_in->MAXIMUM_KEYLENGTH, + e_in->KEYLENGTH_MULTIPLE), + e(e_in), state(e->BLOCK_SIZE) + { + position = 0; + } + +/* +* CBC-MAC Destructor +*/ +CBC_MAC::~CBC_MAC() + { + delete e; + } + +} diff --git a/botan/src/mac/cbc_mac/cbc_mac.h b/botan/src/mac/cbc_mac/cbc_mac.h new file mode 100644 index 0000000..d17d792 --- /dev/null +++ b/botan/src/mac/cbc_mac/cbc_mac.h @@ -0,0 +1,40 @@ +/* +* CBC-MAC +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_CBC_MAC_H__ +#define BOTAN_CBC_MAC_H__ + +#include <botan/mac.h> +#include <botan/block_cipher.h> + +namespace Botan { + +/* +* CBC-MAC +*/ +class BOTAN_DLL CBC_MAC : public MessageAuthenticationCode + { + public: + void clear() throw(); + std::string name() const; + MessageAuthenticationCode* clone() const; + + CBC_MAC(BlockCipher* e); + ~CBC_MAC(); + private: + void add_data(const byte[], u32bit); + void final_result(byte[]); + void key_schedule(const byte[], u32bit); + + BlockCipher* e; + SecureVector<byte> state; + u32bit position; + }; + +} + +#endif diff --git a/botan/src/mac/cbc_mac/info.txt b/botan/src/mac/cbc_mac/info.txt new file mode 100644 index 0000000..3a54349 --- /dev/null +++ b/botan/src/mac/cbc_mac/info.txt @@ -0,0 +1,14 @@ +realname "CBC-MAC" + +define CBC_MAC + +load_on auto + +<add> +cbc_mac.cpp +cbc_mac.h +</add> + +<requires> +block +</requires> diff --git a/botan/src/mac/cmac/cmac.cpp b/botan/src/mac/cmac/cmac.cpp new file mode 100644 index 0000000..84aa61e --- /dev/null +++ b/botan/src/mac/cmac/cmac.cpp @@ -0,0 +1,162 @@ +/* +* CMAC +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/cmac.h> +#include <botan/xor_buf.h> + +namespace Botan { + +/* +* Perform CMAC's multiplication in GF(2^n) +*/ +SecureVector<byte> CMAC::poly_double(const MemoryRegion<byte>& in, + byte polynomial) + { + const bool do_xor = (in[0] & 0x80) ? true : false; + + SecureVector<byte> out = in; + + byte carry = 0; + for(u32bit j = out.size(); j != 0; --j) + { + byte temp = out[j-1]; + out[j-1] = (temp << 1) | carry; + carry = (temp >> 7); + } + + if(do_xor) + out[out.size()-1] ^= polynomial; + + return out; + } + +/* +* Update an CMAC Calculation +*/ +void CMAC::add_data(const byte input[], u32bit length) + { + buffer.copy(position, input, length); + if(position + length > OUTPUT_LENGTH) + { + xor_buf(state, buffer, OUTPUT_LENGTH); + e->encrypt(state); + input += (OUTPUT_LENGTH - position); + length -= (OUTPUT_LENGTH - position); + while(length > OUTPUT_LENGTH) + { + xor_buf(state, input, OUTPUT_LENGTH); + e->encrypt(state); + input += OUTPUT_LENGTH; + length -= OUTPUT_LENGTH; + } + buffer.copy(input, length); + position = 0; + } + position += length; + } + +/* +* Finalize an CMAC Calculation +*/ +void CMAC::final_result(byte mac[]) + { + xor_buf(state, buffer, position); + + if(position == OUTPUT_LENGTH) + { + xor_buf(state, B, OUTPUT_LENGTH); + } + else + { + state[position] ^= 0x80; + xor_buf(state, P, OUTPUT_LENGTH); + } + + e->encrypt(state); + + for(u32bit j = 0; j != OUTPUT_LENGTH; ++j) + mac[j] = state[j]; + + state.clear(); + buffer.clear(); + position = 0; + } + +/* +* CMAC Key Schedule +*/ +void CMAC::key_schedule(const byte key[], u32bit length) + { + clear(); + e->set_key(key, length); + e->encrypt(B); + B = poly_double(B, polynomial); + P = poly_double(B, polynomial); + } + +/* +* Clear memory of sensitive data +*/ +void CMAC::clear() throw() + { + e->clear(); + state.clear(); + buffer.clear(); + B.clear(); + P.clear(); + position = 0; + } + +/* +* Return the name of this type +*/ +std::string CMAC::name() const + { + return "CMAC(" + e->name() + ")"; + } + +/* +* Return a clone of this object +*/ +MessageAuthenticationCode* CMAC::clone() const + { + return new CMAC(e->clone()); + } + +/* +* CMAC Constructor +*/ +CMAC::CMAC(BlockCipher* e_in) : + MessageAuthenticationCode(e_in->BLOCK_SIZE, + e_in->MINIMUM_KEYLENGTH, + e_in->MAXIMUM_KEYLENGTH, + e_in->KEYLENGTH_MULTIPLE), + e(e_in) + { + if(e->BLOCK_SIZE == 16) + polynomial = 0x87; + else if(e->BLOCK_SIZE == 8) + polynomial = 0x1B; + else + throw Invalid_Argument("CMAC cannot use the cipher " + e->name()); + + state.create(OUTPUT_LENGTH); + buffer.create(OUTPUT_LENGTH); + B.create(OUTPUT_LENGTH); + P.create(OUTPUT_LENGTH); + position = 0; + } + +/* +* CMAC Destructor +*/ +CMAC::~CMAC() + { + delete e; + } + +} diff --git a/botan/src/mac/cmac/cmac.h b/botan/src/mac/cmac/cmac.h new file mode 100644 index 0000000..5a6deb7 --- /dev/null +++ b/botan/src/mac/cmac/cmac.h @@ -0,0 +1,44 @@ +/* +* CMAC +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_CMAC_H__ +#define BOTAN_CMAC_H__ + +#include <botan/mac.h> +#include <botan/block_cipher.h> + +namespace Botan { + +/* +* CMAC +*/ +class BOTAN_DLL CMAC : public MessageAuthenticationCode + { + public: + void clear() throw(); + std::string name() const; + MessageAuthenticationCode* clone() const; + + static SecureVector<byte> poly_double(const MemoryRegion<byte>& in, + byte polynomial); + + CMAC(BlockCipher* e); + ~CMAC(); + private: + void add_data(const byte[], u32bit); + void final_result(byte[]); + void key_schedule(const byte[], u32bit); + + BlockCipher* e; + SecureVector<byte> buffer, state, B, P; + u32bit position; + byte polynomial; + }; + +} + +#endif diff --git a/botan/src/mac/cmac/info.txt b/botan/src/mac/cmac/info.txt new file mode 100644 index 0000000..b593c9d --- /dev/null +++ b/botan/src/mac/cmac/info.txt @@ -0,0 +1,14 @@ +realname "CMAC" + +define CMAC + +load_on auto + +<add> +cmac.cpp +cmac.h +</add> + +<requires> +block +</requires> diff --git a/botan/src/mac/hmac/hmac.cpp b/botan/src/mac/hmac/hmac.cpp new file mode 100644 index 0000000..717e264 --- /dev/null +++ b/botan/src/mac/hmac/hmac.cpp @@ -0,0 +1,99 @@ +/* +* HMAC +* (C) 1999-2007 Jack Lloyd +* 2007 Yves Jerschow +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/hmac.h> +#include <botan/xor_buf.h> + +namespace Botan { + +/* +* Update a HMAC Calculation +*/ +void HMAC::add_data(const byte input[], u32bit length) + { + hash->update(input, length); + } + +/* +* Finalize a HMAC Calculation +*/ +void HMAC::final_result(byte mac[]) + { + hash->final(mac); + hash->update(o_key); + hash->update(mac, OUTPUT_LENGTH); + hash->final(mac); + hash->update(i_key); + } + +/* +* HMAC Key Schedule +*/ +void HMAC::key_schedule(const byte key[], u32bit length) + { + hash->clear(); + std::fill(i_key.begin(), i_key.end(), 0x36); + std::fill(o_key.begin(), o_key.end(), 0x5C); + + if(length > hash->HASH_BLOCK_SIZE) + { + SecureVector<byte> hmac_key = hash->process(key, length); + xor_buf(i_key, hmac_key, hmac_key.size()); + xor_buf(o_key, hmac_key, hmac_key.size()); + } + else + { + xor_buf(i_key, key, length); + xor_buf(o_key, key, length); + } + + hash->update(i_key); + } + +/* +* Clear memory of sensitive data +*/ +void HMAC::clear() throw() + { + hash->clear(); + i_key.clear(); + o_key.clear(); + } + +/* +* Return the name of this type +*/ +std::string HMAC::name() const + { + return "HMAC(" + hash->name() + ")"; + } + +/* +* Return a clone of this object +*/ +MessageAuthenticationCode* HMAC::clone() const + { + return new HMAC(hash->clone()); + } + +/* +* HMAC Constructor +*/ +HMAC::HMAC(HashFunction* hash_in) : + MessageAuthenticationCode(hash_in->OUTPUT_LENGTH, + 1, 2*hash_in->HASH_BLOCK_SIZE), + hash(hash_in) + { + if(hash->HASH_BLOCK_SIZE == 0) + throw Invalid_Argument("HMAC cannot be used with " + hash->name()); + + i_key.create(hash->HASH_BLOCK_SIZE); + o_key.create(hash->HASH_BLOCK_SIZE); + } + +} diff --git a/botan/src/mac/hmac/hmac.h b/botan/src/mac/hmac/hmac.h new file mode 100644 index 0000000..932af71 --- /dev/null +++ b/botan/src/mac/hmac/hmac.h @@ -0,0 +1,38 @@ +/* +* HMAC +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_HMAC_H__ +#define BOTAN_HMAC_H__ + +#include <botan/mac.h> +#include <botan/hash.h> + +namespace Botan { + +/* +* HMAC +*/ +class BOTAN_DLL HMAC : public MessageAuthenticationCode + { + public: + void clear() throw(); + std::string name() const; + MessageAuthenticationCode* clone() const; + + HMAC(HashFunction* hash); + ~HMAC() { delete hash; } + private: + void add_data(const byte[], u32bit); + void final_result(byte[]); + void key_schedule(const byte[], u32bit); + HashFunction* hash; + SecureVector<byte> i_key, o_key; + }; + +} + +#endif diff --git a/botan/src/mac/hmac/info.txt b/botan/src/mac/hmac/info.txt new file mode 100644 index 0000000..cdf2e67 --- /dev/null +++ b/botan/src/mac/hmac/info.txt @@ -0,0 +1,14 @@ +realname "HMAC" + +define HMAC + +load_on auto + +<add> +hmac.cpp +hmac.h +</add> + +<requires> +hash +</requires> diff --git a/botan/src/mac/info.txt b/botan/src/mac/info.txt new file mode 100644 index 0000000..239eb63 --- /dev/null +++ b/botan/src/mac/info.txt @@ -0,0 +1,13 @@ +realname "Message Authentication Codes" + +load_on auto + +<add> +mac.h +mac.cpp +</add> + +<requires> +buf_comp +sym_algo +</requires> diff --git a/botan/src/mac/mac.cpp b/botan/src/mac/mac.cpp new file mode 100644 index 0000000..96df255 --- /dev/null +++ b/botan/src/mac/mac.cpp @@ -0,0 +1,26 @@ +/** +Message Authentication Code base class +(C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/mac.h> + +namespace Botan { + +/** +* Default (deterministic) MAC verification operation +*/ +bool MessageAuthenticationCode::verify_mac(const byte mac[], u32bit length) + { + SecureVector<byte> our_mac = final(); + if(our_mac.size() != length) + return false; + for(u32bit j = 0; j != length; ++j) + if(mac[j] != our_mac[j]) + return false; + return true; + } + +} diff --git a/botan/src/mac/mac.h b/botan/src/mac/mac.h new file mode 100644 index 0000000..3ec5fff --- /dev/null +++ b/botan/src/mac/mac.h @@ -0,0 +1,60 @@ +/** +* Base class for message authentiction codes +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_MESSAGE_AUTH_CODE_BASE_H__ +#define BOTAN_MESSAGE_AUTH_CODE_BASE_H__ + +#include <botan/buf_comp.h> +#include <botan/sym_algo.h> +#include <string> + +namespace Botan { + +/** +* This class represents Message Authentication Code (MAC) objects. +*/ +class BOTAN_DLL MessageAuthenticationCode : public BufferedComputation, + public SymmetricAlgorithm + { + public: + /** + * Verify a MAC. + * @param in the MAC to verify as a byte array + * @param length the length of the byte array + * @return true if the MAC is valid, false otherwise + */ + virtual bool verify_mac(const byte[], u32bit); + + /** + * Get a new object representing the same algorithm as *this + */ + virtual MessageAuthenticationCode* clone() const = 0; + + /** + * Get the name of this algorithm. + * @return the name of this algorithm + */ + virtual std::string name() const = 0; + + /** + * Reset the internal state of this object. + */ + virtual void clear() throw() = 0; + + MessageAuthenticationCode(u32bit mac_len, + u32bit key_min, + u32bit key_max = 0, + u32bit key_mod = 1) : + BufferedComputation(mac_len), + SymmetricAlgorithm(key_min, key_max, key_mod) {} + + virtual ~MessageAuthenticationCode() {} + }; + +} + +#endif diff --git a/botan/src/mac/ssl3mac/info.txt b/botan/src/mac/ssl3mac/info.txt new file mode 100644 index 0000000..f879116 --- /dev/null +++ b/botan/src/mac/ssl3mac/info.txt @@ -0,0 +1,14 @@ +realname "SSLv3 MAC" + +define SSL3_MAC + +load_on auto + +<add> +ssl3_mac.cpp +ssl3_mac.h +</add> + +<requires> +hash +</requires> diff --git a/botan/src/mac/ssl3mac/ssl3_mac.cpp b/botan/src/mac/ssl3mac/ssl3_mac.cpp new file mode 100644 index 0000000..c29296c --- /dev/null +++ b/botan/src/mac/ssl3mac/ssl3_mac.cpp @@ -0,0 +1,90 @@ +/* +* SSL3-MAC +* (C) 1999-2004 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/ssl3_mac.h> + +namespace Botan { + +/* +* Update a SSL3-MAC Calculation +*/ +void SSL3_MAC::add_data(const byte input[], u32bit length) + { + hash->update(input, length); + } + +/* +* Finalize a SSL3-MAC Calculation +*/ +void SSL3_MAC::final_result(byte mac[]) + { + hash->final(mac); + hash->update(o_key); + hash->update(mac, OUTPUT_LENGTH); + hash->final(mac); + hash->update(i_key); + } + +/* +* SSL3-MAC Key Schedule +*/ +void SSL3_MAC::key_schedule(const byte key[], u32bit length) + { + hash->clear(); + std::fill(i_key.begin(), i_key.end(), 0x36); + std::fill(o_key.begin(), o_key.end(), 0x5C); + + i_key.copy(key, length); + o_key.copy(key, length); + hash->update(i_key); + } + +/* +* Clear memory of sensitive data +*/ +void SSL3_MAC::clear() throw() + { + hash->clear(); + i_key.clear(); + o_key.clear(); + } + +/* +* Return the name of this type +*/ +std::string SSL3_MAC::name() const + { + return "SSL3-MAC(" + hash->name() + ")"; + } + +/* +* Return a clone of this object +*/ +MessageAuthenticationCode* SSL3_MAC::clone() const + { + return new SSL3_MAC(hash->clone()); + } + +/* +* SSL3-MAC Constructor +*/ +SSL3_MAC::SSL3_MAC(HashFunction* hash_in) : + MessageAuthenticationCode(hash_in->OUTPUT_LENGTH, + hash_in->OUTPUT_LENGTH), + hash(hash_in) + { + if(hash->HASH_BLOCK_SIZE == 0) + throw Invalid_Argument("SSL3-MAC cannot be used with " + hash->name()); + + u32bit INNER_HASH_LENGTH = + (hash->name() == "SHA-160") ? 60 : hash->HASH_BLOCK_SIZE; + + i_key.create(INNER_HASH_LENGTH); + o_key.create(INNER_HASH_LENGTH); + } + +} diff --git a/botan/src/mac/ssl3mac/ssl3_mac.h b/botan/src/mac/ssl3mac/ssl3_mac.h new file mode 100644 index 0000000..dcaf7f4 --- /dev/null +++ b/botan/src/mac/ssl3mac/ssl3_mac.h @@ -0,0 +1,39 @@ +/* +* SSL3-MAC +* (C) 1999-2004 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_SSL3_MAC_H__ +#define BOTAN_SSL3_MAC_H__ + +#include <botan/hash.h> +#include <botan/mac.h> + +namespace Botan { + +/* +* SSL3-MAC +*/ +class BOTAN_DLL SSL3_MAC : public MessageAuthenticationCode + { + public: + void clear() throw(); + std::string name() const; + MessageAuthenticationCode* clone() const; + + SSL3_MAC(HashFunction*); + ~SSL3_MAC() { delete hash; } + private: + void add_data(const byte[], u32bit); + void final_result(byte[]); + void key_schedule(const byte[], u32bit); + + HashFunction* hash; + SecureVector<byte> i_key, o_key; + }; + +} + +#endif diff --git a/botan/src/mac/x919_mac/info.txt b/botan/src/mac/x919_mac/info.txt new file mode 100644 index 0000000..f2ebd5b --- /dev/null +++ b/botan/src/mac/x919_mac/info.txt @@ -0,0 +1,14 @@ +realname "ANSI X9.19 MAC" + +define ANSI_X919_MAC + +load_on auto + +<add> +x919_mac.cpp +x919_mac.h +</add> + +<requires> +block +</requires> diff --git a/botan/src/mac/x919_mac/x919_mac.cpp b/botan/src/mac/x919_mac/x919_mac.cpp new file mode 100644 index 0000000..ef89cac --- /dev/null +++ b/botan/src/mac/x919_mac/x919_mac.cpp @@ -0,0 +1,107 @@ +/* +* ANSI X9.19 MAC +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/x919_mac.h> +#include <botan/xor_buf.h> +#include <algorithm> + +namespace Botan { + +/* +* Update an ANSI X9.19 MAC Calculation +*/ +void ANSI_X919_MAC::add_data(const byte input[], u32bit length) + { + u32bit xored = std::min(8 - position, length); + xor_buf(state + position, input, xored); + position += xored; + + if(position < 8) return; + + e->encrypt(state); + input += xored; + length -= xored; + while(length >= 8) + { + xor_buf(state, input, 8); + e->encrypt(state); + input += 8; + length -= 8; + } + + xor_buf(state, input, length); + position = length; + } + +/* +* Finalize an ANSI X9.19 MAC Calculation +*/ +void ANSI_X919_MAC::final_result(byte mac[]) + { + if(position) + e->encrypt(state); + d->decrypt(state, mac); + e->encrypt(mac); + state.clear(); + position = 0; + } + +/* +* ANSI X9.19 MAC Key Schedule +*/ +void ANSI_X919_MAC::key_schedule(const byte key[], u32bit length) + { + e->set_key(key, 8); + if(length == 8) d->set_key(key, 8); + else d->set_key(key + 8, 8); + } + +/* +* Clear memory of sensitive data +*/ +void ANSI_X919_MAC::clear() throw() + { + e->clear(); + d->clear(); + state.clear(); + position = 0; + } + +std::string ANSI_X919_MAC::name() const + { + return "X9.19-MAC"; + } + +MessageAuthenticationCode* ANSI_X919_MAC::clone() const + { + return new ANSI_X919_MAC(e->clone()); + } + +/* +* ANSI X9.19 MAC Constructor +*/ +ANSI_X919_MAC::ANSI_X919_MAC(BlockCipher* e_in) : + MessageAuthenticationCode(e_in->BLOCK_SIZE, + e_in->MINIMUM_KEYLENGTH, + 2*e_in->MAXIMUM_KEYLENGTH, + 2*e_in->KEYLENGTH_MULTIPLE), + e(e_in), d(e->clone()), position(0) + { + if(e->name() != "DES") + throw Invalid_Argument("ANSI X9.19 MAC only supports DES"); + } + +/* +* ANSI X9.19 MAC Destructor +le*/ +ANSI_X919_MAC::~ANSI_X919_MAC() + { + delete e; + delete d; + } + +} diff --git a/botan/src/mac/x919_mac/x919_mac.h b/botan/src/mac/x919_mac/x919_mac.h new file mode 100644 index 0000000..1c2a06b --- /dev/null +++ b/botan/src/mac/x919_mac/x919_mac.h @@ -0,0 +1,41 @@ +/* +* ANSI X9.19 MAC +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ANSI_X919_MAC_H__ +#define BOTAN_ANSI_X919_MAC_H__ + +#include <botan/mac.h> +#include <botan/block_cipher.h> + +namespace Botan { + +/** +* DES/3DES-based MAC from ANSI X9.19 +*/ +class BOTAN_DLL ANSI_X919_MAC : public MessageAuthenticationCode + { + public: + void clear() throw(); + std::string name() const; + MessageAuthenticationCode* clone() const; + + ANSI_X919_MAC(BlockCipher*); + ~ANSI_X919_MAC(); + private: + void add_data(const byte[], u32bit); + void final_result(byte[]); + void key_schedule(const byte[], u32bit); + + BlockCipher* e; + BlockCipher* d; + SecureBuffer<byte, 8> state; + u32bit position; + }; + +} + +#endif diff --git a/botan/src/math/bigint/big_code.cpp b/botan/src/math/bigint/big_code.cpp new file mode 100644 index 0000000..74701e5 --- /dev/null +++ b/botan/src/math/bigint/big_code.cpp @@ -0,0 +1,154 @@ +/* +* BigInt Encoding/Decoding +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/bigint.h> +#include <botan/divide.h> +#include <botan/charset.h> +#include <botan/hex.h> + +namespace Botan { + +/* +* Encode a BigInt +*/ +void BigInt::encode(byte output[], const BigInt& n, Base base) + { + if(base == Binary) + n.binary_encode(output); + else if(base == Hexadecimal) + { + SecureVector<byte> binary(n.encoded_size(Binary)); + n.binary_encode(binary); + for(u32bit j = 0; j != binary.size(); ++j) + Hex_Encoder::encode(binary[j], output + 2*j); + } + else if(base == Octal) + { + BigInt copy = n; + const u32bit output_size = n.encoded_size(Octal); + for(u32bit j = 0; j != output_size; ++j) + { + output[output_size - 1 - j] = Charset::digit2char(copy % 8); + copy /= 8; + } + } + else if(base == Decimal) + { + BigInt copy = n; + BigInt remainder; + copy.set_sign(Positive); + const u32bit output_size = n.encoded_size(Decimal); + for(u32bit j = 0; j != output_size; ++j) + { + divide(copy, 10, copy, remainder); + output[output_size - 1 - j] = + Charset::digit2char(remainder.word_at(0)); + if(copy.is_zero()) + break; + } + } + else + throw Invalid_Argument("Unknown BigInt encoding method"); + } + +/* +* Encode a BigInt +*/ +SecureVector<byte> BigInt::encode(const BigInt& n, Base base) + { + SecureVector<byte> output(n.encoded_size(base)); + encode(output, n, base); + if(base != Binary) + for(u32bit j = 0; j != output.size(); ++j) + if(output[j] == 0) + output[j] = '0'; + return output; + } + +/* +* Encode a BigInt, with leading 0s if needed +*/ +SecureVector<byte> BigInt::encode_1363(const BigInt& n, u32bit bytes) + { + const u32bit n_bytes = n.bytes(); + if(n_bytes > bytes) + throw Encoding_Error("encode_1363: n is too large to encode properly"); + + const u32bit leading_0s = bytes - n_bytes; + + SecureVector<byte> output(bytes); + encode(output + leading_0s, n, Binary); + return output; + } + +/* +* Decode a BigInt +*/ +BigInt BigInt::decode(const MemoryRegion<byte>& buf, Base base) + { + return BigInt::decode(buf, buf.size(), base); + } + +/* +* Decode a BigInt +*/ +BigInt BigInt::decode(const byte buf[], u32bit length, Base base) + { + BigInt r; + if(base == Binary) + r.binary_decode(buf, length); + else if(base == Hexadecimal) + { + SecureVector<byte> hex; + for(u32bit j = 0; j != length; ++j) + if(Hex_Decoder::is_valid(buf[j])) + hex.append(buf[j]); + + u32bit offset = (hex.size() % 2); + SecureVector<byte> binary(hex.size() / 2 + offset); + + if(offset) + { + byte temp[2] = { '0', hex[0] }; + binary[0] = Hex_Decoder::decode(temp); + } + + for(u32bit j = offset; j != binary.size(); ++j) + binary[j] = Hex_Decoder::decode(hex+2*j-offset); + r.binary_decode(binary, binary.size()); + } + else if(base == Decimal || base == Octal) + { + const u32bit RADIX = ((base == Decimal) ? 10 : 8); + for(u32bit j = 0; j != length; ++j) + { + if(Charset::is_space(buf[j])) + continue; + + if(!Charset::is_digit(buf[j])) + throw Invalid_Argument("BigInt::decode: " + "Invalid character in decimal input"); + + byte x = Charset::char2digit(buf[j]); + if(x >= RADIX) + { + if(RADIX == 10) + throw Invalid_Argument("BigInt: Invalid decimal string"); + else + throw Invalid_Argument("BigInt: Invalid octal string"); + } + + r *= RADIX; + r += x; + } + } + else + throw Invalid_Argument("Unknown BigInt decoding method"); + return r; + } + +} diff --git a/botan/src/math/bigint/big_io.cpp b/botan/src/math/bigint/big_io.cpp new file mode 100644 index 0000000..b50fcce --- /dev/null +++ b/botan/src/math/bigint/big_io.cpp @@ -0,0 +1,55 @@ +/* +* BigInt Input/Output +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/bigint.h> +#include <iostream> + +namespace Botan { + +/* +* Write the BigInt into a stream +*/ +std::ostream& operator<<(std::ostream& stream, const BigInt& n) + { + BigInt::Base base = BigInt::Decimal; + if(stream.flags() & std::ios::hex) + base = BigInt::Hexadecimal; + else if(stream.flags() & std::ios::oct) + base = BigInt::Octal; + + if(n == 0) + stream.write("0", 1); + else + { + if(n < 0) + stream.write("-", 1); + SecureVector<byte> buffer = BigInt::encode(n, base); + u32bit skip = 0; + while(buffer[skip] == '0' && skip < buffer.size()) + ++skip; + stream.write(reinterpret_cast<const char*>(buffer.begin()) + skip, + buffer.size() - skip); + } + if(!stream.good()) + throw Stream_IO_Error("BigInt output operator has failed"); + return stream; + } + +/* +* Read the BigInt from a stream +*/ +std::istream& operator>>(std::istream& stream, BigInt& n) + { + std::string str; + std::getline(stream, str); + if(stream.bad() || (stream.fail() && !stream.eof())) + throw Stream_IO_Error("BigInt input operator has failed"); + n = BigInt(str); + return stream; + } + +} diff --git a/botan/src/math/bigint/big_ops2.cpp b/botan/src/math/bigint/big_ops2.cpp new file mode 100644 index 0000000..488eca9 --- /dev/null +++ b/botan/src/math/bigint/big_ops2.cpp @@ -0,0 +1,224 @@ +/* +* BigInt Assignment Operators +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/bigint.h> +#include <botan/mp_core.h> +#include <botan/bit_ops.h> +#include <algorithm> + +namespace Botan { + +/* +* Addition Operator +*/ +BigInt& BigInt::operator+=(const BigInt& y) + { + const u32bit x_sw = sig_words(), y_sw = y.sig_words(); + + const u32bit reg_size = std::max(x_sw, y_sw) + 1; + grow_to(reg_size); + + if(sign() == y.sign()) + bigint_add2(get_reg(), reg_size - 1, y.data(), y_sw); + else + { + s32bit relative_size = bigint_cmp(data(), x_sw, y.data(), y_sw); + + if(relative_size < 0) + { + SecureVector<word> z(reg_size - 1); + bigint_sub3(z, y.data(), reg_size - 1, data(), x_sw); + copy_mem(get_reg().begin(), z.begin(), z.size()); + set_sign(y.sign()); + } + else if(relative_size == 0) + { + get_reg().clear(); + set_sign(Positive); + } + else if(relative_size > 0) + bigint_sub2(get_reg(), x_sw, y.data(), y_sw); + } + + return (*this); + } + +/* +* Subtraction Operator +*/ +BigInt& BigInt::operator-=(const BigInt& y) + { + const u32bit x_sw = sig_words(), y_sw = y.sig_words(); + + s32bit relative_size = bigint_cmp(data(), x_sw, y.data(), y_sw); + + const u32bit reg_size = std::max(x_sw, y_sw) + 1; + grow_to(reg_size); + + if(relative_size < 0) + { + if(sign() == y.sign()) + { + SecureVector<word> z(reg_size - 1); + bigint_sub3(z, y.data(), reg_size - 1, data(), x_sw); + copy_mem(get_reg().begin(), z.begin(), z.size()); + } + else + bigint_add2(get_reg(), reg_size - 1, y.data(), y_sw); + + set_sign(y.reverse_sign()); + } + else if(relative_size == 0) + { + if(sign() == y.sign()) + { + get_reg().clear(); + set_sign(Positive); + } + else + bigint_shl1(get_reg(), x_sw, 0, 1); + } + else if(relative_size > 0) + { + if(sign() == y.sign()) + bigint_sub2(get_reg(), x_sw, y.data(), y_sw); + else + bigint_add2(get_reg(), reg_size - 1, y.data(), y_sw); + } + + return (*this); + } + +/* +* Multiplication Operator +*/ +BigInt& BigInt::operator*=(const BigInt& y) + { + const u32bit x_sw = sig_words(), y_sw = y.sig_words(); + set_sign((sign() == y.sign()) ? Positive : Negative); + + if(x_sw == 0 || y_sw == 0) + { + get_reg().clear(); + set_sign(Positive); + } + else if(x_sw == 1 && y_sw) + { + grow_to(y_sw + 2); + bigint_linmul3(get_reg(), y.data(), y_sw, word_at(0)); + } + else if(y_sw == 1 && x_sw) + { + grow_to(x_sw + 2); + bigint_linmul2(get_reg(), x_sw, y.word_at(0)); + } + else + { + grow_to(size() + y.size()); + + SecureVector<word> z(data(), x_sw); + SecureVector<word> workspace(size()); + + bigint_mul(get_reg(), size(), workspace, + z, z.size(), x_sw, + y.data(), y.size(), y_sw); + } + + return (*this); + } + +/* +* Division Operator +*/ +BigInt& BigInt::operator/=(const BigInt& y) + { + if(y.sig_words() == 1 && power_of_2(y.word_at(0))) + (*this) >>= (y.bits() - 1); + else + (*this) = (*this) / y; + return (*this); + } + +/* +* Modulo Operator +*/ +BigInt& BigInt::operator%=(const BigInt& mod) + { + return (*this = (*this) % mod); + } + +/* +* Modulo Operator +*/ +word BigInt::operator%=(word mod) + { + if(mod == 0) + throw BigInt::DivideByZero(); + if(power_of_2(mod)) + { + word result = (word_at(0) & (mod - 1)); + clear(); + grow_to(2); + get_reg()[0] = result; + return result; + } + + word remainder = 0; + + for(u32bit j = sig_words(); j > 0; --j) + remainder = bigint_modop(remainder, word_at(j-1), mod); + clear(); + grow_to(2); + + if(remainder && sign() == BigInt::Negative) + get_reg()[0] = mod - remainder; + else + get_reg()[0] = remainder; + + set_sign(BigInt::Positive); + + return word_at(0); + } + +/* +* Left Shift Operator +*/ +BigInt& BigInt::operator<<=(u32bit shift) + { + if(shift) + { + const u32bit shift_words = shift / MP_WORD_BITS, + shift_bits = shift % MP_WORD_BITS, + words = sig_words(); + + grow_to(words + shift_words + (shift_bits ? 1 : 0)); + bigint_shl1(get_reg(), words, shift_words, shift_bits); + } + + return (*this); + } + +/* +* Right Shift Operator +*/ +BigInt& BigInt::operator>>=(u32bit shift) + { + if(shift) + { + const u32bit shift_words = shift / MP_WORD_BITS, + shift_bits = shift % MP_WORD_BITS; + + bigint_shr1(get_reg(), sig_words(), shift_words, shift_bits); + + if(is_zero()) + set_sign(Positive); + } + + return (*this); + } + +} diff --git a/botan/src/math/bigint/big_ops3.cpp b/botan/src/math/bigint/big_ops3.cpp new file mode 100644 index 0000000..ad8b7bb --- /dev/null +++ b/botan/src/math/bigint/big_ops3.cpp @@ -0,0 +1,190 @@ +/* +* BigInt Binary Operators +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/bigint.h> +#include <botan/divide.h> +#include <botan/mp_core.h> +#include <botan/bit_ops.h> +#include <algorithm> + +namespace Botan { + +/* +* Addition Operator +*/ +BigInt operator+(const BigInt& x, const BigInt& y) + { + const u32bit x_sw = x.sig_words(), y_sw = y.sig_words(); + + BigInt z(x.sign(), std::max(x_sw, y_sw) + 1); + + if((x.sign() == y.sign())) + bigint_add3(z.get_reg(), x.data(), x_sw, y.data(), y_sw); + else + { + s32bit relative_size = bigint_cmp(x.data(), x_sw, y.data(), y_sw); + + if(relative_size < 0) + { + bigint_sub3(z.get_reg(), y.data(), y_sw, x.data(), x_sw); + z.set_sign(y.sign()); + } + else if(relative_size == 0) + z.set_sign(BigInt::Positive); + else if(relative_size > 0) + bigint_sub3(z.get_reg(), x.data(), x_sw, y.data(), y_sw); + } + + return z; + } + +/* +* Subtraction Operator +*/ +BigInt operator-(const BigInt& x, const BigInt& y) + { + const u32bit x_sw = x.sig_words(), y_sw = y.sig_words(); + + s32bit relative_size = bigint_cmp(x.data(), x_sw, y.data(), y_sw); + + BigInt z(BigInt::Positive, std::max(x_sw, y_sw) + 1); + + if(relative_size < 0) + { + if(x.sign() == y.sign()) + bigint_sub3(z.get_reg(), y.data(), y_sw, x.data(), x_sw); + else + bigint_add3(z.get_reg(), x.data(), x_sw, y.data(), y_sw); + z.set_sign(y.reverse_sign()); + } + else if(relative_size == 0) + { + if(x.sign() != y.sign()) + bigint_shl2(z.get_reg(), x.data(), x_sw, 0, 1); + } + else if(relative_size > 0) + { + if(x.sign() == y.sign()) + bigint_sub3(z.get_reg(), x.data(), x_sw, y.data(), y_sw); + else + bigint_add3(z.get_reg(), x.data(), x_sw, y.data(), y_sw); + z.set_sign(x.sign()); + } + return z; + } + +/* +* Multiplication Operator +*/ +BigInt operator*(const BigInt& x, const BigInt& y) + { + const u32bit x_sw = x.sig_words(), y_sw = y.sig_words(); + + BigInt z(BigInt::Positive, x.size() + y.size()); + + if(x_sw == 1 && y_sw) + bigint_linmul3(z.get_reg(), y.data(), y_sw, x.word_at(0)); + else if(y_sw == 1 && x_sw) + bigint_linmul3(z.get_reg(), x.data(), x_sw, y.word_at(0)); + else if(x_sw && y_sw) + { + SecureVector<word> workspace(z.size()); + bigint_mul(z.get_reg(), z.size(), workspace, + x.data(), x.size(), x_sw, + y.data(), y.size(), y_sw); + } + + if(x_sw && y_sw && x.sign() != y.sign()) + z.flip_sign(); + return z; + } + +/* +* Division Operator +*/ +BigInt operator/(const BigInt& x, const BigInt& y) + { + BigInt q, r; + divide(x, y, q, r); + return q; + } + +/* +* Modulo Operator +*/ +BigInt operator%(const BigInt& n, const BigInt& mod) + { + if(mod.is_zero()) + throw BigInt::DivideByZero(); + if(mod.is_negative()) + throw Invalid_Argument("BigInt::operator%: modulus must be > 0"); + if(n.is_positive() && mod.is_positive() && n < mod) + return n; + + BigInt q, r; + divide(n, mod, q, r); + return r; + } + +/* +* Modulo Operator +*/ +word operator%(const BigInt& n, word mod) + { + if(mod == 0) + throw BigInt::DivideByZero(); + if(power_of_2(mod)) + return (n.word_at(0) & (mod - 1)); + + word remainder = 0; + + for(u32bit j = n.sig_words(); j > 0; --j) + remainder = bigint_modop(remainder, n.word_at(j-1), mod); + + if(remainder && n.sign() == BigInt::Negative) + return mod - remainder; + return remainder; + } + +/* +* Left Shift Operator +*/ +BigInt operator<<(const BigInt& x, u32bit shift) + { + if(shift == 0) + return x; + + const u32bit shift_words = shift / MP_WORD_BITS, + shift_bits = shift % MP_WORD_BITS; + + const u32bit x_sw = x.sig_words(); + + BigInt y(x.sign(), x_sw + shift_words + (shift_bits ? 1 : 0)); + bigint_shl2(y.get_reg(), x.data(), x_sw, shift_words, shift_bits); + return y; + } + +/* +* Right Shift Operator +*/ +BigInt operator>>(const BigInt& x, u32bit shift) + { + if(shift == 0) + return x; + if(x.bits() <= shift) + return 0; + + const u32bit shift_words = shift / MP_WORD_BITS, + shift_bits = shift % MP_WORD_BITS, + x_sw = x.sig_words(); + + BigInt y(x.sign(), x_sw - shift_words); + bigint_shr2(y.get_reg(), x.data(), x_sw, shift_words, shift_bits); + return y; + } + +} diff --git a/botan/src/math/bigint/big_rand.cpp b/botan/src/math/bigint/big_rand.cpp new file mode 100644 index 0000000..b641bae --- /dev/null +++ b/botan/src/math/bigint/big_rand.cpp @@ -0,0 +1,61 @@ +/* +* BigInt Random Generation +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/bigint.h> +#include <botan/parsing.h> + +namespace Botan { + +/* +* Construct a BigInt of a specific form +*/ +BigInt::BigInt(NumberType type, u32bit bits) + { + set_sign(Positive); + + if(type == Power2) + set_bit(bits); + else + throw Invalid_Argument("BigInt(NumberType): Unknown type"); + } + +/* +* Randomize this number +*/ +void BigInt::randomize(RandomNumberGenerator& rng, + u32bit bitsize) + { + set_sign(Positive); + + if(bitsize == 0) + clear(); + else + { + SecureVector<byte> array((bitsize + 7) / 8); + rng.randomize(array, array.size()); + if(bitsize % 8) + array[0] &= 0xFF >> (8 - (bitsize % 8)); + array[0] |= 0x80 >> ((bitsize % 8) ? (8 - bitsize % 8) : 0); + binary_decode(array, array.size()); + } + } + +/* +* Generate a random integer within given range +*/ +BigInt BigInt::random_integer(RandomNumberGenerator& rng, + const BigInt& min, const BigInt& max) + { + BigInt range = max - min; + + if(range <= 0) + throw Invalid_Argument("random_integer: invalid min/max values"); + + return (min + (BigInt(rng, range.bits() + 2) % range)); + } + +} diff --git a/botan/src/math/bigint/bigint.cpp b/botan/src/math/bigint/bigint.cpp new file mode 100644 index 0000000..926bedc --- /dev/null +++ b/botan/src/math/bigint/bigint.cpp @@ -0,0 +1,369 @@ +/* +* BigInt Base +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/bigint.h> +#include <botan/mp_core.h> +#include <botan/loadstor.h> +#include <botan/parsing.h> +#include <botan/util.h> + +namespace Botan { + +/* +* Construct a BigInt from a regular number +*/ +BigInt::BigInt(u64bit n) + { + set_sign(Positive); + + if(n == 0) + return; + + const u32bit limbs_needed = sizeof(u64bit) / sizeof(word); + + reg.create(4*limbs_needed); + for(u32bit j = 0; j != limbs_needed; ++j) + reg[j] = ((n >> (j*MP_WORD_BITS)) & MP_WORD_MASK); + } + +/* +* Construct a BigInt of the specified size +*/ +BigInt::BigInt(Sign s, u32bit size) + { + reg.create(round_up(size, 8)); + signedness = s; + } + +/* +* Construct a BigInt from a "raw" BigInt +*/ +BigInt::BigInt(const BigInt& b) + { + const u32bit b_words = b.sig_words(); + + if(b_words) + { + reg.create(round_up(b_words, 8)); + reg.copy(b.data(), b_words); + set_sign(b.sign()); + } + else + { + reg.create(2); + set_sign(Positive); + } + } + +/* +* Construct a BigInt from a string +*/ +BigInt::BigInt(const std::string& str) + { + Base base = Decimal; + u32bit markers = 0; + bool negative = false; + if(str.length() > 0 && str[0] == '-') { markers += 1; negative = true; } + + if(str.length() > markers + 2 && str[markers ] == '0' && + str[markers + 1] == 'x') + { markers += 2; base = Hexadecimal; } + else if(str.length() > markers + 1 && str[markers] == '0') + { markers += 1; base = Octal; } + + *this = decode(reinterpret_cast<const byte*>(str.data()) + markers, + str.length() - markers, base); + + if(negative) set_sign(Negative); + else set_sign(Positive); + } + +/* +* Construct a BigInt from an encoded BigInt +*/ +BigInt::BigInt(const byte input[], u32bit length, Base base) + { + set_sign(Positive); + *this = decode(input, length, base); + } + +/* +* Construct a BigInt from an encoded BigInt +*/ +BigInt::BigInt(RandomNumberGenerator& rng, u32bit bits) + { + set_sign(Positive); + randomize(rng, bits); + } + +/* +* Swap this BigInt with another +*/ +void BigInt::swap(BigInt& other) + { + reg.swap(other.reg); + std::swap(signedness, other.signedness); + } + +/* +* Grow the internal storage +*/ +void BigInt::grow_reg(u32bit n) + { + reg.grow_to(round_up(size() + n, 8)); + } + +/* +* Grow the internal storage +*/ +void BigInt::grow_to(u32bit n) + { + if(n > size()) + reg.grow_to(round_up(n, 8)); + } + +/* +* Comparison Function +*/ +s32bit BigInt::cmp(const BigInt& n, bool check_signs) const + { + if(check_signs) + { + if(n.is_positive() && this->is_negative()) return -1; + if(n.is_negative() && this->is_positive()) return 1; + if(n.is_negative() && this->is_negative()) + return (-bigint_cmp(data(), sig_words(), n.data(), n.sig_words())); + } + return bigint_cmp(data(), sig_words(), n.data(), n.sig_words()); + } + +/* +* Convert this number to a u32bit, if possible +*/ +u32bit BigInt::to_u32bit() const + { + if(is_negative()) + throw Encoding_Error("BigInt::to_u32bit: Number is negative"); + if(bits() >= 32) + throw Encoding_Error("BigInt::to_u32bit: Number is too big to convert"); + + u32bit out = 0; + for(u32bit j = 0; j != 4; ++j) + out = (out << 8) | byte_at(3-j); + return out; + } + +/* +* Return byte n of this number +*/ +byte BigInt::byte_at(u32bit n) const + { + const u32bit WORD_BYTES = sizeof(word); + u32bit word_num = n / WORD_BYTES, byte_num = n % WORD_BYTES; + if(word_num >= size()) + return 0; + else + return get_byte(WORD_BYTES - byte_num - 1, reg[word_num]); + } + +/* +* Return bit n of this number +*/ +bool BigInt::get_bit(u32bit n) const + { + return ((word_at(n / MP_WORD_BITS) >> (n % MP_WORD_BITS)) & 1); + } + +/* +* Return bits {offset...offset+length} +*/ +u32bit BigInt::get_substring(u32bit offset, u32bit length) const + { + if(length > 32) + throw Invalid_Argument("BigInt::get_substring: Substring size too big"); + + u64bit piece = 0; + for(u32bit j = 0; j != 8; ++j) + piece = (piece << 8) | byte_at((offset / 8) + (7-j)); + + u64bit mask = (1 << length) - 1; + u32bit shift = (offset % 8); + + return static_cast<u32bit>((piece >> shift) & mask); + } + +/* +* Set bit number n +*/ +void BigInt::set_bit(u32bit n) + { + const u32bit which = n / MP_WORD_BITS; + const word mask = static_cast<word>(1) << (n % MP_WORD_BITS); + if(which >= size()) grow_to(which + 1); + reg[which] |= mask; + } + +/* +* Clear bit number n +*/ +void BigInt::clear_bit(u32bit n) + { + const u32bit which = n / MP_WORD_BITS; + const word mask = static_cast<word>(1) << (n % MP_WORD_BITS); + if(which < size()) + reg[which] &= ~mask; + } + +/* +* Clear all but the lowest n bits +*/ +void BigInt::mask_bits(u32bit n) + { + if(n == 0) { clear(); return; } + if(n >= bits()) return; + + const u32bit top_word = n / MP_WORD_BITS; + const word mask = (static_cast<word>(1) << (n % MP_WORD_BITS)) - 1; + + if(top_word < size()) + for(u32bit j = top_word + 1; j != size(); ++j) + reg[j] = 0; + + reg[top_word] &= mask; + } + +/* +* Count how many bytes are being used +*/ +u32bit BigInt::bytes() const + { + return (bits() + 7) / 8; + } + +/* +* Count how many bits are being used +*/ +u32bit BigInt::bits() const + { + if(sig_words() == 0) + return 0; + + u32bit full_words = sig_words() - 1, top_bits = MP_WORD_BITS; + word top_word = word_at(full_words), mask = MP_WORD_TOP_BIT; + + while(top_bits && ((top_word & mask) == 0)) + { mask >>= 1; top_bits--; } + + return (full_words * MP_WORD_BITS + top_bits); + } + +/* +* Calcluate the size in a certain base +*/ +u32bit BigInt::encoded_size(Base base) const + { + static const double LOG_2_BASE_10 = 0.30102999566; + + if(base == Binary) + return bytes(); + else if(base == Hexadecimal) + return 2*bytes(); + else if(base == Octal) + return ((bits() + 2) / 3); + else if(base == Decimal) + return static_cast<u32bit>((bits() * LOG_2_BASE_10) + 1); + else + throw Invalid_Argument("Unknown base for BigInt encoding"); + } + +/* +* Set the sign +*/ +void BigInt::set_sign(Sign s) + { + if(is_zero()) + signedness = Positive; + else + signedness = s; + } + +/* +* Reverse the value of the sign flag +*/ +void BigInt::flip_sign() + { + set_sign(reverse_sign()); + } + +/* +* Return the opposite value of the current sign +*/ +BigInt::Sign BigInt::reverse_sign() const + { + if(sign() == Positive) + return Negative; + return Positive; + } + +/* +* Return the negation of this number +*/ +BigInt BigInt::operator-() const + { + BigInt x = (*this); + x.flip_sign(); + return x; + } + +/* +* Return the absolute value of this number +*/ +BigInt BigInt::abs() const + { + BigInt x = (*this); + x.set_sign(Positive); + return x; + } + +/* +* Encode this number into bytes +*/ +void BigInt::binary_encode(byte output[]) const + { + const u32bit sig_bytes = bytes(); + for(u32bit j = 0; j != sig_bytes; ++j) + output[sig_bytes-j-1] = byte_at(j); + } + +/* +* Set this number to the value in buf +*/ +void BigInt::binary_decode(const byte buf[], u32bit length) + { + const u32bit WORD_BYTES = sizeof(word); + + reg.create(round_up((length / WORD_BYTES) + 1, 8)); + + for(u32bit j = 0; j != length / WORD_BYTES; ++j) + { + u32bit top = length - WORD_BYTES*j; + for(u32bit k = WORD_BYTES; k > 0; --k) + reg[j] = (reg[j] << 8) | buf[top - k]; + } + for(u32bit j = 0; j != length % WORD_BYTES; ++j) + reg[length / WORD_BYTES] = (reg[length / WORD_BYTES] << 8) | buf[j]; + } + +/* +* Set this number to the value in buf +*/ +void BigInt::binary_decode(const MemoryRegion<byte>& buf) + { + binary_decode(buf, buf.size()); + } + +} diff --git a/botan/src/math/bigint/bigint.h b/botan/src/math/bigint/bigint.h new file mode 100644 index 0000000..16a1bba --- /dev/null +++ b/botan/src/math/bigint/bigint.h @@ -0,0 +1,534 @@ +/* +* BigInt +* (C) 1999-2008 Jack Lloyd +* 2007 FlexSecure +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_BIGINT_H__ +#define BOTAN_BIGINT_H__ + +#include <botan/rng.h> +#include <botan/secmem.h> +#include <botan/mp_types.h> +#include <iosfwd> + +namespace Botan { + +/** + * Big Integer representation. This class defines an integer type, + * that can be very big. Additionally some helper functions are + * defined to work more comfortably. + + */ +class BOTAN_DLL BigInt + { + public: + /** + * Base-Enumerator (currently 8,10,16 and 256 are defined) + */ + enum Base { Octal = 8, Decimal = 10, Hexadecimal = 16, Binary = 256 }; + + /** + * Sign symbol definitions for positive and negative numbers + */ + enum Sign { Negative = 0, Positive = 1 }; + + /** + * Number types (Powers of 2) + */ + enum NumberType { Power2 }; + + /** + * DivideByZero Exception + */ + struct DivideByZero : public Exception + { DivideByZero() : Exception("BigInt divide by zero") {} }; + + /************* + * operators + *************/ + + /** + * += Operator + * @param y the BigInt to add to the local value + */ + BigInt& operator+=(const BigInt& y); + + /** + * -= Operator + * @param y the BigInt to subtract from the local value + */ + BigInt& operator-=(const BigInt& y); + + /** + * *= Operator + * @param y the BigInt to multiply with the local value + */ + BigInt& operator*=(const BigInt& y); + + /** + * /= Operator + * @param y the BigInt to divide the local value by + */ + BigInt& operator/=(const BigInt& y); + + /** + * %= Operator, modulo operator. + * @param y the modulus to reduce the local value by + */ + BigInt& operator%=(const BigInt& y); + + /** + * %= Operator + * @param y the modulus (word) to reduce the local value by + */ + word operator%=(word y); + + /** + * <<= Operator + * @param y the amount of bits to shift the local value left + */ + BigInt& operator<<=(u32bit y); + + /** + * >>= Operator + * @param y the amount of bits to shift the local value right + */ + BigInt& operator>>=(u32bit y); + + /** + * ++ Operator + */ + BigInt& operator++() { return (*this += 1); } + + /** + * -- Operator + */ + BigInt& operator--() { return (*this -= 1); } + + /** + * ++ Operator (postfix) + */ + BigInt operator++(int) { BigInt x = (*this); ++(*this); return x; } + + /** + * -- Operator (postfix) + */ + BigInt operator--(int) { BigInt x = (*this); --(*this); return x; } + + /** + * - Operator + */ + BigInt operator-() const; + + /** + * ! Operator + */ + bool operator !() const { return (!is_nonzero()); } + + /** + * [] Operator (array access) + */ + word& operator[](u32bit i) { return reg[i]; } + + /** + * [] Operator (array access) + */ + word operator[](u32bit i) const { return reg[i]; } + + /** + * Zeroize the BigInt + */ + void clear() { get_reg().clear(); } + + /************* + * functions + ************/ + + /** + * Compare *this to another BigInt. + * @param n the BigInt value to compare to the local value. + * @param check_signs Include sign in comparison? + * @result if (this<n) return -1, if (this>n) return 1, if both + * values are identical return 0. + */ + s32bit cmp(const BigInt& n, bool check_signs = true) const; + + /** + * Test if the integer has an even value + * @result true, if the integer an even value, false otherwise + */ + bool is_even() const { return (get_bit(0) == 0); } + + /** + * Test if the integer has an odd value + * @result true, if the integer an odd value, false otherwise + */ + bool is_odd() const { return (get_bit(0) == 1); } + + /** + * Test if the integer is not zero. + * @result true, if the integer has a non-zero value, false otherwise + */ + bool is_nonzero() const { return (!is_zero()); } + + /** + * Test if the integer is zero. + * @result true, if the integer has the value zero, false otherwise + */ + bool is_zero() const + { + const u32bit sw = sig_words(); + + for(u32bit i = 0; i != sw; ++i) + if(reg[i]) + return false; + return true; + } + + /** + * Set bit at specified position + * @param n bit position to set + */ + void set_bit(u32bit n); + + /** + * Clear bit at specified position + * @param n bit position to clear + */ + void clear_bit(u32bit n); + + /** + * Clear all but the lowest n bits + * @param n amount of bits to keep + */ + void mask_bits(u32bit n); + + /** + * Return bit value at specified position + * @param n the bit offset to test + * @result true, if the bit at position n is set, false otherwise + */ + bool get_bit(u32bit n) const; + + /** + * Return (a maximum of) 32 bits of the complete value + * @param offset the offset to start extracting + * @param length amount of bits to extract (starting at offset) + * @result the integer extracted from the register starting at + * offset with specified length + */ + u32bit get_substring(u32bit offset, u32bit length) const; + + byte byte_at(u32bit) const; + + /** + * Return the word at a specified position of the internal register + * @param n position in the register + * @return the value at position n + */ + word word_at(u32bit n) const + { return ((n < size()) ? reg[n] : 0); } + + /** + * Return the integer as an unsigned 32bit-integer-value. If the + * value is negative OR to big to be stored in 32bits, this + * function will throw an exception. + * @result a 32bit-integer + */ + u32bit to_u32bit() const; + + /** + * Tests if the sign of the integer is negative. + * @result true, if the integer has a negative sign, + */ + bool is_negative() const { return (sign() == Negative); } + + /** + * Tests if the sign of the integer is positive. + * @result true, if the integer has a positive sign, + */ + bool is_positive() const { return (sign() == Positive); } + + /** + * Return the sign of the integer + * @result the sign of the integer + */ + Sign sign() const { return (signedness); } + + /** + * Return the opposite sign of the represented integer value + * @result the opposite sign of the represented integer value + */ + Sign reverse_sign() const; + + /** + * Flip (change!) the sign of the integer to its opposite value + */ + void flip_sign(); + + /** + * Set sign of the integer + * @param sign new Sign to set + */ + void set_sign(Sign sign); + + /** + * Give absolute (positive) value of the integer + * @result absolute (positive) value of the integer + */ + BigInt abs() const; + + /** + * Give size of internal register + * @result size of internal register in words + */ + u32bit size() const { return get_reg().size(); } + + /** + * Give significant words of the represented integer value + * @result significant words of the represented integer value + */ + u32bit sig_words() const + { + const word* x = reg.begin(); + u32bit sig = reg.size(); + + while(sig && (x[sig-1] == 0)) + sig--; + return sig; + } + + /** + * Give byte-length of the integer + * @result byte-length of the represented integer value + */ + u32bit bytes() const; + + /** + * Get the bit-length of the integer. + * @result bit-length of the represented integer value + */ + u32bit bits() const; + + /** + * Return a pointer to the big integer word register. + * @result a pointer to the start of the internal register of + * the integer value + */ + const word* data() const { return reg.begin(); } + + /** + * return a reference to the internal register containing the value + * @result a reference to the word-array (SecureVector<word>) + * with the internal register value (containing the integer + * value) + */ + SecureVector<word>& get_reg() { return reg; } + + /** + * return a const reference to the internal register containing the value + * @result a const reference to the word-array (SecureVector<word>) + * with the internal register value (containing the integer + * value) + */ + const SecureVector<word>& get_reg() const { return reg; } + + /** + * Increase internal register buffer by n words + * @param n increase by n words + */ + void grow_reg(u32bit n); + + void grow_to(u32bit n); + + /** + * Fill BigInt with a random number with size of bitsize + * @param rng the random number generator to use + * @param bitsize number of bits the created random value should have + */ + void randomize(RandomNumberGenerator& rng, u32bit bitsize = 0); + + /** + * Store BigInt-value in a given byte array + * @param buf destination byte array for the integer value + */ + void binary_encode(byte buf[]) const; + + /** + * Read integer value from a byte array with given size + * @param buf byte array buffer containing the integer + * @param length size of buf + */ + void binary_decode(const byte buf[], u32bit length); + + /** + * Read integer value from a byte array (MemoryRegion<byte>) + * @param buf the BigInt value to compare to the local value. + */ + void binary_decode(const MemoryRegion<byte>& buf); + + u32bit encoded_size(Base = Binary) const; + + /** + @param rng a random number generator + @result a random integer between min and max + */ + static BigInt random_integer(RandomNumberGenerator& rng, + const BigInt& min, const BigInt& max); + + /** + * Encode the integer value from a BigInt to a SecureVector of bytes + * @param n the BigInt to use as integer source + * @param base number-base of resulting byte array representation + * @result SecureVector of bytes containing the integer with given base + */ + static SecureVector<byte> encode(const BigInt& n, Base base = Binary); + + /** + * Encode the integer value from a BigInt to a byte array + * @param buf destination byte array for the encoded integer + * value with given base + * @param n the BigInt to use as integer source + * @param base number-base of resulting byte array representation + */ + static void encode(byte buf[], const BigInt& n, Base base = Binary); + + /** + * Create a BigInt from an integer in a byte array + * @param buf the BigInt value to compare to the local value. + * @param length size of buf + * @param base number-base of the integer in buf + * @result BigInt-representing the given integer read from the byte array + */ + static BigInt decode(const byte buf[], u32bit length, + Base base = Binary); + + static BigInt decode(const MemoryRegion<byte>&, Base = Binary); + + /** + * Encode a Big Integer to a byte array according to IEEE1363. + * @param n the Big Integer to encode + * @param bytes the length of the resulting SecureVector<byte> + * @result a SecureVector<byte> containing the encoded Big Integer + */ + static SecureVector<byte> encode_1363(const BigInt& n, u32bit bytes); + + /** + * Swap BigInt-value with given BigInt. + * @param bigint the BigInt to swap values with + */ + void swap(BigInt& bigint); + + /** + * constructors + */ + + /** + * Create empty BigInt + */ + BigInt() { signedness = Positive; } + + /** + * Create BigInt from 64bit-Integer value + * @param n 64bit-integer + */ + BigInt(u64bit n); + + /** + * Copy-Constructor: clone given BigInt + * @param bigint the BigInt to clone + */ + BigInt(const BigInt& bigint); + + /** + * Create BigInt from a string. + * If the string starts with 0x the rest of the string will be + * interpreted as hexadecimal digits. + * If the string starts with 0 and the second character is NOT + * an 'x' the string will be interpreted as octal digits. + * If the string starts with non-zero digit, it will be + * interpreted as a decimal number. + * @param str the string to parse for an integer value + */ + BigInt(const std::string& str); + + /** + * Create a BigInt from an integer in a byte array + * @param buf the BigInt value to compare to the local value. + * @param length size of buf + * @param base number-base of the integer in buf + */ + BigInt(const byte buf[], u32bit length, Base base = Binary); + + /** + * Create a random BigInt of the specified size + * @param rng random number generator + * @param bits size in bits + */ + BigInt(RandomNumberGenerator& rng, u32bit bits); + + /** + * Create BigInt from unsigned 32 bit integer value and an + * also specify the sign of the value + * @param n integer value + */ + BigInt(Sign, u32bit n); + + /** + * Create a number of the specified type and size + * @param type the type of number to create + * @param n the size + */ + BigInt(NumberType type, u32bit n); + + private: + SecureVector<word> reg; + Sign signedness; + }; + +/* +* Arithmetic Operators +*/ +BigInt BOTAN_DLL operator+(const BigInt&, const BigInt&); +BigInt BOTAN_DLL operator-(const BigInt&, const BigInt&); +BigInt BOTAN_DLL operator*(const BigInt&, const BigInt&); +BigInt BOTAN_DLL operator/(const BigInt&, const BigInt&); +BigInt BOTAN_DLL operator%(const BigInt&, const BigInt&); +word BOTAN_DLL operator%(const BigInt&, word); +BigInt BOTAN_DLL operator<<(const BigInt&, u32bit); +BigInt BOTAN_DLL operator>>(const BigInt&, u32bit); + +/* +* Comparison Operators +*/ +inline bool operator==(const BigInt& a, const BigInt& b) + { return (a.cmp(b) == 0); } +inline bool operator!=(const BigInt& a, const BigInt& b) + { return (a.cmp(b) != 0); } +inline bool operator<=(const BigInt& a, const BigInt& b) + { return (a.cmp(b) <= 0); } +inline bool operator>=(const BigInt& a, const BigInt& b) + { return (a.cmp(b) >= 0); } +inline bool operator<(const BigInt& a, const BigInt& b) + { return (a.cmp(b) < 0); } +inline bool operator>(const BigInt& a, const BigInt& b) + { return (a.cmp(b) > 0); } + +/* +* I/O Operators +*/ +BOTAN_DLL std::ostream& operator<<(std::ostream&, const BigInt&); +BOTAN_DLL std::istream& operator>>(std::istream&, BigInt&); + +} + +namespace std { + +inline void swap(Botan::BigInt& a, Botan::BigInt& b) { a.swap(b); } + +} + +#endif diff --git a/botan/src/math/bigint/divide.cpp b/botan/src/math/bigint/divide.cpp new file mode 100644 index 0000000..6afaa0f --- /dev/null +++ b/botan/src/math/bigint/divide.cpp @@ -0,0 +1,106 @@ +/* +* Division Algorithm +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/divide.h> +#include <botan/mp_core.h> + +namespace Botan { + +namespace { + +/* +* Handle signed operands, if necessary +*/ +void sign_fixup(const BigInt& x, const BigInt& y, BigInt& q, BigInt& r) + { + if(x.sign() == BigInt::Negative) + { + q.flip_sign(); + if(r.is_nonzero()) { --q; r = y.abs() - r; } + } + if(y.sign() == BigInt::Negative) + q.flip_sign(); + } + +} + +/* +* Solve x = q * y + r +*/ +void divide(const BigInt& x, const BigInt& y_arg, BigInt& q, BigInt& r) + { + if(y_arg.is_zero()) + throw BigInt::DivideByZero(); + + BigInt y = y_arg; + const u32bit y_words = y.sig_words(); + r = x; + + r.set_sign(BigInt::Positive); + y.set_sign(BigInt::Positive); + + s32bit compare = r.cmp(y); + + if(compare < 0) + q = 0; + else if(compare == 0) + { + q = 1; + r = 0; + } + else + { + u32bit shifts = 0; + word y_top = y[y.sig_words()-1]; + while(y_top < MP_WORD_TOP_BIT) { y_top <<= 1; ++shifts; } + y <<= shifts; + r <<= shifts; + + const u32bit n = r.sig_words() - 1, t = y_words - 1; + + q.get_reg().create(n - t + 1); + if(n <= t) + { + while(r > y) { r -= y; ++q; } + r >>= shifts; + sign_fixup(x, y_arg, q, r); + return; + } + + BigInt temp = y << (MP_WORD_BITS * (n-t)); + + while(r >= temp) { r -= temp; ++q[n-t]; } + + for(u32bit j = n; j != t; --j) + { + const word x_j0 = r.word_at(j); + const word x_j1 = r.word_at(j-1); + const word y_t = y.word_at(t); + + if(x_j0 == y_t) + q[j-t-1] = MP_WORD_MAX; + else + q[j-t-1] = bigint_divop(x_j0, x_j1, y_t); + + while(bigint_divcore(q[j-t-1], y_t, y.word_at(t-1), + x_j0, x_j1, r.word_at(j-2))) + --q[j-t-1]; + + r -= (q[j-t-1] * y) << (MP_WORD_BITS * (j-t-1)); + if(r.is_negative()) + { + r += y << (MP_WORD_BITS * (j-t-1)); + --q[j-t-1]; + } + } + r >>= shifts; + } + + sign_fixup(x, y_arg, q, r); + } + +} diff --git a/botan/src/math/bigint/divide.h b/botan/src/math/bigint/divide.h new file mode 100644 index 0000000..9445b13 --- /dev/null +++ b/botan/src/math/bigint/divide.h @@ -0,0 +1,19 @@ +/* +* Division +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_DIVISON_ALGORITHM_H__ +#define BOTAN_DIVISON_ALGORITHM_H__ + +#include <botan/bigint.h> + +namespace Botan { + +void BOTAN_DLL divide(const BigInt&, const BigInt&, BigInt&, BigInt&); + +} + +#endif diff --git a/botan/src/math/bigint/info.txt b/botan/src/math/bigint/info.txt new file mode 100644 index 0000000..513703d --- /dev/null +++ b/botan/src/math/bigint/info.txt @@ -0,0 +1,33 @@ +realname "BigInt" + +load_on auto + +define BIGINT + +<add> +bigint.h +divide.h +mp_core.h +mp_types.h +big_code.cpp +big_io.cpp +big_ops2.cpp +big_ops3.cpp +big_rand.cpp +bigint.cpp +divide.cpp +mp_asm.cpp +mp_comba.cpp +mp_karat.cpp +mp_misc.cpp +mp_shift.cpp +</add> + +<requires> +alloc +hex +mp_amd64|mp_asm64|mp_ia32|mp_ia32_msvc|mp_generic +monty_generic +mulop_generic +rng +</requires> diff --git a/botan/src/math/bigint/monty_amd64/info.txt b/botan/src/math/bigint/monty_amd64/info.txt new file mode 100644 index 0000000..a897045 --- /dev/null +++ b/botan/src/math/bigint/monty_amd64/info.txt @@ -0,0 +1,32 @@ +realname "Montgomery Reduction (x86-64)" + +mp_bits 64 + +load_on never + +<add> +mp_monty.S +</add> + +<arch> +amd64 +</arch> + +<cc> +gcc +icc +</cc> + +# ELF systems +<os> +linux +freebsd +dragonfly +netbsd +openbsd +solaris +</os> + +<requires> +asm_amd64 +</requires> diff --git a/botan/src/math/bigint/monty_amd64/mp_monty.S b/botan/src/math/bigint/monty_amd64/mp_monty.S new file mode 100644 index 0000000..22045c3 --- /dev/null +++ b/botan/src/math/bigint/monty_amd64/mp_monty.S @@ -0,0 +1,399 @@ +/* +* Montgomery Reduction Source File +* (C) 2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/asm_macr.h> + +START_LISTING(mp_monty.S) + +START_FUNCTION(bigint_monty_redc) + pushq %r15 # + pushq %r14 # + pushq %r13 # + pushq %r12 # + pushq %rbp # + pushq %rbx # + + movq %rdi, %r14 # z + movq %rdx, %r12 # x + movl %esi, %ebp # z_size + + xorl %esi, %esi # j.76 + movq %r8, -16(%rsp) # u, u + movl %ecx, %ebx # x_size, x_size + movl %ecx, %r8d # x_size, blocks_of_8 + andl $-8, %r8d #, blocks_of_8 + testl %ecx, %ecx # x_size + je .L3 #, + mov %ecx, %eax # x_size, pretmp.71 + leal 1(%rbx), %r15d #, k.73 + salq $3, %rax #, + xorl %r13d, %r13d # j + movq %rax, -8(%rsp) #, pretmp.21 + .p2align 4,,10 + .p2align 3 +.L11: + mov %r13d, %eax # j, j + movq -16(%rsp), %rdi # u, y + leaq (%r14,%rax,8), %r11 #, z_j + xorl %r9d, %r9d # i + imulq (%r11), %rdi #* z_j, y + xorl %r10d, %r10d # carry + testl %r8d, %r8d # blocks_of_8 + je .L7 #, + .p2align 4,,10 + .p2align 3 +.LOOP_MUL_ADD: + mov %r9d, %ecx # i, i + addl $8, %r9d #, i + salq $3, %rcx #, D.2315 + leaq (%r11,%rcx), %rsi #, tmp130 + leaq (%r12,%rcx), %rcx #, tmp131 + + movq 8*0(%rcx), %rax + mulq %rdi # y + addq %r10, %rax # carry + adcq $0,%rdx + addq 8*0(%rsi), %rax + adcq $0,%rdx + movq %rdx,%r10 # carry + movq %rax, 8*0 (%rsi) + + movq 8*1(%rcx), %rax + mulq %rdi # y + addq %r10, %rax # carry + adcq $0,%rdx + addq 8*1(%rsi), %rax + adcq $0,%rdx + movq %rdx,%r10 # carry + movq %rax, 8*1 (%rsi) + + movq 8*2(%rcx), %rax + mulq %rdi # y + addq %r10, %rax # carry + adcq $0,%rdx + addq 8*2(%rsi), %rax + adcq $0,%rdx + movq %rdx,%r10 # carry + movq %rax, 8*2 (%rsi) + + movq 8*3(%rcx), %rax + mulq %rdi # y + addq %r10, %rax # carry + adcq $0,%rdx + addq 8*3(%rsi), %rax + adcq $0,%rdx + movq %rdx,%r10 # carry + movq %rax, 8*3 (%rsi) + + movq 8*4(%rcx), %rax + mulq %rdi # y + addq %r10, %rax # carry + adcq $0,%rdx + addq 8*4(%rsi), %rax + adcq $0,%rdx + movq %rdx,%r10 # carry + movq %rax, 8*4 (%rsi) + + movq 8*5(%rcx), %rax + mulq %rdi # y + addq %r10, %rax # carry + adcq $0,%rdx + addq 8*5(%rsi), %rax + adcq $0,%rdx + movq %rdx,%r10 # carry + movq %rax, 8*5 (%rsi) + + movq 8*6(%rcx), %rax + mulq %rdi # y + addq %r10, %rax # carry + adcq $0,%rdx + addq 8*6(%rsi), %rax + adcq $0,%rdx + movq %rdx,%r10 # carry + movq %rax, 8*6 (%rsi) + + movq 8*7(%rcx), %rax + mulq %rdi # y + addq %r10, %rax # carry + adcq $0,%rdx + addq 8*7(%rsi), %rax + adcq $0,%rdx + movq %rdx,%r10 # carry + movq %rax, 8*7 (%rsi) + + cmpl %r9d, %r8d # i, blocks_of_8 + jne .LOOP_MUL_ADD #, + cmpl %r8d, %ebx # blocks_of_8, x_size + je .L8 #, +.L7: + movl %r8d, %esi # blocks_of_8, i + .p2align 4,,10 + .p2align 3 +.L5: + mov %esi, %eax # i, i + movq %rdi, %rcx # y, b + leaq (%r11, %rax,8), %r9 #, D.2325 + incl %esi # i + movq (%r12, %rax,8), %rax #* x, tmp133 + + mulq %rcx # b + addq (%r9), %rax #* D.2325, a + adcq $0,%rdx # + addq %r10, %rax # carry, a + adcq $0,%rdx # + + cmpl %esi, %ebx # i, x_size + movq %rdx, %r10 #, carry + movq %rax, (%r9) # a,* D.2325 + jne .L5 #, +.L8: + movq -8(%rsp), %rdx # pretmp.21, + leaq (%r11,%rdx), %rax #, D.2332 + movq (%rax), %rcx #* D.2332, D.2333 + leaq (%r10,%rcx), %rdx #, z_sum + movq %rdx, (%rax) # z_sum,* D.2332 + cmpq %rdx, %rcx # z_sum, D.2333 + jbe .L9 #, + cmpl %ebp, %r15d # z_size, k.73 + je .L9 #, + movl %r15d, %ecx # k.73, k + jmp .L10 # + .p2align 4,,10 + .p2align 3 +.L31: + incl %ecx # k + cmpl %ecx, %ebp # k, z_size + .p2align 4,,4 + .p2align 3 + je .L9 #, +.L10: + mov %ecx, %edx # k, k + leaq (%r11,%rdx,8), %rdx #, D.2342 + movq (%rdx), %rax #* D.2342, tmp136 + incq %rax # D.2344 + movq %rax, (%rdx) # D.2344,* D.2342 + testq %rax, %rax # D.2344 + je .L31 #, +.L9: + incl %r13d # j + decl %ebp # z_size + cmpl %r13d, %ebx # j, x_size + jne .L11 #, + movl %ebx, %esi # x_size, j.76 +.L3: + leal (%rbx,%rbx), %eax #, tmp137 + mov %eax, %eax + leaq (%r14, %rax,8), %rdi #, D.2349 + cmpq $0, (%rdi) #,* D.2349 + jne .L12 #, + testl %ebx, %ebx # x_size + je .L12 #, + leal -1(%rbx), %ecx #, j + leal (%rsi,%rcx), %edx #, tmp141 + mov %ecx, %eax # j, j + movq (%r14,%rdx,8), %rbp #* z, + cmpq %rbp, (%r12, %rax,8) #,* x + jb .L12 #, + ja .L_EXIT #, + leal -2(%rsi,%rbx), %edx #, ivtmp.45 + jmp .L14 # + .p2align 4,,10 + .p2align 3 +.L15: + mov %edx, %eax # ivtmp.45, ivtmp.45 + decl %ecx # j + movq (%r14, %rax,8), %rsi #* z, D.2360 + mov %ecx, %eax # j, j + movq (%r12, %rax,8), %rax #* x, temp.68 + cmpq %rax, %rsi + ja .L12 #, + decl %edx # ivtmp.45 + cmpq %rax, %rsi + jb .L_EXIT #, +.L14: + testl %ecx, %ecx # j + jne .L15 #, +.L12: + xorl %ecx, %ecx # j + xorl %r10d, %r10d # carry + mov %ebx, %esi # x_size, pretmp.19 + testl %r8d, %r8d # blocks_of_8 + je .L17 #, + .p2align 4,,10 + .p2align 3 +.L22: + mov %ecx, %edx # j, D.2375 + addl $8, %ecx #, j + leaq (%rdx,%rsi), %rax #, tmp146 + leaq (%r12,%rdx,8), %rdx #, tmp150 + leaq (%r14, %rax,8), %rax #, tmp148 + + rorq %r10 # carry + + movq 8*0(%rdx), %r10 + sbbq %r10, 8*0(%rax) + + movq 8*1(%rdx), %r10 + sbbq %r10, 8*1(%rax) + + movq 8*2(%rdx), %r10 + sbbq %r10, 8*2(%rax) + + movq 8*3(%rdx), %r10 + sbbq %r10, 8*3(%rax) + + movq 8*4(%rdx), %r10 + sbbq %r10, 8*4(%rax) + + movq 8*5(%rdx), %r10 + sbbq %r10, 8*5(%rax) + + movq 8*6(%rdx), %r10 + sbbq %r10, 8*6(%rax) + + movq 8*7(%rdx), %r10 + sbbq %r10, 8*7(%rax) + + sbbq %r10,%r10 # carry + negq %r10 # carry + + cmpl %ecx, %r8d # j, blocks_of_8 + jne .L22 #, +.L17: + cmpl %r8d, %ebx # blocks_of_8, x_size + je .L19 #, + leal (%r8,%rbx), %r9d #, ivtmp.33 + movl %r8d, %esi # blocks_of_8, j + .p2align 4,,10 + .p2align 3 +.L20: + mov %r9d, %eax # ivtmp.33, ivtmp.33 + mov %esi, %ecx # j, j + leaq (%r14, %rax,8), %rax #, D.2387 + incl %esi # j + movq (%rax), %rdx #* D.2387, tmp153 + incl %r9d # ivtmp.33 + + rorq %r10 # carry + sbbq (%r12,%rcx,8),%rdx #* x, x + sbbq %r10,%r10 # carry + negq %r10 # carry + + cmpl %esi, %ebx # j, x_size + movq %rdx, (%rax) # x,* D.2387 + jne .L20 #, +.L19: + testq %r10, %r10 # carry + je .L_EXIT #, + decq (%rdi) #* D.2349 +.L_EXIT: + popq %rbx # + popq %rbp # + popq %r12 # + popq %r13 # + popq %r14 # + popq %r15 # +END_FUNCTION(bigint_monty_redc) + + +#if 0 + #define Z_ARR ARG_1 // rdi +#define Z_SIZE ARG_2_32 // esi +// X_ARR is ARG_3 == rdx, moved b/c needed for multiply +#define X_SIZE ARG_4_32 // ecx +#define U ARG_5 // r8 + +/* + We need all arguments for a while (we can reuse U eventually) + So only temp registers are + TEMP_1 %r10 + TEMP_2 %r11 + TEMP_3 = ARG_6 = %r9 + void return, so also + R0 %rax (aka TEMP_9) + is free (but needed for multiply) + + Can push: + %rbx (base pointer, callee saved) + %rpb (frame pointer, callee saved) + %r12-%r15 (callee saved) + + Can push base/frame pointers since this is a leaf function + and does not reference any data. +*/ + + push %r12 + push %r13 + push %r14 + push %r15 + +#define LOOP_CTR_I %r12 +#define LOOP_CTR_J %r13 + +#define CARRY TEMP_1 +#define Z_WORD TEMP_2 +#define X_ARR TEMP_3 +#define MUL_LO %rax +#define MUL_HI %rdx + + ASSIGN(X_ARR, ARG_3) + + /* + ZEROIZE(CARRY) + + ASSIGN(LOOP_CTR, X_SIZE) + + JUMP_IF_ZERO(LOOP_CTR, .L_MULADD_DONE) + JUMP_IF_LT(LOOP_CTR, 8, .LOOP_MULADD1) + +#define MULADD_OP(N) \ + ASSIGN(MUL_LO, ARRAY8(X_ARR, N)) ; \ + ASSIGN(Z_WORD, ARRAY8(Z_ARR, N)) ; \ + MUL(Y) ; \ + ADD(Z_WORD, CARRY) ; \ + ASSIGN(CARRY, MUL_HI) ; \ + ADD_LAST_CARRY(CARRY) ; \ + ADD(Z_WORD, MUL_LO) ; \ + ADD_LAST_CARRY(CARRY) ; \ + ASSIGN(ARRAY8(Z_ARR, N), Z_WORD) + +ALIGN +.LOOP_MULADD8: + MULADD_OP(0) + MULADD_OP(1) + MULADD_OP(2) + MULADD_OP(3) + MULADD_OP(4) + MULADD_OP(5) + MULADD_OP(6) + MULADD_OP(7) + + SUB_IMM(LOOP_CTR, 8) + ADD_IMM(Z_ARR, 64) + ADD_IMM(X_ARR, 64) + cmp IMM(8), LOOP_CTR + jge .LOOP_MULADD8 + + JUMP_IF_ZERO(LOOP_CTR, .L_MULADD_DONE) + +ALIGN +.LOOP_MULADD1: + MULADD_OP(0) + + SUB_IMM(LOOP_CTR, 1) + ADD_IMM(Z_ARR, 8) + ADD_IMM(X_ARR, 8) + + cmp IMM(0), LOOP_CTR + jne .LOOP_MULADD1 +*/ + + pop %r15 + pop %r14 + pop %r13 + pop %r12 +#endif diff --git a/botan/src/math/bigint/monty_generic/info.txt b/botan/src/math/bigint/monty_generic/info.txt new file mode 100644 index 0000000..6f5f0e7 --- /dev/null +++ b/botan/src/math/bigint/monty_generic/info.txt @@ -0,0 +1,7 @@ +realname "Montgomery Reduction" + +load_on dep + +<add> +mp_monty.cpp +</add> diff --git a/botan/src/math/bigint/monty_generic/mp_monty.cpp b/botan/src/math/bigint/monty_generic/mp_monty.cpp new file mode 100644 index 0000000..5409e25 --- /dev/null +++ b/botan/src/math/bigint/monty_generic/mp_monty.cpp @@ -0,0 +1,78 @@ +/* +* Montgomery Reduction +* (C) 1999-2008 Jack Lloyd +* 2006 Luca Piccarreta +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/mp_core.h> +#include <botan/mp_asm.h> +#include <botan/mp_asmi.h> + +namespace Botan { + +extern "C" { + +/* +* Montgomery Reduction Algorithm +*/ +void bigint_monty_redc(word z[], u32bit z_size, + const word x[], u32bit x_size, word u) + { + const u32bit blocks_of_8 = x_size - (x_size % 8); + + for(u32bit i = 0; i != x_size; ++i) + { + word* z_i = z + i; + + const word y = z_i[0] * u; + + word carry = 0; + + for(u32bit j = 0; j != blocks_of_8; j += 8) + carry = word8_madd3(z_i + j, x + j, y, carry); + + for(u32bit j = blocks_of_8; j != x_size; ++j) + z_i[j] = word_madd3(x[j], y, z_i[j], &carry); + + word z_sum = z_i[x_size] + carry; + carry = (z_sum < z_i[x_size]); + z_i[x_size] = z_sum; + + for(u32bit j = x_size + 1; carry && j != z_size - i; ++j) + { + ++z_i[j]; + carry = !z_i[j]; + } + } + + // Check if z[x_size...x_size+1] >= x[0...x_size] using bigint_cmp (inlined) + if(!z[x_size + x_size]) + { + for(u32bit i = x_size; i > 0; --i) + { + if(z[x_size + i - 1] > x[i-1]) + break; + + if(z[x_size + i - 1] < x[i-1]) + return; + } + } + + // If the compare above is true, subtract using bigint_sub2 (inlined) + word carry = 0; + + for(u32bit i = 0; i != blocks_of_8; i += 8) + carry = word8_sub2(z + x_size + i, x + i, carry); + + for(u32bit i = blocks_of_8; i != x_size; ++i) + z[x_size + i] = word_sub(z[x_size + i], x[i], &carry); + + if(carry) + --z[x_size+x_size]; + } + +} + +} diff --git a/botan/src/math/bigint/mp_amd64/info.txt b/botan/src/math/bigint/mp_amd64/info.txt new file mode 100644 index 0000000..84a5bcf --- /dev/null +++ b/botan/src/math/bigint/mp_amd64/info.txt @@ -0,0 +1,19 @@ +realname "MPI Core (x86-64)" + +mp_bits 64 + +load_on dep + +<add> +mp_asm.h +mp_asmi.h +</add> + +<arch> +amd64 +</arch> + +<cc> +gcc +icc +</cc> diff --git a/botan/src/math/bigint/mp_amd64/mp_asm.h b/botan/src/math/bigint/mp_amd64/mp_asm.h new file mode 100644 index 0000000..fa66d04 --- /dev/null +++ b/botan/src/math/bigint/mp_amd64/mp_asm.h @@ -0,0 +1,69 @@ +/* +* Lowest Level MPI Algorithms +* (C) 1999-2008 Jack Lloyd +* 2006 Luca Piccarreta +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_MP_ASM_H__ +#define BOTAN_MP_ASM_H__ + +#include <botan/mp_types.h> + +#if (BOTAN_MP_WORD_BITS != 64) + #error The mp_amd64 module requires that BOTAN_MP_WORD_BITS == 64 +#endif + +namespace Botan { + +extern "C" { + +/* +* Helper Macros for amd64 Assembly +*/ +#define ASM(x) x "\n\t" + +/* +* Word Multiply +*/ +inline word word_madd2(word a, word b, word* c) + { + asm( + ASM("mulq %[b]") + ASM("addq %[c],%[a]") + ASM("adcq $0,%[carry]") + + : [a]"=a"(a), [b]"=rm"(b), [carry]"=&d"(*c) + : "0"(a), "1"(b), [c]"g"(*c) : "cc"); + + return a; + } + +/* +* Word Multiply/Add +*/ +inline word word_madd3(word a, word b, word c, word* d) + { + asm( + ASM("mulq %[b]") + + ASM("addq %[c],%[a]") + ASM("adcq $0,%[carry]") + + ASM("addq %[d],%[a]") + ASM("adcq $0,%[carry]") + + : [a]"=a"(a), [b]"=rm"(b), [carry]"=&d"(*d) + : "0"(a), "1"(b), [c]"g"(c), [d]"g"(*d) : "cc"); + + return a; + } + +#undef ASM + +} + +} + +#endif diff --git a/botan/src/math/bigint/mp_amd64/mp_asmi.h b/botan/src/math/bigint/mp_amd64/mp_asmi.h new file mode 100644 index 0000000..8bccbaa --- /dev/null +++ b/botan/src/math/bigint/mp_amd64/mp_asmi.h @@ -0,0 +1,243 @@ +/* +* Lowest Level MPI Algorithms +* (C) 1999-2007 Jack Lloyd +* 2006 Luca Piccarreta +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_MP_ASM_INTERNAL_H__ +#define BOTAN_MP_ASM_INTERNAL_H__ + +#include <botan/mp_asm.h> + +namespace Botan { + +extern "C" { + +/* +* Helper Macros for amd64 Assembly +*/ +#ifndef ASM + #define ASM(x) x "\n\t" +#endif + +#define ADDSUB2_OP(OPERATION, INDEX) \ + ASM("movq 8*" #INDEX "(%[y]), %[carry]") \ + ASM(OPERATION " %[carry], 8*" #INDEX "(%[x])") \ + +#define ADDSUB3_OP(OPERATION, INDEX) \ + ASM("movq 8*" #INDEX "(%[x]), %[carry]") \ + ASM(OPERATION " 8*" #INDEX "(%[y]), %[carry]") \ + ASM("movq %[carry], 8*" #INDEX "(%[z])") \ + +#define LINMUL_OP(WRITE_TO, INDEX) \ + ASM("movq 8*" #INDEX "(%[x]),%%rax") \ + ASM("mulq %[y]") \ + ASM("addq %[carry],%%rax") \ + ASM("adcq $0,%%rdx") \ + ASM("movq %%rdx,%[carry]") \ + ASM("movq %%rax, 8*" #INDEX "(%[" WRITE_TO "])") + +#define MULADD_OP(IGNORED, INDEX) \ + ASM("movq 8*" #INDEX "(%[x]),%%rax") \ + ASM("mulq %[y]") \ + ASM("addq %[carry],%%rax") \ + ASM("adcq $0,%%rdx") \ + ASM("addq 8*" #INDEX "(%[z]),%%rax") \ + ASM("adcq $0,%%rdx") \ + ASM("movq %%rdx,%[carry]") \ + ASM("movq %%rax, 8*" #INDEX " (%[z])") + +#define DO_8_TIMES(MACRO, ARG) \ + MACRO(ARG, 0) \ + MACRO(ARG, 1) \ + MACRO(ARG, 2) \ + MACRO(ARG, 3) \ + MACRO(ARG, 4) \ + MACRO(ARG, 5) \ + MACRO(ARG, 6) \ + MACRO(ARG, 7) + +#define ADD_OR_SUBTRACT(CORE_CODE) \ + ASM("rorq %[carry]") \ + CORE_CODE \ + ASM("sbbq %[carry],%[carry]") \ + ASM("negq %[carry]") + +/* +* Word Addition +*/ +inline word word_add(word x, word y, word* carry) + { +#if 0 + asm( + ADD_OR_SUBTRACT(ASM("adcq %[y],%[x]")) + : [x]"=r"(x), [carry]"=r"(*carry) + : "0"(x), [y]"rm"(y), "1"(*carry) + : "cc"); + return x; +#else + word z = x + y; + word c1 = (z < x); + z += *carry; + *carry = c1 | (z < *carry); + return z; +#endif + } + +/* +* Eight Word Block Addition, Two Argument +*/ +inline word word8_add2(word x[8], const word y[8], word carry) + { + asm( + ADD_OR_SUBTRACT(DO_8_TIMES(ADDSUB2_OP, "adcq")) + : [carry]"=r"(carry) + : [x]"r"(x), [y]"r"(y), "0"(carry) + : "cc", "memory"); + return carry; + } + +/* +* Eight Word Block Addition, Three Argument +*/ +inline word word8_add3(word z[8], const word x[8], const word y[8], word carry) + { + asm( + ADD_OR_SUBTRACT(DO_8_TIMES(ADDSUB3_OP, "adcq")) + : [carry]"=r"(carry) + : [x]"r"(x), [y]"r"(y), [z]"r"(z), "0"(carry) + : "cc", "memory"); + return carry; + } + +/* +* Word Subtraction +*/ +inline word word_sub(word x, word y, word* carry) + { + asm( + ADD_OR_SUBTRACT(ASM("sbbq %[y],%[x]")) + : [x]"=r"(x), [carry]"=r"(*carry) + : "0"(x), [y]"rm"(y), "1"(*carry) + : "cc"); + return x; + } + +/* +* Eight Word Block Subtraction, Two Argument +*/ +inline word word8_sub2(word x[8], const word y[8], word carry) + { + asm( + ADD_OR_SUBTRACT(DO_8_TIMES(ADDSUB2_OP, "sbbq")) + : [carry]"=r"(carry) + : [x]"r"(x), [y]"r"(y), "0"(carry) + : "cc", "memory"); + return carry; + } + +/* +* Eight Word Block Subtraction, Three Argument +*/ +inline word word8_sub3(word z[8], const word x[8], const word y[8], word carry) + { + asm( + ADD_OR_SUBTRACT(DO_8_TIMES(ADDSUB3_OP, "sbbq")) + : [carry]"=r"(carry) + : [x]"r"(x), [y]"r"(y), [z]"r"(z), "0"(carry) + : "cc", "memory"); + return carry; + } + +/* +* Eight Word Block Linear Multiplication +*/ +inline word word8_linmul2(word x[8], word y, word carry) + { + asm( + DO_8_TIMES(LINMUL_OP, "x") + : [carry]"=r"(carry) + : [x]"r"(x), [y]"rm"(y), "0"(carry) + : "cc", "%rax", "%rdx"); + return carry; + } + +/* +* Eight Word Block Linear Multiplication +*/ +inline word word8_linmul3(word z[8], const word x[8], word y, word carry) + { + asm( + DO_8_TIMES(LINMUL_OP, "z") + : [carry]"=r"(carry) + : [z]"r"(z), [x]"r"(x), [y]"rm"(y), "0"(carry) + : "cc", "%rax", "%rdx"); + return carry; + } + +/* +* Eight Word Block Multiply/Add +*/ +inline word word8_madd3(word z[8], const word x[8], word y, word carry) + { + asm( + DO_8_TIMES(MULADD_OP, "") + : [carry]"=r"(carry) + : [z]"r"(z), [x]"r"(x), [y]"rm"(y), "0"(carry) + : "cc", "%rax", "%rdx"); + return carry; + } + +/* +* Multiply-Add Accumulator +*/ +inline void word3_muladd(word* w2, word* w1, word* w0, word x, word y) + { + asm( + ASM("mulq %[y]") + + ASM("addq %[x],%[w0]") + ASM("adcq %[y],%[w1]") + ASM("adcq $0,%[w2]") + + : [w0]"=r"(*w0), [w1]"=r"(*w1), [w2]"=r"(*w2) + : [x]"a"(x), [y]"d"(y), "0"(*w0), "1"(*w1), "2"(*w2) + : "cc"); + } + +/* +* Multiply-Add Accumulator +*/ +inline void word3_muladd_2(word* w2, word* w1, word* w0, word x, word y) + { + asm( + ASM("mulq %[y]") + + ASM("addq %[x],%[w0]") + ASM("adcq %[y],%[w1]") + ASM("adcq $0,%[w2]") + + ASM("addq %[x],%[w0]") + ASM("adcq %[y],%[w1]") + ASM("adcq $0,%[w2]") + + : [w0]"=r"(*w0), [w1]"=r"(*w1), [w2]"=r"(*w2) + : [x]"a"(x), [y]"d"(y), "0"(*w0), "1"(*w1), "2"(*w2) + : "cc"); + } + + +#undef ASM +#undef DO_8_TIMES +#undef ADD_OR_SUBTRACT +#undef ADDSUB2_OP +#undef ADDSUB3_OP +#undef LINMUL_OP +#undef MULADD_OP + +} + +} +#endif diff --git a/botan/src/math/bigint/mp_asm.cpp b/botan/src/math/bigint/mp_asm.cpp new file mode 100644 index 0000000..ea9738d --- /dev/null +++ b/botan/src/math/bigint/mp_asm.cpp @@ -0,0 +1,179 @@ +/* +* Lowest Level MPI Algorithms +* (C) 1999-2008 Jack Lloyd +* 2006 Luca Piccarreta +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/mp_asm.h> +#include <botan/mp_asmi.h> +#include <botan/mp_core.h> +#include <botan/mem_ops.h> + +namespace Botan { + +extern "C" { + +/* +* Two Operand Addition, No Carry +*/ +word bigint_add2_nc(word x[], u32bit x_size, const word y[], u32bit y_size) + { + word carry = 0; + + const u32bit blocks = y_size - (y_size % 8); + + for(u32bit j = 0; j != blocks; j += 8) + carry = word8_add2(x + j, y + j, carry); + + for(u32bit j = blocks; j != y_size; ++j) + x[j] = word_add(x[j], y[j], &carry); + + if(!carry) + return 0; + + for(u32bit j = y_size; j != x_size; ++j) + if(++x[j]) + return 0; + + return 1; + } + +/* +* Three Operand Addition, No Carry +*/ +word bigint_add3_nc(word z[], const word x[], u32bit x_size, + const word y[], u32bit y_size) + { + if(x_size < y_size) + { return bigint_add3_nc(z, y, y_size, x, x_size); } + + word carry = 0; + + const u32bit blocks = y_size - (y_size % 8); + + for(u32bit j = 0; j != blocks; j += 8) + carry = word8_add3(z + j, x + j, y + j, carry); + + for(u32bit j = blocks; j != y_size; ++j) + z[j] = word_add(x[j], y[j], &carry); + + for(u32bit j = y_size; j != x_size; ++j) + { + word x_j = x[j] + carry; + if(carry && x_j) + carry = 0; + z[j] = x_j; + } + + return carry; + } + +/* +* Two Operand Addition +*/ +void bigint_add2(word x[], u32bit x_size, const word y[], u32bit y_size) + { + if(bigint_add2_nc(x, x_size, y, y_size)) + ++x[x_size]; + } + +/* +* Three Operand Addition +*/ +void bigint_add3(word z[], const word x[], u32bit x_size, + const word y[], u32bit y_size) + { + if(bigint_add3_nc(z, x, x_size, y, y_size)) + ++z[(x_size > y_size ? x_size : y_size)]; + } + +/* +* Two Operand Subtraction +*/ +void bigint_sub2(word x[], u32bit x_size, const word y[], u32bit y_size) + { + word carry = 0; + + const u32bit blocks = y_size - (y_size % 8); + + for(u32bit j = 0; j != blocks; j += 8) + carry = word8_sub2(x + j, y + j, carry); + + for(u32bit j = blocks; j != y_size; ++j) + x[j] = word_sub(x[j], y[j], &carry); + + if(!carry) return; + + for(u32bit j = y_size; j != x_size; ++j) + { + --x[j]; + if(x[j] != MP_WORD_MAX) return; + } + } + +/* +* Three Operand Subtraction +*/ +void bigint_sub3(word z[], const word x[], u32bit x_size, + const word y[], u32bit y_size) + { + word carry = 0; + + const u32bit blocks = y_size - (y_size % 8); + + for(u32bit j = 0; j != blocks; j += 8) + carry = word8_sub3(z + j, x + j, y + j, carry); + + for(u32bit j = blocks; j != y_size; ++j) + z[j] = word_sub(x[j], y[j], &carry); + + for(u32bit j = y_size; j != x_size; ++j) + { + word x_j = x[j] - carry; + if(carry && x_j != MP_WORD_MAX) + carry = 0; + z[j] = x_j; + } + } + +/* +* Two Operand Linear Multiply +*/ +void bigint_linmul2(word x[], u32bit x_size, word y) + { + const u32bit blocks = x_size - (x_size % 8); + + word carry = 0; + + for(u32bit j = 0; j != blocks; j += 8) + carry = word8_linmul2(x + j, y, carry); + + for(u32bit j = blocks; j != x_size; ++j) + x[j] = word_madd2(x[j], y, &carry); + + x[x_size] = carry; + } + +/* +* Three Operand Linear Multiply +*/ +void bigint_linmul3(word z[], const word x[], u32bit x_size, word y) + { + const u32bit blocks = x_size - (x_size % 8); + + word carry = 0; + + for(u32bit j = 0; j != blocks; j += 8) + carry = word8_linmul3(z + j, x + j, y, carry); + + for(u32bit j = blocks; j != x_size; ++j) + z[j] = word_madd2(x[j], y, &carry); + + z[x_size] = carry; + } + +} + +} diff --git a/botan/src/math/bigint/mp_asm64/info.txt b/botan/src/math/bigint/mp_asm64/info.txt new file mode 100644 index 0000000..5c112c4 --- /dev/null +++ b/botan/src/math/bigint/mp_asm64/info.txt @@ -0,0 +1,27 @@ +realname "MPI Core (Alpha/IA-64/MIPS64/PowerPC-64/SPARC64)" + +mp_bits 64 + +load_on dep + +<add> +mp_asm.h +mp_generic:mp_asmi.h +</add> + +<arch> +#amd64 +alpha +ia64 +mips64 +ppc64 +sparc64 +</arch> + +# The inline asm only works with gcc, but it looks like (at least on +# UltraSPARC), using 64-bit words and the sythensized multiply is a 5 to 25% +# win, so it's probably worth using elsewhere. +<cc> +gcc +sunwspro +</cc> diff --git a/botan/src/math/bigint/mp_asm64/mp_asm.h b/botan/src/math/bigint/mp_asm64/mp_asm.h new file mode 100644 index 0000000..c9159ea --- /dev/null +++ b/botan/src/math/bigint/mp_asm64/mp_asm.h @@ -0,0 +1,119 @@ +/* +* MPI Multiply-Add Core +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_MP_MADD_H__ +#define BOTAN_MP_MADD_H__ + +#include <botan/mp_types.h> + +namespace Botan { + +#if (BOTAN_MP_WORD_BITS != 64) + #error The mp_asm64 module requires that BOTAN_MP_WORD_BITS == 64 +#endif + +#if defined(BOTAN_TARGET_ARCH_IS_ALPHA) + +#define BOTAN_WORD_MUL(a,b,z1,z0) do { \ + asm("umulh %1,%2,%0" : "=r" (z0) : "r" (a), "r" (b)); \ + z1 = a * b; \ +} while(0); + +#elif defined(BOTAN_TARGET_ARCH_IS_AMD64) + +#define BOTAN_WORD_MUL(a,b,z1,z0) do { \ + asm("mulq %3" : "=d" (z0), "=a" (z1) : \ + "a" (a), "rm" (b) : "cc"); \ +} while(0); + +#elif defined(BOTAN_TARGET_ARCH_IS_IA64) + +#define BOTAN_WORD_MUL(a,b,z1,z0) do { \ + asm("xmpy.hu %0=%1,%2" : "=f" (z0) : "f" (a), "f" (b)); \ + z1 = a * b; \ +} while(0); + +#elif defined(BOTAN_TARGET_ARCH_IS_PPC64) + +#define BOTAN_WORD_MUL(a,b,z1,z0) do { \ + asm("mulhdu %0,%1,%2" : "=r" (z0) : "r" (a), "r" (b) : "cc"); \ + z1 = a * b; \ +} while(0); + +#elif defined(BOTAN_TARGET_ARCH_IS_MIPS64) + +#define BOTAN_WORD_MUL(a,b,z1,z0) do { \ + asm("dmultu %2,%3" : "=h" (z0), "=l" (z1) : "r" (a), "r" (b)); \ +} while(0); + +#else + +// Do a 64x64->128 multiply using four 64x64->64 multiplies +// plus some adds and shifts. Last resort for CPUs like UltraSPARC, +// with 64-bit registers/ALU, but no 64x64->128 multiply. +inline void bigint_2word_mul(word a, word b, word* z1, word* z0) + { + const u32bit MP_HWORD_BITS = BOTAN_MP_WORD_BITS / 2; + const word MP_HWORD_MASK = ((word)1 << MP_HWORD_BITS) - 1; + + const word a_hi = (a >> MP_HWORD_BITS); + const word a_lo = (a & MP_HWORD_MASK); + const word b_hi = (b >> MP_HWORD_BITS); + const word b_lo = (b & MP_HWORD_MASK); + + word x0 = a_hi * b_hi; + word x1 = a_lo * b_hi; + word x2 = a_hi * b_lo; + word x3 = a_lo * b_lo; + + x2 += x3 >> (MP_HWORD_BITS); + x2 += x1; + if(x2 < x1) + x0 += ((word)1 << MP_HWORD_BITS); + + *z0 = x0 + (x2 >> MP_HWORD_BITS); + *z1 = ((x2 & MP_HWORD_MASK) << MP_HWORD_BITS) + (x3 & MP_HWORD_MASK); + } + +#define BOTAN_WORD_MUL(a,b,z1,z0) bigint_2word_mul(a, b, &z1, &z0) + +#endif + +/* +* Word Multiply/Add +*/ +inline word word_madd2(word a, word b, word* c) + { + word z0 = 0, z1 = 0; + + BOTAN_WORD_MUL(a, b, z1, z0); + + z1 += *c; if(z1 < *c) z0++; + + *c = z0; + return z1; + } + +/* +* Word Multiply/Add +*/ +inline word word_madd3(word a, word b, word c, word* d) + { + word z0 = 0, z1 = 0; + + BOTAN_WORD_MUL(a, b, z1, z0); + + z1 += c; if(z1 < c) z0++; + z1 += *d; if(z1 < *d) z0++; + + *d = z0; + return z1; + } + +} + +#endif diff --git a/botan/src/math/bigint/mp_comba.cpp b/botan/src/math/bigint/mp_comba.cpp new file mode 100644 index 0000000..218038d --- /dev/null +++ b/botan/src/math/bigint/mp_comba.cpp @@ -0,0 +1,920 @@ +/* +* Comba Multiplication and Squaring +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/mp_core.h> +#include <botan/mp_asmi.h> + +namespace Botan { + +extern "C" { + +/* +* Comba 4x4 Squaring +*/ +void bigint_comba_sqr4(word z[8], const word x[4]) + { + word w2 = 0, w1 = 0, w0 = 0; + + word3_muladd(&w2, &w1, &w0, x[0], x[0]); + z[0] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[0], x[1]); + z[1] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[0], x[2]); + word3_muladd(&w2, &w1, &w0, x[1], x[1]); + z[2] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[0], x[3]); + word3_muladd_2(&w2, &w1, &w0, x[1], x[2]); + z[3] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[1], x[3]); + word3_muladd(&w2, &w1, &w0, x[2], x[2]); + z[4] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[2], x[3]); + z[5] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[3], x[3]); + z[6] = w0; + z[7] = w1; + } + +/* +* Comba 4x4 Multiplication +*/ +void bigint_comba_mul4(word z[8], const word x[4], const word y[4]) + { + word w2 = 0, w1 = 0, w0 = 0; + + word3_muladd(&w2, &w1, &w0, x[0], y[0]); + z[0] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[0], y[1]); + word3_muladd(&w2, &w1, &w0, x[1], y[0]); + z[1] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[0], y[2]); + word3_muladd(&w2, &w1, &w0, x[1], y[1]); + word3_muladd(&w2, &w1, &w0, x[2], y[0]); + z[2] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[0], y[3]); + word3_muladd(&w2, &w1, &w0, x[1], y[2]); + word3_muladd(&w2, &w1, &w0, x[2], y[1]); + word3_muladd(&w2, &w1, &w0, x[3], y[0]); + z[3] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[1], y[3]); + word3_muladd(&w2, &w1, &w0, x[2], y[2]); + word3_muladd(&w2, &w1, &w0, x[3], y[1]); + z[4] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[2], y[3]); + word3_muladd(&w2, &w1, &w0, x[3], y[2]); + z[5] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[3], y[3]); + z[6] = w0; + z[7] = w1; + } + +/* +* Comba 6x6 Squaring +*/ +void bigint_comba_sqr6(word z[12], const word x[6]) + { + word w2 = 0, w1 = 0, w0 = 0; + + word3_muladd(&w2, &w1, &w0, x[0], x[0]); + z[0] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[0], x[1]); + z[1] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[0], x[2]); + word3_muladd(&w2, &w1, &w0, x[1], x[1]); + z[2] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[0], x[3]); + word3_muladd_2(&w2, &w1, &w0, x[1], x[2]); + z[3] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[0], x[4]); + word3_muladd_2(&w2, &w1, &w0, x[1], x[3]); + word3_muladd(&w2, &w1, &w0, x[2], x[2]); + z[4] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[0], x[5]); + word3_muladd_2(&w2, &w1, &w0, x[1], x[4]); + word3_muladd_2(&w2, &w1, &w0, x[2], x[3]); + z[5] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[1], x[5]); + word3_muladd_2(&w2, &w1, &w0, x[2], x[4]); + word3_muladd(&w2, &w1, &w0, x[3], x[3]); + z[6] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[2], x[5]); + word3_muladd_2(&w2, &w1, &w0, x[3], x[4]); + z[7] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[3], x[5]); + word3_muladd(&w2, &w1, &w0, x[4], x[4]); + z[8] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[4], x[5]); + z[9] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[5], x[5]); + z[10] = w0; + z[11] = w1; + } + +/* +* Comba 6x6 Multiplication +*/ +void bigint_comba_mul6(word z[12], const word x[6], const word y[6]) + { + word w2 = 0, w1 = 0, w0 = 0; + + word3_muladd(&w2, &w1, &w0, x[0], y[0]); + z[0] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[0], y[1]); + word3_muladd(&w2, &w1, &w0, x[1], y[0]); + z[1] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[0], y[2]); + word3_muladd(&w2, &w1, &w0, x[1], y[1]); + word3_muladd(&w2, &w1, &w0, x[2], y[0]); + z[2] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[0], y[3]); + word3_muladd(&w2, &w1, &w0, x[1], y[2]); + word3_muladd(&w2, &w1, &w0, x[2], y[1]); + word3_muladd(&w2, &w1, &w0, x[3], y[0]); + z[3] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[0], y[4]); + word3_muladd(&w2, &w1, &w0, x[1], y[3]); + word3_muladd(&w2, &w1, &w0, x[2], y[2]); + word3_muladd(&w2, &w1, &w0, x[3], y[1]); + word3_muladd(&w2, &w1, &w0, x[4], y[0]); + z[4] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[0], y[5]); + word3_muladd(&w2, &w1, &w0, x[1], y[4]); + word3_muladd(&w2, &w1, &w0, x[2], y[3]); + word3_muladd(&w2, &w1, &w0, x[3], y[2]); + word3_muladd(&w2, &w1, &w0, x[4], y[1]); + word3_muladd(&w2, &w1, &w0, x[5], y[0]); + z[5] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[1], y[5]); + word3_muladd(&w2, &w1, &w0, x[2], y[4]); + word3_muladd(&w2, &w1, &w0, x[3], y[3]); + word3_muladd(&w2, &w1, &w0, x[4], y[2]); + word3_muladd(&w2, &w1, &w0, x[5], y[1]); + z[6] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[2], y[5]); + word3_muladd(&w2, &w1, &w0, x[3], y[4]); + word3_muladd(&w2, &w1, &w0, x[4], y[3]); + word3_muladd(&w2, &w1, &w0, x[5], y[2]); + z[7] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[3], y[5]); + word3_muladd(&w2, &w1, &w0, x[4], y[4]); + word3_muladd(&w2, &w1, &w0, x[5], y[3]); + z[8] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[4], y[5]); + word3_muladd(&w2, &w1, &w0, x[5], y[4]); + z[9] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[5], y[5]); + z[10] = w0; + z[11] = w1; + } + +/* +* Comba 8x8 Squaring +*/ +void bigint_comba_sqr8(word z[16], const word x[8]) + { + word w2 = 0, w1 = 0, w0 = 0; + + word3_muladd(&w2, &w1, &w0, x[0], x[0]); + z[0] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[0], x[1]); + z[1] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[0], x[2]); + word3_muladd(&w2, &w1, &w0, x[1], x[1]); + z[2] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[0], x[3]); + word3_muladd_2(&w2, &w1, &w0, x[1], x[2]); + z[3] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[0], x[4]); + word3_muladd_2(&w2, &w1, &w0, x[1], x[3]); + word3_muladd(&w2, &w1, &w0, x[2], x[2]); + z[4] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[0], x[5]); + word3_muladd_2(&w2, &w1, &w0, x[1], x[4]); + word3_muladd_2(&w2, &w1, &w0, x[2], x[3]); + z[5] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[0], x[6]); + word3_muladd_2(&w2, &w1, &w0, x[1], x[5]); + word3_muladd_2(&w2, &w1, &w0, x[2], x[4]); + word3_muladd(&w2, &w1, &w0, x[3], x[3]); + z[6] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[0], x[7]); + word3_muladd_2(&w2, &w1, &w0, x[1], x[6]); + word3_muladd_2(&w2, &w1, &w0, x[2], x[5]); + word3_muladd_2(&w2, &w1, &w0, x[3], x[4]); + z[7] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[1], x[7]); + word3_muladd_2(&w2, &w1, &w0, x[2], x[6]); + word3_muladd_2(&w2, &w1, &w0, x[3], x[5]); + word3_muladd(&w2, &w1, &w0, x[4], x[4]); + z[8] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[2], x[7]); + word3_muladd_2(&w2, &w1, &w0, x[3], x[6]); + word3_muladd_2(&w2, &w1, &w0, x[4], x[5]); + z[9] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[3], x[7]); + word3_muladd_2(&w2, &w1, &w0, x[4], x[6]); + word3_muladd(&w2, &w1, &w0, x[5], x[5]); + z[10] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[4], x[7]); + word3_muladd_2(&w2, &w1, &w0, x[5], x[6]); + z[11] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[5], x[7]); + word3_muladd(&w2, &w1, &w0, x[6], x[6]); + z[12] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[6], x[7]); + z[13] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[7], x[7]); + z[14] = w0; + z[15] = w1; + } + +/* +* Comba 8x8 Multiplication +*/ +void bigint_comba_mul8(word z[16], const word x[8], const word y[8]) + { + word w2 = 0, w1 = 0, w0 = 0; + + word3_muladd(&w2, &w1, &w0, x[0], y[0]); + z[0] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[0], y[1]); + word3_muladd(&w2, &w1, &w0, x[1], y[0]); + z[1] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[0], y[2]); + word3_muladd(&w2, &w1, &w0, x[1], y[1]); + word3_muladd(&w2, &w1, &w0, x[2], y[0]); + z[2] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[0], y[3]); + word3_muladd(&w2, &w1, &w0, x[1], y[2]); + word3_muladd(&w2, &w1, &w0, x[2], y[1]); + word3_muladd(&w2, &w1, &w0, x[3], y[0]); + z[3] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[0], y[4]); + word3_muladd(&w2, &w1, &w0, x[1], y[3]); + word3_muladd(&w2, &w1, &w0, x[2], y[2]); + word3_muladd(&w2, &w1, &w0, x[3], y[1]); + word3_muladd(&w2, &w1, &w0, x[4], y[0]); + z[4] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[0], y[5]); + word3_muladd(&w2, &w1, &w0, x[1], y[4]); + word3_muladd(&w2, &w1, &w0, x[2], y[3]); + word3_muladd(&w2, &w1, &w0, x[3], y[2]); + word3_muladd(&w2, &w1, &w0, x[4], y[1]); + word3_muladd(&w2, &w1, &w0, x[5], y[0]); + z[5] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[0], y[6]); + word3_muladd(&w2, &w1, &w0, x[1], y[5]); + word3_muladd(&w2, &w1, &w0, x[2], y[4]); + word3_muladd(&w2, &w1, &w0, x[3], y[3]); + word3_muladd(&w2, &w1, &w0, x[4], y[2]); + word3_muladd(&w2, &w1, &w0, x[5], y[1]); + word3_muladd(&w2, &w1, &w0, x[6], y[0]); + z[6] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[0], y[7]); + word3_muladd(&w2, &w1, &w0, x[1], y[6]); + word3_muladd(&w2, &w1, &w0, x[2], y[5]); + word3_muladd(&w2, &w1, &w0, x[3], y[4]); + word3_muladd(&w2, &w1, &w0, x[4], y[3]); + word3_muladd(&w2, &w1, &w0, x[5], y[2]); + word3_muladd(&w2, &w1, &w0, x[6], y[1]); + word3_muladd(&w2, &w1, &w0, x[7], y[0]); + z[7] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[1], y[7]); + word3_muladd(&w2, &w1, &w0, x[2], y[6]); + word3_muladd(&w2, &w1, &w0, x[3], y[5]); + word3_muladd(&w2, &w1, &w0, x[4], y[4]); + word3_muladd(&w2, &w1, &w0, x[5], y[3]); + word3_muladd(&w2, &w1, &w0, x[6], y[2]); + word3_muladd(&w2, &w1, &w0, x[7], y[1]); + z[8] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[2], y[7]); + word3_muladd(&w2, &w1, &w0, x[3], y[6]); + word3_muladd(&w2, &w1, &w0, x[4], y[5]); + word3_muladd(&w2, &w1, &w0, x[5], y[4]); + word3_muladd(&w2, &w1, &w0, x[6], y[3]); + word3_muladd(&w2, &w1, &w0, x[7], y[2]); + z[9] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[3], y[7]); + word3_muladd(&w2, &w1, &w0, x[4], y[6]); + word3_muladd(&w2, &w1, &w0, x[5], y[5]); + word3_muladd(&w2, &w1, &w0, x[6], y[4]); + word3_muladd(&w2, &w1, &w0, x[7], y[3]); + z[10] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[4], y[7]); + word3_muladd(&w2, &w1, &w0, x[5], y[6]); + word3_muladd(&w2, &w1, &w0, x[6], y[5]); + word3_muladd(&w2, &w1, &w0, x[7], y[4]); + z[11] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[5], y[7]); + word3_muladd(&w2, &w1, &w0, x[6], y[6]); + word3_muladd(&w2, &w1, &w0, x[7], y[5]); + z[12] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[6], y[7]); + word3_muladd(&w2, &w1, &w0, x[7], y[6]); + z[13] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[7], y[7]); + z[14] = w0; + z[15] = w1; + } + +/* +* Comba 16x16 Squaring +*/ +void bigint_comba_sqr16(word z[32], const word x[16]) + { + word w2 = 0, w1 = 0, w0 = 0; + + word3_muladd(&w2, &w1, &w0, x[ 0], x[ 0]); + z[ 0] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[ 0], x[ 1]); + z[ 1] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[ 0], x[ 2]); + word3_muladd(&w2, &w1, &w0, x[ 1], x[ 1]); + z[ 2] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[ 0], x[ 3]); + word3_muladd_2(&w2, &w1, &w0, x[ 1], x[ 2]); + z[ 3] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[ 0], x[ 4]); + word3_muladd_2(&w2, &w1, &w0, x[ 1], x[ 3]); + word3_muladd(&w2, &w1, &w0, x[ 2], x[ 2]); + z[ 4] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[ 0], x[ 5]); + word3_muladd_2(&w2, &w1, &w0, x[ 1], x[ 4]); + word3_muladd_2(&w2, &w1, &w0, x[ 2], x[ 3]); + z[ 5] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[ 0], x[ 6]); + word3_muladd_2(&w2, &w1, &w0, x[ 1], x[ 5]); + word3_muladd_2(&w2, &w1, &w0, x[ 2], x[ 4]); + word3_muladd(&w2, &w1, &w0, x[ 3], x[ 3]); + z[ 6] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[ 0], x[ 7]); + word3_muladd_2(&w2, &w1, &w0, x[ 1], x[ 6]); + word3_muladd_2(&w2, &w1, &w0, x[ 2], x[ 5]); + word3_muladd_2(&w2, &w1, &w0, x[ 3], x[ 4]); + z[ 7] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[ 0], x[ 8]); + word3_muladd_2(&w2, &w1, &w0, x[ 1], x[ 7]); + word3_muladd_2(&w2, &w1, &w0, x[ 2], x[ 6]); + word3_muladd_2(&w2, &w1, &w0, x[ 3], x[ 5]); + word3_muladd(&w2, &w1, &w0, x[ 4], x[ 4]); + z[ 8] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[ 0], x[ 9]); + word3_muladd_2(&w2, &w1, &w0, x[ 1], x[ 8]); + word3_muladd_2(&w2, &w1, &w0, x[ 2], x[ 7]); + word3_muladd_2(&w2, &w1, &w0, x[ 3], x[ 6]); + word3_muladd_2(&w2, &w1, &w0, x[ 4], x[ 5]); + z[ 9] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[ 0], x[10]); + word3_muladd_2(&w2, &w1, &w0, x[ 1], x[ 9]); + word3_muladd_2(&w2, &w1, &w0, x[ 2], x[ 8]); + word3_muladd_2(&w2, &w1, &w0, x[ 3], x[ 7]); + word3_muladd_2(&w2, &w1, &w0, x[ 4], x[ 6]); + word3_muladd(&w2, &w1, &w0, x[ 5], x[ 5]); + z[10] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[ 0], x[11]); + word3_muladd_2(&w2, &w1, &w0, x[ 1], x[10]); + word3_muladd_2(&w2, &w1, &w0, x[ 2], x[ 9]); + word3_muladd_2(&w2, &w1, &w0, x[ 3], x[ 8]); + word3_muladd_2(&w2, &w1, &w0, x[ 4], x[ 7]); + word3_muladd_2(&w2, &w1, &w0, x[ 5], x[ 6]); + z[11] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[ 0], x[12]); + word3_muladd_2(&w2, &w1, &w0, x[ 1], x[11]); + word3_muladd_2(&w2, &w1, &w0, x[ 2], x[10]); + word3_muladd_2(&w2, &w1, &w0, x[ 3], x[ 9]); + word3_muladd_2(&w2, &w1, &w0, x[ 4], x[ 8]); + word3_muladd_2(&w2, &w1, &w0, x[ 5], x[ 7]); + word3_muladd(&w2, &w1, &w0, x[ 6], x[ 6]); + z[12] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[ 0], x[13]); + word3_muladd_2(&w2, &w1, &w0, x[ 1], x[12]); + word3_muladd_2(&w2, &w1, &w0, x[ 2], x[11]); + word3_muladd_2(&w2, &w1, &w0, x[ 3], x[10]); + word3_muladd_2(&w2, &w1, &w0, x[ 4], x[ 9]); + word3_muladd_2(&w2, &w1, &w0, x[ 5], x[ 8]); + word3_muladd_2(&w2, &w1, &w0, x[ 6], x[ 7]); + z[13] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[ 0], x[14]); + word3_muladd_2(&w2, &w1, &w0, x[ 1], x[13]); + word3_muladd_2(&w2, &w1, &w0, x[ 2], x[12]); + word3_muladd_2(&w2, &w1, &w0, x[ 3], x[11]); + word3_muladd_2(&w2, &w1, &w0, x[ 4], x[10]); + word3_muladd_2(&w2, &w1, &w0, x[ 5], x[ 9]); + word3_muladd_2(&w2, &w1, &w0, x[ 6], x[ 8]); + word3_muladd(&w2, &w1, &w0, x[ 7], x[ 7]); + z[14] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[ 0], x[15]); + word3_muladd_2(&w2, &w1, &w0, x[ 1], x[14]); + word3_muladd_2(&w2, &w1, &w0, x[ 2], x[13]); + word3_muladd_2(&w2, &w1, &w0, x[ 3], x[12]); + word3_muladd_2(&w2, &w1, &w0, x[ 4], x[11]); + word3_muladd_2(&w2, &w1, &w0, x[ 5], x[10]); + word3_muladd_2(&w2, &w1, &w0, x[ 6], x[ 9]); + word3_muladd_2(&w2, &w1, &w0, x[ 7], x[ 8]); + z[15] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[ 1], x[15]); + word3_muladd_2(&w2, &w1, &w0, x[ 2], x[14]); + word3_muladd_2(&w2, &w1, &w0, x[ 3], x[13]); + word3_muladd_2(&w2, &w1, &w0, x[ 4], x[12]); + word3_muladd_2(&w2, &w1, &w0, x[ 5], x[11]); + word3_muladd_2(&w2, &w1, &w0, x[ 6], x[10]); + word3_muladd_2(&w2, &w1, &w0, x[ 7], x[ 9]); + word3_muladd(&w2, &w1, &w0, x[ 8], x[ 8]); + z[16] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[ 2], x[15]); + word3_muladd_2(&w2, &w1, &w0, x[ 3], x[14]); + word3_muladd_2(&w2, &w1, &w0, x[ 4], x[13]); + word3_muladd_2(&w2, &w1, &w0, x[ 5], x[12]); + word3_muladd_2(&w2, &w1, &w0, x[ 6], x[11]); + word3_muladd_2(&w2, &w1, &w0, x[ 7], x[10]); + word3_muladd_2(&w2, &w1, &w0, x[ 8], x[ 9]); + z[17] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[ 3], x[15]); + word3_muladd_2(&w2, &w1, &w0, x[ 4], x[14]); + word3_muladd_2(&w2, &w1, &w0, x[ 5], x[13]); + word3_muladd_2(&w2, &w1, &w0, x[ 6], x[12]); + word3_muladd_2(&w2, &w1, &w0, x[ 7], x[11]); + word3_muladd_2(&w2, &w1, &w0, x[ 8], x[10]); + word3_muladd(&w2, &w1, &w0, x[ 9], x[ 9]); + z[18] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[ 4], x[15]); + word3_muladd_2(&w2, &w1, &w0, x[ 5], x[14]); + word3_muladd_2(&w2, &w1, &w0, x[ 6], x[13]); + word3_muladd_2(&w2, &w1, &w0, x[ 7], x[12]); + word3_muladd_2(&w2, &w1, &w0, x[ 8], x[11]); + word3_muladd_2(&w2, &w1, &w0, x[ 9], x[10]); + z[19] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[ 5], x[15]); + word3_muladd_2(&w2, &w1, &w0, x[ 6], x[14]); + word3_muladd_2(&w2, &w1, &w0, x[ 7], x[13]); + word3_muladd_2(&w2, &w1, &w0, x[ 8], x[12]); + word3_muladd_2(&w2, &w1, &w0, x[ 9], x[11]); + word3_muladd(&w2, &w1, &w0, x[10], x[10]); + z[20] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[ 6], x[15]); + word3_muladd_2(&w2, &w1, &w0, x[ 7], x[14]); + word3_muladd_2(&w2, &w1, &w0, x[ 8], x[13]); + word3_muladd_2(&w2, &w1, &w0, x[ 9], x[12]); + word3_muladd_2(&w2, &w1, &w0, x[10], x[11]); + z[21] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[ 7], x[15]); + word3_muladd_2(&w2, &w1, &w0, x[ 8], x[14]); + word3_muladd_2(&w2, &w1, &w0, x[ 9], x[13]); + word3_muladd_2(&w2, &w1, &w0, x[10], x[12]); + word3_muladd(&w2, &w1, &w0, x[11], x[11]); + z[22] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[ 8], x[15]); + word3_muladd_2(&w2, &w1, &w0, x[ 9], x[14]); + word3_muladd_2(&w2, &w1, &w0, x[10], x[13]); + word3_muladd_2(&w2, &w1, &w0, x[11], x[12]); + z[23] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[ 9], x[15]); + word3_muladd_2(&w2, &w1, &w0, x[10], x[14]); + word3_muladd_2(&w2, &w1, &w0, x[11], x[13]); + word3_muladd(&w2, &w1, &w0, x[12], x[12]); + z[24] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[10], x[15]); + word3_muladd_2(&w2, &w1, &w0, x[11], x[14]); + word3_muladd_2(&w2, &w1, &w0, x[12], x[13]); + z[25] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[11], x[15]); + word3_muladd_2(&w2, &w1, &w0, x[12], x[14]); + word3_muladd(&w2, &w1, &w0, x[13], x[13]); + z[26] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[12], x[15]); + word3_muladd_2(&w2, &w1, &w0, x[13], x[14]); + z[27] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[13], x[15]); + word3_muladd(&w2, &w1, &w0, x[14], x[14]); + z[28] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd_2(&w2, &w1, &w0, x[14], x[15]); + z[29] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[15], x[15]); + z[30] = w0; + z[31] = w1; + } + +/* +* Comba 16x16 Multiplication +*/ +void bigint_comba_mul16(word z[32], const word x[16], const word y[16]) + { + word w2 = 0, w1 = 0, w0 = 0; + + word3_muladd(&w2, &w1, &w0, x[ 0], y[ 0]); + z[0] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[ 0], y[ 1]); + word3_muladd(&w2, &w1, &w0, x[ 1], y[ 0]); + z[1] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[ 0], y[ 2]); + word3_muladd(&w2, &w1, &w0, x[ 1], y[ 1]); + word3_muladd(&w2, &w1, &w0, x[ 2], y[ 0]); + z[2] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[ 0], y[ 3]); + word3_muladd(&w2, &w1, &w0, x[ 1], y[ 2]); + word3_muladd(&w2, &w1, &w0, x[ 2], y[ 1]); + word3_muladd(&w2, &w1, &w0, x[ 3], y[ 0]); + z[3] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[ 0], y[ 4]); + word3_muladd(&w2, &w1, &w0, x[ 1], y[ 3]); + word3_muladd(&w2, &w1, &w0, x[ 2], y[ 2]); + word3_muladd(&w2, &w1, &w0, x[ 3], y[ 1]); + word3_muladd(&w2, &w1, &w0, x[ 4], y[ 0]); + z[4] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[ 0], y[ 5]); + word3_muladd(&w2, &w1, &w0, x[ 1], y[ 4]); + word3_muladd(&w2, &w1, &w0, x[ 2], y[ 3]); + word3_muladd(&w2, &w1, &w0, x[ 3], y[ 2]); + word3_muladd(&w2, &w1, &w0, x[ 4], y[ 1]); + word3_muladd(&w2, &w1, &w0, x[ 5], y[ 0]); + z[5] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[ 0], y[ 6]); + word3_muladd(&w2, &w1, &w0, x[ 1], y[ 5]); + word3_muladd(&w2, &w1, &w0, x[ 2], y[ 4]); + word3_muladd(&w2, &w1, &w0, x[ 3], y[ 3]); + word3_muladd(&w2, &w1, &w0, x[ 4], y[ 2]); + word3_muladd(&w2, &w1, &w0, x[ 5], y[ 1]); + word3_muladd(&w2, &w1, &w0, x[ 6], y[ 0]); + z[6] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[ 0], y[ 7]); + word3_muladd(&w2, &w1, &w0, x[ 1], y[ 6]); + word3_muladd(&w2, &w1, &w0, x[ 2], y[ 5]); + word3_muladd(&w2, &w1, &w0, x[ 3], y[ 4]); + word3_muladd(&w2, &w1, &w0, x[ 4], y[ 3]); + word3_muladd(&w2, &w1, &w0, x[ 5], y[ 2]); + word3_muladd(&w2, &w1, &w0, x[ 6], y[ 1]); + word3_muladd(&w2, &w1, &w0, x[ 7], y[ 0]); + z[7] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[ 0], y[ 8]); + word3_muladd(&w2, &w1, &w0, x[ 1], y[ 7]); + word3_muladd(&w2, &w1, &w0, x[ 2], y[ 6]); + word3_muladd(&w2, &w1, &w0, x[ 3], y[ 5]); + word3_muladd(&w2, &w1, &w0, x[ 4], y[ 4]); + word3_muladd(&w2, &w1, &w0, x[ 5], y[ 3]); + word3_muladd(&w2, &w1, &w0, x[ 6], y[ 2]); + word3_muladd(&w2, &w1, &w0, x[ 7], y[ 1]); + word3_muladd(&w2, &w1, &w0, x[ 8], y[ 0]); + z[8] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[ 0], y[ 9]); + word3_muladd(&w2, &w1, &w0, x[ 1], y[ 8]); + word3_muladd(&w2, &w1, &w0, x[ 2], y[ 7]); + word3_muladd(&w2, &w1, &w0, x[ 3], y[ 6]); + word3_muladd(&w2, &w1, &w0, x[ 4], y[ 5]); + word3_muladd(&w2, &w1, &w0, x[ 5], y[ 4]); + word3_muladd(&w2, &w1, &w0, x[ 6], y[ 3]); + word3_muladd(&w2, &w1, &w0, x[ 7], y[ 2]); + word3_muladd(&w2, &w1, &w0, x[ 8], y[ 1]); + word3_muladd(&w2, &w1, &w0, x[ 9], y[ 0]); + z[9] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[ 0], y[10]); + word3_muladd(&w2, &w1, &w0, x[ 1], y[ 9]); + word3_muladd(&w2, &w1, &w0, x[ 2], y[ 8]); + word3_muladd(&w2, &w1, &w0, x[ 3], y[ 7]); + word3_muladd(&w2, &w1, &w0, x[ 4], y[ 6]); + word3_muladd(&w2, &w1, &w0, x[ 5], y[ 5]); + word3_muladd(&w2, &w1, &w0, x[ 6], y[ 4]); + word3_muladd(&w2, &w1, &w0, x[ 7], y[ 3]); + word3_muladd(&w2, &w1, &w0, x[ 8], y[ 2]); + word3_muladd(&w2, &w1, &w0, x[ 9], y[ 1]); + word3_muladd(&w2, &w1, &w0, x[10], y[ 0]); + z[10] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[ 0], y[11]); + word3_muladd(&w2, &w1, &w0, x[ 1], y[10]); + word3_muladd(&w2, &w1, &w0, x[ 2], y[ 9]); + word3_muladd(&w2, &w1, &w0, x[ 3], y[ 8]); + word3_muladd(&w2, &w1, &w0, x[ 4], y[ 7]); + word3_muladd(&w2, &w1, &w0, x[ 5], y[ 6]); + word3_muladd(&w2, &w1, &w0, x[ 6], y[ 5]); + word3_muladd(&w2, &w1, &w0, x[ 7], y[ 4]); + word3_muladd(&w2, &w1, &w0, x[ 8], y[ 3]); + word3_muladd(&w2, &w1, &w0, x[ 9], y[ 2]); + word3_muladd(&w2, &w1, &w0, x[10], y[ 1]); + word3_muladd(&w2, &w1, &w0, x[11], y[ 0]); + z[11] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[ 0], y[12]); + word3_muladd(&w2, &w1, &w0, x[ 1], y[11]); + word3_muladd(&w2, &w1, &w0, x[ 2], y[10]); + word3_muladd(&w2, &w1, &w0, x[ 3], y[ 9]); + word3_muladd(&w2, &w1, &w0, x[ 4], y[ 8]); + word3_muladd(&w2, &w1, &w0, x[ 5], y[ 7]); + word3_muladd(&w2, &w1, &w0, x[ 6], y[ 6]); + word3_muladd(&w2, &w1, &w0, x[ 7], y[ 5]); + word3_muladd(&w2, &w1, &w0, x[ 8], y[ 4]); + word3_muladd(&w2, &w1, &w0, x[ 9], y[ 3]); + word3_muladd(&w2, &w1, &w0, x[10], y[ 2]); + word3_muladd(&w2, &w1, &w0, x[11], y[ 1]); + word3_muladd(&w2, &w1, &w0, x[12], y[ 0]); + z[12] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[ 0], y[13]); + word3_muladd(&w2, &w1, &w0, x[ 1], y[12]); + word3_muladd(&w2, &w1, &w0, x[ 2], y[11]); + word3_muladd(&w2, &w1, &w0, x[ 3], y[10]); + word3_muladd(&w2, &w1, &w0, x[ 4], y[ 9]); + word3_muladd(&w2, &w1, &w0, x[ 5], y[ 8]); + word3_muladd(&w2, &w1, &w0, x[ 6], y[ 7]); + word3_muladd(&w2, &w1, &w0, x[ 7], y[ 6]); + word3_muladd(&w2, &w1, &w0, x[ 8], y[ 5]); + word3_muladd(&w2, &w1, &w0, x[ 9], y[ 4]); + word3_muladd(&w2, &w1, &w0, x[10], y[ 3]); + word3_muladd(&w2, &w1, &w0, x[11], y[ 2]); + word3_muladd(&w2, &w1, &w0, x[12], y[ 1]); + word3_muladd(&w2, &w1, &w0, x[13], y[ 0]); + z[13] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[ 0], y[14]); + word3_muladd(&w2, &w1, &w0, x[ 1], y[13]); + word3_muladd(&w2, &w1, &w0, x[ 2], y[12]); + word3_muladd(&w2, &w1, &w0, x[ 3], y[11]); + word3_muladd(&w2, &w1, &w0, x[ 4], y[10]); + word3_muladd(&w2, &w1, &w0, x[ 5], y[ 9]); + word3_muladd(&w2, &w1, &w0, x[ 6], y[ 8]); + word3_muladd(&w2, &w1, &w0, x[ 7], y[ 7]); + word3_muladd(&w2, &w1, &w0, x[ 8], y[ 6]); + word3_muladd(&w2, &w1, &w0, x[ 9], y[ 5]); + word3_muladd(&w2, &w1, &w0, x[10], y[ 4]); + word3_muladd(&w2, &w1, &w0, x[11], y[ 3]); + word3_muladd(&w2, &w1, &w0, x[12], y[ 2]); + word3_muladd(&w2, &w1, &w0, x[13], y[ 1]); + word3_muladd(&w2, &w1, &w0, x[14], y[ 0]); + z[14] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[ 0], y[15]); + word3_muladd(&w2, &w1, &w0, x[ 1], y[14]); + word3_muladd(&w2, &w1, &w0, x[ 2], y[13]); + word3_muladd(&w2, &w1, &w0, x[ 3], y[12]); + word3_muladd(&w2, &w1, &w0, x[ 4], y[11]); + word3_muladd(&w2, &w1, &w0, x[ 5], y[10]); + word3_muladd(&w2, &w1, &w0, x[ 6], y[ 9]); + word3_muladd(&w2, &w1, &w0, x[ 7], y[ 8]); + word3_muladd(&w2, &w1, &w0, x[ 8], y[ 7]); + word3_muladd(&w2, &w1, &w0, x[ 9], y[ 6]); + word3_muladd(&w2, &w1, &w0, x[10], y[ 5]); + word3_muladd(&w2, &w1, &w0, x[11], y[ 4]); + word3_muladd(&w2, &w1, &w0, x[12], y[ 3]); + word3_muladd(&w2, &w1, &w0, x[13], y[ 2]); + word3_muladd(&w2, &w1, &w0, x[14], y[ 1]); + word3_muladd(&w2, &w1, &w0, x[15], y[ 0]); + z[15] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[ 1], y[15]); + word3_muladd(&w2, &w1, &w0, x[ 2], y[14]); + word3_muladd(&w2, &w1, &w0, x[ 3], y[13]); + word3_muladd(&w2, &w1, &w0, x[ 4], y[12]); + word3_muladd(&w2, &w1, &w0, x[ 5], y[11]); + word3_muladd(&w2, &w1, &w0, x[ 6], y[10]); + word3_muladd(&w2, &w1, &w0, x[ 7], y[ 9]); + word3_muladd(&w2, &w1, &w0, x[ 8], y[ 8]); + word3_muladd(&w2, &w1, &w0, x[ 9], y[ 7]); + word3_muladd(&w2, &w1, &w0, x[10], y[ 6]); + word3_muladd(&w2, &w1, &w0, x[11], y[ 5]); + word3_muladd(&w2, &w1, &w0, x[12], y[ 4]); + word3_muladd(&w2, &w1, &w0, x[13], y[ 3]); + word3_muladd(&w2, &w1, &w0, x[14], y[ 2]); + word3_muladd(&w2, &w1, &w0, x[15], y[ 1]); + z[16] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[ 2], y[15]); + word3_muladd(&w2, &w1, &w0, x[ 3], y[14]); + word3_muladd(&w2, &w1, &w0, x[ 4], y[13]); + word3_muladd(&w2, &w1, &w0, x[ 5], y[12]); + word3_muladd(&w2, &w1, &w0, x[ 6], y[11]); + word3_muladd(&w2, &w1, &w0, x[ 7], y[10]); + word3_muladd(&w2, &w1, &w0, x[ 8], y[ 9]); + word3_muladd(&w2, &w1, &w0, x[ 9], y[ 8]); + word3_muladd(&w2, &w1, &w0, x[10], y[ 7]); + word3_muladd(&w2, &w1, &w0, x[11], y[ 6]); + word3_muladd(&w2, &w1, &w0, x[12], y[ 5]); + word3_muladd(&w2, &w1, &w0, x[13], y[ 4]); + word3_muladd(&w2, &w1, &w0, x[14], y[ 3]); + word3_muladd(&w2, &w1, &w0, x[15], y[ 2]); + z[17] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[ 3], y[15]); + word3_muladd(&w2, &w1, &w0, x[ 4], y[14]); + word3_muladd(&w2, &w1, &w0, x[ 5], y[13]); + word3_muladd(&w2, &w1, &w0, x[ 6], y[12]); + word3_muladd(&w2, &w1, &w0, x[ 7], y[11]); + word3_muladd(&w2, &w1, &w0, x[ 8], y[10]); + word3_muladd(&w2, &w1, &w0, x[ 9], y[ 9]); + word3_muladd(&w2, &w1, &w0, x[10], y[ 8]); + word3_muladd(&w2, &w1, &w0, x[11], y[ 7]); + word3_muladd(&w2, &w1, &w0, x[12], y[ 6]); + word3_muladd(&w2, &w1, &w0, x[13], y[ 5]); + word3_muladd(&w2, &w1, &w0, x[14], y[ 4]); + word3_muladd(&w2, &w1, &w0, x[15], y[ 3]); + z[18] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[ 4], y[15]); + word3_muladd(&w2, &w1, &w0, x[ 5], y[14]); + word3_muladd(&w2, &w1, &w0, x[ 6], y[13]); + word3_muladd(&w2, &w1, &w0, x[ 7], y[12]); + word3_muladd(&w2, &w1, &w0, x[ 8], y[11]); + word3_muladd(&w2, &w1, &w0, x[ 9], y[10]); + word3_muladd(&w2, &w1, &w0, x[10], y[ 9]); + word3_muladd(&w2, &w1, &w0, x[11], y[ 8]); + word3_muladd(&w2, &w1, &w0, x[12], y[ 7]); + word3_muladd(&w2, &w1, &w0, x[13], y[ 6]); + word3_muladd(&w2, &w1, &w0, x[14], y[ 5]); + word3_muladd(&w2, &w1, &w0, x[15], y[ 4]); + z[19] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[ 5], y[15]); + word3_muladd(&w2, &w1, &w0, x[ 6], y[14]); + word3_muladd(&w2, &w1, &w0, x[ 7], y[13]); + word3_muladd(&w2, &w1, &w0, x[ 8], y[12]); + word3_muladd(&w2, &w1, &w0, x[ 9], y[11]); + word3_muladd(&w2, &w1, &w0, x[10], y[10]); + word3_muladd(&w2, &w1, &w0, x[11], y[ 9]); + word3_muladd(&w2, &w1, &w0, x[12], y[ 8]); + word3_muladd(&w2, &w1, &w0, x[13], y[ 7]); + word3_muladd(&w2, &w1, &w0, x[14], y[ 6]); + word3_muladd(&w2, &w1, &w0, x[15], y[ 5]); + z[20] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[ 6], y[15]); + word3_muladd(&w2, &w1, &w0, x[ 7], y[14]); + word3_muladd(&w2, &w1, &w0, x[ 8], y[13]); + word3_muladd(&w2, &w1, &w0, x[ 9], y[12]); + word3_muladd(&w2, &w1, &w0, x[10], y[11]); + word3_muladd(&w2, &w1, &w0, x[11], y[10]); + word3_muladd(&w2, &w1, &w0, x[12], y[ 9]); + word3_muladd(&w2, &w1, &w0, x[13], y[ 8]); + word3_muladd(&w2, &w1, &w0, x[14], y[ 7]); + word3_muladd(&w2, &w1, &w0, x[15], y[ 6]); + z[21] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[ 7], y[15]); + word3_muladd(&w2, &w1, &w0, x[ 8], y[14]); + word3_muladd(&w2, &w1, &w0, x[ 9], y[13]); + word3_muladd(&w2, &w1, &w0, x[10], y[12]); + word3_muladd(&w2, &w1, &w0, x[11], y[11]); + word3_muladd(&w2, &w1, &w0, x[12], y[10]); + word3_muladd(&w2, &w1, &w0, x[13], y[ 9]); + word3_muladd(&w2, &w1, &w0, x[14], y[ 8]); + word3_muladd(&w2, &w1, &w0, x[15], y[ 7]); + z[22] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[ 8], y[15]); + word3_muladd(&w2, &w1, &w0, x[ 9], y[14]); + word3_muladd(&w2, &w1, &w0, x[10], y[13]); + word3_muladd(&w2, &w1, &w0, x[11], y[12]); + word3_muladd(&w2, &w1, &w0, x[12], y[11]); + word3_muladd(&w2, &w1, &w0, x[13], y[10]); + word3_muladd(&w2, &w1, &w0, x[14], y[ 9]); + word3_muladd(&w2, &w1, &w0, x[15], y[ 8]); + z[23] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[ 9], y[15]); + word3_muladd(&w2, &w1, &w0, x[10], y[14]); + word3_muladd(&w2, &w1, &w0, x[11], y[13]); + word3_muladd(&w2, &w1, &w0, x[12], y[12]); + word3_muladd(&w2, &w1, &w0, x[13], y[11]); + word3_muladd(&w2, &w1, &w0, x[14], y[10]); + word3_muladd(&w2, &w1, &w0, x[15], y[ 9]); + z[24] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[10], y[15]); + word3_muladd(&w2, &w1, &w0, x[11], y[14]); + word3_muladd(&w2, &w1, &w0, x[12], y[13]); + word3_muladd(&w2, &w1, &w0, x[13], y[12]); + word3_muladd(&w2, &w1, &w0, x[14], y[11]); + word3_muladd(&w2, &w1, &w0, x[15], y[10]); + z[25] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[11], y[15]); + word3_muladd(&w2, &w1, &w0, x[12], y[14]); + word3_muladd(&w2, &w1, &w0, x[13], y[13]); + word3_muladd(&w2, &w1, &w0, x[14], y[12]); + word3_muladd(&w2, &w1, &w0, x[15], y[11]); + z[26] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[12], y[15]); + word3_muladd(&w2, &w1, &w0, x[13], y[14]); + word3_muladd(&w2, &w1, &w0, x[14], y[13]); + word3_muladd(&w2, &w1, &w0, x[15], y[12]); + z[27] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[13], y[15]); + word3_muladd(&w2, &w1, &w0, x[14], y[14]); + word3_muladd(&w2, &w1, &w0, x[15], y[13]); + z[28] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[14], y[15]); + word3_muladd(&w2, &w1, &w0, x[15], y[14]); + z[29] = w0; w0 = w1; w1 = w2; w2 = 0; + + word3_muladd(&w2, &w1, &w0, x[15], y[15]); + z[30] = w0; + z[31] = w1; + } + +} + +} diff --git a/botan/src/math/bigint/mp_core.h b/botan/src/math/bigint/mp_core.h new file mode 100644 index 0000000..ea27a77 --- /dev/null +++ b/botan/src/math/bigint/mp_core.h @@ -0,0 +1,98 @@ +/* +* MPI Algorithms +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_MP_CORE_H__ +#define BOTAN_MP_CORE_H__ + +#include <botan/mp_types.h> + +namespace Botan { + +/* +* The size of the word type, in bits +*/ +const u32bit MP_WORD_BITS = BOTAN_MP_WORD_BITS; + +extern "C" { + +/* +* Addition/Subtraction Operations +*/ +void bigint_add2(word[], u32bit, const word[], u32bit); +void bigint_add3(word[], const word[], u32bit, const word[], u32bit); + +word bigint_add2_nc(word[], u32bit, const word[], u32bit); +word bigint_add3_nc(word[], const word[], u32bit, const word[], u32bit); + +void bigint_sub2(word[], u32bit, const word[], u32bit); +void bigint_sub3(word[], const word[], u32bit, const word[], u32bit); + +/* +* Shift Operations +*/ +void bigint_shl1(word[], u32bit, u32bit, u32bit); +void bigint_shl2(word[], const word[], u32bit, u32bit, u32bit); +void bigint_shr1(word[], u32bit, u32bit, u32bit); +void bigint_shr2(word[], const word[], u32bit, u32bit, u32bit); + +/* +* Simple O(N^2) Multiplication and Squaring +*/ +void bigint_simple_mul(word z[], const word x[], u32bit x_size, + const word y[], u32bit y_size); +void bigint_simple_sqr(word z[], const word x[], u32bit x_size); + +/* +* Linear Multiply +*/ +void bigint_linmul2(word[], u32bit, word); +void bigint_linmul3(word[], const word[], u32bit, word); +void bigint_linmul_add(word[], u32bit, const word[], u32bit, word); + +/* +* Montgomery Reduction +*/ +void bigint_monty_redc(word[], u32bit, const word[], u32bit, word); + +/* +* Misc Utility Operations +*/ +u32bit bigint_divcore(word, word, word, word, word, word); +s32bit bigint_cmp(const word[], u32bit, const word[], u32bit); +word bigint_divop(word, word, word); +word bigint_modop(word, word, word); +void bigint_wordmul(word, word, word*, word*); + +/* +* Comba Multiplication / Squaring +*/ +void bigint_comba_mul4(word[8], const word[4], const word[4]); +void bigint_comba_mul6(word[12], const word[6], const word[6]); +void bigint_comba_mul8(word[16], const word[8], const word[8]); +void bigint_comba_mul16(word[32], const word[16], const word[16]); + +void bigint_comba_sqr4(word[8], const word[4]); +void bigint_comba_sqr6(word[12], const word[6]); +void bigint_comba_sqr8(word[16], const word[8]); +void bigint_comba_sqr8(word[32], const word[16]); +void bigint_comba_sqr16(word[64], const word[32]); + +} + +/* +* High Level Multiplication/Squaring Interfaces +*/ +void bigint_mul(word[], u32bit, word[], + const word[], u32bit, u32bit, + const word[], u32bit, u32bit); + +void bigint_sqr(word[], u32bit, word[], + const word[], u32bit, u32bit); + +} + +#endif diff --git a/botan/src/math/bigint/mp_generic/info.txt b/botan/src/math/bigint/mp_generic/info.txt new file mode 100644 index 0000000..8bf75fe --- /dev/null +++ b/botan/src/math/bigint/mp_generic/info.txt @@ -0,0 +1,8 @@ +realname "MPI Core (C++)" + +load_on dep + +<add> +mp_asm.h +mp_asmi.h +</add> diff --git a/botan/src/math/bigint/mp_generic/mp_asm.h b/botan/src/math/bigint/mp_generic/mp_asm.h new file mode 100644 index 0000000..7c18343 --- /dev/null +++ b/botan/src/math/bigint/mp_generic/mp_asm.h @@ -0,0 +1,54 @@ +/* +* Lowest Level MPI Algorithms +* (C) 1999-2008 Jack Lloyd +* 2006 Luca Piccarreta +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_MP_ASM_H__ +#define BOTAN_MP_ASM_H__ + +#include <botan/mp_types.h> + +#if (BOTAN_MP_WORD_BITS == 8) + typedef Botan::u16bit dword; +#elif (BOTAN_MP_WORD_BITS == 16) + typedef Botan::u32bit dword; +#elif (BOTAN_MP_WORD_BITS == 32) + typedef Botan::u64bit dword; +#elif (BOTAN_MP_WORD_BITS == 64) + #error BOTAN_MP_WORD_BITS can be 64 only with assembly support +#else + #error BOTAN_MP_WORD_BITS must be 8, 16, 32, or 64 +#endif + +namespace Botan { + +extern "C" { + +/* +* Word Multiply/Add +*/ +inline word word_madd2(word a, word b, word* c) + { + dword z = (dword)a * b + *c; + *c = (word)(z >> BOTAN_MP_WORD_BITS); + return (word)z; + } + +/* +* Word Multiply/Add +*/ +inline word word_madd3(word a, word b, word c, word* d) + { + dword z = (dword)a * b + c + *d; + *d = (word)(z >> BOTAN_MP_WORD_BITS); + return (word)z; + } + +} + +} + +#endif diff --git a/botan/src/math/bigint/mp_generic/mp_asmi.h b/botan/src/math/bigint/mp_generic/mp_asmi.h new file mode 100644 index 0000000..21c4db2 --- /dev/null +++ b/botan/src/math/bigint/mp_generic/mp_asmi.h @@ -0,0 +1,191 @@ +/* +* Lowest Level MPI Algorithms +* (C) 1999-2008 Jack Lloyd +* 2006 Luca Piccarreta +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_MP_ASM_INTERNAL_H__ +#define BOTAN_MP_ASM_INTERNAL_H__ + +#include <botan/mp_asm.h> + +namespace Botan { + +extern "C" { + +/* +* Word Addition +*/ +inline word word_add(word x, word y, word* carry) + { + word z = x + y; + word c1 = (z < x); + z += *carry; + *carry = c1 | (z < *carry); + return z; + } + +/* +* Eight Word Block Addition, Two Argument +*/ +inline word word8_add2(word x[8], const word y[8], word carry) + { + x[0] = word_add(x[0], y[0], &carry); + x[1] = word_add(x[1], y[1], &carry); + x[2] = word_add(x[2], y[2], &carry); + x[3] = word_add(x[3], y[3], &carry); + x[4] = word_add(x[4], y[4], &carry); + x[5] = word_add(x[5], y[5], &carry); + x[6] = word_add(x[6], y[6], &carry); + x[7] = word_add(x[7], y[7], &carry); + return carry; + } + +/* +* Eight Word Block Addition, Three Argument +*/ +inline word word8_add3(word z[8], const word x[8], + const word y[8], word carry) + { + z[0] = word_add(x[0], y[0], &carry); + z[1] = word_add(x[1], y[1], &carry); + z[2] = word_add(x[2], y[2], &carry); + z[3] = word_add(x[3], y[3], &carry); + z[4] = word_add(x[4], y[4], &carry); + z[5] = word_add(x[5], y[5], &carry); + z[6] = word_add(x[6], y[6], &carry); + z[7] = word_add(x[7], y[7], &carry); + return carry; + } + +/* +* Word Subtraction +*/ +inline word word_sub(word x, word y, word* carry) + { + word t0 = x - y; + word c1 = (t0 > x); + word z = t0 - *carry; + *carry = c1 | (z > t0); + return z; + } + +/* +* Eight Word Block Subtraction, Two Argument +*/ +inline word word8_sub2(word x[4], const word y[4], word carry) + { + x[0] = word_sub(x[0], y[0], &carry); + x[1] = word_sub(x[1], y[1], &carry); + x[2] = word_sub(x[2], y[2], &carry); + x[3] = word_sub(x[3], y[3], &carry); + x[4] = word_sub(x[4], y[4], &carry); + x[5] = word_sub(x[5], y[5], &carry); + x[6] = word_sub(x[6], y[6], &carry); + x[7] = word_sub(x[7], y[7], &carry); + return carry; + } + +/* +* Eight Word Block Subtraction, Three Argument +*/ +inline word word8_sub3(word z[8], const word x[8], + const word y[8], word carry) + { + z[0] = word_sub(x[0], y[0], &carry); + z[1] = word_sub(x[1], y[1], &carry); + z[2] = word_sub(x[2], y[2], &carry); + z[3] = word_sub(x[3], y[3], &carry); + z[4] = word_sub(x[4], y[4], &carry); + z[5] = word_sub(x[5], y[5], &carry); + z[6] = word_sub(x[6], y[6], &carry); + z[7] = word_sub(x[7], y[7], &carry); + return carry; + } + +/* +* Eight Word Block Linear Multiplication +*/ +inline word word8_linmul2(word x[4], word y, word carry) + { + x[0] = word_madd2(x[0], y, &carry); + x[1] = word_madd2(x[1], y, &carry); + x[2] = word_madd2(x[2], y, &carry); + x[3] = word_madd2(x[3], y, &carry); + x[4] = word_madd2(x[4], y, &carry); + x[5] = word_madd2(x[5], y, &carry); + x[6] = word_madd2(x[6], y, &carry); + x[7] = word_madd2(x[7], y, &carry); + return carry; + } + +/* +* Eight Word Block Linear Multiplication +*/ +inline word word8_linmul3(word z[8], const word x[8], word y, word carry) + { + z[0] = word_madd2(x[0], y, &carry); + z[1] = word_madd2(x[1], y, &carry); + z[2] = word_madd2(x[2], y, &carry); + z[3] = word_madd2(x[3], y, &carry); + z[4] = word_madd2(x[4], y, &carry); + z[5] = word_madd2(x[5], y, &carry); + z[6] = word_madd2(x[6], y, &carry); + z[7] = word_madd2(x[7], y, &carry); + return carry; + } + +/* +* Eight Word Block Multiply/Add +*/ +inline word word8_madd3(word z[8], const word x[8], word y, word carry) + { + z[0] = word_madd3(x[0], y, z[0], &carry); + z[1] = word_madd3(x[1], y, z[1], &carry); + z[2] = word_madd3(x[2], y, z[2], &carry); + z[3] = word_madd3(x[3], y, z[3], &carry); + z[4] = word_madd3(x[4], y, z[4], &carry); + z[5] = word_madd3(x[5], y, z[5], &carry); + z[6] = word_madd3(x[6], y, z[6], &carry); + z[7] = word_madd3(x[7], y, z[7], &carry); + return carry; + } + +/* +* Multiply-Add Accumulator +*/ +inline void word3_muladd(word* w2, word* w1, word* w0, word a, word b) + { + word carry = *w0; + *w0 = word_madd2(a, b, &carry); + *w1 += carry; + *w2 += (*w1 < carry) ? 1 : 0; + } + +/* +* Multiply-Add Accumulator +*/ +inline void word3_muladd_2(word* w2, word* w1, word* w0, word a, word b) + { + word carry = 0; + a = word_madd2(a, b, &carry); + b = carry; + + word top = (b >> (BOTAN_MP_WORD_BITS-1)); + b <<= 1; + b |= (a >> (BOTAN_MP_WORD_BITS-1)); + a <<= 1; + + carry = 0; + *w0 = word_add(*w0, a, &carry); + *w1 = word_add(*w1, b, &carry); + *w2 = word_add(*w2, top, &carry); + } + +} + +} + +#endif diff --git a/botan/src/math/bigint/mp_ia32/info.txt b/botan/src/math/bigint/mp_ia32/info.txt new file mode 100644 index 0000000..51f98fd --- /dev/null +++ b/botan/src/math/bigint/mp_ia32/info.txt @@ -0,0 +1,19 @@ +realname "MPI Core (IA-32)" + +mp_bits 32 + +load_on asm_ok + +<add> +mp_asm.h +mp_asmi.h +</add> + +<arch> +ia32 +</arch> + +<cc> +gcc +icc +</cc> diff --git a/botan/src/math/bigint/mp_ia32/mp_asm.h b/botan/src/math/bigint/mp_ia32/mp_asm.h new file mode 100644 index 0000000..4d3afc9 --- /dev/null +++ b/botan/src/math/bigint/mp_ia32/mp_asm.h @@ -0,0 +1,67 @@ +/* +* Lowest Level MPI Algorithms +* (C) 1999-2008 Jack Lloyd +* 2006 Luca Piccarreta +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_MP_ASM_H__ +#define BOTAN_MP_ASM_H__ + +#include <botan/mp_types.h> + +#if (BOTAN_MP_WORD_BITS != 32) + #error The mp_ia32 module requires that BOTAN_MP_WORD_BITS == 32 +#endif + +namespace Botan { + +extern "C" { + +/* +* Helper Macros for x86 Assembly +*/ +#define ASM(x) x "\n\t" + +/* +* Word Multiply +*/ +inline word word_madd2(word a, word b, word* c) + { + asm( + ASM("mull %[b]") + ASM("addl %[c],%[a]") + ASM("adcl $0,%[carry]") + + : [a]"=a"(a), [b]"=rm"(b), [carry]"=&d"(*c) + : "0"(a), "1"(b), [c]"g"(*c) : "cc"); + + return a; + } + +/* +* Word Multiply/Add +*/ +inline word word_madd3(word a, word b, word c, word* d) + { + asm( + ASM("mull %[b]") + + ASM("addl %[c],%[a]") + ASM("adcl $0,%[carry]") + + ASM("addl %[d],%[a]") + ASM("adcl $0,%[carry]") + + : [a]"=a"(a), [b]"=rm"(b), [carry]"=&d"(*d) + : "0"(a), "1"(b), [c]"g"(c), [d]"g"(*d) : "cc"); + + return a; + } + +} + +} + +#endif diff --git a/botan/src/math/bigint/mp_ia32/mp_asmi.h b/botan/src/math/bigint/mp_ia32/mp_asmi.h new file mode 100644 index 0000000..28b99ab --- /dev/null +++ b/botan/src/math/bigint/mp_ia32/mp_asmi.h @@ -0,0 +1,235 @@ +/* +* Lowest Level MPI Algorithms +* (C) 1999-2007 Jack Lloyd +* 2006 Luca Piccarreta +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_MP_ASM_INTERNAL_H__ +#define BOTAN_MP_ASM_INTERNAL_H__ + +#include <botan/mp_asm.h> + +namespace Botan { + +extern "C" { + +/* +* Helper Macros for x86 Assembly +*/ +#ifndef ASM + #define ASM(x) x "\n\t" +#endif + +#define ADDSUB2_OP(OPERATION, INDEX) \ + ASM("movl 4*" #INDEX "(%[y]), %[carry]") \ + ASM(OPERATION " %[carry], 4*" #INDEX "(%[x])") \ + +#define ADDSUB3_OP(OPERATION, INDEX) \ + ASM("movl 4*" #INDEX "(%[x]), %[carry]") \ + ASM(OPERATION " 4*" #INDEX "(%[y]), %[carry]") \ + ASM("movl %[carry], 4*" #INDEX "(%[z])") \ + +#define LINMUL_OP(WRITE_TO, INDEX) \ + ASM("movl 4*" #INDEX "(%[x]),%%eax") \ + ASM("mull %[y]") \ + ASM("addl %[carry],%%eax") \ + ASM("adcl $0,%%edx") \ + ASM("movl %%edx,%[carry]") \ + ASM("movl %%eax, 4*" #INDEX "(%[" WRITE_TO "])") + +#define MULADD_OP(IGNORED, INDEX) \ + ASM("movl 4*" #INDEX "(%[x]),%%eax") \ + ASM("mull %[y]") \ + ASM("addl %[carry],%%eax") \ + ASM("adcl $0,%%edx") \ + ASM("addl 4*" #INDEX "(%[z]),%%eax") \ + ASM("adcl $0,%%edx") \ + ASM("movl %%edx,%[carry]") \ + ASM("movl %%eax, 4*" #INDEX " (%[z])") + +#define DO_8_TIMES(MACRO, ARG) \ + MACRO(ARG, 0) \ + MACRO(ARG, 1) \ + MACRO(ARG, 2) \ + MACRO(ARG, 3) \ + MACRO(ARG, 4) \ + MACRO(ARG, 5) \ + MACRO(ARG, 6) \ + MACRO(ARG, 7) + +#define ADD_OR_SUBTRACT(CORE_CODE) \ + ASM("rorl %[carry]") \ + CORE_CODE \ + ASM("sbbl %[carry],%[carry]") \ + ASM("negl %[carry]") + +/* +* Word Addition +*/ +inline word word_add(word x, word y, word* carry) + { +#if 0 + asm( + ADD_OR_SUBTRACT(ASM("adcl %[y],%[x]")) + : [x]"=r"(x), [carry]"=r"(*carry) + : "0"(x), [y]"rm"(y), "1"(*carry) + : "cc"); + return x; +#else + word z = x + y; + word c1 = (z < x); + z += *carry; + *carry = c1 | (z < *carry); + return z; +#endif + } + +/* +* Eight Word Block Addition, Two Argument +*/ +inline word word8_add2(word x[8], const word y[8], word carry) + { + asm( + ADD_OR_SUBTRACT(DO_8_TIMES(ADDSUB2_OP, "adcl")) + : [carry]"=r"(carry) + : [x]"r"(x), [y]"r"(y), "0"(carry) + : "cc", "memory"); + return carry; + } + +/* +* Eight Word Block Addition, Three Argument +*/ +inline word word8_add3(word z[8], const word x[8], const word y[8], word carry) + { + asm( + ADD_OR_SUBTRACT(DO_8_TIMES(ADDSUB3_OP, "adcl")) + : [carry]"=r"(carry) + : [x]"r"(x), [y]"r"(y), [z]"r"(z), "0"(carry) + : "cc", "memory"); + return carry; + } + +/* +* Word Subtraction +*/ +inline word word_sub(word x, word y, word* carry) + { + asm( + ADD_OR_SUBTRACT(ASM("sbbl %[y],%[x]")) + : [x]"=r"(x), [carry]"=r"(*carry) + : "0"(x), [y]"rm"(y), "1"(*carry) + : "cc"); + return x; + } + +/* +* Eight Word Block Subtraction, Two Argument +*/ +inline word word8_sub2(word x[8], const word y[8], word carry) + { + asm( + ADD_OR_SUBTRACT(DO_8_TIMES(ADDSUB2_OP, "sbbl")) + : [carry]"=r"(carry) + : [x]"r"(x), [y]"r"(y), "0"(carry) + : "cc", "memory"); + return carry; + } + +/* +* Eight Word Block Subtraction, Three Argument +*/ +inline word word8_sub3(word z[8], const word x[8], const word y[8], word carry) + { + asm( + ADD_OR_SUBTRACT(DO_8_TIMES(ADDSUB3_OP, "sbbl")) + : [carry]"=r"(carry) + : [x]"r"(x), [y]"r"(y), [z]"r"(z), "0"(carry) + : "cc", "memory"); + return carry; + } + +/* +* Eight Word Block Linear Multiplication +*/ +inline word word8_linmul2(word x[8], word y, word carry) + { + asm( + DO_8_TIMES(LINMUL_OP, "x") + : [carry]"=r"(carry) + : [x]"r"(x), [y]"rm"(y), "0"(carry) + : "cc", "%eax", "%edx"); + return carry; + } + +/* +* Eight Word Block Linear Multiplication +*/ +inline word word8_linmul3(word z[8], const word x[8], word y, word carry) + { + asm( + DO_8_TIMES(LINMUL_OP, "z") + : [carry]"=r"(carry) + : [z]"r"(z), [x]"r"(x), [y]"rm"(y), "0"(carry) + : "cc", "%eax", "%edx"); + return carry; + } + +/* +* Eight Word Block Multiply/Add +*/ +inline word word8_madd3(word z[8], const word x[8], word y, word carry) + { + asm( + DO_8_TIMES(MULADD_OP, "") + : [carry]"=r"(carry) + : [z]"r"(z), [x]"r"(x), [y]"rm"(y), "0"(carry) + : "cc", "%eax", "%edx"); + return carry; + } + +/* +* Multiply-Add Accumulator +*/ +inline void word3_muladd(word* w2, word* w1, word* w0, word x, word y) + { + asm( + ASM("mull %[y]") + + ASM("addl %[x],%[w0]") + ASM("adcl %[y],%[w1]") + ASM("adcl $0,%[w2]") + + : [w0]"=r"(*w0), [w1]"=r"(*w1), [w2]"=r"(*w2) + : [x]"a"(x), [y]"d"(y), "0"(*w0), "1"(*w1), "2"(*w2) + : "cc"); + } + +/* +* Multiply-Add Accumulator +*/ +inline void word3_muladd_2(word* w2, word* w1, word* w0, word x, word y) + { + asm( + ASM("mull %[y]") + + ASM("addl %[x],%[w0]") + ASM("adcl %[y],%[w1]") + ASM("adcl $0,%[w2]") + + ASM("addl %[x],%[w0]") + ASM("adcl %[y],%[w1]") + ASM("adcl $0,%[w2]") + + : [w0]"=r"(*w0), [w1]"=r"(*w1), [w2]"=r"(*w2) + : [x]"a"(x), [y]"d"(y), "0"(*w0), "1"(*w1), "2"(*w2) + : "cc"); + } + +} + +} + +#endif diff --git a/botan/src/math/bigint/mp_ia32_msvc/info.txt b/botan/src/math/bigint/mp_ia32_msvc/info.txt new file mode 100644 index 0000000..9c7ac9b --- /dev/null +++ b/botan/src/math/bigint/mp_ia32_msvc/info.txt @@ -0,0 +1,18 @@ +realname "x86 MPI Assembler Core (MSVC)" + +mp_bits 32 + +load_on dep + +<add> +mp_generic:mp_asm.h +mp_asmi.h +</add> + +<arch> +ia32 +</arch> + +<cc> +msvc +</cc> diff --git a/botan/src/math/bigint/mp_ia32_msvc/mp_asmi.h b/botan/src/math/bigint/mp_ia32_msvc/mp_asmi.h new file mode 100644 index 0000000..33ce6eb --- /dev/null +++ b/botan/src/math/bigint/mp_ia32_msvc/mp_asmi.h @@ -0,0 +1,528 @@ +/* +* Lowest Level MPI Algorithms +* (C) 1999-2006 Jack Lloyd +* 2006 Luca Piccarreta +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_MP_ASM_INTERNAL_H__ +#define BOTAN_MP_ASM_INTERNAL_H__ + +#include <botan/mp_asm.h> + +namespace Botan { + +extern "C" { + +/* +* Word Addition +*/ +inline word word_add(word x, word y, word* carry) + { + word z = x + y; + word c1 = (z < x); + z += *carry; + *carry = c1 | (z < *carry); + return z; + } + +/* +* Eight Word Block Addition, Two Argument +*/ +inline word word8_add2(word x[8], const word y[8], word carry) + { + __asm { + mov edx,[x] + mov esi,[y] + xor eax,eax + sub eax,[carry] //force CF=1 iff *carry==1 + mov eax,[esi] + adc [edx],eax + mov eax,[esi+4] + adc [edx+4],eax + mov eax,[esi+8] + adc [edx+8],eax + mov eax,[esi+12] + adc [edx+12],eax + mov eax,[esi+16] + adc [edx+16],eax + mov eax,[esi+20] + adc [edx+20],eax + mov eax,[esi+24] + adc [edx+24],eax + mov eax,[esi+28] + adc [edx+28],eax + sbb eax,eax + neg eax + } + } + +/* +* Eight Word Block Addition, Three Argument +*/ +inline word word8_add3(word z[8], const word x[8], const word y[8], word carry) + { + __asm { + mov edi,[x] + mov esi,[y] + mov ebx,[z] + xor eax,eax + sub eax,[carry] //force CF=1 iff *carry==1 + mov eax,[edi] + adc eax,[esi] + mov [ebx],eax + + mov eax,[edi+4] + adc eax,[esi+4] + mov [ebx+4],eax + + mov eax,[edi+8] + adc eax,[esi+8] + mov [ebx+8],eax + + mov eax,[edi+12] + adc eax,[esi+12] + mov [ebx+12],eax + + mov eax,[edi+16] + adc eax,[esi+16] + mov [ebx+16],eax + + mov eax,[edi+20] + adc eax,[esi+20] + mov [ebx+20],eax + + mov eax,[edi+24] + adc eax,[esi+24] + mov [ebx+24],eax + + mov eax,[edi+28] + adc eax,[esi+28] + mov [ebx+28],eax + + sbb eax,eax + neg eax + } + } + +/* +* Word Subtraction +*/ +inline word word_sub(word x, word y, word* carry) + { + word t0 = x - y; + word c1 = (t0 > x); + word z = t0 - *carry; + *carry = c1 | (z > t0); + return z; + } + +/* +* Eight Word Block Subtraction, Two Argument +*/ +inline word word8_sub2(word x[8], const word y[8], word carry) + { + _asm { + mov edi,[x] + mov esi,[y] + xor eax,eax + sub eax,[carry] //force CF=1 iff *carry==1 + mov eax,[edi] + sbb eax,[esi] + mov [edi],eax + mov eax,[edi+4] + sbb eax,[esi+4] + mov [edi+4],eax + mov eax,[edi+8] + sbb eax,[esi+8] + mov [edi+8],eax + mov eax,[edi+12] + sbb eax,[esi+12] + mov [edi+12],eax + mov eax,[edi+16] + sbb eax,[esi+16] + mov [edi+16],eax + mov eax,[edi+20] + sbb eax,[esi+20] + mov [edi+20],eax + mov eax,[edi+24] + sbb eax,[esi+24] + mov [edi+24],eax + mov eax,[edi+28] + sbb eax,[esi+28] + mov [edi+28],eax + sbb eax,eax + neg eax + } + } + +/* +* Eight Word Block Subtraction, Three Argument +*/ +inline word word8_sub3(word z[8], const word x[8], + const word y[8], word carry) + { + __asm { + mov edi,[x] + mov esi,[y] + xor eax,eax + sub eax,[carry] //force CF=1 iff *carry==1 + mov ebx,[z] + mov eax,[edi] + sbb eax,[esi] + mov [ebx],eax + mov eax,[edi+4] + sbb eax,[esi+4] + mov [ebx+4],eax + mov eax,[edi+8] + sbb eax,[esi+8] + mov [ebx+8],eax + mov eax,[edi+12] + sbb eax,[esi+12] + mov [ebx+12],eax + mov eax,[edi+16] + sbb eax,[esi+16] + mov [ebx+16],eax + mov eax,[edi+20] + sbb eax,[esi+20] + mov [ebx+20],eax + mov eax,[edi+24] + sbb eax,[esi+24] + mov [ebx+24],eax + mov eax,[edi+28] + sbb eax,[esi+28] + mov [ebx+28],eax + sbb eax,eax + neg eax + } + } + +/* +* Eight Word Block Linear Multiplication +*/ +inline word word8_linmul2(word x[8], word y, word carry) + { + __asm + { + mov esi,[x] + mov eax,[esi] //load a + mul [y] //edx(hi):eax(lo)=a*b + add eax,[carry] //sum lo carry + adc edx,0 //sum hi carry + mov ecx,edx //store carry + mov [esi],eax //load a + + mov eax,[esi+4] //load a + mul [y] //edx(hi):eax(lo)=a*b + add eax,ecx //sum lo carry + adc edx,0 //sum hi carry + mov ecx,edx //store carry + mov [esi+4],eax //load a + + mov eax,[esi+8] //load a + mul [y] //edx(hi):eax(lo)=a*b + add eax,ecx //sum lo carry + adc edx,0 //sum hi carry + mov ecx,edx //store carry + mov [esi+8],eax //load a + + mov eax,[esi+12] //load a + mul [y] //edx(hi):eax(lo)=a*b + add eax,ecx //sum lo carry + adc edx,0 //sum hi carry + mov ecx,edx //store carry + mov [esi+12],eax //load a + + mov eax,[esi+16] //load a + mul [y] //edx(hi):eax(lo)=a*b + add eax,ecx //sum lo carry + adc edx,0 //sum hi carry + mov ecx,edx //store carry + mov [esi+16],eax //load a + + mov eax,[esi+20] //load a + mul [y] //edx(hi):eax(lo)=a*b + add eax,ecx //sum lo carry + adc edx,0 //sum hi carry + mov ecx,edx //store carry + mov [esi+20],eax //load a + + mov eax,[esi+24] //load a + mul [y] //edx(hi):eax(lo)=a*b + add eax,ecx //sum lo carry + adc edx,0 //sum hi carry + mov ecx,edx //store carry + mov [esi+24],eax //load a + + mov eax,[esi+28] //load a + mul [y] //edx(hi):eax(lo)=a*b + add eax,ecx //sum lo carry + adc edx,0 //sum hi carry + mov [esi+28],eax //load a + + mov eax,edx //store carry + } + } + +/* +* Eight Word Block Linear Multiplication +*/ +inline word word8_muladd(word z[8], const word x[8], + word y, word carry) + { + __asm + { + mov esi,[x] + mov ebx,[y] + mov edi,[z] + mov eax,[esi] //load a + mul ebx //edx(hi):eax(lo)=a*b + add eax,[carry] //sum lo carry + adc edx,0 //sum hi carry + add eax,[edi] //sum lo z + adc edx,0 //sum hi z + mov ecx,edx //carry for next block = hi z + mov [edi],eax //save lo z + + mov eax,[esi+4] + mul ebx + add eax,ecx + adc edx,0 + add eax,[edi+4] + adc edx,0 + mov ecx,edx + mov [edi+4],eax + + mov eax,[esi+8] + mul ebx + add eax,ecx + adc edx,0 + add eax,[edi+8] + adc edx,0 + mov ecx,edx + mov [edi+8],eax + + mov eax,[esi+12] + mul ebx + add eax,ecx + adc edx,0 + add eax,[edi+12] + adc edx,0 + mov ecx,edx + mov [edi+12],eax + + mov eax,[esi+16] + mul ebx + add eax,ecx + adc edx,0 + add eax,[edi+16] + adc edx,0 + mov ecx,edx + mov [edi+16],eax + + mov eax,[esi+20] + mul ebx + add eax,ecx + adc edx,0 + add eax,[edi+20] + adc edx,0 + mov ecx,edx + mov [edi+20],eax + + mov eax,[esi+24] + mul ebx + add eax,ecx + adc edx,0 + add eax,[edi+24] + adc edx,0 + mov ecx,edx + mov [edi+24],eax + + mov eax,[esi+28] + mul ebx + add eax,ecx + adc edx,0 + add eax,[edi+28] + adc edx,0 + mov [edi+28],eax + mov eax,edx + } + } + +inline word word8_linmul3(word z[4], const word x[4], word y, word carry) + { + __asm + { +#if 0 + //it's slower!!! + mov edx,[z] + mov eax,[x] + movd mm7,[y] + + movd mm0,[eax] + movd mm1,[eax+4] + movd mm2,[eax+8] + pmuludq mm0,mm7 + pmuludq mm1,mm7 + pmuludq mm2,mm7 + + movd mm6,[carry] + paddq mm0,mm6 + movd [edx],mm0 + + psrlq mm0,32 + paddq mm1,mm0 + movd [edx+4],mm1 + + movd mm3,[eax+12] + psrlq mm1,32 + paddq mm2,mm1 + movd [edx+8],mm2 + + pmuludq mm3,mm7 + movd mm4,[eax+16] + psrlq mm2,32 + paddq mm3,mm2 + movd [edx+12],mm3 + + pmuludq mm4,mm7 + movd mm5,[eax+20] + psrlq mm3,32 + paddq mm4,mm3 + movd [edx+16],mm4 + + pmuludq mm5,mm7 + movd mm0,[eax+24] + psrlq mm4,32 + paddq mm5,mm4 + movd [edx+20],mm5 + + pmuludq mm0,mm7 + movd mm1,[eax+28] + psrlq mm5,32 + paddq mm0,mm5 + movd [edx+24],mm0 + + pmuludq mm1,mm7 + psrlq mm0,32 + paddq mm1,mm0 + movd [edx+28],mm1 + + psrlq mm1,32 + movd eax,mm1 + emms +#else + mov edi,[z] + mov esi,[x] + mov eax,[esi] //load a + mul [y] //edx(hi):eax(lo)=a*b + add eax,[carry] //sum lo carry + adc edx,0 //sum hi carry + mov ecx,edx //store carry + mov [edi],eax //load a + + mov eax,[esi+4] //load a + mul [y] //edx(hi):eax(lo)=a*b + add eax,ecx //sum lo carry + adc edx,0 //sum hi carry + mov ecx,edx //store carry + mov [edi+4],eax //load a + + mov eax,[esi+8] //load a + mul [y] //edx(hi):eax(lo)=a*b + add eax,ecx //sum lo carry + adc edx,0 //sum hi carry + mov ecx,edx //store carry + mov [edi+8],eax //load a + + mov eax,[esi+12] //load a + mul [y] //edx(hi):eax(lo)=a*b + add eax,ecx //sum lo carry + adc edx,0 //sum hi carry + mov ecx,edx //store carry + mov [edi+12],eax //load a + + mov eax,[esi+16] //load a + mul [y] //edx(hi):eax(lo)=a*b + add eax,ecx //sum lo carry + adc edx,0 //sum hi carry + mov ecx,edx //store carry + mov [edi+16],eax //load a + + mov eax,[esi+20] //load a + mul [y] //edx(hi):eax(lo)=a*b + add eax,ecx //sum lo carry + adc edx,0 //sum hi carry + mov ecx,edx //store carry + mov [edi+20],eax //load a + + mov eax,[esi+24] //load a + mul [y] //edx(hi):eax(lo)=a*b + add eax,ecx //sum lo carry + adc edx,0 //sum hi carry + mov ecx,edx //store carry + mov [edi+24],eax //load a + + mov eax,[esi+28] //load a + mul [y] //edx(hi):eax(lo)=a*b + add eax,ecx //sum lo carry + adc edx,0 //sum hi carry + mov [edi+28],eax //load a + mov eax,edx //store carry +#endif + } + } + +/* +* Eight Word Block Multiply/Add +*/ +inline word word8_madd3(word z[8], const word x[8], word y, word carry) + { + z[0] = word_madd3(x[0], y, z[0], &carry); + z[1] = word_madd3(x[1], y, z[1], &carry); + z[2] = word_madd3(x[2], y, z[2], &carry); + z[3] = word_madd3(x[3], y, z[3], &carry); + z[4] = word_madd3(x[4], y, z[4], &carry); + z[5] = word_madd3(x[5], y, z[5], &carry); + z[6] = word_madd3(x[6], y, z[6], &carry); + z[7] = word_madd3(x[7], y, z[7], &carry); + return carry; + } + +/* +* Multiply-Add Accumulator +*/ +inline void word3_muladd(word* w2, word* w1, word* w0, word a, word b) + { + word carry = *w0; + *w0 = word_madd2(a, b, &carry); + *w1 += carry; + *w2 += (*w1 < carry) ? 1 : 0; + } + +/* +* Multiply-Add Accumulator +*/ +inline void word3_muladd_2(word* w2, word* w1, word* w0, word a, word b) + { + word carry = 0; + a = word_madd2(a, b, &carry); + b = carry; + + word top = (b >> (BOTAN_MP_WORD_BITS-1)); + b <<= 1; + b |= (a >> (BOTAN_MP_WORD_BITS-1)); + a <<= 1; + + carry = 0; + *w0 = word_add(*w0, a, &carry); + *w1 = word_add(*w1, b, &carry); + *w2 = word_add(*w2, top, &carry); + } + +} + +} + +#endif diff --git a/botan/src/math/bigint/mp_karat.cpp b/botan/src/math/bigint/mp_karat.cpp new file mode 100644 index 0000000..f30d418 --- /dev/null +++ b/botan/src/math/bigint/mp_karat.cpp @@ -0,0 +1,336 @@ +/* +* Karatsuba Multiplication/Squaring +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/mp_core.h> +#include <botan/mem_ops.h> +#include <botan/mp_asmi.h> + +namespace Botan { + +namespace { + +/* +* Karatsuba Multiplication Operation +*/ +void karatsuba_mul(word z[], const word x[], const word y[], u32bit N, + word workspace[]) + { + if(N == 6) + bigint_comba_mul6(z, x, y); + else if(N == 8) + bigint_comba_mul8(z, x, y); + else if(N == 16) + bigint_comba_mul16(z, x, y); + else if(N < BOTAN_KARAT_MUL_THRESHOLD || N % 2) + bigint_simple_mul(z, x, N, y, N); + else + { + const u32bit N2 = N / 2; + + const word* x0 = x; + const word* x1 = x + N2; + const word* y0 = y; + const word* y1 = y + N2; + word* z0 = z; + word* z1 = z + N; + + const s32bit cmp0 = bigint_cmp(x0, N2, x1, N2); + const s32bit cmp1 = bigint_cmp(y1, N2, y0, N2); + + clear_mem(workspace, 2*N); + + if(cmp0 && cmp1) + { + if(cmp0 > 0) + bigint_sub3(z0, x0, N2, x1, N2); + else + bigint_sub3(z0, x1, N2, x0, N2); + + if(cmp1 > 0) + bigint_sub3(z1, y1, N2, y0, N2); + else + bigint_sub3(z1, y0, N2, y1, N2); + + karatsuba_mul(workspace, z0, z1, N2, workspace+N); + } + + karatsuba_mul(z0, x0, y0, N2, workspace+N); + karatsuba_mul(z1, x1, y1, N2, workspace+N); + + const u32bit blocks_of_8 = N - (N % 8); + + word carry = 0; + + for(u32bit j = 0; j != blocks_of_8; j += 8) + carry = word8_add3(workspace + N + j, z0 + j, z1 + j, carry); + + for(u32bit j = blocks_of_8; j != N; ++j) + workspace[N + j] = word_add(z0[j], z1[j], &carry); + + word carry2 = 0; + + for(u32bit j = 0; j != blocks_of_8; j += 8) + carry2 = word8_add2(z + N2 + j, workspace + N + j, carry2); + + for(u32bit j = blocks_of_8; j != N; ++j) + z[N2 + j] = word_add(z[N2 + j], workspace[N + j], &carry2); + + z[N + N2] = word_add(z[N + N2], carry2, &carry); + + if(carry) + for(u32bit j = 1; j != N2; ++j) + if(++z[N + N2 + j]) + break; + + if((cmp0 == cmp1) || (cmp0 == 0) || (cmp1 == 0)) + bigint_add2(z + N2, 2*N-N2, workspace, N); + else + bigint_sub2(z + N2, 2*N-N2, workspace, N); + } + } + +/* +* Karatsuba Squaring Operation +*/ +void karatsuba_sqr(word z[], const word x[], u32bit N, word workspace[]) + { + if(N == 6) + bigint_comba_sqr6(z, x); + else if(N == 8) + bigint_comba_sqr8(z, x); + else if(N == 16) + bigint_comba_sqr16(z, x); + else if(N < BOTAN_KARAT_SQR_THRESHOLD || N % 2) + bigint_simple_sqr(z, x, N); + else + { + const u32bit N2 = N / 2; + + const word* x0 = x; + const word* x1 = x + N2; + word* z0 = z; + word* z1 = z + N; + + const s32bit cmp = bigint_cmp(x0, N2, x1, N2); + + clear_mem(workspace, 2*N); + + if(cmp) + { + if(cmp > 0) + bigint_sub3(z0, x0, N2, x1, N2); + else + bigint_sub3(z0, x1, N2, x0, N2); + + karatsuba_sqr(workspace, z0, N2, workspace+N); + } + + karatsuba_sqr(z0, x0, N2, workspace+N); + karatsuba_sqr(z1, x1, N2, workspace+N); + + const u32bit blocks_of_8 = N - (N % 8); + + word carry = 0; + + for(u32bit j = 0; j != blocks_of_8; j += 8) + carry = word8_add3(workspace + N + j, z0 + j, z1 + j, carry); + + for(u32bit j = blocks_of_8; j != N; ++j) + workspace[N + j] = word_add(z0[j], z1[j], &carry); + + word carry2 = 0; + + for(u32bit j = 0; j != blocks_of_8; j += 8) + carry2 = word8_add2(z + N2 + j, workspace + N + j, carry2); + + for(u32bit j = blocks_of_8; j != N; ++j) + z[N2 + j] = word_add(z[N2 + j], workspace[N + j], &carry2); + + z[N + N2] = word_add(z[N + N2], carry2, &carry); + + if(carry) + for(u32bit j = 1; j != N2; ++j) + if(++z[N + N2 + j]) + break; + + if(cmp == 0) + bigint_add2(z + N2, 2*N-N2, workspace, N); + else + bigint_sub2(z + N2, 2*N-N2, workspace, N); + } + } + +/* +* Pick a good size for the Karatsuba multiply +*/ +u32bit karatsuba_size(u32bit z_size, + u32bit x_size, u32bit x_sw, + u32bit y_size, u32bit y_sw) + { + if(x_sw > x_size || x_sw > y_size || y_sw > x_size || y_sw > y_size) + return 0; + + if(((x_size == x_sw) && (x_size % 2)) || + ((y_size == y_sw) && (y_size % 2))) + return 0; + + const u32bit start = (x_sw > y_sw) ? x_sw : y_sw; + const u32bit end = (x_size < y_size) ? x_size : y_size; + + if(start == end) + { + if(start % 2) + return 0; + return start; + } + + for(u32bit j = start; j <= end; ++j) + { + if(j % 2) + continue; + + if(2*j > z_size) + return 0; + + if(x_sw <= j && j <= x_size && y_sw <= j && j <= y_size) + { + if(j % 4 == 2 && + (j+2) <= x_size && (j+2) <= y_size && 2*(j+2) <= z_size) + return j+2; + return j; + } + } + + return 0; + } + +/* +* Pick a good size for the Karatsuba squaring +*/ +u32bit karatsuba_size(u32bit z_size, u32bit x_size, u32bit x_sw) + { + if(x_sw == x_size) + { + if(x_sw % 2) + return 0; + return x_sw; + } + + for(u32bit j = x_sw; j <= x_size; ++j) + { + if(j % 2) + continue; + + if(2*j > z_size) + return 0; + + if(j % 4 == 2 && (j+2) <= x_size && 2*(j+2) <= z_size) + return j+2; + return j; + } + + return 0; + } + +} + +/* +* Multiplication Algorithm Dispatcher +*/ +void bigint_mul(word z[], u32bit z_size, word workspace[], + const word x[], u32bit x_size, u32bit x_sw, + const word y[], u32bit y_size, u32bit y_sw) + { + if(x_sw == 1) + { + bigint_linmul3(z, y, y_sw, x[0]); + } + else if(y_sw == 1) + { + bigint_linmul3(z, x, x_sw, y[0]); + } + else if(x_sw <= 4 && x_size >= 4 && + y_sw <= 4 && y_size >= 4 && z_size >= 8) + { + bigint_comba_mul4(z, x, y); + } + else if(x_sw <= 6 && x_size >= 6 && + y_sw <= 6 && y_size >= 6 && z_size >= 12) + { + bigint_comba_mul6(z, x, y); + } + else if(x_sw <= 8 && x_size >= 8 && + y_sw <= 8 && y_size >= 8 && z_size >= 16) + { + bigint_comba_mul8(z, x, y); + } + else if(x_sw <= 16 && x_size >= 16 && + y_sw <= 16 && y_size >= 16 && z_size >= 32) + { + bigint_comba_mul16(z, x, y); + } + else if(x_sw < BOTAN_KARAT_MUL_THRESHOLD || y_sw < BOTAN_KARAT_MUL_THRESHOLD) + bigint_simple_mul(z, x, x_sw, y, y_sw); + else + { + const u32bit N = karatsuba_size(z_size, x_size, x_sw, y_size, y_sw); + + if(N) + { + clear_mem(workspace, 2*N); + karatsuba_mul(z, x, y, N, workspace); + } + else + bigint_simple_mul(z, x, x_sw, y, y_sw); + } + } + +/* +* Squaring Algorithm Dispatcher +*/ +void bigint_sqr(word z[], u32bit z_size, word workspace[], + const word x[], u32bit x_size, u32bit x_sw) + { + if(x_sw == 1) + { + bigint_linmul3(z, x, x_sw, x[0]); + } + else if(x_sw <= 4 && x_size >= 4 && z_size >= 8) + { + bigint_comba_sqr4(z, x); + } + else if(x_sw <= 6 && x_size >= 6 && z_size >= 12) + { + bigint_comba_sqr6(z, x); + } + else if(x_sw <= 8 && x_size >= 8 && z_size >= 16) + { + bigint_comba_sqr8(z, x); + } + else if(x_sw <= 16 && x_size >= 16 && z_size >= 32) + { + bigint_comba_sqr16(z, x); + } + else if(x_size < BOTAN_KARAT_SQR_THRESHOLD) + { + bigint_simple_sqr(z, x, x_sw); + } + else + { + const u32bit N = karatsuba_size(z_size, x_size, x_sw); + + if(N) + { + clear_mem(workspace, 2*N); + karatsuba_sqr(z, x, N, workspace); + } + else + bigint_simple_sqr(z, x, x_sw); + } + } + +} diff --git a/botan/src/math/bigint/mp_misc.cpp b/botan/src/math/bigint/mp_misc.cpp new file mode 100644 index 0000000..6b7fc65 --- /dev/null +++ b/botan/src/math/bigint/mp_misc.cpp @@ -0,0 +1,94 @@ +/* +* MP Misc Functions +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/mp_core.h> +#include <botan/mp_asm.h> + +namespace Botan { + +extern "C" { + +/* +* Core Division Operation +*/ +u32bit bigint_divcore(word q, word y1, word y2, + word x1, word x2, word x3) + { + word y0 = 0; + y2 = word_madd2(q, y2, &y0); + y1 = word_madd2(q, y1, &y0); + + if(y0 > x1) return 1; + if(y0 < x1) return 0; + if(y1 > x2) return 1; + if(y1 < x2) return 0; + if(y2 > x3) return 1; + if(y2 < x3) return 0; + return 0; + } + +/* +* Compare two MP integers +*/ +s32bit bigint_cmp(const word x[], u32bit x_size, + const word y[], u32bit y_size) + { + if(x_size < y_size) { return (-bigint_cmp(y, y_size, x, x_size)); } + + while(x_size > y_size) + { + if(x[x_size-1]) + return 1; + x_size--; + } + for(u32bit j = x_size; j > 0; --j) + { + if(x[j-1] > y[j-1]) return 1; + if(x[j-1] < y[j-1]) return -1; + } + return 0; + } + +/* +* Do a 2-word/1-word Division +*/ +word bigint_divop(word n1, word n0, word d) + { + word high = n1 % d, quotient = 0; + + for(u32bit j = 0; j != MP_WORD_BITS; ++j) + { + word high_top_bit = (high & MP_WORD_TOP_BIT); + + high <<= 1; + high |= (n0 >> (MP_WORD_BITS-1-j)) & 1; + quotient <<= 1; + + if(high_top_bit || high >= d) + { + high -= d; + quotient |= 1; + } + } + + return quotient; + } + +/* +* Do a 2-word/1-word Modulo +*/ +word bigint_modop(word n1, word n0, word d) + { + word z = bigint_divop(n1, n0, d); + word dummy = 0; + z = word_madd2(z, d, &dummy); + return (n0-z); + } + +} + +} diff --git a/botan/src/math/bigint/mp_shift.cpp b/botan/src/math/bigint/mp_shift.cpp new file mode 100644 index 0000000..a7de79c --- /dev/null +++ b/botan/src/math/bigint/mp_shift.cpp @@ -0,0 +1,138 @@ +/* +* MP Shift Algorithms +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/mp_core.h> +#include <botan/mem_ops.h> + +namespace Botan { + +extern "C" { + +/* +* Single Operand Left Shift +*/ +void bigint_shl1(word x[], u32bit x_size, u32bit word_shift, u32bit bit_shift) + { + if(word_shift) + { + for(u32bit j = 1; j != x_size + 1; ++j) + x[(x_size - j) + word_shift] = x[x_size - j]; + clear_mem(x, word_shift); + } + + if(bit_shift) + { + word carry = 0; + for(u32bit j = word_shift; j != x_size + word_shift + 1; ++j) + { + word temp = x[j]; + x[j] = (temp << bit_shift) | carry; + carry = (temp >> (MP_WORD_BITS - bit_shift)); + } + } + } + +/* +* Single Operand Right Shift +*/ +void bigint_shr1(word x[], u32bit x_size, u32bit word_shift, u32bit bit_shift) + { + if(x_size < word_shift) + { + clear_mem(x, x_size); + return; + } + + if(word_shift) + { + copy_mem(x, x + word_shift, x_size - word_shift); + clear_mem(x + x_size - word_shift, word_shift); + } + + if(bit_shift) + { + word carry = 0; + + u32bit top = x_size - word_shift; + + while(top >= 4) + { + word w = x[top-1]; + x[top-1] = (w >> bit_shift) | carry; + carry = (w << (MP_WORD_BITS - bit_shift)); + + w = x[top-2]; + x[top-2] = (w >> bit_shift) | carry; + carry = (w << (MP_WORD_BITS - bit_shift)); + + w = x[top-3]; + x[top-3] = (w >> bit_shift) | carry; + carry = (w << (MP_WORD_BITS - bit_shift)); + + w = x[top-4]; + x[top-4] = (w >> bit_shift) | carry; + carry = (w << (MP_WORD_BITS - bit_shift)); + + top -= 4; + } + + while(top) + { + word w = x[top-1]; + x[top-1] = (w >> bit_shift) | carry; + carry = (w << (MP_WORD_BITS - bit_shift)); + + top--; + } + } + } + +/* +* Two Operand Left Shift +*/ +void bigint_shl2(word y[], const word x[], u32bit x_size, + u32bit word_shift, u32bit bit_shift) + { + for(u32bit j = 0; j != x_size; ++j) + y[j + word_shift] = x[j]; + if(bit_shift) + { + word carry = 0; + for(u32bit j = word_shift; j != x_size + word_shift + 1; ++j) + { + word w = y[j]; + y[j] = (w << bit_shift) | carry; + carry = (w >> (MP_WORD_BITS - bit_shift)); + } + } + } + +/* +* Two Operand Right Shift +*/ +void bigint_shr2(word y[], const word x[], u32bit x_size, + u32bit word_shift, u32bit bit_shift) + { + if(x_size < word_shift) return; + + for(u32bit j = 0; j != x_size - word_shift; ++j) + y[j] = x[j + word_shift]; + if(bit_shift) + { + word carry = 0; + for(u32bit j = x_size - word_shift; j > 0; --j) + { + word w = y[j-1]; + y[j-1] = (w >> bit_shift) | carry; + carry = (w << (MP_WORD_BITS - bit_shift)); + } + } + } + +} + +} diff --git a/botan/src/math/bigint/mp_types.h b/botan/src/math/bigint/mp_types.h new file mode 100644 index 0000000..1648713 --- /dev/null +++ b/botan/src/math/bigint/mp_types.h @@ -0,0 +1,33 @@ +/* +* Low Level MPI Types +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_MPI_TYPES_H__ +#define BOTAN_MPI_TYPES_H__ + +#include <botan/types.h> + +namespace Botan { + +#if (BOTAN_MP_WORD_BITS == 8) + typedef byte word; +#elif (BOTAN_MP_WORD_BITS == 16) + typedef u16bit word; +#elif (BOTAN_MP_WORD_BITS == 32) + typedef u32bit word; +#elif (BOTAN_MP_WORD_BITS == 64) + typedef u64bit word; +#else + #error BOTAN_MP_WORD_BITS must be 8, 16, 32, or 64 +#endif + +const word MP_WORD_MASK = ~static_cast<word>(0); +const word MP_WORD_TOP_BIT = static_cast<word>(1) << (8*sizeof(word) - 1); +const word MP_WORD_MAX = MP_WORD_MASK; + +} + +#endif diff --git a/botan/src/math/bigint/mulop_amd64/info.txt b/botan/src/math/bigint/mulop_amd64/info.txt new file mode 100644 index 0000000..77990df --- /dev/null +++ b/botan/src/math/bigint/mulop_amd64/info.txt @@ -0,0 +1,32 @@ +realname "BigInt Multiply-Add (x86-64)" + +mp_bits 64 + +load_on never + +<add> +mp_mulop_amd64.S +</add> + +<arch> +amd64 +</arch> + +<cc> +gcc +icc +</cc> + +# ELF systems +<os> +linux +freebsd +dragonfly +netbsd +openbsd +solaris +</os> + +<requires> +asm_amd64 +</requires> diff --git a/botan/src/math/bigint/mulop_amd64/mp_mulop.cpp b/botan/src/math/bigint/mulop_amd64/mp_mulop.cpp new file mode 100644 index 0000000..cbd723e --- /dev/null +++ b/botan/src/math/bigint/mulop_amd64/mp_mulop.cpp @@ -0,0 +1,96 @@ +/* +* Simple O(N^2) Multiplication and Squaring +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/mp_asm.h> +#include <botan/mp_asmi.h> +#include <botan/mp_core.h> +#include <botan/mem_ops.h> + +namespace Botan { + +extern "C" { + +/* +* Simple O(N^2) Multiplication +*/ +void bigint_simple_mul(word z[], const word x[], u32bit x_size, + const word y[], u32bit y_size) + { + const u32bit blocks = x_size - (x_size % 8); + + clear_mem(z, x_size + y_size); + + for(u32bit i = 0; i != y_size; ++i) + { + word carry = 0; + + for(u32bit j = 0; j != blocks; j += 8) + carry = word8_madd3(z + i + j, x + j, y[i], carry); + + for(u32bit j = blocks; j != x_size; ++j) + z[i+j] = word_madd3(x[j], y[i], z[i+j], &carry); + + z[x_size+i] = carry; + } + } + +inline word word_sqr(word x, + +/* +* Simple O(N^2) Squaring + +This is exactly the same algorithm as bigint_simple_mul, +however because C/C++ compilers suck at alias analysis it +is good to have the version where the compiler knows +that x == y +*/ +void bigint_simple_sqr(word z[], const word x[], u32bit x_size) + { + clear_mem(z, 2*x_size); + + for(u32bit i = 0; i != x_size; ++i) + { + const word x_i = x[i]; + + word carry = z[2*i]; + z[2*i] = word_madd2(x_i, x_i, z[2*i], &carry); + + for(u32bit j = i; j != x_size; ++j) + { + // z[i+j] = z[i+j] + 2 * x[j] * x_i + carry; + + /* + load z[i+j] into register + load x[j] into %hi + mulq %[x_i] -> x[i] * x[j] -> %lo:%hi + shlq %lo, $1 + + // put carry bit (cf) from %lo into %temp + xorl %temp + adcq $0, %temp + + // high bit of lo now in cf + shl %hi, $1 + // add in lowest bid from %lo + orl %temp, %hi + + addq %[c], %[lo] + adcq $0, %[hi] + addq %[z_ij], %[lo] + adcq $0, %[hi] + + */ + + } + + z[x_size+i] = carry; + } + } + +} + +} diff --git a/botan/src/math/bigint/mulop_amd64/mp_mulop_amd64.S b/botan/src/math/bigint/mulop_amd64/mp_mulop_amd64.S new file mode 100644 index 0000000..63ac55e --- /dev/null +++ b/botan/src/math/bigint/mulop_amd64/mp_mulop_amd64.S @@ -0,0 +1,130 @@ +/* +* Simple O(N^2) Multiplication and Squaring +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/asm_macr.h> + +START_LISTING(mp_mulop.S) + +#if 0 +void bigint_simple_sqr(word z[], const word x[], u32bit x_size) + { + const u32bit blocks = x_size - (x_size % 8); + + clear_mem(z, 2*x_size); + + for(u32bit i = 0; i != x_size; ++i) + { + word carry = 0; + + /* + for(u32bit j = 0; j != blocks; j += 8) + carry = word8_madd3(z + i + j, x + j, x[i], carry); + + for(u32bit j = blocks; j != x_size; ++j) + z[i+j] = word_madd3(x[j], x[i], z[i+j], &carry); + */ + + + for(u32bit j = 0; j != x_size; ++j) + z[i+j] = word_madd3(x[j], x[i], z[i+j], &carry); + + for(u32bit j = 0; j != x_size; ++j) + { + dword z = (dword)a * b + c + *d; + *d = (word)(z >> BOTAN_MP_WORD_BITS); + return (word)z; + } + + + + z[i+j] = word_madd3(x[j], x[i], z[i+j], &carry); + + } + + + + z[x_size+i] = carry; + } + } + +#endif + +START_FUNCTION(bigint_simple_sqr) + +#define Z_ARR ARG_1 +#define X_ARR ARG_2 +//#define X_SIZE ARG_3_32 + +#define CARRY TEMP_1 +#define Z_WORD TEMP_2 +#define LOOP_I TEMP_3 +#define LOOP_J TEMP_4 +#define X_SIZE TEMP_5 +#define MUL_LO %rax +// arg 3, xsize +#define MUL_HI %rdx + +// need arg3 == rdx for multiply + ASSIGN(X_SIZE, ARG3_32) + + ZEROIZE(CARRY) + + ZEROIZE(LOOP_I) + +.LOOP_ZEROIZE_Z: + + cmp LOOP_I, X_SIZE + + + + + JUMP_IF_ZERO(LOOP_CTR, .L_MULADD_DONE) + JUMP_IF_LT(LOOP_CTR, 8, .LOOP_MULADD1) + +#define MULADD_OP(N) \ + ASSIGN(MUL_LO, ARRAY8(X_ARR, N)) ; \ + ASSIGN(Z_WORD, ARRAY8(Z_ARR, N)) ; \ + MUL(Y) ; \ + ADD(Z_WORD, CARRY) ; \ + ASSIGN(CARRY, MUL_HI) ; \ + ADD_LAST_CARRY(CARRY) ; \ + ADD(Z_WORD, MUL_LO) ; \ + ADD_LAST_CARRY(CARRY) ; \ + ASSIGN(ARRAY8(Z_ARR, N), Z_WORD) + +.LOOP_MULADD8: + MULADD_OP(0) + MULADD_OP(1) + MULADD_OP(2) + MULADD_OP(3) + MULADD_OP(4) + MULADD_OP(5) + MULADD_OP(6) + MULADD_OP(7) + + SUB_IMM(LOOP_CTR, 8) + ADD_IMM(Z_ARR, 64) + ADD_IMM(X_ARR, 64) + cmp IMM(8), LOOP_CTR + jge .LOOP_MULADD8 + + JUMP_IF_ZERO(LOOP_CTR, .L_MULADD_DONE) + +ALIGN +.LOOP_MULADD1: + MULADD_OP(0) + + SUB_IMM(LOOP_CTR, 1) + ADD_IMM(Z_ARR, 8) + ADD_IMM(X_ARR, 8) + + cmp IMM(0), LOOP_CTR + jne .LOOP_MULADD1 + +.L_MULADD_DONE: + RETURN_VALUE_IS(CARRY) +END_FUNCTION(bigint_simple_square) diff --git a/botan/src/math/bigint/mulop_generic/info.txt b/botan/src/math/bigint/mulop_generic/info.txt new file mode 100644 index 0000000..28ebe41 --- /dev/null +++ b/botan/src/math/bigint/mulop_generic/info.txt @@ -0,0 +1,7 @@ +realname "BigInt Multiply-Add" + +load_on dep + +<add> +mp_mulop.cpp +</add> diff --git a/botan/src/math/bigint/mulop_generic/mp_mulop.cpp b/botan/src/math/bigint/mulop_generic/mp_mulop.cpp new file mode 100644 index 0000000..4647d00 --- /dev/null +++ b/botan/src/math/bigint/mulop_generic/mp_mulop.cpp @@ -0,0 +1,77 @@ +/* +* Simple O(N^2) Multiplication and Squaring +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/mp_asm.h> +#include <botan/mp_asmi.h> +#include <botan/mp_core.h> +#include <botan/mem_ops.h> + +namespace Botan { + +extern "C" { + +/* +* Simple O(N^2) Multiplication +*/ +void bigint_simple_mul(word z[], const word x[], u32bit x_size, + const word y[], u32bit y_size) + { + const u32bit x_size_8 = x_size - (x_size % 8); + + clear_mem(z, x_size + y_size); + + for(u32bit i = 0; i != y_size; ++i) + { + const word y_i = y[i]; + + word carry = 0; + + for(u32bit j = 0; j != x_size_8; j += 8) + carry = word8_madd3(z + i + j, x + j, y_i, carry); + + for(u32bit j = x_size_8; j != x_size; ++j) + z[i+j] = word_madd3(x[j], y_i, z[i+j], &carry); + + z[x_size+i] = carry; + } + } + +/* +* Simple O(N^2) Squaring + +This is exactly the same algorithm as bigint_simple_mul, +however because C/C++ compilers suck at alias analysis it +is good to have the version where the compiler knows +that x == y + +There is an O(n^1.5) squaring algorithm specified in Handbook of +Applied Cryptography, chapter 14 +*/ +void bigint_simple_sqr(word z[], const word x[], u32bit x_size) + { + const u32bit x_size_8 = x_size - (x_size % 8); + + clear_mem(z, 2*x_size); + + for(u32bit i = 0; i != x_size; ++i) + { + const word x_i = x[i]; + word carry = 0; + + for(u32bit j = 0; j != x_size_8; j += 8) + carry = word8_madd3(z + i + j, x + j, x_i, carry); + + for(u32bit j = x_size_8; j != x_size; ++j) + z[i+j] = word_madd3(x[j], x_i, z[i+j], &carry); + + z[x_size+i] = carry; + } + } + +} + +} diff --git a/botan/src/math/bigint/mulop_ia32/info.txt b/botan/src/math/bigint/mulop_ia32/info.txt new file mode 100644 index 0000000..b995dd8 --- /dev/null +++ b/botan/src/math/bigint/mulop_ia32/info.txt @@ -0,0 +1,34 @@ +realname "BigInt Multiply-Add (IA-32)" + +mp_bits 32 + +# Out of date, still implements bigint_mul_add_words + +load_on request + +<add> +mp_mulop.S +</add> + +<arch> +ia32 +</arch> + +<cc> +gcc +icc +</cc> + +# ELF systems +<os> +linux +freebsd +dragonfly +netbsd +openbsd +solaris +</os> + +<requires> +asm_ia32 +</requires> diff --git a/botan/src/math/bigint/mulop_ia32/mp_mulop.S b/botan/src/math/bigint/mulop_ia32/mp_mulop.S new file mode 100644 index 0000000..716166f --- /dev/null +++ b/botan/src/math/bigint/mulop_ia32/mp_mulop.S @@ -0,0 +1,64 @@ +/* +* Multiply/Add Algorithm Source File +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/asm_macr.h> + +START_LISTING(mp_muladd.S) + +START_FUNCTION(bigint_mul_add_words) + SPILL_REGS() +#define PUSHED 4 + +#define LOOP_CTR ESI + ASSIGN(LOOP_CTR, ARG(3)) /* x_size */ + ZEROIZE(EDI) + + ASSIGN(ECX, ARG(1)) /* z[] */ + ASSIGN(EBX, ARG(2)) /* x[] */ + ASSIGN(EBP, ARG(4)) /* y */ + +#define MULADD_OP(N) \ + ASSIGN(EAX, ARRAY4(EBX, N)) ; \ + MUL(EBP) ; \ + ADD_W_CARRY(EAX, EDX, EDI) ; \ + ASSIGN(EDI, EDX) ; \ + ADD_W_CARRY(ARRAY4(ECX, N), EDI, EAX) ; + + JUMP_IF_ZERO(LOOP_CTR, .MUL_ADD_DONE) + JUMP_IF_LT(LOOP_CTR, 8, .MULADD1_LOOP) + +START_LOOP(.MULADD8) + MULADD_OP(0) + MULADD_OP(1) + MULADD_OP(2) + MULADD_OP(3) + MULADD_OP(4) + MULADD_OP(5) + MULADD_OP(6) + MULADD_OP(7) + + SUB_IMM(LOOP_CTR, 8) + ADD_IMM(EBX, 32) + ADD_IMM(ECX, 32) +LOOP_UNTIL_LT(LOOP_CTR, 8, .MULADD8) + + JUMP_IF_ZERO(LOOP_CTR, .MUL_ADD_DONE) + +START_LOOP(.MULADD1) + MULADD_OP(0) + + SUB_IMM(LOOP_CTR, 1) + ADD_IMM(EBX, 4) + ADD_IMM(ECX, 4) +LOOP_UNTIL_EQ(LOOP_CTR, 0, .MULADD1) + +.MUL_ADD_DONE: + + ASSIGN(EAX, EDI) +#undef PUSHED + RESTORE_REGS() +END_FUNCTION(bigint_mul_add_words) diff --git a/botan/src/math/gfpmath/curve_gfp.cpp b/botan/src/math/gfpmath/curve_gfp.cpp new file mode 100644 index 0000000..e12ade0 --- /dev/null +++ b/botan/src/math/gfpmath/curve_gfp.cpp @@ -0,0 +1,165 @@ +/****** +* Elliptic curves over GF(p) +* +* (C) 2007 Martin Doering +* Christoph Ludwig +* Falko Strenzke +* 2008 Jack Lloyd +******/ + +#include <botan/curve_gfp.h> +#include <botan/bigint.h> +#include <assert.h> +#include <ostream> + +namespace Botan { + +void CurveGFp::set_shrd_mod(const std::tr1::shared_ptr<GFpModulus> mod) + { + mp_mod = mod; + mA.turn_off_sp_red_mul();// m.m. is not needed, must be trf. back + mB.turn_off_sp_red_mul();// m.m. is not needed, must be trf. back + //ok, above we destroy any evantually computated montg. mult. values, + // but that won't influence performance in usual applications + mA.set_shrd_mod(mod); + mB.set_shrd_mod(mod); + } + +CurveGFp::CurveGFp(const GFpElement& a, const GFpElement& b, + const BigInt& p) + : mA(a), + mB(b) + { + if(!((p == mA.get_p()) && (p == mB.get_p()))) + { + throw Invalid_Argument("could not construct curve: moduli of arguments differ"); + } + std::tr1::shared_ptr<GFpModulus> p_mod = std::tr1::shared_ptr<GFpModulus>(new GFpModulus(p)); + // the above is the creation of the GFpModuls object which will be shared point-wide + // (in the context of a point of course) + set_shrd_mod(p_mod); + } +// copy constructor +CurveGFp::CurveGFp(const CurveGFp& other) + : mA(other.get_a()), + mB(other.get_b()) + { + mp_mod = std::tr1::shared_ptr<GFpModulus>(new GFpModulus(*other.mp_mod)); + assert(mp_mod->p_equal_to(mA.get_p())); + assert(mp_mod->p_equal_to(mB.get_p())); + set_shrd_mod(mp_mod); + if(other.mp_mres_a.get()) + { + mp_mres_a = std::tr1::shared_ptr<GFpElement>(new GFpElement(*other.mp_mres_a)); + } + if(other.mp_mres_b.get()) + { + mp_mres_b = std::tr1::shared_ptr<GFpElement>(new GFpElement(*other.mp_mres_b)); + } + if(other.mp_mres_one.get()) + { + mp_mres_one = std::tr1::shared_ptr<GFpElement>(new GFpElement(*other.mp_mres_one)); + } + + } + +// assignment operator +const CurveGFp& CurveGFp::operator=(const CurveGFp& other) + { + // for exception safety... + GFpElement a_tmp = other.mA; + GFpElement b_tmp = other.mB; + mA.swap(a_tmp); + mB.swap(b_tmp); + + std::tr1::shared_ptr<GFpModulus> p_mod = std::tr1::shared_ptr<GFpModulus>(new GFpModulus(*other.mp_mod)); + set_shrd_mod(p_mod); + + // exception safety note: no problem if we have a throw from here on... + if(other.mp_mres_a.get()) + { + mp_mres_a = std::tr1::shared_ptr<GFpElement>(new GFpElement(*other.mp_mres_a)); + } + if(other.mp_mres_b.get()) + { + mp_mres_b = std::tr1::shared_ptr<GFpElement>(new GFpElement(*other.mp_mres_b)); + } + if(other.mp_mres_one.get()) + { + mp_mres_one = std::tr1::shared_ptr<GFpElement>(new GFpElement(*other.mp_mres_one)); + } + return *this; + } + +// getters +const GFpElement& CurveGFp::get_a() const + { + return mA; + } + +const GFpElement& CurveGFp::get_b() const + { + return mB; + } + +const BigInt CurveGFp::get_p() const + { + assert(mp_mod.get() != 0); + return mp_mod->get_p(); + } + +// swaps the states of *this and other, does not throw +void CurveGFp::swap(CurveGFp& other) + { + mA.swap(other.mA); + mB.swap(other.mB); + mp_mod.swap(other.mp_mod); + std::swap(mp_mres_a, other.mp_mres_a); + std::swap(mp_mres_b, other.mp_mres_b); + std::swap(mp_mres_one, other.mp_mres_one); + } + +GFpElement const CurveGFp::get_mres_a() const + { + if(mp_mres_a.get() == 0) + { + mp_mres_a = std::tr1::shared_ptr<GFpElement>(new GFpElement(mA)); + mp_mres_a->turn_on_sp_red_mul(); + mp_mres_a->get_mres(); + } + return GFpElement(*mp_mres_a); + } + +GFpElement const CurveGFp::get_mres_b() const + { + if(mp_mres_b.get() == 0) + { + mp_mres_b = std::tr1::shared_ptr<GFpElement>(new GFpElement(mB)); + mp_mres_b->turn_on_sp_red_mul(); + mp_mres_b->get_mres(); + } + return GFpElement(*mp_mres_b); + } + +std::tr1::shared_ptr<GFpElement const> const CurveGFp::get_mres_one() const + { + if(mp_mres_one.get() == 0) + { + mp_mres_one = std::tr1::shared_ptr<GFpElement>(new GFpElement(mp_mod->get_p(), 1)); + mp_mres_one->turn_on_sp_red_mul(); + mp_mres_one->get_mres(); + } + return mp_mres_one; + } + +bool operator==(const CurveGFp& lhs, const CurveGFp& rhs) + { + return (lhs.get_p() == rhs.get_p() && lhs.get_a() == rhs.get_a() && lhs.get_b() == rhs.get_b()); + } + +std::ostream& operator<<(std::ostream& output, const CurveGFp& elem) + { + return output << "y^2f = x^3 + (" << elem.get_a() << ")x + (" << elem.get_b() << ")"; + } + +} diff --git a/botan/src/math/gfpmath/curve_gfp.h b/botan/src/math/gfpmath/curve_gfp.h new file mode 100644 index 0000000..81cf6ce --- /dev/null +++ b/botan/src/math/gfpmath/curve_gfp.h @@ -0,0 +1,177 @@ +/****** + * Elliptic curves over GF(p) (header file) + * + * (C) 2007 Martin Doering + * doering@cdc.informatik.tu-darmstadt.de + * Christoph Ludwig + * ludwig@fh-worms.de + * Falko Strenzke + * strenzke@flexsecure.de + ******/ + +#ifndef BOTAN_GFP_CURVE_H__ +#define BOTAN_GFP_CURVE_H__ + +#include <botan/bigint.h> +#include <botan/gfp_element.h> +#include <iosfwd> + +namespace Botan { + +/** +* This class represents an elliptic curve over GF(p) +* +* Distributed under the terms of the Botan license +*/ +class BOTAN_DLL CurveGFp + { + public: + + /** + * Construct the elliptic curve E: y^2 = x^3 + ax + b over GF(p) + * @param a first coefficient + * @param b second coefficient + * @param p prime number of the field + */ + CurveGFp(const GFpElement& a, const GFpElement& b, + const BigInt& p); + + /** + * Copy constructor + * @param other The curve to clone + */ + CurveGFp(const CurveGFp& other); + + /** + * Assignment operator + * @param other The curve to use as source for the assignment + */ + const CurveGFp& operator=(const CurveGFp& other); + + /** + * Set the shared GFpModulus object. + * Warning: do not use this function unless you know in detail how + * the sharing of values + * in the various EC related objects works. + * Do NOT spread pointers to a GFpModulus over different threads! + * @param mod a shared pointer to a GFpModulus object suitable for + * *this. + */ + void set_shrd_mod(const std::tr1::shared_ptr<GFpModulus> mod); + + // getters + + /** + * Get coefficient a + * @result coefficient a + */ + const GFpElement& get_a() const; + + /** + * Get coefficient b + * @result coefficient b + */ + const GFpElement& get_b() const; + + /** + * Get the GFpElement coefficient a transformed + * to its m-residue. This can be used for efficency reasons: the curve + * stores the transformed version after the first invocation of this + * function. + * @result the coefficient a, transformed to its m-residue + */ + GFpElement const get_mres_a() const; + + /** + * Get the GFpElement coefficient b transformed + * to its m-residue. This can be used for efficency reasons: the curve + * stores the transformed version after the first invocation of this + * function. + * @result the coefficient b, transformed to its m-residue + */ + GFpElement const get_mres_b() const; + + + /** + * Get the GFpElement 1 transformed + * to its m-residue. This can be used for efficency reasons: the curve + * stores the transformed version after the first invocation of this + * function. + * @result the GFpElement 1, transformed to its m-residue + */ + std::tr1::shared_ptr<GFpElement const> const get_mres_one() const; + + /** + * Get prime modulus of the field of the curve + * @result prime modulus of the field of the curve + */ + BigInt const get_p() const; + /*inline std::tr1::shared_ptr<BigInt> const get_ptr_p() const + { + return mp_p; + }*/ + + /** + * Retrieve a shared pointer to the curves GFpModulus object for efficient storage + * and computation of montgomery multiplication related data members and functions. + * Warning: do not use this function unless you know in detail how the sharing of values + * in the various EC related objects works. + * Do NOT spread pointers to a GFpModulus over different threads! + * @result a shared pointer to a GFpModulus object + */ + inline std::tr1::shared_ptr<GFpModulus> const get_ptr_mod() const + { + return mp_mod; + } + + /** + * swaps the states of *this and other, does not throw + * @param other The curve to swap values with + */ + void swap(CurveGFp& other); + + private: + std::tr1::shared_ptr<GFpModulus> mp_mod; + GFpElement mA; + GFpElement mB; + mutable std::tr1::shared_ptr<GFpElement> mp_mres_a; + mutable std::tr1::shared_ptr<GFpElement> mp_mres_b; + mutable std::tr1::shared_ptr<GFpElement> mp_mres_one; + }; + +// relational operators +bool operator==(const CurveGFp& lhs, const CurveGFp& rhs); + +inline bool operator!=(const CurveGFp& lhs, const CurveGFp& rhs) + { + return !(lhs == rhs); + } + +// io operators +std::ostream& operator<<(std::ostream& output, const CurveGFp& elem); + +// swaps the states of curve1 and curve2, does not throw! +// cf. Meyers, Item 25 +inline +void swap(CurveGFp& curve1, CurveGFp& curve2) + { + curve1.swap(curve2); + } + +} // namespace Botan + + +namespace std { + +// swaps the states of curve1 and curve2, does not throw! +// cf. Meyers, Item 25 +template<> inline +void swap<Botan::CurveGFp>(Botan::CurveGFp& curve1, + Botan::CurveGFp& curve2) + { + curve1.swap(curve2); + } + +} // namespace std + +#endif diff --git a/botan/src/math/gfpmath/gfp_element.cpp b/botan/src/math/gfpmath/gfp_element.cpp new file mode 100644 index 0000000..b718093 --- /dev/null +++ b/botan/src/math/gfpmath/gfp_element.cpp @@ -0,0 +1,699 @@ +/****** + * Arithmetic for prime fields GF(p) (source file) + * + * (C) 2007 Martin Doering + * doering@cdc.informatik.tu-darmstadt.de + * Christoph Ludwig + * ludwig@fh-worms.de + * Falko Strenzke + * strenzke@flexsecure.de + ******/ + +#include <botan/gfp_element.h> +#include <botan/numthry.h> +#include <botan/def_powm.h> +#include <botan/mp_types.h> +#include <botan/mp_asm.h> +#include <botan/mp_asmi.h> +#include <assert.h> +#include <ostream> + +namespace Botan { + +namespace { + +void inner_montg_mult_sos(word result[], const word* a_bar, const word* b_bar, const word* n, const word* n_dash, u32bit s) + { + SecureVector<word> t; + t.grow_to(2*s+1); + + // t = a_bar * b_bar + for (u32bit i=0; i<s; i++) + { + word C = 0; + word S = 0; + for (u32bit j=0; j<s; j++) + { + // we use: + // word word_madd3(word a, word b, word c, word d, word* carry) + // returns a * b + c + d and resets the carry (not using it as input) + + S = word_madd3(a_bar[j], b_bar[i], t[i+j], &C); + t[i+j] = S; + } + t[i+s] = C; + } + + // ??? + for (u32bit i=0; i<s; i++) + { + // word word_madd2(word a, word b, word c, word* carry) + // returns a * b + c, resets the carry + + word C = 0; + word zero = 0; + word m = word_madd2(t[i], n_dash[0], &zero); + + for (u32bit j=0; j<s; j++) + { + word S = word_madd3(m, n[j], t[i+j], &C); + t[i+j] = S; + } + + //// mp_mulop.cpp: + ////word bigint_mul_add_words(word z[], const word x[], u32bit x_size, word y) + u32bit cnt = 0; + while (C > 0) + { + // we need not worry here about C > 1, because the other operand is zero + word tmp = word_add(t[i+s+cnt], 0, &C); + t[i+s+cnt] = tmp; + cnt++; + } + } + + // u = t + SecureVector<word> u; + u.grow_to(s+1); + for (u32bit j=0; j<s+1; j++) + { + u[j] = t[j+s]; + } + + // t = u - n + word B = 0; + word D = 0; + for (u32bit i=0; i<s; i++) + { + D = word_sub(u[i], n[i], &B); + t[i] = D; + } + D = word_sub(u[s], 0, &B); + t[s] = D; + + // if t >= 0 (B == 0 -> no borrow), return t + if(B == 0) + { + for (u32bit i=0; i<s; i++) + { + result[i] = t[i]; + } + } + else // else return u + { + for (u32bit i=0; i<s; i++) + { + result[i] = u[i]; + } + } + } + +void montg_mult(BigInt& result, BigInt& a_bar, BigInt& b_bar, const BigInt& m, const BigInt& m_dash, const BigInt) + { + if(m.is_zero() || m_dash.is_zero()) + throw Invalid_Argument("montg_mult(): neither modulus nor m_dash may be zero (and one of them was)"); + + if(a_bar.is_zero() || b_bar.is_zero()) + result = 0; + + u32bit s = m.sig_words(); + a_bar.grow_to(s); + b_bar.grow_to(s); + result.grow_to(s); + + inner_montg_mult_sos(result.get_reg(), a_bar.data(), b_bar.data(), + m.data(), m_dash.data(), s); + } + +/** +*calculates R=b^n (here b=2) with R>m (and R beeing as small as possible) for an odd modulus m. +* no check for oddity is performed! +* +* Distributed under the terms of the Botan license +*/ +BigInt montgm_calc_r_oddmod(const BigInt& prime) + { + u32bit n = prime.sig_words(); + BigInt result(1); + result <<= n*BOTAN_MP_WORD_BITS; + return result; + } + +/** +*calculates m' with r*r^-1 - m*m' = 1 +* where r^-1 is the multiplicative inverse of r to the modulus m +*/ +BigInt montgm_calc_m_dash(const BigInt& r, const BigInt& m, const BigInt& r_inv) + { + BigInt result = ((r * r_inv) - BigInt(1))/m; + return result; + } + +BigInt montg_trf_to_mres(const BigInt& ord_res, const BigInt& r, const BigInt& m) + { + BigInt result(ord_res); + result *= r; + result %= m; + return result; + } + +BigInt montg_trf_to_ordres(const BigInt& m_res, const BigInt& m, const BigInt& r_inv) + { + BigInt result(m_res); + result *= r_inv; + result %= m; + return result; + } + +} + +GFpElement::GFpElement(const BigInt& p, const BigInt& value, bool use_montgm) + : mp_mod(), + m_value(value %p), + m_use_montgm(use_montgm), + m_is_trf(false) + { + assert(mp_mod.get() == 0); + mp_mod = std::tr1::shared_ptr<GFpModulus>(new GFpModulus(p)); + assert(mp_mod->m_p_dash == 0); + if(m_use_montgm) + ensure_montgm_precomp(); + } + +GFpElement::GFpElement(std::tr1::shared_ptr<GFpModulus> const mod, const BigInt& value, bool use_montgm) + : mp_mod(), + m_value(value % mod->m_p), + m_use_montgm(use_montgm), + m_is_trf(false) + { + assert(mp_mod.get() == 0); + mp_mod = mod; + } + +GFpElement::GFpElement(const GFpElement& other) + : m_value(other.m_value), + m_use_montgm(other.m_use_montgm), + m_is_trf(other.m_is_trf) + + { + //creates an independent copy + assert((other.m_is_trf && other.m_use_montgm) || !other.m_is_trf); + mp_mod.reset(new GFpModulus(*other.mp_mod)); // copy-ctor of GFpModulus + } + +void GFpElement::turn_on_sp_red_mul() const + { + ensure_montgm_precomp(); + m_use_montgm = true; + } + +void GFpElement::turn_off_sp_red_mul() const + { + if(m_is_trf) + { + trf_to_ordres(); + // will happen soon anyway, so we can do it here already + // (this is not lazy but way more secure concerning our internal logic here) + } + m_use_montgm = false; + } + +void GFpElement::ensure_montgm_precomp() const + { + if((!mp_mod->m_r.is_zero()) && (!mp_mod->m_r_inv.is_zero()) && (!mp_mod->m_p_dash.is_zero())) + { + // values are already set, nothing more to do + } + else + { + BigInt tmp_r(montgm_calc_r_oddmod(mp_mod->m_p)); + + BigInt tmp_r_inv(inverse_mod(tmp_r, mp_mod->m_p)); + + BigInt tmp_p_dash(montgm_calc_m_dash(tmp_r, mp_mod->m_p, tmp_r_inv)); + + mp_mod->m_r.grow_reg(tmp_r.size()); + mp_mod->m_r_inv.grow_reg(tmp_r_inv.size()); + mp_mod->m_p_dash.grow_reg(tmp_p_dash.size()); + + mp_mod->m_r = tmp_r; + mp_mod->m_r_inv = tmp_r_inv; + mp_mod->m_p_dash = tmp_p_dash; + + assert(!mp_mod->m_r.is_zero()); + assert(!mp_mod->m_r_inv.is_zero()); + assert(!mp_mod->m_p_dash.is_zero()); + } + + } + +void GFpElement::set_shrd_mod(std::tr1::shared_ptr<GFpModulus> const p_mod) + { + mp_mod = p_mod; + } + +void GFpElement::trf_to_mres() const + { + if(!m_use_montgm) + { + throw Illegal_Transformation("GFpElement is not allowed to be transformed to m-residue"); + } + assert(m_is_trf == false); + assert(!mp_mod->m_r_inv.is_zero()); + assert(!mp_mod->m_p_dash.is_zero()); + m_value = montg_trf_to_mres(m_value, mp_mod->m_r, mp_mod->m_p); + m_is_trf = true; + } + +void GFpElement::trf_to_ordres() const + { + assert(m_is_trf == true); + m_value = montg_trf_to_ordres(m_value, mp_mod->m_p, mp_mod->m_r_inv); + m_is_trf = false; + } + +bool GFpElement::align_operands_res(const GFpElement& lhs, const GFpElement& rhs) //static + { + assert(lhs.mp_mod->m_p == rhs.mp_mod->m_p); + if(lhs.m_use_montgm && rhs.m_use_montgm) + { + assert(rhs.mp_mod->m_p_dash == lhs.mp_mod->m_p_dash); + assert(rhs.mp_mod->m_r == lhs.mp_mod->m_r); + assert(rhs.mp_mod->m_r_inv == lhs.mp_mod->m_r_inv); + if(!lhs.m_is_trf && !rhs.m_is_trf) + { + return false; + } + else if(lhs.m_is_trf && rhs.m_is_trf) + { + return true; + } + else // one is transf., the other not + { + if(!lhs.m_is_trf) + { + lhs.trf_to_mres(); + assert(rhs.m_is_trf==true); + return true; + } + assert(rhs.m_is_trf==false); + assert(lhs.m_is_trf==true); + rhs.trf_to_mres(); // the only possibility left... + return true; + } + } + else // at least one of them does not use mm + // (so it is impossible that both use it) + { + if(lhs.m_is_trf) + { + lhs.trf_to_ordres(); + assert(rhs.m_is_trf == false); + return false; + } + if(rhs.m_is_trf) + { + rhs.trf_to_ordres(); + assert(lhs.m_is_trf == false); + return false; + } + return false; + } + assert(false); + } + +bool GFpElement::is_trf_to_mres() const + { + return m_is_trf; + } + +const BigInt& GFpElement::get_p() const + { + return (mp_mod->m_p); + } + +const BigInt& GFpElement::get_value() const + { + if(m_is_trf) + { + assert(m_use_montgm); + trf_to_ordres(); + } + return m_value; + } + +const BigInt& GFpElement::get_mres() const + { + if(!m_use_montgm) + { + // does the following exception really make sense? + // wouldn´t it be better to simply turn on montg.mult. when + // this explicit request is made? + throw Illegal_Transformation("GFpElement is not allowed to be transformed to m-residue"); + } + if(!m_is_trf) + { + trf_to_mres(); + } + + return m_value; + } + +const GFpElement& GFpElement::operator=(const GFpElement& other) + { + m_value.grow_reg(other.m_value.size()); // grow first for exception safety + + //m_value = other.m_value; + + // m_use_montgm = other.m_use_montgm; + // m_is_trf = other.m_is_trf; + // we want to keep the member pointers, which might be part of a "sharing group" + // but we may not simply overwrite the BigInt values with those of the argument!! + // if ours already contains precomputations, it would be hazardous to + // set them back to zero. + // thus we first check for equality of the moduli, + // then whether either of the two objects already contains + // precomputed values. + + // we also deal with the case were the pointers themsevles are equal: + if(mp_mod.get() == other.mp_mod.get()) + { + // everything ok, we are in the same sharing group anyway, nothing to do + m_value = other.m_value; // cannot throw + m_use_montgm = other.m_use_montgm; + m_is_trf = other.m_is_trf; + return *this; + } + if(mp_mod->m_p != other.mp_mod->m_p) + { + // the moduli are different, this is a special case + // which will not occur in usual applications, + // so we don´t hesitate to simply create new objects + // (we do want to create an independent copy) + mp_mod.reset(new GFpModulus(*other.mp_mod)); // this could throw, + // and because of this + // we haven't modified + // anything so far + m_value = other.m_value; // can't throw + m_use_montgm = other.m_use_montgm; + m_is_trf = other.m_is_trf; + return *this; + } + // exception safety note: from now on we are on the safe + // side with respect to the modulus, + // so we can assign the value now: + m_value = other.m_value; + m_use_montgm = other.m_use_montgm; + m_is_trf = other.m_is_trf; + // the moduli are equal, but we deal with different sharing groups. + // we will NOT fuse the sharing goups + // and we will NOT reset already precomputed values + if(mp_mod->has_precomputations()) + { + // our own sharing group already has precomputed values, + // so nothing to do. + return *this; + } + else + { + // let´s see whether the argument has something for us... + if(other.mp_mod->has_precomputations()) + { + // fetch them for our sharing group + // exc. safety note: grow first + mp_mod->m_p_dash.grow_reg(other.mp_mod->m_p_dash.size()); + mp_mod->m_r.grow_reg(other.mp_mod->m_r.size()); + mp_mod->m_r_inv.grow_reg(other.mp_mod->m_r_inv.size()); + + mp_mod->m_p_dash = other.mp_mod->m_p_dash; + mp_mod->m_r = other.mp_mod->m_r; + mp_mod->m_r_inv = other.mp_mod->m_r_inv; + return *this; + } + } + // our precomputations aren´t set, the arguments neither, + // so we let them alone + return *this; + } + +void GFpElement::share_assign(const GFpElement& other) + { + assert((other.m_is_trf && other.m_use_montgm) || !other.m_is_trf); + + // use grow_to to make it exc safe + m_value.grow_reg(other.m_value.size()); + m_value = other.m_value; + + m_use_montgm = other.m_use_montgm; + m_is_trf = other.m_is_trf; + mp_mod = other.mp_mod; // cannot throw + } + +GFpElement& GFpElement::operator+=(const GFpElement& rhs) + { + GFpElement::align_operands_res(*this, rhs); + + workspace = m_value; + workspace += rhs.m_value; + if(workspace >= mp_mod->m_p) + workspace -= mp_mod->m_p; + + m_value = workspace; + assert(m_value < mp_mod->m_p); + assert(m_value >= 0); + + return *this; + } + +GFpElement& GFpElement::operator-=(const GFpElement& rhs) + { + GFpElement::align_operands_res(*this, rhs); + + workspace = m_value; + + workspace -= rhs.m_value; + + if(workspace.is_negative()) + workspace += mp_mod->m_p; + + m_value = workspace; + assert(m_value < mp_mod->m_p); + assert(m_value >= 0); + return *this; + } + +GFpElement& GFpElement::operator*= (u32bit rhs) + { + workspace = m_value; + workspace *= rhs; + workspace %= mp_mod->m_p; + m_value = workspace; + return *this; + } + +GFpElement& GFpElement::operator*=(const GFpElement& rhs) + { + assert(rhs.mp_mod->m_p == mp_mod->m_p); + // here, we do not use align_operands_res() for one simple reason: + // we want to enforce the transformation to an m-residue, otherwise it would + // never happen + if(m_use_montgm && rhs.m_use_montgm) + { + assert(rhs.mp_mod->m_p == mp_mod->m_p); // is montgm. mult is on, then precomps must be there + assert(rhs.mp_mod->m_p_dash == mp_mod->m_p_dash); + assert(rhs.mp_mod->m_r == mp_mod->m_r); + if(!m_is_trf) + { + trf_to_mres(); + } + if(!rhs.m_is_trf) + { + rhs.trf_to_mres(); + } + workspace = m_value; + montg_mult(m_value, workspace, rhs.m_value, mp_mod->m_p, mp_mod->m_p_dash, mp_mod->m_r); + } + else // ordinary multiplication + { + if(m_is_trf) + { + assert(m_use_montgm); + trf_to_ordres(); + } + if(rhs.m_is_trf) + { + assert(rhs.m_use_montgm); + rhs.trf_to_ordres(); + } + + workspace = m_value; + workspace *= rhs.m_value; + workspace %= mp_mod->m_p; + m_value = workspace; + } + return *this; + } + +GFpElement& GFpElement::operator/=(const GFpElement& rhs) + { + bool use_mres = GFpElement::align_operands_res(*this, rhs); + assert((this->m_is_trf && rhs.m_is_trf) || !(this->m_is_trf && rhs.m_is_trf)); + // (internal note: see C86) + if(use_mres) + { + assert(m_use_montgm && rhs.m_use_montgm); + GFpElement rhs_ordres(rhs); + rhs_ordres.trf_to_ordres(); + rhs_ordres.inverse_in_place(); + workspace = m_value; + workspace *= rhs_ordres.get_value(); + workspace %= mp_mod->m_p; + m_value = workspace; + + } + else + { + GFpElement inv_rhs(rhs); + inv_rhs.inverse_in_place(); + *this *= inv_rhs; + } + return *this; + } + +bool GFpElement::is_zero() + { + return (m_value.is_zero()); + // this is correct because x_bar = x * r = x = 0 for x = 0 + } + +GFpElement& GFpElement::inverse_in_place() + { + m_value = inverse_mod(m_value, mp_mod->m_p); + if(m_is_trf) + { + assert(m_use_montgm); + + m_value *= mp_mod->m_r; + m_value *= mp_mod->m_r; + m_value %= mp_mod->m_p; + } + assert(m_value <= mp_mod->m_p); + return *this; + } + +GFpElement& GFpElement::negate() + { + m_value = mp_mod->m_p - m_value; + assert(m_value <= mp_mod->m_p); + return *this; + } + +void GFpElement::swap(GFpElement& other) + { + m_value.swap(other.m_value); + mp_mod.swap(other.mp_mod); + std::swap<bool>(m_use_montgm,other.m_use_montgm); + std::swap<bool>(m_is_trf,other.m_is_trf); + } + +std::ostream& operator<<(std::ostream& output, const GFpElement& elem) + { + return output << '(' << elem.get_value() << "," << elem.get_p() << ')'; + } + +bool operator==(const GFpElement& lhs, const GFpElement& rhs) + { + // for effeciency reasons we firstly check whether + //the modulus pointers are different in the first place: + if(lhs.get_ptr_mod() != rhs.get_ptr_mod()) + { + if(lhs.get_p() != rhs.get_p()) + { + return false; + } + } + // so the modulus is equal, now check the values + bool use_mres = GFpElement::align_operands_res(lhs, rhs); + + if(use_mres) + { + return (lhs.get_mres() == rhs.get_mres()); + } + else + { + return(lhs.get_value() == rhs.get_value()); + } + } + +GFpElement operator+(const GFpElement& lhs, const GFpElement& rhs) + { + // consider the case that lhs and rhs both use montgm: + // then += returns an element which uses montgm. + // thus the return value of op+ here will be an element + // using montgm in this case + // NOTE: the rhs might be transformed when using op+, the lhs never + GFpElement result(lhs); + result += rhs; + return result; + } + +GFpElement operator-(const GFpElement& lhs, const GFpElement& rhs) + { + GFpElement result(lhs); + result -= rhs; + return result; + // NOTE: the rhs might be transformed when using op-, the lhs never + } + +GFpElement operator-(const GFpElement& lhs) + { + return(GFpElement(lhs)).negate(); + } + +GFpElement operator*(const GFpElement& lhs, const GFpElement& rhs) + { + // consider the case that lhs and rhs both use montgm: + // then *= returns an element which uses montgm. + // thus the return value of op* here will be an element + // using montgm in this case + GFpElement result(lhs); + result *= rhs; + return result; + } + +GFpElement operator*(const GFpElement& lhs, u32bit rhs) + { + GFpElement result(lhs); + result *= rhs; + return result; + } + +GFpElement operator*(u32bit lhs, const GFpElement& rhs) + { + return rhs*lhs; + } + +GFpElement operator/(const GFpElement& lhs, const GFpElement& rhs) + { + GFpElement result (lhs); + result /= rhs; + return result; + } + +SecureVector<byte> FE2OSP(const GFpElement& elem) + { + return BigInt::encode_1363(elem.get_value(), elem.get_p().bytes()); + } + +GFpElement OS2FEP(MemoryRegion<byte> const& os, BigInt p) + { + return GFpElement(p, BigInt::decode(os.begin(), os.size())); + } + +GFpElement inverse(const GFpElement& elem) + { + return GFpElement(elem).inverse_in_place(); + } + +} + diff --git a/botan/src/math/gfpmath/gfp_element.h b/botan/src/math/gfpmath/gfp_element.h new file mode 100644 index 0000000..4e0ee98 --- /dev/null +++ b/botan/src/math/gfpmath/gfp_element.h @@ -0,0 +1,311 @@ +/****** + * Arithmetic for prime fields GF(p) (header file) + * + * (C) 2007 Martin Doering + * doering@cdc.informatik.tu-darmstadt.de + * Christoph Ludwig + * ludwig@fh-worms.de + * Falko Strenzke + * strenzke@flexsecure.de + ******/ + +#ifndef BOTAN_GFP_ELEMENT_H__ +#define BOTAN_GFP_ELEMENT_H__ + +#include <botan/bigint.h> +#include <botan/gfp_modulus.h> +#include <iosfwd> + +#if defined(BOTAN_USE_STD_TR1) + #include <tr1/memory> +#elif defined(BOTAN_USE_BOOST_TR1) + #include <boost/tr1/memory.hpp> +#else + #error "Please choose a TR1 implementation in build.h" +#endif + +namespace Botan { + +struct Illegal_Transformation : public Exception + { + Illegal_Transformation(const std::string& err = + "Requested transformation is not possible") + : Exception(err) {} + }; + +/** + * This class represents one element in GF(p). Enables the convenient, + * transparent use of the montgomery multiplication. + */ +class BOTAN_DLL GFpElement + { + private: + std::tr1::shared_ptr<GFpModulus> mp_mod; + mutable BigInt m_value; // ordinary residue or m-residue respectively + mutable BigInt workspace; + + // ***************************************** + // data members for montgomery multiplication + mutable bool m_use_montgm; + //mutable BigInt m_mres; + // this bool tells use whether the m_mres carries + // the actual value (in this case mValue doesn´t) + mutable bool m_is_trf; + + void ensure_montgm_precomp() const; + void trf_to_mres() const; + void trf_to_ordres() const; + + public: + + /** construct an element of GF(p) with the given value. + * use_montg defaults to false and determines wether Montgomery + * multiplications will be use when applying operators *, *= + * @param p the prime number of the field + * @param value the element value + * @param use_montgm whether this object will use Montgomery multiplication + */ + explicit GFpElement (const BigInt& p, const BigInt& value, bool use_montgm = false); + + + /** construct an element of GF(p) with the given value (defaults + * to 0). use_montg defaults to false and determines wether + * montgomery multiplications will be use when applying operators + * '*' , '*='. Use this constructor for efficient use of + * Montgomery multiplication in a context with a fixed a modulus. + * Warning: do not use this function unless you know in detail + * about the implications of using the shared GFpModulus objects! + * @param mod shared pointer to the GFpModulus to be shared + * @param value the element value + * @param use_montgm whether this object will use Montgomery multiplication + */ + explicit GFpElement(std::tr1::shared_ptr<GFpModulus> const mod, + const BigInt& value, bool use_mongm = false); + + /** + * Copy constructor + * @param other The element to clone + */ + GFpElement(const GFpElement& other); + + /** + * Assignment operator. + * makes *this a totally independent object + * (gives *this independent modulus specific values). + + * @param other The element to assign to our object + */ + const GFpElement& operator=(const GFpElement& other); + + /** + * Works like the assignment operator, but lets + * *this share the modulus dependend value with other. + * Warning: do not use this function unless you know in detail about + * the implications of using + * the shared GFpModulus objects! + * @param other The element to assign to our object + */ + void share_assign(const GFpElement& other); + + /** + * Switch Montgomery multiplcation optimizations ON + */ + void turn_on_sp_red_mul() const; + + /** + * Switch Montgomery multiplcation optimizations OFF + */ + void turn_off_sp_red_mul() const; + + /** + * += Operator + * @param rhs the GFpElement to add to the local value + * @result *this + */ + GFpElement& operator+=(const GFpElement& rhs); + + /** + * -= Operator + * @param rhs the GFpElement to subtract from the local value + * @result *this + */ + GFpElement& operator-=(const GFpElement& rhs); + + /** + * *= Operator + * @param rhs the GFpElement to multiply with the local value + * @result *this + */ + GFpElement& operator*=(const GFpElement& rhs); + /** + * /= Operator + * @param rhs the GFpElement to divide the local value by + * @result *this + */ + GFpElement& operator/=(const GFpElement& rhs); + + /** + * *= Operator + * @param rhs the value to multiply with the local value + * @result *this + */ + GFpElement& operator*= (u32bit rhs); + + /** + * Negate internal value(*this *= -1 ) + * @return *this + */ + GFpElement& negate(); + + /** + * Assigns the inverse of *this to *this, i.e. + * *this = (*this)^(-1) + * @result *this + */ + GFpElement& inverse_in_place(); + + /** + * checks whether the value is zero (without provoking + * a backtransformation to the ordinary-residue) + * @result true, if the value is zero, false otherwise. + */ + bool is_zero(); + + /** + * return prime number of GF(p) + * @result a prime number + */ + const BigInt& get_p() const; + + /** + * Return the represented value in GF(p) + * @result The value in GF(p) + */ + const BigInt& get_value() const; + + /** + * Returns the shared pointer to the GFpModulus of *this. + * Warning: do not use this function unless you know in detail about + * the implications of using + * the shared GFpModulus objects! + * @result the shared pointer to the GFpModulus of *this + */ + inline std::tr1::shared_ptr<GFpModulus> const get_ptr_mod() const + { + return mp_mod; + } + + + /** + * Sets the shared pointer to the GFpModulus of *this. + * Warning: do not use this function unless you know in detail about + * the implications of using + * the shared GFpModulus objects! + * @param mod a shared pointer to a GFpModulus that will be held in *this + */ + void set_shrd_mod(std::tr1::shared_ptr<GFpModulus> const mod); + + /** + * Tells whether this GFpElement is currently transformed to it´ m-residue, + * i.e. in the form x_bar = x * r mod m. + * @result true if it is currently transformed to it´s m-residue. + */ + bool is_trf_to_mres() const; + + /** + * Transforms this to x_bar = x * r mod m + * @result return the value x_bar. + */ + const BigInt& get_mres() const; + + /** + * Check, if montgomery multiplication is used. + * @result true, if montgomery multiplication is used, false otherwise + */ + bool is_use_montgm() const + { + return m_use_montgm; + } + + /** + * Transforms the arguments in such way that either both + * are in m-residue representation (returns true) or both are + * in ordinary residue representation (returns false). + * m-residue is prefered in case of ambiguity. + * does not toggle m_use_montgm of the arguments. + * Don´t be confused about the constness of the arguments: + * the transformation between normal residue and m-residue is + * considered as leaving the object const. + * @param lhs the first operand to be aligned + * @param rhs the second operand to be aligned + * @result true if both are transformed to their m-residue, + * false it both are transformed to their normal residue. + */ + static bool align_operands_res(const GFpElement& lhs, const GFpElement& rhs); + + //friend declarations for non-member functions + + /** + * write a GFpElement to an output stream. + * @param output the output stream to write to + * @param elem the object to write + * @result the output stream + */ + friend std::ostream& operator<<(std::ostream& output, const GFpElement& elem); + + friend class Point_Coords_GFp; + + /** + * swaps the states of *this and other, does not throw! + * @param other The value to swap with + */ + void swap(GFpElement& other); + + }; + +// relational operators +bool operator==(const GFpElement& lhs, const GFpElement& rhs); +inline bool operator!=(const GFpElement& lhs, const GFpElement& rhs ) + { + return !operator==(lhs, rhs); + } + +// arithmetic operators +GFpElement operator+(const GFpElement& lhs, const GFpElement& rhs); +GFpElement operator-(const GFpElement& lhs, const GFpElement& rhs); +GFpElement operator-(const GFpElement& lhs); + +GFpElement operator*(const GFpElement& lhs, const GFpElement& rhs); +GFpElement operator/(const GFpElement& lhs, const GFpElement& rhs); +GFpElement operator* (const GFpElement& lhs, u32bit rhs); +GFpElement operator* (u32bit rhs, const GFpElement& lhs); + +// io operators +std::ostream& operator<<(std::ostream& output, const GFpElement& elem); + +// return (*this)^(-1) +GFpElement inverse(const GFpElement& elem); + +// encoding and decoding +SecureVector<byte> FE2OSP(const GFpElement& elem); +GFpElement OS2FEP(MemoryRegion<byte> const& os, BigInt p); + +inline void swap(GFpElement& x, GFpElement& y) + { + x.swap(y); + } + +} + +namespace std { + +template<> inline +void swap<Botan::GFpElement>(Botan::GFpElement& x, + Botan::GFpElement& y) + { + x.swap(y); + } + +} + +#endif diff --git a/botan/src/math/gfpmath/gfp_modulus.h b/botan/src/math/gfpmath/gfp_modulus.h new file mode 100644 index 0000000..b5c0857 --- /dev/null +++ b/botan/src/math/gfpmath/gfp_modulus.h @@ -0,0 +1,127 @@ +/****** + * Modulus and related data for a specific + * implementation of GF(p) (header file) + * + * (C) 2008 Martin Döring + * doering@cdc.informatik.tu-darmstadt.de + * Christoph Ludwig + * ludwig@fh-worms.de + * Falko Strenzke + * strenzke@flexsecure.de + ******/ + +#ifndef BOTAN_GFP_MODULUS_H__ +#define BOTAN_GFP_MODULUS_H__ + +#include <botan/bigint.h> + +namespace Botan +{ + +class BOTAN_DLL GFpElement; +/** +* This class represents a GFpElement modulus including the modulus related +* values necessary for the montgomery multiplication. +* +* Distributed under the terms of the Botan license +*/ +class BOTAN_DLL GFpModulus + { + friend class GFpElement; + private: + BigInt m_p; // the modulus itself + mutable BigInt m_p_dash; + mutable BigInt m_r; + mutable BigInt m_r_inv; + public: + + /** + * Construct a GF(P)-Modulus from a BigInt + */ + GFpModulus(BigInt p) + : m_p(p), + m_p_dash(), + m_r(), + m_r_inv() + {} + + /** + * Tells whether the precomputations necessary for the use of the + * montgomery multiplication have yet been established. + * @result true if the precomputated value are already available. + */ + inline bool has_precomputations() const + { + return(!m_p_dash.is_zero() && !m_r.is_zero() && !m_r_inv.is_zero()); + } + + /** + * Swaps this with another GFpModulus, does not throw. + * @param other the GFpModulus to swap *this with. + */ + inline void swap(GFpModulus& other) + { + m_p.swap(other.m_p); + m_p_dash.swap(other.m_p_dash); + m_r.swap(other.m_r); + m_r_inv.swap(other.m_r_inv); + } + + /** + * Tells whether the modulus of *this is equal to the argument. + * @param mod the modulus to compare this with + * @result true if the modulus of *this and the argument are equal. + */ + inline bool p_equal_to(const BigInt& mod) const + { + return (m_p == mod); + } + + /** + * Return the modulus of this GFpModulus. + * @result the modulus of *this. + */ + inline const BigInt& get_p() const + { + return m_p; + } + + /** + * returns the montgomery multiplication related value r. + * Warning: will be zero if precomputations have not yet been + * performed! + * @result r + */ + inline const BigInt& get_r() const + { + return m_r; + } + + /** + * returns the montgomery multiplication related value r^{-1}. + * Warning: will be zero if precomputations have not yet been + * performed! + * @result r^{-1} + */ + inline const BigInt& get_r_inv() const + { + return m_r_inv; + } + + /** + * returns the montgomery multiplication related value p'. + * Warning: will be zero if precomputations have not yet been + * performed! + * @result p' + */ + inline const BigInt& get_p_dash() const + { + return m_p_dash; + } + // default cp-ctor, op= are fine + }; + +} + +#endif + diff --git a/botan/src/math/gfpmath/info.txt b/botan/src/math/gfpmath/info.txt new file mode 100644 index 0000000..1a52144 --- /dev/null +++ b/botan/src/math/gfpmath/info.txt @@ -0,0 +1,22 @@ +realname "GF(p) Math" + +uses_tr1 yes + +load_on auto + +define BIGINT_GFP + +<add> +curve_gfp.cpp +curve_gfp.h +gfp_element.cpp +gfp_element.h +gfp_modulus.h +point_gfp.cpp +point_gfp.h +</add> + +<requires> +bigint +numbertheory +</requires> diff --git a/botan/src/math/gfpmath/point_gfp.cpp b/botan/src/math/gfpmath/point_gfp.cpp new file mode 100644 index 0000000..9139c3e --- /dev/null +++ b/botan/src/math/gfpmath/point_gfp.cpp @@ -0,0 +1,1154 @@ +/****** +* Arithmetic for point groups of elliptic curves +* over GF(p) (source file) +* +* (C) 2007 Martin Doering +* Christoph Ludwig +* Falko Strenzke +* 2008 Jack Lloyd +******/ + +#include <botan/point_gfp.h> +#include <botan/numthry.h> + +namespace Botan { + +// construct the point at infinity or a random point +PointGFp::PointGFp(const CurveGFp& curve) + : mC(curve), + mX(curve.get_p(), 0), + mY(curve.get_p(), 1), + mZ(curve.get_p(), 0), + mZpow2(curve.get_p(),0), + mZpow3(curve.get_p(),0), + mAZpow4(curve.get_p(),0), + mZpow2_set(false), + mZpow3_set(false), + mAZpow4_set(false) + { + // first set the point wide pointer + + set_shrd_mod(mC.get_ptr_mod()); + + } + +// construct a point given its jacobian projective coordinates +PointGFp::PointGFp(const CurveGFp& curve, const GFpElement& x, + const GFpElement& y, const GFpElement& z) + : mC(curve), + mX(x), + mY(y), + mZ(z), + mZpow2(curve.get_p(),0), + mZpow3(curve.get_p(),0), + mAZpow4(curve.get_p(),0), + mZpow2_set(false), + mZpow3_set(false), + mAZpow4_set(false) + { + set_shrd_mod(mC.get_ptr_mod()); + } +PointGFp::PointGFp ( const CurveGFp& curve, const GFpElement& x, + const GFpElement& y ) + :mC(curve), + mX(x), + mY(y), + mZ(curve.get_p(),1), + mZpow2(curve.get_p(),0), + mZpow3(curve.get_p(),0), + mAZpow4(curve.get_p(),0), + mZpow2_set(false), + mZpow3_set(false), + mAZpow4_set(false) + { + set_shrd_mod(mC.get_ptr_mod()); + } + +// copy constructor +PointGFp::PointGFp(const PointGFp& other) + : mC(other.mC), + mX(other.mX), + mY(other.mY), + mZ(other.mZ), + mZpow2(other.mZpow2), + mZpow3(other.mZpow3), + mAZpow4(other.mAZpow4), + mZpow2_set(other.mZpow2_set), + mZpow3_set(other.mZpow3_set), + mAZpow4_set(other.mAZpow4_set) + { + set_shrd_mod(mC.get_ptr_mod()); + } + +// assignment operator +const PointGFp& PointGFp::operator=(PointGFp const& other) + { + mC = other.get_curve(); + mX = other.get_jac_proj_x(); + mY = other.get_jac_proj_y(); + mZ = other.get_jac_proj_z(); + mZpow2 = GFpElement(other.mZpow2); + mZpow3 = GFpElement(other.mZpow3); + mAZpow4 = GFpElement(other.mAZpow4); + mZpow2_set = other.mZpow2_set; + mZpow3_set = other.mZpow3_set; + mAZpow4_set = other.mAZpow4_set; + set_shrd_mod(mC.get_ptr_mod()); + return *this; + } + +const PointGFp& PointGFp::assign_within_same_curve(PointGFp const& other) + { + mX = other.get_jac_proj_x(); + mY = other.get_jac_proj_y(); + mZ = other.get_jac_proj_z(); + mZpow2_set = false; + mZpow3_set = false; + mAZpow4_set = false; + // the rest stays! + return *this; + } + +void PointGFp::set_shrd_mod(std::tr1::shared_ptr<GFpModulus> p_mod) + { + mX.set_shrd_mod(p_mod); + mY.set_shrd_mod(p_mod); + mZ.set_shrd_mod(p_mod); + mZpow2.set_shrd_mod(p_mod); + mZpow3.set_shrd_mod(p_mod); + mAZpow4.set_shrd_mod(p_mod); + } + +void PointGFp::ensure_worksp() const + { + if (mp_worksp_gfp_el.get() != 0) + { + if ((*mp_worksp_gfp_el).size() == GFPEL_WKSP_SIZE) + { + return; + } + else + { + throw Invalid_State("encountered incorrect size for PointGFp´s GFpElement workspace"); + } + } + + mp_worksp_gfp_el = std::tr1::shared_ptr<std::vector<GFpElement> >(new std::vector<GFpElement>); + mp_worksp_gfp_el->reserve(9); + for (u32bit i=0; i<GFPEL_WKSP_SIZE; i++) + { + mp_worksp_gfp_el->push_back(GFpElement(1,0)); + + } + } + +// arithmetic operators +PointGFp& PointGFp::operator+=(const PointGFp& rhs) + { + if (is_zero()) + { + *this = rhs; + return *this; + } + if (rhs.is_zero()) + { + return *this; + } + ensure_worksp(); + + if (rhs.mZ == *(mC.get_mres_one())) + { + //U1 = mX; + (*mp_worksp_gfp_el)[0].share_assign(mX); + + //S1 = mY; + (*mp_worksp_gfp_el)[2].share_assign(mY); + } + else + { + if ((!rhs.mZpow2_set) || (!rhs.mZpow3_set)) + { + rhs.mZpow2 = rhs.mZ; + rhs.mZpow2 *= rhs.mZ; + rhs.mZpow3 = rhs.mZpow2; + rhs.mZpow3 *= rhs.mZ; + + rhs.mZpow2_set = true; + rhs.mZpow3_set = true; + } + //U1 = mX * rhs.mZpow2; + (*mp_worksp_gfp_el)[0].share_assign(mX); + (*mp_worksp_gfp_el)[0] *= rhs.mZpow2; + + //S1 = mY * rhs.mZpow3; + (*mp_worksp_gfp_el)[2].share_assign(mY); + (*mp_worksp_gfp_el)[2] *= rhs.mZpow3; + + } + if (mZ == *(mC.get_mres_one())) + { + //U2 = rhs.mX; + (*mp_worksp_gfp_el)[1].share_assign(rhs.mX); + + //S2 = rhs.mY; + (*mp_worksp_gfp_el)[3].share_assign(rhs.mY); + } + else + { + if ((!mZpow2_set) || (!mZpow3_set)) + { + // precomputation can´t be used, because *this changes anyway + mZpow2 = mZ; + mZpow2 *= mZ; + + mZpow3 = mZpow2; + mZpow3 *= mZ; + } + //U2 = rhs.mX * mZpow2; + (*mp_worksp_gfp_el)[1].share_assign(rhs.mX); + (*mp_worksp_gfp_el)[1] *= mZpow2; + + //S2 = rhs.mY * mZpow3; + (*mp_worksp_gfp_el)[3].share_assign(rhs.mY); + (*mp_worksp_gfp_el)[3] *= mZpow3; + + } + //GFpElement H(U2 - U1); + + (*mp_worksp_gfp_el)[4].share_assign((*mp_worksp_gfp_el)[1]); + (*mp_worksp_gfp_el)[4] -= (*mp_worksp_gfp_el)[0]; + + //GFpElement r(S2 - S1); + (*mp_worksp_gfp_el)[5].share_assign((*mp_worksp_gfp_el)[3]); + (*mp_worksp_gfp_el)[5] -= (*mp_worksp_gfp_el)[2]; + + //if(H.is_zero()) + if ((*mp_worksp_gfp_el)[4].is_zero()) + + { + if ((*mp_worksp_gfp_el)[5].is_zero()) + + { + mult2_in_place(); + return *this; + } + *this = PointGFp(mC); // setting myself to zero + return *this; + } + + //U2 = H * H; + (*mp_worksp_gfp_el)[1].share_assign((*mp_worksp_gfp_el)[4]); + (*mp_worksp_gfp_el)[1] *= (*mp_worksp_gfp_el)[4]; + + //S2 = U2 * H; + (*mp_worksp_gfp_el)[3].share_assign((*mp_worksp_gfp_el)[1]); + (*mp_worksp_gfp_el)[3] *= (*mp_worksp_gfp_el)[4]; + + //U2 *= U1; + (*mp_worksp_gfp_el)[1] *= (*mp_worksp_gfp_el)[0]; + + //GFpElement x(r*r - S2 - (U2+U2)); + (*mp_worksp_gfp_el)[6].share_assign((*mp_worksp_gfp_el)[5]); + (*mp_worksp_gfp_el)[6] *= (*mp_worksp_gfp_el)[5]; + (*mp_worksp_gfp_el)[6] -= (*mp_worksp_gfp_el)[3]; + (*mp_worksp_gfp_el)[6] -= (*mp_worksp_gfp_el)[1]; + (*mp_worksp_gfp_el)[6] -= (*mp_worksp_gfp_el)[1]; + + //GFpElement z(S1 * S2); + (*mp_worksp_gfp_el)[8].share_assign((*mp_worksp_gfp_el)[2]); + (*mp_worksp_gfp_el)[8] *= (*mp_worksp_gfp_el)[3]; + + //GFpElement y(r * (U2-x) - z); + (*mp_worksp_gfp_el)[7].share_assign((*mp_worksp_gfp_el)[1]); + (*mp_worksp_gfp_el)[7] -= (*mp_worksp_gfp_el)[6]; + (*mp_worksp_gfp_el)[7] *= (*mp_worksp_gfp_el)[5]; + (*mp_worksp_gfp_el)[7] -= (*mp_worksp_gfp_el)[8]; + + if (mZ == *(mC.get_mres_one())) + { + if (rhs.mZ != *(mC.get_mres_one())) + { + //z = rhs.mZ * H; + (*mp_worksp_gfp_el)[8].share_assign(rhs.mZ); + (*mp_worksp_gfp_el)[8] *= (*mp_worksp_gfp_el)[4]; + } + else + { + //z = H; + (*mp_worksp_gfp_el)[8].share_assign((*mp_worksp_gfp_el)[4]); + } + } + else if (rhs.mZ != *(mC.get_mres_one())) + { + //U1 = mZ * rhs.mZ; + (*mp_worksp_gfp_el)[0].share_assign(mZ); + (*mp_worksp_gfp_el)[0] *= rhs.mZ; + + //z = U1 * H; + (*mp_worksp_gfp_el)[8].share_assign((*mp_worksp_gfp_el)[0]); + (*mp_worksp_gfp_el)[8] *= (*mp_worksp_gfp_el)[4]; + + } + else + { + //z = mZ * H; + (*mp_worksp_gfp_el)[8].share_assign(mZ); + (*mp_worksp_gfp_el)[8] *= (*mp_worksp_gfp_el)[4]; + + } + mZpow2_set = false; + mZpow3_set = false; + mAZpow4_set = false; + + mX = (*mp_worksp_gfp_el)[6]; + mY = (*mp_worksp_gfp_el)[7]; + mZ = (*mp_worksp_gfp_el)[8]; + + return *this; + + } +PointGFp& PointGFp::operator-=(const PointGFp& rhs) + { + PointGFp minus_rhs = PointGFp(rhs).negate(); + + if (is_zero()) + { + *this = minus_rhs; + } + else + { + *this += minus_rhs; + } + return *this; + } + +PointGFp& PointGFp::mult_this_secure(const BigInt& scalar, + const BigInt& /*point_order*/, + const BigInt& /*max_secr*/) + { + // NOTE: FS: so far this is code duplication of op*=. + // we have to see how we deal with this. + // fact is that we will probably modify this function + // while evaluating the countermeasures + // whereas we probably will not start modifying the + // function operator*=. + // however, in the end both should be merged. + + // use montgomery mult. in this operation + this->turn_on_sp_red_mul(); + + std::tr1::shared_ptr<PointGFp> H(new PointGFp(this->mC)); + std::tr1::shared_ptr<PointGFp> tmp; // used for AADA + + PointGFp P(*this); + BigInt m(scalar); + + if (m < BigInt(0)) + { + m = -m; + P.negate(); + } + if (P.is_zero() || (m == BigInt(0))) + { + *this = *H; + return *this; + } + if (m == BigInt(1)) + { + return *this; + } + // +#ifdef CM_AADA +#ifndef CM_RAND_EXP + int max_secr_bits = max_secr.bits(); +#endif +#endif + + int mul_bits = m.bits(); // this is used for a determined number of loop runs in + // the mult_loop where leading zero´s are padded if necessary. + // Here we assign the value that will be used when no countermeasures are specified +#ifdef CM_RAND_EXP + u32bit rand_r_bit_len = 20; // Coron(99) proposes 20 bit for r + +#ifdef CM_AADA + + BigInt r_max(1); + +#endif // CM_AADA + + // use randomized exponent +#ifdef TA_COLL_T + static BigInt r_randexp; + if (new_rand) + { + r_randexp = random_integer(rand_r_bit_len); + } + //assert(!r_randexp.is_zero()); +#else + BigInt r_randexp(random_integer(rand_r_bit_len)); +#endif + + m += r_randexp * point_order; + // determine mul_bits... +#ifdef CM_AADA + // AADA with rand. Exp. + //assert(rand_r_bit_len > 0); + r_max <<= rand_r_bit_len; + r_max -= 1; + //assert(r_max.bits() == rand_r_bit_len); + mul_bits = (max_secr + point_order * r_max).bits(); +#else + // rand. Exp. without AADA + mul_bits = m.bits(); +#endif // CM_AADA + + +#endif // CM_RAND_EXP + + // determine mul_bits... +#if (CM_AADA == 1 && CM_RAND_EXP != 1) + + mul_bits = max_secr_bits; +#endif // CM_AADA without CM_RAND_EXP + + //assert(mul_bits != 0); + + + H = mult_loop(mul_bits-1, m, H, tmp, P); + + if (!H->is_zero()) // cannot convert if H == O + { + *this = H->get_z_to_one(); + }else + { + *this = *H; + } + mX.turn_off_sp_red_mul(); + mY.turn_off_sp_red_mul(); + mZ.turn_off_sp_red_mul(); + return *this; + } + +PointGFp& PointGFp::operator*=(const BigInt& scalar) + { + // use montgomery mult. in this operation + + this->turn_on_sp_red_mul(); + + PointGFp H(this->mC); // create as zero + H.turn_on_sp_red_mul(); + PointGFp P(*this); + P.turn_on_sp_red_mul(); + BigInt m(scalar); + if (m < BigInt(0)) + { + m = -m; + P.negate(); + } + if (P.is_zero() || (m == BigInt(0))) + { + *this = H; + return *this; + } + if (m == BigInt(1)) + { + //*this == P already + return *this; + } + + const int l = m.bits() - 1; + for (int i=l; i >=0; i--) + { + + H.mult2_in_place(); + if (m.get_bit(i)) + { + H += P; + } + } + + if (!H.is_zero()) // cannot convert if H == O + { + *this = H.get_z_to_one(); + }else + { + *this = H; + } + return *this; + } + +inline std::tr1::shared_ptr<PointGFp> PointGFp::mult_loop(int l, + const BigInt& m, + std::tr1::shared_ptr<PointGFp> H, + std::tr1::shared_ptr<PointGFp> tmp, + const PointGFp& P) + { + //assert(l >= (int)m.bits()- 1); + tmp = H; + std::tr1::shared_ptr<PointGFp> to_add(new PointGFp(P)); // we just need some point + // so that we can use op= + // inside the loop + for (int i=l; i >=0; i--) + { + H->mult2_in_place(); + +#ifndef CM_AADA + + if (m.get_bit(i)) + { + *H += P; + } +#else // (CM_AADA is in) + + if (H.get() == to_add.get()) + { + to_add = tmp; // otherwise all pointers might point to the same object + // and we always need two objects to be able to switch around + } + to_add->assign_within_same_curve(*H); + tmp = H; + *tmp += P; // tmp already points to H + + if (m.get_bit(i)) + { + H = tmp; // NOTE: assign the pointer, not the value! + // (so that the operation is fast and thus as difficult + // to detect as possible) + } + else + { + H = to_add; // NOTE: this is necessary, because the assignment + // "*tmp = ..." already changed what H pointed to + + + } +#endif // CM_AADA + + } + return H; + } + +PointGFp& PointGFp::negate() + { + if (!is_zero()) + { + mY.negate(); + } + return *this; + } + +// *this *= 2 +PointGFp& PointGFp::mult2_in_place() + { + if (is_zero()) + { + return *this; + } + if (mY.is_zero()) + { + + *this = PointGFp(mC); // setting myself to zero + return *this; + } + ensure_worksp(); + + (*mp_worksp_gfp_el)[0].share_assign(mY); + (*mp_worksp_gfp_el)[0] *= mY; + + //GFpElement S(mX * z); + (*mp_worksp_gfp_el)[1].share_assign(mX); + (*mp_worksp_gfp_el)[1] *= (*mp_worksp_gfp_el)[0]; + + //GFpElement x(S + S); + (*mp_worksp_gfp_el)[2].share_assign((*mp_worksp_gfp_el)[1]); + (*mp_worksp_gfp_el)[2] += (*mp_worksp_gfp_el)[1]; + + //S = x + x; + (*mp_worksp_gfp_el)[1].share_assign((*mp_worksp_gfp_el)[2]); + (*mp_worksp_gfp_el)[1] += (*mp_worksp_gfp_el)[2]; + + if (!mAZpow4_set) + { + if (mZ == *(mC.get_mres_one())) + { + mAZpow4 = mC.get_mres_a(); + mAZpow4_set = true; + } + else + { + if (!mZpow2_set) + { + mZpow2 = mZ; + mZpow2 *= mZ; + + mZpow2_set = true; + } + //x = mZpow2 * mZpow2; + (*mp_worksp_gfp_el)[2].share_assign(mZpow2); + (*mp_worksp_gfp_el)[2] *= mZpow2; + + //mAZpow4 = mC.get_mres_a() * x; + mAZpow4 = mC.get_mres_a(); + mAZpow4 *= (*mp_worksp_gfp_el)[2]; + + } + + } + + //GFpElement y(mX * mX); + (*mp_worksp_gfp_el)[3].share_assign(mX); + (*mp_worksp_gfp_el)[3] *= mX; + + //GFpElement M(y + y + y + mAZpow4); + (*mp_worksp_gfp_el)[4].share_assign((*mp_worksp_gfp_el)[3]); + (*mp_worksp_gfp_el)[4] += (*mp_worksp_gfp_el)[3]; + (*mp_worksp_gfp_el)[4] += (*mp_worksp_gfp_el)[3]; + (*mp_worksp_gfp_el)[4] += mAZpow4; + + //x = M * M - (S+S); + (*mp_worksp_gfp_el)[2].share_assign((*mp_worksp_gfp_el)[4]); + (*mp_worksp_gfp_el)[2] *= (*mp_worksp_gfp_el)[4]; + (*mp_worksp_gfp_el)[2] -= (*mp_worksp_gfp_el)[1]; + (*mp_worksp_gfp_el)[2] -= (*mp_worksp_gfp_el)[1]; + + //y = z * z; + (*mp_worksp_gfp_el)[3].share_assign((*mp_worksp_gfp_el)[0]); + (*mp_worksp_gfp_el)[3] *= (*mp_worksp_gfp_el)[0]; + + //GFpElement U(y + y); + (*mp_worksp_gfp_el)[5].share_assign((*mp_worksp_gfp_el)[3]); + (*mp_worksp_gfp_el)[5] += (*mp_worksp_gfp_el)[3]; + + //z = U + U; + (*mp_worksp_gfp_el)[0].share_assign((*mp_worksp_gfp_el)[5]); + (*mp_worksp_gfp_el)[0] += (*mp_worksp_gfp_el)[5]; + + //U = z + z; + (*mp_worksp_gfp_el)[5].share_assign((*mp_worksp_gfp_el)[0]); + (*mp_worksp_gfp_el)[5] += (*mp_worksp_gfp_el)[0]; + + //y = M * (S - x) - U; + (*mp_worksp_gfp_el)[3].share_assign((*mp_worksp_gfp_el)[1]); + (*mp_worksp_gfp_el)[3] -= (*mp_worksp_gfp_el)[2]; + (*mp_worksp_gfp_el)[3] *= (*mp_worksp_gfp_el)[4]; + (*mp_worksp_gfp_el)[3] -= (*mp_worksp_gfp_el)[5]; + + if (mZ != *(mC.get_mres_one())) + { + //z = mY * mZ; + (*mp_worksp_gfp_el)[0].share_assign(mY); + (*mp_worksp_gfp_el)[0] *= mZ; + + } + else + { + //z = mY; + (*mp_worksp_gfp_el)[0].share_assign(mY); + + } + //z = z + z; + (*mp_worksp_gfp_el)[6].share_assign((*mp_worksp_gfp_el)[0]); + (*mp_worksp_gfp_el)[0] += (*mp_worksp_gfp_el)[6]; + + //mX = x; + //mY = y; + //mZ = z; + mX = (*mp_worksp_gfp_el)[2]; + mY = (*mp_worksp_gfp_el)[3]; + mZ = (*mp_worksp_gfp_el)[0]; + + mZpow2_set = false; + mZpow3_set = false; + mAZpow4_set = false; + return *this; + } + +void PointGFp::turn_on_sp_red_mul() const + { + mX.turn_on_sp_red_mul(); + mY.turn_on_sp_red_mul(); + mZ.turn_on_sp_red_mul(); + + // also pretransform, otherwise + // we might have bad results with respect to + // performance because + // additions/subtractions in mult2_in_place() + // and op+= spread untransformed GFpElements + mX.get_mres(); + mY.get_mres(); + mZ.get_mres(); + + mZpow2.turn_on_sp_red_mul(); + mZpow3.turn_on_sp_red_mul(); + mAZpow4.turn_on_sp_red_mul(); + } +// getters + +/** +* returns a point equivalent to *this but were +* Z has value one, i.e. x and y correspond to +* their values in affine coordinates +* +* Distributed under the terms of the Botan license +*/ +PointGFp const PointGFp::get_z_to_one() const + { + return PointGFp(*this).set_z_to_one(); + } + +/** +* changes the representation of *this so that +* Z has value one, i.e. x and y correspond to +* their values in affine coordinates. +* returns *this. +*/ +const PointGFp& PointGFp::set_z_to_one() const + { + if (!(mZ.get_value() == BigInt(1)) && !(mZ.get_value() == BigInt(0))) + { + GFpElement z = inverse(mZ); + GFpElement z2 = z * z; + z *= z2; + GFpElement x = mX * z2; + GFpElement y = mY * z; + mZ = GFpElement(mC.get_p(), BigInt(1)); + mX = x; + mY = y; + } + else + { + if (mZ.get_value() == BigInt(0)) + { + throw Illegal_Transformation("cannot convert Z to one"); + } + } + return *this; // mZ = 1 already + } + +const CurveGFp PointGFp::get_curve() const + { + return mC; + } + +GFpElement const PointGFp::get_affine_x() const + { + + if (is_zero()) + { + throw Illegal_Transformation("cannot convert to affine"); + + } + /*if(!mZpow2_set) + {*/ + mZpow2 = mZ * mZ; + mZpow2_set = true; + //} + //assert(mZpow2 == mZ*mZ); + GFpElement z2 = mZpow2; + return mX * z2.inverse_in_place(); + } + +GFpElement const PointGFp::get_affine_y() const + { + + if (is_zero()) + { + throw Illegal_Transformation("cannot convert to affine"); + + } + /*if(!mZpow3_set ) + {*/ + mZpow3 = mZ * mZ * mZ; + mZpow3_set = true; + //} + //assert(mZpow3 == mZ * mZ *mZ); + GFpElement z3 = mZpow3; + return mY * z3.inverse_in_place(); + } + +GFpElement const PointGFp::get_jac_proj_x() const + { + return GFpElement(mX); + } + +GFpElement const PointGFp::get_jac_proj_y() const + { + return GFpElement(mY); + } + +GFpElement const PointGFp::get_jac_proj_z() const + { + return GFpElement(mZ); + } + +// Is this the point at infinity? +bool PointGFp::is_zero() const + { + return(mX.is_zero() && mZ.is_zero()); + //NOTE: the calls to GFpElement::is_zero() instead of getting the value and + // and comparing it are import because they do not provoke backtransformations + // to the ordinary residue. + } + +// Is the point still on the curve?? +// (If everything is correct, the point is always on its curve; then the +// function will return silently. If Oskar managed to corrupt this object's state, +// then it will throw an exception.) + +void PointGFp::check_invariants() const + { + if (is_zero()) + { + return; + } + const GFpElement y2 = mY * mY; + const GFpElement x3 = mX * mX * mX; + + if (mZ.get_value() == BigInt(1)) + { + GFpElement ax = mC.get_a() * mX; + if(y2 != (x3 + ax + mC.get_b())) + { + throw Illegal_Point(); + } + + } + + mZpow2 = mZ * mZ; + mZpow2_set = true; + mZpow3 = mZpow2 * mZ; + mZpow3_set = true; + mAZpow4 = mZpow3 * mZ * mC.get_a(); + mAZpow4_set = true; + const GFpElement aXZ4 = mAZpow4 * mX; + const GFpElement bZ6 = mC.get_b() * mZpow3 * mZpow3; + + if (y2 != (x3 + aXZ4 + bZ6)) + throw Illegal_Point(); + } + +// swaps the states of *this and other, does not throw! +void PointGFp::swap(PointGFp& other) + { + mC.swap(other.mC); + mX.swap(other.mX); + mY.swap(other.mY); + mZ.swap(other.mZ); + mZpow2.swap(other.mZpow2); + mZpow3.swap(other.mZpow3); + mAZpow4.swap(other.mAZpow4); + std::swap<bool>(mZpow2_set, other.mZpow2_set); + std::swap<bool>(mZpow3_set, other.mZpow3_set); + std::swap<bool>(mAZpow4_set, other.mAZpow4_set); + } + +PointGFp const mult2(const PointGFp& point) + { + return (PointGFp(point)).mult2_in_place(); + } + +bool operator==(const PointGFp& lhs, PointGFp const& rhs) + { + if (lhs.is_zero() && rhs.is_zero()) + { + return true; + } + if ((lhs.is_zero() && !rhs.is_zero()) || (!lhs.is_zero() && rhs.is_zero())) + { + return false; + } + // neither operand is zero, so we can call get_z_to_one() + //assert(!lhs.is_zero()); + //assert(!rhs.is_zero()); + PointGFp aff_lhs = lhs.get_z_to_one(); + PointGFp aff_rhs = rhs.get_z_to_one(); + return (aff_lhs.get_curve() == aff_rhs.get_curve() && + aff_lhs.get_jac_proj_x() == aff_rhs.get_jac_proj_x() && + aff_lhs.get_jac_proj_y() == aff_rhs.get_jac_proj_y()); + } + +// arithmetic operators +PointGFp operator+(const PointGFp& lhs, PointGFp const& rhs) + { + PointGFp tmp(lhs); + return tmp += rhs; + } + +PointGFp operator-(const PointGFp& lhs, PointGFp const& rhs) + { + PointGFp tmp(lhs); + return tmp -= rhs; + } + +PointGFp operator-(const PointGFp& lhs) + { + return PointGFp(lhs).negate(); + } + +PointGFp operator*(const BigInt& scalar, const PointGFp& point) + { + PointGFp result(point); + return result *= scalar; + } + +PointGFp operator*(const PointGFp& point, const BigInt& scalar) + { + PointGFp result(point); + return result *= scalar; + } + +PointGFp mult_point_secure(const PointGFp& point, const BigInt& scalar, + const BigInt& point_order, const BigInt& max_secret) + { + PointGFp result(point); + result.mult_this_secure(scalar, point_order, max_secret); + return result; + } + +// encoding and decoding +SecureVector<byte> EC2OSP(const PointGFp& point, byte format) + { + SecureVector<byte> result; + if (format == PointGFp::UNCOMPRESSED) + { + result = encode_uncompressed(point); + } + else if (format == PointGFp::COMPRESSED) + { + result = encode_compressed(point); + + } + else if (format == PointGFp::HYBRID) + { + result = encode_hybrid(point); + } + else + { + throw Format_Error("illegal point encoding format specification"); + } + return result; + } +SecureVector<byte> encode_compressed(const PointGFp& point) + { + + + if (point.is_zero()) + { + SecureVector<byte> result (1); + result[0] = 0; + return result; + + } + u32bit l = point.get_curve().get_p().bits(); + int dummy = l & 7; + if (dummy != 0) + { + l += 8 - dummy; + } + l /= 8; + SecureVector<byte> result (l+1); + result[0] = 2; + BigInt x = point.get_affine_x().get_value(); + SecureVector<byte> bX = BigInt::encode_1363(x, l); + result.copy(1, bX.begin(), bX.size()); + BigInt y = point.get_affine_y().get_value(); + if (y.get_bit(0)) + { + result[0] |= 1; + } + return result; + } + + +SecureVector<byte> encode_uncompressed(const PointGFp& point) + { + if (point.is_zero()) + { + SecureVector<byte> result (1); + result[0] = 0; + return result; + } + u32bit l = point.get_curve().get_p().bits(); + int dummy = l & 7; + if (dummy != 0) + { + l += 8 - dummy; + } + l /= 8; + SecureVector<byte> result (2*l+1); + result[0] = 4; + BigInt x = point.get_affine_x().get_value(); + BigInt y = point.get_affine_y().get_value(); + SecureVector<byte> bX = BigInt::encode_1363(x, l); + SecureVector<byte> bY = BigInt::encode_1363(y, l); + result.copy(1, bX.begin(), l); + result.copy(l+1, bY.begin(), l); + return result; + + } + +SecureVector<byte> encode_hybrid(const PointGFp& point) + { + if (point.is_zero()) + { + SecureVector<byte> result (1); + result[0] = 0; + return result; + } + u32bit l = point.get_curve().get_p().bits(); + int dummy = l & 7; + if (dummy != 0) + { + l += 8 - dummy; + } + l /= 8; + SecureVector<byte> result (2*l+1); + result[0] = 6; + BigInt x = point.get_affine_x().get_value(); + BigInt y = point.get_affine_y().get_value(); + SecureVector<byte> bX = BigInt::encode_1363(x, l); + SecureVector<byte> bY = BigInt::encode_1363(y, l); + result.copy(1, bX.begin(), bX.size()); + result.copy(l+1, bY.begin(), bY.size()); + if (y.get_bit(0)) + { + result[0] |= 1; + } + return result; + } + +PointGFp OS2ECP(MemoryRegion<byte> const& os, const CurveGFp& curve) + { + if (os.size() == 1 && os[0] == 0) + { + return PointGFp(curve); // return zero + } + SecureVector<byte> bX; + SecureVector<byte> bY; + + GFpElement x(1,0); + GFpElement y(1,0); + GFpElement z(1,0); + + const byte pc = os[0]; + BigInt bi_dec_x; + BigInt bi_dec_y; + switch (pc) + { + case 2: + case 3: + //compressed form + bX = SecureVector<byte>(os.size() - 1); + bX.copy(os.begin()+1, os.size()-1); + + /* Problem wäre, wenn decode() das erste bit als Vorzeichen interpretiert. + *--------------------- + * AW(FS): decode() interpretiert das erste Bit nicht als Vorzeichen + */ + bi_dec_x = BigInt::decode(bX, bX.size()); + x = GFpElement(curve.get_p(), bi_dec_x); + bool yMod2; + yMod2 = (pc & 1) == 1; + y = PointGFp::decompress(yMod2, x, curve); + break; + case 4: + // uncompressed form + int l; + l = (os.size() -1)/2; + bX = SecureVector<byte>(l); + bY = SecureVector<byte>(l); + bX.copy(os.begin()+1, l); + bY.copy(os.begin()+1+l, l); + bi_dec_x = BigInt::decode(bX.begin(), bX.size()); + + bi_dec_y = BigInt::decode(bY.begin(),bY.size()); + x = GFpElement(curve.get_p(), bi_dec_x); + y = GFpElement(curve.get_p(), bi_dec_y); + break; + + case 6: + case 7: + //hybrid form + l = (os.size() - 1)/2; + bX = SecureVector<byte>(l); + bY = SecureVector<byte>(l); + bX.copy(os.begin() + 1, l); + bY.copy(os.begin()+1+l, l); + yMod2 = (pc & 0x01) == 1; + if (!(PointGFp::decompress(yMod2, x, curve) == y)) + { + throw Illegal_Point("error during decoding hybrid format"); + } + break; + default: + throw Format_Error("encountered illegal format specification while decoding point"); + } + z = GFpElement(curve.get_p(), BigInt(1)); + //assert((x.is_trf_to_mres() && x.is_use_montgm()) || !x.is_trf_to_mres()); + //assert((y.is_trf_to_mres() && y.is_use_montgm()) || !y.is_trf_to_mres()); + //assert((z.is_trf_to_mres() && z.is_use_montgm()) || !z.is_trf_to_mres()); + PointGFp result(curve, x, y, z); + result.check_invariants(); + //assert((result.get_jac_proj_x().is_trf_to_mres() && result.get_jac_proj_x().is_use_montgm()) || !result.get_jac_proj_x().is_trf_to_mres()); + //assert((result.get_jac_proj_y().is_trf_to_mres() && result.get_jac_proj_y().is_use_montgm()) || !result.get_jac_proj_y().is_trf_to_mres()); + //assert((result.get_jac_proj_z().is_trf_to_mres() && result.get_jac_proj_z().is_use_montgm()) || !result.get_jac_proj_z().is_trf_to_mres()); + return result; + } + +GFpElement PointGFp::decompress(bool yMod2, const GFpElement& x, + const CurveGFp& curve) + { + BigInt xVal = x.get_value(); + BigInt xpow3 = xVal * xVal * xVal; + BigInt g = curve.get_a().get_value() * xVal; + g += xpow3; + g += curve.get_b().get_value(); + g = g%curve.get_p(); + BigInt z = ressol(g, curve.get_p()); + + if(z < 0) + throw Illegal_Point("error during decompression"); + + bool zMod2 = z.get_bit(0); + if ((zMod2 && ! yMod2) || (!zMod2 && yMod2)) + { + z = curve.get_p() - z; + } + return GFpElement(curve.get_p(),z); + } + +PointGFp const create_random_point(RandomNumberGenerator& rng, + const CurveGFp& curve) + { + + // create a random point + GFpElement mX(1,1); + GFpElement mY(1,1); + GFpElement mZ(1,1); + GFpElement minusOne(curve.get_p(), BigInt(BigInt::Negative,1)); + mY = minusOne; + GFpElement y2(1,1); + GFpElement x(1,1); + + while (mY == minusOne) + { + BigInt value(rng, curve.get_p().bits()); + mX = GFpElement(curve.get_p(),value); + y2 = curve.get_a() * mX; + x = mX * mX; + x *= mX; + y2 += (x + curve.get_b()); + + value = ressol(y2.get_value(), curve.get_p()); + + if(value < 0) + mY = minusOne; + else + mY = GFpElement(curve.get_p(), value); + } + mZ = GFpElement(curve.get_p(), BigInt(1)); + + return PointGFp(curve, mX, mY, mZ); + } + +} // namespace Botan diff --git a/botan/src/math/gfpmath/point_gfp.h b/botan/src/math/gfpmath/point_gfp.h new file mode 100644 index 0000000..771605e --- /dev/null +++ b/botan/src/math/gfpmath/point_gfp.h @@ -0,0 +1,315 @@ +/* +* Arithmetic over GF(p) +* +* (C) 2007 Martin Doering +* Christoph Ludwig +* Falko Strenzke +* (C) 2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_POINT_GFP_H__ +#define BOTAN_POINT_GFP_H__ + +#include <botan/curve_gfp.h> +#include <botan/gfp_element.h> +#include <botan/bigint.h> +#include <botan/exceptn.h> +#include <vector> + +namespace Botan { + +struct Illegal_Point : public Exception + { + Illegal_Point(const std::string& err = "") : Exception(err) {} + }; + +/** +* This class represents one point on a curve of GF(p). +*/ +class BOTAN_DLL PointGFp + { + public: + /** + * uncompressed encoding byte value + */ + static const int UNCOMPRESSED = 0; + + /** + * compressed encoding byte value + */ + static const int COMPRESSED = 1; + + /** + * hybrid encoding byte value + */ + static const int HYBRID = 2; + + /** + * Construct the point O + * @param curve The base curve + */ + explicit PointGFp(const CurveGFp& curve); + + /** + * Construct a point given its affine coordinates + * @param curve the base curve + * @param x affine x coordinate + * @param y affine y coordinate + */ + explicit PointGFp(const CurveGFp& curve, GFpElement const& x, + GFpElement const& y); + + /** + * Construct a point given its jacobian projective coordinates + * @param curve the base curve + * @param x jacobian projective x coordinate + * @param y jacobian projective y coordinate + * @param z jacobian projective y coordinate + */ + explicit PointGFp(const CurveGFp& curve, GFpElement const& x, + GFpElement const& y, GFpElement const& z); + + /** + * copy constructor + * @param other the value to clone + */ + PointGFp(const PointGFp& other); + + /** + * assignment operator + * @param other The point to use as source for the assignment + */ + const PointGFp& operator=(const PointGFp& other); + + /** + * assign another point which is on the same curve as *this + * @param other The point to use as source for the assignment + */ + const PointGFp& assign_within_same_curve(const PointGFp& other); + + + + /** + * += Operator + * @param rhs the PointGFp to add to the local value + * @result resulting PointGFp + */ + PointGFp& operator+=(const PointGFp& rhs); + + /** + * -= Operator + * @param rhs the PointGFp to subtract from the local value + * @result resulting PointGFp + */ + PointGFp& operator-=(const PointGFp& rhs); + + /** + * *= Operator + * This function turns on the the special reduction multiplication + * itself for fast computation, turns it off again when finished. + * @param scalar the PointGFp to multiply with *this + * @result resulting PointGFp + */ + PointGFp& operator*=(const BigInt& scalar); + + /** + * the equivalent to operator*= with countermeasures against + * sidechannel attacks, using the randomized exponent + * and add-and-double-always + * countermeasures (suitable for ECDSA and ECKAEG) + * @param scalar the scalar to multiply the point with + * @param point_order a multiple of the order of the point + *(= n * k in the general case; k is the cofactor) + * @param max_secr the maximal size of the scalar + * (will usually be n-1 ) + * @result resulting PointGFp + */ + PointGFp& mult_this_secure(const BigInt& scalar, + const BigInt& point_order, + const BigInt& max_secr + ); + + /** + * Negate internal value(*this *= -1 ) + * @return *this + */ + PointGFp& negate(); + + /** + * Multiply the point by two(*this *= 2 ) + * @return *this + */ + PointGFp& mult2_in_place(); + + /** + * Set z coordinate to one. + * @return *this + */ + const PointGFp& set_z_to_one() const; + + /** + * Turn on the special reduction multiplication (i.e. the + * Montgomery multiplication in the current implementation) for + * the coordinates. This enables fast execution of mult2_in_place() + * and operator+=(). + */ + void turn_on_sp_red_mul() const; + + /** + * Return a point + * where the coordinates are transformed + * so that z equals one, + * thus x and y have just the affine values. + * @result *this + */ + PointGFp const get_z_to_one() const; + + /** + * Return base curve of this point + * @result the curve over GF(p) of this point + */ + CurveGFp const get_curve() const; + + /** + * get affine x coordinate + * @result affine x coordinate + */ + GFpElement const get_affine_x() const; + + /** + * get affine y coordinate + * @result affine y coordinate + */ + GFpElement const get_affine_y() const; + + /** + * get the jacobian projective x coordinate + * @result jacobian projective x coordinate + */ + GFpElement const get_jac_proj_x() const; + + /** + * get the jacobian projective y coordinate + * @result jacobian projective y coordinate + */ + GFpElement const get_jac_proj_y() const; + + /** + * get the jacobian projective z coordinate + * @result jacobian projective z coordinate + */ + GFpElement const get_jac_proj_z() const; + + /** + * Is this the point at infinity? + * @result true, if this point is at infinity, false otherwise. + */ + bool is_zero() const; + + /** + * Checks whether the point is to be found on the underlying curve. + * Throws an Invalid_Point exception in case of detecting that the point + * does not satisfy the curve equation. + * To be used to ensure against fault attacks. + */ + void check_invariants() const; + + + /** + * swaps the states of *this and other, does not throw! + * @param other the object to swap values with + */ + void swap(PointGFp& other); + + /** + * Sets the shared pointer to the GFpModulus that will be + * held in *this, specifically the various members of *this. + * Warning: do not use this function unless you know in detail about + * the implications of using + * the shared GFpModulus objects! + * Do NOT spread a shared pointer to GFpModulus over different + * threads! + * @param mod a shared pointer to a GFpModulus that will + * be held in the members *this + */ + void set_shrd_mod(std::tr1::shared_ptr<GFpModulus> p_mod); + + static GFpElement decompress(bool yMod2, GFpElement const& x, const CurveGFp& curve); + + private: + static const u32bit GFPEL_WKSP_SIZE = 9; + void ensure_worksp() const; + + inline std::tr1::shared_ptr<PointGFp> mult_loop(int l, const BigInt& m, + std::tr1::shared_ptr<PointGFp> H, + std::tr1::shared_ptr<PointGFp> tmp, + const PointGFp& P); + + CurveGFp mC; + mutable GFpElement mX; // NOTE: these values must be mutable (affine<->proj) + mutable GFpElement mY; + mutable GFpElement mZ; + mutable GFpElement mZpow2; // mZ^2 + mutable GFpElement mZpow3; // mZ^3 + mutable GFpElement mAZpow4; // mA*mZ^4 + mutable bool mZpow2_set; + mutable bool mZpow3_set; + mutable bool mAZpow4_set; + mutable std::tr1::shared_ptr<std::vector<GFpElement> > mp_worksp_gfp_el; + + }; + +// relational operators +bool operator==(const PointGFp& lhs, const PointGFp& rhs); +inline bool operator!=(const PointGFp& lhs, const PointGFp& rhs ) + { + return !operator==(lhs, rhs); + } + +// arithmetic operators +PointGFp operator+(const PointGFp& lhs, const PointGFp& rhs); +PointGFp operator-(const PointGFp& lhs, const PointGFp& rhs); +PointGFp operator-(const PointGFp& lhs); + +PointGFp operator*(const BigInt& scalar, const PointGFp& point); +PointGFp operator*(const PointGFp& point, const BigInt& scalar); +PointGFp mult_point_secure(const PointGFp& point, + const BigInt& scalar, + const BigInt& point_order, + const BigInt& max_secret); + +PointGFp const mult2(const PointGFp& point); + +PointGFp const create_random_point(RandomNumberGenerator& rng, + const CurveGFp& curve); + +// encoding and decoding +SecureVector<byte> EC2OSP(const PointGFp& point, byte format); +PointGFp OS2ECP(MemoryRegion<byte> const& os, const CurveGFp& curve); + +SecureVector<byte> encode_uncompressed(const PointGFp& point); // maybe make private +SecureVector<byte> encode_hybrid(const PointGFp& point); // maybe make private +SecureVector<byte> encode_compressed(const PointGFp& point); // maybe make private + +// swaps the states of point1 and point2, does not throw! +// cf. Meyers, Item 25 +inline +void swap(PointGFp& point1, PointGFp& point2 ) + { + point1.swap(point2); + } + +} // namespace Botan + +namespace std { + +// swaps the states of point1 and point2, does not throw! +// cf. Meyers, Item 25 +template<> inline void +swap<Botan::PointGFp>(Botan::PointGFp& x, Botan::PointGFp& y) { x.swap(y); } + +} // namespace std + +#endif diff --git a/botan/src/math/numbertheory/blinding.cpp b/botan/src/math/numbertheory/blinding.cpp new file mode 100644 index 0000000..c6a3fd1 --- /dev/null +++ b/botan/src/math/numbertheory/blinding.cpp @@ -0,0 +1,49 @@ +/* +* Blinder +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/blinding.h> +#include <botan/numthry.h> + +namespace Botan { + +/* +* Blinder Constructor +*/ +Blinder::Blinder(const BigInt& e, const BigInt& d, const BigInt& n) + { + if(e < 1 || d < 1 || n < 1) + throw Invalid_Argument("Blinder: Arguments too small"); + + reducer = Modular_Reducer(n); + this->e = e; + this->d = d; + } + +/* +* Blind a number +*/ +BigInt Blinder::blind(const BigInt& i) const + { + if(!reducer.initialized()) + return i; + + e = reducer.square(e); + d = reducer.square(d); + return reducer.multiply(i, e); + } + +/* +* Unblind a number +*/ +BigInt Blinder::unblind(const BigInt& i) const + { + if(!reducer.initialized()) + return i; + return reducer.multiply(i, d); + } + +} diff --git a/botan/src/math/numbertheory/blinding.h b/botan/src/math/numbertheory/blinding.h new file mode 100644 index 0000000..5f7f9e6 --- /dev/null +++ b/botan/src/math/numbertheory/blinding.h @@ -0,0 +1,34 @@ +/* +* Blinder +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_BLINDER_H__ +#define BOTAN_BLINDER_H__ + +#include <botan/bigint.h> +#include <botan/reducer.h> + +namespace Botan { + +/* +* Blinding Function Object +*/ +class BOTAN_DLL Blinder + { + public: + BigInt blind(const BigInt&) const; + BigInt unblind(const BigInt&) const; + + Blinder() {} + Blinder(const BigInt&, const BigInt&, const BigInt&); + private: + Modular_Reducer reducer; + mutable BigInt e, d; + }; + +} + +#endif diff --git a/botan/src/math/numbertheory/def_powm.h b/botan/src/math/numbertheory/def_powm.h new file mode 100644 index 0000000..472c865 --- /dev/null +++ b/botan/src/math/numbertheory/def_powm.h @@ -0,0 +1,64 @@ +/* +* Modular Exponentiation +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_DEFAULT_MODEXP_H__ +#define BOTAN_DEFAULT_MODEXP_H__ + +#include <botan/pow_mod.h> +#include <botan/reducer.h> +#include <vector> + +namespace Botan { + +/* +* Fixed Window Exponentiator +*/ +class BOTAN_DLL Fixed_Window_Exponentiator : public Modular_Exponentiator + { + public: + void set_exponent(const BigInt&); + void set_base(const BigInt&); + BigInt execute() const; + + Modular_Exponentiator* copy() const + { return new Fixed_Window_Exponentiator(*this); } + + Fixed_Window_Exponentiator(const BigInt&, Power_Mod::Usage_Hints); + private: + Modular_Reducer reducer; + BigInt exp; + u32bit window_bits; + std::vector<BigInt> g; + Power_Mod::Usage_Hints hints; + }; + +/* +* Montgomery Exponentiator +*/ +class BOTAN_DLL Montgomery_Exponentiator : public Modular_Exponentiator + { + public: + void set_exponent(const BigInt&); + void set_base(const BigInt&); + BigInt execute() const; + + Modular_Exponentiator* copy() const + { return new Montgomery_Exponentiator(*this); } + + Montgomery_Exponentiator(const BigInt&, Power_Mod::Usage_Hints); + private: + BigInt exp, modulus; + BigInt R2, R_mod; + std::vector<BigInt> g; + word mod_prime; + u32bit mod_words, exp_bits, window_bits; + Power_Mod::Usage_Hints hints; + }; + +} + +#endif diff --git a/botan/src/math/numbertheory/dsa_gen.cpp b/botan/src/math/numbertheory/dsa_gen.cpp new file mode 100644 index 0000000..83646e5 --- /dev/null +++ b/botan/src/math/numbertheory/dsa_gen.cpp @@ -0,0 +1,135 @@ +/* +* DSA Parameter Generation +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/numthry.h> +#include <botan/algo_factory.h> +#include <botan/hash.h> +#include <botan/parsing.h> +#include <algorithm> +#include <memory> + +namespace Botan { + +namespace { + +/* +* Check if this size is allowed by FIPS 186-3 +*/ +bool fips186_3_valid_size(u32bit pbits, u32bit qbits) + { + if(qbits == 160) + return (pbits == 512 || pbits == 768 || pbits == 1024); + + if(qbits == 224) + return (pbits == 2048); + + if(qbits == 256) + return (pbits == 2048 || pbits == 3072); + + return false; + } + +} + +/* +* Attempt DSA prime generation with given seed +*/ +bool generate_dsa_primes(RandomNumberGenerator& rng, + Algorithm_Factory& af, + BigInt& p, BigInt& q, + u32bit pbits, u32bit qbits, + const MemoryRegion<byte>& seed_c) + { + if(!fips186_3_valid_size(pbits, qbits)) + throw Invalid_Argument( + "FIPS 186-3 does not allow DSA domain parameters of " + + to_string(pbits) + "/" + to_string(qbits) + " bits long"); + + if(seed_c.size() * 8 < qbits) + throw Invalid_Argument( + "Generating a DSA parameter set with a " + to_string(qbits) + + "long q requires a seed at least as many bits long"); + + std::auto_ptr<HashFunction> hash( + af.make_hash_function("SHA-" + to_string(qbits))); + + const u32bit HASH_SIZE = hash->OUTPUT_LENGTH; + + class Seed + { + public: + Seed(const MemoryRegion<byte>& s) : seed(s) {} + + operator MemoryRegion<byte>& () { return seed; } + + Seed& operator++() + { + for(u32bit j = seed.size(); j > 0; --j) + if(++seed[j-1]) + break; + return (*this); + } + private: + SecureVector<byte> seed; + }; + + Seed seed(seed_c); + + q.binary_decode(hash->process(seed)); + q.set_bit(qbits-1); + q.set_bit(0); + + if(!is_prime(q, rng)) + return false; + + const u32bit n = (pbits-1) / (HASH_SIZE * 8), + b = (pbits-1) % (HASH_SIZE * 8); + + BigInt X; + SecureVector<byte> V(HASH_SIZE * (n+1)); + + for(u32bit j = 0; j != 4096; ++j) + { + for(u32bit k = 0; k <= n; ++k) + { + ++seed; + hash->update(seed); + hash->final(V + HASH_SIZE * (n-k)); + } + + X.binary_decode(V + (HASH_SIZE - 1 - b/8), + V.size() - (HASH_SIZE - 1 - b/8)); + X.set_bit(pbits-1); + + p = X - (X % (2*q) - 1); + + if(p.bits() == pbits && is_prime(p, rng)) + return true; + } + return false; + } + +/* +* Generate DSA Primes +*/ +SecureVector<byte> generate_dsa_primes(RandomNumberGenerator& rng, + Algorithm_Factory& af, + BigInt& p, BigInt& q, + u32bit pbits, u32bit qbits) + { + SecureVector<byte> seed(qbits/8); + + while(true) + { + rng.randomize(seed, seed.size()); + + if(generate_dsa_primes(rng, af, p, q, pbits, qbits, seed)) + return seed; + } + } + +} diff --git a/botan/src/math/numbertheory/info.txt b/botan/src/math/numbertheory/info.txt new file mode 100644 index 0000000..1595c73 --- /dev/null +++ b/botan/src/math/numbertheory/info.txt @@ -0,0 +1,33 @@ +realname "Math Functions" + +load_on auto + +define BIGINT_MATH + +<add> +blinding.cpp +blinding.h +def_powm.h +dsa_gen.cpp +jacobi.cpp +make_prm.cpp +mp_numth.cpp +numthry.cpp +numthry.h +pow_mod.cpp +pow_mod.h +powm_fw.cpp +powm_mnt.cpp +primes.cpp +reducer.cpp +reducer.h +ressol.cpp +</add> + +<requires> +algo_factory +bigint +hash +libstate +rng +</requires> diff --git a/botan/src/math/numbertheory/jacobi.cpp b/botan/src/math/numbertheory/jacobi.cpp new file mode 100644 index 0000000..2ad05ff --- /dev/null +++ b/botan/src/math/numbertheory/jacobi.cpp @@ -0,0 +1,53 @@ +/* +* Jacobi Function +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/numthry.h> + +namespace Botan { + +/* +* Calculate the Jacobi symbol +*/ +s32bit jacobi(const BigInt& a, const BigInt& n) + { + if(a.is_negative()) + throw Invalid_Argument("jacobi: first argument must be non-negative"); + if(n.is_even() || n < 2) + throw Invalid_Argument("jacobi: second argument must be odd and > 1"); + + BigInt x = a, y = n; + s32bit J = 1; + + while(y > 1) + { + x %= y; + if(x > y / 2) + { + x = y - x; + if(y % 4 == 3) + J = -J; + } + if(x.is_zero()) + return 0; + + u32bit shifts = low_zero_bits(x); + x >>= shifts; + if(shifts % 2) + { + word y_mod_8 = y % 8; + if(y_mod_8 == 3 || y_mod_8 == 5) + J = -J; + } + + if(x % 4 == 3 && y % 4 == 3) + J = -J; + std::swap(x, y); + } + return J; + } + +} diff --git a/botan/src/math/numbertheory/make_prm.cpp b/botan/src/math/numbertheory/make_prm.cpp new file mode 100644 index 0000000..b136b6d --- /dev/null +++ b/botan/src/math/numbertheory/make_prm.cpp @@ -0,0 +1,97 @@ +/* +* Prime Generation +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/numthry.h> +#include <botan/parsing.h> +#include <algorithm> + +namespace Botan { + +/* +* Generate a random prime +*/ +BigInt random_prime(RandomNumberGenerator& rng, + u32bit bits, const BigInt& coprime, + u32bit equiv, u32bit modulo) + { + if(bits <= 1) + throw Invalid_Argument("random_prime: Can't make a prime of " + + to_string(bits) + " bits"); + else if(bits == 2) + return ((rng.next_byte() % 2) ? 2 : 3); + else if(bits == 3) + return ((rng.next_byte() % 2) ? 5 : 7); + else if(bits == 4) + return ((rng.next_byte() % 2) ? 11 : 13); + + if(coprime <= 0) + throw Invalid_Argument("random_prime: coprime must be > 0"); + if(modulo % 2 == 1 || modulo == 0) + throw Invalid_Argument("random_prime: Invalid modulo value"); + if(equiv >= modulo || equiv % 2 == 0) + throw Invalid_Argument("random_prime: equiv must be < modulo, and odd"); + + while(true) + { + BigInt p(rng, bits); + p.set_bit(bits - 2); + p.set_bit(0); + + if(p % modulo != equiv) + p += (modulo - p % modulo) + equiv; + + const u32bit sieve_size = std::min(bits / 2, PRIME_TABLE_SIZE); + SecureVector<u32bit> sieve(sieve_size); + + for(u32bit j = 0; j != sieve.size(); ++j) + sieve[j] = p % PRIMES[j]; + + u32bit counter = 0; + while(true) + { + if(counter == 4096 || p.bits() > bits) + break; + + bool passes_sieve = true; + ++counter; + p += modulo; + + if(p.bits() > bits) + break; + + for(u32bit j = 0; j != sieve.size(); ++j) + { + sieve[j] = (sieve[j] + modulo) % PRIMES[j]; + if(sieve[j] == 0) + passes_sieve = false; + } + + if(!passes_sieve || gcd(p - 1, coprime) != 1) + continue; + if(passes_mr_tests(rng, p)) + return p; + } + } + } + +/* +* Generate a random safe prime +*/ +BigInt random_safe_prime(RandomNumberGenerator& rng, u32bit bits) + { + if(bits <= 64) + throw Invalid_Argument("random_safe_prime: Can't make a prime of " + + to_string(bits) + " bits"); + + BigInt p; + do + p = (random_prime(rng, bits - 1) << 1) + 1; + while(!is_prime(p, rng)); + return p; + } + +} diff --git a/botan/src/math/numbertheory/mp_numth.cpp b/botan/src/math/numbertheory/mp_numth.cpp new file mode 100644 index 0000000..45a3984 --- /dev/null +++ b/botan/src/math/numbertheory/mp_numth.cpp @@ -0,0 +1,71 @@ +/* +* Fused and Important MP Algorithms +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/numthry.h> +#include <botan/mp_core.h> +#include <botan/util.h> +#include <algorithm> + +namespace Botan { + +/* +* Square a BigInt +*/ +BigInt square(const BigInt& x) + { + const u32bit x_sw = x.sig_words(); + + BigInt z(BigInt::Positive, round_up(2*x_sw, 16)); + SecureVector<word> workspace(z.size()); + + bigint_sqr(z.get_reg(), z.size(), workspace, + x.data(), x.size(), x_sw); + return z; + } + +/* +* Multiply-Add Operation +*/ +BigInt mul_add(const BigInt& a, const BigInt& b, const BigInt& c) + { + if(c.is_negative() || c.is_zero()) + throw Invalid_Argument("mul_add: Third argument must be > 0"); + + BigInt::Sign sign = BigInt::Positive; + if(a.sign() != b.sign()) + sign = BigInt::Negative; + + const u32bit a_sw = a.sig_words(); + const u32bit b_sw = b.sig_words(); + const u32bit c_sw = c.sig_words(); + + BigInt r(sign, std::max(a.size() + b.size(), c_sw) + 1); + SecureVector<word> workspace(r.size()); + + bigint_mul(r.get_reg(), r.size(), workspace, + a.data(), a.size(), a_sw, + b.data(), b.size(), b_sw); + const u32bit r_size = std::max(r.sig_words(), c_sw); + bigint_add2(r.get_reg(), r_size, c.data(), c_sw); + return r; + } + +/* +* Subtract-Multiply Operation +*/ +BigInt sub_mul(const BigInt& a, const BigInt& b, const BigInt& c) + { + if(a.is_negative() || b.is_negative()) + throw Invalid_Argument("sub_mul: First two arguments must be >= 0"); + + BigInt r = a; + r -= b; + r *= c; + return r; + } + +} diff --git a/botan/src/math/numbertheory/numthry.cpp b/botan/src/math/numbertheory/numthry.cpp new file mode 100644 index 0000000..4486813 --- /dev/null +++ b/botan/src/math/numbertheory/numthry.cpp @@ -0,0 +1,346 @@ +/* +* Number Theory Functions +* (C) 1999-2009 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/numthry.h> +#include <botan/bit_ops.h> +#include <algorithm> + +namespace Botan { + +namespace { + +/* +* Miller-Rabin Iterations +*/ +u32bit miller_rabin_test_iterations(u32bit bits, bool verify) + { + struct mapping { u32bit bits; u32bit verify_iter; u32bit check_iter; }; + + static const mapping tests[] = { + { 50, 55, 25 }, + { 100, 38, 22 }, + { 160, 32, 18 }, + { 163, 31, 17 }, + { 168, 30, 16 }, + { 177, 29, 16 }, + { 181, 28, 15 }, + { 185, 27, 15 }, + { 190, 26, 15 }, + { 195, 25, 14 }, + { 201, 24, 14 }, + { 208, 23, 14 }, + { 215, 22, 13 }, + { 222, 21, 13 }, + { 231, 20, 13 }, + { 241, 19, 12 }, + { 252, 18, 12 }, + { 264, 17, 12 }, + { 278, 16, 11 }, + { 294, 15, 10 }, + { 313, 14, 9 }, + { 334, 13, 8 }, + { 360, 12, 8 }, + { 392, 11, 7 }, + { 430, 10, 7 }, + { 479, 9, 6 }, + { 542, 8, 6 }, + { 626, 7, 5 }, + { 746, 6, 4 }, + { 926, 5, 3 }, + { 1232, 4, 2 }, + { 1853, 3, 2 }, + { 0, 0, 0 } + }; + + for(u32bit i = 0; tests[i].bits; ++i) + { + if(bits <= tests[i].bits) + { + if(verify) + return tests[i].verify_iter; + else + return tests[i].check_iter; + } + } + return 2; + } + +} + +/* +* Return the number of 0 bits at the end of n +*/ +u32bit low_zero_bits(const BigInt& n) + { + if(n.is_negative() || n.is_zero()) return 0; + + u32bit low_zero = 0; + + if(n.is_positive() && n.is_nonzero()) + { + for(u32bit i = 0; i != n.size(); ++i) + { + word x = n[i]; + + if(x) + { + low_zero += ctz(x); + break; + } + else + low_zero += BOTAN_MP_WORD_BITS; + } + } + + return low_zero; + } + +/* +* Calculate the GCD +*/ +BigInt gcd(const BigInt& a, const BigInt& b) + { + if(a.is_zero() || b.is_zero()) return 0; + if(a == 1 || b == 1) return 1; + + BigInt x = a, y = b; + x.set_sign(BigInt::Positive); + y.set_sign(BigInt::Positive); + u32bit shift = std::min(low_zero_bits(x), low_zero_bits(y)); + + x >>= shift; + y >>= shift; + + while(x.is_nonzero()) + { + x >>= low_zero_bits(x); + y >>= low_zero_bits(y); + if(x >= y) { x -= y; x >>= 1; } + else { y -= x; y >>= 1; } + } + + return (y << shift); + } + +/* +* Calculate the LCM +*/ +BigInt lcm(const BigInt& a, const BigInt& b) + { + return ((a * b) / gcd(a, b)); + } + +/* +* Find the Modular Inverse +*/ +BigInt inverse_mod(const BigInt& n, const BigInt& mod) + { + if(mod.is_zero()) + throw BigInt::DivideByZero(); + if(mod.is_negative() || n.is_negative()) + throw Invalid_Argument("inverse_mod: arguments must be non-negative"); + + if(n.is_zero() || (n.is_even() && mod.is_even())) + return 0; + + BigInt x = mod, y = n, u = mod, v = n; + BigInt A = 1, B = 0, C = 0, D = 1; + + while(u.is_nonzero()) + { + u32bit zero_bits = low_zero_bits(u); + u >>= zero_bits; + for(u32bit i = 0; i != zero_bits; ++i) + { + if(A.is_odd() || B.is_odd()) + { A += y; B -= x; } + A >>= 1; B >>= 1; + } + + zero_bits = low_zero_bits(v); + v >>= zero_bits; + for(u32bit i = 0; i != zero_bits; ++i) + { + if(C.is_odd() || D.is_odd()) + { C += y; D -= x; } + C >>= 1; D >>= 1; + } + + if(u >= v) { u -= v; A -= C; B -= D; } + else { v -= u; C -= A; D -= B; } + } + + if(v != 1) + return 0; + + while(D.is_negative()) D += mod; + while(D >= mod) D -= mod; + + return D; + } + +/* +* Modular Exponentiation +*/ +BigInt power_mod(const BigInt& base, const BigInt& exp, const BigInt& mod) + { + Power_Mod pow_mod(mod); + pow_mod.set_base(base); + pow_mod.set_exponent(exp); + return pow_mod.execute(); + } + +/* +* Do simple tests of primality +*/ +s32bit simple_primality_tests(const BigInt& n) + { + const s32bit NOT_PRIME = -1, UNKNOWN = 0, PRIME = 1; + + if(n == 2) + return PRIME; + if(n <= 1 || n.is_even()) + return NOT_PRIME; + + if(n <= PRIMES[PRIME_TABLE_SIZE-1]) + { + const word num = n.word_at(0); + for(u32bit i = 0; PRIMES[i]; ++i) + { + if(num == PRIMES[i]) return PRIME; + if(num < PRIMES[i]) return NOT_PRIME; + } + return NOT_PRIME; + } + + u32bit check_first = std::min(n.bits() / 32, PRIME_PRODUCTS_TABLE_SIZE); + for(u32bit i = 0; i != check_first; ++i) + if(gcd(n, PRIME_PRODUCTS[i]) != 1) + return NOT_PRIME; + + return UNKNOWN; + } + +/* +* Fast check of primality +*/ +bool check_prime(const BigInt& n, RandomNumberGenerator& rng) + { + return run_primality_tests(rng, n, 0); + } + +/* +* Test for primality +*/ +bool is_prime(const BigInt& n, RandomNumberGenerator& rng) + { + return run_primality_tests(rng, n, 1); + } + +/* +* Verify primality +*/ +bool verify_prime(const BigInt& n, RandomNumberGenerator& rng) + { + return run_primality_tests(rng, n, 2); + } + +/* +* Verify primality +*/ +bool run_primality_tests(RandomNumberGenerator& rng, + const BigInt& n, u32bit level) + { + s32bit simple_tests = simple_primality_tests(n); + if(simple_tests) return (simple_tests == 1) ? true : false; + return passes_mr_tests(rng, n, level); + } + +/* +* Test for primaility using Miller-Rabin +*/ +bool passes_mr_tests(RandomNumberGenerator& rng, + const BigInt& n, u32bit level) + { + const u32bit PREF_NONCE_BITS = 40; + + if(level > 2) + level = 2; + + MillerRabin_Test mr(n); + + if(!mr.passes_test(2)) + return false; + + if(level == 0) + return true; + + const u32bit NONCE_BITS = std::min(n.bits() - 1, PREF_NONCE_BITS); + + const bool verify = (level == 2); + + u32bit tests = miller_rabin_test_iterations(n.bits(), verify); + + BigInt nonce; + for(u32bit i = 0; i != tests; ++i) + { + if(!verify && PRIMES[i] < (n-1)) + nonce = PRIMES[i]; + else + { + while(nonce < 2 || nonce >= (n-1)) + nonce.randomize(rng, NONCE_BITS); + } + + if(!mr.passes_test(nonce)) + return false; + } + return true; + } + +/* +* Miller-Rabin Test +*/ +bool MillerRabin_Test::passes_test(const BigInt& a) + { + if(a < 2 || a >= n_minus_1) + throw Invalid_Argument("Bad size for nonce in Miller-Rabin test"); + + BigInt y = pow_mod(a); + if(y == 1 || y == n_minus_1) + return true; + + for(u32bit i = 1; i != s; ++i) + { + y = reducer.square(y); + + if(y == 1) + return false; + if(y == n_minus_1) + return true; + } + return false; + } + +/* +* Miller-Rabin Constructor +*/ +MillerRabin_Test::MillerRabin_Test(const BigInt& num) + { + if(num.is_even() || num < 3) + throw Invalid_Argument("MillerRabin_Test: Invalid number for testing"); + + n = num; + n_minus_1 = n - 1; + s = low_zero_bits(n_minus_1); + r = n_minus_1 >> s; + + pow_mod = Fixed_Exponent_Power_Mod(r, n); + reducer = Modular_Reducer(n); + } + +} diff --git a/botan/src/math/numbertheory/numthry.h b/botan/src/math/numbertheory/numthry.h new file mode 100644 index 0000000..e4c0437 --- /dev/null +++ b/botan/src/math/numbertheory/numthry.h @@ -0,0 +1,120 @@ +/* +* Number Theory Functions +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_NUMBER_THEORY_H__ +#define BOTAN_NUMBER_THEORY_H__ + +#include <botan/bigint.h> +#include <botan/reducer.h> +#include <botan/pow_mod.h> +#include <botan/rng.h> + +namespace Botan { + +/* +* Fused Arithmetic Operations +*/ +BigInt BOTAN_DLL mul_add(const BigInt&, const BigInt&, const BigInt&); +BigInt BOTAN_DLL sub_mul(const BigInt&, const BigInt&, const BigInt&); + +/* +* Number Theory Functions +*/ +inline BigInt abs(const BigInt& n) { return n.abs(); } + +void BOTAN_DLL divide(const BigInt&, const BigInt&, BigInt&, BigInt&); + +BigInt BOTAN_DLL gcd(const BigInt&, const BigInt&); +BigInt BOTAN_DLL lcm(const BigInt&, const BigInt&); + +BigInt BOTAN_DLL square(const BigInt&); +BigInt BOTAN_DLL inverse_mod(const BigInt&, const BigInt&); +s32bit BOTAN_DLL jacobi(const BigInt&, const BigInt&); + +BigInt BOTAN_DLL power_mod(const BigInt&, const BigInt&, const BigInt&); + +/* +* Compute the square root of x modulo a prime +* using the Shanks-Tonnelli algorithm +*/ +BigInt ressol(const BigInt& x, const BigInt& p); + +/* +* Utility Functions +*/ +u32bit BOTAN_DLL low_zero_bits(const BigInt&); + +/* +* Primality Testing +*/ +bool BOTAN_DLL check_prime(const BigInt&, RandomNumberGenerator&); +bool BOTAN_DLL is_prime(const BigInt&, RandomNumberGenerator&); +bool BOTAN_DLL verify_prime(const BigInt&, RandomNumberGenerator&); + +s32bit BOTAN_DLL simple_primality_tests(const BigInt&); + +bool BOTAN_DLL passes_mr_tests(RandomNumberGenerator&, + const BigInt&, u32bit = 1); + +bool BOTAN_DLL run_primality_tests(RandomNumberGenerator&, + const BigInt&, u32bit = 1); + +/* +* Random Number Generation +*/ +BigInt BOTAN_DLL random_prime(RandomNumberGenerator&, + u32bit bits, const BigInt& coprime = 1, + u32bit equiv = 1, u32bit equiv_mod = 2); + +BigInt BOTAN_DLL random_safe_prime(RandomNumberGenerator&, + u32bit); + +/* +* DSA Parameter Generation +*/ +class Algorithm_Factory; + +SecureVector<byte> BOTAN_DLL +generate_dsa_primes(RandomNumberGenerator& rng, + Algorithm_Factory& af, + BigInt& p, BigInt& q, + u32bit pbits, u32bit qbits); + +bool BOTAN_DLL +generate_dsa_primes(RandomNumberGenerator& rng, + Algorithm_Factory& af, + BigInt& p_out, BigInt& q_out, + u32bit p_bits, u32bit q_bits, + const MemoryRegion<byte>& seed); + +/* +* Prime Numbers +*/ +const u32bit PRIME_TABLE_SIZE = 6541; +const u32bit PRIME_PRODUCTS_TABLE_SIZE = 256; + +extern const u16bit BOTAN_DLL PRIMES[]; +extern const u64bit PRIME_PRODUCTS[]; + +/* +* Miller-Rabin Primality Tester +*/ +class BOTAN_DLL MillerRabin_Test + { + public: + bool passes_test(const BigInt&); + MillerRabin_Test(const BigInt&); + private: + BigInt n, r, n_minus_1; + u32bit s; + Fixed_Exponent_Power_Mod pow_mod; + Modular_Reducer reducer; + }; + +} + +#endif diff --git a/botan/src/math/numbertheory/pow_mod.cpp b/botan/src/math/numbertheory/pow_mod.cpp new file mode 100644 index 0000000..fd9b8e9 --- /dev/null +++ b/botan/src/math/numbertheory/pow_mod.cpp @@ -0,0 +1,157 @@ +/* +* Modular Exponentiation Proxy +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/pow_mod.h> +#include <botan/pk_engine.h> + +namespace Botan { + +/* +* Power_Mod Constructor +*/ +Power_Mod::Power_Mod(const BigInt& n, Usage_Hints hints) + { + core = 0; + set_modulus(n, hints); + } + +/* +* Power_Mod Copy Constructor +*/ +Power_Mod::Power_Mod(const Power_Mod& other) + { + core = 0; + if(other.core) + core = other.core->copy(); + } + +/* +* Power_Mod Assignment Operator +*/ +Power_Mod& Power_Mod::operator=(const Power_Mod& other) + { + delete core; + core = 0; + if(other.core) + core = other.core->copy(); + return (*this); + } + +/* +* Power_Mod Destructor +*/ +Power_Mod::~Power_Mod() + { + delete core; + } + +/* +* Set the modulus +*/ +void Power_Mod::set_modulus(const BigInt& n, Usage_Hints hints) const + { + delete core; + core = ((n == 0) ? 0 : Engine_Core::mod_exp(n, hints)); + } + +/* +* Set the base +*/ +void Power_Mod::set_base(const BigInt& b) const + { + if(b.is_zero() || b.is_negative()) + throw Invalid_Argument("Power_Mod::set_base: arg must be > 0"); + + if(!core) + throw Internal_Error("Power_Mod::set_base: core was NULL"); + core->set_base(b); + } + +/* +* Set the exponent +*/ +void Power_Mod::set_exponent(const BigInt& e) const + { + if(e.is_negative()) + throw Invalid_Argument("Power_Mod::set_exponent: arg must be > 0"); + + if(!core) + throw Internal_Error("Power_Mod::set_exponent: core was NULL"); + core->set_exponent(e); + } + +/* +* Compute the result +*/ +BigInt Power_Mod::execute() const + { + if(!core) + throw Internal_Error("Power_Mod::execute: core was NULL"); + return core->execute(); + } + +namespace { + +/* +* Choose potentially useful hints +*/ +Power_Mod::Usage_Hints choose_base_hints(const BigInt& b, const BigInt& n) + { + if(b == 2) + return Power_Mod::Usage_Hints(Power_Mod::BASE_IS_2 | + Power_Mod::BASE_IS_SMALL); + + const u32bit b_bits = b.bits(); + const u32bit n_bits = n.bits(); + + if(b_bits < n_bits / 32) + return Power_Mod::BASE_IS_SMALL; + if(b_bits > n_bits / 4) + return Power_Mod::BASE_IS_LARGE; + + return Power_Mod::NO_HINTS; + } + +/* +* Choose potentially useful hints +*/ +Power_Mod::Usage_Hints choose_exp_hints(const BigInt& e, const BigInt& n) + { + const u32bit e_bits = e.bits(); + const u32bit n_bits = n.bits(); + + if(e_bits < n_bits / 32) + return Power_Mod::BASE_IS_SMALL; + if(e_bits > n_bits / 4) + return Power_Mod::BASE_IS_LARGE; + return Power_Mod::NO_HINTS; + } + +} + +/* +* Fixed_Exponent_Power_Mod Constructor +*/ +Fixed_Exponent_Power_Mod::Fixed_Exponent_Power_Mod(const BigInt& e, + const BigInt& n, + Usage_Hints hints) : + Power_Mod(n, Usage_Hints(hints | EXP_IS_FIXED | choose_exp_hints(e, n))) + { + set_exponent(e); + } + +/* +* Fixed_Base_Power_Mod Constructor +*/ +Fixed_Base_Power_Mod::Fixed_Base_Power_Mod(const BigInt& b, const BigInt& n, + Usage_Hints hints) : + Power_Mod(n, Usage_Hints(hints | BASE_IS_FIXED | choose_base_hints(b, n))) + { + set_base(b); + } + +} diff --git a/botan/src/math/numbertheory/pow_mod.h b/botan/src/math/numbertheory/pow_mod.h new file mode 100644 index 0000000..6952dcd --- /dev/null +++ b/botan/src/math/numbertheory/pow_mod.h @@ -0,0 +1,93 @@ +/* +* Modular Exponentiator +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_POWER_MOD_H__ +#define BOTAN_POWER_MOD_H__ + +#include <botan/bigint.h> + +namespace Botan { + +/* +* Modular Exponentiator Interface +*/ +class BOTAN_DLL Modular_Exponentiator + { + public: + virtual void set_base(const BigInt&) = 0; + virtual void set_exponent(const BigInt&) = 0; + virtual BigInt execute() const = 0; + virtual Modular_Exponentiator* copy() const = 0; + virtual ~Modular_Exponentiator() {} + }; + +/* +* Modular Exponentiator Proxy +*/ +class BOTAN_DLL Power_Mod + { + public: + enum Usage_Hints { + NO_HINTS = 0x0000, + + BASE_IS_FIXED = 0x0001, + BASE_IS_SMALL = 0x0002, + BASE_IS_LARGE = 0x0004, + BASE_IS_2 = 0x0008, + + EXP_IS_FIXED = 0x0100, + EXP_IS_SMALL = 0x0200, + EXP_IS_LARGE = 0x0400 + }; + + void set_modulus(const BigInt&, Usage_Hints = NO_HINTS) const; + void set_base(const BigInt&) const; + void set_exponent(const BigInt&) const; + + BigInt execute() const; + + Power_Mod& operator=(const Power_Mod&); + + Power_Mod(const BigInt& = 0, Usage_Hints = NO_HINTS); + Power_Mod(const Power_Mod&); + ~Power_Mod(); + private: + mutable Modular_Exponentiator* core; + Usage_Hints hints; + }; + +/* +* Fixed Exponent Modular Exponentiator Proxy +*/ +class BOTAN_DLL Fixed_Exponent_Power_Mod : public Power_Mod + { + public: + BigInt operator()(const BigInt& b) const + { set_base(b); return execute(); } + + Fixed_Exponent_Power_Mod() {} + Fixed_Exponent_Power_Mod(const BigInt&, const BigInt&, + Usage_Hints = NO_HINTS); + }; + +/* +* Fixed Base Modular Exponentiator Proxy +*/ +class BOTAN_DLL Fixed_Base_Power_Mod : public Power_Mod + { + public: + BigInt operator()(const BigInt& e) const + { set_exponent(e); return execute(); } + + Fixed_Base_Power_Mod() {} + Fixed_Base_Power_Mod(const BigInt&, const BigInt&, + Usage_Hints = NO_HINTS); + }; + +} + +#endif diff --git a/botan/src/math/numbertheory/powm_fw.cpp b/botan/src/math/numbertheory/powm_fw.cpp new file mode 100644 index 0000000..b764ee7 --- /dev/null +++ b/botan/src/math/numbertheory/powm_fw.cpp @@ -0,0 +1,104 @@ +/* +* Fixed Window Exponentiation +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/def_powm.h> +#include <botan/numthry.h> +#include <vector> + +namespace Botan { + +namespace { + +/* +* Try to choose a good window size +*/ +u32bit choose_window_bits(u32bit exp_bits, u32bit, + Power_Mod::Usage_Hints hints) + { + static const u32bit wsize[][2] = { + { 2048, 7 }, { 1024, 6 }, { 256, 5 }, { 128, 4 }, { 64, 3 }, { 0, 0 } + }; + + u32bit window_bits = 3; + + if(exp_bits) + { + for(u32bit j = 0; wsize[j][0]; ++j) + { + if(exp_bits >= wsize[j][0]) + { + window_bits += wsize[j][1]; + break; + } + } + } + + if(hints & Power_Mod::EXP_IS_FIXED) + window_bits += 2; + if(hints & Power_Mod::EXP_IS_LARGE) + window_bits += 2; + if(hints & Power_Mod::BASE_IS_FIXED) + ++window_bits; + + return window_bits; + } + +} + +/* +* Set the exponent +*/ +void Fixed_Window_Exponentiator::set_exponent(const BigInt& e) + { + exp = e; + } + +/* +* Set the base +*/ +void Fixed_Window_Exponentiator::set_base(const BigInt& base) + { + window_bits = choose_window_bits(exp.bits(), base.bits(), hints); + + g.resize((1 << window_bits) - 1); + g[0] = base; + for(u32bit j = 1; j != g.size(); ++j) + g[j] = reducer.multiply(g[j-1], g[0]); + } + +/* +* Compute the result +*/ +BigInt Fixed_Window_Exponentiator::execute() const + { + const u32bit exp_nibbles = (exp.bits() + window_bits - 1) / window_bits; + + BigInt x = 1; + for(u32bit j = exp_nibbles; j > 0; --j) + { + for(u32bit k = 0; k != window_bits; ++k) + x = reducer.square(x); + + u32bit nibble = exp.get_substring(window_bits*(j-1), window_bits); + if(nibble) + x = reducer.multiply(x, g[nibble-1]); + } + return x; + } + +/* +* Fixed_Window_Exponentiator Constructor +*/ +Fixed_Window_Exponentiator::Fixed_Window_Exponentiator(const BigInt& n, + Power_Mod::Usage_Hints hints) + { + reducer = Modular_Reducer(n); + this->hints = hints; + window_bits = 0; + } + +} diff --git a/botan/src/math/numbertheory/powm_mnt.cpp b/botan/src/math/numbertheory/powm_mnt.cpp new file mode 100644 index 0000000..e6d8cc3 --- /dev/null +++ b/botan/src/math/numbertheory/powm_mnt.cpp @@ -0,0 +1,180 @@ +/* +* Montgomery Exponentiation +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/def_powm.h> +#include <botan/numthry.h> +#include <botan/mp_core.h> + +namespace Botan { + +namespace { + +/* +* Try to choose a good window size +*/ +u32bit choose_window_bits(u32bit exp_bits, u32bit, + Power_Mod::Usage_Hints hints) + { + static const u32bit wsize[][2] = { + { 2048, 4 }, { 1024, 3 }, { 256, 2 }, { 128, 1 }, { 0, 0 } + }; + + u32bit window_bits = 1; + + if(exp_bits) + { + for(u32bit j = 0; wsize[j][0]; ++j) + { + if(exp_bits >= wsize[j][0]) + { + window_bits += wsize[j][1]; + break; + } + } + } + + if(hints & Power_Mod::BASE_IS_FIXED) + window_bits += 2; + if(hints & Power_Mod::EXP_IS_LARGE) + ++window_bits; + + return window_bits; + } + +/* +* Montgomery Reduction +*/ +inline void montgomery_reduce(BigInt& out, MemoryRegion<word>& z_buf, + const BigInt& x_bn, u32bit x_size, word u) + { + const word* x = x_bn.data(); + word* z = z_buf.begin(); + u32bit z_size = z_buf.size(); + + bigint_monty_redc(z, z_size, x, x_size, u); + + out.get_reg().set(z + x_size, x_size + 1); + } + +} + +/* +* Set the exponent +*/ +void Montgomery_Exponentiator::set_exponent(const BigInt& exp) + { + this->exp = exp; + exp_bits = exp.bits(); + } + +/* +* Set the base +*/ +void Montgomery_Exponentiator::set_base(const BigInt& base) + { + window_bits = choose_window_bits(exp.bits(), base.bits(), hints); + + g.resize((1 << window_bits) - 1); + + SecureVector<word> z(2 * (mod_words + 1)); + SecureVector<word> workspace(z.size()); + + g[0] = (base >= modulus) ? (base % modulus) : base; + bigint_mul(z.begin(), z.size(), workspace, + g[0].data(), g[0].size(), g[0].sig_words(), + R2.data(), R2.size(), R2.sig_words()); + + montgomery_reduce(g[0], z, modulus, mod_words, mod_prime); + + const BigInt& x = g[0]; + const u32bit x_sig = x.sig_words(); + + for(u32bit j = 1; j != g.size(); ++j) + { + const BigInt& y = g[j-1]; + const u32bit y_sig = y.sig_words(); + + z.clear(); + bigint_mul(z.begin(), z.size(), workspace, + x.data(), x.size(), x_sig, + y.data(), y.size(), y_sig); + + montgomery_reduce(g[j], z, modulus, mod_words, mod_prime); + } + } + +/* +* Compute the result +*/ +BigInt Montgomery_Exponentiator::execute() const + { + const u32bit exp_nibbles = (exp_bits + window_bits - 1) / window_bits; + + BigInt x = R_mod; + SecureVector<word> z(2 * (mod_words + 1)); + SecureVector<word> workspace(2 * (mod_words + 1)); + + for(u32bit j = exp_nibbles; j > 0; --j) + { + for(u32bit k = 0; k != window_bits; ++k) + { + z.clear(); + bigint_sqr(z.begin(), z.size(), workspace, + x.data(), x.size(), x.sig_words()); + + montgomery_reduce(x, z, modulus, mod_words, mod_prime); + } + + u32bit nibble = exp.get_substring(window_bits*(j-1), window_bits); + if(nibble) + { + const BigInt& y = g[nibble-1]; + + z.clear(); + bigint_mul(z.begin(), z.size(), workspace, + x.data(), x.size(), x.sig_words(), + y.data(), y.size(), y.sig_words()); + + montgomery_reduce(x, z, modulus, mod_words, mod_prime); + } + } + + z.clear(); + z.copy(x.data(), x.size()); + + montgomery_reduce(x, z, modulus, mod_words, mod_prime); + return x; + } + +/* +* Montgomery_Exponentiator Constructor +*/ +Montgomery_Exponentiator::Montgomery_Exponentiator(const BigInt& mod, + Power_Mod::Usage_Hints hints) + { + if(!mod.is_positive()) + throw Exception("Montgomery_Exponentiator: modulus must be positive"); + if(mod.is_even()) + throw Exception("Montgomery_Exponentiator: modulus must be odd"); + + window_bits = 0; + this->hints = hints; + modulus = mod; + + mod_words = modulus.sig_words(); + + BigInt mod_prime_bn(BigInt::Power2, MP_WORD_BITS); + mod_prime = (mod_prime_bn - inverse_mod(modulus, mod_prime_bn)).word_at(0); + + R_mod = BigInt(BigInt::Power2, MP_WORD_BITS * mod_words); + R_mod %= modulus; + + R2 = BigInt(BigInt::Power2, 2 * MP_WORD_BITS * mod_words); + R2 %= modulus; + } + +} diff --git a/botan/src/math/numbertheory/primes.cpp b/botan/src/math/numbertheory/primes.cpp new file mode 100644 index 0000000..a9c8ab5 --- /dev/null +++ b/botan/src/math/numbertheory/primes.cpp @@ -0,0 +1,676 @@ +/* +* Small Primes Table +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/numthry.h> + +namespace Botan { + +const u16bit PRIMES[PRIME_TABLE_SIZE+1] = { + 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, + 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, + 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, + 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, + 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, + 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, + 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, + 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, + 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, + 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, + 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, + 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, + 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, + 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, + 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, + 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, + 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, + 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, + 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, + 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, + 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, + 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523, 1531, 1543, + 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609, + 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 1697, + 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, + 1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, + 1877, 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, + 1979, 1987, 1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029, 2039, + 2053, 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129, + 2131, 2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213, 2221, + 2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287, 2293, 2297, + 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357, 2371, 2377, 2381, + 2383, 2389, 2393, 2399, 2411, 2417, 2423, 2437, 2441, 2447, 2459, + 2467, 2473, 2477, 2503, 2521, 2531, 2539, 2543, 2549, 2551, 2557, + 2579, 2591, 2593, 2609, 2617, 2621, 2633, 2647, 2657, 2659, 2663, + 2671, 2677, 2683, 2687, 2689, 2693, 2699, 2707, 2711, 2713, 2719, + 2729, 2731, 2741, 2749, 2753, 2767, 2777, 2789, 2791, 2797, 2801, + 2803, 2819, 2833, 2837, 2843, 2851, 2857, 2861, 2879, 2887, 2897, + 2903, 2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999, + 3001, 3011, 3019, 3023, 3037, 3041, 3049, 3061, 3067, 3079, 3083, + 3089, 3109, 3119, 3121, 3137, 3163, 3167, 3169, 3181, 3187, 3191, + 3203, 3209, 3217, 3221, 3229, 3251, 3253, 3257, 3259, 3271, 3299, + 3301, 3307, 3313, 3319, 3323, 3329, 3331, 3343, 3347, 3359, 3361, + 3371, 3373, 3389, 3391, 3407, 3413, 3433, 3449, 3457, 3461, 3463, + 3467, 3469, 3491, 3499, 3511, 3517, 3527, 3529, 3533, 3539, 3541, + 3547, 3557, 3559, 3571, 3581, 3583, 3593, 3607, 3613, 3617, 3623, + 3631, 3637, 3643, 3659, 3671, 3673, 3677, 3691, 3697, 3701, 3709, + 3719, 3727, 3733, 3739, 3761, 3767, 3769, 3779, 3793, 3797, 3803, + 3821, 3823, 3833, 3847, 3851, 3853, 3863, 3877, 3881, 3889, 3907, + 3911, 3917, 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989, 4001, + 4003, 4007, 4013, 4019, 4021, 4027, 4049, 4051, 4057, 4073, 4079, + 4091, 4093, 4099, 4111, 4127, 4129, 4133, 4139, 4153, 4157, 4159, + 4177, 4201, 4211, 4217, 4219, 4229, 4231, 4241, 4243, 4253, 4259, + 4261, 4271, 4273, 4283, 4289, 4297, 4327, 4337, 4339, 4349, 4357, + 4363, 4373, 4391, 4397, 4409, 4421, 4423, 4441, 4447, 4451, 4457, + 4463, 4481, 4483, 4493, 4507, 4513, 4517, 4519, 4523, 4547, 4549, + 4561, 4567, 4583, 4591, 4597, 4603, 4621, 4637, 4639, 4643, 4649, + 4651, 4657, 4663, 4673, 4679, 4691, 4703, 4721, 4723, 4729, 4733, + 4751, 4759, 4783, 4787, 4789, 4793, 4799, 4801, 4813, 4817, 4831, + 4861, 4871, 4877, 4889, 4903, 4909, 4919, 4931, 4933, 4937, 4943, + 4951, 4957, 4967, 4969, 4973, 4987, 4993, 4999, 5003, 5009, 5011, + 5021, 5023, 5039, 5051, 5059, 5077, 5081, 5087, 5099, 5101, 5107, + 5113, 5119, 5147, 5153, 5167, 5171, 5179, 5189, 5197, 5209, 5227, + 5231, 5233, 5237, 5261, 5273, 5279, 5281, 5297, 5303, 5309, 5323, + 5333, 5347, 5351, 5381, 5387, 5393, 5399, 5407, 5413, 5417, 5419, + 5431, 5437, 5441, 5443, 5449, 5471, 5477, 5479, 5483, 5501, 5503, + 5507, 5519, 5521, 5527, 5531, 5557, 5563, 5569, 5573, 5581, 5591, + 5623, 5639, 5641, 5647, 5651, 5653, 5657, 5659, 5669, 5683, 5689, + 5693, 5701, 5711, 5717, 5737, 5741, 5743, 5749, 5779, 5783, 5791, + 5801, 5807, 5813, 5821, 5827, 5839, 5843, 5849, 5851, 5857, 5861, + 5867, 5869, 5879, 5881, 5897, 5903, 5923, 5927, 5939, 5953, 5981, + 5987, 6007, 6011, 6029, 6037, 6043, 6047, 6053, 6067, 6073, 6079, + 6089, 6091, 6101, 6113, 6121, 6131, 6133, 6143, 6151, 6163, 6173, + 6197, 6199, 6203, 6211, 6217, 6221, 6229, 6247, 6257, 6263, 6269, + 6271, 6277, 6287, 6299, 6301, 6311, 6317, 6323, 6329, 6337, 6343, + 6353, 6359, 6361, 6367, 6373, 6379, 6389, 6397, 6421, 6427, 6449, + 6451, 6469, 6473, 6481, 6491, 6521, 6529, 6547, 6551, 6553, 6563, + 6569, 6571, 6577, 6581, 6599, 6607, 6619, 6637, 6653, 6659, 6661, + 6673, 6679, 6689, 6691, 6701, 6703, 6709, 6719, 6733, 6737, 6761, + 6763, 6779, 6781, 6791, 6793, 6803, 6823, 6827, 6829, 6833, 6841, + 6857, 6863, 6869, 6871, 6883, 6899, 6907, 6911, 6917, 6947, 6949, + 6959, 6961, 6967, 6971, 6977, 6983, 6991, 6997, 7001, 7013, 7019, + 7027, 7039, 7043, 7057, 7069, 7079, 7103, 7109, 7121, 7127, 7129, + 7151, 7159, 7177, 7187, 7193, 7207, 7211, 7213, 7219, 7229, 7237, + 7243, 7247, 7253, 7283, 7297, 7307, 7309, 7321, 7331, 7333, 7349, + 7351, 7369, 7393, 7411, 7417, 7433, 7451, 7457, 7459, 7477, 7481, + 7487, 7489, 7499, 7507, 7517, 7523, 7529, 7537, 7541, 7547, 7549, + 7559, 7561, 7573, 7577, 7583, 7589, 7591, 7603, 7607, 7621, 7639, + 7643, 7649, 7669, 7673, 7681, 7687, 7691, 7699, 7703, 7717, 7723, + 7727, 7741, 7753, 7757, 7759, 7789, 7793, 7817, 7823, 7829, 7841, + 7853, 7867, 7873, 7877, 7879, 7883, 7901, 7907, 7919, 7927, 7933, + 7937, 7949, 7951, 7963, 7993, 8009, 8011, 8017, 8039, 8053, 8059, + 8069, 8081, 8087, 8089, 8093, 8101, 8111, 8117, 8123, 8147, 8161, + 8167, 8171, 8179, 8191, 8209, 8219, 8221, 8231, 8233, 8237, 8243, + 8263, 8269, 8273, 8287, 8291, 8293, 8297, 8311, 8317, 8329, 8353, + 8363, 8369, 8377, 8387, 8389, 8419, 8423, 8429, 8431, 8443, 8447, + 8461, 8467, 8501, 8513, 8521, 8527, 8537, 8539, 8543, 8563, 8573, + 8581, 8597, 8599, 8609, 8623, 8627, 8629, 8641, 8647, 8663, 8669, + 8677, 8681, 8689, 8693, 8699, 8707, 8713, 8719, 8731, 8737, 8741, + 8747, 8753, 8761, 8779, 8783, 8803, 8807, 8819, 8821, 8831, 8837, + 8839, 8849, 8861, 8863, 8867, 8887, 8893, 8923, 8929, 8933, 8941, + 8951, 8963, 8969, 8971, 8999, 9001, 9007, 9011, 9013, 9029, 9041, + 9043, 9049, 9059, 9067, 9091, 9103, 9109, 9127, 9133, 9137, 9151, + 9157, 9161, 9173, 9181, 9187, 9199, 9203, 9209, 9221, 9227, 9239, + 9241, 9257, 9277, 9281, 9283, 9293, 9311, 9319, 9323, 9337, 9341, + 9343, 9349, 9371, 9377, 9391, 9397, 9403, 9413, 9419, 9421, 9431, + 9433, 9437, 9439, 9461, 9463, 9467, 9473, 9479, 9491, 9497, 9511, + 9521, 9533, 9539, 9547, 9551, 9587, 9601, 9613, 9619, 9623, 9629, + 9631, 9643, 9649, 9661, 9677, 9679, 9689, 9697, 9719, 9721, 9733, + 9739, 9743, 9749, 9767, 9769, 9781, 9787, 9791, 9803, 9811, 9817, + 9829, 9833, 9839, 9851, 9857, 9859, 9871, 9883, 9887, 9901, 9907, + 9923, 9929, 9931, 9941, 9949, 9967, 9973, 10007, 10009, 10037, 10039, +10061, 10067, 10069, 10079, 10091, 10093, 10099, 10103, 10111, 10133, 10139, +10141, 10151, 10159, 10163, 10169, 10177, 10181, 10193, 10211, 10223, 10243, +10247, 10253, 10259, 10267, 10271, 10273, 10289, 10301, 10303, 10313, 10321, +10331, 10333, 10337, 10343, 10357, 10369, 10391, 10399, 10427, 10429, 10433, +10453, 10457, 10459, 10463, 10477, 10487, 10499, 10501, 10513, 10529, 10531, +10559, 10567, 10589, 10597, 10601, 10607, 10613, 10627, 10631, 10639, 10651, +10657, 10663, 10667, 10687, 10691, 10709, 10711, 10723, 10729, 10733, 10739, +10753, 10771, 10781, 10789, 10799, 10831, 10837, 10847, 10853, 10859, 10861, +10867, 10883, 10889, 10891, 10903, 10909, 10937, 10939, 10949, 10957, 10973, +10979, 10987, 10993, 11003, 11027, 11047, 11057, 11059, 11069, 11071, 11083, +11087, 11093, 11113, 11117, 11119, 11131, 11149, 11159, 11161, 11171, 11173, +11177, 11197, 11213, 11239, 11243, 11251, 11257, 11261, 11273, 11279, 11287, +11299, 11311, 11317, 11321, 11329, 11351, 11353, 11369, 11383, 11393, 11399, +11411, 11423, 11437, 11443, 11447, 11467, 11471, 11483, 11489, 11491, 11497, +11503, 11519, 11527, 11549, 11551, 11579, 11587, 11593, 11597, 11617, 11621, +11633, 11657, 11677, 11681, 11689, 11699, 11701, 11717, 11719, 11731, 11743, +11777, 11779, 11783, 11789, 11801, 11807, 11813, 11821, 11827, 11831, 11833, +11839, 11863, 11867, 11887, 11897, 11903, 11909, 11923, 11927, 11933, 11939, +11941, 11953, 11959, 11969, 11971, 11981, 11987, 12007, 12011, 12037, 12041, +12043, 12049, 12071, 12073, 12097, 12101, 12107, 12109, 12113, 12119, 12143, +12149, 12157, 12161, 12163, 12197, 12203, 12211, 12227, 12239, 12241, 12251, +12253, 12263, 12269, 12277, 12281, 12289, 12301, 12323, 12329, 12343, 12347, +12373, 12377, 12379, 12391, 12401, 12409, 12413, 12421, 12433, 12437, 12451, +12457, 12473, 12479, 12487, 12491, 12497, 12503, 12511, 12517, 12527, 12539, +12541, 12547, 12553, 12569, 12577, 12583, 12589, 12601, 12611, 12613, 12619, +12637, 12641, 12647, 12653, 12659, 12671, 12689, 12697, 12703, 12713, 12721, +12739, 12743, 12757, 12763, 12781, 12791, 12799, 12809, 12821, 12823, 12829, +12841, 12853, 12889, 12893, 12899, 12907, 12911, 12917, 12919, 12923, 12941, +12953, 12959, 12967, 12973, 12979, 12983, 13001, 13003, 13007, 13009, 13033, +13037, 13043, 13049, 13063, 13093, 13099, 13103, 13109, 13121, 13127, 13147, +13151, 13159, 13163, 13171, 13177, 13183, 13187, 13217, 13219, 13229, 13241, +13249, 13259, 13267, 13291, 13297, 13309, 13313, 13327, 13331, 13337, 13339, +13367, 13381, 13397, 13399, 13411, 13417, 13421, 13441, 13451, 13457, 13463, +13469, 13477, 13487, 13499, 13513, 13523, 13537, 13553, 13567, 13577, 13591, +13597, 13613, 13619, 13627, 13633, 13649, 13669, 13679, 13681, 13687, 13691, +13693, 13697, 13709, 13711, 13721, 13723, 13729, 13751, 13757, 13759, 13763, +13781, 13789, 13799, 13807, 13829, 13831, 13841, 13859, 13873, 13877, 13879, +13883, 13901, 13903, 13907, 13913, 13921, 13931, 13933, 13963, 13967, 13997, +13999, 14009, 14011, 14029, 14033, 14051, 14057, 14071, 14081, 14083, 14087, +14107, 14143, 14149, 14153, 14159, 14173, 14177, 14197, 14207, 14221, 14243, +14249, 14251, 14281, 14293, 14303, 14321, 14323, 14327, 14341, 14347, 14369, +14387, 14389, 14401, 14407, 14411, 14419, 14423, 14431, 14437, 14447, 14449, +14461, 14479, 14489, 14503, 14519, 14533, 14537, 14543, 14549, 14551, 14557, +14561, 14563, 14591, 14593, 14621, 14627, 14629, 14633, 14639, 14653, 14657, +14669, 14683, 14699, 14713, 14717, 14723, 14731, 14737, 14741, 14747, 14753, +14759, 14767, 14771, 14779, 14783, 14797, 14813, 14821, 14827, 14831, 14843, +14851, 14867, 14869, 14879, 14887, 14891, 14897, 14923, 14929, 14939, 14947, +14951, 14957, 14969, 14983, 15013, 15017, 15031, 15053, 15061, 15073, 15077, +15083, 15091, 15101, 15107, 15121, 15131, 15137, 15139, 15149, 15161, 15173, +15187, 15193, 15199, 15217, 15227, 15233, 15241, 15259, 15263, 15269, 15271, +15277, 15287, 15289, 15299, 15307, 15313, 15319, 15329, 15331, 15349, 15359, +15361, 15373, 15377, 15383, 15391, 15401, 15413, 15427, 15439, 15443, 15451, +15461, 15467, 15473, 15493, 15497, 15511, 15527, 15541, 15551, 15559, 15569, +15581, 15583, 15601, 15607, 15619, 15629, 15641, 15643, 15647, 15649, 15661, +15667, 15671, 15679, 15683, 15727, 15731, 15733, 15737, 15739, 15749, 15761, +15767, 15773, 15787, 15791, 15797, 15803, 15809, 15817, 15823, 15859, 15877, +15881, 15887, 15889, 15901, 15907, 15913, 15919, 15923, 15937, 15959, 15971, +15973, 15991, 16001, 16007, 16033, 16057, 16061, 16063, 16067, 16069, 16073, +16087, 16091, 16097, 16103, 16111, 16127, 16139, 16141, 16183, 16187, 16189, +16193, 16217, 16223, 16229, 16231, 16249, 16253, 16267, 16273, 16301, 16319, +16333, 16339, 16349, 16361, 16363, 16369, 16381, 16411, 16417, 16421, 16427, +16433, 16447, 16451, 16453, 16477, 16481, 16487, 16493, 16519, 16529, 16547, +16553, 16561, 16567, 16573, 16603, 16607, 16619, 16631, 16633, 16649, 16651, +16657, 16661, 16673, 16691, 16693, 16699, 16703, 16729, 16741, 16747, 16759, +16763, 16787, 16811, 16823, 16829, 16831, 16843, 16871, 16879, 16883, 16889, +16901, 16903, 16921, 16927, 16931, 16937, 16943, 16963, 16979, 16981, 16987, +16993, 17011, 17021, 17027, 17029, 17033, 17041, 17047, 17053, 17077, 17093, +17099, 17107, 17117, 17123, 17137, 17159, 17167, 17183, 17189, 17191, 17203, +17207, 17209, 17231, 17239, 17257, 17291, 17293, 17299, 17317, 17321, 17327, +17333, 17341, 17351, 17359, 17377, 17383, 17387, 17389, 17393, 17401, 17417, +17419, 17431, 17443, 17449, 17467, 17471, 17477, 17483, 17489, 17491, 17497, +17509, 17519, 17539, 17551, 17569, 17573, 17579, 17581, 17597, 17599, 17609, +17623, 17627, 17657, 17659, 17669, 17681, 17683, 17707, 17713, 17729, 17737, +17747, 17749, 17761, 17783, 17789, 17791, 17807, 17827, 17837, 17839, 17851, +17863, 17881, 17891, 17903, 17909, 17911, 17921, 17923, 17929, 17939, 17957, +17959, 17971, 17977, 17981, 17987, 17989, 18013, 18041, 18043, 18047, 18049, +18059, 18061, 18077, 18089, 18097, 18119, 18121, 18127, 18131, 18133, 18143, +18149, 18169, 18181, 18191, 18199, 18211, 18217, 18223, 18229, 18233, 18251, +18253, 18257, 18269, 18287, 18289, 18301, 18307, 18311, 18313, 18329, 18341, +18353, 18367, 18371, 18379, 18397, 18401, 18413, 18427, 18433, 18439, 18443, +18451, 18457, 18461, 18481, 18493, 18503, 18517, 18521, 18523, 18539, 18541, +18553, 18583, 18587, 18593, 18617, 18637, 18661, 18671, 18679, 18691, 18701, +18713, 18719, 18731, 18743, 18749, 18757, 18773, 18787, 18793, 18797, 18803, +18839, 18859, 18869, 18899, 18911, 18913, 18917, 18919, 18947, 18959, 18973, +18979, 19001, 19009, 19013, 19031, 19037, 19051, 19069, 19073, 19079, 19081, +19087, 19121, 19139, 19141, 19157, 19163, 19181, 19183, 19207, 19211, 19213, +19219, 19231, 19237, 19249, 19259, 19267, 19273, 19289, 19301, 19309, 19319, +19333, 19373, 19379, 19381, 19387, 19391, 19403, 19417, 19421, 19423, 19427, +19429, 19433, 19441, 19447, 19457, 19463, 19469, 19471, 19477, 19483, 19489, +19501, 19507, 19531, 19541, 19543, 19553, 19559, 19571, 19577, 19583, 19597, +19603, 19609, 19661, 19681, 19687, 19697, 19699, 19709, 19717, 19727, 19739, +19751, 19753, 19759, 19763, 19777, 19793, 19801, 19813, 19819, 19841, 19843, +19853, 19861, 19867, 19889, 19891, 19913, 19919, 19927, 19937, 19949, 19961, +19963, 19973, 19979, 19991, 19993, 19997, 20011, 20021, 20023, 20029, 20047, +20051, 20063, 20071, 20089, 20101, 20107, 20113, 20117, 20123, 20129, 20143, +20147, 20149, 20161, 20173, 20177, 20183, 20201, 20219, 20231, 20233, 20249, +20261, 20269, 20287, 20297, 20323, 20327, 20333, 20341, 20347, 20353, 20357, +20359, 20369, 20389, 20393, 20399, 20407, 20411, 20431, 20441, 20443, 20477, +20479, 20483, 20507, 20509, 20521, 20533, 20543, 20549, 20551, 20563, 20593, +20599, 20611, 20627, 20639, 20641, 20663, 20681, 20693, 20707, 20717, 20719, +20731, 20743, 20747, 20749, 20753, 20759, 20771, 20773, 20789, 20807, 20809, +20849, 20857, 20873, 20879, 20887, 20897, 20899, 20903, 20921, 20929, 20939, +20947, 20959, 20963, 20981, 20983, 21001, 21011, 21013, 21017, 21019, 21023, +21031, 21059, 21061, 21067, 21089, 21101, 21107, 21121, 21139, 21143, 21149, +21157, 21163, 21169, 21179, 21187, 21191, 21193, 21211, 21221, 21227, 21247, +21269, 21277, 21283, 21313, 21317, 21319, 21323, 21341, 21347, 21377, 21379, +21383, 21391, 21397, 21401, 21407, 21419, 21433, 21467, 21481, 21487, 21491, +21493, 21499, 21503, 21517, 21521, 21523, 21529, 21557, 21559, 21563, 21569, +21577, 21587, 21589, 21599, 21601, 21611, 21613, 21617, 21647, 21649, 21661, +21673, 21683, 21701, 21713, 21727, 21737, 21739, 21751, 21757, 21767, 21773, +21787, 21799, 21803, 21817, 21821, 21839, 21841, 21851, 21859, 21863, 21871, +21881, 21893, 21911, 21929, 21937, 21943, 21961, 21977, 21991, 21997, 22003, +22013, 22027, 22031, 22037, 22039, 22051, 22063, 22067, 22073, 22079, 22091, +22093, 22109, 22111, 22123, 22129, 22133, 22147, 22153, 22157, 22159, 22171, +22189, 22193, 22229, 22247, 22259, 22271, 22273, 22277, 22279, 22283, 22291, +22303, 22307, 22343, 22349, 22367, 22369, 22381, 22391, 22397, 22409, 22433, +22441, 22447, 22453, 22469, 22481, 22483, 22501, 22511, 22531, 22541, 22543, +22549, 22567, 22571, 22573, 22613, 22619, 22621, 22637, 22639, 22643, 22651, +22669, 22679, 22691, 22697, 22699, 22709, 22717, 22721, 22727, 22739, 22741, +22751, 22769, 22777, 22783, 22787, 22807, 22811, 22817, 22853, 22859, 22861, +22871, 22877, 22901, 22907, 22921, 22937, 22943, 22961, 22963, 22973, 22993, +23003, 23011, 23017, 23021, 23027, 23029, 23039, 23041, 23053, 23057, 23059, +23063, 23071, 23081, 23087, 23099, 23117, 23131, 23143, 23159, 23167, 23173, +23189, 23197, 23201, 23203, 23209, 23227, 23251, 23269, 23279, 23291, 23293, +23297, 23311, 23321, 23327, 23333, 23339, 23357, 23369, 23371, 23399, 23417, +23431, 23447, 23459, 23473, 23497, 23509, 23531, 23537, 23539, 23549, 23557, +23561, 23563, 23567, 23581, 23593, 23599, 23603, 23609, 23623, 23627, 23629, +23633, 23663, 23669, 23671, 23677, 23687, 23689, 23719, 23741, 23743, 23747, +23753, 23761, 23767, 23773, 23789, 23801, 23813, 23819, 23827, 23831, 23833, +23857, 23869, 23873, 23879, 23887, 23893, 23899, 23909, 23911, 23917, 23929, +23957, 23971, 23977, 23981, 23993, 24001, 24007, 24019, 24023, 24029, 24043, +24049, 24061, 24071, 24077, 24083, 24091, 24097, 24103, 24107, 24109, 24113, +24121, 24133, 24137, 24151, 24169, 24179, 24181, 24197, 24203, 24223, 24229, +24239, 24247, 24251, 24281, 24317, 24329, 24337, 24359, 24371, 24373, 24379, +24391, 24407, 24413, 24419, 24421, 24439, 24443, 24469, 24473, 24481, 24499, +24509, 24517, 24527, 24533, 24547, 24551, 24571, 24593, 24611, 24623, 24631, +24659, 24671, 24677, 24683, 24691, 24697, 24709, 24733, 24749, 24763, 24767, +24781, 24793, 24799, 24809, 24821, 24841, 24847, 24851, 24859, 24877, 24889, +24907, 24917, 24919, 24923, 24943, 24953, 24967, 24971, 24977, 24979, 24989, +25013, 25031, 25033, 25037, 25057, 25073, 25087, 25097, 25111, 25117, 25121, +25127, 25147, 25153, 25163, 25169, 25171, 25183, 25189, 25219, 25229, 25237, +25243, 25247, 25253, 25261, 25301, 25303, 25307, 25309, 25321, 25339, 25343, +25349, 25357, 25367, 25373, 25391, 25409, 25411, 25423, 25439, 25447, 25453, +25457, 25463, 25469, 25471, 25523, 25537, 25541, 25561, 25577, 25579, 25583, +25589, 25601, 25603, 25609, 25621, 25633, 25639, 25643, 25657, 25667, 25673, +25679, 25693, 25703, 25717, 25733, 25741, 25747, 25759, 25763, 25771, 25793, +25799, 25801, 25819, 25841, 25847, 25849, 25867, 25873, 25889, 25903, 25913, +25919, 25931, 25933, 25939, 25943, 25951, 25969, 25981, 25997, 25999, 26003, +26017, 26021, 26029, 26041, 26053, 26083, 26099, 26107, 26111, 26113, 26119, +26141, 26153, 26161, 26171, 26177, 26183, 26189, 26203, 26209, 26227, 26237, +26249, 26251, 26261, 26263, 26267, 26293, 26297, 26309, 26317, 26321, 26339, +26347, 26357, 26371, 26387, 26393, 26399, 26407, 26417, 26423, 26431, 26437, +26449, 26459, 26479, 26489, 26497, 26501, 26513, 26539, 26557, 26561, 26573, +26591, 26597, 26627, 26633, 26641, 26647, 26669, 26681, 26683, 26687, 26693, +26699, 26701, 26711, 26713, 26717, 26723, 26729, 26731, 26737, 26759, 26777, +26783, 26801, 26813, 26821, 26833, 26839, 26849, 26861, 26863, 26879, 26881, +26891, 26893, 26903, 26921, 26927, 26947, 26951, 26953, 26959, 26981, 26987, +26993, 27011, 27017, 27031, 27043, 27059, 27061, 27067, 27073, 27077, 27091, +27103, 27107, 27109, 27127, 27143, 27179, 27191, 27197, 27211, 27239, 27241, +27253, 27259, 27271, 27277, 27281, 27283, 27299, 27329, 27337, 27361, 27367, +27397, 27407, 27409, 27427, 27431, 27437, 27449, 27457, 27479, 27481, 27487, +27509, 27527, 27529, 27539, 27541, 27551, 27581, 27583, 27611, 27617, 27631, +27647, 27653, 27673, 27689, 27691, 27697, 27701, 27733, 27737, 27739, 27743, +27749, 27751, 27763, 27767, 27773, 27779, 27791, 27793, 27799, 27803, 27809, +27817, 27823, 27827, 27847, 27851, 27883, 27893, 27901, 27917, 27919, 27941, +27943, 27947, 27953, 27961, 27967, 27983, 27997, 28001, 28019, 28027, 28031, +28051, 28057, 28069, 28081, 28087, 28097, 28099, 28109, 28111, 28123, 28151, +28163, 28181, 28183, 28201, 28211, 28219, 28229, 28277, 28279, 28283, 28289, +28297, 28307, 28309, 28319, 28349, 28351, 28387, 28393, 28403, 28409, 28411, +28429, 28433, 28439, 28447, 28463, 28477, 28493, 28499, 28513, 28517, 28537, +28541, 28547, 28549, 28559, 28571, 28573, 28579, 28591, 28597, 28603, 28607, +28619, 28621, 28627, 28631, 28643, 28649, 28657, 28661, 28663, 28669, 28687, +28697, 28703, 28711, 28723, 28729, 28751, 28753, 28759, 28771, 28789, 28793, +28807, 28813, 28817, 28837, 28843, 28859, 28867, 28871, 28879, 28901, 28909, +28921, 28927, 28933, 28949, 28961, 28979, 29009, 29017, 29021, 29023, 29027, +29033, 29059, 29063, 29077, 29101, 29123, 29129, 29131, 29137, 29147, 29153, +29167, 29173, 29179, 29191, 29201, 29207, 29209, 29221, 29231, 29243, 29251, +29269, 29287, 29297, 29303, 29311, 29327, 29333, 29339, 29347, 29363, 29383, +29387, 29389, 29399, 29401, 29411, 29423, 29429, 29437, 29443, 29453, 29473, +29483, 29501, 29527, 29531, 29537, 29567, 29569, 29573, 29581, 29587, 29599, +29611, 29629, 29633, 29641, 29663, 29669, 29671, 29683, 29717, 29723, 29741, +29753, 29759, 29761, 29789, 29803, 29819, 29833, 29837, 29851, 29863, 29867, +29873, 29879, 29881, 29917, 29921, 29927, 29947, 29959, 29983, 29989, 30011, +30013, 30029, 30047, 30059, 30071, 30089, 30091, 30097, 30103, 30109, 30113, +30119, 30133, 30137, 30139, 30161, 30169, 30181, 30187, 30197, 30203, 30211, +30223, 30241, 30253, 30259, 30269, 30271, 30293, 30307, 30313, 30319, 30323, +30341, 30347, 30367, 30389, 30391, 30403, 30427, 30431, 30449, 30467, 30469, +30491, 30493, 30497, 30509, 30517, 30529, 30539, 30553, 30557, 30559, 30577, +30593, 30631, 30637, 30643, 30649, 30661, 30671, 30677, 30689, 30697, 30703, +30707, 30713, 30727, 30757, 30763, 30773, 30781, 30803, 30809, 30817, 30829, +30839, 30841, 30851, 30853, 30859, 30869, 30871, 30881, 30893, 30911, 30931, +30937, 30941, 30949, 30971, 30977, 30983, 31013, 31019, 31033, 31039, 31051, +31063, 31069, 31079, 31081, 31091, 31121, 31123, 31139, 31147, 31151, 31153, +31159, 31177, 31181, 31183, 31189, 31193, 31219, 31223, 31231, 31237, 31247, +31249, 31253, 31259, 31267, 31271, 31277, 31307, 31319, 31321, 31327, 31333, +31337, 31357, 31379, 31387, 31391, 31393, 31397, 31469, 31477, 31481, 31489, +31511, 31513, 31517, 31531, 31541, 31543, 31547, 31567, 31573, 31583, 31601, +31607, 31627, 31643, 31649, 31657, 31663, 31667, 31687, 31699, 31721, 31723, +31727, 31729, 31741, 31751, 31769, 31771, 31793, 31799, 31817, 31847, 31849, +31859, 31873, 31883, 31891, 31907, 31957, 31963, 31973, 31981, 31991, 32003, +32009, 32027, 32029, 32051, 32057, 32059, 32063, 32069, 32077, 32083, 32089, +32099, 32117, 32119, 32141, 32143, 32159, 32173, 32183, 32189, 32191, 32203, +32213, 32233, 32237, 32251, 32257, 32261, 32297, 32299, 32303, 32309, 32321, +32323, 32327, 32341, 32353, 32359, 32363, 32369, 32371, 32377, 32381, 32401, +32411, 32413, 32423, 32429, 32441, 32443, 32467, 32479, 32491, 32497, 32503, +32507, 32531, 32533, 32537, 32561, 32563, 32569, 32573, 32579, 32587, 32603, +32609, 32611, 32621, 32633, 32647, 32653, 32687, 32693, 32707, 32713, 32717, +32719, 32749, 32771, 32779, 32783, 32789, 32797, 32801, 32803, 32831, 32833, +32839, 32843, 32869, 32887, 32909, 32911, 32917, 32933, 32939, 32941, 32957, +32969, 32971, 32983, 32987, 32993, 32999, 33013, 33023, 33029, 33037, 33049, +33053, 33071, 33073, 33083, 33091, 33107, 33113, 33119, 33149, 33151, 33161, +33179, 33181, 33191, 33199, 33203, 33211, 33223, 33247, 33287, 33289, 33301, +33311, 33317, 33329, 33331, 33343, 33347, 33349, 33353, 33359, 33377, 33391, +33403, 33409, 33413, 33427, 33457, 33461, 33469, 33479, 33487, 33493, 33503, +33521, 33529, 33533, 33547, 33563, 33569, 33577, 33581, 33587, 33589, 33599, +33601, 33613, 33617, 33619, 33623, 33629, 33637, 33641, 33647, 33679, 33703, +33713, 33721, 33739, 33749, 33751, 33757, 33767, 33769, 33773, 33791, 33797, +33809, 33811, 33827, 33829, 33851, 33857, 33863, 33871, 33889, 33893, 33911, +33923, 33931, 33937, 33941, 33961, 33967, 33997, 34019, 34031, 34033, 34039, +34057, 34061, 34123, 34127, 34129, 34141, 34147, 34157, 34159, 34171, 34183, +34211, 34213, 34217, 34231, 34253, 34259, 34261, 34267, 34273, 34283, 34297, +34301, 34303, 34313, 34319, 34327, 34337, 34351, 34361, 34367, 34369, 34381, +34403, 34421, 34429, 34439, 34457, 34469, 34471, 34483, 34487, 34499, 34501, +34511, 34513, 34519, 34537, 34543, 34549, 34583, 34589, 34591, 34603, 34607, +34613, 34631, 34649, 34651, 34667, 34673, 34679, 34687, 34693, 34703, 34721, +34729, 34739, 34747, 34757, 34759, 34763, 34781, 34807, 34819, 34841, 34843, +34847, 34849, 34871, 34877, 34883, 34897, 34913, 34919, 34939, 34949, 34961, +34963, 34981, 35023, 35027, 35051, 35053, 35059, 35069, 35081, 35083, 35089, +35099, 35107, 35111, 35117, 35129, 35141, 35149, 35153, 35159, 35171, 35201, +35221, 35227, 35251, 35257, 35267, 35279, 35281, 35291, 35311, 35317, 35323, +35327, 35339, 35353, 35363, 35381, 35393, 35401, 35407, 35419, 35423, 35437, +35447, 35449, 35461, 35491, 35507, 35509, 35521, 35527, 35531, 35533, 35537, +35543, 35569, 35573, 35591, 35593, 35597, 35603, 35617, 35671, 35677, 35729, +35731, 35747, 35753, 35759, 35771, 35797, 35801, 35803, 35809, 35831, 35837, +35839, 35851, 35863, 35869, 35879, 35897, 35899, 35911, 35923, 35933, 35951, +35963, 35969, 35977, 35983, 35993, 35999, 36007, 36011, 36013, 36017, 36037, +36061, 36067, 36073, 36083, 36097, 36107, 36109, 36131, 36137, 36151, 36161, +36187, 36191, 36209, 36217, 36229, 36241, 36251, 36263, 36269, 36277, 36293, +36299, 36307, 36313, 36319, 36341, 36343, 36353, 36373, 36383, 36389, 36433, +36451, 36457, 36467, 36469, 36473, 36479, 36493, 36497, 36523, 36527, 36529, +36541, 36551, 36559, 36563, 36571, 36583, 36587, 36599, 36607, 36629, 36637, +36643, 36653, 36671, 36677, 36683, 36691, 36697, 36709, 36713, 36721, 36739, +36749, 36761, 36767, 36779, 36781, 36787, 36791, 36793, 36809, 36821, 36833, +36847, 36857, 36871, 36877, 36887, 36899, 36901, 36913, 36919, 36923, 36929, +36931, 36943, 36947, 36973, 36979, 36997, 37003, 37013, 37019, 37021, 37039, +37049, 37057, 37061, 37087, 37097, 37117, 37123, 37139, 37159, 37171, 37181, +37189, 37199, 37201, 37217, 37223, 37243, 37253, 37273, 37277, 37307, 37309, +37313, 37321, 37337, 37339, 37357, 37361, 37363, 37369, 37379, 37397, 37409, +37423, 37441, 37447, 37463, 37483, 37489, 37493, 37501, 37507, 37511, 37517, +37529, 37537, 37547, 37549, 37561, 37567, 37571, 37573, 37579, 37589, 37591, +37607, 37619, 37633, 37643, 37649, 37657, 37663, 37691, 37693, 37699, 37717, +37747, 37781, 37783, 37799, 37811, 37813, 37831, 37847, 37853, 37861, 37871, +37879, 37889, 37897, 37907, 37951, 37957, 37963, 37967, 37987, 37991, 37993, +37997, 38011, 38039, 38047, 38053, 38069, 38083, 38113, 38119, 38149, 38153, +38167, 38177, 38183, 38189, 38197, 38201, 38219, 38231, 38237, 38239, 38261, +38273, 38281, 38287, 38299, 38303, 38317, 38321, 38327, 38329, 38333, 38351, +38371, 38377, 38393, 38431, 38447, 38449, 38453, 38459, 38461, 38501, 38543, +38557, 38561, 38567, 38569, 38593, 38603, 38609, 38611, 38629, 38639, 38651, +38653, 38669, 38671, 38677, 38693, 38699, 38707, 38711, 38713, 38723, 38729, +38737, 38747, 38749, 38767, 38783, 38791, 38803, 38821, 38833, 38839, 38851, +38861, 38867, 38873, 38891, 38903, 38917, 38921, 38923, 38933, 38953, 38959, +38971, 38977, 38993, 39019, 39023, 39041, 39043, 39047, 39079, 39089, 39097, +39103, 39107, 39113, 39119, 39133, 39139, 39157, 39161, 39163, 39181, 39191, +39199, 39209, 39217, 39227, 39229, 39233, 39239, 39241, 39251, 39293, 39301, +39313, 39317, 39323, 39341, 39343, 39359, 39367, 39371, 39373, 39383, 39397, +39409, 39419, 39439, 39443, 39451, 39461, 39499, 39503, 39509, 39511, 39521, +39541, 39551, 39563, 39569, 39581, 39607, 39619, 39623, 39631, 39659, 39667, +39671, 39679, 39703, 39709, 39719, 39727, 39733, 39749, 39761, 39769, 39779, +39791, 39799, 39821, 39827, 39829, 39839, 39841, 39847, 39857, 39863, 39869, +39877, 39883, 39887, 39901, 39929, 39937, 39953, 39971, 39979, 39983, 39989, +40009, 40013, 40031, 40037, 40039, 40063, 40087, 40093, 40099, 40111, 40123, +40127, 40129, 40151, 40153, 40163, 40169, 40177, 40189, 40193, 40213, 40231, +40237, 40241, 40253, 40277, 40283, 40289, 40343, 40351, 40357, 40361, 40387, +40423, 40427, 40429, 40433, 40459, 40471, 40483, 40487, 40493, 40499, 40507, +40519, 40529, 40531, 40543, 40559, 40577, 40583, 40591, 40597, 40609, 40627, +40637, 40639, 40693, 40697, 40699, 40709, 40739, 40751, 40759, 40763, 40771, +40787, 40801, 40813, 40819, 40823, 40829, 40841, 40847, 40849, 40853, 40867, +40879, 40883, 40897, 40903, 40927, 40933, 40939, 40949, 40961, 40973, 40993, +41011, 41017, 41023, 41039, 41047, 41051, 41057, 41077, 41081, 41113, 41117, +41131, 41141, 41143, 41149, 41161, 41177, 41179, 41183, 41189, 41201, 41203, +41213, 41221, 41227, 41231, 41233, 41243, 41257, 41263, 41269, 41281, 41299, +41333, 41341, 41351, 41357, 41381, 41387, 41389, 41399, 41411, 41413, 41443, +41453, 41467, 41479, 41491, 41507, 41513, 41519, 41521, 41539, 41543, 41549, +41579, 41593, 41597, 41603, 41609, 41611, 41617, 41621, 41627, 41641, 41647, +41651, 41659, 41669, 41681, 41687, 41719, 41729, 41737, 41759, 41761, 41771, +41777, 41801, 41809, 41813, 41843, 41849, 41851, 41863, 41879, 41887, 41893, +41897, 41903, 41911, 41927, 41941, 41947, 41953, 41957, 41959, 41969, 41981, +41983, 41999, 42013, 42017, 42019, 42023, 42043, 42061, 42071, 42073, 42083, +42089, 42101, 42131, 42139, 42157, 42169, 42179, 42181, 42187, 42193, 42197, +42209, 42221, 42223, 42227, 42239, 42257, 42281, 42283, 42293, 42299, 42307, +42323, 42331, 42337, 42349, 42359, 42373, 42379, 42391, 42397, 42403, 42407, +42409, 42433, 42437, 42443, 42451, 42457, 42461, 42463, 42467, 42473, 42487, +42491, 42499, 42509, 42533, 42557, 42569, 42571, 42577, 42589, 42611, 42641, +42643, 42649, 42667, 42677, 42683, 42689, 42697, 42701, 42703, 42709, 42719, +42727, 42737, 42743, 42751, 42767, 42773, 42787, 42793, 42797, 42821, 42829, +42839, 42841, 42853, 42859, 42863, 42899, 42901, 42923, 42929, 42937, 42943, +42953, 42961, 42967, 42979, 42989, 43003, 43013, 43019, 43037, 43049, 43051, +43063, 43067, 43093, 43103, 43117, 43133, 43151, 43159, 43177, 43189, 43201, +43207, 43223, 43237, 43261, 43271, 43283, 43291, 43313, 43319, 43321, 43331, +43391, 43397, 43399, 43403, 43411, 43427, 43441, 43451, 43457, 43481, 43487, +43499, 43517, 43541, 43543, 43573, 43577, 43579, 43591, 43597, 43607, 43609, +43613, 43627, 43633, 43649, 43651, 43661, 43669, 43691, 43711, 43717, 43721, +43753, 43759, 43777, 43781, 43783, 43787, 43789, 43793, 43801, 43853, 43867, +43889, 43891, 43913, 43933, 43943, 43951, 43961, 43963, 43969, 43973, 43987, +43991, 43997, 44017, 44021, 44027, 44029, 44041, 44053, 44059, 44071, 44087, +44089, 44101, 44111, 44119, 44123, 44129, 44131, 44159, 44171, 44179, 44189, +44201, 44203, 44207, 44221, 44249, 44257, 44263, 44267, 44269, 44273, 44279, +44281, 44293, 44351, 44357, 44371, 44381, 44383, 44389, 44417, 44449, 44453, +44483, 44491, 44497, 44501, 44507, 44519, 44531, 44533, 44537, 44543, 44549, +44563, 44579, 44587, 44617, 44621, 44623, 44633, 44641, 44647, 44651, 44657, +44683, 44687, 44699, 44701, 44711, 44729, 44741, 44753, 44771, 44773, 44777, +44789, 44797, 44809, 44819, 44839, 44843, 44851, 44867, 44879, 44887, 44893, +44909, 44917, 44927, 44939, 44953, 44959, 44963, 44971, 44983, 44987, 45007, +45013, 45053, 45061, 45077, 45083, 45119, 45121, 45127, 45131, 45137, 45139, +45161, 45179, 45181, 45191, 45197, 45233, 45247, 45259, 45263, 45281, 45289, +45293, 45307, 45317, 45319, 45329, 45337, 45341, 45343, 45361, 45377, 45389, +45403, 45413, 45427, 45433, 45439, 45481, 45491, 45497, 45503, 45523, 45533, +45541, 45553, 45557, 45569, 45587, 45589, 45599, 45613, 45631, 45641, 45659, +45667, 45673, 45677, 45691, 45697, 45707, 45737, 45751, 45757, 45763, 45767, +45779, 45817, 45821, 45823, 45827, 45833, 45841, 45853, 45863, 45869, 45887, +45893, 45943, 45949, 45953, 45959, 45971, 45979, 45989, 46021, 46027, 46049, +46051, 46061, 46073, 46091, 46093, 46099, 46103, 46133, 46141, 46147, 46153, +46171, 46181, 46183, 46187, 46199, 46219, 46229, 46237, 46261, 46271, 46273, +46279, 46301, 46307, 46309, 46327, 46337, 46349, 46351, 46381, 46399, 46411, +46439, 46441, 46447, 46451, 46457, 46471, 46477, 46489, 46499, 46507, 46511, +46523, 46549, 46559, 46567, 46573, 46589, 46591, 46601, 46619, 46633, 46639, +46643, 46649, 46663, 46679, 46681, 46687, 46691, 46703, 46723, 46727, 46747, +46751, 46757, 46769, 46771, 46807, 46811, 46817, 46819, 46829, 46831, 46853, +46861, 46867, 46877, 46889, 46901, 46919, 46933, 46957, 46993, 46997, 47017, +47041, 47051, 47057, 47059, 47087, 47093, 47111, 47119, 47123, 47129, 47137, +47143, 47147, 47149, 47161, 47189, 47207, 47221, 47237, 47251, 47269, 47279, +47287, 47293, 47297, 47303, 47309, 47317, 47339, 47351, 47353, 47363, 47381, +47387, 47389, 47407, 47417, 47419, 47431, 47441, 47459, 47491, 47497, 47501, +47507, 47513, 47521, 47527, 47533, 47543, 47563, 47569, 47581, 47591, 47599, +47609, 47623, 47629, 47639, 47653, 47657, 47659, 47681, 47699, 47701, 47711, +47713, 47717, 47737, 47741, 47743, 47777, 47779, 47791, 47797, 47807, 47809, +47819, 47837, 47843, 47857, 47869, 47881, 47903, 47911, 47917, 47933, 47939, +47947, 47951, 47963, 47969, 47977, 47981, 48017, 48023, 48029, 48049, 48073, +48079, 48091, 48109, 48119, 48121, 48131, 48157, 48163, 48179, 48187, 48193, +48197, 48221, 48239, 48247, 48259, 48271, 48281, 48299, 48311, 48313, 48337, +48341, 48353, 48371, 48383, 48397, 48407, 48409, 48413, 48437, 48449, 48463, +48473, 48479, 48481, 48487, 48491, 48497, 48523, 48527, 48533, 48539, 48541, +48563, 48571, 48589, 48593, 48611, 48619, 48623, 48647, 48649, 48661, 48673, +48677, 48679, 48731, 48733, 48751, 48757, 48761, 48767, 48779, 48781, 48787, +48799, 48809, 48817, 48821, 48823, 48847, 48857, 48859, 48869, 48871, 48883, +48889, 48907, 48947, 48953, 48973, 48989, 48991, 49003, 49009, 49019, 49031, +49033, 49037, 49043, 49057, 49069, 49081, 49103, 49109, 49117, 49121, 49123, +49139, 49157, 49169, 49171, 49177, 49193, 49199, 49201, 49207, 49211, 49223, +49253, 49261, 49277, 49279, 49297, 49307, 49331, 49333, 49339, 49363, 49367, +49369, 49391, 49393, 49409, 49411, 49417, 49429, 49433, 49451, 49459, 49463, +49477, 49481, 49499, 49523, 49529, 49531, 49537, 49547, 49549, 49559, 49597, +49603, 49613, 49627, 49633, 49639, 49663, 49667, 49669, 49681, 49697, 49711, +49727, 49739, 49741, 49747, 49757, 49783, 49787, 49789, 49801, 49807, 49811, +49823, 49831, 49843, 49853, 49871, 49877, 49891, 49919, 49921, 49927, 49937, +49939, 49943, 49957, 49991, 49993, 49999, 50021, 50023, 50033, 50047, 50051, +50053, 50069, 50077, 50087, 50093, 50101, 50111, 50119, 50123, 50129, 50131, +50147, 50153, 50159, 50177, 50207, 50221, 50227, 50231, 50261, 50263, 50273, +50287, 50291, 50311, 50321, 50329, 50333, 50341, 50359, 50363, 50377, 50383, +50387, 50411, 50417, 50423, 50441, 50459, 50461, 50497, 50503, 50513, 50527, +50539, 50543, 50549, 50551, 50581, 50587, 50591, 50593, 50599, 50627, 50647, +50651, 50671, 50683, 50707, 50723, 50741, 50753, 50767, 50773, 50777, 50789, +50821, 50833, 50839, 50849, 50857, 50867, 50873, 50891, 50893, 50909, 50923, +50929, 50951, 50957, 50969, 50971, 50989, 50993, 51001, 51031, 51043, 51047, +51059, 51061, 51071, 51109, 51131, 51133, 51137, 51151, 51157, 51169, 51193, +51197, 51199, 51203, 51217, 51229, 51239, 51241, 51257, 51263, 51283, 51287, +51307, 51329, 51341, 51343, 51347, 51349, 51361, 51383, 51407, 51413, 51419, +51421, 51427, 51431, 51437, 51439, 51449, 51461, 51473, 51479, 51481, 51487, +51503, 51511, 51517, 51521, 51539, 51551, 51563, 51577, 51581, 51593, 51599, +51607, 51613, 51631, 51637, 51647, 51659, 51673, 51679, 51683, 51691, 51713, +51719, 51721, 51749, 51767, 51769, 51787, 51797, 51803, 51817, 51827, 51829, +51839, 51853, 51859, 51869, 51871, 51893, 51899, 51907, 51913, 51929, 51941, +51949, 51971, 51973, 51977, 51991, 52009, 52021, 52027, 52051, 52057, 52067, +52069, 52081, 52103, 52121, 52127, 52147, 52153, 52163, 52177, 52181, 52183, +52189, 52201, 52223, 52237, 52249, 52253, 52259, 52267, 52289, 52291, 52301, +52313, 52321, 52361, 52363, 52369, 52379, 52387, 52391, 52433, 52453, 52457, +52489, 52501, 52511, 52517, 52529, 52541, 52543, 52553, 52561, 52567, 52571, +52579, 52583, 52609, 52627, 52631, 52639, 52667, 52673, 52691, 52697, 52709, +52711, 52721, 52727, 52733, 52747, 52757, 52769, 52783, 52807, 52813, 52817, +52837, 52859, 52861, 52879, 52883, 52889, 52901, 52903, 52919, 52937, 52951, +52957, 52963, 52967, 52973, 52981, 52999, 53003, 53017, 53047, 53051, 53069, +53077, 53087, 53089, 53093, 53101, 53113, 53117, 53129, 53147, 53149, 53161, +53171, 53173, 53189, 53197, 53201, 53231, 53233, 53239, 53267, 53269, 53279, +53281, 53299, 53309, 53323, 53327, 53353, 53359, 53377, 53381, 53401, 53407, +53411, 53419, 53437, 53441, 53453, 53479, 53503, 53507, 53527, 53549, 53551, +53569, 53591, 53593, 53597, 53609, 53611, 53617, 53623, 53629, 53633, 53639, +53653, 53657, 53681, 53693, 53699, 53717, 53719, 53731, 53759, 53773, 53777, +53783, 53791, 53813, 53819, 53831, 53849, 53857, 53861, 53881, 53887, 53891, +53897, 53899, 53917, 53923, 53927, 53939, 53951, 53959, 53987, 53993, 54001, +54011, 54013, 54037, 54049, 54059, 54083, 54091, 54101, 54121, 54133, 54139, +54151, 54163, 54167, 54181, 54193, 54217, 54251, 54269, 54277, 54287, 54293, +54311, 54319, 54323, 54331, 54347, 54361, 54367, 54371, 54377, 54401, 54403, +54409, 54413, 54419, 54421, 54437, 54443, 54449, 54469, 54493, 54497, 54499, +54503, 54517, 54521, 54539, 54541, 54547, 54559, 54563, 54577, 54581, 54583, +54601, 54617, 54623, 54629, 54631, 54647, 54667, 54673, 54679, 54709, 54713, +54721, 54727, 54751, 54767, 54773, 54779, 54787, 54799, 54829, 54833, 54851, +54869, 54877, 54881, 54907, 54917, 54919, 54941, 54949, 54959, 54973, 54979, +54983, 55001, 55009, 55021, 55049, 55051, 55057, 55061, 55073, 55079, 55103, +55109, 55117, 55127, 55147, 55163, 55171, 55201, 55207, 55213, 55217, 55219, +55229, 55243, 55249, 55259, 55291, 55313, 55331, 55333, 55337, 55339, 55343, +55351, 55373, 55381, 55399, 55411, 55439, 55441, 55457, 55469, 55487, 55501, +55511, 55529, 55541, 55547, 55579, 55589, 55603, 55609, 55619, 55621, 55631, +55633, 55639, 55661, 55663, 55667, 55673, 55681, 55691, 55697, 55711, 55717, +55721, 55733, 55763, 55787, 55793, 55799, 55807, 55813, 55817, 55819, 55823, +55829, 55837, 55843, 55849, 55871, 55889, 55897, 55901, 55903, 55921, 55927, +55931, 55933, 55949, 55967, 55987, 55997, 56003, 56009, 56039, 56041, 56053, +56081, 56087, 56093, 56099, 56101, 56113, 56123, 56131, 56149, 56167, 56171, +56179, 56197, 56207, 56209, 56237, 56239, 56249, 56263, 56267, 56269, 56299, +56311, 56333, 56359, 56369, 56377, 56383, 56393, 56401, 56417, 56431, 56437, +56443, 56453, 56467, 56473, 56477, 56479, 56489, 56501, 56503, 56509, 56519, +56527, 56531, 56533, 56543, 56569, 56591, 56597, 56599, 56611, 56629, 56633, +56659, 56663, 56671, 56681, 56687, 56701, 56711, 56713, 56731, 56737, 56747, +56767, 56773, 56779, 56783, 56807, 56809, 56813, 56821, 56827, 56843, 56857, +56873, 56891, 56893, 56897, 56909, 56911, 56921, 56923, 56929, 56941, 56951, +56957, 56963, 56983, 56989, 56993, 56999, 57037, 57041, 57047, 57059, 57073, +57077, 57089, 57097, 57107, 57119, 57131, 57139, 57143, 57149, 57163, 57173, +57179, 57191, 57193, 57203, 57221, 57223, 57241, 57251, 57259, 57269, 57271, +57283, 57287, 57301, 57329, 57331, 57347, 57349, 57367, 57373, 57383, 57389, +57397, 57413, 57427, 57457, 57467, 57487, 57493, 57503, 57527, 57529, 57557, +57559, 57571, 57587, 57593, 57601, 57637, 57641, 57649, 57653, 57667, 57679, +57689, 57697, 57709, 57713, 57719, 57727, 57731, 57737, 57751, 57773, 57781, +57787, 57791, 57793, 57803, 57809, 57829, 57839, 57847, 57853, 57859, 57881, +57899, 57901, 57917, 57923, 57943, 57947, 57973, 57977, 57991, 58013, 58027, +58031, 58043, 58049, 58057, 58061, 58067, 58073, 58099, 58109, 58111, 58129, +58147, 58151, 58153, 58169, 58171, 58189, 58193, 58199, 58207, 58211, 58217, +58229, 58231, 58237, 58243, 58271, 58309, 58313, 58321, 58337, 58363, 58367, +58369, 58379, 58391, 58393, 58403, 58411, 58417, 58427, 58439, 58441, 58451, +58453, 58477, 58481, 58511, 58537, 58543, 58549, 58567, 58573, 58579, 58601, +58603, 58613, 58631, 58657, 58661, 58679, 58687, 58693, 58699, 58711, 58727, +58733, 58741, 58757, 58763, 58771, 58787, 58789, 58831, 58889, 58897, 58901, +58907, 58909, 58913, 58921, 58937, 58943, 58963, 58967, 58979, 58991, 58997, +59009, 59011, 59021, 59023, 59029, 59051, 59053, 59063, 59069, 59077, 59083, +59093, 59107, 59113, 59119, 59123, 59141, 59149, 59159, 59167, 59183, 59197, +59207, 59209, 59219, 59221, 59233, 59239, 59243, 59263, 59273, 59281, 59333, +59341, 59351, 59357, 59359, 59369, 59377, 59387, 59393, 59399, 59407, 59417, +59419, 59441, 59443, 59447, 59453, 59467, 59471, 59473, 59497, 59509, 59513, +59539, 59557, 59561, 59567, 59581, 59611, 59617, 59621, 59627, 59629, 59651, +59659, 59663, 59669, 59671, 59693, 59699, 59707, 59723, 59729, 59743, 59747, +59753, 59771, 59779, 59791, 59797, 59809, 59833, 59863, 59879, 59887, 59921, +59929, 59951, 59957, 59971, 59981, 59999, 60013, 60017, 60029, 60037, 60041, +60077, 60083, 60089, 60091, 60101, 60103, 60107, 60127, 60133, 60139, 60149, +60161, 60167, 60169, 60209, 60217, 60223, 60251, 60257, 60259, 60271, 60289, +60293, 60317, 60331, 60337, 60343, 60353, 60373, 60383, 60397, 60413, 60427, +60443, 60449, 60457, 60493, 60497, 60509, 60521, 60527, 60539, 60589, 60601, +60607, 60611, 60617, 60623, 60631, 60637, 60647, 60649, 60659, 60661, 60679, +60689, 60703, 60719, 60727, 60733, 60737, 60757, 60761, 60763, 60773, 60779, +60793, 60811, 60821, 60859, 60869, 60887, 60889, 60899, 60901, 60913, 60917, +60919, 60923, 60937, 60943, 60953, 60961, 61001, 61007, 61027, 61031, 61043, +61051, 61057, 61091, 61099, 61121, 61129, 61141, 61151, 61153, 61169, 61211, +61223, 61231, 61253, 61261, 61283, 61291, 61297, 61331, 61333, 61339, 61343, +61357, 61363, 61379, 61381, 61403, 61409, 61417, 61441, 61463, 61469, 61471, +61483, 61487, 61493, 61507, 61511, 61519, 61543, 61547, 61553, 61559, 61561, +61583, 61603, 61609, 61613, 61627, 61631, 61637, 61643, 61651, 61657, 61667, +61673, 61681, 61687, 61703, 61717, 61723, 61729, 61751, 61757, 61781, 61813, +61819, 61837, 61843, 61861, 61871, 61879, 61909, 61927, 61933, 61949, 61961, +61967, 61979, 61981, 61987, 61991, 62003, 62011, 62017, 62039, 62047, 62053, +62057, 62071, 62081, 62099, 62119, 62129, 62131, 62137, 62141, 62143, 62171, +62189, 62191, 62201, 62207, 62213, 62219, 62233, 62273, 62297, 62299, 62303, +62311, 62323, 62327, 62347, 62351, 62383, 62401, 62417, 62423, 62459, 62467, +62473, 62477, 62483, 62497, 62501, 62507, 62533, 62539, 62549, 62563, 62581, +62591, 62597, 62603, 62617, 62627, 62633, 62639, 62653, 62659, 62683, 62687, +62701, 62723, 62731, 62743, 62753, 62761, 62773, 62791, 62801, 62819, 62827, +62851, 62861, 62869, 62873, 62897, 62903, 62921, 62927, 62929, 62939, 62969, +62971, 62981, 62983, 62987, 62989, 63029, 63031, 63059, 63067, 63073, 63079, +63097, 63103, 63113, 63127, 63131, 63149, 63179, 63197, 63199, 63211, 63241, +63247, 63277, 63281, 63299, 63311, 63313, 63317, 63331, 63337, 63347, 63353, +63361, 63367, 63377, 63389, 63391, 63397, 63409, 63419, 63421, 63439, 63443, +63463, 63467, 63473, 63487, 63493, 63499, 63521, 63527, 63533, 63541, 63559, +63577, 63587, 63589, 63599, 63601, 63607, 63611, 63617, 63629, 63647, 63649, +63659, 63667, 63671, 63689, 63691, 63697, 63703, 63709, 63719, 63727, 63737, +63743, 63761, 63773, 63781, 63793, 63799, 63803, 63809, 63823, 63839, 63841, +63853, 63857, 63863, 63901, 63907, 63913, 63929, 63949, 63977, 63997, 64007, +64013, 64019, 64033, 64037, 64063, 64067, 64081, 64091, 64109, 64123, 64151, +64153, 64157, 64171, 64187, 64189, 64217, 64223, 64231, 64237, 64271, 64279, +64283, 64301, 64303, 64319, 64327, 64333, 64373, 64381, 64399, 64403, 64433, +64439, 64451, 64453, 64483, 64489, 64499, 64513, 64553, 64567, 64577, 64579, +64591, 64601, 64609, 64613, 64621, 64627, 64633, 64661, 64663, 64667, 64679, +64693, 64709, 64717, 64747, 64763, 64781, 64783, 64793, 64811, 64817, 64849, +64853, 64871, 64877, 64879, 64891, 64901, 64919, 64921, 64927, 64937, 64951, +64969, 64997, 65003, 65011, 65027, 65029, 65033, 65053, 65063, 65071, 65089, +65099, 65101, 65111, 65119, 65123, 65129, 65141, 65147, 65167, 65171, 65173, +65179, 65183, 65203, 65213, 65239, 65257, 65267, 65269, 65287, 65293, 65309, +65323, 65327, 65353, 65357, 65371, 65381, 65393, 65407, 65413, 65419, 65423, +65437, 65447, 65449, 65479, 65497, 65519, 65521, 0 }; + +const u64bit PRIME_PRODUCTS[PRIME_PRODUCTS_TABLE_SIZE] = { +(u64bit) 0xFF658BDE2F2A43DFULL, (u64bit) 0xFEEB94CD535119EDULL, (u64bit) 0xFA921839EC24DDD5ULL, (u64bit) 0xFDDA766C77E1E605ULL, +(u64bit) 0xFF3024B0EB4EE333ULL, (u64bit) 0xFEEE350BBC92F4DFULL, (u64bit) 0xFFC724B7D011D01BULL, (u64bit) 0xFEED34B826C33B05ULL, +(u64bit) 0xFE69D8DE3F85C6E3ULL, (u64bit) 0xFE3B48909250918FULL, (u64bit) 0xFF8EC0CE9C632429ULL, (u64bit) 0xFFD92A5C78226D6BULL, +(u64bit) 0xFFB4BFB0C65133CFULL, (u64bit) 0xFE77113704902C57ULL, (u64bit) 0xFF8A21D222EA81FDULL, (u64bit) 0xFEDA1299661CF5ABULL, +(u64bit) 0xFF4CE86187737D0DULL, (u64bit) 0xFFD26443A07F519DULL, (u64bit) 0xFFA817B7191D7967ULL, (u64bit) 0xFF00EDC142868873ULL, +(u64bit) 0xFFB9C6D7F7A239B7ULL, (u64bit) 0xFFE76D3481E98E39ULL, (u64bit) 0xFF76D5432584120DULL, (u64bit) 0xFFAA499F071EC705ULL, +(u64bit) 0xFEB5198F05722E59ULL, (u64bit) 0xFF7E0431CA41107FULL, (u64bit) 0xFFCFD52FEDDC928FULL, (u64bit) 0xFE0EA42537BC6ABFULL, +(u64bit) 0xFF64937896876925ULL, (u64bit) 0xFC6FC87E811607D3ULL, (u64bit) 0xFFBF600E6CDD0F4FULL, (u64bit) 0xFF022700FE658243ULL, +(u64bit) 0xFF2E21166779D6B9ULL, (u64bit) 0xFFC224624C665C33ULL, (u64bit) 0xFF1372F41FF177ADULL, (u64bit) 0xFF31E57E972D0C13ULL, +(u64bit) 0xFFA891F866404D23ULL, (u64bit) 0xFF7BF13EF716E9A3ULL, (u64bit) 0xFE51CAFD9466E733ULL, (u64bit) 0xFDA1CF55F6D6336FULL, +(u64bit) 0xFFAF6C040ED0950FULL, (u64bit) 0xFFAA1725F40BA269ULL, (u64bit) 0xFEC593BC3570BEEBULL, (u64bit) 0xFEE05B35B426F413ULL, +(u64bit) 0xFFCA5209A08890F9ULL, (u64bit) 0xFFED8AF70EB0CC89ULL, (u64bit) 0xFF3F98E3E27860A5ULL, (u64bit) 0xFF92FECD017FF9F7ULL, +(u64bit) 0xFEFA655B2609018FULL, (u64bit) 0xFFFC51D15AAC7B77ULL, (u64bit) 0xFEF5007E71420DB1ULL, (u64bit) 0xFFEC4784141332D1ULL, +(u64bit) 0xFE8384ED4E1D21CDULL, (u64bit) 0xFFD3FF614D3ECC47ULL, (u64bit) 0xFFDE5166FD540313ULL, (u64bit) 0xFF5320ECED04B26FULL, +(u64bit) 0xFF223980F122FF75ULL, (u64bit) 0xFF19C1F27CB1B4A5ULL, (u64bit) 0xFF0F1DFC9DA9523BULL, (u64bit) 0xFF82DE7B387F5427ULL, +(u64bit) 0xFF9A026BA87314E3ULL, (u64bit) 0xFFAC7FF3ACE64E77ULL, (u64bit) 0xFF808EB2FD5873C3ULL, (u64bit) 0xFE983ED5BB363301ULL, +(u64bit) 0xFF714856DB2CFE95ULL, (u64bit) 0xFF84E1510CF3EB9FULL, (u64bit) 0xFF29D04C1DA0B115ULL, (u64bit) 0xFFBCF3BF9433552FULL, +(u64bit) 0xFF32203D58A4C473ULL, (u64bit) 0xFFF00910A15021C3ULL, (u64bit) 0xFDE93041F28240ADULL, (u64bit) 0xFFC518BCD81C03C5ULL, +(u64bit) 0xFEF504CD8BB9CBDDULL, (u64bit) 0xFEB8FFBFFF116A6BULL, (u64bit) 0xFF7642E0785ADA23ULL, (u64bit) 0xFFECF068800FD50DULL, +(u64bit) 0xFFD703577CA247A7ULL, (u64bit) 0xFF54C0ECAD2C9691ULL, (u64bit) 0xFFC031706B8C72F5ULL, (u64bit) 0xFFE59E5CA58BBDF5ULL, +(u64bit) 0xFFF31FAFFD3B331DULL, (u64bit) 0xFF64DDF32349FF6DULL, (u64bit) 0xFFE38309D0BD4A51ULL, (u64bit) 0xFF8C934F76B3C737ULL, +(u64bit) 0xFFDC80B4BAEAFC1FULL, (u64bit) 0xFFCC1FE4C856FBD9ULL, (u64bit) 0xFFDB5976DDF601FDULL, (u64bit) 0xFFD3DD25F424433DULL, +(u64bit) 0xFFC00FA367E746C7ULL, (u64bit) 0xFFE08BF011CC854FULL, (u64bit) 0xFFC3F21982468F6DULL, (u64bit) 0xFFDA6C52478A76DFULL, +(u64bit) 0xFFC67D95AADED363ULL, (u64bit) 0xFFD605D18C3AFC65ULL, (u64bit) 0xFFE828C9D698F1DFULL, (u64bit) 0xFFBE5098D83B7737ULL, +(u64bit) 0xFF79EB34474ABFB9ULL, (u64bit) 0xFFD27AEED0786363ULL, (u64bit) 0xFFD0FE27B77C271FULL, (u64bit) 0xFFFBB6563BD065EFULL, +(u64bit) 0xFFF3638F8635E1EBULL, (u64bit) 0xFFBE862C22C9F065ULL, (u64bit) 0xFF44712D8488A01DULL, (u64bit) 0xFF7EEC97F9913111ULL, +(u64bit) 0xFFC23CC78CB12AB1ULL, (u64bit) 0xFFF390FE85F81D3DULL, (u64bit) 0xFFE8EA21A0FB9931ULL, (u64bit) 0xFFB9D42D17A93385ULL, +(u64bit) 0xFFCDB63AB21E904DULL, (u64bit) 0xFF5EB7F2210D33DFULL, (u64bit) 0xFFE6F6C7BB60C9DFULL, (u64bit) 0xFFAD4CA8DC26D699ULL, +(u64bit) 0xFF7BE75BD21DCA51ULL, (u64bit) 0xFEF89CE23CB61789ULL, (u64bit) 0xFF40ECA3CCA22CE5ULL, (u64bit) 0xF52BDF080F7ABA6FULL, +(u64bit) 0xEC8F38C8B28E0493ULL, (u64bit) 0xE68E732A2ABED62FULL, (u64bit) 0xE21A13779E0CCDC7ULL, (u64bit) 0xD823C075C325191BULL, +(u64bit) 0xD1B284C91EED248BULL, (u64bit) 0xCBA5A08068E8C1F7ULL, (u64bit) 0xC483EE5A2228985DULL, (u64bit) 0xBCAEE9F787AC75EBULL, +(u64bit) 0xB782DAB1B77D3E09ULL, (u64bit) 0xB0D77226F15E387BULL, (u64bit) 0xAA2A8727D47941CDULL, (u64bit) 0xA4A45682E9CE533DULL, +(u64bit) 0x9CAF15AF4CE7FCF7ULL, (u64bit) 0x94C051DD15537305ULL, (u64bit) 0x9006D2FBD933A297ULL, (u64bit) 0x8C4DED05F19B7399ULL, +(u64bit) 0x884FD7A270AD1B1BULL, (u64bit) 0x83C687D33F238D4BULL, (u64bit) 0xFF62E2BAE50C6C16ULL, (u64bit) 0x7A59E1FD9D203DBBULL, +(u64bit) 0x764F1DC07B0E442DULL, (u64bit) 0x72732FE1F2023153ULL, (u64bit) 0x6E373B550764872FULL, (u64bit) 0x680FFFD267C5F3FFULL, +(u64bit) 0x6206BFEC14F1CFC5ULL, (u64bit) 0x5FA6F70CFD587265ULL, (u64bit) 0x5CC7A1B4F6DF9823ULL, (u64bit) 0x599291B29311407FULL, +(u64bit) 0xFF3CEBD359B67EF9ULL, (u64bit) 0x51C573C14F289F6DULL, (u64bit) 0x4FA265B31B73C6DFULL, (u64bit) 0x4B3154ACBD077DEDULL, +(u64bit) 0x4785C96B29A1E437ULL, (u64bit) 0x451F887F646CF763ULL, (u64bit) 0x429DC254C5490571ULL, (u64bit) 0x408410840EAE2883ULL, +(u64bit) 0x3E12CC83606624F3ULL, (u64bit) 0x3A70D774B821DA71ULL, (u64bit) 0x37A21449A196A825ULL, (u64bit) 0x34C5D056E2278B81ULL, +(u64bit) 0xFA0C6CAB29D8E297ULL, (u64bit) 0x2FA5AEC982A5972BULL, (u64bit) 0x2D6831749426068FULL, (u64bit) 0x2B7F876418155CA7ULL, +(u64bit) 0x2A1B897ED2AB433DULL, (u64bit) 0x28C9430D0F92132FULL, (u64bit) 0x26DF879EBF12E103ULL, (u64bit) 0xFD2FAB4CA364D43BULL, +(u64bit) 0x22B5B4FC40D4C35FULL, (u64bit) 0x209298AA84D7E6A1ULL, (u64bit) 0x1ED4B9F11445F1E7ULL, (u64bit) 0x1DC6D2DD416CC91DULL, +(u64bit) 0x1C1517A52E37C3EFULL, (u64bit) 0x1A808916125AEF2FULL, (u64bit) 0x197A2FB2938FF13DULL, (u64bit) 0x1814AA6C087B561DULL, +(u64bit) 0xFB3B173E72947609ULL, (u64bit) 0x1571187A8E3D4D6BULL, (u64bit) 0x13D306D29263C139ULL, (u64bit) 0xF8AEC6ADA137E865ULL, +(u64bit) 0x123EA204BAB48731ULL, (u64bit) 0x11012099D202F297ULL, (u64bit) 0x10290E15797C21BDULL, (u64bit) 0x0F3AB38E679D6317ULL, +(u64bit) 0xF50B5505D593FCF9ULL, (u64bit) 0xFF23754F7F2052B5ULL, (u64bit) 0x0CC52D96BC2E5A2DULL, (u64bit) 0x0BF80EAD87B228E5ULL, +(u64bit) 0x0B59A623082C9171ULL, (u64bit) 0xF44E28B9221A433BULL, (u64bit) 0x09DB5CDD2505EABDULL, (u64bit) 0x09638C123BCAB351ULL, +(u64bit) 0xFDB9AE6935254CD3ULL, (u64bit) 0xFFE30D7E4F02F163ULL, (u64bit) 0x07CF1FC053B9C61FULL, (u64bit) 0x0789244FF1705821ULL, +(u64bit) 0x06FBD05649B0B9C7ULL, (u64bit) 0xEF9713EC6A0C250BULL, (u64bit) 0xF47691AD6AA9F0DBULL, (u64bit) 0xF2A8EB02CB08CA51ULL, +(u64bit) 0xF9559D40380A20E1ULL, (u64bit) 0x04E15138A5B9BF43ULL, (u64bit) 0xEECD739EA48F3ABBULL, (u64bit) 0xF76E7E7530574E79ULL, +(u64bit) 0xF8393D2E42D7D277ULL, (u64bit) 0xF666F9AD3A16D173ULL, (u64bit) 0xF403C629749F3ED5ULL, (u64bit) 0xFBD7EC45F220A473ULL, +(u64bit) 0xFA8AFF7491B234FDULL, (u64bit) 0xFF471CE534D1F537ULL, (u64bit) 0xF4BEBFDD9C54CEC9ULL, (u64bit) 0xDD04722310A6CE9DULL, +(u64bit) 0xFD8071236214FA05ULL, (u64bit) 0xFBCA07B399A482DDULL, (u64bit) 0xFD9642C104864C17ULL, (u64bit) 0xFA525105AADEFA39ULL, +(u64bit) 0xF71122156406E645ULL, (u64bit) 0xFF415FDFD1247539ULL, (u64bit) 0xFB709936F52446AFULL, (u64bit) 0xFF7734CCB806CDA7ULL, +(u64bit) 0xF801E9A88CD3D70DULL, (u64bit) 0xFC0C00AC9BCC5491ULL, (u64bit) 0xFF462CD8E52ED221ULL, (u64bit) 0xFC97426300FCE331ULL, +(u64bit) 0xFEB3049C5E37A059ULL, (u64bit) 0xFFFC8AB1E05051CDULL, (u64bit) 0xFE5F4621F2D9FE63ULL, (u64bit) 0xFE931DB54FC5D521ULL, +(u64bit) 0xFFDE43D960FE42A5ULL, (u64bit) 0xFFDBFAD1B802BDB5ULL, (u64bit) 0xFF23C485F6B7BF53ULL, (u64bit) 0xFFC98F169C8DF21BULL, +(u64bit) 0xF1609D0E2E564D01ULL, (u64bit) 0xCB10B976C333834BULL, (u64bit) 0x9B52037A38DAB8F9ULL, (u64bit) 0x800E88FF5E929095ULL, +(u64bit) 0x55A9AD1C21F5E173ULL, (u64bit) 0x3D1A64E4E555D699ULL, (u64bit) 0x2A5D1D73694F7B93ULL, (u64bit) 0x198F4260D8807623ULL, +(u64bit) 0x140D45BB525C35EBULL, (u64bit) 0x102F4743FF914EEBULL, (u64bit) 0x0CB114936A734FBFULL, (u64bit) 0x096D97150B7B0A71ULL, +(u64bit) 0x06F06B90B850C2E5ULL, (u64bit) 0x053B17A0D7F7386BULL, (u64bit) 0xE3AD1CE3C82FE6A5ULL, (u64bit) 0xDAE968B4B710E857ULL, +(u64bit) 0xFA2DC15B2C96BE77ULL, (u64bit) 0xF1FF5F22AF135BD9ULL, (u64bit) 0xFC65C5CAAA878A13ULL, (u64bit) 0xFB9427EB08CF9C11ULL, +(u64bit) 0xCCB12B6FEBFE285DULL, (u64bit) 0x5BAADA462B48F999ULL, (u64bit) 0x2E53167EC64B703BULL, (u64bit) 0x1264ED670CD61961ULL, +(u64bit) 0x071F216A9AB74E2DULL, (u64bit) 0xEE26503C1266CE55ULL, (u64bit) 0x4C6004C7E404E4B5ULL, (u64bit) 0xCB649E41ECE95F85ULL +}; + +} diff --git a/botan/src/math/numbertheory/reducer.cpp b/botan/src/math/numbertheory/reducer.cpp new file mode 100644 index 0000000..fbd675e --- /dev/null +++ b/botan/src/math/numbertheory/reducer.cpp @@ -0,0 +1,97 @@ +/* +* Modular Reducer +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/reducer.h> +#include <botan/numthry.h> +#include <botan/mp_core.h> + +namespace Botan { + +/* +* Modular_Reducer Constructor +*/ +Modular_Reducer::Modular_Reducer(const BigInt& mod) + { + if(mod <= 0) + throw Invalid_Argument("Modular_Reducer: modulus must be positive"); + + modulus = mod; + mod_words = modulus.sig_words(); + + modulus_2 = Botan::square(modulus); + mod2_words = modulus_2.sig_words(); + + mu = BigInt(BigInt::Power2, 2 * MP_WORD_BITS * mod_words) / modulus; + mu_words = mu.sig_words(); + } + +/* +* Barrett Reduction +*/ +BigInt Modular_Reducer::reduce(const BigInt& x) const + { + if(mod_words == 0) + throw Invalid_State("Modular_Reducer: Never initalized"); + + BigInt t1 = x; + t1.set_sign(BigInt::Positive); + + if(t1 < modulus) + { + if(x.is_negative() && t1.is_nonzero()) + return modulus - t1; + return x; + } + + if(t1 >= modulus_2) + return (x % modulus); + + t1 >>= (MP_WORD_BITS * (mod_words - 1)); + t1 *= mu; + t1 >>= (MP_WORD_BITS * (mod_words + 1)); + + t1 *= modulus; + t1.mask_bits(MP_WORD_BITS * (mod_words+1)); + + BigInt t2 = x; + t2.set_sign(BigInt::Positive); + t2.mask_bits(MP_WORD_BITS * (mod_words+1)); + + t1 = t2 - t1; + + if(t1.is_negative()) + { + BigInt b_to_k1(BigInt::Power2, MP_WORD_BITS * (mod_words+1)); + t1 += b_to_k1; + } + + while(t1 >= modulus) + t1 -= modulus; + + if(x.is_negative() && t1.is_nonzero()) + t1 = modulus - t1; + + return t1; + } + +/* +* Multiply, followed by a reduction +*/ +BigInt Modular_Reducer::multiply(const BigInt& x, const BigInt& y) const + { + return reduce(x * y); + } + +/* +* Square, followed by a reduction +*/ +BigInt Modular_Reducer::square(const BigInt& x) const + { + return reduce(Botan::square(x)); + } + +} diff --git a/botan/src/math/numbertheory/reducer.h b/botan/src/math/numbertheory/reducer.h new file mode 100644 index 0000000..d234e07 --- /dev/null +++ b/botan/src/math/numbertheory/reducer.h @@ -0,0 +1,36 @@ +/* +* Modular Reducer +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_MODARITH_H__ +#define BOTAN_MODARITH_H__ + +#include <botan/bigint.h> + +namespace Botan { + +/* +* Modular Reducer +*/ +class BOTAN_DLL Modular_Reducer + { + public: + BigInt multiply(const BigInt&, const BigInt&) const; + BigInt square(const BigInt&) const; + BigInt reduce(const BigInt&) const; + + bool initialized() const { return (mod_words != 0); } + + Modular_Reducer() { mod_words = 0; } + Modular_Reducer(const BigInt&); + private: + BigInt modulus, modulus_2, mu; + u32bit mod_words, mod2_words, mu_words; + }; + +} + +#endif diff --git a/botan/src/math/numbertheory/ressol.cpp b/botan/src/math/numbertheory/ressol.cpp new file mode 100644 index 0000000..d51acb8 --- /dev/null +++ b/botan/src/math/numbertheory/ressol.cpp @@ -0,0 +1,82 @@ +/* +* Shanks-Tonnelli (RESSOL) +* (C) 2007-2008 Falko Strenzke, FlexSecure GmbH +* (C) 2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/numthry.h> +#include <botan/reducer.h> + +namespace Botan { + +/* +* Shanks-Tonnelli algorithm +*/ +BigInt ressol(const BigInt& a, const BigInt& p) + { + if(a < 0) + throw Invalid_Argument("ressol(): a to solve for must be positive"); + if(p <= 1) + throw Invalid_Argument("ressol(): prime must be > 1"); + + if(a == 0) + return 0; + if(p == 2) + return a; + + if(jacobi(a, p) != 1) // not a quadratic residue + return -BigInt(1); + + if(p % 4 == 3) + return power_mod(a, ((p+1) >> 2), p); + + u32bit s = low_zero_bits(p - 1); + BigInt q = p >> s; + + q -= 1; + q >>= 1; + + Modular_Reducer mod_p(p); + + BigInt r = power_mod(a, q, p); + BigInt n = mod_p.multiply(a, mod_p.square(r)); + r = mod_p.multiply(r, a); + + if(n == 1) + return r; + + // find random non quadratic residue z + BigInt z = 2; + while(jacobi(z, p) == 1) // while z quadratic residue + ++z; + + BigInt c = power_mod(z, (q << 1) + 1, p); + + while(n > 1) + { + q = n; + + u32bit i = 0; + while(q != 1) + { + q = mod_p.square(q); + ++i; + } + u32bit t = s; + + if(t <= i) + return -BigInt(1); + + c = power_mod(c, BigInt(BigInt::Power2, t-i-1), p); + r = mod_p.multiply(r, c); + c = mod_p.square(c); + n = mod_p.multiply(n, c); + s = i; + } + + return r; + } + +} diff --git a/botan/src/modes/cbc/cbc.cpp b/botan/src/modes/cbc/cbc.cpp new file mode 100644 index 0000000..f26d4d6 --- /dev/null +++ b/botan/src/modes/cbc/cbc.cpp @@ -0,0 +1,158 @@ +/* +* CBC Mode +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/cbc.h> +#include <botan/xor_buf.h> +#include <algorithm> + +namespace Botan { + +/* +* CBC Encryption Constructor +*/ +CBC_Encryption::CBC_Encryption(BlockCipher* ciph, + BlockCipherModePaddingMethod* pad) : + BlockCipherMode(ciph, "CBC", ciph->BLOCK_SIZE), + padder(pad) + { + if(!padder->valid_blocksize(BLOCK_SIZE)) + throw Invalid_Block_Size(name(), padder->name()); + } + +/* +* CBC Encryption Constructor +*/ +CBC_Encryption::CBC_Encryption(BlockCipher* ciph, + BlockCipherModePaddingMethod* pad, + const SymmetricKey& key, + const InitializationVector& iv) : + BlockCipherMode(ciph, "CBC", ciph->BLOCK_SIZE), + padder(pad) + { + if(!padder->valid_blocksize(BLOCK_SIZE)) + throw Invalid_Block_Size(name(), padder->name()); + set_key(key); + set_iv(iv); + } + +/* +* Encrypt in CBC mode +*/ +void CBC_Encryption::write(const byte input[], u32bit length) + { + while(length) + { + u32bit xored = std::min(BLOCK_SIZE - position, length); + xor_buf(state + position, input, xored); + input += xored; + length -= xored; + position += xored; + if(position == BLOCK_SIZE) + { + cipher->encrypt(state); + send(state, BLOCK_SIZE); + position = 0; + } + } + } + +/* +* Finish encrypting in CBC mode +*/ +void CBC_Encryption::end_msg() + { + SecureVector<byte> padding(BLOCK_SIZE); + padder->pad(padding, padding.size(), position); + write(padding, padder->pad_bytes(BLOCK_SIZE, position)); + if(position != 0) + throw Exception(name() + ": Did not pad to full blocksize"); + } + +/* +* Return a CBC mode name +*/ +std::string CBC_Encryption::name() const + { + return (cipher->name() + "/" + mode_name + "/" + padder->name()); + } + +/* +* CBC Decryption Constructor +*/ +CBC_Decryption::CBC_Decryption(BlockCipher* ciph, + BlockCipherModePaddingMethod* pad) : + BlockCipherMode(ciph, "CBC", ciph->BLOCK_SIZE), + padder(pad) + { + if(!padder->valid_blocksize(BLOCK_SIZE)) + throw Invalid_Block_Size(name(), padder->name()); + temp.create(BLOCK_SIZE); + } + +/* +* CBC Decryption Constructor +*/ +CBC_Decryption::CBC_Decryption(BlockCipher* ciph, + BlockCipherModePaddingMethod* pad, + const SymmetricKey& key, + const InitializationVector& iv) : + BlockCipherMode(ciph, "CBC", ciph->BLOCK_SIZE), + padder(pad) + { + if(!padder->valid_blocksize(BLOCK_SIZE)) + throw Invalid_Block_Size(name(), padder->name()); + temp.create(BLOCK_SIZE); + set_key(key); + set_iv(iv); + } + +/* +* Decrypt in CBC mode +*/ +void CBC_Decryption::write(const byte input[], u32bit length) + { + while(length) + { + if(position == BLOCK_SIZE) + { + cipher->decrypt(buffer, temp); + xor_buf(temp, state, BLOCK_SIZE); + send(temp, BLOCK_SIZE); + state = buffer; + position = 0; + } + u32bit added = std::min(BLOCK_SIZE - position, length); + buffer.copy(position, input, added); + input += added; + length -= added; + position += added; + } + } + +/* +* Finish decrypting in CBC mode +*/ +void CBC_Decryption::end_msg() + { + if(position != BLOCK_SIZE) + throw Decoding_Error(name()); + cipher->decrypt(buffer, temp); + xor_buf(temp, state, BLOCK_SIZE); + send(temp, padder->unpad(temp, BLOCK_SIZE)); + state = buffer; + position = 0; + } + +/* +* Return a CBC mode name +*/ +std::string CBC_Decryption::name() const + { + return (cipher->name() + "/" + mode_name + "/" + padder->name()); + } + +} diff --git a/botan/src/modes/cbc/cbc.h b/botan/src/modes/cbc/cbc.h new file mode 100644 index 0000000..a926ac1 --- /dev/null +++ b/botan/src/modes/cbc/cbc.h @@ -0,0 +1,55 @@ +/* +* CBC Mode +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_CBC_H__ +#define BOTAN_CBC_H__ + +#include <botan/modebase.h> +#include <botan/mode_pad.h> + +namespace Botan { + +/* +* CBC Encryption +*/ +class BOTAN_DLL CBC_Encryption : public BlockCipherMode + { + public: + CBC_Encryption(BlockCipher*, BlockCipherModePaddingMethod*); + CBC_Encryption(BlockCipher*, BlockCipherModePaddingMethod*, + const SymmetricKey&, const InitializationVector&); + + ~CBC_Encryption() { delete padder; } + private: + std::string name() const; + void write(const byte[], u32bit); + void end_msg(); + const BlockCipherModePaddingMethod* padder; + }; + +/* +* CBC Decryption +*/ +class BOTAN_DLL CBC_Decryption : public BlockCipherMode + { + public: + CBC_Decryption(BlockCipher*, BlockCipherModePaddingMethod*); + CBC_Decryption(BlockCipher*, BlockCipherModePaddingMethod*, + const SymmetricKey&, const InitializationVector&); + + ~CBC_Decryption() { delete padder; } + private: + std::string name() const; + void write(const byte[], u32bit); + void end_msg(); + const BlockCipherModePaddingMethod* padder; + SecureVector<byte> temp; + }; + +} + +#endif diff --git a/botan/src/modes/cbc/info.txt b/botan/src/modes/cbc/info.txt new file mode 100644 index 0000000..de81dcb --- /dev/null +++ b/botan/src/modes/cbc/info.txt @@ -0,0 +1,14 @@ +realname "CBC block cipher mode" + +define CBC + +load_on auto + +<add> +cbc.cpp +cbc.h +</add> + +<requires> +mode_pad +</requires> diff --git a/botan/src/modes/cfb/cfb.cpp b/botan/src/modes/cfb/cfb.cpp new file mode 100644 index 0000000..a126bd9 --- /dev/null +++ b/botan/src/modes/cfb/cfb.cpp @@ -0,0 +1,143 @@ +/* +* CFB Mode +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/cfb.h> +#include <botan/parsing.h> +#include <botan/xor_buf.h> +#include <algorithm> + +namespace Botan { + +namespace { + +/* +* Check the feedback size +*/ +void check_feedback(u32bit BLOCK_SIZE, u32bit FEEDBACK_SIZE, u32bit bits, + const std::string& name) + { + if(FEEDBACK_SIZE == 0 || FEEDBACK_SIZE > BLOCK_SIZE || bits % 8 != 0) + throw Invalid_Argument(name + ": Invalid feedback size " + + to_string(bits)); + } + +} + +/* +* CFB Encryption Constructor +*/ +CFB_Encryption::CFB_Encryption(BlockCipher* ciph, + u32bit fback_bits) : + BlockCipherMode(ciph, "CFB", ciph->BLOCK_SIZE, 1), + FEEDBACK_SIZE(fback_bits ? fback_bits / 8: BLOCK_SIZE) + { + check_feedback(BLOCK_SIZE, FEEDBACK_SIZE, fback_bits, name()); + } + +/* +* CFB Encryption Constructor +*/ +CFB_Encryption::CFB_Encryption(BlockCipher* ciph, + const SymmetricKey& key, + const InitializationVector& iv, + u32bit fback_bits) : + BlockCipherMode(ciph, "CFB", ciph->BLOCK_SIZE, 1), + FEEDBACK_SIZE(fback_bits ? fback_bits / 8: BLOCK_SIZE) + { + check_feedback(BLOCK_SIZE, FEEDBACK_SIZE, fback_bits, name()); + set_key(key); + set_iv(iv); + } + +/* +* Encrypt data in CFB mode +*/ +void CFB_Encryption::write(const byte input[], u32bit length) + { + while(length) + { + u32bit xored = std::min(FEEDBACK_SIZE - position, length); + xor_buf(buffer + position, input, xored); + send(buffer + position, xored); + input += xored; + length -= xored; + position += xored; + if(position == FEEDBACK_SIZE) + feedback(); + } + } + +/* +* Do the feedback +*/ +void CFB_Encryption::feedback() + { + for(u32bit j = 0; j != BLOCK_SIZE - FEEDBACK_SIZE; ++j) + state[j] = state[j + FEEDBACK_SIZE]; + state.copy(BLOCK_SIZE - FEEDBACK_SIZE, buffer, FEEDBACK_SIZE); + cipher->encrypt(state, buffer); + position = 0; + } + +/* +* CFB Decryption Constructor +*/ +CFB_Decryption::CFB_Decryption(BlockCipher* ciph, + u32bit fback_bits) : + BlockCipherMode(ciph, "CFB", ciph->BLOCK_SIZE, 1), + FEEDBACK_SIZE(fback_bits ? fback_bits / 8 : BLOCK_SIZE) + { + check_feedback(BLOCK_SIZE, FEEDBACK_SIZE, fback_bits, name()); + } + +/* +* CFB Decryption Constructor +*/ +CFB_Decryption::CFB_Decryption(BlockCipher* ciph, + const SymmetricKey& key, + const InitializationVector& iv, + u32bit fback_bits) : + BlockCipherMode(ciph, "CFB", ciph->BLOCK_SIZE, 1), + FEEDBACK_SIZE(fback_bits ? fback_bits / 8 : BLOCK_SIZE) + { + check_feedback(BLOCK_SIZE, FEEDBACK_SIZE, fback_bits, name()); + set_key(key); + set_iv(iv); + } + +/* +* Decrypt data in CFB mode +*/ +void CFB_Decryption::write(const byte input[], u32bit length) + { + while(length) + { + u32bit xored = std::min(FEEDBACK_SIZE - position, length); + xor_buf(buffer + position, input, xored); + send(buffer + position, xored); + buffer.copy(position, input, xored); + input += xored; + length -= xored; + position += xored; + if(position == FEEDBACK_SIZE) + feedback(); + } + } + +/* +* Do the feedback +*/ +void CFB_Decryption::feedback() + { + for(u32bit j = 0; j != BLOCK_SIZE - FEEDBACK_SIZE; ++j) + state[j] = state[j + FEEDBACK_SIZE]; + state.copy(BLOCK_SIZE - FEEDBACK_SIZE, buffer, FEEDBACK_SIZE); + cipher->encrypt(state, buffer); + position = 0; + } + +} diff --git a/botan/src/modes/cfb/cfb.h b/botan/src/modes/cfb/cfb.h new file mode 100644 index 0000000..7810c00 --- /dev/null +++ b/botan/src/modes/cfb/cfb.h @@ -0,0 +1,47 @@ +/* +* CFB Mode +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_CFB_H__ +#define BOTAN_CFB_H__ + +#include <botan/modebase.h> + +namespace Botan { + +/* +* CFB Encryption +*/ +class BOTAN_DLL CFB_Encryption : public BlockCipherMode + { + public: + CFB_Encryption(BlockCipher*, u32bit = 0); + CFB_Encryption(BlockCipher*, const SymmetricKey&, + const InitializationVector&, u32bit = 0); + private: + void write(const byte[], u32bit); + void feedback(); + const u32bit FEEDBACK_SIZE; + }; + +/* +* CFB Decryption +*/ +class BOTAN_DLL CFB_Decryption : public BlockCipherMode + { + public: + CFB_Decryption(BlockCipher*, u32bit = 0); + CFB_Decryption(BlockCipher*, const SymmetricKey&, + const InitializationVector&, u32bit = 0); + private: + void write(const byte[], u32bit); + void feedback(); + const u32bit FEEDBACK_SIZE; + }; + +} + +#endif diff --git a/botan/src/modes/cfb/info.txt b/botan/src/modes/cfb/info.txt new file mode 100644 index 0000000..d66df1e --- /dev/null +++ b/botan/src/modes/cfb/info.txt @@ -0,0 +1,15 @@ +realname "CFB block cipher mode" + +define CFB + +load_on auto + +<add> +cfb.cpp +cfb.h +</add> + +<requires> +modes +</requires> + diff --git a/botan/src/modes/ctr/ctr.cpp b/botan/src/modes/ctr/ctr.cpp new file mode 100644 index 0000000..9eb42ec --- /dev/null +++ b/botan/src/modes/ctr/ctr.cpp @@ -0,0 +1,75 @@ +/* +* CTR Mode +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/ctr.h> +#include <botan/xor_buf.h> +#include <algorithm> + +namespace Botan { + +/* +* CTR-BE Constructor +*/ +CTR_BE::CTR_BE(BlockCipher* ciph) : + BlockCipherMode(ciph, "CTR-BE", ciph->BLOCK_SIZE, 1) + { + } + +/* +* CTR-BE Constructor +*/ +CTR_BE::CTR_BE(BlockCipher* ciph, const SymmetricKey& key, + const InitializationVector& iv) : + BlockCipherMode(ciph, "CTR-BE", ciph->BLOCK_SIZE, 1) + { + set_key(key); + set_iv(iv); + } + +/* +* CTR-BE Encryption/Decryption +*/ +void CTR_BE::write(const byte input[], u32bit length) + { + u32bit copied = std::min(BLOCK_SIZE - position, length); + xor_buf(buffer + position, input, copied); + send(buffer + position, copied); + input += copied; + length -= copied; + position += copied; + + if(position == BLOCK_SIZE) + increment_counter(); + + while(length >= BLOCK_SIZE) + { + xor_buf(buffer, input, BLOCK_SIZE); + send(buffer, BLOCK_SIZE); + + input += BLOCK_SIZE; + length -= BLOCK_SIZE; + increment_counter(); + } + + xor_buf(buffer + position, input, length); + send(buffer + position, length); + position += length; + } + +/* +* Increment the counter and update the buffer +*/ +void CTR_BE::increment_counter() + { + for(s32bit j = BLOCK_SIZE - 1; j >= 0; --j) + if(++state[j]) + break; + cipher->encrypt(state, buffer); + position = 0; + } + +} diff --git a/botan/src/modes/ctr/ctr.h b/botan/src/modes/ctr/ctr.h new file mode 100644 index 0000000..aa0db57 --- /dev/null +++ b/botan/src/modes/ctr/ctr.h @@ -0,0 +1,31 @@ +/* +* CTR Mode +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_COUNTER_MODE_H__ +#define BOTAN_COUNTER_MODE_H__ + +#include <botan/modebase.h> +#include <botan/modebase.h> + +namespace Botan { + +/* +* CTR-BE Mode +*/ +class BOTAN_DLL CTR_BE : public BlockCipherMode + { + public: + CTR_BE(BlockCipher*); + CTR_BE(BlockCipher*, const SymmetricKey&, const InitializationVector&); + private: + void write(const byte[], u32bit); + void increment_counter(); + }; + +} + +#endif diff --git a/botan/src/modes/ctr/info.txt b/botan/src/modes/ctr/info.txt new file mode 100644 index 0000000..cb291a2 --- /dev/null +++ b/botan/src/modes/ctr/info.txt @@ -0,0 +1,15 @@ +realname "CTR block cipher mode" + +define CTR + +load_on auto + +<add> +ctr.cpp +ctr.h +</add> + +<requires> +modes +</requires> + diff --git a/botan/src/modes/cts/cts.cpp b/botan/src/modes/cts/cts.cpp new file mode 100644 index 0000000..99f042f --- /dev/null +++ b/botan/src/modes/cts/cts.cpp @@ -0,0 +1,134 @@ +/* +* CTS Mode +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/cts.h> +#include <botan/xor_buf.h> +#include <algorithm> + +namespace Botan { + +/* +* Encrypt a block +*/ +void CTS_Encryption::encrypt(const byte block[]) + { + xor_buf(state, block, BLOCK_SIZE); + cipher->encrypt(state); + send(state, BLOCK_SIZE); + } + +/* +* Encrypt in CTS mode +*/ +void CTS_Encryption::write(const byte input[], u32bit length) + { + u32bit copied = std::min(BUFFER_SIZE - position, length); + buffer.copy(position, input, copied); + length -= copied; + input += copied; + position += copied; + + if(length == 0) return; + + encrypt(buffer); + if(length > BLOCK_SIZE) + { + encrypt(buffer + BLOCK_SIZE); + while(length > 2*BLOCK_SIZE) + { + encrypt(input); + length -= BLOCK_SIZE; + input += BLOCK_SIZE; + } + position = 0; + } + else + { + copy_mem(buffer.begin(), buffer + BLOCK_SIZE, BLOCK_SIZE); + position = BLOCK_SIZE; + } + buffer.copy(position, input, length); + position += length; + } + +/* +* Finish encrypting in CTS mode +*/ +void CTS_Encryption::end_msg() + { + if(position < BLOCK_SIZE + 1) + throw Exception("CTS_Encryption: insufficient data to encrypt"); + xor_buf(state, buffer, BLOCK_SIZE); + cipher->encrypt(state); + SecureVector<byte> cn = state; + clear_mem(buffer + position, BUFFER_SIZE - position); + encrypt(buffer + BLOCK_SIZE); + send(cn, position - BLOCK_SIZE); + } + +/* +* Decrypt a block +*/ +void CTS_Decryption::decrypt(const byte block[]) + { + cipher->decrypt(block, temp); + xor_buf(temp, state, BLOCK_SIZE); + send(temp, BLOCK_SIZE); + state.copy(block, BLOCK_SIZE); + } + +/* +* Decrypt in CTS mode +*/ +void CTS_Decryption::write(const byte input[], u32bit length) + { + u32bit copied = std::min(BUFFER_SIZE - position, length); + buffer.copy(position, input, copied); + length -= copied; + input += copied; + position += copied; + + if(length == 0) return; + + decrypt(buffer); + if(length > BLOCK_SIZE) + { + decrypt(buffer + BLOCK_SIZE); + while(length > 2*BLOCK_SIZE) + { + decrypt(input); + length -= BLOCK_SIZE; + input += BLOCK_SIZE; + } + position = 0; + } + else + { + copy_mem(buffer.begin(), buffer + BLOCK_SIZE, BLOCK_SIZE); + position = BLOCK_SIZE; + } + buffer.copy(position, input, length); + position += length; + } + +/* +* Finish decrypting in CTS mode +*/ +void CTS_Decryption::end_msg() + { + cipher->decrypt(buffer, temp); + xor_buf(temp, buffer + BLOCK_SIZE, position - BLOCK_SIZE); + SecureVector<byte> xn = temp; + copy_mem(buffer + position, xn + (position - BLOCK_SIZE), + BUFFER_SIZE - position); + cipher->decrypt(buffer + BLOCK_SIZE, temp); + xor_buf(temp, state, BLOCK_SIZE); + send(temp, BLOCK_SIZE); + send(xn, position - BLOCK_SIZE); + } + +} diff --git a/botan/src/modes/cts/cts.h b/botan/src/modes/cts/cts.h new file mode 100644 index 0000000..9b17203 --- /dev/null +++ b/botan/src/modes/cts/cts.h @@ -0,0 +1,60 @@ +/* +* CTS Mode +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_CTS_H__ +#define BOTAN_CTS_H__ + +#include <botan/modebase.h> +#include <botan/block_cipher.h> + +namespace Botan { + +/* +* CTS Encryption +*/ +class BOTAN_DLL CTS_Encryption : public BlockCipherMode + { + public: + CTS_Encryption(BlockCipher* ciph) : + BlockCipherMode(ciph, "CTS", ciph->BLOCK_SIZE, 0, 2) {} + + CTS_Encryption(BlockCipher* ciph, + const SymmetricKey& key, + const InitializationVector& iv) : + BlockCipherMode(ciph, "CTS", ciph->BLOCK_SIZE, 0, 2) + { set_key(key); set_iv(iv); } + private: + void write(const byte[], u32bit); + void end_msg(); + void encrypt(const byte[]); + }; + +/* +* CTS Decryption +*/ +class BOTAN_DLL CTS_Decryption : public BlockCipherMode + { + public: + CTS_Decryption(BlockCipher* ciph) : + BlockCipherMode(ciph, "CTS", ciph->BLOCK_SIZE, 0, 2) + { temp.create(BLOCK_SIZE); } + + CTS_Decryption(BlockCipher* ciph, + const SymmetricKey& key, + const InitializationVector& iv) : + BlockCipherMode(ciph, "CTS", ciph->BLOCK_SIZE, 0, 2) + { set_key(key); set_iv(iv); temp.create(BLOCK_SIZE); } + private: + void write(const byte[], u32bit); + void end_msg(); + void decrypt(const byte[]); + SecureVector<byte> temp; + }; + +} + +#endif diff --git a/botan/src/modes/cts/info.txt b/botan/src/modes/cts/info.txt new file mode 100644 index 0000000..9eb16ad --- /dev/null +++ b/botan/src/modes/cts/info.txt @@ -0,0 +1,14 @@ +realname "CTS block cipher mode" + +define CTS + +load_on auto + +<add> +cts.cpp +cts.h +</add> + +<requires> +block +</requires> diff --git a/botan/src/modes/eax/eax.cpp b/botan/src/modes/eax/eax.cpp new file mode 100644 index 0000000..67465a7 --- /dev/null +++ b/botan/src/modes/eax/eax.cpp @@ -0,0 +1,172 @@ +/* +* EAX Mode Encryption +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/eax.h> +#include <botan/cmac.h> +#include <botan/xor_buf.h> +#include <botan/parsing.h> +#include <algorithm> + +namespace Botan { + +namespace { + +/* +* EAX MAC-based PRF +*/ +SecureVector<byte> eax_prf(byte tag, u32bit BLOCK_SIZE, + MessageAuthenticationCode* mac, + const byte in[], u32bit length) + { + for(u32bit j = 0; j != BLOCK_SIZE - 1; ++j) + mac->update(0); + mac->update(tag); + mac->update(in, length); + return mac->final(); + } + +} + +/* +* EAX_Base Constructor +*/ +EAX_Base::EAX_Base(BlockCipher* ciph, + u32bit tag_size) : + TAG_SIZE(tag_size ? tag_size / 8 : ciph->BLOCK_SIZE), + BLOCK_SIZE(ciph->BLOCK_SIZE) + { + cipher = ciph; + mac = new CMAC(cipher->clone()); + + if(tag_size % 8 != 0 || TAG_SIZE == 0 || TAG_SIZE > mac->OUTPUT_LENGTH) + throw Invalid_Argument(name() + ": Bad tag size " + to_string(tag_size)); + + state.create(BLOCK_SIZE); + buffer.create(BLOCK_SIZE); + position = 0; + } + +/* +* Check if a keylength is valid for EAX +*/ +bool EAX_Base::valid_keylength(u32bit n) const + { + if(!cipher->valid_keylength(n)) + return false; + if(!mac->valid_keylength(n)) + return false; + return true; + } + +/* +* Set the EAX key +*/ +void EAX_Base::set_key(const SymmetricKey& key) + { + cipher->set_key(key); + mac->set_key(key); + header_mac = eax_prf(1, BLOCK_SIZE, mac, 0, 0); + } + +/* +* Do setup at the start of each message +*/ +void EAX_Base::start_msg() + { + for(u32bit j = 0; j != BLOCK_SIZE - 1; ++j) + mac->update(0); + mac->update(2); + } + +/* +* Set the EAX nonce +*/ +void EAX_Base::set_iv(const InitializationVector& iv) + { + nonce_mac = eax_prf(0, BLOCK_SIZE, mac, iv.begin(), iv.length()); + state = nonce_mac; + cipher->encrypt(state, buffer); + } + +/* +* Set the EAX header +*/ +void EAX_Base::set_header(const byte header[], u32bit length) + { + header_mac = eax_prf(1, BLOCK_SIZE, mac, header, length); + } + +/* +* Return the name of this cipher mode +*/ +std::string EAX_Base::name() const + { + return (cipher->name() + "/EAX"); + } + +/* +* Increment the counter and update the buffer +*/ +void EAX_Base::increment_counter() + { + for(s32bit j = BLOCK_SIZE - 1; j >= 0; --j) + if(++state[j]) + break; + cipher->encrypt(state, buffer); + position = 0; + } + +/* +* Encrypt in EAX mode +*/ +void EAX_Encryption::write(const byte input[], u32bit length) + { + u32bit copied = std::min(BLOCK_SIZE - position, length); + xor_buf(buffer + position, input, copied); + send(buffer + position, copied); + mac->update(buffer + position, copied); + input += copied; + length -= copied; + position += copied; + + if(position == BLOCK_SIZE) + increment_counter(); + + while(length >= BLOCK_SIZE) + { + xor_buf(buffer, input, BLOCK_SIZE); + send(buffer, BLOCK_SIZE); + mac->update(buffer, BLOCK_SIZE); + + input += BLOCK_SIZE; + length -= BLOCK_SIZE; + increment_counter(); + } + + xor_buf(buffer + position, input, length); + send(buffer + position, length); + mac->update(buffer + position, length); + position += length; + } + +/* +* Finish encrypting in EAX mode +*/ +void EAX_Encryption::end_msg() + { + SecureVector<byte> data_mac = mac->final(); + xor_buf(data_mac, nonce_mac, data_mac.size()); + xor_buf(data_mac, header_mac, data_mac.size()); + + send(data_mac, TAG_SIZE); + + state.clear(); + buffer.clear(); + position = 0; + } + +} diff --git a/botan/src/modes/eax/eax.h b/botan/src/modes/eax/eax.h new file mode 100644 index 0000000..1bb2e51 --- /dev/null +++ b/botan/src/modes/eax/eax.h @@ -0,0 +1,85 @@ +/* +* EAX Mode +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_EAX_H__ +#define BOTAN_EAX_H__ + +#include <botan/basefilt.h> +#include <botan/block_cipher.h> +#include <botan/mac.h> + +namespace Botan { + +/* +* EAX Base Class +*/ +class BOTAN_DLL EAX_Base : public Keyed_Filter + { + public: + void set_key(const SymmetricKey&); + void set_iv(const InitializationVector&); + void set_header(const byte[], u32bit); + std::string name() const; + + bool valid_keylength(u32bit) const; + + ~EAX_Base() { delete cipher; delete mac; } + protected: + EAX_Base(BlockCipher*, u32bit); + void start_msg(); + void increment_counter(); + + const u32bit TAG_SIZE, BLOCK_SIZE; + BlockCipher* cipher; + MessageAuthenticationCode* mac; + SecureVector<byte> nonce_mac, header_mac, state, buffer; + u32bit position; + }; + +/* +* EAX Encryption +*/ +class BOTAN_DLL EAX_Encryption : public EAX_Base + { + public: + EAX_Encryption(BlockCipher* ciph, u32bit tag_size = 0) : + EAX_Base(ciph, tag_size) {} + + EAX_Encryption(BlockCipher* ciph, const SymmetricKey& key, + const InitializationVector& iv, + u32bit tag_size) : EAX_Base(ciph, tag_size) + { + set_key(key); + set_iv(iv); + } + private: + void write(const byte[], u32bit); + void end_msg(); + }; + +/* +* EAX Decryption +*/ +class BOTAN_DLL EAX_Decryption : public EAX_Base + { + public: + EAX_Decryption(BlockCipher* ciph, u32bit tag_size = 0); + + EAX_Decryption(BlockCipher* ciph, const SymmetricKey& key, + const InitializationVector& iv, + u32bit tag_size = 0); + private: + void write(const byte[], u32bit); + void do_write(const byte[], u32bit); + void end_msg(); + SecureVector<byte> queue; + u32bit queue_start, queue_end; + }; + +} + +#endif diff --git a/botan/src/modes/eax/eax_dec.cpp b/botan/src/modes/eax/eax_dec.cpp new file mode 100644 index 0000000..b7e5795 --- /dev/null +++ b/botan/src/modes/eax/eax_dec.cpp @@ -0,0 +1,127 @@ +/* +* EAX Mode Encryption +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/eax.h> +#include <botan/xor_buf.h> +#include <botan/parsing.h> +#include <algorithm> + +namespace Botan { + +/* +* EAX_Decryption Constructor +*/ +EAX_Decryption::EAX_Decryption(BlockCipher* ciph, + u32bit tag_size) : + EAX_Base(ciph, tag_size) + { + queue.create(2*TAG_SIZE + DEFAULT_BUFFERSIZE); + queue_start = queue_end = 0; + } + +/* +* EAX_Decryption Constructor +*/ +EAX_Decryption::EAX_Decryption(BlockCipher* ciph, + const SymmetricKey& key, + const InitializationVector& iv, + u32bit tag_size) : + EAX_Base(ciph, tag_size) + { + set_key(key); + set_iv(iv); + queue.create(2*TAG_SIZE + DEFAULT_BUFFERSIZE); + queue_start = queue_end = 0; + } + +/* +* Decrypt in EAX mode +*/ +void EAX_Decryption::write(const byte input[], u32bit length) + { + while(length) + { + const u32bit copied = std::min(length, queue.size() - queue_end); + + queue.copy(queue_end, input, copied); + input += copied; + length -= copied; + queue_end += copied; + + SecureVector<byte> block_buf(cipher->BLOCK_SIZE); + while((queue_end - queue_start) > TAG_SIZE) + { + u32bit removed = (queue_end - queue_start) - TAG_SIZE; + do_write(queue + queue_start, removed); + queue_start += removed; + } + + if(queue_start + TAG_SIZE == queue_end && + queue_start >= queue.size() / 2) + { + SecureVector<byte> queue_data(TAG_SIZE); + queue_data.copy(queue + queue_start, TAG_SIZE); + queue.copy(queue_data, TAG_SIZE); + queue_start = 0; + queue_end = TAG_SIZE; + } + } + } + +/* +* Decrypt in EAX mode +*/ +void EAX_Decryption::do_write(const byte input[], u32bit length) + { + mac->update(input, length); + + u32bit copied = std::min(BLOCK_SIZE - position, length); + xor_buf(buffer + position, input, copied); + send(buffer + position, copied); + input += copied; + length -= copied; + position += copied; + + if(position == BLOCK_SIZE) + increment_counter(); + + while(length >= BLOCK_SIZE) + { + xor_buf(buffer, input, BLOCK_SIZE); + send(buffer, BLOCK_SIZE); + + input += BLOCK_SIZE; + length -= BLOCK_SIZE; + increment_counter(); + } + + xor_buf(buffer + position, input, length); + send(buffer + position, length); + position += length; + } + +/* +* Finish decrypting in EAX mode +*/ +void EAX_Decryption::end_msg() + { + if((queue_end - queue_start) != TAG_SIZE) + throw Integrity_Failure(name() + ": Message authentication failure"); + + SecureVector<byte> data_mac = mac->final(); + + for(u32bit j = 0; j != TAG_SIZE; ++j) + if(queue[queue_start+j] != (data_mac[j] ^ nonce_mac[j] ^ header_mac[j])) + throw Integrity_Failure(name() + ": Message authentication failure"); + + state.clear(); + buffer.clear(); + position = 0; + queue_start = queue_end = 0; + } + +} diff --git a/botan/src/modes/eax/info.txt b/botan/src/modes/eax/info.txt new file mode 100644 index 0000000..d1fc7e0 --- /dev/null +++ b/botan/src/modes/eax/info.txt @@ -0,0 +1,18 @@ +realname "EAX block cipher mode" + +define EAX + +load_on auto + +<add> +eax.cpp +eax.h +eax_dec.cpp +</add> + +<requires> +block +cmac +filters +mac +</requires> diff --git a/botan/src/modes/ecb/ecb.cpp b/botan/src/modes/ecb/ecb.cpp new file mode 100644 index 0000000..8da0a48 --- /dev/null +++ b/botan/src/modes/ecb/ecb.cpp @@ -0,0 +1,105 @@ +/* +* ECB Mode +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/ecb.h> + +namespace Botan { + +/* +* Verify the IV is not set +*/ +bool ECB::valid_iv_size(u32bit iv_size) const + { + if(iv_size == 0) + return true; + return false; + } + +/* +* Return an ECB mode name +*/ +std::string ECB::name() const + { + return (cipher->name() + "/" + mode_name + "/" + padder->name()); + } + +/* +* Encrypt in ECB mode +*/ +void ECB_Encryption::write(const byte input[], u32bit length) + { + buffer.copy(position, input, length); + if(position + length >= BLOCK_SIZE) + { + cipher->encrypt(buffer); + send(buffer, BLOCK_SIZE); + input += (BLOCK_SIZE - position); + length -= (BLOCK_SIZE - position); + while(length >= BLOCK_SIZE) + { + cipher->encrypt(input, buffer); + send(buffer, BLOCK_SIZE); + input += BLOCK_SIZE; + length -= BLOCK_SIZE; + } + buffer.copy(input, length); + position = 0; + } + position += length; + } + +/* +* Finish encrypting in ECB mode +*/ +void ECB_Encryption::end_msg() + { + SecureVector<byte> padding(BLOCK_SIZE); + padder->pad(padding, padding.size(), position); + write(padding, padder->pad_bytes(BLOCK_SIZE, position)); + if(position != 0) + throw Encoding_Error(name() + ": Did not pad to full blocksize"); + } + +/* +* Decrypt in ECB mode +*/ +void ECB_Decryption::write(const byte input[], u32bit length) + { + buffer.copy(position, input, length); + if(position + length > BLOCK_SIZE) + { + cipher->decrypt(buffer); + send(buffer, BLOCK_SIZE); + input += (BLOCK_SIZE - position); + length -= (BLOCK_SIZE - position); + while(length > BLOCK_SIZE) + { + cipher->decrypt(input, buffer); + send(buffer, BLOCK_SIZE); + input += BLOCK_SIZE; + length -= BLOCK_SIZE; + } + buffer.copy(input, length); + position = 0; + } + position += length; + } + +/* +* Finish decrypting in ECB mode +*/ +void ECB_Decryption::end_msg() + { + if(position != BLOCK_SIZE) + throw Decoding_Error(name()); + cipher->decrypt(buffer); + send(buffer, padder->unpad(buffer, BLOCK_SIZE)); + state = buffer; + position = 0; + } + +} diff --git a/botan/src/modes/ecb/ecb.h b/botan/src/modes/ecb/ecb.h new file mode 100644 index 0000000..5230f9b --- /dev/null +++ b/botan/src/modes/ecb/ecb.h @@ -0,0 +1,73 @@ +/* +* ECB Mode +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ECB_H__ +#define BOTAN_ECB_H__ + +#include <botan/modebase.h> +#include <botan/mode_pad.h> +#include <botan/block_cipher.h> + +namespace Botan { + +/* +* ECB +*/ +class BOTAN_DLL ECB : public BlockCipherMode + { + protected: + ECB(BlockCipher* ciph, BlockCipherModePaddingMethod* pad) : + BlockCipherMode(ciph, "ECB", 0), padder(pad) {} + ~ECB() { delete padder; } + + std::string name() const; + BlockCipherModePaddingMethod* padder; + private: + bool valid_iv_size(u32bit) const; + }; + +/* +* ECB Encryption +*/ +class BOTAN_DLL ECB_Encryption : public ECB + { + public: + ECB_Encryption(BlockCipher* ciph, + BlockCipherModePaddingMethod* pad) : + ECB(ciph, pad) {} + + ECB_Encryption(BlockCipher* ciph, + BlockCipherModePaddingMethod* pad, + const SymmetricKey& key) : + ECB(ciph, pad) { set_key(key); } + private: + void write(const byte[], u32bit); + void end_msg(); + }; + +/* +* ECB Decryption +*/ +class BOTAN_DLL ECB_Decryption : public ECB + { + public: + ECB_Decryption(BlockCipher* ciph, + BlockCipherModePaddingMethod* pad) : + ECB(ciph, pad) {} + + ECB_Decryption(BlockCipher* ciph, + BlockCipherModePaddingMethod* pad, + const SymmetricKey& key) : + ECB(ciph, pad) { set_key(key); } + private: + void write(const byte[], u32bit); + void end_msg(); + }; + +} + +#endif diff --git a/botan/src/modes/ecb/info.txt b/botan/src/modes/ecb/info.txt new file mode 100644 index 0000000..06b7b4f --- /dev/null +++ b/botan/src/modes/ecb/info.txt @@ -0,0 +1,15 @@ +realname "ECB block cipher mode" + +define ECB + +load_on auto + +<add> +ecb.cpp +ecb.h +</add> + +<requires> +block +mode_pad +</requires> diff --git a/botan/src/modes/info.txt b/botan/src/modes/info.txt new file mode 100644 index 0000000..e089e74 --- /dev/null +++ b/botan/src/modes/info.txt @@ -0,0 +1,15 @@ +realname "Cipher Mode Base Class" + +define CIPHER_MODEBASE + +load_on auto + +<add> +modebase.cpp +modebase.h +</add> + +<requires> +block +filters +</requires> diff --git a/botan/src/modes/mode_pad/info.txt b/botan/src/modes/mode_pad/info.txt new file mode 100644 index 0000000..f22cf74 --- /dev/null +++ b/botan/src/modes/mode_pad/info.txt @@ -0,0 +1,10 @@ +realname "Cipher Mode Padding Method" + +define CIPHER_MODE_PADDING + +load_on auto + +<add> +mode_pad.cpp +mode_pad.h +</add> diff --git a/botan/src/modes/mode_pad/mode_pad.cpp b/botan/src/modes/mode_pad/mode_pad.cpp new file mode 100644 index 0000000..b8badd7 --- /dev/null +++ b/botan/src/modes/mode_pad/mode_pad.cpp @@ -0,0 +1,128 @@ +/* +* CBC Padding Methods +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/mode_pad.h> +#include <botan/exceptn.h> +#include <botan/util.h> + +namespace Botan { + +/* +* Default amount of padding +*/ +u32bit BlockCipherModePaddingMethod::pad_bytes(u32bit bs, u32bit pos) const + { + return (bs - pos); + } + +/* +* Pad with PKCS #7 Method +*/ +void PKCS7_Padding::pad(byte block[], u32bit size, u32bit position) const + { + for(u32bit j = 0; j != size; ++j) + block[j] = (size-position); + } + +/* +* Unpad with PKCS #7 Method +*/ +u32bit PKCS7_Padding::unpad(const byte block[], u32bit size) const + { + u32bit position = block[size-1]; + if(position > size) + throw Decoding_Error(name()); + for(u32bit j = size-position; j != size-1; ++j) + if(block[j] != position) + throw Decoding_Error(name()); + return (size-position); + } + +/* +* Query if the size is valid for this method +*/ +bool PKCS7_Padding::valid_blocksize(u32bit size) const + { + if(size > 0 && size < 256) + return true; + else + return false; + } + +/* +* Pad with ANSI X9.23 Method +*/ +void ANSI_X923_Padding::pad(byte block[], u32bit size, u32bit position) const + { + for(u32bit j = 0; j != size-position; ++j) + block[j] = 0; + block[size-position-1] = (size-position); + } + +/* +* Unpad with ANSI X9.23 Method +*/ +u32bit ANSI_X923_Padding::unpad(const byte block[], u32bit size) const + { + u32bit position = block[size-1]; + if(position > size) + throw Decoding_Error(name()); + for(u32bit j = size-position; j != size-1; ++j) + if(block[j] != 0) + throw Decoding_Error(name()); + return (size-position); + } + +/* +* Query if the size is valid for this method +*/ +bool ANSI_X923_Padding::valid_blocksize(u32bit size) const + { + if(size > 0 && size < 256) + return true; + else + return false; + } + +/* +* Pad with One and Zeros Method +*/ +void OneAndZeros_Padding::pad(byte block[], u32bit size, u32bit) const + { + block[0] = 0x80; + for(u32bit j = 1; j != size; ++j) + block[j] = 0x00; + } + +/* +* Unpad with One and Zeros Method +*/ +u32bit OneAndZeros_Padding::unpad(const byte block[], u32bit size) const + { + while(size) + { + if(block[size-1] == 0x80) + break; + if(block[size-1] != 0x00) + throw Decoding_Error(name()); + size--; + } + if(!size) + throw Decoding_Error(name()); + return (size-1); + } + +/* +* Query if the size is valid for this method +*/ +bool OneAndZeros_Padding::valid_blocksize(u32bit size) const + { + if(size) return true; + else return false; + } + +} diff --git a/botan/src/modes/mode_pad/mode_pad.h b/botan/src/modes/mode_pad/mode_pad.h new file mode 100644 index 0000000..a486d3c --- /dev/null +++ b/botan/src/modes/mode_pad/mode_pad.h @@ -0,0 +1,120 @@ +/** +* CBC Padding Methods +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_CBC_PADDING_H__ +#define BOTAN_CBC_PADDING_H__ + +#include <botan/types.h> +#include <string> + +namespace Botan { + +/** +* Block Cipher Mode Padding Method +* This class is pretty limited, it cannot deal well with +* randomized padding methods, or any padding method that +* wants to add more than one block. For instance, it should +* be possible to define cipher text stealing mode as simply +* a padding mode for CBC, which happens to consume the last +* two block (and requires use of the block cipher). +*/ +class BOTAN_DLL BlockCipherModePaddingMethod + { + public: + /** + * @param block output buffer + * @param size of the block + * @param current_position in the last block + */ + virtual void pad(byte block[], + u32bit size, + u32bit current_position) const = 0; + + /** + * @param block the last block + * @param size the of the block + */ + virtual u32bit unpad(const byte block[], + u32bit size) const = 0; + + /** + * @param block_size of the cipher + * @param position in the current block + * @return number of padding bytes that will be appended + */ + virtual u32bit pad_bytes(u32bit block_size, + u32bit position) const; + + /** + * @param block_size of the cipher + * @return valid block size for this padding mode + */ + virtual bool valid_blocksize(u32bit block_size) const = 0; + + /** + * @return name of the mode + */ + virtual std::string name() const = 0; + + /** + * virtual destructor + */ + virtual ~BlockCipherModePaddingMethod() {} + }; + +/** +* PKCS#7 Padding +*/ +class BOTAN_DLL PKCS7_Padding : public BlockCipherModePaddingMethod + { + public: + void pad(byte[], u32bit, u32bit) const; + u32bit unpad(const byte[], u32bit) const; + bool valid_blocksize(u32bit) const; + std::string name() const { return "PKCS7"; } + }; + +/** +* ANSI X9.23 Padding +*/ +class BOTAN_DLL ANSI_X923_Padding : public BlockCipherModePaddingMethod + { + public: + void pad(byte[], u32bit, u32bit) const; + u32bit unpad(const byte[], u32bit) const; + bool valid_blocksize(u32bit) const; + std::string name() const { return "X9.23"; } + }; + +/** +* One And Zeros Padding +*/ +class BOTAN_DLL OneAndZeros_Padding : public BlockCipherModePaddingMethod + { + public: + void pad(byte[], u32bit, u32bit) const; + u32bit unpad(const byte[], u32bit) const; + bool valid_blocksize(u32bit) const; + std::string name() const { return "OneAndZeros"; } + }; + +/** +* Null Padding +*/ +class BOTAN_DLL Null_Padding : public BlockCipherModePaddingMethod + { + public: + void pad(byte[], u32bit, u32bit) const { return; } + u32bit unpad(const byte[], u32bit size) const { return size; } + u32bit pad_bytes(u32bit, u32bit) const { return 0; } + bool valid_blocksize(u32bit) const { return true; } + std::string name() const { return "NoPadding"; } + }; + +} + +#endif diff --git a/botan/src/modes/modebase.cpp b/botan/src/modes/modebase.cpp new file mode 100644 index 0000000..8293acc --- /dev/null +++ b/botan/src/modes/modebase.cpp @@ -0,0 +1,54 @@ +/* +* Block Cipher Mode +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/modebase.h> + +namespace Botan { + +/* +* Block Cipher Mode Constructor +*/ +BlockCipherMode::BlockCipherMode(BlockCipher* cipher_ptr, + const std::string& cipher_mode_name, + u32bit iv_size, u32bit iv_meth, + u32bit buf_mult) : + BLOCK_SIZE(cipher_ptr->BLOCK_SIZE), BUFFER_SIZE(buf_mult * BLOCK_SIZE), + IV_METHOD(iv_meth), mode_name(cipher_mode_name) + { + base_ptr = cipher = cipher_ptr; + buffer.create(BUFFER_SIZE); + state.create(iv_size); + position = 0; + } + +/* +* Return the name of this type +*/ +std::string BlockCipherMode::name() const + { + return (cipher->name() + "/" + mode_name); + } + +/* +* Set the IV +*/ +void BlockCipherMode::set_iv(const InitializationVector& new_iv) + { + if(new_iv.length() != state.size()) + throw Invalid_IV_Length(name(), new_iv.length()); + + state = new_iv.bits_of(); + buffer.clear(); + position = 0; + + if(IV_METHOD == 1) + cipher->encrypt(state, buffer); + else if(IV_METHOD == 2) + cipher->encrypt(state); + } + +} diff --git a/botan/src/modes/modebase.h b/botan/src/modes/modebase.h new file mode 100644 index 0000000..173fde5 --- /dev/null +++ b/botan/src/modes/modebase.h @@ -0,0 +1,39 @@ +/* +* Block Cipher Mode +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_MODEBASE_H__ +#define BOTAN_MODEBASE_H__ + +#include <botan/basefilt.h> +#include <botan/block_cipher.h> + +namespace Botan { + +/** +* This class represents an abstract block cipher mode +*/ +class BOTAN_DLL BlockCipherMode : public Keyed_Filter + { + public: + std::string name() const; + + BlockCipherMode(BlockCipher*, const std::string&, + u32bit, u32bit = 0, u32bit = 1); + + virtual ~BlockCipherMode() { delete cipher; } + protected: + void set_iv(const InitializationVector&); + const u32bit BLOCK_SIZE, BUFFER_SIZE, IV_METHOD; + const std::string mode_name; + BlockCipher* cipher; + SecureVector<byte> buffer, state; + u32bit position; + }; + +} + +#endif diff --git a/botan/src/modes/ofb/info.txt b/botan/src/modes/ofb/info.txt new file mode 100644 index 0000000..3cba415 --- /dev/null +++ b/botan/src/modes/ofb/info.txt @@ -0,0 +1,14 @@ +realname "OFB block cipher mode" + +define OFB + +load_on auto + +<add> +ofb.cpp +ofb.h +</add> + +<requires> +block +</requires> diff --git a/botan/src/modes/ofb/ofb.cpp b/botan/src/modes/ofb/ofb.cpp new file mode 100644 index 0000000..cb40fde --- /dev/null +++ b/botan/src/modes/ofb/ofb.cpp @@ -0,0 +1,66 @@ +/* +* OFB Mode +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/ofb.h> +#include <botan/xor_buf.h> +#include <algorithm> + +namespace Botan { + +/* +* OFB Constructor +*/ +OFB::OFB(BlockCipher* ciph) : + BlockCipherMode(ciph, "OFB", ciph->BLOCK_SIZE, 2) + { + } + +/* +* OFB Constructor +*/ +OFB::OFB(BlockCipher* ciph, const SymmetricKey& key, + const InitializationVector& iv) : + BlockCipherMode(ciph, "OFB", ciph->BLOCK_SIZE, 2) + { + set_key(key); + set_iv(iv); + } + +/* +* OFB Encryption/Decryption +*/ +void OFB::write(const byte input[], u32bit length) + { + u32bit copied = std::min(BLOCK_SIZE - position, length); + xor_buf(buffer, input, state + position, copied); + send(buffer, copied); + input += copied; + length -= copied; + position += copied; + + if(position == BLOCK_SIZE) + { + cipher->encrypt(state); + position = 0; + } + + while(length >= BLOCK_SIZE) + { + xor_buf(buffer, input, state, BLOCK_SIZE); + send(buffer, BLOCK_SIZE); + + input += BLOCK_SIZE; + length -= BLOCK_SIZE; + cipher->encrypt(state); + } + + xor_buf(buffer, input, state + position, length); + send(buffer, length); + position += length; + } + +} diff --git a/botan/src/modes/ofb/ofb.h b/botan/src/modes/ofb/ofb.h new file mode 100644 index 0000000..a3aadc1 --- /dev/null +++ b/botan/src/modes/ofb/ofb.h @@ -0,0 +1,33 @@ +/* +* OFB Mode +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_OUTPUT_FEEDBACK_MODE_H__ +#define BOTAN_OUTPUT_FEEDBACK_MODE_H__ + +#include <botan/modebase.h> +#include <botan/block_cipher.h> + +namespace Botan { + +/* +* OFB Mode +*/ +class BOTAN_DLL OFB : public BlockCipherMode + { + public: + OFB(BlockCipher* cipher); + + OFB(BlockCipher* cipher, + const SymmetricKey& key, + const InitializationVector& iv); + private: + void write(const byte[], u32bit); + }; + +} + +#endif diff --git a/botan/src/modes/xts/info.txt b/botan/src/modes/xts/info.txt new file mode 100644 index 0000000..65c7df2 --- /dev/null +++ b/botan/src/modes/xts/info.txt @@ -0,0 +1,15 @@ +realname "XTS block cipher mode" + +define XTS + +load_on auto + +<add> +xts.cpp +xts.h +</add> + +<requires> +block +filters +</requires> diff --git a/botan/src/modes/xts/xts.cpp b/botan/src/modes/xts/xts.cpp new file mode 100644 index 0000000..8780ae1 --- /dev/null +++ b/botan/src/modes/xts/xts.cpp @@ -0,0 +1,344 @@ +/* +* XTS Mode +* (C) 2009 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/xts.h> +#include <botan/xor_buf.h> +#include <algorithm> +#include <stdexcept> + +namespace Botan { + +namespace { + +void poly_double(byte tweak[], u32bit size) + { + const byte polynomial = 0x87; // for 128 bit ciphers + + byte carry = 0; + for(u32bit i = 0; i != size; ++i) + { + byte carry2 = (tweak[i] >> 7); + tweak[i] = (tweak[i] << 1) | carry; + carry = carry2; + } + + if(carry) + tweak[0] ^= polynomial; + } + +} + +/* +* XTS_Encryption constructor +*/ +XTS_Encryption::XTS_Encryption(BlockCipher* ciph) : cipher(ciph) + { + if(cipher->BLOCK_SIZE != 16) + throw std::invalid_argument("Bad cipher for XTS: " + cipher->name()); + + cipher2 = cipher->clone(); + tweak.create(cipher->BLOCK_SIZE); + buffer.create(2 * cipher->BLOCK_SIZE); + position = 0; + } + +/* +* XTS_Encryption constructor +*/ +XTS_Encryption::XTS_Encryption(BlockCipher* ciph, + const SymmetricKey& key, + const InitializationVector& iv) : cipher(ciph) + { + if(cipher->BLOCK_SIZE != 16) + throw std::invalid_argument("Bad cipher for XTS: " + cipher->name()); + + cipher2 = cipher->clone(); + tweak.create(cipher->BLOCK_SIZE); + buffer.create(2 * cipher->BLOCK_SIZE); + position = 0; + + set_key(key); + set_iv(iv); + } + +/* +* Return the name +*/ +std::string XTS_Encryption::name() const + { + return (cipher->name() + "/XTS"); + } + +/* +* Set new tweak +*/ +void XTS_Encryption::set_iv(const InitializationVector& iv) + { + if(iv.length() != tweak.size()) + throw Invalid_IV_Length(name(), iv.length()); + + tweak = iv.bits_of(); + cipher2->encrypt(tweak); + } + +void XTS_Encryption::set_key(const SymmetricKey& key) + { + u32bit key_half = key.length() / 2; + + if(key.length() % 2 == 1 || !cipher->valid_keylength(key_half)) + throw Invalid_Key_Length(name(), key.length()); + + cipher->set_key(key.begin(), key_half); + cipher2->set_key(key.begin() + key_half, key_half); + } + +void XTS_Encryption::encrypt(const byte block[]) + { + /* + * We can always use the first 16 bytes of buffer as temp space, + * since either the input block is buffer (in which case this is + * just buffer ^= tweak) or it not, in which case we already read + * and used the data there and are processing new input. Kind of + * subtle/nasty, but saves allocating a distinct temp buf. + */ + + xor_buf(buffer, block, tweak, cipher->BLOCK_SIZE); + cipher->encrypt(buffer); + xor_buf(buffer, tweak, cipher->BLOCK_SIZE); + + poly_double(tweak, cipher->BLOCK_SIZE); + + send(buffer, cipher->BLOCK_SIZE); + } + +/* +* Encrypt in XTS mode +*/ +void XTS_Encryption::write(const byte input[], u32bit length) + { + const u32bit BLOCK_SIZE = cipher->BLOCK_SIZE; + + u32bit copied = std::min(buffer.size() - position, length); + buffer.copy(position, input, copied); + length -= copied; + input += copied; + position += copied; + + if(length == 0) return; + + encrypt(buffer); + if(length > BLOCK_SIZE) + { + encrypt(buffer + BLOCK_SIZE); + while(length > buffer.size()) + { + encrypt(input); + length -= BLOCK_SIZE; + input += BLOCK_SIZE; + } + position = 0; + } + else + { + copy_mem(buffer.begin(), buffer + BLOCK_SIZE, BLOCK_SIZE); + position = BLOCK_SIZE; + } + buffer.copy(position, input, length); + position += length; + } + +/* +* Finish encrypting in XTS mode +*/ +void XTS_Encryption::end_msg() + { + const u32bit BLOCK_SIZE = cipher->BLOCK_SIZE; + + if(position < BLOCK_SIZE) + throw Exception("XTS_Encryption: insufficient data to encrypt"); + else if(position == BLOCK_SIZE) + { + encrypt(buffer); + } + else if(position == 2*BLOCK_SIZE) + { + encrypt(buffer); + encrypt(buffer + BLOCK_SIZE); + } + else + { // steal ciphertext + xor_buf(buffer, tweak, cipher->BLOCK_SIZE); + cipher->encrypt(buffer); + xor_buf(buffer, tweak, cipher->BLOCK_SIZE); + + poly_double(tweak, cipher->BLOCK_SIZE); + + for(u32bit i = 0; i != position - cipher->BLOCK_SIZE; ++i) + std::swap(buffer[i], buffer[i + cipher->BLOCK_SIZE]); + + xor_buf(buffer, tweak, cipher->BLOCK_SIZE); + cipher->encrypt(buffer); + xor_buf(buffer, tweak, cipher->BLOCK_SIZE); + + send(buffer, position); + } + + position = 0; + } + +/* +* XTS_Decryption constructor +*/ +XTS_Decryption::XTS_Decryption(BlockCipher* ciph) + { + cipher = ciph; + cipher2 = ciph->clone(); + tweak.create(cipher->BLOCK_SIZE); + buffer.create(2 * cipher->BLOCK_SIZE); + position = 0; + } + +/* +* XTS_Decryption constructor +*/ +XTS_Decryption::XTS_Decryption(BlockCipher* ciph, + const SymmetricKey& key, + const InitializationVector& iv) + { + cipher = ciph; + cipher2 = ciph->clone(); + tweak.create(cipher->BLOCK_SIZE); + buffer.create(2 * cipher->BLOCK_SIZE); + position = 0; + + set_key(key); + set_iv(iv); + } + +/* +* Return the name +*/ +std::string XTS_Decryption::name() const + { + return (cipher->name() + "/XTS"); + } + +/* +* Set new tweak +*/ +void XTS_Decryption::set_iv(const InitializationVector& iv) + { + if(iv.length() != tweak.size()) + throw Invalid_IV_Length(name(), iv.length()); + + tweak = iv.bits_of(); + cipher2->encrypt(tweak); + } + +void XTS_Decryption::set_key(const SymmetricKey& key) + { + u32bit key_half = key.length() / 2; + + if(key.length() % 2 == 1 || !cipher->valid_keylength(key_half)) + throw Invalid_Key_Length(name(), key.length()); + + cipher->set_key(key.begin(), key_half); + cipher2->set_key(key.begin() + key_half, key_half); + } + +/* +* Decrypt a block +*/ +void XTS_Decryption::decrypt(const byte block[]) + { + xor_buf(buffer, block, tweak, cipher->BLOCK_SIZE); + cipher->decrypt(buffer); + xor_buf(buffer, tweak, cipher->BLOCK_SIZE); + + poly_double(tweak, cipher->BLOCK_SIZE); + + send(buffer, cipher->BLOCK_SIZE); + } + +/* +* Decrypt in XTS mode +*/ +void XTS_Decryption::write(const byte input[], u32bit length) + { + const u32bit BLOCK_SIZE = cipher->BLOCK_SIZE; + + u32bit copied = std::min(buffer.size() - position, length); + buffer.copy(position, input, copied); + length -= copied; + input += copied; + position += copied; + + if(length == 0) return; + + decrypt(buffer); + if(length > BLOCK_SIZE) + { + decrypt(buffer + BLOCK_SIZE); + while(length > 2*BLOCK_SIZE) + { + decrypt(input); + length -= BLOCK_SIZE; + input += BLOCK_SIZE; + } + position = 0; + } + else + { + copy_mem(buffer.begin(), buffer + BLOCK_SIZE, BLOCK_SIZE); + position = BLOCK_SIZE; + } + buffer.copy(position, input, length); + position += length; + } + +/* +* Finish decrypting in XTS mode +*/ +void XTS_Decryption::end_msg() + { + const u32bit BLOCK_SIZE = cipher->BLOCK_SIZE; + + if(position < BLOCK_SIZE) + throw Exception("XTS_Decryption: insufficient data to decrypt"); + else if(position == BLOCK_SIZE) + { + decrypt(buffer); + } + else if(position == 2*BLOCK_SIZE) + { + decrypt(buffer); + decrypt(buffer + BLOCK_SIZE); + } + else + { + SecureVector<byte> tweak2 = tweak; + + poly_double(tweak2, cipher->BLOCK_SIZE); + + xor_buf(buffer, tweak2, cipher->BLOCK_SIZE); + cipher->decrypt(buffer); + xor_buf(buffer, tweak2, cipher->BLOCK_SIZE); + + for(u32bit i = 0; i != position - cipher->BLOCK_SIZE; ++i) + std::swap(buffer[i], buffer[i + cipher->BLOCK_SIZE]); + + xor_buf(buffer, tweak, cipher->BLOCK_SIZE); + cipher->decrypt(buffer); + xor_buf(buffer, tweak, cipher->BLOCK_SIZE); + + send(buffer, position); + } + + position = 0; + } + +} diff --git a/botan/src/modes/xts/xts.h b/botan/src/modes/xts/xts.h new file mode 100644 index 0000000..0155817 --- /dev/null +++ b/botan/src/modes/xts/xts.h @@ -0,0 +1,76 @@ +/* +* XTS mode, from IEEE P1619 +* (C) 2009 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_XTS_H__ +#define BOTAN_XTS_H__ + +#include <botan/basefilt.h> +#include <botan/block_cipher.h> + +namespace Botan { + +/* +* XTS Encryption +*/ +class BOTAN_DLL XTS_Encryption : public Keyed_Filter + { + public: + void set_key(const SymmetricKey& key); + void set_iv(const InitializationVector& iv); + + std::string name() const; + + XTS_Encryption(BlockCipher* ciph); + + XTS_Encryption(BlockCipher* ciph, + const SymmetricKey& key, + const InitializationVector& iv); + + ~XTS_Encryption() { delete cipher; delete cipher2; } + private: + void write(const byte[], u32bit); + void end_msg(); + void encrypt(const byte block[]); + + BlockCipher* cipher; + BlockCipher* cipher2; + SecureVector<byte> tweak; + SecureVector<byte> buffer; + u32bit position; + }; + +/* +* XTS Decryption +*/ +class BOTAN_DLL XTS_Decryption : public Keyed_Filter + { + public: + void set_key(const SymmetricKey& key); + void set_iv(const InitializationVector& iv); + + std::string name() const; + + XTS_Decryption(BlockCipher* ciph); + + XTS_Decryption(BlockCipher* ciph, + const SymmetricKey& key, + const InitializationVector& iv); + private: + void write(const byte[], u32bit); + void end_msg(); + void decrypt(const byte[]); + + BlockCipher* cipher; + BlockCipher* cipher2; + SecureVector<byte> tweak; + SecureVector<byte> buffer; + u32bit position; + }; + +} + +#endif diff --git a/botan/src/mutex/info.txt b/botan/src/mutex/info.txt new file mode 100644 index 0000000..ff79bf7 --- /dev/null +++ b/botan/src/mutex/info.txt @@ -0,0 +1,9 @@ +realname "Mutex Wrappers" + +define MUTEX_WRAPPERS + +load_on auto + +<add> +mutex.h +</add> diff --git a/botan/src/mutex/mutex.h b/botan/src/mutex/mutex.h new file mode 100644 index 0000000..a04ff83 --- /dev/null +++ b/botan/src/mutex/mutex.h @@ -0,0 +1,56 @@ +/* +* Mutex +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_MUTEX_H__ +#define BOTAN_MUTEX_H__ + +#include <botan/exceptn.h> + +namespace Botan { + +/* +* Mutex Base Class +*/ +class BOTAN_DLL Mutex + { + public: + virtual void lock() = 0; + virtual void unlock() = 0; + virtual ~Mutex() {} + }; + +/* +* Mutex Factory +*/ +class BOTAN_DLL Mutex_Factory + { + public: + virtual Mutex* make() = 0; + virtual ~Mutex_Factory() {} + }; + +/* +* Mutex Holding Class +*/ +class BOTAN_DLL Mutex_Holder + { + public: + Mutex_Holder(Mutex* m) : mux(m) + { + if(!mux) + throw Invalid_Argument("Mutex_Holder: Argument was NULL"); + mux->lock(); + } + + ~Mutex_Holder() { mux->unlock(); } + private: + Mutex* mux; + }; + +} + +#endif diff --git a/botan/src/mutex/noop_mutex/info.txt b/botan/src/mutex/noop_mutex/info.txt new file mode 100644 index 0000000..1f49f5e --- /dev/null +++ b/botan/src/mutex/noop_mutex/info.txt @@ -0,0 +1,10 @@ +realname "No-Op Mutex" + +load_on auto + +define MUTEX_NOOP + +<add> +mux_noop.cpp +mux_noop.h +</add> diff --git a/botan/src/mutex/noop_mutex/mux_noop.cpp b/botan/src/mutex/noop_mutex/mux_noop.cpp new file mode 100644 index 0000000..5c45084 --- /dev/null +++ b/botan/src/mutex/noop_mutex/mux_noop.cpp @@ -0,0 +1,50 @@ +/* +* No-Op Mutex Factory +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/mux_noop.h> + +namespace Botan { + +/* +* No-Op Mutex Factory +*/ +Mutex* Noop_Mutex_Factory::make() + { + class Noop_Mutex : public Mutex + { + public: + class Mutex_State_Error : public Internal_Error + { + public: + Mutex_State_Error(const std::string& where) : + Internal_Error("Noop_Mutex::" + where + ": " + + "Mutex is already " + where + "ed") {} + }; + + void lock() + { + if(locked) + throw Mutex_State_Error("lock"); + locked = true; + } + + void unlock() + { + if(!locked) + throw Mutex_State_Error("unlock"); + locked = false; + } + + Noop_Mutex() { locked = false; } + private: + bool locked; + }; + + return new Noop_Mutex; + } + +} diff --git a/botan/src/mutex/noop_mutex/mux_noop.h b/botan/src/mutex/noop_mutex/mux_noop.h new file mode 100644 index 0000000..94201cb --- /dev/null +++ b/botan/src/mutex/noop_mutex/mux_noop.h @@ -0,0 +1,26 @@ +/* +* No-Op Mutex Factory +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_NOOP_MUTEX_FACTORY_H__ +#define BOTAN_NOOP_MUTEX_FACTORY_H__ + +#include <botan/mutex.h> + +namespace Botan { + +/* +* No-Op Mutex Factory +*/ +class BOTAN_DLL Noop_Mutex_Factory : public Mutex_Factory + { + public: + Mutex* make(); + }; + +} + +#endif diff --git a/botan/src/mutex/pthreads/info.txt b/botan/src/mutex/pthreads/info.txt new file mode 100644 index 0000000..f135dea --- /dev/null +++ b/botan/src/mutex/pthreads/info.txt @@ -0,0 +1,30 @@ +realname "Pthread Mutex" + +define MUTEX_PTHREAD + +load_on auto + +<add> +mux_pthr.cpp +mux_pthr.h +</add> + +<libs> +all!qnx,freebsd,dragonfly,openbsd,netbsd -> pthread +</libs> + +<os> +aix +cygwin +darwin +freebsd +dragonfly +hpux +irix +linux +netbsd +openbsd +qnx +solaris +tru64 +</os> diff --git a/botan/src/mutex/pthreads/mux_pthr.cpp b/botan/src/mutex/pthreads/mux_pthr.cpp new file mode 100644 index 0000000..9f1d981 --- /dev/null +++ b/botan/src/mutex/pthreads/mux_pthr.cpp @@ -0,0 +1,58 @@ +/* +* Pthread Mutex +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/mux_pthr.h> +#include <botan/exceptn.h> + +#ifndef _POSIX_C_SOURCE + #define _POSIX_C_SOURCE 199506 +#endif + +#include <pthread.h> + +namespace Botan { + +/* +* Pthread Mutex Factory +*/ +Mutex* Pthread_Mutex_Factory::make() + { + + class Pthread_Mutex : public Mutex + { + public: + void lock() + { + if(pthread_mutex_lock(&mutex) != 0) + throw Exception("Pthread_Mutex::lock: Error occured"); + } + + void unlock() + { + if(pthread_mutex_unlock(&mutex) != 0) + throw Exception("Pthread_Mutex::unlock: Error occured"); + } + + Pthread_Mutex() + { + if(pthread_mutex_init(&mutex, 0) != 0) + throw Exception("Pthread_Mutex: initialization failed"); + } + + ~Pthread_Mutex() + { + if(pthread_mutex_destroy(&mutex) != 0) + throw Invalid_State("~Pthread_Mutex: mutex is still locked"); + } + private: + pthread_mutex_t mutex; + }; + + return new Pthread_Mutex(); + } + +} diff --git a/botan/src/mutex/pthreads/mux_pthr.h b/botan/src/mutex/pthreads/mux_pthr.h new file mode 100644 index 0000000..1188539 --- /dev/null +++ b/botan/src/mutex/pthreads/mux_pthr.h @@ -0,0 +1,26 @@ +/* +* Pthread Mutex +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_MUTEX_PTHREAD_H__ +#define BOTAN_MUTEX_PTHREAD_H__ + +#include <botan/mutex.h> + +namespace Botan { + +/* +* Pthread Mutex Factory +*/ +class BOTAN_DLL Pthread_Mutex_Factory : public Mutex_Factory + { + public: + Mutex* make(); + }; + +} + +#endif diff --git a/botan/src/mutex/qt_mutex/info.txt b/botan/src/mutex/qt_mutex/info.txt new file mode 100644 index 0000000..a21108c --- /dev/null +++ b/botan/src/mutex/qt_mutex/info.txt @@ -0,0 +1,18 @@ +realname "Qt Mutex" + +define MUTEX_QT + +note "You'll probably have to add -I/-L flags to the Makefile to find Qt" + +load_on request + +<add> +mux_qt.cpp +mux_qt.h +</add> + +# I think we want to always use qt-mt, not qt -- not much point in supporting +# mutexes in a single threaded application, after all. +<libs> +all -> qt-mt +</libs> diff --git a/botan/src/mutex/qt_mutex/mux_qt.cpp b/botan/src/mutex/qt_mutex/mux_qt.cpp new file mode 100644 index 0000000..0f670c8 --- /dev/null +++ b/botan/src/mutex/qt_mutex/mux_qt.cpp @@ -0,0 +1,35 @@ +/* +* Qt Thread Mutex +* (C) 2004-2007 Justin Karneges +* 2004-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/mux_qt.h> +#include <qmutex.h> + +#if !defined(QT_THREAD_SUPPORT) + #error Your version of Qt does not support threads or mutexes +#endif + +namespace Botan { + +/* +* Qt Mutex Factory +*/ +Mutex* Qt_Mutex_Factory::make() + { + class Qt_Mutex : public Mutex + { + public: + void lock() { mutex.lock(); } + void unlock() { mutex.unlock(); } + private: + QMutex mutex; + }; + + return new Qt_Mutex(); + } + +} diff --git a/botan/src/mutex/qt_mutex/mux_qt.h b/botan/src/mutex/qt_mutex/mux_qt.h new file mode 100644 index 0000000..5aed77f --- /dev/null +++ b/botan/src/mutex/qt_mutex/mux_qt.h @@ -0,0 +1,27 @@ +/* +* Qt Mutex +* (C) 2004-2007 Justin Karneges +* 2004-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_MUTEX_QT_H__ +#define BOTAN_MUTEX_QT_H__ + +#include <botan/mutex.h> + +namespace Botan { + +/* +* Qt Mutex +*/ +class BOTAN_DLL Qt_Mutex_Factory : public Mutex_Factory + { + public: + Mutex* make(); + }; + +} + +#endif diff --git a/botan/src/mutex/win32_crit_section/info.txt b/botan/src/mutex/win32_crit_section/info.txt new file mode 100644 index 0000000..a2d339c --- /dev/null +++ b/botan/src/mutex/win32_crit_section/info.txt @@ -0,0 +1,17 @@ +realname "Win32 Mutex" + +define MUTEX_WIN32 +modset win32 + +load_on auto + +<add> +mux_win32.cpp +mux_win32.h +</add> + +<os> +cygwin +windows +mingw +</os> diff --git a/botan/src/mutex/win32_crit_section/mux_win32.cpp b/botan/src/mutex/win32_crit_section/mux_win32.cpp new file mode 100644 index 0000000..2a96789 --- /dev/null +++ b/botan/src/mutex/win32_crit_section/mux_win32.cpp @@ -0,0 +1,34 @@ +/* +* Win32 Mutex +* (C) 2006 Luca Piccarreta +* 2006-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/mux_win32.h> +#include <windows.h> + +namespace Botan { + +/* +* Win32 Mutex Factory +*/ +Mutex* Win32_Mutex_Factory::make() + { + class Win32_Mutex : public Mutex + { + public: + void lock() { EnterCriticalSection(&mutex); } + void unlock() { LeaveCriticalSection(&mutex); } + + Win32_Mutex() { InitializeCriticalSection(&mutex); } + ~Win32_Mutex() { DeleteCriticalSection(&mutex); } + private: + CRITICAL_SECTION mutex; + }; + + return new Win32_Mutex(); + } + +} diff --git a/botan/src/mutex/win32_crit_section/mux_win32.h b/botan/src/mutex/win32_crit_section/mux_win32.h new file mode 100644 index 0000000..a91850e --- /dev/null +++ b/botan/src/mutex/win32_crit_section/mux_win32.h @@ -0,0 +1,26 @@ +/* +* Win32 Mutex +* (C) 2006 Luca Piccarreta +* 2006-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_MUTEX_WIN32_H__ +#define BOTAN_MUTEX_WIN32_H__ + +#include <botan/mutex.h> + +namespace Botan { + +/* +* Win32 Mutex Factory +*/ +class BOTAN_DLL Win32_Mutex_Factory : public Mutex_Factory + { + public: + Mutex* make(); + }; +} + +#endif diff --git a/botan/src/pbe/get_pbe.cpp b/botan/src/pbe/get_pbe.cpp new file mode 100644 index 0000000..3217101 --- /dev/null +++ b/botan/src/pbe/get_pbe.cpp @@ -0,0 +1,130 @@ +/* +* PBE Retrieval +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/get_pbe.h> +#include <botan/oids.h> +#include <botan/scan_name.h> +#include <botan/parsing.h> +#include <botan/libstate.h> + +#if defined(BOTAN_HAS_PBE_PKCS_V15) + #include <botan/pbes1.h> +#endif + +#if defined(BOTAN_HAS_PBE_PKCS_V20) + #include <botan/pbes2.h> +#endif + +namespace Botan { + +/* +* Get an encryption PBE, set new parameters +*/ +PBE* get_pbe(const std::string& algo_spec) + { + SCAN_Name request(algo_spec); + + const std::string pbe = request.algo_name(); + std::string digest_name = request.arg(0); + const std::string cipher = request.arg(1); + + std::vector<std::string> cipher_spec = split_on(cipher, '/'); + if(cipher_spec.size() != 2) + throw Invalid_Argument("PBE: Invalid cipher spec " + cipher); + + const std::string cipher_algo = global_state().deref_alias(cipher_spec[0]); + const std::string cipher_mode = cipher_spec[1]; + + if(cipher_mode != "CBC") + throw Invalid_Argument("PBE: Invalid cipher mode " + cipher); + + Algorithm_Factory& af = global_state().algorithm_factory(); + + const BlockCipher* block_cipher = af.prototype_block_cipher(cipher_algo); + if(!block_cipher) + throw Algorithm_Not_Found(cipher_algo); + + const HashFunction* hash_function = af.prototype_hash_function(digest_name); + if(!hash_function) + throw Algorithm_Not_Found(digest_name); + + if(request.arg_count() != 2) + throw Invalid_Algorithm_Name(algo_spec); + +#if defined(BOTAN_HAS_PBE_PKCS_V15) + if(pbe == "PBE-PKCS5v15") + return new PBE_PKCS5v15(block_cipher->clone(), + hash_function->clone(), + ENCRYPTION); +#endif + +#if defined(BOTAN_HAS_PBE_PKCS_V20) + if(pbe == "PBE-PKCS5v20") + return new PBE_PKCS5v20(block_cipher->clone(), + hash_function->clone()); +#endif + + throw Algorithm_Not_Found(algo_spec); + } + +/* +* Get a decryption PBE, decode parameters +*/ +PBE* get_pbe(const OID& pbe_oid, DataSource& params) + { + SCAN_Name request(OIDS::lookup(pbe_oid)); + + const std::string pbe = request.algo_name(); + +#if defined(BOTAN_HAS_PBE_PKCS_V15) + if(pbe == "PBE-PKCS5v15") + { + if(request.arg_count() != 2) + throw Invalid_Algorithm_Name(request.as_string()); + + std::string digest_name = request.arg(0); + const std::string cipher = request.arg(1); + + std::vector<std::string> cipher_spec = split_on(cipher, '/'); + if(cipher_spec.size() != 2) + throw Invalid_Argument("PBE: Invalid cipher spec " + cipher); + + const std::string cipher_algo = global_state().deref_alias(cipher_spec[0]); + const std::string cipher_mode = cipher_spec[1]; + + if(cipher_mode != "CBC") + throw Invalid_Argument("PBE: Invalid cipher mode " + cipher); + + Algorithm_Factory& af = global_state().algorithm_factory(); + + const BlockCipher* block_cipher = af.prototype_block_cipher(cipher_algo); + if(!block_cipher) + throw Algorithm_Not_Found(cipher_algo); + + const HashFunction* hash_function = + af.prototype_hash_function(digest_name); + + if(!hash_function) + throw Algorithm_Not_Found(digest_name); + + PBE* pbe = new PBE_PKCS5v15(block_cipher->clone(), + hash_function->clone(), + DECRYPTION); + pbe->decode_params(params); + return pbe; + } +#endif + +#if defined(BOTAN_HAS_PBE_PKCS_V20) + if(pbe == "PBE-PKCS5v20") + return new PBE_PKCS5v20(params); +#endif + + throw Algorithm_Not_Found(pbe_oid.as_string()); + } + +} diff --git a/botan/src/pbe/get_pbe.h b/botan/src/pbe/get_pbe.h new file mode 100644 index 0000000..04eda66 --- /dev/null +++ b/botan/src/pbe/get_pbe.h @@ -0,0 +1,33 @@ +/* +* PBE Lookup +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_LOOKUP_PBE_H__ +#define BOTAN_LOOKUP_PBE_H__ + +#include <botan/pbe.h> +#include <string> + +namespace Botan { + +/** +* Factory function for PBEs. +* @param algo_spec the name of the PBE algorithm to retrieve +* @return a pointer to a PBE with randomly created parameters +*/ +BOTAN_DLL PBE* get_pbe(const std::string&); + +/** +* Factory function for PBEs. +* @param pbe_oid the oid of the desired PBE +* @param params a DataSource providing the DER encoded parameters to use +* @return a pointer to the PBE with the specified parameters +*/ +BOTAN_DLL PBE* get_pbe(const OID&, DataSource&); + +} + +#endif diff --git a/botan/src/pbe/info.txt b/botan/src/pbe/info.txt new file mode 100644 index 0000000..c4210b2 --- /dev/null +++ b/botan/src/pbe/info.txt @@ -0,0 +1,16 @@ +realname "PBE Base" + +load_on dep + +define PASSWORD_BASED_ENCRYPTION + +<add> +get_pbe.cpp +get_pbe.h +</add> + +<requires> +filters +libstate +oid_lookup +</requires> diff --git a/botan/src/pbe/pbes1/info.txt b/botan/src/pbe/pbes1/info.txt new file mode 100644 index 0000000..70c6bae --- /dev/null +++ b/botan/src/pbe/pbes1/info.txt @@ -0,0 +1,19 @@ +realname "PKCS5 v1.5 PBE" + +define PBE_PKCS_V15 + +load_on auto + +<add> +pbes1.cpp +pbes1.h +</add> + +<requires> +asn1 +block +cbc +filters +hash +pbkdf1 +</requires> diff --git a/botan/src/pbe/pbes1/pbes1.cpp b/botan/src/pbe/pbes1/pbes1.cpp new file mode 100644 index 0000000..21bd330 --- /dev/null +++ b/botan/src/pbe/pbes1/pbes1.cpp @@ -0,0 +1,184 @@ +/* +* PKCS #5 PBES1 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/pbes1.h> +#include <botan/pbkdf1.h> +#include <botan/der_enc.h> +#include <botan/ber_dec.h> +#include <botan/cbc.h> +#include <algorithm> + +namespace Botan { + +/* +* Encrypt some bytes using PBES1 +*/ +void PBE_PKCS5v15::write(const byte input[], u32bit length) + { + while(length) + { + u32bit put = std::min(DEFAULT_BUFFERSIZE, length); + pipe.write(input, length); + flush_pipe(true); + length -= put; + } + } + +/* +* Start encrypting with PBES1 +*/ +void PBE_PKCS5v15::start_msg() + { + if(direction == ENCRYPTION) + pipe.append(new CBC_Encryption(block_cipher->clone(), + new PKCS7_Padding, + key, iv)); + else + pipe.append(new CBC_Decryption(block_cipher->clone(), + new PKCS7_Padding, + key, iv)); + + pipe.start_msg(); + if(pipe.message_count() > 1) + pipe.set_default_msg(pipe.default_msg() + 1); + } + +/* +* Finish encrypting with PBES1 +*/ +void PBE_PKCS5v15::end_msg() + { + pipe.end_msg(); + flush_pipe(false); + pipe.reset(); + } + +/* +* Flush the pipe +*/ +void PBE_PKCS5v15::flush_pipe(bool safe_to_skip) + { + if(safe_to_skip && pipe.remaining() < 64) + return; + + SecureVector<byte> buffer(DEFAULT_BUFFERSIZE); + while(pipe.remaining()) + { + u32bit got = pipe.read(buffer, buffer.size()); + send(buffer, got); + } + } + +/* +* Set the passphrase to use +*/ +void PBE_PKCS5v15::set_key(const std::string& passphrase) + { + PKCS5_PBKDF1 pbkdf(hash_function->clone()); + + pbkdf.set_iterations(iterations); + pbkdf.change_salt(salt, salt.size()); + SymmetricKey key_and_iv = pbkdf.derive_key(16, passphrase); + + key.set(key_and_iv.begin(), 8); + iv.set(key_and_iv.begin() + 8, 8); + } + +/* +* Create a new set of PBES1 parameters +*/ +void PBE_PKCS5v15::new_params(RandomNumberGenerator& rng) + { + iterations = 2048; + salt.create(8); + rng.randomize(salt, salt.size()); + } + +/* +* Encode PKCS#5 PBES1 parameters +*/ +MemoryVector<byte> PBE_PKCS5v15::encode_params() const + { + return DER_Encoder() + .start_cons(SEQUENCE) + .encode(salt, OCTET_STRING) + .encode(iterations) + .end_cons() + .get_contents(); + } + +/* +* Decode PKCS#5 PBES1 parameters +*/ +void PBE_PKCS5v15::decode_params(DataSource& source) + { + BER_Decoder(source) + .start_cons(SEQUENCE) + .decode(salt, OCTET_STRING) + .decode(iterations) + .verify_end() + .end_cons(); + + if(salt.size() != 8) + throw Decoding_Error("PBES1: Encoded salt is not 8 octets"); + } + +/* +* Return an OID for this PBES1 type +*/ +OID PBE_PKCS5v15::get_oid() const + { + const OID base_pbes1_oid("1.2.840.113549.1.5"); + + const std::string cipher = block_cipher->name(); + const std::string digest = hash_function->name(); + + if(cipher == "DES" && digest == "MD2") + return (base_pbes1_oid + 1); + else if(cipher == "DES" && digest == "MD5") + return (base_pbes1_oid + 3); + else if(cipher == "DES" && digest == "SHA-160") + return (base_pbes1_oid + 10); + else if(cipher == "RC2" && digest == "MD2") + return (base_pbes1_oid + 4); + else if(cipher == "RC2" && digest == "MD5") + return (base_pbes1_oid + 6); + else if(cipher == "RC2" && digest == "SHA-160") + return (base_pbes1_oid + 11); + else + throw Internal_Error("PBE-PKCS5 v1.5: get_oid() has run out of options"); + } + +/* +* PKCS#5 v1.5 PBE Constructor +*/ +PBE_PKCS5v15::PBE_PKCS5v15(BlockCipher* cipher, + HashFunction* hash, + Cipher_Dir dir) : + direction(dir), block_cipher(cipher), hash_function(hash) + { + if(cipher->name() != "DES" && cipher->name() != "RC2") + { + throw Invalid_Argument("PBE_PKCS5v1.5: Unknown cipher " + + cipher->name()); + } + + if(hash->name() != "MD2" && hash->name() != "MD5" && + hash->name() != "SHA-160") + { + throw Invalid_Argument("PBE_PKCS5v1.5: Unknown hash " + + hash->name()); + } + } + +PBE_PKCS5v15::~PBE_PKCS5v15() + { + delete block_cipher; + delete hash_function; + } + +} diff --git a/botan/src/pbe/pbes1/pbes1.h b/botan/src/pbe/pbes1/pbes1.h new file mode 100644 index 0000000..2e1855d --- /dev/null +++ b/botan/src/pbe/pbes1/pbes1.h @@ -0,0 +1,53 @@ +/* +* PKCS #5 v1.5 PBE +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_PBE_PKCS_V15_H__ +#define BOTAN_PBE_PKCS_V15_H__ + +#include <botan/pbe.h> +#include <botan/block_cipher.h> +#include <botan/hash.h> +#include <botan/pipe.h> + +namespace Botan { + +/* +* PKCS#5 v1.5 PBE +*/ +class BOTAN_DLL PBE_PKCS5v15 : public PBE + { + public: + void write(const byte[], u32bit); + void start_msg(); + void end_msg(); + + PBE_PKCS5v15(BlockCipher* cipher, + HashFunction* hash, + Cipher_Dir); + + ~PBE_PKCS5v15(); + private: + void set_key(const std::string&); + void new_params(RandomNumberGenerator& rng); + MemoryVector<byte> encode_params() const; + void decode_params(DataSource&); + OID get_oid() const; + + void flush_pipe(bool); + + Cipher_Dir direction; + BlockCipher* block_cipher; + HashFunction* hash_function; + + SecureVector<byte> salt, key, iv; + u32bit iterations; + Pipe pipe; + }; + +} + +#endif diff --git a/botan/src/pbe/pbes2/info.txt b/botan/src/pbe/pbes2/info.txt new file mode 100644 index 0000000..cd37b1e --- /dev/null +++ b/botan/src/pbe/pbes2/info.txt @@ -0,0 +1,23 @@ +realname "PKCS5 v2.0 PBE" + +define PBE_PKCS_V20 + +load_on auto + +<add> +pbes2.cpp +pbes2.h +</add> + +<requires> +algo_factory +asn1 +block +cbc +filters +hash +hmac +libstate +oid_lookup +pbkdf2 +</requires> diff --git a/botan/src/pbe/pbes2/pbes2.cpp b/botan/src/pbe/pbes2/pbes2.cpp new file mode 100644 index 0000000..b7e2589 --- /dev/null +++ b/botan/src/pbe/pbes2/pbes2.cpp @@ -0,0 +1,239 @@ +/** +* PKCS #5 PBES2 +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/pbes2.h> +#include <botan/pbkdf2.h> +#include <botan/hmac.h> +#include <botan/cbc.h> +#include <botan/algo_factory.h> +#include <botan/libstate.h> +#include <botan/der_enc.h> +#include <botan/ber_dec.h> +#include <botan/parsing.h> +#include <botan/asn1_obj.h> +#include <botan/oids.h> +#include <algorithm> +#include <memory> + +namespace Botan { + +/** +* Encrypt some bytes using PBES2 +*/ +void PBE_PKCS5v20::write(const byte input[], u32bit length) + { + while(length) + { + u32bit put = std::min(DEFAULT_BUFFERSIZE, length); + pipe.write(input, length); + flush_pipe(true); + length -= put; + } + } + +/** +* Start encrypting with PBES2 +*/ +void PBE_PKCS5v20::start_msg() + { + if(direction == ENCRYPTION) + pipe.append(new CBC_Encryption(block_cipher->clone(), + new PKCS7_Padding, + key, iv)); + else + pipe.append(new CBC_Decryption(block_cipher->clone(), + new PKCS7_Padding, + key, iv)); + + pipe.start_msg(); + if(pipe.message_count() > 1) + pipe.set_default_msg(pipe.default_msg() + 1); + } + +/** +* Finish encrypting with PBES2 +*/ +void PBE_PKCS5v20::end_msg() + { + pipe.end_msg(); + flush_pipe(false); + pipe.reset(); + } + +/** +* Flush the pipe +*/ +void PBE_PKCS5v20::flush_pipe(bool safe_to_skip) + { + if(safe_to_skip && pipe.remaining() < 64) + return; + + SecureVector<byte> buffer(DEFAULT_BUFFERSIZE); + while(pipe.remaining()) + { + u32bit got = pipe.read(buffer, buffer.size()); + send(buffer, got); + } + } + +/** +* Set the passphrase to use +*/ +void PBE_PKCS5v20::set_key(const std::string& passphrase) + { + PKCS5_PBKDF2 pbkdf(new HMAC(hash_function->clone())); + + pbkdf.set_iterations(iterations); + pbkdf.change_salt(salt, salt.size()); + key = pbkdf.derive_key(key_length, passphrase).bits_of(); + } + +/** +* Create a new set of PBES2 parameters +*/ +void PBE_PKCS5v20::new_params(RandomNumberGenerator& rng) + { + iterations = 2048; + key_length = block_cipher->MAXIMUM_KEYLENGTH; + + salt.create(8); + rng.randomize(salt, salt.size()); + + iv.create(block_cipher->BLOCK_SIZE); + rng.randomize(iv, iv.size()); + } + +/** +* Encode PKCS#5 PBES2 parameters +*/ +MemoryVector<byte> PBE_PKCS5v20::encode_params() const + { + return DER_Encoder() + .start_cons(SEQUENCE) + .encode( + AlgorithmIdentifier("PKCS5.PBKDF2", + DER_Encoder() + .start_cons(SEQUENCE) + .encode(salt, OCTET_STRING) + .encode(iterations) + .encode(key_length) + .end_cons() + .get_contents() + ) + ) + .encode( + AlgorithmIdentifier(block_cipher->name() + "/CBC", + DER_Encoder() + .encode(iv, OCTET_STRING) + .get_contents() + ) + ) + .end_cons() + .get_contents(); + } + +/** +* Decode PKCS#5 PBES2 parameters +*/ +void PBE_PKCS5v20::decode_params(DataSource& source) + { + AlgorithmIdentifier kdf_algo, enc_algo; + + BER_Decoder(source) + .start_cons(SEQUENCE) + .decode(kdf_algo) + .decode(enc_algo) + .verify_end() + .end_cons(); + + if(kdf_algo.oid == OIDS::lookup("PKCS5.PBKDF2")) + { + BER_Decoder(kdf_algo.parameters) + .start_cons(SEQUENCE) + .decode(salt, OCTET_STRING) + .decode(iterations) + .decode_optional(key_length, INTEGER, UNIVERSAL) + .verify_end() + .end_cons(); + } + else + throw Decoding_Error("PBE-PKCS5 v2.0: Unknown KDF algorithm " + + kdf_algo.oid.as_string()); + + Algorithm_Factory& af = global_state().algorithm_factory(); + + std::string cipher = OIDS::lookup(enc_algo.oid); + std::vector<std::string> cipher_spec = split_on(cipher, '/'); + if(cipher_spec.size() != 2) + throw Decoding_Error("PBE-PKCS5 v2.0: Invalid cipher spec " + cipher); + + if(!known_cipher(cipher_spec[0]) || cipher_spec[1] != "CBC") + throw Decoding_Error("PBE-PKCS5 v2.0: Don't know param format for " + + cipher); + + BER_Decoder(enc_algo.parameters).decode(iv, OCTET_STRING).verify_end(); + + block_cipher = af.make_block_cipher(cipher_spec[0]); + hash_function = af.make_hash_function("SHA-160"); + + if(key_length == 0) + key_length = block_cipher->MAXIMUM_KEYLENGTH; + + if(salt.size() < 8) + throw Decoding_Error("PBE-PKCS5 v2.0: Encoded salt is too small"); + } + +/** +* Return an OID for PBES2 +*/ +OID PBE_PKCS5v20::get_oid() const + { + return OIDS::lookup("PBE-PKCS5v20"); + } + +/** +* Check if this is a known PBES2 cipher +*/ +bool PBE_PKCS5v20::known_cipher(const std::string& algo) + { + if(algo == "AES-128" || algo == "AES-192" || algo == "AES-256") + return true; + if(algo == "DES" || algo == "TripleDES") + return true; + return false; + } + +/** +* PKCS#5 v2.0 PBE Constructor +*/ +PBE_PKCS5v20::PBE_PKCS5v20(BlockCipher* cipher, + HashFunction* digest) : + direction(ENCRYPTION), block_cipher(cipher), hash_function(digest) + { + if(!known_cipher(block_cipher->name())) + throw Invalid_Argument("PBE-PKCS5 v2.0: Invalid cipher " + cipher->name()); + if(hash_function->name() != "SHA-160") + throw Invalid_Argument("PBE-PKCS5 v2.0: Invalid digest " + digest->name()); + } + +/** +* PKCS#5 v2.0 PBE Constructor +*/ +PBE_PKCS5v20::PBE_PKCS5v20(DataSource& params) : direction(DECRYPTION) + { + hash_function = 0; + block_cipher = 0; + decode_params(params); + } + +PBE_PKCS5v20::~PBE_PKCS5v20() + { + delete hash_function; + delete block_cipher; + } + +} diff --git a/botan/src/pbe/pbes2/pbes2.h b/botan/src/pbe/pbes2/pbes2.h new file mode 100644 index 0000000..fc460a2 --- /dev/null +++ b/botan/src/pbe/pbes2/pbes2.h @@ -0,0 +1,53 @@ +/* +* PKCS #5 v2.0 PBE +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_PBE_PKCS_v20_H__ +#define BOTAN_PBE_PKCS_v20_H__ + +#include <botan/pbe.h> +#include <botan/block_cipher.h> +#include <botan/hash.h> +#include <botan/pipe.h> + +namespace Botan { + +/* +* PKCS#5 v2.0 PBE +*/ +class BOTAN_DLL PBE_PKCS5v20 : public PBE + { + public: + static bool known_cipher(const std::string&); + + void write(const byte[], u32bit); + void start_msg(); + void end_msg(); + + PBE_PKCS5v20(DataSource&); + PBE_PKCS5v20(BlockCipher*, HashFunction*); + + ~PBE_PKCS5v20(); + private: + void set_key(const std::string&); + void new_params(RandomNumberGenerator& rng); + MemoryVector<byte> encode_params() const; + void decode_params(DataSource&); + OID get_oid() const; + + void flush_pipe(bool); + + Cipher_Dir direction; + BlockCipher* block_cipher; + HashFunction* hash_function; + SecureVector<byte> salt, key, iv; + u32bit iterations, key_length; + Pipe pipe; + }; + +} + +#endif diff --git a/botan/src/pk_pad/eme.cpp b/botan/src/pk_pad/eme.cpp new file mode 100644 index 0000000..74bba5a --- /dev/null +++ b/botan/src/pk_pad/eme.cpp @@ -0,0 +1,50 @@ +/* +* EME Base Class +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/eme.h> + +namespace Botan { + +/* +* Encode a message +*/ +SecureVector<byte> EME::encode(const byte msg[], u32bit msg_len, + u32bit key_bits, + RandomNumberGenerator& rng) const + { + return pad(msg, msg_len, key_bits, rng); + } + +/* +* Encode a message +*/ +SecureVector<byte> EME::encode(const MemoryRegion<byte>& msg, + u32bit key_bits, + RandomNumberGenerator& rng) const + { + return pad(msg, msg.size(), key_bits, rng); + } + +/* +* Decode a message +*/ +SecureVector<byte> EME::decode(const byte msg[], u32bit msg_len, + u32bit key_bits) const + { + return unpad(msg, msg_len, key_bits); + } + +/* +* Decode a message +*/ +SecureVector<byte> EME::decode(const MemoryRegion<byte>& msg, + u32bit key_bits) const + { + return unpad(msg, msg.size(), key_bits); + } + +} diff --git a/botan/src/pk_pad/eme.h b/botan/src/pk_pad/eme.h new file mode 100644 index 0000000..321c1d0 --- /dev/null +++ b/botan/src/pk_pad/eme.h @@ -0,0 +1,42 @@ +/* +* EME Classes +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_PUBKEY_EME_ENCRYPTION_PAD_H__ +#define BOTAN_PUBKEY_EME_ENCRYPTION_PAD_H__ + +#include <botan/secmem.h> +#include <botan/rng.h> + +namespace Botan { + +/* +* Encoding Method for Encryption +*/ +class BOTAN_DLL EME + { + public: + virtual u32bit maximum_input_size(u32bit) const = 0; + + SecureVector<byte> encode(const byte[], u32bit, u32bit, + RandomNumberGenerator&) const; + SecureVector<byte> encode(const MemoryRegion<byte>&, u32bit, + RandomNumberGenerator&) const; + + SecureVector<byte> decode(const byte[], u32bit, u32bit) const; + SecureVector<byte> decode(const MemoryRegion<byte>&, u32bit) const; + + virtual ~EME() {} + private: + virtual SecureVector<byte> pad(const byte[], u32bit, u32bit, + RandomNumberGenerator&) const = 0; + + virtual SecureVector<byte> unpad(const byte[], u32bit, u32bit) const = 0; + }; + +} + +#endif diff --git a/botan/src/pk_pad/eme1/eme1.cpp b/botan/src/pk_pad/eme1/eme1.cpp new file mode 100644 index 0000000..13f68f8 --- /dev/null +++ b/botan/src/pk_pad/eme1/eme1.cpp @@ -0,0 +1,103 @@ +/* +* EME1 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/eme1.h> +#include <botan/mgf1.h> +#include <memory> + +namespace Botan { + +/* +* EME1 Pad Operation +*/ +SecureVector<byte> EME1::pad(const byte in[], u32bit in_length, + u32bit key_length, + RandomNumberGenerator& rng) const + { + key_length /= 8; + + if(in_length > key_length - 2*HASH_LENGTH - 1) + throw Exception("EME1: Input is too large"); + + SecureVector<byte> out(key_length); + + out.clear(); + + rng.randomize(out, HASH_LENGTH); + + out.copy(HASH_LENGTH, Phash, Phash.size()); + out[out.size() - in_length - 1] = 0x01; + out.copy(out.size() - in_length, in, in_length); + mgf->mask(out, HASH_LENGTH, out + HASH_LENGTH, out.size() - HASH_LENGTH); + mgf->mask(out + HASH_LENGTH, out.size() - HASH_LENGTH, out, HASH_LENGTH); + + return out; + } + +/* +* EME1 Unpad Operation +*/ +SecureVector<byte> EME1::unpad(const byte in[], u32bit in_length, + u32bit key_length) const + { + /* + Must be careful about error messages here; if an attacker can + distinguish them, it is easy to use the differences as an oracle to + find the secret key, as described in "A Chosen Ciphertext Attack on + RSA Optimal Asymmetric Encryption Padding (OAEP) as Standardized in + PKCS #1 v2.0", James Manger, Crypto 2001 + */ + + key_length /= 8; + if(in_length > key_length) + throw Decoding_Error("Invalid EME1 encoding"); + + SecureVector<byte> tmp(key_length); + tmp.copy(key_length - in_length, in, in_length); + + mgf->mask(tmp + HASH_LENGTH, tmp.size() - HASH_LENGTH, tmp, HASH_LENGTH); + mgf->mask(tmp, HASH_LENGTH, tmp + HASH_LENGTH, tmp.size() - HASH_LENGTH); + + for(u32bit j = 0; j != Phash.size(); ++j) + if(tmp[j+HASH_LENGTH] != Phash[j]) + throw Decoding_Error("Invalid EME1 encoding"); + + for(u32bit j = HASH_LENGTH + Phash.size(); j != tmp.size(); ++j) + { + if(tmp[j] && tmp[j] != 0x01) + throw Decoding_Error("Invalid EME1 encoding"); + if(tmp[j] && tmp[j] == 0x01) + { + SecureVector<byte> retval(tmp + j + 1, tmp.size() - j - 1); + return retval; + } + } + throw Decoding_Error("Invalid EME1 encoding"); + } + +/* +* Return the max input size for a given key size +*/ +u32bit EME1::maximum_input_size(u32bit keybits) const + { + if(keybits / 8 > 2*HASH_LENGTH + 1) + return ((keybits / 8) - 2*HASH_LENGTH - 1); + else + return 0; + } + +/* +* EME1 Constructor +*/ +EME1::EME1(HashFunction* hash, const std::string& P) : + HASH_LENGTH(hash->OUTPUT_LENGTH) + { + Phash = hash->process(P); + mgf = new MGF1(hash); + } + +} diff --git a/botan/src/pk_pad/eme1/eme1.h b/botan/src/pk_pad/eme1/eme1.h new file mode 100644 index 0000000..4df5c5f --- /dev/null +++ b/botan/src/pk_pad/eme1/eme1.h @@ -0,0 +1,45 @@ +/* +* EME1 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_EME1_H__ +#define BOTAN_EME1_H__ + +#include <botan/eme.h> +#include <botan/kdf.h> +#include <botan/hash.h> + +namespace Botan { + +/* +* EME1 +*/ +class BOTAN_DLL EME1 : public EME + { + public: + u32bit maximum_input_size(u32bit) const; + + /** + EME1 constructor. Hash will be deleted by ~EME1 (when mgf is deleted) + + P is an optional label. Normally empty. + */ + EME1(HashFunction* hash, const std::string& P = ""); + + ~EME1() { delete mgf; } + private: + SecureVector<byte> pad(const byte[], u32bit, u32bit, + RandomNumberGenerator&) const; + SecureVector<byte> unpad(const byte[], u32bit, u32bit) const; + + const u32bit HASH_LENGTH; + SecureVector<byte> Phash; + MGF* mgf; + }; + +} + +#endif diff --git a/botan/src/pk_pad/eme1/info.txt b/botan/src/pk_pad/eme1/info.txt new file mode 100644 index 0000000..2f61265 --- /dev/null +++ b/botan/src/pk_pad/eme1/info.txt @@ -0,0 +1,16 @@ +realname "EME1" + +define EME1 + +load_on auto + +<add> +eme1.h +eme1.cpp +</add> + +<requires> +hash +kdf +mgf1 +</requires> diff --git a/botan/src/pk_pad/eme_pkcs/eme_pkcs.cpp b/botan/src/pk_pad/eme_pkcs/eme_pkcs.cpp new file mode 100644 index 0000000..c2f9c91 --- /dev/null +++ b/botan/src/pk_pad/eme_pkcs/eme_pkcs.cpp @@ -0,0 +1,70 @@ +/* +* PKCS1 EME +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/eme_pkcs.h> + +namespace Botan { + +/* +* PKCS1 Pad Operation +*/ +SecureVector<byte> EME_PKCS1v15::pad(const byte in[], u32bit inlen, + u32bit olen, + RandomNumberGenerator& rng) const + { + olen /= 8; + + if(olen < 10) + throw Encoding_Error("PKCS1: Output space too small"); + if(inlen > olen - 10) + throw Encoding_Error("PKCS1: Input is too large"); + + SecureVector<byte> out(olen); + + out[0] = 0x02; + for(u32bit j = 1; j != olen - inlen - 1; ++j) + while(out[j] == 0) + out[j] = rng.next_byte(); + out.copy(olen - inlen, in, inlen); + + return out; + } + +/* +* PKCS1 Unpad Operation +*/ +SecureVector<byte> EME_PKCS1v15::unpad(const byte in[], u32bit inlen, + u32bit key_len) const + { + if(inlen != key_len / 8 || inlen < 10 || in[0] != 0x02) + throw Decoding_Error("PKCS1::unpad"); + + u32bit seperator = 0; + for(u32bit j = 0; j != inlen; ++j) + if(in[j] == 0) + { + seperator = j; + break; + } + if(seperator < 9) + throw Decoding_Error("PKCS1::unpad"); + + return SecureVector<byte>(in + seperator + 1, inlen - seperator - 1); + } + +/* +* Return the max input size for a given key size +*/ +u32bit EME_PKCS1v15::maximum_input_size(u32bit keybits) const + { + if(keybits / 8 > 10) + return ((keybits / 8) - 10); + else + return 0; + } + +} diff --git a/botan/src/pk_pad/eme_pkcs/eme_pkcs.h b/botan/src/pk_pad/eme_pkcs/eme_pkcs.h new file mode 100644 index 0000000..1aeedf5 --- /dev/null +++ b/botan/src/pk_pad/eme_pkcs/eme_pkcs.h @@ -0,0 +1,30 @@ +/* +* EME PKCS#1 v1.5 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_EME_PKCS1_H__ +#define BOTAN_EME_PKCS1_H__ + +#include <botan/eme.h> + +namespace Botan { + +/* +* EME_PKCS1v15 +*/ +class BOTAN_DLL EME_PKCS1v15 : public EME + { + public: + u32bit maximum_input_size(u32bit) const; + private: + SecureVector<byte> pad(const byte[], u32bit, u32bit, + RandomNumberGenerator&) const; + SecureVector<byte> unpad(const byte[], u32bit, u32bit) const; + }; + +} + +#endif diff --git a/botan/src/pk_pad/eme_pkcs/info.txt b/botan/src/pk_pad/eme_pkcs/info.txt new file mode 100644 index 0000000..88d9caf --- /dev/null +++ b/botan/src/pk_pad/eme_pkcs/info.txt @@ -0,0 +1,10 @@ +realname "PKCSv1 v1.5 EME" + +define EME_PKCS1v15 + +load_on auto + +<add> +eme_pkcs.h +eme_pkcs.cpp +</add> diff --git a/botan/src/pk_pad/emsa.h b/botan/src/pk_pad/emsa.h new file mode 100644 index 0000000..e2491e4 --- /dev/null +++ b/botan/src/pk_pad/emsa.h @@ -0,0 +1,36 @@ +/* +* EMSA Classes +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_PUBKEY_EMSA_H__ +#define BOTAN_PUBKEY_EMSA_H__ + +#include <botan/secmem.h> +#include <botan/rng.h> + +namespace Botan { + +/* +* Encoding Method for Signatures, Appendix +*/ +class BOTAN_DLL EMSA + { + public: + virtual void update(const byte[], u32bit) = 0; + virtual SecureVector<byte> raw_data() = 0; + + virtual SecureVector<byte> encoding_of(const MemoryRegion<byte>&, + u32bit, + RandomNumberGenerator& rng) = 0; + + virtual bool verify(const MemoryRegion<byte>&, const MemoryRegion<byte>&, + u32bit) throw() = 0; + virtual ~EMSA() {} + }; + +} + +#endif diff --git a/botan/src/pk_pad/emsa1/emsa1.cpp b/botan/src/pk_pad/emsa1/emsa1.cpp new file mode 100644 index 0000000..26d709c --- /dev/null +++ b/botan/src/pk_pad/emsa1/emsa1.cpp @@ -0,0 +1,105 @@ +/* +* EMSA1 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/emsa1.h> + +namespace Botan { + +namespace { + +SecureVector<byte> emsa1_encoding(const MemoryRegion<byte>& msg, + u32bit output_bits) + { + if(8*msg.size() <= output_bits) + return msg; + + u32bit shift = 8*msg.size() - output_bits; + + u32bit byte_shift = shift / 8, bit_shift = shift % 8; + SecureVector<byte> digest(msg.size() - byte_shift); + + for(u32bit j = 0; j != msg.size() - byte_shift; ++j) + digest[j] = msg[j]; + + if(bit_shift) + { + byte carry = 0; + for(u32bit j = 0; j != digest.size(); ++j) + { + byte temp = digest[j]; + digest[j] = (temp >> bit_shift) | carry; + carry = (temp << (8 - bit_shift)); + } + } + return digest; + } + +} + +/* +* EMSA1 Update Operation +*/ +void EMSA1::update(const byte input[], u32bit length) + { + hash->update(input, length); + } + +/* +* Return the raw (unencoded) data +*/ +SecureVector<byte> EMSA1::raw_data() + { + return hash->final(); + } + +/* +* EMSA1 Encode Operation +*/ +SecureVector<byte> EMSA1::encoding_of(const MemoryRegion<byte>& msg, + u32bit output_bits, + RandomNumberGenerator&) + { + if(msg.size() != hash->OUTPUT_LENGTH) + throw Encoding_Error("EMSA1::encoding_of: Invalid size for input"); + return emsa1_encoding(msg, output_bits); + } + +/* +* EMSA1 Decode/Verify Operation +*/ +bool EMSA1::verify(const MemoryRegion<byte>& coded, + const MemoryRegion<byte>& raw, u32bit key_bits) throw() + { + try { + if(raw.size() != hash->OUTPUT_LENGTH) + throw Encoding_Error("EMSA1::encoding_of: Invalid size for input"); + + SecureVector<byte> our_coding = emsa1_encoding(raw, key_bits); + + if(our_coding == coded) return true; + if(our_coding[0] != 0) return false; + if(our_coding.size() <= coded.size()) return false; + + u32bit offset = 0; + while(our_coding[offset] == 0 && offset < our_coding.size()) + ++offset; + if(our_coding.size() - offset != coded.size()) + return false; + + for(u32bit j = 0; j != coded.size(); ++j) + if(coded[j] != our_coding[j+offset]) + return false; + + return true; + } + catch(Invalid_Argument) + { + return false; + } + } + +} diff --git a/botan/src/pk_pad/emsa1/emsa1.h b/botan/src/pk_pad/emsa1/emsa1.h new file mode 100644 index 0000000..a5dac07 --- /dev/null +++ b/botan/src/pk_pad/emsa1/emsa1.h @@ -0,0 +1,41 @@ +/* +* EMSA1 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_EMSA1_H__ +#define BOTAN_EMSA1_H__ + +#include <botan/emsa.h> +#include <botan/hash.h> + +namespace Botan { + +/* +* EMSA1 +*/ +class BOTAN_DLL EMSA1 : public EMSA + { + public: + EMSA1(HashFunction* h) : hash(h) {} + ~EMSA1() { delete hash; } + protected: + const HashFunction* hash_ptr() const { return hash; } + private: + void update(const byte[], u32bit); + SecureVector<byte> raw_data(); + + SecureVector<byte> encoding_of(const MemoryRegion<byte>&, u32bit, + RandomNumberGenerator& rng); + + bool verify(const MemoryRegion<byte>&, const MemoryRegion<byte>&, + u32bit) throw(); + + HashFunction* hash; + }; + +} + +#endif diff --git a/botan/src/pk_pad/emsa1/info.txt b/botan/src/pk_pad/emsa1/info.txt new file mode 100644 index 0000000..086270b --- /dev/null +++ b/botan/src/pk_pad/emsa1/info.txt @@ -0,0 +1,14 @@ +realname "EMSA1" + +define EMSA1 + +load_on auto + +<add> +emsa1.h +emsa1.cpp +</add> + +<requires> +hash +</requires> diff --git a/botan/src/pk_pad/emsa1_bsi/emsa1_bsi.cpp b/botan/src/pk_pad/emsa1_bsi/emsa1_bsi.cpp new file mode 100644 index 0000000..212091e --- /dev/null +++ b/botan/src/pk_pad/emsa1_bsi/emsa1_bsi.cpp @@ -0,0 +1,29 @@ +/* +* EMSA1 BSI +* (C) 1999-2008 Jack Lloyd +* 2008 Falko Strenzke, FlexSecure GmbH +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/emsa1_bsi.h> + +namespace Botan { + +/* +* EMSA1 BSI Encode Operation +*/ +SecureVector<byte> EMSA1_BSI::encoding_of(const MemoryRegion<byte>& msg, + u32bit output_bits, + RandomNumberGenerator&) + { + if(msg.size() != hash_ptr()->OUTPUT_LENGTH) + throw Encoding_Error("EMSA1_BSI::encoding_of: Invalid size for input"); + + if(8*msg.size() <= output_bits) + return msg; + + throw Encoding_Error("EMSA1_BSI::encoding_of: max key input size exceeded"); + } + +} diff --git a/botan/src/pk_pad/emsa1_bsi/emsa1_bsi.h b/botan/src/pk_pad/emsa1_bsi/emsa1_bsi.h new file mode 100644 index 0000000..ec86d40 --- /dev/null +++ b/botan/src/pk_pad/emsa1_bsi/emsa1_bsi.h @@ -0,0 +1,32 @@ +/* +* EMSA1 BSI Variant +* (C) 1999-2008 Jack Lloyd +* 2007 FlexSecure GmbH +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_EMSA1_BSI_H__ +#define BOTAN_EMSA1_BSI_H__ + +#include <botan/emsa1.h> + +namespace Botan { + +/** +EMSA1_BSI is a variant of EMSA1 specified by the BSI. It accepts only +hash values which are less or equal than the maximum key length. The +implementation comes from InSiTo +*/ +class BOTAN_DLL EMSA1_BSI : public EMSA1 + { + public: + EMSA1_BSI(HashFunction* hash) : EMSA1(hash) {} + private: + SecureVector<byte> encoding_of(const MemoryRegion<byte>&, u32bit, + RandomNumberGenerator& rng); + }; + +} + +#endif diff --git a/botan/src/pk_pad/emsa1_bsi/info.txt b/botan/src/pk_pad/emsa1_bsi/info.txt new file mode 100644 index 0000000..14a9fd3 --- /dev/null +++ b/botan/src/pk_pad/emsa1_bsi/info.txt @@ -0,0 +1,14 @@ +realname "EMSA1 (BSI variant)" + +define EMSA1_BSI + +load_on auto + +<add> +emsa1_bsi.h +emsa1_bsi.cpp +</add> + +<requires> +emsa1 +</requires> diff --git a/botan/src/pk_pad/emsa2/emsa2.cpp b/botan/src/pk_pad/emsa2/emsa2.cpp new file mode 100644 index 0000000..aee3231 --- /dev/null +++ b/botan/src/pk_pad/emsa2/emsa2.cpp @@ -0,0 +1,112 @@ +/* +* EMSA2 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/emsa2.h> +#include <botan/hash_id.h> + +namespace Botan { + +namespace { + +/* +* EMSA2 Encode Operation +*/ +SecureVector<byte> emsa2_encoding(const MemoryRegion<byte>& msg, + u32bit output_bits, + const MemoryRegion<byte>& empty_hash, + byte hash_id) + { + const u32bit HASH_SIZE = empty_hash.size(); + + u32bit output_length = (output_bits + 1) / 8; + + if(msg.size() != HASH_SIZE) + throw Encoding_Error("EMSA2::encoding_of: Bad input length"); + if(output_length < HASH_SIZE + 4) + throw Encoding_Error("EMSA2::encoding_of: Output length is too small"); + + bool empty = true; + for(u32bit j = 0; j != HASH_SIZE; ++j) + if(empty_hash[j] != msg[j]) + empty = false; + + SecureVector<byte> output(output_length); + + output[0] = (empty ? 0x4B : 0x6B); + output[output_length - 3 - HASH_SIZE] = 0xBA; + set_mem(output + 1, output_length - 4 - HASH_SIZE, 0xBB); + output.copy(output_length - (HASH_SIZE + 2), msg, msg.size()); + output[output_length-2] = hash_id; + output[output_length-1] = 0xCC; + + return output; + } + +} + +/* +* EMSA2 Update Operation +*/ +void EMSA2::update(const byte input[], u32bit length) + { + hash->update(input, length); + } + +/* +* Return the raw (unencoded) data +*/ +SecureVector<byte> EMSA2::raw_data() + { + return hash->final(); + } + +/* +* EMSA2 Encode Operation +*/ +SecureVector<byte> EMSA2::encoding_of(const MemoryRegion<byte>& msg, + u32bit output_bits, + RandomNumberGenerator&) + { + return emsa2_encoding(msg, output_bits, empty_hash, hash_id); + } + +/* +* EMSA2 Verify Operation +*/ +bool EMSA2::verify(const MemoryRegion<byte>& coded, + const MemoryRegion<byte>& raw, + u32bit key_bits) throw() + { + try + { + return (coded == emsa2_encoding(raw, key_bits, + empty_hash, hash_id)); + } + catch(...) + { + return false; + } + } + +/* +* EMSA2 Constructor +*/ +EMSA2::EMSA2(HashFunction* hash_in) : hash(hash_in) + { + empty_hash = hash->final(); + + hash_id = ieee1363_hash_id(hash->name()); + + if(hash_id == 0) + { + std::string hashname = hash->name(); + delete hash; + throw Encoding_Error("EMSA2 cannot be used with " + hashname); + } + } + +} diff --git a/botan/src/pk_pad/emsa2/emsa2.h b/botan/src/pk_pad/emsa2/emsa2.h new file mode 100644 index 0000000..76888d1 --- /dev/null +++ b/botan/src/pk_pad/emsa2/emsa2.h @@ -0,0 +1,41 @@ +/* +* EMSA2 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_EMSA2_H__ +#define BOTAN_EMSA2_H__ + +#include <botan/emsa.h> +#include <botan/hash.h> + +namespace Botan { + +/* +* EMSA2 +*/ +class BOTAN_DLL EMSA2 : public EMSA + { + public: + EMSA2(HashFunction* hash); + ~EMSA2() { delete hash; } + private: + void update(const byte[], u32bit); + SecureVector<byte> raw_data(); + + SecureVector<byte> encoding_of(const MemoryRegion<byte>&, u32bit, + RandomNumberGenerator& rng); + + bool verify(const MemoryRegion<byte>&, const MemoryRegion<byte>&, + u32bit) throw(); + + SecureVector<byte> empty_hash; + HashFunction* hash; + byte hash_id; + }; + +} + +#endif diff --git a/botan/src/pk_pad/emsa2/info.txt b/botan/src/pk_pad/emsa2/info.txt new file mode 100644 index 0000000..1c8161c --- /dev/null +++ b/botan/src/pk_pad/emsa2/info.txt @@ -0,0 +1,15 @@ +realname "EMSA2" + +define EMSA2 + +load_on auto + +<add> +emsa2.h +emsa2.cpp +</add> + +<requires> +hash +hash_id +</requires> diff --git a/botan/src/pk_pad/emsa3/emsa3.cpp b/botan/src/pk_pad/emsa3/emsa3.cpp new file mode 100644 index 0000000..4d50abd --- /dev/null +++ b/botan/src/pk_pad/emsa3/emsa3.cpp @@ -0,0 +1,152 @@ +/* +* EMSA3 and EMSA3_Raw +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/emsa3.h> +#include <botan/hash_id.h> + +namespace Botan { + +namespace { + +/** +* EMSA3 Encode Operation +*/ +SecureVector<byte> emsa3_encoding(const MemoryRegion<byte>& msg, + u32bit output_bits, + const byte hash_id[], + u32bit hash_id_length) + { + u32bit output_length = output_bits / 8; + if(output_length < hash_id_length + msg.size() + 10) + throw Encoding_Error("emsa3_encoding: Output length is too small"); + + SecureVector<byte> T(output_length); + const u32bit P_LENGTH = output_length - msg.size() - hash_id_length - 2; + + T[0] = 0x01; + set_mem(T+1, P_LENGTH, 0xFF); + T[P_LENGTH+1] = 0x00; + T.copy(P_LENGTH+2, hash_id, hash_id_length); + T.copy(output_length-msg.size(), msg, msg.size()); + return T; + } + +} + +/** +* EMSA3 Update Operation +*/ +void EMSA3::update(const byte input[], u32bit length) + { + hash->update(input, length); + } + +/** +* Return the raw (unencoded) data +*/ +SecureVector<byte> EMSA3::raw_data() + { + return hash->final(); + } + +/** +* EMSA3 Encode Operation +*/ +SecureVector<byte> EMSA3::encoding_of(const MemoryRegion<byte>& msg, + u32bit output_bits, + RandomNumberGenerator&) + { + if(msg.size() != hash->OUTPUT_LENGTH) + throw Encoding_Error("EMSA3::encoding_of: Bad input length"); + + return emsa3_encoding(msg, output_bits, + hash_id, hash_id.size()); + } + +/** +* Default signature decoding +*/ +bool EMSA3::verify(const MemoryRegion<byte>& coded, + const MemoryRegion<byte>& raw, + u32bit key_bits) throw() + { + if(raw.size() != hash->OUTPUT_LENGTH) + return false; + + try + { + return (coded == emsa3_encoding(raw, key_bits, + hash_id, hash_id.size())); + } + catch(...) + { + return false; + } + } + +/** +* EMSA3 Constructor +*/ +EMSA3::EMSA3(HashFunction* hash_in) : hash(hash_in) + { + hash_id = pkcs_hash_id(hash->name()); + } + +/** +* EMSA3 Destructor +*/ +EMSA3::~EMSA3() + { + delete hash; + } + +/** +* EMSA3_Raw Update Operation +*/ +void EMSA3_Raw::update(const byte input[], u32bit length) + { + message.append(input, length); + } + +/** +* Return the raw (unencoded) data +*/ +SecureVector<byte> EMSA3_Raw::raw_data() + { + SecureVector<byte> ret = message; + message.clear(); + return ret; + } + +/** +* EMSA3_Raw Encode Operation +*/ +SecureVector<byte> EMSA3_Raw::encoding_of(const MemoryRegion<byte>& msg, + u32bit output_bits, + RandomNumberGenerator&) + { + return emsa3_encoding(msg, output_bits, 0, 0); + } + +/** +* Default signature decoding +*/ +bool EMSA3_Raw::verify(const MemoryRegion<byte>& coded, + const MemoryRegion<byte>& raw, + u32bit key_bits) throw() + { + try + { + return (coded == emsa3_encoding(raw, key_bits, 0, 0)); + } + catch(...) + { + return false; + } + } + +} diff --git a/botan/src/pk_pad/emsa3/emsa3.h b/botan/src/pk_pad/emsa3/emsa3.h new file mode 100644 index 0000000..301f214 --- /dev/null +++ b/botan/src/pk_pad/emsa3/emsa3.h @@ -0,0 +1,65 @@ +/* +* EMSA3 and EMSA3_Raw +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_EMSA3_H__ +#define BOTAN_EMSA3_H__ + +#include <botan/emsa.h> +#include <botan/hash.h> + +namespace Botan { + +/** +* EMSA3 +* aka PKCS #1 v1.5 signature padding +* aka PKCS #1 block type 1 +*/ +class BOTAN_DLL EMSA3 : public EMSA + { + public: + EMSA3(HashFunction*); + ~EMSA3(); + + void update(const byte[], u32bit); + + SecureVector<byte> raw_data(); + + SecureVector<byte> encoding_of(const MemoryRegion<byte>&, u32bit, + RandomNumberGenerator& rng); + + bool verify(const MemoryRegion<byte>&, const MemoryRegion<byte>&, + u32bit) throw(); + private: + HashFunction* hash; + SecureVector<byte> hash_id; + }; + +/** +* EMSA3_Raw which is EMSA3 without a hash or digest id (which +* according to QCA docs is "identical to PKCS#11's CKM_RSA_PKCS +* mechanism", something I have not confirmed) +*/ +class BOTAN_DLL EMSA3_Raw : public EMSA + { + public: + void update(const byte[], u32bit); + + SecureVector<byte> raw_data(); + + SecureVector<byte> encoding_of(const MemoryRegion<byte>&, u32bit, + RandomNumberGenerator& rng); + + bool verify(const MemoryRegion<byte>&, const MemoryRegion<byte>&, + u32bit) throw(); + + private: + SecureVector<byte> message; + }; + +} + +#endif diff --git a/botan/src/pk_pad/emsa3/info.txt b/botan/src/pk_pad/emsa3/info.txt new file mode 100644 index 0000000..90e4b9b --- /dev/null +++ b/botan/src/pk_pad/emsa3/info.txt @@ -0,0 +1,15 @@ +realname "EMSA3" + +define EMSA3 + +load_on auto + +<add> +emsa3.h +emsa3.cpp +</add> + +<requires> +hash +hash_id +</requires> diff --git a/botan/src/pk_pad/emsa4/emsa4.cpp b/botan/src/pk_pad/emsa4/emsa4.cpp new file mode 100644 index 0000000..cff9a15 --- /dev/null +++ b/botan/src/pk_pad/emsa4/emsa4.cpp @@ -0,0 +1,143 @@ +/* +* EMSA4 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/emsa4.h> +#include <botan/mgf1.h> +#include <botan/bit_ops.h> + +namespace Botan { + +/* +* EMSA4 Update Operation +*/ +void EMSA4::update(const byte input[], u32bit length) + { + hash->update(input, length); + } + +/* +* Return the raw (unencoded) data +*/ +SecureVector<byte> EMSA4::raw_data() + { + return hash->final(); + } + +/* +* EMSA4 Encode Operation +*/ +SecureVector<byte> EMSA4::encoding_of(const MemoryRegion<byte>& msg, + u32bit output_bits, + RandomNumberGenerator& rng) + { + const u32bit HASH_SIZE = hash->OUTPUT_LENGTH; + + if(msg.size() != HASH_SIZE) + throw Encoding_Error("EMSA4::encoding_of: Bad input length"); + if(output_bits < 8*HASH_SIZE + 8*SALT_SIZE + 9) + throw Encoding_Error("EMSA4::encoding_of: Output length is too small"); + + const u32bit output_length = (output_bits + 7) / 8; + + SecureVector<byte> salt(SALT_SIZE); + rng.randomize(salt, SALT_SIZE); + + for(u32bit j = 0; j != 8; ++j) + hash->update(0); + hash->update(msg); + hash->update(salt, SALT_SIZE); + SecureVector<byte> H = hash->final(); + + SecureVector<byte> EM(output_length); + + EM[output_length - HASH_SIZE - SALT_SIZE - 2] = 0x01; + EM.copy(output_length - 1 - HASH_SIZE - SALT_SIZE, salt, SALT_SIZE); + mgf->mask(H, HASH_SIZE, EM, output_length - HASH_SIZE - 1); + EM[0] &= 0xFF >> (8 * ((output_bits + 7) / 8) - output_bits); + EM.copy(output_length - 1 - HASH_SIZE, H, HASH_SIZE); + EM[output_length-1] = 0xBC; + + return EM; + } + +/* +* EMSA4 Decode/Verify Operation +*/ +bool EMSA4::verify(const MemoryRegion<byte>& const_coded, + const MemoryRegion<byte>& raw, u32bit key_bits) throw() + { + const u32bit HASH_SIZE = hash->OUTPUT_LENGTH; + const u32bit KEY_BYTES = (key_bits + 7) / 8; + + if(key_bits < 8*HASH_SIZE + 9) + return false; + if(raw.size() != HASH_SIZE) + return false; + if(const_coded.size() > KEY_BYTES) + return false; + if(const_coded[const_coded.size()-1] != 0xBC) + return false; + + SecureVector<byte> coded = const_coded; + if(coded.size() < KEY_BYTES) + { + SecureVector<byte> temp(KEY_BYTES); + temp.copy(KEY_BYTES - coded.size(), coded, coded.size()); + coded = temp; + } + + const u32bit TOP_BITS = 8 * ((key_bits + 7) / 8) - key_bits; + if(TOP_BITS > 8 - high_bit(coded[0])) + return false; + + SecureVector<byte> DB(coded.begin(), coded.size() - HASH_SIZE - 1); + SecureVector<byte> H(coded + coded.size() - HASH_SIZE - 1, HASH_SIZE); + + mgf->mask(H, H.size(), DB, coded.size() - H.size() - 1); + DB[0] &= 0xFF >> TOP_BITS; + + u32bit salt_offset = 0; + for(u32bit j = 0; j != DB.size(); ++j) + { + if(DB[j] == 0x01) + { salt_offset = j + 1; break; } + if(DB[j]) + return false; + } + if(salt_offset == 0) + return false; + + SecureVector<byte> salt(DB + salt_offset, DB.size() - salt_offset); + + for(u32bit j = 0; j != 8; ++j) + hash->update(0); + hash->update(raw); + hash->update(salt); + SecureVector<byte> H2 = hash->final(); + + return (H == H2); + } + +/* +* EMSA4 Constructor +*/ +EMSA4::EMSA4(HashFunction* h) : + SALT_SIZE(h->OUTPUT_LENGTH), hash(h) + { + mgf = new MGF1(hash->clone()); + } + +/* +* EMSA4 Constructor +*/ +EMSA4::EMSA4(HashFunction* h, u32bit salt_size) : + SALT_SIZE(salt_size), hash(h) + { + mgf = new MGF1(hash->clone()); + } + +} diff --git a/botan/src/pk_pad/emsa4/emsa4.h b/botan/src/pk_pad/emsa4/emsa4.h new file mode 100644 index 0000000..b716178 --- /dev/null +++ b/botan/src/pk_pad/emsa4/emsa4.h @@ -0,0 +1,43 @@ +/* +* EMSA4 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_EMSA4_H__ +#define BOTAN_EMSA4_H__ + +#include <botan/emsa.h> +#include <botan/hash.h> +#include <botan/kdf.h> + +namespace Botan { + +/* +* EMSA4 +*/ +class BOTAN_DLL EMSA4 : public EMSA + { + public: + EMSA4(HashFunction*); + EMSA4(HashFunction*, u32bit); + + ~EMSA4() { delete hash; delete mgf; } + private: + void update(const byte[], u32bit); + SecureVector<byte> raw_data(); + + SecureVector<byte> encoding_of(const MemoryRegion<byte>&, u32bit, + RandomNumberGenerator& rng); + bool verify(const MemoryRegion<byte>&, const MemoryRegion<byte>&, + u32bit) throw(); + + u32bit SALT_SIZE; + HashFunction* hash; + const MGF* mgf; + }; + +} + +#endif diff --git a/botan/src/pk_pad/emsa4/info.txt b/botan/src/pk_pad/emsa4/info.txt new file mode 100644 index 0000000..29ef4e0 --- /dev/null +++ b/botan/src/pk_pad/emsa4/info.txt @@ -0,0 +1,16 @@ +realname "EMSA4" + +define EMSA4 + +load_on auto + +<add> +emsa4.h +emsa4.cpp +</add> + +<requires> +hash +kdf +mgf1 +</requires> diff --git a/botan/src/pk_pad/emsa_raw/emsa_raw.cpp b/botan/src/pk_pad/emsa_raw/emsa_raw.cpp new file mode 100644 index 0000000..d5973ee --- /dev/null +++ b/botan/src/pk_pad/emsa_raw/emsa_raw.cpp @@ -0,0 +1,50 @@ +/* +* EMSA-Raw +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/emsa_raw.h> + +namespace Botan { + +/* +* EMSA-Raw Encode Operation +*/ +void EMSA_Raw::update(const byte input[], u32bit length) + { + message.append(input, length); + } + +/* +* Return the raw (unencoded) data +*/ +SecureVector<byte> EMSA_Raw::raw_data() + { + SecureVector<byte> buf = message; + message.destroy(); + return buf; + } + +/* +* EMSA-Raw Encode Operation +*/ +SecureVector<byte> EMSA_Raw::encoding_of(const MemoryRegion<byte>& msg, + u32bit, + RandomNumberGenerator&) + { + return msg; + } + +/* +* EMSA-Raw Verify Operation +*/ +bool EMSA_Raw::verify(const MemoryRegion<byte>& coded, + const MemoryRegion<byte>& raw, + u32bit) throw() + { + return (coded == raw); + } + +} diff --git a/botan/src/pk_pad/emsa_raw/emsa_raw.h b/botan/src/pk_pad/emsa_raw/emsa_raw.h new file mode 100644 index 0000000..1b0ad51 --- /dev/null +++ b/botan/src/pk_pad/emsa_raw/emsa_raw.h @@ -0,0 +1,34 @@ +/* +* EMSA-Raw +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_EMSA_RAW_H__ +#define BOTAN_EMSA_RAW_H__ + +#include <botan/emsa.h> + +namespace Botan { + +/* +* EMSA-Raw +*/ +class BOTAN_DLL EMSA_Raw : public EMSA + { + private: + void update(const byte[], u32bit); + SecureVector<byte> raw_data(); + + SecureVector<byte> encoding_of(const MemoryRegion<byte>&, u32bit, + RandomNumberGenerator&); + bool verify(const MemoryRegion<byte>&, const MemoryRegion<byte>&, + u32bit) throw(); + + SecureVector<byte> message; + }; + +} + +#endif diff --git a/botan/src/pk_pad/emsa_raw/info.txt b/botan/src/pk_pad/emsa_raw/info.txt new file mode 100644 index 0000000..2a88d10 --- /dev/null +++ b/botan/src/pk_pad/emsa_raw/info.txt @@ -0,0 +1,10 @@ +realname "EMSA-Raw" + +define EMSA_RAW + +load_on auto + +<add> +emsa_raw.h +emsa_raw.cpp +</add> diff --git a/botan/src/pk_pad/hash_id/hash_id.cpp b/botan/src/pk_pad/hash_id/hash_id.cpp new file mode 100644 index 0000000..c83ad87 --- /dev/null +++ b/botan/src/pk_pad/hash_id/hash_id.cpp @@ -0,0 +1,116 @@ +/* +* Hash Function Identification +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/hash_id.h> +#include <botan/exceptn.h> + +namespace Botan { + +namespace PKCS_IDS { + +const byte MD2_ID[] = { +0x30, 0x20, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0x86, +0xF7, 0x0D, 0x02, 0x02, 0x05, 0x00, 0x04, 0x10 }; + +const byte MD5_ID[] = { +0x30, 0x20, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0x86, +0xF7, 0x0D, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 }; + +const byte RIPEMD_128_ID[] = { +0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x24, 0x03, 0x02, +0x02, 0x05, 0x00, 0x04, 0x14 }; + +const byte RIPEMD_160_ID[] = { +0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x24, 0x03, 0x02, +0x01, 0x05, 0x00, 0x04, 0x14 }; + +const byte SHA_160_ID[] = { +0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x0E, 0x03, 0x02, +0x1A, 0x05, 0x00, 0x04, 0x14 }; + +const byte SHA_224_ID[] = { +0x30, 0x2D, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, +0x65, 0x03, 0x04, 0x02, 0x04, 0x05, 0x00, 0x04, 0x1C }; + +const byte SHA_256_ID[] = { +0x30, 0x31, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, +0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20 }; + +const byte SHA_384_ID[] = { +0x30, 0x41, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, +0x65, 0x03, 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30 }; + +const byte SHA_512_ID[] = { +0x30, 0x51, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, +0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40 }; + +const byte TIGER_ID[] = { +0x30, 0x29, 0x30, 0x0D, 0x06, 0x09, 0x2B, 0x06, 0x01, 0x04, +0x01, 0xDA, 0x47, 0x0C, 0x02, 0x05, 0x00, 0x04, 0x18 }; + +} + +/** +* @return HashID as specified by PKCS +* For details see RFC 3447 section 9.2 +* http://tools.ietf.org/html/rfc3447#section-9.2 +*/ +MemoryVector<byte> pkcs_hash_id(const std::string& name) + { + MemoryVector<byte> out; + + if(name == "Parallel(MD5,SHA-160)") + return out; + + if(name == "MD2") + out.set(PKCS_IDS::MD2_ID, sizeof(PKCS_IDS::MD2_ID)); + else if(name == "MD5") + out.set(PKCS_IDS::MD5_ID, sizeof(PKCS_IDS::MD5_ID)); + else if(name == "RIPEMD-128") + out.set(PKCS_IDS::RIPEMD_128_ID, sizeof(PKCS_IDS::RIPEMD_128_ID)); + else if(name == "RIPEMD-160") + out.set(PKCS_IDS::RIPEMD_160_ID, sizeof(PKCS_IDS::RIPEMD_160_ID)); + else if(name == "SHA-160") + out.set(PKCS_IDS::SHA_160_ID, sizeof(PKCS_IDS::SHA_160_ID)); + else if(name == "SHA-224") + out.set(PKCS_IDS::SHA_224_ID, sizeof(PKCS_IDS::SHA_224_ID)); + else if(name == "SHA-256") + out.set(PKCS_IDS::SHA_256_ID, sizeof(PKCS_IDS::SHA_256_ID)); + else if(name == "SHA-384") + out.set(PKCS_IDS::SHA_384_ID, sizeof(PKCS_IDS::SHA_384_ID)); + else if(name == "SHA-512") + out.set(PKCS_IDS::SHA_512_ID, sizeof(PKCS_IDS::SHA_512_ID)); + else if(name == "Tiger(24,3)") + out.set(PKCS_IDS::TIGER_ID, sizeof(PKCS_IDS::TIGER_ID)); + + if(out.size()) + return out; + + throw Invalid_Argument("No PKCS #1 identifier for " + name); + } + +/** +* @return HashID as specified by IEEE 1363/X9.31 +*/ +byte ieee1363_hash_id(const std::string& name) + { + if(name == "SHA-160") return 0x33; + + if(name == "SHA-224") return 0x38; + if(name == "SHA-256") return 0x34; + if(name == "SHA-384") return 0x36; + if(name == "SHA-512") return 0x35; + + if(name == "RIPEMD-160") return 0x31; + if(name == "RIPEMD-128") return 0x32; + + if(name == "Whirlpool") return 0x37; + + return 0; + } + +} diff --git a/botan/src/pk_pad/hash_id/hash_id.h b/botan/src/pk_pad/hash_id/hash_id.h new file mode 100644 index 0000000..847d910 --- /dev/null +++ b/botan/src/pk_pad/hash_id/hash_id.h @@ -0,0 +1,24 @@ +/* +* Hash Function Identification +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_HASHID_H__ +#define BOTAN_HASHID_H__ + +#include <botan/secmem.h> +#include <string> + +namespace Botan { + +/* +* Return the values of various defined HashIDs +*/ +BOTAN_DLL MemoryVector<byte> pkcs_hash_id(const std::string&); +BOTAN_DLL byte ieee1363_hash_id(const std::string&); + +} + +#endif diff --git a/botan/src/pk_pad/hash_id/info.txt b/botan/src/pk_pad/hash_id/info.txt new file mode 100644 index 0000000..9354325 --- /dev/null +++ b/botan/src/pk_pad/hash_id/info.txt @@ -0,0 +1,14 @@ +realname "Hash Function Identifiers" + +define HASH_ID + +load_on auto + +<add> +hash_id.cpp +hash_id.h +</add> + +<requires> +alloc +</requires> diff --git a/botan/src/pk_pad/info.txt b/botan/src/pk_pad/info.txt new file mode 100644 index 0000000..c281b15 --- /dev/null +++ b/botan/src/pk_pad/info.txt @@ -0,0 +1,16 @@ +realname "Public Key EME/EMSA Padding Modes" + +define PK_PADDING + +load_on auto + +<add> +emsa.h +eme.cpp +eme.h +</add> + +<requires> +alloc +rng +</requires> diff --git a/botan/src/pubkey/dh/dh.cpp b/botan/src/pubkey/dh/dh.cpp new file mode 100644 index 0000000..0c9d02f --- /dev/null +++ b/botan/src/pubkey/dh/dh.cpp @@ -0,0 +1,119 @@ +/* +* Diffie-Hellman +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/dh.h> +#include <botan/numthry.h> +#include <botan/util.h> + +namespace Botan { + +/* +* DH_PublicKey Constructor +*/ +DH_PublicKey::DH_PublicKey(const DL_Group& grp, const BigInt& y1) + { + group = grp; + y = y1; + X509_load_hook(); + } + +/* +* Algorithm Specific X.509 Initialization Code +*/ +void DH_PublicKey::X509_load_hook() + { + } + +/* +* Return the maximum input size in bits +*/ +u32bit DH_PublicKey::max_input_bits() const + { + return group_p().bits(); + } + +/* +* Return the public value for key agreement +*/ +MemoryVector<byte> DH_PublicKey::public_value() const + { + return BigInt::encode_1363(y, group_p().bytes()); + } + +/* +* Create a DH private key +*/ +DH_PrivateKey::DH_PrivateKey(RandomNumberGenerator& rng, + const DL_Group& grp, + const BigInt& x_arg) + { + group = grp; + x = x_arg; + + if(x == 0) + { + const BigInt& p = group_p(); + x.randomize(rng, 2 * dl_work_factor(p.bits())); + PKCS8_load_hook(rng, true); + } + else + PKCS8_load_hook(rng, false); + } + +/* +* Algorithm Specific PKCS #8 Initialization Code +*/ +void DH_PrivateKey::PKCS8_load_hook(RandomNumberGenerator& rng, + bool generated) + { + if(y == 0) + y = power_mod(group_g(), x, group_p()); + core = DH_Core(rng, group, x); + + if(generated) + gen_check(rng); + else + load_check(rng); + } + +/* +* Return the public value for key agreement +*/ +MemoryVector<byte> DH_PrivateKey::public_value() const + { + return DH_PublicKey::public_value(); + } + +/* +* Derive a key +*/ +SecureVector<byte> DH_PrivateKey::derive_key(const byte w[], + u32bit w_len) const + { + return derive_key(BigInt::decode(w, w_len)); + } + +/* +* Derive a key +*/ +SecureVector<byte> DH_PrivateKey::derive_key(const DH_PublicKey& key) const + { + return derive_key(key.get_y()); + } + +/* +* Derive a key +*/ +SecureVector<byte> DH_PrivateKey::derive_key(const BigInt& w) const + { + const BigInt& p = group_p(); + if(w <= 1 || w >= p-1) + throw Invalid_Argument(algo_name() + "::derive_key: Invalid key input"); + return BigInt::encode_1363(core.agree(w), p.bytes()); + } + +} diff --git a/botan/src/pubkey/dh/dh.h b/botan/src/pubkey/dh/dh.h new file mode 100644 index 0000000..fa558bc --- /dev/null +++ b/botan/src/pubkey/dh/dh.h @@ -0,0 +1,80 @@ +/* +* Diffie-Hellman +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_DIFFIE_HELLMAN_H__ +#define BOTAN_DIFFIE_HELLMAN_H__ + +#include <botan/dl_algo.h> +#include <botan/dh_core.h> + +namespace Botan { + +/** +* This class represents Diffie-Hellman public keys. +*/ +class BOTAN_DLL DH_PublicKey : public virtual DL_Scheme_PublicKey + { + public: + std::string algo_name() const { return "DH"; } + + MemoryVector<byte> public_value() const; + u32bit max_input_bits() const; + + DL_Group::Format group_format() const { return DL_Group::ANSI_X9_42; } + + /** + * Construct an uninitialized key. Use this constructor if you wish + * to decode an encoded key into the new instance. + */ + DH_PublicKey() {} + + /** + * Construct a public key with the specified parameters. + * @param grp the DL group to use in the key + * @param y the public value y + */ + DH_PublicKey(const DL_Group& grp, const BigInt& y); + private: + void X509_load_hook(); + }; + +/** +* This class represents Diffie-Hellman private keys. +*/ +class BOTAN_DLL DH_PrivateKey : public DH_PublicKey, + public PK_Key_Agreement_Key, + public virtual DL_Scheme_PrivateKey + { + public: + SecureVector<byte> derive_key(const byte other[], u32bit length) const; + SecureVector<byte> derive_key(const DH_PublicKey& other) const; + SecureVector<byte> derive_key(const BigInt& other) const; + + MemoryVector<byte> public_value() const; + + /** + * Construct an uninitialized key. Use this constructor if you wish + * to decode an encoded key into the new instance. + */ + DH_PrivateKey() {} + + /** + * Construct a private key with predetermined value. + * @param rng random number generator to use + * @param grp the group to be used in the key + * @param x the key's secret value (or if zero, generate a new key) + */ + DH_PrivateKey(RandomNumberGenerator& rng, const DL_Group& grp, + const BigInt& x = 0); + private: + void PKCS8_load_hook(RandomNumberGenerator& rng, bool = false); + DH_Core core; + }; + +} + +#endif diff --git a/botan/src/pubkey/dh/dh_core.cpp b/botan/src/pubkey/dh/dh_core.cpp new file mode 100644 index 0000000..78a26a8 --- /dev/null +++ b/botan/src/pubkey/dh/dh_core.cpp @@ -0,0 +1,69 @@ +/* +* PK Algorithm Core +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/dh_core.h> +#include <botan/numthry.h> +#include <botan/pk_engine.h> +#include <botan/parsing.h> +#include <algorithm> + +namespace Botan { + +namespace { + +const u32bit BLINDING_BITS = BOTAN_PRIVATE_KEY_OP_BLINDING_BITS; + +} + +/* +* DH_Core Constructor +*/ +DH_Core::DH_Core(RandomNumberGenerator& rng, + const DL_Group& group, const BigInt& x) + { + op = Engine_Core::dh_op(group, x); + + const BigInt& p = group.get_p(); + + BigInt k(rng, std::min(p.bits()-1, BLINDING_BITS)); + + if(k != 0) + blinder = Blinder(k, power_mod(inverse_mod(k, p), x, p), p); + } + +/* +* DH_Core Copy Constructor +*/ +DH_Core::DH_Core(const DH_Core& core) + { + op = 0; + if(core.op) + op = core.op->clone(); + blinder = core.blinder; + } + +/* +* DH_Core Assignment Operator +*/ +DH_Core& DH_Core::operator=(const DH_Core& core) + { + delete op; + if(core.op) + op = core.op->clone(); + blinder = core.blinder; + return (*this); + } + +/* +* DH Operation +*/ +BigInt DH_Core::agree(const BigInt& i) const + { + return blinder.unblind(op->agree(blinder.blind(i))); + } + +} diff --git a/botan/src/pubkey/dh/dh_core.h b/botan/src/pubkey/dh/dh_core.h new file mode 100644 index 0000000..91b50a2 --- /dev/null +++ b/botan/src/pubkey/dh/dh_core.h @@ -0,0 +1,38 @@ +/* +* DH Core +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_DH_CORE_H__ +#define BOTAN_DH_CORE_H__ + +#include <botan/dh_op.h> +#include <botan/blinding.h> + +namespace Botan { + +/* +* DH Core +*/ +class BOTAN_DLL DH_Core + { + public: + BigInt agree(const BigInt&) const; + + DH_Core& operator=(const DH_Core&); + + DH_Core() { op = 0; } + DH_Core(const DH_Core&); + DH_Core(RandomNumberGenerator& rng, + const DL_Group&, const BigInt&); + ~DH_Core() { delete op; } + private: + DH_Operation* op; + Blinder blinder; + }; + +} + +#endif diff --git a/botan/src/pubkey/dh/dh_op.h b/botan/src/pubkey/dh/dh_op.h new file mode 100644 index 0000000..50f3d78 --- /dev/null +++ b/botan/src/pubkey/dh/dh_op.h @@ -0,0 +1,45 @@ +/* +* DH Operations +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_DH_OPS_H__ +#define BOTAN_DH_OPS_H__ + +#include <botan/dl_group.h> +#include <botan/reducer.h> +#include <botan/pow_mod.h> + +namespace Botan { + +/* +* DH Operation Interface +*/ +class BOTAN_DLL DH_Operation + { + public: + virtual BigInt agree(const BigInt&) const = 0; + virtual DH_Operation* clone() const = 0; + virtual ~DH_Operation() {} + }; + +/* +* Botan's Default DH Operation +*/ +class BOTAN_DLL Default_DH_Op : public DH_Operation + { + public: + BigInt agree(const BigInt& i) const { return powermod_x_p(i); } + DH_Operation* clone() const { return new Default_DH_Op(*this); } + + Default_DH_Op(const DL_Group& group, const BigInt& x) : + powermod_x_p(x, group.get_p()) {} + private: + Fixed_Exponent_Power_Mod powermod_x_p; + }; + +} + +#endif diff --git a/botan/src/pubkey/dh/info.txt b/botan/src/pubkey/dh/info.txt new file mode 100644 index 0000000..33af9a8 --- /dev/null +++ b/botan/src/pubkey/dh/info.txt @@ -0,0 +1,20 @@ +realname "Diffie-Hellman Key Agreement" + +define DIFFIE_HELLMAN + +load_on auto + +<add> +dh.cpp +dh.h +dh_core.cpp +dh_core.h +dh_op.h +</add> + +<requires> +dl_algo +dl_group +libstate +numbertheory +</requires> diff --git a/botan/src/pubkey/dl_algo/dl_algo.cpp b/botan/src/pubkey/dl_algo/dl_algo.cpp new file mode 100644 index 0000000..8ce3446 --- /dev/null +++ b/botan/src/pubkey/dl_algo/dl_algo.cpp @@ -0,0 +1,167 @@ +/* +* DL Scheme +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/dl_algo.h> +#include <botan/numthry.h> +#include <botan/der_enc.h> +#include <botan/ber_dec.h> + +namespace Botan { + +/* +* Return the X.509 public key encoder +*/ +X509_Encoder* DL_Scheme_PublicKey::x509_encoder() const + { + class DL_Scheme_Encoder : public X509_Encoder + { + public: + AlgorithmIdentifier alg_id() const + { + MemoryVector<byte> group = + key->group.DER_encode(key->group_format()); + + return AlgorithmIdentifier(key->get_oid(), group); + } + + MemoryVector<byte> key_bits() const + { + return DER_Encoder().encode(key->y).get_contents(); + } + + DL_Scheme_Encoder(const DL_Scheme_PublicKey* k) : key(k) {} + private: + const DL_Scheme_PublicKey* key; + }; + + return new DL_Scheme_Encoder(this); + } + +/* +* Return the X.509 public key decoder +*/ +X509_Decoder* DL_Scheme_PublicKey::x509_decoder() + { + class DL_Scheme_Decoder : public X509_Decoder + { + public: + void alg_id(const AlgorithmIdentifier& alg_id) + { + DataSource_Memory source(alg_id.parameters); + key->group.BER_decode(source, key->group_format()); + } + + void key_bits(const MemoryRegion<byte>& bits) + { + BER_Decoder(bits).decode(key->y); + key->X509_load_hook(); + } + + DL_Scheme_Decoder(DL_Scheme_PublicKey* k) : key(k) {} + private: + DL_Scheme_PublicKey* key; + }; + + return new DL_Scheme_Decoder(this); + } + +/* +* Return the PKCS #8 private key encoder +*/ +PKCS8_Encoder* DL_Scheme_PrivateKey::pkcs8_encoder() const + { + class DL_Scheme_Encoder : public PKCS8_Encoder + { + public: + AlgorithmIdentifier alg_id() const + { + MemoryVector<byte> group = + key->group.DER_encode(key->group_format()); + + return AlgorithmIdentifier(key->get_oid(), group); + } + + MemoryVector<byte> key_bits() const + { + return DER_Encoder().encode(key->x).get_contents(); + } + + DL_Scheme_Encoder(const DL_Scheme_PrivateKey* k) : key(k) {} + private: + const DL_Scheme_PrivateKey* key; + }; + + return new DL_Scheme_Encoder(this); + } + +/* +* Return the PKCS #8 private key decoder +*/ +PKCS8_Decoder* DL_Scheme_PrivateKey::pkcs8_decoder(RandomNumberGenerator& rng) + { + class DL_Scheme_Decoder : public PKCS8_Decoder + { + public: + void alg_id(const AlgorithmIdentifier& alg_id) + { + DataSource_Memory source(alg_id.parameters); + key->group.BER_decode(source, key->group_format()); + } + + void key_bits(const MemoryRegion<byte>& bits) + { + BER_Decoder(bits).decode(key->x); + key->PKCS8_load_hook(rng); + } + + DL_Scheme_Decoder(DL_Scheme_PrivateKey* k, RandomNumberGenerator& r) : + key(k), rng(r) {} + private: + DL_Scheme_PrivateKey* key; + RandomNumberGenerator& rng; + }; + + return new DL_Scheme_Decoder(this, rng); + } + +/* +* Check Public DL Parameters +*/ +bool DL_Scheme_PublicKey::check_key(RandomNumberGenerator& rng, + bool strong) const + { + if(y < 2 || y >= group_p()) + return false; + if(!group.verify_group(rng, strong)) + return false; + return true; + } + +/* +* Check DL Scheme Private Parameters +*/ +bool DL_Scheme_PrivateKey::check_key(RandomNumberGenerator& rng, + bool strong) const + { + const BigInt& p = group_p(); + const BigInt& g = group_g(); + + if(y < 2 || y >= p || x < 2 || x >= p) + return false; + if(!group.verify_group(rng, strong)) + return false; + + if(!strong) + return true; + + if(y != power_mod(g, x, p)) + return false; + + return true; + } + +} diff --git a/botan/src/pubkey/dl_algo/dl_algo.h b/botan/src/pubkey/dl_algo/dl_algo.h new file mode 100644 index 0000000..256ce96 --- /dev/null +++ b/botan/src/pubkey/dl_algo/dl_algo.h @@ -0,0 +1,116 @@ +/* +* DL Scheme +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_DL_ALGO_H__ +#define BOTAN_DL_ALGO_H__ + +#include <botan/dl_group.h> +#include <botan/x509_key.h> +#include <botan/pkcs8.h> +#include <botan/rng.h> + +namespace Botan { + +/** +* This class represents discrete logarithm (DL) public keys. +*/ +class BOTAN_DLL DL_Scheme_PublicKey : public virtual Public_Key + { + public: + bool check_key(RandomNumberGenerator& rng, bool) const; + + /** + * Get the DL domain parameters of this key. + * @return the DL domain parameters of this key + */ + const DL_Group& get_domain() const { return group; } + + /** + * Get the public value y with y = g^x mod p where x is the secret key. + */ + const BigInt& get_y() const { return y; } + + /** + * Get the prime p of the underlying DL group. + * @return the prime p + */ + const BigInt& group_p() const { return group.get_p(); } + + /** + * Get the prime q of the underlying DL group. + * @return the prime q + */ + const BigInt& group_q() const { return group.get_q(); } + + /** + * Get the generator g of the underlying DL group. + * @return the generator g + */ + const BigInt& group_g() const { return group.get_g(); } + + /** + * Get the underlying groups encoding format. + * @return the encoding format + */ + virtual DL_Group::Format group_format() const = 0; + + /** + * Get an X509 encoder for this key. + * @return an encoder usable to encode this key. + */ + X509_Encoder* x509_encoder() const; + + /** + * Get an X509 decoder for this key. + * @return an decoder usable to decode a DL key and store the + * values in this instance. + */ + X509_Decoder* x509_decoder(); + protected: + BigInt y; + DL_Group group; + private: + virtual void X509_load_hook() {} + }; + +/** +* This class represents discrete logarithm (DL) private keys. +*/ +class BOTAN_DLL DL_Scheme_PrivateKey : public virtual DL_Scheme_PublicKey, + public virtual Private_Key + { + public: + bool check_key(RandomNumberGenerator& rng, bool) const; + + /** + * Get the secret key x. + * @return the secret key + */ + const BigInt& get_x() const { return x; } + + /** + * Get an PKCS#8 encoder for this key. + * @return an encoder usable to encode this key. + */ + PKCS8_Encoder* pkcs8_encoder() const; + + /** + * Get an PKCS#8 decoder for this key. + * @param rng the rng to use + * @return an decoder usable to decode a DL key and store the + * values in this instance. + */ + PKCS8_Decoder* pkcs8_decoder(RandomNumberGenerator& rng); + protected: + BigInt x; + private: + virtual void PKCS8_load_hook(RandomNumberGenerator&, bool = false) {} + }; + +} + +#endif diff --git a/botan/src/pubkey/dl_algo/info.txt b/botan/src/pubkey/dl_algo/info.txt new file mode 100644 index 0000000..15a7751 --- /dev/null +++ b/botan/src/pubkey/dl_algo/info.txt @@ -0,0 +1,18 @@ +realname "Discrete Logarithm PK Algorithms" + +define DL_PUBLIC_KEY_FAMILY + +load_on auto + +<add> +dl_algo.cpp +dl_algo.h +</add> + +<requires> +asn1 +dl_group +numbertheory +pk_codecs +rng +</requires> diff --git a/botan/src/pubkey/dl_group/dl_group.cpp b/botan/src/pubkey/dl_group/dl_group.cpp new file mode 100644 index 0000000..81c5d5e --- /dev/null +++ b/botan/src/pubkey/dl_group/dl_group.cpp @@ -0,0 +1,333 @@ +/* +* Discrete Logarithm Parameters +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/dl_group.h> +#include <botan/libstate.h> +#include <botan/parsing.h> +#include <botan/numthry.h> +#include <botan/der_enc.h> +#include <botan/ber_dec.h> +#include <botan/pipe.h> +#include <botan/util.h> +#include <botan/pem.h> + +namespace Botan { + +/* +* DL_Group Constructor +*/ +DL_Group::DL_Group() + { + initialized = false; + } + +/* +* DL_Group Constructor +*/ +DL_Group::DL_Group(const std::string& type) + { + std::string grp_contents = global_state().get("dl", type); + + if(grp_contents == "") + throw Invalid_Argument("DL_Group: Unknown group " + type); + + DataSource_Memory pem(grp_contents); + PEM_decode(pem); + } + +/* +* DL_Group Constructor +*/ +DL_Group::DL_Group(RandomNumberGenerator& rng, + PrimeType type, u32bit pbits, u32bit qbits) + { + if(pbits < 512) + throw Invalid_Argument("DL_Group: prime size " + to_string(pbits) + + " is too small"); + + if(type == Strong) + { + p = random_safe_prime(rng, pbits); + q = (p - 1) / 2; + g = 2; + } + else if(type == Prime_Subgroup || type == DSA_Kosherizer) + { + if(type == Prime_Subgroup) + { + if(!qbits) + qbits = 2 * dl_work_factor(pbits); + + q = random_prime(rng, qbits); + BigInt X; + while(p.bits() != pbits || !is_prime(p, rng)) + { + X.randomize(rng, pbits); + p = X - (X % (2*q) - 1); + } + } + else + { + qbits = qbits ? qbits : ((pbits == 1024) ? 160 : 256); + generate_dsa_primes(rng, + global_state().algorithm_factory(), + p, q, pbits, qbits); + } + + g = make_dsa_generator(p, q); + } + + initialized = true; + } + +/* +* DL_Group Constructor +*/ +DL_Group::DL_Group(RandomNumberGenerator& rng, + const MemoryRegion<byte>& seed, u32bit pbits, u32bit qbits) + { + if(!generate_dsa_primes(rng, + global_state().algorithm_factory(), + p, q, pbits, qbits, seed)) + throw Invalid_Argument("DL_Group: The seed given does not " + "generate a DSA group"); + + g = make_dsa_generator(p, q); + + initialized = true; + } + +/* +* DL_Group Constructor +*/ +DL_Group::DL_Group(const BigInt& p1, const BigInt& g1) + { + initialize(p1, 0, g1); + } + +/* +* DL_Group Constructor +*/ +DL_Group::DL_Group(const BigInt& p1, const BigInt& q1, const BigInt& g1) + { + initialize(p1, q1, g1); + } + +/* +* DL_Group Initializer +*/ +void DL_Group::initialize(const BigInt& p1, const BigInt& q1, const BigInt& g1) + { + if(p1 < 3) + throw Invalid_Argument("DL_Group: Prime invalid"); + if(g1 < 2 || g1 >= p1) + throw Invalid_Argument("DL_Group: Generator invalid"); + if(q1 < 0 || q1 >= p1) + throw Invalid_Argument("DL_Group: Subgroup invalid"); + + p = p1; + g = g1; + q = q1; + + initialized = true; + } + +/* +* Verify that the group has been set +*/ +void DL_Group::init_check() const + { + if(!initialized) + throw Invalid_State("DLP group cannot be used uninitialized"); + } + +/* +* Verify the parameters +*/ +bool DL_Group::verify_group(RandomNumberGenerator& rng, + bool strong) const + { + init_check(); + + if(g < 2 || p < 3 || q < 0) + return false; + if((q != 0) && ((p - 1) % q != 0)) + return false; + + if(!strong) + return true; + + if(!check_prime(p, rng)) + return false; + if((q > 0) && !check_prime(q, rng)) + return false; + return true; + } + +/* +* Return the prime +*/ +const BigInt& DL_Group::get_p() const + { + init_check(); + return p; + } + +/* +* Return the generator +*/ +const BigInt& DL_Group::get_g() const + { + init_check(); + return g; + } + +/* +* Return the subgroup +*/ +const BigInt& DL_Group::get_q() const + { + init_check(); + if(q == 0) + throw Format_Error("DLP group has no q prime specified"); + return q; + } + +/* +* DER encode the parameters +*/ +SecureVector<byte> DL_Group::DER_encode(Format format) const + { + init_check(); + + if((q == 0) && (format != PKCS_3)) + throw Encoding_Error("The ANSI DL parameter formats require a subgroup"); + + if(format == ANSI_X9_57) + { + return DER_Encoder() + .start_cons(SEQUENCE) + .encode(p) + .encode(q) + .encode(g) + .end_cons() + .get_contents(); + } + else if(format == ANSI_X9_42) + { + return DER_Encoder() + .start_cons(SEQUENCE) + .encode(p) + .encode(g) + .encode(q) + .end_cons() + .get_contents(); + } + else if(format == PKCS_3) + { + return DER_Encoder() + .start_cons(SEQUENCE) + .encode(p) + .encode(g) + .end_cons() + .get_contents(); + } + + throw Invalid_Argument("Unknown DL_Group encoding " + to_string(format)); + } + +/* +* PEM encode the parameters +*/ +std::string DL_Group::PEM_encode(Format format) const + { + SecureVector<byte> encoding = DER_encode(format); + if(format == PKCS_3) + return PEM_Code::encode(encoding, "DH PARAMETERS"); + else if(format == ANSI_X9_57) + return PEM_Code::encode(encoding, "DSA PARAMETERS"); + else if(format == ANSI_X9_42) + return PEM_Code::encode(encoding, "X942 DH PARAMETERS"); + else + throw Invalid_Argument("Unknown DL_Group encoding " + to_string(format)); + } + +/* +* Decode BER encoded parameters +*/ +void DL_Group::BER_decode(DataSource& source, Format format) + { + BigInt new_p, new_q, new_g; + + BER_Decoder decoder(source); + BER_Decoder ber = decoder.start_cons(SEQUENCE); + + if(format == ANSI_X9_57) + { + ber.decode(new_p) + .decode(new_q) + .decode(new_g) + .verify_end(); + } + else if(format == ANSI_X9_42) + { + ber.decode(new_p) + .decode(new_g) + .decode(new_q) + .discard_remaining(); + } + else if(format == PKCS_3) + { + ber.decode(new_p) + .decode(new_g) + .discard_remaining(); + } + else + throw Invalid_Argument("Unknown DL_Group encoding " + to_string(format)); + + initialize(new_p, new_q, new_g); + } + +/* +* Decode PEM encoded parameters +*/ +void DL_Group::PEM_decode(DataSource& source) + { + std::string label; + DataSource_Memory ber(PEM_Code::decode(source, label)); + + if(label == "DH PARAMETERS") + BER_decode(ber, PKCS_3); + else if(label == "DSA PARAMETERS") + BER_decode(ber, ANSI_X9_57); + else if(label == "X942 DH PARAMETERS") + BER_decode(ber, ANSI_X9_42); + else + throw Decoding_Error("DL_Group: Invalid PEM label " + label); + } + +/* +* Create a random DSA-style generator +*/ +BigInt DL_Group::make_dsa_generator(const BigInt& p, const BigInt& q) + { + BigInt g, e = (p - 1) / q; + + for(u32bit j = 0; j != PRIME_TABLE_SIZE; ++j) + { + g = power_mod(PRIMES[j], e, p); + if(g != 1) + break; + } + + if(g == 1) + throw Exception("DL_Group: Couldn't create a suitable generator"); + + return g; + } + +} diff --git a/botan/src/pubkey/dl_group/dl_group.h b/botan/src/pubkey/dl_group/dl_group.h new file mode 100644 index 0000000..a84a85f --- /dev/null +++ b/botan/src/pubkey/dl_group/dl_group.h @@ -0,0 +1,162 @@ +/* +* Discrete Logarithm Group +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_DL_PARAM_H__ +#define BOTAN_DL_PARAM_H__ + +#include <botan/bigint.h> +#include <botan/data_src.h> + +namespace Botan { + +/** +* This class represents discrete logarithm groups. It holds a prime p, +* a prime q = (p-1)/2 and g = x^((p-1)/q) mod p. +*/ +class BOTAN_DLL DL_Group + { + public: + /** + * Get the prime p. + * @return the prime p + */ + const BigInt& get_p() const; + + /** + * Get the prime q. + * @return the prime q + */ + const BigInt& get_q() const; + + /** + * Get the base g. + * @return the base g + */ + const BigInt& get_g() const; + + /** + * The DL group encoding format variants. + */ + enum Format { + ANSI_X9_42, + ANSI_X9_57, + PKCS_3, + + DSA_PARAMETERS = ANSI_X9_57, + DH_PARAMETERS = ANSI_X9_42, + X942_DH_PARAMETERS = ANSI_X9_42, + PKCS3_DH_PARAMETERS = PKCS_3 + }; + + /** + * Determine the prime creation for DL groups. + */ + enum PrimeType { Strong, Prime_Subgroup, DSA_Kosherizer }; + + /** + * Perform validity checks on the group. + * @param rng the rng to use + * @param strong whether to perform stronger by lengthier tests + * @return true if the object is consistent, false otherwise + */ + bool verify_group(RandomNumberGenerator& rng, bool strong) const; + + /** + * Encode this group into a string using PEM encoding. + * @param format the encoding format + * @return the string holding the PEM encoded group + */ + std::string PEM_encode(Format format) const; + + /** + * Encode this group into a string using DER encoding. + * @param format the encoding format + * @return the string holding the DER encoded group + */ + SecureVector<byte> DER_encode(Format format) const; + + /** + * Decode a DER/BER encoded group into this instance. + * @param src a DataSource providing the encoded group + * @param format the format of the encoded group + */ + void BER_decode(DataSource& src, Format format); + + /** + * Decode a PEM encoded group into this instance. + * @param src a DataSource providing the encoded group + */ + void PEM_decode(DataSource& src); + + /** + * Construct a DL group with uninitialized internal value. + * Use this constructor is you wish to set the groups values + * from a DER or PEM encoded group. + */ + DL_Group(); + + /** + * Construct a DL group that is registered in the configuration. + * @param name the name that is configured in the global configuration + * for the desired group. If no configuration file is specified, + * the default values from the file policy.cpp will be used. For instance, + * use "modp/ietf/768" as name. + */ + DL_Group(const std::string& name); + + /** + * Create a new group randomly. + * @param rng the random number generator to use + * @param type specifies how the creation of primes p and q shall + * be performed. If type=Strong, then p will be determined as a + * safe prime, and q will be chosen as (p-1)/2. If + * type=Prime_Subgroup and qbits = 0, then the size of q will be + * determined according to the estimated difficulty of the DL + * problem. If type=DSA_Kosherizer, DSA primes will be created. + * @param pbits the number of bits of p + * @param qbits the number of bits of q. Leave it as 0 to have + * the value determined according to pbits. + */ + DL_Group(RandomNumberGenerator& rng, PrimeType type, + u32bit pbits, u32bit qbits = 0); + + /** + * Create a DSA group with a given seed. + * @param rng the random number generator to use + * @param seed the seed to use to create the random primes + * @param pbits the desired bit size of the prime p + * @param qbits the desired bit size of the prime q. + */ + DL_Group(RandomNumberGenerator& rng, const MemoryRegion<byte>& seed, + u32bit pbits = 1024, u32bit qbits = 0); + + /** + * Create a DL group. The prime q will be determined according to p. + * @param p the prime p + * @param g the base g + */ + DL_Group(const BigInt& p, const BigInt& g); + + /** + * Create a DL group. + * @param p the prime p + * @param q the prime q + * @param g the base g + */ + DL_Group(const BigInt& p, const BigInt& q, const BigInt& g); + private: + static BigInt make_dsa_generator(const BigInt&, const BigInt&); + + void init_check() const; + void initialize(const BigInt&, const BigInt&, const BigInt&); + bool initialized; + BigInt p, q, g; + }; + +} + +#endif diff --git a/botan/src/pubkey/dl_group/info.txt b/botan/src/pubkey/dl_group/info.txt new file mode 100644 index 0000000..6b9884a --- /dev/null +++ b/botan/src/pubkey/dl_group/info.txt @@ -0,0 +1,19 @@ +realname "DL Group" + +load_on auto + +define DL_GROUP + +<add> +dl_group.cpp +dl_group.h +</add> + +<requires> +asn1 +bigint +filters +libstate +numbertheory +pem +</requires> diff --git a/botan/src/pubkey/dlies/dlies.cpp b/botan/src/pubkey/dlies/dlies.cpp new file mode 100644 index 0000000..c441ed1 --- /dev/null +++ b/botan/src/pubkey/dlies/dlies.cpp @@ -0,0 +1,137 @@ +/* +* DLIES +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/dlies.h> +#include <botan/look_pk.h> +#include <botan/xor_buf.h> + +namespace Botan { + +/* +* DLIES_Encryptor Constructor +*/ +DLIES_Encryptor::DLIES_Encryptor(const PK_Key_Agreement_Key& k, + KDF* kdf_obj, + MessageAuthenticationCode* mac_obj, + u32bit mac_kl) : + key(k), kdf(kdf_obj), mac(mac_obj), mac_keylen(mac_kl) + { + } + +DLIES_Encryptor::~DLIES_Encryptor() + { + delete kdf; + delete mac; + } + +/* +* DLIES Encryption +*/ +SecureVector<byte> DLIES_Encryptor::enc(const byte in[], u32bit length, + RandomNumberGenerator&) const + { + if(length > maximum_input_size()) + throw Invalid_Argument("DLIES: Plaintext too large"); + if(other_key.is_empty()) + throw Invalid_State("DLIES: The other key was never set"); + + MemoryVector<byte> v = key.public_value(); + + SecureVector<byte> out(v.size() + length + mac->OUTPUT_LENGTH); + out.copy(v, v.size()); + out.copy(v.size(), in, length); + + SecureVector<byte> vz(v, key.derive_key(other_key, other_key.size())); + + const u32bit K_LENGTH = length + mac_keylen; + OctetString K = kdf->derive_key(K_LENGTH, vz, vz.size()); + if(K.length() != K_LENGTH) + throw Encoding_Error("DLIES: KDF did not provide sufficient output"); + byte* C = out + v.size(); + + xor_buf(C, K.begin() + mac_keylen, length); + mac->set_key(K.begin(), mac_keylen); + + mac->update(C, length); + for(u32bit j = 0; j != 8; ++j) + mac->update(0); + + mac->final(C + length); + + return out; + } + +/* +* Set the other parties public key +*/ +void DLIES_Encryptor::set_other_key(const MemoryRegion<byte>& ok) + { + other_key = ok; + } + +/* +* Return the max size, in bytes, of a message +*/ +u32bit DLIES_Encryptor::maximum_input_size() const + { + return 32; + } + +/* +* DLIES_Decryptor Constructor +*/ +DLIES_Decryptor::DLIES_Decryptor(const PK_Key_Agreement_Key& k, + KDF* kdf_obj, + MessageAuthenticationCode* mac_obj, + u32bit mac_kl) : + key(k), kdf(kdf_obj), mac(mac_obj), mac_keylen(mac_kl) + { + } + +DLIES_Decryptor::~DLIES_Decryptor() + { + delete kdf; + delete mac; + } + +/* +* DLIES Decryption +*/ +SecureVector<byte> DLIES_Decryptor::dec(const byte msg[], u32bit length) const + { + const u32bit public_len = key.public_value().size(); + + if(length < public_len + mac->OUTPUT_LENGTH) + throw Decoding_Error("DLIES decryption: ciphertext is too short"); + + const u32bit CIPHER_LEN = length - public_len - mac->OUTPUT_LENGTH; + + SecureVector<byte> v(msg, public_len); + SecureVector<byte> C(msg + public_len, CIPHER_LEN); + SecureVector<byte> T(msg + public_len + CIPHER_LEN, mac->OUTPUT_LENGTH); + + SecureVector<byte> vz(v, key.derive_key(v, v.size())); + + const u32bit K_LENGTH = C.size() + mac_keylen; + OctetString K = kdf->derive_key(K_LENGTH, vz, vz.size()); + if(K.length() != K_LENGTH) + throw Encoding_Error("DLIES: KDF did not provide sufficient output"); + + mac->set_key(K.begin(), mac_keylen); + mac->update(C); + for(u32bit j = 0; j != 8; ++j) + mac->update(0); + SecureVector<byte> T2 = mac->final(); + if(T != T2) + throw Integrity_Failure("DLIES: message authentication failed"); + + xor_buf(C, K.begin() + mac_keylen, C.size()); + + return C; + } + +} diff --git a/botan/src/pubkey/dlies/dlies.h b/botan/src/pubkey/dlies/dlies.h new file mode 100644 index 0000000..88a22b9 --- /dev/null +++ b/botan/src/pubkey/dlies/dlies.h @@ -0,0 +1,69 @@ +/* +* DLIES +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_DLIES_H__ +#define BOTAN_DLIES_H__ + +#include <botan/pubkey.h> +#include <botan/mac.h> +#include <botan/kdf.h> + +namespace Botan { + +/* +* DLIES Encryption +*/ +class BOTAN_DLL DLIES_Encryptor : public PK_Encryptor + { + public: + DLIES_Encryptor(const PK_Key_Agreement_Key&, + KDF* kdf, + MessageAuthenticationCode* mac, + u32bit mac_key_len = 20); + + ~DLIES_Encryptor(); + + void set_other_key(const MemoryRegion<byte>&); + private: + SecureVector<byte> enc(const byte[], u32bit, + RandomNumberGenerator&) const; + u32bit maximum_input_size() const; + + const PK_Key_Agreement_Key& key; + SecureVector<byte> other_key; + + KDF* kdf; + MessageAuthenticationCode* mac; + u32bit mac_keylen; + }; + +/* +* DLIES Decryption +*/ +class BOTAN_DLL DLIES_Decryptor : public PK_Decryptor + { + public: + DLIES_Decryptor(const PK_Key_Agreement_Key&, + KDF* kdf, + MessageAuthenticationCode* mac, + u32bit mac_key_len = 20); + + ~DLIES_Decryptor(); + + private: + SecureVector<byte> dec(const byte[], u32bit) const; + + const PK_Key_Agreement_Key& key; + + KDF* kdf; + MessageAuthenticationCode* mac; + u32bit mac_keylen; + }; + +} + +#endif diff --git a/botan/src/pubkey/dlies/info.txt b/botan/src/pubkey/dlies/info.txt new file mode 100644 index 0000000..5138aaf --- /dev/null +++ b/botan/src/pubkey/dlies/info.txt @@ -0,0 +1,16 @@ +realname "DLIES" + +define DLIES + +load_on auto + +<add> +dlies.cpp +dlies.h +</add> + +<requires> +kdf +libstate +mac +</requires> diff --git a/botan/src/pubkey/dsa/dsa.cpp b/botan/src/pubkey/dsa/dsa.cpp new file mode 100644 index 0000000..b0688ae --- /dev/null +++ b/botan/src/pubkey/dsa/dsa.cpp @@ -0,0 +1,134 @@ +/* +* DSA +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/dsa.h> +#include <botan/numthry.h> +#include <botan/keypair.h> +#include <botan/look_pk.h> + +namespace Botan { + +/* +* DSA_PublicKey Constructor +*/ +DSA_PublicKey::DSA_PublicKey(const DL_Group& grp, const BigInt& y1) + { + group = grp; + y = y1; + X509_load_hook(); + } + +/* +* Algorithm Specific X.509 Initialization Code +*/ +void DSA_PublicKey::X509_load_hook() + { + core = DSA_Core(group, y); + } + +/* +* DSA Verification Function +*/ +bool DSA_PublicKey::verify(const byte msg[], u32bit msg_len, + const byte sig[], u32bit sig_len) const + { + return core.verify(msg, msg_len, sig, sig_len); + } + +/* +* Return the maximum input size in bits +*/ +u32bit DSA_PublicKey::max_input_bits() const + { + return group_q().bits(); + } + +/* +* Return the size of each portion of the sig +*/ +u32bit DSA_PublicKey::message_part_size() const + { + return group_q().bytes(); + } + +/* +* Create a DSA private key +*/ +DSA_PrivateKey::DSA_PrivateKey(RandomNumberGenerator& rng, + const DL_Group& grp, + const BigInt& x_arg) + { + group = grp; + x = x_arg; + + if(x == 0) + { + x = BigInt::random_integer(rng, 2, group_q() - 1); + PKCS8_load_hook(rng, true); + } + else + PKCS8_load_hook(rng, false); + } + +/* +* Algorithm Specific PKCS #8 Initialization Code +*/ +void DSA_PrivateKey::PKCS8_load_hook(RandomNumberGenerator& rng, + bool generated) + { + y = power_mod(group_g(), x, group_p()); + core = DSA_Core(group, y, x); + + if(generated) + gen_check(rng); + else + load_check(rng); + } + +/* +* DSA Signature Operation +*/ +SecureVector<byte> DSA_PrivateKey::sign(const byte in[], u32bit length, + RandomNumberGenerator& rng) const + { + const BigInt& q = group_q(); + + BigInt k; + do + k.randomize(rng, q.bits()); + while(k >= q); + + return core.sign(in, length, k); + } + +/* +* Check Private DSA Parameters +*/ +bool DSA_PrivateKey::check_key(RandomNumberGenerator& rng, bool strong) const + { + if(!DL_Scheme_PrivateKey::check_key(rng, strong) || x >= group_q()) + return false; + + if(!strong) + return true; + + try + { + KeyPair::check_key(rng, + get_pk_signer(*this, "EMSA1(SHA-1)"), + get_pk_verifier(*this, "EMSA1(SHA-1)") + ); + } + catch(Self_Test_Failure) + { + return false; + } + + return true; + } + +} diff --git a/botan/src/pubkey/dsa/dsa.h b/botan/src/pubkey/dsa/dsa.h new file mode 100644 index 0000000..4c9b708 --- /dev/null +++ b/botan/src/pubkey/dsa/dsa.h @@ -0,0 +1,62 @@ +/* +* DSA +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_DSA_H__ +#define BOTAN_DSA_H__ + +#include <botan/dl_algo.h> +#include <botan/dsa_core.h> + +namespace Botan { + +/* +* DSA Public Key +*/ +class BOTAN_DLL DSA_PublicKey : public PK_Verifying_wo_MR_Key, + public virtual DL_Scheme_PublicKey + { + public: + std::string algo_name() const { return "DSA"; } + + DL_Group::Format group_format() const { return DL_Group::ANSI_X9_57; } + u32bit message_parts() const { return 2; } + u32bit message_part_size() const; + + bool verify(const byte[], u32bit, const byte[], u32bit) const; + u32bit max_input_bits() const; + + DSA_PublicKey() {} + DSA_PublicKey(const DL_Group&, const BigInt&); + protected: + DSA_Core core; + private: + void X509_load_hook(); + }; + +/* +* DSA Private Key +*/ +class BOTAN_DLL DSA_PrivateKey : public DSA_PublicKey, + public PK_Signing_Key, + public virtual DL_Scheme_PrivateKey + { + public: + SecureVector<byte> sign(const byte[], u32bit, + RandomNumberGenerator& rng) const; + + bool check_key(RandomNumberGenerator& rng, bool) const; + + DSA_PrivateKey() {} + DSA_PrivateKey(RandomNumberGenerator&, const DL_Group&, + const BigInt& = 0); + private: + void PKCS8_load_hook(RandomNumberGenerator& rng, bool = false); + }; + +} + +#endif diff --git a/botan/src/pubkey/dsa/dsa_core.cpp b/botan/src/pubkey/dsa/dsa_core.cpp new file mode 100644 index 0000000..e5a23a5 --- /dev/null +++ b/botan/src/pubkey/dsa/dsa_core.cpp @@ -0,0 +1,63 @@ +/* +* DSA Core +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/dsa_core.h> +#include <botan/numthry.h> +#include <botan/pk_engine.h> +#include <botan/parsing.h> +#include <algorithm> + +namespace Botan { + +/* +* DSA_Core Constructor +*/ +DSA_Core::DSA_Core(const DL_Group& group, const BigInt& y, const BigInt& x) + { + op = Engine_Core::dsa_op(group, y, x); + } + +/* +* DSA_Core Copy Constructor +*/ +DSA_Core::DSA_Core(const DSA_Core& core) + { + op = 0; + if(core.op) + op = core.op->clone(); + } + +/* +* DSA_Core Assignment Operator +*/ +DSA_Core& DSA_Core::operator=(const DSA_Core& core) + { + delete op; + if(core.op) + op = core.op->clone(); + return (*this); + } + +/* +* DSA Verification Operation +*/ +bool DSA_Core::verify(const byte msg[], u32bit msg_length, + const byte sig[], u32bit sig_length) const + { + return op->verify(msg, msg_length, sig, sig_length); + } + +/* +* DSA Signature Operation +*/ +SecureVector<byte> DSA_Core::sign(const byte in[], u32bit length, + const BigInt& k) const + { + return op->sign(in, length, k); + } + +} diff --git a/botan/src/pubkey/dsa/dsa_core.h b/botan/src/pubkey/dsa/dsa_core.h new file mode 100644 index 0000000..8bb1621 --- /dev/null +++ b/botan/src/pubkey/dsa/dsa_core.h @@ -0,0 +1,37 @@ +/* +* DSA Core +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_DSA_CORE_H__ +#define BOTAN_DSA_CORE_H__ + +#include <botan/dsa_op.h> +#include <botan/dl_group.h> + +namespace Botan { + +/* +* DSA Core +*/ +class BOTAN_DLL DSA_Core + { + public: + SecureVector<byte> sign(const byte[], u32bit, const BigInt&) const; + bool verify(const byte[], u32bit, const byte[], u32bit) const; + + DSA_Core& operator=(const DSA_Core&); + + DSA_Core() { op = 0; } + DSA_Core(const DSA_Core&); + DSA_Core(const DL_Group&, const BigInt&, const BigInt& = 0); + ~DSA_Core() { delete op; } + private: + DSA_Operation* op; + }; + +} + +#endif diff --git a/botan/src/pubkey/dsa/dsa_op.cpp b/botan/src/pubkey/dsa/dsa_op.cpp new file mode 100644 index 0000000..5b92144 --- /dev/null +++ b/botan/src/pubkey/dsa/dsa_op.cpp @@ -0,0 +1,73 @@ +/* +* DSA Operations +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/dsa_op.h> + +namespace Botan { + +/* +* Default_DSA_Op Constructor +*/ +Default_DSA_Op::Default_DSA_Op(const DL_Group& grp, const BigInt& y1, + const BigInt& x1) : x(x1), y(y1), group(grp) + { + powermod_g_p = Fixed_Base_Power_Mod(group.get_g(), group.get_p()); + powermod_y_p = Fixed_Base_Power_Mod(y, group.get_p()); + mod_p = Modular_Reducer(group.get_p()); + mod_q = Modular_Reducer(group.get_q()); + } + +/* +* Default DSA Verify Operation +*/ +bool Default_DSA_Op::verify(const byte msg[], u32bit msg_len, + const byte sig[], u32bit sig_len) const + { + const BigInt& q = group.get_q(); + + if(sig_len != 2*q.bytes() || msg_len > q.bytes()) + return false; + + BigInt r(sig, q.bytes()); + BigInt s(sig + q.bytes(), q.bytes()); + BigInt i(msg, msg_len); + + if(r <= 0 || r >= q || s <= 0 || s >= q) + return false; + + s = inverse_mod(s, q); + s = mod_p.multiply(powermod_g_p(mod_q.multiply(s, i)), + powermod_y_p(mod_q.multiply(s, r))); + + return (mod_q.reduce(s) == r); + } + +/* +* Default DSA Sign Operation +*/ +SecureVector<byte> Default_DSA_Op::sign(const byte in[], u32bit length, + const BigInt& k) const + { + if(x == 0) + throw Internal_Error("Default_DSA_Op::sign: No private key"); + + const BigInt& q = group.get_q(); + BigInt i(in, length); + + BigInt r = mod_q.reduce(powermod_g_p(k)); + BigInt s = mod_q.multiply(inverse_mod(k, q), mul_add(x, r, i)); + + if(r.is_zero() || s.is_zero()) + throw Internal_Error("Default_DSA_Op::sign: r or s was zero"); + + SecureVector<byte> output(2*q.bytes()); + r.binary_encode(output + (output.size() / 2 - r.bytes())); + s.binary_encode(output + (output.size() - s.bytes())); + return output; + } + +} diff --git a/botan/src/pubkey/dsa/dsa_op.h b/botan/src/pubkey/dsa/dsa_op.h new file mode 100644 index 0000000..0b112c6 --- /dev/null +++ b/botan/src/pubkey/dsa/dsa_op.h @@ -0,0 +1,53 @@ +/* +* DSA Operations +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_DSA_OPS_H__ +#define BOTAN_DSA_OPS_H__ + +#include <botan/numthry.h> +#include <botan/pow_mod.h> +#include <botan/reducer.h> +#include <botan/dl_group.h> + +namespace Botan { + +/* +* DSA Operation +*/ +class BOTAN_DLL DSA_Operation + { + public: + virtual bool verify(const byte[], u32bit, + const byte[], u32bit) const = 0; + virtual SecureVector<byte> sign(const byte[], u32bit, + const BigInt&) const = 0; + virtual DSA_Operation* clone() const = 0; + virtual ~DSA_Operation() {} + }; + +/* +* Botan's Default DSA Operation +*/ +class BOTAN_DLL Default_DSA_Op : public DSA_Operation + { + public: + bool verify(const byte[], u32bit, const byte[], u32bit) const; + SecureVector<byte> sign(const byte[], u32bit, const BigInt&) const; + + DSA_Operation* clone() const { return new Default_DSA_Op(*this); } + + Default_DSA_Op(const DL_Group&, const BigInt&, const BigInt&); + private: + const BigInt x, y; + const DL_Group group; + Fixed_Base_Power_Mod powermod_g_p, powermod_y_p; + Modular_Reducer mod_p, mod_q; + }; + +} + +#endif diff --git a/botan/src/pubkey/dsa/info.txt b/botan/src/pubkey/dsa/info.txt new file mode 100644 index 0000000..c70e02d --- /dev/null +++ b/botan/src/pubkey/dsa/info.txt @@ -0,0 +1,22 @@ +realname "DSA" + +define DSA + +load_on auto + +<add> +dsa.cpp +dsa.h +dsa_core.cpp +dsa_core.h +dsa_op.cpp +dsa_op.h +</add> + +<requires> +dl_algo +dl_group +keypair +libstate +numbertheory +</requires> diff --git a/botan/src/pubkey/ec_dompar/ec_dompar.cpp b/botan/src/pubkey/ec_dompar/ec_dompar.cpp new file mode 100644 index 0000000..6cfcc06 --- /dev/null +++ b/botan/src/pubkey/ec_dompar/ec_dompar.cpp @@ -0,0 +1,573 @@ + +#include <botan/ec_dompar.h> +#include <botan/pubkey_enums.h> +#include <botan/parsing.h> +#include <botan/hex.h> +#include <botan/pipe.h> + +namespace Botan { + +namespace { + +std::vector<std::string> get_standard_domain_parameter(const std::string& oid) + { + // using a linear search here is pretty nasty... revisit + + /* SEC2 */ + + if(oid == "1.3.132.0.6") + { + /* secp112r1; source: Flexiprovider */ + std::vector<std::string> dom_par; + dom_par.push_back("0xdb7c2abf62e35e668076bead208b"); //p + dom_par.push_back("0xDB7C2ABF62E35E668076BEAD2088"); // a + dom_par.push_back("0x659EF8BA043916EEDE8911702B22"); // b + dom_par.push_back("0409487239995A5EE76B55F9C2F098A89CE5AF8724C0A23E0E0ff77500"); // G + dom_par.push_back("0xDB7C2ABF62E35E7628DFAC6561C5"); // order + dom_par.push_back("1"); // cofactor + + return dom_par; + } + + if(oid == "1.3.132.0.7") + { + /* secp112r2; source: Flexiprovider */ + std::vector<std::string> dom_par; + dom_par.push_back("0xdb7c2abf62e35e668076bead208b"); //p + dom_par.push_back("0x6127C24C05F38A0AAAF65C0EF02C"); // a + dom_par.push_back("0x51DEF1815DB5ED74FCC34C85D709"); // b + dom_par.push_back("044BA30AB5E892B4E1649DD0928643ADCD46F5882E3747DEF36E956E97"); // G + dom_par.push_back("0x36DF0AAFD8B8D7597CA10520D04B"); // order + dom_par.push_back("1"); // cofactor + + return dom_par; + } + + if(oid == "1.3.132.0.28") + { + /* secp128r1; source: Flexiprovider */ + std::vector<std::string> dom_par; + dom_par.push_back("0xfffffffdffffffffffffffffffffffff"); //p + dom_par.push_back("0xffffffFDffffffffffffffffffffffFC"); // a + dom_par.push_back("0xE87579C11079F43DD824993C2CEE5ED3"); // b + dom_par.push_back("04161ff7528B899B2D0C28607CA52C5B86CF5AC8395BAFEB13C02DA292DDED7A83"); // G + dom_par.push_back("0xffffffFE0000000075A30D1B9038A115"); // order + dom_par.push_back("1"); // cofactor + + return dom_par; + } + + if(oid == "1.3.132.0.29") + { + /* secp128r2; source: Flexiprovider */ + std::vector<std::string> dom_par; + dom_par.push_back("0xfffffffdffffffffffffffffffffffff"); //p + dom_par.push_back("0xD6031998D1B3BBFEBF59CC9BBff9AEE1"); // a + dom_par.push_back("0x5EEEFCA380D02919DC2C6558BB6D8A5D"); // b + dom_par.push_back("047B6AA5D85E572983E6FB32A7CDEBC14027B6916A894D3AEE7106FE805FC34B44"); // G + dom_par.push_back("0x3ffffffF7ffffffFBE0024720613B5A3"); // order + dom_par.push_back("4"); // cofactor + + return dom_par; + } + + if(oid == "1.3.132.0.9") + { + /* secp160k1; source: Flexiprovider */ + std::vector<std::string> dom_par; + dom_par.push_back("0xfffffffffffffffffffffffffffffffeffffac73"); //p + dom_par.push_back("0x0000000000000000000000000000000000000000"); // a + dom_par.push_back("0x0000000000000000000000000000000000000007"); // b + dom_par.push_back("043B4C382CE37AA192A4019E763036F4F5DD4D7EBB938CF935318FDCED6BC28286531733C3F03C4FEE"); // G + dom_par.push_back("0x0100000000000000000001B8FA16DFAB9ACA16B6B3"); // order + dom_par.push_back("1"); // cofactor + + return dom_par; + } + + if(oid == "1.3.132.0.30") + { + /* secp160r2; source: Flexiprovider */ + std::vector<std::string> dom_par; + dom_par.push_back("0xfffffffffffffffffffffffffffffffeffffac73"); //p + dom_par.push_back("0xffffffffffffffffffffffffffffffFEffffAC70"); // a + dom_par.push_back("0xB4E134D3FB59EB8BAB57274904664D5AF50388BA"); // b + dom_par.push_back("0452DCB034293A117E1F4ff11B30F7199D3144CE6DFEAffEF2E331F296E071FA0DF9982CFEA7D43F2E"); // G + dom_par.push_back("0x0100000000000000000000351EE786A818F3A1A16B"); // order + dom_par.push_back("1"); // cofactor + + return dom_par; + } + + if(oid == "1.3.132.0.31") + { + /* secp192k1; source: Flexiprovider */ + std::vector<std::string> dom_par; + dom_par.push_back("0xfffffffffffffffffffffffffffffffffffffffeffffee37"); //p + dom_par.push_back("0x000000000000000000000000000000000000000000000000"); // a + dom_par.push_back("0x000000000000000000000000000000000000000000000003"); // b + dom_par.push_back("04DB4ff10EC057E9AE26B07D0280B7F4341DA5D1B1EAE06C7D9B2F2F6D9C5628A7844163D015BE86344082AA88D95E2F9D"); // G + dom_par.push_back("0xffffffffffffffffffffffFE26F2FC170F69466A74DEFD8D"); // order + dom_par.push_back("1"); // cofactor + + return dom_par; + } + + if(oid == "1.3.132.0.32") + { + /* secp224k1; source: Flexiprovider */ + std::vector<std::string> dom_par; + dom_par.push_back("0xfffffffffffffffffffffffffffffffffffffffffffffffeffffe56d"); //p + dom_par.push_back("0x00000000000000000000000000000000000000000000000000000000"); // a + dom_par.push_back("0x00000000000000000000000000000000000000000000000000000005"); // b + dom_par.push_back("04A1455B334DF099DF30FC28A169A467E9E47075A90F7E650EB6B7A45C7E089FED7FBA344282CAFBD6F7E319F7C0B0BD59E2CA4BDB556D61A5"); // G + dom_par.push_back("0x010000000000000000000000000001DCE8D2EC6184CAF0A971769FB1F7"); // order + dom_par.push_back("1"); // cofactor + + return dom_par; + } + + if(oid == "1.3.132.0.33") + { + /* secp224r1; source: Flexiprovider */ + std::vector<std::string> dom_par; + dom_par.push_back("0xffffffffffffffffffffffffffffffff000000000000000000000001"); //p + dom_par.push_back("0xffffffffffffffffffffffffffffffFEffffffffffffffffffffffFE"); // a + dom_par.push_back("0xB4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355ffB4"); // b + dom_par.push_back("04B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34"); // G + dom_par.push_back("0xffffffffffffffffffffffffffff16A2E0B8F03E13DD29455C5C2A3D"); // order + dom_par.push_back("1"); // cofactor + + return dom_par; + } + + if(oid == "1.3.132.0.10") + { + /* secp256k1; source: Flexiprovider */ + std::vector<std::string> dom_par; + dom_par.push_back("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"); //p + dom_par.push_back("0x0000000000000000000000000000000000000000000000000000000000000000"); // a + dom_par.push_back("0x0000000000000000000000000000000000000000000000000000000000000007"); // b + dom_par.push_back("0479BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8"); // G + dom_par.push_back("0xffffffffffffffffffffffffffffffFEBAAEDCE6AF48A03BBFD25E8CD0364141"); // order + dom_par.push_back("1"); // cofactor + + return dom_par; + } + + if(oid == "1.3.132.0.34") + { + /* secp384r1; source: Flexiprovider */ + std::vector<std::string> dom_par; + dom_par.push_back("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000ffffffff"); //p + dom_par.push_back("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffFEffffffff0000000000000000ffffffFC"); // a + dom_par.push_back("0xB3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF"); // b + dom_par.push_back("04AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B9859F741E082542A385502F25DBF55296C3A545E3872760AB73617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147CE9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F"); // G + dom_par.push_back("0xffffffffffffffffffffffffffffffffffffffffffffffffC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973"); // order + dom_par.push_back("1"); // cofactor + + return dom_par; + } + + if(oid == "1.3.132.0.35") + { + /* secp521r1; source: Flexiprovider */ + std::vector<std::string> dom_par; + dom_par.push_back("0x01ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"); //p + dom_par.push_back("0x01ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffFC"); // a + dom_par.push_back("0x0051953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573DF883D2C34F1EF451FD46B503F00"); // b + dom_par.push_back("0400C6858E06B70404E9CD9E3ECB662395B4429C648139053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2ffA8DE3348B3C1856A429BF97E7E31C2E5BD66011839296A789A3BC0045C8A5FB42C7D1BD998F54449579B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C7086A272C24088BE94769FD16650"); // G + dom_par.push_back("0x01ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffFA51868783BF2F966B7FCC0148F709A5D03BB5C9B8899C47AEBB6FB71E91386409"); // order + dom_par.push_back("1"); // cofactor + + return dom_par; + } + + /* NIS */ + + if(oid == "1.3.6.1.4.1.8301.3.1.2.9.0.38") + { + /* NIST curve P-521; source: Flexiprovider */ + std::vector<std::string> dom_par; + dom_par.push_back("0x1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"); //p + dom_par.push_back("0x01ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffFC"); // a + dom_par.push_back("0x051953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573DF883D2C34F1EF451FD46B503F00"); // b + dom_par.push_back("0400C6858E06B70404E9CD9E3ECB662395B4429C648139053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2ffA8DE3348B3C1856A429BF97E7E31C2E5BD66011839296A789A3BC0045C8A5FB42C7D1BD998F54449579B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C7086A272C24088BE94769FD16650"); // G + dom_par.push_back("0x01ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffFA51868783BF2F966B7FCC0148F709A5D03BB5C9B8899C47AEBB6FB71E91386409"); // order + dom_par.push_back("1"); // cofactor + + return dom_par; + } + + /* BrainPool */ + + if(oid == "1.3.36.3.3.2.8.1.1.1") + { + /* brainpoolP160r1; source: Flexiprovider */ + std::vector<std::string> dom_par; + dom_par.push_back("0xE95E4A5F737059DC60DFC7AD95B3D8139515620F"); //p + dom_par.push_back("0x340E7BE2A280EB74E2BE61BADA745D97E8F7C300"); // a + dom_par.push_back("0x1E589A8595423412134FAA2DBDEC95C8D8675E58"); // b + dom_par.push_back("04BED5AF16EA3F6A4F62938C4631EB5AF7BDBCDBC31667CB477A1A8EC338F94741669C976316DA6321"); // G + dom_par.push_back("0xE95E4A5F737059DC60DF5991D45029409E60FC09"); // order + dom_par.push_back("1"); // cofactor + + return dom_par; + } + + if(oid == "1.3.36.3.3.2.8.1.1.3") + { + /* brainpoolP192r1; source: Flexiprovider */ + std::vector<std::string> dom_par; + dom_par.push_back("0xC302F41D932A36CDA7A3463093D18DB78FCE476DE1A86297"); //p + dom_par.push_back("0x6A91174076B1E0E19C39C031FE8685C1CAE040E5C69A28EF"); // a + dom_par.push_back("0x469A28EF7C28CCA3DC721D044F4496BCCA7EF4146FBF25C9"); // b + dom_par.push_back("04C0A0647EAAB6A48753B033C56CB0F0900A2F5C4853375FD614B690866ABD5BB88B5F4828C1490002E6773FA2FA299B8F"); // G + dom_par.push_back("0xC302F41D932A36CDA7A3462F9E9E916B5BE8F1029AC4ACC1"); // order + dom_par.push_back("1"); // cofactor + + return dom_par; + } + + if(oid == "1.3.36.3.3.2.8.1.1.5") + { + /* brainpoolP224r1; source: Flexiprovider */ + std::vector<std::string> dom_par; + dom_par.push_back("0xD7C134AA264366862A18302575D1D787B09F075797DA89F57EC8C0FF"); //p + dom_par.push_back("0x68A5E62CA9CE6C1C299803A6C1530B514E182AD8B0042A59CAD29F43"); // a + dom_par.push_back("0x2580F63CCFE44138870713B1A92369E33E2135D266DBB372386C400B"); // b + dom_par.push_back("040D9029AD2C7E5CF4340823B2A87DC68C9E4CE3174C1E6EFDEE12C07D58AA56F772C0726F24C6B89E4ECDAC24354B9E99CAA3F6D3761402CD"); // G + dom_par.push_back("0xD7C134AA264366862A18302575D0FB98D116BC4B6DDEBCA3A5A7939F"); // order + dom_par.push_back("1"); // cofactor + + return dom_par; + } + + if(oid == "1.3.36.3.3.2.8.1.1.7") + { + /* brainpoolP256r1; source: Flexiprovider */ + std::vector<std::string> dom_par; + dom_par.push_back("0xA9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377"); //p + dom_par.push_back("0x7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9"); // a + dom_par.push_back("0x26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6"); // b + dom_par.push_back("048BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997"); // G + dom_par.push_back("0xA9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7"); // order + dom_par.push_back("1"); // cofactor + + return dom_par; + } + + if(oid == "1.3.36.3.3.2.8.1.1.9") + { + /* brainpoolP320r1; source: Flexiprovider */ + std::vector<std::string> dom_par; + dom_par.push_back("0xD35E472036BC4FB7E13C785ED201E065F98FCFA6F6F40DEF4F92B9EC7893EC28FCD412B1F1B32E27"); //p + dom_par.push_back("0x3EE30B568FBAB0F883CCEBD46D3F3BB8A2A73513F5EB79DA66190EB085FFA9F492F375A97D860EB4"); // a + dom_par.push_back("0x520883949DFDBC42D3AD198640688A6FE13F41349554B49ACC31DCCD884539816F5EB4AC8FB1F1A6"); // b + dom_par.push_back("0443BD7E9AFB53D8B85289BCC48EE5BFE6F20137D10A087EB6E7871E2A10A599C710AF8D0D39E2061114FDD05545EC1CC8AB4093247F77275E0743FFED117182EAA9C77877AAAC6AC7D35245D1692E8EE1"); // G + dom_par.push_back("0xD35E472036BC4FB7E13C785ED201E065F98FCFA5B68F12A32D482EC7EE8658E98691555B44C59311"); // order + dom_par.push_back("1"); // cofactor + + return dom_par; + } + + if(oid == "1.3.36.3.3.2.8.1.1.11") + { + /* brainpoolP384r1; source: Flexiprovider */ + std::vector<std::string> dom_par; + dom_par.push_back("0x8CB91E82A3386D280F5D6F7E50E641DF152F7109ED5456B412B1DA197FB71123ACD3A729901D1A71874700133107EC53"); //p + dom_par.push_back("0x7BC382C63D8C150C3C72080ACE05AFA0C2BEA28E4FB22787139165EFBA91F90F8AA5814A503AD4EB04A8C7DD22CE2826"); // a + dom_par.push_back("0x4A8C7DD22CE28268B39B55416F0447C2FB77DE107DCD2A62E880EA53EEB62D57CB4390295DBC9943AB78696FA504C11"); // b + dom_par.push_back("041D1C64F068CF45FFA2A63A81B7C13F6B8847A3E77EF14FE3DB7FCAFE0CBD10E8E826E03436D646AAEF87B2E247D4AF1E8ABE1D7520F9C2A45CB1EB8E95CFD55262B70B29FEEC5864E19C054FF99129280E4646217791811142820341263C5315"); // G + dom_par.push_back("0x8CB91E82A3386D280F5D6F7E50E641DF152F7109ED5456B31F166E6CAC0425A7CF3AB6AF6B7FC3103B883202E9046565"); // order + dom_par.push_back("1"); // cofactor + + return dom_par; + } + + if(oid == "1.3.36.3.3.2.8.1.1.13") + { + /* brainpoolP512r1; source: Flexiprovider */ + std::vector<std::string> dom_par; + dom_par.push_back("0xAADD9DB8DBE9C48B3FD4E6AE33C9FC07CB308DB3B3C9D20ED6639CCA703308717D4D9B009BC66842AECDA12AE6A380E62881FF2F2D82C68528AA6056583A48F3"); //p + dom_par.push_back("0x7830A3318B603B89E2327145AC234CC594CBDD8D3DF91610A83441CAEA9863BC2DED5D5AA8253AA10A2EF1C98B9AC8B57F1117A72BF2C7B9E7C1AC4D77FC94CA"); // a + dom_par.push_back("0x3DF91610A83441CAEA9863BC2DED5D5AA8253AA10A2EF1C98B9AC8B57F1117A72BF2C7B9E7C1AC4D77FC94CADC083E67984050B75EBAE5DD2809BD638016F723"); // b + dom_par.push_back("0481AEE4BDD82ED9645A21322E9C4C6A9385ED9F70B5D916C1B43B62EEF4D0098EFF3B1F78E2D0D48D50D1687B93B97D5F7C6D5047406A5E688B352209BCB9F8227DDE385D566332ECC0EABFA9CF7822FDF209F70024A57B1AA000C55B881F8111B2DCDE494A5F485E5BCA4BD88A2763AED1CA2B2FA8F0540678CD1E0F3AD80892"); // G + dom_par.push_back("0xAADD9DB8DBE9C48B3FD4E6AE33C9FC07CB308DB3B3C9D20ED6639CCA70330870553E5C414CA92619418661197FAC10471DB1D381085DDADDB58796829CA90069"); // order + dom_par.push_back("1"); // cofactor + + return dom_par; + } + + if(oid == "1.3.132.0.8") + { + std::vector<std::string> dom_par; + dom_par.push_back("0xffffffffffffffffffffffffffffffff7fffffff"); //p + dom_par.push_back("0xffffffffffffffffffffffffffffffff7ffffffc"); // a + dom_par.push_back("0x1c97befc54bd7a8b65acf89f81d4d4adc565fa45"); // b + dom_par.push_back("024a96b5688ef573284664698968c38bb913cbfc82"); // G + dom_par.push_back("0x0100000000000000000001f4c8f927aed3ca752257"); // order + dom_par.push_back("1"); // cofactor + return dom_par; + } + + if(oid == "1.2.840.10045.3.1.1") // prime192v1 Flexiprovider + { + std::vector<std::string> dom_par; + dom_par.push_back("0xfffffffffffffffffffffffffffffffeffffffffffffffff"); //p + dom_par.push_back("0xfffffffffffffffffffffffffffffffefffffffffffffffc"); // a + dom_par.push_back("0x64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1"); // b + dom_par.push_back("03188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012"); // G + dom_par.push_back("0xffffffffffffffffffffffff99def836146bc9b1b4d22831"); // order + dom_par.push_back("1"); // cofactor + return dom_par; + } + + /* prime192v2; source: Flexiprovider */ + if(oid == "1.2.840.10045.3.1.2") + { + std::vector<std::string> dom_par; + dom_par.push_back("0xfffffffffffffffffffffffffffffffeffffffffffffffff"); //p + dom_par.push_back("0xffffffffffffffffffffffffffffffFeffffffffffffffFC"); // a + dom_par.push_back("0xcc22d6dfb95c6b25e49c0d6364a4e5980c393aa21668d953"); // b + dom_par.push_back("03eea2bae7e1497842f2de7769cfe9c989c072ad696f48034a"); // G + dom_par.push_back("0xfffffffffffffffffffffffe5fb1a724dc80418648d8dd31"); // order + dom_par.push_back("1"); // cofactor + return dom_par; + } + + /* prime192v3; source: Flexiprovider */ + if(oid == "1.2.840.10045.3.1.3") + { + std::vector<std::string> dom_par; + dom_par.push_back("0xfffffffffffffffffffffffffffffffeffffffffffffffff"); //p + dom_par.push_back("0xfffffffffffffffffffffffffffffffefffffffffffffffc"); // a + dom_par.push_back("0x22123dc2395a05caa7423daeccc94760a7d462256bd56916"); // b + dom_par.push_back("027d29778100c65a1da1783716588dce2b8b4aee8e228f1896"); // G + dom_par.push_back("0xffffffffffffffffffffffff7a62d031c83f4294f640ec13"); // order + dom_par.push_back("1"); // cofactor + return dom_par; + } + + /* prime239v1; source: Flexiprovider */ + if(oid == "1.2.840.10045.3.1.4") + { + std::vector<std::string> dom_par; + dom_par.push_back("0x7fffffffffffffffffffffff7fffffffffff8000000000007fffffffffff"); //p + dom_par.push_back("0x7ffFffffffffffffffffffff7fffffffffff8000000000007ffffffffffc"); // a + dom_par.push_back("0x6B016C3BDCF18941D0D654921475CA71A9DB2FB27D1D37796185C2942C0A"); // b + dom_par.push_back("020ffA963CDCA8816CCC33B8642BEDF905C3D358573D3F27FBBD3B3CB9AAAF"); // G + dom_par.push_back("0x7fffffffffffffffffffffff7fffff9e5e9a9f5d9071fbd1522688909d0b"); // order + dom_par.push_back("1"); // cofactor + return dom_par; + } + + /* prime239v2; source: Flexiprovider */ + if(oid == "1.2.840.10045.3.1.5") + { + std::vector<std::string> dom_par; + dom_par.push_back("0x7fffffffffffffffffffffff7fffffffffff8000000000007fffffffffff"); //p + dom_par.push_back("0x7ffFffffffffffffffffffff7ffFffffffff8000000000007ffFffffffFC"); // a + dom_par.push_back("0x617FAB6832576CBBFED50D99F0249C3FEE58B94BA0038C7AE84C8C832F2C"); // b + dom_par.push_back("0238AF09D98727705120C921BB5E9E26296A3CDCF2F35757A0EAFD87B830E7"); // G + dom_par.push_back("0x7fffffffffffffffffffffff800000CFA7E8594377D414C03821BC582063"); // order + dom_par.push_back("1"); // cofactor + return dom_par; + } + + /* prime239v3; source: Flexiprovider */ + if(oid == "1.2.840.10045.3.1.6") + { + std::vector<std::string> dom_par; + dom_par.push_back("0x7fffffffffffffffffffffff7fffffffffff8000000000007fffffffffff"); //p + dom_par.push_back("0x7ffFffffffffffffffffffff7ffFffffffff8000000000007ffFffffffFC"); // a + dom_par.push_back("0x255705FA2A306654B1F4CB03D6A750A30C250102D4988717D9BA15AB6D3E"); // b + dom_par.push_back("036768AE8E18BB92CFCF005C949AA2C6D94853D0E660BBF854B1C9505FE95A"); // G + dom_par.push_back("0x7fffffffffffffffffffffff7fffff975DEB41B3A6057C3C432146526551"); // order + dom_par.push_back("1"); // cofactor + return dom_par; + } + + /* prime256v1; source: Flexiprovider */ + if(oid == "1.2.840.10045.3.1.7") + { + std::vector<std::string> dom_par; + dom_par.push_back("0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff"); //p + dom_par.push_back("0xffffffff00000001000000000000000000000000ffffffffffffffffffffffFC"); // a + dom_par.push_back("0x5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B"); // b + dom_par.push_back("036B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296"); // G + dom_par.push_back("0xffffffff00000000ffffffffffffffffBCE6FAADA7179E84F3B9CAC2FC632551"); // order + dom_par.push_back("1"); // cofactor + return dom_par; + } + + throw Invalid_Argument("No such ECC curve " + oid); + } + +EC_Domain_Params get_ec_dompar(const std::string& oid) + { + std::vector<std::string> dom_par = get_standard_domain_parameter(oid); + + BigInt p(dom_par[0]); // give as 0x... + GFpElement a(p, BigInt(dom_par[1])); + GFpElement b(p, BigInt(dom_par[2])); + + Pipe pipe(new Hex_Decoder); + pipe.process_msg(dom_par[3]); + SecureVector<byte> sv_g = pipe.read_all(); + + CurveGFp curve(a, b, p); + PointGFp G = OS2ECP ( sv_g, curve ); + G.check_invariants(); + BigInt order(dom_par[4]); + BigInt cofactor(dom_par[5]); + EC_Domain_Params result(curve, G, order, cofactor); + return result; + } + +} + +EC_Domain_Params get_EC_Dom_Pars_by_oid(std::string oid) + { + EC_Domain_Params result = get_ec_dompar(oid); + result.m_oid = oid; + return result; + } + +EC_Domain_Params::EC_Domain_Params(const CurveGFp& curve, const PointGFp& base_point, + const BigInt& order, const BigInt& cofactor) + : m_curve(curve), + m_base_point(base_point), + m_order(order), + m_cofactor(cofactor), + m_oid("") + { } + +namespace { + +SecureVector<byte> encode_der_ec_dompar_explicit(EC_Domain_Params const& dom_pars) + { + u32bit ecpVers1 = 1; + OID curve_type_oid("1.2.840.10045.1.1"); + + DER_Encoder der; + + der.start_cons(SEQUENCE) + .encode(ecpVers1) + .start_cons(SEQUENCE) + .encode(curve_type_oid) + .encode(dom_pars.get_curve().get_p()) + .end_cons() + .start_cons(SEQUENCE) + .encode(FE2OSP ( dom_pars.get_curve().get_a() ), OCTET_STRING) + .encode(FE2OSP ( dom_pars.get_curve().get_b() ), OCTET_STRING) + .end_cons() + .encode(EC2OSP ( dom_pars.get_base_point(), PointGFp::UNCOMPRESSED), OCTET_STRING) + .encode(dom_pars.get_order()) + .encode(dom_pars.get_cofactor()) + .end_cons(); + + return der.get_contents(); + } + +EC_Domain_Params decode_ber_ec_dompar_explicit(SecureVector<byte> const& encoded) + { + BigInt ecpVers1(1); + OID curve_type_oid; + SecureVector<byte> sv_a; + SecureVector<byte> sv_b; + BigInt p; + SecureVector<byte> sv_base_point; + BigInt order; + BigInt cofactor; + BER_Decoder dec(encoded); + dec + .start_cons(SEQUENCE) + .decode(ecpVers1) + .start_cons(SEQUENCE) + .decode(curve_type_oid) + .decode(p) + .end_cons() + .start_cons(SEQUENCE) + .decode(sv_a, OCTET_STRING) + .decode(sv_b, OCTET_STRING) + .end_cons() + .decode(sv_base_point, OCTET_STRING) + .decode(order) + .decode(cofactor) + .verify_end() + .end_cons(); + if(ecpVers1 != 1) + { + throw Decoding_Error("wrong ecpVers"); + } + // Set the domain parameters + if(curve_type_oid.as_string() != "1.2.840.10045.1.1") // NOTE: hardcoded: prime field type + { + throw Decoding_Error("wrong curve type oid where prime field was expected"); + } + GFpElement a(p,BigInt::decode(sv_a, sv_a.size())); + GFpElement b(p,BigInt::decode(sv_b, sv_b.size())); + CurveGFp curve(a,b,p); + PointGFp G = OS2ECP ( sv_base_point, curve ); + G.check_invariants(); + return EC_Domain_Params(curve, G, order, cofactor); + } + +} // end anonymous namespace + +SecureVector<byte> encode_der_ec_dompar(EC_Domain_Params const& dom_pars, EC_dompar_enc enc_type) + { + SecureVector<byte> result; + + if(enc_type == ENC_EXPLICIT) + { + result = encode_der_ec_dompar_explicit(dom_pars); + } + else if(enc_type == ENC_OID) + { + OID dom_par_oid(dom_pars.get_oid()); + result = DER_Encoder().encode(dom_par_oid).get_contents(); + } + else if(enc_type == ENC_IMPLICITCA) + { + result = DER_Encoder().encode_null().get_contents(); + } + else + { + throw Internal_Error("encountered illegal value for ec parameter encoding type"); + } + return result; + } + +EC_Domain_Params decode_ber_ec_dompar(SecureVector<byte> const& encoded) + { + BER_Decoder dec(encoded); + BER_Object obj = dec.get_next_object(); + ASN1_Tag tag = obj.type_tag; + std::auto_ptr<EC_Domain_Params> p_result; + + if(tag == OBJECT_ID) + { + OID dom_par_oid; + BER_Decoder(encoded).decode(dom_par_oid); + return EC_Domain_Params(get_ec_dompar(dom_par_oid.as_string())); + } + else if(tag == SEQUENCE) + return EC_Domain_Params(decode_ber_ec_dompar_explicit(encoded)); + else if(tag == NULL_TAG) + throw Decoding_Error("cannot decode ECDSA parameters that are ImplicitCA"); + + throw Decoding_Error("encountered unexpected when trying to decode domain parameters"); + } + +bool operator==(EC_Domain_Params const& lhs, EC_Domain_Params const& rhs) + { + return ((lhs.get_curve() == rhs.get_curve()) && + (lhs.get_base_point() == rhs.get_base_point()) && + (lhs.get_order() == rhs.get_order()) && + (lhs.get_cofactor() == rhs.get_cofactor())); + } + +} + diff --git a/botan/src/pubkey/ec_dompar/ec_dompar.h b/botan/src/pubkey/ec_dompar/ec_dompar.h new file mode 100644 index 0000000..47971d8 --- /dev/null +++ b/botan/src/pubkey/ec_dompar/ec_dompar.h @@ -0,0 +1,121 @@ +/* +* ECDSA Domain Parameters +* (C) 2007 Falko Strenzke, FlexSecure GmbH +* 2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ECC_DOMAIN_PARAMETERS_H__ +#define BOTAN_ECC_DOMAIN_PARAMETERS_H__ + +#include <botan/point_gfp.h> +#include <botan/gfp_element.h> +#include <botan/curve_gfp.h> +#include <botan/bigint.h> +#include <botan/der_enc.h> +#include <botan/ber_dec.h> +#include <botan/alg_id.h> +#include <botan/pubkey_enums.h> + +namespace Botan { + +/** +* This class represents elliptic curce domain parameters +*/ +class BOTAN_DLL EC_Domain_Params + { + public: + + /** + * Construct Domain paramers from specified parameters + * @param curve elliptic curve + * @param base_point a base point + * @param order the order of the base point + * @param cofactor the cofactor + */ + EC_Domain_Params(const CurveGFp& curve, + const PointGFp& base_point, + const BigInt& order, + const BigInt& cofactor); + + /** + * Return domain parameter curve + * @result domain parameter curve + */ + const CurveGFp& get_curve() const + { + return m_curve; + } + + /** + * Return domain parameter curve + * @result domain parameter curve + */ + const PointGFp& get_base_point() const + { + return m_base_point; + } + + /** + * Return the order of the base point + * @result order of the base point + */ + const BigInt& get_order() const + { + return m_order; + } + + /** + * Return the cofactor + * @result the cofactor + */ + const BigInt& get_cofactor() const + { + return m_cofactor; + } + + /** + * Return the OID of these domain parameters + * @result the OID + */ + std::string get_oid() const { return m_oid; } + + private: + friend EC_Domain_Params get_EC_Dom_Pars_by_oid(std::string oid); + + CurveGFp m_curve; + PointGFp m_base_point; + BigInt m_order; + BigInt m_cofactor; + std::string m_oid; + }; + +bool operator==(EC_Domain_Params const& lhs, EC_Domain_Params const& rhs); + +inline bool operator!=(const EC_Domain_Params& lhs, + const EC_Domain_Params& rhs) + { + return !(lhs == rhs); + } + +enum EC_dompar_enc { ENC_EXPLICIT = 0, ENC_IMPLICITCA = 1, ENC_OID = 2 }; + +SecureVector<byte> encode_der_ec_dompar(EC_Domain_Params const& dom_pars, + EC_dompar_enc enc_type); + +EC_Domain_Params decode_ber_ec_dompar(SecureVector<byte> const& encoded); + +/** +* Factory function, the only way to obtain EC domain parameters with +* an OID. The demanded OID has to be registered in the InSiTo +* configuration. Consult the file ec_dompar.cpp for the default +* configuration. +* @param oid the oid of the demanded EC domain parameters +* @result the EC domain parameters associated with the OID +*/ +EC_Domain_Params get_EC_Dom_Pars_by_oid(std::string oid); + +} + +#endif diff --git a/botan/src/pubkey/ec_dompar/info.txt b/botan/src/pubkey/ec_dompar/info.txt new file mode 100644 index 0000000..2127837 --- /dev/null +++ b/botan/src/pubkey/ec_dompar/info.txt @@ -0,0 +1,18 @@ +realname "ECC Domain Parameters" + +define ECC_DOMAIN_PARAMATERS + +load_on auto + +<add> +ec_dompar.cpp +ec_dompar.h +</add> + +<requires> +asn1 +bigint +filters +gfpmath +hex +</requires> diff --git a/botan/src/pubkey/ecc_key/ecc_key.cpp b/botan/src/pubkey/ecc_key/ecc_key.cpp new file mode 100644 index 0000000..615efec --- /dev/null +++ b/botan/src/pubkey/ecc_key/ecc_key.cpp @@ -0,0 +1,269 @@ +/* +* ECC Key implemenation +* (C) 2007 Manuel Hartl, FlexSecure GmbH +* Falko Strenzke, FlexSecure GmbH +* 2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/ecc_key.h> +#include <botan/x509_key.h> +#include <botan/numthry.h> +#include <botan/util.h> +#include <botan/der_enc.h> +#include <botan/ber_dec.h> +#include <botan/secmem.h> +#include <botan/point_gfp.h> + +namespace Botan { + +/* +* EC_PublicKey +*/ +void EC_PublicKey::affirm_init() const // virtual + { + if((mp_dom_pars.get() == 0) || (mp_public_point.get() == 0)) + throw Invalid_State("cannot use uninitialized EC_Key"); + } + +const EC_Domain_Params& EC_PublicKey::domain_parameters() const + { + if(!mp_dom_pars.get()) + throw Invalid_State("EC_PublicKey::domain_parameters(): " + "ec domain parameters are not yet set"); + + return *mp_dom_pars; + } + +const PointGFp& EC_PublicKey::public_point() const + { + if(!mp_public_point.get()) + throw Invalid_State("EC_PublicKey::public_point(): public point not set"); + + return *mp_public_point; + } + +bool EC_PublicKey::domain_parameters_set() + { + return mp_dom_pars.get(); + } + +void EC_PublicKey::X509_load_hook() + { + try + { + // the base point is checked to be on curve already when decoding it + affirm_init(); + mp_public_point->check_invariants(); + } + catch(Illegal_Point) + { + throw Decoding_Error("decoded public point was found not to lie on curve"); + } + } + +X509_Encoder* EC_PublicKey::x509_encoder() const + { + class EC_Key_Encoder : public X509_Encoder + { + public: + AlgorithmIdentifier alg_id() const + { + key->affirm_init(); + + SecureVector<byte> params = + encode_der_ec_dompar(key->domain_parameters(), key->m_param_enc); + + return AlgorithmIdentifier(key->get_oid(), params); + } + + MemoryVector<byte> key_bits() const + { + key->affirm_init(); + return EC2OSP(*(key->mp_public_point), PointGFp::COMPRESSED); + } + + EC_Key_Encoder(const EC_PublicKey* k): key(k) {} + private: + const EC_PublicKey* key; + }; + + return new EC_Key_Encoder(this); + } + +X509_Decoder* EC_PublicKey::x509_decoder() + { + class EC_Key_Decoder : public X509_Decoder + { + public: + void alg_id(const AlgorithmIdentifier& alg_id) + { + key->mp_dom_pars.reset(new EC_Domain_Params(decode_ber_ec_dompar(alg_id.parameters))); + } + + void key_bits(const MemoryRegion<byte>& bits) + { + key->mp_public_point.reset( + new PointGFp( + OS2ECP(bits, key->domain_parameters().get_curve()) + )); + + key->X509_load_hook(); + } + + EC_Key_Decoder(EC_PublicKey* k): key(k) {} + private: + EC_PublicKey* key; + }; + + return new EC_Key_Decoder(this); + } + +void EC_PublicKey::set_parameter_encoding(EC_dompar_enc type) + { + if((type != ENC_EXPLICIT) && (type != ENC_IMPLICITCA) && (type != ENC_OID)) + throw Invalid_Argument("Invalid encoding type for EC-key object specified"); + + affirm_init(); + + if((type == ENC_OID) && (mp_dom_pars->get_oid() == "")) + throw Invalid_Argument("Invalid encoding type ENC_OID specified for " + "EC-key object whose corresponding domain " + "parameters are without oid"); + + m_param_enc = type; + } + +/******************************** +* EC_PrivateKey +********************************/ +void EC_PrivateKey::affirm_init() const // virtual + { + if(m_private_value == 0) + throw Invalid_State("cannot use EC_PrivateKey when private key is uninitialized"); + + EC_PublicKey::affirm_init(); + } + +const BigInt& EC_PrivateKey::private_value() const + { + if(m_private_value == 0) + throw Invalid_State("cannot use EC_PrivateKey when private key is uninitialized"); + + return m_private_value; + } + +/** +* EC_PrivateKey generator +**/ +void EC_PrivateKey::generate_private_key(RandomNumberGenerator& rng) + { + if(mp_dom_pars.get() == 0) + { + throw Invalid_State("cannot generate private key when domain parameters are not set"); + } + + BigInt tmp_private_value(0); + tmp_private_value = BigInt::random_integer(rng, 1, mp_dom_pars->get_order()); + mp_public_point = std::auto_ptr<PointGFp>( new PointGFp (mp_dom_pars->get_base_point())); + mp_public_point->mult_this_secure(tmp_private_value, + mp_dom_pars->get_order(), + mp_dom_pars->get_order()-1); + + //assert(mp_public_point.get() != 0); + tmp_private_value.swap(m_private_value); + } + +/** +* Return the PKCS #8 public key encoder +**/ +PKCS8_Encoder* EC_PrivateKey::pkcs8_encoder() const + { + class EC_Key_Encoder : public PKCS8_Encoder + { + public: + AlgorithmIdentifier alg_id() const + { + key->affirm_init(); + + SecureVector<byte> params = + encode_der_ec_dompar(key->domain_parameters(), ENC_EXPLICIT); + + return AlgorithmIdentifier(key->get_oid(), params); + } + + MemoryVector<byte> key_bits() const + { + key->affirm_init(); + SecureVector<byte> octstr_secret = + BigInt::encode_1363(key->m_private_value, key->m_private_value.bytes()); + + return DER_Encoder() + .start_cons(SEQUENCE) + .encode(BigInt(1)) + .encode(octstr_secret, OCTET_STRING) + .end_cons() + .get_contents(); + } + + EC_Key_Encoder(const EC_PrivateKey* k): key(k) {} + private: + const EC_PrivateKey* key; + }; + + return new EC_Key_Encoder(this); + } + +/** +* Return the PKCS #8 public key decoder +*/ +PKCS8_Decoder* EC_PrivateKey::pkcs8_decoder(RandomNumberGenerator&) + { + class EC_Key_Decoder : public PKCS8_Decoder + { + public: + void alg_id(const AlgorithmIdentifier& alg_id) + { + key->mp_dom_pars.reset(new EC_Domain_Params(decode_ber_ec_dompar(alg_id.parameters))); + } + + void key_bits(const MemoryRegion<byte>& bits) + { + u32bit version; + SecureVector<byte> octstr_secret; + + BER_Decoder(bits) + .start_cons(SEQUENCE) + .decode(version) + .decode(octstr_secret, OCTET_STRING) + .verify_end() + .end_cons(); + + key->m_private_value = BigInt::decode(octstr_secret, octstr_secret.size()); + + if(version != 1) + throw Decoding_Error("Wrong PKCS #1 key format version for EC key"); + + key->PKCS8_load_hook(); + } + + EC_Key_Decoder(EC_PrivateKey* k): key(k) {} + private: + EC_PrivateKey* key; + }; + + return new EC_Key_Decoder(this); + } + +void EC_PrivateKey::PKCS8_load_hook(bool) + { + // we cannot use affirm_init() here because mp_public_point might still be null + if(mp_dom_pars.get() == 0) + throw Invalid_State("attempt to set public point for an uninitialized key"); + + mp_public_point.reset(new PointGFp(m_private_value * mp_dom_pars->get_base_point())); + mp_public_point->check_invariants(); + } + +} diff --git a/botan/src/pubkey/ecc_key/ecc_key.h b/botan/src/pubkey/ecc_key/ecc_key.h new file mode 100644 index 0000000..0ca9a0e --- /dev/null +++ b/botan/src/pubkey/ecc_key/ecc_key.h @@ -0,0 +1,154 @@ +/* +* ECDSA +* (C) 2007 Falko Strenzke, FlexSecure GmbH +* Manuel Hartl, FlexSecure GmbH +* (C) 2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ECC_PUBLIC_KEY_BASE_H__ +#define BOTAN_ECC_PUBLIC_KEY_BASE_H__ + +#include <botan/bigint.h> +#include <botan/curve_gfp.h> +#include <botan/pk_keys.h> +#include <botan/ec_dompar.h> +#include <botan/x509_key.h> +#include <botan/pkcs8.h> + +namespace Botan { + +/** +* This class represents abstract EC Public Keys. When encoding a key +* via an encoder that can be accessed via the corresponding member +* functions, the key will decide upon its internally stored encoding +* information whether to encode itself with or without domain +* parameters, or using the domain parameter oid. Furthermore, a public +* key without domain parameters can be decoded. In that case, it +* cannot be used for verification until its domain parameters are set +* by calling the corresponding member function. +*/ +class BOTAN_DLL EC_PublicKey : public virtual Public_Key + { + public: + + /** + * Tells whether this key knows his own domain parameters. + * @result true if the domain parameters are set, false otherwise + */ + bool domain_parameters_set(); + + /** + * Get the public point of this key. + * @throw Invalid_State is thrown if the + * domain parameters of this point are not set + * @result the public point of this key + */ + const PointGFp& public_point() const; + + /** + * Get the domain parameters of this key. + * @throw Invalid_State is thrown if the + * domain parameters of this point are not set + * @result the domain parameters of this key + */ + const EC_Domain_Params& domain_parameters() const; + + /** + * Set the domain parameter encoding to be used when encoding this key. + * @param enc the encoding to use + */ + void set_parameter_encoding(EC_dompar_enc enc); + + /** + * Get the domain parameter encoding to be used when encoding this key. + * @result the encoding to use + */ + inline int get_parameter_encoding() const + { + return m_param_enc; + } + + //ctors + EC_PublicKey() + : m_param_enc(ENC_EXPLICIT) + { + //assert(mp_dom_pars.get() == 0); + //assert(mp_public_point.get() == 0); + } + + /** + * Get an x509_encoder that can be used to encode this key. + * @result an x509_encoder for this key + */ + X509_Encoder* x509_encoder() const; + + /** + * Get an x509_decoder that can be used to decode a stored key into + * this key. + * @result an x509_decoder for this key + */ + X509_Decoder* x509_decoder(); + + /** + * Make sure that the public point and domain parameters of this key are set. + * @throw Invalid_State if either of the two data members is not set + */ + virtual void affirm_init() const; + + virtual ~EC_PublicKey() {} + protected: + virtual void X509_load_hook(); + + SecureVector<byte> m_enc_public_point; // stores the public point + + std::auto_ptr<EC_Domain_Params> mp_dom_pars; + std::auto_ptr<PointGFp> mp_public_point; + EC_dompar_enc m_param_enc; + }; + +/** +* This abstract class represents general EC Private Keys +*/ +class BOTAN_DLL EC_PrivateKey : public virtual EC_PublicKey, public virtual Private_Key + { + public: + + /** + * Get an PKCS#8 encoder that can be used to encoded this key. + * @result an PKCS#8 encoder for this key + */ + PKCS8_Encoder* pkcs8_encoder() const; + + /** + * Get an PKCS#8 decoder that can be used to decoded a stored key into + * this key. + * @result an PKCS#8 decoder for this key + */ + PKCS8_Decoder* pkcs8_decoder(RandomNumberGenerator&); + + /** + * Get the private key value of this key object. + * @result the private key value of this key object + */ + const BigInt& private_value() const; + + /** + * Make sure that the public key parts of this object are set + * (calls EC_PublicKey::affirm_init()) as well as the private key + * value. + * @throw Invalid_State if the above conditions are not satisfied + */ + virtual void affirm_init() const; + + virtual ~EC_PrivateKey() {} + protected: + virtual void PKCS8_load_hook(bool = false); + void generate_private_key(RandomNumberGenerator&); + BigInt m_private_value; + }; + +} + +#endif diff --git a/botan/src/pubkey/ecc_key/info.txt b/botan/src/pubkey/ecc_key/info.txt new file mode 100644 index 0000000..2a3c9a3 --- /dev/null +++ b/botan/src/pubkey/ecc_key/info.txt @@ -0,0 +1,20 @@ +realname "ECC Public Key" + +define ECC_PUBLIC_KEY_CRYPTO + +load_on auto + +<add> +ecc_key.cpp +ecc_key.h +</add> + +<requires> +alloc +asn1 +bigint +ec_dompar +gfpmath +numbertheory +pk_codecs +</requires> diff --git a/botan/src/pubkey/ecdsa/ecdsa.cpp b/botan/src/pubkey/ecdsa/ecdsa.cpp new file mode 100644 index 0000000..9640c63 --- /dev/null +++ b/botan/src/pubkey/ecdsa/ecdsa.cpp @@ -0,0 +1,230 @@ +/* +* ECDSA implemenation +* (C) 2007 Manuel Hartl, FlexSecure GmbH +* 2007 Falko Strenzke, FlexSecure GmbH +* 2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/ecdsa.h> +#include <botan/numthry.h> +#include <botan/util.h> +#include <botan/der_enc.h> +#include <botan/ber_dec.h> +#include <botan/secmem.h> +#include <botan/point_gfp.h> + +namespace Botan { + +ECDSA_PrivateKey::ECDSA_PrivateKey(RandomNumberGenerator& rng, + const EC_Domain_Params& dom_pars) + { + mp_dom_pars = std::auto_ptr<EC_Domain_Params>(new EC_Domain_Params(dom_pars)); + generate_private_key(rng); + + try + { + mp_public_point->check_invariants(); + } + catch(Illegal_Point& e) + { + throw Invalid_State("ECDSA key generation failed"); + } + + m_ecdsa_core = ECDSA_Core(*mp_dom_pars, m_private_value, *mp_public_point); + } + +/* +* ECDSA_PublicKey +*/ +void ECDSA_PublicKey::affirm_init() const // virtual + { + EC_PublicKey::affirm_init(); + } + +void ECDSA_PublicKey::set_domain_parameters(const EC_Domain_Params& dom_pars) + { + if(mp_dom_pars.get()) + { + // they are already set, we must ensure that they are equal to the arg + if(dom_pars != *mp_dom_pars.get()) + throw Invalid_Argument("EC_PublicKey::set_domain_parameters - cannot reset to a new value"); + + return; + } + + if(m_enc_public_point.size() == 0) + throw Invalid_State("EC_PublicKey::set_domain_parameters(): encoded public point isn't set"); + + // now try to decode the public key ... + PointGFp tmp_pp(OS2ECP(m_enc_public_point, dom_pars.get_curve())); + try + { + tmp_pp.check_invariants(); + } + catch(Illegal_Point e) + { + throw Invalid_State("EC_PublicKey::set_domain_parameters(): point does not lie on provided curve"); + } + + std::auto_ptr<EC_Domain_Params> p_tmp_pars(new EC_Domain_Params(dom_pars)); + ECDSA_Core tmp_ecdsa_core(*p_tmp_pars, BigInt(0), tmp_pp); + mp_public_point.reset(new PointGFp(tmp_pp)); + m_ecdsa_core = tmp_ecdsa_core; + mp_dom_pars = p_tmp_pars; + } + +void ECDSA_PublicKey::set_all_values(const ECDSA_PublicKey& other) + { + m_param_enc = other.m_param_enc; + m_ecdsa_core = other.m_ecdsa_core; + m_enc_public_point = other.m_enc_public_point; + if(other.mp_dom_pars.get()) + mp_dom_pars.reset(new EC_Domain_Params(other.domain_parameters())); + + if(other.mp_public_point.get()) + mp_public_point.reset(new PointGFp(other.public_point())); + } + +ECDSA_PublicKey::ECDSA_PublicKey(const ECDSA_PublicKey& other) + : Public_Key(), + EC_PublicKey(), + PK_Verifying_wo_MR_Key() + { + set_all_values(other); + } + +const ECDSA_PublicKey& ECDSA_PublicKey::operator=(const ECDSA_PublicKey& rhs) + { + set_all_values(rhs); + return *this; + } + +bool ECDSA_PublicKey::verify(const byte message[], + u32bit mess_len, + const byte signature[], + u32bit sig_len) const + { + affirm_init(); + + BigInt r, s; + + BER_Decoder(signature, sig_len) + .start_cons(SEQUENCE) + .decode(r) + .decode(s) + .end_cons() + .verify_end(); + + u32bit enc_len = std::max(r.bytes(), s.bytes()); + + SecureVector<byte> sv_plain_sig; + + sv_plain_sig.append(BigInt::encode_1363(r, enc_len)); + sv_plain_sig.append(BigInt::encode_1363(s, enc_len)); + + return m_ecdsa_core.verify(sv_plain_sig, sv_plain_sig.size(), + message, mess_len); + } + +ECDSA_PublicKey::ECDSA_PublicKey(const EC_Domain_Params& dom_par, + const PointGFp& public_point) + { + mp_dom_pars = std::auto_ptr<EC_Domain_Params>(new EC_Domain_Params(dom_par)); + mp_public_point = std::auto_ptr<PointGFp>(new PointGFp(public_point)); + m_param_enc = ENC_EXPLICIT; + m_ecdsa_core = ECDSA_Core(*mp_dom_pars, BigInt(0), *mp_public_point); + } + +void ECDSA_PublicKey::X509_load_hook() + { + EC_PublicKey::X509_load_hook(); + EC_PublicKey::affirm_init(); + m_ecdsa_core = ECDSA_Core ( *mp_dom_pars, BigInt ( 0 ), *mp_public_point ); + } + +u32bit ECDSA_PublicKey::max_input_bits() const + { + if(!mp_dom_pars.get()) + { + throw Invalid_State("ECDSA_PublicKey::max_input_bits(): domain parameters not set"); + } + return mp_dom_pars->get_order().bits(); + } + +/************************* +* ECDSA_PrivateKey +*************************/ +void ECDSA_PrivateKey::affirm_init() const // virtual + { + EC_PrivateKey::affirm_init(); + } + +void ECDSA_PrivateKey::PKCS8_load_hook(bool generated) + { + EC_PrivateKey::PKCS8_load_hook(generated); + EC_PrivateKey::affirm_init(); + m_ecdsa_core = ECDSA_Core(*mp_dom_pars, m_private_value, *mp_public_point); + } + +void ECDSA_PrivateKey::set_all_values(const ECDSA_PrivateKey& other) + { + m_private_value = other.m_private_value; + m_param_enc = other.m_param_enc; + m_ecdsa_core = other.m_ecdsa_core; + m_enc_public_point = other.m_enc_public_point; + + if(other.mp_dom_pars.get()) + mp_dom_pars.reset(new EC_Domain_Params(other.domain_parameters())); + + if(other.mp_public_point.get()) + mp_public_point.reset(new PointGFp(other.public_point())); + } + +ECDSA_PrivateKey::ECDSA_PrivateKey(ECDSA_PrivateKey const& other) + : Public_Key(), + EC_PublicKey(), + Private_Key(), + ECDSA_PublicKey(), + EC_PrivateKey(), + PK_Signing_Key() + { + set_all_values(other); + } + + +const ECDSA_PrivateKey& ECDSA_PrivateKey::operator=(const ECDSA_PrivateKey& rhs) + { + set_all_values(rhs); + return *this; + } + +SecureVector<byte> ECDSA_PrivateKey::sign(const byte message[], + u32bit mess_len, + RandomNumberGenerator& rng) const + { + affirm_init(); + + SecureVector<byte> sv_sig = m_ecdsa_core.sign(message, mess_len, rng); + + if(sv_sig.size() % 2 != 0) + throw Invalid_Argument("Erroneous length of signature"); + + u32bit rs_len = sv_sig.size() / 2; + SecureVector<byte> sv_r, sv_s; + sv_r.set(sv_sig.begin(), rs_len); + sv_s.set(&sv_sig[rs_len], rs_len); + + BigInt r = BigInt::decode(sv_r, sv_r.size()); + BigInt s = BigInt::decode(sv_s, sv_s.size()); + + return DER_Encoder() + .start_cons(SEQUENCE) + .encode(r) + .encode(s) + .end_cons() + .get_contents(); + } + +} diff --git a/botan/src/pubkey/ecdsa/ecdsa.h b/botan/src/pubkey/ecdsa/ecdsa.h new file mode 100644 index 0000000..3794457 --- /dev/null +++ b/botan/src/pubkey/ecdsa/ecdsa.h @@ -0,0 +1,145 @@ +/* +* ECDSA +* (C) 2007 Falko Strenzke, FlexSecure GmbH +* Manuel Hartl, FlexSecure GmbH +* (C) 2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ECDSA_KEY_H__ +#define BOTAN_ECDSA_KEY_H__ + +#include <botan/ecc_key.h> +#include <botan/ecdsa_core.h> + +namespace Botan { + +/** +* This class represents ECDSA Public Keys. +*/ +class BOTAN_DLL ECDSA_PublicKey : public virtual EC_PublicKey, + public PK_Verifying_wo_MR_Key + { + public: + + /** + * Get this keys algorithm name. + * @result this keys algorithm name ("ECDSA") + */ + std::string algo_name() const { return "ECDSA"; } + + /** + * Get the maximum number of bits allowed to be fed to this key. + * This is the bitlength of the order of the base point. + + * @result the maximum number of input bits + */ + u32bit max_input_bits() const; + + /** + * Verify a message with this key. + * @param message the byte array containing the message + * @param mess_len the number of bytes in the message byte array + * @param signature the byte array containing the signature + * @param sig_len the number of bytes in the signature byte array + */ + bool verify(const byte message[], u32bit mess_len, + const byte signature[], u32bit sig_len) const; + + /** + * Default constructor. Use this one if you want to later fill + * this object with data from an encoded key. + */ + ECDSA_PublicKey() {} + + /** + * Construct a public key from a given public point. + * @param dom_par the domain parameters associated with this key + * @param public_point the public point defining this key + */ + ECDSA_PublicKey(const EC_Domain_Params& dom_par, + const PointGFp& public_point); // sets core + + ECDSA_PublicKey const& operator=(const ECDSA_PublicKey& rhs); + + ECDSA_PublicKey(const ECDSA_PublicKey& other); + + /** + * Set the domain parameters of this key. This function has to be + * used when a key encoded without domain parameters was decoded into + * this key. Otherwise it will not be able to verify a signature. + * @param dom_pars the domain_parameters associated with this key + * @throw Invalid_Argument if the point was found not to be satisfying the + * curve equation of the provided domain parameters + * or if this key already has domain parameters set + * and these are differing from those given as the parameter + */ + void set_domain_parameters(const EC_Domain_Params& dom_pars); + + /** + * Ensure that the public point and domain parameters of this key are set. + * @throw Invalid_State if either of the two data members is not set + */ + virtual void affirm_init() const; + + protected: + void X509_load_hook(); + virtual void set_all_values(const ECDSA_PublicKey& other); + + ECDSA_Core m_ecdsa_core; + }; + +/** +* This class represents ECDSA Private Keys +*/ +class BOTAN_DLL ECDSA_PrivateKey : public ECDSA_PublicKey, + public EC_PrivateKey, + public PK_Signing_Key + { + public: + //ctors + + /** + * Default constructor. Use this one if you want to later fill + * this object with data from an encoded key. + */ + ECDSA_PrivateKey() {} + + /** + * Generate a new private key + * @param the domain parameters to used for this key + */ + ECDSA_PrivateKey(RandomNumberGenerator& rng, + const EC_Domain_Params& domain); + + ECDSA_PrivateKey(const ECDSA_PrivateKey& other); + ECDSA_PrivateKey const& operator=(const ECDSA_PrivateKey& rhs); + + /** + * Sign a message with this key. + * @param message the byte array representing the message to be signed + * @param mess_len the length of the message byte array + * @result the signature + */ + + SecureVector<byte> sign(const byte message[], u32bit mess_len, + RandomNumberGenerator& rng) const; + + /** + * Make sure that the public key parts of this object are set + * (calls EC_PublicKey::affirm_init()) as well as the private key + * value. + * @throw Invalid_State if the above conditions are not satisfied + */ + virtual void affirm_init() const; + + protected: + virtual void set_all_values(const ECDSA_PrivateKey& other); + private: + void PKCS8_load_hook(bool = false); + }; + +} + +#endif diff --git a/botan/src/pubkey/ecdsa/ecdsa_core.cpp b/botan/src/pubkey/ecdsa/ecdsa_core.cpp new file mode 100644 index 0000000..93808cc --- /dev/null +++ b/botan/src/pubkey/ecdsa/ecdsa_core.cpp @@ -0,0 +1,55 @@ +/* +* ECDSA Core +* (C) 1999-2007 Jack Lloyd +* (C) 2007 FlexSecure GmbH +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/ecdsa_core.h> +#include <botan/numthry.h> +#include <botan/pk_engine.h> +#include <botan/parsing.h> +#include <algorithm> + +namespace Botan { + +/* +* ECDSA Operation +*/ +bool ECDSA_Core::verify(const byte signature[], u32bit sig_len, + const byte message[], u32bit mess_len) const + { + //assert(op.get()); + return op->verify(signature, sig_len, message, mess_len); + } + +SecureVector<byte> ECDSA_Core::sign(const byte message[], + u32bit mess_len, + RandomNumberGenerator& rng) const + { + //assert(op.get()); + return op->sign(message, mess_len, rng); + } + +ECDSA_Core& ECDSA_Core::operator=(const ECDSA_Core& core) + { + delete op; + if(core.op) + op = core.op->clone(); + return (*this); + } + +ECDSA_Core::ECDSA_Core(const ECDSA_Core& core) + { + op = 0; + if(core.op) + op = core.op->clone(); + } + +ECDSA_Core::ECDSA_Core(EC_Domain_Params const& dom_pars, const BigInt& priv_key, PointGFp const& pub_key) + { + op = Engine_Core::ecdsa_op(dom_pars, priv_key, pub_key); + } + +} diff --git a/botan/src/pubkey/ecdsa/ecdsa_core.h b/botan/src/pubkey/ecdsa/ecdsa_core.h new file mode 100644 index 0000000..ceccc94 --- /dev/null +++ b/botan/src/pubkey/ecdsa/ecdsa_core.h @@ -0,0 +1,47 @@ +/* +* ECDSA Core +* (C) 1999-2007 Jack Lloyd +* (C) 2007 FlexSecure GmbH +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ECDSA_CORE_H__ +#define BOTAN_ECDSA_CORE_H__ + +#include <botan/ecdsa_op.h> +#include <botan/blinding.h> +#include <botan/ec_dompar.h> + +namespace Botan { + +/* +* ECDSA Core +*/ +class BOTAN_DLL ECDSA_Core + { + public: + bool verify(const byte signature[], u32bit sig_len, + const byte message[], u32bit mess_len) const; + + SecureVector<byte> sign(const byte message[], u32bit mess_len, + RandomNumberGenerator& rng) const; + + ECDSA_Core& operator=(const ECDSA_Core&); + + ECDSA_Core() { op = 0; } + + ECDSA_Core(const ECDSA_Core&); + + ECDSA_Core(const EC_Domain_Params& dom_pars, + const BigInt& priv_key, + const PointGFp& pub_key); + + ~ECDSA_Core() { delete op; } + private: + ECDSA_Operation* op; + }; + +} + +#endif diff --git a/botan/src/pubkey/ecdsa/ecdsa_op.cpp b/botan/src/pubkey/ecdsa/ecdsa_op.cpp new file mode 100644 index 0000000..986043e --- /dev/null +++ b/botan/src/pubkey/ecdsa/ecdsa_op.cpp @@ -0,0 +1,129 @@ +/* +* ECDSA Operation +* (C) 2007 FlexSecure GmbH +* 2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/ecdsa_op.h> +#include <botan/numthry.h> + +namespace Botan { + +bool Default_ECDSA_Op::verify(const byte signature[], u32bit sig_len, + const byte message[], u32bit mess_len) const + { + if(sig_len % 2 != 0) + throw Invalid_Argument("Erroneous length of signature"); + + //NOTE: it is not checked whether the public point is set + if(m_dom_pars.get_curve().get_p() == 0) + throw Internal_Error("domain parameters not set"); + + BigInt e(message, mess_len); + + u32bit rs_len = sig_len/2; + SecureVector<byte> sv_r; + SecureVector<byte> sv_s; + sv_r.set(signature, rs_len); + sv_s.set(signature+rs_len, rs_len); + BigInt r = BigInt::decode ( sv_r, sv_r.size()); + BigInt s = BigInt::decode (sv_s, sv_s.size()); + + if(r < 0 || r >= m_dom_pars.get_order()) + throw Invalid_Argument("r in ECDSA signature has an illegal value"); + + if(s < 0 || s >= m_dom_pars.get_order()) + throw Invalid_Argument("s in ECDSA signature has an illegal value"); + + BigInt w = inverse_mod(s, m_dom_pars.get_order()); + + PointGFp R = w*(e*m_dom_pars.get_base_point() + r*m_pub_key); + if(R.is_zero()) + return false; + + BigInt x = R.get_affine_x().get_value(); + bool result = (x % m_dom_pars.get_order() == r); + return result; + } + +SecureVector<byte> Default_ECDSA_Op::sign(const byte message[], + u32bit mess_len, + RandomNumberGenerator& rng) const + { + if(m_priv_key == 0) + throw Internal_Error("Default_ECDSA_Op::sign(): no private key"); + + if(m_dom_pars.get_curve().get_p() == 0) + throw Internal_Error("Default_ECDSA_Op::sign(): domain parameters not set"); + + BigInt e(message, mess_len); + + // generate k + BigInt k; + BigInt r(0); + const BigInt n(m_dom_pars.get_order()); + while(r == 0) + { + k = BigInt::random_integer(rng, 1, n); + + PointGFp k_times_P(m_dom_pars.get_base_point()); + k_times_P.mult_this_secure(k, n, n-1); + k_times_P.check_invariants(); + r = k_times_P.get_affine_x().get_value() % n; + } + BigInt k_inv = inverse_mod(k, n); + + // use randomization against attacks on s: + // a = k_inv * (r*(d + x) + e) mod n + // b = k_inv * r * x mod n + // s = a - b mod n + // where x is a random integer + +#if defined(CMS_RAND) + BigInt x = BigInt::random_integer(0, n); + BigInt s = m_priv_key + x; // obscure the secret from the beginning + // all following operations thus are randomized + s *= r; + s += e; + s *= k_inv; + s %= n; + + BigInt b = x; // again, start with the random number + b *= r; + b *= k_inv; + b %= n; + s -= b; // s = a - b + if(s <= 0) // s %= n + { + s += n; + } +#else // CMS_RAND + // no countermeasure here + BigInt s(r); + s *= m_priv_key; + s += e; + s *= k_inv; + s %= n; + +#endif // CMS_RAND + + SecureVector<byte> sv_r = BigInt::encode_1363 ( r, m_dom_pars.get_order().bytes() ); + SecureVector<byte> sv_s = BigInt::encode_1363 ( s, m_dom_pars.get_order().bytes() ); + + SecureVector<byte> result(sv_r); + result.append(sv_s); + return result; + } + +Default_ECDSA_Op::Default_ECDSA_Op(const EC_Domain_Params& dom_pars, const BigInt& priv_key, const PointGFp& pub_key) + : m_dom_pars(dom_pars), + m_pub_key(pub_key), + m_priv_key(priv_key) + { + + } + +} + diff --git a/botan/src/pubkey/ecdsa/ecdsa_op.h b/botan/src/pubkey/ecdsa/ecdsa_op.h new file mode 100644 index 0000000..25831a9 --- /dev/null +++ b/botan/src/pubkey/ecdsa/ecdsa_op.h @@ -0,0 +1,64 @@ +/* +* ECDSA Operations +* (C) 1999-2008 Jack Lloyd +* (C) 2007 FlexSecure GmbH +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ECDSA_OPERATIONS_H__ +#define BOTAN_ECDSA_OPERATIONS_H__ + +#include <botan/ec_dompar.h> +#include <botan/rng.h> + +namespace Botan { + +/* +* ECDSA Operation +*/ +class BOTAN_DLL ECDSA_Operation + { + public: + virtual bool verify(const byte sig[], u32bit sig_len, + const byte msg[], u32bit msg_len) const = 0; + + virtual SecureVector<byte> sign(const byte message[], + u32bit mess_len, + RandomNumberGenerator&) const = 0; + + virtual ECDSA_Operation* clone() const = 0; + + virtual ~ECDSA_Operation() {} + }; + + +/* +* Default ECDSA operation +*/ +class BOTAN_DLL Default_ECDSA_Op : public ECDSA_Operation + { + public: + bool verify(const byte signature[], u32bit sig_len, + const byte message[], u32bit mess_len) const; + + SecureVector<byte> sign(const byte message[], u32bit mess_len, + RandomNumberGenerator& rng) const; + + ECDSA_Operation* clone() const + { + return new Default_ECDSA_Op(*this); + } + + Default_ECDSA_Op(const EC_Domain_Params& dom_pars, + const BigInt& priv_key, + const PointGFp& pub_key); + private: + EC_Domain_Params m_dom_pars; + PointGFp m_pub_key; + BigInt m_priv_key; + }; + +} + +#endif diff --git a/botan/src/pubkey/ecdsa/info.txt b/botan/src/pubkey/ecdsa/info.txt new file mode 100644 index 0000000..743440f --- /dev/null +++ b/botan/src/pubkey/ecdsa/info.txt @@ -0,0 +1,25 @@ +realname "ECDSA" + +define ECDSA + +load_on auto + +<add> +ecdsa.cpp +ecdsa.h +ecdsa_core.cpp +ecdsa_core.h +ecdsa_op.cpp +ecdsa_op.h +</add> + +<requires> +alloc +asn1 +ec_dompar +ecc_key +gfpmath +libstate +numbertheory +rng +</requires> diff --git a/botan/src/pubkey/eckaeg/eckaeg.cpp b/botan/src/pubkey/eckaeg/eckaeg.cpp new file mode 100644 index 0000000..b8ff75d --- /dev/null +++ b/botan/src/pubkey/eckaeg/eckaeg.cpp @@ -0,0 +1,152 @@ +/* +* ECKAEG implemenation +* (C) 2007 Manuel Hartl, FlexSecure GmbH +* 2007 Falko Strenzke, FlexSecure GmbH +* 2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/eckaeg.h> +#include <botan/numthry.h> +#include <botan/util.h> +#include <botan/der_enc.h> +#include <botan/ber_dec.h> +#include <botan/secmem.h> +#include <botan/point_gfp.h> + +namespace Botan { + +/********************************* +* ECKAEG_PublicKey +*********************************/ + +void ECKAEG_PublicKey::affirm_init() const // virtual + { + EC_PublicKey::affirm_init(); + } + +void ECKAEG_PublicKey::set_all_values(ECKAEG_PublicKey const& other) + { + m_param_enc = other.m_param_enc; + m_eckaeg_core = other.m_eckaeg_core; + m_enc_public_point = other.m_enc_public_point; + if(other.mp_dom_pars.get()) + { + mp_dom_pars.reset(new EC_Domain_Params(*(other.mp_dom_pars))); + } + if(other.mp_public_point.get()) + { + mp_public_point.reset(new PointGFp(*(other.mp_public_point))); + } + } + +ECKAEG_PublicKey::ECKAEG_PublicKey(ECKAEG_PublicKey const& other) + : Public_Key(), + EC_PublicKey() + { + set_all_values(other); + } + +ECKAEG_PublicKey const& ECKAEG_PublicKey::operator=(ECKAEG_PublicKey const& rhs) + { + set_all_values(rhs); + return *this; + } + +void ECKAEG_PublicKey::X509_load_hook() + { + EC_PublicKey::X509_load_hook(); + EC_PublicKey::affirm_init(); + m_eckaeg_core = ECKAEG_Core(*mp_dom_pars, BigInt(0), *mp_public_point); + } + +ECKAEG_PublicKey::ECKAEG_PublicKey(EC_Domain_Params const& dom_par, PointGFp const& public_point) + { + mp_dom_pars = std::auto_ptr<EC_Domain_Params>(new EC_Domain_Params(dom_par)); + mp_public_point = std::auto_ptr<PointGFp>(new PointGFp(public_point)); + if(mp_public_point->get_curve() != mp_dom_pars->get_curve()) + { + throw Invalid_Argument("ECKAEG_PublicKey(): curve of arg. point and curve of arg. domain parameters are different"); + } + EC_PublicKey::affirm_init(); + m_eckaeg_core = ECKAEG_Core(*mp_dom_pars, BigInt(0), *mp_public_point); + } + +/********************************* +* ECKAEG_PrivateKey +*********************************/ +void ECKAEG_PrivateKey::affirm_init() const // virtual + { + EC_PrivateKey::affirm_init(); + } + +void ECKAEG_PrivateKey::PKCS8_load_hook(bool generated) + { + EC_PrivateKey::PKCS8_load_hook(generated); + EC_PrivateKey::affirm_init(); + m_eckaeg_core = ECKAEG_Core(*mp_dom_pars, m_private_value, *mp_public_point); + } + +void ECKAEG_PrivateKey::set_all_values(ECKAEG_PrivateKey const& other) + { + m_private_value = other.m_private_value; + m_param_enc = other.m_param_enc; + m_eckaeg_core = other.m_eckaeg_core; + m_enc_public_point = other.m_enc_public_point; + if(other.mp_dom_pars.get()) + { + mp_dom_pars.reset(new EC_Domain_Params(*(other.mp_dom_pars))); + } + if(other.mp_public_point.get()) + { + mp_public_point.reset(new PointGFp(*(other.mp_public_point))); + } + } + +ECKAEG_PrivateKey::ECKAEG_PrivateKey(ECKAEG_PrivateKey const& other) + : Public_Key(), + EC_PublicKey(), + Private_Key(), + ECKAEG_PublicKey(), + EC_PrivateKey(), + PK_Key_Agreement_Key() + { + set_all_values(other); + } + +ECKAEG_PrivateKey const& ECKAEG_PrivateKey::operator= (ECKAEG_PrivateKey const& rhs) + { + set_all_values(rhs); + return *this; + } + +MemoryVector<byte> ECKAEG_PrivateKey::public_value() const + { + return EC2OSP(public_point(), PointGFp::UNCOMPRESSED); + } + +/** +* Derive a key +*/ +SecureVector<byte> ECKAEG_PrivateKey::derive_key(const byte key[], + u32bit key_len) const + { + MemoryVector<byte> key_x(key, key_len); // FIXME: nasty/slow + PointGFp point = OS2ECP(key_x, public_point().get_curve()); + + return m_eckaeg_core.agree(point); + } + +/** +* Derive a key +*/ +SecureVector<byte> ECKAEG_PrivateKey::derive_key(const ECKAEG_PublicKey& key) const + { + affirm_init(); + key.affirm_init(); + + return m_eckaeg_core.agree(key.public_point()); + } + +} diff --git a/botan/src/pubkey/eckaeg/eckaeg.h b/botan/src/pubkey/eckaeg/eckaeg.h new file mode 100644 index 0000000..31b6574 --- /dev/null +++ b/botan/src/pubkey/eckaeg/eckaeg.h @@ -0,0 +1,137 @@ +/* +* ECKAEG +* (C) 2007 Falko Strenzke, FlexSecure GmbH +* Manuel Hartl, FlexSecure GmbH +* (C) 2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ECKAEG_KEY_H__ +#define BOTAN_ECKAEG_KEY_H__ + +#include <botan/ecc_key.h> +#include <botan/eckaeg_core.h> + +namespace Botan { + +/** +* This class represents ECKAEG Public Keys. +*/ +class BOTAN_DLL ECKAEG_PublicKey : public virtual EC_PublicKey + { + public: + + /** + * Default constructor. Use this one if you want to later fill + * this object with data from an encoded key. + */ + ECKAEG_PublicKey() {} + + /** + * Construct a public key from a given public point. + * @param dom_par the domain parameters associated with this key + * @param public_point the public point defining this key + */ + ECKAEG_PublicKey(const EC_Domain_Params& dom_par, + const PointGFp& public_point); + + /** + * Get this keys algorithm name. + * @result this keys algorithm name + */ + std::string algo_name() const { return "ECKAEG"; } + + /** + * Get the maximum number of bits allowed to be fed to this key. + * This is the bitlength of the order of the base point. + + * @result the maximum number of input bits + */ + u32bit max_input_bits() const + { + if(!mp_dom_pars.get()) + throw Invalid_State("ECKAEG_PublicKey::max_input_bits(): domain parameters not set"); + + return mp_dom_pars->get_order().bits(); + } + + ECKAEG_PublicKey(ECKAEG_PublicKey const& other); + ECKAEG_PublicKey const& operator= (ECKAEG_PublicKey const& rhs); + + /** + * Make sure that the public point and domain parameters of this + * key are set. + * @throw Invalid_State if either of the two data members is not set + */ + virtual void affirm_init() const; + + protected: + void X509_load_hook(); + virtual void set_all_values(const ECKAEG_PublicKey& other); + + ECKAEG_Core m_eckaeg_core; + }; + +/** +* This class represents ECKAEG Private Keys. +*/ +class BOTAN_DLL ECKAEG_PrivateKey : public ECKAEG_PublicKey, + public EC_PrivateKey, + public PK_Key_Agreement_Key + { + public: + + /** + * Generate a new private key + * @param the domain parameters to used for this key + */ + ECKAEG_PrivateKey(RandomNumberGenerator& rng, + const EC_Domain_Params& dom_pars) + { + mp_dom_pars = std::auto_ptr<EC_Domain_Params>(new EC_Domain_Params(dom_pars)); + generate_private_key(rng); + mp_public_point->check_invariants(); + m_eckaeg_core = ECKAEG_Core(*mp_dom_pars, m_private_value, *mp_public_point); + } + + /** + * Default constructor. Use this one if you want to later fill this object with data + * from an encoded key. + */ + ECKAEG_PrivateKey() {} + ECKAEG_PrivateKey(ECKAEG_PrivateKey const& other); + ECKAEG_PrivateKey const& operator=(ECKAEG_PrivateKey const& rhs); + + MemoryVector<byte> public_value() const; + + void PKCS8_load_hook(bool = false); + + /** + * Derive a shared key with the other partys public key. + * @param key the other partys public key + * @param key_len the other partys public key + */ + SecureVector<byte> derive_key(const byte key[], u32bit key_len) const; + + /** + * Derive a shared key with the other partys public key. + * @param other the other partys public key + */ + SecureVector<byte> derive_key(const ECKAEG_PublicKey& other) const; + + /** + * Make sure that the public key parts of this object are set + * (calls EC_PublicKey::affirm_init()) as well as the private key + * value. + * @throw Invalid_State if the above conditions are not satisfied + */ + virtual void affirm_init() const; + + protected: + virtual void set_all_values(const ECKAEG_PrivateKey& other); + }; + +} + +#endif diff --git a/botan/src/pubkey/eckaeg/eckaeg_core.cpp b/botan/src/pubkey/eckaeg/eckaeg_core.cpp new file mode 100644 index 0000000..dc89a87 --- /dev/null +++ b/botan/src/pubkey/eckaeg/eckaeg_core.cpp @@ -0,0 +1,59 @@ +/* +* ECKAEG Core +* (C) 1999-2007 Jack Lloyd +* (C) 2007 FlexSecure GmbH +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/eckaeg_core.h> +#include <botan/numthry.h> +#include <botan/pk_engine.h> +#include <botan/parsing.h> +#include <algorithm> + +namespace Botan { + +/* +* ECKAEG_Core Constructor +*/ +ECKAEG_Core::ECKAEG_Core(const EC_Domain_Params& dom_pars, + const BigInt& priv_key, + const PointGFp& pub_key) + { + op = Engine_Core::eckaeg_op(dom_pars, priv_key, pub_key); + } + +/* +* ECKAEG_Core Copy Constructor +*/ +ECKAEG_Core::ECKAEG_Core(const ECKAEG_Core& core) + { + op = 0; + if(core.op) + op = core.op->clone(); + blinder = core.blinder; + } + +/* +* ECKAEG_Core Assignment Operator +*/ +ECKAEG_Core& ECKAEG_Core::operator=(const ECKAEG_Core& core) + { + delete op; + if(core.op) + op = core.op->clone(); + blinder = core.blinder; + return (*this); + } + +/* +* ECKAEG Operation +*/ +SecureVector<byte> ECKAEG_Core::agree(const PointGFp& otherKey) const + { + //assert(op.get()); + return op->agree(otherKey); + } + +} diff --git a/botan/src/pubkey/eckaeg/eckaeg_core.h b/botan/src/pubkey/eckaeg/eckaeg_core.h new file mode 100644 index 0000000..d632c94 --- /dev/null +++ b/botan/src/pubkey/eckaeg/eckaeg_core.h @@ -0,0 +1,44 @@ +/* +* ECKAEG Core +* (C) 1999-2007 Jack Lloyd +* (C) 2007 FlexSecure GmbH +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ECKAEG_CORE_H__ +#define BOTAN_ECKAEG_CORE_H__ + +#include <botan/eckaeg_op.h> +#include <botan/blinding.h> +#include <botan/ec_dompar.h> + +namespace Botan { + +/* +* ECKAEG Core +*/ +class BOTAN_DLL ECKAEG_Core + { + public: + SecureVector<byte> agree(const PointGFp&) const; + + ECKAEG_Core& operator=(const ECKAEG_Core&); + + ECKAEG_Core() { op = 0; } + + ECKAEG_Core(const ECKAEG_Core&); + + ECKAEG_Core(const EC_Domain_Params& dom_pars, + const BigInt& priv_key, + PointGFp const& pub_key); + + ~ECKAEG_Core() { delete op; } + private: + ECKAEG_Operation* op; + Blinder blinder; + }; + +} + +#endif diff --git a/botan/src/pubkey/eckaeg/eckaeg_op.cpp b/botan/src/pubkey/eckaeg/eckaeg_op.cpp new file mode 100644 index 0000000..0cb5c3d --- /dev/null +++ b/botan/src/pubkey/eckaeg/eckaeg_op.cpp @@ -0,0 +1,36 @@ +/* +* ECKAEG Operation +* (C) 2007 FlexSecure GmbH +* 2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/eckaeg_op.h> +#include <botan/numthry.h> + +namespace Botan { + +Default_ECKAEG_Op::Default_ECKAEG_Op(const EC_Domain_Params& dom_pars, + const BigInt& priv_key, + const PointGFp& pub_key) + : m_dom_pars(dom_pars), + m_pub_key(pub_key), + m_priv_key(priv_key) + { + } + +SecureVector<byte> Default_ECKAEG_Op::agree(const PointGFp& i) const + { + BigInt cofactor(m_dom_pars.get_cofactor()); + BigInt n = m_dom_pars.get_order(); + BigInt l(inverse_mod(cofactor,n)); // l=h^-1 mod n + PointGFp Q(cofactor*i); // q = h*Pb + PointGFp S(Q); + BigInt group_order = m_dom_pars.get_cofactor() * n; + S.mult_this_secure((m_priv_key*l)%n, group_order, n-1); + S.check_invariants(); + return FE2OSP(S.get_affine_x()); // fe2os(xs) + } + +} diff --git a/botan/src/pubkey/eckaeg/eckaeg_op.h b/botan/src/pubkey/eckaeg/eckaeg_op.h new file mode 100644 index 0000000..27cf4f3 --- /dev/null +++ b/botan/src/pubkey/eckaeg/eckaeg_op.h @@ -0,0 +1,49 @@ +/* +* ECKAEG Operations +* (C) 1999-2008 Jack Lloyd +* 2007 FlexSecure GmbH +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ECKAEG_OPERATIONS_H__ +#define BOTAN_ECKAEG_OPERATIONS_H__ + +#include <botan/ec_dompar.h> + +namespace Botan { + +/* +* ECKAEG Operation +*/ +class BOTAN_DLL ECKAEG_Operation + { + public: + virtual SecureVector<byte> agree(const PointGFp&) const = 0; + virtual ECKAEG_Operation* clone() const = 0; + virtual ~ECKAEG_Operation() {} + }; + +/* +* Default ECKAEG operation +*/ +class BOTAN_DLL Default_ECKAEG_Op : public ECKAEG_Operation + { + public: + SecureVector<byte> agree(const PointGFp& i) const; + + ECKAEG_Operation* clone() const { return new Default_ECKAEG_Op(*this); } + + Default_ECKAEG_Op(const EC_Domain_Params& dom_pars, + const BigInt& priv_key, + const PointGFp& pub_key); + private: + EC_Domain_Params m_dom_pars; + PointGFp m_pub_key; + BigInt m_priv_key; + }; + + +} + +#endif diff --git a/botan/src/pubkey/eckaeg/info.txt b/botan/src/pubkey/eckaeg/info.txt new file mode 100644 index 0000000..6b78f7d --- /dev/null +++ b/botan/src/pubkey/eckaeg/info.txt @@ -0,0 +1,24 @@ +realname "ECKAEG" + +define ECKAEG + +load_on auto + +<add> +eckaeg.cpp +eckaeg.h +eckaeg_core.cpp +eckaeg_core.h +eckaeg_op.cpp +eckaeg_op.h +</add> + +<requires> +alloc +asn1 +ec_dompar +ecc_key +gfpmath +libstate +numbertheory +</requires> diff --git a/botan/src/pubkey/elgamal/elg_core.cpp b/botan/src/pubkey/elgamal/elg_core.cpp new file mode 100644 index 0000000..8b8c8f5 --- /dev/null +++ b/botan/src/pubkey/elgamal/elg_core.cpp @@ -0,0 +1,97 @@ +/* +* ElGamal Core +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/elg_core.h> +#include <botan/numthry.h> +#include <botan/pk_engine.h> +#include <botan/parsing.h> +#include <algorithm> + +namespace Botan { + +namespace { + +const u32bit BLINDING_BITS = BOTAN_PRIVATE_KEY_OP_BLINDING_BITS; + +} + +/* +* ELG_Core Constructor +*/ +ELG_Core::ELG_Core(const DL_Group& group, const BigInt& y) + { + op = Engine_Core::elg_op(group, y, 0); + p_bytes = 0; + } + +/* +* ELG_Core Constructor +*/ +ELG_Core::ELG_Core(RandomNumberGenerator& rng, + const DL_Group& group, const BigInt& y, const BigInt& x) + { + op = Engine_Core::elg_op(group, y, x); + + const BigInt& p = group.get_p(); + p_bytes = p.bytes(); + + if(BLINDING_BITS) + { + BigInt k(rng, std::min(p.bits()-1, BLINDING_BITS)); + blinder = Blinder(k, power_mod(k, x, p), p); + } + } + +/* +* ELG_Core Copy Constructor +*/ +ELG_Core::ELG_Core(const ELG_Core& core) + { + op = 0; + if(core.op) + op = core.op->clone(); + blinder = core.blinder; + p_bytes = core.p_bytes; + } + +/* +* ELG_Core Assignment Operator +*/ +ELG_Core& ELG_Core::operator=(const ELG_Core& core) + { + delete op; + if(core.op) + op = core.op->clone(); + blinder = core.blinder; + p_bytes = core.p_bytes; + return (*this); + } + +/* +* ElGamal Encrypt Operation +*/ +SecureVector<byte> ELG_Core::encrypt(const byte in[], u32bit length, + const BigInt& k) const + { + return op->encrypt(in, length, k); + } + +/* +* ElGamal Decrypt Operation +*/ +SecureVector<byte> ELG_Core::decrypt(const byte in[], u32bit length) const + { + if(length != 2*p_bytes) + throw Invalid_Argument("ELG_Core::decrypt: Invalid message"); + + BigInt a(in, p_bytes); + BigInt b(in + p_bytes, p_bytes); + + return BigInt::encode(blinder.unblind(op->decrypt(blinder.blind(a), b))); + } + +} diff --git a/botan/src/pubkey/elgamal/elg_core.h b/botan/src/pubkey/elgamal/elg_core.h new file mode 100644 index 0000000..a7768a6 --- /dev/null +++ b/botan/src/pubkey/elgamal/elg_core.h @@ -0,0 +1,44 @@ +/* +* ElGamal Core +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ELGAMAL_CORE_H__ +#define BOTAN_ELGAMAL_CORE_H__ + +#include <botan/elg_op.h> +#include <botan/blinding.h> +#include <botan/dl_group.h> + +namespace Botan { + +/* +* ElGamal Core +*/ +class BOTAN_DLL ELG_Core + { + public: + SecureVector<byte> encrypt(const byte[], u32bit, const BigInt&) const; + SecureVector<byte> decrypt(const byte[], u32bit) const; + + ELG_Core& operator=(const ELG_Core&); + + ELG_Core() { op = 0; } + ELG_Core(const ELG_Core&); + + ELG_Core(const DL_Group&, const BigInt&); + ELG_Core(RandomNumberGenerator&, const DL_Group&, + const BigInt&, const BigInt&); + + ~ELG_Core() { delete op; } + private: + ELG_Operation* op; + Blinder blinder; + u32bit p_bytes; + }; + +} + +#endif diff --git a/botan/src/pubkey/elgamal/elg_op.cpp b/botan/src/pubkey/elgamal/elg_op.cpp new file mode 100644 index 0000000..1e476ab --- /dev/null +++ b/botan/src/pubkey/elgamal/elg_op.cpp @@ -0,0 +1,56 @@ +/* +* ElGamal Operations +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/elg_op.h> + +namespace Botan { + +/* +* Default_ELG_Op Constructor +*/ +Default_ELG_Op::Default_ELG_Op(const DL_Group& group, const BigInt& y, + const BigInt& x) : p(group.get_p()) + { + powermod_g_p = Fixed_Base_Power_Mod(group.get_g(), p); + powermod_y_p = Fixed_Base_Power_Mod(y, p); + mod_p = Modular_Reducer(p); + + if(x != 0) + powermod_x_p = Fixed_Exponent_Power_Mod(x, p); + } + +/* +* Default ElGamal Encrypt Operation +*/ +SecureVector<byte> Default_ELG_Op::encrypt(const byte in[], u32bit length, + const BigInt& k) const + { + BigInt m(in, length); + if(m >= p) + throw Invalid_Argument("Default_ELG_Op::encrypt: Input is too large"); + + BigInt a = powermod_g_p(k); + BigInt b = mod_p.multiply(m, powermod_y_p(k)); + + SecureVector<byte> output(2*p.bytes()); + a.binary_encode(output + (p.bytes() - a.bytes())); + b.binary_encode(output + output.size() / 2 + (p.bytes() - b.bytes())); + return output; + } + +/* +* Default ElGamal Decrypt Operation +*/ +BigInt Default_ELG_Op::decrypt(const BigInt& a, const BigInt& b) const + { + if(a >= p || b >= p) + throw Invalid_Argument("Default_ELG_Op: Invalid message"); + + return mod_p.multiply(b, inverse_mod(powermod_x_p(a), p)); + } + +} diff --git a/botan/src/pubkey/elgamal/elg_op.h b/botan/src/pubkey/elgamal/elg_op.h new file mode 100644 index 0000000..39ed897 --- /dev/null +++ b/botan/src/pubkey/elgamal/elg_op.h @@ -0,0 +1,52 @@ +/* +* ElGamal Operations +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ELGAMAL_OPS_H__ +#define BOTAN_ELGAMAL_OPS_H__ + +#include <botan/pow_mod.h> +#include <botan/numthry.h> +#include <botan/reducer.h> +#include <botan/dl_group.h> + +namespace Botan { + +/* +* ElGamal Operation +*/ +class BOTAN_DLL ELG_Operation + { + public: + virtual SecureVector<byte> encrypt(const byte[], u32bit, + const BigInt&) const = 0; + virtual BigInt decrypt(const BigInt&, const BigInt&) const = 0; + virtual ELG_Operation* clone() const = 0; + virtual ~ELG_Operation() {} + }; + +/* +* Botan's Default ElGamal Operation +*/ +class BOTAN_DLL Default_ELG_Op : public ELG_Operation + { + public: + SecureVector<byte> encrypt(const byte[], u32bit, const BigInt&) const; + BigInt decrypt(const BigInt&, const BigInt&) const; + + ELG_Operation* clone() const { return new Default_ELG_Op(*this); } + + Default_ELG_Op(const DL_Group&, const BigInt&, const BigInt&); + private: + const BigInt p; + Fixed_Base_Power_Mod powermod_g_p, powermod_y_p; + Fixed_Exponent_Power_Mod powermod_x_p; + Modular_Reducer mod_p; + }; + +} + +#endif diff --git a/botan/src/pubkey/elgamal/elgamal.cpp b/botan/src/pubkey/elgamal/elgamal.cpp new file mode 100644 index 0000000..1f79df5 --- /dev/null +++ b/botan/src/pubkey/elgamal/elgamal.cpp @@ -0,0 +1,124 @@ +/* +* ElGamal +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/elgamal.h> +#include <botan/numthry.h> +#include <botan/keypair.h> +#include <botan/look_pk.h> +#include <botan/util.h> + +namespace Botan { + +/* +* ElGamal_PublicKey Constructor +*/ +ElGamal_PublicKey::ElGamal_PublicKey(const DL_Group& grp, const BigInt& y1) + { + group = grp; + y = y1; + X509_load_hook(); + } + +/* +* Algorithm Specific X.509 Initialization Code +*/ +void ElGamal_PublicKey::X509_load_hook() + { + core = ELG_Core(group, y); + } + +/* +* ElGamal Encryption Function +*/ +SecureVector<byte> +ElGamal_PublicKey::encrypt(const byte in[], u32bit length, + RandomNumberGenerator& rng) const + { + BigInt k(rng, 2 * dl_work_factor(group_p().bits())); + return core.encrypt(in, length, k); + } + +/* +* Return the maximum input size in bits +*/ +u32bit ElGamal_PublicKey::max_input_bits() const + { + return (group_p().bits() - 1); + } + +/* +* ElGamal_PrivateKey Constructor +*/ +ElGamal_PrivateKey::ElGamal_PrivateKey(RandomNumberGenerator& rng, + const DL_Group& grp, + const BigInt& x_arg) + { + group = grp; + x = x_arg; + + if(x == 0) + { + x.randomize(rng, 2 * dl_work_factor(group_p().bits())); + PKCS8_load_hook(rng, true); + } + else + PKCS8_load_hook(rng, false); + } + +/* +* Algorithm Specific PKCS #8 Initialization Code +*/ +void ElGamal_PrivateKey::PKCS8_load_hook(RandomNumberGenerator& rng, + bool generated) + { + if(y == 0) + y = power_mod(group_g(), x, group_p()); + core = ELG_Core(rng, group, y, x); + + if(generated) + gen_check(rng); + else + load_check(rng); + } + +/* +* ElGamal Decryption Function +*/ +SecureVector<byte> ElGamal_PrivateKey::decrypt(const byte in[], + u32bit length) const + { + return core.decrypt(in, length); + } + +/* +* Check Private ElGamal Parameters +*/ +bool ElGamal_PrivateKey::check_key(RandomNumberGenerator& rng, + bool strong) const + { + if(!DL_Scheme_PrivateKey::check_key(rng, strong)) + return false; + + if(!strong) + return true; + + try + { + KeyPair::check_key(rng, + get_pk_encryptor(*this, "EME1(SHA-1)"), + get_pk_decryptor(*this, "EME1(SHA-1)") + ); + } + catch(Self_Test_Failure) + { + return false; + } + + return true; + } + +} diff --git a/botan/src/pubkey/elgamal/elgamal.h b/botan/src/pubkey/elgamal/elgamal.h new file mode 100644 index 0000000..93e640f --- /dev/null +++ b/botan/src/pubkey/elgamal/elgamal.h @@ -0,0 +1,59 @@ +/* +* ElGamal +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ELGAMAL_H__ +#define BOTAN_ELGAMAL_H__ + +#include <botan/dl_algo.h> +#include <botan/elg_core.h> + +namespace Botan { + +/* +* ElGamal Public Key +*/ +class BOTAN_DLL ElGamal_PublicKey : public PK_Encrypting_Key, + public virtual DL_Scheme_PublicKey + { + public: + std::string algo_name() const { return "ElGamal"; } + DL_Group::Format group_format() const { return DL_Group::ANSI_X9_42; } + + SecureVector<byte> encrypt(const byte[], u32bit, + RandomNumberGenerator& rng) const; + u32bit max_input_bits() const; + + ElGamal_PublicKey() {} + ElGamal_PublicKey(const DL_Group&, const BigInt&); + protected: + ELG_Core core; + private: + void X509_load_hook(); + }; + +/* +* ElGamal Private Key +*/ +class BOTAN_DLL ElGamal_PrivateKey : public ElGamal_PublicKey, + public PK_Decrypting_Key, + public virtual DL_Scheme_PrivateKey + { + public: + SecureVector<byte> decrypt(const byte[], u32bit) const; + + bool check_key(RandomNumberGenerator& rng, bool) const; + + ElGamal_PrivateKey() {} + ElGamal_PrivateKey(RandomNumberGenerator&, const DL_Group&, + const BigInt& = 0); + private: + void PKCS8_load_hook(RandomNumberGenerator&, bool = false); + }; + +} + +#endif diff --git a/botan/src/pubkey/elgamal/info.txt b/botan/src/pubkey/elgamal/info.txt new file mode 100644 index 0000000..d7ae614 --- /dev/null +++ b/botan/src/pubkey/elgamal/info.txt @@ -0,0 +1,22 @@ +realname "ElGamal" + +define ELGAMAL + +load_on auto + +<add> +elgamal.cpp +elgamal.h +elg_core.cpp +elg_core.h +elg_op.cpp +elg_op.h +</add> + +<requires> +dl_algo +dl_group +keypair +libstate +numbertheory +</requires> diff --git a/botan/src/pubkey/if_algo/if_algo.cpp b/botan/src/pubkey/if_algo/if_algo.cpp new file mode 100644 index 0000000..556c86f --- /dev/null +++ b/botan/src/pubkey/if_algo/if_algo.cpp @@ -0,0 +1,215 @@ +/* +* IF Scheme +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/if_algo.h> +#include <botan/numthry.h> +#include <botan/der_enc.h> +#include <botan/ber_dec.h> + +namespace Botan { + +/* +* Return the X.509 public key encoder +*/ +X509_Encoder* IF_Scheme_PublicKey::x509_encoder() const + { + class IF_Scheme_Encoder : public X509_Encoder + { + public: + AlgorithmIdentifier alg_id() const + { + return AlgorithmIdentifier(key->get_oid(), + AlgorithmIdentifier::USE_NULL_PARAM); + } + + MemoryVector<byte> key_bits() const + { + return DER_Encoder() + .start_cons(SEQUENCE) + .encode(key->n) + .encode(key->e) + .end_cons() + .get_contents(); + } + + IF_Scheme_Encoder(const IF_Scheme_PublicKey* k) : key(k) {} + private: + const IF_Scheme_PublicKey* key; + }; + + return new IF_Scheme_Encoder(this); + } + +/* +* Return the X.509 public key decoder +*/ +X509_Decoder* IF_Scheme_PublicKey::x509_decoder() + { + class IF_Scheme_Decoder : public X509_Decoder + { + public: + void alg_id(const AlgorithmIdentifier&) {} + + void key_bits(const MemoryRegion<byte>& bits) + { + BER_Decoder(bits) + .start_cons(SEQUENCE) + .decode(key->n) + .decode(key->e) + .verify_end() + .end_cons(); + + key->X509_load_hook(); + } + + IF_Scheme_Decoder(IF_Scheme_PublicKey* k) : key(k) {} + private: + IF_Scheme_PublicKey* key; + }; + + return new IF_Scheme_Decoder(this); + } + +/* +* Return the PKCS #8 public key encoder +*/ +PKCS8_Encoder* IF_Scheme_PrivateKey::pkcs8_encoder() const + { + class IF_Scheme_Encoder : public PKCS8_Encoder + { + public: + AlgorithmIdentifier alg_id() const + { + return AlgorithmIdentifier(key->get_oid(), + AlgorithmIdentifier::USE_NULL_PARAM); + } + + MemoryVector<byte> key_bits() const + { + return DER_Encoder() + .start_cons(SEQUENCE) + .encode(static_cast<u32bit>(0)) + .encode(key->n) + .encode(key->e) + .encode(key->d) + .encode(key->p) + .encode(key->q) + .encode(key->d1) + .encode(key->d2) + .encode(key->c) + .end_cons() + .get_contents(); + } + + IF_Scheme_Encoder(const IF_Scheme_PrivateKey* k) : key(k) {} + private: + const IF_Scheme_PrivateKey* key; + }; + + return new IF_Scheme_Encoder(this); + } + +/* +* Return the PKCS #8 public key decoder +*/ +PKCS8_Decoder* IF_Scheme_PrivateKey::pkcs8_decoder(RandomNumberGenerator& rng) + { + class IF_Scheme_Decoder : public PKCS8_Decoder + { + public: + void alg_id(const AlgorithmIdentifier&) {} + + void key_bits(const MemoryRegion<byte>& bits) + { + u32bit version; + + BER_Decoder(bits) + .start_cons(SEQUENCE) + .decode(version) + .decode(key->n) + .decode(key->e) + .decode(key->d) + .decode(key->p) + .decode(key->q) + .decode(key->d1) + .decode(key->d2) + .decode(key->c) + .end_cons(); + + if(version != 0) + throw Decoding_Error("Unknown PKCS #1 key format version"); + + key->PKCS8_load_hook(rng); + } + + IF_Scheme_Decoder(IF_Scheme_PrivateKey* k, RandomNumberGenerator& r) : + key(k), rng(r) {} + private: + IF_Scheme_PrivateKey* key; + RandomNumberGenerator& rng; + }; + + return new IF_Scheme_Decoder(this, rng); + } + +/* +* Algorithm Specific X.509 Initialization Code +*/ +void IF_Scheme_PublicKey::X509_load_hook() + { + core = IF_Core(e, n); + } + +/* +* Algorithm Specific PKCS #8 Initialization Code +*/ +void IF_Scheme_PrivateKey::PKCS8_load_hook(RandomNumberGenerator& rng, + bool generated) + { + if(n == 0) n = p * q; + if(d1 == 0) d1 = d % (p - 1); + if(d2 == 0) d2 = d % (q - 1); + if(c == 0) c = inverse_mod(q, p); + + core = IF_Core(rng, e, n, d, p, q, d1, d2, c); + + if(generated) + gen_check(rng); + else + load_check(rng); + } + +/* +* Check IF Scheme Public Parameters +*/ +bool IF_Scheme_PublicKey::check_key(RandomNumberGenerator&, bool) const + { + if(n < 35 || n.is_even() || e < 2) + return false; + return true; + } + +/* +* Check IF Scheme Private Parameters +*/ +bool IF_Scheme_PrivateKey::check_key(RandomNumberGenerator& rng, + bool strong) const + { + if(n < 35 || n.is_even() || e < 2 || d < 2 || p < 3 || q < 3 || p*q != n) + return false; + + if(!strong) + return true; + + if(d1 != d % (p - 1) || d2 != d % (q - 1) || c != inverse_mod(q, p)) + return false; + if(!check_prime(p, rng) || !check_prime(q, rng)) + return false; + return true; + } + +} diff --git a/botan/src/pubkey/if_algo/if_algo.h b/botan/src/pubkey/if_algo/if_algo.h new file mode 100644 index 0000000..32a29be --- /dev/null +++ b/botan/src/pubkey/if_algo/if_algo.h @@ -0,0 +1,85 @@ +/* +* IF Scheme +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_IF_ALGO_H__ +#define BOTAN_IF_ALGO_H__ + +#include <botan/if_core.h> +#include <botan/x509_key.h> +#include <botan/pkcs8.h> + +namespace Botan { + +/** +* This class represents public keys +* of integer factorization based (IF) public key schemes. +*/ +class BOTAN_DLL IF_Scheme_PublicKey : public virtual Public_Key + { + public: + bool check_key(RandomNumberGenerator& rng, bool) const; + + /** + * Get n = p * q. + * @return n + */ + const BigInt& get_n() const { return n; } + + /** + * Get the public exponent used by the key. + * @return the public exponent + */ + const BigInt& get_e() const { return e; } + + u32bit max_input_bits() const { return (n.bits() - 1); } + + X509_Encoder* x509_encoder() const; + X509_Decoder* x509_decoder(); + protected: + virtual void X509_load_hook(); + BigInt n, e; + IF_Core core; + }; + +/** +* This class represents public keys +* of integer factorization based (IF) public key schemes. +*/ +class BOTAN_DLL IF_Scheme_PrivateKey : public virtual IF_Scheme_PublicKey, + public virtual Private_Key + { + public: + bool check_key(RandomNumberGenerator& rng, bool) const; + + /** + * Get the first prime p. + * @return the prime p + */ + const BigInt& get_p() const { return p; } + + /** + * Get the second prime q. + * @return the prime q + */ + const BigInt& get_q() const { return q; } + + /** + * Get d with exp * d = 1 mod (p - 1, q - 1). + * @return d + */ + const BigInt& get_d() const { return d; } + + PKCS8_Encoder* pkcs8_encoder() const; + PKCS8_Decoder* pkcs8_decoder(RandomNumberGenerator&); + protected: + virtual void PKCS8_load_hook(RandomNumberGenerator&, bool = false); + BigInt d, p, q, d1, d2, c; + }; + +} + +#endif diff --git a/botan/src/pubkey/if_algo/if_core.cpp b/botan/src/pubkey/if_algo/if_core.cpp new file mode 100644 index 0000000..8cc6a81 --- /dev/null +++ b/botan/src/pubkey/if_algo/if_core.cpp @@ -0,0 +1,87 @@ +/* +* IF Algorithm Core +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/if_core.h> +#include <botan/numthry.h> +#include <botan/pk_engine.h> +#include <botan/parsing.h> +#include <algorithm> + +namespace Botan { + +namespace { + +const u32bit BLINDING_BITS = BOTAN_PRIVATE_KEY_OP_BLINDING_BITS; + +} + +/* +* IF_Core Constructor +*/ +IF_Core::IF_Core(const BigInt& e, const BigInt& n) + { + op = Engine_Core::if_op(e, n, 0, 0, 0, 0, 0, 0); + } + + +/* +* IF_Core Constructor +*/ +IF_Core::IF_Core(RandomNumberGenerator& rng, + const BigInt& e, const BigInt& n, const BigInt& d, + const BigInt& p, const BigInt& q, + const BigInt& d1, const BigInt& d2, const BigInt& c) + { + op = Engine_Core::if_op(e, n, d, p, q, d1, d2, c); + + if(BLINDING_BITS) + { + BigInt k(rng, std::min(n.bits()-1, BLINDING_BITS)); + blinder = Blinder(power_mod(k, e, n), inverse_mod(k, n), n); + } + } + +/* +* IF_Core Copy Constructor +*/ +IF_Core::IF_Core(const IF_Core& core) + { + op = 0; + if(core.op) + op = core.op->clone(); + blinder = core.blinder; + } + +/* +* IF_Core Assignment Operator +*/ +IF_Core& IF_Core::operator=(const IF_Core& core) + { + delete op; + if(core.op) + op = core.op->clone(); + blinder = core.blinder; + return (*this); + } + +/* +* IF Public Operation +*/ +BigInt IF_Core::public_op(const BigInt& i) const + { + return op->public_op(i); + } + +/* +* IF Private Operation +*/ +BigInt IF_Core::private_op(const BigInt& i) const + { + return blinder.unblind(op->private_op(blinder.blind(i))); + } + +} diff --git a/botan/src/pubkey/if_algo/if_core.h b/botan/src/pubkey/if_algo/if_core.h new file mode 100644 index 0000000..b7f4877 --- /dev/null +++ b/botan/src/pubkey/if_algo/if_core.h @@ -0,0 +1,45 @@ +/* +* IF Algorithm Core +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_IF_CORE_H__ +#define BOTAN_IF_CORE_H__ + +#include <botan/if_op.h> +#include <botan/blinding.h> + +namespace Botan { + +/* +* IF Core +*/ +class BOTAN_DLL IF_Core + { + public: + BigInt public_op(const BigInt&) const; + BigInt private_op(const BigInt&) const; + + IF_Core& operator=(const IF_Core&); + + IF_Core() { op = 0; } + IF_Core(const IF_Core&); + + IF_Core(const BigInt&, const BigInt&); + + IF_Core(RandomNumberGenerator& rng, + const BigInt&, const BigInt&, + const BigInt&, const BigInt&, const BigInt&, + const BigInt&, const BigInt&, const BigInt&); + + ~IF_Core() { delete op; } + private: + IF_Operation* op; + Blinder blinder; + }; + +} + +#endif diff --git a/botan/src/pubkey/if_algo/if_op.cpp b/botan/src/pubkey/if_algo/if_op.cpp new file mode 100644 index 0000000..27aef45 --- /dev/null +++ b/botan/src/pubkey/if_algo/if_op.cpp @@ -0,0 +1,47 @@ +/* +* IF (RSA/RW) Operation +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/if_op.h> +#include <botan/numthry.h> + +namespace Botan { + +/* +* Default_IF_Op Constructor +*/ +Default_IF_Op::Default_IF_Op(const BigInt& e, const BigInt& n, const BigInt&, + const BigInt& p, const BigInt& q, + const BigInt& d1, const BigInt& d2, + const BigInt& c) + { + powermod_e_n = Fixed_Exponent_Power_Mod(e, n); + + if(d1 != 0 && d2 != 0 && p != 0 && q != 0) + { + powermod_d1_p = Fixed_Exponent_Power_Mod(d1, p); + powermod_d2_q = Fixed_Exponent_Power_Mod(d2, q); + reducer = Modular_Reducer(p); + this->c = c; + this->q = q; + } + } + +/* +* Default IF Private Operation +*/ +BigInt Default_IF_Op::private_op(const BigInt& i) const + { + if(q == 0) + throw Internal_Error("Default_IF_Op::private_op: No private key"); + + BigInt j1 = powermod_d1_p(i); + BigInt j2 = powermod_d2_q(i); + j1 = reducer.reduce(sub_mul(j1, j2, c)); + return mul_add(j1, q, j2); + } + +} diff --git a/botan/src/pubkey/if_algo/if_op.h b/botan/src/pubkey/if_algo/if_op.h new file mode 100644 index 0000000..516902f --- /dev/null +++ b/botan/src/pubkey/if_algo/if_op.h @@ -0,0 +1,52 @@ +/* +* IF Operations +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_IF_OP_H__ +#define BOTAN_IF_OP_H__ + +#include <botan/bigint.h> +#include <botan/pow_mod.h> +#include <botan/reducer.h> + +namespace Botan { + +/* +* IF Operation +*/ +class BOTAN_DLL IF_Operation + { + public: + virtual BigInt public_op(const BigInt&) const = 0; + virtual BigInt private_op(const BigInt&) const = 0; + virtual IF_Operation* clone() const = 0; + virtual ~IF_Operation() {} + }; + +/* +* Default IF Operation +*/ +class BOTAN_DLL Default_IF_Op : public IF_Operation + { + public: + BigInt public_op(const BigInt& i) const + { return powermod_e_n(i); } + BigInt private_op(const BigInt&) const; + + IF_Operation* clone() const { return new Default_IF_Op(*this); } + + Default_IF_Op(const BigInt&, const BigInt&, const BigInt&, + const BigInt&, const BigInt&, const BigInt&, + const BigInt&, const BigInt&); + private: + Fixed_Exponent_Power_Mod powermod_e_n, powermod_d1_p, powermod_d2_q; + Modular_Reducer reducer; + BigInt c, q; + }; + +} + +#endif diff --git a/botan/src/pubkey/if_algo/info.txt b/botan/src/pubkey/if_algo/info.txt new file mode 100644 index 0000000..d2142f4 --- /dev/null +++ b/botan/src/pubkey/if_algo/info.txt @@ -0,0 +1,22 @@ +realname "Integer Factorization Algorithms" + +define IF_PUBLIC_KEY_FAMILY + +load_on dep + +<add> +if_algo.cpp +if_algo.h +if_core.cpp +if_core.h +if_op.cpp +if_op.h +</add> + +<requires> +asn1 +bigint +libstate +numbertheory +pk_codecs +</requires> diff --git a/botan/src/pubkey/info.txt b/botan/src/pubkey/info.txt new file mode 100644 index 0000000..ee8da5b --- /dev/null +++ b/botan/src/pubkey/info.txt @@ -0,0 +1,30 @@ +realname "Public Key Base" + +define PUBLIC_KEY_CRYPTO + +load_on auto + +<add> +pk_algs.cpp +pk_algs.h +pk_filts.cpp +pk_filts.h +pk_keys.cpp +pk_keys.h +pubkey.cpp +pubkey.h +pubkey_enums.cpp +pubkey_enums.h +</add> + +<requires> +alloc +asn1 +bigint +filters +kdf +oid_lookup +pk_pad +rng +sym_algo +</requires> diff --git a/botan/src/pubkey/keypair/info.txt b/botan/src/pubkey/keypair/info.txt new file mode 100644 index 0000000..9e75864 --- /dev/null +++ b/botan/src/pubkey/keypair/info.txt @@ -0,0 +1,14 @@ +realname "Keypair Testing" + +define KEYPAIR_TESTING + +load_on auto + +<add> +keypair.cpp +keypair.h +</add> + +<requires> +libstate +</requires> diff --git a/botan/src/pubkey/keypair/keypair.cpp b/botan/src/pubkey/keypair/keypair.cpp new file mode 100644 index 0000000..486577f --- /dev/null +++ b/botan/src/pubkey/keypair/keypair.cpp @@ -0,0 +1,73 @@ +/* +* Keypair Checks +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/keypair.h> +#include <botan/look_pk.h> +#include <memory> + +namespace Botan { + +namespace KeyPair { + +/* +* Check an encryption key pair for consistency +*/ +void check_key(RandomNumberGenerator& rng, + PK_Encryptor* encryptor, PK_Decryptor* decryptor) + { + if(encryptor->maximum_input_size() == 0) + return; + + std::auto_ptr<PK_Encryptor> enc(encryptor); + std::auto_ptr<PK_Decryptor> dec(decryptor); + + SecureVector<byte> message(enc->maximum_input_size() - 1); + rng.randomize(message, message.size()); + + SecureVector<byte> ciphertext = enc->encrypt(message, rng); + if(ciphertext == message) + throw Self_Test_Failure("Encryption key pair consistency failure"); + + SecureVector<byte> message2 = dec->decrypt(ciphertext); + if(message != message2) + throw Self_Test_Failure("Encryption key pair consistency failure"); + } + +/* +* Check a signature key pair for consistency +*/ +void check_key(RandomNumberGenerator& rng, + PK_Signer* signer, PK_Verifier* verifier) + { + std::auto_ptr<PK_Signer> sig(signer); + std::auto_ptr<PK_Verifier> ver(verifier); + + SecureVector<byte> message(16); + rng.randomize(message, message.size()); + + SecureVector<byte> signature; + + try + { + signature = sig->sign_message(message, rng); + } + catch(Encoding_Error) + { + return; + } + + if(!ver->verify_message(message, signature)) + throw Self_Test_Failure("Signature key pair consistency failure"); + + ++message[0]; + if(ver->verify_message(message, signature)) + throw Self_Test_Failure("Signature key pair consistency failure"); + } + +} + +} diff --git a/botan/src/pubkey/keypair/keypair.h b/botan/src/pubkey/keypair/keypair.h new file mode 100644 index 0000000..b1d5c2d --- /dev/null +++ b/botan/src/pubkey/keypair/keypair.h @@ -0,0 +1,47 @@ +/* +* Keypair Checks +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_KEYPAIR_H__ +#define BOTAN_KEYPAIR_H__ + +#include <botan/pubkey.h> + +namespace Botan { + +namespace KeyPair { + +/** +* Tests whether the specified encryptor and decryptor are related to each other, +* i.e. whether encrypting with the encryptor and consecutive decryption leads to +* the original plaintext. +* @param rng the rng to use +* @param enc the encryptor to test +* @param dec the decryptor to test +* @throw Self_Test_Failure if the arguments are not related to each other +*/ +BOTAN_DLL void check_key(RandomNumberGenerator& rng, + PK_Encryptor* enc, + PK_Decryptor* dec); + +/** +* Tests whether the specified signer and verifier are related to each other, +* i.e. whether a signature created with the signer and can be +* successfully verified with the verifier. +* @param rng the rng to use +* @param sig the signer to test +* @param ver the verifier to test +* @throw Self_Test_Failure if the arguments are not related to each other +*/ +BOTAN_DLL void check_key(RandomNumberGenerator& rng, + PK_Signer* sig, + PK_Verifier* ver); + +} + +} + +#endif diff --git a/botan/src/pubkey/nr/info.txt b/botan/src/pubkey/nr/info.txt new file mode 100644 index 0000000..c89820a --- /dev/null +++ b/botan/src/pubkey/nr/info.txt @@ -0,0 +1,22 @@ +realname "Nyberg-Rueppel" + +define NYBERG_RUEPPEL + +load_on auto + +<add> +nr.cpp +nr.h +nr_core.cpp +nr_core.h +nr_op.cpp +nr_op.h +</add> + +<requires> +dl_algo +dl_group +keypair +libstate +numbertheory +</requires> diff --git a/botan/src/pubkey/nr/nr.cpp b/botan/src/pubkey/nr/nr.cpp new file mode 100644 index 0000000..ad4ae78 --- /dev/null +++ b/botan/src/pubkey/nr/nr.cpp @@ -0,0 +1,134 @@ +/* +* Nyberg-Rueppel +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/nr.h> +#include <botan/numthry.h> +#include <botan/keypair.h> +#include <botan/look_pk.h> + +namespace Botan { + +/* +* NR_PublicKey Constructor +*/ +NR_PublicKey::NR_PublicKey(const DL_Group& grp, const BigInt& y1) + { + group = grp; + y = y1; + X509_load_hook(); + } + +/* +* Algorithm Specific X.509 Initialization Code +*/ +void NR_PublicKey::X509_load_hook() + { + core = NR_Core(group, y); + } + +/* +* Nyberg-Rueppel Verification Function +*/ +SecureVector<byte> NR_PublicKey::verify(const byte sig[], u32bit sig_len) const + { + return core.verify(sig, sig_len); + } + +/* +* Return the maximum input size in bits +*/ +u32bit NR_PublicKey::max_input_bits() const + { + return (group_q().bits() - 1); + } + +/* +* Return the size of each portion of the sig +*/ +u32bit NR_PublicKey::message_part_size() const + { + return group_q().bytes(); + } + +/* +* Create a NR private key +*/ +NR_PrivateKey::NR_PrivateKey(RandomNumberGenerator& rng, + const DL_Group& grp, + const BigInt& x_arg) + { + group = grp; + x = x_arg; + + if(x == 0) + { + x = BigInt::random_integer(rng, 2, group_q() - 1); + PKCS8_load_hook(rng, true); + } + else + PKCS8_load_hook(rng, false); + } + +/* +* Algorithm Specific PKCS #8 Initialization Code +*/ +void NR_PrivateKey::PKCS8_load_hook(RandomNumberGenerator& rng, + bool generated) + { + if(y == 0) + y = power_mod(group_g(), x, group_p()); + core = NR_Core(group, y, x); + + if(generated) + gen_check(rng); + else + load_check(rng); + } + +/* +* Nyberg-Rueppel Signature Operation +*/ +SecureVector<byte> NR_PrivateKey::sign(const byte in[], u32bit length, + RandomNumberGenerator& rng) const + { + const BigInt& q = group_q(); + + BigInt k; + do + k.randomize(rng, q.bits()); + while(k >= q); + + return core.sign(in, length, k); + } + +/* +* Check Private Nyberg-Rueppel Parameters +*/ +bool NR_PrivateKey::check_key(RandomNumberGenerator& rng, bool strong) const + { + if(!DL_Scheme_PrivateKey::check_key(rng, strong) || x >= group_q()) + return false; + + if(!strong) + return true; + + try + { + KeyPair::check_key(rng, + get_pk_signer(*this, "EMSA1(SHA-1)"), + get_pk_verifier(*this, "EMSA1(SHA-1)") + ); + } + catch(Self_Test_Failure) + { + return false; + } + + return true; + } + +} diff --git a/botan/src/pubkey/nr/nr.h b/botan/src/pubkey/nr/nr.h new file mode 100644 index 0000000..144c5ec --- /dev/null +++ b/botan/src/pubkey/nr/nr.h @@ -0,0 +1,63 @@ +/* +* Nyberg-Rueppel +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_NYBERG_RUEPPEL_H__ +#define BOTAN_NYBERG_RUEPPEL_H__ + +#include <botan/dl_algo.h> +#include <botan/nr_core.h> + +namespace Botan { + +/* +* Nyberg-Rueppel Public Key +*/ +class BOTAN_DLL NR_PublicKey : public PK_Verifying_with_MR_Key, + public virtual DL_Scheme_PublicKey + { + public: + std::string algo_name() const { return "NR"; } + + SecureVector<byte> verify(const byte[], u32bit) const; + u32bit max_input_bits() const; + + DL_Group::Format group_format() const { return DL_Group::ANSI_X9_57; } + u32bit message_parts() const { return 2; } + u32bit message_part_size() const; + + NR_PublicKey() {} + NR_PublicKey(const DL_Group&, const BigInt&); + protected: + NR_Core core; + private: + void X509_load_hook(); + }; + +/* +* Nyberg-Rueppel Private Key +*/ +class BOTAN_DLL NR_PrivateKey : public NR_PublicKey, + public PK_Signing_Key, + public virtual DL_Scheme_PrivateKey + { + public: + SecureVector<byte> sign(const byte[], u32bit, + RandomNumberGenerator& rng) const; + + bool check_key(RandomNumberGenerator& rng, bool) const; + + NR_PrivateKey() {} + + NR_PrivateKey(RandomNumberGenerator&, const DL_Group&, + const BigInt& = 0); + private: + void PKCS8_load_hook(RandomNumberGenerator&, bool = false); + }; + +} + +#endif diff --git a/botan/src/pubkey/nr/nr_core.cpp b/botan/src/pubkey/nr/nr_core.cpp new file mode 100644 index 0000000..afa1115 --- /dev/null +++ b/botan/src/pubkey/nr/nr_core.cpp @@ -0,0 +1,62 @@ +/* +* NR Core +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/nr_core.h> +#include <botan/numthry.h> +#include <botan/pk_engine.h> +#include <botan/parsing.h> +#include <algorithm> + +namespace Botan { + +/* +* NR_Core Constructor +*/ +NR_Core::NR_Core(const DL_Group& group, const BigInt& y, const BigInt& x) + { + op = Engine_Core::nr_op(group, y, x); + } + +/* +* NR_Core Copy Constructor +*/ +NR_Core::NR_Core(const NR_Core& core) + { + op = 0; + if(core.op) + op = core.op->clone(); + } + +/* +* NR_Core Assignment Operator +*/ +NR_Core& NR_Core::operator=(const NR_Core& core) + { + delete op; + if(core.op) + op = core.op->clone(); + return (*this); + } + +/* +* NR Verification Operation +*/ +SecureVector<byte> NR_Core::verify(const byte in[], u32bit length) const + { + return op->verify(in, length); + } + +/* +* NR Signature Operation +*/ +SecureVector<byte> NR_Core::sign(const byte in[], u32bit length, + const BigInt& k) const + { + return op->sign(in, length, k); + } + +} diff --git a/botan/src/pubkey/nr/nr_core.h b/botan/src/pubkey/nr/nr_core.h new file mode 100644 index 0000000..4837736 --- /dev/null +++ b/botan/src/pubkey/nr/nr_core.h @@ -0,0 +1,37 @@ +/* +* NR Core +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_NR_CORE_H__ +#define BOTAN_NR_CORE_H__ + +#include <botan/nr_op.h> +#include <botan/dl_group.h> + +namespace Botan { + +/* +* NR Core +*/ +class BOTAN_DLL NR_Core + { + public: + SecureVector<byte> sign(const byte[], u32bit, const BigInt&) const; + SecureVector<byte> verify(const byte[], u32bit) const; + + NR_Core& operator=(const NR_Core&); + + NR_Core() { op = 0; } + NR_Core(const NR_Core&); + NR_Core(const DL_Group&, const BigInt&, const BigInt& = 0); + ~NR_Core() { delete op; } + private: + NR_Operation* op; + }; + +} + +#endif diff --git a/botan/src/pubkey/nr/nr_op.cpp b/botan/src/pubkey/nr/nr_op.cpp new file mode 100644 index 0000000..b5efa3d --- /dev/null +++ b/botan/src/pubkey/nr/nr_op.cpp @@ -0,0 +1,71 @@ +/* +* NR Operations +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/nr_op.h> + +namespace Botan { + +/* +* Default_NR_Op Constructor +*/ +Default_NR_Op::Default_NR_Op(const DL_Group& grp, const BigInt& y1, + const BigInt& x1) : x(x1), y(y1), group(grp) + { + powermod_g_p = Fixed_Base_Power_Mod(group.get_g(), group.get_p()); + powermod_y_p = Fixed_Base_Power_Mod(y, group.get_p()); + mod_p = Modular_Reducer(group.get_p()); + mod_q = Modular_Reducer(group.get_q()); + } + +/* +* Default NR Verify Operation +*/ +SecureVector<byte> Default_NR_Op::verify(const byte in[], u32bit length) const + { + const BigInt& q = group.get_q(); + + if(length != 2*q.bytes()) + return false; + + BigInt c(in, q.bytes()); + BigInt d(in + q.bytes(), q.bytes()); + + if(c.is_zero() || c >= q || d >= q) + throw Invalid_Argument("Default_NR_Op::verify: Invalid signature"); + + BigInt i = mod_p.multiply(powermod_g_p(d), powermod_y_p(c)); + return BigInt::encode(mod_q.reduce(c - i)); + } + +/* +* Default NR Sign Operation +*/ +SecureVector<byte> Default_NR_Op::sign(const byte in[], u32bit length, + const BigInt& k) const + { + if(x == 0) + throw Internal_Error("Default_NR_Op::sign: No private key"); + + const BigInt& q = group.get_q(); + + BigInt f(in, length); + + if(f >= q) + throw Invalid_Argument("Default_NR_Op::sign: Input is out of range"); + + BigInt c = mod_q.reduce(powermod_g_p(k) + f); + if(c.is_zero()) + throw Internal_Error("Default_NR_Op::sign: c was zero"); + BigInt d = mod_q.reduce(k - x * c); + + SecureVector<byte> output(2*q.bytes()); + c.binary_encode(output + (output.size() / 2 - c.bytes())); + d.binary_encode(output + (output.size() - d.bytes())); + return output; + } + +} diff --git a/botan/src/pubkey/nr/nr_op.h b/botan/src/pubkey/nr/nr_op.h new file mode 100644 index 0000000..cba1465 --- /dev/null +++ b/botan/src/pubkey/nr/nr_op.h @@ -0,0 +1,53 @@ +/* +* NR Operations +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_NR_OPS_H__ +#define BOTAN_NR_OPS_H__ + +#include <botan/pow_mod.h> +#include <botan/numthry.h> +#include <botan/reducer.h> +#include <botan/dl_group.h> + +namespace Botan { + +/* +* NR Operation +*/ +class BOTAN_DLL NR_Operation + { + public: + virtual SecureVector<byte> verify(const byte[], u32bit) const = 0; + virtual SecureVector<byte> sign(const byte[], u32bit, + const BigInt&) const = 0; + virtual NR_Operation* clone() const = 0; + virtual ~NR_Operation() {} + }; + +/* +* Botan's Default NR Operation +*/ +class BOTAN_DLL Default_NR_Op : public NR_Operation + { + public: + SecureVector<byte> verify(const byte[], u32bit) const; + SecureVector<byte> sign(const byte[], u32bit, const BigInt&) const; + + NR_Operation* clone() const { return new Default_NR_Op(*this); } + + Default_NR_Op(const DL_Group&, const BigInt&, const BigInt&); + private: + const BigInt x, y; + const DL_Group group; + Fixed_Base_Power_Mod powermod_g_p, powermod_y_p; + Modular_Reducer mod_p, mod_q; + }; + + +} + +#endif diff --git a/botan/src/pubkey/pk_algs.cpp b/botan/src/pubkey/pk_algs.cpp new file mode 100644 index 0000000..99d7294 --- /dev/null +++ b/botan/src/pubkey/pk_algs.cpp @@ -0,0 +1,112 @@ +/* +* PK Key +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/pk_algs.h> + +#if defined(BOTAN_HAS_RSA) + #include <botan/rsa.h> +#endif + +#if defined(BOTAN_HAS_DSA) + #include <botan/dsa.h> +#endif + +#if defined(BOTAN_HAS_DIFFIE_HELLMAN) + #include <botan/dh.h> +#endif + +#if defined(BOTAN_HAS_ECDSA) + #include <botan/ecdsa.h> +#endif + +#if defined(BOTAN_HAS_NYBERG_RUEPPEL) + #include <botan/nr.h> +#endif + +#if defined(BOTAN_HAS_RW) + #include <botan/rw.h> +#endif + +#if defined(BOTAN_HAS_ELGAMAL) + #include <botan/elgamal.h> +#endif + +namespace Botan { + +/* +* Get an PK public key object +*/ +Public_Key* get_public_key(const std::string& alg_name) + { +#if defined(BOTAN_HAS_RSA) + if(alg_name == "RSA") return new RSA_PublicKey; +#endif + +#if defined(BOTAN_HAS_DSA) + if(alg_name == "DSA") return new DSA_PublicKey; +#endif + +#if defined(BOTAN_HAS_DIFFIE_HELLMAN) + if(alg_name == "DH") return new DH_PublicKey; +#endif + +#if defined(BOTAN_HAS_NYBERG_RUEPPEL) + if(alg_name == "NR") return new NR_PublicKey; +#endif + +#if defined(BOTAN_HAS_RW) + if(alg_name == "RW") return new RW_PublicKey; +#endif + +#if defined(BOTAN_HAS_ELG) + if(alg_name == "ELG") return new ElGamal_PublicKey; +#endif + +#if defined(BOTAN_HAS_ECDSA) + if(alg_name == "ECDSA") return new ECDSA_PublicKey; +#endif + + return 0; + } + +/* +* Get an PK private key object +*/ +Private_Key* get_private_key(const std::string& alg_name) + { +#if defined(BOTAN_HAS_RSA) + if(alg_name == "RSA") return new RSA_PrivateKey; +#endif + +#if defined(BOTAN_HAS_DSA) + if(alg_name == "DSA") return new DSA_PrivateKey; +#endif + +#if defined(BOTAN_HAS_DIFFIE_HELLMAN) + if(alg_name == "DH") return new DH_PrivateKey; +#endif + +#if defined(BOTAN_HAS_NYBERG_RUEPPEL) + if(alg_name == "NR") return new NR_PrivateKey; +#endif + +#if defined(BOTAN_HAS_RW) + if(alg_name == "RW") return new RW_PrivateKey; +#endif + +#if defined(BOTAN_HAS_ELG) + if(alg_name == "ELG") return new ElGamal_PrivateKey; +#endif + +#if defined(BOTAN_HAS_ECDSA) + if(alg_name == "ECDSA") return new ECDSA_PrivateKey; +#endif + + return 0; + } + +} diff --git a/botan/src/pubkey/pk_algs.h b/botan/src/pubkey/pk_algs.h new file mode 100644 index 0000000..c41bf1a --- /dev/null +++ b/botan/src/pubkey/pk_algs.h @@ -0,0 +1,31 @@ +/* +* PK Key Factory +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_PK_KEY_FACTORY_H__ +#define BOTAN_PK_KEY_FACTORY_H__ + +#include <botan/pk_keys.h> + +namespace Botan { + +/** +* Get an empty public key object. +* @param name the name of the desired public key algorithm +* @return the public key object +*/ +BOTAN_DLL Public_Key* get_public_key(const std::string&); + +/** +* Get an empty private key object. +* @param name the name of the desired public key algorithm +* @return the private key object +*/ +BOTAN_DLL Private_Key* get_private_key(const std::string&); + +} + +#endif diff --git a/botan/src/pubkey/pk_codecs/info.txt b/botan/src/pubkey/pk_codecs/info.txt new file mode 100644 index 0000000..96511a6 --- /dev/null +++ b/botan/src/pubkey/pk_codecs/info.txt @@ -0,0 +1,18 @@ +realname "PK codecs (PKCS8, X.509)" + +load_on auto + +<add> +pkcs8.h +pkcs8.cpp +x509_key.h +x509_key.cpp +</add> + +<requires> +asn1 +filters +oid_lookup +pbe +pem +</requires> diff --git a/botan/src/pubkey/pk_codecs/pkcs8.cpp b/botan/src/pubkey/pk_codecs/pkcs8.cpp new file mode 100644 index 0000000..8a464ec --- /dev/null +++ b/botan/src/pubkey/pk_codecs/pkcs8.cpp @@ -0,0 +1,313 @@ +/* +* PKCS #8 +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/pkcs8.h> +#include <botan/get_pbe.h> +#include <botan/der_enc.h> +#include <botan/ber_dec.h> +#include <botan/asn1_obj.h> +#include <botan/pk_algs.h> +#include <botan/oids.h> +#include <botan/pem.h> +#include <memory> + +namespace Botan { + +namespace PKCS8 { + +namespace { + +/* +* Get info from an EncryptedPrivateKeyInfo +*/ +SecureVector<byte> PKCS8_extract(DataSource& source, + AlgorithmIdentifier& pbe_alg_id) + { + SecureVector<byte> key_data; + + BER_Decoder(source) + .start_cons(SEQUENCE) + .decode(pbe_alg_id) + .decode(key_data, OCTET_STRING) + .verify_end(); + + return key_data; + } + +/* +* PEM decode and/or decrypt a private key +*/ +SecureVector<byte> PKCS8_decode(DataSource& source, const User_Interface& ui, + AlgorithmIdentifier& pk_alg_id) + { + AlgorithmIdentifier pbe_alg_id; + SecureVector<byte> key_data, key; + bool is_encrypted = true; + + try { + if(ASN1::maybe_BER(source) && !PEM_Code::matches(source)) + key_data = PKCS8_extract(source, pbe_alg_id); + else + { + std::string label; + key_data = PEM_Code::decode(source, label); + if(label == "PRIVATE KEY") + is_encrypted = false; + else if(label == "ENCRYPTED PRIVATE KEY") + { + DataSource_Memory key_source(key_data); + key_data = PKCS8_extract(key_source, pbe_alg_id); + } + else + throw PKCS8_Exception("Unknown PEM label " + label); + } + + if(key_data.is_empty()) + throw PKCS8_Exception("No key data found"); + } + catch(Decoding_Error) + { + throw Decoding_Error("PKCS #8 private key decoding failed"); + } + + if(!is_encrypted) + key = key_data; + + const u32bit MAX_TRIES = 3; + + u32bit tries = 0; + while(true) + { + try { + if(MAX_TRIES && tries >= MAX_TRIES) + break; + + if(is_encrypted) + { + DataSource_Memory params(pbe_alg_id.parameters); + std::auto_ptr<PBE> pbe(get_pbe(pbe_alg_id.oid, params)); + + User_Interface::UI_Result result = User_Interface::OK; + const std::string passphrase = + ui.get_passphrase("PKCS #8 private key", source.id(), result); + + if(result == User_Interface::CANCEL_ACTION) + break; + + pbe->set_key(passphrase); + Pipe decryptor(pbe.release()); + + decryptor.process_msg(key_data, key_data.size()); + key = decryptor.read_all(); + } + + u32bit version; + + BER_Decoder(key) + .start_cons(SEQUENCE) + .decode(version) + .decode(pk_alg_id) + .decode(key, OCTET_STRING) + .discard_remaining() + .end_cons(); + + if(version != 0) + throw Decoding_Error("PKCS #8: Unknown version number"); + + break; + } + catch(Decoding_Error) + { + ++tries; + } + } + + if(key.is_empty()) + throw Decoding_Error("PKCS #8 private key decoding failed"); + return key; + } + +} + +/* +* DER or PEM encode a PKCS #8 private key +*/ +void encode(const Private_Key& key, Pipe& pipe, X509_Encoding encoding) + { + std::auto_ptr<PKCS8_Encoder> encoder(key.pkcs8_encoder()); + if(!encoder.get()) + throw Encoding_Error("PKCS8::encode: Key does not support encoding"); + + const u32bit PKCS8_VERSION = 0; + + SecureVector<byte> contents = + DER_Encoder() + .start_cons(SEQUENCE) + .encode(PKCS8_VERSION) + .encode(encoder->alg_id()) + .encode(encoder->key_bits(), OCTET_STRING) + .end_cons() + .get_contents(); + + if(encoding == PEM) + pipe.write(PEM_Code::encode(contents, "PRIVATE KEY")); + else + pipe.write(contents); + } + +/* +* Encode and encrypt a PKCS #8 private key +*/ +void encrypt_key(const Private_Key& key, + Pipe& pipe, + RandomNumberGenerator& rng, + const std::string& pass, const std::string& pbe_algo, + X509_Encoding encoding) + { + const std::string DEFAULT_PBE = "PBE-PKCS5v20(SHA-1,TripleDES/CBC)"; + + Pipe raw_key; + raw_key.start_msg(); + encode(key, raw_key, RAW_BER); + raw_key.end_msg(); + + std::auto_ptr<PBE> pbe(get_pbe(((pbe_algo != "") ? pbe_algo : DEFAULT_PBE))); + + pbe->new_params(rng); + pbe->set_key(pass); + + AlgorithmIdentifier pbe_algid(pbe->get_oid(), pbe->encode_params()); + + Pipe key_encrytor(pbe.release()); + key_encrytor.process_msg(raw_key); + + SecureVector<byte> enc_key = + DER_Encoder() + .start_cons(SEQUENCE) + .encode(pbe_algid) + .encode(key_encrytor.read_all(), OCTET_STRING) + .end_cons() + .get_contents(); + + if(encoding == PEM) + pipe.write(PEM_Code::encode(enc_key, "ENCRYPTED PRIVATE KEY")); + else + pipe.write(enc_key); + } + +/* +* PEM encode a PKCS #8 private key +*/ +std::string PEM_encode(const Private_Key& key) + { + Pipe pem; + pem.start_msg(); + encode(key, pem, PEM); + pem.end_msg(); + return pem.read_all_as_string(); + } + +/* +* Encrypt and PEM encode a PKCS #8 private key +*/ +std::string PEM_encode(const Private_Key& key, + RandomNumberGenerator& rng, + const std::string& pass, + const std::string& pbe_algo) + { + if(pass == "") + return PEM_encode(key); + + Pipe pem; + pem.start_msg(); + encrypt_key(key, pem, rng, pass, pbe_algo, PEM); + pem.end_msg(); + return pem.read_all_as_string(); + } + +/* +* Extract a private key and return it +*/ +Private_Key* load_key(DataSource& source, + RandomNumberGenerator& rng, + const User_Interface& ui) + { + AlgorithmIdentifier alg_id; + SecureVector<byte> pkcs8_key = PKCS8_decode(source, ui, alg_id); + + const std::string alg_name = OIDS::lookup(alg_id.oid); + if(alg_name == "" || alg_name == alg_id.oid.as_string()) + throw PKCS8_Exception("Unknown algorithm OID: " + + alg_id.oid.as_string()); + + std::auto_ptr<Private_Key> key(get_private_key(alg_name)); + + if(!key.get()) + throw PKCS8_Exception("Unknown PK algorithm/OID: " + alg_name + ", " + + alg_id.oid.as_string()); + + std::auto_ptr<PKCS8_Decoder> decoder(key->pkcs8_decoder(rng)); + + if(!decoder.get()) + throw Decoding_Error("Key does not support PKCS #8 decoding"); + + decoder->alg_id(alg_id); + decoder->key_bits(pkcs8_key); + + return key.release(); + } + +/* +* Extract a private key and return it +*/ +Private_Key* load_key(const std::string& fsname, + RandomNumberGenerator& rng, + const User_Interface& ui) + { + DataSource_Stream source(fsname, true); + return PKCS8::load_key(source, rng, ui); + } + +/* +* Extract a private key and return it +*/ +Private_Key* load_key(DataSource& source, + RandomNumberGenerator& rng, + const std::string& pass) + { + return PKCS8::load_key(source, rng, User_Interface(pass)); + } + +/* +* Extract a private key and return it +*/ +Private_Key* load_key(const std::string& fsname, + RandomNumberGenerator& rng, + const std::string& pass) + { + return PKCS8::load_key(fsname, rng, User_Interface(pass)); + } + +/* +* Make a copy of this private key +*/ +Private_Key* copy_key(const Private_Key& key, + RandomNumberGenerator& rng) + { + Pipe bits; + + bits.start_msg(); + PKCS8::encode(key, bits); + bits.end_msg(); + + DataSource_Memory source(bits.read_all()); + return PKCS8::load_key(source, rng); + } + +} + +} diff --git a/botan/src/pubkey/pk_codecs/pkcs8.h b/botan/src/pubkey/pk_codecs/pkcs8.h new file mode 100644 index 0000000..28008bd --- /dev/null +++ b/botan/src/pubkey/pk_codecs/pkcs8.h @@ -0,0 +1,177 @@ +/* +* PKCS #8 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_PKCS8_H__ +#define BOTAN_PKCS8_H__ + +#include <botan/x509_key.h> +#include <botan/ui.h> + +namespace Botan { + +/** +* PKCS #8 Private Key Encoder. +*/ +class BOTAN_DLL PKCS8_Encoder + { + public: + /** + * Get the algorithm identifier associated with the scheme + * this encoders key is part of. + * @return the algorithm identifier + */ + virtual AlgorithmIdentifier alg_id() const = 0; + + /** + * Get the DER encoded key. + * @return the DER encoded key + */ + // FIXME: Why not SecureVector? + virtual MemoryVector<byte> key_bits() const = 0; + virtual ~PKCS8_Encoder() {} + }; + +/* +* PKCS #8 Private Key Decoder +*/ +class BOTAN_DLL PKCS8_Decoder + { + public: + /** + * Set the algorithm identifier associated with the scheme + * this decoders key is part of. + * @param alg_id the algorithm identifier + */ + virtual void alg_id(const AlgorithmIdentifier&) = 0; + + /** + * Set the DER encoded key. + * @param key the DER encoded key + */ + virtual void key_bits(const MemoryRegion<byte>&) = 0; + virtual ~PKCS8_Decoder() {} + }; + +/** +* PKCS #8 General Exception +*/ +struct BOTAN_DLL PKCS8_Exception : public Decoding_Error + { + PKCS8_Exception(const std::string& error) : + Decoding_Error("PKCS #8: " + error) {} + }; + +namespace PKCS8 { + +/** +* Encode a private key into a pipe. +* @param key the private key to encode +* @param pipe the pipe to feed the encoded key into +* @param enc the encoding type to use +*/ +BOTAN_DLL void encode(const Private_Key& key, Pipe& pipe, + X509_Encoding enc = PEM); + +/** +* Encode and encrypt a private key into a pipe. +* @param key the private key to encode +* @param pipe the pipe to feed the encoded key into +* @param pass the password to use for encryption +* @param rng the rng to use +* @param pbe_algo the name of the desired password-based encryption algorithm. +* Provide an empty string to use the default PBE defined in the configuration +* under base/default_pbe. +* @param enc the encoding type to use +*/ +BOTAN_DLL void encrypt_key(const Private_Key& key, + Pipe& pipe, + RandomNumberGenerator& rng, + const std::string& pass, + const std::string& pbe_algo = "", + X509_Encoding enc = PEM); + + +/** +* Get a string containing a PEM encoded private key. +* @param key the key to encode +* @return the encoded key +*/ +BOTAN_DLL std::string PEM_encode(const Private_Key& key); + +/** +* Get a string containing a PEM encoded private key, encrypting it with a +* password. +* @param key the key to encode +* @param rng the rng to use +* @param pass the password to use for encryption +* @param pbe_algo the name of the desired password-based encryption algorithm. +* Provide an empty string to use the default PBE defined in the configuration +* under base/default_pbe. +*/ +BOTAN_DLL std::string PEM_encode(const Private_Key& key, + RandomNumberGenerator& rng, + const std::string& pass, + const std::string& pbe_algo = ""); + +/** +* Load a key from a data source. +* @param source the data source providing the encoded key +* @param rng the rng to use +* @param ui the user interface to be used for passphrase dialog +* @return the loaded private key object +*/ +BOTAN_DLL Private_Key* load_key(DataSource& source, + RandomNumberGenerator& rng, + const User_Interface& ui); + +/** Load a key from a data source. +* @param source the data source providing the encoded key +* @param rng the rng to use +* @param pass the passphrase to decrypt the key. Provide an empty +* string if the key is not encoded. +* @return the loaded private key object +*/ +BOTAN_DLL Private_Key* load_key(DataSource& source, + RandomNumberGenerator& rng, + const std::string& pass = ""); + +/** +* Load a key from a file. +* @param filename the path to the file containing the encoded key +* @param rng the rng to use +* @param ui the user interface to be used for passphrase dialog +* @return the loaded private key object +*/ +BOTAN_DLL Private_Key* load_key(const std::string& filename, + RandomNumberGenerator& rng, + const User_Interface& ui); + +/** Load a key from a file. +* @param filename the path to the file containing the encoded key +* @param rng the rng to use +* @param pass the passphrase to decrypt the key. Provide an empty +* string if the key is not encoded. +* @return the loaded private key object +*/ +BOTAN_DLL Private_Key* load_key(const std::string& filename, + RandomNumberGenerator& rng, + const std::string& pass = ""); + +/** +* Copy an existing encoded key object. +* @param key the key to copy +* @param rng the rng to use +* @return the new copy of the key +*/ +BOTAN_DLL Private_Key* copy_key(const Private_Key& key, + RandomNumberGenerator& rng); + +} + +} + +#endif diff --git a/botan/src/pubkey/pk_codecs/x509_key.cpp b/botan/src/pubkey/pk_codecs/x509_key.cpp new file mode 100644 index 0000000..455e627 --- /dev/null +++ b/botan/src/pubkey/pk_codecs/x509_key.cpp @@ -0,0 +1,176 @@ +/* +* X.509 Public Key +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/x509_key.h> +#include <botan/filters.h> +#include <botan/asn1_obj.h> +#include <botan/der_enc.h> +#include <botan/ber_dec.h> +#include <botan/pk_algs.h> +#include <botan/oids.h> +#include <botan/pem.h> +#include <memory> + +namespace Botan { + +namespace X509 { + +/* +* DER or PEM encode a X.509 public key +*/ +void encode(const Public_Key& key, Pipe& pipe, X509_Encoding encoding) + { + std::auto_ptr<X509_Encoder> encoder(key.x509_encoder()); + if(!encoder.get()) + throw Encoding_Error("X509::encode: Key does not support encoding"); + + MemoryVector<byte> der = + DER_Encoder() + .start_cons(SEQUENCE) + .encode(encoder->alg_id()) + .encode(encoder->key_bits(), BIT_STRING) + .end_cons() + .get_contents(); + + if(encoding == PEM) + pipe.write(PEM_Code::encode(der, "PUBLIC KEY")); + else + pipe.write(der); + } + +/* +* PEM encode a X.509 public key +*/ +std::string PEM_encode(const Public_Key& key) + { + Pipe pem; + pem.start_msg(); + encode(key, pem, PEM); + pem.end_msg(); + return pem.read_all_as_string(); + } + +/* +* Extract a public key and return it +*/ +Public_Key* load_key(DataSource& source) + { + try { + AlgorithmIdentifier alg_id; + MemoryVector<byte> key_bits; + + if(ASN1::maybe_BER(source) && !PEM_Code::matches(source)) + { + BER_Decoder(source) + .start_cons(SEQUENCE) + .decode(alg_id) + .decode(key_bits, BIT_STRING) + .verify_end() + .end_cons(); + } + else + { + DataSource_Memory ber( + PEM_Code::decode_check_label(source, "PUBLIC KEY") + ); + + BER_Decoder(ber) + .start_cons(SEQUENCE) + .decode(alg_id) + .decode(key_bits, BIT_STRING) + .verify_end() + .end_cons(); + } + + if(key_bits.is_empty()) + throw Decoding_Error("X.509 public key decoding failed"); + + const std::string alg_name = OIDS::lookup(alg_id.oid); + if(alg_name == "") + throw Decoding_Error("Unknown algorithm OID: " + + alg_id.oid.as_string()); + + std::auto_ptr<Public_Key> key_obj(get_public_key(alg_name)); + if(!key_obj.get()) + throw Decoding_Error("Unknown PK algorithm/OID: " + alg_name + ", " + + alg_id.oid.as_string()); + + std::auto_ptr<X509_Decoder> decoder(key_obj->x509_decoder()); + + if(!decoder.get()) + throw Decoding_Error("Key does not support X.509 decoding"); + + decoder->alg_id(alg_id); + decoder->key_bits(key_bits); + + return key_obj.release(); + } + catch(Decoding_Error) + { + throw Decoding_Error("X.509 public key decoding failed"); + } + } + +/* +* Extract a public key and return it +*/ +Public_Key* load_key(const std::string& fsname) + { + DataSource_Stream source(fsname, true); + return X509::load_key(source); + } + +/* +* Extract a public key and return it +*/ +Public_Key* load_key(const MemoryRegion<byte>& mem) + { + DataSource_Memory source(mem); + return X509::load_key(source); + } + +/* +* Make a copy of this public key +*/ +Public_Key* copy_key(const Public_Key& key) + { + Pipe bits; + bits.start_msg(); + X509::encode(key, bits, RAW_BER); + bits.end_msg(); + DataSource_Memory source(bits.read_all()); + return X509::load_key(source); + } + +/* +* Find the allowable key constraints +*/ +Key_Constraints find_constraints(const Public_Key& pub_key, + Key_Constraints limits) + { + const Public_Key* key = &pub_key; + u32bit constraints = 0; + + if(dynamic_cast<const PK_Encrypting_Key*>(key)) + constraints |= KEY_ENCIPHERMENT | DATA_ENCIPHERMENT; + + if(dynamic_cast<const PK_Key_Agreement_Key*>(key)) + constraints |= KEY_AGREEMENT; + + if(dynamic_cast<const PK_Verifying_wo_MR_Key*>(key) || + dynamic_cast<const PK_Verifying_with_MR_Key*>(key)) + constraints |= DIGITAL_SIGNATURE | NON_REPUDIATION; + + if(limits) + constraints &= limits; + + return Key_Constraints(constraints); + } + +} + +} diff --git a/botan/src/pubkey/pk_codecs/x509_key.h b/botan/src/pubkey/pk_codecs/x509_key.h new file mode 100644 index 0000000..9404b7e --- /dev/null +++ b/botan/src/pubkey/pk_codecs/x509_key.h @@ -0,0 +1,110 @@ +/* +* X.509 Public Key +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_X509_PUBLIC_KEY_H__ +#define BOTAN_X509_PUBLIC_KEY_H__ + +#include <botan/pipe.h> +#include <botan/pk_keys.h> +#include <botan/alg_id.h> +#include <botan/pubkey_enums.h> + +namespace Botan { + +/** +* This class represents abstract X.509 public key encoders. +*/ +class BOTAN_DLL X509_Encoder + { + public: + virtual AlgorithmIdentifier alg_id() const = 0; + virtual MemoryVector<byte> key_bits() const = 0; + virtual ~X509_Encoder() {} + }; + +/** +* This class represents abstract X.509 public key decoders. +*/ +class BOTAN_DLL X509_Decoder + { + public: + virtual void alg_id(const AlgorithmIdentifier&) = 0; + virtual void key_bits(const MemoryRegion<byte>&) = 0; + virtual ~X509_Decoder() {} + }; + +/** +* This namespace contains functions for handling X509 objects. +*/ +namespace X509 { + +/* +* X.509 Public Key Encoding/Decoding +*/ + +/** +* Encode a key into a pipe. +* @param key the public key to encode +* @param pipe the pipe to feed the encoded key into +* @param enc the encoding type to use +*/ +BOTAN_DLL void encode(const Public_Key& key, Pipe& pipe, + X509_Encoding enc = PEM); + +/** +* PEM encode a public key into a string. +* @param key the key to encode +* @return the PEM encoded key +*/ +BOTAN_DLL std::string PEM_encode(const Public_Key& key); + +/** +* Create a public key from a data source. +* @param source the source providing the DER or PEM encoded key +* @return the new public key object +*/ +BOTAN_DLL Public_Key* load_key(DataSource& source); + +/** +* Create a public key from a string. +* @param enc the string containing the PEM encoded key +* @return the new public key object +*/ +BOTAN_DLL Public_Key* load_key(const std::string& enc); + +/** +* Create a public key from a memory region. +* @param enc the memory region containing the DER or PEM encoded key +* @return the new public key object +*/ +BOTAN_DLL Public_Key* load_key(const MemoryRegion<byte>& enc); + +/** +* Copy a key. +* @param key the public key to copy +* @return the new public key object +*/ +BOTAN_DLL Public_Key* copy_key(const Public_Key& key); + +/** +* Create the key constraints for a specific public key. +* @param pub_key the public key from which the basic set of +* constraints to be placed in the return value is derived +* @param limits additional limits that will be incorporated into the +* return value +* @return the combination of key type specific constraints and +* additional limits +*/ + +BOTAN_DLL Key_Constraints find_constraints(const Public_Key& pub_key, + Key_Constraints limits); + +} + +} + +#endif diff --git a/botan/src/pubkey/pk_filts.cpp b/botan/src/pubkey/pk_filts.cpp new file mode 100644 index 0000000..18da9c1 --- /dev/null +++ b/botan/src/pubkey/pk_filts.cpp @@ -0,0 +1,115 @@ +/* +* PK Filters +* (C) 1999-2009 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/pk_filts.h> + +namespace Botan { + +/* +* Append to the buffer +*/ +void PK_Encryptor_Filter::write(const byte input[], u32bit length) + { + buffer.append(input, length); + } + +/* +* Encrypt the message +*/ +void PK_Encryptor_Filter::end_msg() + { + send(cipher->encrypt(buffer, buffer.size(), rng)); + buffer.destroy(); + } + +/* +* Append to the buffer +*/ +void PK_Decryptor_Filter::write(const byte input[], u32bit length) + { + buffer.append(input, length); + } + +/* +* Decrypt the message +*/ +void PK_Decryptor_Filter::end_msg() + { + send(cipher->decrypt(buffer, buffer.size())); + buffer.destroy(); + } + +/* +* Add more data +*/ +void PK_Signer_Filter::write(const byte input[], u32bit length) + { + signer->update(input, length); + } + +/* +* Sign the message +*/ +void PK_Signer_Filter::end_msg() + { + send(signer->signature(rng)); + } + +/* +* Add more data +*/ +void PK_Verifier_Filter::write(const byte input[], u32bit length) + { + verifier->update(input, length); + } + +/* +* Verify the message +*/ +void PK_Verifier_Filter::end_msg() + { + if(signature.is_empty()) + throw Exception("PK_Verifier_Filter: No signature to check against"); + bool is_valid = verifier->check_signature(signature, signature.size()); + send((is_valid ? 1 : 0)); + } + +/* +* Set the signature to check +*/ +void PK_Verifier_Filter::set_signature(const byte sig[], u32bit length) + { + signature.set(sig, length); + } + +/* +* Set the signature to check +*/ +void PK_Verifier_Filter::set_signature(const MemoryRegion<byte>& sig) + { + signature = sig; + } + +/* +* PK_Verifier_Filter Constructor +*/ +PK_Verifier_Filter::PK_Verifier_Filter(PK_Verifier* v, const byte sig[], + u32bit length) : + verifier(v), signature(sig, length) + { + } + +/* +* PK_Verifier_Filter Constructor +*/ +PK_Verifier_Filter::PK_Verifier_Filter(PK_Verifier* v, + const MemoryRegion<byte>& sig) : + verifier(v), signature(sig) + { + } + +} diff --git a/botan/src/pubkey/pk_filts.h b/botan/src/pubkey/pk_filts.h new file mode 100644 index 0000000..8bf3fc2 --- /dev/null +++ b/botan/src/pubkey/pk_filts.h @@ -0,0 +1,91 @@ +/* +* PK Filters +* (C) 1999-2009 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_PK_FILTERS_H__ +#define BOTAN_PK_FILTERS_H__ + +#include <botan/filter.h> +#include <botan/pubkey.h> + +namespace Botan { + +/* +* PK_Encryptor Filter +*/ +class BOTAN_DLL PK_Encryptor_Filter : public Filter + { + public: + void write(const byte[], u32bit); + void end_msg(); + PK_Encryptor_Filter(PK_Encryptor* c, + RandomNumberGenerator& rng_ref) : + cipher(c), rng(rng_ref) {} + ~PK_Encryptor_Filter() { delete cipher; } + private: + PK_Encryptor* cipher; + RandomNumberGenerator& rng; + SecureVector<byte> buffer; + }; + +/* +* PK_Decryptor Filter +*/ +class BOTAN_DLL PK_Decryptor_Filter : public Filter + { + public: + void write(const byte[], u32bit); + void end_msg(); + PK_Decryptor_Filter(PK_Decryptor* c) : cipher(c) {} + ~PK_Decryptor_Filter() { delete cipher; } + private: + PK_Decryptor* cipher; + SecureVector<byte> buffer; + }; + +/* +* PK_Signer Filter +*/ +class BOTAN_DLL PK_Signer_Filter : public Filter + { + public: + void write(const byte[], u32bit); + void end_msg(); + + PK_Signer_Filter(PK_Signer* s, + RandomNumberGenerator& rng_ref) : + signer(s), rng(rng_ref) {} + + ~PK_Signer_Filter() { delete signer; } + private: + PK_Signer* signer; + RandomNumberGenerator& rng; + }; + +/* +* PK_Verifier Filter +*/ +class BOTAN_DLL PK_Verifier_Filter : public Filter + { + public: + void write(const byte[], u32bit); + void end_msg(); + + void set_signature(const byte[], u32bit); + void set_signature(const MemoryRegion<byte>&); + + PK_Verifier_Filter(PK_Verifier* v) : verifier(v) {} + PK_Verifier_Filter(PK_Verifier*, const byte[], u32bit); + PK_Verifier_Filter(PK_Verifier*, const MemoryRegion<byte>&); + ~PK_Verifier_Filter() { delete verifier; } + private: + PK_Verifier* verifier; + SecureVector<byte> signature; + }; + +} + +#endif diff --git a/botan/src/pubkey/pk_keys.cpp b/botan/src/pubkey/pk_keys.cpp new file mode 100644 index 0000000..b931585 --- /dev/null +++ b/botan/src/pubkey/pk_keys.cpp @@ -0,0 +1,54 @@ +/* +* PK Key Types +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/pk_keys.h> +#include <botan/oids.h> + +namespace Botan { + +/* +* Default OID access +*/ +OID Public_Key::get_oid() const + { + try { + return OIDS::lookup(algo_name()); + } + catch(Lookup_Error) + { + throw Lookup_Error("PK algo " + algo_name() + " has no defined OIDs"); + } + } + +/* +* Run checks on a loaded public key +*/ +void Public_Key::load_check(RandomNumberGenerator& rng) const + { + if(!check_key(rng, BOTAN_PUBLIC_KEY_STRONG_CHECKS_ON_LOAD)) + throw Invalid_Argument(algo_name() + ": Invalid public key"); + } + +/* +* Run checks on a loaded private key +*/ +void Private_Key::load_check(RandomNumberGenerator& rng) const + { + if(!check_key(rng, BOTAN_PRIVATE_KEY_STRONG_CHECKS_ON_LOAD)) + throw Invalid_Argument(algo_name() + ": Invalid private key"); + } + +/* +* Run checks on a generated private key +*/ +void Private_Key::gen_check(RandomNumberGenerator& rng) const + { + if(!check_key(rng, BOTAN_PRIVATE_KEY_STRONG_CHECKS_ON_GENERATE)) + throw Self_Test_Failure(algo_name() + " private key generation failed"); + } + +} diff --git a/botan/src/pubkey/pk_keys.h b/botan/src/pubkey/pk_keys.h new file mode 100644 index 0000000..5b61257 --- /dev/null +++ b/botan/src/pubkey/pk_keys.h @@ -0,0 +1,180 @@ +/* +* PK Key Types +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_PK_KEYS_H__ +#define BOTAN_PK_KEYS_H__ + +#include <botan/secmem.h> +#include <botan/asn1_oid.h> +#include <botan/rng.h> + +namespace Botan { + +/** +* Public Key Base Class. +*/ +class BOTAN_DLL Public_Key + { + public: + /** + * Get the name of the underlying public key scheme. + * @return the name of the public key scheme + */ + virtual std::string algo_name() const = 0; + + /** + * Get the OID of the underlying public key scheme. + * @return the OID of the public key scheme + */ + virtual OID get_oid() const; + + /** + * Test the key values for consistency. + * @param rng rng to use + * @param strong whether to perform strong and lengthy version + * of the test + * @return true if the test is passed + */ + virtual bool check_key(RandomNumberGenerator&, bool) const + { return true; } + + /** + * Find out the number of message parts supported by this scheme. + * @return the number of message parts + */ + virtual u32bit message_parts() const { return 1; } + + /** + * Find out the message part size supported by this scheme/key. + * @return the size of the message parts + */ + virtual u32bit message_part_size() const { return 0; } + + /** + * Get the maximum message size in bits supported by this public key. + * @return the maximum message in bits + */ + virtual u32bit max_input_bits() const = 0; + + /** + * Get an X509 encoder that can be used to encode this key in X509 format. + * @return an X509 encoder for this key + */ + virtual class X509_Encoder* x509_encoder() const = 0; + + /** + * Get an X509 decoder that can be used to set the values of this + * key based on an X509 encoded key object. + * @return an X509 decoder for this key + */ + virtual class X509_Decoder* x509_decoder() = 0; + + virtual ~Public_Key() {} + protected: + virtual void load_check(RandomNumberGenerator&) const; + }; + +/** +* Private Key Base Class +*/ +class BOTAN_DLL Private_Key : public virtual Public_Key + { + public: + /** + * Get a PKCS#8 encoder that can be used to encode this key in + * PKCS#8 format. + * @return an PKCS#8 encoder for this key + */ + virtual class PKCS8_Encoder* pkcs8_encoder() const + { return 0; } + + /** + * Get an PKCS#8 decoder that can be used to set the values of this key + * based on an PKCS#8 encoded key object. + * @return an PKCS#8 decoder for this key + */ + virtual class PKCS8_Decoder* pkcs8_decoder(RandomNumberGenerator&) + { return 0; } + protected: + void load_check(RandomNumberGenerator&) const; + void gen_check(RandomNumberGenerator&) const; + }; + +/** +* PK Encrypting Key. +*/ +class BOTAN_DLL PK_Encrypting_Key : public virtual Public_Key + { + public: + virtual SecureVector<byte> encrypt(const byte[], u32bit, + RandomNumberGenerator&) const = 0; + virtual ~PK_Encrypting_Key() {} + }; + +/** +* PK Decrypting Key +*/ +class BOTAN_DLL PK_Decrypting_Key : public virtual Private_Key + { + public: + virtual SecureVector<byte> decrypt(const byte[], u32bit) const = 0; + virtual ~PK_Decrypting_Key() {} + }; + +/** +* PK Signing Key +*/ +class BOTAN_DLL PK_Signing_Key : public virtual Private_Key + { + public: + virtual SecureVector<byte> sign(const byte[], u32bit, + RandomNumberGenerator& rng) const = 0; + virtual ~PK_Signing_Key() {} + }; + +/** +* PK Verifying Key, Message Recovery Version +*/ +class BOTAN_DLL PK_Verifying_with_MR_Key : public virtual Public_Key + { + public: + virtual SecureVector<byte> verify(const byte[], u32bit) const = 0; + virtual ~PK_Verifying_with_MR_Key() {} + }; + +/** +* PK Verifying Key, No Message Recovery Version +*/ +class BOTAN_DLL PK_Verifying_wo_MR_Key : public virtual Public_Key + { + public: + virtual bool verify(const byte[], u32bit, + const byte[], u32bit) const = 0; + virtual ~PK_Verifying_wo_MR_Key() {} + }; + +/** +* PK Secret Value Derivation Key +*/ +class BOTAN_DLL PK_Key_Agreement_Key : public virtual Private_Key + { + public: + virtual SecureVector<byte> derive_key(const byte[], u32bit) const = 0; + virtual MemoryVector<byte> public_value() const = 0; + virtual ~PK_Key_Agreement_Key() {} + }; + +/* +* Typedefs +*/ +typedef PK_Key_Agreement_Key PK_KA_Key; +typedef Public_Key X509_PublicKey; +typedef Private_Key PKCS8_PrivateKey; + +} + +#endif diff --git a/botan/src/pubkey/pubkey.cpp b/botan/src/pubkey/pubkey.cpp new file mode 100644 index 0000000..4ddaa6f --- /dev/null +++ b/botan/src/pubkey/pubkey.cpp @@ -0,0 +1,396 @@ +/* +* Public Key Base +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/pubkey.h> +#include <botan/der_enc.h> +#include <botan/ber_dec.h> +#include <botan/bigint.h> +#include <botan/parsing.h> +#include <botan/bit_ops.h> +#include <memory> + +namespace Botan { + +/* +* Encrypt a message +*/ +SecureVector<byte> PK_Encryptor::encrypt(const byte in[], u32bit len, + RandomNumberGenerator& rng) const + { + return enc(in, len, rng); + } + +/* +* Encrypt a message +*/ +SecureVector<byte> PK_Encryptor::encrypt(const MemoryRegion<byte>& in, + RandomNumberGenerator& rng) const + { + return enc(in.begin(), in.size(), rng); + } + +/* +* Decrypt a message +*/ +SecureVector<byte> PK_Decryptor::decrypt(const byte in[], u32bit len) const + { + return dec(in, len); + } + +/* +* Decrypt a message +*/ +SecureVector<byte> PK_Decryptor::decrypt(const MemoryRegion<byte>& in) const + { + return dec(in.begin(), in.size()); + } + +/* +* PK_Encryptor_MR_with_EME Constructor +*/ +PK_Encryptor_MR_with_EME::PK_Encryptor_MR_with_EME(const PK_Encrypting_Key& k, + EME* eme_obj) : + key(k), encoder(eme_obj) + { + } + +/* +* Encrypt a message +*/ +SecureVector<byte> +PK_Encryptor_MR_with_EME::enc(const byte msg[], + u32bit length, + RandomNumberGenerator& rng) const + { + SecureVector<byte> message; + if(encoder) + message = encoder->encode(msg, length, key.max_input_bits(), rng); + else + message.set(msg, length); + + if(8*(message.size() - 1) + high_bit(message[0]) > key.max_input_bits()) + throw Exception("PK_Encryptor_MR_with_EME: Input is too large"); + + return key.encrypt(message, message.size(), rng); + } + +/* +* Return the max size, in bytes, of a message +*/ +u32bit PK_Encryptor_MR_with_EME::maximum_input_size() const + { + if(!encoder) + return (key.max_input_bits() / 8); + else + return encoder->maximum_input_size(key.max_input_bits()); + } + +/* +* PK_Decryptor_MR_with_EME Constructor +*/ +PK_Decryptor_MR_with_EME::PK_Decryptor_MR_with_EME(const PK_Decrypting_Key& k, + EME* eme_obj) : + key(k), encoder(eme_obj) + { + } + +/* +* Decrypt a message +*/ +SecureVector<byte> PK_Decryptor_MR_with_EME::dec(const byte msg[], + u32bit length) const + { + try { + SecureVector<byte> decrypted = key.decrypt(msg, length); + if(encoder) + return encoder->decode(decrypted, key.max_input_bits()); + else + return decrypted; + } + catch(Invalid_Argument) + { + throw Exception("PK_Decryptor_MR_with_EME: Input is invalid"); + } + catch(Decoding_Error) + { + throw Exception("PK_Decryptor_MR_with_EME: Input is invalid"); + } + } + +/* +* PK_Signer Constructor +*/ +PK_Signer::PK_Signer(const PK_Signing_Key& k, EMSA* emsa_obj) : + key(k), emsa(emsa_obj) + { + sig_format = IEEE_1363; + } + +/* +* Set the signature format +*/ +void PK_Signer::set_output_format(Signature_Format format) + { + if(key.message_parts() == 1 && format != IEEE_1363) + throw Invalid_State("PK_Signer: Cannot set the output format for " + + key.algo_name() + " keys"); + sig_format = format; + } + +/* +* Sign a message +*/ +SecureVector<byte> PK_Signer::sign_message(const byte msg[], u32bit length, + RandomNumberGenerator& rng) + { + update(msg, length); + return signature(rng); + } + +/* +* Sign a message +*/ +SecureVector<byte> PK_Signer::sign_message(const MemoryRegion<byte>& msg, + RandomNumberGenerator& rng) + { + return sign_message(msg, msg.size(), rng); + } + +/* +* Add more to the message to be signed +*/ +void PK_Signer::update(const byte in[], u32bit length) + { + emsa->update(in, length); + } + +/* +* Add more to the message to be signed +*/ +void PK_Signer::update(byte in) + { + update(&in, 1); + } + +/* +* Add more to the message to be signed +*/ +void PK_Signer::update(const MemoryRegion<byte>& in) + { + update(in, in.size()); + } + +/* +* Create a signature +*/ +SecureVector<byte> PK_Signer::signature(RandomNumberGenerator& rng) + { + SecureVector<byte> encoded = emsa->encoding_of(emsa->raw_data(), + key.max_input_bits(), + rng); + + SecureVector<byte> plain_sig = key.sign(encoded, encoded.size(), rng); + + if(key.message_parts() == 1 || sig_format == IEEE_1363) + return plain_sig; + + if(sig_format == DER_SEQUENCE) + { + if(plain_sig.size() % key.message_parts()) + throw Encoding_Error("PK_Signer: strange signature size found"); + const u32bit SIZE_OF_PART = plain_sig.size() / key.message_parts(); + + std::vector<BigInt> sig_parts(key.message_parts()); + for(u32bit j = 0; j != sig_parts.size(); ++j) + sig_parts[j].binary_decode(plain_sig + SIZE_OF_PART*j, SIZE_OF_PART); + + return DER_Encoder() + .start_cons(SEQUENCE) + .encode_list(sig_parts) + .end_cons() + .get_contents(); + } + else + throw Encoding_Error("PK_Signer: Unknown signature format " + + to_string(sig_format)); + } + +/* +* PK_Verifier Constructor +*/ +PK_Verifier::PK_Verifier(EMSA* emsa_obj) + { + emsa = emsa_obj; + sig_format = IEEE_1363; + } + +/* +* PK_Verifier Destructor +*/ +PK_Verifier::~PK_Verifier() + { + delete emsa; + } + +/* +* Set the signature format +*/ +void PK_Verifier::set_input_format(Signature_Format format) + { + if(key_message_parts() == 1 && format != IEEE_1363) + throw Invalid_State("PK_Verifier: This algorithm always uses IEEE 1363"); + sig_format = format; + } + +/* +* Verify a message +*/ +bool PK_Verifier::verify_message(const MemoryRegion<byte>& msg, + const MemoryRegion<byte>& sig) + { + return verify_message(msg, msg.size(), sig, sig.size()); + } + +/* +* Verify a message +*/ +bool PK_Verifier::verify_message(const byte msg[], u32bit msg_length, + const byte sig[], u32bit sig_length) + { + update(msg, msg_length); + return check_signature(sig, sig_length); + } + +/* +* Append to the message +*/ +void PK_Verifier::update(const byte in[], u32bit length) + { + emsa->update(in, length); + } + +/* +* Append to the message +*/ +void PK_Verifier::update(byte in) + { + update(&in, 1); + } + +/* +* Append to the message +*/ +void PK_Verifier::update(const MemoryRegion<byte>& in) + { + update(in, in.size()); + } + +/* +* Check a signature +*/ +bool PK_Verifier::check_signature(const MemoryRegion<byte>& sig) + { + return check_signature(sig, sig.size()); + } + +/* +* Check a signature +*/ +bool PK_Verifier::check_signature(const byte sig[], u32bit length) + { + try { + if(sig_format == IEEE_1363) + return validate_signature(emsa->raw_data(), sig, length); + else if(sig_format == DER_SEQUENCE) + { + BER_Decoder decoder(sig, length); + BER_Decoder ber_sig = decoder.start_cons(SEQUENCE); + + u32bit count = 0; + SecureVector<byte> real_sig; + while(ber_sig.more_items()) + { + BigInt sig_part; + ber_sig.decode(sig_part); + real_sig.append(BigInt::encode_1363(sig_part, + key_message_part_size())); + ++count; + } + if(count != key_message_parts()) + throw Decoding_Error("PK_Verifier: signature size invalid"); + + return validate_signature(emsa->raw_data(), + real_sig, real_sig.size()); + } + else + throw Decoding_Error("PK_Verifier: Unknown signature format " + + to_string(sig_format)); + } + catch(Invalid_Argument) { return false; } + catch(Decoding_Error) { return false; } + } + +/* +* Verify a signature +*/ +bool PK_Verifier_with_MR::validate_signature(const MemoryRegion<byte>& msg, + const byte sig[], u32bit sig_len) + { + SecureVector<byte> output_of_key = key.verify(sig, sig_len); + return emsa->verify(output_of_key, msg, key.max_input_bits()); + } + +/* +* Verify a signature +*/ +bool PK_Verifier_wo_MR::validate_signature(const MemoryRegion<byte>& msg, + const byte sig[], u32bit sig_len) + { + Null_RNG rng; + + SecureVector<byte> encoded = + emsa->encoding_of(msg, key.max_input_bits(), rng); + + return key.verify(encoded, encoded.size(), sig, sig_len); + } + +/* +* PK_Key_Agreement Constructor +*/ +PK_Key_Agreement::PK_Key_Agreement(const PK_Key_Agreement_Key& k, + KDF* kdf_obj) : + key(k), kdf(kdf_obj) + { + } + +/* +* Perform Key Agreement Operation +*/ +SymmetricKey PK_Key_Agreement::derive_key(u32bit key_len, + const byte in[], u32bit in_len, + const std::string& params) const + { + return derive_key(key_len, in, in_len, + reinterpret_cast<const byte*>(params.data()), + params.length()); + } + +/* +* Perform Key Agreement Operation +*/ +SymmetricKey PK_Key_Agreement::derive_key(u32bit key_len, const byte in[], + u32bit in_len, const byte params[], + u32bit params_len) const + { + OctetString z = key.derive_key(in, in_len); + if(!kdf) + return z; + + return kdf->derive_key(key_len, z.bits_of(), params, params_len); + } + +} diff --git a/botan/src/pubkey/pubkey.h b/botan/src/pubkey/pubkey.h new file mode 100644 index 0000000..c73a54d --- /dev/null +++ b/botan/src/pubkey/pubkey.h @@ -0,0 +1,392 @@ +/* +* Public Key Interface +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_PUBKEY_H__ +#define BOTAN_PUBKEY_H__ + +#include <botan/pk_keys.h> +#include <botan/symkey.h> +#include <botan/rng.h> +#include <botan/eme.h> +#include <botan/emsa.h> +#include <botan/kdf.h> + +namespace Botan { + +/** +* The two types of signature format supported by Botan. +*/ +enum Signature_Format { IEEE_1363, DER_SEQUENCE }; + +/** +* Public Key Encryptor +*/ +class BOTAN_DLL PK_Encryptor + { + public: + + /** + * Encrypt a message. + * @param in the message as a byte array + * @param length the length of the above byte array + * @param rng the random number source to use + * @return the encrypted message + */ + SecureVector<byte> encrypt(const byte in[], u32bit length, + RandomNumberGenerator& rng) const; + + /** + * Encrypt a message. + * @param in the message + * @param rng the random number source to use + * @return the encrypted message + */ + SecureVector<byte> encrypt(const MemoryRegion<byte>& in, + RandomNumberGenerator& rng) const; + + /** + * Return the maximum allowed message size in bytes. + * @return the maximum message size in bytes + */ + virtual u32bit maximum_input_size() const = 0; + + virtual ~PK_Encryptor() {} + private: + virtual SecureVector<byte> enc(const byte[], u32bit, + RandomNumberGenerator&) const = 0; + }; + +/** +* Public Key Decryptor +*/ +class BOTAN_DLL PK_Decryptor + { + public: + /** + * Decrypt a ciphertext. + * @param in the ciphertext as a byte array + * @param length the length of the above byte array + * @return the decrypted message + */ + SecureVector<byte> decrypt(const byte in[], u32bit length) const; + + /** + * Decrypt a ciphertext. + * @param in the ciphertext + * @return the decrypted message + */ + SecureVector<byte> decrypt(const MemoryRegion<byte>& in) const; + + virtual ~PK_Decryptor() {} + private: + virtual SecureVector<byte> dec(const byte[], u32bit) const = 0; + }; + +/** +* Public Key Signer. Use the sign_message() functions for small +* messages. Use multiple calls update() to process large messages and +* generate the signature by finally calling signature(). +*/ +class BOTAN_DLL PK_Signer + { + public: + /** + * Sign a message. + * @param in the message to sign as a byte array + * @param length the length of the above byte array + * @param rng the rng to use + * @return the signature + */ + SecureVector<byte> sign_message(const byte in[], u32bit length, + RandomNumberGenerator& rng); + + /** + * Sign a message. + * @param in the message to sign + * @param rng the rng to use + * @return the signature + */ + SecureVector<byte> sign_message(const MemoryRegion<byte>& in, + RandomNumberGenerator& rng); + + /** + * Add a message part (single byte). + * @param the byte to add + */ + void update(byte in); + + /** + * Add a message part. + * @param in the message part to add as a byte array + * @param length the length of the above byte array + */ + void update(const byte in[], u32bit length); + + /** + * Add a message part. + * @param in the message part to add + */ + void update(const MemoryRegion<byte>& in); + + /** + * Get the signature of the so far processed message (provided by the + * calls to update()). + * @param rng the rng to use + * @return the signature of the total message + */ + SecureVector<byte> signature(RandomNumberGenerator& rng); + + /** + * Set the output format of the signature. + * @param format the signature format to use + */ + void set_output_format(Signature_Format format); + + /** + * Construct a PK Signer. + * @param key the key to use inside this signer + * @param emsa the EMSA to use + * An example would be "EMSA1(SHA-224)". + */ + PK_Signer(const PK_Signing_Key& key, EMSA* emsa); + + ~PK_Signer() { delete emsa; } + private: + PK_Signer(const PK_Signer&); + PK_Signer& operator=(const PK_Signer&); + + const PK_Signing_Key& key; + Signature_Format sig_format; + EMSA* emsa; + }; + +/** +* Public Key Verifier. Use the verify_message() functions for small +* messages. Use multiple calls update() to process large messages and +* verify the signature by finally calling check_signature(). +*/ +class BOTAN_DLL PK_Verifier + { + public: + /** + * Verify a signature. + * @param msg the message that the signature belongs to, as a byte array + * @param msg_length the length of the above byte array msg + * @param sig the signature as a byte array + * @param sig_length the length of the above byte array sig + * @return true if the signature is valid + */ + bool verify_message(const byte msg[], u32bit msg_length, + const byte sig[], u32bit sig_length); + /** + * Verify a signature. + * @param msg the message that the signature belongs to + * @param sig the signature + * @return true if the signature is valid + */ + bool verify_message(const MemoryRegion<byte>& msg, + const MemoryRegion<byte>& sig); + + /** + * Add a message part (single byte) of the message corresponding to the + * signature to be verified. + * @param msg_part the byte to add + */ + void update(byte msg_part); + + /** + * Add a message part of the message corresponding to the + * signature to be verified. + * @param msg_part the new message part as a byte array + * @param length the length of the above byte array + */ + void update(const byte msg_part[], u32bit length); + + /** + * Add a message part of the message corresponding to the + * signature to be verified. + * @param msg_part the new message part + */ + void update(const MemoryRegion<byte>& msg_part); + + /** + * Check the signature of the buffered message, i.e. the one build + * by successive calls to update. + * @param sig the signature to be verified as a byte array + * @param length the length of the above byte array + * @return true if the signature is valid, false otherwise + */ + bool check_signature(const byte sig[], u32bit length); + + /** + * Check the signature of the buffered message, i.e. the one build + * by successive calls to update. + * @param sig the signature to be verified + * @return true if the signature is valid, false otherwise + */ + bool check_signature(const MemoryRegion<byte>& sig); + + /** + * Set the format of the signatures fed to this verifier. + * @param format the signature format to use + */ + void set_input_format(Signature_Format format); + + /** + * Construct a PK Verifier. + * @param emsa the EMSA to use + * An example would be new EMSA1(new SHA_224) + */ + PK_Verifier(EMSA* emsa); + + virtual ~PK_Verifier(); + protected: + virtual bool validate_signature(const MemoryRegion<byte>&, + const byte[], u32bit) = 0; + virtual u32bit key_message_parts() const = 0; + virtual u32bit key_message_part_size() const = 0; + + Signature_Format sig_format; + EMSA* emsa; + private: + PK_Verifier(const PK_Verifier&); + PK_Verifier& operator=(const PK_Verifier&); + }; + +/* +* Key Agreement +*/ +class BOTAN_DLL PK_Key_Agreement + { + public: + SymmetricKey derive_key(u32bit, const byte[], u32bit, + const std::string& = "") const; + SymmetricKey derive_key(u32bit, const byte[], u32bit, + const byte[], u32bit) const; + + /** + * Construct a PK Key Agreement. + * @param key the key to use + * @param kdf the KDF to use + */ + PK_Key_Agreement(const PK_Key_Agreement_Key& key, KDF* kdf); + + ~PK_Key_Agreement() { delete kdf; } + private: + PK_Key_Agreement(const PK_Key_Agreement_Key&); + PK_Key_Agreement& operator=(const PK_Key_Agreement&); + + const PK_Key_Agreement_Key& key; + KDF* kdf; + }; + +/** +* Encryption with an MR algorithm and an EME. +*/ +class BOTAN_DLL PK_Encryptor_MR_with_EME : public PK_Encryptor + { + public: + u32bit maximum_input_size() const; + + /** + * Construct an instance. + * @param key the key to use inside the decryptor + * @param eme the EME to use + */ + PK_Encryptor_MR_with_EME(const PK_Encrypting_Key& key, + EME* eme); + + ~PK_Encryptor_MR_with_EME() { delete encoder; } + private: + PK_Encryptor_MR_with_EME(const PK_Encryptor_MR_with_EME&); + PK_Encryptor_MR_with_EME& operator=(const PK_Encryptor_MR_with_EME&); + + SecureVector<byte> enc(const byte[], u32bit, + RandomNumberGenerator& rng) const; + + const PK_Encrypting_Key& key; + const EME* encoder; + }; + +/** +* Decryption with an MR algorithm and an EME. +*/ +class BOTAN_DLL PK_Decryptor_MR_with_EME : public PK_Decryptor + { + public: + /** + * Construct an instance. + * @param key the key to use inside the encryptor + * @param eme the EME to use + */ + PK_Decryptor_MR_with_EME(const PK_Decrypting_Key& key, + EME* eme); + + ~PK_Decryptor_MR_with_EME() { delete encoder; } + private: + PK_Decryptor_MR_with_EME(const PK_Decryptor_MR_with_EME&); + PK_Decryptor_MR_with_EME& operator=(const PK_Decryptor_MR_with_EME&); + + SecureVector<byte> dec(const byte[], u32bit) const; + + const PK_Decrypting_Key& key; + const EME* encoder; + }; + +/** +* Public Key Verifier with Message Recovery. +*/ +class BOTAN_DLL PK_Verifier_with_MR : public PK_Verifier + { + public: + /** + * Construct an instance. + * @param key the key to use inside the verifier + * @param emsa_name the name of the EMSA to use + */ + PK_Verifier_with_MR(const PK_Verifying_with_MR_Key& k, + EMSA* emsa_obj) : PK_Verifier(emsa_obj), key(k) {} + + private: + PK_Verifier_with_MR(const PK_Verifying_with_MR_Key&); + PK_Verifier_with_MR& operator=(const PK_Verifier_with_MR&); + + bool validate_signature(const MemoryRegion<byte>&, const byte[], u32bit); + u32bit key_message_parts() const { return key.message_parts(); } + u32bit key_message_part_size() const { return key.message_part_size(); } + + const PK_Verifying_with_MR_Key& key; + }; + +/** +* Public Key Verifier without Message Recovery +*/ +class BOTAN_DLL PK_Verifier_wo_MR : public PK_Verifier + { + public: + /** + * Construct an instance. + * @param key the key to use inside the verifier + * @param emsa_name the name of the EMSA to use + */ + PK_Verifier_wo_MR(const PK_Verifying_wo_MR_Key& k, + EMSA* emsa_obj) : PK_Verifier(emsa_obj), key(k) {} + + private: + PK_Verifier_wo_MR(const PK_Verifying_wo_MR_Key&); + PK_Verifier_wo_MR& operator=(const PK_Verifier_wo_MR&); + + bool validate_signature(const MemoryRegion<byte>&, const byte[], u32bit); + u32bit key_message_parts() const { return key.message_parts(); } + u32bit key_message_part_size() const { return key.message_part_size(); } + + const PK_Verifying_wo_MR_Key& key; + }; + +} + +#endif diff --git a/botan/src/pubkey/pubkey_enums.cpp b/botan/src/pubkey/pubkey_enums.cpp new file mode 100644 index 0000000..327107d --- /dev/null +++ b/botan/src/pubkey/pubkey_enums.cpp @@ -0,0 +1,42 @@ +/* +* KeyUsage +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/pubkey_enums.h> +#include <botan/ber_dec.h> + +namespace Botan { + +namespace BER { + +/* +* Decode a BER encoded KeyUsage +*/ +void decode(BER_Decoder& source, Key_Constraints& key_usage) + { + BER_Object obj = source.get_next_object(); + + if(obj.type_tag != BIT_STRING || obj.class_tag != UNIVERSAL) + throw BER_Bad_Tag("Bad tag for usage constraint", + obj.type_tag, obj.class_tag); + if(obj.value.size() != 2 && obj.value.size() != 3) + throw BER_Decoding_Error("Bad size for BITSTRING in usage constraint"); + if(obj.value[0] >= 8) + throw BER_Decoding_Error("Invalid unused bits in usage constraint"); + + const byte mask = (0xFF << obj.value[0]); + obj.value[obj.value.size()-1] &= mask; + + u16bit usage = 0; + for(u32bit j = 1; j != obj.value.size(); ++j) + usage = (obj.value[j] << 8) | usage; + + key_usage = Key_Constraints(usage); + } + +} + +} diff --git a/botan/src/pubkey/pubkey_enums.h b/botan/src/pubkey/pubkey_enums.h new file mode 100644 index 0000000..53e319f --- /dev/null +++ b/botan/src/pubkey/pubkey_enums.h @@ -0,0 +1,77 @@ +/* +* Enumerations +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ENUMS_H__ +#define BOTAN_ENUMS_H__ + +#include <botan/ber_dec.h> + +namespace Botan { + +/** +* X.509v3 Key Constraints. +*/ +enum Key_Constraints { + NO_CONSTRAINTS = 0, + DIGITAL_SIGNATURE = 32768, + NON_REPUDIATION = 16384, + KEY_ENCIPHERMENT = 8192, + DATA_ENCIPHERMENT = 4096, + KEY_AGREEMENT = 2048, + KEY_CERT_SIGN = 1024, + CRL_SIGN = 512, + ENCIPHER_ONLY = 256, + DECIPHER_ONLY = 128 +}; + +/** +* BER Decoding Function for key constraints +*/ +namespace BER { + +void BOTAN_DLL decode(BER_Decoder&, Key_Constraints&); + +} + +/** +* X.509v2 CRL Reason Code. +*/ +enum CRL_Code { + UNSPECIFIED = 0, + KEY_COMPROMISE = 1, + CA_COMPROMISE = 2, + AFFILIATION_CHANGED = 3, + SUPERSEDED = 4, + CESSATION_OF_OPERATION = 5, + CERTIFICATE_HOLD = 6, + REMOVE_FROM_CRL = 8, + PRIVLEDGE_WITHDRAWN = 9, + AA_COMPROMISE = 10, + + DELETE_CRL_ENTRY = 0xFF00, + OCSP_GOOD = 0xFF01, + OCSP_UNKNOWN = 0xFF02 +}; + +/* +* Various Other Enumerations +*/ + +/** +* The two types of X509 encoding supported by Botan. +*/ +enum X509_Encoding { RAW_BER, PEM }; + +/** +* Value to encode in case of no path limit in the X509 +* BasicConstraints extension. +*/ +static const u32bit NO_CERT_PATH_LIMIT = 0xFFFFFFF0; + +} + +#endif diff --git a/botan/src/pubkey/rsa/info.txt b/botan/src/pubkey/rsa/info.txt new file mode 100644 index 0000000..7729fd8 --- /dev/null +++ b/botan/src/pubkey/rsa/info.txt @@ -0,0 +1,17 @@ +realname "RSA" + +define RSA + +load_on auto + +<add> +rsa.cpp +rsa.h +</add> + +<requires> +if_algo +keypair +libstate +numbertheory +</requires> diff --git a/botan/src/pubkey/rsa/rsa.cpp b/botan/src/pubkey/rsa/rsa.cpp new file mode 100644 index 0000000..83e6e1b --- /dev/null +++ b/botan/src/pubkey/rsa/rsa.cpp @@ -0,0 +1,164 @@ +/* +* RSA +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/rsa.h> +#include <botan/parsing.h> +#include <botan/numthry.h> +#include <botan/keypair.h> +#include <botan/look_pk.h> + +namespace Botan { + +/* +* RSA_PublicKey Constructor +*/ +RSA_PublicKey::RSA_PublicKey(const BigInt& mod, const BigInt& exp) + { + n = mod; + e = exp; + X509_load_hook(); + } + +/* +* RSA Public Operation +*/ +BigInt RSA_PublicKey::public_op(const BigInt& i) const + { + if(i >= n) + throw Invalid_Argument(algo_name() + "::public_op: input is too large"); + return core.public_op(i); + } + +/* +* RSA Encryption Function +*/ +SecureVector<byte> RSA_PublicKey::encrypt(const byte in[], u32bit len, + RandomNumberGenerator&) const + { + BigInt i(in, len); + return BigInt::encode_1363(public_op(i), n.bytes()); + } + +/* +* RSA Verification Function +*/ +SecureVector<byte> RSA_PublicKey::verify(const byte in[], u32bit len) const + { + BigInt i(in, len); + return BigInt::encode(public_op(i)); + } + +/* +* Create a RSA private key +*/ +RSA_PrivateKey::RSA_PrivateKey(RandomNumberGenerator& rng, + u32bit bits, u32bit exp) + { + if(bits < 512) + throw Invalid_Argument(algo_name() + ": Can't make a key that is only " + + to_string(bits) + " bits long"); + if(exp < 3 || exp % 2 == 0) + throw Invalid_Argument(algo_name() + ": Invalid encryption exponent"); + + e = exp; + p = random_prime(rng, (bits + 1) / 2, e); + q = random_prime(rng, bits - p.bits(), e); + d = inverse_mod(e, lcm(p - 1, q - 1)); + + PKCS8_load_hook(rng, true); + + if(n.bits() != bits) + throw Self_Test_Failure(algo_name() + " private key generation failed"); + } + +/* +* RSA_PrivateKey Constructor +*/ +RSA_PrivateKey::RSA_PrivateKey(RandomNumberGenerator& rng, + const BigInt& prime1, const BigInt& prime2, + const BigInt& exp, const BigInt& d_exp, + const BigInt& mod) + { + p = prime1; + q = prime2; + e = exp; + d = d_exp; + n = mod; + + if(d == 0) + d = inverse_mod(e, lcm(p - 1, q - 1)); + + PKCS8_load_hook(rng); + } + +/* +* RSA Private Operation +*/ +BigInt RSA_PrivateKey::private_op(const byte in[], u32bit length) const + { + BigInt i(in, length); + if(i >= n) + throw Invalid_Argument(algo_name() + "::private_op: input is too large"); + + BigInt r = core.private_op(i); + if(i != public_op(r)) + throw Self_Test_Failure(algo_name() + " private operation check failed"); + return r; + } + +/* +* RSA Decryption Operation +*/ +SecureVector<byte> RSA_PrivateKey::decrypt(const byte in[], u32bit len) const + { + return BigInt::encode(private_op(in, len)); + } + +/* +* RSA Signature Operation +*/ +SecureVector<byte> RSA_PrivateKey::sign(const byte in[], u32bit len, + RandomNumberGenerator&) const + { + return BigInt::encode_1363(private_op(in, len), n.bytes()); + } + +/* +* Check Private RSA Parameters +*/ +bool RSA_PrivateKey::check_key(RandomNumberGenerator& rng, bool strong) const + { + if(!IF_Scheme_PrivateKey::check_key(rng, strong)) + return false; + + if(!strong) + return true; + + if((e * d) % lcm(p - 1, q - 1) != 1) + return false; + + try + { + KeyPair::check_key(rng, + get_pk_encryptor(*this, "EME1(SHA-1)"), + get_pk_decryptor(*this, "EME1(SHA-1)") + ); + + KeyPair::check_key(rng, + get_pk_signer(*this, "EMSA4(SHA-1)"), + get_pk_verifier(*this, "EMSA4(SHA-1)") + ); + } + catch(Self_Test_Failure) + { + return false; + } + + return true; + } + +} diff --git a/botan/src/pubkey/rsa/rsa.h b/botan/src/pubkey/rsa/rsa.h new file mode 100644 index 0000000..f07533a --- /dev/null +++ b/botan/src/pubkey/rsa/rsa.h @@ -0,0 +1,88 @@ +/* +* RSA +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_RSA_H__ +#define BOTAN_RSA_H__ + +#include <botan/if_algo.h> + +namespace Botan { + +/** +* RSA Public Key +*/ +class BOTAN_DLL RSA_PublicKey : public PK_Encrypting_Key, + public PK_Verifying_with_MR_Key, + public virtual IF_Scheme_PublicKey + { + public: + std::string algo_name() const { return "RSA"; } + + SecureVector<byte> encrypt(const byte[], u32bit, + RandomNumberGenerator& rng) const; + + SecureVector<byte> verify(const byte[], u32bit) const; + + RSA_PublicKey() {} + RSA_PublicKey(const BigInt&, const BigInt&); + protected: + BigInt public_op(const BigInt&) const; + }; + +/** +* RSA Private Key class. +*/ +class BOTAN_DLL RSA_PrivateKey : public RSA_PublicKey, + public PK_Decrypting_Key, + public PK_Signing_Key, + public IF_Scheme_PrivateKey + { + public: + SecureVector<byte> sign(const byte[], u32bit, + RandomNumberGenerator&) const; + + SecureVector<byte> decrypt(const byte[], u32bit) const; + + bool check_key(RandomNumberGenerator& rng, bool) const; + + /** + * Default constructor, does not set any internal values. Use this + * constructor if you wish to decode a DER or PEM encoded key. + */ + RSA_PrivateKey() {} + + /** + * Construct a private key from the specified parameters. + * @param rng the random number generator to use + * @param prime1 the first prime + * @param prime2 the second prime + * @param exp the exponent + * @param d_exp if specified, this has to be d with + * exp * d = 1 mod (p - 1, q - 1). Leave it as 0 if you wish to + * the constructor to calculate it. + * @param n if specified, this must be n = p * q. Leave it as 0 + * if you wish to the constructor to calculate it. + */ + RSA_PrivateKey(RandomNumberGenerator& rng, + const BigInt& p, const BigInt& q, const BigInt& e, + const BigInt& d = 0, const BigInt& n = 0); + + /** + * Create a new private key with the specified bit length + * @param rng the random number generator to use + * @param bits the desired bit length of the private key + * @param exp the public exponent to be used + */ + RSA_PrivateKey(RandomNumberGenerator& rng, + u32bit bits, u32bit exp = 65537); + private: + BigInt private_op(const byte[], u32bit) const; + }; + +} + +#endif diff --git a/botan/src/pubkey/rw/info.txt b/botan/src/pubkey/rw/info.txt new file mode 100644 index 0000000..ada6c37 --- /dev/null +++ b/botan/src/pubkey/rw/info.txt @@ -0,0 +1,17 @@ +realname "Rabin-Williams" + +define RW + +load_on auto + +<add> +rw.cpp +rw.h +</add> + +<requires> +if_algo +keypair +libstate +numbertheory +</requires> diff --git a/botan/src/pubkey/rw/rw.cpp b/botan/src/pubkey/rw/rw.cpp new file mode 100644 index 0000000..def0ae6 --- /dev/null +++ b/botan/src/pubkey/rw/rw.cpp @@ -0,0 +1,148 @@ +/* +* Rabin-Williams +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/rw.h> +#include <botan/numthry.h> +#include <botan/keypair.h> +#include <botan/look_pk.h> +#include <botan/parsing.h> +#include <algorithm> + +namespace Botan { + +/* +* RW_PublicKey Constructor +*/ +RW_PublicKey::RW_PublicKey(const BigInt& mod, const BigInt& exp) + { + n = mod; + e = exp; + X509_load_hook(); + } + +/* +* Rabin-Williams Public Operation +*/ +BigInt RW_PublicKey::public_op(const BigInt& i) const + { + if((i > (n >> 1)) || i.is_negative()) + throw Invalid_Argument(algo_name() + "::public_op: i > n / 2 || i < 0"); + + BigInt r = core.public_op(i); + if(r % 16 == 12) return r; + if(r % 8 == 6) return 2*r; + + r = n - r; + if(r % 16 == 12) return r; + if(r % 8 == 6) return 2*r; + + throw Invalid_Argument(algo_name() + "::public_op: Invalid input"); + } + +/* +* Rabin-Williams Verification Function +*/ +SecureVector<byte> RW_PublicKey::verify(const byte in[], u32bit len) const + { + BigInt i(in, len); + return BigInt::encode(public_op(i)); + } + +/* +* Create a Rabin-Williams private key +*/ +RW_PrivateKey::RW_PrivateKey(RandomNumberGenerator& rng, + u32bit bits, u32bit exp) + { + if(bits < 512) + throw Invalid_Argument(algo_name() + ": Can't make a key that is only " + + to_string(bits) + " bits long"); + if(exp < 2 || exp % 2 == 1) + throw Invalid_Argument(algo_name() + ": Invalid encryption exponent"); + + e = exp; + p = random_prime(rng, (bits + 1) / 2, e / 2, 3, 4); + q = random_prime(rng, bits - p.bits(), e / 2, ((p % 8 == 3) ? 7 : 3), 8); + d = inverse_mod(e, lcm(p - 1, q - 1) >> 1); + + PKCS8_load_hook(rng, true); + + if(n.bits() != bits) + throw Self_Test_Failure(algo_name() + " private key generation failed"); + } + +/* +* RW_PrivateKey Constructor +*/ +RW_PrivateKey::RW_PrivateKey(RandomNumberGenerator& rng, + const BigInt& prime1, const BigInt& prime2, + const BigInt& exp, const BigInt& d_exp, + const BigInt& mod) + { + p = prime1; + q = prime2; + e = exp; + d = d_exp; + n = mod; + + if(d == 0) + d = inverse_mod(e, lcm(p - 1, q - 1) >> 1); + + PKCS8_load_hook(rng); + } + +/* +* Rabin-Williams Signature Operation +*/ +SecureVector<byte> RW_PrivateKey::sign(const byte in[], u32bit len, + RandomNumberGenerator&) const + { + BigInt i(in, len); + if(i >= n || i % 16 != 12) + throw Invalid_Argument(algo_name() + "::sign: Invalid input"); + + BigInt r; + if(jacobi(i, n) == 1) r = core.private_op(i); + else r = core.private_op(i >> 1); + + r = std::min(r, n - r); + if(i != public_op(r)) + throw Self_Test_Failure(algo_name() + " private operation check failed"); + + return BigInt::encode_1363(r, n.bytes()); + } + +/* +* Check Private Rabin-Williams Parameters +*/ +bool RW_PrivateKey::check_key(RandomNumberGenerator& rng, bool strong) const + { + if(!IF_Scheme_PrivateKey::check_key(rng, strong)) + return false; + + if(!strong) + return true; + + if((e * d) % (lcm(p - 1, q - 1) / 2) != 1) + return false; + + try + { + KeyPair::check_key(rng, + get_pk_signer(*this, "EMSA2(SHA-1)"), + get_pk_verifier(*this, "EMSA2(SHA-1)") + ); + } + catch(Self_Test_Failure) + { + return false; + } + + return true; + } + +} diff --git a/botan/src/pubkey/rw/rw.h b/botan/src/pubkey/rw/rw.h new file mode 100644 index 0000000..900e5eb --- /dev/null +++ b/botan/src/pubkey/rw/rw.h @@ -0,0 +1,56 @@ +/* +* Rabin-Williams +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_RW_H__ +#define BOTAN_RW_H__ + +#include <botan/if_algo.h> + +namespace Botan { + +/* +* Rabin-Williams Public Key +*/ +class BOTAN_DLL RW_PublicKey : public PK_Verifying_with_MR_Key, + public virtual IF_Scheme_PublicKey + { + public: + std::string algo_name() const { return "RW"; } + + SecureVector<byte> verify(const byte[], u32bit) const; + + RW_PublicKey() {} + RW_PublicKey(const BigInt&, const BigInt&); + protected: + BigInt public_op(const BigInt&) const; + }; + +/* +* Rabin-Williams Private Key +*/ +class BOTAN_DLL RW_PrivateKey : public RW_PublicKey, + public PK_Signing_Key, + public IF_Scheme_PrivateKey + { + public: + SecureVector<byte> sign(const byte[], u32bit, + RandomNumberGenerator& rng) const; + + bool check_key(RandomNumberGenerator& rng, bool) const; + + RW_PrivateKey() {} + + RW_PrivateKey(RandomNumberGenerator&, + const BigInt&, const BigInt&, const BigInt&, + const BigInt& = 0, const BigInt& = 0); + + RW_PrivateKey(RandomNumberGenerator& rng, u32bit bits, u32bit = 2); + }; + +} + +#endif diff --git a/botan/src/rng/auto_rng/auto_rng.cpp b/botan/src/rng/auto_rng/auto_rng.cpp new file mode 100644 index 0000000..8405170 --- /dev/null +++ b/botan/src/rng/auto_rng/auto_rng.cpp @@ -0,0 +1,158 @@ +/* +* Auto Seeded RNG +* (C) 2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/auto_rng.h> +#include <botan/parsing.h> +#include <botan/timer.h> +#include <botan/hmac.h> +#include <botan/sha2_32.h> +#include <botan/sha2_64.h> + +#if defined(BOTAN_HAS_RANDPOOL) + #include <botan/randpool.h> +#endif + +#if defined(BOTAN_HAS_HMAC_RNG) + #include <botan/hmac_rng.h> +#endif + +#if defined(BOTAN_HAS_X931_RNG) + #include <botan/x931_rng.h> +#endif + +#if defined(BOTAN_HAS_AES) + #include <botan/aes.h> +#endif + +#if defined(BOTAN_HAS_TIMER_HARDWARE) + #include <botan/tm_hard.h> +#endif + +#if defined(BOTAN_HAS_TIMER_POSIX) + #include <botan/tm_posix.h> +#endif + +#if defined(BOTAN_HAS_TIMER_UNIX) + #include <botan/tm_unix.h> +#endif + +#if defined(BOTAN_HAS_TIMER_WIN32) + #include <botan/tm_win32.h> +#endif + +#if defined(BOTAN_HAS_ENTROPY_SRC_DEVICE) + #include <botan/es_dev.h> +#endif + +#if defined(BOTAN_HAS_ENTROPY_SRC_EGD) + #include <botan/es_egd.h> +#endif + +#if defined(BOTAN_HAS_ENTROPY_SRC_UNIX) + #include <botan/es_unix.h> +#endif + +#if defined(BOTAN_HAS_ENTROPY_SRC_BEOS) + #include <botan/es_beos.h> +#endif + +#if defined(BOTAN_HAS_ENTROPY_SRC_CAPI) + #include <botan/es_capi.h> +#endif + +#if defined(BOTAN_HAS_ENTROPY_SRC_WIN32) + #include <botan/es_win32.h> +#endif + +#if defined(BOTAN_HAS_ENTROPY_SRC_FTW) + #include <botan/es_ftw.h> +#endif + +namespace Botan { + +namespace { + +/** +* Add any known entropy sources to this RNG +*/ +void add_entropy_sources(RandomNumberGenerator* rng) + { + + // Add a high resolution timer, if available +#if defined(BOTAN_HAS_TIMER_HARDWARE) + rng->add_entropy_source(new Hardware_Timer); +#elif defined(BOTAN_HAS_TIMER_POSIX) + rng->add_entropy_source(new POSIX_Timer); +#elif defined(BOTAN_HAS_TIMER_UNIX) + rng->add_entropy_source(new Unix_Timer); +#elif defined(BOTAN_HAS_TIMER_WIN32) + rng->add_entropy_source(new Win32_Timer); +#endif + +#if defined(BOTAN_HAS_ENTROPY_SRC_DEVICE) + rng->add_entropy_source( + new Device_EntropySource( + split_on("/dev/urandom:/dev/random:/dev/srandom", ':') + ) + ); +#endif + +#if defined(BOTAN_HAS_ENTROPY_SRC_EGD) + rng->add_entropy_source( + new EGD_EntropySource(split_on("/var/run/egd-pool:/dev/egd-pool", ':')) + ); +#endif + +#if defined(BOTAN_HAS_ENTROPY_SRC_CAPI) + rng->add_entropy_source(new Win32_CAPI_EntropySource); +#endif + +#if defined(BOTAN_HAS_ENTROPY_SRC_FTW) + rng->add_entropy_source(new FTW_EntropySource("/proc")); +#endif + +#if defined(BOTAN_HAS_ENTROPY_SRC_WIN32) + rng->add_entropy_source(new Win32_EntropySource); +#endif + +#if defined(BOTAN_HAS_ENTROPY_SRC_BEOS) + rng->add_entropy_source(new BeOS_EntropySource); +#endif + +#if defined(BOTAN_HAS_ENTROPY_SRC_UNIX) + rng->add_entropy_source( + new Unix_EntropySource(split_on("/bin:/sbin:/usr/bin:/usr/sbin", ':')) + ); +#endif + } + +} + +AutoSeeded_RNG::AutoSeeded_RNG(u32bit poll_bits) + { + rng = 0; + +#if defined(BOTAN_HAS_HMAC_RNG) + rng = new HMAC_RNG(new HMAC(new SHA_512), new HMAC(new SHA_256)); +#elif defined(BOTAN_HAS_RANDPOOL) && defined(BOTAN_HAS_AES) + rng = new Randpool(new AES_256, new HMAC(new SHA_256)); +#endif + + if(!rng) + throw Algorithm_Not_Found("No usable RNG found enabled in build"); + + /* If X9.31 is available, use it to wrap the other RNG as a failsafe */ +#if defined(BOTAN_HAS_X931_RNG) && defined(BOTAN_HAS_AES) + rng = new ANSI_X931_RNG(new AES_256, rng); +#endif + + add_entropy_sources(rng); + + rng->reseed(poll_bits); + } + +} diff --git a/botan/src/rng/auto_rng/auto_rng.h b/botan/src/rng/auto_rng/auto_rng.h new file mode 100644 index 0000000..f18f8e5 --- /dev/null +++ b/botan/src/rng/auto_rng/auto_rng.h @@ -0,0 +1,44 @@ +/* +* Auto Seeded RNG +* (C) 2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_AUTO_SEEDING_RNG_H__ +#define BOTAN_AUTO_SEEDING_RNG_H__ + +#include <botan/rng.h> +#include <string> + +namespace Botan { + +/** +* RNG that attempts to seed itself +*/ +class BOTAN_DLL AutoSeeded_RNG : public RandomNumberGenerator + { + public: + void randomize(byte out[], u32bit len) + { rng->randomize(out, len); } + bool is_seeded() const + { return rng->is_seeded(); } + void clear() throw() { rng->clear(); } + std::string name() const + { return "AutoSeeded(" + rng->name() + ")"; } + + void reseed(u32bit poll_bits = 256) { rng->reseed(poll_bits); } + void add_entropy_source(EntropySource* es) + { rng->add_entropy_source(es); } + void add_entropy(const byte in[], u32bit len) + { rng->add_entropy(in, len); } + + AutoSeeded_RNG(u32bit poll_bits = 256); + ~AutoSeeded_RNG() { delete rng; } + private: + RandomNumberGenerator* rng; + }; + +} + +#endif diff --git a/botan/src/rng/auto_rng/info.txt b/botan/src/rng/auto_rng/info.txt new file mode 100644 index 0000000..7d5d5dd --- /dev/null +++ b/botan/src/rng/auto_rng/info.txt @@ -0,0 +1,16 @@ +realname "Auto-seeded Random Number Generator" + +define AUTO_SEEDING_RNG + +load_on auto + +<add> +auto_rng.h +auto_rng.cpp +</add> + +<requires> +hmac +sha2 +timer +</requires> diff --git a/botan/src/rng/hmac_rng/hmac_rng.cpp b/botan/src/rng/hmac_rng/hmac_rng.cpp new file mode 100644 index 0000000..113489d --- /dev/null +++ b/botan/src/rng/hmac_rng/hmac_rng.cpp @@ -0,0 +1,223 @@ +/* +* HMAC_RNG +* (C) 2008-2009 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/hmac_rng.h> +#include <botan/loadstor.h> +#include <botan/xor_buf.h> +#include <botan/util.h> +#include <botan/stl_util.h> +#include <algorithm> + +namespace Botan { + +namespace { + +void hmac_prf(MessageAuthenticationCode* prf, + MemoryRegion<byte>& K, + u32bit& counter, + const std::string& label) + { + prf->update(K, K.size()); + prf->update(label); + for(u32bit i = 0; i != 4; ++i) + prf->update(get_byte(i, counter)); + prf->final(K); + + ++counter; + } + +} + +/** +* Generate a buffer of random bytes +*/ +void HMAC_RNG::randomize(byte out[], u32bit length) + { + if(!is_seeded()) + throw PRNG_Unseeded(name()); + + /* + HMAC KDF as described in E-t-E, using a CTXinfo of "rng" + */ + while(length) + { + hmac_prf(prf, K, counter, "rng"); + + const u32bit copied = std::min(K.size(), length); + + copy_mem(out, K.begin(), copied); + out += copied; + length -= copied; + } + } + +/** +* Reseed the internal state, also accepting user input to include +*/ +void HMAC_RNG::reseed_with_input(u32bit poll_bits, + const byte input[], u32bit input_length) + { + /** + Using the terminology of E-t-E, XTR is the MAC function (normally + HMAC) seeded with XTS (below) and we form SKM, the key material, by + fast polling each source, and then slow polling as many as we think + we need (in the following loop), and feeding all of the poll + results, along with any optional user input, along with, finally, + feedback of the current PRK value, into the extractor function. + */ + + Entropy_Accumulator_BufferedComputation accum(*extractor, poll_bits); + + if(!entropy_sources.empty()) + { + u32bit poll_attempt = 0; + + while(!accum.polling_goal_achieved() && poll_attempt < poll_bits) + { + entropy_sources[poll_attempt % entropy_sources.size()]->poll(accum); + ++poll_attempt; + } + } + + // And now add the user-provided input, if any + if(input_length) + accum.add(input, input_length, 1); + + /* + It is necessary to feed forward poll data. Otherwise, a good poll + (collecting a large amount of conditional entropy) followed by a + bad one (collecting little) would be unsafe. Do this by generating + new PRF outputs using the previous key and feeding them into the + extractor function. + + Cycle the RNG once (CTXinfo="rng"), then generate a new PRF output + using the CTXinfo "reseed". Provide these values as input to the + extractor function. + */ + hmac_prf(prf, K, counter, "rng"); + extractor->update(K); // K is the CTXinfo=rng PRF output + + hmac_prf(prf, K, counter, "reseed"); + extractor->update(K); // K is the CTXinfo=reseed PRF output + + /* Now derive the new PRK using everything that has been fed into + the extractor, and set the PRF key to that */ + prf->set_key(extractor->final()); + + // Now generate a new PRF output to use as the XTS extractor salt + hmac_prf(prf, K, counter, "xts"); + extractor->set_key(K, K.size()); + + // Reset state + K.clear(); + counter = 0; + + if(input_length || accum.bits_collected() >= poll_bits) + seeded = true; + } + +/** +* Reseed the internal state +*/ +void HMAC_RNG::reseed(u32bit poll_bits) + { + reseed_with_input(poll_bits, 0, 0); + } + +/** +* Add user-supplied entropy by reseeding and including this +* input among the poll data +*/ +void HMAC_RNG::add_entropy(const byte input[], u32bit length) + { + reseed_with_input(0, input, length); + } + +/** +* Add another entropy source to the list +*/ +void HMAC_RNG::add_entropy_source(EntropySource* src) + { + entropy_sources.push_back(src); + } + +/* +* Clear memory of sensitive data +*/ +void HMAC_RNG::clear() throw() + { + extractor->clear(); + prf->clear(); + K.clear(); + counter = 0; + seeded = false; + } + +/** +* Return the name of this type +*/ +std::string HMAC_RNG::name() const + { + return "HMAC_RNG(" + extractor->name() + "," + prf->name() + ")"; + } + +/** +* HMAC_RNG Constructor +*/ +HMAC_RNG::HMAC_RNG(MessageAuthenticationCode* extractor_mac, + MessageAuthenticationCode* prf_mac) : + extractor(extractor_mac), prf(prf_mac) + { + // First PRF inputs are all zero, as specified in section 2 + K.create(prf->OUTPUT_LENGTH); + counter = 0; + seeded = false; + + /* + Normally we want to feedback PRF output into the input to the + extractor function to ensure a single bad poll does not damage the + RNG, but obviously that is meaningless to do on the first poll. + + We will want to use the PRF before we set the first key (in + reseed_with_input), and it is a pain to keep track if it is set or + not. Since the first time it doesn't matter anyway, just set it to + a constant: randomize() will not produce output unless is_seeded() + returns true, and that will only be the case if the estimated + entropy counter is high enough. That variable is only set when a + reseeding is performed. + */ + std::string prf_key = "Botan HMAC_RNG PRF"; + prf->set_key(reinterpret_cast<const byte*>(prf_key.c_str()), + prf_key.length()); + + /* + This will be used as the first XTS value when extracting input. + XTS values after this one are generated using the PRF. + + If I understand the E-t-E paper correctly (specifically Section 4), + using this fixed extractor key is safe to do. + */ + std::string xts = "Botan HMAC_RNG XTS"; + extractor->set_key(reinterpret_cast<const byte*>(xts.c_str()), + xts.length()); + } + +/** +* HMAC_RNG Destructor +*/ +HMAC_RNG::~HMAC_RNG() + { + delete extractor; + delete prf; + + std::for_each(entropy_sources.begin(), entropy_sources.end(), + del_fun<EntropySource>()); + + counter = 0; + } + +} diff --git a/botan/src/rng/hmac_rng/hmac_rng.h b/botan/src/rng/hmac_rng/hmac_rng.h new file mode 100644 index 0000000..318e2a9 --- /dev/null +++ b/botan/src/rng/hmac_rng/hmac_rng.h @@ -0,0 +1,59 @@ +/* +* HMAC RNG +* (C) 2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_HMAC_RNG_H__ +#define BOTAN_HMAC_RNG_H__ + +#include <botan/mac.h> +#include <botan/rng.h> +#include <vector> + +namespace Botan { + +/** +HMAC_RNG - based on the design described in "On Extract-then-Expand +Key Derivation Functions and an HMAC-based KDF" by Hugo Krawczyk +(henceforce, 'E-t-E') + +However it actually can be parameterized with any two MAC functions, +not restricted to HMAC (this variation is also described in Krawczyk's +paper), for instance one could use HMAC(SHA-512) as the extractor +and CMAC(AES-256) as the PRF. +*/ +class BOTAN_DLL HMAC_RNG : public RandomNumberGenerator + { + public: + void randomize(byte buf[], u32bit len); + bool is_seeded() const { return seeded; } + void clear() throw(); + std::string name() const; + + void reseed(u32bit poll_bits); + void add_entropy_source(EntropySource* es); + void add_entropy(const byte[], u32bit); + + HMAC_RNG(MessageAuthenticationCode* extractor, + MessageAuthenticationCode* prf); + + ~HMAC_RNG(); + private: + void reseed_with_input(u32bit poll_bits, + const byte input[], u32bit length); + + MessageAuthenticationCode* extractor; + MessageAuthenticationCode* prf; + + std::vector<EntropySource*> entropy_sources; + bool seeded; + + SecureVector<byte> K, io_buffer; + u32bit counter, source_index; + }; + +} + +#endif diff --git a/botan/src/rng/hmac_rng/info.txt b/botan/src/rng/hmac_rng/info.txt new file mode 100644 index 0000000..2c7f13e --- /dev/null +++ b/botan/src/rng/hmac_rng/info.txt @@ -0,0 +1,14 @@ +realname "HMAC RNG" + +define HMAC_RNG + +load_on auto + +<add> +hmac_rng.cpp +hmac_rng.h +</add> + +<requires> +mac +</requires> diff --git a/botan/src/rng/info.txt b/botan/src/rng/info.txt new file mode 100644 index 0000000..44a4166 --- /dev/null +++ b/botan/src/rng/info.txt @@ -0,0 +1,12 @@ +realname "Random Number Generators" + +load_on auto + +<add> +rng.cpp +rng.h +</add> + +<requires> +entropy +</requires> diff --git a/botan/src/rng/randpool/info.txt b/botan/src/rng/randpool/info.txt new file mode 100644 index 0000000..cc7f615 --- /dev/null +++ b/botan/src/rng/randpool/info.txt @@ -0,0 +1,15 @@ +realname "Randpool RNG" + +define RANDPOOL + +load_on auto + +<add> +randpool.cpp +randpool.h +</add> + +<requires> +block +mac +</requires> diff --git a/botan/src/rng/randpool/randpool.cpp b/botan/src/rng/randpool/randpool.cpp new file mode 100644 index 0000000..4d7b92d --- /dev/null +++ b/botan/src/rng/randpool/randpool.cpp @@ -0,0 +1,214 @@ +/* +* Randpool +* (C) 1999-2009 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/randpool.h> +#include <botan/loadstor.h> +#include <botan/xor_buf.h> +#include <botan/util.h> +#include <botan/stl_util.h> +#include <algorithm> + +namespace Botan { + +namespace { + +/** +* PRF based on a MAC +*/ +enum RANDPOOL_PRF_TAG { + CIPHER_KEY = 0, + MAC_KEY = 1, + GEN_OUTPUT = 2 +}; + +} + +/** +* Generate a buffer of random bytes +*/ +void Randpool::randomize(byte out[], u32bit length) + { + if(!is_seeded()) + throw PRNG_Unseeded(name()); + + update_buffer(); + while(length) + { + const u32bit copied = std::min(length, buffer.size()); + copy_mem(out, buffer.begin(), copied); + out += copied; + length -= copied; + update_buffer(); + } + } + +/** +* Refill the output buffer +*/ +void Randpool::update_buffer() + { + const u64bit timestamp = system_time(); + + for(u32bit i = 0; i != counter.size(); ++i) + if(++counter[i]) + break; + store_be(timestamp, counter + 4); + + mac->update(static_cast<byte>(GEN_OUTPUT)); + mac->update(counter, counter.size()); + SecureVector<byte> mac_val = mac->final(); + + for(u32bit i = 0; i != mac_val.size(); ++i) + buffer[i % buffer.size()] ^= mac_val[i]; + cipher->encrypt(buffer); + + if(counter[0] % ITERATIONS_BEFORE_RESEED == 0) + mix_pool(); + } + +/** +* Mix the entropy pool +*/ +void Randpool::mix_pool() + { + const u32bit BLOCK_SIZE = cipher->BLOCK_SIZE; + + mac->update(static_cast<byte>(MAC_KEY)); + mac->update(pool, pool.size()); + mac->set_key(mac->final()); + + mac->update(static_cast<byte>(CIPHER_KEY)); + mac->update(pool, pool.size()); + cipher->set_key(mac->final()); + + xor_buf(pool, buffer, BLOCK_SIZE); + cipher->encrypt(pool); + for(u32bit i = 1; i != POOL_BLOCKS; ++i) + { + const byte* previous_block = pool + BLOCK_SIZE*(i-1); + byte* this_block = pool + BLOCK_SIZE*i; + xor_buf(this_block, previous_block, BLOCK_SIZE); + cipher->encrypt(this_block); + } + + update_buffer(); + } + +/** +* Reseed the internal state +*/ +void Randpool::reseed(u32bit poll_bits) + { + Entropy_Accumulator_BufferedComputation accum(*mac, poll_bits); + + if(!entropy_sources.empty()) + { + u32bit poll_attempt = 0; + + while(!accum.polling_goal_achieved() && poll_attempt < poll_bits) + { + entropy_sources[poll_attempt % entropy_sources.size()]->poll(accum); + ++poll_attempt; + } + } + + SecureVector<byte> mac_val = mac->final(); + + xor_buf(pool, mac_val, mac_val.size()); + mix_pool(); + + if(accum.bits_collected() >= poll_bits) + seeded = true; + } + +/** +* Add user-supplied entropy +*/ +void Randpool::add_entropy(const byte input[], u32bit length) + { + SecureVector<byte> mac_val = mac->process(input, length); + xor_buf(pool, mac_val, mac_val.size()); + mix_pool(); + + if(length) + seeded = true; + } + +/** +* Add another entropy source to the list +*/ +void Randpool::add_entropy_source(EntropySource* src) + { + entropy_sources.push_back(src); + } + +/** +* Clear memory of sensitive data +*/ +void Randpool::clear() throw() + { + cipher->clear(); + mac->clear(); + pool.clear(); + buffer.clear(); + counter.clear(); + seeded = false; + } + +/** +* Return the name of this type +*/ +std::string Randpool::name() const + { + return "Randpool(" + cipher->name() + "," + mac->name() + ")"; + } + +/** +* Randpool Constructor +*/ +Randpool::Randpool(BlockCipher* cipher_in, + MessageAuthenticationCode* mac_in, + u32bit pool_blocks, + u32bit iter_before_reseed) : + ITERATIONS_BEFORE_RESEED(iter_before_reseed), + POOL_BLOCKS(pool_blocks), + cipher(cipher_in), + mac(mac_in) + { + const u32bit BLOCK_SIZE = cipher->BLOCK_SIZE; + const u32bit OUTPUT_LENGTH = mac->OUTPUT_LENGTH; + + if(OUTPUT_LENGTH < BLOCK_SIZE || + !cipher->valid_keylength(OUTPUT_LENGTH) || + !mac->valid_keylength(OUTPUT_LENGTH)) + { + std::string ciphername = cipher->name(), macname = mac->name(); + delete cipher; + delete mac; + throw Internal_Error("Randpool: Invalid algorithm combination " + + ciphername + "/" + macname); + } + + buffer.create(BLOCK_SIZE); + pool.create(POOL_BLOCKS * BLOCK_SIZE); + counter.create(12); + seeded = false; + } + +/** +* Randpool Destructor +*/ +Randpool::~Randpool() + { + delete cipher; + delete mac; + + std::for_each(entropy_sources.begin(), entropy_sources.end(), + del_fun<EntropySource>()); + } + +} diff --git a/botan/src/rng/randpool/randpool.h b/botan/src/rng/randpool/randpool.h new file mode 100644 index 0000000..b6a3add --- /dev/null +++ b/botan/src/rng/randpool/randpool.h @@ -0,0 +1,53 @@ +/* +* Randpool +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_RANDPOOL_H__ +#define BOTAN_RANDPOOL_H__ + +#include <botan/rng.h> +#include <botan/block_cipher.h> +#include <botan/mac.h> +#include <vector> + +namespace Botan { + +/** +* Randpool +*/ +class BOTAN_DLL Randpool : public RandomNumberGenerator + { + public: + void randomize(byte[], u32bit); + bool is_seeded() const { return seeded; } + void clear() throw(); + std::string name() const; + + void reseed(u32bit bits_to_collect); + void add_entropy_source(EntropySource* es); + void add_entropy(const byte input[], u32bit length); + + Randpool(BlockCipher* cipher, MessageAuthenticationCode* mac, + u32bit pool_blocks = 32, + u32bit iterations_before_reseed = 128); + + ~Randpool(); + private: + void update_buffer(); + void mix_pool(); + + u32bit ITERATIONS_BEFORE_RESEED, POOL_BLOCKS; + BlockCipher* cipher; + MessageAuthenticationCode* mac; + + std::vector<EntropySource*> entropy_sources; + SecureVector<byte> pool, buffer, counter; + bool seeded; + }; + +} + +#endif diff --git a/botan/src/rng/rng.cpp b/botan/src/rng/rng.cpp new file mode 100644 index 0000000..aa9b73f --- /dev/null +++ b/botan/src/rng/rng.cpp @@ -0,0 +1,38 @@ +/* +* Random Number Generator Base +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/rng.h> + +#if defined(BOTAN_HAS_AUTO_SEEDING_RNG) + #include <botan/auto_rng.h> +#endif + +namespace Botan { + +/* +* Get a single random byte +*/ +byte RandomNumberGenerator::next_byte() + { + byte out; + this->randomize(&out, 1); + return out; + } + +/* +* Create and seed a new RNG object +*/ +RandomNumberGenerator* RandomNumberGenerator::make_rng() + { +#if defined(BOTAN_HAS_AUTO_SEEDING_RNG) + return new AutoSeeded_RNG; +#endif + + throw Algorithm_Not_Found("RandomNumberGenerator::make_rng - no RNG found"); + } + +} diff --git a/botan/src/rng/rng.h b/botan/src/rng/rng.h new file mode 100644 index 0000000..41904db --- /dev/null +++ b/botan/src/rng/rng.h @@ -0,0 +1,103 @@ +/* +* RandomNumberGenerator +* (C) 1999-2009 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_RANDOM_NUMBER_GENERATOR_H__ +#define BOTAN_RANDOM_NUMBER_GENERATOR_H__ + +#include <botan/entropy_src.h> +#include <botan/exceptn.h> +#include <string> + +namespace Botan { + +/** +* This class represents a random number (RNG) generator object. +*/ +class BOTAN_DLL RandomNumberGenerator + { + public: + /** + * Create a seeded and active RNG object for general application use + */ + static RandomNumberGenerator* make_rng(); + + /** + * Randomize a byte array. + * @param output the byte array to hold the random output. + * @param length the length of the byte array output. + */ + virtual void randomize(byte output[], u32bit length) = 0; + + /** + * Return a random byte + * @return random byte + */ + byte next_byte(); + + /** + * Check whether this RNG is seeded. + * @return true if this RNG was already seeded, false otherwise. + */ + virtual bool is_seeded() const { return true; } + + /** + * Clear all internally held values of this RNG. + */ + virtual void clear() throw() = 0; + + /** + * Return the name of this object + */ + virtual std::string name() const = 0; + + /** + * Seed this RNG using the entropy sources it contains. + * @param bits_to_collect is the number of bits of entropy to + attempt to gather from the entropy sources + */ + virtual void reseed(u32bit bits_to_collect) = 0; + + /** + * Add this entropy source to the RNG object + * @param source the entropy source which will be retained and used by RNG + */ + virtual void add_entropy_source(EntropySource* source) = 0; + + /** + * Add entropy to this RNG. + * @param in a byte array containg the entropy to be added + * @param length the length of the byte array in + */ + virtual void add_entropy(const byte in[], u32bit length) = 0; + + RandomNumberGenerator() {} + virtual ~RandomNumberGenerator() {} + private: + RandomNumberGenerator(const RandomNumberGenerator&) {} + RandomNumberGenerator& operator=(const RandomNumberGenerator&) + { return (*this); } + }; + +/* +* Null Random Number Generator +*/ +class BOTAN_DLL Null_RNG : public RandomNumberGenerator + { + public: + void randomize(byte[], u32bit) { throw PRNG_Unseeded("Null_RNG"); } + void clear() throw() {} + std::string name() const { return "Null_RNG"; } + + void reseed(u32bit) {} + bool is_seeded() const { return false; } + void add_entropy(const byte[], u32bit) {} + void add_entropy_source(EntropySource* es) { delete es; } + }; + +} + +#endif diff --git a/botan/src/rng/x931_rng/info.txt b/botan/src/rng/x931_rng/info.txt new file mode 100644 index 0000000..633eb02 --- /dev/null +++ b/botan/src/rng/x931_rng/info.txt @@ -0,0 +1,14 @@ +realname "ANSI X9.31 PRNG" + +define X931_RNG + +load_on auto + +<add> +x931_rng.cpp +x931_rng.h +</add> + +<requires> +block +</requires> diff --git a/botan/src/rng/x931_rng/x931_rng.cpp b/botan/src/rng/x931_rng/x931_rng.cpp new file mode 100644 index 0000000..e239bce --- /dev/null +++ b/botan/src/rng/x931_rng/x931_rng.cpp @@ -0,0 +1,154 @@ +/* +* ANSI X9.31 RNG +* (C) 1999-2009 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/x931_rng.h> +#include <botan/xor_buf.h> +#include <algorithm> + +namespace Botan { + +/** +* Generate a buffer of random bytes +*/ +void ANSI_X931_RNG::randomize(byte out[], u32bit length) + { + if(!is_seeded()) + throw PRNG_Unseeded(name()); + + while(length) + { + if(position == R.size()) + update_buffer(); + + const u32bit copied = std::min(length, R.size() - position); + + copy_mem(out, R + position, copied); + out += copied; + length -= copied; + position += copied; + } + } + +/** +* Refill the internal state +*/ +void ANSI_X931_RNG::update_buffer() + { + SecureVector<byte> DT(cipher->BLOCK_SIZE); + + prng->randomize(DT, DT.size()); + cipher->encrypt(DT); + + xor_buf(R, V, DT, cipher->BLOCK_SIZE); + cipher->encrypt(R); + + xor_buf(V, R, DT, cipher->BLOCK_SIZE); + cipher->encrypt(V); + + position = 0; + } + +/** +* Reset V and the cipher key with new values +*/ +void ANSI_X931_RNG::rekey() + { + if(prng->is_seeded()) + { + SecureVector<byte> key(cipher->MAXIMUM_KEYLENGTH); + prng->randomize(key, key.size()); + cipher->set_key(key, key.size()); + + if(V.size() != cipher->BLOCK_SIZE) + V.create(cipher->BLOCK_SIZE); + prng->randomize(V, V.size()); + + update_buffer(); + } + } + +/** +* Reseed the internal state +*/ +void ANSI_X931_RNG::reseed(u32bit poll_bits) + { + prng->reseed(poll_bits); + rekey(); + } + +/** +* Add a entropy source to the underlying PRNG +*/ +void ANSI_X931_RNG::add_entropy_source(EntropySource* src) + { + prng->add_entropy_source(src); + } + +/** +* Add some entropy to the underlying PRNG +*/ +void ANSI_X931_RNG::add_entropy(const byte input[], u32bit length) + { + prng->add_entropy(input, length); + rekey(); + } + +/** +* Check if the the PRNG is seeded +*/ +bool ANSI_X931_RNG::is_seeded() const + { + return V.has_items(); + } + +/** +* Clear memory of sensitive data +*/ +void ANSI_X931_RNG::clear() throw() + { + cipher->clear(); + prng->clear(); + R.clear(); + V.destroy(); + + position = 0; + } + +/** +* Return the name of this type +*/ +std::string ANSI_X931_RNG::name() const + { + return "X9.31(" + cipher->name() + ")"; + } + +/** +* ANSI X931 RNG Constructor +*/ +ANSI_X931_RNG::ANSI_X931_RNG(BlockCipher* cipher_in, + RandomNumberGenerator* prng_in) + { + if(!prng_in || !cipher_in) + throw Invalid_Argument("ANSI_X931_RNG constructor: NULL arguments"); + + cipher = cipher_in; + prng = prng_in; + + R.create(cipher->BLOCK_SIZE); + position = 0; + } + +/** +* ANSI X931 RNG Destructor +*/ +ANSI_X931_RNG::~ANSI_X931_RNG() + { + delete cipher; + delete prng; + } + +} diff --git a/botan/src/rng/x931_rng/x931_rng.h b/botan/src/rng/x931_rng/x931_rng.h new file mode 100644 index 0000000..44e9b44 --- /dev/null +++ b/botan/src/rng/x931_rng/x931_rng.h @@ -0,0 +1,45 @@ +/* +* ANSI X9.31 RNG +* (C) 1999-2009 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ANSI_X931_RNG_H__ +#define BOTAN_ANSI_X931_RNG_H__ + +#include <botan/rng.h> +#include <botan/block_cipher.h> + +namespace Botan { + +/** +* ANSI X9.31 RNG +*/ +class BOTAN_DLL ANSI_X931_RNG : public RandomNumberGenerator + { + public: + void randomize(byte[], u32bit); + bool is_seeded() const; + void clear() throw(); + std::string name() const; + + void reseed(u32bit poll_bits); + void add_entropy_source(EntropySource*); + void add_entropy(const byte[], u32bit); + + ANSI_X931_RNG(BlockCipher*, RandomNumberGenerator*); + ~ANSI_X931_RNG(); + private: + void rekey(); + void update_buffer(); + + BlockCipher* cipher; + RandomNumberGenerator* prng; + SecureVector<byte> V, R; + u32bit position; + }; + +} + +#endif diff --git a/botan/src/s2k/info.txt b/botan/src/s2k/info.txt new file mode 100644 index 0000000..e603fd9 --- /dev/null +++ b/botan/src/s2k/info.txt @@ -0,0 +1,13 @@ +realname "String to Key Functions" + +load_on auto + +<add> +s2k.cpp +s2k.h +</add> + +<requires> +rng +sym_algo +</requires> diff --git a/botan/src/s2k/pbkdf1/info.txt b/botan/src/s2k/pbkdf1/info.txt new file mode 100644 index 0000000..4c5b275 --- /dev/null +++ b/botan/src/s2k/pbkdf1/info.txt @@ -0,0 +1,14 @@ +realname "Pbkdf1" + +define PBKDF1 + +load_on auto + +<add> +pbkdf1.cpp +pbkdf1.h +</add> + +<requires> +hash +</requires> diff --git a/botan/src/s2k/pbkdf1/pbkdf1.cpp b/botan/src/s2k/pbkdf1/pbkdf1.cpp new file mode 100644 index 0000000..04e3aa4 --- /dev/null +++ b/botan/src/s2k/pbkdf1/pbkdf1.cpp @@ -0,0 +1,55 @@ +/* +* PBKDF1 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/pbkdf1.h> + +namespace Botan { + +/* +* Return a PKCS#5 PBKDF1 derived key +*/ +OctetString PKCS5_PBKDF1::derive(u32bit key_len, + const std::string& passphrase, + const byte salt[], u32bit salt_size, + u32bit iterations) const + { + if(iterations == 0) + throw Invalid_Argument("PKCS#5 PBKDF1: Invalid iteration count"); + + if(key_len > hash->OUTPUT_LENGTH) + throw Exception("PKCS#5 PBKDF1: Requested output length too long"); + + hash->update(passphrase); + hash->update(salt, salt_size); + SecureVector<byte> key = hash->final(); + + for(u32bit j = 1; j != iterations; ++j) + { + hash->update(key); + hash->final(key); + } + + return OctetString(key, std::min(key_len, key.size())); + } + +/* +* Clone this type +*/ +S2K* PKCS5_PBKDF1::clone() const + { + return new PKCS5_PBKDF1(hash->clone()); + } + +/* +* Return the name of this type +*/ +std::string PKCS5_PBKDF1::name() const + { + return "PBKDF1(" + hash->name() + ")"; + } + +} diff --git a/botan/src/s2k/pbkdf1/pbkdf1.h b/botan/src/s2k/pbkdf1/pbkdf1.h new file mode 100644 index 0000000..4e5cafd --- /dev/null +++ b/botan/src/s2k/pbkdf1/pbkdf1.h @@ -0,0 +1,44 @@ +/* +* PBKDF1 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_PBKDF1_H__ +#define BOTAN_PBKDF1_H__ + +#include <botan/s2k.h> +#include <botan/hash.h> + +namespace Botan { + +/** +* This class implements the PKCS #5 PBKDF1 functionality. +*/ +class BOTAN_DLL PKCS5_PBKDF1 : public S2K + { + public: + std::string name() const; + S2K* clone() const; + + /** + * Create a PKCS #5 instance using the specified hash function. + * @param hash a pointer to a hash function object to use + */ + PKCS5_PBKDF1(HashFunction* hash_in) : hash(hash_in) {} + + PKCS5_PBKDF1(const PKCS5_PBKDF1& other) : + S2K(), hash(other.hash->clone()) {} + + ~PKCS5_PBKDF1() { delete hash; } + private: + OctetString derive(u32bit, const std::string&, + const byte[], u32bit, u32bit) const; + + HashFunction* hash; + }; + +} + +#endif diff --git a/botan/src/s2k/pbkdf2/info.txt b/botan/src/s2k/pbkdf2/info.txt new file mode 100644 index 0000000..921aeb1 --- /dev/null +++ b/botan/src/s2k/pbkdf2/info.txt @@ -0,0 +1,14 @@ +realname "Pbkdf2" + +define PBKDF2 + +load_on auto + +<add> +pbkdf2.cpp +pbkdf2.h +</add> + +<requires> +mac +</requires> diff --git a/botan/src/s2k/pbkdf2/pbkdf2.cpp b/botan/src/s2k/pbkdf2/pbkdf2.cpp new file mode 100644 index 0000000..1de27c9 --- /dev/null +++ b/botan/src/s2k/pbkdf2/pbkdf2.cpp @@ -0,0 +1,82 @@ +/* +* PBKDF2 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/pbkdf2.h> +#include <botan/loadstor.h> +#include <botan/xor_buf.h> + +namespace Botan { + +/* +* Return a PKCS#5 PBKDF2 derived key +*/ +OctetString PKCS5_PBKDF2::derive(u32bit key_len, + const std::string& passphrase, + const byte salt[], u32bit salt_size, + u32bit iterations) const + { + if(iterations == 0) + throw Invalid_Argument("PKCS#5 PBKDF2: Invalid iteration count"); + + if(passphrase.length() == 0) + throw Invalid_Argument("PKCS#5 PBKDF2: Empty passphrase is invalid"); + + mac->set_key(reinterpret_cast<const byte*>(passphrase.data()), + passphrase.length()); + + SecureVector<byte> key(key_len); + + byte* T = key.begin(); + + u32bit counter = 1; + while(key_len) + { + u32bit T_size = std::min(mac->OUTPUT_LENGTH, key_len); + SecureVector<byte> U(mac->OUTPUT_LENGTH); + + mac->update(salt, salt_size); + for(u32bit j = 0; j != 4; ++j) + mac->update(get_byte(j, counter)); + mac->final(U); + xor_buf(T, U, T_size); + + for(u32bit j = 1; j != iterations; ++j) + { + mac->update(U); + mac->final(U); + xor_buf(T, U, T_size); + } + + key_len -= T_size; + T += T_size; + ++counter; + } + + return key; + } + +/* +* Return the name of this type +*/ +std::string PKCS5_PBKDF2::name() const + { + return "PBKDF2(" + mac->name() + ")"; + } + +S2K* PKCS5_PBKDF2::clone() const + { + return new PKCS5_PBKDF2(mac->clone()); + } + +/* +* PKCS5_PBKDF2 Constructor +*/ +PKCS5_PBKDF2::PKCS5_PBKDF2(MessageAuthenticationCode* m) : mac(m) {} + +PKCS5_PBKDF2::~PKCS5_PBKDF2() { delete mac; } + +} diff --git a/botan/src/s2k/pbkdf2/pbkdf2.h b/botan/src/s2k/pbkdf2/pbkdf2.h new file mode 100644 index 0000000..7510338 --- /dev/null +++ b/botan/src/s2k/pbkdf2/pbkdf2.h @@ -0,0 +1,40 @@ +/* +* PBKDF2 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_PBKDF2_H__ +#define BOTAN_PBKDF2_H__ + +#include <botan/s2k.h> +#include <botan/mac.h> + +namespace Botan { + +/** +* This class implements the PKCS #5 PBKDF2 functionality. +*/ +class BOTAN_DLL PKCS5_PBKDF2 : public S2K + { + public: + std::string name() const; + S2K* clone() const; + + /** + * Create a PKCS #5 instance using the specified message auth code + * @param mac the MAC to use + */ + PKCS5_PBKDF2(MessageAuthenticationCode* mac); + ~PKCS5_PBKDF2(); + private: + OctetString derive(u32bit, const std::string&, + const byte[], u32bit, u32bit) const; + + MessageAuthenticationCode* mac; + }; + +} + +#endif diff --git a/botan/src/s2k/pgps2k/info.txt b/botan/src/s2k/pgps2k/info.txt new file mode 100644 index 0000000..14b75a0 --- /dev/null +++ b/botan/src/s2k/pgps2k/info.txt @@ -0,0 +1,14 @@ +realname "Pgps2k" + +define PGPS2K + +load_on auto + +<add> +pgp_s2k.cpp +pgp_s2k.h +</add> + +<requires> +hash +</requires> diff --git a/botan/src/s2k/pgps2k/pgp_s2k.cpp b/botan/src/s2k/pgps2k/pgp_s2k.cpp new file mode 100644 index 0000000..86394d8 --- /dev/null +++ b/botan/src/s2k/pgps2k/pgp_s2k.cpp @@ -0,0 +1,74 @@ +/* +* OpenPGP S2K +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/pgp_s2k.h> +#include <algorithm> +#include <memory> + +namespace Botan { + +/* +* Derive a key using the OpenPGP S2K algorithm +*/ +OctetString OpenPGP_S2K::derive(u32bit key_len, const std::string& passphrase, + const byte salt_buf[], u32bit salt_size, + u32bit iterations) const + { + SecureVector<byte> key(key_len), hash_buf; + + u32bit pass = 0, generated = 0, + total_size = passphrase.size() + salt_size; + u32bit to_hash = std::max(iterations, total_size); + + hash->clear(); + while(key_len > generated) + { + for(u32bit j = 0; j != pass; ++j) + hash->update(0); + + u32bit left = to_hash; + while(left >= total_size) + { + hash->update(salt_buf, salt_size); + hash->update(passphrase); + left -= total_size; + } + if(left <= salt_size) + hash->update(salt_buf, left); + else + { + hash->update(salt_buf, salt_size); + left -= salt_size; + hash->update(reinterpret_cast<const byte*>(passphrase.data()), left); + } + + hash_buf = hash->final(); + key.copy(generated, hash_buf, hash->OUTPUT_LENGTH); + generated += hash->OUTPUT_LENGTH; + ++pass; + } + + return key; + } + +/* +* Return the name of this type +*/ +std::string OpenPGP_S2K::name() const + { + return "OpenPGP-S2K(" + hash->name() + ")"; + } + +/* +* Return a clone of this object +*/ +S2K* OpenPGP_S2K::clone() const + { + return new OpenPGP_S2K(hash->clone()); + } + +} diff --git a/botan/src/s2k/pgps2k/pgp_s2k.h b/botan/src/s2k/pgps2k/pgp_s2k.h new file mode 100644 index 0000000..00e95f7 --- /dev/null +++ b/botan/src/s2k/pgps2k/pgp_s2k.h @@ -0,0 +1,36 @@ +/* +* OpenPGP S2K +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_OPENPGP_S2K_H__ +#define BOTAN_OPENPGP_S2K_H__ + +#include <botan/s2k.h> +#include <botan/hash.h> + +namespace Botan { + +/* +* OpenPGP S2K +*/ +class BOTAN_DLL OpenPGP_S2K : public S2K + { + public: + std::string name() const; + S2K* clone() const; + + OpenPGP_S2K(HashFunction* hash_in) : hash(hash_in) {} + ~OpenPGP_S2K() { delete hash; } + private: + OctetString derive(u32bit, const std::string&, + const byte[], u32bit, u32bit) const; + + HashFunction* hash; + }; + +} + +#endif diff --git a/botan/src/s2k/s2k.cpp b/botan/src/s2k/s2k.cpp new file mode 100644 index 0000000..b8a8ef7 --- /dev/null +++ b/botan/src/s2k/s2k.cpp @@ -0,0 +1,55 @@ +/* +* S2K +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/s2k.h> + +namespace Botan { + +/* +* Derive a key from a passphrase +*/ +OctetString S2K::derive_key(u32bit key_len, + const std::string& passphrase) const + { + return derive(key_len, passphrase, salt, salt.size(), iterations()); + } + +/* +* Set the number of iterations +*/ +void S2K::set_iterations(u32bit i) + { + iter = i; + } + +/* +* Change the salt +*/ +void S2K::change_salt(const byte new_salt[], u32bit length) + { + salt.set(new_salt, length); + } + +/* +* Change the salt +*/ +void S2K::change_salt(const MemoryRegion<byte>& new_salt) + { + change_salt(new_salt.begin(), new_salt.size()); + } + +/* +* Create a new random salt +*/ +void S2K::new_random_salt(RandomNumberGenerator& rng, + u32bit length) + { + salt.create(length); + rng.randomize(salt, length); + } + +} diff --git a/botan/src/s2k/s2k.h b/botan/src/s2k/s2k.h new file mode 100644 index 0000000..7af9251 --- /dev/null +++ b/botan/src/s2k/s2k.h @@ -0,0 +1,102 @@ +/* +* S2K +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_S2K_H__ +#define BOTAN_S2K_H__ + +#include <botan/symkey.h> +#include <botan/rng.h> + +namespace Botan { + +/* +* S2K Interface +*/ +class BOTAN_DLL S2K + { + public: + /** + * Create a copy of this object. + * @return an auto_ptr to a copy of this object + */ + virtual S2K* clone() const = 0; + + /** + * Get the algorithm name. + * @return the name of this S2K algorithm + */ + virtual std::string name() const = 0; + + /** + * Clear this objects internal values. + */ + virtual void clear() {} + + /** + * Derive a key from a passphrase with this S2K object. It will use + * the salt value and number of iterations configured in this object. + * @param key_len the desired length of the key to produce + * @param passphrase the password to derive the key from + */ + OctetString derive_key(u32bit key_len, + const std::string& passphrase) const; + + /** + * Set the number of iterations for the one-way function during + * key generation. + * @param n the desired number of iterations + */ + void set_iterations(u32bit n); + + /** + * Set a new salt value. + * @param new_salt a byte array defining the new salt value + * @param len the length of the above byte array + */ + void change_salt(const byte new_salt[], u32bit len); + + /** + * Set a new salt value. + * @param new_salt the new salt value + */ + void change_salt(const MemoryRegion<byte>& new_salt); + + /** + * Create a new random salt value using the rng + * @param rng the random number generator to use + * @param len the desired length of the new salt value + */ + void new_random_salt(RandomNumberGenerator& rng, u32bit len); + + /** + * Get the number of iterations for the key derivation currently + * configured in this S2K object. + * @return the current number of iterations + */ + u32bit iterations() const { return iter; } + + /** + * Get the currently configured salt value of this S2K object. + * @return the current salt value + */ + SecureVector<byte> current_salt() const { return salt; } + + S2K() { iter = 0; } + virtual ~S2K() {} + private: + S2K(const S2K&) {} + S2K& operator=(const S2K&) { return (*this); } + + virtual OctetString derive(u32bit, const std::string&, + const byte[], u32bit, u32bit) const = 0; + SecureVector<byte> salt; + u32bit iter; + }; + +} + +#endif diff --git a/botan/src/selftest/info.txt b/botan/src/selftest/info.txt new file mode 100644 index 0000000..323a610 --- /dev/null +++ b/botan/src/selftest/info.txt @@ -0,0 +1,21 @@ +realname "Selftests" + +define SELFTESTS + +load_on auto + +<add> +selftest.cpp +selftest.h +</add> + +<requires> +algo_factory +cbc +cfb +ctr +ecb +filters +hmac +ofb +</requires> diff --git a/botan/src/selftest/selftest.cpp b/botan/src/selftest/selftest.cpp new file mode 100644 index 0000000..d644e86 --- /dev/null +++ b/botan/src/selftest/selftest.cpp @@ -0,0 +1,184 @@ +/* +* Startup Self Tests +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/selftest.h> +#include <botan/filters.h> +#include <botan/ecb.h> +#include <botan/cbc.h> +#include <botan/cfb.h> +#include <botan/ofb.h> +#include <botan/ctr.h> +#include <botan/hmac.h> + +namespace Botan { + +namespace { + +/* +* Perform a Known Answer Test +*/ +void do_kat(const std::string& in, const std::string& out, + const std::string& algo_name, Filter* filter) + { + if(out.length()) + { + Pipe pipe(new Hex_Decoder, filter, new Hex_Encoder); + pipe.process_msg(in); + + if(out != pipe.read_all_as_string()) + throw Self_Test_Failure(algo_name + " startup test"); + } + } + +/* +* Perform a KAT for a cipher +*/ +void cipher_kat(const BlockCipher* proto, + const std::string& key_str, + const std::string& iv_str, + const std::string& in, + const std::string& ecb_out, + const std::string& cbc_out, + const std::string& cfb_out, + const std::string& ofb_out, + const std::string& ctr_out) + { + SymmetricKey key(key_str); + InitializationVector iv(iv_str); + + std::string name = proto->name(); + + do_kat(in, ecb_out, name + "/ECB", + new ECB_Encryption(proto->clone(), new Null_Padding, key)); + do_kat(ecb_out, in, name + "/ECB", + new ECB_Decryption(proto->clone(), new Null_Padding, key)); + + do_kat(in, cbc_out, name + "/CBC", + new CBC_Encryption(proto->clone(), new Null_Padding, key, iv)); + do_kat(cbc_out, in, name + "/CBC", + new CBC_Decryption(proto->clone(), new Null_Padding, key, iv)); + + do_kat(in, cfb_out, name + "/CFB", + new CFB_Encryption(proto->clone(), key, iv)); + do_kat(cfb_out, in, name + "/CFB", + new CFB_Decryption(proto->clone(), key, iv)); + + do_kat(in, ofb_out, name + "/OFB", new OFB(proto->clone(), key, iv)); + + do_kat(in, ctr_out, name + "/CTR-BE", + new CTR_BE(proto->clone(), key, iv)); + } + +} + +/* +* Perform Self Tests +*/ +bool passes_self_tests(Algorithm_Factory& af) + { + try + { + if(const BlockCipher* proto = af.prototype_block_cipher("DES")) + { + cipher_kat(proto, + "0123456789ABCDEF", "1234567890ABCDEF", + "4E6F77206973207468652074696D6520666F7220616C6C20", + "3FA40E8A984D48156A271787AB8883F9893D51EC4B563B53", + "E5C7CDDE872BF27C43E934008C389C0F683788499A7C05F6", + "F3096249C7F46E51A69E839B1A92F78403467133898EA622", + "F3096249C7F46E5135F24A242EEB3D3F3D6D5BE3255AF8C3", + "F3096249C7F46E51163A8CA0FFC94C27FA2F80F480B86F75"); + } + + if(const BlockCipher* proto = af.prototype_block_cipher("TripleDES")) + { + cipher_kat(proto, + "385D7189A5C3D485E1370AA5D408082B5CCCCB5E19F2D90E", + "C141B5FCCD28DC8A", + "6E1BD7C6120947A464A6AAB293A0F89A563D8D40D3461B68", + "64EAAD4ACBB9CEAD6C7615E7C7E4792FE587D91F20C7D2F4", + "6235A461AFD312973E3B4F7AA7D23E34E03371F8E8C376C9", + "E26BA806A59B0330DE40CA38E77A3E494BE2B212F6DD624B", + "E26BA806A59B03307DE2BCC25A08BA40A8BA335F5D604C62", + "E26BA806A59B03303C62C2EFF32D3ACDD5D5F35EBCC53371"); + } + + if(const BlockCipher* proto = af.prototype_block_cipher("AES")) + { + cipher_kat(proto, + "2B7E151628AED2A6ABF7158809CF4F3C", + "000102030405060708090A0B0C0D0E0F", + "6BC1BEE22E409F96E93D7E117393172A" + "AE2D8A571E03AC9C9EB76FAC45AF8E51", + "3AD77BB40D7A3660A89ECAF32466EF97" + "F5D3D58503B9699DE785895A96FDBAAF", + "7649ABAC8119B246CEE98E9B12E9197D" + "5086CB9B507219EE95DB113A917678B2", + "3B3FD92EB72DAD20333449F8E83CFB4A" + "C8A64537A0B3A93FCDE3CDAD9F1CE58B", + "3B3FD92EB72DAD20333449F8E83CFB4A" + "7789508D16918F03F53C52DAC54ED825", + "3B3FD92EB72DAD20333449F8E83CFB4A" + "010C041999E03F36448624483E582D0E"); + } + + if(const HashFunction* proto = af.prototype_hash_function("SHA-1")) + { + do_kat("", "DA39A3EE5E6B4B0D3255BFEF95601890AFD80709", + proto->name(), new Hash_Filter(proto->clone())); + + do_kat("616263", "A9993E364706816ABA3E25717850C26C9CD0D89D", + proto->name(), new Hash_Filter(proto->clone())); + + do_kat("6162636462636465636465666465666765666768666768696768696A" + "68696A6B696A6B6C6A6B6C6D6B6C6D6E6C6D6E6F6D6E6F706E6F7071", + "84983E441C3BD26EBAAE4AA1F95129E5E54670F1", + proto->name(), new Hash_Filter(proto->clone())); + + do_kat("4869205468657265", + "B617318655057264E28BC0B6FB378C8EF146BE00", + "HMAC(" + proto->name() + ")", + new MAC_Filter(new HMAC(proto->clone()), + SymmetricKey("0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B"))); + } + + if(const HashFunction* proto = af.prototype_hash_function("SHA-256")) + { + do_kat("", + "E3B0C44298FC1C149AFBF4C8996FB924" + "27AE41E4649B934CA495991B7852B855", + proto->name(), new Hash_Filter(proto->clone())); + + do_kat("616263", + "BA7816BF8F01CFEA414140DE5DAE2223" + "B00361A396177A9CB410FF61F20015AD", + proto->name(), new Hash_Filter(proto->clone())); + + do_kat("6162636462636465636465666465666765666768666768696768696A" + "68696A6B696A6B6C6A6B6C6D6B6C6D6E6C6D6E6F6D6E6F706E6F7071", + "248D6A61D20638B8E5C026930C3E6039" + "A33CE45964FF2167F6ECEDD419DB06C1", + proto->name(), new Hash_Filter(proto->clone())); + + do_kat("4869205468657265", + "198A607EB44BFBC69903A0F1CF2BBDC5" + "BA0AA3F3D9AE3C1C7A3B1696A0B68CF7", + "HMAC(" + proto->name() + ")", + new MAC_Filter(new HMAC(proto->clone()), + SymmetricKey("0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B" + "0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B"))); + } + } + catch(std::exception) + { + return false; + } + + return true; + } + +} diff --git a/botan/src/selftest/selftest.h b/botan/src/selftest/selftest.h new file mode 100644 index 0000000..9e36d22 --- /dev/null +++ b/botan/src/selftest/selftest.h @@ -0,0 +1,22 @@ +/* +* Startup Self Test +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_SELF_TESTS_H__ +#define BOTAN_SELF_TESTS_H__ + +#include <botan/algo_factory.h> + +namespace Botan { + +/* +* Self Tests +*/ +BOTAN_DLL bool passes_self_tests(Algorithm_Factory& af); + +} + +#endif diff --git a/botan/src/src.pro b/botan/src/src.pro new file mode 100644 index 0000000..08de10e --- /dev/null +++ b/botan/src/src.pro @@ -0,0 +1,573 @@ +TEMPLATE = lib +TARGET = Botan + +CONFIG += dll + +include(../../../../qtcreatorlibrary.pri) + +DEPENDPATH += . +INCLUDEPATH += $$PWD $$PWD/../build $$PWD/../build/botan + +win32 { + win32-msvc* { + QMAKE_CXXFLAGS += -wd4251 -wd4290 -wd4250 + DEFINES += BOTAN_DLL=__declspec(dllexport) + } else { + QMAKE_CFLAGS += -fpermissive -finline-functions -Wno-long-long + QMAKE_CXXFLAGS += -fpermissive -finline-functions -Wno-long-long + } + LIBS += -ladvapi32 -luser32 +} + +unix { + QMAKE_CFLAGS += -fPIC -ansi -fpermissive -finline-functions -Wno-long-long + QMAKE_CXXFLAGS += -fPIC -ansi -fpermissive -finline-functions -Wno-long-long + QMAKE_CXXFLAGS_HIDESYMS -= -fvisibility-inlines-hidden # for ubuntu 7.04 +} + +# Input +HEADERS += algo_factory/algo_cache.h \ + algo_factory/algo_factory.h \ + alloc/allocate.h \ + alloc/mem_pool/mem_pool.h \ + alloc/secmem.h \ + alloc/system_alloc/defalloc.h \ + asn1/alg_id.h \ + asn1/asn1_int.h \ + asn1/asn1_obj.h \ + asn1/asn1_oid.h \ + asn1/ber_dec.h \ + asn1/der_enc.h \ + benchmark/benchmark.h \ + block/aes/aes.h \ + block/block_cipher.h \ + block/blowfish/blowfish.h \ + block/cast/cast128.h \ + block/cast/cast256.h \ + block/des/des.h \ + block/des/desx.h \ + block/gost_28147/gost_28147.h \ + block/idea/idea.h \ + block/kasumi/kasumi.h \ + block/lion/lion.h \ + block/lubyrack/lubyrack.h \ + block/mars/mars.h \ + block/misty1/misty1.h \ + block/noekeon/noekeon.h \ + block/rc2/rc2.h \ + block/rc5/rc5.h \ + block/rc6/rc6.h \ + block/safer/safer_sk.h \ + block/seed/seed.h \ + block/serpent/serpent.h \ + block/skipjack/skipjack.h \ + block/square/square.h \ + block/tea/tea.h \ + block/twofish/twofish.h \ + block/xtea/xtea.h \ + cert/x509/certstor.h \ + cert/x509/crl_ent.h \ + cert/x509/pkcs10.h \ + cert/x509/x509_ca.h \ + cert/x509/x509_crl.h \ + cert/x509/x509_ext.h \ + cert/x509/x509_obj.h \ + cert/x509/x509cert.h \ + cert/x509/x509find.h \ + cert/x509/x509self.h \ + cert/x509/x509stor.h \ + checksum/adler32/adler32.h \ + checksum/crc24/crc24.h \ + checksum/crc32/crc32.h \ + cms/cms_dec.h \ + cms/cms_enc.h \ + codec/base64/base64.h \ + codec/hex/hex.h \ + codec/openpgp/openpgp.h \ + codec/pem/pem.h \ + cryptobox/cryptobox.h \ + engine/def_engine/def_eng.h \ + engine/engine.h \ + entropy/entropy_src.h \ + filters/basefilt.h \ + filters/buf_filt.h \ + filters/data_snk.h \ + filters/data_src.h \ + filters/filter.h \ + filters/filters.h \ + filters/out_buf.h \ + filters/pbe.h \ + filters/pipe.h \ + filters/secqueue.h \ + hash/fork256/fork256.h \ + hash/gost_3411/gost_3411.h \ + hash/has160/has160.h \ + hash/hash.h \ + hash/md2/md2.h \ + hash/md4/md4.h \ + hash/md5/md5.h \ + hash/mdx_hash/mdx_hash.h \ + hash/par_hash/par_hash.h \ + hash/rmd128/rmd128.h \ + hash/rmd160/rmd160.h \ + hash/sha1/sha160.h \ + hash/sha2/sha2_32.h \ + hash/sha2/sha2_64.h \ + hash/skein/skein_512.h \ + hash/tiger/tiger.h \ + hash/whirlpool/whrlpool.h \ + kdf/kdf.h \ + kdf/kdf1/kdf1.h \ + kdf/kdf2/kdf2.h \ + kdf/mgf1/mgf1.h \ + kdf/ssl_prf/prf_ssl3.h \ + kdf/tls_prf/prf_tls.h \ + kdf/x942_prf/prf_x942.h \ + libstate/botan.h \ + libstate/init.h \ + libstate/libstate.h \ + libstate/look_pk.h \ + libstate/lookup.h \ + libstate/oid_lookup/oids.h \ + libstate/pk_engine.h \ + libstate/scan_name.h \ + mac/cbc_mac/cbc_mac.h \ + mac/cmac/cmac.h \ + mac/hmac/hmac.h \ + mac/mac.h \ + mac/ssl3mac/ssl3_mac.h \ + mac/x919_mac/x919_mac.h \ + math/bigint/bigint.h \ + math/bigint/divide.h \ + math/bigint/mp_core.h \ + math/bigint/mp_generic/mp_asm.h \ + math/bigint/mp_generic/mp_asmi.h \ + math/bigint/mp_types.h \ + math/numbertheory/blinding.h \ + math/numbertheory/def_powm.h \ + math/numbertheory/numthry.h \ + math/numbertheory/pow_mod.h \ + math/numbertheory/reducer.h \ + modes/cbc/cbc.h \ + modes/cfb/cfb.h \ + modes/ctr/ctr.h \ + modes/cts/cts.h \ + modes/eax/eax.h \ + modes/ecb/ecb.h \ + modes/mode_pad/mode_pad.h \ + modes/modebase.h \ + modes/ofb/ofb.h \ + modes/xts/xts.h \ + mutex/mutex.h \ + mutex/noop_mutex/mux_noop.h \ + pbe/get_pbe.h \ + pbe/pbes1/pbes1.h \ + pbe/pbes2/pbes2.h \ + pk_pad/eme.h \ + pk_pad/eme1/eme1.h \ + pk_pad/eme_pkcs/eme_pkcs.h \ + pk_pad/emsa.h \ + pk_pad/emsa1/emsa1.h \ + pk_pad/emsa1_bsi/emsa1_bsi.h \ + pk_pad/emsa2/emsa2.h \ + pk_pad/emsa3/emsa3.h \ + pk_pad/emsa4/emsa4.h \ + pk_pad/emsa_raw/emsa_raw.h \ + pk_pad/hash_id/hash_id.h \ + pubkey/dh/dh.h \ + pubkey/dh/dh_core.h \ + pubkey/dh/dh_op.h \ + pubkey/dl_algo/dl_algo.h \ + pubkey/dl_group/dl_group.h \ + pubkey/dlies/dlies.h \ + pubkey/dsa/dsa.h \ + pubkey/dsa/dsa_core.h \ + pubkey/dsa/dsa_op.h \ + pubkey/elgamal/elg_core.h \ + pubkey/elgamal/elg_op.h \ + pubkey/elgamal/elgamal.h \ + pubkey/if_algo/if_algo.h \ + pubkey/if_algo/if_core.h \ + pubkey/if_algo/if_op.h \ + pubkey/keypair/keypair.h \ + pubkey/nr/nr.h \ + pubkey/nr/nr_core.h \ + pubkey/nr/nr_op.h \ + pubkey/pk_algs.h \ + pubkey/pk_codecs/pkcs8.h \ + pubkey/pk_codecs/x509_key.h \ + pubkey/pk_filts.h \ + pubkey/pk_keys.h \ + pubkey/pubkey.h \ + pubkey/pubkey_enums.h \ + pubkey/rsa/rsa.h \ + pubkey/rw/rw.h \ + rng/auto_rng/auto_rng.h \ + rng/hmac_rng/hmac_rng.h \ + rng/randpool/randpool.h \ + rng/rng.h \ + rng/x931_rng/x931_rng.h \ + s2k/pbkdf1/pbkdf1.h \ + s2k/pbkdf2/pbkdf2.h \ + s2k/pgps2k/pgp_s2k.h \ + s2k/s2k.h \ + selftest/selftest.h \ + stream/arc4/arc4.h \ + stream/salsa20/salsa20.h \ + stream/stream_cipher.h \ + stream/turing/turing.h \ + stream/wid_wake/wid_wake.h \ + sym_algo/sym_algo.h \ + sym_algo/symkey.h \ + timer/timer.h \ + utils/bit_ops.h \ + utils/bswap.h \ + utils/buf_comp/buf_comp.h \ + utils/charset.h \ + utils/datastor/datastor.h \ + utils/exceptn.h \ + utils/loadstor.h \ + utils/mem_ops.h \ + utils/parsing.h \ + utils/rotate.h \ + utils/stl_util.h \ + utils/types.h \ + utils/ui.h \ + utils/util.h \ + utils/version.h \ + utils/xor_buf.h + +win32 { + HEADERS += entropy/cryptoapi_rng/es_capi.h \ + entropy/win32_stats/es_win32.h \ + mutex/win32_crit_section/mux_win32.h \ + timer/win32_query_perf_ctr/tm_win32.h +} + +unix { + HEADERS += alloc/alloc_mmap/mmap_mem.h \ + cert/cvc/cvc_ado.h \ + cert/cvc/cvc_ca.h \ + cert/cvc/cvc_cert.h \ + cert/cvc/cvc_gen_cert.h \ + cert/cvc/cvc_key.h \ + cert/cvc/cvc_req.h \ + cert/cvc/cvc_self.h \ + cert/cvc/eac_asn_obj.h \ + cert/cvc/eac_obj.h \ + cert/cvc/ecdsa_sig.h \ + cert/cvc/freestore.h \ + cert/cvc/signed_obj.h \ + entropy/dev_random/es_dev.h \ + entropy/egd/es_egd.h \ + entropy/proc_walk/es_ftw.h \ + entropy/unix_procs/es_unix.h \ + entropy/unix_procs/unix_cmd.h \ + filters/fd_unix/fd_unix.h \ + math/gfpmath/curve_gfp.h \ + math/gfpmath/gfp_element.h \ + math/gfpmath/gfp_modulus.h \ + math/gfpmath/point_gfp.h \ + mutex/pthreads/mux_pthr.h \ + pubkey/ec_dompar/ec_dompar.h \ + pubkey/ecc_key/ecc_key.h \ + pubkey/ecdsa/ecdsa.h \ + pubkey/ecdsa/ecdsa_core.h \ + pubkey/ecdsa/ecdsa_op.h \ + pubkey/eckaeg/eckaeg.h \ + pubkey/eckaeg/eckaeg_core.h \ + pubkey/eckaeg/eckaeg_op.h \ + timer/gettimeofday/tm_unix.h +} + +linux*-g++* { + HEADERS += timer/posix_rt/tm_posix.h +} + +SOURCES += algo_factory/algo_factory.cpp \ + algo_factory/prov_weight.cpp \ + alloc/mem_pool/mem_pool.cpp \ + alloc/system_alloc/defalloc.cpp \ + asn1/alg_id.cpp \ + asn1/asn1_alt.cpp \ + asn1/asn1_att.cpp \ + asn1/asn1_dn.cpp \ + asn1/asn1_int.cpp \ + asn1/asn1_oid.cpp \ + asn1/asn1_str.cpp \ + asn1/asn1_tm.cpp \ + asn1/ber_dec.cpp \ + asn1/der_enc.cpp \ + benchmark/benchmark.cpp \ + block/aes/aes.cpp \ + block/aes/aes_tab.cpp \ + block/blowfish/blfs_tab.cpp \ + block/blowfish/blowfish.cpp \ + block/cast/cast128.cpp \ + block/cast/cast256.cpp \ + block/cast/cast_tab.cpp \ + block/des/des.cpp \ + block/des/des_tab.cpp \ + block/des/desx.cpp \ + block/gost_28147/gost_28147.cpp \ + block/idea/idea.cpp \ + block/kasumi/kasumi.cpp \ + block/lion/lion.cpp \ + block/lubyrack/lubyrack.cpp \ + block/mars/mars.cpp \ + block/mars/mars_tab.cpp \ + block/misty1/misty1.cpp \ + block/noekeon/noekeon.cpp \ + block/rc2/rc2.cpp \ + block/rc5/rc5.cpp \ + block/rc6/rc6.cpp \ + block/safer/safe_tab.cpp \ + block/safer/safer_sk.cpp \ + block/seed/seed.cpp \ + block/seed/seed_tab.cpp \ + block/serpent/serpent.cpp \ + block/skipjack/skipjack.cpp \ + block/square/sqr_tab.cpp \ + block/square/square.cpp \ + block/tea/tea.cpp \ + block/twofish/two_tab.cpp \ + block/twofish/twofish.cpp \ + block/xtea/xtea.cpp \ + cert/x509/certstor.cpp \ + cert/x509/crl_ent.cpp \ + cert/x509/pkcs10.cpp \ + cert/x509/x509_ca.cpp \ + cert/x509/x509_crl.cpp \ + cert/x509/x509_ext.cpp \ + cert/x509/x509_obj.cpp \ + cert/x509/x509cert.cpp \ + cert/x509/x509find.cpp \ + cert/x509/x509opt.cpp \ + cert/x509/x509self.cpp \ + cert/x509/x509stor.cpp \ + checksum/adler32/adler32.cpp \ + checksum/crc24/crc24.cpp \ + checksum/crc32/crc32.cpp \ + cms/cms_algo.cpp \ + cms/cms_comp.cpp \ + cms/cms_dalg.cpp \ + cms/cms_dec.cpp \ + cms/cms_ealg.cpp \ + cms/cms_enc.cpp \ + codec/base64/b64_char.cpp \ + codec/base64/base64.cpp \ + codec/hex/hex.cpp \ + codec/hex/hex_char.cpp \ + codec/openpgp/openpgp.cpp \ + codec/pem/pem.cpp \ + cryptobox/cryptobox.cpp \ + engine/def_engine/def_mode.cpp \ + engine/def_engine/def_pk_ops.cpp \ + engine/def_engine/def_powm.cpp \ + engine/def_engine/lookup_block.cpp \ + engine/def_engine/lookup_hash.cpp \ + engine/def_engine/lookup_mac.cpp \ + engine/def_engine/lookup_stream.cpp \ + filters/algo_filt.cpp \ + filters/basefilt.cpp \ + filters/buf_filt.cpp \ + filters/data_snk.cpp \ + filters/data_src.cpp \ + filters/filter.cpp \ + filters/out_buf.cpp \ + filters/pipe.cpp \ + filters/pipe_io.cpp \ + filters/pipe_rw.cpp \ + filters/secqueue.cpp \ + hash/fork256/fork256.cpp \ + hash/gost_3411/gost_3411.cpp \ + hash/has160/has160.cpp \ + hash/md2/md2.cpp \ + hash/md4/md4.cpp \ + hash/md5/md5.cpp \ + hash/mdx_hash/mdx_hash.cpp \ + hash/par_hash/par_hash.cpp \ + hash/rmd128/rmd128.cpp \ + hash/rmd160/rmd160.cpp \ + hash/sha1/sha160.cpp \ + hash/sha2/sha2_32.cpp \ + hash/sha2/sha2_64.cpp \ + hash/skein/skein_512.cpp \ + hash/tiger/tig_tab.cpp \ + hash/tiger/tiger.cpp \ + hash/whirlpool/whrl_tab.cpp \ + hash/whirlpool/whrlpool.cpp \ + kdf/kdf.cpp \ + kdf/kdf1/kdf1.cpp \ + kdf/kdf2/kdf2.cpp \ + kdf/mgf1/mgf1.cpp \ + kdf/ssl_prf/prf_ssl3.cpp \ + kdf/tls_prf/prf_tls.cpp \ + kdf/x942_prf/prf_x942.cpp \ + libstate/get_enc.cpp \ + libstate/init.cpp \ + libstate/libstate.cpp \ + libstate/look_pk.cpp \ + libstate/lookup.cpp \ + libstate/oid_lookup/oids.cpp \ + libstate/pk_engine.cpp \ + libstate/policy.cpp \ + libstate/scan_name.cpp \ + mac/cbc_mac/cbc_mac.cpp \ + mac/cmac/cmac.cpp \ + mac/hmac/hmac.cpp \ + mac/mac.cpp \ + mac/ssl3mac/ssl3_mac.cpp \ + mac/x919_mac/x919_mac.cpp \ + math/bigint/big_code.cpp \ + math/bigint/big_io.cpp \ + math/bigint/big_ops2.cpp \ + math/bigint/big_ops3.cpp \ + math/bigint/big_rand.cpp \ + math/bigint/bigint.cpp \ + math/bigint/divide.cpp \ + math/bigint/monty_generic/mp_monty.cpp \ + math/bigint/mp_asm.cpp \ + math/bigint/mp_comba.cpp \ + math/bigint/mp_karat.cpp \ + math/bigint/mp_misc.cpp \ + math/bigint/mp_shift.cpp \ + math/bigint/mulop_generic/mp_mulop.cpp \ + math/numbertheory/blinding.cpp \ + math/numbertheory/dsa_gen.cpp \ + math/numbertheory/jacobi.cpp \ + math/numbertheory/make_prm.cpp \ + math/numbertheory/mp_numth.cpp \ + math/numbertheory/numthry.cpp \ + math/numbertheory/pow_mod.cpp \ + math/numbertheory/powm_fw.cpp \ + math/numbertheory/powm_mnt.cpp \ + math/numbertheory/primes.cpp \ + math/numbertheory/reducer.cpp \ + math/numbertheory/ressol.cpp \ + modes/cbc/cbc.cpp \ + modes/cfb/cfb.cpp \ + modes/ctr/ctr.cpp \ + modes/cts/cts.cpp \ + modes/eax/eax.cpp \ + modes/eax/eax_dec.cpp \ + modes/ecb/ecb.cpp \ + modes/mode_pad/mode_pad.cpp \ + modes/modebase.cpp \ + modes/ofb/ofb.cpp \ + modes/xts/xts.cpp \ + mutex/noop_mutex/mux_noop.cpp \ + pbe/get_pbe.cpp \ + pbe/pbes1/pbes1.cpp \ + pbe/pbes2/pbes2.cpp \ + pk_pad/eme.cpp \ + pk_pad/eme1/eme1.cpp \ + pk_pad/eme_pkcs/eme_pkcs.cpp \ + pk_pad/emsa1/emsa1.cpp \ + pk_pad/emsa1_bsi/emsa1_bsi.cpp \ + pk_pad/emsa2/emsa2.cpp \ + pk_pad/emsa3/emsa3.cpp \ + pk_pad/emsa4/emsa4.cpp \ + pk_pad/emsa_raw/emsa_raw.cpp \ + pk_pad/hash_id/hash_id.cpp \ + pubkey/dh/dh.cpp \ + pubkey/dh/dh_core.cpp \ + pubkey/dl_algo/dl_algo.cpp \ + pubkey/dl_group/dl_group.cpp \ + pubkey/dlies/dlies.cpp \ + pubkey/dsa/dsa.cpp \ + pubkey/dsa/dsa_core.cpp \ + pubkey/dsa/dsa_op.cpp \ + pubkey/elgamal/elg_core.cpp \ + pubkey/elgamal/elg_op.cpp \ + pubkey/elgamal/elgamal.cpp \ + pubkey/if_algo/if_algo.cpp \ + pubkey/if_algo/if_core.cpp \ + pubkey/if_algo/if_op.cpp \ + pubkey/keypair/keypair.cpp \ + pubkey/nr/nr.cpp \ + pubkey/nr/nr_core.cpp \ + pubkey/nr/nr_op.cpp \ + pubkey/pk_algs.cpp \ + pubkey/pk_codecs/pkcs8.cpp \ + pubkey/pk_codecs/x509_key.cpp \ + pubkey/pk_filts.cpp \ + pubkey/pk_keys.cpp \ + pubkey/pubkey.cpp \ + pubkey/pubkey_enums.cpp \ + pubkey/rsa/rsa.cpp \ + pubkey/rw/rw.cpp \ + rng/auto_rng/auto_rng.cpp \ + rng/hmac_rng/hmac_rng.cpp \ + rng/randpool/randpool.cpp \ + rng/rng.cpp \ + rng/x931_rng/x931_rng.cpp \ + s2k/pbkdf1/pbkdf1.cpp \ + s2k/pbkdf2/pbkdf2.cpp \ + s2k/pgps2k/pgp_s2k.cpp \ + s2k/s2k.cpp \ + selftest/selftest.cpp \ + stream/arc4/arc4.cpp \ + stream/salsa20/salsa20.cpp \ + stream/stream_cipher.cpp \ + stream/turing/tur_tab.cpp \ + stream/turing/turing.cpp \ + stream/wid_wake/wid_wake.cpp \ + sym_algo/symkey.cpp \ + timer/timer.cpp \ + utils/charset.cpp \ + utils/datastor/datastor.cpp \ + utils/exceptn.cpp \ + utils/mlock.cpp \ + utils/parsing.cpp \ + utils/ui.cpp \ + utils/util.cpp \ + utils/version.cpp + +win32 { +SOURCES += entropy/cryptoapi_rng/es_capi.cpp \ + entropy/win32_stats/es_win32.cpp \ + mutex/win32_crit_section/mux_win32.cpp \ + timer/win32_query_perf_ctr/tm_win32.cpp +} + +unix { + SOURCES += alloc/alloc_mmap/mmap_mem.cpp \ + cert/cvc/asn1_eac_str.cpp \ + cert/cvc/asn1_eac_tm.cpp \ + cert/cvc/cvc_ado.cpp \ + cert/cvc/cvc_ca.cpp \ + cert/cvc/cvc_cert.cpp \ + cert/cvc/cvc_req.cpp \ + cert/cvc/cvc_self.cpp \ + cert/cvc/ecdsa_sig.cpp \ + cert/cvc/signed_obj.cpp \ + entropy/dev_random/es_dev.cpp \ + entropy/egd/es_egd.cpp \ + entropy/proc_walk/es_ftw.cpp \ + entropy/unix_procs/es_unix.cpp \ + entropy/unix_procs/unix_cmd.cpp \ + entropy/unix_procs/unix_src.cpp \ + filters/fd_unix/fd_unix.cpp \ + math/gfpmath/curve_gfp.cpp \ + math/gfpmath/gfp_element.cpp \ + math/gfpmath/point_gfp.cpp \ + mutex/pthreads/mux_pthr.cpp \ + pubkey/ec_dompar/ec_dompar.cpp \ + pubkey/ecc_key/ecc_key.cpp \ + pubkey/ecdsa/ecdsa.cpp \ + pubkey/ecdsa/ecdsa_core.cpp \ + pubkey/ecdsa/ecdsa_op.cpp \ + pubkey/eckaeg/eckaeg.cpp \ + pubkey/eckaeg/eckaeg_core.cpp \ + pubkey/eckaeg/eckaeg_op.cpp \ + timer/gettimeofday/tm_unix.cpp +} + +linux*-g++* { + SOURCES += timer/posix_rt/tm_posix.cpp +} + +linux*-g++* { + LIBS += -lrt +} diff --git a/botan/src/stream/arc4/arc4.cpp b/botan/src/stream/arc4/arc4.cpp new file mode 100644 index 0000000..0f78f73 --- /dev/null +++ b/botan/src/stream/arc4/arc4.cpp @@ -0,0 +1,105 @@ +/* +* ARC4 +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/arc4.h> +#include <botan/xor_buf.h> +#include <botan/parsing.h> + +namespace Botan { + +/* +* Combine cipher stream with message +*/ +void ARC4::cipher(const byte in[], byte out[], u32bit length) + { + while(length >= buffer.size() - position) + { + xor_buf(out, in, buffer.begin() + position, buffer.size() - position); + length -= (buffer.size() - position); + in += (buffer.size() - position); + out += (buffer.size() - position); + generate(); + } + xor_buf(out, in, buffer.begin() + position, length); + position += length; + } + +/* +* Generate cipher stream +*/ +void ARC4::generate() + { + u32bit SX, SY; + for(u32bit j = 0; j != buffer.size(); j += 4) + { + SX = state[X+1]; Y = (Y + SX) % 256; SY = state[Y]; + state[X+1] = SY; state[Y] = SX; + buffer[j] = state[(SX + SY) % 256]; + + SX = state[X+2]; Y = (Y + SX) % 256; SY = state[Y]; + state[X+2] = SY; state[Y] = SX; + buffer[j+1] = state[(SX + SY) % 256]; + + SX = state[X+3]; Y = (Y + SX) % 256; SY = state[Y]; + state[X+3] = SY; state[Y] = SX; + buffer[j+2] = state[(SX + SY) % 256]; + + X = (X + 4) % 256; + SX = state[X]; Y = (Y + SX) % 256; SY = state[Y]; + state[X] = SY; state[Y] = SX; + buffer[j+3] = state[(SX + SY) % 256]; + } + position = 0; + } + +/* +* ARC4 Key Schedule +*/ +void ARC4::key_schedule(const byte key[], u32bit length) + { + clear(); + for(u32bit j = 0; j != 256; ++j) + state[j] = j; + for(u32bit j = 0, state_index = 0; j != 256; ++j) + { + state_index = (state_index + key[j % length] + state[j]) % 256; + std::swap(state[j], state[state_index]); + } + for(u32bit j = 0; j <= SKIP; j += buffer.size()) + generate(); + position += (SKIP % buffer.size()); + } + +/* +* Return the name of this type +*/ +std::string ARC4::name() const + { + if(SKIP == 0) return "ARC4"; + if(SKIP == 256) return "MARK-4"; + else return "RC4_skip(" + to_string(SKIP) + ")"; + } + +/* +* Clear memory of sensitive data +*/ +void ARC4::clear() throw() + { + state.clear(); + buffer.clear(); + position = X = Y = 0; + } + +/* +* ARC4 Constructor +*/ +ARC4::ARC4(u32bit s) : StreamCipher(1, 256), SKIP(s) + { + clear(); + } + +} diff --git a/botan/src/stream/arc4/arc4.h b/botan/src/stream/arc4/arc4.h new file mode 100644 index 0000000..aa2cea7 --- /dev/null +++ b/botan/src/stream/arc4/arc4.h @@ -0,0 +1,41 @@ +/* +* ARC4 +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ARC4_H__ +#define BOTAN_ARC4_H__ + +#include <botan/stream_cipher.h> +#include <botan/types.h> + +namespace Botan { + +/* +* ARC4 +*/ +class BOTAN_DLL ARC4 : public StreamCipher + { + public: + void clear() throw(); + std::string name() const; + StreamCipher* clone() const { return new ARC4(SKIP); } + ARC4(u32bit = 0); + ~ARC4() { clear(); } + private: + void cipher(const byte[], byte[], u32bit); + void key_schedule(const byte[], u32bit); + void generate(); + + const u32bit SKIP; + + SecureBuffer<byte, DEFAULT_BUFFERSIZE> buffer; + SecureBuffer<u32bit, 256> state; + u32bit X, Y, position; + }; + +} + +#endif diff --git a/botan/src/stream/arc4/info.txt b/botan/src/stream/arc4/info.txt new file mode 100644 index 0000000..e4689cf --- /dev/null +++ b/botan/src/stream/arc4/info.txt @@ -0,0 +1,14 @@ +realname "ARC4" + +define ARC4 + +load_on auto + +<requires> +stream +</requires> + +<add> +arc4.cpp +arc4.h +</add> diff --git a/botan/src/stream/info.txt b/botan/src/stream/info.txt new file mode 100644 index 0000000..295c737 --- /dev/null +++ b/botan/src/stream/info.txt @@ -0,0 +1,14 @@ +realname "Stream Ciphers" + +load_on auto + +define STREAM_CIPHER + +<add> +stream_cipher.h +stream_cipher.cpp +</add> + +<requires> +sym_algo +</requires> diff --git a/botan/src/stream/salsa20/info.txt b/botan/src/stream/salsa20/info.txt new file mode 100644 index 0000000..db93830 --- /dev/null +++ b/botan/src/stream/salsa20/info.txt @@ -0,0 +1,14 @@ +realname "Salsa20" + +define SALSA20 + +load_on auto + +<requires> +stream +</requires> + +<add> +salsa20.cpp +salsa20.h +</add> diff --git a/botan/src/stream/salsa20/salsa20.cpp b/botan/src/stream/salsa20/salsa20.cpp new file mode 100644 index 0000000..7513779 --- /dev/null +++ b/botan/src/stream/salsa20/salsa20.cpp @@ -0,0 +1,217 @@ +/* +* Salsa20 +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/salsa20.h> +#include <botan/mem_ops.h> +#include <botan/xor_buf.h> +#include <botan/loadstor.h> +#include <botan/parsing.h> + +namespace Botan { + +namespace { + +/* +* Generate Salsa20 cipher stream +*/ +void salsa20(byte output[64], const u32bit input[16]) + { + u32bit x00 = input[0]; + u32bit x01 = input[1]; + u32bit x02 = input[2]; + u32bit x03 = input[3]; + u32bit x04 = input[4]; + u32bit x05 = input[5]; + u32bit x06 = input[6]; + u32bit x07 = input[7]; + u32bit x08 = input[8]; + u32bit x09 = input[9]; + u32bit x10 = input[10]; + u32bit x11 = input[11]; + u32bit x12 = input[12]; + u32bit x13 = input[13]; + u32bit x14 = input[14]; + u32bit x15 = input[15]; + + for(u32bit i = 0; i != 10; ++i) + { + x04 ^= rotate_left(x00 + x12, 7); + x08 ^= rotate_left(x04 + x00, 9); + x12 ^= rotate_left(x08 + x04, 13); + x00 ^= rotate_left(x12 + x08, 18); + x09 ^= rotate_left(x05 + x01, 7); + x13 ^= rotate_left(x09 + x05, 9); + x01 ^= rotate_left(x13 + x09, 13); + x05 ^= rotate_left(x01 + x13, 18); + x14 ^= rotate_left(x10 + x06, 7); + x02 ^= rotate_left(x14 + x10, 9); + x06 ^= rotate_left(x02 + x14, 13); + x10 ^= rotate_left(x06 + x02, 18); + x03 ^= rotate_left(x15 + x11, 7); + x07 ^= rotate_left(x03 + x15, 9); + x11 ^= rotate_left(x07 + x03, 13); + x15 ^= rotate_left(x11 + x07, 18); + + x01 ^= rotate_left(x00 + x03, 7); + x02 ^= rotate_left(x01 + x00, 9); + x03 ^= rotate_left(x02 + x01, 13); + x00 ^= rotate_left(x03 + x02, 18); + x06 ^= rotate_left(x05 + x04, 7); + x07 ^= rotate_left(x06 + x05, 9); + x04 ^= rotate_left(x07 + x06, 13); + x05 ^= rotate_left(x04 + x07, 18); + x11 ^= rotate_left(x10 + x09, 7); + x08 ^= rotate_left(x11 + x10, 9); + x09 ^= rotate_left(x08 + x11, 13); + x10 ^= rotate_left(x09 + x08, 18); + x12 ^= rotate_left(x15 + x14, 7); + x13 ^= rotate_left(x12 + x15, 9); + x14 ^= rotate_left(x13 + x12, 13); + x15 ^= rotate_left(x14 + x13, 18); + } + + store_le(x00 + input[ 0], output + 4 * 0); + store_le(x01 + input[ 1], output + 4 * 1); + store_le(x02 + input[ 2], output + 4 * 2); + store_le(x03 + input[ 3], output + 4 * 3); + store_le(x04 + input[ 4], output + 4 * 4); + store_le(x05 + input[ 5], output + 4 * 5); + store_le(x06 + input[ 6], output + 4 * 6); + store_le(x07 + input[ 7], output + 4 * 7); + store_le(x08 + input[ 8], output + 4 * 8); + store_le(x09 + input[ 9], output + 4 * 9); + store_le(x10 + input[10], output + 4 * 10); + store_le(x11 + input[11], output + 4 * 11); + store_le(x12 + input[12], output + 4 * 12); + store_le(x13 + input[13], output + 4 * 13); + store_le(x14 + input[14], output + 4 * 14); + store_le(x15 + input[15], output + 4 * 15); + } + +} + +/* +* Combine cipher stream with message +*/ +void Salsa20::cipher(const byte in[], byte out[], u32bit length) + { + while(length >= buffer.size() - position) + { + xor_buf(out, in, buffer.begin() + position, buffer.size() - position); + length -= (buffer.size() - position); + in += (buffer.size() - position); + out += (buffer.size() - position); + salsa20(buffer.begin(), state); + + ++state[8]; + if(!state[8]) // if overflow in state[8] + ++state[9]; // carry to state[9] + + position = 0; + } + + xor_buf(out, in, buffer.begin() + position, length); + + position += length; + } + +/* +* Salsa20 Key Schedule +*/ +void Salsa20::key_schedule(const byte key[], u32bit length) + { + static const u32bit TAU[] = + { 0x61707865, 0x3120646e, 0x79622d36, 0x6b206574 }; + + static const u32bit SIGMA[] = + { 0x61707865, 0x3320646e, 0x79622d32, 0x6b206574 }; + + clear(); + + if(length == 16) + { + state[0] = TAU[0]; + state[1] = load_le<u32bit>(key, 0); + state[2] = load_le<u32bit>(key, 1); + state[3] = load_le<u32bit>(key, 2); + state[4] = load_le<u32bit>(key, 3); + state[5] = TAU[1]; + state[10] = TAU[2]; + state[11] = load_le<u32bit>(key, 0); + state[12] = load_le<u32bit>(key, 1); + state[13] = load_le<u32bit>(key, 2); + state[14] = load_le<u32bit>(key, 3); + state[15] = TAU[3]; + } + else if(length == 32) + { + state[0] = SIGMA[0]; + state[1] = load_le<u32bit>(key, 0); + state[2] = load_le<u32bit>(key, 1); + state[3] = load_le<u32bit>(key, 2); + state[4] = load_le<u32bit>(key, 3); + state[5] = SIGMA[1]; + state[10] = SIGMA[2]; + state[11] = load_le<u32bit>(key, 4); + state[12] = load_le<u32bit>(key, 5); + state[13] = load_le<u32bit>(key, 6); + state[14] = load_le<u32bit>(key, 7); + state[15] = SIGMA[3]; + } + + const byte ZERO[8] = { 0 }; + resync(ZERO, sizeof(ZERO)); + } + +/* +* Return the name of this type +*/ +void Salsa20::resync(const byte iv[], u32bit length) + { + if(length != IV_LENGTH) + throw Invalid_IV_Length(name(), length); + + state[6] = load_le<u32bit>(iv, 0); + state[7] = load_le<u32bit>(iv, 1); + state[8] = 0; + state[9] = 0; + + salsa20(buffer.begin(), state); + ++state[8]; + if(!state[8]) // if overflow in state[8] + ++state[9]; // carry to state[9] + + position = 0; + } + +/* +* Return the name of this type +*/ +std::string Salsa20::name() const + { + return "Salsa20"; + } + +/* +* Clear memory of sensitive data +*/ +void Salsa20::clear() throw() + { + state.clear(); + buffer.clear(); + position = 0; + } + +/* +* Salsa20 Constructor +*/ +Salsa20::Salsa20() : StreamCipher(16, 32, 16, 8) + { + clear(); + } + +} diff --git a/botan/src/stream/salsa20/salsa20.h b/botan/src/stream/salsa20/salsa20.h new file mode 100644 index 0000000..3dbfddb --- /dev/null +++ b/botan/src/stream/salsa20/salsa20.h @@ -0,0 +1,41 @@ +/* +* Salsa20 +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_SALSA20_H__ +#define BOTAN_SALSA20_H__ + +#include <botan/stream_cipher.h> + +namespace Botan { + +/* +* Salsa20 +*/ +class BOTAN_DLL Salsa20 : public StreamCipher + { + public: + void clear() throw(); + std::string name() const; + StreamCipher* clone() const { return new Salsa20; } + + void resync(const byte[], u32bit); + + Salsa20(); + ~Salsa20() { clear(); } + private: + void cipher(const byte[], byte[], u32bit); + void key_schedule(const byte[], u32bit); + + SecureBuffer<u32bit, 16> state; + + SecureBuffer<byte, 64> buffer; + u32bit position; + }; + +} + +#endif diff --git a/botan/src/stream/stream_cipher.cpp b/botan/src/stream/stream_cipher.cpp new file mode 100644 index 0000000..68bb5d4 --- /dev/null +++ b/botan/src/stream/stream_cipher.cpp @@ -0,0 +1,30 @@ +/** +* Stream Cipher Default Implementation for IV and Seek +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/stream_cipher.h> + +namespace Botan { + +/* +* Default StreamCipher Resync Operation +*/ +void StreamCipher::resync(const byte[], u32bit length) + { + if(length) + throw Exception("The stream cipher " + name() + + " does not support resyncronization"); + } + +/* +* Default StreamCipher Seek Operation +*/ +void StreamCipher::seek(u32bit) + { + throw Exception("The stream cipher " + name() + " does not support seek()"); + } + +} diff --git a/botan/src/stream/stream_cipher.h b/botan/src/stream/stream_cipher.h new file mode 100644 index 0000000..8ea3591 --- /dev/null +++ b/botan/src/stream/stream_cipher.h @@ -0,0 +1,92 @@ +/** +* Stream Cipher +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_STREAM_CIPHER_H__ +#define BOTAN_STREAM_CIPHER_H__ + +#include <botan/sym_algo.h> + +namespace Botan { + +/* +* Stream Cipher +*/ +class BOTAN_DLL StreamCipher : public SymmetricAlgorithm + { + public: + const u32bit IV_LENGTH; + + /** + * Encrypt a message. + * @param i the plaintext + * @param o the byte array to hold the output, i.e. the ciphertext + * @param len the length of both i and o + */ + void encrypt(const byte i[], byte o[], u32bit len) { cipher(i, o, len); } + + /** + * Decrypt a message. + * @param i the ciphertext to decrypt + * @param o the byte array to hold the output, i.e. the plaintext + * @param len the length of both i and o + */ + void decrypt(const byte i[], byte o[], u32bit len) { cipher(i, o, len); } + + /** + * Encrypt a message. + * @param in the plaintext as input, after the function has + * returned it will hold the ciphertext + + * @param len the length of in + */ + void encrypt(byte in[], u32bit len) { cipher(in, in, len); } + + /** + * Decrypt a message. + * @param in the ciphertext as input, after the function has + * returned it will hold the plaintext + * @param len the length of in + */ + void decrypt(byte in[], u32bit len) { cipher(in, in, len); } + + /** + * Resync the cipher using the IV + * @param iv the initialization vector + * @param iv_len the length of the IV in bytes + */ + virtual void resync(const byte iv[], u32bit iv_len); + + /** + * Seek ahead in the stream. + * @param len the length to seek ahead. + */ + virtual void seek(u32bit len); + + /** + * Get a new object representing the same algorithm as *this + */ + virtual StreamCipher* clone() const = 0; + + /** + * Zeroize internal state + */ + virtual void clear() throw() = 0; + + StreamCipher(u32bit key_min, u32bit key_max = 0, + u32bit key_mod = 1, + u32bit iv_len = 0) : + SymmetricAlgorithm(key_min, key_max, key_mod), + IV_LENGTH(iv_len) {} + + virtual ~StreamCipher() {} + private: + virtual void cipher(const byte[], byte[], u32bit) = 0; + }; + +} + +#endif diff --git a/botan/src/stream/turing/info.txt b/botan/src/stream/turing/info.txt new file mode 100644 index 0000000..c251a0a --- /dev/null +++ b/botan/src/stream/turing/info.txt @@ -0,0 +1,15 @@ +realname "Turing" + +define TURING + +load_on auto + +<requires> +stream +</requires> + +<add> +tur_tab.cpp +turing.cpp +turing.h +</add> diff --git a/botan/src/stream/turing/tur_tab.cpp b/botan/src/stream/turing/tur_tab.cpp new file mode 100644 index 0000000..a2edd5a --- /dev/null +++ b/botan/src/stream/turing/tur_tab.cpp @@ -0,0 +1,81 @@ +/* +* Tables for Turing +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/turing.h> + +namespace Botan { + +const byte Turing::SBOX[256] = { + 0x61, 0x51, 0xEB, 0x19, 0xB9, 0x5D, 0x60, 0x38, 0x7C, 0xB2, 0x06, 0x12, + 0xC4, 0x5B, 0x16, 0x3B, 0x2B, 0x18, 0x83, 0xB0, 0x7F, 0x75, 0xFA, 0xA0, + 0xE9, 0xDD, 0x6D, 0x7A, 0x6B, 0x68, 0x2D, 0x49, 0xB5, 0x1C, 0x90, 0xF7, + 0xED, 0x9F, 0xE8, 0xCE, 0xAE, 0x77, 0xC2, 0x13, 0xFD, 0xCD, 0x3E, 0xCF, + 0x37, 0x6A, 0xD4, 0xDB, 0x8E, 0x65, 0x1F, 0x1A, 0x87, 0xCB, 0x40, 0x15, + 0x88, 0x0D, 0x35, 0xB3, 0x11, 0x0F, 0xD0, 0x30, 0x48, 0xF9, 0xA8, 0xAC, + 0x85, 0x27, 0x0E, 0x8A, 0xE0, 0x50, 0x64, 0xA7, 0xCC, 0xE4, 0xF1, 0x98, + 0xFF, 0xA1, 0x04, 0xDA, 0xD5, 0xBC, 0x1B, 0xBB, 0xD1, 0xFE, 0x31, 0xCA, + 0xBA, 0xD9, 0x2E, 0xF3, 0x1D, 0x47, 0x4A, 0x3D, 0x71, 0x4C, 0xAB, 0x7D, + 0x8D, 0xC7, 0x59, 0xB8, 0xC1, 0x96, 0x1E, 0xFC, 0x44, 0xC8, 0x7B, 0xDC, + 0x5C, 0x78, 0x2A, 0x9D, 0xA5, 0xF0, 0x73, 0x22, 0x89, 0x05, 0xF4, 0x07, + 0x21, 0x52, 0xA6, 0x28, 0x9A, 0x92, 0x69, 0x8F, 0xC5, 0xC3, 0xF5, 0xE1, + 0xDE, 0xEC, 0x09, 0xF2, 0xD3, 0xAF, 0x34, 0x23, 0xAA, 0xDF, 0x7E, 0x82, + 0x29, 0xC0, 0x24, 0x14, 0x03, 0x32, 0x4E, 0x39, 0x6F, 0xC6, 0xB1, 0x9B, + 0xEA, 0x72, 0x79, 0x41, 0xD8, 0x26, 0x6C, 0x5E, 0x2C, 0xB4, 0xA2, 0x53, + 0x57, 0xE2, 0x9C, 0x86, 0x54, 0x95, 0xB6, 0x80, 0x8C, 0x36, 0x67, 0xBD, + 0x08, 0x93, 0x2F, 0x99, 0x5A, 0xF8, 0x3A, 0xD7, 0x56, 0x84, 0xD2, 0x01, + 0xF6, 0x66, 0x4D, 0x55, 0x8B, 0x0C, 0x0B, 0x46, 0xB7, 0x3C, 0x45, 0x91, + 0xA4, 0xE3, 0x70, 0xD6, 0xFB, 0xE6, 0x10, 0xA9, 0xC9, 0x00, 0x9E, 0xE7, + 0x4F, 0x76, 0x25, 0x3F, 0x5F, 0xA3, 0x33, 0x20, 0x02, 0xEF, 0x62, 0x74, + 0xEE, 0x17, 0x81, 0x42, 0x58, 0x0A, 0x4B, 0x63, 0xE5, 0xBE, 0x6E, 0xAD, + 0xBF, 0x43, 0x94, 0x97 }; + +const u32bit Turing::Q_BOX[256] = { + 0x1FAA1887, 0x4E5E435C, 0x9165C042, 0x250E6EF4, 0x5957EE20, 0xD484FED3, + 0xA666C502, 0x7E54E8AE, 0xD12EE9D9, 0xFC1F38D4, 0x49829B5D, 0x1B5CDF3C, + 0x74864249, 0xDA2E3963, 0x28F4429F, 0xC8432C35, 0x4AF40325, 0x9FC0DD70, + 0xD8973DED, 0x1A02DC5E, 0xCD175B42, 0xF10012BF, 0x6694D78C, 0xACAAB26B, + 0x4EC11B9A, 0x3F168146, 0xC0EA8EC5, 0xB38AC28F, 0x1FED5C0F, 0xAAB4101C, + 0xEA2DB082, 0x470929E1, 0xE71843DE, 0x508299FC, 0xE72FBC4B, 0x2E3915DD, + 0x9FA803FA, 0x9546B2DE, 0x3C233342, 0x0FCEE7C3, 0x24D607EF, 0x8F97EBAB, + 0xF37F859B, 0xCD1F2E2F, 0xC25B71DA, 0x75E2269A, 0x1E39C3D1, 0xEDA56B36, + 0xF8C9DEF2, 0x46C9FC5F, 0x1827B3A3, 0x70A56DDF, 0x0D25B510, 0x000F85A7, + 0xB2E82E71, 0x68CB8816, 0x8F951E2A, 0x72F5F6AF, 0xE4CBC2B3, 0xD34FF55D, + 0x2E6B6214, 0x220B83E3, 0xD39EA6F5, 0x6FE041AF, 0x6B2F1F17, 0xAD3B99EE, + 0x16A65EC0, 0x757016C6, 0xBA7709A4, 0xB0326E01, 0xF4B280D9, 0x4BFB1418, + 0xD6AFF227, 0xFD548203, 0xF56B9D96, 0x6717A8C0, 0x00D5BF6E, 0x10EE7888, + 0xEDFCFE64, 0x1BA193CD, 0x4B0D0184, 0x89AE4930, 0x1C014F36, 0x82A87088, + 0x5EAD6C2A, 0xEF22C678, 0x31204DE7, 0xC9C2E759, 0xD200248E, 0x303B446B, + 0xB00D9FC2, 0x9914A895, 0x906CC3A1, 0x54FEF170, 0x34C19155, 0xE27B8A66, + 0x131B5E69, 0xC3A8623E, 0x27BDFA35, 0x97F068CC, 0xCA3A6ACD, 0x4B55E936, + 0x86602DB9, 0x51DF13C1, 0x390BB16D, 0x5A80B83C, 0x22B23763, 0x39D8A911, + 0x2CB6BC13, 0xBF5579D7, 0x6C5C2FA8, 0xA8F4196E, 0xBCDB5476, 0x6864A866, + 0x416E16AD, 0x897FC515, 0x956FEB3C, 0xF6C8A306, 0x216799D9, 0x171A9133, + 0x6C2466DD, 0x75EB5DCD, 0xDF118F50, 0xE4AFB226, 0x26B9CEF3, 0xADB36189, + 0x8A7A19B1, 0xE2C73084, 0xF77DED5C, 0x8B8BC58F, 0x06DDE421, 0xB41E47FB, + 0xB1CC715E, 0x68C0FF99, 0x5D122F0F, 0xA4D25184, 0x097A5E6C, 0x0CBF18BC, + 0xC2D7C6E0, 0x8BB7E420, 0xA11F523F, 0x35D9B8A2, 0x03DA1A6B, 0x06888C02, + 0x7DD1E354, 0x6BBA7D79, 0x32CC7753, 0xE52D9655, 0xA9829DA1, 0x301590A7, + 0x9BC1C149, 0x13537F1C, 0xD3779B69, 0x2D71F2B7, 0x183C58FA, 0xACDC4418, + 0x8D8C8C76, 0x2620D9F0, 0x71A80D4D, 0x7A74C473, 0x449410E9, 0xA20E4211, + 0xF9C8082B, 0x0A6B334A, 0xB5F68ED2, 0x8243CC1B, 0x453C0FF3, 0x9BE564A0, + 0x4FF55A4F, 0x8740F8E7, 0xCCA7F15F, 0xE300FE21, 0x786D37D6, 0xDFD506F1, + 0x8EE00973, 0x17BBDE36, 0x7A670FA8, 0x5C31AB9E, 0xD4DAB618, 0xCC1F52F5, + 0xE358EB4F, 0x19B9E343, 0x3A8D77DD, 0xCDB93DA6, 0x140FD52D, 0x395412F8, + 0x2BA63360, 0x37E53AD0, 0x80700F1C, 0x7624ED0B, 0x703DC1EC, 0xB7366795, + 0xD6549D15, 0x66CE46D7, 0xD17ABE76, 0xA448E0A0, 0x28F07C02, 0xC31249B7, + 0x6E9ED6BA, 0xEAA47F78, 0xBBCFFFBD, 0xC507CA84, 0xE965F4DA, 0x8E9F35DA, + 0x6AD2AA44, 0x577452AC, 0xB5D674A7, 0x5461A46A, 0x6763152A, 0x9C12B7AA, + 0x12615927, 0x7B4FB118, 0xC351758D, 0x7E81687B, 0x5F52F0B3, 0x2D4254ED, + 0xD4C77271, 0x0431ACAB, 0xBEF94AEC, 0xFEE994CD, 0x9C4D9E81, 0xED623730, + 0xCF8A21E8, 0x51917F0B, 0xA7A9B5D6, 0xB297ADF8, 0xEED30431, 0x68CAC921, + 0xF1B35D46, 0x7A430A36, 0x51194022, 0x9ABCA65E, 0x85EC70BA, 0x39AEA8CC, + 0x737BAE8B, 0x582924D5, 0x03098A5A, 0x92396B81, 0x18DE2522, 0x745C1CB8, + 0xA1B8FE1D, 0x5DB3C697, 0x29164F83, 0x97C16376, 0x8419224C, 0x21203B35, + 0x833AC0FE, 0xD966A19A, 0xAAF0B24F, 0x40FDA998, 0xE7D52D71, 0x390896A8, + 0xCEE6053F, 0xD0B0D300, 0xFF99CBCC, 0x065E3D40 }; + +} diff --git a/botan/src/stream/turing/turing.cpp b/botan/src/stream/turing/turing.cpp new file mode 100644 index 0000000..b988568 --- /dev/null +++ b/botan/src/stream/turing/turing.cpp @@ -0,0 +1,307 @@ +/* +* Turing +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/turing.h> +#include <botan/loadstor.h> +#include <botan/xor_buf.h> + +namespace Botan { + +namespace { + +/* +* Perform an N-way PHT +*/ +inline void PHT(MemoryRegion<u32bit>& buf) + { + u32bit sum = 0; + for(u32bit j = 0; j < buf.size() - 1; ++j) + sum += buf[j]; + buf[buf.size()-1] += sum; + sum = buf[buf.size()-1]; + for(u32bit j = 0; j < buf.size() - 1; ++j) + buf[j] += sum; + } + +} + +/* +* Combine cipher stream with message +*/ +void Turing::cipher(const byte in[], byte out[], u32bit length) + { + while(length >= buffer.size() - position) + { + xor_buf(out, in, buffer.begin() + position, buffer.size() - position); + length -= (buffer.size() - position); + in += (buffer.size() - position); + out += (buffer.size() - position); + generate(); + } + xor_buf(out, in, buffer.begin() + position, length); + position += length; + } + +/* +* Generate cipher stream +*/ +void Turing::generate() + { + // Table for Turing's polynomial multiplication + static const u32bit MULT_TAB[256] = { + 0x00000000, 0xD02B4367, 0xED5686CE, 0x3D7DC5A9, 0x97AC41D1, 0x478702B6, + 0x7AFAC71F, 0xAAD18478, 0x631582EF, 0xB33EC188, 0x8E430421, 0x5E684746, + 0xF4B9C33E, 0x24928059, 0x19EF45F0, 0xC9C40697, 0xC62A4993, 0x16010AF4, + 0x2B7CCF5D, 0xFB578C3A, 0x51860842, 0x81AD4B25, 0xBCD08E8C, 0x6CFBCDEB, + 0xA53FCB7C, 0x7514881B, 0x48694DB2, 0x98420ED5, 0x32938AAD, 0xE2B8C9CA, + 0xDFC50C63, 0x0FEE4F04, 0xC154926B, 0x117FD10C, 0x2C0214A5, 0xFC2957C2, + 0x56F8D3BA, 0x86D390DD, 0xBBAE5574, 0x6B851613, 0xA2411084, 0x726A53E3, + 0x4F17964A, 0x9F3CD52D, 0x35ED5155, 0xE5C61232, 0xD8BBD79B, 0x089094FC, + 0x077EDBF8, 0xD755989F, 0xEA285D36, 0x3A031E51, 0x90D29A29, 0x40F9D94E, + 0x7D841CE7, 0xADAF5F80, 0x646B5917, 0xB4401A70, 0x893DDFD9, 0x59169CBE, + 0xF3C718C6, 0x23EC5BA1, 0x1E919E08, 0xCEBADD6F, 0xCFA869D6, 0x1F832AB1, + 0x22FEEF18, 0xF2D5AC7F, 0x58042807, 0x882F6B60, 0xB552AEC9, 0x6579EDAE, + 0xACBDEB39, 0x7C96A85E, 0x41EB6DF7, 0x91C02E90, 0x3B11AAE8, 0xEB3AE98F, + 0xD6472C26, 0x066C6F41, 0x09822045, 0xD9A96322, 0xE4D4A68B, 0x34FFE5EC, + 0x9E2E6194, 0x4E0522F3, 0x7378E75A, 0xA353A43D, 0x6A97A2AA, 0xBABCE1CD, + 0x87C12464, 0x57EA6703, 0xFD3BE37B, 0x2D10A01C, 0x106D65B5, 0xC04626D2, + 0x0EFCFBBD, 0xDED7B8DA, 0xE3AA7D73, 0x33813E14, 0x9950BA6C, 0x497BF90B, + 0x74063CA2, 0xA42D7FC5, 0x6DE97952, 0xBDC23A35, 0x80BFFF9C, 0x5094BCFB, + 0xFA453883, 0x2A6E7BE4, 0x1713BE4D, 0xC738FD2A, 0xC8D6B22E, 0x18FDF149, + 0x258034E0, 0xF5AB7787, 0x5F7AF3FF, 0x8F51B098, 0xB22C7531, 0x62073656, + 0xABC330C1, 0x7BE873A6, 0x4695B60F, 0x96BEF568, 0x3C6F7110, 0xEC443277, + 0xD139F7DE, 0x0112B4B9, 0xD31DD2E1, 0x03369186, 0x3E4B542F, 0xEE601748, + 0x44B19330, 0x949AD057, 0xA9E715FE, 0x79CC5699, 0xB008500E, 0x60231369, + 0x5D5ED6C0, 0x8D7595A7, 0x27A411DF, 0xF78F52B8, 0xCAF29711, 0x1AD9D476, + 0x15379B72, 0xC51CD815, 0xF8611DBC, 0x284A5EDB, 0x829BDAA3, 0x52B099C4, + 0x6FCD5C6D, 0xBFE61F0A, 0x7622199D, 0xA6095AFA, 0x9B749F53, 0x4B5FDC34, + 0xE18E584C, 0x31A51B2B, 0x0CD8DE82, 0xDCF39DE5, 0x1249408A, 0xC26203ED, + 0xFF1FC644, 0x2F348523, 0x85E5015B, 0x55CE423C, 0x68B38795, 0xB898C4F2, + 0x715CC265, 0xA1778102, 0x9C0A44AB, 0x4C2107CC, 0xE6F083B4, 0x36DBC0D3, + 0x0BA6057A, 0xDB8D461D, 0xD4630919, 0x04484A7E, 0x39358FD7, 0xE91ECCB0, + 0x43CF48C8, 0x93E40BAF, 0xAE99CE06, 0x7EB28D61, 0xB7768BF6, 0x675DC891, + 0x5A200D38, 0x8A0B4E5F, 0x20DACA27, 0xF0F18940, 0xCD8C4CE9, 0x1DA70F8E, + 0x1CB5BB37, 0xCC9EF850, 0xF1E33DF9, 0x21C87E9E, 0x8B19FAE6, 0x5B32B981, + 0x664F7C28, 0xB6643F4F, 0x7FA039D8, 0xAF8B7ABF, 0x92F6BF16, 0x42DDFC71, + 0xE80C7809, 0x38273B6E, 0x055AFEC7, 0xD571BDA0, 0xDA9FF2A4, 0x0AB4B1C3, + 0x37C9746A, 0xE7E2370D, 0x4D33B375, 0x9D18F012, 0xA06535BB, 0x704E76DC, + 0xB98A704B, 0x69A1332C, 0x54DCF685, 0x84F7B5E2, 0x2E26319A, 0xFE0D72FD, + 0xC370B754, 0x135BF433, 0xDDE1295C, 0x0DCA6A3B, 0x30B7AF92, 0xE09CECF5, + 0x4A4D688D, 0x9A662BEA, 0xA71BEE43, 0x7730AD24, 0xBEF4ABB3, 0x6EDFE8D4, + 0x53A22D7D, 0x83896E1A, 0x2958EA62, 0xF973A905, 0xC40E6CAC, 0x14252FCB, + 0x1BCB60CF, 0xCBE023A8, 0xF69DE601, 0x26B6A566, 0x8C67211E, 0x5C4C6279, + 0x6131A7D0, 0xB11AE4B7, 0x78DEE220, 0xA8F5A147, 0x958864EE, 0x45A32789, + 0xEF72A3F1, 0x3F59E096, 0x0224253F, 0xD20F6658 }; + + /* + I tried an implementation without precomputed LFSR offsets, since + I thought that might allow (especially on x86-64) the use of leal to + compute all the offsets.. However on my Core2 with GCC 4.3 it + turned out significantly slower (238 Mib/s, versus 300 Mib/s + with precomputed offsets) + + I also tried using byte vs u32bit for the offset variable (since + x86 memory addressing modes can be odd), but it made things even + slower (186 Mib/s) + */ + static const byte OFFSETS[221] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 12, 14, 15, 16, + 5, 6, 7, 8, 9, 10, 11, 12, 13, 0, 2, 3, 4, + 10, 11, 12, 13, 14, 15, 16, 0, 1, 5, 7, 8, 9, + 15, 16, 0, 1, 2, 3, 4, 5, 6, 10, 12, 13, 14, + 3, 4, 5, 6, 7, 8, 9, 10, 11, 15, 0, 1, 2, + 8, 9, 10, 11, 12, 13, 14, 15, 16, 3, 5, 6, 7, + 13, 14, 15, 16, 0, 1, 2, 3, 4, 8, 10, 11, 12, + 1, 2, 3, 4, 5, 6, 7, 8, 9, 13, 15, 16, 0, + 6, 7, 8, 9, 10, 11, 12, 13, 14, 1, 3, 4, 5, + 11, 12, 13, 14, 15, 16, 0, 1, 2, 6, 8, 9, 10, + 16, 0, 1, 2, 3, 4, 5, 6, 7, 11, 13, 14, 15, + 4, 5, 6, 7, 8, 9, 10, 11, 12, 16, 1, 2, 3, + 9, 10, 11, 12, 13, 14, 15, 16, 0, 4, 6, 7, 8, + 14, 15, 16, 0, 1, 2, 3, 4, 5, 9, 11, 12, 13, + 2, 3, 4, 5, 6, 7, 8, 9, 10, 14, 16, 0, 1, + 7, 8, 9, 10, 11, 12, 13, 14, 15, 2, 4, 5, 6, + 12, 13, 14, 15, 16, 0, 1, 2, 3, 7, 9, 10, 11 }; + + for(u32bit j = 0; j != 17; ++j) + { + const byte* R_off = OFFSETS + 13*j; + + u32bit R0 = R[R_off[0]]; + u32bit R1 = R[R_off[1]]; + u32bit R2 = R[R_off[2]]; + u32bit R3 = R[R_off[3]]; + u32bit R4 = R[R_off[4]]; + + const u32bit R5 = R[R_off[5]]; + const u32bit R6 = R[R_off[6]]; + const u32bit R7 = R[R_off[7]]; + const u32bit R8 = R[R_off[8]]; + const u32bit R9 = R[R_off[9]]; + const u32bit R10 = R[R_off[10]]; + const u32bit R11 = R[R_off[11]]; + const u32bit R12 = R[R_off[12]]; + + R[R_off[0]] = R0 = ((R0 << 8) ^ MULT_TAB[(R0 >> 24) & 0xFF]) ^ R11 ^ R4; + + u32bit A = R0; + u32bit B = R10; + u32bit C = R7; + u32bit D = R2; + u32bit E = R1; + + E += A + B + C + D; + + A += E; + B += E; + C += E; + D += E; + + A = S0[get_byte(0, A)] ^ S1[get_byte(1, A)] ^ + S2[get_byte(2, A)] ^ S3[get_byte(3, A)]; + B = S0[get_byte(1, B)] ^ S1[get_byte(2, B)] ^ + S2[get_byte(3, B)] ^ S3[get_byte(0, B)]; + C = S0[get_byte(2, C)] ^ S1[get_byte(3, C)] ^ + S2[get_byte(0, C)] ^ S3[get_byte(1, C)]; + D = S0[get_byte(3, D)] ^ S1[get_byte(0, D)] ^ + S2[get_byte(1, D)] ^ S3[get_byte(2, D)]; + E = S0[get_byte(0, E)] ^ S1[get_byte(1, E)] ^ + S2[get_byte(2, E)] ^ S3[get_byte(3, E)]; + + E += A + B + C + D; + + A += E; + B += E; + C += E; + D += E; + + R[R_off[1]] = R1 = ((R1 << 8) ^ MULT_TAB[(R1 >> 24) & 0xFF]) ^ R12 ^ R5; + R[R_off[2]] = R2 = ((R2 << 8) ^ MULT_TAB[(R2 >> 24) & 0xFF]) ^ R0 ^ R6; + R[R_off[3]] = ((R3 << 8) ^ MULT_TAB[(R3 >> 24) & 0xFF]) ^ R1 ^ R7; + + E += R4; + + R[R_off[4]] = ((R4 << 8) ^ MULT_TAB[(R4 >> 24) & 0xFF]) ^ R2 ^ R8; + + A += R1; + B += R12; + C += R9; + D += R5; + + store_be(A, buffer + 20*j + 0); + store_be(B, buffer + 20*j + 4); + store_be(C, buffer + 20*j + 8); + store_be(D, buffer + 20*j + 12); + store_be(E, buffer + 20*j + 16); + } + + position = 0; + } + +/* +* Turing's byte mixing step +*/ +u32bit Turing::fixedS(u32bit W) + { + for(u32bit j = 0; j != 4; ++j) + { + byte B = SBOX[get_byte(j, W)]; + W ^= rotate_left(Q_BOX[B], j*8); + W &= rotate_right(0x00FFFFFF, j*8); + W |= B << (24-j*8); + } + return W; + } + +/* +* Generate the expanded Turing Sbox tables +*/ +void Turing::gen_sbox(MemoryRegion<u32bit>& S, u32bit which, + const MemoryRegion<u32bit>& K) + { + for(u32bit j = 0; j != 256; ++j) + { + u32bit W = 0, C = j; + + for(u32bit k = 0; k < K.size(); ++k) + { + C = SBOX[get_byte(which, K[k]) ^ C]; + W ^= rotate_left(Q_BOX[C], k + 8*which); + } + S[j] = (W & rotate_right(0x00FFFFFF, 8*which)) | (C << (24 - 8*which)); + } + } + +/* +* Turing Key Schedule +*/ +void Turing::key_schedule(const byte key[], u32bit length) + { + K.create(length / 4); + for(u32bit j = 0; j != length; ++j) + K[j/4] = (K[j/4] << 8) + key[j]; + + for(u32bit j = 0; j != K.size(); ++j) + K[j] = fixedS(K[j]); + + PHT(K); + + gen_sbox(S0, 0, K); + gen_sbox(S1, 1, K); + gen_sbox(S2, 2, K); + gen_sbox(S3, 3, K); + + resync(0, 0); + } + +/* +* Resynchronization +*/ +void Turing::resync(const byte iv[], u32bit length) + { + if(length % 4 != 0 || length > 16) + throw Invalid_IV_Length(name(), length); + + SecureVector<u32bit> IV(length / 4); + for(u32bit j = 0; j != length; ++j) + IV[j/4] = (IV[j/4] << 8) + iv[j]; + + for(u32bit j = 0; j != IV.size(); ++j) + R[j] = IV[j] = fixedS(IV[j]); + + for(u32bit j = 0; j != K.size(); ++j) + R[j+IV.size()] = K[j]; + + R[K.size() + IV.size()] = (0x010203 << 8) | (K.size() << 4) | IV.size(); + + for(u32bit j = K.size() + IV.size() + 1; j != 17; ++j) + { + const u32bit W = R[j-K.size()-IV.size()-1] + R[j-1]; + R[j] = S0[get_byte(0, W)] ^ S1[get_byte(1, W)] ^ + S2[get_byte(2, W)] ^ S3[get_byte(3, W)]; + } + + PHT(R); + + generate(); + } + +/* +* Clear memory of sensitive data +*/ +void Turing::clear() throw() + { + S0.clear(); + S1.clear(); + S2.clear(); + S3.clear(); + + buffer.clear(); + position = 0; + } + +} diff --git a/botan/src/stream/turing/turing.h b/botan/src/stream/turing/turing.h new file mode 100644 index 0000000..d48c1d8 --- /dev/null +++ b/botan/src/stream/turing/turing.h @@ -0,0 +1,47 @@ +/* +* Turing +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_TURING_H__ +#define BOTAN_TURING_H__ + +#include <botan/stream_cipher.h> + +namespace Botan { + +/* +* Turing +*/ +class BOTAN_DLL Turing : public StreamCipher + { + public: + void clear() throw(); + std::string name() const { return "Turing"; } + StreamCipher* clone() const { return new Turing; } + Turing() : StreamCipher(4, 32, 4) { position = 0; } + private: + void cipher(const byte[], byte[], u32bit); + void key_schedule(const byte[], u32bit); + void resync(const byte[], u32bit); + void generate(); + + static u32bit fixedS(u32bit); + static void gen_sbox(MemoryRegion<u32bit>&, u32bit, + const MemoryRegion<u32bit>&); + + static const u32bit Q_BOX[256]; + static const byte SBOX[256]; + + SecureBuffer<u32bit, 256> S0, S1, S2, S3; + SecureBuffer<u32bit, 17> R; + SecureVector<u32bit> K; + SecureBuffer<byte, 340> buffer; + u32bit position; + }; + +} + +#endif diff --git a/botan/src/stream/wid_wake/info.txt b/botan/src/stream/wid_wake/info.txt new file mode 100644 index 0000000..9441641 --- /dev/null +++ b/botan/src/stream/wid_wake/info.txt @@ -0,0 +1,14 @@ +realname "WiderWake" + +define WID_WAKE + +load_on auto + +<requires> +stream +</requires> + +<add> +wid_wake.cpp +wid_wake.h +</add> diff --git a/botan/src/stream/wid_wake/wid_wake.cpp b/botan/src/stream/wid_wake/wid_wake.cpp new file mode 100644 index 0000000..1dc0fd7 --- /dev/null +++ b/botan/src/stream/wid_wake/wid_wake.cpp @@ -0,0 +1,147 @@ +/* +* WiderWake +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/wid_wake.h> +#include <botan/loadstor.h> +#include <botan/xor_buf.h> + +namespace Botan { + +/* +* Combine cipher stream with message +*/ +void WiderWake_41_BE::cipher(const byte in[], byte out[], u32bit length) + { + while(length >= buffer.size() - position) + { + xor_buf(out, in, buffer + position, buffer.size() - position); + length -= (buffer.size() - position); + in += (buffer.size() - position); + out += (buffer.size() - position); + generate(buffer.size()); + } + xor_buf(out, in, buffer + position, length); + position += length; + } + +/* +* Generate cipher stream +*/ +void WiderWake_41_BE::generate(u32bit length) + { + u32bit R0 = state[0], R1 = state[1], + R2 = state[2], R3 = state[3], + R4 = state[4]; + + for(u32bit j = 0; j != length; j += 8) + { + u32bit R0a; + + store_be(R3, buffer + j); + + R0a = R4 + R3; R3 += R2; R2 += R1; R1 += R0; + R0a = (R0a >> 8) ^ T[(R0a & 0xFF)]; + R1 = (R1 >> 8) ^ T[(R1 & 0xFF)]; + R2 = (R2 >> 8) ^ T[(R2 & 0xFF)]; + R3 = (R3 >> 8) ^ T[(R3 & 0xFF)]; + R4 = R0; R0 = R0a; + + store_be(R3, buffer + j + 4); + + R0a = R4 + R3; R3 += R2; R2 += R1; R1 += R0; + R0a = (R0a >> 8) ^ T[(R0a & 0xFF)]; + R1 = (R1 >> 8) ^ T[(R1 & 0xFF)]; + R2 = (R2 >> 8) ^ T[(R2 & 0xFF)]; + R3 = (R3 >> 8) ^ T[(R3 & 0xFF)]; + R4 = R0; R0 = R0a; + } + + state[0] = R0; + state[1] = R1; + state[2] = R2; + state[3] = R3; + state[4] = R4; + + position = 0; + } + +/* +* WiderWake Key Schedule +*/ +void WiderWake_41_BE::key_schedule(const byte key[], u32bit) + { + for(u32bit j = 0; j != 4; ++j) + t_key[j] = load_be<u32bit>(key, j); + + static const u32bit MAGIC[8] = { + 0x726A8F3B, 0xE69A3B5C, 0xD3C71FE5, 0xAB3C73D2, + 0x4D3A8EB3, 0x0396D6E8, 0x3D4C2F7A, 0x9EE27CF3 }; + + for(u32bit j = 0; j != 4; ++j) + T[j] = t_key[j]; + for(u32bit j = 4; j != 256; ++j) + { + u32bit X = T[j-1] + T[j-4]; + T[j] = (X >> 3) ^ MAGIC[X % 8]; + } + for(u32bit j = 0; j != 23; ++j) + T[j] += T[j+89]; + + u32bit X = T[33]; + u32bit Z = (T[59] | 0x01000001) & 0xFF7FFFFF; + for(u32bit j = 0; j != 256; ++j) + { + X = (X & 0xFF7FFFFF) + Z; + T[j] = (T[j] & 0x00FFFFFF) ^ X; + } + X = (T[X & 0xFF] ^ X) & 0xFF; + Z = T[0]; + T[0] = T[X]; + for(u32bit j = 1; j != 256; ++j) + { + T[X] = T[j]; + X = (T[j ^ X] ^ X) & 0xFF; + T[j] = T[X]; + } + T[X] = Z; + + position = 0; + const byte iv[8] = { 0 }; + resync(iv, 8); + } + +/* +* Resynchronization +*/ +void WiderWake_41_BE::resync(const byte iv[], u32bit length) + { + if(length != 8) + throw Invalid_IV_Length(name(), length); + + for(u32bit j = 0; j != 4; ++j) + state[j] = t_key[j]; + state[4] = load_be<u32bit>(iv, 0); + state[0] ^= state[4]; + state[2] ^= load_be<u32bit>(iv, 1); + + generate(8*4); + generate(buffer.size()); + } + +/* +* Clear memory of sensitive data +*/ +void WiderWake_41_BE::clear() throw() + { + position = 0; + t_key.clear(); + state.clear(); + T.clear(); + buffer.clear(); + } + +} diff --git a/botan/src/stream/wid_wake/wid_wake.h b/botan/src/stream/wid_wake/wid_wake.h new file mode 100644 index 0000000..4720afd --- /dev/null +++ b/botan/src/stream/wid_wake/wid_wake.h @@ -0,0 +1,41 @@ +/* +* WiderWake +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_WIDER_WAKE_H__ +#define BOTAN_WIDER_WAKE_H__ + +#include <botan/stream_cipher.h> + +namespace Botan { + +/* +* WiderWake4+1-BE +*/ +class BOTAN_DLL WiderWake_41_BE : public StreamCipher + { + public: + void clear() throw(); + std::string name() const { return "WiderWake4+1-BE"; } + StreamCipher* clone() const { return new WiderWake_41_BE; } + WiderWake_41_BE() : StreamCipher(16, 16, 1, 8) {} + private: + void cipher(const byte[], byte[], u32bit); + void key_schedule(const byte[], u32bit); + void resync(const byte[], u32bit); + + void generate(u32bit); + + SecureBuffer<byte, DEFAULT_BUFFERSIZE> buffer; + SecureBuffer<u32bit, 256> T; + SecureBuffer<u32bit, 5> state; + SecureBuffer<u32bit, 4> t_key; + u32bit position; + }; + +} + +#endif diff --git a/botan/src/sym_algo/info.txt b/botan/src/sym_algo/info.txt new file mode 100644 index 0000000..03804a9 --- /dev/null +++ b/botan/src/sym_algo/info.txt @@ -0,0 +1,16 @@ +realname "Symmetric Algorithms" + +load_on auto + +<add> +sym_algo.h +symkey.cpp +symkey.h +</add> + +<requires> +alloc +filters +hex +rng +</requires> diff --git a/botan/src/sym_algo/sym_algo.h b/botan/src/sym_algo/sym_algo.h new file mode 100644 index 0000000..1c8b816 --- /dev/null +++ b/botan/src/sym_algo/sym_algo.h @@ -0,0 +1,101 @@ +/** +* Symmetric Algorithm Base Class +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_SYMMETRIC_ALGORITHM_H__ +#define BOTAN_SYMMETRIC_ALGORITHM_H__ + +#include <botan/types.h> +#include <botan/exceptn.h> +#include <botan/symkey.h> + +namespace Botan { + +/** +* This class represents a symmetric algorithm object. +*/ +class BOTAN_DLL SymmetricAlgorithm + { + public: + + /** + * The maximum allowed key length. + */ + const u32bit MAXIMUM_KEYLENGTH; + + /** + * The minimal allowed key length. + */ + const u32bit MINIMUM_KEYLENGTH; + + /** + * A valid keylength is a multiple of this value. + */ + const u32bit KEYLENGTH_MULTIPLE; + + /** + * The name of the algorithm. + * @return the name of the algorithm + */ + virtual std::string name() const = 0; + + /** + * Set the symmetric key of this object. + * @param key the SymmetricKey to be set. + */ + void set_key(const SymmetricKey& key) throw(Invalid_Key_Length) + { set_key(key.begin(), key.length()); } + + /** + * Set the symmetric key of this object. + * @param key the to be set as a byte array. + * @param the length of the byte array. + */ + void set_key(const byte key[], u32bit length) throw(Invalid_Key_Length) + { + if(!valid_keylength(length)) + throw Invalid_Key_Length(name(), length); + key_schedule(key, length); + } + + /** + * Check whether a given key length is valid for this algorithm. + * @param length the key length to be checked. + * @return true if the key length is valid. + */ + bool valid_keylength(u32bit length) const + { + return ((length >= MINIMUM_KEYLENGTH) && + (length <= MAXIMUM_KEYLENGTH) && + (length % KEYLENGTH_MULTIPLE == 0)); + } + + /** + * Construct a SymmetricAlgorithm. + * @param key_min the minimum allowed key length + * @param key_max the maximum allowed key length + * @param key_mod any valid key length must be a multiple of this value + */ + SymmetricAlgorithm(u32bit key_min, u32bit key_max, u32bit key_mod) : + MAXIMUM_KEYLENGTH(key_max ? key_max : key_min), + MINIMUM_KEYLENGTH(key_min), + KEYLENGTH_MULTIPLE(key_mod) + {} + + virtual ~SymmetricAlgorithm() {} + private: + virtual void key_schedule(const byte[], u32bit) = 0; + }; + +/** +* The two possible directions for cipher filters, determining whether they +* actually perform encryption or decryption. +*/ +enum Cipher_Dir { ENCRYPTION, DECRYPTION }; + +} + +#endif diff --git a/botan/src/sym_algo/symkey.cpp b/botan/src/sym_algo/symkey.cpp new file mode 100644 index 0000000..32dfe68 --- /dev/null +++ b/botan/src/sym_algo/symkey.cpp @@ -0,0 +1,141 @@ +/* +* OctetString +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/symkey.h> +#include <botan/xor_buf.h> +#include <botan/rng.h> +#include <botan/pipe.h> +#include <botan/hex.h> +#include <algorithm> + +namespace Botan { + +/* +* Create an OctetString from RNG output +*/ +OctetString::OctetString(RandomNumberGenerator& rng, + u32bit length) + { + bits.create(length); + rng.randomize(bits, length); + } + +/* +* Create an OctetString from a hex string +*/ +void OctetString::change(const std::string& hex_string) + { + SecureVector<byte> hex; + for(u32bit j = 0; j != hex_string.length(); ++j) + if(Hex_Decoder::is_valid(hex_string[j])) + hex.append(hex_string[j]); + + if(hex.size() % 2 != 0) + throw Invalid_Argument("OctetString: hex string must encode full bytes"); + bits.create(hex.size() / 2); + for(u32bit j = 0; j != bits.size(); ++j) + bits[j] = Hex_Decoder::decode(hex.begin() + 2*j); + } + +/* +* Create an OctetString from a byte string +*/ +void OctetString::change(const byte in[], u32bit n) + { + bits.create(n); + bits.copy(in, n); + } + +/* +* Set the parity of each key byte to odd +*/ +void OctetString::set_odd_parity() + { + const byte ODD_PARITY[256] = { + 0x01, 0x01, 0x02, 0x02, 0x04, 0x04, 0x07, 0x07, 0x08, 0x08, 0x0B, 0x0B, + 0x0D, 0x0D, 0x0E, 0x0E, 0x10, 0x10, 0x13, 0x13, 0x15, 0x15, 0x16, 0x16, + 0x19, 0x19, 0x1A, 0x1A, 0x1C, 0x1C, 0x1F, 0x1F, 0x20, 0x20, 0x23, 0x23, + 0x25, 0x25, 0x26, 0x26, 0x29, 0x29, 0x2A, 0x2A, 0x2C, 0x2C, 0x2F, 0x2F, + 0x31, 0x31, 0x32, 0x32, 0x34, 0x34, 0x37, 0x37, 0x38, 0x38, 0x3B, 0x3B, + 0x3D, 0x3D, 0x3E, 0x3E, 0x40, 0x40, 0x43, 0x43, 0x45, 0x45, 0x46, 0x46, + 0x49, 0x49, 0x4A, 0x4A, 0x4C, 0x4C, 0x4F, 0x4F, 0x51, 0x51, 0x52, 0x52, + 0x54, 0x54, 0x57, 0x57, 0x58, 0x58, 0x5B, 0x5B, 0x5D, 0x5D, 0x5E, 0x5E, + 0x61, 0x61, 0x62, 0x62, 0x64, 0x64, 0x67, 0x67, 0x68, 0x68, 0x6B, 0x6B, + 0x6D, 0x6D, 0x6E, 0x6E, 0x70, 0x70, 0x73, 0x73, 0x75, 0x75, 0x76, 0x76, + 0x79, 0x79, 0x7A, 0x7A, 0x7C, 0x7C, 0x7F, 0x7F, 0x80, 0x80, 0x83, 0x83, + 0x85, 0x85, 0x86, 0x86, 0x89, 0x89, 0x8A, 0x8A, 0x8C, 0x8C, 0x8F, 0x8F, + 0x91, 0x91, 0x92, 0x92, 0x94, 0x94, 0x97, 0x97, 0x98, 0x98, 0x9B, 0x9B, + 0x9D, 0x9D, 0x9E, 0x9E, 0xA1, 0xA1, 0xA2, 0xA2, 0xA4, 0xA4, 0xA7, 0xA7, + 0xA8, 0xA8, 0xAB, 0xAB, 0xAD, 0xAD, 0xAE, 0xAE, 0xB0, 0xB0, 0xB3, 0xB3, + 0xB5, 0xB5, 0xB6, 0xB6, 0xB9, 0xB9, 0xBA, 0xBA, 0xBC, 0xBC, 0xBF, 0xBF, + 0xC1, 0xC1, 0xC2, 0xC2, 0xC4, 0xC4, 0xC7, 0xC7, 0xC8, 0xC8, 0xCB, 0xCB, + 0xCD, 0xCD, 0xCE, 0xCE, 0xD0, 0xD0, 0xD3, 0xD3, 0xD5, 0xD5, 0xD6, 0xD6, + 0xD9, 0xD9, 0xDA, 0xDA, 0xDC, 0xDC, 0xDF, 0xDF, 0xE0, 0xE0, 0xE3, 0xE3, + 0xE5, 0xE5, 0xE6, 0xE6, 0xE9, 0xE9, 0xEA, 0xEA, 0xEC, 0xEC, 0xEF, 0xEF, + 0xF1, 0xF1, 0xF2, 0xF2, 0xF4, 0xF4, 0xF7, 0xF7, 0xF8, 0xF8, 0xFB, 0xFB, + 0xFD, 0xFD, 0xFE, 0xFE }; + + for(u32bit j = 0; j != bits.size(); ++j) + bits[j] = ODD_PARITY[bits[j]]; + } + +/* +* Hex encode an OctetString +*/ +std::string OctetString::as_string() const + { + Pipe pipe(new Hex_Encoder); + pipe.process_msg(bits); + return pipe.read_all_as_string(); + } + +/* +* XOR Operation for OctetStrings +*/ +OctetString& OctetString::operator^=(const OctetString& k) + { + if(&k == this) { bits.clear(); return (*this); } + xor_buf(bits.begin(), k.begin(), std::min(length(), k.length())); + return (*this); + } + +/* +* Equality Operation for OctetStrings +*/ +bool operator==(const OctetString& s1, const OctetString& s2) + { + return (s1.bits_of() == s2.bits_of()); + } + +/* +* Unequality Operation for OctetStrings +*/ +bool operator!=(const OctetString& s1, const OctetString& s2) + { + return !(s1 == s2); + } + +/* +* Append Operation for OctetStrings +*/ +OctetString operator+(const OctetString& k1, const OctetString& k2) + { + return OctetString(SecureVector<byte>(k1.bits_of(), k2.bits_of())); + } + +/* +* XOR Operation for OctetStrings +*/ +OctetString operator^(const OctetString& k1, const OctetString& k2) + { + SecureVector<byte> ret(std::max(k1.length(), k2.length())); + ret.copy(k1.begin(), k1.length()); + xor_buf(ret, k2.begin(), k2.length()); + return OctetString(ret); + } + +} diff --git a/botan/src/sym_algo/symkey.h b/botan/src/sym_algo/symkey.h new file mode 100644 index 0000000..5504297 --- /dev/null +++ b/botan/src/sym_algo/symkey.h @@ -0,0 +1,62 @@ +/* +* OctetString +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_SYMKEY_H__ +#define BOTAN_SYMKEY_H__ + +#include <botan/secmem.h> +#include <string> + +namespace Botan { + +/* +* Octet String +*/ +class BOTAN_DLL OctetString + { + public: + u32bit length() const { return bits.size(); } + SecureVector<byte> bits_of() const { return bits; } + + const byte* begin() const { return bits.begin(); } + const byte* end() const { return bits.end(); } + + std::string as_string() const; + + OctetString& operator^=(const OctetString&); + + void set_odd_parity(); + + void change(const std::string&); + void change(const byte[], u32bit); + void change(const MemoryRegion<byte>& in) { bits = in; } + + OctetString(class RandomNumberGenerator&, u32bit len); + OctetString(const std::string& str = "") { change(str); } + OctetString(const byte in[], u32bit len) { change(in, len); } + OctetString(const MemoryRegion<byte>& in) { change(in); } + private: + SecureVector<byte> bits; + }; + +/* +* Operations on Octet Strings +*/ +BOTAN_DLL bool operator==(const OctetString&, const OctetString&); +BOTAN_DLL bool operator!=(const OctetString&, const OctetString&); +BOTAN_DLL OctetString operator+(const OctetString&, const OctetString&); +BOTAN_DLL OctetString operator^(const OctetString&, const OctetString&); + +/* +* Alternate Names +*/ +typedef OctetString SymmetricKey; +typedef OctetString InitializationVector; + +} + +#endif diff --git a/botan/src/timer/cpu_counter/info.txt b/botan/src/timer/cpu_counter/info.txt new file mode 100644 index 0000000..025663a --- /dev/null +++ b/botan/src/timer/cpu_counter/info.txt @@ -0,0 +1,36 @@ +realname "Hardware Timer" + +define TIMER_HARDWARE + +load_on asm_ok + +<add> +tm_hard.cpp +tm_hard.h +</add> + +<cc> +gcc +</cc> + +<arch> +# RDTSC: Pentium and up +i586 +i686 +athlon +pentium4 +pentium-m +amd64 + +ppc # PPC timebase register +ppc64 # PPC timebase register +alpha # rpcc +sparc64 # %tick register +ia64 # ar.itc +s390x +hppa +</arch> + +<requires> +timer +</requires> diff --git a/botan/src/timer/cpu_counter/tm_hard.cpp b/botan/src/timer/cpu_counter/tm_hard.cpp new file mode 100644 index 0000000..9e31aee --- /dev/null +++ b/botan/src/timer/cpu_counter/tm_hard.cpp @@ -0,0 +1,51 @@ +/* +* Hardware Timer +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/tm_hard.h> + +namespace Botan { + +/* +* Get the timestamp +*/ +u64bit Hardware_Timer::clock() const + { + u64bit rtc = 0; + +#if defined(BOTAN_TARGET_ARCH_IS_IA32) || defined(BOTAN_TARGET_ARCH_IS_AMD64) + u32bit rtc_low = 0, rtc_high = 0; + asm volatile("rdtsc" : "=d" (rtc_high), "=a" (rtc_low)); + rtc = (static_cast<u64bit>(rtc_high) << 32) | rtc_low; + +#elif defined(BOTAN_TARGET_ARCH_IS_PPC) || defined(BOTAN_TARGET_ARCH_IS_PPC64) + u32bit rtc_low = 0, rtc_high = 0; + asm volatile("mftbu %0; mftb %1" : "=r" (rtc_high), "=r" (rtc_low)); + rtc = (static_cast<u64bit>(rtc_high) << 32) | rtc_low; + +#elif defined(BOTAN_TARGET_ARCH_IS_ALPHA) + asm volatile("rpcc %0" : "=r" (rtc)); + +#elif defined(BOTAN_TARGET_ARCH_IS_SPARC64) + asm volatile("rd %%tick, %0" : "=r" (rtc)); + +#elif defined(BOTAN_TARGET_ARCH_IS_IA64) + asm volatile("mov %0=ar.itc" : "=r" (rtc)); + +#elif defined(BOTAN_TARGET_ARCH_IS_S390X) + asm volatile("stck 0(%0)" : : "a" (&rtc) : "memory", "cc"); + +#elif defined(BOTAN_TARGET_ARCH_IS_HPPA) + asm volatile("mfctl 16,%0" : "=r" (rtc)); // 64-bit only? + +#else + #error "Unsure how to access hardware timer on this system" +#endif + + return rtc; + } + +} diff --git a/botan/src/timer/cpu_counter/tm_hard.h b/botan/src/timer/cpu_counter/tm_hard.h new file mode 100644 index 0000000..2e338ec --- /dev/null +++ b/botan/src/timer/cpu_counter/tm_hard.h @@ -0,0 +1,33 @@ +/* +* Hardware Timer +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_TIMER_HARDWARE_H__ +#define BOTAN_TIMER_HARDWARE_H__ + +#include <botan/timer.h> + +namespace Botan { + +/* +* Hardware Timer +*/ +class BOTAN_DLL Hardware_Timer : public Timer + { + public: + /* + @todo: Add sync(Timer& wall_clock, bool milliseconds) which busy + loops using wall_clock and tries to guess the tick rate of the + hardware counter, allowing it to be used for benchmarks, etc + */ + + std::string name() const { return "Hardware Timer"; } + u64bit clock() const; + }; + +} + +#endif diff --git a/botan/src/timer/gettimeofday/info.txt b/botan/src/timer/gettimeofday/info.txt new file mode 100644 index 0000000..a58e808 --- /dev/null +++ b/botan/src/timer/gettimeofday/info.txt @@ -0,0 +1,33 @@ +realname "Unix Timer" + +define TIMER_UNIX + +load_on auto +modset unix,beos + +<add> +tm_unix.cpp +tm_unix.h +</add> + +<os> +aix +beos +cygwin +darwin +freebsd +dragonfly +hpux +irix +linux +netbsd +openbsd +qnx +solaris +tru64 +</os> + +<requires> +timer +</requires> + diff --git a/botan/src/timer/gettimeofday/tm_unix.cpp b/botan/src/timer/gettimeofday/tm_unix.cpp new file mode 100644 index 0000000..e32df71 --- /dev/null +++ b/botan/src/timer/gettimeofday/tm_unix.cpp @@ -0,0 +1,24 @@ +/* +* Unix Timer +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/tm_unix.h> +#include <botan/util.h> +#include <sys/time.h> + +namespace Botan { + +/* +* Get the timestamp +*/ +u64bit Unix_Timer::clock() const + { + struct ::timeval tv; + ::gettimeofday(&tv, 0); + return combine_timers(tv.tv_sec, tv.tv_usec, 1000000); + } + +} diff --git a/botan/src/timer/gettimeofday/tm_unix.h b/botan/src/timer/gettimeofday/tm_unix.h new file mode 100644 index 0000000..c304dbb --- /dev/null +++ b/botan/src/timer/gettimeofday/tm_unix.h @@ -0,0 +1,27 @@ +/* +* Unix Timer +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_TIMER_UNIX_H__ +#define BOTAN_TIMER_UNIX_H__ + +#include <botan/timer.h> + +namespace Botan { + +/* +* Unix Timer +*/ +class BOTAN_DLL Unix_Timer : public Timer + { + public: + std::string name() const { return "Unix gettimeofday"; } + u64bit clock() const; + }; + +} + +#endif diff --git a/botan/src/timer/info.txt b/botan/src/timer/info.txt new file mode 100644 index 0000000..6408dca --- /dev/null +++ b/botan/src/timer/info.txt @@ -0,0 +1,14 @@ +realname "Timer Base Class" + +define TIMER + +load_on auto + +<add> +timer.cpp +timer.h +</add> + +<requires> +rng +</requires> diff --git a/botan/src/timer/posix_rt/info.txt b/botan/src/timer/posix_rt/info.txt new file mode 100644 index 0000000..fa530ea --- /dev/null +++ b/botan/src/timer/posix_rt/info.txt @@ -0,0 +1,29 @@ +realname "POSIX Timer" + +define TIMER_POSIX + +load_on auto + +<add> +tm_posix.cpp +tm_posix.h +</add> + +<libs> +linux -> rt +</libs> + +# The *BSDs put clock_gettime in sys/time.h, not time.h like POSIX says +<os> +cygwin +linux +#freebsd +dragonfly +#netbsd +#openbsd +</os> + +<requires> +timer +</requires> + diff --git a/botan/src/timer/posix_rt/tm_posix.cpp b/botan/src/timer/posix_rt/tm_posix.cpp new file mode 100644 index 0000000..d356384 --- /dev/null +++ b/botan/src/timer/posix_rt/tm_posix.cpp @@ -0,0 +1,33 @@ +/* +* POSIX Timer +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/tm_posix.h> +#include <botan/util.h> + +#ifndef _POSIX_C_SOURCE + #define _POSIX_C_SOURCE 199309 +#endif + +#include <time.h> + +#ifndef CLOCK_REALTIME + #define CLOCK_REALTIME 0 +#endif + +namespace Botan { + +/* +* Get the timestamp +*/ +u64bit POSIX_Timer::clock() const + { + struct ::timespec tv; + ::clock_gettime(CLOCK_REALTIME, &tv); + return combine_timers(tv.tv_sec, tv.tv_nsec, 1000000000); + } + +} diff --git a/botan/src/timer/posix_rt/tm_posix.h b/botan/src/timer/posix_rt/tm_posix.h new file mode 100644 index 0000000..8bedccf --- /dev/null +++ b/botan/src/timer/posix_rt/tm_posix.h @@ -0,0 +1,27 @@ +/* +* POSIX Timer +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_TIMER_POSIX_H__ +#define BOTAN_TIMER_POSIX_H__ + +#include <botan/timer.h> + +namespace Botan { + +/* +* POSIX Timer +*/ +class BOTAN_DLL POSIX_Timer : public Timer + { + public: + std::string name() const { return "POSIX clock_gettime"; } + u64bit clock() const; + }; + +} + +#endif diff --git a/botan/src/timer/timer.cpp b/botan/src/timer/timer.cpp new file mode 100644 index 0000000..035c217 --- /dev/null +++ b/botan/src/timer/timer.cpp @@ -0,0 +1,52 @@ +/** +* Timestamp Functions +* (C) 1999-2009 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/timer.h> +#include <botan/loadstor.h> +#include <botan/util.h> +#include <ctime> + +namespace Botan { + +/** +* Get the system clock +*/ +u64bit system_time() + { + return static_cast<u64bit>(std::time(0)); + } + +/** +* Read the clock and return the output +*/ +void Timer::poll(Entropy_Accumulator& accum) + { + const u64bit clock_value = this->clock(); + accum.add(clock_value, 0); + } + +/** +* Combine a two time values into a single one +*/ +u64bit Timer::combine_timers(u32bit seconds, u32bit parts, u32bit parts_hz) + { + static const u64bit NANOSECONDS_UNITS = 1000000000; + + u64bit res = seconds * NANOSECONDS_UNITS; + res += parts * (NANOSECONDS_UNITS / parts_hz); + return res; + } + +/** +* ANSI Clock +*/ +u64bit ANSI_Clock_Timer::clock() const + { + return combine_timers(std::time(0), std::clock(), CLOCKS_PER_SEC); + } + +} diff --git a/botan/src/timer/timer.h b/botan/src/timer/timer.h new file mode 100644 index 0000000..b6e8ef4 --- /dev/null +++ b/botan/src/timer/timer.h @@ -0,0 +1,45 @@ +/** +* Timestamp Functions +* (C) 1999-2009 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_TIMERS_H__ +#define BOTAN_TIMERS_H__ + +#include <botan/rng.h> + +namespace Botan { + +/** +* Timer Interface +*/ +class BOTAN_DLL Timer : public EntropySource + { + public: + /** + @return nanoseconds resolution timestamp, unknown epoch + */ + virtual u64bit clock() const = 0; + + void poll(Entropy_Accumulator& accum); + + virtual ~Timer() {} + protected: + static u64bit combine_timers(u32bit, u32bit, u32bit); + }; + +/** +* ANSI Clock Timer +*/ +class BOTAN_DLL ANSI_Clock_Timer : public Timer + { + public: + std::string name() const { return "ANSI clock"; } + u64bit clock() const; + }; + +} + +#endif diff --git a/botan/src/timer/win32_query_perf_ctr/info.txt b/botan/src/timer/win32_query_perf_ctr/info.txt new file mode 100644 index 0000000..4bb1ddb --- /dev/null +++ b/botan/src/timer/win32_query_perf_ctr/info.txt @@ -0,0 +1,26 @@ +realname "Win32 Timer" + +define TIMER_WIN32 +modset win32 + +load_on auto + +<add> +tm_win32.cpp +tm_win32.h +</add> + +<os> +cygwin +windows +mingw +</os> + +<libs> +windows -> user32.lib +</libs> + +<requires> +timer +</requires> + diff --git a/botan/src/timer/win32_query_perf_ctr/tm_win32.cpp b/botan/src/timer/win32_query_perf_ctr/tm_win32.cpp new file mode 100644 index 0000000..6b878e6 --- /dev/null +++ b/botan/src/timer/win32_query_perf_ctr/tm_win32.cpp @@ -0,0 +1,23 @@ +/* +* Win32 Timer +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/tm_win32.h> +#include <windows.h> + +namespace Botan { + +/* +* Get the timestamp +*/ +u64bit Win32_Timer::clock() const + { + LARGE_INTEGER tv; + ::QueryPerformanceCounter(&tv); + return tv.QuadPart; + } + +} diff --git a/botan/src/timer/win32_query_perf_ctr/tm_win32.h b/botan/src/timer/win32_query_perf_ctr/tm_win32.h new file mode 100644 index 0000000..5bcb720 --- /dev/null +++ b/botan/src/timer/win32_query_perf_ctr/tm_win32.h @@ -0,0 +1,27 @@ +/* +* Win32 Timer +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_TIMER_WIN32_H__ +#define BOTAN_TIMER_WIN32_H__ + +#include <botan/timer.h> + +namespace Botan { + +/* +* Win32 Timer +*/ +class BOTAN_DLL Win32_Timer : public Timer + { + public: + std::string name() const { return "Win32 QueryPerformanceCounter"; } + u64bit clock() const; + }; + +} + +#endif diff --git a/botan/src/utils/asm_amd64/asm_macr.h b/botan/src/utils/asm_amd64/asm_macr.h new file mode 100644 index 0000000..287fa3e --- /dev/null +++ b/botan/src/utils/asm_amd64/asm_macr.h @@ -0,0 +1,127 @@ +/* +* Assembly Macros +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_AMD64_ASM_MACROS_H__ +#define BOTAN_AMD64_ASM_MACROS_H__ + +/* +* General/Global Macros +*/ +#define ALIGN .p2align 4,,15 + +#define START_LISTING(FILENAME) \ + .file #FILENAME; \ + .text; \ + ALIGN; + +#if defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + +/* +* Function Definitions +*/ +#define START_FUNCTION(func_name) \ + ALIGN; \ + .global func_name; \ + .type func_name,@function; \ +func_name: + +#define END_FUNCTION(func_name) \ + ret + +/* +* Conditional Jumps +*/ +#define JUMP_IF_ZERO(REG, LABEL) \ + cmp IMM(0), REG; \ + jz LABEL + +#define JUMP_IF_LT(REG, NUM, LABEL) \ + cmp IMM(NUM), REG; \ + jl LABEL + +/* +* Register Names +*/ +#define R0 %rax +#define R1 %rbx +#define R2 %rcx +#define R2_32 %ecx +#define R3 %rdx +#define R3_32 %edx +#define R4 %rsp +#define R5 %rbp +#define R6 %rsi +#define R6_32 %esi +#define R7 %rdi +#define R8 %r8 +#define R9 %r9 +#define R9_32 %r9d +#define R10 %r10 +#define R11 %r11 +#define R12 %r12 +#define R13 %r13 +#define R14 %r14 +#define R15 %r15 +#define R16 %r16 + +#define ARG_1 R7 +#define ARG_2 R6 +#define ARG_2_32 R6_32 +#define ARG_3 R3 +#define ARG_3_32 R3_32 +#define ARG_4 R2 +#define ARG_4_32 R2_32 +#define ARG_5 R8 +#define ARG_6 R9 +#define ARG_6_32 R9_32 + +#define TEMP_1 R10 +#define TEMP_2 R11 +#define TEMP_3 ARG_6 +#define TEMP_4 ARG_5 +#define TEMP_5 ARG_4 +#define TEMP_5_32 ARG_4_32 +#define TEMP_6 ARG_3 +#define TEMP_7 ARG_2 +#define TEMP_8 ARG_1 +#define TEMP_9 R0 + +/* +* Memory Access Operations +*/ +#define ARRAY8(REG, NUM) 8*(NUM)(REG) +#define ARRAY4(REG, NUM) 4*(NUM)(REG) + +#define ASSIGN(TO, FROM) mov FROM, TO + +/* +* ALU Operations +*/ +#define IMM(VAL) $VAL + +#define ADD(TO, FROM) add FROM, TO +#define ADD_LAST_CARRY(REG) adc IMM(0), REG +#define ADD_IMM(TO, NUM) ADD(TO, IMM(NUM)) +#define ADD_W_CARRY(TO1, TO2, FROM) add FROM, TO1; adc IMM(0), TO2; +#define SUB_IMM(TO, NUM) sub IMM(NUM), TO +#define MUL(REG) mul REG + +#define XOR(TO, FROM) xor FROM, TO +#define AND(TO, FROM) and FROM, TO +#define OR(TO, FROM) or FROM, TO +#define NOT(REG) not REG +#define ZEROIZE(REG) XOR(REG, REG) + +#define RETURN_VALUE_IS(V) ASSIGN(%rax, V) + +#define ROTL_IMM(REG, NUM) rol IMM(NUM), REG +#define ROTR_IMM(REG, NUM) ror IMM(NUM), REG +#define ADD3_IMM(TO, FROM, NUM) lea NUM(TO,FROM,1), TO + +#endif diff --git a/botan/src/utils/asm_amd64/info.txt b/botan/src/utils/asm_amd64/info.txt new file mode 100644 index 0000000..19035b5 --- /dev/null +++ b/botan/src/utils/asm_amd64/info.txt @@ -0,0 +1,16 @@ +realname "Assembler Macros (x86-64)" + +load_on dep + +<add> +asm_macr.h +</add> + +<arch> +amd64 +</arch> + +<cc> +gcc +icc +</cc> diff --git a/botan/src/utils/asm_ia32/asm_macr.h b/botan/src/utils/asm_ia32/asm_macr.h new file mode 100644 index 0000000..2ea6951 --- /dev/null +++ b/botan/src/utils/asm_ia32/asm_macr.h @@ -0,0 +1,128 @@ +/* +* Assembly Macros +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_IA32_ASM_MACROS_H__ +#define BOTAN_IA32_ASM_MACROS_H__ + +/* +* General/Global Macros +*/ +#define ALIGN .p2align 4,,15 + +#define START_LISTING(FILENAME) \ + .file #FILENAME; \ + .text; \ + ALIGN; + +#if defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + +/* +* Function Definitions +*/ +#define START_FUNCTION(func_name) \ + ALIGN; \ + .global func_name; \ + .type func_name,@function; \ +func_name: + +#define END_FUNCTION(func_name) \ + ret + +/* +* Loop Control +*/ +#define START_LOOP(LABEL) \ + ALIGN; \ + LABEL##_LOOP: + +#define LOOP_UNTIL_EQ(REG, NUM, LABEL) \ + cmpl IMM(NUM), REG; \ + jne LABEL##_LOOP + +#define LOOP_UNTIL_LT(REG, NUM, LABEL) \ + cmpl IMM(NUM), REG; \ + jge LABEL##_LOOP + +/* + Conditional Jumps +*/ +#define JUMP_IF_ZERO(REG, LABEL) \ + cmpl IMM(0), REG; \ + jz LABEL + +#define JUMP_IF_LT(REG, NUM, LABEL) \ + cmpl IMM(NUM), REG; \ + jl LABEL + +/* +* Register Names +*/ +#define EAX %eax +#define EBX %ebx +#define ECX %ecx +#define EDX %edx +#define EBP %ebp +#define EDI %edi +#define ESI %esi +#define ESP %esp + +/* +* Memory Access Operations +*/ +#define ARRAY1(REG, NUM) (NUM)(REG) +#define ARRAY4(REG, NUM) 4*(NUM)(REG) +#define ARRAY4_INDIRECT(BASE, OFFSET, NUM) 4*(NUM)(BASE,OFFSET,4) +#define ARG(NUM) 4*(PUSHED) + ARRAY4(ESP, NUM) + +#define ASSIGN(TO, FROM) movl FROM, TO +#define ASSIGN_BYTE(TO, FROM) movzbl FROM, TO + +#define PUSH(REG) pushl REG +#define POP(REG) popl REG + +#define SPILL_REGS() \ + PUSH(EBP) ; \ + PUSH(EDI) ; \ + PUSH(ESI) ; \ + PUSH(EBX) + +#define RESTORE_REGS() \ + POP(EBX) ; \ + POP(ESI) ; \ + POP(EDI) ; \ + POP(EBP) + +/* +* ALU Operations +*/ +#define IMM(VAL) $VAL + +#define ADD(TO, FROM) addl FROM, TO +#define ADD_IMM(TO, NUM) ADD(TO, IMM(NUM)) +#define ADD_W_CARRY(TO1, TO2, FROM) addl FROM, TO1; adcl IMM(0), TO2; +#define SUB_IMM(TO, NUM) subl IMM(NUM), TO +#define ADD2_IMM(TO, FROM, NUM) leal NUM(FROM), TO +#define ADD3_IMM(TO, FROM, NUM) leal NUM(TO,FROM,1), TO +#define MUL(REG) mull REG + +#define SHL_IMM(REG, SHIFT) shll IMM(SHIFT), REG +#define SHR_IMM(REG, SHIFT) shrl IMM(SHIFT), REG +#define SHL2_3(TO, FROM) leal 0(,FROM,8), TO + +#define XOR(TO, FROM) xorl FROM, TO +#define AND(TO, FROM) andl FROM, TO +#define OR(TO, FROM) orl FROM, TO +#define NOT(REG) notl REG +#define ZEROIZE(REG) XOR(REG, REG) + +#define ROTL_IMM(REG, NUM) roll IMM(NUM), REG +#define ROTR_IMM(REG, NUM) rorl IMM(NUM), REG +#define BSWAP(REG) bswapl REG + +#endif diff --git a/botan/src/utils/asm_ia32/info.txt b/botan/src/utils/asm_ia32/info.txt new file mode 100644 index 0000000..4340c35 --- /dev/null +++ b/botan/src/utils/asm_ia32/info.txt @@ -0,0 +1,16 @@ +realname "Assembler Macros (IA-32)" + +load_on dep + +<add> +asm_macr.h +</add> + +<arch> +ia32 +</arch> + +<cc> +gcc +icc +</cc> diff --git a/botan/src/utils/bit_ops.h b/botan/src/utils/bit_ops.h new file mode 100644 index 0000000..c02ec53 --- /dev/null +++ b/botan/src/utils/bit_ops.h @@ -0,0 +1,91 @@ +/* +* Bit/Word Operations +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_BIT_OPS_H__ +#define BOTAN_BIT_OPS_H__ + +#include <botan/types.h> + +namespace Botan { + +/* +* Return true iff arg is 2**n for some n > 0 +* T should be an unsigned integer type +*/ +template<typename T> +inline bool power_of_2(T arg) + { + return ((arg != 0 && arg != 1) && ((arg & (arg-1)) == 0)); + } + +/* +* Return the index of the highest set bit +* T is an unsigned integer type +*/ +template<typename T> +inline u32bit high_bit(T n) + { + for(u32bit i = 8*sizeof(T); i > 0; --i) + if((n >> (i - 1)) & 0x01) + return i; + return 0; + } + +/* +* Return the index of the lowest set bit +*/ +template<typename T> +inline u32bit low_bit(T n) + { + for(u32bit i = 0; i != 8*sizeof(T); ++i) + if((n >> i) & 0x01) + return (i + 1); + return 0; + } + +/* +* Return the number of significant bytes in n +*/ +template<typename T> +inline u32bit significant_bytes(T n) + { + for(u32bit j = 0; j != sizeof(T); ++j) + if(get_byte(j, n)) + return sizeof(T)-j; + return 0; + } + +/* +* Return the Hamming weight of n +*/ +template<typename T> +inline u32bit hamming_weight(T n) + { + const byte NIBBLE_WEIGHTS[] = { + 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 }; + + u32bit weight = 0; + for(u32bit i = 0; i != 2*sizeof(T); ++i) + weight += NIBBLE_WEIGHTS[(n >> (4*i)) & 0x0F]; + return weight; + } + +/* +* Count the trailing zero bits in n +*/ +template<typename T> +inline u32bit ctz(T n) + { + for(u32bit i = 0; i != 8*sizeof(T); ++i) + if((n >> i) & 0x01) + return i; + return 8*sizeof(T); + } + +} + +#endif diff --git a/botan/src/utils/bswap.h b/botan/src/utils/bswap.h new file mode 100644 index 0000000..ec1e814 --- /dev/null +++ b/botan/src/utils/bswap.h @@ -0,0 +1,62 @@ +/* +* Byte Swapping Operations +* (C) 1999-2008 Jack Lloyd +* (C) 2007 Yves Jerschow +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_BYTE_SWAP_H__ +#define BOTAN_BYTE_SWAP_H__ + +#include <botan/types.h> +#include <botan/rotate.h> + +namespace Botan { + +/* +* Byte Swapping Functions +*/ +inline u16bit reverse_bytes(u16bit input) + { + return rotate_left(input, 8); + } + +inline u32bit reverse_bytes(u32bit input) + { +#if BOTAN_USE_GCC_INLINE_ASM && \ + (defined(BOTAN_TARGET_ARCH_IS_IA32) || defined(BOTAN_TARGET_ARCH_IS_AMD64)) + + /* GCC-style inline assembly for x86 or x86-64 */ + asm("bswapl %0" : "=r" (input) : "0" (input)); + return input; + +#elif defined(_MSC_VER) && defined(BOTAN_TARGET_ARCH_IS_IA32) + /* Visual C++ inline asm for 32-bit x86, by Yves Jerschow */ + __asm mov eax, input; + __asm bswap eax; + +#else + /* Generic implementation */ + input = ((input & 0xFF00FF00) >> 8) | ((input & 0x00FF00FF) << 8); + return rotate_left(input, 16); +#endif + } + +inline u64bit reverse_bytes(u64bit input) + { +#if BOTAN_USE_GCC_INLINE_ASM && defined(BOTAN_TARGET_ARCH_IS_AMD64) + asm("bswapq %0" : "=r" (input) : "0" (input)); + return input; +#else + u32bit hi = ((input >> 40) & 0x00FF00FF) | ((input >> 24) & 0xFF00FF00); + u32bit lo = ((input & 0xFF00FF00) >> 8) | ((input & 0x00FF00FF) << 8); + hi = (hi << 16) | (hi >> 16); + lo = (lo << 16) | (lo >> 16); + return (static_cast<u64bit>(lo) << 32) | hi; +#endif + } + +} + +#endif diff --git a/botan/src/utils/buf_comp/buf_comp.h b/botan/src/utils/buf_comp/buf_comp.h new file mode 100644 index 0000000..3f1e90b --- /dev/null +++ b/botan/src/utils/buf_comp/buf_comp.h @@ -0,0 +1,126 @@ +/** +* BufferedComputation +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_BUFFERED_COMPUTATION_H__ +#define BOTAN_BUFFERED_COMPUTATION_H__ + +#include <botan/secmem.h> + +namespace Botan { + +/** +* This class represents any kind of computation which +* uses an internal state, +* such as hash functions. +*/ +class BOTAN_DLL BufferedComputation + { + public: + + /** + * The length of the output of this function in bytes. + */ + const u32bit OUTPUT_LENGTH; + + /** + * Add new input to process. + * @param in the input to process as a byte array + * @param the length of the byte array + */ + void update(const byte in[], u32bit length) { add_data(in, length); } + + /** + * Add new input to process. + * @param in the input to process as a MemoryRegion + */ + void update(const MemoryRegion<byte>& in) { add_data(in, in.size()); } + + /** + * Add new input to process. + * @param str the input to process as a std::string. Will be interpreted + * as a byte array based on + * the strings encoding. + */ + void update(const std::string& str) + { + add_data(reinterpret_cast<const byte*>(str.data()), str.size()); + } + + /** + * Process a single byte. + * @param in the byte to process + */ + void update(byte in) { add_data(&in, 1); } + + /** + * Complete the computation and retrieve the + * final result. + * @param out The byte array to be filled with the result. + * Must be of length OUTPUT_LENGTH. + */ + void final(byte out[]) { final_result(out); } + + /** + * Complete the computation and retrieve the + * final result. + * @return a SecureVector holding the result + */ + SecureVector<byte> final() + { + SecureVector<byte> output(OUTPUT_LENGTH); + final_result(output); + return output; + } + + /** + * Update and finalize computation. Does the same as calling update() + * and final() consecutively. + * @param in the input to process as a byte array + * @param length the length of the byte array + * @result the result of the call to final() + */ + SecureVector<byte> process(const byte in[], u32bit length) + { + add_data(in, length); + return final(); + } + + /** + * Update and finalize computation. Does the same as calling update() + * and final() consecutively. + * @param in the input to process + * @result the result of the call to final() + */ + SecureVector<byte> process(const MemoryRegion<byte>& in) + { + add_data(in, in.size()); + return final(); + } + + /** + * Update and finalize computation. Does the same as calling update() + * and final() consecutively. + * @param in the input to process as a string + * @result the result of the call to final() + */ + SecureVector<byte> process(const std::string& in) + { + update(in); + return final(); + } + + BufferedComputation(u32bit out_len) : OUTPUT_LENGTH(out_len) {} + virtual ~BufferedComputation() {} + private: + BufferedComputation& operator=(const BufferedComputation&); + virtual void add_data(const byte[], u32bit) = 0; + virtual void final_result(byte[]) = 0; + }; + +} + +#endif diff --git a/botan/src/utils/buf_comp/info.txt b/botan/src/utils/buf_comp/info.txt new file mode 100644 index 0000000..bcbbc23 --- /dev/null +++ b/botan/src/utils/buf_comp/info.txt @@ -0,0 +1,11 @@ +realname "Buffered Computation" + +load_on auto + +<add> +buf_comp.h +</add> + +<requires> +alloc +</requires> diff --git a/botan/src/utils/charset.cpp b/botan/src/utils/charset.cpp new file mode 100644 index 0000000..53125ca --- /dev/null +++ b/botan/src/utils/charset.cpp @@ -0,0 +1,201 @@ +/* +* Character Set Handling +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/charset.h> +#include <botan/parsing.h> +#include <botan/exceptn.h> +#include <cctype> + +namespace Botan { + +namespace Charset { + +namespace { + +/* +* Convert from UCS-2 to ISO 8859-1 +*/ +std::string ucs2_to_latin1(const std::string& ucs2) + { + if(ucs2.size() % 2 == 1) + throw Decoding_Error("UCS-2 string has an odd number of bytes"); + + std::string latin1; + + for(u32bit j = 0; j != ucs2.size(); j += 2) + { + const byte c1 = ucs2[j]; + const byte c2 = ucs2[j+1]; + + if(c1 != 0) + throw Decoding_Error("UCS-2 has non-Latin1 characters"); + + latin1 += static_cast<char>(c2); + } + + return latin1; + } + +/* +* Convert from UTF-8 to ISO 8859-1 +*/ +std::string utf8_to_latin1(const std::string& utf8) + { + std::string iso8859; + + u32bit position = 0; + while(position != utf8.size()) + { + const byte c1 = static_cast<byte>(utf8[position++]); + + if(c1 <= 0x7F) + iso8859 += static_cast<char>(c1); + else if(c1 >= 0xC0 && c1 <= 0xC7) + { + if(position == utf8.size()) + throw Decoding_Error("UTF-8: sequence truncated"); + + const byte c2 = static_cast<byte>(utf8[position++]); + const byte iso_char = ((c1 & 0x07) << 6) | (c2 & 0x3F); + + if(iso_char <= 0x7F) + throw Decoding_Error("UTF-8: sequence longer than needed"); + + iso8859 += static_cast<char>(iso_char); + } + else + throw Decoding_Error("UTF-8: Unicode chars not in Latin1 used"); + } + + return iso8859; + } + +/* +* Convert from ISO 8859-1 to UTF-8 +*/ +std::string latin1_to_utf8(const std::string& iso8859) + { + std::string utf8; + for(u32bit j = 0; j != iso8859.size(); ++j) + { + const byte c = static_cast<byte>(iso8859[j]); + + if(c <= 0x7F) + utf8 += static_cast<char>(c); + else + { + utf8 += static_cast<char>((0xC0 | (c >> 6))); + utf8 += static_cast<char>((0x80 | (c & 0x3F))); + } + } + return utf8; + } + +} + +/* +* Perform character set transcoding +*/ +std::string transcode(const std::string& str, + Character_Set to, Character_Set from) + { + if(to == LOCAL_CHARSET) + to = LATIN1_CHARSET; + if(from == LOCAL_CHARSET) + from = LATIN1_CHARSET; + + if(to == from) + return str; + + if(from == LATIN1_CHARSET && to == UTF8_CHARSET) + return latin1_to_utf8(str); + if(from == UTF8_CHARSET && to == LATIN1_CHARSET) + return utf8_to_latin1(str); + if(from == UCS2_CHARSET && to == LATIN1_CHARSET) + return ucs2_to_latin1(str); + + throw Invalid_Argument("Unknown transcoding operation from " + + to_string(from) + " to " + to_string(to)); + } + +/* +* Check if a character represents a digit +*/ +bool is_digit(char c) + { + if(c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || + c == '5' || c == '6' || c == '7' || c == '8' || c == '9') + return true; + return false; + } + +/* +* Check if a character represents whitespace +*/ +bool is_space(char c) + { + if(c == ' ' || c == '\t' || c == '\n' || c == '\r') + return true; + return false; + } + +/* +* Convert a character to a digit +*/ +byte char2digit(char c) + { + switch(c) + { + case '0': return 0; + case '1': return 1; + case '2': return 2; + case '3': return 3; + case '4': return 4; + case '5': return 5; + case '6': return 6; + case '7': return 7; + case '8': return 8; + case '9': return 9; + } + + throw Invalid_Argument("char2digit: Input is not a digit character"); + } + +/* +* Convert a digit to a character +*/ +char digit2char(byte b) + { + switch(b) + { + case 0: return '0'; + case 1: return '1'; + case 2: return '2'; + case 3: return '3'; + case 4: return '4'; + case 5: return '5'; + case 6: return '6'; + case 7: return '7'; + case 8: return '8'; + case 9: return '9'; + } + + throw Invalid_Argument("digit2char: Input is not a digit"); + } + +/* +* Case-insensitive character comparison +*/ +bool caseless_cmp(char a, char b) + { + return (std::tolower(static_cast<unsigned char>(a)) == + std::tolower(static_cast<unsigned char>(b))); + } + +} + +} diff --git a/botan/src/utils/charset.h b/botan/src/utils/charset.h new file mode 100644 index 0000000..eebb199 --- /dev/null +++ b/botan/src/utils/charset.h @@ -0,0 +1,44 @@ +/* +* Character Set Handling +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_CHARSET_H__ +#define BOTAN_CHARSET_H__ + +#include <botan/types.h> +#include <string> + +namespace Botan { + +/** +* The different charsets (nominally) supported by Botan. +*/ +enum Character_Set { + LOCAL_CHARSET, + UCS2_CHARSET, + UTF8_CHARSET, + LATIN1_CHARSET +}; + +namespace Charset { + +/* +* Character Set Handling +*/ +std::string transcode(const std::string&, Character_Set, Character_Set); + +bool is_digit(char); +bool is_space(char); +bool caseless_cmp(char, char); + +byte char2digit(char); +char digit2char(byte); + +} + +} + +#endif diff --git a/botan/src/utils/datastor/datastor.cpp b/botan/src/utils/datastor/datastor.cpp new file mode 100644 index 0000000..129dad9 --- /dev/null +++ b/botan/src/utils/datastor/datastor.cpp @@ -0,0 +1,172 @@ +/* +* Data Store +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/datastor.h> +#include <botan/exceptn.h> +#include <botan/parsing.h> +#include <botan/stl_util.h> +#include <botan/filters.h> + +namespace Botan { + +/* +* Default Matcher transform operation (identity) +*/ +std::pair<std::string, std::string> +Data_Store::Matcher::transform(const std::string& key, + const std::string& value) const + { + return std::make_pair(key, value); + } + +/* +* Data_Store Equality Comparison +*/ +bool Data_Store::operator==(const Data_Store& other) const + { + return (contents == other.contents); + } + +/* +* Check if this key has at least one value +*/ +bool Data_Store::has_value(const std::string& key) const + { + return (contents.lower_bound(key) != contents.end()); + } + +/* +* Search based on an arbitrary predicate +*/ +std::multimap<std::string, std::string> +Data_Store::search_with(const Matcher& matcher) const + { + std::multimap<std::string, std::string> out; + + std::multimap<std::string, std::string>::const_iterator i = + contents.begin(); + + while(i != contents.end()) + { + if(matcher(i->first, i->second)) + out.insert(matcher.transform(i->first, i->second)); + ++i; + } + + return out; + } + +/* +* Search based on key equality +*/ +std::vector<std::string> Data_Store::get(const std::string& looking_for) const + { + typedef std::multimap<std::string, std::string>::const_iterator iter; + + std::pair<iter, iter> range = contents.equal_range(looking_for); + + std::vector<std::string> out; + for(iter i = range.first; i != range.second; ++i) + out.push_back(i->second); + return out; + } + +/* +* Get a single atom +*/ +std::string Data_Store::get1(const std::string& key) const + { + std::vector<std::string> vals = get(key); + + if(vals.empty()) + throw Invalid_State("Data_Store::get1: Not values for " + key); + if(vals.size() > 1) + throw Invalid_State("Data_Store::get1: More than one value for " + key); + + return vals[0]; + } + +/* +* Get a single MemoryVector atom +*/ +MemoryVector<byte> +Data_Store::get1_memvec(const std::string& key) const + { + std::vector<std::string> vals = get(key); + + if(vals.size() > 1) + throw Invalid_State("Data_Store::get1_memvec: Multiple values for " + + key); + + if(vals.empty()) + return MemoryVector<byte>(); + + Pipe pipe(new Hex_Decoder(FULL_CHECK)); + pipe.start_msg(); + if(vals.size()) + pipe.write(vals[0]); + pipe.end_msg(); + return pipe.read_all(); + } + +/* +* Get a single u32bit atom +*/ +u32bit Data_Store::get1_u32bit(const std::string& key, + u32bit default_val) const + { + std::vector<std::string> vals = get(key); + + if(vals.empty()) + return default_val; + else if(vals.size() > 1) + throw Invalid_State("Data_Store::get1_u32bit: Multiple values for " + + key); + + return to_u32bit(vals[0]); + } + +/* +* Insert a single key and value +*/ +void Data_Store::add(const std::string& key, const std::string& val) + { + multimap_insert(contents, key, val); + } + +/* +* Insert a single key and value +*/ +void Data_Store::add(const std::string& key, u32bit val) + { + add(key, to_string(val)); + } + +/* +* Insert a single key and value +*/ +void Data_Store::add(const std::string& key, const MemoryRegion<byte>& val) + { + Pipe pipe(new Hex_Encoder); + pipe.process_msg(val); + add(key, pipe.read_all_as_string()); + } + +/* +* Insert a mapping of key/value pairs +*/ +void Data_Store::add(const std::multimap<std::string, std::string>& in) + { + std::multimap<std::string, std::string>::const_iterator i = in.begin(); + while(i != in.end()) + { + contents.insert(*i); + ++i; + } + } + +} diff --git a/botan/src/utils/datastor/datastor.h b/botan/src/utils/datastor/datastor.h new file mode 100644 index 0000000..7ee626f --- /dev/null +++ b/botan/src/utils/datastor/datastor.h @@ -0,0 +1,61 @@ +/* +* Data Store +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_DATA_STORE_H__ +#define BOTAN_DATA_STORE_H__ + +#include <botan/secmem.h> +#include <utility> +#include <string> +#include <vector> +#include <map> + +namespace Botan { + +/** +* Data Store +*/ +class BOTAN_DLL Data_Store + { + public: + class BOTAN_DLL Matcher + { + public: + virtual bool operator()(const std::string&, + const std::string&) const = 0; + + virtual std::pair<std::string, std::string> + transform(const std::string&, const std::string&) const; + + virtual ~Matcher() {} + }; + + bool operator==(const Data_Store&) const; + + std::multimap<std::string, std::string> + search_with(const Matcher&) const; + + std::vector<std::string> get(const std::string&) const; + + std::string get1(const std::string&) const; + + MemoryVector<byte> get1_memvec(const std::string&) const; + u32bit get1_u32bit(const std::string&, u32bit = 0) const; + + bool has_value(const std::string&) const; + + void add(const std::multimap<std::string, std::string>&); + void add(const std::string&, const std::string&); + void add(const std::string&, u32bit); + void add(const std::string&, const MemoryRegion<byte>&); + private: + std::multimap<std::string, std::string> contents; + }; + +} + +#endif diff --git a/botan/src/utils/datastor/info.txt b/botan/src/utils/datastor/info.txt new file mode 100644 index 0000000..8c38a3a --- /dev/null +++ b/botan/src/utils/datastor/info.txt @@ -0,0 +1,13 @@ +realname "Datastore" + +load_on auto + +<add> +datastor.cpp +datastor.h +</add> + +<requires> +alloc +filters +</requires> diff --git a/botan/src/utils/exceptn.cpp b/botan/src/utils/exceptn.cpp new file mode 100644 index 0000000..753d634 --- /dev/null +++ b/botan/src/utils/exceptn.cpp @@ -0,0 +1,62 @@ +/* +* Exceptions +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/exceptn.h> +#include <botan/parsing.h> + +namespace Botan { + +/* +* Constructor for Invalid_Key_Length +*/ +Invalid_Key_Length::Invalid_Key_Length(const std::string& name, u32bit length) + { + set_msg(name + " cannot accept a key of length " + to_string(length)); + } + +/* +* Constructor for Invalid_Block_Size +*/ +Invalid_Block_Size::Invalid_Block_Size(const std::string& mode, + const std::string& pad) + { + set_msg("Padding method " + pad + " cannot be used with " + mode); + } + +/* +* Constructor for Invalid_IV_Length +*/ +Invalid_IV_Length::Invalid_IV_Length(const std::string& mode, u32bit bad_len) + { + set_msg("IV length " + to_string(bad_len) + " is invalid for " + mode); + } + +/* +* Constructor for Algorithm_Not_Found +*/ +Algorithm_Not_Found::Algorithm_Not_Found(const std::string& name) + { + set_msg("Could not find any algorithm named \"" + name + "\""); + } + +/* +* Constructor for Invalid_Algorithm_Name +*/ +Invalid_Algorithm_Name::Invalid_Algorithm_Name(const std::string& name) + { + set_msg("Invalid algorithm name: " + name); + } + +/* +* Constructor for Config_Error +*/ +Config_Error::Config_Error(const std::string& err, u32bit line) + { + set_msg("Config error at line " + to_string(line) + ": " + err); + } + +} diff --git a/botan/src/utils/exceptn.h b/botan/src/utils/exceptn.h new file mode 100644 index 0000000..a55d842 --- /dev/null +++ b/botan/src/utils/exceptn.h @@ -0,0 +1,197 @@ +/* +* Exceptions +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_EXCEPTION_H__ +#define BOTAN_EXCEPTION_H__ + +#include <botan/types.h> +#include <exception> +#include <string> + +namespace Botan { + +/* +* Exception Base Class +*/ +class BOTAN_DLL Exception : public std::exception + { + public: + const char* what() const throw() { return msg.c_str(); } + Exception(const std::string& m = "Unknown error") { set_msg(m); } + virtual ~Exception() throw() {} + protected: + void set_msg(const std::string& m) { msg = "Botan: " + m; } + private: + std::string msg; + }; + +/* +* Invalid_Argument Exception +*/ +struct BOTAN_DLL Invalid_Argument : public Exception + { + Invalid_Argument(const std::string& err = "") : Exception(err) {} + }; + +/* +* Invalid_Key_Length Exception +*/ +struct BOTAN_DLL Invalid_Key_Length : public Invalid_Argument + { + Invalid_Key_Length(const std::string&, u32bit); + }; + +/* +* Invalid_Block_Size Exception +*/ +struct BOTAN_DLL Invalid_Block_Size : public Invalid_Argument + { + Invalid_Block_Size(const std::string&, const std::string&); + }; + +/* +* Invalid_IV_Length Exception +*/ +struct BOTAN_DLL Invalid_IV_Length : public Invalid_Argument + { + Invalid_IV_Length(const std::string&, u32bit); + }; + +/* +* Invalid_State Exception +*/ +struct BOTAN_DLL Invalid_State : public Exception + { + Invalid_State(const std::string& err) : Exception(err) {} + }; + +/* +* PRNG_Unseeded Exception +*/ +struct BOTAN_DLL PRNG_Unseeded : public Invalid_State + { + PRNG_Unseeded(const std::string& algo) : + Invalid_State("PRNG not seeded: " + algo) {} + }; + +/* +* Policy_Violation Exception +*/ +struct BOTAN_DLL Policy_Violation : public Invalid_State + { + Policy_Violation(const std::string& err) : + Invalid_State("Policy violation: " + err) {} + }; + +/* +* Lookup_Error Exception +*/ +struct BOTAN_DLL Lookup_Error : public Exception + { + Lookup_Error(const std::string& err) : Exception(err) {} + }; + +/* +* Algorithm_Not_Found Exception +*/ +struct BOTAN_DLL Algorithm_Not_Found : public Exception + { + Algorithm_Not_Found(const std::string&); + }; + +/* +* Format_Error Exception +*/ +struct BOTAN_DLL Format_Error : public Exception + { + Format_Error(const std::string& err = "") : Exception(err) {} + }; + +/* +* Invalid_Algorithm_Name Exception +*/ +struct BOTAN_DLL Invalid_Algorithm_Name : public Format_Error + { + Invalid_Algorithm_Name(const std::string&); + }; + +/* +* Encoding_Error Exception +*/ +struct BOTAN_DLL Encoding_Error : public Format_Error + { + Encoding_Error(const std::string& name) : + Format_Error("Encoding error: " + name) {} + }; + +/* +* Decoding_Error Exception +*/ +struct BOTAN_DLL Decoding_Error : public Format_Error + { + Decoding_Error(const std::string& name) : + Format_Error("Decoding error: " + name) {} + }; + +/* +* Invalid_OID Exception +*/ +struct BOTAN_DLL Invalid_OID : public Decoding_Error + { + Invalid_OID(const std::string& oid) : + Decoding_Error("Invalid ASN.1 OID: " + oid) {} + }; + +/* +* Stream_IO_Error Exception +*/ +struct BOTAN_DLL Stream_IO_Error : public Exception + { + Stream_IO_Error(const std::string& err) : + Exception("I/O error: " + err) {} + }; + +/* +* Configuration Error Exception +*/ +struct BOTAN_DLL Config_Error : public Format_Error + { + Config_Error(const std::string& err) : + Format_Error("Config error: " + err) {} + Config_Error(const std::string&, u32bit); + }; + +/* +* Integrity Failure Exception +*/ +struct BOTAN_DLL Integrity_Failure : public Exception + { + Integrity_Failure(const std::string& err) : + Exception("Integrity failure: " + err) {} + }; + +/* +* Internal_Error Exception +*/ +struct BOTAN_DLL Internal_Error : public Exception + { + Internal_Error(const std::string& err) : + Exception("Internal error: " + err) {} + }; + +/* +* Self Test Failure Exception +*/ +struct BOTAN_DLL Self_Test_Failure : public Internal_Error + { + Self_Test_Failure(const std::string& err) : + Internal_Error("Self test failed: " + err) {} + }; + +} + +#endif diff --git a/botan/src/utils/info.txt b/botan/src/utils/info.txt new file mode 100644 index 0000000..ab50b88 --- /dev/null +++ b/botan/src/utils/info.txt @@ -0,0 +1,33 @@ +realname "Utility Functions" + +define UTIL_FUNCTIONS + +load_on always + +<libs> +tru64 -> rt +</libs> + +<add> +bit_ops.h +bswap.h +charset.cpp +charset.h +exceptn.cpp +exceptn.h +loadstor.h +mem_ops.h +mlock.cpp +parsing.cpp +parsing.h +rotate.h +stl_util.h +types.h +ui.cpp +ui.h +util.cpp +util.h +version.cpp +version.h +xor_buf.h +</add> diff --git a/botan/src/utils/loadstor.h b/botan/src/utils/loadstor.h new file mode 100644 index 0000000..77ed155 --- /dev/null +++ b/botan/src/utils/loadstor.h @@ -0,0 +1,281 @@ +/* +* Load/Store Operators +* (C) 1999-2007 Jack Lloyd +* 2007 Yves Jerschow +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_LOAD_STORE_H__ +#define BOTAN_LOAD_STORE_H__ + +#include <botan/types.h> +#include <botan/bswap.h> +#include <botan/rotate.h> + +#if BOTAN_TARGET_UNALIGNED_LOADSTOR_OK + +#if defined(BOTAN_TARGET_CPU_IS_BIG_ENDIAN) + +#define BOTAN_ENDIAN_N2B(x) (x) +#define BOTAN_ENDIAN_B2N(x) (x) + +#define BOTAN_ENDIAN_N2L(x) reverse_bytes(x) +#define BOTAN_ENDIAN_L2N(x) reverse_bytes(x) + +#elif defined(BOTAN_TARGET_CPU_IS_LITTLE_ENDIAN) + +#define BOTAN_ENDIAN_N2L(x) (x) +#define BOTAN_ENDIAN_L2N(x) (x) + +#define BOTAN_ENDIAN_N2B(x) reverse_bytes(x) +#define BOTAN_ENDIAN_B2N(x) reverse_bytes(x) + +#endif + +#endif + +namespace Botan { + +/* +* Byte Extraction Function +*/ +template<typename T> inline byte get_byte(u32bit byte_num, T input) + { + return (input >> ((sizeof(T)-1-(byte_num&(sizeof(T)-1))) << 3)); + } + +/* +* Byte to Word Conversions +*/ +inline u16bit make_u16bit(byte i0, byte i1) + { + return ((static_cast<u16bit>(i0) << 8) | i1); + } + +inline u32bit make_u32bit(byte i0, byte i1, byte i2, byte i3) + { + return ((static_cast<u32bit>(i0) << 24) | + (static_cast<u32bit>(i1) << 16) | + (static_cast<u32bit>(i2) << 8) | + (static_cast<u32bit>(i3))); + } + +inline u64bit make_u64bit(byte i0, byte i1, byte i2, byte i3, + byte i4, byte i5, byte i6, byte i7) + { + return ((static_cast<u64bit>(i0) << 56) | + (static_cast<u64bit>(i1) << 48) | + (static_cast<u64bit>(i2) << 40) | + (static_cast<u64bit>(i3) << 32) | + (static_cast<u64bit>(i4) << 24) | + (static_cast<u64bit>(i5) << 16) | + (static_cast<u64bit>(i6) << 8) | + (static_cast<u64bit>(i7))); + } + +/* +* Endian-Specific Word Loading Operations +*/ +template<typename T> +inline T load_be(const byte in[], u32bit off) + { + in += off * sizeof(T); + T out = 0; + for(u32bit j = 0; j != sizeof(T); j++) + out = (out << 8) | in[j]; + return out; + } + +template<typename T> +inline T load_le(const byte in[], u32bit off) + { + in += off * sizeof(T); + T out = 0; + for(u32bit j = 0; j != sizeof(T); j++) + out = (out << 8) | in[sizeof(T)-1-j]; + return out; + } + +template<> +inline u16bit load_be<u16bit>(const byte in[], u32bit off) + { +#if BOTAN_TARGET_UNALIGNED_LOADSTOR_OK + return BOTAN_ENDIAN_N2B(*(reinterpret_cast<const u16bit*>(in) + off)); +#else + in += off * sizeof(u16bit); + return make_u16bit(in[0], in[1]); +#endif + } + +template<> +inline u16bit load_le<u16bit>(const byte in[], u32bit off) + { +#if BOTAN_TARGET_UNALIGNED_LOADSTOR_OK + return BOTAN_ENDIAN_N2L(*(reinterpret_cast<const u16bit*>(in) + off)); +#else + in += off * sizeof(u16bit); + return make_u16bit(in[1], in[0]); +#endif + } + +template<> +inline u32bit load_be<u32bit>(const byte in[], u32bit off) + { +#if BOTAN_TARGET_UNALIGNED_LOADSTOR_OK + return BOTAN_ENDIAN_N2B(*(reinterpret_cast<const u32bit*>(in) + off)); +#else + in += off * sizeof(u32bit); + return make_u32bit(in[0], in[1], in[2], in[3]); +#endif + } + +template<> +inline u32bit load_le<u32bit>(const byte in[], u32bit off) + { +#if BOTAN_TARGET_UNALIGNED_LOADSTOR_OK + return BOTAN_ENDIAN_N2L(*(reinterpret_cast<const u32bit*>(in) + off)); +#else + in += off * sizeof(u32bit); + return make_u32bit(in[3], in[2], in[1], in[0]); +#endif + } + +template<> +inline u64bit load_be<u64bit>(const byte in[], u32bit off) + { +#if BOTAN_TARGET_UNALIGNED_LOADSTOR_OK + return BOTAN_ENDIAN_N2B(*(reinterpret_cast<const u64bit*>(in) + off)); +#else + in += off * sizeof(u64bit); + return make_u64bit(in[0], in[1], in[2], in[3], + in[4], in[5], in[6], in[7]); +#endif + } + +template<> +inline u64bit load_le<u64bit>(const byte in[], u32bit off) + { +#if BOTAN_TARGET_UNALIGNED_LOADSTOR_OK + return BOTAN_ENDIAN_N2L(*(reinterpret_cast<const u64bit*>(in) + off)); +#else + in += off * sizeof(u64bit); + return make_u64bit(in[7], in[6], in[5], in[4], + in[3], in[2], in[1], in[0]); +#endif + } + +/* +* Endian-Specific Word Storing Operations +*/ +inline void store_be(u16bit in, byte out[2]) + { +#if BOTAN_TARGET_UNALIGNED_LOADSTOR_OK + *reinterpret_cast<u16bit*>(out) = BOTAN_ENDIAN_B2N(in); +#else + out[0] = get_byte(0, in); + out[1] = get_byte(1, in); +#endif + } + +inline void store_le(u16bit in, byte out[2]) + { +#if BOTAN_TARGET_UNALIGNED_LOADSTOR_OK + *reinterpret_cast<u16bit*>(out) = BOTAN_ENDIAN_L2N(in); +#else + out[0] = get_byte(1, in); + out[1] = get_byte(0, in); +#endif + } + +inline void store_be(u32bit in, byte out[4]) + { +#if BOTAN_TARGET_UNALIGNED_LOADSTOR_OK + *reinterpret_cast<u32bit*>(out) = BOTAN_ENDIAN_B2N(in); +#else + out[0] = get_byte(0, in); + out[1] = get_byte(1, in); + out[2] = get_byte(2, in); + out[3] = get_byte(3, in); +#endif + } + +inline void store_le(u32bit in, byte out[4]) + { +#if BOTAN_TARGET_UNALIGNED_LOADSTOR_OK + *reinterpret_cast<u32bit*>(out) = BOTAN_ENDIAN_L2N(in); +#else + out[0] = get_byte(3, in); + out[1] = get_byte(2, in); + out[2] = get_byte(1, in); + out[3] = get_byte(0, in); +#endif + } + +inline void store_be(u64bit in, byte out[8]) + { +#if BOTAN_TARGET_UNALIGNED_LOADSTOR_OK + *reinterpret_cast<u64bit*>(out) = BOTAN_ENDIAN_B2N(in); +#else + out[0] = get_byte(0, in); + out[1] = get_byte(1, in); + out[2] = get_byte(2, in); + out[3] = get_byte(3, in); + out[4] = get_byte(4, in); + out[5] = get_byte(5, in); + out[6] = get_byte(6, in); + out[7] = get_byte(7, in); +#endif + } + +inline void store_le(u64bit in, byte out[8]) + { +#if BOTAN_TARGET_UNALIGNED_LOADSTOR_OK + *reinterpret_cast<u64bit*>(out) = BOTAN_ENDIAN_L2N(in); +#else + out[0] = get_byte(7, in); + out[1] = get_byte(6, in); + out[2] = get_byte(5, in); + out[3] = get_byte(4, in); + out[4] = get_byte(3, in); + out[5] = get_byte(2, in); + out[6] = get_byte(1, in); + out[7] = get_byte(0, in); +#endif + } + +template<typename T> +inline void store_le(byte out[], T a, T b) + { + store_le(a, out + (0 * sizeof(T))); + store_le(b, out + (1 * sizeof(T))); + } + +template<typename T> +inline void store_be(byte out[], T a, T b) + { + store_be(a, out + (0 * sizeof(T))); + store_be(b, out + (1 * sizeof(T))); + } + +template<typename T> +inline void store_le(byte out[], T a, T b, T c, T d) + { + store_le(a, out + (0 * sizeof(T))); + store_le(b, out + (1 * sizeof(T))); + store_le(c, out + (2 * sizeof(T))); + store_le(d, out + (3 * sizeof(T))); + } + +template<typename T> +inline void store_be(byte out[], T a, T b, T c, T d) + { + store_be(a, out + (0 * sizeof(T))); + store_be(b, out + (1 * sizeof(T))); + store_be(c, out + (2 * sizeof(T))); + store_be(d, out + (3 * sizeof(T))); + } + +} + +#endif diff --git a/botan/src/utils/mem_ops.h b/botan/src/utils/mem_ops.h new file mode 100644 index 0000000..0fcf34b --- /dev/null +++ b/botan/src/utils/mem_ops.h @@ -0,0 +1,40 @@ +/* +* Memory Operations +* (C) 1999-2009 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_MEMORY_OPS_H__ +#define BOTAN_MEMORY_OPS_H__ + +#include <botan/types.h> +#include <cstring> + +namespace Botan { + +/* +* Memory Manipulation Functions +*/ +template<typename T> inline void copy_mem(T* out, const T* in, u32bit n) + { std::memmove(out, in, sizeof(T)*n); } + +template<typename T> inline void clear_mem(T* ptr, u32bit n) + { if(n) std::memset(ptr, 0, sizeof(T)*n); } + +template<typename T> inline void set_mem(T* ptr, u32bit n, byte val) + { std::memset(ptr, val, sizeof(T)*n); } + +template<typename T> inline bool same_mem(const T* p1, const T* p2, u32bit n) + { + bool is_same = true; + + for(u32bit i = 0; i != n; ++i) + is_same &= (p1[i] == p2[i]); + + return is_same; + } + +} + +#endif diff --git a/botan/src/utils/mlock.cpp b/botan/src/utils/mlock.cpp new file mode 100644 index 0000000..ea0da26 --- /dev/null +++ b/botan/src/utils/mlock.cpp @@ -0,0 +1,55 @@ +/* +* Memory Locking Functions +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/util.h> + +#if defined(BOTAN_TARGET_OS_HAS_POSIX_MLOCK) + #include <sys/types.h> + #include <sys/mman.h> +#elif defined(BOTAN_TARGET_OS_HAS_WIN32_VIRTUAL_LOCK) + #include <windows.h> +#endif + +namespace Botan { + +/* +* Lock an area of memory into RAM +*/ +#if defined(BOTAN_TARGET_OS_HAS_POSIX_MLOCK) \ + || defined(BOTAN_TARGET_OS_HAS_WIN32_VIRTUAL_LOCK) +bool lock_mem(void* ptr, u32bit bytes) +#else +bool lock_mem(void*, u32bit) +#endif + { +#if defined(BOTAN_TARGET_OS_HAS_POSIX_MLOCK) + return (mlock(ptr, bytes) == 0); +#elif defined(BOTAN_TARGET_OS_HAS_WIN32_VIRTUAL_LOCK) + return (VirtualLock(ptr, bytes) != 0); +#else + return false; +#endif + } + +/* +* Unlock a previously locked region of memory +*/ +#if defined(BOTAN_TARGET_OS_HAS_POSIX_MLOCK) \ + || defined(BOTAN_TARGET_OS_HAS_WIN32_VIRTUAL_LOCK) +void unlock_mem(void* ptr, u32bit bytes) +#else +void unlock_mem(void*, u32bit) +#endif + { +#if defined(BOTAN_TARGET_OS_HAS_POSIX_MLOCK) + munlock(ptr, bytes); +#elif defined(BOTAN_TARGET_OS_HAS_WIN32_VIRTUAL_LOCK) + VirtualUnlock(ptr, bytes); +#endif + } + +} diff --git a/botan/src/utils/parsing.cpp b/botan/src/utils/parsing.cpp new file mode 100644 index 0000000..bdb9e79 --- /dev/null +++ b/botan/src/utils/parsing.cpp @@ -0,0 +1,288 @@ +/* +* Parser Functions +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/parsing.h> +#include <botan/exceptn.h> +#include <botan/charset.h> +#include <botan/loadstor.h> + +namespace Botan { + +/* +* Convert a string into an integer +*/ +u32bit to_u32bit(const std::string& number) + { + u32bit n = 0; + + for(std::string::const_iterator j = number.begin(); j != number.end(); ++j) + { + const u32bit OVERFLOW_MARK = 0xFFFFFFFF / 10; + + byte digit = Charset::char2digit(*j); + + if((n > OVERFLOW_MARK) || (n == OVERFLOW_MARK && digit > 5)) + throw Decoding_Error("to_u32bit: Integer overflow"); + n *= 10; + n += digit; + } + return n; + } + +/* +* Convert an integer into a string +*/ +std::string to_string(u64bit n, u32bit min_len) + { + std::string lenstr; + if(n) + { + while(n > 0) + { + lenstr = Charset::digit2char(n % 10) + lenstr; + n /= 10; + } + } + else + lenstr = "0"; + + while(lenstr.size() < min_len) + lenstr = "0" + lenstr; + + return lenstr; + } + +/* +* Convert a string into a time duration +*/ +u32bit timespec_to_u32bit(const std::string& timespec) + { + if(timespec == "") + return 0; + + const char suffix = timespec[timespec.size()-1]; + std::string value = timespec.substr(0, timespec.size()-1); + + u32bit scale = 1; + + if(Charset::is_digit(suffix)) + value += suffix; + else if(suffix == 's') + scale = 1; + else if(suffix == 'm') + scale = 60; + else if(suffix == 'h') + scale = 60 * 60; + else if(suffix == 'd') + scale = 24 * 60 * 60; + else if(suffix == 'y') + scale = 365 * 24 * 60 * 60; + else + throw Decoding_Error("timespec_to_u32bit: Bad input " + timespec); + + return scale * to_u32bit(value); + } + +/* +* Parse a SCAN-style algorithm name +*/ +std::vector<std::string> parse_algorithm_name(const std::string& namex) + { + if(namex.find('(') == std::string::npos && + namex.find(')') == std::string::npos) + return std::vector<std::string>(1, namex); + + std::string name = namex, substring; + std::vector<std::string> elems; + u32bit level = 0; + + elems.push_back(name.substr(0, name.find('('))); + name = name.substr(name.find('(')); + + for(std::string::const_iterator j = name.begin(); j != name.end(); ++j) + { + char c = *j; + + if(c == '(') + ++level; + if(c == ')') + { + if(level == 1 && j == name.end() - 1) + { + if(elems.size() == 1) + elems.push_back(substring.substr(1)); + else + elems.push_back(substring); + return elems; + } + + if(level == 0 || (level == 1 && j != name.end() - 1)) + throw Invalid_Algorithm_Name(namex); + --level; + } + + if(c == ',' && level == 1) + { + if(elems.size() == 1) + elems.push_back(substring.substr(1)); + else + elems.push_back(substring); + substring.clear(); + } + else + substring += c; + } + + if(substring != "") + throw Invalid_Algorithm_Name(namex); + + return elems; + } + +/* +* Split the string on slashes +*/ +std::vector<std::string> split_on(const std::string& str, char delim) + { + std::vector<std::string> elems; + if(str == "") return elems; + + std::string substr; + for(std::string::const_iterator j = str.begin(); j != str.end(); ++j) + { + if(*j == delim) + { + if(substr != "") + elems.push_back(substr); + substr.clear(); + } + else + substr += *j; + } + + if(substr == "") + throw Format_Error("Unable to split string: " + str); + elems.push_back(substr); + + return elems; + } + +/* +* Parse an ASN.1 OID string +*/ +std::vector<u32bit> parse_asn1_oid(const std::string& oid) + { + std::string substring; + std::vector<u32bit> oid_elems; + + for(std::string::const_iterator j = oid.begin(); j != oid.end(); ++j) + { + char c = *j; + + if(c == '.') + { + if(substring == "") + throw Invalid_OID(oid); + oid_elems.push_back(to_u32bit(substring)); + substring.clear(); + } + else + substring += c; + } + + if(substring == "") + throw Invalid_OID(oid); + oid_elems.push_back(to_u32bit(substring)); + + if(oid_elems.size() < 2) + throw Invalid_OID(oid); + + return oid_elems; + } + +/* +* X.500 String Comparison +*/ +bool x500_name_cmp(const std::string& name1, const std::string& name2) + { + std::string::const_iterator p1 = name1.begin(); + std::string::const_iterator p2 = name2.begin(); + + while((p1 != name1.end()) && Charset::is_space(*p1)) ++p1; + while((p2 != name2.end()) && Charset::is_space(*p2)) ++p2; + + while(p1 != name1.end() && p2 != name2.end()) + { + if(Charset::is_space(*p1)) + { + if(!Charset::is_space(*p2)) + return false; + + while((p1 != name1.end()) && Charset::is_space(*p1)) ++p1; + while((p2 != name2.end()) && Charset::is_space(*p2)) ++p2; + + if(p1 == name1.end() && p2 == name2.end()) + return true; + } + + if(!Charset::caseless_cmp(*p1, *p2)) + return false; + ++p1; + ++p2; + } + + while((p1 != name1.end()) && Charset::is_space(*p1)) ++p1; + while((p2 != name2.end()) && Charset::is_space(*p2)) ++p2; + + if((p1 != name1.end()) || (p2 != name2.end())) + return false; + return true; + } + +/* +* Convert a decimal-dotted string to binary IP +*/ +u32bit string_to_ipv4(const std::string& str) + { + std::vector<std::string> parts = split_on(str, '.'); + + if(parts.size() != 4) + throw Decoding_Error("Invalid IP string " + str); + + u32bit ip = 0; + + for(size_t j = 0; j != parts.size(); j++) + { + u32bit octet = to_u32bit(parts[j]); + + if(octet > 255) + throw Decoding_Error("Invalid IP string " + str); + + ip = (ip << 8) | (octet & 0xFF); + } + + return ip; + } + +/* +* Convert an IP address to decimal-dotted string +*/ +std::string ipv4_to_string(u32bit ip) + { + std::string str; + + for(size_t j = 0; j != sizeof(ip); j++) + { + if(j) + str += "."; + str += to_string(get_byte(j, ip)); + } + + return str; + } + +} diff --git a/botan/src/utils/parsing.h b/botan/src/utils/parsing.h new file mode 100644 index 0000000..2c29d5b --- /dev/null +++ b/botan/src/utils/parsing.h @@ -0,0 +1,41 @@ +/* +* Parser Functions +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_PARSER_H__ +#define BOTAN_PARSER_H__ + +#include <botan/types.h> +#include <string> +#include <vector> + +namespace Botan { + +/* +* String Parsing Functions +*/ +BOTAN_DLL std::vector<std::string> parse_algorithm_name(const std::string&); +BOTAN_DLL std::vector<std::string> split_on(const std::string&, char); +BOTAN_DLL std::vector<u32bit> parse_asn1_oid(const std::string&); +BOTAN_DLL bool x500_name_cmp(const std::string&, const std::string&); + +/* +* String/Integer Conversions +*/ +BOTAN_DLL std::string to_string(u64bit, u32bit = 0); +BOTAN_DLL u32bit to_u32bit(const std::string&); + +BOTAN_DLL u32bit timespec_to_u32bit(const std::string& timespec); + +/* +* String/Network Address Conversions +*/ +BOTAN_DLL u32bit string_to_ipv4(const std::string&); +BOTAN_DLL std::string ipv4_to_string(u32bit); + +} + +#endif diff --git a/botan/src/utils/rotate.h b/botan/src/utils/rotate.h new file mode 100644 index 0000000..c8f8d4a --- /dev/null +++ b/botan/src/utils/rotate.h @@ -0,0 +1,30 @@ +/* +* Word Rotation Operations +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_WORD_ROTATE_H__ +#define BOTAN_WORD_ROTATE_H__ + +#include <botan/types.h> + +namespace Botan { + +/* +* Word Rotation Functions +*/ +template<typename T> inline T rotate_left(T input, u32bit rot) + { + return static_cast<T>((input << rot) | (input >> (8*sizeof(T)-rot)));; + } + +template<typename T> inline T rotate_right(T input, u32bit rot) + { + return static_cast<T>((input >> rot) | (input << (8*sizeof(T)-rot))); + } + +} + +#endif diff --git a/botan/src/utils/stl_util.h b/botan/src/utils/stl_util.h new file mode 100644 index 0000000..18c8b14 --- /dev/null +++ b/botan/src/utils/stl_util.h @@ -0,0 +1,86 @@ +/* +* STL Utility Functions +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_STL_UTIL_H__ +#define BOTAN_STL_UTIL_H__ + +#include <map> + +namespace Botan { + +/* +* Copy-on-Predicate Algorithm +*/ +template<typename InputIterator, typename OutputIterator, typename Predicate> +OutputIterator copy_if(InputIterator current, InputIterator end, + OutputIterator dest, Predicate copy_p) + { + while(current != end) + { + if(copy_p(*current)) + *dest++ = *current; + ++current; + } + return dest; + } + +/* +* Searching through a std::map +*/ +template<typename K, typename V> +inline V search_map(const std::map<K, V>& mapping, + const K& key, + const V& null_result = V()) + { + typename std::map<K, V>::const_iterator i = mapping.find(key); + if(i == mapping.end()) + return null_result; + return i->second; + } + +template<typename K, typename V, typename R> +inline R search_map(const std::map<K, V>& mapping, const K& key, + const R& null_result, const R& found_result) + { + typename std::map<K, V>::const_iterator i = mapping.find(key); + if(i == mapping.end()) + return null_result; + return found_result; + } + +/* +* Function adaptor for delete operation +*/ +template<class T> +class del_fun : public std::unary_function<T, void> + { + public: + void operator()(T* ptr) { delete ptr; } + }; + +/* +* Delete the second half of a pair of objects +*/ +template<typename Pair> +void delete2nd(Pair& pair) + { + delete pair.second; + } + +/* +* Insert a key/value pair into a multimap +*/ +template<typename K, typename V> +void multimap_insert(std::multimap<K, V>& multimap, + const K& key, const V& value) + { + multimap.insert(std::make_pair(key, value)); + } + +} + +#endif diff --git a/botan/src/utils/types.h b/botan/src/utils/types.h new file mode 100644 index 0000000..304628d --- /dev/null +++ b/botan/src/utils/types.h @@ -0,0 +1,42 @@ +/* +* Low Level Types +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_TYPES_H__ +#define BOTAN_TYPES_H__ + +#include <botan/build.h> + +namespace Botan { + +typedef unsigned char byte; +typedef unsigned short u16bit; +typedef unsigned int u32bit; + +typedef signed int s32bit; + +#if defined(_MSC_VER) || defined(__BORLANDC__) + typedef unsigned __int64 u64bit; +#elif defined(__KCC) + typedef unsigned __long_long u64bit; +#elif defined(__GNUG__) + __extension__ typedef unsigned long long u64bit; +#else + typedef unsigned long long u64bit; +#endif + +static const u32bit DEFAULT_BUFFERSIZE = BOTAN_DEFAULT_BUFFER_SIZE; + +} + +namespace Botan_types { + +using Botan::byte; +using Botan::u32bit; + +} + +#endif diff --git a/botan/src/utils/ui.cpp b/botan/src/utils/ui.cpp new file mode 100644 index 0000000..e6c3430 --- /dev/null +++ b/botan/src/utils/ui.cpp @@ -0,0 +1,36 @@ +/* +* User Interface +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/ui.h> + +namespace Botan { + +/* +* Get a passphrase from the user +*/ +std::string User_Interface::get_passphrase(const std::string&, + const std::string&, + UI_Result& action) const + { + action = OK; + + if(!first_try) + action = CANCEL_ACTION; + + return preset_passphrase; + } + +/* +* User_Interface Constructor +*/ +User_Interface::User_Interface(const std::string& preset) : + preset_passphrase(preset) + { + first_try = true; + } + +} diff --git a/botan/src/utils/ui.h b/botan/src/utils/ui.h new file mode 100644 index 0000000..fe62c60 --- /dev/null +++ b/botan/src/utils/ui.h @@ -0,0 +1,36 @@ +/* +* User Interface +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_UI_H__ +#define BOTAN_UI_H__ + +#include <botan/build.h> +#include <string> + +namespace Botan { + +/* +* User Interface +*/ +class BOTAN_DLL User_Interface + { + public: + enum UI_Result { OK, CANCEL_ACTION }; + + virtual std::string get_passphrase(const std::string&, + const std::string&, + UI_Result&) const; + User_Interface(const std::string& = ""); + virtual ~User_Interface() {} + protected: + std::string preset_passphrase; + mutable bool first_try; + }; + +} + +#endif diff --git a/botan/src/utils/util.cpp b/botan/src/utils/util.cpp new file mode 100644 index 0000000..84dfd1a --- /dev/null +++ b/botan/src/utils/util.cpp @@ -0,0 +1,68 @@ +/* +* Utility Functions +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/util.h> +#include <algorithm> +#include <cmath> + +namespace Botan { + +/* +* Round up n to multiple of align_to +*/ +u32bit round_up(u32bit n, u32bit align_to) + { + if(n % align_to || n == 0) + n += align_to - (n % align_to); + return n; + } + +/* +* Round down n to multiple of align_to +*/ +u32bit round_down(u32bit n, u32bit align_to) + { + return (n - (n % align_to)); + } + +/* +* Choose the exponent size for a DL group +*/ +u32bit dl_work_factor(u32bit bits) + { +#if 0 + /* + These values were taken from RFC 3526 + */ + if(bits <= 1536) + return 90; + else if(bits <= 2048) + return 110; + else if(bits <= 3072) + return 130; + else if(bits <= 4096) + return 150; + else if(bits <= 6144) + return 170; + else if(bits <= 8192) + return 190; + return 256; +#else + const u32bit MIN_ESTIMATE = 64; + + const double log_x = bits / 1.44; + + const double strength = + 2.76 * std::pow(log_x, 1.0/3.0) * std::pow(std::log(log_x), 2.0/3.0); + + if(strength > MIN_ESTIMATE) + return static_cast<u32bit>(strength); + return MIN_ESTIMATE; +#endif + } + +} diff --git a/botan/src/utils/util.h b/botan/src/utils/util.h new file mode 100644 index 0000000..ac78673 --- /dev/null +++ b/botan/src/utils/util.h @@ -0,0 +1,39 @@ +/* +* Utility Functions +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_UTIL_H__ +#define BOTAN_UTIL_H__ + +#include <botan/types.h> + +namespace Botan { + +/* +* Time Access Functions +*/ +BOTAN_DLL u64bit system_time(); + +/* +* Memory Locking Functions +*/ +BOTAN_DLL bool lock_mem(void*, u32bit); +BOTAN_DLL void unlock_mem(void*, u32bit); + +/* +* Misc Utility Functions +*/ +BOTAN_DLL u32bit round_up(u32bit, u32bit); +BOTAN_DLL u32bit round_down(u32bit, u32bit); + +/* +* Work Factor Estimates +*/ +BOTAN_DLL u32bit dl_work_factor(u32bit); + +} + +#endif diff --git a/botan/src/utils/version.cpp b/botan/src/utils/version.cpp new file mode 100644 index 0000000..d540864 --- /dev/null +++ b/botan/src/utils/version.cpp @@ -0,0 +1,36 @@ +/* +* Version Information +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/version.h> +#include <botan/parsing.h> + +namespace Botan { + +/* + These are intentionally compiled rather than inlined, so an + application running against a shared library can test the true + version they are running against. +*/ + +/* +* Return the version as a string +*/ +std::string version_string() + { + return to_string(version_major()) + "." + + to_string(version_minor()) + "." + + to_string(version_patch()); + } + +/* +* Return parts of the version as integers +*/ +u32bit version_major() { return BOTAN_VERSION_MAJOR; } +u32bit version_minor() { return BOTAN_VERSION_MINOR; } +u32bit version_patch() { return BOTAN_VERSION_PATCH; } + +} diff --git a/botan/src/utils/version.h b/botan/src/utils/version.h new file mode 100644 index 0000000..3cc44e8 --- /dev/null +++ b/botan/src/utils/version.h @@ -0,0 +1,61 @@ +/* +* Version Information +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_VERSION_H__ +#define BOTAN_VERSION_H__ + +#include <botan/types.h> +#include <string> + +namespace Botan { + +/* +* Get information describing the version +*/ + +/** +* Get the version string identifying the version of Botan. +* @return the version string +*/ +BOTAN_DLL std::string version_string(); + +/** +* Get the major version number. +* @return the major version number +*/ +BOTAN_DLL u32bit version_major(); + +/** +* Get the minor version number. +* @return the minor version number +*/ +BOTAN_DLL u32bit version_minor(); + +/** +* Get the patch number. +* @return the patch number +*/ +BOTAN_DLL u32bit version_patch(); + +/* +* Macros for compile-time version checks +*/ +#define BOTAN_VERSION_CODE_FOR(a,b,c) ((a << 16) | (b << 8) | (c)) + +/** +* Compare using BOTAN_VERSION_CODE_FOR, as in +* # if BOTAN_VERSION_CODE < BOTAN_VERSION_CODE_FOR(1,8,0) +* # error "Botan version too old" +* # endif +*/ +#define BOTAN_VERSION_CODE BOTAN_VERSION_CODE_FOR(BOTAN_VERSION_MAJOR, \ + BOTAN_VERSION_MINOR, \ + BOTAN_VERSION_PATCH) + +} + +#endif diff --git a/botan/src/utils/xor_buf.h b/botan/src/utils/xor_buf.h new file mode 100644 index 0000000..39781f0 --- /dev/null +++ b/botan/src/utils/xor_buf.h @@ -0,0 +1,74 @@ +/** +* XOR operations +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_XOR_BUF_H__ +#define BOTAN_XOR_BUF_H__ + +#include <botan/types.h> + +namespace Botan { + +/** +* XOR arrays. Postcondition out[i] = in[i] ^ out[i] forall i = 0...length +* @param out the input/output buffer +* @param in the read-only input buffer +* @param length the length of the buffers +*/ +inline void xor_buf(byte out[], const byte in[], u32bit length) + { + while(length >= 8) + { +#if BOTAN_UNALIGNED_LOADSTOR_OK + *reinterpret_cast<u64bit*>(out) ^= *reinterpret_cast<const u64bit*>(in); +#else + out[0] ^= in[0]; out[1] ^= in[1]; + out[2] ^= in[2]; out[3] ^= in[3]; + out[4] ^= in[4]; out[5] ^= in[5]; + out[6] ^= in[6]; out[7] ^= in[7]; +#endif + + out += 8; in += 8; length -= 8; + } + for(u32bit j = 0; j != length; ++j) + out[j] ^= in[j]; + } + +/** +* XOR arrays. Postcondition out[i] = in[i] ^ in2[i] forall i = 0...length +* @param out the output buffer +* @param in the first input buffer +* @param in2 the second output buffer +* @param length the length of the three buffers +*/ +inline void xor_buf(byte out[], + const byte in[], + const byte in2[], + u32bit length) + { + while(length >= 8) + { +#if BOTAN_UNALIGNED_LOADSTOR_OK + *reinterpret_cast<u64bit*>(out) = + *reinterpret_cast<const u64bit*>(in) ^ + *reinterpret_cast<const u64bit*>(in2); +#else + out[0] = in[0] ^ in2[0]; out[1] = in[1] ^ in2[1]; + out[2] = in[2] ^ in2[2]; out[3] = in[3] ^ in2[3]; + out[4] = in[4] ^ in2[4]; out[5] = in[5] ^ in2[5]; + out[6] = in[6] ^ in2[6]; out[7] = in[7] ^ in2[7]; +#endif + + in += 8; in2 += 8; out += 8; length -= 8; + } + + for(u32bit j = 0; j != length; ++j) + out[j] = in[j] ^ in2[j]; + } + +} + +#endif |