----------------------------------------------------------------------------- Purpose: Tests cryptography hex encoding -----------------------------------------------------------------------------
| 73 | // Purpose: Tests cryptography hex encoding |
| 74 | //----------------------------------------------------------------------------- |
| 75 | void TestCryptoEncoding() |
| 76 | { |
| 77 | bool bRet; |
| 78 | |
| 79 | // If you change the source data you'll need to change the expected encoding |
| 80 | // output strings further down! |
| 81 | uint8 rgubData[] = { 0x14, 0xfe, 0x26, 0x19, 0x54, 0x78, 0x00, 0x35, 0x19, 0xa9, 0x54, 0x4e, 0x99 }; |
| 82 | uint cubData = V_ARRAYSIZE( rgubData ); |
| 83 | char rgchEncodedData[k_cMedBuff]; |
| 84 | uint cchEncodedData = V_ARRAYSIZE( rgchEncodedData ); |
| 85 | uint8 rgubDecodedData[k_cSmallBuff]; |
| 86 | uint cubDecodedData = V_ARRAYSIZE( rgubDecodedData ); |
| 87 | |
| 88 | // HEX |
| 89 | // encode some data |
| 90 | bRet = CCrypto::HexEncode( rgubData, cubData, rgchEncodedData, cchEncodedData ); |
| 91 | CHECK( bRet ); // must succeed |
| 92 | |
| 93 | // decode the data |
| 94 | bRet = CCrypto::HexDecode( rgchEncodedData, rgubDecodedData, &cubDecodedData ); |
| 95 | CHECK( bRet ); // must succeed |
| 96 | CHECK( cubDecodedData == cubData ); // must be of correct size |
| 97 | |
| 98 | bRet = (0 == V_memcmp( rgubData, rgubDecodedData, cubData )); |
| 99 | CHECK( bRet ); // decoded data must match original |
| 100 | |
| 101 | // test the documented, if questionable, permissiveness of hexdecode. Note that Crypto++ |
| 102 | // documentation claims that the last partial byte should have been parsed as E0, but this |
| 103 | // has been disproven by testing with 5.6.1 and 5.6.2. this test verifies that there is no |
| 104 | // change in the behavior of partial strings, if the algorithm should be updated. |
| 105 | cubDecodedData = V_ARRAYSIZE( rgubDecodedData ); |
| 106 | bRet = CCrypto::HexDecode( "x,F\nF1\t ,2\t~E ", rgubDecodedData, &cubDecodedData ); |
| 107 | CHECK( bRet ); // must succeed |
| 108 | CHECK( cubDecodedData == 2 && rgubDecodedData[0] == 0xFF && rgubDecodedData[1] == 0x12 ); |
| 109 | |
| 110 | // this hilarious string is offered up for laughs to verify that we remain as broken as ever. |
| 111 | // Crypto++'s documentation claims that it will "correctly" parse this string as FF 12 E0. |
| 112 | // In reality, it does the dumb/obvious thing and discards the 'x' and ' ' characters and |
| 113 | // parses the '0's, resulting in 4 bytes. There is no evidence that any version of Crypto++ |
| 114 | // has ever matched the documentation and actually done "smart" prefix skipping. |
| 115 | cubDecodedData = V_ARRAYSIZE( rgubDecodedData ); |
| 116 | bRet = CCrypto::HexDecode( "0xFF 0x12 0xE", rgubDecodedData, &cubDecodedData ); |
| 117 | CHECK( bRet ); |
| 118 | CHECK( cubDecodedData == 4 && rgubDecodedData[0] == 0x0F && rgubDecodedData[1] == 0xF0 && rgubDecodedData[2] == 0x12 && rgubDecodedData[3] == 0x0E ); |
| 119 | |
| 120 | // BASE64 |
| 121 | cchEncodedData = V_ARRAYSIZE( rgchEncodedData ); |
| 122 | bRet = CCrypto::Base64Encode( rgubData, cubData, rgchEncodedData, &cchEncodedData ); |
| 123 | CHECK( bRet ); // must succeed |
| 124 | |
| 125 | // decode the data |
| 126 | cubDecodedData = V_ARRAYSIZE( rgubDecodedData ); |
| 127 | bRet = CCrypto::Base64Decode( rgchEncodedData, cchEncodedData, rgubDecodedData, &cubDecodedData ); |
| 128 | CHECK( bRet ); // must succeed |
| 129 | CHECK( cubDecodedData == cubData ); // must be of correct size |
| 130 | |
| 131 | bRet = ( 0 == V_memcmp( rgubData, rgubDecodedData, cubData ) ); |
| 132 | CHECK( bRet ); // decoded data must match original |