Add an AES 256 implementation.

Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
This commit is contained in:
NIIBE Yutaka 2022-06-08 15:22:04 +09:00
parent e26445c687
commit 12473d8d4f
5 changed files with 493 additions and 8 deletions

View File

@ -0,0 +1,137 @@
#
# gen_rijndael_t_table.py - Generate Rijndael T-Table
#
# Non-Copyright (CC0) 2020 Free Software Initiative of Japan
# Author: NIIBE Yutaka <gniibe@fsij.org>
#
# This file is made available under the Creative Commons CC0 1.0
# Universal Public Domain Dedication.
#
# To the extent possible under law, the person who associated CC0 with
# this work has waived all copyright and related or neighboring rights
# to this work.
#
#
# Generate Rijindael T-Table (the first one) in the format
# of Mbed TLS (originally callled PolarSSL, by Paul Bakker).
#
# Rijndael uses GF(2^8) with a irreducible polynomial:
#
# m(x) = x^8 + x^4 + x^3 + x + 1
#
# A point in the field is represented by 8-bit binary.
# 0x1b represents a lower part of m(x), x^4 + x^3 + x + 1
m_irreducible = 0x1b
# The constant
C = 0x63
# Function XTIME - Multiplication by x
#
# Given a polynomial A of GF(2)[x], compute a polynomial multiplied by x,
# modulo m(x).
def xtime(a):
global m_irreducible
if a & 0x80:
return ((a & 0x7f) << 1) ^ m_irreducible
else:
return a << 1
# Function GMULT - Galore field multiplication
#
# Given polynomials A, B of GF(2)[x], compute a polynomial A * B,
# modulo m(x).
def gmult(a,b):
r = 0
for i in range(8):
r = r ^ ((b & 0x01) * a)
b = b >> 1
a = xtime(a)
return r
# Function INV - Multiplicative inverse
#
# Given polynomials A of GF(2)[x], compute its multiplicative inverse.
#
# Extended to have defined value at A=0, returning 0.
#
# Note about the implementation:
#
# Brute force appoarch finding an inverse is more efficient. Further,
# for efficiency, using two tables of exp and log (with any good base
# like 3, 5, 6...) to compute exp(log(1) - log(a)) is much better,
# when called many times.
#
def inv(a):
r = 1
for i in range(254): # 254 = 2^8 - 2
r = gmult(r,a)
return r
# Circular shift operation
def rotate(a,n):
return ((a << n) & 0xff) | (a >> (8 - n))
def rijndael_affine_transform(v):
global C
return v ^ rotate(v,1) ^ rotate(v,2) ^ rotate(v,3) ^ rotate(v,4) ^ C
def sbox_value(i):
return rijndael_affine_transform(inv(i))
def print_t_table_value(i,x):
y = xtime(x)
z = x ^ y
punct = "" if i==255 else ", \\\n " if i % 4 == 3 else ", "
print("V(","%02X," % z,"%02X," % x,"%02X," % x,"%02X)" % y,
punct, sep='', end='')
if __name__ == '__main__':
#
# Usage:
# $ python3 gen_rijndael_t_table.py
#
# Usage with args, replacing the irreducible and the constant:
# $ python3 gen_rijndael_t_table.py 1b 63
#
# Usage to generate Rcon
# $ python3 gen_rijndael_t_table.py 'Rcon[10]'
#
# $ python3 gen_rijndael_t_table.py 1b 63 'Rcon[10]'
#
name_rcon = None
import sys
if len(sys.argv) == 2:
name_rcon = sys.argv[1]
elif len(sys.argv) >= 3:
m_irreducible = int(sys.argv[1],16)
C = int(sys.argv[2],16)
if len(sys.argv) == 4:
name_rcon = sys.argv[3]
if name_rcon:
n_rcon = int(name_rcon[name_rcon.index('[')+1:name_rcon.index(']')])
print("static const uint32_t %s =\n{" % name_rcon)
x = 1
print(" ", end='')
for i in range(n_rcon):
punct = "\n" if i==n_rcon-1 else ",\n " if i % 4 == 3 else ", "
print("0x%08X" % x, punct, sep='', end='')
x = xtime(x)
print("};")
exit(0)
# Note: I just want to use list comprehension of Python, though not needed
S_box=[sbox_value(i) for i in range(256)]
print("#define FT \\")
print(" ", end='')
for i in range(256):
x = S_box[i]
print_t_table_value(i,x)
print("")

