2
0

cmac-test.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* cmac-test.c
  2. *
  3. * Copyright (C) 2006-2024 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #include <wolfssl/options.h>
  22. #include <wolfssl/wolfcrypt/cmac.h>
  23. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  24. #include <sys/mman.h>
  25. #include <hw/inout.h>
  26. #include <sys/iofunc.h>
  27. #include <sys/neutrino.h>
  28. static const byte k256[] =
  29. {
  30. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  31. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  32. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  33. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  34. };
  35. static int createTag(const byte* key, int keySz, byte* msg, int msgSz,
  36. byte* msg2, int msg2Sz)
  37. {
  38. Cmac cmac;
  39. byte tag[AES_BLOCK_SIZE];
  40. word32 i, tagSz;
  41. byte out[48];
  42. word32 outSz;
  43. XMEMSET(tag, 0, sizeof(tag));
  44. tagSz = AES_BLOCK_SIZE;
  45. outSz = 48;
  46. wc_caamCoverKey((byte*)key, keySz, out, &outSz, 0);
  47. if (wc_InitCmac(&cmac, NULL, keySz, WC_CMAC_AES, out) != 0) {
  48. printf("Cmac init error\n");
  49. return -1;
  50. }
  51. if (wc_CmacUpdate(&cmac, msg, msgSz) != 0) {
  52. printf("message update error\n");
  53. return -1;
  54. }
  55. if (msg2Sz > 0) {
  56. if (wc_CmacUpdate(&cmac, msg2, msg2Sz) != 0) {
  57. printf("message2 update error\n");
  58. return -1;
  59. }
  60. }
  61. if (wc_CmacFinal(&cmac, tag, &tagSz) != 0) {
  62. printf("create tag error\n");
  63. return -1;
  64. }
  65. printf("TAG :");
  66. for (i = 0; i < tagSz; i++)
  67. printf("%02X", tag[i]);
  68. printf("\n");
  69. return 0;
  70. }
  71. int main(int argc, char* argv[])
  72. {
  73. uintptr_t virtual_base;
  74. byte msg[256];
  75. byte msg2[256];
  76. int i;
  77. printf("checking out permissions, can we call mmap_device_io?\n");
  78. virtual_base = mmap_device_io(0x00010000, 0x02140000);
  79. if (virtual_base == (uintptr_t)MAP_FAILED) {
  80. perror("mmap_device_io failed for base address ");
  81. }
  82. else {
  83. munmap_device_io(virtual_base, 0x00010000);
  84. }
  85. wolfCrypt_Init();
  86. XMEMSET(msg, 1, sizeof(msg));
  87. XMEMSET(msg2, 9, sizeof(msg2));
  88. for (i = 0; i < 256; i+=8) {
  89. if (createTag(k256, sizeof(k256), msg, i, NULL, 0) != 0)
  90. return -1;
  91. }
  92. wolfCrypt_Cleanup();
  93. return 0;
  94. }