openssl.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * \file openssl.h
  3. *
  4. * \brief OpenSSL wrapper (definitions, inline functions).
  5. *
  6. * Copyright (C) 2006-2010, Brainspark B.V.
  7. *
  8. * This file is part of PolarSSL (http://www.polarssl.org)
  9. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  10. *
  11. * All rights reserved.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License along
  24. * with this program; if not, write to the Free Software Foundation, Inc.,
  25. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  26. */
  27. /*
  28. * OpenSSL wrapper contributed by David Barett
  29. */
  30. #ifndef POLARSSL_OPENSSL_H
  31. #define POLARSSL_OPENSSL_H
  32. #include "polarssl/aes.h"
  33. #include "polarssl/md5.h"
  34. #include "polarssl/rsa.h"
  35. #include "polarssl/sha1.h"
  36. #define AES_SIZE 16
  37. #define AES_BLOCK_SIZE 16
  38. #define AES_KEY aes_context
  39. #define MD5_CTX md5_context
  40. #define SHA_CTX sha1_context
  41. #define SHA1_Init( CTX ) \
  42. sha1_starts( (CTX) )
  43. #define SHA1_Update( CTX, BUF, LEN ) \
  44. sha1_update( (CTX), (unsigned char *)(BUF), (LEN) )
  45. #define SHA1_Final( OUT, CTX ) \
  46. sha1_finish( (CTX), (OUT) )
  47. #define MD5_Init( CTX ) \
  48. md5_starts( (CTX) )
  49. #define MD5_Update( CTX, BUF, LEN ) \
  50. md5_update( (CTX), (unsigned char *)(BUF), (LEN) )
  51. #define MD5_Final( OUT, CTX ) \
  52. md5_finish( (CTX), (OUT) )
  53. #define AES_set_encrypt_key( KEY, KEYSIZE, CTX ) \
  54. aes_setkey_enc( (CTX), (KEY), (KEYSIZE) )
  55. #define AES_set_decrypt_key( KEY, KEYSIZE, CTX ) \
  56. aes_setkey_dec( (CTX), (KEY), (KEYSIZE) )
  57. #define AES_cbc_encrypt( INPUT, OUTPUT, LEN, CTX, IV, MODE ) \
  58. aes_crypt_cbc( (CTX), (MODE), (LEN), (IV), (INPUT), (OUTPUT) )
  59. /*
  60. * RSA stuff follows. TODO: needs cleanup
  61. */
  62. inline int __RSA_Passthrough( void *output, void *input, int size )
  63. {
  64. memcpy( output, input, size );
  65. return size;
  66. }
  67. inline rsa_context* d2i_RSA_PUBKEY( void *ignore, unsigned char **bufptr,
  68. int len )
  69. {
  70. unsigned char *buffer = *(unsigned char **) bufptr;
  71. rsa_context *rsa;
  72. /*
  73. * Not a general-purpose parser: only parses public key from *exactly*
  74. * openssl genrsa -out privkey.pem 512 (or 1024)
  75. * openssl rsa -in privkey.pem -out privatekey.der -outform der
  76. * openssl rsa -in privkey.pem -out pubkey.der -outform der -pubout
  77. *
  78. * TODO: make a general-purpose parse
  79. */
  80. if( ignore != 0 || ( len != 94 && len != 162 ) )
  81. return( 0 );
  82. rsa = (rsa_context *) malloc( sizeof( rsa_rsa ) );
  83. if( rsa == NULL )
  84. return( 0 );
  85. memset( rsa, 0, sizeof( rsa_context ) );
  86. if( ( len == 94 &&
  87. mpi_read_binary( &rsa->N, &buffer[ 25], 64 ) == 0 &&
  88. mpi_read_binary( &rsa->E, &buffer[ 91], 3 ) == 0 ) ||
  89. ( len == 162 &&
  90. mpi_read_binary( &rsa->N, &buffer[ 29], 128 ) == 0 ) &&
  91. mpi_read_binary( &rsa->E, &buffer[159], 3 ) == 0 )
  92. {
  93. /*
  94. * key read successfully
  95. */
  96. rsa->len = ( mpi_msb( &rsa->N ) + 7 ) >> 3;
  97. return( rsa );
  98. }
  99. else
  100. {
  101. memset( rsa, 0, sizeof( rsa_context ) );
  102. free( rsa );
  103. return( 0 );
  104. }
  105. }
  106. #define RSA rsa_context
  107. #define RSA_PKCS1_PADDING 1 /* ignored; always encrypt with this */
  108. #define RSA_size( CTX ) (CTX)->len
  109. #define RSA_free( CTX ) rsa_free( CTX )
  110. #define ERR_get_error( ) "ERR_get_error() not supported"
  111. #define RSA_blinding_off( IGNORE )
  112. #define d2i_RSAPrivateKey( a, b, c ) new rsa_context /* TODO: C++ bleh */
  113. 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; }
  114. 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; }
  115. 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; }
  116. 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; }
  117. #ifdef __cplusplus
  118. }
  119. #endif
  120. #endif /* openssl.h */