pubkey_speed.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright (C) 2009 Martin Willi
  3. * HSR Hochschule fuer Technik Rapperswil
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. */
  15. #include <stdio.h>
  16. #include <time.h>
  17. #include <library.h>
  18. #include <utils/debug.h>
  19. #include <credentials/keys/private_key.h>
  20. void start_timing(struct timespec *start)
  21. {
  22. clock_gettime(CLOCK_THREAD_CPUTIME_ID, start);
  23. }
  24. double end_timing(struct timespec *start)
  25. {
  26. struct timespec end;
  27. clock_gettime(CLOCK_THREAD_CPUTIME_ID, &end);
  28. return (end.tv_nsec - start->tv_nsec) / 1000000000.0 +
  29. (end.tv_sec - start->tv_sec) * 1.0;
  30. }
  31. static void usage()
  32. {
  33. printf("usage: pubkey_speed plugins rsa|ecdsa rounds < key\n");
  34. exit(1);
  35. }
  36. int main(int argc, char *argv[])
  37. {
  38. private_key_t *private;
  39. public_key_t *public;
  40. struct timespec timing;
  41. int round, rounds, read;
  42. char buf[8096], *pos = buf;
  43. key_type_t type = KEY_ANY;
  44. signature_scheme_t scheme = SIGN_UNKNOWN;
  45. chunk_t keydata, *sigs, data;
  46. if (argc < 4)
  47. {
  48. usage();
  49. }
  50. rounds = atoi(argv[3]);
  51. if (streq(argv[2], "rsa"))
  52. {
  53. type = KEY_RSA;
  54. scheme = SIGN_RSA_EMSA_PKCS1_SHA1;
  55. }
  56. else if (streq(argv[2], "ecdsa"))
  57. {
  58. type = KEY_ECDSA;
  59. }
  60. else
  61. {
  62. usage();
  63. }
  64. library_init(NULL, "pubkey_speed");
  65. lib->plugins->load(lib->plugins, argv[1]);
  66. atexit(library_deinit);
  67. keydata = chunk_create(buf, 0);
  68. while ((read = fread(pos, 1, sizeof(buf) - (pos - buf), stdin)))
  69. {
  70. pos += read;
  71. keydata.len += read;
  72. }
  73. private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, type,
  74. BUILD_BLOB_PEM, keydata, BUILD_END);
  75. if (!private)
  76. {
  77. printf("parsing private key failed.\n");
  78. exit(1);
  79. }
  80. if (type == KEY_ECDSA)
  81. {
  82. switch (private->get_keysize(private))
  83. {
  84. case 256:
  85. scheme = SIGN_ECDSA_256;
  86. break;
  87. case 384:
  88. scheme = SIGN_ECDSA_384;
  89. break;
  90. case 521:
  91. scheme = SIGN_ECDSA_521;
  92. break;
  93. default:
  94. printf("%d bit ECDSA private key size not supported",
  95. private->get_keysize(private));
  96. exit(1);
  97. }
  98. }
  99. printf("%4d bit %N: ", private->get_keysize(private),
  100. key_type_names, type);
  101. sigs = malloc(sizeof(chunk_t) * rounds);
  102. data = chunk_from_chars(0x01,0x02,0x03,0x04,0x05,0x06,0x07);
  103. start_timing(&timing);
  104. for (round = 0; round < rounds; round++)
  105. {
  106. if (!private->sign(private, scheme, NULL, data, &sigs[round]))
  107. {
  108. printf("creating signature failed\n");
  109. exit(1);
  110. }
  111. };
  112. printf("sign()/s: %8.1f ", rounds / end_timing(&timing));
  113. public = private->get_public_key(private);
  114. if (!public)
  115. {
  116. printf("extracting public key failed\n");
  117. exit(1);
  118. }
  119. start_timing(&timing);
  120. for (round = 0; round < rounds; round++)
  121. {
  122. if (!public->verify(public, scheme, NULL, data, sigs[round]))
  123. {
  124. printf("signature verification failed\n");
  125. exit(1);
  126. }
  127. }
  128. printf("verify()/s: %8.1f\n", rounds / end_timing(&timing));
  129. public->destroy(public);
  130. private->destroy(private);
  131. for (round = 0; round < rounds; round++)
  132. {
  133. free(sigs[round].ptr);
  134. }
  135. free(sigs);
  136. return 0;
  137. }