Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WasmThemis utilities #489

Merged
merged 3 commits into from
Jul 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions src/wrappers/themis/wasm/src/secure_keygen.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

const libthemis = require('./libthemis.js')
const errors = require('./themis_error.js')
const utils = require('./utils.js')

const subsystem = 'KeyPair'

Expand All @@ -40,31 +41,20 @@ const keyKinds = {

class PrivateKey extends Uint8Array {
constructor(array) {
array = coerceToBytes(array)
array = utils.coerceToBytes(array)
super(array)
validateKeyBuffer(this, [keyKinds.EC_PRIVATE, keyKinds.RSA_PRIVATE])
}
}

class PublicKey extends Uint8Array {
constructor(array) {
array = coerceToBytes(array)
array = utils.coerceToBytes(array)
super(array)
validateKeyBuffer(this, [keyKinds.EC_PUBLIC, keyKinds.RSA_PUBLIC])
}
}

function coerceToBytes(key) {
if (key instanceof Uint8Array) {
return key
}
if (key instanceof ArrayBuffer) {
return new Uint8Array(key)
}
throw new ThemisError(subsystem, ThemisErrorCode.INVALID_PARAMETER,
'key type mismatch, expect "Uint8Array" or "ArrayBuffer"')
}

function validateKeyBuffer(buffer, expectedKinds) {
if (buffer.length == 0) {
throw new ThemisError(subsystem, ThemisErrorCode.INVALID_PARAMETER,
Expand Down
2 changes: 1 addition & 1 deletion src/wrappers/themis/wasm/src/themis_error.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const errorCodes = {
class ThemisError extends Error {
constructor(subsystem, errorCode) {
var message = 'Themis: ' + subsystem + ': '
if (arguments.length >= 2) {
if (arguments.length > 2) {
message += arguments[2]
for (var i = 3; i < arguments.length; i++) {
message += ', '
Expand Down
33 changes: 33 additions & 0 deletions src/wrappers/themis/wasm/src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) 2019 Cossack Labs Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
* @file
* Miscellaneous utilities.
*/

/**
* Convert an object into a byte buffer.
*
* @throws TypeError if input type is not supported
*/
module.exports.coerceToBytes = function(buffer) {
if (buffer instanceof Uint8Array) {
return buffer
}
if (buffer instanceof ArrayBuffer) {
return new Uint8Array(buffer)
}
throw new TypeError('type mismatch, expect "Uint8Array" or "ArrayBuffer"')
}