Skip to content

Commit 7efed12

Browse files
author
jin
committed
Fallback to old crypto
1 parent 4e8ccf6 commit 7efed12

File tree

2 files changed

+159
-2
lines changed

2 files changed

+159
-2
lines changed

meetup/meetup.ts

+30-2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,29 @@ namespace $ {
7979

8080
}
8181

82+
@ $mol_mem_key
83+
peer_secret_old( peer: $mol_int62_string ) {
84+
85+
const priv = $piterjs_domain.secure_private()
86+
const land = this.joined_node()?.land
87+
if( !land ) return null
88+
89+
if( priv ) {
90+
91+
const auth = this.land.peer()
92+
const pub = peer === auth.id ? auth.key_public_serial : land.unit( peer, peer )?.data as string | undefined
93+
return pub ? $mol_wire_sync( $piterjs_secret ).derive( priv, pub ) : null
94+
95+
} else {
96+
97+
const priv = land.peer().key_private_serial
98+
const pub = $piterjs_domain.secure_public()
99+
return $mol_wire_sync( $mol_crypto_secret ).derive( priv, pub )
100+
101+
}
102+
103+
}
104+
82105
@ $mol_mem
83106
joined_node() {
84107
return this.yoke( 'joined', $hyoo_crowd_dict, [ '' ], [], [ '0_0' ] )
@@ -107,8 +130,13 @@ namespace $ {
107130
try {
108131
return $mol_charset_decode( secret.decrypt( closed as Uint8Array, salt ) )
109132
} catch( error ) {
110-
$mol_fail_log( error )
111-
return ''
133+
try {
134+
const secret = $mol_wire_sync( this.peer_secret_old( id )! )
135+
return $mol_charset_decode( secret.decrypt( closed as Uint8Array, $mol_charset_encode( this.id() ) ) )
136+
} catch( error ) {
137+
$mol_fail_log( error )
138+
return ''
139+
}
112140
}
113141

114142
}

secret/secret.ts

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
namespace $ {
2+
3+
const algorithm = {
4+
name: 'AES-GCM',
5+
length: 128,
6+
tagLength: 32,
7+
}
8+
9+
/** Symmetric cipher with shortest payload */
10+
export class $piterjs_secret extends Object {
11+
12+
/** Key size in bytes. */
13+
static size = 16
14+
15+
constructor(
16+
readonly native: CryptoKey & { type: 'secret' }
17+
) {
18+
super()
19+
}
20+
21+
static async generate() {
22+
return new this(
23+
await $mol_crypto_native.subtle.generateKey(
24+
algorithm,
25+
true,
26+
[ 'encrypt', 'decrypt' ]
27+
) as CryptoKey & { type: 'secret' }
28+
)
29+
}
30+
31+
static async from( serial: BufferSource | string ) {
32+
33+
if( typeof serial === 'string' ) {
34+
serial = $mol_charset_encode( serial )
35+
serial = await $mol_crypto_native.subtle.digest( 'SHA-256', serial )
36+
}
37+
38+
return new this(
39+
await $mol_crypto_native.subtle.importKey(
40+
'raw',
41+
serial,
42+
algorithm,
43+
true,
44+
[ 'encrypt', 'decrypt' ],
45+
) as CryptoKey & { type: 'secret' }
46+
)
47+
48+
}
49+
50+
static async derive( private_serial: string, public_serial: string ) {
51+
52+
const ecdh = { name: "ECDH", namedCurve: "P-256" }
53+
const jwk = { crv: 'P-256', ext: true, kty: 'EC' }
54+
55+
const private_key = await $mol_crypto_native.subtle.importKey(
56+
'jwk',
57+
{
58+
... jwk,
59+
key_ops: [ 'deriveKey' ],
60+
x: private_serial.slice( 0, 43 ),
61+
y: private_serial.slice( 43, 86 ),
62+
d: private_serial.slice( 86, 129 ),
63+
},
64+
ecdh,
65+
true,
66+
[ 'deriveKey' ],
67+
)
68+
69+
const public_key = await $mol_crypto_native.subtle.importKey(
70+
'jwk',
71+
{
72+
... jwk,
73+
key_ops: [],
74+
x: public_serial.slice( 0, 43 ),
75+
y: public_serial.slice( 43, 86 ),
76+
},
77+
ecdh,
78+
true,
79+
[],
80+
)
81+
82+
const secret = await $mol_crypto_native.subtle.deriveKey(
83+
{
84+
name: "ECDH",
85+
public: public_key,
86+
},
87+
private_key,
88+
algorithm,
89+
true,
90+
[ "encrypt", "decrypt" ],
91+
)
92+
93+
return new this( secret as CryptoKey & { type: 'secret' } )
94+
}
95+
96+
/** 16 bytes */
97+
async serial() {
98+
return await $mol_crypto_native.subtle.exportKey(
99+
'raw',
100+
this.native,
101+
)
102+
}
103+
104+
/** 16n bytes */
105+
async encrypt( open: BufferSource, salt: BufferSource ): Promise< ArrayBuffer > {
106+
return await $mol_crypto_native.subtle.encrypt(
107+
{
108+
... algorithm,
109+
iv: salt,
110+
},
111+
this.native,
112+
open
113+
)
114+
}
115+
116+
async decrypt( closed: BufferSource, salt : BufferSource ): Promise< ArrayBuffer > {
117+
return await $mol_crypto_native.subtle.decrypt(
118+
{
119+
... algorithm,
120+
iv: salt,
121+
},
122+
this.native,
123+
closed
124+
)
125+
}
126+
127+
}
128+
129+
}

0 commit comments

Comments
 (0)