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
, CSSM_ALGID_SHA1HMAC
,
54 isSigning
? CSSM_KEYUSE_SIGN
: CSSM_KEYUSE_VERIFY
,
56 if((keyLen
< HMAC_MIN_KEY_SIZE
) || (keyLen
> HMAC_MAX_KEY_SIZE
)) {
57 CssmError::throwMe(CSSMERR_CSP_INVALID_ATTR_KEY
);
60 CSSM_RETURN crtn
= hmacInit(mHmac
, keyData
, keyLen
);
62 CssmError::throwMe(crtn
);
66 void MacContext::update(const CssmData
&data
)
68 CSSM_RETURN crtn
= hmacUpdate(mHmac
,
72 CssmError::throwMe(crtn
);
77 void MacContext::final(CssmData
&out
)
79 if(out
.length() < kHMACSHA1DigestSize
) {
80 CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR
);
82 hmacFinal(mHmac
, out
.data());
86 void MacContext::final(const CssmData
&in
)
88 unsigned char mac
[kHMACSHA1DigestSize
];
89 hmacFinal(mHmac
, mac
);
90 if(memcmp(mac
, in
.data(), kHMACSHA1DigestSize
)) {
91 CssmError::throwMe(CSSMERR_CSP_VERIFY_FAILED
);
95 size_t MacContext::outputSize(bool final
, size_t inSize
)
97 return kHMACSHA1DigestSize
;
100 #ifdef CRYPTKIT_CSP_ENABLE
102 MacLegacyContext::~MacLegacyContext()
105 hmacLegacyFree(mHmac
);
110 /* called out from CSPFullPluginSession....
111 * both generate and verify: */
112 void MacLegacyContext::init(const Context
&context
, bool isSigning
)
115 mHmac
= hmacLegacyAlloc();
117 CssmError::throwMe(CSSMERR_CSP_MEMORY_ERROR
);
121 /* obtain key from context */
123 UInt8
*keyData
= NULL
;
125 /* FIXME - this may require a different key alg */
126 symmetricKeyBits(context
, CSSM_ALGID_SHA1HMAC
,
127 isSigning
? CSSM_KEYUSE_SIGN
: CSSM_KEYUSE_VERIFY
,
129 if((keyLen
< HMAC_MIN_KEY_SIZE
) || (keyLen
> HMAC_MAX_KEY_SIZE
)) {
130 CssmError::throwMe(CSSMERR_CSP_INVALID_ATTR_KEY
);
133 OSStatus ortn
= hmacLegacyInit(mHmac
, keyData
, keyLen
);
135 MacOSError::throwMe(ortn
);
139 void MacLegacyContext::update(const CssmData
&data
)
141 OSStatus ortn
= hmacLegacyUpdate(mHmac
,
145 MacOSError::throwMe(ortn
);
150 void MacLegacyContext::final(CssmData
&out
)
152 if(out
.length() < kHMACSHA1DigestSize
) {
153 CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR
);
155 hmacLegacyFinal(mHmac
, out
.data());
159 void MacLegacyContext::final(const CssmData
&in
)
161 unsigned char mac
[kHMACSHA1DigestSize
];
162 hmacLegacyFinal(mHmac
, mac
);
163 if(memcmp(mac
, in
.data(), kHMACSHA1DigestSize
)) {
164 CssmError::throwMe(CSSMERR_CSP_VERIFY_FAILED
);
168 size_t MacLegacyContext::outputSize(bool final
, size_t inSize
)
170 return kHMACSHA1DigestSize
;
173 #endif /* CRYPTKIT_CSP_ENABLE */