|
| 1 | +// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved. |
| 2 | +// Use of this source code is governed by an MIT license |
| 3 | +// that can be found in the LICENSE file. |
| 4 | +module ecdsa |
| 5 | + |
| 6 | +// See https://docs.openssl.org/master/man7/openssl_user_macros/#description |
| 7 | +// should be 0x30000000L, but a lot of EC_KEY method was deprecated on version 3.0 |
| 8 | +// #define OPENSSL_API_COMPAT 0x10100000L |
| 9 | + |
| 10 | +#flag darwin -L /opt/homebrew/opt/openssl/lib -I /opt/homebrew/opt/openssl/include |
| 11 | + |
| 12 | +#flag -I/usr/include/openssl |
| 13 | +#flag -lcrypto |
| 14 | +#flag darwin -I/usr/local/opt/openssl/include |
| 15 | +#flag darwin -L/usr/local/opt/openssl/lib |
| 16 | +#include <openssl/ecdsa.h> |
| 17 | +#include <openssl/obj_mac.h> |
| 18 | +#include <openssl/objects.h> |
| 19 | +#include <openssl/bn.h> |
| 20 | +#include <openssl/evp.h> |
| 21 | +#include <openssl/x509.h> |
| 22 | +#include <openssl/bio.h> |
| 23 | +#include <openssl/pem.h> |
| 24 | + |
| 25 | +// The new opaque of public key pair high level API |
| 26 | +@[typedef] |
| 27 | +struct C.EVP_PKEY {} |
| 28 | + |
| 29 | +fn C.EVP_PKEY_new() &C.EVP_PKEY |
| 30 | +fn C.EVP_PKEY_free(key &C.EVP_PKEY) |
| 31 | +fn C.EVP_PKEY_get1_EC_KEY(pkey &C.EVP_PKEY) &C.EC_KEY |
| 32 | +fn C.EVP_PKEY_base_id(key &C.EVP_PKEY) int |
| 33 | + |
| 34 | +// Elliptic curve keypair declarations |
| 35 | +@[typedef] |
| 36 | +struct C.EC_KEY {} |
| 37 | + |
| 38 | +fn C.EC_KEY_new_by_curve_name(nid int) &C.EC_KEY |
| 39 | +fn C.EC_KEY_generate_key(key &C.EC_KEY) int |
| 40 | +fn C.EC_KEY_dup(src &C.EC_KEY) &C.EC_KEY |
| 41 | +fn C.EC_KEY_free(key &C.EC_KEY) |
| 42 | +fn C.EC_KEY_set_public_key(key &C.EC_KEY, &C.EC_POINT) int |
| 43 | +fn C.EC_KEY_set_private_key(key &C.EC_KEY, prv &C.BIGNUM) int |
| 44 | +fn C.EC_KEY_get0_group(key &C.EC_KEY) &C.EC_GROUP |
| 45 | +fn C.EC_KEY_get0_private_key(key &C.EC_KEY) &C.BIGNUM |
| 46 | +fn C.EC_KEY_get0_public_key(key &C.EC_KEY) &C.EC_POINT |
| 47 | +fn C.EC_KEY_get_conv_form(k &C.EC_KEY) int |
| 48 | +fn C.EC_KEY_check_key(key &C.EC_KEY) int |
| 49 | +fn C.EC_KEY_up_ref(key &C.EC_KEY) int |
| 50 | + |
| 51 | +// BIO input output declarations. |
| 52 | +@[typedef] |
| 53 | +struct C.BIO_METHOD {} |
| 54 | + |
| 55 | +@[typedef] |
| 56 | +pub struct C.BIO {} |
| 57 | + |
| 58 | +fn C.BIO_new(t &C.BIO_METHOD) &C.BIO |
| 59 | +fn C.BIO_free_all(a &C.BIO) |
| 60 | +fn C.BIO_s_mem() &C.BIO_METHOD |
| 61 | +fn C.BIO_write(b &C.BIO, buf &u8, length int) int |
| 62 | +fn C.PEM_read_bio_PrivateKey(bp &C.BIO, x &&C.EVP_PKEY, cb int, u &voidptr) &C.EVP_PKEY |
| 63 | +fn C.PEM_read_bio_PUBKEY(bp &C.BIO, x &&C.EVP_PKEY, cb int, u &voidptr) &C.EVP_PKEY |
| 64 | +fn C.d2i_PUBKEY(k &&C.EVP_PKEY, pp &&u8, length u32) &C.EVP_PKEY |
| 65 | + |
| 66 | +// Elliptic curve point related declarations. |
| 67 | +@[typedef] |
| 68 | +struct C.EC_POINT {} |
| 69 | + |
| 70 | +fn C.EC_POINT_new(group &C.EC_GROUP) &C.EC_POINT |
| 71 | +fn C.EC_POINT_mul(group &C.EC_GROUP, r &C.EC_POINT, n &C.BIGNUM, q &C.EC_POINT, m &C.BIGNUM, ctx &C.BN_CTX) int |
| 72 | +fn C.EC_POINT_point2oct(g &C.EC_GROUP, p &C.EC_POINT, form int, buf &u8, max_out int, ctx &C.BN_CTX) int |
| 73 | +fn C.EC_POINT_cmp(group &C.EC_GROUP, a &C.EC_POINT, b &C.EC_POINT, ctx &C.BN_CTX) int |
| 74 | +fn C.EC_POINT_free(point &C.EC_POINT) |
| 75 | + |
| 76 | +// Elliptic group (curve) related declarations. |
| 77 | +@[typedef] |
| 78 | +struct C.EC_GROUP {} |
| 79 | + |
| 80 | +fn C.EC_GROUP_free(group &C.EC_GROUP) |
| 81 | +fn C.EC_GROUP_get_degree(g &C.EC_GROUP) int |
| 82 | +fn C.EC_GROUP_get_curve_name(g &C.EC_GROUP) int |
| 83 | +fn C.EC_GROUP_cmp(a &C.EC_GROUP, b &C.EC_GROUP, ctx &C.BN_CTX) int |
| 84 | + |
| 85 | +// Elliptic BIGNUM related declarations. |
| 86 | +@[typedef] |
| 87 | +struct C.BIGNUM {} |
| 88 | + |
| 89 | +fn C.BN_num_bits(a &C.BIGNUM) int |
| 90 | +fn C.BN_bn2bin(a &C.BIGNUM, to &u8) int |
| 91 | +fn C.BN_bn2binpad(a &C.BIGNUM, to &u8, tolen int) int |
| 92 | +fn C.BN_cmp(a &C.BIGNUM, b &C.BIGNUM) int |
| 93 | +fn C.BN_bin2bn(s &u8, len int, ret &C.BIGNUM) &C.BIGNUM |
| 94 | +fn C.BN_free(a &C.BIGNUM) |
| 95 | + |
| 96 | +// BIGNUM context |
| 97 | +@[typedef] |
| 98 | +struct C.BN_CTX {} |
| 99 | + |
| 100 | +fn C.BN_CTX_new() &C.BN_CTX |
| 101 | +fn C.BN_CTX_free(ctx &C.BN_CTX) |
| 102 | + |
| 103 | +// ELliptic ECDSA signing and verifying related declarations. |
| 104 | +@[typedef] |
| 105 | +struct C.ECDSA_SIG {} |
| 106 | + |
| 107 | +fn C.ECDSA_size(key &C.EC_KEY) u32 |
| 108 | +fn C.ECDSA_sign(type_ int, dgst &u8, dgstlen int, sig &u8, siglen &u32, eckey &C.EC_KEY) int |
| 109 | +fn C.ECDSA_verify(type_ int, dgst &u8, dgstlen int, sig &u8, siglen int, eckey &C.EC_KEY) int |
0 commit comments