Reference implementations of PQC
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

65 lines
1.7 KiB

  1. #include <stddef.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include "api.h"
  6. #include "randombytes.h"
  7. #define NTESTS 5
  8. static void printbytes(const uint8_t *x, size_t xlen) {
  9. size_t i;
  10. for (i = 0; i < xlen; i++) {
  11. printf("%02x", x[i]);
  12. }
  13. printf("\n");
  14. }
  15. // https://stackoverflow.com/a/1489985/1711232
  16. #define PASTER(x, y) x##_##y
  17. #define EVALUATOR(x, y) PASTER(x, y)
  18. #define NAMESPACE(fun) EVALUATOR(PQCLEAN_NAMESPACE, fun)
  19. #define CRYPTO_BYTES NAMESPACE(CRYPTO_BYTES)
  20. #define CRYPTO_PUBLICKEYBYTES NAMESPACE(CRYPTO_PUBLICKEYBYTES)
  21. #define CRYPTO_SECRETKEYBYTES NAMESPACE(CRYPTO_SECRETKEYBYTES)
  22. #define CRYPTO_CIPHERTEXTBYTES NAMESPACE(CRYPTO_CIPHERTEXTBYTES)
  23. #define crypto_kem_keypair NAMESPACE(crypto_kem_keypair)
  24. #define crypto_kem_enc NAMESPACE(crypto_kem_enc)
  25. #define crypto_kem_dec NAMESPACE(crypto_kem_dec)
  26. int main(void) {
  27. uint8_t key_a[CRYPTO_BYTES], key_b[CRYPTO_BYTES];
  28. uint8_t pk[CRYPTO_PUBLICKEYBYTES];
  29. uint8_t sendb[CRYPTO_CIPHERTEXTBYTES];
  30. uint8_t sk_a[CRYPTO_SECRETKEYBYTES];
  31. int i, j;
  32. for (i = 0; i < NTESTS; i++) {
  33. // Key-pair generation
  34. crypto_kem_keypair(pk, sk_a);
  35. printbytes(pk, CRYPTO_PUBLICKEYBYTES);
  36. printbytes(sk_a, CRYPTO_SECRETKEYBYTES);
  37. // Encapsulation
  38. crypto_kem_enc(sendb, key_b, pk);
  39. printbytes(sendb, CRYPTO_CIPHERTEXTBYTES);
  40. printbytes(key_b, CRYPTO_BYTES);
  41. // Decapsulation
  42. crypto_kem_dec(key_a, sendb, sk_a);
  43. printbytes(key_a, CRYPTO_BYTES);
  44. for (j = 0; j < CRYPTO_BYTES; j++) {
  45. if (key_a[j] != key_b[j]) {
  46. printf("ERROR\n");
  47. return -1;
  48. }
  49. }
  50. }
  51. return 0;
  52. }