<commit_before>use ffi::{pk11, sec};
use std::{ptr, mem};
use libc::{c_uint, c_int, c_void};

#[allow(non_camel_case_types)]
pub enum RSAPadding
{
    PKCS1v15,
    OAEP_MGF1_SHA1,
    OAEP_MGF1_SHA224,
    OAEP_MGF1_SHA256,
    OAEP_MGF1_SHA384,
    OAEP_MGF1_SHA512,
}

enum RSAPaddingParam
{
    NullParam,
    OAEPParam(pk11::CK_RSA_PKCS_OAEP_PARAMS),
}

impl RSAPaddingParam
{
    unsafe fn to_secitem(&self) -> sec::SECItem
    {
        match *self
        {
            NullParam => sec::SECItem::from_struct(&()),
            OAEPParam(ref param) => sec::SECItem::from_struct(param),
        }
    }
}

impl RSAPadding
{
    fn to_ckm(&self) -> pk11::CK_MECHANISM_TYPE
    {
        match *self
        {
            PKCS1v15 => pk11::CKM_RSA_PKCS,
            OAEP_MGF1_SHA1
            | OAEP_MGF1_SHA224
            | OAEP_MGF1_SHA256
            | OAEP_MGF1_SHA384
            | OAEP_MGF1_SHA512 => pk11::CKM_RSA_PKCS_OAEP,
        }
    }

    fn get_param(&self) -> RSAPaddingParam
    {
        match *self
        {
            PKCS1v15 => NullParam,
            OAEP_MGF1_SHA1 => OAEPParam(pk11::CK_RSA_PKCS_OAEP_PARAMS::from_algorithm(pk11::CKM_SHA_1)),
            OAEP_MGF1_SHA224 => OAEPParam(pk11::CK_RSA_PKCS_OAEP_PARAMS::from_algorithm(pk11::CKM_SHA_224)),
            OAEP_MGF1_SHA256 => OAEPParam(pk11::CK_RSA_PKCS_OAEP_PARAMS::from_algorithm(pk11::CKM_SHA_256)),
            OAEP_MGF1_SHA384 => OAEPParam(pk11::CK_RSA_PKCS_OAEP_PARAMS::from_algorithm(pk11::CKM_SHA_384)),
            OAEP_MGF1_SHA512 => OAEPParam(pk11::CK_RSA_PKCS_OAEP_PARAMS::from_algorithm(pk11::CKM_SHA_512)),
        }
    }
}

pub struct RSAPrivateKey
{
    key: *mut pk11::SECKEYPrivateKey,
}

impl RSAPrivateKey
{
    pub fn load(data: &[u8]) -> ::NSSResult<RSAPrivateKey>
    {
        try!(::nss::init());
        unsafe
        {
            let mut der = sec::SECItem::new(data);
            let slot = try_ptr!(pk11::PK11_GetInternalKeySlot());
            let mut key = ptr::null_mut();

            try!(pk11::PK11_ImportDERPrivateKeyInfoAndReturnKey(slot, &mut der, ptr::null_mut(), ptr::null_mut(), false, true, pk11::KU_ALL, &mut key, ptr::null_mut()).to_result());

            pk11::PK11_FreeSlot(slot);
            Ok(RSAPrivateKey { key: key })
        }
    }

