| 1 |
package tpm2 |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto" |
| 5 |
"crypto/rsa" |
| 6 |
"errors" |
| 7 |
"fmt" |
| 8 |
"io" |
| 9 |
"strings" |
| 10 |
) |
| 11 |
|
| 12 |
var ( |
| 13 |
// The source of randomness used for encapsulation ran out of data. |
| 14 |
ErrInsufficientRandom = errors.New("random source did not provide enough data") |
| 15 |
) |
| 16 |
|
| 17 |
// An rsaKey is an RSA-OAEP-based Labeled Encapsulation key. |
| 18 |
type rsaKey struct { |
| 19 |
// The actual public key. |
| 20 |
rsaPub rsa.PublicKey |
| 21 |
// The scheme hash algorithm to use for the OAEP-based encapsulation. |
| 22 |
hash crypto.Hash |
| 23 |
// The name algorithm of the key. |
| 24 |
nameAlg TPMIAlgHash |
| 25 |
// The symmetric parameters of the key. |
| 26 |
symParms *TPMTSymDefObject |
| 27 |
} |
| 28 |
|
| 29 |
func importRSAEncapsulationKey(pub *TPMTPublic) (*rsaKey, error) { |
| 30 |
rsaParms, err := pub.Parameters.RSADetail() |
| 31 |
if err != nil { |
| 32 |
return nil, err |
| 33 |
} |
| 34 |
rsaPub, err := pub.Unique.RSA() |
| 35 |
if err != nil { |
| 36 |
return nil, err |
| 37 |
} |
| 38 |
rsa, err := RSAPub(rsaParms, rsaPub) |
| 39 |
if err != nil { |
| 40 |
return nil, err |
| 41 |
} |
| 42 |
|
| 43 |
// Decide what hash algorithm to use for OAEP. |
| 44 |
// It's the scheme hash algorithm if not null, otherwise it's the name algorithm. |
| 45 |
hashAlgID := pub.NameAlg |
| 46 |
if rsaParms.Scheme.Scheme == TPMAlgOAEP { |
| 47 |
oaep, err := rsaParms.Scheme.Details.OAEP() |
| 48 |
if err != nil { |
| 49 |
return nil, err |
| 50 |
} |
| 51 |
if oaep.HashAlg != TPMAlgNull { |
| 52 |
hashAlgID = oaep.HashAlg |
| 53 |
} |
| 54 |
} |
| 55 |
hashAlg, err := hashAlgID.Hash() |
| 56 |
if err != nil { |
| 57 |
return nil, err |
| 58 |
} |
| 59 |
|
| 60 |
return &rsaKey{ |
| 61 |
rsaPub: *rsa, |
| 62 |
hash: hashAlg, |
| 63 |
nameAlg: pub.NameAlg, |
| 64 |
symParms: &rsaParms.Symmetric, |
| 65 |
}, nil |
| 66 |
} |
| 67 |
|
| 68 |
// Encapsulate performs the OAEP-based RSA Labeled Encapsulation. |
| 69 |
func (pub *rsaKey) Encapsulate(random io.Reader, label string) (secret []byte, ciphertext []byte, err error) { |
| 70 |
secret = make([]byte, pub.hash.Size()) |
| 71 |
n, err := random.Read(secret) |
| 72 |
if err != nil { |
| 73 |
return nil, nil, err |
| 74 |
} |
| 75 |
if n != len(secret) { |
| 76 |
return nil, nil, fmt.Errorf("%w: only read %d bytes but %d were needed", ErrInsufficientRandom, n, len(secret)) |
| 77 |
} |
| 78 |
|
| 79 |
ciphertext, err = pub.encapsulateDerandomized(random, secret, label) |
| 80 |
if err != nil { |
| 81 |
return nil, nil, err |
| 82 |
} |
| 83 |
return secret, ciphertext, err |
| 84 |
} |
| 85 |
|
| 86 |
// encapsulateDerandomized is a derandomized internal version of Encapsulate for testing. |
| 87 |
func (pub *rsaKey) encapsulateDerandomized(oaepSaltReader io.Reader, secret []byte, label string) (ciphertext []byte, err error) { |
| 88 |
// Ensure label is null-terminated. |
| 89 |
if !strings.HasSuffix(label, "\x00") { |
| 90 |
label = label + "\x00" |
| 91 |
} |
| 92 |
|
| 93 |
if len(secret) != pub.hash.Size() { |
| 94 |
return nil, fmt.Errorf("%w: secret was only %d bytes but %d were needed", ErrInsufficientRandom, len(secret), pub.hash.Size()) |
| 95 |
} |
| 96 |
|
| 97 |
ciphertext, err = rsa.EncryptOAEP(pub.hash.New(), oaepSaltReader, &pub.rsaPub, secret, []byte(label)) |
| 98 |
if err != nil { |
| 99 |
return nil, err |
| 100 |
} |
| 101 |
return ciphertext, err |
| 102 |
} |
| 103 |
|
| 104 |
// NameAlg implements LabeledEncapsulationKey. |
| 105 |
func (pub *rsaKey) NameAlg() TPMAlgID { |
| 106 |
return pub.nameAlg |
| 107 |
} |
| 108 |
|
| 109 |
// SymmetricParameters implements LabeledEncapsulationkey. |
| 110 |
func (pub *rsaKey) SymmetricParameters() *TPMTSymDefObject { |
| 111 |
return pub.symParms |
| 112 |
} |