diff options
| author | 2023-03-30 11:17:27 +0200 | |
|---|---|---|
| committer | 2023-03-30 11:17:27 +0200 | |
| commit | 6ee71cdbc8ac38fd97cfd11459cf9ce58a09689f (patch) | |
| tree | 5f52543a01511bca831314ac769d3124a9a2057a /xs_openssl.h | |
| parent | Backport from xs. (diff) | |
| download | snac2-6ee71cdbc8ac38fd97cfd11459cf9ce58a09689f.tar.gz snac2-6ee71cdbc8ac38fd97cfd11459cf9ce58a09689f.tar.xz snac2-6ee71cdbc8ac38fd97cfd11459cf9ce58a09689f.zip | |
Backport from xs.
Diffstat (limited to '')
| -rw-r--r-- | xs_openssl.h | 78 |
1 files changed, 22 insertions, 56 deletions
diff --git a/xs_openssl.h b/xs_openssl.h index f9cc452..88da916 100644 --- a/xs_openssl.h +++ b/xs_openssl.h | |||
| @@ -4,10 +4,13 @@ | |||
| 4 | 4 | ||
| 5 | #define _XS_OPENSSL_H | 5 | #define _XS_OPENSSL_H |
| 6 | 6 | ||
| 7 | xs_str *xs_md5_hex(const xs_val *input, int size); | 7 | xs_str *_xs_digest(const xs_val *input, int size, const char *digest, int as_hex); |
| 8 | xs_str *xs_sha1_hex(const xs_val *input, int size); | 8 | |
| 9 | xs_str *xs_sha256_hex(const xs_val *input, int size); | 9 | #define xs_md5_hex(input, size) _xs_digest(input, size, "md5", 1) |
| 10 | xs_str *xs_sha256_base64(const xs_val *input, int size); | 10 | #define xs_sha1_hex(input, size) _xs_digest(input, size, "sha1", 1) |
| 11 | #define xs_sha256_hex(input, size) _xs_digest(input, size, "sha256", 1) | ||
| 12 | #define xs_sha256_base64(input, size) _xs_digest(input, size, "sha256", 0) | ||
| 13 | |||
| 11 | xs_dict *xs_rsa_genkey(int bits); | 14 | xs_dict *xs_rsa_genkey(int bits); |
| 12 | xs_str *xs_rsa_sign(const char *secret, const char *mem, int size); | 15 | xs_str *xs_rsa_sign(const char *secret, const char *mem, int size); |
| 13 | int xs_rsa_verify(const char *pubkey, const char *mem, int size, const char *b64sig); | 16 | int xs_rsa_verify(const char *pubkey, const char *mem, int size, const char *b64sig); |
| @@ -17,67 +20,30 @@ int xs_evp_verify(const char *pubkey, const char *mem, int size, const char *b64 | |||
| 17 | 20 | ||
| 18 | #ifdef XS_IMPLEMENTATION | 21 | #ifdef XS_IMPLEMENTATION |
| 19 | 22 | ||
| 20 | #include "openssl/md5.h" | ||
| 21 | #include "openssl/sha.h" | ||
| 22 | #include "openssl/rsa.h" | 23 | #include "openssl/rsa.h" |
| 23 | #include "openssl/pem.h" | 24 | #include "openssl/pem.h" |
| 24 | #include "openssl/evp.h" | 25 | #include "openssl/evp.h" |
| 25 | 26 | ||
| 26 | xs_str *xs_md5_hex(const xs_val *input, int size) | 27 | xs_str *_xs_digest(const xs_val *input, int size, const char *digest, int as_hex) |
| 27 | { | 28 | /* generic function for generating and encoding digests */ |
| 28 | unsigned char md5[16]; | ||
| 29 | MD5_CTX ctx; | ||
| 30 | |||
| 31 | MD5_Init(&ctx); | ||
| 32 | MD5_Update(&ctx, input, size); | ||
| 33 | MD5_Final(md5, &ctx); | ||
| 34 | |||
| 35 | return xs_hex_enc((char *)md5, sizeof(md5)); | ||
| 36 | } | ||
| 37 | |||
| 38 | |||
| 39 | xs_str *xs_sha1_hex(const xs_val *input, int size) | ||
| 40 | { | ||
| 41 | unsigned char sha1[20]; | ||
| 42 | SHA_CTX ctx; | ||
| 43 | |||
| 44 | SHA1_Init(&ctx); | ||
| 45 | SHA1_Update(&ctx, input, size); | ||
| 46 | SHA1_Final(sha1, &ctx); | ||
| 47 | |||
| 48 | return xs_hex_enc((char *)sha1, sizeof(sha1)); | ||
| 49 | } | ||
| 50 | |||
| 51 | |||
| 52 | unsigned char *_xs_sha256(const void *input, int size, unsigned char *sha256) | ||
| 53 | { | 29 | { |
| 54 | SHA256_CTX ctx; | 30 | const EVP_MD *md; |
| 55 | |||
| 56 | SHA256_Init(&ctx); | ||
| 57 | SHA256_Update(&ctx, input, size); | ||
| 58 | SHA256_Final(sha256, &ctx); | ||
| 59 | |||
| 60 | return sha256; | ||
| 61 | } | ||
| 62 | |||
| 63 | |||
| 64 | xs_str *xs_sha256_hex(const xs_val *input, int size) | ||
| 65 | { | ||
| 66 | unsigned char sha256[32]; | ||
| 67 | |||
| 68 | _xs_sha256(input, size, sha256); | ||
| 69 | |||
| 70 | return xs_hex_enc((char *)sha256, sizeof(sha256)); | ||
| 71 | } | ||
| 72 | 31 | ||
| 32 | if ((md = EVP_get_digestbyname(digest)) == NULL) | ||
| 33 | return NULL; | ||
| 73 | 34 | ||
| 74 | xs_str *xs_sha256_base64(const xs_val *input, int size) | 35 | unsigned char output[1024]; |
| 75 | { | 36 | unsigned int out_size; |
| 76 | unsigned char sha256[32]; | 37 | EVP_MD_CTX *mdctx; |
| 77 | 38 | ||
| 78 | _xs_sha256(input, size, sha256); | 39 | mdctx = EVP_MD_CTX_new(); |
| 40 | EVP_DigestInit_ex(mdctx, md, NULL); | ||
| 41 | EVP_DigestUpdate(mdctx, input, size); | ||
| 42 | EVP_DigestFinal_ex(mdctx, output, &out_size); | ||
| 43 | EVP_MD_CTX_free(mdctx); | ||
| 79 | 44 | ||
| 80 | return xs_base64_enc((char *)sha256, sizeof(sha256)); | 45 | return as_hex ? xs_hex_enc ((char *)output, out_size) : |
| 46 | xs_base64_enc((char *)output, out_size); | ||
| 81 | } | 47 | } |
| 82 | 48 | ||
| 83 | 49 | ||