View File

@ -8,6 +8,7 @@ CHOPSTX = ../chopstx
CSRC = main.c \ CSRC = main.c \
usb_desc.c usb_ctrl.c \ usb_desc.c usb_ctrl.c \
usb-ccid.c openpgp.c ac.c openpgp-do.c flash.c \ usb-ccid.c openpgp.c ac.c openpgp-do.c flash.c \
aes.c \
bn.c mod.c \ bn.c mod.c \
modp256k1.c jpc_p256k1.c ec_p256k1.c call-ec_p256k1.c \ modp256k1.c jpc_p256k1.c ec_p256k1.c call-ec_p256k1.c \
mod25638.c ecc-ed25519.c ecc-mont.c sha512.c \ mod25638.c ecc-ed25519.c ecc-mont.c sha512.c \
@ -17,14 +18,6 @@ CSRC = main.c \
INCDIR = INCDIR =
CRYPTDIR = ../polarssl
CRYPTSRCDIR = $(CRYPTDIR)/library
CRYPTINCDIR = $(CRYPTDIR)/include
CRYPTSRC = $(CRYPTSRCDIR)/aes.c
CSRC += $(CRYPTSRC)
INCDIR += $(CRYPTINCDIR)
include config.mk include config.mk
USE_SYS = yes USE_SYS = yes

66
src/aes-t-table.c.in Normal file
View File

