]> git.saurik.com Git - apple/security.git/blob - sec/Security/Regressions/secitem/si-40-seckey-custom.c
Security-55471.14.8.tar.gz
[apple/security.git] / sec / Security / Regressions / secitem / si-40-seckey-custom.c
1 /*
2 * si-40-seckey.c
3 * Security
4 *
5 * Created by Michael Brouwer on 1/29/07.
6 * Copyright (c) 2007-2008,2010 Apple Inc. All Rights Reserved.
7 *
8 */
9
10 #include <CoreFoundation/CoreFoundation.h>
11 #include <Security/SecKeyPriv.h>
12
13 #include "Security_regressions.h"
14
15 #define CFReleaseNull(CF) { CFTypeRef _cf = (CF); if (_cf) { (CF) = NULL; CFRelease(_cf); } }
16
17 static SecKeyRef customKey;
18 static SecKeyRef initedCustomKey;
19
20 static OSStatus CustomKeyInit(SecKeyRef key, const uint8_t *key_data,
21 CFIndex key_len, SecKeyEncoding encoding)
22 {
23 ok(key, "CustomKeyInit");
24 is(key->key, NULL, "key->key is NULL");
25 initedCustomKey = key;
26 return errSecSuccess;
27 }
28
29 static void CustomKeyDestroy(SecKeyRef key)
30 {
31 is(customKey, key, "CustomKeyDestroy");
32 }
33
34 static OSStatus CustomKeyRawSign(SecKeyRef key, SecPadding padding,
35 const uint8_t *dataToSign, size_t dataToSignLen,
36 uint8_t *sig, size_t *sigLen)
37 {
38 is(customKey, key, "CustomKeyRawSign");
39 return errSecSuccess;
40 }
41
42 static OSStatus CustomKeyRawVerify(
43 SecKeyRef key, SecPadding padding, const uint8_t *signedData,
44 size_t signedDataLen, const uint8_t *sig, size_t sigLen)
45 {
46 is(customKey, key, "CustomKeyRawVerify");
47 return errSecSuccess;
48 }
49
50 static OSStatus CustomKeyEncrypt(SecKeyRef key, SecPadding padding,
51 const uint8_t *plainText, size_t plainTextLen,
52 uint8_t *cipherText, size_t *cipherTextLen)
53 {
54 is(customKey, key, "CustomKeyEncrypt");
55 return errSecSuccess;
56 }
57
58 static OSStatus CustomKeyDecrypt(SecKeyRef key, SecPadding padding,
59 const uint8_t *cipherText, size_t cipherTextLen,
60 uint8_t *plainText, size_t *plainTextLen)
61 {
62 is(customKey, key, "CustomKeyDecrypt");
63 return errSecSuccess;
64 }
65
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)
69 {
70 is(customKey, key, "CustomKeyCompute");
71 return errSecSuccess;
72 }
73
74 static size_t CustomKeyBlockSize(SecKeyRef key)
75 {
76 is(customKey, key, "CustomKeyBlockSize");
77 return 42;
78 }
79
80 static CFDictionaryRef CustomKeyCopyAttributeDictionary(SecKeyRef key)
81 {
82 is(customKey, key, "CustomKeyCopyAttributeDictionary");
83 CFDictionaryRef dict = CFDictionaryCreate(kCFAllocatorDefault, NULL, NULL,
84 0, NULL, NULL);
85 return dict;
86 }
87
88 SecKeyDescriptor kCustomKeyDescriptor = {
89 kSecKeyDescriptorVersion,
90 "CustomKey",
91 0, /* extraBytes */
92 CustomKeyInit,
93 CustomKeyDestroy,
94 CustomKeyRawSign,
95 CustomKeyRawVerify,
96 CustomKeyEncrypt,
97 CustomKeyDecrypt,
98 CustomKeyCompute,
99 CustomKeyBlockSize,
100 CustomKeyCopyAttributeDictionary,
101 };
102
103 /* Test basic add delete update copy matching stuff. */
104 static void tests(void)
105 {
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");
113
114 SecPadding padding = kSecPaddingPKCS1;
115 const uint8_t *src = NULL;
116 size_t srcLen = 3;
117 uint8_t *dst = NULL;
118 size_t dstLen = 3;
119
120 ok_status(SecKeyDecrypt(customKey, padding, src, srcLen, dst, &dstLen),
121 "SecKeyDecrypt");
122 ok_status(SecKeyEncrypt(customKey, padding, src, srcLen, dst, &dstLen),
123 "SecKeyEncrypt");
124 ok_status(SecKeyRawSign(customKey, padding, src, srcLen, dst, &dstLen),
125 "SecKeyRawSign");
126 ok_status(SecKeyRawVerify(customKey, padding, src, srcLen, dst, dstLen),
127 "SecKeyRawVerify");
128 is(SecKeyGetSize(customKey, kSecKeyKeySizeInBits), (size_t)42*8, "SecKeyGetSize");
129
130 CFDictionaryRef attrDict = NULL;
131 ok(attrDict = SecKeyCopyAttributeDictionary(customKey),
132 "SecKeyCopyAttributeDictionary");
133 CFReleaseNull(attrDict);
134
135 //ok(SecKeyGeneratePair(customKey, ), "SecKeyGeneratePair");
136 ok(SecKeyGetTypeID() != 0, "SecKeyGetTypeID works");
137
138 if (customKey) {
139 CFRelease(customKey);
140 customKey = NULL;
141 }
142 }
143
144 int si_40_seckey_custom(int argc, char *const *argv)
145 {
146 plan_tests(18);
147
148
149 tests();
150
151 return 0;
152 }