test_suite_des.function 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. BEGIN_HEADER
  2. #include <polarssl/des.h>
  3. END_HEADER
  4. BEGIN_DEPENDENCIES
  5. depends_on:POLARSSL_DES_C
  6. END_DEPENDENCIES
  7. BEGIN_CASE
  8. des_encrypt_ecb:hex_key_string:hex_src_string:hex_dst_string
  9. {
  10. unsigned char key_str[100];
  11. unsigned char src_str[100];
  12. unsigned char dst_str[100];
  13. unsigned char output[100];
  14. des_context ctx;
  15. memset(key_str, 0x00, 100);
  16. memset(src_str, 0x00, 100);
  17. memset(dst_str, 0x00, 100);
  18. memset(output, 0x00, 100);
  19. unhexify( key_str, {hex_key_string} );
  20. unhexify( src_str, {hex_src_string} );
  21. des_setkey_enc( &ctx, key_str );
  22. TEST_ASSERT( des_crypt_ecb( &ctx, src_str, output ) == 0 );
  23. hexify( dst_str, output, 8 );
  24. TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
  25. }
  26. END_CASE
  27. BEGIN_CASE
  28. des_decrypt_ecb:hex_key_string:hex_src_string:hex_dst_string
  29. {
  30. unsigned char key_str[100];
  31. unsigned char src_str[100];
  32. unsigned char dst_str[100];
  33. unsigned char output[100];
  34. des_context ctx;
  35. memset(key_str, 0x00, 100);
  36. memset(src_str, 0x00, 100);
  37. memset(dst_str, 0x00, 100);
  38. memset(output, 0x00, 100);
  39. unhexify( key_str, {hex_key_string} );
  40. unhexify( src_str, {hex_src_string} );
  41. des_setkey_dec( &ctx, key_str );
  42. TEST_ASSERT( des_crypt_ecb( &ctx, src_str, output ) == 0 );
  43. hexify( dst_str, output, 8 );
  44. TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
  45. }
  46. END_CASE
  47. BEGIN_CASE
  48. des_encrypt_cbc:hex_key_string:hex_iv_string:hex_src_string:hex_dst_string:cbc_result
  49. {
  50. unsigned char key_str[100];
  51. unsigned char iv_str[100];
  52. unsigned char src_str[100];
  53. unsigned char dst_str[100];
  54. unsigned char output[100];
  55. des_context ctx;
  56. int src_len;
  57. memset(key_str, 0x00, 100);
  58. memset(iv_str, 0x00, 100);
  59. memset(src_str, 0x00, 100);
  60. memset(dst_str, 0x00, 100);
  61. memset(output, 0x00, 100);
  62. unhexify( key_str, {hex_key_string} );
  63. unhexify( iv_str, {hex_iv_string} );
  64. src_len = unhexify( src_str, {hex_src_string} );
  65. des_setkey_enc( &ctx, key_str );
  66. TEST_ASSERT( des_crypt_cbc( &ctx, DES_ENCRYPT, src_len, iv_str, src_str, output ) == {cbc_result} );
  67. if( {cbc_result} == 0 )
  68. {
  69. hexify( dst_str, output, src_len );
  70. TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
  71. }
  72. }
  73. END_CASE
  74. BEGIN_CASE
  75. des_decrypt_cbc:hex_key_string:hex_iv_string:hex_src_string:hex_dst_string:cbc_result
  76. {
  77. unsigned char key_str[100];
  78. unsigned char iv_str[100];
  79. unsigned char src_str[100];
  80. unsigned char dst_str[100];
  81. unsigned char output[100];
  82. des_context ctx;
  83. int src_len;
  84. memset(key_str, 0x00, 100);
  85. memset(iv_str, 0x00, 100);
  86. memset(src_str, 0x00, 100);
  87. memset(dst_str, 0x00, 100);
  88. memset(output, 0x00, 100);
  89. unhexify( key_str, {hex_key_string} );
  90. unhexify( iv_str, {hex_iv_string} );
  91. src_len = unhexify( src_str, {hex_src_string} );
  92. des_setkey_dec( &ctx, key_str );
  93. TEST_ASSERT( des_crypt_cbc( &ctx, DES_DECRYPT, src_len, iv_str, src_str, output ) == {cbc_result} );
  94. if( {cbc_result} == 0 )
  95. {
  96. hexify( dst_str, output, src_len );
  97. TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
  98. }
  99. }
  100. END_CASE
  101. BEGIN_CASE
  102. des3_encrypt_ecb:key_count:hex_key_string:hex_src_string:hex_dst_string
  103. {
  104. unsigned char key_str[100];
  105. unsigned char src_str[100];
  106. unsigned char dst_str[100];
  107. unsigned char output[100];
  108. des3_context ctx;
  109. memset(key_str, 0x00, 100);
  110. memset(src_str, 0x00, 100);
  111. memset(dst_str, 0x00, 100);
  112. memset(output, 0x00, 100);
  113. unhexify( key_str, {hex_key_string} );
  114. unhexify( src_str, {hex_src_string} );
  115. if( {key_count} == 2 )
  116. des3_set2key_enc( &ctx, key_str );
  117. else if( {key_count} == 3 )
  118. des3_set3key_enc( &ctx, key_str );
  119. else
  120. TEST_ASSERT( 0 );
  121. TEST_ASSERT( des3_crypt_ecb( &ctx, src_str, output ) == 0 );
  122. hexify( dst_str, output, 8 );
  123. TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
  124. }
  125. END_CASE
  126. BEGIN_CASE
  127. des3_decrypt_ecb:key_count:hex_key_string:hex_src_string:hex_dst_string
  128. {
  129. unsigned char key_str[100];
  130. unsigned char src_str[100];
  131. unsigned char dst_str[100];
  132. unsigned char output[100];
  133. des3_context ctx;
  134. memset(key_str, 0x00, 100);
  135. memset(src_str, 0x00, 100);
  136. memset(dst_str, 0x00, 100);
  137. memset(output, 0x00, 100);
  138. unhexify( key_str, {hex_key_string} );
  139. unhexify( src_str, {hex_src_string} );
  140. if( {key_count} == 2 )
  141. des3_set2key_dec( &ctx, key_str );
  142. else if( {key_count} == 3 )
  143. des3_set3key_dec( &ctx, key_str );
  144. else
  145. TEST_ASSERT( 0 );
  146. TEST_ASSERT( des3_crypt_ecb( &ctx, src_str, output ) == 0 );
  147. hexify( dst_str, output, 8 );
  148. TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
  149. }
  150. END_CASE
  151. BEGIN_CASE
  152. des3_encrypt_cbc:key_count:hex_key_string:hex_iv_string:hex_src_string:hex_dst_string:cbc_result
  153. {
  154. unsigned char key_str[100];
  155. unsigned char iv_str[100];
  156. unsigned char src_str[100];
  157. unsigned char dst_str[100];
  158. unsigned char output[100];
  159. des3_context ctx;
  160. int src_len;
  161. memset(key_str, 0x00, 100);
  162. memset(iv_str, 0x00, 100);
  163. memset(src_str, 0x00, 100);
  164. memset(dst_str, 0x00, 100);
  165. memset(output, 0x00, 100);
  166. unhexify( key_str, {hex_key_string} );
  167. unhexify( iv_str, {hex_iv_string} );
  168. src_len = unhexify( src_str, {hex_src_string} );
  169. if( {key_count} == 2 )
  170. des3_set2key_enc( &ctx, key_str );
  171. else if( {key_count} == 3 )
  172. des3_set3key_enc( &ctx, key_str );
  173. else
  174. TEST_ASSERT( 0 );
  175. TEST_ASSERT( des3_crypt_cbc( &ctx, DES_ENCRYPT, src_len, iv_str, src_str, output ) == {cbc_result} );
  176. if( {cbc_result} == 0 )
  177. {
  178. hexify( dst_str, output, src_len );
  179. TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
  180. }
  181. }
  182. END_CASE
  183. BEGIN_CASE
  184. des3_decrypt_cbc:key_count:hex_key_string:hex_iv_string:hex_src_string:hex_dst_string:cbc_result
  185. {
  186. unsigned char key_str[100];
  187. unsigned char iv_str[100];
  188. unsigned char src_str[100];
  189. unsigned char dst_str[100];
  190. unsigned char output[100];
  191. des3_context ctx;
  192. int src_len;
  193. memset(key_str, 0x00, 100);
  194. memset(iv_str, 0x00, 100);
  195. memset(src_str, 0x00, 100);
  196. memset(dst_str, 0x00, 100);
  197. memset(output, 0x00, 100);
  198. unhexify( key_str, {hex_key_string} );
  199. unhexify( iv_str, {hex_iv_string} );
  200. src_len = unhexify( src_str, {hex_src_string} );
  201. if( {key_count} == 2 )
  202. des3_set2key_dec( &ctx, key_str );
  203. else if( {key_count} == 3 )
  204. des3_set3key_dec( &ctx, key_str );
  205. else
  206. TEST_ASSERT( 0 );
  207. TEST_ASSERT( des3_crypt_cbc( &ctx, DES_DECRYPT, src_len, iv_str, src_str, output ) == {cbc_result} );
  208. if( {cbc_result} == 0 )
  209. {
  210. hexify( dst_str, output, src_len );
  211. TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
  212. }
  213. }
  214. END_CASE
  215. BEGIN_CASE
  216. des_key_parity_run:
  217. {
  218. int i, j, cnt;
  219. unsigned char key[DES_KEY_SIZE];
  220. unsigned int parity;
  221. memset( key, 0, DES_KEY_SIZE );
  222. cnt = 0;
  223. // Iterate through all possible byte values
  224. //
  225. for( i = 0; i < 32; i++ )
  226. {
  227. for( j = 0; j < 8; j++ )
  228. key[j] = cnt++;
  229. // Set the key parity according to the table
  230. //
  231. des_key_set_parity( key );
  232. // Check the parity with a function
  233. //
  234. for( j = 0; j < 8; j++ )
  235. {
  236. parity = key[j] ^ ( key[j] >> 4 );
  237. parity = parity ^
  238. ( parity >> 1 ) ^
  239. ( parity >> 2 ) ^
  240. ( parity >> 3 );
  241. parity &= 1;
  242. if( parity != 1 )
  243. TEST_ASSERT( 0 );
  244. }
  245. // Check the parity with the table
  246. //
  247. TEST_ASSERT( des_key_check_key_parity( key ) == 0 );
  248. }
  249. }
  250. END_CASE
  251. BEGIN_CASE
  252. des_selftest:
  253. {
  254. TEST_ASSERT( des_self_test( 0 ) == 0 );
  255. }
  256. END_CASE