@ -0,0 +1,66 @@
/* Generated by ../misc/gen_rijndael_t_table.py */
#define FT \
V(A5,63,63,C6), V(84,7C,7C,F8), V(99,77,77,EE), V(8D,7B,7B,F6), \
V(0D,F2,F2,FF), V(BD,6B,6B,D6), V(B1,6F,6F,DE), V(54,C5,C5,91), \
V(50,30,30,60), V(03,01,01,02), V(A9,67,67,CE), V(7D,2B,2B,56), \
V(19,FE,FE,E7), V(62,D7,D7,B5), V(E6,AB,AB,4D), V(9A,76,76,EC), \
V(45,CA,CA,8F), V(9D,82,82,1F), V(40,C9,C9,89), V(87,7D,7D,FA), \
V(15,FA,FA,EF), V(EB,59,59,B2), V(C9,47,47,8E), V(0B,F0,F0,FB), \
V(EC,AD,AD,41), V(67,D4,D4,B3), V(FD,A2,A2,5F), V(EA,AF,AF,45), \
V(BF,9C,9C,23), V(F7,A4,A4,53), V(96,72,72,E4), V(5B,C0,C0,9B), \
V(C2,B7,B7,75), V(1C,FD,FD,E1), V(AE,93,93,3D), V(6A,26,26,4C), \
V(5A,36,36,6C), V(41,3F,3F,7E), V(02,F7,F7,F5), V(4F,CC,CC,83), \
V(5C,34,34,68), V(F4,A5,A5,51), V(34,E5,E5,D1), V(08,F1,F1,F9), \
V(93,71,71,E2), V(73,D8,D8,AB), V(53,31,31,62), V(3F,15,15,2A), \
V(0C,04,04,08), V(52,C7,C7,95), V(65,23,23,46), V(5E,C3,C3,9D), \
V(28,18,18,30), V(A1,96,96,37), V(0F,05,05,0A), V(B5,9A,9A,2F), \
V(09,07,07,0E), V(36,12,12,24), V(9B,80,80,1B), V(3D,E2,E2,DF), \
V(26,EB,EB,CD), V(69,27,27,4E), V(CD,B2,B2,7F), V(9F,75,75,EA), \
V(1B,09,09,12), V(9E,83,83,1D), V(74,2C,2C,58), V(2E,1A,1A,34), \
V(2D,1B,1B,36), V(B2,6E,6E,DC), V(EE,5A,5A,B4), V(FB,A0,A0,5B), \
V(F6,52,52,A4), V(4D,3B,3B,76), V(61,D6,D6,B7), V(CE,B3,B3,7D), \
V(7B,29,29,52), V(3E,E3,E3,DD), V(71,2F,2F,5E), V(97,84,84,13), \
V(F5,53,53,A6), V(68,D1,D1,B9), V(00,00,00,00), V(2C,ED,ED,C1), \
V(60,20,20,40), V(1F,FC,FC,E3), V(C8,B1,B1,79), V(ED,5B,5B,B6), \
V(BE,6A,6A,D4), V(46,CB,CB,8D), V(D9,BE,BE,67), V(4B,39,39,72), \
V(DE,4A,4A,94), V(D4,4C,4C,98), V(E8,58,58,B0), V(4A,CF,CF,85), \
V(6B,D0,D0,BB), V(2A,EF,EF,C5), V(E5,AA,AA,4F), V(16,FB,FB,ED), \
V(C5,43,43,86), V(D7,4D,4D,9A), V(55,33,33,66), V(94,85,85,11), \
V(CF,45,45,8A), V(10,F9,F9,E9), V(06,02,02,04), V(81,7F,7F,FE), \
V(F0,50,50,A0), V(44,3C,3C,78), V(BA,9F,9F,25), V(E3,A8,A8,4B), \
V(F3,51,51,A2), V(FE,A3,A3,5D), V(C0,40,40,80), V(8A,8F,8F,05), \
V(AD,92,92,3F), V(BC,9D,9D,21), V(48,38,38,70), V(04,F5,F5,F1), \
V(DF,BC,BC,63), V(C1,B6,B6,77), V(75,DA,DA,AF), V(63,21,21,42), \
V(30,10,10,20), V(1A,FF,FF,E5), V(0E,F3,F3,FD), V(6D,D2,D2,BF), \
V(4C,CD,CD,81), V(14,0C,0C,18), V(35,13,13,26), V(2F,EC,EC,C3), \
V(E1,5F,5F,BE), V(A2,97,97,35), V(CC,44,44,88), V(39,17,17,2E), \
V(57,C4,C4,93), V(F2,A7,A7,55), V(82,7E,7E,FC), V(47,3D,3D,7A), \
V(AC,64,64,C8), V(E7,5D,5D,BA), V(2B,19,19,32), V(95,73,73,E6), \
V(A0,60,60,C0), V(98,81,81,19), V(D1,4F,4F,9E), V(7F,DC,DC,A3), \
V(66,22,22,44), V(7E,2A,2A,54), V(AB,90,90,3B), V(83,88,88,0B), \
V(CA,46,46,8C), V(29,EE,EE,C7), V(D3,B8,B8,6B), V(3C,14,14,28), \
V(79,DE,DE,A7), V(E2,5E,5E,BC), V(1D,0B,0B,16), V(76,DB,DB,AD), \
V(3B,E0,E0,DB), V(56,32,32,64), V(4E,3A,3A,74), V(1E,0A,0A,14), \
V(DB,49,49,92), V(0A,06,06,0C), V(6C,24,24,48), V(E4,5C,5C,B8), \
V(5D,C2,C2,9F), V(6E,D3,D3,BD), V(EF,AC,AC,43), V(A6,62,62,C4), \
V(A8,91,91,39), V(A4,95,95,31), V(37,E4,E4,D3), V(8B,79,79,F2), \
V(32,E7,E7,D5), V(43,C8,C8,8B), V(59,37,37,6E), V(B7,6D,6D,DA), \
V(8C,8D,8D,01), V(64,D5,D5,B1), V(D2,4E,4E,9C), V(E0,A9,A9,49), \
V(B4,6C,6C,D8), V(FA,56,56,AC), V(07,F4,F4,F3), V(25,EA,EA,CF), \
V(AF,65,65,CA), V(8E,7A,7A,F4), V(E9,AE,AE,47), V(18,08,08,10), \
V(D5,BA,BA,6F), V(88,78,78,F0), V(6F,25,25,4A), V(72,2E,2E,5C), \
V(24,1C,1C,38), V(F1,A6,A6,57), V(C7,B4,B4,73), V(51,C6,C6,97), \
V(23,E8,E8,CB), V(7C,DD,DD,A1), V(9C,74,74,E8), V(21,1F,1F,3E), \
V(DD,4B,4B,96), V(DC,BD,BD,61), V(86,8B,8B,0D), V(85,8A,8A,0F), \
V(90,70,70,E0), V(42,3E,3E,7C), V(C4,B5,B5,71), V(AA,66,66,CC), \
V(D8,48,48,90), V(05,03,03,06), V(01,F6,F6,F7), V(12,0E,0E,1C), \
V(A3,61,61,C2), V(5F,35,35,6A), V(F9,57,57,AE), V(D0,B9,B9,69), \
V(91,86,86,17), V(58,C1,C1,99), V(27,1D,1D,3A), V(B9,9E,9E,27), \
V(38,E1,E1,D9), V(13,F8,F8,EB), V(B3,98,98,2B), V(33,11,11,22), \
V(BB,69,69,D2), V(70,D9,D9,A9), V(89,8E,8E,07), V(A7,94,94,33), \
V(B6,9B,9B,2D), V(22,1E,1E,3C), V(92,87,87,15), V(20,E9,E9,C9), \
V(49,CE,CE,87), V(FF,55,55,AA), V(78,28,28,50), V(7A,DF,DF,A5), \
V(8F,8C,8C,03), V(F8,A1,A1,59), V(80,89,89,09), V(17,0D,0D,1A), \
V(DA,BF,BF,65), V(31,E6,E6,D7), V(C6,42,42,84), V(B8,68,68,D0), \
V(C3,41,41,82), V(B0,99,99,29), V(77,2D,2D,5A), V(11,0F,0F,1E), \
V(CB,B0,B0,7B), V(FC,54,54,A8), V(D6,BB,BB,6D), V(3A,16,16,2C)

