|
| 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 | +} |
0 commit comments