2 * Copyright (c) 2000-2001,2011-2012,2014 Apple 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"
24 #include <Security/cssmerr.h>
25 #include <CommonCrypto/CommonDigest.h> /* for digest sizes */
26 #ifdef CRYPTKIT_CSP_ENABLE
27 #include <security_cryptkit/HmacSha1Legacy.h>
28 #endif /* CRYPTKIT_CSP_ENABLE */
30 MacContext::~MacContext()
32 memset(&hmacCtx
, 0, sizeof(hmacCtx
));
35 /* called out from CSPFullPluginSession....
36 * both generate and verify */
37 void MacContext::init(const Context
&context
, bool isSigning
)
39 CCHmacAlgorithm ccAlg
;
41 /* obtain key from context */
43 uint8
*keyData
= NULL
;
45 symmetricKeyBits(context
, session(), mAlg
,
46 isSigning
? CSSM_KEYUSE_SIGN
: CSSM_KEYUSE_VERIFY
,
50 case CSSM_ALGID_SHA1HMAC
:
51 minKey
= HMAC_SHA_MIN_KEY_SIZE
;
52 mDigestSize
= CC_SHA1_DIGEST_LENGTH
;
53 ccAlg
= kCCHmacAlgSHA1
;
55 case CSSM_ALGID_MD5HMAC
:
56 minKey
= HMAC_MD5_MIN_KEY_SIZE
;
57 mDigestSize
= CC_MD5_DIGEST_LENGTH
;
58 ccAlg
= kCCHmacAlgMD5
;
61 assert(0); // factory should not have called us
62 CssmError::throwMe(CSSMERR_CSP_INVALID_ALGORITHM
);
64 if((keyLen
< minKey
) || (keyLen
> HMAC_MAX_KEY_SIZE
)) {
65 CssmError::throwMe(CSSMERR_CSP_INVALID_ATTR_KEY
);
67 CCHmacInit(&hmacCtx
, ccAlg
, keyData
, keyLen
);
70 void MacContext::update(const CssmData
&data
)
72 CCHmacUpdate(&hmacCtx
, data
.data(), data
.length());
76 void MacContext::final(CssmData
&out
)
78 if(out
.length() < mDigestSize
) {
79 CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR
);
81 CCHmacFinal(&hmacCtx
, out
.data());
82 out
.Length
= mDigestSize
;
86 #define MAX_DIGEST_SIZE CC_SHA1_DIGEST_LENGTH
88 void MacContext::final(const CssmData
&in
)
90 unsigned char mac
[MAX_DIGEST_SIZE
];
92 CCHmacFinal(&hmacCtx
, mac
);
93 if(memcmp(mac
, in
.data(), mDigestSize
)) {
94 CssmError::throwMe(CSSMERR_CSP_VERIFY_FAILED
);
98 size_t MacContext::outputSize(bool final
, size_t inSize
)
103 #ifdef CRYPTKIT_CSP_ENABLE
105 MacLegacyContext::~MacLegacyContext()
108 hmacLegacyFree(mHmac
);
113 /* called out from CSPFullPluginSession....
114 * both generate and verify: */
115 void MacLegacyContext::init(const Context
&context
, bool isSigning
)
118 mHmac
= hmacLegacyAlloc();
120 CssmError::throwMe(CSSMERR_CSP_MEMORY_ERROR
);
124 /* obtain key from context */
126 uint8
*keyData
= NULL
;
128 /* FIXME - this may require a different key alg */
129 symmetricKeyBits(context
, session(), CSSM_ALGID_SHA1HMAC
,
130 isSigning
? CSSM_KEYUSE_SIGN
: CSSM_KEYUSE_VERIFY
,
132 if((keyLen
< HMAC_SHA_MIN_KEY_SIZE
) || (keyLen
> HMAC_MAX_KEY_SIZE
)) {
133 CssmError::throwMe(CSSMERR_CSP_INVALID_ATTR_KEY
);
136 OSStatus ortn
= hmacLegacyInit(mHmac
, keyData
, (UInt32
)keyLen
);
138 MacOSError::throwMe(ortn
);
142 void MacLegacyContext::update(const CssmData
&data
)
144 OSStatus ortn
= hmacLegacyUpdate(mHmac
,
146 (UInt32
)data
.length());
148 MacOSError::throwMe(ortn
);
153 void MacLegacyContext::final(CssmData
&out
)
155 if(out
.length() < kHMACSHA1DigestSize
) {
156 CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR
);
158 hmacLegacyFinal(mHmac
, out
.data());
162 void MacLegacyContext::final(const CssmData
&in
)
164 unsigned char mac
[kHMACSHA1DigestSize
];
165 hmacLegacyFinal(mHmac
, mac
);
166 if(memcmp(mac
, in
.data(), kHMACSHA1DigestSize
)) {
167 CssmError::throwMe(CSSMERR_CSP_VERIFY_FAILED
);
171 size_t MacLegacyContext::outputSize(bool final
, size_t inSize
)
173 return kHMACSHA1DigestSize
;
176 #endif /* CRYPTKIT_CSP_ENABLE */