t-generaterandom.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* t-generaterandom.c - Regression test.
  2. Copyright (C) 2016 g10 Code GmbH
  3. This file is part of Scute.
  4. Scute is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. Scute is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with Scute; if not, write to the Free Software Foundation,
  14. Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. In addition, as a special exception, g10 Code GmbH gives permission
  16. to link this library: with the Mozilla Foundation's code for
  17. Mozilla (or with modified versions of it that use the same license
  18. as the "Mozilla" code), and distribute the linked executables. You
  19. must obey the GNU General Public License in all respects for all of
  20. the code used other than "Mozilla". If you modify this file, you
  21. may extend this exception to your version of the file, but you are
  22. not obligated to do so. If you do not wish to do so, delete this
  23. exception statement from your version. */
  24. #include <stdio.h>
  25. #include <stdbool.h>
  26. #include "t-support.h"
  27. int
  28. main (int argc, char *argv[])
  29. {
  30. CK_RV err;
  31. CK_SLOT_ID_PTR slots;
  32. CK_ULONG slots_count;
  33. unsigned int i;
  34. (void) argc;
  35. (void) argv;
  36. init_cryptoki ();
  37. err = C_GetSlotList (true, NULL, &slots_count);
  38. fail_if_err (err);
  39. if (slots_count == 0)
  40. {
  41. printf ("Skipping test because no token is present.\n");
  42. return 77;
  43. }
  44. printf ("Number of slots with tokens: %lu\n", slots_count);
  45. slots = malloc (sizeof (CK_SLOT_ID) * slots_count);
  46. if (!slots)
  47. fail_if_err (CKR_HOST_MEMORY);
  48. err = C_GetSlotList (true, slots, &slots_count);
  49. fail_if_err (err);
  50. for (i = 0; i < slots_count; i++)
  51. {
  52. CK_TOKEN_INFO info;
  53. printf ("%2i. Slot ID %lu\n", i, slots[i]);
  54. err = C_GetTokenInfo (slots[i], &info);
  55. fail_if_err (err);
  56. if ((info.flags & CKF_RNG) > 0)
  57. {
  58. CK_SESSION_HANDLE session;
  59. unsigned char buffer[16];
  60. unsigned int j;
  61. printf(" RNG available\n");
  62. err = C_OpenSession (slots[i], CKF_SERIAL_SESSION, NULL, NULL,
  63. &session);
  64. fail_if_err (err);
  65. printf (" Session ID: %lu\n", session);
  66. err = C_GenerateRandom (session, buffer, sizeof(buffer));
  67. fail_if_err (err);
  68. printf (" Random bytes: 0x");
  69. for (j = 0; j < sizeof(buffer); j++)
  70. printf ("%02x", buffer[j]);
  71. printf ("\n");
  72. err = C_CloseSession (session);
  73. fail_if_err (err);
  74. }
  75. else
  76. printf (" No RNG available on token\n");
  77. }
  78. return 0;
  79. }