1 C x86_64/poly1305-internal.asm
4 Copyright (C) 2013 Niels Möller
6 This file is part of GNU Nettle.
8 GNU Nettle is free software: you can redistribute it and/or
9 modify it under the terms of either:
11 * the GNU Lesser General Public License as published by the Free
12 Software Foundation; either version 3 of the License, or (at your
13 option) any later version.
17 * the GNU General Public License as published by the Free
18 Software Foundation; either version 2 of the License, or (at your
19 option) any later version.
21 or both in parallel, as here.
23 GNU Nettle is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 General Public License for more details.
28 You should have received copies of the GNU General Public License and
29 the GNU Lesser General Public License along with this program. If
30 not, see http://www.gnu.org/licenses/.
33 .file "poly1305-internal.asm"
35 C Registers mainly used by poly1305_block
44 C poly1305_set_key(struct poly1305_ctx *ctx, const uint8_t key[16])
51 PROLOGUE(nettle_poly1305_set_key)
53 mov $0x0ffffffc0fffffff, %r8
60 mov %rax, P1305_R1 (CTX)
63 mov %rax, P1305_S1 (CTX)
64 xor XREG(%rax), XREG(%rax)
65 mov %rax, P1305_H0 (CTX)
66 mov %rax, P1305_H1 (CTX)
67 mov XREG(%rax), P1305_H2 (CTX)
72 EPILOGUE(nettle_poly1305_set_key)
74 C 64-bit multiplication mod 2^130 - 5
76 C (x_0 + B x_1 + B^2 x_2) * (r_0 + B r_1) =
84 C Then r_1 B^2 = r_1/4 (2^130) = 5/4 r_1.
85 C and r_1 B^3 = 5/4 B r_1
88 C x_0 r_0 + x_1 (5/4 r_1) + B (x_0 r_1 + x_1 r_0 + x_2 5/4 r_1 + B x_2 r_0)
97 C _poly1305_block (struct poly1305_ctx *ctx, const uint8_t m[16], unsigned hi)
99 PROLOGUE(_nettle_poly1305_block)
103 mov XREG(%rdx), XREG(T2)
106 C Inputs: CTX, T0, T1, T2,
107 C Outputs: H0, H1, H2, stored into the context.
109 add P1305_H0 (CTX), T0
110 adc P1305_H1 (CTX), T1
111 adc P1305_H2 (CTX), XREG(T2)
112 mov P1305_R0 (CTX), %rax
116 mov P1305_S1 (CTX), %rax C 5/4 r1
120 imul P1305_R0 (CTX), T2 C x2*r0
123 mov P1305_R0 (CTX), %rax
127 mov P1305_R1 (CTX), %rax
138 mov H0, P1305_H0 (CTX)
139 mov H1, P1305_H1 (CTX)
140 mov XREG(T2), P1305_H2 (CTX)
143 EPILOGUE(_nettle_poly1305_block)
145 C poly1305_digest (struct poly1305_ctx *ctx, uint8_t *s)
150 PROLOGUE(nettle_poly1305_digest)
153 mov P1305_H0 (CTX), H0
154 mov P1305_H1 (CTX), H1
155 mov P1305_H2 (CTX), XREG(H2)
156 mov XREG(H2), XREG(%rax)
164 C Use %rax instead of %rsi
166 C Add 5, use result if >= 2^130
179 xor XREG(%rax), XREG(%rax)
180 mov %rax, P1305_H0 (CTX)
181 mov %rax, P1305_H1 (CTX)
182 mov XREG(%rax), P1305_H2 (CTX)