(envs: EncryptedEnvEntry[], publicKeyBytes: Uint8Array)
| 928 | } |
| 929 | |
| 930 | async function encryptEnvWithKey(envs: EncryptedEnvEntry[], publicKeyBytes: Uint8Array) { |
| 931 | const envsJson = JSON.stringify({ env: envs }); |
| 932 | const remotePubkey = publicKeyBytes && publicKeyBytes.length ? publicKeyBytes : new Uint8Array(); |
| 933 | |
| 934 | const seed = crypto.getRandomValues(new Uint8Array(32)); |
| 935 | const keyPair = x25519.generateKeyPair(seed); |
| 936 | const shared = x25519.sharedKey(keyPair.private, remotePubkey); |
| 937 | |
| 938 | const importedShared = await crypto.subtle.importKey( |
| 939 | 'raw', |
| 940 | shared, |
| 941 | { name: 'AES-GCM', length: 256 }, |
| 942 | true, |
| 943 | ['encrypt'], |
| 944 | ); |
| 945 | const iv = crypto.getRandomValues(new Uint8Array(12)); |
| 946 | const encrypted = await crypto.subtle.encrypt( |
| 947 | { name: 'AES-GCM', iv }, |
| 948 | importedShared, |
| 949 | new TextEncoder().encode(envsJson), |
| 950 | ); |
| 951 | |
| 952 | const result = new Uint8Array(iv.length + keyPair.public.byteLength + encrypted.byteLength); |
| 953 | result.set(keyPair.public, 0); |
| 954 | result.set(iv, keyPair.public.byteLength); |
| 955 | result.set(new Uint8Array(encrypted), keyPair.public.byteLength + iv.length); |
| 956 | |
| 957 | return result; |
| 958 | } |
| 959 | |
| 960 | async function createVm() { |
| 961 | try { |
no test coverage detected