crypto.cpp 6.7 KB

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