modes_test.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* test CFB/OFB/CBC modes */
  2. #include <tomcrypt_test.h>
  3. int modes_test(void)
  4. {
  5. unsigned char pt[64], ct[64], tmp[64], key[16], iv[16], iv2[16];
  6. int cipher_idx;
  7. symmetric_CBC cbc;
  8. symmetric_CFB cfb;
  9. symmetric_OFB ofb;
  10. symmetric_CTR ctr;
  11. unsigned long l;
  12. /* make a random pt, key and iv */
  13. yarrow_read(pt, 64, &yarrow_prng);
  14. yarrow_read(key, 16, &yarrow_prng);
  15. yarrow_read(iv, 16, &yarrow_prng);
  16. /* get idx of AES handy */
  17. cipher_idx = find_cipher("aes");
  18. if (cipher_idx == -1) {
  19. fprintf(stderr, "test requires AES");
  20. return 1;
  21. }
  22. #ifdef CBC
  23. /* test CBC mode */
  24. /* encode the block */
  25. DO(cbc_start(cipher_idx, iv, key, 16, 0, &cbc));
  26. l = sizeof(iv2);
  27. DO(cbc_getiv(iv2, &l, &cbc));
  28. if (l != 16 || memcmp(iv2, iv, 16)) {
  29. fprintf(stderr, "cbc_getiv failed");
  30. return 1;
  31. }
  32. DO(cbc_encrypt(pt, ct, 64, &cbc));
  33. /* decode the block */
  34. DO(cbc_setiv(iv2, l, &cbc));
  35. zeromem(tmp, sizeof(tmp));
  36. DO(cbc_decrypt(ct, tmp, 64, &cbc));
  37. if (memcmp(tmp, pt, 64) != 0) {
  38. fprintf(stderr, "CBC failed");
  39. return 1;
  40. }
  41. #endif
  42. #ifdef CFB
  43. /* test CFB mode */
  44. /* encode the block */
  45. DO(cfb_start(cipher_idx, iv, key, 16, 0, &cfb));
  46. l = sizeof(iv2);
  47. DO(cfb_getiv(iv2, &l, &cfb));
  48. /* note we don't memcmp iv2/iv since cfb_start processes the IV for the first block */
  49. if (l != 16) {
  50. fprintf(stderr, "cfb_getiv failed");
  51. return 1;
  52. }
  53. DO(cfb_encrypt(pt, ct, 64, &cfb));
  54. /* decode the block */
  55. DO(cfb_setiv(iv, l, &cfb));
  56. zeromem(tmp, sizeof(tmp));
  57. DO(cfb_decrypt(ct, tmp, 64, &cfb));
  58. if (memcmp(tmp, pt, 64) != 0) {
  59. fprintf(stderr, "CFB failed");
  60. return 1;
  61. }
  62. #endif
  63. #ifdef OFB
  64. /* test OFB mode */
  65. /* encode the block */
  66. DO(ofb_start(cipher_idx, iv, key, 16, 0, &ofb));
  67. l = sizeof(iv2);
  68. DO(ofb_getiv(iv2, &l, &ofb));
  69. if (l != 16 || memcmp(iv2, iv, 16)) {
  70. fprintf(stderr, "ofb_getiv failed");
  71. return 1;
  72. }
  73. DO(ofb_encrypt(pt, ct, 64, &ofb));
  74. /* decode the block */
  75. DO(ofb_setiv(iv2, l, &ofb));
  76. zeromem(tmp, sizeof(tmp));
  77. DO(ofb_decrypt(ct, tmp, 64, &ofb));
  78. if (memcmp(tmp, pt, 64) != 0) {
  79. fprintf(stderr, "OFB failed");
  80. return 1;
  81. }
  82. #endif
  83. #ifdef CTR
  84. /* test CTR mode */
  85. /* encode the block */
  86. DO(ctr_start(cipher_idx, iv, key, 16, 0, CTR_COUNTER_LITTLE_ENDIAN, &ctr));
  87. l = sizeof(iv2);
  88. DO(ctr_getiv(iv2, &l, &ctr));
  89. if (l != 16 || memcmp(iv2, iv, 16)) {
  90. fprintf(stderr, "ctr_getiv failed");
  91. return 1;
  92. }
  93. DO(ctr_encrypt(pt, ct, 57, &ctr));
  94. /* decode the block */
  95. DO(ctr_setiv(iv2, l, &ctr));
  96. zeromem(tmp, sizeof(tmp));
  97. DO(ctr_decrypt(ct, tmp, 57, &ctr));
  98. if (memcmp(tmp, pt, 57) != 0) {
  99. fprintf(stderr, "CTR failed");
  100. return 1;
  101. }
  102. #endif
  103. return 0;
  104. }
  105. /* $Source$ */
  106. /* $Revision$ */
  107. /* $Date$ */