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.
19 * MacContext.cpp - AppleCSPContext for HMACSHA1
22 #include "MacContext.h"
23 #include <PBKDF2/HMACSHA1.h>
24 #include <Security/cssmerr.h>
25 #include <Security/utilities.h>
26 #ifdef CRYPTKIT_CSP_ENABLE
27 #include <CryptKit/HmacSha1Legacy.h>
28 #endif /* CRYPTKIT_CSP_ENABLE */
30 MacContext::~MacContext()
38 /* called out from CSPFullPluginSession....
39 * both generate and verify: */
40 void MacContext::init(const Context
&context
, bool isSigning
)
45 CssmError::throwMe(CSSMERR_CSP_MEMORY_ERROR
);
49 /* obtain key from context */
51 UInt8
*keyData
= NULL
;
53 symmetricKeyBits(context
, mAlg
,
54 isSigning
? CSSM_KEYUSE_SIGN
: CSSM_KEYUSE_VERIFY
,
58 case CSSM_ALGID_SHA1HMAC
:
59 minKey
= HMAC_SHA_MIN_KEY_SIZE
;
60 mDigestSize
= kHMACSHA1DigestSize
;
62 case CSSM_ALGID_MD5HMAC
:
63 minKey
= HMAC_MD5_MIN_KEY_SIZE
;
64 mDigestSize
= kHMACMD5DigestSize
;
67 assert(0); // factory should not have called us
68 CssmError::throwMe(CSSMERR_CSP_INVALID_ALGORITHM
);
70 if((keyLen
< minKey
) || (keyLen
> HMAC_MAX_KEY_SIZE
)) {
71 CssmError::throwMe(CSSMERR_CSP_INVALID_ATTR_KEY
);
74 CSSM_RETURN crtn
= hmacInit(mHmac
, keyData
, keyLen
,
75 (mAlg
== CSSM_ALGID_SHA1HMAC
) ? CSSM_TRUE
: CSSM_FALSE
);
77 CssmError::throwMe(crtn
);
81 void MacContext::update(const CssmData
&data
)
83 CSSM_RETURN crtn
= hmacUpdate(mHmac
,
87 CssmError::throwMe(crtn
);
92 void MacContext::final(CssmData
&out
)
94 if(out
.length() < mDigestSize
) {
95 CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR
);
97 hmacFinal(mHmac
, out
.data());
101 void MacContext::final(const CssmData
&in
)
103 unsigned char mac
[kHMACSHA1DigestSize
];
104 hmacFinal(mHmac
, mac
);
105 if(memcmp(mac
, in
.data(), mDigestSize
)) {
106 CssmError::throwMe(CSSMERR_CSP_VERIFY_FAILED
);
110 size_t MacContext::outputSize(bool final
, size_t inSize
)
115 #ifdef CRYPTKIT_CSP_ENABLE
117 MacLegacyContext::~MacLegacyContext()
120 hmacLegacyFree(mHmac
);
125 /* called out from CSPFullPluginSession....
126 * both generate and verify: */
127 void MacLegacyContext::init(const Context
&context
, bool isSigning
)
130 mHmac
= hmacLegacyAlloc();
132 CssmError::throwMe(CSSMERR_CSP_MEMORY_ERROR
);
136 /* obtain key from context */
138 UInt8
*keyData
= NULL
;
140 /* FIXME - this may require a different key alg */
141 symmetricKeyBits(context
, CSSM_ALGID_SHA1HMAC
,
142 isSigning
? CSSM_KEYUSE_SIGN
: CSSM_KEYUSE_VERIFY
,
144 if((keyLen
< HMAC_SHA_MIN_KEY_SIZE
) || (keyLen
> HMAC_MAX_KEY_SIZE
)) {
145 CssmError::throwMe(CSSMERR_CSP_INVALID_ATTR_KEY
);
148 OSStatus ortn
= hmacLegacyInit(mHmac
, keyData
, keyLen
);
150 MacOSError::throwMe(ortn
);
154 void MacLegacyContext::update(const CssmData
&data
)
156 OSStatus ortn
= hmacLegacyUpdate(mHmac
,
160 MacOSError::throwMe(ortn
);
165 void MacLegacyContext::final(CssmData
&out
)
167 if(out
.length() < kHMACSHA1DigestSize
) {
168 CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR
);
170 hmacLegacyFinal(mHmac
, out
.data());
174 void MacLegacyContext::final(const CssmData
&in
)
176 unsigned char mac
[kHMACSHA1DigestSize
];
177 hmacLegacyFinal(mHmac
, mac
);
178 if(memcmp(mac
, in
.data(), kHMACSHA1DigestSize
)) {
179 CssmError::throwMe(CSSMERR_CSP_VERIFY_FAILED
);
183 size_t MacLegacyContext::outputSize(bool final
, size_t inSize
)
185 return kHMACSHA1DigestSize
;
188 #endif /* CRYPTKIT_CSP_ENABLE */