| 1 | extern crate commoncrypto_sys; |
| 2 | extern crate hex; |
| 3 | |
| 4 | use hex::ToHex; |
| 5 | |
| 6 | const TO_HASH: &'static str = "The quick brown fox jumps over the lazy dog"; |
| 7 | const TO_HASH_MD5: &'static str = "9e107d9d372bb6826bd81d3542a419d6"; |
| 8 | const TO_HASH_SHA1: &'static str = "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12"; |
| 9 | const TO_HASH_SHA256: &'static str = "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592"; |
| 10 | const TO_HASH_SHA384: &'static str = "ca737f1014a48f4c0b6dd43cb177b0afd9e5169367544c494011e3317dbf9a509cb1e5dc1e85a941bbee3d7f2afbc9b1"; |
| 11 | const TO_HASH_SHA512: &'static str = "07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6"; |
| 12 | |
| 13 | macro_rules! test_cc_hash { |
| 14 | ( |
| 15 | $test_name: ident, |
| 16 | $ctx_ty: ident, |
| 17 | $digest_len: ident, |
| 18 | $init_func: ident, |
| 19 | $update_func: ident, |
| 20 | $final_func: ident, |
| 21 | $expected_hash: ident |
| 22 | ) => { |
| 23 | #[test] |
| 24 | fn $test_name() { |
| 25 | let mut ctx = commoncrypto_sys::$ctx_ty::default(); |
| 26 | let mut md = [0u8; commoncrypto_sys::$digest_len]; |
| 27 | unsafe { |
| 28 | assert_eq!(commoncrypto_sys::$init_func(&mut ctx), 1); |
| 29 | assert_eq!(commoncrypto_sys::$update_func(&mut ctx, TO_HASH.as_ptr(), TO_HASH.len()), 1); |
| 30 | assert_eq!(commoncrypto_sys::$final_func(md.as_mut_ptr(), &mut ctx), 1); |
| 31 | } |
| 32 | assert_eq!(md.to_vec().to_hex(), $expected_hash); |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | macro_rules! test_ccdigest { |
| 38 | ( |
| 39 | $test_name: ident, |
| 40 | $algorithm: ident, |
| 41 | $digest_len: ident, |
| 42 | $expected_hash: ident |
| 43 | ) => { |
| 44 | #[test] |
| 45 | fn $test_name() { |
| 46 | let mut md = [0u8; commoncrypto_sys::$digest_len]; |
| 47 | unsafe { |
| 48 | assert_eq!(commoncrypto_sys::CCDigest(commoncrypto_sys::CCDigestAlgorithm::$algorithm, |
| 49 | TO_HASH.as_ptr(), |
| 50 | TO_HASH.len(), |
| 51 | md.as_mut_ptr()), 0) |
| 52 | } |
| 53 | assert_eq!(md.to_vec().to_hex(), $expected_hash); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | macro_rules! test_ccdigestgetoutputsize { |
| 59 | ( |
| 60 | $test_name: ident, |
| 61 | $algorithm: ident, |
| 62 | $expected_digest_len: ident |
| 63 | ) => { |
| 64 | #[test] |
| 65 | fn $test_name() { |
| 66 | unsafe { |
| 67 | assert_eq!(commoncrypto_sys::CCDigestGetOutputSize(commoncrypto_sys::CCDigestAlgorithm::$algorithm), |
| 68 | commoncrypto_sys::$expected_digest_len); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | test_cc_hash!(md5_hash, |
| 75 | CC_MD5_CTX, |
| 76 | MD5_DIGEST_LENGTH, |
| 77 | CC_MD5_Init, |
| 78 | CC_MD5_Update, |
| 79 | CC_MD5_Final, |
| 80 | TO_HASH_MD5); |
| 81 | test_cc_hash!(sha1_hash, |
| 82 | CC_SHA_CTX, |
| 83 | SHA1_DIGEST_LENGTH, |
| 84 | CC_SHA1_Init, |
| 85 | CC_SHA1_Update, |
| 86 | CC_SHA1_Final, |
| 87 | TO_HASH_SHA1); |
| 88 | test_cc_hash!(sha256_hash, |
| 89 | CC_SHA256_CTX, |
| 90 | SHA256_DIGEST_LENGTH, |
| 91 | CC_SHA256_Init, |
| 92 | CC_SHA256_Update, |
| 93 | CC_SHA256_Final, |
| 94 | TO_HASH_SHA256); |
| 95 | test_cc_hash!(sha384_hash, |
| 96 | CC_SHA512_CTX, |
| 97 | SHA384_DIGEST_LENGTH, |
| 98 | CC_SHA384_Init, |
| 99 | CC_SHA384_Update, |
| 100 | CC_SHA384_Final, |
| 101 | TO_HASH_SHA384); |
| 102 | test_cc_hash!(sha512_hash, |
| 103 | CC_SHA512_CTX, |
| 104 | SHA512_DIGEST_LENGTH, |
| 105 | CC_SHA512_Init, |
| 106 | CC_SHA512_Update, |
| 107 | CC_SHA512_Final, |
| 108 | TO_HASH_SHA512); |
| 109 | |
| 110 | test_ccdigest!(md5_ccdigest, kCCDigestMD5, MD5_DIGEST_LENGTH, TO_HASH_MD5); |
| 111 | test_ccdigest!(sha1_ccdigest, |
| 112 | kCCDigestSHA1, |
| 113 | SHA1_DIGEST_LENGTH, |
| 114 | TO_HASH_SHA1); |
| 115 | test_ccdigest!(sha256_ccdigest, |
| 116 | kCCDigestSHA256, |
| 117 | SHA256_DIGEST_LENGTH, |
| 118 | TO_HASH_SHA256); |
| 119 | test_ccdigest!(sha384_ccdigest, |
| 120 | kCCDigestSHA384, |
| 121 | SHA384_DIGEST_LENGTH, |
| 122 | TO_HASH_SHA384); |
| 123 | test_ccdigest!(sha512_ccdigest, |
| 124 | kCCDigestSHA512, |
| 125 | SHA512_DIGEST_LENGTH, |
| 126 | TO_HASH_SHA512); |
| 127 | |
| 128 | test_ccdigestgetoutputsize!(md5_ccdigestoutputsize, kCCDigestMD5, MD5_DIGEST_LENGTH); |
| 129 | test_ccdigestgetoutputsize!(sha1_ccdigestoutputsize, kCCDigestSHA1, SHA1_DIGEST_LENGTH); |
| 130 | test_ccdigestgetoutputsize!(sha256_ccdigestoutputsize, |
| 131 | kCCDigestSHA256, |
| 132 | SHA256_DIGEST_LENGTH); |
| 133 | test_ccdigestgetoutputsize!(sha384_ccdigestoutputsize, |
| 134 | kCCDigestSHA384, |
| 135 | SHA384_DIGEST_LENGTH); |
| 136 | test_ccdigestgetoutputsize!(sha512_ccdigestoutputsize, |
| 137 | kCCDigestSHA512, |
| 138 | SHA512_DIGEST_LENGTH); |