arcfour-crypt.asm 2.16 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
C -*- mode: asm; asm-comment-char: ?C; -*-  
C nettle, low-level cryptographics library
C 
C Copyright (C) 2002, 2005 Niels Mller
C  
C The nettle library is free software; you can redistribute it and/or modify
C it under the terms of the GNU Lesser General Public License as published by
C the Free Software Foundation; either version 2.1 of the License, or (at your
C option) any later version.
C 
C The nettle library is distributed in the hope that it will be useful, but
C WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
C or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
C License for more details.
C 
C You should have received a copy of the GNU Lesser General Public License
C along with the nettle library; see the file COPYING.LIB.  If not, write to
C the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
C MA 02111-1307, USA.

C Registers

define(<CTX>,	<%i0>)
define(<LENGTH>,<%i1>)
define(<DST>,	<%i2>)
define(<SRC>,	<%i3>)

define(<I>,	<%i4>)
define(<J>,	<%i5>)
define(<SI>,	<%g1>)
define(<SJ>,	<%g2>)
define(<TMP>,	<%g3>)

C	FIXME: Consider using the callers window

define(<FRAME_SIZE>, 104)

	.file "arcfour-crypt.asm"

	C arcfour_crypt(struct arcfour_ctx *ctx,
	C               unsigned length, uint8_t *dst,
	C               const uint8_t *src)

	.section	".text"
	.align 16
	.proc	020
	
PROLOGUE(nettle_arcfour_crypt)

	save	%sp, -FRAME_SIZE, %sp
	cmp	LENGTH, 0
	be	.Lend
	
	C	Load both I and J
55
	lduh	[CTX + ARCFOUR_I], I
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
	and	I, 0xff, J
	srl	I, 8, I

.Loop:	
	add	I, 1, I
	and	I, 0xff, I
	ldub	[CTX + I], SI
	add	J, SI, J
	and	J, 0xff, J
	ldub	[CTX + J], SJ
	stb	SJ, [CTX + I]
	stb	SI, [CTX + J]
	add	SI, SJ, SI
	and	SI, 0xff, SI
	ldub	[CTX + SI], SI
	ldub	[SRC], TMP
	xor	TMP, SI, TMP
	stb	TMP, [DST]
	subcc	LENGTH,1,LENGTH
	add	SRC, 1, SRC
	bne	.Loop
	add	DST, 1, DST

	C	Save back I and J	
	sll	I, 8, I
	or	I, J, I
82
	stuh	I, [CTX + ARCFOUR_I]
83
84
85
86
87
88
89
90
91
92

.Lend:
	ret
	restore

EPILOGUE(nettle_arcfour_crypt)

C Some stats from adriana.lysator.liu.se (SS1000$, 85 MHz), for AES 128

C 1:	nettle-1.13 C-code
93
94
C 2:	First working version of the assembler code
	
95
C	MB/s	cycles/byte	Code size (bytes)
96
97
C 1:	6.6	12.4		132
C 2:	5.6	14.5		116