1/*
2 * Copyright (c) 2010-2011 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 @header EncryptTransform
26 Provide the implementation class for the Encryption and Decryption
27 transforms
28
29 */
30
31#if !defined(__ENCRYPT_TRANSFORM__)
32#define __ENCRYPT_TRANSFORM__ 1
33
34#include <CommonCrypto/CommonCryptor.h>
35#include <CoreFoundation/CoreFoundation.h>
36#include <Security/cssmapi.h>
37#include <Security/cssmapple.h>
38#include <Security/cssmtype.h>
39#include <Security/SecKey.h>
40#include "Transform.h"
41#include "TransformFactory.h"
42
43
44class EncryptDecryptBase : public Transform
45{
46protected:
47	CSSM_PADDING			m_cssm_padding;
48	CSSM_ENCRYPT_MODE		m_mode;
49	CSSM_KEY_PTR			m_cssm_key;			// The cssm key from the reference key
50	CSSM_CC_HANDLE			m_handle;			// The context for this key either encrypt or decrypt
51	Boolean					m_forEncryption;
52	Boolean					m_oaep_padding;
53	CFMutableDataRef		m_processedData;
54	// for "single chunk" modes or paddings (i.e. OAEP) m_accumulator accumulates all the raw cleartext until EOS.
55	CFMutableDataRef		m_accumulator;
56    SecTransformAttributeRef inputAH;
57
58	// Used to serialize CDSA setup operations for encrypt/decrypt on a given key
59	static dispatch_once_t	serializerSetUp;
60	static dispatch_queue_t		serializerTransformStartingExecution;
61
62	virtual void			Finalize();
63	virtual Boolean 		TransformCanExecute();
64	virtual CFErrorRef 		TransformStartingExecution();
65	CFErrorRef				SerializedTransformStartingExecution();
66	virtual void 			AttributeChanged(SecTransformAttributeRef ah, CFTypeRef value);
67
68	CFDataRef				apply_oaep_padding(CFDataRef value);
69	CFDataRef				remove_oaep_padding(CFDataRef value);
70
71	EncryptDecryptBase(CFStringRef type);
72
73	virtual 				~EncryptDecryptBase();
74
75	void					SendCSSMError(CSSM_RETURN error);
76
77public:
78	// overload to return a CFDictionary that contains the state of your transform.  Values returned should be
79	// serializable.  Remember that this state will be restored before SecTransformExecute is called.  Do not
80	// include the transform name in your state (this will be done for you by SecTransformCopyExternalRepresentation).
81	virtual CFDictionaryRef CopyState();
82
83	// overload to restore the state of your transform
84	virtual void 			RestoreState(CFDictionaryRef state);
85
86	// your own routines
87	virtual bool 			InitializeObject(SecKeyRef key, CFErrorRef *error);
88
89
90};
91
92
93class EncryptTransform : public EncryptDecryptBase
94{
95protected:
96
97public:
98
99	static TransformFactory* MakeTransformFactory();
100
101public:
102
103protected:
104	EncryptTransform() ;
105
106public:
107	virtual 				~EncryptTransform();
108	static SecTransformRef 	Make();
109};
110
111
112
113class DecryptTransform : public EncryptDecryptBase
114{
115protected:
116
117public:
118
119	static TransformFactory* MakeTransformFactory();
120
121public:
122
123protected:
124	DecryptTransform();
125
126public:
127	virtual 				~DecryptTransform();
128	static SecTransformRef 	Make();
129};
130
131
132#endif /* !__ENCRYPT_TRANSFORM__ */
133