pmac_test.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. * Tom St Denis, [email protected], http://libtomcrypt.org
  10. */
  11. /* PMAC implementation by Tom St Denis */
  12. #include "mycrypt.h"
  13. #ifdef PMAC
  14. int pmac_test(void)
  15. {
  16. #if !defined(LTC_TEST)
  17. return CRYPT_NOP;
  18. #else
  19. static const struct {
  20. int msglen;
  21. unsigned char key[16], msg[34], tag[16];
  22. } tests[] = {
  23. /* PMAC-AES-128-0B */
  24. {
  25. 0,
  26. /* key */
  27. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  28. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
  29. /* msg */
  30. { 0x00 },
  31. /* tag */
  32. { 0x43, 0x99, 0x57, 0x2c, 0xd6, 0xea, 0x53, 0x41,
  33. 0xb8, 0xd3, 0x58, 0x76, 0xa7, 0x09, 0x8a, 0xf7 }
  34. },
  35. /* PMAC-AES-128-3B */
  36. {
  37. 3,
  38. /* key */
  39. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  40. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
  41. /* msg */
  42. { 0x00, 0x01, 0x02 },
  43. /* tag */
  44. { 0x25, 0x6b, 0xa5, 0x19, 0x3c, 0x1b, 0x99, 0x1b,
  45. 0x4d, 0xf0, 0xc5, 0x1f, 0x38, 0x8a, 0x9e, 0x27 }
  46. },
  47. /* PMAC-AES-128-16B */
  48. {
  49. 16,
  50. /* key */
  51. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  52. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
  53. /* msg */
  54. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  55. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
  56. /* tag */
  57. { 0xeb, 0xbd, 0x82, 0x2f, 0xa4, 0x58, 0xda, 0xf6,
  58. 0xdf, 0xda, 0xd7, 0xc2, 0x7d, 0xa7, 0x63, 0x38 }
  59. },
  60. /* PMAC-AES-128-20B */
  61. {
  62. 20,
  63. /* key */
  64. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  65. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
  66. /* msg */
  67. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  68. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  69. 0x10, 0x11, 0x12, 0x13 },
  70. /* tag */
  71. { 0x04, 0x12, 0xca, 0x15, 0x0b, 0xbf, 0x79, 0x05,
  72. 0x8d, 0x8c, 0x75, 0xa5, 0x8c, 0x99, 0x3f, 0x55 }
  73. },
  74. /* PMAC-AES-128-32B */
  75. {
  76. 32,
  77. /* key */
  78. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  79. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
  80. /* msg */
  81. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  82. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  83. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  84. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f },
  85. /* tag */
  86. { 0xe9, 0x7a, 0xc0, 0x4e, 0x9e, 0x5e, 0x33, 0x99,
  87. 0xce, 0x53, 0x55, 0xcd, 0x74, 0x07, 0xbc, 0x75 }
  88. },
  89. /* PMAC-AES-128-34B */
  90. {
  91. 34,
  92. /* key */
  93. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  94. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
  95. /* msg */
  96. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  97. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  98. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  99. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  100. 0x20, 0x21 },
  101. /* tag */
  102. { 0x5c, 0xba, 0x7d, 0x5e, 0xb2, 0x4f, 0x7c, 0x86,
  103. 0xcc, 0xc5, 0x46, 0x04, 0xe5, 0x3d, 0x55, 0x12 }
  104. }
  105. };
  106. int err, x, idx;
  107. unsigned long len;
  108. unsigned char outtag[MAXBLOCKSIZE];
  109. /* AES can be under rijndael or aes... try to find it */
  110. if ((idx = find_cipher("aes")) == -1) {
  111. if ((idx = find_cipher("rijndael")) == -1) {
  112. return CRYPT_NOP;
  113. }
  114. }
  115. for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
  116. len = sizeof(outtag);
  117. if ((err = pmac_memory(idx, tests[x].key, 16, tests[x].msg, tests[x].msglen, outtag, &len)) != CRYPT_OK) {
  118. return err;
  119. }
  120. if (memcmp(outtag, tests[x].tag, len)) {
  121. #if 0
  122. unsigned long y;
  123. printf("\nTAG:\n");
  124. for (y = 0; y < len; ) {
  125. printf("0x%02x", outtag[y]);
  126. if (y < len-1) printf(", ");
  127. if (!(++y % 8)) printf("\n");
  128. }
  129. #endif
  130. return CRYPT_FAIL_TESTVECTOR;
  131. }
  132. }
  133. return CRYPT_OK;
  134. #endif /* LTC_TEST */
  135. }
  136. #endif /* PMAC_MODE */