2 * Copyright (c) 2010-2011 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 @header EncryptTransform
26 Provide the implementation class for the Encryption and Decryption
31 #if !defined(__ENCRYPT_TRANSFORM__)
32 #define __ENCRYPT_TRANSFORM__ 1
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"
44 class EncryptDecryptBase
: public Transform
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
;
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
;
62 virtual void Finalize();
63 virtual Boolean
TransformCanExecute();
64 virtual CFErrorRef
TransformStartingExecution();
65 CFErrorRef
SerializedTransformStartingExecution();
66 virtual void AttributeChanged(SecTransformAttributeRef ah
, CFTypeRef value
);
68 CFDataRef
apply_oaep_padding(CFDataRef value
);
69 CFDataRef
remove_oaep_padding(CFDataRef value
);
71 EncryptDecryptBase(CFStringRef type
);
73 virtual ~EncryptDecryptBase();
75 void SendCSSMError(CSSM_RETURN error
);
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();
83 // overload to restore the state of your transform
84 virtual void RestoreState(CFDictionaryRef state
);
87 virtual bool InitializeObject(SecKeyRef key
, CFErrorRef
*error
);
93 class EncryptTransform
: public EncryptDecryptBase
99 static TransformFactory
* MakeTransformFactory();
107 virtual ~EncryptTransform();
108 static SecTransformRef
Make();
113 class DecryptTransform
: public EncryptDecryptBase
119 static TransformFactory
* MakeTransformFactory();
127 virtual ~DecryptTransform();
128 static SecTransformRef
Make();
132 #endif /* !__ENCRYPT_TRANSFORM__ */