#include <CoreFoundation/CoreFoundation.h>
#include "corecrypto/ccsha1.h"
#include "corecrypto/ccrsa_priv.h"
#include "corecrypto/ccrng_system.h"
#include "corecrypto/ccn.h"
#include "stdio.h"
#include "misc.h"
#warning "This declaration should be in some headers"
CFDataRef oaep_padding_via_c(int desired_message_length, CFDataRef dataValue);
CFDataRef oaep_padding_via_c(int desired_message_length, CFDataRef dataValue)
{
cc_unit paddingBuffer[ccn_nof_size(desired_message_length)];
bzero(paddingBuffer, sizeof(cc_unit) * ccn_nof_size(desired_message_length)); static dispatch_once_t randomNumberGenneratorInitialzed;
static struct ccrng_system_state rng;
dispatch_once(&randomNumberGenneratorInitialzed, ^{
ccrng_system_init(&rng);
});
ccrsa_oaep_encode(ccsha1_di(),
(struct ccrng_state*)&rng,
sizeof(paddingBuffer), (cc_unit*)paddingBuffer,
CFDataGetLength(dataValue), CFDataGetBytePtr(dataValue));
ccn_swap(ccn_nof_size(sizeof(paddingBuffer)), (cc_unit*)paddingBuffer);
CFDataRef paddedValue = CFDataCreate(NULL, (UInt8*)paddingBuffer, desired_message_length);
return paddedValue ? paddedValue : (void*)GetNoMemoryErrorAndRetain();
}
#warning "This declaration should be in some headers"
CFDataRef oaep_unpadding_via_c(CFDataRef encodedMessage);
CFDataRef oaep_unpadding_via_c(CFDataRef encodedMessage)
{
size_t mlen = CFDataGetLength(encodedMessage);
cc_size n = ccn_nof_size(mlen);
cc_unit paddingBuffer[n];
UInt8 plainText[mlen];
ccn_read_uint(n, paddingBuffer, mlen, CFDataGetBytePtr(encodedMessage));
size_t plainTextLength = mlen;
int err = ccrsa_oaep_decode(ccsha1_di(), &plainTextLength, plainText, mlen, paddingBuffer);
if (err) {
return (void*)fancy_error(CFSTR("CoreCrypto"), err, CFSTR("OAEP decode error"));
}
CFDataRef result = CFDataCreate(NULL, (UInt8*)plainText, plainTextLength);
return result;
}