@@ -933,6 +933,140 @@ async function decrypt(algorithm, key, data) {
933
933
return cipherOrWrap ( kWebCryptoCipherDecrypt , algorithm , key , data , 'decrypt' ) ;
934
934
}
935
935
936
+ async function encapsulateBits ( encapsulationAlgorithm , encapsulationKey ) {
937
+ emitExperimentalWarning ( 'The encapsulateBits Web Crypto API method' ) ;
938
+ if ( this !== subtle ) throw new ERR_INVALID_THIS ( 'SubtleCrypto' ) ;
939
+
940
+ webidl ??= require ( 'internal/crypto/webidl' ) ;
941
+ const prefix = "Failed to execute 'encapsulateBits' on 'SubtleCrypto'" ;
942
+ webidl . requiredArguments ( arguments . length , 2 , { prefix } ) ;
943
+ encapsulationAlgorithm = webidl . converters . AlgorithmIdentifier ( encapsulationAlgorithm , {
944
+ prefix,
945
+ context : '1st argument' ,
946
+ } ) ;
947
+ encapsulationKey = webidl . converters . CryptoKey ( encapsulationKey , {
948
+ prefix,
949
+ context : '2nd argument' ,
950
+ } ) ;
951
+
952
+ // eslint-disable-next-line no-unused-vars
953
+ const normalizedEncapsulationAlgorithm = normalizeAlgorithm ( encapsulationAlgorithm , 'encapsulate' ) ;
954
+
955
+ // It is not possible to get here just yet
956
+ const assert = require ( 'internal/assert' ) ;
957
+ assert . fail ( 'Unreachable code' ) ;
958
+ }
959
+
960
+ async function encapsulateKey ( encapsulationAlgorithm , encapsulationKey , sharedKeyAlgorithm , extractable , usages ) {
961
+ emitExperimentalWarning ( 'The encapsulateKey Web Crypto API method' ) ;
962
+ if ( this !== subtle ) throw new ERR_INVALID_THIS ( 'SubtleCrypto' ) ;
963
+
964
+ webidl ??= require ( 'internal/crypto/webidl' ) ;
965
+ const prefix = "Failed to execute 'encapsulateKey' on 'SubtleCrypto'" ;
966
+ webidl . requiredArguments ( arguments . length , 5 , { prefix } ) ;
967
+ encapsulationAlgorithm = webidl . converters . AlgorithmIdentifier ( encapsulationAlgorithm , {
968
+ prefix,
969
+ context : '1st argument' ,
970
+ } ) ;
971
+ encapsulationKey = webidl . converters . CryptoKey ( encapsulationKey , {
972
+ prefix,
973
+ context : '2nd argument' ,
974
+ } ) ;
975
+ sharedKeyAlgorithm = webidl . converters . AlgorithmIdentifier ( sharedKeyAlgorithm , {
976
+ prefix,
977
+ context : '3rd argument' ,
978
+ } ) ;
979
+ extractable = webidl . converters . boolean ( extractable , {
980
+ prefix,
981
+ context : '4th argument' ,
982
+ } ) ;
983
+ usages = webidl . converters [ 'sequence<KeyUsage>' ] ( usages , {
984
+ prefix,
985
+ context : '5th argument' ,
986
+ } ) ;
987
+
988
+ // eslint-disable-next-line no-unused-vars
989
+ const normalizedEncapsulationAlgorithm = normalizeAlgorithm ( encapsulationAlgorithm , 'encapsulate' ) ;
990
+ // eslint-disable-next-line no-unused-vars
991
+ const normalizedSharedKeyAlgorithm = normalizeAlgorithm ( sharedKeyAlgorithm , 'importKey' ) ;
992
+
993
+ // It is not possible to get here just yet
994
+ const assert = require ( 'internal/assert' ) ;
995
+ assert . fail ( 'Unreachable code' ) ;
996
+ }
997
+
998
+ async function decapsulateBits ( decapsulationAlgorithm , decapsulationKey , ciphertext ) {
999
+ emitExperimentalWarning ( 'The decapsulateBits Web Crypto API method' ) ;
1000
+ if ( this !== subtle ) throw new ERR_INVALID_THIS ( 'SubtleCrypto' ) ;
1001
+
1002
+ webidl ??= require ( 'internal/crypto/webidl' ) ;
1003
+ const prefix = "Failed to execute 'decapsulateBits' on 'SubtleCrypto'" ;
1004
+ webidl . requiredArguments ( arguments . length , 3 , { prefix } ) ;
1005
+ decapsulationAlgorithm = webidl . converters . AlgorithmIdentifier ( decapsulationAlgorithm , {
1006
+ prefix,
1007
+ context : '1st argument' ,
1008
+ } ) ;
1009
+ decapsulationKey = webidl . converters . CryptoKey ( decapsulationKey , {
1010
+ prefix,
1011
+ context : '2nd argument' ,
1012
+ } ) ;
1013
+ ciphertext = webidl . converters . BufferSource ( ciphertext , {
1014
+ prefix,
1015
+ context : '3rd argument' ,
1016
+ } ) ;
1017
+
1018
+ // eslint-disable-next-line no-unused-vars
1019
+ const normalizedDecapsulationAlgorithm = normalizeAlgorithm ( decapsulationAlgorithm , 'decapsulate' ) ;
1020
+
1021
+ // It is not possible to get here just yet
1022
+ const assert = require ( 'internal/assert' ) ;
1023
+ assert . fail ( 'Unreachable code' ) ;
1024
+ }
1025
+
1026
+ async function decapsulateKey (
1027
+ decapsulationAlgorithm , decapsulationKey , ciphertext , sharedKeyAlgorithm , extractable , usages ,
1028
+ ) {
1029
+ emitExperimentalWarning ( 'The decapsulateKey Web Crypto API method' ) ;
1030
+ if ( this !== subtle ) throw new ERR_INVALID_THIS ( 'SubtleCrypto' ) ;
1031
+
1032
+ webidl ??= require ( 'internal/crypto/webidl' ) ;
1033
+ const prefix = "Failed to execute 'decapsulateKey' on 'SubtleCrypto'" ;
1034
+ webidl . requiredArguments ( arguments . length , 6 , { prefix } ) ;
1035
+ decapsulationAlgorithm = webidl . converters . AlgorithmIdentifier ( decapsulationAlgorithm , {
1036
+ prefix,
1037
+ context : '1st argument' ,
1038
+ } ) ;
1039
+ decapsulationKey = webidl . converters . CryptoKey ( decapsulationKey , {
1040
+ prefix,
1041
+ context : '2nd argument' ,
1042
+ } ) ;
1043
+ ciphertext = webidl . converters . BufferSource ( ciphertext , {
1044
+ prefix,
1045
+ context : '3rd argument' ,
1046
+ } ) ;
1047
+ sharedKeyAlgorithm = webidl . converters . AlgorithmIdentifier ( sharedKeyAlgorithm , {
1048
+ prefix,
1049
+ context : '4th argument' ,
1050
+ } ) ;
1051
+ extractable = webidl . converters . boolean ( extractable , {
1052
+ prefix,
1053
+ context : '5th argument' ,
1054
+ } ) ;
1055
+ usages = webidl . converters [ 'sequence<KeyUsage>' ] ( usages , {
1056
+ prefix,
1057
+ context : '6th argument' ,
1058
+ } ) ;
1059
+
1060
+ // eslint-disable-next-line no-unused-vars
1061
+ const normalizedDecapsulationAlgorithm = normalizeAlgorithm ( decapsulationAlgorithm , 'decapsulate' ) ;
1062
+ // eslint-disable-next-line no-unused-vars
1063
+ const normalizedSharedKeyAlgorithm = normalizeAlgorithm ( sharedKeyAlgorithm , 'importKey' ) ;
1064
+
1065
+ // It is not possible to get here just yet
1066
+ const assert = require ( 'internal/assert' ) ;
1067
+ assert . fail ( 'Unreachable code' ) ;
1068
+ }
1069
+
936
1070
// The SubtleCrypto and Crypto classes are defined as part of the
937
1071
// Web Crypto API standard: https://www.w3.org/TR/WebCryptoAPI/
938
1072
@@ -958,18 +1092,22 @@ class SubtleCrypto {
958
1092
} ) ;
959
1093
960
1094
switch ( operation ) {
961
- case 'encrypt' :
1095
+ case 'decapsulateBits' :
1096
+ case 'decapsulateKey' :
962
1097
case 'decrypt' :
963
- case 'sign ' :
964
- case 'verify ' :
1098
+ case 'deriveBits ' :
1099
+ case 'deriveKey ' :
965
1100
case 'digest' :
1101
+ case 'encapsulateBits' :
1102
+ case 'encapsulateKey' :
1103
+ case 'encrypt' :
1104
+ case 'exportKey' :
966
1105
case 'generateKey' :
967
- case 'deriveKey' :
968
- case 'deriveBits' :
969
1106
case 'importKey' :
970
- case 'exportKey' :
971
- case 'wrapKey' :
1107
+ case 'sign' :
972
1108
case 'unwrapKey' :
1109
+ case 'verify' :
1110
+ case 'wrapKey' :
973
1111
break ;
974
1112
default :
975
1113
return false ;
@@ -1003,7 +1141,7 @@ class SubtleCrypto {
1003
1141
if ( ! check ( 'exportKey' , additionalAlgorithm ) ) {
1004
1142
return false ;
1005
1143
}
1006
- } else if ( operation === 'unwrapKey' ) {
1144
+ } else if ( operation === 'unwrapKey' || operation === 'encapsulateKey' || operation === 'decapsulateKey' ) {
1007
1145
additionalAlgorithm = webidl . converters . AlgorithmIdentifier ( lengthOrAdditionalAlgorithm , {
1008
1146
prefix,
1009
1147
context : '3rd argument' ,
@@ -1027,6 +1165,14 @@ class SubtleCrypto {
1027
1165
}
1028
1166
1029
1167
function check ( op , alg , length ) {
1168
+ if ( op === 'encapsulateBits' || op === 'encapsulateKey' ) {
1169
+ op = 'encapsulate' ;
1170
+ }
1171
+
1172
+ if ( op === 'decapsulateBits' || op === 'decapsulateKey' ) {
1173
+ op = 'decapsulate' ;
1174
+ }
1175
+
1030
1176
let normalizedAlgorithm ;
1031
1177
try {
1032
1178
normalizedAlgorithm = normalizeAlgorithm ( alg , op ) ;
@@ -1043,16 +1189,18 @@ function check(op, alg, length) {
1043
1189
}
1044
1190
1045
1191
switch ( op ) {
1046
- case 'encrypt ' :
1192
+ case 'decapsulate ' :
1047
1193
case 'decrypt' :
1048
- case 'sign' :
1049
- case 'verify' :
1050
1194
case 'digest' :
1195
+ case 'encapsulate' :
1196
+ case 'encrypt' :
1197
+ case 'exportKey' :
1051
1198
case 'generateKey' :
1052
1199
case 'importKey' :
1053
- case 'exportKey' :
1054
- case 'wrapKey' :
1200
+ case 'sign' :
1055
1201
case 'unwrapKey' :
1202
+ case 'verify' :
1203
+ case 'wrapKey' :
1056
1204
return true ;
1057
1205
case 'deriveBits' : {
1058
1206
if ( normalizedAlgorithm . name === 'HKDF' ) {
@@ -1230,6 +1378,34 @@ ObjectDefineProperties(
1230
1378
writable : true ,
1231
1379
value : unwrapKey ,
1232
1380
} ,
1381
+ encapsulateBits : {
1382
+ __proto__ : null ,
1383
+ enumerable : true ,
1384
+ configurable : true ,
1385
+ writable : true ,
1386
+ value : encapsulateBits ,
1387
+ } ,
1388
+ encapsulateKey : {
1389
+ __proto__ : null ,
1390
+ enumerable : true ,
1391
+ configurable : true ,
1392
+ writable : true ,
1393
+ value : encapsulateKey ,
1394
+ } ,
1395
+ decapsulateBits : {
1396
+ __proto__ : null ,
1397
+ enumerable : true ,
1398
+ configurable : true ,
1399
+ writable : true ,
1400
+ value : decapsulateBits ,
1401
+ } ,
1402
+ decapsulateKey : {
1403
+ __proto__ : null ,
1404
+ enumerable : true ,
1405
+ configurable : true ,
1406
+ writable : true ,
1407
+ value : decapsulateKey ,
1408
+ } ,
1233
1409
} ) ;
1234
1410
1235
1411
module . exports = {
0 commit comments