|
| 1 | +const wolfcrypt = require( '../build/Release/wolfcrypt' ); |
| 2 | +const stream = require( 'stream' ); |
| 3 | + |
| 4 | +class WolfSSLEVP |
| 5 | +{ |
| 6 | + /** |
| 7 | + * Creates a new evp cipher by calling EVP_CIPHER_CTX_new |
| 8 | + * |
| 9 | + * @remarks finalize or free must be called to free the cipher |
| 10 | + */ |
| 11 | + constructor() |
| 12 | + { |
| 13 | + this.totalInputLength = 0 |
| 14 | + this.evp = wolfcrypt.EVP_CIPHER_CTX_new() |
| 15 | + } |
| 16 | + |
| 17 | + /** |
| 18 | + * Updates the internal state with data for cipher. |
| 19 | + * |
| 20 | + * @param data The data that will be added to the cipher. |
| 21 | + * |
| 22 | + * @returns The result data if possible. |
| 23 | + * |
| 24 | + * @throws {Error} If the cipher update fails. |
| 25 | + * |
| 26 | + * @remarks This function can be called multiple times. |
| 27 | + */ |
| 28 | + update( data ) |
| 29 | + { |
| 30 | + if ( this.evp == null ) |
| 31 | + { |
| 32 | + throw 'Cipher is not allocated' |
| 33 | + } |
| 34 | + |
| 35 | + if ( typeof data == 'string' ) |
| 36 | + { |
| 37 | + data = Buffer.from( data ) |
| 38 | + } |
| 39 | + |
| 40 | + this.totalInputLength += data.length |
| 41 | + |
| 42 | + let outBuffer = Buffer.alloc( this.totalInputLength ) |
| 43 | + |
| 44 | + let ret = wolfcrypt.EVP_CipherUpdate( this.evp, outBuffer, data, data.length ) |
| 45 | + |
| 46 | + if ( ret < 0 ) |
| 47 | + { |
| 48 | + throw 'Failed to update cipher' |
| 49 | + } |
| 50 | + |
| 51 | + if ( ret > 0 ) |
| 52 | + { |
| 53 | + this.totalInputLength -= ret; |
| 54 | + |
| 55 | + return outBuffer.subarray( 0, ret ) |
| 56 | + } |
| 57 | + |
| 58 | + return Buffer.alloc( 0 ) |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Finalize the encryption/decryption process. |
| 63 | + * |
| 64 | + * @returns The last block of data. |
| 65 | + * |
| 66 | + * @throws {Error} If the EVP_CipherFinal fails. |
| 67 | + * |
| 68 | + * @remarks This function should be called once to finalize the |
| 69 | + * encryption/decryption process. |
| 70 | + */ |
| 71 | + finalize() |
| 72 | + { |
| 73 | + if ( this.evp == null ) |
| 74 | + { |
| 75 | + throw 'Cipher is not allocated' |
| 76 | + } |
| 77 | + |
| 78 | + if ( this.totalInputLength % 16 != 0 ) |
| 79 | + { |
| 80 | + this.totalInputLength += ( 16 - this.totalInputLength % 16 ) |
| 81 | + } |
| 82 | + |
| 83 | + let outBuffer = Buffer.alloc( this.totalInputLength ) |
| 84 | + this.totalInputLength = 0; |
| 85 | + |
| 86 | + let ret = wolfcrypt.EVP_CipherFinal( this.evp, outBuffer ) |
| 87 | + |
| 88 | + this.free() |
| 89 | + |
| 90 | + if ( ret < 0 ) |
| 91 | + { |
| 92 | + throw 'Failed to finalize cipher' |
| 93 | + } |
| 94 | + |
| 95 | + if ( ret > 0 ) |
| 96 | + { |
| 97 | + return outBuffer.subarray( 0, ret ) |
| 98 | + } |
| 99 | + |
| 100 | + return Buffer.alloc( 0 ) |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Frees the evp ctx |
| 105 | + * |
| 106 | + * @throws {Error} If the evp pointer is set to null |
| 107 | + * |
| 108 | + * @remarks This function should be called if the caller |
| 109 | + * no longer wants to use the cipher, update and finalize |
| 110 | + * will throw errors if free has been called |
| 111 | + */ |
| 112 | + free() |
| 113 | + { |
| 114 | + if ( this.evp != null ) |
| 115 | + { |
| 116 | + wolfcrypt.EVP_CIPHER_CTX_free( this.evp ) |
| 117 | + this.evp = null |
| 118 | + } |
| 119 | + else |
| 120 | + { |
| 121 | + throw 'Cipher is not allocated' |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +class WolfSSLEncryptor extends WolfSSLEVP |
| 127 | +{ |
| 128 | + /** |
| 129 | + * Initializes the evp cipher for encryption by calling EVP_CipherInit |
| 130 | + * |
| 131 | + * @param cipher the cipher to be used |
| 132 | + * @param key aes key |
| 133 | + * @param iv aes initialization vector |
| 134 | + */ |
| 135 | + constructor( cipher, key, iv ) |
| 136 | + { |
| 137 | + super() |
| 138 | + wolfcrypt.EVP_CipherInit( this.evp, cipher, key, iv, 1 ) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +exports.WolfSSLEncryptor = WolfSSLEncryptor |
| 143 | + |
| 144 | +class WolfSSLDecryptor extends WolfSSLEVP |
| 145 | +{ |
| 146 | + /** |
| 147 | + * Initializes the evp cipher for decryption by calling EVP_CipherInit |
| 148 | + * |
| 149 | + * @param cipher the cipher to be used |
| 150 | + * @param key aes key |
| 151 | + * @param iv aes initialization vector |
| 152 | + */ |
| 153 | + constructor( cipher, key, iv ) |
| 154 | + { |
| 155 | + super() |
| 156 | + wolfcrypt.EVP_CipherInit( this.evp, cipher, key, iv, 0 ) |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +exports.WolfSSLDecryptor = WolfSSLDecryptor |
| 161 | + |
| 162 | +class WolfSSLEVPStream extends stream.Transform |
| 163 | +{ |
| 164 | + constructor() |
| 165 | + { |
| 166 | + super() |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Transforms input data by encrypting or decrypting it with cipher.update |
| 171 | + * |
| 172 | + * @param chunk the data to be encrypted |
| 173 | + * @param enc encoding of the chunk |
| 174 | + * @param cb the callback function that handles |
| 175 | + * the next task of the stream |
| 176 | + */ |
| 177 | + _transform( chunk, enc, cb ) |
| 178 | + { |
| 179 | + let buffer = Buffer.isBuffer( chunk ) ? chunk: new Buffer( chunk, enc ) |
| 180 | + |
| 181 | + let ret_buffer = this.cipher.update( chunk ) |
| 182 | + |
| 183 | + if ( ret_buffer.length > 0 ) |
| 184 | + { |
| 185 | + this.push( ret_buffer ) |
| 186 | + } |
| 187 | + |
| 188 | + cb() |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * Called when the end of input is reached, call cipher.finalize |
| 193 | + * to finish the encryption |
| 194 | + * |
| 195 | + * @param cb the callback function that handles |
| 196 | + * the next task of the stream |
| 197 | + */ |
| 198 | + _flush( cb ) |
| 199 | + { |
| 200 | + let ret_buffer = this.cipher.finalize() |
| 201 | + |
| 202 | + if ( ret_buffer.length > 0 ) |
| 203 | + { |
| 204 | + this.push( ret_buffer ) |
| 205 | + } |
| 206 | + |
| 207 | + cb() |
| 208 | + } |
| 209 | +} |
| 210 | + |
| 211 | +class WolfSSLEncryptionStream extends WolfSSLEVPStream |
| 212 | +{ |
| 213 | + /** |
| 214 | + * Creates a new encryptor and sets it to cipher, can then be used |
| 215 | + * by the generic stream methods |
| 216 | + * |
| 217 | + * @param cipher the cipher to be used |
| 218 | + * @param key aes key |
| 219 | + * @param iv aes initialization vector |
| 220 | + */ |
| 221 | + constructor( cipher, key, iv ) |
| 222 | + { |
| 223 | + super() |
| 224 | + this.cipher = new WolfSSLEncryptor( cipher, key, iv ) |
| 225 | + } |
| 226 | +} |
| 227 | + |
| 228 | +exports.WolfSSLEncryptionStream = WolfSSLEncryptionStream |
| 229 | + |
| 230 | +class WolfSSLDecryptionStream extends WolfSSLEVPStream |
| 231 | +{ |
| 232 | + /** |
| 233 | + * Creates a new encryptor and sets it to cipher, can then be used |
| 234 | + * by the generic stream methods |
| 235 | + * |
| 236 | + * @param cipher the cipher to be used |
| 237 | + * @param key aes key |
| 238 | + * @param iv aes initialization vector |
| 239 | + */ |
| 240 | + constructor( cipher, key, iv ) |
| 241 | + { |
| 242 | + super() |
| 243 | + this.cipher = new WolfSSLDecryptor( cipher, key, iv ) |
| 244 | + } |
| 245 | +} |
| 246 | + |
| 247 | +exports.WolfSSLDecryptionStream = WolfSSLDecryptionStream |
0 commit comments