1238384Sjkim/* ==================================================================== 2238384Sjkim * Copyright (c) 2008 The OpenSSL Project. All rights reserved. 3238384Sjkim * 4238384Sjkim * Redistribution and use in source and binary forms, with or without 5238384Sjkim * modification, are permitted provided that the following conditions 6238384Sjkim * are met: 7238384Sjkim * 8238384Sjkim * 1. Redistributions of source code must retain the above copyright 9280297Sjkim * notice, this list of conditions and the following disclaimer. 10238384Sjkim * 11238384Sjkim * 2. Redistributions in binary form must reproduce the above copyright 12238384Sjkim * notice, this list of conditions and the following disclaimer in 13238384Sjkim * the documentation and/or other materials provided with the 14238384Sjkim * distribution. 15238384Sjkim * 16238384Sjkim * 3. All advertising materials mentioning features or use of this 17238384Sjkim * software must display the following acknowledgment: 18238384Sjkim * "This product includes software developed by the OpenSSL Project 19238384Sjkim * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 20238384Sjkim * 21238384Sjkim * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 22238384Sjkim * endorse or promote products derived from this software without 23238384Sjkim * prior written permission. For written permission, please contact 24238384Sjkim * openssl-core@openssl.org. 25238384Sjkim * 26238384Sjkim * 5. Products derived from this software may not be called "OpenSSL" 27238384Sjkim * nor may "OpenSSL" appear in their names without prior written 28238384Sjkim * permission of the OpenSSL Project. 29238384Sjkim * 30238384Sjkim * 6. Redistributions of any form whatsoever must retain the following 31238384Sjkim * acknowledgment: 32238384Sjkim * "This product includes software developed by the OpenSSL Project 33238384Sjkim * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 34238384Sjkim * 35238384Sjkim * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 36238384Sjkim * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37238384Sjkim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38238384Sjkim * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 39238384Sjkim * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40238384Sjkim * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41238384Sjkim * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42238384Sjkim * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43238384Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44238384Sjkim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45238384Sjkim * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46238384Sjkim * OF THE POSSIBILITY OF SUCH DAMAGE. 47238384Sjkim * ==================================================================== 48238384Sjkim * 49238384Sjkim */ 50238384Sjkim 51238384Sjkim#include <openssl/crypto.h> 52238384Sjkim#include "modes_lcl.h" 53238384Sjkim#include <string.h> 54238384Sjkim 55238384Sjkim#ifndef MODES_DEBUG 56238384Sjkim# ifndef NDEBUG 57238384Sjkim# define NDEBUG 58238384Sjkim# endif 59238384Sjkim#endif 60238384Sjkim#include <assert.h> 61238384Sjkim 62280297Sjkim/* 63280297Sjkim * NOTE: the IV/counter CTR mode is big-endian. The code itself is 64280297Sjkim * endian-neutral. 65280297Sjkim */ 66238384Sjkim 67238384Sjkim/* increment counter (128-bit int) by 1 */ 68280297Sjkimstatic void ctr128_inc(unsigned char *counter) 69280297Sjkim{ 70296279Sjkim u32 n = 16, c = 1; 71238384Sjkim 72280297Sjkim do { 73280297Sjkim --n; 74296279Sjkim c += counter[n]; 75296279Sjkim counter[n] = (u8)c; 76296279Sjkim c >>= 8; 77280297Sjkim } while (n); 78238384Sjkim} 79238384Sjkim 80238384Sjkim#if !defined(OPENSSL_SMALL_FOOTPRINT) 81280297Sjkimstatic void ctr128_inc_aligned(unsigned char *counter) 82280297Sjkim{ 83296279Sjkim size_t *data, c, d, n; 84280297Sjkim const union { 85280297Sjkim long one; 86280297Sjkim char little; 87280297Sjkim } is_endian = { 88280297Sjkim 1 89280297Sjkim }; 90238384Sjkim 91296279Sjkim if (is_endian.little || ((size_t)counter % sizeof(size_t)) != 0) { 92280297Sjkim ctr128_inc(counter); 93280297Sjkim return; 94280297Sjkim } 95238384Sjkim 96280297Sjkim data = (size_t *)counter; 97296279Sjkim c = 1; 98280297Sjkim n = 16 / sizeof(size_t); 99280297Sjkim do { 100280297Sjkim --n; 101296279Sjkim d = data[n] += c; 102296279Sjkim /* did addition carry? */ 103312826Sjkim c = ((d - c) & ~d) >> (sizeof(size_t) * 8 - 1); 104280297Sjkim } while (n); 105238384Sjkim} 106238384Sjkim#endif 107238384Sjkim 108280297Sjkim/* 109280297Sjkim * The input encrypted as though 128bit counter mode is being used. The 110280297Sjkim * extra state information to record how much of the 128bit block we have 111280297Sjkim * used is contained in *num, and the encrypted counter is kept in 112280297Sjkim * ecount_buf. Both *num and ecount_buf must be initialised with zeros 113280297Sjkim * before the first call to CRYPTO_ctr128_encrypt(). This algorithm assumes 114280297Sjkim * that the counter is in the x lower bits of the IV (ivec), and that the 115280297Sjkim * application has full control over overflow and the rest of the IV. This 116280297Sjkim * implementation takes NO responsability for checking that the counter 117280297Sjkim * doesn't overflow into the rest of the IV when incremented. 118238384Sjkim */ 119238384Sjkimvoid CRYPTO_ctr128_encrypt(const unsigned char *in, unsigned char *out, 120280297Sjkim size_t len, const void *key, 121280297Sjkim unsigned char ivec[16], 122280297Sjkim unsigned char ecount_buf[16], unsigned int *num, 123280297Sjkim block128_f block) 124238384Sjkim{ 125280297Sjkim unsigned int n; 126280297Sjkim size_t l = 0; 127238384Sjkim 128280297Sjkim assert(in && out && key && ecount_buf && num); 129280297Sjkim assert(*num < 16); 130238384Sjkim 131280297Sjkim n = *num; 132238384Sjkim 133238384Sjkim#if !defined(OPENSSL_SMALL_FOOTPRINT) 134280297Sjkim if (16 % sizeof(size_t) == 0) { /* always true actually */ 135280297Sjkim do { 136280297Sjkim while (n && len) { 137280297Sjkim *(out++) = *(in++) ^ ecount_buf[n]; 138280297Sjkim --len; 139280297Sjkim n = (n + 1) % 16; 140280297Sjkim } 141238384Sjkim 142280297Sjkim# if defined(STRICT_ALIGNMENT) 143296279Sjkim if (((size_t)in | (size_t)out | (size_t)ecount_buf) 144296279Sjkim % sizeof(size_t) != 0) 145280297Sjkim break; 146280297Sjkim# endif 147280297Sjkim while (len >= 16) { 148280297Sjkim (*block) (ivec, ecount_buf, key); 149280297Sjkim ctr128_inc_aligned(ivec); 150296279Sjkim for (n = 0; n < 16; n += sizeof(size_t)) 151280297Sjkim *(size_t *)(out + n) = 152280297Sjkim *(size_t *)(in + n) ^ *(size_t *)(ecount_buf + n); 153280297Sjkim len -= 16; 154280297Sjkim out += 16; 155280297Sjkim in += 16; 156280297Sjkim n = 0; 157280297Sjkim } 158280297Sjkim if (len) { 159280297Sjkim (*block) (ivec, ecount_buf, key); 160280297Sjkim ctr128_inc_aligned(ivec); 161280297Sjkim while (len--) { 162280297Sjkim out[n] = in[n] ^ ecount_buf[n]; 163280297Sjkim ++n; 164280297Sjkim } 165280297Sjkim } 166280297Sjkim *num = n; 167280297Sjkim return; 168280297Sjkim } while (0); 169280297Sjkim } 170280297Sjkim /* the rest would be commonly eliminated by x86* compiler */ 171238384Sjkim#endif 172280297Sjkim while (l < len) { 173280297Sjkim if (n == 0) { 174280297Sjkim (*block) (ivec, ecount_buf, key); 175280297Sjkim ctr128_inc(ivec); 176280297Sjkim } 177280297Sjkim out[l] = in[l] ^ ecount_buf[n]; 178280297Sjkim ++l; 179280297Sjkim n = (n + 1) % 16; 180280297Sjkim } 181238384Sjkim 182280297Sjkim *num = n; 183238384Sjkim} 184238384Sjkim 185238384Sjkim/* increment upper 96 bits of 128-bit counter by 1 */ 186280297Sjkimstatic void ctr96_inc(unsigned char *counter) 187280297Sjkim{ 188296279Sjkim u32 n = 12, c = 1; 189238384Sjkim 190280297Sjkim do { 191280297Sjkim --n; 192296279Sjkim c += counter[n]; 193296279Sjkim counter[n] = (u8)c; 194296279Sjkim c >>= 8; 195280297Sjkim } while (n); 196238384Sjkim} 197238384Sjkim 198238384Sjkimvoid CRYPTO_ctr128_encrypt_ctr32(const unsigned char *in, unsigned char *out, 199280297Sjkim size_t len, const void *key, 200280297Sjkim unsigned char ivec[16], 201280297Sjkim unsigned char ecount_buf[16], 202280297Sjkim unsigned int *num, ctr128_f func) 203238384Sjkim{ 204280297Sjkim unsigned int n, ctr32; 205238384Sjkim 206280297Sjkim assert(in && out && key && ecount_buf && num); 207280297Sjkim assert(*num < 16); 208238384Sjkim 209280297Sjkim n = *num; 210238384Sjkim 211280297Sjkim while (n && len) { 212280297Sjkim *(out++) = *(in++) ^ ecount_buf[n]; 213280297Sjkim --len; 214280297Sjkim n = (n + 1) % 16; 215280297Sjkim } 216238384Sjkim 217280297Sjkim ctr32 = GETU32(ivec + 12); 218280297Sjkim while (len >= 16) { 219280297Sjkim size_t blocks = len / 16; 220280297Sjkim /* 221280297Sjkim * 1<<28 is just a not-so-small yet not-so-large number... 222280297Sjkim * Below condition is practically never met, but it has to 223280297Sjkim * be checked for code correctness. 224280297Sjkim */ 225280297Sjkim if (sizeof(size_t) > sizeof(unsigned int) && blocks > (1U << 28)) 226280297Sjkim blocks = (1U << 28); 227280297Sjkim /* 228280297Sjkim * As (*func) operates on 32-bit counter, caller 229280297Sjkim * has to handle overflow. 'if' below detects the 230280297Sjkim * overflow, which is then handled by limiting the 231280297Sjkim * amount of blocks to the exact overflow point... 232280297Sjkim */ 233280297Sjkim ctr32 += (u32)blocks; 234280297Sjkim if (ctr32 < blocks) { 235280297Sjkim blocks -= ctr32; 236280297Sjkim ctr32 = 0; 237280297Sjkim } 238280297Sjkim (*func) (in, out, blocks, key, ivec); 239280297Sjkim /* (*ctr) does not update ivec, caller does: */ 240280297Sjkim PUTU32(ivec + 12, ctr32); 241280297Sjkim /* ... overflow was detected, propogate carry. */ 242280297Sjkim if (ctr32 == 0) 243280297Sjkim ctr96_inc(ivec); 244280297Sjkim blocks *= 16; 245280297Sjkim len -= blocks; 246280297Sjkim out += blocks; 247280297Sjkim in += blocks; 248280297Sjkim } 249280297Sjkim if (len) { 250280297Sjkim memset(ecount_buf, 0, 16); 251280297Sjkim (*func) (ecount_buf, ecount_buf, 1, key, ivec); 252280297Sjkim ++ctr32; 253280297Sjkim PUTU32(ivec + 12, ctr32); 254280297Sjkim if (ctr32 == 0) 255280297Sjkim ctr96_inc(ivec); 256280297Sjkim while (len--) { 257280297Sjkim out[n] = in[n] ^ ecount_buf[n]; 258280297Sjkim ++n; 259280297Sjkim } 260280297Sjkim } 261238384Sjkim 262280297Sjkim *num = n; 263238384Sjkim} 264