@@ -18,6 +18,7 @@ import {
1818import { KasPublicKeyInfo } from '../access.js' ;
1919import { computeECDSASig , extractRSValuesFromSignature } from '../nanotdf-crypto/ecdsaSignature.js' ;
2020import { ConfigurationError } from '../errors.js' ;
21+ import PolicyType from './enum/PolicyTypeEnum.js' ;
2122
2223/**
2324 * Encrypt the plain data into nanotdf buffer
@@ -28,14 +29,16 @@ import { ConfigurationError } from '../errors.js';
2829 * @param iv
2930 * @param data The data to be encrypted
3031 * @param ecdsaBinding Flag to enable ECDSA binding
32+ * @param policyType Policy type to use for the nanotdf
3133 */
3234export default async function encrypt (
3335 policy : string ,
3436 kasInfo : KasPublicKeyInfo ,
3537 ephemeralKeyPair : CryptoKeyPair ,
3638 iv : Uint8Array ,
3739 data : string | ArrayBufferLike ,
38- ecdsaBinding : boolean = DefaultParams . ecdsaBinding
40+ ecdsaBinding : boolean = DefaultParams . ecdsaBinding ,
41+ policyType ?: PolicyType
3942) : Promise < ArrayBuffer > {
4043 // Generate a symmetric key.
4144 if ( ! ephemeralKeyPair . privateKey ) {
@@ -54,23 +57,32 @@ export default async function encrypt(
5457 // Auth tag length for policy and payload
5558 const authTagLengthInBytes = authTagLengthForCipher ( DefaultParams . symmetricCipher ) / 8 ;
5659
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+
6678
6779 let policyBinding : Uint8Array ;
6880
6981 // Calculate the policy binding.
7082 if ( ecdsaBinding ) {
7183 const curveName = await getCurveNameFromPrivateKey ( ephemeralKeyPair . privateKey ) ;
7284 const ecdsaPrivateKey = await convertECDHToECDSA ( ephemeralKeyPair . privateKey , curveName ) ;
73- const ecdsaSignature = await computeECDSASig ( ecdsaPrivateKey , new Uint8Array ( encryptedPolicy ) ) ;
85+ const ecdsaSignature = await computeECDSASig ( ecdsaPrivateKey , policyContent ) ;
7486 const { r, s } = extractRSValuesFromSignature ( new Uint8Array ( ecdsaSignature ) ) ;
7587
7688 const rLength = r . length ;
@@ -84,15 +96,15 @@ export default async function encrypt(
8496 policyBinding [ 1 + rLength ] = sLength ;
8597 policyBinding . set ( s , 1 + rLength + 1 ) ;
8698 } else {
87- const signature = await digest ( 'SHA-256' , new Uint8Array ( encryptedPolicy ) ) ;
99+ const signature = await digest ( 'SHA-256' , policyContent ) ;
88100 policyBinding = new Uint8Array ( signature . slice ( - GMAC_BINDING_LEN ) ) ;
89101 }
90102
91103 // Create embedded policy
92104 const embeddedPolicy = new EmbeddedPolicy (
93- DefaultParams . policyType ,
105+ policyType ?? PolicyType . EmbeddedEncrypted ,
94106 policyBinding ,
95- new Uint8Array ( encryptedPolicy )
107+ policyContent
96108 ) ;
97109
98110 if ( ! ephemeralKeyPair . publicKey ) {
0 commit comments