236
src/aes.c Normal file
View File

@ -0,0 +1,236 @@
/*
* aes.c - AES256 for Gnuk
*
* Copyright (C) 2020 Free Software Initiative of Japan
* Author: NIIBE Yutaka <gniibe@fsij.org>
*
* This file is a part of Gnuk, a GnuPG USB Token implementation.
*
* Gnuk is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Gnuk is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
* This is an implementation using four T tables (little endian),
* keysize fixed for AES-256.
*
*/
/*
* For AES-256:
*
* Block size in bit = 128, Nb = 4 (= 128 / 32) (in 32-bit word)
* Key size in bit = 256, Nk = 8 (= 256 / 32) (in 32-bit word)
* Number of round Nr = 14
*
* Contextsize = Nb * (Nr + 1) = 60 (in 32-bit word)
*/
#define Nr 14
#define Nk (AES_KEY_SIZE/4)
#include <string.h>
#include "aes.h"
static uint32_t
get_uint32_le (const unsigned char *b, unsigned int i)
{
return ( ((uint32_t)b[i ] )
| ((uint32_t)b[i + 1] << 8)
| ((uint32_t)b[i + 2] << 16)
| ((uint32_t)b[i + 3] << 24));
}
static void
put_uint32_le (unsigned char *b, unsigned int i, uint32_t n)
{
b[i ] = (unsigned char) ((n) );
b[i + 1] = (unsigned char) ((n) >> 8);
b[i + 2] = (unsigned char) ((n) >> 16);
b[i + 3] = (unsigned char) ((n) >> 24);
}
/* Forward table */
#include "aes-t-table.c.in"
#define V(a,b,c,d) 0x##a##b##c##d
/* Note that We expose FT0 table. */
const uint32_t FT0[256] = { FT };
#undef V
#define V(a,b,c,d) 0x##b##c##d##a
static const uint32_t FT1[256] = { FT };
#undef V
#define V(a,b,c,d) 0x##c##d##a##b
static const uint32_t FT2[256] = { FT };
#undef V
#define V(a,b,c,d) 0x##d##a##b##c
static const uint32_t FT3[256] = { FT };
#undef V
#undef FT
/* Round constants */
/*
* Note: For AES-256, since (AES_CONTEXT_SIZE / Nk) = 7, we have no
* carry-over in xtime computation for round constants, thus, the
* irreducible polynomial doesn't matter at all.
*/
#define Rcon(i) (1 << i)
static void
key_expansion_step_0 (uint32_t *RK, unsigned int i)
{
RK[8] = RK[0] ^ (FT3[( RK[7] >> 8 ) & 0xff] & 0x000000ff)
^ (FT0[( RK[7] >> 16 ) & 0Xff] & 0x0000ff00)
^ (FT1[( RK[7] >> 24 ) & 0Xff] & 0x00ff0000)
^ (FT2[( RK[7] ) & 0xff] & 0xff000000)
^ Rcon(i);
RK[9] = RK[1] ^ RK[8];
RK[10] = RK[2] ^ RK[9];
RK[11] = RK[3] ^ RK[10];
}
static void
key_expansion_step_1 (uint32_t *RK)
{
RK[12] = RK[4] ^ (FT3[( RK[11] ) & 0xff] & 0x000000ff)
^ (FT0[( RK[11] >> 8 ) & 0xff] & 0x0000ff00)
^ (FT1[( RK[11] >> 16 ) & 0xff] & 0x00ff0000)
^ (FT2[( RK[11] >> 24 ) & 0xff] & 0xff000000);
RK[13] = RK[5] ^ RK[12];
RK[14] = RK[6] ^ RK[13];
RK[15] = RK[7] ^ RK[14];
}
/*
* AES key setup
*/
void
aes_set_key (aes_context *ctx, const unsigned char key[AES_KEY_SIZE])
{
unsigned int i;
uint32_t *RK = ctx->rk;
/* Nk times */
RK[0] = get_uint32_le (key, 0);
RK[1] = get_uint32_le (key, 4);
RK[2] = get_uint32_le (key, 8);
RK[3] = get_uint32_le (key, 12);
RK[4] = get_uint32_le (key, 16);
RK[5] = get_uint32_le (key, 20);
RK[6] = get_uint32_le (key, 24);
RK[7] = get_uint32_le (key, 28);
for (i = 0; i < (AES_CONTEXT_SIZE / Nk) - 1; i++)
{
key_expansion_step_0 (RK, i);
key_expansion_step_1 (RK);
RK += Nk;
}
key_expansion_step_0 (RK, i);
}
static uint32_t
round_calc_step (uint32_t y0, uint32_t y1, uint32_t y2, uint32_t y3)
{
uint32_t x;
x = FT0[( y0 ) & 0xff]
^ FT1[( y1 >> 8 ) & 0xff]
^ FT2[( y2 >> 16 ) & 0xff]
^ FT3[( y3 >> 24 ) & 0xff];
return x;
}
#define ROUND(X0,X1,X2,X3,Y0,Y1,Y2,Y3) \
X0 = round_calc_step (Y0, Y1, Y2, Y3) ^ *RK++; \
X1 = round_calc_step (Y1, Y2, Y3, Y0) ^ *RK++; \
X2 = round_calc_step (Y2, Y3, Y0, Y1) ^ *RK++; \
X3 = round_calc_step (Y3, Y0, Y1, Y2) ^ *RK++
static uint32_t
last_round_calc_step (uint32_t y0, uint32_t y1, uint32_t y2, uint32_t y3)
{
uint32_t x;
x = (FT3[( y0 ) & 0xff] & 0x000000ff)
^ (FT0[( y1 >> 8 ) & 0xff] & 0x0000ff00)
^ (FT1[( y2 >> 16 ) & 0xff] & 0x00ff0000)
^ (FT2[( y3 >> 24 ) & 0xff] & 0xff000000);
return x;
}
#define LAST_ROUND(X0,X1,X2,X3,Y0,Y1,Y2,Y3) \
X0 = last_round_calc_step (Y0, Y1, Y2, Y3) ^ *RK++; \
X1 = last_round_calc_step (Y1, Y2, Y3, Y0) ^ *RK++; \
X2 = last_round_calc_step (Y2, Y3, Y0, Y1) ^ *RK++; \
X3 = last_round_calc_step (Y3, Y0, Y1, Y2) ^ *RK++
/*
* AES block encryption
*/
void
aes_encrypt (const aes_context *ctx,
unsigned char output[AES_BLOCK_SIZE],
const unsigned char input[AES_BLOCK_SIZE])
{
uint32_t X0, X1, X2, X3, Y0, Y1, Y2, Y3;
const uint32_t *RK = ctx->rk;
/* Nb times */
X0 = get_uint32_le (input, 0) ^ *RK++;
X1 = get_uint32_le (input, 4) ^ *RK++;
X2 = get_uint32_le (input, 8) ^ *RK++;
X3 = get_uint32_le (input, 12) ^ *RK++;
/* Nr-1 times */
ROUND (Y0, Y1, Y2, Y3, X0, X1, X2, X3);
ROUND (X0, X1, X2, X3, Y0, Y1, Y2, Y3);
ROUND (Y0, Y1, Y2, Y3, X0, X1, X2, X3);
ROUND (X0, X1, X2, X3, Y0, Y1, Y2, Y3);
ROUND (Y0, Y1, Y2, Y3, X0, X1, X2, X3);
ROUND (X0, X1, X2, X3, Y0, Y1, Y2, Y3);
ROUND (Y0, Y1, Y2, Y3, X0, X1, X2, X3);
ROUND (X0, X1, X2, X3, Y0, Y1, Y2, Y3);
ROUND (Y0, Y1, Y2, Y3, X0, X1, X2, X3);
ROUND (X0, X1, X2, X3, Y0, Y1, Y2, Y3);
ROUND (Y0, Y1, Y2, Y3, X0, X1, X2, X3);
ROUND (X0, X1, X2, X3, Y0, Y1, Y2, Y3);
ROUND (Y0, Y1, Y2, Y3, X0, X1, X2, X3);
/* And, then */
LAST_ROUND (X0, X1, X2, X3, Y0, Y1, Y2, Y3);
/* Nb times */
put_uint32_le (output, 0, X0);
put_uint32_le (output, 4, X1);
put_uint32_le (output, 8, X2);
put_uint32_le (output, 12, X3);
}
/*
* AES key teardown
*/
void
aes_clear_key (aes_context *ctx)
{
memset (ctx->rk, 0, sizeof ctx->rk);
/* to compiler: no removal of memset above, please */
asm ("" : : "m" (ctx->rk) : "memory");
}

