Skip to content

Commit

Permalink
added askar_key_get_supported_backends to FFI functionality
Browse files Browse the repository at this point in the history
Signed-off-by: Berend Sliedrecht <[email protected]>
  • Loading branch information
Berend Sliedrecht committed May 7, 2024
1 parent e739882 commit fae1148
Show file tree
Hide file tree
Showing 16 changed files with 686 additions and 312 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion askar-crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ p384 = { version = "0.13", default-features = false, features = [
"ecdh",
], optional = true }
rand = { version = "0.8", default-features = false }
secure-env = { package = "animo-secure-env", version = "0.1", optional = true }
secure-env = { package = "animo-secure-env", version = "0.2", optional = true }
serde = { version = "1.0", default-features = false, features = ["derive"] }
serde-json-core = { version = "0.5", default-features = false }
sha2 = { version = "0.10", default-features = false }
Expand Down
18 changes: 10 additions & 8 deletions include/libaries_askar.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,14 @@ typedef struct ArcHandle_FfiKeyEntryList {

typedef struct ArcHandle_FfiKeyEntryList KeyEntryListHandle;

typedef struct FfiResultList_String FfiStringList;

typedef struct ArcHandle_FfiStringList {
const FfiStringList *_0;
} ArcHandle_FfiStringList;

typedef struct ArcHandle_FfiStringList StringListHandle;

typedef int64_t CallbackId;

typedef void (*LogCallback)(const void *context,
Expand All @@ -229,14 +237,6 @@ typedef void (*LogCallback)(const void *context,
const char *file,
int32_t line);

typedef struct FfiResultList_String FfiStringList;

typedef struct ArcHandle_FfiStringList {
const FfiStringList *_0;
} ArcHandle_FfiStringList;

typedef struct ArcHandle_FfiStringList StringListHandle;

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
Expand Down Expand Up @@ -384,6 +384,8 @@ ErrorCode askar_key_get_public_bytes(LocalKeyHandle handle, struct SecretBuffer

ErrorCode askar_key_get_secret_bytes(LocalKeyHandle handle, struct SecretBuffer *out);

ErrorCode skar_key_get_supported_backends(StringListHandle *out);

ErrorCode askar_key_sign_message(LocalKeyHandle handle,
struct ByteBuffer message,
FfiStr sig_type,
Expand Down
27 changes: 27 additions & 0 deletions src/ffi/key.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::{
handle::ArcHandle,
result_list::{FfiStringList, StringListHandle},
secret::{EncryptedBuffer, SecretBuffer},
ErrorCode,
};
Expand Down Expand Up @@ -576,3 +577,29 @@ pub extern "C" fn askar_key_derive_ecdh_1pu(
Ok(ErrorCode::Success)
}
}

#[no_mangle]
pub extern "C" fn askar_key_get_supported_backends(out: *mut StringListHandle) -> ErrorCode {
catch_err! {
trace!("Retrieving supported key backends");
check_useful_c_ptr!(out);

let mut backends = vec![KeyBackend::Software];

if cfg!(feature = "mobile_secure_element") {
backends.push(KeyBackend::SecureElement);
}

let backends: Vec<String> = backends
.iter()
.map(|b| <KeyBackend as Into<&str>>::into(b.clone()).to_owned())
.collect();

let string_list = StringListHandle::create(FfiStringList::from(backends));


unsafe { *out = string_list };

Ok(ErrorCode::Success)
}
}
2 changes: 1 addition & 1 deletion src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mod macros;
mod error;
mod key;
mod log;
mod result_list;
pub(crate) mod result_list;
mod secret;
mod store;
mod tags;
Expand Down
28 changes: 27 additions & 1 deletion wrappers/javascript/aries-askar-nodejs/src/NodeJSAriesAskar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ import {
} from '@hyperledger/aries-askar-shared'

import {
allocateStringListHandle,
serializeArguments,
encryptedBufferStructToClass,
deallocateCallbackBuffer,
Expand Down Expand Up @@ -616,7 +617,7 @@ export class NodeJSAriesAskar implements AriesAskar {
}

public keyGenerate(options: KeyGenerateOptions): LocalKeyHandle {
const { algorithm, ephemeral,backend } = serializeArguments(options)
const { algorithm, ephemeral, backend } = serializeArguments(options)
const ret = allocatePointer()

const errorCode = this.nativeAriesAskar.askar_key_generate(algorithm, backend, ephemeral, ret)
Expand Down Expand Up @@ -742,6 +743,31 @@ export class NodeJSAriesAskar implements AriesAskar {
return encryptedBufferStructToClass(encryptedBuffer)
}

public keyGetSupportedBackends(): Array<string> {
const stringListHandlePtr = allocateStringListHandle()

const keyGetSupportedBackendsErrorCode = this.nativeAriesAskar.askar_key_get_supported_backends(stringListHandlePtr)
this.handleError(keyGetSupportedBackendsErrorCode)
const stringListHandle = stringListHandlePtr.deref() as Buffer

const listCountPtr = allocateInt32Buffer()
const stringListCountErrorCode = this.nativeAriesAskar.askar_string_list_count(stringListHandle, listCountPtr)
this.handleError(stringListCountErrorCode)
const count = listCountPtr.deref() as number

const supportedBackends = []

for (let i = 0; i < count; i++) {
const strPtr = allocateStringBuffer()
const errorCode = this.nativeAriesAskar.askar_string_list_get_item(stringListHandle, i, strPtr)
this.handleError(errorCode)
supportedBackends.push(strPtr.deref() as string)
}
this.nativeAriesAskar.askar_string_list_free(stringListHandle)

return supportedBackends
}

public scanFree(options: ScanFreeOptions): void {
const { scanHandle } = serializeArguments(options)

Expand Down
2 changes: 2 additions & 0 deletions wrappers/javascript/aries-askar-nodejs/src/ffi/alloc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export const allocateAeadParams = (): Buffer => alloc(AeadParamsStruct)

export const allocateLocalKeyHandle = allocatePointer

export const allocateStringListHandle = allocatePointer

export const allocateCallbackBuffer = (callback: Buffer) => setTimeout(() => callback, 1000000)

export const deallocateCallbackBuffer = (id: number) => clearTimeout(id as unknown as NodeJS.Timeout)
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export const nativeBindings = {
[FFI_POINTER, ByteBufferStruct, ByteBufferStruct, FFI_STRING, FFI_INT8_PTR],
],
askar_key_wrap_key: [FFI_ERROR_CODE, [FFI_POINTER, FFI_POINTER, ByteBufferStruct, EncryptedBufferStructPtr]],
askar_key_get_supported_backends: [FFI_ERROR_CODE, [FFI_STRING_LIST_HANDLE]],

askar_scan_free: [FFI_ERROR_CODE, [FFI_SCAN_HANDLE]],
askar_scan_next: [FFI_ERROR_CODE, [FFI_SCAN_HANDLE, FFI_CALLBACK_PTR, FFI_CALLBACK_ID]],
Expand Down
9 changes: 9 additions & 0 deletions wrappers/javascript/aries-askar-nodejs/tests/keys.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import { Key, KeyAlgs, KeyMethod } from '@hyperledger/aries-askar-shared'

import { ariesAskarNodeJS } from '../src'

describe('keys', () => {
beforeAll(() => {
require('@hyperledger/aries-askar-nodejs')
})

test('supported backends', () => {
const backends = ariesAskarNodeJS.keyGetSupportedBackends()

expect(backends.length).toStrictEqual(1)
expect(backends).toStrictEqual(expect.arrayContaining(['software']))
})

test('aes cbc hmac', () => {
const key = Key.generate(KeyAlgs.AesA128CbcHs256)
expect(key.algorithm).toStrictEqual(KeyAlgs.AesA128CbcHs256)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import type {
SessionHandle,
StoreHandle,
} from '../crypto'
import type { KeyAlgs, LogLevel, SigAlgs } from '../enums'
import { KeyBackend } from '../enums'
import type { KeyAlgs, LogLevel, SigAlgs, KeyBackend } from '../enums'
import type { AriesAskarErrorObject } from '../error'
import type { AeadParams, EncryptedBuffer } from '../types'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AriesAskarError } from "../error"
import { AriesAskarError } from '../error'

export enum KeyBackend {
Software = 'software',
Expand Down
3 changes: 2 additions & 1 deletion wrappers/javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"format": "yarn prettier --write",
"check-format": "yarn prettier --list-different",
"lint": "eslint .",
"lint:fix": "yarn lint --fix",
"validate": "yarn lint && yarn check-types && yarn check-format",
"reset": "find . -type dir -name node_modules | xargs rm -rf && rm -rf yarn.lock",
"example": "yarn --cwd react-native-example",
Expand All @@ -42,7 +43,7 @@
"eslint-import-resolver-typescript": "^2.5.0",
"eslint-plugin-import": "^2.25.4",
"eslint-plugin-prettier": "^4.0.0",
"lerna": "^7.2.0",
"lerna": "^8.1.2",
"prettier": "^2.5.1",
"rimraf": "^5.0.5",
"ts-jest": "^29.0.5",
Expand Down
Loading

0 comments on commit fae1148

Please sign in to comment.