static void ccasn_encode_int(cc_size n, const cc_unit*s, size_t s_size, uint8_t **buffer) { **buffer = ASN1_INTEGER; *buffer += 1; DERSize itemLength = 4; DEREncodeLength(s_size, *buffer, &itemLength); *buffer += itemLength; ccn_write_int(n, s, s_size, *buffer); *buffer += s_size; }
CCCryptorStatus CCRSACryptorGeneratePair(size_t keysize, uint32_t e, CCRSACryptorRef *publicKey, CCRSACryptorRef *privateKey) { CCCryptorStatus retval; CCRSACryptor *privateCryptor = NULL; CCRSACryptor *publicCryptor = NULL; struct ccrng_state *theRng1 = ccDRBGGetRngState(); struct ccrng_state *theRng2 = ccDevRandomGetRngState(); CC_DEBUG_LOG(ASL_LEVEL_ERR, "Entering\n"); // ccrsa_generate_key() requires the exponent as length / pointer to bytes cc_unit cc_unit_e = (cc_unit) e; size_t eSize = ccn_write_int_size(1, &cc_unit_e); uint8_t eBytes[eSize]; ccn_write_int(1, &cc_unit_e, eSize, eBytes); *publicKey = *privateKey = NULL; __Require_Action((privateCryptor = ccMallocRSACryptor(keysize, ccRSAKeyPrivate)) != NULL, errOut, retval = kCCMemoryFailure); // __Require_Action((ccrsa_generate_key(keysize, privateCryptor->rsaKey.full, sizeof(e), &e, theRng) == 0), errOut, retval = kCCDecodeError); __Require_Action((ccrsa_generate_931_key(keysize, privateCryptor->fk, eSize, eBytes, theRng1, theRng2) == 0), errOut, retval = kCCDecodeError); privateCryptor->keyType = ccRSAKeyPrivate; __Require_Action((publicCryptor = CCRSACryptorGetPublicKeyFromPrivateKey(privateCryptor)) != NULL, errOut, retval = kCCMemoryFailure); *publicKey = publicCryptor; *privateKey = privateCryptor; __Require_Action(ccRSApairwiseConsistencyCheck(*privateKey, *publicKey) == true, errOut, retval = kCCDecodeError); return kCCSuccess; errOut: if(privateCryptor) ccRSACryptorClear(privateCryptor); if(publicCryptor) ccRSACryptorClear(publicCryptor); *publicKey = *privateKey = NULL; return retval; }