/src/nss/lib/freebl/desblapi.c
Line | Count | Source |
1 | | /* |
2 | | * desblapi.c |
3 | | * |
4 | | * core source file for DES-150 library |
5 | | * Implement DES Modes of Operation and Triple-DES. |
6 | | * Adapt DES-150 to blapi API. |
7 | | * |
8 | | * This Source Code Form is subject to the terms of the Mozilla Public |
9 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
10 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
11 | | |
12 | | #ifdef FREEBL_NO_DEPEND |
13 | | #include "stubs.h" |
14 | | #endif |
15 | | |
16 | | #include "des.h" |
17 | | #include "blapii.h" |
18 | | #include <stddef.h> |
19 | | #include "secerr.h" |
20 | | |
21 | | #if defined(NSS_X86_OR_X64) |
22 | | /* Intel X86 CPUs do unaligned loads and stores without complaint. */ |
23 | | #define COPY8B(to, from, ptr) \ |
24 | 355k | HALFPTR(to) \ |
25 | 355k | [0] = HALFPTR(from)[0]; \ |
26 | 355k | HALFPTR(to) \ |
27 | 355k | [1] = HALFPTR(from)[1]; |
28 | | #else |
29 | | #define COPY8B(to, from, ptr) memcpy(to, from, 8) |
30 | | #endif |
31 | 196k | #define COPY8BTOHALF(to, from) COPY8B(to, from, from) |
32 | 158k | #define COPY8BFROMHALF(to, from) COPY8B(to, from, to) |
33 | | |
34 | | static void |
35 | | DES_ECB(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len) |
36 | 74 | { |
37 | 221 | while (len) { |
38 | 147 | DES_Do1Block(cx->ks0, in, out); |
39 | 147 | len -= 8; |
40 | 147 | in += 8; |
41 | 147 | out += 8; |
42 | 147 | } |
43 | 74 | } |
44 | | |
45 | | static void |
46 | | DES_EDE3_ECB(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len) |
47 | 49.1k | { |
48 | 343k | while (len) { |
49 | 294k | DES_Do1Block(cx->ks0, in, out); |
50 | 294k | len -= 8; |
51 | 294k | in += 8; |
52 | 294k | DES_Do1Block(cx->ks1, out, out); |
53 | 294k | DES_Do1Block(cx->ks2, out, out); |
54 | 294k | out += 8; |
55 | 294k | } |
56 | 49.1k | } |
57 | | |
58 | | static void NO_SANITIZE_ALIGNMENT |
59 | | DES_CBCEn(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len) |
60 | 124 | { |
61 | 124 | const BYTE *bufend = in + len; |
62 | 124 | HALF vec[2]; |
63 | | |
64 | 580 | while (in != bufend) { |
65 | 456 | COPY8BTOHALF(vec, in); |
66 | 456 | in += 8; |
67 | 456 | vec[0] ^= cx->iv[0]; |
68 | 456 | vec[1] ^= cx->iv[1]; |
69 | 456 | DES_Do1Block(cx->ks0, (BYTE *)vec, (BYTE *)cx->iv); |
70 | 456 | COPY8BFROMHALF(out, cx->iv); |
71 | 456 | out += 8; |
72 | 456 | } |
73 | 124 | } |
74 | | |
75 | | static void NO_SANITIZE_ALIGNMENT |
76 | | DES_CBCDe(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len) |
77 | 2.52k | { |
78 | 2.52k | const BYTE *bufend; |
79 | 2.52k | HALF oldciphertext[2]; |
80 | 2.52k | HALF plaintext[2]; |
81 | | |
82 | 55.2k | for (bufend = in + len; in != bufend;) { |
83 | 52.6k | oldciphertext[0] = cx->iv[0]; |
84 | 52.6k | oldciphertext[1] = cx->iv[1]; |
85 | 52.6k | COPY8BTOHALF(cx->iv, in); |
86 | 52.6k | in += 8; |
87 | 52.6k | DES_Do1Block(cx->ks0, (BYTE *)cx->iv, (BYTE *)plaintext); |
88 | 52.6k | plaintext[0] ^= oldciphertext[0]; |
89 | 52.6k | plaintext[1] ^= oldciphertext[1]; |
90 | 52.6k | COPY8BFROMHALF(out, plaintext); |
91 | 52.6k | out += 8; |
92 | 52.6k | } |
93 | 2.52k | } |
94 | | |
95 | | static void NO_SANITIZE_ALIGNMENT |
96 | | DES_EDE3CBCEn(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len) |
97 | 1.02k | { |
98 | 1.02k | const BYTE *bufend = in + len; |
99 | 1.02k | HALF vec[2]; |
100 | | |
101 | 4.55k | while (in != bufend) { |
102 | 3.53k | COPY8BTOHALF(vec, in); |
103 | 3.53k | in += 8; |
104 | 3.53k | vec[0] ^= cx->iv[0]; |
105 | 3.53k | vec[1] ^= cx->iv[1]; |
106 | 3.53k | DES_Do1Block(cx->ks0, (BYTE *)vec, (BYTE *)cx->iv); |
107 | 3.53k | DES_Do1Block(cx->ks1, (BYTE *)cx->iv, (BYTE *)cx->iv); |
108 | 3.53k | DES_Do1Block(cx->ks2, (BYTE *)cx->iv, (BYTE *)cx->iv); |
109 | 3.53k | COPY8BFROMHALF(out, cx->iv); |
110 | 3.53k | out += 8; |
111 | 3.53k | } |
112 | 1.02k | } |
113 | | |
114 | | static void NO_SANITIZE_ALIGNMENT |
115 | | DES_EDE3CBCDe(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len) |
116 | 19.3k | { |
117 | 19.3k | const BYTE *bufend; |
118 | 19.3k | HALF oldciphertext[2]; |
119 | 19.3k | HALF plaintext[2]; |
120 | | |
121 | 121k | for (bufend = in + len; in != bufend;) { |
122 | 102k | oldciphertext[0] = cx->iv[0]; |
123 | 102k | oldciphertext[1] = cx->iv[1]; |
124 | 102k | COPY8BTOHALF(cx->iv, in); |
125 | 102k | in += 8; |
126 | 102k | DES_Do1Block(cx->ks0, (BYTE *)cx->iv, (BYTE *)plaintext); |
127 | 102k | DES_Do1Block(cx->ks1, (BYTE *)plaintext, (BYTE *)plaintext); |
128 | 102k | DES_Do1Block(cx->ks2, (BYTE *)plaintext, (BYTE *)plaintext); |
129 | 102k | plaintext[0] ^= oldciphertext[0]; |
130 | 102k | plaintext[1] ^= oldciphertext[1]; |
131 | 102k | COPY8BFROMHALF(out, plaintext); |
132 | 102k | out += 8; |
133 | 102k | } |
134 | 19.3k | } |
135 | | |
136 | | DESContext * |
137 | | DES_AllocateContext(void) |
138 | 0 | { |
139 | 0 | return PORT_ZNew(DESContext); |
140 | 0 | } |
141 | | |
142 | | SECStatus |
143 | | DES_InitContext(DESContext *cx, const unsigned char *key, unsigned int keylen, |
144 | | const unsigned char *iv, int mode, unsigned int encrypt, |
145 | | unsigned int unused) |
146 | 86.9k | { |
147 | 86.9k | DESDirection opposite; |
148 | 86.9k | if (!cx) { |
149 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
150 | 0 | return SECFailure; |
151 | 0 | } |
152 | 86.9k | cx->direction = encrypt ? DES_ENCRYPT : DES_DECRYPT; |
153 | 86.9k | opposite = encrypt ? DES_DECRYPT : DES_ENCRYPT; |
154 | 86.9k | switch (mode) { |
155 | 103 | case NSS_DES: /* DES ECB */ |
156 | 103 | DES_MakeSchedule(cx->ks0, key, cx->direction); |
157 | 103 | cx->worker = &DES_ECB; |
158 | 103 | break; |
159 | | |
160 | 49.1k | case NSS_DES_EDE3: /* DES EDE ECB */ |
161 | 49.1k | cx->worker = &DES_EDE3_ECB; |
162 | 49.1k | if (encrypt) { |
163 | 49.1k | DES_MakeSchedule(cx->ks0, key, cx->direction); |
164 | 49.1k | DES_MakeSchedule(cx->ks1, key + 8, opposite); |
165 | 49.1k | DES_MakeSchedule(cx->ks2, key + 16, cx->direction); |
166 | 49.1k | } else { |
167 | 0 | DES_MakeSchedule(cx->ks2, key, cx->direction); |
168 | 0 | DES_MakeSchedule(cx->ks1, key + 8, opposite); |
169 | 0 | DES_MakeSchedule(cx->ks0, key + 16, cx->direction); |
170 | 0 | } |
171 | 49.1k | break; |
172 | | |
173 | 17.3k | case NSS_DES_CBC: /* DES CBC */ |
174 | 17.3k | COPY8BTOHALF(cx->iv, iv); |
175 | 17.3k | cx->worker = encrypt ? &DES_CBCEn : &DES_CBCDe; |
176 | 17.3k | DES_MakeSchedule(cx->ks0, key, cx->direction); |
177 | 17.3k | break; |
178 | | |
179 | 20.3k | case NSS_DES_EDE3_CBC: /* DES EDE CBC */ |
180 | 20.3k | COPY8BTOHALF(cx->iv, iv); |
181 | 20.3k | if (encrypt) { |
182 | 10.1k | cx->worker = &DES_EDE3CBCEn; |
183 | 10.1k | DES_MakeSchedule(cx->ks0, key, cx->direction); |
184 | 10.1k | DES_MakeSchedule(cx->ks1, key + 8, opposite); |
185 | 10.1k | DES_MakeSchedule(cx->ks2, key + 16, cx->direction); |
186 | 10.2k | } else { |
187 | 10.2k | cx->worker = &DES_EDE3CBCDe; |
188 | 10.2k | DES_MakeSchedule(cx->ks2, key, cx->direction); |
189 | 10.2k | DES_MakeSchedule(cx->ks1, key + 8, opposite); |
190 | 10.2k | DES_MakeSchedule(cx->ks0, key + 16, cx->direction); |
191 | 10.2k | } |
192 | 20.3k | break; |
193 | | |
194 | 0 | default: |
195 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
196 | 0 | return SECFailure; |
197 | 86.9k | } |
198 | 86.9k | return SECSuccess; |
199 | 86.9k | } |
200 | | |
201 | | DESContext * |
202 | | DES_CreateContext(const BYTE *key, const BYTE *iv, int mode, PRBool encrypt) |
203 | 86.9k | { |
204 | 86.9k | DESContext *cx = PORT_ZNew(DESContext); |
205 | 86.9k | SECStatus rv = DES_InitContext(cx, key, 0, iv, mode, encrypt, 0); |
206 | | |
207 | 86.9k | if (rv != SECSuccess) { |
208 | 0 | PORT_ZFree(cx, sizeof *cx); |
209 | 0 | cx = NULL; |
210 | 0 | } |
211 | 86.9k | return cx; |
212 | 86.9k | } |
213 | | |
214 | | void |
215 | | DES_DestroyContext(DESContext *cx, PRBool freeit) |
216 | 86.9k | { |
217 | 86.9k | if (cx) { |
218 | 86.9k | memset(cx, 0, sizeof *cx); |
219 | 86.9k | if (freeit) |
220 | 86.9k | PORT_Free(cx); |
221 | 86.9k | } |
222 | 86.9k | } |
223 | | |
224 | | SECStatus |
225 | | DES_Encrypt(DESContext *cx, BYTE *out, unsigned int *outLen, |
226 | | unsigned int maxOutLen, const BYTE *in, unsigned int inLen) |
227 | 50.3k | { |
228 | | |
229 | 50.3k | if ((inLen % 8) != 0 || maxOutLen < inLen || !cx || |
230 | 50.3k | cx->direction != DES_ENCRYPT) { |
231 | 26 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
232 | 26 | return SECFailure; |
233 | 26 | } |
234 | | |
235 | 50.3k | cx->worker(cx, out, in, inLen); |
236 | 50.3k | if (outLen) |
237 | 50.3k | *outLen = inLen; |
238 | 50.3k | return SECSuccess; |
239 | 50.3k | } |
240 | | |
241 | | SECStatus |
242 | | DES_Decrypt(DESContext *cx, BYTE *out, unsigned int *outLen, |
243 | | unsigned int maxOutLen, const BYTE *in, unsigned int inLen) |
244 | 21.9k | { |
245 | | |
246 | 21.9k | if ((inLen % 8) != 0 || maxOutLen < inLen || !cx || |
247 | 21.9k | cx->direction != DES_DECRYPT) { |
248 | 20 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
249 | 20 | return SECFailure; |
250 | 20 | } |
251 | | |
252 | 21.9k | cx->worker(cx, out, in, inLen); |
253 | 21.9k | if (outLen) |
254 | 21.9k | *outLen = inLen; |
255 | 21.9k | return SECSuccess; |
256 | 21.9k | } |