all AES CBC funcs done

This commit is contained in:
brent s. 2022-04-28 05:40:27 -04:00
parent 5da1bbcd11
commit 9027750325
Signed by: bts
GPG Key ID: 8C004C2F93481F6B
6 changed files with 177 additions and 19 deletions

View File

@ -734,7 +734,7 @@ pre.rouge {
<h1>OpenSSH Key Structure Guide</h1> <h1>OpenSSH Key Structure Guide</h1>
<div class="details"> <div class="details">
<span id="author" class="author">brent saner &lt;bts@square-r00t.net&gt;, https://r00t2.io</span><br> <span id="author" class="author">brent saner &lt;bts@square-r00t.net&gt;, https://r00t2.io</span><br>
<span id="revdate">Last updated 2022-04-28 05:18:26 -0400</span> <span id="revdate">Last updated 2022-04-28 05:40:27 -0400</span>
</div> </div>
<div id="toc" class="toc2"> <div id="toc" class="toc2">
<div id="toctitle">Table of Contents</div> <div id="toctitle">Table of Contents</div>

View File

@ -2,16 +2,30 @@ package cbc


import ( import (
`bytes` `bytes`
gAes `crypto/aes`
gCipher `crypto/cipher`
`io` `io`


`r00t2.io/sshkeys/cipher`
`r00t2.io/sshkeys/cipher/aes` `r00t2.io/sshkeys/cipher/aes`
`r00t2.io/sshkeys/cipher/aes/aes128` `r00t2.io/sshkeys/cipher/aes/aes128`
`r00t2.io/sshkeys/internal` `r00t2.io/sshkeys/internal`
) )


// Setup populates a Cipher from a key. The key must include the IV suffixed to the actual key.
func (c *Cipher) Setup(key []byte) (err error) { func (c *Cipher) Setup(key []byte) (err error) {


// TODO if key == nil || len(key) < aes128.KdfKeySize {
err = cipher.ErrBadKeyLen
return
}

if c == nil {
c = &Cipher{}
}

c.key = key[0:aes128.KeySize]
c.iv = key[aes128.KeySize:(aes128.KdfKeySize)]


return return
} }
@ -67,6 +81,8 @@ func (c *Cipher) Encrypt(data interface{}) (encrypted *bytes.Reader, err error)
var b []byte var b []byte
var cryptDst []byte var cryptDst []byte
var padded *bytes.Reader var padded *bytes.Reader
var cryptBlock gCipher.Block
var crypter gCipher.BlockMode


if b, err = internal.SerializeData(data); err != nil { if b, err = internal.SerializeData(data); err != nil {
return return
@ -83,8 +99,14 @@ func (c *Cipher) Encrypt(data interface{}) (encrypted *bytes.Reader, err error)


cryptDst = make([]byte, len(b)) cryptDst = make([]byte, len(b))


// TODO if cryptBlock, err = gAes.NewCipher(c.key); err != nil {
_ = cryptDst return
}
crypter = gCipher.NewCBCEncrypter(cryptBlock, c.iv)

crypter.CryptBlocks(cryptDst, b)

encrypted = bytes.NewReader(cryptDst)


return return
} }
@ -135,7 +157,25 @@ func (c *Cipher) AllocateEncrypt(data interface{}) (encrypted *bytes.Reader, err
*/ */
func (c *Cipher) Pad(data interface{}) (paddedBuf *bytes.Reader, err error) { func (c *Cipher) Pad(data interface{}) (paddedBuf *bytes.Reader, err error) {


// TODO var b []byte
var padNum int
var pad []byte
var buf *bytes.Buffer

if b, err = internal.UnpackBytes(data); err != nil {
return
}
buf = bytes.NewBuffer(b)

for padIdx := 1; (buf.Len() % aes.BlockSize) != 0; padIdx++ {

padNum = padIdx & cipher.PadMod
pad = []byte{byte(uint32(padNum))}

if _, err = buf.Write(pad); err != nil {
return
}
}


return return
} }
@ -154,6 +194,8 @@ func (c *Cipher) Decrypt(data interface{}) (decrypted *bytes.Reader, err error)


var b []byte var b []byte
var decryptDst []byte var decryptDst []byte
var cryptBlock gCipher.Block
var decrypter gCipher.BlockMode


if b, err = internal.SerializeData(data); err != nil { if b, err = internal.SerializeData(data); err != nil {
return return
@ -161,8 +203,14 @@ func (c *Cipher) Decrypt(data interface{}) (decrypted *bytes.Reader, err error)


decryptDst = make([]byte, len(b)) decryptDst = make([]byte, len(b))


// TODO if cryptBlock, err = gAes.NewCipher(c.key); err != nil {
_ = decryptDst return
}
decrypter = gCipher.NewCBCDecrypter(cryptBlock, c.iv)

decrypter.CryptBlocks(decryptDst, b)

decrypted = bytes.NewReader(decryptDst)


return return
} }

