]>
Commit | Line | Data |
---|---|---|
fa7225c8 A |
1 | /* |
2 | * Copyright (c) 2016 Apple Inc. All Rights Reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * This file contains Original Code and/or Modifications of Original Code | |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. Please obtain a copy of the License at | |
10 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
11 | * file. | |
12 | * | |
13 | * The Original Code and all software distributed under the License are | |
14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
18 | * Please see the License for the specific language governing rights and | |
19 | * limitations under the License. | |
20 | * | |
21 | * @APPLE_LICENSE_HEADER_END@ | |
22 | */ | |
23 | ||
24 | ||
25 | #import <Foundation/Foundation.h> | |
26 | ||
27 | #include "shared_regressions.h" | |
28 | ||
29 | static void test_export_import_run(int size) { | |
30 | NSError *error; | |
31 | id privKey = CFBridgingRelease(SecKeyCreateRandomKey((CFDictionaryRef)@{(id)kSecAttrKeyType: (id)kSecAttrKeyTypeECSECPrimeRandom, (id)kSecAttrKeySizeInBits: @(size)}, (void *)&error)); | |
32 | ok(privKey, "generate private key (size %d, error %@)", size, error); | |
33 | ||
34 | NSData *message = [NSData dataWithBytes:"hello" length:5]; | |
35 | error = nil; | |
36 | NSData *signature = CFBridgingRelease(SecKeyCreateSignature((SecKeyRef)privKey, kSecKeyAlgorithmECDSASignatureMessageX962SHA1, (CFDataRef)message, (void *)&error)); | |
37 | ok(signature, "create signature, %@", error); | |
38 | ||
39 | id pubKey = CFBridgingRelease(SecKeyCopyPublicKey((SecKeyRef)privKey)); | |
40 | error = nil; | |
41 | NSData *pubKeyData = CFBridgingRelease(SecKeyCopyExternalRepresentation((SecKeyRef)pubKey, (void *)&error)); | |
42 | ok(pubKeyData, "export public key, %@", error); | |
43 | size = (size + 7) / 8; | |
44 | is(pubKeyData.length, (unsigned)size * 2 + 1, "pubkey data has expected length"); | |
45 | ||
46 | id importedPubKey = CFBridgingRelease(SecKeyCreateWithData((CFDataRef)pubKeyData, (CFDictionaryRef)@{(id)kSecAttrKeyType: (id)kSecAttrKeyTypeECSECPrimeRandom, (id)kSecAttrKeyClass: (id)kSecAttrKeyClassPublic}, (void *)&error)); | |
47 | ok(importedPubKey, "import public key, %@", error); | |
48 | ok(SecKeyVerifySignature((SecKeyRef)importedPubKey, kSecKeyAlgorithmECDSASignatureMessageX962SHA1, (CFDataRef)message, (CFDataRef)signature, (void *)&error), "verify signature, %@", error); | |
49 | ||
50 | error = nil; | |
51 | NSData *privKeyData = CFBridgingRelease(SecKeyCopyExternalRepresentation((SecKeyRef)privKey, (void *)&error)); | |
52 | ok(privKeyData, "export privKey, %@", error); | |
53 | is(privKeyData.length, (unsigned)size * 3 + 1, "privkey data has expected length"); | |
54 | ||
55 | error = nil; | |
56 | id importedPrivKey = CFBridgingRelease(SecKeyCreateWithData((CFDataRef)privKeyData, (CFDictionaryRef)@{(id)kSecAttrKeyType: (id)kSecAttrKeyTypeECSECPrimeRandom, (id)kSecAttrKeyClass: (id)kSecAttrKeyClassPrivate}, (void *)&error)); | |
57 | ok(importedPrivKey, "import privKey, %@", error); | |
58 | ||
59 | id importedPrivKeyPubKey = CFBridgingRelease(SecKeyCopyPublicKey((SecKeyRef)importedPrivKey)); | |
60 | error = nil; | |
61 | ok(SecKeyVerifySignature((SecKeyRef)importedPrivKeyPubKey, kSecKeyAlgorithmECDSASignatureMessageX962SHA1, (CFDataRef)message, (CFDataRef)signature, (void *)&error), "verify signature, %@", error); | |
62 | } | |
63 | static const int TestExportImportRun = 10; | |
64 | ||
65 | static void test_export_import() { | |
66 | test_export_import_run(192); | |
67 | test_export_import_run(256); | |
68 | test_export_import_run(521); | |
69 | } | |
70 | static const int TestExportImport = TestExportImportRun * 3; | |
71 | ||
72 | static const int TestCount = TestExportImport; | |
73 | int si_44_seckey_ec(int argc, char *const *argv) { | |
74 | plan_tests(TestCount); | |
75 | ||
76 | @autoreleasepool { | |
77 | test_export_import(); | |
78 | } | |
79 | ||
80 | return 0; | |
81 | } |