mbed TLS v1.3.14
openssl.h
Go to the documentation of this file.
1 
27 /*
28  * OpenSSL wrapper contributed by David Barett
29  */
30 
31 #if ! defined(POLARSSL_DEPRECATED_REMOVED)
32 
33 #if defined(POLARSSL_DEPRECATED_WARNING)
34 #warning "Including openssl.h is deprecated"
35 #endif
36 
37 #ifndef POLARSSL_OPENSSL_H
38 #define POLARSSL_OPENSSL_H
39 
40 #include "aes.h"
41 #include "md5.h"
42 #include "rsa.h"
43 #include "sha1.h"
44 
45 #define AES_SIZE 16
46 #define AES_BLOCK_SIZE 16
47 #define AES_KEY aes_context
48 #define MD5_CTX md5_context
49 #define SHA_CTX sha1_context
50 
51 #define SHA1_Init( CTX ) \
52  sha1_starts( (CTX) )
53 #define SHA1_Update( CTX, BUF, LEN ) \
54  sha1_update( (CTX), (unsigned char *)(BUF), (LEN) )
55 #define SHA1_Final( OUT, CTX ) \
56  sha1_finish( (CTX), (OUT) )
57 
58 #define MD5_Init( CTX ) \
59  md5_starts( (CTX) )
60 #define MD5_Update( CTX, BUF, LEN ) \
61  md5_update( (CTX), (unsigned char *)(BUF), (LEN) )
62 #define MD5_Final( OUT, CTX ) \
63  md5_finish( (CTX), (OUT) )
64 
65 #define AES_set_encrypt_key( KEY, KEYSIZE, CTX ) \
66  aes_setkey_enc( (CTX), (KEY), (KEYSIZE) )
67 #define AES_set_decrypt_key( KEY, KEYSIZE, CTX ) \
68  aes_setkey_dec( (CTX), (KEY), (KEYSIZE) )
69 #define AES_cbc_encrypt( INPUT, OUTPUT, LEN, CTX, IV, MODE ) \
70  aes_crypt_cbc( (CTX), (MODE), (LEN), (IV), (INPUT), (OUTPUT) )
71 
72 #ifdef __cplusplus
73 extern "C" {
74 #endif
75 
76 /*
77  * RSA stuff follows. TODO: needs cleanup
78  */
79 inline int __RSA_Passthrough( void *output, void *input, int size )
80 {
81  memcpy( output, input, size );
82  return size;
83 }
84 
85 inline rsa_context* d2i_RSA_PUBKEY( void *ignore, unsigned char **bufptr,
86  int len )
87 {
88  unsigned char *buffer = *(unsigned char **) bufptr;
89  rsa_context *rsa;
90 
91  /*
92  * Not a general-purpose parser: only parses public key from *exactly*
93  * openssl genrsa -out privkey.pem 512 (or 1024)
94  * openssl rsa -in privkey.pem -out privatekey.der -outform der
95  * openssl rsa -in privkey.pem -out pubkey.der -outform der -pubout
96  *
97  * TODO: make a general-purpose parse
98  */
99  if( ignore != 0 || ( len != 94 && len != 162 ) )
100  return( 0 );
101 
102  rsa = (rsa_context *) malloc( sizeof( rsa_rsa ) );
103  if( rsa == NULL )
104  return( 0 );
105 
106  memset( rsa, 0, sizeof( rsa_context ) );
107 
108  if( ( len == 94 &&
109  mpi_read_binary( &rsa->N, &buffer[ 25], 64 ) == 0 &&
110  mpi_read_binary( &rsa->E, &buffer[ 91], 3 ) == 0 ) ||
111  ( len == 162 &&
112  mpi_read_binary( &rsa->N, &buffer[ 29], 128 ) == 0 ) &&
113  mpi_read_binary( &rsa->E, &buffer[159], 3 ) == 0 )
114  {
115  /*
116  * key read successfully
117  */
118  rsa->len = ( mpi_msb( &rsa->N ) + 7 ) >> 3;
119  return( rsa );
120  }
121  else
122  {
123  memset( rsa, 0, sizeof( rsa_context ) );
124  free( rsa );
125  return( 0 );
126  }
127 }
128 
129 #define RSA rsa_context
130 #define RSA_PKCS1_PADDING 1 /* ignored; always encrypt with this */
131 #define RSA_size( CTX ) (CTX)->len
132 #define RSA_free( CTX ) rsa_free( CTX )
133 #define ERR_get_error( ) "ERR_get_error() not supported"
134 #define RSA_blinding_off( IGNORE )
135 
136 #define d2i_RSAPrivateKey( a, b, c ) new rsa_context /* TODO: C++ bleh */
137 
138 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; }
139 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; }
140 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; }
141 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; }
142 
143 #ifdef __cplusplus
144 }
145 #endif
146 
147 #endif /* openssl.h */
148 #endif /* POLARSSL_DEPRECATED_REMOVED */
rsa_context * d2i_RSA_PUBKEY(void *ignore, unsigned char **bufptr, int len)
Definition: openssl.h:85
#define RSA_size(CTX)
Definition: openssl.h:131
int RSA_public_encrypt(int size, unsigned char *input, unsigned char *output, RSA *key, int ignore)
Definition: openssl.h:140
#define RSA_PUBLIC
Definition: rsa.h:56
int rsa_pkcs1_decrypt(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, size_t *olen, const unsigned char *input, unsigned char *output, size_t output_max_len)
Generic wrapper to perform a PKCS#1 decryption using the mode from the context.
int RSA_public_decrypt(int size, unsigned char *input, unsigned char *output, RSA *key, int ignore)
Definition: openssl.h:138
size_t len
Definition: rsa.h:83
int __RSA_Passthrough(void *output, void *input, int size)
Definition: openssl.h:79
RSA context structure.
Definition: rsa.h:80
int rsa_pkcs1_encrypt(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, size_t ilen, const unsigned char *input, unsigned char *output)
Generic wrapper to perform a PKCS#1 encryption using the mode from the context.
#define RSA_PRIVATE
Definition: rsa.h:57
mpi N
Definition: rsa.h:85
#define RSA
Definition: openssl.h:129
AES block cipher.
int RSA_private_decrypt(int size, unsigned char *input, unsigned char *output, RSA *key, int ignore)
Definition: openssl.h:139
mpi E
Definition: rsa.h:86
size_t mpi_msb(const mpi *X)
Return the number of bits up to and including the most significant '1' bit'.
int mpi_read_binary(mpi *X, const unsigned char *buf, size_t buflen)
Import X from unsigned binary data, big endian.
The RSA public-key cryptosystem.
SHA-1 cryptographic hash function.
int RSA_private_encrypt(int size, unsigned char *input, unsigned char *output, RSA *key, int ignore)
Definition: openssl.h:141
MD5 message digest algorithm (hash function)