X-Git-Url: https://git.saurik.com/apple/security.git/blobdiff_plain/80e2389990082500d76eb566d4946be3e786c3ef..d8f41ccd20de16f8ebe2ccc84d47bf1cb2b26bbb:/Security/libsecurity_transform/lib/CEncryptDecrypt.c?ds=sidebyside diff --git a/Security/libsecurity_transform/lib/CEncryptDecrypt.c b/Security/libsecurity_transform/lib/CEncryptDecrypt.c new file mode 100644 index 00000000..2c960f75 --- /dev/null +++ b/Security/libsecurity_transform/lib/CEncryptDecrypt.c @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2012 Apple Inc. All Rights Reserved. + * + * @APPLE_LICENSE_HEADER_START@ + * + * This file contains Original Code and/or Modifications of Original Code + * as defined in and that are subject to the Apple Public Source License + * Version 2.0 (the 'License'). You may not use this file except in + * compliance with the License. Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this + * file. + * + * The Original Code and all software distributed under the License are + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. + * Please see the License for the specific language governing rights and + * limitations under the License. + * + * @APPLE_LICENSE_HEADER_END@ + */ + + +#include +#include "corecrypto/ccsha1.h" +#include "corecrypto/ccrsa_priv.h" +#include "corecrypto/ccrng_system.h" +#include "corecrypto/ccn.h" +#include "stdio.h" +#include "misc.h" + +// corecrypto headers don't like C++ (on a deaper level then extern "C" {} can fix +// so we need a C "shim" for all our corecrypto use. + +#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)); // XXX needed?? + 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) { + // XXX should make a CFError or something. + return (void*)fancy_error(CFSTR("CoreCrypto"), err, CFSTR("OAEP decode error")); + } + CFDataRef result = CFDataCreate(NULL, (UInt8*)plainText, plainTextLength); + return result; +}