-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: pin_derived_eph_pub+priv (#128)
Signed-off-by: Berend Sliedrecht <[email protected]>
- Loading branch information
1 parent
4ffd02f
commit 62ea105
Showing
14 changed files
with
158 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,6 +100,6 @@ android | |
|
||
# build output | ||
dist/ | ||
types/ | ||
packages/ui/types | ||
|
||
storybook-static | ||
storybook-static |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const FUNKE_WALLET_INSTANCE_LONG_TERM_AES_KEY_ID = 'FUNKE_WALLET_INSTANCE_LONG_TERM_AES_KEY_ID' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { aes128Gcm } from '@package/agent' | ||
import { FUNKE_WALLET_INSTANCE_LONG_TERM_AES_KEY_ID } from '../constants' | ||
|
||
export const funkeAes128Gcm = aes128Gcm(FUNKE_WALLET_INSTANCE_LONG_TERM_AES_KEY_ID) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { type AgentContext, TypedArrayEncoder } from '@credo-ts/core' | ||
import { Key, KeyAlgs, KeyMethod } from '@hyperledger/aries-askar-react-native' | ||
import { kdf } from '@package/secure-store/kdf' | ||
import { funkeAes128Gcm } from './aes' | ||
|
||
/** | ||
* | ||
* Derive a key pair based on a numeric pin according to the steps in B' | ||
* | ||
* returns pin_derived_eph_pub + pin_derived_eph_priv | ||
* | ||
* @todo Might be good later to add methods like `signWithPidPin` | ||
* | ||
*/ | ||
export const deriveKeypairFromPin = async (agentContext: AgentContext, pin: Array<number>) => { | ||
if (!(await funkeAes128Gcm.aes128GcmHasKey({ agentContext }))) { | ||
throw new Error('No AES key found in storage. Flow is called in an incorrect way!') | ||
} | ||
|
||
const pinSecret = await funkeAes128Gcm.aes128GcmEncrypt({ | ||
agentContext, | ||
data: new Uint8Array(pin), | ||
}) | ||
|
||
const pinSeed = await kdf.derive( | ||
TypedArrayEncoder.toUtf8String(new Uint8Array(pin)), | ||
TypedArrayEncoder.toUtf8String(pinSecret) | ||
) | ||
|
||
return Key.fromSeed({ | ||
seed: new Uint8Array(TypedArrayEncoder.fromHex(pinSeed)), | ||
method: KeyMethod.None, | ||
algorithm: KeyAlgs.EcSecp256r1, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { assertAskarWallet } from '@credo-ts/askar/build/utils/assertAskarWallet' | ||
import type { AgentContext } from '@credo-ts/core' | ||
import { Key, KeyAlgs } from '@hyperledger/aries-askar-react-native' | ||
|
||
const aes128GcmGenerateAndStoreKey = | ||
(id: string) => | ||
async ({ agentContext }: { agentContext: AgentContext }) => { | ||
const wallet = agentContext.wallet | ||
const key = Key.generate(KeyAlgs.AesA128Gcm) | ||
assertAskarWallet(wallet) | ||
await wallet.withSession((session) => session.insertKey({ name: id, key })) | ||
} | ||
|
||
const aes128GcmHasKey = | ||
(id: string) => | ||
async ({ agentContext }: { agentContext: AgentContext }) => { | ||
const wallet = agentContext.wallet | ||
assertAskarWallet(wallet) | ||
const aesKey = await wallet.withSession((session) => session.fetchKey({ name: id })) | ||
|
||
return Boolean(aesKey) | ||
} | ||
|
||
const aes128GcmGetKey = | ||
(id: string) => | ||
async ({ agentContext }: { agentContext: AgentContext }) => { | ||
const wallet = agentContext.wallet | ||
assertAskarWallet(wallet) | ||
const aesKey = await wallet.withSession((session) => session.fetchKey({ name: id })) | ||
|
||
if (!aesKey) { | ||
throw new Error(`AES-128-GCM key not found with id: ${id}`) | ||
} | ||
|
||
return aesKey.key | ||
} | ||
|
||
const aes128GcmEncrypt = | ||
(id: string) => | ||
async ({ | ||
data, | ||
agentContext, | ||
nonce = new Uint8Array(12).fill(1), | ||
}: { | ||
data: Uint8Array | ||
agentContext: AgentContext | ||
nonce?: Uint8Array | ||
}) => { | ||
const wallet = agentContext.wallet | ||
assertAskarWallet(wallet) | ||
|
||
const key = await aes128GcmGetKey(id)({ agentContext }) | ||
|
||
const { ciphertextWithTag } = key.aeadEncrypt({ nonce, message: data }) | ||
|
||
return ciphertextWithTag | ||
} | ||
|
||
export const aes128Gcm = (id: string) => ({ | ||
aes128GcmGenerateAndStoreKey: aes128GcmGenerateAndStoreKey(id), | ||
aes128GcmHasKey: aes128GcmHasKey(id), | ||
aes128GcmEncrypt: aes128GcmEncrypt(id), | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './aes' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.