@@ -23,6 +23,7 @@ import (
23
23
)
24
24
25
25
const (
26
+ keyAccessSchemaVersion = "1.0"
26
27
maxFileSizeSupported = 68719476736 // 64gb
27
28
defaultMimeType = "application/octet-stream"
28
29
tdfAsZip = "zip"
@@ -234,7 +235,8 @@ func (s SDK) CreateTDFContext(ctx context.Context, writer io.Writer, reader io.R
234
235
return nil , fmt .Errorf ("io.writer.Write failed: %w" , err )
235
236
}
236
237
237
- segmentSig , err := calculateSignature (cipherData , tdfObject .payloadKey [:], tdfConfig .segmentIntegrityAlgorithm )
238
+ segmentSig , err := calculateSignature (cipherData , tdfObject .payloadKey [:],
239
+ tdfConfig .segmentIntegrityAlgorithm , false )
238
240
if err != nil {
239
241
return nil , fmt .Errorf ("splitKey.GetSignaturefailed: %w" , err )
240
242
}
@@ -252,7 +254,8 @@ func (s SDK) CreateTDFContext(ctx context.Context, writer io.Writer, reader io.R
252
254
readPos += readSize
253
255
}
254
256
255
- rootSignature , err := calculateSignature ([]byte (aggregateHash ), tdfObject .payloadKey [:], tdfConfig .integrityAlgorithm )
257
+ rootSignature , err := calculateSignature ([]byte (aggregateHash ), tdfObject .payloadKey [:],
258
+ tdfConfig .integrityAlgorithm , false )
256
259
if err != nil {
257
260
return nil , fmt .Errorf ("splitKey.GetSignaturefailed: %w" , err )
258
261
}
@@ -299,11 +302,17 @@ func (s SDK) CreateTDFContext(ctx context.Context, writer io.Writer, reader io.R
299
302
tmpAssertion .Statement = assertion .Statement
300
303
tmpAssertion .AppliesToState = assertion .AppliesToState
301
304
302
- hashOfAssertion , err := tmpAssertion .GetHash ()
305
+ hashOfAssertionAsHex , err := tmpAssertion .GetHash ()
303
306
if err != nil {
304
307
return nil , err
305
308
}
306
309
310
+ hashOfAssertion := make ([]byte , hex .DecodedLen (len (hashOfAssertionAsHex )))
311
+ _ , err = hex .Decode (hashOfAssertion , hashOfAssertionAsHex )
312
+ if err != nil {
313
+ return nil , fmt .Errorf ("error decoding hex string: %w" , err )
314
+ }
315
+
307
316
var completeHashBuilder strings.Builder
308
317
completeHashBuilder .WriteString (aggregateHash )
309
318
completeHashBuilder .Write (hashOfAssertion )
@@ -320,7 +329,7 @@ func (s SDK) CreateTDFContext(ctx context.Context, writer io.Writer, reader io.R
320
329
assertionSigningKey = assertion .SigningKey
321
330
}
322
331
323
- if err := tmpAssertion .Sign (string (hashOfAssertion ), string (encoded ), assertionSigningKey ); err != nil {
332
+ if err := tmpAssertion .Sign (string (hashOfAssertionAsHex ), string (encoded ), assertionSigningKey ); err != nil {
324
333
return nil , fmt .Errorf ("failed to sign assertion: %w" , err )
325
334
}
326
335
@@ -358,6 +367,8 @@ func (r *Reader) Manifest() Manifest {
358
367
// prepare the manifest for TDF
359
368
func (s SDK ) prepareManifest (ctx context.Context , t * TDFObject , tdfConfig TDFConfig ) error { //nolint:funlen,gocognit // Better readability keeping it as is
360
369
manifest := Manifest {}
370
+
371
+ manifest .TDFVersion = TDFSpecVersion
361
372
if len (tdfConfig .splitPlan ) == 0 && len (tdfConfig .kasInfoList ) == 0 {
362
373
return fmt .Errorf ("%w: no key access template specified or inferred" , errInvalidKasInfo )
363
374
}
@@ -488,6 +499,7 @@ func (s SDK) prepareManifest(ctx context.Context, t *TDFObject, tdfConfig TDFCon
488
499
EncryptedMetadata : encryptedMetadata ,
489
500
SplitID : splitID ,
490
501
WrappedKey : string (ocrypto .Base64Encode (wrappedKey )),
502
+ SchemaVersion : keyAccessSchemaVersion ,
491
503
}
492
504
493
505
manifest .EncryptionInformation .KeyAccessObjs = append (manifest .EncryptionInformation .KeyAccessObjs , keyAccess )
@@ -603,6 +615,8 @@ func (r *Reader) WriteTo(writer io.Writer) (int64, error) {
603
615
}
604
616
}
605
617
618
+ isLegacyTDF := r .manifest .TDFVersion == ""
619
+
606
620
var totalBytes int64
607
621
var payloadReadOffset int64
608
622
for _ , seg := range r .manifest .EncryptionInformation .IntegrityInformation .Segments {
@@ -621,7 +635,7 @@ func (r *Reader) WriteTo(writer io.Writer) (int64, error) {
621
635
sigAlg = GMAC
622
636
}
623
637
624
- payloadSig , err := calculateSignature (readBuf , r .payloadKey , sigAlg )
638
+ payloadSig , err := calculateSignature (readBuf , r .payloadKey , sigAlg , isLegacyTDF )
625
639
if err != nil {
626
640
return totalBytes , fmt .Errorf ("splitKey.GetSignaturefailed: %w" , err )
627
641
}
@@ -682,6 +696,7 @@ func (r *Reader) ReadAt(buf []byte, offset int64) (int, error) { //nolint:funlen
682
696
return 0 , ErrTDFPayloadReadFail
683
697
}
684
698
699
+ isLegacyTDF := r .manifest .TDFVersion == ""
685
700
var decryptedBuf bytes.Buffer
686
701
var payloadReadOffset int64
687
702
for index , seg := range r .manifest .EncryptionInformation .IntegrityInformation .Segments {
@@ -705,7 +720,7 @@ func (r *Reader) ReadAt(buf []byte, offset int64) (int, error) { //nolint:funlen
705
720
sigAlg = GMAC
706
721
}
707
722
708
- payloadSig , err := calculateSignature (readBuf , r .payloadKey , sigAlg )
723
+ payloadSig , err := calculateSignature (readBuf , r .payloadKey , sigAlg , isLegacyTDF )
709
724
if err != nil {
710
725
return 0 , fmt .Errorf ("splitKey.GetSignaturefailed: %w" , err )
711
726
}
@@ -1019,18 +1034,29 @@ func (r *Reader) buildKey(_ context.Context, results []kaoResult) error {
1019
1034
}
1020
1035
1021
1036
// Get the hash of the assertion
1022
- hashOfAssertion , err := assertion .GetHash ()
1037
+ hashOfAssertionAsHex , err := assertion .GetHash ()
1023
1038
if err != nil {
1024
1039
return fmt .Errorf ("%w: failed to get hash of assertion: %w" , ErrAssertionFailure {ID : assertion .ID }, err )
1025
1040
}
1026
1041
1042
+ hashOfAssertion := make ([]byte , hex .DecodedLen (len (hashOfAssertionAsHex )))
1043
+ _ , err = hex .Decode (hashOfAssertion , hashOfAssertionAsHex )
1044
+ if err != nil {
1045
+ return fmt .Errorf ("error decoding hex string: %w" , err )
1046
+ }
1047
+
1048
+ isLegacyTDF := r .manifest .TDFVersion == ""
1049
+ if isLegacyTDF {
1050
+ hashOfAssertion = hashOfAssertionAsHex
1051
+ }
1052
+
1027
1053
var completeHashBuilder bytes.Buffer
1028
1054
completeHashBuilder .Write (aggregateHash .Bytes ())
1029
1055
completeHashBuilder .Write (hashOfAssertion )
1030
1056
1031
1057
base64Hash := ocrypto .Base64Encode (completeHashBuilder .Bytes ())
1032
1058
1033
- if string (hashOfAssertion ) != assertionHash {
1059
+ if string (hashOfAssertionAsHex ) != assertionHash {
1034
1060
return fmt .Errorf ("%w: assertion hash missmatch" , ErrAssertionFailure {ID : assertion .ID })
1035
1061
}
1036
1062
@@ -1092,29 +1118,36 @@ func (r *Reader) doPayloadKeyUnwrap(ctx context.Context) error { //nolint:gocogn
1092
1118
}
1093
1119
1094
1120
// calculateSignature calculate signature of data of the given algorithm.
1095
- func calculateSignature (data []byte , secret []byte , alg IntegrityAlgorithm ) (string , error ) {
1121
+ func calculateSignature (data []byte , secret []byte , alg IntegrityAlgorithm , isLegacyTDF bool ) (string , error ) {
1096
1122
if alg == HS256 {
1097
1123
hmac := ocrypto .CalculateSHA256Hmac (secret , data )
1098
- return hex .EncodeToString (hmac ), nil
1124
+ if isLegacyTDF {
1125
+ return hex .EncodeToString (hmac ), nil
1126
+ }
1127
+ return string (hmac ), nil
1099
1128
}
1100
1129
if kGMACPayloadLength > len (data ) {
1101
1130
return "" , fmt .Errorf ("fail to create gmac signature" )
1102
1131
}
1103
1132
1104
- return hex .EncodeToString (data [len (data )- kGMACPayloadLength :]), nil
1133
+ if isLegacyTDF {
1134
+ return hex .EncodeToString (data [len (data )- kGMACPayloadLength :]), nil
1135
+ }
1136
+ return string (data [len (data )- kGMACPayloadLength :]), nil
1105
1137
}
1106
1138
1107
1139
// validate the root signature
1108
1140
func validateRootSignature (manifest Manifest , aggregateHash , secret []byte ) (bool , error ) {
1109
1141
rootSigAlg := manifest .EncryptionInformation .IntegrityInformation .RootSignature .Algorithm
1110
1142
rootSigValue := manifest .EncryptionInformation .IntegrityInformation .RootSignature .Signature
1143
+ isLegacyTDF := manifest .TDFVersion == ""
1111
1144
1112
1145
sigAlg := HS256
1113
1146
if strings .EqualFold (gmacIntegrityAlgorithm , rootSigAlg ) {
1114
1147
sigAlg = GMAC
1115
1148
}
1116
1149
1117
- sig , err := calculateSignature (aggregateHash , secret , sigAlg )
1150
+ sig , err := calculateSignature (aggregateHash , secret , sigAlg , isLegacyTDF )
1118
1151
if err != nil {
1119
1152
return false , fmt .Errorf ("splitkey.getSignature failed:%w" , err )
1120
1153
}
0 commit comments