You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by cc...@apache.org on 2019/10/17 20:19:46 UTC
[mynewt-artifact] branch master updated (a13c22c -> 91a9bc9)
This is an automated email from the ASF dual-hosted git repository.
ccollins pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-artifact.git.
from a13c22c Fix signing with elliptic curves
new 35e0c0f Add AES-256-CTR capabilities and dependencies
new 91a9bc9 Fix issues with hash calculation
The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
image/create.go | 118 +++++++++++++++++++++++++++++++++++++++++++-------------
image/image.go | 44 +++++++++++----------
sec/encrypt.go | 18 ++++++---
3 files changed, 128 insertions(+), 52 deletions(-)
[mynewt-artifact] 01/02: Add AES-256-CTR capabilities and
dependencies
Posted by cc...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ccollins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-artifact.git
commit 35e0c0fb16e8cd2e5fed741c3db4c13f6e4c4748
Author: Andy Gross <an...@juul.com>
AuthorDate: Thu Oct 10 15:50:07 2019 -0500
Add AES-256-CTR capabilities and dependencies
This patch adds the necessary changes to support AES-256-CTR and hw key
encryption. In addition, header and image padding have been added to
satisfy the hardware restrictions of being 1KB aligned for the encrypted
payload.
Signed-off-by: Andy Gross <an...@juul.com>
---
image/create.go | 93 ++++++++++++++++++++++++++++++++++++++++++++++++---------
image/image.go | 44 ++++++++++++++-------------
sec/encrypt.go | 18 +++++++----
3 files changed, 116 insertions(+), 39 deletions(-)
diff --git a/image/create.go b/image/create.go
index 1bdf2d1..51003f6 100644
--- a/image/create.go
+++ b/image/create.go
@@ -27,6 +27,7 @@ import (
"crypto/rsa"
"crypto/sha256"
"encoding/asn1"
+ "encoding/base64"
"encoding/binary"
"io/ioutil"
"math/big"
@@ -40,6 +41,8 @@ type ImageCreator struct {
Body []byte
Version ImageVersion
SigKeys []sec.PrivSignKey
+ HWKeyIndex int
+ Nonce []byte
PlainSecret []byte
CipherSecret []byte
HeaderSize int
@@ -50,9 +53,12 @@ type ImageCreator struct {
type ImageCreateOpts struct {
SrcBinFilename string
SrcEncKeyFilename string
+ SrcEncKeyIndex int
Version ImageVersion
SigKeys []sec.PrivSignKey
LoaderHash []byte
+ HdrPad int
+ ImagePad int
}
type ECDSASig struct {
@@ -94,6 +100,30 @@ func sigTlvType(key sec.PrivSignKey) uint8 {
}
}
+func GenerateHWKeyIndexTLV(secretIndex uint32) (ImageTlv, error) {
+ id := make([]byte, 4)
+ binary.LittleEndian.PutUint32(id, secretIndex)
+ return ImageTlv{
+ Header: ImageTlvHdr{
+ Type: IMAGE_TLV_SECRET_ID,
+ Pad: 0,
+ Len: uint16(len(id)),
+ },
+ Data: id,
+ }, nil
+}
+
+func GenerateNonceTLV(nonce []byte) (ImageTlv, error) {
+ return ImageTlv{
+ Header: ImageTlvHdr{
+ Type: IMAGE_TLV_AES_NONCE,
+ Pad: 0,
+ Len: uint16(len(nonce)),
+ },
+ Data: nonce,
+ }, nil
+}
+
func GenerateEncTlv(cipherSecret []byte) (ImageTlv, error) {
var encType uint8
@@ -243,6 +273,7 @@ func GenerateImage(opts ImageCreateOpts) (Image, error) {
ic.Body = srcBin
ic.Version = opts.Version
ic.SigKeys = opts.SigKeys
+ ic.HWKeyIndex = opts.SrcEncKeyIndex
if opts.LoaderHash != nil {
ic.InitialHash = opts.LoaderHash
@@ -251,6 +282,20 @@ func GenerateImage(opts ImageCreateOpts) (Image, error) {
ic.Bootable = true
}
+ if opts.HdrPad > 0 {
+ ic.HeaderSize = opts.HdrPad
+ }
+
+ if opts.ImagePad > 0 {
+ pad := opts.ImagePad - (len(ic.Body) % opts.ImagePad)
+ ic.Body = append(ic.Body, bytes.Repeat([]byte{byte(0xff)}, pad)...)
+ }
+
+ if ic.HWKeyIndex >= 0 {
+ hash := sha256.Sum256(ic.Body)
+ ic.Nonce = hash[:8]
+ }
+
if opts.SrcEncKeyFilename != "" {
plainSecret, err := GeneratePlainSecret()
if err != nil {
@@ -262,18 +307,25 @@ func GenerateImage(opts ImageCreateOpts) (Image, error) {
return Image{}, errors.Wrapf(err, "error reading pubkey file")
}
- pubKe, err := sec.ParsePubEncKey(pubKeBytes)
- if err != nil {
- return Image{}, err
- }
-
- cipherSecret, err := pubKe.Encrypt(plainSecret)
- if err != nil {
- return Image{}, err
+ if ic.HWKeyIndex < 0 {
+ pubKe, err := sec.ParsePubEncKey(pubKeBytes)
+ if err != nil {
+ return Image{}, err
+ }
+
+ cipherSecret, err := pubKe.Encrypt(plainSecret)
+ if err != nil {
+ return Image{}, err
+ }
+
+ ic.CipherSecret = cipherSecret
+ ic.PlainSecret = plainSecret
+ } else {
+ ic.PlainSecret, err = base64.StdEncoding.DecodeString(string(pubKeBytes))
+ if err != nil {
+ return Image{}, err
+ }
}
-
- ic.PlainSecret = plainSecret
- ic.CipherSecret = cipherSecret
}
ri, err := ic.Create()
@@ -349,7 +401,8 @@ func (ic *ImageCreator) Create() (Image, error) {
img.Header.Flags |= IMAGE_F_NON_BOOTABLE
}
- if ic.CipherSecret != nil {
+ // Set encrypted image flag if image is to be treated as encrypted
+ if ic.CipherSecret != nil && ic.HWKeyIndex < 0 {
img.Header.Flags |= IMAGE_F_ENCRYPTED
}
@@ -373,7 +426,7 @@ func (ic *ImageCreator) Create() (Image, error) {
// Followed by data.
if ic.CipherSecret != nil {
- encBody, err := sec.EncryptAES(ic.Body, ic.PlainSecret)
+ encBody, err := sec.EncryptAES(ic.Body, ic.PlainSecret, ic.Nonce)
if err != nil {
return img, err
}
@@ -399,7 +452,19 @@ func (ic *ImageCreator) Create() (Image, error) {
}
img.Tlvs = append(img.Tlvs, tlvs...)
- if ic.CipherSecret != nil {
+ if ic.HWKeyIndex > 0 {
+ tlv, err := GenerateHWKeyIndexTLV(uint32(ic.HWKeyIndex))
+ if err != nil {
+ return img, err
+ }
+ img.Tlvs = append(img.Tlvs, tlv)
+
+ tlv, err = GenerateNonceTLV(ic.Nonce)
+ if err != nil {
+ return img, err
+ }
+ img.Tlvs = append(img.Tlvs, tlv)
+ } else if ic.CipherSecret != nil {
tlv, err := GenerateEncTlv(ic.CipherSecret)
if err != nil {
return img, err
diff --git a/image/image.go b/image/image.go
index 1530ea3..6387393 100644
--- a/image/image.go
+++ b/image/image.go
@@ -54,27 +54,31 @@ const (
* Image trailer TLV types.
*/
const (
- IMAGE_TLV_KEYHASH = 0x01
- IMAGE_TLV_SHA256 = 0x10
- IMAGE_TLV_RSA2048 = 0x20
- IMAGE_TLV_ECDSA224 = 0x21
- IMAGE_TLV_ECDSA256 = 0x22
- IMAGE_TLV_RSA3072 = 0x23
- IMAGE_TLV_ED25519 = 0x24
- IMAGE_TLV_ENC_RSA = 0x30
- IMAGE_TLV_ENC_KEK = 0x31
+ IMAGE_TLV_KEYHASH = 0x01
+ IMAGE_TLV_SHA256 = 0x10
+ IMAGE_TLV_RSA2048 = 0x20
+ IMAGE_TLV_ECDSA224 = 0x21
+ IMAGE_TLV_ECDSA256 = 0x22
+ IMAGE_TLV_RSA3072 = 0x23
+ IMAGE_TLV_ED25519 = 0x24
+ IMAGE_TLV_ENC_RSA = 0x30
+ IMAGE_TLV_ENC_KEK = 0x31
+ IMAGE_TLV_AES_NONCE = 0x50
+ IMAGE_TLV_SECRET_ID = 0x60
)
var imageTlvTypeNameMap = map[uint8]string{
- IMAGE_TLV_KEYHASH: "KEYHASH",
- IMAGE_TLV_SHA256: "SHA256",
- IMAGE_TLV_RSA2048: "RSA2048",
- IMAGE_TLV_ECDSA224: "ECDSA224",
- IMAGE_TLV_ECDSA256: "ECDSA256",
- IMAGE_TLV_RSA3072: "RSA3072",
- IMAGE_TLV_ED25519: "ED25519",
- IMAGE_TLV_ENC_RSA: "ENC_RSA",
- IMAGE_TLV_ENC_KEK: "ENC_KEK",
+ IMAGE_TLV_KEYHASH: "KEYHASH",
+ IMAGE_TLV_SHA256: "SHA256",
+ IMAGE_TLV_RSA2048: "RSA2048",
+ IMAGE_TLV_ECDSA224: "ECDSA224",
+ IMAGE_TLV_ECDSA256: "ECDSA256",
+ IMAGE_TLV_RSA3072: "RSA3072",
+ IMAGE_TLV_ED25519: "ED25519",
+ IMAGE_TLV_ENC_RSA: "ENC_RSA",
+ IMAGE_TLV_ENC_KEK: "ENC_KEK",
+ IMAGE_TLV_AES_NONCE: "AES_NONCE",
+ IMAGE_TLV_SECRET_ID: "SEC_KEY_ID",
}
type ImageVersion struct {
@@ -496,7 +500,7 @@ func Encrypt(img Image, pubEncKey sec.PubEncKey) (Image, error) {
return dup, err
}
- body, err := sec.EncryptAES(dup.Body, plainSecret)
+ body, err := sec.EncryptAES(dup.Body, plainSecret, nil)
if err != nil {
return dup, err
}
@@ -531,7 +535,7 @@ func Decrypt(img Image, privEncKey sec.PrivEncKey) (Image, error) {
return img, err
}
- body, err := sec.EncryptAES(dup.Body, plainSecret)
+ body, err := sec.EncryptAES(dup.Body, plainSecret, nil)
if err != nil {
return img, err
}
diff --git a/sec/encrypt.go b/sec/encrypt.go
index 75b2a76..b63ab8d 100644
--- a/sec/encrypt.go
+++ b/sec/encrypt.go
@@ -64,9 +64,9 @@ func parsePubKePem(b []byte) (PubEncKey, error) {
}
func parsePubKeBase64(keyBytes []byte) (PubEncKey, error) {
- if len(keyBytes) != 16 {
+ if len(keyBytes) != 16 && len(keyBytes) != 32 {
return PubEncKey{}, errors.Errorf(
- "unexpected key size: %d != 16", len(keyBytes))
+ "unexpected key size: %d != 16 or 32", len(keyBytes))
}
cipher, err := aes.NewCipher(keyBytes)
@@ -151,13 +151,21 @@ func (k *PrivEncKey) Decrypt(ciph []byte) ([]byte, error) {
return decryptRsa(k.Rsa, ciph)
}
-func EncryptAES(plain []byte, secret []byte) ([]byte, error) {
+func EncryptAES(plain []byte, secret []byte, nonce []byte) ([]byte, error) {
blk, err := aes.NewCipher(secret)
if err != nil {
return nil, errors.Errorf("Failed to create block cipher")
}
- nonce := make([]byte, 16)
- stream := cipher.NewCTR(blk, nonce)
+
+ var iv []byte
+ if nonce == nil {
+ iv = make([]byte, 16)
+ } else {
+ zeros := make([]byte, 8)
+ iv = append(nonce, zeros...)
+ }
+
+ stream := cipher.NewCTR(blk, iv)
dataBuf := make([]byte, 16)
encBuf := make([]byte, 16)
[mynewt-artifact] 02/02: Fix issues with hash calculation
Posted by cc...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ccollins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-artifact.git
commit 91a9bc9025e6a3b992f2033c1c427a03b7651dad
Author: Andy Gross <an...@juul.com>
AuthorDate: Wed Oct 16 01:05:25 2019 -0500
Fix issues with hash calculation
This patch fixes the hash calculation to use the correct encrypted or
unencrypted application image bytes. This patch also fixes an
incorrect padding if a header size > 32 bytes is used.
Signed-off-by: Andy Gross <an...@juul.com>
---
image/create.go | 31 +++++++++++++++----------------
1 file changed, 15 insertions(+), 16 deletions(-)
diff --git a/image/create.go b/image/create.go
index 51003f6..8783b70 100644
--- a/image/create.go
+++ b/image/create.go
@@ -287,8 +287,8 @@ func GenerateImage(opts ImageCreateOpts) (Image, error) {
}
if opts.ImagePad > 0 {
- pad := opts.ImagePad - (len(ic.Body) % opts.ImagePad)
- ic.Body = append(ic.Body, bytes.Repeat([]byte{byte(0xff)}, pad)...)
+ tail_pad := opts.ImagePad - (len(ic.Body) % opts.ImagePad)
+ ic.Body = append(ic.Body, bytes.Repeat([]byte{byte(0xff)}, tail_pad)...)
}
if ic.HWKeyIndex >= 0 {
@@ -367,14 +367,6 @@ func calcHash(initialHash []byte, hdr ImageHdr, pad []byte,
return nil, err
}
- extra := hdr.HdrSz - IMAGE_HEADER_SIZE
- if extra > 0 {
- b := make([]byte, extra)
- if err := add(b); err != nil {
- return nil, err
- }
- }
-
if err := add(plainBody); err != nil {
return nil, err
}
@@ -401,7 +393,7 @@ func (ic *ImageCreator) Create() (Image, error) {
img.Header.Flags |= IMAGE_F_NON_BOOTABLE
}
- // Set encrypted image flag if image is to be treated as encrypted
+ // Set encrypted image flag if image is to be treated as encrypted
if ic.CipherSecret != nil && ic.HWKeyIndex < 0 {
img.Header.Flags |= IMAGE_F_ENCRYPTED
}
@@ -419,22 +411,29 @@ func (ic *ImageCreator) Create() (Image, error) {
img.Pad = make([]byte, extra)
}
- hashBytes, err := calcHash(ic.InitialHash, img.Header, img.Pad, ic.Body)
- if err != nil {
- return img, err
- }
+ payload := &ic.Body
// Followed by data.
- if ic.CipherSecret != nil {
+ if ic.PlainSecret != nil {
encBody, err := sec.EncryptAES(ic.Body, ic.PlainSecret, ic.Nonce)
if err != nil {
return img, err
}
img.Body = append(img.Body, encBody...)
+
+ if ic.HWKeyIndex >= 0 {
+ payload = &encBody
+ }
+
} else {
img.Body = append(img.Body, ic.Body...)
}
+ hashBytes, err := calcHash(ic.InitialHash, img.Header, img.Pad, *payload)
+ if err != nil {
+ return img, err
+ }
+
// Hash TLV.
tlv := ImageTlv{
Header: ImageTlvHdr{