crypto.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #include <memory>
  2. using std::unique_ptr;
  3. #include <openssl/err.h>
  4. #include <openssl/bn.h>
  5. #include <openssl/rsa.h>
  6. #include <openssl/evp.h>
  7. #include <openssl/pem.h>
  8. //using BN_ptr = std::unique_ptr<BIGNUM, decltype(&::BN_free)>;
  9. //using RSA_ptr = std::unique_ptr<RSA, decltype(&::RSA_free)>;
  10. EVP_CIPHER_CTX *ctx;
  11. RSA* rsa;
  12. BIGNUM* bn;
  13. //RSA_ptr rsa_signing_keypair; //(RSA_new(), ::RSA_free);
  14. //BN_ptr rsa_bignum;
  15. // assumes that the digest is at least of length 256/8 bytes.
  16. uint32_t generate_sha256_hash(const unsigned char *message, size_t message_len, unsigned char *digest)
  17. {
  18. EVP_MD_CTX *mdctx; unsigned int digest_len;
  19. if((mdctx = EVP_MD_CTX_create()) == NULL)
  20. {
  21. printf("EVP_MD_CTX_create returned NULL - could not create context\n"); fflush(stdout); return 0x1;
  22. }
  23. if(EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL) != 1)
  24. {
  25. printf("EVP_DigestInit_ex returned 0 - could not initialize hash with SHA256\n"); fflush(stdout); return 0x2;
  26. }
  27. if(EVP_DigestUpdate(mdctx, message, message_len) != 1)
  28. {
  29. printf("EVP_DigestUpdate returned 0 - could not compute SHA256 hash\n"); fflush(stdout); return 0x3;
  30. }
  31. if(1 != EVP_DigestFinal_ex(mdctx, digest, &digest_len))
  32. {
  33. printf("EVP_DigestFinal_ex returned 0 - could not finalize SHA256 hash\n"); fflush(stdout); return 0x4;
  34. }
  35. if(digest_len != 32)
  36. {
  37. printf("EVP_DigestFinal_ex returned a digest length of 0x%x instead of 0x20\n", digest_len); fflush(stdout); return 0x5;
  38. }
  39. EVP_MD_CTX_destroy(mdctx);
  40. return 0;
  41. }
  42. //private:
  43. // RSA_ptr rsa(RSA_new(), ::RSA_free);
  44. // BN_ptr bn(BN_new(), ::BN_free);
  45. uint32_t generate_rsa_keypair(FILE* fp, std::string& priv_key_str, std::string& pub_key_str) //, uint8_t* hash)
  46. {
  47. int rc;
  48. rsa=RSA_new();
  49. bn=BN_new();
  50. rc = BN_set_word(bn, 3);
  51. if(rc != 1)
  52. return 0x1;
  53. rc = RSA_generate_key_ex(rsa, 3072, bn, NULL);
  54. if(rc != 1)
  55. return 0x2;
  56. printf("Generated key\n"); fflush(stdout);
  57. /* int pub_key_der_encoded_len, priv_key_der_encoded_len;
  58. unsigned char *pub_key_der, priv_key_der;
  59. pub_key_der = NULL;
  60. pub_key_der_encoded_len = i2d_RSAPublicKey(rsa.get(), (unsigned char**) &pub_key_der);
  61. if (pub_key_der_encoded_len < 0)
  62. return 0x3;
  63. priv_key_der = NULL;
  64. priv_key_der_encoded_len = i2d_RSAPrivateKey(rsa.get(), (unsigned char**) &priv_key_der);
  65. if (priv_key_der_encoded_len < 0)
  66. return 0x4;
  67. printf("Done\n"); fflush(stdout);
  68. // priv_key_str=std::string(priv_key_der, priv_key_der_encoded_len); //, priv_key_der);
  69. // pub_key_str=std::string(pub_key_der, pub_key_der_encoded_len);
  70. */
  71. // BIO* bio_rsa;
  72. // rc = PEM_write_RSA_PUBKEY(fp, rsa);
  73. rc= PEM_write_RSAPrivateKey(fp, rsa, NULL, NULL, 0, NULL, NULL);
  74. if(rc != 1)
  75. return 0x3;
  76. fflush(fp);
  77. // bio_rsa = BIO_new_file("apache_signature_keypair.pem", "w+");
  78. // rc = PEM_write_bio_RSAPublicKey(bio_rsa, rsa.get());
  79. // if(rc != 1)
  80. // return 0x3;
  81. // BIO_flush(bio_rsa); free(bio_rsa);
  82. return 0;
  83. }
  84. uint32_t generate_rsa_keypair_hash(uint8_t* hash)
  85. {
  86. /*
  87. uint32_t return_internal;
  88. const BIGNUM* n_internal_bigendian_struct;
  89. RSA_get0_key(rsa, &n_internal_bigendian_struct, NULL, NULL);
  90. BIGNUM* n_bigendian_struct = BN_dup(n_internal_bigendian_struct);
  91. uint32_t count;
  92. int n_bignum_length=BN_num_bytes(n_bigendian_struct);
  93. unsigned char *n_bigendian = (unsigned char*) malloc(n_bignum_length);
  94. int length_bignum_le = BN_bn2bin(n_bigendian_struct, n_bigendian);
  95. unsigned char* n_littleendian = (unsigned char*) malloc(length_bignum_le);
  96. for(count=0; count<length_bignum_le; count++)
  97. n_littleendian[count] = n_bigendian[length_bignum_le-count-1];
  98. free(n_bigendian);
  99. // unsigned char hash[32];
  100. return_internal=generate_sha256_hash(n_littleendian, length_bignum_le, hash);
  101. free(n_littleendian);
  102. if(return_internal != 0)
  103. { return return_internal ; }// TODO: Memory leak here.
  104. for(count=0;count<32; count++)
  105. printf("%x", hash[count]);
  106. printf("\n");
  107. fflush(stdout);
  108. return return_internal;
  109. */
  110. return 0; //length_bignum_le;
  111. }
  112. void crypto_cleanup()
  113. {
  114. RSA_free(rsa);
  115. BN_free(bn);
  116. EVP_CIPHER_CTX_free(ctx);
  117. }
  118. // Code adapted from here: https://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption
  119. int aes_gcm_128(int enc, unsigned char *key, unsigned char *iv, unsigned char* plaintext, int plaintext_len, unsigned char *ciphertext, uint32_t* op_ciphertext_len, unsigned char* tag)
  120. {
  121. int len;
  122. int ciphertext_len;
  123. if(ctx == NULL)
  124. {
  125. /* Create and initialise the context */
  126. if(!(ctx = EVP_CIPHER_CTX_new())) {
  127. ERR_print_errors_fp(stderr);
  128. fflush(stderr);
  129. return 0x1;
  130. }
  131. }
  132. /* Initialise the encryption operation. */
  133. if(1 != EVP_CipherInit_ex(ctx, EVP_aes_128_gcm(), NULL, key, iv, enc))
  134. {
  135. EVP_CIPHER_CTX_init(ctx);
  136. ERR_print_errors_fp(stderr);
  137. return 0x2;
  138. }
  139. /* Provide the message to be encrypted, and obtain the encrypted output.
  140. * EVP_EncryptUpdate can be called multiple times if necessary
  141. */
  142. if(1 != EVP_CipherUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
  143. {
  144. EVP_CIPHER_CTX_init(ctx);
  145. ERR_print_errors_fp(stderr);
  146. return 0x3;
  147. }
  148. ciphertext_len = len;
  149. if(enc == 0)
  150. {
  151. if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, tag))
  152. {
  153. EVP_CIPHER_CTX_init(ctx);
  154. ERR_print_errors_fp(stderr); fflush(stderr);
  155. return 0x5;
  156. }
  157. }
  158. /* Finalise the encryption. Normally ciphertext bytes may be written at
  159. * this stage, but this does not occur in GCM mode
  160. */
  161. // TODO: ^^^ Why the heck does it not occur in GCM mode ?
  162. if(1 != EVP_CipherFinal_ex(ctx, ciphertext + len, &len))
  163. {
  164. EVP_CIPHER_CTX_init(ctx);
  165. ERR_print_errors_fp(stderr); fflush(stderr);
  166. return 0x4;
  167. }
  168. ciphertext_len += len;
  169. /* Get the tag */
  170. if(enc == 1)
  171. {
  172. if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag))
  173. {
  174. EVP_CIPHER_CTX_init(ctx);
  175. ERR_print_errors_fp(stderr); fflush(stderr);
  176. return 0x5;
  177. }
  178. }
  179. /* Clean up */
  180. EVP_CIPHER_CTX_init(ctx);
  181. *op_ciphertext_len=ciphertext_len;
  182. return 0;
  183. }