View File

@ -2,16 +2,30 @@ package cbc


import ( import (
`bytes` `bytes`
gAes `crypto/aes`
gCipher `crypto/cipher`
`io` `io`


`r00t2.io/sshkeys/cipher`
`r00t2.io/sshkeys/cipher/aes` `r00t2.io/sshkeys/cipher/aes`
`r00t2.io/sshkeys/cipher/aes/aes128` `r00t2.io/sshkeys/cipher/aes/aes128`
`r00t2.io/sshkeys/internal` `r00t2.io/sshkeys/internal`
) )


// Setup populates a Cipher from a key. The key must include the IV suffixed to the actual key.
func (c *Cipher) Setup(key []byte) (err error) { func (c *Cipher) Setup(key []byte) (err error) {


// TODO if key == nil || len(key) < aes128.KdfKeySize {
err = cipher.ErrBadKeyLen
return
}

if c == nil {
c = &Cipher{}
}

c.key = key[0:aes128.KeySize]
c.iv = key[aes128.KeySize:(aes128.KdfKeySize)]


return return
} }
@ -67,6 +81,8 @@ func (c *Cipher) Encrypt(data interface{}) (encrypted *bytes.Reader, err error)
var b []byte var b []byte
var cryptDst []byte var cryptDst []byte
var padded *bytes.Reader var padded *bytes.Reader
var cryptBlock gCipher.Block
var crypter gCipher.BlockMode


if b, err = internal.SerializeData(data); err != nil { if b, err = internal.SerializeData(data); err != nil {
return return
@ -83,8 +99,14 @@ func (c *Cipher) Encrypt(data interface{}) (encrypted *bytes.Reader, err error)


cryptDst = make([]byte, len(b)) cryptDst = make([]byte, len(b))


// TODO if cryptBlock, err = gAes.NewCipher(c.key); err != nil {
_ = cryptDst return
}
crypter = gCipher.NewCBCEncrypter(cryptBlock, c.iv)

crypter.CryptBlocks(cryptDst, b)

encrypted = bytes.NewReader(cryptDst)


return return
} }
@ -135,7 +157,25 @@ func (c *Cipher) AllocateEncrypt(data interface{}) (encrypted *bytes.Reader, err
*/ */
func (c *Cipher) Pad(data interface{}) (paddedBuf *bytes.Reader, err error) { func (c *Cipher) Pad(data interface{}) (paddedBuf *bytes.Reader, err error) {


// TODO var b []byte
var padNum int
var pad []byte
var buf *bytes.Buffer

if b, err = internal.UnpackBytes(data); err != nil {
return
}
buf = bytes.NewBuffer(b)

for padIdx := 1; (buf.Len() % aes.BlockSize) != 0; padIdx++ {

padNum = padIdx & cipher.PadMod
pad = []byte{byte(uint32(padNum))}

if _, err = buf.Write(pad); err != nil {
return
}
}


return return
} }
@ -154,6 +194,8 @@ func (c *Cipher) Decrypt(data interface{}) (decrypted *bytes.Reader, err error)


var b []byte var b []byte
var decryptDst []byte var decryptDst []byte
var cryptBlock gCipher.Block
var decrypter gCipher.BlockMode


if b, err = internal.SerializeData(data); err != nil { if b, err = internal.SerializeData(data); err != nil {
return return
@ -161,8 +203,14 @@ func (c *Cipher) Decrypt(data interface{}) (decrypted *bytes.Reader, err error)


decryptDst = make([]byte, len(b)) decryptDst = make([]byte, len(b))


// TODO if cryptBlock, err = gAes.NewCipher(c.key); err != nil {
_ = decryptDst return
}
decrypter = gCipher.NewCBCDecrypter(cryptBlock, c.iv)

decrypter.CryptBlocks(decryptDst, b)

decrypted = bytes.NewReader(decryptDst)


return return
} }

