Files
aes/utils/mixColumns.ts
T
2026-06-24 16:28:42 +02:00

29 lines
1.0 KiB
TypeScript

export const galoisMultiplyBy2 = (value: number): number => {
return (value << 1) ^ ((value & 0x80) ? 0x1b : 0x00)
}
export const galoisMultiplyBy3 = (value: number): number => {
return galoisMultiplyBy2(value) ^ value
}
const clampToByte = (value: number): number => {
return value & 0xFF
}
export const mixColumns = (state: number[][]): number[][] => {
const result: number[][] = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
]
for (let col = 0; col < 4; col++) {
result[0][col] = clampToByte(galoisMultiplyBy2(state[0][col]) ^ galoisMultiplyBy3(state[1][col]) ^ state[2][col] ^ state[3][col])
result[1][col] = clampToByte(state[0][col] ^ galoisMultiplyBy2(state[1][col]) ^ galoisMultiplyBy3(state[2][col]) ^ state[3][col])
result[2][col] = clampToByte(state[0][col] ^ state[1][col] ^ galoisMultiplyBy2(state[2][col]) ^ galoisMultiplyBy3(state[3][col]))
result[3][col] = clampToByte(galoisMultiplyBy3(state[0][col]) ^ state[1][col] ^ state[2][col] ^ galoisMultiplyBy2(state[3][col]))
}
return result
}