1/*
2 * Copyright (c) 2000-2001,2011-2012,2014 Apple Inc. All Rights Reserved.
3 *
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
8 * using this file.
9 *
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.
16 */
17
18
19//
20// cryptoclient - client interface to CSSM CSP encryption/decryption operations
21//
22#ifndef _H_CDSA_CLIENT_CRYPTOCLIENT
23#define _H_CDSA_CLIENT_CRYPTOCLIENT  1
24
25#include <security_cdsa_client/cspclient.h>
26#include <security_cdsa_client/keyclient.h>
27
28namespace Security {
29namespace CssmClient {
30
31
32//
33// Common features of various cryptographic operations contexts.
34// These all use symmetric or asymmetric contexts.
35//
36class Crypt : public Context {
37public:
38	Crypt(const CSP &csp, CSSM_ALGORITHMS alg);
39
40public:
41    // Context attributes
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	void initVector(const CssmData &v)		{ mInitVector = &v; set(CSSM_ATTRIBUTE_INIT_VECTOR, v); }
48	CSSM_PADDING padding() const			{ return mPadding; }
49	void padding(CSSM_PADDING p)			{ mPadding = p; set(CSSM_ATTRIBUTE_PADDING, p); }
50
51protected:
52	void activate();
53
54protected:
55	CSSM_ENCRYPT_MODE mMode;
56	Key mKey;
57	const CssmData *mInitVector;
58	CSSM_PADDING mPadding;
59    RecursiveMutex mActivateMutex;
60};
61
62
63
64//
65// An encryption context
66//
67class Encrypt : public Crypt
68{
69public:
70	Encrypt(const CSP &csp, CSSM_ALGORITHMS alg) : Crypt(csp, alg) {};
71
72public:
73	// integrated
74	CSSM_SIZE encrypt(const CssmData *in, uint32 inCount, CssmData *out, uint32 outCount,
75		CssmData &remData);
76	CSSM_SIZE encrypt(const CssmData &in, CssmData &out, CssmData &remData)
77	{ return encrypt(&in, 1, &out, 1, remData); }
78
79	// staged update
80	void init(); // Optional
81	CSSM_SIZE encrypt(const CssmData *in, uint32 inCount, CssmData *out, uint32 outCount);
82	CSSM_SIZE encrypt(const CssmData &in, CssmData &out)
83	{ return encrypt(&in, 1, &out, 1); }
84	// staged final
85	void final(CssmData &remData);
86};
87
88//
89// An Decryption context
90//
91class Decrypt : public Crypt
92{
93public:
94	Decrypt(const CSP &csp, CSSM_ALGORITHMS alg) : Crypt(csp, alg) {};
95
96public:
97	// integrated
98	CSSM_SIZE decrypt(const CssmData *in, uint32 inCount, CssmData *out, uint32 outCount,
99		CssmData &remData);
100	CSSM_SIZE decrypt(const CssmData &in, CssmData &out, CssmData &remData)
101	{ return decrypt(&in, 1, &out, 1, remData); }
102
103	// staged update
104	void init(); // Optional
105	CSSM_SIZE decrypt(const CssmData *in, uint32 inCount, CssmData *out, uint32 outCount);
106	CSSM_SIZE decrypt(const CssmData &in, CssmData &out)
107	{ return decrypt(&in, 1, &out, 1); }
108	// staged final
109	void final(CssmData &remData);
110};
111
112
113} // end namespace CssmClient
114} // end namespace Security
115
116#endif // _H_CDSA_CLIENT_CRYPTOCLIENT
117