1/*
2 * Copyright (c) 2012 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25#include <CoreFoundation/CoreFoundation.h>
26#include "corecrypto/ccsha1.h"
27#include "corecrypto/ccrsa_priv.h"
28#include "corecrypto/ccrng_system.h"
29#include "corecrypto/ccn.h"
30#include "stdio.h"
31#include "misc.h"
32
33// corecrypto headers don't like C++ (on a deaper level then extern "C" {} can fix
34// so we need a C "shim" for all our corecrypto use.
35
36#warning "This declaration should be in some headers"
37CFDataRef oaep_padding_via_c(int desired_message_length, CFDataRef dataValue);
38CFDataRef oaep_padding_via_c(int desired_message_length, CFDataRef dataValue)
39{
40	cc_unit paddingBuffer[ccn_nof_size(desired_message_length)];
41	bzero(paddingBuffer, sizeof(cc_unit) * ccn_nof_size(desired_message_length)); // XXX needed??
42	static dispatch_once_t randomNumberGenneratorInitialzed;
43	static struct ccrng_system_state rng;
44	dispatch_once(&randomNumberGenneratorInitialzed, ^{
45        ccrng_system_init(&rng);
46	});
47
48    ccrsa_oaep_encode(ccsha1_di(),
49                      (struct ccrng_state*)&rng,
50                      sizeof(paddingBuffer), (cc_unit*)paddingBuffer,
51                      CFDataGetLength(dataValue), CFDataGetBytePtr(dataValue));
52    ccn_swap(ccn_nof_size(sizeof(paddingBuffer)), (cc_unit*)paddingBuffer);
53
54    CFDataRef paddedValue = CFDataCreate(NULL, (UInt8*)paddingBuffer, desired_message_length);
55	return paddedValue ? paddedValue : (void*)GetNoMemoryErrorAndRetain();
56}
57
58#warning "This declaration should be in some headers"
59CFDataRef oaep_unpadding_via_c(CFDataRef encodedMessage);
60CFDataRef oaep_unpadding_via_c(CFDataRef encodedMessage)
61{
62	size_t mlen = CFDataGetLength(encodedMessage);
63	cc_size n = ccn_nof_size(mlen);
64	cc_unit paddingBuffer[n];
65	UInt8 plainText[mlen];
66
67	ccn_read_uint(n, paddingBuffer, mlen, CFDataGetBytePtr(encodedMessage));
68	size_t plainTextLength = mlen;
69    int err = ccrsa_oaep_decode(ccsha1_di(), &plainTextLength, plainText, mlen, paddingBuffer);
70
71    if (err) {
72		// XXX should make a CFError or something.
73		return (void*)fancy_error(CFSTR("CoreCrypto"), err, CFSTR("OAEP decode error"));
74    }
75	CFDataRef result = CFDataCreate(NULL, (UInt8*)plainText, plainTextLength);
76	return result;
77}
78