michael@0: /* michael@0: * desblapi.c michael@0: * michael@0: * core source file for DES-150 library michael@0: * Implement DES Modes of Operation and Triple-DES. michael@0: * Adapt DES-150 to blapi API. michael@0: * michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifdef FREEBL_NO_DEPEND michael@0: #include "stubs.h" michael@0: #endif michael@0: michael@0: #include "des.h" michael@0: #include michael@0: #include "secerr.h" michael@0: michael@0: #if defined(NSS_X86_OR_X64) michael@0: /* Intel X86 CPUs do unaligned loads and stores without complaint. */ michael@0: #define COPY8B(to, from, ptr) \ michael@0: HALFPTR(to)[0] = HALFPTR(from)[0]; \ michael@0: HALFPTR(to)[1] = HALFPTR(from)[1]; michael@0: #elif defined(USE_MEMCPY) michael@0: #define COPY8B(to, from, ptr) memcpy(to, from, 8) michael@0: #else michael@0: #define COPY8B(to, from, ptr) \ michael@0: if (((ptrdiff_t)(ptr) & 0x3) == 0) { \ michael@0: HALFPTR(to)[0] = HALFPTR(from)[0]; \ michael@0: HALFPTR(to)[1] = HALFPTR(from)[1]; \ michael@0: } else if (((ptrdiff_t)(ptr) & 0x1) == 0) { \ michael@0: SHORTPTR(to)[0] = SHORTPTR(from)[0]; \ michael@0: SHORTPTR(to)[1] = SHORTPTR(from)[1]; \ michael@0: SHORTPTR(to)[2] = SHORTPTR(from)[2]; \ michael@0: SHORTPTR(to)[3] = SHORTPTR(from)[3]; \ michael@0: } else { \ michael@0: BYTEPTR(to)[0] = BYTEPTR(from)[0]; \ michael@0: BYTEPTR(to)[1] = BYTEPTR(from)[1]; \ michael@0: BYTEPTR(to)[2] = BYTEPTR(from)[2]; \ michael@0: BYTEPTR(to)[3] = BYTEPTR(from)[3]; \ michael@0: BYTEPTR(to)[4] = BYTEPTR(from)[4]; \ michael@0: BYTEPTR(to)[5] = BYTEPTR(from)[5]; \ michael@0: BYTEPTR(to)[6] = BYTEPTR(from)[6]; \ michael@0: BYTEPTR(to)[7] = BYTEPTR(from)[7]; \ michael@0: } michael@0: #endif michael@0: #define COPY8BTOHALF(to, from) COPY8B(to, from, from) michael@0: #define COPY8BFROMHALF(to, from) COPY8B(to, from, to) michael@0: michael@0: static void michael@0: DES_ECB(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len) michael@0: { michael@0: while (len) { michael@0: DES_Do1Block(cx->ks0, in, out); michael@0: len -= 8; michael@0: in += 8; michael@0: out += 8; michael@0: } michael@0: } michael@0: michael@0: static void michael@0: DES_EDE3_ECB(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len) michael@0: { michael@0: while (len) { michael@0: DES_Do1Block(cx->ks0, in, out); michael@0: len -= 8; michael@0: in += 8; michael@0: DES_Do1Block(cx->ks1, out, out); michael@0: DES_Do1Block(cx->ks2, out, out); michael@0: out += 8; michael@0: } michael@0: } michael@0: michael@0: static void michael@0: DES_CBCEn(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len) michael@0: { michael@0: const BYTE * bufend = in + len; michael@0: HALF vec[2]; michael@0: michael@0: while (in != bufend) { michael@0: COPY8BTOHALF(vec, in); michael@0: in += 8; michael@0: vec[0] ^= cx->iv[0]; michael@0: vec[1] ^= cx->iv[1]; michael@0: DES_Do1Block( cx->ks0, (BYTE *)vec, (BYTE *)cx->iv); michael@0: COPY8BFROMHALF(out, cx->iv); michael@0: out += 8; michael@0: } michael@0: } michael@0: michael@0: static void michael@0: DES_CBCDe(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len) michael@0: { michael@0: const BYTE * bufend; michael@0: HALF oldciphertext[2]; michael@0: HALF plaintext [2]; michael@0: michael@0: for (bufend = in + len; in != bufend; ) { michael@0: oldciphertext[0] = cx->iv[0]; michael@0: oldciphertext[1] = cx->iv[1]; michael@0: COPY8BTOHALF(cx->iv, in); michael@0: in += 8; michael@0: DES_Do1Block(cx->ks0, (BYTE *)cx->iv, (BYTE *)plaintext); michael@0: plaintext[0] ^= oldciphertext[0]; michael@0: plaintext[1] ^= oldciphertext[1]; michael@0: COPY8BFROMHALF(out, plaintext); michael@0: out += 8; michael@0: } michael@0: } michael@0: michael@0: static void michael@0: DES_EDE3CBCEn(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len) michael@0: { michael@0: const BYTE * bufend = in + len; michael@0: HALF vec[2]; michael@0: michael@0: while (in != bufend) { michael@0: COPY8BTOHALF(vec, in); michael@0: in += 8; michael@0: vec[0] ^= cx->iv[0]; michael@0: vec[1] ^= cx->iv[1]; michael@0: DES_Do1Block( cx->ks0, (BYTE *)vec, (BYTE *)cx->iv); michael@0: DES_Do1Block( cx->ks1, (BYTE *)cx->iv, (BYTE *)cx->iv); michael@0: DES_Do1Block( cx->ks2, (BYTE *)cx->iv, (BYTE *)cx->iv); michael@0: COPY8BFROMHALF(out, cx->iv); michael@0: out += 8; michael@0: } michael@0: } michael@0: michael@0: static void michael@0: DES_EDE3CBCDe(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len) michael@0: { michael@0: const BYTE * bufend; michael@0: HALF oldciphertext[2]; michael@0: HALF plaintext [2]; michael@0: michael@0: for (bufend = in + len; in != bufend; ) { michael@0: oldciphertext[0] = cx->iv[0]; michael@0: oldciphertext[1] = cx->iv[1]; michael@0: COPY8BTOHALF(cx->iv, in); michael@0: in += 8; michael@0: DES_Do1Block(cx->ks0, (BYTE *)cx->iv, (BYTE *)plaintext); michael@0: DES_Do1Block(cx->ks1, (BYTE *)plaintext, (BYTE *)plaintext); michael@0: DES_Do1Block(cx->ks2, (BYTE *)plaintext, (BYTE *)plaintext); michael@0: plaintext[0] ^= oldciphertext[0]; michael@0: plaintext[1] ^= oldciphertext[1]; michael@0: COPY8BFROMHALF(out, plaintext); michael@0: out += 8; michael@0: } michael@0: } michael@0: michael@0: DESContext * michael@0: DES_AllocateContext(void) michael@0: { michael@0: return PORT_ZNew(DESContext); michael@0: } michael@0: michael@0: SECStatus michael@0: DES_InitContext(DESContext *cx, const unsigned char *key, unsigned int keylen, michael@0: const unsigned char *iv, int mode, unsigned int encrypt, michael@0: unsigned int unused) michael@0: { michael@0: DESDirection opposite; michael@0: if (!cx) { michael@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); michael@0: return SECFailure; michael@0: } michael@0: cx->direction = encrypt ? DES_ENCRYPT : DES_DECRYPT; michael@0: opposite = encrypt ? DES_DECRYPT : DES_ENCRYPT; michael@0: switch (mode) { michael@0: case NSS_DES: /* DES ECB */ michael@0: DES_MakeSchedule( cx->ks0, key, cx->direction); michael@0: cx->worker = &DES_ECB; michael@0: break; michael@0: michael@0: case NSS_DES_EDE3: /* DES EDE ECB */ michael@0: cx->worker = &DES_EDE3_ECB; michael@0: if (encrypt) { michael@0: DES_MakeSchedule(cx->ks0, key, cx->direction); michael@0: DES_MakeSchedule(cx->ks1, key + 8, opposite); michael@0: DES_MakeSchedule(cx->ks2, key + 16, cx->direction); michael@0: } else { michael@0: DES_MakeSchedule(cx->ks2, key, cx->direction); michael@0: DES_MakeSchedule(cx->ks1, key + 8, opposite); michael@0: DES_MakeSchedule(cx->ks0, key + 16, cx->direction); michael@0: } michael@0: break; michael@0: michael@0: case NSS_DES_CBC: /* DES CBC */ michael@0: COPY8BTOHALF(cx->iv, iv); michael@0: cx->worker = encrypt ? &DES_CBCEn : &DES_CBCDe; michael@0: DES_MakeSchedule(cx->ks0, key, cx->direction); michael@0: break; michael@0: michael@0: case NSS_DES_EDE3_CBC: /* DES EDE CBC */ michael@0: COPY8BTOHALF(cx->iv, iv); michael@0: if (encrypt) { michael@0: cx->worker = &DES_EDE3CBCEn; michael@0: DES_MakeSchedule(cx->ks0, key, cx->direction); michael@0: DES_MakeSchedule(cx->ks1, key + 8, opposite); michael@0: DES_MakeSchedule(cx->ks2, key + 16, cx->direction); michael@0: } else { michael@0: cx->worker = &DES_EDE3CBCDe; michael@0: DES_MakeSchedule(cx->ks2, key, cx->direction); michael@0: DES_MakeSchedule(cx->ks1, key + 8, opposite); michael@0: DES_MakeSchedule(cx->ks0, key + 16, cx->direction); michael@0: } michael@0: break; michael@0: michael@0: default: michael@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); michael@0: return SECFailure; michael@0: } michael@0: return SECSuccess; michael@0: } michael@0: michael@0: DESContext * michael@0: DES_CreateContext(const BYTE * key, const BYTE *iv, int mode, PRBool encrypt) michael@0: { michael@0: DESContext *cx = PORT_ZNew(DESContext); michael@0: SECStatus rv = DES_InitContext(cx, key, 0, iv, mode, encrypt, 0); michael@0: michael@0: if (rv != SECSuccess) { michael@0: PORT_ZFree(cx, sizeof *cx); michael@0: cx = NULL; michael@0: } michael@0: return cx; michael@0: } michael@0: michael@0: void michael@0: DES_DestroyContext(DESContext *cx, PRBool freeit) michael@0: { michael@0: if (cx) { michael@0: memset(cx, 0, sizeof *cx); michael@0: if (freeit) michael@0: PORT_Free(cx); michael@0: } michael@0: } michael@0: michael@0: SECStatus michael@0: DES_Encrypt(DESContext *cx, BYTE *out, unsigned int *outLen, michael@0: unsigned int maxOutLen, const BYTE *in, unsigned int inLen) michael@0: { michael@0: michael@0: if ((inLen % 8) != 0 || maxOutLen < inLen || !cx || michael@0: cx->direction != DES_ENCRYPT) { michael@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); michael@0: return SECFailure; michael@0: } michael@0: michael@0: cx->worker(cx, out, in, inLen); michael@0: if (outLen) michael@0: *outLen = inLen; michael@0: return SECSuccess; michael@0: } michael@0: michael@0: SECStatus michael@0: DES_Decrypt(DESContext *cx, BYTE *out, unsigned int *outLen, michael@0: unsigned int maxOutLen, const BYTE *in, unsigned int inLen) michael@0: { michael@0: michael@0: if ((inLen % 8) != 0 || maxOutLen < inLen || !cx || michael@0: cx->direction != DES_DECRYPT) { michael@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); michael@0: return SECFailure; michael@0: } michael@0: michael@0: cx->worker(cx, out, in, inLen); michael@0: if (outLen) michael@0: *outLen = inLen; michael@0: return SECSuccess; michael@0: }