Fix EVP_DigestSign interface when used with DES CMAC
[openssl.git] / providers / implementations / ciphers / cipher_des_hw.c
1 /*
2  * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 /*
11  * DES low level APIs are deprecated for public use, but still ok for internal
12  * use.
13  */
14 #include "internal/deprecated.h"
15
16 #include "prov/ciphercommon.h"
17 #include "cipher_des.h"
18
19 static int cipher_hw_des_initkey(PROV_CIPHER_CTX *ctx,
20                                  const unsigned char *key, size_t keylen)
21 {
22     PROV_DES_CTX *dctx = (PROV_DES_CTX *)ctx;
23     DES_cblock *deskey = (DES_cblock *)key;
24     DES_key_schedule *ks = &dctx->dks.ks;
25
26     dctx->dstream.cbc = NULL;
27 #if defined(SPARC_DES_CAPABLE)
28     if (SPARC_DES_CAPABLE) {
29         if (ctx->mode == EVP_CIPH_CBC_MODE) {
30             des_t4_key_expand(&deskey[0], ks);
31             dctx->dstream.cbc = ctx->enc ? des_t4_cbc_encrypt :
32                                            des_t4_cbc_decrypt;
33             return 1;
34         }
35     }
36 #endif
37     DES_set_key_unchecked(deskey, ks);
38     return 1;
39 }
40
41 static void cipher_hw_des_copyctx(PROV_CIPHER_CTX *dst,
42                                   const PROV_CIPHER_CTX *src)
43 {
44     PROV_DES_CTX *sctx = (PROV_DES_CTX *)src;
45     PROV_DES_CTX *dctx = (PROV_DES_CTX *)dst;
46
47     *dctx = *sctx;
48     dst->ks = &dctx->dks.ks;
49 }
50
51 static int cipher_hw_des_ecb_cipher(PROV_CIPHER_CTX *ctx, unsigned char *out,
52                                     const unsigned char *in, size_t len)
53 {
54     size_t i, bl = ctx->blocksize;
55     DES_key_schedule *key = &(((PROV_DES_CTX *)ctx)->dks.ks);
56
57     if (len < bl)
58         return 1;
59     for (i = 0, len -= bl; i <= len; i += bl)
60         DES_ecb_encrypt((const_DES_cblock *)(in + i),
61                         (const_DES_cblock *)(out + i), key, ctx->enc);
62     return 1;
63 }
64
65 static int cipher_hw_des_cbc_cipher(PROV_CIPHER_CTX *ctx, unsigned char *out,
66                                     const unsigned char *in, size_t len)
67 {
68     DES_key_schedule *key = &(((PROV_DES_CTX *)ctx)->dks.ks);
69
70     while (len >= MAXCHUNK) {
71         DES_ncbc_encrypt(in, out, MAXCHUNK, key, (DES_cblock *)ctx->iv,
72                          ctx->enc);
73         len -= MAXCHUNK;
74         in += MAXCHUNK;
75         out += MAXCHUNK;
76     }
77     if (len > 0)
78         DES_ncbc_encrypt(in, out, (long)len, key, (DES_cblock *)ctx->iv,
79                          ctx->enc);
80     return 1;
81 }
82
83 static int cipher_hw_des_ofb64_cipher(PROV_CIPHER_CTX *ctx, unsigned char *out,
84                                       const unsigned char *in, size_t len)
85 {
86     int num = ctx->num;
87     DES_key_schedule *key = &(((PROV_DES_CTX *)ctx)->dks.ks);
88
89     while (len >= MAXCHUNK) {
90         DES_ofb64_encrypt(in, out, MAXCHUNK, key, (DES_cblock *)ctx->iv, &num);
91         len -= MAXCHUNK;
92         in += MAXCHUNK;
93         out += MAXCHUNK;
94     }
95     if (len > 0) {
96         DES_ofb64_encrypt(in, out, (long)len, key, (DES_cblock *)ctx->iv, &num);
97     }
98     ctx->num = num;
99     return 1;
100 }
101
102 static int cipher_hw_des_cfb64_cipher(PROV_CIPHER_CTX *ctx, unsigned char *out,
103                                       const unsigned char *in, size_t len)
104 {
105     size_t chunk = MAXCHUNK;
106     DES_key_schedule *key = &(((PROV_DES_CTX *)ctx)->dks.ks);
107     int num = ctx->num;
108
109     if (len < chunk)
110         chunk = len;
111     while (len > 0 && len >= chunk) {
112         DES_cfb64_encrypt(in, out, (long)chunk, key, (DES_cblock *)ctx->iv,
113                           &num, ctx->enc);
114         len -= chunk;
115         in += chunk;
116         out += chunk;
117         if (len < chunk)
118             chunk = len;
119     }
120     ctx->num = num;
121     return 1;
122 }
123
124 /*
125  * Although we have a CFB-r implementation for DES, it doesn't pack the right
126  * way, so wrap it here
127  */
128 static int cipher_hw_des_cfb1_cipher(PROV_CIPHER_CTX *ctx, unsigned char *out,
129                                      const unsigned char *in, size_t inl)
130 {
131     size_t n, chunk = MAXCHUNK / 8;
132     DES_key_schedule *key = &(((PROV_DES_CTX *)ctx)->dks.ks);
133     unsigned char c[1], d[1];
134
135     if (inl < chunk)
136         chunk = inl;
137
138     while (inl && inl >= chunk) {
139         for (n = 0; n < chunk * 8; ++n) {
140             c[0] = (in[n / 8] & (1 << (7 - n % 8))) ? 0x80 : 0;
141             DES_cfb_encrypt(c, d, 1, 1, key, (DES_cblock *)ctx->iv, ctx->enc);
142             out[n / 8] =
143                 (out[n / 8] & ~(0x80 >> (unsigned int)(n % 8))) |
144                 ((d[0] & 0x80) >> (unsigned int)(n % 8));
145         }
146         inl -= chunk;
147         in += chunk;
148         out += chunk;
149         if (inl < chunk)
150             chunk = inl;
151     }
152
153     return 1;
154 }
155
156 static int cipher_hw_des_cfb8_cipher(PROV_CIPHER_CTX *ctx, unsigned char *out,
157                                      const unsigned char *in, size_t inl)
158 {
159     DES_key_schedule *key = &(((PROV_DES_CTX *)ctx)->dks.ks);
160
161     while (inl >= MAXCHUNK) {
162         DES_cfb_encrypt(in, out, 8, (long)MAXCHUNK, key,
163                         (DES_cblock *)ctx->iv, ctx->enc);
164         inl -= MAXCHUNK;
165         in += MAXCHUNK;
166         out += MAXCHUNK;
167     }
168     if (inl > 0)
169         DES_cfb_encrypt(in, out, 8, (long)inl, key,
170                         (DES_cblock *)ctx->iv, ctx->enc);
171     return 1;
172 }
173
174 #define PROV_CIPHER_HW_des_mode(mode)                                          \
175 static const PROV_CIPHER_HW des_##mode = {                                     \
176     cipher_hw_des_initkey,                                                     \
177     cipher_hw_des_##mode##_cipher,                                             \
178     cipher_hw_des_copyctx                                                      \
179 };                                                                             \
180 const PROV_CIPHER_HW *PROV_CIPHER_HW_des_##mode(void)                          \
181 {                                                                              \
182     return &des_##mode;                                                        \
183 }
184
185 PROV_CIPHER_HW_des_mode(ecb)
186 PROV_CIPHER_HW_des_mode(cbc)
187 PROV_CIPHER_HW_des_mode(ofb64)
188 PROV_CIPHER_HW_des_mode(cfb64)
189 PROV_CIPHER_HW_des_mode(cfb1)
190 PROV_CIPHER_HW_des_mode(cfb8)