5 * Created by Michael Brouwer on 1/29/07.
6 * Copyright (c) 2007-2008,2010 Apple Inc. All Rights Reserved.
10 #include <CoreFoundation/CoreFoundation.h>
11 #include <Security/SecKeyPriv.h>
13 #include "Security_regressions.h"
15 #define CFReleaseNull(CF) { CFTypeRef _cf = (CF); if (_cf) { (CF) = NULL; CFRelease(_cf); } }
17 static SecKeyRef customKey
;
18 static SecKeyRef initedCustomKey
;
20 static OSStatus
CustomKeyInit(SecKeyRef key
, const uint8_t *key_data
,
21 CFIndex key_len
, SecKeyEncoding encoding
)
23 ok(key
, "CustomKeyInit");
24 is(key
->key
, NULL
, "key->key is NULL");
25 initedCustomKey
= key
;
29 static void CustomKeyDestroy(SecKeyRef key
)
31 is(customKey
, key
, "CustomKeyDestroy");
34 static OSStatus
CustomKeyRawSign(SecKeyRef key
, SecPadding padding
,
35 const uint8_t *dataToSign
, size_t dataToSignLen
,
36 uint8_t *sig
, size_t *sigLen
)
38 is(customKey
, key
, "CustomKeyRawSign");
42 static OSStatus
CustomKeyRawVerify(
43 SecKeyRef key
, SecPadding padding
, const uint8_t *signedData
,
44 size_t signedDataLen
, const uint8_t *sig
, size_t sigLen
)
46 is(customKey
, key
, "CustomKeyRawVerify");
50 static OSStatus
CustomKeyEncrypt(SecKeyRef key
, SecPadding padding
,
51 const uint8_t *plainText
, size_t plainTextLen
,
52 uint8_t *cipherText
, size_t *cipherTextLen
)
54 is(customKey
, key
, "CustomKeyEncrypt");
58 static OSStatus
CustomKeyDecrypt(SecKeyRef key
, SecPadding padding
,
59 const uint8_t *cipherText
, size_t cipherTextLen
,
60 uint8_t *plainText
, size_t *plainTextLen
)
62 is(customKey
, key
, "CustomKeyDecrypt");
66 static OSStatus
CustomKeyCompute(SecKeyRef key
,
67 const uint8_t *pub_key
, size_t pub_key_len
,
68 uint8_t *computed_key
, size_t *computed_key_len
)
70 is(customKey
, key
, "CustomKeyCompute");
74 static size_t CustomKeyBlockSize(SecKeyRef key
)
76 is(customKey
, key
, "CustomKeyBlockSize");
80 static CFDictionaryRef
CustomKeyCopyAttributeDictionary(SecKeyRef key
)
82 is(customKey
, key
, "CustomKeyCopyAttributeDictionary");
83 CFDictionaryRef dict
= CFDictionaryCreate(kCFAllocatorDefault
, NULL
, NULL
,
88 SecKeyDescriptor kCustomKeyDescriptor
= {
89 kSecKeyDescriptorVersion
,
100 CustomKeyCopyAttributeDictionary
,
103 /* Test basic add delete update copy matching stuff. */
104 static void tests(void)
106 const uint8_t *keyData
= (const uint8_t *)"abc";
107 CFIndex keyDataLength
= 3;
108 SecKeyEncoding encoding
= kSecKeyEncodingRaw
;
109 ok(customKey
= SecKeyCreate(kCFAllocatorDefault
,
110 &kCustomKeyDescriptor
, keyData
, keyDataLength
, encoding
),
111 "create custom key");
112 is(customKey
, initedCustomKey
, "CustomKeyInit got the right key");
114 SecPadding padding
= kSecPaddingPKCS1
;
115 const uint8_t *src
= NULL
;
120 ok_status(SecKeyDecrypt(customKey
, padding
, src
, srcLen
, dst
, &dstLen
),
122 ok_status(SecKeyEncrypt(customKey
, padding
, src
, srcLen
, dst
, &dstLen
),
124 ok_status(SecKeyRawSign(customKey
, padding
, src
, srcLen
, dst
, &dstLen
),
126 ok_status(SecKeyRawVerify(customKey
, padding
, src
, srcLen
, dst
, dstLen
),
128 is(SecKeyGetSize(customKey
, kSecKeyKeySizeInBits
), (size_t)42*8, "SecKeyGetSize");
130 CFDictionaryRef attrDict
= NULL
;
131 ok(attrDict
= SecKeyCopyAttributeDictionary(customKey
),
132 "SecKeyCopyAttributeDictionary");
133 CFReleaseNull(attrDict
);
135 //ok(SecKeyGeneratePair(customKey, ), "SecKeyGeneratePair");
136 ok(SecKeyGetTypeID() != 0, "SecKeyGetTypeID works");
139 CFRelease(customKey
);
144 int si_40_seckey_custom(int argc
, char *const *argv
)