Skip to content

Commit 96570e0

Browse files
committed
CRYPTO Middleware Upgrade
In Progress do not run
1 parent 5f3e6b2 commit 96570e0

File tree

10 files changed

+215
-182
lines changed

10 files changed

+215
-182
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import {
2+
compactMapArray,
3+
hasValue,
4+
isArray,
5+
isNumber
6+
} from '@universalweb/acid';
7+
import { currentCertificateVersion, currentVersion } from '../../defaults.js';
8+
import { encryptionKeypairAlgorithm } from '../index';
9+
import { kyber768_xChaCha } from '../cipherSuite/Kyber768_xChaCha.js';
10+
import { setOptions } from '../utils.js';
11+
import { viatCipherSuite } from './viat.js';
12+
import { x25519_kyber768Half_xchacha20 } from '../cipherSuite/x25519_Kyber768Half_xChaCha.js';
13+
import { x25519_kyber768_xchacha20 } from './x25519_Kyber768_xChaCha.js';
14+
import { x25519_xChaCha } from '../cipherSuite/x25519_xChaCha.js';
15+
const cipherList = [
16+
x25519_xChaCha,
17+
x25519_kyber768Half_xchacha20,
18+
kyber768_xChaCha,
19+
x25519_kyber768_xchacha20,
20+
viatCipherSuite
21+
];
22+
export const cipherSuites = new Map();
23+
const cipherSuitesVersion1 = new Map();
24+
cipherSuites.set(currentVersion, cipherSuitesVersion1);
25+
cipherSuitesVersion1.set('all', cipherList);
26+
setOptions(cipherSuitesVersion1, cipherList);
27+
export function getEncryptionKeypairAlgorithm(algo = 0, version = currentCertificateVersion) {
28+
if (!hasValue(algo)) {
29+
return false;
30+
}
31+
const versionMap = encryptionKeypairAlgorithm.get(version);
32+
if (versionMap) {
33+
return versionMap.get(algo);
34+
}
35+
}
36+
export const cipherSuitesCertificates = new Map();
37+
const cipherSuitesCertificatesVersion1 = new Map();
38+
cipherSuitesCertificates.set(currentVersion, cipherSuitesCertificatesVersion1);
39+
cipherSuitesCertificatesVersion1.set('all', cipherList);
40+
setOptions(cipherSuitesCertificatesVersion1, cipherList);
41+
export function getCipherSuite(cipherSuiteName = 0, version = currentVersion) {
42+
if (!hasValue(cipherSuiteName)) {
43+
return false;
44+
}
45+
const versionMap = cipherSuites.get(version);
46+
if (versionMap) {
47+
return versionMap.get(cipherSuiteName);
48+
}
49+
}
50+
export function getCipherSuites(indexes, version = currentVersion) {
51+
if (indexes) {
52+
if (isNumber(indexes)) {
53+
return getCipherSuite(indexes, version);
54+
} else if (isArray(indexes)) {
55+
const cipherSuitesArray = compactMapArray(indexes, (value) => {
56+
const cipherSuite = getCipherSuite(value, version);
57+
if (cipherSuite) {
58+
return cipherSuite;
59+
}
60+
});
61+
return cipherSuitesArray;
62+
}
63+
}
64+
return getCipherSuite('all', version);
65+
}

cryptoMiddleware/cipherSuite/viat.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const viatCipherSuite = {
88
name: 'viatCipherSuite',
99
alias: 'x25519_kyber768_xchacha20_dilithium65_sphincs+',
1010
description: 'Crystals-Kyber768 with XChaCha20 and SHAKE256.',
11-
id: 2,
11+
id: 4,
1212
preferred: true,
1313
speed: 0,
1414
security: 1,
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { currentVersion } from '../../defaults.js';
2+
import { hasValue } from '@universalweb/acid';
3+
import { setOptions } from '../utils.js';
4+
import { xChaCha } from './xChaCha.js';
5+
const cipherList = [xChaCha];
6+
export const encryptionAlgorithms = new Map();
7+
const encryptionAlgorithmsVersion1 = new Map();
8+
encryptionAlgorithms.set(1, encryptionAlgorithmsVersion1);
9+
setOptions(encryptionAlgorithmsVersion1, cipherList);
10+
export function getEncryptionAlgorithm(encryptionAlgorithmName = 0, version = currentVersion) {
11+
if (!hasValue(encryptionAlgorithmName)) {
12+
return false;
13+
}
14+
const algoVersion = encryptionAlgorithms.get(version);
15+
if (algoVersion) {
16+
return algoVersion.get(encryptionAlgorithmName);
17+
}
18+
}

cryptoMiddleware/hash/index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { blake3 } from './blake3.js';
2+
import { currentVersion } from '../../defaults.js';
3+
import { hasValue } from '@universalweb/acid';
4+
import { setOptions } from '../utils.js';
5+
const cipherList = [blake3];
6+
export const hashAlgorithms = new Map();
7+
const hashAlgorithmsVersion1 = new Map();
8+
hashAlgorithms.set(1, hashAlgorithmsVersion1);
9+
setOptions(hashAlgorithmsVersion1, cipherList);
10+
export function getHashAlgorithm(hashAlgorithmName = 0, version = currentVersion) {
11+
if (!hasValue(hashAlgorithmName)) {
12+
return false;
13+
}
14+
const algoVersion = hashAlgorithms.get(version);
15+
if (algoVersion) {
16+
return algoVersion.get(hashAlgorithmName);
17+
}
18+
}

cryptoMiddleware/index.js

Lines changed: 5 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -1,169 +1,5 @@
1-
import * as defaultCrypto from '#crypto';
2-
import {
3-
assign,
4-
clearBuffer,
5-
compactMapArray,
6-
eachArray,
7-
hasValue,
8-
isArray,
9-
isNumber,
10-
isUndefined
11-
} from '@universalweb/acid';
12-
import { currentCertificateVersion, currentVersion } from '../defaults.js';
13-
import { blake3 } from './hash/blake3.js';
14-
import { dilithium44 } from './signature/dilithium44.js';
15-
import { dilithium44_ed25519 } from './signature/dilithium44_ed25519.js';
16-
import { dilithium65 } from './signature/dilithium65.js';
17-
import { dilithium87 } from './signature/dilithium87.js';
18-
import { ed25519 } from './signature/ed25519.js';
19-
import { kyber768 } from './keyExchange/kyber768.js';
20-
import { kyber768Half_x25519 } from './keyExchange/kyber768Half_x25519.js';
21-
import { kyber768_x25519 } from './keyExchange/kyber768_x25519.js';
22-
import { kyber768_xChaCha } from './cipherSuite/Kyber768_xChaCha.js';
23-
import { x25519 } from './keyExchange/x25519_blake3.js';
24-
import { x25519_kyber768Half_xchacha20 } from './cipherSuite/x25519_Kyber768Half_xChaCha.js';
25-
import { x25519_xChaCha } from './cipherSuite/x25519_xChaCha.js';
26-
function setOption(source, option) {
27-
const {
28-
id, name: cipherName, alias
29-
} = option;
30-
if (hasValue(cipherName)) {
31-
source.set(cipherName, option);
32-
}
33-
if (hasValue(id)) {
34-
source.set(id, option);
35-
}
36-
if (hasValue(alias)) {
37-
source.set(alias, option);
38-
}
39-
}
40-
export const cipherSuites = new Map();
41-
const cipherSuitesVersion1 = new Map();
42-
cipherSuites.set(currentVersion, cipherSuitesVersion1);
43-
cipherSuitesVersion1.set('all', [
44-
x25519_xChaCha,
45-
x25519_kyber768Half_xchacha20,
46-
kyber768_xChaCha
47-
]);
48-
setOption(cipherSuitesVersion1, x25519_xChaCha);
49-
setOption(cipherSuitesVersion1, x25519_kyber768Half_xchacha20);
50-
setOption(cipherSuitesVersion1, kyber768_xChaCha);
51-
export const encryptionKeypairAlgorithm = new Map();
52-
const encryptionKeypairAlgorithmVersion1 = new Map();
53-
encryptionKeypairAlgorithm.set(currentVersion, encryptionKeypairAlgorithmVersion1);
54-
encryptionKeypairAlgorithm.set('all', [
55-
kyber768Half_x25519,
56-
kyber768_x25519,
57-
kyber768,
58-
x25519,
59-
]);
60-
setOption(encryptionKeypairAlgorithmVersion1, kyber768);
61-
setOption(encryptionKeypairAlgorithmVersion1, x25519);
62-
setOption(encryptionKeypairAlgorithmVersion1, kyber768Half_x25519);
63-
setOption(encryptionKeypairAlgorithmVersion1, kyber768_x25519);
64-
export function getEncryptionKeypairAlgorithm(algo = 0, version = currentCertificateVersion) {
65-
if (!hasValue(algo)) {
66-
return false;
67-
}
68-
const versionMap = encryptionKeypairAlgorithm.get(version);
69-
if (versionMap) {
70-
return versionMap.get(algo);
71-
}
72-
}
73-
export const cipherSuitesCertificates = new Map();
74-
const cipherSuitesCertificatesVersion1 = new Map();
75-
cipherSuitesCertificates.set(currentVersion, cipherSuitesCertificatesVersion1);
76-
cipherSuitesCertificatesVersion1.set('all', [
77-
x25519_xChaCha,
78-
x25519_kyber768Half_xchacha20,
79-
kyber768_xChaCha
80-
]);
81-
setOption(cipherSuitesCertificatesVersion1, x25519_xChaCha);
82-
setOption(cipherSuitesCertificatesVersion1, x25519_kyber768Half_xchacha20);
83-
setOption(cipherSuitesCertificatesVersion1, kyber768_xChaCha);
84-
export function getCipherSuite(cipherSuiteName = 0, version = currentVersion) {
85-
if (!hasValue(cipherSuiteName)) {
86-
return false;
87-
}
88-
const versionMap = cipherSuites.get(version);
89-
if (versionMap) {
90-
return versionMap.get(cipherSuiteName);
91-
}
92-
}
93-
export function getCipherSuites(indexes, version = currentVersion) {
94-
if (indexes) {
95-
if (isNumber(indexes)) {
96-
return getCipherSuite(indexes, version);
97-
} else if (isArray(indexes)) {
98-
const cipherSuitesArray = compactMapArray(indexes, (value) => {
99-
const cipherSuite = getCipherSuite(value, version);
100-
if (cipherSuite) {
101-
return cipherSuite;
102-
}
103-
});
104-
return cipherSuitesArray;
105-
}
106-
}
107-
return getCipherSuite('all', version);
108-
}
109-
export const publicKeyAlgorithms = new Map();
110-
const publicKeyAlgorithmVersion1 = new Map();
111-
publicKeyAlgorithms.set(1, publicKeyAlgorithmVersion1);
112-
publicKeyAlgorithmVersion1.set('all', [
113-
ed25519,
114-
dilithium44_ed25519,
115-
dilithium44,
116-
dilithium65,
117-
dilithium87
118-
]);
119-
setOption(publicKeyAlgorithmVersion1, ed25519);
120-
setOption(publicKeyAlgorithmVersion1, dilithium44_ed25519);
121-
setOption(publicKeyAlgorithmVersion1, dilithium44);
122-
setOption(publicKeyAlgorithmVersion1, dilithium65);
123-
setOption(publicKeyAlgorithmVersion1, dilithium87);
124-
export function getSignatureAlgorithm(publicKeyAlgorithmName = 0, version = currentVersion) {
125-
if (!hasValue(publicKeyAlgorithmName)) {
126-
return false;
127-
}
128-
const versionMap = publicKeyAlgorithms.get(version);
129-
if (versionMap) {
130-
return versionMap.get(publicKeyAlgorithmName);
131-
}
132-
}
133-
export const publicKeyCertificateAlgorithms = new Map();
134-
const publicKeyCertificateAlgorithmsVersion1 = new Map();
135-
publicKeyCertificateAlgorithms.set(currentVersion, publicKeyCertificateAlgorithmsVersion1);
136-
publicKeyCertificateAlgorithmsVersion1.set('all', [
137-
ed25519,
138-
dilithium44_ed25519,
139-
dilithium44,
140-
dilithium65,
141-
dilithium87
142-
]);
143-
setOption(publicKeyCertificateAlgorithmsVersion1, ed25519);
144-
setOption(publicKeyCertificateAlgorithmsVersion1, dilithium44_ed25519);
145-
setOption(publicKeyCertificateAlgorithmsVersion1, dilithium44);
146-
setOption(publicKeyCertificateAlgorithmsVersion1, dilithium65);
147-
setOption(publicKeyCertificateAlgorithmsVersion1, dilithium87);
148-
export function getSignatureAlgorithmByCertificate(publicKeyAlgorithmName = 0, version = currentCertificateVersion) {
149-
if (!hasValue(publicKeyAlgorithmName)) {
150-
return false;
151-
}
152-
const algoVersion = publicKeyCertificateAlgorithms.get(version);
153-
if (algoVersion) {
154-
return algoVersion.get(publicKeyAlgorithmName);
155-
}
156-
}
157-
export const hashAlgorithms = new Map();
158-
const hashAlgorithmsVersion1 = new Map();
159-
hashAlgorithms.set(1, hashAlgorithmsVersion1);
160-
setOption(hashAlgorithmsVersion1, blake3);
161-
export function getHashAlgorithm(hashAlgorithmName = 0, version = currentVersion) {
162-
if (!hasValue(hashAlgorithmName)) {
163-
return false;
164-
}
165-
const algoVersion = hashAlgorithms.get(version);
166-
if (algoVersion) {
167-
return algoVersion.get(hashAlgorithmName);
168-
}
169-
}
1+
export * from './signature/index.js';
2+
export * from './hash/index.js';
3+
export * from './keyExchange/index.js';
4+
export * from './encryption/index.js';
5+
export * from './cipherSuite/index.js';
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { currentCertificateVersion, currentVersion } from '../../defaults.js';
2+
import { hasValue } from '@universalweb/acid';
3+
import { kyber768 } from './kyber768.js';
4+
import { kyber768Half_x25519 } from './kyber768Half_x25519.js';
5+
import { kyber768_x25519 } from './kyber768_x25519.js';
6+
import { setOptions } from '../utils.js';
7+
import { x25519 } from './x25519.js';
8+
import { x25519_blake3 } from './x25519_blake3.js';
9+
const cipherList = [
10+
kyber768Half_x25519,
11+
kyber768_x25519,
12+
kyber768,
13+
x25519,
14+
x25519_blake3
15+
];
16+
export const encryptionKeypairAlgorithm = new Map();
17+
const encryptionKeypairAlgorithmVersion1 = new Map();
18+
encryptionKeypairAlgorithm.set(currentVersion, encryptionKeypairAlgorithmVersion1);
19+
encryptionKeypairAlgorithmVersion1.set('all', cipherList);
20+
setOptions(encryptionKeypairAlgorithmVersion1, cipherList);
21+
export function getEncryptionKeypairAlgorithm(algo = 0, version = currentCertificateVersion) {
22+
if (!hasValue(algo)) {
23+
return false;
24+
}
25+
const versionMap = encryptionKeypairAlgorithm.get(version);
26+
if (versionMap) {
27+
return versionMap.get(algo);
28+
}
29+
}

cryptoMiddleware/signature/ed25519_deprecated.js renamed to cryptoMiddleware/signature/ed25519_sodium.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ export function getPublicKeyFromPrivateKey(privateKey) {
8989
crypto_sign_ed25519_sk_to_pk(publicKey, privateKey);
9090
return publicKey;
9191
}
92-
export const ed25519 = {
93-
name: 'ed25519',
94-
alias: 'default',
95-
id: 0,
92+
export const ed25519_sodium = {
93+
name: 'ed25519_sodium',
94+
alias: 'ed25519_sodium',
95+
id: 6,
9696
publicKeySize,
9797
privateKeySize,
9898
signatureSize,
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { currentCertificateVersion, currentVersion } from '../../defaults.js';
2+
import { dilithium44 } from './dilithium44.js';
3+
import { dilithium44_ed25519 } from './dilithium44_ed25519.js';
4+
import { dilithium65 } from './dilithium65.js';
5+
import { dilithium87 } from './dilithium87.js';
6+
import { ed25519 } from './ed25519.js';
7+
import { ed25519_sodium } from './ed25519_sodium.js';
8+
import { hasValue } from '@universalweb/acid';
9+
import { setOptions } from '../utils.js';
10+
import { sphincs192 } from './sphincs192.js';
11+
const cipherList = [
12+
ed25519,
13+
dilithium44_ed25519,
14+
dilithium44,
15+
dilithium65,
16+
dilithium87,
17+
sphincs192,
18+
ed25519_sodium
19+
];
20+
export const publicKeyAlgorithms = new Map();
21+
const publicKeyAlgorithmVersion1 = new Map();
22+
publicKeyAlgorithms.set(1, publicKeyAlgorithmVersion1);
23+
publicKeyAlgorithmVersion1.set('all', cipherList);
24+
setOptions(publicKeyAlgorithmVersion1, cipherList);
25+
export function getSignatureAlgorithm(publicKeyAlgorithmName = 0, version = currentVersion) {
26+
if (!hasValue(publicKeyAlgorithmName)) {
27+
return false;
28+
}
29+
const versionMap = publicKeyAlgorithms.get(version);
30+
if (versionMap) {
31+
return versionMap.get(publicKeyAlgorithmName);
32+
}
33+
}
34+
export const publicKeyCertificateAlgorithms = new Map();
35+
const publicKeyCertificateAlgorithmsVersion1 = new Map();
36+
publicKeyCertificateAlgorithms.set(currentVersion, publicKeyCertificateAlgorithmsVersion1);
37+
publicKeyCertificateAlgorithmsVersion1.set('all', cipherList);
38+
setOptions(publicKeyCertificateAlgorithmsVersion1, cipherList);
39+
export function getSignatureAlgorithmByCertificate(publicKeyAlgorithmName = 0, version = currentCertificateVersion) {
40+
if (!hasValue(publicKeyAlgorithmName)) {
41+
return false;
42+
}
43+
const algoVersion = publicKeyCertificateAlgorithms.get(version);
44+
if (algoVersion) {
45+
return algoVersion.get(publicKeyAlgorithmName);
46+
}
47+
}

0 commit comments

Comments
 (0)