1 /* Copyright (c) 2001, Matej Pfajfar.
2 * Copyright (c) 2001-2004, Roger Dingledine.
3 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4 * Copyright (c) 2007-2021, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
9 * \brief Use NSS to implement AES_CTR.
13 #include "lib/crypt_ops/aes.h"
14 #include "lib/crypt_ops/crypto_nss_mgt.h"
15 #include "lib/crypt_ops/crypto_util.h"
16 #include "lib/log/util_bug.h"
18 DISABLE_GCC_WARNING("-Wstrict-prototypes")
21 ENABLE_GCC_WARNING("-Wstrict-prototypes")
24 aes_new_cipher(const uint8_t *key
, const uint8_t *iv
,
27 const CK_MECHANISM_TYPE ckm
= CKM_AES_CTR
;
28 SECItem keyItem
= { .type
= siBuffer
,
29 .data
= (unsigned char *)key
,
30 .len
= (key_bits
/ 8) };
31 CK_AES_CTR_PARAMS params
;
32 params
.ulCounterBits
= 128;
33 memcpy(params
.cb
, iv
, 16);
34 SECItem ivItem
= { .type
= siBuffer
,
35 .data
= (unsigned char *)¶ms
,
36 .len
= sizeof(params
) };
37 PK11SlotInfo
*slot
= NULL
;
38 PK11SymKey
*keyObj
= NULL
;
39 SECItem
*ivObj
= NULL
;
40 PK11Context
*result
= NULL
;
42 slot
= PK11_GetBestSlot(ckm
, NULL
);
46 keyObj
= PK11_ImportSymKey(slot
, ckm
, PK11_OriginUnwrap
,
47 CKA_ENCRYPT
, &keyItem
, NULL
);
51 ivObj
= PK11_ParamFromIV(ckm
, &ivItem
);
55 PORT_SetError(SEC_ERROR_IO
);
56 result
= PK11_CreateContextBySymKey(ckm
, CKA_ENCRYPT
, keyObj
, ivObj
);
59 memwipe(¶ms
, 0, sizeof(params
));
61 SECITEM_FreeItem(ivObj
, PR_TRUE
);
63 PK11_FreeSymKey(keyObj
);
68 return (aes_cnt_cipher_t
*)result
;
72 aes_cipher_free_(aes_cnt_cipher_t
*cipher
)
76 PK11_DestroyContext((PK11Context
*) cipher
, PR_TRUE
);
80 aes_crypt_inplace(aes_cnt_cipher_t
*cipher
, char *data_
, size_t len_
)
82 tor_assert(len_
<= INT_MAX
);
85 PK11Context
*ctx
= (PK11Context
*)cipher
;
86 unsigned char *data
= (unsigned char *)data_
;
90 s
= PK11_CipherOp(ctx
, data
, &result_len
, len
, data
, len
);
91 tor_assert(s
== SECSuccess
);
92 tor_assert(result_len
== len
);
96 evaluate_evp_for_aes(int force_value
)
103 evaluate_ctr_for_aes(void)