Skip to content

Instantly share code, notes, and snippets.

@otamajakusi
Created November 3, 2022 06:38
Show Gist options
  • Save otamajakusi/6015babcd224c93c172c268cfd593931 to your computer and use it in GitHub Desktop.
Save otamajakusi/6015babcd224c93c172c268cfd593931 to your computer and use it in GitHub Desktop.
dump signature generated by `openssl dgst -sign private.pem -sha256 myfile > signature.sig`
#include <openssl/pem.h>
#include <openssl/rsa.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
int usage(const char *cmd) {
fprintf(stderr, "Error: %s private.pem signature.sig\n", cmd);
return 1;
}
int load_data(const char *name, unsigned char *buf, size_t buf_len) {
FILE *fp;
size_t read_len;
fp = fopen(name, "rb");
if (fp == NULL) {
return -1;
}
read_len = fread(buf, 1, buf_len, fp);
fclose(fp);
return read_len;
}
void dump(const unsigned char *data, size_t len) {
for (size_t i = 0; i < len; i ++) {
printf("%02x", data[i]);
}
printf("\n");
}
int main(int argc, char *argv[]) {
FILE* fp;
RSA *key;
/*
EVP_PKEY *key = NULL;
EVP_PKEY_CTX *ctx = NULL;
*/
unsigned char buf[2048 / 8];
unsigned char out[2048 / 8];
size_t outlen;
memset(out, 0, sizeof(out));
if (argc != 3) {
return usage(argv[0]);
}
fp = fopen(argv[1], "rb");
key = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
if (load_data("test.sig", buf, sizeof(buf)) < 0) {
return usage(argv[0]);
}
outlen = RSA_public_decrypt(sizeof(buf), buf, out, key, RSA_NO_PADDING);
if (outlen < 0) {
return usage(argv[0]);
}
dump(out, outlen);
memset(out, 0, sizeof(out));
outlen = RSA_public_decrypt(sizeof(buf), buf, out, key, RSA_PKCS1_PADDING);
if (outlen < 0) {
return usage(argv[0]);
}
dump(out, outlen);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment