- (djm) [rijndael.c rijndael.h] Sync with newly-ressurected versions ine

OpenBSD
This commit is contained in:
Damien Miller 2014-05-15 13:45:26 +10:00
parent f028460d0b
commit ec0b67eb3b
3 changed files with 109 additions and 90 deletions

View File

@ -1,3 +1,7 @@
20140519
- (djm) [rijndael.c rijndael.h] Sync with newly-ressurected versions ine
OpenBSD
20140430 20140430
- (dtucker) [defines.h] Define __GNUC_PREREQ__ macro if we don't already - (dtucker) [defines.h] Define __GNUC_PREREQ__ macro if we don't already
have it. Only attempt to use __attribute__(__bounded__) for gcc. have it. Only attempt to use __attribute__(__bounded__) for gcc.

View File

@ -1,4 +1,4 @@
/* $OpenBSD: rijndael.c,v 1.16 2004/06/23 00:39:38 mouring Exp $ */ /* $OpenBSD: rijndael.c,v 1.18 2014/04/29 15:42:07 markus Exp $ */
/** /**
* rijndael-alg-fst.c * rijndael-alg-fst.c
@ -25,6 +25,7 @@
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "includes.h" #include "includes.h"
#include <stdlib.h> #include <stdlib.h>
@ -32,7 +33,7 @@
#include "rijndael.h" #include "rijndael.h"
#define FULL_UNROLL #undef FULL_UNROLL
/* /*
Te0[x] = S [x].[02, 01, 01, 03]; Te0[x] = S [x].[02, 01, 01, 03];
@ -247,7 +248,6 @@ static const u32 Te2[256] = {
0xb0cb7bb0U, 0x54fca854U, 0xbbd66dbbU, 0x163a2c16U, 0xb0cb7bb0U, 0x54fca854U, 0xbbd66dbbU, 0x163a2c16U,
}; };
static const u32 Te3[256] = { static const u32 Te3[256] = {
0x6363a5c6U, 0x7c7c84f8U, 0x777799eeU, 0x7b7b8df6U, 0x6363a5c6U, 0x7c7c84f8U, 0x777799eeU, 0x7b7b8df6U,
0xf2f20dffU, 0x6b6bbdd6U, 0x6f6fb1deU, 0xc5c55491U, 0xf2f20dffU, 0x6b6bbdd6U, 0x6f6fb1deU, 0xc5c55491U,
0x30305060U, 0x01010302U, 0x6767a9ceU, 0x2b2b7d56U, 0x30305060U, 0x01010302U, 0x6767a9ceU, 0x2b2b7d56U,
@ -532,7 +532,6 @@ static const u32 Td2[256] = {
0xf4cd65daU, 0xbed50605U, 0x621fd134U, 0xfe8ac4a6U, 0xf4cd65daU, 0xbed50605U, 0x621fd134U, 0xfe8ac4a6U,
0x539d342eU, 0x55a0a2f3U, 0xe132058aU, 0xeb75a4f6U, 0x539d342eU, 0x55a0a2f3U, 0xe132058aU, 0xeb75a4f6U,
0xec390b83U, 0xefaa4060U, 0x9f065e71U, 0x1051bd6eU, 0xec390b83U, 0xefaa4060U, 0x9f065e71U, 0x1051bd6eU,
0x8af93e21U, 0x063d96ddU, 0x05aedd3eU, 0xbd464de6U, 0x8af93e21U, 0x063d96ddU, 0x05aedd3eU, 0xbd464de6U,
0x8db59154U, 0x5d0571c4U, 0xd46f0406U, 0x15ff6050U, 0x8db59154U, 0x5d0571c4U, 0xd46f0406U, 0x15ff6050U,
0xfb241998U, 0xe997d6bdU, 0x43cc8940U, 0x9e7767d9U, 0xfb241998U, 0xe997d6bdU, 0x43cc8940U, 0x9e7767d9U,
@ -724,7 +723,9 @@ static const u32 rcon[] = {
* *
* @return the number of rounds for the given cipher key size. * @return the number of rounds for the given cipher key size.
*/ */
static int rijndaelKeySetupEnc(u32 rk[/*4*(Nr + 1)*/], const u8 cipherKey[], int keyBits) { int
rijndaelKeySetupEnc(u32 rk[/*4*(Nr + 1)*/], const u8 cipherKey[], int keyBits)
{
int i = 0; int i = 0;
u32 temp; u32 temp;
@ -809,18 +810,21 @@ static int rijndaelKeySetupEnc(u32 rk[/*4*(Nr + 1)*/], const u8 cipherKey[], int
* *
* @return the number of rounds for the given cipher key size. * @return the number of rounds for the given cipher key size.
*/ */
static int int
rijndaelKeySetupDec(u32 rk[/*4*(Nr + 1)*/], const u8 cipherKey[], int keyBits, rijndaelKeySetupDec(u32 rk[/*4*(Nr + 1)*/], const u8 cipherKey[], int keyBits,
int have_encrypt) { int have_encrypt)
{
int Nr, i, j; int Nr, i, j;
u32 temp; u32 temp;
if (have_encrypt) { /* expand the cipher key: */
if (have_encrypt > 0) {
/* Already done */
Nr = have_encrypt; Nr = have_encrypt;
} else { } else {
/* expand the cipher key: */
Nr = rijndaelKeySetupEnc(rk, cipherKey, keyBits); Nr = rijndaelKeySetupEnc(rk, cipherKey, keyBits);
} }
/* invert the order of the round keys: */ /* invert the order of the round keys: */
for (i = 0, j = 4*Nr; i < j; i += 4, j -= 4) { for (i = 0, j = 4*Nr; i < j; i += 4, j -= 4) {
temp = rk[i ]; rk[i ] = rk[j ]; rk[j ] = temp; temp = rk[i ]; rk[i ] = rk[j ]; rk[j ] = temp;
@ -855,7 +859,10 @@ rijndaelKeySetupDec(u32 rk[/*4*(Nr + 1)*/], const u8 cipherKey[], int keyBits,
return Nr; return Nr;
} }
static void rijndaelEncrypt(const u32 rk[/*4*(Nr + 1)*/], int Nr, const u8 pt[16], u8 ct[16]) { void
rijndaelEncrypt(const u32 rk[/*4*(Nr + 1)*/], int Nr, const u8 pt[16],
u8 ct[16])
{
u32 s0, s1, s2, s3, t0, t1, t2, t3; u32 s0, s1, s2, s3, t0, t1, t2, t3;
#ifndef FULL_UNROLL #ifndef FULL_UNROLL
int r; int r;
@ -1036,7 +1043,10 @@ static void rijndaelEncrypt(const u32 rk[/*4*(Nr + 1)*/], int Nr, const u8 pt[16
PUTU32(ct + 12, s3); PUTU32(ct + 12, s3);
} }
static void rijndaelDecrypt(const u32 rk[/*4*(Nr + 1)*/], int Nr, const u8 ct[16], u8 pt[16]) { static void
rijndaelDecrypt(const u32 rk[/*4*(Nr + 1)*/], int Nr, const u8 ct[16],
u8 pt[16])
{
u32 s0, s1, s2, s3, t0, t1, t2, t3; u32 s0, s1, s2, s3, t0, t1, t2, t3;
#ifndef FULL_UNROLL #ifndef FULL_UNROLL
int r; int r;

View File

@ -1,4 +1,4 @@
/* $OpenBSD: rijndael.h,v 1.12 2001/12/19 07:18:56 deraadt Exp $ */ /* $OpenBSD: rijndael.h,v 1.14 2014/04/29 15:42:07 markus Exp $ */
/** /**
* rijndael-alg-fst.h * rijndael-alg-fst.h
@ -25,27 +25,32 @@
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#ifndef __RIJNDAEL_H #ifndef _PRIVATE_RIJNDAEL_H
#define __RIJNDAEL_H #define _PRIVATE_RIJNDAEL_H
#define MAXKC (256/32) #define AES_MAXKEYBITS (256)
#define MAXKB (256/8) #define AES_MAXKEYBYTES (AES_MAXKEYBITS/8)
#define MAXNR 14 /* for 256-bit keys, fewer for less */
#define AES_MAXROUNDS 14
typedef unsigned char u8; typedef unsigned char u8;
typedef unsigned short u16; typedef unsigned short u16;
typedef unsigned int u32; typedef unsigned int u32;
int rijndaelKeySetupEnc(unsigned int [], const unsigned char [], int);
void rijndaelEncrypt(const unsigned int [], int, const unsigned char [],
unsigned char []);
/* The structure for key information */ /* The structure for key information */
typedef struct { typedef struct {
int decrypt; int decrypt;
int Nr; /* key-length-dependent number of rounds */ int Nr; /* key-length-dependent number of rounds */
u32 ek[4*(MAXNR + 1)]; /* encrypt key schedule */ u32 ek[4*(AES_MAXROUNDS + 1)]; /* encrypt key schedule */
u32 dk[4*(MAXNR + 1)]; /* decrypt key schedule */ u32 dk[4*(AES_MAXROUNDS + 1)]; /* decrypt key schedule */
} rijndael_ctx; } rijndael_ctx;
void rijndael_set_key(rijndael_ctx *, u_char *, int, int); void rijndael_set_key(rijndael_ctx *, u_char *, int, int);
void rijndael_decrypt(rijndael_ctx *, u_char *, u_char *); void rijndael_decrypt(rijndael_ctx *, u_char *, u_char *);
void rijndael_encrypt(rijndael_ctx *, u_char *, u_char *); void rijndael_encrypt(rijndael_ctx *, u_char *, u_char *);
#endif /* __RIJNDAEL_H */ #endif /* _PRIVATE_RIJNDAEL_H */