53
src/aes.h Normal file
View File

@ -0,0 +1,53 @@
#include <stdint.h>
#define AES_BLOCK_SIZE 16 /* in byte */
/*
* For AES-256:
*
* Block size in bit = 128, Nb = 4 (= 128 / 32) (in 32-bit word)
* Key size in bit = 256, Nk = 8 (= 256 / 32) (in 32-bit word)
* Number of round Nr = 14
*
* Contextsize = Nb * (Nr + 1) = 60 (in 32-bit word)
*/
#define AES_KEY_SIZE 32 /* in byte */
#define AES_CONTEXT_SIZE 60 /* in word */
/**
* AES context structure
*
*/
typedef struct
{
uint32_t rk[AES_CONTEXT_SIZE]; /*!< AES round keys (60-words) */
} aes_context;
/**
* AES key setup
*
* @param ctx AES context
* @param key key
*
*/
void aes_set_key (aes_context *ctx, const unsigned char key[AES_KEY_SIZE]);
/**
* AES block encryption
*
* @param ctx AES context
* @param output output block
* @param input input block
*
*/
void aes_encrypt (const aes_context *ctx,
unsigned char output[AES_BLOCK_SIZE],
const unsigned char input[AES_BLOCK_SIZE]);
/**
* AES key teardown
*
* @param ctx AES context
*
*/
void aes_clear_key (aes_context *ctx);