@@ -933,6 +933,140 @@ async function decrypt(algorithm, key, data) {
933933 return cipherOrWrap ( kWebCryptoCipherDecrypt , algorithm , key , data , 'decrypt' ) ;
934934}
935935
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+
9361070// The SubtleCrypto and Crypto classes are defined as part of the
9371071// Web Crypto API standard: https://www.w3.org/TR/WebCryptoAPI/
9381072
@@ -958,18 +1092,22 @@ class SubtleCrypto {
9581092 } ) ;
9591093
9601094 switch ( operation ) {
961- case 'encrypt' :
1095+ case 'decapsulateBits' :
1096+ case 'decapsulateKey' :
9621097 case 'decrypt' :
963- case 'sign ' :
964- case 'verify ' :
1098+ case 'deriveBits ' :
1099+ case 'deriveKey ' :
9651100 case 'digest' :
1101+ case 'encapsulateBits' :
1102+ case 'encapsulateKey' :
1103+ case 'encrypt' :
1104+ case 'exportKey' :
9661105 case 'generateKey' :
967- case 'deriveKey' :
968- case 'deriveBits' :
9691106 case 'importKey' :
970- case 'exportKey' :
971- case 'wrapKey' :
1107+ case 'sign' :
9721108 case 'unwrapKey' :
1109+ case 'verify' :
1110+ case 'wrapKey' :
9731111 break ;
9741112 default :
9751113 return false ;
@@ -1003,7 +1141,7 @@ class SubtleCrypto {
10031141 if ( ! check ( 'exportKey' , additionalAlgorithm ) ) {
10041142 return false ;
10051143 }
1006- } else if ( operation === 'unwrapKey' ) {
1144+ } else if ( operation === 'unwrapKey' || operation === 'encapsulateKey' || operation === 'decapsulateKey' ) {
10071145 additionalAlgorithm = webidl . converters . AlgorithmIdentifier ( lengthOrAdditionalAlgorithm , {
10081146 prefix,
10091147 context : '3rd argument' ,
@@ -1027,6 +1165,14 @@ class SubtleCrypto {
10271165}
10281166
10291167function 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+
10301176 let normalizedAlgorithm ;
10311177 try {
10321178 normalizedAlgorithm = normalizeAlgorithm ( alg , op ) ;
@@ -1043,16 +1189,18 @@ function check(op, alg, length) {
10431189 }
10441190
10451191 switch ( op ) {
1046- case 'encrypt ' :
1192+ case 'decapsulate ' :
10471193 case 'decrypt' :
1048- case 'sign' :
1049- case 'verify' :
10501194 case 'digest' :
1195+ case 'encapsulate' :
1196+ case 'encrypt' :
1197+ case 'exportKey' :
10511198 case 'generateKey' :
10521199 case 'importKey' :
1053- case 'exportKey' :
1054- case 'wrapKey' :
1200+ case 'sign' :
10551201 case 'unwrapKey' :
1202+ case 'verify' :
1203+ case 'wrapKey' :
10561204 return true ;
10571205 case 'deriveBits' : {
10581206 if ( normalizedAlgorithm . name === 'HKDF' ) {
@@ -1230,6 +1378,34 @@ ObjectDefineProperties(
12301378 writable : true ,
12311379 value : unwrapKey ,
12321380 } ,
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+ } ,
12331409 } ) ;
12341410
12351411module . exports = {
0 commit comments