Line data Source code
1 : /* 2 : Mosh: the mobile shell 3 : Copyright 2012 Keith Winstein 4 : 5 : This program is free software: you can redistribute it and/or modify 6 : it under the terms of the GNU General Public License as published by 7 : the Free Software Foundation, either version 3 of the License, or 8 : (at your option) any later version. 9 : 10 : This program is distributed in the hope that it will be useful, 11 : but WITHOUT ANY WARRANTY; without even the implied warranty of 12 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 : GNU General Public License for more details. 14 : 15 : You should have received a copy of the GNU General Public License 16 : along with this program. If not, see <http://www.gnu.org/licenses/>. 17 : 18 : In addition, as a special exception, the copyright holders give 19 : permission to link the code of portions of this program with the 20 : OpenSSL library under certain conditions as described in each 21 : individual source file, and distribute linked combinations including 22 : the two. 23 : 24 : You must obey the GNU General Public License in all respects for all 25 : of the code used other than OpenSSL. If you modify file(s) with this 26 : exception, you may extend this exception to your version of the 27 : file(s), but you are not obligated to do so. If you do not wish to do 28 : so, delete this exception statement from your version. If you delete 29 : this exception statement from all source files in the program, then 30 : also delete it here. 31 : */ 32 : 33 : #ifndef CRYPTO_HPP 34 : #define CRYPTO_HPP 35 : 36 : #include "ae.h" 37 : #include <string> 38 : #include <string.h> 39 : #include <stdint.h> 40 : #include <stdlib.h> 41 : #include <exception> 42 : 43 : 44 : long int myatoi( const char *str ); 45 : 46 : class PRNG; 47 : 48 : namespace Crypto { 49 : using std::string; 50 : 51 : class CryptoException : public std::exception { 52 : public: 53 : string text; 54 : bool fatal; 55 2033 : CryptoException( string s_text, bool s_fatal = false ) 56 2033 : : text( s_text ), fatal( s_fatal ) {}; 57 0 : const char *what() const throw () { return text.c_str(); } 58 4066 : ~CryptoException() throw () {} 59 : }; 60 : 61 : /* 62 : * OCB (and other algorithms) require a source of nonce/sequence 63 : * numbers that never repeats its output. Enforce that with this 64 : * function. 65 : */ 66 : uint64_t unique( void ); 67 : 68 : /* 16-byte-aligned buffer, with length. */ 69 : class AlignedBuffer { 70 : private: 71 : size_t m_len; 72 : void *m_allocated; 73 : char *m_data; 74 : 75 : public: 76 : AlignedBuffer( size_t len, const char *data = NULL ); 77 : 78 10144 : ~AlignedBuffer() { 79 10144 : free( m_allocated ); 80 8536 : } 81 : 82 164057 : char * data( void ) const { return m_data; } 83 90029 : size_t len( void ) const { return m_len; } 84 : 85 : private: 86 : /* Not implemented */ 87 : AlignedBuffer( const AlignedBuffer & ); 88 : AlignedBuffer & operator=( const AlignedBuffer & ); 89 : }; 90 : 91 : class Base64Key { 92 : private: 93 : unsigned char key[ 16 ]; 94 : 95 : public: 96 : Base64Key(); /* random key */ 97 : Base64Key(PRNG &prng); 98 : Base64Key( string printable_key ); 99 : string printable_key( void ) const; 100 263300 : unsigned char *data( void ) { return key; } 101 : }; 102 : 103 : class Nonce { 104 : public: 105 : static const int NONCE_LEN = 12; 106 : 107 : private: 108 : char bytes[ NONCE_LEN ]; 109 : 110 : public: 111 : Nonce( uint64_t val ); 112 : Nonce( const char *s_bytes, size_t len ); 113 : 114 76202 : string cc_str( void ) const { return string( bytes + 4, 8 ); } 115 78167 : const char *data( void ) const { return bytes; } 116 : uint64_t val( void ) const; 117 : }; 118 : 119 8151764 : class Message { 120 : public: 121 : const Nonce nonce; 122 : const string text; 123 : 124 : Message( const char *nonce_bytes, size_t nonce_len, 125 : const char *text_bytes, size_t text_len ) 126 : : nonce( nonce_bytes, nonce_len ), 127 : text( text_bytes, text_len ) {} 128 : 129 8076154 : Message( const Nonce & s_nonce, const string & s_text ) 130 8076154 : : nonce( s_nonce ), 131 8076154 : text( s_text ) {} 132 : }; 133 : 134 : class Session { 135 : private: 136 : Base64Key key; 137 : AlignedBuffer ctx_buf; 138 : ae_ctx *ctx; 139 : uint64_t blocks_encrypted; 140 : 141 : AlignedBuffer plaintext_buffer; 142 : AlignedBuffer ciphertext_buffer; 143 : AlignedBuffer nonce_buffer; 144 : 145 : public: 146 : static const int RECEIVE_MTU = 2048; 147 : /* Overhead (not counting the nonce, which is handled by network transport) */ 148 : static const int ADDED_BYTES = 16 /* final OCB block */; 149 : 150 : Session( Base64Key s_key ); 151 : ~Session(); 152 : 153 : const string encrypt( const Message & plaintext ); 154 : const Message decrypt( const char *str, size_t len ); 155 34801 : const Message decrypt( const string & ciphertext ) { 156 34801 : return decrypt( ciphertext.data(), ciphertext.size() ); 157 : } 158 : 159 : Session( const Session & ); 160 : Session & operator=( const Session & ); 161 : }; 162 : 163 : void disable_dumping_core( void ); 164 : void reenable_dumping_core( void ); 165 : } 166 : 167 : #endif