wolfSSL 3.11.1 for TLS1.3 beta

Fork of wolfSSL by wolf SSL

Committer:
wolfSSL
Date:
Thu Apr 28 00:57:21 2016 +0000
Revision:
4:1b0d80432c79
wolfSSL 3.9.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 4:1b0d80432c79 1 /* hc128.c
wolfSSL 4:1b0d80432c79 2 *
wolfSSL 4:1b0d80432c79 3 * Copyright (C) 2006-2016 wolfSSL Inc.
wolfSSL 4:1b0d80432c79 4 *
wolfSSL 4:1b0d80432c79 5 * This file is part of wolfSSL.
wolfSSL 4:1b0d80432c79 6 *
wolfSSL 4:1b0d80432c79 7 * wolfSSL is free software; you can redistribute it and/or modify
wolfSSL 4:1b0d80432c79 8 * it under the terms of the GNU General Public License as published by
wolfSSL 4:1b0d80432c79 9 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 4:1b0d80432c79 10 * (at your option) any later version.
wolfSSL 4:1b0d80432c79 11 *
wolfSSL 4:1b0d80432c79 12 * wolfSSL is distributed in the hope that it will be useful,
wolfSSL 4:1b0d80432c79 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 4:1b0d80432c79 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 4:1b0d80432c79 15 * GNU General Public License for more details.
wolfSSL 4:1b0d80432c79 16 *
wolfSSL 4:1b0d80432c79 17 * You should have received a copy of the GNU General Public License
wolfSSL 4:1b0d80432c79 18 * along with this program; if not, write to the Free Software
wolfSSL 4:1b0d80432c79 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
wolfSSL 4:1b0d80432c79 20 */
wolfSSL 4:1b0d80432c79 21
wolfSSL 4:1b0d80432c79 22
wolfSSL 4:1b0d80432c79 23 #ifdef HAVE_CONFIG_H
wolfSSL 4:1b0d80432c79 24 #include <config.h>
wolfSSL 4:1b0d80432c79 25 #endif
wolfSSL 4:1b0d80432c79 26
wolfSSL 4:1b0d80432c79 27 #include <wolfssl/wolfcrypt/settings.h>
wolfSSL 4:1b0d80432c79 28
wolfSSL 4:1b0d80432c79 29 #ifdef HAVE_HC128
wolfSSL 4:1b0d80432c79 30
wolfSSL 4:1b0d80432c79 31 #include <wolfssl/wolfcrypt/hc128.h>
wolfSSL 4:1b0d80432c79 32 #include <wolfssl/wolfcrypt/error-crypt.h>
wolfSSL 4:1b0d80432c79 33 #include <wolfssl/wolfcrypt/logging.h>
wolfSSL 4:1b0d80432c79 34 #ifdef NO_INLINE
wolfSSL 4:1b0d80432c79 35 #include <wolfssl/wolfcrypt/hc128.h>
wolfSSL 4:1b0d80432c79 36 #include <wolfssl/wolfcrypt/misc.h>
wolfSSL 4:1b0d80432c79 37 #else
wolfSSL 4:1b0d80432c79 38 #include <wolfcrypt/src/misc.c>
wolfSSL 4:1b0d80432c79 39 #endif
wolfSSL 4:1b0d80432c79 40
wolfSSL 4:1b0d80432c79 41
wolfSSL 4:1b0d80432c79 42 #ifdef BIG_ENDIAN_ORDER
wolfSSL 4:1b0d80432c79 43 #define LITTLE32(x) ByteReverseWord32(x)
wolfSSL 4:1b0d80432c79 44 #else
wolfSSL 4:1b0d80432c79 45 #define LITTLE32(x) (x)
wolfSSL 4:1b0d80432c79 46 #endif
wolfSSL 4:1b0d80432c79 47
wolfSSL 4:1b0d80432c79 48
wolfSSL 4:1b0d80432c79 49 /*h1 function*/
wolfSSL 4:1b0d80432c79 50 #define h1(ctx, x, y) { \
wolfSSL 4:1b0d80432c79 51 byte a,c; \
wolfSSL 4:1b0d80432c79 52 a = (byte) (x); \
wolfSSL 4:1b0d80432c79 53 c = (byte) ((x) >> 16); \
wolfSSL 4:1b0d80432c79 54 y = (ctx->T[512+a])+(ctx->T[512+256+c]); \
wolfSSL 4:1b0d80432c79 55 }
wolfSSL 4:1b0d80432c79 56
wolfSSL 4:1b0d80432c79 57 /*h2 function*/
wolfSSL 4:1b0d80432c79 58 #define h2(ctx, x, y) { \
wolfSSL 4:1b0d80432c79 59 byte a,c; \
wolfSSL 4:1b0d80432c79 60 a = (byte) (x); \
wolfSSL 4:1b0d80432c79 61 c = (byte) ((x) >> 16); \
wolfSSL 4:1b0d80432c79 62 y = (ctx->T[a])+(ctx->T[256+c]); \
wolfSSL 4:1b0d80432c79 63 }
wolfSSL 4:1b0d80432c79 64
wolfSSL 4:1b0d80432c79 65 /*one step of HC-128, update P and generate 32 bits keystream*/
wolfSSL 4:1b0d80432c79 66 #define step_P(ctx,u,v,a,b,c,d,n){ \
wolfSSL 4:1b0d80432c79 67 word32 tem0,tem1,tem2,tem3; \
wolfSSL 4:1b0d80432c79 68 h1((ctx),(ctx->X[(d)]),tem3); \
wolfSSL 4:1b0d80432c79 69 tem0 = rotrFixed((ctx->T[(v)]),23); \
wolfSSL 4:1b0d80432c79 70 tem1 = rotrFixed((ctx->X[(c)]),10); \
wolfSSL 4:1b0d80432c79 71 tem2 = rotrFixed((ctx->X[(b)]),8); \
wolfSSL 4:1b0d80432c79 72 (ctx->T[(u)]) += tem2+(tem0 ^ tem1); \
wolfSSL 4:1b0d80432c79 73 (ctx->X[(a)]) = (ctx->T[(u)]); \
wolfSSL 4:1b0d80432c79 74 (n) = tem3 ^ (ctx->T[(u)]) ; \
wolfSSL 4:1b0d80432c79 75 }
wolfSSL 4:1b0d80432c79 76
wolfSSL 4:1b0d80432c79 77 /*one step of HC-128, update Q and generate 32 bits keystream*/
wolfSSL 4:1b0d80432c79 78 #define step_Q(ctx,u,v,a,b,c,d,n){ \
wolfSSL 4:1b0d80432c79 79 word32 tem0,tem1,tem2,tem3; \
wolfSSL 4:1b0d80432c79 80 h2((ctx),(ctx->Y[(d)]),tem3); \
wolfSSL 4:1b0d80432c79 81 tem0 = rotrFixed((ctx->T[(v)]),(32-23)); \
wolfSSL 4:1b0d80432c79 82 tem1 = rotrFixed((ctx->Y[(c)]),(32-10)); \
wolfSSL 4:1b0d80432c79 83 tem2 = rotrFixed((ctx->Y[(b)]),(32-8)); \
wolfSSL 4:1b0d80432c79 84 (ctx->T[(u)]) += tem2 + (tem0 ^ tem1); \
wolfSSL 4:1b0d80432c79 85 (ctx->Y[(a)]) = (ctx->T[(u)]); \
wolfSSL 4:1b0d80432c79 86 (n) = tem3 ^ (ctx->T[(u)]) ; \
wolfSSL 4:1b0d80432c79 87 }
wolfSSL 4:1b0d80432c79 88
wolfSSL 4:1b0d80432c79 89 /*16 steps of HC-128, generate 512 bits keystream*/
wolfSSL 4:1b0d80432c79 90 static void generate_keystream(HC128* ctx, word32* keystream)
wolfSSL 4:1b0d80432c79 91 {
wolfSSL 4:1b0d80432c79 92 word32 cc,dd;
wolfSSL 4:1b0d80432c79 93 cc = ctx->counter1024 & 0x1ff;
wolfSSL 4:1b0d80432c79 94 dd = (cc+16)&0x1ff;
wolfSSL 4:1b0d80432c79 95
wolfSSL 4:1b0d80432c79 96 if (ctx->counter1024 < 512)
wolfSSL 4:1b0d80432c79 97 {
wolfSSL 4:1b0d80432c79 98 ctx->counter1024 = (ctx->counter1024 + 16) & 0x3ff;
wolfSSL 4:1b0d80432c79 99 step_P(ctx, cc+0, cc+1, 0, 6, 13,4, keystream[0]);
wolfSSL 4:1b0d80432c79 100 step_P(ctx, cc+1, cc+2, 1, 7, 14,5, keystream[1]);
wolfSSL 4:1b0d80432c79 101 step_P(ctx, cc+2, cc+3, 2, 8, 15,6, keystream[2]);
wolfSSL 4:1b0d80432c79 102 step_P(ctx, cc+3, cc+4, 3, 9, 0, 7, keystream[3]);
wolfSSL 4:1b0d80432c79 103 step_P(ctx, cc+4, cc+5, 4, 10,1, 8, keystream[4]);
wolfSSL 4:1b0d80432c79 104 step_P(ctx, cc+5, cc+6, 5, 11,2, 9, keystream[5]);
wolfSSL 4:1b0d80432c79 105 step_P(ctx, cc+6, cc+7, 6, 12,3, 10,keystream[6]);
wolfSSL 4:1b0d80432c79 106 step_P(ctx, cc+7, cc+8, 7, 13,4, 11,keystream[7]);
wolfSSL 4:1b0d80432c79 107 step_P(ctx, cc+8, cc+9, 8, 14,5, 12,keystream[8]);
wolfSSL 4:1b0d80432c79 108 step_P(ctx, cc+9, cc+10,9, 15,6, 13,keystream[9]);
wolfSSL 4:1b0d80432c79 109 step_P(ctx, cc+10,cc+11,10,0, 7, 14,keystream[10]);
wolfSSL 4:1b0d80432c79 110 step_P(ctx, cc+11,cc+12,11,1, 8, 15,keystream[11]);
wolfSSL 4:1b0d80432c79 111 step_P(ctx, cc+12,cc+13,12,2, 9, 0, keystream[12]);
wolfSSL 4:1b0d80432c79 112 step_P(ctx, cc+13,cc+14,13,3, 10,1, keystream[13]);
wolfSSL 4:1b0d80432c79 113 step_P(ctx, cc+14,cc+15,14,4, 11,2, keystream[14]);
wolfSSL 4:1b0d80432c79 114 step_P(ctx, cc+15,dd+0, 15,5, 12,3, keystream[15]);
wolfSSL 4:1b0d80432c79 115 }
wolfSSL 4:1b0d80432c79 116 else
wolfSSL 4:1b0d80432c79 117 {
wolfSSL 4:1b0d80432c79 118 ctx->counter1024 = (ctx->counter1024 + 16) & 0x3ff;
wolfSSL 4:1b0d80432c79 119 step_Q(ctx, 512+cc+0, 512+cc+1, 0, 6, 13,4, keystream[0]);
wolfSSL 4:1b0d80432c79 120 step_Q(ctx, 512+cc+1, 512+cc+2, 1, 7, 14,5, keystream[1]);
wolfSSL 4:1b0d80432c79 121 step_Q(ctx, 512+cc+2, 512+cc+3, 2, 8, 15,6, keystream[2]);
wolfSSL 4:1b0d80432c79 122 step_Q(ctx, 512+cc+3, 512+cc+4, 3, 9, 0, 7, keystream[3]);
wolfSSL 4:1b0d80432c79 123 step_Q(ctx, 512+cc+4, 512+cc+5, 4, 10,1, 8, keystream[4]);
wolfSSL 4:1b0d80432c79 124 step_Q(ctx, 512+cc+5, 512+cc+6, 5, 11,2, 9, keystream[5]);
wolfSSL 4:1b0d80432c79 125 step_Q(ctx, 512+cc+6, 512+cc+7, 6, 12,3, 10,keystream[6]);
wolfSSL 4:1b0d80432c79 126 step_Q(ctx, 512+cc+7, 512+cc+8, 7, 13,4, 11,keystream[7]);
wolfSSL 4:1b0d80432c79 127 step_Q(ctx, 512+cc+8, 512+cc+9, 8, 14,5, 12,keystream[8]);
wolfSSL 4:1b0d80432c79 128 step_Q(ctx, 512+cc+9, 512+cc+10,9, 15,6, 13,keystream[9]);
wolfSSL 4:1b0d80432c79 129 step_Q(ctx, 512+cc+10,512+cc+11,10,0, 7, 14,keystream[10]);
wolfSSL 4:1b0d80432c79 130 step_Q(ctx, 512+cc+11,512+cc+12,11,1, 8, 15,keystream[11]);
wolfSSL 4:1b0d80432c79 131 step_Q(ctx, 512+cc+12,512+cc+13,12,2, 9, 0, keystream[12]);
wolfSSL 4:1b0d80432c79 132 step_Q(ctx, 512+cc+13,512+cc+14,13,3, 10,1, keystream[13]);
wolfSSL 4:1b0d80432c79 133 step_Q(ctx, 512+cc+14,512+cc+15,14,4, 11,2, keystream[14]);
wolfSSL 4:1b0d80432c79 134 step_Q(ctx, 512+cc+15,512+dd+0, 15,5, 12,3, keystream[15]);
wolfSSL 4:1b0d80432c79 135 }
wolfSSL 4:1b0d80432c79 136 }
wolfSSL 4:1b0d80432c79 137
wolfSSL 4:1b0d80432c79 138
wolfSSL 4:1b0d80432c79 139 /* The following defines the initialization functions */
wolfSSL 4:1b0d80432c79 140 #define f1(x) (rotrFixed((x),7) ^ rotrFixed((x),18) ^ ((x) >> 3))
wolfSSL 4:1b0d80432c79 141 #define f2(x) (rotrFixed((x),17) ^ rotrFixed((x),19) ^ ((x) >> 10))
wolfSSL 4:1b0d80432c79 142
wolfSSL 4:1b0d80432c79 143 /*update table P*/
wolfSSL 4:1b0d80432c79 144 #define update_P(ctx,u,v,a,b,c,d){ \
wolfSSL 4:1b0d80432c79 145 word32 tem0,tem1,tem2,tem3; \
wolfSSL 4:1b0d80432c79 146 tem0 = rotrFixed((ctx->T[(v)]),23); \
wolfSSL 4:1b0d80432c79 147 tem1 = rotrFixed((ctx->X[(c)]),10); \
wolfSSL 4:1b0d80432c79 148 tem2 = rotrFixed((ctx->X[(b)]),8); \
wolfSSL 4:1b0d80432c79 149 h1((ctx),(ctx->X[(d)]),tem3); \
wolfSSL 4:1b0d80432c79 150 (ctx->T[(u)]) = ((ctx->T[(u)]) + tem2+(tem0^tem1)) ^ tem3; \
wolfSSL 4:1b0d80432c79 151 (ctx->X[(a)]) = (ctx->T[(u)]); \
wolfSSL 4:1b0d80432c79 152 }
wolfSSL 4:1b0d80432c79 153
wolfSSL 4:1b0d80432c79 154 /*update table Q*/
wolfSSL 4:1b0d80432c79 155 #define update_Q(ctx,u,v,a,b,c,d){ \
wolfSSL 4:1b0d80432c79 156 word32 tem0,tem1,tem2,tem3; \
wolfSSL 4:1b0d80432c79 157 tem0 = rotrFixed((ctx->T[(v)]),(32-23)); \
wolfSSL 4:1b0d80432c79 158 tem1 = rotrFixed((ctx->Y[(c)]),(32-10)); \
wolfSSL 4:1b0d80432c79 159 tem2 = rotrFixed((ctx->Y[(b)]),(32-8)); \
wolfSSL 4:1b0d80432c79 160 h2((ctx),(ctx->Y[(d)]),tem3); \
wolfSSL 4:1b0d80432c79 161 (ctx->T[(u)]) = ((ctx->T[(u)]) + tem2+(tem0^tem1)) ^ tem3; \
wolfSSL 4:1b0d80432c79 162 (ctx->Y[(a)]) = (ctx->T[(u)]); \
wolfSSL 4:1b0d80432c79 163 }
wolfSSL 4:1b0d80432c79 164
wolfSSL 4:1b0d80432c79 165 /*16 steps of HC-128, without generating keystream, */
wolfSSL 4:1b0d80432c79 166 /*but use the outputs to update P and Q*/
wolfSSL 4:1b0d80432c79 167 static void setup_update(HC128* ctx) /*each time 16 steps*/
wolfSSL 4:1b0d80432c79 168 {
wolfSSL 4:1b0d80432c79 169 word32 cc,dd;
wolfSSL 4:1b0d80432c79 170 cc = ctx->counter1024 & 0x1ff;
wolfSSL 4:1b0d80432c79 171 dd = (cc+16)&0x1ff;
wolfSSL 4:1b0d80432c79 172
wolfSSL 4:1b0d80432c79 173 if (ctx->counter1024 < 512)
wolfSSL 4:1b0d80432c79 174 {
wolfSSL 4:1b0d80432c79 175 ctx->counter1024 = (ctx->counter1024 + 16) & 0x3ff;
wolfSSL 4:1b0d80432c79 176 update_P(ctx, cc+0, cc+1, 0, 6, 13, 4);
wolfSSL 4:1b0d80432c79 177 update_P(ctx, cc+1, cc+2, 1, 7, 14, 5);
wolfSSL 4:1b0d80432c79 178 update_P(ctx, cc+2, cc+3, 2, 8, 15, 6);
wolfSSL 4:1b0d80432c79 179 update_P(ctx, cc+3, cc+4, 3, 9, 0, 7);
wolfSSL 4:1b0d80432c79 180 update_P(ctx, cc+4, cc+5, 4, 10,1, 8);
wolfSSL 4:1b0d80432c79 181 update_P(ctx, cc+5, cc+6, 5, 11,2, 9);
wolfSSL 4:1b0d80432c79 182 update_P(ctx, cc+6, cc+7, 6, 12,3, 10);
wolfSSL 4:1b0d80432c79 183 update_P(ctx, cc+7, cc+8, 7, 13,4, 11);
wolfSSL 4:1b0d80432c79 184 update_P(ctx, cc+8, cc+9, 8, 14,5, 12);
wolfSSL 4:1b0d80432c79 185 update_P(ctx, cc+9, cc+10,9, 15,6, 13);
wolfSSL 4:1b0d80432c79 186 update_P(ctx, cc+10,cc+11,10,0, 7, 14);
wolfSSL 4:1b0d80432c79 187 update_P(ctx, cc+11,cc+12,11,1, 8, 15);
wolfSSL 4:1b0d80432c79 188 update_P(ctx, cc+12,cc+13,12,2, 9, 0);
wolfSSL 4:1b0d80432c79 189 update_P(ctx, cc+13,cc+14,13,3, 10, 1);
wolfSSL 4:1b0d80432c79 190 update_P(ctx, cc+14,cc+15,14,4, 11, 2);
wolfSSL 4:1b0d80432c79 191 update_P(ctx, cc+15,dd+0, 15,5, 12, 3);
wolfSSL 4:1b0d80432c79 192 }
wolfSSL 4:1b0d80432c79 193 else
wolfSSL 4:1b0d80432c79 194 {
wolfSSL 4:1b0d80432c79 195 ctx->counter1024 = (ctx->counter1024 + 16) & 0x3ff;
wolfSSL 4:1b0d80432c79 196 update_Q(ctx, 512+cc+0, 512+cc+1, 0, 6, 13, 4);
wolfSSL 4:1b0d80432c79 197 update_Q(ctx, 512+cc+1, 512+cc+2, 1, 7, 14, 5);
wolfSSL 4:1b0d80432c79 198 update_Q(ctx, 512+cc+2, 512+cc+3, 2, 8, 15, 6);
wolfSSL 4:1b0d80432c79 199 update_Q(ctx, 512+cc+3, 512+cc+4, 3, 9, 0, 7);
wolfSSL 4:1b0d80432c79 200 update_Q(ctx, 512+cc+4, 512+cc+5, 4, 10,1, 8);
wolfSSL 4:1b0d80432c79 201 update_Q(ctx, 512+cc+5, 512+cc+6, 5, 11,2, 9);
wolfSSL 4:1b0d80432c79 202 update_Q(ctx, 512+cc+6, 512+cc+7, 6, 12,3, 10);
wolfSSL 4:1b0d80432c79 203 update_Q(ctx, 512+cc+7, 512+cc+8, 7, 13,4, 11);
wolfSSL 4:1b0d80432c79 204 update_Q(ctx, 512+cc+8, 512+cc+9, 8, 14,5, 12);
wolfSSL 4:1b0d80432c79 205 update_Q(ctx, 512+cc+9, 512+cc+10,9, 15,6, 13);
wolfSSL 4:1b0d80432c79 206 update_Q(ctx, 512+cc+10,512+cc+11,10,0, 7, 14);
wolfSSL 4:1b0d80432c79 207 update_Q(ctx, 512+cc+11,512+cc+12,11,1, 8, 15);
wolfSSL 4:1b0d80432c79 208 update_Q(ctx, 512+cc+12,512+cc+13,12,2, 9, 0);
wolfSSL 4:1b0d80432c79 209 update_Q(ctx, 512+cc+13,512+cc+14,13,3, 10, 1);
wolfSSL 4:1b0d80432c79 210 update_Q(ctx, 512+cc+14,512+cc+15,14,4, 11, 2);
wolfSSL 4:1b0d80432c79 211 update_Q(ctx, 512+cc+15,512+dd+0, 15,5, 12, 3);
wolfSSL 4:1b0d80432c79 212 }
wolfSSL 4:1b0d80432c79 213 }
wolfSSL 4:1b0d80432c79 214
wolfSSL 4:1b0d80432c79 215
wolfSSL 4:1b0d80432c79 216 /* for the 128-bit key: key[0]...key[15]
wolfSSL 4:1b0d80432c79 217 * key[0] is the least significant byte of ctx->key[0] (K_0);
wolfSSL 4:1b0d80432c79 218 * key[3] is the most significant byte of ctx->key[0] (K_0);
wolfSSL 4:1b0d80432c79 219 * ...
wolfSSL 4:1b0d80432c79 220 * key[12] is the least significant byte of ctx->key[3] (K_3)
wolfSSL 4:1b0d80432c79 221 * key[15] is the most significant byte of ctx->key[3] (K_3)
wolfSSL 4:1b0d80432c79 222 *
wolfSSL 4:1b0d80432c79 223 * for the 128-bit iv: iv[0]...iv[15]
wolfSSL 4:1b0d80432c79 224 * iv[0] is the least significant byte of ctx->iv[0] (IV_0);
wolfSSL 4:1b0d80432c79 225 * iv[3] is the most significant byte of ctx->iv[0] (IV_0);
wolfSSL 4:1b0d80432c79 226 * ...
wolfSSL 4:1b0d80432c79 227 * iv[12] is the least significant byte of ctx->iv[3] (IV_3)
wolfSSL 4:1b0d80432c79 228 * iv[15] is the most significant byte of ctx->iv[3] (IV_3)
wolfSSL 4:1b0d80432c79 229 */
wolfSSL 4:1b0d80432c79 230
wolfSSL 4:1b0d80432c79 231
wolfSSL 4:1b0d80432c79 232
wolfSSL 4:1b0d80432c79 233 static void Hc128_SetIV(HC128* ctx, const byte* inIv)
wolfSSL 4:1b0d80432c79 234 {
wolfSSL 4:1b0d80432c79 235 word32 i;
wolfSSL 4:1b0d80432c79 236 word32 iv[4];
wolfSSL 4:1b0d80432c79 237
wolfSSL 4:1b0d80432c79 238 if (inIv)
wolfSSL 4:1b0d80432c79 239 XMEMCPY(iv, inIv, sizeof(iv));
wolfSSL 4:1b0d80432c79 240 else
wolfSSL 4:1b0d80432c79 241 XMEMSET(iv, 0, sizeof(iv));
wolfSSL 4:1b0d80432c79 242
wolfSSL 4:1b0d80432c79 243 for (i = 0; i < (128 >> 5); i++)
wolfSSL 4:1b0d80432c79 244 ctx->iv[i] = LITTLE32(iv[i]);
wolfSSL 4:1b0d80432c79 245
wolfSSL 4:1b0d80432c79 246 for (; i < 8; i++) ctx->iv[i] = ctx->iv[i-4];
wolfSSL 4:1b0d80432c79 247
wolfSSL 4:1b0d80432c79 248 /* expand the key and IV into the table T */
wolfSSL 4:1b0d80432c79 249 /* (expand the key and IV into the table P and Q) */
wolfSSL 4:1b0d80432c79 250
wolfSSL 4:1b0d80432c79 251 for (i = 0; i < 8; i++) ctx->T[i] = ctx->key[i];
wolfSSL 4:1b0d80432c79 252 for (i = 8; i < 16; i++) ctx->T[i] = ctx->iv[i-8];
wolfSSL 4:1b0d80432c79 253
wolfSSL 4:1b0d80432c79 254 for (i = 16; i < (256+16); i++)
wolfSSL 4:1b0d80432c79 255 ctx->T[i] = f2(ctx->T[i-2]) + ctx->T[i-7] + f1(ctx->T[i-15]) +
wolfSSL 4:1b0d80432c79 256 ctx->T[i-16]+i;
wolfSSL 4:1b0d80432c79 257
wolfSSL 4:1b0d80432c79 258 for (i = 0; i < 16; i++) ctx->T[i] = ctx->T[256+i];
wolfSSL 4:1b0d80432c79 259
wolfSSL 4:1b0d80432c79 260 for (i = 16; i < 1024; i++)
wolfSSL 4:1b0d80432c79 261 ctx->T[i] = f2(ctx->T[i-2]) + ctx->T[i-7] + f1(ctx->T[i-15]) +
wolfSSL 4:1b0d80432c79 262 ctx->T[i-16]+256+i;
wolfSSL 4:1b0d80432c79 263
wolfSSL 4:1b0d80432c79 264 /* initialize counter1024, X and Y */
wolfSSL 4:1b0d80432c79 265 ctx->counter1024 = 0;
wolfSSL 4:1b0d80432c79 266 for (i = 0; i < 16; i++) ctx->X[i] = ctx->T[512-16+i];
wolfSSL 4:1b0d80432c79 267 for (i = 0; i < 16; i++) ctx->Y[i] = ctx->T[512+512-16+i];
wolfSSL 4:1b0d80432c79 268
wolfSSL 4:1b0d80432c79 269 /* run the cipher 1024 steps before generating the output */
wolfSSL 4:1b0d80432c79 270 for (i = 0; i < 64; i++) setup_update(ctx);
wolfSSL 4:1b0d80432c79 271 }
wolfSSL 4:1b0d80432c79 272
wolfSSL 4:1b0d80432c79 273
wolfSSL 4:1b0d80432c79 274 static INLINE int DoKey(HC128* ctx, const byte* key, const byte* iv)
wolfSSL 4:1b0d80432c79 275 {
wolfSSL 4:1b0d80432c79 276 word32 i;
wolfSSL 4:1b0d80432c79 277
wolfSSL 4:1b0d80432c79 278 /* Key size in bits 128 */
wolfSSL 4:1b0d80432c79 279 for (i = 0; i < (128 >> 5); i++)
wolfSSL 4:1b0d80432c79 280 ctx->key[i] = LITTLE32(((word32*)key)[i]);
wolfSSL 4:1b0d80432c79 281
wolfSSL 4:1b0d80432c79 282 for ( ; i < 8 ; i++) ctx->key[i] = ctx->key[i-4];
wolfSSL 4:1b0d80432c79 283
wolfSSL 4:1b0d80432c79 284 Hc128_SetIV(ctx, iv);
wolfSSL 4:1b0d80432c79 285
wolfSSL 4:1b0d80432c79 286 return 0;
wolfSSL 4:1b0d80432c79 287 }
wolfSSL 4:1b0d80432c79 288
wolfSSL 4:1b0d80432c79 289
wolfSSL 4:1b0d80432c79 290 /* Key setup */
wolfSSL 4:1b0d80432c79 291 int wc_Hc128_SetKey(HC128* ctx, const byte* key, const byte* iv)
wolfSSL 4:1b0d80432c79 292 {
wolfSSL 4:1b0d80432c79 293 #ifdef XSTREAM_ALIGN
wolfSSL 4:1b0d80432c79 294 if ((wolfssl_word)key % 4) {
wolfSSL 4:1b0d80432c79 295 int alignKey[4];
wolfSSL 4:1b0d80432c79 296
wolfSSL 4:1b0d80432c79 297 /* iv gets aligned in SetIV */
wolfSSL 4:1b0d80432c79 298 WOLFSSL_MSG("Hc128SetKey unaligned key");
wolfSSL 4:1b0d80432c79 299
wolfSSL 4:1b0d80432c79 300 XMEMCPY(alignKey, key, sizeof(alignKey));
wolfSSL 4:1b0d80432c79 301
wolfSSL 4:1b0d80432c79 302 return DoKey(ctx, (const byte*)alignKey, iv);
wolfSSL 4:1b0d80432c79 303 }
wolfSSL 4:1b0d80432c79 304 #endif /* XSTREAM_ALIGN */
wolfSSL 4:1b0d80432c79 305
wolfSSL 4:1b0d80432c79 306 return DoKey(ctx, key, iv);
wolfSSL 4:1b0d80432c79 307 }
wolfSSL 4:1b0d80432c79 308
wolfSSL 4:1b0d80432c79 309
wolfSSL 4:1b0d80432c79 310
wolfSSL 4:1b0d80432c79 311 /* The following defines the encryption of data stream */
wolfSSL 4:1b0d80432c79 312 static INLINE int DoProcess(HC128* ctx, byte* output, const byte* input,
wolfSSL 4:1b0d80432c79 313 word32 msglen)
wolfSSL 4:1b0d80432c79 314 {
wolfSSL 4:1b0d80432c79 315 word32 i, keystream[16];
wolfSSL 4:1b0d80432c79 316
wolfSSL 4:1b0d80432c79 317 for ( ; msglen >= 64; msglen -= 64, input += 64, output += 64)
wolfSSL 4:1b0d80432c79 318 {
wolfSSL 4:1b0d80432c79 319 generate_keystream(ctx, keystream);
wolfSSL 4:1b0d80432c79 320
wolfSSL 4:1b0d80432c79 321 /* unroll loop */
wolfSSL 4:1b0d80432c79 322 ((word32*)output)[0] = ((word32*)input)[0] ^ LITTLE32(keystream[0]);
wolfSSL 4:1b0d80432c79 323 ((word32*)output)[1] = ((word32*)input)[1] ^ LITTLE32(keystream[1]);
wolfSSL 4:1b0d80432c79 324 ((word32*)output)[2] = ((word32*)input)[2] ^ LITTLE32(keystream[2]);
wolfSSL 4:1b0d80432c79 325 ((word32*)output)[3] = ((word32*)input)[3] ^ LITTLE32(keystream[3]);
wolfSSL 4:1b0d80432c79 326 ((word32*)output)[4] = ((word32*)input)[4] ^ LITTLE32(keystream[4]);
wolfSSL 4:1b0d80432c79 327 ((word32*)output)[5] = ((word32*)input)[5] ^ LITTLE32(keystream[5]);
wolfSSL 4:1b0d80432c79 328 ((word32*)output)[6] = ((word32*)input)[6] ^ LITTLE32(keystream[6]);
wolfSSL 4:1b0d80432c79 329 ((word32*)output)[7] = ((word32*)input)[7] ^ LITTLE32(keystream[7]);
wolfSSL 4:1b0d80432c79 330 ((word32*)output)[8] = ((word32*)input)[8] ^ LITTLE32(keystream[8]);
wolfSSL 4:1b0d80432c79 331 ((word32*)output)[9] = ((word32*)input)[9] ^ LITTLE32(keystream[9]);
wolfSSL 4:1b0d80432c79 332 ((word32*)output)[10] = ((word32*)input)[10] ^ LITTLE32(keystream[10]);
wolfSSL 4:1b0d80432c79 333 ((word32*)output)[11] = ((word32*)input)[11] ^ LITTLE32(keystream[11]);
wolfSSL 4:1b0d80432c79 334 ((word32*)output)[12] = ((word32*)input)[12] ^ LITTLE32(keystream[12]);
wolfSSL 4:1b0d80432c79 335 ((word32*)output)[13] = ((word32*)input)[13] ^ LITTLE32(keystream[13]);
wolfSSL 4:1b0d80432c79 336 ((word32*)output)[14] = ((word32*)input)[14] ^ LITTLE32(keystream[14]);
wolfSSL 4:1b0d80432c79 337 ((word32*)output)[15] = ((word32*)input)[15] ^ LITTLE32(keystream[15]);
wolfSSL 4:1b0d80432c79 338 }
wolfSSL 4:1b0d80432c79 339
wolfSSL 4:1b0d80432c79 340 if (msglen > 0)
wolfSSL 4:1b0d80432c79 341 {
wolfSSL 4:1b0d80432c79 342 XMEMSET(keystream, 0, sizeof(keystream)); /* hush the static analysis */
wolfSSL 4:1b0d80432c79 343 generate_keystream(ctx, keystream);
wolfSSL 4:1b0d80432c79 344
wolfSSL 4:1b0d80432c79 345 #ifdef BIG_ENDIAN_ORDER
wolfSSL 4:1b0d80432c79 346 {
wolfSSL 4:1b0d80432c79 347 word32 wordsLeft = msglen / sizeof(word32);
wolfSSL 4:1b0d80432c79 348 if (msglen % sizeof(word32)) wordsLeft++;
wolfSSL 4:1b0d80432c79 349
wolfSSL 4:1b0d80432c79 350 ByteReverseWords(keystream, keystream, wordsLeft * sizeof(word32));
wolfSSL 4:1b0d80432c79 351 }
wolfSSL 4:1b0d80432c79 352 #endif
wolfSSL 4:1b0d80432c79 353
wolfSSL 4:1b0d80432c79 354 for (i = 0; i < msglen; i++)
wolfSSL 4:1b0d80432c79 355 output[i] = input[i] ^ ((byte*)keystream)[i];
wolfSSL 4:1b0d80432c79 356 }
wolfSSL 4:1b0d80432c79 357
wolfSSL 4:1b0d80432c79 358 return 0;
wolfSSL 4:1b0d80432c79 359 }
wolfSSL 4:1b0d80432c79 360
wolfSSL 4:1b0d80432c79 361
wolfSSL 4:1b0d80432c79 362 /* Encrypt/decrypt a message of any size */
wolfSSL 4:1b0d80432c79 363 int wc_Hc128_Process(HC128* ctx, byte* output, const byte* input, word32 msglen)
wolfSSL 4:1b0d80432c79 364 {
wolfSSL 4:1b0d80432c79 365 #ifdef XSTREAM_ALIGN
wolfSSL 4:1b0d80432c79 366 if ((wolfssl_word)input % 4 || (wolfssl_word)output % 4) {
wolfSSL 4:1b0d80432c79 367 #ifndef NO_WOLFSSL_ALLOC_ALIGN
wolfSSL 4:1b0d80432c79 368 byte* tmp;
wolfSSL 4:1b0d80432c79 369 WOLFSSL_MSG("Hc128Process unaligned");
wolfSSL 4:1b0d80432c79 370
wolfSSL 4:1b0d80432c79 371 tmp = (byte*)XMALLOC(msglen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 4:1b0d80432c79 372 if (tmp == NULL) return MEMORY_E;
wolfSSL 4:1b0d80432c79 373
wolfSSL 4:1b0d80432c79 374 XMEMCPY(tmp, input, msglen);
wolfSSL 4:1b0d80432c79 375 DoProcess(ctx, tmp, tmp, msglen);
wolfSSL 4:1b0d80432c79 376 XMEMCPY(output, tmp, msglen);
wolfSSL 4:1b0d80432c79 377
wolfSSL 4:1b0d80432c79 378 XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 4:1b0d80432c79 379
wolfSSL 4:1b0d80432c79 380 return 0;
wolfSSL 4:1b0d80432c79 381 #else
wolfSSL 4:1b0d80432c79 382 return BAD_ALIGN_E;
wolfSSL 4:1b0d80432c79 383 #endif
wolfSSL 4:1b0d80432c79 384 }
wolfSSL 4:1b0d80432c79 385 #endif /* XSTREAM_ALIGN */
wolfSSL 4:1b0d80432c79 386
wolfSSL 4:1b0d80432c79 387 return DoProcess(ctx, output, input, msglen);
wolfSSL 4:1b0d80432c79 388 }
wolfSSL 4:1b0d80432c79 389
wolfSSL 4:1b0d80432c79 390
wolfSSL 4:1b0d80432c79 391 #else /* HAVE_HC128 */
wolfSSL 4:1b0d80432c79 392
wolfSSL 4:1b0d80432c79 393
wolfSSL 4:1b0d80432c79 394 #ifdef _MSC_VER
wolfSSL 4:1b0d80432c79 395 /* 4206 warning for blank file */
wolfSSL 4:1b0d80432c79 396 #pragma warning(disable: 4206)
wolfSSL 4:1b0d80432c79 397 #endif
wolfSSL 4:1b0d80432c79 398
wolfSSL 4:1b0d80432c79 399
wolfSSL 4:1b0d80432c79 400 #endif /* HAVE_HC128 */
wolfSSL 4:1b0d80432c79 401