]>
Commit | Line | Data |
---|---|---|
1308e022 | 1 | /* |
f53c7764 | 2 | * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved. |
1308e022 MC |
3 | * Copyright 2014 Cryptography Research, Inc. |
4 | * | |
a7f182b7 | 5 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
1308e022 MC |
6 | * this file except in compliance with the License. You can obtain a copy |
7 | * in the file LICENSE in the source distribution or at | |
8 | * https://www.openssl.org/source/license.html | |
9 | * | |
10 | * Originally written by Mike Hamburg | |
7324473f MC |
11 | */ |
12 | ||
ae4186b0 DMSP |
13 | #ifndef OSSL_CRYPTO_EC_CURVE448_WORD_H |
14 | # define OSSL_CRYPTO_EC_CURVE448_WORD_H | |
205fd638 MC |
15 | |
16 | # include <string.h> | |
205fd638 | 17 | # include <assert.h> |
c1f15b76 | 18 | # include <stdlib.h> |
205fd638 MC |
19 | # include <openssl/e_os2.h> |
20 | # include "arch_intrinsics.h" | |
205fd638 | 21 | # include "curve448utils.h" |
205fd638 | 22 | |
205fd638 MC |
23 | # if (ARCH_WORD_BITS == 64) |
24 | typedef uint64_t word_t, mask_t; | |
25 | typedef __uint128_t dword_t; | |
26 | typedef int32_t hsword_t; | |
27 | typedef int64_t sword_t; | |
28 | typedef __int128_t dsword_t; | |
29 | # elif (ARCH_WORD_BITS == 32) | |
30 | typedef uint32_t word_t, mask_t; | |
31 | typedef uint64_t dword_t; | |
32 | typedef int16_t hsword_t; | |
33 | typedef int32_t sword_t; | |
34 | typedef int64_t dsword_t; | |
35 | # else | |
aeeef83c | 36 | # error "For now, we only support 32- and 64-bit architectures." |
205fd638 MC |
37 | # endif |
38 | ||
39 | /* | |
40 | * Scalar limbs are keyed off of the API word size instead of the arch word | |
41 | * size. | |
42 | */ | |
aeeef83c | 43 | # if C448_WORD_BITS == 64 |
205fd638 | 44 | # define SC_LIMB(x) (x) |
aeeef83c | 45 | # elif C448_WORD_BITS == 32 |
c1f15b76 | 46 | # define SC_LIMB(x) ((uint32_t)(x)),((x) >> 32) |
205fd638 | 47 | # else |
aeeef83c | 48 | # error "For now we only support 32- and 64-bit architectures." |
205fd638 MC |
49 | # endif |
50 | ||
205fd638 | 51 | /* |
aeeef83c | 52 | * The plan on booleans: The external interface uses c448_bool_t, but this |
205fd638 MC |
53 | * might be a different size than our particular arch's word_t (and thus |
54 | * mask_t). Also, the caller isn't guaranteed to pass it as nonzero. So | |
55 | * bool_to_mask converts word sizes and checks nonzero. On the flip side, | |
56 | * mask_t is always -1 or 0, but it might be a different size than | |
aeeef83c MC |
57 | * c448_bool_t. On the third hand, we have success vs boolean types, but |
58 | * that's handled in common.h: it converts between c448_bool_t and | |
59 | * c448_error_t. | |
7324473f | 60 | */ |
aeeef83c | 61 | static ossl_inline c448_bool_t mask_to_bool(mask_t m) |
205fd638 | 62 | { |
aeeef83c | 63 | return (c448_sword_t)(sword_t)m; |
7324473f MC |
64 | } |
65 | ||
aeeef83c | 66 | static ossl_inline mask_t bool_to_mask(c448_bool_t m) |
205fd638 | 67 | { |
7324473f MC |
68 | /* On most arches this will be optimized to a simple cast. */ |
69 | mask_t ret = 0; | |
094c071c | 70 | unsigned int i; |
aeeef83c | 71 | unsigned int limit = sizeof(c448_bool_t) / sizeof(mask_t); |
8d55f844 | 72 | |
205fd638 MC |
73 | if (limit < 1) |
74 | limit = 1; | |
8d55f844 | 75 | for (i = 0; i < limit; i++) |
205fd638 | 76 | ret |= ~word_is_zero(m >> (i * 8 * sizeof(word_t))); |
8d55f844 | 77 | |
7324473f MC |
78 | return ret; |
79 | } | |
80 | ||
ae4186b0 | 81 | #endif /* OSSL_CRYPTO_EC_CURVE448_WORD_H */ |