@@ -488,15 +488,25 @@ func (t *TPM) NewAK(opts *AKConfig) (*AK, error) {
488488 return t .tpm .newAK (opts )
489489}
490490
491- // NewKey creates an application key certified by the attestation key. If opts is nil
492- // then DefaultConfig is used .
493- func ( t * TPM ) NewKey ( ak * AK , opts * KeyConfig ) ( * Key , error ) {
491+ // Return a KeyConfig with default values if appropriate. It never modifies the
492+ // incoming pointer .
493+ func optsSetDefault ( opts * KeyConfig ) * KeyConfig {
494494 if opts == nil {
495- opts = defaultConfig
495+ return defaultConfig
496496 }
497497 if opts .Algorithm == "" && opts .Size == 0 {
498- opts = defaultConfig
498+ optsCopy := * opts
499+ optsCopy .Algorithm = defaultConfig .Algorithm
500+ optsCopy .Size = defaultConfig .Size
501+ return & optsCopy
499502 }
503+ return opts
504+ }
505+
506+ // NewKey creates an application key certified by the attestation key. If opts is nil
507+ // then DefaultConfig is used.
508+ func (t * TPM ) NewKey (ak * AK , opts * KeyConfig ) (* Key , error ) {
509+ opts = optsSetDefault (opts )
500510 return t .tpm .newKey (ak , opts )
501511}
502512
@@ -506,12 +516,7 @@ func (t *TPM) NewKey(ak *AK, opts *KeyConfig) (*Key, error) {
506516// Thus it can be used in cases where the attestation key was not created
507517// by go-attestation library. If opts is nil then DefaultConfig is used.
508518func (t * TPM ) NewKeyCertifiedByKey (akHandle tpmutil.Handle , akAlg Algorithm , opts * KeyConfig ) (* Key , error ) {
509- if opts == nil {
510- opts = defaultConfig
511- }
512- if opts .Algorithm == "" && opts .Size == 0 {
513- opts = defaultConfig
514- }
519+ opts = optsSetDefault (opts )
515520 ck := certifyingKey {handle : akHandle , alg : akAlg }
516521 return t .tpm .newKeyCertifiedByKey (ck , opts )
517522}
0 commit comments