modes_test.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* test CFB/OFB/CBC modes */
  2. #include "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 x, 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, &test_yarrow);
  14. yarrow_read(key, 16, &test_yarrow);
  15. yarrow_read(iv, 16, &test_yarrow);
  16. /* get idx of AES handy */
  17. cipher_idx = find_cipher("aes");
  18. if (cipher_idx == -1) {
  19. printf("test requires AES");
  20. return 1;
  21. }
  22. /* test CBC mode */
  23. /* encode the block */
  24. DO(cbc_start(cipher_idx, iv, key, 16, 0, &cbc));
  25. l = sizeof(iv2);
  26. DO(cbc_getiv(iv2, &l, &cbc));
  27. if (l != 16 || memcmp(iv2, iv, 16)) {
  28. printf("cbc_getiv failed");
  29. return 1;
  30. }
  31. for (x = 0; x < 4; x++) {
  32. DO(cbc_encrypt(pt+x*16, ct+x*16, &cbc));
  33. }
  34. /* decode the block */
  35. DO(cbc_setiv(iv2, l, &cbc));
  36. zeromem(tmp, sizeof(tmp));
  37. for (x = 0; x < 4; x++) {
  38. DO(cbc_decrypt(ct+x*16, tmp+x*16, &cbc));
  39. }
  40. if (memcmp(tmp, pt, 64) != 0) {
  41. printf("CBC failed");
  42. return 1;
  43. }
  44. /* test CFB mode */
  45. /* encode the block */
  46. DO(cfb_start(cipher_idx, iv, key, 16, 0, &cfb));
  47. l = sizeof(iv2);
  48. DO(cfb_getiv(iv2, &l, &cfb));
  49. /* note we don't memcmp iv2/iv since cfb_start processes the IV for the first block */
  50. if (l != 16) {
  51. printf("cfb_getiv failed");
  52. return 1;
  53. }
  54. DO(cfb_encrypt(pt, ct, 64, &cfb));
  55. /* decode the block */
  56. DO(cfb_setiv(iv, l, &cfb));
  57. zeromem(tmp, sizeof(tmp));
  58. DO(cfb_decrypt(ct, tmp, 64, &cfb));
  59. if (memcmp(tmp, pt, 64) != 0) {
  60. printf("CFB failed");
  61. return 1;
  62. }
  63. /* test OFB mode */
  64. /* encode the block */
  65. DO(ofb_start(cipher_idx, iv, key, 16, 0, &ofb));
  66. l = sizeof(iv2);
  67. DO(ofb_getiv(iv2, &l, &ofb));
  68. if (l != 16 || memcmp(iv2, iv, 16)) {
  69. printf("ofb_getiv failed");
  70. return 1;
  71. }
  72. DO(ofb_encrypt(pt, ct, 64, &ofb));
  73. /* decode the block */
  74. DO(ofb_setiv(iv2, l, &ofb));
  75. zeromem(tmp, sizeof(tmp));
  76. DO(ofb_decrypt(ct, tmp, 64, &ofb));
  77. if (memcmp(tmp, pt, 64) != 0) {
  78. printf("OFB failed");
  79. return 1;
  80. }
  81. /* test CTR mode */
  82. /* encode the block */
  83. DO(ctr_start(cipher_idx, iv, key, 16, 0, &ctr));
  84. l = sizeof(iv2);
  85. DO(ctr_getiv(iv2, &l, &ctr));
  86. if (l != 16 || memcmp(iv2, iv, 16)) {
  87. printf("ctr_getiv failed");
  88. return 1;
  89. }
  90. DO(ctr_encrypt(pt, ct, 64, &ctr));
  91. /* decode the block */
  92. DO(ctr_setiv(iv2, l, &ctr));
  93. zeromem(tmp, sizeof(tmp));
  94. DO(ctr_decrypt(ct, tmp, 64, &ctr));
  95. if (memcmp(tmp, pt, 64) != 0) {
  96. printf("CTR failed");
  97. return 1;
  98. }
  99. return 0;
  100. }