1#include <Availability.h>
2#include "capabilities.h"
3#include "testmore.h"
4#include "testbyteBuffer.h"
5
6#if (CCEC == 0)
7entryPoint(CommonEC,"Elliptic Curve Cryptography")
8#else
9
10#include <CommonCrypto/CommonECCryptor.h>
11
12static int kTestTestCount = 9;
13
14int CommonEC(int argc, char *const *argv) {
15	CCCryptorStatus retval;
16    size_t keysize;
17    CCECCryptorRef publicKey, privateKey;
18    CCECCryptorRef publicKey2;
19    // byteBuffer keydata, dekeydata;
20    byteBuffer hash;
21    char encryptedKey[8192];
22    size_t encryptedKeyLen = 8192;
23    // char decryptedKey[8192];
24    // size_t decryptedKeyLen = 8192;
25    char signature[8192];
26    size_t signatureLen = 8192;
27    char importexport[8192];
28    size_t importexportLen = 8192;
29    uint32_t valid;
30    int accum = 0;
31    int debug = 0;
32
33	plan_tests(kTestTestCount);
34
35    keysize = 256;
36
37    retval = CCECCryptorGeneratePair(keysize, &publicKey, &privateKey);
38    if(debug) printf("Keys Generated\n");
39    ok(retval == 0, "Generate an EC Key Pair");
40	accum |= retval;
41
42#ifdef ECDH
43    keydata = hexStringToBytes("000102030405060708090a0b0c0d0e0f");
44
45    retval = CCECCryptorWrapKey(publicKey, keydata->bytes, keydata->len, encryptedKey, &encryptedKeyLen, kCCDigestSHA1);
46
47    ok(retval == 0, "Wrap Key Data with EC Encryption - ccPKCS1Padding");
48    accum |= retval;
49
50    retval = CCECCryptorUnwrapKey(privateKey, encryptedKey, encryptedKeyLen,
51                        decryptedKey, &decryptedKeyLen);
52
53    ok(retval == 0, "Unwrap Key Data with EC Encryption - ccPKCS1Padding");
54    accum |= retval;
55
56	dekeydata = bytesToBytes(decryptedKey, decryptedKeyLen);
57
58	ok(bytesAreEqual(dekeydata, keydata), "Round Trip CCECCryptorWrapKey/CCECCryptorUnwrapKey");
59    accum |= retval;
60#endif
61
62
63    hash = hexStringToBytes("000102030405060708090a0b0c0d0e0f");
64
65    retval = CCECCryptorSignHash(privateKey,
66                     hash->bytes, hash->len,
67                     signature, &signatureLen);
68
69    ok(retval == 0, "EC Signing");
70    valid = 0;
71    accum |= retval;
72    if(debug) printf("Signing Complete\n");
73
74    retval = CCECCryptorVerifyHash(publicKey,
75                       hash->bytes, hash->len,
76                       signature, signatureLen, &valid);
77    ok(retval == 0, "EC Verifying");
78    accum |= retval;
79	ok(valid, "EC Validity");
80    accum |= retval;
81    if(debug) printf("Verify Complete\n");
82
83    // Mess with the sig - see what happens
84    signature[signatureLen-3] += 3;
85    retval = CCECCryptorVerifyHash(publicKey,
86                                   hash->bytes, hash->len,
87                                   signature, signatureLen, &valid);
88    ok(retval == 0, "EC Verifying");
89    accum |= retval;
90	ok(!valid, "EC Invalid Signature");
91    accum |= retval;
92
93    if(debug) printf("Verify2 Complete\n");
94
95    encryptedKeyLen = 8192;
96	retval = CCECCryptorExportPublicKey(publicKey, importexport, &importexportLen);
97
98    ok(retval == 0, "EC Export Public Key");
99    accum |= retval;
100
101    retval = CCECCryptorImportPublicKey(importexport, importexportLen, &publicKey2);
102
103    ok(retval == 0, "EC Import Public Key");
104    accum |= retval;
105
106	encryptedKeyLen = 8192;
107    retval = CCECCryptorComputeSharedSecret(privateKey, publicKey, encryptedKey, &encryptedKeyLen);
108
109    ok(retval == 0, "EC Shared Secret");
110    accum |= retval;
111
112    return accum;
113}
114#endif /* CCEC */
115