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 <sliedrecht@berend.io>
  • Loading branch information
berendsliedrecht committed May 7, 2024
1 parent e739882 commit a0ab8f1
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 14 deletions.
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
26 changes: 26 additions & 0 deletions wrappers/python/aries_askar/bindings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ async def store_list_profiles(handle: StoreHandle) -> Sequence[str]:
byref(buf),
)
ret.append(str(buf))

return ret


Expand Down Expand Up @@ -865,6 +866,31 @@ def key_unwrap_key(
return result


def key_get_supported_backends() -> Sequence[str]:
handle = StringListHandle()
invoke("askar_key_get_string_handle", POINTER(StringListHandle), byref(handle))
count = c_int32()
invoke(
"askar_string_list_count",
(StringListHandle, POINTER(c_int32)),
handle,
byref(count),
)
ret = []
for idx in range(count.value):
buf = StrBuffer()
invoke(
"askar_string_list_get_item",
(StringListHandle, c_int32, POINTER(StrBuffer)),
handle,
idx,
byref(buf),
)
ret.append(str(buf))

return ret


def key_crypto_box_random_nonce() -> ByteBuffer:
buf = ByteBuffer()
invoke(
Expand Down
14 changes: 12 additions & 2 deletions wrappers/python/aries_askar/key.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Handling of Key instances."""

from typing import Union, Optional
from typing import Union, Optional, Sequence

from . import bindings
from .bindings import AeadParams, Encrypted, LocalKeyHandle
Expand All @@ -15,7 +15,13 @@ def __init__(self, handle: LocalKeyHandle):
self._handle = handle

@classmethod
def generate(cls, alg: Union[str, KeyAlg], *, key_backend: Optional[KeyBackend] = None, ephemeral: bool = False) -> "Key":
def generate(
cls,
alg: Union[str, KeyAlg],
*,
key_backend: Optional[KeyBackend] = None,
ephemeral: bool = False,
) -> "Key":
return cls(bindings.key_generate(alg, key_backend, ephemeral))

@classmethod
Expand All @@ -40,6 +46,10 @@ def from_public_bytes(cls, alg: Union[str, KeyAlg], public: bytes) -> "Key":
def from_jwk(cls, jwk: Union[dict, str, bytes]) -> "Key":
return cls(bindings.key_from_jwk(jwk))

@classmethod
def get_supported_backends(cls) -> Sequence[str]:
return bindings.key_get_supported_backends()

@property
def handle(self) -> LocalKeyHandle:
"""Accessor for the key handle."""
Expand Down
7 changes: 7 additions & 0 deletions wrappers/python/tests/test_keys.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json

from aries_askar.types import KeyBackend
import pytest

from aries_askar import (
Expand All @@ -9,6 +10,12 @@
)


def test_get_supported_backends():
backends = Key.get_supported_backends()

assert backends == [str(KeyBackend.Software)]


def test_aes_cbc_hmac():
key = Key.generate(KeyAlg.A128CBC_HS256)
assert key.algorithm == KeyAlg.A128CBC_HS256
Expand Down

0 comments on commit a0ab8f1

Please sign in to comment.