store_test.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include <tomcrypt_test.h>
  2. /* Test store/load macros with offsets */
  3. int store_test(void)
  4. {
  5. unsigned char buf[256];
  6. int y;
  7. ulong32 L, L1;
  8. ulong64 LL, LL1;
  9. #ifdef LTC_FAST
  10. int x, z;
  11. #endif
  12. for (y = 0; y < 4; y++) {
  13. L = 0x12345678UL;
  14. L1 = 0;
  15. STORE32L(L, buf + y);
  16. LOAD32L(L1, buf + y);
  17. if (L1 != L) {
  18. fprintf(stderr, "\n32L failed at offset %d\n", y);
  19. return 1;
  20. }
  21. STORE32H(L, buf + y);
  22. LOAD32H(L1, buf + y);
  23. if (L1 != L) {
  24. fprintf(stderr, "\n32H failed at offset %d\n", y);
  25. return 1;
  26. }
  27. }
  28. for (y = 0; y < 8; y++) {
  29. LL = CONST64 (0x01020304050607);
  30. LL1 = 0;
  31. STORE64L(LL, buf + y);
  32. LOAD64L(LL1, buf + y);
  33. if (LL1 != LL) {
  34. fprintf(stderr, "\n64L failed at offset %d\n", y);
  35. return 1;
  36. }
  37. STORE64H(LL, buf + y);
  38. LOAD64H(LL1, buf + y);
  39. if (LL1 != LL) {
  40. fprintf(stderr, "\n64H failed at offset %d\n", y);
  41. return 1;
  42. }
  43. }
  44. /* test LTC_FAST */
  45. #ifdef LTC_FAST
  46. y = 16;
  47. for (z = 0; z < y; z++) {
  48. /* fill y bytes with random */
  49. yarrow_read(buf+z, y, &yarrow_prng);
  50. yarrow_read(buf+z+y, y, &yarrow_prng);
  51. /* now XOR it byte for byte */
  52. for (x = 0; x < y; x++) {
  53. buf[2*y+z+x] = buf[z+x] ^ buf[z+y+x];
  54. }
  55. /* now XOR it word for word */
  56. for (x = 0; x < y; x += sizeof(LTC_FAST_TYPE)) {
  57. *((LTC_FAST_TYPE*)(&buf[3*y+z+x])) = *((LTC_FAST_TYPE*)(&buf[z+x])) ^ *((LTC_FAST_TYPE*)(&buf[z+y+x]));
  58. }
  59. if (memcmp(&buf[2*y+z], &buf[3*y+z], y)) {
  60. fprintf(stderr, "\nLTC_FAST failed at offset %d\n", z);
  61. return 1;
  62. }
  63. }
  64. #endif
  65. return 0;
  66. }
  67. /* $Source$ */
  68. /* $Revision$ */
  69. /* $Date$ */