Skip to content

Instantly share code, notes, and snippets.

@richsalz
Created February 16, 2023 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richsalz/31a98a3095fa36ab6a66082a0c028a6f to your computer and use it in GitHub Desktop.
Save richsalz/31a98a3095fa36ab6a66082a0c028a6f to your computer and use it in GitHub Desktop.
#include <string>
#include <string_view>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/x509.h>
constexpr size_t k25519PrivateKeyBytes = 32;
constexpr size_t k25519PublicKeyBytes = 32;
char private_key[k25519PrivateKeyBytes];
char *foo(std::string_view private_key)
{
constexpr const char private_prefix[] = {
0x30, 0x2e, // SEQUENCE(3 elem) (46 bytes)
0x02, 0x01, 0x00, // INTEGER 0
0x30, 0x05, // SEQUENCE(1 elem) (5 bytes)
0x06, 0x03, // OBJECT IDENTIFIER (3 bytes)
0x2b, 0x65, 0x6e, // OBJ_X25519 -> 1.3.101.110 -> 1*40+3, 101, 110
0x04, 0x22, // OCTET STRING(1 elem) (nested)
0x04, 0x20}; // OCTET STRING(32 bytes)
std::string private_asn1;
private_asn1.assign(private_prefix, sizeof(private_prefix));
private_asn1.append(private_key.data(), private_key.size());
const unsigned char *ptr = reinterpret_cast<const unsigned char*> (private_asn1.data());
const unsigned char *end = ptr + private_asn1.size();
PKCS8_PRIV_KEY_INFO* p8(d2i_PKCS8_PRIV_KEY_INFO(nullptr, &ptr, private_asn1.size()));
if (!p8 || ptr != end)
{
return nullptr;
}
EVP_PKEY* key(EVP_PKCS82PKEY(p8));
if (!key)
{
return nullptr;
}
X509_PUBKEY *ppubkey = nullptr;
if (1 != X509_PUBKEY_set(&ppubkey, key))
{
return nullptr;
}
const unsigned char *p = nullptr;
int plen;
if (1 != X509_PUBKEY_get0_param(nullptr, &p, &plen, nullptr, ppubkey) || !p || plen != k25519PublicKeyBytes)
{
}
else
{
}
X509_PUBKEY_free(ppubkey);
EVP_PKEY_free(key);
PKCS8_PRIV_KEY_INFO_free(p8);
return nullptr;
}
int main()
{
for(int i = 0 ; i < 256; i++)
{
private_key[0] = i;
for(int j = 0 ; j < 256; j++)
{
private_key[1] = j;
foo({private_key, k25519PrivateKeyBytes});
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment