2 * Copyright (c) 2000-2001,2011-2012,2014 Apple Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 // cryptoclient - client interface to CSSM CSP encryption/decryption operations
22 #ifndef _H_CDSA_CLIENT_CRYPTOCLIENT
23 #define _H_CDSA_CLIENT_CRYPTOCLIENT 1
25 #include <security_cdsa_client/cspclient.h>
26 #include <security_cdsa_client/keyclient.h>
29 namespace CssmClient
{
33 // Common features of various cryptographic operations contexts.
34 // These all use symmetric or asymmetric contexts.
36 class Crypt
: public Context
{
38 Crypt(const CSP
&csp
, CSSM_ALGORITHMS alg
);
42 CSSM_ENCRYPT_MODE
mode() const { return mMode
; }
43 void mode(CSSM_ENCRYPT_MODE m
) { mMode
= m
; set(CSSM_ATTRIBUTE_MODE
, m
); }
44 Key
key() const { return mKey
; }
45 void key(const Key
&k
);
46 const CssmData
&initVector() const { return *mInitVector
; }
47 // The following function is invalid: you cannot save a pointer to an object passed in by reference.
48 // Fixing this error leads to corrupted mutexes everywhere; I cannot figure out why.
49 // To use the Crypt class, you must ensure that the CssmData object you pass in here lives for the lifetime of Crypt.
50 void initVector(const CssmData
&v
) { mInitVector
= &v
; set(CSSM_ATTRIBUTE_INIT_VECTOR
, v
); }
51 CSSM_PADDING
padding() const { return mPadding
; }
52 void padding(CSSM_PADDING p
) { mPadding
= p
; set(CSSM_ATTRIBUTE_PADDING
, p
); }
58 CSSM_ENCRYPT_MODE mMode
;
60 const CssmData
*mInitVector
;
61 CSSM_PADDING mPadding
;
62 RecursiveMutex mActivateMutex
;
68 // An encryption context
70 class Encrypt
: public Crypt
73 Encrypt(const CSP
&csp
, CSSM_ALGORITHMS alg
) : Crypt(csp
, alg
) {};
77 CSSM_SIZE
encrypt(const CssmData
*in
, uint32 inCount
, CssmData
*out
, uint32 outCount
,
79 CSSM_SIZE
encrypt(const CssmData
&in
, CssmData
&out
, CssmData
&remData
)
80 { return encrypt(&in
, 1, &out
, 1, remData
); }
83 void init(); // Optional
84 CSSM_SIZE
encrypt(const CssmData
*in
, uint32 inCount
, CssmData
*out
, uint32 outCount
);
85 CSSM_SIZE
encrypt(const CssmData
&in
, CssmData
&out
)
86 { return encrypt(&in
, 1, &out
, 1); }
88 void final(CssmData
&remData
);
92 // An Decryption context
94 class Decrypt
: public Crypt
97 Decrypt(const CSP
&csp
, CSSM_ALGORITHMS alg
) : Crypt(csp
, alg
) {};
101 CSSM_SIZE
decrypt(const CssmData
*in
, uint32 inCount
, CssmData
*out
, uint32 outCount
,
103 CSSM_SIZE
decrypt(const CssmData
&in
, CssmData
&out
, CssmData
&remData
)
104 { return decrypt(&in
, 1, &out
, 1, remData
); }
107 void init(); // Optional
108 CSSM_SIZE
decrypt(const CssmData
*in
, uint32 inCount
, CssmData
*out
, uint32 outCount
);
109 CSSM_SIZE
decrypt(const CssmData
&in
, CssmData
&out
)
110 { return decrypt(&in
, 1, &out
, 1); }
112 void final(CssmData
&remData
);
116 } // end namespace CssmClient
117 } // end namespace Security
119 #endif // _H_CDSA_CLIENT_CRYPTOCLIENT