1 /* 2 * Copyright 2017-2023 The OpenSSL Project Authors. All Rights Reserved. 3 * Copyright 2014 Cryptography Research, Inc. 4 * 5 * Licensed under the Apache License 2.0 (the "License"). You may not use 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 11 */ 12 13 #include "internal/e_os.h" 14 #include <openssl/macros.h> 15 #include "internal/numbers.h" 16 17 #ifdef UINT128_MAX 18 /* We have support for 128 bit ints, so do nothing here */ 19 NON_EMPTY_TRANSLATION_UNIT 20 #else 21 22 # include "../field.h" 23 24 void ossl_gf_mul(gf_s * RESTRICT cs, const gf as, const gf bs) 25 { 26 const uint32_t *a = as->limb, *b = bs->limb; 27 uint32_t *c = cs->limb; 28 uint64_t accum0 = 0, accum1 = 0, accum2 = 0; 29 uint32_t mask = (1 << 28) - 1; 30 uint32_t aa[8], bb[8]; 31 int i, j; 32 33 for (i = 0; i < 8; i++) { 34 aa[i] = a[i] + a[i + 8]; 35 bb[i] = b[i] + b[i + 8]; 36 } 37 38 for (j = 0; j < 8; j++) { 39 accum2 = 0; 40 for (i = 0; i < j + 1; i++) { 41 accum2 += widemul(a[j - i], b[i]); 42 accum1 += widemul(aa[j - i], bb[i]); 43 accum0 += widemul(a[8 + j - i], b[8 + i]); 44 } 45 accum1 -= accum2; 46 accum0 += accum2; 47 accum2 = 0; 48 for (i = j + 1; i < 8; i++) { 49 accum0 -= widemul(a[8 + j - i], b[i]); 50 accum2 += widemul(aa[8 + j - i], bb[i]); 51 accum1 += widemul(a[16 + j - i], b[8 + i]); 52 } 53 accum1 += accum2; 54 accum0 += accum2; 55 c[j] = ((uint32_t)(accum0)) & mask; 56 c[j + 8] = ((uint32_t)(accum1)) & mask; 57 accum0 >>= 28; 58 accum1 >>= 28; 59 } 60 61 accum0 += accum1; 62 accum0 += c[8]; 63 accum1 += c[0]; 64 c[8] = ((uint32_t)(accum0)) & mask; 65 c[0] = ((uint32_t)(accum1)) & mask; 66 67 accum0 >>= 28; 68 accum1 >>= 28; 69 c[9] += ((uint32_t)(accum0)); 70 c[1] += ((uint32_t)(accum1)); 71 } 72 73 void ossl_gf_mulw_unsigned(gf_s * RESTRICT cs, const gf as, uint32_t b) 74 { 75 const uint32_t *a = as->limb; 76 uint32_t *c = cs->limb; 77 uint64_t accum0 = 0, accum8 = 0; 78 uint32_t mask = (1 << 28) - 1; 79 int i; 80 81 assert(b <= mask); 82 83 for (i = 0; i < 8; i++) { 84 accum0 += widemul(b, a[i]); 85 accum8 += widemul(b, a[i + 8]); 86 c[i] = accum0 & mask; 87 accum0 >>= 28; 88 c[i + 8] = accum8 & mask; 89 accum8 >>= 28; 90 } 91 92 accum0 += accum8 + c[8]; 93 c[8] = ((uint32_t)accum0) & mask; 94 c[9] += (uint32_t)(accum0 >> 28); 95 96 accum8 += c[0]; 97 c[0] = ((uint32_t)accum8) & mask; 98 c[1] += (uint32_t)(accum8 >> 28); 99 } 100 101 void ossl_gf_sqr(gf_s * RESTRICT cs, const gf as) 102 { 103 ossl_gf_mul(cs, as, as); /* Performs better with a dedicated square */ 104 } 105 #endif 106