選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

test.c 2.3 KiB

1年前
1年前
1年前
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include "api.h"
  2. #include "randombytes.h"
  3. #include "hal.h"
  4. #include <string.h>
  5. #define NTESTS 10
  6. const uint8_t canary[8] = {
  7. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF
  8. };
  9. /* allocate a bit more for all keys and messages and
  10. * make sure it is not touched by the implementations.
  11. */
  12. static void write_canary(uint8_t *d) {
  13. for (size_t i = 0; i < 8; i++) {
  14. d[i] = canary[i];
  15. }
  16. }
  17. static int check_canary(const uint8_t *d) {
  18. for (size_t i = 0; i < 8; i++) {
  19. if (d[i] != canary[i]) {
  20. return -1;
  21. }
  22. }
  23. return 0;
  24. }
  25. static int test_keys(void)
  26. {
  27. unsigned char key_a[CRYPTO_BYTES+16], key_b[CRYPTO_BYTES+16];
  28. unsigned char pk[CRYPTO_PUBLICKEYBYTES+16];
  29. unsigned char sendb[CRYPTO_CIPHERTEXTBYTES+16];
  30. unsigned char sk_a[CRYPTO_SECRETKEYBYTES+16];
  31. write_canary(key_a); write_canary(key_a+sizeof(key_a)-8);
  32. write_canary(key_b); write_canary(key_b+sizeof(key_b)-8);
  33. write_canary(pk); write_canary(pk+sizeof(pk)-8);
  34. write_canary(sendb); write_canary(sendb+sizeof(sendb)-8);
  35. write_canary(sk_a); write_canary(sk_a+sizeof(sk_a)-8);
  36. int i;
  37. for(i=0; i<NTESTS; i++)
  38. {
  39. //Alice generates a public key
  40. crypto_kem_keypair(pk+8, sk_a+8);
  41. hal_send_str("DONE key pair generation!");
  42. //Bob derives a secret key and creates a response
  43. crypto_kem_enc(sendb+8, key_b+8, pk+8);
  44. hal_send_str("DONE encapsulation!");
  45. //Alice uses Bobs response to get her secret key
  46. crypto_kem_dec(key_a+8, sendb+8, sk_a+8);
  47. hal_send_str("DONE decapsulation!");
  48. if(memcmp(key_a+8, key_b+8, CRYPTO_BYTES))
  49. {
  50. hal_send_str("ERROR KEYS\n");
  51. }
  52. else if(check_canary(key_a) || check_canary(key_a+sizeof(key_a)-8) ||
  53. check_canary(key_b) || check_canary(key_b+sizeof(key_b)-8) ||
  54. check_canary(pk) || check_canary(pk+sizeof(pk)-8) ||
  55. check_canary(sendb) || check_canary(sendb+sizeof(sendb)-8) ||
  56. check_canary(sk_a) || check_canary(sk_a+sizeof(sk_a)-8))
  57. {
  58. hal_send_str("ERROR canary overwritten\n");
  59. }
  60. else
  61. {
  62. hal_send_str("OK KEYS\n");
  63. }
  64. }
  65. return 0;
  66. }
  67. int main(void)
  68. {
  69. hal_setup(CLOCK_FAST);
  70. int i;
  71. // marker for automated testing
  72. for(i=0;i<10;i++)
  73. hal_send_str("==========================");
  74. test_keys();
  75. hal_send_str("## That's all folks! #");
  76. while(1);
  77. return 0;
  78. }