View File

@ -2,16 +2,30 @@ package cbc


import ( import (
`bytes` `bytes`
gAes `crypto/aes`
gCipher `crypto/cipher`
`io` `io`


`r00t2.io/sshkeys/cipher`
`r00t2.io/sshkeys/cipher/aes` `r00t2.io/sshkeys/cipher/aes`
`r00t2.io/sshkeys/cipher/aes/aes128` `r00t2.io/sshkeys/cipher/aes/aes128`
`r00t2.io/sshkeys/internal` `r00t2.io/sshkeys/internal`
) )


// Setup populates a Cipher from a key. The key must include the IV suffixed to the actual key.
func (c *Cipher) Setup(key []byte) (err error) { func (c *Cipher) Setup(key []byte) (err error) {


// TODO if key == nil || len(key) < aes128.KdfKeySize {
err = cipher.ErrBadKeyLen
return
}

if c == nil {
c = &Cipher{}
}

c.key = key[0:aes128.KeySize]
c.iv = key[aes128.KeySize:(aes128.KdfKeySize)]


return return
} }
@ -67,6 +81,8 @@ func (c *Cipher) Encrypt(data interface{}) (encrypted *bytes.Reader, err error)
var b []byte var b []byte
var cryptDst []byte var cryptDst []byte
var padded *bytes.Reader var padded *bytes.Reader
var cryptBlock gCipher.Block
var crypter gCipher.BlockMode


if b, err = internal.SerializeData(data); err != nil { if b, err = internal.SerializeData(data); err != nil {
return return
@ -83,8 +99,14 @@ func (c *Cipher) Encrypt(data interface{}) (encrypted *bytes.Reader, err error)


cryptDst = make([]byte, len(b)) cryptDst = make([]byte, len(b))


// TODO if cryptBlock, err = gAes.NewCipher(c.key); err != nil {
_ = cryptDst return
}
crypter = gCipher.NewCBCEncrypter(cryptBlock, c.iv)

crypter.CryptBlocks(cryptDst, b)

encrypted = bytes.NewReader(cryptDst)


return return
} }
@ -135,7 +157,25 @@ func (c *Cipher) AllocateEncrypt(data interface{}) (encrypted *bytes.Reader, err
*/ */
func (c *Cipher) Pad(data interface{}) (paddedBuf *bytes.Reader, err error) { func (c *Cipher) Pad(data interface{}) (paddedBuf *bytes.Reader, err error) {


// TODO var b []byte
var padNum int
var pad []byte
var buf *bytes.Buffer

if b, err = internal.UnpackBytes(data); err != nil {
return
}
buf = bytes.NewBuffer(b)

for padIdx := 1; (buf.Len() % aes.BlockSize) != 0; padIdx++ {

padNum = padIdx & cipher.PadMod
pad = []byte{byte(uint32(padNum))}

if _, err = buf.Write(pad); err != nil {
return
}
}


return return
} }
@ -154,6 +194,8 @@ func (c *Cipher) Decrypt(data interface{}) (decrypted *bytes.Reader, err error)


var b []byte var b []byte
var decryptDst []byte var decryptDst []byte
var cryptBlock gCipher.Block
var decrypter gCipher.BlockMode


if b, err = internal.SerializeData(data); err != nil { if b, err = internal.SerializeData(data); err != nil {
return return
@ -161,8 +203,14 @@ func (c *Cipher) Decrypt(data interface{}) (decrypted *bytes.Reader, err error)


decryptDst = make([]byte, len(b)) decryptDst = make([]byte, len(b))


// TODO if cryptBlock, err = gAes.NewCipher(c.key); err != nil {
_ = decryptDst return
}
decrypter = gCipher.NewCBCDecrypter(cryptBlock, c.iv)

decrypter.CryptBlocks(decryptDst, b)

decrypted = bytes.NewReader(decryptDst)


return return
} }

5
cipher/consts.go Normal file
View File

@ -0,0 +1,5 @@
package cipher

const (
PadMod int = 0xff
)

9
cipher/errs.go Normal file
View File

@ -0,0 +1,9 @@
package cipher

import (
`errors`
)

var (
ErrBadKeyLen error = errors.New("the specified key does not match the Cipher.BlockSize size")
)