Openssl_crypto.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 <string.h>
  9. EVP_CIPHER_CTX* ctx;
  10. int generate_sha256_hash(const unsigned char *message, size_t message_len, unsigned char *digest)
  11. {
  12. EVP_MD_CTX *mdctx; unsigned int digest_len;
  13. if((mdctx = EVP_MD_CTX_create()) == NULL)
  14. return 0x1;
  15. if(EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL) != 1)
  16. return 0x2;
  17. if(EVP_DigestUpdate(mdctx, message, message_len) != 1)
  18. return 0x3;
  19. if(EVP_DigestFinal_ex(mdctx, digest, &digest_len) != 1)
  20. return 0x4;
  21. if(digest_len != 32)
  22. return 0x5;
  23. EVP_MD_CTX_destroy(mdctx);
  24. return 0;
  25. }
  26. int ecdh_key_gen(unsigned char* public_key_x, unsigned char* public_key_y, unsigned char* private_key)
  27. {
  28. EC_KEY * keypair = NULL;
  29. EC_GROUP* ecdh_group = NULL;
  30. const EC_POINT *public_key_point = NULL;
  31. const BIGNUM *private_key_bignum = NULL;
  32. BIGNUM *public_key_x_bignum = NULL;
  33. BIGNUM *public_key_y_bignum = NULL;
  34. int ret;
  35. keypair = EC_KEY_new();
  36. if (NULL == keypair) {
  37. return 0x42;
  38. }
  39. ecdh_group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
  40. if (NULL == ecdh_group) {
  41. EC_KEY_free(keypair);
  42. return 0x0f;
  43. }
  44. if (0 == EC_KEY_set_group (keypair, ecdh_group)) {
  45. EC_KEY_free(keypair);
  46. EC_GROUP_free(ecdh_group);
  47. return 0x43;
  48. }
  49. // TODO: Check return values.
  50. EC_KEY_set_asn1_flag(keypair, OPENSSL_EC_NAMED_CURVE);
  51. // Generating keypair
  52. if (EC_KEY_generate_key(keypair) == 0) {
  53. EC_KEY_free(keypair);
  54. EC_GROUP_free(ecdh_group);
  55. return 0x01;
  56. }
  57. // Extracting public key point from keypair. TODO: Note that it seems that this pointer does not need to be freed separately from the keypair pointer.
  58. public_key_point = EC_KEY_get0_public_key(keypair);
  59. if (NULL == public_key_point)
  60. {
  61. EC_KEY_free(keypair);
  62. EC_GROUP_free(ecdh_group);
  63. return 0x04;
  64. }
  65. // Extracting private key bignum from keypair. TODO: Note that it seems that this pointer does not need to be freed separately from the keypair pointer.
  66. private_key_bignum = EC_KEY_get0_private_key(keypair);
  67. if (NULL == private_key_bignum)
  68. {
  69. EC_KEY_free(keypair);
  70. EC_GROUP_free(ecdh_group);
  71. return 0x05;
  72. }
  73. // Converting private key bignum to string (in big-endian format).
  74. if(!BN_bn2bin(private_key_bignum, private_key))
  75. {
  76. EC_KEY_free(keypair);
  77. EC_GROUP_free(ecdh_group);
  78. return 0x06;
  79. }
  80. public_key_x_bignum = BN_new();
  81. public_key_y_bignum = BN_new();
  82. if (NULL == public_key_x_bignum || NULL == public_key_y_bignum) {
  83. EC_KEY_free(keypair);
  84. EC_GROUP_free(ecdh_group);
  85. return 0x42;//SGX_ERROR_OUT_OF_MEMORY;
  86. }
  87. // Extracting public key bignums from public key points.
  88. if (!EC_POINT_get_affine_coordinates_GFp(ecdh_group, public_key_point, public_key_x_bignum, public_key_y_bignum, NULL))
  89. {
  90. BN_clear_free(public_key_x_bignum);
  91. BN_clear_free(public_key_y_bignum);
  92. EC_KEY_free(keypair);
  93. EC_GROUP_free(ecdh_group);
  94. return 0x07;
  95. }
  96. // Converting public key x bignum to string (in big-endian format) and setting first output argument.
  97. ret = BN_bn2bin(public_key_x_bignum, public_key_x);
  98. BN_clear_free(public_key_x_bignum);
  99. if(ret == 0)
  100. {
  101. BN_clear_free(public_key_y_bignum);
  102. EC_KEY_free(keypair);
  103. EC_GROUP_free(ecdh_group);
  104. return 0x08;
  105. }
  106. // Converting public key y bignum to string (in big-endian format) and setting second output argument.
  107. ret = BN_bn2bin(public_key_y_bignum, public_key_y);
  108. BN_clear_free(public_key_y_bignum);
  109. EC_KEY_free(keypair);
  110. EC_GROUP_free(ecdh_group);
  111. if(ret == 0)
  112. return 0x09;
  113. return 0;
  114. }
  115. unsigned long check_ecdh_public_key(unsigned char* public_key_x, unsigned char* public_key_y) //, unsigned char* priv_key, unsigned char* shared_key_x, unsigned char* shared_key_y)
  116. {
  117. EC_GROUP *ec_group = NULL;
  118. EC_POINT *public_key_point = NULL;
  119. BIGNUM *public_key_x_bignum = NULL;
  120. BIGNUM *public_key_y_bignum = NULL;
  121. ec_group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
  122. if (NULL == ec_group) {
  123. return 0x0f;
  124. }
  125. public_key_point = EC_POINT_new(ec_group);
  126. if (NULL == public_key_point) {
  127. EC_GROUP_free(ec_group);
  128. return 0x42;
  129. }
  130. public_key_x_bignum = BN_bin2bn(public_key_x, 32, NULL);
  131. if (NULL == public_key_x_bignum) {
  132. EC_POINT_free(public_key_point);
  133. EC_GROUP_free(ec_group);
  134. return 0xfd;
  135. }
  136. // converts the y value of the point, represented as positive integer in big-endian into a BIGNUM
  137. public_key_y_bignum = BN_bin2bn(public_key_y, 32, NULL);
  138. if (NULL == public_key_y_bignum) {
  139. BN_clear_free(public_key_x_bignum);
  140. EC_POINT_free(public_key_point);
  141. EC_GROUP_free(ec_group);
  142. return 0xfe;
  143. }
  144. // sets point based on x,y coordinates
  145. int ret = EC_POINT_set_affine_coordinates_GFp(ec_group, public_key_point, public_key_x_bignum, public_key_y_bignum, NULL);
  146. BN_clear_free(public_key_x_bignum);
  147. BN_clear_free(public_key_y_bignum);
  148. if (1 != ret) {
  149. EC_POINT_free(public_key_point);
  150. EC_GROUP_free(ec_group);
  151. return ERR_get_error();
  152. }
  153. // checks if point is on curve
  154. //
  155. int ret_point_on_curve = EC_POINT_is_on_curve(ec_group, public_key_point, NULL);
  156. EC_POINT_free(public_key_point);
  157. EC_GROUP_free(ec_group);
  158. if (1 != ret_point_on_curve) {
  159. return ret_point_on_curve << 4; //ret_point_on_curve;// break;
  160. }
  161. return 0;
  162. }
  163. unsigned long compute_ecdh_shared_key(unsigned char* given_public_key_x, unsigned char* given_public_key_y, unsigned char* own_private_key, unsigned char* derived_key)
  164. {
  165. unsigned long long_ret = 0;
  166. EC_GROUP *ec_group = NULL;
  167. EC_POINT *given_public_key_point = NULL;
  168. BIGNUM *given_public_key_x_bignum = NULL;
  169. BIGNUM *given_public_key_y_bignum = NULL;
  170. EC_KEY *own_keypair = NULL;
  171. BIGNUM *own_private_key_bignum = NULL;
  172. int shared_key_len = 32;
  173. unsigned char shared_key[32];
  174. ec_group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
  175. if (NULL == ec_group) {
  176. return 0x0f;
  177. }
  178. given_public_key_point = EC_POINT_new(ec_group);
  179. if (NULL == given_public_key_point) {
  180. EC_GROUP_free(ec_group);
  181. return 0x42;
  182. }
  183. given_public_key_x_bignum = BN_bin2bn(given_public_key_x, 32, NULL);
  184. if (NULL == given_public_key_x_bignum) {
  185. EC_POINT_free(given_public_key_point);
  186. EC_GROUP_free(ec_group);
  187. return 0xfd;
  188. }
  189. // converts the y value of the point, represented as positive integer in big-endian into a BIGNUM
  190. given_public_key_y_bignum = BN_bin2bn(given_public_key_y, 32, NULL);
  191. if (NULL == given_public_key_y_bignum) {
  192. BN_clear_free(given_public_key_x_bignum); // TODO: You should probably not be freeing it here - since ya dont own it?
  193. EC_POINT_free(given_public_key_point);
  194. EC_GROUP_free(ec_group);
  195. return 0xfe;
  196. }
  197. // sets point based on x,y coordinates
  198. int ret = EC_POINT_set_affine_coordinates_GFp(ec_group, given_public_key_point, given_public_key_x_bignum, given_public_key_y_bignum, NULL);
  199. BN_clear_free(given_public_key_x_bignum);
  200. BN_clear_free(given_public_key_y_bignum);
  201. if (1 != ret) {
  202. EC_POINT_free(given_public_key_point);
  203. EC_GROUP_free(ec_group);
  204. return ERR_get_error();
  205. }
  206. // checks if point is on curve
  207. //
  208. int ret_point_on_curve = EC_POINT_is_on_curve(ec_group, given_public_key_point, NULL);
  209. if (1 != ret_point_on_curve) {
  210. EC_POINT_free(given_public_key_point);
  211. EC_GROUP_free(ec_group);
  212. return ret_point_on_curve << 4; //ret_point_on_curve;// break;
  213. }
  214. // create empty shared key BN
  215. //
  216. own_keypair = EC_KEY_new();
  217. if (own_keypair == NULL) {
  218. EC_POINT_free(given_public_key_point);
  219. EC_GROUP_free(ec_group);
  220. return 0x42;//ret = SGX_ERROR_OUT_OF_MEMORY;
  221. }
  222. // init private key group (set curve)
  223. //
  224. if (EC_KEY_set_group (own_keypair, ec_group) != 1) {
  225. EC_KEY_free(own_keypair);
  226. EC_POINT_free(given_public_key_point);
  227. EC_GROUP_free(ec_group);
  228. return ERR_get_error(); //break;
  229. }
  230. // init private key with BN value
  231. //
  232. own_private_key_bignum = BN_bin2bn(own_private_key, 32, NULL);
  233. if(own_private_key_bignum == NULL) {
  234. EC_POINT_free(given_public_key_point);
  235. EC_GROUP_free(ec_group);
  236. EC_KEY_free(own_keypair);
  237. return 0x44;
  238. }
  239. long_ret = EC_KEY_set_private_key(own_keypair, own_private_key_bignum);
  240. BN_clear_free(own_private_key_bignum);
  241. if (long_ret != 1) {
  242. EC_POINT_free(given_public_key_point);
  243. EC_GROUP_free(ec_group);
  244. EC_KEY_free(own_keypair);
  245. return ERR_get_error();
  246. }
  247. // calculate shared dh key
  248. //
  249. shared_key_len = ECDH_compute_key(shared_key, 32, given_public_key_point, own_keypair, NULL);
  250. if (shared_key_len <= 0) {
  251. long_ret = ERR_get_error();
  252. }
  253. else
  254. long_ret = generate_sha256_hash(shared_key, 32, derived_key);
  255. EC_POINT_free(given_public_key_point);
  256. EC_GROUP_free(ec_group);
  257. EC_KEY_free(own_keypair);
  258. return long_ret;
  259. }
  260. int compute_ecdsa_signature(unsigned char* signature_data, uint32_t signature_data_length, unsigned char* own_private_key, unsigned char* signature)
  261. {
  262. unsigned long long_ret = 0;
  263. EC_GROUP *ec_group = NULL;
  264. EC_POINT *given_public_key_point = NULL;
  265. EC_KEY *own_keypair = NULL;
  266. BIGNUM *own_private_key_bignum = NULL;
  267. unsigned char effective_signature_data[32];
  268. ECDSA_SIG *openssl_signature;
  269. unsigned char** temp = NULL;
  270. uint32_t signature_length;
  271. // &signature_length will not be NULL
  272. ec_group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
  273. if (NULL == ec_group) {
  274. return 0x0f;
  275. }
  276. own_keypair = EC_KEY_new();
  277. if (own_keypair == NULL) {
  278. EC_GROUP_free(ec_group);
  279. return 0x42;//ret = SGX_ERROR_OUT_OF_MEMORY;
  280. }
  281. // init private key group (set curve)
  282. //
  283. if (EC_KEY_set_group (own_keypair, ec_group) != 1) {
  284. EC_KEY_free(own_keypair);
  285. EC_GROUP_free(ec_group);
  286. return ERR_get_error(); //break;
  287. }
  288. // init private key with BN value
  289. //
  290. own_private_key_bignum = BN_bin2bn(own_private_key, 32, NULL);
  291. if(own_private_key_bignum == NULL) {
  292. EC_GROUP_free(ec_group);
  293. EC_KEY_free(own_keypair);
  294. return 0x44;
  295. }
  296. long_ret = EC_KEY_set_private_key(own_keypair, own_private_key_bignum);
  297. BN_clear_free(own_private_key_bignum);
  298. if (long_ret != 1) {
  299. EC_GROUP_free(ec_group);
  300. EC_KEY_free(own_keypair);
  301. return ERR_get_error();
  302. }
  303. long_ret = generate_sha256_hash(signature_data, signature_data_length, effective_signature_data);
  304. if(long_ret != 0)
  305. {
  306. EC_GROUP_free(ec_group);
  307. EC_KEY_free(own_keypair);
  308. return long_ret;
  309. }
  310. openssl_signature = ECDSA_do_sign(effective_signature_data, 32, own_keypair);
  311. if(openssl_signature==NULL)
  312. {
  313. EC_GROUP_free(ec_group);
  314. EC_KEY_free(own_keypair);
  315. return 0x64;
  316. }
  317. signature_length = i2d_ECDSA_SIG(openssl_signature, temp);
  318. if(signature_length==0)
  319. {
  320. EC_GROUP_free(ec_group);
  321. EC_KEY_free(own_keypair);
  322. return 0x77;
  323. }
  324. signature_length=i2d_ECDSA_SIG(openssl_signature, &signature);
  325. if(signature_length==0)
  326. {
  327. EC_GROUP_free(ec_group);
  328. EC_KEY_free(own_keypair);
  329. return 0x79;
  330. }
  331. return 0;
  332. }
  333. // Code adapted from here: https://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption
  334. int aes_gcm_256(int enc, unsigned char *key, unsigned char *iv, unsigned char* plaintext, uint32_t plaintext_len, unsigned char *ciphertext, uint32_t* op_ciphertext_len, unsigned char* tag)
  335. {
  336. int len;
  337. int ciphertext_len;
  338. int reset_return;
  339. if(ctx == NULL)
  340. {
  341. // Create and initialise the context //
  342. if(!(ctx = EVP_CIPHER_CTX_new())) { return 0x1; }
  343. }
  344. // Initialise the encryption operation. //
  345. if(1 != EVP_CipherInit_ex(ctx, EVP_aes_256_gcm(), NULL, key, iv, enc))
  346. {
  347. reset_return = EVP_CIPHER_CTX_reset(ctx);
  348. if(reset_return != 1)
  349. return 0xf2;
  350. return 0x2;
  351. }
  352. // Provide the message to be encrypted, and obtain the encrypted output. * EVP_EncryptUpdate can be called multiple times if necessary
  353. //
  354. if(1 != EVP_CipherUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
  355. {
  356. reset_return = EVP_CIPHER_CTX_reset(ctx);
  357. if(1 != reset_return)
  358. return 0xF3;
  359. return 0x3;
  360. }
  361. ciphertext_len = len;
  362. if(enc == 0)
  363. {
  364. if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, tag))
  365. {
  366. reset_return = EVP_CIPHER_CTX_reset(ctx);
  367. if(1 != reset_return)
  368. return 0xF5;
  369. return 0x5;
  370. }
  371. }
  372. // Finalise the encryption. Normally ciphertext bytes may be written at this stage, but this does not occur in GCM mode
  373. //
  374. // TODO: ^^^ Why the heck does it not occur in GCM mode ?
  375. if(1 != EVP_CipherFinal_ex(ctx, ciphertext + len, &len))
  376. {
  377. reset_return = EVP_CIPHER_CTX_reset(ctx);
  378. if(1 != reset_return)
  379. return 0xF4;
  380. return 0x4;
  381. }
  382. ciphertext_len += len;
  383. // Get the tag
  384. if(enc == 1)
  385. {
  386. if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag))
  387. {
  388. reset_return = EVP_CIPHER_CTX_reset(ctx);
  389. if(1 != reset_return)
  390. return 0xF5;
  391. return 0x5;
  392. }
  393. }
  394. // Clean up //
  395. if(1 != EVP_CIPHER_CTX_reset(ctx))
  396. {
  397. return 0xF0;
  398. }
  399. *op_ciphertext_len=ciphertext_len;
  400. //EVP_CIPHER_CTX_free(ctx); // TODO: memory leaks here - need to free this for erroneous cases too.
  401. return 0;
  402. }
  403. uint32_t base64_decoding_wrapper(unsigned char* src, unsigned char* dest, uint32_t length)
  404. {
  405. int length_with_padding = EVP_DecodeBlock(dest, src, length);
  406. if(length_with_padding == -1)
  407. return length_with_padding;
  408. char* first_equals_character = strstr((char*)src, "=");
  409. if(first_equals_character != NULL)
  410. {
  411. if(first_equals_character == (char*)src + length - 1) // the first equals character is also the last character in the string ==> Only one equals ==> 2 valid bytes, only 1 padding 0
  412. length_with_padding -= 1;
  413. else //if(first_equals_character == src + 38) // assuming that the base64 string is valid (EVP_DecodeBlock would have thrown an error in that case), then first_equals_character == src + 38 ==> Another equals at +29 ==> 1 valid bytes, 2 padding 0s
  414. length_with_padding -= 2;
  415. }
  416. return length_with_padding;
  417. }