main.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* Copyright © 2012, Intel Corporation. All rights reserved.
  2. Redistribution and use in source and binary forms, with or without
  3. modification, are permitted provided that the following conditions are met:
  4. - Redistributions of source code must retain the above copyright notice,
  5. this list of conditions and the following disclaimer.
  6. - Redistributions in binary form must reproduce the above copyright
  7. notice, this list of conditions and the following disclaimer in the
  8. documentation and/or other materials provided with the distribution.
  9. - Neither the name of Intel Corporation nor the names of its contributors
  10. may be used to endorse or promote products derived from this software
  11. without specific prior written permission.
  12. THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION "AS IS" AND ANY EXPRESS OR
  13. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  14. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  15. EVENT SHALL INTEL CORPORATION BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  16. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  17. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  18. BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  19. IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  20. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  21. POSSIBILITY OF SUCH DAMAGE. */
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include "rdrand.h"
  25. #define BUFFSIZE 1275
  26. int main()
  27. {
  28. int r;
  29. uint16_t u16;
  30. uint32_t u32;
  31. uint64_t u64;
  32. uint32_t array32[10];
  33. uint64_t array64[10];
  34. unsigned char buffer[BUFFSIZE];
  35. r = rdrand_16(&u16, 10);
  36. if (r != RDRAND_SUCCESS ) printf("rdrand instruction failed with code %d\n", r);
  37. r = rdrand_32(&u32, 10);
  38. if (r != RDRAND_SUCCESS ) printf("rdrand instruction failed with code %d\n", r);
  39. r = rdrand_64(&u64, 10);
  40. if (r != RDRAND_SUCCESS ) printf("rdrand instruction failed with code %d\n", r);
  41. printf("uint16: %u\n", u16);
  42. printf("uint32: %u\n", u32);
  43. printf("uint64: %llu\n", (unsigned long long) u64);
  44. r = rdrand_get_n_32(10, array32);
  45. if ( r == RDRAND_SUCCESS ) {
  46. int i;
  47. printf("\n10 uint32's:\n");
  48. for (i= 0; i< 10; ++i) {
  49. printf("%u\n", array32[i]);
  50. }
  51. } else printf("rdrand instruction failed with code %d\n", r);
  52. r = rdrand_get_n_64(10, array64);
  53. if ( r == RDRAND_SUCCESS ) {
  54. int i;
  55. printf("\n10 uint64's:\n");
  56. for (i= 0; i< 10; ++i) {
  57. printf("%llu\n", (unsigned long long) array64[i]);
  58. }
  59. } else printf("rdrand instruction failed with code %d\n", r);
  60. memset(buffer, 0, BUFFSIZE);
  61. r = rdrand_get_bytes(BUFFSIZE, buffer);
  62. if ( r == RDRAND_SUCCESS ) {
  63. int i, j;
  64. printf("\nBuffer of %ld bytes:\n", (long) BUFFSIZE);
  65. j= 0;
  66. for (i= 0; i< BUFFSIZE; ++i)
  67. {
  68. printf("%02x ", buffer[i]);
  69. ++j;
  70. if ( j == 16 ) {
  71. j= 0;
  72. printf("\n");
  73. } else if ( j == 8 ) printf(" ");
  74. }
  75. printf("\n");
  76. } else printf("rdrand instruction failed with code %d\n", r);
  77. }