tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

cryptox.h (7155B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 #ifndef CRYPTOX_H
      6 #define CRYPTOX_H
      7 
      8 #define XP_MIN_SIGNATURE_LEN_IN_BYTES 256
      9 
     10 #define CryptoX_Result int
     11 #define CryptoX_Success 0
     12 #define CryptoX_Error (-1)
     13 
     14 #if defined(MAR_NSS)
     15 
     16 #  include "cert.h"
     17 #  include "keyhi.h"
     18 #  include "cryptohi.h"
     19 
     20 #  define CryptoX_InvalidHandleValue NULL
     21 #  define CryptoX_ProviderHandle void*
     22 #  define CryptoX_SignatureHandle VFYContext*
     23 #  define CryptoX_PublicKey SECKEYPublicKey*
     24 #  define CryptoX_Certificate CERTCertificate*
     25 
     26 #  ifdef __cplusplus
     27 extern "C" {
     28 #  endif
     29 CryptoX_Result NSS_LoadPublicKey(const unsigned char* certData,
     30                                 unsigned int certDataSize,
     31                                 SECKEYPublicKey** publicKey);
     32 CryptoX_Result NSS_VerifyBegin(VFYContext** ctx,
     33                               SECKEYPublicKey* const* publicKey);
     34 CryptoX_Result NSS_VerifySignature(VFYContext* const* ctx,
     35                                   const unsigned char* signature,
     36                                   unsigned int signatureLen);
     37 #  ifdef __cplusplus
     38 }  // extern "C"
     39 #  endif
     40 
     41 #  define CryptoX_InitCryptoProvider(CryptoHandle) CryptoX_Success
     42 #  define CryptoX_VerifyBegin(CryptoHandle, SignatureHandle, PublicKey) \
     43    NSS_VerifyBegin(SignatureHandle, PublicKey)
     44 #  define CryptoX_FreeSignatureHandle(SignatureHandle) \
     45    VFY_DestroyContext(*SignatureHandle, PR_TRUE)
     46 #  define CryptoX_VerifyUpdate(SignatureHandle, buf, len) \
     47    VFY_Update(*SignatureHandle, (const unsigned char*)(buf), len)
     48 #  define CryptoX_LoadPublicKey(CryptoHandle, certData, dataSize, publicKey) \
     49    NSS_LoadPublicKey(certData, dataSize, publicKey)
     50 #  define CryptoX_VerifySignature(hash, publicKey, signedData, len) \
     51    NSS_VerifySignature(hash, (const unsigned char*)(signedData), len)
     52 #  define CryptoX_FreePublicKey(key) SECKEY_DestroyPublicKey(*key)
     53 #  define CryptoX_FreeCertificate(cert) CERT_DestroyCertificate(*cert)
     54 
     55 #elif XP_MACOSX
     56 
     57 #  define CryptoX_InvalidHandleValue NULL
     58 #  define CryptoX_ProviderHandle void*
     59 #  define CryptoX_SignatureHandle void*
     60 #  define CryptoX_PublicKey void*
     61 #  define CryptoX_Certificate void*
     62 
     63 // Forward-declare Objective-C functions implemented in MacVerifyCrypto.mm.
     64 #  ifdef __cplusplus
     65 extern "C" {
     66 #  endif
     67 CryptoX_Result CryptoMac_InitCryptoProvider();
     68 CryptoX_Result CryptoMac_VerifyBegin(CryptoX_SignatureHandle* aInputData);
     69 CryptoX_Result CryptoMac_VerifyUpdate(CryptoX_SignatureHandle* aInputData,
     70                                      void* aBuf, unsigned int aLen);
     71 CryptoX_Result CryptoMac_LoadPublicKey(const unsigned char* aCertData,
     72                                       unsigned int aDataSize,
     73                                       CryptoX_PublicKey* aPublicKey);
     74 CryptoX_Result CryptoMac_VerifySignature(CryptoX_SignatureHandle* aInputData,
     75                                         CryptoX_PublicKey* aPublicKey,
     76                                         const unsigned char* aSignature,
     77                                         unsigned int aSignatureLen);
     78 void CryptoMac_FreeSignatureHandle(CryptoX_SignatureHandle* aInputData);
     79 void CryptoMac_FreePublicKey(CryptoX_PublicKey* aPublicKey);
     80 #  ifdef __cplusplus
     81 }  // extern "C"
     82 #  endif
     83 
     84 #  define CryptoX_InitCryptoProvider(aProviderHandle) \
     85    CryptoMac_InitCryptoProvider()
     86 #  define CryptoX_VerifyBegin(aCryptoHandle, aInputData, aPublicKey) \
     87    CryptoMac_VerifyBegin(aInputData)
     88 #  define CryptoX_VerifyUpdate(aInputData, aBuf, aLen) \
     89    CryptoMac_VerifyUpdate(aInputData, aBuf, aLen)
     90 #  define CryptoX_LoadPublicKey(aProviderHandle, aCertData, aDataSize, \
     91                                aPublicKey)                            \
     92    CryptoMac_LoadPublicKey(aCertData, aDataSize, aPublicKey)
     93 #  define CryptoX_VerifySignature(aInputData, aPublicKey, aSignature, \
     94                                  aSignatureLen)                      \
     95    CryptoMac_VerifySignature(aInputData, aPublicKey, aSignature, aSignatureLen)
     96 #  define CryptoX_FreeSignatureHandle(aInputData) \
     97    CryptoMac_FreeSignatureHandle(aInputData)
     98 #  define CryptoX_FreePublicKey(aPublicKey) CryptoMac_FreePublicKey(aPublicKey)
     99 #  define CryptoX_FreeCertificate(aCertificate)
    100 
    101 #elif defined(XP_WIN)
    102 
    103 #  include <windows.h>
    104 #  include <wincrypt.h>
    105 
    106 CryptoX_Result CryptoAPI_InitCryptoContext(HCRYPTPROV* provider);
    107 CryptoX_Result CryptoAPI_LoadPublicKey(HCRYPTPROV hProv, BYTE* certData,
    108                                       DWORD sizeOfCertData,
    109                                       HCRYPTKEY* publicKey);
    110 CryptoX_Result CryptoAPI_VerifyBegin(HCRYPTPROV provider, HCRYPTHASH* hash);
    111 CryptoX_Result CryptoAPI_VerifyUpdate(HCRYPTHASH* hash, BYTE* buf, DWORD len);
    112 CryptoX_Result CryptoAPI_VerifySignature(HCRYPTHASH* hash, HCRYPTKEY* pubKey,
    113                                         const BYTE* signature,
    114                                         DWORD signatureLen);
    115 
    116 #  define CryptoX_InvalidHandleValue ((ULONG_PTR)NULL)
    117 #  define CryptoX_ProviderHandle HCRYPTPROV
    118 #  define CryptoX_SignatureHandle HCRYPTHASH
    119 #  define CryptoX_PublicKey HCRYPTKEY
    120 #  define CryptoX_Certificate HCERTSTORE
    121 #  define CryptoX_InitCryptoProvider(CryptoHandle) \
    122    CryptoAPI_InitCryptoContext(CryptoHandle)
    123 #  define CryptoX_VerifyBegin(CryptoHandle, SignatureHandle, PublicKey) \
    124    CryptoAPI_VerifyBegin(CryptoHandle, SignatureHandle)
    125 #  define CryptoX_FreeSignatureHandle(SignatureHandle)
    126 #  define CryptoX_VerifyUpdate(SignatureHandle, buf, len) \
    127    CryptoAPI_VerifyUpdate(SignatureHandle, (BYTE*)(buf), len)
    128 #  define CryptoX_LoadPublicKey(CryptoHandle, certData, dataSize, publicKey) \
    129    CryptoAPI_LoadPublicKey(CryptoHandle, (BYTE*)(certData), dataSize,       \
    130                            publicKey)
    131 #  define CryptoX_VerifySignature(hash, publicKey, signedData, len) \
    132    CryptoAPI_VerifySignature(hash, publicKey, signedData, len)
    133 #  define CryptoX_FreePublicKey(key) CryptDestroyKey(*(key))
    134 #  define CryptoX_FreeCertificate(cert) \
    135    CertCloseStore(*(cert), CERT_CLOSE_STORE_FORCE_FLAG);
    136 
    137 #else
    138 
    139 /* This default implementation is necessary because we don't want to
    140 * link to NSS from updater code on non Windows platforms.  On Windows
    141 * we use CyrptoAPI instead of NSS.  We don't call any function as they
    142 * would just fail, but this simplifies linking.
    143 */
    144 
    145 #  define CryptoX_InvalidHandleValue NULL
    146 #  define CryptoX_ProviderHandle void*
    147 #  define CryptoX_SignatureHandle void*
    148 #  define CryptoX_PublicKey void*
    149 #  define CryptoX_Certificate void*
    150 #  define CryptoX_InitCryptoProvider(CryptoHandle) CryptoX_Error
    151 #  define CryptoX_VerifyBegin(CryptoHandle, SignatureHandle, PublicKey) \
    152    CryptoX_Error
    153 #  define CryptoX_FreeSignatureHandle(SignatureHandle)
    154 #  define CryptoX_VerifyUpdate(SignatureHandle, buf, len) CryptoX_Error
    155 #  define CryptoX_LoadPublicKey(CryptoHandle, certData, dataSize, publicKey) \
    156    CryptoX_Error
    157 #  define CryptoX_VerifySignature(hash, publicKey, signedData, len) \
    158    CryptoX_Error
    159 #  define CryptoX_FreePublicKey(key) CryptoX_Error
    160 
    161 #endif
    162 
    163 #endif