@@ -19,30 +19,26 @@ public func AppleCryptoNative_ChaCha20Poly1305Encrypt(
1919    aadPtr:  UnsafeMutableRawPointer , 
2020    aadLength:  Int32 
2121 )  ->  Int32  { 
22-     if  #available( macOS 10 . 15 ,  * )  { 
23-         let  nonceData  =  Data ( bytesNoCopy:  noncePtr,  count:  Int ( nonceLength) ,  deallocator:  Data . Deallocator. none) 
24-         let  key  =  Data ( bytesNoCopy:  keyPtr,  count:  Int ( keyLength) ,  deallocator:  Data . Deallocator. none) 
25-         let  plaintext  =  Data ( bytesNoCopy:  plaintextPtr,  count:  Int ( plaintextLength) ,  deallocator:  Data . Deallocator. none) 
26-         let  aad  =  Data ( bytesNoCopy:  aadPtr,  count:  Int ( aadLength) ,  deallocator:  Data . Deallocator. none) 
27-         let  symmetricKey  =  SymmetricKey ( data:  key) 
22+     let  nonceData  =  Data ( bytesNoCopy:  noncePtr,  count:  Int ( nonceLength) ,  deallocator:  Data . Deallocator. none) 
23+     let  key  =  Data ( bytesNoCopy:  keyPtr,  count:  Int ( keyLength) ,  deallocator:  Data . Deallocator. none) 
24+     let  plaintext  =  Data ( bytesNoCopy:  plaintextPtr,  count:  Int ( plaintextLength) ,  deallocator:  Data . Deallocator. none) 
25+     let  aad  =  Data ( bytesNoCopy:  aadPtr,  count:  Int ( aadLength) ,  deallocator:  Data . Deallocator. none) 
26+     let  symmetricKey  =  SymmetricKey ( data:  key) 
2827
29-         guard  let  nonce =  try ? ChaChaPoly . Nonce ( data:  nonceData)  else  { 
30-             return  0 
31-         } 
32- 
33-         guard  let  result =  try ? ChaChaPoly . seal ( plaintext,  using:  symmetricKey,  nonce:  nonce,  authenticating:  aad)  else  { 
34-             return  0 
35-         } 
36- 
37-         assert ( ciphertextBufferLength >=  result. ciphertext. count) 
38-         assert ( tagBufferLength >=  result. tag. count) 
28+     guard  let  nonce =  try ? ChaChaPoly . Nonce ( data:  nonceData)  else  { 
29+         return  0 
30+     } 
3931
40-         result. ciphertext. copyBytes ( to:  ciphertextBuffer,  count:  result. ciphertext. count) 
41-         result. tag. copyBytes ( to:  tagBuffer,  count:  result. tag. count) 
42-         return  1 
32+     guard  let  result =  try ? ChaChaPoly . seal ( plaintext,  using:  symmetricKey,  nonce:  nonce,  authenticating:  aad)  else  { 
33+         return  0 
4334    } 
4435
45-     return  - 1 
36+     assert ( ciphertextBufferLength >=  result. ciphertext. count) 
37+     assert ( tagBufferLength >=  result. tag. count) 
38+ 
39+     result. ciphertext. copyBytes ( to:  ciphertextBuffer,  count:  result. ciphertext. count) 
40+     result. tag. copyBytes ( to:  tagBuffer,  count:  result. tag. count) 
41+     return  1 
4642 } 
4743
4844@_cdecl ( " AppleCryptoNative_ChaCha20Poly1305Decrypt " )  
@@ -61,36 +57,32 @@ public func AppleCryptoNative_ChaCha20Poly1305Decrypt(
6157    aadLength:  Int32 
6258)  ->  Int32 
6359{ 
64-     if  #available( macOS 10 . 15 ,  * )  { 
65-         let  nonceData  =  Data ( bytesNoCopy:  noncePtr,  count:  Int ( nonceLength) ,  deallocator:  Data . Deallocator. none) 
66-         let  key  =  Data ( bytesNoCopy:  keyPtr,  count:  Int ( keyLength) ,  deallocator:  Data . Deallocator. none) 
67-         let  ciphertext  =  Data ( bytesNoCopy:  ciphertextPtr,  count:  Int ( ciphertextLength) ,  deallocator:  Data . Deallocator. none) 
68-         let  aad  =  Data ( bytesNoCopy:  aadPtr,  count:  Int ( aadLength) ,  deallocator:  Data . Deallocator. none) 
69-         let  tag  =  Data ( bytesNoCopy:  tagPtr,  count:  Int ( tagLength) ,  deallocator:  Data . Deallocator. none) 
70-         let  symmetricKey  =  SymmetricKey ( data:  key) 
60+     let  nonceData  =  Data ( bytesNoCopy:  noncePtr,  count:  Int ( nonceLength) ,  deallocator:  Data . Deallocator. none) 
61+     let  key  =  Data ( bytesNoCopy:  keyPtr,  count:  Int ( keyLength) ,  deallocator:  Data . Deallocator. none) 
62+     let  ciphertext  =  Data ( bytesNoCopy:  ciphertextPtr,  count:  Int ( ciphertextLength) ,  deallocator:  Data . Deallocator. none) 
63+     let  aad  =  Data ( bytesNoCopy:  aadPtr,  count:  Int ( aadLength) ,  deallocator:  Data . Deallocator. none) 
64+     let  tag  =  Data ( bytesNoCopy:  tagPtr,  count:  Int ( tagLength) ,  deallocator:  Data . Deallocator. none) 
65+     let  symmetricKey  =  SymmetricKey ( data:  key) 
7166
72-          guard  let  nonce =  try ? ChaChaPoly . Nonce ( data:  nonceData)  else  { 
73-              return  0 
74-          } 
67+     guard  let  nonce =  try ? ChaChaPoly . Nonce ( data:  nonceData)  else  { 
68+         return  0 
69+     } 
7570
76-          guard  let  sealedBoxRestored =  try ? ChaChaPoly . SealedBox ( nonce:  nonce,  ciphertext:  ciphertext,  tag:  tag)  else  { 
77-              return  0 
78-          } 
71+     guard  let  sealedBoxRestored =  try ? ChaChaPoly . SealedBox ( nonce:  nonce,  ciphertext:  ciphertext,  tag:  tag)  else  { 
72+         return  0 
73+     } 
7974
80-          do  { 
81-              let  result  =  try ChaChaPoly . open ( sealedBoxRestored,  using:  symmetricKey,  authenticating:  aad) 
75+     do  { 
76+         let  result  =  try ChaChaPoly . open ( sealedBoxRestored,  using:  symmetricKey,  authenticating:  aad) 
8277
83-             assert ( plaintextBufferLength >=  result. count) 
84-             result. copyBytes ( to:  plaintextBuffer,  count:  result. count) 
85-             return  1 
86-         } 
87-         catch  CryptoKitError . authenticationFailure { 
88-             return  - 2 
89-         } 
90-         catch  { 
91-             return  0 
92-         } 
78+         assert ( plaintextBufferLength >=  result. count) 
79+         result. copyBytes ( to:  plaintextBuffer,  count:  result. count) 
80+         return  1 
81+     } 
82+     catch  CryptoKitError . authenticationFailure { 
83+         return  - 1 
84+     } 
85+     catch  { 
86+         return  0 
9387    } 
94- 
95-     return  - 1 
9688} 
0 commit comments