aboutsummaryrefslogtreecommitdiff
path: root/store/works/life/information-security-experiment/rsa.cpp
blob: 5b88284d679118b3bb85ffcbf9f4a75130ef9318 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <openssl/evp.h>
#include <openssl/rsa.h>

#include <iomanip>
#include <iostream>
#include <vector>

int main() {

  auto ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, nullptr);
  EVP_PKEY *pkey = NULL;
  EVP_PKEY_keygen_init(ctx);
  EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 2048);
  EVP_PKEY_keygen(ctx, &pkey);
  EVP_PKEY_CTX_free(ctx);
  ctx = nullptr;

  unsigned char cleartext[] = "abc";
  int cleartext_size = sizeof(cleartext) / sizeof(cleartext[0]);

  EVP_MD_CTX *md_ctx = EVP_MD_CTX_create();
  EVP_DigestInit_ex(md_ctx, EVP_sha256(), NULL);
  EVP_DigestUpdate(md_ctx, cleartext, cleartext_size);
  unsigned char digest[EVP_MAX_MD_SIZE];
  unsigned int digest_size;
  EVP_DigestFinal_ex(md_ctx, digest, &digest_size);
  EVP_MD_CTX_destroy(md_ctx);
  md_ctx = nullptr;
  std::cout << "SHA256 digest of 'abc' is: " << std::hex << std::setfill('0');
  for (unsigned int i = 0; i < digest_size; i++) {
    std::cout << std::setw(2) << (int)digest[i];
  }
  std::cout << std::endl;

  std::vector<unsigned char> sig;
  size_t sig_len;

  ctx = EVP_PKEY_CTX_new(pkey, nullptr);
  EVP_PKEY_sign_init(ctx);
  EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING);
  EVP_PKEY_sign(ctx, nullptr, &sig_len, digest, digest_size);
  sig.resize(sig_len);
  EVP_PKEY_sign(ctx, sig.data(), &sig_len, digest, digest_size);
  EVP_PKEY_CTX_free(ctx);

  std::cout << "Signature: " << std::hex << std::setfill('0');
  for (auto c : sig) {
    std::cout << std::setw(2) << static_cast<int>(c);
  }
  std::cout << std::endl;

  ctx = EVP_PKEY_CTX_new(pkey, nullptr);
  EVP_PKEY_verify_init(ctx);
  EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING);
  auto result = EVP_PKEY_verify(ctx, sig.data(), sig_len, digest, digest_size);
  if (result == 1) {
    std::cout << "Verification succeeded." << std::endl;
  } else if (result == 0) {
    std::cout << "Verification failed." << std::endl;
  } else {
    std::cerr << "Verification error." << std::endl;
  }

  return 0;
}