blob: 6de9992eafa250362efc23fd14752042ac16b13d [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file aes-internal-enc.c
2 * AES (Rijndael) cipher - encrypt.
Harald Welte781bd5d2011-12-06 22:23:52 +01003 *
4 * Modifications to public domain implementation:
5 * - support only 128-bit keys
6 * - cleanup
7 * - use C pre-processor to make it easier to change S table access
8 * - added option (AES_SMALL_TABLES) for reducing code size by about 8 kB at
9 * cost of reduced throughput (quite small difference on Pentium 4,
10 * 10-25% when using -O1 or -O2 optimization)
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020011 */
12/*
Harald Welte781bd5d2011-12-06 22:23:52 +010013 * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License version 2 as
17 * published by the Free Software Foundation.
18 *
19 * Alternatively, this software may be distributed under the terms of BSD
20 * license.
21 *
Harald Weltee08da972017-11-13 01:00:26 +090022 * SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
23 *
Harald Welte781bd5d2011-12-06 22:23:52 +010024 * See README and COPYING for more details.
25 */
26
27#include "includes.h"
28
29#include "common.h"
30#include "crypto.h"
31#include "aes_i.h"
32
33static void rijndaelEncrypt(const u32 rk[/*44*/], const u8 pt[16], u8 ct[16])
34{
35 u32 s0, s1, s2, s3, t0, t1, t2, t3;
36 const int Nr = 10;
37#ifndef FULL_UNROLL
38 int r;
39#endif /* ?FULL_UNROLL */
40
41 /*
42 * map byte array block to cipher state
43 * and add initial round key:
44 */
45 s0 = GETU32(pt ) ^ rk[0];
46 s1 = GETU32(pt + 4) ^ rk[1];
47 s2 = GETU32(pt + 8) ^ rk[2];
48 s3 = GETU32(pt + 12) ^ rk[3];
49
50#define ROUND(i,d,s) \
51d##0 = TE0(s##0) ^ TE1(s##1) ^ TE2(s##2) ^ TE3(s##3) ^ rk[4 * i]; \
52d##1 = TE0(s##1) ^ TE1(s##2) ^ TE2(s##3) ^ TE3(s##0) ^ rk[4 * i + 1]; \
53d##2 = TE0(s##2) ^ TE1(s##3) ^ TE2(s##0) ^ TE3(s##1) ^ rk[4 * i + 2]; \
54d##3 = TE0(s##3) ^ TE1(s##0) ^ TE2(s##1) ^ TE3(s##2) ^ rk[4 * i + 3]
55
56#ifdef FULL_UNROLL
57
58 ROUND(1,t,s);
59 ROUND(2,s,t);
60 ROUND(3,t,s);
61 ROUND(4,s,t);
62 ROUND(5,t,s);
63 ROUND(6,s,t);
64 ROUND(7,t,s);
65 ROUND(8,s,t);
66 ROUND(9,t,s);
67
68 rk += Nr << 2;
69
70#else /* !FULL_UNROLL */
71
72 /* Nr - 1 full rounds: */
73 r = Nr >> 1;
74 for (;;) {
75 ROUND(1,t,s);
76 rk += 8;
77 if (--r == 0)
78 break;
79 ROUND(0,s,t);
80 }
81
82#endif /* ?FULL_UNROLL */
83
84#undef ROUND
85
86 /*
87 * apply last round and
88 * map cipher state to byte array block:
89 */
90 s0 = TE41(t0) ^ TE42(t1) ^ TE43(t2) ^ TE44(t3) ^ rk[0];
91 PUTU32(ct , s0);
92 s1 = TE41(t1) ^ TE42(t2) ^ TE43(t3) ^ TE44(t0) ^ rk[1];
93 PUTU32(ct + 4, s1);
94 s2 = TE41(t2) ^ TE42(t3) ^ TE43(t0) ^ TE44(t1) ^ rk[2];
95 PUTU32(ct + 8, s2);
96 s3 = TE41(t3) ^ TE42(t0) ^ TE43(t1) ^ TE44(t2) ^ rk[3];
97 PUTU32(ct + 12, s3);
98}
99
100
101void * aes_encrypt_init(const u8 *key, size_t len)
102{
103 u32 *rk;
104 if (len != 16)
105 return NULL;
106 rk = os_malloc(AES_PRIV_SIZE);
107 if (rk == NULL)
108 return NULL;
109 rijndaelKeySetupEnc(rk, key);
110 return rk;
111}
112
113
114void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
115{
116 rijndaelEncrypt(ctx, plain, crypt);
117}
118
119
120void aes_encrypt_deinit(void *ctx)
121{
122 os_memset(ctx, 0, AES_PRIV_SIZE);
123 os_free(ctx);
124}