Home | History | Annotate | Download | only in webcrypto
      1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef CONTENT_RENDERER_WEBCRYPTO_WEBCRYPTO_IMPL_H_
      6 #define CONTENT_RENDERER_WEBCRYPTO_WEBCRYPTO_IMPL_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "base/compiler_specific.h"
     10 #include "content/common/content_export.h"
     11 #include "third_party/WebKit/public/platform/WebCrypto.h"
     12 
     13 namespace content {
     14 
     15 class CONTENT_EXPORT WebCryptoImpl
     16     : NON_EXPORTED_BASE(public blink::WebCrypto) {
     17  public:
     18   WebCryptoImpl();
     19 
     20   virtual void encrypt(
     21       const blink::WebCryptoAlgorithm& algorithm,
     22       const blink::WebCryptoKey& key,
     23       const unsigned char* data,
     24       unsigned data_size,
     25       blink::WebCryptoResult result);
     26   virtual void decrypt(
     27       const blink::WebCryptoAlgorithm& algorithm,
     28       const blink::WebCryptoKey& key,
     29       const unsigned char* data,
     30       unsigned data_size,
     31       blink::WebCryptoResult result);
     32   virtual void digest(
     33       const blink::WebCryptoAlgorithm& algorithm,
     34       const unsigned char* data,
     35       unsigned data_size,
     36       blink::WebCryptoResult result);
     37   virtual void generateKey(
     38       const blink::WebCryptoAlgorithm& algorithm,
     39       bool extractable,
     40       blink::WebCryptoKeyUsageMask usage_mask,
     41       blink::WebCryptoResult result);
     42   virtual void importKey(
     43       blink::WebCryptoKeyFormat format,
     44       const unsigned char* key_data,
     45       unsigned key_data_size,
     46       const blink::WebCryptoAlgorithm& algorithm_or_null,
     47       bool extractable,
     48       blink::WebCryptoKeyUsageMask usage_mask,
     49       blink::WebCryptoResult result);
     50   virtual void exportKey(
     51       blink::WebCryptoKeyFormat format,
     52       const blink::WebCryptoKey& key,
     53       blink::WebCryptoResult result);
     54   virtual void sign(
     55       const blink::WebCryptoAlgorithm& algorithm,
     56       const blink::WebCryptoKey& key,
     57       const unsigned char* data,
     58       unsigned data_size,
     59       blink::WebCryptoResult result);
     60   virtual void verifySignature(
     61       const blink::WebCryptoAlgorithm& algorithm,
     62       const blink::WebCryptoKey& key,
     63       const unsigned char* signature,
     64       unsigned signature_size,
     65       const unsigned char* data,
     66       unsigned data_size,
     67       blink::WebCryptoResult result);
     68 
     69 
     70  protected:
     71   friend class WebCryptoImplTest;
     72 
     73   void Init();
     74 
     75   bool EncryptInternal(
     76       const blink::WebCryptoAlgorithm& algorithm,
     77       const blink::WebCryptoKey& key,
     78       const unsigned char* data,
     79       unsigned data_size,
     80       blink::WebArrayBuffer* buffer);
     81   bool DecryptInternal(
     82       const blink::WebCryptoAlgorithm& algorithm,
     83       const blink::WebCryptoKey& key,
     84       const unsigned char* data,
     85       unsigned data_size,
     86       blink::WebArrayBuffer* buffer);
     87   bool DigestInternal(
     88       const blink::WebCryptoAlgorithm& algorithm,
     89       const unsigned char* data,
     90       unsigned data_size,
     91       blink::WebArrayBuffer* buffer);
     92   bool GenerateKeyInternal(
     93       const blink::WebCryptoAlgorithm& algorithm,
     94       bool extractable,
     95       blink::WebCryptoKeyUsageMask usage_mask,
     96       blink::WebCryptoKey* key);
     97   bool GenerateKeyPairInternal(
     98       const blink::WebCryptoAlgorithm& algorithm,
     99       bool extractable,
    100       blink::WebCryptoKeyUsageMask usage_mask,
    101       blink::WebCryptoKey* public_key,
    102       blink::WebCryptoKey* private_key);
    103   bool ImportKeyInternal(
    104       blink::WebCryptoKeyFormat format,
    105       const unsigned char* key_data,
    106       unsigned key_data_size,
    107       const blink::WebCryptoAlgorithm& algorithm_or_null,
    108       bool extractable,
    109       blink::WebCryptoKeyUsageMask usage_mask,
    110       blink::WebCryptoKey* key);
    111   bool ExportKeyInternal(
    112       blink::WebCryptoKeyFormat format,
    113       const blink::WebCryptoKey& key,
    114       blink::WebArrayBuffer* buffer);
    115   bool SignInternal(
    116       const blink::WebCryptoAlgorithm& algorithm,
    117       const blink::WebCryptoKey& key,
    118       const unsigned char* data,
    119       unsigned data_size,
    120       blink::WebArrayBuffer* buffer);
    121   bool VerifySignatureInternal(
    122       const blink::WebCryptoAlgorithm& algorithm,
    123       const blink::WebCryptoKey& key,
    124       const unsigned char* signature,
    125       unsigned signature_size,
    126       const unsigned char* data,
    127       unsigned data_size,
    128       bool* signature_match);
    129 
    130   bool ImportKeyJwk(
    131       const unsigned char* key_data,
    132       unsigned key_data_size,
    133       const blink::WebCryptoAlgorithm& algorithm_or_null,
    134       bool extractable,
    135       blink::WebCryptoKeyUsageMask usage_mask,
    136       blink::WebCryptoKey* key);
    137   bool ImportRsaPublicKeyInternal(
    138       const unsigned char* modulus_data,
    139       unsigned modulus_size,
    140       const unsigned char* exponent_data,
    141       unsigned exponent_size,
    142       const blink::WebCryptoAlgorithm& algorithm,
    143       bool extractable,
    144       blink::WebCryptoKeyUsageMask usage_mask,
    145       blink::WebCryptoKey* key);
    146 
    147  private:
    148   DISALLOW_COPY_AND_ASSIGN(WebCryptoImpl);
    149 };
    150 
    151 }  // namespace content
    152 
    153 #endif  // CONTENT_RENDERER_WEBCRYPTO_WEBCRYPTO_IMPL_H_
    154