Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: init c2rust script #4564

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft

build: init c2rust script #4564

wants to merge 4 commits into from

Conversation

camshaft
Copy link
Contributor

@camshaft camshaft commented May 20, 2024

Description of changes:

This change adds an initial c2rust script for experimentation. Currently, all of the main C files transpile to Rust without warnings, except for 2:

Transpiling s2n_random.c
warning: Did not recognize inline asm constraint: qm
It is likely that this will cause compilation errors or incorrect semantics in the translated program; please manually correct.
Transpiling s2n_tls13_secrets.c
error: Failed to translate s2n_connection_tls_exporter: Init list not implemented for Enum(CDeclId(1420))

The bin and tests directory still needs to be considered and fixed as well.

Testing:

Assuming docker is installed, transpiling the code simply requires running ./scripts/s2n2rust/run.sh. If everything goes correctly, you'll get a cargo project at scripts/s2n2rust/target/s2n-tls.

The emitted rust code is pretty decent. We still need to apply refactoring and analysis to the output to improve it further (which is the main area of research here). Here's a few samples of what it looks like today:

pub unsafe extern "C" fn s2n_stuffer_read_uint64(
    mut stuffer: *mut s2n_stuffer,
    mut u: *mut u64,
) -> libc::c_int {
    ensure_ref!(u as *const libc::c_void);
    let mut data: [u8; 8] = [0; 8];
    guard!(s2n_stuffer_read_bytes(
        stuffer,
        data.as_mut_ptr(),
        ::core::mem::size_of::<[u8; 8]>() as libc::c_ulong as u32,
    ),);
    *u = (data[0 as libc::c_int as usize] as u64) << 56 as libc::c_int;
    *u |= (data[1 as libc::c_int as usize] as u64) << 48 as libc::c_int;
    *u |= (data[2 as libc::c_int as usize] as u64) << 40 as libc::c_int;
    *u |= (data[3 as libc::c_int as usize] as u64) << 32 as libc::c_int;
    *u |= (data[4 as libc::c_int as usize] as u64) << 24 as libc::c_int;
    *u |= (data[5 as libc::c_int as usize] as u64) << 16 as libc::c_int;
    *u |= (data[6 as libc::c_int as usize] as u64) << 8 as libc::c_int;
    *u |= data[7 as libc::c_int as usize] as libc::c_ulong;
    return ok!();
}
unsafe extern "C" fn s2n_aead_cipher_aes_gcm_encrypt(
    mut key: *mut s2n_session_key,
    mut iv: *mut s2n_blob,
    mut aad: *mut s2n_blob,
    mut in_0: *mut s2n_blob,
    mut out: *mut s2n_blob,
) -> libc::c_int {
    ensure_ref!(in_0 as *const libc::c_void);
    ensure_ref!(out as *const libc::c_void);
    ensure_ref!(iv as *const libc::c_void);
    ensure_ref!(key as *const libc::c_void);
    ensure_ref!(aad as *const libc::c_void);
    ensure!(
        (*in_0).size >= 16 as libc::c_int as libc::c_uint,
        S2N_ERR_SAFETY as libc::c_int,
    );
    ensure!((*out).size >= (*in_0).size, S2N_ERR_SAFETY as libc::c_int);
    ensure!(
        (*iv).size == (4 as libc::c_int + 8 as libc::c_int) as libc::c_uint,
        S2N_ERR_SAFETY as libc::c_int,
    );
    let mut in_len: usize = ((*in_0).size).wrapping_sub(16 as libc::c_int as libc::c_uint) as usize;
    let mut out_len: usize = 0 as libc::c_int as usize;
    guard_ossl!(
        aws_lc_0_17_0_EVP_AEAD_CTX_seal(
            (*key).evp_aead_ctx,
            (*out).data,
            &mut out_len,
            (*out).size as usize,
            (*iv).data,
            (*iv).size as usize,
            (*in_0).data,
            in_len,
            (*aad).data,
            (*aad).size as usize,
        ),
        S2N_ERR_ENCRYPT as libc::c_int,
    );
    error_if!(
        in_len.wrapping_add(16 as libc::c_int as libc::c_ulong) != out_len,
        S2N_ERR_ENCRYPT as libc::c_int,
    );
    return ok!();
}

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@github-actions github-actions bot added the s2n-core team label May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant