@@ -18,6 +18,7 @@ import {
18
18
import { KasPublicKeyInfo } from '../access.js' ;
19
19
import { computeECDSASig , extractRSValuesFromSignature } from '../nanotdf-crypto/ecdsaSignature.js' ;
20
20
import { ConfigurationError } from '../errors.js' ;
21
+ import PolicyType from './enum/PolicyTypeEnum.js' ;
21
22
22
23
/**
23
24
* Encrypt the plain data into nanotdf buffer
@@ -28,14 +29,16 @@ import { ConfigurationError } from '../errors.js';
28
29
* @param iv
29
30
* @param data The data to be encrypted
30
31
* @param ecdsaBinding Flag to enable ECDSA binding
32
+ * @param policyType Policy type to use for the nanotdf
31
33
*/
32
34
export default async function encrypt (
33
35
policy : string ,
34
36
kasInfo : KasPublicKeyInfo ,
35
37
ephemeralKeyPair : CryptoKeyPair ,
36
38
iv : Uint8Array ,
37
39
data : string | ArrayBufferLike ,
38
- ecdsaBinding : boolean = DefaultParams . ecdsaBinding
40
+ ecdsaBinding : boolean = DefaultParams . ecdsaBinding ,
41
+ policyType ?: PolicyType
39
42
) : Promise < ArrayBuffer > {
40
43
// Generate a symmetric key.
41
44
if ( ! ephemeralKeyPair . privateKey ) {
@@ -54,23 +57,32 @@ export default async function encrypt(
54
57
// Auth tag length for policy and payload
55
58
const authTagLengthInBytes = authTagLengthForCipher ( DefaultParams . symmetricCipher ) / 8 ;
56
59
57
- // Encrypt the policy
58
- const policyIV = new Uint8Array ( iv . length ) . fill ( 0 ) ;
59
- const policyAsBuffer = new TextEncoder ( ) . encode ( policy ) ;
60
- const encryptedPolicy = await cryptoEncrypt (
61
- symmetricKey ,
62
- policyAsBuffer ,
63
- policyIV ,
64
- authTagLengthInBytes * 8
65
- ) ;
60
+ let policyContent : Uint8Array ;
61
+ if ( policyType === PolicyType . EmbeddedText ) {
62
+ // Store policy as plain text
63
+ policyContent = new TextEncoder ( ) . encode ( policy ) ;
64
+ } else {
65
+ // Encrypt the policy
66
+ const policyIV = new Uint8Array ( iv . length ) . fill ( 0 ) ;
67
+ const policyAsBuffer = new TextEncoder ( ) . encode ( policy ) ;
68
+ policyContent = new Uint8Array (
69
+ await cryptoEncrypt (
70
+ symmetricKey ,
71
+ policyAsBuffer ,
72
+ policyIV ,
73
+ authTagLengthInBytes * 8
74
+ )
75
+ ) ;
76
+ }
77
+
66
78
67
79
let policyBinding : Uint8Array ;
68
80
69
81
// Calculate the policy binding.
70
82
if ( ecdsaBinding ) {
71
83
const curveName = await getCurveNameFromPrivateKey ( ephemeralKeyPair . privateKey ) ;
72
84
const ecdsaPrivateKey = await convertECDHToECDSA ( ephemeralKeyPair . privateKey , curveName ) ;
73
- const ecdsaSignature = await computeECDSASig ( ecdsaPrivateKey , new Uint8Array ( encryptedPolicy ) ) ;
85
+ const ecdsaSignature = await computeECDSASig ( ecdsaPrivateKey , policyContent ) ;
74
86
const { r, s } = extractRSValuesFromSignature ( new Uint8Array ( ecdsaSignature ) ) ;
75
87
76
88
const rLength = r . length ;
@@ -84,15 +96,15 @@ export default async function encrypt(
84
96
policyBinding [ 1 + rLength ] = sLength ;
85
97
policyBinding . set ( s , 1 + rLength + 1 ) ;
86
98
} else {
87
- const signature = await digest ( 'SHA-256' , new Uint8Array ( encryptedPolicy ) ) ;
99
+ const signature = await digest ( 'SHA-256' , policyContent ) ;
88
100
policyBinding = new Uint8Array ( signature . slice ( - GMAC_BINDING_LEN ) ) ;
89
101
}
90
102
91
103
// Create embedded policy
92
104
const embeddedPolicy = new EmbeddedPolicy (
93
- DefaultParams . policyType ,
105
+ policyType ?? PolicyType . EmbeddedEncrypted ,
94
106
policyBinding ,
95
- new Uint8Array ( encryptedPolicy )
107
+ policyContent
96
108
) ;
97
109
98
110
if ( ! ephemeralKeyPair . publicKey ) {
0 commit comments