Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/openssl/src/crypto/cmac/cmac.c |
Switch to Source code | Preprocessed file |
Line | Source | Count | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | - | |||||||||||||||||||||||||
2 | - | |||||||||||||||||||||||||
3 | struct CMAC_CTX_st { | - | ||||||||||||||||||||||||
4 | - | |||||||||||||||||||||||||
5 | EVP_CIPHER_CTX *cctx; | - | ||||||||||||||||||||||||
6 | - | |||||||||||||||||||||||||
7 | unsigned char k1[32]; | - | ||||||||||||||||||||||||
8 | unsigned char k2[32]; | - | ||||||||||||||||||||||||
9 | - | |||||||||||||||||||||||||
10 | unsigned char tbl[32]; | - | ||||||||||||||||||||||||
11 | - | |||||||||||||||||||||||||
12 | unsigned char last_block[32]; | - | ||||||||||||||||||||||||
13 | - | |||||||||||||||||||||||||
14 | int nlast_block; | - | ||||||||||||||||||||||||
15 | }; | - | ||||||||||||||||||||||||
16 | - | |||||||||||||||||||||||||
17 | - | |||||||||||||||||||||||||
18 | - | |||||||||||||||||||||||||
19 | static void make_kn(unsigned char *k1, const unsigned char *l, int bl) | - | ||||||||||||||||||||||||
20 | { | - | ||||||||||||||||||||||||
21 | int i; | - | ||||||||||||||||||||||||
22 | unsigned char c = l[0], carry = c >> 7, cnext; | - | ||||||||||||||||||||||||
23 | - | |||||||||||||||||||||||||
24 | - | |||||||||||||||||||||||||
25 | for (i = 0; i < bl - 1
| 8-104 | ||||||||||||||||||||||||
26 | k1[i] = (c << 1) | ((cnext = l[i + 1]) >> 7); executed 104 times by 1 test: k1[i] = (c << 1) | ((cnext = l[i + 1]) >> 7); Executed by:
| 104 | ||||||||||||||||||||||||
27 | - | |||||||||||||||||||||||||
28 | - | |||||||||||||||||||||||||
29 | k1[i] = (c << 1) ^ ((0 - carry) & (bl == 16 ? 0x87 : 0x1b)); | - | ||||||||||||||||||||||||
30 | } executed 8 times by 1 test: end of block Executed by:
| 8 | ||||||||||||||||||||||||
31 | - | |||||||||||||||||||||||||
32 | CMAC_CTX *CMAC_CTX_new(void) | - | ||||||||||||||||||||||||
33 | { | - | ||||||||||||||||||||||||
34 | CMAC_CTX *ctx; | - | ||||||||||||||||||||||||
35 | - | |||||||||||||||||||||||||
36 | if ((
| 0-12 | ||||||||||||||||||||||||
37 | ((void *)0)
| 0-12 | ||||||||||||||||||||||||
38 | ) { | - | ||||||||||||||||||||||||
39 | ERR_put_error(15,(120),((1|64)),__FILE__,51); | - | ||||||||||||||||||||||||
40 | return never executed: return ((void *)0) ; never executed: return ((void *)0) ; | 0 | ||||||||||||||||||||||||
41 | ((void *)0) never executed: return ((void *)0) ; | 0 | ||||||||||||||||||||||||
42 | ; never executed: return ((void *)0) ; | 0 | ||||||||||||||||||||||||
43 | } | - | ||||||||||||||||||||||||
44 | ctx->cctx = EVP_CIPHER_CTX_new(); | - | ||||||||||||||||||||||||
45 | if (ctx->cctx ==
| 0-12 | ||||||||||||||||||||||||
46 | ((void *)0)
| 0-12 | ||||||||||||||||||||||||
47 | ) { | - | ||||||||||||||||||||||||
48 | CRYPTO_free(ctx, __FILE__, 56); | - | ||||||||||||||||||||||||
49 | return never executed: return ((void *)0) ; never executed: return ((void *)0) ; | 0 | ||||||||||||||||||||||||
50 | ((void *)0) never executed: return ((void *)0) ; | 0 | ||||||||||||||||||||||||
51 | ; never executed: return ((void *)0) ; | 0 | ||||||||||||||||||||||||
52 | } | - | ||||||||||||||||||||||||
53 | ctx->nlast_block = -1; | - | ||||||||||||||||||||||||
54 | return executed 12 times by 1 test: ctx;return ctx; Executed by:
executed 12 times by 1 test: return ctx; Executed by:
| 12 | ||||||||||||||||||||||||
55 | } | - | ||||||||||||||||||||||||
56 | - | |||||||||||||||||||||||||
57 | void CMAC_CTX_cleanup(CMAC_CTX *ctx) | - | ||||||||||||||||||||||||
58 | { | - | ||||||||||||||||||||||||
59 | EVP_CIPHER_CTX_reset(ctx->cctx); | - | ||||||||||||||||||||||||
60 | OPENSSL_cleanse(ctx->tbl, 32); | - | ||||||||||||||||||||||||
61 | OPENSSL_cleanse(ctx->k1, 32); | - | ||||||||||||||||||||||||
62 | OPENSSL_cleanse(ctx->k2, 32); | - | ||||||||||||||||||||||||
63 | OPENSSL_cleanse(ctx->last_block, 32); | - | ||||||||||||||||||||||||
64 | ctx->nlast_block = -1; | - | ||||||||||||||||||||||||
65 | } executed 12 times by 1 test: end of block Executed by:
| 12 | ||||||||||||||||||||||||
66 | - | |||||||||||||||||||||||||
67 | EVP_CIPHER_CTX *CMAC_CTX_get0_cipher_ctx(CMAC_CTX *ctx) | - | ||||||||||||||||||||||||
68 | { | - | ||||||||||||||||||||||||
69 | return never executed: ctx->cctx;return ctx->cctx; never executed: return ctx->cctx; | 0 | ||||||||||||||||||||||||
70 | } | - | ||||||||||||||||||||||||
71 | - | |||||||||||||||||||||||||
72 | void CMAC_CTX_free(CMAC_CTX *ctx) | - | ||||||||||||||||||||||||
73 | { | - | ||||||||||||||||||||||||
74 | if (!ctx
| 0-12 | ||||||||||||||||||||||||
75 | return; never executed: return; | 0 | ||||||||||||||||||||||||
76 | CMAC_CTX_cleanup(ctx); | - | ||||||||||||||||||||||||
77 | EVP_CIPHER_CTX_free(ctx->cctx); | - | ||||||||||||||||||||||||
78 | CRYPTO_free(ctx, __FILE__, 84); | - | ||||||||||||||||||||||||
79 | } executed 12 times by 1 test: end of block Executed by:
| 12 | ||||||||||||||||||||||||
80 | - | |||||||||||||||||||||||||
81 | int CMAC_CTX_copy(CMAC_CTX *out, const CMAC_CTX *in) | - | ||||||||||||||||||||||||
82 | { | - | ||||||||||||||||||||||||
83 | int bl; | - | ||||||||||||||||||||||||
84 | if (in->nlast_block == -1
| 0-8 | ||||||||||||||||||||||||
85 | return never executed: 0;return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
86 | if (!EVP_CIPHER_CTX_copy(out->cctx, in->cctx)
| 0-8 | ||||||||||||||||||||||||
87 | return never executed: 0;return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
88 | bl = EVP_CIPHER_CTX_block_size(in->cctx); | - | ||||||||||||||||||||||||
89 | memcpy(out->k1, in->k1, bl); | - | ||||||||||||||||||||||||
90 | memcpy(out->k2, in->k2, bl); | - | ||||||||||||||||||||||||
91 | memcpy(out->tbl, in->tbl, bl); | - | ||||||||||||||||||||||||
92 | memcpy(out->last_block, in->last_block, bl); | - | ||||||||||||||||||||||||
93 | out->nlast_block = in->nlast_block; | - | ||||||||||||||||||||||||
94 | return executed 8 times by 1 test: 1;return 1; Executed by:
executed 8 times by 1 test: return 1; Executed by:
| 8 | ||||||||||||||||||||||||
95 | } | - | ||||||||||||||||||||||||
96 | - | |||||||||||||||||||||||||
97 | int CMAC_Init(CMAC_CTX *ctx, const void *key, size_t keylen, | - | ||||||||||||||||||||||||
98 | const EVP_CIPHER *cipher, ENGINE *impl) | - | ||||||||||||||||||||||||
99 | { | - | ||||||||||||||||||||||||
100 | static const unsigned char zero_iv[32] = { 0 }; | - | ||||||||||||||||||||||||
101 | - | |||||||||||||||||||||||||
102 | if (!key
| 0-4 | ||||||||||||||||||||||||
103 | - | |||||||||||||||||||||||||
104 | if (ctx->nlast_block == -1
| 0-4 | ||||||||||||||||||||||||
105 | return never executed: 0;return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
106 | if (!EVP_EncryptInit_ex(ctx->cctx,
| 0-4 | ||||||||||||||||||||||||
107 | ((void *)0)
| 0-4 | ||||||||||||||||||||||||
108 | ,
| 0-4 | ||||||||||||||||||||||||
109 | ((void *)0)
| 0-4 | ||||||||||||||||||||||||
110 | ,
| 0-4 | ||||||||||||||||||||||||
111 | ((void *)0)
| 0-4 | ||||||||||||||||||||||||
112 | , zero_iv)
| 0-4 | ||||||||||||||||||||||||
113 | return never executed: 0;return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
114 | memset(ctx->tbl, 0, EVP_CIPHER_CTX_block_size(ctx->cctx)); | - | ||||||||||||||||||||||||
115 | ctx->nlast_block = 0; | - | ||||||||||||||||||||||||
116 | return executed 4 times by 1 test: 1;return 1; Executed by:
executed 4 times by 1 test: return 1; Executed by:
| 4 | ||||||||||||||||||||||||
117 | } | - | ||||||||||||||||||||||||
118 | - | |||||||||||||||||||||||||
119 | if (cipher
| 0-4 | ||||||||||||||||||||||||
120 | ((void *)0)
| 0-4 | ||||||||||||||||||||||||
121 | ,
| 0-4 | ||||||||||||||||||||||||
122 | ((void *)0)
| 0-4 | ||||||||||||||||||||||||
123 | )
| 0-4 | ||||||||||||||||||||||||
124 | return never executed: 0;return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
125 | - | |||||||||||||||||||||||||
126 | if (key
| 0-4 | ||||||||||||||||||||||||
127 | int bl; | - | ||||||||||||||||||||||||
128 | if (!EVP_CIPHER_CTX_cipher(ctx->cctx)
| 0-4 | ||||||||||||||||||||||||
129 | return never executed: 0;return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
130 | if (!EVP_CIPHER_CTX_set_key_length(ctx->cctx, keylen)
| 0-4 | ||||||||||||||||||||||||
131 | return never executed: 0;return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
132 | if (!EVP_EncryptInit_ex(ctx->cctx,
| 0-4 | ||||||||||||||||||||||||
133 | ((void *)0)
| 0-4 | ||||||||||||||||||||||||
134 | ,
| 0-4 | ||||||||||||||||||||||||
135 | ((void *)0)
| 0-4 | ||||||||||||||||||||||||
136 | , key, zero_iv)
| 0-4 | ||||||||||||||||||||||||
137 | return never executed: 0;return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
138 | bl = EVP_CIPHER_CTX_block_size(ctx->cctx); | - | ||||||||||||||||||||||||
139 | if (!EVP_Cipher(ctx->cctx, ctx->tbl, zero_iv, bl)
| 0-4 | ||||||||||||||||||||||||
140 | return never executed: 0;return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
141 | make_kn(ctx->k1, ctx->tbl, bl); | - | ||||||||||||||||||||||||
142 | make_kn(ctx->k2, ctx->k1, bl); | - | ||||||||||||||||||||||||
143 | OPENSSL_cleanse(ctx->tbl, bl); | - | ||||||||||||||||||||||||
144 | - | |||||||||||||||||||||||||
145 | if (!EVP_EncryptInit_ex(ctx->cctx,
| 0-4 | ||||||||||||||||||||||||
146 | ((void *)0)
| 0-4 | ||||||||||||||||||||||||
147 | ,
| 0-4 | ||||||||||||||||||||||||
148 | ((void *)0)
| 0-4 | ||||||||||||||||||||||||
149 | ,
| 0-4 | ||||||||||||||||||||||||
150 | ((void *)0)
| 0-4 | ||||||||||||||||||||||||
151 | , zero_iv)
| 0-4 | ||||||||||||||||||||||||
152 | return never executed: 0;return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
153 | - | |||||||||||||||||||||||||
154 | memset(ctx->tbl, 0, bl); | - | ||||||||||||||||||||||||
155 | ctx->nlast_block = 0; | - | ||||||||||||||||||||||||
156 | } executed 4 times by 1 test: end of block Executed by:
| 4 | ||||||||||||||||||||||||
157 | return executed 4 times by 1 test: 1;return 1; Executed by:
executed 4 times by 1 test: return 1; Executed by:
| 4 | ||||||||||||||||||||||||
158 | } | - | ||||||||||||||||||||||||
159 | - | |||||||||||||||||||||||||
160 | int CMAC_Update(CMAC_CTX *ctx, const void *in, size_t dlen) | - | ||||||||||||||||||||||||
161 | { | - | ||||||||||||||||||||||||
162 | const unsigned char *data = in; | - | ||||||||||||||||||||||||
163 | size_t bl; | - | ||||||||||||||||||||||||
164 | if (ctx->nlast_block == -1
| 0-4 | ||||||||||||||||||||||||
165 | return never executed: 0;return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
166 | if (dlen == 0
| 1-3 | ||||||||||||||||||||||||
167 | return executed 1 time by 1 test: 1;return 1; Executed by:
executed 1 time by 1 test: return 1; Executed by:
| 1 | ||||||||||||||||||||||||
168 | bl = EVP_CIPHER_CTX_block_size(ctx->cctx); | - | ||||||||||||||||||||||||
169 | - | |||||||||||||||||||||||||
170 | if (ctx->nlast_block > 0
| 0-3 | ||||||||||||||||||||||||
171 | size_t nleft; | - | ||||||||||||||||||||||||
172 | nleft = bl - ctx->nlast_block; | - | ||||||||||||||||||||||||
173 | if (dlen < nleft
| 0 | ||||||||||||||||||||||||
174 | nleft = dlen; never executed: nleft = dlen; | 0 | ||||||||||||||||||||||||
175 | memcpy(ctx->last_block + ctx->nlast_block, data, nleft); | - | ||||||||||||||||||||||||
176 | dlen -= nleft; | - | ||||||||||||||||||||||||
177 | ctx->nlast_block += nleft; | - | ||||||||||||||||||||||||
178 | - | |||||||||||||||||||||||||
179 | if (dlen == 0
| 0 | ||||||||||||||||||||||||
180 | return never executed: 1;return 1; never executed: return 1; | 0 | ||||||||||||||||||||||||
181 | data += nleft; | - | ||||||||||||||||||||||||
182 | - | |||||||||||||||||||||||||
183 | if (!EVP_Cipher(ctx->cctx, ctx->tbl, ctx->last_block, bl)
| 0 | ||||||||||||||||||||||||
184 | return never executed: 0;return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
185 | } never executed: end of block | 0 | ||||||||||||||||||||||||
186 | - | |||||||||||||||||||||||||
187 | while (dlen > bl
| 3-6 | ||||||||||||||||||||||||
188 | if (!EVP_Cipher(ctx->cctx, ctx->tbl, data, bl)
| 0-6 | ||||||||||||||||||||||||
189 | return never executed: 0;return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
190 | dlen -= bl; | - | ||||||||||||||||||||||||
191 | data += bl; | - | ||||||||||||||||||||||||
192 | } executed 6 times by 1 test: end of block Executed by:
| 6 | ||||||||||||||||||||||||
193 | - | |||||||||||||||||||||||||
194 | memcpy(ctx->last_block, data, dlen); | - | ||||||||||||||||||||||||
195 | ctx->nlast_block = dlen; | - | ||||||||||||||||||||||||
196 | return executed 3 times by 1 test: 1;return 1; Executed by:
executed 3 times by 1 test: return 1; Executed by:
| 3 | ||||||||||||||||||||||||
197 | - | |||||||||||||||||||||||||
198 | } | - | ||||||||||||||||||||||||
199 | - | |||||||||||||||||||||||||
200 | int CMAC_Final(CMAC_CTX *ctx, unsigned char *out, size_t *poutlen) | - | ||||||||||||||||||||||||
201 | { | - | ||||||||||||||||||||||||
202 | int i, bl, lb; | - | ||||||||||||||||||||||||
203 | if (ctx->nlast_block == -1
| 0-8 | ||||||||||||||||||||||||
204 | return never executed: 0;return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
205 | bl = EVP_CIPHER_CTX_block_size(ctx->cctx); | - | ||||||||||||||||||||||||
206 | *poutlen = (size_t)bl; | - | ||||||||||||||||||||||||
207 | if (!out
| 4 | ||||||||||||||||||||||||
208 | return executed 4 times by 1 test: 1;return 1; Executed by:
executed 4 times by 1 test: return 1; Executed by:
| 4 | ||||||||||||||||||||||||
209 | lb = ctx->nlast_block; | - | ||||||||||||||||||||||||
210 | - | |||||||||||||||||||||||||
211 | if (lb == bl
| 1-3 | ||||||||||||||||||||||||
212 | for (i = 0; i < bl
| 3-40 | ||||||||||||||||||||||||
213 | out[i] = ctx->last_block[i] ^ ctx->k1[i]; executed 40 times by 1 test: out[i] = ctx->last_block[i] ^ ctx->k1[i]; Executed by:
| 40 | ||||||||||||||||||||||||
214 | } executed 3 times by 1 test: else {end of block Executed by:
| 3 | ||||||||||||||||||||||||
215 | ctx->last_block[lb] = 0x80; | - | ||||||||||||||||||||||||
216 | if (bl - lb > 1
| 0-1 | ||||||||||||||||||||||||
217 | memset(ctx->last_block + lb + 1, 0, bl - lb - 1); executed 1 time by 1 test: memset(ctx->last_block + lb + 1, 0, bl - lb - 1); Executed by:
| 1 | ||||||||||||||||||||||||
218 | for (i = 0; i < bl
| 1-16 | ||||||||||||||||||||||||
219 | out[i] = ctx->last_block[i] ^ ctx->k2[i]; executed 16 times by 1 test: out[i] = ctx->last_block[i] ^ ctx->k2[i]; Executed by:
| 16 | ||||||||||||||||||||||||
220 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||||||||||||||
221 | if (!EVP_Cipher(ctx->cctx, out, out, bl)
| 0-4 | ||||||||||||||||||||||||
222 | OPENSSL_cleanse(out, bl); | - | ||||||||||||||||||||||||
223 | return never executed: 0;return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
224 | } | - | ||||||||||||||||||||||||
225 | return executed 4 times by 1 test: 1;return 1; Executed by:
executed 4 times by 1 test: return 1; Executed by:
| 4 | ||||||||||||||||||||||||
226 | } | - | ||||||||||||||||||||||||
227 | - | |||||||||||||||||||||||||
228 | int CMAC_resume(CMAC_CTX *ctx) | - | ||||||||||||||||||||||||
229 | { | - | ||||||||||||||||||||||||
230 | if (ctx->nlast_block == -1
| 0 | ||||||||||||||||||||||||
231 | return never executed: 0;return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
232 | - | |||||||||||||||||||||||||
233 | - | |||||||||||||||||||||||||
234 | - | |||||||||||||||||||||||||
235 | - | |||||||||||||||||||||||||
236 | - | |||||||||||||||||||||||||
237 | - | |||||||||||||||||||||||||
238 | - | |||||||||||||||||||||||||
239 | return never executed: EVP_EncryptInit_ex(ctx->cctx, return EVP_EncryptInit_ex(ctx->cctx, ((void *)0) , ((void *)0) , ((void *)0) , ctx->tbl); never executed: return EVP_EncryptInit_ex(ctx->cctx, ((void *)0) , ((void *)0) , ((void *)0) , ctx->tbl); | 0 | ||||||||||||||||||||||||
240 | ((void *)0) never executed: return EVP_EncryptInit_ex(ctx->cctx, ((void *)0) , ((void *)0) , ((void *)0) , ctx->tbl); | 0 | ||||||||||||||||||||||||
241 | , never executed: return EVP_EncryptInit_ex(ctx->cctx, ((void *)0) , ((void *)0) , ((void *)0) , ctx->tbl); | 0 | ||||||||||||||||||||||||
242 | ((void *)0) never executed: return EVP_EncryptInit_ex(ctx->cctx, ((void *)0) , ((void *)0) , ((void *)0) , ctx->tbl); | 0 | ||||||||||||||||||||||||
243 | , never executed: return EVP_EncryptInit_ex(ctx->cctx, ((void *)0) , ((void *)0) , ((void *)0) , ctx->tbl); | 0 | ||||||||||||||||||||||||
244 | ((void *)0) never executed: return EVP_EncryptInit_ex(ctx->cctx, ((void *)0) , ((void *)0) , ((void *)0) , ctx->tbl); | 0 | ||||||||||||||||||||||||
245 | , ctx->tbl); never executed: return EVP_EncryptInit_ex(ctx->cctx, ((void *)0) , ((void *)0) , ((void *)0) , ctx->tbl); | 0 | ||||||||||||||||||||||||
246 | } | - | ||||||||||||||||||||||||
Switch to Source code | Preprocessed file |