Renesas / SecureDweet
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers evp.h Source File

evp.h

00001 /* evp.h
00002  *
00003  * Copyright (C) 2006-2016 wolfSSL Inc.
00004  *
00005  * This file is part of wolfSSL.
00006  *
00007  * wolfSSL 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  * wolfSSL 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
00020  */
00021 
00022 
00023 
00024 /*  evp.h defines mini evp openssl compatibility layer 
00025  *
00026  */
00027 
00028 
00029 #ifndef WOLFSSL_EVP_H_
00030 #define WOLFSSL_EVP_H_
00031 
00032 #include <wolfssl/wolfcrypt/settings.h>
00033 
00034 #ifdef WOLFSSL_PREFIX
00035 #include "prefix_evp.h"
00036 #endif
00037 
00038 #ifndef NO_MD5
00039     #include <wolfssl/openssl/md5.h>
00040 #endif
00041 #include <wolfssl/openssl/sha.h>
00042 #include <wolfssl/openssl/ripemd.h>
00043 #include <wolfssl/openssl/rsa.h>
00044 #include <wolfssl/openssl/dsa.h>
00045 #include <wolfssl/openssl/ec.h>
00046 
00047 #include <wolfssl/wolfcrypt/aes.h>
00048 #include <wolfssl/wolfcrypt/des3.h>
00049 #include <wolfssl/wolfcrypt/arc4.h>
00050 
00051 
00052 #ifdef __cplusplus
00053     extern "C" {
00054 #endif
00055 
00056 typedef char WOLFSSL_EVP_MD;
00057 typedef char WOLFSSL_EVP_CIPHER;
00058 
00059 #ifndef NO_MD5
00060     WOLFSSL_API const WOLFSSL_EVP_MD* wolfSSL_EVP_md5(void);
00061 #endif
00062 WOLFSSL_API const WOLFSSL_EVP_MD* wolfSSL_EVP_sha1(void);
00063 WOLFSSL_API const WOLFSSL_EVP_MD* wolfSSL_EVP_sha256(void);
00064 WOLFSSL_API const WOLFSSL_EVP_MD* wolfSSL_EVP_sha384(void);
00065 WOLFSSL_API const WOLFSSL_EVP_MD* wolfSSL_EVP_sha512(void);
00066 WOLFSSL_API const WOLFSSL_EVP_MD* wolfSSL_EVP_ripemd160(void);
00067 
00068 WOLFSSL_API const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_128_cbc(void);
00069 WOLFSSL_API const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_192_cbc(void);
00070 WOLFSSL_API const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_256_cbc(void);
00071 WOLFSSL_API const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_128_ctr(void);
00072 WOLFSSL_API const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_192_ctr(void);
00073 WOLFSSL_API const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_256_ctr(void);
00074 WOLFSSL_API const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_des_cbc(void);
00075 WOLFSSL_API const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_des_ede3_cbc(void);
00076 WOLFSSL_API const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_rc4(void);
00077 WOLFSSL_API const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_idea_cbc(void);
00078 WOLFSSL_API const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_enc_null(void);
00079 
00080 
00081 typedef union {
00082     #ifndef NO_MD5
00083         WOLFSSL_MD5_CTX    md5;
00084     #endif
00085     WOLFSSL_SHA_CTX    sha;
00086     WOLFSSL_SHA256_CTX sha256;
00087     #ifdef WOLFSSL_SHA384
00088         WOLFSSL_SHA384_CTX sha384;
00089     #endif
00090     #ifdef WOLFSSL_SHA512
00091         WOLFSSL_SHA512_CTX sha512;
00092     #endif
00093     #ifdef WOLFSSL_RIPEMD
00094         WOLFSSL_RIPEMD_CTX ripemd;
00095     #endif
00096 } WOLFSSL_Hasher;
00097 
00098 
00099 typedef struct WOLFSSL_EVP_MD_CTX {
00100     unsigned char macType;
00101     WOLFSSL_Hasher hash;
00102 } WOLFSSL_EVP_MD_CTX;
00103 
00104 
00105 typedef union {
00106 #ifndef NO_AES
00107     Aes  aes;
00108 #endif
00109 #ifndef NO_DES3
00110     Des  des;
00111     Des3 des3;
00112 #endif
00113     Arc4 arc4;
00114 #ifdef HAVE_IDEA
00115     Idea idea;
00116 #endif
00117 } WOLFSSL_Cipher;
00118 
00119 
00120 enum {
00121     AES_128_CBC_TYPE  = 1,
00122     AES_192_CBC_TYPE  = 2,
00123     AES_256_CBC_TYPE  = 3,
00124     AES_128_CTR_TYPE  = 4,
00125     AES_192_CTR_TYPE  = 5,
00126     AES_256_CTR_TYPE  = 6,
00127     DES_CBC_TYPE      = 7,
00128     DES_EDE3_CBC_TYPE = 8,
00129     ARC4_TYPE         = 9,
00130     NULL_CIPHER_TYPE  = 10,
00131     EVP_PKEY_RSA      = 11,
00132     EVP_PKEY_DSA      = 12,
00133     EVP_PKEY_EC       = 13,
00134     IDEA_CBC_TYPE     = 14,
00135     NID_sha1          = 64,
00136     NID_md2           = 3,
00137     NID_md5           =  4
00138 };
00139 
00140 
00141 typedef struct WOLFSSL_EVP_CIPHER_CTX {
00142     int            keyLen;         /* user may set for variable */
00143     unsigned char  enc;            /* if encrypt side, then true */
00144     unsigned char  cipherType;
00145 #ifndef NO_AES
00146     unsigned char  iv[AES_BLOCK_SIZE];    /* working iv pointer into cipher */
00147 #elif !defined(NO_DES3)
00148     unsigned char  iv[DES_BLOCK_SIZE];    /* working iv pointer into cipher */
00149 #endif
00150     WOLFSSL_Cipher  cipher;
00151 } WOLFSSL_EVP_CIPHER_CTX;
00152 
00153 
00154 WOLFSSL_API int  wolfSSL_EVP_MD_size(const WOLFSSL_EVP_MD* md);
00155 WOLFSSL_API void wolfSSL_EVP_MD_CTX_init(WOLFSSL_EVP_MD_CTX* ctx);
00156 WOLFSSL_API int  wolfSSL_EVP_MD_CTX_cleanup(WOLFSSL_EVP_MD_CTX* ctx);
00157 
00158 WOLFSSL_API int wolfSSL_EVP_DigestInit(WOLFSSL_EVP_MD_CTX* ctx,
00159                                      const WOLFSSL_EVP_MD* type);
00160 WOLFSSL_API int wolfSSL_EVP_DigestUpdate(WOLFSSL_EVP_MD_CTX* ctx, const void* data,
00161                                        unsigned long sz);
00162 WOLFSSL_API int wolfSSL_EVP_DigestFinal(WOLFSSL_EVP_MD_CTX* ctx, unsigned char* md,
00163                                       unsigned int* s);
00164 WOLFSSL_API int wolfSSL_EVP_DigestFinal_ex(WOLFSSL_EVP_MD_CTX* ctx,
00165                                             unsigned char* md, unsigned int* s);
00166 #ifndef NO_MD5
00167 WOLFSSL_API int wolfSSL_EVP_BytesToKey(const WOLFSSL_EVP_CIPHER*,
00168                               const WOLFSSL_EVP_MD*, const unsigned char*,
00169                               const unsigned char*, int, int, unsigned char*,
00170                               unsigned char*);
00171 #endif
00172 
00173 WOLFSSL_API void wolfSSL_EVP_CIPHER_CTX_init(WOLFSSL_EVP_CIPHER_CTX* ctx);
00174 WOLFSSL_API int  wolfSSL_EVP_CIPHER_CTX_cleanup(WOLFSSL_EVP_CIPHER_CTX* ctx);
00175 
00176 WOLFSSL_API int  wolfSSL_EVP_CIPHER_CTX_iv_length(const WOLFSSL_EVP_CIPHER_CTX*);
00177 
00178 
00179 WOLFSSL_API int  wolfSSL_EVP_CipherInit(WOLFSSL_EVP_CIPHER_CTX* ctx,
00180                                     const WOLFSSL_EVP_CIPHER* type,
00181                                     unsigned char* key, unsigned char* iv,
00182                                     int enc);
00183 WOLFSSL_API int  wolfSSL_EVP_CIPHER_CTX_key_length(WOLFSSL_EVP_CIPHER_CTX* ctx);
00184 WOLFSSL_API int  wolfSSL_EVP_CIPHER_CTX_set_key_length(WOLFSSL_EVP_CIPHER_CTX* ctx,
00185                                                      int keylen);
00186 WOLFSSL_API int  wolfSSL_EVP_Cipher(WOLFSSL_EVP_CIPHER_CTX* ctx,
00187                           unsigned char* dst, unsigned char* src,
00188                           unsigned int len);
00189 
00190 WOLFSSL_API const WOLFSSL_EVP_MD* wolfSSL_EVP_get_digestbynid(int);
00191 
00192 WOLFSSL_API WOLFSSL_RSA* wolfSSL_EVP_PKEY_get1_RSA(WOLFSSL_EVP_PKEY*);
00193 WOLFSSL_API WOLFSSL_DSA* wolfSSL_EVP_PKEY_get1_DSA(WOLFSSL_EVP_PKEY*);
00194 WOLFSSL_API WOLFSSL_EC_KEY *wolfSSL_EVP_PKEY_get1_EC_KEY(WOLFSSL_EVP_PKEY *key);
00195 
00196 /* these next ones don't need real OpenSSL type, for OpenSSH compat only */
00197 WOLFSSL_API void* wolfSSL_EVP_X_STATE(const WOLFSSL_EVP_CIPHER_CTX* ctx);
00198 WOLFSSL_API int   wolfSSL_EVP_X_STATE_LEN(const WOLFSSL_EVP_CIPHER_CTX* ctx);
00199 
00200 WOLFSSL_API void  wolfSSL_3des_iv(WOLFSSL_EVP_CIPHER_CTX* ctx, int doset,
00201                                 unsigned char* iv, int len);
00202 WOLFSSL_API void  wolfSSL_aes_ctr_iv(WOLFSSL_EVP_CIPHER_CTX* ctx, int doset,
00203                                 unsigned char* iv, int len);
00204 
00205 WOLFSSL_API int  wolfSSL_StoreExternalIV(WOLFSSL_EVP_CIPHER_CTX* ctx);
00206 WOLFSSL_API int  wolfSSL_SetInternalIV(WOLFSSL_EVP_CIPHER_CTX* ctx);
00207 
00208 
00209 /* end OpenSSH compat */
00210 
00211 typedef WOLFSSL_EVP_MD         EVP_MD;
00212 typedef WOLFSSL_EVP_CIPHER     EVP_CIPHER;
00213 typedef WOLFSSL_EVP_MD_CTX     EVP_MD_CTX;
00214 typedef WOLFSSL_EVP_CIPHER_CTX EVP_CIPHER_CTX;
00215 
00216 #ifndef NO_MD5
00217     #define EVP_md5       wolfSSL_EVP_md5
00218 #endif
00219 #define EVP_sha1      wolfSSL_EVP_sha1
00220 #define EVP_sha256    wolfSSL_EVP_sha256
00221 #define EVP_sha384    wolfSSL_EVP_sha384
00222 #define EVP_sha512    wolfSSL_EVP_sha512
00223 #define EVP_ripemd160 wolfSSL_EVP_ripemd160
00224 
00225 #define EVP_aes_128_cbc  wolfSSL_EVP_aes_128_cbc
00226 #define EVP_aes_192_cbc  wolfSSL_EVP_aes_192_cbc
00227 #define EVP_aes_256_cbc  wolfSSL_EVP_aes_256_cbc
00228 #define EVP_aes_128_ctr  wolfSSL_EVP_aes_128_ctr
00229 #define EVP_aes_192_ctr  wolfSSL_EVP_aes_192_ctr
00230 #define EVP_aes_256_ctr  wolfSSL_EVP_aes_256_ctr
00231 #define EVP_des_cbc      wolfSSL_EVP_des_cbc
00232 #define EVP_des_ede3_cbc wolfSSL_EVP_des_ede3_cbc
00233 #define EVP_rc4          wolfSSL_EVP_rc4
00234 #define EVP_idea_cbc     wolfSSL_EVP_idea_cbc
00235 #define EVP_enc_null     wolfSSL_EVP_enc_null
00236 
00237 #define EVP_MD_size        wolfSSL_EVP_MD_size
00238 #define EVP_MD_CTX_init    wolfSSL_EVP_MD_CTX_init
00239 #define EVP_MD_CTX_cleanup wolfSSL_EVP_MD_CTX_cleanup
00240 #define EVP_DigestInit     wolfSSL_EVP_DigestInit
00241 #define EVP_DigestUpdate   wolfSSL_EVP_DigestUpdate
00242 #define EVP_DigestFinal    wolfSSL_EVP_DigestFinal
00243 #define EVP_DigestFinal_ex wolfSSL_EVP_DigestFinal_ex
00244 #define EVP_BytesToKey     wolfSSL_EVP_BytesToKey
00245 
00246 #define EVP_CIPHER_CTX_init           wolfSSL_EVP_CIPHER_CTX_init
00247 #define EVP_CIPHER_CTX_cleanup        wolfSSL_EVP_CIPHER_CTX_cleanup
00248 #define EVP_CIPHER_CTX_iv_length      wolfSSL_EVP_CIPHER_CTX_iv_length
00249 #define EVP_CIPHER_CTX_key_length     wolfSSL_EVP_CIPHER_CTX_key_length
00250 #define EVP_CIPHER_CTX_set_key_length wolfSSL_EVP_CIPHER_CTX_set_key_length
00251 #define EVP_CipherInit                wolfSSL_EVP_CipherInit
00252 #define EVP_Cipher                    wolfSSL_EVP_Cipher
00253 
00254 #define EVP_get_digestbynid           wolfSSL_EVP_get_digestbynid
00255 
00256 #define EVP_PKEY_get1_RSA   wolfSSL_EVP_PKEY_get1_RSA
00257 #define EVP_PKEY_get1_DSA   wolfSSL_EVP_PKEY_get1_DSA
00258 #define EVP_PKEY_get1_EC_KEY wolfSSL_EVP_PKEY_get1_EC_KEY
00259 
00260 
00261 #ifndef EVP_MAX_MD_SIZE
00262     #define EVP_MAX_MD_SIZE   64     /* sha512 */
00263 #endif
00264 
00265 #ifdef __cplusplus
00266     } /* extern "C" */
00267 #endif
00268 
00269 
00270 #endif /* WOLFSSL_EVP_H_ */
00271