I2C toy code
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

131 rader
3.1 KiB

  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis
  2. *
  3. * LibTomCrypt is a library that provides various cryptographic
  4. * algorithms in a highly modular and flexible manner.
  5. *
  6. * The library is free for all purposes without any express
  7. * guarantee it works.
  8. */
  9. /* test CFB/OFB/CBC modes */
  10. #include <tomcrypt_test.h>
  11. int modes_test(void)
  12. {
  13. int ret = CRYPT_NOP;
  14. #ifdef LTC_CBC_MODE
  15. symmetric_CBC cbc;
  16. #endif
  17. #ifdef LTC_CFB_MODE
  18. symmetric_CFB cfb;
  19. #endif
  20. #ifdef LTC_OFB_MODE
  21. symmetric_OFB ofb;
  22. #endif
  23. #if defined(LTC_CBC_MODE) || defined(LTC_CFB_MODE) || defined(LTC_OFB_MODE)
  24. unsigned char pt[64], ct[64], tmp[64], key[16], iv[16], iv2[16];
  25. int cipher_idx;
  26. unsigned long l;
  27. /* make a random pt, key and iv */
  28. yarrow_read(pt, 64, &yarrow_prng);
  29. yarrow_read(key, 16, &yarrow_prng);
  30. yarrow_read(iv, 16, &yarrow_prng);
  31. /* get idx of AES handy */
  32. cipher_idx = find_cipher("aes");
  33. if (cipher_idx == -1) {
  34. fprintf(stderr, "test requires AES");
  35. return 1;
  36. }
  37. #endif
  38. #ifdef LTC_F8_MODE
  39. DO(ret = f8_test_mode());
  40. #endif
  41. #ifdef LTC_LRW_MODE
  42. DO(ret = lrw_test());
  43. #endif
  44. #ifdef LTC_CBC_MODE
  45. /* test CBC mode */
  46. /* encode the block */
  47. DO(ret = cbc_start(cipher_idx, iv, key, 16, 0, &cbc));
  48. l = sizeof(iv2);
  49. DO(ret = cbc_getiv(iv2, &l, &cbc));
  50. if (l != 16 || memcmp(iv2, iv, 16)) {
  51. fprintf(stderr, "cbc_getiv failed");
  52. return 1;
  53. }
  54. DO(ret = cbc_encrypt(pt, ct, 64, &cbc));
  55. /* decode the block */
  56. DO(ret = cbc_setiv(iv2, l, &cbc));
  57. zeromem(tmp, sizeof(tmp));
  58. DO(ret = cbc_decrypt(ct, tmp, 64, &cbc));
  59. if (memcmp(tmp, pt, 64) != 0) {
  60. fprintf(stderr, "CBC failed");
  61. return 1;
  62. }
  63. #endif
  64. #ifdef LTC_CFB_MODE
  65. /* test CFB mode */
  66. /* encode the block */
  67. DO(ret = cfb_start(cipher_idx, iv, key, 16, 0, &cfb));
  68. l = sizeof(iv2);
  69. DO(ret = cfb_getiv(iv2, &l, &cfb));
  70. /* note we don't memcmp iv2/iv since cfb_start processes the IV for the first block */
  71. if (l != 16) {
  72. fprintf(stderr, "cfb_getiv failed");
  73. return 1;
  74. }
  75. DO(ret = cfb_encrypt(pt, ct, 64, &cfb));
  76. /* decode the block */
  77. DO(ret = cfb_setiv(iv, l, &cfb));
  78. zeromem(tmp, sizeof(tmp));
  79. DO(ret = cfb_decrypt(ct, tmp, 64, &cfb));
  80. if (memcmp(tmp, pt, 64) != 0) {
  81. fprintf(stderr, "CFB failed");
  82. return 1;
  83. }
  84. #endif
  85. #ifdef LTC_OFB_MODE
  86. /* test OFB mode */
  87. /* encode the block */
  88. DO(ret = ofb_start(cipher_idx, iv, key, 16, 0, &ofb));
  89. l = sizeof(iv2);
  90. DO(ret = ofb_getiv(iv2, &l, &ofb));
  91. if (l != 16 || memcmp(iv2, iv, 16)) {
  92. fprintf(stderr, "ofb_getiv failed");
  93. return 1;
  94. }
  95. DO(ret = ofb_encrypt(pt, ct, 64, &ofb));
  96. /* decode the block */
  97. DO(ret = ofb_setiv(iv2, l, &ofb));
  98. zeromem(tmp, sizeof(tmp));
  99. DO(ret = ofb_decrypt(ct, tmp, 64, &ofb));
  100. if (memcmp(tmp, pt, 64) != 0) {
  101. fprintf(stderr, "OFB failed");
  102. return 1;
  103. }
  104. #endif
  105. #if defined(LTC_CTR_MODE) && defined(LTC_RIJNDAEL)
  106. DO(ret = ctr_test());
  107. #endif
  108. #ifdef LTC_XTS_MODE
  109. DO(ret = xts_test());
  110. #endif
  111. return 0;
  112. }
  113. /* ref: $Format:%D$ */
  114. /* git commit: $Format:%H$ */
  115. /* commit time: $Format:%ai$ */