Openssl_crypto.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. #include <openssl/ec.h>
  2. #include <openssl/bn.h>
  3. #include <openssl/rsa.h>
  4. #include <openssl/evp.h>
  5. #include <openssl/err.h>
  6. #include <openssl/rand.h>
  7. #include <openssl/ecdh.h>
  8. #include <stdio.h>
  9. int generate_sha256_hash(const unsigned char *message, size_t message_len, unsigned char *digest);
  10. int ec_key_gen(unsigned char* pub_key_x, unsigned char* pub_key_y, unsigned char* priv_key)
  11. {
  12. // unsigned char entropy_buf[ADD_ENTROPY_SIZE] = {0};
  13. // RAND_add(entropy_buf, sizeof(entropy_buf), ADD_ENTROPY_SIZE);
  14. // RAND_seed(entropy_buf, sizeof(entropy_buf));
  15. EC_KEY * ec_key = NULL;
  16. ec_key = EC_KEY_new();
  17. if (NULL == ec_key) {
  18. return 0x42;
  19. }
  20. EC_GROUP* ec_group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
  21. if (NULL == ec_group) {
  22. //EC_KEY_free(ec_key);
  23. return 0x0f;
  24. }
  25. if (0 == EC_KEY_set_group (ec_key, ec_group)) {
  26. EC_KEY_free(ec_key);
  27. EC_GROUP_free(ec_group);
  28. return 0x43; //break;
  29. }
  30. EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);
  31. int ret = EC_KEY_generate_key(ec_key);
  32. if (!ret) {
  33. // printf("EC_KEY_generate_key failure\n");
  34. return 0x01;
  35. }
  36. ///////////////////////// Openssl code ////////////
  37. const EC_POINT *public_k = NULL;
  38. const BIGNUM *private_k = NULL;
  39. public_k = EC_KEY_get0_public_key(ec_key);
  40. if (NULL == public_k)
  41. {
  42. EC_KEY_free(ec_key);
  43. return 0x04;
  44. }
  45. private_k = EC_KEY_get0_private_key(ec_key);
  46. if (NULL == private_k)
  47. {
  48. EC_KEY_free(ec_key);
  49. return 0x05;
  50. }
  51. if(!BN_bn2bin(private_k, priv_key))
  52. {
  53. EC_KEY_free(ec_key);
  54. return 0x06;
  55. }
  56. BIGNUM *pub_k_x = NULL;
  57. BIGNUM *pub_k_y = NULL;
  58. pub_k_x = BN_new();
  59. pub_k_y = BN_new();
  60. if (NULL == pub_k_x || NULL == pub_k_y) {
  61. return 0x42;//SGX_ERROR_OUT_OF_MEMORY;
  62. }
  63. // extract two BNs representing the public key
  64. //
  65. if (!EC_POINT_get_affine_coordinates_GFp(ec_group, public_k, pub_k_x, pub_k_y, NULL))
  66. //EC_KEY_free(ec_key);
  67. {
  68. //printf("%ld\n", ERR_get_error());
  69. BN_clear_free(pub_k_x); BN_clear_free(pub_k_y);
  70. EC_KEY_free(ec_key);
  71. EC_GROUP_free(ec_group);
  72. return 0x07;
  73. }
  74. ret = BN_bn2bin(pub_k_x, pub_key_x);
  75. BN_clear_free(pub_k_x);
  76. if(ret == 0)
  77. {
  78. BN_clear_free(pub_k_y);
  79. EC_KEY_free(ec_key);
  80. EC_GROUP_free(ec_group);
  81. return 0x08;
  82. }
  83. ret = BN_bn2bin(pub_k_y, pub_key_y);
  84. BN_clear_free(pub_k_y);
  85. EC_KEY_free(ec_key);
  86. EC_GROUP_free(ec_group);
  87. if(ret == 0)
  88. return 0x09;
  89. return 0;
  90. }
  91. unsigned long check_key(unsigned char* given_key_x, unsigned char* given_key_y) //, unsigned char* priv_key, unsigned char* shared_key_x, unsigned char* shared_key_y)
  92. {
  93. EC_GROUP* ec_group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
  94. if (NULL == ec_group) {
  95. //EC_KEY_free(ec_key);
  96. return 0x0f;
  97. }
  98. EC_POINT *ec_point = NULL;
  99. BIGNUM *b_x = NULL;
  100. BIGNUM *b_y = NULL;
  101. ec_point = EC_POINT_new(ec_group);
  102. if (NULL == ec_point) {
  103. return 0x42; //retval = SGX_ERROR_OUT_OF_MEMORY;
  104. // break;
  105. }
  106. // converts the x value of the point, represented as positive integer in big-endian into a BIGNUM
  107. b_x = BN_bin2bn(given_key_x, 32, NULL);
  108. if (NULL == b_x) {
  109. EC_POINT_free(ec_point);
  110. // return ERR_get_error();
  111. return 0xfd;
  112. }
  113. // converts the y value of the point, represented as positive integer in big-endian into a BIGNUM
  114. b_y = BN_bin2bn(given_key_y, 32, NULL);
  115. if (NULL == b_y) {
  116. BN_clear_free(b_x);
  117. EC_POINT_free(ec_point);
  118. return 0xfe;
  119. }
  120. // sets point based on x,y coordinates
  121. int ret = EC_POINT_set_affine_coordinates_GFp(ec_group, ec_point, b_x, b_y, NULL);
  122. if (1 != ret) {
  123. BN_clear_free(b_x);
  124. BN_clear_free(b_y);
  125. EC_POINT_free(ec_point);
  126. return ERR_get_error();
  127. }
  128. // checks if point is on curve
  129. //
  130. int ret_point_on_curve = EC_POINT_is_on_curve(ec_group, ec_point, NULL);
  131. if (1 != ret_point_on_curve) {
  132. EC_POINT_free(ec_point);
  133. BN_clear_free(b_x);
  134. BN_clear_free(b_y);
  135. return ret_point_on_curve << 4; //ret_point_on_curve;// break;
  136. }
  137. BN_clear_free(b_x);
  138. BN_clear_free(b_y);
  139. EC_POINT_free(ec_point);
  140. return 0;
  141. }
  142. unsigned long compute_shared_ECDHE_key(unsigned char* given_key_x, unsigned char* given_key_y, unsigned char* priv_key, unsigned char* shared_key, unsigned char* derived_key)
  143. {
  144. // int valid_point = check_key(given_key_x, given_key_y);
  145. // if(valid_point != 0)
  146. // return valid_point;
  147. unsigned long long_ret = 0;
  148. EC_GROUP* ec_group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
  149. if (NULL == ec_group) {
  150. return 0x0f;
  151. }
  152. EC_POINT *given_point = NULL;
  153. BIGNUM *b_x = NULL;
  154. BIGNUM *b_y = NULL;
  155. EC_KEY *private_key = NULL;
  156. given_point = EC_POINT_new(ec_group);
  157. if (NULL == given_point) {
  158. return 0x42; //retval = SGX_ERROR_OUT_OF_MEMORY;
  159. // break;
  160. }
  161. // converts the x value of the point, represented as positive integer in big-endian into a BIGNUM
  162. b_x = BN_bin2bn(given_key_x, 32, NULL);
  163. if (NULL == b_x) {
  164. EC_POINT_free(given_point);
  165. return 0xfd;
  166. }
  167. // converts the y value of the point, represented as positive integer in big-endian into a BIGNUM
  168. b_y = BN_bin2bn(given_key_y, 32, NULL);
  169. if (NULL == b_y) {
  170. EC_POINT_free(given_point);
  171. BN_clear_free(b_x);
  172. return 0xfe;
  173. }
  174. // sets point based on x,y coordinates
  175. int ret = EC_POINT_set_affine_coordinates_GFp(ec_group, given_point, b_x, b_y, NULL);
  176. if (1 != ret) {
  177. EC_POINT_free(given_point);
  178. BN_clear_free(b_x);
  179. BN_clear_free(b_y);
  180. return ERR_get_error();
  181. }
  182. // checks if point is on curve
  183. //
  184. int ret_point_on_curve = EC_POINT_is_on_curve(ec_group, given_point, NULL);
  185. if (1 != ret_point_on_curve) {
  186. EC_POINT_free(given_point);
  187. BN_clear_free(b_x);
  188. BN_clear_free(b_y);
  189. if(ret_point_on_curve == 0)
  190. return 0x43;
  191. else
  192. return ERR_get_error();
  193. }
  194. // create empty shared key BN
  195. //
  196. private_key = EC_KEY_new();
  197. if (private_key == NULL) {
  198. EC_POINT_free(given_point);
  199. BN_clear_free(b_x);
  200. BN_clear_free(b_y);
  201. return 0x42;//ret = SGX_ERROR_OUT_OF_MEMORY;
  202. }
  203. // init private key group (set curve)
  204. //
  205. if (EC_KEY_set_group (private_key, ec_group) != 1) {
  206. EC_POINT_free(given_point);
  207. EC_KEY_free(private_key);
  208. BN_clear_free(b_x);
  209. BN_clear_free(b_y);
  210. return ERR_get_error(); //break;
  211. }
  212. // init private key with BN value
  213. //
  214. BIGNUM *BN_dh_privB = NULL;
  215. BN_dh_privB = BN_bin2bn(priv_key, 32, NULL);
  216. if(BN_dh_privB == NULL) {
  217. EC_POINT_free(given_point);
  218. EC_KEY_free(private_key);
  219. BN_clear_free(b_x);
  220. BN_clear_free(b_y);
  221. return 0x44;
  222. }
  223. if (EC_KEY_set_private_key(private_key, BN_dh_privB) != 1) {
  224. EC_POINT_free(given_point);
  225. EC_KEY_free(private_key);
  226. BN_clear_free(b_x);
  227. BN_clear_free(b_y);
  228. BN_clear_free(BN_dh_privB);
  229. return ERR_get_error();
  230. }
  231. // calculate shared dh key
  232. //
  233. int shared_key_len = 32; //sizeof(sgx_ec256_dh_shared_t);
  234. shared_key_len = ECDH_compute_key(shared_key, 32, given_point, private_key, NULL);
  235. if (shared_key_len <= 0) {
  236. long_ret = ERR_get_error();
  237. }
  238. else
  239. long_ret = generate_sha256_hash(shared_key, 32, derived_key);
  240. BN_clear_free(BN_dh_privB);
  241. BN_clear_free(b_x);
  242. BN_clear_free(b_y);
  243. EC_POINT_free(given_point);
  244. EC_KEY_free(private_key);
  245. return long_ret;
  246. }
  247. int generate_sha256_hash(const unsigned char *message, size_t message_len, unsigned char *digest)
  248. {
  249. EVP_MD_CTX *mdctx; unsigned int digest_len;
  250. if((mdctx = EVP_MD_CTX_create()) == NULL)
  251. {
  252. //printf("EVP_MD_CTX_create returned NULL - could not create context\n"); fflush(stdout);
  253. return 0x1;
  254. }
  255. if(EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL) != 1)
  256. {
  257. //printf("EVP_DigestInit_ex returned 0 - could not initialize hash with SHA256\n"); fflush(stdout);
  258. return 0x2;
  259. }
  260. if(EVP_DigestUpdate(mdctx, message, message_len) != 1)
  261. {
  262. //printf("EVP_DigestUpdate returned 0 - could not compute SHA256 hash\n"); fflush(stdout);
  263. return 0x3;
  264. }
  265. if(1 != EVP_DigestFinal_ex(mdctx, digest, &digest_len))
  266. {
  267. //printf("EVP_DigestFinal_ex returned 0 - could not finalize SHA256 hash\n"); fflush(stdout);
  268. return 0x4;
  269. }
  270. if(digest_len != 32)
  271. {
  272. // printf("EVP_DigestFinal_ex returned a digest length of 0x%x instead of 0x20\n", digest_len); fflush(stdout);
  273. return 0x5;
  274. }
  275. EVP_MD_CTX_destroy(mdctx);
  276. return 0;
  277. }