2 * Copyright (C) 2013 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
28 #if ENABLE(SUBTLE_CRYPTO)
30 #include "CryptoAlgorithmIdentifier.h"
31 #include <CommonCrypto/CommonCryptor.h>
32 #include <wtf/Vector.h>
34 #if USE(APPLE_INTERNAL_SDK)
35 #include <CommonCrypto/CommonCryptorSPI.h>
36 #include <CommonCrypto/CommonECCryptor.h>
37 #include <CommonCrypto/CommonRSACryptor.h>
38 #include <CommonCrypto/CommonRandomSPI.h>
41 #ifndef _CC_RSACRYPTOR_H_
50 typedef uint32_t CCDigestAlgorithm;
56 typedef uint32_t CCRSAKeyType;
59 ccPKCS1Padding = 1001,
62 typedef uint32_t CCAsymmetricPadding;
65 kCCNotVerified = -4306
69 typedef struct _CCBigNumRef *CCBigNumRef;
71 typedef struct __CCRandom *CCRandomRef;
72 extern const CCRandomRef kCCRandomDefault;
73 extern "C" int CCRandomCopyBytes(CCRandomRef rnd, void *bytes, size_t count);
75 typedef struct _CCRSACryptor *CCRSACryptorRef;
76 extern "C" CCCryptorStatus CCRSACryptorEncrypt(CCRSACryptorRef publicKey, CCAsymmetricPadding padding, const void *plainText, size_t plainTextLen, void *cipherText, size_t *cipherTextLen, const void *tagData, size_t tagDataLen, CCDigestAlgorithm digestType);
77 extern "C" CCCryptorStatus CCRSACryptorDecrypt(CCRSACryptorRef privateKey, CCAsymmetricPadding padding, const void *cipherText, size_t cipherTextLen, void *plainText, size_t *plainTextLen, const void *tagData, size_t tagDataLen, CCDigestAlgorithm digestType);
78 extern "C" CCCryptorStatus CCRSACryptorSign(CCRSACryptorRef privateKey, CCAsymmetricPadding padding, const void *hashToSign, size_t hashSignLen, CCDigestAlgorithm digestType, size_t saltLen, void *signedData, size_t *signedDataLen);
79 extern "C" CCCryptorStatus CCRSACryptorVerify(CCRSACryptorRef publicKey, CCAsymmetricPadding padding, const void *hash, size_t hashLen, CCDigestAlgorithm digestType, size_t saltLen, const void *signedData, size_t signedDataLen);
80 extern "C" CCCryptorStatus CCRSACryptorCreateFromData(CCRSAKeyType keyType, uint8_t *modulus, size_t modulusLength, uint8_t *exponent, size_t exponentLength, uint8_t *p, size_t pLength, uint8_t *q, size_t qLength, CCRSACryptorRef *ref);
81 extern "C" CCCryptorStatus CCRSACryptorGeneratePair(size_t keysize, uint32_t e, CCRSACryptorRef *publicKey, CCRSACryptorRef *privateKey);
82 extern "C" CCRSACryptorRef CCRSACryptorGetPublicKeyFromPrivateKey(CCRSACryptorRef privkey);
83 extern "C" void CCRSACryptorRelease(CCRSACryptorRef key);
84 extern "C" CCCryptorStatus CCRSAGetKeyComponents(CCRSACryptorRef rsaKey, uint8_t *modulus, size_t *modulusLength, uint8_t *exponent, size_t *exponentLength, uint8_t *p, size_t *pLength, uint8_t *q, size_t *qLength);
85 extern "C" CCRSAKeyType CCRSAGetKeyType(CCRSACryptorRef key);
86 extern "C" CCCryptorStatus CCRSACryptorImport(const void *keyPackage, size_t keyPackageLen, CCRSACryptorRef *key);
87 extern "C" CCCryptorStatus CCRSACryptorExport(CCRSACryptorRef key, void *out, size_t *outLen);
89 #ifndef _CC_ECCRYPTOR_H_
94 typedef uint32_t CCECKeyType;
97 kCCImportKeyBinary = 0,
99 typedef uint32_t CCECKeyExternalFormat;
102 typedef struct _CCECCryptor *CCECCryptorRef;
103 extern "C" CCCryptorStatus CCECCryptorGeneratePair(size_t keysize, CCECCryptorRef *publicKey, CCECCryptorRef *privateKey);
104 extern "C" void CCECCryptorRelease(CCECCryptorRef key);
105 extern "C" CCCryptorStatus CCECCryptorImportKey(CCECKeyExternalFormat format, void *keyPackage, size_t keyPackageLen, CCECKeyType keyType, CCECCryptorRef *key);
106 extern "C" CCCryptorStatus CCECCryptorExportKey(CCECKeyExternalFormat format, void *keyPackage, size_t *keyPackageLen, CCECKeyType keyType, CCECCryptorRef key);
107 extern "C" int CCECGetKeySize(CCECCryptorRef key);
108 extern "C" CCCryptorStatus CCECCryptorCreateFromData(size_t keySize, uint8_t *qX, size_t qXLength, uint8_t *qY, size_t qYLength, CCECCryptorRef *ref);
109 extern "C" CCCryptorStatus CCECCryptorGetKeyComponents(CCECCryptorRef ecKey, size_t *keySize, uint8_t *qX, size_t *qXLength, uint8_t *qY, size_t *qYLength, uint8_t *d, size_t *dLength);
110 extern "C" CCCryptorStatus CCECCryptorComputeSharedSecret(CCECCryptorRef privateKey, CCECCryptorRef publicKey, void *out, size_t *outLen);
112 #if !USE(APPLE_INTERNAL_SDK)
113 extern "C" CCCryptorStatus CCCryptorGCM(CCOperation op, CCAlgorithm alg, const void* key, size_t keyLength, const void* iv, size_t ivLen, const void* aData, size_t aDataLen, const void* dataIn, size_t dataInLength, void* dataOut, void* tag, size_t* tagLength);
120 CCBigNum(const uint8_t*, size_t);
123 CCBigNum(const CCBigNum&);
124 CCBigNum(CCBigNum&&);
125 CCBigNum& operator=(const CCBigNum&);
126 CCBigNum& operator=(CCBigNum&&);
128 Vector<uint8_t> data() const;
130 CCBigNum operator-(uint32_t) const;
131 CCBigNum operator%(const CCBigNum&) const;
132 CCBigNum inverse(const CCBigNum& modulus) const;
135 CCBigNum(CCBigNumRef);
137 CCBigNumRef m_number;
140 bool getCommonCryptoDigestAlgorithm(CryptoAlgorithmIdentifier, CCDigestAlgorithm&);
142 } // namespace WebCore
144 #endif // ENABLE(SUBTLE_CRYPTO)