11import { Buffer } from "node:buffer" ;
22import crypto , { CipherGCMTypes } from "node:crypto" ;
33import { JsonParser , JsonSerializer } from "@peculiar/json-schema" ;
4+ import {
5+ assertBufferSource , toArrayBuffer , toUint8Array ,
6+ } from "@peculiar/utils" ;
47import * as core from "webcrypto-core" ;
58import { CryptoKey } from "../../keys" ;
69import { AesCryptoKey } from "./key" ;
@@ -27,7 +30,7 @@ export class AesCrypto {
2730 case "jwk" :
2831 return JsonSerializer . toJSON ( key ) ;
2932 case "raw" :
30- return new Uint8Array ( key . data ) . buffer ;
33+ return toArrayBuffer ( key . data ) ;
3134 default :
3235 throw new core . OperationError ( "format: Must be 'jwk' or 'raw'" ) ;
3336 }
@@ -41,8 +44,9 @@ export class AesCrypto {
4144 key = JsonParser . fromJSON ( keyData , { targetSchema : AesCryptoKey } ) ;
4245 break ;
4346 case "raw" :
47+ assertBufferSource ( keyData ) ;
4448 key = new AesCryptoKey ( ) ;
45- key . data = Buffer . from ( keyData as ArrayBuffer ) ;
49+ key . data = Buffer . from ( toUint8Array ( keyData ) ) ;
4650 break ;
4751 default :
4852 throw new core . OperationError ( "format: Must be 'jwk' or 'raw'" ) ;
@@ -105,91 +109,91 @@ export class AesCrypto {
105109 }
106110
107111 public static async encryptAesCBC ( algorithm : AesCbcParams , key : AesCryptoKey , data : Buffer ) {
108- const cipher = crypto . createCipheriv ( `aes-${ key . algorithm . length } -cbc` , key . data , new Uint8Array ( algorithm . iv as ArrayBuffer ) ) ;
112+ const cipher = crypto . createCipheriv ( `aes-${ key . algorithm . length } -cbc` , key . data , toUint8Array ( algorithm . iv ) ) ;
109113 let enc = cipher . update ( data ) ;
110114 enc = Buffer . concat ( [ enc , cipher . final ( ) ] ) ;
111- const res = new Uint8Array ( enc ) . buffer ;
115+ const res = toArrayBuffer ( enc ) ;
112116 return res ;
113117 }
114118
115119 public static async decryptAesCBC ( algorithm : AesCbcParams , key : AesCryptoKey , data : Buffer ) {
116- const decipher = crypto . createDecipheriv ( `aes-${ key . algorithm . length } -cbc` , key . data , new Uint8Array ( algorithm . iv as ArrayBuffer ) ) ;
120+ const decipher = crypto . createDecipheriv ( `aes-${ key . algorithm . length } -cbc` , key . data , toUint8Array ( algorithm . iv ) ) ;
117121 let dec = decipher . update ( data ) ;
118122 dec = Buffer . concat ( [ dec , decipher . final ( ) ] ) ;
119- return new Uint8Array ( dec ) . buffer ;
123+ return toArrayBuffer ( dec ) ;
120124 }
121125
122126 public static async encryptAesCTR ( algorithm : AesCtrParams , key : AesCryptoKey , data : Buffer ) {
123- const cipher = crypto . createCipheriv ( `aes-${ key . algorithm . length } -ctr` , key . data , Buffer . from ( algorithm . counter as ArrayBuffer ) ) ;
127+ const cipher = crypto . createCipheriv ( `aes-${ key . algorithm . length } -ctr` , key . data , Buffer . from ( toUint8Array ( algorithm . counter ) ) ) ;
124128 let enc = cipher . update ( data ) ;
125129 enc = Buffer . concat ( [ enc , cipher . final ( ) ] ) ;
126- const res = new Uint8Array ( enc ) . buffer ;
130+ const res = toArrayBuffer ( enc ) ;
127131 return res ;
128132 }
129133
130134 public static async decryptAesCTR ( algorithm : AesCtrParams , key : AesCryptoKey , data : Buffer ) {
131- const decipher = crypto . createDecipheriv ( `aes-${ key . algorithm . length } -ctr` , key . data , new Uint8Array ( algorithm . counter as ArrayBuffer ) ) ;
135+ const decipher = crypto . createDecipheriv ( `aes-${ key . algorithm . length } -ctr` , key . data , toUint8Array ( algorithm . counter ) ) ;
132136 let dec = decipher . update ( data ) ;
133137 dec = Buffer . concat ( [ dec , decipher . final ( ) ] ) ;
134- return new Uint8Array ( dec ) . buffer ;
138+ return toArrayBuffer ( dec ) ;
135139 }
136140
137141 public static async encryptAesGCM ( algorithm : AesGcmParams , key : AesCryptoKey , data : Buffer ) {
138142 const cipher = crypto . createCipheriv (
139143 `aes-${ key . algorithm . length } -gcm` as CipherGCMTypes ,
140144 key . data ,
141- Buffer . from ( algorithm . iv as ArrayBuffer ) ,
145+ Buffer . from ( toUint8Array ( algorithm . iv ) ) ,
142146 { authTagLength : ( algorithm . tagLength || 128 ) >> 3 } ,
143147 ) ; // NodeJs d.ts doesn't support CipherGCMOptions for createCipheriv
144148 if ( algorithm . additionalData ) {
145- cipher . setAAD ( Buffer . from ( algorithm . additionalData as ArrayBuffer ) ) ;
149+ cipher . setAAD ( Buffer . from ( toUint8Array ( algorithm . additionalData ) ) ) ;
146150 }
147151 let enc = cipher . update ( data ) ;
148152 enc = Buffer . concat ( [ enc , cipher . final ( ) , cipher . getAuthTag ( ) ] ) ;
149- const res = new Uint8Array ( enc ) . buffer ;
153+ const res = toArrayBuffer ( enc ) ;
150154 return res ;
151155 }
152156
153157 public static async decryptAesGCM ( algorithm : AesGcmParams , key : AesCryptoKey , data : Buffer ) {
154158 const tagLength = ( algorithm . tagLength || 128 ) >> 3 ;
155- const decipher = crypto . createDecipheriv ( `aes-${ key . algorithm . length } -gcm` as CipherGCMTypes , key . data , new Uint8Array ( algorithm . iv as ArrayBuffer ) , { authTagLength : tagLength } ) ;
159+ const decipher = crypto . createDecipheriv ( `aes-${ key . algorithm . length } -gcm` as CipherGCMTypes , key . data , toUint8Array ( algorithm . iv ) , { authTagLength : tagLength } ) ;
156160 const enc = data . slice ( 0 , data . length - tagLength ) ;
157161 const tag = data . slice ( data . length - tagLength ) ;
158162 if ( algorithm . additionalData ) {
159- decipher . setAAD ( Buffer . from ( algorithm . additionalData as ArrayBuffer ) ) ;
163+ decipher . setAAD ( Buffer . from ( toUint8Array ( algorithm . additionalData ) ) ) ;
160164 }
161165 decipher . setAuthTag ( tag ) ;
162166 let dec = decipher . update ( enc ) ;
163167 dec = Buffer . concat ( [ dec , decipher . final ( ) ] ) ;
164- return new Uint8Array ( dec ) . buffer ;
168+ return toArrayBuffer ( dec ) ;
165169 }
166170
167171 public static async encryptAesKW ( algorithm : Algorithm , key : AesCryptoKey , data : Buffer ) {
168172 const cipher = crypto . createCipheriv ( `id-aes${ key . algorithm . length } -wrap` , key . data , this . AES_KW_IV ) ;
169173 let enc = cipher . update ( data ) ;
170174 enc = Buffer . concat ( [ enc , cipher . final ( ) ] ) ;
171- return new Uint8Array ( enc ) . buffer ;
175+ return toArrayBuffer ( enc ) ;
172176 }
173177
174178 public static async decryptAesKW ( algorithm : Algorithm , key : AesCryptoKey , data : Buffer ) {
175179 const decipher = crypto . createDecipheriv ( `id-aes${ key . algorithm . length } -wrap` , key . data , this . AES_KW_IV ) ;
176180 let dec = decipher . update ( data ) ;
177181 dec = Buffer . concat ( [ dec , decipher . final ( ) ] ) ;
178- return new Uint8Array ( dec ) . buffer ;
182+ return toArrayBuffer ( dec ) ;
179183 }
180184
181185 public static async encryptAesECB ( algorithm : Algorithm , key : AesCryptoKey , data : Buffer ) {
182186 const cipher = crypto . createCipheriv ( `aes-${ key . algorithm . length } -ecb` , key . data , new Uint8Array ( 0 ) ) ;
183187 let enc = cipher . update ( data ) ;
184188 enc = Buffer . concat ( [ enc , cipher . final ( ) ] ) ;
185- const res = new Uint8Array ( enc ) . buffer ;
189+ const res = toArrayBuffer ( enc ) ;
186190 return res ;
187191 }
188192
189193 public static async decryptAesECB ( algorithm : Algorithm , key : AesCryptoKey , data : Buffer ) {
190194 const decipher = crypto . createDecipheriv ( `aes-${ key . algorithm . length } -ecb` , key . data , new Uint8Array ( 0 ) ) ;
191195 let dec = decipher . update ( data ) ;
192196 dec = Buffer . concat ( [ dec , decipher . final ( ) ] ) ;
193- return new Uint8Array ( dec ) . buffer ;
197+ return toArrayBuffer ( dec ) ;
194198 }
195199}
0 commit comments