EcdsaSignature BackendOpenSsl::sign_data(const ecdsa256::PrivateKey& key, const ByteBuffer& data)
{
    auto priv_key = internal_private_key(key);
    auto digest = calculate_digest(data);

    // sign message data represented by the digest
    openssl::Signature signature { ECDSA_do_sign(digest.data(), digest.size(), priv_key) };

    EcdsaSignature ecdsa_signature;
    X_Coordinate_Only coordinate;

    if (signature) {
        const size_t len = field_size(PublicKeyAlgorithm::Ecdsa_Nistp256_With_Sha256);
        const auto num_bytes_s = BN_num_bytes(signature->s);
        assert(len >= static_cast<size_t>(num_bytes_s));
        ecdsa_signature.s.resize(len, 0x00);
        BN_bn2bin(signature->s, ecdsa_signature.s.data() + len - num_bytes_s);

        const auto num_bytes_r = BN_num_bytes(signature->r);
        assert(len >= static_cast<size_t>(num_bytes_r));
        coordinate.x.resize(len, 0x00);
        BN_bn2bin(signature->r, coordinate.x.data() + len - num_bytes_r);
    } else {
        throw openssl::Exception();
    }

    ecdsa_signature.R = std::move(coordinate);
    return ecdsa_signature;
}
PublicKey create_random_public_key(int seed)
{
    const std::size_t size = field_size(PublicKeyAlgorithm::Ecies_Nistp256);
    EccPoint point = Uncompressed { random_byte_sequence(size, seed),
        random_byte_sequence(size, seed + 1) };
    ecies_nistp256 ecies;
    ecies.public_key = point;
    ecies.supported_symm_alg = SymmetricAlgorithm::Aes128_Ccm;

    return ecies;
}
Exemple #3
0
m1_struct *
newstruct(M1_compiler *comp, char *name, m1_structfield *fields) {
    m1_struct *str = (m1_struct *)m1_malloc(sizeof(m1_struct));    
    str->name      = name;
    str->fields    = fields;
    
    /* struct fields are linked in reverse order, so the first one is
       in fact the one last added, and therefore has the highest offset.
       The offset of the last field is the size of the whole struct
       except the size of the last field itself, so add that.
     */
    str->size = fields->offset + field_size(fields);
    
    assert(comp != NULL);
    return str;   
}
Exemple #4
0
EcdsaSignature BackendOpenSsl::sign_data(const ecdsa256::PrivateKey& key, const ByteBuffer& data)
{
    auto priv_key = internal_private_key(key);
    auto digest = calculate_digest(data);

    // sign message data represented by the digest
    openssl::Signature signature { ECDSA_do_sign(digest.data(), digest.size(), priv_key) };
#if OPENSSL_API_COMPAT < 0x10100000L
    const BIGNUM* sig_r = signature->r;
    const BIGNUM* sig_s = signature->s;
#else
    const BIGNUM* sig_r = nullptr;
    const BIGNUM* sig_s = nullptr;
    ECDSA_SIG_get0(signature, &sig_r, &sig_s);
#endif

    EcdsaSignature ecdsa_signature;
    X_Coordinate_Only coordinate;

    if (sig_r && sig_s) {
        const size_t len = field_size(PublicKeyAlgorithm::ECDSA_NISTP256_With_SHA256);

        const auto num_bytes_s = BN_num_bytes(sig_s);
        assert(len >= static_cast<size_t>(num_bytes_s));
        ecdsa_signature.s.resize(len, 0x00);
        BN_bn2bin(sig_s, ecdsa_signature.s.data() + len - num_bytes_s);

        const auto num_bytes_r = BN_num_bytes(sig_r);
        assert(len >= static_cast<size_t>(num_bytes_r));
        coordinate.x.resize(len, 0x00);
        BN_bn2bin(sig_r, coordinate.x.data() + len - num_bytes_r);
    } else {
        throw openssl::Exception();
    }

    ecdsa_signature.R = std::move(coordinate);
    return ecdsa_signature;
}