1/*
2 * Copyright (c) 2000-2001,2011,2013-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 * bfContext.h - glue between BlockCrytpor and ssleay Blowfish
21 *				 implementation
22 */
23
24#ifndef _BF_CONTEXT_H_
25#define _BF_CONTEXT_H_
26
27#include "AppleCSPContext.h"
28#include "BlockCryptor.h"
29#include <openssl/blowfish.h>
30
31class BlowfishContext : public BlockCryptor {
32public:
33	BlowfishContext(AppleCSPSession &session) :
34		BlockCryptor(session),
35		mInitFlag(false),
36		mRawKeySize(0)	{ }
37	~BlowfishContext();
38
39	// called by CSPFullPluginSession
40	void init(const Context &context, bool encoding = true);
41
42	// As an optimization, we allow reuse of a modified context.
43	// The main thing we avoid is a redundant key scheduling. We
44	// save the current raw keys bits in mRawKey and compare on
45	// re-init.
46	bool changed(const Context &context)	 { return true; }
47
48	// called by BlockCryptor
49	void encryptBlock(
50		const void		*plainText,		// length implied (one block)
51		size_t			plainTextLen,
52		void			*cipherText,
53		size_t			&cipherTextLen,	// in/out, throws on overflow
54		bool			final);
55	void decryptBlock(
56		const void		*cipherText,	// length implied (one cipher block)
57		size_t			cipherTextLen,
58		void			*plainText,
59		size_t			&plainTextLen,	// in/out, throws on overflow
60		bool			final);
61
62private:
63	void deleteKey();
64
65	/* scheduled key */
66	BF_KEY				mBfKey;
67	bool				mInitFlag;			// for easy reuse
68
69	/*
70	 * Raw key bits saved here and checked on re-init to avoid
71	 * extra key schedule
72	 */
73	uint8				mRawKey[BF_MAX_KEY_SIZE_BYTES];
74	uint32				mRawKeySize;
75
76
77};	/* BlowfishContext */
78
79#endif //_BF_CONTEXT_H_
80