security/nss/lib/freebl/desblapi.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/lib/freebl/desblapi.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,273 @@
     1.4 +/*
     1.5 + *  desblapi.c
     1.6 + *
     1.7 + *  core source file for DES-150 library
     1.8 + *  Implement DES Modes of Operation and Triple-DES.
     1.9 + *  Adapt DES-150 to blapi API.
    1.10 + *
    1.11 + * This Source Code Form is subject to the terms of the Mozilla Public
    1.12 + * License, v. 2.0. If a copy of the MPL was not distributed with this
    1.13 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
    1.14 +
    1.15 +#ifdef FREEBL_NO_DEPEND
    1.16 +#include "stubs.h"
    1.17 +#endif
    1.18 +
    1.19 +#include "des.h"
    1.20 +#include <stddef.h>
    1.21 +#include "secerr.h"
    1.22 +
    1.23 +#if defined(NSS_X86_OR_X64)
    1.24 +/* Intel X86 CPUs do unaligned loads and stores without complaint. */
    1.25 +#define COPY8B(to, from, ptr) \
    1.26 +    	HALFPTR(to)[0] = HALFPTR(from)[0]; \
    1.27 +    	HALFPTR(to)[1] = HALFPTR(from)[1]; 
    1.28 +#elif defined(USE_MEMCPY)
    1.29 +#define COPY8B(to, from, ptr) memcpy(to, from, 8)
    1.30 +#else
    1.31 +#define COPY8B(to, from, ptr) \
    1.32 +    if (((ptrdiff_t)(ptr) & 0x3) == 0) { \
    1.33 +    	HALFPTR(to)[0] = HALFPTR(from)[0]; \
    1.34 +    	HALFPTR(to)[1] = HALFPTR(from)[1]; \
    1.35 +    } else if (((ptrdiff_t)(ptr) & 0x1) == 0) { \
    1.36 +    	SHORTPTR(to)[0] = SHORTPTR(from)[0]; \
    1.37 +    	SHORTPTR(to)[1] = SHORTPTR(from)[1]; \
    1.38 +    	SHORTPTR(to)[2] = SHORTPTR(from)[2]; \
    1.39 +    	SHORTPTR(to)[3] = SHORTPTR(from)[3]; \
    1.40 +    } else { \
    1.41 +    	BYTEPTR(to)[0] = BYTEPTR(from)[0]; \
    1.42 +    	BYTEPTR(to)[1] = BYTEPTR(from)[1]; \
    1.43 +    	BYTEPTR(to)[2] = BYTEPTR(from)[2]; \
    1.44 +    	BYTEPTR(to)[3] = BYTEPTR(from)[3]; \
    1.45 +    	BYTEPTR(to)[4] = BYTEPTR(from)[4]; \
    1.46 +    	BYTEPTR(to)[5] = BYTEPTR(from)[5]; \
    1.47 +    	BYTEPTR(to)[6] = BYTEPTR(from)[6]; \
    1.48 +    	BYTEPTR(to)[7] = BYTEPTR(from)[7]; \
    1.49 +    } 
    1.50 +#endif
    1.51 +#define COPY8BTOHALF(to, from) COPY8B(to, from, from)
    1.52 +#define COPY8BFROMHALF(to, from) COPY8B(to, from, to)
    1.53 +
    1.54 +static void 
    1.55 +DES_ECB(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len)
    1.56 +{
    1.57 +    while (len) {
    1.58 +	DES_Do1Block(cx->ks0, in, out);
    1.59 +	len -= 8;
    1.60 +	in  += 8;
    1.61 +	out += 8;
    1.62 +    }
    1.63 +}
    1.64 +
    1.65 +static void 
    1.66 +DES_EDE3_ECB(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len)
    1.67 +{
    1.68 +    while (len) {
    1.69 +	DES_Do1Block(cx->ks0,  in, out);
    1.70 +	len -= 8;
    1.71 +	in  += 8;
    1.72 +	DES_Do1Block(cx->ks1, out, out);
    1.73 +	DES_Do1Block(cx->ks2, out, out);
    1.74 +	out += 8;
    1.75 +    }
    1.76 +}
    1.77 +
    1.78 +static void 
    1.79 +DES_CBCEn(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len)
    1.80 +{
    1.81 +    const BYTE * bufend = in + len;
    1.82 +    HALF  vec[2];
    1.83 +
    1.84 +    while (in != bufend) {
    1.85 +	COPY8BTOHALF(vec, in);
    1.86 +	in += 8;
    1.87 +	vec[0] ^= cx->iv[0];
    1.88 +	vec[1] ^= cx->iv[1];
    1.89 +	DES_Do1Block( cx->ks0, (BYTE *)vec, (BYTE *)cx->iv);
    1.90 +	COPY8BFROMHALF(out, cx->iv);
    1.91 +	out += 8;
    1.92 +    }
    1.93 +}
    1.94 +
    1.95 +static void 
    1.96 +DES_CBCDe(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len)
    1.97 +{
    1.98 +    const BYTE * bufend;
    1.99 +    HALF oldciphertext[2];
   1.100 +    HALF plaintext    [2];
   1.101 +
   1.102 +    for (bufend = in + len; in != bufend; ) {
   1.103 +	oldciphertext[0] = cx->iv[0];
   1.104 +	oldciphertext[1] = cx->iv[1];
   1.105 +	COPY8BTOHALF(cx->iv, in);
   1.106 +	in += 8;
   1.107 +	DES_Do1Block(cx->ks0, (BYTE *)cx->iv, (BYTE *)plaintext);
   1.108 +	plaintext[0] ^= oldciphertext[0];
   1.109 +	plaintext[1] ^= oldciphertext[1];
   1.110 +	COPY8BFROMHALF(out, plaintext);
   1.111 +	out += 8;
   1.112 +    }
   1.113 +}
   1.114 +
   1.115 +static void 
   1.116 +DES_EDE3CBCEn(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len)
   1.117 +{
   1.118 +    const BYTE * bufend = in + len;
   1.119 +    HALF  vec[2];
   1.120 +
   1.121 +    while (in != bufend) {
   1.122 +	COPY8BTOHALF(vec, in);
   1.123 +	in += 8;
   1.124 +	vec[0] ^= cx->iv[0];
   1.125 +	vec[1] ^= cx->iv[1];
   1.126 +	DES_Do1Block( cx->ks0, (BYTE *)vec,    (BYTE *)cx->iv);
   1.127 +	DES_Do1Block( cx->ks1, (BYTE *)cx->iv, (BYTE *)cx->iv);
   1.128 +	DES_Do1Block( cx->ks2, (BYTE *)cx->iv, (BYTE *)cx->iv);
   1.129 +	COPY8BFROMHALF(out, cx->iv);
   1.130 +	out += 8;
   1.131 +    }
   1.132 +}
   1.133 +
   1.134 +static void 
   1.135 +DES_EDE3CBCDe(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len)
   1.136 +{
   1.137 +    const BYTE * bufend;
   1.138 +    HALF oldciphertext[2];
   1.139 +    HALF plaintext    [2];
   1.140 +
   1.141 +    for (bufend = in + len; in != bufend; ) {
   1.142 +	oldciphertext[0] = cx->iv[0];
   1.143 +	oldciphertext[1] = cx->iv[1];
   1.144 +	COPY8BTOHALF(cx->iv, in);
   1.145 +	in += 8;
   1.146 +	DES_Do1Block(cx->ks0, (BYTE *)cx->iv,    (BYTE *)plaintext);
   1.147 +	DES_Do1Block(cx->ks1, (BYTE *)plaintext, (BYTE *)plaintext);
   1.148 +	DES_Do1Block(cx->ks2, (BYTE *)plaintext, (BYTE *)plaintext);
   1.149 +	plaintext[0] ^= oldciphertext[0];
   1.150 +	plaintext[1] ^= oldciphertext[1];
   1.151 +	COPY8BFROMHALF(out, plaintext);
   1.152 +	out += 8;
   1.153 +    }
   1.154 +}
   1.155 +
   1.156 +DESContext *
   1.157 +DES_AllocateContext(void)
   1.158 +{
   1.159 +    return PORT_ZNew(DESContext);
   1.160 +}
   1.161 +
   1.162 +SECStatus   
   1.163 +DES_InitContext(DESContext *cx, const unsigned char *key, unsigned int keylen,
   1.164 +	        const unsigned char *iv, int mode, unsigned int encrypt,
   1.165 +	        unsigned int unused)
   1.166 +{
   1.167 +    DESDirection opposite;
   1.168 +    if (!cx) {
   1.169 +	PORT_SetError(SEC_ERROR_INVALID_ARGS);
   1.170 +    	return SECFailure;
   1.171 +    }
   1.172 +    cx->direction = encrypt ? DES_ENCRYPT : DES_DECRYPT;
   1.173 +    opposite      = encrypt ? DES_DECRYPT : DES_ENCRYPT;
   1.174 +    switch (mode) {
   1.175 +    case NSS_DES:	/* DES ECB */
   1.176 +	DES_MakeSchedule( cx->ks0, key, cx->direction);
   1.177 +	cx->worker = &DES_ECB;
   1.178 +	break;
   1.179 +
   1.180 +    case NSS_DES_EDE3:	/* DES EDE ECB */
   1.181 +	cx->worker = &DES_EDE3_ECB;
   1.182 +	if (encrypt) {
   1.183 +	    DES_MakeSchedule(cx->ks0, key,      cx->direction);
   1.184 +	    DES_MakeSchedule(cx->ks1, key +  8, opposite);
   1.185 +	    DES_MakeSchedule(cx->ks2, key + 16, cx->direction);
   1.186 +	} else {
   1.187 +	    DES_MakeSchedule(cx->ks2, key,      cx->direction);
   1.188 +	    DES_MakeSchedule(cx->ks1, key +  8, opposite);
   1.189 +	    DES_MakeSchedule(cx->ks0, key + 16, cx->direction);
   1.190 +	}
   1.191 +	break;
   1.192 +
   1.193 +    case NSS_DES_CBC:	/* DES CBC */
   1.194 +	COPY8BTOHALF(cx->iv, iv);
   1.195 +	cx->worker = encrypt ? &DES_CBCEn : &DES_CBCDe;
   1.196 +	DES_MakeSchedule(cx->ks0, key, cx->direction);
   1.197 +	break;
   1.198 +
   1.199 +    case NSS_DES_EDE3_CBC:	/* DES EDE CBC */
   1.200 +	COPY8BTOHALF(cx->iv, iv);
   1.201 +	if (encrypt) {
   1.202 +	    cx->worker = &DES_EDE3CBCEn;
   1.203 +	    DES_MakeSchedule(cx->ks0, key,      cx->direction);
   1.204 +	    DES_MakeSchedule(cx->ks1, key +  8, opposite);
   1.205 +	    DES_MakeSchedule(cx->ks2, key + 16, cx->direction);
   1.206 +	} else {
   1.207 +	    cx->worker = &DES_EDE3CBCDe;
   1.208 +	    DES_MakeSchedule(cx->ks2, key,      cx->direction);
   1.209 +	    DES_MakeSchedule(cx->ks1, key +  8, opposite);
   1.210 +	    DES_MakeSchedule(cx->ks0, key + 16, cx->direction);
   1.211 +	}
   1.212 +	break;
   1.213 +
   1.214 +    default:
   1.215 +	PORT_SetError(SEC_ERROR_INVALID_ARGS);
   1.216 +	return SECFailure;
   1.217 +    }
   1.218 +    return SECSuccess;
   1.219 +}
   1.220 +
   1.221 +DESContext *
   1.222 +DES_CreateContext(const BYTE * key, const BYTE *iv, int mode, PRBool encrypt)
   1.223 +{
   1.224 +    DESContext *cx = PORT_ZNew(DESContext);
   1.225 +    SECStatus rv   = DES_InitContext(cx, key, 0, iv, mode, encrypt, 0);
   1.226 +
   1.227 +    if (rv != SECSuccess) {
   1.228 +    	PORT_ZFree(cx, sizeof *cx);
   1.229 +	cx = NULL;
   1.230 +    }
   1.231 +    return cx;
   1.232 +}
   1.233 +
   1.234 +void
   1.235 +DES_DestroyContext(DESContext *cx, PRBool freeit)
   1.236 +{
   1.237 +    if (cx) {
   1.238 +    	memset(cx, 0, sizeof *cx);
   1.239 +	if (freeit)
   1.240 +	    PORT_Free(cx);
   1.241 +    }
   1.242 +}
   1.243 +
   1.244 +SECStatus
   1.245 +DES_Encrypt(DESContext *cx, BYTE *out, unsigned int *outLen,
   1.246 +            unsigned int maxOutLen, const BYTE *in, unsigned int inLen)
   1.247 +{
   1.248 +
   1.249 +    if ((inLen % 8) != 0 || maxOutLen < inLen || !cx || 
   1.250 +        cx->direction != DES_ENCRYPT) {
   1.251 +    	PORT_SetError(SEC_ERROR_INVALID_ARGS);
   1.252 +	return SECFailure;
   1.253 +    }
   1.254 +
   1.255 +    cx->worker(cx, out, in, inLen);
   1.256 +    if (outLen)
   1.257 +	*outLen = inLen;
   1.258 +    return SECSuccess;
   1.259 +}
   1.260 +
   1.261 +SECStatus
   1.262 +DES_Decrypt(DESContext *cx, BYTE *out, unsigned int *outLen,
   1.263 +            unsigned int maxOutLen, const BYTE *in, unsigned int inLen)
   1.264 +{
   1.265 +
   1.266 +    if ((inLen % 8) != 0 || maxOutLen < inLen || !cx || 
   1.267 +        cx->direction != DES_DECRYPT) {
   1.268 +    	PORT_SetError(SEC_ERROR_INVALID_ARGS);
   1.269 +	return SECFailure;
   1.270 +    }
   1.271 +
   1.272 +    cx->worker(cx, out, in, inLen);
   1.273 +    if (outLen)
   1.274 +	*outLen = inLen;
   1.275 +    return SECSuccess;
   1.276 +}

mercurial