Skip to content

Commit 8066826

Browse files
author
Shubham Chaturvedi
committed
feat: Primitives for Go
1 parent 74b4972 commit 8066826

File tree

14 files changed

+866
-0
lines changed

14 files changed

+866
-0
lines changed

AwsCryptographyPrimitives/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,8 @@ PYTHON_MODULE_NAME=aws_cryptography_primitives
4949

5050
TRANSLATION_RECORD_PYTHON := \
5151
--translation-record ../StandardLibrary/runtimes/python/src/smithy_dafny_standard_library/internaldafny/generated/dafny_src-py.dtr
52+
53+
GO_MODULE_NAME="github.com/aws/aws-cryptographic-material-providers-library/primitives"
54+
55+
TRANSLATION_RECORD_GO := \
56+
StandardLibrary/runtimes/go/ImplementationFromDafny-go/ImplementationFromDafny-go.dtr
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package AESEncryption
2+
3+
import (
4+
"crypto/aes"
5+
"crypto/cipher"
6+
"fmt"
7+
8+
"github.com/aws/aws-cryptographic-material-providers-library/primitives/AwsCryptographyPrimitivesTypes"
9+
"github.com/dafny-lang/DafnyRuntimeGo/v4/dafny"
10+
"github.com/dafny-lang/DafnyStandardLibGo/Wrappers"
11+
)
12+
13+
var m_AESEncryption struct {
14+
AES_GCM CompanionStruct_Default___
15+
}
16+
17+
func (CompanionStruct_Default___) AESDecryptExtern(algo AwsCryptographyPrimitivesTypes.AES__GCM, key dafny.Sequence, cipherText dafny.Sequence, authTag dafny.Sequence, iv dafny.Sequence, aad dafny.Sequence) Wrappers.Result {
18+
keyBytes := dafny.ToByteArray(key)
19+
cipherTextBytes := dafny.ToByteArray(cipherText)
20+
authTagBytes := dafny.ToByteArray(authTag)
21+
ivBytes := dafny.ToByteArray(iv)
22+
aadBytes := dafny.ToByteArray(aad)
23+
24+
if algo.Dtor_keyLength() != int32(len(keyBytes)) {
25+
return Wrappers.Companion_Result_.Create_Failure_(AwsCryptographyPrimitivesTypes.Companion_Error_.Create_AwsCryptographicPrimitivesError_(dafny.SeqOfChars([]dafny.Char(fmt.Errorf("algorithm key length %d doesn't match actual key length %d ", algo.Dtor_keyLength(), len(keyBytes)).Error())...)))
26+
27+
}
28+
29+
if algo.Dtor_ivLength() != int32(len(ivBytes)) {
30+
return Wrappers.Companion_Result_.Create_Failure_(AwsCryptographyPrimitivesTypes.Companion_Error_.Create_AwsCryptographicPrimitivesError_(dafny.SeqOfChars([]dafny.Char(fmt.Errorf("algorithm iv length %d doesn't match actual iv length %d ", algo.Dtor_ivLength(), len(ivBytes)).Error())...)))
31+
}
32+
33+
if algo.Dtor_tagLength() != int32(len(authTagBytes)) {
34+
return Wrappers.Companion_Result_.Create_Failure_(AwsCryptographyPrimitivesTypes.Companion_Error_.Create_AwsCryptographicPrimitivesError_(dafny.SeqOfChars([]dafny.Char(fmt.Errorf("algorithm tag length %d doesn't match actual tag length %d ", algo.Dtor_tagLength(), len(authTagBytes)).Error())...)))
35+
}
36+
37+
block, err := aes.NewCipher(keyBytes)
38+
if err != nil {
39+
return Wrappers.Companion_Result_.Create_Failure_(AwsCryptographyPrimitivesTypes.Companion_Error_.Create_AwsCryptographicPrimitivesError_(dafny.SeqOfChars([]dafny.Char(err.Error())...)))
40+
}
41+
42+
if algo.Is_AES__GCM() {
43+
gcm, err := cipher.NewGCM(block)
44+
if err != nil {
45+
return Wrappers.Companion_Result_.Create_Failure_(AwsCryptographyPrimitivesTypes.Companion_Error_.Create_AwsCryptographicPrimitivesError_(dafny.SeqOfChars([]dafny.Char(err.Error())...)))
46+
}
47+
48+
plaintext, err := gcm.Open(nil, ivBytes, append(cipherTextBytes, authTagBytes...), aadBytes)
49+
if err != nil {
50+
return Wrappers.Companion_Result_.Create_Failure_(AwsCryptographyPrimitivesTypes.Companion_Error_.Create_AwsCryptographicPrimitivesError_(dafny.SeqOfChars([]dafny.Char(err.Error())...)))
51+
}
52+
return Wrappers.Companion_Result_.Create_Success_(dafny.SeqOfBytes(plaintext))
53+
}
54+
return Wrappers.Companion_Result_.Create_Failure_(false)
55+
}
56+
57+
func (CompanionStruct_Default___) AESEncryptExtern(algo AwsCryptographyPrimitivesTypes.AES__GCM, iv dafny.Sequence, key dafny.Sequence, msg dafny.Sequence, aad dafny.Sequence) Wrappers.Result {
58+
keyBytes := dafny.ToByteArray(key)
59+
ivBytes := dafny.ToByteArray(iv)
60+
aadBytes := dafny.ToByteArray(aad)
61+
62+
if algo.Dtor_keyLength() != int32(len(keyBytes)) {
63+
return Wrappers.Companion_Result_.Create_Failure_(AwsCryptographyPrimitivesTypes.Companion_Error_.Create_AwsCryptographicPrimitivesError_(dafny.SeqOfChars([]dafny.Char(fmt.Errorf("algorithm key length %d doesn't match actual key length %d ", algo.Dtor_keyLength(), len(keyBytes)).Error())...)))
64+
65+
}
66+
67+
if algo.Dtor_ivLength() != int32(len(ivBytes)) {
68+
return Wrappers.Companion_Result_.Create_Failure_(AwsCryptographyPrimitivesTypes.Companion_Error_.Create_AwsCryptographicPrimitivesError_(dafny.SeqOfChars([]dafny.Char(fmt.Errorf("algorithm iv length %d doesn't match actual iv length %d ", algo.Dtor_ivLength(), len(ivBytes)).Error())...)))
69+
}
70+
71+
block, err := aes.NewCipher(keyBytes)
72+
if err != nil {
73+
return Wrappers.Companion_Result_.Create_Failure_(AwsCryptographyPrimitivesTypes.Companion_Error_.Create_AwsCryptographicPrimitivesError_(dafny.SeqOfChars([]dafny.Char(err.Error())...)))
74+
}
75+
76+
if algo.Is_AES__GCM() {
77+
gcm, err := cipher.NewGCM(block)
78+
if err != nil {
79+
return Wrappers.Companion_Result_.Create_Failure_(AwsCryptographyPrimitivesTypes.Companion_Error_.Create_AwsCryptographicPrimitivesError_(dafny.SeqOfChars([]dafny.Char(err.Error())...)))
80+
}
81+
82+
cipherText := gcm.Seal(nil, ivBytes, dafny.ToByteArray(msg), aadBytes)
83+
if cipherText == nil {
84+
return Wrappers.Companion_Result_.Create_Failure_(AwsCryptographyPrimitivesTypes.Companion_Error_.Create_AwsCryptographicPrimitivesError_(dafny.SeqOfChars([]dafny.Char(fmt.Errorf("failed to do AES_GCM Encrypt with the given parameters").Error())...)))
85+
}
86+
return Wrappers.Companion_Result_.Create_Success_(AwsCryptographyPrimitivesTypes.Companion_AESEncryptOutput_.Create_AESEncryptOutput_(dafny.SeqOfBytes(cipherText[:len(cipherText)-gcm.Overhead()]), dafny.SeqOfBytes(cipherText[len(cipherText)-gcm.Overhead():])))
87+
}
88+
return Wrappers.Companion_Result_.Create_Failure_(false)
89+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package AesKdfCtr
2+
3+
import (
4+
"crypto/aes"
5+
"crypto/cipher"
6+
7+
dafny "github.com/dafny-lang/DafnyRuntimeGo/v4/dafny"
8+
"github.com/dafny-lang/DafnyStandardLibGo/Wrappers"
9+
)
10+
11+
func AesKdfCtrStream(nonce dafny.Sequence, key dafny.Sequence, length uint32) Wrappers.Result {
12+
13+
block, err := aes.NewCipher(dafny.ToByteArray(key))
14+
15+
if err != nil {
16+
panic(err)
17+
}
18+
19+
stream := cipher.NewCTR(block, dafny.ToByteArray(nonce))
20+
21+
result := make([]byte, length)
22+
23+
stream.XORKeyStream(result, make([]byte, length))
24+
return Wrappers.Companion_Result_.Create_Success_(dafny.SeqOfBytes(result))
25+
}

0 commit comments

Comments
 (0)