    pub fn gen(key_size_bits: uint) -> ::NSSResult<RSAPrivateKey>
    {
        try!(::nss::init());
        unsafe
        {
            let slot = try_ptr!(pk11::PK11_GetInternalKeySlot());
            let mut param = pk11::PK11RSAGenParams { key_size_bits: key_size_bits as c_int, pe: 65537, };
            let param_ptr = mem::transmute::<_, *mut c_void>(&mut param);
            let mut pubkey = ptr::null_mut();
            let privkey = try_ptr!(pk11::PK11_GenerateKeyPair(slot, pk11::CKM_RSA_PKCS_KEY_PAIR_GEN, param_ptr, &mut pubkey, false, false, ptr::null_mut()));

            pk11::SECKEY_DestroyPublicKey(pubkey);
            pk11::PK11_FreeSlot(slot);

            Ok(RSAPrivateKey { key: privkey })
        }<commit_msg>Change parameter to prevent issue with exporting<commit_after>use ffi::{pk11, sec};
use std::{ptr, mem};
use libc::{c_uint, c_int, c_void};

#[allow(non_camel_case_types)]
pub enum RSAPadding
{
    PKCS1v15,
    OAEP_MGF1_SHA1,
    OAEP_MGF1_SHA224,
    OAEP_MGF1_SHA256,
    OAEP_MGF1_SHA384,
    OAEP_MGF1_SHA512,
}

enum RSAPaddingParam
{
    NullParam,
    OAEPParam(pk11::CK_RSA_PKCS_OAEP_PARAMS),
}

impl RSAPaddingParam
{
    unsafe fn to_secitem(&self) -> sec::SECItem
    {
        match *self
        {
            NullParam => sec::SECItem::from_struct(&()),
            OAEPParam(ref param) => sec::SECItem::from_struct(param),
        }
    }
}

impl RSAPadding
{
    fn to_ckm(&self) -> pk11::CK_MECHANISM_TYPE
    {
        match *self
        {
            PKCS1v15 => pk11::CKM_RSA_PKCS,
            OAEP_MGF1_SHA1
            | OAEP_MGF1_SHA224
            | OAEP_MGF1_SHA256
            | OAEP_MGF1_SHA384
            | OAEP_MGF1_SHA512 => pk11::CKM_RSA_PKCS_OAEP,
        }
    }

    fn get_param(&self) -> RSAPaddingParam
    {
        match *self
        {
            PKCS1v15 => NullParam,
            OAEP_MGF1_SHA1 => OAEPParam(pk11::CK_RSA_PKCS_OAEP_PARAMS::from_algorithm(pk11::CKM_SHA_1)),
            OAEP_MGF1_SHA224 => OAEPParam(pk11::CK_RSA_PKCS_OAEP_PARAMS::from_algorithm(pk11::CKM_SHA_224)),
            OAEP_MGF1_SHA256 => OAEPParam(pk11::CK_RSA_PKCS_OAEP_PARAMS::from_algorithm(pk11::CKM_SHA_256)),
            OAEP_MGF1_SHA384 => OAEPParam(pk11::CK_RSA_PKCS_OAEP_PARAMS::from_algorithm(pk11::CKM_SHA_384)),
            OAEP_MGF1_SHA512 => OAEPParam(pk11::CK_RSA_PKCS_OAEP_PARAMS::from_algorithm(pk11::CKM_SHA_512)),
        }
    }
}

pub struct RSAPrivateKey
{
    key: *mut pk11::SECKEYPrivateKey,
}

impl RSAPrivateKey
{
    pub fn load(data: &[u8]) -> ::NSSResult<RSAPrivateKey>
    {
        try!(::nss::init());
        unsafe
        {
            let mut der = sec::SECItem::new(data);
            let slot = try_ptr!(pk11::PK11_GetInternalKeySlot());
            let mut key = ptr::null_mut();

            try!(pk11::PK11_ImportDERPrivateKeyInfoAndReturnKey(slot, &mut der, ptr::null_mut(), ptr::null_mut(), false, false, pk11::KU_ALL, &mut key, ptr::null_mut()).to_result());

            pk11::PK11_FreeSlot(slot);
            Ok(RSAPrivateKey { key: key })
        }
    }

    pub fn gen(key_size_bits: uint) -> ::NSSResult<RSAPrivateKey>
    {
        try!(::nss::init());
        unsafe
        {
            let slot = try_ptr!(pk11::PK11_GetInternalKeySlot());
            let mut param = pk11::PK11RSAGenParams { key_size_bits: key_size_bits as c_int, pe: 65537, };
            let param_ptr = mem::transmute::<_, *mut c_void>(&mut param);
            let mut pubkey = ptr::null_mut();
            let privkey = try_ptr!(pk11::PK11_GenerateKeyPair(slot, pk11::CKM_RSA_PKCS_KEY_PAIR_GEN, param_ptr, &mut pubkey, false, false, ptr::null_mut()));

            pk11::SECKEY_DestroyPublicKey(pubkey);
            pk11::PK11_FreeSlot(slot);

            Ok(RSAPrivateKey { key: privkey })
        }
{'fraction_non_alphanumeric': 0.12693702604681834, 'fraction_numerical': 0.04879657105176393, 'mean_word_length': 2.7800623052959503, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '188648', 'n_tokens_mistral': 2536, 'n_tokens_neox': 2344, 'n_words': 316}