2 * Copyright (c) 2000-2001 Apple Computer, 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 * RSA_asymmetric.cpp - CSPContext for RSA asymmetric encryption
23 #include "RSA_asymmetric.h"
24 #include "RSA_DSA_utils.h"
25 #include <Security/debugging.h>
27 #define rsaCryptDebug(args...) debug("rsaCrypt", ## args)
28 #define rbprintf(args...) debug("rsaBuf", ## args)
30 RSA_CryptContext::~RSA_CryptContext()
33 assert(mRsaKey
!= NULL
);
36 mAllocdRsaKey
= false;
40 /* called by CSPFullPluginSession */
41 void RSA_CryptContext::init(const Context
&context
, bool encoding
= true)
43 if(mInitFlag
&& !opStarted()) {
44 /* reusing - e.g. query followed by encrypt */
48 /* optional mode to use alternate key class (e.g., decrypt with public key) */
49 CSSM_KEYCLASS keyClass
;
50 switch (context
.getInt(CSSM_ATTRIBUTE_MODE
)) {
51 case CSSM_ALGMODE_PUBLIC_KEY
:
52 keyClass
= CSSM_KEYCLASS_PUBLIC_KEY
;
54 case CSSM_ALGMODE_PRIVATE_KEY
:
55 keyClass
= CSSM_KEYCLASS_PRIVATE_KEY
;
57 case CSSM_ALGMODE_NONE
:
58 /* default, not present in context: infer from op type */
59 keyClass
= encoding
? CSSM_KEYCLASS_PUBLIC_KEY
: CSSM_KEYCLASS_PRIVATE_KEY
;
62 CssmError::throwMe(CSSMERR_CSP_INVALID_ATTR_MODE
);
65 /* fetch key from context */
68 mRsaKey
= contextToRsaKey(context
,
71 encoding
? CSSM_KEYUSE_ENCRYPT
: CSSM_KEYUSE_DECRYPT
,
78 /* validate context - TBD */
80 /* finally, have BlockCryptor set up its stuff. */
81 unsigned cipherBlockSize
= RSA_size(mRsaKey
);
82 unsigned plainBlockSize
= cipherBlockSize
- 11;
83 setup(encoding
? plainBlockSize
: cipherBlockSize
, // blockSizeIn
84 encoding
? cipherBlockSize
: plainBlockSize
, // blockSizeOut
92 /* called by BlockCryptor */
93 void RSA_CryptContext::encryptBlock(
94 const void *plainText
, // length implied (one block)
97 size_t &cipherTextLen
, // in/out, throws on overflow
102 if(mRsaKey
->d
== NULL
) {
103 irtn
= RSA_public_encrypt(plainTextLen
,
104 (unsigned char *)plainText
,
105 (unsigned char *)cipherText
,
110 irtn
= RSA_private_encrypt(plainTextLen
,
111 (unsigned char *)plainText
,
112 (unsigned char *)cipherText
,
117 throwRsaDsa("RSA_public_encrypt");
119 else if((unsigned)irtn
> cipherTextLen
) {
120 rsaCryptDebug("RSA_public_encrypt overflow");
121 CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR
);
123 cipherTextLen
= (size_t)irtn
;
126 void RSA_CryptContext::decryptBlock(
127 const void *cipherText
, // length implied (one cipher block)
129 size_t &plainTextLen
, // in/out, throws on overflow
134 if(mRsaKey
->d
== NULL
) {
135 irtn
= RSA_public_decrypt(inBlockSize(),
136 (unsigned char *)cipherText
,
137 (unsigned char *)plainText
,
142 irtn
= RSA_private_decrypt(inBlockSize(),
143 (unsigned char *)cipherText
,
144 (unsigned char *)plainText
,
149 throwRsaDsa("RSA_private_decrypt");
151 else if((unsigned)irtn
> plainTextLen
) {
152 rsaCryptDebug("RSA_private_decrypt overflow");
153 CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR
);
155 plainTextLen
= (size_t)irtn
;
158 size_t RSA_CryptContext::outputSize(
159 bool final
, // ignored
160 size_t inSize
= 0) // output for given input size
162 UInt32 rawBytes
= inSize
+ inBufSize();
163 UInt32 rawBlocks
= (rawBytes
+ inBlockSize() - 1) / inBlockSize();
164 rbprintf("--- RSA_CryptContext::outputSize inSize 0x%lx outSize 0x%lx mInBufSize 0x%lx",
165 inSize
, rawBlocks
* outBlockSize(), inBufSize());
166 return rawBlocks
* outBlockSize();