xmlsecurityengine/xmlseccrypto/inc/xmlsecc_cryptowrapper.h
changeset 0 e35f40988205
equal deleted inserted replaced
-1:000000000000 0:e35f40988205
       
     1 /*
       
     2 * Copyright (c) 2005-2006 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Methods that allows to encrypt and decrypt data.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 /* A wrapper for gcrypt.h */
       
    20 
       
    21 #ifndef __SYMBIANCRYPTO_CRYPTO_WRAPPER_H__
       
    22 #define __SYMBIANCRYPTO_CRYPTO_WRAPPER_H__    
       
    23 
       
    24 #include <e32def.h>
       
    25 #include "xmlsecc_config.h"
       
    26 
       
    27 #include "xmlsecc_evpwrapper.h"
       
    28 
       
    29 #ifdef __cplusplus
       
    30 extern "C" {
       
    31 #endif /* __cplusplus */ 
       
    32 
       
    33 
       
    34 /************************************
       
    35  *                                  
       
    36  *   symmetric cipher functions     
       
    37  *                                  
       
    38  ************************************/
       
    39 
       
    40 /* Port SC_CIPHER_HD */
       
    41 
       
    42 #ifndef _SC_GCC_ATTR_DEPRECATED
       
    43 #define _SC_GCC_ATTR_DEPRECATED
       
    44 #endif
       
    45 
       
    46 struct sc_cipher_handle;
       
    47 typedef struct sc_cipher_handle *sc_cipher_hd_t;
       
    48 typedef struct sc_cipher_handle *SC_CIPHER_HD _SC_GCC_ATTR_DEPRECATED;
       
    49 typedef struct sc_cipher_handle *ScCipherHd _SC_GCC_ATTR_DEPRECATED;
       
    50 typedef sc_cipher_hd_t 	ScCipherHd;
       
    51 
       
    52 #define	ISLAST	1
       
    53 #define NOTLAST	0
       
    54 
       
    55 /* data type compatibility */
       
    56 typedef unsigned char 			byte;
       
    57 
       
    58 
       
    59 /* Module specification structure for ciphers.  */
       
    60 typedef struct sc_cipher_spec
       
    61 {
       
    62   const char *name;
       
    63   const char **aliases;
       
    64   size_t blocksize;
       
    65   size_t keylen;
       
    66   size_t contextsize;
       
    67 } sc_cipher_spec_t;
       
    68 
       
    69 /* All symmetric encryption algorithms are identified by their IDs.
       
    70    More IDs may be registered at runtime. */
       
    71 enum sc_cipher_algos
       
    72   {
       
    73     SC_CIPHER_NONE        = 0,
       
    74     SC_CIPHER_IDEA        = 1,
       
    75     SC_CIPHER_3DES        = 2,
       
    76     SC_CIPHER_CAST5       = 3,
       
    77     SC_CIPHER_BLOWFISH    = 4,
       
    78     SC_CIPHER_SAFER_SK128 = 5,
       
    79     SC_CIPHER_DES_SK      = 6,
       
    80     SC_CIPHER_AES128      = 7,
       
    81     SC_CIPHER_AES192      = 8,
       
    82     SC_CIPHER_AES256      = 9,
       
    83     SC_CIPHER_TWOFISH     = 10,
       
    84 
       
    85     /* Other cipher numbers are above 300 for OpenPGP reasons. */
       
    86     SC_CIPHER_ARCFOUR     = 301,  /* Fully compatible with RSA's RC4 (tm). */
       
    87     SC_CIPHER_DES         = 302,  /* Yes, this is single key 56 bit DES. */
       
    88     SC_CIPHER_TWOFISH128  = 303,
       
    89     SC_CIPHER_SERPENT128  = 304,
       
    90     SC_CIPHER_SERPENT192  = 305,
       
    91     SC_CIPHER_SERPENT256  = 306,
       
    92     SC_CIPHER_RFC2268_40  = 307,  /* Ron's Cipher 2 (40 bit). */
       
    93     SC_CIPHER_RFC2268_128 = 308   /* Ron's Cipher 2 (128 bit). */
       
    94   };
       
    95 
       
    96 /* The supported encryption modes.  Note that not all of them are
       
    97    supported for each algorithm. */
       
    98 enum sc_cipher_modes 
       
    99   {
       
   100     SC_CIPHER_MODE_NONE   = 0,  /* Not yet specified. */
       
   101     SC_CIPHER_MODE_ECB    = 1,  /* Electronic codebook. */
       
   102     SC_CIPHER_MODE_CFB    = 2,  /* Cipher feedback. */
       
   103     SC_CIPHER_MODE_CBC    = 3,  /* Cipher block chaining. */
       
   104     SC_CIPHER_MODE_STREAM = 4,  /* Used with stream ciphers. */
       
   105     SC_CIPHER_MODE_OFB    = 5,  /* Outer feedback. */
       
   106     SC_CIPHER_MODE_CTR    = 6   /* Counter. */
       
   107   };
       
   108 
       
   109 /* Flags used with the open function. */ 
       
   110 enum sc_cipher_flags
       
   111   {
       
   112     SC_CIPHER_SECURE      = 1,  /* Allocate in secure memory. */
       
   113     SC_CIPHER_ENABLE_SYNC = 2,  /* Enable CFB sync mode. */
       
   114     SC_CIPHER_CBC_CTS     = 4,  /* Enable CBC cipher text stealing (CTS). */
       
   115     SC_CIPHER_CBC_MAC     = 8   /* Enable CBC message auth. code (MAC). */
       
   116   };
       
   117 
       
   118 /* To avoid that a compiler optimizes certain memset calls away, these
       
   119    macros may be used instead. */
       
   120 #define wipememory2(_ptr,_set,_len) do { \
       
   121               volatile char *_vptr=(volatile char *)(_ptr); \
       
   122               size_t _vlen=(_len); \
       
   123               while(_vlen) { *_vptr=(_set); _vptr++; _vlen--; } \
       
   124                   } while(0)
       
   125 #define wipememory(_ptr,_len) wipememory2(_ptr,0,_len)
       
   126 
       
   127 
       
   128 
       
   129 /************************************
       
   130  *                                  
       
   131  *   random generating functions    
       
   132  *                                  
       
   133  ************************************/
       
   134 
       
   135 /* The possible values for the random quality.  The rule of thumb is
       
   136    to use STRONG for session keys and VERY_STRONG for key material.
       
   137    WEAK is currently an alias for STRONG and should not be used
       
   138    anymore - use sc_create_nonce instead. */
       
   139 typedef enum sc_random_level
       
   140   {
       
   141     SC_WEAK_RANDOM = 0,
       
   142     SC_STRONG_RANDOM = 1,
       
   143     SC_VERY_STRONG_RANDOM = 2
       
   144   }
       
   145 sc_random_level_t;
       
   146 
       
   147 /* Fill BUFFER with LENGTH bytes of random, using random numbers of
       
   148    quality LEVEL. */
       
   149 TInt sc_randomize (unsigned char *buffer, size_t length,
       
   150                      enum sc_random_level level);
       
   151 
       
   152 /* Retrieve the block length used with algorithm A. */
       
   153 size_t sc_cipher_get_algo_blklen (int algo);
       
   154 
       
   155 /* Set the IV to be used for the encryption context C to IV with
       
   156    length IVLEN.  The length should match the required length. */
       
   157 int sc_cipher_setiv ( sc_cipher_hd_t c, const byte *iv, unsigned int ivlen );
       
   158 
       
   159 /* Encrypt the plaintext of size INLEN in IN using the cipher handle H
       
   160    into the buffer OUT which has an allocated length of OUTSIZE.  For
       
   161    most algorithms it is possible to pass NULL for in and 0 for INLEN
       
   162    and do a in-place decryption of the data provided in OUT.  */
       
   163 int sc_cipher_encrypt (sc_cipher_hd_t h,
       
   164                                   void *out, size_t outsize,
       
   165                                   const void *in, size_t inlen, int last);
       
   166 
       
   167 
       
   168 /* The counterpart to sc_cipher_encrypt.  */
       
   169 int sc_cipher_decrypt (sc_cipher_hd_t h,
       
   170                                   void *out, size_t outsize,
       
   171                                   const void *in, size_t inlen, int *outlen, int last);
       
   172 
       
   173 /* Create a handle for algorithm ALGO to be used in MODE.  FLAGS may
       
   174    be given as an bitwise OR of the sc_cipher_flags values. */
       
   175 int sc_cipher_open (sc_cipher_hd_t *handle,
       
   176                               int algo, int mode, unsigned int flags);
       
   177 
       
   178 
       
   179 /* Close the cioher handle H and release all resource. */
       
   180 void sc_cipher_close (sc_cipher_hd_t h);
       
   181 
       
   182 /* Retrieved the key length used with algorithm A. */
       
   183 size_t sc_cipher_get_algo_keylen (int algo);
       
   184 
       
   185 /* Set the key to be used for the encryption context C to KEY with
       
   186    length KEYLEN.  The length should match the required length. */
       
   187 int
       
   188 sc_cipher_setkey (sc_cipher_hd_t c, byte *key, unsigned int keylen);
       
   189 
       
   190 /* Set specification blocksize in context */
       
   191 void set_ctx_blocksize(sc_cipher_hd_t c, size_t bklen);
       
   192 
       
   193 
       
   194 /************************************
       
   195  *                                  
       
   196  *   cryptograhic hash functions    
       
   197  *                                  
       
   198  ************************************/
       
   199 
       
   200 #define XMLSEC_SYMBIAN_MAX_DIGEST_SIZE		32
       
   201 
       
   202 /* Algorithm IDs for the hash functions we know about. Not all of them
       
   203    are implemnted. */
       
   204 enum sc_md_algos
       
   205   {
       
   206     SC_MD_NONE    = 0,  
       
   207     SC_MD_MD5     = 1,
       
   208     SC_MD_SHA1    = 2,
       
   209     SC_MD_RMD160  = 3,
       
   210     SC_MD_MD2     = 5,
       
   211     SC_MD_TIGER   = 6,   /* TIGER/192. */
       
   212     SC_MD_HAVAL   = 7,   /* HAVAL, 5 pass, 160 bit. */
       
   213     SC_MD_SHA256  = 8,
       
   214     SC_MD_SHA384  = 9,
       
   215     SC_MD_SHA512  = 10,
       
   216     SC_MD_MD4     = 301,
       
   217     SC_MD_CRC32               = 302,
       
   218     SC_MD_CRC32_RFC1510       = 303,
       
   219     SC_MD_CRC24_RFC2440       = 304
       
   220   };
       
   221 
       
   222 /* Flags used with the open function.  */
       
   223 enum sc_md_flags
       
   224   {
       
   225     SC_MD_FLAG_SECURE = 1,  /* Allocate all buffers in "secure"
       
   226                                  memory.  */
       
   227     SC_MD_FLAG_HMAC   = 2   /* Make an HMAC out of this
       
   228                                  algorithm.  */
       
   229   };
       
   230     
       
   231 /* Forward declaration.  */
       
   232 struct sc_md_handle;
       
   233 typedef struct sc_md_handle *sc_md_hd_t;
       
   234 
       
   235 
       
   236 /* Compatibility types, do not use them.  */
       
   237 typedef struct sc_md_handle *SC_MD_HD _SC_GCC_ATTR_DEPRECATED;
       
   238 typedef struct sc_md_handle *ScMDHd _SC_GCC_ATTR_DEPRECATED;
       
   239 
       
   240 /* Create a message digest object for algorithm ALGO.  FLAGS may be
       
   241    given as an bitwise OR of the sc_md_flags values.  ALGO may be
       
   242    given as 0 if the algorithms to be used are later set using
       
   243    gcry_md_enable.  */
       
   244 int sc_md_open (sc_md_hd_t *h, int algo, unsigned int flags);
       
   245 
       
   246 /* Release the message digest object HD.  */
       
   247 void sc_md_close (sc_md_hd_t hd);
       
   248 
       
   249 /* Pass LENGTH bytes of data in BUFFER to the digest object HD so that
       
   250    it can update the digest values.  This is the actual hash
       
   251    function. */
       
   252 void sc_md_write (sc_md_hd_t hd, unsigned char *buffer, size_t length);
       
   253 
       
   254 /* Read out the final digest from HD return the digest value for
       
   255    algorithm ALGO. */
       
   256 const unsigned char *sc_md_read (sc_md_hd_t hd, int algo);
       
   257 
       
   258 
       
   259 /* Retrieve the length in bytes of the digest yielded by algorithm
       
   260    ALGO. */
       
   261 unsigned int sc_md_get_algo_dlen (sc_md_hd_t hd);
       
   262 
       
   263 /* Finalize the digest calculation.  This is not really needed because
       
   264    sc_md_read() does this implicitly. */
       
   265 void sc_md_final(sc_md_hd_t a);
       
   266 
       
   267 /* Set key for HMAC */
       
   268 int sc_md_setkey(sc_md_hd_t hd, unsigned char *buffer, size_t length);
       
   269 
       
   270 /************************************
       
   271  *                                  
       
   272  *            RSA Sign functions    
       
   273  *                                  
       
   274  ************************************/
       
   275 
       
   276 /* data type compatibility */
       
   277 
       
   278 // OpenSSL
       
   279 
       
   280 //typedef ScMDHd				EVP_MD_CTX;
       
   281 #define EVP_MD_CTX	ScMDHd
       
   282 
       
   283 #define sc_sign_init(a,b)		sc_md_open(a,b,SC_MD_FLAG_SECURE)	
       
   284 #define sc_sign_update(a,b,c)	sc_md_write(a,b,c)
       
   285 
       
   286 
       
   287 /* Signature final */
       
   288 TInt sc_sign_final(sc_md_hd_t hd,unsigned char *outbuf, unsigned int *outlen, EVP_PKEY *pkey);
       
   289 
       
   290 /************************************
       
   291  *                                  
       
   292  *          RSA Verify functions    
       
   293  *                                  
       
   294  ************************************/
       
   295  
       
   296 #define sc_verify_init(a,b)  		sc_md_open(a,b,SC_MD_FLAG_SECURE)
       
   297 #define sc_verify_update(a,b,c) 	sc_md_write(a,b,c)
       
   298 
       
   299 /* Verify final */
       
   300 TInt sc_verify_final(sc_md_hd_t hd, unsigned char *signature, unsigned int len, EVP_PKEY *pkey);
       
   301 
       
   302 #ifdef __cplusplus
       
   303 }
       
   304 #endif /* __cplusplus */
       
   305 
       
   306 #endif /* __SYMBIANCRYPTO_CRYPTO_WRAPPER_H__ */
       
   307 
       
   308 #define __SYMBIANCRYPTO_CRYPTO_WRAPPER_H__