Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
openssl.h
00001 /** 00002 * \file openssl.h 00003 * 00004 * \brief OpenSSL wrapper (definitions, inline functions). 00005 * 00006 * Copyright (C) 2006-2010, Brainspark B.V. 00007 * 00008 * This file is part of PolarSSL (http://www.polarssl.org) 00009 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> 00010 * 00011 * All rights reserved. 00012 * 00013 * This program is free software; you can redistribute it and/or modify 00014 * it under the terms of the GNU General Public License as published by 00015 * the Free Software Foundation; either version 2 of the License, or 00016 * (at your option) any later version. 00017 * 00018 * This program is distributed in the hope that it will be useful, 00019 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00020 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00021 * GNU General Public License for more details. 00022 * 00023 * You should have received a copy of the GNU General Public License along 00024 * with this program; if not, write to the Free Software Foundation, Inc., 00025 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00026 */ 00027 /* 00028 * OpenSSL wrapper contributed by David Barett 00029 */ 00030 #ifndef POLARSSL_OPENSSL_H 00031 #define POLARSSL_OPENSSL_H 00032 00033 #include "aes.h" 00034 #include "md5.h" 00035 #include "rsa.h" 00036 #include "sha1.h" 00037 00038 #define AES_SIZE 16 00039 #define AES_BLOCK_SIZE 16 00040 #define AES_KEY aes_context 00041 #define MD5_CTX md5_context 00042 #define SHA_CTX sha1_context 00043 00044 #define SHA1_Init( CTX ) \ 00045 sha1_starts( (CTX) ) 00046 #define SHA1_Update( CTX, BUF, LEN ) \ 00047 sha1_update( (CTX), (unsigned char *)(BUF), (LEN) ) 00048 #define SHA1_Final( OUT, CTX ) \ 00049 sha1_finish( (CTX), (OUT) ) 00050 00051 #define MD5_Init( CTX ) \ 00052 md5_starts( (CTX) ) 00053 #define MD5_Update( CTX, BUF, LEN ) \ 00054 md5_update( (CTX), (unsigned char *)(BUF), (LEN) ) 00055 #define MD5_Final( OUT, CTX ) \ 00056 md5_finish( (CTX), (OUT) ) 00057 00058 #define AES_set_encrypt_key( KEY, KEYSIZE, CTX ) \ 00059 aes_setkey_enc( (CTX), (KEY), (KEYSIZE) ) 00060 #define AES_set_decrypt_key( KEY, KEYSIZE, CTX ) \ 00061 aes_setkey_dec( (CTX), (KEY), (KEYSIZE) ) 00062 #define AES_cbc_encrypt( INPUT, OUTPUT, LEN, CTX, IV, MODE ) \ 00063 aes_crypt_cbc( (CTX), (MODE), (LEN), (IV), (INPUT), (OUTPUT) ) 00064 00065 #ifdef __cplusplus 00066 extern "C" { 00067 #endif 00068 00069 /* 00070 * RSA stuff follows. TODO: needs cleanup 00071 */ 00072 inline int __RSA_Passthrough( void *output, void *input, int size ) 00073 { 00074 memcpy( output, input, size ); 00075 return size; 00076 } 00077 00078 inline rsa_context* d2i_RSA_PUBKEY( void *ignore, unsigned char **bufptr, 00079 int len ) 00080 { 00081 unsigned char *buffer = *(unsigned char **) bufptr; 00082 rsa_context *rsa; 00083 00084 /* 00085 * Not a general-purpose parser: only parses public key from *exactly* 00086 * openssl genrsa -out privkey.pem 512 (or 1024) 00087 * openssl rsa -in privkey.pem -out privatekey.der -outform der 00088 * openssl rsa -in privkey.pem -out pubkey.der -outform der -pubout 00089 * 00090 * TODO: make a general-purpose parse 00091 */ 00092 if( ignore != 0 || ( len != 94 && len != 162 ) ) 00093 return( 0 ); 00094 00095 rsa = (rsa_context *) malloc( sizeof( rsa_rsa ) ); 00096 if( rsa == NULL ) 00097 return( 0 ); 00098 00099 memset( rsa, 0, sizeof( rsa_context ) ); 00100 00101 if( ( len == 94 && 00102 mpi_read_binary( &rsa->N , &buffer[ 25], 64 ) == 0 && 00103 mpi_read_binary( &rsa->E , &buffer[ 91], 3 ) == 0 ) || 00104 ( len == 162 && 00105 mpi_read_binary( &rsa->N , &buffer[ 29], 128 ) == 0 ) && 00106 mpi_read_binary( &rsa->E , &buffer[159], 3 ) == 0 ) 00107 { 00108 /* 00109 * key read successfully 00110 */ 00111 rsa->len = ( mpi_msb( &rsa->N ) + 7 ) >> 3; 00112 return( rsa ); 00113 } 00114 else 00115 { 00116 memset( rsa, 0, sizeof( rsa_context ) ); 00117 free( rsa ); 00118 return( 0 ); 00119 } 00120 } 00121 00122 #define RSA rsa_context 00123 #define RSA_PKCS1_PADDING 1 /* ignored; always encrypt with this */ 00124 #define RSA_size( CTX ) (CTX)->len 00125 #define RSA_free( CTX ) rsa_free( CTX ) 00126 #define ERR_get_error( ) "ERR_get_error() not supported" 00127 #define RSA_blinding_off( IGNORE ) 00128 00129 #define d2i_RSAPrivateKey( a, b, c ) new rsa_context /* TODO: C++ bleh */ 00130 00131 inline int RSA_public_decrypt ( int size, unsigned char* input, unsigned char* output, RSA* key, int ignore ) { int outsize=size; if( !rsa_pkcs1_decrypt( key, RSA_PUBLIC, &outsize, input, output ) ) return outsize; else return -1; } 00132 inline int RSA_private_decrypt( int size, unsigned char* input, unsigned char* output, RSA* key, int ignore ) { int outsize=size; if( !rsa_pkcs1_decrypt( key, RSA_PRIVATE, &outsize, input, output ) ) return outsize; else return -1; } 00133 inline int RSA_public_encrypt ( int size, unsigned char* input, unsigned char* output, RSA* key, int ignore ) { if( !rsa_pkcs1_encrypt( key, RSA_PUBLIC, size, input, output ) ) return RSA_size(key); else return -1; } 00134 inline int RSA_private_encrypt( int size, unsigned char* input, unsigned char* output, RSA* key, int ignore ) { if( !rsa_pkcs1_encrypt( key, RSA_PRIVATE, size, input, output ) ) return RSA_size(key); else return -1; } 00135 00136 #ifdef __cplusplus 00137 } 00138 #endif 00139 00140 #endif /* openssl.h */ 00141 00142
Generated on Tue Jul 12 2022 19:40:18 by
1.7.2