encapsulation.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * encapsulation.c
  3. *
  4. * Created on: May 4, 2018
  5. * Author: vader
  6. */
  7. #include <limits.h>
  8. #include "../include/encapsulation.h"
  9. /*
  10. * encapsulation:
  11. * This function produces the ciphert_text given the shared secret and
  12. * public key
  13. *
  14. * Params:
  15. * cipher_text: Provide an array that can hold the size of the ciphertext
  16. * secret_shared: Provide the shared secret
  17. * public_key: Provide the public_key
  18. */
  19. int encapsulation(unsigned char *ciphert_text, unsigned char *secret_shared,
  20. const unsigned char *public_key) {
  21. int result;
  22. matrix *M = make_matrix((code_length - ((signature_block_size * pol_deg) * extension)), code_length);
  23. recover_public_key_from_array(public_key, M);
  24. result = encrypt(ciphert_text, secret_shared, M);
  25. free_matrix(M);
  26. M = NULL;
  27. return result;
  28. }
  29. int encrypt(unsigned char *ciphert_text, unsigned char *secret_shared,
  30. matrix *G) {
  31. int i;
  32. unsigned char m[code_dimension] = { 0 }; //[k_prime] = { 0 };
  33. unsigned char d[k_prime] = { 0 };
  34. unsigned char error_array[code_length] = { 0 };
  35. unsigned char sigma[(code_dimension - k_sec)] = { 0 };
  36. unsigned char hash_sigma[code_length] = { 0 };
  37. unsigned char u[code_dimension] = { 0 };
  38. gf c[code_length] = { 0 };
  39. unsigned char r[code_dimension] = { 0 };
  40. unsigned char K[ss_length] = { 0 };
  41. PRINT_DEBUG_ENCAP("Generation Random M: \n");
  42. random_m(m);
  43. // Only required for DAGs_1 at the moment but do not want to hard code it.
  44. if (F_q_size < UCHAR_MAX){
  45. for (i = 0; i < k_prime; i++) {
  46. m[i] = m[i] % F_q_size;
  47. }
  48. }
  49. #ifdef DEBUG_ENCAP
  50. for (i = 0; i < k_prime; i++) {
  51. PRINT_DEBUG_ENCAP(" %" PRIu16 ", ", m[i]);
  52. }
  53. PRINT_DEBUG_ENCAP("\nStarting hashing: \n");
  54. #endif
  55. #if defined(DAGS_3) || defined(DAGS_5)
  56. shake256(r, code_dimension, m, k_prime);
  57. shake256(d, k_prime, m, k_prime);
  58. #else
  59. shake128(r, code_dimension, m, k_prime);
  60. shake128(d, k_prime, m, k_prime);
  61. #endif
  62. // Type conversion
  63. if (F_q_size < UCHAR_MAX) {
  64. for (i = 0; i < k_prime; i++) {
  65. d[i] = d[i] & (F_q_size - 1);
  66. }
  67. for (i = 0; i < code_dimension; i++) {
  68. r[i] = r[i] & (F_q_size - 1);
  69. }
  70. }
  71. PRINT_DEBUG_ENCAP("Generating sigma and rho: \n");
  72. memcpy(sigma, &r[k_sec], code_dimension - k_sec);
  73. PRINT_DEBUG_ENCAP("Expanding m: \n");
  74. memcpy(u, r, k_sec );
  75. memcpy(&u[k_sec], m, code_dimension - k_sec);
  76. PRINT_DEBUG_ENCAP("Generating error_array: \n");
  77. #if defined(DAGS_3) || defined(DAGS_5)
  78. shake256(hash_sigma, code_length, sigma, k_prime);
  79. #else
  80. shake128(hash_sigma, code_length, sigma, k_prime);
  81. #endif
  82. random_e(hash_sigma, error_array);
  83. #ifdef DEBUG_ENCAP
  84. PRINT_DEBUG_ENCAP("message:\n");
  85. for (i = 0; i < code_dimension - k_sec; i++)
  86. PRINT_DEBUG_ENCAP(" %" PRIu16 ", ", u[i]);
  87. PRINT_DEBUG_ENCAP("\nEncaps_error_array:\n");
  88. for (i = 0; i < code_length; i++) {
  89. printf(" %" PRIu16 ", ", error_array[i]);
  90. }
  91. PRINT_DEBUG_ENCAP("\n");
  92. #endif
  93. PRINT_DEBUG_ENCAP("Computing m*G: \n");
  94. multiply_vector_matrix(u, G, c);//c = message*G
  95. PRINT_DEBUG_ENCAP("Computing (m*G) + error: \n");
  96. for (i = 0; i < code_length; i++) {
  97. ciphert_text[i] = gf_add(c[i] , error_array[i]); //c + error
  98. }
  99. memcpy(&ciphert_text[code_length], d, k_prime);
  100. #ifdef DEBUG_ENCAP
  101. for (i = 0; i < code_length; i++)
  102. PRINT_DEBUG_ENCAP(" %" PRIu16 ", ", ciphert_text[i]);
  103. PRINT_DEBUG_ENCAP("|\nHashing (m*G) + error: \n");
  104. #endif
  105. #if defined(DAGS_3) || defined(DAGS_5)
  106. shake256(K, ss_length, m, k_prime);
  107. #else
  108. shake128(K, ss_length, m, k_prime);
  109. #endif
  110. memcpy(secret_shared, K, ss_length);
  111. return EXIT_SUCCESS;
  112. }