Skip to content

add AES-128-CFB mode and it's test code.#182

Open
jifanchn wants to merge 3 commits into
kokke:masterfrom
jifanchn:master
Open

add AES-128-CFB mode and it's test code.#182
jifanchn wants to merge 3 commits into
kokke:masterfrom
jifanchn:master

Conversation

@jifanchn

Copy link
Copy Markdown

Hi kokke:

I write a small piece of code to support CFB mode.

I think the code style is quite like the CFB part.

And I generated the test sample by a small golang program and paste the results into test code.

May this is helpful to others.


package main

import (
	"crypto/aes"
	"crypto/cipher"
	"encoding/hex"
	"fmt"
)

func main() {
	in := []byte{ 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46, 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
		0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee, 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2,
		0x73, 0xbe, 0xd6, 0xb8, 0xe3, 0xc1, 0x74, 0x3b, 0x71, 0x16, 0xe6, 0x9e, 0x22, 0x22, 0x95, 0x16,
		0x3f, 0xf1, 0xca, 0xa1, 0x68, 0x1f, 0xac, 0x09, 0x12, 0x0e, 0xca, 0x30, 0x75, 0x86, 0xe1, 0xa7 }
	key := []byte{0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c}
	iv := []byte{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }
	encrypted := AesEncryptCFB(in, key, iv)
	print(fmt.Printf("%v\n", encrypted))
	line := 0
	for i:=0; i<len(encrypted); i++ {
		print("0x" + hex.EncodeToString(encrypted[i:i+1]) + ", ")
		line += 1
		if line == 16 {
			line = 0
			print("\n")
		}
	}

}

func AesEncryptCFB(origData []byte, key []byte, iv []byte) (encrypted []byte) {
	block, err := aes.NewCipher(key)
	if err != nil {
		panic(err)
	}
	encrypted = make([]byte, len(origData))

	stream := cipher.NewCFBEncrypter(block, iv)
	stream.XORKeyStream(encrypted, origData)
	return encrypted
}

Comment thread aes.h Outdated
Comment on lines +87 to +91
#endif // #if defined(CTR) && (CTR == 1)
#endif // #if defined(CFB) && (CFB == 1)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should revert to the original line since the #if is for CTR

@wmjdgla

wmjdgla commented Jan 6, 2022

Copy link
Copy Markdown

In aes.h/c, the code guarded by #if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1)) should also apply to CFB mode, so I think the guard should be updated to include CFB?

@jifanchn

Copy link
Copy Markdown
Author

Already fixed

@lil-skelly lil-skelly left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was looking for a CFB mode implementation, and thankfully, I found this.
The code looks well structured and seems functional, I don't see why this hasn't been merged yet. @kokke , perhaps you want to take a look at this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants