store_test.c 1.8 KB

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