cyassl re-port with cellular comms, PSK test

Dependencies:   VodafoneUSBModem_bleedingedge2 mbed-rtos mbed-src

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers evp.h Source File

evp.h

00001 /* evp.h
00002  *
00003  * Copyright (C) 2012 Sawtooth Consulting Ltd.
00004  *
00005  * This file is part of CyaSSL.
00006  *
00007  * CyaSSL is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * CyaSSL is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
00020  */
00021 
00022 
00023 /*  evp.h defines mini evp openssl compatibility layer 
00024  *
00025  */
00026 
00027 
00028 #ifndef CYASSL_EVP_H_
00029 #define CYASSL_EVP_H_
00030 
00031 #include <cyassl/ctaocrypt/settings.h>
00032 
00033 #ifdef YASSL_PREFIX
00034 #include "prefix_evp.h"
00035 #endif
00036 
00037 #include <cyassl/openssl/md5.h>
00038 #include <cyassl/openssl/sha.h>
00039 #include <cyassl/openssl/ripemd.h>
00040 #include <cyassl/openssl/rsa.h>
00041 #include <cyassl/openssl/dsa.h>
00042 
00043 #include <cyassl/ctaocrypt/aes.h>
00044 #include <cyassl/ctaocrypt/des3.h>
00045 #include <cyassl/ctaocrypt/arc4.h>
00046 
00047 
00048 #ifdef __cplusplus
00049     extern "C" {
00050 #endif
00051 
00052 typedef char CYASSL_EVP_MD;
00053 typedef char CYASSL_EVP_CIPHER;
00054 
00055 CYASSL_API const CYASSL_EVP_MD* CyaSSL_EVP_md5(void);
00056 CYASSL_API const CYASSL_EVP_MD* CyaSSL_EVP_sha1(void);
00057 CYASSL_API const CYASSL_EVP_MD* CyaSSL_EVP_sha256(void);
00058 CYASSL_API const CYASSL_EVP_MD* CyaSSL_EVP_sha384(void);
00059 CYASSL_API const CYASSL_EVP_MD* CyaSSL_EVP_sha512(void);
00060 CYASSL_API const CYASSL_EVP_MD* CyaSSL_EVP_ripemd160(void);
00061 
00062 CYASSL_API const CYASSL_EVP_CIPHER* CyaSSL_EVP_aes_128_cbc(void);
00063 CYASSL_API const CYASSL_EVP_CIPHER* CyaSSL_EVP_aes_192_cbc(void);
00064 CYASSL_API const CYASSL_EVP_CIPHER* CyaSSL_EVP_aes_256_cbc(void);
00065 CYASSL_API const CYASSL_EVP_CIPHER* CyaSSL_EVP_aes_128_ctr(void);
00066 CYASSL_API const CYASSL_EVP_CIPHER* CyaSSL_EVP_aes_192_ctr(void);
00067 CYASSL_API const CYASSL_EVP_CIPHER* CyaSSL_EVP_aes_256_ctr(void);
00068 CYASSL_API const CYASSL_EVP_CIPHER* CyaSSL_EVP_des_cbc(void);
00069 CYASSL_API const CYASSL_EVP_CIPHER* CyaSSL_EVP_des_ede3_cbc(void);
00070 CYASSL_API const CYASSL_EVP_CIPHER* CyaSSL_EVP_rc4(void);
00071 CYASSL_API const CYASSL_EVP_CIPHER* CyaSSL_EVP_enc_null(void);
00072 
00073 
00074 typedef union {
00075     CYASSL_MD5_CTX    md5;
00076     CYASSL_SHA_CTX    sha;
00077     CYASSL_SHA256_CTX sha256;
00078     #ifdef CYASSL_SHA384
00079         CYASSL_SHA384_CTX sha384;
00080     #endif
00081     #ifdef CYASSL_SHA512
00082         CYASSL_SHA512_CTX sha512;
00083     #endif
00084     #ifdef CYASSL_RIPEMD
00085         CYASSL_RIPEMD_CTX ripemd;
00086     #endif
00087 } CYASSL_Hasher;
00088 
00089 
00090 typedef struct CYASSL_EVP_MD_CTX {
00091     unsigned char macType;
00092     CYASSL_Hasher hash;
00093 } CYASSL_EVP_MD_CTX;
00094 
00095 
00096 typedef union {
00097     Aes  aes;
00098     Des  des;
00099     Des3 des3;
00100     Arc4 arc4;
00101 } CYASSL_Cipher;
00102 
00103 
00104 enum {
00105     AES_128_CBC_TYPE  = 1,
00106     AES_192_CBC_TYPE  = 2,
00107     AES_256_CBC_TYPE  = 3,
00108     AES_128_CTR_TYPE  = 4,
00109     AES_192_CTR_TYPE  = 5,
00110     AES_256_CTR_TYPE  = 6,
00111     DES_CBC_TYPE      = 7,
00112     DES_EDE3_CBC_TYPE = 8,
00113     ARC4_TYPE         = 9,
00114     NULL_CIPHER_TYPE  = 10,
00115     EVP_PKEY_RSA      = 11,
00116     EVP_PKEY_DSA      = 12,
00117     NID_sha1          = 64,
00118     NID_md5           =  4
00119 };
00120 
00121 
00122 typedef struct CYASSL_EVP_CIPHER_CTX {
00123     int            keyLen;         /* user may set for variable */
00124     unsigned char  enc;            /* if encrypt side, then true */
00125     unsigned char  cipherType;
00126     unsigned char  iv[AES_BLOCK_SIZE];    /* working iv pointer into cipher */
00127     CYASSL_Cipher  cipher;
00128 } CYASSL_EVP_CIPHER_CTX;
00129 
00130 
00131 CYASSL_API int  CyaSSL_EVP_MD_size(const CYASSL_EVP_MD* md);
00132 CYASSL_API void CyaSSL_EVP_MD_CTX_init(CYASSL_EVP_MD_CTX* ctx);
00133 CYASSL_API int  CyaSSL_EVP_MD_CTX_cleanup(CYASSL_EVP_MD_CTX* ctx);
00134 
00135 CYASSL_API int CyaSSL_EVP_DigestInit(CYASSL_EVP_MD_CTX* ctx,
00136                                      const CYASSL_EVP_MD* type);
00137 CYASSL_API int CyaSSL_EVP_DigestUpdate(CYASSL_EVP_MD_CTX* ctx, const void* data,
00138                                        unsigned long sz);
00139 CYASSL_API int CyaSSL_EVP_DigestFinal(CYASSL_EVP_MD_CTX* ctx, unsigned char* md,
00140                                       unsigned int* s);
00141 CYASSL_API int CyaSSL_EVP_DigestFinal_ex(CYASSL_EVP_MD_CTX* ctx,
00142                                             unsigned char* md, unsigned int* s);
00143 CYASSL_API int CyaSSL_EVP_BytesToKey(const CYASSL_EVP_CIPHER*,
00144                               const CYASSL_EVP_MD*, const unsigned char*,
00145                               const unsigned char*, int, int, unsigned char*,
00146                               unsigned char*);
00147 
00148 CYASSL_API void CyaSSL_EVP_CIPHER_CTX_init(CYASSL_EVP_CIPHER_CTX* ctx);
00149 CYASSL_API int  CyaSSL_EVP_CIPHER_CTX_cleanup(CYASSL_EVP_CIPHER_CTX* ctx);
00150 
00151 CYASSL_API int  CyaSSL_EVP_CIPHER_CTX_iv_length(const CYASSL_EVP_CIPHER_CTX*);
00152 
00153 
00154 CYASSL_API int  CyaSSL_EVP_CipherInit(CYASSL_EVP_CIPHER_CTX* ctx,
00155                                     const CYASSL_EVP_CIPHER* type,
00156                                     unsigned char* key, unsigned char* iv,
00157                                     int enc);
00158 CYASSL_API int  CyaSSL_EVP_CIPHER_CTX_key_length(CYASSL_EVP_CIPHER_CTX* ctx);
00159 CYASSL_API int  CyaSSL_EVP_CIPHER_CTX_set_key_length(CYASSL_EVP_CIPHER_CTX* ctx,
00160                                                      int keylen);
00161 CYASSL_API int  CyaSSL_EVP_Cipher(CYASSL_EVP_CIPHER_CTX* ctx,
00162                           unsigned char* dst, unsigned char* src,
00163                           unsigned int len);
00164 
00165 CYASSL_API const CYASSL_EVP_MD* CyaSSL_EVP_get_digestbynid(int);
00166 
00167 CYASSL_API CYASSL_RSA* CyaSSL_EVP_PKEY_get1_RSA(CYASSL_EVP_PKEY*);
00168 CYASSL_API CYASSL_DSA* CyaSSL_EVP_PKEY_get1_DSA(CYASSL_EVP_PKEY*);
00169 
00170 /* these next ones don't need real OpenSSL type, for OpenSSH compat only */
00171 CYASSL_API void* CyaSSL_EVP_X_STATE(const CYASSL_EVP_CIPHER_CTX* ctx);
00172 CYASSL_API int   CyaSSL_EVP_X_STATE_LEN(const CYASSL_EVP_CIPHER_CTX* ctx);
00173 
00174 CYASSL_API void  CyaSSL_3des_iv(CYASSL_EVP_CIPHER_CTX* ctx, int doset,
00175                                 unsigned char* iv, int len);
00176 CYASSL_API void  CyaSSL_aes_ctr_iv(CYASSL_EVP_CIPHER_CTX* ctx, int doset,
00177                                 unsigned char* iv, int len);
00178 
00179 CYASSL_API int  CyaSSL_StoreExternalIV(CYASSL_EVP_CIPHER_CTX* ctx);
00180 CYASSL_API int  CyaSSL_SetInternalIV(CYASSL_EVP_CIPHER_CTX* ctx);
00181 
00182 
00183 /* end OpenSSH compat */
00184 
00185 typedef CYASSL_EVP_MD         EVP_MD;
00186 typedef CYASSL_EVP_CIPHER     EVP_CIPHER;
00187 typedef CYASSL_EVP_MD_CTX     EVP_MD_CTX;
00188 typedef CYASSL_EVP_CIPHER_CTX EVP_CIPHER_CTX;
00189 
00190 #define EVP_md5       CyaSSL_EVP_md5
00191 #define EVP_sha1      CyaSSL_EVP_sha1
00192 #define EVP_sha256    CyaSSL_EVP_sha256
00193 #define EVP_sha384    CyaSSL_EVP_sha384
00194 #define EVP_sha512    CyaSSL_EVP_sha512
00195 #define EVP_ripemd160 CyaSSL_EVP_ripemd160
00196 
00197 #define EVP_aes_128_cbc  CyaSSL_EVP_aes_128_cbc
00198 #define EVP_aes_192_cbc  CyaSSL_EVP_aes_192_cbc
00199 #define EVP_aes_256_cbc  CyaSSL_EVP_aes_256_cbc
00200 #define EVP_aes_128_ctr  CyaSSL_EVP_aes_128_ctr
00201 #define EVP_aes_192_ctr  CyaSSL_EVP_aes_192_ctr
00202 #define EVP_aes_256_ctr  CyaSSL_EVP_aes_256_ctr
00203 #define EVP_des_cbc      CyaSSL_EVP_des_cbc
00204 #define EVP_des_ede3_cbc CyaSSL_EVP_des_ede3_cbc
00205 #define EVP_rc4          CyaSSL_EVP_rc4
00206 #define EVP_enc_null     CyaSSL_EVP_enc_null
00207 
00208 #define EVP_MD_size        CyaSSL_EVP_MD_size
00209 #define EVP_MD_CTX_init    CyaSSL_EVP_MD_CTX_init
00210 #define EVP_MD_CTX_cleanup CyaSSL_EVP_MD_CTX_cleanup
00211 #define EVP_DigestInit     CyaSSL_EVP_DigestInit
00212 #define EVP_DigestUpdate   CyaSSL_EVP_DigestUpdate
00213 #define EVP_DigestFinal    CyaSSL_EVP_DigestFinal
00214 #define EVP_DigestFinal_ex CyaSSL_EVP_DigestFinal_ex
00215 #define EVP_BytesToKey     CyaSSL_EVP_BytesToKey
00216 
00217 #define EVP_CIPHER_CTX_init           CyaSSL_EVP_CIPHER_CTX_init
00218 #define EVP_CIPHER_CTX_cleanup        CyaSSL_EVP_CIPHER_CTX_cleanup
00219 #define EVP_CIPHER_CTX_iv_length      CyaSSL_EVP_CIPHER_CTX_iv_length
00220 #define EVP_CIPHER_CTX_key_length     CyaSSL_EVP_CIPHER_CTX_key_length
00221 #define EVP_CIPHER_CTX_set_key_length CyaSSL_EVP_CIPHER_CTX_set_key_length
00222 #define EVP_CipherInit                CyaSSL_EVP_CipherInit
00223 #define EVP_Cipher                    CyaSSL_EVP_Cipher
00224 
00225 #define EVP_get_digestbynid           CyaSSL_EVP_get_digestbynid
00226 
00227 #define EVP_PKEY_get1_RSA   CyaSSL_EVP_PKEY_get1_RSA
00228 #define EVP_PKEY_get1_DSA   CyaSSL_EVP_PKEY_get1_DSA
00229 
00230 #ifndef EVP_MAX_MD_SIZE
00231     #define EVP_MAX_MD_SIZE   64     /* sha512 */
00232 #endif
00233 
00234 #ifdef __cplusplus
00235     } /* extern "C" */
00236 #endif
00237 
00238 
00239 #endif /* CYASSL_EVP_H_ */