Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aes: Add doc.odin with an AES decryption example #3932

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions core/crypto/aes/doc.odin
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
package aes implements the AES block cipher and some common modes.


An example of how to use 'AES-ECB instance' to decrypt binary files

```odin

package foo

import "core:fmt"
import "core:crypto/aes"
import "core:os"
import "core:bytes"

main :: proc() {
data, ok := os.read_entire_file("data.bin")
if !ok {fmt.eprintln("Error Reading File")}
Kelimion marked this conversation as resolved.
Show resolved Hide resolved

key := transmute([]u8)string("aeskey")
ctx: aes.Context_ECB

aes.init_ecb(&ctx, key)

if len(data) % aes.BLOCK_SIZE != 0 {
Kelimion marked this conversation as resolved.
Show resolved Hide resolved
fmt.eprintln("Error: Data length is not a multiple of AES block size")
return
}

// Allocate space for decrypted data
srcdata := make([]byte, len(data))

// Decrypt data in 16-byte blocks
for i in 0 ..< len(data) / aes.BLOCK_SIZE {
Copy link
Member

Choose a reason for hiding this comment

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

I would rename srcdata to plaintext and rewrite the loop like this:

for len(data) > 0 {
   aes.decrypt_ecb(&ctx, plaintext[:aes.BLOCK_SIZE], data[:aes.BLOCK_SIZE])
   data = data[aes.BLOCK_SIZE:]
   plaintext = plaintext[aes.BLOCK_SIZE:]
}

Copy link
Author

Choose a reason for hiding this comment

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

For some reason, when I rewrite the for loop this way, it gives me no output. I'm still studying how to write more idiomatic Odin code, and I will rewrite it properly soon.

Copy link
Member

Choose a reason for hiding this comment

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

I didn't have an encrypted file to test with so I just wrote it here.

And I think I know why. :-) We're advancing the output slice here, which means it's 0 bytes at the end.
Will need to keep a backup of the original slice, too.

_plaintext := plaintext // Preserve original slice view of the decrypted data
for len(data) > 0 {
   aes.decrypt_ecb(&ctx, _plaintext[:aes.BLOCK_SIZE], data[:aes.BLOCK_SIZE])
   data = data[aes.BLOCK_SIZE:]
   _plaintext = _plaintext[aes.BLOCK_SIZE:] // Advance second view of the same destination memory
}

Copy link
Member

Choose a reason for hiding this comment

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

package foo
import "core:fmt"
import "core:crypto/aes"
import "core:os"
import "core:bytes"

BLOCK_SIZE :: aes.BLOCK_SIZE

main :: proc() {
	data, ok := os.read_entire_file("data.bin")
	if !ok {
		fmt.eprintln("Error Reading File")
		return
	}

	if len(data) % aes.BLOCK_SIZE != 0 {
		fmt.eprintln("Error: Data length is not a multiple of AES block size")
		return
	}

	key := transmute([]u8)string("aeskey")
	ctx: aes.Context_ECB
	aes.init_ecb(&ctx, key)
	// Allocate space for decrypted data
	plaintext := make([]byte, len(data))

	plain := plaintext // Preserve original slice view of the decrypted data
	for len(data) > 0 {
		aes.decrypt_ecb(&ctx, plain[:BLOCK_SIZE], data[:BLOCK_SIZE])
		data  = data[BLOCK_SIZE:]  // Advance encrypted data
		plain = plain[BLOCK_SIZE:] // Advance second view of the same destination memory
	}

	// Output decrypted data
	fmt.println(plaintext)
	os.write_entire_file("rcdecrypted.bin", plaintext)
}

Copy link
Member

Choose a reason for hiding this comment

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

Does this work?

Copy link
Author

Choose a reason for hiding this comment

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

Yes, worked like a charm

start := i * aes.BLOCK_SIZE
end := start + aes.BLOCK_SIZE
aes.decrypt_ecb(&ctx, srcdata[start:end], data[start:end])
}

// Output decrypted data
fmt.println(srcdata)
os.write_entire_file("rcdecrypted.bin",srcdata)
}
```

*/
package aes